#ifndef lint
static char sccsi2[] = "%W% (Sun) %G%";
#endif
#include <string.h>
typedef int bool;
#define FALSE 0
#define TRUE 1
static signed char hexindex[] = {
-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
};
static signed 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) (((unsigned char) (c) > 127) ? -1 : \
index_64[(unsigned char) (c)])
static int
unqp(unsigned char byte1, unsigned char byte2)
{
if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
return (-1);
return (hexindex[byte1] << 4 | hexindex[byte2]);
}
#define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
bool
decode_rfc2047(char *str, char *dst, char *charset)
{
char *p, *q, *pp;
char *startofmime, *endofmime;
int c, quoted_printable;
bool encoding_found = FALSE;
bool between_encodings = FALSE;
bool equals_pending = FALSE;
int whitespace = 0;
if (str == NULL)
return (FALSE);
if (!strchr(str, '='))
return (FALSE);
for (p = str, q = dst; *p; p++) {
if (equals_pending) {
*q++ = '=';
equals_pending = FALSE;
between_encodings = FALSE;
}
if (*p != '=') {
if (between_encodings && is_lws(*p))
whitespace++;
else
between_encodings = FALSE;
*q++ = *p;
continue;
}
equals_pending = TRUE;
if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
startofmime = p + 2;
for (pp = startofmime; *pp && *pp != '?'; pp++)
;
if (!*pp)
continue;
strncpy(charset, startofmime, pp - startofmime);
charset[pp - startofmime] = '\0';
startofmime = pp + 1;
if (*startofmime != 'B' && *startofmime != 'b' &&
*startofmime != 'Q' && *startofmime != 'q')
continue;
quoted_printable = (*startofmime == 'Q' ||
*startofmime == 'q');
startofmime++;
if (*startofmime != '?')
continue;
startofmime++;
endofmime = NULL;
for (pp = startofmime; *pp && *(pp+1); pp++) {
if (is_lws(*pp))
break;
else if (*pp == '?' && pp[1] == '=') {
endofmime = pp;
break;
}
}
if (is_lws(*pp) || endofmime == NULL)
continue;
equals_pending = FALSE;
if (between_encodings)
q -= whitespace;
if (quoted_printable) {
for (pp = startofmime; pp < endofmime; pp++) {
if (*pp == '=') {
c = unqp(pp[1], pp[2]);
if (c == -1)
continue;
if (c != 0)
*q++ = c;
pp += 2;
} else if (*pp == '_')
*q++ = ' ';
else
*q++ = *pp;
}
} else {
int c1, c2, c3, c4;
pp = startofmime;
while (pp < endofmime) {
while ((pp < endofmime) &&
((c1 = char64(*pp)) == -1)) {
pp++;
}
if (pp < endofmime)
pp++;
while ((pp < endofmime) &&
((c2 = char64(*pp)) == -1)) {
pp++;
}
if (pp < endofmime && c1 != -1 &&
c2 != -1) {
*q++ = (c1 << 2) | (c2 >> 4);
pp++;
}
while ((pp < endofmime) &&
((c3 = char64(*pp)) == -1)) {
pp++;
}
if (pp < endofmime && c2 != -1 &&
c3 != -1) {
*q++ = ((c2 & 0xF) << 4) |
(c3 >> 2);
pp++;
}
while ((pp < endofmime) &&
((c4 = char64(*pp)) == -1)) {
pp++;
}
if (pp < endofmime && c3 != -1 &&
c4 != -1) {
*q++ = ((c3 & 0x3) << 6) | (c4);
pp++;
}
}
}
p = endofmime + 1;
encoding_found = TRUE;
between_encodings = TRUE;
whitespace = 0;
}
}
if (equals_pending)
*q++ = '=';
*q = '\0';
return (encoding_found);
}