]> diplodocus.org Git - nmh/blobdiff - sbr/encode_rfc2047.c
Slightly clarify mhbuild man page.
[nmh] / sbr / encode_rfc2047.c
index 0df5f7064cb4cf82a79ec2c7d3a79e42b973c029..ac2b6dc3913974eb6bd4b59fb014da6d4e6c53f3 100644 (file)
@@ -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)++;