Offer a simple coloured display..
[s-mailx.git] / mime_cte.c
blobbf0cdd6856a1637f03ea0cfffc85b71f2c137a08
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Content-Transfer-Encodings as defined in RFC 2045:
3 *@ - Quoted-Printable, section 6.7
4 *@ - Base64, section 6.8
6 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
8 */
9 /* QP quoting idea, _b64_decode(), b64_encode() taken from NetBSDs mailx(1): */
10 /* $NetBSD: mime_codecs.c,v 1.9 2009/04/10 13:08:25 christos Exp $ */
12 * Copyright (c) 2006 The NetBSD Foundation, Inc.
13 * All rights reserved.
15 * This code is derived from software contributed to The NetBSD Foundation
16 * by Anon Ymous.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 enum _qact {
45 N = 0, /* Do not quote */
46 Q = 1, /* Must quote */
47 SP = 2, /* sp */
48 XF = 3, /* Special character 'F' - maybe quoted */
49 XD = 4, /* Special character '.' - maybe quoted */
50 US = '_', /* In header, special character ' ' quoted as '_' */
51 QM = '?', /* In header, special character ? not always quoted */
52 EQ = Q, /* '=' must be quoted */
53 TB = SP, /* Treat '\t' as a space */
54 NL = N, /* Don't quote '\n' (NL) */
55 CR = Q /* Always quote a '\r' (CR) */
58 /* Lookup tables to decide wether a character must be encoded or not.
59 * Email header differences according to RFC 2047, section 4.2:
60 * - also quote SP (as the underscore _), TAB, ?, _, CR, LF
61 * - don't care about the special ^F[rom] and ^.$ */
62 static ui8_t const _qtab_body[] = {
63 Q, Q, Q, Q, Q, Q, Q, Q, Q,TB,NL, Q, Q,CR, Q, Q,
64 Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q,
65 SP, N, N, N, N, N, N, N, N, N, N, N, N, N,XD, N,
66 N, N, N, N, N, N, N, N, N, N, N, N, N,EQ, N, N,
68 N, N, N, N, N, N,XF, N, N, N, N, N, N, N, N, N,
69 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
70 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
71 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, Q,
73 _qtab_head[] = {
74 Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q,
75 Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q,
76 US, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
77 N, N, N, N, N, N, N, N, N, N, N, N, N,EQ, N,QM,
79 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
80 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, Q,
81 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
82 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, Q,
85 /* Check wether **s* must be quoted according to *ishead*, else body rules;
86 * *sol* indicates wether we are at the first character of a line/field */
87 SINLINE enum _qact _mustquote(char const *s, char const *e, bool_t sol,
88 bool_t ishead);
90 /* Convert c to/from a hexadecimal character string */
91 SINLINE char * _qp_ctohex(char *store, char c);
92 SINLINE si32_t _qp_cfromhex(char const *hex);
94 /* Trim WS and make *work* point to the decodable range of *in*.
95 * Return the amount of bytes a b64_decode operation on that buffer requires */
96 static size_t _b64_decode_prepare(struct str *work,
97 struct str const *in);
99 /* Perform b64_decode on sufficiently spaced & multiple-of-4 base *in*put.
100 * Return number of useful bytes in *out* or -1 on error */
101 static ssize_t _b64_decode(struct str *out, struct str *in);
103 SINLINE enum _qact
104 _mustquote(char const *s, char const *e, bool_t sol, bool_t ishead)
106 ui8_t const *qtab = ishead ? _qtab_head : _qtab_body;
107 enum _qact a = ((ui8_t)*s > 0x7F) ? Q : qtab[(ui8_t)*s], r;
109 if ((r = a) == N || (r = a) == Q)
110 goto jleave;
111 r = Q;
113 /* Special header fields */
114 if (ishead) {
115 /* ' ' -> '_' */
116 if (a == US) {
117 r = US;
118 goto jleave;
120 /* Treat '?' only special if part of '=?' and '?=' (still to much quoting
121 * since it's '=?CHARSET?CTE?stuff?=', and especially the trailing ?=
122 * should be hard too match */
123 if (a == QM && ((! sol && s[-1] == '=') || (s < e && s[1] == '=')))
124 goto jleave;
125 goto jnquote;
128 /* Body-only */
130 if (a == SP) {
131 /* WS only if trailing white space */
132 if (s + 1 == e || s[1] == '\n')
133 goto jleave;
134 goto jnquote;
137 /* Rest are special begin-of-line cases */
138 if (!sol)
139 goto jnquote;
141 /* ^From */
142 if (a == XF) {
143 if (s + 4 < e && s[1] == 'r' && s[2] == 'o' && s[3] == 'm')
144 goto jleave;
145 goto jnquote;
147 /* ^.$ */
148 if (a == XD && (s + 1 == e || s[1] == '\n'))
149 goto jleave;
150 jnquote:
151 r = N;
152 jleave:
153 return r;
156 SINLINE char *
157 _qp_ctohex(char *store, char c)
159 static char const hexmap[] = "0123456789ABCDEF";
161 store[2] = '\0';
162 store[1] = hexmap[(ui8_t)c & 0x0F];
163 c = ((ui8_t)c >> 4) & 0x0F;
164 store[0] = hexmap[(ui8_t)c];
165 return store;
168 SINLINE si32_t
169 _qp_cfromhex(char const *hex)
171 /* Be robust, allow lowercase hexadecimal letters, too */
172 static ui8_t const atoi16[] = {
173 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 0x30-0x37 */
174 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x38-0x3F */
175 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, /* 0x40-0x47 */
176 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x48-0x4f */
177 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x50-0x57 */
178 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x58-0x5f */
179 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF /* 0x60-0x67 */
181 ui8_t i1, i2;
182 si32_t r;
184 if ((i1 = (ui8_t)hex[0] - '0') >= NELEM(atoi16) ||
185 (i2 = (ui8_t)hex[1] - '0') >= NELEM(atoi16))
186 goto jerr;
187 i1 = atoi16[i1];
188 i2 = atoi16[i2];
189 if ((i1 | i2) & 0xF0)
190 goto jerr;
191 r = i1;
192 r <<= 4;
193 r += i2;
194 jleave:
195 return r;
196 jerr:
197 r = -1;
198 goto jleave;
201 static size_t
202 _b64_decode_prepare(struct str *work, struct str const *in)
204 char *cp = in->s;
205 size_t cp_len = in->l;
207 while (cp_len > 0 && spacechar(*cp))
208 ++cp, --cp_len;
209 work->s = cp;
211 for (cp += cp_len; cp_len > 0; --cp_len) {
212 char c = *--cp;
213 if (!spacechar(c))
214 break;
216 work->l = cp_len;
218 if (cp_len > 16)
219 cp_len = ((cp_len * 3) >> 2) + (cp_len >> 3);
220 return cp_len;
223 static ssize_t
224 _b64_decode(struct str *out, struct str *in)
226 static signed char const b64index[] = {
227 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
228 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
229 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
230 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-2,-1,-1,
231 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
232 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
233 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
234 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
236 #define EQU (ui32_t)-2
237 #define BAD (ui32_t)-1
238 #define uchar64(c) ((c) >= sizeof(b64index) ? BAD : (ui32_t)b64index[(c)])
240 ssize_t ret = -1;
241 ui8_t *p = (ui8_t*)out->s;
242 ui8_t const *q = (ui8_t const*)in->s, *end;
244 out->l = 0;
246 for (end = q + in->l; q + 4 <= end; q += 4) {
247 ui32_t a = uchar64(q[0]), b = uchar64(q[1]), c = uchar64(q[2]),
248 d = uchar64(q[3]);
250 if (a >= EQU || b >= EQU || c == BAD || d == BAD)
251 goto jleave;
253 *p++ = ((a << 2) | ((b & 0x30) >> 4));
254 if (c == EQU) { /* got '=' */
255 if (d != EQU)
256 goto jleave;
257 break;
259 *p++ = (((b & 0x0f) << 4) | ((c & 0x3c) >> 2));
260 if (d == EQU) /* got '=' */
261 break;
262 *p++ = (((c & 0x03) << 6) | d);
264 #undef uchar64
265 #undef EQU
266 #undef BAD
268 ret = PTR2SIZE((char*)p - out->s);
269 out->l = (size_t)ret;
270 jleave:
271 in->l -= PTR2SIZE((char*)UNCONST(q) - in->s);
272 in->s = UNCONST(q);
273 return ret;
276 FL size_t
277 mime_cte_mustquote(char const *ln, size_t lnlen, bool_t ishead)
279 size_t ret;
280 bool_t sol;
282 for (ret = 0, sol = TRU1; lnlen > 0; sol = FAL0, ++ln, --lnlen)
283 ret += (_mustquote(ln, ln + lnlen, sol, ishead) != N);
284 return ret;
287 FL size_t
288 qp_encode_calc_size(size_t len)
290 /* Worst case: 'CRLF' -> '=0D=0A=\n\0' */
291 len = (len * 3) + 1/* soft NL */ + 1/* visual NL */ + 1/* NUL */;
292 return len;
295 #ifdef notyet
296 FL struct str *
297 qp_encode_cp(struct str *out, char const *cp, enum qpflags flags)
299 struct str in;
300 in.s = UNCONST(cp);
301 in.l = strlen(cp);
302 return qp_encode(out, &in, flags);
305 FL struct str *
306 qp_encode_buf(struct str *out, void const *vp, size_t vp_len,
307 enum qpflags flags)
309 struct str in;
310 in.s = UNCONST(vp);
311 in.l = vp_len;
312 return qp_encode(out, &in, flags);
314 #endif /* notyet */
316 FL struct str *
317 qp_encode(struct str *out, struct str const *in, enum qpflags flags)
319 bool_t sol = (flags & QP_ISHEAD ? FAL0 : TRU1), seenx;
320 ssize_t lnlen;
321 char *qp;
322 char const *is, *ie;
324 if ((flags & QP_BUF) == 0) {
325 lnlen = qp_encode_calc_size(in->l);
326 out->s = (flags & QP_SALLOC) ? salloc(lnlen) : srealloc(out->s, lnlen);
328 qp = out->s;
329 is = in->s;
330 ie = is + in->l;
332 /* QP_ISHEAD? */
333 if (!sol) {
334 for (seenx = FAL0, sol = TRU1; is < ie; sol = FAL0, ++qp) {
335 enum _qact mq = _mustquote(is, ie, sol, TRU1);
336 char c = *is++;
338 if (mq == N) {
339 /* We convert into a single *encoded-word*, that'll end up in
340 * =?C?Q??=; quote '?' from when we're inside there on */
341 if (seenx && c == '?')
342 goto jheadq;
343 *qp = c;
344 } else if (mq == US)
345 *qp = US;
346 else {
347 seenx = TRU1;
348 jheadq:
349 *qp++ = '=';
350 qp = _qp_ctohex(qp, c) + 1;
353 goto jleave;
356 /* The body needs to take care for soft line breaks etc. */
357 for (lnlen = 0, seenx = FAL0; is < ie; sol = FAL0) {
358 enum _qact mq = _mustquote(is, ie, sol, FAL0);
359 char c = *is++;
361 if (mq == N && (c != '\n' || !seenx)) {
362 *qp++ = c;
363 if (++lnlen < QP_LINESIZE - 1)
364 continue;
365 /* Don't write a soft line break when we're in the last possible
366 * column and either an LF has been written or only an LF follows, as
367 * that'll end the line anyway */
368 /* XXX but - ensure is+1>=ie, then??
369 * xxx and/or - what about resetting lnlen; that contra
370 * xxx dicts input==1 input line assertion, though */
371 if (c == '\n' || is == ie || *is == '\n')
372 continue;
373 jsoftnl:
374 qp[0] = '=';
375 qp[1] = '\n';
376 qp += 2;
377 lnlen = 0;
378 continue;
381 if (lnlen > QP_LINESIZE - 3 - 1) {
382 qp[0] = '=';
383 qp[1] = '\n';
384 qp += 2;
385 lnlen = 0;
387 *qp++ = '=';
388 qp = _qp_ctohex(qp, c);
389 qp += 2;
390 lnlen += 3;
391 if (c != '\n' || !seenx)
392 seenx = (c == '\r');
393 else {
394 seenx = FAL0;
395 goto jsoftnl;
399 /* Enforce soft line break if we haven't seen LF */
400 if (in->l > 0 && *--is != '\n') {
401 qp[0] = '=';
402 qp[1] = '\n';
403 qp += 2;
405 jleave:
406 out->l = PTR2SIZE(qp - out->s);
407 out->s[out->l] = '\0';
408 return out;
411 FL int
412 qp_decode(struct str *out, struct str const *in, struct str *rest)
414 int ret = STOP;
415 char *os, *oc;
416 char const *is, *ie;
418 if (rest != NULL && rest->l != 0) {
419 os = out->s;
420 *out = *rest;
421 rest->s = os;
422 rest->l = 0;
425 oc = os =
426 out->s = srealloc(out->s, out->l + in->l + 3);
427 oc += out->l;
428 is = in->s;
429 ie = is + in->l;
431 /* Decoding encoded-word (RFC 2049) in a header field? */
432 if (rest == NULL) {
433 while (PTRCMP(is, <, ie)) {
434 si32_t c = *is++;
435 if (c == '=') {
436 if (PTRCMP(is + 1, >=, ie)) {
437 ++is;
438 goto jehead;
440 c = _qp_cfromhex(is);
441 is += 2;
442 if (c >= 0)
443 *oc++ = (char)c;
444 else {
445 /* Illegal according to RFC 2045, section 6.7. Almost follow */
446 jehead:
447 /* TODO 0xFFFD
448 *oc[0] = '['; oc[1] = '?'; oc[2] = ']';
449 *oc += 3; 0xFFFD TODO
450 */ *oc++ = '?';
452 } else
453 *oc++ = (c == '_') ? ' ' : (char)c;
455 goto jleave; /* XXX QP decode, header: errors not reported */
458 /* Decoding a complete message/mimepart body line */
459 while (PTRCMP(is, <, ie)) {
460 si32_t c = *is++;
461 if (c != '=') {
462 *oc++ = (char)c;
463 continue;
466 /* RFC 2045, 6.7:
467 * Therefore, when decoding a Quoted-Printable body, any
468 * trailing white space on a line must be deleted, as it will
469 * necessarily have been added by intermediate transport
470 * agents */
471 for (; PTRCMP(is, <, ie) && blankchar(*is); ++is)
473 if (PTRCMP(is + 1, >=, ie)) {
474 /* Soft line break? */
475 if (*is == '\n')
476 goto jsoftnl;
477 ++is;
478 goto jebody;
481 /* Not a soft line break? */
482 if (*is != '\n') {
483 c = _qp_cfromhex(is);
484 is += 2;
485 if (c >= 0)
486 *oc++ = (char)c;
487 else {
488 /* Illegal according to RFC 2045, section 6.7.
489 * Almost follow it and include the = and the follow char */
490 jebody:
491 /* TODO 0xFFFD
492 *oc[0] = '['; oc[1] = '?'; oc[2] = ']';
493 *oc += 3; 0xFFFD TODO
494 */ *oc++ = '?';
496 continue;
499 /* CRLF line endings are encoded as QP, followed by a soft line break, so
500 * check for this special case, and simply forget we have seen one, so as
501 * not to end up with the entire DOS file in a contiguous buffer */
502 jsoftnl:
503 if (PTRCMP(oc, >, os) && oc[-1] == '\n') {
504 #if 0 /* TODO qp_decode() we do not normalize CRLF
505 * TODO to LF because for that we would need
506 * TODO to know if we are about to write to
507 * TODO the display or do save the file!
508 * TODO 'hope the MIME/send layer rewrite will
509 * TODO offer the possibility to DTRT */
510 if (oc - 1 > os && oc[-2] == '\r') {
511 --oc;
512 oc[-1] = '\n';
514 #endif
515 break;
517 out->l = PTR2SIZE(oc - os);
518 rest->s = srealloc(rest->s, rest->l + out->l);
519 memcpy(rest->s + rest->l, out->s, out->l);
520 rest->l += out->l;
521 oc = os;
522 break;
524 /* XXX RFC: QP decode should check no trailing WS on line */
525 jleave:
526 out->l = PTR2SIZE(oc - os);
527 ret = OKAY;
528 return ret;
531 FL size_t
532 b64_encode_calc_size(size_t len)
534 len = (len * 4) / 3;
535 len += (((len / B64_ENCODE_INPUT_PER_LINE) + 1) * 3);
536 len += 2 + 1; /* CRLF, \0 */
537 return len;
540 FL struct str *
541 b64_encode(struct str *out, struct str const *in, enum b64flags flags)
543 static char const b64table[] =
544 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
545 ui8_t const *p = (ui8_t const*)in->s;
546 ssize_t i, lnlen;
547 char *b64;
549 if (!(flags & B64_BUF)) {
550 i = b64_encode_calc_size(in->l);
551 out->s = (flags & B64_SALLOC) ? salloc(i) : srealloc(out->s, i);
553 b64 = out->s;
555 if (!(flags & (B64_CRLF | B64_LF)))
556 flags &= ~B64_MULTILINE;
558 for (lnlen = 0, i = (ssize_t)in->l; i > 0; p += 3, i -= 3) {
559 ui32_t a = p[0], b, c;
561 b64[0] = b64table[a >> 2];
562 switch (i) {
563 case 1:
564 b64[1] = b64table[((a & 0x3) << 4)];
565 b64[2] =
566 b64[3] = '=';
567 break;
568 case 2:
569 b = p[1];
570 b64[1] = b64table[((a & 0x3) << 4) | ((b & 0xf0) >> 4)];
571 b64[2] = b64table[((b & 0xf) << 2)];
572 b64[3] = '=';
573 break;
574 default:
575 b = p[1];
576 c = p[2];
577 b64[1] = b64table[((a & 0x3) << 4) | ((b & 0xf0) >> 4)];
578 b64[2] = b64table[((b & 0xf) << 2) | ((c & 0xc0) >> 6)];
579 b64[3] = b64table[c & 0x3f];
580 break;
583 b64 += 4;
584 if (!(flags & B64_MULTILINE))
585 continue;
586 lnlen += 4;
587 if (lnlen < B64_LINESIZE)
588 continue;
590 lnlen = 0;
591 if (flags & B64_CRLF)
592 *b64++ = '\r';
593 if (flags & (B64_CRLF | B64_LF))
594 *b64++ = '\n';
597 if ((flags & (B64_CRLF | B64_LF)) &&
598 (!(flags & B64_MULTILINE) || lnlen != 0)) {
599 if (flags & B64_CRLF)
600 *b64++ = '\r';
601 if (flags & (B64_CRLF | B64_LF))
602 *b64++ = '\n';
604 out->l = PTR2SIZE(b64 - out->s);
605 out->s[out->l] = '\0';
606 return out;
609 FL struct str *
610 b64_encode_cp(struct str *out, char const *cp, enum b64flags flags)
612 struct str in;
613 in.s = UNCONST(cp);
614 in.l = strlen(cp);
615 return b64_encode(out, &in, flags);
618 FL struct str *
619 b64_encode_buf(struct str *out, void const *vp, size_t vp_len,
620 enum b64flags flags)
622 struct str in;
623 in.s = UNCONST(vp);
624 in.l = vp_len;
625 return b64_encode(out, &in, flags);
628 FL int
629 b64_decode(struct str *out, struct str const *in, struct str *rest)
631 struct str work;
632 char *x;
633 int ret = STOP;
634 size_t len = _b64_decode_prepare(&work, in);
636 /* Ignore an empty input, as may happen for an empty final line */
637 if (work.l == 0) {
638 /* In B64_T cases there may be leftover decoded data for
639 * iconv(3) though, even if that means it's incomplete
640 * multibyte character we have to copy over */
641 /* XXX strictly speaking this should not be handled in here,
642 * XXX since its leftover decoded data from an iconv(3);
643 * XXX like this we shared the prototype with QP, though?? */
644 if (rest != NULL && rest->l > 0) {
645 x = out->s;
646 *out = *rest;
647 rest->s = x;
648 rest->l = 0;
649 } else
650 out->l = 0;
651 ret = OKAY;
652 goto jleave;
654 if (work.l >= 4 && (work.l & 3) == 0) {
655 out->s = srealloc(out->s, len);
656 ret = OKAY;
658 if (ret != OKAY || (ssize_t)(len = _b64_decode(out, &work)) < 0)
659 goto jerr;
660 jleave:
661 return ret;
663 jerr: {
664 char const *err = tr(15, "[Invalid Base64 encoding ignored]\n");
665 len = strlen(err);
666 x = out->s = srealloc(out->s, len + 2);
667 if (rest != NULL && rest->l)
668 *x++ = '\n';
669 memcpy(x, err, len);
670 x += len;
671 *x = '\0';
672 out->l = PTR2SIZE(x - out->s);
673 if (rest != NULL)
674 rest->l = 0;
675 ret = STOP;
676 goto jleave;
680 /* vim:set fenc=utf-8:s-it-mode */