Use GLib's implementation of Base64 instead of our own.
[claws.git] / src / unmime.c
index 0fe584059600373dcbf27fee7e5046edd55fcb8c..aeda9e402f56f90f8e9089b06ef653565b8ce111 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include <glib.h>
@@ -26,7 +27,6 @@
 #include <ctype.h>
 
 #include "codeconv.h"
-#include "base64.h"
 #include "quoted-printable.h"
 
 #define ENCODED_WORD_BEGIN     "=?"
@@ -34,7 +34,7 @@
 
 /* Decodes headers based on RFC2045 and RFC2047. */
 
-gchar *unmime_header(const gchar *encoded_str)
+gchar *unmime_header(const gchar *encoded_str, gboolean addr_field)
 {
        const gchar *p = encoded_str;
        const gchar *eword_begin_p, *encoding_begin_p, *text_begin_p,
@@ -45,11 +45,13 @@ gchar *unmime_header(const gchar *encoded_str)
        GString *outbuf;
        gchar *out_str;
        gsize out_len;
+       int in_quote = FALSE;
 
        outbuf = g_string_sized_new(strlen(encoded_str) * 2);
 
        while (*p != '\0') {
                gchar *decoded_text = NULL;
+               const gchar *quote_p;
                gint len;
 
                eword_begin_p = strstr(p, ENCODED_WORD_BEGIN);
@@ -57,6 +59,18 @@ gchar *unmime_header(const gchar *encoded_str)
                        g_string_append(outbuf, p);
                        break;
                }
+               
+               quote_p = p;
+               while ((quote_p = strchr(quote_p, '"')) != NULL) {
+                       if (quote_p && quote_p < eword_begin_p) {
+                               /* Found a quote before the encoded word. */
+                               in_quote = !in_quote;
+                               quote_p++;
+                       }
+                       if (quote_p >= eword_begin_p)
+                               break;
+               }
+
                encoding_begin_p = strchr(eword_begin_p + 2, '?');
                if (!encoding_begin_p) {
                        g_string_append(outbuf, p);
@@ -97,11 +111,11 @@ gchar *unmime_header(const gchar *encoded_str)
                encoding = g_ascii_toupper(*(encoding_begin_p + 1));
 
                if (encoding == 'B') {
-                       decoded_text = g_malloc
-                               (eword_end_p - (text_begin_p + 1) + 1);
-                       len = base64_decode(decoded_text, text_begin_p + 1,
-                                           eword_end_p - (text_begin_p + 1));
-                       decoded_text[len] = '\0';
+                       gchar *tmp;
+                       tmp = g_strndup(text_begin_p + 1, eword_end_p - (text_begin_p + 1) + 1);
+                       decoded_text = g_base64_decode(tmp, &out_len);
+                       g_free(tmp);
+                       decoded_text[out_len] = '\0';
                } else if (encoding == 'Q') {
                        decoded_text = g_malloc
                                (eword_end_p - (text_begin_p + 1) + 1);
@@ -114,6 +128,18 @@ gchar *unmime_header(const gchar *encoded_str)
                        continue;
                }
 
+               /* An encoded word MUST not appear within a quoted string,
+                * so quoting that word after decoding should be safe.
+                * We check there are no quotes just to be sure. If there
+                * are, well, the comma won't pose a problem, probably.
+                */
+               if (addr_field && strchr(decoded_text, ',') && !in_quote &&
+                   !strchr(decoded_text, '"')) {
+                       gchar *tmp = g_strdup_printf("\"%s\"", decoded_text);
+                       g_free(decoded_text);
+                       decoded_text = tmp;
+               }
+
                /* convert to UTF-8 */
                conv_str = conv_codeset_strdup(decoded_text, charset, NULL);
                if (!conv_str || !g_utf8_validate(conv_str, -1, NULL)) {