From a8c23a4c2a086db0d88e6dc6f320bb3c8735b169 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Fri, 14 Dec 2018 20:35:23 -0500 Subject: [PATCH] rawnet - fix logging, add hexdump. --- src/rawnet/rawnetsupp.c | 29 +++++++++++++++++++++++++++++ src/rawnet/rawnetsupp.h | 4 +++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/rawnet/rawnetsupp.c b/src/rawnet/rawnetsupp.c index 94c8541..4f6e33e 100644 --- a/src/rawnet/rawnetsupp.c +++ b/src/rawnet/rawnetsupp.c @@ -149,3 +149,32 @@ unsigned long crc32_buf(const char *buffer, unsigned int len) { return ~crc; } + +void rawnet_hexdump(unsigned char *what, int count ) { + static const char hex[] = "0123456789abcdef"; + char buffer1[16 * 3 + 1]; + char buffer2[16 + 1]; + unsigned offset; + + + offset = 0; + while (count) { + unsigned char x = *what++; + + buffer1[offset * 3] = hex[x >> 4]; + buffer1[offset * 3 + 1] = hex[x & 0x0f]; + buffer1[offset * 3 + 2] = ' '; + + buffer2[offset] = isprint(x) ? x : '.'; + + + --count; + ++offset; + if (offset == 16 || count == 0) { + buffer1[offset * 3] = 0; + buffer2[offset] = 0; + fprintf(stderr, "%-50s %s\n", buffer1, buffer2); + offset = 0; + } + } +} diff --git a/src/rawnet/rawnetsupp.h b/src/rawnet/rawnetsupp.h index 34dd97d..3e1f667 100644 --- a/src/rawnet/rawnetsupp.h +++ b/src/rawnet/rawnetsupp.h @@ -52,5 +52,7 @@ extern int util_string_set(char **str, const char *new_value); extern unsigned long crc32_buf(const char *buffer, unsigned int len); -#define log_message(level,...) fprintf(stderr,__VA_ARGS__) +void rawnet_hexdump(unsigned char *what, int count ); + +#define log_message(level,...) do { fprintf(stderr,__VA_ARGS__); fputs("\n", stderr); } while (0) #endif