1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ MIME support functions.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #ifndef HAVE_AMALGAMATION
46 static char *_cs_iter_base
, *_cs_iter
;
48 # define _CS_ITER_GET() ((_cs_iter != NULL) ? _cs_iter : charset_get_8bit())
50 # define _CS_ITER_GET() ((_cs_iter != NULL) ? _cs_iter : charset_get_lc())
52 #define _CS_ITER_STEP() _cs_iter = n_strsep(&_cs_iter_base, ',', TRU1)
54 /* Is 7-bit enough? */
56 static bool_t
_has_highbit(char const *s
);
57 static bool_t
_name_highbit(struct name
*np
);
60 /* fwrite(3) while checking for displayability */
61 static ssize_t
_fwrite_td(struct str
const *input
, enum tdflags flags
,
62 struct str
*outrest
, struct quoteflt
*qf
);
64 /* Convert header fields to RFC 1522 format and write to the file fo */
65 static ssize_t
mime_write_tohdr(struct str
*in
, FILE *fo
);
67 /* Write len characters of the passed string to the passed file, doing charset
68 * and header conversion */
69 static ssize_t
convhdra(char const *str
, size_t len
, FILE *fp
);
71 /* Write an address to a header field */
72 static ssize_t
mime_write_tohdr_a(struct str
*in
, FILE *f
);
74 /* Append to buf, handling resizing */
75 static void _append_str(char **buf
, size_t *sz
, size_t *pos
,
76 char const *str
, size_t len
);
77 static void _append_conv(char **buf
, size_t *sz
, size_t *pos
,
78 char const *str
, size_t len
);
82 _has_highbit(char const *s
)
100 _name_highbit(struct name
*np
)
106 if (_has_highbit(np
->n_name
) || _has_highbit(np
->n_fullname
))
115 #endif /* HAVE_ICONV */
117 static sigjmp_buf __mimefwtd_actjmp
; /* TODO someday.. */
118 static int __mimefwtd_sig
; /* TODO someday.. */
119 static sighandler_type __mimefwtd_opipe
;
121 __mimefwtd_onsig(int sig
) /* TODO someday, we won't need it no more */
123 NYD_X
; /* Signal handler */
124 __mimefwtd_sig
= sig
;
125 siglongjmp(__mimefwtd_actjmp
, 1);
129 _fwrite_td(struct str
const *input
, enum tdflags flags
, struct str
*outrest
,
132 /* TODO note: after send/MIME layer rewrite we will have a string pool
133 * TODO so that memory allocation count drops down massively; for now,
134 * TODO v14.* that is, we pay a lot & heavily depend on the allocator */
135 /* TODO well if we get a broken pipe here, and it happens to
136 * TODO happen pretty easy when sleeping in a full pipe buffer,
137 * TODO then the current codebase performs longjump away;
138 * TODO this leaves memory leaks behind ('think up to 3 per,
139 * TODO dep. upon alloca availability). For this to be fixed
140 * TODO we either need to get rid of the longjmp()s (tm) or
141 * TODO the storage must come from the outside or be tracked
142 * TODO in a carrier struct. Best both. But storage reuse
143 * TODO would be a bigbig win besides */
144 /* *input* _may_ point to non-modifyable buffer; but even then it only
145 * needs to be dup'ed away if we have to transform the content */
156 if ((flags
& TD_ICONV
) && iconvd
!= (iconv_t
)-1) {
159 if (outrest
!= NULL
&& outrest
->l
> 0) {
160 in
.l
= outrest
->l
+ input
->l
;
161 in
.s
= buf
= smalloc(in
.l
+1);
162 memcpy(in
.s
, outrest
->s
, outrest
->l
);
163 memcpy(&in
.s
[outrest
->l
], input
->s
, input
->l
);
167 if (n_iconv_str(iconvd
, n_ICONV_UNIDEFAULT
, &out
, &in
, &in
) != 0 &&
168 outrest
!= NULL
&& in
.l
> 0) {
169 n_iconv_reset(iconvd
);
170 /* Incomplete multibyte at EOF is special */
171 if (flags
& _TD_EOF
) {
172 out
.s
= srealloc(out
.s
, out
.l
+ sizeof(n_unirepl
));
173 if(options
& OPT_UNICODE
){
174 memcpy(&out
.s
[out
.l
], n_unirepl
, sizeof(n_unirepl
) -1);
175 out
.l
+= sizeof(n_unirepl
) -1;
177 out
.s
[out
.l
++] = '?';
179 n_str_add(outrest
, &in
);
184 flags
&= ~_TD_BUFCOPY
;
190 /* Else, if we will modify the data bytes and thus introduce the potential
191 * of messing up multibyte sequences which become splitted over buffer
192 * boundaries TODO and unless we don't have our filter chain which will
193 * TODO make these hacks go by, buffer data until we see a NL */
194 if((flags
& (TD_ISPR
| TD_DELCTRL
)) && outrest
!= NULL
&&
196 iconvd
== (iconv_t
)-1 &&
198 (!(flags
& _TD_EOF
) || outrest
->l
> 0)
203 for (cp
= &in
.s
[in
.l
]; cp
> in
.s
&& cp
[-1] != '\n'; --cp
)
205 i
= PTR2SIZE(cp
- in
.s
);
209 n_str_assign_buf(outrest
, cp
, in
.l
- i
);
211 memcpy(cp
, in
.s
, in
.l
= i
);
212 (in
.s
= cp
)[in
.l
= i
] = '\0';
213 flags
&= ~_TD_BUFCOPY
;
215 n_str_add_buf(outrest
, input
->s
, input
->l
);
223 makeprint(&in
, &out
);
224 else if (flags
& _TD_BUFCOPY
)
225 n_str_dup(&out
, &in
);
228 if (flags
& TD_DELCTRL
)
229 out
.l
= delctrl(out
.s
, out
.l
);
232 __mimefwtd_opipe
= safe_signal(SIGPIPE
, &__mimefwtd_onsig
);
233 if (sigsetjmp(__mimefwtd_actjmp
, 1)) {
238 rv
= quoteflt_push(qf
, out
.s
, out
.l
);
243 if (in
.s
!= input
->s
)
245 safe_signal(SIGPIPE
, __mimefwtd_opipe
);
246 if (__mimefwtd_sig
!= 0)
247 n_raise(__mimefwtd_sig
);
254 mime_write_tohdr(struct str
*in
, FILE *fo
)
256 /* TODO mime_write_tohdr(): we don't know the name of our header->maxcol..
257 * TODO MIME/send layer rewrite: more available state!!
258 * TODO Because of this we cannot make a difference in between structured
259 * TODO and unstructured headers (RFC 2047, 5. (2))
260 * TODO NOT MULTIBYTE SAFE IF AN ENCODED WORD HAS TO BE SPLITTED!
261 * TODO To be better we had to mbtowc_l() (non-std! and no locale!!) and
262 * TODO work char-wise! -> S-CText..
263 * TODO The real problem for STD compatibility is however that "in" is
264 * TODO already iconv(3) encoded to the target character set! We could
265 * TODO also solve it (very expensively!) if we would narrow down to an
266 * TODO encoded word and then iconv(3)+MIME encode in one go, in which
267 * TODO case multibyte errors could be catched! */
269 /* Maximum line length *//* XXX we are too inflexible and could use
270 * XXX MIME_LINELEN unless an RFC 2047 encoding was actually used */
271 _MAXCOL
= MIME_LINELEN_RFC2047
274 _FIRST
= 1<<0, /* Nothing written yet, start of string */
275 _NO_QP
= 1<<1, /* No quoted-printable allowed */
276 _NO_B64
= 1<<2, /* Ditto, base64 */
277 _ENC_LAST
= 1<<3, /* Last round generated encoded word */
278 _SHOULD_BEE
= 1<<4, /* Avoid lines longer than SHOULD via encoding */
280 _RND_MASK
= (1<<_RND_SHIFT
) - 1,
281 _SPACE
= 1<<(_RND_SHIFT
+1), /* Leading whitespace */
282 _8BIT
= 1<<(_RND_SHIFT
+2), /* High bit set */
283 _ENCODE
= 1<<(_RND_SHIFT
+3), /* Need encoding */
284 _ENC_B64
= 1<<(_RND_SHIFT
+4), /* - let it be base64 */
285 _OVERLONG
= 1<<(_RND_SHIFT
+5) /* Temporarily rised limit */
288 struct str cout
, cin
;
289 char const *cset7
, *cset8
, *wbot
, *upper
, *wend
, *wcur
;
290 ui32_t cset7_len
, cset8_len
;
295 cout
.s
= NULL
, cout
.l
= 0;
296 cset7
= charset_get_7bit();
297 cset7_len
= (ui32_t
)strlen(cset7
);
298 cset8
= _CS_ITER_GET(); /* TODO MIME/send layer: iter active? iter! else */
299 cset8_len
= (ui32_t
)strlen(cset8
);
301 /* RFC 1468, "MIME Considerations":
302 * ISO-2022-JP may also be used in MIME Part 2 headers. The "B"
303 * encoding should be used with ISO-2022-JP text. */
304 /* TODO of course, our current implementation won't deal properly with
305 * TODO any stateful encoding at all... (the standard says each encoded
306 * TODO word must include all necessary reset sequences..., i.e., each
307 * TODO encoded word must be a self-contained iconv(3) life cycle) */
308 if (!asccasecmp(cset8
, "iso-2022-jp") || mime_enc_target() == MIMEE_B64
)
312 upper
= wbot
+ in
->l
;
313 col
= sizeof("Mail-Followup-To: ") -1; /* dreadful thing */
315 for (sz
= 0; wbot
< upper
; flags
&= ~_FIRST
, wbot
= wend
) {
318 while (wcur
< upper
&& whitechar(*wcur
)) {
323 /* Any occurrence of whitespace resets prevention of lines >SHOULD via
324 * enforced encoding (xxx SHOULD, but.. encoding is expensive!!) */
326 flags
&= ~_SHOULD_BEE
;
328 /* Data ends with WS - dump it and done.
329 * Also, if we have seen multiple successive whitespace characters, then
330 * if there was no encoded word last, i.e., if we can simply take them
331 * over to the output as-is, keep one WS for possible later separation
332 * purposes and simply print the others as-is, directly! */
337 if ((flags
& (_ENC_LAST
| _SPACE
)) == _SPACE
&& wcur
- wbot
> 1) {
342 /* Skip over a word to next non-whitespace, keep track along the way
343 * whether our 7-bit charset suffices to represent the data */
344 for (wend
= wcur
; wend
< upper
; ++wend
) {
345 if (whitechar(*wend
))
347 if ((uc_i
)*wend
& 0x80)
351 /* Decide whether the range has to become encoded or not */
352 i
= PTR2SIZE(wend
- wcur
);
353 j
= mime_enc_mustquote(wcur
, i
, MIMEEF_ISHEAD
);
354 /* If it just cannot fit on a SHOULD line length, force encode */
356 flags
|= _SHOULD_BEE
; /* (Sigh: SHOULD only, not MUST..) */
359 if ((flags
& _SHOULD_BEE
) || j
> 0) {
362 /* Use base64 if requested or more than 50% -37.5-% of the bytes of
363 * the string need to be encoded */
364 if ((flags
& _NO_QP
) || j
>= i
>> 1)/*(i >> 2) + (i >> 3))*/
367 DBG( if (flags
& _8BIT
) assert(flags
& _ENCODE
); )
369 if (!(flags
& _ENCODE
)) {
370 /* Encoded word produced, but no linear whitespace for necessary RFC
371 * 2047 separation? Generate artificial data (bad standard!) */
372 if ((flags
& (_ENC_LAST
| _SPACE
)) == _ENC_LAST
) {
373 if (col
>= _MAXCOL
) {
386 /* todo No effort here: (1) v15.0 has to bring complete rewrite,
387 * todo (2) the standard is braindead and (3) usually this is one
388 * todo word only, and why be smarter than the standard? */
390 i
= PTR2SIZE(wend
- wbot
);
391 if (i
+ col
<= (flags
& _OVERLONG
? MIME_LINELEN_MAX
: _MAXCOL
)) {
392 i
= fwrite(wbot
, sizeof *wbot
, i
, fo
);
398 /* Doesn't fit, try to break the line first; */
401 if (whitechar(*wbot
)) {
402 putc((uc_i
)*wbot
, fo
);
405 putc(' ', fo
); /* Bad standard: artificial data! */
412 /* It is so long that it needs to be broken, effectively causing
413 * artificial spaces to be inserted (bad standard), yuck */
414 /* todo This is not multibyte safe, as above; and completely stupid
415 * todo P.S.: our _SHOULD_BEE prevents these cases in the meanwhile */
416 /* FIXME OPT_UNICODE and parse using UTF-8 sync possibility! */
417 wcur
= wbot
+ MIME_LINELEN_MAX
- 8;
422 /* Encoding to encoded word(s); deal with leading whitespace, place
423 * a separator first as necessary: encoded words must always be
424 * separated from text and other encoded words with linear WS.
425 * And if an encoded word was last, intermediate whitespace must
426 * also be encoded, otherwise it would get stripped away! */
427 wcur
= n_UNCONST(n_empty
);
428 if ((flags
& (_ENC_LAST
| _SPACE
)) != _SPACE
) {
429 /* Reinclude whitespace */
431 /* We don't need to place a separator at the very beginning */
432 if (!(flags
& _FIRST
))
433 wcur
= n_UNCONST(" ");
438 pstate
|= PS_HEADER_NEEDED_MIME
;
441 * An 'encoded-word' may not be more than 75 characters long,
442 * including 'charset', 'encoding', 'encoded-text', and
443 * delimiters. If it is desirable to encode more text than will
444 * fit in an 'encoded-word' of 75 characters, multiple
445 * 'encoded-word's (separated by CRLF SPACE) may be used.
447 * While there is no limit to the length of a multiple-line
448 * header field, each line of a header field that contains one
449 * or more 'encoded-word's is limited to 76 characters */
451 cin
.s
= n_UNCONST(wbot
);
452 cin
.l
= PTR2SIZE(wend
- wbot
);
458 xout
= b64_encode(&cout
, &cin
, B64_ISHEAD
| B64_ISENCWORD
);
460 xout
= qp_encode(&cout
, &cin
, QP_ISHEAD
| QP_ISENCWORD
);
467 /* (Avoid trigraphs in the RFC 2047 placeholder..) */
468 i
= j
+ (flags
& _8BIT
? cset8_len
: cset7_len
) + sizeof("=!!B!!=") -1;
473 /* Unfortunately RFC 2047 explicitly disallows encoded words to be
474 * longer (just like RFC 5322's "a line SHOULD fit in 78 but MAY be
475 * 998 characters long"), so we cannot use the _OVERLONG mechanism,
476 * even though all tested mailers seem to support it */
477 if (i
+ col
<= (/*flags & _OVERLONG ? MIME_LINELEN_MAX :*/ _MAXCOL
)) {
478 fprintf(fo
, "%.1s=?%s?%c?%.*s?=",
479 wcur
, (flags
& _8BIT
? cset8
: cset7
),
480 (flags
& _ENC_B64
? 'B' : 'Q'),
481 (int)cout
.l
, cout
.s
);
487 /* Doesn't fit, try to break the line first */
488 /* TODO I've commented out the _FIRST test since we (1) cannot do
489 * TODO _OVERLONG since (MUAs support but) the standard disallows,
490 * TODO and because of our iconv problem i prefer an empty first line
491 * TODO in favour of a possibly messed up multibytes character. :-( */
492 if (col
> 1 /* TODO && !(flags & _FIRST)*/) {
496 if (!(flags
& _SPACE
)) {
498 wcur
= n_UNCONST(n_empty
);
499 /*flags |= _OVERLONG;*/
500 goto jenc_retry_same
;
502 putc((uc_i
)*wcur
, fo
);
503 if (whitechar(*(wcur
= wbot
)))
507 wcur
= n_UNCONST(n_empty
);
509 /*flags &= ~_OVERLONG;*/
514 /* It is so long that it needs to be broken, effectively causing
515 * artificial data to be inserted (bad standard), yuck */
516 /* todo This is not multibyte safe, as above */
517 /*if (!(flags & _OVERLONG)) { Mechanism explicitly forbidden by 2047
522 /* FIXME OPT_UNICODE and parse using UTF-8 sync possibility! */
523 i
= PTR2SIZE(wend
- wbot
) + !!(flags
& _SPACE
);
524 j
= 3 + !(flags
& _ENC_B64
);
528 /* (Note the problem most likely is the transfer-encoding blow,
529 * which is why we test this *after* the decrements.. */
544 convhdra(char const *str
, size_t len
, FILE *fp
)
553 cin
.s
= n_UNCONST(str
);
557 if (iconvd
!= (iconv_t
)-1) {
559 if(n_iconv_str(iconvd
, n_ICONV_IGN_NOREVERSE
, &ciconv
, &cin
, NULL
) != 0){
560 n_iconv_reset(iconvd
);
566 ret
= mime_write_tohdr(&cin
, fp
);
569 if (ciconv
.s
!= NULL
)
577 mime_write_tohdr_a(struct str
*in
, FILE *f
) /* TODO error handling */
579 char const *cp
, *lastcp
;
585 if ((cp
= routeaddr(in
->s
)) != NULL
&& cp
> lastcp
) {
586 if ((sz
= convhdra(lastcp
, PTR2SIZE(cp
- lastcp
), f
)) < 0)
594 for ( ; *cp
!= '\0'; ++cp
) {
597 sz
+= fwrite(lastcp
, 1, PTR2SIZE(cp
- lastcp
+ 1), f
);
599 cp
= skip_comment(cp
);
601 if ((x
= convhdra(lastcp
, PTR2SIZE(cp
- lastcp
), f
)) < 0) {
613 if (*cp
== '\\' && cp
[1] != '\0')
620 sz
+= fwrite(lastcp
, 1, PTR2SIZE(cp
- lastcp
), f
);
627 _append_str(char **buf
, size_t *sz
, size_t *pos
, char const *str
, size_t len
)
630 *buf
= srealloc(*buf
, *sz
+= len
);
631 memcpy(&(*buf
)[*pos
], str
, len
);
637 _append_conv(char **buf
, size_t *sz
, size_t *pos
, char const *str
, size_t len
)
642 in
.s
= n_UNCONST(str
);
644 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
);
645 _append_str(buf
, sz
, pos
, out
.s
, out
.l
);
651 charset_get_7bit(void)
656 if ((t
= ok_vlook(charset_7bit
)) == NULL
)
664 charset_get_8bit(void)
669 if ((t
= ok_vlook(CHARSET_8BIT_OKEY
)) == NULL
)
682 if ((t
= ok_vlook(ttycharset
)) == NULL
)
689 charset_iter_reset(char const *a_charset_to_try_first
)
692 size_t sarrl
[3], len
;
695 n_UNUSED(a_charset_to_try_first
);
698 sarr
[0] = a_charset_to_try_first
;
699 if ((sarr
[1] = ok_vlook(sendcharsets
)) == NULL
&&
700 ok_blook(sendcharsets_else_ttycharset
))
701 sarr
[1] = charset_get_lc();
702 sarr
[2] = charset_get_8bit();
704 sarr
[2] = charset_get_lc();
707 sarrl
[2] = len
= strlen(sarr
[2]);
709 if ((cp
= n_UNCONST(sarr
[1])) != NULL
)
710 len
+= (sarrl
[1] = strlen(cp
));
713 if ((cp
= n_UNCONST(sarr
[0])) != NULL
)
714 len
+= (sarrl
[0] = strlen(cp
));
719 _cs_iter_base
= cp
= salloc(len
+ 1 + 1 +1);
722 if ((len
= sarrl
[0]) != 0) {
723 memcpy(cp
, sarr
[0], len
);
727 if ((len
= sarrl
[1]) != 0) {
728 memcpy(cp
, sarr
[1], len
);
734 memcpy(cp
, sarr
[2], len
);
739 return (_cs_iter
!= NULL
);
743 charset_iter_next(void)
749 rv
= (_cs_iter
!= NULL
);
755 charset_iter_is_valid(void)
760 rv
= (_cs_iter
!= NULL
);
777 charset_iter_or_fallback(void)
788 charset_iter_recurse(char *outer_storage
[2]) /* TODO LEGACY FUN, REMOVE */
791 outer_storage
[0] = _cs_iter_base
;
792 outer_storage
[1] = _cs_iter
;
797 charset_iter_restore(char *outer_storage
[2]) /* TODO LEGACY FUN, REMOVE */
800 _cs_iter_base
= outer_storage
[0];
801 _cs_iter
= outer_storage
[1];
807 need_hdrconv(struct header
*hp
) /* TODO once only, then iter */
809 struct n_header_field
*hfp
;
815 if((hfp
= hp
->h_user_headers
) != NULL
)
816 do if(_has_highbit(hfp
->hf_dat
+ hfp
->hf_nl
+1))
818 while((hfp
= hfp
->hf_next
) != NULL
);
820 if((hfp
= hp
->h_custom_headers
) != NULL
||
821 (hp
->h_custom_headers
= hfp
= n_customhdr_query()) != NULL
)
822 do if(_has_highbit(hfp
->hf_dat
+ hfp
->hf_nl
+1))
824 while((hfp
= hfp
->hf_next
) != NULL
);
826 if (hp
->h_mft
!= NULL
) {
827 if (_name_highbit(hp
->h_mft
))
830 if (hp
->h_from
!= NULL
) {
831 if (_name_highbit(hp
->h_from
))
833 } else if (_has_highbit(myaddrs(NULL
)))
836 if (_name_highbit(hp
->h_replyto
))
838 } else if (_has_highbit(ok_vlook(replyto
)))
841 if (_name_highbit(hp
->h_sender
))
843 } else if (_has_highbit(ok_vlook(sender
)))
846 if (_name_highbit(hp
->h_to
))
848 if (_name_highbit(hp
->h_cc
))
850 if (_name_highbit(hp
->h_bcc
))
852 if (_has_highbit(hp
->h_subject
))
854 rv
= _CS_ITER_GET(); /* TODO MIME/send: iter active? iter! else */
858 #endif /* HAVE_ICONV */
861 mime_fromhdr(struct str
const *in
, struct str
*out
, enum tdflags flags
)
863 /* TODO mime_fromhdr(): is called with strings that contain newlines;
864 * TODO this is the usual newline problem all around the codebase;
865 * TODO i.e., if we strip it, then the display misses it ;>
866 * TODO this is why it is so messy and why S-nail v14.2 plus additional
867 * TODO patch for v14.5.2 (and maybe even v14.5.3 subminor) occurred, and
868 * TODO why our display reflects what is contained in the message: the 1:1
869 * TODO relationship of message content and display!
870 * TODO instead a header line should be decoded to what it is (a single
871 * TODO line that is) and it should be objective to the backend whether
872 * TODO it'll be folded to fit onto the display or not, e.g., for search
873 * TODO purposes etc. then the only condition we have to honour in here
874 * TODO is that whitespace in between multiple adjacent MIME encoded words
875 * TODO á la RFC 2047 is discarded; i.e.: this function should deal with
876 * TODO RFC 2047 and be renamed: mime_fromhdr() -> mime_rfc2047_decode() */
877 struct str cin
, cout
;
878 char *p
, *op
, *upper
;
879 ui32_t convert
, lastenc
, lastoutl
;
883 iconv_t fhicd
= (iconv_t
)-1;
889 *(out
->s
= smalloc(1)) = '\0';
895 tcs
= charset_get_lc();
899 lastenc
= lastoutl
= 0;
903 if (*p
== '=' && *(p
+ 1) == '?') {
908 while (p
< upper
&& *p
!= '?')
909 ++p
; /* strip charset */
914 if (flags
& TD_ICONV
) {
915 size_t i
= PTR2SIZE(p
- cbeg
);
916 char *ltag
, *cs
= ac_alloc(i
);
918 memcpy(cs
, cbeg
, --i
);
920 /* RFC 2231 extends the RFC 2047 character set definition in
921 * encoded words by language tags - silently strip those off */
922 if ((ltag
= strchr(cs
, '*')) != NULL
)
925 if (fhicd
!= (iconv_t
)-1)
926 n_iconv_close(fhicd
);
927 fhicd
= asccasecmp(cs
, tcs
) ? n_iconv_open(tcs
, cs
) : (iconv_t
)-1;
933 convert
= CONV_FROMB64
;
936 convert
= CONV_FROMQP
;
938 default: /* invalid, ignore */
946 if (PTRCMP(p
+ 1, >=, upper
))
948 if (*p
++ == '?' && *p
== '=')
957 if (convert
== CONV_FROMB64
) {
958 if(!b64_decode_header(&cout
, &cin
))
959 n_str_assign_cp(&cout
, _("[Invalid Base64 encoding]"));
960 }else if(!qp_decode_header(&cout
, &cin
))
961 n_str_assign_cp(&cout
, _("[Invalid Quoted-Printable encoding]"));
965 if ((flags
& TD_ICONV
) && fhicd
!= (iconv_t
)-1) {
966 cin
.s
= NULL
, cin
.l
= 0; /* XXX string pool ! */
967 convert
= n_iconv_str(fhicd
, n_ICONV_UNIDEFAULT
, &cin
, &cout
, NULL
);
968 out
= n_str_add(out
, &cin
);
969 if (convert
) {/* EINVAL at EOS */
970 n_iconv_reset(fhicd
);
971 out
= n_str_add_buf(out
, "?", 1); /* TODO unicode replacement */
976 out
= n_str_add(out
, &cout
);
977 lastenc
= lastoutl
= out
->l
;
984 onlyws
= (lastenc
> 0);
988 if (op
[0] == '=' && (PTRCMP(op
+ 1, ==, upper
) || op
[1] == '?'))
990 if (onlyws
&& !blankchar(*op
))
994 out
= n_str_add_buf(out
, p
, PTR2SIZE(op
- p
));
996 if (!onlyws
|| lastoutl
!= lastenc
)
1001 out
->s
[out
->l
] = '\0';
1003 if (flags
& TD_ISPR
) {
1004 makeprint(out
, &cout
);
1008 if (flags
& TD_DELCTRL
)
1009 out
->l
= delctrl(out
->s
, out
->l
);
1011 if (fhicd
!= (iconv_t
)-1)
1012 n_iconv_close(fhicd
);
1020 mime_fromaddr(char const *name
)
1022 char const *cp
, *lastcp
;
1024 size_t ressz
= 1, rescur
= 0;
1029 if (*name
== '\0') {
1030 res
= savestr(name
);
1034 if ((cp
= routeaddr(name
)) != NULL
&& cp
> name
) {
1035 _append_conv(&res
, &ressz
, &rescur
, name
, PTR2SIZE(cp
- name
));
1040 for ( ; *cp
; ++cp
) {
1043 _append_str(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
+ 1));
1045 cp
= skip_comment(cp
);
1047 _append_conv(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
));
1054 if (*cp
== '\\' && cp
[1] != '\0')
1061 _append_str(&res
, &ressz
, &rescur
, lastcp
, PTR2SIZE(cp
- lastcp
));
1062 /* TODO rescur==0: inserted to silence Coverity ...; check that */
1064 res
= n_UNCONST(n_empty
);
1077 xmime_write(char const *ptr
, size_t size
, FILE *f
, enum conversion convert
,
1078 enum tdflags dflags
)
1081 struct quoteflt
*qf
;
1084 quoteflt_reset(qf
= quoteflt_dummy(), f
);
1085 rv
= mime_write(ptr
, size
, f
, convert
, dflags
, qf
, NULL
, NULL
);
1091 static sigjmp_buf __mimemw_actjmp
; /* TODO someday.. */
1092 static int __mimemw_sig
; /* TODO someday.. */
1093 static sighandler_type __mimemw_opipe
;
1095 __mimemw_onsig(int sig
) /* TODO someday, we won't need it no more */
1097 NYD_X
; /* Signal handler */
1099 siglongjmp(__mimemw_actjmp
, 1);
1103 mime_write(char const *ptr
, size_t size
, FILE *f
,
1104 enum conversion convert
, enum tdflags
volatile dflags
,
1105 struct quoteflt
*qf
, struct str
* volatile outrest
,
1106 struct str
* volatile inrest
)
1108 /* TODO note: after send/MIME layer rewrite we will have a string pool
1109 * TODO so that memory allocation count drops down massively; for now,
1110 * TODO v14.0 that is, we pay a lot & heavily depend on the allocator */
1112 ssize_t
volatile sz
;
1115 dflags
|= _TD_BUFCOPY
;
1117 in
.s
= n_UNCONST(ptr
);
1119 if(inrest
!= NULL
&& inrest
->l
> 0){
1120 out
.s
= smalloc(inrest
->l
+ size
+ 1);
1121 memcpy(out
.s
, inrest
->s
, inrest
->l
);
1123 memcpy(&out
.s
[inrest
->l
], in
.s
, size
);
1126 (in
.s
= out
.s
)[in
.l
= size
] = '\0';
1127 dflags
&= ~_TD_BUFCOPY
;
1133 if ((sz
= size
) == 0) {
1134 if (outrest
!= NULL
&& outrest
->l
!= 0)
1140 if ((dflags
& TD_ICONV
) && iconvd
!= (iconv_t
)-1 &&
1141 (convert
== CONV_TOQP
|| convert
== CONV_8BIT
||
1142 convert
== CONV_TOB64
|| convert
== CONV_TOHDR
)) {
1143 if (n_iconv_str(iconvd
, n_ICONV_IGN_NOREVERSE
, &out
, &in
, NULL
) != 0) {
1144 n_iconv_reset(iconvd
);
1145 /* TODO This causes hard-failure. We would need to have an action
1146 * TODO policy FAIL|IGNORE|SETERROR(but continue). Better huh? */
1152 dflags
&= ~_TD_BUFCOPY
;
1158 __mimemw_opipe
= safe_signal(SIGPIPE
, &__mimemw_onsig
);
1159 if (sigsetjmp(__mimemw_actjmp
, 1))
1164 if(!qp_decode_part(&out
, &in
, outrest
, inrest
)){
1165 n_err(_("Invalid Quoted-Printable encoding ignored\n"));
1166 sz
= 0; /* TODO sz = -1 stops outer levels! */
1171 if(qp_encode(&out
, &in
, QP_NONE
) == NULL
){
1172 sz
= 0; /* TODO sz = -1 stops outer levels! */
1177 sz
= quoteflt_push(qf
, in
.s
, in
.l
);
1180 if(!b64_decode_part(&out
, &in
, outrest
, inrest
))
1185 case CONV_FROMB64_T
:
1186 if(!b64_decode_part(&out
, &in
, outrest
, inrest
)){
1188 n_err(_("Invalid Base64 encoding ignored\n"));
1189 sz
= 0; /* TODO sz = -1 stops outer levels! */
1194 if ((sz
= out
.l
) != 0) {
1195 ui32_t opl
= qf
->qf_pfix_len
;
1196 sz
= _fwrite_td(&out
, (dflags
& ~_TD_BUFCOPY
), outrest
, qf
);
1197 qf
->qf_pfix_len
= opl
;
1201 if(b64_encode(&out
, &in
, B64_LF
| B64_MULTILINE
) == NULL
){
1206 sz
= fwrite(out
.s
, sizeof *out
.s
, out
.l
, f
);
1207 if (sz
!= (ssize_t
)out
.l
)
1211 mime_fromhdr(&in
, &out
, TD_ISPR
| TD_ICONV
| (dflags
& TD_DELCTRL
));
1212 sz
= quoteflt_push(qf
, out
.s
, out
.l
);
1215 sz
= mime_write_tohdr(&in
, f
);
1218 sz
= mime_write_tohdr_a(&in
, f
);
1221 sz
= _fwrite_td(&in
, dflags
, NULL
, qf
);
1229 safe_signal(SIGPIPE
, __mimemw_opipe
);
1230 if (__mimemw_sig
!= 0)
1231 n_raise(__mimemw_sig
);