-
Notifications
You must be signed in to change notification settings - Fork 14
/
hastyhex.c
392 lines (361 loc) · 12.6 KB
/
hastyhex.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define VERSION "1.0.0"
static const char hex[16] = "0123456789abcdef";
/* Same as isalnum(3), but without locale. */
static int
xisalnum(int c)
{
return (c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z');
}
/* Same as strchr(3). */
static const char *
xstrchr(const char *s, int c)
{
for (c = (char)c;; s++) {
if (*s == c) {
return s;
} else if (!*s) {
return 0;
}
}
}
static int
color(int b)
{
#define CN 0x37 /* null */
#define CS 0x92 /* space */
#define CP 0x96 /* print */
#define CC 0x95 /* control */
#define CH 0x93 /* high */
static const unsigned char table[] = {
CN, CC, CC, CC, CC, CC, CC, CC, CC, CC, CS, CS, CS, CS, CC, CC,
CC, CC, CC, CC, CC, CC, CC, CC, CC, CC, CC, CC, CC, CC, CC, CC,
CS, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP,
CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP,
CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP,
CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP,
CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP,
CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP, CC,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH,
CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH, CH
};
return table[b];
}
static int
display(int b)
{
static const char table[] = {
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x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0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
0x2e, 0x2e, 0x2e,
};
return table[b];
}
static void
process_color(FILE *in, FILE *out)
{
int i, n;
unsigned long offset = 0;
unsigned char input[16] = {0};
char template[] =
"00000000 "
"\33[XXm## \33[XXm## \33[XXm## \33[XXm## "
"\33[XXm## \33[XXm## \33[XXm## \33[XXm## "
"\33[XXm## \33[XXm## \33[XXm## \33[XXm## "
"\33[XXm## \33[XXm## \33[XXm## \33[XXm## "
"\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm."
"\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm.\33[XXm."
"\33[0m\n";
static const int slots[] = {
/* ANSI-color, hex, ANSI-color, ASCII */
12, 15, 142, 145,
20, 23, 148, 151,
28, 31, 154, 157,
36, 39, 160, 163,
44, 47, 166, 169,
52, 55, 172, 175,
60, 63, 178, 181,
68, 71, 184, 187,
77, 80, 190, 193,
85, 88, 196, 199,
93, 96, 202, 205,
101, 104, 208, 211,
109, 112, 214, 217,
117, 120, 220, 223,
125, 128, 226, 229,
133, 136, 232, 235
};
do {
n = fread(input, 1, 16, in);
/* Write the offset */
for (i = 0; i < 8; i++)
template[i] = hex[(offset >> (28 - i * 4)) & 15];
/* Fill out the template */
for (i = 0; i < 16; i++) {
/* Use a fixed loop count instead of "n" to encourage loop
* unrolling by the compiler. Empty bytes will be erased
* later.
*/
int v = input[i];
int c = color(v);
template[slots[i * 4 + 0] + 0] = hex[c >> 4];
template[slots[i * 4 + 0] + 1] = hex[c & 15];
template[slots[i * 4 + 1] + 0] = hex[v >> 4];
template[slots[i * 4 + 1] + 1] = hex[v & 15];
template[slots[i * 4 + 2] + 0] = hex[c >> 4];
template[slots[i * 4 + 2] + 1] = hex[c & 15];
template[slots[i * 4 + 3] + 0] = display(v);
}
/* Erase any trailing bytes */
for (i = n; i < 16; i++) {
/* This loop is only used once: the last line of output. The
* branch predictor will quickly learn that it's never taken.
*/
template[slots[i * 4 + 0] + 0] = '0';
template[slots[i * 4 + 0] + 1] = '0';
template[slots[i * 4 + 1] + 0] = ' ';
template[slots[i * 4 + 1] + 1] = ' ';
template[slots[i * 4 + 2] + 0] = '0';
template[slots[i * 4 + 2] + 1] = '0';
template[slots[i * 4 + 3] + 0] = ' ';
}
if (!fwrite(template, sizeof(template) - 1, 1, out))
break; /* Output error */
offset += 16;
} while (n == 16);
}
static void
process_plain(FILE *in, FILE *out)
{
int i, n;
unsigned long offset = 0;
unsigned char input[16] = {0};
char template[] =
"00000000 ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## "
"................\n";
static const int slots[] = {
10, 60, 13, 61, 16, 62, 19, 63, 22, 64, 25, 65, 28, 66, 31, 67,
35, 68, 38, 69, 41, 70, 44, 71, 47, 72, 50, 73, 53, 74, 56, 75
};
do {
n = fread(input, 1, 16, in);
/* Write the offset */
for (i = 0; i < 8; i++)
template[i] = hex[(offset >> (28 - i * 4)) & 15];
/* Fill out the template */
for (i = 0; i < 16; i++) {
int v = input[i];
template[slots[i * 2 + 0] + 0] = hex[v >> 4];
template[slots[i * 2 + 0] + 1] = hex[v & 15];
template[slots[i * 2 + 1] + 0] = display(v);
}
/* Erase any trailing bytes */
for (i = n; i < 16; i++) {
template[slots[i * 2 + 0] + 0] = ' ';
template[slots[i * 2 + 0] + 1] = ' ';
template[slots[i * 2 + 1] + 0] = ' ';
}
if (!fwrite(template, sizeof(template) - 1, 1, out))
break;
offset += 16;
} while (n == 16);
}
static int xoptind = 1;
static int xoptopt;
static char *xoptarg;
/* Same as getopt(3) but never prints errors. */
static int
xgetopt(int argc, char * const argv[], const char *optstring)
{
static int optpos = 1;
const char *arg;
arg = xoptind < argc ? argv[xoptind] : 0;
if (arg && arg[0] == '-' && arg[1] == '-' && !arg[2]) {
xoptind++;
return -1;
} else if (!arg || arg[0] != '-' || !xisalnum(arg[1])) {
return -1;
} else {
const char *opt = xstrchr(optstring, arg[optpos]);
xoptopt = arg[optpos];
if (!opt) {
return '?';
} else if (opt[1] == ':') {
if (arg[optpos + 1]) {
xoptarg = (char *)arg + optpos + 1;
xoptind++;
optpos = 1;
return xoptopt;
} else if (argv[xoptind + 1]) {
xoptarg = (char *)argv[xoptind + 1];
xoptind += 2;
optpos = 1;
return xoptopt;
} else {
return ':';
}
} else {
if (!arg[++optpos]) {
xoptind++;
optpos = 1;
}
return xoptopt;
}
}
}
static int
usage(FILE *f)
{
static const char usage[] =
"usage: hastyhex [-fhlp] [-o FILE]\n"
" -f force output fully-buffered\n"
" -h print this help message\n"
" -l force output line-buffered\n"
" -o FILE output to file instead of standard output\n"
" -p do not output color (\"plain\")\n"
" -V print version information\n";
return fwrite(usage, sizeof(usage)-1, 1, f) && !fflush(f);
}
static int
version(void)
{
static const char version[] = "hastyhex " VERSION "\n";
return fwrite(version, sizeof(version)-1, 1, stdout) && !fflush(stdout);
}
static const char *
run(int argc, char **argv)
{
int option;
FILE *in = stdin;
FILE *out = stdout;
const char *outfile = 0;
enum {MODE_COLOR, MODE_PLAIN} mode = MODE_COLOR;
enum {BUF_AUTO, BUF_LINE, BUF_FULL} buf_mode = BUF_AUTO;
static char missing[] = "missing argument: -?";
static char illegal[] = "illegal option: -?";
#ifdef _WIN32
{ /* Set stdin/stdout to binary mode. */
int _setmode(int, int);
_setmode(0, 0x8000);
_setmode(1, 0x8000);
}
{ /* Best effort enable ANSI escape processing. */
__declspec(dllimport) void *__stdcall GetStdHandle(unsigned);
__declspec(dllimport) int __stdcall GetConsoleMode(void *, unsigned *);
__declspec(dllimport) int __stdcall SetConsoleMode(void *, unsigned);
void *handle;
unsigned mode;
handle = GetStdHandle(-11); /* STD_OUTPUT_HANDLE */
if (GetConsoleMode(handle, &mode)) {
mode |= 0x0004; /* ENABLE_VIRTUAL_TERMINAL_PROCESSING */
SetConsoleMode(handle, mode); /* ignore errors */
}
}
#endif
while ((option = xgetopt(argc, argv, ":fhlo:pV")) != -1) {
switch (option) {
case 'f': buf_mode = BUF_FULL;
break;
case 'h': return usage(stdout) ? 0 : "write error";
case 'l': buf_mode = BUF_LINE;
break;
case 'o': outfile = xoptarg;
break;
case 'p': mode = MODE_PLAIN;
break;
case 'V': return version() ? 0 : "write error";
case ':': missing[sizeof(missing)-2] = xoptopt;
usage(stderr);
return missing;
case '?': illegal[sizeof(illegal)-2] = xoptopt;
usage(stderr);
return illegal;
}
}
/* Configure input */
if (argv[xoptind]) {
char *path = argv[xoptind];
if (argv[xoptind+1]) {
return "too many arguments";
}
if (path[0] != '-' || path[1] != 0) {
in = fopen(path, "rb");
if (!in) {
return path;
}
}
}
/* Configure output */
if (outfile) {
out = fopen(outfile, "wb");
if (!out) {
return outfile;
}
}
switch (buf_mode) {
static char buf[1L << 18];
case BUF_AUTO: break;
case BUF_LINE: setvbuf(out, buf, _IOLBF, sizeof(buf)); break;
case BUF_FULL: setvbuf(out, buf, _IOFBF, sizeof(buf)); break;
}
/* Process input using user-selected mode */
switch (mode) {
case MODE_COLOR: process_color(in, out); break;
case MODE_PLAIN: process_plain(in, out); break;
}
/* Check for errors before exiting */
if (ferror(in)) {
return "input error";
}
if (fflush(out) || ferror(out)) {
return "output error";
}
return 0;
}
int
main(int argc, char **argv)
{
const char *err = run(argc, argv);
if (err) {
fputs("hastyhex: ", stderr);
if (errno) {
fputs(strerror(errno), stderr);
fputs(": ", stderr);
}
fputs(err, stderr);
fputs("\n", stderr);
return 1;
}
return 0;
}