git-tar-tree: no more void pointer arithmetic
[git.git] / base85.c
1 #include "cache.h"
2
3 #undef DEBUG_85
4
5 #ifdef DEBUG_85
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)
9 #else
10 #define say(a) do {} while(0)
11 #define say1(a,b) do {} while(0)
12 #define say2(a,b,c) do {} while(0)
13 #endif
14
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         ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
25         '|', '}', '~'
26 };
27
28 static char de85[256];
29 static void prep_base85(void)
30 {
31         int i;
32         if (de85['Z'])
33                 return;
34         for (i = 0; i < ARRAY_SIZE(en85); i++) {
35                 int ch = en85[i];
36                 de85[ch] = i + 1;
37         }
38 }
39
40 int decode_85(char *dst, char *buffer, int len)
41 {
42         prep_base85();
43
44         say2("decode 85 <%.*s>", len/4*5, buffer);
45         while (len) {
46                 unsigned acc = 0;
47                 int de, cnt = 4;
48                 unsigned char ch;
49                 do {
50                         ch = *buffer++;
51                         de = de85[ch];
52                         if (--de < 0)
53                                 return error("invalid base85 alphabet %c", ch);
54                         acc = acc * 85 + de;
55                 } while (--cnt);
56                 ch = *buffer++;
57                 de = de85[ch];
58                 if (--de < 0)
59                         return error("invalid base85 alphabet %c", ch);
60                 /*
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).
66                  */
67                 if (0x03030303 < acc ||
68                     0xffffffff - de < (acc *= 85))
69                         error("invalid base85 sequence %.5s", buffer-5);
70                 acc += de;
71                 say1(" %08x", acc);
72
73                 cnt = (len < 4) ? len : 4;
74                 len -= cnt;
75                 do {
76                         acc = (acc << 8) | (acc >> 24);
77                         *dst++ = acc;
78                 } while (--cnt);
79         }
80         say("\n");
81
82         return 0;
83 }
84
85 void encode_85(char *buf, unsigned char *data, int bytes)
86 {
87         prep_base85();
88
89         say("encode 85");
90         while (bytes) {
91                 unsigned acc = 0;
92                 int cnt;
93                 for (cnt = 24; cnt >= 0; cnt -= 8) {
94                         int ch = *data++;
95                         acc |= ch << cnt;
96                         if (--bytes == 0)
97                                 break;
98                 }
99                 say1(" %08x", acc);
100                 for (cnt = 4; cnt >= 0; cnt--) {
101                         int val = acc % 85;
102                         acc /= 85;
103                         buf[cnt] = en85[val];
104                 }
105                 buf += 5;
106         }
107         say("\n");
108
109         *buf = 0;
110 }
111
112 #ifdef DEBUG_85
113 int main(int ac, char **av)
114 {
115         char buf[1024];
116
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);
123                 return 0;
124         }
125         if (!strcmp(av[1], "-d")) {
126                 int len = *av[2];
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);
131                 return 0;
132         }
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);
137                 return 0;
138         }
139 }
140 #endif