1 /*****************************************************************************\
3 \*****************************************************************************/
11 * David S. Peterson. All rights reserved.
13 * Author: David S. Peterson <dave_peterson@pobox.com>
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
18 * 1. Redistributions of source code must retain the above copyright notice,
19 * this list of conditions, and the entire permission notice, including
20 * the following disclaimer of warranties.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions, and the entire permission notice,
23 * including the following disclaimer in the documentation and/or other
24 * materials provided with the distribution.
25 * 3. The name(s) of the author(s) may not be used to endorse or promote
26 * products derived from this software without specific prior written
29 * ALTERNATIVELY, this product may be distributed under the terms of the GNU
30 * General Public License, in which case the provisions of the GPL are
31 * required INSTEAD OF the above restrictions. (This clause is necessary due
32 * to a potential bad interaction between the GPL and the restrictions
33 * contained in a BSD-style copyright.)
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
38 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 static void addrprint(FILE * outfile
, uint64_t address
, int width
);
49 /*--------------------------------------------------------------------------
52 * Write a hex dump of 'mem' to 'outfile'.
55 * mem: a pointer to the memory to display
56 * bytes: the number of bytes of data to display
57 * addrprint_start: The address to associate with the first byte of
58 * data. For instance, a value of 0 indicates that the
59 * first byte displayed should be labeled as byte 0.
60 * outfile: The place where the hex dump should be written.
61 * For instance, stdout or stderr may be passed here.
62 * format: A structure specifying how the hex dump should be
64 *--------------------------------------------------------------------------*/
65 void hexdump(const void *mem
, int bytes
, uint64_t addrprint_start
,
66 FILE * outfile
, const hexdump_format_t
* format
)
68 int bytes_left
, index
, i
;
69 const unsigned char *p
;
71 /* Quietly return if the caller asks us to do something unreasonable. */
72 if ((format
->bytes_per_line
<= 0) || (bytes
< 0))
75 p
= (const unsigned char *)mem
;
78 /* Each iteration handles one full line of output. When loop
79 * terminates, the number of remaining bytes to display (if any)
80 * will not be enough to fill an entire line.
82 for (bytes_left
= bytes
;
83 bytes_left
>= format
->bytes_per_line
;
84 bytes_left
-= format
->bytes_per_line
) {
85 /* print start address for current line */
86 fprintf(outfile
, "%s", format
->indent
);
87 addrprint(outfile
, addrprint_start
+ index
,
88 format
->addrprint_width
);
89 fprintf(outfile
, "%s", format
->sep1
);
91 /* display the bytes in hex */
93 fprintf(outfile
, "%02x", p
[index
++]);
95 if (++i
>= format
->bytes_per_line
)
98 fprintf(outfile
, "%s", format
->sep2
);
101 index
-= format
->bytes_per_line
;
102 fprintf(outfile
, "%s", format
->sep3
);
104 /* display the bytes as characters */
105 for (i
= 0; i
< format
->bytes_per_line
; i
++, index
++)
106 fputc(isprint(p
[index
])?p
[index
]:format
->nonprintable
, outfile
);
108 fprintf(outfile
, "\n");
114 /* print start address for last line */
115 fprintf(outfile
, "%s", format
->indent
);
116 addrprint(outfile
, addrprint_start
+ index
, format
->addrprint_width
);
117 fprintf(outfile
, "%s", format
->sep1
);
119 /* display bytes for last line in hex */
120 for (i
= 0; i
< bytes_left
; i
++) {
121 fprintf(outfile
, "%02x", p
[index
++]);
122 fprintf(outfile
, "%s", format
->sep2
);
127 /* pad the rest of the hex byte area with spaces */
129 fprintf(outfile
, " ");
131 if (++i
>= format
->bytes_per_line
)
134 fprintf(outfile
, "%s", format
->sep2
);
137 fprintf(outfile
, "%s", format
->sep3
);
139 /* display bytes for last line as characters */
140 for (i
= 0; i
< bytes_left
; i
++)
141 fputc(isprint(p
[index
])?p
[index
++]:format
->nonprintable
, outfile
);
143 /* pad the rest of the character area with spaces */
144 for (; i
< format
->bytes_per_line
; i
++)
145 fprintf(outfile
, " ");
147 fprintf(outfile
, "\n");
150 /*--------------------------------------------------------------------------
153 * Display an address as a hexadecimal number.
156 * outfile: the place where the output should be written
157 * address: the address to display
158 * width: The number of bytes wide the address should be displayed as.
159 * Must be a value from 1 to 8.
160 *--------------------------------------------------------------------------*/
161 static void addrprint(FILE * outfile
, uint64_t address
, int width
)
166 /* force the user's input to be valid */
172 /* convert address to string */
173 sprintf(s
, "%016llx", (unsigned long long)address
);
175 /* write it out, with colons separating consecutive 16-bit
176 * chunks of the address
178 for (i
= 16 - (2 * width
);;) {
179 fprintf(outfile
, "%c", s
[i
]);
185 fprintf(outfile
, ":");