1N/A/*
1N/A * Copyright (c) 1998-2003, 2006 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A * Copyright (c) 1994, 1996-1997 Eric P. Allman. All rights reserved.
1N/A * Copyright (c) 1994
1N/A * The Regents of the University of California. All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sendmail.h>
1N/A#include <string.h>
1N/A
1N/ASM_RCSID("@(#)$Id: mime.c,v 8.147 2007/09/26 23:29:11 ca Exp $")
1N/A
1N/A/*
1N/A** MIME support.
1N/A**
1N/A** I am indebted to John Beck of Hewlett-Packard, who contributed
1N/A** his code to me for inclusion. As it turns out, I did not use
1N/A** his code since he used a "minimum change" approach that used
1N/A** several temp files, and I wanted a "minimum impact" approach
1N/A** that would avoid copying. However, looking over his code
1N/A** helped me cement my understanding of the problem.
1N/A**
1N/A** I also looked at, but did not directly use, Nathaniel
1N/A** Borenstein's "code.c" module. Again, it functioned as
1N/A** a file-to-file translator, which did not fit within my
1N/A** design bounds, but it was a useful base for understanding
1N/A** the problem.
1N/A*/
1N/A
1N/A/* use "old" mime 7 to 8 algorithm by default */
1N/A#ifndef MIME7TO8_OLD
1N/A# define MIME7TO8_OLD 1
1N/A#endif /* ! MIME7TO8_OLD */
1N/A
1N/A#if MIME8TO7
1N/Astatic int isboundary __P((char *, char **));
1N/Astatic int mimeboundary __P((char *, char **));
1N/Astatic int mime_getchar __P((SM_FILE_T *, char **, int *));
1N/Astatic int mime_getchar_crlf __P((SM_FILE_T *, char **, int *));
1N/A
1N/A/* character set for hex and base64 encoding */
1N/Astatic char Base16Code[] = "0123456789ABCDEF";
1N/Astatic char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1N/A
1N/A/* types of MIME boundaries */
1N/A# define MBT_SYNTAX 0 /* syntax error */
1N/A# define MBT_NOTSEP 1 /* not a boundary */
1N/A# define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */
1N/A# define MBT_FINAL 3 /* final boundary (trailing -- included) */
1N/A
1N/Astatic char *MimeBoundaryNames[] =
1N/A{
1N/A "SYNTAX", "NOTSEP", "INTERMED", "FINAL"
1N/A};
1N/A
1N/Astatic bool MapNLtoCRLF;
1N/A
1N/A/*
1N/A** MIME8TO7 -- output 8 bit body in 7 bit format
1N/A**
1N/A** The header has already been output -- this has to do the
1N/A** 8 to 7 bit conversion. It would be easy if we didn't have
1N/A** to deal with nested formats (multipart/xxx and message/rfc822).
1N/A**
1N/A** We won't be called if we don't have to do a conversion, and
1N/A** appropriate MIME-Version: and Content-Type: fields have been
1N/A** output. Any Content-Transfer-Encoding: field has not been
1N/A** output, and we can add it here.
1N/A**
1N/A** Parameters:
1N/A** mci -- mailer connection information.
1N/A** header -- the header for this body part.
1N/A** e -- envelope.
1N/A** boundaries -- the currently pending message boundaries.
1N/A** NULL if we are processing the outer portion.
1N/A** flags -- to tweak processing.
1N/A** level -- recursion level.
1N/A**
1N/A** Returns:
1N/A** An indicator of what terminated the message part:
1N/A** MBT_FINAL -- the final boundary
1N/A** MBT_INTERMED -- an intermediate boundary
1N/A** MBT_NOTSEP -- an end of file
1N/A** SM_IO_EOF -- I/O error occurred
1N/A*/
1N/A
1N/Astruct args
1N/A{
1N/A char *a_field; /* name of field */
1N/A char *a_value; /* value of that field */
1N/A};
1N/A
1N/Aint
1N/Amime8to7(mci, header, e, boundaries, flags, level)
1N/A register MCI *mci;
1N/A HDR *header;
1N/A register ENVELOPE *e;
1N/A char **boundaries;
1N/A int flags;
1N/A int level;
1N/A{
1N/A register char *p;
1N/A int linelen;
1N/A int bt;
1N/A off_t offset;
1N/A size_t sectionsize, sectionhighbits;
1N/A int i;
1N/A char *type;
1N/A char *subtype;
1N/A char *cte;
1N/A char **pvp;
1N/A int argc = 0;
1N/A char *bp;
1N/A bool use_qp = false;
1N/A struct args argv[MAXMIMEARGS];
1N/A char bbuf[128];
1N/A char buf[MAXLINE];
1N/A char pvpbuf[MAXLINE];
1N/A extern unsigned char MimeTokenTab[256];
1N/A
1N/A if (level > MAXMIMENESTING)
1N/A {
1N/A if (!bitset(EF_TOODEEP, e->e_flags))
1N/A {
1N/A if (tTd(43, 4))
1N/A sm_dprintf("mime8to7: too deep, level=%d\n",
1N/A level);
1N/A usrerr("mime8to7: recursion level %d exceeded",
1N/A level);
1N/A e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
1N/A }
1N/A }
1N/A if (tTd(43, 1))
1N/A {
1N/A sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
1N/A if (boundaries[0] == NULL)
1N/A sm_dprintf(" <none>");
1N/A else
1N/A {
1N/A for (i = 0; boundaries[i] != NULL; i++)
1N/A sm_dprintf(" %s", boundaries[i]);
1N/A }
1N/A sm_dprintf("\n");
1N/A }
1N/A MapNLtoCRLF = true;
1N/A p = hvalue("Content-Transfer-Encoding", header);
1N/A if (p == NULL ||
1N/A (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1N/A MimeTokenTab, false)) == NULL ||
1N/A pvp[0] == NULL)
1N/A {
1N/A cte = NULL;
1N/A }
1N/A else
1N/A {
1N/A cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
1N/A cte = sm_rpool_strdup_x(e->e_rpool, buf);
1N/A }
1N/A
1N/A type = subtype = NULL;
1N/A p = hvalue("Content-Type", header);
1N/A if (p == NULL)
1N/A {
1N/A if (bitset(M87F_DIGEST, flags))
1N/A p = "message/rfc822";
1N/A else
1N/A p = "text/plain";
1N/A }
1N/A if (p != NULL &&
1N/A (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1N/A MimeTokenTab, false)) != NULL &&
1N/A pvp[0] != NULL)
1N/A {
1N/A if (tTd(43, 40))
1N/A {
1N/A for (i = 0; pvp[i] != NULL; i++)
1N/A sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
1N/A }
1N/A type = *pvp++;
1N/A if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
1N/A *++pvp != NULL)
1N/A {
1N/A subtype = *pvp++;
1N/A }
1N/A
1N/A /* break out parameters */
1N/A while (*pvp != NULL && argc < MAXMIMEARGS)
1N/A {
1N/A /* skip to semicolon separator */
1N/A while (*pvp != NULL && strcmp(*pvp, ";") != 0)
1N/A pvp++;
1N/A if (*pvp++ == NULL || *pvp == NULL)
1N/A break;
1N/A
1N/A /* complain about empty values */
1N/A if (strcmp(*pvp, ";") == 0)
1N/A {
1N/A usrerr("mime8to7: Empty parameter in Content-Type header");
1N/A
1N/A /* avoid bounce loops */
1N/A e->e_flags |= EF_DONT_MIME;
1N/A continue;
1N/A }
1N/A
1N/A /* extract field name */
1N/A argv[argc].a_field = *pvp++;
1N/A
1N/A /* see if there is a value */
1N/A if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
1N/A (*++pvp == NULL || strcmp(*pvp, ";") != 0))
1N/A {
1N/A argv[argc].a_value = *pvp;
1N/A argc++;
1N/A }
1N/A }
1N/A }
1N/A
1N/A /* check for disaster cases */
1N/A if (type == NULL)
1N/A type = "-none-";
1N/A if (subtype == NULL)
1N/A subtype = "-none-";
1N/A
1N/A /* don't propagate some flags more than one level into the message */
1N/A flags &= ~M87F_DIGEST;
1N/A
1N/A /*
1N/A ** Check for cases that can not be encoded.
1N/A **
1N/A ** For example, you can't encode certain kinds of types
1N/A ** or already-encoded messages. If we find this case,
1N/A ** just copy it through.
1N/A */
1N/A
1N/A (void) sm_snprintf(buf, sizeof(buf), "%.100s/%.100s", type, subtype);
1N/A if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
1N/A flags |= M87F_NO8BIT;
1N/A
1N/A# ifdef USE_B_CLASS
1N/A if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
1N/A MapNLtoCRLF = false;
1N/A# endif /* USE_B_CLASS */
1N/A if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
1N/A use_qp = true;
1N/A
1N/A /*
1N/A ** Multipart requires special processing.
1N/A **
1N/A ** Do a recursive descent into the message.
1N/A */
1N/A
1N/A if (sm_strcasecmp(type, "multipart") == 0 &&
1N/A (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)) &&
1N/A !bitset(EF_TOODEEP, e->e_flags)
1N/A )
1N/A {
1N/A
1N/A if (sm_strcasecmp(subtype, "digest") == 0)
1N/A flags |= M87F_DIGEST;
1N/A
1N/A for (i = 0; i < argc; i++)
1N/A {
1N/A if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
1N/A break;
1N/A }
1N/A if (i >= argc || argv[i].a_value == NULL)
1N/A {
1N/A usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
1N/A i >= argc ? "missing" : "bogus", p);
1N/A p = "---";
1N/A
1N/A /* avoid bounce loops */
1N/A e->e_flags |= EF_DONT_MIME;
1N/A }
1N/A else
1N/A {
1N/A p = argv[i].a_value;
1N/A stripquotes(p);
1N/A }
1N/A if (sm_strlcpy(bbuf, p, sizeof(bbuf)) >= sizeof(bbuf))
1N/A {
1N/A usrerr("mime8to7: multipart boundary \"%s\" too long",
1N/A p);
1N/A
1N/A /* avoid bounce loops */
1N/A e->e_flags |= EF_DONT_MIME;
1N/A }
1N/A
1N/A if (tTd(43, 1))
1N/A sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
1N/A bbuf);
1N/A for (i = 0; i < MAXMIMENESTING; i++)
1N/A {
1N/A if (boundaries[i] == NULL)
1N/A break;
1N/A }
1N/A if (i >= MAXMIMENESTING)
1N/A {
1N/A if (tTd(43, 4))
1N/A sm_dprintf("mime8to7: too deep, i=%d\n", i);
1N/A if (!bitset(EF_TOODEEP, e->e_flags))
1N/A usrerr("mime8to7: multipart nesting boundary too deep");
1N/A
1N/A /* avoid bounce loops */
1N/A e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
1N/A }
1N/A else
1N/A {
1N/A boundaries[i] = bbuf;
1N/A boundaries[i + 1] = NULL;
1N/A }
1N/A mci->mci_flags |= MCIF_INMIME;
1N/A
1N/A /* skip the early "comment" prologue */
1N/A if (!putline("", mci))
1N/A goto writeerr;
1N/A mci->mci_flags &= ~MCIF_INHEADER;
1N/A bt = MBT_FINAL;
1N/A while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
1N/A != NULL)
1N/A {
1N/A bt = mimeboundary(buf, boundaries);
1N/A if (bt != MBT_NOTSEP)
1N/A break;
1N/A if (!putxline(buf, strlen(buf), mci,
1N/A PXLF_MAPFROM|PXLF_STRIP8BIT))
1N/A goto writeerr;
1N/A if (tTd(43, 99))
1N/A sm_dprintf(" ...%s", buf);
1N/A }
1N/A if (sm_io_eof(e->e_dfp))
1N/A bt = MBT_FINAL;
1N/A while (bt != MBT_FINAL)
1N/A {
1N/A auto HDR *hdr = NULL;
1N/A
1N/A (void) sm_strlcpyn(buf, sizeof(buf), 2, "--", bbuf);
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A if (tTd(43, 35))
1N/A sm_dprintf(" ...%s\n", buf);
1N/A collect(e->e_dfp, false, &hdr, e, false);
1N/A if (tTd(43, 101))
1N/A putline("+++after collect", mci);
1N/A if (!putheader(mci, hdr, e, flags))
1N/A goto writeerr;
1N/A if (tTd(43, 101))
1N/A putline("+++after putheader", mci);
1N/A bt = mime8to7(mci, hdr, e, boundaries, flags,
1N/A level + 1);
1N/A if (bt == SM_IO_EOF)
1N/A goto writeerr;
1N/A }
1N/A (void) sm_strlcpyn(buf, sizeof(buf), 3, "--", bbuf, "--");
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A if (tTd(43, 35))
1N/A sm_dprintf(" ...%s\n", buf);
1N/A boundaries[i] = NULL;
1N/A mci->mci_flags &= ~MCIF_INMIME;
1N/A
1N/A /* skip the late "comment" epilogue */
1N/A while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
1N/A != NULL)
1N/A {
1N/A bt = mimeboundary(buf, boundaries);
1N/A if (bt != MBT_NOTSEP)
1N/A break;
1N/A if (!putxline(buf, strlen(buf), mci,
1N/A PXLF_MAPFROM|PXLF_STRIP8BIT))
1N/A goto writeerr;
1N/A if (tTd(43, 99))
1N/A sm_dprintf(" ...%s", buf);
1N/A }
1N/A if (sm_io_eof(e->e_dfp))
1N/A bt = MBT_FINAL;
1N/A if (tTd(43, 3))
1N/A sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
1N/A MimeBoundaryNames[bt]);
1N/A return bt;
1N/A }
1N/A
1N/A /*
1N/A ** Message/xxx types -- recurse exactly once.
1N/A **
1N/A ** Class 's' is predefined to have "rfc822" only.
1N/A */
1N/A
1N/A if (sm_strcasecmp(type, "message") == 0)
1N/A {
1N/A if (!wordinclass(subtype, 's') ||
1N/A bitset(EF_TOODEEP, e->e_flags))
1N/A {
1N/A flags |= M87F_NO8BIT;
1N/A }
1N/A else
1N/A {
1N/A auto HDR *hdr = NULL;
1N/A
1N/A if (!putline("", mci))
1N/A goto writeerr;
1N/A
1N/A mci->mci_flags |= MCIF_INMIME;
1N/A collect(e->e_dfp, false, &hdr, e, false);
1N/A if (tTd(43, 101))
1N/A putline("+++after collect", mci);
1N/A if (!putheader(mci, hdr, e, flags))
1N/A goto writeerr;
1N/A if (tTd(43, 101))
1N/A putline("+++after putheader", mci);
1N/A if (hvalue("MIME-Version", hdr) == NULL &&
1N/A !bitset(M87F_NO8TO7, flags) &&
1N/A !putline("MIME-Version: 1.0", mci))
1N/A goto writeerr;
1N/A bt = mime8to7(mci, hdr, e, boundaries, flags,
1N/A level + 1);
1N/A mci->mci_flags &= ~MCIF_INMIME;
1N/A return bt;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A ** Non-compound body type
1N/A **
1N/A ** Compute the ratio of seven to eight bit characters;
1N/A ** use that as a heuristic to decide how to do the
1N/A ** encoding.
1N/A */
1N/A
1N/A sectionsize = sectionhighbits = 0;
1N/A if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
1N/A {
1N/A /* remember where we were */
1N/A offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
1N/A if (offset == -1)
1N/A syserr("mime8to7: cannot sm_io_tell on %cf%s",
1N/A DATAFL_LETTER, e->e_id);
1N/A
1N/A /* do a scan of this body type to count character types */
1N/A while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
1N/A != NULL)
1N/A {
1N/A if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
1N/A break;
1N/A for (p = buf; *p != '\0'; p++)
1N/A {
1N/A /* count bytes with the high bit set */
1N/A sectionsize++;
1N/A if (bitset(0200, *p))
1N/A sectionhighbits++;
1N/A }
1N/A
1N/A /*
1N/A ** Heuristic: if 1/4 of the first 4K bytes are 8-bit,
1N/A ** assume base64. This heuristic avoids double-reading
1N/A ** large graphics or video files.
1N/A */
1N/A
1N/A if (sectionsize >= 4096 &&
1N/A sectionhighbits > sectionsize / 4)
1N/A break;
1N/A }
1N/A
1N/A /* return to the original offset for processing */
1N/A /* XXX use relative seeks to handle >31 bit file sizes? */
1N/A if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
1N/A syserr("mime8to7: cannot sm_io_fseek on %cf%s",
1N/A DATAFL_LETTER, e->e_id);
1N/A else
1N/A sm_io_clearerr(e->e_dfp);
1N/A }
1N/A
1N/A /*
1N/A ** Heuristically determine encoding method.
1N/A ** If more than 1/8 of the total characters have the
1N/A ** eighth bit set, use base64; else use quoted-printable.
1N/A ** However, only encode binary encoded data as base64,
1N/A ** since otherwise the NL=>CRLF mapping will be a problem.
1N/A */
1N/A
1N/A if (tTd(43, 8))
1N/A {
1N/A sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
1N/A (long) sectionhighbits, (long) sectionsize,
1N/A cte == NULL ? "[none]" : cte,
1N/A type == NULL ? "[none]" : type,
1N/A subtype == NULL ? "[none]" : subtype);
1N/A }
1N/A if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
1N/A sectionsize = sectionhighbits;
1N/A linelen = 0;
1N/A bp = buf;
1N/A if (sectionhighbits == 0)
1N/A {
1N/A /* no encoding necessary */
1N/A if (cte != NULL &&
1N/A bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
1N/A mci->mci_flags) &&
1N/A !bitset(M87F_NO8TO7, flags))
1N/A {
1N/A /*
1N/A ** Skip _unless_ in MIME mode and potentially
1N/A ** converting from 8 bit to 7 bit MIME. See
1N/A ** putheader() for the counterpart where the
1N/A ** CTE header is skipped in the opposite
1N/A ** situation.
1N/A */
1N/A
1N/A (void) sm_snprintf(buf, sizeof(buf),
1N/A "Content-Transfer-Encoding: %.200s", cte);
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A if (tTd(43, 36))
1N/A sm_dprintf(" ...%s\n", buf);
1N/A }
1N/A if (!putline("", mci))
1N/A goto writeerr;
1N/A mci->mci_flags &= ~MCIF_INHEADER;
1N/A while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
1N/A != NULL)
1N/A {
1N/A if (!bitset(MCIF_INLONGLINE, mci->mci_flags))
1N/A {
1N/A bt = mimeboundary(buf, boundaries);
1N/A if (bt != MBT_NOTSEP)
1N/A break;
1N/A }
1N/A if (!putxline(buf, strlen(buf), mci,
1N/A PXLF_MAPFROM|PXLF_NOADDEOL))
1N/A goto writeerr;
1N/A }
1N/A if (sm_io_eof(e->e_dfp))
1N/A bt = MBT_FINAL;
1N/A }
1N/A else if (!MapNLtoCRLF ||
1N/A (sectionsize / 8 < sectionhighbits && !use_qp))
1N/A {
1N/A /* use base64 encoding */
1N/A int c1, c2;
1N/A
1N/A if (tTd(43, 36))
1N/A sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
1N/A if (!putline("Content-Transfer-Encoding: base64", mci))
1N/A goto writeerr;
1N/A (void) sm_snprintf(buf, sizeof(buf),
1N/A "X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
1N/A MyHostName, e->e_id);
1N/A if (!putline(buf, mci) || !putline("", mci))
1N/A goto writeerr;
1N/A mci->mci_flags &= ~MCIF_INHEADER;
1N/A while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
1N/A SM_IO_EOF)
1N/A {
1N/A if (linelen > 71)
1N/A {
1N/A *bp = '\0';
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A linelen = 0;
1N/A bp = buf;
1N/A }
1N/A linelen += 4;
1N/A *bp++ = Base64Code[(c1 >> 2)];
1N/A c1 = (c1 & 0x03) << 4;
1N/A c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
1N/A if (c2 == SM_IO_EOF)
1N/A {
1N/A *bp++ = Base64Code[c1];
1N/A *bp++ = '=';
1N/A *bp++ = '=';
1N/A break;
1N/A }
1N/A c1 |= (c2 >> 4) & 0x0f;
1N/A *bp++ = Base64Code[c1];
1N/A c1 = (c2 & 0x0f) << 2;
1N/A c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
1N/A if (c2 == SM_IO_EOF)
1N/A {
1N/A *bp++ = Base64Code[c1];
1N/A *bp++ = '=';
1N/A break;
1N/A }
1N/A c1 |= (c2 >> 6) & 0x03;
1N/A *bp++ = Base64Code[c1];
1N/A *bp++ = Base64Code[c2 & 0x3f];
1N/A }
1N/A *bp = '\0';
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A }
1N/A else
1N/A {
1N/A /* use quoted-printable encoding */
1N/A int c1, c2;
1N/A int fromstate;
1N/A BITMAP256 badchars;
1N/A
1N/A /* set up map of characters that must be mapped */
1N/A clrbitmap(badchars);
1N/A for (c1 = 0x00; c1 < 0x20; c1++)
1N/A setbitn(c1, badchars);
1N/A clrbitn('\t', badchars);
1N/A for (c1 = 0x7f; c1 < 0x100; c1++)
1N/A setbitn(c1, badchars);
1N/A setbitn('=', badchars);
1N/A if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
1N/A for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
1N/A setbitn(*p, badchars);
1N/A
1N/A if (tTd(43, 36))
1N/A sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
1N/A if (!putline("Content-Transfer-Encoding: quoted-printable",
1N/A mci))
1N/A goto writeerr;
1N/A (void) sm_snprintf(buf, sizeof(buf),
1N/A "X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
1N/A MyHostName, e->e_id);
1N/A if (!putline(buf, mci) || !putline("", mci))
1N/A goto writeerr;
1N/A mci->mci_flags &= ~MCIF_INHEADER;
1N/A fromstate = 0;
1N/A c2 = '\n';
1N/A while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
1N/A SM_IO_EOF)
1N/A {
1N/A if (c1 == '\n')
1N/A {
1N/A if (c2 == ' ' || c2 == '\t')
1N/A {
1N/A *bp++ = '=';
1N/A *bp++ = Base16Code[(c2 >> 4) & 0x0f];
1N/A *bp++ = Base16Code[c2 & 0x0f];
1N/A }
1N/A if (buf[0] == '.' && bp == &buf[1])
1N/A {
1N/A buf[0] = '=';
1N/A *bp++ = Base16Code[('.' >> 4) & 0x0f];
1N/A *bp++ = Base16Code['.' & 0x0f];
1N/A }
1N/A *bp = '\0';
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A linelen = fromstate = 0;
1N/A bp = buf;
1N/A c2 = c1;
1N/A continue;
1N/A }
1N/A if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
1N/A bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
1N/A {
1N/A *bp++ = '=';
1N/A *bp++ = '2';
1N/A *bp++ = '0';
1N/A linelen += 3;
1N/A }
1N/A else if (c2 == ' ' || c2 == '\t')
1N/A {
1N/A *bp++ = c2;
1N/A linelen++;
1N/A }
1N/A if (linelen > 72 &&
1N/A (linelen > 75 || c1 != '.' ||
1N/A (linelen > 73 && c2 == '.')))
1N/A {
1N/A if (linelen > 73 && c2 == '.')
1N/A bp--;
1N/A else
1N/A c2 = '\n';
1N/A *bp++ = '=';
1N/A *bp = '\0';
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A linelen = fromstate = 0;
1N/A bp = buf;
1N/A if (c2 == '.')
1N/A {
1N/A *bp++ = '.';
1N/A linelen++;
1N/A }
1N/A }
1N/A if (bitnset(bitidx(c1), badchars))
1N/A {
1N/A *bp++ = '=';
1N/A *bp++ = Base16Code[(c1 >> 4) & 0x0f];
1N/A *bp++ = Base16Code[c1 & 0x0f];
1N/A linelen += 3;
1N/A }
1N/A else if (c1 != ' ' && c1 != '\t')
1N/A {
1N/A if (linelen < 4 && c1 == "From"[linelen])
1N/A fromstate++;
1N/A *bp++ = c1;
1N/A linelen++;
1N/A }
1N/A c2 = c1;
1N/A }
1N/A
1N/A /* output any saved character */
1N/A if (c2 == ' ' || c2 == '\t')
1N/A {
1N/A *bp++ = '=';
1N/A *bp++ = Base16Code[(c2 >> 4) & 0x0f];
1N/A *bp++ = Base16Code[c2 & 0x0f];
1N/A linelen += 3;
1N/A }
1N/A
1N/A if (linelen > 0 || boundaries[0] != NULL)
1N/A {
1N/A *bp = '\0';
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A }
1N/A
1N/A }
1N/A if (tTd(43, 3))
1N/A sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
1N/A return bt;
1N/A
1N/A writeerr:
1N/A return SM_IO_EOF;
1N/A}
1N/A/*
1N/A** MIME_GETCHAR -- get a character for MIME processing
1N/A**
1N/A** Treats boundaries as SM_IO_EOF.
1N/A**
1N/A** Parameters:
1N/A** fp -- the input file.
1N/A** boundaries -- the current MIME boundaries.
1N/A** btp -- if the return value is SM_IO_EOF, *btp is set to
1N/A** the type of the boundary.
1N/A**
1N/A** Returns:
1N/A** The next character in the input stream.
1N/A*/
1N/A
1N/Astatic int
1N/Amime_getchar(fp, boundaries, btp)
1N/A register SM_FILE_T *fp;
1N/A char **boundaries;
1N/A int *btp;
1N/A{
1N/A int c;
1N/A static unsigned char *bp = NULL;
1N/A static int buflen = 0;
1N/A static bool atbol = true; /* at beginning of line */
1N/A static int bt = MBT_SYNTAX; /* boundary type of next SM_IO_EOF */
1N/A static unsigned char buf[128]; /* need not be a full line */
1N/A int start = 0; /* indicates position of - in buffer */
1N/A
1N/A if (buflen == 1 && *bp == '\n')
1N/A {
1N/A /* last \n in buffer may be part of next MIME boundary */
1N/A c = *bp;
1N/A }
1N/A else if (buflen > 0)
1N/A {
1N/A buflen--;
1N/A return *bp++;
1N/A }
1N/A else
1N/A c = sm_io_getc(fp, SM_TIME_DEFAULT);
1N/A bp = buf;
1N/A buflen = 0;
1N/A if (c == '\n')
1N/A {
1N/A /* might be part of a MIME boundary */
1N/A *bp++ = c;
1N/A atbol = true;
1N/A c = sm_io_getc(fp, SM_TIME_DEFAULT);
1N/A if (c == '\n')
1N/A {
1N/A (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
1N/A return c;
1N/A }
1N/A start = 1;
1N/A }
1N/A if (c != SM_IO_EOF)
1N/A *bp++ = c;
1N/A else
1N/A bt = MBT_FINAL;
1N/A if (atbol && c == '-')
1N/A {
1N/A /* check for a message boundary */
1N/A c = sm_io_getc(fp, SM_TIME_DEFAULT);
1N/A if (c != '-')
1N/A {
1N/A if (c != SM_IO_EOF)
1N/A *bp++ = c;
1N/A else
1N/A bt = MBT_FINAL;
1N/A buflen = bp - buf - 1;
1N/A bp = buf;
1N/A return *bp++;
1N/A }
1N/A
1N/A /* got "--", now check for rest of separator */
1N/A *bp++ = '-';
1N/A while (bp < &buf[sizeof(buf) - 2] &&
1N/A (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
1N/A c != '\n')
1N/A {
1N/A *bp++ = c;
1N/A }
1N/A *bp = '\0'; /* XXX simply cut off? */
1N/A bt = mimeboundary((char *) &buf[start], boundaries);
1N/A switch (bt)
1N/A {
1N/A case MBT_FINAL:
1N/A case MBT_INTERMED:
1N/A /* we have a message boundary */
1N/A buflen = 0;
1N/A *btp = bt;
1N/A return SM_IO_EOF;
1N/A }
1N/A
1N/A if (bp < &buf[sizeof(buf) - 2] && c != SM_IO_EOF)
1N/A *bp++ = c;
1N/A }
1N/A
1N/A atbol = c == '\n';
1N/A buflen = bp - buf - 1;
1N/A if (buflen < 0)
1N/A {
1N/A *btp = bt;
1N/A return SM_IO_EOF;
1N/A }
1N/A bp = buf;
1N/A return *bp++;
1N/A}
1N/A/*
1N/A** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
1N/A**
1N/A** Parameters:
1N/A** fp -- the input file.
1N/A** boundaries -- the current MIME boundaries.
1N/A** btp -- if the return value is SM_IO_EOF, *btp is set to
1N/A** the type of the boundary.
1N/A**
1N/A** Returns:
1N/A** The next character in the input stream.
1N/A*/
1N/A
1N/Astatic int
1N/Amime_getchar_crlf(fp, boundaries, btp)
1N/A register SM_FILE_T *fp;
1N/A char **boundaries;
1N/A int *btp;
1N/A{
1N/A static bool sendlf = false;
1N/A int c;
1N/A
1N/A if (sendlf)
1N/A {
1N/A sendlf = false;
1N/A return '\n';
1N/A }
1N/A c = mime_getchar(fp, boundaries, btp);
1N/A if (c == '\n' && MapNLtoCRLF)
1N/A {
1N/A sendlf = true;
1N/A return '\r';
1N/A }
1N/A return c;
1N/A}
1N/A/*
1N/A** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
1N/A**
1N/A** Parameters:
1N/A** line -- the input line.
1N/A** boundaries -- the set of currently pending boundaries.
1N/A**
1N/A** Returns:
1N/A** MBT_NOTSEP -- if this is not a separator line
1N/A** MBT_INTERMED -- if this is an intermediate separator
1N/A** MBT_FINAL -- if this is a final boundary
1N/A** MBT_SYNTAX -- if this is a boundary for the wrong
1N/A** enclosure -- i.e., a syntax error.
1N/A*/
1N/A
1N/Astatic int
1N/Amimeboundary(line, boundaries)
1N/A register char *line;
1N/A char **boundaries;
1N/A{
1N/A int type = MBT_NOTSEP;
1N/A int i;
1N/A int savec;
1N/A
1N/A if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
1N/A return MBT_NOTSEP;
1N/A i = strlen(line);
1N/A if (i > 0 && line[i - 1] == '\n')
1N/A i--;
1N/A
1N/A /* strip off trailing whitespace */
1N/A while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
1N/A#if _FFR_MIME_CR_OK
1N/A || line[i - 1] == '\r'
1N/A#endif /* _FFR_MIME_CR_OK */
1N/A ))
1N/A i--;
1N/A savec = line[i];
1N/A line[i] = '\0';
1N/A
1N/A if (tTd(43, 5))
1N/A sm_dprintf("mimeboundary: line=\"%s\"... ", line);
1N/A
1N/A /* check for this as an intermediate boundary */
1N/A if (isboundary(&line[2], boundaries) >= 0)
1N/A type = MBT_INTERMED;
1N/A else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
1N/A {
1N/A /* check for a final boundary */
1N/A line[i - 2] = '\0';
1N/A if (isboundary(&line[2], boundaries) >= 0)
1N/A type = MBT_FINAL;
1N/A line[i - 2] = '-';
1N/A }
1N/A
1N/A line[i] = savec;
1N/A if (tTd(43, 5))
1N/A sm_dprintf("%s\n", MimeBoundaryNames[type]);
1N/A return type;
1N/A}
1N/A/*
1N/A** DEFCHARSET -- return default character set for message
1N/A**
1N/A** The first choice for character set is for the mailer
1N/A** corresponding to the envelope sender. If neither that
1N/A** nor the global configuration file has a default character
1N/A** set defined, return "unknown-8bit" as recommended by
1N/A** RFC 1428 section 3.
1N/A**
1N/A** Parameters:
1N/A** e -- the envelope for this message.
1N/A**
1N/A** Returns:
1N/A** The default character set for that mailer.
1N/A*/
1N/A
1N/Achar *
1N/Adefcharset(e)
1N/A register ENVELOPE *e;
1N/A{
1N/A if (e != NULL && e->e_from.q_mailer != NULL &&
1N/A e->e_from.q_mailer->m_defcharset != NULL)
1N/A return e->e_from.q_mailer->m_defcharset;
1N/A if (DefaultCharSet != NULL)
1N/A return DefaultCharSet;
1N/A return "unknown-8bit";
1N/A}
1N/A/*
1N/A** ISBOUNDARY -- is a given string a currently valid boundary?
1N/A**
1N/A** Parameters:
1N/A** line -- the current input line.
1N/A** boundaries -- the list of valid boundaries.
1N/A**
1N/A** Returns:
1N/A** The index number in boundaries if the line is found.
1N/A** -1 -- otherwise.
1N/A**
1N/A*/
1N/A
1N/Astatic int
1N/Aisboundary(line, boundaries)
1N/A char *line;
1N/A char **boundaries;
1N/A{
1N/A register int i;
1N/A
1N/A for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
1N/A {
1N/A if (strcmp(line, boundaries[i]) == 0)
1N/A return i;
1N/A }
1N/A return -1;
1N/A}
1N/A#endif /* MIME8TO7 */
1N/A
1N/A#if MIME7TO8
1N/Astatic int mime_fromqp __P((unsigned char *, unsigned char **, int));
1N/A
1N/A/*
1N/A** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
1N/A**
1N/A** This is a hack. Supports translating the two 7-bit body-encodings
1N/A** (quoted-printable and base64) to 8-bit coded bodies.
1N/A**
1N/A** There is not much point in supporting multipart here, as the UA
1N/A** will be able to deal with encoded MIME bodies if it can parse MIME
1N/A** multipart messages.
1N/A**
1N/A** Note also that we won't be called unless it is a text/plain MIME
1N/A** message, encoded base64 or QP and mailer flag '9' has been defined
1N/A** on mailer.
1N/A**
1N/A** Contributed by Marius Olaffson <marius@rhi.hi.is>.
1N/A**
1N/A** Parameters:
1N/A** mci -- mailer connection information.
1N/A** header -- the header for this body part.
1N/A** e -- envelope.
1N/A**
1N/A** Returns:
1N/A** true iff body was written successfully
1N/A*/
1N/A
1N/Astatic char index_64[128] =
1N/A{
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
1N/A 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
1N/A -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
1N/A 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
1N/A -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
1N/A 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
1N/A};
1N/A
1N/A# define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
1N/A
1N/Abool
1N/Amime7to8(mci, header, e)
1N/A register MCI *mci;
1N/A HDR *header;
1N/A register ENVELOPE *e;
1N/A{
1N/A int pxflags;
1N/A register char *p;
1N/A char *cte;
1N/A char **pvp;
1N/A unsigned char *fbufp;
1N/A char buf[MAXLINE];
1N/A unsigned char fbuf[MAXLINE + 1];
1N/A char pvpbuf[MAXLINE];
1N/A extern unsigned char MimeTokenTab[256];
1N/A
1N/A p = hvalue("Content-Transfer-Encoding", header);
1N/A if (p == NULL ||
1N/A (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1N/A MimeTokenTab, false)) == NULL ||
1N/A pvp[0] == NULL)
1N/A {
1N/A /* "can't happen" -- upper level should have caught this */
1N/A syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
1N/A
1N/A /* avoid bounce loops */
1N/A e->e_flags |= EF_DONT_MIME;
1N/A
1N/A /* cheap failsafe algorithm -- should work on text/plain */
1N/A if (p != NULL)
1N/A {
1N/A (void) sm_snprintf(buf, sizeof(buf),
1N/A "Content-Transfer-Encoding: %s", p);
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A }
1N/A if (!putline("", mci))
1N/A goto writeerr;
1N/A mci->mci_flags &= ~MCIF_INHEADER;
1N/A while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
1N/A != NULL)
1N/A {
1N/A if (!putline(buf, mci))
1N/A goto writeerr;
1N/A }
1N/A return true;
1N/A }
1N/A cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
1N/A cte = sm_rpool_strdup_x(e->e_rpool, buf);
1N/A
1N/A mci->mci_flags |= MCIF_INHEADER;
1N/A if (!putline("Content-Transfer-Encoding: 8bit", mci))
1N/A goto writeerr;
1N/A (void) sm_snprintf(buf, sizeof(buf),
1N/A "X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
1N/A cte, MyHostName, e->e_id);
1N/A if (!putline(buf, mci) || !putline("", mci))
1N/A goto writeerr;
1N/A mci->mci_flags &= ~MCIF_INHEADER;
1N/A
1N/A /*
1N/A ** Translate body encoding to 8-bit. Supports two types of
1N/A ** encodings; "base64" and "quoted-printable". Assume qp if
1N/A ** it is not base64.
1N/A */
1N/A
1N/A pxflags = PXLF_MAPFROM;
1N/A if (sm_strcasecmp(cte, "base64") == 0)
1N/A {
1N/A int c1, c2, c3, c4;
1N/A
1N/A fbufp = fbuf;
1N/A while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
1N/A SM_IO_EOF)
1N/A {
1N/A if (isascii(c1) && isspace(c1))
1N/A continue;
1N/A
1N/A do
1N/A {
1N/A c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1N/A } while (isascii(c2) && isspace(c2));
1N/A if (c2 == SM_IO_EOF)
1N/A break;
1N/A
1N/A do
1N/A {
1N/A c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1N/A } while (isascii(c3) && isspace(c3));
1N/A if (c3 == SM_IO_EOF)
1N/A break;
1N/A
1N/A do
1N/A {
1N/A c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
1N/A } while (isascii(c4) && isspace(c4));
1N/A if (c4 == SM_IO_EOF)
1N/A break;
1N/A
1N/A if (c1 == '=' || c2 == '=')
1N/A continue;
1N/A c1 = CHAR64(c1);
1N/A c2 = CHAR64(c2);
1N/A
1N/A#if MIME7TO8_OLD
1N/A#define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1N/A ++fbufp;
1N/A#else /* MIME7TO8_OLD */
1N/A#define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
1N/A { \
1N/A ++fbufp; \
1N/A pxflags |= PXLF_NOADDEOL; \
1N/A }
1N/A#endif /* MIME7TO8_OLD */
1N/A
1N/A#define PUTLINE64 \
1N/A do \
1N/A { \
1N/A if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
1N/A { \
1N/A CHK_EOL; \
1N/A if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1N/A goto writeerr; \
1N/A pxflags &= ~PXLF_NOADDEOL; \
1N/A fbufp = fbuf; \
1N/A } \
1N/A } while (0)
1N/A
1N/A *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
1N/A PUTLINE64;
1N/A if (c3 == '=')
1N/A continue;
1N/A c3 = CHAR64(c3);
1N/A *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
1N/A PUTLINE64;
1N/A if (c4 == '=')
1N/A continue;
1N/A c4 = CHAR64(c4);
1N/A *fbufp = ((c3 & 0x03) << 6) | c4;
1N/A PUTLINE64;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A int off;
1N/A
1N/A /* quoted-printable */
1N/A pxflags |= PXLF_NOADDEOL;
1N/A fbufp = fbuf;
1N/A while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1N/A sizeof(buf)) != NULL)
1N/A {
1N/A off = mime_fromqp((unsigned char *) buf, &fbufp,
1N/A &fbuf[MAXLINE] - fbufp);
1N/Aagain:
1N/A if (off < -1)
1N/A continue;
1N/A
1N/A if (fbufp - fbuf > 0)
1N/A {
1N/A if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1N/A mci, pxflags))
1N/A goto writeerr;
1N/A }
1N/A fbufp = fbuf;
1N/A if (off >= 0 && buf[off] != '\0')
1N/A {
1N/A off = mime_fromqp((unsigned char *) (buf + off),
1N/A &fbufp,
1N/A &fbuf[MAXLINE] - fbufp);
1N/A goto again;
1N/A }
1N/A }
1N/A }
1N/A
1N/A /* force out partial last line */
1N/A if (fbufp > fbuf)
1N/A {
1N/A *fbufp = '\0';
1N/A if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1N/A goto writeerr;
1N/A }
1N/A
1N/A /*
1N/A ** The decoded text may end without an EOL. Since this function
1N/A ** is only called for text/plain MIME messages, it is safe to
1N/A ** add an extra one at the end just in case. This is a hack,
1N/A ** but so is auto-converting MIME in the first place.
1N/A */
1N/A
1N/A if (!putline("", mci))
1N/A goto writeerr;
1N/A
1N/A if (tTd(43, 3))
1N/A sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1N/A return true;
1N/A
1N/A writeerr:
1N/A return false;
1N/A}
1N/A/*
1N/A** The following is based on Borenstein's "codes.c" module, with simplifying
1N/A** changes as we do not deal with multipart, and to do the translation in-core,
1N/A** with an attempt to prevent overrun of output buffers.
1N/A**
1N/A** What is needed here are changes to defend this code better against
1N/A** bad encodings. Questionable to always return 0xFF for bad mappings.
1N/A*/
1N/A
1N/Astatic char index_hex[128] =
1N/A{
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
1N/A -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
1N/A -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
1N/A};
1N/A
1N/A# define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
1N/A
1N/A/*
1N/A** MIME_FROMQP -- decode quoted printable string
1N/A**
1N/A** Parameters:
1N/A** infile -- input (encoded) string
1N/A** outfile -- output string
1N/A** maxlen -- size of output buffer
1N/A**
1N/A** Returns:
1N/A** -2 if decoding failure
1N/A** -1 if infile completely decoded into outfile
1N/A** >= 0 is the position in infile decoding
1N/A** reached before maxlen was reached
1N/A*/
1N/A
1N/Astatic int
1N/Amime_fromqp(infile, outfile, maxlen)
1N/A unsigned char *infile;
1N/A unsigned char **outfile;
1N/A int maxlen; /* Max # of chars allowed in outfile */
1N/A{
1N/A int c1, c2;
1N/A int nchar = 0;
1N/A unsigned char *b;
1N/A
1N/A /* decrement by one for trailing '\0', at least one other char */
1N/A if (--maxlen < 1)
1N/A return 0;
1N/A
1N/A b = infile;
1N/A while ((c1 = *infile++) != '\0' && nchar < maxlen)
1N/A {
1N/A if (c1 == '=')
1N/A {
1N/A if ((c1 = *infile++) == '\0')
1N/A break;
1N/A
1N/A if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
1N/A {
1N/A /* ignore it and the rest of the buffer */
1N/A return -2;
1N/A }
1N/A else
1N/A {
1N/A do
1N/A {
1N/A if ((c2 = *infile++) == '\0')
1N/A {
1N/A c2 = -1;
1N/A break;
1N/A }
1N/A } while ((c2 = HEXCHAR(c2)) == -1);
1N/A
1N/A if (c2 == -1)
1N/A break;
1N/A nchar++;
1N/A *(*outfile)++ = c1 << 4 | c2;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A nchar++;
1N/A *(*outfile)++ = c1;
1N/A if (c1 == '\n')
1N/A break;
1N/A }
1N/A }
1N/A *(*outfile)++ = '\0';
1N/A if (nchar >= maxlen)
1N/A return (infile - b - 1);
1N/A return -1;
1N/A}
1N/A#endif /* MIME7TO8 */