Merge branch 'rs/strbuf-detach'
[git/git.git] / base85.c
CommitLineData
0660626c
JH
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
98746061
JN
10#define say(a) do { /* nothing */ } while (0)
11#define say1(a,b) do { /* nothing */ } while (0)
12#define say2(a,b,c) do { /* nothing */ } while (0)
0660626c
JH
13#endif
14
15static 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
28static char de85[256];
29static 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
f9815772 40int decode_85(char *dst, const char *buffer, int len)
0660626c
JH
41{
42 prep_base85();
43
5f050e3c 44 say2("decode 85 <%.*s>", len / 4 * 5, buffer);
0660626c
JH
45 while (len) {
46 unsigned acc = 0;
addaacab
NP
47 int de, cnt = 4;
48 unsigned char ch;
49 do {
50 ch = *buffer++;
51 de = de85[ch];
52 if (--de < 0)
0660626c 53 return error("invalid base85 alphabet %c", ch);
0660626c 54 acc = acc * 85 + de;
addaacab
NP
55 } while (--cnt);
56 ch = *buffer++;
57 de = de85[ch];
58 if (--de < 0)
59 return error("invalid base85 alphabet %c", ch);
0606c36a
AG
60 /* Detect overflow. */
61 if (0xffffffff / 85 < acc ||
addaacab 62 0xffffffff - de < (acc *= 85))
86d14e1b 63 return error("invalid base85 sequence %.5s", buffer-5);
addaacab 64 acc += de;
0660626c 65 say1(" %08x", acc);
addaacab
NP
66
67 cnt = (len < 4) ? len : 4;
68 len -= cnt;
69 do {
70 acc = (acc << 8) | (acc >> 24);
71 *dst++ = acc;
72 } while (--cnt);
0660626c
JH
73 }
74 say("\n");
75
76 return 0;
77}
78
f9815772 79void encode_85(char *buf, const unsigned char *data, int bytes)
0660626c 80{
0660626c
JH
81 say("encode 85");
82 while (bytes) {
83 unsigned acc = 0;
84 int cnt;
addaacab 85 for (cnt = 24; cnt >= 0; cnt -= 8) {
48fb7deb 86 unsigned ch = *data++;
addaacab
NP
87 acc |= ch << cnt;
88 if (--bytes == 0)
89 break;
0660626c
JH
90 }
91 say1(" %08x", acc);
addaacab 92 for (cnt = 4; cnt >= 0; cnt--) {
0660626c
JH
93 int val = acc % 85;
94 acc /= 85;
addaacab 95 buf[cnt] = en85[val];
0660626c
JH
96 }
97 buf += 5;
98 }
99 say("\n");
100
101 *buf = 0;
102}
103
104#ifdef DEBUG_85
105int main(int ac, char **av)
106{
107 char buf[1024];
108
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;
75b7e16b 113 else len = len + 'a' - 26 - 1;
0660626c
JH
114 printf("encoded: %c%s\n", len, buf);
115 return 0;
116 }
117 if (!strcmp(av[1], "-d")) {
118 int len = *av[2];
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);
123 return 0;
124 }
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);
129 return 0;
130 }
131}
132#endif