blob: bbacdca31b3bc3c1020d49e8e0c4c539a1232cbc [file] [log] [blame]
Elijah Newren9b5041f2023-04-22 20:17:13 +00001#include "git-compat-util.h"
2#include "base85.h"
Junio C Hamano06606262006-05-05 02:41:53 -07003
4#undef DEBUG_85
5
6#ifdef DEBUG_85
7#define say(a) fprintf(stderr, a)
8#define say1(a,b) fprintf(stderr, a, b)
9#define say2(a,b,c) fprintf(stderr, a, b, c)
10#else
Jonathan Nieder98746062010-08-12 17:11:15 -050011#define say(a) do { /* nothing */ } while (0)
12#define say1(a,b) do { /* nothing */ } while (0)
13#define say2(a,b,c) do { /* nothing */ } while (0)
Junio C Hamano06606262006-05-05 02:41:53 -070014#endif
15
16static const char en85[] = {
17 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
18 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
19 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
20 'U', 'V', 'W', 'X', 'Y', 'Z',
21 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
22 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
23 'u', 'v', 'w', 'x', 'y', 'z',
24 '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
25 ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
26 '|', '}', '~'
27};
28
29static char de85[256];
30static void prep_base85(void)
31{
32 int i;
33 if (de85['Z'])
34 return;
35 for (i = 0; i < ARRAY_SIZE(en85); i++) {
36 int ch = en85[i];
37 de85[ch] = i + 1;
38 }
39}
40
Jim Meyeringf9815772007-04-10 00:56:33 +020041int decode_85(char *dst, const char *buffer, int len)
Junio C Hamano06606262006-05-05 02:41:53 -070042{
43 prep_base85();
44
Junio C Hamano5f050e32013-10-15 15:27:34 -070045 say2("decode 85 <%.*s>", len / 4 * 5, buffer);
Junio C Hamano06606262006-05-05 02:41:53 -070046 while (len) {
47 unsigned acc = 0;
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040048 int de, cnt = 4;
49 unsigned char ch;
50 do {
51 ch = *buffer++;
52 de = de85[ch];
53 if (--de < 0)
Junio C Hamano06606262006-05-05 02:41:53 -070054 return error("invalid base85 alphabet %c", ch);
Junio C Hamano06606262006-05-05 02:41:53 -070055 acc = acc * 85 + de;
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040056 } while (--cnt);
57 ch = *buffer++;
58 de = de85[ch];
59 if (--de < 0)
60 return error("invalid base85 alphabet %c", ch);
Andreas Gruenbacher0606c362010-01-08 14:40:00 +010061 /* Detect overflow. */
62 if (0xffffffff / 85 < acc ||
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040063 0xffffffff - de < (acc *= 85))
Jerald Fitzjerald86d14e12007-05-30 05:39:58 -070064 return error("invalid base85 sequence %.5s", buffer-5);
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040065 acc += de;
Junio C Hamano06606262006-05-05 02:41:53 -070066 say1(" %08x", acc);
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040067
68 cnt = (len < 4) ? len : 4;
69 len -= cnt;
70 do {
71 acc = (acc << 8) | (acc >> 24);
72 *dst++ = acc;
73 } while (--cnt);
Junio C Hamano06606262006-05-05 02:41:53 -070074 }
75 say("\n");
76
77 return 0;
78}
79
Jim Meyeringf9815772007-04-10 00:56:33 +020080void encode_85(char *buf, const unsigned char *data, int bytes)
Junio C Hamano06606262006-05-05 02:41:53 -070081{
Junio C Hamano06606262006-05-05 02:41:53 -070082 say("encode 85");
83 while (bytes) {
84 unsigned acc = 0;
85 int cnt;
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040086 for (cnt = 24; cnt >= 0; cnt -= 8) {
Linus Torvalds48fb7de2009-06-17 17:22:27 -070087 unsigned ch = *data++;
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040088 acc |= ch << cnt;
89 if (--bytes == 0)
90 break;
Junio C Hamano06606262006-05-05 02:41:53 -070091 }
92 say1(" %08x", acc);
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040093 for (cnt = 4; cnt >= 0; cnt--) {
Junio C Hamano06606262006-05-05 02:41:53 -070094 int val = acc % 85;
95 acc /= 85;
Nicolas Pitreaddaaca2006-05-08 11:31:11 -040096 buf[cnt] = en85[val];
Junio C Hamano06606262006-05-05 02:41:53 -070097 }
98 buf += 5;
99 }
100 say("\n");
101
102 *buf = 0;
103}
104
105#ifdef DEBUG_85
106int main(int ac, char **av)
107{
108 char buf[1024];
109
110 if (!strcmp(av[1], "-e")) {
111 int len = strlen(av[2]);
112 encode_85(buf, av[2], len);
113 if (len <= 26) len = len + 'A' - 1;
Andreas Gruenbacher75b7e162010-01-08 14:39:58 +0100114 else len = len + 'a' - 26 - 1;
Junio C Hamano06606262006-05-05 02:41:53 -0700115 printf("encoded: %c%s\n", len, buf);
116 return 0;
117 }
118 if (!strcmp(av[1], "-d")) {
119 int len = *av[2];
120 if ('A' <= len && len <= 'Z') len = len - 'A' + 1;
121 else len = len - 'a' + 26 + 1;
122 decode_85(buf, av[2]+1, len);
123 printf("decoded: %.*s\n", len, buf);
124 return 0;
125 }
126 if (!strcmp(av[1], "-t")) {
127 char t[4] = { -1,-1,-1,-1 };
128 encode_85(buf, t, 4);
129 printf("encoded: D%s\n", buf);
130 return 0;
131 }
132}
133#endif