-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexcat.c
156 lines (130 loc) · 3.03 KB
/
hexcat.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
/* SPDX-License-Identifier: MIT */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__unix__) || defined(UNIX) || defined(__linux)
# include <unistd.h>
#endif
#if defined(WIN32) || defined(CYGWIN)
# include <io.h>
#endif
#include "hexcat.h"
#define VERSION 0.1
#define BUFSIZE 16
char
colorize(int ch)
{
/*
* Since space characters are printable, check for them before checking for
* other printable characters.
*/
if (isspace(ch))
return 3;
if (ch == 0)
return 7;
if (ch == 255)
return 4;
return isprint(ch) ? 2 : 1;
}
void
usage()
{
fputs("usage: hexcat [-hvncxu] [-o FILE] [FILE]\n", stderr);
exit(0);
}
int
main(int argc, char *argv[])
{
char *arg = NULL;
FILE *fp = stdin, *ofp = stdout;
enum Colorize usecolor = ON;
int printascii = 1;
int uppercase = 0;
/* Disable colorized output by default for non-TTYs. */
if (!isatty(STDOUT_FILENO)) usecolor = OFF;
while (argc >= 2) {
arg = argv[1] + (!strncmp(argv[1], "--", 2) && argv[1][2]);
if (!strncmp(arg, "-h", 2)) usage();
else if (!strncmp(arg, "-n", 2)) usecolor = OFF;
else if (!strncmp(arg, "-c", 2)) usecolor = FORCE;
else if (!strncmp(arg, "-x", 2)) printascii = 0;
else if (!strncmp(arg, "-u", 2)) uppercase = 1;
else if (!strncmp(arg, "-v", 2)) {
fprintf(stderr, "hexcat v%.1f\n", VERSION);
return 0;
} else if (!strncmp(arg, "-o", 2)) {
argv++;
argc--;
ofp = fopen(argv[1], "w");
if (ofp == NULL) {
fprintf(stderr, "hexcat: ");
perror(argv[1]);
return 1;
}
if (usecolor != FORCE) {
usecolor = OFF;
}
} else break;
argv++;
argc--;
}
if (argc > 1) {
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "hexcat: ");
perror(argv[1]);
return 1;
}
}
unsigned char buf[BUFSIZE];
unsigned long size = 0;
size_t bytes_read;
while ((bytes_read = fread(buf, sizeof(char), BUFSIZE, fp)) > 0) {
/* Print an 8-character length, 0-padded long int in hexadecimal form. */
if (uppercase)
fprintf(ofp, "%08lX: ", size);
else
fprintf(ofp, "%08lx: ", size);
for (int i = 0; i < bytes_read; i++) {
int c = buf[i];
if (usecolor) {
if (uppercase)
fprintf(ofp, "\033[1;3%dm%02X\033[0m ", colorize(c), c);
else
fprintf(ofp, "\033[1;3%dm%02x\033[0m ", colorize(c), c);
} else {
if (uppercase)
fprintf(ofp, "%02X ", buf[i]);
else
fprintf(ofp, "%02x ", buf[i]);
}
if (i == 7)
fprintf(ofp, " ");
}
if (bytes_read < 8) fprintf(ofp, " ");
while (bytes_read < BUFSIZE) {
fprintf(ofp, " ");
buf[bytes_read++] = ' ';
}
if (printascii) {
fprintf(ofp, " | ");
for (int i = 0; i < BUFSIZE; i++) {
int c = buf[i];
if (usecolor)
fprintf(ofp, "\033[1;3%dm%c\033[0m", colorize(c), isprint(c) ? c : '.');
else
fprintf(ofp, "%c", isprint(c) ? c : '.');
}
}
fputc('\n', ofp);
size += BUFSIZE;
if (bytes_read < 0)
perror("Read error.");
/* Reset all values in the buffer. */
for (int i = 0; i < BUFSIZE; i++)
buf[i] = '\0';
}
fclose(fp);
fclose(ofp);
}