# HG changeset patch # User Matt Johnston # Date 1603723904 -28800 # Node ID a339b1c4b9f2608e532d382ecb0d975296fed48f # Parent 2406a9987810b1ecf455425bf5900aac06585483 Print ascii in printhex too diff -r 2406a9987810 -r a339b1c4b9f2 dbutil.c --- a/dbutil.c Sun Oct 25 22:52:36 2020 +0800 +++ b/dbutil.c Mon Oct 26 22:51:44 2020 +0800 @@ -385,20 +385,37 @@ #if DEBUG_TRACE void printhex(const char * label, const unsigned char * buf, int len) { - - int i; + int i, j; fprintf(stderr, "%s\n", label); - for (i = 0; i < len; i++) { - fprintf(stderr, "%02x", buf[i]); - if (i % 16 == 15) { - fprintf(stderr, "\n"); + /* for each 16 byte line */ + for (j = 0; j < len; j += 16) { + const int linelen = MIN(16, len - j); + + /* print hex digits */ + for (i = 0; i < 16; i++) { + if (i < linelen) { + fprintf(stderr, "%02x", buf[j+i]); + } else { + fprintf(stderr, " "); + } + // separator between pairs + if (i % 2 ==1) { + fprintf(stderr, " "); + } } - else if (i % 2 == 1) { - fprintf(stderr, " "); + + /* print characters */ + fprintf(stderr, " "); + for (i = 0; i < linelen; i++) { + char c = buf[j+i]; + if (!isprint(c)) { + c = '.'; + } + fputc(c, stderr); } + fprintf(stderr, "\n"); } - fprintf(stderr, "\n"); } void printmpint(const char *label, mp_int *mp) {