cc-test.sh: do a simple \`Resend' test
[s-mailx.git] / mime_enc.c
blob5c8d4fe2c7cc26c2227c1d9516a0b23eff618c05
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Content-Transfer-Encodings as defined in RFC 2045 (and RFC 2047):
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 - 2015 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.
39 #undef n_FILE
40 #define n_FILE mime_enc
42 #ifndef HAVE_AMALGAMATION
43 # include "nail.h"
44 #endif
46 enum _qact {
47 N = 0, /* Do not quote */
48 Q = 1, /* Must quote */
49 SP = 2, /* sp */
50 XF = 3, /* Special character 'F' - maybe quoted */
51 XD = 4, /* Special character '.' - maybe quoted */
52 UU = 5, /* In header, _ must be quoted in encoded word */
53 US = '_', /* In header, ' ' must be quoted as _ in encoded word */
54 QM = '?', /* In header, special character ? not always quoted */
55 EQ = '=', /* In header, '=' must be quoted in encoded word */
56 HT ='\t', /* In body HT=SP, in head HT=HT, but quote in encoded word */
57 NL = N, /* Don't quote '\n' (NL) */
58 CR = Q /* Always quote a '\r' (CR) */
61 /* Lookup tables to decide wether a character must be encoded or not.
62 * Email header differences according to RFC 2047, section 4.2:
63 * - also quote SP (as the underscore _), TAB, ?, _, CR, LF
64 * - don't care about the special ^F[rom] and ^.$ */
65 static ui8_t const _qtab_body[] = {
66 Q, Q, Q, Q, Q, Q, Q, Q, Q,SP,NL, Q, Q,CR, Q, Q,
67 Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q,
68 SP, N, N, N, N, N, N, N, N, N, N, N, N, N,XD, N,
69 N, N, N, N, N, N, N, N, N, N, N, N, N, Q, N, N,
71 N, N, N, N, N, N,XF, N, N, N, N, N, N, N, N, N,
72 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
73 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
74 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, Q,
76 _qtab_head[] = {
77 Q, Q, Q, Q, Q, Q, Q, Q, Q,HT, Q, Q, Q, Q, Q, Q,
78 Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q, Q,
79 US, 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,EQ, N,QM,
82 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
83 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,UU,
84 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N,
85 N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, Q,
88 /* For decoding be robust and allow lowercase letters, too */
89 static char const _qp_itoa16[] = "0123456789ABCDEF";
90 static ui8_t const _qp_atoi16[] = {
91 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 0x30-0x37 */
92 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x38-0x3F */
93 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, /* 0x40-0x47 */
94 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x48-0x4f */
95 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x50-0x57 */
96 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0x58-0x5f */
97 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF /* 0x60-0x67 */
100 /* The decoding table is only accessed via _B64_DECUI8() */
101 static char const _b64_enctbl[] =
102 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
103 static signed char const _b64__dectbl[] = {
104 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
105 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
106 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
107 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-2,-1,-1,
108 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
109 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
110 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
111 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
113 #define _B64_EQU (ui32_t)-2
114 #define _B64_BAD (ui32_t)-1
115 #define _B64_DECUI8(C) \
116 ((C) >= sizeof(_b64__dectbl) ? _B64_BAD : (ui32_t)_b64__dectbl[(ui8_t)(C)])
118 /* ASCII case-insensitive check wether Content-Transfer-Encoding: header body
119 * hbody defined this encoding type */
120 static bool_t _is_ct_enc(char const *hbody, char const *encoding);
122 /* Check wether *s must be quoted according to flags, else body rules;
123 * sol indicates wether we are at the first character of a line/field */
124 SINLINE enum _qact _mustquote(char const *s, char const *e, bool_t sol,
125 enum mime_enc_flags flags);
127 /* Convert c to/from a hexadecimal character string */
128 SINLINE char * _qp_ctohex(char *store, char c);
129 SINLINE si32_t _qp_cfromhex(char const *hex);
131 /* Trim WS and make work point to the decodable range of in*
132 * Return the amount of bytes a b64_decode operation on that buffer requires */
133 static size_t _b64_decode_prepare(struct str *work,
134 struct str const *in);
136 /* Perform b64_decode on sufficiently spaced & multiple-of-4 base in(put).
137 * Return number of useful bytes in out or -1 on error */
138 static ssize_t _b64_decode(struct str *out, struct str *in);
140 static bool_t
141 _is_ct_enc(char const *hbody, char const *encoding)
143 bool_t quoted, rv;
144 int c;
145 NYD2_ENTER;
147 if (*hbody == '"')
148 quoted = TRU1, ++hbody;
149 else
150 quoted = FAL0;
151 rv = FAL0;
153 while (*hbody != '\0' && *encoding != '\0')
154 if ((c = *hbody++, lowerconv(c) != *encoding++))
155 goto jleave;
156 rv = TRU1;
158 if (quoted && *hbody == '"')
159 goto jleave;
160 if (*hbody == '\0' || whitechar(*hbody))
161 goto jleave;
162 rv = FAL0;
163 jleave:
164 NYD2_LEAVE;
165 return rv;
168 SINLINE enum _qact
169 _mustquote(char const *s, char const *e, bool_t sol, enum mime_enc_flags flags)
171 ui8_t const *qtab;
172 enum _qact a, r;
173 NYD2_ENTER;
175 qtab = (flags & (MIMEEF_ISHEAD | MIMEEF_ISENCWORD))
176 ? _qtab_head : _qtab_body;
177 a = ((ui8_t)*s > 0x7F) ? Q : qtab[(ui8_t)*s];
179 if ((r = a) == N || (r = a) == Q)
180 goto jleave;
181 r = Q;
183 /* Special header fields */
184 if (flags & (MIMEEF_ISHEAD | MIMEEF_ISENCWORD)) {
185 /* Special massage for encoded words */
186 if (flags & MIMEEF_ISENCWORD) {
187 switch (a) {
188 case HT:
189 case US:
190 case EQ:
191 r = a;
192 /* FALLTHRU */
193 case UU:
194 goto jleave;
195 default:
196 break;
200 /* Treat '?' only special if part of '=?' .. '?=' (still too much quoting
201 * since it's '=?CHARSET?CTE?stuff?=', and especially the trailing ?=
202 * should be hard too match */
203 if (a == QM && ((!sol && s[-1] == '=') || (s < e && s[1] == '=')))
204 goto jleave;
205 goto jnquote;
208 /* Body-only */
210 if (a == SP) {
211 /* WS only if trailing white space */
212 if (PTRCMP(s + 1, ==, e) || s[1] == '\n')
213 goto jleave;
214 goto jnquote;
217 /* Rest are special begin-of-line cases */
218 if (!sol)
219 goto jnquote;
221 /* ^From */
222 if (a == XF) {
223 if (PTRCMP(s + 4, <, e) && s[1] == 'r' && s[2] == 'o' && s[3] == 'm')
224 goto jleave;
225 goto jnquote;
227 /* ^.$ */
228 if (a == XD && (PTRCMP(s + 1, ==, e) || s[1] == '\n'))
229 goto jleave;
230 jnquote:
231 r = N;
232 jleave:
233 NYD2_LEAVE;
234 return r;
237 SINLINE char *
238 _qp_ctohex(char *store, char c)
240 NYD2_ENTER;
241 store[2] = '\0';
242 store[1] = _qp_itoa16[(ui8_t)c & 0x0F];
243 c = ((ui8_t)c >> 4) & 0x0F;
244 store[0] = _qp_itoa16[(ui8_t)c];
245 NYD2_LEAVE;
246 return store;
249 SINLINE si32_t
250 _qp_cfromhex(char const *hex)
252 ui8_t i1, i2;
253 si32_t rv;
254 NYD2_ENTER;
256 if ((i1 = (ui8_t)hex[0] - '0') >= NELEM(_qp_atoi16) ||
257 (i2 = (ui8_t)hex[1] - '0') >= NELEM(_qp_atoi16))
258 goto jerr;
259 i1 = _qp_atoi16[i1];
260 i2 = _qp_atoi16[i2];
261 if ((i1 | i2) & 0xF0u)
262 goto jerr;
263 rv = i1;
264 rv <<= 4;
265 rv += i2;
266 jleave:
267 NYD2_LEAVE;
268 return rv;
269 jerr:
270 rv = -1;
271 goto jleave;
274 static size_t
275 _b64_decode_prepare(struct str *work, struct str const *in)
277 char *cp;
278 size_t cp_len;
279 NYD2_ENTER;
281 cp = in->s;
282 cp_len = in->l;
284 while (cp_len > 0 && spacechar(*cp))
285 ++cp, --cp_len;
286 work->s = cp;
288 for (cp += cp_len; cp_len > 0; --cp_len) {
289 char c = *--cp;
290 if (!spacechar(c))
291 break;
293 work->l = cp_len;
295 if (cp_len > 16)
296 cp_len = ((cp_len * 3) >> 2) + (cp_len >> 3);
297 NYD2_LEAVE;
298 return cp_len;
301 static ssize_t
302 _b64_decode(struct str *out, struct str *in)
304 ssize_t rv = -1;
305 ui8_t *p;
306 ui8_t const *q, *end;
307 NYD2_ENTER;
309 p = (ui8_t*)out->s + out->l;
310 q = (ui8_t const*)in->s;
312 for (end = q + in->l; PTRCMP(q + 4, <=, end);) {
313 ui32_t a = _B64_DECUI8(q[0]), b = _B64_DECUI8(q[1]),
314 c = _B64_DECUI8(q[2]), d = _B64_DECUI8(q[3]);
315 q += 4;
317 if (a >= _B64_EQU || b >= _B64_EQU || c == _B64_BAD || d == _B64_BAD)
318 goto jleave;
320 *p++ = ((a << 2) | ((b & 0x30) >> 4));
321 if (c == _B64_EQU) { /* got '=' */
322 if (d != _B64_EQU)
323 goto jleave;
324 break;
326 *p++ = (((b & 0x0F) << 4) | ((c & 0x3C) >> 2));
327 if (d == _B64_EQU) /* got '=' */
328 break;
329 *p++ = (((c & 0x03) << 6) | d);
331 rv ^= rv;
333 jleave: {
334 size_t i = PTR2SIZE((char*)p - out->s);
335 out->l = i;
336 if (rv == 0)
337 rv = (ssize_t)i;
339 in->l -= PTR2SIZE((char*)UNCONST(q) - in->s);
340 in->s = UNCONST(q);
341 NYD2_LEAVE;
342 return rv;
345 FL char *
346 mime_char_to_hexseq(char store[3], char c)
348 char *rv;
349 NYD2_ENTER;
351 rv = _qp_ctohex(store, c);
352 NYD2_LEAVE;
353 return rv;
356 FL si32_t
357 mime_hexseq_to_char(char const *hex)
359 si32_t rv;
360 NYD2_ENTER;
362 rv = _qp_cfromhex(hex);
363 NYD2_LEAVE;
364 return rv;
367 FL enum mime_enc
368 mime_enc_target(void)
370 char const *cp;
371 enum mime_enc rv;
372 NYD2_ENTER;
374 if ((cp = ok_vlook(encoding)) == NULL)
375 rv = MIME_DEFAULT_ENCODING;
376 else if (!asccasecmp(cp, "quoted-printable"))
377 rv = MIMEE_QP;
378 else if (!asccasecmp(cp, "8bit"))
379 rv = MIMEE_8B;
380 else if (!asccasecmp(cp, "base64"))
381 rv = MIMEE_B64;
382 else {
383 n_err(_("Warning: invalid *encoding*, using Base64: \"%s\"\n"), cp);
384 rv = MIMEE_B64;
386 NYD2_LEAVE;
387 return rv;
390 FL enum mime_enc
391 mime_enc_from_ctehead(char const *hbody)
393 enum mime_enc rv;
394 NYD2_ENTER;
396 if (hbody == NULL || _is_ct_enc(hbody, "7bit"))
397 rv = MIMEE_7B;
398 else if (_is_ct_enc(hbody, "8bit"))
399 rv = MIMEE_8B;
400 else if (_is_ct_enc(hbody, "base64"))
401 rv = MIMEE_B64;
402 else if (_is_ct_enc(hbody, "binary"))
403 rv = MIMEE_BIN;
404 else if (_is_ct_enc(hbody, "quoted-printable"))
405 rv = MIMEE_QP;
406 else
407 rv = MIMEE_NONE;
408 NYD2_LEAVE;
409 return rv;
412 FL char const *
413 mime_enc_from_conversion(enum conversion const convert) /* TODO booom */
415 char const *rv;
416 NYD_ENTER;
418 switch (convert) {
419 case CONV_7BIT: rv = "7bit"; break;
420 case CONV_8BIT: rv = "8bit"; break;
421 case CONV_TOQP: rv = "quoted-printable"; break;
422 case CONV_TOB64: rv = "base64"; break;
423 default: rv = ""; break;
425 NYD_LEAVE;
426 return rv;
429 FL size_t
430 mime_enc_mustquote(char const *ln, size_t lnlen, enum mime_enc_flags flags)
432 size_t rv;
433 bool_t sol;
434 NYD_ENTER;
436 for (rv = 0, sol = TRU1; lnlen > 0; sol = FAL0, ++ln, --lnlen)
437 switch (_mustquote(ln, ln + lnlen, sol, flags)) {
438 case US:
439 case EQ:
440 case HT:
441 assert(flags & MIMEEF_ISENCWORD);
442 /* FALLTHRU */
443 case N:
444 continue;
445 default:
446 ++rv;
448 NYD_LEAVE;
449 return rv;
452 FL size_t
453 qp_encode_calc_size(size_t len)
455 size_t bytes, lines;
456 NYD_ENTER;
458 /* The worst case sequence is 'CRLF' -> '=0D=0A=\n\0'.
459 * However, we must be aware that (a) the output may span multiple lines
460 * and (b) the input does not end with a newline itself (nonetheless):
461 * LC_ALL=C awk 'BEGIN{
462 * for (i = 1; i < 100000; ++i) printf "\xC3\xBC"
463 * }' |
464 * MAILRC=/dev/null LC_ALL=en_US.UTF-8 s-nail -nvvd \
465 * -Ssendcharsets=utf8 -s testsub ./LETTER */
466 bytes = len * 3;
467 lines = bytes / QP_LINESIZE;
468 len += lines;
470 bytes = len * 3;
471 /* Trailing hard NL may be missing, so there may be two lines.
472 * Thus add soft + hard NL per line and a trailing NUL */
473 lines = (bytes / QP_LINESIZE) + 1;
474 lines <<= 1;
475 bytes += lines;
476 len = ++bytes;
478 NYD_LEAVE;
479 return len;
482 #ifdef notyet
483 FL struct str *
484 qp_encode_cp(struct str *out, char const *cp, enum qpflags flags)
486 struct str in;
487 NYD_ENTER;
489 in.s = UNCONST(cp);
490 in.l = strlen(cp);
491 out = qp_encode(out, &in, flags);
492 NYD_LEAVE;
493 return out;
496 FL struct str *
497 qp_encode_buf(struct str *out, void const *vp, size_t vp_len,
498 enum qpflags flags)
500 struct str in;
501 NYD_ENTER;
503 in.s = UNCONST(vp);
504 in.l = vp_len;
505 out = qp_encode(out, &in, flags);
506 NYD_LEAVE;
507 return out;
509 #endif /* notyet */
511 FL struct str *
512 qp_encode(struct str *out, struct str const *in, enum qpflags flags)
514 bool_t sol = (flags & QP_ISHEAD ? FAL0 : TRU1), seenx;
515 ssize_t lnlen;
516 char *qp;
517 char const *is, *ie;
518 NYD_ENTER;
520 if (!(flags & QP_BUF)) {
521 lnlen = qp_encode_calc_size(in->l);
522 out->s = (flags & QP_SALLOC) ? salloc(lnlen) : srealloc(out->s, lnlen);
524 qp = out->s;
525 is = in->s;
526 ie = is + in->l;
528 /* QP_ISHEAD? */
529 if (!sol) {
530 enum mime_enc_flags ef = MIMEEF_ISHEAD |
531 (flags & QP_ISENCWORD ? MIMEEF_ISENCWORD : 0);
533 for (seenx = FAL0, sol = TRU1; is < ie; sol = FAL0, ++qp) {
534 enum _qact mq = _mustquote(is, ie, sol, ef);
535 char c = *is++;
537 if (mq == N) {
538 /* We convert into a single *encoded-word*, that'll end up in
539 * =?C?Q??=; quote '?' from when we're inside there on */
540 if (seenx && c == '?')
541 goto jheadq;
542 *qp = c;
543 } else if (mq == US)
544 *qp = US;
545 else {
546 seenx = TRU1;
547 jheadq:
548 *qp++ = '=';
549 qp = _qp_ctohex(qp, c) + 1;
552 goto jleave;
555 /* The body needs to take care for soft line breaks etc. */
556 for (lnlen = 0, seenx = FAL0; is < ie; sol = FAL0) {
557 enum _qact mq = _mustquote(is, ie, sol, MIMEEF_NONE);
558 char c = *is++;
560 if (mq == N && (c != '\n' || !seenx)) {
561 *qp++ = c;
562 if (++lnlen < QP_LINESIZE - 1)
563 continue;
564 /* Don't write a soft line break when we're in the last possible
565 * column and either an LF has been written or only an LF follows, as
566 * that'll end the line anyway */
567 /* XXX but - ensure is+1>=ie, then??
568 * xxx and/or - what about resetting lnlen; that contra
569 * xxx dicts input==1 input line assertion, though */
570 if (c == '\n' || is == ie || *is == '\n')
571 continue;
572 jsoftnl:
573 qp[0] = '=';
574 qp[1] = '\n';
575 qp += 2;
576 lnlen = 0;
577 continue;
580 if (lnlen > QP_LINESIZE - 3 - 1) {
581 qp[0] = '=';
582 qp[1] = '\n';
583 qp += 2;
584 lnlen = 0;
586 *qp++ = '=';
587 qp = _qp_ctohex(qp, c);
588 qp += 2;
589 lnlen += 3;
590 if (c != '\n' || !seenx)
591 seenx = (c == '\r');
592 else {
593 seenx = FAL0;
594 goto jsoftnl;
598 /* Enforce soft line break if we haven't seen LF */
599 if (in->l > 0 && *--is != '\n') {
600 qp[0] = '=';
601 qp[1] = '\n';
602 qp += 2;
604 jleave:
605 out->l = PTR2SIZE(qp - out->s);
606 out->s[out->l] = '\0';
607 NYD_LEAVE;
608 return out;
611 FL int
612 qp_decode(struct str *out, struct str const *in, struct str *rest)
614 int rv = STOP;
615 char *os, *oc;
616 char const *is, *ie;
617 NYD_ENTER;
619 if (rest != NULL && rest->l != 0) {
620 os = out->s;
621 *out = *rest;
622 rest->s = os;
623 rest->l = 0;
626 oc = os =
627 out->s = srealloc(out->s, out->l + in->l + 3);
628 oc += out->l;
629 is = in->s;
630 ie = is + in->l;
632 /* Decoding encoded-word (RFC 2049) in a header field? */
633 if (rest == NULL) {
634 while (is < ie) {
635 si32_t c = *is++;
636 if (c == '=') {
637 if (PTRCMP(is + 1, >=, ie)) {
638 ++is;
639 goto jehead;
641 c = _qp_cfromhex(is);
642 is += 2;
643 if (c >= 0)
644 *oc++ = (char)c;
645 else {
646 /* Invalid according to RFC 2045, section 6.7. Almost follow */
647 jehead:
648 /* TODO 0xFFFD
649 *oc[0] = '['; oc[1] = '?'; oc[2] = ']';
650 *oc += 3; 0xFFFD TODO
651 */ *oc++ = '?';
653 } else
654 *oc++ = (c == '_' /* US */) ? ' ' : (char)c;
656 goto jleave; /* XXX QP decode, header: errors not reported */
659 /* Decoding a complete message/mimepart body line */
660 while (is < ie) {
661 si32_t c = *is++;
662 if (c != '=') {
663 *oc++ = (char)c;
664 continue;
667 /* RFC 2045, 6.7:
668 * Therefore, when decoding a Quoted-Printable body, any
669 * trailing white space on a line must be deleted, as it will
670 * necessarily have been added by intermediate transport
671 * agents */
672 for (; is < ie && blankchar(*is); ++is)
674 if (PTRCMP(is + 1, >=, ie)) {
675 /* Soft line break? */
676 if (*is == '\n')
677 goto jsoftnl;
678 ++is;
679 goto jebody;
682 /* Not a soft line break? */
683 if (*is != '\n') {
684 c = _qp_cfromhex(is);
685 is += 2;
686 if (c >= 0)
687 *oc++ = (char)c;
688 else {
689 /* Invalid according to RFC 2045, section 6.7.
690 * Almost follow it and include the = and the follow char */
691 jebody:
692 /* TODO 0xFFFD
693 *oc[0] = '['; oc[1] = '?'; oc[2] = ']';
694 *oc += 3; 0xFFFD TODO
695 */ *oc++ = '?';
697 continue;
700 /* CRLF line endings are encoded as QP, followed by a soft line break, so
701 * check for this special case, and simply forget we have seen one, so as
702 * not to end up with the entire DOS file in a contiguous buffer */
703 jsoftnl:
704 if (oc > os && oc[-1] == '\n') {
705 #if 0 /* TODO qp_decode() we do not normalize CRLF
706 * TODO to LF because for that we would need
707 * TODO to know if we are about to write to
708 * TODO the display or do save the file!
709 * TODO 'hope the MIME/send layer rewrite will
710 * TODO offer the possibility to DTRT */
711 if (oc - 1 > os && oc[-2] == '\r') {
712 --oc;
713 oc[-1] = '\n';
715 #endif
716 break;
718 out->l = PTR2SIZE(oc - os);
719 rest->s = srealloc(rest->s, rest->l + out->l);
720 memcpy(rest->s + rest->l, out->s, out->l);
721 rest->l += out->l;
722 oc = os;
723 break;
725 /* XXX RFC: QP decode should check no trailing WS on line */
726 jleave:
727 out->l = PTR2SIZE(oc - os);
728 rv = OKAY;
729 NYD_LEAVE;
730 return rv;
733 FL size_t
734 b64_encode_calc_size(size_t len)
736 NYD_ENTER;
737 len = (len * 4) / 3;
738 len += (((len / B64_ENCODE_INPUT_PER_LINE) + 1) * 3);
739 len += 2 + 1; /* CRLF, \0 */
740 NYD_LEAVE;
741 return len;
744 FL struct str *
745 b64_encode(struct str *out, struct str const *in, enum b64flags flags)
747 ui8_t const *p;
748 ssize_t i, lnlen;
749 char *b64;
750 NYD_ENTER;
752 p = (ui8_t const*)in->s;
754 if (!(flags & B64_BUF)) {
755 i = b64_encode_calc_size(in->l);
756 out->s = (flags & B64_SALLOC) ? salloc(i) : srealloc(out->s, i);
758 b64 = out->s;
760 if (!(flags & (B64_CRLF | B64_LF)))
761 flags &= ~B64_MULTILINE;
763 for (lnlen = 0, i = (ssize_t)in->l; i > 0; p += 3, i -= 3) {
764 ui32_t a = p[0], b, c;
766 b64[0] = _b64_enctbl[a >> 2];
767 switch (i) {
768 case 1:
769 b64[1] = _b64_enctbl[((a & 0x3) << 4)];
770 b64[2] =
771 b64[3] = '=';
772 break;
773 case 2:
774 b = p[1];
775 b64[1] = _b64_enctbl[((a & 0x03) << 4) | ((b & 0xF0u) >> 4)];
776 b64[2] = _b64_enctbl[((b & 0x0F) << 2)];
777 b64[3] = '=';
778 break;
779 default:
780 b = p[1];
781 c = p[2];
782 b64[1] = _b64_enctbl[((a & 0x03) << 4) | ((b & 0xF0u) >> 4)];
783 b64[2] = _b64_enctbl[((b & 0x0F) << 2) | ((c & 0xC0u) >> 6)];
784 b64[3] = _b64_enctbl[c & 0x3F];
785 break;
788 b64 += 4;
789 if (!(flags & B64_MULTILINE))
790 continue;
791 lnlen += 4;
792 if (lnlen < B64_LINESIZE)
793 continue;
795 lnlen = 0;
796 if (flags & B64_CRLF)
797 *b64++ = '\r';
798 if (flags & (B64_CRLF | B64_LF))
799 *b64++ = '\n';
802 if ((flags & (B64_CRLF | B64_LF)) &&
803 (!(flags & B64_MULTILINE) || lnlen != 0)) {
804 if (flags & B64_CRLF)
805 *b64++ = '\r';
806 if (flags & (B64_CRLF | B64_LF))
807 *b64++ = '\n';
809 out->l = PTR2SIZE(b64 - out->s);
810 out->s[out->l] = '\0';
812 /* Base64 includes + and /, replace them with _ and -.
813 * This is base64url according to RFC 4648, then. Since we only support
814 * that for encoding and it is only used for boundary strings, this is
815 * yet a primitive implementation; xxx use tables; support decoding */
816 if (flags & B64_RFC4648URL) {
817 char c;
819 for (b64 = out->s; (c = *b64) != '\0'; ++b64)
820 if (c == '+')
821 *b64 = '-';
822 else if (c == '/')
823 *b64 = '_';
825 NYD_LEAVE;
826 return out;
829 FL struct str *
830 b64_encode_buf(struct str *out, void const *vp, size_t vp_len,
831 enum b64flags flags)
833 struct str in;
834 NYD_ENTER;
836 in.s = UNCONST(vp);
837 in.l = vp_len;
838 out = b64_encode(out, &in, flags);
839 NYD_LEAVE;
840 return out;
843 #ifdef HAVE_SMTP
844 FL struct str *
845 b64_encode_cp(struct str *out, char const *cp, enum b64flags flags)
847 struct str in;
848 NYD_ENTER;
850 in.s = UNCONST(cp);
851 in.l = strlen(cp);
852 out = b64_encode(out, &in, flags);
853 NYD_LEAVE;
854 return out;
856 #endif
858 FL int
859 b64_decode(struct str *out, struct str const *in, struct str *rest)
861 struct str work;
862 char *x;
863 size_t len;
864 int rv; /* XXX -> bool_t */
865 NYD_ENTER;
867 len = _b64_decode_prepare(&work, in);
868 out->l = 0;
870 /* TODO B64_T is different since we must not fail for errors; in v15.0 this
871 * TODO will be filter based and B64_T will have a different one than B64,
872 * TODO for now special treat this all-horror */
873 if (rest != NULL) {
874 /* With B64_T there may be leftover decoded data for iconv(3), even if
875 * that means it's incomplete multibyte character we have to copy over */
876 /* TODO strictly speaking this should not be handled in here,
877 * TODO since its leftover decoded data from an iconv(3);
878 * TODO In v15.0 this path will be filter based, each filter having its
879 * TODO own buffer for such purpose; for now we are BUSTED since for
880 * TODO Base64 rest is owned by iconv(3) */
881 if (rest->l > 0) {
882 x = out->s;
883 *out = *rest;
884 rest->s = x; /* Just for ownership reasons (all TODO in here..) */
885 rest->l = 0;
886 len += out->l;
889 out->s = srealloc(out->s, len +1);
891 for (;;) {
892 if (_b64_decode(out, &work) >= 0) {
893 if (work.l == 0)
894 break;
896 x = out->s + out->l;
898 /* Partial/False last sequence. TODO not solvable for non-EOF;
899 * TODO yes, invalid, but seen in the wild and should be handled,
900 * TODO but for that we had to have our v15.0 filter which doesn't
901 * TODO work line based but content buffer based */
902 if ((len = work.l) <= 4) {
903 switch (len) {
904 case 4: /* FALLTHRU */
905 case 3: x[2] = '?'; /* FALLTHRU */
906 case 2: x[1] = '?'; /* FALLTHRU */
907 default: x[0] = '?'; break;
909 out->l += len;
910 break;
913 /* TODO Bad content: this problem is not solvable! I've seen
914 * TODO messages which broke lines in the middle of a Base64
915 * TODO tuple, followed by an invalid character ("!"), the follow
916 * TODO line starting with whitespace and the remaining sequence.
917 * TODO OpenSSL bailed, mutt(1) got it right (silently..).
918 * TODO Since "rest" is not usable by us, we cannot continue
919 * TODO sequences. We will be able to do so with the v15.0 filter
920 * TODO approach, if we */
921 /* Bad content: skip over a single sequence */
922 for (;;) {
923 *x++ = '?';
924 ++out->l;
925 if (--work.l == 0)
926 break;
927 else {
928 ui8_t bc = (ui8_t)*++work.s;
929 ui32_t state = _B64_DECUI8(bc);
931 if (state != _B64_EQU && state != _B64_BAD)
932 break;
936 rv = OKAY;
937 goto jleave;
940 /* Ignore an empty input, as may happen for an empty final line */
941 if (work.l == 0) {
942 out->s = srealloc(out->s, 1);
943 rv = OKAY;
944 } else if (work.l >= 4 && !(work.l & 3)) {
945 out->s = srealloc(out->s, len +1);
946 if ((ssize_t)(len = _b64_decode(out, &work)) < 0)
947 goto jerr;
948 rv = OKAY;
949 } else
950 goto jerr;
952 jleave:
953 out->s[out->l] = '\0';
954 NYD_LEAVE;
955 return rv;
957 jerr: {
958 char const *err = _("[Invalid Base64 encoding]\n");
959 out->l = len = strlen(err);
960 out->s = srealloc(out->s, len +1);
961 memcpy(out->s, err, len);
962 rv = STOP;
963 goto jleave;
967 /* s-it-mode */