+
+ 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);
+ *q++ = '?';
+ *q++ = '=';
+ *q++ = '\n';
+ *q++ = '\0';
+ }
+
+ /*
+ * 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?B?", charset);
+
+ if (writeBase64raw((unsigned char *) p, strlen(p),
+ (unsigned char *) q) != OK) {
+ advise(NULL, "Internal error: base64 encoding of header failed");
+ return 1;