sync with 0.8.8cvs3
[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_LOCALE_H
30 #  include <locale.h>
31 #endif
32
33 #if HAVE_LIBJCONV
34 #  include <jconv.h>
35 #endif
36
37 #include "intl.h"
38 #include "codeconv.h"
39 #include "unmime.h"
40 #include "base64.h"
41 #include "quoted-printable.h"
42 #include "utils.h"
43 #include "prefs_common.h"
44
45 typedef enum
46 {
47         JIS_ASCII,
48         JIS_KANJI,
49         JIS_HWKANA,
50         JIS_AUXKANJI
51 } JISState;
52
53 #define SUBST_CHAR      '_'
54 #define ESC             '\033'
55
56 #define iseuckanji(c) \
57         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xfe)
58 #define iseuchwkana1(c) \
59         (((c) & 0xff) == 0x8e)
60 #define iseuchwkana2(c) \
61         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
62 #define iseucaux(c) \
63         (((c) & 0xff) == 0x8f)
64 #define isunprintableeuckanji(c) \
65         (((c) & 0xff) >= 0xa9 && ((c) & 0xff) <= 0xaf)
66 #define issjiskanji1(c) \
67         ((((c) & 0xff) >= 0x81 && ((c) & 0xff) <= 0x9f) || \
68          (((c) & 0xff) >= 0xe0 && ((c) & 0xff) <= 0xfc))
69 #define issjiskanji2(c) \
70         ((((c) & 0xff) >= 0x40 && ((c) & 0xff) <= 0x7e) || \
71          (((c) & 0xff) >= 0x80 && ((c) & 0xff) <= 0xfc))
72 #define issjishwkana(c) \
73         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
74
75 #define K_IN()                          \
76         if (state != JIS_KANJI) {       \
77                 *out++ = ESC;           \
78                 *out++ = '$';           \
79                 *out++ = 'B';           \
80                 state = JIS_KANJI;      \
81         }
82
83 #define K_OUT()                         \
84         if (state != JIS_ASCII) {       \
85                 *out++ = ESC;           \
86                 *out++ = '(';           \
87                 *out++ = 'B';           \
88                 state = JIS_ASCII;      \
89         }
90
91 #define HW_IN()                         \
92         if (state != JIS_HWKANA) {      \
93                 *out++ = ESC;           \
94                 *out++ = '(';           \
95                 *out++ = 'I';           \
96                 state = JIS_HWKANA;     \
97         }
98
99 #define AUX_IN()                        \
100         if (state != JIS_AUXKANJI) {    \
101                 *out++ = ESC;           \
102                 *out++ = '$';           \
103                 *out++ = '(';           \
104                 *out++ = 'D';           \
105                 state = JIS_AUXKANJI;   \
106         }
107
108 void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
109 {
110         const guchar *in = inbuf;
111         guchar *out = outbuf;
112         JISState state = JIS_ASCII;
113
114         while (*in != '\0') {
115                 if (*in == ESC) {
116                         in++;
117                         if (*in == '$') {
118                                 if (*(in + 1) == '@' || *(in + 1) == 'B') {
119                                         state = JIS_KANJI;
120                                         in += 2;
121                                 } else if (*(in + 1) == '(' &&
122                                            *(in + 2) == 'D') {
123                                         state = JIS_AUXKANJI;
124                                         in += 3;
125                                 } else {
126                                         /* unknown escape sequence */
127                                         state = JIS_ASCII;
128                                 }
129                         } else if (*in == '(') {
130                                 if (*(in + 1) == 'B' || *(in + 1) == 'J') {
131                                         state = JIS_ASCII;
132                                         in += 2;
133                                 } else if (*(in + 1) == 'I') {
134                                         state = JIS_HWKANA;
135                                         in += 2;
136                                 } else {
137                                         /* unknown escape sequence */
138                                         state = JIS_ASCII;
139                                 }
140                         } else {
141                                 /* unknown escape sequence */
142                                 state = JIS_ASCII;
143                         }
144                 } else if (*in == 0x0e) {
145                         state = JIS_HWKANA;
146                         in++;
147                 } else if (*in == 0x0f) {
148                         state = JIS_ASCII;
149                         in++;
150                 } else {
151                         switch (state) {
152                         case JIS_ASCII:
153                                 *out++ = *in++;
154                                 break;
155                         case JIS_KANJI:
156                                 *out++ = *in++ | 0x80;
157                                 if (*in == '\0') break;
158                                 *out++ = *in++ | 0x80;
159                                 break;
160                         case JIS_HWKANA:
161                                 *out++ = 0x8e;
162                                 *out++ = *in++ | 0x80;
163                                 break;
164                         case JIS_AUXKANJI:
165                                 *out++ = 0x8f;
166                                 *out++ = *in++ | 0x80;
167                                 if (*in == '\0') break;
168                                 *out++ = *in++ | 0x80;
169                                 break;
170                         }
171                 }
172         }
173
174         *out = '\0';
175 }
176
177 void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
178 {
179         const guchar *in = inbuf;
180         guchar *out = outbuf;
181         JISState state = JIS_ASCII;
182
183         while (*in != '\0') {
184                 if (isascii(*in)) {
185                         K_OUT();
186                         *out++ = *in++;
187                 } else if (iseuckanji(*in)) {
188                         if (iseuckanji(*(in + 1))) {
189                                 K_IN();
190                                 *out++ = *in++ & 0x7f;
191                                 *out++ = *in++ & 0x7f;
192                         } else {
193                                 K_OUT();
194                                 *out++ = SUBST_CHAR;
195                                 in++;
196                                 if (*in != '\0' && !isascii(*in)) {
197                                         *out++ = SUBST_CHAR;
198                                         in++;
199                                 }
200                         }
201                 } else if (iseuchwkana1(*in)) {
202                         in++;
203                         if (iseuchwkana2(*in)) {
204                                 HW_IN();
205                                 *out++ = *in++ & 0x7f;
206                         } else {
207                                 K_OUT();
208                                 if (*in != '\0' && !isascii(*in)) {
209                                         *out++ = SUBST_CHAR;
210                                         in++;
211                                 }
212                         }
213                 } else if (iseucaux(*in)) {
214                         in++;
215                         if (iseuckanji(*in) && iseuckanji(*(in + 1))) {
216                                 AUX_IN();
217                                 *out++ = *in++ & 0x7f;
218                                 *out++ = *in++ & 0x7f;
219                         } else {
220                                 K_OUT();
221                                 if (*in != '\0' && !isascii(*in)) {
222                                         *out++ = SUBST_CHAR;
223                                         in++;
224                                         if (*in != '\0' && !isascii(*in)) {
225                                                 *out++ = SUBST_CHAR;
226                                                 in++;
227                                         }
228                                 }
229                         }
230                 } else {
231                         K_OUT();
232                         *out++ = SUBST_CHAR;
233                         in++;
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         conv->code_conv_func = conv_get_code_conv_func(charset, NULL);
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         if (conv->code_conv_func != conv_noconv)
556                 conv->code_conv_func(outbuf, outlen, inbuf);
557         else {
558                 gchar *str;
559
560                 str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
561                 if (!str)
562                         return -1;
563                 else {
564                         strncpy2(outbuf, str, outlen);
565                         g_free(str);
566                 }
567         }
568 #else /* !HAVE_LIBJCONV */
569         conv->code_conv_func(outbuf, outlen, inbuf);
570 #endif
571
572         return 0;
573 }
574
575 gchar *conv_codeset_strdup(const gchar *inbuf,
576                            const gchar *src_codeset, const gchar *dest_codeset)
577 {
578         gchar *buf;
579         size_t len;
580         CodeConvFunc conv_func;
581 #if HAVE_LIBJCONV
582         gint actual_codeset;
583         const gchar *const *codesets;
584         gint n_codesets;
585 #endif
586
587         conv_func = conv_get_code_conv_func(src_codeset, dest_codeset);
588         if (conv_func != conv_noconv) {
589                 len = (strlen(inbuf) + 1) * 3;
590                 buf = g_malloc(len);
591                 if (!buf) return NULL;
592
593                 conv_func(buf, len, inbuf);
594                 return g_realloc(buf, strlen(buf) + 1);
595         }
596
597 #if !HAVE_LIBJCONV
598         return g_strdup(inbuf);
599 #else
600         /* don't convert if src and dest codeset are identical */
601         if (src_codeset && dest_codeset &&
602             !strcasecmp(src_codeset, dest_codeset))
603                 return g_strdup(inbuf);
604
605         if (src_codeset) {
606                 codesets = &src_codeset;
607                 n_codesets = 1;
608         } else
609                 codesets = jconv_info_get_pref_codesets(&n_codesets);
610         if (!dest_codeset) {
611                 dest_codeset = conv_get_current_charset_str();
612                 /* don't convert if current codeset is US-ASCII */
613                 if (!strcasecmp(dest_codeset, CS_US_ASCII))
614                         return g_strdup(inbuf);
615         }
616
617         if (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
618                              codesets, n_codesets,
619                              &actual_codeset, dest_codeset)
620             == 0)
621                 return buf;
622         else {
623 #if 0
624                 g_warning("code conversion from %s to %s failed\n",
625                           codesets && codesets[0] ? codesets[0] : "(unknown)",
626                           dest_codeset);
627 #endif /* 0 */
628                 return NULL;
629         }
630 #endif /* HAVE_LIBJCONV */
631 }
632
633 CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
634                                      const gchar *dest_charset_str)
635 {
636         CodeConvFunc code_conv = conv_noconv;
637         CharSet src_charset;
638         CharSet dest_charset;
639
640         if (!src_charset_str)
641                 src_charset = conv_get_current_charset();
642         else
643                 src_charset = conv_get_charset_from_str(src_charset_str);
644
645         /* auto detection mode */
646         if (!src_charset_str && !dest_charset_str) {
647                 if (src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS)
648                         return conv_anytodisp;
649                 else
650                         return conv_noconv;
651         }
652
653         dest_charset = conv_get_charset_from_str(dest_charset_str);
654
655         switch (src_charset) {
656         case C_ISO_2022_JP:
657         case C_ISO_2022_JP_2:
658                 if (dest_charset == C_AUTO)
659                         code_conv = conv_jistodisp;
660                 else if (dest_charset == C_EUC_JP)
661                         code_conv = conv_jistoeuc;
662                 break;
663         case C_US_ASCII:
664                 if (dest_charset == C_AUTO)
665                         code_conv = conv_ustodisp;
666                 break;
667         case C_ISO_8859_1:
668 #if !HAVE_LIBJCONV
669         case C_ISO_8859_2:
670         case C_ISO_8859_4:
671         case C_ISO_8859_5:
672         case C_ISO_8859_7:
673         case C_ISO_8859_8:
674         case C_ISO_8859_9:
675         case C_ISO_8859_11:
676         case C_ISO_8859_13:
677         case C_ISO_8859_15:
678 #endif
679                 if (dest_charset == C_AUTO)
680                         code_conv = conv_latintodisp;
681                 break;
682         case C_SHIFT_JIS:
683                 if (dest_charset == C_AUTO)
684                         code_conv = conv_sjistodisp;
685                 else if (dest_charset == C_EUC_JP)
686                         code_conv = conv_sjistoeuc;
687                 break;
688         case C_EUC_JP:
689                 if (dest_charset == C_AUTO)
690                         code_conv = conv_euctodisp;
691                 else if (dest_charset == C_ISO_2022_JP ||
692                          dest_charset == C_ISO_2022_JP_2)
693                         code_conv = conv_euctojis;
694                 break;
695         default:
696                 break;
697         }
698
699         return code_conv;
700 }
701
702 static const struct {
703         CharSet charset;
704         gchar *const name;
705 } charsets[] = {
706         {C_US_ASCII,            CS_US_ASCII},
707         {C_US_ASCII,            CS_ANSI_X3_4_1968},
708         {C_UTF_8,               CS_UTF_8},
709         {C_ISO_8859_1,          CS_ISO_8859_1},
710         {C_ISO_8859_2,          CS_ISO_8859_2},
711         {C_ISO_8859_4,          CS_ISO_8859_4},
712         {C_ISO_8859_5,          CS_ISO_8859_5},
713         {C_ISO_8859_7,          CS_ISO_8859_7},
714         {C_ISO_8859_8,          CS_ISO_8859_8},
715         {C_ISO_8859_9,          CS_ISO_8859_9},
716         {C_ISO_8859_11,         CS_ISO_8859_11},
717         {C_ISO_8859_13,         CS_ISO_8859_13},
718         {C_ISO_8859_15,         CS_ISO_8859_15},
719         {C_BALTIC,              CS_BALTIC},
720         {C_CP1251,              CS_CP1251},
721         {C_WINDOWS_1251,        CS_WINDOWS_1251},
722         {C_KOI8_R,              CS_KOI8_R},
723         {C_KOI8_U,              CS_KOI8_U},
724         {C_ISO_2022_JP,         CS_ISO_2022_JP},
725         {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
726         {C_EUC_JP,              CS_EUC_JP},
727         {C_EUC_JP,              CS_EUCJP},
728         {C_SHIFT_JIS,           CS_SHIFT_JIS},
729         {C_SHIFT_JIS,           CS_SHIFT__JIS},
730         {C_SHIFT_JIS,           CS_SJIS},
731         {C_ISO_2022_KR,         CS_ISO_2022_KR},
732         {C_EUC_KR,              CS_EUC_KR},
733         {C_ISO_2022_CN,         CS_ISO_2022_CN},
734         {C_EUC_CN,              CS_EUC_CN},
735         {C_GB2312,              CS_GB2312},
736         {C_EUC_TW,              CS_EUC_TW},
737         {C_BIG5,                CS_BIG5},
738         {C_TIS_620,             CS_TIS_620},
739         {C_WINDOWS_874,         CS_WINDOWS_874},
740 };
741
742 #if !HAVE_LIBJCONV
743 static const struct {
744         gchar *const locale;
745         CharSet charset;
746         CharSet out_charset;
747 } locale_table[] = {
748         {"ja_JP.eucJP"  , C_EUC_JP      , C_ISO_2022_JP},
749         {"ja_JP.ujis"   , C_EUC_JP      , C_ISO_2022_JP},
750         {"ja_JP.EUC"    , C_EUC_JP      , C_ISO_2022_JP},
751         {"ja_JP.SJIS"   , C_SHIFT_JIS   , C_ISO_2022_JP},
752         {"ja_JP.JIS"    , C_ISO_2022_JP , C_ISO_2022_JP},
753         {"ja_JP"        , C_EUC_JP      , C_ISO_2022_JP},
754         {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
755         {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
756         {"zh_CN"        , C_GB2312      , C_GB2312},
757         {"zh_TW.eucTW"  , C_EUC_TW      , C_BIG5},
758         {"zh_TW.Big5"   , C_BIG5        , C_BIG5},
759         {"zh_TW"        , C_BIG5        , C_BIG5},
760
761         {"ru_RU.KOI8-R" , C_KOI8_R      , C_ISO_8859_5},
762         {"ru_RU.CP1251" , C_WINDOWS_1251, C_ISO_8859_5},
763
764         {"bg_BG"        , C_WINDOWS_1251, C_WINDOWS_1251},
765
766         {"en_US"        , C_ISO_8859_1  , C_ISO_8859_1},
767         {"ca_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
768         {"da_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
769         {"de_DE"        , C_ISO_8859_1  , C_ISO_8859_1},
770         {"nl_NL"        , C_ISO_8859_1  , C_ISO_8859_1},
771         {"et_EE"        , C_ISO_8859_1  , C_ISO_8859_1},
772         {"fi_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
773         {"fr_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
774         {"is_IS"        , C_ISO_8859_1  , C_ISO_8859_1},
775         {"it_IT"        , C_ISO_8859_1  , C_ISO_8859_1},
776         {"no_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
777         {"pt_PT"        , C_ISO_8859_1  , C_ISO_8859_1},
778         {"pt_BR"        , C_ISO_8859_1  , C_ISO_8859_1},
779         {"es_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
780         {"sv_SE"        , C_ISO_8859_1  , C_ISO_8859_1},
781
782         {"hr_HR"        , C_ISO_8859_2  , C_ISO_8859_2},
783         {"hu_HU"        , C_ISO_8859_2  , C_ISO_8859_2},
784         {"pl_PL"        , C_ISO_8859_2  , C_ISO_8859_2},
785         {"ro_RO"        , C_ISO_8859_2  , C_ISO_8859_2},
786         {"sk_SK"        , C_ISO_8859_2  , C_ISO_8859_2},
787         {"sl_SI"        , C_ISO_8859_2  , C_ISO_8859_2},
788         {"ru_RU"        , C_ISO_8859_5  , C_ISO_8859_5},
789         {"el_GR"        , C_ISO_8859_7  , C_ISO_8859_7},
790         {"iw_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
791         {"tr_TR"        , C_ISO_8859_9  , C_ISO_8859_9},
792
793         {"th_TH"        , C_TIS_620     , C_TIS_620},
794         /* {"th_TH"     , C_WINDOWS_874}, */
795         /* {"th_TH"     , C_ISO_8859_11}, */
796
797         {"lt_LT.iso88594"       , C_ISO_8859_4  , C_ISO_8859_4},
798         {"lt_LT.ISO8859-4"      , C_ISO_8859_4  , C_ISO_8859_4},
799         {"lt_LT.ISO_8859-4"     , C_ISO_8859_4  , C_ISO_8859_4},
800         {"lt_LT"                , C_ISO_8859_13 , C_ISO_8859_13},
801         {"lv_LV"                , C_ISO_8859_13 , C_ISO_8859_13},
802
803         {"C"                    , C_US_ASCII    , C_US_ASCII},
804         {"POSIX"                , C_US_ASCII    , C_US_ASCII},
805         {"ANSI_X3.4-1968"       , C_US_ASCII    , C_US_ASCII},
806 };
807 #endif /* !HAVE_LIBJCONV */
808
809 const gchar *conv_get_charset_str(CharSet charset)
810 {
811         gint i;
812
813         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
814                 if (charsets[i].charset == charset)
815                         return charsets[i].name;
816         }
817
818         return NULL;
819 }
820
821 CharSet conv_get_charset_from_str(const gchar *charset)
822 {
823         gint i;
824
825         if (!charset) return C_AUTO;
826
827         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
828                 if (!strcasecmp(charsets[i].name, charset))
829                         return charsets[i].charset;
830         }
831
832         return C_AUTO;
833 }
834
835 CharSet conv_get_current_charset(void)
836 {
837         static CharSet cur_charset = -1;
838         gint i;
839
840 #if HAVE_LIBJCONV
841         const gchar *cur_codeset;
842 #else
843         const gchar *cur_locale;
844 #endif
845
846         if (cur_charset != -1)
847                 return cur_charset;
848
849 #if HAVE_LIBJCONV
850         cur_codeset = jconv_info_get_current_codeset();
851         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
852                 if (!strcasecmp(cur_codeset, charsets[i].name)) {
853                         cur_charset = charsets[i].charset;
854                         return cur_charset;
855                 }
856         }
857 #else
858         cur_locale = conv_get_current_locale();
859         if (!cur_locale) {
860                 cur_charset = C_US_ASCII;
861                 return cur_charset;
862         }
863
864         if (strcasestr(cur_locale, "UTF-8")) {
865                 cur_charset = C_UTF_8;
866                 return cur_charset;
867         }
868
869         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
870                 const gchar *p;
871
872                 /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
873                 /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
874                 if (!strncasecmp(cur_locale, locale_table[i].locale,
875                                  strlen(locale_table[i].locale))) {
876                         cur_charset = locale_table[i].charset;
877                         return cur_charset;
878                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
879                          !strchr(p + 1, '.')) {
880                         if (strlen(cur_locale) == 2 &&
881                             !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
882                                 cur_charset = locale_table[i].charset;
883                                 return cur_charset;
884                         }
885                 }
886         }
887 #endif
888
889         cur_charset = C_AUTO;
890         return cur_charset;
891 }
892
893 const gchar *conv_get_current_charset_str(void)
894 {
895         static const gchar *codeset = NULL;
896
897         if (!codeset)
898                 codeset = conv_get_charset_str(conv_get_current_charset());
899
900         return codeset ? codeset : "US-ASCII";
901 }
902
903 CharSet conv_get_outgoing_charset(void)
904 {
905         static CharSet out_charset = -1;
906         gint i;
907
908 #if HAVE_LIBJCONV
909         gint j, n_pref_codesets;
910         const gchar *const *pref_codesets;
911 #else
912         const gchar *cur_locale;
913 #endif
914
915         if (out_charset != -1)
916                 return out_charset;
917
918 #if HAVE_LIBJCONV
919         /* skip US-ASCII and UTF-8 */
920         pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
921         for (i = 0; i < n_pref_codesets; i++) {
922                 for (j = 3; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
923                         if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
924                                 out_charset = charsets[j].charset;
925                                 return out_charset;
926                         }
927                 }
928         }
929
930         for (i = 0; i < n_pref_codesets; i++) {
931                 if (!strcasecmp(pref_codesets[i], "UTF-8")) {
932                         out_charset = C_UTF_8;
933                         return out_charset;
934                 }
935         }
936
937         out_charset = C_AUTO;
938 #else
939         cur_locale = conv_get_current_locale();
940         if (!cur_locale) {
941                 out_charset = C_AUTO;
942                 return out_charset;
943         }
944
945         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
946                 const gchar *p;
947
948                 if (!strncasecmp(cur_locale, locale_table[i].locale,
949                                  strlen(locale_table[i].locale))) {
950                         out_charset = locale_table[i].out_charset;
951                         break;
952                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
953                          !strchr(p + 1, '.')) {
954                         if (strlen(cur_locale) == 2 &&
955                             !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
956                                 out_charset = locale_table[i].out_charset;
957                                 break;
958                         }
959                 }
960         }
961
962         /* encoding conversion without libjconv is only supported
963            on Japanese locale for now */
964         if (out_charset == C_ISO_2022_JP)
965                 return out_charset;
966
967         out_charset = conv_get_current_charset();
968 #endif
969
970         return out_charset;
971 }
972
973 const gchar *conv_get_outgoing_charset_str(void)
974 {
975         CharSet out_charset;
976         const gchar *str;
977
978         if (prefs_common.outgoing_charset) {
979                 if (!isalpha(prefs_common.outgoing_charset[0])) {
980                         g_free(prefs_common.outgoing_charset);
981                         prefs_common.outgoing_charset = g_strdup(CS_AUTO);
982                 } else if (strcmp(prefs_common.outgoing_charset, CS_AUTO) != 0)
983                         return prefs_common.outgoing_charset;
984         }
985
986         out_charset = conv_get_outgoing_charset();
987         str = conv_get_charset_str(out_charset);
988
989         return str ? str : "US-ASCII";
990 }
991
992 const gchar *conv_get_current_locale(void)
993 {
994         gchar *cur_locale;
995
996         cur_locale = g_getenv("LC_ALL");
997         if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
998         if (!cur_locale) cur_locale = g_getenv("LANG");
999         if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
1000
1001         debug_print("current locale: %s\n",
1002                     cur_locale ? cur_locale : "(none)");
1003
1004         return cur_locale;
1005 }
1006
1007 void conv_unmime_header_overwrite(gchar *str)
1008 {
1009         gchar *buf;
1010         gint buflen;
1011         CharSet cur_charset;
1012
1013         cur_charset = conv_get_current_charset();
1014
1015         if (cur_charset == C_EUC_JP) {
1016                 buflen = strlen(str) * 2 + 1;
1017                 Xalloca(buf, buflen, return);
1018                 conv_anytodisp(buf, buflen, str);
1019                 unmime_header(str, buf);
1020         } else {
1021                 buflen = strlen(str) + 1;
1022                 Xalloca(buf, buflen, return);
1023                 unmime_header(buf, str);
1024                 strncpy2(str, buf, buflen);
1025         }
1026 }
1027
1028 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
1029                         const gchar *charset)
1030 {
1031         CharSet cur_charset;
1032
1033         cur_charset = conv_get_current_charset();
1034
1035         if (cur_charset == C_EUC_JP) {
1036                 gchar *buf;
1037                 gint buflen;
1038
1039                 buflen = strlen(str) * 2 + 1;
1040                 Xalloca(buf, buflen, return);
1041                 conv_anytodisp(buf, buflen, str);
1042                 unmime_header(outbuf, buf);
1043         } else
1044                 unmime_header(outbuf, str);
1045 }
1046
1047 #define MAX_LINELEN     76
1048 #define MIMESEP_BEGIN   "=?"
1049 #define MIMESEP_END     "?="
1050
1051 #define B64LEN(len)     ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
1052
1053 #define LBREAK_IF_REQUIRED(cond)                                \
1054 {                                                               \
1055         if (len - (destp - dest) < MAX_LINELEN + 2) {           \
1056                 *destp = '\0';                                  \
1057                 return;                                         \
1058         }                                                       \
1059                                                                 \
1060         if ((cond) && *srcp) {                                  \
1061                 if (destp > dest && isspace(*(destp - 1)))      \
1062                         destp--;                                \
1063                 else if (isspace(*srcp))                        \
1064                         srcp++;                                 \
1065                 if (*srcp) {                                    \
1066                         *destp++ = '\n';                        \
1067                         *destp++ = ' ';                         \
1068                         left = MAX_LINELEN - 1;                 \
1069                 }                                               \
1070         }                                                       \
1071 }
1072
1073 void conv_encode_header(gchar *dest, gint len, const gchar *src,
1074                         gint header_len)
1075 {
1076         const gchar *cur_encoding;
1077         const gchar *out_encoding;
1078         gint mimestr_len;
1079         gchar *mimesep_enc;
1080         gint left;
1081         const gchar *srcp = src;
1082         gchar *destp = dest;
1083         gboolean use_base64;
1084
1085         if (MB_CUR_MAX > 1) {
1086                 use_base64 = TRUE;
1087                 mimesep_enc = "?B?";
1088         } else {
1089                 use_base64 = FALSE;
1090                 mimesep_enc = "?Q?";
1091         }
1092
1093         cur_encoding = conv_get_current_charset_str();
1094         if (!strcmp(cur_encoding, "US-ASCII"))
1095                 cur_encoding = "ISO-8859-1";
1096         out_encoding = conv_get_outgoing_charset_str();
1097         if (!strcmp(out_encoding, "US-ASCII"))
1098                 out_encoding = "ISO-8859-1";
1099
1100         mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
1101                 strlen(mimesep_enc) + strlen(MIMESEP_END);
1102
1103         left = MAX_LINELEN - header_len;
1104
1105         while (*srcp) {
1106                 LBREAK_IF_REQUIRED(left <= 0);
1107
1108                 while (isspace(*srcp)) {
1109                         *destp++ = *srcp++;
1110                         left--;
1111                         LBREAK_IF_REQUIRED(left <= 0);
1112                 }
1113
1114                 /* output as it is if the next word is ASCII string */
1115                 if (!is_next_nonascii(srcp)) {
1116                         gint word_len;
1117
1118                         word_len = get_next_word_len(srcp);
1119                         LBREAK_IF_REQUIRED(left < word_len);
1120                         while (word_len > 0) {
1121                                 LBREAK_IF_REQUIRED(left <= 0);
1122                                 *destp++ = *srcp++;
1123                                 left--;
1124                                 word_len--;
1125                         }
1126
1127                         continue;
1128                 }
1129
1130                 while (1) {
1131                         gint mb_len = 0;
1132                         gint cur_len = 0;
1133                         gchar *part_str;
1134                         gchar *out_str;
1135                         gchar *enc_str;
1136                         const gchar *p = srcp;
1137                         gint out_str_len;
1138                         gint out_enc_str_len;
1139                         gint mime_block_len;
1140                         gboolean cont = FALSE;
1141
1142                         while (*p != '\0') {
1143                                 if (isspace(*p) && !is_next_nonascii(p + 1))
1144                                         break;
1145
1146                                 if (MB_CUR_MAX > 1) {
1147                                         mb_len = mblen(p, MB_CUR_MAX);
1148                                         if (mb_len < 0) {
1149                                                 g_warning("conv_encode_header(): invalid multibyte character encountered\n");
1150                                                 mb_len = 1;
1151                                         }
1152                                 } else
1153                                         mb_len = 1;
1154
1155                                 Xstrndup_a(part_str, srcp, cur_len + mb_len, );
1156                                 out_str = conv_codeset_strdup
1157                                         (part_str, cur_encoding, out_encoding);
1158                                 if (!out_str) {
1159                                         g_warning("conv_encode_header(): code conversion failed\n");
1160                                         out_str = g_strdup(out_str);
1161                                 }
1162                                 out_str_len = strlen(out_str);
1163
1164                                 if (use_base64)
1165                                         out_enc_str_len = B64LEN(out_str_len);
1166                                 else
1167                                         out_enc_str_len =
1168                                                 qp_get_q_encoding_len(out_str);
1169
1170                                 g_free(out_str);
1171
1172                                 if (mimestr_len + out_enc_str_len <= left) {
1173                                         cur_len += mb_len;
1174                                         p += mb_len;
1175                                 } else if (cur_len == 0) {
1176                                         LBREAK_IF_REQUIRED(1);
1177                                         continue;
1178                                 } else {
1179                                         cont = TRUE;
1180                                         break;
1181                                 }
1182                         }
1183
1184                         if (cur_len > 0) {
1185                                 Xstrndup_a(part_str, srcp, cur_len, );
1186                                 out_str = conv_codeset_strdup
1187                                         (part_str, cur_encoding, out_encoding);
1188                                 if (!out_str) {
1189                                         g_warning("conv_encode_header(): code conversion failed\n");
1190                                         out_str = g_strdup(out_str);
1191                                 }
1192                                 out_str_len = strlen(out_str);
1193
1194                                 if (use_base64)
1195                                         out_enc_str_len = B64LEN(out_str_len);
1196                                 else
1197                                         out_enc_str_len =
1198                                                 qp_get_q_encoding_len(out_str);
1199
1200                                 Xalloca(enc_str, out_enc_str_len + 1, );
1201                                 if (use_base64)
1202                                         base64_encode(enc_str, out_str, out_str_len);
1203                                 else
1204                                         qp_q_encode(enc_str, out_str);
1205
1206                                 g_free(out_str);
1207
1208                                 /* output MIME-encoded string block */
1209                                 mime_block_len = mimestr_len + strlen(enc_str);
1210                                 g_snprintf(destp, mime_block_len + 1,
1211                                            MIMESEP_BEGIN "%s%s%s" MIMESEP_END,
1212                                            out_encoding, mimesep_enc, enc_str);
1213                                 destp += mime_block_len;
1214                                 srcp += cur_len;
1215
1216                                 left -= mime_block_len;
1217                         }
1218
1219                         LBREAK_IF_REQUIRED(cont);
1220
1221                         if (cur_len == 0)
1222                                 break;
1223                 }
1224         }
1225
1226         *destp = '\0';
1227 }
1228
1229 #undef LBREAK_IF_REQUIRED