Browse code

Added flags parameter to format_hex_ex.

We add the flags parameter without changing the signature of
the function by repurposing the space_break parameter into
space_break_flags where the lower 8 bits are used for the
previous space_break parameter and the higher bits are used
for flag values.

Added new flag FHE_CAPS that formats the generated hex string
in upper case.

Signed-off-by: James Yonan <james@openvpn.net>
Acked-by: Steffan Karger <steffan.karger@fox-it.com>
Message-Id: <1456993146-63968-4-git-send-email-james@openvpn.net>
URL: http://article.gmane.org/gmane.network.openvpn.devel/11275
Signed-off-by: Gert Doering <gert@greenie.muc.de>

James Yonan authored on 2016/03/03 17:19:00
Showing 2 changed files
... ...
@@ -435,18 +435,21 @@ gc_transfer (struct gc_arena *dest, struct gc_arena *src)
435 435
 
436 436
 char *
437 437
 format_hex_ex (const uint8_t *data, int size, int maxoutput,
438
-	       int space_break, const char* separator,
438
+	       unsigned int space_break_flags, const char* separator,
439 439
 	       struct gc_arena *gc)
440 440
 {
441 441
   struct buffer out = alloc_buf_gc (maxoutput ? maxoutput :
442
-				    ((size * 2) + (size / space_break) * (int) strlen (separator) + 2),
442
+				    ((size * 2) + (size / (space_break_flags & FHE_SPACE_BREAK_MASK)) * (int) strlen (separator) + 2),
443 443
 				    gc);
444 444
   int i;
445 445
   for (i = 0; i < size; ++i)
446 446
     {
447
-      if (separator && i && !(i % space_break))
447
+      if (separator && i && !(i % (space_break_flags & FHE_SPACE_BREAK_MASK)))
448 448
 	buf_printf (&out, "%s", separator);
449
-      buf_printf (&out, "%02x", data[i]);
449
+      if (space_break_flags & FHE_CAPS)
450
+	buf_printf (&out, "%02X", data[i]);
451
+      else
452
+	buf_printf (&out, "%02x", data[i]);
450 453
     }
451 454
   buf_catrunc (&out, "[more...]");
452 455
   return (char *)out.data;
... ...
@@ -403,9 +403,11 @@ bool buf_parse (struct buffer *buf, const int delim, char *line, const int size)
403 403
 /*
404 404
  * Hex dump -- Output a binary buffer to a hex string and return it.
405 405
  */
406
+#define FHE_SPACE_BREAK_MASK 0xFF /* space_break parameter in lower 8 bits */
407
+#define FHE_CAPS 0x100            /* output hex in caps */
406 408
 char *
407 409
 format_hex_ex (const uint8_t *data, int size, int maxoutput,
408
-	       int space_break, const char* separator,
410
+	       unsigned int space_break_flags, const char* separator,
409 411
 	       struct gc_arena *gc);
410 412
 
411 413
 static inline char *