📄 mime.c
字号:
}
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';
putline(buf, mci);
linelen = fromstate = 0;
bp = buf;
if (c2 == '.')
{
*bp++ = '.';
linelen++;
}
}
if (bitnset(c1 & 0xff, 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;
}
/* output any saved character */
if (c2 == ' ' || c2 == '\t')
{
*bp++ = '=';
*bp++ = Base16Code[(c2 >> 4) & 0x0f];
*bp++ = Base16Code[c2 & 0x0f];
linelen += 3;
}
if (linelen > 0 || boundaries[0] != NULL)
{
*bp = '\0';
putline(buf, mci);
}
}
if (tTd(43, 3))
dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
return bt;
}
/*
** MIME_GETCHAR -- get a character for MIME processing
**
** Treats boundaries as EOF.
**
** Parameters:
** fp -- the input file.
** boundaries -- the current MIME boundaries.
** btp -- if the return value is EOF, *btp is set to
** the type of the boundary.
**
** Returns:
** The next character in the input stream.
*/
static int
mime_getchar(fp, boundaries, btp)
register FILE *fp;
char **boundaries;
int *btp;
{
int c;
static u_char *bp = NULL;
static int buflen = 0;
static bool atbol = TRUE; /* at beginning of line */
static int bt = MBT_SYNTAX; /* boundary type of next EOF */
static u_char buf[128]; /* need not be a full line */
int start = 0; /* indicates position of - in buffer */
if (buflen == 1 && *bp == '\n')
{
/* last \n in buffer may be part of next MIME boundary */
c = *bp;
}
else if (buflen > 0)
{
buflen--;
return *bp++;
}
else
c = getc(fp);
bp = buf;
buflen = 0;
if (c == '\n')
{
/* might be part of a MIME boundary */
*bp++ = c;
atbol = TRUE;
c = getc(fp);
if (c == '\n')
{
(void) ungetc(c, fp);
return c;
}
start = 1;
}
if (c != EOF)
*bp++ = c;
else
bt = MBT_FINAL;
if (atbol && c == '-')
{
/* check for a message boundary */
c = getc(fp);
if (c != '-')
{
if (c != EOF)
*bp++ = c;
else
bt = MBT_FINAL;
buflen = bp - buf - 1;
bp = buf;
return *bp++;
}
/* got "--", now check for rest of separator */
*bp++ = '-';
while (bp < &buf[sizeof buf - 2] &&
(c = getc(fp)) != EOF && c != '\n')
{
*bp++ = c;
}
*bp = '\0';
bt = mimeboundary((char *) &buf[start], boundaries);
switch (bt)
{
case MBT_FINAL:
case MBT_INTERMED:
/* we have a message boundary */
buflen = 0;
*btp = bt;
return EOF;
}
atbol = c == '\n';
if (c != EOF)
*bp++ = c;
}
buflen = bp - buf - 1;
if (buflen < 0)
{
*btp = bt;
return EOF;
}
bp = buf;
return *bp++;
}
/*
** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
**
** Parameters:
** fp -- the input file.
** boundaries -- the current MIME boundaries.
** btp -- if the return value is EOF, *btp is set to
** the type of the boundary.
**
** Returns:
** The next character in the input stream.
*/
static int
mime_getchar_crlf(fp, boundaries, btp)
register FILE *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;
}
/*
** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
**
** Parameters:
** line -- the input line.
** boundaries -- the set of currently pending boundaries.
**
** Returns:
** MBT_NOTSEP -- if this is not a separator line
** MBT_INTERMED -- if this is an intermediate separator
** MBT_FINAL -- if this is a final boundary
** MBT_SYNTAX -- if this is a boundary for the wrong
** enclosure -- i.e., a syntax error.
*/
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 (line[i - 1] == '\n')
i--;
/* strip off trailing whitespace */
while (line[i - 1] == ' ' || line[i - 1] == '\t')
i--;
savec = line[i];
line[i] = '\0';
if (tTd(43, 5))
dprintf("mimeboundary: line=\"%s\"... ", line);
/* check for this as an intermediate boundary */
if (isboundary(&line[2], boundaries) >= 0)
type = MBT_INTERMED;
else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
{
/* check for a final boundary */
line[i - 2] = '\0';
if (isboundary(&line[2], boundaries) >= 0)
type = MBT_FINAL;
line[i - 2] = '-';
}
line[i] = savec;
if (tTd(43, 5))
dprintf("%s\n", MimeBoundaryNames[type]);
return type;
}
/*
** DEFCHARSET -- return default character set for message
**
** The first choice for character set is for the mailer
** corresponding to the envelope sender. If neither that
** nor the global configuration file has a default character
** set defined, return "unknown-8bit" as recommended by
** RFC 1428 section 3.
**
** Parameters:
** e -- the envelope for this message.
**
** Returns:
** The default character set for that mailer.
*/
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";
}
/*
** ISBOUNDARY -- is a given string a currently valid boundary?
**
** Parameters:
** line -- the current input line.
** boundaries -- the list of valid boundaries.
**
** Returns:
** The index number in boundaries if the line is found.
** -1 -- otherwise.
**
*/
static int
isboundary(line, boundaries)
char *line;
char **boundaries;
{
register int i;
for (i = 0; boundaries[i] != NULL; i++)
{
if (strcmp(line, boundaries[i]) == 0)
return i;
}
return -1;
}
#endif /* MIME8TO7 */
#if MIME7TO8
/*
** MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
**
** This is a hack. Supports translating the two 7-bit body-encodings
** (quoted-printable and base64) to 8-bit coded bodies.
**
** There is not much point in supporting multipart here, as the UA
** will be able to deal with encoded MIME bodies if it can parse MIME
** multipart messages.
**
** Note also that we wont be called unless it is a text/plain MIME
** message, encoded base64 or QP and mailer flag '9' has been defined
** on mailer.
**
** Contributed by Marius Olaffson <marius@rhi.hi.is>.
**
** Parameters:
** mci -- mailer connection information.
** header -- the header for this body part.
** e -- envelope.
**
** Returns:
** none.
*/
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)])
void
mime7to8(mci, header, e)
register MCI *mci;
HDR *header;
register ENVELOPE *e;
{
register char *p;
char *cte;
char **pvp;
u_char *fbufp;
char buf[MAXLINE];
u_char fbuf[MAXLINE + 1];
char pvpbuf[MAXLINE];
extern u_char MimeTokenTab[256];
p = hvalue("Content-Transfer-Encoding", header);
if (p == NULL ||
(pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL,
MimeTokenTab)) == NULL ||
pvp[0] == NULL)
{
/* "can't happen" -- upper level should have caught this */
syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
/* avoid bounce loops */
e->e_flags |= EF_DONT_MIME;
/* cheap failsafe algorithm -- should work on text/plain */
if (p != NULL)
{
snprintf(buf, sizeof buf,
"Content-Transfer-Encoding: %s", p);
putline(buf, mci);
}
putline("", mci);
mci->mci_flags &= ~MCIF_INHEADER;
while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
putline(buf, mci);
return;
}
cataddr(pvp, NULL, buf, sizeof buf, '\0');
cte = newstr(buf);
mci->mci_flags |= MCIF_INHEADER;
putline("Content-Transfer-Encoding: 8bit", mci);
snprintf(buf, sizeof buf,
"X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
cte, MyHostName, e->e_id);
putline(buf, mci);
putline("", mci);
mci->mci_flags &= ~MCIF_INHEADER;
/*
** Translate body encoding to 8-bit. Supports two types of
** encodings; "base64" and "quoted-printable". Assume qp if
** it is not base64.
*/
if (strcasecmp(cte, "base64") == 0)
{
int c1, c2, c3, c4;
fbufp = fbuf;
while ((c1 = fgetc(e->e_dfp)) != EOF)
{
if (isascii(c1) && isspace(c1))
continue;
do
{
c2 = fgetc(e->e_dfp);
} while (isascii(c2) && isspace(c2));
if (c2 == EOF)
break;
do
{
c3 = fgetc(e->e_dfp);
} while (isascii(c3) && isspace(c3));
if (c3 == EOF)
break;
do
{
c4 = fgetc(e->e_dfp);
} while (isascii(c4) && isspace(c4));
if (c4 == EOF)
break;
if (c1 == '=' || c2 == '=')
continue;
c1 = CHAR64(c1);
c2 = CHAR64(c2);
*fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
{
if (*--fbufp != '\n' ||
(fbufp > fbuf && *--fbufp != '\r'))
fbufp++;
putxline((char *) fbuf, fbufp - fbuf,
mci, PXLF_MAPFROM);
fbufp = fbuf;
}
if (c3 == '=')
continue;
c3 = CHAR64(c3);
*fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
{
if (*--fbufp != '\n' ||
(fbufp > fbuf && *--fbufp != '\r'))
fbufp++;
putxline((char *) fbuf, fbufp - fbuf,
mci, PXLF_MAPFROM);
fbufp = fbuf;
}
if (c4 == '=')
continue;
c4 = CHAR64(c4);
*fbufp = ((c3 & 0x03) << 6) | c4;
if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])
{
if (*--fbufp != '\n' ||
(fbufp > fbuf && *--fbufp != '\r'))
fbufp++;
putxline((char *) fbuf, fbufp - fbuf,
mci, PXLF_MAPFROM);
fbufp = fbuf;
}
}
}
else
{
/* quoted-printable */
fbufp = fbuf;
while (fgets(buf, sizeof buf, e->e_dfp) != NULL)
{
if (mime_fromqp((u_char *) buf, &fbufp, 0,
&fbuf[MAXLINE] - fbufp) == 0)
continue;
if (fbufp - fbuf > 0)
putxline((char *) fbuf, fbufp - fbuf - 1, mci,
PXLF_MAPFROM);
fbufp = fbuf;
}
}
/* force out partial last line */
if (fbufp > fbuf)
{
*fbufp = '\0';
putxline((char *) fbuf, fbufp - fbuf, mci, PXLF_MAPFROM);
}
if (tTd(43, 3))
dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
}
/*
** The following is based on Borenstein's "codes.c" module, with simplifying
** changes as we do not deal with multipart, and to do the translation in-core,
** with an attempt to prevent overrun of output buffers.
**
** What is needed here are changes to defned this code better against
** bad encodings. Questionable to always return 0xFF for bad mappings.
*/
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, state, maxlen)
u_char *infile;
u_char **outfile;
int state; /* Decoding body (0) or header (1) */
int maxlen; /* Max # of chars allowed in outfile */
{
int c1, c2;
int nchar = 0;
while ((c1 = *infile++) != '\0')
{
if (c1 == '=')
{
if ((c1 = *infile++) == 0)
break;
if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
{
/* ignore it */
if (state == 0)
return 0;
}
else
{
do
{
if ((c2 = *infile++) == '\0')
{
c2 = -1;
break;
}
} while ((c2 = HEXCHAR(c2)) == -1);
if (c2 == -1 || ++nchar > maxlen)
break;
*(*outfile)++ = c1 << 4 | c2;
}
}
else
{
if (state == 1 && c1 == '_')
c1 = ' ';
if (++nchar > maxlen)
break;
*(*outfile)++ = c1;
if (c1 == '\n')
break;
}
}
*(*outfile)++ = '\0';
return 1;
}
#endif /* MIME7TO8 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -