+
+ if (strcasecmp(charset, "UTF-8") == 0) {
+ /*
+ * p points to the start of our current buffer, so p + numencode
+ * is one past the last character to encode
+ */
+
+ while (numencode > 0 && ((*(p + numencode) & 0xc0) == 0x80))
+ numencode--;
+
+ if (numencode == 0) {
+ advise(NULL, "Internal error: could not find start of "
+ "UTF-8 character when base64 encoding header");
+ return 1;
+ }
+ }
+
+ if (writeBase64raw((unsigned char *) p, numencode,
+ (unsigned char *) q) != OK) {
+ advise(NULL, "Internal error: base64 encoding of header failed");
+ return 1;
+ }
+
+ p += numencode;
+ q += base64len(numencode);
+
+ /*
+ * This will point us at the beginning of the new line (trust me).
+ */
+
+ linestart = q + 3;
+
+ /*
+ * What's going on here? Well, we know we're continuing to the next
+ * line, so we want to add continuation padding. We also add the
+ * trailing marker for the RFC 2047 token at this time as well.
+ * This uses a trick of snprintf(); we tell it to print a zero-length
+ * string, but pad it out to prefixlen - 1 characters; that ends
+ * up always printing out the requested number of spaces. We use
+ * prefixlen - 1 because we always add a space on the starting
+ * token marker; this makes things work out correctly for the first
+ * line, which should have a space between the ':' and the start
+ * of the token.
+ *
+ * It's okay if you don't follow all of that.
+ */
+
+ q += snprintf(q, outlen - (q - output), "?=\n%*s", prefixlen - 1, "");
+ }
+
+ /*
+ * We're here if there is either no prefix, or we can fit it in less
+ * than ENCODELINELIMIT characters. Encode the whole thing.
+ */
+
+ outlen += prefixlen + 9 + charsetlen + base64len(strlen(p));
+ curlen = q - output;
+
+ output = mh_xrealloc(output, outlen);
+ q = output + curlen;
+
+ q += snprintf(q, outlen - (q - output), "%s=?%s?B?",
+ prefixlen ? " " : "", charset);
+
+ if (writeBase64raw((unsigned char *) p, strlen(p),
+ (unsigned char *) q) != OK) {
+ advise(NULL, "Internal error: base64 encoding of header failed");
+ return 1;