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 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/*****************************************************************************
*
* File ccapitst.c
*
* Modification History:
* Name Description
* Madhu Katragadda Ported for C API
******************************************************************************
*/
#include <stdalign.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "unicode/uloc.h"
#include "unicode/ucnv.h"
#include "unicode/ucnv_err.h"
#include "unicode/putil.h"
#include "unicode/uset.h"
#include "unicode/ustring.h"
#include "unicode/utf8.h"
#include "ucnv_bld.h" /* for sizeof(UConverter) */
#include "cmemory.h" /* for UAlignedMemory */
#include "cintltst.h"
#include "ccapitst.h"
#include "cstring.h"
#define NUM_CODEPAGE 1
#define MAX_FILE_LEN 1024*20
#define UCS_FILE_NAME_SIZE 512
/*returns an action other than the one provided*/
#if !UCONFIG_NO_LEGACY_CONVERSION
static UConverterFromUCallback otherUnicodeAction(UConverterFromUCallback MIA);
static UConverterToUCallback otherCharAction(UConverterToUCallback MIA);
#endif
static UConverter *
cnv_open(const char *name, UErrorCode *pErrorCode) {
if(name!=NULL && name[0]=='*') {
return ucnv_openPackage(loadTestData(pErrorCode), name+1, pErrorCode);
} else {
return ucnv_open(name, pErrorCode);
}
}
static void ListNames(void);
static void TestFlushCache(void);
static void TestDuplicateAlias(void);
static void TestCCSID(void);
static void TestJ932(void);
static void TestJ1968(void);
#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
static void TestLMBCSMaxChar(void);
#endif
static void TestConvertClone(void);
#if !UCONFIG_NO_LEGACY_CONVERSION
static void TestConvertSafeCloneCallback(void);
#endif
static void TestEBCDICSwapLFNL(void);
static void TestConvertEx(void);
static void TestConvertExFromUTF8(void);
static void TestConvertExFromUTF8_C5F0(void);
static void TestConvertAlgorithmic(void);
void TestDefaultConverterError(void); /* defined in cctest.c */
void TestDefaultConverterSet(void); /* defined in cctest.c */
static void TestToUCountPending(void);
static void TestFromUCountPending(void);
static void TestDefaultName(void);
static void TestCompareNames(void);
static void TestSubstString(void);
static void InvalidArguments(void);
static void TestGetName(void);
static void TestUTFBOM(void);
void addTestConvert(TestNode** root);
void addTestConvert(TestNode** root)
{
addTest(root, &ListNames, "tsconv/ccapitst/ListNames");
addTest(root, &TestConvert, "tsconv/ccapitst/TestConvert");
addTest(root, &TestFlushCache, "tsconv/ccapitst/TestFlushCache");
addTest(root, &TestAlias, "tsconv/ccapitst/TestAlias");
addTest(root, &TestDuplicateAlias, "tsconv/ccapitst/TestDuplicateAlias");
addTest(root, &TestConvertSafeClone, "tsconv/ccapitst/TestConvertSafeClone");
addTest(root, &TestConvertClone, "tsconv/ccapitst/TestConvertClone");
#if !UCONFIG_NO_LEGACY_CONVERSION
addTest(root, &TestConvertSafeCloneCallback,"tsconv/ccapitst/TestConvertSafeCloneCallback");
#endif
addTest(root, &TestCCSID, "tsconv/ccapitst/TestCCSID");
addTest(root, &TestJ932, "tsconv/ccapitst/TestJ932");
addTest(root, &TestJ1968, "tsconv/ccapitst/TestJ1968");
#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
addTest(root, &TestLMBCSMaxChar, "tsconv/ccapitst/TestLMBCSMaxChar");
#endif
addTest(root, &TestEBCDICSwapLFNL, "tsconv/ccapitst/TestEBCDICSwapLFNL");
addTest(root, &TestConvertEx, "tsconv/ccapitst/TestConvertEx");
addTest(root, &TestConvertExFromUTF8, "tsconv/ccapitst/TestConvertExFromUTF8");
addTest(root, &TestConvertExFromUTF8_C5F0, "tsconv/ccapitst/TestConvertExFromUTF8_C5F0");
addTest(root, &TestConvertAlgorithmic, "tsconv/ccapitst/TestConvertAlgorithmic");
addTest(root, &TestDefaultConverterError, "tsconv/ccapitst/TestDefaultConverterError");
addTest(root, &TestDefaultConverterSet, "tsconv/ccapitst/TestDefaultConverterSet");
#if !UCONFIG_NO_FILE_IO
addTest(root, &TestToUCountPending, "tsconv/ccapitst/TestToUCountPending");
addTest(root, &TestFromUCountPending, "tsconv/ccapitst/TestFromUCountPending");
#endif
addTest(root, &TestDefaultName, "tsconv/ccapitst/TestDefaultName");
addTest(root, &TestCompareNames, "tsconv/ccapitst/TestCompareNames");
addTest(root, &TestSubstString, "tsconv/ccapitst/TestSubstString");
addTest(root, &InvalidArguments, "tsconv/ccapitst/InvalidArguments");
addTest(root, &TestGetName, "tsconv/ccapitst/TestGetName");
addTest(root, &TestUTFBOM, "tsconv/ccapitst/TestUTFBOM");
}
static void ListNames(void) {
UErrorCode err = U_ZERO_ERROR;
int32_t testLong1 = 0;
const char* available_conv;
UEnumeration *allNamesEnum = NULL;
int32_t allNamesCount = 0;
uint16_t count;
log_verbose("Testing ucnv_openAllNames()...");
allNamesEnum = ucnv_openAllNames(&err);
if(U_FAILURE(err)) {
log_data_err("FAILURE! ucnv_openAllNames() -> %s\n", myErrorName(err));
}
else {
const char *string = NULL;
int32_t len = 0;
int32_t count1 = 0;
int32_t count2 = 0;
allNamesCount = uenum_count(allNamesEnum, &err);
while ((string = uenum_next(allNamesEnum, &len, &err))) {
count1++;
log_verbose("read \"%s\", length %i\n", string, len);
}
if (U_FAILURE(err)) {
log_err("FAILURE! uenum_next(allNamesEnum...) set an error: %s\n", u_errorName(err));
err = U_ZERO_ERROR;
}
uenum_reset(allNamesEnum, &err);
while ((string = uenum_next(allNamesEnum, &len, &err))) {
count2++;
ucnv_close(ucnv_open(string, &err));
log_verbose("read \"%s\", length %i (%s)\n", string, len, U_SUCCESS(err) ? "available" : "unavailable");
err = U_ZERO_ERROR;
}
if (count1 != count2) {
log_err("FAILURE! uenum_reset(allNamesEnum, &err); doesn't work\n");
}
}
uenum_close(allNamesEnum);
err = U_ZERO_ERROR;
/*Tests ucnv_getAvailableName(), getAvailableCount()*/
log_verbose("Testing ucnv_countAvailable()...");
testLong1=ucnv_countAvailable();
log_info("Number of available codepages: %d/%d\n", testLong1, allNamesCount);
log_verbose("\n---Testing ucnv_getAvailableName.."); /*need to check this out */
available_conv = ucnv_getAvailableName(testLong1);
/*test ucnv_getAvailableName with err condition*/
log_verbose("\n---Testing ucnv_getAvailableName..with index < 0 ");
available_conv = ucnv_getAvailableName(-1);
if(available_conv != NULL){
log_err("ucnv_getAvailableName() with index < 0) should return NULL\n");
}
/* Test ucnv_countAliases() etc. */
count = ucnv_countAliases("utf-8", &err);
if(U_FAILURE(err)) {
log_data_err("FAILURE! ucnv_countAliases(\"utf-8\") -> %s\n", myErrorName(err));
} else if(count <= 0) {
log_err("FAILURE! ucnv_countAliases(\"utf-8\") -> %d aliases\n", count);
} else {
/* try to get the aliases individually */
const char *alias;
alias = ucnv_getAlias("utf-8", 0, &err);
if(U_FAILURE(err)) {
log_err("FAILURE! ucnv_getAlias(\"utf-8\", 0) -> %s\n", myErrorName(err));
} else if(strcmp("UTF-8", alias) != 0) {
log_err("FAILURE! ucnv_getAlias(\"utf-8\", 0) -> %s instead of UTF-8\n", alias);
} else {
uint16_t aliasNum;
for(aliasNum = 0; aliasNum < count; ++aliasNum) {
alias = ucnv_getAlias("utf-8", aliasNum, &err);
if(U_FAILURE(err)) {
log_err("FAILURE! ucnv_getAlias(\"utf-8\", %d) -> %s\n", aliasNum, myErrorName(err));
} else if(strlen(alias) > 20) {
/* sanity check */
log_err("FAILURE! ucnv_getAlias(\"utf-8\", %d) -> alias %s insanely long, corrupt?!\n", aliasNum, alias);
} else {
log_verbose("alias %d for utf-8: %s\n", aliasNum, alias);
}
}
if(U_SUCCESS(err)) {
/* try to fill an array with all aliases */
const char **aliases;
aliases=(const char **)malloc(count * sizeof(const char *));
if(aliases != 0) {
ucnv_getAliases("utf-8", aliases, &err);
if(U_FAILURE(err)) {
log_err("FAILURE! ucnv_getAliases(\"utf-8\") -> %s\n", myErrorName(err));
} else {
for(aliasNum = 0; aliasNum < count; ++aliasNum) {
/* compare the pointers with the ones returned individually */
alias = ucnv_getAlias("utf-8", aliasNum, &err);
if(U_FAILURE(err)) {
log_err("FAILURE! ucnv_getAlias(\"utf-8\", %d) -> %s\n", aliasNum, myErrorName(err));
} else if(aliases[aliasNum] != alias) {
log_err("FAILURE! ucnv_getAliases(\"utf-8\")[%d] != ucnv_getAlias(\"utf-8\", %d)\n", aliasNum, aliasNum);
}
}
}
free((char **)aliases);
}
}
}
}
}
static void TestConvert(void)
{
#if !UCONFIG_NO_LEGACY_CONVERSION
char myptr[4];
char save[4];
int32_t testLong1 = 0;
uint16_t rest = 0;
int32_t len = 0;
int32_t x = 0;
FILE* ucs_file_in = NULL;
UChar BOM = 0x0000;
UChar myUChar = 0x0000;
char* mytarget; /* [MAX_FILE_LEN] */
char* mytarget_1;
char* mytarget_use;
UChar* consumedUni = NULL;
char* consumed = NULL;
char* output_cp_buffer; /* [MAX_FILE_LEN] */
UChar* ucs_file_buffer; /* [MAX_FILE_LEN] */
UChar* ucs_file_buffer_use;
UChar* my_ucs_file_buffer; /* [MAX_FILE_LEN] */
UChar* my_ucs_file_buffer_1;
int8_t ii = 0;
uint16_t codepage_index = 0;
int32_t cp = 0;
UErrorCode err = U_ZERO_ERROR;
char ucs_file_name[UCS_FILE_NAME_SIZE];
UConverterFromUCallback MIA1, MIA1_2;
UConverterToUCallback MIA2, MIA2_2;
const void *MIA1Context, *MIA1Context2, *MIA2Context, *MIA2Context2;
UConverter* someConverters[5];
UConverter* myConverter = 0;
UChar* displayname = 0;
const char* locale;
UChar* uchar1 = 0;
UChar* uchar2 = 0;
UChar* uchar3 = 0;
int32_t targetcapacity2;
int32_t targetcapacity;
int32_t targetsize;
int32_t disnamelen;
const UChar* tmp_ucs_buf;
const UChar* tmp_consumedUni=NULL;
const char* tmp_mytarget_use;
const char* tmp_consumed;
/******************************************************************
Checking Unicode -> ksc
******************************************************************/
const char* CodePagesToTest[NUM_CODEPAGE] =
{
"ibm-949_P110-1999"
};
const uint16_t CodePageNumberToTest[NUM_CODEPAGE] =
{
949
};
const int8_t CodePagesMinChars[NUM_CODEPAGE] =
{
1
};
const int8_t CodePagesMaxChars[NUM_CODEPAGE] =
{
2
};
const uint16_t CodePagesSubstitutionChars[NUM_CODEPAGE] =
{
0xAFFE
};
const char* CodePagesTestFiles[NUM_CODEPAGE] =
{
"uni-text.bin"
};
const UConverterPlatform CodePagesPlatform[NUM_CODEPAGE] =
{
UCNV_IBM
};
const char* CodePagesLocale[NUM_CODEPAGE] =
{
"ko_KR"
};
UConverterFromUCallback oldFromUAction = NULL;
UConverterToUCallback oldToUAction = NULL;
const void* oldFromUContext = NULL;
const void* oldToUContext = NULL;
/* Allocate memory */
mytarget = (char*) malloc(MAX_FILE_LEN * sizeof(mytarget[0]));
output_cp_buffer = (char*) malloc(MAX_FILE_LEN * sizeof(output_cp_buffer[0]));
ucs_file_buffer = (UChar*) malloc(MAX_FILE_LEN * sizeof(ucs_file_buffer[0]));
my_ucs_file_buffer = (UChar*) malloc(MAX_FILE_LEN * sizeof(my_ucs_file_buffer[0]));
ucs_file_buffer_use = ucs_file_buffer;
mytarget_1=mytarget;
mytarget_use = mytarget;
my_ucs_file_buffer_1=my_ucs_file_buffer;
/* flush the converter cache to get a consistent state before the flushing is tested */
ucnv_flushCache();
/*Testing ucnv_openU()*/
{
UChar converterName[]={ 0x0069, 0x0062, 0x006d, 0x002d, 0x0039, 0x0034, 0x0033, 0x0000}; /*ibm-943*/
UChar firstSortedName[]={ 0x0021, 0x0000}; /* ! */
UChar lastSortedName[]={ 0x007E, 0x0000}; /* ~ */
const char *illegalNameChars={ "ibm-943 ibm-943 ibm-943 ibm-943 ibm-943 ibm-943 ibm-943 ibm-943 ibm-943 ibm-943"};
UChar illegalName[100];
UConverter *converter=NULL;
err=U_ZERO_ERROR;
converter=ucnv_openU(converterName, &err);
if(U_FAILURE(err)){
log_data_err("FAILURE! ucnv_openU(ibm-943, err) failed. %s\n", myErrorName(err));
}
ucnv_close(converter);
err=U_ZERO_ERROR;
converter=ucnv_openU(NULL, &err);
if(U_FAILURE(err)){
log_err("FAILURE! ucnv_openU(NULL, err) failed. %s\n", myErrorName(err));
}
ucnv_close(converter);
/*testing with error value*/
err=U_ILLEGAL_ARGUMENT_ERROR;
converter=ucnv_openU(converterName, &err);
if(!(converter == NULL)){
log_data_err("FAILURE! ucnv_openU(ibm-943, U_ILLEGAL_ARGUMENT_ERROR) is expected to fail\n");
}
ucnv_close(converter);
err=U_ZERO_ERROR;
u_uastrcpy(illegalName, "");
u_uastrcpy(illegalName, illegalNameChars);
ucnv_openU(illegalName, &err);
if(!(err==U_ILLEGAL_ARGUMENT_ERROR)){
log_err("FAILURE! ucnv_openU(illegalName, err) is expected to fail\n");
}
err=U_ZERO_ERROR;
ucnv_openU(firstSortedName, &err);
if(err!=U_FILE_ACCESS_ERROR){
log_err("FAILURE! ucnv_openU(firstSortedName, err) is expected to fail\n");
}
err=U_ZERO_ERROR;
ucnv_openU(lastSortedName, &err);
if(err!=U_FILE_ACCESS_ERROR){
log_err("FAILURE! ucnv_openU(lastSortedName, err) is expected to fail\n");
}
err=U_ZERO_ERROR;
}
log_verbose("Testing ucnv_open() with converter name greater than 7 characters\n");
{
UConverter *cnv=NULL;
err=U_ZERO_ERROR;
cnv=ucnv_open("ibm-949,Madhu", &err);
if(U_FAILURE(err)){
log_data_err("FAILURE! ucnv_open(\"ibm-949,Madhu\", err) failed. %s\n", myErrorName(err));
}
ucnv_close(cnv);
}
/*Testing ucnv_convert()*/
{
int32_t targetLimit=0, sourceLimit=0, i=0, targetCapacity=0;
const uint8_t source[]={ 0x00, 0x04, 0x05, 0x06, 0xa2, 0xb4, 0x00};
const uint8_t expectedTarget[]={ 0x00, 0x37, 0x2d, 0x2e, 0x0e, 0x49, 0x62, 0x0f, 0x00};
char *target=0;
sourceLimit=UPRV_LENGTHOF(source);
err=U_ZERO_ERROR;
targetLimit=0;
targetCapacity=ucnv_convert("ibm-1364", "ibm-1363", NULL, targetLimit , (const char*)source, sourceLimit, &err);
if(err == U_BUFFER_OVERFLOW_ERROR){
err=U_ZERO_ERROR;
targetLimit=targetCapacity+1;
target=(char*)malloc(sizeof(char) * targetLimit);
targetCapacity=ucnv_convert("ibm-1364", "ibm-1363", target, targetLimit , (const char*)source, sourceLimit, &err);
}
if(U_FAILURE(err)){
log_data_err("FAILURE! ucnv_convert(ibm-1363->ibm-1364) failed. %s\n", myErrorName(err));
}
else {
for(i=0; i<targetCapacity; i++){
if(target[i] != expectedTarget[i]){
log_err("FAIL: ucnv_convert(ibm-1363->ibm-1364) failed.at index \n i=%d, Expected: %lx Got: %lx\n", i, (UChar)expectedTarget[i], (uint8_t)target[i]);
}
}
i=ucnv_convert("ibm-1364", "ibm-1363", target, targetLimit , (const char*)source+1, -1, &err);
if(U_FAILURE(err) || i!=7){
log_err("FAILURE! ucnv_convert() with sourceLimit=-1 failed: %s, returned %d instead of 7\n",
u_errorName(err), i);
}
/*Test error conditions*/
err=U_ZERO_ERROR;
i=ucnv_convert("ibm-1364", "ibm-1363", target, targetLimit , (const char*)source, 0, &err);
if(i !=0){
log_err("FAILURE! ucnv_convert() with sourceLimit=0 is expected to return 0\n");
}
err=U_ILLEGAL_ARGUMENT_ERROR;
sourceLimit=UPRV_LENGTHOF(source);
i=ucnv_convert("ibm-1364", "ibm-1363", target, targetLimit , (const char*)source, sourceLimit, &err);
if(i !=0 ){
log_err("FAILURE! ucnv_convert() with err=U_ILLEGAL_ARGUMENT_ERROR is expected to return 0\n");
}
err=U_ZERO_ERROR;
sourceLimit=UPRV_LENGTHOF(source);
targetLimit=0;
i=ucnv_convert("ibm-1364", "ibm-1363", target, targetLimit , (const char*)source, sourceLimit, &err);
if(!(U_FAILURE(err) && err==U_BUFFER_OVERFLOW_ERROR)){
log_err("FAILURE! ucnv_convert() with targetLimit=0 is expected to throw U_BUFFER_OVERFLOW_ERROR\n");
}
err=U_ZERO_ERROR;
free(target);
}
}
/*Testing ucnv_openCCSID and ucnv_open with error conditions*/
log_verbose("\n---Testing ucnv_open with err ! = U_ZERO_ERROR...\n");
err=U_ILLEGAL_ARGUMENT_ERROR;
if(ucnv_open(NULL, &err) != NULL){
log_err("ucnv_open with err != U_ZERO_ERROR is supposed to fail\n");
}
if(ucnv_openCCSID(1051, UCNV_IBM, &err) != NULL){
log_err("ucnv_open with err != U_ZERO_ERROR is supposed to fail\n");
}
err=U_ZERO_ERROR;
/* Testing ucnv_openCCSID(), ucnv_open(), ucnv_getName() */
log_verbose("\n---Testing ucnv_open default...\n");
someConverters[0] = ucnv_open(NULL,&err);
someConverters[1] = ucnv_open(NULL,&err);
someConverters[2] = ucnv_open("utf8", &err);
someConverters[3] = ucnv_openCCSID(949,UCNV_IBM,&err);
ucnv_close(ucnv_openCCSID(1051, UCNV_IBM, &err)); /* test for j350; ucnv_close(NULL) is safe */
if (U_FAILURE(err)){ log_data_err("FAILURE! %s\n", myErrorName(err));}
/* Testing ucnv_getName()*/
/*default code page */
ucnv_getName(someConverters[0], &err);
if(U_FAILURE(err)) {
log_data_err("getName[0] failed\n");
} else {
log_verbose("getName(someConverters[0]) returned %s\n", ucnv_getName(someConverters[0], &err));
}
ucnv_getName(someConverters[1], &err);
if(U_FAILURE(err)) {
log_data_err("getName[1] failed\n");
} else {
log_verbose("getName(someConverters[1]) returned %s\n", ucnv_getName(someConverters[1], &err));
}
ucnv_close(someConverters[0]);
ucnv_close(someConverters[1]);
ucnv_close(someConverters[2]);
ucnv_close(someConverters[3]);
for (codepage_index=0; codepage_index < NUM_CODEPAGE; ++codepage_index)
{
int32_t i = 0;
err = U_ZERO_ERROR;
#ifdef U_TOPSRCDIR
strcpy(ucs_file_name, U_TOPSRCDIR U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING);
#else
strcpy(ucs_file_name, loadTestData(&err));
if(U_FAILURE(err)){
log_err("\nCouldn't get the test data directory... Exiting...Error:%s\n", u_errorName(err));
return;
}
{
char* index = strrchr(ucs_file_name,(char)U_FILE_SEP_CHAR);
if((unsigned int)(index-ucs_file_name) != (strlen(ucs_file_name)-1)){
*(index+1)=0;
}
}
strcat(ucs_file_name,".."U_FILE_SEP_STRING);
#endif
strcat(ucs_file_name, CodePagesTestFiles[codepage_index]);
ucs_file_in = fopen(ucs_file_name,"rb");
if (!ucs_file_in)
{
log_data_err("Couldn't open the Unicode file [%s]... Exiting...\n", ucs_file_name);
return;
}
/*Creates a converter and testing ucnv_openCCSID(u_int code_page, platform, errstatus*/
/* myConverter =ucnv_openCCSID(CodePageNumberToTest[codepage_index],UCNV_IBM, &err); */
/* ucnv_flushCache(); */
myConverter =ucnv_open( "ibm-949", &err);
if (!myConverter || U_FAILURE(err))
{
log_data_err("Error creating the ibm-949 converter - %s \n", u_errorName(err));
fclose(ucs_file_in);
break;
}
/*testing for ucnv_getName() */
log_verbose("Testing ucnv_getName()...\n");
ucnv_getName(myConverter, &err);
if(U_FAILURE(err))
log_err("Error in getName\n");
else
{
log_verbose("getName o.k. %s\n", ucnv_getName(myConverter, &err));
}
if (uprv_stricmp(ucnv_getName(myConverter, &err), CodePagesToTest[codepage_index]))
log_err("getName failed\n");
else
log_verbose("getName ok\n");
/*Test getName with error condition*/
{
const char* name=0;
err=U_ILLEGAL_ARGUMENT_ERROR;
log_verbose("Testing ucnv_getName with err != U_ZERO_ERROR");
name=ucnv_getName(myConverter, &err);
if(name != NULL){
log_err("ucnv_getName() with err != U_ZERO_ERROR is expected to fail");
}
err=U_ZERO_ERROR;
}
/*Tests ucnv_getMaxCharSize() and ucnv_getMinCharSize()*/
log_verbose("Testing ucnv_getMaxCharSize()...\n");
if (ucnv_getMaxCharSize(myConverter)==CodePagesMaxChars[codepage_index])
log_verbose("Max byte per character OK\n");
else
log_err("Max byte per character failed\n");
log_verbose("\n---Testing ucnv_getMinCharSize()...\n");
if (ucnv_getMinCharSize(myConverter)==CodePagesMinChars[codepage_index])
log_verbose("Min byte per character OK\n");
else
log_err("Min byte per character failed\n");
/*Testing for ucnv_getSubstChars() and ucnv_setSubstChars()*/
log_verbose("\n---Testing ucnv_getSubstChars...\n");
ii=4;
ucnv_getSubstChars(myConverter, myptr, &ii, &err);
if (ii <= 0) {
log_err("ucnv_getSubstChars returned a negative number %d\n", ii);
}
for(x=0;x<ii;x++)
rest = (uint16_t)(((unsigned char)rest << 8) + (unsigned char)myptr[x]);
if (rest==CodePagesSubstitutionChars[codepage_index])
log_verbose("Substitution character ok\n");
else
log_err("Substitution character failed.\n");
log_verbose("\n---Testing ucnv_setSubstChars RoundTrip Test ...\n");
ucnv_setSubstChars(myConverter, myptr, ii, &err);
if (U_FAILURE(err))
{
log_err("FAILURE! %s\n", myErrorName(err));
}
ucnv_getSubstChars(myConverter,save, &ii, &err);
if (U_FAILURE(err))
{
log_err("FAILURE! %s\n", myErrorName(err));
}
if (strncmp(save, myptr, ii))
log_err("Saved substitution character failed\n");
else
log_verbose("Saved substitution character ok\n");
/*Testing for ucnv_getSubstChars() and ucnv_setSubstChars() with error conditions*/
log_verbose("\n---Testing ucnv_getSubstChars.. with len < minBytesPerChar\n");
ii=1;
ucnv_getSubstChars(myConverter, myptr, &ii, &err);
if(err != U_INDEX_OUTOFBOUNDS_ERROR){
log_err("ucnv_getSubstChars() with len < minBytesPerChar should throw U_INDEX_OUTOFBOUNDS_ERROR Got %s\n", myErrorName(err));
}
err=U_ZERO_ERROR;
ii=4;
ucnv_getSubstChars(myConverter, myptr, &ii, &err);
log_verbose("\n---Testing ucnv_setSubstChars.. with len < minBytesPerChar\n");
ucnv_setSubstChars(myConverter, myptr, 0, &err);
if(err != U_ILLEGAL_ARGUMENT_ERROR){
log_err("ucnv_setSubstChars() with len < minBytesPerChar should throw U_ILLEGAL_ARGUMENT_ERROR Got %s\n", myErrorName(err));
}
log_verbose("\n---Testing ucnv_setSubstChars.. with err != U_ZERO_ERROR \n");
strcpy(myptr, "abc");
ucnv_setSubstChars(myConverter, myptr, ii, &err);
err=U_ZERO_ERROR;
ucnv_getSubstChars(myConverter, save, &ii, &err);
if(strncmp(save, myptr, ii) == 0){
log_err("uncv_setSubstChars() with err != U_ZERO_ERROR shouldn't set the SubstChars and just return\n");
}
log_verbose("\n---Testing ucnv_getSubstChars.. with err != U_ZERO_ERROR \n");
err=U_ZERO_ERROR;
strcpy(myptr, "abc");
ucnv_setSubstChars(myConverter, myptr, ii, &err);
err=U_ILLEGAL_ARGUMENT_ERROR;
ucnv_getSubstChars(myConverter, save, &ii, &err);
if(strncmp(save, myptr, ii) == 0){
log_err("uncv_setSubstChars() with err != U_ZERO_ERROR shouldn't fill the SubstChars in the buffer, it just returns\n");
}
err=U_ZERO_ERROR;
/*------*/
#ifdef U_ENABLE_GENERIC_ISO_2022
/*resetState ucnv_reset()*/
log_verbose("\n---Testing ucnv_reset()..\n");
ucnv_reset(myConverter);
{
UChar32 c;
const uint8_t in[]={ 0x1b, 0x25, 0x42, 0x31, 0x32, 0x61, 0xc0, 0x80, 0xe0, 0x80, 0x80, 0xf0, 0x80, 0x80, 0x80};
const char *source=(const char *)in, *limit=(const char *)in+sizeof(in);
UConverter *cnv=ucnv_open("ISO_2022", &err);
if(U_FAILURE(err)) {
log_err("Unable to open a iso-2022 converter: %s\n", u_errorName(err));
}
c=ucnv_getNextUChar(cnv, &source, limit, &err);
if((U_FAILURE(err) || c != (UChar32)0x0031)) {
log_err("ucnv_getNextUChar() failed: %s\n", u_errorName(err));
}
ucnv_reset(cnv);
ucnv_close(cnv);
}
#endif
/*getDisplayName*/
log_verbose("\n---Testing ucnv_getDisplayName()...\n");
locale=CodePagesLocale[codepage_index];
len=0;
displayname=NULL;
disnamelen = ucnv_getDisplayName(myConverter, locale, displayname, len, &err);
if(err==U_BUFFER_OVERFLOW_ERROR) {
err=U_ZERO_ERROR;
displayname=(UChar*)malloc((disnamelen+1) * sizeof(UChar));
ucnv_getDisplayName(myConverter,locale,displayname,disnamelen+1, &err);
if(U_FAILURE(err)) {
log_err("getDisplayName failed. The error is %s\n", myErrorName(err));
}
else {
log_verbose(" getDisplayName o.k.\n");
}
free(displayname);
displayname=NULL;
}
else {
log_err("getDisplayName preflight doesn't work. Error is %s\n", myErrorName(err));
}
/*test ucnv_getDiaplayName with error condition*/
err= U_ILLEGAL_ARGUMENT_ERROR;
len=ucnv_getDisplayName(myConverter,locale,NULL,0, &err);
if( len !=0 ){
log_err("ucnv_getDisplayName() with err != U_ZERO_ERROR is supposed to return 0\n");
}
/*test ucnv_getDiaplayName with error condition*/
err=U_ZERO_ERROR;
len=ucnv_getDisplayName(NULL,locale,NULL,0, &err);
if( len !=0 || U_SUCCESS(err)){
log_err("ucnv_getDisplayName(NULL) with cnv == NULL is supposed to return 0\n");
}
err=U_ZERO_ERROR;
/* testing ucnv_setFromUCallBack() and ucnv_getFromUCallBack()*/
ucnv_getFromUCallBack(myConverter, &MIA1, &MIA1Context);
log_verbose("\n---Testing ucnv_setFromUCallBack...\n");
ucnv_setFromUCallBack(myConverter, otherUnicodeAction(MIA1), &BOM, &oldFromUAction, &oldFromUContext, &err);
if (U_FAILURE(err) || oldFromUAction != MIA1 || oldFromUContext != MIA1Context)
{
log_err("FAILURE! %s\n", myErrorName(err));
}
ucnv_getFromUCallBack(myConverter, &MIA1_2, &MIA1Context2);
if (MIA1_2 != otherUnicodeAction(MIA1) || MIA1Context2 != &BOM)
log_err("get From UCallBack failed\n");
else
log_verbose("get From UCallBack ok\n");
log_verbose("\n---Testing getFromUCallBack Roundtrip...\n");
ucnv_setFromUCallBack(myConverter,MIA1, MIA1Context, &oldFromUAction, &oldFromUContext, &err);
if (U_FAILURE(err) || oldFromUAction != otherUnicodeAction(MIA1) || oldFromUContext != &BOM)
{
log_err("FAILURE! %s\n", myErrorName(err));
}
ucnv_getFromUCallBack(myConverter, &MIA1_2, &MIA1Context2);
if (MIA1_2 != MIA1 || MIA1Context2 != MIA1Context)
log_err("get From UCallBack action failed\n");
else
log_verbose("get From UCallBack action ok\n");
/*testing ucnv_setToUCallBack with error conditions*/
err=U_ILLEGAL_ARGUMENT_ERROR;
log_verbose("\n---Testing setFromUCallBack. with err != U_ZERO_ERROR..\n");
ucnv_setFromUCallBack(myConverter, otherUnicodeAction(MIA1), &BOM, &oldFromUAction, &oldFromUContext, &err);
ucnv_getFromUCallBack(myConverter, &MIA1_2, &MIA1Context2);
if(MIA1_2 == otherUnicodeAction(MIA1) || MIA1Context2 == &BOM){
log_err("To setFromUCallBack with err != U_ZERO_ERROR is supposed to fail\n");
}
err=U_ZERO_ERROR;
/*testing ucnv_setToUCallBack() and ucnv_getToUCallBack()*/
ucnv_getToUCallBack(myConverter, &MIA2, &MIA2Context);
log_verbose("\n---Testing setTo UCallBack...\n");
ucnv_setToUCallBack(myConverter,otherCharAction(MIA2), &BOM, &oldToUAction, &oldToUContext, &err);
if (U_FAILURE(err) || oldToUAction != MIA2 || oldToUContext != MIA2Context)
{
log_err("FAILURE! %s\n", myErrorName(err));
}
ucnv_getToUCallBack(myConverter, &MIA2_2, &MIA2Context2);
if (MIA2_2 != otherCharAction(MIA2) || MIA2Context2 != &BOM)
log_err("To UCallBack failed\n");
else
log_verbose("To UCallBack ok\n");
log_verbose("\n---Testing setTo UCallBack Roundtrip...\n");
ucnv_setToUCallBack(myConverter,MIA2, MIA2Context, &oldToUAction, &oldToUContext, &err);
if (U_FAILURE(err) || oldToUAction != otherCharAction(MIA2) || oldToUContext != &BOM)
{ log_err("FAILURE! %s\n", myErrorName(err)); }
ucnv_getToUCallBack(myConverter, &MIA2_2, &MIA2Context2);
if (MIA2_2 != MIA2 || MIA2Context2 != MIA2Context)
log_err("To UCallBack failed\n");
else
log_verbose("To UCallBack ok\n");
/*testing ucnv_setToUCallBack with error conditions*/
err=U_ILLEGAL_ARGUMENT_ERROR;
log_verbose("\n---Testing setToUCallBack. with err != U_ZERO_ERROR..\n");
ucnv_setToUCallBack(myConverter,otherCharAction(MIA2), NULL, &oldToUAction, &oldToUContext, &err);
ucnv_getToUCallBack(myConverter, &MIA2_2, &MIA2Context2);
if (MIA2_2 == otherCharAction(MIA2) || MIA2Context2 == &BOM){
log_err("To setToUCallBack with err != U_ZERO_ERROR is supposed to fail\n");
}
err=U_ZERO_ERROR;
/*getcodepageid testing ucnv_getCCSID() */
log_verbose("\n----Testing getCCSID....\n");
cp = ucnv_getCCSID(myConverter,&err);
if (U_FAILURE(err))
{
log_err("FAILURE!..... %s\n", myErrorName(err));
}
if (cp != CodePageNumberToTest[codepage_index])
log_err("Codepage number test failed\n");
else
log_verbose("Codepage number test OK\n");
/*testing ucnv_getCCSID() with err != U_ZERO_ERROR*/
err=U_ILLEGAL_ARGUMENT_ERROR;
if( ucnv_getCCSID(myConverter,&err) != -1){
log_err("ucnv_getCCSID() with err != U_ZERO_ERROR is supposed to fail\n");
}
err=U_ZERO_ERROR;
/*getCodepagePlatform testing ucnv_getPlatform()*/
log_verbose("\n---Testing getCodepagePlatform ..\n");
if (CodePagesPlatform[codepage_index]!=ucnv_getPlatform(myConverter, &err))
log_err("Platform codepage test failed\n");
else
log_verbose("Platform codepage test ok\n");
if (U_FAILURE(err))
{
log_err("FAILURE! %s\n", myErrorName(err));
}
/*testing ucnv_getPlatform() with err != U_ZERO_ERROR*/
err= U_ILLEGAL_ARGUMENT_ERROR;
if(ucnv_getPlatform(myConverter, &err) != UCNV_UNKNOWN){
log_err("ucnv)getPlatform with err != U_ZERO_ERROR is supposed to fail\n");
}
err=U_ZERO_ERROR;
/*Reads the BOM*/
{
// Note: gcc produces a compile warning if the return value from fread() is ignored.
size_t numRead = fread(&BOM, sizeof(UChar), 1, ucs_file_in);
(void)numRead;
}
if (BOM!=0xFEFF && BOM!=0xFFFE)
{
log_err("File Missing BOM...Bailing!\n");
fclose(ucs_file_in);
break;
}
/*Reads in the file*/
while(!feof(ucs_file_in)&&(i+=(int32_t)fread(ucs_file_buffer+i, sizeof(UChar), 1, ucs_file_in)))
{
myUChar = ucs_file_buffer[i-1];
ucs_file_buffer[i-1] = (UChar)((BOM==0xFEFF)?myUChar:((myUChar >> 8) | (myUChar << 8))); /*adjust if BIG_ENDIAN*/
}
myUChar = ucs_file_buffer[i-1];
ucs_file_buffer[i-1] = (UChar)((BOM==0xFEFF)?myUChar:((myUChar >> 8) | (myUChar << 8))); /*adjust if BIG_ENDIAN Corner Case*/
/*testing ucnv_fromUChars() and ucnv_toUChars() */
/*uchar1---fromUChar--->output_cp_buffer --toUChar--->uchar2*/
uchar1=(UChar*)malloc(sizeof(UChar) * (i+1));
u_uastrcpy(uchar1,"");
u_strncpy(uchar1,ucs_file_buffer,i);
uchar1[i] = 0;
uchar3=(UChar*)malloc(sizeof(UChar)*(i+1));
u_uastrcpy(uchar3,"");
u_strncpy(uchar3,ucs_file_buffer,i);
uchar3[i] = 0;
/*Calls the Conversion Routine */
testLong1 = MAX_FILE_LEN;
log_verbose("\n---Testing ucnv_fromUChars()\n");
targetcapacity = ucnv_fromUChars(myConverter, output_cp_buffer, testLong1, uchar1, -1, &err);
if (U_FAILURE(err))
{
log_err("\nFAILURE...%s\n", myErrorName(err));
}
else
log_verbose(" ucnv_fromUChars() o.k.\n");
/*test the conversion routine */
log_verbose("\n---Testing ucnv_toUChars()\n");
/*call it first time for trapping the targetcapacity and size needed to allocate memory for the buffer uchar2 */
targetcapacity2=0;
targetsize = ucnv_toUChars(myConverter,
NULL,
targetcapacity2,
output_cp_buffer,
(int32_t)strlen(output_cp_buffer),
&err);
/*if there is an buffer overflow then trap the values and pass them and make the actual call*/
if(err==U_BUFFER_OVERFLOW_ERROR)
{
err=U_ZERO_ERROR;
uchar2=(UChar*)malloc((targetsize+1) * sizeof(UChar));
targetsize = ucnv_toUChars(myConverter,
uchar2,
targetsize+1,
output_cp_buffer,
(int32_t)strlen(output_cp_buffer),
&err);
if(U_FAILURE(err))
log_err("ucnv_toUChars() FAILED %s\n", myErrorName(err));
else
log_verbose(" ucnv_toUChars() o.k.\n");
if(u_strcmp(uchar1,uchar2)!=0)
log_err("equality test failed with conversion routine\n");
}
else
{
log_err("ERR: calling toUChars: Didn't get U_BUFFER_OVERFLOW .. expected it.\n");
}
/*Testing ucnv_fromUChars and ucnv_toUChars with error conditions*/
err=U_ILLEGAL_ARGUMENT_ERROR;
log_verbose("\n---Testing ucnv_fromUChars() with err != U_ZERO_ERROR\n");
targetcapacity = ucnv_fromUChars(myConverter, output_cp_buffer, testLong1, uchar1, -1, &err);
if (targetcapacity !=0) {
log_err("\nFAILURE: ucnv_fromUChars with err != U_ZERO_ERROR is expected to fail and return 0\n");
}
err=U_ZERO_ERROR;
log_verbose("\n---Testing ucnv_fromUChars() with converter=NULL\n");
targetcapacity = ucnv_fromUChars(NULL, output_cp_buffer, testLong1, uchar1, -1, &err);
if (targetcapacity !=0 || err != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("\nFAILURE: ucnv_fromUChars with converter=NULL is expected to fail\n");
}
err=U_ZERO_ERROR;
log_verbose("\n---Testing ucnv_fromUChars() with sourceLength = 0\n");
targetcapacity = ucnv_fromUChars(myConverter, output_cp_buffer, testLong1, uchar1, 0, &err);
if (targetcapacity !=0) {
log_err("\nFAILURE: ucnv_fromUChars with sourceLength 0 is expected to return 0\n");
}
log_verbose("\n---Testing ucnv_fromUChars() with targetLength = 0\n");
targetcapacity = ucnv_fromUChars(myConverter, output_cp_buffer, 0, uchar1, -1, &err);
if (err != U_BUFFER_OVERFLOW_ERROR) {
log_err("\nFAILURE: ucnv_fromUChars with targetLength 0 is expected to fail and throw U_BUFFER_OVERFLOW_ERROR\n");
}
/*toUChars with error conditions*/
targetsize = ucnv_toUChars(myConverter, uchar2, targetsize, output_cp_buffer, (int32_t)strlen(output_cp_buffer), &err);
if(targetsize != 0){
log_err("\nFAILURE: ucnv_toUChars with err != U_ZERO_ERROR is expected to fail and return 0\n");
}
err=U_ZERO_ERROR;
targetsize = ucnv_toUChars(myConverter, uchar2, -1, output_cp_buffer, (int32_t)strlen(output_cp_buffer), &err);
if(targetsize != 0 || err != U_ILLEGAL_ARGUMENT_ERROR){
log_err("\nFAILURE: ucnv_toUChars with targetsize < 0 is expected to throw U_ILLEGAL_ARGUMENT_ERROR and return 0\n");
}
err=U_ZERO_ERROR;
targetsize = ucnv_toUChars(myConverter, uchar2, 0, output_cp_buffer, 0, &err);
if (targetsize !=0) {
log_err("\nFAILURE: ucnv_toUChars with sourceLength 0 is expected to return 0\n");
}
targetcapacity2=0;
targetsize = ucnv_toUChars(myConverter, NULL, targetcapacity2, output_cp_buffer, (int32_t)strlen(output_cp_buffer), &err);
if (err != U_STRING_NOT_TERMINATED_WARNING) {
log_err("\nFAILURE: ucnv_toUChars(targetLength)->%s instead of U_STRING_NOT_TERMINATED_WARNING\n",
u_errorName(err));
}
err=U_ZERO_ERROR;
/*-----*/
/*testing for ucnv_fromUnicode() and ucnv_toUnicode() */
/*Clean up re-usable vars*/
log_verbose("Testing ucnv_fromUnicode().....\n");
tmp_ucs_buf=ucs_file_buffer_use;
ucnv_fromUnicode(myConverter, &mytarget_1,
mytarget + MAX_FILE_LEN,
&tmp_ucs_buf,
ucs_file_buffer_use+i,
NULL,
true,
&err);
consumedUni = (UChar*)tmp_consumedUni;
(void)consumedUni; /* Suppress set but not used warning. */
if (U_FAILURE(err))
{
log_err("FAILURE! %s\n", myErrorName(err));
}
else
log_verbose("ucnv_fromUnicode() o.k.\n");
/*Uni1 ----ToUnicode----> Cp2 ----FromUnicode---->Uni3 */
log_verbose("Testing ucnv_toUnicode().....\n");
tmp_mytarget_use=mytarget_use;
tmp_consumed = consumed;
ucnv_toUnicode(myConverter, &my_ucs_file_buffer_1,
my_ucs_file_buffer + MAX_FILE_LEN,
&tmp_mytarget_use,
mytarget_use + (mytarget_1 - mytarget),
NULL,
false,
&err);
consumed = (char*)tmp_consumed;
if (U_FAILURE(err))
{
log_err("FAILURE! %s\n", myErrorName(err));
}
else
log_verbose("ucnv_toUnicode() o.k.\n");
log_verbose("\n---Testing RoundTrip ...\n");
u_strncpy(uchar3, my_ucs_file_buffer,i);
uchar3[i] = 0;
if(u_strcmp(uchar1,uchar3)==0)
log_verbose("Equality test o.k.\n");
else
log_err("Equality test failed\n");
/*sanity compare */
if(uchar2 == NULL)
{
log_err("uchar2 was NULL (ccapitst.c line %d), couldn't do sanity check\n", __LINE__);
}
else
{
if(u_strcmp(uchar2, uchar3)==0)
log_verbose("Equality test o.k.\n");
else
log_err("Equality test failed\n");
}
fclose(ucs_file_in);
ucnv_close(myConverter);
if (uchar1 != 0) free(uchar1);
if (uchar2 != 0) free(uchar2);
if (uchar3 != 0) free(uchar3);
}
free((void*)mytarget);
free((void*)output_cp_buffer);
free((void*)ucs_file_buffer);
free((void*)my_ucs_file_buffer);
#endif
}
#if !UCONFIG_NO_LEGACY_CONVERSION
static UConverterFromUCallback otherUnicodeAction(UConverterFromUCallback MIA)
{
return (MIA==(UConverterFromUCallback)UCNV_FROM_U_CALLBACK_STOP)?(UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SUBSTITUTE:(UConverterFromUCallback)UCNV_FROM_U_CALLBACK_STOP;
}
static UConverterToUCallback otherCharAction(UConverterToUCallback MIA)
{
return (MIA==(UConverterToUCallback)UCNV_TO_U_CALLBACK_STOP)?(UConverterToUCallback)UCNV_TO_U_CALLBACK_SUBSTITUTE:(UConverterToUCallback)UCNV_TO_U_CALLBACK_STOP;
}
#endif
static void TestFlushCache(void) {
#if !UCONFIG_NO_LEGACY_CONVERSION
UErrorCode err = U_ZERO_ERROR;
UConverter* someConverters[5];
int flushCount = 0;
/* flush the converter cache to get a consistent state before the flushing is tested */
ucnv_flushCache();
/*Testing ucnv_open()*/
/* Note: These converters have been chosen because they do NOT
encode the Latin characters (U+0041, ...), and therefore are
highly unlikely to be chosen as system default codepages */
someConverters[0] = ucnv_open("ibm-1047", &err);
if (U_FAILURE(err)) {
log_data_err("FAILURE! %s\n", myErrorName(err));
}
someConverters[1] = ucnv_open("ibm-1047", &err);
if (U_FAILURE(err)) {
log_data_err("FAILURE! %s\n", myErrorName(err));
}
someConverters[2] = ucnv_open("ibm-1047", &err);
if (U_FAILURE(err)) {
log_data_err("FAILURE! %s\n", myErrorName(err));
}
someConverters[3] = ucnv_open("gb18030", &err);
if (U_FAILURE(err)) {
log_data_err("FAILURE! %s\n", myErrorName(err));
}
someConverters[4] = ucnv_open("ibm-954", &err);
if (U_FAILURE(err)) {
log_data_err("FAILURE! %s\n", myErrorName(err));
}
/* Testing ucnv_flushCache() */
log_verbose("\n---Testing ucnv_flushCache...\n");
if ((flushCount=ucnv_flushCache())==0)
log_verbose("Flush cache ok\n");
else
log_data_err("Flush Cache failed [line %d], expect 0 got %d \n", __LINE__, flushCount);
/*testing ucnv_close() and ucnv_flushCache() */
ucnv_close(someConverters[0]);
ucnv_close(someConverters[1]);
if ((flushCount=ucnv_flushCache())==0)
log_verbose("Flush cache ok\n");
else
log_data_err("Flush Cache failed [line %d], expect 0 got %d \n", __LINE__, flushCount);
ucnv_close(someConverters[2]);
ucnv_close(someConverters[3]);
if ((flushCount=ucnv_flushCache())==2)
log_verbose("Flush cache ok\n"); /*because first, second and third are same */
else
log_data_err("Flush Cache failed line %d, got %d expected 2 or there is an error in ucnv_close()\n",
__LINE__,
flushCount);
ucnv_close(someConverters[4]);
if ( (flushCount=ucnv_flushCache())==1)
log_verbose("Flush cache ok\n");
else
log_data_err("Flush Cache failed line %d, expected 1 got %d \n", __LINE__, flushCount);
#endif
}
/**
* Test the converter alias API, specifically the fuzzy matching of
* alias names and the alias table integrity. Make sure each
* converter has at least one alias (itself), and that its listed
* aliases map back to itself. Check some hard-coded UTF-8 and
* ISO_2022 aliases to make sure they work.
*/
static void TestAlias(void) {
int32_t i, ncnv;
UErrorCode status = U_ZERO_ERROR;
/* Predetermined aliases that we expect to map back to ISO_2022
* and UTF-8. UPDATE THIS DATA AS NECESSARY. */
const char* ISO_2022_NAMES[] =
{"ISO_2022,locale=ja,version=2", "ISO-2022-JP-2", "csISO2022JP2",
"Iso-2022jP2", "isO-2022_Jp_2", "iSo--2022,locale=ja,version=2"};
int32_t ISO_2022_NAMES_LENGTH = UPRV_LENGTHOF(ISO_2022_NAMES);
const char *UTF8_NAMES[] =
{ "UTF-8", "utf-8", "utf8", "ibm-1208",
"utf_8", "ibm1208", "cp1208" };
int32_t UTF8_NAMES_LENGTH = UPRV_LENGTHOF(UTF8_NAMES);
struct {
const char *name;
const char *alias;
} CONVERTERS_NAMES[] = {
{ "UTF-32BE", "UTF32_BigEndian" },
{ "UTF-32LE", "UTF32_LittleEndian" },
{ "UTF-32", "ISO-10646-UCS-4" },
{ "UTF32_PlatformEndian", "UTF32_PlatformEndian" },
{ "UTF-32", "ucs-4" }
};
int32_t CONVERTERS_NAMES_LENGTH = UPRV_LENGTHOF(CONVERTERS_NAMES);
/* When there are bugs in gencnval or in ucnv_io, converters can
appear to have no aliases. */
ncnv = ucnv_countAvailable();
log_verbose("%d converters\n", ncnv);
for (i=0; i<ncnv; ++i) {
const char *name = ucnv_getAvailableName(i);
const char *alias0;
uint16_t na = ucnv_countAliases(name, &status);
uint16_t j;
UConverter *cnv;
if (na == 0) {
log_err("FAIL: Converter \"%s\" (i=%d)"
" has no aliases; expect at least one\n",
name, i);
continue;
}
cnv = ucnv_open(name, &status);
if (U_FAILURE(status)) {
log_data_err("FAIL: Converter \"%s\" (i=%d)"
" can't be opened.\n",
name, i);
}
else {
if (strcmp(ucnv_getName(cnv, &status), name) != 0
&& (strstr(name, "PlatformEndian") == 0 && strstr(name, "OppositeEndian") == 0)) {
log_err("FAIL: Converter \"%s\" returned \"%s\" for getName. "
"They should be the same\n",
name, ucnv_getName(cnv, &status));
}
}
ucnv_close(cnv);
status = U_ZERO_ERROR;
alias0 = ucnv_getAlias(name, 0, &status);
for (j=1; j<na; ++j) {
const char *alias;
/* Make sure each alias maps back to the same list of
aliases. Assume that if alias 0 is the same, the whole
list is the same (this should always be true). */
const char *mapBack;
status = U_ZERO_ERROR;
alias = ucnv_getAlias(name, j, &status);
if (status == U_AMBIGUOUS_ALIAS_WARNING) {
log_err("FAIL: Converter \"%s\"is ambiguous\n", name);
}
if (alias == NULL) {
log_err("FAIL: Converter \"%s\" -> "
"alias[%d]=NULL\n",
name, j);
continue;
}
mapBack = ucnv_getAlias(alias, 0, &status);
if (mapBack == NULL) {
log_err("FAIL: Converter \"%s\" -> "
"alias[%d]=\"%s\" -> "
"alias[0]=NULL, exp. \"%s\"\n",
name, j, alias, alias0);
continue;
}
if (0 != strcmp(alias0, mapBack)) {
int32_t idx;
UBool foundAlias = false;
if (status == U_AMBIGUOUS_ALIAS_WARNING) {
/* Make sure that we only get this mismapping when there is
an ambiguous alias, and the other converter has this alias too. */
for (idx = 0; idx < ucnv_countAliases(mapBack, &status); idx++) {
if (strcmp(ucnv_getAlias(mapBack, (uint16_t)idx, &status), alias) == 0) {
foundAlias = true;
break;
}
}
}
/* else not ambiguous, and this is a real problem. foundAlias = false */
if (!foundAlias) {
log_err("FAIL: Converter \"%s\" -> "
"alias[%d]=\"%s\" -> "
"alias[0]=\"%s\", exp. \"%s\"\n",
name, j, alias, mapBack, alias0);
}
}
}
}
/* Check a list of predetermined aliases that we expect to map
* back to ISO_2022 and UTF-8. */
for (i=1; i<ISO_2022_NAMES_LENGTH; ++i) {
const char* mapBack = ucnv_getAlias(ISO_2022_NAMES[i], 0, &status);
if(!mapBack) {
log_data_err("Couldn't get alias for %s. You probably have no data\n", ISO_2022_NAMES[i]);
continue;
}
if (0 != strcmp(mapBack, ISO_2022_NAMES[0])) {
log_err("FAIL: \"%s\" -> \"%s\", expect \"ISO_2022,locale=ja,version=2\"\n",
ISO_2022_NAMES[i], mapBack);
}
}
for (i=1; i<UTF8_NAMES_LENGTH; ++i) {
const char* mapBack = ucnv_getAlias(UTF8_NAMES[i], 0, &status);
if(!mapBack) {
log_data_err("Couldn't get alias for %s. You probably have no data\n", UTF8_NAMES[i]);
continue;
}
if (mapBack && 0 != strcmp(mapBack, UTF8_NAMES[0])) {
log_err("FAIL: \"%s\" -> \"%s\", expect UTF-8\n",
UTF8_NAMES[i], mapBack);
}
}
/*
* Check a list of predetermined aliases that we expect to map
* back to predermined converter names.
*/
for (i = 0; i < CONVERTERS_NAMES_LENGTH; ++i) {
const char* mapBack = ucnv_getAlias(CONVERTERS_NAMES[i].alias, 0, &status);
if(!mapBack) {
log_data_err("Couldn't get alias for %s. You probably have no data\n", CONVERTERS_NAMES[i].name);
continue;
}
if (0 != strcmp(mapBack, CONVERTERS_NAMES[i].name)) {
log_err("FAIL: \"%s\" -> \"%s\", expect %s\n",
CONVERTERS_NAMES[i].alias, mapBack, CONVERTERS_NAMES[i].name);
}
}
}
static void TestDuplicateAlias(void) {
const char *alias;
UErrorCode status = U_ZERO_ERROR;
status = U_ZERO_ERROR;
alias = ucnv_getStandardName("Shift_JIS", "IBM", &status);
if (alias == NULL || strcmp(alias, "ibm-943") != 0 || status != U_AMBIGUOUS_ALIAS_WARNING) {
log_data_err("FAIL: Didn't get ibm-943 for Shift_JIS {IBM}. Got %s\n", alias);
}
status = U_ZERO_ERROR;
alias = ucnv_getStandardName("ibm-943", "IANA", &status);
if (alias == NULL || strcmp(alias, "Shift_JIS") != 0 || status != U_AMBIGUOUS_ALIAS_WARNING) {
log_data_err("FAIL: Didn't get Shift_JIS for ibm-943 {IANA}. Got %s\n", alias);
}
status = U_ZERO_ERROR;
alias = ucnv_getStandardName("ibm-943_P130-2000", "IANA", &status);
if (alias != NULL || status == U_AMBIGUOUS_ALIAS_WARNING) {
log_data_err("FAIL: Didn't get NULL for ibm-943 {IANA}. Got %s\n", alias);
}
}
/* Test safe clone callback */
static uint32_t TSCC_nextSerial(void)
{
static uint32_t n = 1;
return (n++);
}
typedef struct
{
uint32_t magic; /* 0xC0FFEE to identify that the object is OK */
uint32_t serial; /* minted from nextSerial, above */
UBool wasClosed; /* close happened on the object */
} TSCCContext;
static TSCCContext *TSCC_clone(TSCCContext *ctx)
{
TSCCContext *newCtx = (TSCCContext *)malloc(sizeof(TSCCContext));
newCtx->serial = TSCC_nextSerial();
newCtx->wasClosed = 0;
newCtx->magic = 0xC0FFEE;
log_verbose("TSCC_clone: %p:%d -> new context %p:%d\n", ctx, ctx->serial, newCtx, newCtx->serial);
return newCtx;
}
#if !UCONFIG_NO_LEGACY_CONVERSION
static void TSCC_fromU(const void *context,
UConverterFromUnicodeArgs *fromUArgs,
const UChar* codeUnits,
int32_t length,
UChar32 codePoint,
UConverterCallbackReason reason,
UErrorCode * err)
{
// suppress compiler warnings about unused variables
(void)codeUnits;
(void)length;
(void)codePoint;
TSCCContext *ctx = (TSCCContext*)context;
UConverterFromUCallback junkFrom;
log_verbose("TSCC_fromU: Context %p:%d called, reason %d on cnv %p\n", ctx, ctx->serial, reason, fromUArgs->converter);
if(ctx->magic != 0xC0FFEE) {
log_err("TSCC_fromU: Context %p:%d magic is 0x%x should be 0xC0FFEE.\n", ctx,ctx->serial, ctx->magic);
return;
}
if(reason == UCNV_CLONE) {
UErrorCode subErr = U_ZERO_ERROR;
TSCCContext *newCtx;
TSCCContext *junkCtx;
TSCCContext **pjunkCtx = &junkCtx;
/* "recreate" it */
log_verbose("TSCC_fromU: cloning..\n");
newCtx = TSCC_clone(ctx);
if(newCtx == NULL) {
log_err("TSCC_fromU: internal clone failed on %p\n", ctx);
}
/* now, SET it */
ucnv_getFromUCallBack(fromUArgs->converter, &junkFrom, (const void**)pjunkCtx);
ucnv_setFromUCallBack(fromUArgs->converter, junkFrom, newCtx, NULL, NULL, &subErr);
if(U_FAILURE(subErr)) {
*err = subErr;
}
}
if(reason == UCNV_CLOSE) {
log_verbose("TSCC_fromU: Context %p:%d closing\n", ctx, ctx->serial);
ctx->wasClosed = true;
}
}
static void TSCC_toU(const void *context,
UConverterToUnicodeArgs *toUArgs,
const char* codeUnits,
int32_t length,
UConverterCallbackReason reason,
UErrorCode * err)
{
// suppress compiler warnings about unused variables
(void)codeUnits;
(void)length;
TSCCContext *ctx = (TSCCContext*)context;
UConverterToUCallback junkFrom;
log_verbose("TSCC_toU: Context %p:%d called, reason %d on cnv %p\n", ctx, ctx->serial, reason, toUArgs->converter);
if(ctx->magic != 0xC0FFEE) {
log_err("TSCC_toU: Context %p:%d magic is 0x%x should be 0xC0FFEE.\n", ctx,ctx->serial, ctx->magic);
return;
}
if(reason == UCNV_CLONE) {
UErrorCode subErr = U_ZERO_ERROR;
TSCCContext *newCtx;
TSCCContext *junkCtx;
TSCCContext **pjunkCtx = &junkCtx;
/* "recreate" it */
log_verbose("TSCC_toU: cloning..\n");
newCtx = TSCC_clone(ctx);
if(newCtx == NULL) {
log_err("TSCC_toU: internal clone failed on %p\n", ctx);
}
/* now, SET it */
ucnv_getToUCallBack(toUArgs->converter, &junkFrom, (const void**)pjunkCtx);
ucnv_setToUCallBack(toUArgs->converter, junkFrom, newCtx, NULL, NULL, &subErr);
if(U_FAILURE(subErr)) {
*err = subErr;
}
}
if(reason == UCNV_CLOSE) {
log_verbose("TSCC_toU: Context %p:%d closing\n", ctx, ctx->serial);
ctx->wasClosed = true;
}
}
static void TSCC_init(TSCCContext *q)
{
q->magic = 0xC0FFEE;
q->serial = TSCC_nextSerial();
q->wasClosed = 0;
}
static void TSCC_print_log(TSCCContext *q, const char *name)
{
if(q==NULL) {
log_verbose("TSCContext: %s is NULL!!\n", name);
} else {
if(q->magic != 0xC0FFEE) {
log_err("TSCCContext: %p:%d's magic is %x, supposed to be 0xC0FFEE\n",
q,q->serial, q->magic);
}
log_verbose("TSCCContext %p:%d=%s - magic %x, %s\n",
q, q->serial, name, q->magic, q->wasClosed?"CLOSED":"open");
}
}
static void TestConvertSafeCloneCallback(void)
{
UErrorCode err = U_ZERO_ERROR;
TSCCContext from1, to1;
TSCCContext *from2, *from3, *to2, *to3;
TSCCContext **pfrom2 = &from2, **pfrom3 = &from3, **pto2 = &to2, **pto3 = &to3;
char hunk[8192];
int32_t hunkSize = 8192;
UConverterFromUCallback junkFrom;
UConverterToUCallback junkTo;
UConverter *conv1, *conv2 = NULL;
conv1 = ucnv_open("iso-8859-3", &err);
if(U_FAILURE(err)) {
log_data_err("Err opening iso-8859-3, %s\n", u_errorName(err));
return;
}
log_verbose("Opened conv1=%p\n", conv1);
TSCC_init(&from1);
TSCC_init(&to1);
TSCC_print_log(&from1, "from1");
TSCC_print_log(&to1, "to1");
ucnv_setFromUCallBack(conv1, TSCC_fromU, &from1, NULL, NULL, &err);
log_verbose("Set from1 on conv1\n");
TSCC_print_log(&from1, "from1");
ucnv_setToUCallBack(conv1, TSCC_toU, &to1, NULL, NULL, &err);
log_verbose("Set to1 on conv1\n");
TSCC_print_log(&to1, "to1");
conv2 = ucnv_safeClone(conv1, hunk, &hunkSize, &err);
if(U_FAILURE(err)) {
log_err("safeClone failed: %s\n", u_errorName(err));
return;
}
log_verbose("Cloned to conv2=%p.\n", conv2);
/********** from *********************/
ucnv_getFromUCallBack(conv2, &junkFrom, (const void**)pfrom2);
ucnv_getFromUCallBack(conv1, &junkFrom, (const void**)pfrom3);
TSCC_print_log(from2, "from2");
TSCC_print_log(from3, "from3(==from1)");
if(from2 == NULL) {
log_err("FAIL! from2 is null \n");
return;
}
if(from3 == NULL) {
log_err("FAIL! from3 is null \n");
return;
}
if(from3 != (&from1) ) {
log_err("FAIL! conv1's FROM context changed!\n");
}
if(from2 == (&from1) ) {
log_err("FAIL! conv1's FROM context is the same as conv2's!\n");
}
if(from1.wasClosed) {
log_err("FAIL! from1 is closed \n");
}
if(from2->wasClosed) {
log_err("FAIL! from2 was closed\n");
}
/********** to *********************/
ucnv_getToUCallBack(conv2, &junkTo, (const void**)pto2);
ucnv_getToUCallBack(conv1, &junkTo, (const void**)pto3);
TSCC_print_log(to2, "to2");
TSCC_print_log(to3, "to3(==to1)");
if(to2 == NULL) {
log_err("FAIL! to2 is null \n");
return;
}
if(to3 == NULL) {
log_err("FAIL! to3 is null \n");
return;
}
if(to3 != (&to1) ) {
log_err("FAIL! conv1's TO context changed!\n");
}
if(to2 == (&to1) ) {
log_err("FAIL! conv1's TO context is the same as conv2's!\n");
}
if(to1.wasClosed) {
log_err("FAIL! to1 is closed \n");
}
if(to2->wasClosed) {
log_err("FAIL! to2 was closed\n");
}
/*************************************/
ucnv_close(conv1);
log_verbose("ucnv_closed (conv1)\n");
TSCC_print_log(&from1, "from1");
TSCC_print_log(from2, "from2");
TSCC_print_log(&to1, "to1");
TSCC_print_log(to2, "to2");
if(from1.wasClosed == false) {
log_err("FAIL! from1 is NOT closed \n");
}
if(from2->wasClosed) {
log_err("FAIL! from2 was closed\n");
}
if(to1.wasClosed == false) {
log_err("FAIL! to1 is NOT closed \n");
}
if(to2->wasClosed) {
log_err("FAIL! to2 was closed\n");
}
ucnv_close(conv2);
log_verbose("ucnv_closed (conv2)\n");
TSCC_print_log(&from1, "from1");
TSCC_print_log(from2, "from2");
if(from1.wasClosed == false) {
log_err("FAIL! from1 is NOT closed \n");
}
if(from2->wasClosed == false) {
log_err("FAIL! from2 was NOT closed\n");
}
TSCC_print_log(&to1, "to1");
TSCC_print_log(to2, "to2");
if(to1.wasClosed == false) {
log_err("FAIL! to1 is NOT closed \n");
}
if(to2->wasClosed == false) {
log_err("FAIL! to2 was NOT closed\n");
}
if(to2 != (&to1)) {
free(to2); /* to1 is stack based */
}
if(from2 != (&from1)) {
free(from2); /* from1 is stack based */
}
}
#endif
static UBool
containsAnyOtherByte(uint8_t *p, int32_t length, uint8_t b) {
while(length>0) {
if(*p!=b) {
return true;
}
++p;
--length;
}
return false;
}
static void TestConvertSafeClone(void)
{
/* one 'regular' & all the 'private stateful' converters */
static const char *const names[] = {
#if !UCONFIG_NO_LEGACY_CONVERSION
"ibm-1047",
"ISO_2022,locale=zh,version=1",
#endif
"SCSU",
#if !UCONFIG_NO_LEGACY_CONVERSION
"HZ",
"lmbcs",
"ISCII,version=0",
"ISO_2022,locale=kr,version=1",
"ISO_2022,locale=jp,version=2",
#endif
"BOCU-1",
"UTF-7",
#if !UCONFIG_NO_LEGACY_CONVERSION
"IMAP-mailbox-name",
"ibm-1047-s390"
#else
"IMAP=mailbox-name"
#endif
};
/* store the actual sizes of each converter */
int32_t actualSizes[UPRV_LENGTHOF(names)];
static const int32_t bufferSizes[] = {
U_CNV_SAFECLONE_BUFFERSIZE,
(int32_t)(3*sizeof(UConverter))/2, /* 1.5*sizeof(UConverter) */
(int32_t)sizeof(UConverter)/2 /* 0.5*sizeof(UConverter) */
};
char charBuffer[21]; /* Leave at an odd number for alignment testing */
uint8_t buffer[3] [U_CNV_SAFECLONE_BUFFERSIZE];
int32_t bufferSize, maxBufferSize;
const char *maxName;
UConverter * cnv, *cnv2;
UErrorCode err;
char *pCharBuffer;
const char *pConstCharBuffer;
const char *charBufferLimit = charBuffer + UPRV_LENGTHOF(charBuffer);
UChar uniBuffer[] = {0x0058, 0x0059, 0x005A}; /* "XYZ" */
UChar uniCharBuffer[20];
char charSourceBuffer[] = { 0x1b, 0x24, 0x42 };
const char *pCharSource = charSourceBuffer;
const char *pCharSourceLimit = charSourceBuffer + sizeof(charSourceBuffer);
UChar *pUCharTarget = uniCharBuffer;
UChar *pUCharTargetLimit = uniCharBuffer + UPRV_LENGTHOF(uniCharBuffer);
const UChar * pUniBuffer;
const UChar *uniBufferLimit = uniBuffer + UPRV_LENGTHOF(uniBuffer);
int32_t idx, j;
err = U_ZERO_ERROR;
cnv = ucnv_open(names[0], &err);
if(U_SUCCESS(err)) {
/* Check the various error & informational states: */
/* Null status - just returns NULL */
bufferSize = U_CNV_SAFECLONE_BUFFERSIZE;
if (NULL != ucnv_safeClone(cnv, buffer[0], &bufferSize, NULL))
{
log_err("FAIL: Cloned converter failed to deal correctly with null status\n");
}
/* error status - should return 0 & keep error the same */
err = U_MEMORY_ALLOCATION_ERROR;
if (NULL != ucnv_safeClone(cnv, buffer[0], &bufferSize, &err) || err != U_MEMORY_ALLOCATION_ERROR)
{
log_err("FAIL: Cloned converter failed to deal correctly with incoming error status\n");
}
err = U_ZERO_ERROR;
/* Null buffer size pointer is ok */
if (NULL == (cnv2 = ucnv_safeClone(cnv, buffer[0], NULL, &err)) || U_FAILURE(err))
{
log_err("FAIL: Cloned converter failed to deal correctly with null bufferSize pointer\n");
}
ucnv_close(cnv2);
err = U_ZERO_ERROR;
/* buffer size pointer is 0 - fill in pbufferSize with a size */
bufferSize = 0;
if (NULL != ucnv_safeClone(cnv, buffer[0], &bufferSize, &err) || U_FAILURE(err) || bufferSize <= 0)
{
log_err("FAIL: Cloned converter failed a sizing request ('preflighting')\n");
}
/* Verify our define is large enough */
if (U_CNV_SAFECLONE_BUFFERSIZE < bufferSize)
{
log_err("FAIL: Pre-calculated buffer size is too small\n");
}
/* Verify we can use this run-time calculated size */
if (NULL == (cnv2 = ucnv_safeClone(cnv, buffer[0], &bufferSize, &err)) || U_FAILURE(err))
{
log_err("FAIL: Converter can't be cloned with run-time size\n");
}
if (cnv2) {
ucnv_close(cnv2);
}
/* size one byte too small - should allocate & let us know */
--bufferSize;
if (NULL == (cnv2 = ucnv_safeClone(cnv, NULL, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
{
log_err("FAIL: Cloned converter failed to deal correctly with too-small buffer size\n");
}
if (cnv2) {
ucnv_close(cnv2);
}
err = U_ZERO_ERROR;
bufferSize = U_CNV_SAFECLONE_BUFFERSIZE;
/* Null buffer pointer - return converter & set error to U_SAFECLONE_ALLOCATED_ERROR */
if (NULL == (cnv2 = ucnv_safeClone(cnv, NULL, &bufferSize, &err)) || err != U_SAFECLONE_ALLOCATED_WARNING)
{
log_err("FAIL: Cloned converter failed to deal correctly with null buffer pointer\n");
}
if (cnv2) {
ucnv_close(cnv2);
}
err = U_ZERO_ERROR;
/* Null converter - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
if (NULL != ucnv_safeClone(NULL, buffer[0], &bufferSize, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
{
log_err("FAIL: Cloned converter failed to deal correctly with null converter pointer\n");
}
ucnv_close(cnv);
}
maxBufferSize = 0;
maxName = "";
/* Do these cloned converters work at all - shuffle UChars to chars & back again..*/
for(j = 0; j < UPRV_LENGTHOF(bufferSizes); ++j) {
for (idx = 0; idx < UPRV_LENGTHOF(names); idx++)
{
err = U_ZERO_ERROR;
cnv = ucnv_open(names[idx], &err);
if(U_FAILURE(err)) {
log_data_err("ucnv_open(\"%s\") failed - %s\n", names[idx], u_errorName(err));
continue;
}
if(j == 0) {
/* preflight to get maxBufferSize */
actualSizes[idx] = 0;
ucnv_safeClone(cnv, NULL, &actualSizes[idx], &err);
if(actualSizes[idx] > maxBufferSize) {
maxBufferSize = actualSizes[idx];
maxName = names[idx];
}
}
memset(buffer, 0xaa, sizeof(buffer));
bufferSize = bufferSizes[j];
cnv2 = ucnv_safeClone(cnv, buffer[1], &bufferSize, &err);
/* close the original immediately to make sure that the clone works by itself */
ucnv_close(cnv);
if( actualSizes[idx] <= (bufferSizes[j] - (int32_t)alignof(UConverter)) &&
err == U_SAFECLONE_ALLOCATED_WARNING
) {
log_err("ucnv_safeClone(%s) did a heap clone although the buffer was large enough\n", names[idx]);
}
/* check if the clone function overwrote any bytes that it is not supposed to touch */
if(bufferSize <= bufferSizes[j]) {
/* used the stack buffer */
if( containsAnyOtherByte(buffer[0], (int32_t)sizeof(buffer[0]), 0xaa) ||
containsAnyOtherByte(buffer[1]+bufferSize, (int32_t)(sizeof(buffer)-(sizeof(buffer[0])+bufferSize)), 0xaa)
) {
log_err("cloning %s in a stack buffer overwrote bytes outside the bufferSize %d (requested %d)\n",
names[idx], bufferSize, bufferSizes[j]);
}
} else {
/* heap-allocated the clone */
if(containsAnyOtherByte(buffer[0], (int32_t)sizeof(buffer), 0xaa)) {
log_err("cloning %s used the heap (bufferSize %d, requested %d) but overwrote stack buffer bytes\n",
names[idx], bufferSize, bufferSizes[j]);
}
}
pCharBuffer = charBuffer;
pUniBuffer = uniBuffer;
ucnv_fromUnicode(cnv2,
&pCharBuffer,
charBufferLimit,
&pUniBuffer,
uniBufferLimit,
NULL,
true,
&err);
if(U_FAILURE(err)){
log_err("FAIL: cloned converter failed to do fromU conversion. Error: %s\n",u_errorName(err));
}
ucnv_toUnicode(cnv2,
&pUCharTarget,
pUCharTargetLimit,
&pCharSource,
pCharSourceLimit,
NULL,
true,
&err
);
if(U_FAILURE(err)){
log_err("FAIL: cloned converter failed to do toU conversion. Error: %s\n",u_errorName(err));
}
pConstCharBuffer = charBuffer;
if (uniBuffer [0] != ucnv_getNextUChar(cnv2, &pConstCharBuffer, pCharBuffer, &err))
{
log_err("FAIL: Cloned converter failed to do conversion. Error: %s\n",u_errorName(err));
}
ucnv_close(cnv2);
}
}
log_verbose("ucnv_safeClone(): sizeof(UConverter)=%lu max preflighted clone size=%d (%s) U_CNV_SAFECLONE_BUFFERSIZE=%d\n",
sizeof(UConverter), maxBufferSize, maxName, U_CNV_SAFECLONE_BUFFERSIZE);
if(maxBufferSize > U_CNV_SAFECLONE_BUFFERSIZE) {
log_err("ucnv_safeClone(): max preflighted clone size=%d (%s) is larger than U_CNV_SAFECLONE_BUFFERSIZE=%d\n",
maxBufferSize, maxName, U_CNV_SAFECLONE_BUFFERSIZE);
}
}
static void TestConvertClone(void)
{
/* one 'regular' & all the 'private stateful' converters */
static const char *const names[] = {
#if !UCONFIG_NO_LEGACY_CONVERSION
"ibm-1047",
"ISO_2022,locale=zh,version=1",
#endif
"SCSU",
#if !UCONFIG_NO_LEGACY_CONVERSION
"HZ",
"lmbcs",
"ISCII,version=0",
"ISO_2022,locale=kr,version=1",
"ISO_2022,locale=jp,version=2",
#endif
"BOCU-1",
"UTF-7",
#if !UCONFIG_NO_LEGACY_CONVERSION
"IMAP-mailbox-name",
"ibm-1047-s390"
#else
"IMAP=mailbox-name"
#endif
};
char charBuffer[21]; /* Leave at an odd number for alignment testing */
UConverter * cnv, *cnv2;
UErrorCode err;
char *pCharBuffer;
const char *pConstCharBuffer;
const char *charBufferLimit = charBuffer + UPRV_LENGTHOF(charBuffer);
UChar uniBuffer[] = {0x0058, 0x0059, 0x005A}; /* "XYZ" */
UChar uniCharBuffer[20];
char charSourceBuffer[] = { 0x1b, 0x24, 0x42 };
const char *pCharSource = charSourceBuffer;
const char *pCharSourceLimit = charSourceBuffer + sizeof(charSourceBuffer);
UChar *pUCharTarget = uniCharBuffer;
UChar *pUCharTargetLimit = uniCharBuffer + UPRV_LENGTHOF(uniCharBuffer);
const UChar * pUniBuffer;
const UChar *uniBufferLimit = uniBuffer + UPRV_LENGTHOF(uniBuffer);
int32_t idx;
err = U_ZERO_ERROR;
cnv = ucnv_open(names[0], &err);
if(U_SUCCESS(err)) {
/* Check the various error & informational states: */
/* Null status - just returns NULL */
if (NULL != ucnv_clone(cnv, NULL))
{
log_err("FAIL: Cloned converter failed to deal correctly with null status\n");
}
/* error status - should return 0 & keep error the same */
err = U_MEMORY_ALLOCATION_ERROR;
if (NULL != ucnv_clone(cnv, &err) || err != U_MEMORY_ALLOCATION_ERROR)
{
log_err("FAIL: Cloned converter failed to deal correctly with incoming error status\n");
}
err = U_ZERO_ERROR;
/* Null buffer size pointer is ok */
if (NULL == (cnv2 = ucnv_clone(cnv, &err)) || U_FAILURE(err))
{
log_err("FAIL: Failed to clone.\n");
}
ucnv_close(cnv2);
err = U_ZERO_ERROR;
/* Null converter - return NULL & set U_ILLEGAL_ARGUMENT_ERROR */
if (NULL != ucnv_clone(NULL, &err) || err != U_ILLEGAL_ARGUMENT_ERROR)
{
log_err("FAIL: Cloned converter failed to deal correctly with null converter pointer\n");
}
ucnv_close(cnv);
}
/* Do these cloned converters work at all - shuffle UChars to chars & back again..*/
for (idx = 0; idx < UPRV_LENGTHOF(names); idx++)
{
err = U_ZERO_ERROR;
cnv = ucnv_open(names[idx], &err);
if(U_FAILURE(err)) {
log_data_err("ucnv_open(\"%s\") failed - %s\n", names[idx], u_errorName(err));
continue;
}
cnv2 = ucnv_clone(cnv, &err);
/* close the original immediately to make sure that the clone works by itself */
ucnv_close(cnv);
pCharBuffer = charBuffer;
pUniBuffer = uniBuffer;
ucnv_fromUnicode(cnv2,
&pCharBuffer,
charBufferLimit,
&pUniBuffer,
uniBufferLimit,
NULL,
true,
&err);
if(U_FAILURE(err)){
log_err("FAIL: cloned converter failed to do fromU conversion. Error: %s\n",u_errorName(err));
}
ucnv_toUnicode(cnv2,
&pUCharTarget,
pUCharTargetLimit,
&pCharSource,
pCharSourceLimit,
NULL,
true,
&err
);
if(U_FAILURE(err)){
log_err("FAIL: cloned converter failed to do toU conversion. Error: %s\n",u_errorName(err));
}
pConstCharBuffer = charBuffer;
if (uniBuffer [0] != ucnv_getNextUChar(cnv2, &pConstCharBuffer, pCharBuffer, &err))
{
log_err("FAIL: Cloned converter failed to do conversion. Error: %s\n",u_errorName(err));
}
ucnv_close(cnv2);
}
}
static void TestCCSID(void) {
#if !UCONFIG_NO_LEGACY_CONVERSION
UConverter *cnv;
UErrorCode errorCode;
int32_t ccsids[]={ 37, 850, 943, 949, 950, 1047, 1252, 1392, 33722 };
int32_t i, ccsid;
for(i=0; i<UPRV_LENGTHOF(ccsids); ++i) {
ccsid=ccsids[i];
errorCode=U_ZERO_ERROR;
cnv=ucnv_openCCSID(ccsid, UCNV_IBM, &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("error: ucnv_openCCSID(%ld) failed (%s)\n", ccsid, u_errorName(errorCode));
continue;
}
if(ccsid!=ucnv_getCCSID(cnv, &errorCode)) {
log_err("error: ucnv_getCCSID(ucnv_openCCSID(%ld))=%ld\n", ccsid, ucnv_getCCSID(cnv, &errorCode));
}
/* skip gb18030(ccsid 1392) */
if(ccsid != 1392 && UCNV_IBM!=ucnv_getPlatform(cnv, &errorCode)) {
log_err("error: ucnv_getPlatform(ucnv_openCCSID(%ld))=%ld!=UCNV_IBM\n", ccsid, ucnv_getPlatform(cnv, &errorCode));
}
ucnv_close(cnv);
}
#endif
}
/* jitterbug 932: ucnv_convert() bugs --------------------------------------- */
/* CHUNK_SIZE defined in common\ucnv.c: */
#define CHUNK_SIZE 1024
static void bug1(void);
static void bug2(void);
static void bug3(void);
static void
TestJ932(void)
{
bug1(); /* Unicode intermediate buffer straddle bug */
bug2(); /* pre-flighting size incorrect caused by simple overflow */
bug3(); /* pre-flighting size incorrect caused by expansion overflow */
}
/*
* jitterbug 932: test chunking boundary conditions in
int32_t ucnv_convert(const char *toConverterName,
const char *fromConverterName,
char *target,
int32_t targetSize,
const char *source,
int32_t sourceSize,
UErrorCode * err)
* See discussions on the icu mailing list in
* 2001-April with the subject "converter 'flush' question".
*
* Bug report and test code provided by Edward J. Batutis.
*/
static void bug1(void)
{
#if !UCONFIG_NO_LEGACY_CONVERSION
char char_in[CHUNK_SIZE+32];
char char_out[CHUNK_SIZE*2];
/* GB 18030 equivalent of U+10000 is 90308130 */
static const char test_seq[]={ (char)0x90u, 0x30, (char)0x81u, 0x30 };
UErrorCode err = U_ZERO_ERROR;
int32_t i, test_seq_len = sizeof(test_seq);
/*
* causes straddle bug in Unicode intermediate buffer by sliding the test sequence forward
* until the straddle bug appears. I didn't want to hard-code everything so this test could
* be expanded - however this is the only type of straddle bug I can think of at the moment -
* a high surrogate in the last position of the Unicode intermediate buffer. Apparently no
* other Unicode sequences cause a bug since combining sequences are not supported by the
* converters.
*/
for (i = test_seq_len; i >= 0; i--) {
/* put character sequence into input buffer */
memset(char_in, 0x61, sizeof(char_in)); /* GB 18030 'a' */
memcpy(char_in + (CHUNK_SIZE - i), test_seq, test_seq_len);
/* do the conversion */
ucnv_convert("us-ascii", /* out */
"gb18030", /* in */
char_out,
sizeof(char_out),
char_in,
sizeof(char_in),
&err);
/* bug1: */
if (err == U_TRUNCATED_CHAR_FOUND) {
/* this happens when surrogate pair straddles the intermediate buffer in
* T_UConverter_fromCodepageToCodepage */
log_err("error j932 bug 1: expected success, got U_TRUNCATED_CHAR_FOUND\n");
}
}
#endif
}
/* bug2: pre-flighting loop bug: simple overflow causes bug */
static void bug2(void)
{
/* US-ASCII "1234567890" */
static const char source[]={ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 };
#if !UCONFIG_ONLY_HTML_CONVERSION
static const char sourceUTF8[]={ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, (char)0xef, (char)0x80, (char)0x80 };
static const char sourceUTF32[]={ 0x00, 0x00, 0x00, 0x30,
0x00, 0x00, 0x00, 0x31,
0x00, 0x00, 0x00, 0x32,
0x00, 0x00, 0x00, 0x33,
0x00, 0x00, 0x00, 0x34,
0x00, 0x00, 0x00, 0x35,
0x00, 0x00, 0x00, 0x36,
0x00, 0x00, 0x00, 0x37,
0x00, 0x00, 0x00, 0x38,
0x00, 0x00, (char)0xf0, 0x00};
#endif
static char target[5];
UErrorCode err = U_ZERO_ERROR;
int32_t size;
/* do the conversion */
size = ucnv_convert("iso-8859-1", /* out */
"us-ascii", /* in */
target,
sizeof(target),
source,
sizeof(source),
&err);
if ( size != 10 ) {
/* bug2: size is 5, should be 10 */
log_data_err("error j932 bug 2 us-ascii->iso-8859-1: got preflighting size %d instead of 10\n", size);
}
#if !UCONFIG_ONLY_HTML_CONVERSION
err = U_ZERO_ERROR;
/* do the conversion */
size = ucnv_convert("UTF-32BE", /* out */
"UTF-8", /* in */
target,
sizeof(target),
sourceUTF8,
sizeof(sourceUTF8),
&err);
if ( size != 32 ) {
/* bug2: size is 5, should be 32 */
log_err("error j932 bug 2 UTF-8->UTF-32BE: got preflighting size %d instead of 32\n", size);
}
err = U_ZERO_ERROR;
/* do the conversion */
size = ucnv_convert("UTF-8", /* out */
"UTF-32BE", /* in */
target,
sizeof(target),
sourceUTF32,
sizeof(sourceUTF32),
&err);
if ( size != 12 ) {
/* bug2: size is 5, should be 12 */
log_err("error j932 bug 2 UTF-32BE->UTF-8: got preflighting size %d instead of 12\n", size);
}
#endif
}
/*
* bug3: when the characters expand going from source to target codepage
* you get bug3 in addition to bug2
*/
static void bug3(void)
{
#if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
char char_in[CHUNK_SIZE*4];
char target[5];
UErrorCode err = U_ZERO_ERROR;
int32_t size;
/*
* first get the buggy size from bug2 then
* compare it to buggy size with an expansion
*/
memset(char_in, 0x61, sizeof(char_in)); /* US-ASCII 'a' */
/* do the conversion */
size = ucnv_convert("lmbcs", /* out */
"us-ascii", /* in */
target,
sizeof(target),
char_in,
sizeof(char_in),
&err);
if ( size != sizeof(char_in) ) {
/*
* bug2: size is 0x2805 (CHUNK_SIZE*2+5 - maybe 5 is the size of the overflow buffer
* in the converter?), should be CHUNK_SIZE*4
*
* Markus 2001-05-18: 5 is the size of our target[] here, ucnv_convert() did not reset targetSize...
*/
log_data_err("error j932 bug 2/3a: expected preflighting size 0x%04x, got 0x%04x\n", sizeof(char_in), size);
}
/*
* now do the conversion with expansion
* ascii 0x08 expands to 0x0F 0x28 in lmbcs
*/
memset(char_in, 8, sizeof(char_in));
err = U_ZERO_ERROR;
/* do the conversion */
size = ucnv_convert("lmbcs", /* out */
"us-ascii", /* in */
target,
sizeof(target),
char_in,
sizeof(char_in),
&err);
/* expect 2X expansion */
if ( size != sizeof(char_in) * 2 ) {
/*
* bug3:
* bug2 would lead us to expect 0x2805, but it isn't that either, it is 0x3c05:
*/
log_data_err("error j932 bug 3b: expected 0x%04x, got 0x%04x\n", sizeof(char_in) * 2, size);
}
#endif
}
static void
convertExStreaming(UConverter *srcCnv, UConverter *targetCnv,
const char *src, int32_t srcLength,
const char *expectTarget, int32_t expectTargetLength,
int32_t chunkSize,
const char *testName,
UErrorCode expectCode) {
UChar pivotBuffer[CHUNK_SIZE];
UChar *pivotSource, *pivotTarget;
const UChar *pivotLimit;
char targetBuffer[CHUNK_SIZE];
char *target;
const char *srcLimit, *finalSrcLimit, *targetLimit;
int32_t targetLength;
UBool flush;
UErrorCode errorCode;
/* setup */
if(chunkSize>CHUNK_SIZE) {
chunkSize=CHUNK_SIZE;
}
pivotSource=pivotTarget=pivotBuffer;
pivotLimit=pivotBuffer+chunkSize;
finalSrcLimit=src+srcLength;
target=targetBuffer;
targetLimit=targetBuffer+chunkSize;
ucnv_resetToUnicode(srcCnv);
ucnv_resetFromUnicode(targetCnv);
errorCode=U_ZERO_ERROR;
flush=false;
/* convert, streaming-style (both converters and pivot keep state) */
for(;;) {
/* for testing, give ucnv_convertEx() at most <chunkSize> input/pivot/output units at a time */
if(src+chunkSize<=finalSrcLimit) {
srcLimit=src+chunkSize;
} else {
srcLimit=finalSrcLimit;
}
ucnv_convertEx(targetCnv, srcCnv,
&target, targetLimit,
&src, srcLimit,
pivotBuffer, &pivotSource, &pivotTarget, pivotLimit,
false, flush, &errorCode);
targetLength=(int32_t)(target-targetBuffer);
if(target>targetLimit) {
log_err("ucnv_convertEx(%s) chunk[%d] target %p exceeds targetLimit %p\n",
testName, chunkSize, target, targetLimit);
break; /* TODO: major problem! */
}
if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
/* continue converting another chunk */
errorCode=U_ZERO_ERROR;
if(targetLength+chunkSize<=(int32_t)sizeof(targetBuffer)) {
targetLimit=target+chunkSize;
} else {
targetLimit=targetBuffer+(int32_t)sizeof(targetBuffer);
}
} else if(U_FAILURE(errorCode)) {
/* failure */
break;
} else if(flush) {
/* all done */
break;
} else if(src==finalSrcLimit && pivotSource==pivotTarget) {
/* all consumed, now flush without input (separate from conversion for testing) */
flush=true;
}
}
if(!(errorCode==expectCode || (expectCode==U_ZERO_ERROR && errorCode==U_STRING_NOT_TERMINATED_WARNING))) {
log_err("ucnv_convertEx(%s) chunk[%d] results in %s instead of %s\n",
testName, chunkSize, u_errorName(errorCode), u_errorName(expectCode));
} else if(targetLength!=expectTargetLength) {
log_err("ucnv_convertEx(%s) chunk[%d] writes %d bytes instead of %d\n",
testName, chunkSize, targetLength, expectTargetLength);
} else if(memcmp(targetBuffer, expectTarget, targetLength)!=0) {
log_err("ucnv_convertEx(%s) chunk[%d] writes different bytes than expected\n",
testName, chunkSize);
}
}
static void
convertExMultiStreaming(UConverter *srcCnv, UConverter *targetCnv,
const char *src, int32_t srcLength,
const char *expectTarget, int32_t expectTargetLength,
const char *testName,
UErrorCode expectCode) {
convertExStreaming(srcCnv, targetCnv,
src, srcLength,
expectTarget, expectTargetLength,
1, testName, expectCode);
convertExStreaming(srcCnv, targetCnv,
src, srcLength,
expectTarget, expectTargetLength,
3, testName, expectCode);
convertExStreaming(srcCnv, targetCnv,
src, srcLength,
expectTarget, expectTargetLength,
7, testName, expectCode);
}
static void TestConvertEx(void) {
#if !UCONFIG_NO_LEGACY_CONVERSION
static const uint8_t
utf8[]={
/* 4e00 30a1 ff61 0410 */
0xe4, 0xb8, 0x80, 0xe3, 0x82, 0xa1, 0xef, 0xbd, 0xa1, 0xd0, 0x90
},
shiftJIS[]={
0x88, 0xea, 0x83, 0x40, 0xa1, 0x84, 0x40
},
errorTarget[]={
/*
* expected output when converting shiftJIS[] from UTF-8 to Shift-JIS:
* SUB, SUB, 0x40, SUB, SUB, 0x40
*/
0xfc, 0xfc, 0xfc, 0xfc, 0x40, 0xfc, 0xfc, 0xfc, 0xfc, 0x40
};
char srcBuffer[100], targetBuffer[100];
const char *src;
char *target;
UChar pivotBuffer[100];
UChar *pivotSource, *pivotTarget;
UConverter *cnv1, *cnv2;
UErrorCode errorCode;
errorCode=U_ZERO_ERROR;
cnv1=ucnv_open("UTF-8", &errorCode);
if(U_FAILURE(errorCode)) {
log_err("unable to open a UTF-8 converter - %s\n", u_errorName(errorCode));
return;
}
cnv2=ucnv_open("Shift-JIS", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("unable to open a Shift-JIS converter - %s\n", u_errorName(errorCode));
ucnv_close(cnv1);
return;
}
/* test ucnv_convertEx() with streaming conversion style */
convertExMultiStreaming(cnv1, cnv2,
(const char *)utf8, sizeof(utf8), (const char *)shiftJIS, sizeof(shiftJIS),
"UTF-8 -> Shift-JIS", U_ZERO_ERROR);
convertExMultiStreaming(cnv2, cnv1,
(const char *)shiftJIS, sizeof(shiftJIS), (const char *)utf8, sizeof(utf8),
"Shift-JIS -> UTF-8", U_ZERO_ERROR);
/* U_ZERO_ERROR because by default the SUB callbacks are set */
convertExMultiStreaming(cnv1, cnv2,
(const char *)shiftJIS, sizeof(shiftJIS), (const char *)errorTarget, sizeof(errorTarget),
"shiftJIS[] UTF-8 -> Shift-JIS", U_ZERO_ERROR);
/* test some simple conversions */
/* NUL-terminated source and target */
errorCode=U_STRING_NOT_TERMINATED_WARNING;
memcpy(srcBuffer, utf8, sizeof(utf8));
srcBuffer[sizeof(utf8)]=0;
src=srcBuffer;
target=targetBuffer;
ucnv_convertEx(cnv2, cnv1, &target, targetBuffer+sizeof(targetBuffer), &src, NULL,
NULL, NULL, NULL, NULL, true, true, &errorCode);
if( errorCode!=U_ZERO_ERROR ||
target-targetBuffer!=sizeof(shiftJIS) ||
*target!=0 ||
memcmp(targetBuffer, shiftJIS, sizeof(shiftJIS))!=0
) {
log_err("ucnv_convertEx(simple UTF-8 -> Shift_JIS) fails: %s - writes %d bytes, expect %d\n",
u_errorName(errorCode), target-targetBuffer, sizeof(shiftJIS));
}
/* NUL-terminated source and U_STRING_NOT_TERMINATED_WARNING */
errorCode=U_AMBIGUOUS_ALIAS_WARNING;
memset(targetBuffer, 0xff, sizeof(targetBuffer));
src=srcBuffer;
target=targetBuffer;
ucnv_convertEx(cnv2, cnv1, &target, targetBuffer+sizeof(shiftJIS), &src, NULL,
NULL, NULL, NULL, NULL, true, true, &errorCode);
if( errorCode!=U_STRING_NOT_TERMINATED_WARNING ||
target-targetBuffer!=sizeof(shiftJIS) ||
*target!=(char)0xff ||
memcmp(targetBuffer, shiftJIS, sizeof(shiftJIS))!=0
) {
log_err("ucnv_convertEx(simple UTF-8 -> Shift_JIS) fails: %s, expect U_STRING_NOT_TERMINATED_WARNING - writes %d bytes, expect %d\n",
u_errorName(errorCode), target-targetBuffer, sizeof(shiftJIS));
}
/* bad arguments */
errorCode=U_MESSAGE_PARSE_ERROR;
src=srcBuffer;
target=targetBuffer;
ucnv_convertEx(cnv2, cnv1, &target, targetBuffer+sizeof(targetBuffer), &src, NULL,
NULL, NULL, NULL, NULL, true, true, &errorCode);
if(errorCode!=U_MESSAGE_PARSE_ERROR) {
log_err("ucnv_convertEx(U_MESSAGE_PARSE_ERROR) sets %s\n", u_errorName(errorCode));
}
/* pivotLimit==pivotStart */
errorCode=U_ZERO_ERROR;
pivotSource=pivotTarget=pivotBuffer;
ucnv_convertEx(cnv2, cnv1, &target, targetBuffer+sizeof(targetBuffer), &src, NULL,
pivotBuffer, &pivotSource, &pivotTarget, pivotBuffer, true, true, &errorCode);
if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_convertEx(pivotLimit==pivotStart) sets %s\n", u_errorName(errorCode));
}
/* *pivotSource==NULL */
errorCode=U_ZERO_ERROR;
pivotSource=NULL;
ucnv_convertEx(cnv2, cnv1, &target, targetBuffer+sizeof(targetBuffer), &src, NULL,
pivotBuffer, &pivotSource, &pivotTarget, pivotBuffer+1, true, true, &errorCode);
if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_convertEx(*pivotSource==NULL) sets %s\n", u_errorName(errorCode));
}
/* *source==NULL */
errorCode=U_ZERO_ERROR;
src=NULL;
pivotSource=pivotBuffer;
ucnv_convertEx(cnv2, cnv1, &target, targetBuffer+sizeof(targetBuffer), &src, NULL,
pivotBuffer, &pivotSource, &pivotTarget, pivotBuffer+1, true, true, &errorCode);
if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_convertEx(*source==NULL) sets %s\n", u_errorName(errorCode));
}
/* streaming conversion without a pivot buffer */
errorCode=U_ZERO_ERROR;
src=srcBuffer;
pivotSource=pivotBuffer;
ucnv_convertEx(cnv2, cnv1, &target, targetBuffer+sizeof(targetBuffer), &src, NULL,
NULL, &pivotSource, &pivotTarget, pivotBuffer+1, true, false, &errorCode);
if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_convertEx(pivotStart==NULL) sets %s\n", u_errorName(errorCode));
}
ucnv_close(cnv1);
ucnv_close(cnv2);
#endif
}
/* Test illegal UTF-8 input: Data and functions for TestConvertExFromUTF8(). */
static const char *const badUTF8[]={
/* trail byte */
"\x80",
/* truncated multi-byte sequences */
"\xd0",
"\xe0",
"\xe1",
"\xed",
"\xee",
"\xf0",
"\xf1",
"\xf4",
"\xf8",
"\xfc",
"\xe0\x80",
"\xe0\xa0",
"\xe1\x80",
"\xed\x80",
"\xed\xa0",
"\xee\x80",
"\xf0\x80",
"\xf0\x90",
"\xf1\x80",
"\xf4\x80",
"\xf4\x90",
"\xf8\x80",
"\xfc\x80",
"\xf0\x80\x80",
"\xf0\x90\x80",
"\xf1\x80\x80",
"\xf4\x80\x80",
"\xf4\x90\x80",
"\xf8\x80\x80",
"\xfc\x80\x80",
"\xf8\x80\x80\x80",
"\xfc\x80\x80\x80",
"\xfc\x80\x80\x80\x80",
/* complete sequences but non-shortest forms or out of range etc. */
"\xc0\x80",
"\xe0\x80\x80",
"\xed\xa0\x80",
"\xf0\x80\x80\x80",
"\xf4\x90\x80\x80",
"\xf8\x80\x80\x80\x80",
"\xfc\x80\x80\x80\x80\x80",
"\xfe",
"\xff"
};
#define ARG_CHAR_ARR_SIZE 8
/* get some character that can be converted and convert it */
static UBool getTestChar(UConverter *cnv, const char *converterName,
char charUTF8[4], int32_t *pCharUTF8Length,
char char0[ARG_CHAR_ARR_SIZE], int32_t *pChar0Length,
char char1[ARG_CHAR_ARR_SIZE], int32_t *pChar1Length) {
UChar utf16[U16_MAX_LENGTH];
int32_t utf16Length;
const UChar *utf16Source;
char *target;
USet *set;
UChar32 c;
UErrorCode errorCode;
errorCode=U_ZERO_ERROR;
set=uset_open(1, 0);
ucnv_getUnicodeSet(cnv, set, UCNV_ROUNDTRIP_SET, &errorCode);
c=uset_charAt(set, uset_size(set)/2);
uset_close(set);
utf16Length=0;
U16_APPEND_UNSAFE(utf16, utf16Length, c);
*pCharUTF8Length=0;
U8_APPEND_UNSAFE(charUTF8, *pCharUTF8Length, c);
utf16Source=utf16;
target=char0;
ucnv_fromUnicode(cnv,
&target, char0+ARG_CHAR_ARR_SIZE,
&utf16Source, utf16+utf16Length,
NULL, false, &errorCode);
*pChar0Length=(int32_t)(target-char0);
utf16Source=utf16;
target=char1;
ucnv_fromUnicode(cnv,
&target, char1+ARG_CHAR_ARR_SIZE,
&utf16Source, utf16+utf16Length,
NULL, false, &errorCode);
*pChar1Length=(int32_t)(target-char1);
if(U_FAILURE(errorCode)) {
log_err("unable to get test character for %s - %s\n", converterName, u_errorName(errorCode));
return false;
}
return true;
}
static UBool isOneTruncatedUTF8(const char *s, int32_t length) {
if(length==0) {
return false;
} else if(length==1) {
return U8_IS_LEAD(s[0]);
} else {
int32_t count=U8_COUNT_TRAIL_BYTES(s[0]);
if(length<=count) {
// 2 or more bytes, but fewer than the lead byte indicates.
int32_t oneLength=0;
U8_FWD_1(s, oneLength, length);
// Truncated if we reach the end of the string.
// Not true if the lead byte and first trail byte do not start a valid sequence,
// e.g., E0 80 -> oneLength=1.
return oneLength==length;
}
return false;
}
}
static void testFromTruncatedUTF8(UConverter *utf8Cnv, UConverter *cnv, const char *converterName,
char charUTF8[4], int32_t charUTF8Length,
char char0[8], int32_t char0Length,
char char1[8], int32_t char1Length) {
// suppress compiler warnings about unused variables
(void)char0;
(void)char0Length;
(void)char1;
(void)char1Length;
char utf8[16];
int32_t utf8Length;
char output[16];
int32_t outputLength;
char invalidChars[8];
int8_t invalidLength;
const char *source;
char *target;
UChar pivotBuffer[8];
UChar *pivotSource, *pivotTarget;
UErrorCode errorCode;
int32_t i;
/* test truncated sequences */
errorCode=U_ZERO_ERROR;
ucnv_setToUCallBack(utf8Cnv, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &errorCode);
memcpy(utf8, charUTF8, charUTF8Length);
for(i=0; i<UPRV_LENGTHOF(badUTF8); ++i) {
/* truncated sequence? */
int32_t length = (int32_t)strlen(badUTF8[i]);
if(!isOneTruncatedUTF8(badUTF8[i], length)) {
continue;
}
/* assemble a string with the test character and the truncated sequence */
memcpy(utf8+charUTF8Length, badUTF8[i], length);
utf8Length=charUTF8Length+length;
/* convert and check the invalidChars */
source=utf8;
target=output;
pivotSource=pivotTarget=pivotBuffer;
errorCode=U_ZERO_ERROR;
ucnv_convertEx(cnv, utf8Cnv,
&target, output+sizeof(output),
&source, utf8+utf8Length,
pivotBuffer, &pivotSource, &pivotTarget, pivotBuffer+UPRV_LENGTHOF(pivotBuffer),
true, true, /* reset & flush */
&errorCode);
outputLength=(int32_t)(target-output);
(void)outputLength; /* Suppress set but not used warning. */
if(errorCode!=U_TRUNCATED_CHAR_FOUND || pivotSource!=pivotBuffer) {
log_err("unexpected error %s from %s badUTF8[%ld]\n", u_errorName(errorCode), converterName, (long)i);
continue;
}
errorCode=U_ZERO_ERROR;
invalidLength=(int8_t)sizeof(invalidChars);
ucnv_getInvalidChars(utf8Cnv, invalidChars, &invalidLength, &errorCode);
if(invalidLength!=length || 0!=memcmp(invalidChars, badUTF8[i], length)) {
log_err("wrong invalidChars from %s badUTF8[%ld]\n", converterName, (long)i);
}
}
}
static void testFromBadUTF8(UConverter *utf8Cnv, UConverter *cnv, const char *converterName,
char charUTF8[4], int32_t charUTF8Length,
char char0[8], int32_t char0Length,
char char1[8], int32_t char1Length) {
char utf8[600], expect[600];
int32_t utf8Length, expectLength;
char testName[32];
UErrorCode errorCode;
int32_t i;
errorCode=U_ZERO_ERROR;
ucnv_setToUCallBack(utf8Cnv, UCNV_TO_U_CALLBACK_SKIP, NULL, NULL, NULL, &errorCode);
/*
* assemble an input string with the test character between each
* bad sequence,
* and an expected string with repeated test character output
*/
memcpy(utf8, charUTF8, charUTF8Length);
utf8Length=charUTF8Length;
memcpy(expect, char0, char0Length);
expectLength=char0Length;
for(i=0; i<UPRV_LENGTHOF(badUTF8); ++i) {
int32_t length = (int32_t)strlen(badUTF8[i]);
memcpy(utf8+utf8Length, badUTF8[i], length);
utf8Length+=length;
memcpy(utf8+utf8Length, charUTF8, charUTF8Length);
utf8Length+=charUTF8Length;
memcpy(expect+expectLength, char1, char1Length);
expectLength+=char1Length;
}
/* expect that each bad UTF-8 sequence is detected and skipped */
strcpy(testName, "from bad UTF-8 to ");
strcat(testName, converterName);
convertExMultiStreaming(utf8Cnv, cnv,
utf8, utf8Length,
expect, expectLength,
testName,
U_ZERO_ERROR);
}
/* Test illegal UTF-8 input. */
static void TestConvertExFromUTF8(void) {
static const char *const converterNames[]={
#if !UCONFIG_NO_LEGACY_CONVERSION
"windows-1252",
"shift-jis",
#endif
"us-ascii",
"iso-8859-1",
"utf-8"
};
UConverter *utf8Cnv, *cnv;
UErrorCode errorCode;
int32_t i;
/* fromUnicode versions of some character, from initial state and later */
char charUTF8[4], char0[8], char1[8];
int32_t charUTF8Length, char0Length, char1Length;
errorCode=U_ZERO_ERROR;
utf8Cnv=ucnv_open("UTF-8", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("unable to open UTF-8 converter - %s\n", u_errorName(errorCode));
return;
}
for(i=0; i<UPRV_LENGTHOF(converterNames); ++i) {
errorCode=U_ZERO_ERROR;
cnv=ucnv_open(converterNames[i], &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("unable to open %s converter - %s\n", converterNames[i], u_errorName(errorCode));
continue;
}
if(!getTestChar(cnv, converterNames[i], charUTF8, &charUTF8Length, char0, &char0Length, char1, &char1Length)) {
continue;
}
testFromTruncatedUTF8(utf8Cnv, cnv, converterNames[i], charUTF8, charUTF8Length, char0, char0Length, char1, char1Length);
testFromBadUTF8(utf8Cnv, cnv, converterNames[i], charUTF8, charUTF8Length, char0, char0Length, char1, char1Length);
ucnv_close(cnv);
}
ucnv_close(utf8Cnv);
}
static void TestConvertExFromUTF8_C5F0(void) {
static const char *const converterNames[]={
#if !UCONFIG_NO_LEGACY_CONVERSION
"windows-1251",
"shift-jis",
#endif
"us-ascii",
"iso-8859-1",
"utf-8"
};
UConverter *utf8Cnv, *cnv;
UErrorCode errorCode;
int32_t i;
static const char bad_utf8[2]={ (char)0xC5, (char)0xF0 };
/* Expect "��" (2x U+FFFD as decimal NCRs) */
static const char twoNCRs[16]={
0x26, 0x23, 0x36, 0x35, 0x35, 0x33, 0x33, 0x3B,
0x26, 0x23, 0x36, 0x35, 0x35, 0x33, 0x33, 0x3B
};
static const char twoFFFD[6]={
(char)0xef, (char)0xbf, (char)0xbd,
(char)0xef, (char)0xbf, (char)0xbd
};
const char *expected;
int32_t expectedLength;
char dest[20]; /* longer than longest expectedLength */
const char *src;
char *target;
UChar pivotBuffer[128];
UChar *pivotSource, *pivotTarget;
errorCode=U_ZERO_ERROR;
utf8Cnv=ucnv_open("UTF-8", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("unable to open UTF-8 converter - %s\n", u_errorName(errorCode));
return;
}
for(i=0; i<UPRV_LENGTHOF(converterNames); ++i) {
errorCode=U_ZERO_ERROR;
cnv=ucnv_open(converterNames[i], &errorCode);
ucnv_setFromUCallBack(cnv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_DEC,
NULL, NULL, &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("unable to open %s converter - %s\n",
converterNames[i], u_errorName(errorCode));
continue;
}
src=bad_utf8;
target=dest;
uprv_memset(dest, 9, sizeof(dest));
if(i==UPRV_LENGTHOF(converterNames)-1) {
/* conversion to UTF-8 yields two U+FFFD directly */
expected=twoFFFD;
expectedLength=6;
} else {
/* conversion to a non-Unicode charset yields two NCRs */
expected=twoNCRs;
expectedLength=16;
}
pivotBuffer[0]=0;
pivotBuffer[1]=1;
pivotBuffer[2]=2;
pivotSource=pivotTarget=pivotBuffer;
ucnv_convertEx(
cnv, utf8Cnv,
&target, dest+expectedLength,
&src, bad_utf8+sizeof(bad_utf8),
pivotBuffer, &pivotSource, &pivotTarget, pivotBuffer+UPRV_LENGTHOF(pivotBuffer),
true, true, &errorCode);
if( errorCode!=U_STRING_NOT_TERMINATED_WARNING || src!=bad_utf8+2 ||
target!=dest+expectedLength || 0!=uprv_memcmp(dest, expected, expectedLength) ||
dest[expectedLength]!=9
) {
log_err("ucnv_convertEx(UTF-8 C5 F0 -> %s/decimal NCRs) failed\n", converterNames[i]);
}
ucnv_close(cnv);
}
ucnv_close(utf8Cnv);
}
static void
TestConvertAlgorithmic(void) {
#if !UCONFIG_NO_LEGACY_CONVERSION
static const uint8_t
utf8[]={
/* 4e00 30a1 ff61 0410 */
0xe4, 0xb8, 0x80, 0xe3, 0x82, 0xa1, 0xef, 0xbd, 0xa1, 0xd0, 0x90
},
shiftJIS[]={
0x88, 0xea, 0x83, 0x40, 0xa1, 0x84, 0x40
},
/*errorTarget[]={*/
/*
* expected output when converting shiftJIS[] from UTF-8 to Shift-JIS:
* SUB, SUB, 0x40, SUB, SUB, 0x40
*/
/* 0x81, 0xa1, 0x81, 0xa1, 0x40, 0x81, 0xa1, 0x81, 0xa1, 0x40*/
/*},*/
utf16[]={
0xfe, 0xff /* BOM only, no text */
};
#if !UCONFIG_ONLY_HTML_CONVERSION
static const uint8_t utf32[]={
0xff, 0xfe, 0, 0 /* BOM only, no text */
};
#endif
char target[100], utf8NUL[100], shiftJISNUL[100];
UConverter *cnv;
UErrorCode errorCode;
int32_t length;
errorCode=U_ZERO_ERROR;
cnv=ucnv_open("Shift-JIS", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("unable to open a Shift-JIS converter - %s\n", u_errorName(errorCode));
ucnv_close(cnv);
return;
}
memcpy(utf8NUL, utf8, sizeof(utf8));
utf8NUL[sizeof(utf8)]=0;
memcpy(shiftJISNUL, shiftJIS, sizeof(shiftJIS));
shiftJISNUL[sizeof(shiftJIS)]=0;
/*
* The to/from algorithmic convenience functions share a common implementation,
* so we need not test all permutations of them.
*/
/* length in, not terminated out */
errorCode=U_ZERO_ERROR;
length=ucnv_fromAlgorithmic(cnv, UCNV_UTF8, target, sizeof(shiftJIS), (const char *)utf8, sizeof(utf8), &errorCode);
if( errorCode!=U_STRING_NOT_TERMINATED_WARNING ||
length!=sizeof(shiftJIS) ||
memcmp(target, shiftJIS, length)!=0
) {
log_err("ucnv_fromAlgorithmic(UTF-8 -> Shift-JIS) fails (%s expect U_STRING_NOT_TERMINATED_WARNING), returns %d expect %d\n",
u_errorName(errorCode), length, sizeof(shiftJIS));
}
/* terminated in and out */
memset(target, 0x55, sizeof(target));
errorCode=U_STRING_NOT_TERMINATED_WARNING;
length=ucnv_toAlgorithmic(UCNV_UTF8, cnv, target, sizeof(target), shiftJISNUL, -1, &errorCode);
if( errorCode!=U_ZERO_ERROR ||
length!=sizeof(utf8) ||
memcmp(target, utf8, length)!=0
) {
log_err("ucnv_toAlgorithmic(Shift-JIS -> UTF-8) fails (%s expect U_ZERO_ERROR), returns %d expect %d\n",
u_errorName(errorCode), length, sizeof(shiftJIS));
}
/* empty string, some target buffer */
errorCode=U_STRING_NOT_TERMINATED_WARNING;
length=ucnv_toAlgorithmic(UCNV_UTF8, cnv, target, sizeof(target), shiftJISNUL, 0, &errorCode);
if( errorCode!=U_ZERO_ERROR ||
length!=0
) {
log_err("ucnv_toAlgorithmic(empty string -> UTF-8) fails (%s expect U_ZERO_ERROR), returns %d expect 0\n",
u_errorName(errorCode), length);
}
/* pseudo-empty string, no target buffer */
errorCode=U_ZERO_ERROR;
length=ucnv_fromAlgorithmic(cnv, UCNV_UTF16, target, 0, (const char *)utf16, 2, &errorCode);
if( errorCode!=U_STRING_NOT_TERMINATED_WARNING ||
length!=0
) {
log_err("ucnv_fromAlgorithmic(UTF-16 only BOM -> Shift-JIS) fails (%s expect U_STRING_NOT_TERMINATED_WARNING), returns %d expect 0\n",
u_errorName(errorCode), length);
}
#if !UCONFIG_ONLY_HTML_CONVERSION
errorCode=U_ZERO_ERROR;
length=ucnv_fromAlgorithmic(cnv, UCNV_UTF32, target, 0, (const char *)utf32, 4, &errorCode);
if( errorCode!=U_STRING_NOT_TERMINATED_WARNING ||
length!=0
) {
log_err("ucnv_fromAlgorithmic(UTF-32 only BOM -> Shift-JIS) fails (%s expect U_STRING_NOT_TERMINATED_WARNING), returns %d expect 0\n",
u_errorName(errorCode), length);
}
#endif
/* bad arguments */
errorCode=U_MESSAGE_PARSE_ERROR;
length=ucnv_fromAlgorithmic(cnv, UCNV_UTF16, target, 0, (const char *)utf16, 2, &errorCode);
if(errorCode!=U_MESSAGE_PARSE_ERROR) {
log_err("ucnv_fromAlgorithmic(U_MESSAGE_PARSE_ERROR) sets %s\n", u_errorName(errorCode));
}
/* source==NULL */
errorCode=U_ZERO_ERROR;
length=ucnv_fromAlgorithmic(cnv, UCNV_UTF16, target, 0, NULL, 2, &errorCode);
if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_fromAlgorithmic(source==NULL) sets %s\n", u_errorName(errorCode));
}
/* illegal alg. type */
errorCode=U_ZERO_ERROR;
length=ucnv_fromAlgorithmic(cnv, (UConverterType)99, target, 0, (const char *)utf16, 2, &errorCode);
if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_fromAlgorithmic(illegal alg. type) sets %s\n", u_errorName(errorCode));
}
ucnv_close(cnv);
#endif
}
#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION
static void TestLMBCSMaxChar(void) {
static const struct {
int8_t maxSize;
const char *name;
} converter[] = {
/* some non-LMBCS converters - perfect test setup here */
{ 1, "US-ASCII"},
{ 1, "ISO-8859-1"},
{ 2, "UTF-16"},
{ 2, "UTF-16BE"},
{ 3, "UTF-8"},
{ 3, "CESU-8"},
{ 3, "SCSU"},
{ 4, "UTF-32"},
{ 4, "UTF-7"},
{ 4, "IMAP-mailbox-name"},
{ 4, "BOCU-1"},
{ 1, "windows-1256"},
{ 2, "Shift-JIS"},
{ 2, "ibm-16684"},
{ 3, "ibm-930"},
{ 3, "ibm-1390"},
{ 4, "*test3"},
{ 16,"*test4"},
{ 4, "ISCII"},
{ 4, "HZ"},
{ 3, "ISO-2022"},
{ 8, "ISO-2022-KR"},
{ 6, "ISO-2022-JP"},
{ 8, "ISO-2022-CN"},
/* LMBCS */
{ 3, "LMBCS-1"},
{ 3, "LMBCS-2"},
{ 3, "LMBCS-3"},
{ 3, "LMBCS-4"},
{ 3, "LMBCS-5"},
{ 3, "LMBCS-6"},
{ 3, "LMBCS-8"},
{ 3, "LMBCS-11"},
{ 3, "LMBCS-16"},
{ 3, "LMBCS-17"},
{ 3, "LMBCS-18"},
{ 3, "LMBCS-19"}
};
int32_t idx;
for (idx = 0; idx < UPRV_LENGTHOF(converter); idx++) {
UErrorCode status = U_ZERO_ERROR;
UConverter *cnv = cnv_open(converter[idx].name, &status);
if (U_FAILURE(status)) {
continue;
}
if (converter[idx].maxSize != ucnv_getMaxCharSize(cnv)) {
log_err("error: ucnv_getMaxCharSize(%s) expected %d, got %d\n",
converter[idx].name, converter[idx].maxSize, ucnv_getMaxCharSize(cnv));
}
ucnv_close(cnv);
}
/* mostly test that the macro compiles */
if(UCNV_GET_MAX_BYTES_FOR_STRING(1, 2)<10) {
log_err("error UCNV_GET_MAX_BYTES_FOR_STRING(1, 2)<10\n");
}
}
#endif
static void TestJ1968(void) {
UErrorCode err = U_ZERO_ERROR;
UConverter *cnv;
char myConvName[] = "My really really really really really really really really really really really"
" really really really really really really really really really really really"
" really really really really really really really really long converter name";
UChar myConvNameU[sizeof(myConvName)];
u_charsToUChars(myConvName, myConvNameU, sizeof(myConvName));
err = U_ZERO_ERROR;
myConvNameU[UCNV_MAX_CONVERTER_NAME_LENGTH+1] = 0;
cnv = ucnv_openU(myConvNameU, &err);
if (cnv || err != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("1U) Didn't get U_ILLEGAL_ARGUMENT_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
myConvNameU[UCNV_MAX_CONVERTER_NAME_LENGTH] = 0;
cnv = ucnv_openU(myConvNameU, &err);
if (cnv || err != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("2U) Didn't get U_ILLEGAL_ARGUMENT_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
myConvNameU[UCNV_MAX_CONVERTER_NAME_LENGTH-1] = 0;
cnv = ucnv_openU(myConvNameU, &err);
if (cnv || err != U_FILE_ACCESS_ERROR) {
log_err("3U) Didn't get U_FILE_ACCESS_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
cnv = ucnv_open(myConvName, &err);
if (cnv || err != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("1) Didn't get U_ILLEGAL_ARGUMENT_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
myConvName[UCNV_MAX_CONVERTER_NAME_LENGTH] = ',';
cnv = ucnv_open(myConvName, &err);
if (cnv || err != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("2) Didn't get U_ILLEGAL_ARGUMENT_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
myConvName[UCNV_MAX_CONVERTER_NAME_LENGTH-1] = ',';
cnv = ucnv_open(myConvName, &err);
if (cnv || err != U_FILE_ACCESS_ERROR) {
log_err("3) Didn't get U_FILE_ACCESS_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
myConvName[UCNV_MAX_CONVERTER_NAME_LENGTH-1] = ',';
memcpy(myConvName + UCNV_MAX_CONVERTER_NAME_LENGTH, "locale=", 7);
cnv = ucnv_open(myConvName, &err);
if (cnv || err != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("4) Didn't get U_ILLEGAL_ARGUMENT_ERROR as expected %s\n", u_errorName(err));
}
/* The comma isn't really a part of the converter name. */
err = U_ZERO_ERROR;
myConvName[UCNV_MAX_CONVERTER_NAME_LENGTH] = 0;
cnv = ucnv_open(myConvName, &err);
if (cnv || err != U_FILE_ACCESS_ERROR) {
log_err("5) Didn't get U_FILE_ACCESS_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
myConvName[UCNV_MAX_CONVERTER_NAME_LENGTH-1] = ' ';
cnv = ucnv_open(myConvName, &err);
if (cnv || err != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("6) Didn't get U_ILLEGAL_ARGUMENT_ERROR as expected %s\n", u_errorName(err));
}
err = U_ZERO_ERROR;
myConvName[UCNV_MAX_CONVERTER_NAME_LENGTH-1] = 0;
cnv = ucnv_open(myConvName, &err);
if (cnv || err != U_FILE_ACCESS_ERROR) {
log_err("7) Didn't get U_FILE_ACCESS_ERROR as expected %s\n", u_errorName(err));
}
}
#if !UCONFIG_NO_LEGACY_CONVERSION
static void
testSwap(const char *name, UBool swap) {
/*
* Test Unicode text.
* Contains characters that are the highest for some of the
* tested conversions, to make sure that the ucnvmbcs.c code that modifies the
* tables copies the entire tables.
*/
static const UChar text[]={
0x61, 0xd, 0x62, 0xa, 0x4e00, 0x3000, 0xfffd, 0xa, 0x20, 0x85, 0xff5e, 0x7a
};
UChar uNormal[32], uSwapped[32];
char normal[32], swapped[32];
const UChar *pcu;
UChar *pu;
char *pc;
int32_t i, normalLength, swappedLength;
UChar u;
char c;
const char *swappedName;
UConverter *cnv, *swapCnv;
UErrorCode errorCode;
/* if the swap flag is false, then the test encoding is not EBCDIC and must not swap */
/* open both the normal and the LF/NL-swapping converters */
strcpy(swapped, name);
strcat(swapped, UCNV_SWAP_LFNL_OPTION_STRING);
errorCode=U_ZERO_ERROR;
swapCnv=ucnv_open(swapped, &errorCode);
cnv=ucnv_open(name, &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("TestEBCDICSwapLFNL error: unable to open %s or %s (%s)\n", name, swapped, u_errorName(errorCode));
goto cleanup;
}
/* the name must contain the swap option if and only if we expect the converter to swap */
swappedName=ucnv_getName(swapCnv, &errorCode);
if(U_FAILURE(errorCode)) {
log_err("TestEBCDICSwapLFNL error: ucnv_getName(%s,swaplfnl) failed (%s)\n", name, u_errorName(errorCode));
goto cleanup;
}
pc=strstr(swappedName, UCNV_SWAP_LFNL_OPTION_STRING);
if(swap != (pc!=NULL)) {
log_err("TestEBCDICSwapLFNL error: ucnv_getName(%s,swaplfnl)=%s should (%d) contain 'swaplfnl'\n", name, swappedName, swap);
goto cleanup;
}
/* convert to EBCDIC */
pcu=text;
pc=normal;
ucnv_fromUnicode(cnv, &pc, normal+UPRV_LENGTHOF(normal), &pcu, text+UPRV_LENGTHOF(text), NULL, true, &errorCode);
normalLength=(int32_t)(pc-normal);
pcu=text;
pc=swapped;
ucnv_fromUnicode(swapCnv, &pc, swapped+UPRV_LENGTHOF(swapped), &pcu, text+UPRV_LENGTHOF(text), NULL, true, &errorCode);
swappedLength=(int32_t)(pc-swapped);
if(U_FAILURE(errorCode)) {
log_err("TestEBCDICSwapLFNL error converting to %s - (%s)\n", name, u_errorName(errorCode));
goto cleanup;
}
/* compare EBCDIC output */
if(normalLength!=swappedLength) {
log_err("TestEBCDICSwapLFNL error converting to %s - output lengths %d vs. %d\n", name, normalLength, swappedLength);
goto cleanup;
}
for(i=0; i<normalLength; ++i) {
/* swap EBCDIC LF/NL for comparison */
c=normal[i];
if(swap) {
if(c==0x15) {
c=0x25;
} else if(c==0x25) {
c=0x15;
}
}
if(c!=swapped[i]) {
log_err("TestEBCDICSwapLFNL error converting to %s - did not swap properly, output[%d]=0x%02x\n", name, i, (uint8_t)swapped[i]);
goto cleanup;
}
}
/* convert back to Unicode (may not roundtrip) */
pc=normal;
pu=uNormal;
ucnv_toUnicode(cnv, &pu, uNormal+UPRV_LENGTHOF(uNormal), (const char **)&pc, normal+normalLength, NULL, true, &errorCode);
normalLength=(int32_t)(pu-uNormal);
pc=normal;
pu=uSwapped;
ucnv_toUnicode(swapCnv, &pu, uSwapped+UPRV_LENGTHOF(uSwapped), (const char **)&pc, normal+swappedLength, NULL, true, &errorCode);
swappedLength=(int32_t)(pu-uSwapped);
if(U_FAILURE(errorCode)) {
log_err("TestEBCDICSwapLFNL error converting from %s - (%s)\n", name, u_errorName(errorCode));
goto cleanup;
}
/* compare EBCDIC output */
if(normalLength!=swappedLength) {
log_err("TestEBCDICSwapLFNL error converting from %s - output lengths %d vs. %d\n", name, normalLength, swappedLength);
goto cleanup;
}
for(i=0; i<normalLength; ++i) {
/* swap EBCDIC LF/NL for comparison */
u=uNormal[i];
if(swap) {
if(u==0xa) {
u=0x85;
} else if(u==0x85) {
u=0xa;
}
}
if(u!=uSwapped[i]) {
log_err("TestEBCDICSwapLFNL error converting from %s - did not swap properly, output[%d]=U+%04x\n", name, i, uSwapped[i]);
goto cleanup;
}
}
/* clean up */
cleanup:
ucnv_close(cnv);
ucnv_close(swapCnv);
}
static void
TestEBCDICSwapLFNL(void) {
static const struct {
const char *name;
UBool swap;
} tests[]={
{ "ibm-37", true },
{ "ibm-1047", true },
{ "ibm-1140", true },
{ "ibm-930", true },
{ "iso-8859-3", false }
};
int i;
for(i=0; i<UPRV_LENGTHOF(tests); ++i) {
testSwap(tests[i].name, tests[i].swap);
}
}
#else
static void
TestEBCDICSwapLFNL() {
/* test nothing... */
}
#endif
static void TestFromUCountPending(void){
#if !UCONFIG_NO_LEGACY_CONVERSION
UErrorCode status = U_ZERO_ERROR;
/* const UChar expectedUnicode[] = { 0x20ac, 0x0005, 0x0006, 0x000b, 0xdbc4, 0xde34, 0xd84d, 0xdc56, 0xfffd}; */
static const struct {
UChar input[6];
int32_t len;
int32_t exp;
}fromUnicodeTests[] = {
/*m:n conversion*/
{{0xdbc4},1,1},
{{ 0xdbc4, 0xde34, 0xd84d},3,1},
{{ 0xdbc4, 0xde34, 0xd900},3,3},
};
int i;
UConverter* cnv = ucnv_openPackage(loadTestData(&status), "test3", &status);
if(U_FAILURE(status)){
log_data_err("Could not create converter for test3. Error: %s\n", u_errorName(status));
return;
}
for(i=0; i<UPRV_LENGTHOF(fromUnicodeTests); ++i) {
char tgt[10];
char* target = tgt;
char* targetLimit = target + 10;
const UChar* source = fromUnicodeTests[i].input;
const UChar* sourceLimit = source + fromUnicodeTests[i].len;
int32_t len = 0;
ucnv_reset(cnv);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, false, &status);
len = ucnv_fromUCountPending(cnv, &status);
if(U_FAILURE(status)){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
continue;
}
if(len != fromUnicodeTests[i].exp){
log_err("Did not get the expected output for ucnv_fromUInputConsumed.\n");
}
}
status = U_ZERO_ERROR;
{
/*
* The converter has to read the tail before it knows that
* only head alone matches.
* At the end, the output for head will overflow the target,
* middle will be pending, and tail will not have been consumed.
*/
/*
\U00101234 -> x (<U101234> \x07 |0)
\U00101234\U00050005 -> y (<U101234>+<U50005> \x07+\x00+\x01\x02\x0e+\x05 |0)
\U00101234\U00050005\U00060006 -> z (<U101234>+<U50005>+<U60006> \x07+\x00+\x01\x02\x0f+\x09 |0)
\U00060007 -> unassigned
*/
static const UChar head[] = {0xDBC4,0xDE34,0xD900,0xDC05,0x0000};/* \U00101234\U00050005 */
static const UChar middle[] = {0xD940,0x0000}; /* first half of \U00060006 or \U00060007 */
static const UChar tail[] = {0xDC07,0x0000};/* second half of \U00060007 */
char tgt[10];
char* target = tgt;
char* targetLimit = target + 2; /* expect overflow from converting \U00101234\U00050005 */
const UChar* source = head;
const UChar* sourceLimit = source + u_strlen(head);
int32_t len = 0;
ucnv_reset(cnv);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, false, &status);
len = ucnv_fromUCountPending(cnv, &status);
if(U_FAILURE(status)){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
}
if(len!=4){
log_err("ucnv_fromUInputHeld did not return correct length for head\n");
}
source = middle;
sourceLimit = source + u_strlen(middle);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, false, &status);
len = ucnv_fromUCountPending(cnv, &status);
if(U_FAILURE(status)){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
}
if(len!=5){
log_err("ucnv_fromUInputHeld did not return correct length for middle\n");
}
source = tail;
sourceLimit = source + u_strlen(tail);
ucnv_fromUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, false, &status);
if(status != U_BUFFER_OVERFLOW_ERROR){
log_err("ucnv_fromUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
status = U_ZERO_ERROR;
len = ucnv_fromUCountPending(cnv, &status);
/* middle[1] is pending, tail has not been consumed */
if(U_FAILURE(status)){
log_err("ucnv_fromUInputHeld call did not succeed. Error: %s\n", u_errorName(status));
}
if(len!=1){
log_err("ucnv_fromUInputHeld did not return correct length for tail\n");
}
}
ucnv_close(cnv);
#endif
}
static void
TestToUCountPending(void){
#if !UCONFIG_NO_LEGACY_CONVERSION
UErrorCode status = U_ZERO_ERROR;
static const struct {
char input[6];
int32_t len;
int32_t exp;
}toUnicodeTests[] = {
/*m:n conversion*/
{{0x05, 0x01, 0x02},3,3},
{{0x01, 0x02},2,2},
{{0x07, 0x00, 0x01, 0x02},4,4},
};
int i;
UConverterToUCallback *oldToUAction= NULL;
UConverter* cnv = ucnv_openPackage(loadTestData(&status), "test3", &status);
if(U_FAILURE(status)){
log_data_err("Could not create converter for test3. Error: %s\n", u_errorName(status));
return;
}
ucnv_setToUCallBack(cnv, UCNV_TO_U_CALLBACK_STOP, NULL, oldToUAction, NULL, &status);
for(i=0; i<UPRV_LENGTHOF(toUnicodeTests); ++i) {
UChar tgt[20];
UChar* target = tgt;
UChar* targetLimit = target + 20;
const char* source = toUnicodeTests[i].input;
const char* sourceLimit = source + toUnicodeTests[i].len;
int32_t len = 0;
ucnv_reset(cnv);
ucnv_toUnicode(cnv, &target, targetLimit, &source, sourceLimit, NULL, false, &status);
len = ucnv_toUCountPending(cnv,&status);
if(U_FAILURE(status)){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
status = U_ZERO_ERROR;
continue;
}
if(len != toUnicodeTests[i].exp){
log_err("Did not get the expected output for ucnv_toUInputConsumed.\n");
}
}
status = U_ZERO_ERROR;
ucnv_close(cnv);
{
/*
* The converter has to read the tail before it knows that
* only head alone matches.
* At the end, the output for head will overflow the target,
* mid will be pending, and tail will not have been consumed.
*/
char head[] = { 0x01, 0x02, 0x03, 0x0a , 0x00};
char mid[] = { 0x01, 0x02, 0x03, 0x0b, 0x00 };
char tail[] = { 0x01, 0x02, 0x03, 0x0d, 0x00 };
/*
0x01, 0x02, 0x03, 0x0a -> x (<U23456> \x01\x02\x03\x0a |0)
0x01, 0x02, 0x03, 0x0b -> y (<U000b> \x01\x02\x03\x0b |0)
0x01, 0x02, 0x03, 0x0d -> z (<U34567> \x01\x02\x03\x0d |3)
0x01, 0x02, 0x03, 0x0a + 0x01, 0x02, 0x03, 0x0b + 0x01 + many more -> z (see test4 "many bytes, and bytes per UChar")
*/
UChar tgt[10];
UChar* target = tgt;
UChar* targetLimit = target + 1; /* expect overflow from converting */
const char* source = head;
const char* sourceLimit = source + strlen(head);
int32_t len = 0;
cnv = ucnv_openPackage(loadTestData(&status), "test4", &status);
if(U_FAILURE(status)){
log_err("Could not create converter for test3. Error: %s\n", u_errorName(status));
return;
}
ucnv_setToUCallBack(cnv, UCNV_TO_U_CALLBACK_STOP, NULL, oldToUAction, NULL, &status);
ucnv_toUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, false, &status);
len = ucnv_toUCountPending(cnv,&status);
if(U_FAILURE(status)){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
if(len != 4){
log_err("Did not get the expected len for head.\n");
}
source=mid;
sourceLimit = source+strlen(mid);
ucnv_toUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, false, &status);
len = ucnv_toUCountPending(cnv,&status);
if(U_FAILURE(status)){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
if(len != 8){
log_err("Did not get the expected len for mid.\n");
}
source=tail;
sourceLimit = source+strlen(tail);
targetLimit = target;
ucnv_toUnicode(cnv,&target, targetLimit, &source, sourceLimit, NULL, false, &status);
if(status != U_BUFFER_OVERFLOW_ERROR){
log_err("ucnv_toUnicode call did not succeed. Error: %s\n", u_errorName(status));
}
status = U_ZERO_ERROR;
len = ucnv_toUCountPending(cnv,&status);
/* mid[4] is pending, tail has not been consumed */
if(U_FAILURE(status)){
log_err("ucnv_toUCountPending call did not succeed. Error: %s\n", u_errorName(status));
}
if(len != 4){
log_err("Did not get the expected len for tail.\n");
}
ucnv_close(cnv);
}
#endif
}
static void TestOneDefaultNameChange(const char *name, const char *expected) {
UErrorCode status = U_ZERO_ERROR;
UConverter *cnv;
ucnv_setDefaultName(name);
if(strcmp(ucnv_getDefaultName(), expected)==0)
log_verbose("setDefaultName of %s works.\n", name);
else
log_err("setDefaultName of %s failed\n", name);
cnv=ucnv_open(NULL, &status);
if (U_FAILURE(status) || cnv == NULL) {
log_err("opening the default converter of %s failed\n", name);
return;
}
if(strcmp(ucnv_getName(cnv, &status), expected)==0)
log_verbose("ucnv_getName of %s works.\n", name);
else
log_err("ucnv_getName of %s failed\n", name);
ucnv_close(cnv);
}
static void TestDefaultName(void) {
/*Testing ucnv_getDefaultName() and ucnv_setDefaultNAme()*/
static char defaultName[UCNV_MAX_CONVERTER_NAME_LENGTH + 1];
strcpy(defaultName, ucnv_getDefaultName());
log_verbose("getDefaultName returned %s\n", defaultName);
/*change the default name by setting it */
TestOneDefaultNameChange("UTF-8", "UTF-8");
#if U_CHARSET_IS_UTF8
TestOneDefaultNameChange("ISCII,version=1", "UTF-8");
TestOneDefaultNameChange("ISCII,version=2", "UTF-8");
TestOneDefaultNameChange("ISO-8859-1", "UTF-8");
#else
# if !UCONFIG_NO_LEGACY_CONVERSION && !UCONFIG_ONLY_HTML_CONVERSION
TestOneDefaultNameChange("ISCII,version=1", "ISCII,version=1");
TestOneDefaultNameChange("ISCII,version=2", "ISCII,version=2");
# endif
TestOneDefaultNameChange("ISO-8859-1", "ISO-8859-1");
#endif
/*set the default name back*/
ucnv_setDefaultName(defaultName);
}
/* Test that ucnv_compareNames() matches names according to spec. ----------- */
static int
sign(int n) {
if(n==0) {
return 0;
} else if(n<0) {
return -1;
} else /* n>0 */ {
return 1;
}
}
static void
compareNames(const char **names) {
const char *relation, *name1, *name2;
int rel, result;
relation=*names++;
if(*relation=='=') {
rel = 0;
} else if(*relation=='<') {
rel = -1;
} else {
rel = 1;
}
name1=*names++;
if(name1==NULL) {
return;
}
while((name2=*names++)!=NULL) {
result=ucnv_compareNames(name1, name2);
if(sign(result)!=rel) {
log_err("ucnv_compareNames(\"%s\", \"%s\")=%d, sign!=%d\n", name1, name2, result, rel);
}
name1=name2;
}
}
static void
TestCompareNames(void) {
static const char *equalUTF8[]={ "=", "UTF-8", "utf_8", "u*T@f08", "Utf 8", NULL };
static const char *equalIBM[]={ "=", "ibm-37", "IBM037", "i-B-m 00037", "ibm-0037", "IBM00037", NULL };
static const char *lessMac[]={ "<", "macos-0_1-10.2", "macos-1-10.0.2", "macos-1-10.2", NULL };
static const char *lessUTF080[]={ "<", "UTF-0008", "utf$080", "u*T@f0800", "Utf 0000000009", NULL };
compareNames(equalUTF8);
compareNames(equalIBM);
compareNames(lessMac);
compareNames(lessUTF080);
}
static void
TestSubstString(void) {
static const UChar surrogate[1]={ 0xd900 };
char buffer[16];
static const UChar sub[5]={ 0x61, 0x62, 0x63, 0x64, 0x65 };
static const char subChars[5]={ 0x61, 0x62, 0x63, 0x64, 0x65 };
UConverter *cnv;
UErrorCode errorCode;
int32_t length;
int8_t len8;
/* UTF-16/32: test that the BOM is output before the sub character */
errorCode=U_ZERO_ERROR;
cnv=ucnv_open("UTF-16", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("ucnv_open(UTF-16) failed - %s\n", u_errorName(errorCode));
return;
}
length=ucnv_fromUChars(cnv, buffer, (int32_t)sizeof(buffer), surrogate, 1, &errorCode);
ucnv_close(cnv);
if(U_FAILURE(errorCode) ||
length!=4 ||
NULL == ucnv_detectUnicodeSignature(buffer, length, NULL, &errorCode)
) {
log_err("ucnv_fromUChars(UTF-16, U+D900) did not write a BOM\n");
}
errorCode=U_ZERO_ERROR;
cnv=ucnv_open("UTF-32", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("ucnv_open(UTF-32) failed - %s\n", u_errorName(errorCode));
return;
}
length=ucnv_fromUChars(cnv, buffer, (int32_t)sizeof(buffer), surrogate, 1, &errorCode);
ucnv_close(cnv);
if(U_FAILURE(errorCode) ||
length!=8 ||
NULL == ucnv_detectUnicodeSignature(buffer, length, NULL, &errorCode)
) {
log_err("ucnv_fromUChars(UTF-32, U+D900) did not write a BOM\n");
}
/* Simple API test of ucnv_setSubstString() + ucnv_getSubstChars(). */
errorCode=U_ZERO_ERROR;
cnv=ucnv_open("ISO-8859-1", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("ucnv_open(ISO-8859-1) failed - %s\n", u_errorName(errorCode));
return;
}
ucnv_setSubstString(cnv, sub, UPRV_LENGTHOF(sub), &errorCode);
if(U_FAILURE(errorCode)) {
log_err("ucnv_setSubstString(ISO-8859-1, sub[5]) failed - %s\n", u_errorName(errorCode));
} else {
len8 = sizeof(buffer);
ucnv_getSubstChars(cnv, buffer, &len8, &errorCode);
/* Stateless converter, we expect the string converted to charset bytes. */
if(U_FAILURE(errorCode) || len8!=sizeof(subChars) || 0!=uprv_memcmp(buffer, subChars, len8)) {
log_err("ucnv_getSubstChars(ucnv_setSubstString(ISO-8859-1, sub[5])) failed - %s\n", u_errorName(errorCode));
}
}
ucnv_close(cnv);
#if !UCONFIG_NO_LEGACY_CONVERSION
errorCode=U_ZERO_ERROR;
cnv=ucnv_open("HZ", &errorCode);
if(U_FAILURE(errorCode)) {
log_data_err("ucnv_open(HZ) failed - %s\n", u_errorName(errorCode));
return;
}
ucnv_setSubstString(cnv, sub, UPRV_LENGTHOF(sub), &errorCode);
if(U_FAILURE(errorCode)) {
log_err("ucnv_setSubstString(HZ, sub[5]) failed - %s\n", u_errorName(errorCode));
} else {
len8 = sizeof(buffer);
ucnv_getSubstChars(cnv, buffer, &len8, &errorCode);
/* Stateful converter, we expect that the Unicode string was set and that we get an empty char * string now. */
if(U_FAILURE(errorCode) || len8!=0) {
log_err("ucnv_getSubstChars(ucnv_setSubstString(HZ, sub[5])) failed - %s\n", u_errorName(errorCode));
}
}
ucnv_close(cnv);
#endif
/*
* Further testing of ucnv_setSubstString() is done via intltest convert.
* We do not test edge cases of illegal arguments and similar because the
* function implementation uses all of its parameters in calls to other
* functions with UErrorCode parameters.
*/
}
static void
InvalidArguments(void) {
UConverter *cnv;
UErrorCode errorCode;
char charBuffer[2] = {1, 1};
char ucharAsCharBuffer[2] = {2, 2};
char *charsPtr = charBuffer;
UChar *ucharsPtr = (UChar *)ucharAsCharBuffer;
UChar *ucharsBadPtr = (UChar *)(ucharAsCharBuffer + 1);
errorCode=U_ZERO_ERROR;
cnv=ucnv_open("UTF-8", &errorCode);
if(U_FAILURE(errorCode)) {
log_err("ucnv_open() failed - %s\n", u_errorName(errorCode));
return;
}
errorCode=U_ZERO_ERROR;
/* This one should fail because an incomplete UChar is being passed in */
ucnv_fromUnicode(cnv, &charsPtr, charsPtr, (const UChar **)&ucharsPtr, ucharsBadPtr, NULL, true, &errorCode);
if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_fromUnicode() failed to return U_ILLEGAL_ARGUMENT_ERROR for incomplete UChar * buffer - %s\n", u_errorName(errorCode));
}
errorCode=U_ZERO_ERROR;
/* This one should fail because ucharsBadPtr is > than ucharsPtr */
ucnv_fromUnicode(cnv, &charsPtr, charsPtr, (const UChar **)&ucharsBadPtr, ucharsPtr, NULL, true, &errorCode);
if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_fromUnicode() failed to return U_ILLEGAL_ARGUMENT_ERROR for bad limit pointer - %s\n", u_errorName(errorCode));
}
errorCode=U_ZERO_ERROR;
/* This one should fail because an incomplete UChar is being passed in */
ucnv_toUnicode(cnv, &ucharsPtr, ucharsBadPtr, (const char **)&charsPtr, charsPtr, NULL, true, &errorCode);
if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_toUnicode() failed to return U_ILLEGAL_ARGUMENT_ERROR for incomplete UChar * buffer - %s\n", u_errorName(errorCode));
}
errorCode=U_ZERO_ERROR;
/* This one should fail because ucharsBadPtr is > than ucharsPtr */
ucnv_toUnicode(cnv, &ucharsBadPtr, ucharsPtr, (const char **)&charsPtr, charsPtr, NULL, true, &errorCode);
if(errorCode != U_ILLEGAL_ARGUMENT_ERROR) {
log_err("ucnv_toUnicode() failed to return U_ILLEGAL_ARGUMENT_ERROR for bad limit pointer - %s\n", u_errorName(errorCode));
}
if (charBuffer[0] != 1 || charBuffer[1] != 1
|| ucharAsCharBuffer[0] != 2 || ucharAsCharBuffer[1] != 2)
{
log_err("Data was incorrectly written to buffers\n");
}
ucnv_close(cnv);
}
static void TestGetName(void) {
static const char *const names[] = {
"Unicode", "UTF-16",
"UnicodeBigUnmarked", "UTF-16BE",
"UnicodeBig", "UTF-16BE,version=1",
"UnicodeLittleUnmarked", "UTF-16LE",
"UnicodeLittle", "UTF-16LE,version=1",
"x-UTF-16LE-BOM", "UTF-16LE,version=1"
};
int32_t i;
for(i = 0; i < UPRV_LENGTHOF(names); i += 2) {
UErrorCode errorCode = U_ZERO_ERROR;
UConverter *cnv = ucnv_open(names[i], &errorCode);
if(U_SUCCESS(errorCode)) {
const char *name = ucnv_getName(cnv, &errorCode);
if(U_FAILURE(errorCode) || 0 != strcmp(name, names[i+1])) {
log_err("ucnv_getName(%s) = %s != %s -- %s\n",
names[i], name, names[i+1], u_errorName(errorCode));
}
ucnv_close(cnv);
}
}
}
static void TestUTFBOM(void) {
static const UChar a16[] = { 0x61 };
static const char *const names[] = {
"UTF-16",
"UTF-16,version=1",
"UTF-16BE",
"UnicodeBig",
"UTF-16LE",
"UnicodeLittle"
};
static const uint8_t expected[][5] = {
#if U_IS_BIG_ENDIAN
{ 4, 0xfe, 0xff, 0, 0x61 },
{ 4, 0xfe, 0xff, 0, 0x61 },
#else
{ 4, 0xff, 0xfe, 0x61, 0 },
{ 4, 0xff, 0xfe, 0x61, 0 },
#endif
{ 2, 0, 0x61 },
{ 4, 0xfe, 0xff, 0, 0x61 },
{ 2, 0x61, 0 },
{ 4, 0xff, 0xfe, 0x61, 0 }
};
char bytes[10];
int32_t i;
for(i = 0; i < UPRV_LENGTHOF(names); ++i) {
UErrorCode errorCode = U_ZERO_ERROR;
UConverter *cnv = ucnv_open(names[i], &errorCode);
int32_t length = 0;
const uint8_t *exp = expected[i];
if (U_FAILURE(errorCode)) {
log_err_status(errorCode, "Unable to open converter: %s got error code: %s\n", names[i], u_errorName(errorCode));
continue;
}
length = ucnv_fromUChars(cnv, bytes, (int32_t)sizeof(bytes), a16, 1, &errorCode);
if(U_FAILURE(errorCode) || length != exp[0] || 0 != memcmp(bytes, exp+1, length)) {
log_err("unexpected %s BOM writing behavior -- %s\n",
names[i], u_errorName(errorCode));
}
ucnv_close(cnv);
}
}
|