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