2 * Copyright (c) 1998-2003, 2006, 2013 Proofpoint, 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.149 2013-11-22 20:51:56 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
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;
113 size_t sectionsize, sectionhighbits;
122 struct args argv[MAXMIMEARGS];
125 char pvpbuf[MAXLINE];
126 extern unsigned char MimeTokenTab[256];
128 if (level > MAXMIMENESTING)
130 if (!bitset(EF_TOODEEP, e->e_flags))
133 sm_dprintf("mime8to7: too deep, level=%d\n",
135 usrerr("mime8to7: recursion level %d exceeded",
137 e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
142 sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
143 if (boundaries[0] == NULL)
144 sm_dprintf(" <none>");
147 for (i = 0; boundaries[i] != NULL; i++)
148 sm_dprintf(" %s", boundaries[i]);
153 p = hvalue("Content-Transfer-Encoding", header);
155 (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
156 MimeTokenTab, false)) == NULL ||
163 cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
164 cte = sm_rpool_strdup_x(e->e_rpool, buf);
167 type = subtype = NULL;
168 p = hvalue("Content-Type", header);
171 if (bitset(M87F_DIGEST, flags))
172 p = "message/rfc822";
177 (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
178 MimeTokenTab, false)) != NULL &&
183 for (i = 0; pvp[i] != NULL; i++)
184 sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
187 if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
193 /* break out parameters */
194 while (*pvp != NULL && argc < MAXMIMEARGS)
196 /* skip to semicolon separator */
197 while (*pvp != NULL && strcmp(*pvp, ";") != 0)
199 if (*pvp++ == NULL || *pvp == NULL)
202 /* complain about empty values */
203 if (strcmp(*pvp, ";") == 0)
205 usrerr("mime8to7: Empty parameter in Content-Type header");
207 /* avoid bounce loops */
208 e->e_flags |= EF_DONT_MIME;
212 /* extract field name */
213 argv[argc].a_field = *pvp++;
215 /* see if there is a value */
216 if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
217 (*++pvp == NULL || strcmp(*pvp, ";") != 0))
219 argv[argc].a_value = *pvp;
225 /* check for disaster cases */
231 /* don't propagate some flags more than one level into the message */
232 flags &= ~M87F_DIGEST;
235 ** Check for cases that can not be encoded.
237 ** For example, you can't encode certain kinds of types
238 ** or already-encoded messages. If we find this case,
239 ** just copy it through.
242 (void) sm_snprintf(buf, sizeof(buf), "%.100s/%.100s", type, subtype);
243 if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
244 flags |= M87F_NO8BIT;
247 if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
250 if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
254 ** Multipart requires special processing.
256 ** Do a recursive descent into the message.
259 if (sm_strcasecmp(type, "multipart") == 0 &&
260 (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)) &&
261 !bitset(EF_TOODEEP, e->e_flags)
265 if (sm_strcasecmp(subtype, "digest") == 0)
266 flags |= M87F_DIGEST;
268 for (i = 0; i < argc; i++)
270 if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
273 if (i >= argc || argv[i].a_value == NULL)
275 usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
276 i >= argc ? "missing" : "bogus", p);
279 /* avoid bounce loops */
280 e->e_flags |= EF_DONT_MIME;
285 unfoldstripquotes(p);
287 if (sm_strlcpy(bbuf, p, sizeof(bbuf)) >= sizeof(bbuf))
289 usrerr("mime8to7: multipart boundary \"%s\" too long",
292 /* avoid bounce loops */
293 e->e_flags |= EF_DONT_MIME;
297 sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
299 for (i = 0; i < MAXMIMENESTING; i++)
301 if (boundaries[i] == NULL)
304 if (i >= MAXMIMENESTING)
307 sm_dprintf("mime8to7: too deep, i=%d\n", i);
308 if (!bitset(EF_TOODEEP, e->e_flags))
309 usrerr("mime8to7: multipart nesting boundary too deep");
311 /* avoid bounce loops */
312 e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
316 boundaries[i] = bbuf;
317 boundaries[i + 1] = NULL;
319 mci->mci_flags |= MCIF_INMIME;
321 /* skip the early "comment" prologue */
322 if (!putline("", mci))
324 mci->mci_flags &= ~MCIF_INHEADER;
326 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
329 bt = mimeboundary(buf, boundaries);
330 if (bt != MBT_NOTSEP)
332 if (!putxline(buf, blen, mci,
333 PXLF_MAPFROM|PXLF_STRIP8BIT))
336 sm_dprintf(" ...%s", buf);
338 if (sm_io_eof(e->e_dfp))
340 while (bt != MBT_FINAL)
342 auto HDR *hdr = NULL;
344 (void) sm_strlcpyn(buf, sizeof(buf), 2, "--", bbuf);
345 if (!putline(buf, mci))
348 sm_dprintf(" ...%s\n", buf);
349 collect(e->e_dfp, false, &hdr, e, false);
351 putline("+++after collect", mci);
352 if (!putheader(mci, hdr, e, flags))
355 putline("+++after putheader", mci);
356 bt = mime8to7(mci, hdr, e, boundaries, flags,
361 (void) sm_strlcpyn(buf, sizeof(buf), 3, "--", bbuf, "--");
362 if (!putline(buf, mci))
365 sm_dprintf(" ...%s\n", buf);
366 boundaries[i] = NULL;
367 mci->mci_flags &= ~MCIF_INMIME;
369 /* skip the late "comment" epilogue */
370 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
373 bt = mimeboundary(buf, boundaries);
374 if (bt != MBT_NOTSEP)
376 if (!putxline(buf, blen, mci,
377 PXLF_MAPFROM|PXLF_STRIP8BIT))
380 sm_dprintf(" ...%s", buf);
382 if (sm_io_eof(e->e_dfp))
385 sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
386 MimeBoundaryNames[bt]);
391 ** Message/xxx types -- recurse exactly once.
393 ** Class 's' is predefined to have "rfc822" only.
396 if (sm_strcasecmp(type, "message") == 0)
398 if (!wordinclass(subtype, 's') ||
399 bitset(EF_TOODEEP, e->e_flags))
401 flags |= M87F_NO8BIT;
405 auto HDR *hdr = NULL;
407 if (!putline("", mci))
410 mci->mci_flags |= MCIF_INMIME;
411 collect(e->e_dfp, false, &hdr, e, false);
413 putline("+++after collect", mci);
414 if (!putheader(mci, hdr, e, flags))
417 putline("+++after putheader", mci);
418 if (hvalue("MIME-Version", hdr) == NULL &&
419 !bitset(M87F_NO8TO7, flags) &&
420 !putline("MIME-Version: 1.0", mci))
422 bt = mime8to7(mci, hdr, e, boundaries, flags,
424 mci->mci_flags &= ~MCIF_INMIME;
430 ** Non-compound body type
432 ** Compute the ratio of seven to eight bit characters;
433 ** use that as a heuristic to decide how to do the
437 sectionsize = sectionhighbits = 0;
438 if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
440 /* remember where we were */
441 offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
443 syserr("mime8to7: cannot sm_io_tell on %cf%s",
444 DATAFL_LETTER, e->e_id);
446 /* do a scan of this body type to count character types */
447 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
450 if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
452 for (i = 0; i < blen; i++)
454 /* count bytes with the high bit set */
456 if (bitset(0200, buf[i]))
461 ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
462 ** assume base64. This heuristic avoids double-reading
463 ** large graphics or video files.
466 if (sectionsize >= 4096 &&
467 sectionhighbits > sectionsize / 4)
471 /* return to the original offset for processing */
472 /* XXX use relative seeks to handle >31 bit file sizes? */
473 if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
474 syserr("mime8to7: cannot sm_io_fseek on %cf%s",
475 DATAFL_LETTER, e->e_id);
477 sm_io_clearerr(e->e_dfp);
481 ** Heuristically determine encoding method.
482 ** If more than 1/8 of the total characters have the
483 ** eighth bit set, use base64; else use quoted-printable.
484 ** However, only encode binary encoded data as base64,
485 ** since otherwise the NL=>CRLF mapping will be a problem.
490 sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
491 (long) sectionhighbits, (long) sectionsize,
492 cte == NULL ? "[none]" : cte,
493 type == NULL ? "[none]" : type,
494 subtype == NULL ? "[none]" : subtype);
496 if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
497 sectionsize = sectionhighbits;
500 if (sectionhighbits == 0)
502 /* no encoding necessary */
504 bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
506 !bitset(M87F_NO8TO7, flags))
509 ** Skip _unless_ in MIME mode and potentially
510 ** converting from 8 bit to 7 bit MIME. See
511 ** putheader() for the counterpart where the
512 ** CTE header is skipped in the opposite
516 (void) sm_snprintf(buf, sizeof(buf),
517 "Content-Transfer-Encoding: %.200s", cte);
518 if (!putline(buf, mci))
521 sm_dprintf(" ...%s\n", buf);
523 if (!putline("", mci))
525 mci->mci_flags &= ~MCIF_INHEADER;
526 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
529 if (!bitset(MCIF_INLONGLINE, mci->mci_flags))
531 bt = mimeboundary(buf, boundaries);
532 if (bt != MBT_NOTSEP)
535 if (!putxline(buf, blen, mci,
536 PXLF_MAPFROM|PXLF_NOADDEOL))
539 if (sm_io_eof(e->e_dfp))
542 else if (!MapNLtoCRLF ||
543 (sectionsize / 8 < sectionhighbits && !use_qp))
545 /* use base64 encoding */
549 sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
550 if (!putline("Content-Transfer-Encoding: base64", mci))
552 (void) sm_snprintf(buf, sizeof(buf),
553 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
554 MyHostName, e->e_id);
555 if (!putline(buf, mci) || !putline("", mci))
557 mci->mci_flags &= ~MCIF_INHEADER;
558 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
564 if (!putline(buf, mci))
570 *bp++ = Base64Code[(c1 >> 2)];
571 c1 = (c1 & 0x03) << 4;
572 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
575 *bp++ = Base64Code[c1];
580 c1 |= (c2 >> 4) & 0x0f;
581 *bp++ = Base64Code[c1];
582 c1 = (c2 & 0x0f) << 2;
583 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
586 *bp++ = Base64Code[c1];
590 c1 |= (c2 >> 6) & 0x03;
591 *bp++ = Base64Code[c1];
592 *bp++ = Base64Code[c2 & 0x3f];
595 if (!putline(buf, mci))
600 /* use quoted-printable encoding */
605 /* set up map of characters that must be mapped */
607 for (c1 = 0x00; c1 < 0x20; c1++)
608 setbitn(c1, badchars);
609 clrbitn('\t', badchars);
610 for (c1 = 0x7f; c1 < 0x100; c1++)
611 setbitn(c1, badchars);
612 setbitn('=', badchars);
613 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
614 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
615 setbitn(*p, badchars);
618 sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
619 if (!putline("Content-Transfer-Encoding: quoted-printable",
622 (void) sm_snprintf(buf, sizeof(buf),
623 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
624 MyHostName, e->e_id);
625 if (!putline(buf, mci) || !putline("", mci))
627 mci->mci_flags &= ~MCIF_INHEADER;
630 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
635 if (c2 == ' ' || c2 == '\t')
638 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
639 *bp++ = Base16Code[c2 & 0x0f];
641 if (buf[0] == '.' && bp == &buf[1])
644 *bp++ = Base16Code[('.' >> 4) & 0x0f];
645 *bp++ = Base16Code['.' & 0x0f];
648 if (!putline(buf, mci))
650 linelen = fromstate = 0;
655 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
656 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
663 else if (c2 == ' ' || c2 == '\t')
669 (linelen > 75 || c1 != '.' ||
670 (linelen > 73 && c2 == '.')))
672 if (linelen > 73 && c2 == '.')
678 if (!putline(buf, mci))
680 linelen = fromstate = 0;
688 if (bitnset(bitidx(c1), badchars))
691 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
692 *bp++ = Base16Code[c1 & 0x0f];
695 else if (c1 != ' ' && c1 != '\t')
697 if (linelen < 4 && c1 == "From"[linelen])
705 /* output any saved character */
706 if (c2 == ' ' || c2 == '\t')
709 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
710 *bp++ = Base16Code[c2 & 0x0f];
714 if (linelen > 0 || boundaries[0] != NULL)
717 if (!putline(buf, mci))
723 sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
730 ** MIME_GETCHAR -- get a character for MIME processing
732 ** Treats boundaries as SM_IO_EOF.
735 ** fp -- the input file.
736 ** boundaries -- the current MIME boundaries.
737 ** btp -- if the return value is SM_IO_EOF, *btp is set to
738 ** the type of the boundary.
741 ** The next character in the input stream.
745 mime_getchar(fp, boundaries, btp)
746 register SM_FILE_T *fp;
751 static unsigned char *bp = NULL;
752 static int buflen = 0;
753 static bool atbol = true; /* at beginning of line */
754 static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
755 static unsigned char buf[128]; /* need not be a full line */
756 int start = 0; /* indicates position of - in buffer */
758 if (buflen == 1 && *bp == '\n')
760 /* last \n in buffer may be part of next MIME boundary */
769 c = sm_io_getc(fp, SM_TIME_DEFAULT);
774 /* might be part of a MIME boundary */
777 c = sm_io_getc(fp, SM_TIME_DEFAULT);
780 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
789 if (atbol && c == '-')
791 /* check for a message boundary */
792 c = sm_io_getc(fp, SM_TIME_DEFAULT);
799 buflen = bp - buf - 1;
804 /* got "--", now check for rest of separator */
806 while (bp < &buf[sizeof(buf) - 2] &&
807 (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
812 *bp = '\0'; /* XXX simply cut off? */
813 bt = mimeboundary((char *) &buf[start], boundaries);
818 /* we have a message boundary */
824 if (bp < &buf[sizeof(buf) - 2] && c != SM_IO_EOF)
829 buflen = bp - buf - 1;
839 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
842 ** fp -- the input file.
843 ** boundaries -- the current MIME boundaries.
844 ** btp -- if the return value is SM_IO_EOF, *btp is set to
845 ** the type of the boundary.
848 ** The next character in the input stream.
852 mime_getchar_crlf(fp, boundaries, btp)
853 register SM_FILE_T *fp;
857 static bool sendlf = false;
865 c = mime_getchar(fp, boundaries, btp);
866 if (c == '\n' && MapNLtoCRLF)
874 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
877 ** line -- the input line.
878 ** boundaries -- the set of currently pending boundaries.
881 ** MBT_NOTSEP -- if this is not a separator line
882 ** MBT_INTERMED -- if this is an intermediate separator
883 ** MBT_FINAL -- if this is a final boundary
884 ** MBT_SYNTAX -- if this is a boundary for the wrong
885 ** enclosure -- i.e., a syntax error.
889 mimeboundary(line, boundaries)
893 int type = MBT_NOTSEP;
897 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
900 if (i > 0 && line[i - 1] == '\n')
903 /* strip off trailing whitespace */
904 while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
906 || line[i - 1] == '\r'
914 sm_dprintf("mimeboundary: line=\"%s\"... ", line);
916 /* check for this as an intermediate boundary */
917 if (isboundary(&line[2], boundaries) >= 0)
919 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
921 /* check for a final boundary */
923 if (isboundary(&line[2], boundaries) >= 0)
930 sm_dprintf("%s\n", MimeBoundaryNames[type]);
934 ** DEFCHARSET -- return default character set for message
936 ** The first choice for character set is for the mailer
937 ** corresponding to the envelope sender. If neither that
938 ** nor the global configuration file has a default character
939 ** set defined, return "unknown-8bit" as recommended by
940 ** RFC 1428 section 3.
943 ** e -- the envelope for this message.
946 ** The default character set for that mailer.
951 register ENVELOPE *e;
953 if (e != NULL && e->e_from.q_mailer != NULL &&
954 e->e_from.q_mailer->m_defcharset != NULL)
955 return e->e_from.q_mailer->m_defcharset;
956 if (DefaultCharSet != NULL)
957 return DefaultCharSet;
958 return "unknown-8bit";
961 ** ISBOUNDARY -- is a given string a currently valid boundary?
964 ** line -- the current input line.
965 ** boundaries -- the list of valid boundaries.
968 ** The index number in boundaries if the line is found.
974 isboundary(line, boundaries)
980 for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
982 if (strcmp(line, boundaries[i]) == 0)
987 #endif /* MIME8TO7 */
990 static int mime_fromqp __P((unsigned char *, unsigned char **, int));
993 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
995 ** This is a hack. Supports translating the two 7-bit body-encodings
996 ** (quoted-printable and base64) to 8-bit coded bodies.
998 ** There is not much point in supporting multipart here, as the UA
999 ** will be able to deal with encoded MIME bodies if it can parse MIME
1000 ** multipart messages.
1002 ** Note also that we won't be called unless it is a text/plain MIME
1003 ** message, encoded base64 or QP and mailer flag '9' has been defined
1006 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
1009 ** mci -- mailer connection information.
1010 ** header -- the header for this body part.
1014 ** true iff body was written successfully
1017 static char index_64[128] =
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,-1, -1,-1,-1,-1,
1021 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
1022 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
1023 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
1024 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
1025 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
1026 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
1029 # define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
1032 mime7to8(mci, header, e)
1035 register ENVELOPE *e;
1041 unsigned char *fbufp;
1043 unsigned char fbuf[MAXLINE + 1];
1044 char pvpbuf[MAXLINE];
1045 extern unsigned char MimeTokenTab[256];
1047 p = hvalue("Content-Transfer-Encoding", header);
1049 (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1050 MimeTokenTab, false)) == NULL ||
1053 /* "can't happen" -- upper level should have caught this */
1054 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
1056 /* avoid bounce loops */
1057 e->e_flags |= EF_DONT_MIME;
1059 /* cheap failsafe algorithm -- should work on text/plain */
1062 (void) sm_snprintf(buf, sizeof(buf),
1063 "Content-Transfer-Encoding: %s", p);
1064 if (!putline(buf, mci))
1067 if (!putline("", mci))
1069 mci->mci_flags &= ~MCIF_INHEADER;
1070 while ((blen = sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1073 if (!putxline(buf, blen, mci, PXLF_MAPFROM))
1078 cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
1079 cte = sm_rpool_strdup_x(e->e_rpool, buf);
1081 mci->mci_flags |= MCIF_INHEADER;
1082 if (!putline("Content-Transfer-Encoding: 8bit", mci))
1084 (void) sm_snprintf(buf, sizeof(buf),
1085 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1086 cte, MyHostName, e->e_id);
1087 if (!putline(buf, mci) || !putline("", mci))
1089 mci->mci_flags &= ~MCIF_INHEADER;
1092 ** Translate body encoding to 8-bit. Supports two types of
1093 ** encodings; "base64" and "quoted-printable". Assume qp if
1094 ** it is not base64.
1097 pxflags = PXLF_MAPFROM;
1098 if (sm_strcasecmp(cte, "base64") == 0)
1103 while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1111 c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1112 } while (SM_ISSPACE(c2));
1113 if (c2 == SM_IO_EOF)
1118 c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1119 } while (SM_ISSPACE(c3));
1120 if (c3 == SM_IO_EOF)
1125 c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1126 } while (SM_ISSPACE(c4));
1127 if (c4 == SM_IO_EOF)
1130 if (c1 == '=' || c2 == '=')
1136 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1138 #else /* MIME7TO8_OLD */
1139 #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1142 pxflags |= PXLF_NOADDEOL; \
1144 #endif /* MIME7TO8_OLD */
1149 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
1152 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1154 pxflags &= ~PXLF_NOADDEOL; \
1159 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1164 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1169 *fbufp = ((c3 & 0x03) << 6) | c4;
1177 /* quoted-printable */
1178 pxflags |= PXLF_NOADDEOL;
1180 while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1183 off = mime_fromqp((unsigned char *) buf, &fbufp,
1184 &fbuf[MAXLINE] - fbufp);
1189 if (fbufp - fbuf > 0)
1191 if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1196 if (off >= 0 && buf[off] != '\0')
1198 off = mime_fromqp((unsigned char *) (buf + off),
1200 &fbuf[MAXLINE] - fbufp);
1206 /* force out partial last line */
1210 if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1215 ** The decoded text may end without an EOL. Since this function
1216 ** is only called for text/plain MIME messages, it is safe to
1217 ** add an extra one at the end just in case. This is a hack,
1218 ** but so is auto-converting MIME in the first place.
1221 if (!putline("", mci))
1225 sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1232 ** The following is based on Borenstein's "codes.c" module, with simplifying
1233 ** changes as we do not deal with multipart, and to do the translation in-core,
1234 ** with an attempt to prevent overrun of output buffers.
1236 ** What is needed here are changes to defend this code better against
1237 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1240 static char index_hex[128] =
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 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1245 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1246 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1247 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1248 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1249 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1252 # define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1255 ** MIME_FROMQP -- decode quoted printable string
1258 ** infile -- input (encoded) string
1259 ** outfile -- output string
1260 ** maxlen -- size of output buffer
1263 ** -2 if decoding failure
1264 ** -1 if infile completely decoded into outfile
1265 ** >= 0 is the position in infile decoding
1266 ** reached before maxlen was reached
1270 mime_fromqp(infile, outfile, maxlen)
1271 unsigned char *infile;
1272 unsigned char **outfile;
1273 int maxlen; /* Max # of chars allowed in outfile */
1279 /* decrement by one for trailing '\0', at least one other char */
1284 while ((c1 = *infile++) != '\0' && nchar < maxlen)
1288 if ((c1 = *infile++) == '\0')
1291 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1293 /* ignore it and the rest of the buffer */
1300 if ((c2 = *infile++) == '\0')
1305 } while ((c2 = HEXCHAR(c2)) == -1);
1310 *(*outfile)++ = c1 << 4 | c2;
1321 *(*outfile)++ = '\0';
1322 if (nchar >= maxlen)
1323 return (infile - b - 1);
1326 #endif /* MIME7TO8 */