4 * This module contains decoding routines for converting
5 * quoted-printable data into pure 8-bit data, in MIME
8 * By Henrik Storner <storner@image.dk>
10 * Configuration file support for fetchmail 4.3.8 by
11 * Frank Damgaard <frda@post3.tele.dk>
20 #include "fetchmail.h"
22 static unsigned char unhex(unsigned char c)
24 if ((c >= '0') && (c <= '9'))
26 else if ((c >= 'A') && (c <= 'F'))
27 return (c - 'A' + 10);
28 else if ((c >= 'a') && (c <= 'f'))
29 return (c - 'a' + 10);
34 static int qp_char(unsigned char c1, unsigned char c2, unsigned char *c_out)
39 if ((c1 > 15) || (c2 > 15))
50 * Routines to decode MIME QP-encoded headers, as per RFC 2047.
53 /* States of the decoding state machine */
54 #define S_COPY_PLAIN 0 /* Just copy, but watch for the QP flag */
55 #define S_SKIP_MIMEINIT 1 /* Get the encoding, and skip header */
56 #define S_COPY_MIME 2 /* Decode a sequence of coded characters */
58 static const char MIMEHDR_INIT[] = "=?"; /* Start of coded sequence */
59 static const char MIMEHDR_END[] = "?="; /* End of coded sequence */
61 void UnMimeHeader(unsigned char *hdr)
63 /* Decode a buffer containing data encoded according to RFC
64 * 2047. This only handles content-transfer-encoding; conversion
65 * between character sets is not implemented. In other words: We
66 * assume the charsets used can be displayed by your mail program
70 /* Note: Decoding is done "in-situ", i.e. without using an
71 * additional buffer for temp. storage. This is possible, since the
72 * decoded string will always be shorter than the encoded string,
73 * due to the en- coding scheme.
76 int state = S_COPY_PLAIN;
77 unsigned char *p_in, *p_out, *p;
78 unsigned char enc = '\0'; /* initialization pacifies -Wall */
81 /* Speed up in case this is not a MIME-encoded header */
82 p = strstr(hdr, MIMEHDR_INIT);
84 return; /* No MIME header */
86 /* Loop through the buffer.
87 * p_in : Next char to be processed.
88 * p_out: Where to put the next processed char
89 * enc : Encoding used (usually, 'q' = quoted-printable)
91 for (p_out = p_in = hdr; (*p_in); ) {
94 p = strstr(p_in, MIMEHDR_INIT);
97 * No more coded data in buffer,
98 * just move remainder into place.
100 i = strlen(p_in); /* How much left */
101 memmove(p_out, p_in, i);
102 p_in += i; p_out += i;
105 /* MIME header init found at location p */
107 /* There are some uncoded chars at the beginning. */
109 memmove(p_out, p_in, i);
113 state = S_SKIP_MIMEINIT;
117 case S_SKIP_MIMEINIT:
118 /* Mime type definition: "charset?encoding?" */
119 p = strchr(p_in, '?');
121 /* p_in .. (p-1) holds the charset */
123 /* *(p+1) is the transfer encoding, *(p+2) must be a '?' */
125 enc = tolower(*(p+1));
130 state = S_COPY_PLAIN;
133 state = S_COPY_PLAIN; /* Invalid data */
137 p = strstr(p_in, MIMEHDR_END); /* Find end of coded data */
138 if (p == NULL) p = p_in + strlen(p_in);
139 for (; (p_in < p); ) {
140 /* Decode all encoded data */
143 /* Decode one char qp-coded at (p_in+1) and (p_in+2) */
144 if (qp_char(*(p_in+1), *(p_in+2), p_out) == 0)
147 /* Invalid QP data - pass through unchanged. */
152 else if (*p_in == '_') {
154 * RFC 2047: '_' inside encoded word represents 0x20.
155 * NOT a space - always the value 0x20.
167 else if (enc == 'b') {
168 /* Decode base64 encoded data */
172 delimsave = *p; *p = '\r';
173 decoded_count = from64tobits(p_out, p_in);
175 if (decoded_count > 0)
176 p_out += decoded_count;
187 p_in += 2; /* Skip the MIMEHDR_END delimiter */
190 * We've completed decoding one encoded sequence. But another
191 * may follow immediately, in which case whitespace before the
192 * new MIMEHDR_INIT delimiter must be discarded.
193 * See if that is the case
195 p = strstr(p_in, MIMEHDR_INIT);
196 state = S_COPY_PLAIN;
199 * There is more MIME data later on. Is there
200 * whitespace only before the delimiter?
205 for (q=p_in; (wsp_only && (q < p)); q++)
206 wsp_only = isspace(*q);
210 * Whitespace-only before the MIME delimiter. OK,
211 * just advance p_in to past the new MIMEHDR_INIT,
212 * and prepare to process the new MIME charset/encoding
215 p_in = p + strlen(MIMEHDR_INIT);
216 state = S_SKIP_MIMEINIT;
229 * Routines for decoding body-parts of a message.
231 * Since the "fetch" part of fetchmail gets a message body
232 * one line at a time, we need to maintain some state variables
233 * across multiple invokations of the UnMimeBodyline() routine.
234 * The driver routine should call MimeBodyType() when all
235 * headers have been received, and then UnMimeBodyline() for
236 * every line in the message body.
239 #define S_BODY_DATA 0
243 * Flag indicating if we are currently processing
244 * the headers or the body of a (multipart) message.
246 static int BodyState = S_BODY_DATA;
249 * Flag indicating if we are in the process of decoding
250 * a quoted-printable body part.
252 static int CurrEncodingIsQP = 0;
253 static int CurrTypeNeedsDecode = 0;
256 * Delimiter for multipart messages. RFC 2046 states that this must
257 * NEVER be longer than 70 characters. Add 3 for the two hyphens
258 * at the beginning, and a terminating null.
260 #define MAX_DELIM_LEN 70
261 static unsigned char MultipartDelimiter[MAX_DELIM_LEN+3];
264 /* This string replaces the "Content-Transfer-Encoding: quoted-printable"
265 * string in all headers, including those in body-parts. The replacement
266 * must be no longer than the original string.
268 static const char ENC8BIT[] = "Content-Transfer-Encoding: 8bit";
269 static void SetEncoding8bit(unsigned char *XferEncOfs)
273 if (XferEncOfs != NULL) {
274 memcpy(XferEncOfs, ENC8BIT, strlen(ENC8BIT));
276 /* If anything left, in this header, replace with whitespace */
277 for (p=XferEncOfs+strlen(ENC8BIT); (*p >= ' '); p++) *p=' ';
281 static char *GetBoundary(char *CntType)
286 /* Find the "boundary" delimiter. It must be preceded with a ';'
287 * and optionally some whitespace.
291 p2 = strchr(p1, ';');
293 for (p2++; isspace(*p2); p2++);
296 } while ((p1) && (strncasecmp(p1, "boundary", 8) != 0));
299 /* No boundary delimiter */
302 /* Skip "boundary", whitespace and '='; check that we do have a '=' */
303 for (p1+=8, flag=0; (isspace(*p1) || (*p1 == '=')); p1++)
304 flag |= (*p1 == '=');
308 /* Find end of boundary delimiter string */
310 /* The delimiter is inside quotes */
312 p2 = strchr(p1, '\"');
314 return NULL; /* No closing '"' !?! */
317 /* There might be more text after the "boundary" string. */
318 p2 = strchr(p1, ';'); /* Safe - delimiter with ';' must be in quotes */
321 /* Zero-terminate the boundary string */
325 return (p1 && strlen(p1)) ? p1 : NULL;
329 int CheckContentType(char *CntType)
332 * Static array of Content-Type's for which we will do
333 * quoted-printable decoding, if requested.
334 * It is probably wise to do this only on known text-only types;
335 * be really careful if you change this.
338 static char *DecodedTypes[] = {
339 "text/", /* Will match ALL content-type's starting with 'text/' */
347 /* Skip whitespace, if any */
348 for (; isspace(*p); p++) ;
352 (strncasecmp(p, DecodedTypes[i], strlen(DecodedTypes[i]))));
355 return (DecodedTypes[i] != NULL);
360 * This routine does three things:
361 * 1) It determines - based on the message headers - whether the
362 * message body is a MIME message that may hold 8 bit data.
363 * - A message that has a "quoted-printable" or "8bit" transfer
364 * encoding is assumed to contain 8-bit data (when decoded).
365 * - A multipart message is assumed to contain 8-bit data
366 * when decoded (there might be quoted-printable body-parts).
367 * - All other messages are assumed NOT to include 8-bit data.
368 * 2) It determines the delimiter-string used in multi-part message
370 * 3) It sets the initial values of the CurrEncodingIsQP,
371 * CurrTypeNeedsDecode, and BodyState variables, from the header
374 * The return value is a bitmask.
376 int MimeBodyType(unsigned char *hdrs, int WantDecode)
378 unsigned char *NxtHdr = hdrs;
379 unsigned char *XferEnc, *XferEncOfs, *CntType, *MimeVer, *p;
380 int HdrsFound = 0; /* We only look for three headers */
381 int BodyType; /* Return value */
383 /* Setup for a standard (no MIME, no QP, 7-bit US-ASCII) message */
384 MultipartDelimiter[0] = '\0';
385 CurrEncodingIsQP = CurrTypeNeedsDecode = 0;
386 BodyState = S_BODY_DATA;
389 /* Just in case ... */
393 XferEnc = XferEncOfs = CntType = MimeVer = NULL;
396 if (strncasecmp("Content-Transfer-Encoding:", NxtHdr, 26) == 0) {
400 xalloca(XferEnc, char *, strlen(p) + 1);
405 else if (strncasecmp("Content-Type:", NxtHdr, 13) == 0) {
407 * This one is difficult. We cannot use the standard
408 * nxtaddr() routine, since the boundary-delimiter is
409 * (probably) enclosed in quotes - and thus appears
410 * as an rfc822 comment, and nxtaddr() "eats" up any
411 * spaces in the delimiter. So, we have to do this
415 /* Skip the "Content-Type:" part and whitespace after it */
416 for (NxtHdr += 13; ((*NxtHdr == ' ') || (*NxtHdr == '\t')); NxtHdr++);
419 * Get the full value of the Content-Type header;
420 * it might span multiple lines. So search for
421 * a newline char, but ignore those that have a
422 * have a TAB or space just after the NL (continued
427 p=strchr((p+1),'\n');
428 } while ( (p != NULL) && ((*(p+1) == '\t') || (*(p+1) == ' ')) );
429 if (p == NULL) p = NxtHdr + strlen(NxtHdr);
431 xalloca(CntType, char *, p-NxtHdr+2);
432 strncpy(CntType, NxtHdr, (p-NxtHdr));
433 *(CntType+(p-NxtHdr)) = '\0';
436 else if (strncasecmp("MIME-Version:", NxtHdr, 13) == 0) {
439 xalloca(MimeVer, char *, strlen(p) + 1);
445 NxtHdr = (strchr(NxtHdr, '\n'));
446 if (NxtHdr != NULL) NxtHdr++;
447 } while ((NxtHdr != NULL) && (*NxtHdr) && (HdrsFound != 3));
450 /* Done looking through the headers, now check what they say */
451 if ((MimeVer != NULL) && (strcmp(MimeVer, "1.0") == 0)) {
453 CurrTypeNeedsDecode = CheckContentType(CntType);
455 /* Check Content-Type to see if this is a multipart message */
456 if ( (CntType != NULL) &&
457 ((strncasecmp(CntType, "multipart/", 10) == 0) ||
458 (strncasecmp(CntType, "message/", 8) == 0)) ) {
460 char *p1 = GetBoundary(CntType);
463 /* The actual delimiter is "--" followed by
464 the boundary string */
465 strcpy(MultipartDelimiter, "--");
466 strncat(MultipartDelimiter, p1, MAX_DELIM_LEN);
467 BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE);
472 * Check Content-Transfer-Encoding, but
473 * ONLY for non-multipart messages (BodyType == 0).
475 if ((XferEnc != NULL) && (BodyType == 0)) {
476 if (strcasecmp(XferEnc, "quoted-printable") == 0) {
477 CurrEncodingIsQP = 1;
478 BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE);
479 if (WantDecode && CurrTypeNeedsDecode) {
480 SetEncoding8bit(XferEncOfs);
483 else if (strcasecmp(XferEnc, "7bit") == 0) {
484 CurrEncodingIsQP = 0;
485 BodyType = (MSG_IS_7BIT);
487 else if (strcasecmp(XferEnc, "8bit") == 0) {
488 CurrEncodingIsQP = 0;
489 BodyType = (MSG_IS_8BIT);
500 * Decode one line of data containing QP data.
501 * Return flag set if this line ends with a soft line-break.
502 * 'bufp' is modified to point to the end of the output buffer.
504 static int DoOneQPLine(unsigned char **bufp, int collapsedoubledot)
506 unsigned char *buf = *bufp;
507 unsigned char *p_in, *p_out, *p;
512 if (collapsedoubledot && (strncmp(buf, "..", 2) == 0))
515 for (p_out = buf; (*p_in); ) {
516 p = strchr(p_in, '=');
518 /* No more QP data, just move remainder into place */
520 memmove(p_out, p_in, n);
521 p_in += n; p_out += n;
525 /* There are some uncoded chars at the beginning. */
527 memmove(p_out, p_in, n);
532 case '\0': case '\r': case '\n':
533 /* Soft line break, skip '=' */
535 if (*p_in == '\r') p_in++;
536 if (*p_in == '\n') p_in++;
541 /* There is a QP encoded byte */
542 if (qp_char(*(p+1), *(p+2), p_out) == 0) {
546 /* Invalid QP data - pass through unchanged. */
562 /* This is called once per line in the message body. We need to scan
563 * all lines in the message body for the multipart delimiter string,
564 * and handle any body-part headers in such messages (these can toggle
565 * qp-decoding on and off).
567 * Note: Messages that are NOT multipart-messages go through this
568 * routine quickly, since BodyState will always be S_BODY_DATA,
569 * and MultipartDelimiter is NULL.
571 * Return flag set if this line ends with a soft line-break.
572 * 'bufp' is modified to point to the end of the output buffer.
575 int UnMimeBodyline(unsigned char **bufp, int collapsedoubledot)
577 unsigned char *buf = *bufp;
582 UnMimeHeader(buf); /* Headers in body-parts can be encoded, too! */
583 if ((*buf == '\0') || (*buf == '\n') || (strcmp(buf, "\r\n") == 0)) {
584 BodyState = S_BODY_DATA;
586 else if (strncasecmp("Content-Transfer-Encoding:", buf, 26) == 0) {
589 XferEnc = nxtaddr(buf);
590 if ((XferEnc != NULL) && (strcasecmp(XferEnc, "quoted-printable") == 0)) {
591 CurrEncodingIsQP = 1;
593 /* Hmm ... we cannot be really sure that CurrTypeNeedsDecode
594 has been set - we may not have seen the Content-Type header
595 yet. But *usually* the Content-Type header comes first, so
596 this will work. And there is really no way of doing it
597 "right" as long as we stick with the line-by-line processing. */
598 if (CurrTypeNeedsDecode) SetEncoding8bit(buf);
601 else if (strncasecmp("Content-Type:", buf, 13) == 0) {
602 CurrTypeNeedsDecode = CheckContentType(nxtaddr(buf));
605 *bufp = (buf + strlen(buf));
609 if ((*MultipartDelimiter) &&
610 (strncmp(buf, MultipartDelimiter, strlen(MultipartDelimiter)) == 0)) {
611 BodyState = S_BODY_HDR;
612 CurrEncodingIsQP = CurrTypeNeedsDecode = 0;
615 if (CurrEncodingIsQP && CurrTypeNeedsDecode)
616 ret = DoOneQPLine(bufp, collapsedoubledot);
618 *bufp = (buf + strlen(buf));
630 char *program_name = "unmime";
633 #define BUFSIZE_INCREMENT 4096
636 #define DBG_FWRITE(B,L,BS,FD) fwrite(B, L, BS, FD)
638 #define DBG_FWRITE(B,L,BS,FD)
641 int main(int argc, char *argv[])
643 unsigned int BufSize;
644 unsigned char *buffer, *buf_p;
645 int nl_count, i, bodytype;
649 FILE *fd_orig, *fd_conv;
653 sprintf(fnam, "/tmp/i_unmime.%x", pid);
654 fd_orig = fopen(fnam, "w");
655 sprintf(fnam, "/tmp/o_unmime.%x", pid);
656 fd_conv = fopen(fnam, "w");
659 BufSize = BUFSIZE_INCREMENT; /* Initial size of buffer */
660 buf_p = buffer = (unsigned char *) xmalloc(BufSize);
664 i = fread(buf_p, 1, 1, stdin);
679 if ((buf_p - buffer) == BufSize) {
680 /* Buffer is full! Get more room. */
681 buffer = xrealloc(buffer, BufSize+BUFSIZE_INCREMENT);
682 buf_p = buffer + BufSize;
683 BufSize += BUFSIZE_INCREMENT;
685 } while ((i > 0) && (nl_count < 2));
688 DBG_FWRITE(buffer, strlen(buffer), 1, fd_orig);
690 UnMimeHeader(buffer);
691 bodytype = MimeBodyType(buffer, 1);
694 fwrite(buffer, i, 1, stdout);
695 DBG_FWRITE(buffer, i, 1, fd_conv);
698 buf_p = (buffer - 1);
701 i = fread(buf_p, 1, 1, stdin);
702 } while ((i == 1) && (*buf_p != '\n'));
705 DBG_FWRITE(buf, (buf_p - buffer), 1, fd_orig);
707 if (buf_p > buffer) {
708 if (bodytype & MSG_NEEDS_DECODE) {
710 UnMimeBodyline(&buf_p, 0);
712 fwrite(buffer, (buf_p - buffer), 1, stdout);
713 DBG_FWRITE(buffer, (buf_p - buffer), 1, fd_conv);
715 } while (buf_p > buffer);