2 * iconv library implemented with Win32 API.
4 * This file is placed in the public domain.
6 * Maintainer: Yukihiro Nakadaira <yukihiro.nakadaira@gmail.com>
8 * If $WINICONV_LIBICONV_DLL environment variable was defined, win_iconv
9 * loads the specified DLL dynamically and uses it. If loading the DLL
10 * or iconv_open() failed, falls back to internal conversion.
11 * $WINICONV_LIBICONV_DLL is a comma separated list. The first loadable
12 * DLL is used. The specified DLL should have iconv_open(),
13 * iconv_close() and iconv() functions. Or these functions can be
14 * libiconv_open(), libiconv_close() and libiconv().
16 * Win32 API does not support strict encoding conversion for some
17 * codepage. And MLang function drop or replace invalid bytes and does
18 * not return useful error status as iconv. This implementation cannot
19 * be used for encoding validation purpose.
22 /* for WC_NO_BEST_FIT_CHARS */
24 # define WINVER 0x0500
36 # define USE_LIBICONV_DLL
39 #if !defined(DEFAULT_LIBICONV_DLL)
40 # define DEFAULT_LIBICONV_DLL ""
43 #define MB_CHAR_MAX 16
45 #define UNICODE_MODE_BOM_DONE 1
46 #define UNICODE_MODE_SWAPPED 2
48 #define FLAG_USE_BOM_ENDIAN 1
49 #define FLAG_TRANSLIT 2 /* //TRANSLIT */
50 #define FLAG_IGNORE 4 /* //IGNORE (not implemented) */
52 #define return_error(code) \
58 #define xstrlcpy(dst, src, size) \
60 strncpy(dst, src, size); \
64 #define xstrlcpyn(dst, src, srclen, size) \
65 xstrlcpy(dst, src, xmin((srclen) + 1, size))
67 #define xmin(a, b) ((a) < (b) ? (a) : (b))
68 #define xmax(a, b) ((a) > (b) ? (a) : (b))
70 #define STATIC_STRLEN(arr) (sizeof(arr) - 1)
72 typedef unsigned char uchar;
73 typedef unsigned short ushort;
74 typedef unsigned int uint;
76 typedef void* iconv_t;
78 iconv_t iconv_open(const char *tocode, const char *fromcode);
79 int iconv_close(iconv_t cd);
80 size_t iconv(iconv_t cd, /* const */ char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
82 /* libiconv interface for vim */
85 iconvctl (iconv_t cd, int request, void* argument)
92 typedef struct compat_t compat_t;
93 typedef struct csconv_t csconv_t;
94 typedef struct rec_iconv_t rec_iconv_t;
96 typedef iconv_t (*f_iconv_open)(const char *tocode, const char *fromcode);
97 typedef int (*f_iconv_close)(iconv_t cd);
98 typedef size_t (*f_iconv)(iconv_t cd, /* const */ char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
99 typedef int* (*f_errno)(void);
100 typedef int (*f_mbtowc)(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
101 typedef int (*f_wctomb)(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
102 typedef int (*f_mblen)(csconv_t *cv, const uchar *buf, int bufsize);
103 typedef int (*f_flush)(csconv_t *cv, uchar *buf, int bufsize);
108 /* unicode mapping for compatibility with other conversion table. */
128 f_iconv_close iconv_close;
133 #if defined(USE_LIBICONV_DLL)
138 static int win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
139 static int win_iconv_close(iconv_t cd);
140 static size_t win_iconv(iconv_t cd, /* const */ char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
142 static int load_mlang(void);
143 static csconv_t make_csconv(const char *name);
144 static int name_to_codepage(const char *name);
145 static uint utf16_to_ucs4(const ushort *wbuf);
146 static void ucs4_to_utf16(uint wc, ushort *wbuf, int *wbufsize);
147 static int is_unicode(int codepage);
148 static int mbtowc_flags(int codepage);
149 static int must_use_null_useddefaultchar(int codepage);
150 static void check_utf_bom(rec_iconv_t *cd, ushort *wbuf, int *wbufsize);
151 static char *strrstr(const char *str, const char *token);
153 #if defined(USE_LIBICONV_DLL)
154 static int libiconv_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
155 static PVOID MyImageDirectoryEntryToData(LPVOID Base, BOOLEAN MappedAsImage, USHORT DirectoryEntry, PULONG Size);
156 static HMODULE find_imported_module_by_funcname(HMODULE hModule, const char *funcname);
158 static HMODULE hwiniconv;
159 static HMODULE hlastdll; /* keep dll loaded for efficiency (unnecessary?) */
162 static int sbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
163 static int dbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
164 static int mbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
165 static int utf8_mblen(csconv_t *cv, const uchar *buf, int bufsize);
166 static int eucjp_mblen(csconv_t *cv, const uchar *buf, int bufsize);
168 static int kernel_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
169 static int kernel_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
170 static int mlang_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
171 static int mlang_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
172 static int utf16_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
173 static int utf16_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
174 static int utf32_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
175 static int utf32_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
176 static int iso2022jp_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
177 static int iso2022jp_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
178 static int iso2022jp_flush(csconv_t *cv, uchar *buf, int bufsize);
183 } codepage_alias[] = {
199 {1201, "unicodeFFFE"},
213 #ifndef GLIB_COMPILATION
215 * Default is big endian.
216 * See rfc2781 4.3 Interpreting text labelled as UTF-16.
225 /* Default is little endian, because the platform is */
236 /* copy from libiconv `iconv -l` */
237 /* !IsValidCodePage(367) */
238 {20127, "ANSI_X3.4-1968"},
239 {20127, "ANSI_X3.4-1986"},
244 {20127, "ISO646-US"},
245 {20127, "ISO_646.IRV:1991"},
250 /* !IsValidCodePage(819) */
253 {28591, "ISO-8859-1"},
254 {28591, "ISO-IR-100"},
255 {28591, "ISO8859-1"},
256 {28591, "ISO_8859-1"},
257 {28591, "ISO_8859-1:1987"},
260 {28591, "CSISOLATIN1"},
264 {1250, "WINDOWS-1250"},
268 {1251, "WINDOWS-1251"},
272 {1252, "WINDOWS-1252"},
276 {1253, "WINDOWS-1253"},
280 {1254, "WINDOWS-1254"},
284 {1255, "WINDOWS-1255"},
288 {1256, "WINDOWS-1256"},
291 {1257, "WINBALTRIM"},
292 {1257, "WINDOWS-1257"},
295 {1258, "WINDOWS-1258"},
300 {850, "CSPC850MULTILINGUAL"},
302 /* !IsValidCodePage(862) */
306 {862, "CSPC862LATINHEBREW"},
313 /* !IsValidCodePage(154) */
315 {154, "CYRILLIC-ASIAN"},
320 /* !IsValidCodePage(1133) */
322 {1133, "IBM-CP1133"},
325 {874, "WINDOWS-874"},
327 /* !IsValidCodePage(51932) */
330 {51932, "WINDOWS-51932"},
336 {932, "SHIFFT_JIS-MS"},
341 {932, "WINDOWS-31J"},
342 {932, "WINDOWS-932"},
343 {932, "CSWINDOWS31J"},
346 {50221, "ISO-2022-JP"},
347 {50221, "ISO-2022-JP-MS"},
348 {50221, "ISO2022-JP"},
349 {50221, "ISO2022-JP-MS"},
351 {50221, "WINDOWS-50221"},
356 {936, "WINDOWS-936"},
371 {437, "CSPC8CODEPAGE437"},
377 {775, "CSPC775BALTIC"},
384 /* !IsValidCodePage(853) */
397 /* !IsValidCodePage(858) */
431 /* !IsValidCodePage(1152) */
435 * Code Page Identifiers
436 * http://msdn2.microsoft.com/en-us/library/ms776446.aspx
438 {37, "IBM037"}, /* IBM EBCDIC US-Canada */
439 {437, "IBM437"}, /* OEM United States */
440 {500, "IBM500"}, /* IBM EBCDIC International */
441 {708, "ASMO-708"}, /* Arabic (ASMO 708) */
442 /* 709 Arabic (ASMO-449+, BCON V4) */
443 /* 710 Arabic - Transparent Arabic */
444 {720, "DOS-720"}, /* Arabic (Transparent ASMO); Arabic (DOS) */
445 {737, "ibm737"}, /* OEM Greek (formerly 437G); Greek (DOS) */
446 {775, "ibm775"}, /* OEM Baltic; Baltic (DOS) */
447 {850, "ibm850"}, /* OEM Multilingual Latin 1; Western European (DOS) */
448 {852, "ibm852"}, /* OEM Latin 2; Central European (DOS) */
449 {855, "IBM855"}, /* OEM Cyrillic (primarily Russian) */
450 {857, "ibm857"}, /* OEM Turkish; Turkish (DOS) */
451 {858, "IBM00858"}, /* OEM Multilingual Latin 1 + Euro symbol */
452 {860, "IBM860"}, /* OEM Portuguese; Portuguese (DOS) */
453 {861, "ibm861"}, /* OEM Icelandic; Icelandic (DOS) */
454 {862, "DOS-862"}, /* OEM Hebrew; Hebrew (DOS) */
455 {863, "IBM863"}, /* OEM French Canadian; French Canadian (DOS) */
456 {864, "IBM864"}, /* OEM Arabic; Arabic (864) */
457 {865, "IBM865"}, /* OEM Nordic; Nordic (DOS) */
458 {866, "cp866"}, /* OEM Russian; Cyrillic (DOS) */
459 {869, "ibm869"}, /* OEM Modern Greek; Greek, Modern (DOS) */
460 {870, "IBM870"}, /* IBM EBCDIC Multilingual/ROECE (Latin 2); IBM EBCDIC Multilingual Latin 2 */
461 {874, "windows-874"}, /* ANSI/OEM Thai (same as 28605, ISO 8859-15); Thai (Windows) */
462 {875, "cp875"}, /* IBM EBCDIC Greek Modern */
463 {932, "shift_jis"}, /* ANSI/OEM Japanese; Japanese (Shift-JIS) */
464 {932, "shift-jis"}, /* alternative name for it */
465 {936, "gb2312"}, /* ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312) */
466 {949, "ks_c_5601-1987"}, /* ANSI/OEM Korean (Unified Hangul Code) */
467 {950, "big5"}, /* ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5) */
468 {1026, "IBM1026"}, /* IBM EBCDIC Turkish (Latin 5) */
469 {1047, "IBM01047"}, /* IBM EBCDIC Latin 1/Open System */
470 {1140, "IBM01140"}, /* IBM EBCDIC US-Canada (037 + Euro symbol); IBM EBCDIC (US-Canada-Euro) */
471 {1141, "IBM01141"}, /* IBM EBCDIC Germany (20273 + Euro symbol); IBM EBCDIC (Germany-Euro) */
472 {1142, "IBM01142"}, /* IBM EBCDIC Denmark-Norway (20277 + Euro symbol); IBM EBCDIC (Denmark-Norway-Euro) */
473 {1143, "IBM01143"}, /* IBM EBCDIC Finland-Sweden (20278 + Euro symbol); IBM EBCDIC (Finland-Sweden-Euro) */
474 {1144, "IBM01144"}, /* IBM EBCDIC Italy (20280 + Euro symbol); IBM EBCDIC (Italy-Euro) */
475 {1145, "IBM01145"}, /* IBM EBCDIC Latin America-Spain (20284 + Euro symbol); IBM EBCDIC (Spain-Euro) */
476 {1146, "IBM01146"}, /* IBM EBCDIC United Kingdom (20285 + Euro symbol); IBM EBCDIC (UK-Euro) */
477 {1147, "IBM01147"}, /* IBM EBCDIC France (20297 + Euro symbol); IBM EBCDIC (France-Euro) */
478 {1148, "IBM01148"}, /* IBM EBCDIC International (500 + Euro symbol); IBM EBCDIC (International-Euro) */
479 {1149, "IBM01149"}, /* IBM EBCDIC Icelandic (20871 + Euro symbol); IBM EBCDIC (Icelandic-Euro) */
480 {1250, "windows-1250"}, /* ANSI Central European; Central European (Windows) */
481 {1251, "windows-1251"}, /* ANSI Cyrillic; Cyrillic (Windows) */
482 {1252, "windows-1252"}, /* ANSI Latin 1; Western European (Windows) */
483 {1253, "windows-1253"}, /* ANSI Greek; Greek (Windows) */
484 {1254, "windows-1254"}, /* ANSI Turkish; Turkish (Windows) */
485 {1255, "windows-1255"}, /* ANSI Hebrew; Hebrew (Windows) */
486 {1256, "windows-1256"}, /* ANSI Arabic; Arabic (Windows) */
487 {1257, "windows-1257"}, /* ANSI Baltic; Baltic (Windows) */
488 {1258, "windows-1258"}, /* ANSI/OEM Vietnamese; Vietnamese (Windows) */
489 {1361, "Johab"}, /* Korean (Johab) */
490 {10000, "macintosh"}, /* MAC Roman; Western European (Mac) */
491 {10001, "x-mac-japanese"}, /* Japanese (Mac) */
492 {10002, "x-mac-chinesetrad"}, /* MAC Traditional Chinese (Big5); Chinese Traditional (Mac) */
493 {10003, "x-mac-korean"}, /* Korean (Mac) */
494 {10004, "x-mac-arabic"}, /* Arabic (Mac) */
495 {10005, "x-mac-hebrew"}, /* Hebrew (Mac) */
496 {10006, "x-mac-greek"}, /* Greek (Mac) */
497 {10007, "x-mac-cyrillic"}, /* Cyrillic (Mac) */
498 {10008, "x-mac-chinesesimp"}, /* MAC Simplified Chinese (GB 2312); Chinese Simplified (Mac) */
499 {10010, "x-mac-romanian"}, /* Romanian (Mac) */
500 {10017, "x-mac-ukrainian"}, /* Ukrainian (Mac) */
501 {10021, "x-mac-thai"}, /* Thai (Mac) */
502 {10029, "x-mac-ce"}, /* MAC Latin 2; Central European (Mac) */
503 {10079, "x-mac-icelandic"}, /* Icelandic (Mac) */
504 {10081, "x-mac-turkish"}, /* Turkish (Mac) */
505 {10082, "x-mac-croatian"}, /* Croatian (Mac) */
506 {20000, "x-Chinese_CNS"}, /* CNS Taiwan; Chinese Traditional (CNS) */
507 {20001, "x-cp20001"}, /* TCA Taiwan */
508 {20002, "x_Chinese-Eten"}, /* Eten Taiwan; Chinese Traditional (Eten) */
509 {20003, "x-cp20003"}, /* IBM5550 Taiwan */
510 {20004, "x-cp20004"}, /* TeleText Taiwan */
511 {20005, "x-cp20005"}, /* Wang Taiwan */
512 {20105, "x-IA5"}, /* IA5 (IRV International Alphabet No. 5, 7-bit); Western European (IA5) */
513 {20106, "x-IA5-German"}, /* IA5 German (7-bit) */
514 {20107, "x-IA5-Swedish"}, /* IA5 Swedish (7-bit) */
515 {20108, "x-IA5-Norwegian"}, /* IA5 Norwegian (7-bit) */
516 {20127, "us-ascii"}, /* US-ASCII (7-bit) */
517 {20261, "x-cp20261"}, /* T.61 */
518 {20269, "x-cp20269"}, /* ISO 6937 Non-Spacing Accent */
519 {20273, "IBM273"}, /* IBM EBCDIC Germany */
520 {20277, "IBM277"}, /* IBM EBCDIC Denmark-Norway */
521 {20278, "IBM278"}, /* IBM EBCDIC Finland-Sweden */
522 {20280, "IBM280"}, /* IBM EBCDIC Italy */
523 {20284, "IBM284"}, /* IBM EBCDIC Latin America-Spain */
524 {20285, "IBM285"}, /* IBM EBCDIC United Kingdom */
525 {20290, "IBM290"}, /* IBM EBCDIC Japanese Katakana Extended */
526 {20297, "IBM297"}, /* IBM EBCDIC France */
527 {20420, "IBM420"}, /* IBM EBCDIC Arabic */
528 {20423, "IBM423"}, /* IBM EBCDIC Greek */
529 {20424, "IBM424"}, /* IBM EBCDIC Hebrew */
530 {20833, "x-EBCDIC-KoreanExtended"}, /* IBM EBCDIC Korean Extended */
531 {20838, "IBM-Thai"}, /* IBM EBCDIC Thai */
532 {20866, "koi8-r"}, /* Russian (KOI8-R); Cyrillic (KOI8-R) */
533 {20871, "IBM871"}, /* IBM EBCDIC Icelandic */
534 {20880, "IBM880"}, /* IBM EBCDIC Cyrillic Russian */
535 {20905, "IBM905"}, /* IBM EBCDIC Turkish */
536 {20924, "IBM00924"}, /* IBM EBCDIC Latin 1/Open System (1047 + Euro symbol) */
537 {20932, "EUC-JP"}, /* Japanese (JIS 0208-1990 and 0121-1990) */
538 {20936, "x-cp20936"}, /* Simplified Chinese (GB2312); Chinese Simplified (GB2312-80) */
539 {20949, "x-cp20949"}, /* Korean Wansung */
540 {21025, "cp1025"}, /* IBM EBCDIC Cyrillic Serbian-Bulgarian */
541 /* 21027 (deprecated) */
542 {21866, "koi8-u"}, /* Ukrainian (KOI8-U); Cyrillic (KOI8-U) */
543 {28591, "iso-8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
544 {28591, "iso8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
545 {28592, "iso-8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
546 {28592, "iso8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
547 {28593, "iso-8859-3"}, /* ISO 8859-3 Latin 3 */
548 {28593, "iso8859-3"}, /* ISO 8859-3 Latin 3 */
549 {28594, "iso-8859-4"}, /* ISO 8859-4 Baltic */
550 {28594, "iso8859-4"}, /* ISO 8859-4 Baltic */
551 {28595, "iso-8859-5"}, /* ISO 8859-5 Cyrillic */
552 {28595, "iso8859-5"}, /* ISO 8859-5 Cyrillic */
553 {28596, "iso-8859-6"}, /* ISO 8859-6 Arabic */
554 {28596, "iso8859-6"}, /* ISO 8859-6 Arabic */
555 {28597, "iso-8859-7"}, /* ISO 8859-7 Greek */
556 {28597, "iso8859-7"}, /* ISO 8859-7 Greek */
557 {28598, "iso-8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
558 {28598, "iso8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
559 {28599, "iso-8859-9"}, /* ISO 8859-9 Turkish */
560 {28599, "iso8859-9"}, /* ISO 8859-9 Turkish */
561 {28603, "iso-8859-13"}, /* ISO 8859-13 Estonian */
562 {28603, "iso8859-13"}, /* ISO 8859-13 Estonian */
563 {28605, "iso-8859-15"}, /* ISO 8859-15 Latin 9 */
564 {28605, "iso8859-15"}, /* ISO 8859-15 Latin 9 */
565 {29001, "x-Europa"}, /* Europa 3 */
566 {38598, "iso-8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
567 {38598, "iso8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
568 {50220, "iso-2022-jp"}, /* ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS) */
569 {50221, "csISO2022JP"}, /* ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow 1 byte Kana) */
570 {50222, "iso-2022-jp"}, /* ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte Kana - SO/SI) */
571 {50225, "iso-2022-kr"}, /* ISO 2022 Korean */
572 {50225, "iso2022-kr"}, /* ISO 2022 Korean */
573 {50227, "x-cp50227"}, /* ISO 2022 Simplified Chinese; Chinese Simplified (ISO 2022) */
574 /* 50229 ISO 2022 Traditional Chinese */
575 /* 50930 EBCDIC Japanese (Katakana) Extended */
576 /* 50931 EBCDIC US-Canada and Japanese */
577 /* 50933 EBCDIC Korean Extended and Korean */
578 /* 50935 EBCDIC Simplified Chinese Extended and Simplified Chinese */
579 /* 50936 EBCDIC Simplified Chinese */
580 /* 50937 EBCDIC US-Canada and Traditional Chinese */
581 /* 50939 EBCDIC Japanese (Latin) Extended and Japanese */
582 {51932, "euc-jp"}, /* EUC Japanese */
583 {51936, "EUC-CN"}, /* EUC Simplified Chinese; Chinese Simplified (EUC) */
584 {51949, "euc-kr"}, /* EUC Korean */
585 /* 51950 EUC Traditional Chinese */
586 {52936, "hz-gb-2312"}, /* HZ-GB2312 Simplified Chinese; Chinese Simplified (HZ) */
587 {54936, "GB18030"}, /* Windows XP and later: GB18030 Simplified Chinese (4 byte); Chinese Simplified (GB18030) */
588 {57002, "x-iscii-de"}, /* ISCII Devanagari */
589 {57003, "x-iscii-be"}, /* ISCII Bengali */
590 {57004, "x-iscii-ta"}, /* ISCII Tamil */
591 {57005, "x-iscii-te"}, /* ISCII Telugu */
592 {57006, "x-iscii-as"}, /* ISCII Assamese */
593 {57007, "x-iscii-or"}, /* ISCII Oriya */
594 {57008, "x-iscii-ka"}, /* ISCII Kannada */
595 {57009, "x-iscii-ma"}, /* ISCII Malayalam */
596 {57010, "x-iscii-gu"}, /* ISCII Gujarati */
597 {57011, "x-iscii-pa"}, /* ISCII Punjabi */
603 * SJIS SHIFTJIS table CP932 table
604 * ---- --------------------------- --------------------------------
605 * 5C U+00A5 YEN SIGN U+005C REVERSE SOLIDUS
606 * 7E U+203E OVERLINE U+007E TILDE
607 * 815C U+2014 EM DASH U+2015 HORIZONTAL BAR
608 * 815F U+005C REVERSE SOLIDUS U+FF3C FULLWIDTH REVERSE SOLIDUS
609 * 8160 U+301C WAVE DASH U+FF5E FULLWIDTH TILDE
610 * 8161 U+2016 DOUBLE VERTICAL LINE U+2225 PARALLEL TO
611 * 817C U+2212 MINUS SIGN U+FF0D FULLWIDTH HYPHEN-MINUS
612 * 8191 U+00A2 CENT SIGN U+FFE0 FULLWIDTH CENT SIGN
613 * 8192 U+00A3 POUND SIGN U+FFE1 FULLWIDTH POUND SIGN
614 * 81CA U+00AC NOT SIGN U+FFE2 FULLWIDTH NOT SIGN
616 * EUC-JP and ISO-2022-JP should be compatible with CP932.
618 * Kernel and MLang have different Unicode mapping table. Make sure
621 static compat_t cp932_compat[] = {
622 {0x00A5, 0x005C, COMPAT_OUT},
623 {0x203E, 0x007E, COMPAT_OUT},
624 {0x2014, 0x2015, COMPAT_OUT},
625 {0x301C, 0xFF5E, COMPAT_OUT},
626 {0x2016, 0x2225, COMPAT_OUT},
627 {0x2212, 0xFF0D, COMPAT_OUT},
628 {0x00A2, 0xFFE0, COMPAT_OUT},
629 {0x00A3, 0xFFE1, COMPAT_OUT},
630 {0x00AC, 0xFFE2, COMPAT_OUT},
634 static compat_t cp20932_compat[] = {
635 {0x00A5, 0x005C, COMPAT_OUT},
636 {0x203E, 0x007E, COMPAT_OUT},
637 {0x2014, 0x2015, COMPAT_OUT},
638 {0xFF5E, 0x301C, COMPAT_OUT|COMPAT_IN},
639 {0x2225, 0x2016, COMPAT_OUT|COMPAT_IN},
640 {0xFF0D, 0x2212, COMPAT_OUT|COMPAT_IN},
641 {0xFFE0, 0x00A2, COMPAT_OUT|COMPAT_IN},
642 {0xFFE1, 0x00A3, COMPAT_OUT|COMPAT_IN},
643 {0xFFE2, 0x00AC, COMPAT_OUT|COMPAT_IN},
647 static compat_t *cp51932_compat = cp932_compat;
649 /* cp20932_compat for kernel. cp932_compat for mlang. */
650 static compat_t *cp5022x_compat = cp932_compat;
652 typedef HRESULT (WINAPI *CONVERTINETSTRING)(
661 typedef HRESULT (WINAPI *CONVERTINETMULTIBYTETOUNICODE)(
665 LPINT lpnMultiCharCount,
667 LPINT lpnWideCharCount
669 typedef HRESULT (WINAPI *CONVERTINETUNICODETOMULTIBYTE)(
673 LPINT lpnWideCharCount,
675 LPINT lpnMultiCharCount
677 typedef HRESULT (WINAPI *ISCONVERTINETSTRINGAVAILABLE)(
681 typedef HRESULT (WINAPI *LCIDTORFC1766A)(
686 typedef HRESULT (WINAPI *LCIDTORFC1766W)(
691 typedef HRESULT (WINAPI *RFC1766TOLCIDA)(
695 typedef HRESULT (WINAPI *RFC1766TOLCIDW)(
699 static CONVERTINETSTRING ConvertINetString;
700 static CONVERTINETMULTIBYTETOUNICODE ConvertINetMultiByteToUnicode;
701 static CONVERTINETUNICODETOMULTIBYTE ConvertINetUnicodeToMultiByte;
702 static ISCONVERTINETSTRINGAVAILABLE IsConvertINetStringAvailable;
703 static LCIDTORFC1766A LcidToRfc1766A;
704 static RFC1766TOLCIDA Rfc1766ToLcidA;
710 char mlang_dll[MAX_PATH + 100];
712 if (ConvertINetString != NULL)
714 n = GetSystemDirectory(mlang_dll, MAX_PATH);
715 if (n > 0 && n < MAX_PATH)
717 if (mlang_dll[n-1] != '\\' &&
718 mlang_dll[n-1] != '/')
719 strcat(mlang_dll, "\\");
720 strcat(mlang_dll, "mlang.dll");
721 h = LoadLibrary(mlang_dll);
725 ConvertINetString = (CONVERTINETSTRING)GetProcAddress(h, "ConvertINetString");
726 ConvertINetMultiByteToUnicode = (CONVERTINETMULTIBYTETOUNICODE)GetProcAddress(h, "ConvertINetMultiByteToUnicode");
727 ConvertINetUnicodeToMultiByte = (CONVERTINETUNICODETOMULTIBYTE)GetProcAddress(h, "ConvertINetUnicodeToMultiByte");
728 IsConvertINetStringAvailable = (ISCONVERTINETSTRINGAVAILABLE)GetProcAddress(h, "IsConvertINetStringAvailable");
729 LcidToRfc1766A = (LCIDTORFC1766A)GetProcAddress(h, "LcidToRfc1766A");
730 Rfc1766ToLcidA = (RFC1766TOLCIDA)GetProcAddress(h, "Rfc1766ToLcidA");
735 iconv_open(const char *tocode, const char *fromcode)
739 cd = (rec_iconv_t *)calloc(1, sizeof(rec_iconv_t));
743 return (iconv_t)(-1);
746 #if defined(USE_LIBICONV_DLL)
747 if (libiconv_iconv_open(cd, tocode, fromcode))
751 if (win_iconv_open(cd, tocode, fromcode))
756 return (iconv_t)(-1);
760 iconv_close(iconv_t _cd)
762 rec_iconv_t *cd = (rec_iconv_t *)_cd;
763 int r = cd->iconv_close(cd->cd);
764 int e = *(cd->_errno());
765 #if defined(USE_LIBICONV_DLL)
766 if (cd->hlibiconv != NULL)
767 FreeLibrary(cd->hlibiconv);
775 iconv(iconv_t _cd, /* const */ char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
777 rec_iconv_t *cd = (rec_iconv_t *)_cd;
778 size_t r = cd->iconv(cd->cd, inbuf, inbytesleft, outbuf, outbytesleft);
779 errno = *(cd->_errno());
784 win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode)
786 cd->from = make_csconv(fromcode);
787 cd->to = make_csconv(tocode);
788 if (cd->from.codepage == -1 || cd->to.codepage == -1)
790 cd->iconv_close = win_iconv_close;
791 cd->iconv = win_iconv;
793 cd->cd = (iconv_t)cd;
798 win_iconv_close(iconv_t cd)
804 win_iconv(iconv_t _cd, /* const */ char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
806 rec_iconv_t *cd = (rec_iconv_t *)_cd;
807 ushort wbuf[MB_CHAR_MAX]; /* enough room for one character */
816 if (inbuf == NULL || *inbuf == NULL)
818 if (outbuf != NULL && *outbuf != NULL && cd->to.flush != NULL)
820 outsize = cd->to.flush(&cd->to, (uchar *)*outbuf, *outbytesleft);
824 *outbytesleft -= outsize;
826 if (is_unicode(cd->from.codepage) && (cd->from.mode & UNICODE_MODE_SWAPPED))
827 cd->from.codepage ^= 1;
833 while (*inbytesleft != 0)
835 mode = cd->from.mode;
838 insize = cd->from.mbtowc(&cd->from, (const uchar *)*inbuf, *inbytesleft, wbuf, &wsize);
842 if (is_unicode(cd->from.codepage) && !(cd->from.mode & UNICODE_MODE_BOM_DONE))
844 check_utf_bom(cd, wbuf, &wsize);
845 cd->from.mode |= UNICODE_MODE_BOM_DONE;
851 *inbytesleft -= insize;
855 if (cd->from.compat != NULL)
857 wc = utf16_to_ucs4(wbuf);
858 cp = cd->from.compat;
859 for (i = 0; cp[i].in != 0; ++i)
861 if ((cp[i].flag & COMPAT_IN) && cp[i].out == wc)
863 ucs4_to_utf16(cp[i].in, wbuf, &wsize);
869 if (cd->to.compat != NULL)
871 wc = utf16_to_ucs4(wbuf);
873 for (i = 0; cp[i].in != 0; ++i)
875 if ((cp[i].flag & COMPAT_OUT) && cp[i].in == wc)
877 ucs4_to_utf16(cp[i].out, wbuf, &wsize);
883 outsize = cd->to.wctomb(&cd->to, wbuf, wsize, (uchar *)*outbuf, *outbytesleft);
886 cd->from.mode = mode;
892 *inbytesleft -= insize;
893 *outbytesleft -= outsize;
900 make_csconv(const char *_name)
904 int use_compat = TRUE;
909 xstrlcpy(name, _name, sizeof(name));
911 /* check for option "enc_name//opt1//opt2" */
912 while ((p = strrstr(name, "//")) != NULL)
914 if (_stricmp(p + 2, "nocompat") == 0)
916 else if (_stricmp(p + 2, "translit") == 0)
917 flag |= FLAG_TRANSLIT;
918 else if (_stricmp(p + 2, "ignore") == 0)
928 cv.codepage = name_to_codepage(name);
929 if (cv.codepage == 1200 || cv.codepage == 1201)
931 cv.mbtowc = utf16_mbtowc;
932 cv.wctomb = utf16_wctomb;
933 if (_stricmp(name, "UTF-16") == 0 ||
934 _stricmp(name, "UTF16") == 0 ||
935 _stricmp(name, "UCS-2") == 0 ||
936 _stricmp(name, "UCS2") == 0)
937 cv.flags |= FLAG_USE_BOM_ENDIAN;
939 else if (cv.codepage == 12000 || cv.codepage == 12001)
941 cv.mbtowc = utf32_mbtowc;
942 cv.wctomb = utf32_wctomb;
943 if (_stricmp(name, "UTF-32") == 0 ||
944 _stricmp(name, "UTF32") == 0 ||
945 _stricmp(name, "UCS-4") == 0 ||
946 _stricmp(name, "UCS4") == 0)
947 cv.flags |= FLAG_USE_BOM_ENDIAN;
949 else if (cv.codepage == 65001)
951 cv.mbtowc = kernel_mbtowc;
952 cv.wctomb = kernel_wctomb;
953 cv.mblen = utf8_mblen;
955 else if ((cv.codepage == 50220 || cv.codepage == 50221 || cv.codepage == 50222) && load_mlang())
957 cv.mbtowc = iso2022jp_mbtowc;
958 cv.wctomb = iso2022jp_wctomb;
959 cv.flush = iso2022jp_flush;
961 else if (cv.codepage == 51932 && load_mlang())
963 cv.mbtowc = mlang_mbtowc;
964 cv.wctomb = mlang_wctomb;
965 cv.mblen = eucjp_mblen;
967 else if (IsValidCodePage(cv.codepage)
968 && GetCPInfoEx(cv.codepage, 0, &cpinfoex) != 0)
970 cv.mbtowc = kernel_mbtowc;
971 cv.wctomb = kernel_wctomb;
972 if (cpinfoex.MaxCharSize == 1)
973 cv.mblen = sbcs_mblen;
974 else if (cpinfoex.MaxCharSize == 2)
975 cv.mblen = dbcs_mblen;
977 cv.mblen = mbcs_mblen;
988 case 932: cv.compat = cp932_compat; break;
989 case 20932: cv.compat = cp20932_compat; break;
990 case 51932: cv.compat = cp51932_compat; break;
991 case 50220: case 50221: case 50222: cv.compat = cp5022x_compat; break;
998 name_to_codepage(const char *name)
1002 if (*name == '\0' ||
1003 strcmp(name, "char") == 0)
1005 else if (strcmp(name, "wchar_t") == 0)
1007 else if (_strnicmp(name, "cp", 2) == 0)
1008 return atoi(name + 2); /* CP123 */
1009 else if ('0' <= name[0] && name[0] <= '9')
1010 return atoi(name); /* 123 */
1011 else if (_strnicmp(name, "xx", 2) == 0)
1012 return atoi(name + 2); /* XX123 for debug */
1014 for (i = 0; codepage_alias[i].name != NULL; ++i)
1015 if (_stricmp(name, codepage_alias[i].name) == 0)
1016 return codepage_alias[i].codepage;
1021 * http://www.faqs.org/rfcs/rfc2781.html
1024 utf16_to_ucs4(const ushort *wbuf)
1027 if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
1028 wc = ((wbuf[0] & 0x3FF) << 10) + (wbuf[1] & 0x3FF) + 0x10000;
1033 ucs4_to_utf16(uint wc, ushort *wbuf, int *wbufsize)
1043 wbuf[0] = 0xD800 | ((wc >> 10) & 0x3FF);
1044 wbuf[1] = 0xDC00 | (wc & 0x3FF);
1050 is_unicode(int codepage)
1052 return (codepage == 1200 || codepage == 1201 ||
1053 codepage == 12000 || codepage == 12001 ||
1054 codepage == 65000 || codepage == 65001);
1058 * Check if codepage is one of those for which the dwFlags parameter
1059 * to MultiByteToWideChar() must be zero. Return zero or
1060 * MB_ERR_INVALID_CHARS. The docs in Platform SDK for for Windows
1061 * Server 2003 R2 claims that also codepage 65001 is one of these, but
1062 * that doesn't seem to be the case. The MSDN docs for MSVS2008 leave
1063 * out 65001 (UTF-8), and that indeed seems to be the case on XP, it
1064 * works fine to pass MB_ERR_INVALID_CHARS in dwFlags when converting
1068 mbtowc_flags(int codepage)
1070 return (codepage == 50220 || codepage == 50221 ||
1071 codepage == 50222 || codepage == 50225 ||
1072 codepage == 50227 || codepage == 50229 ||
1073 codepage == 52936 || codepage == 54936 ||
1074 (codepage >= 57002 && codepage <= 57011) ||
1075 codepage == 65000 || codepage == 42) ? 0 : MB_ERR_INVALID_CHARS;
1079 * Check if codepage is one those for which the lpUsedDefaultChar
1080 * parameter to WideCharToMultiByte() must be NULL. The docs in
1081 * Platform SDK for for Windows Server 2003 R2 claims that this is the
1082 * list below, while the MSDN docs for MSVS2008 claim that it is only
1083 * for 65000 (UTF-7) and 65001 (UTF-8). This time the earlier Platform
1084 * SDK seems to be correct, at least for XP.
1087 must_use_null_useddefaultchar(int codepage)
1089 return (codepage == 65000 || codepage == 65001 ||
1090 codepage == 50220 || codepage == 50221 ||
1091 codepage == 50222 || codepage == 50225 ||
1092 codepage == 50227 || codepage == 50229 ||
1093 codepage == 52936 || codepage == 54936 ||
1094 (codepage >= 57002 && codepage <= 57011) ||
1099 check_utf_bom(rec_iconv_t *cd, ushort *wbuf, int *wbufsize)
1101 /* If we have a BOM, trust it, despite what the caller said */
1102 if (wbuf[0] == 0xFFFE && (cd->from.flags & FLAG_USE_BOM_ENDIAN))
1104 /* swap endian: 1200 <-> 1201 or 12000 <-> 12001 */
1105 cd->from.codepage ^= 1;
1106 cd->from.mode |= UNICODE_MODE_SWAPPED;
1112 * Don't do this if "to" is Unicode,
1113 * except if "to" is UTF-8.
1115 if (wbuf[0] == 0xFEFF && (!is_unicode(cd->to.codepage) || cd->to.codepage == 65001))
1120 strrstr(const char *str, const char *token)
1122 int len = strlen(token);
1123 const char *p = str + strlen(str);
1126 if (p[0] == token[0] && strncmp(p, token, len) == 0)
1131 #if defined(USE_LIBICONV_DLL)
1133 libiconv_iconv_open(rec_iconv_t *cd, const char *fromcode, const char *tocode)
1135 HMODULE hlibiconv = NULL;
1136 HMODULE hmsvcrt = NULL;
1137 char dllname[_MAX_PATH];
1140 f_iconv_open _iconv_open;
1143 * always try to load dll, so that we can switch dll in runtime.
1146 /* XXX: getenv() can't get variable set by SetEnvironmentVariable() */
1147 p = getenv("WINICONV_LIBICONV_DLL");
1149 p = DEFAULT_LIBICONV_DLL;
1150 /* parse comma separated value */
1151 for ( ; *p != 0; p = (*e == ',') ? e + 1 : e)
1158 xstrlcpyn(dllname, p, e - p, sizeof(dllname));
1159 hlibiconv = LoadLibrary(dllname);
1160 if (hlibiconv != NULL)
1162 if (hlibiconv == hwiniconv)
1164 FreeLibrary(hlibiconv);
1172 if (hlastdll != NULL)
1174 /* decrement reference count */
1175 FreeLibrary(hlastdll);
1179 if (hlibiconv == NULL)
1182 hmsvcrt = find_imported_module_by_funcname(hlibiconv, "_errno");
1183 if (hmsvcrt == NULL)
1186 _iconv_open = (f_iconv_open)GetProcAddress(hlibiconv, "libiconv_open");
1187 if (_iconv_open == NULL)
1188 _iconv_open = (f_iconv_open)GetProcAddress(hlibiconv, "iconv_open");
1189 cd->iconv_close = (f_iconv_close)GetProcAddress(hlibiconv, "libiconv_close");
1190 if (cd->iconv_close == NULL)
1191 cd->iconv_close = (f_iconv_close)GetProcAddress(hlibiconv, "iconv_close");
1192 cd->iconv = (f_iconv)GetProcAddress(hlibiconv, "libiconv");
1193 if (cd->iconv == NULL)
1194 cd->iconv = (f_iconv)GetProcAddress(hlibiconv, "iconv");
1195 cd->_errno = (f_errno)GetProcAddress(hmsvcrt, "_errno");
1196 if (_iconv_open == NULL || cd->iconv_close == NULL
1197 || cd->iconv == NULL || cd->_errno == NULL)
1200 /* increment reference count */
1201 hlastdll = LoadLibrary(dllname);
1203 cd->cd = _iconv_open(tocode, fromcode);
1204 if (cd->cd == (iconv_t)(-1))
1207 cd->hlibiconv = hlibiconv;
1211 if (hlibiconv != NULL)
1212 FreeLibrary(hlibiconv);
1213 /* do not free hmsvcrt which is obtained by GetModuleHandle() */
1219 * http://forums.belution.com/ja/vc/000/234/78s.shtml
1220 * http://nienie.com/~masapico/api_ImageDirectoryEntryToData.html
1223 * imagehlp.h or dbghelp.h
1224 * imagehlp.lib or dbghelp.lib
1225 * ImageDirectoryEntryToData()
1227 #define TO_DOS_HEADER(base) ((PIMAGE_DOS_HEADER)(base))
1228 #define TO_NT_HEADERS(base) ((PIMAGE_NT_HEADERS)((LPBYTE)(base) + TO_DOS_HEADER(base)->e_lfanew))
1230 MyImageDirectoryEntryToData(LPVOID Base, BOOLEAN MappedAsImage, USHORT DirectoryEntry, PULONG Size)
1232 /* TODO: MappedAsImage? */
1233 PIMAGE_DATA_DIRECTORY p;
1234 p = TO_NT_HEADERS(Base)->OptionalHeader.DataDirectory + DirectoryEntry;
1235 if (p->VirtualAddress == 0) {
1240 return (PVOID)((LPBYTE)Base + p->VirtualAddress);
1244 find_imported_module_by_funcname(HMODULE hModule, const char *funcname)
1248 PIMAGE_IMPORT_DESCRIPTOR Imp;
1249 PIMAGE_THUNK_DATA Name; /* Import Name Table */
1250 PIMAGE_IMPORT_BY_NAME ImpName;
1252 Base = (DWORD)hModule;
1253 Imp = MyImageDirectoryEntryToData(
1256 IMAGE_DIRECTORY_ENTRY_IMPORT,
1260 for ( ; Imp->OriginalFirstThunk != 0; ++Imp)
1262 Name = (PIMAGE_THUNK_DATA)(Base + Imp->OriginalFirstThunk);
1263 for ( ; Name->u1.Ordinal != 0; ++Name)
1265 if (!IMAGE_SNAP_BY_ORDINAL(Name->u1.Ordinal))
1267 ImpName = (PIMAGE_IMPORT_BY_NAME)
1268 (Base + (DWORD)Name->u1.AddressOfData);
1269 if (strcmp((char *)ImpName->Name, funcname) == 0)
1270 return GetModuleHandle((char *)(Base + Imp->Name));
1279 sbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1285 dbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1287 int len = IsDBCSLeadByteEx(cv->codepage, buf[0]) ? 2 : 1;
1289 return_error(EINVAL);
1294 mbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1298 if (cv->codepage == 54936) {
1299 if (buf[0] <= 0x7F) len = 1;
1300 else if (buf[0] >= 0x81 && buf[0] <= 0xFE &&
1302 ((buf[1] >= 0x40 && buf[1] <= 0x7E) ||
1303 (buf[1] >= 0x80 && buf[1] <= 0xFE))) len = 2;
1304 else if (buf[0] >= 0x81 && buf[0] <= 0xFE &&
1306 buf[1] >= 0x30 && buf[1] <= 0x39) len = 4;
1308 return_error(EINVAL);
1312 return_error(EINVAL);
1316 utf8_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1320 if (buf[0] < 0x80) len = 1;
1321 else if ((buf[0] & 0xE0) == 0xC0) len = 2;
1322 else if ((buf[0] & 0xF0) == 0xE0) len = 3;
1323 else if ((buf[0] & 0xF8) == 0xF0) len = 4;
1324 else if ((buf[0] & 0xFC) == 0xF8) len = 5;
1325 else if ((buf[0] & 0xFE) == 0xFC) len = 6;
1328 return_error(EILSEQ);
1329 else if (bufsize < len)
1330 return_error(EINVAL);
1335 eucjp_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1337 if (buf[0] < 0x80) /* ASCII */
1339 else if (buf[0] == 0x8E) /* JIS X 0201 */
1342 return_error(EINVAL);
1343 else if (!(0xA1 <= buf[1] && buf[1] <= 0xDF))
1344 return_error(EILSEQ);
1347 else if (buf[0] == 0x8F) /* JIS X 0212 */
1350 return_error(EINVAL);
1351 else if (!(0xA1 <= buf[1] && buf[1] <= 0xFE)
1352 || !(0xA1 <= buf[2] && buf[2] <= 0xFE))
1353 return_error(EILSEQ);
1356 else /* JIS X 0208 */
1359 return_error(EINVAL);
1360 else if (!(0xA1 <= buf[0] && buf[0] <= 0xFE)
1361 || !(0xA1 <= buf[1] && buf[1] <= 0xFE))
1362 return_error(EILSEQ);
1368 kernel_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1372 len = cv->mblen(cv, buf, bufsize);
1375 /* If converting from ASCII, reject 8bit
1376 * chars. MultiByteToWideChar() doesn't. Note that for ASCII we
1377 * know that the mblen function is sbcs_mblen() so len is 1.
1379 if (cv->codepage == 20127 && buf[0] >= 0x80)
1380 return_error(EILSEQ);
1381 *wbufsize = MultiByteToWideChar(cv->codepage, mbtowc_flags (cv->codepage),
1382 (const char *)buf, len, (wchar_t *)wbuf, *wbufsize);
1384 return_error(EILSEQ);
1389 kernel_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1391 BOOL usedDefaultChar = 0;
1397 return_error(E2BIG);
1398 if (!must_use_null_useddefaultchar(cv->codepage))
1400 p = &usedDefaultChar;
1401 #ifdef WC_NO_BEST_FIT_CHARS
1402 if (!(cv->flags & FLAG_TRANSLIT))
1403 flags |= WC_NO_BEST_FIT_CHARS;
1406 len = WideCharToMultiByte(cv->codepage, flags,
1407 (const wchar_t *)wbuf, wbufsize, (char *)buf, bufsize, NULL, p);
1410 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1411 return_error(E2BIG);
1412 return_error(EILSEQ);
1414 else if (usedDefaultChar)
1415 return_error(EILSEQ);
1416 else if (cv->mblen(cv, buf, len) != len) /* validate result */
1417 return_error(EILSEQ);
1422 * It seems that the mode (cv->mode) is fixnum.
1423 * For example, when converting iso-2022-jp(cp50221) to unicode:
1424 * in ascii sequence: mode=0xC42C0000
1425 * in jisx0208 sequence: mode=0xC42C0001
1426 * "C42C" is same for each convert session.
1427 * It should be: ((codepage-1)<<16)|state
1430 mlang_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1436 len = cv->mblen(cv, buf, bufsize);
1440 hr = ConvertINetMultiByteToUnicode(&cv->mode, cv->codepage,
1441 (const char *)buf, &insize, (wchar_t *)wbuf, wbufsize);
1442 if (hr != S_OK || insize != len)
1443 return_error(EILSEQ);
1448 mlang_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1450 char tmpbuf[MB_CHAR_MAX]; /* enough room for one character */
1451 int tmpsize = MB_CHAR_MAX;
1452 int insize = wbufsize;
1455 hr = ConvertINetUnicodeToMultiByte(&cv->mode, cv->codepage,
1456 (const wchar_t *)wbuf, &wbufsize, tmpbuf, &tmpsize);
1457 if (hr != S_OK || insize != wbufsize)
1458 return_error(EILSEQ);
1459 else if (bufsize < tmpsize)
1460 return_error(E2BIG);
1461 else if (cv->mblen(cv, (uchar *)tmpbuf, tmpsize) != tmpsize)
1462 return_error(EILSEQ);
1463 memcpy(buf, tmpbuf, tmpsize);
1468 utf16_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1471 return_error(EINVAL);
1472 if (cv->codepage == 1200) /* little endian */
1473 wbuf[0] = (buf[1] << 8) | buf[0];
1474 else if (cv->codepage == 1201) /* big endian */
1475 wbuf[0] = (buf[0] << 8) | buf[1];
1476 if (0xDC00 <= wbuf[0] && wbuf[0] <= 0xDFFF)
1477 return_error(EILSEQ);
1478 if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
1481 return_error(EINVAL);
1482 if (cv->codepage == 1200) /* little endian */
1483 wbuf[1] = (buf[3] << 8) | buf[2];
1484 else if (cv->codepage == 1201) /* big endian */
1485 wbuf[1] = (buf[2] << 8) | buf[3];
1486 if (!(0xDC00 <= wbuf[1] && wbuf[1] <= 0xDFFF))
1487 return_error(EILSEQ);
1496 utf16_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1499 return_error(E2BIG);
1500 if (cv->codepage == 1200) /* little endian */
1502 buf[0] = (wbuf[0] & 0x00FF);
1503 buf[1] = (wbuf[0] & 0xFF00) >> 8;
1505 else if (cv->codepage == 1201) /* big endian */
1507 buf[0] = (wbuf[0] & 0xFF00) >> 8;
1508 buf[1] = (wbuf[0] & 0x00FF);
1510 if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
1513 return_error(E2BIG);
1514 if (cv->codepage == 1200) /* little endian */
1516 buf[2] = (wbuf[1] & 0x00FF);
1517 buf[3] = (wbuf[1] & 0xFF00) >> 8;
1519 else if (cv->codepage == 1201) /* big endian */
1521 buf[2] = (wbuf[1] & 0xFF00) >> 8;
1522 buf[3] = (wbuf[1] & 0x00FF);
1530 utf32_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1535 return_error(EINVAL);
1536 if (cv->codepage == 12000) /* little endian */
1537 wc = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
1538 else if (cv->codepage == 12001) /* big endian */
1539 wc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1540 if ((0xD800 <= wc && wc <= 0xDFFF) || 0x10FFFF < wc)
1541 return_error(EILSEQ);
1542 ucs4_to_utf16(wc, wbuf, wbufsize);
1547 utf32_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1552 return_error(E2BIG);
1553 wc = utf16_to_ucs4(wbuf);
1554 if (cv->codepage == 12000) /* little endian */
1556 buf[0] = wc & 0x000000FF;
1557 buf[1] = (wc & 0x0000FF00) >> 8;
1558 buf[2] = (wc & 0x00FF0000) >> 16;
1559 buf[3] = (wc & 0xFF000000) >> 24;
1561 else if (cv->codepage == 12001) /* big endian */
1563 buf[0] = (wc & 0xFF000000) >> 24;
1564 buf[1] = (wc & 0x00FF0000) >> 16;
1565 buf[2] = (wc & 0x0000FF00) >> 8;
1566 buf[3] = wc & 0x000000FF;
1572 * 50220: ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS)
1573 * 50221: ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow
1575 * 50222: ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte
1578 * MultiByteToWideChar() and WideCharToMultiByte() behave differently
1579 * depending on Windows version. On XP, WideCharToMultiByte() doesn't
1580 * terminate result sequence with ascii escape. But Vista does.
1581 * Use MLang instead.
1584 #define ISO2022_MODE(cs, shift) (((cs) << 8) | (shift))
1585 #define ISO2022_MODE_CS(mode) (((mode) >> 8) & 0xFF)
1586 #define ISO2022_MODE_SHIFT(mode) ((mode) & 0xFF)
1588 #define ISO2022_SI 0
1589 #define ISO2022_SO 1
1592 static const char iso2022_SI_seq[] = "\x0F";
1594 static const char iso2022_SO_seq[] = "\x0E";
1596 typedef struct iso2022_esc_t iso2022_esc_t;
1597 struct iso2022_esc_t {
1604 #define ISO2022JP_CS_ASCII 0
1605 #define ISO2022JP_CS_JISX0201_ROMAN 1
1606 #define ISO2022JP_CS_JISX0201_KANA 2
1607 #define ISO2022JP_CS_JISX0208_1978 3
1608 #define ISO2022JP_CS_JISX0208_1983 4
1609 #define ISO2022JP_CS_JISX0212 5
1611 static iso2022_esc_t iso2022jp_esc[] = {
1612 {"\x1B\x28\x42", 3, 1, ISO2022JP_CS_ASCII},
1613 {"\x1B\x28\x4A", 3, 1, ISO2022JP_CS_JISX0201_ROMAN},
1614 {"\x1B\x28\x49", 3, 1, ISO2022JP_CS_JISX0201_KANA},
1615 {"\x1B\x24\x40", 3, 2, ISO2022JP_CS_JISX0208_1983}, /* unify 1978 with 1983 */
1616 {"\x1B\x24\x42", 3, 2, ISO2022JP_CS_JISX0208_1983},
1617 {"\x1B\x24\x28\x44", 4, 2, ISO2022JP_CS_JISX0212},
1622 iso2022jp_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1624 iso2022_esc_t *iesc = iso2022jp_esc;
1625 char tmp[MB_CHAR_MAX];
1637 for (i = 0; iesc[i].esc != NULL; ++i)
1639 esc_len = iesc[i].esc_len;
1640 if (bufsize < esc_len)
1642 if (strncmp((char *)buf, iesc[i].esc, bufsize) == 0)
1643 return_error(EINVAL);
1647 if (strncmp((char *)buf, iesc[i].esc, esc_len) == 0)
1649 cv->mode = ISO2022_MODE(iesc[i].cs, ISO2022_SI);
1655 /* not supported escape sequence */
1656 return_error(EILSEQ);
1658 else if (buf[0] == iso2022_SO_seq[0])
1660 cv->mode = ISO2022_MODE(ISO2022_MODE_CS(cv->mode), ISO2022_SO);
1664 else if (buf[0] == iso2022_SI_seq[0])
1666 cv->mode = ISO2022_MODE(ISO2022_MODE_CS(cv->mode), ISO2022_SI);
1671 cs = ISO2022_MODE_CS(cv->mode);
1672 shift = ISO2022_MODE_SHIFT(cv->mode);
1674 /* reset the mode for informal sequence */
1677 cs = ISO2022JP_CS_ASCII;
1683 return_error(EINVAL);
1684 for (i = 0; i < len; ++i)
1685 if (!(buf[i] < 0x80))
1686 return_error(EILSEQ);
1687 esc_len = iesc[cs].esc_len;
1688 memcpy(tmp, iesc[cs].esc, esc_len);
1689 if (shift == ISO2022_SO)
1691 memcpy(tmp + esc_len, iso2022_SO_seq, 1);
1694 memcpy(tmp + esc_len, buf, len);
1696 if ((cv->codepage == 50220 || cv->codepage == 50221
1697 || cv->codepage == 50222) && shift == ISO2022_SO)
1699 /* XXX: shift-out cannot be used for mbtowc (both kernel and
1701 esc_len = iesc[ISO2022JP_CS_JISX0201_KANA].esc_len;
1702 memcpy(tmp, iesc[ISO2022JP_CS_JISX0201_KANA].esc, esc_len);
1703 memcpy(tmp + esc_len, buf, len);
1706 insize = len + esc_len;
1707 hr = ConvertINetMultiByteToUnicode(&dummy, cv->codepage,
1708 (const char *)tmp, &insize, (wchar_t *)wbuf, wbufsize);
1709 if (hr != S_OK || insize != len + esc_len)
1710 return_error(EILSEQ);
1712 /* Check for conversion error. Assuming defaultChar is 0x3F. */
1713 /* ascii should be converted from ascii */
1714 if (wbuf[0] == buf[0]
1715 && cv->mode != ISO2022_MODE(ISO2022JP_CS_ASCII, ISO2022_SI))
1716 return_error(EILSEQ);
1718 /* reset the mode for informal sequence */
1719 if (cv->mode != ISO2022_MODE(cs, shift))
1720 cv->mode = ISO2022_MODE(cs, shift);
1726 iso2022jp_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1728 iso2022_esc_t *iesc = iso2022jp_esc;
1729 char tmp[MB_CHAR_MAX];
1730 int tmpsize = MB_CHAR_MAX;
1731 int insize = wbufsize;
1741 * MultiByte = [escape sequence] + character + [escape sequence]
1743 * Whether trailing escape sequence is added depends on which API is
1744 * used (kernel or MLang, and its version).
1746 hr = ConvertINetUnicodeToMultiByte(&dummy, cv->codepage,
1747 (const wchar_t *)wbuf, &wbufsize, tmp, &tmpsize);
1748 if (hr != S_OK || insize != wbufsize)
1749 return_error(EILSEQ);
1750 else if (bufsize < tmpsize)
1751 return_error(E2BIG);
1755 cs = ISO2022JP_CS_ASCII;
1760 for (i = 1; iesc[i].esc != NULL; ++i)
1762 esc_len = iesc[i].esc_len;
1763 if (strncmp(tmp, iesc[i].esc, esc_len) == 0)
1769 if (iesc[i].esc == NULL)
1770 /* not supported escape sequence */
1771 return_error(EILSEQ);
1775 if (tmp[esc_len] == iso2022_SO_seq[0])
1783 /* Check for converting error. Assuming defaultChar is 0x3F. */
1784 /* ascii should be converted from ascii */
1785 if (cs == ISO2022JP_CS_ASCII && !(wbuf[0] < 0x80))
1786 return_error(EILSEQ);
1787 else if (tmpsize < esc_len + len)
1788 return_error(EILSEQ);
1790 if (cv->mode == ISO2022_MODE(cs, shift))
1792 /* remove escape sequence */
1794 memmove(tmp, tmp + esc_len, len);
1799 if (cs == ISO2022JP_CS_ASCII)
1801 esc_len = iesc[ISO2022JP_CS_ASCII].esc_len;
1802 memmove(tmp + esc_len, tmp, len);
1803 memcpy(tmp, iesc[ISO2022JP_CS_ASCII].esc, esc_len);
1805 if (ISO2022_MODE_SHIFT(cv->mode) == ISO2022_SO)
1807 /* shift-in before changing to other mode */
1808 memmove(tmp + 1, tmp, len + esc_len);
1809 memcpy(tmp, iso2022_SI_seq, 1);
1814 if (bufsize < len + esc_len)
1815 return_error(E2BIG);
1816 memcpy(buf, tmp, len + esc_len);
1817 cv->mode = ISO2022_MODE(cs, shift);
1818 return len + esc_len;
1822 iso2022jp_flush(csconv_t *cv, uchar *buf, int bufsize)
1824 iso2022_esc_t *iesc = iso2022jp_esc;
1827 if (cv->mode != ISO2022_MODE(ISO2022JP_CS_ASCII, ISO2022_SI))
1830 if (ISO2022_MODE_SHIFT(cv->mode) != ISO2022_SI)
1832 if (ISO2022_MODE_CS(cv->mode) != ISO2022JP_CS_ASCII)
1833 esc_len += iesc[ISO2022JP_CS_ASCII].esc_len;
1834 if (bufsize < esc_len)
1835 return_error(E2BIG);
1838 if (ISO2022_MODE_SHIFT(cv->mode) != ISO2022_SI)
1840 memcpy(buf, iso2022_SI_seq, 1);
1843 if (ISO2022_MODE_CS(cv->mode) != ISO2022JP_CS_ASCII)
1845 memcpy(buf + esc_len, iesc[ISO2022JP_CS_ASCII].esc,
1846 iesc[ISO2022JP_CS_ASCII].esc_len);
1847 esc_len += iesc[ISO2022JP_CS_ASCII].esc_len;
1854 #if defined(MAKE_DLL) && defined(USE_LIBICONV_DLL)
1856 DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
1860 case DLL_PROCESS_ATTACH:
1861 hwiniconv = (HMODULE)hinstDLL;
1863 case DLL_THREAD_ATTACH:
1864 case DLL_THREAD_DETACH:
1865 case DLL_PROCESS_DETACH:
1872 #if defined(MAKE_EXE)
1877 main(int argc, char **argv)
1879 char *fromcode = NULL;
1880 char *tocode = NULL;
1883 char outbuf[BUFSIZ];
1887 size_t outbytesleft;
1893 _setmode(_fileno(stdin), _O_BINARY);
1894 _setmode(_fileno(stdout), _O_BINARY);
1896 for (i = 1; i < argc; ++i)
1898 if (strcmp(argv[i], "-l") == 0)
1900 for (i = 0; codepage_alias[i].name != NULL; ++i)
1901 printf("%s\n", codepage_alias[i].name);
1905 if (strcmp(argv[i], "-f") == 0)
1906 fromcode = argv[++i];
1907 else if (strcmp(argv[i], "-t") == 0)
1911 in = fopen(argv[i], "rb");
1914 fprintf(stderr, "cannot open %s\n", argv[i]);
1921 if (fromcode == NULL || tocode == NULL)
1923 printf("usage: %s -f from-enc -t to-enc [file]\n", argv[0]);
1927 cd = iconv_open(tocode, fromcode);
1928 if (cd == (iconv_t)(-1))
1930 perror("iconv_open error");
1934 while ((inbytesleft = fread(inbuf + rest, 1, sizeof(inbuf) - rest, in)) != 0
1937 inbytesleft += rest;
1940 outbytesleft = sizeof(outbuf);
1941 r = iconv(cd, &pin, &inbytesleft, &pout, &outbytesleft);
1942 fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, stdout);
1943 if (r == (size_t)(-1) && errno != EINVAL && errno != E2BIG)
1945 perror("conversion error");
1948 memmove(inbuf, pin, inbytesleft);
1952 outbytesleft = sizeof(outbuf);
1953 r = iconv(cd, NULL, NULL, &pout, &outbytesleft);
1954 fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, stdout);
1955 if (r == (size_t)(-1))
1957 perror("conversion error");