b64.c

filter: Statementstmt 92.3% 170 / 184 Branchbran 64.8% 131 / 202 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.9% 301 / 386 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary Base64.xs »
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 92 out_reserve(void **out, size_t *out_cap, size_t need)
57 {
58 size_t new_cap;
59 void *new_buf;
60  
61 92 ▶ if (need <= *out_cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (need <= *out_cap) return 0; 26 66 92
62  
63 66 ▶ new_cap = *out_cap ? *out_cap : 64;
Branch: 1 branch
BranchTrueFalseTotal
new_cap = *out_cap ? *out_cap : 64; 4 62 66
64 156 ▶ while (new_cap < need) {
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < need) { 90 66 156
65 90 ▶ if (new_cap > (SIZE_MAX / 2)) { new_cap = need; break; }
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 23 emit_eol(char **out, size_t *cap, size_t *len, const char *eol)
95 {
96 23 size_t elen = strlen(eol);
97 23 ▶ if (out_reserve((void **)out, cap, *len + elen) < 0) return B64_ERR_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (out_reserve((void **)out, cap, *len + elen) < 0) return B64_ERR_NOMEM; 0 23 23
98 23 memcpy(*out + *len, eol, elen);
99 23 *len += elen;
100 23 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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
if (!rev_initialised) rev_init(); 7 26 33
144 33 ▶ if (err_offset) *err_offset = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = 0; 33 0 33
145  
146 33 ▶ alpha = opts->urlsafe ? ALPHA_URL : ALPHA_STD;
Branch: 1 branch
BranchTrueFalseTotal
alpha = opts->urlsafe ? ALPHA_URL : ALPHA_STD; 3 30 33
147 33 ▶ eol = opts->eol ? opts->eol : "\n";
Branch: 1 branch
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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
BranchTrueFalseTotal
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 /* Main loop: 3 bytes -> 4 chars. */
182 197430 ▶ for (i = 0; i + 3 <= in_len; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 3 <= in_len; i += 3) { 197397 33 197430
183 197397 uint32_t triplet = ((uint32_t)in[i] << 16)
184 197397 | ((uint32_t)in[i+1] << 8)
185 197397 | (uint32_t)in[i+2];
186 197397 ▶ EMIT(alpha[(triplet >> 18) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(triplet >> 18) & 0x3F]); 306 197091 197397
EMIT(alpha[(triplet >> 18) & 0x3F]); 0 306 306
EMIT(alpha[(triplet >> 18) & 0x3F]); 0 0 0
187 197397 ▶ EMIT(alpha[(triplet >> 12) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(triplet >> 12) & 0x3F]); 306 197091 197397
EMIT(alpha[(triplet >> 12) & 0x3F]); 0 306 306
EMIT(alpha[(triplet >> 12) & 0x3F]); 0 0 0
188 197397 ▶ EMIT(alpha[(triplet >> 6) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(triplet >> 6) & 0x3F]); 306 197091 197397
EMIT(alpha[(triplet >> 6) & 0x3F]); 0 306 306
EMIT(alpha[(triplet >> 6) & 0x3F]); 0 0 0
189 197397 ▶ EMIT(alpha[ triplet & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[ triplet & 0x3F]); 306 197091 197397
EMIT(alpha[ triplet & 0x3F]); 17 289 306
EMIT(alpha[ triplet & 0x3F]); 0 17 17
190 }
191  
192 /* Tail: 1 or 2 leftover bytes. */
193 33 tail = in_len - i;
194 33 ▶ if (tail == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (tail == 1) { 13 20 33
195 13 uint32_t b = (uint32_t)in[i] << 16;
196 13 ▶ EMIT(alpha[(b >> 18) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(b >> 18) & 0x3F]); 2 11 13
EMIT(alpha[(b >> 18) & 0x3F]); 0 2 2
EMIT(alpha[(b >> 18) & 0x3F]); 0 0 0
197 13 ▶ EMIT(alpha[(b >> 12) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(b >> 12) & 0x3F]); 2 11 13
EMIT(alpha[(b >> 12) & 0x3F]); 0 2 2
EMIT(alpha[(b >> 12) & 0x3F]); 0 0 0
198 13 ▶ if (opts->padding) { EMIT('='); EMIT('='); }
Branch: 7 branches
BranchTrueFalseTotal
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
199 20 ▶ } else if (tail == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (tail == 2) { 10 10 20
200 10 uint32_t b = ((uint32_t)in[i] << 16) | ((uint32_t)in[i+1] << 8);
201 10 ▶ EMIT(alpha[(b >> 18) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(b >> 18) & 0x3F]); 4 6 10
EMIT(alpha[(b >> 18) & 0x3F]); 0 4 4
EMIT(alpha[(b >> 18) & 0x3F]); 0 0 0
202 10 ▶ EMIT(alpha[(b >> 12) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(b >> 12) & 0x3F]); 4 6 10
EMIT(alpha[(b >> 12) & 0x3F]); 0 4 4
EMIT(alpha[(b >> 12) & 0x3F]); 0 0 0
203 10 ▶ EMIT(alpha[(b >> 6) & 0x3F]);
Branch: 3 branches
BranchTrueFalseTotal
EMIT(alpha[(b >> 6) & 0x3F]); 4 6 10
EMIT(alpha[(b >> 6) & 0x3F]); 0 4 4
EMIT(alpha[(b >> 6) & 0x3F]); 0 0 0
204 10 ▶ if (opts->padding) EMIT('=');
Branch: 4 branches
BranchTrueFalseTotal
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
205 }
206  
207 #undef EMIT
208  
209 /* Final EOL after the body if we wrapped (so the trailing line
210 * terminates) and no eol is already pending - wrap loop only emits
211 * an eol when it just hit `wrap_col == wrap`, so a partial last
212 * line still needs one. PEM mode also wants an EOL before END. */
213 33 *out_len = (size_t)(o - *out);
214 33 ▶ if ((wrap > 0 && wrap_col != 0) || opts->pem) {
Branch: 3 branches
BranchTrueFalseTotal
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
215 6 rc = emit_eol(out, out_cap, out_len, eol);
216 6 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 0 6 6
217 }
218  
219 33 ▶ if (opts->pem) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->pem) { 2 31 33
220 2 rc = emit_pem_header(out, out_cap, out_len,
221 2 "END", opts->pem_label, eol);
222 2 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 0 2 2
223 }
224  
225 33 return B64_OK;
226 }
227  
228 /* ------------------------------------------------------------
229 * Decode
230 *
231 * On PEM input, find the BEGIN/END markers and slice out the body
232 * before running the regular decode loop on it.
233 * ------------------------------------------------------------ */
234  
235 /* Find a substring in [haystack, haystack+haystack_len). NULL if not
236 * found. Standard memmem isn't portable, so roll our own. */
237 static const char *
238 19 mem_find(const char *haystack, size_t haystack_len,
239 const char *needle, size_t needle_len)
240 {
241 const char *limit;
242 const char *p;
243  
244 19 ▶ if (needle_len == 0 || haystack_len < needle_len) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
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
245 19 limit = haystack + (haystack_len - needle_len);
246 280 ▶ for (p = haystack; p <= limit; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = haystack; p <= limit; p++) { 279 1 280
247 279 ▶ if (memcmp(p, needle, needle_len) == 0) return p;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p, needle, needle_len) == 0) return p; 18 261 279
248 }
249 1 return NULL;
250 }
251  
252 /* Locate PEM body. Returns 0 on success and writes [body_start,body_end)
253 * into the out params. Returns negative b64_err_t on failure. */
254 static int
255 5 find_pem_body(const char *in, size_t in_len,
256 const char **body_start, size_t *body_len,
257 size_t *err_offset)
258 {
259 static const char BEGIN_MARK[] = "-----BEGIN ";
260 static const char END_MARK[] = "-----END ";
261 static const char DASH5[] = "-----";
262  
263 const char *begin;
264 const char *begin_label;
265 const char *begin_close;
266 const char *body;
267 const char *end;
268 const char *end_label;
269 const char *end_close;
270 size_t blab_len;
271 size_t elab_len;
272  
273 5 begin = mem_find(in, in_len, BEGIN_MARK, sizeof BEGIN_MARK - 1);
274 5 ▶ if (!begin) {
Branch: 1 branch
BranchTrueFalseTotal
if (!begin) { 0 5 5
275 0 ▶ if (err_offset) *err_offset = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = 0; 0 0 0
276 0 return B64_ERR_PEM_NO_BEGIN;
277 }
278 5 begin_label = begin + sizeof BEGIN_MARK - 1;
279 5 begin_close = mem_find(begin_label,
280 5 in_len - (size_t)(begin_label - in),
281 DASH5, 5);
282 5 ▶ if (!begin_close) {
Branch: 1 branch
BranchTrueFalseTotal
if (!begin_close) { 0 5 5
283 0 ▶ if (err_offset) *err_offset = (size_t)(begin - in);
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = (size_t)(begin - in); 0 0 0
284 0 return B64_ERR_PEM_NO_BEGIN;
285 }
286  
287 /* Body starts right after BEGIN line's trailing dashes. Scan past
288 * any immediate \r/\n. */
289 5 body = begin_close + 5;
290 10 ▶ while (body < in + in_len && (*body == '\r' || *body == '\n')) body++;
Branch: 3 branches
BranchTrueFalseTotal
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
291  
292 5 end = mem_find(body, in_len - (size_t)(body - in),
293 END_MARK, sizeof END_MARK - 1);
294 5 ▶ if (!end) {
Branch: 1 branch
BranchTrueFalseTotal
if (!end) { 1 4 5
295 1 ▶ if (err_offset) *err_offset = (size_t)(begin - in);
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = (size_t)(begin - in); 1 0 1
296 1 return B64_ERR_PEM_NO_END;
297 }
298 4 end_label = end + sizeof END_MARK - 1;
299 4 end_close = mem_find(end_label,
300 4 in_len - (size_t)(end_label - in),
301 DASH5, 5);
302 4 ▶ if (!end_close) {
Branch: 1 branch
BranchTrueFalseTotal
if (!end_close) { 0 4 4
303 0 ▶ if (err_offset) *err_offset = (size_t)(end - in);
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = (size_t)(end - in); 0 0 0
304 0 return B64_ERR_PEM_NO_END;
305 }
306  
307 /* Verify BEGIN and END labels match. */
308 4 blab_len = (size_t)(begin_close - begin_label);
309 4 elab_len = (size_t)(end_close - end_label);
310 4 ▶ if (blab_len != elab_len
Branch: 1 branch
BranchTrueFalseTotal
if (blab_len != elab_len 4 0 4
311 4 ▶ || memcmp(begin_label, end_label, blab_len) != 0)
Branch: 1 branch
BranchTrueFalseTotal
|| memcmp(begin_label, end_label, blab_len) != 0) 1 3 4
312 {
313 1 ▶ if (err_offset) *err_offset = (size_t)(end - in);
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = (size_t)(end - in); 1 0 1
314 1 return B64_ERR_PEM_LABEL;
315 }
316  
317 3 *body_start = body;
318 3 *body_len = (size_t)(end - body);
319 3 return 0;
320 }
321  
322 int
323 34 b64_decode(const char *in, size_t in_len,
324 const b64_options_t *opts,
325 unsigned char **out, size_t *out_cap, size_t *out_len,
326 size_t *err_offset)
327 {
328 const signed char *rev;
329 const char *body;
330 size_t body_len;
331 unsigned char *o;
332 34 uint32_t buf = 0;
333 34 int bits = 0;
334 34 int saw_pad = 0;
335 size_t i;
336  
337 34 ▶ if (!rev_initialised) rev_init();
Branch: 1 branch
BranchTrueFalseTotal
if (!rev_initialised) rev_init(); 1 33 34
338 34 ▶ if (err_offset) *err_offset = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = 0; 34 0 34
339  
340 34 ▶ rev = opts->urlsafe ? REV_URL : REV_STD;
Branch: 1 branch
BranchTrueFalseTotal
rev = opts->urlsafe ? REV_URL : REV_STD; 2 32 34
341  
342 /* PEM: peel envelope. */
343 34 body = in;
344 34 body_len = in_len;
345 34 ▶ if (opts->pem) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->pem) { 5 29 34
346 5 int rc = find_pem_body(in, in_len, &body, &body_len, err_offset);
347 5 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 2 3 5
348 }
349  
350 /* Worst-case decode size: 3 bytes per 4 input chars. */
351 32 ▶ if (out_reserve((void **)out, out_cap,
Branch: 1 branch
BranchTrueFalseTotal
if (out_reserve((void **)out, out_cap, 0 32 32
352 32 *out_len + (body_len / 4 + 1) * 3) < 0)
353 0 return B64_ERR_NOMEM;
354  
355 32 o = *out + *out_len;
356  
357 788946 ▶ for (i = 0; i < body_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < body_len; i++) { 788916 30 788946
358 788916 unsigned char c = (unsigned char)body[i];
359 788916 signed char v = rev[c];
360  
361 788916 ▶ if (v == PAD) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == PAD) { 28 788888 788916
362 28 saw_pad++;
363 28 continue;
364 }
365 788888 ▶ if (v == INVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (v == INVAL) { 34 788854 788888
366 34 ▶ if (opts->strict) {
Branch: 1 branch
BranchTrueFalseTotal
if (opts->strict) { 2 32 34
367 2 ▶ if (err_offset) *err_offset =
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = 2 0 2
368 2 (size_t)(body - in) + i;
369 2 return B64_ERR_BAD_BYTE;
370 }
371 /* Lenient: skip silently. */
372 32 continue;
373 }
374 788854 ▶ if (saw_pad && opts->strict) {
Branch: 2 branches
BranchTrueFalseTotal
if (saw_pad && opts->strict) { 0 788854 788854
if (saw_pad && opts->strict) { 0 0 0
375 0 ▶ if (err_offset) *err_offset = (size_t)(body - in) + i;
Branch: 1 branch
BranchTrueFalseTotal
if (err_offset) *err_offset = (size_t)(body - in) + i; 0 0 0
376 0 return B64_ERR_BAD_BYTE; /* alphabet byte after padding */
377 }
378  
379 788854 buf = (buf << 6) | (uint32_t)v;
380 788854 bits += 6;
381 788854 ▶ if (bits >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits >= 8) { 591632 197222 788854
382 591632 bits -= 8;
383 591632 *o++ = (unsigned char)((buf >> bits) & 0xFF);
384 }
385 }
386  
387 /* Final bits: if exactly 8 bits remain we accept (one trailing
388 * byte). 0 bits is fine. Anything else is truncation. */
389 30 ▶ if (bits != 0 && bits != 2 && bits != 4) {
Branch: 3 branches
BranchTrueFalseTotal
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
390 /* 6 bits left over means an incomplete quartet (e.g. one
391 * stray base64 char with no partner). 2/4 bits are normal
392 * tails for length%3 == 1 / length%3 == 2. */
393 1 return B64_ERR_TRUNCATED;
394 }
395  
396 29 *out_len = (size_t)(o - *out);
397 29 return B64_OK;
398 }
399  
400 /* ------------------------------------------------------------
401 * strerror
402 * ------------------------------------------------------------ */
403  
404 const char *
405 5 b64_strerror(int err)
406 {
407 5 switch ((b64_err_t)err) {
408 0 case B64_OK: return "ok";
409 0 case B64_ERR_NOMEM: return "out of memory";
410 2 case B64_ERR_BAD_BYTE: return "non-alphabet byte in strict mode";
411 1 case B64_ERR_TRUNCATED: return "truncated base64 input";
412 0 case B64_ERR_PEM_NO_BEGIN: return "PEM input missing BEGIN marker";
413 1 case B64_ERR_PEM_NO_END: return "PEM input missing END marker";
414 1 case B64_ERR_PEM_LABEL: return "PEM BEGIN and END labels disagree";
415 }
416 0 return "unknown error";
417 }
↑ Summary Base64.xs »