2013-03-15 20:41:58 +04:00
|
|
|
/*
|
2024-04-12 10:33:23 +03:00
|
|
|
* Helper to hexdump a buffer
|
2013-03-15 20:41:58 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2022-03-23 18:57:32 +03:00
|
|
|
#include "qemu/cutils.h"
|
2013-03-15 20:41:58 +04:00
|
|
|
|
2024-04-12 10:33:24 +03:00
|
|
|
static inline char hexdump_nibble(unsigned x)
|
|
|
|
{
|
|
|
|
return (x < 10 ? '0' : 'a' - 10) + x;
|
|
|
|
}
|
|
|
|
|
2024-04-12 10:33:23 +03:00
|
|
|
GString *qemu_hexdump_line(GString *str, const void *vbuf, size_t len,
|
|
|
|
size_t unit_len, size_t block_len)
|
2013-03-15 20:41:58 +04:00
|
|
|
{
|
2024-04-12 10:33:22 +03:00
|
|
|
const uint8_t *buf = vbuf;
|
2024-04-12 10:33:23 +03:00
|
|
|
size_t u, b;
|
2013-03-15 20:41:58 +04:00
|
|
|
|
2024-04-12 10:33:22 +03:00
|
|
|
if (str == NULL) {
|
|
|
|
/* Estimate the length of the output to avoid reallocs. */
|
2024-04-12 10:33:23 +03:00
|
|
|
size_t est = len * 2;
|
|
|
|
if (unit_len) {
|
|
|
|
est += len / unit_len;
|
|
|
|
}
|
|
|
|
if (block_len) {
|
|
|
|
est += len / block_len;
|
|
|
|
}
|
|
|
|
str = g_string_sized_new(est + 1);
|
2020-09-25 12:10:54 +03:00
|
|
|
}
|
|
|
|
|
2024-04-12 10:33:23 +03:00
|
|
|
for (u = 0, b = 0; len; u++, b++, len--, buf++) {
|
2024-04-12 10:33:24 +03:00
|
|
|
uint8_t c;
|
|
|
|
|
2024-04-12 10:33:23 +03:00
|
|
|
if (unit_len && u == unit_len) {
|
|
|
|
g_string_append_c(str, ' ');
|
|
|
|
u = 0;
|
|
|
|
}
|
|
|
|
if (block_len && b == block_len) {
|
2024-04-12 10:33:22 +03:00
|
|
|
g_string_append_c(str, ' ');
|
2024-04-12 10:33:23 +03:00
|
|
|
b = 0;
|
2013-03-15 20:41:58 +04:00
|
|
|
}
|
2024-04-12 10:33:24 +03:00
|
|
|
|
|
|
|
c = *buf;
|
|
|
|
g_string_append_c(str, hexdump_nibble(c / 16));
|
|
|
|
g_string_append_c(str, hexdump_nibble(c % 16));
|
2020-09-25 12:10:54 +03:00
|
|
|
}
|
2024-04-12 10:33:22 +03:00
|
|
|
|
|
|
|
return str;
|
2024-04-12 10:33:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void asciidump_line(char *line, const void *bufptr, size_t len)
|
|
|
|
{
|
|
|
|
const char *buf = bufptr;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
char c = buf[i];
|
|
|
|
|
|
|
|
if (c < ' ' || c > '~') {
|
|
|
|
c = '.';
|
2013-03-15 20:41:58 +04:00
|
|
|
}
|
2024-04-12 10:33:21 +03:00
|
|
|
*line++ = c;
|
2013-03-15 20:41:58 +04:00
|
|
|
}
|
2020-09-25 12:10:54 +03:00
|
|
|
*line = '\0';
|
|
|
|
}
|
|
|
|
|
2024-04-12 10:33:22 +03:00
|
|
|
#define QEMU_HEXDUMP_LINE_BYTES 16
|
2024-04-12 10:33:21 +03:00
|
|
|
#define QEMU_HEXDUMP_LINE_WIDTH \
|
|
|
|
(QEMU_HEXDUMP_LINE_BYTES * 2 + QEMU_HEXDUMP_LINE_BYTES / 4)
|
|
|
|
|
2020-09-25 12:10:54 +03:00
|
|
|
void qemu_hexdump(FILE *fp, const char *prefix,
|
|
|
|
const void *bufptr, size_t size)
|
|
|
|
{
|
2024-04-12 10:33:22 +03:00
|
|
|
g_autoptr(GString) str = g_string_sized_new(QEMU_HEXDUMP_LINE_WIDTH + 1);
|
2024-04-12 10:33:21 +03:00
|
|
|
char ascii[QEMU_HEXDUMP_LINE_BYTES + 1];
|
|
|
|
size_t b, len;
|
|
|
|
|
|
|
|
for (b = 0; b < size; b += len) {
|
|
|
|
len = MIN(size - b, QEMU_HEXDUMP_LINE_BYTES);
|
|
|
|
|
2024-04-12 10:33:22 +03:00
|
|
|
g_string_truncate(str, 0);
|
2024-04-12 10:33:23 +03:00
|
|
|
qemu_hexdump_line(str, bufptr + b, len, 1, 4);
|
2024-04-12 10:33:21 +03:00
|
|
|
asciidump_line(ascii, bufptr + b, len);
|
2020-09-25 12:10:54 +03:00
|
|
|
|
2024-04-12 10:33:21 +03:00
|
|
|
fprintf(fp, "%s: %04zx: %-*s %s\n",
|
2024-04-12 10:33:22 +03:00
|
|
|
prefix, b, QEMU_HEXDUMP_LINE_WIDTH, str->str, ascii);
|
2020-09-25 12:10:54 +03:00
|
|
|
}
|
|
|
|
|
2013-03-15 20:41:58 +04:00
|
|
|
}
|