+ equals_pending = 1; /* we have a '=' pending */
+
+ /* Check for initial =? */
+ if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
+ startofmime = p + 2;
+
+ /* Scan ahead for the next '?' character */
+ for (pp = startofmime; *pp && *pp != '?'; pp++)
+ ;
+
+ if (!*pp)
+ continue;
+
+ /* Check if character set can be handled natively */
+ if (!check_charset(startofmime, pp - startofmime)) {
+#ifdef HAVE_ICONV
+ /* .. it can't. We'll use iconv then. */
+ *pp = '\0';
+ cd = iconv_open(get_charset(), startofmime);
+ fromutf8 = !mh_strcasecmp(startofmime, "UTF-8");
+ *pp = '?';
+ if (cd == (iconv_t)-1) continue;
+ use_iconv = 1;
+#else
+ continue;
+#endif
+ }
+
+ startofmime = pp + 1;
+
+ /* Check for valid encoding type */
+ if (*startofmime != 'B' && *startofmime != 'b' &&
+ *startofmime != 'Q' && *startofmime != 'q')
+ continue;
+
+ /* Is encoding quoted printable or base64? */
+ quoted_printable = (*startofmime == 'Q' || *startofmime == 'q');
+ startofmime++;
+
+ /* Check for next '?' character */
+ if (*startofmime != '?')
+ continue;
+ startofmime++;
+
+ /*
+ ** Scan ahead for the ending ?=
+ **
+ ** While doing this, we will also check if encoded
+ ** word has any embedded linear whitespace.
+ */
+ endofmime = NULL;
+ for (pp = startofmime; *pp && *(pp+1); pp++) {
+ if (is_lws(*pp)) {
+ break;
+ } else if (*pp == '?' && pp[1] == '=') {
+ endofmime = pp;
+ break;
+ }
+ }
+ if (is_lws(*pp) || endofmime == NULL)
+ continue;
+
+ /*
+ ** We've found an encoded word, so we can drop
+ ** the '=' that was pending
+ */
+ equals_pending = 0;
+
+ /*
+ ** If we are between two encoded words separated
+ ** only by linear whitespace, then we ignore
+ ** the whitespace. We will roll back the buffer
+ ** the number of whitespace characters we've seen
+ ** since last encoded word.
+ */
+ if (between_encodings) {
+ q -= whitespace;
+ dstlen += whitespace;
+ }
+
+#ifdef HAVE_ICONV
+ /*
+ ** empty encoded text. This ensures that we don't
+ ** malloc 0 bytes but skip on to the end
+ */
+ if (endofmime == startofmime && use_iconv) {
+ use_iconv = 0;
+ iconv_close(cd);
+ }
+
+ if (use_iconv) {
+ saveq = q;
+ savedstlen = dstlen;
+ q = convbuf = (char *) mh_xmalloc(endofmime - startofmime);
+ }
+/*
+** ADDCHR2 is for adding characters when q is or might be convbuf:
+** in this case on buffer-full we want to run iconv before returning.
+** I apologise for the dreadful name.
+*/
+# define ADDCHR2(C) do { *q++ = (C); dstlen--; if (!dstlen) goto iconvbuffull; } while (0)
+#else
+# define ADDCHR2(C) ADDCHR(C)
+#endif
+
+ /* Now decode the text */
+ if (quoted_printable) {
+ for (pp = startofmime; pp < endofmime; pp++) {
+ if (*pp == '=') {
+ c = unqp(pp[1], pp[2]);
+ if (c == -1)
+ continue;
+ if (c != 0)
+ *q++ = c;
+ pp += 2;
+ } else if (*pp == '_') {
+ ADDCHR2(' ');
+ } else {
+ ADDCHR2(*pp);
+ }
+ }
+ } else {
+ /* base64 */
+ int c1, c2, c3, c4;
+ c1 = c2 = c3 = c4 = -1;
+
+ pp = startofmime;
+ while (pp < endofmime) {
+ /* 6 + 2 bits */
+ while ((pp < endofmime) &&
+ ((c1 = char64(*pp)) == -1)) {
+ pp++;
+ }
+ if (pp < endofmime) {
+ pp++;
+ }
+ while ((pp < endofmime) &&
+ ((c2 = char64(*pp)) == -1)) {
+ pp++;
+ }
+ if (pp < endofmime && c1 != -1 && c2 != -1) {
+ ADDCHR2((c1 << 2) | (c2 >> 4));
+ pp++;
+ }
+ /* 4 + 4 bits */
+ while ((pp < endofmime) &&
+ ((c3 = char64(*pp)) == -1)) {
+ pp++;
+ }
+ if (pp < endofmime && c2 != -1 && c3 != -1) {
+ ADDCHR2(((c2 & 0xF) << 4) | (c3 >> 2));
+ pp++;
+ }
+ /* 2 + 6 bits */
+ while ((pp < endofmime) &&
+ ((c4 = char64(*pp)) == -1)) {
+ pp++;
+ }
+ if (pp < endofmime && c3 != -1 && c4 != -1) {
+ ADDCHR2(((c3 & 0x3) << 6) | (c4));
+ pp++;
+ }
+ }
+ }
+
+#ifdef HAVE_ICONV
+ iconvbuffull:
+ /*
+ ** NB that the string at convbuf is not necessarily
+ ** NUL terminated here:
+ ** q points to the first byte after the valid part.
+ */
+ /* Convert to native character set */
+ if (use_iconv) {
+ size_t inbytes = q - convbuf;
+ ICONV_CONST char *start = convbuf;
+
+ while (inbytes) {
+ if (iconv(cd, &start, &inbytes, &saveq, &savedstlen) ==
+ (size_t)-1) {
+ if (errno != EILSEQ)
+ break;
+ /*
+ ** character couldn't be
+ ** converted. we output a
+ ** `?' and try to carry on
+ ** which won't work if either
+ ** encoding was stateful
+ */
+ iconv(cd, 0, 0, &saveq, &savedstlen);
+ if (!savedstlen)
+ break;
+ *saveq++ = '?';
+ savedstlen--;
+ if (!savedstlen)
+ break;
+ /* skip to next input character */
+ if (fromutf8) {
+ for (start++;(start < q) && ((*start & 192) == 128);start++)
+ inbytes--;
+ } else
+ start++, inbytes--;
+ if (start >= q)
+ break;
+ }
+ }
+ q = saveq;
+ /*
+ ** Stop now if (1) we hit the end of the
+ ** buffer trying to do MIME decoding and
+ ** have just iconv-converted a partial
+ ** string or (2) our iconv-conversion hit
+ ** the end of the buffer.
+ */
+ if (!dstlen || !savedstlen)
+ goto buffull;
+ dstlen = savedstlen;
+ free(convbuf);
+ }
+#endif
+
+ /*
+ ** Now that we are done decoding this particular
+ ** encoded word, advance string to trailing '='.
+ */
+ p = endofmime + 1;
+
+ encoding_found = 1; /* we found (at least 1) encoded word */
+ between_encodings = 1; /* we have just decoded something */
+ whitespace = 0; /* re-initialize amount of whitespace */
+ }