From fd133e1570308d6121181c729fbaeaf45267eb63 Mon Sep 17 00:00:00 2001 From: Dirk Farin Date: Tue, 30 Jul 2024 12:53:47 +0200 Subject: [PATCH] changes requested in review (#72) --- display.c | 2 +- file.c | 33 ++++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/display.c b/display.c index a2a5675..06a2b31 100644 --- a/display.c +++ b/display.c @@ -164,7 +164,7 @@ void exitCurses(void) static void printaddr(uint64_t addr) { - char templ[7]; + char templ[7]; // maximum string is "%016lX", which is 6 chars + 1 null byte sprintf(templ,"%%0%dlX", nAddrDigits); PRINTW((templ, addr)); } diff --git a/file.c b/file.c index d53288b..3337f15 100644 --- a/file.c +++ b/file.c @@ -16,6 +16,26 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/ #include "hexedit.h" + +// Compute number of hex-digits needed to display an address. +// We only increase this by full bytes (2 digits). +// Because the input is uint64_t, the maximum result is 16 (64bit = 16*4bit). +int compute_nDigits(uint64_t maxAddr) +{ + int digits = 0; + while (maxAddr) { + digits+=2; + maxAddr >>= 8; + } + + if (digits==0) { + return 2; + } + + return digits; +} + + void openFile(void) { struct stat st; @@ -54,16 +74,11 @@ void openFile(void) } biggestLoc = fileSize; - // compute number of digits for address + nAddrDigits = compute_nDigits(biggestLoc); - nAddrDigits=8; - off_t maxAddr = biggestLoc; - if (maxAddr > 0xFFFFFFFF) { - maxAddr >>= 32; - while (maxAddr) { - nAddrDigits+=2; - maxAddr >>= 8; - } + // use at least 8 digits (4 byte addresses) + if (nAddrDigits < 8) { + nAddrDigits = 8; } }