]> git.ipfire.org Git - thirdparty/qemu.git/blame - util/hexdump.c
Merge tag 'pull-target-arm-20240919' of https://git.linaro.org/people/pmaydell/qemu...
[thirdparty/qemu.git] / util / hexdump.c
CommitLineData
6ff66f50 1/*
c49d1c37 2* Helper to hexdump a buffer
6ff66f50
PC
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
aafd7584 16#include "qemu/osdep.h"
415b7327 17#include "qemu/cutils.h"
6ff66f50 18
10e4927b
RH
19static inline char hexdump_nibble(unsigned x)
20{
21 return (x < 10 ? '0' : 'a' - 10) + x;
22}
23
c49d1c37
RH
24GString *qemu_hexdump_line(GString *str, const void *vbuf, size_t len,
25 size_t unit_len, size_t block_len)
6ff66f50 26{
53ee5f55 27 const uint8_t *buf = vbuf;
c49d1c37 28 size_t u, b;
6ff66f50 29
53ee5f55
RH
30 if (str == NULL) {
31 /* Estimate the length of the output to avoid reallocs. */
c49d1c37
RH
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);
bbb16908
LV
40 }
41
c49d1c37 42 for (u = 0, b = 0; len; u++, b++, len--, buf++) {
10e4927b
RH
43 uint8_t c;
44
c49d1c37
RH
45 if (unit_len && u == unit_len) {
46 g_string_append_c(str, ' ');
47 u = 0;
48 }
49 if (block_len && b == block_len) {
53ee5f55 50 g_string_append_c(str, ' ');
c49d1c37 51 b = 0;
6ff66f50 52 }
10e4927b
RH
53
54 c = *buf;
55 g_string_append_c(str, hexdump_nibble(c / 16));
56 g_string_append_c(str, hexdump_nibble(c % 16));
bbb16908 57 }
53ee5f55
RH
58
59 return str;
13dfa933
RH
60}
61
62static 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 = '.';
6ff66f50 71 }
13dfa933 72 *line++ = c;
6ff66f50 73 }
bbb16908
LV
74 *line = '\0';
75}
76
53ee5f55 77#define QEMU_HEXDUMP_LINE_BYTES 16
13dfa933
RH
78#define QEMU_HEXDUMP_LINE_WIDTH \
79 (QEMU_HEXDUMP_LINE_BYTES * 2 + QEMU_HEXDUMP_LINE_BYTES / 4)
80
bbb16908
LV
81void qemu_hexdump(FILE *fp, const char *prefix,
82 const void *bufptr, size_t size)
83{
53ee5f55 84 g_autoptr(GString) str = g_string_sized_new(QEMU_HEXDUMP_LINE_WIDTH + 1);
13dfa933
RH
85 char ascii[QEMU_HEXDUMP_LINE_BYTES + 1];
86 size_t b, len;
87
88 for (b = 0; b < size; b += len) {
89 len = MIN(size - b, QEMU_HEXDUMP_LINE_BYTES);
90
53ee5f55 91 g_string_truncate(str, 0);
c49d1c37 92 qemu_hexdump_line(str, bufptr + b, len, 1, 4);
13dfa933 93 asciidump_line(ascii, bufptr + b, len);
bbb16908 94
13dfa933 95 fprintf(fp, "%s: %04zx: %-*s %s\n",
53ee5f55 96 prefix, b, QEMU_HEXDUMP_LINE_WIDTH, str->str, ascii);
bbb16908
LV
97 }
98
6ff66f50 99}