summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2023-12-27 15:17:48 +0100
committerpgen <p.gen.progs@gmail.com>2023-12-27 15:17:48 +0100
commitbb98205f7f13e838e7458de223931e4b1480ab8d (patch)
treed43798d96909b5ed0ca549d9af517ccb25f107b4
parentbd5d9b44af511bc12dbe03b651c9d5a622a2f6fd (diff)
Add a hexdump function in utils.c for debugging
-rw-r--r--utils.c54
-rw-r--r--utils.h4
2 files changed, 58 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index d74ecdb..a5ad569 100644
--- a/utils.c
+++ b/utils.c
@@ -404,3 +404,57 @@ strprint(char const *s)
return new;
}
+
+void
+hexdump(const char *buf, FILE *fp, const char *prefix, size_t size)
+{
+ unsigned int b;
+ unsigned char d[17];
+ unsigned int o, mo;
+ size_t l;
+
+ o = mo = 0;
+ l = strlen(prefix);
+
+ memset(d, '\0', 17);
+ for (b = 0; b < size; b++)
+ {
+
+ d[b % 16] = isprint(buf[b]) ? (unsigned char)buf[b] : '.';
+
+ if ((b % 16) == 0)
+ {
+ o = l + 7;
+ if (o > mo)
+ mo = o;
+ fprintf(fp, "%s: %04x:", prefix, b);
+ }
+
+ o += 3;
+ if (o > mo)
+ mo = o;
+ fprintf(fp, " %02x", (unsigned char)buf[b]);
+
+ if ((b % 16) == 15)
+ {
+ mo = o;
+ o = 0;
+ fprintf(fp, " |%s|", d);
+ memset(d, '\0', 17);
+ fprintf(fp, "\n");
+ }
+ }
+ if ((b % 16) != 0)
+ {
+ for (unsigned int i = 0; i < mo - o; i++)
+ fprintf(fp, "%c", ' ');
+
+ fprintf(fp, " |%s", d);
+ if (mo > o)
+ for (unsigned int i = 0; i < 16 - strlen(d); i++)
+ fprintf(fp, "%c", ' ');
+ fprintf(fp, "%c", '|');
+ memset(d, '\0', 17);
+ fprintf(fp, "\n");
+ }
+}
diff --git a/utils.h b/utils.h
index 003a527..775bad1 100644
--- a/utils.h
+++ b/utils.h
@@ -79,4 +79,8 @@ strrep(char *s, const char c1, const char c2);
char *
strprint(char const *s);
+
+void
+hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
+
#endif