sync with 0.8.10cvs23
[claws.git] / src / codeconv.c
index 67ca371a64f572a5bda58ee212bbeedee7c2e270..15c3a5f3b3f1e9ee529025a7e372ef36112b62e2 100644 (file)
@@ -370,7 +370,7 @@ static gchar valid_eucjp_tbl[][96] = {
 
 static gboolean isprintableeuckanji(guchar c1, guchar c2)
 {
-       if (c1 <= 0xa0 || c1 == 0xff)
+       if (c1 <= 0xa0 || c1 >= 0xf5)
                return FALSE;
        if (c2 <= 0xa0 || c2 == 0xff)
                return FALSE;
@@ -664,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 {
@@ -697,22 +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();
-               if (!strcasecmp(dest_code, CS_US_ASCII))
-                       dest_code = CS_ISO_8859_1;
-       }
-
-       /* 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);
@@ -743,6 +727,10 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_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:
@@ -757,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:
@@ -767,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;
@@ -805,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;
@@ -820,12 +819,12 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
        while ((n_conv = iconv(cd, (ICONV_CONST gchar **)&inbuf_p, &in_left,
                               &outbuf_p, &out_left)) < 0) {
                if (EILSEQ == errno) {
-                       g_free(outbuf);
-                       outbuf = NULL;
-                       break;
+                       inbuf_p++;
+                       in_left--;
+                       *outbuf_p++ = SUBST_CHAR;
+                       out_left--;
                } else if (EINVAL == errno) {
-                       g_free(outbuf);
-                       outbuf = NULL;
+                       *outbuf_p = '\0';
                        break;
                } else if (E2BIG == errno) {
                        out_size *= 2;
@@ -842,10 +841,8 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
                }
        }
 
-       if (outbuf) {
-               iconv(cd, NULL, NULL, &outbuf_p, &out_left);
-               outbuf = g_realloc(outbuf, strlen(outbuf) + 1);
-       }
+       iconv(cd, NULL, NULL, &outbuf_p, &out_left);
+       outbuf = g_realloc(outbuf, strlen(outbuf) + 1);
 
        iconv_close(cd);
 
@@ -1220,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,
@@ -1294,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--;