forked from win-iconv/win-iconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin_iconv.c
2079 lines (1876 loc) · 62.6 KB
/
win_iconv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* iconv implementation using Win32 API to convert.
*
* This file is placed in the public domain.
*/
/* for WC_NO_BEST_FIT_CHARS */
#ifndef WINVER
# define WINVER 0x0500
#endif
#define STRICT
#include <windows.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#ifdef __GNUC__
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif
/* WORKAROUND: */
#ifndef UNDER_CE
#define GetProcAddressA GetProcAddress
#endif
#if 0
# define MAKE_EXE
# define MAKE_DLL
# define USE_LIBICONV_DLL
#endif
#if !defined(DEFAULT_LIBICONV_DLL)
# define DEFAULT_LIBICONV_DLL ""
#endif
#define MB_CHAR_MAX 16
#define UNICODE_MODE_BOM_DONE 1
#define UNICODE_MODE_SWAPPED 2
#define FLAG_USE_BOM 1
#define FLAG_TRANSLIT 2 /* //TRANSLIT */
#define FLAG_IGNORE 4 /* //IGNORE */
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef void* iconv_t;
iconv_t iconv_open(const char *tocode, const char *fromcode);
int iconv_close(iconv_t cd);
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
/* libiconv interface for vim */
#if defined(MAKE_DLL)
int
iconvctl (iconv_t cd, int request, void* argument)
{
/* not supported */
return 0;
}
#endif
typedef struct compat_t compat_t;
typedef struct csconv_t csconv_t;
typedef struct rec_iconv_t rec_iconv_t;
typedef iconv_t (*f_iconv_open)(const char *tocode, const char *fromcode);
typedef int (*f_iconv_close)(iconv_t cd);
typedef size_t (*f_iconv)(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
typedef int* (*f_errno)(void);
typedef int (*f_mbtowc)(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
typedef int (*f_wctomb)(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
typedef int (*f_mblen)(csconv_t *cv, const uchar *buf, int bufsize);
typedef int (*f_flush)(csconv_t *cv, uchar *buf, int bufsize);
#define COMPAT_IN 1
#define COMPAT_OUT 2
/* unicode mapping for compatibility with other conversion table. */
struct compat_t {
uint in;
uint out;
uint flag;
};
struct csconv_t {
int codepage;
int flags;
f_mbtowc mbtowc;
f_wctomb wctomb;
f_mblen mblen;
f_flush flush;
DWORD mode;
compat_t *compat;
};
struct rec_iconv_t {
iconv_t cd;
f_iconv_close iconv_close;
f_iconv iconv;
f_errno _errno;
csconv_t from;
csconv_t to;
#if defined(USE_LIBICONV_DLL)
HMODULE hlibiconv;
#endif
};
static int win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
static int win_iconv_close(iconv_t cd);
static size_t win_iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
static int load_mlang(void);
static int make_csconv(const char *name, csconv_t *cv);
static int name_to_codepage(const char *name);
static uint utf16_to_ucs4(const ushort *wbuf);
static void ucs4_to_utf16(uint wc, ushort *wbuf, int *wbufsize);
static int mbtowc_flags(int codepage);
static int must_use_null_useddefaultchar(int codepage);
static char *strrstr(const char *str, const char *token);
static char *xstrndup(const char *s, size_t n);
static int seterror(int err);
#if defined(USE_LIBICONV_DLL)
static int libiconv_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
static PVOID MyImageDirectoryEntryToData(LPVOID Base, BOOLEAN MappedAsImage, USHORT DirectoryEntry, PULONG Size);
static FARPROC find_imported_function(HMODULE hModule, const char *funcname);
static HMODULE hwiniconv;
#endif
static int sbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
static int dbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
static int mbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
static int utf8_mblen(csconv_t *cv, const uchar *buf, int bufsize);
static int eucjp_mblen(csconv_t *cv, const uchar *buf, int bufsize);
static int kernel_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
static int kernel_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
static int mlang_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
static int mlang_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
static int utf16_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
static int utf16_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
static int utf32_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
static int utf32_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
static int iso2022jp_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
static int iso2022jp_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
static int iso2022jp_flush(csconv_t *cv, uchar *buf, int bufsize);
static struct {
int codepage;
const char *name;
} codepage_alias[] = {
{65001, "CP65001"},
{65001, "UTF8"},
{65001, "UTF-8"},
{1200, "CP1200"},
{1200, "UTF16LE"},
{1200, "UTF-16LE"},
{1200, "UCS2LE"},
{1200, "UCS-2LE"},
{1200, "UCS-2-INTERNAL"},
{1201, "CP1201"},
{1201, "UTF16BE"},
{1201, "UTF-16BE"},
{1201, "UCS2BE"},
{1201, "UCS-2BE"},
{1201, "unicodeFFFE"},
{12000, "CP12000"},
{12000, "UTF32LE"},
{12000, "UTF-32LE"},
{12000, "UCS4LE"},
{12000, "UCS-4LE"},
{12001, "CP12001"},
{12001, "UTF32BE"},
{12001, "UTF-32BE"},
{12001, "UCS4BE"},
{12001, "UCS-4BE"},
#ifndef GLIB_COMPILATION
/*
* Default is big endian.
* See rfc2781 4.3 Interpreting text labelled as UTF-16.
*/
{1201, "UTF16"},
{1201, "UTF-16"},
{1201, "UCS2"},
{1201, "UCS-2"},
{12001, "UTF32"},
{12001, "UTF-32"},
{12001, "UCS-4"},
{12001, "UCS4"},
#else
/* Default is little endian, because the platform is */
{1200, "UTF16"},
{1200, "UTF-16"},
{1200, "UCS2"},
{1200, "UCS-2"},
{12000, "UTF32"},
{12000, "UTF-32"},
{12000, "UCS4"},
{12000, "UCS-4"},
#endif
/* copy from libiconv `iconv -l` */
/* !IsValidCodePage(367) */
{20127, "ANSI_X3.4-1968"},
{20127, "ANSI_X3.4-1986"},
{20127, "ASCII"},
{20127, "CP367"},
{20127, "IBM367"},
{20127, "ISO-IR-6"},
{20127, "ISO646-US"},
{20127, "ISO_646.IRV:1991"},
{20127, "US"},
{20127, "US-ASCII"},
{20127, "CSASCII"},
/* !IsValidCodePage(819) */
{1252, "CP819"},
{1252, "IBM819"},
{28591, "ISO-8859-1"},
{28591, "ISO-IR-100"},
{28591, "ISO8859-1"},
{28591, "ISO_8859-1"},
{28591, "ISO_8859-1:1987"},
{28591, "L1"},
{28591, "LATIN1"},
{28591, "CSISOLATIN1"},
{1250, "CP1250"},
{1250, "MS-EE"},
{1250, "WINDOWS-1250"},
{1251, "CP1251"},
{1251, "MS-CYRL"},
{1251, "WINDOWS-1251"},
{1252, "CP1252"},
{1252, "MS-ANSI"},
{1252, "WINDOWS-1252"},
{1253, "CP1253"},
{1253, "MS-GREEK"},
{1253, "WINDOWS-1253"},
{1254, "CP1254"},
{1254, "MS-TURK"},
{1254, "WINDOWS-1254"},
{1255, "CP1255"},
{1255, "MS-HEBR"},
{1255, "WINDOWS-1255"},
{1256, "CP1256"},
{1256, "MS-ARAB"},
{1256, "WINDOWS-1256"},
{1257, "CP1257"},
{1257, "WINBALTRIM"},
{1257, "WINDOWS-1257"},
{1258, "CP1258"},
{1258, "WINDOWS-1258"},
{850, "850"},
{850, "CP850"},
{850, "IBM850"},
{850, "CSPC850MULTILINGUAL"},
/* !IsValidCodePage(862) */
{862, "862"},
{862, "CP862"},
{862, "IBM862"},
{862, "CSPC862LATINHEBREW"},
{866, "866"},
{866, "CP866"},
{866, "IBM866"},
{866, "CSIBM866"},
/* !IsValidCodePage(154) */
{154, "CP154"},
{154, "CYRILLIC-ASIAN"},
{154, "PT154"},
{154, "PTCP154"},
{154, "CSPTCP154"},
/* !IsValidCodePage(1133) */
{1133, "CP1133"},
{1133, "IBM-CP1133"},
{874, "CP874"},
{874, "WINDOWS-874"},
/* !IsValidCodePage(51932) */
{51932, "CP51932"},
{51932, "MS51932"},
{51932, "WINDOWS-51932"},
{51932, "EUC-JP"},
{932, "CP932"},
{932, "MS932"},
{932, "SHIFFT_JIS"},
{932, "SHIFFT_JIS-MS"},
{932, "SJIS"},
{932, "SJIS-MS"},
{932, "SJIS-OPEN"},
{932, "SJIS-WIN"},
{932, "WINDOWS-31J"},
{932, "WINDOWS-932"},
{932, "CSWINDOWS31J"},
{50221, "CP50221"},
{50221, "ISO-2022-JP"},
{50221, "ISO-2022-JP-MS"},
{50221, "ISO2022-JP"},
{50221, "ISO2022-JP-MS"},
{50221, "MS50221"},
{50221, "WINDOWS-50221"},
{936, "CP936"},
{936, "GBK"},
{936, "MS936"},
{936, "WINDOWS-936"},
{950, "CP950"},
{950, "BIG5"},
{950, "BIG5HKSCS"},
{950, "BIG5-HKSCS"},
{949, "CP949"},
{949, "UHC"},
{949, "EUC-KR"},
{1361, "CP1361"},
{1361, "JOHAB"},
{437, "437"},
{437, "CP437"},
{437, "IBM437"},
{437, "CSPC8CODEPAGE437"},
{737, "CP737"},
{775, "CP775"},
{775, "IBM775"},
{775, "CSPC775BALTIC"},
{852, "852"},
{852, "CP852"},
{852, "IBM852"},
{852, "CSPCP852"},
/* !IsValidCodePage(853) */
{853, "CP853"},
{855, "855"},
{855, "CP855"},
{855, "IBM855"},
{855, "CSIBM855"},
{857, "857"},
{857, "CP857"},
{857, "IBM857"},
{857, "CSIBM857"},
/* !IsValidCodePage(858) */
{858, "CP858"},
{860, "860"},
{860, "CP860"},
{860, "IBM860"},
{860, "CSIBM860"},
{861, "861"},
{861, "CP-IS"},
{861, "CP861"},
{861, "IBM861"},
{861, "CSIBM861"},
{863, "863"},
{863, "CP863"},
{863, "IBM863"},
{863, "CSIBM863"},
{864, "CP864"},
{864, "IBM864"},
{864, "CSIBM864"},
{865, "865"},
{865, "CP865"},
{865, "IBM865"},
{865, "CSIBM865"},
{869, "869"},
{869, "CP-GR"},
{869, "CP869"},
{869, "IBM869"},
{869, "CSIBM869"},
/* !IsValidCodePage(1152) */
{1125, "CP1125"},
/*
* Code Page Identifiers
* http://msdn2.microsoft.com/en-us/library/ms776446.aspx
*/
{37, "IBM037"}, /* IBM EBCDIC US-Canada */
{437, "IBM437"}, /* OEM United States */
{500, "IBM500"}, /* IBM EBCDIC International */
{708, "ASMO-708"}, /* Arabic (ASMO 708) */
/* 709 Arabic (ASMO-449+, BCON V4) */
/* 710 Arabic - Transparent Arabic */
{720, "DOS-720"}, /* Arabic (Transparent ASMO); Arabic (DOS) */
{737, "ibm737"}, /* OEM Greek (formerly 437G); Greek (DOS) */
{775, "ibm775"}, /* OEM Baltic; Baltic (DOS) */
{850, "ibm850"}, /* OEM Multilingual Latin 1; Western European (DOS) */
{852, "ibm852"}, /* OEM Latin 2; Central European (DOS) */
{855, "IBM855"}, /* OEM Cyrillic (primarily Russian) */
{857, "ibm857"}, /* OEM Turkish; Turkish (DOS) */
{858, "IBM00858"}, /* OEM Multilingual Latin 1 + Euro symbol */
{860, "IBM860"}, /* OEM Portuguese; Portuguese (DOS) */
{861, "ibm861"}, /* OEM Icelandic; Icelandic (DOS) */
{862, "DOS-862"}, /* OEM Hebrew; Hebrew (DOS) */
{863, "IBM863"}, /* OEM French Canadian; French Canadian (DOS) */
{864, "IBM864"}, /* OEM Arabic; Arabic (864) */
{865, "IBM865"}, /* OEM Nordic; Nordic (DOS) */
{866, "cp866"}, /* OEM Russian; Cyrillic (DOS) */
{869, "ibm869"}, /* OEM Modern Greek; Greek, Modern (DOS) */
{870, "IBM870"}, /* IBM EBCDIC Multilingual/ROECE (Latin 2); IBM EBCDIC Multilingual Latin 2 */
{874, "windows-874"}, /* ANSI/OEM Thai (same as 28605, ISO 8859-15); Thai (Windows) */
{875, "cp875"}, /* IBM EBCDIC Greek Modern */
{932, "shift_jis"}, /* ANSI/OEM Japanese; Japanese (Shift-JIS) */
{932, "shift-jis"}, /* alternative name for it */
{936, "gb2312"}, /* ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312) */
{949, "ks_c_5601-1987"}, /* ANSI/OEM Korean (Unified Hangul Code) */
{950, "big5"}, /* ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5) */
{950, "big5hkscs"}, /* ANSI/OEM Traditional Chinese (Hong Kong SAR); Chinese Traditional (Big5-HKSCS) */
{950, "big5-hkscs"}, /* alternative name for it */
{1026, "IBM1026"}, /* IBM EBCDIC Turkish (Latin 5) */
{1047, "IBM01047"}, /* IBM EBCDIC Latin 1/Open System */
{1140, "IBM01140"}, /* IBM EBCDIC US-Canada (037 + Euro symbol); IBM EBCDIC (US-Canada-Euro) */
{1141, "IBM01141"}, /* IBM EBCDIC Germany (20273 + Euro symbol); IBM EBCDIC (Germany-Euro) */
{1142, "IBM01142"}, /* IBM EBCDIC Denmark-Norway (20277 + Euro symbol); IBM EBCDIC (Denmark-Norway-Euro) */
{1143, "IBM01143"}, /* IBM EBCDIC Finland-Sweden (20278 + Euro symbol); IBM EBCDIC (Finland-Sweden-Euro) */
{1144, "IBM01144"}, /* IBM EBCDIC Italy (20280 + Euro symbol); IBM EBCDIC (Italy-Euro) */
{1145, "IBM01145"}, /* IBM EBCDIC Latin America-Spain (20284 + Euro symbol); IBM EBCDIC (Spain-Euro) */
{1146, "IBM01146"}, /* IBM EBCDIC United Kingdom (20285 + Euro symbol); IBM EBCDIC (UK-Euro) */
{1147, "IBM01147"}, /* IBM EBCDIC France (20297 + Euro symbol); IBM EBCDIC (France-Euro) */
{1148, "IBM01148"}, /* IBM EBCDIC International (500 + Euro symbol); IBM EBCDIC (International-Euro) */
{1149, "IBM01149"}, /* IBM EBCDIC Icelandic (20871 + Euro symbol); IBM EBCDIC (Icelandic-Euro) */
{1250, "windows-1250"}, /* ANSI Central European; Central European (Windows) */
{1251, "windows-1251"}, /* ANSI Cyrillic; Cyrillic (Windows) */
{1252, "windows-1252"}, /* ANSI Latin 1; Western European (Windows) */
{1253, "windows-1253"}, /* ANSI Greek; Greek (Windows) */
{1254, "windows-1254"}, /* ANSI Turkish; Turkish (Windows) */
{1255, "windows-1255"}, /* ANSI Hebrew; Hebrew (Windows) */
{1256, "windows-1256"}, /* ANSI Arabic; Arabic (Windows) */
{1257, "windows-1257"}, /* ANSI Baltic; Baltic (Windows) */
{1258, "windows-1258"}, /* ANSI/OEM Vietnamese; Vietnamese (Windows) */
{1361, "Johab"}, /* Korean (Johab) */
{10000, "macintosh"}, /* MAC Roman; Western European (Mac) */
{10001, "x-mac-japanese"}, /* Japanese (Mac) */
{10002, "x-mac-chinesetrad"}, /* MAC Traditional Chinese (Big5); Chinese Traditional (Mac) */
{10003, "x-mac-korean"}, /* Korean (Mac) */
{10004, "x-mac-arabic"}, /* Arabic (Mac) */
{10005, "x-mac-hebrew"}, /* Hebrew (Mac) */
{10006, "x-mac-greek"}, /* Greek (Mac) */
{10007, "x-mac-cyrillic"}, /* Cyrillic (Mac) */
{10008, "x-mac-chinesesimp"}, /* MAC Simplified Chinese (GB 2312); Chinese Simplified (Mac) */
{10010, "x-mac-romanian"}, /* Romanian (Mac) */
{10017, "x-mac-ukrainian"}, /* Ukrainian (Mac) */
{10021, "x-mac-thai"}, /* Thai (Mac) */
{10029, "x-mac-ce"}, /* MAC Latin 2; Central European (Mac) */
{10079, "x-mac-icelandic"}, /* Icelandic (Mac) */
{10081, "x-mac-turkish"}, /* Turkish (Mac) */
{10082, "x-mac-croatian"}, /* Croatian (Mac) */
{20000, "x-Chinese_CNS"}, /* CNS Taiwan; Chinese Traditional (CNS) */
{20001, "x-cp20001"}, /* TCA Taiwan */
{20002, "x_Chinese-Eten"}, /* Eten Taiwan; Chinese Traditional (Eten) */
{20003, "x-cp20003"}, /* IBM5550 Taiwan */
{20004, "x-cp20004"}, /* TeleText Taiwan */
{20005, "x-cp20005"}, /* Wang Taiwan */
{20105, "x-IA5"}, /* IA5 (IRV International Alphabet No. 5, 7-bit); Western European (IA5) */
{20106, "x-IA5-German"}, /* IA5 German (7-bit) */
{20107, "x-IA5-Swedish"}, /* IA5 Swedish (7-bit) */
{20108, "x-IA5-Norwegian"}, /* IA5 Norwegian (7-bit) */
{20127, "us-ascii"}, /* US-ASCII (7-bit) */
{20261, "x-cp20261"}, /* T.61 */
{20269, "x-cp20269"}, /* ISO 6937 Non-Spacing Accent */
{20273, "IBM273"}, /* IBM EBCDIC Germany */
{20277, "IBM277"}, /* IBM EBCDIC Denmark-Norway */
{20278, "IBM278"}, /* IBM EBCDIC Finland-Sweden */
{20280, "IBM280"}, /* IBM EBCDIC Italy */
{20284, "IBM284"}, /* IBM EBCDIC Latin America-Spain */
{20285, "IBM285"}, /* IBM EBCDIC United Kingdom */
{20290, "IBM290"}, /* IBM EBCDIC Japanese Katakana Extended */
{20297, "IBM297"}, /* IBM EBCDIC France */
{20420, "IBM420"}, /* IBM EBCDIC Arabic */
{20423, "IBM423"}, /* IBM EBCDIC Greek */
{20424, "IBM424"}, /* IBM EBCDIC Hebrew */
{20833, "x-EBCDIC-KoreanExtended"}, /* IBM EBCDIC Korean Extended */
{20838, "IBM-Thai"}, /* IBM EBCDIC Thai */
{20866, "koi8-r"}, /* Russian (KOI8-R); Cyrillic (KOI8-R) */
{20871, "IBM871"}, /* IBM EBCDIC Icelandic */
{20880, "IBM880"}, /* IBM EBCDIC Cyrillic Russian */
{20905, "IBM905"}, /* IBM EBCDIC Turkish */
{20924, "IBM00924"}, /* IBM EBCDIC Latin 1/Open System (1047 + Euro symbol) */
{20932, "EUC-JP"}, /* Japanese (JIS 0208-1990 and 0121-1990) */
{20936, "x-cp20936"}, /* Simplified Chinese (GB2312); Chinese Simplified (GB2312-80) */
{20949, "x-cp20949"}, /* Korean Wansung */
{21025, "cp1025"}, /* IBM EBCDIC Cyrillic Serbian-Bulgarian */
/* 21027 (deprecated) */
{21866, "koi8-u"}, /* Ukrainian (KOI8-U); Cyrillic (KOI8-U) */
{28591, "iso-8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
{28591, "iso8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
{28591, "iso_8859-1"},
{28591, "iso_8859_1"},
{28592, "iso-8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
{28592, "iso8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
{28592, "iso_8859-2"},
{28592, "iso_8859_2"},
{28593, "iso-8859-3"}, /* ISO 8859-3 Latin 3 */
{28593, "iso8859-3"}, /* ISO 8859-3 Latin 3 */
{28593, "iso_8859-3"},
{28593, "iso_8859_3"},
{28594, "iso-8859-4"}, /* ISO 8859-4 Baltic */
{28594, "iso8859-4"}, /* ISO 8859-4 Baltic */
{28594, "iso_8859-4"},
{28594, "iso_8859_4"},
{28595, "iso-8859-5"}, /* ISO 8859-5 Cyrillic */
{28595, "iso8859-5"}, /* ISO 8859-5 Cyrillic */
{28595, "iso_8859-5"},
{28595, "iso_8859_5"},
{28596, "iso-8859-6"}, /* ISO 8859-6 Arabic */
{28596, "iso8859-6"}, /* ISO 8859-6 Arabic */
{28596, "iso_8859-6"},
{28596, "iso_8859_6"},
{28597, "iso-8859-7"}, /* ISO 8859-7 Greek */
{28597, "iso8859-7"}, /* ISO 8859-7 Greek */
{28597, "iso_8859-7"},
{28597, "iso_8859_7"},
{28598, "iso-8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
{28598, "iso8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
{28598, "iso_8859-8"},
{28598, "iso_8859_8"},
{28599, "iso-8859-9"}, /* ISO 8859-9 Turkish */
{28599, "iso8859-9"}, /* ISO 8859-9 Turkish */
{28599, "iso_8859-9"},
{28599, "iso_8859_9"},
{28603, "iso-8859-13"}, /* ISO 8859-13 Estonian */
{28603, "iso8859-13"}, /* ISO 8859-13 Estonian */
{28603, "iso_8859-13"},
{28603, "iso_8859_13"},
{28605, "iso-8859-15"}, /* ISO 8859-15 Latin 9 */
{28605, "iso8859-15"}, /* ISO 8859-15 Latin 9 */
{28605, "iso_8859-15"},
{28605, "iso_8859_15"},
{29001, "x-Europa"}, /* Europa 3 */
{38598, "iso-8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
{38598, "iso8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
{38598, "iso_8859-8-i"},
{38598, "iso_8859_8-i"},
{50220, "iso-2022-jp"}, /* ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS) */
{50221, "csISO2022JP"}, /* ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow 1 byte Kana) */
{50222, "iso-2022-jp"}, /* ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte Kana - SO/SI) */
{50225, "iso-2022-kr"}, /* ISO 2022 Korean */
{50225, "iso2022-kr"}, /* ISO 2022 Korean */
{50227, "x-cp50227"}, /* ISO 2022 Simplified Chinese; Chinese Simplified (ISO 2022) */
/* 50229 ISO 2022 Traditional Chinese */
/* 50930 EBCDIC Japanese (Katakana) Extended */
/* 50931 EBCDIC US-Canada and Japanese */
/* 50933 EBCDIC Korean Extended and Korean */
/* 50935 EBCDIC Simplified Chinese Extended and Simplified Chinese */
/* 50936 EBCDIC Simplified Chinese */
/* 50937 EBCDIC US-Canada and Traditional Chinese */
/* 50939 EBCDIC Japanese (Latin) Extended and Japanese */
{51932, "euc-jp"}, /* EUC Japanese */
{51936, "EUC-CN"}, /* EUC Simplified Chinese; Chinese Simplified (EUC) */
{51949, "euc-kr"}, /* EUC Korean */
/* 51950 EUC Traditional Chinese */
{52936, "hz-gb-2312"}, /* HZ-GB2312 Simplified Chinese; Chinese Simplified (HZ) */
{54936, "GB18030"}, /* Windows XP and later: GB18030 Simplified Chinese (4 byte); Chinese Simplified (GB18030) */
{57002, "x-iscii-de"}, /* ISCII Devanagari */
{57003, "x-iscii-be"}, /* ISCII Bengali */
{57004, "x-iscii-ta"}, /* ISCII Tamil */
{57005, "x-iscii-te"}, /* ISCII Telugu */
{57006, "x-iscii-as"}, /* ISCII Assamese */
{57007, "x-iscii-or"}, /* ISCII Oriya */
{57008, "x-iscii-ka"}, /* ISCII Kannada */
{57009, "x-iscii-ma"}, /* ISCII Malayalam */
{57010, "x-iscii-gu"}, /* ISCII Gujarati */
{57011, "x-iscii-pa"}, /* ISCII Punjabi */
{0, NULL}
};
/*
* SJIS SHIFTJIS table CP932 table
* ---- --------------------------- --------------------------------
* 5C U+00A5 YEN SIGN U+005C REVERSE SOLIDUS
* 7E U+203E OVERLINE U+007E TILDE
* 815C U+2014 EM DASH U+2015 HORIZONTAL BAR
* 815F U+005C REVERSE SOLIDUS U+FF3C FULLWIDTH REVERSE SOLIDUS
* 8160 U+301C WAVE DASH U+FF5E FULLWIDTH TILDE
* 8161 U+2016 DOUBLE VERTICAL LINE U+2225 PARALLEL TO
* 817C U+2212 MINUS SIGN U+FF0D FULLWIDTH HYPHEN-MINUS
* 8191 U+00A2 CENT SIGN U+FFE0 FULLWIDTH CENT SIGN
* 8192 U+00A3 POUND SIGN U+FFE1 FULLWIDTH POUND SIGN
* 81CA U+00AC NOT SIGN U+FFE2 FULLWIDTH NOT SIGN
*
* EUC-JP and ISO-2022-JP should be compatible with CP932.
*
* Kernel and MLang have different Unicode mapping table. Make sure
* which API is used.
*/
static compat_t cp932_compat[] = {
{0x00A5, 0x005C, COMPAT_OUT},
{0x203E, 0x007E, COMPAT_OUT},
{0x2014, 0x2015, COMPAT_OUT},
{0x301C, 0xFF5E, COMPAT_OUT},
{0x2016, 0x2225, COMPAT_OUT},
{0x2212, 0xFF0D, COMPAT_OUT},
{0x00A2, 0xFFE0, COMPAT_OUT},
{0x00A3, 0xFFE1, COMPAT_OUT},
{0x00AC, 0xFFE2, COMPAT_OUT},
{0, 0, 0}
};
static compat_t cp20932_compat[] = {
{0x00A5, 0x005C, COMPAT_OUT},
{0x203E, 0x007E, COMPAT_OUT},
{0x2014, 0x2015, COMPAT_OUT},
{0xFF5E, 0x301C, COMPAT_OUT|COMPAT_IN},
{0x2225, 0x2016, COMPAT_OUT|COMPAT_IN},
{0xFF0D, 0x2212, COMPAT_OUT|COMPAT_IN},
{0xFFE0, 0x00A2, COMPAT_OUT|COMPAT_IN},
{0xFFE1, 0x00A3, COMPAT_OUT|COMPAT_IN},
{0xFFE2, 0x00AC, COMPAT_OUT|COMPAT_IN},
{0, 0, 0}
};
static compat_t *cp51932_compat = cp932_compat;
/* cp20932_compat for kernel. cp932_compat for mlang. */
static compat_t *cp5022x_compat = cp932_compat;
typedef HRESULT (WINAPI *CONVERTINETSTRING)(
LPDWORD lpdwMode,
DWORD dwSrcEncoding,
DWORD dwDstEncoding,
LPCSTR lpSrcStr,
LPINT lpnSrcSize,
LPBYTE lpDstStr,
LPINT lpnDstSize
);
typedef HRESULT (WINAPI *CONVERTINETMULTIBYTETOUNICODE)(
LPDWORD lpdwMode,
DWORD dwSrcEncoding,
LPCSTR lpSrcStr,
LPINT lpnMultiCharCount,
LPWSTR lpDstStr,
LPINT lpnWideCharCount
);
typedef HRESULT (WINAPI *CONVERTINETUNICODETOMULTIBYTE)(
LPDWORD lpdwMode,
DWORD dwEncoding,
LPCWSTR lpSrcStr,
LPINT lpnWideCharCount,
LPSTR lpDstStr,
LPINT lpnMultiCharCount
);
typedef HRESULT (WINAPI *ISCONVERTINETSTRINGAVAILABLE)(
DWORD dwSrcEncoding,
DWORD dwDstEncoding
);
typedef HRESULT (WINAPI *LCIDTORFC1766A)(
LCID Locale,
LPSTR pszRfc1766,
int nChar
);
typedef HRESULT (WINAPI *LCIDTORFC1766W)(
LCID Locale,
LPWSTR pszRfc1766,
int nChar
);
typedef HRESULT (WINAPI *RFC1766TOLCIDA)(
LCID *pLocale,
LPSTR pszRfc1766
);
typedef HRESULT (WINAPI *RFC1766TOLCIDW)(
LCID *pLocale,
LPWSTR pszRfc1766
);
static CONVERTINETSTRING ConvertINetString;
static CONVERTINETMULTIBYTETOUNICODE ConvertINetMultiByteToUnicode;
static CONVERTINETUNICODETOMULTIBYTE ConvertINetUnicodeToMultiByte;
static ISCONVERTINETSTRINGAVAILABLE IsConvertINetStringAvailable;
static LCIDTORFC1766A LcidToRfc1766A;
static RFC1766TOLCIDA Rfc1766ToLcidA;
static int
load_mlang(void)
{
HMODULE h;
if (ConvertINetString != NULL)
return TRUE;
h = LoadLibrary(TEXT("mlang.dll"));
if (!h)
return FALSE;
ConvertINetString = (CONVERTINETSTRING)GetProcAddressA(h, "ConvertINetString");
ConvertINetMultiByteToUnicode = (CONVERTINETMULTIBYTETOUNICODE)GetProcAddressA(h, "ConvertINetMultiByteToUnicode");
ConvertINetUnicodeToMultiByte = (CONVERTINETUNICODETOMULTIBYTE)GetProcAddressA(h, "ConvertINetUnicodeToMultiByte");
IsConvertINetStringAvailable = (ISCONVERTINETSTRINGAVAILABLE)GetProcAddressA(h, "IsConvertINetStringAvailable");
LcidToRfc1766A = (LCIDTORFC1766A)GetProcAddressA(h, "LcidToRfc1766A");
Rfc1766ToLcidA = (RFC1766TOLCIDA)GetProcAddressA(h, "Rfc1766ToLcidA");
return TRUE;
}
iconv_t
iconv_open(const char *tocode, const char *fromcode)
{
rec_iconv_t *cd;
cd = (rec_iconv_t *)calloc(1, sizeof(rec_iconv_t));
if (cd == NULL)
return (iconv_t)(-1);
#if defined(USE_LIBICONV_DLL)
errno = 0;
if (libiconv_iconv_open(cd, tocode, fromcode))
return (iconv_t)cd;
#endif
/* reset the errno to prevent reporting wrong error code.
* 0 for unsorted error. */
errno = 0;
if (win_iconv_open(cd, tocode, fromcode))
return (iconv_t)cd;
free(cd);
return (iconv_t)(-1);
}
int
iconv_close(iconv_t _cd)
{
rec_iconv_t *cd = (rec_iconv_t *)_cd;
int r = cd->iconv_close(cd->cd);
int e = *(cd->_errno());
#if defined(USE_LIBICONV_DLL)
if (cd->hlibiconv != NULL)
FreeLibrary(cd->hlibiconv);
#endif
free(cd);
errno = e;
return r;
}
size_t
iconv(iconv_t _cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
{
rec_iconv_t *cd = (rec_iconv_t *)_cd;
size_t r = cd->iconv(cd->cd, inbuf, inbytesleft, outbuf, outbytesleft);
errno = *(cd->_errno());
return r;
}
static int
win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode)
{
if (!make_csconv(fromcode, &cd->from) || !make_csconv(tocode, &cd->to))
return FALSE;
cd->iconv_close = win_iconv_close;
cd->iconv = win_iconv;
cd->_errno = _errno;
cd->cd = (iconv_t)cd;
return TRUE;
}
static int
win_iconv_close(iconv_t cd UNUSED)
{
return 0;
}
static size_t
win_iconv(iconv_t _cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
{
rec_iconv_t *cd = (rec_iconv_t *)_cd;
ushort wbuf[MB_CHAR_MAX]; /* enough room for one character */
int insize;
int outsize;
int wsize;
DWORD frommode;
DWORD tomode;
uint wc;
compat_t *cp;
int i;
if (inbuf == NULL || *inbuf == NULL)
{
if (outbuf != NULL && *outbuf != NULL && cd->to.flush != NULL)
{
tomode = cd->to.mode;
outsize = cd->to.flush(&cd->to, (uchar *)*outbuf, *outbytesleft);
if (outsize == -1)
{
if ((cd->to.flags & FLAG_IGNORE) && errno != E2BIG)
{
outsize = 0;
}
else
{
cd->to.mode = tomode;
return (size_t)(-1);
}
}
*outbuf += outsize;
*outbytesleft -= outsize;
}
cd->from.mode = 0;
cd->to.mode = 0;
return 0;
}
while (*inbytesleft != 0)
{
frommode = cd->from.mode;
tomode = cd->to.mode;
wsize = MB_CHAR_MAX;
insize = cd->from.mbtowc(&cd->from, (const uchar *)*inbuf, *inbytesleft, wbuf, &wsize);
if (insize == -1)
{
if (cd->to.flags & FLAG_IGNORE)
{
cd->from.mode = frommode;
insize = 1;
wsize = 0;
}
else
{
cd->from.mode = frommode;
return (size_t)(-1);
}
}
if (wsize == 0)
{
*inbuf += insize;
*inbytesleft -= insize;
continue;
}
if (cd->from.compat != NULL)
{
wc = utf16_to_ucs4(wbuf);
cp = cd->from.compat;
for (i = 0; cp[i].in != 0; ++i)
{
if ((cp[i].flag & COMPAT_IN) && cp[i].out == wc)
{
ucs4_to_utf16(cp[i].in, wbuf, &wsize);
break;
}
}
}
if (cd->to.compat != NULL)
{
wc = utf16_to_ucs4(wbuf);
cp = cd->to.compat;
for (i = 0; cp[i].in != 0; ++i)
{
if ((cp[i].flag & COMPAT_OUT) && cp[i].in == wc)
{
ucs4_to_utf16(cp[i].out, wbuf, &wsize);
break;
}
}
}
outsize = cd->to.wctomb(&cd->to, wbuf, wsize, (uchar *)*outbuf, *outbytesleft);
if (outsize == -1)
{
if ((cd->to.flags & FLAG_IGNORE) && errno != E2BIG)
{
cd->to.mode = tomode;
outsize = 0;
}
else
{
cd->from.mode = frommode;
cd->to.mode = tomode;
return (size_t)(-1);
}
}
*inbuf += insize;
*outbuf += outsize;
*inbytesleft -= insize;
*outbytesleft -= outsize;
}
return 0;
}
static int
make_csconv(const char *_name, csconv_t *cv)
{
CPINFO cpinfo;
int use_compat = TRUE;
int flag = 0;
char *name;
char *p;
name = xstrndup(_name, strlen(_name));
if (name == NULL)
return FALSE;
/* check for option "enc_name//opt1//opt2" */
while ((p = strrstr(name, "//")) != NULL)
{
if (_stricmp(p + 2, "nocompat") == 0)
use_compat = FALSE;
else if (_stricmp(p + 2, "translit") == 0)
flag |= FLAG_TRANSLIT;
else if (_stricmp(p + 2, "ignore") == 0)
flag |= FLAG_IGNORE;
*p = 0;
}
cv->mode = 0;
cv->flags = flag;
cv->mblen = NULL;
cv->flush = NULL;
cv->compat = NULL;
cv->codepage = name_to_codepage(name);
if (cv->codepage == 1200 || cv->codepage == 1201)
{
cv->mbtowc = utf16_mbtowc;
cv->wctomb = utf16_wctomb;
if (_stricmp(name, "UTF-16") == 0 || _stricmp(name, "UTF16") == 0 ||
_stricmp(name, "UCS-2") == 0 || _stricmp(name, "UCS2") == 0 ||
_stricmp(name,"UCS-2-INTERNAL") == 0)
cv->flags |= FLAG_USE_BOM;
}
else if (cv->codepage == 12000 || cv->codepage == 12001)
{
cv->mbtowc = utf32_mbtowc;
cv->wctomb = utf32_wctomb;
if (_stricmp(name, "UTF-32") == 0 || _stricmp(name, "UTF32") == 0 ||
_stricmp(name, "UCS-4") == 0 || _stricmp(name, "UCS4") == 0)
cv->flags |= FLAG_USE_BOM;
}
else if (cv->codepage == 65001)
{
cv->mbtowc = kernel_mbtowc;
cv->wctomb = kernel_wctomb;
cv->mblen = utf8_mblen;
}
else if ((cv->codepage == 50220 || cv->codepage == 50221 || cv->codepage == 50222) && load_mlang())
{
cv->mbtowc = iso2022jp_mbtowc;
cv->wctomb = iso2022jp_wctomb;
cv->flush = iso2022jp_flush;
}
else if (cv->codepage == 51932 && load_mlang())
{
cv->mbtowc = mlang_mbtowc;
cv->wctomb = mlang_wctomb;
cv->mblen = eucjp_mblen;
}
else if (IsValidCodePage(cv->codepage)
&& GetCPInfo(cv->codepage, &cpinfo) != 0)
{
cv->mbtowc = kernel_mbtowc;
cv->wctomb = kernel_wctomb;
if (cpinfo.MaxCharSize == 1)
cv->mblen = sbcs_mblen;
else if (cpinfo.MaxCharSize == 2)
cv->mblen = dbcs_mblen;
else
cv->mblen = mbcs_mblen;
}
else
{
/* not supported */