sync with 0.8.10cvs23
[claws.git] / src / codeconv.c
index 166e5307308a620379f72c935c14d8b195bd46e2..15c3a5f3b3f1e9ee529025a7e372ef36112b62e2 100644 (file)
@@ -62,8 +62,6 @@ typedef enum
        (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
 #define iseucaux(c) \
        (((c) & 0xff) == 0x8f)
-#define isunprintableeuckanji(c) \
-       (((c) & 0xff) >= 0xa9 && ((c) & 0xff) <= 0xaf)
 #define issjiskanji1(c) \
        ((((c) & 0xff) >= 0x81 && ((c) & 0xff) <= 0x9f) || \
         (((c) & 0xff) >= 0xe0 && ((c) & 0xff) <= 0xfc))
@@ -287,7 +285,7 @@ void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 
 void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       switch (conv_guess_encoding(inbuf)) {
+       switch (conv_guess_ja_encoding(inbuf)) {
        case C_ISO_2022_JP:
                conv_jistoeuc(outbuf, outlen, inbuf);
                break;
@@ -302,7 +300,7 @@ void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 
 void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       switch (conv_guess_encoding(inbuf)) {
+       switch (conv_guess_ja_encoding(inbuf)) {
        case C_EUC_JP:
                conv_euctojis(outbuf, outlen, inbuf);
                break;
@@ -312,6 +310,88 @@ void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 }
 
+static gchar valid_eucjp_tbl[][96] = {
+       /* 0xa2a0 - 0xa2ff */
+       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 0,
+         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 1, 1, 1, 1, 1, 1,
+         1, 1, 0, 0, 0, 0, 0, 0,  0, 0, 1, 1, 1, 1, 1, 1,
+         1, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
+         0, 0, 1, 1, 1, 1, 1, 1,  1, 1, 0, 0, 0, 0, 1, 0 },
+
+       /* 0xa3a0 - 0xa3ff */
+       { 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 0, 0, 0, 0, 0, 0,
+         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
+         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0 },
+
+       /* 0xa4a0 - 0xa4ff */
+       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
+
+       /* 0xa5a0 - 0xa5ff */
+       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
+
+       /* 0xa6a0 - 0xa6ff */
+       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 0, 0, 0, 0, 0, 0, 0,
+         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 0, 0, 0, 0, 0, 0, 0,
+         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
+         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
+
+       /* 0xa7a0 - 0xa7ff */
+       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
+         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
+
+       /* 0xa8a0 - 0xa8ff */
+       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
+         1, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
+         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
+         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
+         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }
+};
+
+static gboolean isprintableeuckanji(guchar c1, guchar c2)
+{
+       if (c1 <= 0xa0 || c1 >= 0xf5)
+               return FALSE;
+       if (c2 <= 0xa0 || c2 == 0xff)
+               return FALSE;
+
+       if (c1 >= 0xa9 && c1 <= 0xaf)
+               return FALSE;
+
+       if (c1 >= 0xa2 && c1 <= 0xa8)
+               return (gboolean)valid_eucjp_tbl[c1 - 0xa2][c2 - 0xa0];
+
+       if (c1 == 0xcf) {
+               if (c2 >= 0xd4 && c2 <= 0xff)
+                       return FALSE;
+       } else if (c1 == 0xf4) {
+               if (c2 >= 0xa7 && c2 <= 0xff)
+                       return FALSE;
+       }
+
+       return TRUE;
+}
+
 void conv_unreadable_eucjp(gchar *str)
 {
        register guchar *p = str;
@@ -324,10 +404,10 @@ void conv_unreadable_eucjp(gchar *str)
                        /* printable 7 bit code */
                        p++;
                } else if (iseuckanji(*p)) {
-                       if (iseuckanji(*(p + 1)) && !isunprintableeuckanji(*p))
+                       if (isprintableeuckanji(*p, *(p + 1))) {
                                /* printable euc-jp code */
                                p += 2;
-                       else {
+                       else {
                                /* substitute unprintable code */
                                *p++ = SUBST_CHAR;
                                if (*p != '\0') {
@@ -376,7 +456,7 @@ void conv_unreadable_latin(gchar *str)
                /* convert CR+LF -> LF */
                if (*p == '\r' && *(p + 1) == '\n')
                        memmove(p, p + 1, strlen(p));
-               else if ((*p & 0xff) >= 0x80 && (*p & 0xff) <= 0x9f)
+               else if ((*p & 0xff) >= 0x7f && (*p & 0xff) <= 0x9f)
                        *p = SUBST_CHAR;
                p++;
        }
@@ -448,7 +528,7 @@ void conv_mb_alnum(gchar *str)
        }
 }
 
-CharSet conv_guess_encoding(const gchar *str)
+CharSet conv_guess_ja_encoding(const gchar *str)
 {
        const guchar *p = str;
        CharSet guessed = C_US_ASCII;
@@ -531,6 +611,32 @@ void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
        strncpy2(outbuf, inbuf, outlen);
 }
 
+void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+{
+       strncpy2(outbuf, inbuf, outlen);
+
+       switch (conv_get_current_charset()) {
+       case C_US_ASCII:
+       case C_ISO_8859_1:
+       case C_ISO_8859_2:
+       case C_ISO_8859_4:
+       case C_ISO_8859_5:
+       case C_ISO_8859_7:
+       case C_ISO_8859_8:
+       case C_ISO_8859_9:
+       case C_ISO_8859_11:
+       case C_ISO_8859_13:
+       case C_ISO_8859_15:
+               conv_unreadable_latin(outbuf);
+               break;
+       case C_EUC_JP:
+               conv_unreadable_eucjp(outbuf);
+               break;
+       default:
+               break;
+       }
+}
+
 CodeConverter *conv_code_converter_new(const gchar *charset)
 {
        CodeConverter *conv;
@@ -558,7 +664,7 @@ gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
        else {
                gchar *str;
 
-               str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
+               str = conv_iconv_strdup(inbuf, conv->charset_str, NULL);
                if (!str)
                        return -1;
                else {
@@ -591,19 +697,6 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        }
 
 #if HAVE_ICONV
-       if (!src_code)
-               src_code = conv_get_outgoing_charset_str();
-       if (!dest_code)
-               dest_code = conv_get_current_charset_str();
-
-       /* don't convert if current codeset is US-ASCII */
-       if (!strcasecmp(dest_code, CS_US_ASCII))
-               return g_strdup(inbuf);
-
-       /* don't convert if src and dest codeset are identical */
-       if (!strcasecmp(src_code, dest_code))
-               return g_strdup(inbuf);
-
        return conv_iconv_strdup(inbuf, src_code, dest_code);
 #else
        return g_strdup(inbuf);
@@ -632,6 +725,13 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
 
        dest_charset = conv_get_charset_from_str(dest_charset_str);
 
+       if (dest_charset == C_US_ASCII)
+               return conv_ustodisp;
+       else if (dest_charset == C_UTF_8 ||
+                (dest_charset == C_AUTO &&
+                 conv_get_current_charset() == C_UTF_8))
+               return conv_noconv;
+
        switch (src_charset) {
        case C_ISO_2022_JP:
        case C_ISO_2022_JP_2:
@@ -645,7 +745,6 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
                        code_conv = conv_ustodisp;
                break;
        case C_ISO_8859_1:
-#if !HAVE_ICONV
        case C_ISO_8859_2:
        case C_ISO_8859_4:
        case C_ISO_8859_5:
@@ -655,7 +754,6 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
        case C_ISO_8859_11:
        case C_ISO_8859_13:
        case C_ISO_8859_15:
-#endif
                if (dest_charset == C_AUTO)
                        code_conv = conv_latintodisp;
                break;
@@ -693,6 +791,19 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
        gint out_left;
        gint n_conv;
 
+       if (!src_code)
+               src_code = conv_get_outgoing_charset_str();
+       if (!dest_code)
+               dest_code = conv_get_current_charset_str();
+
+       /* don't convert if current codeset is US-ASCII */
+       if (!strcasecmp(dest_code, CS_US_ASCII))
+               return g_strdup(inbuf);
+
+       /* don't convert if src and dest codeset are identical */
+       if (!strcasecmp(src_code, dest_code))
+               return g_strdup(inbuf);
+
        cd = iconv_open(dest_code, src_code);
        if (cd == (iconv_t)-1)
                return NULL;
@@ -705,11 +816,13 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
        outbuf_p = outbuf;
        out_left = out_size;
 
-       while ((n_conv = iconv(cd, (gchar **)&inbuf_p, &in_left,
+       while ((n_conv = iconv(cd, (ICONV_CONST gchar **)&inbuf_p, &in_left,
                               &outbuf_p, &out_left)) < 0) {
                if (EILSEQ == errno) {
-                       *outbuf_p = '\0';
-                       break;
+                       inbuf_p++;
+                       in_left--;
+                       *outbuf_p++ = SUBST_CHAR;
+                       out_left--;
                } else if (EINVAL == errno) {
                        *outbuf_p = '\0';
                        break;
@@ -728,6 +841,9 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
                }
        }
 
+       iconv(cd, NULL, NULL, &outbuf_p, &out_left);
+       outbuf = g_realloc(outbuf, strlen(outbuf) + 1);
+
        iconv_close(cd);
 
        return outbuf;
@@ -841,36 +957,88 @@ static const struct {
        {"ANSI_X3.4-1968"       , C_US_ASCII    , C_US_ASCII},
 };
 
-const gchar *conv_get_charset_str(CharSet charset)
+static GHashTable *conv_get_charset_to_str_table(void)
 {
+       static GHashTable *table;
        gint i;
 
+       if (table)
+               return table;
+
+       table = g_hash_table_new(NULL, g_direct_equal);
+
        for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (charsets[i].charset == charset)
-                       return charsets[i].name;
+               if (g_hash_table_lookup(table, GUINT_TO_POINTER(charsets[i].charset))
+                   == NULL) {
+                       g_hash_table_insert
+                               (table, GUINT_TO_POINTER(charsets[i].charset),
+                                charsets[i].name);
+               }
        }
 
-       return NULL;
+       return table;
 }
 
-CharSet conv_get_charset_from_str(const gchar *charset)
+static gint str_case_equal(gconstpointer v, gconstpointer v2)
+{
+       return strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
+}
+
+static guint str_case_hash(gconstpointer key)
+{
+       const gchar *p = key;
+       guint h = *p;
+
+       if (h) {
+               h = tolower(h);
+               for (p += 1; *p != '\0'; p++)
+                       h = (h << 5) - h + tolower(*p);
+       }
+
+       return h;
+}
+
+static GHashTable *conv_get_charset_from_str_table(void)
 {
+       static GHashTable *table;
        gint i;
 
-       if (!charset) return C_AUTO;
+       if (table)
+               return table;
+
+       table = g_hash_table_new(str_case_hash, str_case_equal);
 
        for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (!strcasecmp(charsets[i].name, charset))
-                       return charsets[i].charset;
+               g_hash_table_insert(table, charsets[i].name,
+                                   GUINT_TO_POINTER(charsets[i].charset));
        }
 
-       return C_AUTO;
+       return table;
+}
+
+const gchar *conv_get_charset_str(CharSet charset)
+{
+       GHashTable *table;
+
+       table = conv_get_charset_to_str_table();
+       return g_hash_table_lookup(table, GUINT_TO_POINTER(charset));
+}
+
+CharSet conv_get_charset_from_str(const gchar *charset)
+{
+       GHashTable *table;
+
+       if (!charset) return C_AUTO;
+
+       table = conv_get_charset_from_str_table();
+       return GPOINTER_TO_UINT(g_hash_table_lookup(table, charset));
 }
 
 CharSet conv_get_current_charset(void)
 {
        static CharSet cur_charset = -1;
        const gchar *cur_locale;
+       const gchar *p;
        gint i;
 
        if (cur_charset != -1)
@@ -887,6 +1055,11 @@ CharSet conv_get_current_charset(void)
                return cur_charset;
        }
 
+       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;
 
@@ -917,13 +1090,14 @@ const gchar *conv_get_current_charset_str(void)
        if (!codeset)
                codeset = conv_get_charset_str(conv_get_current_charset());
 
-       return codeset ? codeset : "US-ASCII";
+       return codeset ? codeset : CS_US_ASCII;
 }
 
 CharSet conv_get_outgoing_charset(void)
 {
        static CharSet out_charset = -1;
        const gchar *cur_locale;
+       const gchar *p;
        gint i;
 
        if (out_charset != -1)
@@ -935,6 +1109,11 @@ CharSet conv_get_outgoing_charset(void)
                return out_charset;
        }
 
+       if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
+               out_charset = C_ISO_8859_15;
+               return out_charset;
+       }
+
        for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
                const gchar *p;
 
@@ -980,7 +1159,7 @@ const gchar *conv_get_outgoing_charset_str(void)
        out_charset = conv_get_outgoing_charset();
        str = conv_get_charset_str(out_charset);
 
-       return str ? str : "US-ASCII";
+       return str ? str : CS_US_ASCII;
 }
 
 const gchar *conv_get_current_locale(void)
@@ -1038,30 +1217,33 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                unmime_header(outbuf, str);
 }
 
-#define MAX_LINELEN    76
-#define MIMESEP_BEGIN  "=?"
-#define MIMESEP_END    "?="
+#define MAX_LINELEN            76
+#define MAX_HARD_LINELEN       996
+#define MIMESEP_BEGIN          "=?"
+#define MIMESEP_END            "?="
 
 #define B64LEN(len)    ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
 
-#define LBREAK_IF_REQUIRED(cond, plaintext)                    \
-{                                                              \
-       if (len - (destp - dest) < MAX_LINELEN + 2) {           \
-               *destp = '\0';                                  \
-               return;                                         \
-       }                                                       \
-                                                               \
-       if ((cond) && *srcp) {                                  \
-               if (destp > dest && isspace(*(destp - 1)))      \
-                       destp--;                                \
-               else if (plaintext && isspace(*srcp))           \
-                       srcp++;                                 \
-               if (*srcp) {                                    \
-                       *destp++ = '\n';                        \
-                       *destp++ = ' ';                         \
-                       left = MAX_LINELEN - 1;                 \
-               }                                               \
-       }                                                       \
+#define LBREAK_IF_REQUIRED(cond, is_plain_text)                                \
+{                                                                      \
+       if (len - (destp - dest) < MAX_LINELEN + 2) {                   \
+               *destp = '\0';                                          \
+               return;                                                 \
+       }                                                               \
+                                                                       \
+       if ((cond) && *srcp) {                                          \
+               if (destp > dest && left < MAX_LINELEN - 1) {           \
+                       if (isspace(*(destp - 1)))                      \
+                               destp--;                                \
+                       else if (is_plain_text && isspace(*srcp))       \
+                               srcp++;                                 \
+                       if (*srcp) {                                    \
+                               *destp++ = '\n';                        \
+                               *destp++ = ' ';                         \
+                               left = MAX_LINELEN - 1;                 \
+                       }                                               \
+               }                                                       \
+       }                                                               \
 }
 
 void conv_encode_header(gchar *dest, gint len, const gchar *src,
@@ -1085,11 +1267,11 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        }
 
        cur_encoding = conv_get_current_charset_str();
-       if (!strcmp(cur_encoding, "US-ASCII"))
-               cur_encoding = "ISO-8859-1";
+       if (!strcmp(cur_encoding, CS_US_ASCII))
+               cur_encoding = CS_ISO_8859_1;
        out_encoding = conv_get_outgoing_charset_str();
-       if (!strcmp(out_encoding, "US-ASCII"))
-               out_encoding = "ISO-8859-1";
+       if (!strcmp(out_encoding, CS_US_ASCII))
+               out_encoding = CS_ISO_8859_1;
 
        mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
                strlen(mimesep_enc) + strlen(MIMESEP_END);
@@ -1112,7 +1294,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        word_len = get_next_word_len(srcp);
                        LBREAK_IF_REQUIRED(left < word_len, TRUE);
                        while (word_len > 0) {
-                               LBREAK_IF_REQUIRED(left <= 0, TRUE);
+                               LBREAK_IF_REQUIRED(left + (MAX_HARD_LINELEN - MAX_LINELEN) <= 0, TRUE)
                                *destp++ = *srcp++;
                                left--;
                                word_len--;
@@ -1151,7 +1333,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                        (part_str, cur_encoding, out_encoding);
                                if (!out_str) {
                                        g_warning("conv_encode_header(): code conversion failed\n");
-                                       out_str = g_strdup(out_str);
+                                       conv_unreadable_8bit(part_str);
+                                       out_str = g_strdup(part_str);
                                }
                                out_str_len = strlen(out_str);
 
@@ -1181,7 +1364,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                        (part_str, cur_encoding, out_encoding);
                                if (!out_str) {
                                        g_warning("conv_encode_header(): code conversion failed\n");
-                                       out_str = g_strdup(out_str);
+                                       conv_unreadable_8bit(part_str);
+                                       out_str = g_strdup(part_str);
                                }
                                out_str_len = strlen(out_str);