Commit | Line | Data |
---|---|---|
a56bdb9d A |
1 | #include <stdio.h> |
2 | #include <ctype.h> | |
3 | #include <unistd.h> | |
4 | ||
5 | #define MIN(a, b) \ | |
6 | ({ __typeof(a) _a = (a); __typeof(b) _b = (b); \ | |
7 | (_a < _b) ? _a : _b; }) | |
8 | ||
9 | enum { WIDTH = 16, }; | |
10 | ||
11 | /* | |
12 | * Debug functions only. | |
13 | */ | |
14 | void | |
15 | DumpData(const void *data, size_t len) | |
16 | { | |
17 | unsigned char *base = (unsigned char*)data; | |
18 | unsigned char *end = base + len; | |
19 | unsigned char *cp = base; | |
20 | int allzeroes = 0; | |
21 | ||
22 | while (cp < end) { | |
23 | unsigned char *tend = MIN(end, cp + WIDTH); | |
24 | unsigned char *tmp; | |
25 | int i; | |
26 | size_t gap = (cp + WIDTH) - tend; | |
27 | ||
28 | if (gap != 0 || tend == end) | |
29 | allzeroes = 0; | |
30 | if (allzeroes) { | |
31 | for (tmp = cp; tmp < tend; tmp++) { | |
32 | if (*tmp) { | |
33 | allzeroes = 0; | |
34 | break; | |
35 | } | |
36 | } | |
37 | if (allzeroes == 1) { | |
38 | printf(". . .\n"); | |
39 | allzeroes = 2; | |
40 | } | |
41 | if (allzeroes) { | |
42 | cp += WIDTH; | |
43 | continue; | |
44 | } | |
45 | } | |
46 | allzeroes = 1; | |
47 | ||
48 | printf("%04x: ", (int)(cp - base)); | |
49 | for (i = 0, tmp = cp; tmp < tend; tmp++) { | |
50 | printf("%02x", *tmp); | |
51 | if (++i % 2 == 0) | |
52 | printf(" "); | |
53 | if (*tmp) | |
54 | allzeroes = 0; | |
55 | } | |
56 | for (i = gap; i >= 0; i--) { | |
57 | printf(" "); | |
58 | if (i % 2 == 1) | |
59 | printf(" "); | |
60 | } | |
61 | printf(" |"); | |
62 | for (tmp = cp; tmp < tend; tmp++) { | |
63 | printf("%c", isalnum(*tmp) ? *tmp : '.'); | |
64 | } | |
65 | for (i = 0; i < gap; i++) { | |
66 | printf(" "); | |
67 | } | |
68 | printf("|\n"); | |
69 | cp += WIDTH; | |
70 | } | |
71 | ||
72 | return; | |
73 | ||
74 | } |