X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/fc6ab5f54b16af90d04ca2dc70af82b9e97ebb9c..37c403585f3a2831347ab6785ab5cb86d5064433:/sbr/encode_rfc2047.c diff --git a/sbr/encode_rfc2047.c b/sbr/encode_rfc2047.c index 0df5f706..ac2b6dc3 100644 --- a/sbr/encode_rfc2047.c +++ b/sbr/encode_rfc2047.c @@ -125,14 +125,14 @@ encode_rfc2047(const char *name, char **value, int encoding, * On the encoding we choose, and the specifics of encoding: * * - If a specified encoding is passed in, we use that. - * - If more than 50% of the characters are high-bit, we use base64 - * and encode the whole field as one atom (possibly split). - * - Otherwise, we use quoted-printable. + * - Otherwise, pick which encoding is shorter. + * + * We don't quite handle continuation right here, but it should be + * pretty close. */ if (encoding == CE_UNKNOWN) - encoding = (eightbitcount * 10 / (asciicount + eightbitcount) > 5) ? - CE_BASE64 : CE_QUOTED; + encoding = pref_encoding(asciicount, qpspecialcount, eightbitcount); unfold_header(value, asciicount + eightbitcount); @@ -286,10 +286,13 @@ field_encode_quoted(const char *name, char **value, const char *charset, } } - strcat(q, "?="); + *q++ = '?'; + *q++ = '='; if (prefixlen) - strcat(q, "\n"); + *q++ = '\n'; + + *q = '\0'; free(*value); @@ -514,8 +517,8 @@ unfold_header(char **value, int len) * This has the side effect of stripping off the final newline * for the header; we put it back in the encoding routine. */ - while (is_fws(*q++)) - ; + while (is_fws(*q)) + q++; if (*q == '\0') break; @@ -711,13 +714,27 @@ do_reformat: * we can use m_text directly. */ + /* + * If we were in a group but are no longer, make sure we add a + * semicolon (which needs to be FIRST, as it needs to be at the end + * of the last address). + */ + + if (groupflag && ! mn->m_ingrp) { + output = add(";", output); + column += 1; + } + + groupflag = mn->m_ingrp; + + if (mn->m_gname) { + cp = add(mn->m_gname, NULL); + } + if (reformat) { - if (mn->m_gname) { - cp = add(mn->m_gname, NULL); - } cp = add(adrformat(mn), cp); } else { - cp = add(mn->m_text, NULL); + cp = add(mn->m_text, cp); } len = strlen(cp); @@ -749,21 +766,21 @@ do_reformat: column += len; free(cp); cp = NULL; + } - /* - * If we were in a group but are no longer, make sure we add a - * trailing semicolon. - */ - - if (groupflag && ! mn->m_ingrp) { - output = add(";", output); - } + /* + * Just in case we're at the end of a list + */ - groupflag = mn->m_ingrp; + if (groupflag) { + output = add(";", output); } - *value = output; - output = NULL; + output = add("\n", output); + + free(*value); + *value = output; + output = NULL; out: @@ -790,7 +807,12 @@ scanstring(const char *string, int *asciilen, int *eightbitchars, for (; *string != '\0'; string++) { if ((isascii((unsigned char) *string))) { (*asciilen)++; - if (!qphrasevalid((unsigned char) *string)) + /* + * So, a space is not a valid phrase character, but we're counting + * an exception here, because in q-p a space can be directly + * encoded as an underscore. + */ + if (!qphrasevalid((unsigned char) *string) && *string != ' ') (*specialchars)++; } else { (*eightbitchars)++;