2 * Helper to hexdump a buffer
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
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qemu-common.h"
19 void qemu_hexdump_line(char *line
, unsigned int b
, const void *bufptr
,
20 unsigned int len
, bool ascii
)
22 const char *buf
= bufptr
;
25 if (len
> QEMU_HEXDUMP_LINE_BYTES
) {
26 len
= QEMU_HEXDUMP_LINE_BYTES
;
29 line
+= snprintf(line
, 6, "%04x:", b
);
30 for (i
= 0; i
< QEMU_HEXDUMP_LINE_BYTES
; i
++) {
35 line
+= sprintf(line
, " %02x", (unsigned char)buf
[b
+ i
]);
37 line
+= sprintf(line
, " ");
42 for (i
= 0; i
< len
; i
++) {
44 if (c
< ' ' || c
> '~') {
53 void qemu_hexdump(FILE *fp
, const char *prefix
,
54 const void *bufptr
, size_t size
)
57 char line
[QEMU_HEXDUMP_LINE_LEN
];
59 for (b
= 0; b
< size
; b
+= QEMU_HEXDUMP_LINE_BYTES
) {
61 qemu_hexdump_line(line
, b
, bufptr
, len
, true);
62 fprintf(fp
, "%s: %s\n", prefix
, line
);