2008-09-24 [colin] 3.5.0cvs127
[claws.git] / src / codeconv.c
index 3e7676b11b0f82f822078b61b471729c888348ec..64d88283fe80a113ecc37f41d2af0e7202d83a73 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-2007 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,
@@ -13,8 +13,8 @@
  * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -34,8 +34,6 @@
 #  include <locale.h>
 #endif
 
-#include <iconv.h>
-
 #include "codeconv.h"
 #include "unmime.h"
 #include "base64.h"
 #include "utils.h"
 #include "prefs_common.h"
 
+/* For unknown reasons the inconv.m4 macro undefs that macro if no
+   const is needed.  This would break the code below so we define it. */
+#ifndef ICONV_CONST
+#define ICONV_CONST
+#endif
+
 typedef enum
 {
        JIS_ASCII,
@@ -104,6 +108,21 @@ typedef enum
                state = JIS_AUXKANJI;   \
        }
 
+static CodeConvFunc conv_get_code_conv_func    (const gchar    *src_charset_str,
+                                        const gchar    *dest_charset_str);
+
+static gchar *conv_iconv_strdup_with_cd        (const gchar    *inbuf,
+                                        iconv_t         cd);
+
+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 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);
@@ -126,6 +145,13 @@ 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 gboolean strict_mode = FALSE;
+
+void codeconv_set_strict(gboolean mode)
+{
+       strict_mode = mode;
+}
+
 static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
@@ -522,73 +548,7 @@ static void conv_unreadable_8bit(gchar *str)
        }
 }
 
-#define NCV    '\0'
-
-void conv_mb_alnum(gchar *str)
-{
-       static guchar char_tbl[] = {
-               /* 0xa0 - 0xaf */
-               NCV, ' ', NCV, NCV, ',', '.', NCV, ':',
-               ';', '?', '!', NCV, NCV, NCV, NCV, NCV,
-               /* 0xb0 - 0xbf */
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
-               /* 0xc0 - 0xcf */
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
-               NCV, NCV, '(', ')', NCV, NCV, '[', ']',
-               /* 0xd0 - 0xdf */
-               '{', '}', NCV, NCV, NCV, NCV, NCV, NCV,
-               NCV, NCV, NCV, NCV, '+', '-', NCV, NCV,
-               /* 0xe0 - 0xef */
-               NCV, '=', NCV, '<', '>', NCV, NCV, NCV,
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV
-       };
-
-       register guchar *p = str;
-       register gint len;
-
-       len = strlen(str);
-
-       while (len > 1) {
-               if (*p == 0xa3) {
-                       register guchar ch = *(p + 1);
-
-                       if (ch >= 0xb0 && ch <= 0xfa) {
-                               /* [a-zA-Z] */
-                               *p = ch & 0x7f;
-                               p++;
-                               len--;
-                               memmove(p, p + 1, len);
-                               len--;
-                       } else  {
-                               p += 2;
-                               len -= 2;
-                       }
-               } else if (*p == 0xa1) {
-                       register guchar ch = *(p + 1);
-
-                       if (ch >= 0xa0 && ch <= 0xef &&
-                           NCV != char_tbl[ch - 0xa0]) {
-                               *p = char_tbl[ch - 0xa0];
-                               p++;
-                               len--;
-                               memmove(p, p + 1, len);
-                               len--;
-                       } else {
-                               p += 2;
-                               len -= 2;
-                       }
-               } else if (iseuckanji(*p)) {
-                       p += 2;
-                       len -= 2;
-               } else {
-                       p++;
-                       len--;
-               }
-       }
-}
-
-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;
@@ -670,16 +630,30 @@ void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        gchar *tmpstr;
 
+       codeconv_set_strict(TRUE);
        tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
                                   CS_INTERNAL);
+       codeconv_set_strict(FALSE);
        if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
+               return;
        } else if (tmpstr && !g_utf8_validate(tmpstr, -1, NULL)) {
+               g_free(tmpstr);
+               codeconv_set_strict(TRUE);
+               tmpstr = conv_iconv_strdup(inbuf, 
+                               conv_get_locale_charset_str_no_utf8(),
+                               CS_INTERNAL);
+               codeconv_set_strict(FALSE);
+       }
+       if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+               return;
+       } else {
                g_free(tmpstr);
                conv_utf8todisp(outbuf, outlen, inbuf);
-       } else
-               conv_utf8todisp(outbuf, outlen, inbuf);
+       }
 }
 
 static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
@@ -746,6 +720,9 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_func;
 
+       if (!strcmp2(src_code, dest_code))
+               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_noconv) {
@@ -760,7 +737,7 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        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;
@@ -845,12 +822,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)
@@ -860,7 +841,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);
 
@@ -907,6 +892,10 @@ gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
        while ((n_conv = iconv(cd, (ICONV_CONST gchar **)&inbuf_p, &in_left,
                               &outbuf_p, &out_left)) == (size_t)-1) {
                if (EILSEQ == errno) {
+                       if (strict_mode) {
+                               g_free(outbuf);
+                               return NULL;
+                       }
                        //g_print("iconv(): at %d: %s\n", in_size - in_left, g_strerror(errno));
                        inbuf_p++;
                        in_left--;
@@ -1118,6 +1107,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},
@@ -1256,7 +1246,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;
@@ -1272,7 +1262,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;
        }
@@ -1305,6 +1296,60 @@ CharSet conv_get_locale_charset(void)
        return cur_charset;
 }
 
+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)
+               return cur_charset;
+
+       cur_locale = conv_get_current_locale();
+       if (!cur_locale) {
+               cur_charset = C_US_ASCII;
+               return cur_charset;
+       }
+
+       if (strcasestr(cur_locale, "UTF-8")) {
+               tmp = g_strdup(cur_locale);
+               *(strcasestr(tmp, ".UTF-8")) = '\0';
+               cur_locale = tmp;
+       }
+
+       if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
+               cur_charset = C_ISO_8859_15;
+               return cur_charset;
+       }
+
+       for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
+               const gchar *p;
+
+               /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
+                  "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
+               if (!g_ascii_strncasecmp(cur_locale, locale_table[i].locale,
+                                strlen(locale_table[i].locale))) {
+                       cur_charset = locale_table[i].charset;
+                       return cur_charset;
+               } else if ((p = strchr(locale_table[i].locale, '_')) &&
+                        !strchr(p + 1, '.')) {
+                       if (strlen(cur_locale) == 2 &&
+                           !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                               cur_charset = locale_table[i].charset;
+                               return cur_charset;
+                       }
+               }
+       }
+
+       cur_charset = C_AUTO;
+       return cur_charset;
+}
+
 const gchar *conv_get_locale_charset_str(void)
 {
        static const gchar *codeset = NULL;
@@ -1315,17 +1360,17 @@ const gchar *conv_get_locale_charset_str(void)
        return codeset ? codeset : CS_INTERNAL;
 }
 
-CharSet conv_get_internal_charset(void)
+const gchar *conv_get_locale_charset_str_no_utf8(void)
 {
-       return C_INTERNAL;
-}
+       static const gchar *codeset = NULL;
 
-const gchar *conv_get_internal_charset_str(void)
-{
-       return CS_INTERNAL;
+       if (!codeset)
+               codeset = conv_get_charset_str(conv_get_locale_charset_no_utf8());
+
+       return codeset ? codeset : CS_INTERNAL;
 }
 
-CharSet conv_get_outgoing_charset(void)
+static CharSet conv_get_outgoing_charset(void)
 {
        static CharSet out_charset = -1;
        const gchar *cur_locale;
@@ -1382,31 +1427,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;
@@ -1426,7 +1446,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;
@@ -1563,8 +1583,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--;
@@ -1587,7 +1607,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];
@@ -1596,9 +1616,14 @@ 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");
-                                       conv_unreadable_8bit(part_str);
-                                       out_str = g_strdup(part_str);
+                                       if (strict_mode) {
+                                               *dest = '\0';
+                                               return;
+                                       } else {
+                                               g_warning("conv_encode_header(): code conversion failed\n");
+                                               conv_unreadable_8bit(part_str);
+                                               out_str = g_strdup(part_str);
+                                       }
                                }
                                out_str_len = strlen(out_str);