diff --git a/freelist/hexdump.c b/freelist/hexdump.c index cdf2121..3cfba4b 100644 --- a/freelist/hexdump.c +++ b/freelist/hexdump.c @@ -2,30 +2,31 @@ #include #include #include +#include +#include +#include -void hexdump(const void *data, size_t size) { +void hexdump(const void *data, const size_t size, const size_t width) { const unsigned char *p = (const unsigned char *)data; size_t i, j; - for (i = 0; i < size; i += 16) { - // Print offset + for (i = 0; i < size; i += width) { printf("%08zx ", i); - // Print hex bytes - for (j = 0; j < 16; j++) { + for (j = 0; j < width; j++) { if (i + j < size) { printf("%02X ", p[i + j]); } else { - printf(" "); // padding for incomplete lines + printf(" "); } - if (j == 7) - printf(" "); // extra space in middle + + if ((j + 1) % 8 == 0) + printf(" "); } printf(" |"); - // Print ASCII characters - for (j = 0; j < 16 && i + j < size; j++) { + for (j = 0; j < width && i + j < size; j++) { unsigned char c = p[i + j]; printf("%c", isprint(c) ? c : '.'); } diff --git a/freelist/hexdump.h b/freelist/hexdump.h index d01a907..c057eac 100644 --- a/freelist/hexdump.h +++ b/freelist/hexdump.h @@ -3,6 +3,6 @@ #include -void hexdump(const void *data, size_t size); +void hexdump(const void *data, const size_t size, const size_t width); #endif // HEXDUMP_H diff --git a/freelist/main.c b/freelist/main.c index ffb2068..5e3f2d6 100644 --- a/freelist/main.c +++ b/freelist/main.c @@ -47,7 +47,7 @@ int main() { const uint32_t head_crc = crc32(headblock, BUFFER_SIZE / 2); /* Display a nice hexdump so you can see the layout */ - hexdump(headblock, BUFFER_SIZE * 2); + hexdump(headblock, BUFFER_SIZE * 2, 32); if (!fl_init(&fl, (uintptr_t)mem, BUFFER_SIZE, sizeof(Vec3))) { printf("Freelist failed to initialize!\n"); @@ -130,7 +130,7 @@ int main() { assert(crc32(tailblock, BUFFER_SIZE / 2) == tail_crc); /* Hexdump the heap, free the block and declare victory */ - hexdump(headblock, BUFFER_SIZE * 2); + hexdump(headblock, BUFFER_SIZE * 2, 32); free(headblock); printf("All tests passed!\n"); return 0;