Elijah Newren | 9b5041f | 2023-04-22 20:17:13 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "base85.h" |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3 | |
| 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 Nieder | 9874606 | 2010-08-12 17:11:15 -0500 | [diff] [blame] | 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) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 14 | #endif |
| 15 | |
| 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 | '|', '}', '~' |
| 27 | }; |
| 28 | |
| 29 | static char de85[256]; |
| 30 | static 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 Meyering | f981577 | 2007-04-10 00:56:33 +0200 | [diff] [blame] | 41 | int decode_85(char *dst, const char *buffer, int len) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 42 | { |
| 43 | prep_base85(); |
| 44 | |
Junio C Hamano | 5f050e3 | 2013-10-15 15:27:34 -0700 | [diff] [blame] | 45 | say2("decode 85 <%.*s>", len / 4 * 5, buffer); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 46 | while (len) { |
| 47 | unsigned acc = 0; |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 48 | int de, cnt = 4; |
| 49 | unsigned char ch; |
| 50 | do { |
| 51 | ch = *buffer++; |
| 52 | de = de85[ch]; |
| 53 | if (--de < 0) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 54 | return error("invalid base85 alphabet %c", ch); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 55 | acc = acc * 85 + de; |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 56 | } while (--cnt); |
| 57 | ch = *buffer++; |
| 58 | de = de85[ch]; |
| 59 | if (--de < 0) |
| 60 | return error("invalid base85 alphabet %c", ch); |
Andreas Gruenbacher | 0606c36 | 2010-01-08 14:40:00 +0100 | [diff] [blame] | 61 | /* Detect overflow. */ |
| 62 | if (0xffffffff / 85 < acc || |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 63 | 0xffffffff - de < (acc *= 85)) |
Jerald Fitzjerald | 86d14e1 | 2007-05-30 05:39:58 -0700 | [diff] [blame] | 64 | return error("invalid base85 sequence %.5s", buffer-5); |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 65 | acc += de; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 66 | say1(" %08x", acc); |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 67 | |
| 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 Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 74 | } |
| 75 | say("\n"); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Jim Meyering | f981577 | 2007-04-10 00:56:33 +0200 | [diff] [blame] | 80 | void encode_85(char *buf, const unsigned char *data, int bytes) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 81 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 82 | say("encode 85"); |
| 83 | while (bytes) { |
| 84 | unsigned acc = 0; |
| 85 | int cnt; |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 86 | for (cnt = 24; cnt >= 0; cnt -= 8) { |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 87 | unsigned ch = *data++; |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 88 | acc |= ch << cnt; |
| 89 | if (--bytes == 0) |
| 90 | break; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 91 | } |
| 92 | say1(" %08x", acc); |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 93 | for (cnt = 4; cnt >= 0; cnt--) { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 94 | int val = acc % 85; |
| 95 | acc /= 85; |
Nicolas Pitre | addaaca | 2006-05-08 11:31:11 -0400 | [diff] [blame] | 96 | buf[cnt] = en85[val]; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 97 | } |
| 98 | buf += 5; |
| 99 | } |
| 100 | say("\n"); |
| 101 | |
| 102 | *buf = 0; |
| 103 | } |
| 104 | |
| 105 | #ifdef DEBUG_85 |
| 106 | int 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 Gruenbacher | 75b7e16 | 2010-01-08 14:39:58 +0100 | [diff] [blame] | 114 | else len = len + 'a' - 26 - 1; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 115 | 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 |