rationalize list handling in mutt a bit.
[apps/madmutt.git] / imap / utf7.c
index bc5571a..e2b3340 100644 (file)
 # include "config.h"
 #endif
 
+#include <lib-lib/mem.h>
+
 #include "mutt.h"
 #include "charset.h"
 #include "imap_private.h"
 
-#include "lib/mem.h"
-
-static int 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, 63, -1, -1, -1,
-  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
-};
-
-static char B64Chars[64] = {
-  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
-  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
-  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
-  't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
-  '8', '9', '+', ','
-};
-
 /*
  * Convert the data (u7,u7len) from RFC 2060's UTF-7 to UTF-8.
  * The result is null-terminated and returned, and also stored
@@ -52,7 +33,7 @@ static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8,
   char *buf, *p;
   int b, ch, k;
 
-  p = buf = safe_malloc (u7len + u7len / 8 + 1);
+  p = buf = p_new(char, u7len + u7len / 8 + 1);
 
   for (; u7len; u7++, u7len--) {
     if (*u7 == '&') {
@@ -66,7 +47,7 @@ static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8,
       ch = 0;
       k = 10;
       for (; u7len; u7++, u7len--) {
-        if ((*u7 & 0x80) || (b = Index_64[(int) *u7]) == -1)
+        if ((b = base64val(*u7)) < 0)
           break;
         if (k > 0) {
           ch |= b << k;
@@ -113,13 +94,13 @@ static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8,
   if (u8len)
     *u8len = p - buf;
 
-  safe_realloc (&buf, p - buf);
+  p_realloc(&buf, p - buf);
   if (u8)
     *u8 = buf;
   return buf;
 
 bail:
-  FREE (&buf);
+  p_delete(&buf);
   return 0;
 }
 
@@ -142,7 +123,7 @@ static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7,
    * In the worst case we convert 2 chars to 7 chars. For example:
    * "\x10&\x10&..." -> "&ABA-&-&ABA-&-...".
    */
-  p = buf = safe_malloc ((u8len / 2) * 7 + 6);
+  p = buf = p_new(char, (u8len / 2) * 7 + 6);
 
   while (u8len) {
     unsigned char c = *u8;
@@ -185,17 +166,17 @@ static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7,
       }
       if (ch & ~0xffff)
         ch = 0xfffe;
-      *p++ = B64Chars[b | ch >> k];
+      *p++ = __m_b64chars[b | ch >> k];
       k -= 6;
       for (; k >= 0; k -= 6)
-        *p++ = B64Chars[(ch >> k) & 0x3f];
+        *p++ = __m_b64chars[(ch >> k) & 0x3f];
       b = (ch << (-k)) & 0x3f;
       k += 16;
     }
     else {
       if (base64) {
         if (k > 10)
-          *p++ = B64Chars[b];
+          *p++ = __m_b64chars[b];
         *p++ = '-';
         base64 = 0;
       }
@@ -206,47 +187,50 @@ static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7,
   }
 
   if (u8len) {
-    FREE (&buf);
+    p_delete(&buf);
     return 0;
   }
 
   if (base64) {
     if (k > 10)
-      *p++ = B64Chars[b];
+      *p++ = __m_b64chars[b];
     *p++ = '-';
   }
 
   *p++ = '\0';
   if (u7len)
     *u7len = p - buf;
-  safe_realloc (&buf, p - buf);
+  p_realloc(&buf, p - buf);
   if (u7)
     *u7 = buf;
   return buf;
 
 bail:
-  FREE (&buf);
+  p_delete(&buf);
   return 0;
 }
 
 void imap_utf7_encode (char **s)
 {
   if (Charset) {
-    char *t = safe_strdup (*s);
+    char *t = m_strdup(*s);
 
-    if (!mutt_convert_string (&t, Charset, "UTF-8", 0))
-      utf8_to_utf7 (t, mutt_strlen (t), s, 0);
-    FREE (&t);
+    if (!mutt_convert_string (&t, Charset, "UTF-8", 0)) {
+      char *u7 = utf8_to_utf7 (t, strlen (t), NULL, 0);
+      p_delete(s);
+      *s = u7;
+    }
+    p_delete(&t);
   }
 }
 
 void imap_utf7_decode (char **s)
 {
   if (Charset) {
-    char *t = utf7_to_utf8 (*s, mutt_strlen (*s), 0, 0);
+    char *t = utf7_to_utf8 (*s, m_strlen(*s), 0, 0);
 
     if (t && !mutt_convert_string (&t, "UTF-8", Charset, 0)) {
-      FREE (s);
+      p_delete(s);
       *s = t;
     }
   }