2 * iconv library implemented with Win32 API.
\r
4 * This file is placed in the public domain.
\r
6 * Maintainer: Yukihiro Nakadaira <yukihiro.nakadaira@gmail.com>
\r
8 * If $WINICONV_LIBICONV_DLL environment variable was defined, win_iconv
\r
9 * loads the specified DLL dynamically and uses it. If loading the DLL
\r
10 * or iconv_open() failed, falls back to internal conversion.
\r
11 * $WINICONV_LIBICONV_DLL is a comma separated list. The first loadable
\r
12 * DLL is used. The specified DLL should have iconv_open(),
\r
13 * iconv_close() and iconv() functions. Or these functions can be
\r
14 * libiconv_open(), libiconv_close() and libiconv().
\r
16 * Win32 API does not support strict encoding conversion for some
\r
17 * codepage. And MLang function drop or replace invalid bytes and does
\r
18 * not return useful error status as iconv. This implementation cannot
\r
19 * be used for encoding validation purpose.
\r
22 /* for WC_NO_BEST_FIT_CHARS */
\r
24 # define WINVER 0x0500
\r
28 #include <windows.h>
\r
36 # define USE_LIBICONV_DLL
\r
39 #if !defined(DEFAULT_LIBICONV_DLL)
\r
40 # define DEFAULT_LIBICONV_DLL ""
\r
43 #define MB_CHAR_MAX 16
\r
45 #define UNICODE_MODE_BOM_DONE 1
\r
46 #define UNICODE_MODE_SWAPPED 2
\r
48 #define FLAG_USE_BOM_ENDIAN 1
\r
49 #define FLAG_TRANSLIT 2 /* //TRANSLIT */
\r
50 #define FLAG_IGNORE 4 /* //IGNORE (not implemented) */
\r
52 #define return_error(code) \
\r
58 #define xstrlcpy(dst, src, size) \
\r
60 strncpy(dst, src, size); \
\r
61 dst[size - 1] = 0; \
\r
64 #define xstrlcpyn(dst, src, srclen, size) \
\r
65 xstrlcpy(dst, src, xmin((srclen) + 1, size))
\r
67 #define xmin(a, b) ((a) < (b) ? (a) : (b))
\r
68 #define xmax(a, b) ((a) > (b) ? (a) : (b))
\r
70 #define STATIC_STRLEN(arr) (sizeof(arr) - 1)
\r
72 typedef unsigned char uchar;
\r
73 typedef unsigned short ushort;
\r
74 typedef unsigned int uint;
\r
76 typedef void* iconv_t;
\r
78 iconv_t iconv_open(const char *tocode, const char *fromcode);
\r
79 int iconv_close(iconv_t cd);
\r
80 size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
\r
82 /* libiconv interface for vim */
\r
83 #if defined(MAKE_DLL)
\r
85 iconvctl (iconv_t cd, int request, void* argument)
\r
92 typedef struct compat_t compat_t;
\r
93 typedef struct csconv_t csconv_t;
\r
94 typedef struct rec_iconv_t rec_iconv_t;
\r
96 typedef iconv_t (*f_iconv_open)(const char *tocode, const char *fromcode);
\r
97 typedef int (*f_iconv_close)(iconv_t cd);
\r
98 typedef size_t (*f_iconv)(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
\r
99 typedef int* (*f_errno)(void);
\r
100 typedef int (*f_mbtowc)(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
\r
101 typedef int (*f_wctomb)(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
\r
102 typedef int (*f_mblen)(csconv_t *cv, const uchar *buf, int bufsize);
\r
103 typedef int (*f_flush)(csconv_t *cv, uchar *buf, int bufsize);
\r
105 #define COMPAT_IN 1
\r
106 #define COMPAT_OUT 2
\r
108 /* unicode mapping for compatibility with other conversion table. */
\r
126 struct rec_iconv_t {
\r
128 f_iconv_close iconv_close;
\r
133 #if defined(USE_LIBICONV_DLL)
\r
138 static int win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
\r
139 static int win_iconv_close(iconv_t cd);
\r
140 static size_t win_iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
\r
142 static int load_mlang();
\r
143 static csconv_t make_csconv(const char *name);
\r
144 static int name_to_codepage(const char *name);
\r
145 static uint utf16_to_ucs4(const ushort *wbuf);
\r
146 static void ucs4_to_utf16(uint wc, ushort *wbuf, int *wbufsize);
\r
147 static int is_unicode(int codepage);
\r
148 static int mbtowc_flags(int codepage);
\r
149 static int must_use_null_useddefaultchar(int codepage);
\r
150 static void check_utf_bom(rec_iconv_t *cd, ushort *wbuf, int *wbufsize);
\r
151 static char *strrstr(const char *str, const char *token);
\r
153 #if defined(USE_LIBICONV_DLL)
\r
154 static int libiconv_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
\r
155 static PVOID MyImageDirectoryEntryToData(LPVOID Base, BOOLEAN MappedAsImage, USHORT DirectoryEntry, PULONG Size);
\r
156 static HMODULE find_imported_module_by_funcname(HMODULE hModule, const char *funcname);
\r
158 static HMODULE hwiniconv;
\r
159 static HMODULE hlastdll; /* keep dll loaded for efficiency (unnecessary?) */
\r
162 static int sbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
\r
163 static int dbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
\r
164 static int mbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
\r
165 static int utf8_mblen(csconv_t *cv, const uchar *buf, int bufsize);
\r
166 static int eucjp_mblen(csconv_t *cv, const uchar *buf, int bufsize);
\r
168 static int kernel_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
\r
169 static int kernel_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
\r
170 static int mlang_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
\r
171 static int mlang_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
\r
172 static int utf16_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
\r
173 static int utf16_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
\r
174 static int utf32_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
\r
175 static int utf32_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
\r
176 static int iso2022jp_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
\r
177 static int iso2022jp_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
\r
178 static int iso2022jp_flush(csconv_t *cv, uchar *buf, int bufsize);
\r
183 } codepage_alias[] = {
\r
184 {65001, "CP65001"},
\r
190 {1200, "UTF-16LE"},
\r
196 {1201, "UTF-16BE"},
\r
199 {1201, "unicodeFFFE"},
\r
201 {12000, "CP12000"},
\r
202 {12000, "UTF32LE"},
\r
203 {12000, "UTF-32LE"},
\r
205 {12000, "UCS-4LE"},
\r
207 {12001, "CP12001"},
\r
208 {12001, "UTF32BE"},
\r
209 {12001, "UTF-32BE"},
\r
211 {12001, "UCS-4BE"},
\r
213 #ifndef GLIB_COMPILATION
\r
215 * Default is big endian.
\r
216 * See rfc2781 4.3 Interpreting text labelled as UTF-16.
\r
225 /* Default is little endian, because the platform is */
\r
236 /* copy from libiconv `iconv -l` */
\r
237 /* !IsValidCodePage(367) */
\r
238 {20127, "ANSI_X3.4-1968"},
\r
239 {20127, "ANSI_X3.4-1986"},
\r
243 {20127, "ISO-IR-6"},
\r
244 {20127, "ISO646-US"},
\r
245 {20127, "ISO_646.IRV:1991"},
\r
247 {20127, "US-ASCII"},
\r
248 {20127, "CSASCII"},
\r
250 /* !IsValidCodePage(819) */
\r
253 {28591, "ISO-8859-1"},
\r
254 {28591, "ISO-IR-100"},
\r
255 {28591, "ISO8859-1"},
\r
256 {28591, "ISO_8859-1"},
\r
257 {28591, "ISO_8859-1:1987"},
\r
260 {28591, "CSISOLATIN1"},
\r
264 {1250, "WINDOWS-1250"},
\r
268 {1251, "WINDOWS-1251"},
\r
272 {1252, "WINDOWS-1252"},
\r
275 {1253, "MS-GREEK"},
\r
276 {1253, "WINDOWS-1253"},
\r
280 {1254, "WINDOWS-1254"},
\r
284 {1255, "WINDOWS-1255"},
\r
288 {1256, "WINDOWS-1256"},
\r
291 {1257, "WINBALTRIM"},
\r
292 {1257, "WINDOWS-1257"},
\r
295 {1258, "WINDOWS-1258"},
\r
300 {850, "CSPC850MULTILINGUAL"},
\r
302 /* !IsValidCodePage(862) */
\r
306 {862, "CSPC862LATINHEBREW"},
\r
313 /* !IsValidCodePage(154) */
\r
315 {154, "CYRILLIC-ASIAN"},
\r
318 {154, "CSPTCP154"},
\r
320 /* !IsValidCodePage(1133) */
\r
322 {1133, "IBM-CP1133"},
\r
325 {874, "WINDOWS-874"},
\r
327 /* !IsValidCodePage(51932) */
\r
328 {51932, "CP51932"},
\r
329 {51932, "MS51932"},
\r
330 {51932, "WINDOWS-51932"},
\r
335 {932, "SHIFFT_JIS"},
\r
336 {932, "SHIFFT_JIS-MS"},
\r
339 {932, "SJIS-OPEN"},
\r
341 {932, "WINDOWS-31J"},
\r
342 {932, "WINDOWS-932"},
\r
343 {932, "CSWINDOWS31J"},
\r
345 {50221, "CP50221"},
\r
346 {50221, "ISO-2022-JP"},
\r
347 {50221, "ISO-2022-JP-MS"},
\r
348 {50221, "ISO2022-JP"},
\r
349 {50221, "ISO2022-JP-MS"},
\r
350 {50221, "MS50221"},
\r
351 {50221, "WINDOWS-50221"},
\r
356 {936, "WINDOWS-936"},
\r
371 {437, "CSPC8CODEPAGE437"},
\r
377 {775, "CSPC775BALTIC"},
\r
384 /* !IsValidCodePage(853) */
\r
397 /* !IsValidCodePage(858) */
\r
431 /* !IsValidCodePage(1152) */
\r
435 * Code Page Identifiers
\r
436 * http://msdn2.microsoft.com/en-us/library/ms776446.aspx
\r
438 {37, "IBM037"}, /* IBM EBCDIC US-Canada */
\r
439 {437, "IBM437"}, /* OEM United States */
\r
440 {500, "IBM500"}, /* IBM EBCDIC International */
\r
441 {708, "ASMO-708"}, /* Arabic (ASMO 708) */
\r
442 /* 709 Arabic (ASMO-449+, BCON V4) */
\r
443 /* 710 Arabic - Transparent Arabic */
\r
444 {720, "DOS-720"}, /* Arabic (Transparent ASMO); Arabic (DOS) */
\r
445 {737, "ibm737"}, /* OEM Greek (formerly 437G); Greek (DOS) */
\r
446 {775, "ibm775"}, /* OEM Baltic; Baltic (DOS) */
\r
447 {850, "ibm850"}, /* OEM Multilingual Latin 1; Western European (DOS) */
\r
448 {852, "ibm852"}, /* OEM Latin 2; Central European (DOS) */
\r
449 {855, "IBM855"}, /* OEM Cyrillic (primarily Russian) */
\r
450 {857, "ibm857"}, /* OEM Turkish; Turkish (DOS) */
\r
451 {858, "IBM00858"}, /* OEM Multilingual Latin 1 + Euro symbol */
\r
452 {860, "IBM860"}, /* OEM Portuguese; Portuguese (DOS) */
\r
453 {861, "ibm861"}, /* OEM Icelandic; Icelandic (DOS) */
\r
454 {862, "DOS-862"}, /* OEM Hebrew; Hebrew (DOS) */
\r
455 {863, "IBM863"}, /* OEM French Canadian; French Canadian (DOS) */
\r
456 {864, "IBM864"}, /* OEM Arabic; Arabic (864) */
\r
457 {865, "IBM865"}, /* OEM Nordic; Nordic (DOS) */
\r
458 {866, "cp866"}, /* OEM Russian; Cyrillic (DOS) */
\r
459 {869, "ibm869"}, /* OEM Modern Greek; Greek, Modern (DOS) */
\r
460 {870, "IBM870"}, /* IBM EBCDIC Multilingual/ROECE (Latin 2); IBM EBCDIC Multilingual Latin 2 */
\r
461 {874, "windows-874"}, /* ANSI/OEM Thai (same as 28605, ISO 8859-15); Thai (Windows) */
\r
462 {875, "cp875"}, /* IBM EBCDIC Greek Modern */
\r
463 {932, "shift_jis"}, /* ANSI/OEM Japanese; Japanese (Shift-JIS) */
\r
464 {932, "shift-jis"}, /* alternative name for it */
\r
465 {936, "gb2312"}, /* ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312) */
\r
466 {949, "ks_c_5601-1987"}, /* ANSI/OEM Korean (Unified Hangul Code) */
\r
467 {950, "big5"}, /* ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5) */
\r
468 {1026, "IBM1026"}, /* IBM EBCDIC Turkish (Latin 5) */
\r
469 {1047, "IBM01047"}, /* IBM EBCDIC Latin 1/Open System */
\r
470 {1140, "IBM01140"}, /* IBM EBCDIC US-Canada (037 + Euro symbol); IBM EBCDIC (US-Canada-Euro) */
\r
471 {1141, "IBM01141"}, /* IBM EBCDIC Germany (20273 + Euro symbol); IBM EBCDIC (Germany-Euro) */
\r
472 {1142, "IBM01142"}, /* IBM EBCDIC Denmark-Norway (20277 + Euro symbol); IBM EBCDIC (Denmark-Norway-Euro) */
\r
473 {1143, "IBM01143"}, /* IBM EBCDIC Finland-Sweden (20278 + Euro symbol); IBM EBCDIC (Finland-Sweden-Euro) */
\r
474 {1144, "IBM01144"}, /* IBM EBCDIC Italy (20280 + Euro symbol); IBM EBCDIC (Italy-Euro) */
\r
475 {1145, "IBM01145"}, /* IBM EBCDIC Latin America-Spain (20284 + Euro symbol); IBM EBCDIC (Spain-Euro) */
\r
476 {1146, "IBM01146"}, /* IBM EBCDIC United Kingdom (20285 + Euro symbol); IBM EBCDIC (UK-Euro) */
\r
477 {1147, "IBM01147"}, /* IBM EBCDIC France (20297 + Euro symbol); IBM EBCDIC (France-Euro) */
\r
478 {1148, "IBM01148"}, /* IBM EBCDIC International (500 + Euro symbol); IBM EBCDIC (International-Euro) */
\r
479 {1149, "IBM01149"}, /* IBM EBCDIC Icelandic (20871 + Euro symbol); IBM EBCDIC (Icelandic-Euro) */
\r
480 {1250, "windows-1250"}, /* ANSI Central European; Central European (Windows) */
\r
481 {1251, "windows-1251"}, /* ANSI Cyrillic; Cyrillic (Windows) */
\r
482 {1252, "windows-1252"}, /* ANSI Latin 1; Western European (Windows) */
\r
483 {1253, "windows-1253"}, /* ANSI Greek; Greek (Windows) */
\r
484 {1254, "windows-1254"}, /* ANSI Turkish; Turkish (Windows) */
\r
485 {1255, "windows-1255"}, /* ANSI Hebrew; Hebrew (Windows) */
\r
486 {1256, "windows-1256"}, /* ANSI Arabic; Arabic (Windows) */
\r
487 {1257, "windows-1257"}, /* ANSI Baltic; Baltic (Windows) */
\r
488 {1258, "windows-1258"}, /* ANSI/OEM Vietnamese; Vietnamese (Windows) */
\r
489 {1361, "Johab"}, /* Korean (Johab) */
\r
490 {10000, "macintosh"}, /* MAC Roman; Western European (Mac) */
\r
491 {10001, "x-mac-japanese"}, /* Japanese (Mac) */
\r
492 {10002, "x-mac-chinesetrad"}, /* MAC Traditional Chinese (Big5); Chinese Traditional (Mac) */
\r
493 {10003, "x-mac-korean"}, /* Korean (Mac) */
\r
494 {10004, "x-mac-arabic"}, /* Arabic (Mac) */
\r
495 {10005, "x-mac-hebrew"}, /* Hebrew (Mac) */
\r
496 {10006, "x-mac-greek"}, /* Greek (Mac) */
\r
497 {10007, "x-mac-cyrillic"}, /* Cyrillic (Mac) */
\r
498 {10008, "x-mac-chinesesimp"}, /* MAC Simplified Chinese (GB 2312); Chinese Simplified (Mac) */
\r
499 {10010, "x-mac-romanian"}, /* Romanian (Mac) */
\r
500 {10017, "x-mac-ukrainian"}, /* Ukrainian (Mac) */
\r
501 {10021, "x-mac-thai"}, /* Thai (Mac) */
\r
502 {10029, "x-mac-ce"}, /* MAC Latin 2; Central European (Mac) */
\r
503 {10079, "x-mac-icelandic"}, /* Icelandic (Mac) */
\r
504 {10081, "x-mac-turkish"}, /* Turkish (Mac) */
\r
505 {10082, "x-mac-croatian"}, /* Croatian (Mac) */
\r
506 {20000, "x-Chinese_CNS"}, /* CNS Taiwan; Chinese Traditional (CNS) */
\r
507 {20001, "x-cp20001"}, /* TCA Taiwan */
\r
508 {20002, "x_Chinese-Eten"}, /* Eten Taiwan; Chinese Traditional (Eten) */
\r
509 {20003, "x-cp20003"}, /* IBM5550 Taiwan */
\r
510 {20004, "x-cp20004"}, /* TeleText Taiwan */
\r
511 {20005, "x-cp20005"}, /* Wang Taiwan */
\r
512 {20105, "x-IA5"}, /* IA5 (IRV International Alphabet No. 5, 7-bit); Western European (IA5) */
\r
513 {20106, "x-IA5-German"}, /* IA5 German (7-bit) */
\r
514 {20107, "x-IA5-Swedish"}, /* IA5 Swedish (7-bit) */
\r
515 {20108, "x-IA5-Norwegian"}, /* IA5 Norwegian (7-bit) */
\r
516 {20127, "us-ascii"}, /* US-ASCII (7-bit) */
\r
517 {20261, "x-cp20261"}, /* T.61 */
\r
518 {20269, "x-cp20269"}, /* ISO 6937 Non-Spacing Accent */
\r
519 {20273, "IBM273"}, /* IBM EBCDIC Germany */
\r
520 {20277, "IBM277"}, /* IBM EBCDIC Denmark-Norway */
\r
521 {20278, "IBM278"}, /* IBM EBCDIC Finland-Sweden */
\r
522 {20280, "IBM280"}, /* IBM EBCDIC Italy */
\r
523 {20284, "IBM284"}, /* IBM EBCDIC Latin America-Spain */
\r
524 {20285, "IBM285"}, /* IBM EBCDIC United Kingdom */
\r
525 {20290, "IBM290"}, /* IBM EBCDIC Japanese Katakana Extended */
\r
526 {20297, "IBM297"}, /* IBM EBCDIC France */
\r
527 {20420, "IBM420"}, /* IBM EBCDIC Arabic */
\r
528 {20423, "IBM423"}, /* IBM EBCDIC Greek */
\r
529 {20424, "IBM424"}, /* IBM EBCDIC Hebrew */
\r
530 {20833, "x-EBCDIC-KoreanExtended"}, /* IBM EBCDIC Korean Extended */
\r
531 {20838, "IBM-Thai"}, /* IBM EBCDIC Thai */
\r
532 {20866, "koi8-r"}, /* Russian (KOI8-R); Cyrillic (KOI8-R) */
\r
533 {20871, "IBM871"}, /* IBM EBCDIC Icelandic */
\r
534 {20880, "IBM880"}, /* IBM EBCDIC Cyrillic Russian */
\r
535 {20905, "IBM905"}, /* IBM EBCDIC Turkish */
\r
536 {20924, "IBM00924"}, /* IBM EBCDIC Latin 1/Open System (1047 + Euro symbol) */
\r
537 {20932, "EUC-JP"}, /* Japanese (JIS 0208-1990 and 0121-1990) */
\r
538 {20936, "x-cp20936"}, /* Simplified Chinese (GB2312); Chinese Simplified (GB2312-80) */
\r
539 {20949, "x-cp20949"}, /* Korean Wansung */
\r
540 {21025, "cp1025"}, /* IBM EBCDIC Cyrillic Serbian-Bulgarian */
\r
541 /* 21027 (deprecated) */
\r
542 {21866, "koi8-u"}, /* Ukrainian (KOI8-U); Cyrillic (KOI8-U) */
\r
543 {28591, "iso-8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
\r
544 {28591, "iso8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
\r
545 {28592, "iso-8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
\r
546 {28592, "iso8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
\r
547 {28593, "iso-8859-3"}, /* ISO 8859-3 Latin 3 */
\r
548 {28593, "iso8859-3"}, /* ISO 8859-3 Latin 3 */
\r
549 {28594, "iso-8859-4"}, /* ISO 8859-4 Baltic */
\r
550 {28594, "iso8859-4"}, /* ISO 8859-4 Baltic */
\r
551 {28595, "iso-8859-5"}, /* ISO 8859-5 Cyrillic */
\r
552 {28595, "iso8859-5"}, /* ISO 8859-5 Cyrillic */
\r
553 {28596, "iso-8859-6"}, /* ISO 8859-6 Arabic */
\r
554 {28596, "iso8859-6"}, /* ISO 8859-6 Arabic */
\r
555 {28597, "iso-8859-7"}, /* ISO 8859-7 Greek */
\r
556 {28597, "iso8859-7"}, /* ISO 8859-7 Greek */
\r
557 {28598, "iso-8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
\r
558 {28598, "iso8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
\r
559 {28599, "iso-8859-9"}, /* ISO 8859-9 Turkish */
\r
560 {28599, "iso8859-9"}, /* ISO 8859-9 Turkish */
\r
561 {28603, "iso-8859-13"}, /* ISO 8859-13 Estonian */
\r
562 {28603, "iso8859-13"}, /* ISO 8859-13 Estonian */
\r
563 {28605, "iso-8859-15"}, /* ISO 8859-15 Latin 9 */
\r
564 {28605, "iso8859-15"}, /* ISO 8859-15 Latin 9 */
\r
565 {29001, "x-Europa"}, /* Europa 3 */
\r
566 {38598, "iso-8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
\r
567 {38598, "iso8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
\r
568 {50220, "iso-2022-jp"}, /* ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS) */
\r
569 {50221, "csISO2022JP"}, /* ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow 1 byte Kana) */
\r
570 {50222, "iso-2022-jp"}, /* ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte Kana - SO/SI) */
\r
571 {50225, "iso-2022-kr"}, /* ISO 2022 Korean */
\r
572 {50225, "iso2022-kr"}, /* ISO 2022 Korean */
\r
573 {50227, "x-cp50227"}, /* ISO 2022 Simplified Chinese; Chinese Simplified (ISO 2022) */
\r
574 /* 50229 ISO 2022 Traditional Chinese */
\r
575 /* 50930 EBCDIC Japanese (Katakana) Extended */
\r
576 /* 50931 EBCDIC US-Canada and Japanese */
\r
577 /* 50933 EBCDIC Korean Extended and Korean */
\r
578 /* 50935 EBCDIC Simplified Chinese Extended and Simplified Chinese */
\r
579 /* 50936 EBCDIC Simplified Chinese */
\r
580 /* 50937 EBCDIC US-Canada and Traditional Chinese */
\r
581 /* 50939 EBCDIC Japanese (Latin) Extended and Japanese */
\r
582 {51932, "euc-jp"}, /* EUC Japanese */
\r
583 {51936, "EUC-CN"}, /* EUC Simplified Chinese; Chinese Simplified (EUC) */
\r
584 {51949, "euc-kr"}, /* EUC Korean */
\r
585 /* 51950 EUC Traditional Chinese */
\r
586 {52936, "hz-gb-2312"}, /* HZ-GB2312 Simplified Chinese; Chinese Simplified (HZ) */
\r
587 {54936, "GB18030"}, /* Windows XP and later: GB18030 Simplified Chinese (4 byte); Chinese Simplified (GB18030) */
\r
588 {57002, "x-iscii-de"}, /* ISCII Devanagari */
\r
589 {57003, "x-iscii-be"}, /* ISCII Bengali */
\r
590 {57004, "x-iscii-ta"}, /* ISCII Tamil */
\r
591 {57005, "x-iscii-te"}, /* ISCII Telugu */
\r
592 {57006, "x-iscii-as"}, /* ISCII Assamese */
\r
593 {57007, "x-iscii-or"}, /* ISCII Oriya */
\r
594 {57008, "x-iscii-ka"}, /* ISCII Kannada */
\r
595 {57009, "x-iscii-ma"}, /* ISCII Malayalam */
\r
596 {57010, "x-iscii-gu"}, /* ISCII Gujarati */
\r
597 {57011, "x-iscii-pa"}, /* ISCII Punjabi */
\r
603 * SJIS SHIFTJIS table CP932 table
\r
604 * ---- --------------------------- --------------------------------
\r
605 * 5C U+00A5 YEN SIGN U+005C REVERSE SOLIDUS
\r
606 * 7E U+203E OVERLINE U+007E TILDE
\r
607 * 815C U+2014 EM DASH U+2015 HORIZONTAL BAR
\r
608 * 815F U+005C REVERSE SOLIDUS U+FF3C FULLWIDTH REVERSE SOLIDUS
\r
609 * 8160 U+301C WAVE DASH U+FF5E FULLWIDTH TILDE
\r
610 * 8161 U+2016 DOUBLE VERTICAL LINE U+2225 PARALLEL TO
\r
611 * 817C U+2212 MINUS SIGN U+FF0D FULLWIDTH HYPHEN-MINUS
\r
612 * 8191 U+00A2 CENT SIGN U+FFE0 FULLWIDTH CENT SIGN
\r
613 * 8192 U+00A3 POUND SIGN U+FFE1 FULLWIDTH POUND SIGN
\r
614 * 81CA U+00AC NOT SIGN U+FFE2 FULLWIDTH NOT SIGN
\r
616 * EUC-JP and ISO-2022-JP should be compatible with CP932.
\r
618 * Kernel and MLang have different Unicode mapping table. Make sure
\r
619 * which API is used.
\r
621 static compat_t cp932_compat[] = {
\r
622 {0x00A5, 0x005C, COMPAT_OUT},
\r
623 {0x203E, 0x007E, COMPAT_OUT},
\r
624 {0x2014, 0x2015, COMPAT_OUT},
\r
625 {0x301C, 0xFF5E, COMPAT_OUT},
\r
626 {0x2016, 0x2225, COMPAT_OUT},
\r
627 {0x2212, 0xFF0D, COMPAT_OUT},
\r
628 {0x00A2, 0xFFE0, COMPAT_OUT},
\r
629 {0x00A3, 0xFFE1, COMPAT_OUT},
\r
630 {0x00AC, 0xFFE2, COMPAT_OUT},
\r
634 static compat_t cp20932_compat[] = {
\r
635 {0x00A5, 0x005C, COMPAT_OUT},
\r
636 {0x203E, 0x007E, COMPAT_OUT},
\r
637 {0x2014, 0x2015, COMPAT_OUT},
\r
638 {0xFF5E, 0x301C, COMPAT_OUT|COMPAT_IN},
\r
639 {0x2225, 0x2016, COMPAT_OUT|COMPAT_IN},
\r
640 {0xFF0D, 0x2212, COMPAT_OUT|COMPAT_IN},
\r
641 {0xFFE0, 0x00A2, COMPAT_OUT|COMPAT_IN},
\r
642 {0xFFE1, 0x00A3, COMPAT_OUT|COMPAT_IN},
\r
643 {0xFFE2, 0x00AC, COMPAT_OUT|COMPAT_IN},
\r
647 static compat_t *cp51932_compat = cp932_compat;
\r
649 /* cp20932_compat for kernel. cp932_compat for mlang. */
\r
650 static compat_t *cp5022x_compat = cp932_compat;
\r
652 typedef HRESULT (WINAPI *CONVERTINETSTRING)(
\r
654 DWORD dwSrcEncoding,
\r
655 DWORD dwDstEncoding,
\r
661 typedef HRESULT (WINAPI *CONVERTINETMULTIBYTETOUNICODE)(
\r
663 DWORD dwSrcEncoding,
\r
665 LPINT lpnMultiCharCount,
\r
667 LPINT lpnWideCharCount
\r
669 typedef HRESULT (WINAPI *CONVERTINETUNICODETOMULTIBYTE)(
\r
673 LPINT lpnWideCharCount,
\r
675 LPINT lpnMultiCharCount
\r
677 typedef HRESULT (WINAPI *ISCONVERTINETSTRINGAVAILABLE)(
\r
678 DWORD dwSrcEncoding,
\r
679 DWORD dwDstEncoding
\r
681 typedef HRESULT (WINAPI *LCIDTORFC1766A)(
\r
686 typedef HRESULT (WINAPI *LCIDTORFC1766W)(
\r
691 typedef HRESULT (WINAPI *RFC1766TOLCIDA)(
\r
695 typedef HRESULT (WINAPI *RFC1766TOLCIDW)(
\r
699 static CONVERTINETSTRING ConvertINetString;
\r
700 static CONVERTINETMULTIBYTETOUNICODE ConvertINetMultiByteToUnicode;
\r
701 static CONVERTINETUNICODETOMULTIBYTE ConvertINetUnicodeToMultiByte;
\r
702 static ISCONVERTINETSTRINGAVAILABLE IsConvertINetStringAvailable;
\r
703 static LCIDTORFC1766A LcidToRfc1766A;
\r
704 static RFC1766TOLCIDA Rfc1766ToLcidA;
\r
710 if (ConvertINetString != NULL)
\r
712 h = LoadLibrary("mlang.dll");
\r
715 ConvertINetString = (CONVERTINETSTRING)GetProcAddress(h, "ConvertINetString");
\r
716 ConvertINetMultiByteToUnicode = (CONVERTINETMULTIBYTETOUNICODE)GetProcAddress(h, "ConvertINetMultiByteToUnicode");
\r
717 ConvertINetUnicodeToMultiByte = (CONVERTINETUNICODETOMULTIBYTE)GetProcAddress(h, "ConvertINetUnicodeToMultiByte");
\r
718 IsConvertINetStringAvailable = (ISCONVERTINETSTRINGAVAILABLE)GetProcAddress(h, "IsConvertINetStringAvailable");
\r
719 LcidToRfc1766A = (LCIDTORFC1766A)GetProcAddress(h, "LcidToRfc1766A");
\r
720 Rfc1766ToLcidA = (RFC1766TOLCIDA)GetProcAddress(h, "Rfc1766ToLcidA");
\r
725 iconv_open(const char *tocode, const char *fromcode)
\r
729 cd = (rec_iconv_t *)calloc(1, sizeof(rec_iconv_t));
\r
733 return (iconv_t)(-1);
\r
736 #if defined(USE_LIBICONV_DLL)
\r
737 if (libiconv_iconv_open(cd, tocode, fromcode))
\r
738 return (iconv_t)cd;
\r
741 if (win_iconv_open(cd, tocode, fromcode))
\r
742 return (iconv_t)cd;
\r
746 return (iconv_t)(-1);
\r
750 iconv_close(iconv_t _cd)
\r
752 rec_iconv_t *cd = (rec_iconv_t *)_cd;
\r
753 int r = cd->iconv_close(cd->cd);
\r
754 int e = *(cd->_errno());
\r
755 #if defined(USE_LIBICONV_DLL)
\r
756 if (cd->hlibiconv != NULL)
\r
757 FreeLibrary(cd->hlibiconv);
\r
765 iconv(iconv_t _cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
\r
767 rec_iconv_t *cd = (rec_iconv_t *)_cd;
\r
768 size_t r = cd->iconv(cd->cd, inbuf, inbytesleft, outbuf, outbytesleft);
\r
769 errno = *(cd->_errno());
\r
774 win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode)
\r
776 cd->from = make_csconv(fromcode);
\r
777 cd->to = make_csconv(tocode);
\r
778 if (cd->from.codepage == -1 || cd->to.codepage == -1)
\r
780 cd->iconv_close = win_iconv_close;
\r
781 cd->iconv = win_iconv;
\r
782 cd->_errno = _errno;
\r
783 cd->cd = (iconv_t)cd;
\r
788 win_iconv_close(iconv_t cd)
\r
794 win_iconv(iconv_t _cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
\r
796 rec_iconv_t *cd = (rec_iconv_t *)_cd;
\r
797 ushort wbuf[MB_CHAR_MAX]; /* enough room for one character */
\r
806 if (inbuf == NULL || *inbuf == NULL)
\r
808 if (outbuf != NULL && *outbuf != NULL && cd->to.flush != NULL)
\r
810 outsize = cd->to.flush(&cd->to, (uchar *)*outbuf, *outbytesleft);
\r
812 return (size_t)(-1);
\r
813 *outbuf += outsize;
\r
814 *outbytesleft -= outsize;
\r
816 if (is_unicode(cd->from.codepage) && (cd->from.mode & UNICODE_MODE_SWAPPED))
\r
817 cd->from.codepage ^= 1;
\r
823 while (*inbytesleft != 0)
\r
825 mode = cd->from.mode;
\r
826 wsize = MB_CHAR_MAX;
\r
828 insize = cd->from.mbtowc(&cd->from, (const uchar *)*inbuf, *inbytesleft, wbuf, &wsize);
\r
830 return (size_t)(-1);
\r
832 if (is_unicode(cd->from.codepage) && !(cd->from.mode & UNICODE_MODE_BOM_DONE))
\r
834 check_utf_bom(cd, wbuf, &wsize);
\r
835 cd->from.mode |= UNICODE_MODE_BOM_DONE;
\r
841 *inbytesleft -= insize;
\r
845 if (cd->from.compat != NULL)
\r
847 wc = utf16_to_ucs4(wbuf);
\r
848 cp = cd->from.compat;
\r
849 for (i = 0; cp[i].in != 0; ++i)
\r
851 if ((cp[i].flag & COMPAT_IN) && cp[i].out == wc)
\r
853 ucs4_to_utf16(cp[i].in, wbuf, &wsize);
\r
859 if (cd->to.compat != NULL)
\r
861 wc = utf16_to_ucs4(wbuf);
\r
862 cp = cd->to.compat;
\r
863 for (i = 0; cp[i].in != 0; ++i)
\r
865 if ((cp[i].flag & COMPAT_OUT) && cp[i].in == wc)
\r
867 ucs4_to_utf16(cp[i].out, wbuf, &wsize);
\r
873 outsize = cd->to.wctomb(&cd->to, wbuf, wsize, (uchar *)*outbuf, *outbytesleft);
\r
876 cd->from.mode = mode;
\r
877 return (size_t)(-1);
\r
881 *outbuf += outsize;
\r
882 *inbytesleft -= insize;
\r
883 *outbytesleft -= outsize;
\r
890 make_csconv(const char *_name)
\r
894 int use_compat = TRUE;
\r
899 xstrlcpy(name, _name, sizeof(name));
\r
901 /* check for option "enc_name//opt1//opt2" */
\r
902 while ((p = strrstr(name, "//")) != NULL)
\r
904 if (_stricmp(p + 2, "nocompat") == 0)
\r
905 use_compat = FALSE;
\r
906 else if (_stricmp(p + 2, "translit") == 0)
\r
907 flag |= FLAG_TRANSLIT;
\r
908 else if (_stricmp(p + 2, "ignore") == 0)
\r
909 flag |= FLAG_IGNORE;
\r
918 cv.codepage = name_to_codepage(name);
\r
919 if (cv.codepage == 1200 || cv.codepage == 1201)
\r
921 cv.mbtowc = utf16_mbtowc;
\r
922 cv.wctomb = utf16_wctomb;
\r
923 if (_stricmp(name, "UTF-16") == 0 ||
\r
924 _stricmp(name, "UTF16") == 0 ||
\r
925 _stricmp(name, "UCS-2") == 0 ||
\r
926 _stricmp(name, "UCS2") == 0)
\r
927 cv.flags |= FLAG_USE_BOM_ENDIAN;
\r
929 else if (cv.codepage == 12000 || cv.codepage == 12001)
\r
931 cv.mbtowc = utf32_mbtowc;
\r
932 cv.wctomb = utf32_wctomb;
\r
933 if (_stricmp(name, "UTF-32") == 0 ||
\r
934 _stricmp(name, "UTF32") == 0 ||
\r
935 _stricmp(name, "UCS-4") == 0 ||
\r
936 _stricmp(name, "UCS4") == 0)
\r
937 cv.flags |= FLAG_USE_BOM_ENDIAN;
\r
939 else if (cv.codepage == 65001)
\r
941 cv.mbtowc = kernel_mbtowc;
\r
942 cv.wctomb = kernel_wctomb;
\r
943 cv.mblen = utf8_mblen;
\r
945 else if ((cv.codepage == 50220 || cv.codepage == 50221 || cv.codepage == 50222) && load_mlang())
\r
947 cv.mbtowc = iso2022jp_mbtowc;
\r
948 cv.wctomb = iso2022jp_wctomb;
\r
949 cv.flush = iso2022jp_flush;
\r
951 else if (cv.codepage == 51932 && load_mlang())
\r
953 cv.mbtowc = mlang_mbtowc;
\r
954 cv.wctomb = mlang_wctomb;
\r
955 cv.mblen = eucjp_mblen;
\r
957 else if (IsValidCodePage(cv.codepage)
\r
958 && GetCPInfoEx(cv.codepage, 0, &cpinfoex) != 0)
\r
960 cv.mbtowc = kernel_mbtowc;
\r
961 cv.wctomb = kernel_wctomb;
\r
962 if (cpinfoex.MaxCharSize == 1)
\r
963 cv.mblen = sbcs_mblen;
\r
964 else if (cpinfoex.MaxCharSize == 2)
\r
965 cv.mblen = dbcs_mblen;
\r
967 cv.mblen = mbcs_mblen;
\r
971 /* not supported */
\r
976 switch (cv.codepage)
\r
978 case 932: cv.compat = cp932_compat; break;
\r
979 case 20932: cv.compat = cp20932_compat; break;
\r
980 case 51932: cv.compat = cp51932_compat; break;
\r
981 case 50220: case 50221: case 50222: cv.compat = cp5022x_compat; break;
\r
988 name_to_codepage(const char *name)
\r
992 if (*name == '\0' ||
\r
993 strcmp(name, "char") == 0)
\r
995 else if (strcmp(name, "wchar_t") == 0)
\r
997 else if (_strnicmp(name, "cp", 2) == 0)
\r
998 return atoi(name + 2); /* CP123 */
\r
999 else if ('0' <= name[0] && name[0] <= '9')
\r
1000 return atoi(name); /* 123 */
\r
1001 else if (_strnicmp(name, "xx", 2) == 0)
\r
1002 return atoi(name + 2); /* XX123 for debug */
\r
1004 for (i = 0; codepage_alias[i].name != NULL; ++i)
\r
1005 if (_stricmp(name, codepage_alias[i].name) == 0)
\r
1006 return codepage_alias[i].codepage;
\r
1011 * http://www.faqs.org/rfcs/rfc2781.html
\r
1014 utf16_to_ucs4(const ushort *wbuf)
\r
1016 uint wc = wbuf[0];
\r
1017 if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
\r
1018 wc = ((wbuf[0] & 0x3FF) << 10) + (wbuf[1] & 0x3FF) + 0x10000;
\r
1023 ucs4_to_utf16(uint wc, ushort *wbuf, int *wbufsize)
\r
1033 wbuf[0] = 0xD800 | ((wc >> 10) & 0x3FF);
\r
1034 wbuf[1] = 0xDC00 | (wc & 0x3FF);
\r
1040 is_unicode(int codepage)
\r
1042 return (codepage == 1200 || codepage == 1201 ||
\r
1043 codepage == 12000 || codepage == 12001 ||
\r
1044 codepage == 65000 || codepage == 65001);
\r
1048 * Check if codepage is one of those for which the dwFlags parameter
\r
1049 * to MultiByteToWideChar() must be zero. Return zero or
\r
1050 * MB_ERR_INVALID_CHARS. The docs in Platform SDK for for Windows
\r
1051 * Server 2003 R2 claims that also codepage 65001 is one of these, but
\r
1052 * that doesn't seem to be the case. The MSDN docs for MSVS2008 leave
\r
1053 * out 65001 (UTF-8), and that indeed seems to be the case on XP, it
\r
1054 * works fine to pass MB_ERR_INVALID_CHARS in dwFlags when converting
\r
1058 mbtowc_flags(int codepage)
\r
1060 return (codepage == 50220 || codepage == 50221 ||
\r
1061 codepage == 50222 || codepage == 50225 ||
\r
1062 codepage == 50227 || codepage == 50229 ||
\r
1063 codepage == 52936 || codepage == 54936 ||
\r
1064 (codepage >= 57002 && codepage <= 57011) ||
\r
1065 codepage == 65000 || codepage == 42) ? 0 : MB_ERR_INVALID_CHARS;
\r
1069 * Check if codepage is one those for which the lpUsedDefaultChar
\r
1070 * parameter to WideCharToMultiByte() must be NULL. The docs in
\r
1071 * Platform SDK for for Windows Server 2003 R2 claims that this is the
\r
1072 * list below, while the MSDN docs for MSVS2008 claim that it is only
\r
1073 * for 65000 (UTF-7) and 65001 (UTF-8). This time the earlier Platform
\r
1074 * SDK seems to be correct, at least for XP.
\r
1077 must_use_null_useddefaultchar(int codepage)
\r
1079 return (codepage == 65000 || codepage == 65001 ||
\r
1080 codepage == 50220 || codepage == 50221 ||
\r
1081 codepage == 50222 || codepage == 50225 ||
\r
1082 codepage == 50227 || codepage == 50229 ||
\r
1083 codepage == 52936 || codepage == 54936 ||
\r
1084 (codepage >= 57002 && codepage <= 57011) ||
\r
1089 check_utf_bom(rec_iconv_t *cd, ushort *wbuf, int *wbufsize)
\r
1091 /* If we have a BOM, trust it, despite what the caller said */
\r
1092 if (wbuf[0] == 0xFFFE && (cd->from.flags & FLAG_USE_BOM_ENDIAN))
\r
1094 /* swap endian: 1200 <-> 1201 or 12000 <-> 12001 */
\r
1095 cd->from.codepage ^= 1;
\r
1096 cd->from.mode |= UNICODE_MODE_SWAPPED;
\r
1102 * Don't do this if "to" is Unicode,
\r
1103 * except if "to" is UTF-8.
\r
1105 if (wbuf[0] == 0xFEFF && (!is_unicode(cd->to.codepage) || cd->to.codepage == 65001))
\r
1110 strrstr(const char *str, const char *token)
\r
1112 int len = strlen(token);
\r
1113 const char *p = str + strlen(str);
\r
1115 while (str <= --p)
\r
1116 if (p[0] == token[0] && strncmp(p, token, len) == 0)
\r
1121 #if defined(USE_LIBICONV_DLL)
\r
1123 libiconv_iconv_open(rec_iconv_t *cd, const char *fromcode, const char *tocode)
\r
1125 HMODULE hlibiconv = NULL;
\r
1126 HMODULE hmsvcrt = NULL;
\r
1127 char dllname[_MAX_PATH];
\r
1130 f_iconv_open _iconv_open;
\r
1133 * always try to load dll, so that we can switch dll in runtime.
\r
1136 /* XXX: getenv() can't get variable set by SetEnvironmentVariable() */
\r
1137 p = getenv("WINICONV_LIBICONV_DLL");
\r
1139 p = DEFAULT_LIBICONV_DLL;
\r
1140 /* parse comma separated value */
\r
1141 for ( ; *p != 0; p = (*e == ',') ? e + 1 : e)
\r
1143 e = strchr(p, ',');
\r
1146 else if (e == NULL)
\r
1147 e = p + strlen(p);
\r
1148 xstrlcpyn(dllname, p, e - p, sizeof(dllname));
\r
1149 hlibiconv = LoadLibrary(dllname);
\r
1150 if (hlibiconv != NULL)
\r
1152 if (hlibiconv == hwiniconv)
\r
1154 FreeLibrary(hlibiconv);
\r
1162 if (hlastdll != NULL)
\r
1164 /* decrement reference count */
\r
1165 FreeLibrary(hlastdll);
\r
1169 if (hlibiconv == NULL)
\r
1172 hmsvcrt = find_imported_module_by_funcname(hlibiconv, "_errno");
\r
1173 if (hmsvcrt == NULL)
\r
1176 _iconv_open = (f_iconv_open)GetProcAddress(hlibiconv, "libiconv_open");
\r
1177 if (_iconv_open == NULL)
\r
1178 _iconv_open = (f_iconv_open)GetProcAddress(hlibiconv, "iconv_open");
\r
1179 cd->iconv_close = (f_iconv_close)GetProcAddress(hlibiconv, "libiconv_close");
\r
1180 if (cd->iconv_close == NULL)
\r
1181 cd->iconv_close = (f_iconv_close)GetProcAddress(hlibiconv, "iconv_close");
\r
1182 cd->iconv = (f_iconv)GetProcAddress(hlibiconv, "libiconv");
\r
1183 if (cd->iconv == NULL)
\r
1184 cd->iconv = (f_iconv)GetProcAddress(hlibiconv, "iconv");
\r
1185 cd->_errno = (f_errno)GetProcAddress(hmsvcrt, "_errno");
\r
1186 if (_iconv_open == NULL || cd->iconv_close == NULL
\r
1187 || cd->iconv == NULL || cd->_errno == NULL)
\r
1190 /* increment reference count */
\r
1191 hlastdll = LoadLibrary(dllname);
\r
1193 cd->cd = _iconv_open(tocode, fromcode);
\r
1194 if (cd->cd == (iconv_t)(-1))
\r
1197 cd->hlibiconv = hlibiconv;
\r
1201 if (hlibiconv != NULL)
\r
1202 FreeLibrary(hlibiconv);
\r
1203 /* do not free hmsvcrt which is obtained by GetModuleHandle() */
\r
1209 * http://forums.belution.com/ja/vc/000/234/78s.shtml
\r
1210 * http://nienie.com/~masapico/api_ImageDirectoryEntryToData.html
\r
1212 * The formal way is
\r
1213 * imagehlp.h or dbghelp.h
\r
1214 * imagehlp.lib or dbghelp.lib
\r
1215 * ImageDirectoryEntryToData()
\r
1217 #define TO_DOS_HEADER(base) ((PIMAGE_DOS_HEADER)(base))
\r
1218 #define TO_NT_HEADERS(base) ((PIMAGE_NT_HEADERS)((LPBYTE)(base) + TO_DOS_HEADER(base)->e_lfanew))
\r
1220 MyImageDirectoryEntryToData(LPVOID Base, BOOLEAN MappedAsImage, USHORT DirectoryEntry, PULONG Size)
\r
1222 /* TODO: MappedAsImage? */
\r
1223 PIMAGE_DATA_DIRECTORY p;
\r
1224 p = TO_NT_HEADERS(Base)->OptionalHeader.DataDirectory + DirectoryEntry;
\r
1225 if (p->VirtualAddress == 0) {
\r
1230 return (PVOID)((LPBYTE)Base + p->VirtualAddress);
\r
1234 find_imported_module_by_funcname(HMODULE hModule, const char *funcname)
\r
1238 PIMAGE_IMPORT_DESCRIPTOR Imp;
\r
1239 PIMAGE_THUNK_DATA Name; /* Import Name Table */
\r
1240 PIMAGE_IMPORT_BY_NAME ImpName;
\r
1242 Base = (DWORD)hModule;
\r
1243 Imp = MyImageDirectoryEntryToData(
\r
1246 IMAGE_DIRECTORY_ENTRY_IMPORT,
\r
1250 for ( ; Imp->OriginalFirstThunk != 0; ++Imp)
\r
1252 Name = (PIMAGE_THUNK_DATA)(Base + Imp->OriginalFirstThunk);
\r
1253 for ( ; Name->u1.Ordinal != 0; ++Name)
\r
1255 if (!IMAGE_SNAP_BY_ORDINAL(Name->u1.Ordinal))
\r
1257 ImpName = (PIMAGE_IMPORT_BY_NAME)
\r
1258 (Base + (DWORD)Name->u1.AddressOfData);
\r
1259 if (strcmp((char *)ImpName->Name, funcname) == 0)
\r
1260 return GetModuleHandle((char *)(Base + Imp->Name));
\r
1269 sbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
\r
1275 dbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
\r
1277 int len = IsDBCSLeadByteEx(cv->codepage, buf[0]) ? 2 : 1;
\r
1278 if (bufsize < len)
\r
1279 return_error(EINVAL);
\r
1284 mbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
\r
1288 if (cv->codepage == 54936) {
\r
1289 if (buf[0] <= 0x7F) len = 1;
\r
1290 else if (buf[0] >= 0x81 && buf[0] <= 0xFE &&
\r
1292 ((buf[1] >= 0x40 && buf[1] <= 0x7E) ||
\r
1293 (buf[1] >= 0x80 && buf[1] <= 0xFE))) len = 2;
\r
1294 else if (buf[0] >= 0x81 && buf[0] <= 0xFE &&
\r
1296 buf[1] >= 0x30 && buf[1] <= 0x39) len = 4;
\r
1298 return_error(EINVAL);
\r
1302 return_error(EINVAL);
\r
1306 utf8_mblen(csconv_t *cv, const uchar *buf, int bufsize)
\r
1310 if (buf[0] < 0x80) len = 1;
\r
1311 else if ((buf[0] & 0xE0) == 0xC0) len = 2;
\r
1312 else if ((buf[0] & 0xF0) == 0xE0) len = 3;
\r
1313 else if ((buf[0] & 0xF8) == 0xF0) len = 4;
\r
1314 else if ((buf[0] & 0xFC) == 0xF8) len = 5;
\r
1315 else if ((buf[0] & 0xFE) == 0xFC) len = 6;
\r
1318 return_error(EILSEQ);
\r
1319 else if (bufsize < len)
\r
1320 return_error(EINVAL);
\r
1325 eucjp_mblen(csconv_t *cv, const uchar *buf, int bufsize)
\r
1327 if (buf[0] < 0x80) /* ASCII */
\r
1329 else if (buf[0] == 0x8E) /* JIS X 0201 */
\r
1332 return_error(EINVAL);
\r
1333 else if (!(0xA1 <= buf[1] && buf[1] <= 0xDF))
\r
1334 return_error(EILSEQ);
\r
1337 else if (buf[0] == 0x8F) /* JIS X 0212 */
\r
1340 return_error(EINVAL);
\r
1341 else if (!(0xA1 <= buf[1] && buf[1] <= 0xFE)
\r
1342 || !(0xA1 <= buf[2] && buf[2] <= 0xFE))
\r
1343 return_error(EILSEQ);
\r
1346 else /* JIS X 0208 */
\r
1349 return_error(EINVAL);
\r
1350 else if (!(0xA1 <= buf[0] && buf[0] <= 0xFE)
\r
1351 || !(0xA1 <= buf[1] && buf[1] <= 0xFE))
\r
1352 return_error(EILSEQ);
\r
1358 kernel_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
\r
1362 len = cv->mblen(cv, buf, bufsize);
\r
1365 /* If converting from ASCII, reject 8bit
\r
1366 * chars. MultiByteToWideChar() doesn't. Note that for ASCII we
\r
1367 * know that the mblen function is sbcs_mblen() so len is 1.
\r
1369 if (cv->codepage == 20127 && buf[0] >= 0x80)
\r
1370 return_error(EILSEQ);
\r
1371 *wbufsize = MultiByteToWideChar(cv->codepage, mbtowc_flags (cv->codepage),
\r
1372 (const char *)buf, len, (wchar_t *)wbuf, *wbufsize);
\r
1373 if (*wbufsize == 0)
\r
1374 return_error(EILSEQ);
\r
1379 kernel_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
\r
1381 BOOL usedDefaultChar = 0;
\r
1387 return_error(E2BIG);
\r
1388 if (!must_use_null_useddefaultchar(cv->codepage))
\r
1390 p = &usedDefaultChar;
\r
1391 #ifdef WC_NO_BEST_FIT_CHARS
\r
1392 if (!(cv->flags & FLAG_TRANSLIT))
\r
1393 flags |= WC_NO_BEST_FIT_CHARS;
\r
1396 len = WideCharToMultiByte(cv->codepage, flags,
\r
1397 (const wchar_t *)wbuf, wbufsize, (char *)buf, bufsize, NULL, p);
\r
1400 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
\r
1401 return_error(E2BIG);
\r
1402 return_error(EILSEQ);
\r
1404 else if (usedDefaultChar)
\r
1405 return_error(EILSEQ);
\r
1406 else if (cv->mblen(cv, buf, len) != len) /* validate result */
\r
1407 return_error(EILSEQ);
\r
1412 * It seems that the mode (cv->mode) is fixnum.
\r
1413 * For example, when converting iso-2022-jp(cp50221) to unicode:
\r
1414 * in ascii sequence: mode=0xC42C0000
\r
1415 * in jisx0208 sequence: mode=0xC42C0001
\r
1416 * "C42C" is same for each convert session.
\r
1417 * It should be: ((codepage-1)<<16)|state
\r
1420 mlang_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
\r
1426 len = cv->mblen(cv, buf, bufsize);
\r
1430 hr = ConvertINetMultiByteToUnicode(&cv->mode, cv->codepage,
\r
1431 (const char *)buf, &insize, (wchar_t *)wbuf, wbufsize);
\r
1432 if (hr != S_OK || insize != len)
\r
1433 return_error(EILSEQ);
\r
1438 mlang_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
\r
1440 char tmpbuf[MB_CHAR_MAX]; /* enough room for one character */
\r
1441 int tmpsize = MB_CHAR_MAX;
\r
1442 int insize = wbufsize;
\r
1445 hr = ConvertINetUnicodeToMultiByte(&cv->mode, cv->codepage,
\r
1446 (const wchar_t *)wbuf, &wbufsize, tmpbuf, &tmpsize);
\r
1447 if (hr != S_OK || insize != wbufsize)
\r
1448 return_error(EILSEQ);
\r
1449 else if (bufsize < tmpsize)
\r
1450 return_error(E2BIG);
\r
1451 else if (cv->mblen(cv, (uchar *)tmpbuf, tmpsize) != tmpsize)
\r
1452 return_error(EILSEQ);
\r
1453 memcpy(buf, tmpbuf, tmpsize);
\r
1458 utf16_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
\r
1461 return_error(EINVAL);
\r
1462 if (cv->codepage == 1200) /* little endian */
\r
1463 wbuf[0] = (buf[1] << 8) | buf[0];
\r
1464 else if (cv->codepage == 1201) /* big endian */
\r
1465 wbuf[0] = (buf[0] << 8) | buf[1];
\r
1466 if (0xDC00 <= wbuf[0] && wbuf[0] <= 0xDFFF)
\r
1467 return_error(EILSEQ);
\r
1468 if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
\r
1471 return_error(EINVAL);
\r
1472 if (cv->codepage == 1200) /* little endian */
\r
1473 wbuf[1] = (buf[3] << 8) | buf[2];
\r
1474 else if (cv->codepage == 1201) /* big endian */
\r
1475 wbuf[1] = (buf[2] << 8) | buf[3];
\r
1476 if (!(0xDC00 <= wbuf[1] && wbuf[1] <= 0xDFFF))
\r
1477 return_error(EILSEQ);
\r
1486 utf16_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
\r
1489 return_error(E2BIG);
\r
1490 if (cv->codepage == 1200) /* little endian */
\r
1492 buf[0] = (wbuf[0] & 0x00FF);
\r
1493 buf[1] = (wbuf[0] & 0xFF00) >> 8;
\r
1495 else if (cv->codepage == 1201) /* big endian */
\r
1497 buf[0] = (wbuf[0] & 0xFF00) >> 8;
\r
1498 buf[1] = (wbuf[0] & 0x00FF);
\r
1500 if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
\r
1503 return_error(E2BIG);
\r
1504 if (cv->codepage == 1200) /* little endian */
\r
1506 buf[2] = (wbuf[1] & 0x00FF);
\r
1507 buf[3] = (wbuf[1] & 0xFF00) >> 8;
\r
1509 else if (cv->codepage == 1201) /* big endian */
\r
1511 buf[2] = (wbuf[1] & 0xFF00) >> 8;
\r
1512 buf[3] = (wbuf[1] & 0x00FF);
\r
1520 utf32_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
\r
1525 return_error(EINVAL);
\r
1526 if (cv->codepage == 12000) /* little endian */
\r
1527 wc = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
\r
1528 else if (cv->codepage == 12001) /* big endian */
\r
1529 wc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
\r
1530 if ((0xD800 <= wc && wc <= 0xDFFF) || 0x10FFFF < wc)
\r
1531 return_error(EILSEQ);
\r
1532 ucs4_to_utf16(wc, wbuf, wbufsize);
\r
1537 utf32_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
\r
1542 return_error(E2BIG);
\r
1543 wc = utf16_to_ucs4(wbuf);
\r
1544 if (cv->codepage == 12000) /* little endian */
\r
1546 buf[0] = wc & 0x000000FF;
\r
1547 buf[1] = (wc & 0x0000FF00) >> 8;
\r
1548 buf[2] = (wc & 0x00FF0000) >> 16;
\r
1549 buf[3] = (wc & 0xFF000000) >> 24;
\r
1551 else if (cv->codepage == 12001) /* big endian */
\r
1553 buf[0] = (wc & 0xFF000000) >> 24;
\r
1554 buf[1] = (wc & 0x00FF0000) >> 16;
\r
1555 buf[2] = (wc & 0x0000FF00) >> 8;
\r
1556 buf[3] = wc & 0x000000FF;
\r
1562 * 50220: ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS)
\r
1563 * 50221: ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow
\r
1565 * 50222: ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte
\r
1568 * MultiByteToWideChar() and WideCharToMultiByte() behave differently
\r
1569 * depending on Windows version. On XP, WideCharToMultiByte() doesn't
\r
1570 * terminate result sequence with ascii escape. But Vista does.
\r
1571 * Use MLang instead.
\r
1574 #define ISO2022_MODE(cs, shift) (((cs) << 8) | (shift))
\r
1575 #define ISO2022_MODE_CS(mode) (((mode) >> 8) & 0xFF)
\r
1576 #define ISO2022_MODE_SHIFT(mode) ((mode) & 0xFF)
\r
1578 #define ISO2022_SI 0
\r
1579 #define ISO2022_SO 1
\r
1582 static const char iso2022_SI_seq[] = "\x0F";
\r
1584 static const char iso2022_SO_seq[] = "\x0E";
\r
1586 typedef struct iso2022_esc_t iso2022_esc_t;
\r
1587 struct iso2022_esc_t {
\r
1594 #define ISO2022JP_CS_ASCII 0
\r
1595 #define ISO2022JP_CS_JISX0201_ROMAN 1
\r
1596 #define ISO2022JP_CS_JISX0201_KANA 2
\r
1597 #define ISO2022JP_CS_JISX0208_1978 3
\r
1598 #define ISO2022JP_CS_JISX0208_1983 4
\r
1599 #define ISO2022JP_CS_JISX0212 5
\r
1601 static iso2022_esc_t iso2022jp_esc[] = {
\r
1602 {"\x1B\x28\x42", 3, 1, ISO2022JP_CS_ASCII},
\r
1603 {"\x1B\x28\x4A", 3, 1, ISO2022JP_CS_JISX0201_ROMAN},
\r
1604 {"\x1B\x28\x49", 3, 1, ISO2022JP_CS_JISX0201_KANA},
\r
1605 {"\x1B\x24\x40", 3, 2, ISO2022JP_CS_JISX0208_1983}, /* unify 1978 with 1983 */
\r
1606 {"\x1B\x24\x42", 3, 2, ISO2022JP_CS_JISX0208_1983},
\r
1607 {"\x1B\x24\x28\x44", 4, 2, ISO2022JP_CS_JISX0212},
\r
1612 iso2022jp_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
\r
1614 iso2022_esc_t *iesc = iso2022jp_esc;
\r
1615 char tmp[MB_CHAR_MAX];
\r
1625 if (buf[0] == 0x1B)
\r
1627 for (i = 0; iesc[i].esc != NULL; ++i)
\r
1629 esc_len = iesc[i].esc_len;
\r
1630 if (bufsize < esc_len)
\r
1632 if (strncmp((char *)buf, iesc[i].esc, bufsize) == 0)
\r
1633 return_error(EINVAL);
\r
1637 if (strncmp((char *)buf, iesc[i].esc, esc_len) == 0)
\r
1639 cv->mode = ISO2022_MODE(iesc[i].cs, ISO2022_SI);
\r
1645 /* not supported escape sequence */
\r
1646 return_error(EILSEQ);
\r
1648 else if (buf[0] == iso2022_SO_seq[0])
\r
1650 cv->mode = ISO2022_MODE(ISO2022_MODE_CS(cv->mode), ISO2022_SO);
\r
1654 else if (buf[0] == iso2022_SI_seq[0])
\r
1656 cv->mode = ISO2022_MODE(ISO2022_MODE_CS(cv->mode), ISO2022_SI);
\r
1661 cs = ISO2022_MODE_CS(cv->mode);
\r
1662 shift = ISO2022_MODE_SHIFT(cv->mode);
\r
1664 /* reset the mode for informal sequence */
\r
1665 if (buf[0] < 0x20)
\r
1667 cs = ISO2022JP_CS_ASCII;
\r
1668 shift = ISO2022_SI;
\r
1671 len = iesc[cs].len;
\r
1672 if (bufsize < len)
\r
1673 return_error(EINVAL);
\r
1674 for (i = 0; i < len; ++i)
\r
1675 if (!(buf[i] < 0x80))
\r
1676 return_error(EILSEQ);
\r
1677 esc_len = iesc[cs].esc_len;
\r
1678 memcpy(tmp, iesc[cs].esc, esc_len);
\r
1679 if (shift == ISO2022_SO)
\r
1681 memcpy(tmp + esc_len, iso2022_SO_seq, 1);
\r
1684 memcpy(tmp + esc_len, buf, len);
\r
1686 if ((cv->codepage == 50220 || cv->codepage == 50221
\r
1687 || cv->codepage == 50222) && shift == ISO2022_SO)
\r
1689 /* XXX: shift-out cannot be used for mbtowc (both kernel and
\r
1691 esc_len = iesc[ISO2022JP_CS_JISX0201_KANA].esc_len;
\r
1692 memcpy(tmp, iesc[ISO2022JP_CS_JISX0201_KANA].esc, esc_len);
\r
1693 memcpy(tmp + esc_len, buf, len);
\r
1696 insize = len + esc_len;
\r
1697 hr = ConvertINetMultiByteToUnicode(&dummy, cv->codepage,
\r
1698 (const char *)tmp, &insize, (wchar_t *)wbuf, wbufsize);
\r
1699 if (hr != S_OK || insize != len + esc_len)
\r
1700 return_error(EILSEQ);
\r
1702 /* Check for conversion error. Assuming defaultChar is 0x3F. */
\r
1703 /* ascii should be converted from ascii */
\r
1704 if (wbuf[0] == buf[0]
\r
1705 && cv->mode != ISO2022_MODE(ISO2022JP_CS_ASCII, ISO2022_SI))
\r
1706 return_error(EILSEQ);
\r
1708 /* reset the mode for informal sequence */
\r
1709 if (cv->mode != ISO2022_MODE(cs, shift))
\r
1710 cv->mode = ISO2022_MODE(cs, shift);
\r
1716 iso2022jp_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
\r
1718 iso2022_esc_t *iesc = iso2022jp_esc;
\r
1719 char tmp[MB_CHAR_MAX];
\r
1720 int tmpsize = MB_CHAR_MAX;
\r
1721 int insize = wbufsize;
\r
1731 * MultiByte = [escape sequence] + character + [escape sequence]
\r
1733 * Whether trailing escape sequence is added depends on which API is
\r
1734 * used (kernel or MLang, and its version).
\r
1736 hr = ConvertINetUnicodeToMultiByte(&dummy, cv->codepage,
\r
1737 (const wchar_t *)wbuf, &wbufsize, tmp, &tmpsize);
\r
1738 if (hr != S_OK || insize != wbufsize)
\r
1739 return_error(EILSEQ);
\r
1740 else if (bufsize < tmpsize)
\r
1741 return_error(E2BIG);
\r
1745 cs = ISO2022JP_CS_ASCII;
\r
1750 for (i = 1; iesc[i].esc != NULL; ++i)
\r
1752 esc_len = iesc[i].esc_len;
\r
1753 if (strncmp(tmp, iesc[i].esc, esc_len) == 0)
\r
1759 if (iesc[i].esc == NULL)
\r
1760 /* not supported escape sequence */
\r
1761 return_error(EILSEQ);
\r
1764 shift = ISO2022_SI;
\r
1765 if (tmp[esc_len] == iso2022_SO_seq[0])
\r
1767 shift = ISO2022_SO;
\r
1771 len = iesc[cs].len;
\r
1773 /* Check for converting error. Assuming defaultChar is 0x3F. */
\r
1774 /* ascii should be converted from ascii */
\r
1775 if (cs == ISO2022JP_CS_ASCII && !(wbuf[0] < 0x80))
\r
1776 return_error(EILSEQ);
\r
1777 else if (tmpsize < esc_len + len)
\r
1778 return_error(EILSEQ);
\r
1780 if (cv->mode == ISO2022_MODE(cs, shift))
\r
1782 /* remove escape sequence */
\r
1784 memmove(tmp, tmp + esc_len, len);
\r
1789 if (cs == ISO2022JP_CS_ASCII)
\r
1791 esc_len = iesc[ISO2022JP_CS_ASCII].esc_len;
\r
1792 memmove(tmp + esc_len, tmp, len);
\r
1793 memcpy(tmp, iesc[ISO2022JP_CS_ASCII].esc, esc_len);
\r
1795 if (ISO2022_MODE_SHIFT(cv->mode) == ISO2022_SO)
\r
1797 /* shift-in before changing to other mode */
\r
1798 memmove(tmp + 1, tmp, len + esc_len);
\r
1799 memcpy(tmp, iso2022_SI_seq, 1);
\r
1804 if (bufsize < len + esc_len)
\r
1805 return_error(E2BIG);
\r
1806 memcpy(buf, tmp, len + esc_len);
\r
1807 cv->mode = ISO2022_MODE(cs, shift);
\r
1808 return len + esc_len;
\r
1812 iso2022jp_flush(csconv_t *cv, uchar *buf, int bufsize)
\r
1814 iso2022_esc_t *iesc = iso2022jp_esc;
\r
1817 if (cv->mode != ISO2022_MODE(ISO2022JP_CS_ASCII, ISO2022_SI))
\r
1820 if (ISO2022_MODE_SHIFT(cv->mode) != ISO2022_SI)
\r
1822 if (ISO2022_MODE_CS(cv->mode) != ISO2022JP_CS_ASCII)
\r
1823 esc_len += iesc[ISO2022JP_CS_ASCII].esc_len;
\r
1824 if (bufsize < esc_len)
\r
1825 return_error(E2BIG);
\r
1828 if (ISO2022_MODE_SHIFT(cv->mode) != ISO2022_SI)
\r
1830 memcpy(buf, iso2022_SI_seq, 1);
\r
1833 if (ISO2022_MODE_CS(cv->mode) != ISO2022JP_CS_ASCII)
\r
1835 memcpy(buf + esc_len, iesc[ISO2022JP_CS_ASCII].esc,
\r
1836 iesc[ISO2022JP_CS_ASCII].esc_len);
\r
1837 esc_len += iesc[ISO2022JP_CS_ASCII].esc_len;
\r
1844 #if defined(MAKE_DLL) && defined(USE_LIBICONV_DLL)
\r
1846 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
\r
1848 switch( fdwReason )
\r
1850 case DLL_PROCESS_ATTACH:
\r
1851 hwiniconv = (HMODULE)hinstDLL;
\r
1853 case DLL_THREAD_ATTACH:
\r
1854 case DLL_THREAD_DETACH:
\r
1855 case DLL_PROCESS_DETACH:
\r
1862 #if defined(MAKE_EXE)
\r
1863 #include <stdio.h>
\r
1864 #include <fcntl.h>
\r
1867 main(int argc, char **argv)
\r
1869 char *fromcode = NULL;
\r
1870 char *tocode = NULL;
\r
1872 char inbuf[BUFSIZ];
\r
1873 char outbuf[BUFSIZ];
\r
1876 size_t inbytesleft;
\r
1877 size_t outbytesleft;
\r
1883 _setmode(_fileno(stdin), _O_BINARY);
\r
1884 _setmode(_fileno(stdout), _O_BINARY);
\r
1886 for (i = 1; i < argc; ++i)
\r
1888 if (strcmp(argv[i], "-l") == 0)
\r
1890 for (i = 0; codepage_alias[i].name != NULL; ++i)
\r
1891 printf("%s\n", codepage_alias[i].name);
\r
1895 if (strcmp(argv[i], "-f") == 0)
\r
1896 fromcode = argv[++i];
\r
1897 else if (strcmp(argv[i], "-t") == 0)
\r
1898 tocode = argv[++i];
\r
1901 in = fopen(argv[i], "rb");
\r
1904 fprintf(stderr, "cannot open %s\n", argv[i]);
\r
1911 if (fromcode == NULL || tocode == NULL)
\r
1913 printf("usage: %s -f from-enc -t to-enc [file]\n", argv[0]);
\r
1917 cd = iconv_open(tocode, fromcode);
\r
1918 if (cd == (iconv_t)(-1))
\r
1920 perror("iconv_open error");
\r
1924 while ((inbytesleft = fread(inbuf + rest, 1, sizeof(inbuf) - rest, in)) != 0
\r
1927 inbytesleft += rest;
\r
1930 outbytesleft = sizeof(outbuf);
\r
1931 r = iconv(cd, &pin, &inbytesleft, &pout, &outbytesleft);
\r
1932 fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, stdout);
\r
1933 if (r == (size_t)(-1) && errno != EINVAL && errno != E2BIG)
\r
1935 perror("conversion error");
\r
1938 memmove(inbuf, pin, inbytesleft);
\r
1939 rest = inbytesleft;
\r
1942 outbytesleft = sizeof(outbuf);
\r
1943 r = iconv(cd, NULL, NULL, &pout, &outbytesleft);
\r
1944 fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, stdout);
\r
1945 if (r == (size_t)(-1))
\r
1947 perror("conversion error");
\r