blob: 00eb23f006a6a025c4a9c77232f3fdefba2c2505 [file] [log] [blame]
Stefan Reinauer6540ae52007-07-12 16:35:42 +00001/*****************************************************************************\
2 * hexdump.c
3 * $Id: hexdump.c,v 1.2 2005/12/16 22:45:49 dsp_llnl Exp $
4\*****************************************************************************/
5
6#include "hexdump.h"
7
8/* hexdump.c
9 *
10 * Copyright (C) 2002
11 * David S. Peterson. All rights reserved.
12 *
13 * Author: David S. Peterson <dave_peterson@pobox.com>
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met:
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
27 * permission.
28 *
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.)
34 *
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.
45 */
46
47static void addrprint (FILE *outfile, uint64_t address, int width);
48static void hexprint (FILE *outfile, unsigned char byte);
49static void charprint (FILE *outfile, unsigned char byte,
50 unsigned char nonprintable,
51 is_printable_fn_t is_printable_fn);
52
53/*--------------------------------------------------------------------------
54 * hexdump
55 *
56 * Write a hex dump of 'mem' to 'outfile'.
57 *
58 * parameters:
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
67 * formatted.
68 *--------------------------------------------------------------------------*/
69void 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;
74
75 /* Quietly return if the caller asks us to do something unreasonable. */
76 if ((format->bytes_per_line <= 0) || (bytes < 0))
77 return;
78
79 is_printable_fn = format->is_printable_fn;
80
81 if (is_printable_fn == NULL)
82 is_printable_fn = default_is_printable_fn;
83
84 p = (const unsigned char *) mem;
85 index = 0;
86
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.
90 */
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);
98
99 /* display the bytes in hex */
100 for (i = 0; ; )
101 { hexprint(outfile, p[index++]);
102
103 if (++i >= format->bytes_per_line)
104 break;
105
106 fprintf(outfile, format->sep2);
107 }
108
109 index -= format->bytes_per_line;
110 fprintf(outfile, format->sep3);
111
112 /* display the bytes as characters */
113 for (i = 0; i < format->bytes_per_line; i++)
114 charprint(outfile, p[index++], format->nonprintable,
115 is_printable_fn);
116
117 fprintf(outfile, "\n");
118 }
119
120 if (bytes_left == 0)
121 return;
122
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);
127
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);
132 }
133
134 index -= bytes_left;
135
136 /* pad the rest of the hex byte area with spaces */
137 for (; ; )
138 { fprintf(outfile, " ");
139
140 if (++i >= format->bytes_per_line)
141 break;
142
143 fprintf(outfile, format->sep2);
144 }
145
146 fprintf(outfile, format->sep3);
147
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);
151
152 /* pad the rest of the character area with spaces */
153 for (; i < format->bytes_per_line; i++)
154 fprintf(outfile, " ");
155
156 fprintf(outfile, "\n");
157 }
158
159/*--------------------------------------------------------------------------
160 * default_is_printable_fn
161 *
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
165 * write your own.
166 *
167 * parameters:
168 * c: the input character
169 *
170 * return value:
171 * Return 1 if the input character is printable. Otherwise return 0.
172 *--------------------------------------------------------------------------*/
173int default_is_printable_fn (unsigned char c)
174 { return (c >= 0x20) && (c <= 0x7e); }
175
176/*--------------------------------------------------------------------------
177 * addrprint
178 *
179 * Display an address as a hexadecimal number.
180 *
181 * parameters:
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 *--------------------------------------------------------------------------*/
187static void addrprint (FILE *outfile, uint64_t address, int width)
188 { char s[17];
189 int i;
190
191 /* force the user's input to be valid */
192 if (width < 1)
193 width = 1;
194 else if (width > 8)
195 width = 8;
196
197 /* convert address to string */
198 sprintf(s, "%016llx", (unsigned long long) address);
199
200 /* write it out, with colons separating consecutive 16-bit chunks of the
201 * address
202 */
203 for (i = 16 - (2 * width); ; )
204 { fprintf(outfile, "%c", s[i]);
205
206 if (++i >= 16)
207 break;
208
209 if ((i % 4) == 0)
210 fprintf(outfile, ":");
211 }
212 }
213
214/*--------------------------------------------------------------------------
215 * hexprint
216 *
217 * Display a byte as a two digit hex value.
218 *
219 * parameters:
220 * outfile: the place where the output should be written
221 * byte: the byte to display
222 *--------------------------------------------------------------------------*/
223static 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'
227 };
228
229 fprintf(outfile, "%c%c", tbl[byte >> 4], tbl[byte & 0x0f]);
230 }
231
232/*--------------------------------------------------------------------------
233 * charprint
234 *
235 * Display a byte as its character representation.
236 *
237 * parameters:
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 *--------------------------------------------------------------------------*/
245static 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); }