#include <sendmail.h>
#include <string.h>
SM_RCSID("@(#)$Id: mime.c,v 8.147 2007/09/26 23:29:11 ca Exp $")
#ifndef MIME7TO8_OLD
# define MIME7TO8_OLD 1
#endif
#if MIME8TO7
static int isboundary __P((char *, char **));
static int mimeboundary __P((char *, char **));
static int mime_getchar __P((SM_FILE_T *, char **, int *));
static int mime_getchar_crlf __P((SM_FILE_T *, char **, int *));
static char Base16Code[] = "0123456789ABCDEF";
static char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
# define MBT_SYNTAX 0
# define MBT_NOTSEP 1
# define MBT_INTERMED 2
# define MBT_FINAL 3
static char *MimeBoundaryNames[] =
{
"SYNTAX", "NOTSEP", "INTERMED", "FINAL"
};
static bool MapNLtoCRLF;
struct args
{
char *a_field;
char *a_value;
};
int
mime8to7(mci, header, e, boundaries, flags, level)
register MCI *mci;
HDR *header;
register ENVELOPE *e;
char **boundaries;
int flags;
int level;
{
register char *p;
int linelen;
int bt;
off_t offset;
size_t sectionsize, sectionhighbits;
int i;
char *type;
char *subtype;
char *cte;
char **pvp;
int argc = 0;
char *bp;
bool use_qp = false;
struct args argv[MAXMIMEARGS];
char bbuf[128];
char buf[MAXLINE];
char pvpbuf[MAXLINE];
extern unsigned char MimeTokenTab[256];
if (level > MAXMIMENESTING)
{
if (!bitset(EF_TOODEEP, e->e_flags))
{
if (tTd(43, 4))
sm_dprintf("mime8to7: too deep, level=%d\n",
level);
usrerr("mime8to7: recursion level %d exceeded",
level);
e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
}
}
if (tTd(43, 1))
{
sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
if (boundaries[0] == NULL)
sm_dprintf(" <none>");
else
{
for (i = 0; boundaries[i] != NULL; i++)
sm_dprintf(" %s", boundaries[i]);
}
sm_dprintf("\n");
}
MapNLtoCRLF = true;
p = hvalue("Content-Transfer-Encoding", header);
if (p == NULL ||
(pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
MimeTokenTab, false)) == NULL ||
pvp[0] == NULL)
{
cte = NULL;
}
else
{
cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
cte = sm_rpool_strdup_x(e->e_rpool, buf);
}
type = subtype = NULL;
p = hvalue("Content-Type", header);
if (p == NULL)
{
if (bitset(M87F_DIGEST, flags))
p = "message/rfc822";
else
p = "text/plain";
}
if (p != NULL &&
(pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
MimeTokenTab, false)) != NULL &&
pvp[0] != NULL)
{
if (tTd(43, 40))
{
for (i = 0; pvp[i] != NULL; i++)
sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
}
type = *pvp++;
if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
*++pvp != NULL)
{
subtype = *pvp++;
}
while (*pvp != NULL && argc < MAXMIMEARGS)
{
while (*pvp != NULL && strcmp(*pvp, ";") != 0)
pvp++;
if (*pvp++ == NULL || *pvp == NULL)
break;
if (strcmp(*pvp, ";") == 0)
{
usrerr("mime8to7: Empty parameter in Content-Type header");
e->e_flags |= EF_DONT_MIME;
continue;
}
argv[argc].a_field = *pvp++;
if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
(*++pvp == NULL || strcmp(*pvp, ";") != 0))
{
argv[argc].a_value = *pvp;
argc++;
}
}
}
if (type == NULL)
type = "-none-";
if (subtype == NULL)
subtype = "-none-";
flags &= ~M87F_DIGEST;
(void) sm_snprintf(buf, sizeof(buf), "%.100s/%.100s", type, subtype);
if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
flags |= M87F_NO8BIT;
# ifdef USE_B_CLASS
if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
MapNLtoCRLF = false;
# endif
if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
use_qp = true;
if (sm_strcasecmp(type, "multipart") == 0 &&
(!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)) &&
!bitset(EF_TOODEEP, e->e_flags)
)
{
if (sm_strcasecmp(subtype, "digest") == 0)
flags |= M87F_DIGEST;
for (i = 0; i < argc; i++)
{
if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
break;
}
if (i >= argc || argv[i].a_value == NULL)
{
usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
i >= argc ? "missing" : "bogus", p);
p = "---";
e->e_flags |= EF_DONT_MIME;
}
else
{
p = argv[i].a_value;
stripquotes(p);
}
if (sm_strlcpy(bbuf, p, sizeof(bbuf)) >= sizeof(bbuf))
{
usrerr("mime8to7: multipart boundary \"%s\" too long",
p);
e->e_flags |= EF_DONT_MIME;
}
if (tTd(43, 1))
sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
bbuf);
for (i = 0; i < MAXMIMENESTING; i++)
{
if (boundaries[i] == NULL)
break;
}
if (i >= MAXMIMENESTING)
{
if (tTd(43, 4))
sm_dprintf("mime8to7: too deep, i=%d\n", i);
if (!bitset(EF_TOODEEP, e->e_flags))
usrerr("mime8to7: multipart nesting boundary too deep");
e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
}
else
{
boundaries[i] = bbuf;
boundaries[i + 1] = NULL;
}
mci->mci_flags |= MCIF_INMIME;
if (!putline("", mci))
goto writeerr;
mci->mci_flags &= ~MCIF_INHEADER;
bt = MBT_FINAL;
while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
!= NULL)
{
bt = mimeboundary(buf, boundaries);
if (bt != MBT_NOTSEP)
break;
if (!putxline(buf, strlen(buf), mci,
PXLF_MAPFROM|PXLF_STRIP8BIT))
goto writeerr;
if (tTd(43, 99))
sm_dprintf(" ...%s", buf);
}
if (sm_io_eof(e->e_dfp))
bt = MBT_FINAL;
while (bt != MBT_FINAL)
{
auto HDR *hdr = NULL;
(void) sm_strlcpyn(buf, sizeof(buf), 2, "--", bbuf);
if (!putline(buf, mci))
goto writeerr;
if (tTd(43, 35))
sm_dprintf(" ...%s\n", buf);
collect(e->e_dfp, false, &hdr, e, false);
if (tTd(43, 101))
putline("+++after collect", mci);
if (!putheader(mci, hdr, e, flags))
goto writeerr;
if (tTd(43, 101))
putline("+++after putheader", mci);
bt = mime8to7(mci, hdr, e, boundaries, flags,
level + 1);
if (bt == SM_IO_EOF)
goto writeerr;
}
(void) sm_strlcpyn(buf, sizeof(buf), 3, "--", bbuf, "--");
if (!putline(buf, mci))
goto writeerr;
if (tTd(43, 35))
sm_dprintf(" ...%s\n", buf);
boundaries[i] = NULL;
mci->mci_flags &= ~MCIF_INMIME;
while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
!= NULL)
{
bt = mimeboundary(buf, boundaries);
if (bt != MBT_NOTSEP)
break;
if (!putxline(buf, strlen(buf), mci,
PXLF_MAPFROM|PXLF_STRIP8BIT))
goto writeerr;
if (tTd(43, 99))
sm_dprintf(" ...%s", buf);
}
if (sm_io_eof(e->e_dfp))
bt = MBT_FINAL;
if (tTd(43, 3))
sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
MimeBoundaryNames[bt]);
return bt;
}
if (sm_strcasecmp(type, "message") == 0)
{
if (!wordinclass(subtype, 's') ||
bitset(EF_TOODEEP, e->e_flags))
{
flags |= M87F_NO8BIT;
}
else
{
auto HDR *hdr = NULL;
if (!putline("", mci))
goto writeerr;
mci->mci_flags |= MCIF_INMIME;
collect(e->e_dfp, false, &hdr, e, false);
if (tTd(43, 101))
putline("+++after collect", mci);
if (!putheader(mci, hdr, e, flags))
goto writeerr;
if (tTd(43, 101))
putline("+++after putheader", mci);
if (hvalue("MIME-Version", hdr) == NULL &&
!bitset(M87F_NO8TO7, flags) &&
!putline("MIME-Version: 1.0", mci))
goto writeerr;
bt = mime8to7(mci, hdr, e, boundaries, flags,
level + 1);
mci->mci_flags &= ~MCIF_INMIME;
return bt;
}
}
sectionsize = sectionhighbits = 0;
if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
{
offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
if (offset == -1)
syserr("mime8to7: cannot sm_io_tell on %cf%s",
DATAFL_LETTER, e->e_id);
while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
!= NULL)
{
if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
break;
for (p = buf; *p != '\0'; p++)
{
sectionsize++;
if (bitset(0200, *p))
sectionhighbits++;
}
if (sectionsize >= 4096 &&
sectionhighbits > sectionsize / 4)
break;
}
if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
syserr("mime8to7: cannot sm_io_fseek on %cf%s",
DATAFL_LETTER, e->e_id);
else
sm_io_clearerr(e->e_dfp);
}
if (tTd(43, 8))
{
sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
(long) sectionhighbits, (long) sectionsize,
cte == NULL ? "[none]" : cte,
type == NULL ? "[none]" : type,
subtype == NULL ? "[none]" : subtype);
}
if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
sectionsize = sectionhighbits;
linelen = 0;
bp = buf;
if (sectionhighbits == 0)
{
if (cte != NULL &&
bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
mci->mci_flags) &&
!bitset(M87F_NO8TO7, flags))
{
(void) sm_snprintf(buf, sizeof(buf),
"Content-Transfer-Encoding: %.200s", cte);
if (!putline(buf, mci))
goto writeerr;
if (tTd(43, 36))
sm_dprintf(" ...%s\n", buf);
}
if (!putline("", mci))
goto writeerr;
mci->mci_flags &= ~MCIF_INHEADER;
while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
!= NULL)
{
if (!bitset(MCIF_INLONGLINE, mci->mci_flags))
{
bt = mimeboundary(buf, boundaries);
if (bt != MBT_NOTSEP)
break;
}
if (!putxline(buf, strlen(buf), mci,
PXLF_MAPFROM|PXLF_NOADDEOL))
goto writeerr;
}
if (sm_io_eof(e->e_dfp))
bt = MBT_FINAL;
}
else if (!MapNLtoCRLF ||
(sectionsize / 8 < sectionhighbits && !use_qp))
{
int c1, c2;
if (tTd(43, 36))
sm_dprintf(" ...Content-Transfer-Encoding: base64\n");
if (!putline("Content-Transfer-Encoding: base64", mci))
goto writeerr;
(void) sm_snprintf(buf, sizeof(buf),
"X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
MyHostName, e->e_id);
if (!putline(buf, mci) || !putline("", mci))
goto writeerr;
mci->mci_flags &= ~MCIF_INHEADER;
while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
SM_IO_EOF)
{
if (linelen > 71)
{
*bp = '\0';
if (!putline(buf, mci))
goto writeerr;
linelen = 0;
bp = buf;
}
linelen += 4;
*bp++ = Base64Code[(c1 >> 2)];
c1 = (c1 & 0x03) << 4;
c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
if (c2 == SM_IO_EOF)
{
*bp++ = Base64Code[c1];
*bp++ = '=';
*bp++ = '=';
break;
}
c1 |= (c2 >> 4) & 0x0f;
*bp++ = Base64Code[c1];
c1 = (c2 & 0x0f) << 2;
c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
if (c2 == SM_IO_EOF)
{
*bp++ = Base64Code[c1];
*bp++ = '=';
break;
}
c1 |= (c2 >> 6) & 0x03;
*bp++ = Base64Code[c1];
*bp++ = Base64Code[c2 & 0x3f];
}
*bp = '\0';
if (!putline(buf, mci))
goto writeerr;
}
else
{
int c1, c2;
int fromstate;
BITMAP256 badchars;
clrbitmap(badchars);
for (c1 = 0x00; c1 < 0x20; c1++)
setbitn(c1, badchars);
clrbitn('\t', badchars);
for (c1 = 0x7f; c1 < 0x100; c1++)
setbitn(c1, badchars);
setbitn('=', badchars);
if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
setbitn(*p, badchars);
if (tTd(43, 36))
sm_dprintf(" ...Content-Transfer-Encoding: quoted-printable\n");
if (!putline("Content-Transfer-Encoding: quoted-printable",
mci))
goto writeerr;
(void) sm_snprintf(buf, sizeof(buf),
"X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
MyHostName, e->e_id);
if (!putline(buf, mci) || !putline("", mci))
goto writeerr;
mci->mci_flags &= ~MCIF_INHEADER;
fromstate = 0;
c2 = '\n';
while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
SM_IO_EOF)
{
if (c1 == '\n')
{
if (c2 == ' ' || c2 == '\t')
{
*bp++ = '=';
*bp++ = Base16Code[(c2 >> 4) & 0x0f];
*bp++ = Base16Code[c2 & 0x0f];
}
if (buf[0] == '.' && bp == &buf[1])
{
buf[0] = '=';
*bp++ = Base16Code[('.' >> 4) & 0x0f];
*bp++ = Base16Code['.' & 0x0f];
}
*bp = '\0';
if (!putline(buf, mci))
goto writeerr;
linelen = fromstate = 0;
bp = buf;
c2 = c1;
continue;
}
if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
{
*bp++ = '=';
*bp++ = '2';
*bp++ = '0';
linelen += 3;
}
else if (c2 == ' ' || c2 == '\t')
{
*bp++ = c2;
linelen++;
}
if (linelen > 72 &&
(linelen > 75 || c1 != '.' ||
(linelen > 73 && c2 == '.')))
{
if (linelen > 73 && c2 == '.')
bp--;
else
c2 = '\n';
*bp++ = '=';
*bp = '\0';
if (!putline(buf, mci))
goto writeerr;
linelen = fromstate = 0;
bp = buf;
if (c2 == '.')
{
*bp++ = '.';
linelen++;
}
}
if (bitnset(bitidx(c1), badchars))
{
*bp++ = '=';
*bp++ = Base16Code[(c1 >> 4) & 0x0f];
*bp++ = Base16Code[c1 & 0x0f];
linelen += 3;
}
else if (c1 != ' ' && c1 != '\t')
{
if (linelen < 4 && c1 == "From"[linelen])
fromstate++;
*bp++ = c1;
linelen++;
}
c2 = c1;
}
if (c2 == ' ' || c2 == '\t')
{
*bp++ = '=';
*bp++ = Base16Code[(c2 >> 4) & 0x0f];
*bp++ = Base16Code[c2 & 0x0f];
linelen += 3;
}
if (linelen > 0 || boundaries[0] != NULL)
{
*bp = '\0';
if (!putline(buf, mci))
goto writeerr;
}
}
if (tTd(43, 3))
sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
return bt;
writeerr:
return SM_IO_EOF;
}
static int
mime_getchar(fp, boundaries, btp)
register SM_FILE_T *fp;
char **boundaries;
int *btp;
{
int c;
static unsigned char *bp = NULL;
static int buflen = 0;
static bool atbol = true;
static int bt = MBT_SYNTAX;
static unsigned char buf[128];
int start = 0;
if (buflen == 1 && *bp == '\n')
{
c = *bp;
}
else if (buflen > 0)
{
buflen--;
return *bp++;
}
else
c = sm_io_getc(fp, SM_TIME_DEFAULT);
bp = buf;
buflen = 0;
if (c == '\n')
{
*bp++ = c;
atbol = true;
c = sm_io_getc(fp, SM_TIME_DEFAULT);
if (c == '\n')
{
(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
return c;
}
start = 1;
}
if (c != SM_IO_EOF)
*bp++ = c;
else
bt = MBT_FINAL;
if (atbol && c == '-')
{
c = sm_io_getc(fp, SM_TIME_DEFAULT);
if (c != '-')
{
if (c != SM_IO_EOF)
*bp++ = c;
else
bt = MBT_FINAL;
buflen = bp - buf - 1;
bp = buf;
return *bp++;
}
*bp++ = '-';
while (bp < &buf[sizeof(buf) - 2] &&
(c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
c != '\n')
{
*bp++ = c;
}
*bp = '\0';
bt = mimeboundary((char *) &buf[start], boundaries);
switch (bt)
{
case MBT_FINAL:
case MBT_INTERMED:
buflen = 0;
*btp = bt;
return SM_IO_EOF;
}
if (bp < &buf[sizeof(buf) - 2] && c != SM_IO_EOF)
*bp++ = c;
}
atbol = c == '\n';
buflen = bp - buf - 1;
if (buflen < 0)
{
*btp = bt;
return SM_IO_EOF;
}
bp = buf;
return *bp++;
}
static int
mime_getchar_crlf(fp, boundaries, btp)
register SM_FILE_T *fp;
char **boundaries;
int *btp;
{
static bool sendlf = false;
int c;
if (sendlf)
{
sendlf = false;
return '\n';
}
c = mime_getchar(fp, boundaries, btp);
if (c == '\n' && MapNLtoCRLF)
{
sendlf = true;
return '\r';
}
return c;
}
static int
mimeboundary(line, boundaries)
register char *line;
char **boundaries;
{
int type = MBT_NOTSEP;
int i;
int savec;
if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
return MBT_NOTSEP;
i = strlen(line);
if (i > 0 && line[i - 1] == '\n')
i--;
while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
#if _FFR_MIME_CR_OK
|| line[i - 1] == '\r'
#endif
))
i--;
savec = line[i];
line[i] = '\0';
if (tTd(43, 5))
sm_dprintf("mimeboundary: line=\"%s\"... ", line);
if (isboundary(&line[2], boundaries) >= 0)
type = MBT_INTERMED;
else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
{
line[i - 2] = '\0';
if (isboundary(&line[2], boundaries) >= 0)
type = MBT_FINAL;
line[i - 2] = '-';
}
line[i] = savec;
if (tTd(43, 5))
sm_dprintf("%s\n", MimeBoundaryNames[type]);
return type;
}
char *
defcharset(e)
register ENVELOPE *e;
{
if (e != NULL && e->e_from.q_mailer != NULL &&
e->e_from.q_mailer->m_defcharset != NULL)
return e->e_from.q_mailer->m_defcharset;
if (DefaultCharSet != NULL)
return DefaultCharSet;
return "unknown-8bit";
}
static int
isboundary(line, boundaries)
char *line;
char **boundaries;
{
register int i;
for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
{
if (strcmp(line, boundaries[i]) == 0)
return i;
}
return -1;
}
#endif
#if MIME7TO8
static int mime_fromqp __P((unsigned char *, unsigned char **, int));
static char index_64[128] =
{
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
};
# define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
bool
mime7to8(mci, header, e)
register MCI *mci;
HDR *header;
register ENVELOPE *e;
{
int pxflags;
register char *p;
char *cte;
char **pvp;
unsigned char *fbufp;
char buf[MAXLINE];
unsigned char fbuf[MAXLINE + 1];
char pvpbuf[MAXLINE];
extern unsigned char MimeTokenTab[256];
p = hvalue("Content-Transfer-Encoding", header);
if (p == NULL ||
(pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
MimeTokenTab, false)) == NULL ||
pvp[0] == NULL)
{
syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
e->e_flags |= EF_DONT_MIME;
if (p != NULL)
{
(void) sm_snprintf(buf, sizeof(buf),
"Content-Transfer-Encoding: %s", p);
if (!putline(buf, mci))
goto writeerr;
}
if (!putline("", mci))
goto writeerr;
mci->mci_flags &= ~MCIF_INHEADER;
while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
!= NULL)
{
if (!putline(buf, mci))
goto writeerr;
}
return true;
}
cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
cte = sm_rpool_strdup_x(e->e_rpool, buf);
mci->mci_flags |= MCIF_INHEADER;
if (!putline("Content-Transfer-Encoding: 8bit", mci))
goto writeerr;
(void) sm_snprintf(buf, sizeof(buf),
"X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
cte, MyHostName, e->e_id);
if (!putline(buf, mci) || !putline("", mci))
goto writeerr;
mci->mci_flags &= ~MCIF_INHEADER;
pxflags = PXLF_MAPFROM;
if (sm_strcasecmp(cte, "base64") == 0)
{
int c1, c2, c3, c4;
fbufp = fbuf;
while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
SM_IO_EOF)
{
if (isascii(c1) && isspace(c1))
continue;
do
{
c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
} while (isascii(c2) && isspace(c2));
if (c2 == SM_IO_EOF)
break;
do
{
c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
} while (isascii(c3) && isspace(c3));
if (c3 == SM_IO_EOF)
break;
do
{
c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
} while (isascii(c4) && isspace(c4));
if (c4 == SM_IO_EOF)
break;
if (c1 == '=' || c2 == '=')
continue;
c1 = CHAR64(c1);
c2 = CHAR64(c2);
#if MIME7TO8_OLD
#define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
++fbufp;
#else
#define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
{ \
++fbufp; \
pxflags |= PXLF_NOADDEOL; \
}
#endif
#define PUTLINE64 \
do \
{ \
if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE]) \
{ \
CHK_EOL; \
if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
goto writeerr; \
pxflags &= ~PXLF_NOADDEOL; \
fbufp = fbuf; \
} \
} while (0)
*fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
PUTLINE64;
if (c3 == '=')
continue;
c3 = CHAR64(c3);
*fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
PUTLINE64;
if (c4 == '=')
continue;
c4 = CHAR64(c4);
*fbufp = ((c3 & 0x03) << 6) | c4;
PUTLINE64;
}
}
else
{
int off;
pxflags |= PXLF_NOADDEOL;
fbufp = fbuf;
while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
sizeof(buf)) != NULL)
{
off = mime_fromqp((unsigned char *) buf, &fbufp,
&fbuf[MAXLINE] - fbufp);
again:
if (off < -1)
continue;
if (fbufp - fbuf > 0)
{
if (!putxline((char *) fbuf, fbufp - fbuf - 1,
mci, pxflags))
goto writeerr;
}
fbufp = fbuf;
if (off >= 0 && buf[off] != '\0')
{
off = mime_fromqp((unsigned char *) (buf + off),
&fbufp,
&fbuf[MAXLINE] - fbufp);
goto again;
}
}
}
if (fbufp > fbuf)
{
*fbufp = '\0';
if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
goto writeerr;
}
if (!putline("", mci))
goto writeerr;
if (tTd(43, 3))
sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
return true;
writeerr:
return false;
}
static char index_hex[128] =
{
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
};
# define HEXCHAR(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
static int
mime_fromqp(infile, outfile, maxlen)
unsigned char *infile;
unsigned char **outfile;
int maxlen;
{
int c1, c2;
int nchar = 0;
unsigned char *b;
if (--maxlen < 1)
return 0;
b = infile;
while ((c1 = *infile++) != '\0' && nchar < maxlen)
{
if (c1 == '=')
{
if ((c1 = *infile++) == '\0')
break;
if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
{
return -2;
}
else
{
do
{
if ((c2 = *infile++) == '\0')
{
c2 = -1;
break;
}
} while ((c2 = HEXCHAR(c2)) == -1);
if (c2 == -1)
break;
nchar++;
*(*outfile)++ = c1 << 4 | c2;
}
}
else
{
nchar++;
*(*outfile)++ = c1;
if (c1 == '\n')
break;
}
}
*(*outfile)++ = '\0';
if (nchar >= maxlen)
return (infile - b - 1);
return -1;
}
#endif