From d4c39fd300e91824076615951c20bfd5c8aa4c9e Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Wed, 22 Jul 2026 07:06:30 +0200 Subject: [PATCH] Simplify hexdump_buf() --- src/term.c | 67 ++++++++++++++---------------------------------------- 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/src/term.c b/src/term.c index 73e0f8d2..9cca35a2 100644 --- a/src/term.c +++ b/src/term.c @@ -135,56 +135,24 @@ static const struct command cmd[] = { #define spi_mode (cx->term_spi_mode) -static int hexdump_line(char *buffer, unsigned char *p, int n, int pad) { - char *hexdata = "0123456789abcdef"; - char *b = buffer; - int i = 0; - int j = 0; +// Dump n bytes (max 16) as hex line with adjacent character field into buffer of size 69 +static void hexdump16(char *buf, const unsigned char *p, int n) { + const char *hexdata = "0123456789abcdef"; - for(i = 0; i < n; i++) { - if(i && ((i%8) == 0)) - b[j++] = ' '; - b[j++] = hexdata[(p[i] & 0xf0) >> 4]; - b[j++] = hexdata[(p[i] & 0x0f)]; - if(i < 15) - b[j++] = ' '; + memset(buf, ' ', 67); + buf[50] = buf[67] = '|'; + buf[68] = 0; + + for(int j = 0, i = 0; i < n && i < 16; i++, j += i==8? 2: 1) { + unsigned char c = p[i]; + buf[j++] = hexdata[(c & 0xf0) >> 4]; + buf[j++] = hexdata[c & 0x0f]; + buf[51 + i] = isascii(c) && isspace(c)? ' ': isascii(c) && isgraph(c)? c: '.'; } - - for(i = j; i < pad; i++) - b[i] = ' '; - - b[i] = 0; - - for(i = 0; i < pad; i++) { - if(!((b[i] == '0') || (b[i] == ' '))) - return 0; - } - - return 1; } -static int chardump_line(char *buffer, unsigned char *p, int n, int pad) { - int i; - unsigned char b[128]; - - // Sanity check - n = n < 1? 1: n > (int) sizeof b? (int) sizeof b: n; - - memcpy(b, p, n); - for(int i = 0; i < n; i++) - buffer[i] = isascii(b[i]) && isspace(b[i])? ' ': isascii(b[i]) && isgraph(b[i])? b[i]: '.'; - - for(i = n; i < pad; i++) - buffer[i] = ' '; - - buffer[i] = 0; - - return 0; -} - -static int hexdump_buf(const FILE *f, const AVRMEM *m, int startaddr, const unsigned char *buf, int len) { - char dst1[80]; - char dst2[80]; +static int hexdump_buf(const AVRMEM *m, int startaddr, const unsigned char *buf, int len) { + char dst[80]; int addr = startaddr; unsigned char *p = (unsigned char *) buf; @@ -197,9 +165,8 @@ static int hexdump_buf(const FILE *f, const AVRMEM *m, int startaddr, const unsi if(addr + n > m->size) n = m->size - addr; - hexdump_line(dst1, p, n, 48); - chardump_line(dst2, p, n, 16); - term_out("%0*x %s |%s|\n", m->size > 0x10000? 5: 4, addr, dst1, dst2); + hexdump16(dst, p, n); + term_out("%0*x %s\n", m->size > 0x10000? 5: 4, addr, dst); len -= n; addr += n; if(addr >= m->size) @@ -499,7 +466,7 @@ static int cmd_dump(const PROGRAMMER *pgm, const AVRPART *p, int argc, const cha if(!buf) return -1; - hexdump_buf(stdout, mem, addr, buf, len); + hexdump_buf(mem, addr, buf, len); lterm_out(""); mmt_free(buf);