2 * Copyright (c) 1998-2002 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.130 2002/05/21 03:39:34 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
37 static int isboundary __P((char *, char **));
38 static int mimeboundary __P((char *, char **));
39 static int mime_getchar __P((SM_FILE_T *, char **, int *));
40 static int mime_getchar_crlf __P((SM_FILE_T *, char **, int *));
42 /* character set for hex and base64 encoding */
43 static char Base16Code[] = "0123456789ABCDEF";
44 static char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
46 /* types of MIME boundaries */
47 # define MBT_SYNTAX 0 /* syntax error */
48 # define MBT_NOTSEP 1 /* not a boundary */
49 # define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */
50 # define MBT_FINAL 3 /* final boundary (trailing -- included) */
52 static char *MimeBoundaryNames[] =
54 "SYNTAX", "NOTSEP", "INTERMED", "FINAL"
57 static bool MapNLtoCRLF;
60 ** MIME8TO7 -- output 8 bit body in 7 bit format
62 ** The header has already been output -- this has to do the
63 ** 8 to 7 bit conversion. It would be easy if we didn't have
64 ** to deal with nested formats (multipart/xxx and message/rfc822).
66 ** We won't be called if we don't have to do a conversion, and
67 ** appropriate MIME-Version: and Content-Type: fields have been
68 ** output. Any Content-Transfer-Encoding: field has not been
69 ** output, and we can add it here.
72 ** mci -- mailer connection information.
73 ** header -- the header for this body part.
75 ** boundaries -- the currently pending message boundaries.
76 ** NULL if we are processing the outer portion.
77 ** flags -- to tweak processing.
80 ** An indicator of what terminated the message part:
81 ** MBT_FINAL -- the final boundary
82 ** MBT_INTERMED -- an intermediate boundary
83 ** MBT_NOTSEP -- an end of file
88 char *a_field; /* name of field */
89 char *a_value; /* value of that field */
93 mime8to7(mci, header, e, boundaries, flags)
104 size_t sectionsize, sectionhighbits;
113 struct args argv[MAXMIMEARGS];
116 char pvpbuf[MAXLINE];
117 extern unsigned char MimeTokenTab[256];
121 sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
122 if (boundaries[0] == NULL)
123 sm_dprintf(" <none>");
126 for (i = 0; boundaries[i] != NULL; i++)
127 sm_dprintf(" %s", boundaries[i]);
132 p = hvalue("Content-Transfer-Encoding", header);
134 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
135 MimeTokenTab)) == NULL ||
142 cataddr(pvp, NULL, buf, sizeof buf, '\0');
143 cte = sm_rpool_strdup_x(e->e_rpool, buf);
146 type = subtype = NULL;
147 p = hvalue("Content-Type", header);
150 if (bitset(M87F_DIGEST, flags))
151 p = "message/rfc822";
156 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
157 MimeTokenTab)) != NULL &&
162 for (i = 0; pvp[i] != NULL; i++)
163 sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
166 if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
172 /* break out parameters */
173 while (*pvp != NULL && argc < MAXMIMEARGS)
175 /* skip to semicolon separator */
176 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
178 if (*pvp++ == NULL || *pvp == NULL)
181 /* complain about empty values */
182 if (strcmp(*pvp, ";") == 0)
184 usrerr("mime8to7: Empty parameter in Content-Type header");
186 /* avoid bounce loops */
187 e->e_flags |= EF_DONT_MIME;
191 /* extract field name */
192 argv[argc].a_field = *pvp++;
194 /* see if there is a value */
195 if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
196 (*++pvp == NULL || strcmp(*pvp, ";") != 0))
198 argv[argc].a_value = *pvp;
204 /* check for disaster cases */
210 /* don't propogate some flags more than one level into the message */
211 flags &= ~M87F_DIGEST;
214 ** Check for cases that can not be encoded.
216 ** For example, you can't encode certain kinds of types
217 ** or already-encoded messages. If we find this case,
218 ** just copy it through.
221 (void) sm_snprintf(buf, sizeof buf, "%.100s/%.100s", type, subtype);
222 if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
223 flags |= M87F_NO8BIT;
226 if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
228 # endif /* USE_B_CLASS */
229 if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
233 ** Multipart requires special processing.
235 ** Do a recursive descent into the message.
238 if (sm_strcasecmp(type, "multipart") == 0 &&
239 (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)))
242 if (sm_strcasecmp(subtype, "digest") == 0)
243 flags |= M87F_DIGEST;
245 for (i = 0; i < argc; i++)
247 if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
250 if (i >= argc || argv[i].a_value == NULL)
252 usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
253 i >= argc ? "missing" : "bogus", p);
256 /* avoid bounce loops */
257 e->e_flags |= EF_DONT_MIME;
264 if (sm_strlcpy(bbuf, p, sizeof bbuf) >= sizeof bbuf)
266 usrerr("mime8to7: multipart boundary \"%s\" too long",
269 /* avoid bounce loops */
270 e->e_flags |= EF_DONT_MIME;
274 sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
276 for (i = 0; i < MAXMIMENESTING; i++)
278 if (boundaries[i] == NULL)
281 if (i >= MAXMIMENESTING)
283 usrerr("mime8to7: multipart nesting boundary too deep");
285 /* avoid bounce loops */
286 e->e_flags |= EF_DONT_MIME;
290 boundaries[i] = bbuf;
291 boundaries[i + 1] = NULL;
293 mci->mci_flags |= MCIF_INMIME;
295 /* skip the early "comment" prologue */
297 mci->mci_flags &= ~MCIF_INHEADER;
299 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
302 bt = mimeboundary(buf, boundaries);
303 if (bt != MBT_NOTSEP)
305 putxline(buf, strlen(buf), mci,
306 PXLF_MAPFROM|PXLF_STRIP8BIT);
308 sm_dprintf(" ...%s", buf);
310 if (sm_io_eof(e->e_dfp))
312 while (bt != MBT_FINAL)
314 auto HDR *hdr = NULL;
316 (void) sm_strlcpyn(buf, sizeof buf, 2, "--", bbuf);
319 sm_dprintf(" ...%s\n", buf);
320 collect(e->e_dfp, false, &hdr, e);
322 putline("+++after collect", mci);
323 putheader(mci, hdr, e, flags);
325 putline("+++after putheader", mci);
326 bt = mime8to7(mci, hdr, e, boundaries, flags);
328 (void) sm_strlcpyn(buf, sizeof buf, 3, "--", bbuf, "--");
331 sm_dprintf(" ...%s\n", buf);
332 boundaries[i] = NULL;
333 mci->mci_flags &= ~MCIF_INMIME;
335 /* skip the late "comment" epilogue */
336 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
339 bt = mimeboundary(buf, boundaries);
340 if (bt != MBT_NOTSEP)
342 putxline(buf, strlen(buf), mci,
343 PXLF_MAPFROM|PXLF_STRIP8BIT);
345 sm_dprintf(" ...%s", buf);
347 if (sm_io_eof(e->e_dfp))
350 sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
351 MimeBoundaryNames[bt]);
356 ** Message/xxx types -- recurse exactly once.
358 ** Class 's' is predefined to have "rfc822" only.
361 if (sm_strcasecmp(type, "message") == 0)
363 if (!wordinclass(subtype, 's'))
365 flags |= M87F_NO8BIT;
369 auto HDR *hdr = NULL;
373 mci->mci_flags |= MCIF_INMIME;
374 collect(e->e_dfp, false, &hdr, e);
376 putline("+++after collect", mci);
377 putheader(mci, hdr, e, flags);
379 putline("+++after putheader", mci);
380 if (hvalue("MIME-Version", hdr) == NULL &&
381 !bitset(M87F_NO8TO7, flags))
382 putline("MIME-Version: 1.0", mci);
383 bt = mime8to7(mci, hdr, e, boundaries, flags);
384 mci->mci_flags &= ~MCIF_INMIME;
390 ** Non-compound body type
392 ** Compute the ratio of seven to eight bit characters;
393 ** use that as a heuristic to decide how to do the
397 sectionsize = sectionhighbits = 0;
398 if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
400 /* remember where we were */
401 offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
403 syserr("mime8to7: cannot sm_io_tell on %cf%s",
404 DATAFL_LETTER, e->e_id);
406 /* do a scan of this body type to count character types */
407 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
410 if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
412 for (p = buf; *p != '\0'; p++)
414 /* count bytes with the high bit set */
416 if (bitset(0200, *p))
421 ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
422 ** assume base64. This heuristic avoids double-reading
423 ** large graphics or video files.
426 if (sectionsize >= 4096 &&
427 sectionhighbits > sectionsize / 4)
431 /* return to the original offset for processing */
432 /* XXX use relative seeks to handle >31 bit file sizes? */
433 if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
434 syserr("mime8to7: cannot sm_io_fseek on %cf%s",
435 DATAFL_LETTER, e->e_id);
437 sm_io_clearerr(e->e_dfp);
441 ** Heuristically determine encoding method.
442 ** If more than 1/8 of the total characters have the
443 ** eighth bit set, use base64; else use quoted-printable.
444 ** However, only encode binary encoded data as base64,
445 ** since otherwise the NL=>CRLF mapping will be a problem.
450 sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
451 (long) sectionhighbits, (long) sectionsize,
452 cte == NULL ? "[none]" : cte,
453 type == NULL ? "[none]" : type,
454 subtype == NULL ? "[none]" : subtype);
456 if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
457 sectionsize = sectionhighbits;
460 if (sectionhighbits == 0)
462 /* no encoding necessary */
464 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
466 !bitset(M87F_NO8TO7, flags))
469 ** Skip _unless_ in MIME mode and potentially
470 ** converting from 8 bit to 7 bit MIME. See
471 ** putheader() for the counterpart where the
472 ** CTE header is skipped in the opposite
476 (void) sm_snprintf(buf, sizeof buf,
477 "Content-Transfer-Encoding: %.200s", cte);
480 sm_dprintf(" ...%s\n", buf);
483 mci->mci_flags &= ~MCIF_INHEADER;
484 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
487 bt = mimeboundary(buf, boundaries);
488 if (bt != MBT_NOTSEP)
492 if (sm_io_eof(e->e_dfp))
495 else if (!MapNLtoCRLF ||
496 (sectionsize / 8 < sectionhighbits && !use_qp))
498 /* use base64 encoding */
502 sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
503 putline("Content-Transfer-Encoding: base64", mci);
504 (void) sm_snprintf(buf, sizeof buf,
505 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
506 MyHostName, e->e_id);
509 mci->mci_flags &= ~MCIF_INHEADER;
510 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
521 *bp++ = Base64Code[(c1 >> 2)];
522 c1 = (c1 & 0x03) << 4;
523 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
526 *bp++ = Base64Code[c1];
531 c1 |= (c2 >> 4) & 0x0f;
532 *bp++ = Base64Code[c1];
533 c1 = (c2 & 0x0f) << 2;
534 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
537 *bp++ = Base64Code[c1];
541 c1 |= (c2 >> 6) & 0x03;
542 *bp++ = Base64Code[c1];
543 *bp++ = Base64Code[c2 & 0x3f];
550 /* use quoted-printable encoding */
555 /* set up map of characters that must be mapped */
557 for (c1 = 0x00; c1 < 0x20; c1++)
558 setbitn(c1, badchars);
559 clrbitn('\t', badchars);
560 for (c1 = 0x7f; c1 < 0x100; c1++)
561 setbitn(c1, badchars);
562 setbitn('=', badchars);
563 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
564 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
565 setbitn(*p, badchars);
568 sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
569 putline("Content-Transfer-Encoding: quoted-printable", mci);
570 (void) sm_snprintf(buf, sizeof buf,
571 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
572 MyHostName, e->e_id);
575 mci->mci_flags &= ~MCIF_INHEADER;
578 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
583 if (c2 == ' ' || c2 == '\t')
586 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
587 *bp++ = Base16Code[c2 & 0x0f];
589 if (buf[0] == '.' && bp == &buf[1])
592 *bp++ = Base16Code[('.' >> 4) & 0x0f];
593 *bp++ = Base16Code['.' & 0x0f];
597 linelen = fromstate = 0;
602 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
603 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
610 else if (c2 == ' ' || c2 == '\t')
616 (linelen > 75 || c1 != '.' ||
617 (linelen > 73 && c2 == '.')))
619 if (linelen > 73 && c2 == '.')
626 linelen = fromstate = 0;
634 if (bitnset(bitidx(c1), badchars))
637 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
638 *bp++ = Base16Code[c1 & 0x0f];
641 else if (c1 != ' ' && c1 != '\t')
643 if (linelen < 4 && c1 == "From"[linelen])
651 /* output any saved character */
652 if (c2 == ' ' || c2 == '\t')
655 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
656 *bp++ = Base16Code[c2 & 0x0f];
660 if (linelen > 0 || boundaries[0] != NULL)
668 sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
672 ** MIME_GETCHAR -- get a character for MIME processing
674 ** Treats boundaries as SM_IO_EOF.
677 ** fp -- the input file.
678 ** boundaries -- the current MIME boundaries.
679 ** btp -- if the return value is SM_IO_EOF, *btp is set to
680 ** the type of the boundary.
683 ** The next character in the input stream.
687 mime_getchar(fp, boundaries, btp)
688 register SM_FILE_T *fp;
693 static unsigned char *bp = NULL;
694 static int buflen = 0;
695 static bool atbol = true; /* at beginning of line */
696 static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
697 static unsigned char buf[128]; /* need not be a full line */
698 int start = 0; /* indicates position of - in buffer */
700 if (buflen == 1 && *bp == '\n')
702 /* last \n in buffer may be part of next MIME boundary */
711 c = sm_io_getc(fp, SM_TIME_DEFAULT);
716 /* might be part of a MIME boundary */
719 c = sm_io_getc(fp, SM_TIME_DEFAULT);
722 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
731 if (atbol && c == '-')
733 /* check for a message boundary */
734 c = sm_io_getc(fp, SM_TIME_DEFAULT);
741 buflen = bp - buf - 1;
746 /* got "--", now check for rest of separator */
748 while (bp < &buf[sizeof buf - 2] &&
749 (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
754 *bp = '\0'; /* XXX simply cut off? */
755 bt = mimeboundary((char *) &buf[start], boundaries);
760 /* we have a message boundary */
771 buflen = bp - buf - 1;
781 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
784 ** fp -- the input file.
785 ** boundaries -- the current MIME boundaries.
786 ** btp -- if the return value is SM_IO_EOF, *btp is set to
787 ** the type of the boundary.
790 ** The next character in the input stream.
794 mime_getchar_crlf(fp, boundaries, btp)
795 register SM_FILE_T *fp;
799 static bool sendlf = false;
807 c = mime_getchar(fp, boundaries, btp);
808 if (c == '\n' && MapNLtoCRLF)
816 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
819 ** line -- the input line.
820 ** boundaries -- the set of currently pending boundaries.
823 ** MBT_NOTSEP -- if this is not a separator line
824 ** MBT_INTERMED -- if this is an intermediate separator
825 ** MBT_FINAL -- if this is a final boundary
826 ** MBT_SYNTAX -- if this is a boundary for the wrong
827 ** enclosure -- i.e., a syntax error.
831 mimeboundary(line, boundaries)
835 int type = MBT_NOTSEP;
839 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
842 if (i > 0 && line[i - 1] == '\n')
845 /* strip off trailing whitespace */
846 while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'))
852 sm_dprintf("mimeboundary: line=\"%s\"... ", line);
854 /* check for this as an intermediate boundary */
855 if (isboundary(&line[2], boundaries) >= 0)
857 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
859 /* check for a final boundary */
861 if (isboundary(&line[2], boundaries) >= 0)
868 sm_dprintf("%s\n", MimeBoundaryNames[type]);
872 ** DEFCHARSET -- return default character set for message
874 ** The first choice for character set is for the mailer
875 ** corresponding to the envelope sender. If neither that
876 ** nor the global configuration file has a default character
877 ** set defined, return "unknown-8bit" as recommended by
878 ** RFC 1428 section 3.
881 ** e -- the envelope for this message.
884 ** The default character set for that mailer.
889 register ENVELOPE *e;
891 if (e != NULL && e->e_from.q_mailer != NULL &&
892 e->e_from.q_mailer->m_defcharset != NULL)
893 return e->e_from.q_mailer->m_defcharset;
894 if (DefaultCharSet != NULL)
895 return DefaultCharSet;
896 return "unknown-8bit";
899 ** ISBOUNDARY -- is a given string a currently valid boundary?
902 ** line -- the current input line.
903 ** boundaries -- the list of valid boundaries.
906 ** The index number in boundaries if the line is found.
912 isboundary(line, boundaries)
918 for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
920 if (strcmp(line, boundaries[i]) == 0)
925 #endif /* MIME8TO7 */
928 static int mime_fromqp __P((unsigned char *, unsigned char **, int));
931 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
933 ** This is a hack. Supports translating the two 7-bit body-encodings
934 ** (quoted-printable and base64) to 8-bit coded bodies.
936 ** There is not much point in supporting multipart here, as the UA
937 ** will be able to deal with encoded MIME bodies if it can parse MIME
938 ** multipart messages.
940 ** Note also that we won't be called unless it is a text/plain MIME
941 ** message, encoded base64 or QP and mailer flag '9' has been defined
944 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
947 ** mci -- mailer connection information.
948 ** header -- the header for this body part.
955 static char index_64[128] =
957 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
958 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
959 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
960 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
961 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
962 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
963 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
964 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
967 # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
970 mime7to8(mci, header, e)
973 register ENVELOPE *e;
979 unsigned char *fbufp;
981 unsigned char fbuf[MAXLINE + 1];
982 char pvpbuf[MAXLINE];
983 extern unsigned char MimeTokenTab[256];
985 p = hvalue("Content-Transfer-Encoding", header);
987 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
988 MimeTokenTab)) == NULL ||
991 /* "can't happen" -- upper level should have caught this */
992 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
994 /* avoid bounce loops */
995 e->e_flags |= EF_DONT_MIME;
997 /* cheap failsafe algorithm -- should work on text/plain */
1000 (void) sm_snprintf(buf, sizeof buf,
1001 "Content-Transfer-Encoding: %s", p);
1005 mci->mci_flags &= ~MCIF_INHEADER;
1006 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof buf)
1011 cataddr(pvp, NULL, buf, sizeof buf, '\0');
1012 cte = sm_rpool_strdup_x(e->e_rpool, buf);
1014 mci->mci_flags |= MCIF_INHEADER;
1015 putline("Content-Transfer-Encoding: 8bit", mci);
1016 (void) sm_snprintf(buf, sizeof buf,
1017 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1018 cte, MyHostName, e->e_id);
1021 mci->mci_flags &= ~MCIF_INHEADER;
1024 ** Translate body encoding to 8-bit. Supports two types of
1025 ** encodings; "base64" and "quoted-printable". Assume qp if
1026 ** it is not base64.
1029 pxflags = PXLF_MAPFROM;
1030 if (sm_strcasecmp(cte, "base64") == 0)
1035 while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1038 if (isascii(c1) && isspace(c1))
1043 c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1044 } while (isascii(c2) && isspace(c2));
1045 if (c2 == SM_IO_EOF)
1050 c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1051 } while (isascii(c3) && isspace(c3));
1052 if (c3 == SM_IO_EOF)
1057 c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1058 } while (isascii(c4) && isspace(c4));
1059 if (c4 == SM_IO_EOF)
1062 if (c1 == '=' || c2 == '=')
1067 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1068 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
1070 if (*--fbufp != '\n' ||
1071 (fbufp > fbuf && *--fbufp != '\r'))
1073 pxflags |= PXLF_NOADDEOL;
1076 putxline((char *) fbuf, fbufp - fbuf,
1078 pxflags &= ~PXLF_NOADDEOL;
1084 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1085 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
1087 if (*--fbufp != '\n' ||
1088 (fbufp > fbuf && *--fbufp != '\r'))
1090 pxflags |= PXLF_NOADDEOL;
1093 putxline((char *) fbuf, fbufp - fbuf,
1095 pxflags &= ~PXLF_NOADDEOL;
1101 *fbufp = ((c3 & 0x03) << 6) | c4;
1102 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
1104 if (*--fbufp != '\n' ||
1105 (fbufp > fbuf && *--fbufp != '\r'))
1107 pxflags |= PXLF_NOADDEOL;
1110 putxline((char *) fbuf, fbufp - fbuf,
1112 pxflags &= ~PXLF_NOADDEOL;
1121 /* quoted-printable */
1122 pxflags |= PXLF_NOADDEOL;
1124 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1125 sizeof buf) != NULL)
1127 off = mime_fromqp((unsigned char *) buf, &fbufp,
1128 &fbuf[MAXLINE] - fbufp);
1133 if (fbufp - fbuf > 0)
1134 putxline((char *) fbuf, fbufp - fbuf - 1, mci,
1137 if (off >= 0 && buf[off] != '\0')
1139 off = mime_fromqp((unsigned char *) (buf + off),
1141 &fbuf[MAXLINE] - fbufp);
1147 /* force out partial last line */
1151 putxline((char *) fbuf, fbufp - fbuf, mci, pxflags);
1155 ** The decoded text may end without an EOL. Since this function
1156 ** is only called for text/plain MIME messages, it is safe to
1157 ** add an extra one at the end just in case. This is a hack,
1158 ** but so is auto-converting MIME in the first place.
1164 sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1167 ** The following is based on Borenstein's "codes.c" module, with simplifying
1168 ** changes as we do not deal with multipart, and to do the translation in-core,
1169 ** with an attempt to prevent overrun of output buffers.
1171 ** What is needed here are changes to defend this code better against
1172 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1175 static char index_hex[128] =
1177 -1,-1,-1,-1, -1,-1,-1,-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,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1180 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1181 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1182 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1183 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1184 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1187 # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1190 ** MIME_FROMQP -- decode quoted printable string
1193 ** infile -- input (encoded) string
1194 ** outfile -- output string
1195 ** maxlen -- size of output buffer
1198 ** -2 if decoding failure
1199 ** -1 if infile completely decoded into outfile
1200 ** >= 0 is the position in infile decoding
1201 ** reached before maxlen was reached
1205 mime_fromqp(infile, outfile, maxlen)
1206 unsigned char *infile;
1207 unsigned char **outfile;
1208 int maxlen; /* Max # of chars allowed in outfile */
1214 /* decrement by one for trailing '\0', at least one other char */
1219 while ((c1 = *infile++) != '\0' && nchar < maxlen)
1223 if ((c1 = *infile++) == '\0')
1226 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1228 /* ignore it and the rest of the buffer */
1235 if ((c2 = *infile++) == '\0')
1240 } while ((c2 = HEXCHAR(c2)) == -1);
1245 *(*outfile)++ = c1 << 4 | c2;
1256 *(*outfile)++ = '\0';
1257 if (nchar >= maxlen)
1258 return (infile - b - 1);
1261 #endif /* MIME7TO8 */