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