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