Better fix for bug #3349 (almost all the needed code was there :-)
[claws.git] / src / codeconv.c
index 629e88ad59722f9b892a2b25bfedeb76cebc94ff..565c0c273109ba4742ec6aaf46e8b2295db27fc0 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 "defs.h"
@@ -36,7 +37,6 @@
 
 #include "codeconv.h"
 #include "unmime.h"
-#include "base64.h"
 #include "quoted-printable.h"
 #include "utils.h"
 #include "prefs_common.h"
@@ -749,8 +749,17 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_func;
 
-       if (!strcmp2(src_code, dest_code))
+       if (!strcmp2(src_code, dest_code)) {
+               CharSet dest_charset = conv_get_charset_from_str(dest_code);
+               if (strict_mode && dest_charset == C_UTF_8) {
+                       /* ensure valid UTF-8 if target is UTF-8 */
+                       if (!g_utf8_validate(inbuf, -1, NULL)) {
+                               return NULL;
+                       }
+               }
+               /* otherwise, try for a lucky day */
                return g_strdup(inbuf);
+       }
 
        src_code = conv_get_fallback_for_private_encoding(src_code);
        conv_func = conv_get_code_conv_func(src_code, dest_code);
@@ -1071,7 +1080,11 @@ static const struct {
        {"ru_RU.KOI8-R" , C_KOI8_R      , C_KOI8_R},
        {"ru_RU.KOI8R"  , C_KOI8_R      , C_KOI8_R},
        {"ru_RU.CP1251" , C_WINDOWS_1251, C_KOI8_R},
+#ifdef G_OS_WIN32
+       {"ru_RU"        , C_WINDOWS_1251, C_KOI8_R},
+#else
        {"ru_RU"        , C_ISO_8859_5  , C_KOI8_R},
+#endif
        {"tg_TJ"        , C_KOI8_T      , C_KOI8_T},
        {"ru_UA"        , C_KOI8_U      , C_KOI8_U},
        {"uk_UA.CP1251" , C_WINDOWS_1251, C_KOI8_U},
@@ -1300,8 +1313,8 @@ static CharSet conv_get_locale_charset(void)
                return cur_charset;
        }
 
-       if (strcasestr(cur_locale, ".UTF-8") ||
-           strcasestr(cur_locale, ".utf8")) {
+       if (strcasestr(cur_locale, "UTF-8") ||
+           strcasestr(cur_locale, "utf8")) {
                cur_charset = C_UTF_8;
                return cur_charset;
        }
@@ -1339,14 +1352,12 @@ static CharSet conv_get_locale_charset_no_utf8(void)
        static CharSet cur_charset = -1;
        const gchar *cur_locale;
        const gchar *p;
-       gchar *tmp;
        gint i;
 
-       if (prefs_common.broken_are_utf8)
-               return conv_get_locale_charset();
-
-       if (cur_charset != -1)
+       if (prefs_common.broken_are_utf8) {
+               cur_charset = C_UTF_8;
                return cur_charset;
+       }
 
        cur_locale = conv_get_current_locale();
        if (!cur_locale) {
@@ -1354,10 +1365,10 @@ static CharSet conv_get_locale_charset_no_utf8(void)
                return cur_charset;
        }
 
-       if (strcasestr(cur_locale, "UTF-8")) {
-               tmp = g_strdup(cur_locale);
-               *(strcasestr(tmp, ".UTF-8")) = '\0';
-               cur_locale = tmp;
+       if (strcasestr(cur_locale, "UTF-8") ||
+           strcasestr(cur_locale, "utf8")) {
+               cur_charset = C_UTF_8;
+               return cur_charset;
        }
 
        if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
@@ -1424,7 +1435,8 @@ static CharSet conv_get_outgoing_charset(void)
                return out_charset;
        }
 
-       if (strcasestr(cur_locale, "UTF-8")) {
+       if (strcasestr(cur_locale, "UTF-8") ||
+           strcasestr(cur_locale, "utf8")) {
                out_charset = C_UTF_8;
                return out_charset;
        }
@@ -1502,12 +1514,13 @@ static gboolean conv_is_ja_locale(void)
        return is_ja_locale != 0;
 }
 
-gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
+gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding,
+                          gboolean addr_field)
 {
        gchar buf[BUFFSIZE];
 
        if (is_ascii_str(str))
-               return unmime_header(str);
+               return unmime_header(str, addr_field);
 
        if (default_encoding) {
                gchar *utf8_buf;
@@ -1517,7 +1530,7 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
                if (utf8_buf) {
                        gchar *decoded_str;
 
-                       decoded_str = unmime_header(utf8_buf);
+                       decoded_str = unmime_header(utf8_buf, addr_field);
                        g_free(utf8_buf);
                        return decoded_str;
                }
@@ -1528,7 +1541,7 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
        else
                conv_localetodisp(buf, sizeof(buf), str);
 
-       return unmime_header(buf);
+       return unmime_header(buf, addr_field);
 }
 
 #define MAX_LINELEN            76
@@ -1568,6 +1581,8 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
        }                                                               \
 }
 
+#define B64LEN(len) ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
+
 void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                        gint header_len, gboolean addr_field,
                        const gchar *out_encoding_)
@@ -1688,6 +1703,7 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                                        cur_len += mb_len;
                                        p += mb_len;
                                } else if (cur_len == 0) {
+                                       left = 0;
                                        LBREAK_IF_REQUIRED(1, FALSE);
                                        continue;
                                } else {
@@ -1713,11 +1729,12 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                                        out_enc_str_len =
                                                qp_get_q_encoding_len(out_str);
 
-                               Xalloca(enc_str, out_enc_str_len + 1, );
                                if (use_base64)
-                                       base64_encode(enc_str, out_str, out_str_len);
-                               else
+                                       enc_str = g_base64_encode(out_str, out_str_len);
+                               else {
+                                       Xalloca(enc_str, out_enc_str_len + 1, );
                                        qp_q_encode(enc_str, out_str);
+                               }
 
                                g_free(out_str);
 
@@ -1726,6 +1743,10 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                                g_snprintf(destp, mime_block_len + 1,
                                           MIMESEP_BEGIN "%s%s%s" MIMESEP_END,
                                           out_encoding, mimesep_enc, enc_str);
+
+                               if (use_base64)
+                                       g_free(enc_str);
+
                                destp += mime_block_len;
                                srcp += cur_len;
 
@@ -1749,6 +1770,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
 }
 
 #undef LBREAK_IF_REQUIRED
+#undef B64LEN
+
 gchar *conv_filename_from_utf8(const gchar *utf8_file)
 {
        gchar *fs_file;