Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / protocols / zephyr / ZParseNot.c
bloba3e157f005cee84f1fe536e24626fb7cd7efdc9d
1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the ZParseNotice function.
4 * Created by: Robert French
6 * Copyright (c) 1987,1991 by the Massachusetts Institute of Technology.
7 * For copying and distribution information, see the file
8 * "mit-copyright.h".
9 */
11 #include "internal.h"
13 /* Assume that strlen is efficient on this machine... */
14 #define next_field(ptr) ptr += strlen (ptr) + 1
16 #if defined (__GNUC__) && defined (__vax__)
17 #undef next_field
18 static __inline__ char * Istrend (char *str) {
20 * This should be faster on VAX models outside the 2 series. Don't
21 * use it if you are using MicroVAX 2 servers. If you are using a
22 * VS2 server, use something like
23 * #define next_field(ptr) while(*ptr++)
24 * instead of this code.
26 * This requires use of GCC to get the optimized code, but
27 * everybody uses GCC, don't they? :-)
29 register char *str2 asm ("r1");
30 /* Assumes that no field is longer than 64K.... */
31 asm ("locc $0,$65535,(%1)" : "=r" (str2) : "r" (str) : "r0");
32 return str2;
34 #define next_field(ptr) ptr = Istrend (ptr) + 1
35 #endif
37 #ifdef mips
38 #undef next_field
40 * The compiler doesn't optimize this macro as well as it does the
41 * following function.
43 #define next_fieldXXX(ptr) do{register unsigned c1,c2;c1= *ptr; \
44 while((ptr++,c2= *ptr,c1)&&(ptr++,c1= *ptr,c2));}while(0)
45 static char *next_field_1 (s) char *s; {
47 * Calling overhead is still present, but this routine is faster
48 * than strlen, and doesn't bother with some of the other math
49 * that we'd just have to undo later anyways.
51 register unsigned c1 = *s, c2;
52 while (1) {
53 s++; c2 = *s; if (c1 == 0) break;
54 s++; c1 = *s; if (c2 == 0) break;
55 s++; c2 = *s; if (c1 == 0) break;
56 s++; c1 = *s; if (c2 == 0) break;
58 return s;
60 #define next_field(ptr) ptr=next_field_1(ptr)
61 #endif
63 Code_t ZParseNotice(buffer, len, notice)
64 char *buffer;
65 int len;
66 ZNotice_t *notice;
68 char *ptr, *end;
69 unsigned long temp;
70 int maj, numfields, i;
72 #ifdef __LINE__
73 int lineno;
74 /* Note: This definition of BAD eliminates lint and compiler
75 * complains about the "while (0)", but require that the macro not
76 * be used as the "then" part of an "if" statement that also has
77 * an "else" clause.
79 #define BAD_PACKET {lineno=__LINE__;goto badpkt;}
80 /* This one gets lint/compiler complaints. */
81 /*#define BAD do{lineno=__LINE__;goto badpkt;}while(0)*/
82 #else
83 #define BAD_PACKET goto badpkt
84 #endif
86 (void) memset((char *)notice, 0, sizeof(ZNotice_t));
88 ptr = buffer;
89 end = buffer+len;
91 notice->z_packet = buffer;
93 notice->z_version = ptr;
94 if (strncmp(ptr, ZVERSIONHDR, sizeof(ZVERSIONHDR) - 1))
95 return (ZERR_VERS);
96 ptr += sizeof(ZVERSIONHDR) - 1;
97 if (!*ptr) {
98 #ifdef Z_DEBUG
99 Z_debug ("ZParseNotice: null version string");
100 #endif
101 return ZERR_BADPKT;
103 maj = atoi(ptr);
104 if (maj != ZVERSIONMAJOR)
105 return (ZERR_VERS);
106 next_field (ptr);
108 if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
109 BAD_PACKET;
110 numfields = temp;
111 next_field (ptr);
113 /*XXX 3 */
114 numfields -= 2; /* numfields, version, and checksum */
115 if (numfields < 0) {
116 #ifdef __LINE__
117 lineno = __LINE__;
118 badpkt:
119 #ifdef Z_DEBUG
120 Z_debug ("ZParseNotice: bad packet from %s/%d (line %d)",
121 inet_ntoa (notice->z_uid.zuid_addr.s_addr),
122 notice->z_port, lineno);
123 #else
124 /* We won't use lineno anywhere else, so let's silence a warning. */
125 (void)lineno;
126 #endif
127 #else
128 badpkt:
129 #ifdef Z_DEBUG
130 Z_debug ("ZParseNotice: bad packet from %s/%d",
131 inet_ntoa (notice->z_uid.zuid_addr.s_addr),
132 notice->z_port);
133 #endif
134 #endif
135 return ZERR_BADPKT;
138 if (numfields) {
139 if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
140 BAD_PACKET;
141 notice->z_kind = temp;
142 numfields--;
143 next_field (ptr);
145 else
146 BAD_PACKET;
148 if (numfields) {
149 if (ZReadAscii(ptr, end-ptr, (unsigned char *)&notice->z_uid,
150 sizeof(ZUnique_Id_t)) == ZERR_BADFIELD)
151 BAD_PACKET;
152 notice->z_time.tv_sec = ntohl((unsigned long) notice->z_uid.tv.tv_sec);
153 notice->z_time.tv_usec = ntohl((unsigned long) notice->z_uid.tv.tv_usec);
154 numfields--;
155 next_field (ptr);
157 else
158 BAD_PACKET;
160 if (numfields) {
161 if (ZReadAscii16(ptr, end-ptr, &notice->z_port) == ZERR_BADFIELD)
162 BAD_PACKET;
163 notice->z_port = htons(notice->z_port);
164 numfields--;
165 next_field (ptr);
167 else
168 BAD_PACKET;
170 if (numfields) {
171 if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
172 BAD_PACKET;
173 notice->z_auth = temp;
174 numfields--;
175 next_field (ptr);
177 else
178 BAD_PACKET;
179 notice->z_checked_auth = ZAUTH_UNSET;
181 if (numfields) {
182 if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
183 BAD_PACKET;
184 notice->z_authent_len = temp;
185 numfields--;
186 next_field (ptr);
188 else
189 BAD_PACKET;
191 if (numfields) {
192 notice->z_ascii_authent = ptr;
193 numfields--;
194 next_field (ptr);
196 else
197 BAD_PACKET;
199 if (numfields) {
200 notice->z_class = ptr;
201 numfields--;
202 next_field (ptr);
204 else
205 notice->z_class = "";
207 if (numfields) {
208 notice->z_class_inst = ptr;
209 numfields--;
210 next_field (ptr);
212 else
213 notice->z_class_inst = "";
215 if (numfields) {
216 notice->z_opcode = ptr;
217 numfields--;
218 next_field (ptr);
220 else
221 notice->z_opcode = "";
223 if (numfields) {
224 notice->z_sender = ptr;
225 numfields--;
226 next_field (ptr);
228 else
229 notice->z_sender = "";
231 if (numfields) {
232 notice->z_recipient = ptr;
233 numfields--;
234 next_field (ptr);
236 else
237 notice->z_recipient = "";
239 if (numfields) {
240 notice->z_default_format = ptr;
241 numfields--;
242 next_field (ptr);
244 else
245 notice->z_default_format = "";
247 /*XXX*/
248 if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
249 BAD_PACKET;
250 notice->z_checksum = temp;
251 numfields--;
252 next_field (ptr);
254 if (numfields) {
255 notice->z_multinotice = ptr;
256 numfields--;
257 next_field (ptr);
259 else
260 notice->z_multinotice = "";
262 if (numfields) {
263 if (ZReadAscii(ptr, end-ptr, (unsigned char *)&notice->z_multiuid,
264 sizeof(ZUnique_Id_t)) == ZERR_BADFIELD)
265 BAD_PACKET;
266 notice->z_time.tv_sec = ntohl((unsigned long) notice->z_multiuid.tv.tv_sec);
267 notice->z_time.tv_usec = ntohl((unsigned long) notice->z_multiuid.tv.tv_usec);
268 numfields--;
269 next_field (ptr);
271 else
272 notice->z_multiuid = notice->z_uid;
274 for (i=0;i<Z_MAXOTHERFIELDS && numfields;i++,numfields--) {
275 notice->z_other_fields[i] = ptr;
276 next_field (ptr);
278 notice->z_num_other_fields = i;
280 for (i=0;i<numfields;i++)
281 next_field (ptr);
283 notice->z_message = (void *)ptr;
284 notice->z_message_len = len-(ptr-buffer);
286 return (ZERR_NONE);