Patrick Georgi | 55189c9 | 2020-05-10 20:09:31 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause or GPL-2.0 */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 2 | |
| 3 | #include "hexdump.h" |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 4 | #include <ctype.h> |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 5 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 6 | static void addrprint(FILE * outfile, uint64_t address, int width); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 7 | |
| 8 | /*-------------------------------------------------------------------------- |
| 9 | * hexdump |
| 10 | * |
| 11 | * Write a hex dump of 'mem' to 'outfile'. |
| 12 | * |
| 13 | * parameters: |
| 14 | * mem: a pointer to the memory to display |
| 15 | * bytes: the number of bytes of data to display |
| 16 | * addrprint_start: The address to associate with the first byte of |
| 17 | * data. For instance, a value of 0 indicates that the |
| 18 | * first byte displayed should be labeled as byte 0. |
| 19 | * outfile: The place where the hex dump should be written. |
| 20 | * For instance, stdout or stderr may be passed here. |
| 21 | * format: A structure specifying how the hex dump should be |
| 22 | * formatted. |
| 23 | *--------------------------------------------------------------------------*/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 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; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 29 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 30 | /* Quietly return if the caller asks us to do something unreasonable. */ |
| 31 | if ((format->bytes_per_line <= 0) || (bytes < 0)) |
| 32 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 33 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 34 | p = (const unsigned char *)mem; |
| 35 | index = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 36 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 37 | /* Each iteration handles one full line of output. When loop |
| 38 | * terminates, the number of remaining bytes to display (if any) |
| 39 | * will not be enough to fill an entire line. |
| 40 | */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 41 | for (bytes_left = bytes; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 42 | bytes_left >= format->bytes_per_line; |
| 43 | bytes_left -= format->bytes_per_line) { |
| 44 | /* print start address for current line */ |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 45 | fprintf(outfile, "%s", format->indent); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 46 | addrprint(outfile, addrprint_start + index, |
| 47 | format->addrprint_width); |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 48 | fprintf(outfile, "%s", format->sep1); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 49 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 50 | /* display the bytes in hex */ |
| 51 | for (i = 0;;) { |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 52 | fprintf(outfile, "%02x", p[index++]); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 53 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 54 | if (++i >= format->bytes_per_line) |
| 55 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 56 | |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 57 | fprintf(outfile, "%s", format->sep2); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 58 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 59 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 60 | index -= format->bytes_per_line; |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 61 | fprintf(outfile, "%s", format->sep3); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 62 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 63 | /* display the bytes as characters */ |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 64 | for (i = 0; i < format->bytes_per_line; i++, index++) |
| 65 | fputc(isprint(p[index])?p[index]:format->nonprintable, outfile); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 66 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 67 | fprintf(outfile, "\n"); |
| 68 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 69 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 70 | if (bytes_left == 0) |
| 71 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 72 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 73 | /* print start address for last line */ |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 74 | fprintf(outfile, "%s", format->indent); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 75 | addrprint(outfile, addrprint_start + index, format->addrprint_width); |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 76 | fprintf(outfile, "%s", format->sep1); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 77 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 78 | /* display bytes for last line in hex */ |
| 79 | for (i = 0; i < bytes_left; i++) { |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 80 | fprintf(outfile, "%02x", p[index++]); |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 81 | fprintf(outfile, "%s", format->sep2); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 82 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 83 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 84 | index -= bytes_left; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 85 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 86 | /* pad the rest of the hex byte area with spaces */ |
| 87 | for (;;) { |
| 88 | fprintf(outfile, " "); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 89 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 90 | if (++i >= format->bytes_per_line) |
| 91 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 92 | |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 93 | fprintf(outfile, "%s", format->sep2); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 94 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 95 | |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 96 | fprintf(outfile, "%s", format->sep3); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 97 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 98 | /* display bytes for last line as characters */ |
| 99 | for (i = 0; i < bytes_left; i++) |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 100 | fputc(isprint(p[index])?p[index++]:format->nonprintable, outfile); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 101 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 102 | /* pad the rest of the character area with spaces */ |
| 103 | for (; i < format->bytes_per_line; i++) |
| 104 | fprintf(outfile, " "); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 105 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 106 | fprintf(outfile, "\n"); |
| 107 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 108 | |
| 109 | /*-------------------------------------------------------------------------- |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 110 | * addrprint |
| 111 | * |
| 112 | * Display an address as a hexadecimal number. |
| 113 | * |
| 114 | * parameters: |
| 115 | * outfile: the place where the output should be written |
| 116 | * address: the address to display |
| 117 | * width: The number of bytes wide the address should be displayed as. |
| 118 | * Must be a value from 1 to 8. |
| 119 | *--------------------------------------------------------------------------*/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 120 | static void addrprint(FILE * outfile, uint64_t address, int width) |
| 121 | { |
| 122 | char s[17]; |
| 123 | int i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 124 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 125 | /* force the user's input to be valid */ |
| 126 | if (width < 1) |
| 127 | width = 1; |
| 128 | else if (width > 8) |
| 129 | width = 8; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 130 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 131 | /* convert address to string */ |
| 132 | sprintf(s, "%016llx", (unsigned long long)address); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 133 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 134 | /* write it out, with colons separating consecutive 16-bit |
| 135 | * chunks of the address |
| 136 | */ |
| 137 | for (i = 16 - (2 * width);;) { |
| 138 | fprintf(outfile, "%c", s[i]); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 139 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 140 | if (++i >= 16) |
| 141 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 142 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 143 | if ((i % 4) == 0) |
| 144 | fprintf(outfile, ":"); |
| 145 | } |
| 146 | } |