/usr/share/doc/glibc-doc/html/libc_6.html is in glibc-doc 2.15-0ubuntu10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This file documents the GNU C library.
This is Edition 0.13, last updated 2011-07-19,
of The GNU C Library Reference Manual, for version
2.14 (Ubuntu EGLIBC 2.15-0ubuntu10) .
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software Needs Free Documentation"
and "GNU Lesser General Public License", the Front-Cover texts being
"A GNU Manual", and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: "You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom."
-->
<!-- Created on April 20, 2012 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>The GNU C Library: 6. Character Set Handling</title>
<meta name="description" content="The GNU C Library: 6. Character Set Handling">
<meta name="keywords" content="The GNU C Library: 6. Character Set Handling">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Character-Set-Handling"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="libc_5.html#Envz-Functions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Extended-Char-Intro" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc_5.html#String-and-Array-Utilities" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Character-Set-Handling-1"></a>
<h1 class="chapter">6. Character Set Handling</h1>
<p>Character sets used in the early days of computing had only six, seven,
or eight bits for each character: there was never a case where more than
eight bits (one byte) were used to represent a single character. The
limitations of this approach became more apparent as more people
grappled with non-Roman character sets, where not all the characters
that make up a language’s character set can be represented by <em>2^8</em>
choices. This chapter shows the functionality that was added to the C
library to support multiple character sets.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Extended-Char-Intro">6.1 Introduction to Extended Characters</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Charset-Function-Overview">6.2 Overview about Character Handling Functions</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Restartable-multibyte-conversion">6.3 Restartable Multibyte Conversion Functions</a></td><td> </td><td align="left" valign="top"> Restartable multibyte conversion
Functions.
</td></tr>
<tr><td align="left" valign="top"><a href="#Non_002dreentrant-Conversion">6.4 Non-reentrant Conversion Function</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Generic-Charset-Conversion">6.5 Generic Charset Conversion</a></td><td> </td><td align="left" valign="top"></td></tr>
</table>
<hr size="6">
<a name="Extended-Char-Intro"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Charset-Function-Overview" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Introduction-to-Extended-Characters"></a>
<h2 class="section">6.1 Introduction to Extended Characters</h2>
<p>A variety of solutions is available to overcome the differences between
character sets with a 1:1 relation between bytes and characters and
character sets with ratios of 2:1 or 4:1. The remainder of this
section gives a few examples to help understand the design decisions
made while developing the functionality of the C library.
</p>
<a name="index-internal-representation"></a>
<p>A distinction we have to make right away is between internal and
external representation. <em>Internal representation</em> means the
representation used by a program while keeping the text in memory.
External representations are used when text is stored or transmitted
through some communication channel. Examples of external
representations include files waiting in a directory to be
read and parsed.
</p>
<p>Traditionally there has been no difference between the two representations.
It was equally comfortable and useful to use the same single-byte
representation internally and externally. This comfort level decreases
with more and larger character sets.
</p>
<p>One of the problems to overcome with the internal representation is
handling text that is externally encoded using different character
sets. Assume a program that reads two texts and compares them using
some metric. The comparison can be usefully done only if the texts are
internally kept in a common format.
</p>
<a name="index-wide-character"></a>
<p>For such a common format (<em>=</em> character set) eight bits are certainly
no longer enough. So the smallest entity will have to grow: <em>wide
characters</em> will now be used. Instead of one byte per character, two or
four will be used instead. (Three are not good to address in memory and
more than four bytes seem not to be necessary).
</p>
<a name="index-Unicode"></a>
<a name="index-ISO-10646"></a>
<p>As shown in some other part of this manual,
a completely new family has been created of functions that can handle wide
character texts in memory. The most commonly used character sets for such
internal wide character representations are Unicode and ISO 10646
(also known as UCS for Universal Character Set). Unicode was originally
planned as a 16-bit character set; whereas, ISO 10646 was designed to
be a 31-bit large code space. The two standards are practically identical.
They have the same character repertoire and code table, but Unicode specifies
added semantics. At the moment, only characters in the first <code>0x10000</code>
code positions (the so-called Basic Multilingual Plane, BMP) have been
assigned, but the assignment of more specialized characters outside this
16-bit space is already in progress. A number of encodings have been
defined for Unicode and ISO 10646 characters:
<a name="index-UCS_002d2"></a>
<a name="index-UCS_002d4"></a>
<a name="index-UTF_002d8"></a>
<a name="index-UTF_002d16"></a>
UCS-2 is a 16-bit word that can only represent characters
from the BMP, UCS-4 is a 32-bit word than can represent any Unicode
and ISO 10646 character, UTF-8 is an ASCII compatible encoding where
ASCII characters are represented by ASCII bytes and non-ASCII characters
by sequences of 2-6 non-ASCII bytes, and finally UTF-16 is an extension
of UCS-2 in which pairs of certain UCS-2 words can be used to encode
non-BMP characters up to <code>0x10ffff</code>.
</p>
<p>To represent wide characters the <code>char</code> type is not suitable. For
this reason the ISO C standard introduces a new type that is
designed to keep one character of a wide character string. To maintain
the similarity there is also a type corresponding to <code>int</code> for
those functions that take a single wide character.
</p>
<dl>
<dt><a name="index-wchar_005ft"></a><u>Data type:</u> <b>wchar_t</b></dt>
<dd><p>This data type is used as the base type for wide character strings.
In other words, arrays of objects of this type are the equivalent of
<code>char[]</code> for multibyte character strings. The type is defined in
‘<tt>stddef.h</tt>’.
</p>
<p>The ISO C90 standard, where <code>wchar_t</code> was introduced, does not
say anything specific about the representation. It only requires that
this type is capable of storing all elements of the basic character set.
Therefore it would be legitimate to define <code>wchar_t</code> as <code>char</code>,
which might make sense for embedded systems.
</p>
<p>But for GNU systems <code>wchar_t</code> is always 32 bits wide and, therefore,
capable of representing all UCS-4 values and, therefore, covering all of
ISO 10646. Some Unix systems define <code>wchar_t</code> as a 16-bit type
and thereby follow Unicode very strictly. This definition is perfectly
fine with the standard, but it also means that to represent all
characters from Unicode and ISO 10646 one has to use UTF-16 surrogate
characters, which is in fact a multi-wide-character encoding. But
resorting to multi-wide-character encoding contradicts the purpose of the
<code>wchar_t</code> type.
</p></dd></dl>
<dl>
<dt><a name="index-wint_005ft-1"></a><u>Data type:</u> <b>wint_t</b></dt>
<dd><p><code>wint_t</code> is a data type used for parameters and variables that
contain a single wide character. As the name suggests this type is the
equivalent of <code>int</code> when using the normal <code>char</code> strings. The
types <code>wchar_t</code> and <code>wint_t</code> often have the same
representation if their size is 32 bits wide but if <code>wchar_t</code> is
defined as <code>char</code> the type <code>wint_t</code> must be defined as
<code>int</code> due to the parameter promotion.
</p>
<a name="index-wchar_002eh-2"></a>
<p>This type is defined in ‘<tt>wchar.h</tt>’ and was introduced in
Amendment 1 to ISO C90.
</p></dd></dl>
<p>As there are for the <code>char</code> data type macros are available for
specifying the minimum and maximum value representable in an object of
type <code>wchar_t</code>.
</p>
<dl>
<dt><a name="index-WCHAR_005fMIN"></a><u>Macro:</u> wint_t <b>WCHAR_MIN</b></dt>
<dd><p>The macro <code>WCHAR_MIN</code> evaluates to the minimum value representable
by an object of type <code>wint_t</code>.
</p>
<p>This macro was introduced in Amendment 1 to ISO C90.
</p></dd></dl>
<dl>
<dt><a name="index-WCHAR_005fMAX"></a><u>Macro:</u> wint_t <b>WCHAR_MAX</b></dt>
<dd><p>The macro <code>WCHAR_MAX</code> evaluates to the maximum value representable
by an object of type <code>wint_t</code>.
</p>
<p>This macro was introduced in Amendment 1 to ISO C90.
</p></dd></dl>
<p>Another special wide character value is the equivalent to <code>EOF</code>.
</p>
<dl>
<dt><a name="index-WEOF"></a><u>Macro:</u> wint_t <b>WEOF</b></dt>
<dd><p>The macro <code>WEOF</code> evaluates to a constant expression of type
<code>wint_t</code> whose value is different from any member of the extended
character set.
</p>
<p><code>WEOF</code> need not be the same value as <code>EOF</code> and unlike
<code>EOF</code> it also need <em>not</em> be negative. In other words, sloppy
code like
</p>
<table><tr><td> </td><td><pre class="smallexample">{
int c;
…
while ((c = getc (fp)) < 0)
…
}
</pre></td></tr></table>
<p>has to be rewritten to use <code>WEOF</code> explicitly when wide characters
are used:
</p>
<table><tr><td> </td><td><pre class="smallexample">{
wint_t c;
…
while ((c = wgetc (fp)) != WEOF)
…
}
</pre></td></tr></table>
<a name="index-wchar_002eh-3"></a>
<p>This macro was introduced in Amendment 1 to ISO C90 and is
defined in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>These internal representations present problems when it comes to storing
and transmittal. Because each single wide character consists of more
than one byte, they are effected by byte-ordering. Thus, machines with
different endianesses would see different values when accessing the same
data. This byte ordering concern also applies for communication protocols
that are all byte-based and therefore require that the sender has to
decide about splitting the wide character in bytes. A last (but not least
important) point is that wide characters often require more storage space
than a customized byte-oriented character set.
</p>
<a name="index-multibyte-character"></a>
<a name="index-EBCDIC"></a>
<p>For all the above reasons, an external encoding that is different from
the internal encoding is often used if the latter is UCS-2 or UCS-4.
The external encoding is byte-based and can be chosen appropriately for
the environment and for the texts to be handled. A variety of different
character sets can be used for this external encoding (information that
will not be exhaustively presented here–instead, a description of the
major groups will suffice). All of the ASCII-based character sets
fulfill one requirement: they are "filesystem safe." This means that
the character <code>'/'</code> is used in the encoding <em>only</em> to
represent itself. Things are a bit different for character sets like
EBCDIC (Extended Binary Coded Decimal Interchange Code, a character set
family used by IBM), but if the operation system does not understand
EBCDIC directly the parameters-to-system calls have to be converted
first anyhow.
</p>
<ul>
<li>
The simplest character sets are single-byte character sets. There can
be only up to 256 characters (for 8 bit character sets), which is
not sufficient to cover all languages but might be sufficient to handle
a specific text. Handling of a 8 bit character sets is simple. This
is not true for other kinds presented later, and therefore, the
application one uses might require the use of 8 bit character sets.
<a name="index-ISO-2022"></a>
</li><li>
The ISO 2022 standard defines a mechanism for extended character
sets where one character <em>can</em> be represented by more than one
byte. This is achieved by associating a state with the text.
Characters that can be used to change the state can be embedded in the
text. Each byte in the text might have a different interpretation in each
state. The state might even influence whether a given byte stands for a
character on its own or whether it has to be combined with some more
bytes.
<a name="index-EUC"></a>
<a name="index-Shift_005fJIS"></a>
<a name="index-SJIS"></a>
<p>In most uses of ISO 2022 the defined character sets do not allow
state changes that cover more than the next character. This has the
big advantage that whenever one can identify the beginning of the byte
sequence of a character one can interpret a text correctly. Examples of
character sets using this policy are the various EUC character sets
(used by Sun’s operations systems, EUC-JP, EUC-KR, EUC-TW, and EUC-CN)
or Shift_JIS (SJIS, a Japanese encoding).
</p>
<p>But there are also character sets using a state that is valid for more
than one character and has to be changed by another byte sequence.
Examples for this are ISO-2022-JP, ISO-2022-KR, and ISO-2022-CN.
</p>
</li><li>
<a name="index-ISO-6937"></a>
Early attempts to fix 8 bit character sets for other languages using the
Roman alphabet lead to character sets like ISO 6937. Here bytes
representing characters like the acute accent do not produce output
themselves: one has to combine them with other characters to get the
desired result. For example, the byte sequence <code>0xc2 0x61</code>
(non-spacing acute accent, followed by lower-case ‘a’) to get the “small
a with acute” character. To get the acute accent character on its own,
one has to write <code>0xc2 0x20</code> (the non-spacing acute followed by a
space).
<p>Character sets like ISO 6937 are used in some embedded systems such
as teletex.
</p>
</li><li>
<a name="index-UTF_002d8-1"></a>
Instead of converting the Unicode or ISO 10646 text used internally,
it is often also sufficient to simply use an encoding different than
UCS-2/UCS-4. The Unicode and ISO 10646 standards even specify such an
encoding: UTF-8. This encoding is able to represent all of ISO
10646 31 bits in a byte string of length one to six.
<a name="index-UTF_002d7"></a>
<p>There were a few other attempts to encode ISO 10646 such as UTF-7,
but UTF-8 is today the only encoding that should be used. In fact, with
any luck UTF-8 will soon be the only external encoding that has to be
supported. It proves to be universally usable and its only disadvantage
is that it favors Roman languages by making the byte string
representation of other scripts (Cyrillic, Greek, Asian scripts) longer
than necessary if using a specific character set for these scripts.
Methods like the Unicode compression scheme can alleviate these
problems.
</p></li></ul>
<p>The question remaining is: how to select the character set or encoding
to use. The answer: you cannot decide about it yourself, it is decided
by the developers of the system or the majority of the users. Since the
goal is interoperability one has to use whatever the other people one
works with use. If there are no constraints, the selection is based on
the requirements the expected circle of users will have. In other words,
if a project is expected to be used in only, say, Russia it is fine to use
KOI8-R or a similar character set. But if at the same time people from,
say, Greece are participating one should use a character set that allows
all people to collaborate.
</p>
<p>The most widely useful solution seems to be: go with the most general
character set, namely ISO 10646. Use UTF-8 as the external encoding
and problems about users not being able to use their own language
adequately are a thing of the past.
</p>
<p>One final comment about the choice of the wide character representation
is necessary at this point. We have said above that the natural choice
is using Unicode or ISO 10646. This is not required, but at least
encouraged, by the ISO C standard. The standard defines at least a
macro <code>__STDC_ISO_10646__</code> that is only defined on systems where
the <code>wchar_t</code> type encodes ISO 10646 characters. If this
symbol is not defined one should avoid making assumptions about the wide
character representation. If the programmer uses only the functions
provided by the C library to handle wide character strings there should
be no compatibility problems with other systems.
</p>
<hr size="6">
<a name="Charset-Function-Overview"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Extended-Char-Intro" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Restartable-multibyte-conversion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Overview-about-Character-Handling-Functions"></a>
<h2 class="section">6.2 Overview about Character Handling Functions</h2>
<p>A Unix C library contains three different sets of functions in two
families to handle character set conversion. One of the function families
(the most commonly used) is specified in the ISO C90 standard and,
therefore, is portable even beyond the Unix world. Unfortunately this
family is the least useful one. These functions should be avoided
whenever possible, especially when developing libraries (as opposed to
applications).
</p>
<p>The second family of functions got introduced in the early Unix standards
(XPG2) and is still part of the latest and greatest Unix standard:
Unix 98. It is also the most powerful and useful set of functions.
But we will start with the functions defined in Amendment 1 to
ISO C90.
</p>
<hr size="6">
<a name="Restartable-multibyte-conversion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Charset-Function-Overview" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Selecting-the-Conversion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Restartable-Multibyte-Conversion-Functions"></a>
<h2 class="section">6.3 Restartable Multibyte Conversion Functions</h2>
<p>The ISO C standard defines functions to convert strings from a
multibyte representation to wide character strings. There are a number
of peculiarities:
</p>
<ul>
<li>
The character set assumed for the multibyte encoding is not specified
as an argument to the functions. Instead the character set specified by
the <code>LC_CTYPE</code> category of the current locale is used; see
<a href="libc_7.html#Locale-Categories">Categories of Activities that Locales Affect</a>.
</li><li>
The functions handling more than one character at a time require NUL
terminated strings as the argument (i.e., converting blocks of text
does not work unless one can add a NUL byte at an appropriate place).
The GNU C library contains some extensions to the standard that allow
specifying a size, but basically they also expect terminated strings.
</li></ul>
<p>Despite these limitations the ISO C functions can be used in many
contexts. In graphical user interfaces, for instance, it is not
uncommon to have functions that require text to be displayed in a wide
character string if the text is not simple ASCII. The text itself might
come from a file with translations and the user should decide about the
current locale, which determines the translation and therefore also the
external encoding used. In such a situation (and many others) the
functions described here are perfect. If more freedom while performing
the conversion is necessary take a look at the <code>iconv</code> functions
(see section <a href="#Generic-Charset-Conversion">Generic Charset Conversion</a>).
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Selecting-the-Conversion">6.3.1 Selecting the conversion and its properties</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Keeping-the-state">6.3.2 Representing the state of the conversion</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Converting-a-Character">6.3.3 Converting Single Characters</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Converting-Strings">6.3.4 Converting Multibyte and Wide Character Strings</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Multibyte-Conversion-Example">6.3.5 A Complete Multibyte Conversion Example</a></td><td> </td><td align="left" valign="top"></td></tr>
</table>
<hr size="6">
<a name="Selecting-the-Conversion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Restartable-multibyte-conversion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Keeping-the-state" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Restartable-multibyte-conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Selecting-the-conversion-and-its-properties"></a>
<h3 class="subsection">6.3.1 Selecting the conversion and its properties</h3>
<p>We already said above that the currently selected locale for the
<code>LC_CTYPE</code> category decides about the conversion that is performed
by the functions we are about to describe. Each locale uses its own
character set (given as an argument to <code>localedef</code>) and this is the
one assumed as the external multibyte encoding. The wide character
set is always UCS-4, at least on GNU systems.
</p>
<p>A characteristic of each multibyte character set is the maximum number
of bytes that can be necessary to represent one character. This
information is quite important when writing code that uses the
conversion functions (as shown in the examples below).
The ISO C standard defines two macros that provide this information.
</p>
<dl>
<dt><a name="index-MB_005fLEN_005fMAX"></a><u>Macro:</u> int <b>MB_LEN_MAX</b></dt>
<dd><p><code>MB_LEN_MAX</code> specifies the maximum number of bytes in the multibyte
sequence for a single character in any of the supported locales. It is
a compile-time constant and is defined in ‘<tt>limits.h</tt>’.
<a name="index-limits_002eh-1"></a>
</p></dd></dl>
<dl>
<dt><a name="index-MB_005fCUR_005fMAX"></a><u>Macro:</u> int <b>MB_CUR_MAX</b></dt>
<dd><p><code>MB_CUR_MAX</code> expands into a positive integer expression that is the
maximum number of bytes in a multibyte character in the current locale.
The value is never greater than <code>MB_LEN_MAX</code>. Unlike
<code>MB_LEN_MAX</code> this macro need not be a compile-time constant, and in
the GNU C library it is not.
</p>
<a name="index-stdlib_002eh-6"></a>
<p><code>MB_CUR_MAX</code> is defined in ‘<tt>stdlib.h</tt>’.
</p></dd></dl>
<p>Two different macros are necessary since strictly ISO C90 compilers
do not allow variable length array definitions, but still it is desirable
to avoid dynamic allocation. This incomplete piece of code shows the
problem:
</p>
<table><tr><td> </td><td><pre class="smallexample">{
char buf[MB_LEN_MAX];
ssize_t len = 0;
while (! feof (fp))
{
fread (&buf[len], 1, MB_CUR_MAX - len, fp);
/* <span class="roman">… process</span> buf */
len -= used;
}
}
</pre></td></tr></table>
<p>The code in the inner loop is expected to have always enough bytes in
the array <var>buf</var> to convert one multibyte character. The array
<var>buf</var> has to be sized statically since many compilers do not allow a
variable size. The <code>fread</code> call makes sure that <code>MB_CUR_MAX</code>
bytes are always available in <var>buf</var>. Note that it isn’t
a problem if <code>MB_CUR_MAX</code> is not a compile-time constant.
</p>
<hr size="6">
<a name="Keeping-the-state"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Selecting-the-Conversion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Converting-a-Character" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Restartable-multibyte-conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Representing-the-state-of-the-conversion"></a>
<h3 class="subsection">6.3.2 Representing the state of the conversion</h3>
<a name="index-stateful"></a>
<p>In the introduction of this chapter it was said that certain character
sets use a <em>stateful</em> encoding. That is, the encoded values depend
in some way on the previous bytes in the text.
</p>
<p>Since the conversion functions allow converting a text in more than one
step we must have a way to pass this information from one call of the
functions to another.
</p>
<dl>
<dt><a name="index-mbstate_005ft"></a><u>Data type:</u> <b>mbstate_t</b></dt>
<dd><a name="index-shift-state"></a>
<p>A variable of type <code>mbstate_t</code> can contain all the information
about the <em>shift state</em> needed from one call to a conversion
function to another.
</p>
<a name="index-wchar_002eh-4"></a>
<p><code>mbstate_t</code> is defined in ‘<tt>wchar.h</tt>’. It was introduced in
Amendment 1 to ISO C90.
</p></dd></dl>
<p>To use objects of type <code>mbstate_t</code> the programmer has to define such
objects (normally as local variables on the stack) and pass a pointer to
the object to the conversion functions. This way the conversion function
can update the object if the current multibyte character set is stateful.
</p>
<p>There is no specific function or initializer to put the state object in
any specific state. The rules are that the object should always
represent the initial state before the first use, and this is achieved by
clearing the whole variable with code such as follows:
</p>
<table><tr><td> </td><td><pre class="smallexample">{
mbstate_t state;
memset (&state, '\0', sizeof (state));
/* <span class="roman">from now on <var>state</var> can be used.</span> */
…
}
</pre></td></tr></table>
<p>When using the conversion functions to generate output it is often
necessary to test whether the current state corresponds to the initial
state. This is necessary, for example, to decide whether to emit
escape sequences to set the state to the initial state at certain
sequence points. Communication protocols often require this.
</p>
<dl>
<dt><a name="index-mbsinit"></a><u>Function:</u> int <b>mbsinit</b><i> (const mbstate_t *<var>ps</var>)</i></dt>
<dd><p>The <code>mbsinit</code> function determines whether the state object pointed
to by <var>ps</var> is in the initial state. If <var>ps</var> is a null pointer or
the object is in the initial state the return value is nonzero. Otherwise
it is zero.
</p>
<a name="index-wchar_002eh-5"></a>
<p><code>mbsinit</code> was introduced in Amendment 1 to ISO C90 and is
declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>Code using <code>mbsinit</code> often looks similar to this:
</p>
<table><tr><td> </td><td><pre class="smallexample">{
mbstate_t state;
memset (&state, '\0', sizeof (state));
/* <span class="roman">Use <var>state</var>.</span> */
…
if (! mbsinit (&state))
{
/* <span class="roman">Emit code to return to initial state.</span> */
const wchar_t empty[] = L"";
const wchar_t *srcp = empty;
wcsrtombs (outbuf, &srcp, outbuflen, &state);
}
…
}
</pre></td></tr></table>
<p>The code to emit the escape sequence to get back to the initial state is
interesting. The <code>wcsrtombs</code> function can be used to determine the
necessary output code (see section <a href="#Converting-Strings">Converting Multibyte and Wide Character Strings</a>). Please note that on
GNU systems it is not necessary to perform this extra action for the
conversion from multibyte text to wide character text since the wide
character encoding is not stateful. But there is nothing mentioned in
any standard that prohibits making <code>wchar_t</code> using a stateful
encoding.
</p>
<hr size="6">
<a name="Converting-a-Character"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Keeping-the-state" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Converting-Strings" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Restartable-multibyte-conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Converting-Single-Characters"></a>
<h3 class="subsection">6.3.3 Converting Single Characters</h3>
<p>The most fundamental of the conversion functions are those dealing with
single characters. Please note that this does not always mean single
bytes. But since there is very often a subset of the multibyte
character set that consists of single byte sequences, there are
functions to help with converting bytes. Frequently, ASCII is a subpart
of the multibyte character set. In such a scenario, each ASCII character
stands for itself, and all other characters have at least a first byte
that is beyond the range <em>0</em> to <em>127</em>.
</p>
<dl>
<dt><a name="index-btowc"></a><u>Function:</u> wint_t <b>btowc</b><i> (int <var>c</var>)</i></dt>
<dd><p>The <code>btowc</code> function (“byte to wide character”) converts a valid
single byte character <var>c</var> in the initial shift state into the wide
character equivalent using the conversion rules from the currently
selected locale of the <code>LC_CTYPE</code> category.
</p>
<p>If <code>(unsigned char) <var>c</var></code> is no valid single byte multibyte
character or if <var>c</var> is <code>EOF</code>, the function returns <code>WEOF</code>.
</p>
<p>Please note the restriction of <var>c</var> being tested for validity only in
the initial shift state. No <code>mbstate_t</code> object is used from
which the state information is taken, and the function also does not use
any static state.
</p>
<a name="index-wchar_002eh-6"></a>
<p>The <code>btowc</code> function was introduced in Amendment 1 to ISO C90
and is declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>Despite the limitation that the single byte value is always interpreted
in the initial state, this function is actually useful most of the time.
Most characters are either entirely single-byte character sets or they
are extension to ASCII. But then it is possible to write code like this
(not that this specific example is very useful):
</p>
<table><tr><td> </td><td><pre class="smallexample">wchar_t *
itow (unsigned long int val)
{
static wchar_t buf[30];
wchar_t *wcp = &buf[29];
*wcp = L'\0';
while (val != 0)
{
*--wcp = btowc ('0' + val % 10);
val /= 10;
}
if (wcp == &buf[29])
*--wcp = L'0';
return wcp;
}
</pre></td></tr></table>
<p>Why is it necessary to use such a complicated implementation and not
simply cast <code>'0' + val % 10</code> to a wide character? The answer is
that there is no guarantee that one can perform this kind of arithmetic
on the character of the character set used for <code>wchar_t</code>
representation. In other situations the bytes are not constant at
compile time and so the compiler cannot do the work. In situations like
this, using <code>btowc</code> is required.
</p>
<p>There is also a function for the conversion in the other direction.
</p>
<dl>
<dt><a name="index-wctob"></a><u>Function:</u> int <b>wctob</b><i> (wint_t <var>c</var>)</i></dt>
<dd><p>The <code>wctob</code> function (“wide character to byte”) takes as the
parameter a valid wide character. If the multibyte representation for
this character in the initial state is exactly one byte long, the return
value of this function is this character. Otherwise the return value is
<code>EOF</code>.
</p>
<a name="index-wchar_002eh-7"></a>
<p><code>wctob</code> was introduced in Amendment 1 to ISO C90 and
is declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>There are more general functions to convert single character from
multibyte representation to wide characters and vice versa. These
functions pose no limit on the length of the multibyte representation
and they also do not require it to be in the initial state.
</p>
<dl>
<dt><a name="index-mbrtowc"></a><u>Function:</u> size_t <b>mbrtowc</b><i> (wchar_t *restrict <var>pwc</var>, const char *restrict <var>s</var>, size_t <var>n</var>, mbstate_t *restrict <var>ps</var>)</i></dt>
<dd><a name="index-stateful-1"></a>
<p>The <code>mbrtowc</code> function (“multibyte restartable to wide
character”) converts the next multibyte character in the string pointed
to by <var>s</var> into a wide character and stores it in the wide character
string pointed to by <var>pwc</var>. The conversion is performed according
to the locale currently selected for the <code>LC_CTYPE</code> category. If
the conversion for the character set used in the locale requires a state,
the multibyte string is interpreted in the state represented by the
object pointed to by <var>ps</var>. If <var>ps</var> is a null pointer, a static,
internal state variable used only by the <code>mbrtowc</code> function is
used.
</p>
<p>If the next multibyte character corresponds to the NUL wide character,
the return value of the function is <em>0</em> and the state object is
afterwards in the initial state. If the next <var>n</var> or fewer bytes
form a correct multibyte character, the return value is the number of
bytes starting from <var>s</var> that form the multibyte character. The
conversion state is updated according to the bytes consumed in the
conversion. In both cases the wide character (either the <code>L'\0'</code>
or the one found in the conversion) is stored in the string pointed to
by <var>pwc</var> if <var>pwc</var> is not null.
</p>
<p>If the first <var>n</var> bytes of the multibyte string possibly form a valid
multibyte character but there are more than <var>n</var> bytes needed to
complete it, the return value of the function is <code>(size_t) -2</code> and
no value is stored. Please note that this can happen even if <var>n</var>
has a value greater than or equal to <code>MB_CUR_MAX</code> since the input
might contain redundant shift sequences.
</p>
<p>If the first <code>n</code> bytes of the multibyte string cannot possibly form
a valid multibyte character, no value is stored, the global variable
<code>errno</code> is set to the value <code>EILSEQ</code>, and the function returns
<code>(size_t) -1</code>. The conversion state is afterwards undefined.
</p>
<a name="index-wchar_002eh-8"></a>
<p><code>mbrtowc</code> was introduced in Amendment 1 to ISO C90 and
is declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>Use of <code>mbrtowc</code> is straightforward. A function that copies a
multibyte string into a wide character string while at the same time
converting all lowercase characters into uppercase could look like this
(this is not the final version, just an example; it has no error
checking, and sometimes leaks memory):
</p>
<table><tr><td> </td><td><pre class="smallexample">wchar_t *
mbstouwcs (const char *s)
{
size_t len = strlen (s);
wchar_t *result = malloc ((len + 1) * sizeof (wchar_t));
wchar_t *wcp = result;
wchar_t tmp[1];
mbstate_t state;
size_t nbytes;
memset (&state, '\0', sizeof (state));
while ((nbytes = mbrtowc (tmp, s, len, &state)) > 0)
{
if (nbytes >= (size_t) -2)
/* Invalid input string. */
return NULL;
*wcp++ = towupper (tmp[0]);
len -= nbytes;
s += nbytes;
}
return result;
}
</pre></td></tr></table>
<p>The use of <code>mbrtowc</code> should be clear. A single wide character is
stored in <code><var>tmp</var>[0]</code>, and the number of consumed bytes is stored
in the variable <var>nbytes</var>. If the conversion is successful, the
uppercase variant of the wide character is stored in the <var>result</var>
array and the pointer to the input string and the number of available
bytes is adjusted.
</p>
<p>The only non-obvious thing about <code>mbrtowc</code> might be the way memory
is allocated for the result. The above code uses the fact that there
can never be more wide characters in the converted results than there are
bytes in the multibyte input string. This method yields a pessimistic
guess about the size of the result, and if many wide character strings
have to be constructed this way or if the strings are long, the extra
memory required to be allocated because the input string contains
multibyte characters might be significant. The allocated memory block can
be resized to the correct size before returning it, but a better solution
might be to allocate just the right amount of space for the result right
away. Unfortunately there is no function to compute the length of the wide
character string directly from the multibyte string. There is, however, a
function that does part of the work.
</p>
<dl>
<dt><a name="index-mbrlen"></a><u>Function:</u> size_t <b>mbrlen</b><i> (const char *restrict <var>s</var>, size_t <var>n</var>, mbstate_t *<var>ps</var>)</i></dt>
<dd><p>The <code>mbrlen</code> function (“multibyte restartable length”) computes
the number of at most <var>n</var> bytes starting at <var>s</var>, which form the
next valid and complete multibyte character.
</p>
<p>If the next multibyte character corresponds to the NUL wide character,
the return value is <em>0</em>. If the next <var>n</var> bytes form a valid
multibyte character, the number of bytes belonging to this multibyte
character byte sequence is returned.
</p>
<p>If the first <var>n</var> bytes possibly form a valid multibyte
character but the character is incomplete, the return value is
<code>(size_t) -2</code>. Otherwise the multibyte character sequence is invalid
and the return value is <code>(size_t) -1</code>.
</p>
<p>The multibyte sequence is interpreted in the state represented by the
object pointed to by <var>ps</var>. If <var>ps</var> is a null pointer, a state
object local to <code>mbrlen</code> is used.
</p>
<a name="index-wchar_002eh-9"></a>
<p><code>mbrlen</code> was introduced in Amendment 1 to ISO C90 and
is declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>The attentive reader now will note that <code>mbrlen</code> can be implemented
as
</p>
<table><tr><td> </td><td><pre class="smallexample">mbrtowc (NULL, s, n, ps != NULL ? ps : &internal)
</pre></td></tr></table>
<p>This is true and in fact is mentioned in the official specification.
How can this function be used to determine the length of the wide
character string created from a multibyte character string? It is not
directly usable, but we can define a function <code>mbslen</code> using it:
</p>
<table><tr><td> </td><td><pre class="smallexample">size_t
mbslen (const char *s)
{
mbstate_t state;
size_t result = 0;
size_t nbytes;
memset (&state, '\0', sizeof (state));
while ((nbytes = mbrlen (s, MB_LEN_MAX, &state)) > 0)
{
if (nbytes >= (size_t) -2)
/* <span class="roman">Something is wrong.</span> */
return (size_t) -1;
s += nbytes;
++result;
}
return result;
}
</pre></td></tr></table>
<p>This function simply calls <code>mbrlen</code> for each multibyte character
in the string and counts the number of function calls. Please note that
we here use <code>MB_LEN_MAX</code> as the size argument in the <code>mbrlen</code>
call. This is acceptable since a) this value is larger then the length of
the longest multibyte character sequence and b) we know that the string
<var>s</var> ends with a NUL byte, which cannot be part of any other multibyte
character sequence but the one representing the NUL wide character.
Therefore, the <code>mbrlen</code> function will never read invalid memory.
</p>
<p>Now that this function is available (just to make this clear, this
function is <em>not</em> part of the GNU C library) we can compute the
number of wide character required to store the converted multibyte
character string <var>s</var> using
</p>
<table><tr><td> </td><td><pre class="smallexample">wcs_bytes = (mbslen (s) + 1) * sizeof (wchar_t);
</pre></td></tr></table>
<p>Please note that the <code>mbslen</code> function is quite inefficient. The
implementation of <code>mbstouwcs</code> with <code>mbslen</code> would have to
perform the conversion of the multibyte character input string twice, and
this conversion might be quite expensive. So it is necessary to think
about the consequences of using the easier but imprecise method before
doing the work twice.
</p>
<dl>
<dt><a name="index-wcrtomb"></a><u>Function:</u> size_t <b>wcrtomb</b><i> (char *restrict <var>s</var>, wchar_t <var>wc</var>, mbstate_t *restrict <var>ps</var>)</i></dt>
<dd><p>The <code>wcrtomb</code> function (“wide character restartable to
multibyte”) converts a single wide character into a multibyte string
corresponding to that wide character.
</p>
<p>If <var>s</var> is a null pointer, the function resets the state stored in
the objects pointed to by <var>ps</var> (or the internal <code>mbstate_t</code>
object) to the initial state. This can also be achieved by a call like
this:
</p>
<table><tr><td> </td><td><pre class="smallexample">wcrtombs (temp_buf, L'\0', ps)
</pre></td></tr></table>
<p>since, if <var>s</var> is a null pointer, <code>wcrtomb</code> performs as if it
writes into an internal buffer, which is guaranteed to be large enough.
</p>
<p>If <var>wc</var> is the NUL wide character, <code>wcrtomb</code> emits, if
necessary, a shift sequence to get the state <var>ps</var> into the initial
state followed by a single NUL byte, which is stored in the string
<var>s</var>.
</p>
<p>Otherwise a byte sequence (possibly including shift sequences) is written
into the string <var>s</var>. This only happens if <var>wc</var> is a valid wide
character (i.e., it has a multibyte representation in the character set
selected by locale of the <code>LC_CTYPE</code> category). If <var>wc</var> is no
valid wide character, nothing is stored in the strings <var>s</var>,
<code>errno</code> is set to <code>EILSEQ</code>, the conversion state in <var>ps</var>
is undefined and the return value is <code>(size_t) -1</code>.
</p>
<p>If no error occurred the function returns the number of bytes stored in
the string <var>s</var>. This includes all bytes representing shift
sequences.
</p>
<p>One word about the interface of the function: there is no parameter
specifying the length of the array <var>s</var>. Instead the function
assumes that there are at least <code>MB_CUR_MAX</code> bytes available since
this is the maximum length of any byte sequence representing a single
character. So the caller has to make sure that there is enough space
available, otherwise buffer overruns can occur.
</p>
<a name="index-wchar_002eh-10"></a>
<p><code>wcrtomb</code> was introduced in Amendment 1 to ISO C90 and is
declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>Using <code>wcrtomb</code> is as easy as using <code>mbrtowc</code>. The following
example appends a wide character string to a multibyte character string.
Again, the code is not really useful (or correct), it is simply here to
demonstrate the use and some problems.
</p>
<table><tr><td> </td><td><pre class="smallexample">char *
mbscatwcs (char *s, size_t len, const wchar_t *ws)
{
mbstate_t state;
/* <span class="roman">Find the end of the existing string.</span> */
char *wp = strchr (s, '\0');
len -= wp - s;
memset (&state, '\0', sizeof (state));
do
{
size_t nbytes;
if (len < MB_CUR_LEN)
{
/* <span class="roman">We cannot guarantee that the next</span>
<span class="roman">character fits into the buffer, so</span>
<span class="roman">return an error.</span> */
errno = E2BIG;
return NULL;
}
nbytes = wcrtomb (wp, *ws, &state);
if (nbytes == (size_t) -1)
/* <span class="roman">Error in the conversion.</span> */
return NULL;
len -= nbytes;
wp += nbytes;
}
while (*ws++ != L'\0');
return s;
}
</pre></td></tr></table>
<p>First the function has to find the end of the string currently in the
array <var>s</var>. The <code>strchr</code> call does this very efficiently since a
requirement for multibyte character representations is that the NUL byte
is never used except to represent itself (and in this context, the end
of the string).
</p>
<p>After initializing the state object the loop is entered where the first
task is to make sure there is enough room in the array <var>s</var>. We
abort if there are not at least <code>MB_CUR_LEN</code> bytes available. This
is not always optimal but we have no other choice. We might have less
than <code>MB_CUR_LEN</code> bytes available but the next multibyte character
might also be only one byte long. At the time the <code>wcrtomb</code> call
returns it is too late to decide whether the buffer was large enough. If
this solution is unsuitable, there is a very slow but more accurate
solution.
</p>
<table><tr><td> </td><td><pre class="smallexample"> …
if (len < MB_CUR_LEN)
{
mbstate_t temp_state;
memcpy (&temp_state, &state, sizeof (state));
if (wcrtomb (NULL, *ws, &temp_state) > len)
{
/* <span class="roman">We cannot guarantee that the next</span>
<span class="roman">character fits into the buffer, so</span>
<span class="roman">return an error.</span> */
errno = E2BIG;
return NULL;
}
}
…
</pre></td></tr></table>
<p>Here we perform the conversion that might overflow the buffer so that
we are afterwards in the position to make an exact decision about the
buffer size. Please note the <code>NULL</code> argument for the destination
buffer in the new <code>wcrtomb</code> call; since we are not interested in the
converted text at this point, this is a nice way to express this. The
most unusual thing about this piece of code certainly is the duplication
of the conversion state object, but if a change of the state is necessary
to emit the next multibyte character, we want to have the same shift state
change performed in the real conversion. Therefore, we have to preserve
the initial shift state information.
</p>
<p>There are certainly many more and even better solutions to this problem.
This example is only provided for educational purposes.
</p>
<hr size="6">
<a name="Converting-Strings"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Converting-a-Character" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Multibyte-Conversion-Example" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Restartable-multibyte-conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Converting-Multibyte-and-Wide-Character-Strings"></a>
<h3 class="subsection">6.3.4 Converting Multibyte and Wide Character Strings</h3>
<p>The functions described in the previous section only convert a single
character at a time. Most operations to be performed in real-world
programs include strings and therefore the ISO C standard also
defines conversions on entire strings. However, the defined set of
functions is quite limited; therefore, the GNU C library contains a few
extensions that can help in some important situations.
</p>
<dl>
<dt><a name="index-mbsrtowcs"></a><u>Function:</u> size_t <b>mbsrtowcs</b><i> (wchar_t *restrict <var>dst</var>, const char **restrict <var>src</var>, size_t <var>len</var>, mbstate_t *restrict <var>ps</var>)</i></dt>
<dd><p>The <code>mbsrtowcs</code> function (“multibyte string restartable to wide
character string”) converts an NUL-terminated multibyte character
string at <code>*<var>src</var></code> into an equivalent wide character string,
including the NUL wide character at the end. The conversion is started
using the state information from the object pointed to by <var>ps</var> or
from an internal object of <code>mbsrtowcs</code> if <var>ps</var> is a null
pointer. Before returning, the state object is updated to match the state
after the last converted character. The state is the initial state if the
terminating NUL byte is reached and converted.
</p>
<p>If <var>dst</var> is not a null pointer, the result is stored in the array
pointed to by <var>dst</var>; otherwise, the conversion result is not
available since it is stored in an internal buffer.
</p>
<p>If <var>len</var> wide characters are stored in the array <var>dst</var> before
reaching the end of the input string, the conversion stops and <var>len</var>
is returned. If <var>dst</var> is a null pointer, <var>len</var> is never checked.
</p>
<p>Another reason for a premature return from the function call is if the
input string contains an invalid multibyte sequence. In this case the
global variable <code>errno</code> is set to <code>EILSEQ</code> and the function
returns <code>(size_t) -1</code>.
</p>
<p>In all other cases the function returns the number of wide characters
converted during this call. If <var>dst</var> is not null, <code>mbsrtowcs</code>
stores in the pointer pointed to by <var>src</var> either a null pointer (if
the NUL byte in the input string was reached) or the address of the byte
following the last converted multibyte character.
</p>
<a name="index-wchar_002eh-11"></a>
<p><code>mbsrtowcs</code> was introduced in Amendment 1 to ISO C90 and is
declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>The definition of the <code>mbsrtowcs</code> function has one important
limitation. The requirement that <var>dst</var> has to be a NUL-terminated
string provides problems if one wants to convert buffers with text. A
buffer is normally no collection of NUL-terminated strings but instead a
continuous collection of lines, separated by newline characters. Now
assume that a function to convert one line from a buffer is needed. Since
the line is not NUL-terminated, the source pointer cannot directly point
into the unmodified text buffer. This means, either one inserts the NUL
byte at the appropriate place for the time of the <code>mbsrtowcs</code>
function call (which is not doable for a read-only buffer or in a
multi-threaded application) or one copies the line in an extra buffer
where it can be terminated by a NUL byte. Note that it is not in general
possible to limit the number of characters to convert by setting the
parameter <var>len</var> to any specific value. Since it is not known how
many bytes each multibyte character sequence is in length, one can only
guess.
</p>
<a name="index-stateful-2"></a>
<p>There is still a problem with the method of NUL-terminating a line right
after the newline character, which could lead to very strange results.
As said in the description of the <code>mbsrtowcs</code> function above the
conversion state is guaranteed to be in the initial shift state after
processing the NUL byte at the end of the input string. But this NUL
byte is not really part of the text (i.e., the conversion state after
the newline in the original text could be something different than the
initial shift state and therefore the first character of the next line
is encoded using this state). But the state in question is never
accessible to the user since the conversion stops after the NUL byte
(which resets the state). Most stateful character sets in use today
require that the shift state after a newline be the initial state–but
this is not a strict guarantee. Therefore, simply NUL-terminating a
piece of a running text is not always an adequate solution and,
therefore, should never be used in generally used code.
</p>
<p>The generic conversion interface (see section <a href="#Generic-Charset-Conversion">Generic Charset Conversion</a>)
does not have this limitation (it simply works on buffers, not
strings), and the GNU C library contains a set of functions that take
additional parameters specifying the maximal number of bytes that are
consumed from the input string. This way the problem of
<code>mbsrtowcs</code>’s example above could be solved by determining the line
length and passing this length to the function.
</p>
<dl>
<dt><a name="index-wcsrtombs"></a><u>Function:</u> size_t <b>wcsrtombs</b><i> (char *restrict <var>dst</var>, const wchar_t **restrict <var>src</var>, size_t <var>len</var>, mbstate_t *restrict <var>ps</var>)</i></dt>
<dd><p>The <code>wcsrtombs</code> function (“wide character string restartable to
multibyte string”) converts the NUL-terminated wide character string at
<code>*<var>src</var></code> into an equivalent multibyte character string and
stores the result in the array pointed to by <var>dst</var>. The NUL wide
character is also converted. The conversion starts in the state
described in the object pointed to by <var>ps</var> or by a state object
locally to <code>wcsrtombs</code> in case <var>ps</var> is a null pointer. If
<var>dst</var> is a null pointer, the conversion is performed as usual but the
result is not available. If all characters of the input string were
successfully converted and if <var>dst</var> is not a null pointer, the
pointer pointed to by <var>src</var> gets assigned a null pointer.
</p>
<p>If one of the wide characters in the input string has no valid multibyte
character equivalent, the conversion stops early, sets the global
variable <code>errno</code> to <code>EILSEQ</code>, and returns <code>(size_t) -1</code>.
</p>
<p>Another reason for a premature stop is if <var>dst</var> is not a null
pointer and the next converted character would require more than
<var>len</var> bytes in total to the array <var>dst</var>. In this case (and if
<var>dest</var> is not a null pointer) the pointer pointed to by <var>src</var> is
assigned a value pointing to the wide character right after the last one
successfully converted.
</p>
<p>Except in the case of an encoding error the return value of the
<code>wcsrtombs</code> function is the number of bytes in all the multibyte
character sequences stored in <var>dst</var>. Before returning the state in
the object pointed to by <var>ps</var> (or the internal object in case
<var>ps</var> is a null pointer) is updated to reflect the state after the
last conversion. The state is the initial shift state in case the
terminating NUL wide character was converted.
</p>
<a name="index-wchar_002eh-12"></a>
<p>The <code>wcsrtombs</code> function was introduced in Amendment 1 to
ISO C90 and is declared in ‘<tt>wchar.h</tt>’.
</p></dd></dl>
<p>The restriction mentioned above for the <code>mbsrtowcs</code> function applies
here also. There is no possibility of directly controlling the number of
input characters. One has to place the NUL wide character at the correct
place or control the consumed input indirectly via the available output
array size (the <var>len</var> parameter).
</p>
<dl>
<dt><a name="index-mbsnrtowcs"></a><u>Function:</u> size_t <b>mbsnrtowcs</b><i> (wchar_t *restrict <var>dst</var>, const char **restrict <var>src</var>, size_t <var>nmc</var>, size_t <var>len</var>, mbstate_t *restrict <var>ps</var>)</i></dt>
<dd><p>The <code>mbsnrtowcs</code> function is very similar to the <code>mbsrtowcs</code>
function. All the parameters are the same except for <var>nmc</var>, which is
new. The return value is the same as for <code>mbsrtowcs</code>.
</p>
<p>This new parameter specifies how many bytes at most can be used from the
multibyte character string. In other words, the multibyte character
string <code>*<var>src</var></code> need not be NUL-terminated. But if a NUL byte
is found within the <var>nmc</var> first bytes of the string, the conversion
stops here.
</p>
<p>This function is a GNU extension. It is meant to work around the
problems mentioned above. Now it is possible to convert a buffer with
multibyte character text piece for piece without having to care about
inserting NUL bytes and the effect of NUL bytes on the conversion state.
</p></dd></dl>
<p>A function to convert a multibyte string into a wide character string
and display it could be written like this (this is not a really useful
example):
</p>
<table><tr><td> </td><td><pre class="smallexample">void
showmbs (const char *src, FILE *fp)
{
mbstate_t state;
int cnt = 0;
memset (&state, '\0', sizeof (state));
while (1)
{
wchar_t linebuf[100];
const char *endp = strchr (src, '\n');
size_t n;
/* <span class="roman">Exit if there is no more line.</span> */
if (endp == NULL)
break;
n = mbsnrtowcs (linebuf, &src, endp - src, 99, &state);
linebuf[n] = L'\0';
fprintf (fp, "line %d: \"%S\"\n", linebuf);
}
}
</pre></td></tr></table>
<p>There is no problem with the state after a call to <code>mbsnrtowcs</code>.
Since we don’t insert characters in the strings that were not in there
right from the beginning and we use <var>state</var> only for the conversion
of the given buffer, there is no problem with altering the state.
</p>
<dl>
<dt><a name="index-wcsnrtombs"></a><u>Function:</u> size_t <b>wcsnrtombs</b><i> (char *restrict <var>dst</var>, const wchar_t **restrict <var>src</var>, size_t <var>nwc</var>, size_t <var>len</var>, mbstate_t *restrict <var>ps</var>)</i></dt>
<dd><p>The <code>wcsnrtombs</code> function implements the conversion from wide
character strings to multibyte character strings. It is similar to
<code>wcsrtombs</code> but, just like <code>mbsnrtowcs</code>, it takes an extra
parameter, which specifies the length of the input string.
</p>
<p>No more than <var>nwc</var> wide characters from the input string
<code>*<var>src</var></code> are converted. If the input string contains a NUL
wide character in the first <var>nwc</var> characters, the conversion stops at
this place.
</p>
<p>The <code>wcsnrtombs</code> function is a GNU extension and just like
<code>mbsnrtowcs</code> helps in situations where no NUL-terminated input
strings are available.
</p></dd></dl>
<hr size="6">
<a name="Multibyte-Conversion-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Converting-Strings" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Non_002dreentrant-Conversion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Restartable-multibyte-conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="A-Complete-Multibyte-Conversion-Example"></a>
<h3 class="subsection">6.3.5 A Complete Multibyte Conversion Example</h3>
<p>The example programs given in the last sections are only brief and do
not contain all the error checking, etc. Presented here is a complete
and documented example. It features the <code>mbrtowc</code> function but it
should be easy to derive versions using the other functions.
</p>
<table><tr><td> </td><td><pre class="smallexample">int
file_mbsrtowcs (int input, int output)
{
/* <span class="roman">Note the use of <code>MB_LEN_MAX</code>.</span>
<span class="roman"><code>MB_CUR_MAX</code> cannot portably be used here.</span> */
char buffer[BUFSIZ + MB_LEN_MAX];
mbstate_t state;
int filled = 0;
int eof = 0;
/* <span class="roman">Initialize the state.</span> */
memset (&state, '\0', sizeof (state));
while (!eof)
{
ssize_t nread;
ssize_t nwrite;
char *inp = buffer;
wchar_t outbuf[BUFSIZ];
wchar_t *outp = outbuf;
/* <span class="roman">Fill up the buffer from the input file.</span> */
nread = read (input, buffer + filled, BUFSIZ);
if (nread < 0)
{
perror ("read");
return 0;
}
/* <span class="roman">If we reach end of file, make a note to read no more.</span> */
if (nread == 0)
eof = 1;
/* <span class="roman"><code>filled</code> is now the number of bytes in <code>buffer</code>.</span> */
filled += nread;
/* <span class="roman">Convert those bytes to wide characters--as many as we can.</span> */
while (1)
{
size_t thislen = mbrtowc (outp, inp, filled, &state);
/* <span class="roman">Stop converting at invalid character;</span>
<span class="roman">this can mean we have read just the first part</span>
<span class="roman">of a valid character.</span> */
if (thislen == (size_t) -1)
break;
/* <span class="roman">We want to handle embedded NUL bytes</span>
<span class="roman">but the return value is 0. Correct this.</span> */
if (thislen == 0)
thislen = 1;
/* <span class="roman">Advance past this character.</span> */
inp += thislen;
filled -= thislen;
++outp;
}
/* <span class="roman">Write the wide characters we just made.</span> */
nwrite = write (output, outbuf,
(outp - outbuf) * sizeof (wchar_t));
if (nwrite < 0)
{
perror ("write");
return 0;
}
/* <span class="roman">See if we have a <em>real</em> invalid character.</span> */
if ((eof && filled > 0) || filled >= MB_CUR_MAX)
{
error (0, 0, "invalid multibyte character");
return 0;
}
/* <span class="roman">If any characters must be carried forward,</span>
<span class="roman">put them at the beginning of <code>buffer</code>.</span> */
if (filled > 0)
memmove (buffer, inp, filled);
}
return 1;
}
</pre></td></tr></table>
<hr size="6">
<a name="Non_002dreentrant-Conversion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Multibyte-Conversion-Example" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Non_002dreentrant-Character-Conversion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Non_002dreentrant-Conversion-Function"></a>
<h2 class="section">6.4 Non-reentrant Conversion Function</h2>
<p>The functions described in the previous chapter are defined in
Amendment 1 to ISO C90, but the original ISO C90 standard
also contained functions for character set conversion. The reason that
these original functions are not described first is that they are almost
entirely useless.
</p>
<p>The problem is that all the conversion functions described in the
original ISO C90 use a local state. Using a local state implies that
multiple conversions at the same time (not only when using threads)
cannot be done, and that you cannot first convert single characters and
then strings since you cannot tell the conversion functions which state
to use.
</p>
<p>These original functions are therefore usable only in a very limited set
of situations. One must complete converting the entire string before
starting a new one, and each string/text must be converted with the same
function (there is no problem with the library itself; it is guaranteed
that no library function changes the state of any of these functions).
<strong>For the above reasons it is highly requested that the functions
described in the previous section be used in place of non-reentrant
conversion functions.</strong>
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Non_002dreentrant-Character-Conversion">6.4.1 Non-reentrant Conversion of Single Characters</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Non_002dreentrant-String-Conversion">6.4.2 Non-reentrant Conversion of Strings</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Shift-State">6.4.3 States in Non-reentrant Functions</a></td><td> </td><td align="left" valign="top"></td></tr>
</table>
<hr size="6">
<a name="Non_002dreentrant-Character-Conversion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Non_002dreentrant-Conversion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Non_002dreentrant-String-Conversion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Non_002dreentrant-Conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Non_002dreentrant-Conversion-of-Single-Characters"></a>
<h3 class="subsection">6.4.1 Non-reentrant Conversion of Single Characters</h3>
<dl>
<dt><a name="index-mbtowc"></a><u>Function:</u> int <b>mbtowc</b><i> (wchar_t *restrict <var>result</var>, const char *restrict <var>string</var>, size_t <var>size</var>)</i></dt>
<dd><p>The <code>mbtowc</code> (“multibyte to wide character”) function when called
with non-null <var>string</var> converts the first multibyte character
beginning at <var>string</var> to its corresponding wide character code. It
stores the result in <code>*<var>result</var></code>.
</p>
<p><code>mbtowc</code> never examines more than <var>size</var> bytes. (The idea is
to supply for <var>size</var> the number of bytes of data you have in hand.)
</p>
<p><code>mbtowc</code> with non-null <var>string</var> distinguishes three
possibilities: the first <var>size</var> bytes at <var>string</var> start with
valid multibyte characters, they start with an invalid byte sequence or
just part of a character, or <var>string</var> points to an empty string (a
null character).
</p>
<p>For a valid multibyte character, <code>mbtowc</code> converts it to a wide
character and stores that in <code>*<var>result</var></code>, and returns the
number of bytes in that character (always at least <em>1</em> and never
more than <var>size</var>).
</p>
<p>For an invalid byte sequence, <code>mbtowc</code> returns <em>-1</em>. For an
empty string, it returns <em>0</em>, also storing <code>'\0'</code> in
<code>*<var>result</var></code>.
</p>
<p>If the multibyte character code uses shift characters, then
<code>mbtowc</code> maintains and updates a shift state as it scans. If you
call <code>mbtowc</code> with a null pointer for <var>string</var>, that
initializes the shift state to its standard initial value. It also
returns nonzero if the multibyte character code in use actually has a
shift state. See section <a href="#Shift-State">States in Non-reentrant Functions</a>.
</p></dd></dl>
<dl>
<dt><a name="index-wctomb"></a><u>Function:</u> int <b>wctomb</b><i> (char *<var>string</var>, wchar_t <var>wchar</var>)</i></dt>
<dd><p>The <code>wctomb</code> (“wide character to multibyte”) function converts
the wide character code <var>wchar</var> to its corresponding multibyte
character sequence, and stores the result in bytes starting at
<var>string</var>. At most <code>MB_CUR_MAX</code> characters are stored.
</p>
<p><code>wctomb</code> with non-null <var>string</var> distinguishes three
possibilities for <var>wchar</var>: a valid wide character code (one that can
be translated to a multibyte character), an invalid code, and
<code>L'\0'</code>.
</p>
<p>Given a valid code, <code>wctomb</code> converts it to a multibyte character,
storing the bytes starting at <var>string</var>. Then it returns the number
of bytes in that character (always at least <em>1</em> and never more
than <code>MB_CUR_MAX</code>).
</p>
<p>If <var>wchar</var> is an invalid wide character code, <code>wctomb</code> returns
<em>-1</em>. If <var>wchar</var> is <code>L'\0'</code>, it returns <code>0</code>, also
storing <code>'\0'</code> in <code>*<var>string</var></code>.
</p>
<p>If the multibyte character code uses shift characters, then
<code>wctomb</code> maintains and updates a shift state as it scans. If you
call <code>wctomb</code> with a null pointer for <var>string</var>, that
initializes the shift state to its standard initial value. It also
returns nonzero if the multibyte character code in use actually has a
shift state. See section <a href="#Shift-State">States in Non-reentrant Functions</a>.
</p>
<p>Calling this function with a <var>wchar</var> argument of zero when
<var>string</var> is not null has the side-effect of reinitializing the
stored shift state <em>as well as</em> storing the multibyte character
<code>'\0'</code> and returning <em>0</em>.
</p></dd></dl>
<p>Similar to <code>mbrlen</code> there is also a non-reentrant function that
computes the length of a multibyte character. It can be defined in
terms of <code>mbtowc</code>.
</p>
<dl>
<dt><a name="index-mblen"></a><u>Function:</u> int <b>mblen</b><i> (const char *<var>string</var>, size_t <var>size</var>)</i></dt>
<dd><p>The <code>mblen</code> function with a non-null <var>string</var> argument returns
the number of bytes that make up the multibyte character beginning at
<var>string</var>, never examining more than <var>size</var> bytes. (The idea is
to supply for <var>size</var> the number of bytes of data you have in hand.)
</p>
<p>The return value of <code>mblen</code> distinguishes three possibilities: the
first <var>size</var> bytes at <var>string</var> start with valid multibyte
characters, they start with an invalid byte sequence or just part of a
character, or <var>string</var> points to an empty string (a null character).
</p>
<p>For a valid multibyte character, <code>mblen</code> returns the number of
bytes in that character (always at least <code>1</code> and never more than
<var>size</var>). For an invalid byte sequence, <code>mblen</code> returns
<em>-1</em>. For an empty string, it returns <em>0</em>.
</p>
<p>If the multibyte character code uses shift characters, then <code>mblen</code>
maintains and updates a shift state as it scans. If you call
<code>mblen</code> with a null pointer for <var>string</var>, that initializes the
shift state to its standard initial value. It also returns a nonzero
value if the multibyte character code in use actually has a shift state.
See section <a href="#Shift-State">States in Non-reentrant Functions</a>.
</p>
<a name="index-stdlib_002eh-7"></a>
<p>The function <code>mblen</code> is declared in ‘<tt>stdlib.h</tt>’.
</p></dd></dl>
<hr size="6">
<a name="Non_002dreentrant-String-Conversion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Non_002dreentrant-Character-Conversion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Shift-State" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Non_002dreentrant-Conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Non_002dreentrant-Conversion-of-Strings"></a>
<h3 class="subsection">6.4.2 Non-reentrant Conversion of Strings</h3>
<p>For convenience the ISO C90 standard also defines functions to
convert entire strings instead of single characters. These functions
suffer from the same problems as their reentrant counterparts from
Amendment 1 to ISO C90; see <a href="#Converting-Strings">Converting Multibyte and Wide Character Strings</a>.
</p>
<dl>
<dt><a name="index-mbstowcs"></a><u>Function:</u> size_t <b>mbstowcs</b><i> (wchar_t *<var>wstring</var>, const char *<var>string</var>, size_t <var>size</var>)</i></dt>
<dd><p>The <code>mbstowcs</code> (“multibyte string to wide character string”)
function converts the null-terminated string of multibyte characters
<var>string</var> to an array of wide character codes, storing not more than
<var>size</var> wide characters into the array beginning at <var>wstring</var>.
The terminating null character counts towards the size, so if <var>size</var>
is less than the actual number of wide characters resulting from
<var>string</var>, no terminating null character is stored.
</p>
<p>The conversion of characters from <var>string</var> begins in the initial
shift state.
</p>
<p>If an invalid multibyte character sequence is found, the <code>mbstowcs</code>
function returns a value of <em>-1</em>. Otherwise, it returns the number
of wide characters stored in the array <var>wstring</var>. This number does
not include the terminating null character, which is present if the
number is less than <var>size</var>.
</p>
<p>Here is an example showing how to convert a string of multibyte
characters, allocating enough space for the result.
</p>
<table><tr><td> </td><td><pre class="smallexample">wchar_t *
mbstowcs_alloc (const char *string)
{
size_t size = strlen (string) + 1;
wchar_t *buf = xmalloc (size * sizeof (wchar_t));
size = mbstowcs (buf, string, size);
if (size == (size_t) -1)
return NULL;
buf = xrealloc (buf, (size + 1) * sizeof (wchar_t));
return buf;
}
</pre></td></tr></table>
</dd></dl>
<dl>
<dt><a name="index-wcstombs"></a><u>Function:</u> size_t <b>wcstombs</b><i> (char *<var>string</var>, const wchar_t *<var>wstring</var>, size_t <var>size</var>)</i></dt>
<dd><p>The <code>wcstombs</code> (“wide character string to multibyte string”)
function converts the null-terminated wide character array <var>wstring</var>
into a string containing multibyte characters, storing not more than
<var>size</var> bytes starting at <var>string</var>, followed by a terminating
null character if there is room. The conversion of characters begins in
the initial shift state.
</p>
<p>The terminating null character counts towards the size, so if <var>size</var>
is less than or equal to the number of bytes needed in <var>wstring</var>, no
terminating null character is stored.
</p>
<p>If a code that does not correspond to a valid multibyte character is
found, the <code>wcstombs</code> function returns a value of <em>-1</em>.
Otherwise, the return value is the number of bytes stored in the array
<var>string</var>. This number does not include the terminating null character,
which is present if the number is less than <var>size</var>.
</p></dd></dl>
<hr size="6">
<a name="Shift-State"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Non_002dreentrant-String-Conversion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Generic-Charset-Conversion" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Non_002dreentrant-Conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="States-in-Non_002dreentrant-Functions"></a>
<h3 class="subsection">6.4.3 States in Non-reentrant Functions</h3>
<p>In some multibyte character codes, the <em>meaning</em> of any particular
byte sequence is not fixed; it depends on what other sequences have come
earlier in the same string. Typically there are just a few sequences that
can change the meaning of other sequences; these few are called
<em>shift sequences</em> and we say that they set the <em>shift state</em> for
other sequences that follow.
</p>
<p>To illustrate shift state and shift sequences, suppose we decide that
the sequence <code>0200</code> (just one byte) enters Japanese mode, in which
pairs of bytes in the range from <code>0240</code> to <code>0377</code> are single
characters, while <code>0201</code> enters Latin-1 mode, in which single bytes
in the range from <code>0240</code> to <code>0377</code> are characters, and
interpreted according to the ISO Latin-1 character set. This is a
multibyte code that has two alternative shift states (“Japanese mode”
and “Latin-1 mode”), and two shift sequences that specify particular
shift states.
</p>
<p>When the multibyte character code in use has shift states, then
<code>mblen</code>, <code>mbtowc</code>, and <code>wctomb</code> must maintain and update
the current shift state as they scan the string. To make this work
properly, you must follow these rules:
</p>
<ul>
<li>
Before starting to scan a string, call the function with a null pointer
for the multibyte character address—for example, <code>mblen (NULL,
0)</code>. This initializes the shift state to its standard initial value.
</li><li>
Scan the string one character at a time, in order. Do not “back up”
and rescan characters already scanned, and do not intersperse the
processing of different strings.
</li></ul>
<p>Here is an example of using <code>mblen</code> following these rules:
</p>
<table><tr><td> </td><td><pre class="smallexample">void
scan_string (char *s)
{
int length = strlen (s);
/* <span class="roman">Initialize shift state.</span> */
mblen (NULL, 0);
while (1)
{
int thischar = mblen (s, length);
/* <span class="roman">Deal with end of string and invalid characters.</span> */
if (thischar == 0)
break;
if (thischar == -1)
{
error ("invalid multibyte character");
break;
}
/* <span class="roman">Advance past this character.</span> */
s += thischar;
length -= thischar;
}
}
</pre></td></tr></table>
<p>The functions <code>mblen</code>, <code>mbtowc</code> and <code>wctomb</code> are not
reentrant when using a multibyte code that uses a shift state. However,
no other library functions call these functions, so you don’t have to
worry that the shift state will be changed mysteriously.
</p>
<hr size="6">
<a name="Generic-Charset-Conversion"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Shift-State" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Generic-Conversion-Interface" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Generic-Charset-Conversion-1"></a>
<h2 class="section">6.5 Generic Charset Conversion</h2>
<p>The conversion functions mentioned so far in this chapter all had in
common that they operate on character sets that are not directly
specified by the functions. The multibyte encoding used is specified by
the currently selected locale for the <code>LC_CTYPE</code> category. The
wide character set is fixed by the implementation (in the case of GNU C
library it is always UCS-4 encoded ISO 10646.
</p>
<p>This has of course several problems when it comes to general character
conversion:
</p>
<ul>
<li>
For every conversion where neither the source nor the destination
character set is the character set of the locale for the <code>LC_CTYPE</code>
category, one has to change the <code>LC_CTYPE</code> locale using
<code>setlocale</code>.
<p>Changing the <code>LC_TYPE</code> locale introduces major problems for the rest
of the programs since several more functions (e.g., the character
classification functions, see section <a href="libc_4.html#Classification-of-Characters">Classification of Characters</a>) use the
<code>LC_CTYPE</code> category.
</p>
</li><li>
Parallel conversions to and from different character sets are not
possible since the <code>LC_CTYPE</code> selection is global and shared by all
threads.
</li><li>
If neither the source nor the destination character set is the character
set used for <code>wchar_t</code> representation, there is at least a two-step
process necessary to convert a text using the functions above. One would
have to select the source character set as the multibyte encoding,
convert the text into a <code>wchar_t</code> text, select the destination
character set as the multibyte encoding, and convert the wide character
text to the multibyte (<em>=</em> destination) character set.
<p>Even if this is possible (which is not guaranteed) it is a very tiring
work. Plus it suffers from the other two raised points even more due to
the steady changing of the locale.
</p></li></ul>
<p>The XPG2 standard defines a completely new set of functions, which has
none of these limitations. They are not at all coupled to the selected
locales, and they have no constraints on the character sets selected for
source and destination. Only the set of available conversions limits
them. The standard does not specify that any conversion at all must be
available. Such availability is a measure of the quality of the
implementation.
</p>
<p>In the following text first the interface to <code>iconv</code> and then the
conversion function, will be described. Comparisons with other
implementations will show what obstacles stand in the way of portable
applications. Finally, the implementation is described in so far as might
interest the advanced user who wants to extend conversion capabilities.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Generic-Conversion-Interface">6.5.1 Generic Character Set Conversion Interface</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#iconv-Examples">6.5.2 A complete <code>iconv</code> example</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Other-iconv-Implementations">6.5.3 Some Details about other <code>iconv</code> Implementations</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#glibc-iconv-Implementation">6.5.4 The <code>iconv</code> Implementation in the GNU C library</a></td><td> </td><td align="left" valign="top"></td></tr>
</table>
<hr size="6">
<a name="Generic-Conversion-Interface"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Generic-Charset-Conversion" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#iconv-Examples" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Generic-Charset-Conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Generic-Character-Set-Conversion-Interface"></a>
<h3 class="subsection">6.5.1 Generic Character Set Conversion Interface</h3>
<p>This set of functions follows the traditional cycle of using a resource:
open–use–close. The interface consists of three functions, each of
which implements one step.
</p>
<p>Before the interfaces are described it is necessary to introduce a
data type. Just like other open–use–close interfaces the functions
introduced here work using handles and the ‘<tt>iconv.h</tt>’ header
defines a special type for the handles used.
</p>
<dl>
<dt><a name="index-iconv_005ft"></a><u>Data Type:</u> <b>iconv_t</b></dt>
<dd><p>This data type is an abstract type defined in ‘<tt>iconv.h</tt>’. The user
must not assume anything about the definition of this type; it must be
completely opaque.
</p>
<p>Objects of this type can get assigned handles for the conversions using
the <code>iconv</code> functions. The objects themselves need not be freed, but
the conversions for which the handles stand for have to.
</p></dd></dl>
<p>The first step is the function to create a handle.
</p>
<dl>
<dt><a name="index-iconv_005fopen"></a><u>Function:</u> iconv_t <b>iconv_open</b><i> (const char *<var>tocode</var>, const char *<var>fromcode</var>)</i></dt>
<dd><p>The <code>iconv_open</code> function has to be used before starting a
conversion. The two parameters this function takes determine the
source and destination character set for the conversion, and if the
implementation has the possibility to perform such a conversion, the
function returns a handle.
</p>
<p>If the wanted conversion is not available, the <code>iconv_open</code> function
returns <code>(iconv_t) -1</code>. In this case the global variable
<code>errno</code> can have the following values:
</p>
<dl compact="compact">
<dt> <code>EMFILE</code></dt>
<dd><p>The process already has <code>OPEN_MAX</code> file descriptors open.
</p></dd>
<dt> <code>ENFILE</code></dt>
<dd><p>The system limit of open file is reached.
</p></dd>
<dt> <code>ENOMEM</code></dt>
<dd><p>Not enough memory to carry out the operation.
</p></dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The conversion from <var>fromcode</var> to <var>tocode</var> is not supported.
</p></dd>
</dl>
<p>It is not possible to use the same descriptor in different threads to
perform independent conversions. The data structures associated
with the descriptor include information about the conversion state.
This must not be messed up by using it in different conversions.
</p>
<p>An <code>iconv</code> descriptor is like a file descriptor as for every use a
new descriptor must be created. The descriptor does not stand for all
of the conversions from <var>fromset</var> to <var>toset</var>.
</p>
<p>The GNU C library implementation of <code>iconv_open</code> has one
significant extension to other implementations. To ease the extension
of the set of available conversions, the implementation allows storing
the necessary files with data and code in an arbitrary number of
directories. How this extension must be written will be explained below
(see section <a href="#glibc-iconv-Implementation">The <code>iconv</code> Implementation in the GNU C library</a>). Here it is only important to say
that all directories mentioned in the <code>GCONV_PATH</code> environment
variable are considered only if they contain a file ‘<tt>gconv-modules</tt>’.
These directories need not necessarily be created by the system
administrator. In fact, this extension is introduced to help users
writing and using their own, new conversions. Of course, this does not
work for security reasons in SUID binaries; in this case only the system
directory is considered and this normally is
‘<tt><var>prefix</var>/lib/gconv</tt>’. The <code>GCONV_PATH</code> environment
variable is examined exactly once at the first call of the
<code>iconv_open</code> function. Later modifications of the variable have no
effect.
</p>
<a name="index-iconv_002eh"></a>
<p>The <code>iconv_open</code> function was introduced early in the X/Open
Portability Guide, version 2. It is supported by all commercial
Unices as it is required for the Unix branding. However, the quality and
completeness of the implementation varies widely. The <code>iconv_open</code>
function is declared in ‘<tt>iconv.h</tt>’.
</p></dd></dl>
<p>The <code>iconv</code> implementation can associate large data structure with
the handle returned by <code>iconv_open</code>. Therefore, it is crucial to
free all the resources once all conversions are carried out and the
conversion is not needed anymore.
</p>
<dl>
<dt><a name="index-iconv_005fclose"></a><u>Function:</u> int <b>iconv_close</b><i> (iconv_t <var>cd</var>)</i></dt>
<dd><p>The <code>iconv_close</code> function frees all resources associated with the
handle <var>cd</var>, which must have been returned by a successful call to
the <code>iconv_open</code> function.
</p>
<p>If the function call was successful the return value is <em>0</em>.
Otherwise it is <em>-1</em> and <code>errno</code> is set appropriately.
Defined error are:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The conversion descriptor is invalid.
</p></dd>
</dl>
<a name="index-iconv_002eh-1"></a>
<p>The <code>iconv_close</code> function was introduced together with the rest
of the <code>iconv</code> functions in XPG2 and is declared in ‘<tt>iconv.h</tt>’.
</p></dd></dl>
<p>The standard defines only one actual conversion function. This has,
therefore, the most general interface: it allows conversion from one
buffer to another. Conversion from a file to a buffer, vice versa, or
even file to file can be implemented on top of it.
</p>
<dl>
<dt><a name="index-iconv"></a><u>Function:</u> size_t <b>iconv</b><i> (iconv_t <var>cd</var>, char **<var>inbuf</var>, size_t *<var>inbytesleft</var>, char **<var>outbuf</var>, size_t *<var>outbytesleft</var>)</i></dt>
<dd><a name="index-stateful-3"></a>
<p>The <code>iconv</code> function converts the text in the input buffer
according to the rules associated with the descriptor <var>cd</var> and
stores the result in the output buffer. It is possible to call the
function for the same text several times in a row since for stateful
character sets the necessary state information is kept in the data
structures associated with the descriptor.
</p>
<p>The input buffer is specified by <code>*<var>inbuf</var></code> and it contains
<code>*<var>inbytesleft</var></code> bytes. The extra indirection is necessary for
communicating the used input back to the caller (see below). It is
important to note that the buffer pointer is of type <code>char</code> and the
length is measured in bytes even if the input text is encoded in wide
characters.
</p>
<p>The output buffer is specified in a similar way. <code>*<var>outbuf</var></code>
points to the beginning of the buffer with at least
<code>*<var>outbytesleft</var></code> bytes room for the result. The buffer
pointer again is of type <code>char</code> and the length is measured in
bytes. If <var>outbuf</var> or <code>*<var>outbuf</var></code> is a null pointer, the
conversion is performed but no output is available.
</p>
<p>If <var>inbuf</var> is a null pointer, the <code>iconv</code> function performs the
necessary action to put the state of the conversion into the initial
state. This is obviously a no-op for non-stateful encodings, but if the
encoding has a state, such a function call might put some byte sequences
in the output buffer, which perform the necessary state changes. The
next call with <var>inbuf</var> not being a null pointer then simply goes on
from the initial state. It is important that the programmer never makes
any assumption as to whether the conversion has to deal with states.
Even if the input and output character sets are not stateful, the
implementation might still have to keep states. This is due to the
implementation chosen for the GNU C library as it is described below.
Therefore an <code>iconv</code> call to reset the state should always be
performed if some protocol requires this for the output text.
</p>
<p>The conversion stops for one of three reasons. The first is that all
characters from the input buffer are converted. This actually can mean
two things: either all bytes from the input buffer are consumed or
there are some bytes at the end of the buffer that possibly can form a
complete character but the input is incomplete. The second reason for a
stop is that the output buffer is full. And the third reason is that
the input contains invalid characters.
</p>
<p>In all of these cases the buffer pointers after the last successful
conversion, for input and output buffer, are stored in <var>inbuf</var> and
<var>outbuf</var>, and the available room in each buffer is stored in
<var>inbytesleft</var> and <var>outbytesleft</var>.
</p>
<p>Since the character sets selected in the <code>iconv_open</code> call can be
almost arbitrary, there can be situations where the input buffer contains
valid characters, which have no identical representation in the output
character set. The behavior in this situation is undefined. The
<em>current</em> behavior of the GNU C library in this situation is to
return with an error immediately. This certainly is not the most
desirable solution; therefore, future versions will provide better ones,
but they are not yet finished.
</p>
<p>If all input from the input buffer is successfully converted and stored
in the output buffer, the function returns the number of non-reversible
conversions performed. In all other cases the return value is
<code>(size_t) -1</code> and <code>errno</code> is set appropriately. In such cases
the value pointed to by <var>inbytesleft</var> is nonzero.
</p>
<dl compact="compact">
<dt> <code>EILSEQ</code></dt>
<dd><p>The conversion stopped because of an invalid byte sequence in the input.
After the call, <code>*<var>inbuf</var></code> points at the first byte of the
invalid byte sequence.
</p>
</dd>
<dt> <code>E2BIG</code></dt>
<dd><p>The conversion stopped because it ran out of space in the output buffer.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The conversion stopped because of an incomplete byte sequence at the end
of the input buffer.
</p>
</dd>
<dt> <code>EBADF</code></dt>
<dd><p>The <var>cd</var> argument is invalid.
</p></dd>
</dl>
<a name="index-iconv_002eh-2"></a>
<p>The <code>iconv</code> function was introduced in the XPG2 standard and is
declared in the ‘<tt>iconv.h</tt>’ header.
</p></dd></dl>
<p>The definition of the <code>iconv</code> function is quite good overall. It
provides quite flexible functionality. The only problems lie in the
boundary cases, which are incomplete byte sequences at the end of the
input buffer and invalid input. A third problem, which is not really
a design problem, is the way conversions are selected. The standard
does not say anything about the legitimate names, a minimal set of
available conversions. We will see how this negatively impacts other
implementations, as demonstrated below.
</p>
<hr size="6">
<a name="iconv-Examples"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Generic-Conversion-Interface" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Other-iconv-Implementations" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Generic-Charset-Conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="A-complete-iconv-example"></a>
<h3 class="subsection">6.5.2 A complete <code>iconv</code> example</h3>
<p>The example below features a solution for a common problem. Given that
one knows the internal encoding used by the system for <code>wchar_t</code>
strings, one often is in the position to read text from a file and store
it in wide character buffers. One can do this using <code>mbsrtowcs</code>,
but then we run into the problems discussed above.
</p>
<table><tr><td> </td><td><pre class="smallexample">int
file2wcs (int fd, const char *charset, wchar_t *outbuf, size_t avail)
{
char inbuf[BUFSIZ];
size_t insize = 0;
char *wrptr = (char *) outbuf;
int result = 0;
iconv_t cd;
cd = iconv_open ("WCHAR_T", charset);
if (cd == (iconv_t) -1)
{
/* <span class="roman">Something went wrong.</span> */
if (errno == EINVAL)
error (0, 0, "conversion from '%s' to wchar_t not available",
charset);
else
perror ("iconv_open");
/* <span class="roman">Terminate the output string.</span> */
*outbuf = L'\0';
return -1;
}
while (avail > 0)
{
size_t nread;
size_t nconv;
char *inptr = inbuf;
/* <span class="roman">Read more input.</span> */
nread = read (fd, inbuf + insize, sizeof (inbuf) - insize);
if (nread == 0)
{
/* <span class="roman">When we come here the file is completely read.</span>
<span class="roman">This still could mean there are some unused</span>
<span class="roman">characters in the <code>inbuf</code>. Put them back.</span> */
if (lseek (fd, -insize, SEEK_CUR) == -1)
result = -1;
/* <span class="roman">Now write out the byte sequence to get into the</span>
<span class="roman">initial state if this is necessary.</span> */
iconv (cd, NULL, NULL, &wrptr, &avail);
break;
}
insize += nread;
/* <span class="roman">Do the conversion.</span> */
nconv = iconv (cd, &inptr, &insize, &wrptr, &avail);
if (nconv == (size_t) -1)
{
/* <span class="roman">Not everything went right. It might only be</span>
<span class="roman">an unfinished byte sequence at the end of the</span>
<span class="roman">buffer. Or it is a real problem.</span> */
if (errno == EINVAL)
/* <span class="roman">This is harmless. Simply move the unused</span>
<span class="roman">bytes to the beginning of the buffer so that</span>
<span class="roman">they can be used in the next round.</span> */
memmove (inbuf, inptr, insize);
else
{
/* <span class="roman">It is a real problem. Maybe we ran out of</span>
<span class="roman">space in the output buffer or we have invalid</span>
<span class="roman">input. In any case back the file pointer to</span>
<span class="roman">the position of the last processed byte.</span> */
lseek (fd, -insize, SEEK_CUR);
result = -1;
break;
}
}
}
/* <span class="roman">Terminate the output string.</span> */
if (avail >= sizeof (wchar_t))
*((wchar_t *) wrptr) = L'\0';
if (iconv_close (cd) != 0)
perror ("iconv_close");
return (wchar_t *) wrptr - outbuf;
}
</pre></td></tr></table>
<a name="index-stateful-4"></a>
<p>This example shows the most important aspects of using the <code>iconv</code>
functions. It shows how successive calls to <code>iconv</code> can be used to
convert large amounts of text. The user does not have to care about
stateful encodings as the functions take care of everything.
</p>
<p>An interesting point is the case where <code>iconv</code> returns an error and
<code>errno</code> is set to <code>EINVAL</code>. This is not really an error in the
transformation. It can happen whenever the input character set contains
byte sequences of more than one byte for some character and texts are not
processed in one piece. In this case there is a chance that a multibyte
sequence is cut. The caller can then simply read the remainder of the
takes and feed the offending bytes together with new character from the
input to <code>iconv</code> and continue the work. The internal state kept in
the descriptor is <em>not</em> unspecified after such an event as is the
case with the conversion functions from the ISO C standard.
</p>
<p>The example also shows the problem of using wide character strings with
<code>iconv</code>. As explained in the description of the <code>iconv</code>
function above, the function always takes a pointer to a <code>char</code>
array and the available space is measured in bytes. In the example, the
output buffer is a wide character buffer; therefore, we use a local
variable <var>wrptr</var> of type <code>char *</code>, which is used in the
<code>iconv</code> calls.
</p>
<p>This looks rather innocent but can lead to problems on platforms that
have tight restriction on alignment. Therefore the caller of <code>iconv</code>
has to make sure that the pointers passed are suitable for access of
characters from the appropriate character set. Since, in the
above case, the input parameter to the function is a <code>wchar_t</code>
pointer, this is the case (unless the user violates alignment when
computing the parameter). But in other situations, especially when
writing generic functions where one does not know what type of character
set one uses and, therefore, treats text as a sequence of bytes, it might
become tricky.
</p>
<hr size="6">
<a name="Other-iconv-Implementations"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#iconv-Examples" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#glibc-iconv-Implementation" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Generic-Charset-Conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Some-Details-about-other-iconv-Implementations"></a>
<h3 class="subsection">6.5.3 Some Details about other <code>iconv</code> Implementations</h3>
<p>This is not really the place to discuss the <code>iconv</code> implementation
of other systems but it is necessary to know a bit about them to write
portable programs. The above mentioned problems with the specification
of the <code>iconv</code> functions can lead to portability issues.
</p>
<p>The first thing to notice is that, due to the large number of character
sets in use, it is certainly not practical to encode the conversions
directly in the C library. Therefore, the conversion information must
come from files outside the C library. This is usually done in one or
both of the following ways:
</p>
<ul>
<li>
The C library contains a set of generic conversion functions that can
read the needed conversion tables and other information from data files.
These files get loaded when necessary.
<p>This solution is problematic as it requires a great deal of effort to
apply to all character sets (potentially an infinite set). The
differences in the structure of the different character sets is so large
that many different variants of the table-processing functions must be
developed. In addition, the generic nature of these functions make them
slower than specifically implemented functions.
</p>
</li><li>
The C library only contains a framework that can dynamically load
object files and execute the conversion functions contained therein.
<p>This solution provides much more flexibility. The C library itself
contains only very little code and therefore reduces the general memory
footprint. Also, with a documented interface between the C library and
the loadable modules it is possible for third parties to extend the set
of available conversion modules. A drawback of this solution is that
dynamic loading must be available.
</p></li></ul>
<p>Some implementations in commercial Unices implement a mixture of these
possibilities; the majority implement only the second solution. Using
loadable modules moves the code out of the library itself and keeps
the door open for extensions and improvements, but this design is also
limiting on some platforms since not many platforms support dynamic
loading in statically linked programs. On platforms without this
capability it is therefore not possible to use this interface in
statically linked programs. The GNU C library has, on ELF platforms, no
problems with dynamic loading in these situations; therefore, this
point is moot. The danger is that one gets acquainted with this
situation and forgets about the restrictions on other systems.
</p>
<p>A second thing to know about other <code>iconv</code> implementations is that
the number of available conversions is often very limited. Some
implementations provide, in the standard release (not special
international or developer releases), at most 100 to 200 conversion
possibilities. This does not mean 200 different character sets are
supported; for example, conversions from one character set to a set of 10
others might count as 10 conversions. Together with the other direction
this makes 20 conversion possibilities used up by one character set. One
can imagine the thin coverage these platform provide. Some Unix vendors
even provide only a handful of conversions, which renders them useless for
almost all uses.
</p>
<p>This directly leads to a third and probably the most problematic point.
The way the <code>iconv</code> conversion functions are implemented on all
known Unix systems and the availability of the conversion functions from
character set <em>A</em> to <em>B</em> and the conversion from
<em>B</em> to <em>C</em> does <em>not</em> imply that the
conversion from <em>A</em> to <em>C</em> is available.
</p>
<p>This might not seem unreasonable and problematic at first, but it is a
quite big problem as one will notice shortly after hitting it. To show
the problem we assume to write a program that has to convert from
<em>A</em> to <em>C</em>. A call like
</p>
<table><tr><td> </td><td><pre class="smallexample">cd = iconv_open ("<em>C</em>", "<em>A</em>");
</pre></td></tr></table>
<p>fails according to the assumption above. But what does the program
do now? The conversion is necessary; therefore, simply giving up is not
an option.
</p>
<p>This is a nuisance. The <code>iconv</code> function should take care of this.
But how should the program proceed from here on? If it tries to convert
to character set <em>B</em>, first the two <code>iconv_open</code>
calls
</p>
<table><tr><td> </td><td><pre class="smallexample">cd1 = iconv_open ("<em>B</em>", "<em>A</em>");
</pre></td></tr></table>
<p>and
</p>
<table><tr><td> </td><td><pre class="smallexample">cd2 = iconv_open ("<em>C</em>", "<em>B</em>");
</pre></td></tr></table>
<p>will succeed, but how to find <em>B</em>?
</p>
<p>Unfortunately, the answer is: there is no general solution. On some
systems guessing might help. On those systems most character sets can
convert to and from UTF-8 encoded ISO 10646 or Unicode text. Beside
this only some very system-specific methods can help. Since the
conversion functions come from loadable modules and these modules must
be stored somewhere in the filesystem, one <em>could</em> try to find them
and determine from the available file which conversions are available
and whether there is an indirect route from <em>A</em> to
<em>C</em>.
</p>
<p>This example shows one of the design errors of <code>iconv</code> mentioned
above. It should at least be possible to determine the list of available
conversion programmatically so that if <code>iconv_open</code> says there is no
such conversion, one could make sure this also is true for indirect
routes.
</p>
<hr size="6">
<a name="glibc-iconv-Implementation"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Other-iconv-Implementations" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Format-of-gconv_002dmodules-files" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Generic-Charset-Conversion" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="The-iconv-Implementation-in-the-GNU-C-library"></a>
<h3 class="subsection">6.5.4 The <code>iconv</code> Implementation in the GNU C library</h3>
<p>After reading about the problems of <code>iconv</code> implementations in the
last section it is certainly good to note that the implementation in
the GNU C library has none of the problems mentioned above. What
follows is a step-by-step analysis of the points raised above. The
evaluation is based on the current state of the development (as of
January 1999). The development of the <code>iconv</code> functions is not
complete, but basic functionality has solidified.
</p>
<p>The GNU C library’s <code>iconv</code> implementation uses shared loadable
modules to implement the conversions. A very small number of
conversions are built into the library itself but these are only rather
trivial conversions.
</p>
<p>All the benefits of loadable modules are available in the GNU C library
implementation. This is especially appealing since the interface is
well documented (see below), and it, therefore, is easy to write new
conversion modules. The drawback of using loadable objects is not a
problem in the GNU C library, at least on ELF systems. Since the
library is able to load shared objects even in statically linked
binaries, static linking need not be forbidden in case one wants to use
<code>iconv</code>.
</p>
<p>The second mentioned problem is the number of supported conversions.
Currently, the GNU C library supports more than 150 character sets. The
way the implementation is designed the number of supported conversions
is greater than 22350 (<em>150</em> times <em>149</em>). If any conversion
from or to a character set is missing, it can be added easily.
</p>
<p>Particularly impressive as it may be, this high number is due to the
fact that the GNU C library implementation of <code>iconv</code> does not have
the third problem mentioned above (i.e., whenever there is a conversion
from a character set <em>A</em> to <em>B</em> and from
<em>B</em> to <em>C</em> it is always possible to convert from
<em>A</em> to <em>C</em> directly). If the <code>iconv_open</code>
returns an error and sets <code>errno</code> to <code>EINVAL</code>, there is no
known way, directly or indirectly, to perform the wanted conversion.
</p>
<a name="index-triangulation"></a>
<p>Triangulation is achieved by providing for each character set a
conversion from and to UCS-4 encoded ISO 10646. Using ISO 10646
as an intermediate representation it is possible to <em>triangulate</em>
(i.e., convert with an intermediate representation).
</p>
<p>There is no inherent requirement to provide a conversion to ISO
10646 for a new character set, and it is also possible to provide other
conversions where neither source nor destination character set is ISO
10646. The existing set of conversions is simply meant to cover all
conversions that might be of interest.
</p>
<a name="index-ISO_002d2022_002dJP"></a>
<a name="index-EUC_002dJP"></a>
<p>All currently available conversions use the triangulation method above,
making conversion run unnecessarily slow. If, for example, somebody
often needs the conversion from ISO-2022-JP to EUC-JP, a quicker solution
would involve direct conversion between the two character sets, skipping
the input to ISO 10646 first. The two character sets of interest
are much more similar to each other than to ISO 10646.
</p>
<p>In such a situation one easily can write a new conversion and provide it
as a better alternative. The GNU C library <code>iconv</code> implementation
would automatically use the module implementing the conversion if it is
specified to be more efficient.
</p>
<hr size="6">
<a name="Format-of-gconv_002dmodules-files"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#glibc-iconv-Implementation" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Finding-the-conversion-path-in-iconv" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#glibc-iconv-Implementation" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">6.5.4.1 Format of ‘<tt>gconv-modules</tt>’ files</h4>
<p>All information about the available conversions comes from a file named
‘<tt>gconv-modules</tt>’, which can be found in any of the directories along
the <code>GCONV_PATH</code>. The ‘<tt>gconv-modules</tt>’ files are line-oriented
text files, where each of the lines has one of the following formats:
</p>
<ul>
<li>
If the first non-whitespace character is a <kbd>#</kbd> the line contains only
comments and is ignored.
</li><li>
Lines starting with <code>alias</code> define an alias name for a character
set. Two more words are expected on the line. The first word
defines the alias name, and the second defines the original name of the
character set. The effect is that it is possible to use the alias name
in the <var>fromset</var> or <var>toset</var> parameters of <code>iconv_open</code> and
achieve the same result as when using the real character set name.
<p>This is quite important as a character set has often many different
names. There is normally an official name but this need not correspond to
the most popular name. Beside this many character sets have special
names that are somehow constructed. For example, all character sets
specified by the ISO have an alias of the form <code>ISO-IR-<var>nnn</var></code>
where <var>nnn</var> is the registration number. This allows programs that
know about the registration number to construct character set names and
use them in <code>iconv_open</code> calls. More on the available names and
aliases follows below.
</p>
</li><li>
Lines starting with <code>module</code> introduce an available conversion
module. These lines must contain three or four more words.
<p>The first word specifies the source character set, the second word the
destination character set of conversion implemented in this module, and
the third word is the name of the loadable module. The filename is
constructed by appending the usual shared object suffix (normally
‘<tt>.so</tt>’) and this file is then supposed to be found in the same
directory the ‘<tt>gconv-modules</tt>’ file is in. The last word on the line,
which is optional, is a numeric value representing the cost of the
conversion. If this word is missing, a cost of <em>1</em> is assumed. The
numeric value itself does not matter that much; what counts are the
relative values of the sums of costs for all possible conversion paths.
Below is a more precise description of the use of the cost value.
</p></li></ul>
<p>Returning to the example above where one has written a module to directly
convert from ISO-2022-JP to EUC-JP and back. All that has to be done is
to put the new module, let its name be ISO2022JP-EUCJP.so, in a directory
and add a file ‘<tt>gconv-modules</tt>’ with the following content in the
same directory:
</p>
<table><tr><td> </td><td><pre class="smallexample">module ISO-2022-JP// EUC-JP// ISO2022JP-EUCJP 1
module EUC-JP// ISO-2022-JP// ISO2022JP-EUCJP 1
</pre></td></tr></table>
<p>To see why this is sufficient, it is necessary to understand how the
conversion used by <code>iconv</code> (and described in the descriptor) is
selected. The approach to this problem is quite simple.
</p>
<p>At the first call of the <code>iconv_open</code> function the program reads
all available ‘<tt>gconv-modules</tt>’ files and builds up two tables: one
containing all the known aliases and another that contains the
information about the conversions and which shared object implements
them.
</p>
<hr size="6">
<a name="Finding-the-conversion-path-in-iconv"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Format-of-gconv_002dmodules-files" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#iconv-module-data-structures" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#glibc-iconv-Implementation" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">6.5.4.2 Finding the conversion path in <code>iconv</code></h4>
<p>The set of available conversions form a directed graph with weighted
edges. The weights on the edges are the costs specified in the
‘<tt>gconv-modules</tt>’ files. The <code>iconv_open</code> function uses an
algorithm suitable for search for the best path in such a graph and so
constructs a list of conversions that must be performed in succession
to get the transformation from the source to the destination character
set.
</p>
<p>Explaining why the above ‘<tt>gconv-modules</tt>’ files allows the
<code>iconv</code> implementation to resolve the specific ISO-2022-JP to
EUC-JP conversion module instead of the conversion coming with the
library itself is straightforward. Since the latter conversion takes two
steps (from ISO-2022-JP to ISO 10646 and then from ISO 10646 to
EUC-JP), the cost is <em>1+1 = 2</em>. The above ‘<tt>gconv-modules</tt>’
file, however, specifies that the new conversion modules can perform this
conversion with only the cost of <em>1</em>.
</p>
<p>A mysterious item about the ‘<tt>gconv-modules</tt>’ file above (and also
the file coming with the GNU C library) are the names of the character
sets specified in the <code>module</code> lines. Why do almost all the names
end in <code>//</code>? And this is not all: the names can actually be
regular expressions. At this point in time this mystery should not be
revealed, unless you have the relevant spell-casting materials: ashes
from an original DOS 6.2 boot disk burnt in effigy, a crucifix
blessed by St. Emacs, assorted herbal roots from Central America, sand
from Cebu, etc. Sorry! <strong>The part of the implementation where
this is used is not yet finished. For now please simply follow the
existing examples. It’ll become clearer once it is. –drepper</strong>
</p>
<p>A last remark about the ‘<tt>gconv-modules</tt>’ is about the names not
ending with <code>//</code>. A character set named <code>INTERNAL</code> is often
mentioned. From the discussion above and the chosen name it should have
become clear that this is the name for the representation used in the
intermediate step of the triangulation. We have said that this is UCS-4
but actually that is not quite right. The UCS-4 specification also
includes the specification of the byte ordering used. Since a UCS-4 value
consists of four bytes, a stored value is effected by byte ordering. The
internal representation is <em>not</em> the same as UCS-4 in case the byte
ordering of the processor (or at least the running process) is not the
same as the one required for UCS-4. This is done for performance reasons
as one does not want to perform unnecessary byte-swapping operations if
one is not interested in actually seeing the result in UCS-4. To avoid
trouble with endianness, the internal representation consistently is named
<code>INTERNAL</code> even on big-endian systems where the representations are
identical.
</p>
<hr size="6">
<a name="iconv-module-data-structures"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Finding-the-conversion-path-in-iconv" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#iconv-module-interfaces" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#glibc-iconv-Implementation" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">6.5.4.3 <code>iconv</code> module data structures</h4>
<p>So far this section has described how modules are located and considered
to be used. What remains to be described is the interface of the modules
so that one can write new ones. This section describes the interface as
it is in use in January 1999. The interface will change a bit in the
future but, with luck, only in an upwardly compatible way.
</p>
<p>The definitions necessary to write new modules are publicly available
in the non-standard header ‘<tt>gconv.h</tt>’. The following text,
therefore, describes the definitions from this header file. First,
however, it is necessary to get an overview.
</p>
<p>From the perspective of the user of <code>iconv</code> the interface is quite
simple: the <code>iconv_open</code> function returns a handle that can be used
in calls to <code>iconv</code>, and finally the handle is freed with a call to
<code>iconv_close</code>. The problem is that the handle has to be able to
represent the possibly long sequences of conversion steps and also the
state of each conversion since the handle is all that is passed to the
<code>iconv</code> function. Therefore, the data structures are really the
elements necessary to understanding the implementation.
</p>
<p>We need two different kinds of data structures. The first describes the
conversion and the second describes the state etc. There are really two
type definitions like this in ‘<tt>gconv.h</tt>’.
<a name="index-gconv_002eh"></a>
</p>
<dl>
<dt><a name="index-struct-_005f_005fgconv_005fstep"></a><u>Data type:</u> <b>struct __gconv_step</b></dt>
<dd><p>This data structure describes one conversion a module can perform. For
each function in a loaded module with conversion functions there is
exactly one object of this type. This object is shared by all users of
the conversion (i.e., this object does not contain any information
corresponding to an actual conversion; it only describes the conversion
itself).
</p>
<dl compact="compact">
<dt> <code>struct __gconv_loaded_object *__shlib_handle</code></dt>
<dt> <code>const char *__modname</code></dt>
<dt> <code>int __counter</code></dt>
<dd><p>All these elements of the structure are used internally in the C library
to coordinate loading and unloading the shared. One must not expect any
of the other elements to be available or initialized.
</p>
</dd>
<dt> <code>const char *__from_name</code></dt>
<dt> <code>const char *__to_name</code></dt>
<dd><p><code>__from_name</code> and <code>__to_name</code> contain the names of the source and
destination character sets. They can be used to identify the actual
conversion to be carried out since one module might implement conversions
for more than one character set and/or direction.
</p>
</dd>
<dt> <code>gconv_fct __fct</code></dt>
<dt> <code>gconv_init_fct __init_fct</code></dt>
<dt> <code>gconv_end_fct __end_fct</code></dt>
<dd><p>These elements contain pointers to the functions in the loadable module.
The interface will be explained below.
</p>
</dd>
<dt> <code>int __min_needed_from</code></dt>
<dt> <code>int __max_needed_from</code></dt>
<dt> <code>int __min_needed_to</code></dt>
<dt> <code>int __max_needed_to;</code></dt>
<dd><p>These values have to be supplied in the init function of the module. The
<code>__min_needed_from</code> value specifies how many bytes a character of
the source character set at least needs. The <code>__max_needed_from</code>
specifies the maximum value that also includes possible shift sequences.
</p>
<p>The <code>__min_needed_to</code> and <code>__max_needed_to</code> values serve the
same purpose as <code>__min_needed_from</code> and <code>__max_needed_from</code> but
this time for the destination character set.
</p>
<p>It is crucial that these values be accurate since otherwise the
conversion functions will have problems or not work at all.
</p>
</dd>
<dt> <code>int __stateful</code></dt>
<dd><p>This element must also be initialized by the init function.
<code>int __stateful</code> is nonzero if the source character set is stateful.
Otherwise it is zero.
</p>
</dd>
<dt> <code>void *__data</code></dt>
<dd><p>This element can be used freely by the conversion functions in the
module. <code>void *__data</code> can be used to communicate extra information
from one call to another. <code>void *__data</code> need not be initialized if
not needed at all. If <code>void *__data</code> element is assigned a pointer
to dynamically allocated memory (presumably in the init function) it has
to be made sure that the end function deallocates the memory. Otherwise
the application will leak memory.
</p>
<p>It is important to be aware that this data structure is shared by all
users of this specification conversion and therefore the <code>__data</code>
element must not contain data specific to one specific use of the
conversion function.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-struct-_005f_005fgconv_005fstep_005fdata"></a><u>Data type:</u> <b>struct __gconv_step_data</b></dt>
<dd><p>This is the data structure that contains the information specific to
each use of the conversion functions.
</p>
<dl compact="compact">
<dt> <code>char *__outbuf</code></dt>
<dt> <code>char *__outbufend</code></dt>
<dd><p>These elements specify the output buffer for the conversion step. The
<code>__outbuf</code> element points to the beginning of the buffer, and
<code>__outbufend</code> points to the byte following the last byte in the
buffer. The conversion function must not assume anything about the size
of the buffer but it can be safely assumed the there is room for at
least one complete character in the output buffer.
</p>
<p>Once the conversion is finished, if the conversion is the last step, the
<code>__outbuf</code> element must be modified to point after the last byte
written into the buffer to signal how much output is available. If this
conversion step is not the last one, the element must not be modified.
The <code>__outbufend</code> element must not be modified.
</p>
</dd>
<dt> <code>int __is_last</code></dt>
<dd><p>This element is nonzero if this conversion step is the last one. This
information is necessary for the recursion. See the description of the
conversion function internals below. This element must never be
modified.
</p>
</dd>
<dt> <code>int __invocation_counter</code></dt>
<dd><p>The conversion function can use this element to see how many calls of
the conversion function already happened. Some character sets require a
certain prolog when generating output, and by comparing this value with
zero, one can find out whether it is the first call and whether,
therefore, the prolog should be emitted. This element must never be
modified.
</p>
</dd>
<dt> <code>int __internal_use</code></dt>
<dd><p>This element is another one rarely used but needed in certain
situations. It is assigned a nonzero value in case the conversion
functions are used to implement <code>mbsrtowcs</code> et.al. (i.e., the
function is not used directly through the <code>iconv</code> interface).
</p>
<p>This sometimes makes a difference as it is expected that the
<code>iconv</code> functions are used to translate entire texts while the
<code>mbsrtowcs</code> functions are normally used only to convert single
strings and might be used multiple times to convert entire texts.
</p>
<p>But in this situation we would have problem complying with some rules of
the character set specification. Some character sets require a prolog,
which must appear exactly once for an entire text. If a number of
<code>mbsrtowcs</code> calls are used to convert the text, only the first call
must add the prolog. However, because there is no communication between the
different calls of <code>mbsrtowcs</code>, the conversion functions have no
possibility to find this out. The situation is different for sequences
of <code>iconv</code> calls since the handle allows access to the needed
information.
</p>
<p>The <code>int __internal_use</code> element is mostly used together with
<code>__invocation_counter</code> as follows:
</p>
<table><tr><td> </td><td><pre class="smallexample">if (!data->__internal_use
&& data->__invocation_counter == 0)
/* <span class="roman">Emit prolog.</span> */
…
</pre></td></tr></table>
<p>This element must never be modified.
</p>
</dd>
<dt> <code>mbstate_t *__statep</code></dt>
<dd><p>The <code>__statep</code> element points to an object of type <code>mbstate_t</code>
(see section <a href="#Keeping-the-state">Representing the state of the conversion</a>). The conversion of a stateful character
set must use the object pointed to by <code>__statep</code> to store
information about the conversion state. The <code>__statep</code> element
itself must never be modified.
</p>
</dd>
<dt> <code>mbstate_t __state</code></dt>
<dd><p>This element must <em>never</em> be used directly. It is only part of
this structure to have the needed space allocated.
</p></dd>
</dl>
</dd></dl>
<hr size="6">
<a name="iconv-module-interfaces"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#iconv-module-data-structures" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#glibc-iconv-Implementation" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">6.5.4.4 <code>iconv</code> module interfaces</h4>
<p>With the knowledge about the data structures we now can describe the
conversion function itself. To understand the interface a bit of
knowledge is necessary about the functionality in the C library that
loads the objects with the conversions.
</p>
<p>It is often the case that one conversion is used more than once (i.e.,
there are several <code>iconv_open</code> calls for the same set of character
sets during one program run). The <code>mbsrtowcs</code> et.al. functions in
the GNU C library also use the <code>iconv</code> functionality, which
increases the number of uses of the same functions even more.
</p>
<p>Because of this multiple use of conversions, the modules do not get
loaded exclusively for one conversion. Instead a module once loaded can
be used by an arbitrary number of <code>iconv</code> or <code>mbsrtowcs</code> calls
at the same time. The splitting of the information between conversion-
function-specific information and conversion data makes this possible.
The last section showed the two data structures used to do this.
</p>
<p>This is of course also reflected in the interface and semantics of the
functions that the modules must provide. There are three functions that
must have the following names:
</p>
<dl compact="compact">
<dt> <code>gconv_init</code></dt>
<dd><p>The <code>gconv_init</code> function initializes the conversion function
specific data structure. This very same object is shared by all
conversions that use this conversion and, therefore, no state information
about the conversion itself must be stored in here. If a module
implements more than one conversion, the <code>gconv_init</code> function will
be called multiple times.
</p>
</dd>
<dt> <code>gconv_end</code></dt>
<dd><p>The <code>gconv_end</code> function is responsible for freeing all resources
allocated by the <code>gconv_init</code> function. If there is nothing to do,
this function can be missing. Special care must be taken if the module
implements more than one conversion and the <code>gconv_init</code> function
does not allocate the same resources for all conversions.
</p>
</dd>
<dt> <code>gconv</code></dt>
<dd><p>This is the actual conversion function. It is called to convert one
block of text. It gets passed the conversion step information
initialized by <code>gconv_init</code> and the conversion data, specific to
this use of the conversion functions.
</p></dd>
</dl>
<p>There are three data types defined for the three module interface
functions and these define the interface.
</p>
<dl>
<dt><a name="index-_0028_002a_005f_005fgconv_005finit_005ffct_0029"></a><u>Data type:</u> int <b>(*__gconv_init_fct)</b><i> (struct __gconv_step *)</i></dt>
<dd><p>This specifies the interface of the initialization function of the
module. It is called exactly once for each conversion the module
implements.
</p>
<p>As explained in the description of the <code>struct __gconv_step</code> data
structure above the initialization function has to initialize parts of
it.
</p>
<dl compact="compact">
<dt> <code>__min_needed_from</code></dt>
<dt> <code>__max_needed_from</code></dt>
<dt> <code>__min_needed_to</code></dt>
<dt> <code>__max_needed_to</code></dt>
<dd><p>These elements must be initialized to the exact numbers of the minimum
and maximum number of bytes used by one character in the source and
destination character sets, respectively. If the characters all have the
same size, the minimum and maximum values are the same.
</p>
</dd>
<dt> <code>__stateful</code></dt>
<dd><p>This element must be initialized to an nonzero value if the source
character set is stateful. Otherwise it must be zero.
</p></dd>
</dl>
<p>If the initialization function needs to communicate some information
to the conversion function, this communication can happen using the
<code>__data</code> element of the <code>__gconv_step</code> structure. But since
this data is shared by all the conversions, it must not be modified by
the conversion function. The example below shows how this can be used.
</p>
<table><tr><td> </td><td><pre class="smallexample">#define MIN_NEEDED_FROM 1
#define MAX_NEEDED_FROM 4
#define MIN_NEEDED_TO 4
#define MAX_NEEDED_TO 4
int
gconv_init (struct __gconv_step *step)
{
/* <span class="roman">Determine which direction.</span> */
struct iso2022jp_data *new_data;
enum direction dir = illegal_dir;
enum variant var = illegal_var;
int result;
if (__strcasecmp (step->__from_name, "ISO-2022-JP//") == 0)
{
dir = from_iso2022jp;
var = iso2022jp;
}
else if (__strcasecmp (step->__to_name, "ISO-2022-JP//") == 0)
{
dir = to_iso2022jp;
var = iso2022jp;
}
else if (__strcasecmp (step->__from_name, "ISO-2022-JP-2//") == 0)
{
dir = from_iso2022jp;
var = iso2022jp2;
}
else if (__strcasecmp (step->__to_name, "ISO-2022-JP-2//") == 0)
{
dir = to_iso2022jp;
var = iso2022jp2;
}
result = __GCONV_NOCONV;
if (dir != illegal_dir)
{
new_data = (struct iso2022jp_data *)
malloc (sizeof (struct iso2022jp_data));
result = __GCONV_NOMEM;
if (new_data != NULL)
{
new_data->dir = dir;
new_data->var = var;
step->__data = new_data;
if (dir == from_iso2022jp)
{
step->__min_needed_from = MIN_NEEDED_FROM;
step->__max_needed_from = MAX_NEEDED_FROM;
step->__min_needed_to = MIN_NEEDED_TO;
step->__max_needed_to = MAX_NEEDED_TO;
}
else
{
step->__min_needed_from = MIN_NEEDED_TO;
step->__max_needed_from = MAX_NEEDED_TO;
step->__min_needed_to = MIN_NEEDED_FROM;
step->__max_needed_to = MAX_NEEDED_FROM + 2;
}
/* <span class="roman">Yes, this is a stateful encoding.</span> */
step->__stateful = 1;
result = __GCONV_OK;
}
}
return result;
}
</pre></td></tr></table>
<p>The function first checks which conversion is wanted. The module from
which this function is taken implements four different conversions;
which one is selected can be determined by comparing the names. The
comparison should always be done without paying attention to the case.
</p>
<p>Next, a data structure, which contains the necessary information about
which conversion is selected, is allocated. The data structure
<code>struct iso2022jp_data</code> is locally defined since, outside the
module, this data is not used at all. Please note that if all four
conversions this modules supports are requested there are four data
blocks.
</p>
<p>One interesting thing is the initialization of the <code>__min_</code> and
<code>__max_</code> elements of the step data object. A single ISO-2022-JP
character can consist of one to four bytes. Therefore the
<code>MIN_NEEDED_FROM</code> and <code>MAX_NEEDED_FROM</code> macros are defined
this way. The output is always the <code>INTERNAL</code> character set (aka
UCS-4) and therefore each character consists of exactly four bytes. For
the conversion from <code>INTERNAL</code> to ISO-2022-JP we have to take into
account that escape sequences might be necessary to switch the character
sets. Therefore the <code>__max_needed_to</code> element for this direction
gets assigned <code>MAX_NEEDED_FROM + 2</code>. This takes into account the
two bytes needed for the escape sequences to single the switching. The
asymmetry in the maximum values for the two directions can be explained
easily: when reading ISO-2022-JP text, escape sequences can be handled
alone (i.e., it is not necessary to process a real character since the
effect of the escape sequence can be recorded in the state information).
The situation is different for the other direction. Since it is in
general not known which character comes next, one cannot emit escape
sequences to change the state in advance. This means the escape
sequences that have to be emitted together with the next character.
Therefore one needs more room than only for the character itself.
</p>
<p>The possible return values of the initialization function are:
</p>
<dl compact="compact">
<dt> <code>__GCONV_OK</code></dt>
<dd><p>The initialization succeeded
</p></dd>
<dt> <code>__GCONV_NOCONV</code></dt>
<dd><p>The requested conversion is not supported in the module. This can
happen if the ‘<tt>gconv-modules</tt>’ file has errors.
</p></dd>
<dt> <code>__GCONV_NOMEM</code></dt>
<dd><p>Memory required to store additional information could not be allocated.
</p></dd>
</dl>
</dd></dl>
<p>The function called before the module is unloaded is significantly
easier. It often has nothing at all to do; in which case it can be left
out completely.
</p>
<dl>
<dt><a name="index-_0028_002a_005f_005fgconv_005fend_005ffct_0029"></a><u>Data type:</u> void <b>(*__gconv_end_fct)</b><i> (struct gconv_step *)</i></dt>
<dd><p>The task of this function is to free all resources allocated in the
initialization function. Therefore only the <code>__data</code> element of
the object pointed to by the argument is of interest. Continuing the
example from the initialization function, the finalization function
looks like this:
</p>
<table><tr><td> </td><td><pre class="smallexample">void
gconv_end (struct __gconv_step *data)
{
free (data->__data);
}
</pre></td></tr></table>
</dd></dl>
<p>The most important function is the conversion function itself, which can
get quite complicated for complex character sets. But since this is not
of interest here, we will only describe a possible skeleton for the
conversion function.
</p>
<dl>
<dt><a name="index-_0028_002a_005f_005fgconv_005ffct_0029"></a><u>Data type:</u> int <b>(*__gconv_fct)</b><i> (struct __gconv_step *, struct __gconv_step_data *, const char **, const char *, size_t *, int)</i></dt>
<dd><p>The conversion function can be called for two basic reason: to convert
text or to reset the state. From the description of the <code>iconv</code>
function it can be seen why the flushing mode is necessary. What mode
is selected is determined by the sixth argument, an integer. This
argument being nonzero means that flushing is selected.
</p>
<p>Common to both modes is where the output buffer can be found. The
information about this buffer is stored in the conversion step data. A
pointer to this information is passed as the second argument to this
function. The description of the <code>struct __gconv_step_data</code>
structure has more information on the conversion step data.
</p>
<a name="index-stateful-5"></a>
<p>What has to be done for flushing depends on the source character set.
If the source character set is not stateful, nothing has to be done.
Otherwise the function has to emit a byte sequence to bring the state
object into the initial state. Once this all happened the other
conversion modules in the chain of conversions have to get the same
chance. Whether another step follows can be determined from the
<code>__is_last</code> element of the step data structure to which the first
parameter points.
</p>
<p>The more interesting mode is when actual text has to be converted. The
first step in this case is to convert as much text as possible from the
input buffer and store the result in the output buffer. The start of the
input buffer is determined by the third argument, which is a pointer to a
pointer variable referencing the beginning of the buffer. The fourth
argument is a pointer to the byte right after the last byte in the buffer.
</p>
<p>The conversion has to be performed according to the current state if the
character set is stateful. The state is stored in an object pointed to
by the <code>__statep</code> element of the step data (second argument). Once
either the input buffer is empty or the output buffer is full the
conversion stops. At this point, the pointer variable referenced by the
third parameter must point to the byte following the last processed
byte (i.e., if all of the input is consumed, this pointer and the fourth
parameter have the same value).
</p>
<p>What now happens depends on whether this step is the last one. If it is
the last step, the only thing that has to be done is to update the
<code>__outbuf</code> element of the step data structure to point after the
last written byte. This update gives the caller the information on how
much text is available in the output buffer. In addition, the variable
pointed to by the fifth parameter, which is of type <code>size_t</code>, must
be incremented by the number of characters (<em>not bytes</em>) that were
converted in a non-reversible way. Then, the function can return.
</p>
<p>In case the step is not the last one, the later conversion functions have
to get a chance to do their work. Therefore, the appropriate conversion
function has to be called. The information about the functions is
stored in the conversion data structures, passed as the first parameter.
This information and the step data are stored in arrays, so the next
element in both cases can be found by simple pointer arithmetic:
</p>
<table><tr><td> </td><td><pre class="smallexample">int
gconv (struct __gconv_step *step, struct __gconv_step_data *data,
const char **inbuf, const char *inbufend, size_t *written,
int do_flush)
{
struct __gconv_step *next_step = step + 1;
struct __gconv_step_data *next_data = data + 1;
…
</pre></td></tr></table>
<p>The <code>next_step</code> pointer references the next step information and
<code>next_data</code> the next data record. The call of the next function
therefore will look similar to this:
</p>
<table><tr><td> </td><td><pre class="smallexample"> next_step->__fct (next_step, next_data, &outerr, outbuf,
written, 0)
</pre></td></tr></table>
<p>But this is not yet all. Once the function call returns the conversion
function might have some more to do. If the return value of the function
is <code>__GCONV_EMPTY_INPUT</code>, more room is available in the output
buffer. Unless the input buffer is empty the conversion, functions start
all over again and process the rest of the input buffer. If the return
value is not <code>__GCONV_EMPTY_INPUT</code>, something went wrong and we have
to recover from this.
</p>
<p>A requirement for the conversion function is that the input buffer
pointer (the third argument) always point to the last character that
was put in converted form into the output buffer. This is trivially
true after the conversion performed in the current step, but if the
conversion functions deeper downstream stop prematurely, not all
characters from the output buffer are consumed and, therefore, the input
buffer pointers must be backed off to the right position.
</p>
<p>Correcting the input buffers is easy to do if the input and output
character sets have a fixed width for all characters. In this situation
we can compute how many characters are left in the output buffer and,
therefore, can correct the input buffer pointer appropriately with a
similar computation. Things are getting tricky if either character set
has characters represented with variable length byte sequences, and it
gets even more complicated if the conversion has to take care of the
state. In these cases the conversion has to be performed once again, from
the known state before the initial conversion (i.e., if necessary the
state of the conversion has to be reset and the conversion loop has to be
executed again). The difference now is that it is known how much input
must be created, and the conversion can stop before converting the first
unused character. Once this is done the input buffer pointers must be
updated again and the function can return.
</p>
<p>One final thing should be mentioned. If it is necessary for the
conversion to know whether it is the first invocation (in case a prolog
has to be emitted), the conversion function should increment the
<code>__invocation_counter</code> element of the step data structure just
before returning to the caller. See the description of the <code>struct
__gconv_step_data</code> structure above for more information on how this can
be used.
</p>
<p>The return value must be one of the following values:
</p>
<dl compact="compact">
<dt> <code>__GCONV_EMPTY_INPUT</code></dt>
<dd><p>All input was consumed and there is room left in the output buffer.
</p></dd>
<dt> <code>__GCONV_FULL_OUTPUT</code></dt>
<dd><p>No more room in the output buffer. In case this is not the last step
this value is propagated down from the call of the next conversion
function in the chain.
</p></dd>
<dt> <code>__GCONV_INCOMPLETE_INPUT</code></dt>
<dd><p>The input buffer is not entirely empty since it contains an incomplete
character sequence.
</p></dd>
</dl>
<p>The following example provides a framework for a conversion function.
In case a new conversion has to be written the holes in this
implementation have to be filled and that is it.
</p>
<table><tr><td> </td><td><pre class="smallexample">int
gconv (struct __gconv_step *step, struct __gconv_step_data *data,
const char **inbuf, const char *inbufend, size_t *written,
int do_flush)
{
struct __gconv_step *next_step = step + 1;
struct __gconv_step_data *next_data = data + 1;
gconv_fct fct = next_step->__fct;
int status;
/* <span class="roman">If the function is called with no input this means we have</span>
<span class="roman">to reset to the initial state. The possibly partly</span>
<span class="roman">converted input is dropped.</span> */
if (do_flush)
{
status = __GCONV_OK;
/* <span class="roman">Possible emit a byte sequence which put the state object</span>
<span class="roman">into the initial state.</span> */
/* <span class="roman">Call the steps down the chain if there are any but only</span>
<span class="roman">if we successfully emitted the escape sequence.</span> */
if (status == __GCONV_OK && ! data->__is_last)
status = fct (next_step, next_data, NULL, NULL,
written, 1);
}
else
{
/* <span class="roman">We preserve the initial values of the pointer variables.</span> */
const char *inptr = *inbuf;
char *outbuf = data->__outbuf;
char *outend = data->__outbufend;
char *outptr;
do
{
/* <span class="roman">Remember the start value for this round.</span> */
inptr = *inbuf;
/* <span class="roman">The outbuf buffer is empty.</span> */
outptr = outbuf;
/* <span class="roman">For stateful encodings the state must be safe here.</span> */
/* <span class="roman">Run the conversion loop. <code>status</code> is set</span>
<span class="roman">appropriately afterwards.</span> */
/* <span class="roman">If this is the last step, leave the loop. There is</span>
<span class="roman">nothing we can do.</span> */
if (data->__is_last)
{
/* <span class="roman">Store information about how many bytes are</span>
<span class="roman">available.</span> */
data->__outbuf = outbuf;
/* <span class="roman">If any non-reversible conversions were performed,</span>
<span class="roman">add the number to <code>*written</code>.</span> */
break;
}
/* <span class="roman">Write out all output that was produced.</span> */
if (outbuf > outptr)
{
const char *outerr = data->__outbuf;
int result;
result = fct (next_step, next_data, &outerr,
outbuf, written, 0);
if (result != __GCONV_EMPTY_INPUT)
{
if (outerr != outbuf)
{
/* <span class="roman">Reset the input buffer pointer. We</span>
<span class="roman">document here the complex case.</span> */
size_t nstatus;
/* <span class="roman">Reload the pointers.</span> */
*inbuf = inptr;
outbuf = outptr;
/* <span class="roman">Possibly reset the state.</span> */
/* <span class="roman">Redo the conversion, but this time</span>
<span class="roman">the end of the output buffer is at</span>
<span class="roman"><code>outerr</code>.</span> */
}
/* <span class="roman">Change the status.</span> */
status = result;
}
else
/* <span class="roman">All the output is consumed, we can make</span>
<span class="roman"> another run if everything was ok.</span> */
if (status == __GCONV_FULL_OUTPUT)
status = __GCONV_OK;
}
}
while (status == __GCONV_OK);
/* <span class="roman">We finished one use of this step.</span> */
++data->__invocation_counter;
}
return status;
}
</pre></td></tr></table>
</dd></dl>
<p>This information should be sufficient to write new modules. Anybody
doing so should also take a look at the available source code in the GNU
C library sources. It contains many examples of working and optimized
modules.
</p>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Character-Set-Handling" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc_7.html#Locales" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>root</em> on <em>April 20, 2012</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|