Fix wrong test leading to dead code (and dead func), CID 1438531.
[claws.git] / src / codeconv.c
index 98f5e32df9f11b4460f48e6caf8a9dd26871c137..0865f2c1acbac24701f59ae767794112c8e4a682 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2011 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"
@@ -155,10 +155,16 @@ void codeconv_set_strict(gboolean mode)
 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') {
+       cm_return_val_if_fail(outbuf != NULL, 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 == '$') {
@@ -258,6 +264,8 @@ static gint conv_jis_hantozen(guchar *outbuf, guchar jis_code, guchar sound_sym)
 
        guint16 out_code;
 
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        jis_code &= 0x7f;
        sound_sym &= 0x7f;
 
@@ -291,10 +299,17 @@ static gint conv_jis_hantozen(guchar *outbuf, guchar jis_code, guchar sound_sym)
 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') {
+       cm_return_val_if_fail(outbuf != NULL, 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++;
@@ -380,9 +395,15 @@ static gint conv_euctojis(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') {
+       cm_return_val_if_fail(outbuf != NULL, 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)) {
@@ -428,6 +449,9 @@ static gint conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *eucstr;
 
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        Xalloca(eucstr, outlen, return -1);
 
        if (conv_jistoeuc(eucstr, outlen, inbuf) <0)
@@ -441,6 +465,9 @@ static gint conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *tmpstr;
 
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        tmpstr = conv_iconv_strdup(inbuf, CS_SHIFT_JIS, CS_UTF_8);
        if (tmpstr) {
                strncpy2(outbuf, tmpstr, outlen);
@@ -458,6 +485,9 @@ static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
        static gboolean iconv_ok = TRUE;
        gchar *tmpstr;
 
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        if (cd == (iconv_t)-1) {
                if (!iconv_ok) {
                        strncpy2(outbuf, inbuf, outlen);
@@ -467,7 +497,7 @@ static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
                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);
@@ -490,6 +520,10 @@ static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 static gint conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gint r = -1;
+
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        switch (conv_guess_ja_encoding(inbuf)) {
        case C_ISO_2022_JP:
                r = conv_jistoutf8(outbuf, outlen, inbuf);
@@ -515,6 +549,9 @@ static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        static gboolean iconv_ok = TRUE;
        gchar *tmpstr;
 
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        if (cd == (iconv_t)-1) {
                if (!iconv_ok) {
                        strncpy2(outbuf, inbuf, outlen);
@@ -524,7 +561,7 @@ static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
                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);
@@ -548,6 +585,9 @@ static gint conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *eucstr;
 
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        Xalloca(eucstr, outlen, return -1);
 
        if (conv_utf8toeuc(eucstr, outlen, inbuf) < 0)
@@ -615,21 +655,33 @@ static CharSet conv_guess_ja_encoding(const gchar *str)
 
 static gint conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        return conv_jistoutf8(outbuf, outlen, inbuf);
 }
 
 static gint conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        return conv_sjistoutf8(outbuf, outlen, inbuf);
 }
 
 static gint conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        return conv_euctoutf8(outbuf, outlen, inbuf);
 }
 
 void conv_utf8todisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       cm_return_if_fail(inbuf != NULL);
+       cm_return_if_fail(outbuf != NULL);
+
        if (g_utf8_validate(inbuf, -1, NULL) == TRUE)
                strncpy2(outbuf, inbuf, outlen);
        else
@@ -639,6 +691,10 @@ void conv_utf8todisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gint r = 0;
+
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        if (conv_anytoutf8(outbuf, outlen, inbuf) < 0)
                r = -1;
        if (g_utf8_validate(outbuf, -1, NULL) != TRUE)
@@ -648,6 +704,9 @@ static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 
 static gint conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        strncpy2(outbuf, inbuf, outlen);
        conv_unreadable_8bit(outbuf);
        
@@ -658,6 +717,9 @@ void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *tmpstr;
 
+       cm_return_if_fail(inbuf != NULL);
+       cm_return_if_fail(outbuf != NULL);
+
        codeconv_set_strict(TRUE);
        tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
                                   CS_INTERNAL);
@@ -686,6 +748,9 @@ void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 
 static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       cm_return_val_if_fail(inbuf != NULL, 0);
+       cm_return_val_if_fail(outbuf != NULL, 0);
+
        strncpy2(outbuf, inbuf, outlen);
        return 0;
 }
@@ -693,10 +758,24 @@ static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
 static const gchar *
 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_GBK))
-                       return CS_GBK;
+       if (encoding) {
+               if ((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;
+               }
+               else if(!g_ascii_strcasecmp(encoding, CS_ISO_8859_8_I)) {
+                       /*
+                        * ISO-8859-8-I is a variant which fully
+                        * agrees with ISO-8859-8 on character
+                        * codings, and differs only in directionality
+                        * implications, which are ignored here
+                        * anyway; and is not recognized by iconv
+                        */
+                       return CS_ISO_8859_8;
+               }
        }
 
        return encoding;
@@ -725,6 +804,9 @@ void conv_code_converter_destroy(CodeConverter *conv)
 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                  const gchar *inbuf)
 {
+       cm_return_val_if_fail(inbuf != NULL, -1);
+       cm_return_val_if_fail(outbuf != NULL, -1);
+
        if (conv->code_conv_func != conv_noconv)
                return conv->code_conv_func(outbuf, outlen, inbuf);
        else {
@@ -749,8 +831,19 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_func;
 
-       if (!strcmp2(src_code, dest_code))
+       cm_return_val_if_fail(inbuf != NULL, NULL);
+
+       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);
@@ -760,7 +853,6 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        if (conv_func != conv_noconv) {
                len = (strlen(inbuf) + 1) * 3;
                buf = g_malloc(len);
-               if (!buf) return NULL;
 
                if (conv_func(buf, len, inbuf) == 0 || !strict_mode)
                        return g_realloc(buf, strlen(buf) + 1);
@@ -864,6 +956,8 @@ static gchar *conv_iconv_strdup(const gchar *inbuf,
        iconv_t cd;
        gchar *outbuf;
 
+       cm_return_val_if_fail(inbuf != NULL, NULL);
+
        if (!src_code && !dest_code && 
            g_utf8_validate(inbuf, -1, NULL))
                return g_strdup(inbuf);
@@ -908,6 +1002,8 @@ gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
        size_t n_conv;
        size_t len;
 
+       cm_return_val_if_fail(inbuf != NULL, NULL);
+
        inbuf_p = inbuf;
        in_size = strlen(inbuf);
        in_left = in_size;
@@ -945,7 +1041,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;
                }
@@ -956,7 +1052,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;
                }
@@ -1013,6 +1109,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},
@@ -1045,131 +1142,131 @@ static const struct {
        CharSet charset;
        CharSet out_charset;
 } locale_table[] = {
-       {"ja_JP.eucJP"  , C_EUC_JP      , C_ISO_2022_JP},
-       {"ja_JP.EUC-JP" , C_EUC_JP      , C_ISO_2022_JP},
-       {"ja_JP.EUC"    , C_EUC_JP      , C_ISO_2022_JP},
-       {"ja_JP.ujis"   , C_EUC_JP      , C_ISO_2022_JP},
-       {"ja_JP.SJIS"   , C_SHIFT_JIS   , C_ISO_2022_JP},
-       {"ja_JP.JIS"    , C_ISO_2022_JP , C_ISO_2022_JP},
+       {"ja_JP.eucJP"          , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.EUC-JP"         , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.EUC"            , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.ujis"           , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.SJIS"           , C_SHIFT_JIS   , C_ISO_2022_JP},
+       {"ja_JP.JIS"            , C_ISO_2022_JP , C_ISO_2022_JP},
 #ifdef G_OS_WIN32
-       {"ja_JP"        , C_SHIFT_JIS   , C_ISO_2022_JP},
+       {"ja_JP"                , C_SHIFT_JIS   , C_ISO_2022_JP},
 #else
-       {"ja_JP"        , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP"                , C_EUC_JP      , C_ISO_2022_JP},
 #endif
-       {"ko_KR.EUC-KR" , C_EUC_KR      , C_EUC_KR},
-       {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
+       {"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_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},
-       {"zh_TW.Big5"   , C_BIG5        , C_BIG5},
-       {"zh_TW"        , C_BIG5        , C_BIG5},
-
-       {"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},
+       {"zh_CN.GB2312"         , C_GB2312      , C_GB2312},
+       {"zh_CN.GBK"            , C_GBK         , C_GBK},
+       {"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},
+       {"zh_TW.Big5"           , C_BIG5        , C_BIG5},
+       {"zh_TW"                , C_BIG5        , C_BIG5},
+
+       {"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},
+       {"ru_RU"                , C_WINDOWS_1251, C_KOI8_R},
 #else
-       {"ru_RU"        , C_ISO_8859_5  , C_KOI8_R},
+       {"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},
-       {"uk_UA"        , C_KOI8_U      , C_KOI8_U},
-
-       {"be_BY"        , C_WINDOWS_1251, C_WINDOWS_1251},
-       {"bg_BG"        , C_WINDOWS_1251, C_WINDOWS_1251},
-
-       {"yi_US"        , C_WINDOWS_1255, C_WINDOWS_1255},
-
-       {"af_ZA"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"br_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"ca_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"da_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"de_AT"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"de_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"de_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"de_DE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"de_LU"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_AU"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_BW"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_CA"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_HK"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_IE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_NZ"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_PH"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_SG"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_US"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_ZA"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"en_ZW"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_AR"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_BO"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_CL"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_CO"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_CR"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_DO"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_EC"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_GT"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_HN"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_MX"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_NI"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_PA"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_PE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_PR"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_PY"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_SV"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_US"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_UY"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_VE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"et_EE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"eu_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"fi_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"fo_FO"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"fr_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"fr_CA"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"fr_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"fr_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"fr_LU"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"ga_IE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"gl_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"gv_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"id_ID"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"is_IS"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"it_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"it_IT"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"kl_GL"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"kw_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"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},
-       {"pt_BR"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"pt_PT"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"sq_AL"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"sv_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"sv_SE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"tl_PH"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"uz_UZ"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"wa_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
-
-       {"bs_BA"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"cs_CZ"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"hr_HR"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"hu_HU"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"pl_PL"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"ro_RO"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"sk_SK"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"sl_SI"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"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},
+       {"uk_UA"                , C_KOI8_U      , C_KOI8_U},
+
+       {"be_BY"                , C_WINDOWS_1251, C_WINDOWS_1251},
+       {"bg_BG"                , C_WINDOWS_1251, C_WINDOWS_1251},
+
+       {"yi_US"                , C_WINDOWS_1255, C_WINDOWS_1255},
+
+       {"af_ZA"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"br_FR"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"ca_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"da_DK"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_AT"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_BE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_CH"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_DE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_LU"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_AU"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_BW"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_CA"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_DK"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_GB"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_HK"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_IE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_NZ"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_PH"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_SG"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_US"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_ZA"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_ZW"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_AR"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_BO"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CL"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CO"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CR"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_DO"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_EC"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_GT"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_HN"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_MX"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_NI"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PA"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PR"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PY"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_SV"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_US"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_UY"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_VE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"et_EE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"eu_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fi_FI"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fo_FO"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_BE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_CA"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_CH"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_FR"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_LU"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"ga_IE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"gl_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"gv_GB"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"id_ID"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"is_IS"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"it_CH"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"it_IT"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"kl_GL"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"kw_GB"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"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},
+       {"pt_BR"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"pt_PT"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sq_AL"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sv_FI"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sv_SE"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"tl_PH"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"uz_UZ"                , C_ISO_8859_1  , C_ISO_8859_1},
+       {"wa_BE"                , C_ISO_8859_1  , C_ISO_8859_1},
+
+       {"bs_BA"                , C_ISO_8859_2  , C_ISO_8859_2},
+       {"cs_CZ"                , C_ISO_8859_2  , C_ISO_8859_2},
+       {"hr_HR"                , C_ISO_8859_2  , C_ISO_8859_2},
+       {"hu_HU"                , C_ISO_8859_2  , C_ISO_8859_2},
+       {"pl_PL"                , C_ISO_8859_2  , C_ISO_8859_2},
+       {"ro_RO"                , C_ISO_8859_2  , C_ISO_8859_2},
+       {"sk_SK"                , C_ISO_8859_2  , C_ISO_8859_2},
+       {"sl_SI"                , C_ISO_8859_2  , C_ISO_8859_2},
 
        {"sr_YU@cyrillic"       , C_ISO_8859_5  , C_ISO_8859_5},
        {"sr_YU"                , C_ISO_8859_2  , C_ISO_8859_2},
@@ -1181,49 +1278,49 @@ static const struct {
        {"lt_LT.ISO_8859-4"     , C_ISO_8859_4  , C_ISO_8859_4},
        {"lt_LT"                , C_ISO_8859_13 , C_ISO_8859_13},
 
-       {"mk_MK"        , C_ISO_8859_5  , C_ISO_8859_5},
-
-       {"ar_AE"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_BH"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_DZ"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_EG"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_IQ"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_JO"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_KW"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_LB"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_LY"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_MA"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_OM"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_QA"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_SA"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_SD"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_SY"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_TN"        , C_ISO_8859_6  , C_ISO_8859_6},
-       {"ar_YE"        , C_ISO_8859_6  , C_ISO_8859_6},
-
-       {"el_GR"        , C_ISO_8859_7  , C_ISO_8859_7},
-       {"he_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
-       {"iw_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
-       {"tr_TR"        , C_ISO_8859_9  , C_ISO_8859_9},
-
-       {"lv_LV"        , C_ISO_8859_13 , C_ISO_8859_13},
-       {"mi_NZ"        , C_ISO_8859_13 , C_ISO_8859_13},
-
-       {"cy_GB"        , C_ISO_8859_14 , C_ISO_8859_14},
-
-       {"ar_IN"        , C_UTF_8       , C_UTF_8},
-       {"en_IN"        , C_UTF_8       , C_UTF_8},
-       {"se_NO"        , C_UTF_8       , C_UTF_8},
-       {"ta_IN"        , C_UTF_8       , C_UTF_8},
-       {"te_IN"        , C_UTF_8       , C_UTF_8},
-       {"ur_PK"        , C_UTF_8       , C_UTF_8},
-
-       {"th_TH"        , C_TIS_620     , C_TIS_620},
-       /* {"th_TH"     , C_WINDOWS_874}, */
-       /* {"th_TH"     , C_ISO_8859_11}, */
-
-       {"ka_GE"        , C_GEORGIAN_PS , C_GEORGIAN_PS},
-       {"vi_VN.TCVN"   , C_TCVN5712_1  , C_TCVN5712_1},
+       {"mk_MK"                , C_ISO_8859_5  , C_ISO_8859_5},
+
+       {"ar_AE"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_BH"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_DZ"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_EG"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_IQ"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_JO"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_KW"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_LB"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_LY"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_MA"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_OM"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_QA"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SA"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SD"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SY"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_TN"                , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_YE"                , C_ISO_8859_6  , C_ISO_8859_6},
+
+       {"el_GR"                , C_ISO_8859_7  , C_ISO_8859_7},
+       {"he_IL"                , C_ISO_8859_8  , C_ISO_8859_8},
+       {"iw_IL"                , C_ISO_8859_8  , C_ISO_8859_8},
+       {"tr_TR"                , C_ISO_8859_9  , C_ISO_8859_9},
+
+       {"lv_LV"                , C_ISO_8859_13 , C_ISO_8859_13},
+       {"mi_NZ"                , C_ISO_8859_13 , C_ISO_8859_13},
+
+       {"cy_GB"                , C_ISO_8859_14 , C_ISO_8859_14},
+
+       {"ar_IN"                , C_UTF_8       , C_UTF_8},
+       {"en_IN"                , C_UTF_8       , C_UTF_8},
+       {"se_NO"                , C_UTF_8       , C_UTF_8},
+       {"ta_IN"                , C_UTF_8       , C_UTF_8},
+       {"te_IN"                , C_UTF_8       , C_UTF_8},
+       {"ur_PK"                , C_UTF_8       , C_UTF_8},
+
+       {"th_TH"                , C_TIS_620     , C_TIS_620},
+       /* {"th_TH"             , C_WINDOWS_874}, */
+       /* {"th_TH"             , C_ISO_8859_11}, */
+
+       {"ka_GE"                , C_GEORGIAN_PS , C_GEORGIAN_PS},
+       {"vi_VN.TCVN"           , C_TCVN5712_1  , C_TCVN5712_1},
 
        {"C"                    , C_US_ASCII    , C_US_ASCII},
        {"POSIX"                , C_US_ASCII    , C_US_ASCII},
@@ -1290,12 +1387,12 @@ CharSet conv_get_charset_from_str(const gchar *charset)
 
 static CharSet conv_get_locale_charset(void)
 {
-       static CharSet cur_charset = -1;
+       static CharSet cur_charset = C_UNINITIALIZED;
        const gchar *cur_locale;
        const gchar *p;
        gint i;
 
-       if (cur_charset != -1)
+       if (cur_charset != C_UNINITIALIZED)
                return cur_charset;
 
        cur_locale = conv_get_current_locale();
@@ -1304,8 +1401,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;
        }
@@ -1340,17 +1437,15 @@ static CharSet conv_get_locale_charset(void)
 
 static CharSet conv_get_locale_charset_no_utf8(void)
 {
-       static CharSet cur_charset = -1;
+       static CharSet cur_charset = C_UNINITIALIZED;
        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) {
@@ -1358,10 +1453,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') {
@@ -1414,12 +1509,12 @@ const gchar *conv_get_locale_charset_str_no_utf8(void)
 
 static CharSet conv_get_outgoing_charset(void)
 {
-       static CharSet out_charset = -1;
+       static CharSet out_charset = C_UNINITIALIZED;
        const gchar *cur_locale;
        const gchar *p;
        gint i;
 
-       if (out_charset != -1)
+       if (out_charset != C_UNINITIALIZED)
                return out_charset;
 
        cur_locale = conv_get_current_locale();
@@ -1428,7 +1523,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;
        }
@@ -1511,6 +1607,8 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding,
 {
        gchar buf[BUFFSIZE];
 
+       cm_return_val_if_fail(str != NULL, NULL);
+
        if (is_ascii_str(str))
                return unmime_header(str, addr_field);
 
@@ -1560,9 +1658,7 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding,
                                left = MAX_LINELEN - 1;                 \
                        }                                               \
                } else if (destp == (guchar *)dest && left < 7) {       \
-                       if (isspace(*(destp - 1)))                      \
-                               destp--;                                \
-                       else if (is_plain_text && isspace(*srcp))       \
+                       if (is_plain_text && isspace(*srcp))            \
                                srcp++;                                 \
                        if (*srcp) {                                    \
                                *destp++ = '\n';                        \
@@ -1573,6 +1669,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_)
@@ -1674,7 +1772,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_full(): code conversion failed");
                                                conv_unreadable_8bit(part_str);
                                                out_str = g_strdup(part_str);
                                        }
@@ -1693,6 +1791,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 {
@@ -1706,7 +1805,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_full(): code conversion failed");
                                        conv_unreadable_8bit(part_str);
                                        out_str = g_strdup(part_str);
                                }
@@ -1718,11 +1817,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);
 
@@ -1731,6 +1831,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;
 
@@ -1754,11 +1858,15 @@ 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;
        GError *error = NULL;
 
+       cm_return_val_if_fail(utf8_file != NULL, NULL);
+
        fs_file = g_filename_from_utf8(utf8_file, -1, NULL, NULL, &error);
        if (error) {
                debug_print("failed to convert encoding of file name: %s\n",
@@ -1776,9 +1884,11 @@ gchar *conv_filename_to_utf8(const gchar *fs_file)
        gchar *utf8_file = NULL;
        GError *error = NULL;
 
+       cm_return_val_if_fail(fs_file != NULL, NULL);
+
        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);
        }