X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=lib-mime%2Frfc2047.c;h=8d444bc10cb7c3a9be8aae9d1565d5abfff6aaff;hp=264caff2db57fb85fbba4715d0731ec6a8c0dd2a;hb=09a7c8b5bbcc1630207e799d07e4ac438c9c3f1c;hpb=3a3420a4cb3369d51eecc9287d556596f98e7314 diff --git a/lib-mime/rfc2047.c b/lib-mime/rfc2047.c index 264caff..8d444bc 100644 --- a/lib-mime/rfc2047.c +++ b/lib-mime/rfc2047.c @@ -27,22 +27,13 @@ * please see the file GPL in the top level source directory. */ -#include -#include -#include +#include #include #include "charset.h" #include "thread.h" -#include -#include -#include -#include -#include -#include - /* If you are debugging this file, comment out the following line. */ /*#define NDEBUG*/ @@ -73,7 +64,7 @@ convert_string(const char *from, const char *f, ssize_t flen, cd = mutt_iconv_open(to, from, 0); - if (cd == (iconv_t)(-1)) + if (cd == MUTT_ICONV_ERROR) return -1; obl = 4 * flen + 1; @@ -157,7 +148,7 @@ char *mutt_choose_charset(const char *fromcode, const char *charsets, p_delete(&res); } - mutt_canonical_charset(buf, sizeof(buf), tocode); + charset_canonicalize(buf, sizeof(buf), tocode); m_strreplace(&tocode, buf); } @@ -271,7 +262,7 @@ static size_t try_block(const char *d, ssize_t dlen, ssize_t ibl = dlen; iconv_t cd = mutt_iconv_open(tocode, fromcode, 0); - assert (cd != (iconv_t)(-1)); + assert (cd != MUTT_ICONV_ERROR); ob = buf1; @@ -338,7 +329,7 @@ encode_block(char *s, char *d, ssize_t dlen, if (fromcode) { cd = mutt_iconv_open(tocode, fromcode, 0); - assert (cd != (iconv_t) (-1)); + assert (cd != MUTT_ICONV_ERROR); ib = d, ibl = dlen, ob = buf1, obl = sizeof(buf1) - m_strlen(tocode); n1 = my_iconv(cd, &ib, &ibl, &ob, &obl); n2 = my_iconv(cd, 0, 0, &ob, &obl); @@ -452,7 +443,7 @@ static int rfc2047_encode(const char *d, ssize_t dlen, int col, } /* Hack to avoid labelling 8-bit data as us-ascii. */ - if (!icode && mutt_is_us_ascii(tocode)) + if (!icode && charset_is_us_ascii(tocode)) tocode = "unknown-8bit"; /* Adjust t0 for maximum length of line. */ @@ -564,7 +555,7 @@ static int rfc2047_encode(const char *d, ssize_t dlen, int col, } -void _rfc2047_encode_string(char **pd, int encode_specials, int col) +static void _rfc2047_encode_string(char **pd, int encode_specials, int col) { char *e; ssize_t elen; @@ -604,89 +595,80 @@ void rfc2047_encode_adrlist(address_t *addr, const char *tag) /* Decoding functions */ /****************************************************************************/ -/* decode one word into d[len] */ -static int rfc2047_decode_word(char *d, size_t len, const char *s) +/* decode one word into d[len] =?cst?[QB]?....?= */ +static int +rfc2047_decode_word(char *d, size_t len, const char *p, const char *end) { - const char *p, *eotoken; - char *charset = NULL; - int enc = 0, count = 0; - char *d0; + char charset[STRING] = ""; + const char *t; + char *q, *d0 = NULL; + int enc = 0; - /* =?[QB]?cset?.?= */ - for (p = s; (eotoken = strchr(p, '?')); p = eotoken + 1) { - switch (++count) { - const char *t; - char *q; + p += 2; /* =? */ - case 2: - /* ignore language specification a la RFC 2231 */ - t = memchr(p, '*', eotoken - p) ?: eotoken; - charset = p_dupstr(p, t - p); - break; + t = strchr(p, '?'); + if (!t) + return -1; + m_strncpy(charset, sizeof(charset), p, t - p); - case 3: - switch (*p) { - case 'q': case 'Q': - enc = ENCQUOTEDPRINTABLE; - break; + switch (t[1]) { + case 'q': case 'Q': + enc = ENCQUOTEDPRINTABLE; + break; - case 'b': case 'B': - enc = ENCBASE64; - break; + case 'b': case 'B': + enc = ENCBASE64; + break; - default: - p_delete(&charset); - return -1; + default: + return -1; + } + + if (t[2] != '?') + return -1; + + p = t + 3; /* skip ?[QB]? */ + d0 = q = p_new(char, end - p + 1); /* it's enough space to decode */ + + if (enc == ENCQUOTEDPRINTABLE) { + while (p < end - 2) { + if (*p == '=' && hexval(p[1]) >= 0 && hexval(p[2]) >= 0) { + *q++ = (hexval (p[1]) << 4) | hexval (p[2]); + p += 3; + } else + if (*p == '_') { + *q++ = ' '; + p++; + } else { + *q++ = *p++; } - break; + } + } else { /* enc == ENCBASE64 */ + int c, b = 0, k = 0; - case 4: - d0 = q = p_new(char, m_strlen(s) + 1); - - if (enc == ENCQUOTEDPRINTABLE) { - while (p < eotoken) { - if (*p == '=' && hexval(p[1]) >= 0 && hexval(p[2]) >= 0) { - *q++ = (hexval (p[1]) << 4) | hexval (p[2]); - p += 3; - } else - if (*p == '_') { - *q++ = ' '; - p++; - } else { - *q++ = *p++; - } - } - *q = 0; - } else { /* enc == ENCBASE64 */ - int c, b = 0, k = 0; - - while (p < eotoken) { - if (*p == '=') - break; - - c = base64val(*p++); - if (c < 0) - continue; - - if (k + 6 >= 8) { - k -= 2; - *q++ = b | (c >> k); - b = c << (8 - k); - } else { - b |= c << (k + 2); - k += 6; - } - } - *q = 0; + while (p < end - 2) { + if (*p == '=') + break; + + c = base64val(*p++); + if (c < 0) + continue; + + if (k + 6 >= 8) { + k -= 2; + *q++ = b | (c >> k); + b = c << (8 - k); + } else { + b |= c << (k + 2); + k += 6; } - break; } } + *q = '\0'; - if (charset) + if (*charset) mutt_convert_string(&d0, charset, Charset, M_ICONV_HOOK_FROM); m_strcpy(d, len, d0); - p_delete(&charset); p_delete(&d0); return 0; } @@ -711,7 +693,7 @@ static const char *find_encoded_word(const char *s, const char **x) continue; s += 3; - while (0x20 <= *s && *s < 0x7f && *s != '?') { + while (0x20 <= *s && *s < 0x7f && (*s != '?' || s[1] != '=')) { s++; } @@ -852,7 +834,7 @@ void rfc2047_decode(char **pd) } } - rfc2047_decode_word(d, dlen, p); + rfc2047_decode_word(d, dlen, p, q); found_encoded = 1; s = q; while (*d && dlen)