Fix off-by-one hexdump trailing space, use hd style output, and minor coding style changes
@@ -10,47 +10,34 @@ extern "C" {
/* Based on: http://stackoverflow.com/a/7776146 */
static void hexdump(char *desc, void *addr, size_t len, FILE *fp) {
unsigned int i;
- unsigned char buff[17];
+ unsigned char buf[17];
unsigned char *pc = (unsigned char*)addr;
- // Output description if given.
- if (desc != NULL)
- fprintf(fp, "%s:\n", desc);
+ /* Output description if given. */
+ if (desc != NULL) fprintf(fp, "%s:\n", desc);
- // Process every byte in the data.
for (i = 0; i < (unsigned int)len; i++) {
- // Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
- // Just don't print ASCII for the zeroth line.
- if (i != 0)
- fprintf(fp, " %s\n", buff);
-
- // Output the offset.
- fprintf(fp, " %04x ", i);
+ if (i != 0) fprintf(fp, " |%s|\n", buf);
+ fprintf(fp, "%08x ", i);
} else if ((i % 8) == 0) {
fprintf(fp, " ");
}
-
- // Now the hex code for the specific character.
fprintf(fp, " %02x", pc[i]);
-
- // And store a printable ASCII character for later.
- if ((pc[i] < 0x20) || (pc[i] > 0x7e))
- buff[i % 16] = '.';
- else
- buff[i % 16] = pc[i];
- buff[(i % 16) + 1] = '\0';
+ if ((pc[i] < 0x20) || (pc[i] > 0x7e)) {
+ buf[i % 16] = '.';
+ } else {
+ buf[i % 16] = pc[i];
}
-
- // Pad out last line if not exactly 16 characters.
+ buf[(i % 16) + 1] = '\0';
+ }
+ if (i % 16 <= 8 && i % 16 != 0) fprintf(fp, " ");
while ((i % 16) != 0) {
fprintf(fp, " ");
i++;
}
-
- // And print the final ASCII bit.
- fprintf(fp, " %s\n", buff);
+ fprintf(fp, " |%s|\n", buf);
}
#ifdef __cplusplus
