From: hannes Date: Thu, 7 Nov 2002 20:07:58 +0000 (+0000) Subject: new function bittok2str() X-Git-Tag: tcpdump-3.8-bp~309 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/686992b829acc1a8a68e71f7cd52d9cfc9560ef7 new function bittok2str() this is useful for scanning bitfields the input is a tokenlist and a testvalue the function will parse the tokenlist for each single bitmatch of the testvalue; if it finds a a match it will write teh tokenstring into a buffer and place a comma to seperate the values if there is no match it will format the buffer according to the format string; i saw many private instances of similar bitscanning functions; lets use bittok2str() from now on; --- diff --git a/interface.h b/interface.h index 20978824..d115fec3 100644 --- a/interface.h +++ b/interface.h @@ -18,7 +18,7 @@ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.196 2002-10-18 09:17:47 guy Exp $ (LBL) + * @(#) $Header: /tcpdump/master/tcpdump/interface.h,v 1.197 2002-11-07 20:07:58 hannes Exp $ (LBL) */ #ifndef tcpdump_interface_h @@ -159,6 +159,7 @@ extern void relts_print(int); extern int fn_print(const u_char *, const u_char *); extern int fn_printn(const u_char *, u_int, const u_char *); extern const char *tok2str(const struct tok *, const char *, int); +extern char *bittok2str(const struct tok *, const char *, int); extern const char *tok2strary_internal(const char **, int, const char *, int); #define tok2strary(a,f,i) tok2strary_internal(a, sizeof(a)/sizeof(a[0]),f,i) diff --git a/util.c b/util.c index 2ad40daf..84d4a076 100644 --- a/util.c +++ b/util.c @@ -21,7 +21,7 @@ #ifndef lint static const char rcsid[] = - "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.80 2002-10-11 10:34:07 hannes Exp $ (LBL)"; + "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.81 2002-11-07 20:07:58 hannes Exp $ (LBL)"; #endif #ifdef HAVE_CONFIG_H @@ -246,6 +246,51 @@ tok2str(register const struct tok *lp, register const char *fmt, return (buf); } +/* + * Convert a bit token value to a string; use "fmt" if not found. + * this is useful for parsing bitfields, the output strings are comma seperated + */ +char * +bittok2str(register const struct tok *lp, register const char *fmt, + register int v) +{ + static char buf[256]; /* our stringbuffer */ + int buflen=0; + register int rotbit; /* this is the bit we rotate through all bitpositions */ + register int tokval; + + while (lp->s != NULL) { + tokval=lp->v; /* load our first value */ + rotbit=1; + while (rotbit != 0) { + /* + * lets AND the rotating bit with our token value + * and see if we have got a match + */ + if (tokval == (v&rotbit)) { + /* ok we have found something */ + buflen+=snprintf(buf+buflen, sizeof(buf)-buflen, "%s, ",lp->s); + break; + } + rotbit=rotbit<<1; /* no match - lets shift and try again */ + } + lp++; + } + + if (buflen != 0) { /* did we find anything */ + /* yep, set the the trailing zero 2 bytes before to eliminate the last comma & whitespace */ + buf[buflen-2] = '\0'; + return (buf); + } + else { + /* bummer - lets print the "unknown" message as advised in the fmt string if we got one */ + if (fmt == NULL) + fmt = "#%d"; + (void)snprintf(buf, sizeof(buf), fmt, v); + return (buf); + } +} + /* * Convert a value to a string using an array; the macro * tok2strary() in is the public interface to