From 5626572820d71c9af988aeb4326dd2c2ac6909ca Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 20 Sep 2004 18:03:32 +0000 Subject: [PATCH] Improve hex_dump(). --- src/filesys/fsutil.c | 2 +- src/lib/kernel/bitmap.c | 2 +- src/lib/stdio.c | 54 ++++++++++++++++++++++++++--------------- src/lib/stdio.h | 5 ++-- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/filesys/fsutil.c b/src/filesys/fsutil.c index 6336346..cc0e335 100644 --- a/src/filesys/fsutil.c +++ b/src/filesys/fsutil.c @@ -167,7 +167,7 @@ fsutil_print (const char *filename) if (n == 0) break; - hex_dump (buffer, n, true); + hex_dump (0, buffer, n, true); } palloc_free (buffer); file_close (file); diff --git a/src/lib/kernel/bitmap.c b/src/lib/kernel/bitmap.c index 3690a3a..f1fa113 100644 --- a/src/lib/kernel/bitmap.c +++ b/src/lib/kernel/bitmap.c @@ -323,5 +323,5 @@ bitmap_write (const struct bitmap *b, struct file *file) void bitmap_dump (const struct bitmap *b) { - hex_dump (b->bits, byte_cnt (b), false); + hex_dump (0, b->bits, byte_cnt (b), false); } diff --git a/src/lib/stdio.c b/src/lib/stdio.c index d223d10..034374d 100644 --- a/src/lib/stdio.c +++ b/src/lib/stdio.c @@ -1,5 +1,7 @@ -#include #include +#include +#include +#include #include #include @@ -553,39 +555,53 @@ __printf (const char *format, va_end (args); } -/* Dumps the SIZE bytes in BUFFER to the console as hex bytes - arranged 16 per line. If ASCII is true then the corresponding +/* Dumps the SIZE bytes in BUF to the console as hex bytes + arranged 16 per line, plus offsets listed starting at OFS for + the first byte in BU. If ASCII is true then the corresponding ASCII characters are also rendered alongside. */ void -hex_dump (const void *buffer, size_t size, bool ascii) +hex_dump (uintptr_t ofs, const void *buf_, size_t size, bool ascii) { - const size_t n_per_line = 16; /* Maximum bytes per line. */ - size_t n; /* Number of bytes in this line. */ - const uint8_t *p; /* Start of current line in buffer. */ + const uint8_t *buf = buf_; + const size_t per_line = 16; /* Maximum bytes per line. */ - for (p = buffer; p < (uint8_t *) buffer + size; p += n) + while (size > 0) { + size_t start, end, n; size_t i; - + /* Number of bytes on this line. */ - n = (uint8_t *) (buffer + size) - p; - if (n > n_per_line) - n = n_per_line; + start = ofs % per_line; + end = per_line; + if (end - start > size) + end = start + size; + n = end - start; /* Print line. */ - for (i = 0; i < n; i++) - printf ("%c%02x", i == n_per_line / 2 ? '-' : ' ', (unsigned) p[i]); + printf ("%08jx ", (uintmax_t) ROUND_DOWN (ofs, per_line)); + for (i = 0; i < start; i++) + printf (" "); + for (; i < end; i++) + printf ("%02hhx%c", + buf[i - start], i == per_line / 2 - 1? '-' : ' '); if (ascii) { - for (; i < n_per_line; i++) + for (; i < per_line; i++) printf (" "); - printf (" |"); - for (i = 0; i < n; i++) - printf ("%c", isprint (p[i]) ? p[i] : '.'); - for (; i < n_per_line; i++) + printf ("|"); + for (i = 0; i < start; i++) + printf (" "); + for (; i < end; i++) + printf ("%c", + isprint (buf[i - start]) ? buf[i - start] : '.'); + for (; i < per_line; i++) printf (" "); printf ("|"); } printf ("\n"); + + ofs += n; + buf += n; + size -= n; } } diff --git a/src/lib/stdio.h b/src/lib/stdio.h index cf6c49e..31be6d8 100644 --- a/src/lib/stdio.h +++ b/src/lib/stdio.h @@ -2,9 +2,10 @@ #define __LIB_STDIO_H #include +#include #include #include -#include +#include /* Predefined file handles. */ #define STDIN_FILENO 0 @@ -26,7 +27,7 @@ void putbuf (const char *, size_t); int hprintf (int, const char *, ...) PRINTF_FORMAT (2, 3); int vhprintf (int, const char *, va_list) PRINTF_FORMAT (2, 0); #endif -void hex_dump (const void *, size_t size, bool ascii); +void hex_dump (uintptr_t ofs, const void *, size_t size, bool ascii); /* Internal functions. */ void __vprintf (const char *format, va_list args, -- 2.30.2