Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * hexdump.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 3 | \*****************************************************************************/ |
| 4 | |
| 5 | #include "hexdump.h" |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 6 | #include <ctype.h> |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 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 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 47 | static void addrprint(FILE * outfile, uint64_t address, int width); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 48 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 65 | void 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 70 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 71 | /* Quietly return if the caller asks us to do something unreasonable. */ |
| 72 | if ((format->bytes_per_line <= 0) || (bytes < 0)) |
| 73 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 74 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 75 | p = (const unsigned char *)mem; |
| 76 | index = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 77 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 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. |
| 81 | */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 82 | for (bytes_left = bytes; |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 83 | bytes_left >= format->bytes_per_line; |
| 84 | bytes_left -= format->bytes_per_line) { |
| 85 | /* print start address for current line */ |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 86 | fprintf(outfile, "%s", format->indent); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 87 | addrprint(outfile, addrprint_start + index, |
| 88 | format->addrprint_width); |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 89 | fprintf(outfile, "%s", format->sep1); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 90 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 91 | /* display the bytes in hex */ |
| 92 | for (i = 0;;) { |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 93 | fprintf(outfile, "%02x", p[index++]); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 94 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 95 | if (++i >= format->bytes_per_line) |
| 96 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 97 | |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 98 | fprintf(outfile, "%s", format->sep2); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 99 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 100 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 101 | index -= format->bytes_per_line; |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 102 | fprintf(outfile, "%s", format->sep3); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 103 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 104 | /* display the bytes as characters */ |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 105 | for (i = 0; i < format->bytes_per_line; i++, index++) |
| 106 | fputc(isprint(p[index])?p[index]:format->nonprintable, outfile); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 107 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 108 | fprintf(outfile, "\n"); |
| 109 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 110 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 111 | if (bytes_left == 0) |
| 112 | return; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 113 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 114 | /* print start address for last line */ |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 115 | fprintf(outfile, "%s", format->indent); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 116 | addrprint(outfile, addrprint_start + index, format->addrprint_width); |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 117 | fprintf(outfile, "%s", format->sep1); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 118 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 119 | /* display bytes for last line in hex */ |
| 120 | for (i = 0; i < bytes_left; i++) { |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 121 | fprintf(outfile, "%02x", p[index++]); |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 122 | fprintf(outfile, "%s", format->sep2); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 123 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 124 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 125 | index -= bytes_left; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 126 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 127 | /* pad the rest of the hex byte area with spaces */ |
| 128 | for (;;) { |
| 129 | fprintf(outfile, " "); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 130 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 131 | if (++i >= format->bytes_per_line) |
| 132 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 133 | |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 134 | fprintf(outfile, "%s", format->sep2); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 135 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 136 | |
Vikram Narayanan | f42c377 | 2012-04-07 16:28:26 +0530 | [diff] [blame] | 137 | fprintf(outfile, "%s", format->sep3); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 138 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 139 | /* display bytes for last line as characters */ |
| 140 | for (i = 0; i < bytes_left; i++) |
Patrick Georgi | c7ca3e5 | 2011-01-28 07:41:10 +0000 | [diff] [blame] | 141 | fputc(isprint(p[index])?p[index++]:format->nonprintable, outfile); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 142 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 143 | /* pad the rest of the character area with spaces */ |
| 144 | for (; i < format->bytes_per_line; i++) |
| 145 | fprintf(outfile, " "); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 146 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 147 | fprintf(outfile, "\n"); |
| 148 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 149 | |
| 150 | /*-------------------------------------------------------------------------- |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 151 | * 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 161 | static void addrprint(FILE * outfile, uint64_t address, int width) |
| 162 | { |
| 163 | char s[17]; |
| 164 | int i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 165 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 166 | /* force the user's input to be valid */ |
| 167 | if (width < 1) |
| 168 | width = 1; |
| 169 | else if (width > 8) |
| 170 | width = 8; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 171 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 172 | /* convert address to string */ |
| 173 | sprintf(s, "%016llx", (unsigned long long)address); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 174 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 175 | /* 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 180 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 181 | if (++i >= 16) |
| 182 | break; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 183 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 184 | if ((i % 4) == 0) |
| 185 | fprintf(outfile, ":"); |
| 186 | } |
| 187 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 188 | |