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.147 2007/09/26 23:29:11 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', false);
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 if (!bitset(MCIF_INLONGLINE, mci->mci_flags))
530 bt = mimeboundary(buf, boundaries);
531 if (bt != MBT_NOTSEP)
534 if (!putxline(buf, strlen(buf), mci,
535 PXLF_MAPFROM|PXLF_NOADDEOL))
538 if (sm_io_eof(e->e_dfp))
541 else if (!MapNLtoCRLF ||
542 (sectionsize / 8 < sectionhighbits && !use_qp))
544 /* use base64 encoding */
548 sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
549 if (!putline("Content-Transfer-Encoding: base64", mci))
551 (void) sm_snprintf(buf, sizeof(buf),
552 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
553 MyHostName, e->e_id);
554 if (!putline(buf, mci) || !putline("", mci))
556 mci->mci_flags &= ~MCIF_INHEADER;
557 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
563 if (!putline(buf, mci))
569 *bp++ = Base64Code[(c1 >> 2)];
570 c1 = (c1 & 0x03) << 4;
571 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
574 *bp++ = Base64Code[c1];
579 c1 |= (c2 >> 4) & 0x0f;
580 *bp++ = Base64Code[c1];
581 c1 = (c2 & 0x0f) << 2;
582 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
585 *bp++ = Base64Code[c1];
589 c1 |= (c2 >> 6) & 0x03;
590 *bp++ = Base64Code[c1];
591 *bp++ = Base64Code[c2 & 0x3f];
594 if (!putline(buf, mci))
599 /* use quoted-printable encoding */
604 /* set up map of characters that must be mapped */
606 for (c1 = 0x00; c1 < 0x20; c1++)
607 setbitn(c1, badchars);
608 clrbitn('\t', badchars);
609 for (c1 = 0x7f; c1 < 0x100; c1++)
610 setbitn(c1, badchars);
611 setbitn('=', badchars);
612 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
613 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
614 setbitn(*p, badchars);
617 sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
618 if (!putline("Content-Transfer-Encoding: quoted-printable",
621 (void) sm_snprintf(buf, sizeof(buf),
622 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
623 MyHostName, e->e_id);
624 if (!putline(buf, mci) || !putline("", mci))
626 mci->mci_flags &= ~MCIF_INHEADER;
629 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
634 if (c2 == ' ' || c2 == '\t')
637 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
638 *bp++ = Base16Code[c2 & 0x0f];
640 if (buf[0] == '.' && bp == &buf[1])
643 *bp++ = Base16Code[('.' >> 4) & 0x0f];
644 *bp++ = Base16Code['.' & 0x0f];
647 if (!putline(buf, mci))
649 linelen = fromstate = 0;
654 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
655 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
662 else if (c2 == ' ' || c2 == '\t')
668 (linelen > 75 || c1 != '.' ||
669 (linelen > 73 && c2 == '.')))
671 if (linelen > 73 && c2 == '.')
677 if (!putline(buf, mci))
679 linelen = fromstate = 0;
687 if (bitnset(bitidx(c1), badchars))
690 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
691 *bp++ = Base16Code[c1 & 0x0f];
694 else if (c1 != ' ' && c1 != '\t')
696 if (linelen < 4 && c1 == "From"[linelen])
704 /* output any saved character */
705 if (c2 == ' ' || c2 == '\t')
708 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
709 *bp++ = Base16Code[c2 & 0x0f];
713 if (linelen > 0 || boundaries[0] != NULL)
716 if (!putline(buf, mci))
722 sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
729 ** MIME_GETCHAR -- get a character for MIME processing
731 ** Treats boundaries as SM_IO_EOF.
734 ** fp -- the input file.
735 ** boundaries -- the current MIME boundaries.
736 ** btp -- if the return value is SM_IO_EOF, *btp is set to
737 ** the type of the boundary.
740 ** The next character in the input stream.
744 mime_getchar(fp, boundaries, btp)
745 register SM_FILE_T *fp;
750 static unsigned char *bp = NULL;
751 static int buflen = 0;
752 static bool atbol = true; /* at beginning of line */
753 static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
754 static unsigned char buf[128]; /* need not be a full line */
755 int start = 0; /* indicates position of - in buffer */
757 if (buflen == 1 && *bp == '\n')
759 /* last \n in buffer may be part of next MIME boundary */
768 c = sm_io_getc(fp, SM_TIME_DEFAULT);
773 /* might be part of a MIME boundary */
776 c = sm_io_getc(fp, SM_TIME_DEFAULT);
779 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
788 if (atbol && c == '-')
790 /* check for a message boundary */
791 c = sm_io_getc(fp, SM_TIME_DEFAULT);
798 buflen = bp - buf - 1;
803 /* got "--", now check for rest of separator */
805 while (bp < &buf[sizeof(buf) - 2] &&
806 (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
811 *bp = '\0'; /* XXX simply cut off? */
812 bt = mimeboundary((char *) &buf[start], boundaries);
817 /* we have a message boundary */
823 if (bp < &buf[sizeof(buf) - 2] && c != SM_IO_EOF)
828 buflen = bp - buf - 1;
838 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
841 ** fp -- the input file.
842 ** boundaries -- the current MIME boundaries.
843 ** btp -- if the return value is SM_IO_EOF, *btp is set to
844 ** the type of the boundary.
847 ** The next character in the input stream.
851 mime_getchar_crlf(fp, boundaries, btp)
852 register SM_FILE_T *fp;
856 static bool sendlf = false;
864 c = mime_getchar(fp, boundaries, btp);
865 if (c == '\n' && MapNLtoCRLF)
873 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
876 ** line -- the input line.
877 ** boundaries -- the set of currently pending boundaries.
880 ** MBT_NOTSEP -- if this is not a separator line
881 ** MBT_INTERMED -- if this is an intermediate separator
882 ** MBT_FINAL -- if this is a final boundary
883 ** MBT_SYNTAX -- if this is a boundary for the wrong
884 ** enclosure -- i.e., a syntax error.
888 mimeboundary(line, boundaries)
892 int type = MBT_NOTSEP;
896 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
899 if (i > 0 && line[i - 1] == '\n')
902 /* strip off trailing whitespace */
903 while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
905 || line[i - 1] == '\r'
906 #endif /* _FFR_MIME_CR_OK */
913 sm_dprintf("mimeboundary: line=\"%s\"... ", line);
915 /* check for this as an intermediate boundary */
916 if (isboundary(&line[2], boundaries) >= 0)
918 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
920 /* check for a final boundary */
922 if (isboundary(&line[2], boundaries) >= 0)
929 sm_dprintf("%s\n", MimeBoundaryNames[type]);
933 ** DEFCHARSET -- return default character set for message
935 ** The first choice for character set is for the mailer
936 ** corresponding to the envelope sender. If neither that
937 ** nor the global configuration file has a default character
938 ** set defined, return "unknown-8bit" as recommended by
939 ** RFC 1428 section 3.
942 ** e -- the envelope for this message.
945 ** The default character set for that mailer.
950 register ENVELOPE *e;
952 if (e != NULL && e->e_from.q_mailer != NULL &&
953 e->e_from.q_mailer->m_defcharset != NULL)
954 return e->e_from.q_mailer->m_defcharset;
955 if (DefaultCharSet != NULL)
956 return DefaultCharSet;
957 return "unknown-8bit";
960 ** ISBOUNDARY -- is a given string a currently valid boundary?
963 ** line -- the current input line.
964 ** boundaries -- the list of valid boundaries.
967 ** The index number in boundaries if the line is found.
973 isboundary(line, boundaries)
979 for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
981 if (strcmp(line, boundaries[i]) == 0)
986 #endif /* MIME8TO7 */
989 static int mime_fromqp __P((unsigned char *, unsigned char **, int));
992 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
994 ** This is a hack. Supports translating the two 7-bit body-encodings
995 ** (quoted-printable and base64) to 8-bit coded bodies.
997 ** There is not much point in supporting multipart here, as the UA
998 ** will be able to deal with encoded MIME bodies if it can parse MIME
999 ** multipart messages.
1001 ** Note also that we won't be called unless it is a text/plain MIME
1002 ** message, encoded base64 or QP and mailer flag '9' has been defined
1005 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
1008 ** mci -- mailer connection information.
1009 ** header -- the header for this body part.
1013 ** true iff body was written successfully
1016 static char index_64[128] =
1018 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1019 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1020 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
1021 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
1022 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
1023 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
1024 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
1025 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
1028 # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
1031 mime7to8(mci, header, e)
1034 register ENVELOPE *e;
1040 unsigned char *fbufp;
1042 unsigned char fbuf[MAXLINE + 1];
1043 char pvpbuf[MAXLINE];
1044 extern unsigned char MimeTokenTab[256];
1046 p = hvalue("Content-Transfer-Encoding", header);
1048 (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1049 MimeTokenTab, false)) == NULL ||
1052 /* "can't happen" -- upper level should have caught this */
1053 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
1055 /* avoid bounce loops */
1056 e->e_flags |= EF_DONT_MIME;
1058 /* cheap failsafe algorithm -- should work on text/plain */
1061 (void) sm_snprintf(buf, sizeof(buf),
1062 "Content-Transfer-Encoding: %s", p);
1063 if (!putline(buf, mci))
1066 if (!putline("", mci))
1068 mci->mci_flags &= ~MCIF_INHEADER;
1069 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
1072 if (!putline(buf, mci))
1077 cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
1078 cte = sm_rpool_strdup_x(e->e_rpool, buf);
1080 mci->mci_flags |= MCIF_INHEADER;
1081 if (!putline("Content-Transfer-Encoding: 8bit", mci))
1083 (void) sm_snprintf(buf, sizeof(buf),
1084 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1085 cte, MyHostName, e->e_id);
1086 if (!putline(buf, mci) || !putline("", mci))
1088 mci->mci_flags &= ~MCIF_INHEADER;
1091 ** Translate body encoding to 8-bit. Supports two types of
1092 ** encodings; "base64" and "quoted-printable". Assume qp if
1093 ** it is not base64.
1096 pxflags = PXLF_MAPFROM;
1097 if (sm_strcasecmp(cte, "base64") == 0)
1102 while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1105 if (isascii(c1) && isspace(c1))
1110 c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1111 } while (isascii(c2) && isspace(c2));
1112 if (c2 == SM_IO_EOF)
1117 c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1118 } while (isascii(c3) && isspace(c3));
1119 if (c3 == SM_IO_EOF)
1124 c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1125 } while (isascii(c4) && isspace(c4));
1126 if (c4 == SM_IO_EOF)
1129 if (c1 == '=' || c2 == '=')
1135 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1137 #else /* MIME7TO8_OLD */
1138 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1141 pxflags |= PXLF_NOADDEOL; \
1143 #endif /* MIME7TO8_OLD */
1148 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
1151 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1153 pxflags &= ~PXLF_NOADDEOL; \
1158 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1163 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1168 *fbufp = ((c3 & 0x03) << 6) | c4;
1176 /* quoted-printable */
1177 pxflags |= PXLF_NOADDEOL;
1179 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1180 sizeof(buf)) != NULL)
1182 off = mime_fromqp((unsigned char *) buf, &fbufp,
1183 &fbuf[MAXLINE] - fbufp);
1188 if (fbufp - fbuf > 0)
1190 if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1195 if (off >= 0 && buf[off] != '\0')
1197 off = mime_fromqp((unsigned char *) (buf + off),
1199 &fbuf[MAXLINE] - fbufp);
1205 /* force out partial last line */
1209 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1214 ** The decoded text may end without an EOL. Since this function
1215 ** is only called for text/plain MIME messages, it is safe to
1216 ** add an extra one at the end just in case. This is a hack,
1217 ** but so is auto-converting MIME in the first place.
1220 if (!putline("", mci))
1224 sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1231 ** The following is based on Borenstein's "codes.c" module, with simplifying
1232 ** changes as we do not deal with multipart, and to do the translation in-core,
1233 ** with an attempt to prevent overrun of output buffers.
1235 ** What is needed here are changes to defend this code better against
1236 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1239 static char index_hex[128] =
1241 -1,-1,-1,-1, -1,-1,-1,-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,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1244 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1245 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1246 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-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
1251 # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1254 ** MIME_FROMQP -- decode quoted printable string
1257 ** infile -- input (encoded) string
1258 ** outfile -- output string
1259 ** maxlen -- size of output buffer
1262 ** -2 if decoding failure
1263 ** -1 if infile completely decoded into outfile
1264 ** >= 0 is the position in infile decoding
1265 ** reached before maxlen was reached
1269 mime_fromqp(infile, outfile, maxlen)
1270 unsigned char *infile;
1271 unsigned char **outfile;
1272 int maxlen; /* Max # of chars allowed in outfile */
1278 /* decrement by one for trailing '\0', at least one other char */
1283 while ((c1 = *infile++) != '\0' && nchar < maxlen)
1287 if ((c1 = *infile++) == '\0')
1290 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1292 /* ignore it and the rest of the buffer */
1299 if ((c2 = *infile++) == '\0')
1304 } while ((c2 = HEXCHAR(c2)) == -1);
1309 *(*outfile)++ = c1 << 4 | c2;
1320 *(*outfile)++ = '\0';
1321 if (nchar >= maxlen)
1322 return (infile - b - 1);
1325 #endif /* MIME7TO8 */