X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=rfc2047.c;h=e17325453d17ab2255ad100f27d97bc2cf292970;hp=333667c0820412b7e47523aa50ca00489be8e654;hb=9218fd9d7e2a7976b34683c60cb94f7fda42141a;hpb=a8477ebaa09990b3688164cbe5cf661c4189541d diff --git a/rfc2047.c b/rfc2047.c index 333667c..e173254 100644 --- a/rfc2047.c +++ b/rfc2047.c @@ -13,9 +13,11 @@ #endif #include "mutt.h" +#include "ascii.h" #include "mime.h" #include "charset.h" #include "rfc2047.h" +#include "thread.h" #include "lib/mem.h" #include "lib/str.h" @@ -44,10 +46,10 @@ extern char RFC822Specials[]; -typedef size_t (*encoder_t) (char *, ICONV_CONST char *, size_t, +typedef size_t (*encoder_t) (char *, const char *, size_t, const char *); -static size_t convert_string (ICONV_CONST char *f, size_t flen, +static size_t convert_string (const char *f, size_t flen, const char *from, const char *to, char **t, size_t * tlen) { @@ -60,11 +62,11 @@ static size_t convert_string (ICONV_CONST char *f, size_t flen, if (cd == (iconv_t) (-1)) return (size_t) (-1); obl = 4 * flen + 1; - ob = buf = safe_malloc (obl); - n = iconv (cd, &f, &flen, &ob, &obl); - if (n == (size_t) (-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) { + ob = buf = mem_malloc (obl); + n = my_iconv(cd, &f, &flen, &ob, &obl); + if (n == (size_t) (-1) || my_iconv(cd, 0, 0, &ob, &obl) == (size_t) (-1)) { e = errno; - FREE (&buf); + mem_free (&buf); iconv_close (cd); errno = e; return (size_t) (-1); @@ -73,7 +75,7 @@ static size_t convert_string (ICONV_CONST char *f, size_t flen, *tlen = ob - buf; - safe_realloc (&buf, ob - buf + 1); + mem_realloc (&buf, ob - buf + 1); *t = buf; iconv_close (cd); @@ -102,7 +104,7 @@ char *mutt_choose_charset (const char *fromcode, const char *charsets, n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12)) continue; - t = safe_malloc (n + 1); + t = mem_malloc (n + 1); memcpy (t, p, n); t[n] = '\0'; @@ -112,21 +114,21 @@ char *mutt_choose_charset (const char *fromcode, const char *charsets, if (!tocode || n < bestn) { bestn = n; - FREE (&tocode); + mem_free (&tocode); tocode = t; if (d) { - FREE (&e); + mem_free (&e); e = s; } else - FREE (&s); + mem_free (&s); elen = slen; if (!bestn) break; } else { - FREE (&t); - FREE (&s); + mem_free (&t); + mem_free (&s); } } if (tocode) { @@ -141,7 +143,7 @@ char *mutt_choose_charset (const char *fromcode, const char *charsets, return tocode; } -static size_t b_encoder (char *s, ICONV_CONST char *d, size_t dlen, +static size_t b_encoder (char *s, const char *d, size_t dlen, const char *tocode) { char *s0 = s; @@ -178,7 +180,7 @@ static size_t b_encoder (char *s, ICONV_CONST char *d, size_t dlen, return s - s0; } -static size_t q_encoder (char *s, ICONV_CONST char *d, size_t dlen, +static size_t q_encoder (char *s, const char *d, size_t dlen, const char *tocode) { char hex[] = "0123456789ABCDEF"; @@ -213,13 +215,13 @@ static size_t q_encoder (char *s, ICONV_CONST char *d, size_t dlen, * tocode, unless fromcode is 0, in which case the data is assumed to * be already in tocode, which should be 8-bit and stateless. */ -static size_t try_block (ICONV_CONST char *d, size_t dlen, +static size_t try_block (const char *d, size_t dlen, const char *fromcode, const char *tocode, encoder_t * encoder, size_t * wlen) { char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1]; iconv_t cd; - ICONV_CONST char *ib; + const char *ib; char *ob, *p; size_t ibl, obl; int count, len, len_b, len_q; @@ -228,8 +230,8 @@ static size_t try_block (ICONV_CONST char *d, size_t dlen, cd = mutt_iconv_open (tocode, fromcode, 0); assert (cd != (iconv_t) (-1)); ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - str_len (tocode); - if (iconv (cd, &ib, &ibl, &ob, &obl) == (size_t) (-1) || - iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) { + if (my_iconv(cd, &ib, &ibl, &ob, &obl) == (size_t) (-1) || + my_iconv(cd, 0, 0, &ob, &obl) == (size_t) (-1)) { assert (errno == E2BIG); iconv_close (cd); assert (ib > d); @@ -286,7 +288,7 @@ static size_t encode_block (char *s, char *d, size_t dlen, { char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1]; iconv_t cd; - ICONV_CONST char *ib; + const char *ib; char *ob; size_t ibl, obl, n1, n2; @@ -294,8 +296,8 @@ static size_t encode_block (char *s, char *d, size_t dlen, cd = mutt_iconv_open (tocode, fromcode, 0); assert (cd != (iconv_t) (-1)); ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - str_len (tocode); - n1 = iconv (cd, &ib, &ibl, &ob, &obl); - n2 = iconv (cd, 0, 0, &ob, &obl); + n1 = my_iconv(cd, &ib, &ibl, &ob, &obl); + n2 = my_iconv(cd, 0, 0, &ob, &obl); assert (n1 != (size_t) (-1) && n2 != (size_t) (-1)); iconv_close (cd); return (*encoder) (s, buf1, ob - buf1, tocode); @@ -342,7 +344,7 @@ static size_t choose_block (char *d, size_t dlen, int col, * The input data is assumed to be a single line starting at column col; * if col is non-zero, the preceding character was a space. */ -static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col, +static int rfc2047_encode (const char *d, size_t dlen, int col, const char *fromcode, const char *charsets, char **e, size_t * elen, char *specials) { @@ -355,13 +357,13 @@ static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col, encoder_t encoder; char *tocode1 = 0; const char *tocode; - char *icode = "UTF-8"; + const char *icode = "UTF-8"; /* Try to convert to UTF-8. */ if (convert_string (d, dlen, fromcode, icode, &u, &ulen)) { ret = 1; icode = 0; - u = safe_malloc ((ulen = dlen) + 1); + u = mem_malloc ((ulen = dlen) + 1); memcpy (u, d, dlen); u[ulen] = 0; } @@ -446,7 +448,7 @@ static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col, /* Initialise the output buffer with the us-ascii prefix. */ buflen = 2 * ulen; - buf = safe_malloc (buflen); + buf = mem_malloc (buflen); bufpos = t0 - u; memcpy (buf, u, t0 - u); @@ -482,7 +484,7 @@ static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col, #define LINEBREAK "\n\t" if (bufpos + wlen + str_len (LINEBREAK) > buflen) { buflen = bufpos + wlen + str_len (LINEBREAK); - safe_realloc (&buf, buflen); + mem_realloc (&buf, buflen); } r = encode_block (buf + bufpos, t, n, icode, tocode, encoder); assert (r == wlen); @@ -498,14 +500,14 @@ static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col, /* Add last encoded word and us-ascii suffix to buffer. */ buflen = bufpos + wlen + (u + ulen - t1); - safe_realloc (&buf, buflen + 1); + mem_realloc (&buf, buflen + 1); r = encode_block (buf + bufpos, t, t1 - t, icode, tocode, encoder); assert (r == wlen); bufpos += wlen; memcpy (buf + bufpos, t1, u + ulen - t1); - FREE (&tocode1); - FREE (&u); + mem_free (&tocode1); + mem_free (&u); buf[buflen] = '\0'; @@ -518,7 +520,7 @@ void _rfc2047_encode_string (char **pd, int encode_specials, int col) { char *e; size_t elen; - char *charsets; + const char *charsets; if (!Charset || !*pd) return; @@ -531,7 +533,7 @@ void _rfc2047_encode_string (char **pd, int encode_specials, int col) Charset, charsets, &e, &elen, encode_specials ? RFC822Specials : NULL); - FREE (pd); + mem_free (pd); *pd = e; } @@ -555,7 +557,7 @@ static int rfc2047_decode_word (char *d, const char *s, size_t len) int enc = 0, count = 0; char *charset = NULL; - pd = d0 = safe_malloc (str_len (s)); + pd = d0 = mem_malloc (str_len (s)); for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1) { count++; @@ -565,7 +567,7 @@ static int rfc2047_decode_word (char *d, const char *s, size_t len) t = pp1; if ((t1 = memchr (pp, '*', t - pp))) t = t1; - charset = safe_malloc (t - pp + 1); + charset = mem_malloc (t - pp + 1); memcpy (charset, pp, t - pp); charset[t - pp] = '\0'; break; @@ -575,8 +577,8 @@ static int rfc2047_decode_word (char *d, const char *s, size_t len) else if (toupper ((unsigned char) *pp) == 'B') enc = ENCBASE64; else { - FREE (&charset); - FREE (&d0); + mem_free (&charset); + mem_free (&d0); return (-1); } break; @@ -623,8 +625,8 @@ static int rfc2047_decode_word (char *d, const char *s, size_t len) if (charset) mutt_convert_string (&d0, charset, Charset, M_ICONV_HOOK_FROM); strfcpy (d, d0, len); - FREE (&charset); - FREE (&d0); + mem_free (&charset); + mem_free (&d0); return (0); } @@ -712,7 +714,7 @@ void rfc2047_decode (char **pd) return; dlen = 4 * str_len (s); /* should be enough */ - d = d0 = safe_malloc (dlen + 1); + d = d0 = mem_malloc (dlen + 1); while (*s && dlen > 0) { if (!(p = find_encoded_word (s, &q))) { @@ -728,7 +730,7 @@ void rfc2047_decode (char **pd) char *t; size_t tlen; - t = safe_malloc (n + 1); + t = mem_malloc (n + 1); strfcpy (t, s, n + 1); if (mutt_convert_nonmime_string (&t) == 0) { tlen = str_len (t); @@ -739,7 +741,7 @@ void rfc2047_decode (char **pd) strncpy (d, s, n); d += n; } - FREE (&t); + mem_free (&t); break; } } @@ -787,7 +789,7 @@ void rfc2047_decode (char **pd) } *d = 0; - FREE (pd); + mem_free (pd); *pd = d0; str_adjust (pd); } @@ -800,3 +802,24 @@ void rfc2047_decode_adrlist (ADDRESS * a) a = a->next; } } + +void rfc2047_decode_envelope (ENVELOPE* e) { + + if (!e) + return; + + /* do RFC2047 decoding */ + rfc2047_decode_adrlist (e->from); + rfc2047_decode_adrlist (e->to); + rfc2047_decode_adrlist (e->cc); + rfc2047_decode_adrlist (e->bcc); + rfc2047_decode_adrlist (e->reply_to); + rfc2047_decode_adrlist (e->mail_followup_to); + rfc2047_decode_adrlist (e->return_path); + rfc2047_decode_adrlist (e->sender); + + if (e->subject) { + rfc2047_decode (&e->subject); + mutt_adjust_subject (e); + } +}