-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhd.c
95 lines (83 loc) · 1.77 KB
/
hd.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
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "glyphs.h"
int dump(FILE *inf, bool verbose) {
uint64_t p = 0;
uint8_t buf[32];
int offset = 0;
int skipping = 0;
while (!feof(inf)) {
uint8_t *bytes = buf + offset;
size_t len;
int i;
offset = 16 - offset;
len = fread(bytes, 1, 16, inf);
if (0 == len)
break;
if (!verbose && p && (len == 16) && (0 == memcmp(buf, buf + 16, 16))) {
if (!skipping) {
printf("⋮\n");
skipping = 1;
}
p += 16;
continue;
} else {
skipping = 0;
}
printf("%08lx ", (long unsigned int)p);
for (i = 0; i < 16; i += 1) {
if (i < len) {
printf("%02x ", bytes[i]);
} else {
printf(" ");
}
if (7 == i) {
printf(" ");
}
}
printf(" ");
for (i = 0; i < len; i += 1) {
printf("%s", fluffyglyphs[bytes[i]]);
}
if (-1 == printf("\n")) {
perror("printf");
return 1;
}
p += len;
}
printf("%08lx\n", (long unsigned int)p);
return 0;
}
int main(int argc, char *argv[]) {
FILE *f;
bool verbose = false;
int c;
while ((c = getopt(argc, argv, "v")) != -1) {
switch (c) {
case -1:
break;
case 'v':
verbose = true;
break;
default:
fprintf(stderr, "Usage: %s [-v] [FILENAME]\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "-v Verbose: don't elide output if output lines are identical\n");
return 1;
}
}
if (!argv[optind] || (0 == strcmp("-", argv[optind]))) {
f = stdin;
} else {
f = fopen(argv[optind], "rb");
if (!f) {
perror("open");
return 1;
}
}
dump(f, verbose);
return 0;
}