1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008
|
/*
* bif_xper.c
*
* $Id$
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef MALLOC_DEBUG
/*#define XMLPARSER_FEED_DEBUG*/
#endif
#include "libutil.h"
/* IvAn/ParseDTD/000721 system parser is wiped out
#include <xmlparse.h> */
#include "sqlnode.h"
#include "sqlbif.h"
#include "wi.h"
#include "Dk.h"
#include "bif_xper.h"
#include "xml.h" /* IvAn/TextXmlIndex/000814 */
#include "bif_text.h" /* IvAn/TextXperIndex/000814 */
#include "security.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "langfunc.h"
#include "xmlparser.h"
#include "xmlparser_impl.h"
#ifdef __cplusplus
}
#endif
#include "multibyte.h"
#include "xpathp_impl.h"
#undef DV_LONG_STRING /* for safety */
#undef DV_SHORT_STRING /* for safety */
typedef struct xper_ns_s
{
int xpns_depth; /*!< Depth where it's located */
caddr_t xpns_name; /*!< Name of namespace */
caddr_t xpns_uri; /*!< URI provided as a 'value' of namespace */
size_t xpns_pos; /*!< Offset in a blob */
size_t xpns_old_pos; /*!< Offset in a source blob, for use in bif_xper_copy() */
}
xper_ns_t;
/*! Property list for textual data */
typedef struct xper_textplist_s
{
int xptp_depth; /*!< Depth where it's located */
caddr_t xptp_lang_name; /*!< Name of language as specified in xml:lang attribute */
lang_handler_t *xptp_lang_handler; /*!< Handler for language named in xptp_lang_name */
}
xper_textplist_t;
/* IvAn/TextXperIndex/000815 Word counter added */
/* data structure for parser's callback functions */
typedef struct xper_ctx_s
{
buffer_desc_t *xpc_buf;
it_cursor_t *xpc_itc;
dk_set_t xpc_poss; /*!< Stack of element positions */
dk_set_t xpc_nss; /*!< Stack of namespace attributes */
dk_set_t xpc_textplists; /*!< Stack of plists of texts */
int xpc_depth; /* current element depth */
long xpc_et_pos;
int xpc_tn_length1; /* length of last open tag name */
int xpc_tn_length2; /* length of last close tag name */
caddr_t xpc_pdir;
xper_doc_t *xpc_doc;
/*! \brief Parameter to manage word counting
In some cases, two or more adjacent tags should share one word number.
xpc_word_hider keeps the type of previous indexed item:
XML_MKUP_STAG for opening tag,
XML_MKUP_ETAG for closing tag,
XML_MKUP_STRING for plain text with words.
0 at the beginning of text.
*/
char xpc_word_hider;
wpos_t xpc_main_word_ctr; /*!< counter of words scanned in main text */
wpos_t xpc_attr_word_ctr; /*!< counter of words scanned in attributes */
/*! \brief Buffer for storing words which are not yet counted or indexed
It is possible that one string value will be loaded into persistent XML by a
sequence of calls of cb_character callback, so some words may be split
between two or more calls. To process such words, an accumulating buffer has
added, where a word can be concatenated from its parts. */
utf8char *xpc_text_buf;
/*! \brief Number of chars used in xpc_text_buf */
int xpc_text_buf_use;
/*! \brief Size of xpc_text_buf */
int xpc_text_buf_size;
caddr_t vt_batch; /*!< batch of word indexing information, or NULL */
int xpc_index_attrs; /*!< Flags if attributes should be indexed */
id_hash_t *xpc_id_dict;
caddr_t xpc_src_filename;
FILE *xpc_src_file;
vxml_parser_t *xpc_parser;
dk_set_t xpc_cut_chain; /* Chain of boxes, which will be written into the copy */
dk_set_t xpc_cut_namespaces; /* All namespaces, listed in the cut */
}
xper_ctx_t;
/* xpc_poss is used in such way:
* it is a stack of positions.
* when a start tag is encountered, first we look at the top position.
* If that value is positive, than it is a position of the parent.
* If it is negative, than absolute value is a position of the left sibling.
* And if the stack is empty than we have a first start tag. (It should be root record.)
* Than a position of current tag element will be
* placed on the top of the stack. If top position value is negative then its value
* will be replaced with new one (positive). In the other case new value will be
* pushed on the stack.
* When an end tag is encountered and if the top value is negative, we discard it
* from the stack. Then we negate the top value, marking it as a closed.
*/
/* data structure for a start tag */
struct xper_stag_s
{
char type;
const char *name;
const char **xmlns_atts;
int xmlns_atts_count;
vxml_parser_attrdata_t *attrdata;
int main_atts_count;
long parent;
long left;
long position; /* of the tag in a blob */
int depth;
dk_set_t *nss_ptr;
dk_set_t *textplists_ptr;
int recalc_wrs;
int store_wrs;
xe_word_ranges_t wrs;
lang_handler_t *lh;
};
typedef struct xper_stag_s xper_stag_t;
/*
* Structure of start tag info in a blob:
* 1 DV_XML_MARKUP
* 4 size of the record
* 1 type of markup XML_MKUP_XXX
* X name as a DV_X_STRING
* 4 parent position
* 4 left sibling
* 4 right sibling
* 4 first child
* 4 end tag position
* 4 word position of start tag
* 4 word position of end tag
* 1 children directory flag
* 4 namespace (offset to value or 0)
* 4 2 * number of attributes (no of names + no of values) (2 low-end bytes) | length of addons (2 high-end bytes)
* X [ optional ] addon bytes;
* X attribute 1 name as a DV_X_STRING
* X attribute 1 value as a DV_X_STRING
* ...
* X attribute N name as a DV_X_STRING
* X attribute N value as a DV_X_STRING
* XXXXXXXXXXXXXXX
*/
#define STR_NAME_OFF 6
#define STR_PARENT_OFF (0 * 4)
#define STR_LEFT_SIBLING_OFF (1 * 4)
#define STR_RIGHT_SIBLING_OFF (2 * 4)
#define STR_FIRST_CHILD_OFF (3 * 4)
#define STR_END_TAG_OFF (4 * 4)
#define STR_START_WORD_OFF (5 * 4)
#define STR_END_WORD_OFF (6 * 4)
#define STR_NS_OFF (7 * 4 + 1)
#define STR_ATTR_NO_OFF (8 * 4 + 1)
#define STR_ADDON_OR_ATTR_OFF (9 * 4 + 1)
#define XPER_ROOT_POS XPACK_PREFIX_LEN /* position of " root" in resulting document's BLOB */
#define MIN_START_ROOT_RECORD_SZ (STR_NAME_OFF + 7 /* = length of box " root" */ + STR_ADDON_OR_ATTR_OFF)
#define END_ROOT_RECORD_SZ (STR_NAME_OFF + 7 /* = length of box " root" */)
#define GET_LOCAL_LH(ctx) ( \
(NULL != ctx->xpc_textplists) ? \
((xper_textplist_t *)(ctx->xpc_textplists->data))->xptp_lang_handler : \
ctx->xpc_doc->xd_default_lh )
/* increment step for page number directory */
#define AOP_STEP (PAGE_DATA_SZ / sizeof (dp_addr_t))
static caddr_t start_tag_record (xper_stag_t * d);
static caddr_t end_tag_record (const char *name);
static void xper_blob_append_data (xper_ctx_t * ctx, const char *data, size_t len);
static void set_long_in_blob (xper_ctx_t * ctx, long pos, long value);
static void xper_destroy_ctx (xper_ctx_t * ctx);
static void xper_blob_append_box (xper_ctx_t * ctx, caddr_t box);
static caddr_t xper_get_attribute (xper_entity_t * xpe, const char * name, int name_len);
static xml_entity_t *xp_reference (query_instance_t * qi, caddr_t base, caddr_t ref, xml_doc_t * from_doc, caddr_t * err_ret);
dtd_t *xp_get_addon_dtd (xml_entity_t * xe);
static caddr_t xp_build_expanded_name (xper_entity_t *xpe);
#ifdef XPER_DEBUG
#define xper_dbg_print(fmt) fprintf (stderr, fmt)
#define xper_dbg_print_1(fmt, arg) fprintf (stderr, fmt, arg)
#else
#define xper_dbg_print(fmt)
#define xper_dbg_print_1(fmt, arg)
#endif
#ifdef XPER_DEBUG
static long xper_entity_alloc_ctr = 0;
static long xper_entity_free_ctr = 0;
#endif
#define XPER_FREE_XPD(xpd) \
do \
{ \
if (NULL != (xpd)->xd_dtd) \
dtd_release ((xpd)->xd_dtd); \
dk_free_box ((caddr_t)(xpd->xd_id_dict)); \
dk_free_box (xpd->xd_id_scan); \
dk_free ((xpd), -1 /* not sizeof (xper_doc_t) because it may be doc made by lazy loader */); \
} while (0)
/* size of DV_SHORT_STRING_SERIAL or DV_STRING
* depending on the size of a string
*/
static size_t
dv_string_size (const char *s)
{
size_t size = strlen (s);
size += ((size > 255) ? 5 : 2);
return size;
}
static long
full_dv_string_length (const unsigned char *s)
{
if (DV_SHORT_STRING_SERIAL == *s)
return s[1] + 2;
else if (DV_STRING != *s && DV_ARRAY_OF_POINTER != *s)
sqlr_new_error ("XE000", "XP9A1", "Error while accessing XML_PERSISTENT: invalid string type %d", *s);
s++;
return LONG_REF_NA (s) + 5;
}
static size_t
skip_string_length (unsigned char **ptr)
{
size_t namelen;
if (DV_STRING == **ptr || DV_ARRAY_OF_POINTER == **ptr)
{
namelen = LONG_REF_NA (*ptr + 1);
*ptr += 5;
return namelen;
}
if (DV_SHORT_STRING_SERIAL == **ptr)
{
namelen = (*ptr)[1];
*ptr += 2;
return namelen;
}
sqlr_new_error ("XE000", "XP9A2", "Error while accessing XML_PERSISTENT: invalid string type %d", **ptr);
return 0; /* Never reached */
}
char *
serialize_string (char *ptr, const char *s)
{
size_t size = strlen (s);
if (size > 255)
{
*ptr++ = (char) DV_STRING;
LONG_SET_NA (ptr, size);
ptr += 4;
}
else
{
*ptr++ = (char) DV_SHORT_STRING_SERIAL;
*ptr++ = (unsigned char) size;
}
memcpy (ptr, s, size);
return (ptr + size);
}
ptrlong
find_ns (const char *ns, size_t len, dk_set_t * nss_ptr)
{
if (!nss_ptr)
return 0;
DO_SET (xper_ns_t *, ns_i, nss_ptr)
if (strlen (ns_i->xpns_name) == len &&
(0 == len || 0 == memcmp (ns, ns_i->xpns_name, len)))
return ns_i->xpns_pos;
END_DO_SET ();
return 0;
}
static caddr_t
start_tag_record (xper_stag_t * d)
{
caddr_t res;
size_t size = 1 + STR_ADDON_OR_ATTR_OFF; /* size of fixed-length data members */
unsigned char *rec;
char *colon;
ptrlong nspos;
ptrlong find_res;
long att_word;
ptrlong tmp;
lang_handler_t *lh = d->lh;
wpos_t total_attrs_word_count = 0;
tag_attr_t *attr, *attr_start = d->attrdata->local_attrs;
tag_attr_t *attr_end = attr_start + d->attrdata->local_attrs_count;
nsdecl_t *nsd, *nsd_start = d->attrdata->local_nsdecls;
nsdecl_t *nsd_end = nsd_start + d->attrdata->local_nsdecls_count;
size += dv_string_size (d->name);
if (d->store_wrs)
size += (3 * 4); /* 3 addon longs for attribute indexes */
for (nsd = nsd_start; nsd < nsd_end; nsd++)
{
caddr_t attrname = ((uname___empty == nsd->nsd_prefix) ? uname_xmlns : box_sprintf(10 + box_length (nsd->nsd_prefix), "xmlns:%s", nsd->nsd_prefix));
ccaddr_t uri = nsd->nsd_uri;
ptrlong attrval_offs;
xper_ns_t *ns_data;
size += dv_string_size (attrname);
attrval_offs = size;
size += dv_string_size (uri);
colon = strrchr (attrname, ':');
ns_data = (xper_ns_t *) dk_alloc (sizeof (xper_ns_t));
ns_data->xpns_name = box_copy (nsd->nsd_prefix);
ns_data->xpns_depth = d->depth;
ns_data->xpns_pos = d->position + attrval_offs + 5;
dk_set_push (d->nss_ptr, ns_data);
dk_free_box (attrname);
}
for (attr = attr_start; attr < attr_end; attr++)
{
const char *attrname = attr->ta_raw_name.lm_memblock;
const char *attrvalue = attr->ta_value;
ptrlong attrval_offs;
size += dv_string_size (attrname);
attrval_offs = size;
size += dv_string_size (attrvalue);
colon = strrchr (attrname, ':');
if (('x' != attrname[0]) || ('m' != attrname[1]) || ('l' != attrname[2]))
{ /* Looks like plain attribute */
if (NULL != colon)
size += 10; /* attribute name with namespace */
continue;
}
if (0 == strcmp (attrname, "xml:lang"))
{
xper_textplist_t *textplist_data;
if (NULL != d->textplists_ptr[0])
{ /* If we're already inside element with specified xml:lang */
xper_textplist_t *prev_plist = (xper_textplist_t *) d->textplists_ptr[0]->data;
if (0 == strcmp (attrvalue, prev_plist->xptp_lang_name))
continue; /* redundant xml:lang may be ignored */
lh = lh_get_handler (attrvalue);
if (lh == prev_plist->xptp_lang_handler)
continue; /* handler is common for both local and outer languages */
}
else
{
lh = lh_get_handler (attrvalue);
}
textplist_data = (xper_textplist_t *) dk_alloc (sizeof (xper_textplist_t));
textplist_data->xptp_lang_name = box_string (attrvalue);
textplist_data->xptp_lang_handler = lh;
textplist_data->xptp_depth = d->depth;
dk_set_push (d->textplists_ptr, textplist_data);
continue;
}
}
if (d->recalc_wrs)
{
for (attr = attr_start; attr < attr_end; attr++)
{
const char *attrvalue = attr->ta_value;
wpos_t word_count;
if (&lh__xany == lh) /* Optimization for most common case */
word_count = elh__xany__UTF8.elh_count_words (attrvalue, strlen (attrvalue), lh__xany.lh_is_vtb_word);
else
word_count = lh_count_words (&eh__UTF8, lh, attrvalue, strlen (attrvalue), lh->lh_is_vtb_word);
total_attrs_word_count += 2 + word_count;
}
#ifndef SKIP_XMLNS_WORD_POS
for (nsd = nsd_start; nsd < nsd_end; nsd++)
{
ccaddr_t attrvalue = nsd->nsd_uri;
wpos_t word_count;
if (&lh__xany == lh) /* Optimization for most common case */
word_count = elh__xany__UTF8.elh_count_words (attrvalue, strlen (attrvalue), lh__xany.lh_is_vtb_word);
else
word_count = lh_count_words (&eh__UTF8, lh, attrvalue, strlen (attrvalue), lh->lh_is_vtb_word);
total_attrs_word_count += 2 + word_count;
}
#endif
}
if (d->recalc_wrs)
d->wrs.xewr_attr_this_end = d->wrs.xewr_attr_beg + total_attrs_word_count;
/* now we have size of the record */
res = dk_alloc_box_zero (size, DV_XML_MARKUP);
rec = (unsigned char *) res;
*rec++ = d->type;
rec = (unsigned char *) serialize_string ((char *) rec, d->name);
LONG_SET_NA (rec + STR_PARENT_OFF, d->parent);
LONG_SET_NA (rec + STR_LEFT_SIBLING_OFF, d->left);
/* IvAn/TextXperIndex/000815 */
LONG_SET_NA (rec + STR_START_WORD_OFF, d->wrs.xewr_main_beg);
colon = strrchr (d->name, ':');
nspos = find_ns (d->name, colon ? colon - d->name : 0, d->nss_ptr);
if (nspos)
LONG_SET_NA (rec + STR_NS_OFF, nspos);
rec += STR_ATTR_NO_OFF;
att_word = 2 * (d->attrdata->local_attrs_count + d->attrdata->local_nsdecls_count);
if (d->store_wrs)
{
att_word |= ((3 * 4) << 16); /* 2 low-end bytes for attr count | 2 high-end bytes for addon */
LONG_SET_NA (rec, att_word);
rec += 4;
tmp = d->wrs.xewr_attr_beg;
LONG_SET_NA (rec, tmp);
rec += 4;
tmp = d->wrs.xewr_attr_this_end;
LONG_SET_NA (rec, tmp);
rec += 4;
tmp = d->wrs.xewr_attr_tree_end;
LONG_SET_NA (rec, tmp); /* may be filled later by cb_element_end */
rec += 4;
}
else
{
LONG_SET_NA (rec, att_word);
rec += 4;
}
/* attributes */
for (nsd = nsd_start; nsd < nsd_end; nsd++)
{
caddr_t attrname = ((uname___empty == nsd->nsd_prefix) ? uname_xmlns : box_sprintf(10 + box_length (nsd->nsd_prefix), "xmlns:%s", nsd->nsd_prefix));
ccaddr_t uri = nsd->nsd_uri;
rec = (unsigned char *) serialize_string ((char *) rec, attrname);
rec = (unsigned char *) serialize_string ((char *) rec, uri);
dk_free_box (attrname);
}
for (attr = attr_start; attr < attr_end; attr++)
{
const char *attrname = attr->ta_raw_name.lm_memblock;
const char *attrvalue = attr->ta_value;
colon = 0;
if (('x' != attrname[0]) || ('m' != attrname[1]) || ('l' != attrname[2]))
colon = strrchr (attrname, ':');
if (colon)
{
*rec++ = DV_ARRAY_OF_POINTER;
nspos = dv_string_size (attrname) + 5;
LONG_SET_NA (rec, nspos);
rec += 4;
rec = (unsigned char *) serialize_string ((char *) rec, attrname);
*rec++ = DV_LONG_INT;
find_res = find_ns (attrname, colon - attrname, d->nss_ptr);
LONG_SET_NA (rec, find_res);
rec += 4;
}
else
rec = (unsigned char *) serialize_string ((char *) rec, attrname);
rec = (unsigned char *) serialize_string ((char *) rec, attrvalue);
}
if (rec != (unsigned char *) (res + size))
GPF_T;
return res;
}
caddr_t
end_tag_record (const char *name)
{
size_t nlen = dv_string_size (name);
caddr_t res = dk_alloc_box (nlen + 1, DV_XML_MARKUP);
unsigned char *rec = (unsigned char *) res;
*rec++ = XML_MKUP_ETAG;
rec = (unsigned char *) serialize_string ((char *) rec, name);
return res;
}
void xper_blob_log_page_to_dir (blob_handle_t *ctx_bh, size_t page_idx, dp_addr_t page_addr)
{
if (NULL == ctx_bh->bh_pages)
{
ctx_bh->bh_pages = (dp_addr_t *) dk_alloc_box_zero ((BL_DPS_ON_ROW-1) * sizeof (dp_addr_t), DV_BIN);
ctx_bh->bh_page_dir_complete = 1;
}
else
{
unsigned old_pages_bufsize = box_length (ctx_bh->bh_pages);
unsigned old_n_pages = old_pages_bufsize / sizeof (dp_addr_t);
if (old_n_pages <= page_idx)
{
int new_n_pages = ((1 + old_n_pages / AOP_STEP) * AOP_STEP);
size_t new_dir_sz = new_n_pages * sizeof (dp_addr_t);
dp_addr_t *tmp = (dp_addr_t *) dk_alloc_box_zero (new_dir_sz, DV_BIN); /* not DV_ARRAY_OF_LONG */
memcpy (tmp, ctx_bh->bh_pages, old_pages_bufsize);
dk_free_box ((box_t) ctx_bh->bh_pages);
ctx_bh->bh_pages = tmp;
}
}
ctx_bh->bh_pages[page_idx] = page_addr;
}
#ifndef PAGES_COUNT_FOR_DISKBYTES
#define PAGES_COUNT_FOR_DISKBYTES(diskbytes) (((unsigned)(diskbytes) + PAGE_DATA_SZ-1) / PAGE_DATA_SZ)
#endif
void xper_blob_truncate_dir_buf (blob_handle_t *bh)
{
if (NULL != bh->bh_pages)
{
unsigned old_n_pages = box_length (bh->bh_pages) / sizeof (dp_addr_t);
unsigned new_n_pages = PAGES_COUNT_FOR_DISKBYTES (bh->bh_length);
if (old_n_pages > new_n_pages)
{
size_t new_dir_sz = new_n_pages * sizeof (dp_addr_t);
dp_addr_t *tmp = (dp_addr_t *) dk_alloc_box_zero (new_dir_sz, DV_BIN); /* not DV_ARRAY_OF_LONG */
memcpy (tmp, bh->bh_pages, new_dir_sz);
dk_free_box ((box_t) bh->bh_pages);
bh->bh_pages = tmp;
}
}
}
void
xper_blob_append_data (xper_ctx_t * ctx, const char *data, size_t len)
{
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
size_t filled;
buffer_desc_t *buf;
size_t len1;
ASSERT_OUTSIDE_MAP (ctx->xpc_itc->itc_tree, ctx->xpc_itc->itc_page);
ITC_FAIL (ctx->xpc_itc)
{
filled = LONG_REF/*_NA*/ (ctx->xpc_buf->bd_buffer + DP_BLOB_LEN);
while (len)
{
if (filled >= PAGE_DATA_SZ)
{
size_t old_n_pages = BL_N_PAGES(ctx_bh->bh_length); /* number of new pages is old_n_pages + 1 */
buf = it_new_page (ctx->xpc_itc->itc_tree, ctx->xpc_itc->itc_page,
DPF_BLOB, 0, 0);
#ifdef DEBUG
if (0 == buf->bd_page)
GPF_T;
#endif
xper_blob_log_page_to_dir (ctx_bh, old_n_pages, buf->bd_page);
ITC_LEAVE_MAPS (ctx->xpc_itc);
if (!buf)
{
xper_destroy_ctx (ctx);
log_error ("Out of disk space for database");
sqlr_new_error ("XE000", "XP9A7", "Out of disk space for database while parsing XML");
return;
}
LONG_SET/*_NA*/ (ctx->xpc_buf->bd_buffer + DP_BLOB_LEN, PAGE_DATA_SZ);
LONG_SET/*_NA*/ (ctx->xpc_buf->bd_buffer + DP_OVERFLOW, buf->bd_page);
buf_set_dirty (ctx->xpc_buf);
page_leave_outside_map (ctx->xpc_buf);
ctx->xpc_buf = NULL;
ctx->xpc_buf = buf;
filled = 0;
}
len1 = ((len <= PAGE_DATA_SZ - filled) ? len : PAGE_DATA_SZ - filled);
memcpy (ctx->xpc_buf->bd_buffer + DP_DATA + filled, data, len1);
filled += len1;
ctx_bh->bh_diskbytes = ctx_bh->bh_length += len1;
len -= len1;
data += len1;
}
ITC_LEAVE_MAPS (ctx->xpc_itc);
}
ITC_FAILED
{
ITC_LEAVE_MAPS (ctx->xpc_itc);
xper_destroy_ctx (ctx);
log_error ("Out of disk space for database");
sqlr_new_error ("XE000", "XP102", "ITC error while parsing XML");
}
END_FAIL (ctx->xpc_itc);
LONG_SET/*_NA*/ (ctx->xpc_buf->bd_buffer + DP_OVERFLOW, 0);
LONG_SET/*_NA*/ (ctx->xpc_buf->bd_buffer + DP_BLOB_LEN, (int32) filled);
}
void
xper_blob_append_box (xper_ctx_t * ctx, caddr_t box)
{
unsigned char ccc[5];
dtp_t tag;
size_t len;
tag = box_tag (box);
len = box_length (box);
ccc[0] = tag;
if (DV_SHORT_STRING_SERIAL == tag)
{
ccc[1] = (unsigned char) len;
xper_blob_append_data (ctx, (const char *) ccc, 2);
}
else
{
LONG_SET_NA (ccc + 1, len);
xper_blob_append_data (ctx, (const char *) ccc, 5);
}
xper_blob_append_data (ctx, box, len);
}
void
blob_append_nchars (xper_ctx_t * ctx, const char *buf, size_t len)
{
unsigned char ccc[5];
dtp_t tag = ((len > 255) ? DV_STRING : DV_SHORT_STRING_SERIAL);
ccc[0] = tag;
if (DV_SHORT_STRING_SERIAL == tag)
{
ccc[1] = (unsigned char) len;
xper_blob_append_data (ctx, (const char *) ccc, 2);
}
else
{
LONG_SET_NA (ccc + 1, len);
xper_blob_append_data (ctx, (const char *) ccc, 5);
}
xper_blob_append_data (ctx, buf, len);
}
buffer_desc_t *
get_blob_page_for_read (it_cursor_t * itc, xper_doc_t * xpd, unsigned n)
{
buffer_desc_t *buf;
dp_addr_t nth_page;
nth_page = xpd->xpd_bh->bh_pages[n];
if (!page_wait_blob_access (itc, nth_page, &buf, PA_READ, xpd->xpd_bh, 1))
longjmp_splice (itc->itc_fail_context, RST_ERROR);
return buf;
}
buffer_desc_t *
get_blob_page_for_write (it_cursor_t * itc, xper_doc_t * xpd, unsigned n)
{
buffer_desc_t *buf;
dp_addr_t nth_page;
nth_page = xpd->xpd_bh->bh_pages[n];
if (!page_wait_blob_access (itc, nth_page, &buf, PA_WRITE, xpd->xpd_bh, 1))
longjmp_splice (itc->itc_fail_context, RST_ERROR);
return buf;
}
void
set_long_in_blob (xper_ctx_t * ctx, long pos, long value)
{
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
int last_page_no = (int) (ctx_bh->bh_length - 1) / PAGE_DATA_SZ;
int begin_page_no = pos / PAGE_DATA_SZ;
if (begin_page_no == last_page_no)
{ /* all data are located on the current page */
LONG_SET_NA (ctx->xpc_buf->bd_buffer + DP_DATA + pos % PAGE_DATA_SZ,
value);
}
else
{
buffer_desc_t *buf = NULL;
buf = get_blob_page_for_write (ctx->xpc_itc, ctx->xpc_doc, begin_page_no); /* the beginning is not on the current page */
if (pos % PAGE_DATA_SZ + 4 <= PAGE_DATA_SZ)
{
LONG_SET_NA (buf->bd_buffer + DP_DATA + pos % PAGE_DATA_SZ, value);
buf_set_dirty (buf);
page_leave_outside_map (buf);
}
else
{ /* the worst case - number is split between pages */
char cbuf[4];
int split = PAGE_DATA_SZ - pos % PAGE_DATA_SZ; /* 0 < split < 4 */
int next_page_no = begin_page_no+1;
LONG_SET_NA (cbuf, value);
memcpy (buf->bd_buffer + DP_DATA + pos % PAGE_DATA_SZ, cbuf, split);
buf_set_dirty (buf);
page_leave_outside_map (buf);
if (next_page_no == last_page_no)
{
memcpy (ctx->xpc_buf->bd_buffer + DP_DATA, cbuf + split, 4 - split);
}
else
{
buf = NULL;
buf = get_blob_page_for_write (ctx->xpc_itc, ctx->xpc_doc, next_page_no); /* the end is not on the current page */
memcpy (buf->bd_buffer + DP_DATA, cbuf + split, 4 - split);
buf_set_dirty (buf);
page_leave_outside_map (buf);
}
}
}
}
static void
count_buffered_words (xper_ctx_t * ctx)
{
if (0 != ctx->xpc_text_buf_use)
{
lang_handler_t *lh = GET_LOCAL_LH (ctx);
int ctr;
if (&lh__xany == lh) /* Optimization for most common case */
ctr = elh__xany__UTF8.elh_count_words ((const char *) ctx->xpc_text_buf, ctx->xpc_text_buf_use, lh__xany.lh_is_vtb_word);
else
ctr = lh_count_words (&eh__UTF8, lh, (const char *) ctx->xpc_text_buf, ctx->xpc_text_buf_use, lh->lh_is_vtb_word);
if (ctr > 0)
{
if (XML_MKUP_ETAG == ctx->xpc_word_hider)
ctx->xpc_main_word_ctr--;
ctx->xpc_word_hider = XML_MKUP_TEXT;
ctx->xpc_main_word_ctr += ctr;
}
ctx->xpc_text_buf_use = 0;
}
}
static void
cb_trace_start_tag (xper_ctx_t * ctx, xper_stag_t * data)
{
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
long pos = 0;
if (ctx->xpc_poss)
{
pos = (long) (ptrlong) ctx->xpc_poss->data;
ITC_FAIL (ctx->xpc_itc)
{
if (pos > 0)
{
/* first child */
set_long_in_blob (ctx, pos + ctx->xpc_tn_length1 +
STR_NAME_OFF + STR_FIRST_CHILD_OFF,
(long) ctx_bh->bh_length);
data->parent = pos;
}
else
{
/* right sibling */
set_long_in_blob (ctx, -pos + ctx->xpc_tn_length2 +
STR_NAME_OFF + STR_RIGHT_SIBLING_OFF,
(long) ctx_bh->bh_length);
data->left = -pos;
data->parent =
((ctx->xpc_poss->next) ? (long) (ptrlong) ctx->xpc_poss->next->data : 0);
}
ITC_LEAVE_MAPS (ctx->xpc_itc);
}
ITC_FAILED
{
ITC_LEAVE_MAPS (ctx->xpc_itc);
sqlr_new_error ("XE000", "XP9A9", "ITC error on writing persistent XML");
}
END_FAIL (ctx->xpc_itc);
}
ctx->xpc_tn_length1 = (int) dv_string_size (data->name);
if (pos < 0)
ctx->xpc_poss->data = (void *)((ptrlong)(ctx_bh->bh_length));
else
dk_set_push (&ctx->xpc_poss, (void *)((ptrlong)(ctx_bh->bh_length)));
}
void
cb_element_start (void *userdata, const char * name, vxml_parser_attrdata_t *attrdata)
{
xper_ctx_t *ctx = (xper_ctx_t *) userdata;
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
xper_stag_t data;
caddr_t tmp_box;
/* IvAn/TextXperIndex/000815 Word counter added */
ASSERT_OUTSIDE_MAP (ctx->xpc_itc->itc_tree, ctx->xpc_itc->itc_page); /* To ensure we had no chance to get deadlock inside the XML parser */
count_buffered_words (ctx);
ctx->xpc_word_hider = XML_MKUP_STAG;
++ctx->xpc_depth;
memset (&data, 0, sizeof (data));
data.type = XML_MKUP_STAG;
data.name = name;
data.attrdata = attrdata;
data.nss_ptr = &ctx->xpc_nss;
data.textplists_ptr = &ctx->xpc_textplists;
data.position = (long) ctx_bh->bh_length;
data.depth = ctx->xpc_depth;
data.wrs.xewr_main_beg = ctx->xpc_main_word_ctr;
data.wrs.xewr_attr_beg = ctx->xpc_attr_word_ctr;
data.recalc_wrs = data.store_wrs = ctx->xpc_index_attrs;
data.lh = GET_LOCAL_LH (ctx);
cb_trace_start_tag (ctx, &data);
tmp_box = start_tag_record (&data);
ctx->xpc_attr_word_ctr = data.wrs.xewr_attr_this_end;
xper_blob_append_box (ctx, tmp_box);
dk_free_box (tmp_box);
}
static void
cb_trace_end_tag (xper_ctx_t * ctx, long curr_pos, const char * name)
{
long pos = (long) (ptrlong) ctx->xpc_poss->data;
if (pos < 0)
{
dk_set_pop (&ctx->xpc_poss);
pos = (long) (ptrlong) ctx->xpc_poss->data;
}
ctx->xpc_poss->data = (void *) (ptrlong) -pos;
ctx->xpc_tn_length2 = (int) dv_string_size (name);
/* end tag */
ITC_FAIL (ctx->xpc_itc)
{
set_long_in_blob (ctx, pos + ctx->xpc_tn_length2 +
STR_NAME_OFF + STR_END_TAG_OFF,
curr_pos);
/* IvAn/TextXperIndex/000815 Word counter added */
set_long_in_blob (ctx, pos + ctx->xpc_tn_length2 +
STR_NAME_OFF + STR_END_WORD_OFF,
(long) ctx->xpc_main_word_ctr);
if (ctx->xpc_index_attrs)
{
set_long_in_blob (ctx, pos + ctx->xpc_tn_length2 +
STR_NAME_OFF + STR_ADDON_OR_ATTR_OFF + (2 * 4),
(long) ctx->xpc_attr_word_ctr);
}
ITC_LEAVE_MAPS (ctx->xpc_itc);
}
ITC_FAILED
{
ITC_LEAVE_MAPS (ctx->xpc_itc);
sqlr_new_error ("XE000", "XP9A9", "ITC error on writing persistent XML");
}
END_FAIL (ctx->xpc_itc);
}
void
cb_element_end (void *userdata, const char * name)
{
xper_ctx_t *ctx = (xper_ctx_t *) userdata;
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
caddr_t tmp_box;
xper_ns_t *ns_i;
xper_textplist_t *textplist_i;
/* IvAn/TextXperIndex/000815 Word counter added */
ASSERT_OUTSIDE_MAP (ctx->xpc_itc->itc_tree, ctx->xpc_itc->itc_page); /* To ensure we had no chance to get deadlock inside the XML parser */
count_buffered_words (ctx);
if (XML_MKUP_ETAG != ctx->xpc_word_hider)
{
ctx->xpc_main_word_ctr += 1;
ctx->xpc_word_hider = XML_MKUP_ETAG;
}
cb_trace_end_tag (ctx, (long) ctx_bh->bh_length, name);
tmp_box = end_tag_record (name);
xper_blob_append_box (ctx, tmp_box);
dk_free_box (tmp_box);
--ctx->xpc_depth;
while (ctx->xpc_nss)
{
ns_i = (xper_ns_t *) (ctx->xpc_nss->data);
if (ns_i->xpns_depth <= ctx->xpc_depth)
break;
dk_set_pop (&ctx->xpc_nss);
dk_free_box (ns_i->xpns_name);
dk_free (ns_i, sizeof (xper_ns_t));
}
while (ctx->xpc_textplists)
{
textplist_i = (xper_textplist_t *) (ctx->xpc_textplists->data);
if (textplist_i->xptp_depth <= ctx->xpc_depth)
break;
dk_set_pop (&ctx->xpc_textplists);
dk_free_box (textplist_i->xptp_lang_name);
dk_free (textplist_i, sizeof (xper_textplist_t));
}
}
void
cb_id (void *userdata, char * name)
{
xper_ctx_t *ctx = (xper_ctx_t *) userdata;
caddr_t boxed_name = box_dv_short_string (name);
id_hash_t *dict = ctx->xpc_id_dict;
ptrlong **id_hit, *lpath;
if (NULL == dict)
{
ctx->xpc_id_dict = dict = (id_hash_t *) box_dv_dict_hashtable (509);
}
id_hit = (ptrlong **) id_hash_get (dict, (caddr_t) (&boxed_name));
if (NULL != id_hit)
{
dk_free_box (boxed_name);
return;
}
/* Calculate a path to current element. */
lpath = (ptrlong *) dk_alloc_box (sizeof (ptrlong), DV_ARRAY_OF_LONG);
lpath[0] = ((ctx->xpc_poss) ? (ptrlong) ctx->xpc_poss->data : 0);
/* Save the result. */
id_hash_set (dict, (caddr_t) (&boxed_name), (caddr_t) (&lpath));
}
/* IvAn/TextXperIndex/000815 Word counter added */
void
cb_character (void *userdata, const char * s, size_t len)
{
xper_ctx_t *ctx = (xper_ctx_t *) userdata;
const char *s_frag_begin, *s_tail, *s_end;
unichar uchr;
int space_found;
int frag_len, newbuf_len;
utf8char *newbuf;
/* First, data should be stored inside blob. */
if (len <= 255)
{
unsigned char ccc[2];
ccc[0] = DV_SHORT_STRING_SERIAL;
ccc[1] = (unsigned char) len;
xper_blob_append_data (ctx, (const char *) ccc, 2);
}
else
{
unsigned char ccc[5];
ccc[0] = DV_STRING;
LONG_SET_NA (ccc + 1, len);
xper_blob_append_data (ctx, (const char *) ccc, 5);
}
xper_blob_append_data (ctx, s, len);
/* Now blob looks fine, and we can process words */
s_end = s + len;
s_frag_begin = s_tail = s;
/* First, buffer may begin from incomplete char, such part should be skipped */
while ((s_tail < s_end) && IS_UTF8_CHAR_CONT (s_tail[0]))
s_tail++;
next_frag:
for (;;)
{
uchr = eh_decode_char__UTF8 (&s_tail, s_end);
if (uchr < 0)
{
s_tail = s_end;
space_found = 0;
break;
}
if (unicode3_isspace (uchr))
{
space_found = 1;
break;
}
}
frag_len = (int) (s_tail - s_frag_begin);
if (ctx->xpc_text_buf_use + frag_len > ctx->xpc_text_buf_size)
{
newbuf_len = ctx->xpc_text_buf_size;
do
{
newbuf_len *= 2;
newbuf_len |= 0x3FF;
}
while (ctx->xpc_text_buf_use + frag_len > newbuf_len);
newbuf = (utf8char *) dk_alloc (newbuf_len);
memcpy (newbuf, ctx->xpc_text_buf, ctx->xpc_text_buf_use);
memcpy (newbuf + ctx->xpc_text_buf_use, s_frag_begin, frag_len);
if (0 != ctx->xpc_text_buf_size)
dk_free (ctx->xpc_text_buf, ctx->xpc_text_buf_size);
ctx->xpc_text_buf = newbuf;
ctx->xpc_text_buf_size = newbuf_len;
}
else
{
memcpy (ctx->xpc_text_buf + ctx->xpc_text_buf_use, s_frag_begin, frag_len);
}
ctx->xpc_text_buf_use += frag_len;
if (space_found)
{
count_buffered_words (ctx);
s_frag_begin = s_tail;
}
if (s_tail < s_end)
goto next_frag;
}
/* IvAn/ParseDTD/000721 Structure of entity has changed */
void
cb_entity (void *userdata, const char * refname, size_t reflen, int isparam, const xml_def_4_entity_t * edef)
{
xper_ctx_t *ctx = (xper_ctx_t *) userdata;
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
xper_stag_t data;
vxml_parser_attrdata_t attrdata;
tag_attr_t ta;
caddr_t tmp_box;
char *uri;
long my_pos;
wpos_t tmp_index_attrs;
count_buffered_words (ctx);
/* No changes of \c ctx->xpc_word_hider here - entity ref is transparent for words counting. */
memset (&data, 0, sizeof (data));
memset (&attrdata, 0, sizeof (attrdata));
data.attrdata = &attrdata;
data.type = XML_MKUP_REF;
data.name = box_dv_short_nchars (refname, reflen);
uri = ((NULL != edef) ? edef->xd4e_systemId : NULL);
if (NULL != uri)
{
ta.ta_raw_name.lm_memblock = "SYSTEM";
ta.ta_raw_name.lm_length = 6;
ta.ta_value = box_dv_short_string (uri);
attrdata.local_attrs = &ta;
attrdata.local_attrs_count = 1;
}
data.nss_ptr = &ctx->xpc_nss;
data.textplists_ptr = &ctx->xpc_textplists;
data.position = (long) ctx_bh->bh_length;
data.depth = ctx->xpc_depth + 1;
data.wrs.xewr_main_beg = ctx->xpc_main_word_ctr;
data.recalc_wrs = data.store_wrs = 0;
data.lh = NULL; /* NULL is instead of GET_LOCAL_LH(ctx) to cause GPF on internal errors */
cb_trace_start_tag (ctx, &data);
my_pos = (long) ctx_bh->bh_length;
tmp_box = start_tag_record (&data);
xper_blob_append_box (ctx, tmp_box);
tmp_index_attrs = ctx->xpc_index_attrs;
ctx->xpc_index_attrs = 0;
cb_trace_end_tag (ctx, my_pos, data.name);
ctx->xpc_index_attrs = (int) tmp_index_attrs;
dk_free_box (tmp_box);
if (NULL != uri)
dk_free_box (ta.ta_value);
dk_free_box (( /*nonconst */ char *) (data.name));
}
void
cb_pi (void *userdata, const char * target, const char * pi_data)
{
xper_ctx_t *ctx = (xper_ctx_t *) userdata;
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
xper_stag_t data;
vxml_parser_attrdata_t attrdata;
tag_attr_t ta;
caddr_t tmp_box;
long my_pos;
wpos_t tmp_index_attrs;
count_buffered_words (ctx);
/* No changes of \c ctx->xpc_word_hider here - entity ref is transparent for words counting. */
memset (&data, 0, sizeof (data));
memset (&attrdata, 0, sizeof (attrdata));
data.attrdata = &attrdata;
data.type = XML_MKUP_PI;
data.name = box_dv_short_string (target);
if (NULL != pi_data)
{
ta.ta_raw_name.lm_memblock = "data";
ta.ta_raw_name.lm_length = 4;
ta.ta_value = box_dv_short_string (pi_data);
attrdata.local_attrs = &ta;
attrdata.local_attrs_count = 1;
}
data.nss_ptr = &ctx->xpc_nss;
data.textplists_ptr = &ctx->xpc_textplists;
data.position = (long) ctx_bh->bh_length;
data.depth = ctx->xpc_depth + 1;
data.wrs.xewr_main_beg = ctx->xpc_main_word_ctr;
data.recalc_wrs = data.store_wrs = 0;
data.lh = NULL; /* NULL is instead of GET_LOCAL_LH(ctx) to cause GPF on internal errors */
cb_trace_start_tag (ctx, &data);
my_pos = (long) ctx_bh->bh_length;
tmp_box = start_tag_record (&data);
xper_blob_append_box (ctx, tmp_box);
tmp_index_attrs = ctx->xpc_index_attrs;
ctx->xpc_index_attrs = 0;
cb_trace_end_tag (ctx, my_pos, target);
ctx->xpc_index_attrs = (int) tmp_index_attrs;
if (NULL != pi_data)
dk_free_box (ta.ta_value);
dk_free_box (tmp_box);
dk_free_box (( /*nonconst */ char *) (data.name));
}
void
cb_comment (void *userdata, const char * text)
{
xper_ctx_t *ctx = (xper_ctx_t *) userdata;
size_t len = strlen (text);
size_t fulllen;
unsigned char hdr[11];
count_buffered_words (ctx);
/* First, data should be stored inside blob. */
if (len <= 255)
{
hdr[0] = DV_XML_MARKUP;
fulllen = len + 3;
LONG_SET_NA (hdr + 1, fulllen);
hdr[5] = XML_MKUP_COMMENT;
hdr[6] = DV_SHORT_STRING_SERIAL;
hdr[7] = (unsigned char) len;
xper_blob_append_data (ctx, (const char *) hdr, 8);
}
else
{
hdr[0] = DV_XML_MARKUP;
fulllen = len + 6;
LONG_SET_NA (hdr + 1, fulllen);
hdr[5] = XML_MKUP_COMMENT;
hdr[6] = DV_STRING;
LONG_SET_NA (hdr + 7, len);
xper_blob_append_data (ctx, (const char *) hdr, 11);
}
xper_blob_append_data (ctx, text, len);
}
int
dtd_get_buffer_length (dtd_t * dtd)
{
ecm_el_idx_t el_idx, el_no;
id_hash_t *dict;
char **dict_key;
xml_def_4_entity_t **dict_entity;
id_hash_iterator_t dict_hit;
int len = 0;
if (NULL != dtd->ed_puburi)
len += 1 + (int) dv_string_size ((char *) (dtd->ed_puburi));
if (NULL != dtd->ed_sysuri)
len += 1 + (int) dv_string_size ((char *) (dtd->ed_sysuri));
el_no = dtd->ed_el_no;
for (el_idx = 0; el_idx < el_no; el_idx++)
{
ecm_el_t *el = dtd->ed_els + el_idx;
if (el->ee_has_id_attr)
{
ecm_attr_idx_t key_idx = el->ee_id_attr_idx;
len +=
1 /* type byte */ +
(int) dv_string_size ((char *) (el->ee_name)) +
(int) dv_string_size ((char *) (el->ee_attrs[key_idx].da_name));
}
}
/* GE's processing */
dict = dtd->ed_generics;
if (NULL != dict)
{
for (id_hash_iterator (&dict_hit, dict);
hit_next (&dict_hit, (char **) (&dict_key), (char **) (&dict_entity));
/*no step */ )
{
char type_byte;
if (NULL == dict_entity[0]->xd4e_systemId)
continue;
type_byte = ((NULL != dict_entity[0]->xd4e_publicId) ? 'G' : 'g');
len += 1 /* type_byte */ + (int) dv_string_size (dict_key[0]) +
(int) dv_string_size (dict_entity[0]->xd4e_systemId);
if ('G' == type_byte)
len += (int) dv_string_size (dict_entity[0]->xd4e_publicId);
}
}
if (0 == len)
len = -1;
dtd->ed_xper_text_length = len;
return len;
}
void dtd_save_str_to_buffer (unsigned char **tail_ptr, char *str)
{
unsigned char *tail = tail_ptr[0];
size_t len = strlen (str);
dtp_t tag = ((len > 255) ? DV_STRING : DV_SHORT_STRING_SERIAL);
(tail++)[0] = tag;
if (DV_SHORT_STRING_SERIAL == tag)
{
(tail++)[0] = ((unsigned char)len);
}
else
{
LONG_SET_NA (tail, len);
tail += 4;
}
memcpy (tail, str, len);
tail_ptr[0] = tail + len;
}
void dtd_save_to_buffer (dtd_t *dtd, unsigned char *buf, size_t buf_len)
{
unsigned char *tail = buf;
ecm_el_idx_t el_idx, el_no;
id_hash_t *dict;
char **dict_key;
xml_def_4_entity_t **dict_entity;
id_hash_iterator_t dict_hit;
/* Exactly the same logic as in dtd_get_buffer_length... */
if (NULL != dtd->ed_puburi)
{
(tail++)[0] = 'U';
dtd_save_str_to_buffer (&tail, dtd->ed_puburi);
}
if (NULL != dtd->ed_sysuri)
{
(tail++)[0] = 'u';
dtd_save_str_to_buffer (&tail, dtd->ed_sysuri);
}
el_no = dtd->ed_el_no;
for (el_idx = 0; el_idx < el_no; el_idx++)
{
ecm_el_t *el = dtd->ed_els + el_idx;
if (el->ee_has_id_attr)
{
ecm_attr_idx_t key_idx = el->ee_id_attr_idx;
(tail++)[0] = 'k';
dtd_save_str_to_buffer (&tail, el->ee_name);
dtd_save_str_to_buffer (&tail, el->ee_attrs[key_idx].da_name);
}
}
/* GE's processing */
dict = dtd->ed_generics;
if (NULL != dict)
{
for (id_hash_iterator (&dict_hit, dict);
hit_next (&dict_hit, (char **) (&dict_key), (char **) (&dict_entity));
/*no step */ )
{
unsigned char type_byte;
if (NULL == dict_entity[0]->xd4e_systemId)
continue;
type_byte = ((NULL != dict_entity[0]->xd4e_publicId) ? 'G' : 'g');
(tail++)[0] = type_byte;
dtd_save_str_to_buffer (&tail, dict_key[0]);
dtd_save_str_to_buffer (&tail, dict_entity[0]->xd4e_systemId);
if ('G' == type_byte)
dtd_save_str_to_buffer (&tail, dict_entity[0]->xd4e_publicId);
}
}
if (tail != buf+buf_len)
GPF_T;
}
int
blob_append_dtd (xper_ctx_t * ctx, dtd_t * dtd)
{
/* Uncomment this for debugging: blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh; */
unsigned char *buf;
int len = 0;
len = dtd->ed_xper_text_length;
if (0 == len)
len = dtd_get_buffer_length (dtd);
if (0 >= len)
return 0;
if (len <= 255)
{
unsigned char ccc[2];
ccc[0] = DV_SHORT_STRING_SERIAL;
ccc[1] = len;
xper_blob_append_data (ctx, (const char *) ccc, 2);
}
else
{
unsigned char ccc[5];
ccc[0] = DV_STRING;
LONG_SET_NA (ccc + 1, len);
xper_blob_append_data (ctx, (const char *) ccc, 5);
}
buf = (unsigned char *) dk_alloc (len);
dtd_save_to_buffer (dtd, buf, len);
xper_blob_append_data (ctx, (char *)buf, len);
dk_free (buf, len);
return 1;
}
void
xper_destroy_ctx (xper_ctx_t * ctx)
{
while (NULL != ctx->xpc_cut_chain)
dk_free_box ((box_t) dk_set_pop (&(ctx->xpc_cut_chain)));
while (NULL != ctx->xpc_cut_namespaces)
{
xper_ns_t *curr_ns = (xper_ns_t *) (dk_set_pop (&(ctx->xpc_cut_namespaces)));
dk_free_box (curr_ns->xpns_uri);
dk_free (curr_ns, -1);
}
if ((NULL != ctx->xpc_itc) && (NULL != ctx->xpc_buf))
{
buf_set_dirty (ctx->xpc_buf);
page_leave_outside_map (ctx->xpc_buf); /* This should be done before blob_chain_delete() below */
}
if (NULL != ctx->xpc_doc)
{
blob_handle_t *ctx_bh = ctx->xpc_doc->xpd_bh;
if (NULL != ctx_bh)
{
xper_blob_truncate_dir_buf (ctx_bh);
blob_chain_delete (ctx->xpc_itc, (blob_layout_t *) blob_layout_from_handle_ctor (ctx_bh));
bh_free (ctx_bh);
ctx->xpc_doc->xpd_bh = NULL;
}
XPER_FREE_XPD (ctx->xpc_doc);
ctx->xpc_doc = NULL;
}
if (NULL != ctx->xpc_itc)
{
itc_free (ctx->xpc_itc);
ctx->xpc_itc = NULL;
}
dk_set_free (ctx->xpc_poss);
ctx->xpc_poss = NULL;
if (0 != ctx->xpc_text_buf_size)
{
dk_free (ctx->xpc_text_buf, ctx->xpc_text_buf_size);
ctx->xpc_text_buf_size = 0;
}
if (NULL != ctx->xpc_src_file)
{
fclose (ctx->xpc_src_file);
ctx->xpc_src_file = NULL;
}
if (NULL != ctx->xpc_parser)
{
VXmlParserDestroy (ctx->xpc_parser);
ctx->xpc_parser = NULL;
}
if (NULL != ctx->xpc_src_filename)
{
dk_free_box (ctx->xpc_src_filename);
ctx->xpc_src_filename = NULL;
}
}
static caddr_t
DBG_NAME (get_tag_data) (DBG_PARAMS xper_entity_t * xpe, volatile long pos)
{
volatile int pn;
it_cursor_t *tmp_itc;
index_tree_t * it;
buffer_desc_t *buf = NULL;
size_t len;
caddr_t volatile box = NULL;
dtp_t dtp;
size_t i;
char cbuf[4];
int split = -1;
tmp_itc =
itc_create (NULL, xpe->xe_doc.xd->xd_qi->qi_trx);
it = xpe->xe_doc.xpd->xpd_bh->bh_it;
if (NULL != it)
{
itc_from_it (tmp_itc, it);
}
else
{
dbe_key_t* xper_key = sch_id_to_key (wi_inst.wi_schema, KI_COLS);
itc_from (tmp_itc, xper_key);
}
ITC_FAIL (tmp_itc)
{
pn = pos / PAGE_DATA_SZ;
buf = get_blob_page_for_read (tmp_itc, xpe->xe_doc.xpd, pn);
if (NULL != buf)
dtp = *(unsigned char *) (buf->bd_buffer + DP_DATA + pos % PAGE_DATA_SZ);
else
dtp = DV_UNKNOWN;
if (DV_XML_MARKUP != dtp && DV_SHORT_STRING_SERIAL != dtp && DV_STRING != dtp)
{
if (NULL != buf)
{
page_leave_outside_map (buf);
buf = NULL;
}
itc_free (tmp_itc);
if (box)
dk_free_box (box);
sqlr_new_error ("XE000", "XP9A4", "Error while accessing XML_PERSISTENT: invalid record type %d", dtp);
}
pos++;
if (0 == pos % PAGE_DATA_SZ)
{
page_leave_outside_map (buf);
buf = NULL;
pn++;
buf = get_blob_page_for_read (tmp_itc, xpe->xe_doc.xpd, pn);
ITC_LEAVE_MAPS (tmp_itc);
}
if (DV_SHORT_STRING_SERIAL == dtp)
{
len = *(unsigned char *) (buf->bd_buffer + DP_DATA + pos % PAGE_DATA_SZ);
++pos;
}
else
{
split = PAGE_DATA_SZ - (pos % PAGE_DATA_SZ);
if (split >= 4)
len = LONG_REF_NA (buf->bd_buffer + DP_DATA + pos % PAGE_DATA_SZ);
else
{ /* the worst case - number is split between pages */
memcpy (cbuf, buf->bd_buffer + DP_DATA + PAGE_DATA_SZ - split, split);
page_leave_outside_map (buf);
buf = NULL;
pn++;
buf = get_blob_page_for_read (tmp_itc, xpe->xe_doc.xpd, pn);
memcpy (cbuf + split, buf->bd_buffer + DP_DATA, 4 - split);
len = LONG_REF_NA (cbuf);
}
pos += 4;
if (0 == len)
GPF_T;
}
box = DBG_NAME (dk_alloc_box) (DBG_ARGS len, dtp);
for (i = 0; i < len;)
{
size_t cpsz = ((PAGE_DATA_SZ - pos % PAGE_DATA_SZ < len - i) ?
PAGE_DATA_SZ - pos % PAGE_DATA_SZ :
len - i);
if (0 == pos % PAGE_DATA_SZ)
{
page_leave_outside_map (buf);
buf = NULL;
buf = get_blob_page_for_read (tmp_itc, xpe->xe_doc.xpd, ++pn);
}
memcpy (box + i, buf->bd_buffer + DP_DATA + pos % PAGE_DATA_SZ, cpsz);
pos += (long) cpsz;
i += cpsz;
}
page_leave_outside_map (buf);
buf = NULL;
}
ITC_FAILED
{
if (NULL != buf)
page_leave_outside_map (buf);
itc_free (tmp_itc);
if (box)
dk_free_box (box);
sqlr_new_error ("XE000", "XP9A5", "Error while accessing XML_PERSISTENT");
}
END_FAIL (tmp_itc);
itc_free (tmp_itc);
return box;
}
#ifdef MALLOC_DEBUG
#define get_tag_data(XPE,POS) dbg_get_tag_data (__FILE__, __LINE__, (XPE), (POS))
#endif
caddr_t
DBG_NAME(xper_get_namespace) (DBG_PARAMS xper_entity_t * xpe, long pos)
{
caddr_t tmp, res;
if (!pos)
return NULL;
/* TBD - namespace caching by pos */
tmp = DBG_NAME(get_tag_data) (DBG_ARGS xpe, pos);
res = box_dv_uname_nchars (tmp, box_length (tmp));
dk_free_box (tmp);
return res;
}
/* IvAn/XperTrav/000825 Function has rewritten to support all types of entities */
static void
fill_xper_entity (xper_entity_t * xpe, long pos)
{
long len;
unsigned char *ptr;
caddr_t tmp_box;
int namelen;
dtp_t dtp;
char *buf1, *buf2;
long buf1len, buf1use, buf2len;
tmp_box = get_tag_data (xpe, pos);
xpe->xper_pos = pos; /* moved here from get_tag_data */
if (xpe->xper_name)
dk_free_box (xpe->xper_name);
xpe->xper_name = NULL;
if (NULL != xpe->xper_text)
dk_free_box (xpe->xper_text);
xpe->xper_text = NULL;
dtp = DV_TYPE_OF (tmp_box);
len = box_length (tmp_box);
if (DV_SHORT_STRING_SERIAL == dtp || DV_STRING == dtp)
{
xpe->xper_type = XML_MKUP_TEXT;
buf1len = ((len > 900) ? 0x1000 : len);
buf1 = (char *) dk_alloc (buf1len);
buf1use = 0;
do
{
if (buf1use + len > buf1len)
{
buf2len = buf1len + len;
if (buf2len > 10000000)
{
dk_free_box (tmp_box);
sqlr_new_error ("XE000", "XP9A9", "XML contains abnormally long text data");
}
buf2len *= 2;
if (buf2len > 10000000)
buf2len = 10000000;
buf2 = (char *) dk_alloc (buf2len);
memcpy (buf2, buf1, buf1use);
dk_free (buf1, buf1len);
buf1 = buf2;
buf1len = buf2len;
}
memcpy (buf1 + buf1use, tmp_box, len);
buf1use += len;
pos += len + ((DV_SHORT_STRING_SERIAL == dtp) ? 2 : 5);
dk_free_box (tmp_box);
tmp_box = get_tag_data (xpe, pos);
dtp = DV_TYPE_OF (tmp_box);
len = box_length (tmp_box);
}
while ((DV_SHORT_STRING_SERIAL == dtp) || (DV_STRING == dtp));
xpe->xper_text = dk_alloc_box (buf1use, DV_STRING);
memcpy (xpe->xper_text, buf1, buf1use);
dk_free (buf1, buf1len);
/* \c xpe->xper_parent and \c xpe->xper_left should be filled by caller */
xpe->xper_right = ((XML_MKUP_ETAG == tmp_box[0]) ? 0 : pos);
xpe->xper_end = 0;
xpe->xper_first_child = 0;
/* \c xpe->xper_start_word and \c xpe->xper_end_word should be filled by caller */
xpe->xper_next_item = pos;
dk_free_box (tmp_box);
return;
}
ptr = (unsigned char *) tmp_box;
xpe->xper_type = *ptr++;
switch (xpe->xper_type)
{
case XML_MKUP_STAG:
case XML_MKUP_PI:
case XML_MKUP_REF:
if (DV_STRING == ptr[0])
{
namelen = LONG_REF_NA (ptr + 1);
ptr += 5;
}
else
{
namelen = ptr[1];
ptr += 2;
}
xpe->xper_name = box_dv_uname_nchars ((char *)ptr, namelen);
ptr += namelen;
xpe->xper_parent = LONG_REF_NA (ptr + STR_PARENT_OFF);
xpe->xper_left = LONG_REF_NA (ptr + STR_LEFT_SIBLING_OFF);
xpe->xper_right = LONG_REF_NA (ptr + STR_RIGHT_SIBLING_OFF);
xpe->xper_end = LONG_REF_NA (ptr + STR_END_TAG_OFF);
xpe->xper_first_child = LONG_REF_NA (ptr + STR_FIRST_CHILD_OFF);
/* IvAn/TextXperIndex/000815 Word counter added */
xpe->xper_start_word = LONG_REF_NA (ptr + STR_START_WORD_OFF);
xpe->xper_end_word = LONG_REF_NA (ptr + STR_END_WORD_OFF);
xpe->xper_ns_pos = LONG_REF_NA (ptr + STR_NS_OFF);
xpe->xper_next_item = pos + 5 + len;
break;
case XML_MKUP_ETAG:
if (DV_STRING == ptr[0])
{
namelen = LONG_REF_NA (ptr + 1);
ptr += 5;
}
else
{
namelen = ptr[1];
ptr += 2;
}
xpe->xper_name = box_dv_uname_nchars ((char *)ptr, namelen);
xpe->xper_next_item = pos + 5 + len;
break;
case XML_MKUP_COMMENT:
if (DV_STRING == ptr[0])
{
namelen = LONG_REF_NA (ptr + 1);
ptr += 5;
}
else
{
namelen = ptr[1];
ptr += 2;
}
xpe->xper_name = box_dv_short_nchars ((char *)ptr, namelen);
pos += 5 + len;
xpe->xper_next_item = pos;
dk_free_box (tmp_box);
tmp_box = get_tag_data (xpe, pos);
dtp = DV_TYPE_OF (tmp_box);
if ((DV_SHORT_STRING_SERIAL == dtp) || (DV_STRING == dtp) || (XML_MKUP_ETAG != tmp_box[0]))
xpe->xper_right = pos;
else
xpe->xper_right = 0;
break;
default:
dk_free_box (tmp_box);
sqlr_new_error ("XE000", "XP9AA", "Persistent XML BLOB is invalid");
}
dk_free_box (tmp_box);
}
xml_entity_t *
xp_reference (query_instance_t * qi, caddr_t base, caddr_t ref,
xml_doc_t * from_doc, caddr_t * err_ret)
{
xper_entity_t *xpe;
dtd_t **xpe_dtd_ptr;
caddr_t str;
caddr_t err = NULL;
caddr_t path = xml_uri_resolve (qi, &err, base, ref, "UTF-8");
xml_doc_t * top_doc = from_doc->xd_top_doc;
if (err)
{
dk_free_box (path);
if (NULL == err_ret)
sqlr_resignal (err);
err_ret[0] = err;
return NULL;
}
DO_SET (xml_entity_t *, ref_ent, &top_doc->xd_referenced_entities)
{
if (NULL != ref_ent->xe_doc.xd->xd_uri
&& 0 == strcmp (ref_ent->xe_doc.xpd->xd_uri, path))
{
dk_free_box (path);
return ref_ent;
}
}
END_DO_SET ();
str = xml_uri_get (qi, &err, NULL, base, ref, XML_URI_ANY);
if (DV_XML_ENTITY == DV_TYPE_OF (str))
{
xpe = (xper_entity_t *)str; /* This is actually not quite correct, it can be XmlTree but no XPER-specific things are used */
goto xpe_is_ready; /* see below */
}
if (err)
{
dk_free_box (path);
if (NULL == err_ret)
sqlr_resignal (err);
err_ret[0] = err;
return NULL;
}
xpe = xper_entity (qi, str, NULL, GE_XML, path, NULL, server_default_lh, NULL, 0);
xpe_dtd_ptr = &(xpe->xe_doc.xd->xd_dtd);
if (NULL != from_doc->xd_dtd)
{
dtd_addref (from_doc->xd_dtd, 0);
if (NULL != xpe_dtd_ptr[0])
dtd_release (xpe_dtd_ptr[0]);
xpe_dtd_ptr[0] = from_doc->xd_dtd;
dk_free_box ((caddr_t) (xpe->xe_doc.xd->xd_id_dict));
dk_free_box (xpe->xe_doc.xd->xd_id_scan);
xpe->xe_doc.xd->xd_id_dict = NULL;
xpe->xe_doc.xd->xd_id_scan = NULL;
}
xpe_is_ready:
XD_DOM_LOCK(xpe->xe_doc.xd);
dk_set_push (&top_doc->xd_referenced_entities, (void *) xpe);
xpe->xe_doc.xpd->xd_top_doc = top_doc;
top_doc->xd_weight += xpe->xe_doc.xd->xd_weight;
top_doc->xd_cost += xpe->xe_doc.xd->xd_cost;
if (top_doc->xd_cost > XML_MAX_DOC_COST)
top_doc->xd_cost = XML_MAX_DOC_COST;
return ((xml_entity_t *) (xpe));
}
int
xp_element_name_test (xml_entity_t * xe, XT * node)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
caddr_t ns;
size_t nslen, len;
unsigned char *name, *colon;
if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (node))
{
switch ((ptrlong) node)
{
case XP_TEXT:
return XML_MKUP_TEXT == xpe->xper_type;
case XP_COMMENT:
return XML_MKUP_COMMENT == xpe->xper_type;
case XP_PI:
return XML_MKUP_PI == xpe->xper_type;
case XP_NODE:
return 1;
case XP_ELT:
return ((XML_MKUP_STAG == xpe->xper_type) && (XPER_ROOT_POS != xpe->xper_pos));
case XP_ELT_OR_ROOT:
return (XML_MKUP_STAG == xpe->xper_type);
}
GPF_T;
return 0;
}
if (XML_MKUP_STAG != xpe->xper_type)
return 0;
name = (unsigned char *) (xpe->xper_name);
if (!name || (' ' == name[0]))
return 0;
if (!((XP_NAME_LOCAL == node->type) && (NULL == node->_.name_test.nsuri)))
{
long nspos;
caddr_t tmp_box;
unsigned char *tail;
tmp_box = get_tag_data (xpe, xpe->xper_pos);
tail = (unsigned char *) (tmp_box) + 1 + dv_string_size (xpe->xper_name) + STR_NS_OFF;
nspos = LONG_REF_NA (tail);
dk_free_box (tmp_box);
if (0 == nspos)
{
ns = NULL;
nslen = 0;
}
else
{
ns = xper_get_namespace (xpe, nspos);
nslen = box_length_inline (ns) - 1;
if (0 == nslen)
ns = NULL;
}
}
else
{
ns = NULL;
nslen = 0;
}
if (XP_NAME_NSURI == node->type)
{
/* test is ns.* */
if (!ns)
goto ret0;
if ((nslen == box_length (node->_.name_test.nsuri) - 1) &&
(0 == memcmp (ns, node->_.name_test.nsuri, nslen)) )
goto ret1;
else
goto ret0;
}
len = box_length (name) - 1;
colon = name + len;
for (;;)
{
if (colon == name)
{
colon = NULL;
break;
}
colon--;
if (':' == colon[0])
break;
}
if (colon)
{
len -= (colon - name) + 1;
name = colon + 1;
}
if (NULL == node->_.name_test.nsuri)
{
/* test is name w/o ns */
if (((size_t) (len) == box_length_inline (node->_.name_test.local) - 1) &&
(0 == memcmp (node->_.name_test.local, name, len)) )
goto ret1;
else
goto ret0;
}
/* test with namespace */
if (!ns)
goto ret0;
if (((size_t) (len) != box_length_inline (node->_.name_test.local) - 1) ||
(0 != memcmp (node->_.name_test.local, name, len)) )
goto ret0;
if (XP_NAME_LOCAL == node->type)
goto ret1;
if ((nslen == box_length (node->_.name_test.nsuri) - 1) &&
(0 == memcmp (ns, node->_.name_test.nsuri, nslen)) )
goto ret1;
goto ret0;
ret1:
dk_free_box (ns);
return 1;
ret0:
dk_free_box (ns);
return 0;
}
int
xp_ent_name_test (xml_entity_t * xe, XT * node)
{
if (NULL != xe->xe_attr_name)
return xt_node_test_match (node, xe->xe_attr_name);
return xp_element_name_test (xe, node);
}
int
xper_node_test (xper_entity_t * xpe, long pos, XT * node)
{
long len;
unsigned char *ptr;
caddr_t tmp_box;
int namelen;
int res;
char type;
long save;
if ((XT *) XP_NODE == node)
return 1;
tmp_box = get_tag_data (xpe, pos);
if (DV_XML_MARKUP != DV_TYPE_OF (tmp_box))
{
dk_free_box (tmp_box);
return (((XT *) XP_TEXT == node) ? 1 : 0);
}
len = box_length (tmp_box);
ptr = (unsigned char *) tmp_box;
type = ptr[0];
++ptr;
namelen = (int) skip_string_length (&ptr);
switch (type)
{
case XML_MKUP_REF:
#ifdef DEBUG
GPF_T;
#endif
dk_free_box (tmp_box);
return 0;
case XML_MKUP_COMMENT:
res = ((XP_COMMENT == (ptrlong)node) || (XP_NODE == (ptrlong)node));
dk_free_box (tmp_box);
return res;
default:
save = xpe->xper_pos;
fill_xper_entity (xpe, pos);
res = xp_ent_name_test ((xml_entity_t *) (xpe), node);
fill_xper_entity (xpe, save);
dk_free_box (tmp_box);
return res;
}
}
int
xp_ent_node_test (xml_entity_t * xe, XT * node)
{
return xper_node_test ((xper_entity_t *) xe, ((xper_entity_t *) xe)->xper_pos, node);
}
caddr_t
bif_xml_persistent (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t res;
caddr_t source = bif_arg (qst, args, 0, "xml_persistent");
int argc = BOX_ELEMENTS (args);
caddr_t path = ((argc > 1) ? bif_string_arg (qst, args, 1, "xml_persistent") : NULL);
lang_handler_t *lh = ((argc > 2) ? lh_get_handler (bif_string_arg (qst, args, 2, "xml_persistent")) : server_default_lh);
caddr_t dtd_config = ((argc > 3) ? bif_array_or_null_arg (qst, args, 3, "xml_persistent") : NULL);
int index_attrs = ((argc > 4) ? (int) bif_long_arg (qst, args, 4, "xml_persistent") : 1);
if (!cl_run_local_only)
sqlr_new_error ("42000", "CLXML", "xml_persistent is deprecated in cluster. Use xtree_doc instead.");
res =
(caddr_t) xper_entity ((query_instance_t *) QST_INSTANCE (qst), source,
NULL, 0, box_copy (path), NULL, lh, dtd_config, index_attrs);
return res;
}
caddr_t
bif_xper_doc (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t res;
caddr_t source = bif_arg (qst, args, 0, "xper_doc");
int argc = BOX_ELEMENTS (args);
int parser_mode = ((argc > 1) ? (int) bif_long_arg (qst, args, 1, "xper_doc") : 0);
caddr_t path = ((argc > 2) ? bif_string_arg (qst, args, 2, "xper_doc") : NULL);
caddr_t enc = ((argc > 3) ? bif_string_arg (qst, args, 3, "xper_doc") : NULL);
lang_handler_t *lh = ((argc > 4) ? lh_get_handler (bif_string_arg (qst, args, 4, "xper_doc")) : server_default_lh);
caddr_t dtd_config = ((argc > 5) ? bif_array_or_null_arg (qst, args, 5, "xper_doc") : NULL);
int index_attrs = ((argc > 6) ? (int) bif_long_arg (qst, args, 6, "xper_doc") : 1);
res =
(caddr_t) xper_entity ((query_instance_t *) QST_INSTANCE (qst), source,
NULL, parser_mode, box_copy (path), enc, lh, dtd_config, index_attrs);
return res;
}
size_t
file_read (void *read_cd, char *buf, size_t bsize)
{
FILE *file = (FILE *) read_cd;
return fread (buf, 1, bsize, file);
}
#ifdef XMLPARSER_FEED_DEBUG
static FILE *feed_log = NULL;
#endif
void
dsfi_reset (dk_session_fwd_iter_t * iter, dk_session_t * ses)
{
iter->dsfi_dorigin = ses;
iter->dsfi_buffer = ses->dks_buffer_chain;
iter->dsfi_offset = 0;
if (ses->dks_session->ses_file->ses_file_descriptor)
iter->dsfi_file_len = ses->dks_session->ses_file->ses_fd_fill_chars;
else
iter->dsfi_file_len = 0;
iter->dsfi_file_offset = 0;
#ifdef XMLPARSER_FEED_DEBUG
feed_log = fopen("feed_log", "wb");
#endif
}
extern long read_wides_from_utf8_file (dk_session_t *ses, long nchars, unsigned char *dest, int copy_as_utf8, unsigned char **dest_ptr_out);
size_t
dsfi_read (void *read_cd, char *buf, size_t bsize)
{
#ifdef XMLPARSER_FEED_DEBUG
char *buf_orig = buf;
size_t bsize_orig = bsize;
#endif
dk_session_fwd_iter_t *iter = (dk_session_fwd_iter_t *) read_cd;
size_t res = 0;
size_t rest_len;
dk_session_t *ses = iter->dsfi_dorigin;
while (NULL != iter->dsfi_buffer)
{
rest_len = iter->dsfi_buffer->fill - iter->dsfi_offset;
if (bsize <= rest_len)
{
memcpy (buf, iter->dsfi_buffer->data + iter->dsfi_offset, bsize);
iter->dsfi_offset += bsize;
res += bsize;
goto done;
}
memcpy (buf, iter->dsfi_buffer->data + iter->dsfi_offset, rest_len);
iter->dsfi_offset = 0;
iter->dsfi_buffer = iter->dsfi_buffer->next;
buf += rest_len;
res += rest_len;
bsize -= rest_len;
}
if (iter->dsfi_file_offset < iter->dsfi_file_len)
{
int readed;
if (-1 == strf_lseek (ses->dks_session->ses_file, iter->dsfi_file_offset, SEEK_SET))
{
log_error ("Can't seek in file %s", ses->dks_session->ses_file->ses_temp_file_name);
SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
goto done;
}
if (strses_is_utf8 (ses))
{
readed = read_wides_from_utf8_file (ses, bsize/sizeof (wchar_t), (utf8char *)buf, 0, NULL);
if (readed < 0) /* log_error is called inside read_wides_from_utf8_file() */
goto done;
iter->dsfi_file_offset = strf_lseek (ses->dks_session->ses_file, 0L, SEEK_CUR);
readed *= sizeof (wchar_t);
}
else
{
readed = strf_read (ses->dks_session->ses_file, buf, MIN (bsize, iter->dsfi_file_len - iter->dsfi_file_offset));
if (readed == -1)
{
SESSTAT_SET (ses->dks_session, SST_DISK_ERROR);
log_error ("Can't read from file %s", ses->dks_session->ses_file->ses_temp_file_name);
goto done;
}
iter->dsfi_file_offset += readed;
}
buf += readed;
res += readed;
bsize -= readed;
}
if (bsize == 0)
goto done;
rest_len = iter->dsfi_dorigin->dks_out_fill - iter->dsfi_offset;
if (bsize > rest_len)
bsize = rest_len;
memcpy (buf, iter->dsfi_dorigin->dks_out_buffer + iter->dsfi_offset, bsize);
iter->dsfi_offset += bsize;
res += bsize;
done:
#ifdef XMLPARSER_FEED_DEBUG
if (res > bsize_orig)
GPF_T;
if (buf + bsize > buf_orig + bsize_orig)
GPF_T;
if (0 == res)
fclose (feed_log);
else
fwrite (buf_orig, res, 1, feed_log);
#endif
return res;
}
void
bcfi_reset (bh_from_client_fwd_iter_t * iter, blob_handle_t *bh, client_connection_t *cli)
{
iter->bcfi_bh = bh;
iter->bcfi_cli = cli;
}
size_t
bcfi_read (void *read_cd, char *buf, size_t bsize)
{
bh_from_client_fwd_iter_t *iter = (bh_from_client_fwd_iter_t *)read_cd;
if ((BLOB_ALL_RECEIVED == iter->bcfi_bh->bh_all_received) ||
(BLOB_NULL_RECEIVED == iter->bcfi_bh->bh_all_received) )
return 0;
return bh_get_data_from_user (iter->bcfi_bh, iter->bcfi_cli, (db_buf_t)buf, bsize);
}
extern void bcfi_abend (void *read_cd)
{
caddr_t tmp_buf = dk_alloc (16 * 8192);
while (0 != bcfi_read (read_cd, tmp_buf, 16 * 8192)) {}
}
void
bdfi_reset (bh_from_disk_fwd_iter_t * iter, blob_handle_t *bh, query_instance_t *qi)
{
iter->bdfi_bh = bh;
iter->bdfi_qi = qi;
iter->bdfi_page_idx = 0;
iter->bdfi_page_data_pos = 0;
iter->bdfi_total_pos = 0;
}
size_t
bdfi_read (void *read_cd, char *tgtbuf, size_t bsize)
{
bh_from_disk_fwd_iter_t *iter = (bh_from_disk_fwd_iter_t *)read_cd;
it_cursor_t *tmp_itc;
index_tree_t * it;
buffer_desc_t *buf = NULL;
size_t res = 0;
if (iter->bdfi_total_pos >= iter->bdfi_bh->bh_diskbytes)
return 0;
tmp_itc = itc_create (NULL, iter->bdfi_qi->qi_trx);
it = iter->bdfi_bh->bh_it;
if (NULL != it)
{
itc_from_it (tmp_itc, it);
}
else
{
dbe_key_t* xper_key = sch_id_to_key (wi_inst.wi_schema, KI_COLS);
itc_from (tmp_itc, xper_key);
}
ITC_FAIL (tmp_itc)
{
dp_addr_t nth_page;
if (!iter->bdfi_bh->bh_page_dir_complete)
blob_read_dir (tmp_itc, &iter->bdfi_bh->bh_pages, &iter->bdfi_bh->bh_page_dir_complete, iter->bdfi_bh->bh_dir_page, NULL);
get_more:
nth_page = iter->bdfi_bh->bh_pages[iter->bdfi_page_idx];
if (!page_wait_blob_access (tmp_itc, nth_page, &buf, PA_READ, iter->bdfi_bh, 1))
{
iter->bdfi_total_pos = iter->bdfi_bh->bh_diskbytes;
}
else
{
long len = LONG_REF (buf->bd_buffer + DP_BLOB_LEN);
const char * oldstart = (char *)(buf->bd_buffer + DP_DATA + iter->bdfi_page_data_pos);
if (DV_BLOB_WIDE_HANDLE == DV_TYPE_OF (iter->bdfi_bh))
{
const char * start = oldstart;
int dec = eh_decode_buffer__UTF8 ((unichar *)tgtbuf, bsize / sizeof(unichar), &start, (const char *)(buf->bd_buffer + DP_DATA + len));
if (dec < 0)
{
log_info ("UTF-8 encoding error (%d) in wide-char blob dp = %d start = %d.", dec, nth_page, iter->bdfi_bh->bh_page);
iter->bdfi_total_pos = iter->bdfi_bh->bh_diskbytes;
}
else
{
iter->bdfi_page_data_pos += (start-oldstart);
iter->bdfi_total_pos += (start-oldstart);
dec *= sizeof (unichar);
res += dec;
tgtbuf += dec;
bsize -= dec;
}
}
else
{
size_t got = len - iter->bdfi_page_data_pos;
if (got > bsize)
got = bsize;
memcpy (tgtbuf, oldstart, got);
iter->bdfi_page_data_pos += got;
iter->bdfi_total_pos += got;
res += got;
tgtbuf += got;
bsize -= got;
}
if (iter->bdfi_page_data_pos == len)
{
iter->bdfi_page_idx++;
iter->bdfi_page_data_pos = 0;
}
#ifdef DEBUG
if (iter->bdfi_page_data_pos > len)
GPF_T;
if (iter->bdfi_total_pos > iter->bdfi_bh->bh_diskbytes)
GPF_T;
#endif
}
page_leave_outside_map (buf);
buf = NULL;
ITC_LEAVE_MAPS (tmp_itc);
if (bsize && (iter->bdfi_total_pos < iter->bdfi_bh->bh_diskbytes))
goto get_more; /* see above */
}
ITC_FAILED
{
if (NULL != buf)
page_leave_outside_map (buf);
itc_free (tmp_itc);
sqlr_new_error ("XE000", "XP9A5", "Error while reading XML from BLOB");
}
END_FAIL (tmp_itc);
itc_free (tmp_itc);
return res;
}
int
str_looks_like_serialized_xml (caddr_t source)
{
int slen = box_length (source);
if (IS_WIDE_STRING_DTP (box_tag (source)))
return XE_PLAIN_TEXT;
if (0 == strncmp (source, XPACK_PREFIX, XPER_ROOT_POS))
return XE_XPACK_SERIALIZATION;
if ((slen >= XPER_ROOT_POS + MIN_START_ROOT_RECORD_SZ + END_ROOT_RECORD_SZ) &&
(0 == strncmp (source, XPER_PREFIX, XPER_ROOT_POS)) &&
(slen <= PAGE_DATA_SZ) )
return XE_XPER_SERIALIZATION;
if ((slen > 3) && (DV_ARRAY_OF_POINTER == ((dtp_t *)source)[0]) &&
((DV_LONG_INT == ((dtp_t *)source)[1]) || (DV_SHORT_INT == ((dtp_t *)source)[1])) )
return XE_PLAIN_TEXT_OR_SERIALIZED_VECTOR;
return XE_PLAIN_TEXT;
}
int
strses_looks_like_serialized_xml (dk_session_t *source)
{
int slen = strses_length (source);
char buf[XPER_ROOT_POS];
strses_fragment_to_array (source, buf, 0, XPER_ROOT_POS);
if (IS_WIDE_STRING_DTP (box_tag (source)))
return XE_PLAIN_TEXT;
if ((slen >= XPER_ROOT_POS) && (0 == memcmp (buf, XPACK_PREFIX, XPER_ROOT_POS)))
return XE_XPACK_SERIALIZATION;
if ((slen >= XPER_ROOT_POS + MIN_START_ROOT_RECORD_SZ + END_ROOT_RECORD_SZ) &&
(0 == memcmp (buf, XPER_PREFIX, XPER_ROOT_POS)) &&
(slen <= PAGE_DATA_SZ) )
return XE_XPER_SERIALIZATION;
if ((slen > 3) && (DV_ARRAY_OF_POINTER == ((dtp_t *)source)[0]) &&
((DV_LONG_INT == ((dtp_t *)source)[1]) || (DV_SHORT_INT == ((dtp_t *)source)[1])) )
return XE_PLAIN_TEXT_OR_SERIALIZED_VECTOR;
return XE_PLAIN_TEXT;
}
/* IvAn/TextXperIndex/000815 Special case for blob checking has turned into separate function */
int
blob_looks_like_serialized_xml (query_instance_t * qi, blob_handle_t * bh)
{
volatile int res = 0;
it_cursor_t *itc;
buffer_desc_t *buf = NULL;
unsigned char *ptr;
xper_dbg_print ("Checking if a BLOB is a PERSISTENT XML.\n");
if (DV_BLOB_XPER_HANDLE == box_tag (bh))
return XE_XPER_SERIALIZATION;
if (DV_BLOB_WIDE_HANDLE == box_tag (bh))
return XE_PLAIN_TEXT;
if (bh->bh_length < XPER_ROOT_POS)
return XE_PLAIN_TEXT;
itc = itc_create (NULL, qi->qi_trx);
itc_from_it (itc, bh->bh_it);
ITC_FAIL (itc)
{
if (!page_wait_blob_access (itc, bh->bh_page, &buf, PA_READ, bh, 1))
{
return XE_PLAIN_TEXT;
}
ptr = buf->bd_buffer + DP_DATA;
if ((bh->bh_length >= XPER_ROOT_POS + MIN_START_ROOT_RECORD_SZ + END_ROOT_RECORD_SZ) &&
(0 == strncmp ((const char *) ptr, XPER_PREFIX, XPER_ROOT_POS) &&
DV_XML_MARKUP == ptr[XPER_ROOT_POS]) )
res = XE_XPER_SERIALIZATION;
else if (0 == strncmp ((const char *) ptr, XPACK_PREFIX, XPER_ROOT_POS))
res = XE_XPACK_SERIALIZATION;
else if ((bh->bh_length > 3) && (DV_ARRAY_OF_POINTER == ((dtp_t *)ptr)[0]) &&
((DV_LONG_INT == ((dtp_t *)ptr)[1]) || (DV_SHORT_INT == ((dtp_t *)ptr)[1])) )
res = XE_PLAIN_TEXT_OR_SERIALIZED_VECTOR;
else
res = XE_PLAIN_TEXT;
page_leave_outside_map (buf);
buf = NULL;
ITC_LEAVE_MAPS (itc);
}
ITC_FAILED
{
if (NULL != buf)
page_leave_outside_map (buf);
ITC_LEAVE_MAPS (itc);
itc_free (itc);
sqlr_new_error ("XE000", "XP9A6", "ITC error while checking persistent XML");
}
END_FAIL (itc);
itc_free (itc);
return res;
}
int
looks_like_serialized_xml (query_instance_t * qi, caddr_t source)
{
dtp_t dtp_of_source = DV_TYPE_OF (source);
if (DV_XML_ENTITY == dtp_of_source)
return XE_ENTITY_READY;
if (DV_STRINGP (source))
return str_looks_like_serialized_xml (source);
if (DV_STRING_SESSION == dtp_of_source)
return strses_looks_like_serialized_xml ((dk_session_t *)source);
if (IS_BLOB_HANDLE_DTP (dtp_of_source))
return blob_looks_like_serialized_xml (qi, (blob_handle_t *) source);
return XE_PLAIN_TEXT;
}
static void xper_get_blob_page_dir (xper_doc_t *xpd)
{
it_cursor_t *tmp_itc;
index_tree_t * it;
blob_handle_t * bh = xpd->xpd_bh;
if (bh->bh_page_dir_complete)
return;
tmp_itc =
itc_create (NULL, xpd->xd_qi->qi_trx);
it = xpd->xpd_bh->bh_it;
if (NULL != it)
{
itc_from_it (tmp_itc, it);
}
else
{
dbe_key_t* xper_key = sch_id_to_key (wi_inst.wi_schema, KI_COLS);
itc_from (tmp_itc, xper_key);
}
blob_read_dir (tmp_itc, &bh->bh_pages, &bh->bh_page_dir_complete, bh->bh_dir_page, NULL);
itc_free (tmp_itc);
}
/* IvAn/TextXmlIndex/000814 Like bif_xml_tree, xper_entity should accept
BLOBs with source XML like texts. */
xper_entity_t *
DBG_NAME (xper_entity) (DBG_PARAMS query_instance_t * qi, caddr_t source_arg, caddr_t vt_batch,
int is_html, caddr_t uri, caddr_t enc_name, lang_handler_t * lh, caddr_t dtd_config, int index_attrs)
{
dtp_t dtp_of_source_arg = DV_TYPE_OF (source_arg);
int source_sort = -1; /* An invalid value, just to kill the warning */
bh_from_client_fwd_iter_t bcfi;
bh_from_disk_fwd_iter_t bdfi;
dk_session_fwd_iter_t dsfi;
xml_read_func_t iter = NULL;
xml_read_abend_func_t iter_abend = NULL;
void *iter_data = NULL;
xper_doc_t *xpd;
buffer_desc_t *buf;
xper_entity_t *xpe;
volatile int rc = 1;
caddr_t rc_msg = NULL;
volatile s_size_t source_length = 0;
vxml_parser_config_t config;
xper_ctx_t context;
volatile char source_type = '?';
volatile int source_is_wide = 0;
caddr_t tmp_box;
xper_stag_t root_data;
vxml_parser_attrdata_t root_attrdata;
long pos;
xpd = (xper_doc_t *) DK_ALLOC (sizeof (xper_doc_t));
memset (xpd, 0, sizeof (xper_doc_t));
#ifdef MALLOC_DEBUG
xpd->xd_dbg_file = (char *) file;
xpd->xd_dbg_line = line;
#endif
xpd->xd_qi = qi_top_qi (qi);
xpd->xd_ref_count = 0;
xpd->xd_default_lh = lh;
memset (&context, 0, sizeof (context));
/* IvAn/DvBlobXper/001212 XPER support has changed */
if (dtp_of_source_arg == DV_XML_ENTITY)
{
caddr_t original_uri;
lang_handler_t *original_lh;
if (XE_IS_TREE (source_arg))
{
dk_free (xpd, sizeof (xper_doc_t));
dk_free_box (uri);
sqlr_new_error ("42000", "XE001",
"xml_persistent cannot convert XML tree entity (as argument 1) to XPER entity");
}
xper_dbg_print ("XPER here!\n");
original_uri = ((xml_entity_t *) source_arg)->xe_doc.xd->xd_uri;
original_lh = ((xml_entity_t *) source_arg)->xe_doc.xd->xd_default_lh;
if (
(lh != original_lh) ||
strcmp (((NULL == uri) ? "" : uri), ((NULL == original_uri) ? "" : original_uri)))
{ /* If base URI's differ, we should create new document to carry new path */
xpd->xpd_bh = (blob_handle_t *) box_copy_tree ((box_t) ((xml_entity_t *) source_arg)->xe_doc.xpd->xpd_bh);
xpd->xpd_state = XPD_PERSISTENT;
xpe = (xper_entity_t *) dk_alloc_box_zero (sizeof (xml_entity_un_t), DV_XML_ENTITY);
#ifdef XPER_DEBUG
xper_entity_alloc_ctr++;
#endif
xpe->_ = &xec_xper_xe;
xpe->xe_doc.xpd = xpd;
xpd->xd_top_doc = (xml_doc_t *) xpd;
xpd->xd_uri = uri;
xpd->xd_default_lh = lh;
xpd->xd_ref_count = 1;
fill_xper_entity (xpe, ((xper_entity_t *) source_arg)->xper_pos);
}
else
{ /* If base URI's are equal for both source and the result, then plain entity copying is OK */
dk_free (xpd, sizeof (xper_doc_t));
dk_free_box (uri);
xpe = (xper_entity_t *) xp_copy ((xml_entity_t *) source_arg);
}
return xpe;
}
#ifdef DV_BLOB_XPER_HANDLE
if (dtp_of_source_arg == DV_BLOB_XPER_HANDLE)
{
blob_handle_t *bh = (blob_handle_t *) box_copy_tree (source_arg);
xpd->xpd_bh = bh;
xper_get_blob_page_dir (xpd);
xpd->xpd_state = XPD_PERSISTENT;
xper_dbg_print ("XPER BLOB handle here!\n");
goto create_and_return_xpe;
}
#endif
if (dtp_of_source_arg == DV_BLOB_HANDLE)
{
blob_handle_t *bh = (blob_handle_t *)source_arg;
source_sort = blob_looks_like_serialized_xml (qi, bh);
if (XE_XPER_SERIALIZATION == source_sort)
{
bh = (blob_handle_t *) box_copy_tree (source_arg);
xpd->xpd_bh = bh;
xper_get_blob_page_dir (xpd);
xpd->xpd_state = XPD_PERSISTENT;
xper_dbg_print ("BLOB handle here!\n");
goto create_and_return_xpe;
}
if (XE_XPACK_SERIALIZATION == source_sort)
{
dk_free_box (uri);
sqlr_new_error ("42000", "XE024",
"Unable to convert packed XML serialized data into an persistent XML entity" );
}
if (bh->bh_ask_from_client)
{
bcfi_reset (&bcfi, bh, qi->qi_client);
source_type = 'C';
iter = bcfi_read;
iter_abend = bcfi_abend;
iter_data = &bcfi;
source_is_wide = ((dtp_of_source_arg == DV_BLOB_WIDE_HANDLE) ? 1 : 0);
goto parse_source;
}
source_type = 'B';
bdfi_reset (&bdfi, bh, qi);
iter = bdfi_read;
iter_data = &bdfi;
goto parse_source;
}
if (dtp_of_source_arg == DV_BLOB_WIDE_HANDLE)
{
blob_handle_t *bh = (blob_handle_t *)source_arg;
source_is_wide = 1;
if (bh->bh_ask_from_client)
{
bcfi_reset (&bcfi, bh, qi->qi_client);
source_type = 'C';
iter = bcfi_read;
iter_abend = bcfi_abend;
iter_data = &bcfi;
source_is_wide = ((dtp_of_source_arg == DV_BLOB_WIDE_HANDLE) ? 1 : 0);
goto parse_source;
}
bdfi_reset (&bdfi, bh, qi);
source_type = 'B';
iter = bdfi_read;
iter_data = &bdfi;
goto parse_source;
}
if ((dtp_of_source_arg == DV_SHORT_STRING_SERIAL) ||
(dtp_of_source_arg == DV_STRING) ||
(dtp_of_source_arg == DV_C_STRING))
{ /* 01234567 */
if (!strncasecmp (source_arg, "file://", 7))
{
sec_check_dba (qi, "<read XML from URL of type file://...>");
context.xpc_src_filename = file_native_name_from_iri_path_nchars (source_arg + 7, strlen (source_arg + 7));
file_path_assert (context.xpc_src_filename, NULL, 1);
xper_dbg_print_1 ("File '%s'\n", context.xpc_src_filename);
context.xpc_src_file = fopen (context.xpc_src_filename, "rb");
if (NULL == context.xpc_src_file)
{
caddr_t err = srv_make_new_error ("42000", "XP100", "Error opening file '%s'", context.xpc_src_filename);
xper_destroy_ctx (&context);
dk_free_box (uri);
sqlr_resignal (err);
}
source_type = 'F';
iter = file_read;
iter_data = context.xpc_src_file;
goto parse_source;
}
source_type = 'S';
source_length = box_length (source_arg) - 1;
goto parse_source;
}
if ((dtp_of_source_arg == DV_WIDE) ||
(dtp_of_source_arg == DV_LONG_WIDE))
{
source_length = box_length (source_arg) - sizeof (wchar_t);
source_type = 'S';
source_is_wide = 1;
goto parse_source;
}
if (dtp_of_source_arg == DV_STRING_SESSION)
{
dk_session_t *ses = (dk_session_t *) (source_arg);
source_sort = strses_looks_like_serialized_xml (ses);
if (XE_XPER_SERIALIZATION == source_sort)
{
dk_free_box (uri);
sqlr_new_error ("42000", "XE025",
"Unable to make a persistent XML entity from a string session that contains XPER data" );
}
if (XE_XPACK_SERIALIZATION == source_sort)
{
dk_free_box (uri);
sqlr_new_error ("42000", "XE026",
"Unable to make a persistent XML entity from a string session that contains packed XML serialized data" );
}
dsfi_reset (&dsfi, ses);
source_type = 'D';
iter = dsfi_read;
iter_data = &dsfi;
goto parse_source;
}
dk_free_box (uri);
sqlr_new_error ("42000", "XE004",
"xml_persistent needs a string, string session or BLOB as argument 1, not an arg of type %s (%d)",
dv_type_title (dtp_of_source_arg), dtp_of_source_arg);
parse_source:
QR_RESET_CTX
{
dbe_key_t* xper_key;
xpd->xpd_state = XPD_NEW;
context.xpc_doc = xpd;
xpd->xpd_bh = bh_alloc (DV_BLOB_XPER_HANDLE);
context.xpc_itc = itc_create (NULL, qi->qi_trx);
xper_key = sch_id_to_key (wi_inst.wi_schema, KI_COLS);
itc_from (context.xpc_itc, xper_key);
xpd->xpd_bh->bh_it = context.xpc_itc->itc_tree;
context.xpc_index_attrs = index_attrs;
context.xpc_attr_word_ctr = (index_attrs ? FIRST_ATTR_WORD_POS : 0);
memset (&root_data, 0, sizeof (root_data));
memset (&root_attrdata, 0, sizeof (root_attrdata));
/* IvAn/TextXperIndex/000815 To match xml tree format, "root" replaced with " root". */
root_data.type = XML_MKUP_STAG;
root_data.name = uname__root;
root_data.lh = lh;
root_data.wrs.xewr_main_beg = 0;
root_data.wrs.xewr_attr_beg = FIRST_ATTR_WORD_POS;
root_data.recalc_wrs = root_data.store_wrs = (int) context.xpc_attr_word_ctr;
root_data.attrdata = &root_attrdata;
ITC_FAIL (context.xpc_itc)
{
buf = it_new_page (context.xpc_itc->itc_tree, context.xpc_itc->itc_page,
DPF_BLOB, 0, 0);
ITC_LEAVE_MAPS (context.xpc_itc);
if (!buf)
{
xper_destroy_ctx (&context);
dk_free_box (uri);
sqlr_new_error ("XE000", "XP9A7", "Error allocating a new blob page while parsing XML");
}
xpd->xpd_bh->bh_page = buf->bd_page;
xper_blob_log_page_to_dir (xpd->xpd_bh, 0, buf->bd_page);
context.xpc_buf = buf;
}
ITC_FAILED
{
ITC_LEAVE_MAPS (context.xpc_itc);
xper_destroy_ctx (&context);
dk_free_box (uri);
sqlr_new_error ("XE000", "XP102", "ITC error while parsing XML");
}
END_FAIL (context.xpc_itc);
if ('S' == source_type)
source_sort = str_looks_like_serialized_xml (source_arg);
if (('S' == source_type) && (XE_XPER_SERIALIZATION == source_sort))
{ /* serialized blob content as a DV_X_STRING */
source_length = box_length (source_arg);
/* length of source is less then 2048 - so it always fit one page */
memcpy (buf->bd_buffer + DP_DATA, source_arg, source_length);
xpd->xpd_bh->bh_diskbytes = xpd->xpd_bh->bh_length = source_length;
LONG_SET/*_NA*/ (buf->bd_buffer + DP_BLOB_LEN, (int32) source_length);
LONG_SET/*_NA*/ (buf->bd_buffer + DP_OVERFLOW, 0);
rc = 1;
}
else if (('S' == source_type) && (XE_XPACK_SERIALIZATION == source_sort))
{ /* packed XML blob content as a DV_X_STRING */
dk_free_box (uri);
sqlr_new_error ("42000", "XE023", "Can not compose an XPER entity by deserialization of a packed XML");
}
else
{ /* string is an XML document to parse */
memcpy (buf->bd_buffer + DP_DATA, XPER_PREFIX, XPER_ROOT_POS);
xpd->xpd_bh->bh_length = xpd->xpd_bh->bh_diskbytes = XPER_ROOT_POS;
LONG_SET/*_NA*/ (buf->bd_buffer + DP_BLOB_LEN, XPER_ROOT_POS);
LONG_SET/*_NA*/ (buf->bd_buffer + DP_OVERFLOW, 0);
memset (&config, 0, sizeof (config));
config.input_is_wide = source_is_wide;
config.input_is_ge = is_html & GE_XML;
config.input_is_html = is_html & ~(FINE_XSLT | GE_XML | WEBIMPORT_HTML | FINE_XML_SRCPOS);
config.input_is_xslt = is_html & FINE_XSLT;
config.user_encoding_handler = intl_find_user_charset;
config.uri_resolver = (VXmlUriResolver)xml_uri_resolve_like_get;
config.uri_reader = (VXmlUriReader)xml_uri_get;
config.uri_appdata = qi; /* Both xml_uri_resolve_like_get and xml_uri_get uses qi as first argument */
config.error_reporter = (VXmlErrorReporter)(sqlr_error);
config.initial_src_enc_name = enc_name;
config.dtd_config = dtd_config;
config.uri = ((NULL == uri) ? uname___empty : uri);
config.root_lang_handler = lh;
if (file_read == iter)
config.feed_buf_size = 0x10000;
context.xpc_parser = VXmlParserCreate (&config);
VXmlSetUserData (context.xpc_parser, &context);
VXmlSetElementHandler (context.xpc_parser, cb_element_start, cb_element_end);
VXmlSetIdHandler (context.xpc_parser, (VXmlIdHandler) cb_id);
VXmlSetCharacterDataHandler (context.xpc_parser, cb_character);
VXmlSetEntityRefHandler (context.xpc_parser, cb_entity);
VXmlSetProcessingInstructionHandler (context.xpc_parser, cb_pi);
VXmlSetCommentHandler (context.xpc_parser, cb_comment);
/*
VXmlSetDtdHandler (context.xpc_parser, (VXmlDtdHandler) cb_dtd);
*/
/* start root tag */
dk_set_push (&context.xpc_poss, (void *)((ptrlong)(xpd->xpd_bh->bh_length)));
tmp_box = start_tag_record (&root_data);
xper_blob_append_box (&context, tmp_box);
dk_free_box (tmp_box);
/* IvAn/TextXperIndex/000815 Word counter added, "root" replaced with " root" */
context.xpc_tn_length1 = 7 /*DV_SHORT_STR " root" */ ;
if (iter)
VXmlParserInput (context.xpc_parser, iter, iter_data);
rc = VXmlParse (context.xpc_parser, source_arg, source_length);
if (!rc)
{
if (NULL != iter_abend)
{
iter_abend (iter_data);
iter_abend = NULL;
}
rc_msg = VXmlFullErrorMessage (context.xpc_parser);
}
pos = (long) (ptrlong) context.xpc_poss->data;
if (pos < 0)
{
dk_set_pop (&context.xpc_poss);
pos = (long) (ptrlong) context.xpc_poss->data;
}
context.xpc_poss->data = (void *) (ptrlong) -pos;
/* end root tag */
if (XML_MKUP_ETAG != context.xpc_word_hider)
context.xpc_main_word_ctr += 1;
ITC_FAIL (context.xpc_itc)
{
/* IvAn/TextXperIndex/000815 Word counter added, "root" replaced with " root" */
set_long_in_blob (&context, XPER_ROOT_POS + 7 /*DV_SHORT_STR " root" */ +
STR_NAME_OFF + STR_END_TAG_OFF,
(long) xpd->xpd_bh->bh_length);
set_long_in_blob (&context, XPER_ROOT_POS + 7 /*DV_SHORT_STR " root" */ +
STR_NAME_OFF + STR_END_WORD_OFF,
(long) context.xpc_main_word_ctr);
if (context.xpc_index_attrs)
{
set_long_in_blob (&context, XPER_ROOT_POS + 7 /*DV_SHORT_STR " root" */ +
STR_NAME_OFF + STR_ADDON_OR_ATTR_OFF + (2 * 4),
(long) context.xpc_attr_word_ctr);
}
ITC_LEAVE_MAPS (context.xpc_itc);
}
ITC_FAILED
{
ITC_LEAVE_MAPS (context.xpc_itc);
dk_free_box (uri);
sqlr_new_error ("XE000", "XP9A9", "ITC error on writing persistent XML");
}
END_FAIL (context.xpc_itc);
tmp_box = end_tag_record (root_data.name);
xper_blob_append_box (&context, tmp_box);
dk_free_box (tmp_box);
xpd->xd_weight = XML_XPER_DOC_WEIGHT;
xpd->xd_cost = context.xpc_parser->input_cost;
if (bcfi_read == iter) /* This input is not reproducible so the cost is infinite */
xpd->xd_cost = XML_MAX_DOC_COST;
/* Now it's time to get the dtd in our ownership */
dtd_addref (context.xpc_parser->validator.dv_dtd, 0);
if (NULL != xpd->xd_dtd)
dtd_release (xpd->xd_dtd);
xpd->xd_dtd = context.xpc_parser->validator.dv_dtd;
dk_free_box ((caddr_t) (xpd->xd_id_dict));
dk_free_box (xpd->xd_id_scan);
xpd->xd_id_dict = NULL;
xpd->xd_id_scan = NULL;
/* Now we put dtd into blob, if there's something to put */
if (0 < dtd_get_buffer_length (xpd->xd_dtd))
{
ITC_FAIL (context.xpc_itc)
{
set_long_in_blob (&context, XPER_ROOT_POS + 7 /*DV_SHORT_STR " root" */ +
STR_NAME_OFF + STR_NS_OFF,
(long) xpd->xpd_bh->bh_length);
ITC_LEAVE_MAPS (context.xpc_itc);
}
ITC_FAILED
{
ITC_LEAVE_MAPS (context.xpc_itc);
dk_free_box (uri);
sqlr_new_error ("XE000", "XP9A9", "ITC error on writing persistent XML");
}
END_FAIL (context.xpc_itc);
blob_append_dtd (&context, xpd->xd_dtd);
}
}
buf_set_dirty (context.xpc_buf);
page_leave_outside_map (context.xpc_buf);
context.xpc_buf = NULL;
if (!rc)
{
dk_free_box (uri);
sqlr_new_error ("42000", "XP101", "%.1500s", ((NULL == rc_msg) ? "Error parsing XML" : rc_msg));
}
}
QR_RESET_CODE
{
du_thread_t *self = THREAD_CURRENT_THREAD;
caddr_t err = thr_get_error_code (self);
if (NULL != rc_msg)
dk_free_box (rc_msg);
if (NULL != iter_abend)
{
iter_abend (iter_data);
iter_abend = NULL;
}
POP_QR_RESET;
xper_destroy_ctx (&context);
sqlr_resignal (err);
}
END_QR_RESET;
create_and_return_xpe:
xpe = (xper_entity_t *) dk_alloc_box_zero (sizeof (xml_entity_un_t), DV_XML_ENTITY);
#ifdef XPER_DEBUG
xper_entity_alloc_ctr++;
#endif
xpe->_ = &xec_xper_xe;
xpe->xe_doc.xpd = xpd;
xpd->xd_top_doc = (xml_doc_t *) xpd;
xpd->xd_uri = uri;
xpd->xd_ref_count = 1;
context.xpc_doc = NULL; /* To prevent destruction in xper_destroy_ctx */
xper_destroy_ctx (&context);
xper_blob_truncate_dir_buf (xpd->xpd_bh);
fill_xper_entity (xpe, XPER_ROOT_POS);
return xpe;
}
xml_entity_t *
DBG_NAME (xp_copy) (DBG_PARAMS xml_entity_t * xe)
{
xper_entity_t *xpe;
xper_dbg_print ("xp_copy\n");
if (DV_XML_ENTITY != box_tag (xe))
sqlr_new_error ("XE000", "XP9B1", "Attempt to copy a box with invalid tag.");
xpe = (xper_entity_t *) dk_alloc_box (sizeof (xml_entity_un_t), DV_XML_ENTITY);
#ifdef XPER_DEBUG
xper_entity_alloc_ctr++;
#endif
memcpy (xpe, xe, sizeof (xml_entity_un_t));
xpe->xper_name = box_copy (xpe->xper_name);
if (NULL != xpe->xper_text)
xpe->xper_text = box_copy (xpe->xper_text);
xpe->xe_doc.xd->xd_ref_count++;
if (NULL != xpe->xe_referer)
xpe->xe_referer = xpe->xe_referer->_->xe_copy (xpe->xe_referer);
if (NULL != xpe->xper_cut_ent)
xpe->xper_cut_ent = (xper_entity_t *) xp_copy ((xml_entity_t *) (xpe->xper_cut_ent));
if (NULL != xpe->xe_attr_name)
xpe->xe_attr_name = box_copy_tree (xpe->xe_attr_name);
return (xml_entity_t *) xpe;
}
xper_entity_t *
xper_copy_and_refill (xper_entity_t * src_xpe, long new_pos)
{
xper_entity_t *res_xpe = (xper_entity_t *) dk_alloc_box (sizeof (xml_entity_un_t), DV_XML_ENTITY);
#ifdef XPER_DEBUG
xper_entity_alloc_ctr++;
#endif
memcpy (res_xpe, src_xpe, sizeof (xper_entity_t));
res_xpe->xper_name = NULL;
res_xpe->xper_text = NULL;
res_xpe->xe_doc.xd->xd_ref_count++;
if (NULL != res_xpe->xe_referer)
res_xpe->xe_referer = res_xpe->xe_referer->_->xe_copy (res_xpe->xe_referer);
res_xpe->xper_cut_pos = 0L;
res_xpe->xper_cut_ent = NULL;
fill_xper_entity (res_xpe, new_pos);
return res_xpe;
}
lang_handler_t *
xper_find_lang_handler (xper_entity_t * xpe)
{
if (XML_MKUP_STAG == xpe->xper_type)
{
caddr_t attrvalue = xper_get_attribute (xpe, "xml:lang", 8 /* == strlen("xml:lang") */ );
if (NULL != attrvalue)
{
/* lh_get_handler obtains UTF-8 string, but it's safe because all language
names should be 7-bit, and any wider string will not match any name, anyway. */
lang_handler_t *res = lh_get_handler (attrvalue);
dk_free_box (attrvalue);
return res;
}
}
if (0 == xpe->xper_parent)
return server_default_lh;
xpe = xper_copy_and_refill (xpe, xpe->xper_parent);
for (;;)
{
if (XML_MKUP_STAG == xpe->xper_type)
{
caddr_t attrvalue = xper_get_attribute (xpe, "xml:lang", 8 /* == strlen("xml:lang") */ );
if (NULL != attrvalue)
{
/* lh_get_handler obtains UTF-8 string, but it's safe because all language
names should be 7-bit, and any wider string will not match any name, anyway. */
lang_handler_t *res = lh_get_handler (attrvalue);
dk_free_box (attrvalue);
dk_free_box ((box_t) xpe);
return res;
}
}
if ((XPER_ROOT_POS == xpe->xper_parent) || (0 == xpe->xper_parent))
break;
fill_xper_entity (xpe, xpe->xper_parent);
}
dk_free_box ((box_t) xpe);
return server_default_lh;
}
int
xp_up (xml_entity_t * xe, XT * node, int up_flags)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
int rc;
xper_dbg_print ("xp_parent\n");
/*
Attributes are not children of its element.
But the element is parent of its attributes.
See XPATH, 5.3 "Attribute Nodes"
*/
if (NULL != xe->xe_attr_name)
{
/* If no sideway then element's name should match the test */
if (!(up_flags & XE_UP_SIDEWAY) && !xp_element_name_test (xe, node))
return XI_AT_END;
dk_free_box (xe->xe_attr_name);
xe->xe_attr_name = NULL;
if (!(up_flags & XE_UP_SIDEWAY))
return XI_RESULT;
goto go_sideway;
}
/* Top of the (sub)document */
if ((0 == xpe->xper_parent) ||
((XPER_ROOT_POS == xpe->xper_parent) && (NULL != xpe->xe_referer)))
{
xml_entity_t *up = xpe->xe_referer;
if ((NULL == up) || !(up_flags & XE_UP_MAY_TRANSIT))
return XI_AT_END;
/* No xpe->xe_doc.xtd->xd_ref_count++; before this destroy */
xpe->xe_referer = NULL;
xp_destroy ((xml_entity_t *) xpe);
#ifdef XPER_DEBUG
xper_entity_free_ctr--;
#endif
memcpy (xpe, up, sizeof (xml_entity_un_t));
box_tag_modify (up, DV_ARRAY_OF_LONG); /* do not call destructor, content still referenced due to copy *
* set tag to array, not string cause strings aligned differently */
dk_free_box ((box_t) up);
if (up_flags & XE_UP_SIDEWAY)
goto go_sideway;
if (up_flags & XE_UP_MAY_TRANSIT_ONCE)
return XI_RESULT;
return (xpe->_->xe_up ((xml_entity_t *) xpe, node, up_flags));
}
/* Plain non-attribute entity */
if (up_flags & XE_UP_SIDEWAY)
{
fill_xper_entity (xpe, xpe->xper_parent);
goto go_sideway;
}
if (!xper_node_test (xpe, xpe->xper_parent, node))
return XI_AT_END;
fill_xper_entity (xpe, xpe->xper_parent);
return XI_RESULT;
go_sideway:
/* in-place check or sideway go */
if (up_flags & XE_UP_SIDEWAY_FWD)
{
if (up_flags & XE_UP_SIDEWAY_WR)
rc = (xpe->_->xe_next_sibling_wr ((xml_entity_t *) xpe, node));
else
rc = (xpe->_->xe_next_sibling ((xml_entity_t *) xpe, node));
}
else
{
if (up_flags & XE_UP_SIDEWAY_WR)
rc = (xpe->_->xe_prev_sibling_wr ((xml_entity_t *) xpe, node));
else
rc = (xpe->_->xe_prev_sibling ((xml_entity_t *) xpe, node));
}
return rc;
}
#define XE_NAME(xe) (xe->xe_doc.xd->xd_uri ? xe->xe_doc.xd->xd_uri : "<no URI>")
int
xp_down (xml_entity_t * xe, XT * node)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
xml_entity_t *ref_copy, *old_back, *new_back, *refd;
caddr_t rel_uri;
char *sysid, *sysid_base_uri;
if ((XML_MKUP_REF != xpe->xper_type) || (NULL != xpe->xe_attr_name))
{
if (!xp_ent_name_test ((xml_entity_t *) xpe, node))
return XI_AT_END;
return XI_RESULT;
}
old_back = xpe->xe_referer;
rel_uri = xper_get_attribute (xpe, "SYSTEM", 6);
sysid_base_uri = (char *) xe_get_sysid_base_uri ((xml_entity_t *) (xpe));
if (NULL == rel_uri)
{
rel_uri = sysid = (char *) xe_get_sysid ((xml_entity_t *) (xpe), xpe->xper_name);
}
else
sysid = NULL;
if (NULL != rel_uri)
{
refd = xp_reference (xpe->xe_doc.xpd->xd_qi, sysid_base_uri, rel_uri, xpe->xe_doc.xd, NULL);
}
else
{
caddr_t err = NULL;
refd = xp_reference (xpe->xe_doc.xpd->xd_qi, sysid_base_uri, xpe->xper_name, xpe->xe_doc.xd, &err);
if (NULL == refd)
{
dk_free_tree (err);
return XI_AT_END;
}
}
xpe->xe_referer = NULL;
new_back = xpe->_->xe_copy ((xml_entity_t *) xpe);
new_back->xe_referer = old_back;
xp_destroy ((xml_entity_t *) (xpe));
#ifdef XPER_DEBUG
xper_entity_free_ctr--;
#endif
ref_copy = refd->_->xe_copy ((xml_entity_t *) refd);
memcpy (xpe, ref_copy, sizeof (xml_entity_un_t));
box_tag_modify (ref_copy, DV_ARRAY_OF_LONG); /* no recursive destr etc.
* the tag is not a string tag because of string alignment being to 16 whereas other boxes to 8 */
dk_free_box ((box_t) ref_copy);
if (sysid != rel_uri)
dk_free_box (rel_uri);
xpe->xe_referer = new_back;
if (XI_RESULT == xpe->_->xe_first_child ((xml_entity_t *) xpe, node))
return XI_RESULT;
xpe->_->xe_up ((xml_entity_t *) xpe, (XT *) XP_NODE, (XE_UP_MAY_TRANSIT | XE_UP_MAY_TRANSIT_ONCE));
return XI_AT_END;
}
int
xp_down_rev (xml_entity_t * xe, XT * node)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
xml_entity_t *save_referer, *topmost;
xml_entity_t *last_good = NULL;
int res;
if ((XML_MKUP_REF != xpe->xper_type) || (NULL != xpe->xe_attr_name))
{
if (!xp_ent_name_test ((xml_entity_t *) xpe, node))
return XI_AT_END;
return XI_RESULT;
}
if (XI_AT_END == xe->_->xe_down (xe, (XT *) XP_NODE))
return XI_AT_END;
/* This catch implements some tricky thing. We set xe_referer to
prevent xe_next_sibling from running outside current subdocument:
xe_up will not know that it's a subdocument (will think that it's a whole doc)
Then we restore the status quo. */
save_referer = xe->xe_referer;
xe->xe_referer = NULL;
QR_RESET_CTX
{
xml_entity_t *last_good = NULL;
if (xe->_->xe_ent_name_test (xe, node))
last_good = xe->_->xe_copy (xe);
while (XI_RESULT == xe->_->xe_next_sibling (xe, node))
{
dk_free_tree ((box_t) last_good);
last_good = xe->_->xe_copy (xe);
}
if ((NULL != last_good) && !xe->_->xe_is_same_as (xe, last_good))
{
/* No xpe->xe_doc.xtd->xd_ref_count++; before this destroy */
/* No need in xe->xe_referer = NULL here because it's done above */
xp_destroy ((xml_entity_t *) xpe);
#ifdef XPER_DEBUG
xper_entity_free_ctr--;
#endif
memcpy (xpe, last_good, sizeof (xml_entity_un_t));
box_tag_modify (last_good, DV_ARRAY_OF_LONG); /* do not call destructor, content still referenced due to copy *
* set tag to array, not string cause strings aligned differently */
dk_free_box ((box_t) last_good);
}
else
dk_free_tree ((box_t) last_good);
res = (NULL != last_good) ? XI_RESULT : XI_AT_END;
topmost = xe;
while (NULL != topmost->xe_referer)
topmost = topmost->xe_referer;
topmost->xe_referer = save_referer;
}
QR_RESET_CODE
{
du_thread_t * self = THREAD_CURRENT_THREAD;
caddr_t err = thr_get_error_code (self);
POP_QR_RESET;
dk_free_box ((caddr_t) last_good);
topmost = xe;
while (NULL != topmost->xe_referer)
topmost = topmost->xe_referer;
topmost->xe_referer = save_referer;
sqlr_resignal (err);
return XI_AT_END; /* Never reached */
}
END_QR_RESET;
if (XI_RESULT == res)
return XI_RESULT;
xe->_->xe_up (xe, (XT *) XP_NODE, (XE_UP_MAY_TRANSIT | XE_UP_MAY_TRANSIT_ONCE));
return XI_AT_END;
}
int
xp_get_child_count_any (xml_entity_t * xe)
{
int res = 0;
int rc = xe->_->xe_first_child (xe, (XT *) XP_NODE);
if (XI_RESULT != rc)
return 0;
res++;
while (XI_RESULT == xe->_->xe_next_sibling (xe, (XT *) XP_NODE))
res++;
xe->_->xe_up (xe, (XT *) XP_NODE, XE_UP_MAY_TRANSIT);
return res;
}
int
xp_next_sibling (xml_entity_t * xe, XT * node_test)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
caddr_t tmp_box;
long save, save_parent, left;
xper_dbg_print ("xp_next\n");
if (NULL != xpe->xe_attr_name)
return XI_AT_END;
if (0 == xpe->xper_parent)
return xp_up ((xml_entity_t *) xpe, node_test, (XE_UP_MAY_TRANSIT | XE_UP_SIDEWAY | XE_UP_SIDEWAY_FWD));
if (XML_MKUP_ETAG == xpe->xper_type)
return XI_AT_END;
save = xpe->xper_pos;
save_parent = xpe->xper_parent;
for (;;)
{
left = xpe->xper_pos;
if (XML_MKUP_STAG == xpe->xper_type)
{
save_parent = xpe->xper_parent;
fill_xper_entity (xpe, xpe->xper_end);
}
if (uname__root == xpe->xper_name)
break;
tmp_box = get_tag_data (xpe, xpe->xper_next_item);
if (DV_XML_MARKUP == DV_TYPE_OF (tmp_box) && XML_MKUP_ETAG == tmp_box[0])
{
dk_free_box (tmp_box);
break;
}
dk_free_box (tmp_box);
fill_xper_entity (xpe, xpe->xper_next_item);
xpe->xper_parent = save_parent;
xpe->xper_left = left;
if (XML_MKUP_TEXT == xpe->xper_type)
xpe->xper_start_word++;
if (XI_RESULT == xp_down ((xml_entity_t *) xpe, node_test))
return XI_RESULT;
}
fill_xper_entity (xpe, save);
if (XPER_ROOT_POS == xpe->xper_parent)
return xp_up ((xml_entity_t *) xpe, node_test, (XE_UP_MAY_TRANSIT | XE_UP_SIDEWAY | XE_UP_SIDEWAY_FWD));
return XI_AT_END;
}
int
xp_next_sibling_wr (xml_entity_t * xe, XT * node_test)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
xp_instance_t *xqi = xe->xe_doc.xd->xd_top_doc->xd_xqi;
caddr_t tmp_box;
long save, save_parent, left;
xper_dbg_print ("xp_next\n");
if (NULL != xpe->xe_attr_name)
return XI_AT_END;
if (0 == xpe->xper_parent)
return xp_up ((xml_entity_t *) xpe, node_test, (XE_UP_MAY_TRANSIT | XE_UP_SIDEWAY | XE_UP_SIDEWAY_FWD | XE_UP_SIDEWAY_WR));
if (XML_MKUP_ETAG == xpe->xper_type)
return XI_AT_END;
save = xpe->xper_pos;
save_parent = xpe->xper_parent;
for (;;)
{
left = xpe->xper_pos;
if (XML_MKUP_STAG == xpe->xper_type)
{
save_parent = xpe->xper_parent;
fill_xper_entity (xpe, xpe->xper_end);
}
if (uname__root == xpe->xper_name)
break;
tmp_box = get_tag_data (xpe, xpe->xper_next_item);
if (DV_XML_MARKUP == DV_TYPE_OF (tmp_box) && XML_MKUP_ETAG == tmp_box[0])
{
dk_free_box (tmp_box);
break;
}
dk_free_box (tmp_box);
fill_xper_entity (xpe, xpe->xper_next_item);
xpe->xper_parent = save_parent;
xpe->xper_left = left;
if (!txs_is_hit_in (xqi->xqi_text_node, (caddr_t *) xqi->xqi_qi, (xml_entity_t *) (xpe)))
continue;
if (XI_RESULT == xp_down ((xml_entity_t *) xpe, node_test))
return XI_RESULT;
}
fill_xper_entity (xpe, save);
if (XPER_ROOT_POS == xpe->xper_parent)
return xp_up ((xml_entity_t *) xpe, node_test, (XE_UP_MAY_TRANSIT | XE_UP_SIDEWAY | XE_UP_SIDEWAY_FWD | XE_UP_SIDEWAY_WR));
return XI_AT_END;
}
int
xp_first_child (xml_entity_t * xe, XT * node_test)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
caddr_t tmp_box;
long save = xpe->xper_pos;
long save_parent = xpe->xper_pos;
long left;
xper_dbg_print ("xp_first_child\n");
if (NULL != xpe->xe_attr_name)
return XI_AT_END;
if (XML_MKUP_STAG != xpe->xper_type)
return XI_AT_END;
fill_xper_entity (xpe, xpe->xper_next_item);
for (;;)
{
switch (xpe->xper_type)
{
case XML_MKUP_ETAG:
fill_xper_entity (xpe, save);
return XI_AT_END;
case XML_MKUP_STAG:
break;
case XML_MKUP_TEXT:
xpe->xper_start_word++;
case XML_MKUP_COMMENT:
xpe->xper_parent = save;
xpe->xper_left = 0;
/* break; -- removed to avoid 'unreacheable end of loop' in AIX cc */
}
break;
}
if (XI_RESULT == xp_down ((xml_entity_t *) xpe, node_test))
return XI_RESULT;
for (;;)
{
left = xpe->xper_pos;
if (XML_MKUP_STAG == xpe->xper_type)
{
save_parent = xpe->xper_parent;
fill_xper_entity (xpe, xpe->xper_end);
}
if (uname__root == xpe->xper_name)
break;
tmp_box = get_tag_data (xpe, xpe->xper_next_item);
if (DV_XML_MARKUP == DV_TYPE_OF (tmp_box) && XML_MKUP_ETAG == tmp_box[0])
{
dk_free_box (tmp_box);
break;
}
dk_free_box (tmp_box);
fill_xper_entity (xpe, xpe->xper_next_item);
xpe->xper_parent = save_parent;
xpe->xper_left = left;
if (XI_RESULT == xp_down ((xml_entity_t *) xpe, node_test))
return XI_RESULT;
}
fill_xper_entity (xpe, save);
return XI_AT_END;
}
int
xp_child_count (xml_entity_t * xe)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
long save = xpe->xper_pos;
int count;
xper_dbg_print ("xp_child_count\n");
if (!xpe->xper_first_child)
return 0;
fill_xper_entity (xpe, xpe->xper_first_child);
for (count = 1; xpe->xper_right; ++count)
fill_xper_entity (xpe, xpe->xper_right);
fill_xper_entity (xpe, save);
return count;
}
int
xp_go_left (xper_entity_t * xpe)
{
if (!xpe->xper_left)
return 0;
if (xpe->xper_left < xpe->xper_pos)
{
fill_xper_entity (xpe, xpe->xper_left);
}
else
{
long save = xpe->xper_pos;
long left = xpe->xper_left;
for (;;)
{
xpe->xper_left = (XPER_ROOT_POS - 1);
fill_xper_entity (xpe, xpe->xper_right);
if ((XPER_ROOT_POS - 1) != xpe->xper_left)
{
left = xpe->xper_left;
break;
}
if (XML_MKUP_ETAG == xpe->xper_type)
break;
}
if (!xpe->xper_left)
{
if (!xpe->xper_parent)
return 0;
fill_xper_entity (xpe, xpe->xper_parent);
}
else
{
fill_xper_entity (xpe, xpe->xper_left);
}
if (xpe->xper_next_item >= save)
return 0;
for (;;)
{
left = xpe->xper_pos;
if (XML_MKUP_STAG == xpe->xper_type)
{
if (xpe->xper_end >= save)
break;
fill_xper_entity (xpe, xpe->xper_end);
if (xpe->xper_next_item >= save)
{
fill_xper_entity (xpe, left);
break;
}
fill_xper_entity (xpe, xpe->xper_next_item);
if (xpe->xper_pos >= save)
{
fill_xper_entity (xpe, left);
break;
}
fill_xper_entity (xpe, xpe->xper_next_item);
continue;
}
if (xpe->xper_next_item >= save)
break;
fill_xper_entity (xpe, xpe->xper_next_item);
xpe->xper_left = left;
}
}
return 1;
}
int
xp_prev_sibling (xml_entity_t * xe, XT * node_test)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
long save = xpe->xper_pos;
xper_dbg_print ("xp_prev_sibling\n");
if ((0 == xpe->xper_parent) || ((XPER_ROOT_POS == xpe->xper_parent) && (0 == xpe->xper_left)))
return xp_up ((xml_entity_t *) xpe, node_test, (XE_UP_MAY_TRANSIT | XE_UP_SIDEWAY /*| XE_UP_SIDEWAY_FWD */ ));
if (0 == xpe->xper_left)
return XI_AT_END;
do
{
if (!xp_go_left (xpe))
break;
if (XI_RESULT == xp_down_rev ((xml_entity_t *) xpe, node_test))
return XI_RESULT;
}
while (xpe->xper_left);
fill_xper_entity (xpe, save);
if (XPER_ROOT_POS == xpe->xper_parent)
return xp_up ((xml_entity_t *) xpe, node_test, (XE_UP_MAY_TRANSIT | XE_UP_SIDEWAY /*| XE_UP_SIDEWAY_FWD */ ));
return XI_AT_END;
}
int
DBG_NAME(xp_attribute) (DBG_PARAMS xml_entity_t * xe, int start, XT * node, caddr_t * ret, caddr_t * name_ret)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
unsigned char *ptr, *tptr;
caddr_t buf, ns;
size_t len, num_word, atts_fill, addons;
size_t inx;
int nt_res;
xper_dbg_print ("xp_attribute\n");
if (XML_MKUP_STAG != xpe->xper_type)
return XI_NO_ATTRIBUTE;
buf = get_tag_data (xpe, xpe->xper_pos);
len = box_length (buf);
ptr = (unsigned char *) (buf + 1);
ptr += full_dv_string_length (ptr);
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
if (-1 == start)
start = 0;
for (inx = 0; (int)(inx) < start * 2 && inx < atts_fill; inx++)
ptr += full_dv_string_length (ptr);
for (; inx + 1 < atts_fill; inx += 2)
{
int have_ns;
unsigned char *local;
size_t local_len;
tptr = ptr;
ns = NULL;
if (DV_ARRAY_OF_POINTER == *ptr)
{
have_ns = 1;
tptr += 5;
ns = xper_get_namespace (xpe, LONG_REF_NA (tptr + full_dv_string_length (tptr) + 1));
if (uname___empty == ns)
{
ns = NULL;
have_ns = 0;
}
}
else
have_ns = 0;
len = skip_string_length (&tptr);
if (have_ns)
{
for (local = tptr + len; (local > tptr) && (':' != local[-1]); local--) /* empty body*/ ;
local_len = tptr + len - local;
}
else
{
local = tptr;
local_len = len;
}
if (!have_ns && !ST_P (node, XP_NAME_EXACT) && (local_len >= 5) && !memcmp (local, "xmlns", 5))
nt_res = 0;
else
nt_res = xt_node_test_match_parts (node, (char *) local, local_len, ns);
if (nt_res)
{
if (name_ret)
{
if (ns)
{
caddr_t qname;
BOX_DV_UNAME_COLONCONCAT4 (qname, ns, (char *)local, local_len);
XP_SET (name_ret, qname);
dk_free_box (ns);
}
else
{
XP_SET (name_ret, box_dv_uname_nchars ((char *) (tptr), len));
}
}
if (ret)
{
ptr += full_dv_string_length (ptr);
len = skip_string_length (&ptr);
XP_SET (ret, box_dv_short_nchars ((const char *)ptr, len));
}
dk_free_box (buf);
return (int) (inx / 2 + 1);
}
if (ns)
dk_free_box (ns);
ptr += full_dv_string_length (ptr);
ptr += full_dv_string_length (ptr);
}
dk_free_box (buf);
return XI_NO_ATTRIBUTE;
}
caddr_t
xp_attrvalue (xml_entity_t * xe, caddr_t qname)
{
caddr_t val = NULL;
XT *test = xp_make_name_test_from_qname (NULL /* no xpp needed if qname is expanded */, qname, 1);
xe->_->xe_attribute (xe, -1, test, &val, NULL);
dk_free_tree ((caddr_t) test);
return val;
}
caddr_t
xp_currattrvalue (xml_entity_t * xe)
{
caddr_t val;
caddr_t qname = xe->xe_attr_name;
if (NULL == qname)
GPF_T;
val = xe->_->xe_attrvalue (xe, qname);
if (NULL == val)
GPF_T;
return val;
}
size_t
xp_data_attribute_count (xml_entity_t * xe)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
unsigned char *ptr, *tptr;
caddr_t buf;
size_t len, num_word, atts_fill, addons;
size_t inx, ctr;
if (XML_MKUP_STAG != xpe->xper_type)
return 0;
buf = get_tag_data (xpe, xpe->xper_pos);
len = box_length (buf);
ptr = (unsigned char *) (buf + 1);
ptr += full_dv_string_length (ptr);
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (ctr = inx = 0; inx + 1 < atts_fill; inx += 2)
{
tptr = ptr;
if (DV_ARRAY_OF_POINTER == *ptr)
ctr++;
else
{
len = skip_string_length (&tptr);
if ((5 > len) || memcmp (tptr, "xmlns", 5))
ctr++;
}
ptr += full_dv_string_length (ptr);
ptr += full_dv_string_length (ptr);
}
dk_free_box (buf);
return ctr;
}
static caddr_t
xper_get_attribute (xper_entity_t * xpe, const char * name, int name_len)
{
unsigned char *ptr, *tptr;
caddr_t buf;
long len, num_word, atts_fill, addons;
int inx;
xper_dbg_print ("xper_get_attribute\n");
switch (xpe->xper_type)
{
case XML_MKUP_ETAG:
case XML_MKUP_COMMENT:
case XML_MKUP_TEXT:
return NULL;
}
buf = get_tag_data (xpe, xpe->xper_pos);
len = box_length (buf);
ptr = (unsigned char *) (buf + 1);
ptr += full_dv_string_length (ptr);
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (inx = 0; inx + 1 < atts_fill; inx += 2)
{
tptr = ptr;
if (DV_ARRAY_OF_POINTER == *ptr)
{
tptr += 5;
}
len = (long) skip_string_length (&tptr);
if ((name_len == len) && !memcmp (name, tptr, len))
{
ptr += full_dv_string_length (ptr);
tptr = ptr;
len = (long) skip_string_length (&tptr);
ptr = (unsigned char *) dk_alloc_box (len + 1, DV_STRING);
memcpy (ptr, tptr, len);
ptr[len] = 0;
dk_free_box (buf);
return ((caddr_t) ptr);
}
ptr += full_dv_string_length (ptr);
ptr += full_dv_string_length (ptr);
}
dk_free_box (buf);
return NULL;
}
caddr_t *xp_copy_to_xte_head (xml_entity_t *xe)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
unsigned char *ptr, *tptr;
caddr_t buf;
long len, num_word, atts_fill, addons;
int inx, res_len;
int res_fill = 1;
caddr_t *res = NULL;
caddr_t qname;
xper_dbg_print ("xp_copy_to_xte_head\n");
switch (xpe->xper_type)
{
case XML_MKUP_ETAG:
sqlr_new_error ("37000", "XI028", "Corrupted persistent XML entity.");
case XML_MKUP_TEXT:
{
caddr_t box = get_tag_data (xpe, xpe->xper_pos);
long pos;
dk_session_t *ses = strses_allocate ();
pos = xpe->xper_pos;
for (;;)
{
session_buffered_write (ses, box, box_length (box));
pos += box_length (box) + ((DV_SHORT_STRING_SERIAL == box_tag (box)) ? 2 : 5);
dk_free_box (box);
box = get_tag_data (xpe, pos);
if ((DV_STRING != box_tag (box)) && (DV_SHORT_STRING_SERIAL != box_tag (box)))
break;
}
dk_free_box (box);
if (strses_length (ses) > 10000000)
sqlr_new_error ("42000", "XE005", "String value is longer than 10000000 bytes: source fragment of persistent XML is too large and can not be placed into XMLTree.");
res = (caddr_t *)strses_string (ses);
strses_free (ses);
return res;
}
case XML_MKUP_COMMENT:
if (box_length (xpe->xper_name) > 1)
return (caddr_t *)list (2, list (1, uname__comment), box_copy (xpe->xper_name));
return (caddr_t *)list (1, list (1, uname__comment));
case XML_MKUP_PI:
case XML_MKUP_REF:
res_fill = 3;
break;
}
buf = get_tag_data (xpe, xpe->xper_pos);
len = box_length (buf);
ptr = (unsigned char *) (buf + 1);
ptr += full_dv_string_length (ptr);
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
res_len = res_fill + atts_fill;
res = (caddr_t *)dk_alloc_box (res_len * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
if ((0 == xpe->xper_ns_pos) || (' ' == xpe->xper_name[0]))
qname = box_copy (xpe->xper_name);
else
qname = xp_build_expanded_name (xpe);
switch (xpe->xper_type)
{
case XML_MKUP_PI:
res[0] = uname__pi;
res[1] = uname__bang_name;
res[2] = qname;
break;
case XML_MKUP_REF:
res[0] = uname__ref;
res[1] = uname__bang_name;
res[2] = qname;
break;
default:
res[0] = qname;
break;
}
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (inx = 0; inx + 1 < atts_fill; inx += 2)
{
caddr_t ns = NULL;
tptr = ptr;
if (DV_ARRAY_OF_POINTER == *ptr)
{
tptr += 5;
ns = xper_get_namespace (xpe, LONG_REF_NA (tptr + full_dv_string_length (tptr) + 1));
if (uname___empty == ns)
ns = NULL;
}
len = (long) skip_string_length (&tptr);
if ((5 > len) || memcmp (tptr, "xmlns", 5))
{
if (NULL == ns)
res[res_fill++] = box_dv_uname_nchars ((char *)tptr, len);
else
{
unsigned char *localname = tptr+len;
int locallen;
char *qname;
while ((localname > tptr) && (':' != localname[-1])) localname--;
locallen = (int) ((tptr+len) - localname);
BOX_DV_UNAME_COLONCONCAT4 (qname, ns, (char *)localname, locallen);
res[res_fill++] = qname;
dk_free_box (ns);
}
ptr += full_dv_string_length (ptr);
len = (long) skip_string_length (&ptr);
res[res_fill++] = box_dv_short_nchars ((char *)ptr, len);
ptr += len;
}
else
{
ptr += full_dv_string_length (ptr);
ptr += full_dv_string_length (ptr);
}
}
dk_free_box (buf);
if (res_len == res_fill)
return res;
else
{
int newresboxlen = res_fill * sizeof(caddr_t);
caddr_t *newres = (caddr_t *)dk_alloc_box (newresboxlen, DV_ARRAY_OF_POINTER);
memcpy (newres, res, newresboxlen);
dk_free_box ((box_t) res);
return newres;
}
}
caddr_t *xp_copy_to_xte_subtree (xml_entity_t *xe)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
unsigned char *ptr, *tptr;
caddr_t buf;
long len, num_word, atts_fill, addons;
int inx, res_len;
int res_fill = 1;
caddr_t *res = NULL;
dk_set_t children = NULL;
caddr_t qname;
xper_dbg_print ("xp_copy_to_xte_subtree\n");
switch (xpe->xper_type)
{
case XML_MKUP_ETAG:
sqlr_new_error ("37000", "XI028", "corrupted persistent XML entity detected.");
case XML_MKUP_COMMENT:
if (box_length (xpe->xper_name) > 1)
return (caddr_t *)list (2, list (1, uname__comment), box_copy (xpe->xper_name));
return (caddr_t *)list (1, list (1, uname__comment));
case XML_MKUP_TEXT:
{
caddr_t box = get_tag_data (xpe, xpe->xper_pos);
long pos;
dk_session_t *ses = strses_allocate ();
pos = xpe->xper_pos;
for (;;)
{
session_buffered_write (ses, box, box_length (box));
pos += box_length (box) + ((DV_SHORT_STRING_SERIAL == box_tag (box)) ? 2 : 5);
dk_free_box (box);
box = get_tag_data (xpe, pos);
if ((DV_STRING != box_tag (box)) && (DV_SHORT_STRING_SERIAL != box_tag (box)))
break;
}
dk_free_box (box);
if (strses_length (ses) > 10000000)
sqlr_new_error ("42000", "XE005", "String value is longer than 10000000 bytes: source fragment of persistent XML is too large and can not be placed into XMLTree.");
res = (caddr_t *)strses_string (ses);
strses_free (ses);
return res;
}
case XML_MKUP_PI:
case XML_MKUP_REF:
res_fill = 3;
break;
}
buf = get_tag_data (xpe, xpe->xper_pos);
len = box_length (buf);
ptr = (unsigned char *) (buf + 1);
ptr += full_dv_string_length (ptr);
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
res_len = res_fill + atts_fill;
res = (caddr_t *)dk_alloc_box (res_len * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
if ((0 == xpe->xper_ns_pos) || (' ' == xpe->xper_name[0]))
qname = box_copy (xpe->xper_name);
else
qname = xp_build_expanded_name (xpe);
switch (xpe->xper_type)
{
case XML_MKUP_PI:
res[0] = uname__pi;
res[1] = uname__bang_name;
res[2] = qname;
break;
case XML_MKUP_REF:
res[0] = uname__ref;
res[1] = uname__bang_name;
res[2] = qname;
break;
default:
res[0] = qname;
break;
}
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (inx = 0; inx + 1 < atts_fill; inx += 2)
{
caddr_t ns = NULL;
tptr = ptr;
if (DV_ARRAY_OF_POINTER == *ptr)
{
tptr += 5;
ns = xper_get_namespace (xpe, LONG_REF_NA (tptr + full_dv_string_length (tptr) + 1));
if (uname___empty == ns)
ns = NULL;
}
len = (long) skip_string_length (&tptr);
if ((5 > len) || memcmp (tptr, "xmlns", 5))
{
if (NULL == ns)
res[res_fill++] = box_dv_uname_nchars ((char *)tptr, len);
else
{
unsigned char *localname = tptr+len;
int locallen;
char *qname;
while ((localname > tptr) && (':' != localname[-1])) localname--;
locallen = (int) ((tptr+len) - localname);
BOX_DV_UNAME_COLONCONCAT4 (qname, ns, (char *)localname, locallen);
res[res_fill++] = qname;
dk_free_box (ns);
}
ptr += full_dv_string_length (ptr);
len = (long) skip_string_length (&ptr);
res[res_fill++] = box_dv_short_nchars ((char *)ptr, len);
ptr += len;
}
else
{
ptr += full_dv_string_length (ptr);
ptr += full_dv_string_length (ptr);
}
}
dk_free_box (buf);
if (XML_MKUP_PI == xpe->xper_type)
{
if ((res_fill > 2) && (!strcmp (res[res_fill - 2], "data")))
{
dk_set_push (&children, res[res_fill - 1]);
dk_free_box (res[res_len - 2]);
res_fill -= 2;
}
}
if (XML_MKUP_STAG == xpe->xper_type)
{
long save = xpe->xper_pos;
for (;;)
{
fill_xper_entity (xpe, xpe->xper_next_item);
if (XML_MKUP_ETAG == xpe->xper_type)
break;
dk_set_push (&children, xp_copy_to_xte_subtree ((xml_entity_t *)xpe));
if (XML_MKUP_STAG == xpe->xper_type)
fill_xper_entity (xpe, xpe->xper_end);
}
fill_xper_entity (xpe, save);
}
if (res_len != res_fill)
{
int newresboxlen = res_fill * sizeof(caddr_t);
caddr_t *newres = (caddr_t *)dk_alloc_box (newresboxlen, DV_ARRAY_OF_POINTER);
memcpy (newres, res, newresboxlen);
dk_free_box ((box_t) res);
res = newres;
}
children = dk_set_nreverse (children);
dk_set_push (&children, (void *)res);
return (caddr_t *)list_to_array (children);
}
caddr_t ** xp_copy_to_xte_forest (xml_entity_t *xe)
{
GPF_T;
return NULL;
}
void xp_emulate_input (xml_entity_t *xe, struct vxml_parser_s *parser)
{
sqlr_error ("42000", "Unable to pass persistent XML data to the XML validator.");
}
caddr_t xp_find_expanded_name_by_qname (xml_entity_t *xe, const char *qname, int use_default)
{
#if 0
sqlr_error ("42000", "Unable to convert a qname to expanded name using persistent XML data as context; functions like expand-qname() are for XML Tree documents only");
#else
return NULL;
#endif
}
dk_set_t xp_namespace_scope (xml_entity_t *xe, int use_default)
{
#if 0
sqlr_error ("42000", "Unable to collect namespace data from persistent XML.");
#else
return NULL;
#endif
}
/*
int
xp_equal (xml_entity_t * xe1, xml_entity_t * xe2)
{
xper_entity_t *xpe1 = (xper_entity_t *) xe1;
xper_entity_t *xpe2 = (xper_entity_t *) xe2;
xper_dbg_print ("xp_equal\n");
if (xpe1->_ != xpe2->_ || xpe1->xe_doc.xd != xpe2->xe_doc.xd ||
xpe1->xper_pos != xpe2->xper_pos)
return 0;
return 1;
}
*/
int
xp_is_same_as (const xml_entity_t * this_xe, const xml_entity_t * that_xe)
{
if (XE_IS_PERSISTENT (that_xe))
{
const xper_entity_t *this_xpe = (const xper_entity_t *) (this_xe);
const xper_entity_t *that_xpe = (const xper_entity_t *) (that_xe);
if (this_xpe->xe_doc.xpd != that_xpe->xe_doc.xpd)
return 0;
if (this_xpe->xper_pos != that_xpe->xper_pos)
return 0;
if (this_xpe->xe_attr_name != that_xpe->xe_attr_name)
return 0;
return 1;
}
return 0;
}
static caddr_t xp_build_expanded_name (xper_entity_t *xpe)
{
char *name;
caddr_t ns;
size_t len;
caddr_t exp_name;
size_t nslen;
size_t local_len;
char *colon;
name = xpe->xper_name;
ns = xper_get_namespace (xpe, xpe->xper_ns_pos);
len = box_length (name)-1;
if (uname___empty == ns)
return box_copy (name);
nslen = box_length (ns);
colon = name + len;
while ((colon > name) && (colon[-1] != ':')) colon--;
local_len = (name + len) - colon;
BOX_DV_UNAME_COLONCONCAT4 (exp_name, ns, colon, local_len);
dk_free_box (ns);
return exp_name;
}
static caddr_t
xp_element_name (xml_entity_t * xe)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
xper_dbg_print ("xp_element_name\n");
switch (xpe->xper_type)
{
case XML_MKUP_TEXT:
return uname__txt;
case XML_MKUP_COMMENT:
return uname__comment;
case XML_MKUP_REF:
return uname__ref;
case XML_MKUP_PI:
return uname__pi;
default:
if ((0 == xpe->xper_ns_pos) || (' ' == xpe->xper_name[0]))
{
caddr_t res = box_copy (xpe->xper_name);
return res;
}
return xp_build_expanded_name (xpe);
}
}
static caddr_t
xp_ent_name (xml_entity_t * xe)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
xper_dbg_print ("xp_node_name\n");
if (xe->xe_attr_name)
return box_copy (xe->xe_attr_name);
switch (xpe->xper_type)
{
case XML_MKUP_TEXT:
return uname___empty;
case XML_MKUP_COMMENT:
return uname___empty;
case XML_MKUP_REF:
return box_copy (xpe->xper_name);
case XML_MKUP_PI:
return box_copy (xpe->xper_name);
default:
if ((0 == xpe->xper_ns_pos) || (' ' == xpe->xper_name[0]))
return box_copy (xpe->xper_name);
return xp_build_expanded_name (xpe);
}
}
void
write_escaped (dk_session_t * ses, unsigned char *ptr, int len, wcharset_t *charset)
{
unsigned char *end = ptr+len;
for (/* no init*/; ptr < end; ptr++)
{
switch (*ptr)
{
case '<':
SES_PRINT (ses, "<");
break;
case '>':
SES_PRINT (ses, ">");
break;
case '&':
SES_PRINT (ses, "&");
break;
case '\'':
SES_PRINT (ses, "'");
break;
case '"':
SES_PRINT (ses, """);
break;
case ' ':
case '\r':
case '\n':
case '\t':
session_buffered_write_char (*ptr, ses);
break;
default:
if (((charset != CHARSET_UTF8) && (0x7F <= *ptr)) || (0x20 > *ptr))
{
unichar uni = eh_decode_char__UTF8 ((__constcharptr *)&ptr, (char *)end);
unsigned char encoded_uni =
((NULL != charset) ?
(unsigned char)((ptrlong) gethash ((void *) (ptrlong) uni, charset->chrs_ht)) :
(unsigned char)((uni & ~0xFF) ? 0 : uni) );
ptr--; /* to compensate the 'ptr++' in for */
if (0x20 > encoded_uni)
{
char tmp[32];
snprintf (tmp, sizeof (tmp), "&#%d;", (int)(uni));
SES_PRINT (ses, tmp);
}
else
session_buffered_write_char ((char)(encoded_uni), ses);
}
else
session_buffered_write_char (*ptr, ses);
}
}
}
void
write_escaped_attvalue (dk_session_t * ses, unsigned char *ptr, int len, wcharset_t *charset)
{
unsigned char *end = ptr+len;
for (/* no init*/; ptr < end; ptr++)
{
switch (*ptr)
{
case '<':
SES_PRINT (ses, "<");
break;
case '>':
SES_PRINT (ses, ">");
break;
case '&':
SES_PRINT (ses, "&");
break;
/* Disabled due to Bug3166
case '\'':
SES_PRINT (ses, "'");
*/
break;
case '"':
SES_PRINT (ses, """);
break;
default:
if ((charset != CHARSET_UTF8) && ((0x7F <= *ptr) || (0x20 > *ptr)))
{
unichar uni = eh_decode_char__UTF8 ((__constcharptr *)&ptr, (char *)end);
unsigned char encoded_uni =
((NULL != charset) ?
(unsigned char)((ptrlong) gethash ((void *) (ptrlong) uni, charset->chrs_ht)) :
(unsigned char)((uni & ~0xFF) ? 0 : uni) );
ptr--; /* to compensate the 'ptr++' in for */
if (0x20 > encoded_uni)
{
char tmp[32];
snprintf (tmp, sizeof (tmp), "&#%d;", (int)(uni));
SES_PRINT (ses, tmp);
}
else
session_buffered_write_char ((char)(encoded_uni), ses);
}
else
session_buffered_write_char (*ptr, ses);
}
}
}
void
write_escaped_comment (dk_session_t * ses, unsigned char *ptr, int len, wcharset_t *charset)
{
unsigned char *begin = ptr;
unsigned char *end = ptr+len;
for (/* no init*/; ptr < end; ptr++)
{
if (('>' == *ptr) && (ptr > begin + 1) && ('-' == ptr[-1]) && ('-' == ptr[-2]))
SES_PRINT (ses, ">");
else
{
if ((charset != CHARSET_UTF8) && ((0x7F <= *ptr) || (0x20 > *ptr)))
{
unichar uni = eh_decode_char__UTF8 ((__constcharptr *)&ptr, (char *)end);
unsigned char encoded_uni =
((NULL != charset) ?
(unsigned char)((ptrlong) gethash ((void *) (ptrlong) uni, charset->chrs_ht)) :
(unsigned char)((uni & ~0xFF) ? 0 : uni) );
ptr--; /* to compensate the 'ptr++' in for */
if (0 == encoded_uni)
{
char tmp[32];
snprintf (tmp, sizeof (tmp), "&#%d;", (int)(uni));
SES_PRINT (ses, tmp);
}
else
session_buffered_write_char ((char)(encoded_uni), ses);
}
else
session_buffered_write_char (*ptr, ses);
}
}
}
void
write_escaped_pitext (dk_session_t * ses, unsigned char *ptr, int len, wcharset_t *charset)
{
#ifdef XPER_DEBUG
unsigned char *begin = ptr;
#endif
unsigned char *end = ptr+len;
for (/* no init*/; ptr < end; ptr++)
{
if ((charset != CHARSET_UTF8) && ((0x7F <= *ptr) || (0x20 > *ptr)))
{
unichar uni = eh_decode_char__UTF8 ((__constcharptr *)&ptr, (char *)end);
unsigned char encoded_uni =
((NULL != charset) ?
(unsigned char)((ptrlong) gethash ((void *) (ptrlong) uni, charset->chrs_ht)) :
(unsigned char)((uni & ~0xFF) ? 0 : uni) );
ptr--; /* to compensate the 'ptr++' in for */
if (0 == encoded_uni)
{
char tmp[32];
snprintf (tmp, sizeof (tmp), "&#%d;", (int)(uni));
SES_PRINT (ses, tmp);
}
else
session_buffered_write_char ((char)(encoded_uni), ses);
}
else
session_buffered_write_char (*ptr, ses);
}
}
void
DBG_NAME(xp_string_value) (DBG_PARAMS xml_entity_t * xe, caddr_t * ret, dtp_t dtp)
{
#ifdef XPER_DEBUG
wcharset_t *charset = QST_CHARSET (xe->xe_doc.xd->xd_qi);
#endif
xper_entity_t *xpe = (xper_entity_t *) xe;
caddr_t box = NULL;
long pos;
long epos;
unsigned char *ptr;
int type;
int namelen;
long num_word, atts_fill, addons, alen;
dk_session_t *ses = strses_allocate ();
switch (xpe->xper_type)
{
case XML_MKUP_COMMENT:
session_buffered_write (ses, xpe->xper_name, box_length (xpe->xper_name) - 1);
goto done;
case XML_MKUP_PI:
{
long len, num_word, atts_fill, addons;
box = get_tag_data (xpe, xpe->xper_pos);
len = box_length (box);
ptr = (unsigned char *) (box + 1);
ptr += full_dv_string_length (ptr);
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (; atts_fill > 1; atts_fill -= 2)
{
namelen = (int) skip_string_length (&ptr);
ptr += namelen;
alen = (long) skip_string_length (&ptr);
session_buffered_write (ses, (const char *) ptr, alen);
ptr += alen;
}
goto done;
}
}
box = get_tag_data (xpe, xpe->xper_pos);
xper_dbg_print ("xp_string_value\n");
if (DV_XML_MARKUP == box_tag (box))
{
epos = xpe->xper_end;
for (pos = xpe->xper_pos + box_length (box) + 5; pos < epos; /* no step */)
{
#if 0
long box_pos = pos;
#endif
dk_free_box (box);
box = get_tag_data (xpe, pos);
pos += box_length (box) + ((DV_SHORT_STRING_SERIAL == box_tag (box)) ? 2 : 5);
if ((DV_STRING == box_tag (box)) || (DV_SHORT_STRING_SERIAL == box_tag (box)))
session_buffered_write (ses, box, box_length (box));
else
{
ptr = (unsigned char *) box;
type = *ptr++;
namelen = (int) skip_string_length (&ptr);
switch (type)
{
case XML_MKUP_PI:
ptr += namelen + STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (; atts_fill > 1; atts_fill -= 2)
{
namelen = (int) skip_string_length (&ptr);
ptr += namelen;
alen = (long) skip_string_length (&ptr);
session_buffered_write (ses, (const char *) ptr, alen);
ptr += alen;
}
break;
#if 0
/* TBD: references should be extended and converted to string, but probably with disabled signaling of errors. */
case XML_MKUP_REF:
{
xper_entity_t *ref_xpe = (xper_entity_t *)xe->_->xe_copy (xe);
fill_xper_entity (ref_xpe, box_pos);
if (XI_RESULT == xp_down ((xml_entity_t *) ref_xpe, (XT *) XP_NODE))
{
caddr_t ref_val = NULL;
/* There's no transit allowed in the following line, thus it climbs up to the root of the referenced doc */
ref_xpe->_->xe_up ((xml_entity_t *) ref_xpe, (XT *) XP_NODE, 0);
ref_xpe->_->xe_string_value ((xml_entity_t *) ref_xpe, &ref_val, DV_STRING);
session_buffered_write (ses, ref_val, box_length (ref_val) - 1);
}
dk_free_box (ref_xpe);
break;
}
#endif
case XML_MKUP_COMMENT:
session_buffered_write (ses, (const char *) ptr, namelen);
break;
}
}
}
goto done;
}
if ((DV_STRING == box_tag (box)) || (DV_SHORT_STRING_SERIAL == box_tag (box)))
{
long box_pos = pos = xpe->xper_pos;
for (;;)
{
session_buffered_write (ses, box, box_length (box));
box_pos = pos;
pos += box_length (box) + ((DV_SHORT_STRING_SERIAL == box_tag (box)) ? 2 : 5);
dk_free_box (box);
box = get_tag_data (xpe, pos);
if ((DV_STRING == box_tag (box)) || (DV_SHORT_STRING_SERIAL == box_tag (box)))
continue;
#if 0
/* TBD: support for strings that starts in one subdocument and continues in a nested reference */
if (box[0] == XML_MKUP_REF)
{
xper_entity_t *ref_xpe = (xper_entity_t *)xe->_->xe_copy (xe);
fill_xper_entity (ref_xpe, box_pos);
if (XI_RESULT == xp_down ((xml_entity_t *) ref_xpe, (XT *) XP_NODE))
{
caddr_t ref_val = NULL;
ref_xpe->_.xe_string_value ((xml_entity_t *) ref_xpe, &ref_val, DV_STRING);
session_buffered_write (ses, ref_val, box_length (ref_val) - 1);
}
dk_free_box (ref_xpe);
continue;
}
#endif
break;
}
goto done;
}
done:
dk_free_box (box);
if (strses_length (ses) > 10000000)
sqlr_new_error ("42000", "XE005", "String value is longer than 10000000 bytes: source fragment of persistent XML is too large.");
box = DBG_NAME(strses_string) (DBG_ARGS ses);
strses_free (ses);
if (DV_NUMERIC == dtp)
{
caddr_t val = xp_box_number (box);
XP_SET (ret, val);
dk_free_box (box);
return;
}
XP_SET (ret, box);
}
int
xp_string_value_is_nonempty (xml_entity_t * xe)
{
#ifdef XPER_DEBUG
wcharset_t *charset = QST_CHARSET (xe->xe_doc.xd->xd_qi);
#endif
xper_entity_t *xpe = (xper_entity_t *) xe;
caddr_t box = NULL;
long pos;
long epos;
unsigned char *ptr;
int type;
int namelen;
long num_word, atts_fill, addons, alen;
switch (xpe->xper_type)
{
case XML_MKUP_COMMENT:
return (1 < box_length (xpe->xper_name));
case XML_MKUP_PI:
{
long len, num_word, atts_fill, addons;
box = get_tag_data (xpe, xpe->xper_pos);
len = box_length (box);
ptr = (unsigned char *) (box + 1);
ptr += full_dv_string_length (ptr);
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (; atts_fill > 1; atts_fill -= 2)
{
namelen = (int) skip_string_length (&ptr);
ptr += namelen;
alen = (long) skip_string_length (&ptr);
if (alen)
return 1;
ptr += alen;
}
return 0;
}
}
box = get_tag_data (xpe, xpe->xper_pos);
xper_dbg_print ("xp_string_value\n");
if (DV_XML_MARKUP == box_tag (box))
{
epos = xpe->xper_end;
for (pos = xpe->xper_pos + box_length (box) + 5; pos < epos; /* no step */)
{
#if 0
long box_pos = pos;
#endif
dk_free_box (box);
box = get_tag_data (xpe, pos);
pos += box_length (box) + ((DV_SHORT_STRING_SERIAL == box_tag (box)) ? 2 : 5);
if ((DV_STRING == box_tag (box)) || (DV_SHORT_STRING_SERIAL == box_tag (box)))
{
if (box_length (box))
return 1;
}
else
{
ptr = (unsigned char *) box;
type = *ptr++;
namelen = (int) skip_string_length (&ptr);
switch (type)
{
case XML_MKUP_PI:
ptr += namelen + STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (; atts_fill > 1; atts_fill -= 2)
{
namelen = (int) skip_string_length (&ptr);
ptr += namelen;
alen = (long) skip_string_length (&ptr);
if (alen)
return 1;
ptr += alen;
}
break;
#if 0
/* TBD: references should be extended and converted to string, but probably with disabled signaling of errors. */
case XML_MKUP_REF:
{
xper_entity_t *ref_xpe = (xper_entity_t *)xe->_->xe_copy (xe);
fill_xper_entity (ref_xpe, box_pos);
if (XI_RESULT == xp_down ((xml_entity_t *) ref_xpe, (XT *) XP_NODE))
{
caddr_t ref_val = NULL;
/* There's no transit allowed in the following line, thus it climbs up to the root of the referenced doc */
ref_xpe->_->xe_up ((xml_entity_t *) ref_xpe, (XT *) XP_NODE, 0);
ref_xpe->_->xe_string_value ((xml_entity_t *) ref_xpe, &ref_val, DV_STRING);
session_buffered_write (ses, ref_val, box_length (ref_val) - 1);
}
dk_free_box (ref_xpe);
break;
}
#endif
case XML_MKUP_COMMENT:
if (namelen)
return 1;
break;
}
}
}
return 0;
}
if ((DV_STRING == box_tag (box)) || (DV_SHORT_STRING_SERIAL == box_tag (box)))
{
long box_pos = pos = xpe->xper_pos;
for (;;)
{
if (box_length (box))
return 1;
box_pos = pos;
pos += box_length (box) + ((DV_SHORT_STRING_SERIAL == box_tag (box)) ? 2 : 5);
dk_free_box (box);
box = get_tag_data (xpe, pos);
if ((DV_STRING == box_tag (box)) || (DV_SHORT_STRING_SERIAL == box_tag (box)))
continue;
#if 0
/* TBD: support for strings that starts in one subdocument and continues in a nested reference */
if (box[0] == XML_MKUP_REF)
{
xper_entity_t *ref_xpe = (xper_entity_t *)xe->_->xe_copy (xe);
fill_xper_entity (ref_xpe, box_pos);
if (XI_RESULT == xp_down ((xml_entity_t *) ref_xpe, (XT *) XP_NODE))
{
caddr_t ref_val = NULL;
ref_xpe->_.xe_string_value ((xml_entity_t *) ref_xpe, &ref_val, DV_STRING);
session_buffered_write (ses, ref_val, box_length (ref_val) - 1);
}
dk_free_box (ref_xpe);
continue;
}
#endif
break;
}
return 0;
}
return 0;
}
struct ns_def_s
{
caddr_t nsd_prefix; /*!< prefix assigned to a namespace */
caddr_t nsd_strg; /*!< namespace string itself */
ptrlong nsd_pos; /*!< position of namespace string */
ptrlong nsd_depth; /*!< nesting level where the definition is found, negative for undefined */
};
typedef struct ns_def_s ns_def_t;
#define NSD_FILL(nsd,prefix,strg,pos,depth) \
nsd->nsd_prefix = (prefix); \
nsd->nsd_strg = (strg); \
nsd->nsd_pos = (pos); \
nsd->nsd_depth = (depth)
#define NSD_FREE(nsd) \
dk_free_box (nsd->nsd_prefix); \
dk_free_box (nsd->nsd_strg); \
dk_free_box ((caddr_t)(nsd));
dk_set_t
xp_create_ns_dict (xper_entity_t * xpe)
{
long pos = xpe->xper_pos;
caddr_t box = NULL;
unsigned char *ptr, *avalue, *colon, *name_end, *ns_pos_field;
long ns_pos;
int depth = 0;
int type;
int namelen, anmlen;
long num_word, atts_fill, addons, alen;
int dtp;
dk_set_t outer_items = NULL;
dk_set_t inner_items = NULL;
dk_set_t new_items = NULL;
ns_def_t *new_def;
caddr_t error = NULL;
before_element:
box = get_tag_data (xpe, pos);
ptr = (unsigned char *) box;
dtp = box_tag (box);
switch (dtp)
{
case DV_XML_MARKUP:
type = *ptr++;
namelen = (int) skip_string_length (&ptr);
switch (type)
{
case XML_MKUP_STAG:
/* Start tag: processing of name */
ns_pos_field = ptr + namelen + STR_NS_OFF;
ns_pos = LONG_REF_NA (ns_pos_field);
if ((0 < ns_pos) && (XPER_ROOT_POS != pos))
{
for (colon = ptr, name_end = ptr + namelen; /* no check */ ; colon++)
{
if (colon >= name_end)
{
colon = ptr;
break;
}
if (colon[0] == ':')
break;
}
colon[0] = '\0';
DO_SET (ns_def_t *, def, &outer_items)
{
if (!strcmp (def->nsd_prefix, (const char *) ptr))
goto after_tag_new_def; /* see below */
}
END_DO_SET ()
DO_SET (ns_def_t *, def, &inner_items)
{
if (!strcmp (def->nsd_prefix, (const char *) ptr))
goto after_tag_new_def; /* see below */
}
END_DO_SET ()
new_def = (ns_def_t *) dk_alloc_box (sizeof (ns_def_t), DV_ARRAY_OF_LONG);
NSD_FILL (new_def, box_string ((char *) ptr), NULL, ns_pos, -1);
dk_set_push (&new_items, new_def);
}
after_tag_new_def:
/* Start tag: Processing of attributes */
ptr += namelen + STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (; atts_fill > 1; atts_fill -= 2) /* loop by attributes */
{
int adtp = *ptr;
alen = (long) skip_string_length (&ptr);
avalue = ptr + alen;
switch (adtp)
{
case DV_ARRAY_OF_POINTER: /* name with namespace */
anmlen = (int) skip_string_length (&ptr);
for (colon = ptr, name_end = ptr + anmlen; colon < name_end; colon++)
{
if (colon[0] != ':')
continue;
colon[0] = '\0';
DO_SET (ns_def_t *, def, &new_items)
{
if (!strcmp (def->nsd_prefix, (const char *) ptr))
goto after_attr_new_def; /* see below */
}
END_DO_SET ()
DO_SET (ns_def_t *, def, &outer_items)
{
if (!strcmp (def->nsd_prefix, (const char *) ptr))
goto after_attr_new_def; /* see below */
}
END_DO_SET ()
DO_SET (ns_def_t *, def, &inner_items)
{
if (!strcmp (def->nsd_prefix, (const char *) ptr))
goto after_attr_new_def; /* see below */
}
END_DO_SET ()
ns_pos_field = ptr + anmlen + 1;
ns_pos = LONG_REF_NA (ns_pos_field);
new_def = (ns_def_t *) dk_alloc_box (sizeof (ns_def_t), DV_ARRAY_OF_LONG);
NSD_FILL (new_def, box_string ((char *) ptr), NULL, ns_pos, -1);
dk_set_push (&new_items, new_def);
after_attr_new_def:
break;
}
break;
case DV_STRING:
case DV_SHORT_STRING_SERIAL:
if (strncmp ((const char *) ptr, "xmlns", 5 /* = strlen("xmlns") */ ))
break;
if (':' == ptr[5]) /* declaration of explicit prefix */
{
new_def = (ns_def_t *) dk_alloc_box (sizeof (ns_def_t), DV_ARRAY_OF_LONG);
NSD_FILL (new_def,
box_dv_short_nchars ((const char *) ptr + 6, alen - 6), /* rest of attribute name is a prefix */
NULL,
pos + (avalue - (unsigned char *) (box)), /* position of attribute value in BLOB is a position of namespace */
depth);
dk_set_push (&inner_items, new_def);
}
else if (5 == alen) /* declaration of implicit prefix */
{
new_def = (ns_def_t *) dk_alloc_box (sizeof (ns_def_t), DV_ARRAY_OF_LONG);
NSD_FILL (new_def,
box_string (""), /* no prefix */
NULL,
pos + (avalue - (unsigned char *) (box)), /* position of attribute value in BLOB is a position of namespace */
depth);
dk_set_push (&inner_items, new_def);
}
break;
default:
error = srv_make_new_error ("42000", "XM006",
"Error while serializing XML_PERSISTENT: invalid box tag");
goto emit_error;
}
ptr = avalue; /* skip attribute name */
alen = (long) skip_string_length (&ptr);
ptr += alen; /* skip attribute value */
} /* end of loop by attributes */
/* Start tag: sorting of new items: removal of inner items and storing of outer */
for (;;)
{
before_check_new_item:
if (NULL == new_items)
break;
new_def = (ns_def_t *) dk_set_pop (&new_items);
DO_SET (ns_def_t *, def, &outer_items)
{
if (!strcmp (def->nsd_prefix, new_def->nsd_prefix))
{
NSD_FREE (new_def);
goto before_check_new_item; /* see above */
}
}
END_DO_SET ()
DO_SET (ns_def_t *, def, &inner_items)
{
if (!strcmp (def->nsd_prefix, new_def->nsd_prefix))
{
NSD_FREE (new_def);
goto before_check_new_item; /* see above */
}
}
END_DO_SET ()
dk_set_push (&outer_items, new_def);
}
depth++;
break;
case XML_MKUP_ETAG:
depth--;
while (NULL != inner_items)
{
ns_def_t *itm = ((ns_def_t *) (inner_items->data));
if (itm->nsd_depth < depth)
break;
NSD_FREE (itm);
dk_set_pop (&inner_items);
}
break;
case XML_MKUP_PI:
case XML_MKUP_REF:
case XML_MKUP_COMMENT:
break;
default:
error = srv_make_new_error ("42000", "XM007",
"Error while serializing XML_PERSISTENT: invalid markup type");
goto emit_error;
}
break;
case DV_SHORT_STRING_SERIAL:
case DV_STRING:
break;
default:
error = srv_make_new_error ("42000", "XM008",
"Error while serializing XML_PERSISTENT: invalid box tag");
goto emit_error;
}
pos += ((DV_SHORT_STRING_SERIAL == dtp) ? 2 : 5);
pos += box_length (box);
dk_free_box (box);
if (pos <= xpe->xper_end)
goto before_element; /* see above */
return outer_items;
emit_error:
if (NULL != box)
dk_free_box (box);
DO_SET (ns_def_t *, def, &new_items)
{
NSD_FREE (def);
}
END_DO_SET ()
dk_set_free (new_items);
DO_SET (ns_def_t *, def, &inner_items)
{
NSD_FREE (def);
}
END_DO_SET ()
dk_set_free (inner_items);
DO_SET (ns_def_t *, def, &outer_items)
{
NSD_FREE (def);
}
END_DO_SET ()
dk_set_free (outer_items);
sqlr_resignal (error);
return NULL; /* never reached */
}
long
xp_serialize_element (xper_entity_t * xpe, long pos, dk_session_t * ses, dk_set_t * outer_ns_dict, wcharset_t *charset)
{
caddr_t box = get_tag_data (xpe, pos);
unsigned char *ptr = (unsigned char *) box;
int type;
int namelen;
long num_word, atts_fill, addons, alen, end_pos;
int dtp = box_tag (box);
pos += ((DV_SHORT_STRING_SERIAL == dtp) ? 2 : 5);
pos += box_length (box);
switch (dtp)
{
case DV_XML_MARKUP:
type = *ptr++;
namelen = (int) skip_string_length (&ptr);
switch (type)
{
case XML_MKUP_STAG:
if (' ' == ptr[0])
break;
session_buffered_write_char ('<', ses);
write_escaped (ses, ptr, namelen, charset);
/* Now it's time to imprint all items from additional dictionary */
if (outer_ns_dict[0])
{
DO_SET (ns_def_t *, def, outer_ns_dict)
{
if (NULL != def->nsd_strg)
{
SES_PRINT (ses, " xmlns");
if ('\0' != def->nsd_prefix[0])
{
session_buffered_write_char (':', ses);
write_escaped (ses, (unsigned char *) def->nsd_prefix,
(int) strlen (def->nsd_prefix), charset);
}
SES_PRINT (ses, "=\"");
write_escaped_attvalue (ses, (unsigned char *) def->nsd_strg, box_length (def->nsd_strg) - 1, charset);
session_buffered_write_char ('"', ses);
}
NSD_FREE (def);
}
END_DO_SET ()
dk_set_free (outer_ns_dict[0]);
outer_ns_dict[0] = NULL;
}
ptr += namelen + STR_END_TAG_OFF;
end_pos = LONG_REF_NA (ptr);
ptr += (STR_ATTR_NO_OFF - STR_END_TAG_OFF);
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (; atts_fill > 1; atts_fill -= 2)
{
int adtp = *ptr;
session_buffered_write_char (' ', ses);
alen = (long) skip_string_length (&ptr);
if (DV_ARRAY_OF_POINTER == adtp)
{
char *rem = (char *) ptr;
int anmlen = (int) skip_string_length (&ptr);
write_escaped (ses, ptr, anmlen, charset);
ptr = (unsigned char *) rem;
}
else
write_escaped (ses, ptr, alen, charset);
ptr += alen;
SES_PRINT (ses, "=\"");
alen = (long) skip_string_length (&ptr);
write_escaped_attvalue (ses, ptr, alen, charset);
session_buffered_write_char ('"', ses);
ptr += alen;
}
if (pos == end_pos)
{
SES_PRINT (ses, " /");
dk_free_box (box);
box = get_tag_data (xpe, pos);
dtp = box_tag (box);
pos += ((DV_SHORT_STRING_SERIAL == dtp) ? 2 : 5);
pos += box_length (box);
}
session_buffered_write_char ('>', ses);
break;
case XML_MKUP_ETAG:
if (' ' == ptr[0])
break;
SES_PRINT (ses, "</");
write_escaped (ses, ptr, namelen, charset);
session_buffered_write_char ('>', ses);
break;
case XML_MKUP_PI:
SES_PRINT (ses, "<?");
write_escaped (ses, ptr, namelen, charset);
ptr += namelen + STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
for (; atts_fill > 1; atts_fill -= 2)
{
namelen = (int) skip_string_length (&ptr);
ptr += namelen;
alen = (long) skip_string_length (&ptr);
session_buffered_write_char (' ', ses);
write_escaped_pitext (ses, ptr, alen, charset);
ptr += alen;
}
SES_PRINT (ses, "?>");
break;
case XML_MKUP_REF:
session_buffered_write_char ('&', ses);
write_escaped (ses, ptr, namelen, charset);
session_buffered_write_char (';', ses);
break;
case XML_MKUP_COMMENT:
SES_PRINT (ses, "<!--");
write_escaped_comment (ses, ptr, namelen, charset);
SES_PRINT (ses, "-->");
break;
default:
dk_free_box (box);
sqlr_new_error ("XE000", "XP9B2", "Error while serializing XML_PERSISTENT: invalid markup type %d", xpe->xper_type);
}
break;
case DV_STRING: case DV_SHORT_STRING_SERIAL:
write_escaped (ses, (unsigned char *) box, box_length (box), charset);
break;
default:
dk_free_box (box);
sqlr_new_error ("XE000", "XP9B3", "Error while serializing XML_PERSISTENT: invalid box tag %d", dtp);
break;
}
dk_free_box (box);
return pos;
}
void
xp_serialize (xml_entity_t * xe, dk_session_t * ses)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
wcharset_t * charset = NULL;
dk_set_t dict = NULL;
long pos = xpe->xper_pos;
xper_dbg_print ("xp_serialize\n");
charset = wcharset_by_name_or_dflt (xe->xe_doc.xtd->xout_encoding, xe->xe_doc.xd->xd_qi);
if (XML_MKUP_STAG != xpe->xper_type)
{
pos = xp_serialize_element (xpe, pos, ses, &dict, charset);
return;
}
dict = xp_create_ns_dict (xpe); /*dict = NULL; */
DO_SET (ns_def_t *, def, &dict)
{
def->nsd_strg = xper_get_namespace (xpe, (long) def->nsd_pos);
}
END_DO_SET ()
while (pos <= xpe->xper_end)
{
pos = xp_serialize_element (xpe, pos, ses, &dict, charset);
}
DO_SET (ns_def_t *, def, &dict)
{
NSD_FREE (def);
}
END_DO_SET ()
dk_set_free (dict);
}
struct xper_vtbf_env_s
{
long xve_closing_pos; /*!< Position of closing tag where this environment will end */
caddr_t xve_vtb_name; /*!< Tag name to be indexed, in form '<'[ns_uri]':'localname */
lang_handler_t *xve_outer_lh; /*!< Language outside the environment */
struct xper_vtbf_env_s *xve_outer; /*!< Pointer to outer (ascendant) environment, or NULL */
};
typedef struct xper_vtbf_env_s xper_vtbf_env_t;
/* IvAn/TextXperIndex/000815 Persistent XML support functions added */
const char *
xper_elements_vtb_feed (xper_entity_t * xpe, vt_batch_t * vtb, lh_word_callback_t * cbk, lang_handler_t * root_lh, caddr_t * textbufptr)
{
caddr_t box;
unsigned char *ptr, *start_tag_longs, *start_attrs, *start_name;
char type;
int namelen, vallen, wordlen;
long num_word, atts_fill, atts_ctr, addons;
int dtp;
const char *errmsg = NULL;
long curr_el_pos = XPER_ROOT_POS; /* position of current element in document BLOB */
long ns_pos;
caddr_t ns_uri;
xe_word_ranges_t predicted;
wpos_t old_word_pos;
char word_hider = 0;
const unsigned char *s_frag_begin;
unsigned char *box_end;
unsigned char *newbuf;
int space_found, text_buf_use, text_buf_size, newbuf_len, frag_len;
long el_length;
unichar uchr;
lang_handler_t *active_lang = root_lh;
lang_handler_t *new_lang;
caddr_t vtb_name;
xper_vtbf_env_t *env_stack = NULL;
if (curr_el_pos > xpe->xper_end)
return NULL;
if ((0 != vtb->vtb_word_pos) || (FIRST_ATTR_WORD_POS != vtb->vtb_attr_word_pos))
return "vt_batch_feed cannot index XPER data as part of compound text";
text_buf_use = 0;
text_buf_size = box_length (textbufptr[0]);
goto first_element;
next_element:
if (curr_el_pos > xpe->xper_end)
goto done;
dk_free_box (box);
first_element:
box = get_tag_data (xpe, curr_el_pos);
dtp = box_tag (box);
ptr = (unsigned char *) box;
curr_el_pos += ((DV_SHORT_STRING_SERIAL == dtp) ? 2 : 5);
el_length = box_length (box);
curr_el_pos += el_length;
switch (dtp)
{
case DV_STRING: case DV_SHORT_STRING_SERIAL:
s_frag_begin = ptr;
old_word_pos = vtb->vtb_word_pos;
box_end = (unsigned char *) box + el_length;
while ((ptr < box_end) && IS_UTF8_CHAR_CONT (ptr[0]))
ptr++;
next_frag:
for (;;)
{
uchr = eh_decode_char__UTF8 ((__constcharptr *) (&ptr), (const char *) box_end);
if (uchr < 0)
{
ptr = (unsigned char *) box_end;
space_found = 0;
break;
}
if (unicode3_isspace (uchr))
{
space_found = 1;
break;
}
}
frag_len = (int) (ptr - s_frag_begin);
if (text_buf_use + frag_len > text_buf_size)
{
newbuf_len = text_buf_size;
do
{
newbuf_len *= 2;
newbuf_len |= 0x3FF;
}
while (text_buf_use + frag_len > newbuf_len);
newbuf = (unsigned char *) dk_alloc_box (newbuf_len, DV_STRING);
memcpy (newbuf, textbufptr[0], text_buf_use);
memcpy (newbuf + text_buf_use, s_frag_begin, frag_len);
dk_free_box (textbufptr[0]);
textbufptr[0] = (caddr_t) newbuf;
text_buf_size = newbuf_len;
}
else
{
memcpy (textbufptr[0] + text_buf_use, s_frag_begin, frag_len);
}
text_buf_use += frag_len;
if (space_found)
{
int res;
res = lh_iterate_patched_words (
&eh__UTF8, active_lang,
textbufptr[0], text_buf_use,
active_lang->lh_is_vtb_word, active_lang->lh_normalize_word,
cbk, vtb);
if (res < 0)
{
errmsg = "corrupted text entity (UTF-8 encoding error)";
goto done;
}
text_buf_use = 0;
}
s_frag_begin = ptr;
if (ptr < box_end)
goto next_frag;
if (vtb->vtb_word_pos != old_word_pos) /* some words are indexed */
word_hider = XML_MKUP_TEXT;
goto next_element;
break;
case DV_XML_MARKUP:
break;
default:
errmsg = "corrupted XPER data (invalid box tag)";
goto done;
}
/* If we're not at string element, we should flush \c tmp buffer */
if (text_buf_use)
{
int res;
old_word_pos = vtb->vtb_word_pos;
res = lh_iterate_patched_words (
&eh__UTF8, active_lang,
textbufptr[0], text_buf_use,
active_lang->lh_is_vtb_word, active_lang->lh_normalize_word,
cbk, vtb);
if (res < 0)
{
errmsg = "corrupted text entity (UTF-8 encoding error)";
goto done;
}
text_buf_use = 0;
if (vtb->vtb_word_pos != old_word_pos) /* some words are indexed */
word_hider = XML_MKUP_TEXT;
}
/* Now we're at markup byte */
type = *ptr++;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
start_name = ptr;
switch (type)
{
case XML_MKUP_STAG:
if (namelen > (XML_MAX_EXP_NAME - 2))
{
errmsg = "tag name is too long";
goto done;
}
if ((XML_MKUP_ETAG != word_hider) && (vtb->vtb_word_pos > 0))
vtb->vtb_word_pos--;
word_hider = XML_MKUP_STAG;
ptr += namelen;
start_tag_longs = ptr;
ptr += STR_NS_OFF;
ns_pos = LONG_REF_NA (ptr);
if ((0 != ns_pos) && (' ' != start_name[0]))
{
unsigned char *tail, *local;
uint32 ns_uri_len, local_len;
ns_uri = xper_get_namespace (xpe, ns_pos);
ns_uri_len = box_length (ns_uri) - 1;
if (0 == ns_uri_len)
goto no_box_in_env_stack; /* see below */
local = start_name + namelen;
while ((local > start_name) && (':' != local[-1]))
local--;
local_len = (uint32) (start_name + namelen - local);
wordlen = ns_uri_len + local_len + 2;
vtb_name = dk_alloc_box (wordlen + 1, DV_STRING);
tail = (unsigned char *) vtb_name;
(tail++)[0] = '<';
memcpy (tail, ns_uri, ns_uri_len);
tail += ns_uri_len;
(tail++)[0] = ':';
memcpy (tail, local, local_len);
tail += local_len;
tail[0] = '\0';
dk_free_box (ns_uri);
cbk ((const utf8char *) vtb_name, wordlen, vtb);
goto cbk_for_start_tag_done; /* see below */
}
no_box_in_env_stack:
vtb_name = NULL; /* No need to create separate box and store it in env's stack */
textbufptr[0][0] = '<';
memcpy (textbufptr[0] + 1, start_name, namelen);
wordlen = 1 + namelen;
textbufptr[0][wordlen] = '\0';
cbk ((const utf8char *) textbufptr[0], wordlen, vtb);
cbk_for_start_tag_done:
ptr = start_tag_longs + STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
if (3 * 4 <= addons)
predicted.xewr_attr_beg = LONG_REF_NA (ptr);
else
predicted.xewr_attr_beg = vtb->vtb_attr_word_pos;
ptr += addons;
start_attrs = ptr;
predicted.xewr_main_beg = LONG_REF_NA (start_tag_longs + STR_START_WORD_OFF);
/* predicted.xewr_main_beg has "+1" because we've just indexed tag name */
if (((predicted.xewr_main_beg + 1) != vtb->vtb_word_pos) || (predicted.xewr_attr_beg != vtb->vtb_attr_word_pos))
{
errmsg = "word numbering has corrupted, please check language settings for text index";
goto done;
}
new_lang = active_lang;
for (atts_ctr = atts_fill; atts_ctr > 1; atts_ctr -= 2)
{
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
if ((8 /* == strlen("xml:lang") */ == namelen) && (0 == memcmp (ptr, "xml:lang", 8)))
{
char buf[XML_MAX_EXP_NAME];
ptr += 8;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
if (namelen > (XML_MAX_EXP_NAME - 2))
{
errmsg = "name of language in xml:lang attribute value is too long";
goto done;
}
memcpy (buf, ptr, namelen);
buf[namelen] = '\0';
new_lang = lh_get_handler (buf);
break;
}
ptr += namelen;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
ptr += namelen;
}
if ((NULL != vtb_name) || (new_lang != active_lang))
{
xper_vtbf_env_t *res = (xper_vtbf_env_t *) dk_alloc (sizeof (xper_vtbf_env_t));
res->xve_closing_pos = LONG_REF_NA (start_tag_longs + STR_END_TAG_OFF);
res->xve_vtb_name = vtb_name;
res->xve_outer_lh = active_lang;
res->xve_outer = env_stack;
env_stack = res;
}
active_lang = new_lang;
if (0 != atts_fill)
{
wpos_t saved_pos = vtb->vtb_word_pos;
wpos_t attr_poss[2];
ptr = start_attrs;
attr_poss[0] = vtb->vtb_attr_word_pos;
vtb->vtb_word_pos = attr_poss[0];
for (atts_ctr = atts_fill; atts_ctr > 1; atts_ctr -= 2)
{
int index_this_attr = 1;
utf8char buf[XML_MAX_EXP_NAME];
unsigned char adtp = *ptr;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
start_name = ptr;
ptr += namelen;
if (DV_ARRAY_OF_POINTER == adtp)
{
namelen = (int) skip_string_length ((unsigned char **) (&start_name));
ns_pos = LONG_REF_NA (start_name + namelen + 1);
ns_uri = xper_get_namespace (xpe, ns_pos);
if (uname___empty == ns_uri)
ns_pos = 0;
}
else
ns_pos = 0;
if (0 != ns_pos)
{
unsigned char *tail, *local;
uint32 ns_uri_len, local_len;
ns_uri_len = box_length (ns_uri) - 1;
local = start_name + namelen;
while ((local > start_name) && (':' != local[-1]))
local--;
local_len = (uint32) (start_name + namelen - local);
wordlen = ns_uri_len + local_len + 2;
if (wordlen > (XML_MAX_EXP_NAME - 2))
{
errmsg = "URI-qualified name of attribute is too long";
dk_free_box (ns_uri);
goto done;
}
tail = buf + 1;
memcpy (tail, ns_uri, ns_uri_len);
tail += ns_uri_len;
(tail++)[0] = ':';
memcpy (tail, local, local_len);
tail += local_len;
tail[0] = '\0';
dk_free_box (ns_uri);
}
else
{
if (namelen > (XML_MAX_EXP_NAME - 2))
{
errmsg = "name of attribute is too long";
goto done;
}
if ((namelen < 5) || memcmp (start_name, "xmlns", 5))
{
wordlen = namelen + 1;
memcpy (buf + 1, start_name, namelen);
buf[wordlen] = '\0';
}
else
index_this_attr = 0;
}
if (index_this_attr)
{
buf[0] = '{';
cbk (buf, wordlen, vtb);
vallen = (int) skip_string_length ((unsigned char **) (&ptr));
lh_iterate_patched_words (
&eh__UTF8, active_lang,
(char *) ptr, vallen,
active_lang->lh_is_vtb_word, active_lang->lh_normalize_word,
cbk, vtb );
ptr += vallen;
buf[0] = '}';
cbk (buf, wordlen, vtb);
}
else
{
wpos_t word_count;
vallen = (int) skip_string_length ((unsigned char **) (&ptr));
#ifndef SKIP_XMLNS_WORD_POS
if (&lh__xany == active_lang) /* Optimization for most common case */
word_count = elh__xany__UTF8.elh_count_words ((char *) ptr, vallen, lh__xany.lh_is_vtb_word);
else
word_count = lh_count_words (&eh__UTF8, active_lang, (char *) ptr, vallen, active_lang->lh_is_vtb_word);
vtb->vtb_word_pos += 2 + word_count;
#endif
ptr += vallen;
}
}
attr_poss[1] = vtb->vtb_attr_word_pos = vtb->vtb_word_pos;
vtb->vtb_word_pos = saved_pos;
}
break;
case XML_MKUP_ETAG:
if (namelen > (XML_MAX_EXP_NAME - 2))
{
errmsg = "closing tag name is too long";
goto done;
}
vtb_name = NULL;
while ((NULL != env_stack) && (curr_el_pos > env_stack->xve_closing_pos))
{
xper_vtbf_env_t *outer = env_stack->xve_outer;
vtb_name = env_stack->xve_vtb_name;
active_lang = env_stack->xve_outer_lh;
dk_free (env_stack, -1);
env_stack = outer;
}
if (NULL != vtb_name)
{
vtb_name[0] = '/';
cbk ((const utf8char *) vtb_name, box_length (vtb_name) - 1, vtb);
dk_free_box (vtb_name);
}
else
{
textbufptr[0][0] = '/';
memcpy (textbufptr[0] + 1, start_name, namelen);
wordlen = 1 + namelen;
textbufptr[0][wordlen] = '\0';
cbk ((const utf8char *) textbufptr[0], wordlen, vtb);
}
vtb->vtb_word_pos--;
word_hider = XML_MKUP_ETAG;
break;
case XML_MKUP_COMMENT:
case XML_MKUP_REF:
case XML_MKUP_PI:
break;
default:
errmsg = "invalid markup type";
goto done;
}
goto next_element;
done:
dk_free_box (box);
return errmsg;
}
void
xper_blob_vtb_feed (query_instance_t * qi, blob_handle_t * xper_blob, vt_batch_t * vtb, lh_word_callback_t * cbk, lang_handler_t * lh, caddr_t * textbufptr)
{
xper_entity_t *xpe; /* temporary entity to traverse document */
const char *errmsg;
xpe = xper_entity (qi, (caddr_t) xper_blob, NULL, 0, NULL /* no path */ , NULL /* no encoding */ , lh, NULL /* DTD config */, 0);
errmsg = xper_elements_vtb_feed (xpe, vtb, cbk, lh, textbufptr);
dk_free_tree ((box_t) xpe);
if (NULL != errmsg)
sqlr_new_error ("XE000", "XP9B3", "Error while indexing persistent XML blob: %.1000s", errmsg);
}
void
xper_str_vtb_feed (query_instance_t * qi, caddr_t xper_str, vt_batch_t * vtb, lh_word_callback_t * cbk, lang_handler_t * lh, caddr_t * textbufptr)
{
xper_entity_t *xpe; /* temporary entity to traverse document */
const char *errmsg;
xpe = xper_entity (qi, xper_str, NULL, 0, NULL /* no path */ , NULL /* no encoding */ , lh, NULL /* DTD config */, 0);
errmsg = xper_elements_vtb_feed (xpe, vtb, cbk, lh, textbufptr);
dk_free_tree ((box_t) xpe);
if (NULL != errmsg)
sqlr_new_error ("XE000", "XP9B3", "Error while indexing persistent XML string: %.1000s", errmsg);
}
void
xp_destroy (xml_entity_t * xe)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
xper_dbg_print ("xp_destroy\n");
#ifdef MALLOC_DEBUG
if (xpe->xe_doc.xd->xd_top_doc)
{
dk_set_t refs = xpe->xe_doc.xd->xd_top_doc->xd_referenced_entities;
DO_SET (xml_entity_t *, ref_ent, &refs)
{
dk_alloc_box_assert (ref_ent);
if (ref_ent == xe)
GPF_T1 ("attempt of destroying of an referenced entity");
}
END_DO_SET ();
}
#endif
dk_free_box ((box_t) xpe->xe_referer);
if (0 >= --xpe->xe_doc.xd->xd_ref_count)
{
xper_doc_t *xpd = xpe->xe_doc.xpd;
#if 0 /* This is no longer valid because document cache can release the last entity on the document
and the document will stay locked in that time */
#ifdef DEBUG
if (0 != xpd->xd_dom_lock_count)
GPF_T1("attempt of destroying of a DOM-locked entity");
#endif
#endif
if (XPD_PERSISTENT != xpe->xe_doc.xpd->xpd_state)
{
it_cursor_t aitc;
it_cursor_t *itc = &aitc;
ITC_INIT (itc, xpd->xd_qi->qi_space, xpd->xd_qi->qi_trx);
blob_chain_delete (itc, (blob_layout_t *) blob_layout_from_handle_ctor (xpd->xpd_bh));
}
bh_free (xpd->xpd_bh);
dk_free_box (xpd->xd_uri);
DO_SET (xml_entity_t *, refd, &xpd->xd_referenced_entities)
{
XD_DOM_RELEASE(refd->xe_doc.xd);
#ifdef MALLOC_DEBUG
refd->xe_doc.xd->xd_top_doc = NULL;
#endif
dk_free_box ((caddr_t) refd);
}
END_DO_SET ();
dk_set_free (xpd->xd_referenced_entities);
/*
DO_SET (xml_entity_t *, refd, &xpd->xd_referenced_documents)
{
XD_DOM_RELEASE(refd->xe_doc.xd);
dk_free_box ((caddr_t) refd);
}
END_DO_SET ();
dk_set_free (xpd->xd_referenced_documents);
*/
XPER_FREE_XPD (xpd);
}
if (NULL != xpe->xper_name)
dk_free_box (xpe->xper_name);
/* IvAn/XperTrav/000825 Textual data added */
if (NULL != xpe->xper_text)
dk_free_box (xpe->xper_text);
if (NULL != xpe->xper_cut_ent)
dk_free_box ((box_t) xpe->xper_cut_ent);
if (NULL != xpe->xe_attr_name)
dk_free_tree (xpe->xe_attr_name);
#ifdef XPER_DEBUG
xper_entity_free_ctr++;
#endif
}
void
xp_word_range (xml_entity_t * xe, wpos_t * start, wpos_t * end)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
start[0] = xpe->xper_start_word;
end[0] = xpe->xper_end_word;
}
/* IvAn/TextXperIndex/000815 Persistent XML support functions added */
static const char *
xper_attr_word_count (xper_entity_t * xpe)
{
caddr_t box;
unsigned char *ptr, *start_tag_longs, *start_attrs, *start_name;
char type;
int namelen, vallen;
long num_word, atts_fill, atts_ctr, addons;
int dtp;
const char *errmsg = NULL;
long curr_el_pos = XPER_ROOT_POS; /* position of current element in document BLOB */
ptrlong key_pos; /* position of element whose attributes are now under processing */
wpos_t attr_beg = FIRST_ATTR_WORD_POS;
wpos_t attr_word_count;
wpos_t *pos_stack_top;
long el_length;
xper_doc_t *xpd = xpe->xe_doc.xpd;
lang_handler_t *active_lang = xpd->xd_default_lh;
dk_set_t lang_handler_stack = NULL;
dk_set_t lang_scope_stack = NULL;
dk_set_t pos_stack = NULL;
if (curr_el_pos > xpe->xper_end)
return NULL;
goto first_element;
next_element:
if (curr_el_pos > xpe->xper_end)
goto done;
dk_free_box (box);
first_element:
key_pos = curr_el_pos;
box = get_tag_data (xpe, (long) key_pos);
dtp = box_tag (box);
ptr = (unsigned char *) box;
curr_el_pos += ((DV_SHORT_STRING_SERIAL == dtp) ? 2 : 5);
el_length = box_length (box);
curr_el_pos += el_length;
switch (box_tag (box))
{
case DV_STRING: case DV_SHORT_STRING_SERIAL:
goto next_element;
case DV_XML_MARKUP:
break;
default:
errmsg = "invalid box tag";
goto done;
}
/* Now we're at markup byte */
type = *ptr++;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
switch (type)
{
case XML_MKUP_STAG:
ptr += namelen;
start_tag_longs = ptr;
ptr += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
ptr += 4;
atts_fill = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
ptr += addons;
start_attrs = ptr;
for (atts_ctr = atts_fill; atts_ctr > 1; atts_ctr -= 2)
{
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
if ((8 /* == strlen("xml:lang") */ == namelen) && (0 == memcmp (ptr, "xml:lang", 8)))
{
char buf[XML_MAX_EXP_NAME];
lang_handler_t *new_lang;
ptr += 8;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
if (namelen > (XML_MAX_EXP_NAME - 2))
{
errmsg = "name of language in xml:lang attribute value is too long";
goto done;
}
memcpy (buf, ptr, namelen);
buf[namelen] = '\0';
new_lang = lh_get_handler (buf);
if (new_lang != active_lang)
{
long end_of_element = LONG_REF_NA (start_tag_longs + STR_END_TAG_OFF);
dk_set_push (&lang_scope_stack, (void *) (ptrlong) (end_of_element));
dk_set_push (&lang_handler_stack, (void *) (active_lang));
active_lang = new_lang;
}
ptr += namelen;
continue;
}
ptr += namelen;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
ptr += namelen;
}
ptr = start_attrs;
attr_word_count = 0;
for (atts_ctr = atts_fill; atts_ctr > 1; atts_ctr -= 2)
{
attr_word_count += 2;
namelen = (int) skip_string_length ((unsigned char **) (&ptr));
start_name = ptr;
ptr += namelen;
vallen = (int) skip_string_length ((unsigned char **) (&ptr));
attr_word_count += lh_count_words (&eh__UTF8, active_lang, (char *) ptr, vallen, active_lang->lh_is_vtb_word);
ptr += vallen;
}
pos_stack_top = (wpos_t *) dk_alloc (3 * sizeof (wpos_t));
pos_stack_top[2] = key_pos;
pos_stack_top[0] = attr_beg;
attr_beg += attr_word_count;
pos_stack_top[1] = attr_beg;
dk_set_push (&pos_stack, pos_stack_top);
break;
case XML_MKUP_ETAG:
while ((NULL != lang_scope_stack) && (curr_el_pos > (ptrlong) (lang_scope_stack->data)))
{
dk_set_pop (&lang_scope_stack);
active_lang = (lang_handler_t *) (dk_set_pop (&lang_handler_stack));
}
pos_stack_top = (wpos_t *) dk_set_pop (&pos_stack);
key_pos = pos_stack_top[2];
pos_stack_top[2] = attr_beg;
id_hash_set (xpd->xpd_wrs, (caddr_t) & key_pos, (caddr_t) pos_stack_top);
dk_free (pos_stack_top, 3 * sizeof (wpos_t));
break;
case XML_MKUP_COMMENT:
case XML_MKUP_REF:
case XML_MKUP_PI:
break;
default:
errmsg = "invalid markup type";
goto done;
}
goto next_element;
done:
dk_free_box (box);
return errmsg;
}
void
xp_attr_word_range (xml_entity_t * xe, wpos_t * start, wpos_t * this_end, wpos_t * tree_end)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
xper_doc_t *xpd = xpe->xe_doc.xpd;
if (XML_MKUP_STAG == xpe->xper_type)
{
ptrlong key_pos = xpe->xper_pos;
if (NULL != xpd->xpd_wrs)
{
wpos_t *res = (wpos_t *) id_hash_get (xpd->xpd_wrs, (caddr_t) & key_pos);
if (NULL == res)
goto fail;
start[0] = res[0];
this_end[0] = res[1];
tree_end[0] = res[2];
return;
}
else
{
caddr_t box = get_tag_data (xpe, (long) key_pos);
size_t namelen;
unsigned char *ptr = (unsigned char *) (box + 1);
unsigned long num_word, addon;
namelen = skip_string_length (&ptr);
ptr += namelen + STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (ptr);
addon = (num_word) >> 16;
if ((3 * 4) <= addon)
{
ptr += 4;
start[0] = LONG_REF_NA (ptr);
ptr += 4;
this_end[0] = LONG_REF_NA (ptr);
ptr += 4;
tree_end[0] = LONG_REF_NA (ptr);
dk_free_box (box);
return;
}
dk_free_box (box);
xpd->xpd_wrs = id_hash_allocate (1021, sizeof (void *), 3 * sizeof (wpos_t), voidptrhash, voidptrhashcmp);
xper_attr_word_count (xpe);
}
}
fail:
start[0] = -1;
this_end[0] = -1;
tree_end[0] = -1;
}
int
xp_get_logical_path (xml_entity_t * xe, dk_set_t * path)
{
xper_entity_t *xpe = (xper_entity_t *) xe;
if (0 != xpe->xper_parent)
{
xper_entity_t *parent_iter = (xper_entity_t *) (box_copy ((box_t) xpe));
int ppos;
for (;;)
{
dk_set_push (path, (void *) (ptrlong) (parent_iter->xper_pos));
ppos = parent_iter->xper_parent;
if (0 == ppos)
break;
fill_xper_entity (parent_iter, ppos);
}
dk_free_box ((box_t) parent_iter);
}
if (NULL != xpe->xe_referer)
return xpe->xe_referer->_->xe_get_logical_path (xpe->xe_referer, path);
dk_set_push (path, xpe->xe_doc.xpd); /* There's no xe->xe_doc.xtd->xd_ref_count += 1 here. It's done intentionally. */
return 1;
}
xml_entity_t *
xp_deref_id (xml_entity_t * xe, const char *idbegin, size_t idlength)
{
return NULL;
}
xml_entity_t *
xp_follow_path (xml_entity_t * xe, ptrlong * path, size_t path_depth)
{
return xe;
}
void dtd_load_from_buffer (dtd_t *res, caddr_t dtd_string)
{
size_t len;
unsigned char *end = (unsigned char *) (dtd_string + box_length (dtd_string));
unsigned char *tail = (unsigned char *) (dtd_string);
char *el_name, *attr_name; /* ... to read key (ID) attributes' definitions, type 'k' */
ecm_el_idx_t el_idx;
ecm_el_t *el;
ecm_attr_idx_t attr_idx;
ecm_attr_t *attr;
char *entname, *sysid, *pubid; /* ... to read generic entities, types 'g' and 'G' */
id_hash_t **dictptr, *dict;
xml_def_4_entity_t *newdef;
unsigned char type_byte;
#define DIG_NAME(name) do {\
len = skip_string_length (&tail); \
name = (char *) dk_alloc (len+1); \
memcpy (name, tail, len); \
name[len] = '\0'; \
tail += len; } while (0)
#define DIG_URI(name) do {\
len = skip_string_length (&tail); \
name = box_dv_short_nchars ((char *)tail, len); \
tail += len; } while (0)
again:
if (tail > end)
GPF_T;
if (tail == end)
return;
type_byte = tail[0];
/* This switch is for compatibility with future versions.
If somebody (i.e. me, of course) will add more types, they will be read fine
while they will consist of type byte and sequence of strings */
switch (type_byte)
{
case 'U':
tail++;
DIG_URI (res->ed_puburi);
break;
case 'u':
tail++;
DIG_URI (res->ed_sysuri);
break;
case 'k':
tail++;
DIG_NAME (el_name); /* Reading of name of element */
DIG_NAME (attr_name); /* Reading of name of element's ID attribute */
el_idx = ecm_map_name (el_name, (void **)&(res->ed_els), &(res->ed_el_no), sizeof (ecm_el_t));
el = res->ed_els + el_idx;
attr_idx = ecm_map_name (attr_name, (void **)&(el->ee_attrs), &(el->ee_attrs_no), sizeof (ecm_attr_t));
attr = el->ee_attrs + attr_idx;
attr->da_type = ECM_AT_ID;
el->ee_has_id_attr = 1;
el->ee_id_attr_idx = attr_idx;
break;
case 'G':
case 'g':
tail++;
DIG_URI (entname); /* Reading of name of entity */
DIG_URI (sysid); /* Reading of SYSTEM URI of the entity */
/* Reading of PUBLIC name of the entity */
if ('g' == type_byte)
pubid = NULL;
else
DIG_URI (pubid);
dictptr = &(res->ed_generics);
if (NULL == dictptr[0])
dictptr[0] = id_hash_allocate (251, sizeof (caddr_t), sizeof (caddr_t), strhash, strhashcmp);
dict = dictptr[0];
newdef = (xml_def_4_entity_t *) dk_alloc (sizeof (xml_def_4_entity_t));
memset (newdef, 0, sizeof (xml_def_4_entity_t));
newdef->xd4e_publicId = pubid;
newdef->xd4e_systemId = sysid;
id_hash_set (dict, (caddr_t) (&entname), (caddr_t) (&newdef));
break;
default:
tail++;
/* no break */
case DV_STRING:
len = skip_string_length (&tail);
tail += len;
}
goto again;
}
int dtd_insert_soft (dtd_t *tgt, dtd_t *src)
{
size_t len;
ecm_el_idx_t src_el_idx, src_el_no;
id_hash_t *src_dict;
int id_attrs_changed = 0;
#define COPY_NAME(to,from) do { \
len = strlen((from)); \
(to) = (char *) dk_alloc (len+1); \
memcpy ((to), (from), len); \
(to)[len] = '\0'; } while (0)
if ((NULL == src) || (NULL == tgt))
return 0;
src_el_no = src->ed_el_no;
for (src_el_idx = 0; src_el_idx < src_el_no; src_el_idx++)
{
ecm_el_t *src_el = src->ed_els + src_el_idx;
ecm_el_t *tgt_el;
ecm_attr_t *tgt_attr;
ecm_attr_idx_t key_idx, tgt_attr_idx;
ecm_el_idx_t tgt_el_idx;
char *src_attrname;
if (!src_el->ee_has_id_attr)
continue;
tgt_el_idx = ecm_find_name (src_el->ee_name, tgt->ed_els, tgt->ed_el_no, sizeof (ecm_el_t));
if (ECM_MEM_NOT_FOUND == tgt_el_idx)
{
char *tgt_elname;
COPY_NAME (tgt_elname, src_el->ee_name);
tgt_el_idx = ecm_map_name (tgt_elname, (void **)&(tgt->ed_els), &(tgt->ed_el_no), sizeof (ecm_el_t));
}
tgt_el = tgt->ed_els + tgt_el_idx;
if (tgt_el->ee_has_id_attr)
continue;
key_idx = src_el->ee_id_attr_idx;
src_attrname = src_el->ee_attrs[key_idx].da_name;
tgt_attr_idx = ecm_find_name (src_attrname, tgt_el->ee_attrs, tgt_el->ee_attrs_no, sizeof (ecm_attr_t));
if (ECM_MEM_NOT_FOUND == tgt_attr_idx)
{
char *tgt_attrname;
COPY_NAME (tgt_attrname, src_attrname);
tgt_attr_idx = ecm_map_name (tgt_attrname, (void **)&(tgt_el->ee_attrs), &(tgt_el->ee_attrs_no), sizeof (ecm_attr_t));
}
tgt_attr = tgt_el->ee_attrs + tgt_attr_idx;
tgt_attr->da_type = ECM_AT_ID;
tgt_el->ee_has_id_attr = 1;
tgt_el->ee_id_attr_idx = tgt_attr_idx;
id_attrs_changed = 1;
}
src_dict = src->ed_generics;
if (NULL != src_dict)
{
id_hash_t *tgt_dict = tgt->ed_generics;
id_hash_iterator_t src_dict_hit;
char **src_dict_key_ptr, *tgt_dict_key;
xml_def_4_entity_t **src_ent_ptr, *tgt_ent;
if (NULL == tgt_dict)
tgt_dict = tgt->ed_generics = id_hash_allocate (251, sizeof (caddr_t), sizeof (caddr_t), strhash, strhashcmp);
for (id_hash_iterator (&src_dict_hit, src_dict);
hit_next (&src_dict_hit, (char **) (&src_dict_key_ptr), (char **) (&src_ent_ptr));
/*no step */ )
{
xml_def_4_entity_t **tgt_hit = (xml_def_4_entity_t **)id_hash_get (tgt_dict, (caddr_t)(src_dict_key_ptr));
if (NULL != tgt_hit)
continue;
tgt_ent = (xml_def_4_entity_t *) dk_alloc (sizeof (xml_def_4_entity_t));
memset (tgt_ent, 0, sizeof (xml_def_4_entity_t));
if (src_ent_ptr[0]->xd4e_literalVal)
tgt_ent->xd4e_literalVal = box_dv_short_string (src_ent_ptr[0]->xd4e_literalVal);
if (src_ent_ptr[0]->xd4e_publicId)
tgt_ent->xd4e_publicId = box_dv_short_string (src_ent_ptr[0]->xd4e_publicId);
if (src_ent_ptr[0]->xd4e_systemId)
tgt_ent->xd4e_systemId = box_dv_short_string (src_ent_ptr[0]->xd4e_systemId);
tgt_dict_key = box_dv_short_string (src_dict_key_ptr[0]);
id_hash_set (tgt_dict, (caddr_t) (&tgt_dict_key), (caddr_t) (&tgt_ent));
}
}
return id_attrs_changed;
}
dtd_t *
xp_get_addon_dtd (xml_entity_t * xe)
{
dtd_t *res = NULL;
xper_entity_t *xpe = (xper_entity_t *) xe;
caddr_t root_tag_data = get_tag_data (xpe, XPER_ROOT_POS);
caddr_t root_ns_rec = root_tag_data + 1 /* not STR_NAME_OFF */ + 7 /*DV_SHORT_STR " root" */ + STR_NS_OFF;
long dtd_pos = LONG_REF_NA (root_ns_rec);
dk_free_box (root_tag_data);
if (0 != dtd_pos)
{
caddr_t dtd_string = get_tag_data (xpe, dtd_pos);
res = xe->xe_doc.xpd->xd_dtd = dtd_alloc ();
dtd_load_from_buffer (res, dtd_string);
dk_free_box (dtd_string);
xe_insert_external_dtd (xe);
}
return res;
}
const char *
xp_get_sysid (xml_entity_t * xe, const char *ref_name)
{
xper_entity_t *xpe = (xper_entity_t *) (xe);
dtd_t **xpe_dtd_ptr = &(xpe->xe_doc.xd->xd_top_doc->xd_dtd);
dtd_t *xpe_dtd;
xe_insert_external_dtd (xe);
if (NULL == xpe_dtd_ptr[0])
{ /* Try to process an alternate DTD origin, e.g. read something from XPER BLOB */
dtd_t *addon_dtd = xp_get_addon_dtd ((xml_entity_t *) (xpe));
if (NULL != addon_dtd)
{
dtd_addref (addon_dtd, 0);
xpe_dtd_ptr[0] = addon_dtd;
}
else
{
xpe_dtd_ptr[0] = dtd_alloc ();
dtd_addref (xpe_dtd_ptr[0], 0);
}
}
xpe_dtd = xpe_dtd_ptr[0];
if (NULL != xpe_dtd)
{
id_hash_t *dict = xpe_dtd->ed_generics;
if (NULL != dict)
{
caddr_t hash_val = id_hash_get (dict, (caddr_t) (&ref_name));
if (NULL != hash_val)
{
xml_def_4_entity_t *edef = ((xml_def_4_entity_t **) (void **) (hash_val))[0];
return edef->xd4e_systemId;
}
}
}
xpe_dtd = xpe->xe_doc.xd->xd_top_doc->xd_dtd;
if (NULL != xpe_dtd)
{
id_hash_t *dict = xpe_dtd->ed_generics;
if (NULL != dict)
{
caddr_t hash_val = id_hash_get (dict, (caddr_t) (&ref_name));
if (NULL != hash_val)
{
xml_def_4_entity_t *edef = ((xml_def_4_entity_t **) (void **) (hash_val))[0];
return edef->xd4e_systemId;
}
}
}
return NULL;
}
/*! \brief Calculates position in cut, based on difference between source and cut.
The result will be additionally saved in res4_ADJUST_POS */
#define ADJUST_POS(p, if_less_than) \
(res4_ADJUST_POS = (p), \
res4_ADJUST_POS = ( \
(res4_ADJUST_POS < src_start_pos) ? \
(if_less_than) : \
(tgt_start_pos + (res4_ADJUST_POS - src_start_pos)) \
) )
/*! \brief Reads position item at given \c offset from \c src_box_tail,
call ADJUST_POS for it and stores the adjusted value back */
#define TRANSLATE_POS(offset, if_less_than) \
(ptr4_TRANSLATE = src_box_tail+(offset), \
ADJUST_POS(LONG_REF_NA(ptr4_TRANSLATE), (if_less_than)), \
LONG_SET_NA(ptr4_TRANSLATE, res4_ADJUST_POS) )
/*! \brief Stores a constant at given \c offset from \c src_box_tail */
#define TRANSLATE_TO_CONST(offset, value) \
(ptr4_TRANSLATE = src_box_tail+(offset), \
res4_TRANSLATE = (value), \
LONG_SET_NA(ptr4_TRANSLATE, res4_TRANSLATE) )
/*! \brief Reads main text's word number at given \c offset from \c src_box_tail,
shifts it to new base and stores the changed value back */
#define TRANSLATE_MAIN_WORD_NO(offset) \
(ptr4_TRANSLATE = src_box_tail+(offset), \
res4_TRANSLATE = LONG_REF_NA(ptr4_TRANSLATE)-src_main_start_word, \
LONG_SET_NA(ptr4_TRANSLATE, res4_TRANSLATE) )
/*! \brief Reads attribute text's word number at given \c offset from \c src_box_tail,
shifts it to new base and stores the changed value back */
#define TRANSLATE_ATTR_WORD_NO(offset) \
(ptr4_TRANSLATE = src_box_tail+(offset), \
res4_TRANSLATE = LONG_REF_NA(ptr4_TRANSLATE)-src_attr_start_word+FIRST_ATTR_WORD_POS, \
LONG_SET_NA(ptr4_TRANSLATE, res4_TRANSLATE) )
static size_t
register_ns (volatile long *volatile end_of_cut_pos, dk_set_t * namespaces, size_t old_ns_pos, xper_entity_t * src_xpe)
{
xper_ns_t *new_ns;
dtp_t str_type;
DO_SET (xper_ns_t *, ns_i, namespaces)
if (ns_i->xpns_old_pos == old_ns_pos)
return ns_i->xpns_pos;
END_DO_SET ();
/* Now we're sure that the given old_ns_pos was never provided before */
new_ns = (xper_ns_t *) dk_alloc (sizeof (xper_ns_t));
memset (new_ns, 0, sizeof (xper_ns_t));
new_ns->xpns_uri = get_tag_data (src_xpe, (long) old_ns_pos);
new_ns->xpns_old_pos = old_ns_pos;
new_ns->xpns_pos = end_of_cut_pos[0];
str_type = box_tag (new_ns->xpns_uri);
end_of_cut_pos[0] += ((DV_SHORT_STRING_SERIAL == str_type) ? 2 : 5);
end_of_cut_pos[0] += box_length (new_ns->xpns_uri);
dk_set_push (namespaces, new_ns);
return new_ns->xpns_pos;
}
static void
translate_attributes (volatile long *volatile end_of_cut_pos, dk_set_t * namespaces, unsigned char *src_box_tail, xper_entity_t * src_xpe)
{
long num_word, addons, no_of_attrs;
long old_ns_pos;
size_t attr_len;
unsigned char *ptr4_TRANSLATE; /* Temporary for TRANSLATE_XXX macros */
long res4_TRANSLATE; /* Temporary for TRANSLATE_WORD_NO macro */
dtp_t attr_box_type;
src_box_tail += STR_ATTR_NO_OFF;
num_word = LONG_REF_NA (src_box_tail);
src_box_tail += 4;
no_of_attrs = num_word & 0xFFFF;
addons = ((unsigned long) (num_word)) >> 16;
src_box_tail += addons;
while (no_of_attrs >= 2)
{
attr_box_type = src_box_tail[0];
attr_len = skip_string_length (&src_box_tail);
if (DV_ARRAY_OF_POINTER == attr_box_type)
{
src_box_tail += attr_len - 4;
old_ns_pos = LONG_REF_NA (src_box_tail);
if (old_ns_pos)
{
TRANSLATE_TO_CONST (0,
(long) register_ns (end_of_cut_pos, namespaces, old_ns_pos, src_xpe));
}
src_box_tail += 4;
}
else
src_box_tail += attr_len;
attr_len = skip_string_length (&src_box_tail);
src_box_tail += attr_len;
no_of_attrs -= 2;
}
}
xper_entity_t *
DBG_NAME (xper_cut_xper) (DBG_PARAMS query_instance_t * volatile qi, xper_entity_t * src_xpe)
{
xper_doc_t *tgt_xpd; /* New document */
buffer_desc_t *buf; /* Buffer for new document */
xper_entity_t *tgt_xpe; /* Resulting entity */
xper_ctx_t context; /* Context of creation of new document */
caddr_t src_box; /* Current box from source document */
size_t src_box_length; /* box_length(src_box) */
volatile long src_start_pos; /* The first position of \src_box, equal to \c xpe_src->pos */
long src_curr_pos; /* Position of \src_box, growth from \c src_start_pos to \c src_end_pos */
dtp_t src_box_type; /* Type of \c src_box (DV_LONG/SHORT_STRING, DV_XML_MARKUP) */
unsigned char *src_box_tail; /* Pointer to past-the-end of name in \c src_box */
long src_main_start_word; /* Starting main text's word number in \c xpe_src, ending on will be in context.xpc_main_word_ctr */
volatile long src_attr_start_word; /* Starting attribute text's word number in \c xpe_src, ending on will be in context.xpc_attr_word_ctr */
volatile long src_end_pos; /* The last in-range position for beginning of some \c src_box */
volatile long tgt_start_pos; /* Starting pos in resulting document's BLOB */
volatile long tgt_end_pos; /* Current ending pos of resulting document's BLOB */
long old_ns_pos; /* Namespace position in old (=source) document */
char mkup_type; /* XML_MKUP_XXX value for \c src_box */
long res4_ADJUST_POS; /* Temporary for ADJUST_POS macro */
unsigned char *ptr4_TRANSLATE; /* Temporary for TRANSLATE_XXX macros */
long res4_TRANSLATE; /* Temporary for TRANSLATE_WORD_NO macro */
caddr_t volatile root_box; /* Temporary box for root start and end tags */
xper_ns_t *curr_ns; /* Current namespace to write */
long num_word; /* Mix of number of attributes and size of addon */
long addons; /* Length of addon */
dbe_key_t* xper_key;
unsigned char *tmp_tail;
xper_stag_t root_data;
vxml_parser_attrdata_t root_attrdata;
dtd_t **src_dtd_ptr; /* Pointer to the pointer to DTD in source document */
xper_dbg_print ("xper_cut_xper\n");
/* If this function was called for this entity with the same value of \c xper_pos, then we may have
cached value, and we can return its copy without re-calculation */
if (src_xpe->xper_cut_pos == src_xpe->xper_pos)
return (xper_entity_t *) (xp_copy ((xml_entity_t *) (src_xpe->xper_cut_ent)));
/* If the entity is " root", then just copy it */
if (XPER_ROOT_POS == src_xpe->xper_pos)
return (xper_entity_t *) (xp_copy ((xml_entity_t *) (src_xpe)));
/* If the entity is the only child of " root", then just copy it */
do
{
size_t p1, p2;
if (
(XPER_ROOT_POS + MIN_START_ROOT_RECORD_SZ != src_xpe->xper_pos) &&
(XPER_ROOT_POS + MIN_START_ROOT_RECORD_SZ + (3 * 4) != src_xpe->xper_pos))
break;
if (src_xpe->xper_type != XML_MKUP_STAG)
break;
p1 = (src_xpe->xe_doc.xpd->xpd_bh->bh_length - END_ROOT_RECORD_SZ);
p2 = src_xpe->xper_end + (1 + 4 + 1) + dv_string_size (src_xpe->xper_name);
if (p1 == p2)
return (xper_entity_t *) (xp_copy ((xml_entity_t *) (src_xpe)));
} while (0);
src_dtd_ptr = &(src_xpe->xe_doc.xd->xd_dtd);
if (NULL == src_dtd_ptr[0])
{ /* Try to process an alternate DTD origin, e.g. read something from XPER BLOB */
dtd_t *addon_dtd = xp_get_addon_dtd ((xml_entity_t *) (src_xpe));
if (NULL != addon_dtd)
{
dtd_addref (addon_dtd, 0);
src_dtd_ptr[0] = addon_dtd;
}
else
{
src_dtd_ptr[0] = dtd_alloc ();
dtd_addref (src_dtd_ptr[0], 0);
}
}
memset (&context, 0, sizeof (context));
tgt_xpd = (xper_doc_t *) dk_alloc (sizeof (xper_doc_t));
memset (tgt_xpd, 0, sizeof (xper_doc_t));
#ifdef MALLOC_DEBUG
tgt_xpd->xd_dbg_file = (char *) file;
tgt_xpd->xd_dbg_line = line;
#endif
tgt_xpd->xpd_state = XPD_NEW;
/* If qi is not provided, then use one from source. */
if (NULL == qi)
tgt_xpd->xd_qi = qi = src_xpe->xe_doc.xpd->xd_qi;
else
tgt_xpd->xd_qi = qi_top_qi (qi);
tgt_xpd->xd_ref_count = 0;
memset (&context, 0, sizeof (context));
context.xpc_doc = tgt_xpd;
QR_RESET_CTX
{
/* Phase 1. Data should be read and translated */
memset (&root_data, 0, sizeof (root_data));
memset (&root_attrdata, 0, sizeof (root_attrdata));
root_data.type = XML_MKUP_STAG;
root_data.name = uname__root;
root_data.lh = xper_find_lang_handler (src_xpe);
root_data.attrdata = &root_attrdata;
src_start_pos = src_xpe->xper_pos;
src_main_start_word = 0; /* Just to remove 'used uninitialized' warning */
old_ns_pos = -1; /* Just to remove 'used uninitialized' warning */
src_curr_pos = src_start_pos;
/* There are two different cases: character data and any other stuff. If only
one character data box should be stored, we should count words in it.
Otherwise it's enough to copy box "as is" and adjust all positions and word
counts without thinking about correctness of them. */
if (XML_MKUP_TEXT != src_xpe->xper_type)
{
src_box = get_tag_data (src_xpe, src_curr_pos);
src_box_type = box_tag (src_box);
mkup_type = src_box[0];
switch (mkup_type)
{
case XML_MKUP_STAG:
case XML_MKUP_REF:
case XML_MKUP_PI:
src_box_tail = (unsigned char *) (src_box + 1);
if (DV_STRING == *src_box_tail)
src_box_tail += 5 + LONG_REF_NA (src_box_tail + 1);
else
src_box_tail += 2 + (src_box_tail[1]);
tmp_tail = src_box_tail;
/* In these cases starting tag structure may contain addons with attribute indexing data */
num_word = LONG_REF_NA (src_box_tail + STR_ATTR_NO_OFF);
addons = ((unsigned long) (num_word)) >> 16;
if ((3 * 4) <= addons)
{
src_attr_start_word = LONG_REF_NA (src_box_tail + STR_ADDON_OR_ATTR_OFF);
root_data.wrs.xewr_attr_beg = FIRST_ATTR_WORD_POS;
root_data.wrs.xewr_attr_this_end = FIRST_ATTR_WORD_POS + LONG_REF_NA (src_box_tail + STR_ADDON_OR_ATTR_OFF + (1 * 4)) - src_attr_start_word;
root_data.wrs.xewr_attr_tree_end = FIRST_ATTR_WORD_POS + LONG_REF_NA (src_box_tail + STR_ADDON_OR_ATTR_OFF + (2 * 4)) - src_attr_start_word;
context.xpc_attr_word_ctr = root_data.wrs.xewr_attr_tree_end;
root_data.recalc_wrs = 0;
root_data.store_wrs = 1;
}
else
{
context.xpc_attr_word_ctr = 0;
root_data.recalc_wrs = 0;
root_data.store_wrs = 0;
}
root_box = start_tag_record (&root_data);
dk_set_push (&context.xpc_cut_chain, root_box);
tgt_start_pos = XPER_ROOT_POS + 5 + box_length (root_box);
src_box_tail = (unsigned char *) (root_box + 1 + 7) /*DV_SHORT_STR " root" */ ;
TRANSLATE_TO_CONST (STR_FIRST_CHILD_OFF, ADJUST_POS (src_curr_pos, 0));
src_box_tail = tmp_tail;
src_end_pos = LONG_REF_NA (src_box_tail + STR_END_TAG_OFF);
tgt_end_pos = (long) (
tgt_start_pos + /* Ordinary data will be started at tgt_start_pos */
(src_end_pos - src_start_pos) + /* space before closing tag */
5 + /* space for box header of closing tag */
(src_box_tail - (unsigned char *) (src_box)) + /* space for the body of closing tag */
(1 + 4 + 1 + 7 /*DV_SHORT_STR " root" */ ) /* space for closing " root" tag */
);
src_main_start_word = LONG_REF_NA (src_box_tail + STR_START_WORD_OFF);
context.xpc_main_word_ctr = LONG_REF_NA (src_box_tail + STR_END_WORD_OFF) - src_main_start_word;
/* This value of word counter will not be used until writing of closing tag for " root" */
TRANSLATE_TO_CONST (STR_PARENT_OFF, XPER_ROOT_POS);
TRANSLATE_TO_CONST (STR_LEFT_SIBLING_OFF, 0);
TRANSLATE_TO_CONST (STR_RIGHT_SIBLING_OFF, 0);
TRANSLATE_POS (STR_FIRST_CHILD_OFF, 0);
TRANSLATE_POS (STR_END_TAG_OFF, 0);
TRANSLATE_TO_CONST (STR_START_WORD_OFF, 0);
TRANSLATE_MAIN_WORD_NO (STR_END_WORD_OFF);
if ((3 * 4) <= addons)
{
TRANSLATE_ATTR_WORD_NO (STR_ADDON_OR_ATTR_OFF);
TRANSLATE_ATTR_WORD_NO (STR_ADDON_OR_ATTR_OFF + (4 * 1));
TRANSLATE_ATTR_WORD_NO (STR_ADDON_OR_ATTR_OFF + (4 * 2));
}
old_ns_pos = LONG_REF_NA (src_box_tail + STR_NS_OFF);
if (old_ns_pos)
{
TRANSLATE_TO_CONST (STR_NS_OFF,
(long) register_ns (&tgt_end_pos, &context.xpc_cut_namespaces, old_ns_pos, src_xpe));
}
translate_attributes (&tgt_end_pos, &context.xpc_cut_namespaces, src_box_tail, src_xpe);
break;
case XML_MKUP_ETAG:
case XML_MKUP_TEXT:
case XML_MKUP_COMMENT:
root_data.wrs.xewr_attr_beg = FIRST_ATTR_WORD_POS;
root_data.wrs.xewr_attr_this_end = FIRST_ATTR_WORD_POS;
root_data.wrs.xewr_attr_tree_end = FIRST_ATTR_WORD_POS;
root_data.recalc_wrs = 0;
root_data.store_wrs = 1;
root_box = start_tag_record (&root_data);
dk_set_push (&context.xpc_cut_chain, root_box);
tgt_start_pos = XPER_ROOT_POS + 5 + box_length (root_box);
src_end_pos = src_start_pos;
break;
default:
sqlr_new_error ("XE000", "XP9B4", "Error while indexing cutting XML: invalid markup tag");
}
dk_set_push (&context.xpc_cut_chain, src_box);
src_curr_pos += ((DV_SHORT_STRING_SERIAL == src_box_type) ? 2 : 5);
src_curr_pos += box_length (src_box);
while (src_curr_pos <= src_end_pos)
{
src_box = get_tag_data (src_xpe, src_curr_pos);
src_box_type = box_tag (src_box);
src_curr_pos += ((DV_SHORT_STRING_SERIAL == src_box_type) ? 2 : 5);
src_curr_pos += box_length (src_box);
mkup_type = ((DV_XML_MARKUP != src_box_type) ? XML_MKUP_TEXT : src_box[0]);
switch (mkup_type)
{
case XML_MKUP_STAG:
case XML_MKUP_REF:
case XML_MKUP_PI:
src_box_tail = (unsigned char *) (src_box + 1);
if (DV_STRING == *src_box_tail)
src_box_tail += 5 + LONG_REF_NA (src_box_tail + 1);
else
src_box_tail += 2 + (src_box_tail[1]);
TRANSLATE_POS (STR_PARENT_OFF, XPER_ROOT_POS);
TRANSLATE_POS (STR_LEFT_SIBLING_OFF, 0);
TRANSLATE_POS (STR_RIGHT_SIBLING_OFF, 0);
TRANSLATE_POS (STR_FIRST_CHILD_OFF, 0);
TRANSLATE_POS (STR_END_TAG_OFF, 0);
TRANSLATE_MAIN_WORD_NO (STR_START_WORD_OFF);
TRANSLATE_MAIN_WORD_NO (STR_END_WORD_OFF);
num_word = LONG_REF_NA (src_box_tail + STR_ATTR_NO_OFF);
addons = ((unsigned long) (num_word)) >> 16;
if ((3 * 4) <= addons)
{
TRANSLATE_ATTR_WORD_NO (STR_ADDON_OR_ATTR_OFF);
TRANSLATE_ATTR_WORD_NO (STR_ADDON_OR_ATTR_OFF + (4 * 1));
TRANSLATE_ATTR_WORD_NO (STR_ADDON_OR_ATTR_OFF + (4 * 2));
}
old_ns_pos = LONG_REF_NA (src_box_tail + STR_NS_OFF);
if (old_ns_pos)
{
TRANSLATE_TO_CONST (STR_NS_OFF,
(long) register_ns (&tgt_end_pos, &context.xpc_cut_namespaces, old_ns_pos, src_xpe));
}
translate_attributes (&tgt_end_pos, &context.xpc_cut_namespaces, src_box_tail, src_xpe);
/*TRANSLATE_POS(STR_NS_OFF, 0); */
break;
case XML_MKUP_ETAG:
case XML_MKUP_COMMENT:
case XML_MKUP_TEXT:
break;
default:
sqlr_new_error ("XE000", "XP9B4", "Error while cutting XML: invalid markup tag");
}
dk_set_push (&context.xpc_cut_chain, src_box);
}
}
else
{
root_data.wrs.xewr_attr_beg = FIRST_ATTR_WORD_POS;
root_data.wrs.xewr_attr_this_end = FIRST_ATTR_WORD_POS;
root_data.wrs.xewr_attr_tree_end = FIRST_ATTR_WORD_POS;
root_data.recalc_wrs = 0;
root_data.store_wrs = 1;
root_box = start_tag_record (&root_data);
dk_set_push (&context.xpc_cut_chain, root_box);
tgt_start_pos = XPER_ROOT_POS + 5 + box_length (root_box);
src_main_start_word = 0;
src_box = box_copy (src_xpe->xper_text);
/* src_curr_pos = src_xpe->xper_next_item; */
src_box_length = box_length (src_box);
if (255 < src_box_length)
{
box_tag_modify (src_box, DV_STRING);
src_curr_pos += (long) (5 + src_box_length);
}
else
{
box_tag_modify (src_box, DV_SHORT_STRING_SERIAL);
src_curr_pos += (long) (2 + src_box_length);
}
if (&lh__xany == root_data.lh) /* Optimization for most common case */
context.xpc_main_word_ctr = elh__xany__UTF8.elh_count_words (src_box, box_length (src_box), lh__xany.lh_is_vtb_word);
else
context.xpc_main_word_ctr = lh_count_words (&eh__UTF8, root_data.lh, src_box, box_length (src_box), root_data.lh->lh_is_vtb_word);
dk_set_push (&context.xpc_cut_chain, src_box);
}
src_box_tail = (unsigned char *) (root_box + 1 + 7) /*DV_SHORT_STR " root" */ ;
TRANSLATE_TO_CONST (STR_END_TAG_OFF, ADJUST_POS (src_curr_pos, 0));
TRANSLATE_TO_CONST (STR_END_WORD_OFF, (long) context.xpc_main_word_ctr);
dk_set_push (&context.xpc_cut_chain, end_tag_record (root_data.name));
/* Phase 2. Data should be written */
tgt_xpd->xpd_state = XPD_NEW;
tgt_xpd->xpd_bh = bh_alloc (DV_BLOB_XPER_HANDLE);
context.xpc_itc = itc_create (NULL, qi->qi_trx);
xper_key = sch_id_to_key (wi_inst.wi_schema, KI_COLS);
itc_from (context.xpc_itc, xper_key);
tgt_xpd->xpd_bh->bh_it = context.xpc_itc->itc_tree;
ITC_FAIL (context.xpc_itc)
{
buf = it_new_page (context.xpc_itc->itc_tree, context.xpc_itc->itc_page,
DPF_BLOB, 0, 0);
ITC_LEAVE_MAPS (context.xpc_itc);
if (!buf)
{
xper_destroy_ctx (&context);
sqlr_new_error ("XE000", "XP9A7", "Error allocating a new blob page while cutting XML");
}
tgt_xpd->xpd_bh->bh_page = buf->bd_page;
xper_blob_log_page_to_dir (tgt_xpd->xpd_bh, 0, buf->bd_page);
context.xpc_buf = buf;
}
ITC_FAILED
{
ITC_LEAVE_MAPS (context.xpc_itc);
xper_destroy_ctx (&context);
sqlr_new_error ("XE000", "XP102", "ITC error while cutting XML");
}
END_FAIL (context.xpc_itc);
memcpy (buf->bd_buffer + DP_DATA, XPER_PREFIX, XPER_ROOT_POS);
tgt_xpd->xpd_bh->bh_length = tgt_xpd->xpd_bh->bh_diskbytes = XPER_ROOT_POS;
LONG_SET/*_NA*/ (buf->bd_buffer + DP_BLOB_LEN, XPER_ROOT_POS);
LONG_SET/*_NA*/ (buf->bd_buffer + DP_OVERFLOW, 0);
/* Writing boxes with main data, including root tags */
context.xpc_cut_chain = dk_set_nreverse (context.xpc_cut_chain);
while (NULL != context.xpc_cut_chain)
{
src_box = (caddr_t) (dk_set_pop (&context.xpc_cut_chain));
xper_blob_append_box (&context, src_box);
dk_free_box (src_box);
}
context.xpc_cut_namespaces = dk_set_nreverse (context.xpc_cut_namespaces);
while (NULL != context.xpc_cut_namespaces)
{
curr_ns = (xper_ns_t *) (dk_set_pop (&context.xpc_cut_namespaces));
if (tgt_xpd->xpd_bh->bh_length != curr_ns->xpns_pos)
sqlr_new_error ("XE000", "XP9B4", "Error while cutting XML: internal error in namespace handler");
xper_blob_append_box (&context, curr_ns->xpns_uri);
dk_free_box (curr_ns->xpns_uri);
dk_free (curr_ns, -1);
}
/* Now we put dtd into blob, if there's something to put */
ITC_FAIL (context.xpc_itc)
{
if (0 < dtd_get_buffer_length (src_dtd_ptr[0]))
{
set_long_in_blob (&context, XPER_ROOT_POS + 7 /*DV_SHORT_STR " root" */ +
STR_NAME_OFF + STR_NS_OFF,
(long) tgt_xpd->xpd_bh->bh_length);
blob_append_dtd (&context, src_dtd_ptr[0]);
}
buf_set_dirty (context.xpc_buf);
page_leave_outside_map (context.xpc_buf);
context.xpc_buf = NULL;
}
ITC_FAILED
{
ITC_LEAVE_MAPS (context.xpc_itc);
xper_destroy_ctx (&context);
sqlr_new_error ("XE000", "XP102", "ITC error while cutting XML");
}
END_FAIL (context.xpc_itc);
/* Phase 2 completed */
}
QR_RESET_CODE
{
du_thread_t *self = THREAD_CURRENT_THREAD;
caddr_t err = thr_get_error_code (self);
xper_destroy_ctx (&context);
POP_QR_RESET;
sqlr_resignal (err);
}
END_QR_RESET;
tgt_xpe = (xper_entity_t *) dk_alloc_box_zero (sizeof (xml_entity_un_t), DV_XML_ENTITY);
#ifdef XPER_DEBUG
xper_entity_alloc_ctr++;
#endif
tgt_xpe->_ = &xec_xper_xe;
tgt_xpe->xe_doc.xpd = tgt_xpd;
tgt_xpd->xd_top_doc = (xml_doc_t *) tgt_xpd;
tgt_xpd->xd_uri = box_copy_tree (src_xpe->xe_doc.xpd->xd_uri);
tgt_xpd->xd_ref_count = 1;
tgt_xpd->xd_weight = XML_XPER_DOC_WEIGHT;
tgt_xpd->xd_cost = XML_MAX_DOC_COST;
context.xpc_doc = NULL; /* To prevent destruction in xper_destroy_ctx(). */
xper_destroy_ctx (&context);
xper_blob_truncate_dir_buf (tgt_xpd->xpd_bh);
if (NULL != src_xpe->xper_cut_ent)
dk_free_box ((box_t) src_xpe->xper_cut_ent);
src_xpe->xper_cut_pos = src_start_pos;
src_xpe->xper_cut_ent = tgt_xpe;
/* Now it's time to get the dtd in target's ownership */
dtd_addref (src_dtd_ptr[0], 0);
tgt_xpd->xd_dtd = src_dtd_ptr[0];
return (xper_entity_t *) xp_copy ((xml_entity_t *) tgt_xpe);
}
#undef ADJUST_POS
#undef TRANSLATE_POS
#undef TRANSLATE_WORD_NO
static xml_entity_t *
DBG_NAME (xp_cut) (DBG_PARAMS xml_entity_t * xe, query_instance_t * qi)
{
xper_entity_t *res;
res = DBG_NAME (xper_cut_xper) (DBG_ARGS qi, (xper_entity_t *) xe);
fill_xper_entity (res, XPER_ROOT_POS);
if (((xper_entity_t *) xe)->xper_pos != XPER_ROOT_POS)
fill_xper_entity (res, res->xper_next_item);
if (NULL != xe->xe_attr_name)
res->xe_attr_name = box_copy (xe->xe_attr_name);
return (xml_entity_t *) (res);
}
static xml_entity_t *DBG_NAME (xp_clone) (DBG_PARAMS xml_entity_t * xe, query_instance_t * qi)
{
sqlr_new_error ("42000", "XE022", "Persistent XML entity is not supported by this function");
return NULL;
}
caddr_t
bif_xper_cut (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xper_entity_t *res;
caddr_t source = bif_arg (qst, args, 0, "xml_persistent");
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE007", "Persistent XML entity expected as argument 1 of function xper_cut()");
res = xper_cut_xper ((query_instance_t *) QST_INSTANCE (qst), (xper_entity_t *) source);
fill_xper_entity (res, XPER_ROOT_POS);
if (((xper_entity_t *) source)->xper_pos != XPER_ROOT_POS)
fill_xper_entity (res, res->xper_next_item);
return (caddr_t) res;
}
caddr_t
bif_xper_right_sibling (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xper_entity_t *xpe, *res;
caddr_t source = bif_arg (qst, args, 0, "xper_right_sibling");
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE008", "Persistent XML entity expected as argument 1 of function xper_right_sibling()");
xpe = (xper_entity_t *) source;
if (xpe->xper_right == 0)
return NULL;
res = (xper_entity_t *) xp_copy ((xml_entity_t *) (xpe));
fill_xper_entity (res, xpe->xper_right);
return (caddr_t) (res);
}
caddr_t
bif_xper_left_sibling (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xper_entity_t *xpe, *res;
caddr_t source = bif_arg (qst, args, 0, "xper_left_sibling");
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE009", "Persistent XML entity expected as argument 1 of function xper_right_sibling()");
xpe = (xper_entity_t *) source;
if (xpe->xper_left == 0)
return NULL;
res = (xper_entity_t *) xp_copy ((xml_entity_t *) (xpe));
if (!xp_go_left (res))
{
dk_free_box ((box_t) res);
return NULL;
}
return (caddr_t) (res);
}
caddr_t
bif_xper_parent (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xper_entity_t *xpe, *res;
caddr_t source = bif_arg (qst, args, 0, "xper_parent");
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE010", "Persistent XML entity expected as argument 1 of function xper_parent()");
xpe = (xper_entity_t *) source;
if (xpe->xper_parent == 0)
return NULL;
res = (xper_entity_t *) xp_copy ((xml_entity_t *) (xpe));
fill_xper_entity (res, xpe->xper_parent);
return (caddr_t) (res);
}
caddr_t
bif_xper_root_entity (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xper_entity_t *res;
caddr_t source = bif_arg (qst, args, 0, "xper_root_entity");
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE011", "Persistent XML entity expected as argument 1 of function xper_root_entity()");
res = (xper_entity_t *) xp_copy ((xml_entity_t *) (source));
fill_xper_entity (res, XPER_ROOT_POS);
return (caddr_t) (res);
}
caddr_t
bif_xper_locate_words (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xper_entity_t *xpe, *res, *iter;
int n_args = BOX_ELEMENTS (args);
caddr_t source = bif_arg (qst, args, 0, "xper_locate_words");
long start_word = (long) bif_long_arg (qst, args, 1, "xper_locate_words");
long end_word = (long) ((n_args > 2) ? bif_long_arg (qst, args, 2, "xper_locate_words") : start_word);
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE012", "Persistent XML entity expected as argument 1 of function xper_locate_words()");
if (end_word < start_word)
sqlr_new_error ("42000", "XE013", "In call of function xper_locate_words(), value of argument 3 is greater than of argument 2");
xpe = (xper_entity_t *) (source);
if (XML_MKUP_STAG != xpe->xper_type)
return NULL;
if ((xpe->xper_start_word > start_word) || (xpe->xper_end_word < end_word))
return NULL;
res = (xper_entity_t *) xp_copy ((xml_entity_t *) xpe);
iter = (xper_entity_t *) xp_copy ((xml_entity_t *) res);
for (;;)
{
if (iter->xper_start_word > end_word) /* We're too far to the right. */
break;
if (iter->xper_end_word < start_word) /* We're too far to the left. */
{
if (!iter->xper_right)
break;
fill_xper_entity (iter, iter->xper_right);
continue;
}
if ((iter->xper_start_word <= start_word) && (iter->xper_end_word >= end_word)) /* We're above the place */
{
if (XML_MKUP_STAG != iter->xper_type) /* Place is busy with non-tag entity */
break;
if (!iter->xper_first_child) /* No way to bury in depth */
{
dk_free_box ((box_t) res);
return (caddr_t) (iter);
}
dk_free_box ((box_t) res);
res = (xper_entity_t *) xp_copy ((xml_entity_t *) iter);
fill_xper_entity (iter, iter->xper_first_child);
continue;
}
/* Interval of words may cover more than one tag. Let's move one child right to clarify */
if (!iter->xper_right)
break;
fill_xper_entity (iter, iter->xper_right);
}
dk_free_box ((box_t) iter);
return (caddr_t) (res);
}
caddr_t
bif_xper_tell (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t source = bif_arg (qst, args, 0, "xper_tell");
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE014", "Persistent XML entity expected as argument 1 of function xper_tell()");
return box_num (((xper_entity_t *) (source))->xper_pos);
}
caddr_t
bif_xper_length (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t source = bif_arg (qst, args, 0, "xper_length");
if ((DV_XML_ENTITY != DV_TYPE_OF (source)) || !XE_IS_PERSISTENT (source))
sqlr_new_error ("42000", "XE015", "Persistent XML entity expected as argument 1 of function xper_length()");
return box_num (((xper_entity_t *) (source))->xe_doc.xpd->xpd_bh->bh_length);
}
/* IvAn/XperUpdate/000804 Function xp_log_update added */
void
xp_log_update (xml_entity_t * xe, dk_session_t * log)
{
xper_entity_t *xpe_c = xper_cut_xper (NULL, (xper_entity_t *) xe);
blob_handle_t *bh = (xpe_c->xe_doc.xpd->xpd_bh);
/* The following dirty trick with bh_ask_from_client stuffing is possible
because xp_log_update will be called only inside mutex-ed part of
log_update() */
int from_c = bh->bh_ask_from_client;
xper_dbg_print ("xp_log_update\n");
bh->bh_ask_from_client = 1;
print_object (bh, log, NULL, NULL);
bh->bh_ask_from_client = from_c;
dk_free_box ((box_t) xpe_c);
}
static dk_mutex_t *dtd_refctr_mtx;
#ifdef dtd_addref
#undef dtd_addref
#endif
void
dtd_addref (dtd_t * dtd, int make_global)
{
if (make_global || dtd->ed_is_global)
mutex_enter (dtd_refctr_mtx);
if (make_global)
dtd->ed_is_global = make_global;
dtd->ed_refctr++;
if (dtd->ed_is_global)
mutex_leave (dtd_refctr_mtx);
}
#ifdef dtd_release
#undef dtd_release
#endif
int
dtd_release (dtd_t * dtd)
{
int dtd_is_global = dtd->ed_is_global;
int dtd_refctr;
if (dtd_is_global)
mutex_enter (dtd_refctr_mtx);
dtd_refctr = --(dtd->ed_refctr);
if (dtd_is_global)
mutex_leave (dtd_refctr_mtx);
if (0 > dtd_refctr)
GPF_T;
if (0 != dtd_refctr)
return 0;
dtd_free (dtd);
return 1;
}
int
xml_dtd_destroy (caddr_t box)
{
dtd_release (((dtd_t **) box)[0]);
return 0;
}
caddr_t
xml_dtd_copy (caddr_t box)
{
dtd_t **newres;
dtd_addref (((dtd_t **) box)[0], 0);
newres = (dtd_t **) dk_alloc_box (sizeof (dtd_t *), DV_XML_DTD);
newres[0] = ((dtd_t **) box)[0];
return (caddr_t) newres;
}
caddr_t
bif_dtd_get (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xml_entity_t *entity = bif_entity_arg (qst, args, 0, "dtd_get");
dtd_t **entity_dtd_ptr;
entity_dtd_ptr = &(entity->xe_doc.xd->xd_dtd);
if (NULL == entity_dtd_ptr[0])
{ /* Try to process an alternate DTD origin, e.g. read something from XPER BLOB */
dtd_t *addon_dtd = ((xml_entity_t *) (entity))->_->xe_get_addon_dtd ((xml_entity_t *) (entity));
if (NULL != addon_dtd)
{
dtd_addref (addon_dtd, 0);
entity_dtd_ptr[0] = addon_dtd;
}
}
if (NULL == entity_dtd_ptr[0])
return NEW_DB_NULL;
return xml_dtd_copy ((caddr_t) (entity_dtd_ptr));
}
caddr_t
bif_dtd_attach (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
xml_entity_t *entity = bif_entity_arg (qst, args, 0, "dtd_attach");
caddr_t dtd_box = bif_arg (qst, args, 1, "dtd_attach");
dtd_t **entity_dtd_ptr;
entity_dtd_ptr = &(entity->xe_doc.xd->xd_dtd);
switch (DV_TYPE_OF (dtd_box))
{
case DV_XML_DTD:
dtd_addref (((dtd_t **) dtd_box)[0], 0);
if (NULL != entity_dtd_ptr[0])
dtd_release (entity_dtd_ptr[0]);
entity_dtd_ptr[0] = ((dtd_t **) dtd_box)[0];
break;
case DV_DB_NULL:
/* We cannot simply set entity_dtd_ptr[0] to NULL because bif_dtd_get or similar function may "remember" addon dtd. */
if (NULL != entity_dtd_ptr[0])
dtd_release (entity_dtd_ptr[0]);
entity_dtd_ptr[0] = dtd_alloc ();
dtd_addref (entity_dtd_ptr[0], 0);
break;
default:
sqlr_new_error ("42000", "XE018", "XML DTD object or NULL expected as argument 2 of function dtd_attach()");
break;
}
return NULL; /* never reached */
}
caddr_t
bif_dtd_list_elements (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t dtd_box = bif_arg (qst, args, 0, "dtd_list_elements");
switch (DV_TYPE_OF (dtd_box))
{
case DV_XML_DTD:
{
ecm_el_idx_t el_ctr;
dtd_t *dtd = ((dtd_t **) dtd_box)[0];
caddr_t res = dk_alloc_box (dtd->ed_el_no * sizeof (ptrlong), DV_ARRAY_OF_POINTER);
for (el_ctr = 0; el_ctr < dtd->ed_el_no; el_ctr++)
((ptrlong **) (res))[el_ctr] = (ptrlong *) box_dv_short_string (dtd->ed_els[el_ctr].ee_name);
return res;
}
case DV_DB_NULL:
return dk_alloc_box (0, DV_ARRAY_OF_POINTER);
break;
default:
sqlr_new_error ("42000", "XE019", "XML DTD object or NULL expected as argument 1 of function dtd_list_elements()");
break;
}
return NULL; /* never reached */
}
caddr_t
bif_dtd_get_id_attr_name (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t dtd_box = bif_arg (qst, args, 0, "dtd_get_id_attr_name");
caddr_t el_name = bif_string_arg (qst, args, 1, "dtd_get_id_attr_name");
switch (DV_TYPE_OF (dtd_box))
{
case DV_XML_DTD:
{
dtd_t *dtd = ((dtd_t **) dtd_box)[0];
ecm_el_idx_t el_idx = ecm_find_name (el_name, dtd->ed_els, dtd->ed_el_no, sizeof (dtd->ed_els[0]));
ecm_el_t *el_descr;
ecm_attr_t *id_attr_descr;
if (el_idx < 0)
return NEW_DB_NULL;
el_descr = dtd->ed_els + el_idx;
if (!el_descr->ee_has_id_attr)
return NEW_DB_NULL;
id_attr_descr = el_descr->ee_attrs + el_descr->ee_id_attr_idx;
return box_dv_short_string (id_attr_descr->da_name);
}
break;
case DV_DB_NULL:
return NEW_DB_NULL;
break;
default:
sqlr_new_error ("42000", "XE020", "XML DTD object or NULL expected as argument 1 of function dtd_list_elements()");
break;
}
return NULL; /* never reached */
}
caddr_t
bif_dtd_get_attr_default (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t dtd_box = bif_arg (qst, args, 0, "dtd_get_attr_default");
caddr_t el_name = bif_string_arg (qst, args, 1, "dtd_get_attr_default");
caddr_t attr_name = bif_string_arg (qst, args, 2, "dtd_get_attr_default");
switch (DV_TYPE_OF (dtd_box))
{
case DV_XML_DTD:
{
dtd_t *dtd = ((dtd_t **) dtd_box)[0];
ecm_el_idx_t el_idx;
ecm_el_t *el;
ecm_attr_idx_t attr_idx;
ecm_attr_t *attr;
char *default_value;
el_idx = ecm_find_name (el_name, dtd->ed_els, dtd->ed_el_no, sizeof (dtd->ed_els[0]));
if (el_idx < 0)
return NEW_DB_NULL;
el = dtd->ed_els + el_idx;
attr_idx = ecm_find_name (attr_name, el->ee_attrs, el->ee_attrs_no, sizeof (el->ee_attrs[0]));
if (attr_idx < 0)
return NEW_DB_NULL;
attr = el->ee_attrs + attr_idx;
default_value = ((NULL == attr->da_values) ?
attr->da_default.boxed_value :
((attr->da_default.index < 0) ? NULL : attr->da_values[attr->da_default.index]));
return ((NULL == default_value) ? NULL : box_dv_short_string (default_value));
}
break;
case DV_DB_NULL:
return NEW_DB_NULL;
break;
default:
sqlr_new_error ("42000", "XE021", "XML DTD object or NULL expected as argument 1 of function dtd_get_attr_default()");
break;
}
return NULL; /* never reached */
}
static const char *attribute_type_name[] =
{
/* ECM_AT_CDATA 0 */ "CDATA",
/* ECM_AT_ENUM_NAMES 1 */ "<!-- NAMES -->",
/* ECM_AT_ENUM_NOTATIONS 2 */ "<!-- NOTATIONS -->",
/* ECM_AT_ID 3 */ "ID",
/* ECM_AT_IDREF 4 */ "IDREF",
/* ECM_AT_IDREFS 5 */ "IDREFS",
/* ECM_AT_ENTITY 6 */ "ENTITY",
/* ECM_AT_ENTITIES 7 */ "ENTITIES",
/* ECM_AT_NMTOKEN 8 */ "NMTOKEN",
/* ECM_AT_NMTOKENS 9 */ "NMTOKENS"};
void
dtd_serialize (dtd_t * dtd, dk_session_t * ses)
{
ecm_el_idx_t el_ctr;
ecm_attr_idx_t attr_ctr;
for (el_ctr = dtd->ed_el_no; el_ctr--; /*no step */ )
{
ecm_el_t *el = dtd->ed_els + el_ctr;
session_buffered_write (ses, "<!ELEMENT ", 10);
session_buffered_write (ses, el->ee_name, strlen (el->ee_name));
session_buffered_write_char ('\t', ses);
if (NULL != el->ee_grammar)
session_buffered_write (ses, el->ee_grammar, strlen (el->ee_grammar));
else
session_buffered_write (ses, "ANY", 3);
session_buffered_write (ses, ">\n", 2);
if (NULL != el->ee_errmsg)
{
session_buffered_write (ses, "<!-- ", 5);
session_buffered_write (ses, el->ee_errmsg, strlen (el->ee_errmsg));
session_buffered_write (ses, " -->", 4);
}
}
for (el_ctr = dtd->ed_el_no; el_ctr--; /*no step */ )
{
ecm_el_t *el = dtd->ed_els + el_ctr;
session_buffered_write (ses, "<!ATTLIST ", 10);
session_buffered_write (ses, el->ee_name, strlen (el->ee_name));
for (attr_ctr = el->ee_attrs_no; attr_ctr--; /*no step */ )
{
#if 000
ptrlong ctr;
#endif
ecm_attr_t *attr = el->ee_attrs + attr_ctr;
const char *attrtype = attribute_type_name[attr->da_type];
session_buffered_write (ses, "\n ", 3);
session_buffered_write (ses, attr->da_name, strlen (attr->da_name));
session_buffered_write_char ('\t', ses);
session_buffered_write (ses, attrtype, strlen (attrtype));
#if 000
for (ctr = attr->da_values_no; ctr--; /* no step */ )
dk_free (attr->da_values[ctr], -1);
if (NULL == attr->da_values)
{
if (NULL != attr->da_default.ptr)
dk_free (attr->da_default.ptr, -1);
}
else
dk_free_box (attr->da_values);
#endif
}
session_buffered_write (ses, "\n >\n", 5);
}
}
int
dtd_ses_write_func (void *obj, dk_session_t * ses)
{
dtd_t *dtd = ((dtd_t **) obj)[0];
dk_session_t *strses = strses_allocate ();
dtd_serialize (dtd, strses);
session_buffered_write_char (DV_STRING, ses);
print_long (strses_length (strses), ses);
strses_write_out (strses, ses);
strses_free (strses);
return 1;
}
xe_class_t xec_xper_xe;
void
bif_xper_init (void)
{
dtd_refctr_mtx = mutex_allocate ();
dk_mem_hooks (DV_XML_DTD, xml_dtd_copy, xml_dtd_destroy, 0);
PrpcSetWriter (DV_XML_DTD, (ses_write_func) dtd_ses_write_func);
#ifdef MALLOC_DEBUG
xec_xper_xe.dbg_xe_copy = dbg_xp_copy;
xec_xper_xe.dbg_xe_cut = dbg_xp_cut;
xec_xper_xe.dbg_xe_clone = dbg_xp_clone;
xec_xper_xe.dbg_xe_attribute = dbg_xp_attribute;
xec_xper_xe.dbg_xe_string_value = dbg_xp_string_value;
#else
xec_xper_xe.xe_cut = xp_cut;
xec_xper_xe.xe_copy = xp_copy;
xec_xper_xe.xe_clone = xp_clone;
xec_xper_xe.xe_attribute = xp_attribute;
xec_xper_xe.xe_string_value = xp_string_value;
#endif
xec_xper_xe.xe_string_value_is_nonempty = xp_string_value_is_nonempty;
xec_xper_xe.xe_first_child = xp_first_child;
xec_xper_xe.xe_last_child = NULL; /* Not implemented and no need */
xec_xper_xe.xe_get_child_count_any = xp_get_child_count_any;
xec_xper_xe.xe_next_sibling = xp_next_sibling;
/* IvAn/SmartXContains/001025 WR-optimized iteration function added */
xec_xper_xe.xe_next_sibling_wr = xp_next_sibling_wr;
xec_xper_xe.xe_prev_sibling = xp_prev_sibling;
xec_xper_xe.xe_prev_sibling_wr = xp_prev_sibling; /* No optimization written because this axis is rare thing */
xec_xper_xe.xe_element_name = xp_element_name;
xec_xper_xe.xe_ent_name = xp_ent_name;
xec_xper_xe.xe_is_same_as = xp_is_same_as;
xec_xper_xe.xe_destroy = xp_destroy;
xec_xper_xe.xe_serialize = xp_serialize;
xec_xper_xe.xe_attrvalue = xp_attrvalue;
xec_xper_xe.xe_currattrvalue = xp_currattrvalue;
xec_xper_xe.xe_data_attribute_count = xp_data_attribute_count;
xec_xper_xe.xe_up = xp_up;
xec_xper_xe.xe_down = xp_down;
xec_xper_xe.xe_down_rev = xp_down_rev;
xec_xper_xe.xe_word_range = xp_word_range;
xec_xper_xe.xe_attr_word_range = xp_attr_word_range;
/* IvAn/XperUpdate/000804
For trees, log update uses plain serialization.
For Xper, special handler is needed: it's necessary to record only the blob itself. */
xec_xper_xe.xe_log_update = xp_log_update;
xec_xper_xe.xe_get_logical_path = xp_get_logical_path;
xec_xper_xe.xe_get_addon_dtd = xp_get_addon_dtd;
xec_xper_xe.xe_get_sysid = xp_get_sysid;
xec_xper_xe.xe_element_name_test = xp_element_name_test;
xec_xper_xe.xe_ent_name_test = xp_ent_name_test;
xec_xper_xe.xe_ent_node_test = xp_ent_node_test;
xec_xper_xe.xe_deref_id = xp_deref_id;
xec_xper_xe.xe_follow_path = xp_follow_path;
xec_xper_xe.xe_copy_to_xte_head = xp_copy_to_xte_head;
xec_xper_xe.xe_copy_to_xte_subtree = xp_copy_to_xte_subtree;
xec_xper_xe.xe_copy_to_xte_forest = xp_copy_to_xte_forest;
xec_xper_xe.xe_emulate_input = xp_emulate_input;
xec_xper_xe.xe_reference = xp_reference;
xec_xper_xe.xe_find_expanded_name_by_qname = xp_find_expanded_name_by_qname;
xec_xper_xe.xe_namespace_scope = xp_namespace_scope;
bif_define ("xml_persistent", bif_xml_persistent);
bif_set_uses_index (bif_xml_persistent);
bif_define ("xper_doc", bif_xper_doc);
bif_set_uses_index (bif_xper_doc);
bif_define ("xper_cut", bif_xper_cut);
bif_define ("xper_right_sibling", bif_xper_right_sibling);
bif_define ("xper_left_sibling", bif_xper_left_sibling);
bif_define ("xper_parent", bif_xper_parent);
bif_define ("xper_root_entity", bif_xper_root_entity);
bif_define ("xper_locate_words", bif_xper_locate_words);
bif_define ("xper_tell", bif_xper_tell);
bif_define ("xper_length", bif_xper_length);
bif_define ("dtd_get", bif_dtd_get);
bif_set_uses_index (bif_dtd_get);
bif_define ("dtd_attach", bif_dtd_attach);
bif_define ("dtd_list_elements", bif_dtd_list_elements);
bif_define ("dtd_get_id_attr_name", bif_dtd_get_id_attr_name);
bif_define ("dtd_get_attr_default", bif_dtd_get_attr_default);
}
|