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