aboutsummaryrefslogtreecommitdiffstats
path: root/vppinfra
diff options
context:
space:
mode:
Diffstat (limited to 'vppinfra')
-rw-r--r--vppinfra/vppinfra/format.h3
-rw-r--r--vppinfra/vppinfra/std-formats.c46
2 files changed, 49 insertions, 0 deletions
diff --git a/vppinfra/vppinfra/format.h b/vppinfra/vppinfra/format.h
index 62669cdccd3..45cd3f50e37 100644
--- a/vppinfra/vppinfra/format.h
+++ b/vppinfra/vppinfra/format.h
@@ -297,6 +297,9 @@ u8 * format_memory_size (u8 * s, va_list * va);
/* Format c identifier: e.g. a_name -> "a name". */
u8 * format_c_identifier (u8 * s, va_list * va);
+/* Format hexdump with both hex and printable chars - compatible with text2pcap */
+u8 * format_hexdump (u8 * s, va_list * va);
+
/* Unix specific formats. */
#ifdef CLIB_UNIX
/* Setup input from Unix file. */
diff --git a/vppinfra/vppinfra/std-formats.c b/vppinfra/vppinfra/std-formats.c
index 9bc58164308..b47d8fb5aac 100644
--- a/vppinfra/vppinfra/std-formats.c
+++ b/vppinfra/vppinfra/std-formats.c
@@ -36,6 +36,7 @@
*/
#include <vppinfra/format.h>
+#include <ctype.h>
/* Format vectors. */
u8 * format_vec32 (u8 * s, va_list * va)
@@ -257,3 +258,48 @@ u8 * format_c_identifier (u8 * s, va_list * va)
return s;
}
+
+u8 *
+format_hexdump (u8 * s, va_list * args)
+{
+ u8 * data = va_arg (*args, u8 *);
+ uword len = va_arg (*args, uword);
+ int i, index =0;
+ const int line_len = 16;
+ u8 * line_hex = 0;
+ u8 * line_str = 0;
+ uword indent = format_get_indent (s);
+
+ if (!len)
+ return s;
+
+ for(i=0; i < len; i++)
+ {
+ line_hex = format (line_hex, "%02x ", data[i]);
+ line_str = format (line_str, "%c", isprint (data[i]) ? data[i] : '.');
+ if (!( (i + 1) % line_len))
+ {
+ s = format (s, "%U%05x: %v[%v]",
+ format_white_space, index ? indent : 0,
+ index, line_hex, line_str);
+ if (i < len - 1)
+ s = format (s, "\n");
+ index = i + 1;
+ vec_reset_length(line_hex);
+ vec_reset_length(line_str);
+ }
+ }
+
+ while (i++ % line_len)
+ line_hex = format (line_hex, " ");
+
+ if (vec_len(line_hex))
+ s = format (s, "%U%05x: %v[%v]",
+ format_white_space, indent,
+ index, line_hex, line_str);
+
+ vec_free(line_hex);
+ vec_free(line_str);
+
+ return s;
+}