diff options
Diffstat (limited to 'src/hexdump.c')
-rw-r--r-- | src/hexdump.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/hexdump.c b/src/hexdump.c new file mode 100644 index 0000000..1d51d99 --- /dev/null +++ b/src/hexdump.c @@ -0,0 +1,87 @@ +/* + * hexdump.c: + * + * Copyright (c) 2017 James McKenzie <foss@madingley.org>, + * 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"); + } +} |