blob: a34f4ef88cd8384a37b428e57b4fcb57afd1662e [file] [log] [blame]
Patrick Georgi55189c92020-05-10 20:09:31 +02001/* SPDX-License-Identifier: BSD-3-Clause or GPL-2.0 */
Stefan Reinauer6540ae52007-07-12 16:35:42 +00002
3#include "hexdump.h"
Patrick Georgic7ca3e52011-01-28 07:41:10 +00004#include <ctype.h>
Stefan Reinauer6540ae52007-07-12 16:35:42 +00005
Stefan Reinauer90b96b62010-01-13 21:00:23 +00006static void addrprint(FILE * outfile, uint64_t address, int width);
Stefan Reinauer6540ae52007-07-12 16:35:42 +00007
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 Reinauer90b96b62010-01-13 21:00:23 +000024void 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 Reinauer6540ae52007-07-12 16:35:42 +000029
Stefan Reinauer90b96b62010-01-13 21:00:23 +000030 /* Quietly return if the caller asks us to do something unreasonable. */
31 if ((format->bytes_per_line <= 0) || (bytes < 0))
32 return;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000033
Stefan Reinauer90b96b62010-01-13 21:00:23 +000034 p = (const unsigned char *)mem;
35 index = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000036
Stefan Reinauer90b96b62010-01-13 21:00:23 +000037 /* 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 Reinauer14e22772010-04-27 06:56:47 +000041 for (bytes_left = bytes;
Stefan Reinauer90b96b62010-01-13 21:00:23 +000042 bytes_left >= format->bytes_per_line;
43 bytes_left -= format->bytes_per_line) {
44 /* print start address for current line */
Vikram Narayananf42c3772012-04-07 16:28:26 +053045 fprintf(outfile, "%s", format->indent);
Stefan Reinauer90b96b62010-01-13 21:00:23 +000046 addrprint(outfile, addrprint_start + index,
47 format->addrprint_width);
Vikram Narayananf42c3772012-04-07 16:28:26 +053048 fprintf(outfile, "%s", format->sep1);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000049
Stefan Reinauer90b96b62010-01-13 21:00:23 +000050 /* display the bytes in hex */
51 for (i = 0;;) {
Patrick Georgic7ca3e52011-01-28 07:41:10 +000052 fprintf(outfile, "%02x", p[index++]);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000053
Stefan Reinauer90b96b62010-01-13 21:00:23 +000054 if (++i >= format->bytes_per_line)
55 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000056
Vikram Narayananf42c3772012-04-07 16:28:26 +053057 fprintf(outfile, "%s", format->sep2);
Stefan Reinauer90b96b62010-01-13 21:00:23 +000058 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +000059
Stefan Reinauer90b96b62010-01-13 21:00:23 +000060 index -= format->bytes_per_line;
Vikram Narayananf42c3772012-04-07 16:28:26 +053061 fprintf(outfile, "%s", format->sep3);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000062
Stefan Reinauer90b96b62010-01-13 21:00:23 +000063 /* display the bytes as characters */
Patrick Georgic7ca3e52011-01-28 07:41:10 +000064 for (i = 0; i < format->bytes_per_line; i++, index++)
65 fputc(isprint(p[index])?p[index]:format->nonprintable, outfile);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000066
Stefan Reinauer90b96b62010-01-13 21:00:23 +000067 fprintf(outfile, "\n");
68 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +000069
Stefan Reinauer90b96b62010-01-13 21:00:23 +000070 if (bytes_left == 0)
71 return;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000072
Stefan Reinauer90b96b62010-01-13 21:00:23 +000073 /* print start address for last line */
Vikram Narayananf42c3772012-04-07 16:28:26 +053074 fprintf(outfile, "%s", format->indent);
Stefan Reinauer90b96b62010-01-13 21:00:23 +000075 addrprint(outfile, addrprint_start + index, format->addrprint_width);
Vikram Narayananf42c3772012-04-07 16:28:26 +053076 fprintf(outfile, "%s", format->sep1);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000077
Stefan Reinauer90b96b62010-01-13 21:00:23 +000078 /* display bytes for last line in hex */
79 for (i = 0; i < bytes_left; i++) {
Patrick Georgic7ca3e52011-01-28 07:41:10 +000080 fprintf(outfile, "%02x", p[index++]);
Vikram Narayananf42c3772012-04-07 16:28:26 +053081 fprintf(outfile, "%s", format->sep2);
Stefan Reinauer90b96b62010-01-13 21:00:23 +000082 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +000083
Stefan Reinauer90b96b62010-01-13 21:00:23 +000084 index -= bytes_left;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000085
Stefan Reinauer90b96b62010-01-13 21:00:23 +000086 /* pad the rest of the hex byte area with spaces */
87 for (;;) {
88 fprintf(outfile, " ");
Stefan Reinauer6540ae52007-07-12 16:35:42 +000089
Stefan Reinauer90b96b62010-01-13 21:00:23 +000090 if (++i >= format->bytes_per_line)
91 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000092
Vikram Narayananf42c3772012-04-07 16:28:26 +053093 fprintf(outfile, "%s", format->sep2);
Stefan Reinauer90b96b62010-01-13 21:00:23 +000094 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +000095
Vikram Narayananf42c3772012-04-07 16:28:26 +053096 fprintf(outfile, "%s", format->sep3);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000097
Stefan Reinauer90b96b62010-01-13 21:00:23 +000098 /* display bytes for last line as characters */
99 for (i = 0; i < bytes_left; i++)
Patrick Georgic7ca3e52011-01-28 07:41:10 +0000100 fputc(isprint(p[index])?p[index++]:format->nonprintable, outfile);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000101
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000102 /* pad the rest of the character area with spaces */
103 for (; i < format->bytes_per_line; i++)
104 fprintf(outfile, " ");
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000105
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000106 fprintf(outfile, "\n");
107}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000108
109/*--------------------------------------------------------------------------
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000110 * 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 Reinauer90b96b62010-01-13 21:00:23 +0000120static void addrprint(FILE * outfile, uint64_t address, int width)
121{
122 char s[17];
123 int i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000124
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000125 /* force the user's input to be valid */
126 if (width < 1)
127 width = 1;
128 else if (width > 8)
129 width = 8;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000130
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000131 /* convert address to string */
132 sprintf(s, "%016llx", (unsigned long long)address);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000133
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000134 /* 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 Reinauer6540ae52007-07-12 16:35:42 +0000139
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000140 if (++i >= 16)
141 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000142
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000143 if ((i % 4) == 0)
144 fprintf(outfile, ":");
145 }
146}