| 1 |
|
| /* |
| 2 |
|
| * b64.c - Base64 encode/decode for File::Raw::Base64. |
| 3 |
|
| * |
| 4 |
|
| * Standard alphabet from RFC 4648, |
| 5 |
|
| * URL-safe variant via opts.urlsafe. PEM envelope handling on top. |
| 6 |
|
| * |
| 7 |
|
| * Strict C89 compliant: every declaration at the top of its block |
| 8 |
|
| * scope, no inline for-loop initialisers, no // comments. The dist |
| 9 |
|
| * targets perl 5.8.3+ where the platform compiler may default to |
| 10 |
|
| * C89 (e.g. GCC 4.2.1 on FreeBSD 9.x). |
| 11 |
|
| */ |
| 12 |
|
| |
| 13 |
|
| #include "b64.h" |
| 14 |
|
| |
| 15 |
|
| #include <stdint.h> |
| 16 |
|
| #include <stdlib.h> |
| 17 |
|
| #include <string.h> |
| 18 |
|
| |
| 19 |
|
| /* ------------------------------------------------------------ |
| 20 |
|
| * Alphabet tables |
| 21 |
|
| * ------------------------------------------------------------ */ |
| 22 |
|
| |
| 23 |
|
| static const char ALPHA_STD[64] = |
| 24 |
|
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 25 |
|
| |
| 26 |
|
| static const char ALPHA_URL[64] = |
| 27 |
|
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 28 |
|
| |
| 29 |
|
| /* Reverse-lookup tables. -1 = not in alphabet. -2 = padding ('='). */ |
| 30 |
|
| #define INVAL ((signed char)-1) |
| 31 |
|
| #define PAD ((signed char)-2) |
| 32 |
|
| |
| 33 |
|
| static signed char REV_STD[256]; |
| 34 |
|
| static signed char REV_URL[256]; |
| 35 |
|
| static int rev_initialised = 0; |
| 36 |
|
| |
| 37 |
|
| static void |
| 38 |
8 |
| rev_init(void) |
| 39 |
|
| { |
| 40 |
|
| int i; |
| 41 |
2056 |
▶ | for (i = 0; i < 256; i++) { REV_STD[i] = INVAL; REV_URL[i] = INVAL; } |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 256; i++) { REV_STD[i] = INVAL; REV_URL[i] = INVAL; } |
2048 |
8 |
2056 |
|
| 42 |
520 |
▶ | for (i = 0; i < 64; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 64; i++) { |
512 |
8 |
520 |
|
| 43 |
512 |
| REV_STD[(unsigned char)ALPHA_STD[i]] = (signed char)i; |
| 44 |
512 |
| REV_URL[(unsigned char)ALPHA_URL[i]] = (signed char)i; |
| 45 |
|
| } |
| 46 |
8 |
| REV_STD[(unsigned char)'='] = PAD; |
| 47 |
8 |
| REV_URL[(unsigned char)'='] = PAD; |
| 48 |
8 |
| rev_initialised = 1; |
| 49 |
8 |
| } |
| 50 |
|
| |
| 51 |
|
| /* ------------------------------------------------------------ |
| 52 |
|
| * Output buffer growth |
| 53 |
|
| * ------------------------------------------------------------ */ |
| 54 |
|
| |
| 55 |
|
| static int |
| 56 |
75 |
| out_reserve(void **out, size_t *out_cap, size_t need) |
| 57 |
|
| { |
| 58 |
|
| size_t new_cap; |
| 59 |
|
| void *new_buf; |
| 60 |
|
| |
| 61 |
75 |
▶ | if (need <= *out_cap) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (need <= *out_cap) return 0; |
9 |
66 |
75 |
|
| 62 |
|
| |
| 63 |
66 |
▶ | new_cap = *out_cap ? *out_cap : 64; |
Branch: 1 branch
| Branch | True | False | Total |
| new_cap = *out_cap ? *out_cap : 64; |
4 |
62 |
66 |
|
| 64 |
156 |
▶ | while (new_cap < need) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (new_cap < need) { |
90 |
66 |
156 |
|
| 65 |
90 |
▶ | if (new_cap > (SIZE_MAX / 2)) { new_cap = need; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_cap > (SIZE_MAX / 2)) { new_cap = need; break; } |
0 |
90 |
90 |
|
| 66 |
90 |
| new_cap *= 2; |
| 67 |
|
| } |
| 68 |
66 |
| new_buf = realloc(*out, new_cap); |
| 69 |
66 |
▶ | if (!new_buf) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!new_buf) return -1; |
0 |
66 |
66 |
|
| 70 |
66 |
| *out = new_buf; |
| 71 |
66 |
| *out_cap = new_cap; |
| 72 |
66 |
| return 0; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| /* ------------------------------------------------------------ |
| 76 |
|
| * Defaults |
| 77 |
|
| * ------------------------------------------------------------ */ |
| 78 |
|
| |
| 79 |
|
| void |
| 80 |
73 |
| b64_options_init(b64_options_t *opts) |
| 81 |
|
| { |
| 82 |
73 |
| memset(opts, 0, sizeof *opts); |
| 83 |
73 |
| opts->padding = 1; |
| 84 |
73 |
| opts->pem_label = "DATA"; |
| 85 |
73 |
| opts->eol = "\n"; |
| 86 |
|
| /* urlsafe, wrap, pem, strict default to 0 via memset */ |
| 87 |
73 |
| } |
| 88 |
|
| |
| 89 |
|
| /* ------------------------------------------------------------ |
| 90 |
|
| * Encode |
| 91 |
|
| * ------------------------------------------------------------ */ |
| 92 |
|
| |
| 93 |
|
| static int |
| 94 |
6 |
| emit_eol(char **out, size_t *cap, size_t *len, const char *eol) |
| 95 |
|
| { |
| 96 |
6 |
| size_t elen = strlen(eol); |
| 97 |
6 |
▶ | if (out_reserve((void **)out, cap, *len + elen) < 0) return B64_ERR_NOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_reserve((void **)out, cap, *len + elen) < 0) return B64_ERR_NOMEM; |
0 |
6 |
6 |
|
| 98 |
6 |
| memcpy(*out + *len, eol, elen); |
| 99 |
6 |
| *len += elen; |
| 100 |
6 |
| return 0; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| static int |
| 104 |
4 |
| emit_pem_header(char **out, size_t *cap, size_t *len, |
| 105 |
|
| const char *kind, const char *label, const char *eol) |
| 106 |
|
| { |
| 107 |
|
| /* "-----BEGIN $label-----$eol" */ |
| 108 |
4 |
| size_t llen = strlen(label); |
| 109 |
4 |
| size_t elen = strlen(eol); |
| 110 |
4 |
| size_t klen = strlen(kind); |
| 111 |
4 |
| size_t need = *len + 5 + 1 + klen + 1 + llen + 5 + elen; |
| 112 |
|
| char *p; |
| 113 |
|
| |
| 114 |
4 |
▶ | if (out_reserve((void **)out, cap, need) < 0) return B64_ERR_NOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_reserve((void **)out, cap, need) < 0) return B64_ERR_NOMEM; |
0 |
4 |
4 |
|
| 115 |
4 |
| p = *out + *len; |
| 116 |
4 |
| memcpy(p, "-----", 5); p += 5; |
| 117 |
4 |
| memcpy(p, kind, klen); p += klen; |
| 118 |
4 |
| *p++ = ' '; |
| 119 |
4 |
| memcpy(p, label, llen); p += llen; |
| 120 |
4 |
| memcpy(p, "-----", 5); p += 5; |
| 121 |
4 |
| memcpy(p, eol, elen); p += elen; |
| 122 |
4 |
| *len = (size_t)(p - *out); |
| 123 |
4 |
| return 0; |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| int |
| 127 |
33 |
| b64_encode(const unsigned char *in, size_t in_len, |
| 128 |
|
| const b64_options_t *opts, |
| 129 |
|
| char **out, size_t *out_cap, size_t *out_len, |
| 130 |
|
| size_t *err_offset) |
| 131 |
|
| { |
| 132 |
|
| const char *alpha; |
| 133 |
|
| const char *eol; |
| 134 |
|
| int rc; |
| 135 |
|
| size_t encoded_chars; |
| 136 |
33 |
| size_t wrap_eols = 0; |
| 137 |
|
| int wrap; |
| 138 |
|
| size_t i; |
| 139 |
|
| size_t tail; |
| 140 |
33 |
| int wrap_col = 0; |
| 141 |
|
| char *o; |
| 142 |
|
| |
| 143 |
33 |
▶ | if (!rev_initialised) rev_init(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rev_initialised) rev_init(); |
7 |
26 |
33 |
|
| 144 |
33 |
▶ | if (err_offset) *err_offset = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = 0; |
33 |
0 |
33 |
|
| 145 |
|
| |
| 146 |
33 |
▶ | alpha = opts->urlsafe ? ALPHA_URL : ALPHA_STD; |
Branch: 1 branch
| Branch | True | False | Total |
| alpha = opts->urlsafe ? ALPHA_URL : ALPHA_STD; |
3 |
30 |
33 |
|
| 147 |
33 |
▶ | eol = opts->eol ? opts->eol : "\n"; |
Branch: 1 branch
| Branch | True | False | Total |
| eol = opts->eol ? opts->eol : "\n"; |
33 |
0 |
33 |
|
| 148 |
33 |
| wrap = opts->wrap; |
| 149 |
|
| |
| 150 |
|
| /* PEM begin. */ |
| 151 |
33 |
▶ | if (opts->pem) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->pem) { |
2 |
31 |
33 |
|
| 152 |
2 |
| rc = emit_pem_header(out, out_cap, out_len, |
| 153 |
2 |
| "BEGIN", opts->pem_label, eol); |
| 154 |
2 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
0 |
2 |
2 |
|
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
|
| /* Worst-case sizing: 4 chars per 3 input bytes, plus wrap eols. */ |
| 158 |
33 |
| encoded_chars = ((in_len + 2) / 3) * 4; |
| 159 |
33 |
▶ | if (wrap > 0 && encoded_chars > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (wrap > 0 && encoded_chars > 0) { |
6 |
27 |
33 |
| if (wrap > 0 && encoded_chars > 0) { |
6 |
0 |
6 |
|
| 160 |
6 |
| size_t elen = strlen(eol); |
| 161 |
6 |
| wrap_eols = ((encoded_chars + (size_t)wrap - 1) / (size_t)wrap) * elen; |
| 162 |
|
| } |
| 163 |
33 |
▶ | if (out_reserve((void **)out, out_cap, |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_reserve((void **)out, out_cap, |
0 |
33 |
33 |
|
| 164 |
33 |
| *out_len + encoded_chars + wrap_eols) < 0) |
| 165 |
0 |
| return B64_ERR_NOMEM; |
| 166 |
|
| |
| 167 |
33 |
| o = *out + *out_len; |
| 168 |
|
| |
| 169 |
|
| #define EMIT(c) do { \ |
| 170 |
|
| *o++ = (c); \ |
| 171 |
|
| if (wrap > 0 && ++wrap_col == wrap) { \ |
| 172 |
|
| size_t cur = (size_t)(o - (*out)); \ |
| 173 |
|
| *out_len = cur; \ |
| 174 |
|
| rc = emit_eol(out, out_cap, out_len, eol); \ |
| 175 |
|
| if (rc) return rc; \ |
| 176 |
|
| o = *out + *out_len; \ |
| 177 |
|
| wrap_col = 0; \ |
| 178 |
|
| } \ |
| 179 |
|
| } while (0) |
| 180 |
|
| |
| 181 |
|
| /* Whole lines: when a line holds whole quartets (no wrap, or a wrap |
| 182 |
|
| * that is a multiple of 4, as 64 and 76 are), each line is written |
| 183 |
|
| * in one tight loop and its line end copied after it; the space for |
| 184 |
|
| * both was reserved above. Any other wrap takes the loop below. */ |
| 185 |
33 |
| i = 0; |
| 186 |
33 |
▶ | if (wrap == 0 || wrap % 4 == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (wrap == 0 || wrap % 4 == 0) { |
6 |
27 |
33 |
| if (wrap == 0 || wrap % 4 == 0) { |
6 |
0 |
6 |
|
| 187 |
33 |
▶ | size_t per_line = wrap ? (size_t)wrap / 4 : (size_t)-1; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t per_line = wrap ? (size_t)wrap / 4 : (size_t)-1; |
6 |
27 |
33 |
|
| 188 |
33 |
| size_t elen = strlen(eol); |
| 189 |
74 |
▶ | while (i + 3 <= in_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i + 3 <= in_len) { |
41 |
33 |
74 |
|
| 190 |
41 |
| size_t n = (in_len - i) / 3; |
| 191 |
41 |
▶ | if (n > per_line) n = per_line; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > per_line) n = per_line; |
17 |
24 |
41 |
|
| 192 |
41 |
| wrap_col = (int)(n * 4); |
| 193 |
197438 |
▶ | while (n--) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (n--) { |
197397 |
41 |
197438 |
|
| 194 |
197397 |
| uint32_t triplet = ((uint32_t)in[i] << 16) |
| 195 |
197397 |
| | ((uint32_t)in[i+1] << 8) |
| 196 |
197397 |
| | (uint32_t)in[i+2]; |
| 197 |
197397 |
| o[0] = alpha[(triplet >> 18) & 0x3F]; |
| 198 |
197397 |
| o[1] = alpha[(triplet >> 12) & 0x3F]; |
| 199 |
197397 |
| o[2] = alpha[(triplet >> 6) & 0x3F]; |
| 200 |
197397 |
| o[3] = alpha[ triplet & 0x3F]; |
| 201 |
197397 |
| o += 4; |
| 202 |
197397 |
| i += 3; |
| 203 |
|
| } |
| 204 |
41 |
▶ | if (wrap > 0 && wrap_col == wrap) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (wrap > 0 && wrap_col == wrap) { |
23 |
18 |
41 |
| if (wrap > 0 && wrap_col == wrap) { |
17 |
6 |
23 |
|
| 205 |
17 |
| memcpy(o, eol, elen); |
| 206 |
17 |
| o += elen; |
| 207 |
17 |
| wrap_col = 0; |
| 208 |
|
| } |
| 209 |
|
| } |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| /* Main loop: 3 bytes -> 4 chars. */ |
| 213 |
33 |
▶ | for (; i + 3 <= in_len; i += 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i + 3 <= in_len; i += 3) { |
0 |
33 |
33 |
|
| 214 |
0 |
| uint32_t triplet = ((uint32_t)in[i] << 16) |
| 215 |
0 |
| | ((uint32_t)in[i+1] << 8) |
| 216 |
0 |
| | (uint32_t)in[i+2]; |
| 217 |
0 |
▶ | EMIT(alpha[(triplet >> 18) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(triplet >> 18) & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[(triplet >> 18) & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[(triplet >> 18) & 0x3F]); |
0 |
0 |
0 |
|
| 218 |
0 |
▶ | EMIT(alpha[(triplet >> 12) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(triplet >> 12) & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[(triplet >> 12) & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[(triplet >> 12) & 0x3F]); |
0 |
0 |
0 |
|
| 219 |
0 |
▶ | EMIT(alpha[(triplet >> 6) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(triplet >> 6) & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[(triplet >> 6) & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[(triplet >> 6) & 0x3F]); |
0 |
0 |
0 |
|
| 220 |
0 |
▶ | EMIT(alpha[ triplet & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[ triplet & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[ triplet & 0x3F]); |
0 |
0 |
0 |
| EMIT(alpha[ triplet & 0x3F]); |
0 |
0 |
0 |
|
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| /* Tail: 1 or 2 leftover bytes. */ |
| 224 |
33 |
| tail = in_len - i; |
| 225 |
33 |
▶ | if (tail == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tail == 1) { |
13 |
20 |
33 |
|
| 226 |
13 |
| uint32_t b = (uint32_t)in[i] << 16; |
| 227 |
13 |
▶ | EMIT(alpha[(b >> 18) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(b >> 18) & 0x3F]); |
2 |
11 |
13 |
| EMIT(alpha[(b >> 18) & 0x3F]); |
0 |
2 |
2 |
| EMIT(alpha[(b >> 18) & 0x3F]); |
0 |
0 |
0 |
|
| 228 |
13 |
▶ | EMIT(alpha[(b >> 12) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(b >> 12) & 0x3F]); |
2 |
11 |
13 |
| EMIT(alpha[(b >> 12) & 0x3F]); |
0 |
2 |
2 |
| EMIT(alpha[(b >> 12) & 0x3F]); |
0 |
0 |
0 |
|
| 229 |
13 |
▶ | if (opts->padding) { EMIT('='); EMIT('='); } |
Branch: 7 branches
| Branch | True | False | Total |
| if (opts->padding) { EMIT('='); EMIT('='); } |
12 |
1 |
13 |
| if (opts->padding) { EMIT('='); EMIT('='); } |
2 |
10 |
12 |
| if (opts->padding) { EMIT('='); EMIT('='); } |
0 |
2 |
2 |
| if (opts->padding) { EMIT('='); EMIT('='); } |
0 |
0 |
0 |
| if (opts->padding) { EMIT('='); EMIT('='); } |
2 |
10 |
12 |
| if (opts->padding) { EMIT('='); EMIT('='); } |
0 |
2 |
2 |
| if (opts->padding) { EMIT('='); EMIT('='); } |
0 |
0 |
0 |
|
| 230 |
20 |
▶ | } else if (tail == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (tail == 2) { |
10 |
10 |
20 |
|
| 231 |
10 |
| uint32_t b = ((uint32_t)in[i] << 16) | ((uint32_t)in[i+1] << 8); |
| 232 |
10 |
▶ | EMIT(alpha[(b >> 18) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(b >> 18) & 0x3F]); |
4 |
6 |
10 |
| EMIT(alpha[(b >> 18) & 0x3F]); |
0 |
4 |
4 |
| EMIT(alpha[(b >> 18) & 0x3F]); |
0 |
0 |
0 |
|
| 233 |
10 |
▶ | EMIT(alpha[(b >> 12) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(b >> 12) & 0x3F]); |
4 |
6 |
10 |
| EMIT(alpha[(b >> 12) & 0x3F]); |
0 |
4 |
4 |
| EMIT(alpha[(b >> 12) & 0x3F]); |
0 |
0 |
0 |
|
| 234 |
10 |
▶ | EMIT(alpha[(b >> 6) & 0x3F]); |
Branch: 3 branches
| Branch | True | False | Total |
| EMIT(alpha[(b >> 6) & 0x3F]); |
4 |
6 |
10 |
| EMIT(alpha[(b >> 6) & 0x3F]); |
0 |
4 |
4 |
| EMIT(alpha[(b >> 6) & 0x3F]); |
0 |
0 |
0 |
|
| 235 |
10 |
▶ | if (opts->padding) EMIT('='); |
Branch: 4 branches
| Branch | True | False | Total |
| if (opts->padding) EMIT('='); |
9 |
1 |
10 |
| if (opts->padding) EMIT('='); |
4 |
5 |
9 |
| if (opts->padding) EMIT('='); |
0 |
4 |
4 |
| if (opts->padding) EMIT('='); |
0 |
0 |
0 |
|
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
|
| #undef EMIT |
| 239 |
|
| |
| 240 |
|
| /* Final EOL after the body if we wrapped (so the trailing line |
| 241 |
|
| * terminates) and no eol is already pending - wrap loop only emits |
| 242 |
|
| * an eol when it just hit `wrap_col == wrap`, so a partial last |
| 243 |
|
| * line still needs one. PEM mode also wants an EOL before END. */ |
| 244 |
33 |
| *out_len = (size_t)(o - *out); |
| 245 |
33 |
▶ | if ((wrap > 0 && wrap_col != 0) || opts->pem) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((wrap > 0 && wrap_col != 0) || opts->pem) { |
6 |
27 |
33 |
| if ((wrap > 0 && wrap_col != 0) || opts->pem) { |
0 |
6 |
6 |
| if ((wrap > 0 && wrap_col != 0) || opts->pem) { |
0 |
27 |
27 |
|
| 246 |
6 |
| rc = emit_eol(out, out_cap, out_len, eol); |
| 247 |
6 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
0 |
6 |
6 |
|
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
33 |
▶ | if (opts->pem) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->pem) { |
2 |
31 |
33 |
|
| 251 |
2 |
| rc = emit_pem_header(out, out_cap, out_len, |
| 252 |
2 |
| "END", opts->pem_label, eol); |
| 253 |
2 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
0 |
2 |
2 |
|
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
33 |
| return B64_OK; |
| 257 |
|
| } |
| 258 |
|
| |
| 259 |
|
| /* ------------------------------------------------------------ |
| 260 |
|
| * Decode |
| 261 |
|
| * |
| 262 |
|
| * On PEM input, find the BEGIN/END markers and slice out the body |
| 263 |
|
| * before running the regular decode loop on it. |
| 264 |
|
| * ------------------------------------------------------------ */ |
| 265 |
|
| |
| 266 |
|
| /* Find a substring in [haystack, haystack+haystack_len). NULL if not |
| 267 |
|
| * found. Standard memmem isn't portable, so roll our own. */ |
| 268 |
|
| static const char * |
| 269 |
19 |
| mem_find(const char *haystack, size_t haystack_len, |
| 270 |
|
| const char *needle, size_t needle_len) |
| 271 |
|
| { |
| 272 |
|
| const char *limit; |
| 273 |
|
| const char *p; |
| 274 |
|
| |
| 275 |
19 |
▶ | if (needle_len == 0 || haystack_len < needle_len) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (needle_len == 0 || haystack_len < needle_len) return NULL; |
19 |
0 |
19 |
| if (needle_len == 0 || haystack_len < needle_len) return NULL; |
0 |
19 |
19 |
|
| 276 |
19 |
| limit = haystack + (haystack_len - needle_len); |
| 277 |
280 |
▶ | for (p = haystack; p <= limit; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = haystack; p <= limit; p++) { |
279 |
1 |
280 |
|
| 278 |
279 |
▶ | if (memcmp(p, needle, needle_len) == 0) return p; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(p, needle, needle_len) == 0) return p; |
18 |
261 |
279 |
|
| 279 |
|
| } |
| 280 |
1 |
| return NULL; |
| 281 |
|
| } |
| 282 |
|
| |
| 283 |
|
| /* Locate PEM body. Returns 0 on success and writes [body_start,body_end) |
| 284 |
|
| * into the out params. Returns negative b64_err_t on failure. */ |
| 285 |
|
| static int |
| 286 |
5 |
| find_pem_body(const char *in, size_t in_len, |
| 287 |
|
| const char **body_start, size_t *body_len, |
| 288 |
|
| size_t *err_offset) |
| 289 |
|
| { |
| 290 |
|
| static const char BEGIN_MARK[] = "-----BEGIN "; |
| 291 |
|
| static const char END_MARK[] = "-----END "; |
| 292 |
|
| static const char DASH5[] = "-----"; |
| 293 |
|
| |
| 294 |
|
| const char *begin; |
| 295 |
|
| const char *begin_label; |
| 296 |
|
| const char *begin_close; |
| 297 |
|
| const char *body; |
| 298 |
|
| const char *end; |
| 299 |
|
| const char *end_label; |
| 300 |
|
| const char *end_close; |
| 301 |
|
| size_t blab_len; |
| 302 |
|
| size_t elab_len; |
| 303 |
|
| |
| 304 |
5 |
| begin = mem_find(in, in_len, BEGIN_MARK, sizeof BEGIN_MARK - 1); |
| 305 |
5 |
▶ | if (!begin) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!begin) { |
0 |
5 |
5 |
|
| 306 |
0 |
▶ | if (err_offset) *err_offset = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = 0; |
0 |
0 |
0 |
|
| 307 |
0 |
| return B64_ERR_PEM_NO_BEGIN; |
| 308 |
|
| } |
| 309 |
5 |
| begin_label = begin + sizeof BEGIN_MARK - 1; |
| 310 |
5 |
| begin_close = mem_find(begin_label, |
| 311 |
5 |
| in_len - (size_t)(begin_label - in), |
| 312 |
|
| DASH5, 5); |
| 313 |
5 |
▶ | if (!begin_close) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!begin_close) { |
0 |
5 |
5 |
|
| 314 |
0 |
▶ | if (err_offset) *err_offset = (size_t)(begin - in); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = (size_t)(begin - in); |
0 |
0 |
0 |
|
| 315 |
0 |
| return B64_ERR_PEM_NO_BEGIN; |
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
|
| /* Body starts right after BEGIN line's trailing dashes. Scan past |
| 319 |
|
| * any immediate \r/\n. */ |
| 320 |
5 |
| body = begin_close + 5; |
| 321 |
10 |
▶ | while (body < in + in_len && (*body == '\r' || *body == '\n')) body++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (body < in + in_len && (*body == '\r' || *body == '\n')) body++; |
10 |
0 |
10 |
| while (body < in + in_len && (*body == '\r' || *body == '\n')) body++; |
0 |
10 |
10 |
| while (body < in + in_len && (*body == '\r' || *body == '\n')) body++; |
5 |
5 |
10 |
|
| 322 |
|
| |
| 323 |
5 |
| end = mem_find(body, in_len - (size_t)(body - in), |
| 324 |
|
| END_MARK, sizeof END_MARK - 1); |
| 325 |
5 |
▶ | if (!end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!end) { |
1 |
4 |
5 |
|
| 326 |
1 |
▶ | if (err_offset) *err_offset = (size_t)(begin - in); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = (size_t)(begin - in); |
1 |
0 |
1 |
|
| 327 |
1 |
| return B64_ERR_PEM_NO_END; |
| 328 |
|
| } |
| 329 |
4 |
| end_label = end + sizeof END_MARK - 1; |
| 330 |
4 |
| end_close = mem_find(end_label, |
| 331 |
4 |
| in_len - (size_t)(end_label - in), |
| 332 |
|
| DASH5, 5); |
| 333 |
4 |
▶ | if (!end_close) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!end_close) { |
0 |
4 |
4 |
|
| 334 |
0 |
▶ | if (err_offset) *err_offset = (size_t)(end - in); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = (size_t)(end - in); |
0 |
0 |
0 |
|
| 335 |
0 |
| return B64_ERR_PEM_NO_END; |
| 336 |
|
| } |
| 337 |
|
| |
| 338 |
|
| /* Verify BEGIN and END labels match. */ |
| 339 |
4 |
| blab_len = (size_t)(begin_close - begin_label); |
| 340 |
4 |
| elab_len = (size_t)(end_close - end_label); |
| 341 |
4 |
▶ | if (blab_len != elab_len |
Branch: 1 branch
| Branch | True | False | Total |
| if (blab_len != elab_len |
4 |
0 |
4 |
|
| 342 |
4 |
▶ | || memcmp(begin_label, end_label, blab_len) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| || memcmp(begin_label, end_label, blab_len) != 0) |
1 |
3 |
4 |
|
| 343 |
|
| { |
| 344 |
1 |
▶ | if (err_offset) *err_offset = (size_t)(end - in); |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = (size_t)(end - in); |
1 |
0 |
1 |
|
| 345 |
1 |
| return B64_ERR_PEM_LABEL; |
| 346 |
|
| } |
| 347 |
|
| |
| 348 |
3 |
| *body_start = body; |
| 349 |
3 |
| *body_len = (size_t)(end - body); |
| 350 |
3 |
| return 0; |
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
|
| int |
| 354 |
34 |
| b64_decode(const char *in, size_t in_len, |
| 355 |
|
| const b64_options_t *opts, |
| 356 |
|
| unsigned char **out, size_t *out_cap, size_t *out_len, |
| 357 |
|
| size_t *err_offset) |
| 358 |
|
| { |
| 359 |
|
| const signed char *rev; |
| 360 |
|
| const char *body; |
| 361 |
|
| size_t body_len; |
| 362 |
|
| unsigned char *o; |
| 363 |
34 |
| uint32_t buf = 0; |
| 364 |
34 |
| int bits = 0; |
| 365 |
34 |
| int saw_pad = 0; |
| 366 |
|
| size_t i; |
| 367 |
|
| |
| 368 |
34 |
▶ | if (!rev_initialised) rev_init(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rev_initialised) rev_init(); |
1 |
33 |
34 |
|
| 369 |
34 |
▶ | if (err_offset) *err_offset = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = 0; |
34 |
0 |
34 |
|
| 370 |
|
| |
| 371 |
34 |
▶ | rev = opts->urlsafe ? REV_URL : REV_STD; |
Branch: 1 branch
| Branch | True | False | Total |
| rev = opts->urlsafe ? REV_URL : REV_STD; |
2 |
32 |
34 |
|
| 372 |
|
| |
| 373 |
|
| /* PEM: peel envelope. */ |
| 374 |
34 |
| body = in; |
| 375 |
34 |
| body_len = in_len; |
| 376 |
34 |
▶ | if (opts->pem) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->pem) { |
5 |
29 |
34 |
|
| 377 |
5 |
| int rc = find_pem_body(in, in_len, &body, &body_len, err_offset); |
| 378 |
5 |
▶ | if (rc) return rc; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc) return rc; |
2 |
3 |
5 |
|
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
|
| /* Worst-case decode size: 3 bytes per 4 input chars. */ |
| 382 |
32 |
▶ | if (out_reserve((void **)out, out_cap, |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_reserve((void **)out, out_cap, |
0 |
32 |
32 |
|
| 383 |
32 |
| *out_len + (body_len / 4 + 1) * 3) < 0) |
| 384 |
0 |
| return B64_ERR_NOMEM; |
| 385 |
|
| |
| 386 |
32 |
| o = *out + *out_len; |
| 387 |
|
| |
| 388 |
138 |
▶ | for (i = 0; i < body_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < body_len; i++) { |
113 |
25 |
138 |
|
| 389 |
|
| unsigned char c; |
| 390 |
|
| signed char v; |
| 391 |
|
| |
| 392 |
|
| /* Fast path: at a quartet boundary with nothing pending, four |
| 393 |
|
| * alphabet bytes are three output bytes. A byte outside the |
| 394 |
|
| * alphabet (a line end, padding, noise) fails the sign test, and |
| 395 |
|
| * from it on the careful step below runs, which decides every |
| 396 |
|
| * error, offset and lenient skip as it always has. */ |
| 397 |
113 |
▶ | if (bits == 0 && !saw_pad) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (bits == 0 && !saw_pad) { |
54 |
59 |
113 |
| if (bits == 0 && !saw_pad) { |
54 |
0 |
54 |
|
| 398 |
197256 |
▶ | while (i + 4 <= body_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i + 4 <= body_len) { |
197244 |
12 |
197256 |
|
| 399 |
197244 |
| int v0 = rev[(unsigned char)body[i]]; |
| 400 |
197244 |
| int v1 = rev[(unsigned char)body[i + 1]]; |
| 401 |
197244 |
| int v2 = rev[(unsigned char)body[i + 2]]; |
| 402 |
197244 |
| int v3 = rev[(unsigned char)body[i + 3]]; |
| 403 |
|
| uint32_t q; |
| 404 |
197244 |
▶ | if ((v0 | v1 | v2 | v3) < 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v0 | v1 | v2 | v3) < 0) break; |
42 |
197202 |
197244 |
|
| 405 |
197202 |
| q = ((uint32_t)v0 << 18) | ((uint32_t)v1 << 12) |
| 406 |
197202 |
| | ((uint32_t)v2 << 6) | (uint32_t)v3; |
| 407 |
197202 |
| o[0] = (unsigned char)(q >> 16); |
| 408 |
197202 |
| o[1] = (unsigned char)(q >> 8); |
| 409 |
197202 |
| o[2] = (unsigned char) q; |
| 410 |
197202 |
| o += 3; |
| 411 |
197202 |
| i += 4; |
| 412 |
|
| } |
| 413 |
54 |
▶ | if (i >= body_len) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i >= body_len) break; |
5 |
49 |
54 |
|
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
|
| /* The careful step: one byte. */ |
| 417 |
108 |
| c = (unsigned char)body[i]; |
| 418 |
108 |
| v = rev[c]; |
| 419 |
|
| |
| 420 |
108 |
▶ | if (v == PAD) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v == PAD) { |
28 |
80 |
108 |
|
| 421 |
28 |
| saw_pad++; |
| 422 |
28 |
| continue; |
| 423 |
|
| } |
| 424 |
80 |
▶ | if (v == INVAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v == INVAL) { |
34 |
46 |
80 |
|
| 425 |
34 |
▶ | if (opts->strict) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts->strict) { |
2 |
32 |
34 |
|
| 426 |
2 |
▶ | if (err_offset) *err_offset = |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = |
2 |
0 |
2 |
|
| 427 |
2 |
| (size_t)(body - in) + i; |
| 428 |
2 |
| return B64_ERR_BAD_BYTE; |
| 429 |
|
| } |
| 430 |
|
| /* Lenient: skip silently. */ |
| 431 |
32 |
| continue; |
| 432 |
|
| } |
| 433 |
46 |
▶ | if (saw_pad && opts->strict) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (saw_pad && opts->strict) { |
0 |
46 |
46 |
| if (saw_pad && opts->strict) { |
0 |
0 |
0 |
|
| 434 |
0 |
▶ | if (err_offset) *err_offset = (size_t)(body - in) + i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err_offset) *err_offset = (size_t)(body - in) + i; |
0 |
0 |
0 |
|
| 435 |
0 |
| return B64_ERR_BAD_BYTE; /* alphabet byte after padding */ |
| 436 |
|
| } |
| 437 |
|
| |
| 438 |
46 |
| buf = (buf << 6) | (uint32_t)v; |
| 439 |
46 |
| bits += 6; |
| 440 |
46 |
▶ | if (bits >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bits >= 8) { |
26 |
20 |
46 |
|
| 441 |
26 |
| bits -= 8; |
| 442 |
26 |
| *o++ = (unsigned char)((buf >> bits) & 0xFF); |
| 443 |
|
| } |
| 444 |
|
| } |
| 445 |
|
| |
| 446 |
|
| /* Final bits: if exactly 8 bits remain we accept (one trailing |
| 447 |
|
| * byte). 0 bits is fine. Anything else is truncation. */ |
| 448 |
30 |
▶ | if (bits != 0 && bits != 2 && bits != 4) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (bits != 0 && bits != 2 && bits != 4) { |
20 |
10 |
30 |
| if (bits != 0 && bits != 2 && bits != 4) { |
13 |
7 |
20 |
| if (bits != 0 && bits != 2 && bits != 4) { |
1 |
12 |
13 |
|
| 449 |
|
| /* 6 bits left over means an incomplete quartet (e.g. one |
| 450 |
|
| * stray base64 char with no partner). 2/4 bits are normal |
| 451 |
|
| * tails for length%3 == 1 / length%3 == 2. */ |
| 452 |
1 |
| return B64_ERR_TRUNCATED; |
| 453 |
|
| } |
| 454 |
|
| |
| 455 |
29 |
| *out_len = (size_t)(o - *out); |
| 456 |
29 |
| return B64_OK; |
| 457 |
|
| } |
| 458 |
|
| |
| 459 |
|
| /* ------------------------------------------------------------ |
| 460 |
|
| * strerror |
| 461 |
|
| * ------------------------------------------------------------ */ |
| 462 |
|
| |
| 463 |
|
| const char * |
| 464 |
5 |
| b64_strerror(int err) |
| 465 |
|
| { |
| 466 |
5 |
| switch ((b64_err_t)err) { |
| 467 |
0 |
| case B64_OK: return "ok"; |
| 468 |
0 |
| case B64_ERR_NOMEM: return "out of memory"; |
| 469 |
2 |
| case B64_ERR_BAD_BYTE: return "non-alphabet byte in strict mode"; |
| 470 |
1 |
| case B64_ERR_TRUNCATED: return "truncated base64 input"; |
| 471 |
0 |
| case B64_ERR_PEM_NO_BEGIN: return "PEM input missing BEGIN marker"; |
| 472 |
1 |
| case B64_ERR_PEM_NO_END: return "PEM input missing END marker"; |
| 473 |
1 |
| case B64_ERR_PEM_LABEL: return "PEM BEGIN and END labels disagree"; |
| 474 |
|
| } |
| 475 |
0 |
| return "unknown error"; |
| 476 |
|
| } |