2 ** Routines to encode message headers using RFC 2047-encoding.
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/mhparse.h>
11 #include <h/addrsbr.h>
17 ** List of headers that contain addresses and as a result require special
21 static char *address_headers[] = {
38 ** Macros we use for parsing headers
40 ** Todo: convert the macros to functions
43 #define is_fws(c) (c == '\t' || c == ' ' || c == '\n')
45 #define qphrasevalid(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || \
46 (c >= 'a' && c <= 'z') || \
47 c == '!' || c == '*' || c == '+' || c == '-' || \
48 c == '/' || c == '=' || c == '_')
49 #define qpspecial(c) (c < ' ' || c == '=' || c == '?' || c == '_')
51 #define ENCODELINELIMIT 76
53 static void unfold_header(char **, int);
54 static int field_encode_address(const char *, char **, const char *);
55 static int field_encode_quoted(const char *, char **, const char *, int,
57 static int scanstring(const char *, int *, int *, int *);
58 static int utf8len(const char *);
61 ** Encode a message header using RFC 2047 encoding. We make the assumption
62 ** that all characters < 128 are ASCII and as a consequence don't need any
66 encode_rfc2047(const char *name, char **value, const char *charset)
68 int i, asciicount = 0, eightbitcount = 0, qpspecialcount = 0;
71 /* First, check to see if we even need to encode the header */
73 for (p = *value; *p != '\0'; p++) {
74 if (isascii((unsigned char) *p)) {
76 if (qpspecial((unsigned char) *p)) {
84 if (eightbitcount == 0) {
89 ** Some rules from RFC 2047:
91 ** - Encoded words cannot be more than 75 characters long
92 ** - Multiple "long" encoded words must be on new lines.
94 ** Also, we're not permitted to encode email addresses, so
95 ** we need to actually _parse_ email addresses and only encode
100 ** If charset was NULL, then get the value from the locale. But
101 ** we reject it if it returns US-ASCII
104 if (charset == NULL) {
105 charset = write_charset_8bit();
107 if (strcasecmp(charset, "US-ASCII") == 0) {
108 advise(NULL, "Cannot use US-ASCII with 8 bit characters in header");
113 ** If we have an address header, then we need to parse the addresses
114 ** and only encode the names or comments. Otherwise, handle it
118 for (i = 0; address_headers[i]; i++) {
119 if (strcasecmp(name, address_headers[i]) == 0) {
120 return field_encode_address(name, value, charset);
125 ** On the encoding we choose, and the specifics of encoding:
127 ** - If a specified encoding is passed in, we use that.
128 ** - Otherwise, pick which encoding is shorter.
130 ** We don't quite handle continuation right here, but it should be
134 unfold_header(value, asciicount + eightbitcount);
136 return field_encode_quoted(name, value, charset, asciicount,
137 eightbitcount + qpspecialcount, 0);
141 ** Encode our specified header (or field) using quoted-printable
145 field_encode_quoted(const char *name, char **value, const char *charset,
146 int ascii, int encoded, int phraserules)
148 int prefixlen = name ? strlen(name) + 2: 0;
149 int outlen = 0, column, newline = 1, utf8;
150 int charsetlen = strlen(charset);
151 char *output = NULL, *p, *q = NULL;
154 ** Right now we just encode the whole thing. Maybe later on we'll
155 ** only encode things on a per-atom basis.
160 column = prefixlen + 2; /* Header name plus ": " */
162 utf8 = strcasecmp(charset, "UTF-8") == 0;
165 /* Start a new line, if it's time */
170 ** If it's the start of the header, we don't need
173 ** The length of the output string is ...
174 ** =?charset?Q?...?= so that's
175 ** 7+strlen(charset) + 2 for \n NUL
177 ** plus 1 for every ASCII character and 3 for
178 ** every eight bit or special character (eight
179 ** bit characters are written as =XX).
181 outlen += 9 + charsetlen + ascii + 3 * encoded;
184 /* continue the header */
185 int curlen = q - output, i;
186 outlen += prefixlen + 1; /* Header plus \n ": " */
187 output = mh_xrealloc(output, outlen);
192 for (i = 0; i < prefixlen; i++) {
196 /* do the initial allocation */
198 ** A bit of a hack here; the header can
199 ** contain multiple spaces (probably at
200 ** least one) until we get to the actual
201 ** text. Copy until we get to a non-space.
203 output = mh_xcalloc(outlen, sizeof(char));
210 tokenlen = snprintf(q, outlen - (q - output),
213 column = prefixlen + tokenlen;
218 ** Process each character, encoding if necessary
220 ** Note that we have a different set of rules if we're
221 ** processing RFC 5322 'phrase' (something you'd see in
222 ** an address header).
230 } else if (isascii((unsigned char) *p) && (phraserules ?
231 qphrasevalid((unsigned char) *p)
232 : !qpspecial((unsigned char) *p))) {
236 snprintf(q, outlen - (q - output), "=%02X",
239 column += 2; /* column already incremented by 1 above */
245 if (prefixlen == 0) {
247 ** We haven't been passed in a header name,
248 ** so don't ever wrap the field (we're likely
249 ** doing an address).
254 ** We're not allowed more than ENCODELINELIMIT characters
255 ** per line, so reserve some room for the final ?=.
257 if (column >= ENCODELINELIMIT - 2) {
261 ** Okay, this is a bit weird, but to explain a
264 ** RFC 2047 prohibits the splitting of multibyte
265 ** characters across encoded words. Right now
266 ** we only handle the case of UTF-8, the most
267 ** common multibyte encoding.
269 ** p is now pointing at the next input character.
270 ** If we're using UTF-8 _and_ we'd go over
271 ** ENCODELINELIMIT given the length of the
272 ** complete character, then trigger a newline now.
273 ** Note that we check the length * 3 since we
274 ** have to allow for the encoded output.
276 if (column + (utf8len(p)*3) > ENCODELINELIMIT - 2) {
284 ** This should never happen, but just in case.
285 ** Found by clang static analyzer.
287 admonish (NULL, "null output encoding for %s", *value);
305 ** Calculate the length of a UTF-8 character.
307 ** If it's not a UTF-8 character (or we're in the middle of a multibyte
308 ** character) then simply return 0.
311 utf8len(const char *p)
318 if (isascii((unsigned char) *p) ||
319 (((unsigned char) *p) & 0xc0) == 0x80) {
323 while ((((unsigned char) *p++) & 0xc0) == 0x80) {
331 ** "Unfold" a header, making it a single line (without continuation)
333 ** We cheat a bit here; we never make the string longer, so using the
334 ** original length here is fine.
337 unfold_header(char **value, int len)
339 char *str = mh_xcalloc(len + 1, sizeof(char));
340 char *p = str, *q = *value;
345 ** When we get a newline, skip to the next
346 ** non-whitespace character and add a space to
347 ** replace all of the whitespace
349 ** This has the side effect of stripping off the
350 ** final newline for the header; we put it back
351 ** in the encoding routine.
371 ** Decode a header containing addresses. This means we have to parse
372 ** each address and only encode the display-name or comment field.
375 field_encode_address(const char *name, char **value, const char *charset)
377 int prefixlen = strlen(name) + 2;
378 int column = prefixlen, groupflag;
379 int asciichars, specialchars, eightbitchars;
380 int reformat = 0, errflag = 0;
382 char *mp, *cp = NULL, *output = NULL;
384 size_t tmpbufsize = 0;
389 ** Because these are addresses, we need to handle them individually.
391 ** Break them down and process them one by one. This means we
392 ** have to rewrite the whole header, but that's unavoidable.
396 ** The output headers always have to start with a space first;
397 ** this is just the way the API works right now.
400 output = add(" ", output);
402 for (groupflag = 0; (mp = getname(*value)); ) {
403 if ((mn = getm(mp, NULL, 0, AD_HOST, errbuf)) == NULL) {
404 advise(NULL, "%s: %s", errbuf, mp);
412 ** We only care if the phrase (m_pers) or any trailing
413 ** comment (m_note) have 8-bit characters. If doing q-p,
414 ** we also need to encode anything marked as qspecial().
415 ** Unquote it first so the specialchars count is right.
422 if ((len = strlen(mn->m_pers)) + 1 > tmpbufsize) {
423 tmpbuf = mh_xrealloc(tmpbuf, tmpbufsize = len + 1);
426 unquote_string(mn->m_pers, tmpbuf);
428 if (scanstring(tmpbuf, &asciichars, &eightbitchars,
431 ** If we have 8-bit characters, encode it.
435 ** This is okay, because the output of
436 ** unquote_string will be either equal or shorter
437 ** than the original.
439 strcpy(mn->m_pers, tmpbuf);
441 if (field_encode_quoted(NULL, &mn->m_pers, charset,
443 eightbitchars + specialchars, 1)) {
454 ** The "note" field is generally a comment at the end
455 ** of the address, at least as how it's implemented here.
456 ** Notes are always surrounded by parenthesis (since they're
457 ** comments). Strip them out and then put them back when
458 ** we format the final field, but they do not get encoded.
465 if ((len = strlen(mn->m_note)) + 1 > tmpbufsize) {
466 tmpbuf = mh_xrealloc(tmpbuf, tmpbufsize = len + 1);
469 if (mn->m_note[0] != '(' || mn->m_note[len - 1] != ')') {
470 advise(NULL, "Internal error: Invalid note field \"%s\"",
476 strncpy(tmpbuf, mn->m_note + 1, len - 1);
477 tmpbuf[len - 2] = '\0';
479 if (scanstring(tmpbuf, &asciichars, &eightbitchars,
482 ** If we have 8-bit characters, encode it.
485 if (field_encode_quoted(NULL, &tmpbuf, charset,
487 eightbitchars + specialchars, 1)) {
495 ** Make sure the size of tmpbuf is correct (it
496 ** always gets reallocated in the above functions).
499 tmpbufsize = strlen(tmpbuf) + 1;
502 ** Put the note field back surrounded by
506 mn->m_note = mh_xrealloc(mn->m_note, tmpbufsize + 2);
508 snprintf(mn->m_note, tmpbufsize + 2, "(%s)", tmpbuf);
514 ** So, some explanation is in order.
516 ** We know we need to rewrite at least one address in the
517 ** header, otherwise we wouldn't be here. If we had to
518 ** reformat this particular address, then run it through
519 ** adrformat(). Otherwise we can use m_text directly.
523 ** If we were in a group but are no longer, make sure we
524 ** add a semicolon (which needs to be FIRST, as it needs
525 ** to be at the end of the last address).
528 if (groupflag && ! mn->m_ingrp) {
529 output = add(";", output);
533 groupflag = mn->m_ingrp;
536 cp = add(mn->m_gname, NULL);
540 cp = add(adrformat(mn), cp);
542 cp = add(mn->m_text, cp);
548 ** If we're not at the beginning of the line, add a
549 ** command and either a space or a newline.
552 if (column != prefixlen) {
553 if (len + column + 2 > OUTPUTLINELEN) {
555 if ((size_t) (prefixlen + 3) < tmpbufsize) {
556 tmpbuf = mh_xrealloc(tmpbuf, tmpbufsize = prefixlen + 3);
559 snprintf(tmpbuf, tmpbufsize, ",\n%*s", column = prefixlen, "");
560 output = add(tmpbuf, output);
562 output = add(", ", output);
568 ** Finally add the address
571 output = add(cp, output);
577 ** Just in case we're at the end of a list
581 output = add(";", output);
584 output = add("\n", output);
603 ** Scan a string, check for characters that need to be encoded
607 scanstring(const char *string, int *asciilen, int *eightbitchars,
614 for (; *string != '\0'; string++) {
615 if ((isascii((unsigned char) *string))) {
618 ** So, a space is not a valid phrase character, but
619 ** we're counting an exception here, because in q-p
620 ** a space can be directly encoded as an underscore.
622 if (!qphrasevalid((unsigned char) *string) &&
631 return *eightbitchars > 0;