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" |
Marc-André Lureau | 415b732 | 2022-03-23 19:57:32 +0400 | [diff] [blame] | 17 | #include "qemu/cutils.h" |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 18 | |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 19 | void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr, |
| 20 | unsigned int len, bool ascii) |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 21 | { |
Philippe Mathieu-Daudé | 67263b3 | 2020-08-22 20:09:49 +0200 | [diff] [blame] | 22 | const char *buf = bufptr; |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 23 | int i, c; |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 24 | |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 25 | if (len > QEMU_HEXDUMP_LINE_BYTES) { |
| 26 | len = QEMU_HEXDUMP_LINE_BYTES; |
| 27 | } |
| 28 | |
| 29 | line += snprintf(line, 6, "%04x:", b); |
| 30 | for (i = 0; i < QEMU_HEXDUMP_LINE_BYTES; i++) { |
| 31 | if ((i % 4) == 0) { |
| 32 | *line++ = ' '; |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 33 | } |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 34 | if (i < len) { |
| 35 | line += sprintf(line, " %02x", (unsigned char)buf[b + i]); |
| 36 | } else { |
| 37 | line += sprintf(line, " "); |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 38 | } |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 39 | } |
| 40 | if (ascii) { |
| 41 | *line++ = ' '; |
Isaac Lozano | a155555 | 2016-03-25 03:42:15 -0700 | [diff] [blame] | 42 | for (i = 0; i < len; i++) { |
| 43 | c = buf[b + i]; |
| 44 | if (c < ' ' || c > '~') { |
| 45 | c = '.'; |
| 46 | } |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 47 | *line++ = c; |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 48 | } |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 49 | } |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 50 | *line = '\0'; |
| 51 | } |
| 52 | |
| 53 | void qemu_hexdump(FILE *fp, const char *prefix, |
| 54 | const void *bufptr, size_t size) |
| 55 | { |
| 56 | unsigned int b, len; |
| 57 | char line[QEMU_HEXDUMP_LINE_LEN]; |
| 58 | |
| 59 | for (b = 0; b < size; b += QEMU_HEXDUMP_LINE_BYTES) { |
| 60 | len = size - b; |
| 61 | qemu_hexdump_line(line, b, bufptr, len, true); |
| 62 | fprintf(fp, "%s: %s\n", prefix, line); |
| 63 | } |
| 64 | |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 65 | } |