Skip to content

Commit 90993c2

Browse files
committed
Add hexdump() debug helper function
Signed-off-by: Joachim Wiberg <[email protected]>
1 parent b0b4f7b commit 90993c2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/util.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#define FINIT_UTIL_H_
2626

2727
#include "config.h"
28+
29+
#include <ctype.h>
2830
#ifdef HAVE_TERMIOS_H
2931
#include <poll.h>
3032
#include <stdio.h>
@@ -130,6 +132,39 @@ static inline int suffix(char *path, size_t len, const char *sfx)
130132
return 0;
131133
}
132134

135+
static inline void hexdump(const char *buf, size_t len)
136+
{
137+
size_t i, j;
138+
139+
for (i = 0; i < len; i += 16) {
140+
printf("%08zx ", i);
141+
142+
for (j = 0; j < 8 && i + j < len; j++)
143+
printf("%02x ", buf[i + j]);
144+
145+
if (j == 8) {
146+
printf(" ");
147+
} else {
148+
for (; j < 8; j++)
149+
printf(" ");
150+
printf(" ");
151+
}
152+
153+
for (j = 8; j < 16 && i + j < len; j++)
154+
printf("%02x ", buf[i + j]);
155+
156+
for (; j < 16; j++)
157+
printf(" ");
158+
159+
printf(" |");
160+
for (j = 0; j < 16 && i + j < len; j++) {
161+
char c = buf[i + j];
162+
printf("%c", isprint(c) ? c : '.');
163+
}
164+
printf("|\n");
165+
}
166+
}
167+
133168
#endif /* FINIT_UTIL_H_ */
134169

135170
/**

0 commit comments

Comments
 (0)