Fix bug #3573: Out of bounds read in macro LBREAK_IF_REQUIRED
[claws.git] / src / codeconv.c
index b23f2ef500886e7815c93539d0b954a501d9c312..11b23112c8b127fd15a653f9c8c04086ac8eec1c 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #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"
@@ -108,27 +108,42 @@ typedef enum
                state = JIS_AUXKANJI;   \
        }
 
-static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+static CodeConvFunc conv_get_code_conv_func    (const gchar    *src_charset_str,
+                                        const gchar    *dest_charset_str);
 
-static void conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gchar *conv_iconv_strdup_with_cd        (const gchar    *inbuf,
+                                        iconv_t         cd);
 
-static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gchar *conv_iconv_strdup                (const gchar    *inbuf,
+                                        const gchar    *src_code,
+                                        const gchar    *dest_code);
+
+static CharSet conv_get_locale_charset                 (void);
+static CharSet conv_get_outgoing_charset               (void);
+static CharSet conv_guess_ja_encoding(const gchar *str);
+static gboolean conv_is_ja_locale                      (void);
+
+static gint conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static gint conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf);
 
 static void conv_unreadable_8bit(gchar *str);
 
-static void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
 
-static void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
-static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf);
 
 static gboolean strict_mode = FALSE;
 
@@ -137,13 +152,17 @@ void codeconv_set_strict(gboolean mode)
        strict_mode = mode;
 }
 
-static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
-       guchar *out = outbuf;
+       gchar *out = outbuf;
        JISState state = JIS_ASCII;
 
-       while (*in != '\0') {
+       /*
+        * Loop outputs up to 3 bytes in each pass (aux kanji) and we
+        * need 1 byte to terminate the output
+        */
+       while (*in != '\0' && (out - outbuf) < outlen - 4) {
                if (*in == ESC) {
                        in++;
                        if (*in == '$') {
@@ -204,6 +223,7 @@ static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 
        *out = '\0';
+       return 0;
 }
 
 #define JIS_HWDAKUTEN          0x5e
@@ -272,13 +292,18 @@ static gint conv_jis_hantozen(guchar *outbuf, guchar jis_code, guchar sound_sym)
        return 1;
 }
 
-static void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
-       guchar *out = outbuf;
+       gchar *out = outbuf;
        JISState state = JIS_ASCII;
 
-       while (*in != '\0') {
+       /*
+        * Loop outputs up to 6 bytes in each pass (aux shift + aux
+        * kanji) and we need up to 4 bytes to terminate the output
+        * (ASCII shift + null)
+        */
+       while (*in != '\0' && (out - outbuf) < outlen - 10) {
                if (IS_ASCII(*in)) {
                        K_OUT();
                        *out++ = *in++;
@@ -358,14 +383,19 @@ static void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 
        K_OUT();
        *out = '\0';
+       return 0;
 }
 
-static void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
-       guchar *out = outbuf;
+       gchar *out = outbuf;
 
-       while (*in != '\0') {
+       /*
+        * Loop outputs up to 2 bytes in each pass and we need 1 byte
+        * to terminate the output
+        */
+       while (*in != '\0' && (out - outbuf) < outlen - 3) {
                if (IS_ASCII(*in)) {
                        *out++ = *in++;
                } else if (issjiskanji1(*in)) {
@@ -404,19 +434,23 @@ static void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 
        *out = '\0';
+       return 0;
 }
 
-static void conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *eucstr;
 
-       Xalloca(eucstr, outlen, return);
+       Xalloca(eucstr, outlen, return -1);
 
-       conv_jistoeuc(eucstr, outlen, inbuf);
-       conv_euctoutf8(outbuf, outlen, eucstr);
+       if (conv_jistoeuc(eucstr, outlen, inbuf) <0)
+               return -1;
+       if (conv_euctoutf8(outbuf, outlen, eucstr) < 0)
+               return -1;
+       return 0;
 }
 
-static void conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *tmpstr;
 
@@ -424,11 +458,14 @@ static void conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
        if (tmpstr) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
-       } else
+               return 0;
+       } else {
                strncpy2(outbuf, inbuf, outlen);
+               return -1;
+       }
 }
 
-static void conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        static iconv_t cd = (iconv_t)-1;
        static gboolean iconv_ok = TRUE;
@@ -437,17 +474,17 @@ static void conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
        if (cd == (iconv_t)-1) {
                if (!iconv_ok) {
                        strncpy2(outbuf, inbuf, outlen);
-                       return;
+                       return -1;
                }
                cd = iconv_open(CS_UTF_8, CS_EUC_JP_MS);
                if (cd == (iconv_t)-1) {
                        cd = iconv_open(CS_UTF_8, CS_EUC_JP);
                        if (cd == (iconv_t)-1) {
-                               g_warning("conv_euctoutf8(): %s\n",
+                               g_warning("conv_euctoutf8(): %s",
                                          g_strerror(errno));
                                iconv_ok = FALSE;
                                strncpy2(outbuf, inbuf, outlen);
-                               return;
+                               return -1;
                        }
                }
        }
@@ -456,29 +493,36 @@ static void conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
        if (tmpstr) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
-       } else
+               return 0;
+       } else {
                strncpy2(outbuf, inbuf, outlen);
+               return -1;
+       }
 }
 
-static void conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       gint r = -1;
        switch (conv_guess_ja_encoding(inbuf)) {
        case C_ISO_2022_JP:
-               conv_jistoutf8(outbuf, outlen, inbuf);
+               r = conv_jistoutf8(outbuf, outlen, inbuf);
                break;
        case C_SHIFT_JIS:
-               conv_sjistoutf8(outbuf, outlen, inbuf);
+               r = conv_sjistoutf8(outbuf, outlen, inbuf);
                break;
        case C_EUC_JP:
-               conv_euctoutf8(outbuf, outlen, inbuf);
+               r = conv_euctoutf8(outbuf, outlen, inbuf);
                break;
        default:
+               r = 0;
                strncpy2(outbuf, inbuf, outlen);
                break;
        }
+       
+       return r;
 }
 
-static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        static iconv_t cd = (iconv_t)-1;
        static gboolean iconv_ok = TRUE;
@@ -487,17 +531,17 @@ static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        if (cd == (iconv_t)-1) {
                if (!iconv_ok) {
                        strncpy2(outbuf, inbuf, outlen);
-                       return;
+                       return -1;
                }
                cd = iconv_open(CS_EUC_JP_MS, CS_UTF_8);
                if (cd == (iconv_t)-1) {
                        cd = iconv_open(CS_EUC_JP, CS_UTF_8);
                        if (cd == (iconv_t)-1) {
-                               g_warning("conv_utf8toeuc(): %s\n",
+                               g_warning("conv_utf8toeuc(): %s",
                                          g_strerror(errno));
                                iconv_ok = FALSE;
                                strncpy2(outbuf, inbuf, outlen);
-                               return;
+                               return -1;
                        }
                }
        }
@@ -506,18 +550,25 @@ static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        if (tmpstr) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
-       } else
+               return 0;
+       } else {
                strncpy2(outbuf, inbuf, outlen);
+               return -1;
+       }
 }
 
-static void conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *eucstr;
 
-       Xalloca(eucstr, outlen, return);
+       Xalloca(eucstr, outlen, return -1);
 
-       conv_utf8toeuc(eucstr, outlen, inbuf);
-       conv_euctojis(outbuf, outlen, eucstr);
+       if (conv_utf8toeuc(eucstr, outlen, inbuf) < 0)
+               return -1;
+       if (conv_euctojis(outbuf, outlen, eucstr) < 0)
+               return -1;
+               
+       return 0;
 }
 
 static void conv_unreadable_8bit(gchar *str)
@@ -533,7 +584,7 @@ static void conv_unreadable_8bit(gchar *str)
        }
 }
 
-CharSet conv_guess_ja_encoding(const gchar *str)
+static CharSet conv_guess_ja_encoding(const gchar *str)
 {
        const guchar *p = str;
        CharSet guessed = C_US_ASCII;
@@ -575,19 +626,19 @@ CharSet conv_guess_ja_encoding(const gchar *str)
        return guessed;
 }
 
-static void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_jistoutf8(outbuf, outlen, inbuf);
+       return conv_jistoutf8(outbuf, outlen, inbuf);
 }
 
-static void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_sjistoutf8(outbuf, outlen, inbuf);
+       return conv_sjistoutf8(outbuf, outlen, inbuf);
 }
 
-static void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_euctoutf8(outbuf, outlen, inbuf);
+       return conv_euctoutf8(outbuf, outlen, inbuf);
 }
 
 void conv_utf8todisp(gchar *outbuf, gint outlen, const gchar *inbuf)
@@ -598,17 +649,22 @@ void conv_utf8todisp(gchar *outbuf, gint outlen, const gchar *inbuf)
                conv_ustodisp(outbuf, outlen, inbuf);
 }
 
-static void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_anytoutf8(outbuf, outlen, inbuf);
+       gint r = 0;
+       if (conv_anytoutf8(outbuf, outlen, inbuf) < 0)
+               r = -1;
        if (g_utf8_validate(outbuf, -1, NULL) != TRUE)
                conv_unreadable_8bit(outbuf);
+       return r;
 }
 
-static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
        conv_unreadable_8bit(outbuf);
+       
+       return 0;
 }
 
 void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
@@ -636,15 +692,15 @@ void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
                g_free(tmpstr);
                return;
        } else {
-               if (tmpstr)
-                       g_free(tmpstr);
+               g_free(tmpstr);
                conv_utf8todisp(outbuf, outlen, inbuf);
        }
 }
 
-static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
+       return 0;
 }
 
 static const gchar *
@@ -652,6 +708,8 @@ conv_get_fallback_for_private_encoding(const gchar *encoding)
 {
        if (encoding && (encoding[0] == 'X' || encoding[0] == 'x') &&
            encoding[1] == '-') {
+               if (!g_ascii_strcasecmp(encoding, CS_X_MACCYR))
+                       return CS_MACCYR;
                if (!g_ascii_strcasecmp(encoding, CS_X_GBK))
                        return CS_GBK;
        }
@@ -683,7 +741,7 @@ gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                  const gchar *inbuf)
 {
        if (conv->code_conv_func != conv_noconv)
-               conv->code_conv_func(outbuf, outlen, inbuf);
+               return conv->code_conv_func(outbuf, outlen, inbuf);
        else {
                gchar *str;
 
@@ -706,21 +764,40 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_func;
 
+       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);
+       if (conv_func == conv_ustodisp && strict_mode && !is_ascii_str(inbuf))
+               return NULL;
+
        if (conv_func != conv_noconv) {
                len = (strlen(inbuf) + 1) * 3;
                buf = g_malloc(len);
                if (!buf) return NULL;
 
-               conv_func(buf, len, inbuf);
-               return g_realloc(buf, strlen(buf) + 1);
+               if (conv_func(buf, len, inbuf) == 0 || !strict_mode)
+                       return g_realloc(buf, strlen(buf) + 1);
+               else {
+                       g_free(buf);
+                       return NULL;
+               }
        }
 
        return conv_iconv_strdup(inbuf, src_code, dest_code);
 }
 
-CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
+static CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
                                     const gchar *dest_charset_str)
 {
        CodeConvFunc code_conv = conv_noconv;
@@ -805,12 +882,16 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
        return code_conv;
 }
 
-gchar *conv_iconv_strdup(const gchar *inbuf,
+static gchar *conv_iconv_strdup(const gchar *inbuf,
                         const gchar *src_code, const gchar *dest_code)
 {
        iconv_t cd;
        gchar *outbuf;
 
+       if (!src_code && !dest_code && 
+           g_utf8_validate(inbuf, -1, NULL))
+               return g_strdup(inbuf);
+
        if (!src_code)
                src_code = conv_get_outgoing_charset_str();
        if (!dest_code)
@@ -820,7 +901,11 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
        if (!strcasecmp(src_code, dest_code))
                return g_strdup(inbuf);
 
-       /* don't convert if current codeset is US-ASCII */
+       /* don't convert if dest codeset is US-ASCII */
+       if (!strcasecmp(src_code, CS_US_ASCII))
+               return g_strdup(inbuf);
+
+       /* don't convert if dest codeset is US-ASCII */
        if (!strcasecmp(dest_code, CS_US_ASCII))
                return g_strdup(inbuf);
 
@@ -884,7 +969,7 @@ gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
                } else if (E2BIG == errno) {
                        EXPAND_BUF();
                } else {
-                       g_warning("conv_iconv_strdup(): %s\n",
+                       g_warning("conv_iconv_strdup(): %s",
                                  g_strerror(errno));
                        break;
                }
@@ -895,7 +980,7 @@ gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
                if (E2BIG == errno) {
                        EXPAND_BUF();
                } else {
-                       g_warning("conv_iconv_strdup(): %s\n",
+                       g_warning("conv_iconv_strdup(): %s",
                                  g_strerror(errno));
                        break;
                }
@@ -952,6 +1037,7 @@ static const struct {
        {C_WINDOWS_1257,        CS_WINDOWS_1257},
        {C_WINDOWS_1258,        CS_WINDOWS_1258},
        {C_KOI8_R,              CS_KOI8_R},
+       {C_MACCYR,              CS_MACCYR},
        {C_KOI8_T,              CS_KOI8_T},
        {C_KOI8_U,              CS_KOI8_U},
        {C_ISO_2022_JP,         CS_ISO_2022_JP},
@@ -967,6 +1053,7 @@ static const struct {
        {C_EUC_KR,              CS_EUC_KR},
        {C_ISO_2022_CN,         CS_ISO_2022_CN},
        {C_EUC_CN,              CS_EUC_CN},
+       {C_GB18030,             CS_GB18030},
        {C_GB2312,              CS_GB2312},
        {C_GBK,                 CS_GBK},
        {C_EUC_TW,              CS_EUC_TW},
@@ -996,9 +1083,10 @@ static const struct {
 #endif
        {"ko_KR.EUC-KR" , C_EUC_KR      , C_EUC_KR},
        {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
+       {"zh_CN.GB18030"        , C_GB18030     , C_GB18030},
        {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
        {"zh_CN.GBK"    , C_GBK         , C_GBK},
-       {"zh_CN"        , C_GB2312      , C_GB2312},
+       {"zh_CN"        , C_GB18030     , C_GB18030},
        {"zh_HK"        , C_BIG5_HKSCS  , C_BIG5_HKSCS},
        {"zh_TW.eucTW"  , C_EUC_TW      , C_BIG5},
        {"zh_TW.EUC-TW" , C_EUC_TW      , C_BIG5},
@@ -1008,7 +1096,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},
@@ -1082,6 +1174,7 @@ static const struct {
        {"ms_MY"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"nl_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"nl_NL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"nb_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"nn_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"no_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"oc_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
@@ -1220,7 +1313,7 @@ CharSet conv_get_charset_from_str(const gchar *charset)
        return GPOINTER_TO_UINT(g_hash_table_lookup(table, charset));
 }
 
-CharSet conv_get_locale_charset(void)
+static CharSet conv_get_locale_charset(void)
 {
        static CharSet cur_charset = -1;
        const gchar *cur_locale;
@@ -1236,7 +1329,8 @@ CharSet conv_get_locale_charset(void)
                return cur_charset;
        }
 
-       if (strcasestr(cur_locale, "UTF-8")) {
+       if (strcasestr(cur_locale, "UTF-8") ||
+           strcasestr(cur_locale, "utf8")) {
                cur_charset = C_UTF_8;
                return cur_charset;
        }
@@ -1274,14 +1368,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) {
@@ -1289,10 +1381,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') {
@@ -1343,17 +1435,7 @@ const gchar *conv_get_locale_charset_str_no_utf8(void)
        return codeset ? codeset : CS_INTERNAL;
 }
 
-CharSet conv_get_internal_charset(void)
-{
-       return C_INTERNAL;
-}
-
-const gchar *conv_get_internal_charset_str(void)
-{
-       return CS_INTERNAL;
-}
-
-CharSet conv_get_outgoing_charset(void)
+static CharSet conv_get_outgoing_charset(void)
 {
        static CharSet out_charset = -1;
        const gchar *cur_locale;
@@ -1369,7 +1451,8 @@ 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;
        }
@@ -1410,31 +1493,6 @@ const gchar *conv_get_outgoing_charset_str(void)
        return str ? str : CS_UTF_8;
 }
 
-gboolean conv_is_multibyte_encoding(CharSet encoding)
-{
-       switch (encoding) {
-       case C_EUC_JP:
-       case C_EUC_JP_MS:
-       case C_EUC_KR:
-       case C_EUC_TW:
-       case C_EUC_CN:
-       case C_ISO_2022_JP:
-       case C_ISO_2022_JP_2:
-       case C_ISO_2022_JP_3:
-       case C_ISO_2022_KR:
-       case C_ISO_2022_CN:
-       case C_SHIFT_JIS:
-       case C_GB2312:
-       case C_GBK:
-       case C_BIG5:
-       case C_UTF_8:
-       case C_UTF_7:
-               return TRUE;
-       default:
-               return FALSE;
-       }
-}
-
 const gchar *conv_get_current_locale(void)
 {
        const gchar *cur_locale;
@@ -1454,7 +1512,7 @@ const gchar *conv_get_current_locale(void)
        return cur_locale;
 }
 
-gboolean conv_is_ja_locale(void)
+static gboolean conv_is_ja_locale(void)
 {
        static gint is_ja_locale = -1;
        const gchar *cur_locale;
@@ -1472,12 +1530,13 @@ 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;
@@ -1487,7 +1546,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;
                }
@@ -1498,7 +1557,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
@@ -1524,10 +1583,20 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
                                *destp++ = ' ';                         \
                                left = MAX_LINELEN - 1;                 \
                        }                                               \
+               } else if (destp == (guchar *)dest && left < 7) {       \
+                       if (is_plain_text && isspace(*srcp))            \
+                               srcp++;                                 \
+                       if (*srcp) {                                    \
+                               *destp++ = '\n';                        \
+                               *destp++ = ' ';                         \
+                               left = MAX_LINELEN - 1;                 \
+                       }                                               \
                }                                                       \
        }                                                               \
 }
 
+#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_)
@@ -1541,7 +1610,8 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
        guchar *destp = dest;
        gboolean use_base64;
 
-       g_return_if_fail(g_utf8_validate(src, -1, NULL) == TRUE);
+       cm_return_if_fail(g_utf8_validate(src, -1, NULL) == TRUE);
+       cm_return_if_fail(destp != NULL);
 
        if (MB_CUR_MAX > 1) {
                use_base64 = TRUE;
@@ -1591,8 +1661,8 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                        continue;
                }
 
-               /* don't include parentheses in encoded strings */
-               if (addr_field && (*srcp == '(' || *srcp == ')')) {
+               /* don't include parentheses and quotes in encoded strings */
+               if (addr_field && (*srcp == '(' || *srcp == ')' || *srcp == '"')) {
                        LBREAK_IF_REQUIRED(left < 2, FALSE);
                        *destp++ = *srcp++;
                        left--;
@@ -1615,7 +1685,7 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                                        break;
                                /* don't include parentheses in encoded
                                   strings */
-                               if (addr_field && (*p == '(' || *p == ')'))
+                               if (addr_field && (*p == '(' || *p == ')' || *p == '"'))
                                        break;
 
                                mb_len = g_utf8_skip[*p];
@@ -1628,7 +1698,7 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                                                *dest = '\0';
                                                return;
                                        } else {
-                                               g_warning("conv_encode_header(): code conversion failed\n");
+                                               g_warning("conv_encode_header(): code conversion failed");
                                                conv_unreadable_8bit(part_str);
                                                out_str = g_strdup(part_str);
                                        }
@@ -1647,6 +1717,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 {
@@ -1660,7 +1731,7 @@ void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
                                out_str = conv_codeset_strdup
                                        (part_str, cur_encoding, out_encoding);
                                if (!out_str) {
-                                       g_warning("conv_encode_header(): code conversion failed\n");
+                                       g_warning("conv_encode_header(): code conversion failed");
                                        conv_unreadable_8bit(part_str);
                                        out_str = g_strdup(part_str);
                                }
@@ -1672,11 +1743,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);
 
@@ -1685,6 +1757,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;
 
@@ -1708,6 +1784,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;
@@ -1715,7 +1793,7 @@ gchar *conv_filename_from_utf8(const gchar *utf8_file)
 
        fs_file = g_filename_from_utf8(utf8_file, -1, NULL, NULL, &error);
        if (error) {
-               g_warning("failed to convert encoding of file name: %s\n",
+               debug_print("failed to convert encoding of file name: %s\n",
                          error->message);
                g_error_free(error);
        }
@@ -1732,7 +1810,7 @@ gchar *conv_filename_to_utf8(const gchar *fs_file)
 
        utf8_file = g_filename_to_utf8(fs_file, -1, NULL, NULL, &error);
        if (error) {
-               g_warning("failed to convert encoding of file name: %s\n",
+               g_warning("failed to convert encoding of file name: %s",
                          error->message);
                g_error_free(error);
        }