master
c 127 lines 3.5 KB
Raw
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
16 #include "qemu/osdep.h"
17 #include "qemu/cutils.h"
18 #include "qemu/host-utils.h"
19
20 static inline char hexdump_nibble(unsigned x)
21 {
22 return (x < 10 ? '0' : 'a' - 10) + x;
23 }
24
25 static size_t hexdump_line_length(size_t buf_len, size_t unit_len,
26 size_t block_len)
27 {
28 size_t est = buf_len * 2;
29 if (unit_len) {
30 est += buf_len / unit_len;
31 }
32 if (block_len) {
33 est += buf_len / block_len;
34 }
35 return est;
36 }
37
38 GString *qemu_hexdump_line(GString *str, const void *vbuf, size_t len,
39 size_t unit_len, size_t block_len)
40 {
41 const uint8_t *buf = vbuf;
42 size_t u, b;
43
44 if (str == NULL) {
45 /* Estimate the length of the output to avoid reallocs. */
46 str = g_string_sized_new(hexdump_line_length(len, unit_len, block_len)
47 + 1);
48 }
49
50 for (u = 0, b = 0; len; u++, b++, len--, buf++) {
51 uint8_t c;
52
53 if (unit_len && u == unit_len) {
54 g_string_append_c(str, ' ');
55 u = 0;
56 }
57 if (block_len && b == block_len) {
58 g_string_append_c(str, ' ');
59 b = 0;
60 }
61
62 c = *buf;
63 g_string_append_c(str, hexdump_nibble(c / 16));
64 g_string_append_c(str, hexdump_nibble(c % 16));
65 }
66
67 return str;
68 }
69
70 static void asciidump_line(char *line, const void *bufptr, size_t len)
71 {
72 const char *buf = bufptr;
73
74 for (size_t i = 0; i < len; i++) {
75 char c = buf[i];
76
77 if (c < ' ' || c > '~') {
78 c = '.';
79 }
80 *line++ = c;
81 }
82 *line = '\0';
83 }
84
85 #define QEMU_HEXDUMP_LINE_BYTES 16
86 #define QEMU_HEXDUMP_UNIT 1
87 #define QEMU_HEXDUMP_BLOCK 4
88
89 void qemu_hexdump(FILE *fp, const char *prefix,
90 const void *bufptr, size_t size)
91 {
92 int width = hexdump_line_length(QEMU_HEXDUMP_LINE_BYTES,
93 QEMU_HEXDUMP_UNIT,
94 QEMU_HEXDUMP_BLOCK);
95 g_autoptr(GString) str = g_string_sized_new(width + 1);
96 char ascii[QEMU_HEXDUMP_LINE_BYTES + 1];
97 size_t b, len;
98
99 for (b = 0; b < size; b += len) {
100 len = MIN(size - b, QEMU_HEXDUMP_LINE_BYTES);
101
102 g_string_truncate(str, 0);
103 qemu_hexdump_line(str, bufptr + b, len,
104 QEMU_HEXDUMP_UNIT, QEMU_HEXDUMP_BLOCK);
105 asciidump_line(ascii, bufptr + b, len);
106
107 fprintf(fp, "%s: %04zx: %-*s %s\n", prefix, b, width, str->str, ascii);
108 }
109
110 }
111
112 void qemu_hexdump_to_buffer(char *restrict buffer, size_t buffer_size,
113 const uint8_t *restrict data, size_t data_size)
114 {
115 size_t i;
116 uint64_t required_buffer_size;
117 bool overflow = umul64_overflow(data_size, 2, &required_buffer_size);
118 overflow |= uadd64_overflow(required_buffer_size, 1, &required_buffer_size);
119 assert(!overflow && buffer_size >= required_buffer_size);
120
121 for (i = 0; i < data_size; i++) {
122 uint8_t val = data[i];
123 *(buffer++) = hexdump_nibble(val >> 4);
124 *(buffer++) = hexdump_nibble(val & 0xf);
125 }
126 *buffer = '\0';
127 }