File size: 74,570 Bytes
3f1d7ac |
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 |
/*
* encoding.c : implements the encoding conversion functions needed for XML
*
* Related specs:
* rfc2044 (UTF-8 and UTF-16) F. Yergeau Alis Technologies
* rfc2781 UTF-16, an encoding of ISO 10646, P. Hoffman, F. Yergeau
* [ISO-10646] UTF-8 and UTF-16 in Annexes
* [ISO-8859-1] ISO Latin-1 characters codes.
* [UNICODE] The Unicode Consortium, "The Unicode Standard --
* Worldwide Character Encoding -- Version 1.0", Addison-
* Wesley, Volume 1, 1991, Volume 2, 1992. UTF-8 is
* described in Unicode Technical Report #4.
* [US-ASCII] Coded Character Set--7-bit American Standard Code for
* Information Interchange, ANSI X3.4-1986.
*
* See Copyright for the status of this software.
*
* [email protected]
*
* Original code for IsoLatin1 and UTF-16 by "Martin J. Duerst" <[email protected]>
*/
#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef LIBXML_ICONV_ENABLED
#include <iconv.h>
#include <errno.h>
#endif
#include <libxml/encoding.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#ifdef LIBXML_HTML_ENABLED
#include <libxml/HTMLparser.h>
#endif
#include <libxml/xmlerror.h>
#include "private/buf.h"
#include "private/enc.h"
#include "private/entities.h"
#include "private/error.h"
#include "private/memory.h"
#ifdef LIBXML_ICU_ENABLED
#include <unicode/ucnv.h>
#endif
#define XML_HANDLER_STATIC 1
typedef struct _xmlCharEncodingAlias xmlCharEncodingAlias;
typedef xmlCharEncodingAlias *xmlCharEncodingAliasPtr;
struct _xmlCharEncodingAlias {
const char *name;
const char *alias;
};
static xmlCharEncodingAliasPtr xmlCharEncodingAliases = NULL;
static int xmlCharEncodingAliasesNb = 0;
static int xmlCharEncodingAliasesMax = 0;
static int xmlLittleEndian = 1;
typedef struct {
const char *name;
xmlCharEncoding enc;
} xmlEncTableEntry;
static const xmlEncTableEntry xmlEncTable[] = {
{ "ASCII", XML_CHAR_ENCODING_ASCII },
{ "EUC-JP", XML_CHAR_ENCODING_EUC_JP },
{ "HTML", XML_CHAR_ENCODING_HTML },
{ "ISO LATIN 1", XML_CHAR_ENCODING_8859_1 },
{ "ISO LATIN 2", XML_CHAR_ENCODING_8859_2 },
{ "ISO-10646-UCS-2", XML_CHAR_ENCODING_UCS2 },
{ "ISO-10646-UCS-4", XML_CHAR_ENCODING_UCS4LE },
{ "ISO-2022-JP", XML_CHAR_ENCODING_2022_JP },
{ "ISO-8859-1", XML_CHAR_ENCODING_8859_1 },
{ "ISO-8859-10", XML_CHAR_ENCODING_8859_10 },
{ "ISO-8859-11", XML_CHAR_ENCODING_8859_11 },
{ "ISO-8859-13", XML_CHAR_ENCODING_8859_13 },
{ "ISO-8859-14", XML_CHAR_ENCODING_8859_14 },
{ "ISO-8859-15", XML_CHAR_ENCODING_8859_15 },
{ "ISO-8859-16", XML_CHAR_ENCODING_8859_16 },
{ "ISO-8859-2", XML_CHAR_ENCODING_8859_2 },
{ "ISO-8859-3", XML_CHAR_ENCODING_8859_3 },
{ "ISO-8859-4", XML_CHAR_ENCODING_8859_4 },
{ "ISO-8859-5", XML_CHAR_ENCODING_8859_5 },
{ "ISO-8859-6", XML_CHAR_ENCODING_8859_6 },
{ "ISO-8859-7", XML_CHAR_ENCODING_8859_7 },
{ "ISO-8859-8", XML_CHAR_ENCODING_8859_8 },
{ "ISO-8859-9", XML_CHAR_ENCODING_8859_9 },
{ "ISO-LATIN-1", XML_CHAR_ENCODING_8859_1 },
{ "ISO-LATIN-2", XML_CHAR_ENCODING_8859_2 },
{ "SHIFT_JIS", XML_CHAR_ENCODING_SHIFT_JIS },
{ "UCS-2", XML_CHAR_ENCODING_UCS2 },
{ "UCS-4", XML_CHAR_ENCODING_UCS4LE },
{ "UCS2", XML_CHAR_ENCODING_UCS2 },
{ "UCS4", XML_CHAR_ENCODING_UCS4LE },
{ "US-ASCII", XML_CHAR_ENCODING_ASCII },
{ "UTF-16", XML_CHAR_ENCODING_UTF16 },
{ "UTF-16BE", XML_CHAR_ENCODING_UTF16BE },
{ "UTF-16LE", XML_CHAR_ENCODING_UTF16LE },
{ "UTF-8", XML_CHAR_ENCODING_UTF8 },
{ "UTF16", XML_CHAR_ENCODING_UTF16 },
{ "UTF8", XML_CHAR_ENCODING_UTF8 }
};
static int
asciiToAscii(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen, void *vctxt);
static int
UTF8ToUTF8(unsigned char* out, int *outlen,
const unsigned char* inb, int *inlenb, void *vctxt);
static int
latin1ToUTF8(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen, void *vctxt);
static int
UTF16LEToUTF8(unsigned char* out, int *outlen,
const unsigned char* inb, int *inlenb, void *vctxt);
static int
UTF16BEToUTF8(unsigned char* out, int *outlen,
const unsigned char* inb, int *inlenb, void *vctxt);
#ifdef LIBXML_OUTPUT_ENABLED
static int
UTF8ToLatin1(unsigned char* outb, int *outlen,
const unsigned char* in, int *inlen, void *vctxt);
static int
UTF8ToUTF16(unsigned char* outb, int *outlen,
const unsigned char* in, int *inlen, void *vctxt);
static int
UTF8ToUTF16LE(unsigned char* outb, int *outlen,
const unsigned char* in, int *inlen, void *vctxt);
static int
UTF8ToUTF16BE(unsigned char* outb, int *outlen,
const unsigned char* in, int *inlen, void *vctxt);
#else /* LIBXML_OUTPUT_ENABLED */
#define UTF8ToLatin1 NULL
#define UTF8ToUTF16 NULL
#define UTF8ToUTF16LE NULL
#define UTF8ToUTF16BE NULL
#endif /* LIBXML_OUTPUT_ENABLED */
#if defined(LIBXML_OUTPUT_ENABLED) && defined(LIBXML_HTML_ENABLED)
static int
UTF8ToHtmlWrapper(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen, void *vctxt);
#else
#define UTF8ToHtmlWrapper NULL
#endif
#ifdef LIBXML_ICONV_ENABLED
#define EMPTY_ICONV , (iconv_t) 0, (iconv_t) 0
#else
#define EMPTY_ICONV
#endif
#if !defined(LIBXML_ICONV_ENABLED) && !defined(LIBXML_ICU_ENABLED) && \
defined(LIBXML_ISO8859X_ENABLED)
#include "iso8859x.inc"
static int
ISO8859xToUTF8(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen, void *vctxt);
static int
UTF8ToISO8859x(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen, void *vctxt);
#define MAKE_ISO_HANDLER(name, n) \
{ (char *) name, \
(xmlCharEncodingInputFunc) (void (*)(void)) ISO8859xToUTF8, \
(xmlCharEncodingInputFunc) (void (*)(void)) UTF8ToISO8859x \
EMPTY_ICONV, \
(void *) xmlunicodetable_ISO8859_##n, \
(void *) xmltranscodetable_ISO8859_##n, \
NULL, XML_HANDLER_STATIC }
#else /* LIBXML_ISO8859X_ENABLED */
#define MAKE_ISO_HANDLER(name, n) \
{ (char *) name, NULL, NULL EMPTY_ICONV, NULL, NULL, NULL, \
XML_HANDLER_STATIC }
#endif /* LIBXML_ISO8859X_ENABLED */
#define MAKE_HANDLER(name, in, out) \
{ (char *) name, \
(xmlCharEncodingInputFunc) (void (*)(void)) in, \
(xmlCharEncodingOutputFunc) (void (*)(void)) out \
EMPTY_ICONV, NULL, NULL, NULL, XML_HANDLER_STATIC }
/*
* The layout must match enum xmlCharEncoding.
*
* Names should match the IANA registry if possible:
* https://www.iana.org/assignments/character-sets/character-sets.xhtml
*/
static const xmlCharEncodingHandler defaultHandlers[31] = {
MAKE_HANDLER(NULL, NULL, NULL), /* NONE */
MAKE_HANDLER("UTF-8", UTF8ToUTF8, UTF8ToUTF8),
MAKE_HANDLER("UTF-16LE", UTF16LEToUTF8, UTF8ToUTF16LE),
MAKE_HANDLER("UTF-16BE", UTF16BEToUTF8, UTF8ToUTF16BE),
MAKE_HANDLER("UCS-4LE", NULL, NULL),
MAKE_HANDLER("UCS-4BE", NULL, NULL),
MAKE_HANDLER("IBM037", NULL, NULL),
MAKE_HANDLER("ISO-10646-UCS-4", NULL, NULL), /* UCS4_2143 */
MAKE_HANDLER("ISO-10646-UCS-4", NULL, NULL), /* UCS4_2143 */
MAKE_HANDLER("ISO-10646-UCS-2", NULL, NULL),
MAKE_HANDLER("ISO-8859-1", latin1ToUTF8, UTF8ToLatin1),
MAKE_ISO_HANDLER("ISO-8859-2", 2),
MAKE_ISO_HANDLER("ISO-8859-3", 3),
MAKE_ISO_HANDLER("ISO-8859-4", 4),
MAKE_ISO_HANDLER("ISO-8859-5", 5),
MAKE_ISO_HANDLER("ISO-8859-6", 6),
MAKE_ISO_HANDLER("ISO-8859-7", 7),
MAKE_ISO_HANDLER("ISO-8859-8", 8),
MAKE_ISO_HANDLER("ISO-8859-9", 9),
MAKE_HANDLER("ISO-2022-JP", NULL, NULL),
MAKE_HANDLER("Shift_JIS", NULL, NULL),
MAKE_HANDLER("EUC-JP", NULL, NULL),
MAKE_HANDLER("US-ASCII", asciiToAscii, asciiToAscii),
MAKE_HANDLER("UTF-16", UTF16LEToUTF8, UTF8ToUTF16),
MAKE_HANDLER("HTML", NULL, UTF8ToHtmlWrapper),
MAKE_ISO_HANDLER("ISO-8859-10", 10),
MAKE_ISO_HANDLER("ISO-8859-11", 11),
MAKE_ISO_HANDLER("ISO-8859-13", 13),
MAKE_ISO_HANDLER("ISO-8859-14", 14),
MAKE_ISO_HANDLER("ISO-8859-15", 15),
MAKE_ISO_HANDLER("ISO-8859-16", 16),
};
#define NUM_DEFAULT_HANDLERS \
(sizeof(defaultHandlers) / sizeof(defaultHandlers[0]))
/* the size should be growable, but it's not a big deal ... */
#define MAX_ENCODING_HANDLERS 50
static xmlCharEncodingHandlerPtr *globalHandlers = NULL;
static int nbCharEncodingHandler = 0;
#ifdef LIBXML_ICONV_ENABLED
static int
xmlCharEncIconv(void *vctxt, const char *name, xmlCharEncConverter *conv);
#endif
#ifdef LIBXML_ICU_ENABLED
static int
xmlCharEncUconv(void *vctxt, const char *name, xmlCharEncConverter *conv);
#endif
/************************************************************************
* *
* Generic encoding handling routines *
* *
************************************************************************/
/**
* xmlDetectCharEncoding:
* @in: a pointer to the first bytes of the XML entity, must be at least
* 2 bytes long (at least 4 if encoding is UTF4 variant).
* @len: pointer to the length of the buffer
*
* Guess the encoding of the entity using the first bytes of the entity content
* according to the non-normative appendix F of the XML-1.0 recommendation.
*
* Returns one of the XML_CHAR_ENCODING_... values.
*/
xmlCharEncoding
xmlDetectCharEncoding(const unsigned char* in, int len)
{
if (in == NULL)
return(XML_CHAR_ENCODING_NONE);
if (len >= 4) {
if ((in[0] == 0x00) && (in[1] == 0x00) &&
(in[2] == 0x00) && (in[3] == 0x3C))
return(XML_CHAR_ENCODING_UCS4BE);
if ((in[0] == 0x3C) && (in[1] == 0x00) &&
(in[2] == 0x00) && (in[3] == 0x00))
return(XML_CHAR_ENCODING_UCS4LE);
if ((in[0] == 0x00) && (in[1] == 0x00) &&
(in[2] == 0x3C) && (in[3] == 0x00))
return(XML_CHAR_ENCODING_UCS4_2143);
if ((in[0] == 0x00) && (in[1] == 0x3C) &&
(in[2] == 0x00) && (in[3] == 0x00))
return(XML_CHAR_ENCODING_UCS4_3412);
if ((in[0] == 0x4C) && (in[1] == 0x6F) &&
(in[2] == 0xA7) && (in[3] == 0x94))
return(XML_CHAR_ENCODING_EBCDIC);
if ((in[0] == 0x3C) && (in[1] == 0x3F) &&
(in[2] == 0x78) && (in[3] == 0x6D))
return(XML_CHAR_ENCODING_UTF8);
/*
* Although not part of the recommendation, we also
* attempt an "auto-recognition" of UTF-16LE and
* UTF-16BE encodings.
*/
if ((in[0] == 0x3C) && (in[1] == 0x00) &&
(in[2] == 0x3F) && (in[3] == 0x00))
return(XML_CHAR_ENCODING_UTF16LE);
if ((in[0] == 0x00) && (in[1] == 0x3C) &&
(in[2] == 0x00) && (in[3] == 0x3F))
return(XML_CHAR_ENCODING_UTF16BE);
}
if (len >= 3) {
/*
* Errata on XML-1.0 June 20 2001
* We now allow an UTF8 encoded BOM
*/
if ((in[0] == 0xEF) && (in[1] == 0xBB) &&
(in[2] == 0xBF))
return(XML_CHAR_ENCODING_UTF8);
}
/* For UTF-16 we can recognize by the BOM */
if (len >= 2) {
if ((in[0] == 0xFE) && (in[1] == 0xFF))
return(XML_CHAR_ENCODING_UTF16BE);
if ((in[0] == 0xFF) && (in[1] == 0xFE))
return(XML_CHAR_ENCODING_UTF16LE);
}
return(XML_CHAR_ENCODING_NONE);
}
/**
* xmlCleanupEncodingAliases:
*
* DEPRECATED: This function modifies global state and is not
* thread-safe.
*
* Unregisters all aliases
*/
void
xmlCleanupEncodingAliases(void) {
int i;
if (xmlCharEncodingAliases == NULL)
return;
for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
if (xmlCharEncodingAliases[i].name != NULL)
xmlFree((char *) xmlCharEncodingAliases[i].name);
if (xmlCharEncodingAliases[i].alias != NULL)
xmlFree((char *) xmlCharEncodingAliases[i].alias);
}
xmlCharEncodingAliasesNb = 0;
xmlCharEncodingAliasesMax = 0;
xmlFree(xmlCharEncodingAliases);
xmlCharEncodingAliases = NULL;
}
/**
* xmlGetEncodingAlias:
* @alias: the alias name as parsed, in UTF-8 format (ASCII actually)
*
* DEPRECATED: This function is not thread-safe.
*
* Lookup an encoding name for the given alias.
*
* Returns NULL if not found, otherwise the original name
*/
const char *
xmlGetEncodingAlias(const char *alias) {
int i;
char upper[100];
if (alias == NULL)
return(NULL);
if (xmlCharEncodingAliases == NULL)
return(NULL);
for (i = 0;i < 99;i++) {
upper[i] = (char) toupper((unsigned char) alias[i]);
if (upper[i] == 0) break;
}
upper[i] = 0;
/*
* Walk down the list looking for a definition of the alias
*/
for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
if (!strcmp(xmlCharEncodingAliases[i].alias, upper)) {
return(xmlCharEncodingAliases[i].name);
}
}
return(NULL);
}
/**
* xmlAddEncodingAlias:
* @name: the encoding name as parsed, in UTF-8 format (ASCII actually)
* @alias: the alias name as parsed, in UTF-8 format (ASCII actually)
*
* DEPRECATED: This function modifies global state and is not
* thread-safe.
*
* Registers an alias @alias for an encoding named @name. Existing alias
* will be overwritten.
*
* Returns 0 in case of success, -1 in case of error
*/
int
xmlAddEncodingAlias(const char *name, const char *alias) {
int i;
char upper[100];
char *nameCopy, *aliasCopy;
if ((name == NULL) || (alias == NULL))
return(-1);
for (i = 0;i < 99;i++) {
upper[i] = (char) toupper((unsigned char) alias[i]);
if (upper[i] == 0) break;
}
upper[i] = 0;
if (xmlCharEncodingAliasesNb >= xmlCharEncodingAliasesMax) {
xmlCharEncodingAliasPtr tmp;
int newSize;
newSize = xmlGrowCapacity(xmlCharEncodingAliasesMax, sizeof(tmp[0]),
20, XML_MAX_ITEMS);
if (newSize < 0)
return(-1);
tmp = xmlRealloc(xmlCharEncodingAliases, newSize * sizeof(tmp[0]));
if (tmp == NULL)
return(-1);
xmlCharEncodingAliases = tmp;
xmlCharEncodingAliasesMax = newSize;
}
/*
* Walk down the list looking for a definition of the alias
*/
for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
if (!strcmp(xmlCharEncodingAliases[i].alias, upper)) {
/*
* Replace the definition.
*/
nameCopy = xmlMemStrdup(name);
if (nameCopy == NULL)
return(-1);
xmlFree((char *) xmlCharEncodingAliases[i].name);
xmlCharEncodingAliases[i].name = nameCopy;
return(0);
}
}
/*
* Add the definition
*/
nameCopy = xmlMemStrdup(name);
if (nameCopy == NULL)
return(-1);
aliasCopy = xmlMemStrdup(upper);
if (aliasCopy == NULL) {
xmlFree(nameCopy);
return(-1);
}
xmlCharEncodingAliases[xmlCharEncodingAliasesNb].name = nameCopy;
xmlCharEncodingAliases[xmlCharEncodingAliasesNb].alias = aliasCopy;
xmlCharEncodingAliasesNb++;
return(0);
}
/**
* xmlDelEncodingAlias:
* @alias: the alias name as parsed, in UTF-8 format (ASCII actually)
*
* DEPRECATED: This function modifies global state and is not
* thread-safe.
*
* Unregisters an encoding alias @alias
*
* Returns 0 in case of success, -1 in case of error
*/
int
xmlDelEncodingAlias(const char *alias) {
int i;
if (alias == NULL)
return(-1);
if (xmlCharEncodingAliases == NULL)
return(-1);
/*
* Walk down the list looking for a definition of the alias
*/
for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
if (!strcmp(xmlCharEncodingAliases[i].alias, alias)) {
xmlFree((char *) xmlCharEncodingAliases[i].name);
xmlFree((char *) xmlCharEncodingAliases[i].alias);
xmlCharEncodingAliasesNb--;
memmove(&xmlCharEncodingAliases[i], &xmlCharEncodingAliases[i + 1],
sizeof(xmlCharEncodingAlias) * (xmlCharEncodingAliasesNb - i));
return(0);
}
}
return(-1);
}
static int
xmlCompareEncTableEntries(const void *vkey, const void *ventry) {
const char *key = vkey;
const xmlEncTableEntry *entry = ventry;
return(xmlStrcasecmp(BAD_CAST key, BAD_CAST entry->name));
}
static xmlCharEncoding
xmlParseCharEncodingInternal(const char *name)
{
const xmlEncTableEntry *entry;
if (name == NULL)
return(XML_CHAR_ENCODING_NONE);
entry = bsearch(name, xmlEncTable,
sizeof(xmlEncTable) / sizeof(xmlEncTable[0]),
sizeof(xmlEncTable[0]), xmlCompareEncTableEntries);
if (entry != NULL)
return(entry->enc);
return(XML_CHAR_ENCODING_ERROR);
}
/**
* xmlParseCharEncoding:
* @name: the encoding name as parsed, in UTF-8 format (ASCII actually)
*
* Compare the string to the encoding schemes already known. Note
* that the comparison is case insensitive accordingly to the section
* [XML] 4.3.3 Character Encoding in Entities.
*
* Returns one of the XML_CHAR_ENCODING_... values or XML_CHAR_ENCODING_NONE
* if not recognized.
*/
xmlCharEncoding
xmlParseCharEncoding(const char *name)
{
xmlCharEncoding enc = xmlParseCharEncodingInternal(name);
/* Backward compatibility */
if (enc == XML_CHAR_ENCODING_UTF16)
enc = XML_CHAR_ENCODING_UTF16LE;
return(enc);
}
/**
* xmlGetCharEncodingName:
* @enc: the encoding
*
* The "canonical" name for XML encoding.
* C.f. http://www.w3.org/TR/REC-xml#charencoding
* Section 4.3.3 Character Encoding in Entities
*
* Returns the canonical name for the given encoding
*/
const char*
xmlGetCharEncodingName(xmlCharEncoding enc) {
switch (enc) {
case XML_CHAR_ENCODING_UTF16LE:
return("UTF-16");
case XML_CHAR_ENCODING_UTF16BE:
return("UTF-16");
case XML_CHAR_ENCODING_UCS4LE:
return("ISO-10646-UCS-4");
case XML_CHAR_ENCODING_UCS4BE:
return("ISO-10646-UCS-4");
default:
break;
}
if ((enc <= 0) || ((size_t) enc >= NUM_DEFAULT_HANDLERS))
return(NULL);
return(defaultHandlers[enc].name);
}
/************************************************************************
* *
* Char encoding handlers *
* *
************************************************************************/
/**
* xmlNewCharEncodingHandler:
* @name: the encoding name, in UTF-8 format (ASCII actually)
* @input: the xmlCharEncodingInputFunc to read that encoding
* @output: the xmlCharEncodingOutputFunc to write that encoding
*
* DEPRECATED: This function modifies global state and is not
* thread-safe.
*
* Create and registers an xmlCharEncodingHandler.
*
* Returns the xmlCharEncodingHandlerPtr created (or NULL in case of error).
*/
xmlCharEncodingHandlerPtr
xmlNewCharEncodingHandler(const char *name,
xmlCharEncodingInputFunc input,
xmlCharEncodingOutputFunc output) {
xmlCharEncodingHandlerPtr handler;
const char *alias;
char upper[500];
int i;
char *up = NULL;
/*
* Do the alias resolution
*/
alias = xmlGetEncodingAlias(name);
if (alias != NULL)
name = alias;
/*
* Keep only the uppercase version of the encoding.
*/
if (name == NULL)
return(NULL);
for (i = 0;i < 499;i++) {
upper[i] = (char) toupper((unsigned char) name[i]);
if (upper[i] == 0) break;
}
upper[i] = 0;
up = xmlMemStrdup(upper);
if (up == NULL)
return(NULL);
/*
* allocate and fill-up an handler block.
*/
handler = (xmlCharEncodingHandlerPtr)
xmlMalloc(sizeof(xmlCharEncodingHandler));
if (handler == NULL) {
xmlFree(up);
return(NULL);
}
memset(handler, 0, sizeof(xmlCharEncodingHandler));
handler->input = input;
handler->output = output;
handler->name = up;
handler->flags = XML_HANDLER_STATIC;
#ifdef LIBXML_ICONV_ENABLED
handler->iconv_in = NULL;
handler->iconv_out = NULL;
#endif
/*
* registers and returns the handler.
*/
xmlRegisterCharEncodingHandler(handler);
return(handler);
}
/**
* xmlInitCharEncodingHandlers:
*
* DEPRECATED: Alias for xmlInitParser.
*/
void
xmlInitCharEncodingHandlers(void) {
xmlInitParser();
}
/**
* xmlInitEncodingInternal:
*
* Initialize the char encoding support.
*/
void
xmlInitEncodingInternal(void) {
unsigned short int tst = 0x1234;
unsigned char *ptr = (unsigned char *) &tst;
if (*ptr == 0x12) xmlLittleEndian = 0;
else xmlLittleEndian = 1;
}
/**
* xmlCleanupCharEncodingHandlers:
*
* DEPRECATED: This function will be made private. Call xmlCleanupParser
* to free global state but see the warnings there. xmlCleanupParser
* should be only called once at program exit. In most cases, you don't
* have call cleanup functions at all.
*
* Cleanup the memory allocated for the char encoding support, it
* unregisters all the encoding handlers and the aliases.
*/
void
xmlCleanupCharEncodingHandlers(void) {
xmlCleanupEncodingAliases();
if (globalHandlers == NULL) return;
for (;nbCharEncodingHandler > 0;) {
xmlCharEncodingHandler *handler;
nbCharEncodingHandler--;
handler = globalHandlers[nbCharEncodingHandler];
if (handler != NULL) {
if (handler->name != NULL)
xmlFree(handler->name);
xmlFree(handler);
}
}
xmlFree(globalHandlers);
globalHandlers = NULL;
nbCharEncodingHandler = 0;
}
/**
* xmlRegisterCharEncodingHandler:
* @handler: the xmlCharEncodingHandlerPtr handler block
*
* DEPRECATED: This function modifies global state and is not
* thread-safe.
*
* Register the char encoding handler.
*/
void
xmlRegisterCharEncodingHandler(xmlCharEncodingHandlerPtr handler) {
if (handler == NULL)
return;
if (globalHandlers == NULL) {
globalHandlers = xmlMalloc(
MAX_ENCODING_HANDLERS * sizeof(globalHandlers[0]));
if (globalHandlers == NULL)
goto free_handler;
}
if (nbCharEncodingHandler >= MAX_ENCODING_HANDLERS)
goto free_handler;
globalHandlers[nbCharEncodingHandler++] = handler;
return;
free_handler:
if (handler != NULL) {
if (handler->name != NULL) {
xmlFree(handler->name);
}
xmlFree(handler);
}
}
static int
xmlInvokeConvImpl(xmlCharEncConvImpl impl, void *implCtxt,
const char *name, xmlCharEncodingHandler *handler) {
xmlCharEncConverter conv = { NULL, NULL, NULL, NULL, NULL };
int ret;
ret = impl(implCtxt, name, &conv);
if (ret == XML_ERR_OK) {
handler->input =
(xmlCharEncodingInputFunc) (void (*)(void)) conv.input;
handler->output =
(xmlCharEncodingOutputFunc) (void (*)(void)) conv.output;
handler->ctxtDtor = conv.ctxtDtor;
handler->inputCtxt = conv.inputCtxt;
handler->outputCtxt = conv.outputCtxt;
}
return(ret);
}
/**
* xmlFindExtraHandler:
* @norig: name of the char encoding
* @name: potentially aliased name of the encoding
* @output: boolean, use handler for output
* @impl: a conversion implementation (optional)
* @implCtxt: user data for conversion implementation (optional)
* @out: pointer to resulting handler
*
* Search the non-default handlers for an exact match.
*
* Returns an xmlParserErrors error code.
*/
static int
xmlFindExtraHandler(const char *norig, const char *name, int output,
xmlCharEncConvImpl impl, void *implCtxt,
xmlCharEncodingHandler **out) {
xmlCharEncodingHandler *handler;
int ret;
int i;
handler = xmlMalloc(sizeof(*handler));
if (handler == NULL)
return(XML_ERR_NO_MEMORY);
memset(handler, 0, sizeof(*handler));
handler->name = xmlMemStrdup(name);
if (handler->name == NULL) {
ret = XML_ERR_NO_MEMORY;
goto done;
}
/*
* Try custom implementation before deprecated global handlers.
*
* Note that we pass the original name without deprecated
* alias resolution.
*/
if (impl != NULL) {
ret = xmlInvokeConvImpl(impl, implCtxt, norig, handler);
if (ret != XML_ERR_OK)
goto done;
*out = handler;
return(XML_ERR_OK);
}
/*
* Deprecated
*/
if (globalHandlers != NULL) {
for (i = 0; i < nbCharEncodingHandler; i++) {
xmlCharEncodingHandler *h = globalHandlers[i];
if (!xmlStrcasecmp((const xmlChar *) name,
(const xmlChar *) h->name)) {
if ((output ? h->output : h->input) != NULL) {
*out = h;
ret = XML_ERR_OK;
goto done;
}
}
}
}
#ifdef LIBXML_ICONV_ENABLED
ret = xmlInvokeConvImpl(xmlCharEncIconv, handler, name, handler);
if (ret == XML_ERR_OK) {
*out = handler;
return(XML_ERR_OK);
}
if (ret != XML_ERR_UNSUPPORTED_ENCODING)
goto done;
#endif /* LIBXML_ICONV_ENABLED */
#ifdef LIBXML_ICU_ENABLED
ret = xmlInvokeConvImpl(xmlCharEncUconv, handler, name, handler);
if (ret == XML_ERR_OK) {
*out = handler;
return(XML_ERR_OK);
}
if (ret != XML_ERR_UNSUPPORTED_ENCODING)
goto done;
#endif /* LIBXML_ICU_ENABLED */
ret = XML_ERR_UNSUPPORTED_ENCODING;
done:
if (handler != NULL) {
xmlFree(handler->name);
xmlFree(handler);
}
return(ret);
}
/**
* xmlLookupCharEncodingHandler:
* @enc: an xmlCharEncoding value.
* @out: pointer to result
*
* Find or create a handler matching the encoding. The following
* converters are looked up in order:
*
* - Built-in handler (UTF-8, UTF-16, ISO-8859-1, ASCII)
* - User-registered global handler (deprecated)
* - iconv if enabled
* - ICU if enabled
*
* The handler must be closed with xmlCharEncCloseFunc.
*
* If the encoding is UTF-8, a NULL handler and no error code will
* be returned.
*
* Available since 2.13.0.
*
* Returns XML_ERR_OK, XML_ERR_UNSUPPORTED_ENCODING or another
* xmlParserErrors error code.
*/
int
xmlLookupCharEncodingHandler(xmlCharEncoding enc,
xmlCharEncodingHandler **out) {
const xmlCharEncodingHandler *handler;
if (out == NULL)
return(XML_ERR_ARGUMENT);
*out = NULL;
if ((enc <= 0) || ((size_t) enc >= NUM_DEFAULT_HANDLERS))
return(XML_ERR_UNSUPPORTED_ENCODING);
/* Return NULL handler for UTF-8 */
if ((enc == XML_CHAR_ENCODING_UTF8) ||
(enc == XML_CHAR_ENCODING_NONE))
return(XML_ERR_OK);
handler = &defaultHandlers[enc];
if ((handler->input != NULL) || (handler->output != NULL)) {
*out = (xmlCharEncodingHandler *) handler;
return(XML_ERR_OK);
}
if (handler->name != NULL)
return(xmlFindExtraHandler(handler->name, handler->name, 0,
NULL, NULL, out));
return(XML_ERR_UNSUPPORTED_ENCODING);
}
/**
* xmlGetCharEncodingHandler:
* @enc: an xmlCharEncoding value.
*
* DEPRECATED: Use xmlLookupCharEncodingHandler which has better error
* reporting.
*
* Returns the handler or NULL if no handler was found or an error
* occurred.
*/
xmlCharEncodingHandlerPtr
xmlGetCharEncodingHandler(xmlCharEncoding enc) {
xmlCharEncodingHandler *ret;
xmlLookupCharEncodingHandler(enc, &ret);
return(ret);
}
/**
* xmlCreateCharEncodingHandler:
* @name: a string describing the char encoding.
* @output: boolean, use handler for output
* @impl: a conversion implementation (optional)
* @implCtxt: user data for conversion implementation (optional)
* @out: pointer to result
*
* Find or create a handler matching the encoding. The following
* converters are looked up in order:
*
* - Built-in handler (UTF-8, UTF-16, ISO-8859-1, ASCII)
* - Custom implementation if provided
* - User-registered global handler (deprecated)
* - iconv if enabled
* - ICU if enabled
*
* The handler must be closed with xmlCharEncCloseFunc.
*
* If the encoding is UTF-8, a NULL handler and no error code will
* be returned.
*
* Available since 2.14.0.
*
* Returns XML_ERR_OK, XML_ERR_UNSUPPORTED_ENCODING or another
* xmlParserErrors error code.
*/
int
xmlCreateCharEncodingHandler(const char *name, int output,
xmlCharEncConvImpl impl, void *implCtxt,
xmlCharEncodingHandler **out) {
const xmlCharEncodingHandler *handler;
const char *norig, *nalias;
xmlCharEncoding enc;
if (out == NULL)
return(XML_ERR_ARGUMENT);
*out = NULL;
if (name == NULL)
return(XML_ERR_ARGUMENT);
norig = name;
nalias = xmlGetEncodingAlias(name);
if (nalias != NULL)
name = nalias;
enc = xmlParseCharEncodingInternal(name);
/* Return NULL handler for UTF-8 */
if (enc == XML_CHAR_ENCODING_UTF8)
return(XML_ERR_OK);
if ((enc > 0) && ((size_t) enc < NUM_DEFAULT_HANDLERS)) {
handler = &defaultHandlers[enc];
if ((output ? handler->output : handler->input) != NULL) {
*out = (xmlCharEncodingHandler *) handler;
return(XML_ERR_OK);
}
}
return(xmlFindExtraHandler(norig, name, output, impl, implCtxt, out));
}
/**
* xmlOpenCharEncodingHandler:
* @name: a string describing the char encoding.
* @output: boolean, use handler for output
* @out: pointer to result
*
* Find or create a handler matching the encoding. The following
* converters are looked up in order:
*
* - Built-in handler (UTF-8, UTF-16, ISO-8859-1, ASCII)
* - User-registered global handler (deprecated)
* - iconv if enabled
* - ICU if enabled
*
* The handler must be closed with xmlCharEncCloseFunc.
*
* If the encoding is UTF-8, a NULL handler and no error code will
* be returned.
*
* Available since 2.13.0.
*
* Returns XML_ERR_OK, XML_ERR_UNSUPPORTED_ENCODING or another
* xmlParserErrors error code.
*/
int
xmlOpenCharEncodingHandler(const char *name, int output,
xmlCharEncodingHandler **out) {
return(xmlCreateCharEncodingHandler(name, output, NULL, NULL, out));
}
/**
* xmlFindCharEncodingHandler:
* @name: a string describing the char encoding.
*
* DEPRECATED: Use xmlOpenCharEncodingHandler which has better error
* reporting.
*
* If the encoding is UTF-8, this will return a no-op handler that
* shouldn't be used.
*
* Returns the handler or NULL if no handler was found or an error
* occurred.
*/
xmlCharEncodingHandlerPtr
xmlFindCharEncodingHandler(const char *name) {
xmlCharEncodingHandler *ret;
/*
* This handler shouldn't be used, but we must return a non-NULL
* handler.
*/
if ((xmlStrcasecmp(BAD_CAST name, BAD_CAST "UTF-8") == 0) ||
(xmlStrcasecmp(BAD_CAST name, BAD_CAST "UTF8") == 0))
return((xmlCharEncodingHandlerPtr)
&defaultHandlers[XML_CHAR_ENCODING_UTF8]);
xmlOpenCharEncodingHandler(name, 0, &ret);
return(ret);
}
/************************************************************************
* *
* ICONV based generic conversion functions *
* *
************************************************************************/
#ifdef LIBXML_ICONV_ENABLED
typedef struct {
iconv_t cd;
} xmlIconvCtxt;
/**
* xmlIconvConvert:
* @vctxt: conversion context
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of input bytes
* @inlen: the length of @in
*
* Returns an XML_ENC_ERR code.
*
* The value of @inlen after return is the number of octets consumed
* as the return value is positive, else unpredictable.
* The value of @outlen after return is the number of octets produced.
*/
static int
xmlIconvConvert(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen, void *vctxt) {
xmlIconvCtxt *ctxt = vctxt;
size_t icv_inlen, icv_outlen;
const char *icv_in = (const char *) in;
char *icv_out = (char *) out;
size_t ret;
if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL)) {
if (outlen != NULL) *outlen = 0;
return(XML_ENC_ERR_INTERNAL);
}
icv_inlen = *inlen;
icv_outlen = *outlen;
/*
* Some versions take const, other versions take non-const input.
*/
ret = iconv(ctxt->cd, (void *) &icv_in, &icv_inlen, &icv_out, &icv_outlen);
*inlen -= icv_inlen;
*outlen -= icv_outlen;
if (ret == (size_t) -1) {
if (errno == EILSEQ)
return(XML_ENC_ERR_INPUT);
if (errno == E2BIG)
return(XML_ENC_ERR_SPACE);
/*
* EINVAL means a truncated multi-byte sequence at the end
* of the input buffer. We treat this as success.
*/
if (errno == EINVAL)
return(XML_ENC_ERR_SUCCESS);
return(XML_ENC_ERR_INTERNAL);
}
return(XML_ENC_ERR_SUCCESS);
}
static void
xmlIconvFree(void *vctxt) {
xmlIconvCtxt *ctxt = vctxt;
if (ctxt->cd != (iconv_t) -1)
iconv_close(ctxt->cd);
xmlFree(ctxt);
}
static int
xmlCharEncIconv(void *vctxt, const char *name, xmlCharEncConverter *conv) {
xmlCharEncodingHandler *handler = vctxt;
xmlIconvCtxt *inputCtxt = NULL, *outputCtxt = NULL;
iconv_t icv_in;
iconv_t icv_out;
int ret;
inputCtxt = xmlMalloc(sizeof(xmlIconvCtxt));
if (inputCtxt == NULL) {
ret = XML_ERR_NO_MEMORY;
goto error;
}
inputCtxt->cd = (iconv_t) -1;
icv_in = iconv_open("UTF-8", name);
if (icv_in == (iconv_t) -1) {
if (errno == EINVAL)
ret = XML_ERR_UNSUPPORTED_ENCODING;
else if (errno == ENOMEM)
ret = XML_ERR_NO_MEMORY;
else
ret = XML_ERR_SYSTEM;
goto error;
}
inputCtxt->cd = icv_in;
outputCtxt = xmlMalloc(sizeof(xmlIconvCtxt));
if (outputCtxt == NULL) {
ret = XML_ERR_NO_MEMORY;
goto error;
}
outputCtxt->cd = (iconv_t) -1;
icv_out = iconv_open(name, "UTF-8");
if (icv_out == (iconv_t) -1) {
if (errno == EINVAL)
ret = XML_ERR_UNSUPPORTED_ENCODING;
else if (errno == ENOMEM)
ret = XML_ERR_NO_MEMORY;
else
ret = XML_ERR_SYSTEM;
goto error;
}
outputCtxt->cd = icv_out;
conv->input = xmlIconvConvert;
conv->output = xmlIconvConvert;
conv->ctxtDtor = xmlIconvFree;
conv->inputCtxt = inputCtxt;
conv->outputCtxt = outputCtxt;
/* Backward compatibility */
if (handler != NULL) {
handler->iconv_in = icv_in;
handler->iconv_out = icv_out;
}
return(XML_ERR_OK);
error:
if (inputCtxt != NULL)
xmlIconvFree(inputCtxt);
if (outputCtxt != NULL)
xmlIconvFree(outputCtxt);
return(ret);
}
#endif /* LIBXML_ICONV_ENABLED */
/************************************************************************
* *
* ICU based generic conversion functions *
* *
************************************************************************/
#ifdef LIBXML_ICU_ENABLED
/* Size of pivot buffer, same as icu/source/common/ucnv.cpp CHUNK_SIZE */
#define ICU_PIVOT_BUF_SIZE 1024
typedef struct _uconv_t xmlUconvCtxt;
struct _uconv_t {
UConverter *uconv; /* for conversion between an encoding and UTF-16 */
UConverter *utf8; /* for conversion between UTF-8 and UTF-16 */
UChar *pivot_source;
UChar *pivot_target;
int isInput;
UChar pivot_buf[ICU_PIVOT_BUF_SIZE];
};
/**
* xmlUconvConvert:
* @vctxt: converison context
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of input bytes
* @inlen: the length of @in
*
* Returns an XML_ENC_ERR code.
*
* The value of @inlen after return is the number of octets consumed
* as the return value is positive, else unpredictable.
* The value of @outlen after return is the number of octets produced.
*/
static int
xmlUconvConvert(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen, void *vctxt) {
xmlUconvCtxt *cd = vctxt;
const char *ucv_in = (const char *) in;
char *ucv_out = (char *) out;
UConverter *target, *source;
UErrorCode err = U_ZERO_ERROR;
int ret;
if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL)) {
if (outlen != NULL)
*outlen = 0;
return(XML_ENC_ERR_INTERNAL);
}
/*
* Note that the ICU API is stateful. It can always consume a certain
* amount of input even if the output buffer would overflow. The
* remaining input must be processed by calling ucnv_convertEx with a
* possibly empty input buffer.
*
* ucnv_convertEx is always called with reset and flush set to 0,
* so we don't mess up the state. This should never generate
* U_TRUNCATED_CHAR_FOUND errors.
*/
if (cd->isInput) {
source = cd->uconv;
target = cd->utf8;
} else {
source = cd->utf8;
target = cd->uconv;
}
ucnv_convertEx(target, source, &ucv_out, ucv_out + *outlen,
&ucv_in, ucv_in + *inlen, cd->pivot_buf,
&cd->pivot_source, &cd->pivot_target,
cd->pivot_buf + ICU_PIVOT_BUF_SIZE, 0, 0, &err);
*inlen = ucv_in - (const char*) in;
*outlen = ucv_out - (char *) out;
if (U_SUCCESS(err)) {
ret = XML_ENC_ERR_SUCCESS;
} else {
switch (err) {
case U_TRUNCATED_CHAR_FOUND:
/* Shouldn't happen without flush */
ret = XML_ENC_ERR_SUCCESS;
break;
case U_BUFFER_OVERFLOW_ERROR:
ret = XML_ENC_ERR_SPACE;
break;
case U_INVALID_CHAR_FOUND:
case U_ILLEGAL_CHAR_FOUND:
case U_ILLEGAL_ESCAPE_SEQUENCE:
case U_UNSUPPORTED_ESCAPE_SEQUENCE:
ret = XML_ENC_ERR_INPUT;
break;
case U_MEMORY_ALLOCATION_ERROR:
ret = XML_ENC_ERR_MEMORY;
break;
default:
ret = XML_ENC_ERR_INTERNAL;
break;
}
}
return(ret);
}
static int
openIcuConverter(const char* name, int isInput, xmlUconvCtxt **out)
{
UErrorCode status;
xmlUconvCtxt *conv;
*out = NULL;
conv = (xmlUconvCtxt *) xmlMalloc(sizeof(xmlUconvCtxt));
if (conv == NULL)
return(XML_ERR_NO_MEMORY);
conv->isInput = isInput;
conv->pivot_source = conv->pivot_buf;
conv->pivot_target = conv->pivot_buf;
status = U_ZERO_ERROR;
conv->uconv = ucnv_open(name, &status);
if (U_FAILURE(status))
goto error;
status = U_ZERO_ERROR;
if (isInput) {
ucnv_setToUCallBack(conv->uconv, UCNV_TO_U_CALLBACK_STOP,
NULL, NULL, NULL, &status);
}
else {
ucnv_setFromUCallBack(conv->uconv, UCNV_FROM_U_CALLBACK_STOP,
NULL, NULL, NULL, &status);
}
if (U_FAILURE(status))
goto error;
status = U_ZERO_ERROR;
conv->utf8 = ucnv_open("UTF-8", &status);
if (U_FAILURE(status))
goto error;
*out = conv;
return(0);
error:
if (conv->uconv)
ucnv_close(conv->uconv);
xmlFree(conv);
if (status == U_FILE_ACCESS_ERROR)
return(XML_ERR_UNSUPPORTED_ENCODING);
if (status == U_MEMORY_ALLOCATION_ERROR)
return(XML_ERR_NO_MEMORY);
return(XML_ERR_SYSTEM);
}
static void
closeIcuConverter(xmlUconvCtxt *conv)
{
if (conv == NULL)
return;
ucnv_close(conv->uconv);
ucnv_close(conv->utf8);
xmlFree(conv);
}
static void
xmlUconvFree(void *vctxt) {
closeIcuConverter(vctxt);
}
static int
xmlCharEncUconv(void *vctxt ATTRIBUTE_UNUSED, const char *name,
xmlCharEncConverter *conv) {
xmlUconvCtxt *ucv_in = NULL;
xmlUconvCtxt *ucv_out = NULL;
int ret;
ret = openIcuConverter(name, 1, &ucv_in);
if (ret != 0)
goto error;
ret = openIcuConverter(name, 0, &ucv_out);
if (ret != 0)
goto error;
conv->input = xmlUconvConvert;
conv->output = xmlUconvConvert;
conv->ctxtDtor = xmlUconvFree;
conv->inputCtxt = ucv_in;
conv->outputCtxt = ucv_out;
return(XML_ERR_OK);
error:
if (ucv_in != NULL)
closeIcuConverter(ucv_in);
if (ucv_out != NULL)
closeIcuConverter(ucv_out);
return(ret);
}
#endif /* LIBXML_ICU_ENABLED */
/************************************************************************
* *
* The real API used by libxml for on-the-fly conversion *
* *
************************************************************************/
/**
* xmlEncConvertError:
* @code: XML_ENC_ERR code
*
* Convert XML_ENC_ERR to libxml2 error codes.
*/
static int
xmlEncConvertError(int code) {
int ret;
switch (code) {
case XML_ENC_ERR_SUCCESS:
ret = XML_ERR_OK;
break;
case XML_ENC_ERR_INPUT:
ret = XML_ERR_INVALID_ENCODING;
break;
case XML_ENC_ERR_MEMORY:
ret = XML_ERR_NO_MEMORY;
break;
default:
ret = XML_ERR_INTERNAL_ERROR;
break;
}
return(ret);
}
/**
* xmlEncInputChunk:
* @handler: encoding handler
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of input bytes
* @inlen: the length of @in
*
* The value of @inlen after return is the number of octets consumed
* as the return value is 0, else unpredictable.
* The value of @outlen after return is the number of octets produced.
*
* Returns an XML_ENC_ERR code.
*/
int
xmlEncInputChunk(xmlCharEncodingHandler *handler, unsigned char *out,
int *outlen, const unsigned char *in, int *inlen) {
int ret;
if (handler->input != NULL) {
xmlCharEncConvFunc conv =
(xmlCharEncConvFunc) (void (*)(void)) handler->input;
ret = conv(out, outlen, in, inlen, handler->inputCtxt);
if (ret > 0)
ret = XML_ENC_ERR_SUCCESS;
}
else {
*outlen = 0;
*inlen = 0;
ret = XML_ENC_ERR_INTERNAL;
}
return(ret);
}
/**
* xmlEncOutputChunk:
* @handler: encoding handler
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of input bytes
* @inlen: the length of @in
*
* Returns an XML_ENC_ERR code.
*
* The value of @inlen after return is the number of octets consumed
* as the return value is 0, else unpredictable.
* The value of @outlen after return is the number of octets produced.
*/
static int
xmlEncOutputChunk(xmlCharEncodingHandler *handler, unsigned char *out,
int *outlen, const unsigned char *in, int *inlen) {
int ret;
if (handler->output != NULL) {
xmlCharEncConvFunc conv =
(xmlCharEncConvFunc) (void (*)(void)) handler->output;
ret = conv(out, outlen, in, inlen, handler->outputCtxt);
if (ret > 0)
ret = XML_ENC_ERR_SUCCESS;
}
else {
*outlen = 0;
*inlen = 0;
ret = XML_ENC_ERR_INTERNAL;
}
return(ret);
}
/**
* xmlCharEncFirstLine:
* @handler: char encoding transformation data structure
* @out: an xmlBuffer for the output.
* @in: an xmlBuffer for the input
*
* DEPERECATED: Don't use.
*
* Returns the number of bytes written or an XML_ENC_ERR code.
*/
int
xmlCharEncFirstLine(xmlCharEncodingHandler *handler, xmlBufferPtr out,
xmlBufferPtr in) {
return(xmlCharEncInFunc(handler, out, in));
}
/**
* xmlCharEncInput:
* @input: a parser input buffer
* @sizeOut: pointer to output size
*
* @sizeOut should be set to the maximum output size (or SIZE_MAX).
* After return, it is set to the number of bytes written.
*
* Generic front-end for the encoding handler on parser input
*
* Returns an XML_ENC_ERR code.
*/
int
xmlCharEncInput(xmlParserInputBufferPtr input, size_t *sizeOut)
{
xmlBufPtr out, in;
const xmlChar *dataIn;
size_t availIn;
size_t maxOut;
size_t totalIn, totalOut;
int ret;
out = input->buffer;
in = input->raw;
maxOut = *sizeOut;
totalOut = 0;
*sizeOut = 0;
availIn = xmlBufUse(in);
if (availIn == 0)
return(0);
dataIn = xmlBufContent(in);
totalIn = 0;
while (1) {
size_t availOut;
int completeOut, completeIn;
int c_out, c_in;
availOut = xmlBufAvail(out);
if (availOut > INT_MAX / 2)
availOut = INT_MAX / 2;
if (availOut < maxOut) {
c_out = availOut;
completeOut = 0;
} else {
c_out = maxOut;
completeOut = 1;
}
if (availIn > INT_MAX / 2) {
c_in = INT_MAX / 2;
completeIn = 0;
} else {
c_in = availIn;
completeIn = 1;
}
ret = xmlEncInputChunk(input->encoder, xmlBufEnd(out), &c_out,
dataIn, &c_in);
totalIn += c_in;
dataIn += c_in;
availIn -= c_in;
totalOut += c_out;
maxOut -= c_out;
xmlBufAddLen(out, c_out);
if ((ret != XML_ENC_ERR_SUCCESS) && (ret != XML_ENC_ERR_SPACE)) {
input->error = xmlEncConvertError(ret);
return(ret);
}
if ((completeOut) && (completeIn))
break;
if ((completeOut) && (ret == XML_ENC_ERR_SPACE))
break;
if ((completeIn) && (ret == XML_ENC_ERR_SUCCESS))
break;
if (ret == XML_ENC_ERR_SPACE) {
if (xmlBufGrow(out, 4096) < 0) {
input->error = XML_ERR_NO_MEMORY;
return(XML_ENC_ERR_MEMORY);
}
}
}
xmlBufShrink(in, totalIn);
if (input->rawconsumed > ULONG_MAX - (unsigned long) totalIn)
input->rawconsumed = ULONG_MAX;
else
input->rawconsumed += totalIn;
*sizeOut = totalOut;
return(XML_ERR_OK);
}
/**
* xmlCharEncInFunc:
* @handler: char encoding transformation data structure
* @out: an xmlBuffer for the output.
* @in: an xmlBuffer for the input
*
* Generic front-end for the encoding handler input function
*
* Returns the number of bytes written or an XML_ENC_ERR code.
*/
int
xmlCharEncInFunc(xmlCharEncodingHandler * handler, xmlBufferPtr out,
xmlBufferPtr in)
{
int ret;
int written;
int toconv;
if (handler == NULL)
return(XML_ENC_ERR_INTERNAL);
if (out == NULL)
return(XML_ENC_ERR_INTERNAL);
if (in == NULL)
return(XML_ENC_ERR_INTERNAL);
toconv = in->use;
if (toconv == 0)
return (0);
written = out->size - out->use -1; /* count '\0' */
if (toconv * 2 >= written) {
xmlBufferGrow(out, out->size + toconv * 2);
written = out->size - out->use - 1;
}
ret = xmlEncInputChunk(handler, &out->content[out->use], &written,
in->content, &toconv);
xmlBufferShrink(in, toconv);
out->use += written;
out->content[out->use] = 0;
return (written? written : ret);
}
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlCharEncOutput:
* @output: a parser output buffer
* @init: is this an initialization call without data
*
* Generic front-end for the encoding handler on parser output
* a first call with @init == 1 has to be made first to initiate the
* output in case of non-stateless encoding needing to initiate their
* state or the output (like the BOM in UTF16).
* In case of UTF8 sequence conversion errors for the given encoder,
* the content will be automatically remapped to a CharRef sequence.
*
* Returns the number of bytes written or an XML_ENC_ERR code.
*/
int
xmlCharEncOutput(xmlOutputBufferPtr output, int init)
{
int ret;
size_t written;
int writtentot = 0;
size_t toconv;
int c_in;
int c_out;
xmlBufPtr in;
xmlBufPtr out;
if ((output == NULL) || (output->encoder == NULL) ||
(output->buffer == NULL) || (output->conv == NULL))
return(XML_ENC_ERR_INTERNAL);
out = output->conv;
in = output->buffer;
retry:
written = xmlBufAvail(out);
/*
* First specific handling of the initialization call
*/
if (init) {
c_in = 0;
c_out = written;
/* TODO: Check return value. */
xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
NULL, &c_in);
xmlBufAddLen(out, c_out);
return(c_out);
}
/*
* Conversion itself.
*/
toconv = xmlBufUse(in);
if (toconv > 64 * 1024)
toconv = 64 * 1024;
if (toconv * 4 >= written) {
if (xmlBufGrow(out, toconv * 4) < 0) {
ret = XML_ENC_ERR_MEMORY;
goto error;
}
written = xmlBufAvail(out);
}
if (written > 256 * 1024)
written = 256 * 1024;
c_in = toconv;
c_out = written;
ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
xmlBufContent(in), &c_in);
xmlBufShrink(in, c_in);
xmlBufAddLen(out, c_out);
writtentot += c_out;
if (ret == XML_ENC_ERR_SPACE)
goto retry;
/*
* Attempt to handle error cases
*/
if (ret == XML_ENC_ERR_INPUT) {
xmlChar charref[20];
int len = xmlBufUse(in);
xmlChar *content = xmlBufContent(in);
int cur, charrefLen;
cur = xmlGetUTF8Char(content, &len);
if (cur <= 0)
goto error;
/*
* Removes the UTF8 sequence, and replace it by a charref
* and continue the transcoding phase, hoping the error
* did not mangle the encoder state.
*/
charrefLen = xmlSerializeDecCharRef((char *) charref, cur);
xmlBufGrow(out, charrefLen * 4);
c_out = xmlBufAvail(out);
c_in = charrefLen;
ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
charref, &c_in);
if ((ret < 0) || (c_in != charrefLen)) {
ret = XML_ENC_ERR_INTERNAL;
goto error;
}
xmlBufShrink(in, len);
xmlBufAddLen(out, c_out);
writtentot += c_out;
goto retry;
}
error:
if (((writtentot <= 0) && (ret != 0)) ||
(ret == XML_ENC_ERR_MEMORY)) {
if (output->error == 0)
output->error = xmlEncConvertError(ret);
return(ret);
}
return(writtentot);
}
#endif
/**
* xmlCharEncOutFunc:
* @handler: char encoding transformation data structure
* @out: an xmlBuffer for the output.
* @in: an xmlBuffer for the input
*
* Generic front-end for the encoding handler output function
* a first call with @in == NULL has to be made firs to initiate the
* output in case of non-stateless encoding needing to initiate their
* state or the output (like the BOM in UTF16).
* In case of UTF8 sequence conversion errors for the given encoder,
* the content will be automatically remapped to a CharRef sequence.
*
* Returns the number of bytes written or an XML_ENC_ERR code.
*/
int
xmlCharEncOutFunc(xmlCharEncodingHandler *handler, xmlBufferPtr out,
xmlBufferPtr in) {
int ret;
int written;
int writtentot = 0;
int toconv;
if (handler == NULL) return(XML_ENC_ERR_INTERNAL);
if (out == NULL) return(XML_ENC_ERR_INTERNAL);
retry:
written = out->size - out->use;
if (written > 0)
written--; /* Gennady: count '/0' */
/*
* First specific handling of in = NULL, i.e. the initialization call
*/
if (in == NULL) {
toconv = 0;
/* TODO: Check return value. */
xmlEncOutputChunk(handler, &out->content[out->use], &written,
NULL, &toconv);
out->use += written;
out->content[out->use] = 0;
return(0);
}
/*
* Conversion itself.
*/
toconv = in->use;
if (toconv * 4 >= written) {
xmlBufferGrow(out, toconv * 4);
written = out->size - out->use - 1;
}
ret = xmlEncOutputChunk(handler, &out->content[out->use], &written,
in->content, &toconv);
xmlBufferShrink(in, toconv);
out->use += written;
writtentot += written;
out->content[out->use] = 0;
if (ret == XML_ENC_ERR_SPACE)
goto retry;
/*
* Attempt to handle error cases
*/
if (ret == XML_ENC_ERR_INPUT) {
xmlChar charref[20];
int len = in->use;
const xmlChar *utf = (const xmlChar *) in->content;
int cur, charrefLen;
cur = xmlGetUTF8Char(utf, &len);
if (cur <= 0)
return(ret);
/*
* Removes the UTF8 sequence, and replace it by a charref
* and continue the transcoding phase, hoping the error
* did not mangle the encoder state.
*/
charrefLen = xmlSerializeDecCharRef((char *) charref, cur);
xmlBufferShrink(in, len);
xmlBufferGrow(out, charrefLen * 4);
written = out->size - out->use - 1;
toconv = charrefLen;
ret = xmlEncOutputChunk(handler, &out->content[out->use], &written,
charref, &toconv);
if ((ret < 0) || (toconv != charrefLen))
return(XML_ENC_ERR_INTERNAL);
out->use += written;
writtentot += written;
out->content[out->use] = 0;
goto retry;
}
return(writtentot ? writtentot : ret);
}
/**
* xmlCharEncCloseFunc:
* @handler: char encoding transformation data structure
*
* Releases an xmlCharEncodingHandler. Must be called after
* a handler is no longer in use.
*
* Returns 0.
*/
int
xmlCharEncCloseFunc(xmlCharEncodingHandler *handler) {
if (handler == NULL)
return(0);
if (handler->flags & XML_HANDLER_STATIC)
return(0);
xmlFree(handler->name);
if (handler->ctxtDtor != NULL) {
handler->ctxtDtor(handler->inputCtxt);
handler->ctxtDtor(handler->outputCtxt);
}
xmlFree(handler);
return(0);
}
/**
* xmlByteConsumed:
* @ctxt: an XML parser context
*
* DEPRECATED: Don't use.
*
* This function provides the current index of the parser relative
* to the start of the current entity. This function is computed in
* bytes from the beginning starting at zero and finishing at the
* size in byte of the file if parsing a file. The function is
* of constant cost if the input is UTF-8 but can be costly if run
* on non-UTF-8 input.
*
* Returns the index in bytes from the beginning of the entity or -1
* in case the index could not be computed.
*/
long
xmlByteConsumed(xmlParserCtxtPtr ctxt) {
xmlParserInputPtr in;
if (ctxt == NULL)
return(-1);
in = ctxt->input;
if (in == NULL)
return(-1);
if ((in->buf != NULL) && (in->buf->encoder != NULL)) {
int unused = 0;
xmlCharEncodingHandler * handler = in->buf->encoder;
/*
* Encoding conversion, compute the number of unused original
* bytes from the input not consumed and subtract that from
* the raw consumed value, this is not a cheap operation
*/
if (in->end - in->cur > 0) {
unsigned char *convbuf;
const unsigned char *cur = (const unsigned char *)in->cur;
int toconv, ret;
convbuf = xmlMalloc(32000);
if (convbuf == NULL)
return(-1);
toconv = in->end - cur;
unused = 32000;
ret = xmlEncOutputChunk(handler, convbuf, &unused, cur, &toconv);
xmlFree(convbuf);
if (ret != XML_ENC_ERR_SUCCESS)
return(-1);
}
if (in->buf->rawconsumed < (unsigned long) unused)
return(-1);
return(in->buf->rawconsumed - unused);
}
return(in->consumed + (in->cur - in->base));
}
/************************************************************************
* *
* Conversions To/From UTF8 encoding *
* *
************************************************************************/
static int
asciiToAscii(unsigned char* out, int *poutlen,
const unsigned char* in, int *pinlen,
void *vctxt ATTRIBUTE_UNUSED) {
const unsigned char *inend;
const unsigned char *instart = in;
int inlen, outlen, ret;
if (in == NULL) {
*pinlen = 0;
*poutlen = 0;
return(XML_ENC_ERR_SUCCESS);
}
inlen = *pinlen;
outlen = *poutlen;
if (outlen < inlen) {
inlen = outlen;
ret = XML_ENC_ERR_SPACE;
} else {
ret = inlen;
}
inend = in + inlen;
*poutlen = inlen;
*pinlen = inlen;
while (in < inend) {
unsigned c = *in;
if (c >= 0x80) {
*poutlen = in - instart;
*pinlen = in - instart;
return(XML_ENC_ERR_INPUT);
}
in++;
*out++ = c;
}
return(ret);
}
static int
latin1ToUTF8(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
unsigned char* outstart = out;
const unsigned char* instart = in;
unsigned char* outend;
const unsigned char* inend;
int ret = XML_ENC_ERR_SPACE;
if ((out == NULL) || (in == NULL) || (outlen == NULL) || (inlen == NULL))
return(XML_ENC_ERR_INTERNAL);
outend = out + *outlen;
inend = in + *inlen;
while (in < inend) {
unsigned c = *in;
if (c < 0x80) {
if (out >= outend)
goto done;
*out++ = c;
} else {
if (outend - out < 2)
goto done;
*out++ = (c >> 6) | 0xC0;
*out++ = (c & 0x3F) | 0x80;
}
in++;
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
/**
* isolat1ToUTF8:
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of ISO Latin 1 chars
* @inlen: the length of @in
*
* Take a block of ISO Latin 1 chars in and try to convert it to an UTF-8
* block of chars out.
*
* Returns the number of bytes written or an XML_ENC_ERR code.
*
* The value of @inlen after return is the number of octets consumed
* if the return value is positive, else unpredictable.
* The value of @outlen after return is the number of octets produced.
*/
int
isolat1ToUTF8(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen) {
return(latin1ToUTF8(out, outlen, in, inlen, NULL));
}
static int
UTF8ToUTF8(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
int len;
int ret;
if (in == NULL) {
*inlen = 0;
*outlen = 0;
return(XML_ENC_ERR_SUCCESS);
}
if (*outlen < *inlen) {
len = *outlen;
ret = XML_ENC_ERR_SPACE;
} else {
len = *inlen;
ret = len;
}
memcpy(out, in, len);
*outlen = len;
*inlen = len;
return(ret);
}
#ifdef LIBXML_OUTPUT_ENABLED
static int
UTF8ToLatin1(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
const unsigned char* outend;
const unsigned char* outstart = out;
const unsigned char* instart = in;
const unsigned char* inend;
unsigned c;
int ret = XML_ENC_ERR_SPACE;
if ((out == NULL) || (outlen == NULL) || (inlen == NULL))
return(XML_ENC_ERR_INTERNAL);
if (in == NULL) {
*inlen = 0;
*outlen = 0;
return(XML_ENC_ERR_SUCCESS);
}
inend = in + *inlen;
outend = out + *outlen;
while (in < inend) {
if (out >= outend)
goto done;
c = *in;
if (c < 0x80) {
*out++ = c;
} else if ((c >= 0xC2) && (c <= 0xC3)) {
if (inend - in < 2)
break;
in++;
*out++ = (unsigned char) ((c << 6) | (*in & 0x3F));
} else {
ret = XML_ENC_ERR_INPUT;
goto done;
}
in++;
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
/**
* UTF8Toisolat1:
* @out: a pointer to an array of bytes to store the result
* @outlen: the length of @out
* @in: a pointer to an array of UTF-8 chars
* @inlen: the length of @in
*
* Take a block of UTF-8 chars in and try to convert it to an ISO Latin 1
* block of chars out.
*
* Returns the number of bytes written or an XML_ENC_ERR code.
*
* The value of @inlen after return is the number of octets consumed
* if the return value is positive, else unpredictable.
* The value of @outlen after return is the number of octets produced.
*/
int
UTF8Toisolat1(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen) {
if ((out == NULL) || (outlen == NULL) || (in == NULL) || (inlen == NULL))
return(XML_ENC_ERR_INTERNAL);
return(UTF8ToLatin1(out, outlen, in, inlen, NULL));
}
#endif /* LIBXML_OUTPUT_ENABLED */
static int
UTF16LEToUTF8(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
const unsigned char *instart = in;
const unsigned char *inend = in + (*inlen & ~1);
unsigned char *outstart = out;
unsigned char *outend = out + *outlen;
unsigned c, d;
int ret = XML_ENC_ERR_SPACE;
while (in < inend) {
c = in[0] | (in[1] << 8);
if (c < 0x80) {
if (out >= outend)
goto done;
out[0] = c;
in += 2;
out += 1;
} else if (c < 0x800) {
if (outend - out < 2)
goto done;
out[0] = (c >> 6) | 0xC0;
out[1] = (c & 0x3F) | 0x80;
in += 2;
out += 2;
} else if ((c & 0xF800) != 0xD800) {
if (outend - out < 3)
goto done;
out[0] = (c >> 12) | 0xE0;
out[1] = ((c >> 6) & 0x3F) | 0x80;
out[2] = (c & 0x3F) | 0x80;
in += 2;
out += 3;
} else {
/* Surrogate pair */
if ((c & 0xFC00) != 0xD800) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (inend - in < 4)
break;
d = in[2] | (in[3] << 8);
if ((d & 0xFC00) != 0xDC00) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (outend - out < 4)
goto done;
c = (c << 10) + d - ((0xD800 << 10) + 0xDC00 - 0x10000);
out[0] = (c >> 18) | 0xF0;
out[1] = ((c >> 12) & 0x3F) | 0x80;
out[2] = ((c >> 6) & 0x3F) | 0x80;
out[3] = (c & 0x3F) | 0x80;
in += 4;
out += 4;
}
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
#ifdef LIBXML_OUTPUT_ENABLED
static int
UTF8ToUTF16LE(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
const unsigned char *instart = in;
const unsigned char *inend;
unsigned char *outstart = out;
unsigned char *outend;
unsigned c, d;
int ret = XML_ENC_ERR_SPACE;
/* UTF16LE encoding has no BOM */
if ((out == NULL) || (outlen == NULL) || (inlen == NULL))
return(XML_ENC_ERR_INTERNAL);
if (in == NULL) {
*outlen = 0;
*inlen = 0;
return(0);
}
inend = in + *inlen;
outend = out + (*outlen & ~1);
while (in < inend) {
c = in[0];
if (c < 0x80) {
if (out >= outend)
goto done;
out[0] = c;
out[1] = 0;
in += 1;
out += 2;
} else {
int i, len;
unsigned min;
if (c < 0xE0) {
if (c < 0xC2) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
c &= 0x1F;
len = 2;
min = 0x80;
} else if (c < 0xF0) {
c &= 0x0F;
len = 3;
min = 0x800;
} else {
c &= 0x0F;
len = 4;
min = 0x10000;
}
if (inend - in < len)
break;
for (i = 1; i < len; i++) {
if ((in[i] & 0xC0) != 0x80) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
c = (c << 6) | (in[i] & 0x3F);
}
if ((c < min) ||
((c >= 0xD800) && (c <= 0xDFFF)) ||
(c > 0x10FFFF)) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (c < 0x10000) {
if (out >= outend)
goto done;
out[0] = c & 0xFF;
out[1] = c >> 8;
out += 2;
} else {
if (outend - out < 4)
goto done;
c -= 0x10000;
d = (c & 0x03FF) | 0xDC00;
c = (c >> 10) | 0xD800;
out[0] = c & 0xFF;
out[1] = c >> 8;
out[2] = d & 0xFF;
out[3] = d >> 8;
out += 4;
}
in += len;
}
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
static int
UTF8ToUTF16(unsigned char* outb, int *outlen,
const unsigned char* in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
if (in == NULL) {
/*
* initialization, add the Byte Order Mark for UTF-16LE
*/
if (*outlen >= 2) {
outb[0] = 0xFF;
outb[1] = 0xFE;
*outlen = 2;
*inlen = 0;
return(2);
}
*outlen = 0;
*inlen = 0;
return(0);
}
return (UTF8ToUTF16LE(outb, outlen, in, inlen, NULL));
}
#endif /* LIBXML_OUTPUT_ENABLED */
static int
UTF16BEToUTF8(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
const unsigned char *instart = in;
const unsigned char *inend = in + (*inlen & ~1);
unsigned char *outstart = out;
unsigned char *outend = out + *outlen;
unsigned c, d;
int ret = XML_ENC_ERR_SPACE;
while (in < inend) {
c = (in[0] << 8) | in[1];
if (c < 0x80) {
if (out >= outend)
goto done;
out[0] = c;
in += 2;
out += 1;
} else if (c < 0x800) {
if (outend - out < 2)
goto done;
out[0] = (c >> 6) | 0xC0;
out[1] = (c & 0x3F) | 0x80;
in += 2;
out += 2;
} else if ((c & 0xF800) != 0xD800) {
if (outend - out < 3)
goto done;
out[0] = (c >> 12) | 0xE0;
out[1] = ((c >> 6) & 0x3F) | 0x80;
out[2] = (c & 0x3F) | 0x80;
in += 2;
out += 3;
} else {
/* Surrogate pair */
if ((c & 0xFC00) != 0xD800) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (inend - in < 4)
break;
d = (in[2] << 8) | in[3];
if ((d & 0xFC00) != 0xDC00) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (outend - out < 4)
goto done;
c = (c << 10) + d - ((0xD800 << 10) + 0xDC00 - 0x10000);
out[0] = (c >> 18) | 0xF0;
out[1] = ((c >> 12) & 0x3F) | 0x80;
out[2] = ((c >> 6) & 0x3F) | 0x80;
out[3] = (c & 0x3F) | 0x80;
in += 4;
out += 4;
}
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
#ifdef LIBXML_OUTPUT_ENABLED
static int
UTF8ToUTF16BE(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
const unsigned char *instart = in;
const unsigned char *inend;
unsigned char *outstart = out;
unsigned char *outend;
unsigned c, d;
int ret = XML_ENC_ERR_SPACE;
/* UTF-16BE has no BOM */
if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
if (in == NULL) {
*outlen = 0;
*inlen = 0;
return(0);
}
inend = in + *inlen;
outend = out + (*outlen & ~1);
while (in < inend) {
c = in[0];
if (c < 0x80) {
if (out >= outend)
goto done;
out[0] = 0;
out[1] = c;
in += 1;
out += 2;
} else {
int i, len;
unsigned min;
if (c < 0xE0) {
if (c < 0xC2) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
c &= 0x1F;
len = 2;
min = 0x80;
} else if (c < 0xF0) {
c &= 0x0F;
len = 3;
min = 0x800;
} else {
c &= 0x0F;
len = 4;
min = 0x10000;
}
if (inend - in < len)
break;
for (i = 1; i < len; i++) {
if ((in[i] & 0xC0) != 0x80) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
c = (c << 6) | (in[i] & 0x3F);
}
if ((c < min) ||
((c >= 0xD800) && (c <= 0xDFFF)) ||
(c > 0x10FFFF)) {
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (c < 0x10000) {
if (out >= outend)
goto done;
out[0] = c >> 8;
out[1] = c & 0xFF;
out += 2;
} else {
if (outend - out < 4)
goto done;
c -= 0x10000;
d = (c & 0x03FF) | 0xDC00;
c = (c >> 10) | 0xD800;
out[0] = c >> 8;
out[1] = c & 0xFF;
out[2] = d >> 8;
out[3] = d & 0xFF;
out += 4;
}
in += len;
}
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
#endif /* LIBXML_OUTPUT_ENABLED */
#if defined(LIBXML_OUTPUT_ENABLED) && defined(LIBXML_HTML_ENABLED)
static int
UTF8ToHtmlWrapper(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen,
void *vctxt ATTRIBUTE_UNUSED) {
return(UTF8ToHtml(out, outlen, in, inlen));
}
#endif
#if !defined(LIBXML_ICONV_ENABLED) && !defined(LIBXML_ICU_ENABLED) && \
defined(LIBXML_ISO8859X_ENABLED)
static int
UTF8ToISO8859x(unsigned char *out, int *outlen,
const unsigned char *in, int *inlen, void *vctxt) {
const unsigned char *xlattable = vctxt;
const unsigned char *instart = in;
const unsigned char *inend;
unsigned char *outstart = out;
unsigned char *outend;
int ret = XML_ENC_ERR_SPACE;
if (in == NULL) {
/*
* initialization nothing to do
*/
*outlen = 0;
*inlen = 0;
return(XML_ENC_ERR_SUCCESS);
}
inend = in + *inlen;
outend = out + *outlen;
while (in < inend) {
unsigned d = *in;
if (d < 0x80) {
if (out >= outend)
goto done;
in += 1;
} else if (d < 0xE0) {
unsigned c;
if (inend - in < 2)
break;
c = in[1] & 0x3F;
d = d & 0x1F;
d = xlattable [48 + c + xlattable [d] * 64];
if (d == 0) {
/* not in character set */
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (out >= outend)
goto done;
in += 2;
} else if (d < 0xF0) {
unsigned c1;
unsigned c2;
if (inend - in < 3)
break;
c1 = in[1] & 0x3F;
c2 = in[2] & 0x3F;
d = d & 0x0F;
d = xlattable [48 + c2 + xlattable [48 + c1 +
xlattable [32 + d] * 64] * 64];
if (d == 0) {
/* not in character set */
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (out >= outend)
goto done;
in += 3;
} else {
/* cannot transcode >= U+010000 */
ret = XML_ENC_ERR_INPUT;
goto done;
}
*out++ = d;
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
static int
ISO8859xToUTF8(unsigned char* out, int *outlen,
const unsigned char* in, int *inlen, void *vctxt) {
unsigned short const *unicodetable = vctxt;
const unsigned char* instart = in;
const unsigned char* inend;
unsigned char* outstart = out;
unsigned char* outend;
int ret = XML_ENC_ERR_SPACE;
outend = out + *outlen;
inend = in + *inlen;
while (in < inend) {
unsigned c = *in;
if (c < 0x80) {
if (out >= outend)
goto done;
*out++ = c;
} else {
c = unicodetable[c - 0x80];
if (c == 0) {
/* undefined code point */
ret = XML_ENC_ERR_INPUT;
goto done;
}
if (c < 0x800) {
if (outend - out < 2)
goto done;
*out++ = ((c >> 6) & 0x1F) | 0xC0;
*out++ = (c & 0x3F) | 0x80;
} else {
if (outend - out < 3)
goto done;
*out++ = ((c >> 12) & 0x0F) | 0xE0;
*out++ = ((c >> 6) & 0x3F) | 0x80;
*out++ = (c & 0x3F) | 0x80;
}
}
in += 1;
}
ret = out - outstart;
done:
*outlen = out - outstart;
*inlen = in - instart;
return(ret);
}
#endif
|