From a1555559ab5bad24fcd1c56fd39284afad8f5af7 Mon Sep 17 00:00:00 2001 From: Isaac Lozano <109lozanoi@gmail.com> Date: Fri, 25 Mar 2016 03:42:15 -0700 Subject: util: Improved qemu_hexmap() to include an ascii dump of the buffer qemu_hexdump() in util/hexdump.c has been changed to give also include a ascii dump of the buffer. Also, calls to hex_dump() in net/net.c have been replaced with calls to qemu_hexdump(). This takes care of two misc BiteSized Tasks. Reviewed-by: Thomas Huth Reviewed-by: Gerd Hoffmann Signed-off-by: Isaac Lozano <109lozanoi@gmail.com> Signed-off-by: Jason Wang --- util/hexdump.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'util') diff --git a/util/hexdump.c b/util/hexdump.c index 1d9c129..f879ff0 100644 --- a/util/hexdump.c +++ b/util/hexdump.c @@ -18,21 +18,32 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size) { - unsigned int b; + unsigned int b, len, i, c; - for (b = 0; b < size; b++) { - if ((b % 16) == 0) { - fprintf(fp, "%s: %04x:", prefix, b); + for (b = 0; b < size; b += 16) { + len = size - b; + if (len > 16) { + len = 16; } - if ((b % 4) == 0) { - fprintf(fp, " "); + 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, " %02x", (unsigned char)buf[b]); - if ((b % 16) == 15) { - fprintf(fp, "\n"); + fprintf(fp, " "); + for (i = 0; i < len; i++) { + c = buf[b + i]; + if (c < ' ' || c > '~') { + c = '.'; + } + fprintf(fp, "%c", c); } - } - if ((b % 16) != 0) { fprintf(fp, "\n"); } } -- cgit v1.1