2 * Copyright (c) 1998-2003, 2006 Sendmail, 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.142.2.1 2006/05/23 01:32:08 ca Exp $")
22 ** I am indebted to John Beck of Hewlett-Packard, who contributed
23 ** his code to me for inclusion. As it turns out, I did not use
24 ** his code since he used a "minimum change" approach that used
25 ** several temp files, and I wanted a "minimum impact" approach
26 ** that would avoid copying. However, looking over his code
27 ** helped me cement my understanding of the problem.
29 ** I also looked at, but did not directly use, Nathaniel
30 ** Borenstein's "code.c" module. Again, it functioned as
31 ** a file-to-file translator, which did not fit within my
32 ** design bounds, but it was a useful base for understanding
36 /* use "old" mime 7 to 8 algorithm by default */
38 # define MIME7TO8_OLD 1
39 #endif /* ! MIME7TO8_OLD */
42 static int isboundary __P((char *, char **));
43 static int mimeboundary __P((char *, char **));
44 static int mime_getchar __P((SM_FILE_T *, char **, int *));
45 static int mime_getchar_crlf __P((SM_FILE_T *, char **, int *));
47 /* character set for hex and base64 encoding */
48 static char Base16Code[] = "0123456789ABCDEF";
49 static char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
51 /* types of MIME boundaries */
52 # define MBT_SYNTAX 0 /* syntax error */
53 # define MBT_NOTSEP 1 /* not a boundary */
54 # define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */
55 # define MBT_FINAL 3 /* final boundary (trailing -- included) */
57 static char *MimeBoundaryNames[] =
59 "SYNTAX", "NOTSEP", "INTERMED", "FINAL"
62 static bool MapNLtoCRLF;
65 ** MIME8TO7 -- output 8 bit body in 7 bit format
67 ** The header has already been output -- this has to do the
68 ** 8 to 7 bit conversion. It would be easy if we didn't have
69 ** to deal with nested formats (multipart/xxx and message/rfc822).
71 ** We won't be called if we don't have to do a conversion, and
72 ** appropriate MIME-Version: and Content-Type: fields have been
73 ** output. Any Content-Transfer-Encoding: field has not been
74 ** output, and we can add it here.
77 ** mci -- mailer connection information.
78 ** header -- the header for this body part.
80 ** boundaries -- the currently pending message boundaries.
81 ** NULL if we are processing the outer portion.
82 ** flags -- to tweak processing.
83 ** level -- recursion level.
86 ** An indicator of what terminated the message part:
87 ** MBT_FINAL -- the final boundary
88 ** MBT_INTERMED -- an intermediate boundary
89 ** MBT_NOTSEP -- an end of file
90 ** SM_IO_EOF -- I/O error occurred
95 char *a_field; /* name of field */
96 char *a_value; /* value of that field */
100 mime8to7(mci, header, e, boundaries, flags, level)
103 register ENVELOPE *e;
112 size_t sectionsize, sectionhighbits;
121 struct args argv[MAXMIMEARGS];
124 char pvpbuf[MAXLINE];
125 extern unsigned char MimeTokenTab[256];
127 if (level > MAXMIMENESTING)
129 if (!bitset(EF_TOODEEP, e->e_flags))
132 sm_dprintf("mime8to7: too deep, level=%d\n",
134 usrerr("mime8to7: recursion level %d exceeded",
136 e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
141 sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
142 if (boundaries[0] == NULL)
143 sm_dprintf(" <none>");
146 for (i = 0; boundaries[i] != NULL; i++)
147 sm_dprintf(" %s", boundaries[i]);
152 p = hvalue("Content-Transfer-Encoding", header);
154 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
155 MimeTokenTab, false)) == NULL ||
162 cataddr(pvp, NULL, buf, sizeof buf, '\0');
163 cte = sm_rpool_strdup_x(e->e_rpool, buf);
166 type = subtype = NULL;
167 p = hvalue("Content-Type", header);
170 if (bitset(M87F_DIGEST, flags))
171 p = "message/rfc822";
176 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
177 MimeTokenTab, false)) != NULL &&
182 for (i = 0; pvp[i] != NULL; i++)
183 sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
186 if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
192 /* break out parameters */
193 while (*pvp != NULL && argc < MAXMIMEARGS)
195 /* skip to semicolon separator */
196 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
198 if (*pvp++ == NULL || *pvp == NULL)
201 /* complain about empty values */
202 if (strcmp(*pvp, ";") == 0)
204 usrerr("mime8to7: Empty parameter in Content-Type header");
206 /* avoid bounce loops */
207 e->e_flags |= EF_DONT_MIME;
211 /* extract field name */
212 argv[argc].a_field = *pvp++;
214 /* see if there is a value */
215 if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
216 (*++pvp == NULL || strcmp(*pvp, ";") != 0))
218 argv[argc].a_value = *pvp;
224 /* check for disaster cases */
230 /* don't propagate some flags more than one level into the message */
231 flags &= ~M87F_DIGEST;
234 ** Check for cases that can not be encoded.
236 ** For example, you can't encode certain kinds of types
237 ** or already-encoded messages. If we find this case,
238 ** just copy it through.
241 (void) sm_snprintf(buf, sizeof buf, "%.100s/%.100s", type, subtype);
242 if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
243 flags |= M87F_NO8BIT;
246 if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
248 # endif /* USE_B_CLASS */
249 if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
253 ** Multipart requires special processing.
255 ** Do a recursive descent into the message.
258 if (sm_strcasecmp(type, "multipart") == 0 &&
259 (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)) &&
260 !bitset(EF_TOODEEP, e->e_flags)
264 if (sm_strcasecmp(subtype, "digest") == 0)
265 flags |= M87F_DIGEST;
267 for (i = 0; i < argc; i++)
269 if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
272 if (i >= argc || argv[i].a_value == NULL)
274 usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
275 i >= argc ? "missing" : "bogus", p);
278 /* avoid bounce loops */
279 e->e_flags |= EF_DONT_MIME;
286 if (sm_strlcpy(bbuf, p, sizeof bbuf) >= sizeof bbuf)
288 usrerr("mime8to7: multipart boundary \"%s\" too long",
291 /* avoid bounce loops */
292 e->e_flags |= EF_DONT_MIME;
296 sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
298 for (i = 0; i < MAXMIMENESTING; i++)
300 if (boundaries[i] == NULL)
303 if (i >= MAXMIMENESTING)
306 sm_dprintf("mime8to7: too deep, i=%d\n", i);
307 if (!bitset(EF_TOODEEP, e->e_flags))
308 usrerr("mime8to7: multipart nesting boundary too deep");
310 /* avoid bounce loops */
311 e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
315 boundaries[i] = bbuf;
316 boundaries[i + 1] = NULL;
318 mci->mci_flags |= MCIF_INMIME;
320 /* skip the early "comment" prologue */
321 if (!putline("", mci))
323 mci->mci_flags &= ~MCIF_INHEADER;
325 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
328 bt = mimeboundary(buf, boundaries);
329 if (bt != MBT_NOTSEP)
331 if (!putxline(buf, strlen(buf), mci,
332 PXLF_MAPFROM|PXLF_STRIP8BIT))
335 sm_dprintf(" ...%s", buf);
337 if (sm_io_eof(e->e_dfp))
339 while (bt != MBT_FINAL)
341 auto HDR *hdr = NULL;
343 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", bbuf);
344 if (!putline(buf, mci))
347 sm_dprintf(" ...%s\n", buf);
348 collect(e->e_dfp, false, &hdr, e, false);
350 putline("+++after collect", mci);
351 if (!putheader(mci, hdr, e, flags))
354 putline("+++after putheader", mci);
355 bt = mime8to7(mci, hdr, e, boundaries, flags,
360 (void) sm_strlcpyn(buf, sizeof buf, 3, "--", bbuf, "--");
361 if (!putline(buf, mci))
364 sm_dprintf(" ...%s\n", buf);
365 boundaries[i] = NULL;
366 mci->mci_flags &= ~MCIF_INMIME;
368 /* skip the late "comment" epilogue */
369 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
372 bt = mimeboundary(buf, boundaries);
373 if (bt != MBT_NOTSEP)
375 if (!putxline(buf, strlen(buf), mci,
376 PXLF_MAPFROM|PXLF_STRIP8BIT))
379 sm_dprintf(" ...%s", buf);
381 if (sm_io_eof(e->e_dfp))
384 sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
385 MimeBoundaryNames[bt]);
390 ** Message/xxx types -- recurse exactly once.
392 ** Class 's' is predefined to have "rfc822" only.
395 if (sm_strcasecmp(type, "message") == 0)
397 if (!wordinclass(subtype, 's') ||
398 bitset(EF_TOODEEP, e->e_flags))
400 flags |= M87F_NO8BIT;
404 auto HDR *hdr = NULL;
406 if (!putline("", mci))
409 mci->mci_flags |= MCIF_INMIME;
410 collect(e->e_dfp, false, &hdr, e, false);
412 putline("+++after collect", mci);
413 if (!putheader(mci, hdr, e, flags))
416 putline("+++after putheader", mci);
417 if (hvalue("MIME-Version", hdr) == NULL &&
418 !bitset(M87F_NO8TO7, flags) &&
419 !putline("MIME-Version: 1.0", mci))
421 bt = mime8to7(mci, hdr, e, boundaries, flags,
423 mci->mci_flags &= ~MCIF_INMIME;
429 ** Non-compound body type
431 ** Compute the ratio of seven to eight bit characters;
432 ** use that as a heuristic to decide how to do the
436 sectionsize = sectionhighbits = 0;
437 if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
439 /* remember where we were */
440 offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
442 syserr("mime8to7: cannot sm_io_tell on %cf%s",
443 DATAFL_LETTER, e->e_id);
445 /* do a scan of this body type to count character types */
446 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
449 if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
451 for (p = buf; *p != '\0'; p++)
453 /* count bytes with the high bit set */
455 if (bitset(0200, *p))
460 ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
461 ** assume base64. This heuristic avoids double-reading
462 ** large graphics or video files.
465 if (sectionsize >= 4096 &&
466 sectionhighbits > sectionsize / 4)
470 /* return to the original offset for processing */
471 /* XXX use relative seeks to handle >31 bit file sizes? */
472 if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
473 syserr("mime8to7: cannot sm_io_fseek on %cf%s",
474 DATAFL_LETTER, e->e_id);
476 sm_io_clearerr(e->e_dfp);
480 ** Heuristically determine encoding method.
481 ** If more than 1/8 of the total characters have the
482 ** eighth bit set, use base64; else use quoted-printable.
483 ** However, only encode binary encoded data as base64,
484 ** since otherwise the NL=>CRLF mapping will be a problem.
489 sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
490 (long) sectionhighbits, (long) sectionsize,
491 cte == NULL ? "[none]" : cte,
492 type == NULL ? "[none]" : type,
493 subtype == NULL ? "[none]" : subtype);
495 if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
496 sectionsize = sectionhighbits;
499 if (sectionhighbits == 0)
501 /* no encoding necessary */
503 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
505 !bitset(M87F_NO8TO7, flags))
508 ** Skip _unless_ in MIME mode and potentially
509 ** converting from 8 bit to 7 bit MIME. See
510 ** putheader() for the counterpart where the
511 ** CTE header is skipped in the opposite
515 (void) sm_snprintf(buf, sizeof buf,
516 "Content-Transfer-Encoding: %.200s", cte);
517 if (!putline(buf, mci))
520 sm_dprintf(" ...%s\n", buf);
522 if (!putline("", mci))
524 mci->mci_flags &= ~MCIF_INHEADER;
525 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
528 bt = mimeboundary(buf, boundaries);
529 if (bt != MBT_NOTSEP)
531 if (!putline(buf, mci))
534 if (sm_io_eof(e->e_dfp))
537 else if (!MapNLtoCRLF ||
538 (sectionsize / 8 < sectionhighbits && !use_qp))
540 /* use base64 encoding */
544 sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
545 if (!putline("Content-Transfer-Encoding: base64", mci))
547 (void) sm_snprintf(buf, sizeof buf,
548 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
549 MyHostName, e->e_id);
550 if (!putline(buf, mci) || !putline("", mci))
552 mci->mci_flags &= ~MCIF_INHEADER;
553 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
559 if (!putline(buf, mci))
565 *bp++ = Base64Code[(c1 >> 2)];
566 c1 = (c1 & 0x03) << 4;
567 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
570 *bp++ = Base64Code[c1];
575 c1 |= (c2 >> 4) & 0x0f;
576 *bp++ = Base64Code[c1];
577 c1 = (c2 & 0x0f) << 2;
578 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
581 *bp++ = Base64Code[c1];
585 c1 |= (c2 >> 6) & 0x03;
586 *bp++ = Base64Code[c1];
587 *bp++ = Base64Code[c2 & 0x3f];
590 if (!putline(buf, mci))
595 /* use quoted-printable encoding */
600 /* set up map of characters that must be mapped */
602 for (c1 = 0x00; c1 < 0x20; c1++)
603 setbitn(c1, badchars);
604 clrbitn('\t', badchars);
605 for (c1 = 0x7f; c1 < 0x100; c1++)
606 setbitn(c1, badchars);
607 setbitn('=', badchars);
608 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
609 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
610 setbitn(*p, badchars);
613 sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
614 if (!putline("Content-Transfer-Encoding: quoted-printable",
617 (void) sm_snprintf(buf, sizeof buf,
618 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
619 MyHostName, e->e_id);
620 if (!putline(buf, mci) || !putline("", mci))
622 mci->mci_flags &= ~MCIF_INHEADER;
625 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
630 if (c2 == ' ' || c2 == '\t')
633 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
634 *bp++ = Base16Code[c2 & 0x0f];
636 if (buf[0] == '.' && bp == &buf[1])
639 *bp++ = Base16Code[('.' >> 4) & 0x0f];
640 *bp++ = Base16Code['.' & 0x0f];
643 if (!putline(buf, mci))
645 linelen = fromstate = 0;
650 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
651 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
658 else if (c2 == ' ' || c2 == '\t')
664 (linelen > 75 || c1 != '.' ||
665 (linelen > 73 && c2 == '.')))
667 if (linelen > 73 && c2 == '.')
673 if (!putline(buf, mci))
675 linelen = fromstate = 0;
683 if (bitnset(bitidx(c1), badchars))
686 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
687 *bp++ = Base16Code[c1 & 0x0f];
690 else if (c1 != ' ' && c1 != '\t')
692 if (linelen < 4 && c1 == "From"[linelen])
700 /* output any saved character */
701 if (c2 == ' ' || c2 == '\t')
704 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
705 *bp++ = Base16Code[c2 & 0x0f];
709 if (linelen > 0 || boundaries[0] != NULL)
712 if (!putline(buf, mci))
718 sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
725 ** MIME_GETCHAR -- get a character for MIME processing
727 ** Treats boundaries as SM_IO_EOF.
730 ** fp -- the input file.
731 ** boundaries -- the current MIME boundaries.
732 ** btp -- if the return value is SM_IO_EOF, *btp is set to
733 ** the type of the boundary.
736 ** The next character in the input stream.
740 mime_getchar(fp, boundaries, btp)
741 register SM_FILE_T *fp;
746 static unsigned char *bp = NULL;
747 static int buflen = 0;
748 static bool atbol = true; /* at beginning of line */
749 static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
750 static unsigned char buf[128]; /* need not be a full line */
751 int start = 0; /* indicates position of - in buffer */
753 if (buflen == 1 && *bp == '\n')
755 /* last \n in buffer may be part of next MIME boundary */
764 c = sm_io_getc(fp, SM_TIME_DEFAULT);
769 /* might be part of a MIME boundary */
772 c = sm_io_getc(fp, SM_TIME_DEFAULT);
775 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
784 if (atbol && c == '-')
786 /* check for a message boundary */
787 c = sm_io_getc(fp, SM_TIME_DEFAULT);
794 buflen = bp - buf - 1;
799 /* got "--", now check for rest of separator */
801 while (bp < &buf[sizeof buf - 2] &&
802 (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
807 *bp = '\0'; /* XXX simply cut off? */
808 bt = mimeboundary((char *) &buf[start], boundaries);
813 /* we have a message boundary */
819 if (bp < &buf[sizeof buf - 2] && c != SM_IO_EOF)
824 buflen = bp - buf - 1;
834 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
837 ** fp -- the input file.
838 ** boundaries -- the current MIME boundaries.
839 ** btp -- if the return value is SM_IO_EOF, *btp is set to
840 ** the type of the boundary.
843 ** The next character in the input stream.
847 mime_getchar_crlf(fp, boundaries, btp)
848 register SM_FILE_T *fp;
852 static bool sendlf = false;
860 c = mime_getchar(fp, boundaries, btp);
861 if (c == '\n' && MapNLtoCRLF)
869 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
872 ** line -- the input line.
873 ** boundaries -- the set of currently pending boundaries.
876 ** MBT_NOTSEP -- if this is not a separator line
877 ** MBT_INTERMED -- if this is an intermediate separator
878 ** MBT_FINAL -- if this is a final boundary
879 ** MBT_SYNTAX -- if this is a boundary for the wrong
880 ** enclosure -- i.e., a syntax error.
884 mimeboundary(line, boundaries)
888 int type = MBT_NOTSEP;
892 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
895 if (i > 0 && line[i - 1] == '\n')
898 /* strip off trailing whitespace */
899 while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
901 || line[i - 1] == '\r'
902 #endif /* _FFR_MIME_CR_OK */
909 sm_dprintf("mimeboundary: line=\"%s\"... ", line);
911 /* check for this as an intermediate boundary */
912 if (isboundary(&line[2], boundaries) >= 0)
914 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
916 /* check for a final boundary */
918 if (isboundary(&line[2], boundaries) >= 0)
925 sm_dprintf("%s\n", MimeBoundaryNames[type]);
929 ** DEFCHARSET -- return default character set for message
931 ** The first choice for character set is for the mailer
932 ** corresponding to the envelope sender. If neither that
933 ** nor the global configuration file has a default character
934 ** set defined, return "unknown-8bit" as recommended by
935 ** RFC 1428 section 3.
938 ** e -- the envelope for this message.
941 ** The default character set for that mailer.
946 register ENVELOPE *e;
948 if (e != NULL && e->e_from.q_mailer != NULL &&
949 e->e_from.q_mailer->m_defcharset != NULL)
950 return e->e_from.q_mailer->m_defcharset;
951 if (DefaultCharSet != NULL)
952 return DefaultCharSet;
953 return "unknown-8bit";
956 ** ISBOUNDARY -- is a given string a currently valid boundary?
959 ** line -- the current input line.
960 ** boundaries -- the list of valid boundaries.
963 ** The index number in boundaries if the line is found.
969 isboundary(line, boundaries)
975 for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
977 if (strcmp(line, boundaries[i]) == 0)
982 #endif /* MIME8TO7 */
985 static int mime_fromqp __P((unsigned char *, unsigned char **, int));
988 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
990 ** This is a hack. Supports translating the two 7-bit body-encodings
991 ** (quoted-printable and base64) to 8-bit coded bodies.
993 ** There is not much point in supporting multipart here, as the UA
994 ** will be able to deal with encoded MIME bodies if it can parse MIME
995 ** multipart messages.
997 ** Note also that we won't be called unless it is a text/plain MIME
998 ** message, encoded base64 or QP and mailer flag '9' has been defined
1001 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
1004 ** mci -- mailer connection information.
1005 ** header -- the header for this body part.
1009 ** true iff body was written successfully
1012 static char index_64[128] =
1014 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1015 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1016 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
1017 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
1018 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
1019 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
1020 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
1021 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
1024 # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
1027 mime7to8(mci, header, e)
1030 register ENVELOPE *e;
1036 unsigned char *fbufp;
1038 unsigned char fbuf[MAXLINE + 1];
1039 char pvpbuf[MAXLINE];
1040 extern unsigned char MimeTokenTab[256];
1042 p = hvalue("Content-Transfer-Encoding", header);
1044 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
1045 MimeTokenTab, false)) == NULL ||
1048 /* "can't happen" -- upper level should have caught this */
1049 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
1051 /* avoid bounce loops */
1052 e->e_flags |= EF_DONT_MIME;
1054 /* cheap failsafe algorithm -- should work on text/plain */
1057 (void) sm_snprintf(buf, sizeof buf,
1058 "Content-Transfer-Encoding: %s", p);
1059 if (!putline(buf, mci))
1062 if (!putline("", mci))
1064 mci->mci_flags &= ~MCIF_INHEADER;
1065 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
1068 if (!putline(buf, mci))
1073 cataddr(pvp, NULL, buf, sizeof buf, '\0');
1074 cte = sm_rpool_strdup_x(e->e_rpool, buf);
1076 mci->mci_flags |= MCIF_INHEADER;
1077 if (!putline("Content-Transfer-Encoding: 8bit", mci))
1079 (void) sm_snprintf(buf, sizeof buf,
1080 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1081 cte, MyHostName, e->e_id);
1082 if (!putline(buf, mci) || !putline("", mci))
1084 mci->mci_flags &= ~MCIF_INHEADER;
1087 ** Translate body encoding to 8-bit. Supports two types of
1088 ** encodings; "base64" and "quoted-printable". Assume qp if
1089 ** it is not base64.
1092 pxflags = PXLF_MAPFROM;
1093 if (sm_strcasecmp(cte, "base64") == 0)
1098 while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1101 if (isascii(c1) && isspace(c1))
1106 c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1107 } while (isascii(c2) && isspace(c2));
1108 if (c2 == SM_IO_EOF)
1113 c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1114 } while (isascii(c3) && isspace(c3));
1115 if (c3 == SM_IO_EOF)
1120 c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1121 } while (isascii(c4) && isspace(c4));
1122 if (c4 == SM_IO_EOF)
1125 if (c1 == '=' || c2 == '=')
1131 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1133 #else /* MIME7TO8_OLD */
1134 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1137 pxflags |= PXLF_NOADDEOL; \
1139 #endif /* MIME7TO8_OLD */
1144 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
1147 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1149 pxflags &= ~PXLF_NOADDEOL; \
1154 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1159 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1164 *fbufp = ((c3 & 0x03) << 6) | c4;
1172 /* quoted-printable */
1173 pxflags |= PXLF_NOADDEOL;
1175 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1176 sizeof buf) != NULL)
1178 off = mime_fromqp((unsigned char *) buf, &fbufp,
1179 &fbuf[MAXLINE] - fbufp);
1184 if (fbufp - fbuf > 0)
1186 if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1191 if (off >= 0 && buf[off] != '\0')
1193 off = mime_fromqp((unsigned char *) (buf + off),
1195 &fbuf[MAXLINE] - fbufp);
1201 /* force out partial last line */
1205 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1210 ** The decoded text may end without an EOL. Since this function
1211 ** is only called for text/plain MIME messages, it is safe to
1212 ** add an extra one at the end just in case. This is a hack,
1213 ** but so is auto-converting MIME in the first place.
1216 if (!putline("", mci))
1220 sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1227 ** The following is based on Borenstein's "codes.c" module, with simplifying
1228 ** changes as we do not deal with multipart, and to do the translation in-core,
1229 ** with an attempt to prevent overrun of output buffers.
1231 ** What is needed here are changes to defend this code better against
1232 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1235 static char index_hex[128] =
1237 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1238 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1239 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1240 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1241 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1242 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1243 -1,10,11,12, 13,14,15,-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
1247 # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1250 ** MIME_FROMQP -- decode quoted printable string
1253 ** infile -- input (encoded) string
1254 ** outfile -- output string
1255 ** maxlen -- size of output buffer
1258 ** -2 if decoding failure
1259 ** -1 if infile completely decoded into outfile
1260 ** >= 0 is the position in infile decoding
1261 ** reached before maxlen was reached
1265 mime_fromqp(infile, outfile, maxlen)
1266 unsigned char *infile;
1267 unsigned char **outfile;
1268 int maxlen; /* Max # of chars allowed in outfile */
1274 /* decrement by one for trailing '\0', at least one other char */
1279 while ((c1 = *infile++) != '\0' && nchar < maxlen)
1283 if ((c1 = *infile++) == '\0')
1286 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1288 /* ignore it and the rest of the buffer */
1295 if ((c2 = *infile++) == '\0')
1300 } while ((c2 = HEXCHAR(c2)) == -1);
1305 *(*outfile)++ = c1 << 4 | c2;
1316 *(*outfile)++ = '\0';
1317 if (nchar >= maxlen)
1318 return (infile - b - 1);
1321 #endif /* MIME7TO8 */