| 1 |
|
| 2 |
|
| 3 |
#include "hexdump.h" |
| 4 |
#include <ctype.h> |
| 5 |
|
| 6 |
static void addrprint(FILE * outfile, uint64_t address, int width); |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
void hexdump(const void *mem, int bytes, uint64_t addrprint_start, |
| 25 |
FILE * outfile, const hexdump_format_t * format) |
| 26 |
{ |
| 27 |
int bytes_left, index, i; |
| 28 |
const unsigned char *p; |
| 29 |
|
| 30 |
|
| 31 |
if ((format->bytes_per_line <= 0) || (bytes < 0)) |
| 32 |
return; |
| 33 |
|
| 34 |
p = (const unsigned char *)mem; |
| 35 |
index = 0; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
for (bytes_left = bytes; |
| 42 |
bytes_left >= format->bytes_per_line; |
| 43 |
bytes_left -= format->bytes_per_line) { |
| 44 |
|
| 45 |
fprintf(outfile, "%s", format->indent); |
| 46 |
addrprint(outfile, addrprint_start + index, |
| 47 |
format->addrprint_width); |
| 48 |
fprintf(outfile, "%s", format->sep1); |
| 49 |
|
| 50 |
|
| 51 |
for (i = 0;;) { |
| 52 |
fprintf(outfile, "%02x", p[index++]); |
| 53 |
|
| 54 |
if (++i >= format->bytes_per_line) |
| 55 |
break; |
| 56 |
|
| 57 |
fprintf(outfile, "%s", format->sep2); |
| 58 |
} |
| 59 |
|
| 60 |
index -= format->bytes_per_line; |
| 61 |
fprintf(outfile, "%s", format->sep3); |
| 62 |
|
| 63 |
|
| 64 |
for (i = 0; i < format->bytes_per_line; i++, index++) |
| 65 |
fputc(isprint(p[index])?p[index]:format->nonprintable, outfile); |
| 66 |
|
| 67 |
fprintf(outfile, "\n"); |
| 68 |
} |
| 69 |
|
| 70 |
if (bytes_left == 0) |
| 71 |
return; |
| 72 |
|
| 73 |
|
| 74 |
fprintf(outfile, "%s", format->indent); |
| 75 |
addrprint(outfile, addrprint_start + index, format->addrprint_width); |
| 76 |
fprintf(outfile, "%s", format->sep1); |
| 77 |
|
| 78 |
|
| 79 |
for (i = 0; i < bytes_left; i++) { |
| 80 |
fprintf(outfile, "%02x", p[index++]); |
| 81 |
fprintf(outfile, "%s", format->sep2); |
| 82 |
} |
| 83 |
|
| 84 |
index -= bytes_left; |
| 85 |
|
| 86 |
|
| 87 |
for (;;) { |
| 88 |
fprintf(outfile, " "); |
| 89 |
|
| 90 |
if (++i >= format->bytes_per_line) |
| 91 |
break; |
| 92 |
|
| 93 |
fprintf(outfile, "%s", format->sep2); |
| 94 |
} |
| 95 |
|
| 96 |
fprintf(outfile, "%s", format->sep3); |
| 97 |
|
| 98 |
|
| 99 |
for (i = 0; i < bytes_left; i++) |
| 100 |
fputc(isprint(p[index])?p[index++]:format->nonprintable, outfile); |
| 101 |
|
| 102 |
|
| 103 |
for (; i < format->bytes_per_line; i++) |
| 104 |
fprintf(outfile, " "); |
| 105 |
|
| 106 |
fprintf(outfile, "\n"); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
static void addrprint(FILE * outfile, uint64_t address, int width) |
| 121 |
{ |
| 122 |
char s[17]; |
| 123 |
int i; |
| 124 |
|
| 125 |
|
| 126 |
if (width < 1) |
| 127 |
width = 1; |
| 128 |
else if (width > 8) |
| 129 |
width = 8; |
| 130 |
|
| 131 |
|
| 132 |
sprintf(s, "%016llx", (unsigned long long)address); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
for (i = 16 - (2 * width);;) { |
| 138 |
fprintf(outfile, "%c", s[i]); |
| 139 |
|
| 140 |
if (++i >= 16) |
| 141 |
break; |
| 142 |
|
| 143 |
if ((i % 4) == 0) |
| 144 |
fprintf(outfile, ":"); |
| 145 |
} |
| 146 |
} |