From e072a11725f6cca3772b6fdabdb37975d9d30347 Mon Sep 17 00:00:00 2001 From: Johannes Schriewer Date: Sat, 28 Jul 2018 16:09:08 +0200 Subject: [PATCH] Extend hexdump function for tests --- src/buffer.h | 6 +++--- src/debug.c | 27 --------------------------- src/debug.h | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/buffer.h b/src/buffer.h index 5a14505..4563145 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -200,11 +200,11 @@ static inline size_t buffer_append_buffer(Buffer *dest, Buffer *src) { #if DEBUG #include "debug.h" -static inline void buffer_hexdump(Buffer *buffer) { - hexdump(buffer->data, buffer->len); +static inline void buffer_hexdump(Buffer *buffer, int indent) { + hexdump(buffer->data, buffer->len, indent); } #else -#define buffer_hexdump(_buffer) /* */ +#define buffer_hexdump(_buffer, _indent) /* */ #endif #endif /* buffer_h__included */ diff --git a/src/debug.c b/src/debug.c index 94501fa..ac7a1f8 100644 --- a/src/debug.c +++ b/src/debug.c @@ -4,31 +4,4 @@ #include "debug.h" -void hexdump(char *data, size_t len) { - for (int i = 0; i < len;) { - for (int col = 0; col < 16; col++) { - if (i + col < len) { - fprintf(stdout, "%02x ", data[i + col]); - } else { - fprintf(stdout, " "); - } - } - - fprintf(stdout, " | "); - - for (int col = 0; col < 16; col++) { - if (i + col < len) { - char c = data[i + col]; - if ((c > 127) || (c < 32)) c = '.'; - fprintf(stdout, "%c", c); - } else { - fprintf(stdout, " "); - } - } - - fprintf(stdout, "\n"); - i += 16; - } -} - #endif /* DEBUG */ diff --git a/src/debug.h b/src/debug.h index 2088c6b..c0da3c2 100644 --- a/src/debug.h +++ b/src/debug.h @@ -2,10 +2,49 @@ #define debug_h__included #include +#include + #if DEBUG -void hexdump(char *data, size_t len); +static inline void hexdump(char *data, size_t len, int indent) { + for (int i = 0; i < len;) { + + // indent + for (int col = 0; col < indent; col++) { + fprintf(stdout, " "); + } + + // address + fprintf(stdout, "0x%04x: ", i); + + // hex field + for (int col = 0; col < 16; col++) { + if (i + col < len) { + fprintf(stdout, "%02x ", (uint8_t)data[i + col]); + } else { + fprintf(stdout, " "); + } + } + + // separator + fprintf(stdout, " | "); + + // ascii field + for (int col = 0; col < 16; col++) { + if (i + col < len) { + char c = data[i + col]; + if ((c > 127) || (c < 32)) c = '.'; + fprintf(stdout, "%c", (uint8_t)c); + } else { + fprintf(stdout, " "); + } + } + + fprintf(stdout, "\n"); + i += 16; + } +} #else /* DEBUG */