6 #define say(a) fprintf(stderr, a)
7 #define say1(a,b) fprintf(stderr, a, b)
8 #define say2(a,b,c) fprintf(stderr, a, b, c)
10 #define say(a) do {} while(0)
11 #define say1(a,b) do {} while(0)
12 #define say2(a,b,c) do {} while(0)
15 static const char en85
[] = {
16 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
17 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
18 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
19 'U', 'V', 'W', 'X', 'Y', 'Z',
20 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
21 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
22 'u', 'v', 'w', 'x', 'y', 'z',
23 '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
24 ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
28 static char de85
[256];
29 static void prep_base85(void)
34 for (i
= 0; i
< ARRAY_SIZE(en85
); i
++) {
40 int decode_85(char *dst
, char *buffer
, int len
)
44 say2("decode 85 <%.*s>", len
/4*5, buffer
);
53 return error("invalid base85 alphabet %c", ch
);
59 return error("invalid base85 alphabet %c", ch
);
61 * Detect overflow. The largest
62 * 5-letter possible is "|NsC0" to
63 * encode 0xffffffff, and "|NsC" gives
64 * 0x03030303 at this point (i.e.
65 * 0xffffffff = 0x03030303 * 85).
67 if (0x03030303 < acc
||
68 0xffffffff - de
< (acc
*= 85))
69 error("invalid base85 sequence %.5s", buffer
-5);
73 cnt
= (len
< 4) ? len
: 4;
76 acc
= (acc
<< 8) | (acc
>> 24);
85 void encode_85(char *buf
, unsigned char *data
, int bytes
)
93 for (cnt
= 24; cnt
>= 0; cnt
-= 8) {
100 for (cnt
= 4; cnt
>= 0; cnt
--) {
103 buf
[cnt
] = en85
[val
];
113 int main(int ac
, char **av
)
117 if (!strcmp(av
[1], "-e")) {
118 int len
= strlen(av
[2]);
119 encode_85(buf
, av
[2], len
);
120 if (len
<= 26) len
= len
+ 'A' - 1;
121 else len
= len
+ 'a' - 26 + 1;
122 printf("encoded: %c%s\n", len
, buf
);
125 if (!strcmp(av
[1], "-d")) {
127 if ('A' <= len
&& len
<= 'Z') len
= len
- 'A' + 1;
128 else len
= len
- 'a' + 26 + 1;
129 decode_85(buf
, av
[2]+1, len
);
130 printf("decoded: %.*s\n", len
, buf
);
133 if (!strcmp(av
[1], "-t")) {
134 char t
[4] = { -1,-1,-1,-1 };
135 encode_85(buf
, t
, 4);
136 printf("encoded: D%s\n", buf
);