diff options
author | Kenneth G Raeburn <raeburn@mit.edu> | 1990-11-15 08:07:35 +0000 |
---|---|---|
committer | Kenneth G Raeburn <raeburn@mit.edu> | 1990-11-15 08:07:35 +0000 |
commit | 1019aaf105598e081aa3d724a5f10ef94076dfbb (patch) | |
tree | fce6d92d8bc1c7c124b8428715867f27082ea4da /lib/ZParseNot.c | |
parent | 95ac53b4cfdeb3da6ded1465a08186faa4adfa11 (diff) |
Use faster next_field macro, sometimes machine-dependent.
Also use sizeof instead of strlen when appropriate, and use a
union of types rather than casting the address of an int array.
Diffstat (limited to 'lib/ZParseNot.c')
-rw-r--r-- | lib/ZParseNot.c | 147 |
1 files changed, 103 insertions, 44 deletions
diff --git a/lib/ZParseNot.c b/lib/ZParseNot.c index 4fc9cf1..190d26d 100644 --- a/lib/ZParseNot.c +++ b/lib/ZParseNot.c @@ -13,13 +13,68 @@ /* $Header$ */ #ifndef lint -static char rcsid_ZParseNotice_c[] = "$Header$"; +static char rcsid_ZParseNotice_c[] = + "$Header$"; #endif lint #include <zephyr/mit-copyright.h> #include <zephyr/zephyr_internal.h> +/* Assume that strlen is efficient on this machine... */ +#define next_field(ptr) ptr += strlen (ptr) + 1 + +#if defined (__GNUC__) && defined (__vax__) +#undef next_field +static __inline__ char * Istrend (char *str) { + /* + * This should be faster on VAX models outside the 2 series. Don't + * use it if you are using MicroVAX 2 servers. If you are using a + * VS2 server, use something like + * #define next_field(ptr) while(*ptr++) + * instead of this code. + * + * This requires use of GCC to get the optimized code, but + * everybody uses GCC, don't they? :-) + */ + register char *str2 asm ("r1"); + /* Assumes that no field is longer than 64K.... */ + asm ("locc $0,$65535,(%1)" : "=r" (str2) : "r" (str) : "r0"); + return str2; +} +#define next_field(ptr) ptr = Istrend (ptr) + 1 +#endif + +#ifdef mips +#undef next_field +/* + * The compiler doesn't optimize this macro as well as it does the + * following function. + */ +#define next_fieldXXX(ptr) do{register unsigned c1,c2;c1= *ptr; \ + while((ptr++,c2= *ptr,c1)&&(ptr++,c1= *ptr,c2));}while(0) +static char *next_field_1 (s) char *s; { + /* + * Calling overhead is still present, but this routine is faster + * than strlen, and doesn't bother with some of the other math + * that we'd just have to undo later anyways. + */ + register unsigned c1 = *s, c2; + while (1) { + s++; c2 = *s; if (c1 == 0) break; + s++; c1 = *s; if (c2 == 0) break; + s++; c2 = *s; if (c1 == 0) break; + s++; c1 = *s; if (c2 == 0) break; + } + return s; +} +#define next_field(ptr) ptr=next_field_1(ptr) +#endif + +static int zzz = ZERR_BADPKT; +#undef ZERR_BADPKT +#define ZERR_BADPKT (abort(),zzz) + Code_t ZParseNotice(buffer, len, notice) char *buffer; int len; @@ -27,7 +82,12 @@ Code_t ZParseNotice(buffer, len, notice) { char *ptr, *end; int maj, numfields, i; - unsigned int temp[3]; + union { + int i; + ZUnique_Id_t uid; + u_short us; + ZChecksum_t sum; + } temp; bzero((char *)notice, sizeof(ZNotice_t)); @@ -37,19 +97,19 @@ Code_t ZParseNotice(buffer, len, notice) notice->z_packet = buffer; notice->z_version = ptr; - if (strncmp(ptr, ZVERSIONHDR, strlen(ZVERSIONHDR))) + if (strncmp(ptr, ZVERSIONHDR, sizeof(ZVERSIONHDR) - 1)) return (ZERR_VERS); - ptr += strlen(ZVERSIONHDR); + ptr += sizeof(ZVERSIONHDR) - 1; maj = atoi(ptr); if (maj != ZVERSIONMAJOR) return (ZERR_VERS); - ptr += strlen(ptr)+1; + next_field (ptr); - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, - sizeof(int)) == ZERR_BADFIELD) + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.i, + sizeof(temp.i)) == ZERR_BADFIELD) return (ZERR_BADPKT); - numfields = ntohl((u_long) *temp); - ptr += strlen(ptr)+1; + numfields = ntohl((u_long) temp.i); + next_field (ptr); /*XXX 3 */ numfields -= 2; /* numfields, version, and checksum */ @@ -57,59 +117,59 @@ Code_t ZParseNotice(buffer, len, notice) return (ZERR_BADPKT); if (numfields) { - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, - sizeof(int)) == ZERR_BADFIELD) + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.i, + sizeof(temp.i)) == ZERR_BADFIELD) return (ZERR_BADPKT); - notice->z_kind = (ZNotice_Kind_t)ntohl((u_long) *temp); + notice->z_kind = (ZNotice_Kind_t)ntohl((u_long) temp.i); numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else return (ZERR_BADPKT); if (numfields) { - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.uid, sizeof(ZUnique_Id_t)) == ZERR_BADFIELD) return (ZERR_BADPKT); - bcopy((char *)temp, (char *)¬ice->z_uid, sizeof(ZUnique_Id_t)); + notice->z_uid = temp.uid; notice->z_time.tv_sec = ntohl((u_long) notice->z_uid.tv.tv_sec); notice->z_time.tv_usec = ntohl((u_long) notice->z_uid.tv.tv_usec); numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else return (ZERR_BADPKT); if (numfields) { - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.us, sizeof(u_short)) == ZERR_BADFIELD) return (ZERR_BADPKT); - notice->z_port = *((u_short *)temp); + notice->z_port = temp.us; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else return (ZERR_BADPKT); if (numfields) { - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.i, sizeof(int)) == ZERR_BADFIELD) return (ZERR_BADPKT); - notice->z_auth = *temp; + notice->z_auth = temp.i; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else return (ZERR_BADPKT); if (numfields) { - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.i, sizeof(int)) == ZERR_BADFIELD) return (ZERR_BADPKT); - notice->z_authent_len = ntohl((u_long) *temp); + notice->z_authent_len = ntohl((u_long) temp.i); numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else return (ZERR_BADPKT); @@ -117,7 +177,7 @@ Code_t ZParseNotice(buffer, len, notice) if (numfields) { notice->z_ascii_authent = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else return (ZERR_BADPKT); @@ -125,7 +185,7 @@ Code_t ZParseNotice(buffer, len, notice) if (numfields) { notice->z_class = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else notice->z_class = ""; @@ -133,7 +193,7 @@ Code_t ZParseNotice(buffer, len, notice) if (numfields) { notice->z_class_inst = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else notice->z_class_inst = ""; @@ -141,7 +201,7 @@ Code_t ZParseNotice(buffer, len, notice) if (numfields) { notice->z_opcode = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else notice->z_opcode = ""; @@ -149,7 +209,7 @@ Code_t ZParseNotice(buffer, len, notice) if (numfields) { notice->z_sender = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else notice->z_sender = ""; @@ -157,7 +217,7 @@ Code_t ZParseNotice(buffer, len, notice) if (numfields) { notice->z_recipient = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else notice->z_recipient = ""; @@ -165,51 +225,50 @@ Code_t ZParseNotice(buffer, len, notice) if (numfields) { notice->z_default_format = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else notice->z_default_format = ""; /*XXX*/ - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.sum, sizeof(ZChecksum_t)) == ZERR_BADFIELD) return (ZERR_BADPKT); - notice->z_checksum = ntohl((u_long) *temp); + notice->z_checksum = ntohl((u_long) temp.sum); numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); if (numfields) { notice->z_multinotice = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else notice->z_multinotice = ""; if (numfields) { - if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, + if (ZReadAscii(ptr, end-ptr, (unsigned char *)&temp.uid, sizeof(ZUnique_Id_t)) == ZERR_BADFIELD) return (ZERR_BADPKT); - bcopy((char *)temp, (char *)¬ice->z_multiuid, sizeof(ZUnique_Id_t)); + notice->z_multiuid = temp.uid; notice->z_time.tv_sec = ntohl((u_long) notice->z_multiuid.tv.tv_sec); notice->z_time.tv_usec = ntohl((u_long) notice->z_multiuid.tv.tv_usec); numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } else - bcopy((char *) ¬ice->z_uid, (char *) ¬ice->z_multiuid, - sizeof(ZUnique_Id_t)); + notice->z_uid = notice->z_multiuid; for (i=0;i<Z_MAXOTHERFIELDS && numfields;i++,numfields--) { notice->z_other_fields[i] = ptr; numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); } notice->z_num_other_fields = i; for (i=0;i<numfields;i++) - ptr += strlen(ptr)+1; + next_field (ptr); #ifdef notdef if (ZReadAscii(ptr, end-ptr, (unsigned char *)temp, @@ -218,7 +277,7 @@ Code_t ZParseNotice(buffer, len, notice) return (ZERR_BADPKT); notice->z_checksum = ntohl(*temp); numfields--; - ptr += strlen(ptr)+1; + next_field (ptr); #endif notice->z_message = (caddr_t) ptr; |