Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 1 | /* |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 2 | * Helper to hexdump a buffer |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 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 | |
Richard Henderson | 10e4927 | 2024-04-12 00:33:24 -0700 | [diff] [blame] | 19 | static inline char hexdump_nibble(unsigned x) |
| 20 | { |
| 21 | return (x < 10 ? '0' : 'a' - 10) + x; |
| 22 | } |
| 23 | |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 24 | GString *qemu_hexdump_line(GString *str, const void *vbuf, size_t len, |
| 25 | size_t unit_len, size_t block_len) |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 26 | { |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 27 | const uint8_t *buf = vbuf; |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 28 | size_t u, b; |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 29 | |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 30 | if (str == NULL) { |
| 31 | /* Estimate the length of the output to avoid reallocs. */ |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 32 | size_t est = len * 2; |
| 33 | if (unit_len) { |
| 34 | est += len / unit_len; |
| 35 | } |
| 36 | if (block_len) { |
| 37 | est += len / block_len; |
| 38 | } |
| 39 | str = g_string_sized_new(est + 1); |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 42 | for (u = 0, b = 0; len; u++, b++, len--, buf++) { |
Richard Henderson | 10e4927 | 2024-04-12 00:33:24 -0700 | [diff] [blame] | 43 | uint8_t c; |
| 44 | |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 45 | if (unit_len && u == unit_len) { |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 46 | g_string_append_c(str, ' '); |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 47 | u = 0; |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 48 | } |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 49 | if (block_len && b == block_len) { |
| 50 | g_string_append_c(str, ' '); |
| 51 | b = 0; |
| 52 | } |
Richard Henderson | 10e4927 | 2024-04-12 00:33:24 -0700 | [diff] [blame] | 53 | |
| 54 | c = *buf; |
| 55 | g_string_append_c(str, hexdump_nibble(c / 16)); |
| 56 | g_string_append_c(str, hexdump_nibble(c % 16)); |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 57 | } |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 58 | |
| 59 | return str; |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Richard Henderson | 13dfa93 | 2024-04-12 00:33:21 -0700 | [diff] [blame] | 62 | static void asciidump_line(char *line, const void *bufptr, size_t len) |
| 63 | { |
| 64 | const char *buf = bufptr; |
| 65 | |
| 66 | for (size_t i = 0; i < len; i++) { |
| 67 | char c = buf[i]; |
| 68 | |
| 69 | if (c < ' ' || c > '~') { |
| 70 | c = '.'; |
| 71 | } |
| 72 | *line++ = c; |
| 73 | } |
| 74 | *line = '\0'; |
| 75 | } |
| 76 | |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 77 | #define QEMU_HEXDUMP_LINE_BYTES 16 |
Richard Henderson | 13dfa93 | 2024-04-12 00:33:21 -0700 | [diff] [blame] | 78 | #define QEMU_HEXDUMP_LINE_WIDTH \ |
| 79 | (QEMU_HEXDUMP_LINE_BYTES * 2 + QEMU_HEXDUMP_LINE_BYTES / 4) |
| 80 | |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 81 | void qemu_hexdump(FILE *fp, const char *prefix, |
| 82 | const void *bufptr, size_t size) |
| 83 | { |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 84 | g_autoptr(GString) str = g_string_sized_new(QEMU_HEXDUMP_LINE_WIDTH + 1); |
Richard Henderson | 13dfa93 | 2024-04-12 00:33:21 -0700 | [diff] [blame] | 85 | char ascii[QEMU_HEXDUMP_LINE_BYTES + 1]; |
| 86 | size_t b, len; |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 87 | |
Richard Henderson | 13dfa93 | 2024-04-12 00:33:21 -0700 | [diff] [blame] | 88 | for (b = 0; b < size; b += len) { |
| 89 | len = MIN(size - b, QEMU_HEXDUMP_LINE_BYTES); |
| 90 | |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 91 | g_string_truncate(str, 0); |
Richard Henderson | c49d1c3 | 2024-04-12 00:33:23 -0700 | [diff] [blame] | 92 | qemu_hexdump_line(str, bufptr + b, len, 1, 4); |
Richard Henderson | 13dfa93 | 2024-04-12 00:33:21 -0700 | [diff] [blame] | 93 | asciidump_line(ascii, bufptr + b, len); |
| 94 | |
| 95 | fprintf(fp, "%s: %04zx: %-*s %s\n", |
Richard Henderson | 53ee5f5 | 2024-04-12 00:33:22 -0700 | [diff] [blame] | 96 | prefix, b, QEMU_HEXDUMP_LINE_WIDTH, str->str, ascii); |
Laurent Vivier | bbb1690 | 2020-09-25 11:10:54 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Peter Crosthwaite | 6ff66f5 | 2013-03-15 16:41:58 +0000 | [diff] [blame] | 99 | } |