-
Notifications
You must be signed in to change notification settings - Fork 4
/
hexdump.c
166 lines (125 loc) · 3.27 KB
/
hexdump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "hexdump.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int hexdump_init(struct hexdump *hd, const char *from, const char *to,
const char *color_escape, const char *out_filename) {
memset(hd, 0, sizeof(*hd));
hd->from = from;
hd->to = to;
hd->color_escape = color_escape;
if (!out_filename)
return 0;
hd->out_file = fopen(out_filename, "wt");
if (!hd->out_file)
return -1;
return 0;
}
void hexdump_destroy(struct hexdump *hd) {
if (hd->out_file)
fclose(hd->out_file);
}
size_t print_half_hex(struct hexdump *hd, size_t begin, int half,
const char *buf, unsigned int sz) {
size_t offset = half==0? begin : begin + 8;
size_t end = offset + 8;
size_t i = 0;
for (; offset < end && i < sz; ++offset) {
if (offset < hd->offset) {
printf(" ");
}
else {
printf("%02hhx ", buf[i]);
++i;
}
}
for (; offset < end; ++offset)
printf(" ");
return i;
}
size_t print_ascii(struct hexdump *hd, size_t begin,
const char *buf, unsigned int sz) {
size_t offset = begin;
size_t end = begin + 16;
size_t i = 0;
for (; offset < end && i < sz; ++offset) {
if (offset < hd->offset) {
printf(" ");
}
else {
char c = isprint(buf[i])? buf[i] : '.';
printf("%c", c);
++i;
}
}
for (; offset < end; ++offset)
printf(" ");
return i;
}
size_t print_full_hex(struct hexdump *hd, size_t start_line_offset,
const char *buf, unsigned int sz) {
size_t consumed = print_half_hex(hd, start_line_offset, 0, buf, sz);
buf += consumed;
sz -= consumed;
printf(" ");
consumed += print_half_hex(hd, start_line_offset, 1, buf, sz);
return consumed;
}
void hexdump_print_raw_hex(struct hexdump *hd, const char *buf, unsigned int sz) {
if (hd->out_file) {
for (size_t i = 0; i < sz; ++i) {
fprintf(hd->out_file, "%02hhx", buf[i]);
if ((hd->offset + i) % 16 == 15)
fprintf(hd->out_file, "\n");
}
fflush(hd->out_file);
}
}
void hexdump_sent_print(struct hexdump *hd, const char *buf, unsigned int sz) {
if (!sz)
return;
if (hd->color_escape)
printf("%s", hd->color_escape);
printf("%s -> %s sent %u bytes\n", hd->from, hd->to, sz);
if (hd->color_escape)
printf("%s", "\x1b[1m"); /* bold */
hexdump_print_raw_hex(hd, buf, sz);
while (sz > 0) {
printf("%08x ", hd->offset);
size_t start_line_offset = (hd->offset >> 4) << 4;
print_full_hex(hd, start_line_offset, buf, sz);
printf(" |");
size_t consumed = print_ascii(hd, start_line_offset, buf, sz);
printf("|\n");
buf += consumed;
sz -= consumed;
hd->offset += consumed;
}
if (hd->color_escape)
printf("%s", "\x1b[0m"); /* reset */
fflush(stdout);
}
void hexdump_remain_print(struct hexdump *hd, unsigned int sz_consumed) {
if (hd->color_escape)
printf("%s", hd->color_escape);
hd->offset_consumer += sz_consumed;
if (hd->offset_consumer >= hd->offset) {
printf("%s is in sync\n", hd->to);
}
else {
printf("%s is %i bytes behind\n",
hd->to, hd->offset - hd->offset_consumer);
}
if (hd->color_escape)
printf("%s", "\x1b[0m"); /* reset */
fflush(stdout);
}
void hexdump_shutdown_print(struct hexdump *hd) {
if (hd->color_escape)
printf("%s", hd->color_escape);
printf("%s -> %s flow shutdown\n", hd->from, hd->to);
if (hd->color_escape)
printf("%s", "\x1b[0m"); /* reset */
fflush(stdout);
}