#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "fetchmail.h"
#include "i18n.h"
static unsigned char unhex(unsigned char c)
{
if ((c >= '0') && (c <= '9'))
return (c - '0');
else if ((c >= 'A') && (c <= 'F'))
return (c - 'A' + 10);
else if ((c >= 'a') && (c <= 'f'))
return (c - 'a' + 10);
else
return 16;
}
static int qp_char(unsigned char c1, unsigned char c2, unsigned char *c_out)
{
c1 = unhex(c1);
c2 = unhex(c2);
if ((c1 > 15) || (c2 > 15))
return 1;
else {
*c_out = 16*c1+c2;
return 0;
}
}
#define S_COPY_PLAIN 0
#define S_SKIP_MIMEINIT 1
#define S_COPY_MIME 2
static const char MIMEHDR_INIT[] = "=?";
static const char MIMEHDR_END[] = "?=";
void UnMimeHeader(unsigned char *hdr)
{
int state = S_COPY_PLAIN;
unsigned char *p_in, *p_out, *p;
unsigned char enc = '\0';
int i;
p = strstr(hdr, MIMEHDR_INIT);
if (p == NULL)
return;
for (p_out = p_in = hdr; (*p_in); ) {
switch (state) {
case S_COPY_PLAIN:
p = strstr(p_in, MIMEHDR_INIT);
if (p == NULL) {
i = strlen(p_in);
memmove(p_out, p_in, i);
p_in += i; p_out += i;
}
else {
if (p > p_in) {
i = (p - p_in);
memmove(p_out, p_in, i);
p_out += i;
}
p_in = (p + 2);
state = S_SKIP_MIMEINIT;
}
break;
case S_SKIP_MIMEINIT:
p = strchr(p_in, '?');
if (p != NULL) {
if (*(p+2) == '?') {
enc = tolower(*(p+1));
p_in = p+3;
state = S_COPY_MIME;
}
else
state = S_COPY_PLAIN;
}
else
state = S_COPY_PLAIN;
break;
case S_COPY_MIME:
p = strstr(p_in, MIMEHDR_END);
if (p == NULL) p = p_in + strlen(p_in);
for (; (p_in < p); ) {
if (enc == 'q') {
if (*p_in == '=') {
if (qp_char(*(p_in+1), *(p_in+2), p_out) == 0)
p_in += 3;
else {
*p_out = *p_in;
p_in++;
}
}
else if (*p_in == '_') {
*p_out = 0x20;
p_in++;
}
else {
*p_out = *p_in;
p_in++;
}
p_out++;
}
else if (enc == 'b') {
char delimsave;
int decoded_count;
delimsave = *p; *p = '\r';
decoded_count = from64tobits(p_out, p_in, 0);
*p = delimsave;
if (decoded_count > 0)
p_out += decoded_count;
p_in = p;
}
else {
*p_out = *p_in;
p_in++;
p_out++;
}
}
if (*p_in)
p_in += 2;
p = strstr(p_in, MIMEHDR_INIT);
state = S_COPY_PLAIN;
if (p != NULL) {
unsigned char *q;
int wsp_only = 1;
for (q=p_in; (wsp_only && (q < p)); q++)
wsp_only = isspace(*q);
if (wsp_only) {
p_in = p + sizeof(MIMEHDR_INIT) - 1;
state = S_SKIP_MIMEINIT;
}
}
break;
}
}
*p_out = '\0';
}
#define S_BODY_DATA 0
#define S_BODY_HDR 1
static int BodyState = S_BODY_DATA;
static int CurrEncodingIsQP = 0;
static int CurrTypeNeedsDecode = 0;
#define MAX_DELIM_LEN 70
static unsigned char MultipartDelimiter[MAX_DELIM_LEN+3];
static const char ENC8BIT[] = "Content-Transfer-Encoding: 8bit";
static void SetEncoding8bit(unsigned char *XferEncOfs)
{
unsigned char *p;
if (XferEncOfs != NULL) {
memcpy(XferEncOfs, ENC8BIT, sizeof(ENC8BIT) - 1);
for (p=XferEncOfs+sizeof(ENC8BIT)-1; (*p >= ' '); p++) *p=' ';
}
}
static char *GetBoundary(char *CntType)
{
char *p1, *p2;
int flag;
p1 = CntType;
do {
p2 = strchr(p1, ';');
if (p2)
for (p2++; isspace(*p2); p2++);
p1 = p2;
} while ((p1) && (strncasecmp(p1, "boundary", 8) != 0));
if (p1 == NULL)
return NULL;
for (p1+=8, flag=0; (isspace(*p1) || (*p1 == '=')); p1++)
flag |= (*p1 == '=');
if (!flag)
return NULL;
if (*p1 == '\"') {
p1++;
p2 = strchr(p1, '\"');
if (p2 == NULL)
return NULL;
}
else {
p2 = strchr(p1, ';');
}
if (p2 != NULL)
*p2 = '\0';
return (p1 && strlen(p1)) ? p1 : NULL;
}
int CheckContentType(char *CntType)
{
static char *DecodedTypes[] = {
"text/",
"message/rfc822",
NULL
};
char *p = CntType;
int i;
if (CntType == NULL) return 0;
for (; isspace(*p); p++) ;
for (i=0;
(DecodedTypes[i] &&
(strncasecmp(p, DecodedTypes[i], strlen(DecodedTypes[i]))));
i++) ;
return (DecodedTypes[i] != NULL);
}
int MimeBodyType(unsigned char *hdrs, int WantDecode)
{
unsigned char *NxtHdr = hdrs;
unsigned char *XferEnc, *XferEncOfs, *CntType, *MimeVer, *p;
int HdrsFound = 0;
int BodyType;
MultipartDelimiter[0] = '\0';
CurrEncodingIsQP = CurrTypeNeedsDecode = 0;
BodyState = S_BODY_DATA;
BodyType = 0;
if (hdrs == NULL)
return BodyType;
XferEnc = XferEncOfs = CntType = MimeVer = NULL;
do {
if (strncasecmp("Content-Transfer-Encoding:", NxtHdr, 26) == 0) {
XferEncOfs = NxtHdr;
p = nxtaddr(NxtHdr);
if (p != NULL) {
xalloca(XferEnc, char *, strlen(p) + 1);
strcpy(XferEnc, p);
HdrsFound++;
}
}
else if (strncasecmp("Content-Type:", NxtHdr, 13) == 0) {
for (NxtHdr += 13; ((*NxtHdr == ' ') || (*NxtHdr == '\t')); NxtHdr++);
p = NxtHdr-1;
do {
p=strchr((p+1),'\n');
} while ( (p != NULL) && ((*(p+1) == '\t') || (*(p+1) == ' ')) );
if (p == NULL) p = NxtHdr + strlen(NxtHdr);
xalloca(CntType, char *, p-NxtHdr+2);
strncpy(CntType, NxtHdr, (p-NxtHdr));
*(CntType+(p-NxtHdr)) = '\0';
HdrsFound++;
}
else if (strncasecmp("MIME-Version:", NxtHdr, 13) == 0) {
p = nxtaddr(NxtHdr);
if (p != NULL) {
xalloca(MimeVer, char *, strlen(p) + 1);
strcpy(MimeVer, p);
HdrsFound++;
}
}
NxtHdr = (strchr(NxtHdr, '\n'));
if (NxtHdr != NULL) NxtHdr++;
} while ((NxtHdr != NULL) && (*NxtHdr) && (HdrsFound != 3));
if ((MimeVer != NULL) && (strcmp(MimeVer, "1.0") == 0)) {
CurrTypeNeedsDecode = CheckContentType(CntType);
if ( (CntType != NULL) &&
((strncasecmp(CntType, "multipart/mixed", 16) == 0) ||
(strncasecmp(CntType, "message/", 8) == 0)) ) {
char *p1 = GetBoundary(CntType);
if (p1 != NULL) {
strcpy(MultipartDelimiter, "--");
strncat(MultipartDelimiter, p1, MAX_DELIM_LEN);
MultipartDelimiter[sizeof(MultipartDelimiter)-1] = '\0';
BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE);
}
}
if ((XferEnc != NULL) && (BodyType == 0)) {
if (strcasecmp(XferEnc, "quoted-printable") == 0) {
CurrEncodingIsQP = 1;
BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE);
if (WantDecode && CurrTypeNeedsDecode) {
SetEncoding8bit(XferEncOfs);
}
}
else if (strcasecmp(XferEnc, "7bit") == 0) {
CurrEncodingIsQP = 0;
BodyType = (MSG_IS_7BIT);
}
else if (strcasecmp(XferEnc, "8bit") == 0) {
CurrEncodingIsQP = 0;
BodyType = (MSG_IS_8BIT);
}
}
}
return BodyType;
}
static int DoOneQPLine(unsigned char **bufp, flag delimited, flag issoftline)
{
unsigned char *buf = *bufp;
unsigned char *p_in, *p_out, *p;
int n;
int ret = 0;
if (delimited && !issoftline && buf[0]=='=' && !strncmp(*bufp, "=2E\r\n", 5))
{
strcpy(buf, "..\r\n");
*bufp += 5;
return(FALSE);
}
p_in = buf;
if (delimited && issoftline && (strncmp(buf, "..", 2) == 0))
p_in++;
for (p_out = buf; (*p_in); ) {
p = strchr(p_in, '=');
if (p == NULL) {
n = strlen(p_in);
memmove(p_out, p_in, n);
p_in += n; p_out += n;
}
else {
if (p > p_in) {
n = (p - p_in);
memmove(p_out, p_in, n);
p_out += n;
}
switch (*(p+1)) {
case '\0': case '\r': case '\n':
p_in = p+1;
if (*p_in == '\r') p_in++;
if (*p_in == '\n') p_in++;
ret = 1;
break;
default:
if (qp_char(*(p+1), *(p+2), p_out) == 0) {
p_in = p+3;
}
else {
*p_out = '=';
p_in = p+1;
}
p_out++;
break;
}
}
}
*p_out = '\0';
*bufp = p_out;
return ret;
}
int UnMimeBodyline(unsigned char **bufp, flag delimited, flag softline)
{
unsigned char *buf = *bufp;
int ret = 0;
switch (BodyState) {
case S_BODY_HDR:
UnMimeHeader(buf);
if ((*buf == '\0') || (*buf == '\n') || (strcmp(buf, "\r\n") == 0)) {
BodyState = S_BODY_DATA;
}
else if (strncasecmp("Content-Transfer-Encoding:", buf, 26) == 0) {
char *XferEnc;
XferEnc = nxtaddr(buf);
if ((XferEnc != NULL) && (strcasecmp(XferEnc, "quoted-printable") == 0)) {
CurrEncodingIsQP = 1;
if (CurrTypeNeedsDecode)
SetEncoding8bit(buf);
}
}
else if (strncasecmp("Content-Type:", buf, 13) == 0) {
CurrTypeNeedsDecode = CheckContentType(nxtaddr(buf));
}
*bufp = (buf + strlen(buf));
break;
case S_BODY_DATA:
if ((*MultipartDelimiter) &&
(strncmp(buf, MultipartDelimiter, strlen(MultipartDelimiter)) == 0)) {
BodyState = S_BODY_HDR;
CurrEncodingIsQP = CurrTypeNeedsDecode = 0;
}
if (CurrEncodingIsQP && CurrTypeNeedsDecode)
ret = DoOneQPLine(bufp, delimited, softline);
else
*bufp = (buf + strlen(buf));
break;
}
return ret;
}
#ifdef STANDALONE
#include <stdio.h>
#include <unistd.h>
char *program_name = "unmime";
int outlevel = 0;
#define BUFSIZE_INCREMENT 4096
#ifdef DEBUG
#define DBG_FWRITE(B,L,BS,FD) fwrite(B, L, BS, FD)
#else
#define DBG_FWRITE(B,L,BS,FD)
#endif
int main(int argc, char *argv[])
{
unsigned int BufSize;
unsigned char *buffer, *buf_p;
int nl_count, i, bodytype;
#ifdef DEBUG
pid_t pid;
FILE *fd_orig, *fd_conv;
char fnam[100];
pid = getpid();
sprintf(fnam, "/tmp/i_unmime.%x", pid);
fd_orig = fopen(fnam, "w");
sprintf(fnam, "/tmp/o_unmime.%x", pid);
fd_conv = fopen(fnam, "w");
#endif
BufSize = BUFSIZE_INCREMENT;
buf_p = buffer = (unsigned char *) xmalloc(BufSize);
nl_count = 0;
do {
i = fread(buf_p, 1, 1, stdin);
switch (*buf_p) {
case '\n':
nl_count++;
break;
case '\r':
break;
default:
nl_count = 0;
break;
}
buf_p++;
if ((buf_p - buffer) == BufSize) {
buffer = xrealloc(buffer, BufSize+BUFSIZE_INCREMENT);
buf_p = buffer + BufSize;
BufSize += BUFSIZE_INCREMENT;
}
} while ((i > 0) && (nl_count < 2));
*buf_p = '\0';
DBG_FWRITE(buffer, strlen(buffer), 1, fd_orig);
UnMimeHeader(buffer);
bodytype = MimeBodyType(buffer, 1);
i = strlen(buffer);
fwrite(buffer, i, 1, stdout);
DBG_FWRITE(buffer, i, 1, fd_conv);
do {
buf_p = (buffer - 1);
do {
buf_p++;
i = fread(buf_p, 1, 1, stdin);
} while ((i == 1) && (*buf_p != '\n'));
if (i == 1) buf_p++;
*buf_p = '\0';
DBG_FWRITE(buf, (buf_p - buffer), 1, fd_orig);
if (buf_p > buffer) {
if (bodytype & MSG_NEEDS_DECODE) {
buf_p = buffer;
UnMimeBodyline(&buf_p, 0, 0);
}
fwrite(buffer, (buf_p - buffer), 1, stdout);
DBG_FWRITE(buffer, (buf_p - buffer), 1, fd_conv);
}
} while (buf_p > buffer);
free(buffer);
fflush(stdout);
#ifdef DEBUG
fclose(fd_orig);
fclose(fd_conv);
#endif
return 0;
}
#endif