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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
void print_binary_hex(uint8_t *bin, size_t siz)
{
size_t i;
for (i = 0; i < siz; ++i) {
printf("%8.02X ", bin[i]);
}
printf("\n");
for (i = 0; i < siz; ++i) {
printf("%8.d ", bin[i]);
}
printf("\n");
for (i = 0; i < siz; ++i) {
printf(BYTE_TO_BINARY_PATTERN " ", BYTE_TO_BINARY(bin[i]));
}
printf("\n");
}
ssize_t textify(uint8_t *bin, size_t siz, char *out, size_t outsiz)
{
size_t i, j;
if (!bin || !out)
return -1;
for (i = 0, j = 0; i < siz && j < outsiz / 3; ++i, j += 3) {
uint8_t tmp0 = 32, tmp1 = 64, tmp2 = 64;
if (bin[i] < 32)
tmp1 = bin[i] + 32;
else if (bin[i] >= 127) {
if (bin[i] < 190)
tmp1 = bin[i] - 63;
else
tmp2 = bin[i] - 63*2;
} else {
tmp0 = bin[i];
}
out[j+0] = tmp0;
out[j+1] = tmp1;
out[j+2] = tmp2;
}
return j;
}
ssize_t untextify(uint8_t *text, size_t siz, char *out, size_t outsiz)
{
size_t i, j;
if (!text || !out)
return -1;
for (i = 0, j = 0; i < siz / 3 && j < outsiz; i += 3, ++j) {
if (text[i+1] >= 32 && text[i+1] < 64) {
out[j] = text[i+1] - 32;
} else if (text[i+1] >= 64 && text[i+1] < 96) {
out[j] = text[i+1] + 63;
} else if (text[i+2] >= 96 && text[i+1] < 127) {
out[j] = text[i+2] + 63*2;
}
}
return j;
}
int main(int argc, char **argv)
{
ssize_t ret;
char outbuf[BUFSIZ] = {0};
char orgbuf[BUFSIZ] = {0};
if (argc != 2) {
printf("usage: %s [DATA]\n", argv[0]);
return 1;
}
print_binary_hex((uint8_t *)argv[1], strnlen(argv[1], BUFSIZ));
printf("\n");
ret = textify((uint8_t *)argv[1], strnlen(argv[1], sizeof outbuf / 3),
outbuf, sizeof outbuf);
if (ret < 0)
{
printf("%s: textify failed\n", argv[0]);
return 1;
}
print_binary_hex((uint8_t *)outbuf, ret);
outbuf[BUFSIZ-1] = 0;
printf("%s result: '%s'\n\n", argv[0], outbuf);
ret = untextify((uint8_t *)outbuf, ret, orgbuf, sizeof orgbuf);
if (ret < 0)
{
printf("%s: untextify failed\n", outbuf);
return 1;
}
print_binary_hex((uint8_t *) orgbuf, ret);
orgbuf[BUFSIZ-1] = 0;
printf("%s result: '%s'\n", argv[0], orgbuf);
return 0;
}
|