a_main_setscreensize(): no need to test n_PSO_INTERACTIVE
[s-mailx.git] / mime_enc.c
blob9457694ec5eb13b45f414456b00bd2cddeaa4344
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Content-Transfer-Encodings as defined in RFC 2045 (and RFC 2047;
3 *@ for _header() versions: including "encoded word" as of RFC 2049):
4 *@ - Quoted-Printable, section 6.7
5 *@ - Base64, section 6.8
6 *@ TODO We have no notion of a "current message context" and thus badly log.
7 *@ TODO This is not final yet, v15 will bring "filters".
9 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
10 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
12 /* QP quoting idea, _b64_decode(), b64_encode() taken from NetBSDs mailx(1): */
13 /* $NetBSD: mime_codecs.c,v 1.9 2009/04/10 13:08:25 christos Exp $ */
15 * Copyright (c) 2006 The NetBSD Foundation, Inc.
16 * All rights reserved.
18 * This code is derived from software contributed to The NetBSD Foundation
19 * by Anon Ymous.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
42 #undef n_FILE
43 #define n_FILE mime_enc
45 #ifndef HAVE_AMALGAMATION
46 # include "nail.h"
47 #endif
49 enum a_me_qact{
50 a_ME_N = 0,
51 a_ME_Q = 1, /* Must quote */
52 a_ME_SP = 2, /* sp */
53 a_ME_XF = 3, /* Special character 'F' - maybe quoted */
54 a_ME_XD = 4, /* Special character '.' - maybe quoted */
55 a_ME_UU = 5, /* In header, _ must be quoted in encoded word */
56 a_ME_US = '_', /* In header, ' ' must be quoted as _ in encoded word */
57 a_ME_QM = '?', /* In header, special character ? not always quoted */
58 a_ME_EQ = '=', /* In header, '=' must be quoted in encoded word */
59 a_ME_HT ='\t', /* Body HT=SP. Head HT=HT, BUT quote in encoded word */
60 a_ME_NL = 0, /* Don't quote '\n' (NL) */
61 a_ME_CR = a_ME_Q /* Always quote a '\r' (CR) */
64 /* Lookup tables to decide whether a character must be encoded or not.
65 * Email header differences according to RFC 2047, section 4.2:
66 * - also quote SP (as the underscore _), TAB, ?, _, CR, LF
67 * - don't care about the special ^F[rom] and ^.$ */
68 static ui8_t const a_me_qp_body[] = {
69 a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q,
70 a_ME_Q, a_ME_SP, a_ME_NL, a_ME_Q, a_ME_Q, a_ME_CR, a_ME_Q, a_ME_Q,
71 a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q,
72 a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q,
73 a_ME_SP, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
74 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_XD, a_ME_N,
75 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
76 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_Q, a_ME_N, a_ME_N,
78 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_XF, a_ME_N,
79 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
80 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
81 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
82 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
83 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
84 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
85 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_Q,
86 }, a_me_qp_head[] = {
87 a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q,
88 a_ME_Q, a_ME_HT, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q,
89 a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q,
90 a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q, a_ME_Q,
91 a_ME_US, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
92 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
93 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
94 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_EQ, a_ME_N, a_ME_QM,
96 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
97 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
98 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
99 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_UU,
100 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
101 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
102 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N,
103 a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_N, a_ME_Q,
106 /* The decoding table is only accessed via a_ME_B64_DECUI8() */
107 static char const a_me_b64_enctbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
108 "abcdefghijklmnopqrstuvwxyz" "0123456789" "+/";
109 static signed char const a_me_b64__dectbl[] = {
110 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
111 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
112 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
113 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-2,-1,-1,
114 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
115 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
116 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
117 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
119 #define a_ME_B64_EQU (ui32_t)-2
120 #define a_ME_B64_BAD (ui32_t)-1
121 #define a_ME_B64_DECUI8(C) \
122 ((ui8_t)(C) >= sizeof(a_me_b64__dectbl)\
123 ? a_ME_B64_BAD : (ui32_t)a_me_b64__dectbl[(ui8_t)(C)])
125 /* (Ugly to place an enum here) */
126 static char const a_me_ctes[] = "7bit\0" "8bit\0" \
127 "base64\0" "quoted-printable\0" "binary\0" \
128 /* abbrevs */ "8b\0" "b64\0" "qp\0";
129 enum a_me_ctes_off{
130 a_ME_CTES_7B_OFF = 0, a_ME_CTES_7B_LEN = 4,
131 a_ME_CTES_8B_OFF = 5, a_ME_CTES_8B_LEN = 4,
132 a_ME_CTES_B64_OFF = 10, a_ME_CTES_B64_LEN = 6,
133 a_ME_CTES_QP_OFF = 17, a_ME_CTES_QP_LEN = 16,
134 a_ME_CTES_BIN_OFF = 34, a_ME_CTES_BIN_LEN = 6,
136 a_ME_CTES_S8B_OFF = 41, a_ME_CTES_S8B_LEN = 2,
137 a_ME_CTES_SB64_OFF = 44, a_ME_CTES_SB64_LEN = 3,
138 a_ME_CTES_SQP_OFF = 48, a_ME_CTES_SQP_LEN = 2
141 /* Check whether *s must be quoted according to flags, else body rules;
142 * sol indicates whether we are at the first character of a line/field */
143 SINLINE enum a_me_qact a_me_mustquote(char const *s, char const *e, bool_t sol,
144 enum mime_enc_flags flags);
146 /* Trim WS and make work point to the decodable range of in.
147 * Return the amount of bytes a b64_decode operation on that buffer requires,
148 * or UIZ_MAX on overflow error */
149 static size_t a_me_b64_decode_prepare(struct str *work, struct str const *in);
151 /* Perform b64_decode on in(put) to sufficiently spaced out(put).
152 * Return number of useful bytes in out or -1 on error.
153 * Note: may enter endless loop if in->l < 4 and 0 return is not handled! */
154 static ssize_t a_me_b64_decode(struct str *out, struct str *in);
156 SINLINE enum a_me_qact
157 a_me_mustquote(char const *s, char const *e, bool_t sol,
158 enum mime_enc_flags flags){
159 ui8_t const *qtab;
160 enum a_me_qact a, r;
161 NYD2_ENTER;
163 qtab = (flags & (MIMEEF_ISHEAD | MIMEEF_ISENCWORD))
164 ? a_me_qp_head : a_me_qp_body;
166 if((ui8_t)*s > 0x7F){
167 r = a_ME_Q;
168 goto jleave;
171 a = qtab[(ui8_t)*s];
173 if((r = a) == a_ME_N || a == a_ME_Q)
174 goto jleave;
176 r = a_ME_Q;
178 /* Special header fields */
179 if(flags & (MIMEEF_ISHEAD | MIMEEF_ISENCWORD)){
180 /* Special massage for encoded words */
181 if(flags & MIMEEF_ISENCWORD){
182 switch(a){
183 case a_ME_HT:
184 case a_ME_US:
185 case a_ME_EQ:
186 r = a;
187 /* FALLTHRU */
188 case a_ME_UU:
189 goto jleave;
190 default:
191 break;
195 /* Treat '?' only special if part of '=?' .. '?=' (still too much quoting
196 * since it's '=?CHARSET?CTE?stuff?=', and especially the trailing ?=
197 * should be hard to match */
198 if(a == a_ME_QM && ((!sol && s[-1] == '=') || (s < e && s[1] == '=')))
199 goto jleave;
200 goto jnquote;
203 /* Body-only */
205 if(a == a_ME_SP){
206 /* WS only if trailing white space */
207 if(&s[1] == e || s[1] == '\n')
208 goto jleave;
209 goto jnquote;
212 /* Rest are special begin-of-line cases */
213 if(!sol)
214 goto jnquote;
216 /* ^From */
217 if(a == a_ME_XF){
218 if(&s[4] < e && s[1] == 'r' && s[2] == 'o' && s[3] == 'm' && s[4] == ' ')
219 goto jleave;
220 goto jnquote;
222 /* ^.$ */
223 if(a == a_ME_XD && (&s[1] == e || s[1] == '\n'))
224 goto jleave;
225 jnquote:
226 r = 0;
227 jleave:
228 NYD2_LEAVE;
229 return r;
232 static size_t
233 a_me_b64_decode_prepare(struct str *work, struct str const *in){
234 char *cp;
235 size_t cp_len;
236 NYD2_ENTER;
238 cp = in->s;
239 cp_len = in->l;
241 while(cp_len > 0 && spacechar(*cp))
242 ++cp, --cp_len;
243 work->s = cp;
245 for(cp += cp_len; cp_len > 0; --cp_len){
246 --cp;
247 if(!spacechar(*cp))
248 break;
250 work->l = cp_len;
252 if(cp_len > 16){
253 /* EOVERFLOW */
254 if(UIZ_MAX / 3 <= cp_len){
255 cp_len = UIZ_MAX;
256 goto jleave;
258 cp_len = ((cp_len * 3) >> 2) + (cp_len >> 3);
260 cp_len += (2 * 3) +1;
261 jleave:
262 NYD2_LEAVE;
263 return cp_len;
266 static ssize_t
267 a_me_b64_decode(struct str *out, struct str *in){
268 ui8_t *p;
269 ui8_t const *q, *end;
270 ssize_t rv;
271 NYD2_ENTER;
273 rv = -1;
274 p = (ui8_t*)&out->s[out->l];
275 q = (ui8_t const*)in->s;
277 for(end = &q[in->l]; PTR2SIZE(end - q) >= 4; q += 4){
278 ui32_t a, b, c, d;
280 a = a_ME_B64_DECUI8(q[0]);
281 b = a_ME_B64_DECUI8(q[1]);
282 c = a_ME_B64_DECUI8(q[2]);
283 d = a_ME_B64_DECUI8(q[3]);
285 if(n_UNLIKELY(a >= a_ME_B64_EQU || b >= a_ME_B64_EQU ||
286 c == a_ME_B64_BAD || d == a_ME_B64_BAD))
287 goto jleave;
289 *p++ = ((a << 2) | ((b & 0x30) >> 4));
290 if(c == a_ME_B64_EQU){ /* got '=' */
291 q += 4;
292 if(n_UNLIKELY(d != a_ME_B64_EQU))
293 goto jleave;
294 break;
297 *p++ = (((b & 0x0F) << 4) | ((c & 0x3C) >> 2));
298 if(d == a_ME_B64_EQU) /* got '=' */
299 break;
300 *p++ = (((c & 0x03) << 6) | d);
302 rv ^= rv;
304 jleave:{
305 size_t i;
307 i = PTR2SIZE((char*)p - out->s);
308 out->l = i;
309 if(rv == 0)
310 rv = (ssize_t)i;
312 in->l -= PTR2SIZE(q - (ui8_t*)in->s);
313 in->s = n_UNCONST(q);
314 NYD2_LEAVE;
315 return rv;
318 FL enum mime_enc
319 mime_enc_target(void){
320 char const *cp;
321 enum mime_enc rv;
322 NYD2_ENTER;
324 if((cp = ok_vlook(encoding)) == NULL)
325 rv = MIME_DEFAULT_ENCODING;
326 else if(!asccasecmp(cp, &a_me_ctes[a_ME_CTES_S8B_OFF]) ||
327 !asccasecmp(cp, &a_me_ctes[a_ME_CTES_8B_OFF]))
328 rv = MIMEE_8B;
329 else if(!asccasecmp(cp, &a_me_ctes[a_ME_CTES_SB64_OFF]) ||
330 !asccasecmp(cp, &a_me_ctes[a_ME_CTES_B64_OFF]))
331 rv = MIMEE_B64;
332 else if(!asccasecmp(cp, &a_me_ctes[a_ME_CTES_SQP_OFF]) ||
333 !asccasecmp(cp, &a_me_ctes[a_ME_CTES_QP_OFF]))
334 rv = MIMEE_QP;
335 else{
336 n_err(_("Warning: invalid *encoding*, using Base64: %s\n"), cp);
337 rv = MIMEE_B64;
339 NYD2_LEAVE;
340 return rv;
343 FL enum mime_enc
344 mime_enc_from_ctehead(char const *hbody){
345 enum mime_enc rv;
346 NYD2_ENTER;
348 if(hbody == NULL)
349 rv = MIMEE_7B;
350 else{
351 struct{
352 ui8_t off;
353 ui8_t len;
354 ui8_t enc;
355 ui8_t __dummy;
356 } const *cte, cte_base[] = {
357 {a_ME_CTES_7B_OFF, a_ME_CTES_7B_LEN, MIMEE_7B, 0},
358 {a_ME_CTES_8B_OFF, a_ME_CTES_8B_LEN, MIMEE_8B, 0},
359 {a_ME_CTES_B64_OFF, a_ME_CTES_B64_LEN, MIMEE_B64, 0},
360 {a_ME_CTES_QP_OFF, a_ME_CTES_QP_LEN, MIMEE_QP, 0},
361 {a_ME_CTES_BIN_OFF, a_ME_CTES_BIN_LEN, MIMEE_BIN, 0},
362 {0, 0, MIMEE_NONE, 0}
364 union {char const *s; size_t l;} u;
366 if(*hbody == '"')
367 for(u.s = ++hbody; *u.s != '\0' && *u.s != '"'; ++u.s)
369 else
370 for(u.s = hbody; *u.s != '\0' && !whitechar(*u.s); ++u.s)
372 u.l = PTR2SIZE(u.s - hbody);
374 for(cte = cte_base;;)
375 if(cte->len == u.l && !asccasecmp(&a_me_ctes[cte->off], hbody)){
376 rv = cte->enc;
377 break;
378 }else if((++cte)->enc == MIMEE_NONE){
379 rv = MIMEE_NONE;
380 break;
383 NYD2_LEAVE;
384 return rv;
387 FL char const *
388 mime_enc_from_conversion(enum conversion const convert){
389 char const *rv;
390 NYD2_ENTER;
392 switch(convert){
393 case CONV_7BIT: rv = &a_me_ctes[a_ME_CTES_7B_OFF]; break;
394 case CONV_8BIT: rv = &a_me_ctes[a_ME_CTES_8B_OFF]; break;
395 case CONV_TOQP: rv = &a_me_ctes[a_ME_CTES_QP_OFF]; break;
396 case CONV_TOB64: rv = &a_me_ctes[a_ME_CTES_B64_OFF]; break;
397 case CONV_NONE: rv = &a_me_ctes[a_ME_CTES_BIN_OFF]; break;
398 default: rv = n_empty; break;
400 NYD2_LEAVE;
401 return rv;
404 FL size_t
405 mime_enc_mustquote(char const *ln, size_t lnlen, enum mime_enc_flags flags){
406 size_t rv;
407 bool_t sol;
408 NYD2_ENTER;
410 for(rv = 0, sol = TRU1; lnlen > 0; sol = FAL0, ++ln, --lnlen)
411 switch(a_me_mustquote(ln, ln + lnlen, sol, flags)){
412 case a_ME_US:
413 case a_ME_EQ:
414 case a_ME_HT:
415 assert(flags & MIMEEF_ISENCWORD);
416 /* FALLTHRU */
417 case 0:
418 continue;
419 default:
420 ++rv;
422 NYD2_LEAVE;
423 return rv;
426 FL size_t
427 qp_encode_calc_size(size_t len){
428 size_t bytes, lines;
429 NYD2_ENTER;
431 /* The worst case sequence is 'CRLF' -> '=0D=0A=\n\0'.
432 * However, we must be aware that (a) the output may span multiple lines
433 * and (b) the input does not end with a newline itself (nonetheless):
434 * LC_ALL=C awk 'BEGIN{
435 * for (i = 1; i < 100000; ++i) printf "\xC3\xBC"
436 * }' |
437 * s-nail -:/ -dSsendcharsets=utf8 -s testsub no@where */
439 /* Several EOVERFLOW */
440 if(len >= UIZ_MAX / 3){
441 len = UIZ_MAX;
442 goto jleave;
444 bytes = len * 3;
445 lines = bytes / QP_LINESIZE;
446 len += lines;
448 if(len >= UIZ_MAX / 3){
449 len = UIZ_MAX;
450 goto jleave;
452 /* Trailing hard NL may be missing, so there may be two lines.
453 * Thus add soft + hard NL per line and a trailing NUL */
454 bytes = len * 3;
455 lines = (bytes / QP_LINESIZE) + 1;
456 lines <<= 1;
457 ++bytes;
458 /*if(UIZ_MAX - bytes >= lines){
459 len = UIZ_MAX;
460 goto jleave;
462 bytes += lines;
463 len = bytes;
464 jleave:
465 NYD2_LEAVE;
466 return len;
469 #ifdef notyet
470 FL struct str *
471 qp_encode_cp(struct str *out, char const *cp, enum qpflags flags){
472 struct str in;
473 NYD_ENTER;
475 in.s = n_UNCONST(cp);
476 in.l = strlen(cp);
477 out = qp_encode(out, &in, flags);
478 NYD_LEAVE;
479 return out;
482 FL struct str *
483 qp_encode_buf(struct str *out, void const *vp, size_t vp_len,
484 enum qpflags flags){
485 struct str in;
486 NYD_ENTER;
488 in.s = n_UNCONST(vp);
489 in.l = vp_len;
490 out = qp_encode(out, &in, flags);
491 NYD_LEAVE;
492 return out;
494 #endif /* notyet */
496 FL struct str *
497 qp_encode(struct str *out, struct str const *in, enum qpflags flags){
498 size_t lnlen;
499 char *qp;
500 char const *is, *ie;
501 bool_t sol, seenx;
502 NYD_ENTER;
504 sol = (flags & QP_ISHEAD ? FAL0 : TRU1);
506 if(!(flags & QP_BUF)){
507 if((lnlen = qp_encode_calc_size(in->l)) == UIZ_MAX){
508 out = NULL;
509 goto jerr;
511 out->s = (flags & QP_SALLOC) ? salloc(lnlen) : srealloc(out->s, lnlen);
513 qp = out->s;
514 is = in->s;
515 ie = is + in->l;
517 if(flags & QP_ISHEAD){
518 enum mime_enc_flags ef;
520 ef = MIMEEF_ISHEAD | (flags & QP_ISENCWORD ? MIMEEF_ISENCWORD : 0);
522 for(seenx = FAL0, sol = TRU1; is < ie; sol = FAL0, ++qp){
523 char c;
524 enum a_me_qact mq;
526 mq = a_me_mustquote(is, ie, sol, ef);
527 c = *is++;
529 if(mq == a_ME_N){
530 /* We convert into a single *encoded-word*, that'll end up in
531 * =?C?Q??=; quote '?' from when we're inside there on */
532 if(seenx && c == '?')
533 goto jheadq;
534 *qp = c;
535 }else if(mq == a_ME_US)
536 *qp = a_ME_US;
537 else{
538 seenx = TRU1;
539 jheadq:
540 *qp++ = '=';
541 qp = n_c_to_hex_base16(qp, c) + 1;
544 goto jleave;
547 /* The body needs to take care for soft line breaks etc. */
548 for(lnlen = 0, seenx = FAL0; is < ie; sol = FAL0){
549 char c;
550 enum a_me_qact mq;
552 mq = a_me_mustquote(is, ie, sol, MIMEEF_NONE);
553 c = *is++;
555 if(mq == a_ME_N && (c != '\n' || !seenx)){
556 *qp++ = c;
557 if(++lnlen < QP_LINESIZE - 1)
558 continue;
559 /* Don't write a soft line break when we're in the last possible
560 * column and either an LF has been written or only an LF follows, as
561 * that'll end the line anyway */
562 /* XXX but - ensure is+1>=ie, then??
563 * xxx and/or - what about resetting lnlen; that contra
564 * xxx dicts input==1 input line assertion, though */
565 if(c == '\n' || is == ie || is[0] == '\n' || is[1] == '\n')
566 continue;
567 jsoftnl:
568 qp[0] = '=';
569 qp[1] = '\n';
570 qp += 2;
571 lnlen = 0;
572 continue;
575 if(lnlen > QP_LINESIZE - 3 - 1){
576 qp[0] = '=';
577 qp[1] = '\n';
578 qp += 2;
579 lnlen = 0;
581 *qp++ = '=';
582 qp = n_c_to_hex_base16(qp, c);
583 qp += 2;
584 lnlen += 3;
585 if(c != '\n' || !seenx)
586 seenx = (c == '\r');
587 else{
588 seenx = FAL0;
589 goto jsoftnl;
593 /* Enforce soft line break if we haven't seen LF */
594 if(in->l > 0 && *--is != '\n'){
595 qp[0] = '=';
596 qp[1] = '\n';
597 qp += 2;
599 jleave:
600 out->l = PTR2SIZE(qp - out->s);
601 out->s[out->l] = '\0';
602 jerr:
603 NYD_LEAVE;
604 return out;
607 FL bool_t
608 qp_decode_header(struct str *out, struct str const *in){
609 struct n_string s;
610 char const *is, *ie;
611 NYD_ENTER;
613 /* EOVERFLOW */
614 if(UIZ_MAX -1 - out->l <= in->l ||
615 SI32_MAX <= out->l + in->l){ /* XXX wrong, we may replace */
616 out->l = 0;
617 out = NULL;
618 goto jleave;
621 n_string_creat(&s);
622 n_string_reserve(n_string_take_ownership(&s, out->s,
623 (out->l == 0 ? 0 : out->l +1), out->l),
624 in->l + (in->l >> 2));
626 for(is = in->s, ie = &is[in->l - 1]; is <= ie;){
627 si32_t c;
629 c = *is++;
630 if(c == '='){
631 if(is >= ie){
632 goto jpushc; /* TODO According to RFC 2045, 6.7,
633 * ++is; TODO we should warn the user, but have no context
634 * goto jehead; TODO to do so; can't over and over */
635 }else if((c = n_c_from_hex_base16(is)) >= 0){
636 is += 2;
637 goto jpushc;
638 }else{
639 /* Invalid according to RFC 2045, section 6.7 */
640 /* TODO Follow RFC 2045, 6.7 advise and simply put through */
641 c = '=';
642 goto jpushc;
643 /* TODO jehead:
644 * TODO if(n_psonce & n_PSO_UNICODE)
645 * n_string_push_buf(&s, n_unirepl, sizeof(n_unirepl) -1);
646 * TODO else{
647 * TODO c = '?';
648 * TODO goto jpushc;
649 * TODO }*/
651 }else{
652 jpushc:
653 if(c == '_' /* a_ME_US */)
654 c = ' ';
655 n_string_push_c(&s, (char)c);
659 out->s = n_string_cp(&s);
660 out->l = s.s_len;
661 n_string_gut(n_string_drop_ownership(&s));
662 jleave:
663 NYD_LEAVE;
664 return (out != NULL);
667 FL bool_t
668 qp_decode_part(struct str *out, struct str const *in, struct str *outrest,
669 struct str *inrest_or_null){
670 struct n_string s, *sp;
671 char const *is, *ie;
672 NYD_ENTER;
674 if(outrest->l != 0){
675 is = out->s;
676 *out = *outrest;
677 outrest->s = n_UNCONST(is);
678 outrest->l = 0;
681 /* EOVERFLOW */
682 if(UIZ_MAX -1 - out->l <= in->l ||
683 SI32_MAX <= out->l + in->l) /* XXX wrong, we may replace */
684 goto jerr;
686 sp = n_string_creat(&s);
687 sp = n_string_take_ownership(sp, out->s,
688 (out->l == 0 ? 0 : out->l +1), out->l);
689 sp = n_string_reserve(sp, in->l + (in->l >> 2));
691 for(is = in->s, ie = &is[in->l - 1]; is <= ie;){
692 si32_t c;
694 if((c = *is++) != '='){
695 jpushc:
696 n_string_push_c(&s, (char)c);
697 continue;
700 /* RFC 2045, 6.7:
701 * Therefore, when decoding a Quoted-Printable body, any
702 * trailing white space on a line must be deleted, as it will
703 * necessarily have been added by intermediate transport
704 * agents */
705 for(; is <= ie && blankchar(*is); ++is)
707 if(is >= ie){
708 /* Soft line break? */
709 if(*is == '\n')
710 goto jsoftnl;
711 goto jpushc; /* TODO According to RFC 2045, 6.7,
712 * ++is; TODO we should warn the user, but have no context
713 * goto jebody; TODO to do so; can't over and over */
716 /* Not a soft line break? */
717 if(*is != '\n'){
718 if((c = n_c_from_hex_base16(is)) >= 0){
719 is += 2;
720 goto jpushc;
722 /* Invalid according to RFC 2045, section 6.7 */
723 /* TODO Follow RFC 2045, 6.7 advise and simply put through */
724 c = '=';
725 goto jpushc;
726 /* TODO jebody:
727 * TODO if(n_psonce & n_PSO_UNICODE)
728 * n_string_push_buf(&s, n_unirepl, sizeof(n_unirepl) -1);
729 * TODO else{
730 * TODO c = '?';
731 * TODO goto jpushc;
732 * TODO }*/
735 /* CRLF line endings are encoded as QP, followed by a soft line break, so
736 * check for this special case, and simply forget we have seen one, so as
737 * not to end up with the entire DOS file in a contiguous buffer */
738 jsoftnl:
739 if(s.s_len > 0 && s.s_dat[s.s_len - 1] == '\n'){
740 #if 0 /* TODO qp_decode_part() we do not normalize CRLF
741 * TODO to LF because for that we would need
742 * TODO to know if we are about to write to
743 * TODO the display or do save the file!
744 * TODO 'hope the MIME/send layer rewrite will
745 * TODO offer the possibility to DTRT */
746 if(s.s_len > 1 && s.s_dat[s.s_len - 2] == '\r')
747 n_string_push_c(n_string_trunc(&s, s.s_len - 2), '\n');
748 #endif
749 break;
752 /* C99 */{
753 char *cp;
754 size_t l;
756 if((l = PTR2SIZE(ie - is)) > 0){
757 if(inrest_or_null == NULL)
758 goto jerr;
759 n_str_assign_buf(inrest_or_null, is, l);
761 cp = outrest->s;
762 outrest->s = n_string_cp(&s);
763 outrest->l = s.s_len;
764 n_string_drop_ownership(&s);
765 if(cp != NULL)
766 free(cp);
768 break;
771 out->s = n_string_cp(&s);
772 out->l = s.s_len;
773 n_string_gut(n_string_drop_ownership(&s));
774 jleave:
775 NYD_LEAVE;
776 return (out != NULL);
777 jerr:
778 out->l = 0;
779 out = NULL;
780 goto jleave;
783 FL size_t
784 b64_encode_calc_size(size_t len){
785 NYD2_ENTER;
786 if(len >= UIZ_MAX / 4)
787 len = UIZ_MAX;
788 else{
789 len = (len * 4) / 3;
790 len += (((len / B64_ENCODE_INPUT_PER_LINE) + 1) * 3);
791 len += 2 + 1; /* CRLF, \0 */
793 NYD2_LEAVE;
794 return len;
797 FL struct str *
798 b64_encode(struct str *out, struct str const *in, enum b64flags flags){
799 ui8_t const *p;
800 size_t i, lnlen;
801 char *b64;
802 NYD_ENTER;
804 assert(!(flags & B64_NOPAD) ||
805 !(flags & (B64_CRLF | B64_LF | B64_MULTILINE)));
807 p = (ui8_t const*)in->s;
809 if(!(flags & B64_BUF)){
810 if((i = b64_encode_calc_size(in->l)) == UIZ_MAX){
811 out = NULL;
812 goto jleave;
814 out->s = (flags & B64_SALLOC) ? salloc(i) : srealloc(out->s, i);
816 b64 = out->s;
818 if(!(flags & (B64_CRLF | B64_LF)))
819 flags &= ~B64_MULTILINE;
821 for(lnlen = 0, i = in->l; (ssize_t)i > 0; p += 3, i -= 3){
822 ui32_t a, b, c;
824 a = p[0];
825 b64[0] = a_me_b64_enctbl[a >> 2];
827 switch(i){
828 case 1:
829 b64[1] = a_me_b64_enctbl[((a & 0x3) << 4)];
830 b64[2] =
831 b64[3] = '=';
832 break;
833 case 2:
834 b = p[1];
835 b64[1] = a_me_b64_enctbl[((a & 0x03) << 4) | ((b & 0xF0u) >> 4)];
836 b64[2] = a_me_b64_enctbl[((b & 0x0F) << 2)];
837 b64[3] = '=';
838 break;
839 default:
840 b = p[1];
841 c = p[2];
842 b64[1] = a_me_b64_enctbl[((a & 0x03) << 4) | ((b & 0xF0u) >> 4)];
843 b64[2] = a_me_b64_enctbl[((b & 0x0F) << 2) | ((c & 0xC0u) >> 6)];
844 b64[3] = a_me_b64_enctbl[c & 0x3F];
845 break;
848 b64 += 4;
849 if(!(flags & B64_MULTILINE))
850 continue;
851 lnlen += 4;
852 if(lnlen < B64_LINESIZE)
853 continue;
855 lnlen = 0;
856 if(flags & B64_CRLF)
857 *b64++ = '\r';
858 if(flags & (B64_CRLF | B64_LF))
859 *b64++ = '\n';
862 if((flags & (B64_CRLF | B64_LF)) &&
863 (!(flags & B64_MULTILINE) || lnlen != 0)){
864 if(flags & B64_CRLF)
865 *b64++ = '\r';
866 if(flags & (B64_CRLF | B64_LF))
867 *b64++ = '\n';
868 }else if(flags & B64_NOPAD)
869 while(b64 != out->s && b64[-1] == '=')
870 --b64;
872 out->l = PTR2SIZE(b64 - out->s);
873 out->s[out->l] = '\0';
875 /* Base64 includes + and /, replace them with _ and -.
876 * This is base64url according to RFC 4648, then. Since we only support
877 * that for encoding and it is only used for boundary strings, this is
878 * yet a primitive implementation; xxx use tables; support decoding */
879 if(flags & B64_RFC4648URL){
880 char c;
882 for(b64 = out->s; (c = *b64) != '\0'; ++b64)
883 if(c == '+')
884 *b64 = '-';
885 else if(c == '/')
886 *b64 = '_';
888 jleave:
889 NYD_LEAVE;
890 return out;
893 FL struct str *
894 b64_encode_buf(struct str *out, void const *vp, size_t vp_len,
895 enum b64flags flags){
896 struct str in;
897 NYD_ENTER;
899 in.s = n_UNCONST(vp);
900 in.l = vp_len;
901 out = b64_encode(out, &in, flags);
902 NYD_LEAVE;
903 return out;
906 #ifdef notyet
907 FL struct str *
908 b64_encode_cp(struct str *out, char const *cp, enum b64flags flags){
909 struct str in;
910 NYD_ENTER;
912 in.s = n_UNCONST(cp);
913 in.l = strlen(cp);
914 out = b64_encode(out, &in, flags);
915 NYD_LEAVE;
916 return out;
918 #endif /* notyet */
920 FL bool_t
921 b64_decode(struct str *out, struct str const *in){
922 struct str work;
923 size_t len;
924 NYD_ENTER;
926 out->l = 0;
928 if((len = a_me_b64_decode_prepare(&work, in)) == UIZ_MAX)
929 goto jerr;
931 /* Ignore an empty input, as may happen for an empty final line */
932 if(work.l == 0)
933 out->s = srealloc(out->s, 1);
934 else if(work.l >= 4 && !(work.l & 3)){
935 out->s = srealloc(out->s, len +1);
936 if((ssize_t)(len = a_me_b64_decode(out, &work)) < 0)
937 goto jerr;
938 }else
939 goto jerr;
940 out->s[out->l] = '\0';
941 jleave:
942 NYD_LEAVE;
943 return (out != NULL);
944 jerr:
945 out = NULL;
946 goto jleave;
949 FL bool_t
950 b64_decode_header(struct str *out, struct str const *in){
951 struct str outr, inr;
952 NYD_ENTER;
954 if(!b64_decode(out, in)){
955 memset(&outr, 0, sizeof outr);
956 memset(&inr, 0, sizeof inr);
958 if(!b64_decode_part(out, in, &outr, &inr) || outr.l > 0 || inr.l > 0)
959 out = NULL;
961 if(inr.s != NULL)
962 free(inr.s);
963 if(outr.s != NULL)
964 free(outr.s);
966 NYD_LEAVE;
967 return (out != NULL);
970 FL bool_t
971 b64_decode_part(struct str *out, struct str const *in, struct str *outrest,
972 struct str *inrest_or_null){
973 struct str work, save;
974 ui32_t a, b, c, b64l;
975 char ca, cb, cc, cx;
976 struct n_string s, workbuf;
977 size_t len;
978 NYD_ENTER;
980 n_string_creat(&s);
981 if((len = out->l) > 0 && out->s[len] == '\0')
982 n_string_take_ownership(&s, out->s, len +1, len);
983 else{
984 if(len > 0)
985 n_string_push_buf(&s, out->s, len);
986 if(out->s != NULL)
987 free(out->s);
989 out->s = NULL, out->l = 0;
990 n_string_creat(&workbuf);
992 if((len = a_me_b64_decode_prepare(&work, in)) == UIZ_MAX)
993 goto jerr;
995 if(outrest->l > 0){
996 n_string_push_buf(&s, outrest->s, outrest->l);
997 outrest->l = 0;
1000 /* EOVERFLOW */
1001 if(UIZ_MAX - len <= s.s_len ||
1002 SI32_MAX <= len + s.s_len) /* XXX wrong, we may replace */
1003 goto jerr;
1005 if(work.l == 0)
1006 goto jok;
1008 /* This text decoder is extremely expensive, especially given that in all
1009 * but _invalid_ cases it is not even needed! So try once to do the normal
1010 * decoding, if that fails, go the hard way */
1011 save = work;
1012 out->s = n_string_resize(&s, len + (out->l = b64l = s.s_len))->s_dat;
1014 if(work.l >= 4 && a_me_b64_decode(out, &work) >= 0){
1015 n_string_trunc(&s, out->l);
1016 if(work.l == 0)
1017 goto jok;
1020 n_string_trunc(&s, b64l);
1021 work = save;
1022 out->s = NULL, out->l = 0;
1024 n_UNINIT(ca, 0);
1025 n_UNINIT(cb, 0);
1026 n_UNINIT(cc, 0);
1027 for(b64l = 0;;){
1028 ui32_t x;
1030 x = a_ME_B64_DECUI8((ui8_t)(cx = *work.s));
1031 switch(b64l){
1032 case 0:
1033 if(x >= a_ME_B64_EQU)
1034 goto jrepl;
1035 ca = cx;
1036 a = x;
1037 ++b64l;
1038 break;
1039 case 1:
1040 if(x >= a_ME_B64_EQU)
1041 goto jrepl;
1042 cb = cx;
1043 b = x;
1044 ++b64l;
1045 break;
1046 case 2:
1047 if(x == a_ME_B64_BAD)
1048 goto jrepl;
1049 cc = cx;
1050 c = x;
1051 ++b64l;
1052 break;
1053 case 3:
1054 if(x == a_ME_B64_BAD){
1055 jrepl:
1056 /* TODO This would be wrong since iconv(3) may be applied first! */
1057 #if 0
1058 if(n_psonce & n_PSO_UNICODE)
1059 n_string_push_buf(&s, n_unirepl, sizeof(n_unirepl) -1);
1060 else
1061 n_string_push_c(&s, '?');
1062 #endif
1064 }else if(c == a_ME_B64_EQU && x != a_ME_B64_EQU){
1065 /* This is not only invalid but bogus. Skip it over! */
1066 /* TODO This would be wrong since iconv(3) may be applied first! */
1067 #if 0
1068 n_string_push_buf(&s, n_UNIREPL n_UNIREPL n_UNIREPL n_UNIREPL,
1069 (sizeof(n_UNIREPL) -1) * 4);
1070 #endif
1071 b64l = 0;
1072 }else{
1073 n_string_push_c(&s, (char)((a << 2) | ((b & 0x30) >> 4)));
1074 n_string_push_c(&s, (char)(((b & 0x0F) << 4) | ((c & 0x3C) >> 2)));
1075 if(x != a_ME_B64_EQU)
1076 n_string_push_c(&s, (char)(((c & 0x03) << 6) | x));
1077 ++b64l;
1079 break;
1082 ++work.s;
1083 if(--work.l == 0){
1084 if(b64l > 0 && b64l != 4){
1085 if(inrest_or_null == NULL)
1086 goto jerr;
1087 inrest_or_null->s = srealloc(inrest_or_null->s, b64l +1);
1088 inrest_or_null->s[0] = ca;
1089 if(b64l > 1)
1090 inrest_or_null->s[1] = cb;
1091 if(b64l > 2)
1092 inrest_or_null->s[2] = cc;
1093 inrest_or_null->s[inrest_or_null->l = b64l] = '\0';
1095 goto jok;
1097 if(b64l == 4)
1098 b64l = 0;
1101 jok:
1102 out->s = n_string_cp(&s);
1103 out->l = s.s_len;
1104 n_string_drop_ownership(&s);
1105 jleave:
1106 n_string_gut(&workbuf);
1107 n_string_gut(&s);
1108 NYD_LEAVE;
1109 return (out != NULL);
1110 jerr:
1111 out = NULL;
1112 goto jleave;
1115 /* s-it-mode */