util/dump.c: Add ASCII view to dump output

This commit is contained in:
Tau 2018-11-09 15:59:01 -05:00
parent c1dee321f3
commit 5df0994458

View File

@ -11,7 +11,9 @@
void dump(const void *ptr, size_t nbytes) void dump(const void *ptr, size_t nbytes)
{ {
const uint8_t *bytes; const uint8_t *bytes;
uint8_t c;
size_t i; size_t i;
size_t j;
assert(ptr != NULL); assert(ptr != NULL);
@ -21,17 +23,32 @@ void dump(const void *ptr, size_t nbytes)
bytes = ptr; bytes = ptr;
for (i = 0 ; i < nbytes ; i++) { for (i = 0 ; i < nbytes ; i += 16) {
if (i % 16 == 0) { dprintf(" %08x:", i);
dprintf("\t%08x:", i);
for (j = 0 ; i + j < nbytes && j < 16 ; j++) {
dprintf(" %02x", bytes[i + j]);
} }
dprintf(" %02x", bytes[i]); while (j < 16) {
dprintf(" ");
j++;
}
dprintf(" ");
for (j = 0 ; i + j < nbytes && j < 16 ; j++) {
c = bytes[i + j];
if (c < 0x20 || c >= 0x7F) {
c = '.';
}
dprintf("%c", c);
}
if (i % 16 == 15) {
dprintf("\n"); dprintf("\n");
} }
}
dprintf("\n"); dprintf("\n");
} }