git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / base85.c
| 1 | #include "git-compat-util.h" |
| 2 | #include "base85.h" |
| 3 | |
| 4 | static const char en85[] = { |
| 5 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 6 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
| 7 | 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
| 8 | 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 9 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
| 10 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
| 11 | 'u', 'v', 'w', 'x', 'y', 'z', |
| 12 | '!', '#', '$', '%', '&', '(', ')', '*', '+', '-', |
| 13 | ';', '<', '=', '>', '?', '@', '^', '_', '`', '{', |
| 14 | '|', '}', '~' |
| 15 | }; |
| 16 | |
| 17 | static char de85[256]; |
| 18 | static void prep_base85(void) |
| 19 | { |
| 20 | if (de85['Z']) |
| 21 | return; |
| 22 | for (size_t i = 0; i < ARRAY_SIZE(en85); i++) { |
| 23 | int ch = en85[i]; |
| 24 | de85[ch] = i + 1; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | int decode_85(char *dst, const char *buffer, int len) |
| 29 | { |
| 30 | prep_base85(); |
| 31 | |
| 32 | while (len) { |
| 33 | unsigned acc = 0; |
| 34 | int de, cnt = 4; |
| 35 | unsigned char ch; |
| 36 | do { |
| 37 | ch = *buffer++; |
| 38 | de = de85[ch]; |
| 39 | if (--de < 0) |
| 40 | return error("invalid base85 alphabet %c", ch); |
| 41 | acc = acc * 85 + de; |
| 42 | } while (--cnt); |
| 43 | ch = *buffer++; |
| 44 | de = de85[ch]; |
| 45 | if (--de < 0) |
| 46 | return error("invalid base85 alphabet %c", ch); |
| 47 | /* Detect overflow. */ |
| 48 | if (0xffffffff / 85 < acc || |
| 49 | 0xffffffff - de < (acc *= 85)) |
| 50 | return error("invalid base85 sequence %.5s", buffer-5); |
| 51 | acc += de; |
| 52 | |
| 53 | cnt = (len < 4) ? len : 4; |
| 54 | len -= cnt; |
| 55 | do { |
| 56 | acc = (acc << 8) | (acc >> 24); |
| 57 | *dst++ = acc; |
| 58 | } while (--cnt); |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void encode_85(char *buf, const unsigned char *data, int bytes) |
| 65 | { |
| 66 | while (bytes) { |
| 67 | unsigned acc = 0; |
| 68 | int cnt; |
| 69 | for (cnt = 24; cnt >= 0; cnt -= 8) { |
| 70 | unsigned ch = *data++; |
| 71 | acc |= ch << cnt; |
| 72 | if (--bytes == 0) |
| 73 | break; |
| 74 | } |
| 75 | for (cnt = 4; cnt >= 0; cnt--) { |
| 76 | int val = acc % 85; |
| 77 | acc /= 85; |
| 78 | buf[cnt] = en85[val]; |
| 79 | } |
| 80 | buf += 5; |
| 81 | } |
| 82 | |
| 83 | *buf = 0; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Split the provided data into chunks of at most 52 bytes. Each chunk |
| 88 | * is base85-encoded into a line with the following format: |
| 89 | * - Byte 0: encodes the chunk size (1-52) as a letter: 'A'-'Z' for |
| 90 | * 1-26 bytes, 'a'-'z' for 27-52 bytes. |
| 91 | * - Bytes 1 onwards: base85-encoded data (up to 65 bytes). |
| 92 | * - Followed by a newline and NUL terminator. |
| 93 | * The output callback is called once per line. |
| 94 | */ |
| 95 | void chunk_and_encode85(const unsigned char *data, unsigned long len, |
| 96 | each_encoded_line_fn output, void *output_data) |
| 97 | { |
| 98 | while (len) { |
| 99 | int line_len; |
| 100 | int bytes = (BASE85_CHUNK_SIZE < len) ? BASE85_CHUNK_SIZE : len; |
| 101 | char line[71]; |
| 102 | len -= bytes; |
| 103 | if (bytes <= 26) |
| 104 | line[0] = bytes + 'A' - 1; |
| 105 | else |
| 106 | line[0] = bytes - 26 + 'a' - 1; |
| 107 | encode_85(line + 1, data, bytes); |
| 108 | data = data + bytes; |
| 109 | |
| 110 | line_len = strlen(line); |
| 111 | line[line_len++] = '\n'; |
| 112 | line[line_len] = '\0'; |
| 113 | output(line, line_len, output_data); |
| 114 | } |
| 115 | } |