2 * Copyright (c) 1998-2003, 2006, 2013 Proofpoint, Inc. and its suppliers.
4 * Copyright (c) 1994, 1996-1997 Eric P. Allman. All rights reserved.
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
17 SM_RCSID("@(#)$Id: mime.c,v 8.149 2013-11-22 20:51:56 ca Exp $")
18 #include <sm/sendmail.h>
23 ** I am indebted to John Beck of Hewlett-Packard, who contributed
24 ** his code to me for inclusion. As it turns out, I did not use
25 ** his code since he used a "minimum change" approach that used
26 ** several temp files, and I wanted a "minimum impact" approach
27 ** that would avoid copying. However, looking over his code
28 ** helped me cement my understanding of the problem.
30 ** I also looked at, but did not directly use, Nathaniel
31 ** Borenstein's "code.c" module. Again, it functioned as
32 ** a file-to-file translator, which did not fit within my
33 ** design bounds, but it was a useful base for understanding
37 /* use "old" mime 7 to 8 algorithm by default */
39 # define MIME7TO8_OLD 1
43 static int isboundary __P((char *, char **));
44 static int mimeboundary __P((char *, char **));
45 static int mime_getchar __P((SM_FILE_T *, char **, int *));
46 static int mime_getchar_crlf __P((SM_FILE_T *, char **, int *));
48 /* character set for hex and base64 encoding */
49 static char Base16Code[] = "0123456789ABCDEF";
50 static char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
52 /* types of MIME boundaries */
53 # define MBT_SYNTAX 0 /* syntax error */
54 # define MBT_NOTSEP 1 /* not a boundary */
55 # define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */
56 # define MBT_FINAL 3 /* final boundary (trailing -- included) */
58 static char *MimeBoundaryNames[] =
60 "SYNTAX", "NOTSEP", "INTERMED", "FINAL"
63 static bool MapNLtoCRLF;
66 ** MIME8TO7 -- output 8 bit body in 7 bit format
68 ** The header has already been output -- this has to do the
69 ** 8 to 7 bit conversion. It would be easy if we didn't have
70 ** to deal with nested formats (multipart/xxx and message/rfc822).
72 ** We won't be called if we don't have to do a conversion, and
73 ** appropriate MIME-Version: and Content-Type: fields have been
74 ** output. Any Content-Transfer-Encoding: field has not been
75 ** output, and we can add it here.
78 ** mci -- mailer connection information.
79 ** header -- the header for this body part.
81 ** boundaries -- the currently pending message boundaries.
82 ** NULL if we are processing the outer portion.
83 ** flags -- to tweak processing.
84 ** level -- recursion level.
87 ** An indicator of what terminated the message part:
88 ** MBT_FINAL -- the final boundary
89 ** MBT_INTERMED -- an intermediate boundary
90 ** MBT_NOTSEP -- an end of file
91 ** SM_IO_EOF -- I/O error occurred
96 char *a_field; /* name of field */
97 char *a_value; /* value of that field */
101 mime8to7(mci, header, e, boundaries, flags, level)
104 register ENVELOPE *e;
114 size_t sectionsize, sectionhighbits;
123 struct args argv[MAXMIMEARGS];
126 char pvpbuf[MAXLINE];
127 extern unsigned char MimeTokenTab[256];
129 if (level > MAXMIMENESTING)
131 if (!bitset(EF_TOODEEP, e->e_flags))
134 sm_dprintf("mime8to7: too deep, level=%d\n",
136 usrerr("mime8to7: recursion level %d exceeded",
138 e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
143 sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
144 if (boundaries[0] == NULL)
145 sm_dprintf(" <none>");
148 for (i = 0; boundaries[i] != NULL; i++)
149 sm_dprintf(" %s", boundaries[i]);
154 p = hvalue("Content-Transfer-Encoding", header);
156 (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
157 MimeTokenTab, false)) == NULL ||
164 cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
165 cte = sm_rpool_strdup_x(e->e_rpool, buf);
168 type = subtype = NULL;
169 p = hvalue("Content-Type", header);
172 if (bitset(M87F_DIGEST, flags))
173 p = "message/rfc822";
178 (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
179 MimeTokenTab, false)) != NULL &&
184 for (i = 0; pvp[i] != NULL; i++)
185 sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
188 if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
194 /* break out parameters */
195 while (*pvp != NULL && argc < MAXMIMEARGS)
197 /* skip to semicolon separator */
198 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
200 if (*pvp++ == NULL || *pvp == NULL)
203 /* complain about empty values */
204 if (strcmp(*pvp, ";") == 0)
206 usrerr("mime8to7: Empty parameter in Content-Type header");
208 /* avoid bounce loops */
209 e->e_flags |= EF_DONT_MIME;
213 /* extract field name */
214 argv[argc].a_field = *pvp++;
216 /* see if there is a value */
217 if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
218 (*++pvp == NULL || strcmp(*pvp, ";") != 0))
220 argv[argc].a_value = *pvp;
226 /* check for disaster cases */
232 /* don't propagate some flags more than one level into the message */
233 flags &= ~M87F_DIGEST;
236 ** Check for cases that can not be encoded.
238 ** For example, you can't encode certain kinds of types
239 ** or already-encoded messages. If we find this case,
240 ** just copy it through.
243 (void) sm_snprintf(buf, sizeof(buf), "%.100s/%.100s", type, subtype);
244 if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
245 flags |= M87F_NO8BIT;
248 if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
251 if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
255 ** Multipart requires special processing.
257 ** Do a recursive descent into the message.
260 if (SM_STRCASEEQ(type, "multipart") &&
261 (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)) &&
262 !bitset(EF_TOODEEP, e->e_flags)
266 if (SM_STRCASEEQ(subtype, "digest"))
267 flags |= M87F_DIGEST;
269 for (i = 0; i < argc; i++)
271 if (SM_STRCASEEQ(argv[i].a_field, "boundary"))
274 if (i >= argc || argv[i].a_value == NULL)
276 usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
277 i >= argc ? "missing" : "bogus", p);
280 /* avoid bounce loops */
281 e->e_flags |= EF_DONT_MIME;
286 unfoldstripquotes(p);
288 if (sm_strlcpy(bbuf, p, sizeof(bbuf)) >= sizeof(bbuf))
290 usrerr("mime8to7: multipart boundary \"%s\" too long",
293 /* avoid bounce loops */
294 e->e_flags |= EF_DONT_MIME;
298 sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
300 for (i = 0; i < MAXMIMENESTING; i++)
302 if (boundaries[i] == NULL)
305 if (i >= MAXMIMENESTING)
308 sm_dprintf("mime8to7: too deep, i=%d\n", i);
309 if (!bitset(EF_TOODEEP, e->e_flags))
310 usrerr("mime8to7: multipart nesting boundary too deep");
312 /* avoid bounce loops */
313 e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
317 boundaries[i] = bbuf;
318 boundaries[i + 1] = NULL;
320 mci->mci_flags |= MCIF_INMIME;
322 /* skip the early "comment" prologue */
323 if (!putline("", mci))
325 mci->mci_flags &= ~MCIF_INHEADER;
327 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
330 bt = mimeboundary(buf, boundaries);
331 if (bt != MBT_NOTSEP)
333 if (!putxline(buf, blen, mci,
334 PXLF_MAPFROM|PXLF_STRIP8BIT))
337 sm_dprintf(" ...%s", buf);
339 if (sm_io_eof(e->e_dfp))
341 while (bt != MBT_FINAL)
343 auto HDR *hdr = NULL;
345 (void) sm_strlcpyn(buf, sizeof(buf), 2, "--", bbuf);
346 if (!putline(buf, mci))
349 sm_dprintf(" ...%s\n", buf);
350 collect(e->e_dfp, SMTPMODE_NO, &hdr, e, false);
352 putline("+++after collect", mci);
353 if (!putheader(mci, hdr, e, flags))
356 putline("+++after putheader", mci);
357 bt = mime8to7(mci, hdr, e, boundaries, flags,
362 (void) sm_strlcpyn(buf, sizeof(buf), 3, "--", bbuf, "--");
363 if (!putline(buf, mci))
366 sm_dprintf(" ...%s\n", buf);
367 boundaries[i] = NULL;
368 mci->mci_flags &= ~MCIF_INMIME;
370 /* skip the late "comment" epilogue */
371 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
374 bt = mimeboundary(buf, boundaries);
375 if (bt != MBT_NOTSEP)
377 if (!putxline(buf, blen, mci,
378 PXLF_MAPFROM|PXLF_STRIP8BIT))
381 sm_dprintf(" ...%s", buf);
383 if (sm_io_eof(e->e_dfp))
386 sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
387 MimeBoundaryNames[bt]);
392 ** Message/xxx types -- recurse exactly once.
394 ** Class 's' is predefined to have "rfc822" only.
397 if (SM_STRCASEEQ(type, "message"))
399 if (!wordinclass(subtype, 's') ||
400 bitset(EF_TOODEEP, e->e_flags))
402 flags |= M87F_NO8BIT;
406 auto HDR *hdr = NULL;
408 if (!putline("", mci))
411 mci->mci_flags |= MCIF_INMIME;
412 collect(e->e_dfp, SMTPMODE_NO, &hdr, e, false);
414 putline("+++after collect", mci);
415 if (!putheader(mci, hdr, e, flags))
418 putline("+++after putheader", mci);
419 if (hvalue("MIME-Version", hdr) == NULL &&
420 !bitset(M87F_NO8TO7, flags) &&
421 !putline("MIME-Version: 1.0", mci))
423 bt = mime8to7(mci, hdr, e, boundaries, flags,
425 mci->mci_flags &= ~MCIF_INMIME;
431 ** Non-compound body type
433 ** Compute the ratio of seven to eight bit characters;
434 ** use that as a heuristic to decide how to do the
438 sectionsize = sectionhighbits = 0;
439 if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
441 /* remember where we were */
442 offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
444 syserr("mime8to7: cannot sm_io_tell on %cf%s",
445 DATAFL_LETTER, e->e_id);
447 /* do a scan of this body type to count character types */
448 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
451 if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
453 for (i = 0; i < blen; i++)
455 /* count bytes with the high bit set */
457 if (bitset(0200, buf[i]))
462 ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
463 ** assume base64. This heuristic avoids double-reading
464 ** large graphics or video files.
467 if (sectionsize >= 4096 &&
468 sectionhighbits > sectionsize / 4)
472 /* return to the original offset for processing */
473 /* XXX use relative seeks to handle >31 bit file sizes? */
474 if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
475 syserr("mime8to7: cannot sm_io_fseek on %cf%s",
476 DATAFL_LETTER, e->e_id);
478 sm_io_clearerr(e->e_dfp);
482 ** Heuristically determine encoding method.
483 ** If more than 1/8 of the total characters have the
484 ** eighth bit set, use base64; else use quoted-printable.
485 ** However, only encode binary encoded data as base64,
486 ** since otherwise the LF=>CRLF mapping will be a problem.
491 sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
492 (long) sectionhighbits, (long) sectionsize,
493 cte == NULL ? "[none]" : cte,
494 type == NULL ? "[none]" : type,
495 subtype == NULL ? "[none]" : subtype);
497 if (cte != NULL && SM_STRCASEEQ(cte, "binary"))
498 sectionsize = sectionhighbits;
501 if (sectionhighbits == 0)
503 /* no encoding necessary */
505 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
507 !bitset(M87F_NO8TO7, flags))
510 ** Skip _unless_ in MIME mode and potentially
511 ** converting from 8 bit to 7 bit MIME. See
512 ** putheader() for the counterpart where the
513 ** CTE header is skipped in the opposite
517 (void) sm_snprintf(buf, sizeof(buf),
518 "Content-Transfer-Encoding: %.200s", cte);
519 if (!putline(buf, mci))
522 sm_dprintf(" ...%s\n", buf);
524 if (!putline("", mci))
526 mci->mci_flags &= ~MCIF_INHEADER;
527 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
530 if (!bitset(MCIF_INLONGLINE, mci->mci_flags))
532 bt = mimeboundary(buf, boundaries);
533 if (bt != MBT_NOTSEP)
536 if (!putxline(buf, blen, mci,
537 PXLF_MAPFROM|PXLF_NOADDEOL))
540 if (sm_io_eof(e->e_dfp))
543 else if (!MapNLtoCRLF ||
544 (sectionsize / 8 < sectionhighbits && !use_qp))
546 /* use base64 encoding */
550 sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
551 if (!putline("Content-Transfer-Encoding: base64", mci))
553 (void) sm_snprintf(buf, sizeof(buf),
554 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
555 MyHostName, e->e_id);
556 if (!putline(buf, mci) || !putline("", mci))
558 mci->mci_flags &= ~MCIF_INHEADER;
559 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
565 if (!putline(buf, mci))
571 *bp++ = Base64Code[(c1 >> 2)];
572 c1 = (c1 & 0x03) << 4;
573 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
576 *bp++ = Base64Code[c1];
581 c1 |= (c2 >> 4) & 0x0f;
582 *bp++ = Base64Code[c1];
583 c1 = (c2 & 0x0f) << 2;
584 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
587 *bp++ = Base64Code[c1];
591 c1 |= (c2 >> 6) & 0x03;
592 *bp++ = Base64Code[c1];
593 *bp++ = Base64Code[c2 & 0x3f];
596 if (!putline(buf, mci))
601 /* use quoted-printable encoding */
606 /* set up map of characters that must be mapped */
608 for (c1 = 0x00; c1 < 0x20; c1++)
609 setbitn(c1, badchars);
610 clrbitn('\t', badchars);
611 for (c1 = 0x7f; c1 < 0x100; c1++)
612 setbitn(c1, badchars);
613 setbitn('=', badchars);
614 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
615 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
616 setbitn(*p, badchars);
619 sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
620 if (!putline("Content-Transfer-Encoding: quoted-printable",
623 (void) sm_snprintf(buf, sizeof(buf),
624 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
625 MyHostName, e->e_id);
626 if (!putline(buf, mci) || !putline("", mci))
628 mci->mci_flags &= ~MCIF_INHEADER;
631 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
636 if (c2 == ' ' || c2 == '\t')
639 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
640 *bp++ = Base16Code[c2 & 0x0f];
642 if (buf[0] == '.' && bp == &buf[1])
645 *bp++ = Base16Code[('.' >> 4) & 0x0f];
646 *bp++ = Base16Code['.' & 0x0f];
649 if (!putline(buf, mci))
651 linelen = fromstate = 0;
656 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
657 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
664 else if (c2 == ' ' || c2 == '\t')
670 (linelen > 75 || c1 != '.' ||
671 (linelen > 73 && c2 == '.')))
673 if (linelen > 73 && c2 == '.')
679 if (!putline(buf, mci))
681 linelen = fromstate = 0;
689 if (bitnset(bitidx(c1), badchars))
692 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
693 *bp++ = Base16Code[c1 & 0x0f];
696 else if (c1 != ' ' && c1 != '\t')
698 if (linelen < 4 && c1 == "From"[linelen])
706 /* output any saved character */
707 if (c2 == ' ' || c2 == '\t')
710 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
711 *bp++ = Base16Code[c2 & 0x0f];
715 if (linelen > 0 || boundaries[0] != NULL)
718 if (!putline(buf, mci))
724 sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
731 ** MIME_GETCHAR -- get a character for MIME processing
733 ** Treats boundaries as SM_IO_EOF.
736 ** fp -- the input file.
737 ** boundaries -- the current MIME boundaries.
738 ** btp -- if the return value is SM_IO_EOF, *btp is set to
739 ** the type of the boundary.
742 ** The next character in the input stream.
746 mime_getchar(fp, boundaries, btp)
747 register SM_FILE_T *fp;
752 static unsigned char *bp = NULL;
753 static int buflen = 0;
754 static bool atbol = true; /* at beginning of line */
755 static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
756 static unsigned char buf[128]; /* need not be a full line */
757 int start = 0; /* indicates position of - in buffer */
759 if (buflen == 1 && *bp == '\n')
761 /* last \n in buffer may be part of next MIME boundary */
770 c = sm_io_getc(fp, SM_TIME_DEFAULT);
775 /* might be part of a MIME boundary */
778 c = sm_io_getc(fp, SM_TIME_DEFAULT);
781 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
790 if (atbol && c == '-')
792 /* check for a message boundary */
793 c = sm_io_getc(fp, SM_TIME_DEFAULT);
800 buflen = bp - buf - 1;
805 /* got "--", now check for rest of separator */
807 while (bp < &buf[sizeof(buf) - 2] &&
808 (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
813 *bp = '\0'; /* XXX simply cut off? */
814 bt = mimeboundary((char *) &buf[start], boundaries);
819 /* we have a message boundary */
825 if (bp < &buf[sizeof(buf) - 2] && c != SM_IO_EOF)
830 buflen = bp - buf - 1;
840 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate LF => CRLF
843 ** fp -- the input file.
844 ** boundaries -- the current MIME boundaries.
845 ** btp -- if the return value is SM_IO_EOF, *btp is set to
846 ** the type of the boundary.
849 ** The next character in the input stream.
853 mime_getchar_crlf(fp, boundaries, btp)
854 register SM_FILE_T *fp;
858 static bool sendlf = false;
866 c = mime_getchar(fp, boundaries, btp);
867 if (c == '\n' && MapNLtoCRLF)
875 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
878 ** line -- the input line.
879 ** boundaries -- the set of currently pending boundaries.
882 ** MBT_NOTSEP -- if this is not a separator line
883 ** MBT_INTERMED -- if this is an intermediate separator
884 ** MBT_FINAL -- if this is a final boundary
885 ** MBT_SYNTAX -- if this is a boundary for the wrong
886 ** enclosure -- i.e., a syntax error.
890 mimeboundary(line, boundaries)
894 int type = MBT_NOTSEP;
898 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
901 if (i > 0 && line[i - 1] == '\n')
904 /* strip off trailing whitespace */
905 while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
907 || line[i - 1] == '\r'
915 sm_dprintf("mimeboundary: line=\"%s\"... ", line);
917 /* check for this as an intermediate boundary */
918 if (isboundary(&line[2], boundaries) >= 0)
920 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
922 /* check for a final boundary */
924 if (isboundary(&line[2], boundaries) >= 0)
931 sm_dprintf("%s\n", MimeBoundaryNames[type]);
935 ** DEFCHARSET -- return default character set for message
937 ** The first choice for character set is for the mailer
938 ** corresponding to the envelope sender. If neither that
939 ** nor the global configuration file has a default character
940 ** set defined, return "unknown-8bit" as recommended by
941 ** RFC 1428 section 3.
944 ** e -- the envelope for this message.
947 ** The default character set for that mailer.
952 register ENVELOPE *e;
954 if (e != NULL && e->e_from.q_mailer != NULL &&
955 e->e_from.q_mailer->m_defcharset != NULL)
956 return e->e_from.q_mailer->m_defcharset;
957 if (DefaultCharSet != NULL)
958 return DefaultCharSet;
959 return "unknown-8bit";
962 ** ISBOUNDARY -- is a given string a currently valid boundary?
965 ** line -- the current input line.
966 ** boundaries -- the list of valid boundaries.
969 ** The index number in boundaries if the line is found.
975 isboundary(line, boundaries)
981 for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
983 if (strcmp(line, boundaries[i]) == 0)
988 #endif /* MIME8TO7 */
991 static int mime_fromqp __P((unsigned char *, unsigned char **, int));
994 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
996 ** This is a hack. Supports translating the two 7-bit body-encodings
997 ** (quoted-printable and base64) to 8-bit coded bodies.
999 ** There is not much point in supporting multipart here, as the UA
1000 ** will be able to deal with encoded MIME bodies if it can parse MIME
1001 ** multipart messages.
1003 ** Note also that we won't be called unless it is a text/plain MIME
1004 ** message, encoded base64 or QP and mailer flag '9' has been defined
1007 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
1010 ** mci -- mailer connection information.
1011 ** header -- the header for this body part.
1015 ** true iff body was written successfully
1018 static char index_64[128] =
1020 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1021 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1022 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
1023 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
1024 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
1025 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
1026 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
1027 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
1030 # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
1033 mime7to8(mci, header, e)
1036 register ENVELOPE *e;
1042 unsigned char *fbufp;
1044 unsigned char fbuf[MAXLINE + 1];
1045 char pvpbuf[MAXLINE];
1046 extern unsigned char MimeTokenTab[256];
1048 p = hvalue("Content-Transfer-Encoding", header);
1050 (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1051 MimeTokenTab, false)) == NULL ||
1054 /* "can't happen" -- upper level should have caught this */
1055 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
1057 /* avoid bounce loops */
1058 e->e_flags |= EF_DONT_MIME;
1060 /* cheap failsafe algorithm -- should work on text/plain */
1063 (void) sm_snprintf(buf, sizeof(buf),
1064 "Content-Transfer-Encoding: %s", p);
1065 if (!putline(buf, mci))
1068 if (!putline("", mci))
1070 mci->mci_flags &= ~MCIF_INHEADER;
1071 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1074 if (!putxline(buf, blen, mci, PXLF_MAPFROM))
1079 cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
1080 cte = sm_rpool_strdup_x(e->e_rpool, buf);
1082 mci->mci_flags |= MCIF_INHEADER;
1083 if (!putline("Content-Transfer-Encoding: 8bit", mci))
1085 (void) sm_snprintf(buf, sizeof(buf),
1086 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1087 cte, MyHostName, e->e_id);
1088 if (!putline(buf, mci) || !putline("", mci))
1090 mci->mci_flags &= ~MCIF_INHEADER;
1093 ** Translate body encoding to 8-bit. Supports two types of
1094 ** encodings; "base64" and "quoted-printable". Assume qp if
1095 ** it is not base64.
1098 pxflags = PXLF_MAPFROM;
1099 if (SM_STRCASEEQ(cte, "base64"))
1104 while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1112 c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1113 } while (SM_ISSPACE(c2));
1114 if (c2 == SM_IO_EOF)
1119 c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1120 } while (SM_ISSPACE(c3));
1121 if (c3 == SM_IO_EOF)
1126 c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1127 } while (SM_ISSPACE(c4));
1128 if (c4 == SM_IO_EOF)
1131 if (c1 == '=' || c2 == '=')
1137 # define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1139 # else /* MIME7TO8_OLD */
1140 # define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1143 pxflags |= PXLF_NOADDEOL; \
1145 # endif /* MIME7TO8_OLD */
1150 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
1153 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1155 pxflags &= ~PXLF_NOADDEOL; \
1160 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1165 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1170 *fbufp = ((c3 & 0x03) << 6) | c4;
1178 /* quoted-printable */
1179 pxflags |= PXLF_NOADDEOL;
1181 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1184 off = mime_fromqp((unsigned char *) buf, &fbufp,
1185 &fbuf[MAXLINE] - fbufp);
1190 if (fbufp - fbuf > 0)
1192 if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1197 if (off >= 0 && buf[off] != '\0')
1199 off = mime_fromqp((unsigned char *) (buf + off),
1201 &fbuf[MAXLINE] - fbufp);
1207 /* force out partial last line */
1211 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1216 ** The decoded text may end without an EOL. Since this function
1217 ** is only called for text/plain MIME messages, it is safe to
1218 ** add an extra one at the end just in case. This is a hack,
1219 ** but so is auto-converting MIME in the first place.
1222 if (!putline("", mci))
1226 sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1233 ** The following is based on Borenstein's "codes.c" module, with simplifying
1234 ** changes as we do not deal with multipart, and to do the translation in-core,
1235 ** with an attempt to prevent overrun of output buffers.
1237 ** What is needed here are changes to defend this code better against
1238 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1241 static char index_hex[128] =
1243 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1244 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1245 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1246 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1247 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1248 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1249 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1250 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1253 # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1256 ** MIME_FROMQP -- decode quoted printable string
1259 ** infile -- input (encoded) string
1260 ** outfile -- output string
1261 ** maxlen -- size of output buffer
1264 ** -2 if decoding failure
1265 ** -1 if infile completely decoded into outfile
1266 ** >= 0 is the position in infile decoding
1267 ** reached before maxlen was reached
1271 mime_fromqp(infile, outfile, maxlen)
1272 unsigned char *infile;
1273 unsigned char **outfile;
1274 int maxlen; /* Max # of chars allowed in outfile */
1280 /* decrement by one for trailing '\0', at least one other char */
1285 while ((c1 = *infile++) != '\0' && nchar < maxlen)
1289 if ((c1 = *infile++) == '\0')
1292 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1294 /* ignore it and the rest of the buffer */
1301 if ((c2 = *infile++) == '\0')
1306 } while ((c2 = HEXCHAR(c2)) == -1);
1311 *(*outfile)++ = c1 << 4 | c2;
1322 *(*outfile)++ = '\0';
1323 if (nchar >= maxlen)
1324 return (infile - b - 1);
1327 #endif /* MIME7TO8 */