]> The Tcpdump Group git mirrors - tcpdump/blobdiff - util-print.c
Wrap some common code up as uint2tokary().
[tcpdump] / util-print.c
index e22e535e8d2b2d62c9ead781614180d9f0301567..00160b432d06a0f2a7d78c9319f7cb91ef8373b6 100644 (file)
@@ -625,6 +625,20 @@ tok2strary_internal(const char **lp, int n, const char *fmt,
        return (buf);
 }
 
+const struct tok *
+uint2tokary_internal(const struct uint_tokary dict[], const size_t size,
+                     const u_int val)
+{
+       size_t i;
+       /* Try a direct lookup before the full scan. */
+       if (val < size && dict[val].uintval == val)
+               return dict[val].tokary; /* OK if NULL */
+       for (i = 0; i < size; i++)
+               if (dict[i].uintval == val)
+                       return dict[i].tokary; /* OK if NULL */
+       return NULL;
+}
+
 /*
  * Convert a 32-bit netmask to prefixlen if possible
  * the function returns the prefix-len; if plen == -1