From be0efd884d63079163c747e0d9382456a500007f Mon Sep 17 00:00:00 2001 From: William Lallemand Date: Thu, 22 Nov 2012 18:01:40 +0100 Subject: [PATCH] MINOR: buffer_dump with ASCII Improve the buffer_dump function with ASCII output. --- src/buffer.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index 899f5f4952..281947ab29 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -10,6 +10,7 @@ * */ +#include #include #include @@ -198,23 +199,32 @@ void buffer_bounce_realign(struct buffer *buf) void buffer_dump(FILE *o, struct buffer *b, int from, int to) { fprintf(o, "Dumping buffer %p\n", b); - fprintf(o, " data=%p o=%d i=%d p=%p\n", - b->data, b->o, b->i, b->p); - - if (!to || to > buffer_len(b)) - to = buffer_len(b); + fprintf(o, " data=%p o=%d i=%d p=%p\n" + " relative: p=0x%04x\n", + b->data, b->o, b->i, b->p, (unsigned int)(b->p - b->data)); fprintf(o, "Dumping contents from byte %d to byte %d\n", from, to); - for (; from < to; from++) { - if ((from & 15) == 0) - fprintf(o, " %04x: ", from); - fprintf(o, "%02x ", b->data[from]); - if ((from & 15) == 7) - fprintf(o, "- "); - else if (((from & 15) == 15) && (from != to-1)) - fprintf(o, "\n"); + fprintf(o, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); + /* dump hexa */ + while (from < to) { + int i; + + fprintf(o, " %04x: ", from); + for (i = 0; ((from + i) < to) && (i < 16) ; i++) { + fprintf(o, "%02x ", (unsigned char)b->data[from + i]); + if (((from + i) & 15) == 7) + fprintf(o, "- "); + } + fprintf(o, " "); + for (i = 0; (from + i < to) && (i < 16) ; i++) { + fprintf(o, "%c", isprint(b->data[from + i]) ? b->data[from + i] : '.') ; + if ((((from + i) & 15) == 15) && ((from + i) != to-1)) + fprintf(o, "\n"); + } + from += i; } fprintf(o, "\n--\n"); + fflush(o); } -- 2.39.5