2 * Copyright (c) 1998 Sendmail, Inc. All rights reserved.
3 * Copyright (c) 1994, 1996-1997 Eric P. Allman. All rights reserved.
5 * The Regents of the University of California. All rights reserved.
7 * By using this file, you agree to the terms and conditions set
8 * forth in the LICENSE file which can be found at the top level of
9 * the sendmail distribution.
13 # include "sendmail.h"
17 static char sccsid[] = "@(#)mime.c 8.71 (Berkeley) 1/18/1999";
23 ** I am indebted to John Beck of Hewlett-Packard, who contributed
24 ** his code to me for inclusion. As it turns out, I did not use
25 ** his code since he used a "minimum change" approach that used
26 ** several temp files, and I wanted a "minimum impact" approach
27 ** that would avoid copying. However, looking over his code
28 ** helped me cement my understanding of the problem.
30 ** I also looked at, but did not directly use, Nathaniel
31 ** Borenstein's "code.c" module. Again, it functioned as
32 ** a file-to-file translator, which did not fit within my
33 ** design bounds, but it was a useful base for understanding
39 /* character set for hex and base64 encoding */
40 char Base16Code[] = "0123456789ABCDEF";
41 char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43 /* types of MIME boundaries */
44 #define MBT_SYNTAX 0 /* syntax error */
45 #define MBT_NOTSEP 1 /* not a boundary */
46 #define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */
47 #define MBT_FINAL 3 /* final boundary (trailing -- included) */
49 static char *MimeBoundaryNames[] =
51 "SYNTAX", "NOTSEP", "INTERMED", "FINAL"
56 extern int mimeboundary __P((char *, char **));
58 ** MIME8TO7 -- output 8 bit body in 7 bit format
60 ** The header has already been output -- this has to do the
61 ** 8 to 7 bit conversion. It would be easy if we didn't have
62 ** to deal with nested formats (multipart/xxx and message/rfc822).
64 ** We won't be called if we don't have to do a conversion, and
65 ** appropriate MIME-Version: and Content-Type: fields have been
66 ** output. Any Content-Transfer-Encoding: field has not been
67 ** output, and we can add it here.
70 ** mci -- mailer connection information.
71 ** header -- the header for this body part.
73 ** boundaries -- the currently pending message boundaries.
74 ** NULL if we are processing the outer portion.
75 ** flags -- to tweak processing.
78 ** An indicator of what terminated the message part:
79 ** MBT_FINAL -- the final boundary
80 ** MBT_INTERMED -- an intermediate boundary
81 ** MBT_NOTSEP -- an end of file
86 char *field; /* name of field */
87 char *value; /* value of that field */
91 mime8to7(mci, header, e, boundaries, flags)
102 size_t sectionsize, sectionhighbits;
111 struct args argv[MAXMIMEARGS];
114 char pvpbuf[MAXLINE];
115 extern u_char MimeTokenTab[256];
116 extern int mime_getchar __P((FILE *, char **, int *));
117 extern int mime_getchar_crlf __P((FILE *, char **, int *));
121 printf("mime8to7: flags = %x, boundaries =", flags);
122 if (boundaries[0] == NULL)
126 for (i = 0; boundaries[i] != NULL; i++)
127 printf(" %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');
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 printf("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 /* extract field name */
182 argv[argc].field = *pvp++;
184 /* see if there is a value */
185 if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
186 (*++pvp == NULL || strcmp(*pvp, ";") != 0))
188 argv[argc].value = *pvp;
194 /* check for disaster cases */
200 /* don't propogate some flags more than one level into the message */
201 flags &= ~M87F_DIGEST;
204 ** Check for cases that can not be encoded.
206 ** For example, you can't encode certain kinds of types
207 ** or already-encoded messages. If we find this case,
208 ** just copy it through.
211 snprintf(buf, sizeof buf, "%.100s/%.100s", type, subtype);
212 if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
213 flags |= M87F_NO8BIT;
216 if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
219 if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
223 ** Multipart requires special processing.
225 ** Do a recursive descent into the message.
228 if (strcasecmp(type, "multipart") == 0 &&
229 (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)))
233 if (strcasecmp(subtype, "digest") == 0)
234 flags |= M87F_DIGEST;
236 for (i = 0; i < argc; i++)
238 if (strcasecmp(argv[i].field, "boundary") == 0)
241 if (i >= argc || argv[i].value == NULL)
243 syserr("mime8to7: Content-Type: \"%s\": %s boundary",
244 i >= argc ? "missing" : "bogus", p);
247 /* avoid bounce loops */
248 e->e_flags |= EF_DONT_MIME;
256 if (blen > sizeof bbuf - 1)
258 syserr("mime8to7: multipart boundary \"%s\" too long",
260 blen = sizeof bbuf - 1;
262 /* avoid bounce loops */
263 e->e_flags |= EF_DONT_MIME;
265 strncpy(bbuf, p, blen);
268 printf("mime8to7: multipart boundary \"%s\"\n", bbuf);
269 for (i = 0; i < MAXMIMENESTING; i++)
270 if (boundaries[i] == NULL)
272 if (i >= MAXMIMENESTING)
274 syserr("mime8to7: multipart nesting boundary too deep");
276 /* avoid bounce loops */
277 e->e_flags |= EF_DONT_MIME;
281 boundaries[i] = bbuf;
282 boundaries[i + 1] = NULL;
284 mci->mci_flags |= MCIF_INMIME;
286 /* skip the early "comment" prologue */
288 mci->mci_flags &= ~MCIF_INHEADER;
289 while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
291 bt = mimeboundary(buf, boundaries);
292 if (bt != MBT_NOTSEP)
294 putxline(buf, strlen(buf), mci, PXLF_MAPFROM|PXLF_STRIP8BIT);
296 printf(" ...%s", buf);
300 while (bt != MBT_FINAL)
302 auto HDR *hdr = NULL;
304 snprintf(buf, sizeof buf, "--%s", bbuf);
307 printf(" ...%s\n", buf);
308 collect(e->e_dfp, FALSE, &hdr, e);
310 putline("+++after collect", mci);
311 putheader(mci, hdr, e, flags);
313 putline("+++after putheader", mci);
314 bt = mime8to7(mci, hdr, e, boundaries, flags);
316 snprintf(buf, sizeof buf, "--%s--", bbuf);
319 printf(" ...%s\n", buf);
320 boundaries[i] = NULL;
321 mci->mci_flags &= ~MCIF_INMIME;
323 /* skip the late "comment" epilogue */
324 while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
326 bt = mimeboundary(buf, boundaries);
327 if (bt != MBT_NOTSEP)
329 putxline(buf, strlen(buf), mci, PXLF_MAPFROM|PXLF_STRIP8BIT);
331 printf(" ...%s", buf);
336 printf("\t\t\tmime8to7=>%s (multipart)\n",
337 MimeBoundaryNames[bt]);
342 ** Message/xxx types -- recurse exactly once.
344 ** Class 's' is predefined to have "rfc822" only.
347 if (strcasecmp(type, "message") == 0)
349 if (!wordinclass(subtype, 's'))
351 flags |= M87F_NO8BIT;
355 auto HDR *hdr = NULL;
359 mci->mci_flags |= MCIF_INMIME;
360 collect(e->e_dfp, FALSE, &hdr, e);
362 putline("+++after collect", mci);
363 putheader(mci, hdr, e, flags);
365 putline("+++after putheader", mci);
366 if (hvalue("MIME-Version", hdr) == NULL)
367 putline("MIME-Version: 1.0", mci);
368 bt = mime8to7(mci, hdr, e, boundaries, flags);
369 mci->mci_flags &= ~MCIF_INMIME;
375 ** Non-compound body type
377 ** Compute the ratio of seven to eight bit characters;
378 ** use that as a heuristic to decide how to do the
382 sectionsize = sectionhighbits = 0;
383 if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
385 /* remember where we were */
386 offset = ftell(e->e_dfp);
388 syserr("mime8to7: cannot ftell on df%s", e->e_id);
390 /* do a scan of this body type to count character types */
391 while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
393 if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
395 for (p = buf; *p != '\0'; p++)
397 /* count bytes with the high bit set */
399 if (bitset(0200, *p))
404 ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
405 ** assume base64. This heuristic avoids double-reading
406 ** large graphics or video files.
409 if (sectionsize >= 4096 &&
410 sectionhighbits > sectionsize / 4)
414 /* return to the original offset for processing */
415 /* XXX use relative seeks to handle >31 bit file sizes? */
416 if (fseek(e->e_dfp, offset, SEEK_SET) < 0)
417 syserr("mime8to7: cannot fseek on df%s", e->e_id);
423 ** Heuristically determine encoding method.
424 ** If more than 1/8 of the total characters have the
425 ** eighth bit set, use base64; else use quoted-printable.
426 ** However, only encode binary encoded data as base64,
427 ** since otherwise the NL=>CRLF mapping will be a problem.
432 printf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
433 (long) sectionhighbits, (long) sectionsize,
434 cte == NULL ? "[none]" : cte,
435 type == NULL ? "[none]" : type,
436 subtype == NULL ? "[none]" : subtype);
438 if (cte != NULL && strcasecmp(cte, "binary") == 0)
439 sectionsize = sectionhighbits;
442 if (sectionhighbits == 0)
444 /* no encoding necessary */
446 bitset(MCIF_INMIME, mci->mci_flags) &&
447 !bitset(M87F_NO8TO7, flags))
450 ** Skip _unless_ in MIME mode and potentially
451 ** converting from 8 bit to 7 bit MIME. See
452 ** putheader() for the counterpart where the
453 ** CTE header is skipped in the opposite
457 snprintf(buf, sizeof buf,
458 "Content-Transfer-Encoding: %.200s", cte);
461 printf(" ...%s\n", buf);
464 mci->mci_flags &= ~MCIF_INHEADER;
465 while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
467 bt = mimeboundary(buf, boundaries);
468 if (bt != MBT_NOTSEP)
475 else if (!MapNLtoCRLF ||
476 (sectionsize / 8 < sectionhighbits && !use_qp))
478 /* use base64 encoding */
482 printf(" ...Content-Transfer-Encoding: base64\n");
483 putline("Content-Transfer-Encoding: base64", mci);
484 snprintf(buf, sizeof buf,
485 "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
486 MyHostName, e->e_id);
489 mci->mci_flags &= ~MCIF_INHEADER;
490 while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) != EOF)
500 *bp++ = Base64Code[(c1 >> 2)];
501 c1 = (c1 & 0x03) << 4;
502 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
505 *bp++ = Base64Code[c1];
510 c1 |= (c2 >> 4) & 0x0f;
511 *bp++ = Base64Code[c1];
512 c1 = (c2 & 0x0f) << 2;
513 c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
516 *bp++ = Base64Code[c1];
520 c1 |= (c2 >> 6) & 0x03;
521 *bp++ = Base64Code[c1];
522 *bp++ = Base64Code[c2 & 0x3f];
529 /* use quoted-printable encoding */
534 /* set up map of characters that must be mapped */
536 for (c1 = 0x00; c1 < 0x20; c1++)
537 setbitn(c1, badchars);
538 clrbitn('\t', badchars);
539 for (c1 = 0x7f; c1 < 0x100; c1++)
540 setbitn(c1, badchars);
541 setbitn('=', badchars);
542 if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
543 for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
544 setbitn(*p, badchars);
547 printf(" ...Content-Transfer-Encoding: quoted-printable\n");
548 putline("Content-Transfer-Encoding: quoted-printable", mci);
549 snprintf(buf, sizeof buf,
550 "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
551 MyHostName, e->e_id);
554 mci->mci_flags &= ~MCIF_INHEADER;
557 while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) != EOF)
561 if (c2 == ' ' || c2 == '\t')
564 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
565 *bp++ = Base16Code[c2 & 0x0f];
567 if (buf[0] == '.' && bp == &buf[1])
570 *bp++ = Base16Code[('.' >> 4) & 0x0f];
571 *bp++ = Base16Code['.' & 0x0f];
575 linelen = fromstate = 0;
580 if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
581 bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
588 else if (c2 == ' ' || c2 == '\t')
594 (linelen > 75 || c1 != '.' ||
595 (linelen > 73 && c2 == '.')))
597 if (linelen > 73 && c2 == '.')
604 linelen = fromstate = 0;
612 if (bitnset(c1 & 0xff, badchars))
615 *bp++ = Base16Code[(c1 >> 4) & 0x0f];
616 *bp++ = Base16Code[c1 & 0x0f];
619 else if (c1 != ' ' && c1 != '\t')
621 if (linelen < 4 && c1 == "From"[linelen])
629 /* output any saved character */
630 if (c2 == ' ' || c2 == '\t')
633 *bp++ = Base16Code[(c2 >> 4) & 0x0f];
634 *bp++ = Base16Code[c2 & 0x0f];
638 if (linelen > 0 || boundaries[0] != NULL)
646 printf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
650 ** MIME_GETCHAR -- get a character for MIME processing
652 ** Treats boundaries as EOF.
655 ** fp -- the input file.
656 ** boundaries -- the current MIME boundaries.
657 ** btp -- if the return value is EOF, *btp is set to
658 ** the type of the boundary.
661 ** The next character in the input stream.
665 mime_getchar(fp, boundaries, btp)
671 static u_char *bp = NULL;
672 static int buflen = 0;
673 static bool atbol = TRUE; /* at beginning of line */
674 static int bt = MBT_SYNTAX; /* boundary type of next EOF */
675 static u_char buf[128]; /* need not be a full line */
676 int start = 0; /* indicates position of - in buffer */
678 if (buflen == 1 && *bp == '\n')
680 /* last \n in buffer may be part of next MIME boundary */
694 /* might be part of a MIME boundary */
709 if (atbol && c == '-')
711 /* check for a message boundary */
719 buflen = bp - buf - 1;
724 /* got "--", now check for rest of separator */
726 while (bp < &buf[sizeof buf - 2] &&
727 (c = getc(fp)) != EOF && c != '\n')
732 bt = mimeboundary((char *) &buf[start], boundaries);
737 /* we have a message boundary */
748 buflen = bp - buf - 1;
758 ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
761 ** fp -- the input file.
762 ** boundaries -- the current MIME boundaries.
763 ** btp -- if the return value is EOF, *btp is set to
764 ** the type of the boundary.
767 ** The next character in the input stream.
771 mime_getchar_crlf(fp, boundaries, btp)
776 static bool sendlf = FALSE;
784 c = mime_getchar(fp, boundaries, btp);
785 if (c == '\n' && MapNLtoCRLF)
793 ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
796 ** line -- the input line.
797 ** boundaries -- the set of currently pending boundaries.
800 ** MBT_NOTSEP -- if this is not a separator line
801 ** MBT_INTERMED -- if this is an intermediate separator
802 ** MBT_FINAL -- if this is a final boundary
803 ** MBT_SYNTAX -- if this is a boundary for the wrong
804 ** enclosure -- i.e., a syntax error.
808 mimeboundary(line, boundaries)
812 int type = MBT_NOTSEP;
815 extern int isboundary __P((char *, char **));
817 if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
820 if (line[i - 1] == '\n')
823 /* strip off trailing whitespace */
824 while (line[i - 1] == ' ' || line[i - 1] == '\t')
830 printf("mimeboundary: line=\"%s\"... ", line);
832 /* check for this as an intermediate boundary */
833 if (isboundary(&line[2], boundaries) >= 0)
835 else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
837 /* check for a final boundary */
839 if (isboundary(&line[2], boundaries) >= 0)
846 printf("%s\n", MimeBoundaryNames[type]);
850 ** DEFCHARSET -- return default character set for message
852 ** The first choice for character set is for the mailer
853 ** corresponding to the envelope sender. If neither that
854 ** nor the global configuration file has a default character
855 ** set defined, return "unknown-8bit" as recommended by
856 ** RFC 1428 section 3.
859 ** e -- the envelope for this message.
862 ** The default character set for that mailer.
867 register ENVELOPE *e;
869 if (e != NULL && e->e_from.q_mailer != NULL &&
870 e->e_from.q_mailer->m_defcharset != NULL)
871 return e->e_from.q_mailer->m_defcharset;
872 if (DefaultCharSet != NULL)
873 return DefaultCharSet;
874 return "unknown-8bit";
877 ** ISBOUNDARY -- is a given string a currently valid boundary?
880 ** line -- the current input line.
881 ** boundaries -- the list of valid boundaries.
884 ** The index number in boundaries if the line is found.
890 isboundary(line, boundaries)
896 for (i = 0; boundaries[i] != NULL; i++)
898 if (strcmp(line, boundaries[i]) == 0)
904 #endif /* MIME8TO7 */
909 ** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
911 ** This is a hack. Supports translating the two 7-bit body-encodings
912 ** (quoted-printable and base64) to 8-bit coded bodies.
914 ** There is not much point in supporting multipart here, as the UA
915 ** will be able to deal with encoded MIME bodies if it can parse MIME
916 ** multipart messages.
918 ** Note also that we wont be called unless it is a text/plain MIME
919 ** message, encoded base64 or QP and mailer flag '9' has been defined
922 ** Contributed by Marius Olaffson <marius@rhi.hi.is>.
925 ** mci -- mailer connection information.
926 ** header -- the header for this body part.
933 extern int mime_fromqp __P((u_char *, u_char **, int, int));
935 static char index_64[128] =
937 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
938 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
939 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
940 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
941 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
942 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
943 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
944 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
947 #define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
951 mime7to8(mci, header, e)
954 register ENVELOPE *e;
961 u_char fbuf[MAXLINE + 1];
962 char pvpbuf[MAXLINE];
963 extern u_char MimeTokenTab[256];
965 p = hvalue("Content-Transfer-Encoding", header);
967 (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
968 MimeTokenTab)) == NULL ||
971 /* "can't happen" -- upper level should have caught this */
972 syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
974 /* avoid bounce loops */
975 e->e_flags |= EF_DONT_MIME;
977 /* cheap failsafe algorithm -- should work on text/plain */
980 snprintf(buf, sizeof buf,
981 "Content-Transfer-Encoding: %s", p);
985 mci->mci_flags &= ~MCIF_INHEADER;
986 while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
990 cataddr(pvp, NULL, buf, sizeof buf, '\0');
993 mci->mci_flags |= MCIF_INHEADER;
994 putline("Content-Transfer-Encoding: 8bit", mci);
995 snprintf(buf, sizeof buf,
996 "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
997 cte, MyHostName, e->e_id);
1000 mci->mci_flags &= ~MCIF_INHEADER;
1003 ** Translate body encoding to 8-bit. Supports two types of
1004 ** encodings; "base64" and "quoted-printable". Assume qp if
1005 ** it is not base64.
1008 if (strcasecmp(cte, "base64") == 0)
1013 while ((c1 = fgetc(e->e_dfp)) != EOF)
1015 if (isascii(c1) && isspace(c1))
1020 c2 = fgetc(e->e_dfp);
1021 } while (isascii(c2) && isspace(c2));
1027 c3 = fgetc(e->e_dfp);
1028 } while (isascii(c3) && isspace(c3));
1034 c4 = fgetc(e->e_dfp);
1035 } while (isascii(c4) && isspace(c4));
1039 if (c1 == '=' || c2 == '=')
1044 *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1045 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
1047 if (*--fbufp != '\n' ||
1048 (fbufp > fbuf && *--fbufp != '\r'))
1050 putxline((char *) fbuf, fbufp - fbuf,
1057 *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1058 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
1060 if (*--fbufp != '\n' ||
1061 (fbufp > fbuf && *--fbufp != '\r'))
1063 putxline((char *) fbuf, fbufp - fbuf,
1070 *fbufp = ((c3 & 0x03) << 6) | c4;
1071 if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
1073 if (*--fbufp != '\n' ||
1074 (fbufp > fbuf && *--fbufp != '\r'))
1076 putxline((char *) fbuf, fbufp - fbuf,
1084 /* quoted-printable */
1086 while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
1088 if (mime_fromqp((u_char *) buf, &fbufp, 0,
1089 &fbuf[MAXLINE] - fbufp) == 0)
1092 if (fbufp - fbuf > 0)
1093 putxline((char *) fbuf, fbufp - fbuf - 1, mci,
1099 /* force out partial last line */
1103 putxline((char *) fbuf, fbufp - fbuf, mci, PXLF_MAPFROM);
1106 printf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1109 ** The following is based on Borenstein's "codes.c" module, with simplifying
1110 ** changes as we do not deal with multipart, and to do the translation in-core,
1111 ** with an attempt to prevent overrun of output buffers.
1113 ** What is needed here are changes to defned this code better against
1114 ** bad encodings. Questionable to always return 0xFF for bad mappings.
1117 static char index_hex[128] =
1119 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1120 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1121 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1122 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1123 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1124 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1125 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1126 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1129 #define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1132 mime_fromqp(infile, outfile, state, maxlen)
1135 int state; /* Decoding body (0) or header (1) */
1136 int maxlen; /* Max # of chars allowed in outfile */
1141 while ((c1 = *infile++) != '\0')
1145 if ((c1 = *infile++) == 0)
1148 if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1158 if ((c2 = *infile++) == '\0')
1163 } while ((c2 = HEXCHAR(c2)) == -1);
1165 if (c2 == -1 || ++nchar > maxlen)
1168 *(*outfile)++ = c1 << 4 | c2;
1173 if (state == 1 && c1 == '_')
1176 if (++nchar > maxlen)
1185 *(*outfile)++ = '\0';
1190 #endif /* MIME7TO8 */