1 /*****************************************************************************\
4 \*****************************************************************************/
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);
48 static void hexprint (FILE *outfile, unsigned char byte);
49 static void charprint (FILE *outfile, unsigned char byte,
50 unsigned char nonprintable,
51 is_printable_fn_t is_printable_fn);
53 /*--------------------------------------------------------------------------
56 * Write a hex dump of 'mem' to 'outfile'.
59 * mem: a pointer to the memory to display
60 * bytes: the number of bytes of data to display
61 * addrprint_start: The address to associate with the first byte of
62 * data. For instance, a value of 0 indicates that the
63 * first byte displayed should be labeled as byte 0.
64 * outfile: The place where the hex dump should be written.
65 * For instance, stdout or stderr may be passed here.
66 * format: A structure specifying how the hex dump should be
68 *--------------------------------------------------------------------------*/
69 void hexdump (const void *mem, int bytes, uint64_t addrprint_start,
70 FILE *outfile, const hexdump_format_t *format)
71 { int bytes_left, index, i;
72 const unsigned char *p;
73 is_printable_fn_t is_printable_fn;
75 /* Quietly return if the caller asks us to do something unreasonable. */
76 if ((format->bytes_per_line <= 0) || (bytes < 0))
79 is_printable_fn = format->is_printable_fn;
81 if (is_printable_fn == NULL)
82 is_printable_fn = default_is_printable_fn;
84 p = (const unsigned char *) mem;
87 /* Each iteration handles one full line of output. When loop terminates,
88 * the number of remaining bytes to display (if any) will not be enough to
89 * fill an entire line.
91 for (bytes_left = bytes;
92 bytes_left >= format->bytes_per_line;
93 bytes_left -= format->bytes_per_line)
94 { /* print start address for current line */
95 fprintf(outfile, format->indent);
96 addrprint(outfile, addrprint_start + index, format->addrprint_width);
97 fprintf(outfile, format->sep1);
99 /* display the bytes in hex */
101 { hexprint(outfile, p[index++]);
103 if (++i >= format->bytes_per_line)
106 fprintf(outfile, format->sep2);
109 index -= format->bytes_per_line;
110 fprintf(outfile, format->sep3);
112 /* display the bytes as characters */
113 for (i = 0; i < format->bytes_per_line; i++)
114 charprint(outfile, p[index++], format->nonprintable,
117 fprintf(outfile, "\n");
123 /* print start address for last line */
124 fprintf(outfile, format->indent);
125 addrprint(outfile, addrprint_start + index, format->addrprint_width);
126 fprintf(outfile, format->sep1);
128 /* display bytes for last line in hex */
129 for (i = 0; i < bytes_left; i++)
130 { hexprint(outfile, p[index++]);
131 fprintf(outfile, format->sep2);
136 /* pad the rest of the hex byte area with spaces */
138 { fprintf(outfile, " ");
140 if (++i >= format->bytes_per_line)
143 fprintf(outfile, format->sep2);
146 fprintf(outfile, format->sep3);
148 /* display bytes for last line as characters */
149 for (i = 0; i < bytes_left; i++)
150 charprint(outfile, p[index++], format->nonprintable, is_printable_fn);
152 /* pad the rest of the character area with spaces */
153 for (; i < format->bytes_per_line; i++)
154 fprintf(outfile, " ");
156 fprintf(outfile, "\n");
159 /*--------------------------------------------------------------------------
160 * default_is_printable_fn
162 * Determine whether the input character is printable. The proper behavior
163 * for this type of function may be system-dependent. This function takes a
164 * conservative approach. If it is not adequate for your purposes, you can
168 * c: the input character
171 * Return 1 if the input character is printable. Otherwise return 0.
172 *--------------------------------------------------------------------------*/
173 int default_is_printable_fn (unsigned char c)
174 { return (c >= 0x20) && (c <= 0x7e); }
176 /*--------------------------------------------------------------------------
179 * Display an address as a hexadecimal number.
182 * outfile: the place where the output should be written
183 * address: the address to display
184 * width: The number of bytes wide the address should be displayed as.
185 * Must be a value from 1 to 8.
186 *--------------------------------------------------------------------------*/
187 static void addrprint (FILE *outfile, uint64_t address, int width)
191 /* force the user's input to be valid */
197 /* convert address to string */
198 sprintf(s, "%016llx", (unsigned long long) address);
200 /* write it out, with colons separating consecutive 16-bit chunks of the
203 for (i = 16 - (2 * width); ; )
204 { fprintf(outfile, "%c", s[i]);
210 fprintf(outfile, ":");
214 /*--------------------------------------------------------------------------
217 * Display a byte as a two digit hex value.
220 * outfile: the place where the output should be written
221 * byte: the byte to display
222 *--------------------------------------------------------------------------*/
223 static void hexprint (FILE *outfile, unsigned char byte)
224 { static const char tbl[] =
225 { '0', '1', '2', '3', '4', '5', '6', '7',
226 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
229 fprintf(outfile, "%c%c", tbl[byte >> 4], tbl[byte & 0x0f]);
232 /*--------------------------------------------------------------------------
235 * Display a byte as its character representation.
238 * outfile: the place where the output should be written
239 * byte: the byte to display
240 * nonprintable: a substitute character to display if the byte
241 * represents a nonprintable character
242 * is_printable_fn: a function that returns a boolean value indicating
243 * whether a given character is printable
244 *--------------------------------------------------------------------------*/
245 static void charprint (FILE *outfile, unsigned char byte,
246 unsigned char nonprintable,
247 is_printable_fn_t is_printable_fn)
248 { fprintf(outfile, "%c", is_printable_fn(byte) ? byte : nonprintable); }