]> The Tcpdump Group git mirrors - tcpdump/commitdiff
new function bittok2str()
authorhannes <hannes>
Thu, 7 Nov 2002 20:07:58 +0000 (20:07 +0000)
committerhannes <hannes>
Thu, 7 Nov 2002 20:07:58 +0000 (20:07 +0000)
  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;

interface.h
util.c

index 209788242e4ce85fe1cd50b1c09f7d02f9b6d4a9..d115fec3f0f548fdb37994f400790dcce7643a84 100644 (file)
@@ -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 2ad40daf0f6ec119035a8b6f8d771d165d355c7c..84d4a076122d8c0144908b709edf8e63e808d3a6 100644 (file)
--- 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 <interface.h> is the public interface to