/* * hexdump.c: * * Copyright (c) 2017 James McKenzie , * All rights reserved. * */ #include "project.h" static unsigned char printable (unsigned char c) { if (c < ' ') c = '.'; if (c > 126) c = '.'; return c; } void hexdump (char *prefix, void *_d, size_t os, size_t oe) { unsigned char *d = (unsigned char *) _d; static unsigned char zero[0x10]; size_t s, e; size_t i, j; int gap = 0; s = os & ~15; e = (oe - 1) | 15; e++; for (i = s; i < e; i += 0x10) { if (((i > s) && ((i + 0x10) < oe)) && !memcmp (&d[i], zero, 0x10)) { gap = 1; continue; } if (gap) { printf ("%s ...\n", prefix); gap = 0; } printf ("%s %06lx:", prefix, i); for (j = i; j < (i + 0x10); ++j) { if (j == 8) printf (" "); if ((j < os) || (j >= oe)) printf (" "); else printf (" %02x", d[j]); } printf (" "); for (j = i; j < (i + 0x10); ++j) { if (j == 8) printf (" "); if ((j < os) || (j >= oe)) printf (" "); else printf ("%c", printable (d[j])); } printf ("\n"); } }