Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Helper to hexdump a buffer |
| 3 | * |
| 4 | * Copyright (c) 2013 Red Hat, Inc. |
| 5 | * Copyright (c) 2013 Gerd Hoffmann <kraxel@redhat.com> |
| 6 | * Copyright (c) 2013 Peter Crosthwaite <peter.crosthwaite@xilinx.com> |
| 7 | * Copyright (c) 2013 Xilinx, Inc |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
| 12 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 13 | * GNU GPL, version 2 or (at your option) any later version. |
| 14 | */ |
| 15 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 17 | #include "qemu-common.h" |
| 18 | |
Ed Maste | 3568ac2 | 2013-05-16 11:32:28 -0400 | [diff] [blame] | 19 | void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size) |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 20 | { |
Isaac Lozano | a155555 | 2016-03-25 03:42:15 -0700 | [diff] [blame] | 21 | unsigned int b, len, i, c; |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 22 | |
Isaac Lozano | a155555 | 2016-03-25 03:42:15 -0700 | [diff] [blame] | 23 | for (b = 0; b < size; b += 16) { |
| 24 | len = size - b; |
| 25 | if (len > 16) { |
| 26 | len = 16; |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 27 | } |
Isaac Lozano | a155555 | 2016-03-25 03:42:15 -0700 | [diff] [blame] | 28 | fprintf(fp, "%s: %04x:", prefix, b); |
| 29 | for (i = 0; i < 16; i++) { |
| 30 | if ((i % 4) == 0) { |
| 31 | fprintf(fp, " "); |
| 32 | } |
| 33 | if (i < len) { |
| 34 | fprintf(fp, " %02x", (unsigned char)buf[b + i]); |
| 35 | } else { |
| 36 | fprintf(fp, " "); |
| 37 | } |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 38 | } |
Isaac Lozano | a155555 | 2016-03-25 03:42:15 -0700 | [diff] [blame] | 39 | fprintf(fp, " "); |
| 40 | for (i = 0; i < len; i++) { |
| 41 | c = buf[b + i]; |
| 42 | if (c < ' ' || c > '~') { |
| 43 | c = '.'; |
| 44 | } |
| 45 | fprintf(fp, "%c", c); |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 46 | } |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 47 | fprintf(fp, "\n"); |
| 48 | } |
| 49 | } |