sync with 0.8.8cvs5
[claws.git] / src / codeconv.c
index 715383885463e8e1d77785e7f896dac9329bceec..6d0a7a1ed32d7d0df753c00d6392bef6d1971904 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
  *
  * 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
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
-
-#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
-#  include <wchar.h>
-#  include <wctype.h>
-#endif
+#include <errno.h>
 
 #if HAVE_LOCALE_H
 #  include <locale.h>
 #endif
 
-#if HAVE_LIBJCONV
-#  include <jconv.h>
+#if HAVE_ICONV
+#  include <iconv.h>
 #endif
 
 #include "intl.h"
 #include "codeconv.h"
 #include "unmime.h"
-#include "common/base64.h"
+#include "base64.h"
+#include "quoted-printable.h"
 #include "utils.h"
 #include "prefs_common.h"
 
@@ -539,9 +536,7 @@ CodeConverter *conv_code_converter_new(const gchar *charset)
        CodeConverter *conv;
 
        conv = g_new0(CodeConverter, 1);
-#if !HAVE_LIBJCONV
-       conv->code_conv_func = conv_get_code_conv_func(charset);
-#endif
+       conv->code_conv_func = conv_get_code_conv_func(charset, NULL);
        conv->charset_str = g_strdup(charset);
        conv->charset = conv_get_charset_from_str(charset);
 
@@ -557,17 +552,21 @@ void conv_code_converter_destroy(CodeConverter *conv)
 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                  const gchar *inbuf)
 {
-#if HAVE_LIBJCONV
-       gchar *str;
-
-       str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
-       if (!str)
-               return -1;
+#if HAVE_ICONV
+       if (conv->code_conv_func != conv_noconv)
+               conv->code_conv_func(outbuf, outlen, inbuf);
        else {
-               strncpy2(outbuf, str, outlen);
-               g_free(str);
+               gchar *str;
+
+               str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
+               if (!str)
+                       return -1;
+               else {
+                       strncpy2(outbuf, str, outlen);
+                       g_free(str);
+               }
        }
-#else /* !HAVE_LIBJCONV */
+#else /* !HAVE_ICONV */
        conv->code_conv_func(outbuf, outlen, inbuf);
 #endif
 
@@ -575,137 +574,166 @@ gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
 }
 
 gchar *conv_codeset_strdup(const gchar *inbuf,
-                          const gchar *src_codeset, const gchar *dest_codeset)
+                          const gchar *src_code, const gchar *dest_code)
 {
        gchar *buf;
        size_t len;
-#if HAVE_LIBJCONV
-       gint actual_codeset;
-       const gchar *const *codesets;
-       gint n_codesets;
-#else /* !HAVE_LIBJCONV */
-       CharSet src_charset = C_AUTO, dest_charset = C_AUTO;
-#endif
+       CodeConvFunc conv_func;
 
-       if (!dest_codeset) {
-               CodeConvFunc func;
+       conv_func = conv_get_code_conv_func(src_code, dest_code);
+       if (conv_func != conv_noconv) {
+               len = (strlen(inbuf) + 1) * 3;
+               buf = g_malloc(len);
+               if (!buf) return NULL;
 
-               func = conv_get_code_conv_func(src_codeset);
-               if (func != conv_noconv) {
-                       if (func == conv_jistodisp ||
-                           func == conv_sjistodisp ||
-                           func == conv_anytodisp)
-                               len = strlen(inbuf) * 2 + 1;
-                       else
-                               len = strlen(inbuf) + 1;
-                       buf = g_malloc(len);
-                       if (!buf) return NULL;
-                       func(buf, len, inbuf);
-                       buf = g_realloc(buf, strlen(buf) + 1);
-                       return buf;
-               }
+               conv_func(buf, len, inbuf);
+               return g_realloc(buf, strlen(buf) + 1);
        }
 
-       /* don't convert if src and dest codeset are identical */
-       if (src_codeset && dest_codeset &&
-           !strcasecmp(src_codeset, dest_codeset))
-               return g_strdup(inbuf);
-
-#if HAVE_LIBJCONV
-       if (src_codeset) {
-               codesets = &src_codeset;
-               n_codesets = 1;
-       } else
-               codesets = jconv_info_get_pref_codesets(&n_codesets);
-       if (!dest_codeset) {
-               dest_codeset = conv_get_current_charset_str();
-               /* don't convert if current codeset is US-ASCII */
-               if (!strcasecmp(dest_codeset, CS_US_ASCII))
-                       return g_strdup(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 (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
-                            codesets, n_codesets,
-                            &actual_codeset, dest_codeset)
-           == 0)
-               return buf;
-       else {
-#if 0
-               g_warning("code conversion from %s to %s failed\n",
-                         codesets && codesets[0] ? codesets[0] : "(unknown)",
-                         dest_codeset);
-#endif /* 0 */
-               return NULL;
-       }
-#else /* !HAVE_LIBJCONV */
-       if (src_codeset) {
-               if (!strcasecmp(src_codeset, CS_EUC_JP) ||
-                   !strcasecmp(src_codeset, CS_EUCJP))
-                       src_charset = C_EUC_JP;
-               else if (!strcasecmp(src_codeset, CS_SHIFT_JIS) ||
-                        !strcasecmp(src_codeset, "SHIFT-JIS") ||
-                        !strcasecmp(src_codeset, "SJIS"))
-                       src_charset = C_SHIFT_JIS;
-               if (dest_codeset && !strcasecmp(dest_codeset, CS_ISO_2022_JP))
-                       dest_charset = C_ISO_2022_JP;
-       }
+       /* don't convert if current codeset is US-ASCII */
+       if (!strcasecmp(dest_code, CS_US_ASCII))
+               return g_strdup(inbuf);
 
-       if ((src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS) &&
-           dest_charset == C_ISO_2022_JP) {
-               len = (strlen(inbuf) + 1) * 3;
-               buf = g_malloc(len);
-               if (buf) {
-                       if (src_charset == C_EUC_JP)
-                               conv_euctojis(buf, len, inbuf);
-                       else
-                               conv_anytojis(buf, len, inbuf);
-                       buf = g_realloc(buf, strlen(buf) + 1);
-               }
-       } else
-               buf = g_strdup(inbuf);
+       /* don't convert if src and dest codeset are identical */
+       if (!strcasecmp(src_code, dest_code))
+               return g_strdup(inbuf);
 
-       return buf;
-#endif /* !HAVE_LIBJCONV */
+       return conv_iconv_strdup(inbuf, src_code, dest_code);
+#else
+       return g_strdup(inbuf);
+#endif /* HAVE_ICONV */
 }
 
-CodeConvFunc conv_get_code_conv_func(const gchar *charset)
+CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
+                                    const gchar *dest_charset_str)
 {
-       CodeConvFunc code_conv;
-       CharSet cur_charset;
+       CodeConvFunc code_conv = conv_noconv;
+       CharSet src_charset;
+       CharSet dest_charset;
 
-       if (!charset) {
-               cur_charset = conv_get_current_charset();
-               if (cur_charset == C_EUC_JP || cur_charset == C_SHIFT_JIS)
+       if (!src_charset_str)
+               src_charset = conv_get_current_charset();
+       else
+               src_charset = conv_get_charset_from_str(src_charset_str);
+
+       /* auto detection mode */
+       if (!src_charset_str && !dest_charset_str) {
+               if (src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS)
                        return conv_anytodisp;
                else
                        return conv_noconv;
        }
 
-       if (!strcasecmp(charset, CS_ISO_2022_JP) ||
-           !strcasecmp(charset, CS_ISO_2022_JP_2))
-               code_conv = conv_jistodisp;
-       else if (!strcasecmp(charset, CS_US_ASCII))
-               code_conv = conv_ustodisp;
-       else if (!strncasecmp(charset, CS_ISO_8859_1, 10))
-               code_conv = conv_latintodisp;
-#if !HAVE_LIBJCONV
-       else if (!strncasecmp(charset, "ISO-8859-", 9))
-               code_conv = conv_latintodisp;
+       dest_charset = conv_get_charset_from_str(dest_charset_str);
+
+       switch (src_charset) {
+       case C_ISO_2022_JP:
+       case C_ISO_2022_JP_2:
+               if (dest_charset == C_AUTO)
+                       code_conv = conv_jistodisp;
+               else if (dest_charset == C_EUC_JP)
+                       code_conv = conv_jistoeuc;
+               break;
+       case C_US_ASCII:
+               if (dest_charset == C_AUTO)
+                       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:
+       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:
 #endif
-       else if (!strcasecmp(charset, CS_SHIFT_JIS) ||
-                !strcasecmp(charset, "SHIFT-JIS")  ||
-                !strcasecmp(charset, "SJIS")       ||
-                !strcasecmp(charset, "X-SJIS"))
-               code_conv = conv_sjistodisp;
-       else if (!strcasecmp(charset, CS_EUC_JP) ||
-                !strcasecmp(charset, CS_EUCJP))
-               code_conv = conv_euctodisp;
-       else
-               code_conv = conv_noconv;
+               if (dest_charset == C_AUTO)
+                       code_conv = conv_latintodisp;
+               break;
+       case C_SHIFT_JIS:
+               if (dest_charset == C_AUTO)
+                       code_conv = conv_sjistodisp;
+               else if (dest_charset == C_EUC_JP)
+                       code_conv = conv_sjistoeuc;
+               break;
+       case C_EUC_JP:
+               if (dest_charset == C_AUTO)
+                       code_conv = conv_euctodisp;
+               else if (dest_charset == C_ISO_2022_JP ||
+                        dest_charset == C_ISO_2022_JP_2)
+                       code_conv = conv_euctojis;
+               break;
+       default:
+               break;
+       }
 
        return code_conv;
 }
 
+#if HAVE_ICONV
+gchar *conv_iconv_strdup(const gchar *inbuf,
+                        const gchar *src_code, const gchar *dest_code)
+{
+       iconv_t cd;
+       const gchar *inbuf_p;
+       gchar *outbuf;
+       gchar *outbuf_p;
+       gint in_size;
+       gint in_left;
+       gint out_size;
+       gint out_left;
+       gint n_conv;
+
+       cd = iconv_open(dest_code, src_code);
+       if (cd == (iconv_t)-1)
+               return NULL;
+
+       inbuf_p = inbuf;
+       in_size = strlen(inbuf) + 1;
+       in_left = in_size;
+       out_size = in_size * 2;
+       outbuf = g_malloc(out_size);
+       outbuf_p = outbuf;
+       out_left = out_size;
+
+       while ((n_conv = iconv(cd, (gchar **)&inbuf_p, &in_left,
+                              &outbuf_p, &out_left)) < 0) {
+               if (EILSEQ == errno) {
+                       *outbuf_p = '\0';
+                       break;
+               } else if (EINVAL == errno) {
+                       *outbuf_p = '\0';
+                       break;
+               } else if (E2BIG == errno) {
+                       out_size *= 2;
+                       outbuf = g_realloc(outbuf, out_size);
+                       inbuf_p = inbuf;
+                       in_left = in_size;
+                       outbuf_p = outbuf;
+                       out_left = out_size;
+               } else {
+                       g_warning("conv_iconv_strdup(): %s\n",
+                                 g_strerror(errno));
+                       *outbuf_p = '\0';
+                       break;
+               }
+       }
+
+       iconv_close(cd);
+
+       return outbuf;
+}
+#endif /* HAVE_ICONV */
+
 static const struct {
        CharSet charset;
        gchar *const name;
@@ -733,6 +761,8 @@ static const struct {
        {C_EUC_JP,              CS_EUC_JP},
        {C_EUC_JP,              CS_EUCJP},
        {C_SHIFT_JIS,           CS_SHIFT_JIS},
+       {C_SHIFT_JIS,           CS_SHIFT__JIS},
+       {C_SHIFT_JIS,           CS_SJIS},
        {C_ISO_2022_KR,         CS_ISO_2022_KR},
        {C_EUC_KR,              CS_EUC_KR},
        {C_ISO_2022_CN,         CS_ISO_2022_CN},
@@ -744,7 +774,6 @@ static const struct {
        {C_WINDOWS_874,         CS_WINDOWS_874},
 };
 
-#if !HAVE_LIBJCONV
 static const struct {
        gchar *const locale;
        CharSet charset;
@@ -809,7 +838,6 @@ static const struct {
        {"POSIX"                , C_US_ASCII    , C_US_ASCII},
        {"ANSI_X3.4-1968"       , C_US_ASCII    , C_US_ASCII},
 };
-#endif /* !HAVE_LIBJCONV */
 
 const gchar *conv_get_charset_str(CharSet charset)
 {
@@ -840,26 +868,12 @@ CharSet conv_get_charset_from_str(const gchar *charset)
 CharSet conv_get_current_charset(void)
 {
        static CharSet cur_charset = -1;
-       gint i;
-
-#if HAVE_LIBJCONV
-       const gchar *cur_codeset;
-#else
        const gchar *cur_locale;
-#endif
+       gint i;
 
        if (cur_charset != -1)
                return cur_charset;
 
-#if HAVE_LIBJCONV
-       cur_codeset = jconv_info_get_current_codeset();
-       for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (!strcasecmp(cur_codeset, charsets[i].name)) {
-                       cur_charset = charsets[i].charset;
-                       return cur_charset;
-               }
-       }
-#else
        cur_locale = conv_get_current_locale();
        if (!cur_locale) {
                cur_charset = C_US_ASCII;
@@ -874,8 +888,8 @@ CharSet conv_get_current_charset(void)
        for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
                const gchar *p;
 
-               /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
-               /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
+               /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
+                  "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
                if (!strncasecmp(cur_locale, locale_table[i].locale,
                                 strlen(locale_table[i].locale))) {
                        cur_charset = locale_table[i].charset;
@@ -889,7 +903,6 @@ CharSet conv_get_current_charset(void)
                        }
                }
        }
-#endif
 
        cur_charset = C_AUTO;
        return cur_charset;
@@ -908,39 +921,12 @@ const gchar *conv_get_current_charset_str(void)
 CharSet conv_get_outgoing_charset(void)
 {
        static CharSet out_charset = -1;
-       gint i;
-
-#if HAVE_LIBJCONV
-       gint j, n_pref_codesets;
-       const gchar *const *pref_codesets;
-#else
        const gchar *cur_locale;
-#endif
+       gint i;
 
        if (out_charset != -1)
                return out_charset;
 
-#if HAVE_LIBJCONV
-       /* skip US-ASCII and UTF-8 */
-       pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
-       for (i = 0; i < n_pref_codesets; i++) {
-               for (j = 3; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
-                       if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
-                               out_charset = charsets[j].charset;
-                               return out_charset;
-                       }
-               }
-       }
-
-       for (i = 0; i < n_pref_codesets; i++) {
-               if (!strcasecmp(pref_codesets[i], "UTF-8")) {
-                       out_charset = C_UTF_8;
-                       return out_charset;
-               }
-       }
-
-       out_charset = C_AUTO;
-#else
        cur_locale = conv_get_current_locale();
        if (!cur_locale) {
                out_charset = C_AUTO;
@@ -964,12 +950,13 @@ CharSet conv_get_outgoing_charset(void)
                }
        }
 
-       /* encoding conversion without libjconv is only supported
+#if !HAVE_ICONV
+       /* encoding conversion without iconv() is only supported
           on Japanese locale for now */
        if (out_charset == C_ISO_2022_JP)
                return out_charset;
-
-       out_charset = conv_get_current_charset();
+       else
+               return conv_get_current_charset();
 #endif
 
        return out_charset;
@@ -1049,417 +1036,186 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                unmime_header(outbuf, str);
 }
 
-#define MAX_ENCLEN     75
 #define MAX_LINELEN    76
+#define MIMESEP_BEGIN  "=?"
+#define MIMESEP_END    "?="
 
 #define B64LEN(len)    ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
 
-#if HAVE_LIBJCONV
+#define LBREAK_IF_REQUIRED(cond)                               \
+{                                                              \
+       if (len - (destp - dest) < MAX_LINELEN + 2) {           \
+               *destp = '\0';                                  \
+               return;                                         \
+       }                                                       \
+                                                               \
+       if ((cond) && *srcp) {                                  \
+               if (destp > dest && isspace(*(destp - 1)))      \
+                       destp--;                                \
+               else if (isspace(*srcp))                        \
+                       srcp++;                                 \
+               if (*srcp) {                                    \
+                       *destp++ = '\n';                        \
+                       *destp++ = ' ';                         \
+                       left = MAX_LINELEN - 1;                 \
+               }                                               \
+       }                                                       \
+}
+
 void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        gint header_len)
 {
-       wchar_t *wsrc;
-       wchar_t *wsrcp;
-       gchar *destp;
-       size_t line_len, mimehdr_len, mimehdr_begin_len;
-       gchar *mimehdr_init = "=?";
-       gchar *mimehdr_end = "?=";
-       gchar *mimehdr_enctype = "?B?";
-       const gchar *mimehdr_charset;
-
-       /* g_print("src = %s\n", src); */
-       mimehdr_charset = conv_get_outgoing_charset_str();
-
-       /* convert to wide-character string */
-       wsrcp = wsrc = strdup_mbstowcs(src);
-       if (!wsrc) {
-               g_warning("Can't convert string to wide characters.\n");
-               strncpy2(dest, src, len);
-               return;
+       const gchar *cur_encoding;
+       const gchar *out_encoding;
+       gint mimestr_len;
+       gchar *mimesep_enc;
+       gint left;
+       const gchar *srcp = src;
+       gchar *destp = dest;
+       gboolean use_base64;
+
+       if (MB_CUR_MAX > 1) {
+               use_base64 = TRUE;
+               mimesep_enc = "?B?";
+       } else {
+               use_base64 = FALSE;
+               mimesep_enc = "?Q?";
        }
 
-       mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
-               strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       mimehdr_begin_len = strlen(mimehdr_init) +
-               strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       line_len = header_len;
-       destp = dest;
-       *dest = '\0';
-
-       while (*wsrcp) {
-               wchar_t *wp, *wtmp, *wtmpp;
-               gint nspc = 0;
-               gboolean str_is_non_ascii;
-
-               /* irresponsible buffer overrun check */
-               if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
-
-               /* encode string including space
-                  if non-ASCII string follows */
-               if (is_next_nonascii(wsrcp)) {
-                       wp = wsrcp;
-                       while ((wp = find_wspace(wp)) != NULL)
-                               if (!is_next_nonascii(wp)) break;
-                       str_is_non_ascii = TRUE;
-               } else {
-                       wp = find_wspace(wsrcp);
-                       str_is_non_ascii = FALSE;
-               }
-
-               if (wp != NULL) {
-                       wtmp = wcsndup(wsrcp, wp - wsrcp);
-                       wsrcp = wp + 1;
-                       while (iswspace(wsrcp[nspc])) nspc++;
-               } else {
-                       wtmp = wcsdup(wsrcp);
-                       wsrcp += wcslen(wsrcp);
-               }
-
-               wtmpp = wtmp;
-
-               do {
-                       gint tlen = 0;
-                       gchar *tmp; /* internal codeset */
-                       gchar *raw; /* converted, but not base64 encoded */
-                       register gchar *tmpp;
-                       gint raw_len;
-
-                       tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
-                       *tmp = '\0';
-                       raw = g_strdup("");
-                       raw_len = 0;
-
-                       while (*wtmpp != (wchar_t)0) {
-                               gint mbl;
-                               gint dummy;
-                               gchar *raw_new = NULL;
-                               int raw_new_len = 0;
-                               const gchar *src_codeset;
-
-                               mbl = wctomb(tmpp, *wtmpp);
-                               if (mbl == -1) {
-                                       g_warning("invalid wide character\n");
-                                       wtmpp++;
-                                       continue;
-                               }
-                               /* g_free(raw); */
-                               src_codeset = conv_get_current_charset_str();
-                               /* printf ("tmp = %s, tlen = %d, mbl\n",
-                                       tmp, tlen, mbl); */
-                               if (jconv_alloc_conv(tmp, tlen + mbl,
-                                                    &raw_new, &raw_new_len,
-                                                    &src_codeset, 1,
-                                                    &dummy, mimehdr_charset)
-                                   != 0) {
-                                       g_warning("can't convert\n");
-                                       tmpp[0] = '\0';
-                                       wtmpp++;
-                                       continue;
-                               }
-                               if (str_is_non_ascii) {
-                                       gint dlen = mimehdr_len +
-                                               B64LEN(raw_len);
-                                       if ((line_len + dlen +
-                                            (*(wtmpp + 1) ? 0 : nspc) +
-                                            (line_len > 1 ? 1 : 0))
-                                           > MAX_LINELEN) {
-                                               g_free(raw_new);
-                                               if (tlen == 0) {
-                                                       *destp++ = '\n';
-                                                       *destp++ = ' ';
-                                                       line_len = 1;
-                                                       continue;
-                                               } else {
-                                                       *tmpp = '\0';
-                                                       break;
-                                               }
-                                       }
-                               } else if ((line_len + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc) +
-                                           (line_len > 1 ? 1 : 0))
-                                          > MAX_LINELEN) {
-                                       g_free(raw_new);
-                                       if (1 + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc)
-                                           >= MAX_LINELEN) {
-                                               *tmpp = '\0';
-                                               break;
-                                       }
-                                       *destp++ = '\n';
-                                       *destp++ = ' ';
-                                       line_len = 1;
-                                       continue;
-                               }
-
-                               tmpp += mbl;
-                               *tmpp = '\0';
+       cur_encoding = conv_get_current_charset_str();
+       if (!strcmp(cur_encoding, "US-ASCII"))
+               cur_encoding = "ISO-8859-1";
+       out_encoding = conv_get_outgoing_charset_str();
+       if (!strcmp(out_encoding, "US-ASCII"))
+               out_encoding = "ISO-8859-1";
 
-                               tlen += mbl;
+       mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
+               strlen(mimesep_enc) + strlen(MIMESEP_END);
 
-                               g_free(raw);
-                               raw = raw_new;
-                               raw_len = raw_new_len;
+       left = MAX_LINELEN - header_len;
 
-                               wtmpp++;
-                       }
-                       /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
-                               tmp, tlen, mb_seqlen); */
-
-                       if (tlen == 0 || raw_len == 0) {
-                               g_free(tmp);
-                               g_free(raw);
-                               continue;
-                       }
-
-                       if (line_len > 1 && destp > dest) {
-                               *destp++ = ' ';
-                               *destp = '\0';
-                               line_len++;
-                       }
+       while (*srcp) {
+               LBREAK_IF_REQUIRED(left <= 0);
 
-                       if (str_is_non_ascii) {
-                               g_snprintf(destp, len - strlen(dest), "%s%s%s",
-                                          mimehdr_init, mimehdr_charset,
-                                          mimehdr_enctype);
-                               destp += mimehdr_begin_len;
-                               line_len += mimehdr_begin_len;
-
-                               base64_encode(destp, raw, raw_len);
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
-
-                               strcpy(destp, mimehdr_end);
-                               destp += strlen(mimehdr_end);
-                               line_len += strlen(mimehdr_end);
-                       } else {
-                               strcpy(destp, tmp);
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
-                       }
-
-                       g_free(tmp);
-                       g_free(raw);
-                       /* g_print("line_len = %d\n\n", line_len); */
-               } while (*wtmpp != (wchar_t)0);
-
-               while (iswspace(*wsrcp)) {
-                       gint mbl;
-
-                       mbl = wctomb(destp, *wsrcp++);
-                       if (mbl != -1) {
-                               destp += mbl;
-                               line_len += mbl;
-                       }
+               while (isspace(*srcp)) {
+                       *destp++ = *srcp++;
+                       left--;
+                       LBREAK_IF_REQUIRED(left <= 0);
                }
-               *destp = '\0';
-
-               g_free(wtmp);
-       }
-
-       g_free(wsrc);
-
-       /* g_print("dest = %s\n", dest); */
-}
-#else /* !HAVE_LIBJCONV */
-
-#define JIS_SEQLEN     3
-
-void conv_encode_header(gchar *dest, gint len, const gchar *src,
-                       gint header_len)
-{
-       wchar_t *wsrc;
-       wchar_t *wsrcp;
-       gchar *destp;
-       size_t line_len, mimehdr_len, mimehdr_begin_len;
-       gchar *mimehdr_init = "=?";
-       gchar *mimehdr_end = "?=";
-       gchar *mimehdr_enctype = "?B?";
-       const gchar *mimehdr_charset;
-       gboolean do_conv = FALSE;
-
-       /* g_print("src = %s\n", src); */
-       mimehdr_charset = conv_get_outgoing_charset_str();
-       if (strcmp(mimehdr_charset, "ISO-2022-JP") == 0)
-               do_conv = TRUE;
-       else if (strcmp(mimehdr_charset, "US-ASCII") == 0)
-               mimehdr_charset = "ISO-8859-1";
-
-       /* convert to wide-character string */
-       wsrcp = wsrc = strdup_mbstowcs(src);
-       if (!wsrc) {
-               g_warning("Can't convert string to wide characters.\n");
-               strncpy2(dest, src, len);
-               return;
-       }
 
-       mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
-                     strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       mimehdr_begin_len = strlen(mimehdr_init) +
-                           strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       line_len = header_len;
-       destp = dest;
-       *dest = '\0';
-
-       while (*wsrcp) {
-               wchar_t *wp, *wtmp, *wtmpp;
-               gint nspc = 0;
-               gboolean str_is_non_ascii;
-
-               /* irresponsible buffer overrun check */
-               if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
-
-               /* encode string including space
-                  if non-ASCII string follows */
-               if (is_next_nonascii(wsrcp)) {
-                       wp = wsrcp;
-                       while ((wp = find_wspace(wp)) != NULL)
-                               if (!is_next_nonascii(wp)) break;
-                       str_is_non_ascii = TRUE;
-               } else {
-                       wp = find_wspace(wsrcp);
-                       str_is_non_ascii = FALSE;
-               }
+               /* output as it is if the next word is ASCII string */
+               if (!is_next_nonascii(srcp)) {
+                       gint word_len;
+
+                       word_len = get_next_word_len(srcp);
+                       LBREAK_IF_REQUIRED(left < word_len);
+                       while (word_len > 0) {
+                               LBREAK_IF_REQUIRED(left <= 0);
+                               *destp++ = *srcp++;
+                               left--;
+                               word_len--;
+                       }
 
-               if (wp != NULL) {
-                       wtmp = wcsndup(wsrcp, wp - wsrcp);
-                       wsrcp = wp + 1;
-                       while (iswspace(wsrcp[nspc])) nspc++;
-               } else {
-                       wtmp = wcsdup(wsrcp);
-                       wsrcp += wcslen(wsrcp);
+                       continue;
                }
 
-               wtmpp = wtmp;
-
-               do {
-                       gint prev_mbl = 1, tlen = 0, mb_seqlen = 0;
-                       gchar *tmp;
-                       register gchar *tmpp;
+               while (1) {
+                       gint mb_len = 0;
+                       gint cur_len = 0;
+                       gchar *part_str;
+                       gchar *out_str;
+                       gchar *enc_str;
+                       const gchar *p = srcp;
+                       gint out_str_len;
+                       gint out_enc_str_len;
+                       gint mime_block_len;
+                       gboolean cont = FALSE;
+
+                       while (*p != '\0') {
+                               if (isspace(*p) && !is_next_nonascii(p + 1))
+                                       break;
+
+                               if (MB_CUR_MAX > 1) {
+                                       mb_len = mblen(p, MB_CUR_MAX);
+                                       if (mb_len < 0) {
+                                               g_warning("conv_encode_header(): invalid multibyte character encountered\n");
+                                               mb_len = 1;
+                                       }
+                               } else
+                                       mb_len = 1;
+
+                               Xstrndup_a(part_str, srcp, cur_len + mb_len, );
+                               out_str = conv_codeset_strdup
+                                       (part_str, cur_encoding, out_encoding);
+                               if (!out_str) {
+                                       g_warning("conv_encode_header(): code conversion failed\n");
+                                       out_str = g_strdup(out_str);
+                               }
+                               out_str_len = strlen(out_str);
 
-                       tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
-                       *tmp = '\0';
+                               if (use_base64)
+                                       out_enc_str_len = B64LEN(out_str_len);
+                               else
+                                       out_enc_str_len =
+                                               qp_get_q_encoding_len(out_str);
 
-                       while (*wtmpp != (wchar_t)0) {
-                               gint mbl;
+                               g_free(out_str);
 
-                               mbl = wctomb(tmpp, *wtmpp);
-                               if (mbl == -1) {
-                                       g_warning("invalid wide character\n");
-                                       wtmpp++;
+                               if (mimestr_len + out_enc_str_len <= left) {
+                                       cur_len += mb_len;
+                                       p += mb_len;
+                               } else if (cur_len == 0) {
+                                       LBREAK_IF_REQUIRED(1);
                                        continue;
+                               } else {
+                                       cont = TRUE;
+                                       break;
                                }
+                       }
 
-                               /* length of KI + KO */
-                               if (do_conv && prev_mbl == 1 && mbl == 2)
-                                       mb_seqlen += JIS_SEQLEN * 2;
-
-                               if (str_is_non_ascii) {
-                                       gint dlen = mimehdr_len +
-                                               B64LEN(tlen + mb_seqlen + mbl);
-
-                                       if ((line_len + dlen +
-                                            (*(wtmpp + 1) ? 0 : nspc) +
-                                            (line_len > 1 ? 1 : 0))
-                                           > MAX_LINELEN) {
-                                               if (tlen == 0) {
-                                                       *destp++ = '\n';
-                                                       *destp++ = ' ';
-                                                       line_len = 1;
-                                                       mb_seqlen = 0;
-                                                       continue;
-                                               } else {
-                                                       *tmpp = '\0';
-                                                       break;
-                                               }
-                                       }
-                               } else if ((line_len + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc) +
-                                           (line_len > 1 ? 1 : 0))
-                                          > MAX_LINELEN) {
-                                       if (1 + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc)
-                                           >= MAX_LINELEN) {
-                                               *tmpp = '\0';
-                                               break;
-                                       }
-                                       *destp++ = '\n';
-                                       *destp++ = ' ';
-                                       line_len = 1;
-                                       continue;
+                       if (cur_len > 0) {
+                               Xstrndup_a(part_str, srcp, cur_len, );
+                               out_str = conv_codeset_strdup
+                                       (part_str, cur_encoding, out_encoding);
+                               if (!out_str) {
+                                       g_warning("conv_encode_header(): code conversion failed\n");
+                                       out_str = g_strdup(out_str);
                                }
+                               out_str_len = strlen(out_str);
 
-                               tmpp += mbl;
-                               *tmpp = '\0';
+                               if (use_base64)
+                                       out_enc_str_len = B64LEN(out_str_len);
+                               else
+                                       out_enc_str_len =
+                                               qp_get_q_encoding_len(out_str);
 
-                               tlen += mbl;
-                               prev_mbl = mbl;
+                               Xalloca(enc_str, out_enc_str_len + 1, );
+                               if (use_base64)
+                                       base64_encode(enc_str, out_str, out_str_len);
+                               else
+                                       qp_q_encode(enc_str, out_str);
 
-                               wtmpp++;
-                       }
-                       /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
-                               tmp, tlen, mb_seqlen); */
+                               g_free(out_str);
 
-                       if (tlen == 0) {
-                               g_free(tmp);
-                               continue;
-                       }
+                               /* output MIME-encoded string block */
+                               mime_block_len = mimestr_len + strlen(enc_str);
+                               g_snprintf(destp, mime_block_len + 1,
+                                          MIMESEP_BEGIN "%s%s%s" MIMESEP_END,
+                                          out_encoding, mimesep_enc, enc_str);
+                               destp += mime_block_len;
+                               srcp += cur_len;
 
-                       if (line_len > 1 && destp > dest) {
-                               *destp++ = ' ';
-                               *destp = '\0';
-                               line_len++;
+                               left -= mime_block_len;
                        }
 
-                       if (str_is_non_ascii) {
-                               gchar *raw;
+                       LBREAK_IF_REQUIRED(cont);
 
-                               raw = g_new(gchar, tlen + mb_seqlen + 1);
-                               if (do_conv)
-                                       conv_euctojis(raw, tlen + mb_seqlen + 1,
-                                                     tmp);
-                               else
-                                       strcpy(raw, tmp);
-                               g_snprintf(destp, len - strlen(dest), "%s%s%s",
-                                          mimehdr_init, mimehdr_charset,
-                                          mimehdr_enctype);
-                               destp += mimehdr_begin_len;
-                               line_len += mimehdr_begin_len;
-
-                               base64_encode(destp, raw, strlen(raw));
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
-
-                               strcpy(destp, mimehdr_end);
-                               destp += strlen(mimehdr_end);
-                               line_len += strlen(mimehdr_end);
-
-                               g_free(raw);
-                       } else {
-                               strcpy(destp, tmp);
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
-                       }
-
-                       g_free(tmp);
-                       /* g_print("line_len = %d\n\n", line_len); */
-               } while (*wtmpp != (wchar_t)0);
-
-               while (iswspace(*wsrcp)) {
-                       gint mbl;
-
-                       mbl = wctomb(destp, *wsrcp++);
-                       if (mbl != -1) {
-                               destp += mbl;
-                               line_len += mbl;
-                       }
+                       if (cur_len == 0)
+                               break;
                }
-               *destp = '\0';
-
-               g_free(wtmp);
        }
 
-       g_free(wsrc);
-
-       /* g_print("dest = %s\n", dest); */
+       *destp = '\0';
 }
-#endif /* HAVE_LIBJCONV */
+
+#undef LBREAK_IF_REQUIRED