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.137 2004/09/02 21:37:26 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
93 char *a_field; /* name of field */
94 char *a_value; /* value of that field */
98 mime8to7(mci, header, e, boundaries, flags)
101 register ENVELOPE *e;
109 size_t sectionsize, sectionhighbits;
118 struct args argv[MAXMIMEARGS];
121 char pvpbuf[MAXLINE];
122 extern unsigned char MimeTokenTab[256];
126 sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
127 if (boundaries[0] == NULL)
128 sm_dprintf(" <none>");
131 for (i = 0; boundaries[i] != NULL; i++)
132 sm_dprintf(" %s", boundaries[i]);
137 p = hvalue("Content-Transfer-Encoding", header);
139 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
140 MimeTokenTab, false)) == NULL ||
147 cataddr(pvp, NULL, buf, sizeof buf, '\0');
148 cte = sm_rpool_strdup_x(e->e_rpool, buf);
151 type = subtype = NULL;
152 p = hvalue("Content-Type", header);
155 if (bitset(M87F_DIGEST, flags))
156 p = "message/rfc822";
161 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
162 MimeTokenTab, false)) != NULL &&
167 for (i = 0; pvp[i] != NULL; i++)
168 sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
171 if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
177 /* break out parameters */
178 while (*pvp != NULL && argc < MAXMIMEARGS)
180 /* skip to semicolon separator */
181 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
183 if (*pvp++ == NULL || *pvp == NULL)
186 /* complain about empty values */
187 if (strcmp(*pvp, ";") == 0)
189 usrerr("mime8to7: Empty parameter in Content-Type header");
191 /* avoid bounce loops */
192 e->e_flags |= EF_DONT_MIME;
196 /* extract field name */
197 argv[argc].a_field = *pvp++;
199 /* see if there is a value */
200 if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
201 (*++pvp == NULL || strcmp(*pvp, ";") != 0))
203 argv[argc].a_value = *pvp;
209 /* check for disaster cases */
215 /* don't propogate some flags more than one level into the message */
216 flags &= ~M87F_DIGEST;
219 ** Check for cases that can not be encoded.
221 ** For example, you can't encode certain kinds of types
222 ** or already-encoded messages. If we find this case,
223 ** just copy it through.
226 (void) sm_snprintf(buf, sizeof buf, "%.100s/%.100s", type, subtype);
227 if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
228 flags |= M87F_NO8BIT;
231 if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
233 # endif /* USE_B_CLASS */
234 if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
238 ** Multipart requires special processing.
240 ** Do a recursive descent into the message.
243 if (sm_strcasecmp(type, "multipart") == 0 &&
244 (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)))
247 if (sm_strcasecmp(subtype, "digest") == 0)
248 flags |= M87F_DIGEST;
250 for (i = 0; i < argc; i++)
252 if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
255 if (i >= argc || argv[i].a_value == NULL)
257 usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
258 i >= argc ? "missing" : "bogus", p);
261 /* avoid bounce loops */
262 e->e_flags |= EF_DONT_MIME;
269 if (sm_strlcpy(bbuf, p, sizeof bbuf) >= sizeof bbuf)
271 usrerr("mime8to7: multipart boundary \"%s\" too long",
274 /* avoid bounce loops */
275 e->e_flags |= EF_DONT_MIME;
279 sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
281 for (i = 0; i < MAXMIMENESTING; i++)
283 if (boundaries[i] == NULL)
286 if (i >= MAXMIMENESTING)
288 usrerr("mime8to7: multipart nesting boundary too deep");
290 /* avoid bounce loops */
291 e->e_flags |= EF_DONT_MIME;
295 boundaries[i] = bbuf;
296 boundaries[i + 1] = NULL;
298 mci->mci_flags |= MCIF_INMIME;
300 /* skip the early "comment" prologue */
302 mci->mci_flags &= ~MCIF_INHEADER;
304 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
307 bt = mimeboundary(buf, boundaries);
308 if (bt != MBT_NOTSEP)
310 putxline(buf, strlen(buf), mci,
311 PXLF_MAPFROM|PXLF_STRIP8BIT);
313 sm_dprintf(" ...%s", buf);
315 if (sm_io_eof(e->e_dfp))
317 while (bt != MBT_FINAL)
319 auto HDR *hdr = NULL;
321 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", bbuf);
324 sm_dprintf(" ...%s\n", buf);
325 collect(e->e_dfp, false, &hdr, e, false);
327 putline("+++after collect", mci);
328 putheader(mci, hdr, e, flags);
330 putline("+++after putheader", mci);
331 bt = mime8to7(mci, hdr, e, boundaries, flags);
333 (void) sm_strlcpyn(buf, sizeof buf, 3, "--", bbuf, "--");
336 sm_dprintf(" ...%s\n", buf);
337 boundaries[i] = NULL;
338 mci->mci_flags &= ~MCIF_INMIME;
340 /* skip the late "comment" epilogue */
341 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
344 bt = mimeboundary(buf, boundaries);
345 if (bt != MBT_NOTSEP)
347 putxline(buf, strlen(buf), mci,
348 PXLF_MAPFROM|PXLF_STRIP8BIT);
350 sm_dprintf(" ...%s", buf);
352 if (sm_io_eof(e->e_dfp))
355 sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
356 MimeBoundaryNames[bt]);
361 ** Message/xxx types -- recurse exactly once.
363 ** Class 's' is predefined to have "rfc822" only.
366 if (sm_strcasecmp(type, "message") == 0)
368 if (!wordinclass(subtype, 's'))
370 flags |= M87F_NO8BIT;
374 auto HDR *hdr = NULL;
378 mci->mci_flags |= MCIF_INMIME;
379 collect(e->e_dfp, false, &hdr, e, false);
381 putline("+++after collect", mci);
382 putheader(mci, hdr, e, flags);
384 putline("+++after putheader", mci);
385 if (hvalue("MIME-Version", hdr) == NULL &&
386 !bitset(M87F_NO8TO7, flags))
387 putline("MIME-Version: 1.0", mci);
388 bt = mime8to7(mci, hdr, e, boundaries, flags);
389 mci->mci_flags &= ~MCIF_INMIME;
395 ** Non-compound body type
397 ** Compute the ratio of seven to eight bit characters;
398 ** use that as a heuristic to decide how to do the
402 sectionsize = sectionhighbits = 0;
403 if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
405 /* remember where we were */
406 offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
408 syserr("mime8to7: cannot sm_io_tell on %cf%s",
409 DATAFL_LETTER, e->e_id);
411 /* do a scan of this body type to count character types */
412 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
415 if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
417 for (p = buf; *p != '\0'; p++)
419 /* count bytes with the high bit set */
421 if (bitset(0200, *p))
426 ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
427 ** assume base64. This heuristic avoids double-reading
428 ** large graphics or video files.
431 if (sectionsize >= 4096 &&
432 sectionhighbits > sectionsize / 4)
436 /* return to the original offset for processing */
437 /* XXX use relative seeks to handle >31 bit file sizes? */
438 if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
439 syserr("mime8to7: cannot sm_io_fseek on %cf%s",
440 DATAFL_LETTER, e->e_id);
442 sm_io_clearerr(e->e_dfp);
446 ** Heuristically determine encoding method.
447 ** If more than 1/8 of the total characters have the
448 ** eighth bit set, use base64; else use quoted-printable.
449 ** However, only encode binary encoded data as base64,
450 ** since otherwise the NL=>CRLF mapping will be a problem.
455 sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
456 (long) sectionhighbits, (long) sectionsize,
457 cte == NULL ? "[none]" : cte,
458 type == NULL ? "[none]" : type,
459 subtype == NULL ? "[none]" : subtype);
461 if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
462 sectionsize = sectionhighbits;
465 if (sectionhighbits == 0)
467 /* no encoding necessary */
469 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
471 !bitset(M87F_NO8TO7, flags))
474 ** Skip _unless_ in MIME mode and potentially
475 ** converting from 8 bit to 7 bit MIME. See
476 ** putheader() for the counterpart where the
477 ** CTE header is skipped in the opposite
481 (void) sm_snprintf(buf, sizeof buf,
482 "Content-Transfer-Encoding: %.200s", cte);
485 sm_dprintf(" ...%s\n", buf);
488 mci->mci_flags &= ~MCIF_INHEADER;
489 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
492 bt = mimeboundary(buf, boundaries);
493 if (bt != MBT_NOTSEP)
497 if (sm_io_eof(e->e_dfp))
500 else if (!MapNLtoCRLF ||
501 (sectionsize / 8 < sectionhighbits && !use_qp))
503 /* use base64 encoding */
507 sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
508 putline("Content-Transfer-Encoding: base64", mci);
509 (void) sm_snprintf(buf, sizeof buf,
510 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
511 MyHostName, e->e_id);
514 mci->mci_flags &= ~MCIF_INHEADER;
515 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
526 *bp++ = Base64Code[(c1 >> 2)];
527 c1 = (c1 & 0x03) << 4;
528 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
531 *bp++ = Base64Code[c1];
536 c1 |= (c2 >> 4) & 0x0f;
537 *bp++ = Base64Code[c1];
538 c1 = (c2 & 0x0f) << 2;
539 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
542 *bp++ = Base64Code[c1];
546 c1 |= (c2 >> 6) & 0x03;
547 *bp++ = Base64Code[c1];
548 *bp++ = Base64Code[c2 & 0x3f];
555 /* use quoted-printable encoding */
560 /* set up map of characters that must be mapped */
562 for (c1 = 0x00; c1 < 0x20; c1++)
563 setbitn(c1, badchars);
564 clrbitn('\t', badchars);
565 for (c1 = 0x7f; c1 < 0x100; c1++)
566 setbitn(c1, badchars);
567 setbitn('=', badchars);
568 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
569 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
570 setbitn(*p, badchars);
573 sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
574 putline("Content-Transfer-Encoding: quoted-printable", mci);
575 (void) sm_snprintf(buf, sizeof buf,
576 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
577 MyHostName, e->e_id);
580 mci->mci_flags &= ~MCIF_INHEADER;
583 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
588 if (c2 == ' ' || c2 == '\t')
591 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
592 *bp++ = Base16Code[c2 & 0x0f];
594 if (buf[0] == '.' && bp == &buf[1])
597 *bp++ = Base16Code[('.' >> 4) & 0x0f];
598 *bp++ = Base16Code['.' & 0x0f];
602 linelen = fromstate = 0;
607 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
608 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
615 else if (c2 == ' ' || c2 == '\t')
621 (linelen > 75 || c1 != '.' ||
622 (linelen > 73 && c2 == '.')))
624 if (linelen > 73 && c2 == '.')
631 linelen = fromstate = 0;
639 if (bitnset(bitidx(c1), badchars))
642 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
643 *bp++ = Base16Code[c1 & 0x0f];
646 else if (c1 != ' ' && c1 != '\t')
648 if (linelen < 4 && c1 == "From"[linelen])
656 /* output any saved character */
657 if (c2 == ' ' || c2 == '\t')
660 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
661 *bp++ = Base16Code[c2 & 0x0f];
665 if (linelen > 0 || boundaries[0] != NULL)
673 sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
677 ** MIME_GETCHAR -- get a character for MIME processing
679 ** Treats boundaries as SM_IO_EOF.
682 ** fp -- the input file.
683 ** boundaries -- the current MIME boundaries.
684 ** btp -- if the return value is SM_IO_EOF, *btp is set to
685 ** the type of the boundary.
688 ** The next character in the input stream.
692 mime_getchar(fp, boundaries, btp)
693 register SM_FILE_T *fp;
698 static unsigned char *bp = NULL;
699 static int buflen = 0;
700 static bool atbol = true; /* at beginning of line */
701 static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
702 static unsigned char buf[128]; /* need not be a full line */
703 int start = 0; /* indicates position of - in buffer */
705 if (buflen == 1 && *bp == '\n')
707 /* last \n in buffer may be part of next MIME boundary */
716 c = sm_io_getc(fp, SM_TIME_DEFAULT);
721 /* might be part of a MIME boundary */
724 c = sm_io_getc(fp, SM_TIME_DEFAULT);
727 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
736 if (atbol && c == '-')
738 /* check for a message boundary */
739 c = sm_io_getc(fp, SM_TIME_DEFAULT);
746 buflen = bp - buf - 1;
751 /* got "--", now check for rest of separator */
753 while (bp < &buf[sizeof buf - 2] &&
754 (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
759 *bp = '\0'; /* XXX simply cut off? */
760 bt = mimeboundary((char *) &buf[start], boundaries);
765 /* we have a message boundary */
771 if (bp < &buf[sizeof buf - 2] && c != SM_IO_EOF)
776 buflen = bp - buf - 1;
786 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
789 ** fp -- the input file.
790 ** boundaries -- the current MIME boundaries.
791 ** btp -- if the return value is SM_IO_EOF, *btp is set to
792 ** the type of the boundary.
795 ** The next character in the input stream.
799 mime_getchar_crlf(fp, boundaries, btp)
800 register SM_FILE_T *fp;
804 static bool sendlf = false;
812 c = mime_getchar(fp, boundaries, btp);
813 if (c == '\n' && MapNLtoCRLF)
821 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
824 ** line -- the input line.
825 ** boundaries -- the set of currently pending boundaries.
828 ** MBT_NOTSEP -- if this is not a separator line
829 ** MBT_INTERMED -- if this is an intermediate separator
830 ** MBT_FINAL -- if this is a final boundary
831 ** MBT_SYNTAX -- if this is a boundary for the wrong
832 ** enclosure -- i.e., a syntax error.
836 mimeboundary(line, boundaries)
840 int type = MBT_NOTSEP;
844 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
847 if (i > 0 && line[i - 1] == '\n')
850 /* strip off trailing whitespace */
851 while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
853 || line[i - 1] == '\r'
854 #endif /* _FFR_MIME_CR_OK */
861 sm_dprintf("mimeboundary: line=\"%s\"... ", line);
863 /* check for this as an intermediate boundary */
864 if (isboundary(&line[2], boundaries) >= 0)
866 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
868 /* check for a final boundary */
870 if (isboundary(&line[2], boundaries) >= 0)
877 sm_dprintf("%s\n", MimeBoundaryNames[type]);
881 ** DEFCHARSET -- return default character set for message
883 ** The first choice for character set is for the mailer
884 ** corresponding to the envelope sender. If neither that
885 ** nor the global configuration file has a default character
886 ** set defined, return "unknown-8bit" as recommended by
887 ** RFC 1428 section 3.
890 ** e -- the envelope for this message.
893 ** The default character set for that mailer.
898 register ENVELOPE *e;
900 if (e != NULL && e->e_from.q_mailer != NULL &&
901 e->e_from.q_mailer->m_defcharset != NULL)
902 return e->e_from.q_mailer->m_defcharset;
903 if (DefaultCharSet != NULL)
904 return DefaultCharSet;
905 return "unknown-8bit";
908 ** ISBOUNDARY -- is a given string a currently valid boundary?
911 ** line -- the current input line.
912 ** boundaries -- the list of valid boundaries.
915 ** The index number in boundaries if the line is found.
921 isboundary(line, boundaries)
927 for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
929 if (strcmp(line, boundaries[i]) == 0)
934 #endif /* MIME8TO7 */
937 static int mime_fromqp __P((unsigned char *, unsigned char **, int));
940 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
942 ** This is a hack. Supports translating the two 7-bit body-encodings
943 ** (quoted-printable and base64) to 8-bit coded bodies.
945 ** There is not much point in supporting multipart here, as the UA
946 ** will be able to deal with encoded MIME bodies if it can parse MIME
947 ** multipart messages.
949 ** Note also that we won't be called unless it is a text/plain MIME
950 ** message, encoded base64 or QP and mailer flag '9' has been defined
953 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
956 ** mci -- mailer connection information.
957 ** header -- the header for this body part.
964 static char index_64[128] =
966 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
967 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
968 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
969 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
970 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
971 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
972 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
973 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
976 # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
979 mime7to8(mci, header, e)
982 register ENVELOPE *e;
988 unsigned char *fbufp;
990 unsigned char fbuf[MAXLINE + 1];
991 char pvpbuf[MAXLINE];
992 extern unsigned char MimeTokenTab[256];
994 p = hvalue("Content-Transfer-Encoding", header);
996 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
997 MimeTokenTab, false)) == NULL ||
1000 /* "can't happen" -- upper level should have caught this */
1001 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
1003 /* avoid bounce loops */
1004 e->e_flags |= EF_DONT_MIME;
1006 /* cheap failsafe algorithm -- should work on text/plain */
1009 (void) sm_snprintf(buf, sizeof buf,
1010 "Content-Transfer-Encoding: %s", p);
1014 mci->mci_flags &= ~MCIF_INHEADER;
1015 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
1020 cataddr(pvp, NULL, buf, sizeof buf, '\0');
1021 cte = sm_rpool_strdup_x(e->e_rpool, buf);
1023 mci->mci_flags |= MCIF_INHEADER;
1024 putline("Content-Transfer-Encoding: 8bit", mci);
1025 (void) sm_snprintf(buf, sizeof buf,
1026 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1027 cte, MyHostName, e->e_id);
1030 mci->mci_flags &= ~MCIF_INHEADER;
1033 ** Translate body encoding to 8-bit. Supports two types of
1034 ** encodings; "base64" and "quoted-printable". Assume qp if
1035 ** it is not base64.
1038 pxflags = PXLF_MAPFROM;
1039 if (sm_strcasecmp(cte, "base64") == 0)
1044 while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1047 if (isascii(c1) && isspace(c1))
1052 c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1053 } while (isascii(c2) && isspace(c2));
1054 if (c2 == SM_IO_EOF)
1059 c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1060 } while (isascii(c3) && isspace(c3));
1061 if (c3 == SM_IO_EOF)
1066 c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1067 } while (isascii(c4) && isspace(c4));
1068 if (c4 == SM_IO_EOF)
1071 if (c1 == '=' || c2 == '=')
1077 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1079 #else /* MIME7TO8_OLD */
1080 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1083 pxflags |= PXLF_NOADDEOL; \
1085 #endif /* MIME7TO8_OLD */
1090 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
1093 putxline((char *) fbuf, fbufp - fbuf, mci, pxflags); \
1094 pxflags &= ~PXLF_NOADDEOL; \
1099 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1104 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1109 *fbufp = ((c3 & 0x03) << 6) | c4;
1117 /* quoted-printable */
1118 pxflags |= PXLF_NOADDEOL;
1120 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1121 sizeof buf) != NULL)
1123 off = mime_fromqp((unsigned char *) buf, &fbufp,
1124 &fbuf[MAXLINE] - fbufp);
1129 if (fbufp - fbuf > 0)
1130 putxline((char *) fbuf, fbufp - fbuf - 1, mci,
1133 if (off >= 0 && buf[off] != '\0')
1135 off = mime_fromqp((unsigned char *) (buf + off),
1137 &fbuf[MAXLINE] - fbufp);
1143 /* force out partial last line */
1147 putxline((char *) fbuf, fbufp - fbuf, mci, pxflags);
1151 ** The decoded text may end without an EOL. Since this function
1152 ** is only called for text/plain MIME messages, it is safe to
1153 ** add an extra one at the end just in case. This is a hack,
1154 ** but so is auto-converting MIME in the first place.
1160 sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1163 ** The following is based on Borenstein's "codes.c" module, with simplifying
1164 ** changes as we do not deal with multipart, and to do the translation in-core,
1165 ** with an attempt to prevent overrun of output buffers.
1167 ** What is needed here are changes to defend this code better against
1168 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1171 static char index_hex[128] =
1173 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1174 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1175 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1176 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1177 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1178 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1179 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1180 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1183 # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1186 ** MIME_FROMQP -- decode quoted printable string
1189 ** infile -- input (encoded) string
1190 ** outfile -- output string
1191 ** maxlen -- size of output buffer
1194 ** -2 if decoding failure
1195 ** -1 if infile completely decoded into outfile
1196 ** >= 0 is the position in infile decoding
1197 ** reached before maxlen was reached
1201 mime_fromqp(infile, outfile, maxlen)
1202 unsigned char *infile;
1203 unsigned char **outfile;
1204 int maxlen; /* Max # of chars allowed in outfile */
1210 /* decrement by one for trailing '\0', at least one other char */
1215 while ((c1 = *infile++) != '\0' && nchar < maxlen)
1219 if ((c1 = *infile++) == '\0')
1222 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1224 /* ignore it and the rest of the buffer */
1231 if ((c2 = *infile++) == '\0')
1236 } while ((c2 = HEXCHAR(c2)) == -1);
1241 *(*outfile)++ = c1 << 4 | c2;
1252 *(*outfile)++ = '\0';
1253 if (nchar >= maxlen)
1254 return (infile - b - 1);
1257 #endif /* MIME7TO8 */