1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include "qemu/osdep.h"
17#include "qemu-common.h"
18
19void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr,
20 unsigned int len, bool ascii)
21{
22 const char *buf = bufptr;
23 int i, c;
24
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++ = ' ';
33 }
34 if (i < len) {
35 line += sprintf(line, " %02x", (unsigned char)buf[b + i]);
36 } else {
37 line += sprintf(line, " ");
38 }
39 }
40 if (ascii) {
41 *line++ = ' ';
42 for (i = 0; i < len; i++) {
43 c = buf[b + i];
44 if (c < ' ' || c > '~') {
45 c = '.';
46 }
47 *line++ = c;
48 }
49 }
50 *line = '\0';
51}
52
53void 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
65}
66