documentation: add some commas where they are helpful
[git.git] / base85.c
blobbbacdca31b3bc3c1020d49e8e0c4c539a1232cbc
1 #include "git-compat-util.h"
2 #include "base85.h"
4 #undef DEBUG_85
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
11 #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)
14 #endif
16 static 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 '|', '}', '~'
29 static char de85[256];
30 static void prep_base85(void)
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;
41 int decode_85(char *dst, const char *buffer, int len)
43 prep_base85();
45 say2("decode 85 <%.*s>", len / 4 * 5, buffer);
46 while (len) {
47 unsigned acc = 0;
48 int de, cnt = 4;
49 unsigned char ch;
50 do {
51 ch = *buffer++;
52 de = de85[ch];
53 if (--de < 0)
54 return error("invalid base85 alphabet %c", ch);
55 acc = acc * 85 + de;
56 } while (--cnt);
57 ch = *buffer++;
58 de = de85[ch];
59 if (--de < 0)
60 return error("invalid base85 alphabet %c", ch);
61 /* Detect overflow. */
62 if (0xffffffff / 85 < acc ||
63 0xffffffff - de < (acc *= 85))
64 return error("invalid base85 sequence %.5s", buffer-5);
65 acc += de;
66 say1(" %08x", acc);
68 cnt = (len < 4) ? len : 4;
69 len -= cnt;
70 do {
71 acc = (acc << 8) | (acc >> 24);
72 *dst++ = acc;
73 } while (--cnt);
75 say("\n");
77 return 0;
80 void encode_85(char *buf, const unsigned char *data, int bytes)
82 say("encode 85");
83 while (bytes) {
84 unsigned acc = 0;
85 int cnt;
86 for (cnt = 24; cnt >= 0; cnt -= 8) {
87 unsigned ch = *data++;
88 acc |= ch << cnt;
89 if (--bytes == 0)
90 break;
92 say1(" %08x", acc);
93 for (cnt = 4; cnt >= 0; cnt--) {
94 int val = acc % 85;
95 acc /= 85;
96 buf[cnt] = en85[val];
98 buf += 5;
100 say("\n");
102 *buf = 0;
105 #ifdef DEBUG_85
106 int main(int ac, char **av)
108 char buf[1024];
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;
114 else len = len + 'a' - 26 - 1;
115 printf("encoded: %c%s\n", len, buf);
116 return 0;
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;
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;
133 #endif