From 1d17766135ff2dcbc8f6974787b16ef7ebda75a1 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 21 Aug 2014 12:30:38 -0400 Subject: [PATCH] add hex dump function --- common.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ prototypes.h | 3 +++ 2 files changed, 53 insertions(+) diff --git a/common.c b/common.c index ff7bbba..16d9fa1 100644 --- a/common.c +++ b/common.c @@ -13,6 +13,8 @@ #include +#include +#include #include "options.h" #include "prototypes.h" @@ -163,3 +165,51 @@ int CloseLoop(Connection *connection) return 1; } + + +void hexdump(const unsigned char *data, int size) +{ + const char *HexMap = "0123456789abcdef"; + + static char buffer1[16 * 3 + 1 + 1]; + static char buffer2[16 + 1]; + + Word offset = 0; + unsigned i, j; + + + while(size > 0) + { + unsigned linelen = 16; + + memset(buffer1, ' ', sizeof(buffer1)); + memset(buffer2, ' ', sizeof(buffer2)); + + if (size < linelen) linelen = size; + + + for (i = 0, j = 0; i < linelen; i++) + { + unsigned x = data[i]; + buffer1[j++] = HexMap[x >> 4]; + buffer1[j++] = HexMap[x & 0x0f]; + j++; + if (i == 7) j++; + + buffer2[i] = isprint(x) ? x : '.'; + + } + + buffer1[sizeof(buffer1)-1] = 0; + buffer2[sizeof(buffer2)-1] = 0; + + + printf("%04x:\t%s\t%s\n", offset, buffer1, buffer2); + + offset += 16; + data += 16; + size -= 16; + } + + printf("\n"); +} diff --git a/prototypes.h b/prototypes.h index 8acfa39..cf8e950 100644 --- a/prototypes.h +++ b/prototypes.h @@ -16,6 +16,9 @@ pascal void DisplayCallback(const char *message); +void hexdump(const unsigned char *data, int size); + + typedef struct ReadBlock {