Statistics
| Revision:

root / src / codeconv.c @ 180

History | View | Annotate | Download (44 kB)

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