715383885463e8e1d77785e7f896dac9329bceec
[claws.git] / src / codeconv.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28
29 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
30 #  include <wchar.h>
31 #  include <wctype.h>
32 #endif
33
34 #if HAVE_LOCALE_H
35 #  include <locale.h>
36 #endif
37
38 #if HAVE_LIBJCONV
39 #  include <jconv.h>
40 #endif
41
42 #include "intl.h"
43 #include "codeconv.h"
44 #include "unmime.h"
45 #include "common/base64.h"
46 #include "utils.h"
47 #include "prefs_common.h"
48
49 typedef enum
50 {
51         JIS_ASCII,
52         JIS_KANJI,
53         JIS_HWKANA,
54         JIS_AUXKANJI
55 } JISState;
56
57 #define SUBST_CHAR      '_'
58 #define ESC             '\033'
59
60 #define iseuckanji(c) \
61         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xfe)
62 #define iseuchwkana1(c) \
63         (((c) & 0xff) == 0x8e)
64 #define iseuchwkana2(c) \
65         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
66 #define iseucaux(c) \
67         (((c) & 0xff) == 0x8f)
68 #define isunprintableeuckanji(c) \
69         (((c) & 0xff) >= 0xa9 && ((c) & 0xff) <= 0xaf)
70 #define issjiskanji1(c) \
71         ((((c) & 0xff) >= 0x81 && ((c) & 0xff) <= 0x9f) || \
72          (((c) & 0xff) >= 0xe0 && ((c) & 0xff) <= 0xfc))
73 #define issjiskanji2(c) \
74         ((((c) & 0xff) >= 0x40 && ((c) & 0xff) <= 0x7e) || \
75          (((c) & 0xff) >= 0x80 && ((c) & 0xff) <= 0xfc))
76 #define issjishwkana(c) \
77         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
78
79 #define K_IN()                          \
80         if (state != JIS_KANJI) {       \
81                 *out++ = ESC;           \
82                 *out++ = '$';           \
83                 *out++ = 'B';           \
84                 state = JIS_KANJI;      \
85         }
86
87 #define K_OUT()                         \
88         if (state != JIS_ASCII) {       \
89                 *out++ = ESC;           \
90                 *out++ = '(';           \
91                 *out++ = 'B';           \
92                 state = JIS_ASCII;      \
93         }
94
95 #define HW_IN()                         \
96         if (state != JIS_HWKANA) {      \
97                 *out++ = ESC;           \
98                 *out++ = '(';           \
99                 *out++ = 'I';           \
100                 state = JIS_HWKANA;     \
101         }
102
103 #define AUX_IN()                        \
104         if (state != JIS_AUXKANJI) {    \
105                 *out++ = ESC;           \
106                 *out++ = '$';           \
107                 *out++ = '(';           \
108                 *out++ = 'D';           \
109                 state = JIS_AUXKANJI;   \
110         }
111
112 void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
113 {
114         const guchar *in = inbuf;
115         guchar *out = outbuf;
116         JISState state = JIS_ASCII;
117
118         while (*in != '\0') {
119                 if (*in == ESC) {
120                         in++;
121                         if (*in == '$') {
122                                 if (*(in + 1) == '@' || *(in + 1) == 'B') {
123                                         state = JIS_KANJI;
124                                         in += 2;
125                                 } else if (*(in + 1) == '(' &&
126                                            *(in + 2) == 'D') {
127                                         state = JIS_AUXKANJI;
128                                         in += 3;
129                                 } else {
130                                         /* unknown escape sequence */
131                                         state = JIS_ASCII;
132                                 }
133                         } else if (*in == '(') {
134                                 if (*(in + 1) == 'B' || *(in + 1) == 'J') {
135                                         state = JIS_ASCII;
136                                         in += 2;
137                                 } else if (*(in + 1) == 'I') {
138                                         state = JIS_HWKANA;
139                                         in += 2;
140                                 } else {
141                                         /* unknown escape sequence */
142                                         state = JIS_ASCII;
143                                 }
144                         } else {
145                                 /* unknown escape sequence */
146                                 state = JIS_ASCII;
147                         }
148                 } else if (*in == 0x0e) {
149                         state = JIS_HWKANA;
150                         in++;
151                 } else if (*in == 0x0f) {
152                         state = JIS_ASCII;
153                         in++;
154                 } else {
155                         switch (state) {
156                         case JIS_ASCII:
157                                 *out++ = *in++;
158                                 break;
159                         case JIS_KANJI:
160                                 *out++ = *in++ | 0x80;
161                                 if (*in == '\0') break;
162                                 *out++ = *in++ | 0x80;
163                                 break;
164                         case JIS_HWKANA:
165                                 *out++ = 0x8e;
166                                 *out++ = *in++ | 0x80;
167                                 break;
168                         case JIS_AUXKANJI:
169                                 *out++ = 0x8f;
170                                 *out++ = *in++ | 0x80;
171                                 if (*in == '\0') break;
172                                 *out++ = *in++ | 0x80;
173                                 break;
174                         }
175                 }
176         }
177
178         *out = '\0';
179 }
180
181 void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
182 {
183         const guchar *in = inbuf;
184         guchar *out = outbuf;
185         JISState state = JIS_ASCII;
186
187         while (*in != '\0') {
188                 if (isascii(*in)) {
189                         K_OUT();
190                         *out++ = *in++;
191                 } else if (iseuckanji(*in)) {
192                         if (iseuckanji(*(in + 1))) {
193                                 K_IN();
194                                 *out++ = *in++ & 0x7f;
195                                 *out++ = *in++ & 0x7f;
196                         } else {
197                                 K_OUT();
198                                 *out++ = SUBST_CHAR;
199                                 in++;
200                                 if (*in != '\0' && !isascii(*in)) {
201                                         *out++ = SUBST_CHAR;
202                                         in++;
203                                 }
204                         }
205                 } else if (iseuchwkana1(*in)) {
206                         in++;
207                         if (iseuchwkana2(*in)) {
208                                 HW_IN();
209                                 *out++ = *in++ & 0x7f;
210                         } else {
211                                 K_OUT();
212                                 if (*in != '\0' && !isascii(*in)) {
213                                         *out++ = SUBST_CHAR;
214                                         in++;
215                                 }
216                         }
217                 } else if (iseucaux(*in)) {
218                         in++;
219                         if (iseuckanji(*in) && iseuckanji(*(in + 1))) {
220                                 AUX_IN();
221                                 *out++ = *in++ & 0x7f;
222                                 *out++ = *in++ & 0x7f;
223                         } else {
224                                 K_OUT();
225                                 if (*in != '\0' && !isascii(*in)) {
226                                         *out++ = SUBST_CHAR;
227                                         in++;
228                                         if (*in != '\0' && !isascii(*in)) {
229                                                 *out++ = SUBST_CHAR;
230                                                 in++;
231                                         }
232                                 }
233                         }
234                 } else {
235                         K_OUT();
236                         *out++ = SUBST_CHAR;
237                         in++;
238                 }
239         }
240
241         K_OUT();
242         *out = '\0';
243 }
244
245 void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
246 {
247         const guchar *in = inbuf;
248         guchar *out = outbuf;
249
250         while (*in != '\0') {
251                 if (isascii(*in)) {
252                         *out++ = *in++;
253                 } else if (issjiskanji1(*in)) {
254                         if (issjiskanji2(*(in + 1))) {
255                                 guchar out1 = *in;
256                                 guchar out2 = *(in + 1);
257                                 guchar row;
258
259                                 row = out1 < 0xa0 ? 0x70 : 0xb0;
260                                 if (out2 < 0x9f) {
261                                         out1 = (out1 - row) * 2 - 1;
262                                         out2 -= out2 > 0x7f ? 0x20 : 0x1f;
263                                 } else {
264                                         out1 = (out1 - row) * 2;
265                                         out2 -= 0x7e;
266                                 }
267
268                                 *out++ = out1 | 0x80;
269                                 *out++ = out2 | 0x80;
270                                 in += 2;
271                         } else {
272                                 *out++ = SUBST_CHAR;
273                                 in++;
274                                 if (*in != '\0' && !isascii(*in)) {
275                                         *out++ = SUBST_CHAR;
276                                         in++;
277                                 }
278                         }
279                 } else if (issjishwkana(*in)) {
280                         *out++ = 0x8e;
281                         *out++ = *in++;
282                 } else {
283                         *out++ = SUBST_CHAR;
284                         in++;
285                 }
286         }
287
288         *out = '\0';
289 }
290
291 void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
292 {
293         switch (conv_guess_encoding(inbuf)) {
294         case C_ISO_2022_JP:
295                 conv_jistoeuc(outbuf, outlen, inbuf);
296                 break;
297         case C_SHIFT_JIS:
298                 conv_sjistoeuc(outbuf, outlen, inbuf);
299                 break;
300         default:
301                 strncpy2(outbuf, inbuf, outlen);
302                 break;
303         }
304 }
305
306 void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
307 {
308         switch (conv_guess_encoding(inbuf)) {
309         case C_EUC_JP:
310                 conv_euctojis(outbuf, outlen, inbuf);
311                 break;
312         default:
313                 strncpy2(outbuf, inbuf, outlen);
314                 break;
315         }
316 }
317
318 void conv_unreadable_eucjp(gchar *str)
319 {
320         register guchar *p = str;
321
322         while (*p != '\0') {
323                 if (isascii(*p)) {
324                         /* convert CR+LF -> LF */
325                         if (*p == '\r' && *(p + 1) == '\n')
326                                 memmove(p, p + 1, strlen(p));
327                         /* printable 7 bit code */
328                         p++;
329                 } else if (iseuckanji(*p)) {
330                         if (iseuckanji(*(p + 1)) && !isunprintableeuckanji(*p))
331                                 /* printable euc-jp code */
332                                 p += 2;
333                         else {
334                                 /* substitute unprintable code */
335                                 *p++ = SUBST_CHAR;
336                                 if (*p != '\0') {
337                                         if (isascii(*p))
338                                                 p++;
339                                         else
340                                                 *p++ = SUBST_CHAR;
341                                 }
342                         }
343                 } else if (iseuchwkana1(*p)) {
344                         if (iseuchwkana2(*(p + 1)))
345                                 /* euc-jp hankaku kana */
346                                 p += 2;
347                         else
348                                 *p++ = SUBST_CHAR;
349                 } else if (iseucaux(*p)) {
350                         if (iseuckanji(*(p + 1)) && iseuckanji(*(p + 2))) {
351                                 /* auxiliary kanji */
352                                 p += 3;
353                         } else
354                                 *p++ = SUBST_CHAR;
355                 } else
356                         /* substitute unprintable 1 byte code */
357                         *p++ = SUBST_CHAR;
358         }
359 }
360
361 void conv_unreadable_8bit(gchar *str)
362 {
363         register guchar *p = str;
364
365         while (*p != '\0') {
366                 /* convert CR+LF -> LF */
367                 if (*p == '\r' && *(p + 1) == '\n')
368                         memmove(p, p + 1, strlen(p));
369                 else if (!isascii(*p)) *p = SUBST_CHAR;
370                 p++;
371         }
372 }
373
374 void conv_unreadable_latin(gchar *str)
375 {
376         register guchar *p = str;
377
378         while (*p != '\0') {
379                 /* convert CR+LF -> LF */
380                 if (*p == '\r' && *(p + 1) == '\n')
381                         memmove(p, p + 1, strlen(p));
382                 else if ((*p & 0xff) >= 0x80 && (*p & 0xff) <= 0x9f)
383                         *p = SUBST_CHAR;
384                 p++;
385         }
386 }
387
388 #define NCV     '\0'
389
390 void conv_mb_alnum(gchar *str)
391 {
392         static guchar char_tbl[] = {
393                 /* 0xa0 - 0xaf */
394                 NCV, ' ', NCV, NCV, ',', '.', NCV, ':',
395                 ';', '?', '!', NCV, NCV, NCV, NCV, NCV,
396                 /* 0xb0 - 0xbf */
397                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
398                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
399                 /* 0xc0 - 0xcf */
400                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
401                 NCV, NCV, '(', ')', NCV, NCV, '[', ']',
402                 /* 0xd0 - 0xdf */
403                 '{', '}', NCV, NCV, NCV, NCV, NCV, NCV,
404                 NCV, NCV, NCV, NCV, '+', '-', NCV, NCV,
405                 /* 0xe0 - 0xef */
406                 NCV, '=', NCV, '<', '>', NCV, NCV, NCV,
407                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV
408         };
409
410         register guchar *p = str;
411         register gint len;
412
413         len = strlen(str);
414
415         while (len > 1) {
416                 if (*p == 0xa3) {
417                         register guchar ch = *(p + 1);
418
419                         if (ch >= 0xb0 && ch <= 0xfa) {
420                                 /* [a-zA-Z] */
421                                 *p = ch & 0x7f;
422                                 p++;
423                                 len--;
424                                 memmove(p, p + 1, len);
425                                 len--;
426                         } else  {
427                                 p += 2;
428                                 len -= 2;
429                         }
430                 } else if (*p == 0xa1) {
431                         register guchar ch = *(p + 1);
432
433                         if (ch >= 0xa0 && ch <= 0xef &&
434                             NCV != char_tbl[ch - 0xa0]) {
435                                 *p = char_tbl[ch - 0xa0];
436                                 p++;
437                                 len--;
438                                 memmove(p, p + 1, len);
439                                 len--;
440                         } else {
441                                 p += 2;
442                                 len -= 2;
443                         }
444                 } else if (iseuckanji(*p)) {
445                         p += 2;
446                         len -= 2;
447                 } else {
448                         p++;
449                         len--;
450                 }
451         }
452 }
453
454 CharSet conv_guess_encoding(const gchar *str)
455 {
456         const guchar *p = str;
457         CharSet guessed = C_US_ASCII;
458
459         while (*p != '\0') {
460                 if (*p == ESC && (*(p + 1) == '$' || *(p + 1) == '(')) {
461                         if (guessed == C_US_ASCII)
462                                 return C_ISO_2022_JP;
463                         p += 2;
464                 } else if (isascii(*p)) {
465                         p++;
466                 } else if (iseuckanji(*p) && iseuckanji(*(p + 1))) {
467                         if (*p >= 0xfd && *p <= 0xfe)
468                                 return C_EUC_JP;
469                         else if (guessed == C_SHIFT_JIS) {
470                                 if ((issjiskanji1(*p) &&
471                                      issjiskanji2(*(p + 1))) ||
472                                     issjishwkana(*p))
473                                         guessed = C_SHIFT_JIS;
474                                 else
475                                         guessed = C_EUC_JP;
476                         } else
477                                 guessed = C_EUC_JP;
478                         p += 2;
479                 } else if (issjiskanji1(*p) && issjiskanji2(*(p + 1))) {
480                         if (iseuchwkana1(*p) && iseuchwkana2(*(p + 1)))
481                                 guessed = C_SHIFT_JIS;
482                         else
483                                 return C_SHIFT_JIS;
484                         p += 2;
485                 } else if (issjishwkana(*p)) {
486                         guessed = C_SHIFT_JIS;
487                         p++;
488                 } else {
489                         p++;
490                 }
491         }
492
493         return guessed;
494 }
495
496 void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
497 {
498         conv_jistoeuc(outbuf, outlen, inbuf);
499         conv_unreadable_eucjp(outbuf);
500 }
501
502 void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
503 {
504         conv_sjistoeuc(outbuf, outlen, inbuf);
505         conv_unreadable_eucjp(outbuf);
506 }
507
508 void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
509 {
510         strncpy2(outbuf, inbuf, outlen);
511         conv_unreadable_eucjp(outbuf);
512 }
513
514 void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
515 {
516         conv_anytoeuc(outbuf, outlen, inbuf);
517         conv_unreadable_eucjp(outbuf);
518 }
519
520 void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
521 {
522         strncpy2(outbuf, inbuf, outlen);
523         conv_unreadable_8bit(outbuf);
524 }
525
526 void conv_latintodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
527 {
528         strncpy2(outbuf, inbuf, outlen);
529         conv_unreadable_latin(outbuf);
530 }
531
532 void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
533 {
534         strncpy2(outbuf, inbuf, outlen);
535 }
536
537 CodeConverter *conv_code_converter_new(const gchar *charset)
538 {
539         CodeConverter *conv;
540
541         conv = g_new0(CodeConverter, 1);
542 #if !HAVE_LIBJCONV
543         conv->code_conv_func = conv_get_code_conv_func(charset);
544 #endif
545         conv->charset_str = g_strdup(charset);
546         conv->charset = conv_get_charset_from_str(charset);
547
548         return conv;
549 }
550
551 void conv_code_converter_destroy(CodeConverter *conv)
552 {
553         g_free(conv->charset_str);
554         g_free(conv);
555 }
556
557 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
558                   const gchar *inbuf)
559 {
560 #if HAVE_LIBJCONV
561         gchar *str;
562
563         str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
564         if (!str)
565                 return -1;
566         else {
567                 strncpy2(outbuf, str, outlen);
568                 g_free(str);
569         }
570 #else /* !HAVE_LIBJCONV */
571         conv->code_conv_func(outbuf, outlen, inbuf);
572 #endif
573
574         return 0;
575 }
576
577 gchar *conv_codeset_strdup(const gchar *inbuf,
578                            const gchar *src_codeset, const gchar *dest_codeset)
579 {
580         gchar *buf;
581         size_t len;
582 #if HAVE_LIBJCONV
583         gint actual_codeset;
584         const gchar *const *codesets;
585         gint n_codesets;
586 #else /* !HAVE_LIBJCONV */
587         CharSet src_charset = C_AUTO, dest_charset = C_AUTO;
588 #endif
589
590         if (!dest_codeset) {
591                 CodeConvFunc func;
592
593                 func = conv_get_code_conv_func(src_codeset);
594                 if (func != conv_noconv) {
595                         if (func == conv_jistodisp ||
596                             func == conv_sjistodisp ||
597                             func == conv_anytodisp)
598                                 len = strlen(inbuf) * 2 + 1;
599                         else
600                                 len = strlen(inbuf) + 1;
601                         buf = g_malloc(len);
602                         if (!buf) return NULL;
603                         func(buf, len, inbuf);
604                         buf = g_realloc(buf, strlen(buf) + 1);
605                         return buf;
606                 }
607         }
608
609         /* don't convert if src and dest codeset are identical */
610         if (src_codeset && dest_codeset &&
611             !strcasecmp(src_codeset, dest_codeset))
612                 return g_strdup(inbuf);
613
614 #if HAVE_LIBJCONV
615         if (src_codeset) {
616                 codesets = &src_codeset;
617                 n_codesets = 1;
618         } else
619                 codesets = jconv_info_get_pref_codesets(&n_codesets);
620         if (!dest_codeset) {
621                 dest_codeset = conv_get_current_charset_str();
622                 /* don't convert if current codeset is US-ASCII */
623                 if (!strcasecmp(dest_codeset, CS_US_ASCII))
624                         return g_strdup(inbuf);
625         }
626
627         if (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
628                              codesets, n_codesets,
629                              &actual_codeset, dest_codeset)
630             == 0)
631                 return buf;
632         else {
633 #if 0
634                 g_warning("code conversion from %s to %s failed\n",
635                           codesets && codesets[0] ? codesets[0] : "(unknown)",
636                           dest_codeset);
637 #endif /* 0 */
638                 return NULL;
639         }
640 #else /* !HAVE_LIBJCONV */
641         if (src_codeset) {
642                 if (!strcasecmp(src_codeset, CS_EUC_JP) ||
643                     !strcasecmp(src_codeset, CS_EUCJP))
644                         src_charset = C_EUC_JP;
645                 else if (!strcasecmp(src_codeset, CS_SHIFT_JIS) ||
646                          !strcasecmp(src_codeset, "SHIFT-JIS") ||
647                          !strcasecmp(src_codeset, "SJIS"))
648                         src_charset = C_SHIFT_JIS;
649                 if (dest_codeset && !strcasecmp(dest_codeset, CS_ISO_2022_JP))
650                         dest_charset = C_ISO_2022_JP;
651         }
652
653         if ((src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS) &&
654             dest_charset == C_ISO_2022_JP) {
655                 len = (strlen(inbuf) + 1) * 3;
656                 buf = g_malloc(len);
657                 if (buf) {
658                         if (src_charset == C_EUC_JP)
659                                 conv_euctojis(buf, len, inbuf);
660                         else
661                                 conv_anytojis(buf, len, inbuf);
662                         buf = g_realloc(buf, strlen(buf) + 1);
663                 }
664         } else
665                 buf = g_strdup(inbuf);
666
667         return buf;
668 #endif /* !HAVE_LIBJCONV */
669 }
670
671 CodeConvFunc conv_get_code_conv_func(const gchar *charset)
672 {
673         CodeConvFunc code_conv;
674         CharSet cur_charset;
675
676         if (!charset) {
677                 cur_charset = conv_get_current_charset();
678                 if (cur_charset == C_EUC_JP || cur_charset == C_SHIFT_JIS)
679                         return conv_anytodisp;
680                 else
681                         return conv_noconv;
682         }
683
684         if (!strcasecmp(charset, CS_ISO_2022_JP) ||
685             !strcasecmp(charset, CS_ISO_2022_JP_2))
686                 code_conv = conv_jistodisp;
687         else if (!strcasecmp(charset, CS_US_ASCII))
688                 code_conv = conv_ustodisp;
689         else if (!strncasecmp(charset, CS_ISO_8859_1, 10))
690                 code_conv = conv_latintodisp;
691 #if !HAVE_LIBJCONV
692         else if (!strncasecmp(charset, "ISO-8859-", 9))
693                 code_conv = conv_latintodisp;
694 #endif
695         else if (!strcasecmp(charset, CS_SHIFT_JIS) ||
696                  !strcasecmp(charset, "SHIFT-JIS")  ||
697                  !strcasecmp(charset, "SJIS")       ||
698                  !strcasecmp(charset, "X-SJIS"))
699                 code_conv = conv_sjistodisp;
700         else if (!strcasecmp(charset, CS_EUC_JP) ||
701                  !strcasecmp(charset, CS_EUCJP))
702                 code_conv = conv_euctodisp;
703         else
704                 code_conv = conv_noconv;
705
706         return code_conv;
707 }
708
709 static const struct {
710         CharSet charset;
711         gchar *const name;
712 } charsets[] = {
713         {C_US_ASCII,            CS_US_ASCII},
714         {C_US_ASCII,            CS_ANSI_X3_4_1968},
715         {C_UTF_8,               CS_UTF_8},
716         {C_ISO_8859_1,          CS_ISO_8859_1},
717         {C_ISO_8859_2,          CS_ISO_8859_2},
718         {C_ISO_8859_4,          CS_ISO_8859_4},
719         {C_ISO_8859_5,          CS_ISO_8859_5},
720         {C_ISO_8859_7,          CS_ISO_8859_7},
721         {C_ISO_8859_8,          CS_ISO_8859_8},
722         {C_ISO_8859_9,          CS_ISO_8859_9},
723         {C_ISO_8859_11,         CS_ISO_8859_11},
724         {C_ISO_8859_13,         CS_ISO_8859_13},
725         {C_ISO_8859_15,         CS_ISO_8859_15},
726         {C_BALTIC,              CS_BALTIC},
727         {C_CP1251,              CS_CP1251},
728         {C_WINDOWS_1251,        CS_WINDOWS_1251},
729         {C_KOI8_R,              CS_KOI8_R},
730         {C_KOI8_U,              CS_KOI8_U},
731         {C_ISO_2022_JP,         CS_ISO_2022_JP},
732         {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
733         {C_EUC_JP,              CS_EUC_JP},
734         {C_EUC_JP,              CS_EUCJP},
735         {C_SHIFT_JIS,           CS_SHIFT_JIS},
736         {C_ISO_2022_KR,         CS_ISO_2022_KR},
737         {C_EUC_KR,              CS_EUC_KR},
738         {C_ISO_2022_CN,         CS_ISO_2022_CN},
739         {C_EUC_CN,              CS_EUC_CN},
740         {C_GB2312,              CS_GB2312},
741         {C_EUC_TW,              CS_EUC_TW},
742         {C_BIG5,                CS_BIG5},
743         {C_TIS_620,             CS_TIS_620},
744         {C_WINDOWS_874,         CS_WINDOWS_874},
745 };
746
747 #if !HAVE_LIBJCONV
748 static const struct {
749         gchar *const locale;
750         CharSet charset;
751         CharSet out_charset;
752 } locale_table[] = {
753         {"ja_JP.eucJP"  , C_EUC_JP      , C_ISO_2022_JP},
754         {"ja_JP.ujis"   , C_EUC_JP      , C_ISO_2022_JP},
755         {"ja_JP.EUC"    , C_EUC_JP      , C_ISO_2022_JP},
756         {"ja_JP.SJIS"   , C_SHIFT_JIS   , C_ISO_2022_JP},
757         {"ja_JP.JIS"    , C_ISO_2022_JP , C_ISO_2022_JP},
758         {"ja_JP"        , C_EUC_JP      , C_ISO_2022_JP},
759         {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
760         {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
761         {"zh_CN"        , C_GB2312      , C_GB2312},
762         {"zh_TW.eucTW"  , C_EUC_TW      , C_BIG5},
763         {"zh_TW.Big5"   , C_BIG5        , C_BIG5},
764         {"zh_TW"        , C_BIG5        , C_BIG5},
765
766         {"ru_RU.KOI8-R" , C_KOI8_R      , C_ISO_8859_5},
767         {"ru_RU.CP1251" , C_WINDOWS_1251, C_ISO_8859_5},
768
769         {"bg_BG"        , C_WINDOWS_1251, C_WINDOWS_1251},
770
771         {"en_US"        , C_ISO_8859_1  , C_ISO_8859_1},
772         {"ca_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
773         {"da_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
774         {"de_DE"        , C_ISO_8859_1  , C_ISO_8859_1},
775         {"nl_NL"        , C_ISO_8859_1  , C_ISO_8859_1},
776         {"et_EE"        , C_ISO_8859_1  , C_ISO_8859_1},
777         {"fi_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
778         {"fr_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
779         {"is_IS"        , C_ISO_8859_1  , C_ISO_8859_1},
780         {"it_IT"        , C_ISO_8859_1  , C_ISO_8859_1},
781         {"no_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
782         {"pt_PT"        , C_ISO_8859_1  , C_ISO_8859_1},
783         {"pt_BR"        , C_ISO_8859_1  , C_ISO_8859_1},
784         {"es_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
785         {"sv_SE"        , C_ISO_8859_1  , C_ISO_8859_1},
786
787         {"hr_HR"        , C_ISO_8859_2  , C_ISO_8859_2},
788         {"hu_HU"        , C_ISO_8859_2  , C_ISO_8859_2},
789         {"pl_PL"        , C_ISO_8859_2  , C_ISO_8859_2},
790         {"ro_RO"        , C_ISO_8859_2  , C_ISO_8859_2},
791         {"sk_SK"        , C_ISO_8859_2  , C_ISO_8859_2},
792         {"sl_SI"        , C_ISO_8859_2  , C_ISO_8859_2},
793         {"ru_RU"        , C_ISO_8859_5  , C_ISO_8859_5},
794         {"el_GR"        , C_ISO_8859_7  , C_ISO_8859_7},
795         {"iw_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
796         {"tr_TR"        , C_ISO_8859_9  , C_ISO_8859_9},
797
798         {"th_TH"        , C_TIS_620     , C_TIS_620},
799         /* {"th_TH"     , C_WINDOWS_874}, */
800         /* {"th_TH"     , C_ISO_8859_11}, */
801
802         {"lt_LT.iso88594"       , C_ISO_8859_4  , C_ISO_8859_4},
803         {"lt_LT.ISO8859-4"      , C_ISO_8859_4  , C_ISO_8859_4},
804         {"lt_LT.ISO_8859-4"     , C_ISO_8859_4  , C_ISO_8859_4},
805         {"lt_LT"                , C_ISO_8859_13 , C_ISO_8859_13},
806         {"lv_LV"                , C_ISO_8859_13 , C_ISO_8859_13},
807
808         {"C"                    , C_US_ASCII    , C_US_ASCII},
809         {"POSIX"                , C_US_ASCII    , C_US_ASCII},
810         {"ANSI_X3.4-1968"       , C_US_ASCII    , C_US_ASCII},
811 };
812 #endif /* !HAVE_LIBJCONV */
813
814 const gchar *conv_get_charset_str(CharSet charset)
815 {
816         gint i;
817
818         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
819                 if (charsets[i].charset == charset)
820                         return charsets[i].name;
821         }
822
823         return NULL;
824 }
825
826 CharSet conv_get_charset_from_str(const gchar *charset)
827 {
828         gint i;
829
830         if (!charset) return C_AUTO;
831
832         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
833                 if (!strcasecmp(charsets[i].name, charset))
834                         return charsets[i].charset;
835         }
836
837         return C_AUTO;
838 }
839
840 CharSet conv_get_current_charset(void)
841 {
842         static CharSet cur_charset = -1;
843         gint i;
844
845 #if HAVE_LIBJCONV
846         const gchar *cur_codeset;
847 #else
848         const gchar *cur_locale;
849 #endif
850
851         if (cur_charset != -1)
852                 return cur_charset;
853
854 #if HAVE_LIBJCONV
855         cur_codeset = jconv_info_get_current_codeset();
856         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
857                 if (!strcasecmp(cur_codeset, charsets[i].name)) {
858                         cur_charset = charsets[i].charset;
859                         return cur_charset;
860                 }
861         }
862 #else
863         cur_locale = conv_get_current_locale();
864         if (!cur_locale) {
865                 cur_charset = C_US_ASCII;
866                 return cur_charset;
867         }
868
869         if (strcasestr(cur_locale, "UTF-8")) {
870                 cur_charset = C_UTF_8;
871                 return cur_charset;
872         }
873
874         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
875                 const gchar *p;
876
877                 /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
878                 /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
879                 if (!strncasecmp(cur_locale, locale_table[i].locale,
880                                  strlen(locale_table[i].locale))) {
881                         cur_charset = locale_table[i].charset;
882                         return cur_charset;
883                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
884                          !strchr(p + 1, '.')) {
885                         if (strlen(cur_locale) == 2 &&
886                             !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
887                                 cur_charset = locale_table[i].charset;
888                                 return cur_charset;
889                         }
890                 }
891         }
892 #endif
893
894         cur_charset = C_AUTO;
895         return cur_charset;
896 }
897
898 const gchar *conv_get_current_charset_str(void)
899 {
900         static const gchar *codeset = NULL;
901
902         if (!codeset)
903                 codeset = conv_get_charset_str(conv_get_current_charset());
904
905         return codeset ? codeset : "US-ASCII";
906 }
907
908 CharSet conv_get_outgoing_charset(void)
909 {
910         static CharSet out_charset = -1;
911         gint i;
912
913 #if HAVE_LIBJCONV
914         gint j, n_pref_codesets;
915         const gchar *const *pref_codesets;
916 #else
917         const gchar *cur_locale;
918 #endif
919
920         if (out_charset != -1)
921                 return out_charset;
922
923 #if HAVE_LIBJCONV
924         /* skip US-ASCII and UTF-8 */
925         pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
926         for (i = 0; i < n_pref_codesets; i++) {
927                 for (j = 3; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
928                         if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
929                                 out_charset = charsets[j].charset;
930                                 return out_charset;
931                         }
932                 }
933         }
934
935         for (i = 0; i < n_pref_codesets; i++) {
936                 if (!strcasecmp(pref_codesets[i], "UTF-8")) {
937                         out_charset = C_UTF_8;
938                         return out_charset;
939                 }
940         }
941
942         out_charset = C_AUTO;
943 #else
944         cur_locale = conv_get_current_locale();
945         if (!cur_locale) {
946                 out_charset = C_AUTO;
947                 return out_charset;
948         }
949
950         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
951                 const gchar *p;
952
953                 if (!strncasecmp(cur_locale, locale_table[i].locale,
954                                  strlen(locale_table[i].locale))) {
955                         out_charset = locale_table[i].out_charset;
956                         break;
957                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
958                          !strchr(p + 1, '.')) {
959                         if (strlen(cur_locale) == 2 &&
960                             !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
961                                 out_charset = locale_table[i].out_charset;
962                                 break;
963                         }
964                 }
965         }
966
967         /* encoding conversion without libjconv is only supported
968            on Japanese locale for now */
969         if (out_charset == C_ISO_2022_JP)
970                 return out_charset;
971
972         out_charset = conv_get_current_charset();
973 #endif
974
975         return out_charset;
976 }
977
978 const gchar *conv_get_outgoing_charset_str(void)
979 {
980         CharSet out_charset;
981         const gchar *str;
982
983         if (prefs_common.outgoing_charset) {
984                 if (!isalpha(prefs_common.outgoing_charset[0])) {
985                         g_free(prefs_common.outgoing_charset);
986                         prefs_common.outgoing_charset = g_strdup(CS_AUTO);
987                 } else if (strcmp(prefs_common.outgoing_charset, CS_AUTO) != 0)
988                         return prefs_common.outgoing_charset;
989         }
990
991         out_charset = conv_get_outgoing_charset();
992         str = conv_get_charset_str(out_charset);
993
994         return str ? str : "US-ASCII";
995 }
996
997 const gchar *conv_get_current_locale(void)
998 {
999         gchar *cur_locale;
1000
1001         cur_locale = g_getenv("LC_ALL");
1002         if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
1003         if (!cur_locale) cur_locale = g_getenv("LANG");
1004         if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
1005
1006         debug_print("current locale: %s\n",
1007                     cur_locale ? cur_locale : "(none)");
1008
1009         return cur_locale;
1010 }
1011
1012 void conv_unmime_header_overwrite(gchar *str)
1013 {
1014         gchar *buf;
1015         gint buflen;
1016         CharSet cur_charset;
1017
1018         cur_charset = conv_get_current_charset();
1019
1020         if (cur_charset == C_EUC_JP) {
1021                 buflen = strlen(str) * 2 + 1;
1022                 Xalloca(buf, buflen, return);
1023                 conv_anytodisp(buf, buflen, str);
1024                 unmime_header(str, buf);
1025         } else {
1026                 buflen = strlen(str) + 1;
1027                 Xalloca(buf, buflen, return);
1028                 unmime_header(buf, str);
1029                 strncpy2(str, buf, buflen);
1030         }
1031 }
1032
1033 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
1034                         const gchar *charset)
1035 {
1036         CharSet cur_charset;
1037
1038         cur_charset = conv_get_current_charset();
1039
1040         if (cur_charset == C_EUC_JP) {
1041                 gchar *buf;
1042                 gint buflen;
1043
1044                 buflen = strlen(str) * 2 + 1;
1045                 Xalloca(buf, buflen, return);
1046                 conv_anytodisp(buf, buflen, str);
1047                 unmime_header(outbuf, buf);
1048         } else
1049                 unmime_header(outbuf, str);
1050 }
1051
1052 #define MAX_ENCLEN      75
1053 #define MAX_LINELEN     76
1054
1055 #define B64LEN(len)     ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
1056
1057 #if HAVE_LIBJCONV
1058 void conv_encode_header(gchar *dest, gint len, const gchar *src,
1059                         gint header_len)
1060 {
1061         wchar_t *wsrc;
1062         wchar_t *wsrcp;
1063         gchar *destp;
1064         size_t line_len, mimehdr_len, mimehdr_begin_len;
1065         gchar *mimehdr_init = "=?";
1066         gchar *mimehdr_end = "?=";
1067         gchar *mimehdr_enctype = "?B?";
1068         const gchar *mimehdr_charset;
1069
1070         /* g_print("src = %s\n", src); */
1071         mimehdr_charset = conv_get_outgoing_charset_str();
1072
1073         /* convert to wide-character string */
1074         wsrcp = wsrc = strdup_mbstowcs(src);
1075         if (!wsrc) {
1076                 g_warning("Can't convert string to wide characters.\n");
1077                 strncpy2(dest, src, len);
1078                 return;
1079         }
1080
1081         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
1082                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1083         mimehdr_begin_len = strlen(mimehdr_init) +
1084                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1085         line_len = header_len;
1086         destp = dest;
1087         *dest = '\0';
1088
1089         while (*wsrcp) {
1090                 wchar_t *wp, *wtmp, *wtmpp;
1091                 gint nspc = 0;
1092                 gboolean str_is_non_ascii;
1093
1094                 /* irresponsible buffer overrun check */
1095                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
1096
1097                 /* encode string including space
1098                    if non-ASCII string follows */
1099                 if (is_next_nonascii(wsrcp)) {
1100                         wp = wsrcp;
1101                         while ((wp = find_wspace(wp)) != NULL)
1102                                 if (!is_next_nonascii(wp)) break;
1103                         str_is_non_ascii = TRUE;
1104                 } else {
1105                         wp = find_wspace(wsrcp);
1106                         str_is_non_ascii = FALSE;
1107                 }
1108
1109                 if (wp != NULL) {
1110                         wtmp = wcsndup(wsrcp, wp - wsrcp);
1111                         wsrcp = wp + 1;
1112                         while (iswspace(wsrcp[nspc])) nspc++;
1113                 } else {
1114                         wtmp = wcsdup(wsrcp);
1115                         wsrcp += wcslen(wsrcp);
1116                 }
1117
1118                 wtmpp = wtmp;
1119
1120                 do {
1121                         gint tlen = 0;
1122                         gchar *tmp; /* internal codeset */
1123                         gchar *raw; /* converted, but not base64 encoded */
1124                         register gchar *tmpp;
1125                         gint raw_len;
1126
1127                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
1128                         *tmp = '\0';
1129                         raw = g_strdup("");
1130                         raw_len = 0;
1131
1132                         while (*wtmpp != (wchar_t)0) {
1133                                 gint mbl;
1134                                 gint dummy;
1135                                 gchar *raw_new = NULL;
1136                                 int raw_new_len = 0;
1137                                 const gchar *src_codeset;
1138
1139                                 mbl = wctomb(tmpp, *wtmpp);
1140                                 if (mbl == -1) {
1141                                         g_warning("invalid wide character\n");
1142                                         wtmpp++;
1143                                         continue;
1144                                 }
1145                                 /* g_free(raw); */
1146                                 src_codeset = conv_get_current_charset_str();
1147                                 /* printf ("tmp = %s, tlen = %d, mbl\n",
1148                                         tmp, tlen, mbl); */
1149                                 if (jconv_alloc_conv(tmp, tlen + mbl,
1150                                                      &raw_new, &raw_new_len,
1151                                                      &src_codeset, 1,
1152                                                      &dummy, mimehdr_charset)
1153                                     != 0) {
1154                                         g_warning("can't convert\n");
1155                                         tmpp[0] = '\0';
1156                                         wtmpp++;
1157                                         continue;
1158                                 }
1159                                 if (str_is_non_ascii) {
1160                                         gint dlen = mimehdr_len +
1161                                                 B64LEN(raw_len);
1162                                         if ((line_len + dlen +
1163                                              (*(wtmpp + 1) ? 0 : nspc) +
1164                                              (line_len > 1 ? 1 : 0))
1165                                             > MAX_LINELEN) {
1166                                                 g_free(raw_new);
1167                                                 if (tlen == 0) {
1168                                                         *destp++ = '\n';
1169                                                         *destp++ = ' ';
1170                                                         line_len = 1;
1171                                                         continue;
1172                                                 } else {
1173                                                         *tmpp = '\0';
1174                                                         break;
1175                                                 }
1176                                         }
1177                                 } else if ((line_len + tlen + mbl +
1178                                             (*(wtmpp + 1) ? 0 : nspc) +
1179                                             (line_len > 1 ? 1 : 0))
1180                                            > MAX_LINELEN) {
1181                                         g_free(raw_new);
1182                                         if (1 + tlen + mbl +
1183                                             (*(wtmpp + 1) ? 0 : nspc)
1184                                             >= MAX_LINELEN) {
1185                                                 *tmpp = '\0';
1186                                                 break;
1187                                         }
1188                                         *destp++ = '\n';
1189                                         *destp++ = ' ';
1190                                         line_len = 1;
1191                                         continue;
1192                                 }
1193
1194                                 tmpp += mbl;
1195                                 *tmpp = '\0';
1196
1197                                 tlen += mbl;
1198
1199                                 g_free(raw);
1200                                 raw = raw_new;
1201                                 raw_len = raw_new_len;
1202
1203                                 wtmpp++;
1204                         }
1205                         /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
1206                                 tmp, tlen, mb_seqlen); */
1207
1208                         if (tlen == 0 || raw_len == 0) {
1209                                 g_free(tmp);
1210                                 g_free(raw);
1211                                 continue;
1212                         }
1213
1214                         if (line_len > 1 && destp > dest) {
1215                                 *destp++ = ' ';
1216                                 *destp = '\0';
1217                                 line_len++;
1218                         }
1219
1220                         if (str_is_non_ascii) {
1221                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
1222                                            mimehdr_init, mimehdr_charset,
1223                                            mimehdr_enctype);
1224                                 destp += mimehdr_begin_len;
1225                                 line_len += mimehdr_begin_len;
1226
1227                                 base64_encode(destp, raw, raw_len);
1228                                 line_len += strlen(destp);
1229                                 destp += strlen(destp);
1230
1231                                 strcpy(destp, mimehdr_end);
1232                                 destp += strlen(mimehdr_end);
1233                                 line_len += strlen(mimehdr_end);
1234                         } else {
1235                                 strcpy(destp, tmp);
1236                                 line_len += strlen(destp);
1237                                 destp += strlen(destp);
1238                         }
1239
1240                         g_free(tmp);
1241                         g_free(raw);
1242                         /* g_print("line_len = %d\n\n", line_len); */
1243                 } while (*wtmpp != (wchar_t)0);
1244
1245                 while (iswspace(*wsrcp)) {
1246                         gint mbl;
1247
1248                         mbl = wctomb(destp, *wsrcp++);
1249                         if (mbl != -1) {
1250                                 destp += mbl;
1251                                 line_len += mbl;
1252                         }
1253                 }
1254                 *destp = '\0';
1255
1256                 g_free(wtmp);
1257         }
1258
1259         g_free(wsrc);
1260
1261         /* g_print("dest = %s\n", dest); */
1262 }
1263 #else /* !HAVE_LIBJCONV */
1264
1265 #define JIS_SEQLEN      3
1266
1267 void conv_encode_header(gchar *dest, gint len, const gchar *src,
1268                         gint header_len)
1269 {
1270         wchar_t *wsrc;
1271         wchar_t *wsrcp;
1272         gchar *destp;
1273         size_t line_len, mimehdr_len, mimehdr_begin_len;
1274         gchar *mimehdr_init = "=?";
1275         gchar *mimehdr_end = "?=";
1276         gchar *mimehdr_enctype = "?B?";
1277         const gchar *mimehdr_charset;
1278         gboolean do_conv = FALSE;
1279
1280         /* g_print("src = %s\n", src); */
1281         mimehdr_charset = conv_get_outgoing_charset_str();
1282         if (strcmp(mimehdr_charset, "ISO-2022-JP") == 0)
1283                 do_conv = TRUE;
1284         else if (strcmp(mimehdr_charset, "US-ASCII") == 0)
1285                 mimehdr_charset = "ISO-8859-1";
1286
1287         /* convert to wide-character string */
1288         wsrcp = wsrc = strdup_mbstowcs(src);
1289         if (!wsrc) {
1290                 g_warning("Can't convert string to wide characters.\n");
1291                 strncpy2(dest, src, len);
1292                 return;
1293         }
1294
1295         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
1296                       strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1297         mimehdr_begin_len = strlen(mimehdr_init) +
1298                             strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1299         line_len = header_len;
1300         destp = dest;
1301         *dest = '\0';
1302
1303         while (*wsrcp) {
1304                 wchar_t *wp, *wtmp, *wtmpp;
1305                 gint nspc = 0;
1306                 gboolean str_is_non_ascii;
1307
1308                 /* irresponsible buffer overrun check */
1309                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
1310
1311                 /* encode string including space
1312                    if non-ASCII string follows */
1313                 if (is_next_nonascii(wsrcp)) {
1314                         wp = wsrcp;
1315                         while ((wp = find_wspace(wp)) != NULL)
1316                                 if (!is_next_nonascii(wp)) break;
1317                         str_is_non_ascii = TRUE;
1318                 } else {
1319                         wp = find_wspace(wsrcp);
1320                         str_is_non_ascii = FALSE;
1321                 }
1322
1323                 if (wp != NULL) {
1324                         wtmp = wcsndup(wsrcp, wp - wsrcp);
1325                         wsrcp = wp + 1;
1326                         while (iswspace(wsrcp[nspc])) nspc++;
1327                 } else {
1328                         wtmp = wcsdup(wsrcp);
1329                         wsrcp += wcslen(wsrcp);
1330                 }
1331
1332                 wtmpp = wtmp;
1333
1334                 do {
1335                         gint prev_mbl = 1, tlen = 0, mb_seqlen = 0;
1336                         gchar *tmp;
1337                         register gchar *tmpp;
1338
1339                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
1340                         *tmp = '\0';
1341
1342                         while (*wtmpp != (wchar_t)0) {
1343                                 gint mbl;
1344
1345                                 mbl = wctomb(tmpp, *wtmpp);
1346                                 if (mbl == -1) {
1347                                         g_warning("invalid wide character\n");
1348                                         wtmpp++;
1349                                         continue;
1350                                 }
1351
1352                                 /* length of KI + KO */
1353                                 if (do_conv && prev_mbl == 1 && mbl == 2)
1354                                         mb_seqlen += JIS_SEQLEN * 2;
1355
1356                                 if (str_is_non_ascii) {
1357                                         gint dlen = mimehdr_len +
1358                                                 B64LEN(tlen + mb_seqlen + mbl);
1359
1360                                         if ((line_len + dlen +
1361                                              (*(wtmpp + 1) ? 0 : nspc) +
1362                                              (line_len > 1 ? 1 : 0))
1363                                             > MAX_LINELEN) {
1364                                                 if (tlen == 0) {
1365                                                         *destp++ = '\n';
1366                                                         *destp++ = ' ';
1367                                                         line_len = 1;
1368                                                         mb_seqlen = 0;
1369                                                         continue;
1370                                                 } else {
1371                                                         *tmpp = '\0';
1372                                                         break;
1373                                                 }
1374                                         }
1375                                 } else if ((line_len + tlen + mbl +
1376                                             (*(wtmpp + 1) ? 0 : nspc) +
1377                                             (line_len > 1 ? 1 : 0))
1378                                            > MAX_LINELEN) {
1379                                         if (1 + tlen + mbl +
1380                                             (*(wtmpp + 1) ? 0 : nspc)
1381                                             >= MAX_LINELEN) {
1382                                                 *tmpp = '\0';
1383                                                 break;
1384                                         }
1385                                         *destp++ = '\n';
1386                                         *destp++ = ' ';
1387                                         line_len = 1;
1388                                         continue;
1389                                 }
1390
1391                                 tmpp += mbl;
1392                                 *tmpp = '\0';
1393
1394                                 tlen += mbl;
1395                                 prev_mbl = mbl;
1396
1397                                 wtmpp++;
1398                         }
1399                         /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
1400                                 tmp, tlen, mb_seqlen); */
1401
1402                         if (tlen == 0) {
1403                                 g_free(tmp);
1404                                 continue;
1405                         }
1406
1407                         if (line_len > 1 && destp > dest) {
1408                                 *destp++ = ' ';
1409                                 *destp = '\0';
1410                                 line_len++;
1411                         }
1412
1413                         if (str_is_non_ascii) {
1414                                 gchar *raw;
1415
1416                                 raw = g_new(gchar, tlen + mb_seqlen + 1);
1417                                 if (do_conv)
1418                                         conv_euctojis(raw, tlen + mb_seqlen + 1,
1419                                                       tmp);
1420                                 else
1421                                         strcpy(raw, tmp);
1422                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
1423                                            mimehdr_init, mimehdr_charset,
1424                                            mimehdr_enctype);
1425                                 destp += mimehdr_begin_len;
1426                                 line_len += mimehdr_begin_len;
1427
1428                                 base64_encode(destp, raw, strlen(raw));
1429                                 line_len += strlen(destp);
1430                                 destp += strlen(destp);
1431
1432                                 strcpy(destp, mimehdr_end);
1433                                 destp += strlen(mimehdr_end);
1434                                 line_len += strlen(mimehdr_end);
1435
1436                                 g_free(raw);
1437                         } else {
1438                                 strcpy(destp, tmp);
1439                                 line_len += strlen(destp);
1440                                 destp += strlen(destp);
1441                         }
1442
1443                         g_free(tmp);
1444                         /* g_print("line_len = %d\n\n", line_len); */
1445                 } while (*wtmpp != (wchar_t)0);
1446
1447                 while (iswspace(*wsrcp)) {
1448                         gint mbl;
1449
1450                         mbl = wctomb(destp, *wsrcp++);
1451                         if (mbl != -1) {
1452                                 destp += mbl;
1453                                 line_len += mbl;
1454                         }
1455                 }
1456                 *destp = '\0';
1457
1458                 g_free(wtmp);
1459         }
1460
1461         g_free(wsrc);
1462
1463         /* g_print("dest = %s\n", dest); */
1464 }
1465 #endif /* HAVE_LIBJCONV */