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
, const 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
);
60 /* Detect overflow. */
61 if (0xffffffff / 85 < acc
||
62 0xffffffff - de
< (acc
*= 85))
63 return error("invalid base85 sequence %.5s", buffer
-5);
67 cnt
= (len
< 4) ? len
: 4;
70 acc
= (acc
<< 8) | (acc
>> 24);
79 void encode_85(char *buf
, const unsigned char *data
, int bytes
)
85 for (cnt
= 24; cnt
>= 0; cnt
-= 8) {
92 for (cnt
= 4; cnt
>= 0; cnt
--) {
105 int main(int ac
, char **av
)
109 if (!strcmp(av
[1], "-e")) {
110 int len
= strlen(av
[2]);
111 encode_85(buf
, av
[2], len
);
112 if (len
<= 26) len
= len
+ 'A' - 1;
113 else len
= len
+ 'a' - 26 - 1;
114 printf("encoded: %c%s\n", len
, buf
);
117 if (!strcmp(av
[1], "-d")) {
119 if ('A' <= len
&& len
<= 'Z') len
= len
- 'A' + 1;
120 else len
= len
- 'a' + 26 + 1;
121 decode_85(buf
, av
[2]+1, len
);
122 printf("decoded: %.*s\n", len
, buf
);
125 if (!strcmp(av
[1], "-t")) {
126 char t
[4] = { -1,-1,-1,-1 };
127 encode_85(buf
, t
, 4);
128 printf("encoded: D%s\n", buf
);