xref: /openbmc/qemu/util/hexdump.c (revision a68694cd)
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-common.h"
18 
19 void qemu_hexdump(FILE *fp, const char *prefix,
20                   const void *bufptr, size_t size)
21 {
22     const char *buf = bufptr;
23     unsigned int b, len, i, c;
24 
25     for (b = 0; b < size; b += 16) {
26         len = size - b;
27         if (len > 16) {
28             len = 16;
29         }
30         fprintf(fp, "%s: %04x:", prefix, b);
31         for (i = 0; i < 16; i++) {
32             if ((i % 4) == 0) {
33                 fprintf(fp, " ");
34             }
35             if (i < len) {
36                 fprintf(fp, " %02x", (unsigned char)buf[b + i]);
37             } else {
38                 fprintf(fp, "   ");
39             }
40         }
41         fprintf(fp, " ");
42         for (i = 0; i < len; i++) {
43             c = buf[b + i];
44             if (c < ' ' || c > '~') {
45                 c = '.';
46             }
47             fprintf(fp, "%c", c);
48         }
49         fprintf(fp, "\n");
50     }
51 }
52