summaryrefslogtreecommitdiff
path: root/src/classlibnative/nls/nlsinfo.cpp
blob: babad228f6d02060f9164b983494aae290fe28bd (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
////////////////////////////////////////////////////////////////////////////
//
//  Class:    NLSInfo
//

//
//  Purpose:  This module implements the methods of the COMNlsInfo
//            class.  These methods are the helper functions for the
//            Locale class.
//
//  Date:     August 12, 1998
//
////////////////////////////////////////////////////////////////////////////

//
//  Include Files.
//
#include "common.h"
#include "object.h"
#include "excep.h"
#include "vars.hpp"
#include "interoputil.h"
#include "corhost.h"

#include <winnls.h>

#include "utilcode.h"
#include "frames.h"
#include "field.h"
#include "metasig.h"
#include "nls.h"
#include "nlsinfo.h"
#include "nlstable.h"

//#include <mlang.h>
#include "sortversioning.h"

#include "newapis.h"

//
//  Constant Declarations.
//

#ifndef COMPARE_OPTIONS_IGNORECASE
#define COMPARE_OPTIONS_IGNORECASE  0x00000001
#endif

#ifndef LOCALE_SNAME
#define LOCALE_SNAME                0x0000005c
#endif

#ifndef LOCALE_SNAN
#define LOCALE_SNAN                 0x00000069
#endif

#ifndef LOCALE_SPOSINFINITY
#define LOCALE_SPOSINFINITY         0x0000006a
#endif

#ifndef LOCALE_SNEGINFINITY
#define LOCALE_SNEGINFINITY         0x0000006b
#endif

#ifndef LOCALE_SPARENT
#define LOCALE_SPARENT              0x0000006d
#endif

#ifndef LOCALE_SCONSOLEFALLBACKNAME
#define LOCALE_SCONSOLEFALLBACKNAME 0x0000006e   // Fallback name for within the console
#endif

#ifndef LOCALE_SISO3166CTRYNAME2
#define LOCALE_SISO3166CTRYNAME2    0x00000068
#endif

#ifndef LOCALE_SISO639LANGNAME2
#define LOCALE_SISO639LANGNAME2     0x00000067
#endif

#ifndef LOCALE_SSHORTESTDAYNAME1
#define LOCALE_SSHORTESTDAYNAME1    0x00000060
#endif

// Windows 7 LCTypes
#ifndef LOCALE_INEUTRAL
#define LOCALE_INEUTRAL             0x00000071   // Returns 0 for specific cultures, 1 for neutral cultures.
#endif

#ifndef LCMAP_TITLECASE
#define LCMAP_TITLECASE             0x00000300   // Title Case Letters
#endif

// Windows 8 LCTypes
#ifndef LCMAP_SORTHANDLE
#define LCMAP_SORTHANDLE   0x20000000
#endif

#ifndef LCMAP_HASH
#define LCMAP_HASH   0x00040000
#endif

#ifndef LOCALE_REPLACEMENT
#define LOCALE_REPLACEMENT          0x00000008   // locales that replace shipped locales (callback flag only)
#endif // LOCALE_REPLACEMENT

#define LOCALE_MAX_STRING_SIZE      530          // maximum sice of LOCALE_SKEYBOARDSTOINSTALL, currently 5 "long" + 2 "short" keyboard signatures (YI + 3).

#define MAX_STRING_VALUE        512

// TODO: NLS Arrowhead -Be nice if we could depend more on the OS for this
// Language ID for CHT (Taiwan)
#define LANGID_ZH_TW            0x0404
// Language ID for CHT (Hong-Kong)
#define LANGID_ZH_HK            0x0c04
#define REGION_NAME_0404 W("\x53f0\x7063")
#if BIGENDIAN
#define INTERNATIONAL_CURRENCY_SYMBOL W("\x00a4")
#else
#define INTERNATIONAL_CURRENCY_SYMBOL W("\xa400")
#endif

inline BOOL IsCustomCultureId(LCID lcid)
{
    return (lcid == LOCALE_CUSTOM_DEFAULT || lcid == LOCALE_CUSTOM_UNSPECIFIED);
}

#ifndef FEATURE_CORECLR
//
// Normalization Implementation
//
#define NORMALIZATION_DLL       MAKEDLLNAME(W("normalization"))
HMODULE COMNlsInfo::m_hNormalization = NULL;
PFN_NORMALIZATION_IS_NORMALIZED_STRING COMNlsInfo::m_pfnNormalizationIsNormalizedStringFunc = NULL;
PFN_NORMALIZATION_NORMALIZE_STRING COMNlsInfo::m_pfnNormalizationNormalizeStringFunc = NULL;
PFN_NORMALIZATION_INIT_NORMALIZATION COMNlsInfo::m_pfnNormalizationInitNormalizationFunc = NULL;
#endif

#if FEATURE_CODEPAGES_FILE
/*============================nativeCreateOpenFileMapping============================
**Action: Create or open a named memory file mapping.
**Returns: Pointer to named section, or NULL if failed
**Arguments:
**  StringObject*   inSectionName  - name of section to open/create
**  int             inBytesToAllocate - desired size of memory section in bytes
**                      We use the last 4 bytes (must be aligned, so only choose
**                      inBytesToAllocate in multiples of 4) to indicate if the
**                      section is set or not.  AFTER section is initialized, set
**                      those 4 bytes to non-0, otherwise you'll get get new
**                      heap memory all the time.
**  HANDLE*         mappedFile - is the handle of the memory mapped file. this is
**                      out parameter.
**
** NOTE: We'll try to open the same object, so we can share names.  We don't lock
**       though, so 2 thread could get the same object, but thread 1 might not
**       have initialized it yet.
**
** NOTE: For NT you should add a Global\ to the beginning of the name if you
**       want to share it machine wide.
**
==============================================================================*/
FCIMPL3(LPVOID, COMNlsInfo::nativeCreateOpenFileMapping,
            StringObject* inSectionNameUNSAFE, int inBytesToAllocate, HANDLE *mappedFile)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(inSectionNameUNSAFE));
        PRECONDITION(inBytesToAllocate % 4 == 0);
        PRECONDITION(inBytesToAllocate > 0);
        PRECONDITION(CheckPointer(mappedFile));
    } CONTRACTL_END;

    // Need a place for our result
    LPVOID pResult = NULL;

    STRINGREF inString(inSectionNameUNSAFE);
    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(inString);

    _ASSERTE(inBytesToAllocate % 4 == 0);   // Expected 4 bytes boundaries so we don't get unaligned
    _ASSERTE(inBytesToAllocate > 0);        // Pointless to have <=0 allocation

    StackSString inNameStackBuffer (inString->GetBuffer());
    pResult = NLSTable::OpenOrCreateMemoryMapping((LPCWSTR)inNameStackBuffer, inBytesToAllocate, mappedFile);

    // Worst case allocate some memory, use holder
    //    if (pResult == NULL) pResult = new BYTE[inBytesToAllocate];
    if (pResult == NULL)
    {
        // Need to use a NewHolder
        NewArrayHolder<BYTE> holder (new BYTE[inBytesToAllocate]);
        pResult = holder;
        // Zero out the mapCodePageCached field (an int value, and it's used to check if the section is initialized or not.)
        BYTE* pByte = (BYTE*)pResult;
        FillMemory(pByte + inBytesToAllocate - sizeof(int), sizeof(int), 0);
        holder.SuppressRelease();
    }

    HELPER_METHOD_FRAME_END();

    return pResult;
}
FCIMPLEND
#endif // FEATURE_CODEPAGES_FILE

// InternalIsSortable
//
// Called by CompareInfo.IsSortable() to determine if a string has entirely sortable (ie: defined) code points.
BOOL QCALLTYPE COMNlsInfo::InternalIsSortable(INT_PTR handle, INT_PTR handleOrigin, LPCWSTR localeName, LPCWSTR string, INT32 length)
{
    CONTRACTL
    {
        QCALL_CHECK;
        PRECONDITION(CheckPointer(string));
    } CONTRACTL_END;
    BOOL result = FALSE;
    BEGIN_QCALL;

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();

    if(!(curDomain->m_bUseOsSorting))
    {
        handle = EnsureValidSortHandle(handle, handleOrigin, localeName);
        result = SortVersioning::SortDllIsDefinedString((SortVersioning::PSORTHANDLE) handle, COMPARE_STRING, 0, string, length);
    }
    else if(curDomain->m_pCustomSortLibrary != NULL)
    {
        result = (curDomain->m_pCustomSortLibrary->pIsNLSDefinedString)(COMPARE_STRING, 0, NULL, string, length);
    }
    else
#endif
    {
        // Function should be COMPARE_STRING, dwFlags should be NULL, lpVersionInfo should be NULL for now
        result = NewApis::IsNLSDefinedString(COMPARE_STRING, 0, NULL, string, length);
    }

    END_QCALL;
    return result;
}

////////////////////////////////////////////////////////////////////////////
//
//  InternalGetUserDefaultLocaleName
//
//  Returns a string with the name of our LCID and returns 0 in LCID.
//  If we cant return
//
////////////////////////////////////////////////////////////////////////////
// This is new to longhorn
BOOL QCALLTYPE COMNlsInfo::InternalGetDefaultLocaleName(INT32 langType, QCall::StringHandleOnStack defaultLocaleName)
{
    CONTRACTL
    {
        QCALL_CHECK;
        PRECONDITION((langType == LOCALE_SYSTEM_DEFAULT) || (langType == LOCALE_USER_DEFAULT));
    } CONTRACTL_END;

    BOOL result;
    BEGIN_QCALL;

    WCHAR strName[LOCALE_NAME_MAX_LENGTH];
    int size = 0;

    if (langType == LOCALE_SYSTEM_DEFAULT)
    {
        size = NewApis::GetSystemDefaultLocaleName(strName,NumItems(strName));
    }
    else
    {
        _ASSERT(langType == LOCALE_USER_DEFAULT);
        size = NewApis::GetUserDefaultLocaleName(strName,NumItems(strName));
    }

    // Not found, either not longhorn (no LOCALE_SNAME) or not a valid name
    if (size == 0)
    {
        result = false;
    }
    else
    {
        defaultLocaleName.Set(strName);
        result = true;
    }
    END_QCALL;
    return result;
}

BOOL QCALLTYPE COMNlsInfo::InternalGetSystemDefaultUILanguage(QCall::StringHandleOnStack systemDefaultUiLanguage)
{
    QCALL_CONTRACT;
    BOOL result;
    BEGIN_QCALL;

    WCHAR localeName[LOCALE_NAME_MAX_LENGTH];

    int systemDefaultUiLcid = GetSystemDefaultUILanguage();
    if(systemDefaultUiLcid == LANGID_ZH_TW)
    {
        if (!NewApis::IsZhTwSku())
        {
             systemDefaultUiLcid = LANGID_ZH_HK;
        } 
    }
    
    int length = NewApis::LCIDToLocaleName(systemDefaultUiLcid, localeName, NumItems(localeName), 0);
    if (length == 0)
    {
        result = false;
    }
    else
    {
        systemDefaultUiLanguage.Set(localeName);
        result = true;
    }

    END_QCALL;
    return result;
}

/*
 */
BOOL QCALLTYPE COMNlsInfo::InternalGetUserDefaultUILanguage(QCall::StringHandleOnStack userDefaultUiLanguage)
{
    QCALL_CONTRACT;
    BOOL result;
    BEGIN_QCALL;

    WCHAR wszBuffer[LOCALE_NAME_MAX_LENGTH];
    LPCWSTR wszLangName=NULL;

    int res= 0;
    ULONG uLangCount=0;
    ULONG uBufLen=0;
    res= NewApis::GetUserPreferredUILanguages (MUI_LANGUAGE_NAME,&uLangCount,NULL,&uBufLen);
    if (res == 0)
        ThrowLastError();


    NewArrayHolder<WCHAR> sPreferredLanguages(NULL);

    if (uBufLen > 0 && uLangCount > 0 )
    {
        sPreferredLanguages = new WCHAR[uBufLen];
        res= NewApis::GetUserPreferredUILanguages (MUI_LANGUAGE_NAME,&uLangCount,sPreferredLanguages,&uBufLen);

        if (res == 0)
            ThrowLastError();

         wszLangName=sPreferredLanguages;
// Review size_t to int conversion (possible loss of data).
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4267)
#endif
        res=wcslen(wszLangName)+1;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
    }
    else
    {
        res=0;
    }

    if (res == 0) {
        res = NewApis::GetUserDefaultLocaleName(wszBuffer,NumItems(wszBuffer));
        wszLangName=wszBuffer;
    }


    // If not found, either not longhorn (no LOCALE_SNAME) or not a valid name
    if (res == 0)
    {
        // Didn't find string, return an empty string.
        result = false;
    }
    else
    {
        userDefaultUiLanguage.Set(wszLangName);
        result = true;
    }

    // Return the found language name.  LCID should be found one already.
    END_QCALL;
    return result;
}

// Added but disabled from desktop in .NET 4.0, stayed disabled in .NET 4.5
#ifdef FEATURE_CORECLR
FCIMPL0(Object*, COMNlsInfo::nativeGetResourceFallbackArray)
{
    CONTRACTL
    {
        FCALL_CHECK;
    } CONTRACTL_END;

    DWORD dwFlags = MUI_MERGE_USER_FALLBACK | MUI_MERGE_SYSTEM_FALLBACK;
    ULONG cchLanguagesBuffer = 0;
    ULONG ulNumLanguages = 0;
    BOOL result = FALSE;

    struct _gc
    {
        PTRARRAYREF     resourceFallbackArray;
    } gc;

    gc.resourceFallbackArray = NULL;

    // If the resource lookups we're planning on doing are going to be written to a non-Unicode console,
    // then we should ideally only return languages that can be displayed correctly on the console.  The 
    // trick is guessing at whether we're writing this data to the console, which we can't do well.
    // Instead, we ask new apps to call GetConsoleFallbackUICulture & fall back to en-US.
    bool disableUserFallback;
    BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
    disableUserFallback = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_Resources_DisableUserPreferredFallback) == 1
    END_SO_INTOLERANT_CODE;

    if (disableUserFallback)
        return NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);

    // first call with null buffer to get size
    result = NewApis::GetThreadPreferredUILanguages(dwFlags, &ulNumLanguages, NULL, &cchLanguagesBuffer);
    if (cchLanguagesBuffer > 0)
    {
        NewArrayHolder<WCHAR> stringBuffer = new (nothrow) WCHAR[cchLanguagesBuffer];
        if (stringBuffer != NULL)
        {
            result = NewApis::GetThreadPreferredUILanguages(dwFlags, &ulNumLanguages, stringBuffer, &cchLanguagesBuffer);
            _ASSERTE(result);

            // now string into strings
            gc.resourceFallbackArray = (PTRARRAYREF) AllocateObjectArray(ulNumLanguages, g_pStringClass);

            LPCWSTR buffer = stringBuffer;   // Restart @ buffer beginning
            for(DWORD i = 0; i < ulNumLanguages; i++)
            {
                OBJECTREF o = (OBJECTREF) StringObject::NewString(buffer);
                gc.resourceFallbackArray->SetAt(i, o);
                buffer += (lstrlenW(buffer) + 1);
            }
        }
    }

    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(gc.resourceFallbackArray);

}
FCIMPLEND
#endif // FEATURE_CORECLR

INT32 COMNlsInfo::CallGetUserDefaultUILanguage()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        SO_TOLERANT;
    } CONTRACTL_END;

    static INT32 s_lcid = 0;

    // The user UI language cannot change within a process (in fact it cannot change within a logon session),
    // so we cache it. We dont take a lock while initializing s_lcid for the same reason. If two threads are
    // racing to initialize s_lcid, the worst thing that'll happen is that one thread will call
    // GetUserDefaultUILanguage needlessly, but the final result is going to be the same.
    if (s_lcid == 0)
    {
        INT32 s_lcidTemp = GetUserDefaultUILanguage();
        if (s_lcidTemp == LANGID_ZH_TW)
        {
            // If the UI language ID is 0x0404, we need to do extra check to decide
            // the real UI language, since MUI (in CHT)/HK/TW Windows SKU all uses 0x0404 as their CHT language ID.
            if (!NewApis::IsZhTwSku())
            {
                s_lcidTemp = LANGID_ZH_HK;
            }
        }
        s_lcid = s_lcidTemp;
    }

    return s_lcid;
}

INT_PTR COMNlsInfo::EnsureValidSortHandle(INT_PTR handle, INT_PTR handleOrigin, LPCWSTR localeName)
{
    LIMITED_METHOD_CONTRACT;

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();

    if(!(curDomain->m_bUseOsSorting) && handleOrigin == (INT_PTR) SortVersioning::GetSortHandle && ((SortVersioning::PSORTHANDLE) handle)->dwNLSVersion == curDomain->m_sortVersion)
    {
        return handle;
    }

    if(curDomain->m_bUseOsSorting && curDomain->m_pCustomSortLibrary == NULL && handleOrigin == (INT_PTR) NewApis::LCMapStringEx)
    {
        return handle;
    }

    if(curDomain->m_bUseOsSorting && curDomain->m_pCustomSortLibrary != NULL && handleOrigin == (INT_PTR) curDomain->m_pCustomSortLibrary->pLCMapStringEx)
    {
        return handle;
    }

    // At this point, we can't reuse the sort handle (it has different sort semantics than this domain) so we need to get a new one.
    INT_PTR newHandleOrigin;
    return InitSortHandleHelper(localeName, &newHandleOrigin);
#else
    // For CoreCLR, on Windows 8 and up the handle will be valid. on downlevels the handle will be null
    return handle;
#endif
}

#ifdef FEATURE_SYNTHETIC_CULTURES
////////////////////////////////////////////////////////////////////////////
//
//  WstrToInteger4
//
////////////////////////////////////////////////////////////////////////////

/*=================================WstrToInteger4==================================
**Action: Convert a Unicode string to an integer.  Error checking is ignored.
**Returns: The integer value of wstr
**Arguments:
**      wstr: NULL terminated wide string.  Can have character 0'-'9', 'a'-'f', and 'A' - 'F'
**      Radix: radix to be used in the conversion.
**Exceptions: None.
==============================================================================*/

INT32 COMNlsInfo::WstrToInteger4(
    __in_z LPCWSTR wstr,
    __in int Radix)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        SO_TOLERANT;
        PRECONDITION(CheckPointer(wstr));
        PRECONDITION(Radix > 1 && Radix <= 16);
    } CONTRACTL_END;
    INT32 Value = 0;
    int Base = 1;

    for (int Length = Wszlstrlen(wstr) - 1; Length >= 0; Length--)

    {
        WCHAR ch = wstr[Length];
        _ASSERTE((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'));
        if (ch >= 'a')
        {
            ch = ch - 'a' + 'A';
        }

        Value += ((ch >= 'A') ? (ch - 'A' + 10) : (ch - '0')) * Base;
        Base *= Radix;
    }

    return (Value);
}
#endif // FEATURE_SYNTHETIC_CULTURES


#ifndef FEATURE_CORECLR
FCIMPL1(FC_BOOL_RET, COMNlsInfo::nativeSetThreadLocale, StringObject* localeNameUNSAFE)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(localeNameUNSAFE));
    } CONTRACTL_END;

    LCID lcid = 0;

    // TODO: NLS Arrowhead -A bit scary becausue Set ThreadLocale can't handle custom cultures?
    STRINGREF localeName = (STRINGREF)localeNameUNSAFE;
    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(localeName);
    lcid=NewApis::LocaleNameToLCID(localeName->GetBuffer(),0);
    if (lcid == 0)
    {
        ThrowHR(HRESULT_FROM_WIN32(GetLastError()));
    }
        HELPER_METHOD_FRAME_END();


    BOOL result = TRUE;

    // SetThreadLocale doesn't handle names/custom cultures
#ifdef _MSC_VER
// Get rid of the SetThreadLocale warning in OACR:
#pragma warning(push)
#pragma warning(disable:38010)
#endif
    result = ::SetThreadLocale(lcid);
#ifdef _MSC_VER
#pragma warning(pop)
#endif

    FC_RETURN_BOOL(result);
}
FCIMPLEND
#endif


FCIMPL2(Object*, COMNlsInfo::nativeGetLocaleInfoEx, StringObject* localeNameUNSAFE, INT32 lcType)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(localeNameUNSAFE));
    } CONTRACTL_END;

    struct _gc
    {
        STRINGREF   localeName;
        STRINGREF   refRetVal;
    } gc;

    // Dereference our string
    gc.refRetVal = NULL;
    gc.localeName = (STRINGREF)localeNameUNSAFE;

    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
    StackSString localeNameStackBuffer( gc.localeName->GetBuffer() );

    WCHAR buffer[LOCALE_MAX_STRING_SIZE];
    int result = NewApis::GetLocaleInfoEx(localeNameStackBuffer, lcType, buffer, NumItems(buffer));

    // Make a string out of it
    if (result != 0)
    {
        // Exclude the NULL char at the end, except that LOCALE_FONTSIGNATURE isn't
        // really a string, so we need the last character too.
        gc.refRetVal = StringObject::NewString(buffer, ((lcType & ~LOCALE_NOUSEROVERRIDE) == LOCALE_FONTSIGNATURE) ? result : result-1);
    }
    else
    {
    }
    HELPER_METHOD_FRAME_END();
    return OBJECTREFToObject(gc.refRetVal);
}
FCIMPLEND


FCIMPL2(INT32, COMNlsInfo::nativeGetLocaleInfoExInt, StringObject* localeNameUNSAFE, INT32 lcType)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(localeNameUNSAFE));
    } CONTRACTL_END;

    INT32 result = 0;

    // Dereference our string
    STRINGREF localeName = (STRINGREF)localeNameUNSAFE;
    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(localeName);

    lcType |= LOCALE_RETURN_NUMBER;

    if (NewApis::GetLocaleInfoEx(localeName->GetBuffer(), lcType, (LPWSTR)&result, sizeof(INT32) / sizeof (WCHAR)) == 0)
    {
        // return value of 0 indicates failure and error value is supposed to be set.
        // shouldn't ever really happen
        _ASSERTE(!"catastrophic failure calling NewApis::nativeGetLocaleInfoExInt!  This could be a CultureInfo bug (bad localeName string) or maybe a GCHole.");
    }

    HELPER_METHOD_FRAME_END();

    return result;
}
FCIMPLEND



////////////////////////////////////////////////////////////////////////
//
// Call the Win32 GetLocaleInfo() using the specified lcid to retrieve
// the native digits, probably from the registry override. The return
// indicates whether the call was successful.
//
// Parameters:
//       IN lcid            the LCID to make the Win32 call with
//      OUT pOutputStrAry   The output managed string array.
//
////////////////////////////////////////////////////////////////////////
BOOL COMNlsInfo::GetNativeDigitsFromWin32(LPCWSTR locale, PTRARRAYREF * pOutputStrAry, BOOL useUserOverride)
{
    CONTRACTL
    {
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        THROWS;
    } CONTRACTL_END;

    WCHAR buffer[11];
    int result = 0;

    DWORD lcType = LOCALE_SNATIVEDIGITS;
    if(!useUserOverride)
    {
        lcType |= LOCALE_NOUSEROVERRIDE;
    }
    result = NewApis::GetLocaleInfoEx(locale, lcType, buffer, 11);
    // Be very unforgiving and only support strings of size 10 plus the NULL
    if (result == 11)
    {
        // Break up the unmanaged ten-character ZLS into what NFI wants (a managed
        // ten-string array).
        //
        // Allocate the array of STRINGREFs.  We don't need to check for null because the GC will throw
        // an OutOfMemoryException if there's not enough memory.
        //
        PTRARRAYREF DigitArray = (PTRARRAYREF) AllocateObjectArray(10, g_pStringClass);

        GCPROTECT_BEGIN(DigitArray);
        for(DWORD i = 0;  i < 10; i++) {
            OBJECTREF o = (OBJECTREF) StringObject::NewString(buffer + i, 1);
            DigitArray->SetAt(i, o);
        }
        GCPROTECT_END();

        _ASSERTE(pOutputStrAry != NULL);
        *pOutputStrAry = DigitArray;
    }

    return (result == 11);
}


////////////////////////////////////////////////////////////////////////
//
// Call the Win32 GetLocaleInfoEx() using the specified lcid and LCTYPE.
// The return value can be INT32 or an allocated managed string object, depending on
// which version's called.
//
// Parameters:
//      OUT pOutputInt32    The output int32 value.
//      OUT pOutputRef      The output string value.
//
////////////////////////////////////////////////////////////////////////
BOOL COMNlsInfo::CallGetLocaleInfoEx(LPCWSTR localeName, int lcType, INT32* pOutputInt32, BOOL useUserOverride)
{
    CONTRACTL
    {
        GC_NOTRIGGER;
        MODE_ANY;
        NOTHROW;
    } CONTRACTL_END;

    int result = 0;

    _ASSERT((lcType & LOCALE_RETURN_NUMBER) != 0);
    if(!useUserOverride)
    {
        lcType |= LOCALE_NOUSEROVERRIDE;
    }
    result = NewApis::GetLocaleInfoEx(localeName, lcType, (LPWSTR)pOutputInt32, sizeof(*pOutputInt32));

    return (result != 0);
}

BOOL COMNlsInfo::CallGetLocaleInfoEx(LPCWSTR localeName, int lcType, STRINGREF* pOutputStrRef, BOOL useUserOverride)
{
    CONTRACTL
    {
        THROWS;                 // We can throw since we are allocating managed string.
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    WCHAR buffer[LOCALE_NAME_MAX_LENGTH];
    int result = 0;

    _ASSERT((lcType & LOCALE_RETURN_NUMBER) == 0);
    if(!useUserOverride)
    {
        lcType |= LOCALE_NOUSEROVERRIDE;
    }
    result = NewApis::GetLocaleInfoEx(localeName, lcType, buffer, LOCALE_NAME_MAX_LENGTH);

    if (result != 0)
    {
        _ASSERTE(pOutputStrRef != NULL);
        *pOutputStrRef = StringObject::NewString(buffer, result - 1);
    }

    return (result != 0);
}

#ifdef FEATURE_USE_LCID
FCIMPL1(Object*, COMNlsInfo::LCIDToLocaleName, LCID lcid)
{
    FCALL_CONTRACT;

    STRINGREF refRetVal = NULL;

    // The maximum size for locale name is 85 characters.
    WCHAR localeName[LOCALE_NAME_MAX_LENGTH];
    int result = 0;

    HELPER_METHOD_FRAME_BEGIN_RET_0();

    // Note that this'll return neutral names (unlike native Vista APIs)
    result = NewApis::LCIDToLocaleName(lcid, localeName, LOCALE_NAME_MAX_LENGTH, 0);

    if (result != 0)
    {
        refRetVal = StringObject::NewString(localeName, result - 1);
    }
    else
    {
        refRetVal = StringObject::GetEmptyString();
    }

    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(refRetVal);
}
FCIMPLEND

FCIMPL1(INT32, COMNlsInfo::LocaleNameToLCID, StringObject* localeNameUNSAFE)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(localeNameUNSAFE));
    } CONTRACTL_END;

    INT32 result = 0;

    // Dereference our string
    STRINGREF localeName = (STRINGREF)localeNameUNSAFE;
    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(localeName);

    // Note that this'll return neutral names (unlike native Vista APIs)
    result = NewApis::LocaleNameToLCID(localeName->GetBuffer(), 0);

    HELPER_METHOD_FRAME_END();

    return result;
}
FCIMPLEND
#endif // FEATURE_USE_LCID

////////////////////////////////////////////////////////////////////////
//
// Implementation of CultureInfo.nativeGetNumberFormatInfoValues.
//
// Retrieve NumberFormatInfo (NFI) properties from windows
//
// Parameters:
//      IN/OUT pNumfmtUNSAFE
//                  The pointer of the managed NumberFormatInfo passed
//                  from the managed side.
//                  Note that the native NumberFormatInfo* is defined
//                  in COMNumber.h
// Note:
// Managed string will be allocated and assign to the string fields in
//      the managed NumberFormatInfo passed in pNumftUNSAFE
//
////////////////////////////////////////////////////////////////////////


/*
    This is the list of the data members in the managed NumberFormatInfo and their
    corresponding LCTYPE().

    Win32 GetLocaleInfo() constants             Data members in NumberFormatInfo in the defined order.
    LOCALE_SPOSITIVE                            // String positiveSign
    LOCALE_SNEGATIVE                            // String negativeSign
    LOCALE_SDECIMAL                             // String numberDecimalSeparator
    LOCALE_SGROUPING                            // String numberGroupSeparator
    LOCALE_SMONGROUPING                         // String currencyGroupSeparator
    LOCALE_SMONDECIMALSEP                       // String currencyDecimalSeparator
    LOCALE_SCURRENCY                            // String currencySymbol
    N/A                                         // String ansiCurrencySymbol
    N/A                                         // String nanSymbol
    N/A                                         // String positiveInfinitySymbol
    N/A                                         // String negativeInfinitySymbol
    N/A                                         // String percentDecimalSeparator
    N/A                                         // String percentGroupSeparator
    N/A                                         // String percentSymbol
    N/A                                         // String perMilleSymbol

    N/A                                         // int m_dataItem

    LOCALE_IDIGITS | LOCALE_RETURN_NUMBER,      // int numberDecimalDigits
    LOCALE_ICURRDIGITS | LOCALE_RETURN_NUMBER,  // int currencyDecimalDigits
    LOCALE_ICURRENCY | LOCALE_RETURN_NUMBER,    // int currencyPositivePattern
    LOCALE_INEGCURR | LOCALE_RETURN_NUMBER,      // int currencyNegativePattern
    LOCALE_INEGNUMBER| LOCALE_RETURN_NUMBER,    // int numberNegativePattern
    N/A                                         // int percentPositivePattern
    N/A                                         // int percentNegativePattern
    N/A                                         // int percentDecimalDigits
    N/A                                         // bool isReadOnly=false;
    N/A                                         // internal bool m_useUserOverride;
*/
FCIMPL3(FC_BOOL_RET, COMNlsInfo::nativeGetNumberFormatInfoValues,
        StringObject* localeNameUNSAFE, NumberFormatInfo* pNumfmtUNSAFE, CLR_BOOL useUserOverride) {
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(localeNameUNSAFE));
    } CONTRACTL_END;

    BOOL ret = TRUE;

    struct _gc
    {
        STRINGREF   localeName;
        STRINGREF   stringResult;
        NUMFMTREF   numfmt;
        PTRARRAYREF tempArray;
    } gc;

    // Dereference our string
    gc.localeName = (STRINGREF)localeNameUNSAFE;
    gc.numfmt = (NUMFMTREF) pNumfmtUNSAFE;
    gc.stringResult = NULL;
    gc.tempArray = NULL;

    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
    StackSString localeNameStackBuffer( gc.localeName->GetBuffer() );
    
    // Calling SString::ConvertToUnicode once
    LPCWSTR pLocaleName = localeNameStackBuffer;
    
    //
    // NOTE: We pass the stringResult allocated in the stack and assign it to the fields
    // in numfmt after calling CallGetLocaleInfo().  The reason for this is that CallGetLocaleInfo()
    // allocates a string object, and it may trigger a GC and cause numfmt to be moved.
    // That's why we use the stringResult allocated in the stack since it will not be moved.
    // After CallGetLocaleInfo(), we know that numfmt will not be moved, and it's safe to assign
    // the stringResult to its field.
    //

    // String values
    if (CallGetLocaleInfoEx(pLocaleName, LOCALE_SPOSITIVESIGN , &gc.stringResult, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sPositive), gc.stringResult, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }

    if (CallGetLocaleInfoEx(pLocaleName, LOCALE_SNEGATIVESIGN , &gc.stringResult, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sNegative), gc.stringResult, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }
    if (CallGetLocaleInfoEx(pLocaleName, LOCALE_SDECIMAL , &gc.stringResult, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sNumberDecimal), gc.stringResult, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }
    if (CallGetLocaleInfoEx(pLocaleName, LOCALE_STHOUSAND , &gc.stringResult, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sNumberGroup), gc.stringResult, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }
    if (CallGetLocaleInfoEx(pLocaleName, LOCALE_SMONTHOUSANDSEP , &gc.stringResult, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sCurrencyGroup), gc.stringResult, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }
    if (CallGetLocaleInfoEx(pLocaleName, LOCALE_SMONDECIMALSEP , &gc.stringResult, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sCurrencyDecimal), gc.stringResult, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }
    if (CallGetLocaleInfoEx(pLocaleName, LOCALE_SCURRENCY , &gc.stringResult, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sCurrency), gc.stringResult, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }


    // Numeric values
    ret &= CallGetLocaleInfoEx(pLocaleName, LOCALE_IDIGITS | LOCALE_RETURN_NUMBER           , &(gc.numfmt->cNumberDecimals), useUserOverride);
    _ASSERT(ret == TRUE);
    ret &= CallGetLocaleInfoEx(pLocaleName, LOCALE_ICURRDIGITS | LOCALE_RETURN_NUMBER       , &(gc.numfmt->cCurrencyDecimals), useUserOverride);
    _ASSERT(ret == TRUE);
    ret &= CallGetLocaleInfoEx(pLocaleName, LOCALE_ICURRENCY | LOCALE_RETURN_NUMBER         , &(gc.numfmt->cPosCurrencyFormat), useUserOverride);
    _ASSERT(ret == TRUE);
    ret &= CallGetLocaleInfoEx(pLocaleName, LOCALE_INEGCURR | LOCALE_RETURN_NUMBER          , &(gc.numfmt->cNegCurrencyFormat), useUserOverride);
    _ASSERT(ret == TRUE);
    ret &= CallGetLocaleInfoEx(pLocaleName, LOCALE_INEGNUMBER| LOCALE_RETURN_NUMBER         , &(gc.numfmt->cNegativeNumberFormat), useUserOverride);
    _ASSERT(ret == TRUE);
#ifndef FEATURE_CORECLR
    ret &= CallGetLocaleInfoEx(pLocaleName, LOCALE_IDIGITSUBSTITUTION | LOCALE_RETURN_NUMBER, &(gc.numfmt->iDigitSubstitution), useUserOverride);
    _ASSERT(ret == TRUE);
#endif

    // LOCALE_SNATIVEDIGITS (gc.tempArray of strings)
    if (GetNativeDigitsFromWin32(pLocaleName, &gc.tempArray, useUserOverride)) {
        SetObjectReference((OBJECTREF*)&(gc.numfmt->sNativeDigits), gc.tempArray, NULL);
    }
    else {
        ret = FALSE;
        _ASSERT(FALSE);
    }

    HELPER_METHOD_FRAME_END();
    FC_RETURN_BOOL(ret);
}
FCIMPLEND


////////////////////////////////////////////////////////////////////////
//
// Culture enumeration helper functions
//
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
//
// Enum values for System.Globalization.CultureTypes
//
////////////////////////////////////////////////////////////////////////////

// Neutral cultures are cultures like "en", "de", "zh", etc, for enumeration this includes ALL neutrals regardless of other flags
#define CULTURETYPES_NEUTRALCULTURES              0x0001

// Non-netural cultuers.  Examples are "en-us", "zh-tw", etc., for enumeration this includes ALL specifics regardless of other flags
#define CULTURETYPES_SPECIFICCULTURES             0x0002

// Win32 installed cultures in the system and exists in the framework too., this is effectively all cultures
#define CULTURETYPES_INSTALLEDWIN32CULTURES       0x0004

// User defined custom culture
#define CULTURETYPES_USERCUSTOMCULTURE            0x0008

// User defined replacement custom culture.
#define CULTURETYPES_REPLACEMENTCULTURES          0x0010
// [Obsolete("This value has been deprecated.  Please use other values in CultureTypes.")]
// Culture exists in Win32 but not in the Framework. // TODO: All cultures or no cultures?
#define CULTURETYPES_WINDOWSONLYCULTURES          0x0020
// [Obsolete("This value has been deprecated.  Please use other values in CultureTypes.")]
// the language tag match a culture that ships with the .NET framework, effectively all cultures since we get them from windows
#define CULTURETYPES_FRAMEWORKCULTURES            0x0040


const LPWSTR WHIDBEY_FRAMEWORK_CULTURE_LIST [] =
{
    W(""),
    W("af"),
    W("af-za"),
    W("ar"),
    W("ar-ae"),
    W("ar-bh"),
    W("ar-dz"),
    W("ar-eg"),
    W("ar-iq"),
    W("ar-jo"),
    W("ar-kw"),
    W("ar-lb"),
    W("ar-ly"),
    W("ar-ma"),
    W("ar-om"),
    W("ar-qa"),
    W("ar-sa"),
    W("ar-sy"),
    W("ar-tn"),
    W("ar-ye"),
    W("az"),
    W("az-cyrl-az"),
    W("az-latn-az"),
    W("be"),
    W("be-by"),
    W("bg"),
    W("bg-bg"),
    W("ca"),
    W("ca-es"),
    W("cs"),
    W("cs-cz"),
    W("da"),
    W("da-dk"),
    W("de"),
    W("de-at"),
    W("de-ch"),
    W("de-de"),
    W("de-li"),
    W("de-lu"),
    W("dv"),
    W("dv-mv"),
    W("el"),
    W("el-gr"),
    W("en"),
    W("en-029"),
    W("en-au"),
    W("en-bz"),
    W("en-ca"),
    W("en-gb"),
    W("en-ie"),
    W("en-jm"),
    W("en-nz"),
    W("en-ph"),
    W("en-tt"),
    W("en-us"),
    W("en-za"),
    W("en-zw"),
    W("es"),
    W("es-ar"),
    W("es-bo"),
    W("es-cl"),
    W("es-co"),
    W("es-cr"),
    W("es-do"),
    W("es-ec"),
    W("es-es"),
    W("es-gt"),
    W("es-hn"),
    W("es-mx"),
    W("es-ni"),
    W("es-pa"),
    W("es-pe"),
    W("es-pr"),
    W("es-py"),
    W("es-sv"),
    W("es-uy"),
    W("es-ve"),
    W("et"),
    W("et-ee"),
    W("eu"),
    W("eu-es"),
    W("fa"),
    W("fa-ir"),
    W("fi"),
    W("fi-fi"),
    W("fo"),
    W("fo-fo"),
    W("fr"),
    W("fr-be"),
    W("fr-ca"),
    W("fr-ch"),
    W("fr-fr"),
    W("fr-lu"),
    W("fr-mc"),
    W("gl"),
    W("gl-es"),
    W("gu"),
    W("gu-in"),
    W("he"),
    W("he-il"),
    W("hi"),
    W("hi-in"),
    W("hr"),
    W("hr-hr"),
    W("hu"),
    W("hu-hu"),
    W("hy"),
    W("hy-am"),
    W("id"),
    W("id-id"),
    W("is"),
    W("is-is"),
    W("it"),
    W("it-ch"),
    W("it-it"),
    W("ja"),
    W("ja-jp"),
    W("ka"),
    W("ka-ge"),
    W("kk"),
    W("kk-kz"),
    W("kn"),
    W("kn-in"),
    W("ko"),
    W("ko-kr"),
    W("kok"),
    W("kok-in"),
    W("ky"),
    W("ky-kg"),
    W("lt"),
    W("lt-lt"),
    W("lv"),
    W("lv-lv"),
    W("mk"),
    W("mk-mk"),
    W("mn"),
    W("mn-mn"),
    W("mr"),
    W("mr-in"),
    W("ms"),
    W("ms-bn"),
    W("ms-my"),
    W("nb-no"),
    W("nl"),
    W("nl-be"),
    W("nl-nl"),
    W("nn-no"),
    W("no"),
    W("pa"),
    W("pa-in"),
    W("pl"),
    W("pl-pl"),
    W("pt"),
    W("pt-br"),
    W("pt-pt"),
    W("ro"),
    W("ro-ro"),
    W("ru"),
    W("ru-ru"),
    W("sa"),
    W("sa-in"),
    W("sk"),
    W("sk-sk"),
    W("sl"),
    W("sl-si"),
    W("sq"),
    W("sq-al"),
    W("sr"),
    W("sr-cyrl-cs"),
    W("sr-latn-cs"),
    W("sv"),
    W("sv-fi"),
    W("sv-se"),
    W("sw"),
    W("sw-ke"),
    W("syr"),
    W("syr-sy"),
    W("ta"),
    W("ta-in"),
    W("te"),
    W("te-in"),
    W("th"),
    W("th-th"),
    W("tr"),
    W("tr-tr"),
    W("tt"),
    W("tt-ru"),
    W("uk"),
    W("uk-ua"),
    W("ur"),
    W("ur-pk"),
    W("uz"),
    W("uz-cyrl-uz"),
    W("uz-latn-uz"),
    W("vi"),
    W("vi-vn"),
    W("zh-chs"),
    W("zh-cht"),
    W("zh-cn"),
    W("zh-hans"),
    W("zh-hant"),
    W("zh-hk"),
    W("zh-mo"),
    W("zh-sg"),
    W("zh-tw")
};
#define WHIDBEY_FRAMEWORK_CULTURE_LIST_LENGTH (sizeof(WHIDBEY_FRAMEWORK_CULTURE_LIST) / sizeof(WHIDBEY_FRAMEWORK_CULTURE_LIST[0]))

////////////////////////////////////////////////////////////////////////////
//
//  NlsCompareInvariantNoCase
//
//  This routine does fast caseless comparison without needing the tables.
//  This helps us do the comparisons we need to load the tables :-)
//
//  Returns 0 if identical, <0 if pFirst if first string sorts first.
//
//  This is only intended to help with our locale name comparisons,
//  which are effectively limited to A-Z, 0-9, a-z and - where A-Z and a-z
//  compare as equal.
//
//  WARNING: [\]^_` will be less than A-Z because we make everything lower
//           case before comparing them.
//
//  When bNullEnd is TRUE, both of the strings should be null-terminator to be considered equal.
//  When bNullEnd is FALSE, the strings are considered equal when we reach the number of characters specifed by size
//  or when null terminators are reached, whichever happens first (strncmp-like behavior)
//
////////////////////////////////////////////////////////////////////////////

int NlsCompareInvariantNoCase(
    LPCWSTR pFirst,
    LPCWSTR pSecond,
    int     size,
    BOOL bNullEnd)
{
    int i=0;
    WCHAR first;
    WCHAR second;

    for (;
         size > 0 && (first = *pFirst) != 0 && (second = *pSecond) != 0;
         size--, pFirst++, pSecond++)
    {
        // Make them lower case
        if ((first >= 'A') && (first <= 'Z')) first |= 0x20;
        if ((second >= 'A') && (second <= 'Z')) second |= 0x20;

        // Get the diff
        i = (first - second);

        // Are they the same?
        if (i == 0)
            continue;

        // Otherwise the difference.  Remember we made A-Z into lower case, so
        // the characters [\]^_` will sort < A-Z and also < a-z.  (Those are the
        // characters between A-Z and a-Z in ascii)
        return i;
    }

    // When we are here, one of these holds:
    //    size == 0
    //    or one of the strings has a null terminator
    //    or both of the string reaches null terminator

    if (bNullEnd || size != 0)
    {
        // If bNullEnd is TRUE, always check for null terminator.
        // If bNullEnd is FALSE, we still have to check if one of the strings is terminated eariler
        // than another (hense the size != 0 check).

        // See if one string ended first
        if (*pFirst != 0 || *pSecond != 0)
        {
            // Which one?
            return *pFirst == 0 ? -1 : 1;
        }
    }

    // Return our difference (0)
    return i;
}

BOOL IsWhidbeyFrameworkCulture(__in LPCWSTR lpLocaleString)
{
    int iBottom = 0;
    int iTop = WHIDBEY_FRAMEWORK_CULTURE_LIST_LENGTH - 1;

    // Do a binary search for our name
    while (iBottom <= iTop)
    {
        int     iMiddle = (iBottom + iTop) / 2;
        int     result = NlsCompareInvariantNoCase(lpLocaleString, WHIDBEY_FRAMEWORK_CULTURE_LIST[iMiddle], LOCALE_NAME_MAX_LENGTH, TRUE);
        if (result == 0)
        {
            return TRUE;
        }
        if (result < 0)
        {
            // pLocaleName was < pTest
            iTop = iMiddle - 1;
        }
        else
        {
            // pLocaleName was > pTest
            iBottom = iMiddle + 1;
        }
    }

    return FALSE;
}

// Just Check to see if the OS thinks it is a valid locle
BOOL WINAPI IsOSValidLocaleName(__in LPCWSTR  lpLocaleName, bool bIsNeutralLocale)
{
#ifndef ENABLE_DOWNLEVEL_FOR_NLS
    return ::IsValidLocaleName(lpLocaleName);
#else
    BOOL IsWindows7 = NewApis::IsWindows7Platform();
    // if we're < Win7, we didn't know about neutrals or the invariant.
    if (!IsWindows7 && (bIsNeutralLocale || (lpLocaleName[0] == 0)))
    {
        return false;
    }

    // Work around the name/lcid thingy (can't just link to ::IsValidLocaleName())
    LCID lcid = NewApis::LocaleNameToLCID(lpLocaleName, 0);

    if (IsCustomCultureId(lcid))
    {
        return false;
    }

    if (bIsNeutralLocale)
    {
        // In this case, we're running on Windows 7.
        // For neutral locales, use GetLocaleInfoW.
        // If GetLocaleInfoW works, then the OS knows about it.
        return (::GetLocaleInfoW(lcid, LOCALE_ILANGUAGE, NULL, 0) != 0);
    }

    // This is not a custom locale.
    // Call IsValidLocale() to check if the LCID is installed.
    // IsValidLocale doesn't work for neutral locales.
    return IsValidLocale(lcid, LCID_INSTALLED);
#endif
}


////////////////////////////////////////////////////////////////////////////
//
// Check the dwFlags, which has the 'attributes' of the locale, and decide
// if the locale should be included in the enumeration based on
// the desired CultureTypes.
//
////////////////////////////////////////////////////////////////////////////

BOOL ShouldIncludeByCultureType(INT32 cultureTypes, LPCWSTR lpLocaleString, INT32 dwFlags)
{

    if ((cultureTypes & CULTURETYPES_NEUTRALCULTURES) &&
         ((dwFlags & LOCALE_NEUTRALDATA) || (lpLocaleString[0] == 0))) // Invariant culture get enumerated with the neutrals
    {
        return TRUE;
    }

    if ((cultureTypes & CULTURETYPES_SPECIFICCULTURES) &&
        ((dwFlags & LOCALE_SPECIFICDATA) && (lpLocaleString[0] != 0))) // Invariant culture does not get enumerated with the specifics
    {
        return TRUE;
    }

    if (cultureTypes & CULTURETYPES_INSTALLEDWIN32CULTURES)
    {
        // The user asks for installed Win32 culture, so check
        // if this locale is installed. In W7 and above, when ::IsValidLocaleName()
        // returns true, it means that it is installed.
        // In downlevel (including Vista), we will convert the name to LCID.
        // When the LCID is not a custom locale, we will call ::IsValidLocale(.., LCID_INSTALLED)
        // to verify if the locale is installed.
        // In Vista, we treat custom locale as installed.
        if (IsOSValidLocaleName(lpLocaleString, (dwFlags & LOCALE_NEUTRALDATA) == LOCALE_NEUTRALDATA))
        {
            return TRUE;
        }
    }

    if ((cultureTypes & CULTURETYPES_USERCUSTOMCULTURE) &&
        (dwFlags & LOCALE_SUPPLEMENTAL))
    {
        return TRUE;
    }

    if ((cultureTypes & CULTURETYPES_REPLACEMENTCULTURES) &&
        (dwFlags & LOCALE_REPLACEMENT))
    {
        return TRUE;
    }

    if ((cultureTypes & CULTURETYPES_FRAMEWORKCULTURES) &&
         IsWhidbeyFrameworkCulture(lpLocaleString))
    {
        return TRUE;
    }

    //
    // No need to check CULTURETYPES_WINDOWSONLYCULTURES and CULTURETYPES_FRAMEWORKCULTURES
    // since they are deprecated, and they are handled in the managed code before calling
    // nativeEnumCultureNames.
    //

    return FALSE;
}

////////////////////////////////////////////////////////////////////////////
//
// Struct to hold context to be used in the callback for
// EnumLocaleProcessingCallback
//
////////////////////////////////////////////////////////////////////////////

typedef struct
{
    PTRARRAYREF pCultureNamesArray;
    INT32 count;
    INT32 cultureTypes;
} ENUM_LOCALE_DATA;

////////////////////////////////////////////////////////////////////////////
//
// Callback for NewApis::EnumSystemLocalesEx to count the number of
// locales to be enumerated.
//
////////////////////////////////////////////////////////////////////////////

BOOL CALLBACK EnumLocaleCountCallback(__in_z LPCWSTR lpLocaleString, __in DWORD dwFlags, __in LPARAM lParam)
{
    ENUM_LOCALE_DATA* pData = (ENUM_LOCALE_DATA*)lParam;

    if (ShouldIncludeByCultureType(pData->cultureTypes, lpLocaleString, dwFlags))
    {
        (pData->count)++;
    }
    return TRUE;
}


////////////////////////////////////////////////////////////////////////////
//
// Callback for NewApis::EnumSystemLocalesEx to add the locale name
// into the allocated managed string array.
//
////////////////////////////////////////////////////////////////////////////

BOOL CALLBACK EnumLocaleProcessingCallback(__in_z LPCWSTR lpLocaleString, __in DWORD dwFlags, __in LPARAM lParam)
{
    ENUM_LOCALE_DATA* pData = (ENUM_LOCALE_DATA*)lParam;

    if (ShouldIncludeByCultureType(pData->cultureTypes, lpLocaleString, dwFlags))
    {
        GCX_COOP();

        GCPROTECT_BEGIN(pData->pCultureNamesArray);

        OBJECTREF cultureString = (OBJECTREF) StringObject::NewString(lpLocaleString);
        pData->pCultureNamesArray->SetAt(pData->count, cultureString);
        pData->count++;

        GCPROTECT_END();
    }

    return TRUE;
}


////////////////////////////////////////////////////////////////////////////
//
// Called by CultureData.GetCultures() to enumerate the names of cultures.
// It first calls NewApis::EnumSystemLocalesEx to count the number of
// locales to be enumerated. And it will allocate an managed string
// array with the count. And fill the array with the culture names in
// the 2nd call to NewAPis::EnumSystemLocalesEx.
//
////////////////////////////////////////////////////////////////////////////


int QCALLTYPE COMNlsInfo::nativeEnumCultureNames(INT32 cultureTypes, QCall::ObjectHandleOnStack retStringArray)
{
    CONTRACTL
    {
        QCALL_CHECK;
        // Check CultureTypes.WindowsOnlyCultures and CultureTYpes.FrameworkCultures are deprecated and is
        // handled in the managed code side to provide fallback behavior.
        //PRECONDITION((cultureTypes & (CULTURETYPES_WINDOWSONLYCULTURES | CULTURETYPES_FRAMEWORKCULTURES) == 0));
    } CONTRACTL_END;


    int result;
    DWORD dwFlags = 0;
    PTRARRAYREF cultureNamesArray = NULL;
    ENUM_LOCALE_DATA enumData = { NULL, 0, cultureTypes};

    BEGIN_QCALL;

    //
    // if CultureTypes.FrameworkCulture is specified we'll enumerate all cultures
    // and filter according to the Whidbey framework culture list (for compatibility)
    //

    if (cultureTypes & CULTURETYPES_FRAMEWORKCULTURES)
    {
        dwFlags |= LOCALE_NEUTRALDATA | LOCALE_SPECIFICDATA;
    }

    // Map CultureTypes to Windows enumeration values.
    if (cultureTypes & CULTURETYPES_NEUTRALCULTURES)
    {
        dwFlags |= LOCALE_NEUTRALDATA;
    }

    if (cultureTypes & CULTURETYPES_SPECIFICCULTURES)
    {
        dwFlags |= LOCALE_SPECIFICDATA;
    }

    if (cultureTypes & CULTURETYPES_INSTALLEDWIN32CULTURES)
    {
        // Windows 7 knows about neutrals, whereas Vista and lower don't.
        if (NewApis::IsWindows7Platform())
        {
            dwFlags |= LOCALE_SPECIFICDATA | LOCALE_NEUTRALDATA;
        }
        else
        {
            dwFlags |= LOCALE_SPECIFICDATA;
        }
    }

    dwFlags |= (cultureTypes & CULTURETYPES_USERCUSTOMCULTURE) ? LOCALE_SUPPLEMENTAL: 0;

    // We need special handling for Replacement cultures because Windows does not have a way to enumerate it directly.
    // Replacement locale check will be only used when CultureTypes.SpecificCultures is NOT used.
    dwFlags |= (cultureTypes & CULTURETYPES_REPLACEMENTCULTURES) ? LOCALE_SPECIFICDATA | LOCALE_NEUTRALDATA: 0;


    result = NewApis::EnumSystemLocalesEx((LOCALE_ENUMPROCEX)EnumLocaleCountCallback, dwFlags, (LPARAM)&enumData, NULL) == TRUE ? 1 : 0;

    if (result)
    {

        GCX_COOP();

        GCPROTECT_BEGIN(cultureNamesArray);

        // Now we need to allocate our culture names string array and populate it
        // Get our array object (will throw, don't have to check it)
        cultureNamesArray = (PTRARRAYREF) AllocateObjectArray(enumData.count, g_pStringClass);

        // In the context struct passed to EnumSystemLocalesEx, reset the count and assign the newly allocated string array
        // to hold culture names to be enumerated.
        enumData.count = 0;
        enumData.pCultureNamesArray = cultureNamesArray;

        result = NewApis::EnumSystemLocalesEx((LOCALE_ENUMPROCEX)EnumLocaleProcessingCallback, dwFlags, (LPARAM)&enumData, NULL);

        if (result)
        {
            retStringArray.Set(cultureNamesArray);
        }
        GCPROTECT_END();
    }
    END_QCALL

    return result;

}

//
// InternalCompareString is used in the managed side to handle the synthetic CompareInfo methods (IndexOf, LastIndexOf, IsPrfix, and IsSuffix)
//
INT32 QCALLTYPE COMNlsInfo::InternalCompareString(
    INT_PTR handle,
    INT_PTR handleOrigin,
    LPCWSTR localeName,
    LPCWSTR string1, INT32 offset1, INT32 length1,
    LPCWSTR string2, INT32 offset2, INT32 length2,
    INT32 flags)
{
    CONTRACTL
    {
        QCALL_CHECK;
        PRECONDITION(CheckPointer(string1));
        PRECONDITION(CheckPointer(string2));
        PRECONDITION(CheckPointer(localeName));
    } CONTRACTL_END;

    INT32 result = 1;
    BEGIN_QCALL;

    handle = EnsureValidSortHandle(handle, handleOrigin, localeName);

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();

    if(!(curDomain->m_bUseOsSorting))
    {
        result = SortVersioning::SortDllCompareString((SortVersioning::PSORTHANDLE) handle, flags, &string1[offset1], length1, &string2[offset2], length2, NULL, 0);
    }
    else if (curDomain->m_pCustomSortLibrary != NULL) {
        result = (curDomain->m_pCustomSortLibrary->pCompareStringEx)(handle != NULL ? NULL : localeName, flags, &string1[offset1], length1, &string2[offset2], length2, NULL, NULL, (LPARAM) handle);
    } 
    else 
#endif
    {
        result = NewApis::CompareStringEx(handle != NULL ? NULL : localeName, flags, &string1[offset1], length1, &string2[offset2], length2,NULL,NULL, (LPARAM) handle);
    }

    switch (result)
    {
        case CSTR_LESS_THAN:
            result = -1;
            break;

        case CSTR_EQUAL:
            result = 0;
            break;

        case CSTR_GREATER_THAN:
            result = 1;
            break;

        case 0:
        default:
            _ASSERTE(!"catastrophic failure calling NewApis::CompareStringEx!  This could be a CultureInfo, RegionInfo, or Calendar bug (bad localeName string) or maybe a GCHole.");
            break;
    }

    END_QCALL;
    return result;
}

////////////////////////////////////////////////////////////////////////////
//
//  UseConstantSpaceHashAlgorithm
//  Check for the DWORD "NetFx45_CultureAwareComparerGetHashCode_LongStrings" CLR config option.
//
// .Net 4.5 introduces an opt-in algorithm for determining the hash code of strings that
// uses a constant amount of memory instead of memory proportional to the size of the string
//
// A non-zero value will enable the new algorithm:
//
// 1) Config file (MyApp.exe.config)
//        <?xml version ="1.0"?>
//        <configuration>
//         <runtime>
//          <NetFx45_CultureAwareComparerGetHashCode_LongStrings enabled="1"/>
//         </runtime>
//        </configuration>
// 2) Environment variable
//        set NetFx45_CultureAwareComparerGetHashCode_LongStrings=1
// 3) RegistryKey
//        [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework]
//        "NetFx45_CultureAwareComparerGetHashCode_LongStrings"=dword:00000001
//
////////////////////////////////////////////////////////////////////////////
BOOL UseConstantSpaceHashAlgorithm()
{
    static bool configChecked = false;
    static BOOL useConstantSpaceHashAlgorithm = FALSE;

    if(!configChecked)
    {
        BEGIN_SO_INTOLERANT_CODE_NO_THROW_CHECK_THREAD(return false);
        useConstantSpaceHashAlgorithm = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_NetFx45_CultureAwareComparerGetHashCode_LongStrings) != 0;
        END_SO_INTOLERANT_CODE;

        configChecked = true;
    }
    return useConstantSpaceHashAlgorithm;
}



////////////////////////////////////////////////////////////////////////////
//
//  InternalGetGlobalizedHashCode
//
////////////////////////////////////////////////////////////////////////////
INT32 QCALLTYPE COMNlsInfo::InternalGetGlobalizedHashCode(INT_PTR handle, INT_PTR handleOrigin, LPCWSTR localeName, LPCWSTR string, INT32 length, INT32 dwFlagsIn, BOOL bForceRandomizedHashing, INT64 additionalEntropy)
{
    CONTRACTL
    {
        QCALL_CHECK;
        PRECONDITION(CheckPointer(localeName));
        PRECONDITION(CheckPointer(string, NULL_OK));
    } CONTRACTL_END;

    INT32  iReturnHash  = 0;
    BEGIN_QCALL;

    handle = EnsureValidSortHandle(handle, handleOrigin, localeName);
    int byteCount = 0;

    //
    //  Make sure there is a string.
    //
    if (!string) {
        COMPlusThrowArgumentNull(W("string"),W("ArgumentNull_String"));
    }

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();
#endif // FEATURE_CORECLR

    if(length > 0 && UseConstantSpaceHashAlgorithm() 
    // Note that we can't simply do the hash without the entropy and then try to add it after the fact we need the hash function itself to pass entropy to its inputs.
#ifdef FEATURE_RANDOMIZED_STRING_HASHING
         && !bForceRandomizedHashing
#ifndef FEATURE_CORECLR
         && !curDomain->m_pNlsHashProvider->GetUseRandomHashing()
#else
         && !COMNlsHashProvider::s_NlsHashProvider.GetUseRandomHashing()
#endif // FEATURE_CORECLR
#endif // FEATURE_RANDOMIZED_STRING_HASHING
       )
    {
#ifndef FEATURE_CORECLR
        if(!(curDomain->m_bUseOsSorting))
        {
            iReturnHash=SortVersioning::SortDllGetHashCode((SortVersioning::PSORTHANDLE) handle, dwFlagsIn, string, length, NULL, 0);
        }
        else
#endif
        {
            int iRes = 0;
            int iHashValue = 0;

#ifndef FEATURE_CORECLR
            if (curDomain->m_pCustomSortLibrary != NULL)
            {
                iRes = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(localeName, dwFlagsIn | LCMAP_HASH, string, length, (LPWSTR) &iHashValue, sizeof(INT32), NULL, NULL, 0);
            }
            else
#endif
            {
                iRes = NewApis::LCMapStringEx(localeName, dwFlagsIn | LCMAP_HASH, string, length, (LPWSTR) &iHashValue, sizeof(INT32), NULL, NULL, 0);
            }

            if(iRes != 0)
            {
                iReturnHash = iHashValue;
            }
        }
    }

    if(iReturnHash == 0)
    {
        DWORD dwFlags = (LCMAP_SORTKEY | dwFlagsIn);

        //
        // Caller has already verified that the string is not of zero length
        //
        // Assert if we might hit an AV in LCMapStringEx for the invariant culture.
        _ASSERTE(length > 0 || (dwFlags & LCMAP_LINGUISTIC_CASING) == 0);
#ifndef FEATURE_CORECLR
        if(!(curDomain->m_bUseOsSorting))
        {
            byteCount=SortVersioning::SortDllGetSortKey((SortVersioning::PSORTHANDLE) handle, dwFlagsIn, string, length, NULL, 0, NULL, 0);
        }
        else if (curDomain->m_pCustomSortLibrary != NULL)
        {
            byteCount = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : localeName, dwFlags, string, length, NULL, 0, NULL, NULL, (LPARAM) handle);
        }
        else
#endif
        {
            byteCount=NewApis::LCMapStringEx(handle != NULL ? NULL : localeName, dwFlags, string, length, NULL, 0, NULL, NULL, (LPARAM) handle);
        }

        //A count of 0 indicates that we either had an error or had a zero length string originally.
        if (byteCount==0) {
            COMPlusThrow(kArgumentException, W("Arg_MustBeString"));
        }

        // We used to use a NewArrayHolder here, but it turns out that hurts our large # process
        // scalability in ASP.Net hosting scenarios, using the quick bytes instead mostly stack
        // allocates and ups throughput by 8% in 100 process case, 5% in 1000 process case
        {
            CQuickBytesSpecifySize<MAX_STRING_VALUE * sizeof(WCHAR)> qbBuffer;
            BYTE* pByte = (BYTE*)qbBuffer.AllocThrows(byteCount);

#ifndef FEATURE_CORECLR
            if(!(curDomain->m_bUseOsSorting))
            {
                SortVersioning::SortDllGetSortKey((SortVersioning::PSORTHANDLE) handle, dwFlagsIn, string, length, pByte, byteCount, NULL, 0);
            }
            else if(curDomain->m_pCustomSortLibrary != NULL)
            {
                (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : localeName, dwFlags, string, length, (LPWSTR)pByte, byteCount, NULL, NULL, (LPARAM) handle);
            }
            else
#endif
            {
                NewApis::LCMapStringEx(handle != NULL ? NULL : localeName, dwFlags, string, length, (LPWSTR)pByte, byteCount, NULL,NULL, (LPARAM) handle);
            }

#ifndef FEATURE_CORECLR
            iReturnHash = curDomain->m_pNlsHashProvider->HashSortKey(pByte, byteCount, bForceRandomizedHashing, additionalEntropy);
#else
            iReturnHash = COMNlsHashProvider::s_NlsHashProvider.HashSortKey(pByte, byteCount, bForceRandomizedHashing, additionalEntropy);
#endif // FEATURE_CORECLR
        }
    }
    END_QCALL;
    return(iReturnHash);
}

#ifndef FEATURE_CORECLR // FCalls used by System.TimeZone

FCIMPL0(LONG, COMNlsInfo::nativeGetTimeZoneMinuteOffset)
{
    FCALL_CONTRACT;

    TIME_ZONE_INFORMATION timeZoneInfo;

    GetTimeZoneInformation(&timeZoneInfo);

    //
    // In Win32, UTC = local + offset.  So for Pacific Standard Time, offset = 8.
    // In NLS+, Local time = UTC + offset. So for PST, offset = -8.
    // So we have to reverse the sign here.
    //
    return (timeZoneInfo.Bias * -1);
}
FCIMPLEND

FCIMPL0(Object*, COMNlsInfo::nativeGetStandardName)
{
    FCALL_CONTRACT;

    STRINGREF refRetVal = NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_1(refRetVal);

    TIME_ZONE_INFORMATION timeZoneInfo;
    GetTimeZoneInformation(&timeZoneInfo);

    refRetVal = StringObject::NewString(timeZoneInfo.StandardName);

    HELPER_METHOD_FRAME_END();
    return OBJECTREFToObject(refRetVal);
}
FCIMPLEND

FCIMPL0(Object*, COMNlsInfo::nativeGetDaylightName)
{
    FCALL_CONTRACT;

    STRINGREF refRetVal = NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_1(refRetVal);

    TIME_ZONE_INFORMATION timeZoneInfo;
    GetTimeZoneInformation(&timeZoneInfo);
    // Instead of returning null when daylight saving is not used, now we return the same result as the OS.
    //In this case, if daylight saving time is used, the standard name is returned.

#if 0
    if (result == TIME_ZONE_ID_UNKNOWN || timeZoneInfo.DaylightDate.wMonth == 0) {
        // If daylight saving time is not used in this timezone, return null.
        //
        // Windows NT/2000: TIME_ZONE_ID_UNKNOWN is returned if daylight saving time is not used in
        // the current time zone, because there are no transition dates.
        //
        // For Windows 9x, a zero in the wMonth in DaylightDate means daylight saving time
        // is not specified.
        //
        // If the current timezone uses daylight saving rule, but user unchekced the
        // "Automatically adjust clock for daylight saving changes", the value
        // for DaylightBias will be 0.
        return (I2ARRAYREF)NULL;
    }
#endif  // 0

    refRetVal = StringObject::NewString(timeZoneInfo.DaylightName);

    HELPER_METHOD_FRAME_END();
    return OBJECTREFToObject(refRetVal);
}
FCIMPLEND

FCIMPL1(Object*, COMNlsInfo::nativeGetDaylightChanges, int year)
{
    FCALL_CONTRACT;

    I2ARRAYREF pResultArray = NULL;
    HELPER_METHOD_FRAME_BEGIN_RET_1(pResultArray);

    TIME_ZONE_INFORMATION timeZoneInfo;
    DWORD result = GetTimeZoneInformation(&timeZoneInfo);

    if (result == TIME_ZONE_ID_UNKNOWN || timeZoneInfo.DaylightBias == 0
        || timeZoneInfo.DaylightDate.wMonth == 0
        ) {
        // If daylight saving time is not used in this timezone, return null.
        //
        // If the current timezone uses daylight saving rule, but user unchekced the
        // "Automatically adjust clock for daylight saving changes", the value
        // for DaylightBias will be 0.
        goto lExit;
    }

    pResultArray = (I2ARRAYREF)AllocatePrimitiveArray(ELEMENT_TYPE_I2, 17);

    //
    // The content of timeZoneInfo.StandardDate is 8 words, which
    // contains year, month, day, dayOfWeek, hour, minute, second, millisecond.
    //
    memcpyNoGCRefs(pResultArray->m_Array,
            (LPVOID)&timeZoneInfo.DaylightDate,
            8 * sizeof(INT16));

    //
    // The content of timeZoneInfo.DaylightDate is 8 words, which
    // contains year, month, day, dayOfWeek, hour, minute, second, millisecond.
    //
    memcpyNoGCRefs(((INT16*)pResultArray->m_Array) + 8,
            (LPVOID)&timeZoneInfo.StandardDate,
            8 * sizeof(INT16));

    ((INT16*)pResultArray->m_Array)[16] = (INT16)timeZoneInfo.DaylightBias * -1;

lExit: ;
    HELPER_METHOD_FRAME_END();
    return OBJECTREFToObject(pResultArray);
}
FCIMPLEND

#endif // FEATURE_CORECLR

inline BOOL IsInvariantLocale(STRINGREF localeName)
{
   return localeName->GetStringLength() == 0;
}

// InternalChangeCaseChar
//
// Call LCMapStringEx with a char to make it upper or lower case
// Note that if the locale is English or Invariant we'll try just mapping it if its < 0x7f
FCIMPL5(FC_CHAR_RET, COMNlsInfo::InternalChangeCaseChar,
        INT_PTR handle, // optional sort handle
        INT_PTR handleOrigin, StringObject* localeNameUNSAFE, CLR_CHAR wch, CLR_BOOL bIsToUpper)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(localeNameUNSAFE));
    } CONTRACTL_END;

    CLR_CHAR retVal = '\0';
    int ret_LCMapStringEx = -1;

    // Dereference our string
    STRINGREF localeName(localeNameUNSAFE);

    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(localeName);

    BOOL isInvariantLocale = IsInvariantLocale(localeName);
    // Check for Invariant to avoid A/V in LCMapStringEx
    DWORD linguisticCasing = (isInvariantLocale) ? 0 : LCMAP_LINGUISTIC_CASING;

    handle = EnsureValidSortHandle(handle, handleOrigin, localeName->GetBuffer());

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();

    //For a versioned sort, Invariant should still use the OS
    if(!(curDomain->m_bUseOsSorting) && !isInvariantLocale)
    {
        ret_LCMapStringEx = SortVersioning::SortDllChangeCase((SortVersioning::PSORTHANDLE) handle,
                                    bIsToUpper?LCMAP_UPPERCASE | linguisticCasing:
                                               LCMAP_LOWERCASE | linguisticCasing,
                                    &wch,
                                    1,
                                    &retVal,
                                    1,
                                    NULL, 0);
    }
    else if(curDomain->m_pCustomSortLibrary != NULL)
    {
        ret_LCMapStringEx = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : localeName->GetBuffer(),
                                   bIsToUpper?LCMAP_UPPERCASE | linguisticCasing:
                                              LCMAP_LOWERCASE | linguisticCasing,
                                   &wch,
                                   1,
                                   &retVal,
                                   1,
                                   NULL,
                                   NULL,
                                   (LPARAM) handle);
    }
    else
#endif    
    {
        ret_LCMapStringEx = NewApis::LCMapStringEx(handle != NULL ? NULL : localeName->GetBuffer(),
                                   bIsToUpper?LCMAP_UPPERCASE | linguisticCasing:
                                              LCMAP_LOWERCASE | linguisticCasing,
                                   &wch,
                                   1,
                                   &retVal,
                                   1,
                                   NULL,
                                   NULL,
                                   (LPARAM) handle);
    }

    if (0 == ret_LCMapStringEx)
    {
        // return value of 0 indicates failure and error value is supposed to be set.
        // shouldn't ever really happen
        _ASSERTE(!"catastrophic failure calling NewApis::InternalChangeCaseChar!  This could be a CultureInfo or CompareInfo bug (bad localeName string) or maybe a GCHole.");
    }

    HELPER_METHOD_FRAME_END(); // localeName is now unprotected
    return retVal;
}
FCIMPLEND

// InternalChangeCaseString
//
// Call LCMapStringEx with a string to make it upper or lower case
// Note that if the locale is English or Invariant we'll try just mapping it if its < 0x7f
//
// We typically expect the output string to be the same size as the input.  If not
// we have to count, reallocate the output buffer, and try again.
FCIMPL5(Object*, COMNlsInfo::InternalChangeCaseString,
        INT_PTR handle, // optional sort handle
        INT_PTR handleOrigin, StringObject* localeNameUNSAFE, StringObject* pStringUNSAFE, CLR_BOOL bIsToUpper)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(pStringUNSAFE));
        PRECONDITION(CheckPointer(localeNameUNSAFE));
    } CONTRACTL_END;

    struct _gc
    {
        STRINGREF pResult;
        STRINGREF pString;
        STRINGREF pLocale;
    } gc;

    gc.pResult = NULL;
    gc.pString = ObjectToSTRINGREF(pStringUNSAFE);
    gc.pLocale = ObjectToSTRINGREF(localeNameUNSAFE);

    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc)

    handle = EnsureValidSortHandle(handle, handleOrigin, gc.pLocale->GetBuffer());

    //
    //  Get the length of the string.
    //
    int nLengthInput = gc.pString->GetStringLength();
    int nLengthOutput = nLengthInput; //  initially we assume the string length does not change.

    BOOL isInvariantLocale = IsInvariantLocale(gc.pLocale);
    // Check for Invariant to avoid A/V in LCMapStringEx
    DWORD linguisticCasing = (isInvariantLocale) ? 0 : LCMAP_LINGUISTIC_CASING;
    // Check for Invariant to avoid A/V in LCMapStringEx

    //
    //  Check if we have the empty string.
    //
    if (nLengthInput == 0)
    {
        gc.pResult = ObjectToSTRINGREF(gc.pString);
    }
    else
    {
        //
        //  Create the result string.
        //
        gc.pResult = StringObject::NewString(nLengthOutput);
        LPWSTR pResultStr = gc.pResult->GetBuffer();

        int result;
#ifndef FEATURE_CORECLR
        AppDomain* curDomain = GetAppDomain();

        //Invariant should always use OS
        if(!(curDomain->m_bUseOsSorting) && !isInvariantLocale)
        {
            result = SortVersioning::SortDllChangeCase((SortVersioning::PSORTHANDLE) handle,
                                        bIsToUpper?LCMAP_UPPERCASE | linguisticCasing:
                                                   LCMAP_LOWERCASE | linguisticCasing,
                                        gc.pString->GetBuffer(),
                                        nLengthInput,
                                        pResultStr,
                                        nLengthOutput,
                                        NULL, 0);
        }
        else if(curDomain->m_pCustomSortLibrary != NULL)
        {
            result = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : (gc.pLocale)->GetBuffer(),
                                       bIsToUpper?LCMAP_UPPERCASE | linguisticCasing :
                                                  LCMAP_LOWERCASE | linguisticCasing,
                                       gc.pString->GetBuffer(),
                                       nLengthInput,
                                       pResultStr,
                                       nLengthOutput,
                                       NULL,
                                       NULL,
                                       (LPARAM) handle);
        }
        else
#endif
        {
            result = NewApis::LCMapStringEx(handle != NULL ? NULL : (gc.pLocale)->GetBuffer(),
                                       bIsToUpper?LCMAP_UPPERCASE | linguisticCasing :
                                                  LCMAP_LOWERCASE | linguisticCasing,
                                       gc.pString->GetBuffer(),
                                       nLengthInput,
                                       pResultStr,
                                       nLengthOutput,
                                       NULL,
                                       NULL,
                                       (LPARAM) handle);
        }

        if(0 == result)
        {
            // Failure: Detect if that's due to insufficient buffer
            if (GetLastError()!= ERROR_INSUFFICIENT_BUFFER)
            {
                ThrowLastError();
            }
            // need to update buffer
#ifndef FEATURE_CORECLR
            //Invariant should always use OS
            if(!(curDomain->m_bUseOsSorting) && !IsInvariantLocale(gc.pLocale))
            {
                nLengthOutput = SortVersioning::SortDllChangeCase((SortVersioning::PSORTHANDLE) handle,
                                            bIsToUpper?LCMAP_UPPERCASE | linguisticCasing:
                                                       LCMAP_LOWERCASE | linguisticCasing,
                                            gc.pString->GetBuffer(),
                                            nLengthInput,
                                            NULL,
                                            0,
                                            NULL, 0);
            }
            else if(curDomain->m_pCustomSortLibrary != NULL)
            {
                nLengthOutput = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : (gc.pLocale)->GetBuffer(),
                                                        bIsToUpper?LCMAP_UPPERCASE | linguisticCasing :
                                                                   LCMAP_LOWERCASE | linguisticCasing,
                                                        gc.pString->GetBuffer(),
                                                        nLengthInput,
                                                        NULL,
                                                        0,
                                                        NULL,
                                                        NULL,
                                                        (LPARAM) handle);
            }
            else
#endif
            {
                nLengthOutput = NewApis::LCMapStringEx(handle != NULL ? NULL : (gc.pLocale)->GetBuffer(),
                                                        bIsToUpper?LCMAP_UPPERCASE | linguisticCasing :
                                                                   LCMAP_LOWERCASE | linguisticCasing,
                                                        gc.pString->GetBuffer(),
                                                        nLengthInput,
                                                        NULL,
                                                        0,
                                                        NULL,
                                                        NULL,
                                                        (LPARAM) handle);
            }
            if (nLengthOutput == 0)
            {
                // return value of 0 indicates failure and error value is supposed to be set.
                // shouldn't ever really happen
                _ASSERTE(!"catastrophic failure calling NewApis::InternalChangeCaseString!  This could be a CultureInfo or CompareInfo bug (bad localeName string) or maybe a GCHole.");
            }
            _ASSERTE(nLengthOutput > 0);
            // NOTE: The length of the required buffer does not include the terminating null character.
            // So it can be used as-is for our calculations -- the length we pass in to NewString also does
            // not include the terminating null character.
            // MSDN documentation could be interpreted to mean that the length returned includes the terminating
            // NULL character, but that's not the case.

            // NOTE: Also note that we let the GC take care of the previously allocated pResult.

            gc.pResult = StringObject::NewString(nLengthOutput);
            pResultStr = gc.pResult->GetBuffer();
#ifndef FEATURE_CORECLR
            //Invariant should always use OS
            if(!(curDomain->m_bUseOsSorting) && !IsInvariantLocale(gc.pLocale))
            {
                result = SortVersioning::SortDllChangeCase((SortVersioning::PSORTHANDLE) handle,
                                            bIsToUpper?LCMAP_UPPERCASE | linguisticCasing:
                                                       LCMAP_LOWERCASE | linguisticCasing,
                                            gc.pString->GetBuffer(),
                                            nLengthInput,
                                            pResultStr,
                                            nLengthOutput,
                                            NULL, 0);
            }
            else if(curDomain->m_pCustomSortLibrary != NULL)
            {
                result = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : (gc.pLocale)->GetBuffer(),
                                           bIsToUpper?LCMAP_UPPERCASE | linguisticCasing :
                                                      LCMAP_LOWERCASE | linguisticCasing,
                                           gc.pString->GetBuffer(),
                                           nLengthInput,
                                           pResultStr,
                                           nLengthOutput,
                                           NULL,
                                           NULL,
                                           (LPARAM) handle);
            }
            else
#endif
            {
                result = NewApis::LCMapStringEx(handle != NULL ? NULL : (gc.pLocale)->GetBuffer(),
                                           bIsToUpper?LCMAP_UPPERCASE | linguisticCasing :
                                                      LCMAP_LOWERCASE | linguisticCasing,
                                           gc.pString->GetBuffer(),
                                           nLengthInput,
                                           pResultStr,
                                           nLengthOutput,
                                           NULL,
                                           NULL,
                                           (LPARAM) handle);
            }

            if(0 == result)
            {
                // return value of 0 indicates failure and error value is supposed to be set.
                // shouldn't ever really happen
                _ASSERTE(!"catastrophic failure calling NewApis::InternalChangeCaseString!  This could be a CultureInfo or CompareInfo bug (bad localeName string) or maybe a GCHole.");
            }
        }

        pResultStr[nLengthOutput] = 0;
    }

    HELPER_METHOD_FRAME_END();

    return OBJECTREFToObject(gc.pResult);
}
FCIMPLEND

/*================================InternalGetCaseInsHash================================
**Action:
**Returns:
**Arguments:
**Exceptions:
==============================================================================*/
FCIMPL6(INT32, COMNlsInfo::InternalGetCaseInsHash,
        INT_PTR handle, // optional sort handle
        INT_PTR handleOrigin, StringObject* localeNameUNSAFE, LPVOID pvStrA, CLR_BOOL bForceRandomizedHashing, INT64 additionalEntropy)
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(localeNameUNSAFE));
        PRECONDITION(CheckPointer(pvStrA));
    } CONTRACTL_END;

    STRINGREF localeName = ObjectToSTRINGREF(localeNameUNSAFE);
    STRINGREF strA;

    INT32 result;

    BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException))

    *((LPVOID *)&strA)=pvStrA;

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();
#endif

    //
    // If we know that we don't have any high characters (the common case) we can
    // call a hash function that knows how to do a very fast case conversion.  If
    // we find characters above 0x80, it's much faster to convert the entire string
    // to Uppercase and then call the standard hash function on it.
    //
    // TODO: NLS Arrowhead -We aren't consistent with the fast casing cultures (any en?  fr? de?)
    if (IsCultureEnglishOrInvariant((localeName)->GetBuffer()) &&           // If we're en-US or Invariant
        ((IS_STRING_STATE_UNDETERMINED(strA->GetHighCharState()) &&          // and we're undetermined
           IS_FAST_CASING(strA->InternalCheckHighChars())) ||       // and its fast casing when determined
        IS_FAST_CASING(strA->GetHighCharState())))                         // or we're fast casing that's already determined
    {
        // Notice that for Turkish and Azeri we don't get here and shouldn't use this
        // fast path because of their special Latin casing rules.
#ifndef FEATURE_CORECLR
        result = curDomain->m_pNlsHashProvider->HashiStringKnownLower80(strA->GetBuffer(), strA->GetStringLength(), bForceRandomizedHashing, additionalEntropy);
#else
        result = COMNlsHashProvider::s_NlsHashProvider.HashiStringKnownLower80(strA->GetBuffer(), strA->GetStringLength(), bForceRandomizedHashing, additionalEntropy);
#endif // FEATURE_CORECLR
    }
    else
    {
        handle = EnsureValidSortHandle(handle, handleOrigin, localeName->GetBuffer());

        // Make it upper case
        CQuickBytes newBuffer;
        INT32 length = strA->GetStringLength();
        WCHAR *pNewStr = (WCHAR *)newBuffer.AllocThrows((length + 1) * sizeof(WCHAR));

        // Work around an A/V in LCMapStringEx for the invariant culture.
        // Revisit this after Vista SP2 has been deployed everywhere. 
        DWORD linguisticCasing = 0;
        if (localeName->GetStringLength() > 0)  // if not the invariant culture...
        {
            linguisticCasing = LCMAP_LINGUISTIC_CASING;
        }

        int lcmapResult;
#ifndef FEATURE_CORECLR
        if(!(curDomain->m_bUseOsSorting))
        {
            lcmapResult = SortVersioning::SortDllChangeCase((SortVersioning::PSORTHANDLE) handle,
                                                    LCMAP_UPPERCASE | linguisticCasing,
                                                    strA->GetBuffer(),
                                                    length,
                                                    pNewStr,
                                                    length,
                                                    NULL, 0);
        }
        else if(curDomain->m_pCustomSortLibrary != NULL)
        {
            lcmapResult = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : localeName->GetBuffer(),
                                                   LCMAP_UPPERCASE | linguisticCasing,
                                                   strA->GetBuffer(),
                                                   length,
                                                   pNewStr,
                                                   length,
                                                   NULL, NULL, (LPARAM) handle);
        }
        else
#endif
        {
            lcmapResult = NewApis::LCMapStringEx(handle != NULL ? NULL : localeName->GetBuffer(),
                                                   LCMAP_UPPERCASE | linguisticCasing,
                                                   strA->GetBuffer(),
                                                   length,
                                                   pNewStr,
                                                   length,
                                                   NULL, NULL, (LPARAM) handle);
        }

        if (lcmapResult == 0)
        {
            // return value of 0 indicates failure and error value is supposed to be set.
            // shouldn't ever really happen
            _ASSERTE(!"catastrophic failure calling NewApis::InternalGetCaseInsHash!  This could be a CultureInfo or CompareInfo bug (bad localeName string) or maybe a GCHole.");
        }
        pNewStr[length]='\0';

        // Get hash for the upper case of the new string

#ifndef FEATURE_CORECLR
        result = curDomain->m_pNlsHashProvider->HashString(pNewStr, length, (BOOL)bForceRandomizedHashing, additionalEntropy);
#else
        result = COMNlsHashProvider::s_NlsHashProvider.HashString(pNewStr, length, (BOOL)bForceRandomizedHashing, additionalEntropy);
#endif // FEATURE_CORECLR
    }

    END_SO_INTOLERANT_CODE

    return result;
}
FCIMPLEND

// Fast path for finding a String using OrdinalIgnoreCase rules
// returns true if the fast path succeeded, with foundIndex set to the location where the String was found or -1
// Returns false when FindStringOrdinal isn't handled (we don't have our own general version of this function to fall back on)
// Note for future optimizations: kernel32!FindStringOrdinal(ignoreCase=TRUE) uses per-character table lookup
// to map to upper case before comparison, but isn't otherwise optimized
BOOL QCALLTYPE COMNlsInfo::InternalTryFindStringOrdinalIgnoreCase(
    __in                   DWORD       dwFindNLSStringFlags, // mutually exclusive flags: FIND_FROMSTART, FIND_STARTSWITH, FIND_FROMEND, FIND_ENDSWITH
    __in_ecount(cchSource) LPCWSTR     lpStringSource,       // the string we search in
    __in                   int         cchSource,            // number of characters lpStringSource after sourceIndex
    __in                   int         sourceIndex,          // index from where the search will start in lpStringSource
    __in_ecount(cchValue)  LPCWSTR     lpStringValue,        // the string we search for
    __in                   int         cchValue,
    __out                  int*        foundIndex)           // the index in lpStringSource where we found lpStringValue
{
    CONTRACTL
    {
        QCALL_CHECK;
        PRECONDITION(lpStringSource != NULL);
        PRECONDITION(lpStringValue != NULL);
        PRECONDITION(cchSource>=0);
        PRECONDITION(cchValue>=0);
        PRECONDITION((dwFindNLSStringFlags & FIND_NLS_STRING_FLAGS_NEGATION) == 0);
    } CONTRACTL_END;

    BOOL result = FALSE;

    BEGIN_QCALL;

    LPCWSTR lpSearchStart = NULL;
    if (dwFindNLSStringFlags & (FIND_FROMEND | FIND_ENDSWITH))
    {
        lpSearchStart = &lpStringSource[sourceIndex - cchSource + 1];
    }
    else {
        lpSearchStart = &lpStringSource[sourceIndex];
    }
#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();

    // Check if the default sorting is overridden
    if (curDomain->m_pCustomSortLibrary != NULL)
    {
        *foundIndex = (curDomain->m_pCustomSortLibrary->pFindStringOrdinal)(
            dwFindNLSStringFlags,
            lpSearchStart,
            cchSource,
            lpStringValue,
            cchValue,
            TRUE);
        result = TRUE;
    }
    else
#endif
    {
#ifndef FEATURE_CORESYSTEM
        // kernel function pointer
        typedef int (WINAPI *PFNFindStringOrdinal)(DWORD, LPCWSTR, INT, LPCWSTR, INT, BOOL);
        static PFNFindStringOrdinal FindStringOrdinal = NULL;

        // initizalize kernel32!FindStringOrdinal
        if (FindStringOrdinal == NULL)
        {
            PFNFindStringOrdinal result  = NULL;

            HMODULE hMod=WszGetModuleHandle(WINDOWS_KERNEL32_DLLNAME_W);
            if(hMod != NULL)
                result=(PFNFindStringOrdinal)GetProcAddress(hMod,"FindStringOrdinal");
            
            FindStringOrdinal = (result != NULL) ? result : (PFNFindStringOrdinal)-1;
        }

        // call into the kernel
        if (FindStringOrdinal != (PFNFindStringOrdinal)-1)
#endif
        {
            *foundIndex = FindStringOrdinal(
                dwFindNLSStringFlags,
                lpSearchStart,
                cchSource,
                lpStringValue,
                cchValue,
                TRUE);
            result = TRUE;
        }
    }
    // if we found the pattern string, fixup the index before we return
    if (*foundIndex >= 0)
    {
        if (dwFindNLSStringFlags & (FIND_FROMEND | FIND_ENDSWITH))
            *foundIndex += (sourceIndex - cchSource + 1);
        else
            *foundIndex += sourceIndex;
    }
    END_QCALL;

    return result;
}


// InternalCompareStringOrdinalIgnoreCase
//
// Call ::CompareStringOrdinal for native ordinal behavior
INT32 QCALLTYPE COMNlsInfo::InternalCompareStringOrdinalIgnoreCase(
    LPCWSTR string1, INT32 index1,
    LPCWSTR string2, INT32 index2,
    INT32 length1,
    INT32 length2)
{
    CONTRACTL
    {
        QCALL_CHECK;
        PRECONDITION(CheckPointer(string1));
        PRECONDITION(CheckPointer(string2));
    } CONTRACTL_END;

    INT32 result = 0;

    BEGIN_QCALL;
    //
    //  Get the arguments.
    //  We assume the caller checked them before calling us
    //

    // We don't allow the -1 that native code allows
    _ASSERT(length1 >= 0);
    _ASSERT(length2 >= 0);

    // Do the comparison
#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();
    
    if (curDomain->m_pCustomSortLibrary != NULL) {
        result = (curDomain->m_pCustomSortLibrary->pCompareStringOrdinal)(string1 + index1, length1, string2 + index2, length2, TRUE);
    } 
    else 
#endif
    {
        result = NewApis::CompareStringOrdinal(string1 + index1, length1, string2 + index2, length2, TRUE);
    }

    // The native call shouldn't fail
    _ASSERT(result != 0);
    if (result == 0)
    {
        // return value of 0 indicates failure and error value is supposed to be set.
        // shouldn't ever really happen
        _ASSERTE(!"catastrophic failure calling NewApis::CompareStringOrdinal!  This is usually due to bad arguments.");
    }

    // Adjust the result to the expected -1, 0, 1 result
    result -= 2;

    END_QCALL;

    return result;
}

/**
 * This function returns a pointer to this table that we use in System.Globalization.EncodingTable.
 * No error checking of any sort is performed.  Range checking is entirely the responsibility of the managed
 * code.
 */
FCIMPL0(EncodingDataItem *, COMNlsInfo::nativeGetEncodingTableDataPointer)
{
    LIMITED_METHOD_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;

    return (EncodingDataItem *)EncodingDataTable;
}
FCIMPLEND

/**
 * This function returns a pointer to this table that we use in System.Globalization.EncodingTable.
 * No error checking of any sort is performed.  Range checking is entirely the responsibility of the managed
 * code.
 */
FCIMPL0(CodePageDataItem *, COMNlsInfo::nativeGetCodePageTableDataPointer)
{
    LIMITED_METHOD_CONTRACT;

    STATIC_CONTRACT_SO_TOLERANT;

    return ((CodePageDataItem*) CodePageDataTable);
}
FCIMPLEND


#ifndef FEATURE_CORECLR
//
// Normalization
//

FCIMPL6(int, COMNlsInfo::nativeNormalizationNormalizeString,
            int NormForm, int& iError,
            StringObject* inChars, int inLength,
            CHARArray* outChars, int outLength )
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(inChars));
        PRECONDITION(CheckPointer(outChars, NULL_OK));
    } CONTRACTL_END;

    // Dereference our string
    STRINGREF inString(inChars);
    LPWSTR inCharsBuffer = inString->GetBuffer();

    CHARARRAYREF outCharArray(outChars);
    LPWSTR outCharsBuffer = (outCharArray != NULL) ? ((LPWSTR) (outCharArray->GetDirectPointerToNonObjectElements())) : NULL;

    // The OS APIs do not always set last error in success, so we have to do it explicitly
    SetLastError(ERROR_SUCCESS);

    int iResult = m_pfnNormalizationNormalizeStringFunc(
            NormForm, inCharsBuffer, inLength, outCharsBuffer, outLength);

    // Get our error if necessary
    if (iResult <= 0)
    {
        // if the length is <= 0 there was an error
        iError = GetLastError();

        // Go ahead and return positive lengths/indexes so we don't get confused
        iResult = -iResult;
    }
    else
    {
        iError = 0; // ERROR_SUCCESS
    }

    return iResult;
}
FCIMPLEND

FCIMPL4( FC_BOOL_RET, COMNlsInfo::nativeNormalizationIsNormalizedString,
            int NormForm, int& iError,
            StringObject* chars, int inLength )
{
    CONTRACTL
    {
        FCALL_CHECK;
        PRECONDITION(CheckPointer(chars));
    } CONTRACTL_END;

    STRINGREF inString(chars);
    LPWSTR charsBuffer = inString->GetBuffer();

    // The OS APIs do not always set last error in success, so we have to do it explicitly
    SetLastError(ERROR_SUCCESS);

    // Ask if its normalized
    BOOL bResult = m_pfnNormalizationIsNormalizedStringFunc( NormForm, charsBuffer, inLength);

    // May need an error
    if (bResult == false)
    {
        // If its false there may have been an error
        iError = GetLastError();
    }
    else
    {
        iError = 0; // ERROR_SUCCESS
    }

    FC_RETURN_BOOL(bResult);
}
FCIMPLEND

void QCALLTYPE COMNlsInfo::nativeNormalizationInitNormalization(int NormForm, BYTE* pTableData)
{
    QCALL_CONTRACT;

    BEGIN_QCALL;

    if (m_hNormalization == NULL)
    {
        HMODULE hNormalization = NULL;

        if (pTableData == NULL)
        {
            // Use OS implementation
            hNormalization = GetModuleHandleW(W("kernel32.dll"));
            if (!hNormalization)
               ThrowLastError();
        }
        else
        {
            HRESULT hr = g_pCLRRuntime->LoadLibrary(NORMALIZATION_DLL, &hNormalization);
            if (FAILED(hr))
                ThrowHR(hr);
        }

        _ASSERTE(hNormalization != NULL);
        m_hNormalization = hNormalization;
    }

    if (m_pfnNormalizationIsNormalizedStringFunc == NULL)
    {
        FARPROC pfn = GetProcAddress(m_hNormalization, "IsNormalizedString");
        if (pfn == NULL)
            ThrowLastError();
        m_pfnNormalizationIsNormalizedStringFunc = (PFN_NORMALIZATION_IS_NORMALIZED_STRING)pfn;
    }

    if (m_pfnNormalizationNormalizeStringFunc == NULL)
    {
        FARPROC pfn = GetProcAddress(m_hNormalization, "NormalizeString");
        if (pfn == NULL)
            ThrowLastError();
        m_pfnNormalizationNormalizeStringFunc = (PFN_NORMALIZATION_NORMALIZE_STRING)pfn;
    }

    if (pTableData != NULL)
    {
        if (m_pfnNormalizationInitNormalizationFunc == NULL)
        {
            FARPROC pfn = GetProcAddress(m_hNormalization, "InitNormalization");
            if (pfn == NULL)
                ThrowLastError();
            m_pfnNormalizationInitNormalizationFunc = (PFN_NORMALIZATION_INIT_NORMALIZATION)pfn;
        }

        BYTE* pResult = m_pfnNormalizationInitNormalizationFunc( NormForm, pTableData);
        if (pResult == NULL)
            ThrowOutOfMemory();
    }

    END_QCALL;
}

#endif // FEATURE_CORECLR


//
// This table should be sorted using case-insensitive ordinal order.
// In the managed code, String.CompareStringOrdinalWC() is used to sort this.
//


/**
 * This function returns the number of items in EncodingDataTable.
 */
FCIMPL0(INT32, COMNlsInfo::nativeGetNumEncodingItems)
{
    LIMITED_METHOD_CONTRACT;
    STATIC_CONTRACT_SO_TOLERANT;

    return (m_nEncodingDataTableItems);
}
FCIMPLEND



typedef CultureDataBaseObject* CULTUREDATAREF;

// nativeInitCultureData checks with the OS to see if this is a valid culture.
// If so we populate a limited number of fields.  If its not valid we return false.
//
// The fields we populate:
//
// sWindowsName -- The name that windows thinks this culture is, ie:
//                            en-US if you pass in en-US
//                            de-DE_phoneb if you pass in de-DE_phoneb
//                            fj-FJ if you pass in fj (neutral, on a pre-Windows 7 machine)
//                            fj if you pass in fj (neutral, post-Windows 7 machine)
//
// sRealName -- The name you used to construct the culture, in pretty form
//                       en-US if you pass in EN-us
//                       en if you pass in en
//                       de-DE_phoneb if you pass in de-DE_phoneb
//
// sSpecificCulture -- The specific culture for this culture
//                             en-US for en-US
//                             en-US for en
//                             de-DE_phoneb for alt sort
//                             fj-FJ for fj (neutral)
//
// sName -- The IETF name of this culture (ie: no sort info, could be neutral)
//                en-US if you pass in en-US
//                en if you pass in en
//                de-DE if you pass in de-DE_phoneb
//
// bNeutral -- TRUE if it is a neutral locale
//
// For a neutral we just populate the neutral name, but we leave the windows name pointing to the
// windows locale that's going to provide data for us.
//
FCIMPL1(FC_BOOL_RET, COMNlsInfo::nativeInitCultureData, CultureDataBaseObject *cultureDataUNSAFE)
{
    FCALL_CONTRACT;

    BOOL        success=FALSE;

    struct _gc
    {
        STRINGREF stringResult;
        CULTUREDATAREF cultureData;
    } gc;

    gc.stringResult = NULL;
    gc.cultureData  = (CULTUREDATAREF) cultureDataUNSAFE;
    HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);

    WCHAR       buffer[LOCALE_NAME_MAX_LENGTH];
    int         result;

    StackSString realNameBuffer( ((STRINGREF)gc.cultureData->sRealName)->GetBuffer() );

    // Call GetLocaleInfoEx and see if the OS knows about it.
    // Note that GetLocaleInfoEx has variations:
    // * Pre-Vista it fails and has to go downlevel
    // * Vista succeeds, but not for neutrals
    // * Win7 succeeds for all locales.
    // * Mac does ???
    // The differences should be handled by the NewApis wrapper
    result = NewApis::GetLocaleInfoEx(realNameBuffer, LOCALE_SNAME, buffer, NumItems(buffer));

    // Did it fail?
    if (result == 0)
    {
        // Not a real locale, fail
        goto Exit;
    }

    // It worked, note that the name is the locale name, so use that (even for neutrals)
    // We need to clean up our "real" name, which should look like the windows name right now
    // so overwrite the input with the cleaned up name
    gc.stringResult = StringObject::NewString(buffer, result-1);
    SetObjectReference((OBJECTREF*)&(gc.cultureData->sRealName), gc.stringResult, NULL);

    // Check for neutrality, don't expect to fail
    // (buffer has our name in it, so we don't have to do the gc. stuff)
    DWORD bNeutral;
    if (0 == NewApis::GetLocaleInfoEx(buffer, LOCALE_INEUTRAL | LOCALE_RETURN_NUMBER, (LPWSTR)&bNeutral, sizeof(bNeutral)/sizeof(WCHAR)))
        goto Exit;

    // Remember our neutrality
    gc.cultureData->bNeutral = (bNeutral != 0);

#ifndef FEATURE_CORECLR
    gc.cultureData->bWin32Installed = (IsOSValidLocaleName(buffer, gc.cultureData->bNeutral) != 0);
    gc.cultureData->bFramework = (IsWhidbeyFrameworkCulture(buffer) != 0);
#endif // FEATURE_CORECLR


    // Note: Parents will be set dynamically

    // Start by assuming the windows name'll be the same as the specific name since windows knows
    // about specifics on all versions.  For macs it also works.  Only for downlevel Neutral locales
    // does this have to change.
    gc.stringResult = StringObject::NewString(buffer, result-1);
    SetObjectReference((OBJECTREF*)&(gc.cultureData->sWindowsName), gc.stringResult, NULL);

    // Neutrals and non-neutrals are slightly different
    if (gc.cultureData->bNeutral)
    {
        // Neutral Locale

        // IETF name looks like neutral name
        gc.stringResult = StringObject::NewString(buffer, result-1);
        SetObjectReference((OBJECTREF*)&(gc.cultureData->sName), gc.stringResult, NULL);

        // Specific locale name is whatever ResolveLocaleName (win7+) returns.
        // (Buffer has our name in it, and we can recycle that because windows resolves it before writing to the buffer)
        result = NewApis::ResolveLocaleName(buffer, buffer, NumItems(buffer));

        // 0 is failure, 1 is invariant (""), which we expect
        if (result < 1) goto Exit;

        // We found a locale name, so use it.
        // In vista this should look like a sort name (de-DE_phoneb) or a specific culture (en-US) and be in the "pretty" form
        gc.stringResult = StringObject::NewString(buffer, result - 1);
        SetObjectReference((OBJECTREF*)&(gc.cultureData->sSpecificCulture), gc.stringResult, NULL);

#ifdef FEATURE_CORECLR
        if (!IsWindows7())
        {
            // For neutrals on Windows 7 + the neutral windows name can be the same as the neutral name,
            // but on pre windows 7 names it has to be the specific, so we have to fix it in that case.
            gc.stringResult = StringObject::NewString(buffer, result - 1);
            SetObjectReference((OBJECTREF*)&(gc.cultureData->sWindowsName), gc.stringResult, NULL);
        }
#endif


    }
    else
    {
        // Specific Locale

        // Specific culture's the same as the locale name since we know its not neutral
        // On mac we'll use this as well, even for neutrals. There's no obvious specific
        // culture to use and this isn't exposed, but behaviorally this is correct on mac.
        // Note that specifics include the sort name (de-DE_phoneb)
        gc.stringResult = StringObject::NewString(buffer, result-1);
        SetObjectReference((OBJECTREF*)&(gc.cultureData->sSpecificCulture), gc.stringResult, NULL);

        // We need the IETF name (sname)
        // If we aren't an alt sort locale then this is the same as the windows name.
        // If we are an alt sort locale then this is the same as the part before the _ in the windows name
        // This is for like de-DE_phoneb and es-ES_tradnl that hsouldn't have the _ part

        int localeNameLength = result - 1;

        LCID lcid = NewApis::LocaleNameToLCID(buffer, 0);
        if (!IsCustomCultureId(lcid))
        {
            LPCWSTR index = wcschr(buffer, W('_'));
            if(index)                               // Not a custom culture and looks like an alt sort name
            {
                // Looks like an alt sort, and has a appropriate sort LCID (so not custom), make it smaller for the RFC 4646 style name
                localeNameLength = static_cast<int>(index - buffer);
            }
        }

        gc.stringResult = StringObject::NewString(buffer, localeNameLength);
        _ASSERTE(gc.stringResult != NULL);

        // Now use that name
        SetObjectReference((OBJECTREF*)&(gc.cultureData->sName), gc.stringResult, NULL);
    }

#ifdef FEATURE_CORECLR
    // For Silverlight make sure that the sorting tables are available (< Vista may not have east asian installed)
    result = NewApis::CompareStringEx(((STRINGREF)gc.cultureData->sWindowsName)->GetBuffer(),
                                      0, W("A"), 1, W("B"), 1, NULL, NULL, 0);
    if (result == 0) goto Exit;
#endif

    // It succeeded.
    success = TRUE;

Exit: {}

    HELPER_METHOD_FRAME_END();

    FC_RETURN_BOOL(success);
}
FCIMPLEND


// Return true if we're on Windows 7 (ie: if we have neutral native support)
BOOL COMNlsInfo::IsWindows7()
{
    static BOOL bChecked=FALSE;
    static BOOL bIsWindows7=FALSE;

    if (!bChecked)
    {
        // LOCALE_INEUTRAL is first supported on Windows 7
        if (GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_INEUTRAL, NULL, 0) != 0)
        {
            // Success, we're win7
            bIsWindows7 = TRUE;
        }

        // Either way we checked now
        bChecked = TRUE;
    }

    return bIsWindows7;
}

//
// QCall implementation
//
int QCALLTYPE COMNlsInfo::InternalFindNLSStringEx(
    __in_opt               INT_PTR     handle,               // optional sort handle
    __in_opt               INT_PTR     handleOrigin,
    __in_z                 LPCWSTR     lpLocaleName,         // locale name
    __in                   int         dwFindNLSStringFlags, // search falg
    __in_ecount(cchSource) LPCWSTR     lpStringSource,       // the string we search in
    __in                   int         cchSource,            // number of characters lpStringSource after sourceIndex
    __in                   int         sourceIndex,          // index from where the search will start in lpStringSource
    __in_ecount(cchValue)  LPCWSTR     lpStringValue,        // the string we search for
    __in                   int         cchValue)             // length of the string we search for
{
    CONTRACTL {
        QCALL_CHECK;
        PRECONDITION(lpLocaleName != NULL);
        PRECONDITION(lpStringSource != NULL);
        PRECONDITION(lpStringValue != NULL);
        PRECONDITION(cchSource>=0);
        PRECONDITION(cchValue>=0);
    } CONTRACTL_END;

    int retValue = -1;

    BEGIN_QCALL;

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();
    handle = EnsureValidSortHandle(handle, handleOrigin, lpLocaleName);
#endif

    #define RESERVED_FIND_ASCII_STRING 0x20000000       // This flag used only to tell the sorting DLL can assume the string characters are in ASCII.

#ifndef FEATURE_CORECLR
    int asciiFlag = (dwFindNLSStringFlags & RESERVED_FIND_ASCII_STRING);
#endif // FEATURE_CORECLR

    dwFindNLSStringFlags &= ~RESERVED_FIND_ASCII_STRING;

    if (cchValue == 0)
    {
        retValue = sourceIndex;       // keep Whidbey compatibility
        goto lExit;
    }

    if (sourceIndex<0 || cchSource<0 ||
        ((dwFindNLSStringFlags & (FIND_FROMEND | FIND_ENDSWITH)) && (sourceIndex+1<cchSource)))
    {
        goto lExit;
    }

    if (dwFindNLSStringFlags & COMPARE_OPTIONS_ORDINAL)
    {
        if (dwFindNLSStringFlags & (FIND_FROMEND | FIND_ENDSWITH))
        {
            retValue = NewApis::LastIndexOfString(
                        lpLocaleName,
                        &lpStringSource[sourceIndex - cchSource + 1],
                        cchSource,
                        lpStringValue,
                        cchValue,
                        dwFindNLSStringFlags & FIND_NLS_STRING_FLAGS_NEGATION,
                        dwFindNLSStringFlags & FIND_ENDSWITH);
            if (retValue >= 0)
            {
                retValue += sourceIndex - cchSource + 1;
            }
        }
        else
        {
            retValue = NewApis::IndexOfString(
                        lpLocaleName,
                        &lpStringSource[sourceIndex],
                        cchSource,
                        lpStringValue,
                        cchValue,
                        dwFindNLSStringFlags & FIND_NLS_STRING_FLAGS_NEGATION,
                        dwFindNLSStringFlags & FIND_STARTSWITH);

            if  (retValue >= 0)
            {
                retValue += sourceIndex;
            }
        }
    }
    else
    {
        if (dwFindNLSStringFlags & (FIND_FROMEND | FIND_ENDSWITH))
        {
#ifndef FEATURE_CORECLR
            if(!(curDomain->m_bUseOsSorting))
            {
                retValue = SortVersioning::SortDllFindString((SortVersioning::PSORTHANDLE) handle,
                                                  dwFindNLSStringFlags | asciiFlag,
                                                  &lpStringSource[sourceIndex - cchSource + 1],
                                                  cchSource,
                                                  lpStringValue,
                                                  cchValue,
                                                  NULL, NULL, 0);
            }
            else if(curDomain->m_pCustomSortLibrary != NULL)
            {
                retValue = (curDomain->m_pCustomSortLibrary->pFindNLSStringEx)(
                                        handle != NULL ? NULL : lpLocaleName,
                                        dwFindNLSStringFlags,
                                        &lpStringSource[sourceIndex - cchSource + 1],
                                        cchSource,
                                        lpStringValue,
                                        cchValue, NULL, NULL, NULL, (LPARAM) handle);
            }   
            else
#endif
            {
                retValue = NewApis::FindNLSStringEx(
                                        handle != NULL ? NULL : lpLocaleName,
                                        dwFindNLSStringFlags,
                                        &lpStringSource[sourceIndex - cchSource + 1],
                                        cchSource,
                                        lpStringValue,
                                        cchValue, NULL, NULL, NULL, (LPARAM) handle);
            }

            if (retValue >= 0)
            {
                retValue += sourceIndex - cchSource + 1;
            }
        }
        else
        {
#ifndef FEATURE_CORECLR
            if(!(curDomain->m_bUseOsSorting))
            {
                retValue = SortVersioning::SortDllFindString((SortVersioning::PSORTHANDLE) handle,
                                                  dwFindNLSStringFlags | asciiFlag,
                                                  &lpStringSource[sourceIndex],
                                                  cchSource,
                                                  lpStringValue,
                                                  cchValue,
                                                  NULL, NULL, 0);
            }
            else if(curDomain->m_pCustomSortLibrary != NULL)
            {
                retValue = (curDomain->m_pCustomSortLibrary->pFindNLSStringEx)(
                                        handle != NULL ? NULL : lpLocaleName,
                                        dwFindNLSStringFlags,
                                        &lpStringSource[sourceIndex],
                                        cchSource,
                                        lpStringValue,
                                        cchValue, NULL, NULL, NULL, (LPARAM) handle);
            }
            else
#endif
            {
                retValue = NewApis::FindNLSStringEx(
                                        handle != NULL ? NULL : lpLocaleName,
                                        dwFindNLSStringFlags,
                                        &lpStringSource[sourceIndex],
                                        cchSource,
                                        lpStringValue,
                                        cchValue, NULL, NULL, NULL, (LPARAM) handle);
            }

            if (retValue >= 0)
            {
                retValue += sourceIndex;
            }
        }
    }

lExit:

    END_QCALL;

    return retValue;
}


int QCALLTYPE COMNlsInfo::InternalGetSortKey(
    __in_opt               INT_PTR handle,        // PSORTHANDLE
    __in_opt               INT_PTR handleOrigin,
    __in_z                 LPCWSTR pLocaleName,   // locale name
    __in                   int     flags,         // flags
    __in_ecount(cchSource) LPCWSTR pStringSource, // Source string
    __in                   int     cchSource,     // number of characters in lpStringSource
    __in_ecount(cchTarget) PBYTE   pTarget,       // Target data buffer (may be null to count)
    __in                   int     cchTarget)     // Character count for target buffer
{
    CONTRACTL {
        QCALL_CHECK;
        PRECONDITION(pLocaleName != NULL);
        PRECONDITION(pStringSource != NULL);
//        PRECONDITION(pTarget != NULL);
        PRECONDITION(cchSource>=0);
//        PRECONDITION(cchTarget>=0);
    } CONTRACTL_END;

    int retValue = 0;

    BEGIN_QCALL;


#ifndef FEATURE_CORECLR
    handle = EnsureValidSortHandle(handle, handleOrigin, pLocaleName);

    AppDomain* curDomain = GetAppDomain();

    if(!(curDomain->m_bUseOsSorting))
    {
        retValue = SortVersioning::SortDllGetSortKey((SortVersioning::PSORTHANDLE) handle,
                                                  flags,
                                                  pStringSource,
                                                  cchSource,
                                                  pTarget,
                                                  cchTarget,
                                                  NULL, 0);
    }
    else if(curDomain->m_pCustomSortLibrary != NULL)
    {
        retValue = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(handle != NULL ? NULL : pLocaleName,
                                          flags | LCMAP_SORTKEY,
                                          pStringSource,
                                          cchSource,
                                          (LPWSTR)pTarget,
                                          cchTarget,
                                          NULL,
                                          NULL,
                                          (LPARAM) handle);
    }
    else
#endif
    {
        // Just call NewApis::LCMapStringEx to do our work
        retValue = NewApis::LCMapStringEx(handle != NULL ? NULL : pLocaleName,
                                          flags | LCMAP_SORTKEY,
                                          pStringSource,
                                          cchSource,
                                          (LPWSTR)pTarget,
                                          cchTarget,
                                          NULL,
                                          NULL,
                                          (LPARAM) handle);
    }
    END_QCALL;

    return retValue;
}


// We allow InternalInitSortHandle to return a NULL value
// this is the case for Silverlight or when the appdomain has custom sorting.
// So all the methods that take a SortHandle, also have to
// be able to just call the slower api that looks up the tables based on the locale name
INT_PTR QCALLTYPE COMNlsInfo::InternalInitSortHandle(LPCWSTR localeName, __out INT_PTR* handleOrigin)
{
    CONTRACTL {
        QCALL_CHECK;
        PRECONDITION(localeName != NULL);
    } CONTRACTL_END;
    
    INT_PTR pSort = NULL;

    BEGIN_QCALL;

    pSort = InitSortHandleHelper(localeName, handleOrigin);

    END_QCALL;

    return pSort;
}

INT_PTR COMNlsInfo::InitSortHandleHelper(LPCWSTR localeName, __out INT_PTR* handleOrigin)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        CANNOT_TAKE_LOCK;
        PRECONDITION(localeName != NULL);
    } CONTRACTL_END;

    INT_PTR pSort = NULL;

#ifndef FEATURE_CORECLR
    AppDomain* curDomain = GetAppDomain();

#if _DEBUG
    _ASSERTE(curDomain->m_bSortingInitialized);
#endif

    if(curDomain->m_bUseOsSorting)
    {
        pSort = InternalInitOsSortHandle(localeName, handleOrigin);
    }
    else
    {
        pSort = InternalInitVersionedSortHandle(localeName, handleOrigin);
    }
#else
    // coreclr will try to initialize the handle and if running on downlevel it'll just return null
    pSort = InternalInitOsSortHandle(localeName, handleOrigin);
#endif // FEATURE_CORECLR
    return pSort;
}

#ifndef FEATURE_CORECLR
INT_PTR COMNlsInfo::InternalInitVersionedSortHandle(LPCWSTR localeName, __out INT_PTR* handleOrigin)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        CANNOT_TAKE_LOCK;
        PRECONDITION(localeName != NULL);
    } CONTRACTL_END;

    AppDomain* curDomain = GetAppDomain();

    if(curDomain->m_pCustomSortLibrary != NULL)
    {
        return NULL;
    }

    return InternalInitVersionedSortHandle(localeName, handleOrigin, curDomain->m_sortVersion);
}

INT_PTR COMNlsInfo::InternalInitVersionedSortHandle(LPCWSTR localeName, __out INT_PTR* handleOrigin, DWORD sortVersion)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        CANNOT_TAKE_LOCK;
        PRECONDITION(localeName != NULL);
    } CONTRACTL_END;
    
    INT_PTR pSort = NULL;

    _ASSERTE(NewApis::NotLeakingFrameworkOnlyCultures(localeName));

    *handleOrigin = (INT_PTR) SortVersioning::GetSortHandle;

    // try the requested version
    if(sortVersion != DEFAULT_SORT_VERSION)
    {
        NLSVERSIONINFO  sortVersionInfo;
        sortVersionInfo.dwNLSVersionInfoSize = sizeof(NLSVERSIONINFO);
        sortVersionInfo.dwNLSVersion = sortVersion;
        sortVersionInfo.dwDefinedVersion = sortVersion;
        pSort = (INT_PTR) SortVersioning::GetSortHandle(localeName, &sortVersionInfo);
    }

    // fallback to default version
    if(pSort == NULL)
    {
        pSort = (INT_PTR) SortVersioning::GetSortHandle(localeName, NULL);
    }

    _ASSERTE(RunningOnWin8() || pSort != NULL);

    return pSort;
}
#endif //FEATURE_CORECLR

// Can return NULL
INT_PTR COMNlsInfo::InternalInitOsSortHandle(LPCWSTR localeName, __out INT_PTR* handleOrigin)
{
    CONTRACTL {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        CANNOT_TAKE_LOCK;
        PRECONDITION(localeName != NULL);
    } CONTRACTL_END;
    
    INT_PTR pSort = NULL;

    AppDomain* curDomain = GetAppDomain();

#ifndef FEATURE_CORECLR
    if (RunningOnWin8() || curDomain->m_pCustomSortLibrary != NULL)
#else
    if (RunningOnWin8())
#endif //FEATURE_CORECLR 
    {
        LPARAM lSortHandle;
        int ret;

#ifndef FEATURE_CORECLR
        if (curDomain->m_pCustomSortLibrary != NULL)
        {
            ret = (curDomain->m_pCustomSortLibrary->pLCMapStringEx)(localeName, LCMAP_SORTHANDLE, NULL, 0, (LPWSTR) &lSortHandle, sizeof(LPARAM), NULL, NULL, 0);
            *handleOrigin = (INT_PTR) curDomain->m_pCustomSortLibrary->pLCMapStringEx;
        }
        else
#endif //FEATURE_CORECLR
        {
            ret = NewApis::LCMapStringEx(localeName, LCMAP_SORTHANDLE, NULL, 0, (LPWSTR) &lSortHandle, sizeof(LPARAM), NULL, NULL, 0);
            *handleOrigin = (INT_PTR) NewApis::LCMapStringEx;
        }

        if (ret != 0)
        {
            pSort = (INT_PTR) lSortHandle;
        }
    }

    return pSort;
}

#ifndef FEATURE_CORECLR
BOOL QCALLTYPE COMNlsInfo::InternalGetNlsVersionEx(INT_PTR handle, INT_PTR handleOrigin, LPCWSTR lpLocaleName, NLSVERSIONINFOEX * lpVersionInformation)
{
    CONTRACTL {
        QCALL_CHECK;
    } CONTRACTL_END;

    BOOL ret = FALSE;

    BEGIN_QCALL;
    
    AppDomain* curDomain = GetAppDomain();

    if(curDomain->m_bUseOsSorting)
    {
        if(curDomain->m_pCustomSortLibrary != NULL)
        {
            ret = (curDomain->m_pCustomSortLibrary->pGetNLSVersionEx)(COMPARE_STRING, lpLocaleName, lpVersionInformation);
        }
        else
        {
            ret = GetNLSVersionEx(COMPARE_STRING, lpLocaleName, lpVersionInformation);
        }
    }
    else
    {
        handle = EnsureValidSortHandle(handle, handleOrigin, lpLocaleName);

        NLSVERSIONINFO nlsVersion;
        nlsVersion.dwNLSVersionInfoSize = sizeof(NLSVERSIONINFO);

        ret = SortVersioning::SortGetNLSVersion((SortVersioning::PSORTHANDLE) handle, COMPARE_STRING, &nlsVersion);

        lpVersionInformation->dwNLSVersion = nlsVersion.dwNLSVersion;
        lpVersionInformation->dwDefinedVersion = nlsVersion.dwDefinedVersion;
        lpVersionInformation->dwEffectiveId = 0;
        ZeroMemory(&(lpVersionInformation->guidCustomVersion), sizeof(GUID));                
    }
    
    END_QCALL;
 
    return ret;
}

DWORD QCALLTYPE COMNlsInfo::InternalGetSortVersion()
{
    CONTRACTL {
        QCALL_CHECK;
    } CONTRACTL_END;

    DWORD version = DEFAULT_SORT_VERSION;

    BEGIN_QCALL;

    AppDomain* curDomain = GetAppDomain();
    version = curDomain->m_sortVersion;

    END_QCALL;

    return version;
}

#endif //FEATURE_CORECLR