1: /*
2: * hex2bin - simple hex to bin filter
3: * antirez@invece.org - under GPL version 2
4: */
5:
6: #include <stdio.h>
7: #include <sys/stat.h>
8: #include <sys/types.h>
9: #include <fcntl.h>
10: #include <unistd.h>
11: #include <string.h>
12:
13: int hex2bin(void)
14: {
15: char hex[3];
16: int d = 0;
17: unsigned char c;
18: int stdin_fd = fileno(stdin);
19: int n_read;
20:
21: while((n_read = read(stdin_fd, hex, 2)) > 0)
22: {
23: if (n_read == 1)
24: {
25: if (hex[0] != '\n')
26: {
27: fprintf(stderr,
28: "input parse error, odd digits in hex file\n");
29: exit(1);
30: }
31: else
32: exit(1);
33: }
34: hex[2] = '\0';
35: sscanf(hex, "%x", &d);
36: c = (unsigned char) d;
37: printf("%c", c);
38: }
39: return 0;
40: }
41:
42: int bin2hex(void)
43: {
44: int stdin_fd = fileno(stdin);
45: int n_read;
46: unsigned char c;
47:
48: while((n_read = read(stdin_fd, &c, 1)) > 0)
49: {
50: printf("%.2x", c);
51: }
52: return 0;
53: }
54:
55: int main(int argc, char **argv)
56: {
57: if (argc >= 2 && strstr(argv[1], "-r"))
58: bin2hex();
59: else
60: hex2bin();
61:
62: return 0;
63: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>