blob: 4f0023dd73d64182d4c0f53522784045d9ee977b [file] [log] [blame]
Stefan Reinauer6540ae52007-07-12 16:35:42 +00001/*****************************************************************************\
2 * hexdump.c
Stefan Reinauer6540ae52007-07-12 16:35:42 +00003\*****************************************************************************/
4
5#include "hexdump.h"
Patrick Georgic7ca3e52011-01-28 07:41:10 +00006#include <ctype.h>
Stefan Reinauer6540ae52007-07-12 16:35:42 +00007
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
Stefan Reinauer90b96b62010-01-13 21:00:23 +000047static void addrprint(FILE * outfile, uint64_t address, int width);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000048
49/*--------------------------------------------------------------------------
50 * hexdump
51 *
52 * Write a hex dump of 'mem' to 'outfile'.
53 *
54 * parameters:
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
63 * formatted.
64 *--------------------------------------------------------------------------*/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000065void hexdump(const void *mem, int bytes, uint64_t addrprint_start,
66 FILE * outfile, const hexdump_format_t * format)
67{
68 int bytes_left, index, i;
69 const unsigned char *p;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000070
Stefan Reinauer90b96b62010-01-13 21:00:23 +000071 /* Quietly return if the caller asks us to do something unreasonable. */
72 if ((format->bytes_per_line <= 0) || (bytes < 0))
73 return;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000074
Stefan Reinauer90b96b62010-01-13 21:00:23 +000075 p = (const unsigned char *)mem;
76 index = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000077
Stefan Reinauer90b96b62010-01-13 21:00:23 +000078 /* 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.
81 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000082 for (bytes_left = bytes;
Stefan Reinauer90b96b62010-01-13 21:00:23 +000083 bytes_left >= format->bytes_per_line;
84 bytes_left -= format->bytes_per_line) {
85 /* print start address for current line */
Vikram Narayananf42c3772012-04-07 16:28:26 +053086 fprintf(outfile, "%s", format->indent);
Stefan Reinauer90b96b62010-01-13 21:00:23 +000087 addrprint(outfile, addrprint_start + index,
88 format->addrprint_width);
Vikram Narayananf42c3772012-04-07 16:28:26 +053089 fprintf(outfile, "%s", format->sep1);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000090
Stefan Reinauer90b96b62010-01-13 21:00:23 +000091 /* display the bytes in hex */
92 for (i = 0;;) {
Patrick Georgic7ca3e52011-01-28 07:41:10 +000093 fprintf(outfile, "%02x", p[index++]);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000094
Stefan Reinauer90b96b62010-01-13 21:00:23 +000095 if (++i >= format->bytes_per_line)
96 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000097
Vikram Narayananf42c3772012-04-07 16:28:26 +053098 fprintf(outfile, "%s", format->sep2);
Stefan Reinauer90b96b62010-01-13 21:00:23 +000099 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000100
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000101 index -= format->bytes_per_line;
Vikram Narayananf42c3772012-04-07 16:28:26 +0530102 fprintf(outfile, "%s", format->sep3);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000103
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000104 /* display the bytes as characters */
Patrick Georgic7ca3e52011-01-28 07:41:10 +0000105 for (i = 0; i < format->bytes_per_line; i++, index++)
106 fputc(isprint(p[index])?p[index]:format->nonprintable, outfile);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000107
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000108 fprintf(outfile, "\n");
109 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000110
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000111 if (bytes_left == 0)
112 return;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000113
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000114 /* print start address for last line */
Vikram Narayananf42c3772012-04-07 16:28:26 +0530115 fprintf(outfile, "%s", format->indent);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000116 addrprint(outfile, addrprint_start + index, format->addrprint_width);
Vikram Narayananf42c3772012-04-07 16:28:26 +0530117 fprintf(outfile, "%s", format->sep1);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000118
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000119 /* display bytes for last line in hex */
120 for (i = 0; i < bytes_left; i++) {
Patrick Georgic7ca3e52011-01-28 07:41:10 +0000121 fprintf(outfile, "%02x", p[index++]);
Vikram Narayananf42c3772012-04-07 16:28:26 +0530122 fprintf(outfile, "%s", format->sep2);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000123 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000124
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000125 index -= bytes_left;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000126
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000127 /* pad the rest of the hex byte area with spaces */
128 for (;;) {
129 fprintf(outfile, " ");
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000130
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000131 if (++i >= format->bytes_per_line)
132 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000133
Vikram Narayananf42c3772012-04-07 16:28:26 +0530134 fprintf(outfile, "%s", format->sep2);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000135 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000136
Vikram Narayananf42c3772012-04-07 16:28:26 +0530137 fprintf(outfile, "%s", format->sep3);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000138
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000139 /* display bytes for last line as characters */
140 for (i = 0; i < bytes_left; i++)
Patrick Georgic7ca3e52011-01-28 07:41:10 +0000141 fputc(isprint(p[index])?p[index++]:format->nonprintable, outfile);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000142
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000143 /* pad the rest of the character area with spaces */
144 for (; i < format->bytes_per_line; i++)
145 fprintf(outfile, " ");
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000146
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000147 fprintf(outfile, "\n");
148}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000149
150/*--------------------------------------------------------------------------
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000151 * addrprint
152 *
153 * Display an address as a hexadecimal number.
154 *
155 * parameters:
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 *--------------------------------------------------------------------------*/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000161static void addrprint(FILE * outfile, uint64_t address, int width)
162{
163 char s[17];
164 int i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000165
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000166 /* force the user's input to be valid */
167 if (width < 1)
168 width = 1;
169 else if (width > 8)
170 width = 8;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000171
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000172 /* convert address to string */
173 sprintf(s, "%016llx", (unsigned long long)address);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000174
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000175 /* write it out, with colons separating consecutive 16-bit
176 * chunks of the address
177 */
178 for (i = 16 - (2 * width);;) {
179 fprintf(outfile, "%c", s[i]);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000180
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000181 if (++i >= 16)
182 break;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000183
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000184 if ((i % 4) == 0)
185 fprintf(outfile, ":");
186 }
187}