Fix wrong test leading to dead code (and dead func), CID 1438531.
[claws.git] / src / codeconv.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <errno.h>
33
34 #if HAVE_LOCALE_H
35 #  include <locale.h>
36 #endif
37
38 #include "codeconv.h"
39 #include "unmime.h"
40 #include "quoted-printable.h"
41 #include "utils.h"
42 #include "prefs_common.h"
43
44 /* For unknown reasons the inconv.m4 macro undefs that macro if no
45    const is needed.  This would break the code below so we define it. */
46 #ifndef ICONV_CONST
47 #define ICONV_CONST
48 #endif
49
50 typedef enum
51 {
52         JIS_ASCII,
53         JIS_KANJI,
54         JIS_HWKANA,
55         JIS_AUXKANJI
56 } JISState;
57
58 #define SUBST_CHAR      0x5f;
59 #define ESC             '\033'
60
61 #define iseuckanji(c) \
62         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xfe)
63 #define iseuchwkana1(c) \
64         (((c) & 0xff) == 0x8e)
65 #define iseuchwkana2(c) \
66         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
67 #define iseucaux(c) \
68         (((c) & 0xff) == 0x8f)
69 #define issjiskanji1(c) \
70         ((((c) & 0xff) >= 0x81 && ((c) & 0xff) <= 0x9f) || \
71          (((c) & 0xff) >= 0xe0 && ((c) & 0xff) <= 0xfc))
72 #define issjiskanji2(c) \
73         ((((c) & 0xff) >= 0x40 && ((c) & 0xff) <= 0x7e) || \
74          (((c) & 0xff) >= 0x80 && ((c) & 0xff) <= 0xfc))
75 #define issjishwkana(c) \
76         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
77
78 #define K_IN()                          \
79         if (state != JIS_KANJI) {       \
80                 *out++ = ESC;           \
81                 *out++ = '$';           \
82                 *out++ = 'B';           \
83                 state = JIS_KANJI;      \
84         }
85
86 #define K_OUT()                         \
87         if (state != JIS_ASCII) {       \
88                 *out++ = ESC;           \
89                 *out++ = '(';           \
90                 *out++ = 'B';           \
91                 state = JIS_ASCII;      \
92         }
93
94 #define HW_IN()                         \
95         if (state != JIS_HWKANA) {      \
96                 *out++ = ESC;           \
97                 *out++ = '(';           \
98                 *out++ = 'I';           \
99                 state = JIS_HWKANA;     \
100         }
101
102 #define AUX_IN()                        \
103         if (state != JIS_AUXKANJI) {    \
104                 *out++ = ESC;           \
105                 *out++ = '$';           \
106                 *out++ = '(';           \
107                 *out++ = 'D';           \
108                 state = JIS_AUXKANJI;   \
109         }
110
111 static CodeConvFunc conv_get_code_conv_func     (const gchar    *src_charset_str,
112                                          const gchar    *dest_charset_str);
113
114 static gchar *conv_iconv_strdup_with_cd (const gchar    *inbuf,
115                                          iconv_t         cd);
116
117 static gchar *conv_iconv_strdup         (const gchar    *inbuf,
118                                          const gchar    *src_code,
119                                          const gchar    *dest_code);
120
121 static CharSet conv_get_locale_charset                  (void);
122 static CharSet conv_get_outgoing_charset                (void);
123 static CharSet conv_guess_ja_encoding(const gchar *str);
124 static gboolean conv_is_ja_locale                       (void);
125
126 static gint conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
127 static gint conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf);
128 static gint conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
129
130 static gint conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
131 static gint conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
132 static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
133 static gint conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
134
135 static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
136 static gint conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf);
137
138 static void conv_unreadable_8bit(gchar *str);
139
140 static gint conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
141 static gint conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
142 static gint conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
143
144 static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
145 static gint conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
146 static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf);
147
148 static gboolean strict_mode = FALSE;
149
150 void codeconv_set_strict(gboolean mode)
151 {
152         strict_mode = mode;
153 }
154
155 static gint conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
156 {
157         const guchar *in = inbuf;
158         gchar *out = outbuf;
159         JISState state = JIS_ASCII;
160
161         cm_return_val_if_fail(outbuf != NULL, 0);
162
163         /*
164          * Loop outputs up to 3 bytes in each pass (aux kanji) and we
165          * need 1 byte to terminate the output
166          */
167         while (*in != '\0' && (out - outbuf) < outlen - 4) {
168                 if (*in == ESC) {
169                         in++;
170                         if (*in == '$') {
171                                 if (*(in + 1) == '@' || *(in + 1) == 'B') {
172                                         state = JIS_KANJI;
173                                         in += 2;
174                                 } else if (*(in + 1) == '(' &&
175                                            *(in + 2) == 'D') {
176                                         state = JIS_AUXKANJI;
177                                         in += 3;
178                                 } else {
179                                         /* unknown escape sequence */
180                                         state = JIS_ASCII;
181                                 }
182                         } else if (*in == '(') {
183                                 if (*(in + 1) == 'B' || *(in + 1) == 'J') {
184                                         state = JIS_ASCII;
185                                         in += 2;
186                                 } else if (*(in + 1) == 'I') {
187                                         state = JIS_HWKANA;
188                                         in += 2;
189                                 } else {
190                                         /* unknown escape sequence */
191                                         state = JIS_ASCII;
192                                 }
193                         } else {
194                                 /* unknown escape sequence */
195                                 state = JIS_ASCII;
196                         }
197                 } else if (*in == 0x0e) {
198                         state = JIS_HWKANA;
199                         in++;
200                 } else if (*in == 0x0f) {
201                         state = JIS_ASCII;
202                         in++;
203                 } else {
204                         switch (state) {
205                         case JIS_ASCII:
206                                 *out++ = *in++;
207                                 break;
208                         case JIS_KANJI:
209                                 *out++ = *in++ | 0x80;
210                                 if (*in == '\0') break;
211                                 *out++ = *in++ | 0x80;
212                                 break;
213                         case JIS_HWKANA:
214                                 *out++ = 0x8e;
215                                 *out++ = *in++ | 0x80;
216                                 break;
217                         case JIS_AUXKANJI:
218                                 *out++ = 0x8f;
219                                 *out++ = *in++ | 0x80;
220                                 if (*in == '\0') break;
221                                 *out++ = *in++ | 0x80;
222                                 break;
223                         }
224                 }
225         }
226
227         *out = '\0';
228         return 0;
229 }
230
231 #define JIS_HWDAKUTEN           0x5e
232 #define JIS_HWHANDAKUTEN        0x5f
233
234 static gint conv_jis_hantozen(guchar *outbuf, guchar jis_code, guchar sound_sym)
235 {
236         static guint16 h2z_tbl[] = {
237                 /* 0x20 - 0x2f */
238                 0x0000, 0x2123, 0x2156, 0x2157, 0x2122, 0x2126, 0x2572, 0x2521,
239                 0x2523, 0x2525, 0x2527, 0x2529, 0x2563, 0x2565, 0x2567, 0x2543,
240                 /* 0x30 - 0x3f */
241                 0x213c, 0x2522, 0x2524, 0x2526, 0x2528, 0x252a, 0x252b, 0x252d,
242                 0x252f, 0x2531, 0x2533, 0x2535, 0x2537, 0x2539, 0x253b, 0x253d,
243                 /* 0x40 - 0x4f */
244                 0x253f, 0x2541, 0x2544, 0x2546, 0x2548, 0x254a, 0x254b, 0x254c,
245                 0x254d, 0x254e, 0x254f, 0x2552, 0x2555, 0x2558, 0x255b, 0x255e,
246                 /* 0x50 - 0x5f */
247                 0x255f, 0x2560, 0x2561, 0x2562, 0x2564, 0x2566, 0x2568, 0x2569,
248                 0x256a, 0x256b, 0x256c, 0x256d, 0x256f, 0x2573, 0x212b, 0x212c
249         };
250
251         static guint16 dakuten_tbl[] = {
252                 /* 0x30 - 0x3f */
253                 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x252c, 0x252e,
254                 0x2530, 0x2532, 0x2534, 0x2536, 0x2538, 0x253a, 0x253c, 0x253e,
255                 /* 0x40 - 0x4f */
256                 0x2540, 0x2542, 0x2545, 0x2547, 0x2549, 0x0000, 0x0000, 0x0000,
257                 0x0000, 0x0000, 0x2550, 0x2553, 0x2556, 0x2559, 0x255c, 0x0000
258         };
259
260         static guint16 handakuten_tbl[] = {
261                 /* 0x4a - 0x4e */
262                 0x2551, 0x2554, 0x2557, 0x255a, 0x255d
263         };
264
265         guint16 out_code;
266
267         cm_return_val_if_fail(outbuf != NULL, 0);
268
269         jis_code &= 0x7f;
270         sound_sym &= 0x7f;
271
272         if (jis_code < 0x21 || jis_code > 0x5f)
273                 return 0;
274
275         if (sound_sym == JIS_HWDAKUTEN &&
276             jis_code >= 0x36 && jis_code <= 0x4e) {
277                 out_code = dakuten_tbl[jis_code - 0x30];
278                 if (out_code != 0) {
279                         *outbuf = out_code >> 8;
280                         *(outbuf + 1) = out_code & 0xff;
281                         return 2;
282                 }
283         }
284
285         if (sound_sym == JIS_HWHANDAKUTEN &&
286             jis_code >= 0x4a && jis_code <= 0x4e) {
287                 out_code = handakuten_tbl[jis_code - 0x4a];
288                 *outbuf = out_code >> 8;
289                 *(outbuf + 1) = out_code & 0xff;
290                 return 2;
291         }
292
293         out_code = h2z_tbl[jis_code - 0x20];
294         *outbuf = out_code >> 8;
295         *(outbuf + 1) = out_code & 0xff;
296         return 1;
297 }
298
299 static gint conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
300 {
301         const guchar *in = inbuf;
302         gchar *out = outbuf;
303         JISState state = JIS_ASCII;
304
305         cm_return_val_if_fail(outbuf != NULL, 0);
306
307         /*
308          * Loop outputs up to 6 bytes in each pass (aux shift + aux
309          * kanji) and we need up to 4 bytes to terminate the output
310          * (ASCII shift + null)
311          */
312         while (*in != '\0' && (out - outbuf) < outlen - 10) {
313                 if (IS_ASCII(*in)) {
314                         K_OUT();
315                         *out++ = *in++;
316                 } else if (iseuckanji(*in)) {
317                         if (iseuckanji(*(in + 1))) {
318                                 K_IN();
319                                 *out++ = *in++ & 0x7f;
320                                 *out++ = *in++ & 0x7f;
321                         } else {
322                                 K_OUT();
323                                 *out++ = SUBST_CHAR;
324                                 in++;
325                                 if (*in != '\0' && !IS_ASCII(*in)) {
326                                         *out++ = SUBST_CHAR;
327                                         in++;
328                                 }
329                         }
330                 } else if (iseuchwkana1(*in)) {
331                         if (iseuchwkana2(*(in + 1))) {
332                                 if (prefs_common.allow_jisx0201_kana) {
333                                         HW_IN();
334                                         in++;
335                                         *out++ = *in++ & 0x7f;
336                                 } else {
337                                         guchar jis_ch[2];
338                                         gint len;
339
340                                         if (iseuchwkana1(*(in + 2)) &&
341                                             iseuchwkana2(*(in + 3)))
342                                                 len = conv_jis_hantozen
343                                                         (jis_ch,
344                                                          *(in + 1), *(in + 3));
345                                         else
346                                                 len = conv_jis_hantozen
347                                                         (jis_ch,
348                                                          *(in + 1), '\0');
349                                         if (len == 0)
350                                                 in += 2;
351                                         else {
352                                                 K_IN();
353                                                 in += len * 2;
354                                                 *out++ = jis_ch[0];
355                                                 *out++ = jis_ch[1];
356                                         }
357                                 }
358                         } else {
359                                 K_OUT();
360                                 in++;
361                                 if (*in != '\0' && !IS_ASCII(*in)) {
362                                         *out++ = SUBST_CHAR;
363                                         in++;
364                                 }
365                         }
366                 } else if (iseucaux(*in)) {
367                         in++;
368                         if (iseuckanji(*in) && iseuckanji(*(in + 1))) {
369                                 AUX_IN();
370                                 *out++ = *in++ & 0x7f;
371                                 *out++ = *in++ & 0x7f;
372                         } else {
373                                 K_OUT();
374                                 if (*in != '\0' && !IS_ASCII(*in)) {
375                                         *out++ = SUBST_CHAR;
376                                         in++;
377                                         if (*in != '\0' && !IS_ASCII(*in)) {
378                                                 *out++ = SUBST_CHAR;
379                                                 in++;
380                                         }
381                                 }
382                         }
383                 } else {
384                         K_OUT();
385                         *out++ = SUBST_CHAR;
386                         in++;
387                 }
388         }
389
390         K_OUT();
391         *out = '\0';
392         return 0;
393 }
394
395 static gint conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
396 {
397         const guchar *in = inbuf;
398         gchar *out = outbuf;
399
400         cm_return_val_if_fail(outbuf != NULL, 0);
401
402         /*
403          * Loop outputs up to 2 bytes in each pass and we need 1 byte
404          * to terminate the output
405          */
406         while (*in != '\0' && (out - outbuf) < outlen - 3) {
407                 if (IS_ASCII(*in)) {
408                         *out++ = *in++;
409                 } else if (issjiskanji1(*in)) {
410                         if (issjiskanji2(*(in + 1))) {
411                                 guchar out1 = *in;
412                                 guchar out2 = *(in + 1);
413                                 guchar row;
414
415                                 row = out1 < 0xa0 ? 0x70 : 0xb0;
416                                 if (out2 < 0x9f) {
417                                         out1 = (out1 - row) * 2 - 1;
418                                         out2 -= out2 > 0x7f ? 0x20 : 0x1f;
419                                 } else {
420                                         out1 = (out1 - row) * 2;
421                                         out2 -= 0x7e;
422                                 }
423
424                                 *out++ = out1 | 0x80;
425                                 *out++ = out2 | 0x80;
426                                 in += 2;
427                         } else {
428                                 *out++ = SUBST_CHAR;
429                                 in++;
430                                 if (*in != '\0' && !IS_ASCII(*in)) {
431                                         *out++ = SUBST_CHAR;
432                                         in++;
433                                 }
434                         }
435                 } else if (issjishwkana(*in)) {
436                         *out++ = 0x8e;
437                         *out++ = *in++;
438                 } else {
439                         *out++ = SUBST_CHAR;
440                         in++;
441                 }
442         }
443
444         *out = '\0';
445         return 0;
446 }
447
448 static gint conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
449 {
450         gchar *eucstr;
451
452         cm_return_val_if_fail(inbuf != NULL, 0);
453         cm_return_val_if_fail(outbuf != NULL, 0);
454
455         Xalloca(eucstr, outlen, return -1);
456
457         if (conv_jistoeuc(eucstr, outlen, inbuf) <0)
458                 return -1;
459         if (conv_euctoutf8(outbuf, outlen, eucstr) < 0)
460                 return -1;
461         return 0;
462 }
463
464 static gint conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
465 {
466         gchar *tmpstr;
467
468         cm_return_val_if_fail(inbuf != NULL, 0);
469         cm_return_val_if_fail(outbuf != NULL, 0);
470
471         tmpstr = conv_iconv_strdup(inbuf, CS_SHIFT_JIS, CS_UTF_8);
472         if (tmpstr) {
473                 strncpy2(outbuf, tmpstr, outlen);
474                 g_free(tmpstr);
475                 return 0;
476         } else {
477                 strncpy2(outbuf, inbuf, outlen);
478                 return -1;
479         }
480 }
481
482 static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
483 {
484         static iconv_t cd = (iconv_t)-1;
485         static gboolean iconv_ok = TRUE;
486         gchar *tmpstr;
487
488         cm_return_val_if_fail(inbuf != NULL, 0);
489         cm_return_val_if_fail(outbuf != NULL, 0);
490
491         if (cd == (iconv_t)-1) {
492                 if (!iconv_ok) {
493                         strncpy2(outbuf, inbuf, outlen);
494                         return -1;
495                 }
496                 cd = iconv_open(CS_UTF_8, CS_EUC_JP_MS);
497                 if (cd == (iconv_t)-1) {
498                         cd = iconv_open(CS_UTF_8, CS_EUC_JP);
499                         if (cd == (iconv_t)-1) {
500                                 g_warning("conv_euctoutf8(): %s",
501                                           g_strerror(errno));
502                                 iconv_ok = FALSE;
503                                 strncpy2(outbuf, inbuf, outlen);
504                                 return -1;
505                         }
506                 }
507         }
508
509         tmpstr = conv_iconv_strdup_with_cd(inbuf, cd);
510         if (tmpstr) {
511                 strncpy2(outbuf, tmpstr, outlen);
512                 g_free(tmpstr);
513                 return 0;
514         } else {
515                 strncpy2(outbuf, inbuf, outlen);
516                 return -1;
517         }
518 }
519
520 static gint conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
521 {
522         gint r = -1;
523
524         cm_return_val_if_fail(inbuf != NULL, 0);
525         cm_return_val_if_fail(outbuf != NULL, 0);
526
527         switch (conv_guess_ja_encoding(inbuf)) {
528         case C_ISO_2022_JP:
529                 r = conv_jistoutf8(outbuf, outlen, inbuf);
530                 break;
531         case C_SHIFT_JIS:
532                 r = conv_sjistoutf8(outbuf, outlen, inbuf);
533                 break;
534         case C_EUC_JP:
535                 r = conv_euctoutf8(outbuf, outlen, inbuf);
536                 break;
537         default:
538                 r = 0;
539                 strncpy2(outbuf, inbuf, outlen);
540                 break;
541         }
542         
543         return r;
544 }
545
546 static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
547 {
548         static iconv_t cd = (iconv_t)-1;
549         static gboolean iconv_ok = TRUE;
550         gchar *tmpstr;
551
552         cm_return_val_if_fail(inbuf != NULL, 0);
553         cm_return_val_if_fail(outbuf != NULL, 0);
554
555         if (cd == (iconv_t)-1) {
556                 if (!iconv_ok) {
557                         strncpy2(outbuf, inbuf, outlen);
558                         return -1;
559                 }
560                 cd = iconv_open(CS_EUC_JP_MS, CS_UTF_8);
561                 if (cd == (iconv_t)-1) {
562                         cd = iconv_open(CS_EUC_JP, CS_UTF_8);
563                         if (cd == (iconv_t)-1) {
564                                 g_warning("conv_utf8toeuc(): %s",
565                                           g_strerror(errno));
566                                 iconv_ok = FALSE;
567                                 strncpy2(outbuf, inbuf, outlen);
568                                 return -1;
569                         }
570                 }
571         }
572
573         tmpstr = conv_iconv_strdup_with_cd(inbuf, cd);
574         if (tmpstr) {
575                 strncpy2(outbuf, tmpstr, outlen);
576                 g_free(tmpstr);
577                 return 0;
578         } else {
579                 strncpy2(outbuf, inbuf, outlen);
580                 return -1;
581         }
582 }
583
584 static gint conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf)
585 {
586         gchar *eucstr;
587
588         cm_return_val_if_fail(inbuf != NULL, 0);
589         cm_return_val_if_fail(outbuf != NULL, 0);
590
591         Xalloca(eucstr, outlen, return -1);
592
593         if (conv_utf8toeuc(eucstr, outlen, inbuf) < 0)
594                 return -1;
595         if (conv_euctojis(outbuf, outlen, eucstr) < 0)
596                 return -1;
597                 
598         return 0;
599 }
600
601 static void conv_unreadable_8bit(gchar *str)
602 {
603         register guchar *p = str;
604
605         while (*p != '\0') {
606                 /* convert CR+LF -> LF */
607                 if (*p == '\r' && *(p + 1) == '\n')
608                         memmove(p, p + 1, strlen(p));
609                 else if (!IS_ASCII(*p)) *p = SUBST_CHAR;
610                 p++;
611         }
612 }
613
614 static CharSet conv_guess_ja_encoding(const gchar *str)
615 {
616         const guchar *p = str;
617         CharSet guessed = C_US_ASCII;
618
619         while (*p != '\0') {
620                 if (*p == ESC && (*(p + 1) == '$' || *(p + 1) == '(')) {
621                         if (guessed == C_US_ASCII)
622                                 return C_ISO_2022_JP;
623                         p += 2;
624                 } else if (IS_ASCII(*p)) {
625                         p++;
626                 } else if (iseuckanji(*p) && iseuckanji(*(p + 1))) {
627                         if (*p >= 0xfd && *p <= 0xfe)
628                                 return C_EUC_JP;
629                         else if (guessed == C_SHIFT_JIS) {
630                                 if ((issjiskanji1(*p) &&
631                                      issjiskanji2(*(p + 1))) ||
632                                     issjishwkana(*p))
633                                         guessed = C_SHIFT_JIS;
634                                 else
635                                         guessed = C_EUC_JP;
636                         } else
637                                 guessed = C_EUC_JP;
638                         p += 2;
639                 } else if (issjiskanji1(*p) && issjiskanji2(*(p + 1))) {
640                         if (iseuchwkana1(*p) && iseuchwkana2(*(p + 1)))
641                                 guessed = C_SHIFT_JIS;
642                         else
643                                 return C_SHIFT_JIS;
644                         p += 2;
645                 } else if (issjishwkana(*p)) {
646                         guessed = C_SHIFT_JIS;
647                         p++;
648                 } else {
649                         p++;
650                 }
651         }
652
653         return guessed;
654 }
655
656 static gint conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
657 {
658         cm_return_val_if_fail(inbuf != NULL, 0);
659         cm_return_val_if_fail(outbuf != NULL, 0);
660
661         return conv_jistoutf8(outbuf, outlen, inbuf);
662 }
663
664 static gint conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
665 {
666         cm_return_val_if_fail(inbuf != NULL, 0);
667         cm_return_val_if_fail(outbuf != NULL, 0);
668
669         return conv_sjistoutf8(outbuf, outlen, inbuf);
670 }
671
672 static gint conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
673 {
674         cm_return_val_if_fail(inbuf != NULL, 0);
675         cm_return_val_if_fail(outbuf != NULL, 0);
676
677         return conv_euctoutf8(outbuf, outlen, inbuf);
678 }
679
680 void conv_utf8todisp(gchar *outbuf, gint outlen, const gchar *inbuf)
681 {
682         cm_return_if_fail(inbuf != NULL);
683         cm_return_if_fail(outbuf != NULL);
684
685         if (g_utf8_validate(inbuf, -1, NULL) == TRUE)
686                 strncpy2(outbuf, inbuf, outlen);
687         else
688                 conv_ustodisp(outbuf, outlen, inbuf);
689 }
690
691 static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
692 {
693         gint r = 0;
694
695         cm_return_val_if_fail(inbuf != NULL, 0);
696         cm_return_val_if_fail(outbuf != NULL, 0);
697
698         if (conv_anytoutf8(outbuf, outlen, inbuf) < 0)
699                 r = -1;
700         if (g_utf8_validate(outbuf, -1, NULL) != TRUE)
701                 conv_unreadable_8bit(outbuf);
702         return r;
703 }
704
705 static gint conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
706 {
707         cm_return_val_if_fail(inbuf != NULL, 0);
708         cm_return_val_if_fail(outbuf != NULL, 0);
709
710         strncpy2(outbuf, inbuf, outlen);
711         conv_unreadable_8bit(outbuf);
712         
713         return 0;
714 }
715
716 void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
717 {
718         gchar *tmpstr;
719
720         cm_return_if_fail(inbuf != NULL);
721         cm_return_if_fail(outbuf != NULL);
722
723         codeconv_set_strict(TRUE);
724         tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
725                                    CS_INTERNAL);
726         codeconv_set_strict(FALSE);
727         if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
728                 strncpy2(outbuf, tmpstr, outlen);
729                 g_free(tmpstr);
730                 return;
731         } else if (tmpstr && !g_utf8_validate(tmpstr, -1, NULL)) {
732                 g_free(tmpstr);
733                 codeconv_set_strict(TRUE);
734                 tmpstr = conv_iconv_strdup(inbuf, 
735                                 conv_get_locale_charset_str_no_utf8(),
736                                 CS_INTERNAL);
737                 codeconv_set_strict(FALSE);
738         }
739         if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
740                 strncpy2(outbuf, tmpstr, outlen);
741                 g_free(tmpstr);
742                 return;
743         } else {
744                 g_free(tmpstr);
745                 conv_utf8todisp(outbuf, outlen, inbuf);
746         }
747 }
748
749 static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
750 {
751         cm_return_val_if_fail(inbuf != NULL, 0);
752         cm_return_val_if_fail(outbuf != NULL, 0);
753
754         strncpy2(outbuf, inbuf, outlen);
755         return 0;
756 }
757
758 static const gchar *
759 conv_get_fallback_for_private_encoding(const gchar *encoding)
760 {
761         if (encoding) {
762                 if ((encoding[0] == 'X' || encoding[0] == 'x') &&
763                     encoding[1] == '-') {
764                         if (!g_ascii_strcasecmp(encoding, CS_X_MACCYR))
765                                 return CS_MACCYR;
766                         if (!g_ascii_strcasecmp(encoding, CS_X_GBK))
767                                 return CS_GBK;
768                 }
769                 else if(!g_ascii_strcasecmp(encoding, CS_ISO_8859_8_I)) {
770                         /*
771                          * ISO-8859-8-I is a variant which fully
772                          * agrees with ISO-8859-8 on character
773                          * codings, and differs only in directionality
774                          * implications, which are ignored here
775                          * anyway; and is not recognized by iconv
776                          */
777                         return CS_ISO_8859_8;
778                 }
779         }
780
781         return encoding;
782 }
783
784 CodeConverter *conv_code_converter_new(const gchar *src_charset)
785 {
786         CodeConverter *conv;
787
788         src_charset = conv_get_fallback_for_private_encoding(src_charset);
789
790         conv = g_new0(CodeConverter, 1);
791         conv->code_conv_func = conv_get_code_conv_func(src_charset, NULL);
792         conv->charset_str = g_strdup(src_charset);
793         conv->charset = conv_get_charset_from_str(src_charset);
794
795         return conv;
796 }
797
798 void conv_code_converter_destroy(CodeConverter *conv)
799 {
800         g_free(conv->charset_str);
801         g_free(conv);
802 }
803
804 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
805                   const gchar *inbuf)
806 {
807         cm_return_val_if_fail(inbuf != NULL, -1);
808         cm_return_val_if_fail(outbuf != NULL, -1);
809
810         if (conv->code_conv_func != conv_noconv)
811                 return conv->code_conv_func(outbuf, outlen, inbuf);
812         else {
813                 gchar *str;
814
815                 str = conv_iconv_strdup(inbuf, conv->charset_str, NULL);
816                 if (!str)
817                         return -1;
818                 else {
819                         strncpy2(outbuf, str, outlen);
820                         g_free(str);
821                 }
822         }
823
824         return 0;
825 }
826
827 gchar *conv_codeset_strdup(const gchar *inbuf,
828                            const gchar *src_code, const gchar *dest_code)
829 {
830         gchar *buf;
831         size_t len;
832         CodeConvFunc conv_func;
833
834         cm_return_val_if_fail(inbuf != NULL, NULL);
835
836         if (!strcmp2(src_code, dest_code)) {
837                 CharSet dest_charset = conv_get_charset_from_str(dest_code);
838                 if (strict_mode && dest_charset == C_UTF_8) {
839                         /* ensure valid UTF-8 if target is UTF-8 */
840                         if (!g_utf8_validate(inbuf, -1, NULL)) {
841                                 return NULL;
842                         }
843                 }
844                 /* otherwise, try for a lucky day */
845                 return g_strdup(inbuf);
846         }
847
848         src_code = conv_get_fallback_for_private_encoding(src_code);
849         conv_func = conv_get_code_conv_func(src_code, dest_code);
850         if (conv_func == conv_ustodisp && strict_mode && !is_ascii_str(inbuf))
851                 return NULL;
852
853         if (conv_func != conv_noconv) {
854                 len = (strlen(inbuf) + 1) * 3;
855                 buf = g_malloc(len);
856
857                 if (conv_func(buf, len, inbuf) == 0 || !strict_mode)
858                         return g_realloc(buf, strlen(buf) + 1);
859                 else {
860                         g_free(buf);
861                         return NULL;
862                 }
863         }
864
865         return conv_iconv_strdup(inbuf, src_code, dest_code);
866 }
867
868 static CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
869                                      const gchar *dest_charset_str)
870 {
871         CodeConvFunc code_conv = conv_noconv;
872         CharSet src_charset;
873         CharSet dest_charset;
874
875         if (!src_charset_str)
876                 src_charset = conv_get_locale_charset();
877         else
878                 src_charset = conv_get_charset_from_str(src_charset_str);
879
880         /* auto detection mode */
881         if (!src_charset_str && !dest_charset_str) {
882                 if (conv_is_ja_locale())
883                         return conv_anytodisp;
884                 else
885                         return conv_noconv;
886         }
887
888         dest_charset = conv_get_charset_from_str(dest_charset_str);
889
890         if (dest_charset == C_US_ASCII)
891                 return conv_ustodisp;
892
893         switch (src_charset) {
894         case C_US_ASCII:
895         case C_ISO_8859_1:
896         case C_ISO_8859_2:
897         case C_ISO_8859_3:
898         case C_ISO_8859_4:
899         case C_ISO_8859_5:
900         case C_ISO_8859_6:
901         case C_ISO_8859_7:
902         case C_ISO_8859_8:
903         case C_ISO_8859_9:
904         case C_ISO_8859_10:
905         case C_ISO_8859_11:
906         case C_ISO_8859_13:
907         case C_ISO_8859_14:
908         case C_ISO_8859_15:
909                 break;
910         case C_ISO_2022_JP:
911         case C_ISO_2022_JP_2:
912         case C_ISO_2022_JP_3:
913                 if (dest_charset == C_AUTO)
914                         code_conv = conv_jistodisp;
915                 else if (dest_charset == C_EUC_JP)
916                         code_conv = conv_jistoeuc;
917                 else if (dest_charset == C_UTF_8)
918                         code_conv = conv_jistoutf8;
919                 break;
920         case C_SHIFT_JIS:
921                 if (dest_charset == C_AUTO)
922                         code_conv = conv_sjistodisp;
923                 else if (dest_charset == C_EUC_JP)
924                         code_conv = conv_sjistoeuc;
925                 else if (dest_charset == C_UTF_8)
926                         code_conv = conv_sjistoutf8;
927                 break;
928         case C_EUC_JP:
929                 if (dest_charset == C_AUTO)
930                         code_conv = conv_euctodisp;
931                 else if (dest_charset == C_ISO_2022_JP   ||
932                          dest_charset == C_ISO_2022_JP_2 ||
933                          dest_charset == C_ISO_2022_JP_3)
934                         code_conv = conv_euctojis;
935                 else if (dest_charset == C_UTF_8)
936                         code_conv = conv_euctoutf8;
937                 break;
938         case C_UTF_8:
939                 if (dest_charset == C_EUC_JP)
940                         code_conv = conv_utf8toeuc;
941                 else if (dest_charset == C_ISO_2022_JP   ||
942                          dest_charset == C_ISO_2022_JP_2 ||
943                          dest_charset == C_ISO_2022_JP_3)
944                         code_conv = conv_utf8tojis;
945                 break;
946         default:
947                 break;
948         }
949
950         return code_conv;
951 }
952
953 static gchar *conv_iconv_strdup(const gchar *inbuf,
954                          const gchar *src_code, const gchar *dest_code)
955 {
956         iconv_t cd;
957         gchar *outbuf;
958
959         cm_return_val_if_fail(inbuf != NULL, NULL);
960
961         if (!src_code && !dest_code && 
962             g_utf8_validate(inbuf, -1, NULL))
963                 return g_strdup(inbuf);
964
965         if (!src_code)
966                 src_code = conv_get_outgoing_charset_str();
967         if (!dest_code)
968                 dest_code = CS_INTERNAL;
969
970         /* don't convert if src and dest codeset are identical */
971         if (!strcasecmp(src_code, dest_code))
972                 return g_strdup(inbuf);
973
974         /* don't convert if dest codeset is US-ASCII */
975         if (!strcasecmp(src_code, CS_US_ASCII))
976                 return g_strdup(inbuf);
977
978         /* don't convert if dest codeset is US-ASCII */
979         if (!strcasecmp(dest_code, CS_US_ASCII))
980                 return g_strdup(inbuf);
981
982         cd = iconv_open(dest_code, src_code);
983         if (cd == (iconv_t)-1)
984                 return NULL;
985
986         outbuf = conv_iconv_strdup_with_cd(inbuf, cd);
987
988         iconv_close(cd);
989
990         return outbuf;
991 }
992
993 gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
994 {
995         const gchar *inbuf_p;
996         gchar *outbuf;
997         gchar *outbuf_p;
998         size_t in_size;
999         size_t in_left;
1000         size_t out_size;
1001         size_t out_left;
1002         size_t n_conv;
1003         size_t len;
1004
1005         cm_return_val_if_fail(inbuf != NULL, NULL);
1006
1007         inbuf_p = inbuf;
1008         in_size = strlen(inbuf);
1009         in_left = in_size;
1010         out_size = (in_size + 1) * 2;
1011         outbuf = g_malloc(out_size);
1012         outbuf_p = outbuf;
1013         out_left = out_size;
1014
1015 #define EXPAND_BUF()                            \
1016 {                                               \
1017         len = outbuf_p - outbuf;                \
1018         out_size *= 2;                          \
1019         outbuf = g_realloc(outbuf, out_size);   \
1020         outbuf_p = outbuf + len;                \
1021         out_left = out_size - len;              \
1022 }
1023
1024         while ((n_conv = iconv(cd, (ICONV_CONST gchar **)&inbuf_p, &in_left,
1025                                &outbuf_p, &out_left)) == (size_t)-1) {
1026                 if (EILSEQ == errno) {
1027                         if (strict_mode) {
1028                                 g_free(outbuf);
1029                                 return NULL;
1030                         }
1031                         //g_print("iconv(): at %d: %s\n", in_size - in_left, g_strerror(errno));
1032                         inbuf_p++;
1033                         in_left--;
1034                         if (out_left == 0) {
1035                                 EXPAND_BUF();
1036                         }
1037                         *outbuf_p++ = SUBST_CHAR;
1038                         out_left--;
1039                 } else if (EINVAL == errno) {
1040                         break;
1041                 } else if (E2BIG == errno) {
1042                         EXPAND_BUF();
1043                 } else {
1044                         g_warning("conv_iconv_strdup(): %s",
1045                                   g_strerror(errno));
1046                         break;
1047                 }
1048         }
1049
1050         while ((n_conv = iconv(cd, NULL, NULL, &outbuf_p, &out_left)) ==
1051                (size_t)-1) {
1052                 if (E2BIG == errno) {
1053                         EXPAND_BUF();
1054                 } else {
1055                         g_warning("conv_iconv_strdup(): %s",
1056                                   g_strerror(errno));
1057                         break;
1058                 }
1059         }
1060
1061 #undef EXPAND_BUF
1062
1063         len = outbuf_p - outbuf;
1064         outbuf = g_realloc(outbuf, len + 1);
1065         outbuf[len] = '\0';
1066
1067         return outbuf;
1068 }
1069
1070 static const struct {
1071         CharSet charset;
1072         gchar *const name;
1073 } charsets[] = {
1074         {C_US_ASCII,            CS_US_ASCII},
1075         {C_US_ASCII,            CS_ANSI_X3_4_1968},
1076         {C_UTF_8,               CS_UTF_8},
1077         {C_UTF_7,               CS_UTF_7},
1078         {C_ISO_8859_1,          CS_ISO_8859_1},
1079         {C_ISO_8859_2,          CS_ISO_8859_2},
1080         {C_ISO_8859_3,          CS_ISO_8859_3},
1081         {C_ISO_8859_4,          CS_ISO_8859_4},
1082         {C_ISO_8859_5,          CS_ISO_8859_5},
1083         {C_ISO_8859_6,          CS_ISO_8859_6},
1084         {C_ISO_8859_7,          CS_ISO_8859_7},
1085         {C_ISO_8859_8,          CS_ISO_8859_8},
1086         {C_ISO_8859_9,          CS_ISO_8859_9},
1087         {C_ISO_8859_10,         CS_ISO_8859_10},
1088         {C_ISO_8859_11,         CS_ISO_8859_11},
1089         {C_ISO_8859_13,         CS_ISO_8859_13},
1090         {C_ISO_8859_14,         CS_ISO_8859_14},
1091         {C_ISO_8859_15,         CS_ISO_8859_15},
1092         {C_BALTIC,              CS_BALTIC},
1093         {C_CP1250,              CS_CP1250},
1094         {C_CP1251,              CS_CP1251},
1095         {C_CP1252,              CS_CP1252},
1096         {C_CP1253,              CS_CP1253},
1097         {C_CP1254,              CS_CP1254},
1098         {C_CP1255,              CS_CP1255},
1099         {C_CP1256,              CS_CP1256},
1100         {C_CP1257,              CS_CP1257},
1101         {C_CP1258,              CS_CP1258},
1102         {C_WINDOWS_1250,        CS_WINDOWS_1250},
1103         {C_WINDOWS_1251,        CS_WINDOWS_1251},
1104         {C_WINDOWS_1252,        CS_WINDOWS_1252},
1105         {C_WINDOWS_1253,        CS_WINDOWS_1253},
1106         {C_WINDOWS_1254,        CS_WINDOWS_1254},
1107         {C_WINDOWS_1255,        CS_WINDOWS_1255},
1108         {C_WINDOWS_1256,        CS_WINDOWS_1256},
1109         {C_WINDOWS_1257,        CS_WINDOWS_1257},
1110         {C_WINDOWS_1258,        CS_WINDOWS_1258},
1111         {C_KOI8_R,              CS_KOI8_R},
1112         {C_MACCYR,              CS_MACCYR},
1113         {C_KOI8_T,              CS_KOI8_T},
1114         {C_KOI8_U,              CS_KOI8_U},
1115         {C_ISO_2022_JP,         CS_ISO_2022_JP},
1116         {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
1117         {C_ISO_2022_JP_3,       CS_ISO_2022_JP_3},
1118         {C_EUC_JP,              CS_EUC_JP},
1119         {C_EUC_JP,              CS_EUCJP},
1120         {C_EUC_JP_MS,           CS_EUC_JP_MS},
1121         {C_SHIFT_JIS,           CS_SHIFT_JIS},
1122         {C_SHIFT_JIS,           CS_SHIFT__JIS},
1123         {C_SHIFT_JIS,           CS_SJIS},
1124         {C_ISO_2022_KR,         CS_ISO_2022_KR},
1125         {C_EUC_KR,              CS_EUC_KR},
1126         {C_ISO_2022_CN,         CS_ISO_2022_CN},
1127         {C_EUC_CN,              CS_EUC_CN},
1128         {C_GB18030,             CS_GB18030},
1129         {C_GB2312,              CS_GB2312},
1130         {C_GBK,                 CS_GBK},
1131         {C_EUC_TW,              CS_EUC_TW},
1132         {C_BIG5,                CS_BIG5},
1133         {C_BIG5_HKSCS,          CS_BIG5_HKSCS},
1134         {C_TIS_620,             CS_TIS_620},
1135         {C_WINDOWS_874,         CS_WINDOWS_874},
1136         {C_GEORGIAN_PS,         CS_GEORGIAN_PS},
1137         {C_TCVN5712_1,          CS_TCVN5712_1},
1138 };
1139
1140 static const struct {
1141         gchar *const locale;
1142         CharSet charset;
1143         CharSet out_charset;
1144 } locale_table[] = {
1145         {"ja_JP.eucJP"          , C_EUC_JP      , C_ISO_2022_JP},
1146         {"ja_JP.EUC-JP"         , C_EUC_JP      , C_ISO_2022_JP},
1147         {"ja_JP.EUC"            , C_EUC_JP      , C_ISO_2022_JP},
1148         {"ja_JP.ujis"           , C_EUC_JP      , C_ISO_2022_JP},
1149         {"ja_JP.SJIS"           , C_SHIFT_JIS   , C_ISO_2022_JP},
1150         {"ja_JP.JIS"            , C_ISO_2022_JP , C_ISO_2022_JP},
1151 #ifdef G_OS_WIN32
1152         {"ja_JP"                , C_SHIFT_JIS   , C_ISO_2022_JP},
1153 #else
1154         {"ja_JP"                , C_EUC_JP      , C_ISO_2022_JP},
1155 #endif
1156         {"ko_KR.EUC-KR"         , C_EUC_KR      , C_EUC_KR},
1157         {"ko_KR"                , C_EUC_KR      , C_EUC_KR},
1158         {"zh_CN.GB18030"        , C_GB18030     , C_GB18030},
1159         {"zh_CN.GB2312"         , C_GB2312      , C_GB2312},
1160         {"zh_CN.GBK"            , C_GBK         , C_GBK},
1161         {"zh_CN"                , C_GB18030     , C_GB18030},
1162         {"zh_HK"                , C_BIG5_HKSCS  , C_BIG5_HKSCS},
1163         {"zh_TW.eucTW"          , C_EUC_TW      , C_BIG5},
1164         {"zh_TW.EUC-TW"         , C_EUC_TW      , C_BIG5},
1165         {"zh_TW.Big5"           , C_BIG5        , C_BIG5},
1166         {"zh_TW"                , C_BIG5        , C_BIG5},
1167
1168         {"ru_RU.KOI8-R"         , C_KOI8_R      , C_KOI8_R},
1169         {"ru_RU.KOI8R"          , C_KOI8_R      , C_KOI8_R},
1170         {"ru_RU.CP1251"         , C_WINDOWS_1251, C_KOI8_R},
1171 #ifdef G_OS_WIN32
1172         {"ru_RU"                , C_WINDOWS_1251, C_KOI8_R},
1173 #else
1174         {"ru_RU"                , C_ISO_8859_5  , C_KOI8_R},
1175 #endif
1176         {"tg_TJ"                , C_KOI8_T      , C_KOI8_T},
1177         {"ru_UA"                , C_KOI8_U      , C_KOI8_U},
1178         {"uk_UA.CP1251"         , C_WINDOWS_1251, C_KOI8_U},
1179         {"uk_UA"                , C_KOI8_U      , C_KOI8_U},
1180
1181         {"be_BY"                , C_WINDOWS_1251, C_WINDOWS_1251},
1182         {"bg_BG"                , C_WINDOWS_1251, C_WINDOWS_1251},
1183
1184         {"yi_US"                , C_WINDOWS_1255, C_WINDOWS_1255},
1185
1186         {"af_ZA"                , C_ISO_8859_1  , C_ISO_8859_1},
1187         {"br_FR"                , C_ISO_8859_1  , C_ISO_8859_1},
1188         {"ca_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
1189         {"da_DK"                , C_ISO_8859_1  , C_ISO_8859_1},
1190         {"de_AT"                , C_ISO_8859_1  , C_ISO_8859_1},
1191         {"de_BE"                , C_ISO_8859_1  , C_ISO_8859_1},
1192         {"de_CH"                , C_ISO_8859_1  , C_ISO_8859_1},
1193         {"de_DE"                , C_ISO_8859_1  , C_ISO_8859_1},
1194         {"de_LU"                , C_ISO_8859_1  , C_ISO_8859_1},
1195         {"en_AU"                , C_ISO_8859_1  , C_ISO_8859_1},
1196         {"en_BW"                , C_ISO_8859_1  , C_ISO_8859_1},
1197         {"en_CA"                , C_ISO_8859_1  , C_ISO_8859_1},
1198         {"en_DK"                , C_ISO_8859_1  , C_ISO_8859_1},
1199         {"en_GB"                , C_ISO_8859_1  , C_ISO_8859_1},
1200         {"en_HK"                , C_ISO_8859_1  , C_ISO_8859_1},
1201         {"en_IE"                , C_ISO_8859_1  , C_ISO_8859_1},
1202         {"en_NZ"                , C_ISO_8859_1  , C_ISO_8859_1},
1203         {"en_PH"                , C_ISO_8859_1  , C_ISO_8859_1},
1204         {"en_SG"                , C_ISO_8859_1  , C_ISO_8859_1},
1205         {"en_US"                , C_ISO_8859_1  , C_ISO_8859_1},
1206         {"en_ZA"                , C_ISO_8859_1  , C_ISO_8859_1},
1207         {"en_ZW"                , C_ISO_8859_1  , C_ISO_8859_1},
1208         {"es_AR"                , C_ISO_8859_1  , C_ISO_8859_1},
1209         {"es_BO"                , C_ISO_8859_1  , C_ISO_8859_1},
1210         {"es_CL"                , C_ISO_8859_1  , C_ISO_8859_1},
1211         {"es_CO"                , C_ISO_8859_1  , C_ISO_8859_1},
1212         {"es_CR"                , C_ISO_8859_1  , C_ISO_8859_1},
1213         {"es_DO"                , C_ISO_8859_1  , C_ISO_8859_1},
1214         {"es_EC"                , C_ISO_8859_1  , C_ISO_8859_1},
1215         {"es_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
1216         {"es_GT"                , C_ISO_8859_1  , C_ISO_8859_1},
1217         {"es_HN"                , C_ISO_8859_1  , C_ISO_8859_1},
1218         {"es_MX"                , C_ISO_8859_1  , C_ISO_8859_1},
1219         {"es_NI"                , C_ISO_8859_1  , C_ISO_8859_1},
1220         {"es_PA"                , C_ISO_8859_1  , C_ISO_8859_1},
1221         {"es_PE"                , C_ISO_8859_1  , C_ISO_8859_1},
1222         {"es_PR"                , C_ISO_8859_1  , C_ISO_8859_1},
1223         {"es_PY"                , C_ISO_8859_1  , C_ISO_8859_1},
1224         {"es_SV"                , C_ISO_8859_1  , C_ISO_8859_1},
1225         {"es_US"                , C_ISO_8859_1  , C_ISO_8859_1},
1226         {"es_UY"                , C_ISO_8859_1  , C_ISO_8859_1},
1227         {"es_VE"                , C_ISO_8859_1  , C_ISO_8859_1},
1228         {"et_EE"                , C_ISO_8859_1  , C_ISO_8859_1},
1229         {"eu_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
1230         {"fi_FI"                , C_ISO_8859_1  , C_ISO_8859_1},
1231         {"fo_FO"                , C_ISO_8859_1  , C_ISO_8859_1},
1232         {"fr_BE"                , C_ISO_8859_1  , C_ISO_8859_1},
1233         {"fr_CA"                , C_ISO_8859_1  , C_ISO_8859_1},
1234         {"fr_CH"                , C_ISO_8859_1  , C_ISO_8859_1},
1235         {"fr_FR"                , C_ISO_8859_1  , C_ISO_8859_1},
1236         {"fr_LU"                , C_ISO_8859_1  , C_ISO_8859_1},
1237         {"ga_IE"                , C_ISO_8859_1  , C_ISO_8859_1},
1238         {"gl_ES"                , C_ISO_8859_1  , C_ISO_8859_1},
1239         {"gv_GB"                , C_ISO_8859_1  , C_ISO_8859_1},
1240         {"id_ID"                , C_ISO_8859_1  , C_ISO_8859_1},
1241         {"is_IS"                , C_ISO_8859_1  , C_ISO_8859_1},
1242         {"it_CH"                , C_ISO_8859_1  , C_ISO_8859_1},
1243         {"it_IT"                , C_ISO_8859_1  , C_ISO_8859_1},
1244         {"kl_GL"                , C_ISO_8859_1  , C_ISO_8859_1},
1245         {"kw_GB"                , C_ISO_8859_1  , C_ISO_8859_1},
1246         {"ms_MY"                , C_ISO_8859_1  , C_ISO_8859_1},
1247         {"nl_BE"                , C_ISO_8859_1  , C_ISO_8859_1},
1248         {"nl_NL"                , C_ISO_8859_1  , C_ISO_8859_1},
1249         {"nb_NO"                , C_ISO_8859_1  , C_ISO_8859_1},
1250         {"nn_NO"                , C_ISO_8859_1  , C_ISO_8859_1},
1251         {"no_NO"                , C_ISO_8859_1  , C_ISO_8859_1},
1252         {"oc_FR"                , C_ISO_8859_1  , C_ISO_8859_1},
1253         {"pt_BR"                , C_ISO_8859_1  , C_ISO_8859_1},
1254         {"pt_PT"                , C_ISO_8859_1  , C_ISO_8859_1},
1255         {"sq_AL"                , C_ISO_8859_1  , C_ISO_8859_1},
1256         {"sv_FI"                , C_ISO_8859_1  , C_ISO_8859_1},
1257         {"sv_SE"                , C_ISO_8859_1  , C_ISO_8859_1},
1258         {"tl_PH"                , C_ISO_8859_1  , C_ISO_8859_1},
1259         {"uz_UZ"                , C_ISO_8859_1  , C_ISO_8859_1},
1260         {"wa_BE"                , C_ISO_8859_1  , C_ISO_8859_1},
1261
1262         {"bs_BA"                , C_ISO_8859_2  , C_ISO_8859_2},
1263         {"cs_CZ"                , C_ISO_8859_2  , C_ISO_8859_2},
1264         {"hr_HR"                , C_ISO_8859_2  , C_ISO_8859_2},
1265         {"hu_HU"                , C_ISO_8859_2  , C_ISO_8859_2},
1266         {"pl_PL"                , C_ISO_8859_2  , C_ISO_8859_2},
1267         {"ro_RO"                , C_ISO_8859_2  , C_ISO_8859_2},
1268         {"sk_SK"                , C_ISO_8859_2  , C_ISO_8859_2},
1269         {"sl_SI"                , C_ISO_8859_2  , C_ISO_8859_2},
1270
1271         {"sr_YU@cyrillic"       , C_ISO_8859_5  , C_ISO_8859_5},
1272         {"sr_YU"                , C_ISO_8859_2  , C_ISO_8859_2},
1273
1274         {"mt_MT"                , C_ISO_8859_3  , C_ISO_8859_3},
1275
1276         {"lt_LT.iso88594"       , C_ISO_8859_4  , C_ISO_8859_4},
1277         {"lt_LT.ISO8859-4"      , C_ISO_8859_4  , C_ISO_8859_4},
1278         {"lt_LT.ISO_8859-4"     , C_ISO_8859_4  , C_ISO_8859_4},
1279         {"lt_LT"                , C_ISO_8859_13 , C_ISO_8859_13},
1280
1281         {"mk_MK"                , C_ISO_8859_5  , C_ISO_8859_5},
1282
1283         {"ar_AE"                , C_ISO_8859_6  , C_ISO_8859_6},
1284         {"ar_BH"                , C_ISO_8859_6  , C_ISO_8859_6},
1285         {"ar_DZ"                , C_ISO_8859_6  , C_ISO_8859_6},
1286         {"ar_EG"                , C_ISO_8859_6  , C_ISO_8859_6},
1287         {"ar_IQ"                , C_ISO_8859_6  , C_ISO_8859_6},
1288         {"ar_JO"                , C_ISO_8859_6  , C_ISO_8859_6},
1289         {"ar_KW"                , C_ISO_8859_6  , C_ISO_8859_6},
1290         {"ar_LB"                , C_ISO_8859_6  , C_ISO_8859_6},
1291         {"ar_LY"                , C_ISO_8859_6  , C_ISO_8859_6},
1292         {"ar_MA"                , C_ISO_8859_6  , C_ISO_8859_6},
1293         {"ar_OM"                , C_ISO_8859_6  , C_ISO_8859_6},
1294         {"ar_QA"                , C_ISO_8859_6  , C_ISO_8859_6},
1295         {"ar_SA"                , C_ISO_8859_6  , C_ISO_8859_6},
1296         {"ar_SD"                , C_ISO_8859_6  , C_ISO_8859_6},
1297         {"ar_SY"                , C_ISO_8859_6  , C_ISO_8859_6},
1298         {"ar_TN"                , C_ISO_8859_6  , C_ISO_8859_6},
1299         {"ar_YE"                , C_ISO_8859_6  , C_ISO_8859_6},
1300
1301         {"el_GR"                , C_ISO_8859_7  , C_ISO_8859_7},
1302         {"he_IL"                , C_ISO_8859_8  , C_ISO_8859_8},
1303         {"iw_IL"                , C_ISO_8859_8  , C_ISO_8859_8},
1304         {"tr_TR"                , C_ISO_8859_9  , C_ISO_8859_9},
1305
1306         {"lv_LV"                , C_ISO_8859_13 , C_ISO_8859_13},
1307         {"mi_NZ"                , C_ISO_8859_13 , C_ISO_8859_13},
1308
1309         {"cy_GB"                , C_ISO_8859_14 , C_ISO_8859_14},
1310
1311         {"ar_IN"                , C_UTF_8       , C_UTF_8},
1312         {"en_IN"                , C_UTF_8       , C_UTF_8},
1313         {"se_NO"                , C_UTF_8       , C_UTF_8},
1314         {"ta_IN"                , C_UTF_8       , C_UTF_8},
1315         {"te_IN"                , C_UTF_8       , C_UTF_8},
1316         {"ur_PK"                , C_UTF_8       , C_UTF_8},
1317
1318         {"th_TH"                , C_TIS_620     , C_TIS_620},
1319         /* {"th_TH"             , C_WINDOWS_874}, */
1320         /* {"th_TH"             , C_ISO_8859_11}, */
1321
1322         {"ka_GE"                , C_GEORGIAN_PS , C_GEORGIAN_PS},
1323         {"vi_VN.TCVN"           , C_TCVN5712_1  , C_TCVN5712_1},
1324
1325         {"C"                    , C_US_ASCII    , C_US_ASCII},
1326         {"POSIX"                , C_US_ASCII    , C_US_ASCII},
1327         {"ANSI_X3.4-1968"       , C_US_ASCII    , C_US_ASCII},
1328 };
1329
1330 static GHashTable *conv_get_charset_to_str_table(void)
1331 {
1332         static GHashTable *table;
1333         gint i;
1334
1335         if (table)
1336                 return table;
1337
1338         table = g_hash_table_new(NULL, g_direct_equal);
1339
1340         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
1341                 if (g_hash_table_lookup(table, GUINT_TO_POINTER(charsets[i].charset))
1342                     == NULL) {
1343                         g_hash_table_insert
1344                                 (table, GUINT_TO_POINTER(charsets[i].charset),
1345                                  charsets[i].name);
1346                 }
1347         }
1348
1349         return table;
1350 }
1351
1352 static GHashTable *conv_get_charset_from_str_table(void)
1353 {
1354         static GHashTable *table;
1355         gint i;
1356
1357         if (table)
1358                 return table;
1359
1360         table = g_hash_table_new(str_case_hash, str_case_equal);
1361
1362         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
1363                 g_hash_table_insert(table, charsets[i].name,
1364                                     GUINT_TO_POINTER(charsets[i].charset));
1365         }
1366
1367         return table;
1368 }
1369
1370 const gchar *conv_get_charset_str(CharSet charset)
1371 {
1372         GHashTable *table;
1373
1374         table = conv_get_charset_to_str_table();
1375         return g_hash_table_lookup(table, GUINT_TO_POINTER(charset));
1376 }
1377
1378 CharSet conv_get_charset_from_str(const gchar *charset)
1379 {
1380         GHashTable *table;
1381
1382         if (!charset) return C_AUTO;
1383
1384         table = conv_get_charset_from_str_table();
1385         return GPOINTER_TO_UINT(g_hash_table_lookup(table, charset));
1386 }
1387
1388 static CharSet conv_get_locale_charset(void)
1389 {
1390         static CharSet cur_charset = C_UNINITIALIZED;
1391         const gchar *cur_locale;
1392         const gchar *p;
1393         gint i;
1394
1395         if (cur_charset != C_UNINITIALIZED)
1396                 return cur_charset;
1397
1398         cur_locale = conv_get_current_locale();
1399         if (!cur_locale) {
1400                 cur_charset = C_US_ASCII;
1401                 return cur_charset;
1402         }
1403
1404         if (strcasestr(cur_locale, "UTF-8") ||
1405             strcasestr(cur_locale, "utf8")) {
1406                 cur_charset = C_UTF_8;
1407                 return cur_charset;
1408         }
1409
1410         if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
1411                 cur_charset = C_ISO_8859_15;
1412                 return cur_charset;
1413         }
1414
1415         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
1416                 const gchar *p;
1417
1418                 /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
1419                    "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
1420                 if (!g_ascii_strncasecmp(cur_locale, locale_table[i].locale,
1421                                  strlen(locale_table[i].locale))) {
1422                         cur_charset = locale_table[i].charset;
1423                         return cur_charset;
1424                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
1425                          !strchr(p + 1, '.')) {
1426                         if (strlen(cur_locale) == 2 &&
1427                             !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
1428                                 cur_charset = locale_table[i].charset;
1429                                 return cur_charset;
1430                         }
1431                 }
1432         }
1433
1434         cur_charset = C_AUTO;
1435         return cur_charset;
1436 }
1437
1438 static CharSet conv_get_locale_charset_no_utf8(void)
1439 {
1440         static CharSet cur_charset = C_UNINITIALIZED;
1441         const gchar *cur_locale;
1442         const gchar *p;
1443         gint i;
1444
1445         if (prefs_common.broken_are_utf8) {
1446                 cur_charset = C_UTF_8;
1447                 return cur_charset;
1448         }
1449
1450         cur_locale = conv_get_current_locale();
1451         if (!cur_locale) {
1452                 cur_charset = C_US_ASCII;
1453                 return cur_charset;
1454         }
1455
1456         if (strcasestr(cur_locale, "UTF-8") ||
1457             strcasestr(cur_locale, "utf8")) {
1458                 cur_charset = C_UTF_8;
1459                 return cur_charset;
1460         }
1461
1462         if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
1463                 cur_charset = C_ISO_8859_15;
1464                 return cur_charset;
1465         }
1466
1467         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
1468                 const gchar *p;
1469
1470                 /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
1471                    "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
1472                 if (!g_ascii_strncasecmp(cur_locale, locale_table[i].locale,
1473                                  strlen(locale_table[i].locale))) {
1474                         cur_charset = locale_table[i].charset;
1475                         return cur_charset;
1476                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
1477                          !strchr(p + 1, '.')) {
1478                         if (strlen(cur_locale) == 2 &&
1479                             !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
1480                                 cur_charset = locale_table[i].charset;
1481                                 return cur_charset;
1482                         }
1483                 }
1484         }
1485
1486         cur_charset = C_AUTO;
1487         return cur_charset;
1488 }
1489
1490 const gchar *conv_get_locale_charset_str(void)
1491 {
1492         static const gchar *codeset = NULL;
1493
1494         if (!codeset)
1495                 codeset = conv_get_charset_str(conv_get_locale_charset());
1496
1497         return codeset ? codeset : CS_INTERNAL;
1498 }
1499
1500 const gchar *conv_get_locale_charset_str_no_utf8(void)
1501 {
1502         static const gchar *codeset = NULL;
1503
1504         if (!codeset)
1505                 codeset = conv_get_charset_str(conv_get_locale_charset_no_utf8());
1506
1507         return codeset ? codeset : CS_INTERNAL;
1508 }
1509
1510 static CharSet conv_get_outgoing_charset(void)
1511 {
1512         static CharSet out_charset = C_UNINITIALIZED;
1513         const gchar *cur_locale;
1514         const gchar *p;
1515         gint i;
1516
1517         if (out_charset != C_UNINITIALIZED)
1518                 return out_charset;
1519
1520         cur_locale = conv_get_current_locale();
1521         if (!cur_locale) {
1522                 out_charset = C_AUTO;
1523                 return out_charset;
1524         }
1525
1526         if (strcasestr(cur_locale, "UTF-8") ||
1527             strcasestr(cur_locale, "utf8")) {
1528                 out_charset = C_UTF_8;
1529                 return out_charset;
1530         }
1531
1532         if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
1533                 out_charset = C_ISO_8859_15;
1534                 return out_charset;
1535         }
1536
1537         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
1538                 const gchar *p;
1539
1540                 if (!g_ascii_strncasecmp(cur_locale, locale_table[i].locale,
1541                                  strlen(locale_table[i].locale))) {
1542                         out_charset = locale_table[i].out_charset;
1543                         break;
1544                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
1545                          !strchr(p + 1, '.')) {
1546                         if (strlen(cur_locale) == 2 &&
1547                             !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
1548                                 out_charset = locale_table[i].out_charset;
1549                                 break;
1550                         }
1551                 }
1552         }
1553
1554         return out_charset;
1555 }
1556
1557 const gchar *conv_get_outgoing_charset_str(void)
1558 {
1559         CharSet out_charset;
1560         const gchar *str;
1561
1562         out_charset = conv_get_outgoing_charset();
1563         str = conv_get_charset_str(out_charset);
1564
1565         return str ? str : CS_UTF_8;
1566 }
1567
1568 const gchar *conv_get_current_locale(void)
1569 {
1570         const gchar *cur_locale;
1571
1572 #ifdef G_OS_WIN32
1573         cur_locale = g_win32_getlocale();
1574 #else
1575         cur_locale = g_getenv("LC_ALL");
1576         if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
1577         if (!cur_locale) cur_locale = g_getenv("LANG");
1578         if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
1579 #endif /* G_OS_WIN32 */
1580
1581         debug_print("current locale: %s\n",
1582                     cur_locale ? cur_locale : "(none)");
1583
1584         return cur_locale;
1585 }
1586
1587 static gboolean conv_is_ja_locale(void)
1588 {
1589         static gint is_ja_locale = -1;
1590         const gchar *cur_locale;
1591
1592         if (is_ja_locale != -1)
1593                 return is_ja_locale != 0;
1594
1595         is_ja_locale = 0;
1596         cur_locale = conv_get_current_locale();
1597         if (cur_locale) {
1598                 if (g_ascii_strncasecmp(cur_locale, "ja", 2) == 0)
1599                         is_ja_locale = 1;
1600         }
1601
1602         return is_ja_locale != 0;
1603 }
1604
1605 gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding,
1606                            gboolean addr_field)
1607 {
1608         gchar buf[BUFFSIZE];
1609
1610         cm_return_val_if_fail(str != NULL, NULL);
1611
1612         if (is_ascii_str(str))
1613                 return unmime_header(str, addr_field);
1614
1615         if (default_encoding) {
1616                 gchar *utf8_buf;
1617
1618                 utf8_buf = conv_codeset_strdup
1619                         (str, default_encoding, CS_INTERNAL);
1620                 if (utf8_buf) {
1621                         gchar *decoded_str;
1622
1623                         decoded_str = unmime_header(utf8_buf, addr_field);
1624                         g_free(utf8_buf);
1625                         return decoded_str;
1626                 }
1627         }
1628
1629         if (conv_is_ja_locale())
1630                 conv_anytodisp(buf, sizeof(buf), str);
1631         else
1632                 conv_localetodisp(buf, sizeof(buf), str);
1633
1634         return unmime_header(buf, addr_field);
1635 }
1636
1637 #define MAX_LINELEN             76
1638 #define MAX_HARD_LINELEN        996
1639 #define MIMESEP_BEGIN           "=?"
1640 #define MIMESEP_END             "?="
1641
1642 #define LBREAK_IF_REQUIRED(cond, is_plain_text)                         \
1643 {                                                                       \
1644         if (len - (destp - (guchar *)dest) < MAX_LINELEN + 2) {         \
1645                 *destp = '\0';                                          \
1646                 return;                                                 \
1647         }                                                               \
1648                                                                         \
1649         if ((cond) && *srcp) {                                          \
1650                 if (destp > (guchar *)dest && left < MAX_LINELEN - 1) { \
1651                         if (isspace(*(destp - 1)))                      \
1652                                 destp--;                                \
1653                         else if (is_plain_text && isspace(*srcp))       \
1654                                 srcp++;                                 \
1655                         if (*srcp) {                                    \
1656                                 *destp++ = '\n';                        \
1657                                 *destp++ = ' ';                         \
1658                                 left = MAX_LINELEN - 1;                 \
1659                         }                                               \
1660                 } else if (destp == (guchar *)dest && left < 7) {       \
1661                         if (is_plain_text && isspace(*srcp))            \
1662                                 srcp++;                                 \
1663                         if (*srcp) {                                    \
1664                                 *destp++ = '\n';                        \
1665                                 *destp++ = ' ';                         \
1666                                 left = MAX_LINELEN - 1;                 \
1667                         }                                               \
1668                 }                                                       \
1669         }                                                               \
1670 }
1671
1672 #define B64LEN(len) ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
1673
1674 void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
1675                         gint header_len, gboolean addr_field,
1676                         const gchar *out_encoding_)
1677 {
1678         const gchar *cur_encoding;
1679         const gchar *out_encoding;
1680         gint mimestr_len;
1681         gchar *mimesep_enc;
1682         gint left;
1683         const guchar *srcp = src;
1684         guchar *destp = dest;
1685         gboolean use_base64;
1686
1687         cm_return_if_fail(g_utf8_validate(src, -1, NULL) == TRUE);
1688         cm_return_if_fail(destp != NULL);
1689
1690         if (MB_CUR_MAX > 1) {
1691                 use_base64 = TRUE;
1692                 mimesep_enc = "?B?";
1693         } else {
1694                 use_base64 = FALSE;
1695                 mimesep_enc = "?Q?";
1696         }
1697
1698         cur_encoding = CS_INTERNAL;
1699
1700         if (out_encoding_)
1701                 out_encoding = out_encoding_;
1702         else
1703                 out_encoding = conv_get_outgoing_charset_str();
1704
1705         if (!strcmp(out_encoding, CS_US_ASCII))
1706                 out_encoding = CS_ISO_8859_1;
1707
1708         mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
1709                 strlen(mimesep_enc) + strlen(MIMESEP_END);
1710
1711         left = MAX_LINELEN - header_len;
1712
1713         while (*srcp) {
1714                 LBREAK_IF_REQUIRED(left <= 0, TRUE);
1715
1716                 while (isspace(*srcp)) {
1717                         *destp++ = *srcp++;
1718                         left--;
1719                         LBREAK_IF_REQUIRED(left <= 0, TRUE);
1720                 }
1721
1722                 /* output as it is if the next word is ASCII string */
1723                 if (!is_next_nonascii(srcp)) {
1724                         gint word_len;
1725
1726                         word_len = get_next_word_len(srcp);
1727                         LBREAK_IF_REQUIRED(left < word_len, TRUE);
1728                         while (word_len > 0) {
1729                                 LBREAK_IF_REQUIRED(left + (MAX_HARD_LINELEN - MAX_LINELEN) <= 0, TRUE)
1730                                 *destp++ = *srcp++;
1731                                 left--;
1732                                 word_len--;
1733                         }
1734
1735                         continue;
1736                 }
1737
1738                 /* don't include parentheses and quotes in encoded strings */
1739                 if (addr_field && (*srcp == '(' || *srcp == ')' || *srcp == '"')) {
1740                         LBREAK_IF_REQUIRED(left < 2, FALSE);
1741                         *destp++ = *srcp++;
1742                         left--;
1743                 }
1744
1745                 while (1) {
1746                         gint mb_len = 0;
1747                         gint cur_len = 0;
1748                         gchar *part_str;
1749                         gchar *out_str;
1750                         gchar *enc_str;
1751                         const guchar *p = srcp;
1752                         gint out_str_len;
1753                         gint out_enc_str_len;
1754                         gint mime_block_len;
1755                         gboolean cont = FALSE;
1756
1757                         while (*p != '\0') {
1758                                 if (isspace(*p) && !is_next_nonascii(p + 1))
1759                                         break;
1760                                 /* don't include parentheses in encoded
1761                                    strings */
1762                                 if (addr_field && (*p == '(' || *p == ')' || *p == '"'))
1763                                         break;
1764
1765                                 mb_len = g_utf8_skip[*p];
1766
1767                                 Xstrndup_a(part_str, srcp, cur_len + mb_len, );
1768                                 out_str = conv_codeset_strdup
1769                                         (part_str, cur_encoding, out_encoding);
1770                                 if (!out_str) {
1771                                         if (strict_mode) {
1772                                                 *dest = '\0';
1773                                                 return;
1774                                         } else {
1775                                                 g_warning("conv_encode_header_full(): code conversion failed");
1776                                                 conv_unreadable_8bit(part_str);
1777                                                 out_str = g_strdup(part_str);
1778                                         }
1779                                 }
1780                                 out_str_len = strlen(out_str);
1781
1782                                 if (use_base64)
1783                                         out_enc_str_len = B64LEN(out_str_len);
1784                                 else
1785                                         out_enc_str_len =
1786                                                 qp_get_q_encoding_len(out_str);
1787
1788                                 g_free(out_str);
1789
1790                                 if (mimestr_len + out_enc_str_len <= left) {
1791                                         cur_len += mb_len;
1792                                         p += mb_len;
1793                                 } else if (cur_len == 0) {
1794                                         left = 0;
1795                                         LBREAK_IF_REQUIRED(1, FALSE);
1796                                         continue;
1797                                 } else {
1798                                         cont = TRUE;
1799                                         break;
1800                                 }
1801                         }
1802
1803                         if (cur_len > 0) {
1804                                 Xstrndup_a(part_str, srcp, cur_len, );
1805                                 out_str = conv_codeset_strdup
1806                                         (part_str, cur_encoding, out_encoding);
1807                                 if (!out_str) {
1808                                         g_warning("conv_encode_header_full(): code conversion failed");
1809                                         conv_unreadable_8bit(part_str);
1810                                         out_str = g_strdup(part_str);
1811                                 }
1812                                 out_str_len = strlen(out_str);
1813
1814                                 if (use_base64)
1815                                         out_enc_str_len = B64LEN(out_str_len);
1816                                 else
1817                                         out_enc_str_len =
1818                                                 qp_get_q_encoding_len(out_str);
1819
1820                                 if (use_base64)
1821                                         enc_str = g_base64_encode(out_str, out_str_len);
1822                                 else {
1823                                         Xalloca(enc_str, out_enc_str_len + 1, );
1824                                         qp_q_encode(enc_str, out_str);
1825                                 }
1826
1827                                 g_free(out_str);
1828
1829                                 /* output MIME-encoded string block */
1830                                 mime_block_len = mimestr_len + strlen(enc_str);
1831                                 g_snprintf(destp, mime_block_len + 1,
1832                                            MIMESEP_BEGIN "%s%s%s" MIMESEP_END,
1833                                            out_encoding, mimesep_enc, enc_str);
1834
1835                                 if (use_base64)
1836                                         g_free(enc_str);
1837
1838                                 destp += mime_block_len;
1839                                 srcp += cur_len;
1840
1841                                 left -= mime_block_len;
1842                         }
1843
1844                         LBREAK_IF_REQUIRED(cont, FALSE);
1845
1846                         if (cur_len == 0)
1847                                 break;
1848                 }
1849         }
1850
1851         *destp = '\0';
1852 }
1853
1854 void conv_encode_header(gchar *dest, gint len, const gchar *src,
1855                         gint header_len, gboolean addr_field)
1856 {
1857         conv_encode_header_full(dest,len,src,header_len,addr_field,NULL);
1858 }
1859
1860 #undef LBREAK_IF_REQUIRED
1861 #undef B64LEN
1862
1863 gchar *conv_filename_from_utf8(const gchar *utf8_file)
1864 {
1865         gchar *fs_file;
1866         GError *error = NULL;
1867
1868         cm_return_val_if_fail(utf8_file != NULL, NULL);
1869
1870         fs_file = g_filename_from_utf8(utf8_file, -1, NULL, NULL, &error);
1871         if (error) {
1872                 debug_print("failed to convert encoding of file name: %s\n",
1873                           error->message);
1874                 g_error_free(error);
1875         }
1876         if (!fs_file)
1877                 fs_file = g_strdup(utf8_file);
1878
1879         return fs_file;
1880 }
1881
1882 gchar *conv_filename_to_utf8(const gchar *fs_file)
1883 {
1884         gchar *utf8_file = NULL;
1885         GError *error = NULL;
1886
1887         cm_return_val_if_fail(fs_file != NULL, NULL);
1888
1889         utf8_file = g_filename_to_utf8(fs_file, -1, NULL, NULL, &error);
1890         if (error) {
1891                 g_warning("failed to convert encoding of file name: %s",
1892                           error->message);
1893                 g_error_free(error);
1894         }
1895
1896         if (!utf8_file || !g_utf8_validate(utf8_file, -1, NULL)) {
1897                 g_free(utf8_file);
1898                 utf8_file = g_strdup(fs_file);
1899                 conv_unreadable_8bit(utf8_file);
1900         }
1901
1902         return utf8_file;
1903 }