2 * Copyright (c) 1998-2003 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.139 2006/03/01 18:07:45 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.
85 ** An indicator of what terminated the message part:
86 ** MBT_FINAL -- the final boundary
87 ** MBT_INTERMED -- an intermediate boundary
88 ** MBT_NOTSEP -- an end of file
89 ** SM_IO_EOF -- I/O error occurred
94 char *a_field; /* name of field */
95 char *a_value; /* value of that field */
99 mime8to7(mci, header, e, boundaries, flags)
102 register ENVELOPE *e;
110 size_t sectionsize, sectionhighbits;
119 struct args argv[MAXMIMEARGS];
122 char pvpbuf[MAXLINE];
123 extern unsigned char MimeTokenTab[256];
127 sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
128 if (boundaries[0] == NULL)
129 sm_dprintf(" <none>");
132 for (i = 0; boundaries[i] != NULL; i++)
133 sm_dprintf(" %s", boundaries[i]);
138 p = hvalue("Content-Transfer-Encoding", header);
140 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
141 MimeTokenTab, false)) == NULL ||
148 cataddr(pvp, NULL, buf, sizeof buf, '\0');
149 cte = sm_rpool_strdup_x(e->e_rpool, buf);
152 type = subtype = NULL;
153 p = hvalue("Content-Type", header);
156 if (bitset(M87F_DIGEST, flags))
157 p = "message/rfc822";
162 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
163 MimeTokenTab, false)) != NULL &&
168 for (i = 0; pvp[i] != NULL; i++)
169 sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
172 if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
178 /* break out parameters */
179 while (*pvp != NULL && argc < MAXMIMEARGS)
181 /* skip to semicolon separator */
182 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
184 if (*pvp++ == NULL || *pvp == NULL)
187 /* complain about empty values */
188 if (strcmp(*pvp, ";") == 0)
190 usrerr("mime8to7: Empty parameter in Content-Type header");
192 /* avoid bounce loops */
193 e->e_flags |= EF_DONT_MIME;
197 /* extract field name */
198 argv[argc].a_field = *pvp++;
200 /* see if there is a value */
201 if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
202 (*++pvp == NULL || strcmp(*pvp, ";") != 0))
204 argv[argc].a_value = *pvp;
210 /* check for disaster cases */
216 /* don't propogate some flags more than one level into the message */
217 flags &= ~M87F_DIGEST;
220 ** Check for cases that can not be encoded.
222 ** For example, you can't encode certain kinds of types
223 ** or already-encoded messages. If we find this case,
224 ** just copy it through.
227 (void) sm_snprintf(buf, sizeof buf, "%.100s/%.100s", type, subtype);
228 if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
229 flags |= M87F_NO8BIT;
232 if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
234 # endif /* USE_B_CLASS */
235 if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
239 ** Multipart requires special processing.
241 ** Do a recursive descent into the message.
244 if (sm_strcasecmp(type, "multipart") == 0 &&
245 (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)))
248 if (sm_strcasecmp(subtype, "digest") == 0)
249 flags |= M87F_DIGEST;
251 for (i = 0; i < argc; i++)
253 if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
256 if (i >= argc || argv[i].a_value == NULL)
258 usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
259 i >= argc ? "missing" : "bogus", p);
262 /* avoid bounce loops */
263 e->e_flags |= EF_DONT_MIME;
270 if (sm_strlcpy(bbuf, p, sizeof bbuf) >= sizeof bbuf)
272 usrerr("mime8to7: multipart boundary \"%s\" too long",
275 /* avoid bounce loops */
276 e->e_flags |= EF_DONT_MIME;
280 sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
282 for (i = 0; i < MAXMIMENESTING; i++)
284 if (boundaries[i] == NULL)
287 if (i >= MAXMIMENESTING)
289 usrerr("mime8to7: multipart nesting boundary too deep");
291 /* avoid bounce loops */
292 e->e_flags |= EF_DONT_MIME;
296 boundaries[i] = bbuf;
297 boundaries[i + 1] = NULL;
299 mci->mci_flags |= MCIF_INMIME;
301 /* skip the early "comment" prologue */
302 if (!putline("", mci))
304 mci->mci_flags &= ~MCIF_INHEADER;
306 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
309 bt = mimeboundary(buf, boundaries);
310 if (bt != MBT_NOTSEP)
312 if (!putxline(buf, strlen(buf), mci,
313 PXLF_MAPFROM|PXLF_STRIP8BIT))
316 sm_dprintf(" ...%s", buf);
318 if (sm_io_eof(e->e_dfp))
320 while (bt != MBT_FINAL)
322 auto HDR *hdr = NULL;
324 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", bbuf);
325 if (!putline(buf, mci))
328 sm_dprintf(" ...%s\n", buf);
329 collect(e->e_dfp, false, &hdr, e, false);
331 putline("+++after collect", mci);
332 if (!putheader(mci, hdr, e, flags))
335 putline("+++after putheader", mci);
336 bt = mime8to7(mci, hdr, e, boundaries, flags);
340 (void) sm_strlcpyn(buf, sizeof buf, 3, "--", bbuf, "--");
341 if (!putline(buf, mci))
344 sm_dprintf(" ...%s\n", buf);
345 boundaries[i] = NULL;
346 mci->mci_flags &= ~MCIF_INMIME;
348 /* skip the late "comment" epilogue */
349 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
352 bt = mimeboundary(buf, boundaries);
353 if (bt != MBT_NOTSEP)
355 if (!putxline(buf, strlen(buf), mci,
356 PXLF_MAPFROM|PXLF_STRIP8BIT))
359 sm_dprintf(" ...%s", buf);
361 if (sm_io_eof(e->e_dfp))
364 sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
365 MimeBoundaryNames[bt]);
370 ** Message/xxx types -- recurse exactly once.
372 ** Class 's' is predefined to have "rfc822" only.
375 if (sm_strcasecmp(type, "message") == 0)
377 if (!wordinclass(subtype, 's'))
379 flags |= M87F_NO8BIT;
383 auto HDR *hdr = NULL;
385 if (!putline("", mci))
388 mci->mci_flags |= MCIF_INMIME;
389 collect(e->e_dfp, false, &hdr, e, false);
391 putline("+++after collect", mci);
392 if (!putheader(mci, hdr, e, flags))
395 putline("+++after putheader", mci);
396 if (hvalue("MIME-Version", hdr) == NULL &&
397 !bitset(M87F_NO8TO7, flags) &&
398 !putline("MIME-Version: 1.0", mci))
400 bt = mime8to7(mci, hdr, e, boundaries, flags);
401 mci->mci_flags &= ~MCIF_INMIME;
407 ** Non-compound body type
409 ** Compute the ratio of seven to eight bit characters;
410 ** use that as a heuristic to decide how to do the
414 sectionsize = sectionhighbits = 0;
415 if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
417 /* remember where we were */
418 offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
420 syserr("mime8to7: cannot sm_io_tell on %cf%s",
421 DATAFL_LETTER, e->e_id);
423 /* do a scan of this body type to count character types */
424 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
427 if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
429 for (p = buf; *p != '\0'; p++)
431 /* count bytes with the high bit set */
433 if (bitset(0200, *p))
438 ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
439 ** assume base64. This heuristic avoids double-reading
440 ** large graphics or video files.
443 if (sectionsize >= 4096 &&
444 sectionhighbits > sectionsize / 4)
448 /* return to the original offset for processing */
449 /* XXX use relative seeks to handle >31 bit file sizes? */
450 if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
451 syserr("mime8to7: cannot sm_io_fseek on %cf%s",
452 DATAFL_LETTER, e->e_id);
454 sm_io_clearerr(e->e_dfp);
458 ** Heuristically determine encoding method.
459 ** If more than 1/8 of the total characters have the
460 ** eighth bit set, use base64; else use quoted-printable.
461 ** However, only encode binary encoded data as base64,
462 ** since otherwise the NL=>CRLF mapping will be a problem.
467 sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
468 (long) sectionhighbits, (long) sectionsize,
469 cte == NULL ? "[none]" : cte,
470 type == NULL ? "[none]" : type,
471 subtype == NULL ? "[none]" : subtype);
473 if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
474 sectionsize = sectionhighbits;
477 if (sectionhighbits == 0)
479 /* no encoding necessary */
481 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
483 !bitset(M87F_NO8TO7, flags))
486 ** Skip _unless_ in MIME mode and potentially
487 ** converting from 8 bit to 7 bit MIME. See
488 ** putheader() for the counterpart where the
489 ** CTE header is skipped in the opposite
493 (void) sm_snprintf(buf, sizeof buf,
494 "Content-Transfer-Encoding: %.200s", cte);
495 if (!putline(buf, mci))
498 sm_dprintf(" ...%s\n", buf);
500 if (!putline("", mci))
502 mci->mci_flags &= ~MCIF_INHEADER;
503 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
506 bt = mimeboundary(buf, boundaries);
507 if (bt != MBT_NOTSEP)
509 if (!putline(buf, mci))
512 if (sm_io_eof(e->e_dfp))
515 else if (!MapNLtoCRLF ||
516 (sectionsize / 8 < sectionhighbits && !use_qp))
518 /* use base64 encoding */
522 sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
523 if (!putline("Content-Transfer-Encoding: base64", mci))
525 (void) sm_snprintf(buf, sizeof buf,
526 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
527 MyHostName, e->e_id);
528 if (!putline(buf, mci) || !putline("", mci))
530 mci->mci_flags &= ~MCIF_INHEADER;
531 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
537 if (!putline(buf, mci))
543 *bp++ = Base64Code[(c1 >> 2)];
544 c1 = (c1 & 0x03) << 4;
545 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
548 *bp++ = Base64Code[c1];
553 c1 |= (c2 >> 4) & 0x0f;
554 *bp++ = Base64Code[c1];
555 c1 = (c2 & 0x0f) << 2;
556 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
559 *bp++ = Base64Code[c1];
563 c1 |= (c2 >> 6) & 0x03;
564 *bp++ = Base64Code[c1];
565 *bp++ = Base64Code[c2 & 0x3f];
568 if (!putline(buf, mci))
573 /* use quoted-printable encoding */
578 /* set up map of characters that must be mapped */
580 for (c1 = 0x00; c1 < 0x20; c1++)
581 setbitn(c1, badchars);
582 clrbitn('\t', badchars);
583 for (c1 = 0x7f; c1 < 0x100; c1++)
584 setbitn(c1, badchars);
585 setbitn('=', badchars);
586 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
587 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
588 setbitn(*p, badchars);
591 sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
592 if (!putline("Content-Transfer-Encoding: quoted-printable",
595 (void) sm_snprintf(buf, sizeof buf,
596 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
597 MyHostName, e->e_id);
598 if (!putline(buf, mci) || !putline("", mci))
600 mci->mci_flags &= ~MCIF_INHEADER;
603 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
608 if (c2 == ' ' || c2 == '\t')
611 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
612 *bp++ = Base16Code[c2 & 0x0f];
614 if (buf[0] == '.' && bp == &buf[1])
617 *bp++ = Base16Code[('.' >> 4) & 0x0f];
618 *bp++ = Base16Code['.' & 0x0f];
621 if (!putline(buf, mci))
623 linelen = fromstate = 0;
628 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
629 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
636 else if (c2 == ' ' || c2 == '\t')
642 (linelen > 75 || c1 != '.' ||
643 (linelen > 73 && c2 == '.')))
645 if (linelen > 73 && c2 == '.')
651 if (!putline(buf, mci))
653 linelen = fromstate = 0;
661 if (bitnset(bitidx(c1), badchars))
664 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
665 *bp++ = Base16Code[c1 & 0x0f];
668 else if (c1 != ' ' && c1 != '\t')
670 if (linelen < 4 && c1 == "From"[linelen])
678 /* output any saved character */
679 if (c2 == ' ' || c2 == '\t')
682 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
683 *bp++ = Base16Code[c2 & 0x0f];
687 if (linelen > 0 || boundaries[0] != NULL)
690 if (!putline(buf, mci))
696 sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
703 ** MIME_GETCHAR -- get a character for MIME processing
705 ** Treats boundaries as SM_IO_EOF.
708 ** fp -- the input file.
709 ** boundaries -- the current MIME boundaries.
710 ** btp -- if the return value is SM_IO_EOF, *btp is set to
711 ** the type of the boundary.
714 ** The next character in the input stream.
718 mime_getchar(fp, boundaries, btp)
719 register SM_FILE_T *fp;
724 static unsigned char *bp = NULL;
725 static int buflen = 0;
726 static bool atbol = true; /* at beginning of line */
727 static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
728 static unsigned char buf[128]; /* need not be a full line */
729 int start = 0; /* indicates position of - in buffer */
731 if (buflen == 1 && *bp == '\n')
733 /* last \n in buffer may be part of next MIME boundary */
742 c = sm_io_getc(fp, SM_TIME_DEFAULT);
747 /* might be part of a MIME boundary */
750 c = sm_io_getc(fp, SM_TIME_DEFAULT);
753 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
762 if (atbol && c == '-')
764 /* check for a message boundary */
765 c = sm_io_getc(fp, SM_TIME_DEFAULT);
772 buflen = bp - buf - 1;
777 /* got "--", now check for rest of separator */
779 while (bp < &buf[sizeof buf - 2] &&
780 (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
785 *bp = '\0'; /* XXX simply cut off? */
786 bt = mimeboundary((char *) &buf[start], boundaries);
791 /* we have a message boundary */
797 if (bp < &buf[sizeof buf - 2] && c != SM_IO_EOF)
802 buflen = bp - buf - 1;
812 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
815 ** fp -- the input file.
816 ** boundaries -- the current MIME boundaries.
817 ** btp -- if the return value is SM_IO_EOF, *btp is set to
818 ** the type of the boundary.
821 ** The next character in the input stream.
825 mime_getchar_crlf(fp, boundaries, btp)
826 register SM_FILE_T *fp;
830 static bool sendlf = false;
838 c = mime_getchar(fp, boundaries, btp);
839 if (c == '\n' && MapNLtoCRLF)
847 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
850 ** line -- the input line.
851 ** boundaries -- the set of currently pending boundaries.
854 ** MBT_NOTSEP -- if this is not a separator line
855 ** MBT_INTERMED -- if this is an intermediate separator
856 ** MBT_FINAL -- if this is a final boundary
857 ** MBT_SYNTAX -- if this is a boundary for the wrong
858 ** enclosure -- i.e., a syntax error.
862 mimeboundary(line, boundaries)
866 int type = MBT_NOTSEP;
870 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
873 if (i > 0 && line[i - 1] == '\n')
876 /* strip off trailing whitespace */
877 while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
879 || line[i - 1] == '\r'
880 #endif /* _FFR_MIME_CR_OK */
887 sm_dprintf("mimeboundary: line=\"%s\"... ", line);
889 /* check for this as an intermediate boundary */
890 if (isboundary(&line[2], boundaries) >= 0)
892 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
894 /* check for a final boundary */
896 if (isboundary(&line[2], boundaries) >= 0)
903 sm_dprintf("%s\n", MimeBoundaryNames[type]);
907 ** DEFCHARSET -- return default character set for message
909 ** The first choice for character set is for the mailer
910 ** corresponding to the envelope sender. If neither that
911 ** nor the global configuration file has a default character
912 ** set defined, return "unknown-8bit" as recommended by
913 ** RFC 1428 section 3.
916 ** e -- the envelope for this message.
919 ** The default character set for that mailer.
924 register ENVELOPE *e;
926 if (e != NULL && e->e_from.q_mailer != NULL &&
927 e->e_from.q_mailer->m_defcharset != NULL)
928 return e->e_from.q_mailer->m_defcharset;
929 if (DefaultCharSet != NULL)
930 return DefaultCharSet;
931 return "unknown-8bit";
934 ** ISBOUNDARY -- is a given string a currently valid boundary?
937 ** line -- the current input line.
938 ** boundaries -- the list of valid boundaries.
941 ** The index number in boundaries if the line is found.
947 isboundary(line, boundaries)
953 for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
955 if (strcmp(line, boundaries[i]) == 0)
960 #endif /* MIME8TO7 */
963 static int mime_fromqp __P((unsigned char *, unsigned char **, int));
966 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
968 ** This is a hack. Supports translating the two 7-bit body-encodings
969 ** (quoted-printable and base64) to 8-bit coded bodies.
971 ** There is not much point in supporting multipart here, as the UA
972 ** will be able to deal with encoded MIME bodies if it can parse MIME
973 ** multipart messages.
975 ** Note also that we won't be called unless it is a text/plain MIME
976 ** message, encoded base64 or QP and mailer flag '9' has been defined
979 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
982 ** mci -- mailer connection information.
983 ** header -- the header for this body part.
987 ** true iff body was written successfully
990 static char index_64[128] =
992 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
993 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
994 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
995 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
996 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
997 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
998 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
999 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
1002 # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
1005 mime7to8(mci, header, e)
1008 register ENVELOPE *e;
1014 unsigned char *fbufp;
1016 unsigned char fbuf[MAXLINE + 1];
1017 char pvpbuf[MAXLINE];
1018 extern unsigned char MimeTokenTab[256];
1020 p = hvalue("Content-Transfer-Encoding", header);
1022 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
1023 MimeTokenTab, false)) == NULL ||
1026 /* "can't happen" -- upper level should have caught this */
1027 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
1029 /* avoid bounce loops */
1030 e->e_flags |= EF_DONT_MIME;
1032 /* cheap failsafe algorithm -- should work on text/plain */
1035 (void) sm_snprintf(buf, sizeof buf,
1036 "Content-Transfer-Encoding: %s", p);
1037 if (!putline(buf, mci))
1040 if (!putline("", mci))
1042 mci->mci_flags &= ~MCIF_INHEADER;
1043 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
1046 if (!putline(buf, mci))
1051 cataddr(pvp, NULL, buf, sizeof buf, '\0');
1052 cte = sm_rpool_strdup_x(e->e_rpool, buf);
1054 mci->mci_flags |= MCIF_INHEADER;
1055 if (!putline("Content-Transfer-Encoding: 8bit", mci))
1057 (void) sm_snprintf(buf, sizeof buf,
1058 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1059 cte, MyHostName, e->e_id);
1060 if (!putline(buf, mci) || !putline("", mci))
1062 mci->mci_flags &= ~MCIF_INHEADER;
1065 ** Translate body encoding to 8-bit. Supports two types of
1066 ** encodings; "base64" and "quoted-printable". Assume qp if
1067 ** it is not base64.
1070 pxflags = PXLF_MAPFROM;
1071 if (sm_strcasecmp(cte, "base64") == 0)
1076 while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1079 if (isascii(c1) && isspace(c1))
1084 c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1085 } while (isascii(c2) && isspace(c2));
1086 if (c2 == SM_IO_EOF)
1091 c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1092 } while (isascii(c3) && isspace(c3));
1093 if (c3 == SM_IO_EOF)
1098 c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1099 } while (isascii(c4) && isspace(c4));
1100 if (c4 == SM_IO_EOF)
1103 if (c1 == '=' || c2 == '=')
1109 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1111 #else /* MIME7TO8_OLD */
1112 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1115 pxflags |= PXLF_NOADDEOL; \
1117 #endif /* MIME7TO8_OLD */
1122 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
1125 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1127 pxflags &= ~PXLF_NOADDEOL; \
1132 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1137 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1142 *fbufp = ((c3 & 0x03) << 6) | c4;
1150 /* quoted-printable */
1151 pxflags |= PXLF_NOADDEOL;
1153 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1154 sizeof buf) != NULL)
1156 off = mime_fromqp((unsigned char *) buf, &fbufp,
1157 &fbuf[MAXLINE] - fbufp);
1162 if (fbufp - fbuf > 0)
1164 if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1169 if (off >= 0 && buf[off] != '\0')
1171 off = mime_fromqp((unsigned char *) (buf + off),
1173 &fbuf[MAXLINE] - fbufp);
1179 /* force out partial last line */
1183 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1188 ** The decoded text may end without an EOL. Since this function
1189 ** is only called for text/plain MIME messages, it is safe to
1190 ** add an extra one at the end just in case. This is a hack,
1191 ** but so is auto-converting MIME in the first place.
1194 if (!putline("", mci))
1198 sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1205 ** The following is based on Borenstein's "codes.c" module, with simplifying
1206 ** changes as we do not deal with multipart, and to do the translation in-core,
1207 ** with an attempt to prevent overrun of output buffers.
1209 ** What is needed here are changes to defend this code better against
1210 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1213 static char index_hex[128] =
1215 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1216 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1217 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1218 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1219 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1220 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1221 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1222 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1225 # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1228 ** MIME_FROMQP -- decode quoted printable string
1231 ** infile -- input (encoded) string
1232 ** outfile -- output string
1233 ** maxlen -- size of output buffer
1236 ** -2 if decoding failure
1237 ** -1 if infile completely decoded into outfile
1238 ** >= 0 is the position in infile decoding
1239 ** reached before maxlen was reached
1243 mime_fromqp(infile, outfile, maxlen)
1244 unsigned char *infile;
1245 unsigned char **outfile;
1246 int maxlen; /* Max # of chars allowed in outfile */
1252 /* decrement by one for trailing '\0', at least one other char */
1257 while ((c1 = *infile++) != '\0' && nchar < maxlen)
1261 if ((c1 = *infile++) == '\0')
1264 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1266 /* ignore it and the rest of the buffer */
1273 if ((c2 = *infile++) == '\0')
1278 } while ((c2 = HEXCHAR(c2)) == -1);
1283 *(*outfile)++ = c1 << 4 | c2;
1294 *(*outfile)++ = '\0';
1295 if (nchar >= maxlen)
1296 return (infile - b - 1);
1299 #endif /* MIME7TO8 */