Rocco Rutte:
[apps/madmutt.git] / rfc2047.c
index 209330e..2d57d6a 100644 (file)
--- a/rfc2047.c
+++ b/rfc2047.c
 #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"
@@ -36,7 +38,7 @@
 #endif
 
 #define ENCWORD_LEN_MAX 75
-#define ENCWORD_LEN_MIN 9       /* safe_strlen ("=?.?.?.?=") */
+#define ENCWORD_LEN_MIN 9       /* str_len ("=?.?.?.?=") */
 
 #define HSPACE(x) ((x) == '\0' || (x) == ' ' || (x) == '\t')
 
@@ -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);
+  ob = buf = mem_malloc (obl);
   n = iconv (cd, &f, &flen, &ob, &obl);
   if (n == (size_t) (-1) || 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);
 
@@ -94,7 +96,7 @@ char *mutt_choose_charset (const char *fromcode, const char *charsets,
 
     q = strchr (p, ':');
 
-    n = q ? q - p : safe_strlen (p);
+    n = q ? q - p : str_len (p);
 
     if (!n ||
         /* Assume that we never need more than 12 characters of
@@ -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) {
@@ -147,7 +149,7 @@ static size_t b_encoder (char *s, ICONV_CONST char *d, size_t dlen,
   char *s0 = s;
 
   memcpy (s, "=?", 2), s += 2;
-  memcpy (s, tocode, safe_strlen (tocode)), s += safe_strlen (tocode);
+  memcpy (s, tocode, str_len (tocode)), s += str_len (tocode);
   memcpy (s, "?B?", 3), s += 3;
   for (;;) {
     if (!dlen)
@@ -185,7 +187,7 @@ static size_t q_encoder (char *s, ICONV_CONST char *d, size_t dlen,
   char *s0 = s;
 
   memcpy (s, "=?", 2), s += 2;
-  memcpy (s, tocode, safe_strlen (tocode)), s += safe_strlen (tocode);
+  memcpy (s, tocode, str_len (tocode)), s += str_len (tocode);
   memcpy (s, "?Q?", 3), s += 3;
   while (dlen--) {
     unsigned char c = *d++;
@@ -227,7 +229,7 @@ static size_t try_block (ICONV_CONST char *d, size_t dlen,
   if (fromcode) {
     cd = mutt_iconv_open (tocode, fromcode, 0);
     assert (cd != (iconv_t) (-1));
-    ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - safe_strlen (tocode);
+    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)) {
       assert (errno == E2BIG);
@@ -238,8 +240,8 @@ static size_t try_block (ICONV_CONST char *d, size_t dlen,
     iconv_close (cd);
   }
   else {
-    if (dlen > sizeof (buf1) - safe_strlen (tocode))
-      return sizeof (buf1) - safe_strlen (tocode) + 1;
+    if (dlen > sizeof (buf1) - str_len (tocode))
+      return sizeof (buf1) - str_len (tocode) + 1;
     memcpy (buf1, d, dlen);
     ob = buf1 + dlen;
   }
@@ -254,7 +256,7 @@ static size_t try_block (ICONV_CONST char *d, size_t dlen,
       ++count;
   }
 
-  len = ENCWORD_LEN_MIN - 2 + safe_strlen (tocode);
+  len = ENCWORD_LEN_MIN - 2 + str_len (tocode);
   len_b = len + (((ob - buf1) + 2) / 3) * 4;
   len_q = len + (ob - buf1) + 2 * count;
 
@@ -293,7 +295,7 @@ static size_t encode_block (char *s, char *d, size_t dlen,
   if (fromcode) {
     cd = mutt_iconv_open (tocode, fromcode, 0);
     assert (cd != (iconv_t) (-1));
-    ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - safe_strlen (tocode);
+    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);
     assert (n1 != (size_t) (-1) && n2 != (size_t) (-1));
@@ -361,7 +363,7 @@ static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col,
   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);
 
@@ -480,15 +482,15 @@ static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col,
 
     /* Add to output buffer. */
 #define LINEBREAK "\n\t"
-    if (bufpos + wlen + safe_strlen (LINEBREAK) > buflen) {
-      buflen = bufpos + wlen + safe_strlen (LINEBREAK);
-      safe_realloc (&buf, buflen);
+    if (bufpos + wlen + str_len (LINEBREAK) > buflen) {
+      buflen = bufpos + wlen + str_len (LINEBREAK);
+      mem_realloc (&buf, buflen);
     }
     r = encode_block (buf + bufpos, t, n, icode, tocode, encoder);
     assert (r == wlen);
     bufpos += wlen;
-    memcpy (buf + bufpos, LINEBREAK, safe_strlen (LINEBREAK));
-    bufpos += safe_strlen (LINEBREAK);
+    memcpy (buf + bufpos, LINEBREAK, str_len (LINEBREAK));
+    bufpos += str_len (LINEBREAK);
 #undef LINEBREAK
 
     col = 1;
@@ -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';
 
@@ -527,18 +529,18 @@ void _rfc2047_encode_string (char **pd, int encode_specials, int col)
   if (!charsets || !*charsets)
     charsets = "UTF-8";
 
-  rfc2047_encode (*pd, safe_strlen (*pd), col,
+  rfc2047_encode (*pd, str_len (*pd), col,
                   Charset, charsets, &e, &elen,
                   encode_specials ? RFC822Specials : NULL);
 
-  FREE (pd);
+  mem_free (pd);
   *pd = e;
 }
 
 void rfc2047_encode_adrlist (ADDRESS * addr, const char *tag)
 {
   ADDRESS *ptr = addr;
-  int col = tag ? safe_strlen (tag) + 2 : 32;
+  int col = tag ? str_len (tag) + 2 : 32;
 
   while (ptr) {
     if (ptr->personal)
@@ -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 (safe_strlen (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);
 }
 
@@ -711,14 +713,14 @@ void rfc2047_decode (char **pd)
   if (!s || !*s)
     return;
 
-  dlen = 4 * safe_strlen (s);        /* should be enough */
-  d = d0 = safe_malloc (dlen + 1);
+  dlen = 4 * str_len (s);        /* should be enough */
+  d = d0 = mem_malloc (dlen + 1);
 
   while (*s && dlen > 0) {
     if (!(p = find_encoded_word (s, &q))) {
       /* no encoded words */
       if (!option (OPTSTRICTMIME)) {
-        n = safe_strlen (s);
+        n = str_len (s);
         if (found_encoded && (m = lwslen (s, n)) != 0) {
           if (m != n)
             *d = ' ', d++, dlen--;
@@ -728,10 +730,10 @@ 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 = safe_strlen (t);
+            tlen = str_len (t);
             strncpy (d, t, tlen);
             d += tlen;
           }
@@ -739,7 +741,7 @@ void rfc2047_decode (char **pd)
             strncpy (d, s, n);
             d += n;
           }
-          FREE (&t);
+          mem_free (&t);
           break;
         }
       }
@@ -781,13 +783,13 @@ void rfc2047_decode (char **pd)
     rfc2047_decode_word (d, p, dlen);
     found_encoded = 1;
     s = q;
-    n = safe_strlen (d);
+    n = str_len (d);
     dlen -= n;
     d += n;
   }
   *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);
+  }
+}