aboutsummaryrefslogtreecommitdiff
path: root/util/hexdump.c
blob: 0b4662e701d83ac11d51971d4a60b698113f277a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * Helper to hexdump a buffer
 *
 * Copyright (c) 2013 Red Hat, Inc.
 * Copyright (c) 2013 Gerd Hoffmann <kraxel@redhat.com>
 * Copyright (c) 2013 Peter Crosthwaite <peter.crosthwaite@xilinx.com>
 * Copyright (c) 2013 Xilinx, Inc
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
 */

#include "qemu/osdep.h"
#include "qemu-common.h"

void qemu_hexdump(FILE *fp, const char *prefix,
                  const void *bufptr, size_t size)
{
    const char *buf = bufptr;
    unsigned int b, len, i, c;

    for (b = 0; b < size; b += 16) {
        len = size - b;
        if (len > 16) {
            len = 16;
        }
        fprintf(fp, "%s: %04x:", prefix, b);
        for (i = 0; i < 16; i++) {
            if ((i % 4) == 0) {
                fprintf(fp, " ");
            }
            if (i < len) {
                fprintf(fp, " %02x", (unsigned char)buf[b + i]);
            } else {
                fprintf(fp, "   ");
            }
        }
        fprintf(fp, " ");
        for (i = 0; i < len; i++) {
            c = buf[b + i];
            if (c < ' ' || c > '~') {
                c = '.';
            }
            fprintf(fp, "%c", c);
        }
        fprintf(fp, "\n");
    }
}