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 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376
|
//------------------------------------------------------------------------------
// <copyright file="TdsParser.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Data.SqlClient {
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.ProviderBase;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MSS = Microsoft.SqlServer.Server;
// The TdsParser Object controls reading/writing to the netlib, parsing the tds,
// and surfacing objects to the user.
sealed internal class TdsParser {
private static int _objectTypeCount; // Bid counter
internal readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);
static Task completedTask;
static Task CompletedTask {
get {
if (completedTask == null) {
completedTask = Task.FromResult<object>(null);
}
return completedTask;
}
}
internal int ObjectID {
get {
return _objectID;
}
}
// ReliabilitySection Usage:
//
// #if DEBUG
// TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
//
// RuntimeHelpers.PrepareConstrainedRegions();
// try {
// tdsReliabilitySection.Start();
// #else
// {
// #endif //DEBUG
//
// // code that requires reliability
//
// }
// #if DEBUG
// finally {
// tdsReliabilitySection.Stop();
// }
// #endif //DEBUG
internal struct ReliabilitySection {
#if DEBUG
// do not allocate TLS data in RETAIL bits
[ThreadStatic]
private static int s_reliabilityCount; // initialized to 0 by CLR
private bool m_started; // initialized to false (not started) by CLR
#endif //DEBUG
[Conditional("DEBUG")]
internal void Start() {
#if DEBUG
Debug.Assert(!m_started);
RuntimeHelpers.PrepareConstrainedRegions();
try {
}
finally {
++s_reliabilityCount;
m_started = true;
}
#endif //DEBUG
}
[Conditional("DEBUG")]
internal void Stop() {
#if DEBUG
// cannot assert m_started - ThreadAbortException can be raised before Start is called
if (m_started) {
Debug.Assert(s_reliabilityCount > 0);
RuntimeHelpers.PrepareConstrainedRegions();
try {
}
finally {
--s_reliabilityCount;
m_started = false;
}
}
#endif //DEBUG
}
// you need to setup for a thread abort somewhere before you call this method
[Conditional("DEBUG")]
internal static void Assert(string message) {
#if DEBUG
Debug.Assert(s_reliabilityCount > 0, message);
#endif //DEBUG
}
}
// Default state object for parser
internal TdsParserStateObject _physicalStateObj = null; // Default stateObj and connection for Dbnetlib and non-MARS SNI.
// Also, default logical stateObj and connection for MARS over SNI.
internal TdsParserStateObject _pMarsPhysicalConObj = null; // With MARS enabled, cached physical stateObj and connection.
// Must keep this around - especially for callbacks on pre-MARS
// ReadAsync which will return if physical connection broken!
//
// Per Instance TDS Parser variables
//
// Constants
const int constBinBufferSize = 4096; // Size of the buffer used to read input parameter of type Stream
const int constTextBufferSize = 4096; // Size of the buffer (in chars) user to read input parameter of type TextReader
// State variables
internal TdsParserState _state = TdsParserState.Closed; // status flag for connection
private string _server = ""; // name of server that the parser connects to
internal volatile bool _fResetConnection = false; // flag to denote whether we are needing to call sp_reset
internal volatile bool _fPreserveTransaction = false; // flag to denote whether we need to preserve the transaction when reseting
private SqlCollation _defaultCollation; // default collation from the server
private int _defaultCodePage;
private int _defaultLCID;
internal Encoding _defaultEncoding = null; // for sql character data
private static EncryptionOptions _sniSupportedEncryptionOption = SNILoadHandle.SingletonInstance.Options;
private EncryptionOptions _encryptionOption = _sniSupportedEncryptionOption;
private SqlInternalTransaction _currentTransaction;
private SqlInternalTransaction _pendingTransaction; // pending transaction for Yukon and beyond.
// SQLHOT 483
// need to hold on to the transaction id if distributed transaction merely rolls back without defecting.
private long _retainedTransactionId = SqlInternalTransaction.NullTransactionId;
// This counter is used for the entire connection to track the open result count for all
// operations not under a transaction.
private int _nonTransactedOpenResultCount = 0;
// Connection reference
private SqlInternalConnectionTds _connHandler;
// Async/Mars variables
private bool _fMARS = false;
internal bool _loginWithFailover = false; // set to true while connect in failover mode so parser state object can adjust its logic
internal AutoResetEvent _resetConnectionEvent = null; // Used to serialize executes and call reset on first execute only.
internal TdsParserSessionPool _sessionPool = null; // initialized only when we're a MARS parser.
// Version variables
private bool _isShiloh = false; // set to true if we connect to a 8.0 server (SQL 2000) or later
private bool _isShilohSP1 = false; // set to true if speaking to Shiloh SP1 or later
private bool _isYukon = false; // set to true if speaking to Yukon or later
private bool _isKatmai = false;
private bool _isDenali = false;
private byte[] _sniSpnBuffer = null;
//
// SqlStatistics
private SqlStatistics _statistics = null;
private bool _statisticsIsInTransaction = false;
//
// STATIC TDS Parser variables
//
// NIC address caching
private static byte[] s_nicAddress; // cache the NIC address from the registry
// SSPI variables
private static bool s_fSSPILoaded = false; // bool to indicate whether library has been loaded
private volatile static UInt32 s_maxSSPILength = 0; // variable to hold max SSPI data size, keep for token from server
// ADAL variables
private static bool s_fADALLoaded = false; // bool to indicate whether ADAL library has been loaded
// textptr sequence
private static readonly byte[] s_longDataHeader = { 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
private static object s_tdsParserLock = new object();
// Various other statics
private const int ATTENTION_TIMEOUT = 5000; // internal attention timeout, in ticks
// XML metadata substitue sequence
private static readonly byte[] s_xmlMetadataSubstituteSequence = { 0xe7, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 };
// size of Guid (e.g. _clientConnectionId, ActivityId.Id)
private const int GUID_SIZE = 16;
// NOTE: You must take the internal connection's _parserLock before modifying this
internal bool _asyncWrite = false;
// TCE supported flag, used to determine if new TDS fields are present. This is
// useful when talking to downlevel/uplevel server.
private bool _serverSupportsColumnEncryption = false;
/// <summary>
/// Get or set if column encryption is supported by the server.
/// </summary>
internal bool IsColumnEncryptionSupported {
get {
return _serverSupportsColumnEncryption;
}
set {
_serverSupportsColumnEncryption = value;
}
}
internal TdsParser(bool MARS, bool fAsynchronous) {
_fMARS = MARS; // may change during Connect to pre Yukon servers
_physicalStateObj = new TdsParserStateObject(this);
}
internal SqlInternalConnectionTds Connection {
get {
return _connHandler;
}
}
internal SqlInternalTransaction CurrentTransaction {
get {
return _currentTransaction;
}
set {
Debug.Assert(value == _currentTransaction
|| null == _currentTransaction
|| null == value
|| (null != _currentTransaction && !_currentTransaction.IsLocal), "attempting to change current transaction?");
// If there is currently a transaction active, we don't want to
// change it; this can occur when there is a delegated transaction
// and the user attempts to do an API begin transaction; in these
// cases, it's safe to ignore the set.
if ((null == _currentTransaction && null != value)
||(null != _currentTransaction && null == value)) {
_currentTransaction = value;
}
}
}
internal int DefaultLCID {
get {
return _defaultLCID;
}
}
internal EncryptionOptions EncryptionOptions {
get {
return _encryptionOption;
}
set {
_encryptionOption = value;
}
}
internal bool IsYukonOrNewer {
get {
return _isYukon;
}
}
internal bool IsKatmaiOrNewer {
get {
return _isKatmai;
}
}
internal bool MARSOn {
get {
return _fMARS;
}
}
internal SqlInternalTransaction PendingTransaction {
get {
return _pendingTransaction;
}
set {
Debug.Assert (null != value, "setting a non-null PendingTransaction?");
_pendingTransaction = value;
}
}
internal string Server {
get {
return _server;
}
}
internal TdsParserState State {
get {
return _state;
}
set {
_state = value;
}
}
internal SqlStatistics Statistics {
get {
return _statistics;
}
set {
_statistics = value;
}
}
private bool IncludeTraceHeader {
get {
return (_isDenali && Bid.TraceOn && Bid.IsOn(ActivityCorrelator.CorrelationTracePoints));
}
}
internal int IncrementNonTransactedOpenResultCount() {
// IMPORTANT - this increments the connection wide open result count for all
// operations not under a transaction! Do not call if you intend to modify the
// count for a transaction!
Debug.Assert(_nonTransactedOpenResultCount >= 0, "Unexpected result count state");
int result = Interlocked.Increment(ref _nonTransactedOpenResultCount);
return result;
}
internal void DecrementNonTransactedOpenResultCount() {
// IMPORTANT - this decrements the connection wide open result count for all
// operations not under a transaction! Do not call if you intend to modify the
// count for a transaction!
Interlocked.Decrement(ref _nonTransactedOpenResultCount);
Debug.Assert(_nonTransactedOpenResultCount >= 0, "Unexpected result count state");
}
internal void ProcessPendingAck(TdsParserStateObject stateObj) {
if (stateObj._attentionSent) {
ProcessAttention(stateObj);
}
}
internal void Connect(ServerInfo serverInfo,
SqlInternalConnectionTds connHandler,
bool ignoreSniOpenTimeout,
long timerExpire,
bool encrypt,
bool trustServerCert,
bool integratedSecurity,
bool withFailover,
bool isFirstTransparentAttempt,
SqlAuthenticationMethod authType,
bool disableTnir) {
if (_state != TdsParserState.Closed) {
Debug.Assert(false, "TdsParser.Connect called on non-closed connection!");
return;
}
_connHandler = connHandler;
_loginWithFailover = withFailover;
UInt32 sniStatus = SNILoadHandle.SingletonInstance.SNIStatus;
if (sniStatus != TdsEnums.SNI_SUCCESS) {
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
_physicalStateObj.Dispose();
ThrowExceptionAndWarning(_physicalStateObj);
Debug.Assert(false, "SNI returned status != success, but no error thrown?");
}
//Create LocalDB instance if necessary
if (connHandler.ConnectionOptions.LocalDBInstance != null)
LocalDBAPI.CreateLocalDBInstance(connHandler.ConnectionOptions.LocalDBInstance);
if (integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated) {
LoadSSPILibrary();
// now allocate proper length of buffer
_sniSpnBuffer = new byte[SNINativeMethodWrapper.SniMaxComposedSpnLength];
Bid.Trace("<sc.TdsParser.Connect|SEC> SSPI or Active Directory Authentication Library for SQL Server based integrated authentication\n");
}
else {
_sniSpnBuffer = null;
if (authType == SqlAuthenticationMethod.ActiveDirectoryPassword) {
Bid.Trace("<sc.TdsParser.Connect|SEC> Active Directory Password authentication\n");
}
else if (authType == SqlAuthenticationMethod.SqlPassword) {
Bid.Trace("<sc.TdsParser.Connect|SEC> SQL Password authentication\n");
}
else{
Bid.Trace("<sc.TdsParser.Connect|SEC> SQL authentication\n");
}
}
byte[] instanceName = null;
Debug.Assert(_connHandler != null, "SqlConnectionInternalTds handler can not be null at this point.");
_connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.PreLoginBegin);
_connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.InitializeConnection);
bool fParallel = _connHandler.ConnectionOptions.MultiSubnetFailover;
TransparentNetworkResolutionState transparentNetworkResolutionState;
if (_connHandler.ConnectionOptions.TransparentNetworkIPResolution && !disableTnir)
{
if(isFirstTransparentAttempt)
transparentNetworkResolutionState = TransparentNetworkResolutionState.SequentialMode;
else
transparentNetworkResolutionState = TransparentNetworkResolutionState.ParallelMode;
}
else
transparentNetworkResolutionState = TransparentNetworkResolutionState.DisabledMode;
int totalTimeout = _connHandler.ConnectionOptions.ConnectTimeout;
_physicalStateObj.CreatePhysicalSNIHandle(serverInfo.ExtendedServerName, ignoreSniOpenTimeout, timerExpire,
out instanceName, _sniSpnBuffer, false, true, fParallel, transparentNetworkResolutionState, totalTimeout);
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status) {
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
// Since connect failed, free the unmanaged connection memory.
// HOWEVER - only free this after the netlib error was processed - if you
// don't, the memory for the connection object might not be accurate and thus
// a bad error could be returned (as it was when it was freed to early for me).
_physicalStateObj.Dispose();
Bid.Trace("<sc.TdsParser.Connect|ERR|SEC> Login failure\n");
ThrowExceptionAndWarning(_physicalStateObj);
Debug.Assert(false, "SNI returned status != success, but no error thrown?");
}
_server = serverInfo.ResolvedServerName;
if (null != connHandler.PoolGroupProviderInfo) {
// If we are pooling, check to see if we were processing an
// alias which has changed, which means we need to clean out
// the pool. See Webdata 104293.
// This should not apply to routing, as it is not an alias change, routed connection
// should still use VNN of AlwaysOn cluster as server for pooling purposes.
connHandler.PoolGroupProviderInfo.AliasCheck(serverInfo.PreRoutingServerName==null ?
serverInfo.ResolvedServerName: serverInfo.PreRoutingServerName);
}
_state = TdsParserState.OpenNotLoggedIn;
_physicalStateObj.SniContext = SniContext.Snix_PreLoginBeforeSuccessfullWrite; // SQL BU DT 376766
_physicalStateObj.TimeoutTime = timerExpire;
bool marsCapable = false;
_connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.InitializeConnection);
_connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.SendPreLoginHandshake);
UInt32 result = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
//
Bid.Trace("<sc.TdsParser.Connect|SEC> Sending prelogin handshake\n");
SendPreLoginHandshake(instanceName, encrypt);
_connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.SendPreLoginHandshake);
_connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.ConsumePreLoginHandshake);
_physicalStateObj.SniContext = SniContext.Snix_PreLogin;
Bid.Trace("<sc.TdsParser.Connect|SEC> Consuming prelogin handshake\n");
PreLoginHandshakeStatus status = ConsumePreLoginHandshake(authType, encrypt, trustServerCert, integratedSecurity, out marsCapable,
out _connHandler._fedAuthRequired);
if (status == PreLoginHandshakeStatus.InstanceFailure) {
Bid.Trace("<sc.TdsParser.Connect|SEC> Prelogin handshake unsuccessful. Reattempting prelogin handshake\n");
_physicalStateObj.Dispose(); // Close previous connection
// On Instance failure re-connect and flush SNI named instance cache.
_physicalStateObj.SniContext=SniContext.Snix_Connect;
_physicalStateObj.CreatePhysicalSNIHandle(serverInfo.ExtendedServerName, ignoreSniOpenTimeout, timerExpire, out instanceName, _sniSpnBuffer, true, true, fParallel, transparentNetworkResolutionState, totalTimeout);
if (TdsEnums.SNI_SUCCESS != _physicalStateObj.Status) {
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
Bid.Trace("<sc.TdsParser.Connect|ERR|SEC> Login failure\n");
ThrowExceptionAndWarning(_physicalStateObj);
}
UInt32 retCode = SNINativeMethodWrapper.SniGetConnectionId(_physicalStateObj.Handle, ref _connHandler._clientConnectionId);
Debug.Assert(retCode == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SniGetConnectionId");
Bid.Trace("<sc.TdsParser.Connect|SEC> Sending prelogin handshake\n");
SendPreLoginHandshake(instanceName, encrypt);
status = ConsumePreLoginHandshake(authType, encrypt, trustServerCert, integratedSecurity, out marsCapable,
out _connHandler._fedAuthRequired);
// Don't need to check for Sphinx failure, since we've already consumed
// one pre-login packet and know we are connecting to Shiloh.
if (status == PreLoginHandshakeStatus.InstanceFailure) {
Bid.Trace("<sc.TdsParser.Connect|ERR|SEC> Prelogin handshake unsuccessful. Login failure\n");
throw SQL.InstanceFailure();
}
}
Bid.Trace("<sc.TdsParser.Connect|SEC> Prelogin handshake successful\n");
if (_fMARS && marsCapable) {
// if user explictly disables mars or mars not supported, don't create the session pool
_sessionPool = new TdsParserSessionPool(this);
}
else {
_fMARS = false;
}
if (authType == SqlAuthenticationMethod.ActiveDirectoryPassword || (authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated && _connHandler._fedAuthRequired)) {
Debug.Assert(!integratedSecurity, "The legacy Integrated Security connection string option cannot be true when using Active Directory Authentication Library for SQL Server Based workflows.");
LoadADALLibrary();
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.Connect|SEC> Active directory authentication.Loaded Active Directory Authentication Library for SQL Server\n");
}
}
return;
}
internal void RemoveEncryption() {
Debug.Assert(_encryptionOption == EncryptionOptions.LOGIN, "Invalid encryption option state");
UInt32 error = 0;
// Remove SSL (Encryption) SNI provider since we only wanted to encrypt login.
error = SNINativeMethodWrapper.SNIRemoveProvider(_physicalStateObj.Handle, SNINativeMethodWrapper.ProviderEnum.SSL_PROV);
if (error != TdsEnums.SNI_SUCCESS) {
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
// create a new packet encryption changes the internal packet size Bug# 228403
try {} // EmptyTry/Finally to avoid FXCop violation
finally {
_physicalStateObj.ClearAllWritePackets();
}
}
internal void EnableMars() {
if (_fMARS) {
// Cache physical stateObj and connection.
_pMarsPhysicalConObj = _physicalStateObj;
UInt32 error = 0;
UInt32 info = 0;
// Add SMUX (MARS) SNI provider.
error = SNINativeMethodWrapper.SNIAddProvider(_pMarsPhysicalConObj.Handle, SNINativeMethodWrapper.ProviderEnum.SMUX_PROV, ref info);
if (error != TdsEnums.SNI_SUCCESS) {
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
// HACK HACK HACK - for Async only
// Have to post read to intialize MARS - will get callback on this when connection goes
// down or is closed.
IntPtr temp = IntPtr.Zero;
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
_pMarsPhysicalConObj.IncrementPendingCallbacks();
error = SNINativeMethodWrapper.SNIReadAsync(_pMarsPhysicalConObj.Handle, ref temp);
if (temp != IntPtr.Zero) {
// Be sure to release packet, otherwise it will be leaked by native.
SNINativeMethodWrapper.SNIPacketRelease(temp);
}
}
Debug.Assert(IntPtr.Zero == temp, "unexpected syncReadPacket without corresponding SNIPacketRelease");
if (TdsEnums.SNI_SUCCESS_IO_PENDING != error) {
Debug.Assert(TdsEnums.SNI_SUCCESS != error, "Unexpected successfull read async on physical connection before enabling MARS!");
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
_physicalStateObj = CreateSession(); // Create and open default MARS stateObj and connection.
}
}
internal TdsParserStateObject CreateSession() {
TdsParserStateObject session = new TdsParserStateObject(this, (SNIHandle)_pMarsPhysicalConObj.Handle, true);
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.CreateSession|ADV> %d# created session %d\n", ObjectID, session.ObjectID);
}
return session;
}
internal TdsParserStateObject GetSession(object owner) {
TdsParserStateObject session = null;
//
if (MARSOn) {
session = _sessionPool.GetSession(owner);
Debug.Assert(!session._pendingData, "pending data on a pooled MARS session");
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.GetSession|ADV> %d# getting session %d from pool\n", ObjectID, session.ObjectID);
}
}
else {
session = _physicalStateObj;
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.GetSession|ADV> %d# getting physical session %d\n", ObjectID, session.ObjectID);
}
}
Debug.Assert(session._outputPacketNumber==1, "The packet number is expected to be 1");
return session;
}
internal void PutSession(TdsParserStateObject session) {
session.AssertStateIsClean();
if (MARSOn) {
// This will take care of disposing if the parser is closed
_sessionPool.PutSession(session);
}
else if ((_state == TdsParserState.Closed) || (_state == TdsParserState.Broken)) {
// Parser is closed\broken - dispose the stateObj
Debug.Assert(session == _physicalStateObj, "MARS is off, but session to close is not the _physicalStateObj");
_physicalStateObj.SniContext = SniContext.Snix_Close;
#if DEBUG
_physicalStateObj.InvalidateDebugOnlyCopyOfSniContext();
#endif
_physicalStateObj.Dispose();
}
else {
// Non-MARS, and session is ok - remove its owner
_physicalStateObj.Owner = null;
}
}
// This is called from a ThreadAbort - ensure that it can be run from a CER Catch
internal void BestEffortCleanup() {
_state = TdsParserState.Broken;
var stateObj = _physicalStateObj;
if (stateObj != null) {
var stateObjHandle = stateObj.Handle;
if (stateObjHandle != null) {
stateObjHandle.Dispose();
}
}
if (_fMARS) {
var sessionPool = _sessionPool;
if (sessionPool != null) {
sessionPool.BestEffortCleanup();
}
var marsStateObj = _pMarsPhysicalConObj;
if (marsStateObj != null) {
var marsStateObjHandle = marsStateObj.Handle;
if (marsStateObjHandle != null) {
marsStateObjHandle.Dispose();
}
}
}
}
private void SendPreLoginHandshake(byte[] instanceName, bool encrypt) {
// PreLoginHandshake buffer consists of:
// 1) Standard header, with type = MT_PRELOGIN
// 2) Consecutive 5 bytes for each option, (1 byte length, 2 byte offset, 2 byte payload length)
// 3) Consecutive data blocks for each option
// NOTE: packet data needs to be big endian - not the standard little endian used by
// the rest of the parser.
_physicalStateObj._outputMessageType = TdsEnums.MT_PRELOGIN;
// Initialize option offset into payload buffer
// 5 bytes for each option (1 byte length, 2 byte offset, 2 byte payload length)
int offset = (int)PreLoginOptions.NUMOPT * 5 + 1;
byte[] payload = new byte[(int)PreLoginOptions.NUMOPT * 5 + TdsEnums.MAX_PRELOGIN_PAYLOAD_LENGTH];
int payloadLength = 0;
//
for (int option = (int)PreLoginOptions.VERSION; option < (int)PreLoginOptions.NUMOPT; option++) {
int optionDataSize = 0;
// Fill in the option
_physicalStateObj.WriteByte((byte)option);
// Fill in the offset of the option data
_physicalStateObj.WriteByte((byte)((offset & 0xff00) >> 8)); // send upper order byte
_physicalStateObj.WriteByte((byte)(offset & 0x00ff)); // send lower order byte
switch (option) {
case (int)PreLoginOptions.VERSION:
Version systemDataVersion = ADP.GetAssemblyVersion();
// Major and minor
payload[payloadLength++] = (byte)(systemDataVersion.Major & 0xff);
payload[payloadLength++] = (byte)(systemDataVersion.Minor & 0xff);
// Build (Big Endian)
payload[payloadLength++] = (byte)((systemDataVersion.Build & 0xff00) >> 8);
payload[payloadLength++] = (byte)(systemDataVersion.Build & 0xff);
// Sub-build (Little Endian)
payload[payloadLength++] = (byte)(systemDataVersion.Revision & 0xff);
payload[payloadLength++] = (byte)((systemDataVersion.Revision & 0xff00) >> 8);
offset += 6;
optionDataSize = 6;
break;
case (int)PreLoginOptions.ENCRYPT:
if (_encryptionOption == EncryptionOptions.NOT_SUP) {
// If OS doesn't support encryption, inform server not supported.
payload[payloadLength] = (byte)EncryptionOptions.NOT_SUP;
}
else {
// Else, inform server of user request.
if (encrypt) {
payload[payloadLength] = (byte)EncryptionOptions.ON;
_encryptionOption = EncryptionOptions.ON;
}
else {
payload[payloadLength] = (byte)EncryptionOptions.OFF;
_encryptionOption = EncryptionOptions.OFF;
}
}
payloadLength += 1;
offset += 1;
optionDataSize = 1;
break;
case (int)PreLoginOptions.INSTANCE:
int i = 0;
while (instanceName[i] != 0) {
payload[payloadLength] = instanceName[i];
payloadLength++;
i++;
}
payload[payloadLength] = 0; // null terminate
payloadLength++;
i++;
offset += i;
optionDataSize = i;
break;
case (int)PreLoginOptions.THREADID:
Int32 threadID = TdsParserStaticMethods.GetCurrentThreadIdForTdsLoginOnly();
payload[payloadLength++] = (byte)((0xff000000 & threadID) >> 24);
payload[payloadLength++] = (byte)((0x00ff0000 & threadID) >> 16);
payload[payloadLength++] = (byte)((0x0000ff00 & threadID) >> 8);
payload[payloadLength++] = (byte)(0x000000ff & threadID);
offset += 4;
optionDataSize = 4;
break;
case (int)PreLoginOptions.MARS:
payload[payloadLength++] = (byte)(_fMARS ? 1 : 0);
offset += 1;
optionDataSize += 1;
break;
case (int)PreLoginOptions.TRACEID:
byte[] connectionIdBytes = _connHandler._clientConnectionId.ToByteArray();
Debug.Assert(GUID_SIZE == connectionIdBytes.Length);
Buffer.BlockCopy(connectionIdBytes, 0, payload, payloadLength, GUID_SIZE);
payloadLength += GUID_SIZE;
offset += GUID_SIZE;
optionDataSize = GUID_SIZE;
ActivityCorrelator.ActivityId actId = ActivityCorrelator.Next();
connectionIdBytes = actId.Id.ToByteArray();
Buffer.BlockCopy(connectionIdBytes, 0, payload, payloadLength, GUID_SIZE);
payloadLength += GUID_SIZE;
payload[payloadLength++] = (byte)(0x000000ff & actId.Sequence);
payload[payloadLength++] = (byte)((0x0000ff00 & actId.Sequence) >> 8);
payload[payloadLength++] = (byte)((0x00ff0000 & actId.Sequence) >> 16);
payload[payloadLength++] = (byte)((0xff000000 & actId.Sequence) >> 24);
int actIdSize = GUID_SIZE + sizeof(UInt32);
offset += actIdSize;
optionDataSize += actIdSize;
Bid.Trace("<sc.TdsParser.SendPreLoginHandshake|INFO> ClientConnectionID %ls, ActivityID %ls\n", _connHandler._clientConnectionId.ToString(), actId.ToString());
break;
case (int)PreLoginOptions.FEDAUTHREQUIRED:
payload[payloadLength++] = 0x01;
offset += 1;
optionDataSize += 1;
break;
default:
Debug.Assert(false, "UNKNOWN option in SendPreLoginHandshake");
break;
}
// Write data length
_physicalStateObj.WriteByte((byte)((optionDataSize & 0xff00) >> 8));
_physicalStateObj.WriteByte((byte)(optionDataSize & 0x00ff));
}
// Write out last option - to let server know the second part of packet completed
_physicalStateObj.WriteByte((byte)PreLoginOptions.LASTOPT);
// Write out payload
_physicalStateObj.WriteByteArray(payload, payloadLength, 0);
// Flush packet
_physicalStateObj.WritePacket(TdsEnums.HARDFLUSH);
}
private PreLoginHandshakeStatus ConsumePreLoginHandshake(SqlAuthenticationMethod authType, bool encrypt, bool trustServerCert, bool integratedSecurity, out bool marsCapable, out bool fedAuthRequired) {
// Assign default values
marsCapable = _fMARS;
fedAuthRequired = false;
bool isYukonOrLater = false;
Debug.Assert(_physicalStateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
bool result = _physicalStateObj.TryReadNetworkPacket();
if (!result) { throw SQL.SynchronousCallMayNotPend(); }
if (_physicalStateObj._inBytesRead == 0) {
// If the server did not respond then something has gone wrong and we need to close the connection
_physicalStateObj.AddError(new SqlError(0, (byte)0x00, TdsEnums.FATAL_ERROR_CLASS, _server, SQLMessage.PreloginError(), "", 0));
_physicalStateObj.Dispose();
ThrowExceptionAndWarning(_physicalStateObj);
}
if (!_physicalStateObj.TryProcessHeader()) { throw SQL.SynchronousCallMayNotPend(); }
if (_physicalStateObj._inBytesPacket > TdsEnums.MAX_PACKET_SIZE || _physicalStateObj._inBytesPacket <= 0) {
throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream);
}
byte[] payload = new byte[_physicalStateObj._inBytesPacket];
Debug.Assert(_physicalStateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
result = _physicalStateObj.TryReadByteArray(payload, 0, payload.Length);
if (!result) { throw SQL.SynchronousCallMayNotPend(); }
if (payload[0] == 0xaa) {
// If the first byte is 0xAA, we are connecting to a 6.5 or earlier server, which
// is not supported. SQL BU DT 296425
throw SQL.InvalidSQLServerVersionUnknown();
}
int offset = 0;
int payloadOffset = 0;
int payloadLength = 0;
int option = payload[offset++];
while (option != (byte)PreLoginOptions.LASTOPT) {
switch (option) {
case (int)PreLoginOptions.VERSION:
payloadOffset = payload[offset++] << 8 | payload[offset++];
payloadLength = payload[offset++] << 8 | payload[offset++];
byte majorVersion = payload[payloadOffset];
byte minorVersion = payload[payloadOffset + 1];
int level = (payload[payloadOffset + 2] << 8) |
payload[payloadOffset + 3];
isYukonOrLater = majorVersion >= 9;
if (!isYukonOrLater) {
marsCapable = false; // If pre-Yukon, MARS not supported.
}
break;
case (int)PreLoginOptions.ENCRYPT:
payloadOffset = payload[offset++] << 8 | payload[offset++];
payloadLength = payload[offset++] << 8 | payload[offset++];
EncryptionOptions serverOption = (EncryptionOptions)payload[payloadOffset];
/* internal enum EncryptionOptions {
OFF,
ON,
NOT_SUP,
REQ,
LOGIN
} */
switch (_encryptionOption) {
case (EncryptionOptions.ON):
if (serverOption == EncryptionOptions.NOT_SUP) {
_physicalStateObj.AddError(new SqlError(TdsEnums.ENCRYPTION_NOT_SUPPORTED, (byte)0x00, TdsEnums.FATAL_ERROR_CLASS, _server, SQLMessage.EncryptionNotSupportedByServer(), "", 0));
_physicalStateObj.Dispose();
ThrowExceptionAndWarning(_physicalStateObj);
}
break;
case (EncryptionOptions.OFF):
if (serverOption == EncryptionOptions.OFF) {
// Only encrypt login.
_encryptionOption = EncryptionOptions.LOGIN;
}
else if (serverOption == EncryptionOptions.REQ) {
// Encrypt all.
_encryptionOption = EncryptionOptions.ON;
}
break;
case (EncryptionOptions.NOT_SUP):
if (serverOption == EncryptionOptions.REQ) {
_physicalStateObj.AddError(new SqlError(TdsEnums.ENCRYPTION_NOT_SUPPORTED, (byte)0x00, TdsEnums.FATAL_ERROR_CLASS, _server, SQLMessage.EncryptionNotSupportedByClient(), "", 0));
_physicalStateObj.Dispose();
ThrowExceptionAndWarning(_physicalStateObj);
}
break;
default:
Debug.Assert(false, "Invalid client encryption option detected");
break;
}
if (_encryptionOption == EncryptionOptions.ON ||
_encryptionOption == EncryptionOptions.LOGIN) {
UInt32 error = 0;
// If we're using legacy server certificate validation behavior (Authentication keyword not provided and not using access token), then validate if
// Encrypt=true and Trust Sever Certificate = false.
// If using Authentication keyword or access token, validate if Trust Server Certificate=false.
bool shouldValidateServerCert = (encrypt && !trustServerCert) || ((authType != SqlAuthenticationMethod.NotSpecified || _connHandler._accessTokenInBytes != null) && !trustServerCert);
UInt32 info = (shouldValidateServerCert ? TdsEnums.SNI_SSL_VALIDATE_CERTIFICATE : 0)
| (isYukonOrLater ? TdsEnums.SNI_SSL_USE_SCHANNEL_CACHE : 0);
if (encrypt && !integratedSecurity) {
// optimization: in case of SQL Authentication and encryption, set SNI_SSL_IGNORE_CHANNEL_BINDINGS to let SNI
// know that it does not need to allocate/retrieve the Channel Bindings from the SSL context.
info |= TdsEnums.SNI_SSL_IGNORE_CHANNEL_BINDINGS;
}
// Add SSL (Encryption) SNI provider.
error = SNINativeMethodWrapper.SNIAddProvider(_physicalStateObj.Handle, SNINativeMethodWrapper.ProviderEnum.SSL_PROV, ref info);
if (error != TdsEnums.SNI_SUCCESS) {
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
// in the case where an async connection is made, encryption is used and Windows Authentication is used,
// wait for SSL handshake to complete, so that the SSL context is fully negotiated before we try to use its
// Channel Bindings as part of the Windows Authentication context build (SSL handshake must complete
// before calling SNISecGenClientContext).
error = SNINativeMethodWrapper.SNIWaitForSSLHandshakeToComplete(_physicalStateObj.Handle, _physicalStateObj.GetTimeoutRemaining());
if (error != TdsEnums.SNI_SUCCESS)
{
_physicalStateObj.AddError(ProcessSNIError(_physicalStateObj));
ThrowExceptionAndWarning(_physicalStateObj);
}
// create a new packet encryption changes the internal packet size Bug# 228403
try {} // EmptyTry/Finally to avoid FXCop violation
finally {
_physicalStateObj.ClearAllWritePackets();
}
}
break;
case (int)PreLoginOptions.INSTANCE:
payloadOffset = payload[offset++] << 8 | payload[offset++];
payloadLength = payload[offset++] << 8 | payload[offset++];
byte ERROR_INST = 0x1;
byte instanceResult = payload[payloadOffset];
if (instanceResult == ERROR_INST) {
// Check if server says ERROR_INST. That either means the cached info
// we used to connect is not valid or we connected to a named instance
// listening on default params.
return PreLoginHandshakeStatus.InstanceFailure;
}
break;
case (int)PreLoginOptions.THREADID:
// DO NOTHING FOR THREADID
offset += 4;
break;
case (int)PreLoginOptions.MARS:
payloadOffset = payload[offset++] << 8 | payload[offset++];
payloadLength = payload[offset++] << 8 | payload[offset++];
marsCapable = (payload[payloadOffset] == 0 ? false : true);
Debug.Assert(payload[payloadOffset] == 0 || payload[payloadOffset] == 1, "Value for Mars PreLoginHandshake option not equal to 1 or 0!");
break;
case (int)PreLoginOptions.TRACEID:
// DO NOTHING FOR TRACEID
offset += 4;
break;
case (int)PreLoginOptions.FEDAUTHREQUIRED:
payloadOffset = payload[offset++] << 8 | payload[offset++];
payloadLength = payload[offset++] << 8 | payload[offset++];
// Only 0x00 and 0x01 are accepted values from the server.
if (payload[payloadOffset] != 0x00 && payload[payloadOffset] != 0x01) {
Bid.Trace("<sc.TdsParser.ConsumePreLoginHandshake|ERR> %d#, Server sent an unexpected value for FedAuthRequired PreLogin Option. Value was %d.\n", ObjectID, (int)payload[payloadOffset]);
throw SQL.ParsingErrorValue(ParsingErrorState.FedAuthRequiredPreLoginResponseInvalidValue, (int)payload[payloadOffset]);
}
// We must NOT use the response for the FEDAUTHREQUIRED PreLogin option, if the connection string option
// was not using the new Authentication keyword or in other words, if Authentication=NotSpecified
// Or AccessToken is not null, mean token based authentication is used.
if ((_connHandler.ConnectionOptions != null
&& _connHandler.ConnectionOptions.Authentication != SqlAuthenticationMethod.NotSpecified)
|| _connHandler._accessTokenInBytes != null)
{
fedAuthRequired = payload[payloadOffset] == 0x01 ? true : false;
}
break;
default:
Debug.Assert(false, "UNKNOWN option in ConsumePreLoginHandshake, option:" + option);
// DO NOTHING FOR THESE UNKNOWN OPTIONS
offset += 4;
break;
}
if (offset < payload.Length) {
option = payload[offset++];
}
else {
break;
}
}
return PreLoginHandshakeStatus.Successful;
}
internal void Deactivate(bool connectionIsDoomed) {
// Called when the connection that owns us is deactivated.
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.Deactivate|ADV> %d# deactivating\n", ObjectID);
}
if (Bid.IsOn(Bid.ApiGroup.StateDump)) {
Bid.Trace("<sc.TdsParser.Deactivate|STATE> %d#, %ls\n", ObjectID, TraceString());
}
if (MARSOn) {
_sessionPool.Deactivate();
}
Debug.Assert(connectionIsDoomed || null == _pendingTransaction, "pending transaction at disconnect?");
if (!connectionIsDoomed && null != _physicalStateObj) {
if (_physicalStateObj._pendingData) {
DrainData(_physicalStateObj);
}
if (_physicalStateObj.HasOpenResult) { // SQL BU DT 383773 - need to decrement openResultCount for all pending operations.
_physicalStateObj.DecrementOpenResultCount();
}
}
// Any active, non-distributed transaction must be rolled back. We
// need to wait for distributed transactions to be completed by the
// transaction manager -- we don't want to automatically roll them
// back.
//
// Note that when there is a transaction delegated to this connection,
// we will defer the deactivation of this connection until the
// transaction manager completes the transaction.
SqlInternalTransaction currentTransaction = CurrentTransaction;
if (null != currentTransaction && currentTransaction.HasParentTransaction) {
currentTransaction.CloseFromConnection();
Debug.Assert(null == CurrentTransaction, "rollback didn't clear current transaction?");
}
Statistics = null; // must come after CleanWire or we won't count the stuff that happens there...
}
// Used to close the connection and then free the memory allocated for the netlib connection.
internal void Disconnect() {
if (null != _sessionPool) {
// MARSOn may be true, but _sessionPool not yet created
_sessionPool.Dispose();
}
// Can close the connection if its open or broken
if (_state != TdsParserState.Closed) {
//benign assert - the user could close the connection before consuming all the data
//Debug.Assert(_physicalStateObj._inBytesUsed == _physicalStateObj._inBytesRead && _physicalStateObj._outBytesUsed == _physicalStateObj._inputHeaderLen, "TDSParser closed with data not fully sent or consumed.");
_state = TdsParserState.Closed;
try {
// If the _physicalStateObj has an owner, we will delay the disposal until the owner is finished with it
if (!_physicalStateObj.HasOwner) {
_physicalStateObj.SniContext = SniContext.Snix_Close;
#if DEBUG
_physicalStateObj.InvalidateDebugOnlyCopyOfSniContext();
#endif
_physicalStateObj.Dispose();
}
else {
// Remove the "initial" callback (this will allow the stateObj to be GC collected if need be)
_physicalStateObj.DecrementPendingCallbacks(false);
}
// Not allocated until MARS is actually enabled in SNI.
if (null != _pMarsPhysicalConObj) {
_pMarsPhysicalConObj.Dispose();
}
}
finally {
_pMarsPhysicalConObj = null;
}
}
}
// Fires a single InfoMessageEvent
private void FireInfoMessageEvent(SqlConnection connection, TdsParserStateObject stateObj, SqlError error) {
string serverVersion = null;
Debug.Assert(connection != null && _connHandler.Connection == connection);
if (_state == TdsParserState.OpenLoggedIn) {
serverVersion = _connHandler.ServerVersion;
}
SqlErrorCollection sqlErs = new SqlErrorCollection();
sqlErs.Add(error);
SqlException exc = SqlException.CreateException(sqlErs, serverVersion, _connHandler);
bool notified;
connection.OnInfoMessage(new SqlInfoMessageEventArgs(exc), out notified);
if (notified) {
// observable side-effects, no retry
stateObj._syncOverAsync = true;
}
return;
}
internal void DisconnectTransaction(SqlInternalTransaction internalTransaction) {
Debug.Assert(_currentTransaction != null && _currentTransaction == internalTransaction, "disconnecting different transaction");
if (_currentTransaction != null && _currentTransaction == internalTransaction) {
_currentTransaction = null;
}
}
internal void RollbackOrphanedAPITransactions() {
// Any active, non-distributed transaction must be rolled back.
SqlInternalTransaction currentTransaction = CurrentTransaction;
if (null != currentTransaction && currentTransaction.HasParentTransaction && currentTransaction.IsOrphaned) {
currentTransaction.CloseFromConnection();
Debug.Assert(null == CurrentTransaction, "rollback didn't clear current transaction?");
}
}
internal void ThrowExceptionAndWarning(TdsParserStateObject stateObj, bool callerHasConnectionLock = false, bool asyncClose = false) {
Debug.Assert(!callerHasConnectionLock || _connHandler._parserLock.ThreadMayHaveLock(), "Caller claims to have lock, but connection lock is not taken");
SqlException exception = null;
bool breakConnection;
// This function should only be called when there was an error or warning. If there aren't any
// errors, the handler will be called for the warning(s). If there was an error, the warning(s) will
// be copied to the end of the error collection so that the user may see all the errors and also the
// warnings that occurred.
// can be deleted)
SqlErrorCollection temp = stateObj.GetFullErrorAndWarningCollection(out breakConnection);
Debug.Assert(temp.Count > 0, "TdsParser::ThrowExceptionAndWarning called with no exceptions or warnings!");
if (temp.Count == 0)
{
Bid.Trace("<sc.TdsParser.ThrowExceptionAndWarning|ERR> Potential multi-threaded misuse of connection, unexpectedly empty warnings/errors under lock %d#\n", ObjectID);
}
Debug.Assert(_connHandler != null, "TdsParser::ThrowExceptionAndWarning called with null connectionHandler!");
// Don't break the connection if it is already closed
breakConnection &= (TdsParserState.Closed != _state);
if (breakConnection) {
if ((_state == TdsParserState.OpenNotLoggedIn) && (_connHandler.ConnectionOptions.TransparentNetworkIPResolution || _connHandler.ConnectionOptions.MultiSubnetFailover || _loginWithFailover) && (temp.Count == 1) && ((temp[0].Number == TdsEnums.TIMEOUT_EXPIRED) || (temp[0].Number == TdsEnums.SNI_WAIT_TIMEOUT))) {
// DevDiv2 Bug 459546: With "MultiSubnetFailover=yes" in the Connection String, SQLClient incorrectly throws a Timeout using shorter time slice (3-4 seconds), not honoring the actual 'Connect Timeout'
// http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/459546
// For Multisubnet Failover we slice the timeout to make reconnecting faster (with the assumption that the server will not failover instantaneously)
// However, when timeout occurs we need to not doom the internal connection and also to mark the TdsParser as closed such that the login will be will retried
breakConnection = false;
Disconnect();
}
else {
_state = TdsParserState.Broken;
}
}
Debug.Assert(temp != null, "TdsParser::ThrowExceptionAndWarning: 0 errors in collection");
if (temp != null && temp.Count > 0) {
// Construct the exception now that we've collected all the errors
string serverVersion = null;
if (_state == TdsParserState.OpenLoggedIn) {
serverVersion = _connHandler.ServerVersion;
}
exception = SqlException.CreateException(temp, serverVersion, _connHandler);
if (exception.Procedure == TdsEnums.INIT_ADAL_PACKAGE || exception.Procedure == TdsEnums.INIT_SSPI_PACKAGE) {
exception._doNotReconnect = true;
}
}
// call OnError outside of _ErrorCollectionLock to avoid deadlock
if (exception != null) {
if (breakConnection) {
// report exception to pending async operation
// before OnConnectionClosed overrides the exception
// due to connection close notification through references
var taskSource = stateObj._networkPacketTaskSource;
if (taskSource != null) {
taskSource.TrySetException(ADP.ExceptionWithStackTrace(exception));
}
}
if (asyncClose) {
// Wait until we have the parser lock, then try to close
var connHandler = _connHandler;
Action<Action> wrapCloseAction = closeAction => {
Task.Factory.StartNew(() => {
connHandler._parserLock.Wait(canReleaseFromAnyThread: false);
connHandler.ThreadHasParserLockForClose = true;
try {
closeAction();
}
finally {
connHandler.ThreadHasParserLockForClose = false;
connHandler._parserLock.Release();
}
});
};
_connHandler.OnError(exception, breakConnection, wrapCloseAction);
}
else {
// Let close know that we already have the _parserLock
bool threadAlreadyHadParserLockForClose = _connHandler.ThreadHasParserLockForClose;
if (callerHasConnectionLock) {
_connHandler.ThreadHasParserLockForClose = true;
}
try {
// the following handler will throw an exception or generate a warning event
_connHandler.OnError(exception, breakConnection);
}
finally {
if (callerHasConnectionLock) {
_connHandler.ThreadHasParserLockForClose = threadAlreadyHadParserLockForClose;
}
}
}
}
}
internal SqlError ProcessSNIError(TdsParserStateObject stateObj) {
#if DEBUG
// There is an exception here for MARS as its possible that another thread has closed the connection just as we see an error
Debug.Assert(SniContext.Undefined!=stateObj.DebugOnlyCopyOfSniContext || ((_fMARS) && ((_state == TdsParserState.Closed) || (_state == TdsParserState.Broken))), "SniContext must not be None");
#endif
SNINativeMethodWrapper.SNI_Error sniError = new SNINativeMethodWrapper.SNI_Error();
SNINativeMethodWrapper.SNIGetLastError(sniError);
if (sniError.sniError != 0) {
// handle special SNI error codes that are converted into exception which is not a SqlException.
switch (sniError.sniError) {
case (int)SNINativeMethodWrapper.SniSpecialErrors.MultiSubnetFailoverWithMoreThan64IPs:
// Connecting with the MultiSubnetFailover connection option to a SQL Server instance configured with more than 64 IP addresses is not supported.
throw SQL.MultiSubnetFailoverWithMoreThan64IPs();
case (int)SNINativeMethodWrapper.SniSpecialErrors.MultiSubnetFailoverWithInstanceSpecified:
// Connecting to a named SQL Server instance using the MultiSubnetFailover connection option is not supported.
throw SQL.MultiSubnetFailoverWithInstanceSpecified();
case (int)SNINativeMethodWrapper.SniSpecialErrors.MultiSubnetFailoverWithNonTcpProtocol:
// Connecting to a SQL Server instance using the MultiSubnetFailover connection option is only supported when using the TCP protocol.
throw SQL.MultiSubnetFailoverWithNonTcpProtocol();
// continue building SqlError instance
}
}
// error.errorMessage is null terminated with garbage beyond that, since fixed length
string errorMessage;
int MessageLength = Array.IndexOf(sniError.errorMessage, '\0');
if (MessageLength == -1) {
errorMessage = String.Empty; // If we don't see the expected null return nothing.
} else {
errorMessage = new String(sniError.errorMessage, 0, MessageLength);
}
// Format SNI errors and add Context Information
//
// General syntax is:
// <sqlclient message>
// (provider:<SNIx provider>, error: <SNIx error code> - <SNIx error message>)
//
// errorMessage | sniError |
// -------------------------------------------
// ==null | x | must never happen
// !=null | != 0 | retrieve corresponding errorMessage from resources
// !=null | == 0 | replace text left of errorMessage
//
Debug.Assert(!ADP.IsEmpty(errorMessage),"Empty error message received from SNI");
string sqlContextInfo = Res.GetString(Enum.GetName(typeof(SniContext), stateObj.SniContext));
string providerRid = String.Format((IFormatProvider)null,"SNI_PN{0}", (int)sniError.provider);
string providerName = Res.GetString(providerRid);
Debug.Assert(!ADP.IsEmpty(providerName), String.Format((IFormatProvider)null,"invalid providerResourceId '{0}'", providerRid));
uint win32ErrorCode = sniError.nativeError;
if (sniError.sniError == 0) {
// Provider error. The message from provider is preceeded with non-localizable info from SNI
// strip provider info from SNI
//
int iColon = errorMessage.IndexOf(':');
Debug.Assert(0<=iColon, "':' character missing in sni errorMessage");
Debug.Assert(errorMessage.Length>iColon+1 && errorMessage[iColon+1]==' ', "Expecting a space after the ':' character");
// extract the message excluding the colon and trailing cr/lf chars
if (0<=iColon) {
int len = errorMessage.Length;
len -=2; // exclude "\r\n" sequence
iColon+=2; // skip over ": " sequence
len-=iColon;
/*
The error message should come back in the following format: "TCP Provider: MESSAGE TEXT"
Fix Bug 370686, if the message is recieved on a Win9x OS, the error message will not contain MESSAGE TEXT
per Bug: 269574. If we get a errormessage with no message text, just return the entire message otherwise
return just the message text.
*/
if (len > 0) {
errorMessage = errorMessage.Substring(iColon, len);
}
}
}
else {
// SNI error. Replace the entire message
//
errorMessage = SQL.GetSNIErrorMessage((int)sniError.sniError);
// If its a LocalDB error, then nativeError actually contains a LocalDB-specific error code, not a win32 error code
if (sniError.sniError == (int)SNINativeMethodWrapper.SniSpecialErrors.LocalDBErrorCode) {
errorMessage += LocalDBAPI.GetLocalDBMessage((int)sniError.nativeError);
win32ErrorCode = 0;
}
}
errorMessage = String.Format((IFormatProvider)null, "{0} (provider: {1}, error: {2} - {3})",
sqlContextInfo, providerName, (int)sniError.sniError, errorMessage);
return new SqlError((int)sniError.nativeError, 0x00, TdsEnums.FATAL_ERROR_CLASS,
_server, errorMessage, sniError.function, (int)sniError.lineNumber, win32ErrorCode);
}
internal void CheckResetConnection(TdsParserStateObject stateObj) {
if (_fResetConnection && !stateObj._fResetConnectionSent) {
Debug.Assert(stateObj._outputPacketNumber == 1 || stateObj._outputPacketNumber == 2, "In ResetConnection logic unexpectedly!");
try {
if (_fMARS && !stateObj._fResetEventOwned) {
// If using Async & MARS and we do not own ResetEvent - grab it. We need to not grab lock here
// for case where multiple packets are sent to server from one execute.
stateObj._fResetEventOwned = _resetConnectionEvent.WaitOne(stateObj.GetTimeoutRemaining(), false);
if (stateObj._fResetEventOwned) {
if (stateObj.TimeoutHasExpired) {
// We didn't timeout on the WaitOne, but we timed out by the time we decremented stateObj._timeRemaining.
stateObj._fResetEventOwned = !_resetConnectionEvent.Set();
stateObj.TimeoutTime = 0;
}
}
if (!stateObj._fResetEventOwned) {
// We timed out waiting for ResetEvent. Throw timeout exception and reset
// the buffer. Nothing else to do since we did not actually send anything
// to the server.
stateObj.ResetBuffer();
Debug.Assert(_connHandler != null, "SqlConnectionInternalTds handler can not be null at this point.");
stateObj.AddError(new SqlError(TdsEnums.TIMEOUT_EXPIRED, (byte)0x00, TdsEnums.MIN_ERROR_CLASS, _server, _connHandler.TimeoutErrorInternal.GetErrorMessage(), "", 0, TdsEnums.SNI_WAIT_TIMEOUT));
Debug.Assert(_connHandler._parserLock.ThreadMayHaveLock(), "Thread is writing without taking the connection lock");
ThrowExceptionAndWarning(stateObj, callerHasConnectionLock: true);
}
}
if (_fResetConnection) {
// Check again to see if we need to send reset.
Debug.Assert(!stateObj._fResetConnectionSent, "Unexpected state for sending reset connection");
Debug.Assert(_isShiloh, "TdsParser.cs: Error! _fResetConnection true when not going against Shiloh or later!");
if (_fPreserveTransaction) {
// if we are reseting, set bit in header by or'ing with other value
stateObj._outBuff[1] = (Byte)(stateObj._outBuff[1] | TdsEnums.ST_RESET_CONNECTION_PRESERVE_TRANSACTION);
}
else {
// if we are reseting, set bit in header by or'ing with other value
stateObj._outBuff[1] = (Byte)(stateObj._outBuff[1] | TdsEnums.ST_RESET_CONNECTION);
}
if (!_fMARS) {
_fResetConnection = false; // If not MARS, can turn off flag now.
_fPreserveTransaction = false;
}
else {
stateObj._fResetConnectionSent = true; // Otherwise set flag so we don't resend on multiple packet execute.
}
}
else if (_fMARS && stateObj._fResetEventOwned) {
Debug.Assert(!stateObj._fResetConnectionSent, "Unexpected state on WritePacket ResetConnection");
// Otherwise if Yukon and we grabbed the event, free it. Another execute grabbed the event and
// took care of sending the reset.
stateObj._fResetEventOwned = !_resetConnectionEvent.Set();
Debug.Assert(!stateObj._fResetEventOwned, "Invalid AutoResetEvent state!");
}
}
catch (Exception) {
if (_fMARS && stateObj._fResetEventOwned) {
// If exception thrown, and we are on Yukon and own the event, release it!
stateObj._fResetConnectionSent = false;
stateObj._fResetEventOwned = !_resetConnectionEvent.Set();
Debug.Assert(!stateObj._fResetEventOwned, "Invalid AutoResetEvent state!");
}
throw;
}
}
#if DEBUG
else {
Debug.Assert(!_fResetConnection ||
(_fResetConnection && stateObj._fResetConnectionSent && stateObj._fResetEventOwned),
"Unexpected state on else ResetConnection block in WritePacket");
}
#endif
}
//
// Takes a 16 bit short and writes it.
//
internal byte[] SerializeShort(int v, TdsParserStateObject stateObj) {
if (null == stateObj._bShortBytes) {
stateObj._bShortBytes = new byte[2];
}
else {
Debug.Assert(2 == stateObj._bShortBytes.Length);
}
byte[] bytes = stateObj._bShortBytes;
int current = 0;
bytes[current++] = (byte)(v & 0xff);
bytes[current++] = (byte)((v >> 8) & 0xff);
return bytes;
}
internal void WriteShort(int v, TdsParserStateObject stateObj) {
ReliabilitySection.Assert("unreliable call to WriteShort"); // you need to setup for a thread abort somewhere before you call this method
if ((stateObj._outBytesUsed + 2) > stateObj._outBuff.Length) {
// if all of the short doesn't fit into the buffer
stateObj.WriteByte((byte)(v & 0xff));
stateObj.WriteByte((byte)((v >> 8) & 0xff));
}
else {
// all of the short fits into the buffer
stateObj._outBuff[stateObj._outBytesUsed] = (byte)(v & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 1] = (byte)((v >> 8) & 0xff);
stateObj._outBytesUsed += 2;
}
}
internal void WriteUnsignedShort(ushort us, TdsParserStateObject stateObj) {
WriteShort((short)us, stateObj);
}
//
// Takes a long and writes out an unsigned int
//
internal byte[] SerializeUnsignedInt(uint i, TdsParserStateObject stateObj) {
return SerializeInt((int)i, stateObj);
}
internal void WriteUnsignedInt(uint i, TdsParserStateObject stateObj) {
WriteInt((int)i, stateObj);
}
//
// Takes an int and writes it as an int.
//
internal byte[] SerializeInt(int v, TdsParserStateObject stateObj) {
if (null == stateObj._bIntBytes) {
stateObj._bIntBytes = new byte[4];
}
else {
Debug.Assert (4 == stateObj._bIntBytes.Length);
}
int current = 0;
byte[] bytes = stateObj._bIntBytes;
bytes[current++] = (byte)(v & 0xff);
bytes[current++] = (byte)((v >> 8) & 0xff);
bytes[current++] = (byte)((v >> 16) & 0xff);
bytes[current++] = (byte)((v >> 24) & 0xff);
return bytes;
}
internal void WriteInt(int v, TdsParserStateObject stateObj) {
ReliabilitySection.Assert("unreliable call to WriteInt"); // you need to setup for a thread abort somewhere before you call this method
if ((stateObj._outBytesUsed + 4) > stateObj._outBuff.Length) {
// if all of the int doesn't fit into the buffer
for (int shiftValue = 0; shiftValue < sizeof(int) * 8; shiftValue += 8) {
stateObj.WriteByte((byte)((v >> shiftValue) & 0xff));
}
}
else {
// all of the int fits into the buffer
// NOTE: We don't use a loop here for performance
stateObj._outBuff[stateObj._outBytesUsed] = (byte)(v & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 1] = (byte)((v >> 8) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 2] = (byte)((v >> 16) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 3] = (byte)((v >> 24) & 0xff);
stateObj._outBytesUsed += 4;
}
}
//
// Takes a float and writes it as a 32 bit float.
//
internal byte[] SerializeFloat(float v) {
if (Single.IsInfinity(v) || Single.IsNaN(v)) {
throw ADP.ParameterValueOutOfRange(v.ToString());
}
return BitConverter.GetBytes(v);
}
internal void WriteFloat(float v, TdsParserStateObject stateObj) {
byte[] bytes = BitConverter.GetBytes(v);
stateObj.WriteByteArray(bytes, bytes.Length, 0);
}
//
// Takes a long and writes it as a long.
//
internal byte[] SerializeLong(long v, TdsParserStateObject stateObj) {
int current = 0;
if (null == stateObj._bLongBytes) {
stateObj._bLongBytes = new byte[8];
}
byte[] bytes = stateObj._bLongBytes;
Debug.Assert (8 == bytes.Length, "Cached buffer has wrong size");
bytes[current++] = (byte)(v & 0xff);
bytes[current++] = (byte)((v >> 8) & 0xff);
bytes[current++] = (byte)((v >> 16) & 0xff);
bytes[current++] = (byte)((v >> 24) & 0xff);
bytes[current++] = (byte)((v >> 32) & 0xff);
bytes[current++] = (byte)((v >> 40) & 0xff);
bytes[current++] = (byte)((v >> 48) & 0xff);
bytes[current++] = (byte)((v >> 56) & 0xff);
return bytes;
}
internal void WriteLong(long v, TdsParserStateObject stateObj) {
ReliabilitySection.Assert("unreliable call to WriteLong"); // you need to setup for a thread abort somewhere before you call this method
if ((stateObj._outBytesUsed + 8) > stateObj._outBuff.Length) {
// if all of the long doesn't fit into the buffer
for (int shiftValue = 0; shiftValue < sizeof(long) * 8; shiftValue += 8) {
stateObj.WriteByte((byte)((v >> shiftValue) & 0xff));
}
}
else {
// all of the long fits into the buffer
// NOTE: We don't use a loop here for performance
stateObj._outBuff[stateObj._outBytesUsed] = (byte)(v & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 1] = (byte)((v >> 8) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 2] = (byte)((v >> 16) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 3] = (byte)((v >> 24) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 4] = (byte)((v >> 32) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 5] = (byte)((v >> 40) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 6] = (byte)((v >> 48) & 0xff);
stateObj._outBuff[stateObj._outBytesUsed + 7] = (byte)((v >> 56) & 0xff);
stateObj._outBytesUsed += 8;
}
}
//
// Takes a long and writes part of it
//
internal byte[] SerializePartialLong(long v, int length) {
Debug.Assert(length <= 8, "Length specified is longer than the size of a long");
Debug.Assert(length >= 0, "Length should not be negative");
byte[] bytes = new byte[length];
// all of the long fits into the buffer
for (int index = 0; index < length; index++) {
bytes[index] = (byte)((v >> (index * 8)) & 0xff);
}
return bytes;
}
internal void WritePartialLong(long v, int length, TdsParserStateObject stateObj) {
ReliabilitySection.Assert("unreliable call to WritePartialLong"); // you need to setup for a thread abort somewhere before you call this method
Debug.Assert(length <= 8, "Length specified is longer than the size of a long");
Debug.Assert(length >= 0, "Length should not be negative");
if ((stateObj._outBytesUsed + length) > stateObj._outBuff.Length) {
// if all of the long doesn't fit into the buffer
for (int shiftValue = 0; shiftValue < length * 8; shiftValue += 8) {
stateObj.WriteByte((byte)((v >> shiftValue) & 0xff));
}
}
else {
// all of the long fits into the buffer
for (int index = 0; index < length; index++) {
stateObj._outBuff[stateObj._outBytesUsed + index] = (byte)((v >> (index * 8)) & 0xff);
}
stateObj._outBytesUsed += length;
}
}
//
// Takes a ulong and writes it as a ulong.
//
internal void WriteUnsignedLong(ulong uv, TdsParserStateObject stateObj) {
WriteLong((long)uv, stateObj);
}
//
// Takes a double and writes it as a 64 bit double.
//
internal byte[] SerializeDouble(double v) {
if (Double.IsInfinity(v) || Double.IsNaN(v)) {
throw ADP.ParameterValueOutOfRange(v.ToString());
}
return BitConverter.GetBytes(v);
}
internal void WriteDouble(double v, TdsParserStateObject stateObj) {
byte[] bytes = BitConverter.GetBytes(v);
stateObj.WriteByteArray(bytes, bytes.Length, 0);
}
internal void PrepareResetConnection(bool preserveTransaction) {
// Set flag to reset connection upon next use - only for use on shiloh!
_fResetConnection = true;
_fPreserveTransaction = preserveTransaction;
}
internal bool RunReliably(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) {
RuntimeHelpers.PrepareConstrainedRegions();
try {
#if DEBUG
TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
RuntimeHelpers.PrepareConstrainedRegions();
try {
tdsReliabilitySection.Start();
#endif //DEBUG
return Run(runBehavior, cmdHandler, dataStream, bulkCopyHandler, stateObj);
#if DEBUG
}
finally {
tdsReliabilitySection.Stop();
}
#endif //DEBUG
}
catch (OutOfMemoryException) {
_connHandler.DoomThisConnection();
throw;
}
catch (StackOverflowException) {
_connHandler.DoomThisConnection();
throw;
}
catch (ThreadAbortException) {
_connHandler.DoomThisConnection();
throw;
}
}
internal bool Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) {
bool syncOverAsync = stateObj._syncOverAsync;
try
{
stateObj._syncOverAsync = true;
bool dataReady;
bool result = TryRun(runBehavior, cmdHandler, dataStream, bulkCopyHandler, stateObj, out dataReady);
Debug.Assert(result == true, "Should never return false when _syncOverAsync is set");
return dataReady;
}
finally
{
stateObj._syncOverAsync = syncOverAsync;
}
}
/// <summary>
/// Checks if the given token is a valid TDS token
/// </summary>
/// <param name="token">Token to check</param>
/// <returns>True if the token is a valid TDS token, otherwise false</returns>
internal static bool IsValidTdsToken(byte token) {
return (
token == TdsEnums.SQLERROR ||
token == TdsEnums.SQLINFO ||
token == TdsEnums.SQLLOGINACK ||
token == TdsEnums.SQLENVCHANGE ||
token == TdsEnums.SQLRETURNVALUE ||
token == TdsEnums.SQLRETURNSTATUS ||
token == TdsEnums.SQLCOLNAME ||
token == TdsEnums.SQLCOLFMT ||
token == TdsEnums.SQLCOLMETADATA ||
token == TdsEnums.SQLALTMETADATA ||
token == TdsEnums.SQLTABNAME ||
token == TdsEnums.SQLCOLINFO ||
token == TdsEnums.SQLORDER ||
token == TdsEnums.SQLALTROW ||
token == TdsEnums.SQLROW ||
token == TdsEnums.SQLNBCROW ||
token == TdsEnums.SQLDONE ||
token == TdsEnums.SQLDONEPROC ||
token == TdsEnums.SQLDONEINPROC ||
token == TdsEnums.SQLROWCRC ||
token == TdsEnums.SQLSECLEVEL ||
token == TdsEnums.SQLPROCID ||
token == TdsEnums.SQLOFFSET ||
token == TdsEnums.SQLSSPI ||
token == TdsEnums.SQLFEATUREEXTACK ||
token == TdsEnums.SQLSESSIONSTATE ||
token == TdsEnums.SQLFEDAUTHINFO);
}
// Main parse loop for the top-level tds tokens, calls back into the I*Handler interfaces
internal bool TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, out bool dataReady) {
ReliabilitySection.Assert("unreliable call to Run"); // you need to setup for a thread abort somewhere before you call this method
Debug.Assert((SniContext.Undefined != stateObj.SniContext) && // SniContext must not be Undefined
((stateObj._attentionSent) || ((SniContext.Snix_Execute != stateObj.SniContext) && (SniContext.Snix_SendRows != stateObj.SniContext))), // SniContext should not be Execute or SendRows unless attention was sent (and, therefore, we are looking for an ACK)
String.Format("Unexpected SniContext on call to TryRun; SniContext={0}", stateObj.SniContext));
if (TdsParserState.Broken == State || TdsParserState.Closed == State ) {
dataReady = true;
return true; // Just in case this is called in a loop, expecting data to be returned.
}
dataReady = false;
do {
// If there is data ready, but we didn't exit the loop, then something is wrong
Debug.Assert(!dataReady, "dataReady not expected - did we forget to skip the row?");
if (stateObj._internalTimeout) {
runBehavior = RunBehavior.Attention;
}
if (TdsParserState.Broken == State || TdsParserState.Closed == State)
break; // jump out of the loop if the state is already broken or closed.
if (!stateObj._accumulateInfoEvents && (stateObj._pendingInfoEvents != null)) {
if (RunBehavior.Clean != (RunBehavior.Clean & runBehavior)) {
SqlConnection connection = null;
if (_connHandler != null)
connection = _connHandler.Connection; // SqlInternalConnection holds the user connection object as a weak ref
// We are omitting checks for error.Class in the code below (see processing of INFO) since we know (and assert) that error class
// error.Class < TdsEnums.MIN_ERROR_CLASS for info message.
// Also we know that TdsEnums.MIN_ERROR_CLASS<TdsEnums.MAX_USER_CORRECTABLE_ERROR_CLASS
if ((connection != null) && connection.FireInfoMessageEventOnUserErrors)
{
foreach (SqlError error in stateObj._pendingInfoEvents)
FireInfoMessageEvent(connection, stateObj, error);
}
else
foreach (SqlError error in stateObj._pendingInfoEvents)
stateObj.AddWarning(error);
}
stateObj._pendingInfoEvents=null;
}
byte token;
if (!stateObj.TryReadByte(out token)) {
return false;
}
if (!IsValidTdsToken(token)) {
Debug.Assert(false, String.Format((IFormatProvider)null, "unexpected token; token = {0,-2:X2}", token));
_state = TdsParserState.Broken;
_connHandler.BreakConnection();
Bid.Trace("<sc.TdsParser.Run|ERR> Potential multi-threaded misuse of connection, unexpected TDS token found %d#\n", ObjectID);
throw SQL.ParsingErrorToken(ParsingErrorState.InvalidTdsTokenReceived, token); // MDAC 82443
}
int tokenLength;
if (!TryGetTokenLength(token, stateObj, out tokenLength)) {
return false;
}
switch (token) {
case TdsEnums.SQLERROR:
case TdsEnums.SQLINFO:
{
if (token == TdsEnums.SQLERROR) {
stateObj._errorTokenReceived = true; // Keep track of the fact error token was received - for Done processing.
}
SqlError error;
if (!TryProcessError(token, stateObj, out error)) {
return false;
}
if (token == TdsEnums.SQLINFO && stateObj._accumulateInfoEvents)
{
Debug.Assert(error.Class < TdsEnums.MIN_ERROR_CLASS, "INFO with class > TdsEnums.MIN_ERROR_CLASS");
if (stateObj._pendingInfoEvents == null)
stateObj._pendingInfoEvents = new List<SqlError>();
stateObj._pendingInfoEvents.Add(error);
stateObj._syncOverAsync = true;
break;
}
if (RunBehavior.Clean != (RunBehavior.Clean & runBehavior)) {
// If FireInfoMessageEventOnUserErrors is true, we have to fire event without waiting.
// Otherwise we can go ahead and add it to errors/warnings collection.
SqlConnection connection = null;
if (_connHandler != null)
connection = _connHandler.Connection; // SqlInternalConnection holds the user connection object as a weak ref
if ((connection != null) &&
(connection.FireInfoMessageEventOnUserErrors == true) &&
(error.Class <= TdsEnums.MAX_USER_CORRECTABLE_ERROR_CLASS)) {
// Fire SqlInfoMessage here
FireInfoMessageEvent(connection, stateObj, error);
}
else {
// insert error/info into the appropriate exception - warning if info, exception if error
if (error.Class < TdsEnums.MIN_ERROR_CLASS) {
stateObj.AddWarning(error);
}
else if (error.Class < TdsEnums.FATAL_ERROR_CLASS) {
// VSTFDEVDIV 479643: continue results processing for all non-fatal errors (<20)
stateObj.AddError(error);
// Add it to collection - but do NOT change run behavior UNLESS
// we are in an ExecuteReader call - at which time we will be throwing
// anyways so we need to consume all errors. This is not the case
// if we have already given out a reader. If we have already given out
// a reader we need to throw the error but not halt further processing. We used to
// halt processing and that was a bug preventing the user from
// processing subsequent results.
if (null != dataStream) { // Webdata 104560
if (!dataStream.IsInitialized) {
runBehavior = RunBehavior.UntilDone;
}
}
}
else {
stateObj.AddError(error);
// Else we have a fatal error and we need to change the behavior
// since we want the complete error information in the exception.
// Besides - no further results will be received.
runBehavior = RunBehavior.UntilDone;
}
}
}
else if (error.Class >= TdsEnums.FATAL_ERROR_CLASS) {
stateObj.AddError(error);
}
break;
}
case TdsEnums.SQLCOLINFO:
{
if (null != dataStream) {
_SqlMetaDataSet metaDataSet;
if (!TryProcessColInfo(dataStream.MetaData, dataStream, stateObj, out metaDataSet)) {
return false;
}
if (!dataStream.TrySetMetaData(metaDataSet, false)) {
return false;
}
dataStream.BrowseModeInfoConsumed = true;
}
else { // no dataStream
if (!stateObj.TrySkipBytes(tokenLength)) {
return false;
}
}
break;
}
case TdsEnums.SQLDONE:
case TdsEnums.SQLDONEPROC:
case TdsEnums.SQLDONEINPROC:
{
// RunBehavior can be modified - see SQL BU DT 269516 & 290090
if (!TryProcessDone(cmdHandler, dataStream, ref runBehavior, stateObj)) {
return false;
}
if ((token == TdsEnums.SQLDONEPROC) && (cmdHandler != null)) {
// If the current parse/read is for the results of describe parameter encryption RPC requests,
// call a different handler which will update the describe parameter encryption RPC structures
// with the results, instead of the actual user RPC requests.
if (cmdHandler.IsDescribeParameterEncryptionRPCCurrentlyInProgress) {
cmdHandler.OnDoneDescribeParameterEncryptionProc(stateObj);
}
else {
cmdHandler.OnDoneProc();
}
}
break;
}
case TdsEnums.SQLORDER:
{
// don't do anything with the order token so read off the pipe
if (!stateObj.TrySkipBytes(tokenLength)) {
return false;
}
break;
}
case TdsEnums.SQLALTMETADATA:
{
stateObj.CloneCleanupAltMetaDataSetArray();
if (stateObj._cleanupAltMetaDataSetArray == null) {
// create object on demand (lazy creation)
stateObj._cleanupAltMetaDataSetArray = new _SqlMetaDataSetCollection();
}
_SqlMetaDataSet cleanupAltMetaDataSet;
if (!TryProcessAltMetaData(tokenLength, stateObj, out cleanupAltMetaDataSet)) {
return false;
}
stateObj._cleanupAltMetaDataSetArray.SetAltMetaData(cleanupAltMetaDataSet);
if (null != dataStream) {
byte metadataConsumedByte;
if (!stateObj.TryPeekByte(out metadataConsumedByte)) {
return false;
}
if (!dataStream.TrySetAltMetaDataSet(cleanupAltMetaDataSet, (TdsEnums.SQLALTMETADATA != metadataConsumedByte))) {
return false;
}
}
break;
}
case TdsEnums.SQLALTROW:
{
if (!stateObj.TryStartNewRow(isNullCompressed:false)) { // altrows are not currently null compressed
return false;
}
// read will call run until dataReady. Must not read any data if returnimmetiately set
if (RunBehavior.ReturnImmediately != (RunBehavior.ReturnImmediately & runBehavior)) {
ushort altRowId;
if (!stateObj.TryReadUInt16(out altRowId)) { // get altRowId
return false;
}
if (!TrySkipRow(stateObj._cleanupAltMetaDataSetArray.GetAltMetaData(altRowId), stateObj)) { // skip altRow
return false;
}
}
else {
dataReady = true;
}
break;
}
case TdsEnums.SQLENVCHANGE:
{
// ENVCHANGE must be processed synchronously (since it can modify the state of many objects)
stateObj._syncOverAsync = true;
SqlEnvChange[] env;
if (!TryProcessEnvChange(tokenLength, stateObj, out env)) {
return false;
}
for (int ii = 0; ii < env.Length; ii++) {
if (env[ii] != null && !this.Connection.IgnoreEnvChange) {
switch (env[ii].type) {
case TdsEnums.ENV_BEGINTRAN:
case TdsEnums.ENV_ENLISTDTC:
// When we get notification from the server of a new
// transaction, we move any pending transaction over to
// the current transaction, then we store the token in it.
// if there isn't a pending transaction, then it's either
// a TSQL transaction or a distributed transaction.
Debug.Assert(null == _currentTransaction, "non-null current transaction with an ENV Change");
_currentTransaction = _pendingTransaction;
_pendingTransaction = null;
if (null != _currentTransaction) {
_currentTransaction.TransactionId = env[ii].newLongValue; // this is defined as a ULongLong in the server and in the TDS Spec.
}
else {
TransactionType transactionType = (TdsEnums.ENV_BEGINTRAN == env[ii].type) ? TransactionType.LocalFromTSQL : TransactionType.Distributed;
_currentTransaction = new SqlInternalTransaction(_connHandler, transactionType, null, env[ii].newLongValue);
}
if (null != _statistics && !_statisticsIsInTransaction) {
_statistics.SafeIncrement(ref _statistics._transactions);
}
_statisticsIsInTransaction = true;
_retainedTransactionId = SqlInternalTransaction.NullTransactionId;
break;
case TdsEnums.ENV_DEFECTDTC:
case TdsEnums.ENV_TRANSACTIONENDED:
case TdsEnums.ENV_COMMITTRAN:
// SQLHOT 483
// Must clear the retain id if the server-side transaction ends by anything other
// than rollback.
_retainedTransactionId = SqlInternalTransaction.NullTransactionId;
goto case TdsEnums.ENV_ROLLBACKTRAN;
case TdsEnums.ENV_ROLLBACKTRAN:
// When we get notification of a completed transaction
// we null out the current transaction.
if (null != _currentTransaction) {
#if DEBUG
// Check null for case where Begin and Rollback obtained in the same message.
if (SqlInternalTransaction.NullTransactionId != _currentTransaction.TransactionId) {
Debug.Assert(_currentTransaction.TransactionId != env[ii].newLongValue, "transaction id's are not equal!");
}
#endif
if (TdsEnums.ENV_COMMITTRAN == env[ii].type) {
_currentTransaction.Completed(TransactionState.Committed);
}
else if (TdsEnums.ENV_ROLLBACKTRAN == env[ii].type) {
// Hold onto transaction id if distributed tran is rolled back. This must
// be sent to the server on subsequent executions even though the transaction
// is considered to be rolled back.
if (_currentTransaction.IsDistributed && _currentTransaction.IsActive) {
_retainedTransactionId = env[ii].oldLongValue;
}
_currentTransaction.Completed(TransactionState.Aborted);
}
else {
//
_currentTransaction.Completed(TransactionState.Unknown);
}
_currentTransaction = null;
}
_statisticsIsInTransaction = false;
break;
default:
_connHandler.OnEnvChange(env[ii]);
break;
}
}
}
break;
}
case TdsEnums.SQLLOGINACK:
{
Bid.Trace("<sc.TdsParser.TryRun|SEC> Received login acknowledgement token\n");
SqlLoginAck ack;
if (!TryProcessLoginAck(stateObj, out ack)) {
return false;
}
_connHandler.OnLoginAck(ack);
break;
}
case TdsEnums.SQLFEATUREEXTACK:
{
if (!TryProcessFeatureExtAck(stateObj)) {
return false;
}
break;
}
case TdsEnums.SQLFEDAUTHINFO:
{
_connHandler._federatedAuthenticationInfoReceived = true;
SqlFedAuthInfo info;
Bid.Trace("<sc.TdsParser.TryRun|SEC> Received federated authentication info token\n");
if (!TryProcessFedAuthInfo(stateObj, tokenLength, out info)) {
return false;
}
_connHandler.OnFedAuthInfo(info);
break;
}
case TdsEnums.SQLSESSIONSTATE:
{
if (!TryProcessSessionState(stateObj, tokenLength, _connHandler._currentSessionData)) {
return false;
}
break;
}
case TdsEnums.SQLCOLMETADATA:
{
if (tokenLength != TdsEnums.VARNULL) {
_SqlMetaDataSet metadata;
if (!TryProcessMetaData(tokenLength, stateObj, out metadata,
cmdHandler != null ? cmdHandler.ColumnEncryptionSetting : SqlCommandColumnEncryptionSetting.UseConnectionSetting)) {
return false;
}
stateObj._cleanupMetaData = metadata;
}
else {
if (cmdHandler != null) {
stateObj._cleanupMetaData = cmdHandler.MetaData;
}
}
if (null != dataStream) {
byte peekedToken;
if (!stateObj.TryPeekByte(out peekedToken)) { // temporarily cache next byte
return false;
}
if (!dataStream.TrySetMetaData(stateObj._cleanupMetaData, (TdsEnums.SQLTABNAME == peekedToken || TdsEnums.SQLCOLINFO == peekedToken))) {
return false;
}
}
else if (null != bulkCopyHandler) {
bulkCopyHandler.SetMetaData(stateObj._cleanupMetaData);
}
break;
}
case TdsEnums.SQLROW:
case TdsEnums.SQLNBCROW:
{
Debug.Assert(stateObj._cleanupMetaData != null, "Reading a row, but the metadata is null");
if (token == TdsEnums.SQLNBCROW) {
if (!stateObj.TryStartNewRow(isNullCompressed: true, nullBitmapColumnsCount: stateObj._cleanupMetaData.Length)) {
return false;
}
}
else {
if (!stateObj.TryStartNewRow(isNullCompressed:false)) {
return false;
}
}
if (null != bulkCopyHandler) {
//
if (!TryProcessRow(stateObj._cleanupMetaData, bulkCopyHandler.CreateRowBuffer(), bulkCopyHandler.CreateIndexMap(), stateObj)) {
return false;
}
}
else if (RunBehavior.ReturnImmediately != (RunBehavior.ReturnImmediately & runBehavior)) {
if (!TrySkipRow(stateObj._cleanupMetaData, stateObj)) { // skip rows
return false;
}
}
else {
dataReady = true;
}
if (_statistics != null) {
_statistics.WaitForDoneAfterRow = true;
}
break;
}
case TdsEnums.SQLRETURNSTATUS:
int status;
if (!stateObj.TryReadInt32(out status)) {
return false;
}
if (cmdHandler != null) {
cmdHandler.OnReturnStatus(status);
}
break;
case TdsEnums.SQLRETURNVALUE:
{
SqlReturnValue returnValue;
if (!TryProcessReturnValue(tokenLength, stateObj, out returnValue,
cmdHandler != null ? cmdHandler.ColumnEncryptionSetting : SqlCommandColumnEncryptionSetting.UseConnectionSetting)) {
return false;
}
if (cmdHandler != null) {
cmdHandler.OnReturnValue(returnValue, stateObj);
}
break;
}
case TdsEnums.SQLSSPI:
{
// token length is length of SSPI data - call ProcessSSPI with it
Debug.Assert(stateObj._syncOverAsync, "ProcessSSPI does not support retry, do not attempt asynchronously");
stateObj._syncOverAsync = true;
ProcessSSPI(tokenLength);
break;
}
case TdsEnums.SQLTABNAME:
{
if (null != dataStream) {
MultiPartTableName[] tableNames;
if (!TryProcessTableName(tokenLength, stateObj, out tableNames)) {
return false;
}
dataStream.TableNames = tableNames;
}
else {
if (!stateObj.TrySkipBytes(tokenLength)) {
return false;
}
}
break;
}
default:
Debug.Assert(false, "Unhandled token: " + token.ToString(CultureInfo.InvariantCulture));
break;
}
Debug.Assert(stateObj._pendingData || !dataReady, "dataReady is set, but there is no pending data");
}
// Loop while data pending & runbehavior not return immediately, OR
// if in attention case, loop while no more pending data & attention has not yet been
// received.
while ((stateObj._pendingData &&
(RunBehavior.ReturnImmediately != (RunBehavior.ReturnImmediately & runBehavior))) ||
(!stateObj._pendingData && stateObj._attentionSent && !stateObj._attentionReceived));
#if DEBUG
if ((stateObj._pendingData) && (!dataReady)) {
byte token;
if (!stateObj.TryPeekByte(out token)) {
return false;
}
Debug.Assert(IsValidTdsToken(token), string.Format("DataReady is false, but next token is not valid: {0,-2:X2}", token));
}
#endif
if (!stateObj._pendingData) {
if (null != CurrentTransaction) {
CurrentTransaction.Activate();
}
}
// if we recieved an attention (but this thread didn't send it) then
// we throw an Operation Cancelled error
if (stateObj._attentionReceived) {
// Dev11 #344723: SqlClient stress hang System_Data!Tcp::ReadSync via a call to SqlDataReader::Close
// Spin until SendAttention has cleared _attentionSending, this prevents a race condition between receiving the attention ACK and setting _attentionSent
SpinWait.SpinUntil(() => !stateObj._attentionSending);
Debug.Assert(stateObj._attentionSent, "Attention ACK has been received without attention sent");
if (stateObj._attentionSent) {
// Reset attention state.
stateObj._attentionSent = false;
stateObj._attentionReceived = false;
if (RunBehavior.Clean != (RunBehavior.Clean & runBehavior) && !stateObj._internalTimeout) {
// Add attention error to collection - if not RunBehavior.Clean!
stateObj.AddError(new SqlError(0, 0, TdsEnums.MIN_ERROR_CLASS, _server, SQLMessage.OperationCancelled(), "", 0));
}
}
}
if (stateObj.HasErrorOrWarning) {
ThrowExceptionAndWarning(stateObj);
}
return true;
}
private bool TryProcessEnvChange(int tokenLength, TdsParserStateObject stateObj, out SqlEnvChange[] sqlEnvChange) {
// There could be multiple environment change messages following this token.
byte byteLength;
int processedLength = 0;
int nvalues = 0;
SqlEnvChange[] envarray = new SqlEnvChange[3]; // Why is this hardcoded to 3?
sqlEnvChange = null;
while (tokenLength > processedLength) {
if (nvalues >= envarray.Length) {
// This is a rare path. Most of the time we will have 1 or 2 envchange data streams.
SqlEnvChange[] newenvarray = new SqlEnvChange[envarray.Length + 3];
for (int ii = 0; ii < envarray.Length; ii++)
newenvarray[ii] = envarray[ii];
envarray = newenvarray;
}
SqlEnvChange env = new SqlEnvChange();
if (!stateObj.TryReadByte(out env.type)) {
return false;
}
envarray[nvalues] = env;
nvalues++;
switch (env.type) {
case TdsEnums.ENV_DATABASE:
case TdsEnums.ENV_LANG:
if (!TryReadTwoStringFields(env, stateObj)) {
return false;
}
break;
case TdsEnums.ENV_CHARSET:
// we copied this behavior directly from luxor - see charset envchange
// section from sqlctokn.c
Debug.Assert(!_isShiloh, "Received ENV_CHARSET on non 7.0 server!");
if (!TryReadTwoStringFields(env, stateObj)) {
return false;
}
if (env.newValue == TdsEnums.DEFAULT_ENGLISH_CODE_PAGE_STRING) {
_defaultCodePage = TdsEnums.DEFAULT_ENGLISH_CODE_PAGE_VALUE;
_defaultEncoding = System.Text.Encoding.GetEncoding(_defaultCodePage);
}
else {
Debug.Assert(env.newValue.Length > TdsEnums.CHARSET_CODE_PAGE_OFFSET, "TdsParser.ProcessEnvChange(): charset value received with length <=10");
string stringCodePage = env.newValue.Substring(TdsEnums.CHARSET_CODE_PAGE_OFFSET);
_defaultCodePage = Int32.Parse(stringCodePage, NumberStyles.Integer, CultureInfo.InvariantCulture);
_defaultEncoding = System.Text.Encoding.GetEncoding(_defaultCodePage);
}
break;
case TdsEnums.ENV_PACKETSIZE:
// take care of packet size right here
Debug.Assert(stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
if (!TryReadTwoStringFields(env, stateObj)) {
// Changing packet size does not support retry, should not pend"
throw SQL.SynchronousCallMayNotPend();
}
// Only set on physical state object - this should only occur on LoginAck prior
// to MARS initialization!
Int32 packetSize = Int32.Parse(env.newValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
if (_physicalStateObj.SetPacketSize(packetSize)) {
// If packet size changed, we need to release our SNIPackets since
// those are tied to packet size of connection.
_physicalStateObj.ClearAllWritePackets();
// Update SNI ConsumerInfo value to be resulting packet size
UInt32 unsignedPacketSize = (UInt32) packetSize;
UInt32 result = SNINativeMethodWrapper.SNISetInfo(_physicalStateObj.Handle, SNINativeMethodWrapper.QTypes.SNI_QUERY_CONN_BUFSIZE, ref unsignedPacketSize);
Debug.Assert(result == TdsEnums.SNI_SUCCESS, "Unexpected failure state upon calling SNISetInfo");
}
break;
case TdsEnums.ENV_LOCALEID:
//
if (!TryReadTwoStringFields(env, stateObj)) {
return false;
}
_defaultLCID = Int32.Parse(env.newValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
break;
case TdsEnums.ENV_COMPFLAGS:
if (!TryReadTwoStringFields(env, stateObj)) {
return false;
}
break;
case TdsEnums.ENV_COLLATION:
Debug.Assert(env.newLength == 5 || env.newLength == 0, "Improper length in new collation!");
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
env.newLength = byteLength;
if (env.newLength == 5) {
if (!TryProcessCollation(stateObj, out env.newCollation)) {
return false;
}
// give the parser the new collation values in case parameters don't specify one
_defaultCollation = env.newCollation;
int newCodePage = GetCodePage(env.newCollation, stateObj);
if (newCodePage != _defaultCodePage) {
_defaultCodePage = newCodePage;
_defaultEncoding = System.Text.Encoding.GetEncoding(_defaultCodePage);
}
_defaultLCID = env.newCollation.LCID;
}
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
env.oldLength = byteLength;
Debug.Assert(env.oldLength == 5 || env.oldLength == 0, "Improper length in old collation!");
if (env.oldLength == 5) {
if (!TryProcessCollation(stateObj, out env.oldCollation)) {
return false;
}
}
env.length = 3 + env.newLength + env.oldLength;
break;
case TdsEnums.ENV_BEGINTRAN:
case TdsEnums.ENV_COMMITTRAN:
case TdsEnums.ENV_ROLLBACKTRAN:
case TdsEnums.ENV_ENLISTDTC:
case TdsEnums.ENV_DEFECTDTC:
case TdsEnums.ENV_TRANSACTIONENDED:
Debug.Assert(_isYukon, "Received new ENVCHANGE transaction/DTC token on pre 9.0 server!");
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
env.newLength = byteLength;
Debug.Assert(env.newLength == 0 || env.newLength == 8, "Improper length for new transaction id!");
if (env.newLength > 0) {
if (!stateObj.TryReadInt64(out env.newLongValue)) {
return false;
}
Debug.Assert(env.newLongValue != SqlInternalTransaction.NullTransactionId, "New transaction id is null?"); // the server guarantees that zero is an invalid transaction id.
}
else {
env.newLongValue = SqlInternalTransaction.NullTransactionId; // the server guarantees that zero is an invalid transaction id.
}
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
env.oldLength = byteLength;
Debug.Assert(env.oldLength == 0 || env.oldLength == 8, "Improper length for old transaction id!");
if (env.oldLength > 0) {
if (!stateObj.TryReadInt64(out env.oldLongValue)) {
return false;
}
Debug.Assert(env.oldLongValue != SqlInternalTransaction.NullTransactionId, "Old transaction id is null?"); // the server guarantees that zero is an invalid transaction id.
}
else {
env.oldLongValue = SqlInternalTransaction.NullTransactionId; // the server guarantees that zero is an invalid transaction id.
}
// env.length includes 1 byte type token
env.length = 3 + env.newLength + env.oldLength;
break;
case TdsEnums.ENV_LOGSHIPNODE:
// env.newBinValue is secondary node, env.oldBinValue is witness node
// comes before LoginAck so we can't assert this
if (!TryReadTwoStringFields(env, stateObj)) {
return false;
}
break;
case TdsEnums.ENV_PROMOTETRANSACTION:
Debug.Assert(_isYukon, "Received new ENVCHANGE tokens on pre 9.0 server!");
if (!stateObj.TryReadInt32(out env.newLength)) { // new value has 4 byte length
return false;
}
env.newBinValue = new byte[env.newLength];
if (!stateObj.TryReadByteArray(env.newBinValue, 0, env.newLength)) { // read new value with 4 byte length
return false;
}
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
env.oldLength = byteLength;
Debug.Assert(0 == env.oldLength, "old length should be zero");
// env.length includes 1 byte for type token
env.length = 5 + env.newLength;
break;
case TdsEnums.ENV_TRANSACTIONMANAGERADDRESS:
case TdsEnums.ENV_SPRESETCONNECTIONACK:
//
Debug.Assert(_isYukon, "Received new ENVCHANGE tokens on pre 9.0 server!");
if (!TryReadTwoBinaryFields(env, stateObj)) {
return false;
}
break;
case TdsEnums.ENV_USERINSTANCE:
Debug.Assert(!_isYukon, "Received ENV_USERINSTANCE on non 9.0 server!");
if (!TryReadTwoStringFields(env, stateObj)) {
return false;
}
break;
case TdsEnums.ENV_ROUTING:
ushort newLength;
if (!stateObj.TryReadUInt16(out newLength)) {
return false;
}
env.newLength = newLength;
byte protocol;
if (!stateObj.TryReadByte(out protocol)) {
return false;
}
ushort port;
if (!stateObj.TryReadUInt16(out port)) {
return false;
}
UInt16 serverLen;
if (!stateObj.TryReadUInt16(out serverLen)) {
return false;
}
string serverName;
if (!stateObj.TryReadString(serverLen, out serverName)) {
return false;
}
env.newRoutingInfo = new RoutingInfo(protocol, port, serverName);
UInt16 oldLength;
if (!stateObj.TryReadUInt16(out oldLength)) {
return false;
}
if (!stateObj.TrySkipBytes(oldLength)) {
return false;
}
env.length = env.newLength + oldLength + 5; // 5=2*sizeof(UInt16)+sizeof(byte) [token+newLength+oldLength]
break;
default:
Debug.Assert(false, "Unknown environment change token: " + env.type);
break;
}
processedLength += env.length;
}
sqlEnvChange = envarray;
return true;
}
private bool TryReadTwoBinaryFields(SqlEnvChange env, TdsParserStateObject stateObj) {
// Used by ProcessEnvChangeToken
byte byteLength;
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
env.newLength = byteLength;
env.newBinValue = new byte[env.newLength];
if (!stateObj.TryReadByteArray(env.newBinValue, 0, env.newLength)) {
return false;
}
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
env.oldLength = byteLength;
env.oldBinValue = new byte[env.oldLength];
if (!stateObj.TryReadByteArray(env.oldBinValue, 0, env.oldLength)) {
return false;
}
// env.length includes 1 byte type token
env.length = 3 + env.newLength + env.oldLength;
return true;
}
private bool TryReadTwoStringFields(SqlEnvChange env, TdsParserStateObject stateObj) {
// Used by ProcessEnvChangeToken
byte newLength, oldLength;
string newValue, oldValue;
if (!stateObj.TryReadByte(out newLength)) {
return false;
}
if (!stateObj.TryReadString(newLength, out newValue)) {
return false;
}
if (!stateObj.TryReadByte(out oldLength)) {
return false;
}
if (!stateObj.TryReadString(oldLength, out oldValue)) {
return false;
}
env.newLength = newLength;
env.newValue = newValue;
env.oldLength = oldLength;
env.oldValue = oldValue;
// env.length includes 1 byte type token
env.length = 3 + env.newLength * 2 + env.oldLength * 2;
return true;
}
private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavior run, TdsParserStateObject stateObj) {
ushort curCmd;
ushort status;
int count;
// Can't retry TryProcessDone
stateObj._syncOverAsync = true;
// status
// command
// rowcount (valid only if DONE_COUNT bit is set)
if (!stateObj.TryReadUInt16(out status)) {
return false;
}
if (!stateObj.TryReadUInt16(out curCmd)) {
return false;
}
if (_isYukon) {
long longCount;
if (!stateObj.TryReadInt64(out longCount)) {
return false;
}
count = (int) longCount;
}
else {
if (!stateObj.TryReadInt32(out count)) {
return false;
}
// If we haven't yet completed processing login token stream yet, we may be talking to a Yukon server
// In that case we still have to read another 4 bytes
// But don't try to read beyond the TDS stream in this case, because it generates errors if login failed.
if ( _state == TdsParserState.OpenNotLoggedIn) {
// Login incomplete, if we are reading from Yukon we need to read another int
if (stateObj._inBytesRead > stateObj._inBytesUsed) {
byte b;
if (!stateObj.TryPeekByte(out b)) {
return false;
}
if (b == 0) {
// This is an invalid token value
if (!stateObj.TryReadInt32(out count)) {
return false;
}
}
}
}
}
// We get a done token with the attention bit set
if (TdsEnums.DONE_ATTN == (status & TdsEnums.DONE_ATTN)) {
Debug.Assert(TdsEnums.DONE_MORE != (status & TdsEnums.DONE_MORE),"Not expecting DONE_MORE when receiving DONE_ATTN");
Debug.Assert(stateObj._attentionSent, "Received attention done without sending one!");
stateObj._attentionReceived = true;
Debug.Assert(stateObj._inBytesUsed == stateObj._inBytesRead && stateObj._inBytesPacket == 0, "DONE_ATTN received with more data left on wire");
}
if ((null != cmd) && (TdsEnums.DONE_COUNT == (status & TdsEnums.DONE_COUNT))) {
if (curCmd != TdsEnums.SELECT) {
if (cmd.IsDescribeParameterEncryptionRPCCurrentlyInProgress) {
// The below line is used only for debug asserts and not exposed publicly or impacts functionality otherwise.
cmd.RowsAffectedByDescribeParameterEncryption = count;
}
else {
cmd.InternalRecordsAffected = count;
}
}
// Skip the bogus DONE counts sent by the server
if (stateObj._receivedColMetaData || (curCmd != TdsEnums.SELECT)) {
cmd.OnStatementCompleted(count);
}
}
stateObj._receivedColMetaData = false;
// Surface exception for DONE_ERROR in the case we did not receive an error token
// in the stream, but an error occurred. In these cases, we throw a general server error. The
// situations where this can occur are: an invalid buffer received from client, login error
// and the server refused our connection, and the case where we are trying to log in but
// the server has reached its max connection limit. Bottom line, we need to throw general
// error in the cases where we did not receive a error token along with the DONE_ERROR.
if ((TdsEnums.DONE_ERROR == (TdsEnums.DONE_ERROR & status)) && stateObj.ErrorCount == 0 &&
stateObj._errorTokenReceived == false && (RunBehavior.Clean != (RunBehavior.Clean & run))) {
stateObj.AddError(new SqlError(0, 0, TdsEnums.MIN_ERROR_CLASS, _server, SQLMessage.SevereError(), "", 0));
if (null != reader) { // SQL BU DT 269516
if (!reader.IsInitialized) {
run = RunBehavior.UntilDone;
}
}
}
// Similar to above, only with a more severe error. In this case, if we received
// the done_srverror, this exception will be added to the collection regardless.
// MDAC #93896. Also, per Ashwin, the server will always break the connection in this case.
if ((TdsEnums.DONE_SRVERROR == (TdsEnums.DONE_SRVERROR & status)) && (RunBehavior.Clean != (RunBehavior.Clean & run))) {
stateObj.AddError(new SqlError(0, 0, TdsEnums.FATAL_ERROR_CLASS, _server, SQLMessage.SevereError(), "", 0));
if (null != reader) { // SQL BU DT 269516
if (!reader.IsInitialized) {
run = RunBehavior.UntilDone;
}
}
}
ProcessSqlStatistics(curCmd, status, count);
// stop if the DONE_MORE bit isn't set (see above for attention handling)
if (TdsEnums.DONE_MORE != (status & TdsEnums.DONE_MORE)) {
stateObj._errorTokenReceived = false;
if (stateObj._inBytesUsed >= stateObj._inBytesRead) {
stateObj._pendingData = false;
}
}
// _pendingData set by e.g. 'TdsExecuteSQLBatch'
// _hasOpenResult always set to true by 'WriteMarsHeader'
//
if (!stateObj._pendingData && stateObj._hasOpenResult) {
/*
Debug.Assert(!((sqlTransaction != null && _distributedTransaction != null) ||
(_userStartedLocalTransaction != null && _distributedTransaction != null))
, "ProcessDone - have both distributed and local transactions not null!");
*/ // WebData 112722
stateObj.DecrementOpenResultCount();
}
return true;
}
private void ProcessSqlStatistics(ushort curCmd, ushort status, int count) {
// SqlStatistics bookkeeping stuff
//
if (null != _statistics) {
// any done after row(s) counts as a resultset
if (_statistics.WaitForDoneAfterRow) {
_statistics.SafeIncrement(ref _statistics._sumResultSets);
_statistics.WaitForDoneAfterRow = false;
}
// clear row count DONE_COUNT flag is not set
if (!(TdsEnums.DONE_COUNT == (status & TdsEnums.DONE_COUNT))) {
count = 0;
}
switch (curCmd) {
case TdsEnums.INSERT:
case TdsEnums.DELETE:
case TdsEnums.UPDATE:
case TdsEnums.MERGE:
_statistics.SafeIncrement(ref _statistics._iduCount);
_statistics.SafeAdd(ref _statistics._iduRows, count);
if (!_statisticsIsInTransaction) {
_statistics.SafeIncrement(ref _statistics._transactions);
}
break;
case TdsEnums.SELECT:
_statistics.SafeIncrement(ref _statistics._selectCount);
_statistics.SafeAdd(ref _statistics._selectRows, count);
break;
case TdsEnums.BEGINXACT:
if (!_statisticsIsInTransaction) {
_statistics.SafeIncrement(ref _statistics._transactions);
}
_statisticsIsInTransaction = true;
break;
case TdsEnums.OPENCURSOR:
_statistics.SafeIncrement(ref _statistics._cursorOpens);
break;
case TdsEnums.ABORT:
_statisticsIsInTransaction = false;
break;
case TdsEnums.ENDXACT:
_statisticsIsInTransaction = false;
break;
} // switch
}
else {
switch (curCmd) {
case TdsEnums.BEGINXACT:
_statisticsIsInTransaction = true;
break;
case TdsEnums.ABORT:
case TdsEnums.ENDXACT:
_statisticsIsInTransaction = false;
break;
}
}
}
private bool TryProcessFeatureExtAck(TdsParserStateObject stateObj) {
// read feature ID
byte featureId;
do {
if (!stateObj.TryReadByte(out featureId)) {
return false;
}
if (featureId != TdsEnums.FEATUREEXT_TERMINATOR) {
UInt32 dataLen;
if (!stateObj.TryReadUInt32(out dataLen)) {
return false;
}
byte[] data = new byte[dataLen];
if (dataLen > 0) {
if (!stateObj.TryReadByteArray(data, 0, checked ((int)dataLen))) {
return false;
}
}
_connHandler.OnFeatureExtAck(featureId, data);
}
} while (featureId != TdsEnums.FEATUREEXT_TERMINATOR);
// Check if column encryption was on and feature wasn't acknowledged.
if (_connHandler.ConnectionOptions.ColumnEncryptionSetting == SqlConnectionColumnEncryptionSetting.Enabled && !IsColumnEncryptionSupported) {
throw SQL.TceNotSupported ();
}
return true;
}
private bool TryProcessSessionState(TdsParserStateObject stateObj, int length, SessionData sdata) {
if (length < 5) {
throw SQL.ParsingErrorLength(ParsingErrorState.SessionStateLengthTooShort, length);
}
UInt32 seqNum;
if (!stateObj.TryReadUInt32(out seqNum)) {
return false;
}
if (seqNum == UInt32.MaxValue) {
_connHandler.DoNotPoolThisConnection();
}
byte status;
if (!stateObj.TryReadByte(out status)) {
return false;
}
if (status > 1) {
throw SQL.ParsingErrorStatus(ParsingErrorState.SessionStateInvalidStatus, status);
}
bool recoverable = status != 0;
length -= 5;
while (length > 0) {
byte stateId;
if (!stateObj.TryReadByte(out stateId)) {
return false;
}
int stateLen;
byte stateLenByte;
if (!stateObj.TryReadByte(out stateLenByte)) {
return false;
}
if (stateLenByte < 0xFF) {
stateLen = stateLenByte;
}
else {
if (!stateObj.TryReadInt32(out stateLen)) {
return false;
}
}
byte[] buffer = null;
lock (sdata._delta) {
if (sdata._delta[stateId] == null) {
buffer = new byte[stateLen];
sdata._delta[stateId] = new SessionStateRecord { _version = seqNum, _dataLength = stateLen, _data = buffer, _recoverable = recoverable };
sdata._deltaDirty = true;
if (!recoverable) {
checked { sdata._unrecoverableStatesCount++; }
}
}
else {
if (sdata._delta[stateId]._version <= seqNum) {
SessionStateRecord sv = sdata._delta[stateId];
sv._version = seqNum;
sv._dataLength = stateLen;
if (sv._recoverable != recoverable) {
if (recoverable) {
Debug.Assert(sdata._unrecoverableStatesCount > 0, "Unrecoverable states count >0");
sdata._unrecoverableStatesCount--;
}
else {
checked { sdata._unrecoverableStatesCount++; }
}
sv._recoverable = recoverable;
}
buffer = sv._data;
if (buffer.Length < stateLen) {
buffer = new byte[stateLen];
sv._data = buffer;
}
}
}
}
if (buffer != null) {
if (!stateObj.TryReadByteArray(buffer, 0, stateLen)) {
return false;
}
}
else {
if (!stateObj.TrySkipBytes(stateLen))
return false;
}
if (stateLenByte < 0xFF) {
length -= 2 + stateLen;
}
else {
length -= 6 + stateLen;
}
}
sdata.AssertUnrecoverableStateCountIsCorrect();
return true;
}
private bool TryProcessLoginAck(TdsParserStateObject stateObj, out SqlLoginAck sqlLoginAck) {
SqlLoginAck a = new SqlLoginAck();
sqlLoginAck = null;
// read past interface type and version
if (!stateObj.TrySkipBytes(1)) {
return false;
}
byte[] b = new byte[TdsEnums.VERSION_SIZE];
if (!stateObj.TryReadByteArray(b, 0, b.Length)) {
return false;
}
a.tdsVersion = (UInt32)((((((b[0]<<8)|b[1])<<8)|b[2])<<8)|b[3]); // bytes are in motorola order (high byte first)
UInt32 majorMinor = a.tdsVersion & 0xff00ffff;
UInt32 increment = (a.tdsVersion >> 16) & 0xff;
// Server responds:
// 0x07000000 -> Sphinx // Notice server response format is different for bwd compat
// 0x07010000 -> Shiloh RTM // Notice server response format is different for bwd compat
// 0x71000001 -> Shiloh SP1
// 0x72xx0002 -> Yukon RTM
// information provided by S. Ashwin
switch (majorMinor) {
case TdsEnums.SPHINXORSHILOH_MAJOR<<24|TdsEnums.DEFAULT_MINOR: // Sphinx & Shiloh RTM
// note that sphinx and shiloh_rtm can only be distinguished by the increment
switch (increment) {
case TdsEnums.SHILOH_INCREMENT:
_isShiloh = true;
break;
case TdsEnums.SPHINX_INCREMENT:
// no flag will be set
break;
default:
throw SQL.InvalidTDSVersion();
}
break;
case TdsEnums.SHILOHSP1_MAJOR<<24|TdsEnums.SHILOHSP1_MINOR: // Shiloh SP1
if (increment != TdsEnums.SHILOHSP1_INCREMENT) { throw SQL.InvalidTDSVersion(); }
_isShilohSP1 = true;
break;
case TdsEnums.YUKON_MAJOR<<24|TdsEnums.YUKON_RTM_MINOR: // Yukon
if (increment != TdsEnums.YUKON_INCREMENT) { throw SQL.InvalidTDSVersion(); }
_isYukon = true;
break;
case TdsEnums.KATMAI_MAJOR<<24|TdsEnums.KATMAI_MINOR:
if (increment != TdsEnums.KATMAI_INCREMENT) { throw SQL.InvalidTDSVersion(); }
_isKatmai = true;
break;
case TdsEnums.DENALI_MAJOR << 24|TdsEnums.DENALI_MINOR:
if (increment != TdsEnums.DENALI_INCREMENT) { throw SQL.InvalidTDSVersion(); }
_isDenali = true;
break;
default:
throw SQL.InvalidTDSVersion();
}
_isKatmai |= _isDenali;
_isYukon |= _isKatmai;
_isShilohSP1 |= _isYukon; // includes all lower versions
_isShiloh |= _isShilohSP1; //
a.isVersion8 = _isShiloh;
stateObj._outBytesUsed = stateObj._outputHeaderLen;
byte len;
if (!stateObj.TryReadByte(out len)) {
return false;
}
if (!stateObj.TryReadString(len, out a.programName)) {
return false;
}
if (!stateObj.TryReadByte(out a.majorVersion)) {
return false;
}
if (!stateObj.TryReadByte(out a.minorVersion)) {
return false;
}
byte buildNumHi, buildNumLo;
if (!stateObj.TryReadByte(out buildNumHi)) {
return false;
}
if (!stateObj.TryReadByte(out buildNumLo)) {
return false;
}
a.buildNum = (short)((buildNumHi << 8) + buildNumLo);
Debug.Assert(_state == TdsParserState.OpenNotLoggedIn, "ProcessLoginAck called with state not TdsParserState.OpenNotLoggedIn");
_state = TdsParserState.OpenLoggedIn;
if (_isYukon) {
if (_fMARS) {
_resetConnectionEvent = new AutoResetEvent(true);
}
}
// Fail if SSE UserInstance and we have not received this info.
if ( _connHandler.ConnectionOptions.UserInstance &&
ADP.IsEmpty(_connHandler.InstanceName)) {
stateObj.AddError(new SqlError(0, 0, TdsEnums.FATAL_ERROR_CLASS, Server, SQLMessage.UserInstanceFailure(), "", 0));
ThrowExceptionAndWarning(stateObj);
}
sqlLoginAck = a;
return true;
}
private bool TryProcessFedAuthInfo(TdsParserStateObject stateObj, int tokenLen, out SqlFedAuthInfo sqlFedAuthInfo) {
sqlFedAuthInfo = null;
SqlFedAuthInfo tempFedAuthInfo = new SqlFedAuthInfo();
// Skip reading token length, since it has already been read in caller
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo> FEDAUTHINFO token stream length = {0}\n", tokenLen);
}
if (tokenLen < sizeof(uint)) {
// the token must at least contain a DWORD indicating the number of info IDs
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> FEDAUTHINFO token stream length too short for CountOfInfoIDs.\n");
throw SQL.ParsingErrorLength(ParsingErrorState.FedAuthInfoLengthTooShortForCountOfInfoIds, tokenLen);
}
// read how many FedAuthInfo options there are
uint optionsCount;
if (!stateObj.TryReadUInt32(out optionsCount)) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> Failed to read CountOfInfoIDs in FEDAUTHINFO token stream.\n");
throw SQL.ParsingError(ParsingErrorState.FedAuthInfoFailedToReadCountOfInfoIds);
}
tokenLen -= sizeof(uint); // remaining length is shortened since we read optCount
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo> CountOfInfoIDs = {0}\n", optionsCount.ToString(CultureInfo.InvariantCulture));
}
if (tokenLen > 0) {
// read the rest of the token
byte[] tokenData = new byte[tokenLen];
int totalRead = 0;
bool successfulRead = stateObj.TryReadByteArray(tokenData, 0, tokenLen, out totalRead);
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo> Read rest of FEDAUTHINFO token stream: {0}\n", BitConverter.ToString(tokenData, 0, totalRead));
}
if (!successfulRead || totalRead != tokenLen) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> Failed to read FEDAUTHINFO token stream. Attempted to read {0} bytes, actually read {1}\n", tokenLen, totalRead);
throw SQL.ParsingError(ParsingErrorState.FedAuthInfoFailedToReadTokenStream);
}
// each FedAuthInfoOpt is 9 bytes:
// 1 byte for FedAuthInfoID
// 4 bytes for FedAuthInfoDataLen
// 4 bytes for FedAuthInfoDataOffset
// So this is the index in tokenData for the i-th option
const uint optionSize = 9;
// the total number of bytes for all FedAuthInfoOpts together
uint totalOptionsSize = checked(optionsCount * optionSize);
for (uint i = 0; i < optionsCount; i++) {
uint currentOptionOffset = checked(i * optionSize);
byte id = tokenData[currentOptionOffset];
uint dataLen = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 1)));
uint dataOffset = BitConverter.ToUInt32(tokenData, checked((int)(currentOptionOffset + 5)));
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo> FedAuthInfoOpt: ID={0}, DataLen={1}, Offset={2}\n", id, dataLen.ToString(CultureInfo.InvariantCulture), dataOffset.ToString(CultureInfo.InvariantCulture));
}
// offset is measured from optCount, so subtract to make offset measured
// from the beginning of tokenData
checked {
dataOffset -= sizeof(uint);
}
// if dataOffset points to a region within FedAuthInfoOpt or after the end of the token, throw
if (dataOffset < totalOptionsSize || dataOffset >= tokenLen) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> FedAuthInfoDataOffset points to an invalid location.\n");
throw SQL.ParsingErrorOffset(ParsingErrorState.FedAuthInfoInvalidOffset, unchecked((int)dataOffset));
}
// try to read data and throw if the arguments are bad, meaning the server sent us a bad token
string data;
try {
data = System.Text.Encoding.Unicode.GetString(tokenData, checked((int)dataOffset), checked((int)dataLen));
}
catch (ArgumentOutOfRangeException e) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> Failed to read FedAuthInfoData.\n");
throw SQL.ParsingError(ParsingErrorState.FedAuthInfoFailedToReadData, e);
}
catch (ArgumentException e) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> FedAuthInfoData is not in unicode format.\n");
throw SQL.ParsingError(ParsingErrorState.FedAuthInfoDataNotUnicode, e);
}
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo> FedAuthInfoData: {0}\n", data);
}
// store data in tempFedAuthInfo
switch ((TdsEnums.FedAuthInfoId)id) {
case TdsEnums.FedAuthInfoId.Spn:
tempFedAuthInfo.spn = data;
break;
case TdsEnums.FedAuthInfoId.Stsurl:
tempFedAuthInfo.stsurl = data;
break;
default:
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo> Ignoring unknown federated authentication info option: {0}\n", id);
}
break;
}
}
}
else {
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> FEDAUTHINFO token stream is not long enough to contain the data it claims to.\n");
throw SQL.ParsingErrorLength(ParsingErrorState.FedAuthInfoLengthTooShortForData, tokenLen);
}
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo> Processed FEDAUTHINFO token stream: {0}\n", tempFedAuthInfo.ToString());
if (String.IsNullOrWhiteSpace(tempFedAuthInfo.stsurl) || String.IsNullOrWhiteSpace(tempFedAuthInfo.spn)) {
// We should be receiving both stsurl and spn
Bid.Trace("<sc.TdsParser.TryProcessFedAuthInfo|ERR> FEDAUTHINFO token stream does not contain both STSURL and SPN.\n");
throw SQL.ParsingError(ParsingErrorState.FedAuthInfoDoesNotContainStsurlAndSpn);
}
sqlFedAuthInfo = tempFedAuthInfo;
return true;
}
internal bool TryProcessError(byte token, TdsParserStateObject stateObj, out SqlError error) {
ushort shortLen;
byte byteLen;
int number;
byte state;
byte errorClass;
error = null;
if (!stateObj.TryReadInt32(out number)) {
return false;
}
if (!stateObj.TryReadByte(out state)) {
return false;
}
if (!stateObj.TryReadByte(out errorClass)) {
return false;
}
Debug.Assert(((errorClass >= TdsEnums.MIN_ERROR_CLASS) && token == TdsEnums.SQLERROR) ||
((errorClass < TdsEnums.MIN_ERROR_CLASS) && token == TdsEnums.SQLINFO), "class and token don't match!");
if (!stateObj.TryReadUInt16(out shortLen)) {
return false;
}
string message;
if (!stateObj.TryReadString(shortLen, out message)) {
return false;
}
if (!stateObj.TryReadByte(out byteLen)) {
return false;
}
string server;
// MDAC bug #49307 - server sometimes does not send over server field! In those cases
// we will use our locally cached value.
if (byteLen == 0) {
server = _server;
}
else {
if (!stateObj.TryReadString(byteLen, out server)) {
return false;
}
}
if (!stateObj.TryReadByte(out byteLen)) {
return false;
}
string procedure;
if (!stateObj.TryReadString(byteLen, out procedure)) {
return false;
}
int line;
if (_isYukon) {
if (!stateObj.TryReadInt32(out line)) {
return false;
}
}
else {
ushort shortLine;
if (!stateObj.TryReadUInt16(out shortLine)) {
return false;
}
line = shortLine;
// If we haven't yet completed processing login token stream yet, we may be talking to a Yukon server
// In that case we still have to read another 2 bytes
if ( _state == TdsParserState.OpenNotLoggedIn) {
// Login incomplete
byte b;
if (!stateObj.TryPeekByte(out b)) {
return false;
}
if (b == 0) {
// This is an invalid token value
ushort value;
if (!stateObj.TryReadUInt16(out value)) {
return false;
}
line = (line << 16) + value;
}
}
}
error = new SqlError(number, state, errorClass, _server, message, procedure, line);
return true;
}
internal bool TryProcessReturnValue(int length,
TdsParserStateObject stateObj,
out SqlReturnValue returnValue,
SqlCommandColumnEncryptionSetting columnEncryptionSetting) {
returnValue = null;
SqlReturnValue rec = new SqlReturnValue();
rec.length = length; // In Yukon this length is -1
if (_isYukon) {
if (!stateObj.TryReadUInt16(out rec.parmIndex)) {
return false;
}
}
byte len;
if (!stateObj.TryReadByte(out len)) { // Length of parameter name
return false;
}
rec.parameter = null;
if (len > 0) {
if (!stateObj.TryReadString(len, out rec.parameter)) {
return false;
}
}
// read status and ignore
byte ignored;
if (!stateObj.TryReadByte(out ignored)) {
return false;
}
UInt32 userType;
// read user type - 4 bytes Yukon, 2 backwards
if (IsYukonOrNewer) {
if (!stateObj.TryReadUInt32(out userType)) {
return false;
}
}
else {
ushort userTypeShort;
if (!stateObj.TryReadUInt16(out userTypeShort)) {
return false;
}
userType = userTypeShort;
}
// Read off the flags.
// The first byte is ignored since it doesn't contain any interesting information.
byte flags;
if (!stateObj.TryReadByte(out flags)) {
return false;
}
if (!stateObj.TryReadByte(out flags)) {
return false;
}
// Check if the column is encrypted.
if (_serverSupportsColumnEncryption) {
rec.isEncrypted = (TdsEnums.IsEncrypted == (flags & TdsEnums.IsEncrypted));
}
// read the type
byte tdsType;
if (!stateObj.TryReadByte(out tdsType)) {
return false;
}
// read the MaxLen
// For xml datatpyes, there is no tokenLength
int tdsLen;
if (tdsType == TdsEnums.SQLXMLTYPE) {
tdsLen = TdsEnums.SQL_USHORTVARMAXLEN;
}
else if (IsVarTimeTds(tdsType))
tdsLen = 0; // placeholder until we read the scale, just make sure it's not SQL_USHORTVARMAXLEN
else if (tdsType == TdsEnums.SQLDATE) {
tdsLen = 3;
}
else {
if (!TryGetTokenLength(tdsType, stateObj, out tdsLen)) {
return false;
}
}
rec.metaType = MetaType.GetSqlDataType(tdsType, userType, tdsLen);
rec.type = rec.metaType.SqlDbType;
// always use the nullable type for parameters if Shiloh or later
// Sphinx sometimes sends fixed length return values
if (_isShiloh) {
rec.tdsType = rec.metaType.NullableType;
rec.isNullable = true;
if (tdsLen == TdsEnums.SQL_USHORTVARMAXLEN) {
Debug.Assert(_isYukon, "plp data from pre-Yukon server");
rec.metaType = MetaType.GetMaxMetaTypeFromMetaType(rec.metaType);
}
}
else { // For sphinx, keep the fixed type if that is what is returned
if (rec.metaType.NullableType == tdsType)
rec.isNullable = true;
rec.tdsType = (byte)tdsType;
}
if (rec.type == SqlDbType.Decimal) {
if (!stateObj.TryReadByte(out rec.precision)) {
return false;
}
if (!stateObj.TryReadByte(out rec.scale)) {
return false;
}
}
if (rec.metaType.IsVarTime) {
if (!stateObj.TryReadByte(out rec.scale)) {
return false;
}
}
if (tdsType == TdsEnums.SQLUDT) {
if (!TryProcessUDTMetaData((SqlMetaDataPriv) rec, stateObj)) {
return false;
}
}
if (rec.type == SqlDbType.Xml) {
// Read schema info
byte schemapresent;
if (!stateObj.TryReadByte(out schemapresent)) {
return false;
}
if ((schemapresent & 1) != 0) {
if (!stateObj.TryReadByte(out len)) {
return false;
}
if (len != 0) {
if (!stateObj.TryReadString(len, out rec.xmlSchemaCollectionDatabase)) {
return false;
}
}
if (!stateObj.TryReadByte(out len)) {
return false;
}
if (len != 0) {
if (!stateObj.TryReadString(len, out rec.xmlSchemaCollectionOwningSchema)) {
return false;
}
}
short slen;
if (!stateObj.TryReadInt16(out slen)) {
return false;
}
if (slen != 0) {
if (!stateObj.TryReadString(slen, out rec.xmlSchemaCollectionName)) {
return false;
}
}
}
}
else if (_isShiloh && rec.metaType.IsCharType) {
// read the collation for 8.x servers
if (!TryProcessCollation(stateObj, out rec.collation)) {
return false;
}
int codePage = GetCodePage(rec.collation, stateObj);
// if the column lcid is the same as the default, use the default encoder
if (codePage == _defaultCodePage) {
rec.codePage = _defaultCodePage;
rec.encoding = _defaultEncoding;
}
else {
rec.codePage = codePage;
rec.encoding = System.Text.Encoding.GetEncoding(rec.codePage);
}
}
// For encrypted parameters, read the unencrypted type and encryption information.
if (_serverSupportsColumnEncryption && rec.isEncrypted) {
if (!TryProcessTceCryptoMetadata(stateObj, rec, cipherTable: null, columnEncryptionSetting: columnEncryptionSetting, isReturnValue: true)) {
return false;
}
}
// for now we coerce return values into a SQLVariant, not good...
bool isNull = false;
ulong valLen;
if (!TryProcessColumnHeaderNoNBC(rec, stateObj, out isNull, out valLen)) {
return false;
}
// always read as sql types
Debug.Assert(valLen < (ulong)(Int32.MaxValue), "ProcessReturnValue received data size > 2Gb");
int intlen = valLen > (ulong)(Int32.MaxValue) ? Int32.MaxValue : (int)valLen;
if (rec.metaType.IsPlp) {
intlen = Int32.MaxValue; // If plp data, read it all
}
if (isNull) {
GetNullSqlValue(rec.value, rec, SqlCommandColumnEncryptionSetting.Disabled, _connHandler);
}
else {
// We should never do any decryption here, so pass disabled as the command encryption override.
// We only read the binary value and decryption will be performed by OnReturnValue().
if (!TryReadSqlValue(rec.value, rec, intlen, stateObj, SqlCommandColumnEncryptionSetting.Disabled, columnName:null /*Not used*/)) {
return false;
}
}
returnValue = rec;
return true;
}
internal bool TryProcessTceCryptoMetadata (TdsParserStateObject stateObj,
SqlMetaDataPriv col,
SqlTceCipherInfoTable? cipherTable,
SqlCommandColumnEncryptionSetting columnEncryptionSetting,
bool isReturnValue) {
Debug.Assert(isReturnValue == (cipherTable == null), "Ciphertable is not set iff this is a return value");
// Read the ordinal into cipher table
ushort index = 0;
UInt32 userType;
// For return values there is not cipher table and no ordinal.
if (cipherTable.HasValue) {
if (!stateObj.TryReadUInt16(out index)) {
return false;
}
// validate the index (ordinal passed)
if (index >= cipherTable.Value.Size) {
Bid.Trace("<sc.TdsParser.TryProcessTceCryptoMetadata|TCE> Incorrect ordinal received %d, max tab size: %d\n", index, cipherTable.Value.Size);
throw SQL.ParsingErrorValue(ParsingErrorState.TceInvalidOrdinalIntoCipherInfoTable, index);
}
}
// Read the user type
if (!stateObj.TryReadUInt32(out userType)) {
return false;
}
// Read the base TypeInfo
col.baseTI = new SqlMetaDataPriv();
if (!TryProcessTypeInfo(stateObj, col.baseTI, userType)) {
return false;
}
// Read the cipher algorithm Id
byte cipherAlgorithmId;
if (!stateObj.TryReadByte(out cipherAlgorithmId)) {
return false;
}
string cipherAlgorithmName = null;
if (TdsEnums.CustomCipherAlgorithmId == cipherAlgorithmId) {
// Custom encryption algorithm, read the name
byte nameSize;
if (!stateObj.TryReadByte(out nameSize)) {
return false;
}
if (!stateObj.TryReadString(nameSize, out cipherAlgorithmName)) {
return false;
}
}
// Read Encryption Type.
byte encryptionType;
if (!stateObj.TryReadByte(out encryptionType)) {
return false;
}
// Read Normalization Rule Version.
byte normalizationRuleVersion;
if (!stateObj.TryReadByte(out normalizationRuleVersion)) {
return false;
}
Debug.Assert(col.cipherMD == null, "col.cipherMD should be null in TryProcessTceCryptoMetadata.");
// Check if TCE is enable and if it is set the crypto MD for the column.
// TCE is enabled if the command is set to enabled or to resultset only and this is not a return value
// or if it is set to use connection setting and the connection has TCE enabled.
if ((columnEncryptionSetting == SqlCommandColumnEncryptionSetting.Enabled ||
(columnEncryptionSetting == SqlCommandColumnEncryptionSetting.ResultSetOnly && !isReturnValue)) ||
(columnEncryptionSetting == SqlCommandColumnEncryptionSetting.UseConnectionSetting &&
_connHandler != null && _connHandler.ConnectionOptions != null &&
_connHandler.ConnectionOptions.ColumnEncryptionSetting == SqlConnectionColumnEncryptionSetting.Enabled)) {
col.cipherMD = new SqlCipherMetadata(cipherTable.HasValue ? (SqlTceCipherInfoEntry?)cipherTable.Value[index] : null,
index,
cipherAlgorithmId: cipherAlgorithmId,
cipherAlgorithmName: cipherAlgorithmName,
encryptionType: encryptionType,
normalizationRuleVersion: normalizationRuleVersion);
}
else {
// If TCE is disabled mark the MD as not encrypted.
col.isEncrypted = false;
}
return true;
}
internal bool TryProcessCollation(TdsParserStateObject stateObj, out SqlCollation collation) {
SqlCollation newCollation = new SqlCollation();
if (!stateObj.TryReadUInt32(out newCollation.info)) {
collation = null;
return false;
}
if (!stateObj.TryReadByte(out newCollation.sortId)) {
collation = null;
return false;
}
collation = newCollation;
return true;
}
private void WriteCollation(SqlCollation collation, TdsParserStateObject stateObj) {
if (collation == null) {
_physicalStateObj.WriteByte(0);
}
else {
_physicalStateObj.WriteByte(sizeof(UInt32)+sizeof(byte));
WriteUnsignedInt(collation.info, _physicalStateObj);
_physicalStateObj.WriteByte(collation.sortId);
}
}
internal int GetCodePage(SqlCollation collation, TdsParserStateObject stateObj) {
int codePage = 0;
if (0 != collation.sortId) {
codePage = TdsEnums.CODE_PAGE_FROM_SORT_ID[collation.sortId];
Debug.Assert(0 != codePage, "GetCodePage accessed codepage array and produced 0!, sortID =" + ((Byte)(collation.sortId)).ToString((IFormatProvider)null));
}
else {
int cultureId = collation.LCID;
bool success = false;
try {
codePage = CultureInfo.GetCultureInfo(cultureId).TextInfo.ANSICodePage;
// SqlHot 50001398: CodePage can be zero, but we should defer such errors until
// we actually MUST use the code page (i.e. don't error if no ANSI data is sent).
success = true;
}
catch (ArgumentException e) {
ADP.TraceExceptionWithoutRethrow(e);
}
// If we failed, it is quite possible this is because certain culture id's
// were removed in Win2k and beyond, however Sql Server still supports them.
// There is a workaround for the culture id's listed below, which is to mask
// off the sort id (the leading 1). If that fails, or we have a culture id
// other than the special cases below, we throw an error and throw away the
// rest of the results. For additional info, see MDAC 65963.
// SqlHot 50001398: Sometimes GetCultureInfo will return CodePage 0 instead of throwing.
// treat this as an error also, and switch into the special-case logic.
if (!success || codePage == 0) {
CultureInfo ci = null;
switch (cultureId) {
case 0x10404: // zh-TW
case 0x10804: // zh-CN
case 0x10c04: // zh-HK
case 0x11004: // zh-SG
case 0x11404: // zh-MO
case 0x10411: // ja-JP
case 0x10412: // ko-KR
// If one of the following special cases, mask out sortId and
// retry.
cultureId = cultureId & 0x03fff;
try {
ci = new CultureInfo(cultureId);
success = true;
}
catch (ArgumentException e) {
ADP.TraceExceptionWithoutRethrow(e);
}
break;
case 0x827: // Non-supported Lithuanian code page, map it to supported Lithuanian.
try {
ci = new CultureInfo(0x427);
success = true;
}
catch (ArgumentException e) {
ADP.TraceExceptionWithoutRethrow(e);
}
break;
default:
break;
}
// I don't believe we should still be in failure case, but just in case.
if (!success) {
ThrowUnsupportedCollationEncountered(stateObj);
}
if (null != ci) {
codePage = ci.TextInfo.ANSICodePage;
}
}
}
return codePage;
}
internal void DrainData(TdsParserStateObject stateObj) {
RuntimeHelpers.PrepareConstrainedRegions();
try {
#if DEBUG
TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
RuntimeHelpers.PrepareConstrainedRegions();
try {
tdsReliabilitySection.Start();
#else
{
#endif //DEBUG
try {
SqlDataReader.SharedState sharedState = stateObj._readerState;
if (sharedState != null && sharedState._dataReady) {
var metadata = stateObj._cleanupMetaData;
if (stateObj._partialHeaderBytesRead > 0) {
if (!stateObj.TryProcessHeader()) {
throw SQL.SynchronousCallMayNotPend();
}
}
if (0 == sharedState._nextColumnHeaderToRead) {
// i. user called read but didn't fetch anything
if (!stateObj.Parser.TrySkipRow(stateObj._cleanupMetaData, stateObj)) {
throw SQL.SynchronousCallMayNotPend();
}
}
else {
// iia. if we still have bytes left from a partially read column, skip
if (sharedState._nextColumnDataToRead < sharedState._nextColumnHeaderToRead) {
if ((sharedState._nextColumnHeaderToRead > 0) && (metadata[sharedState._nextColumnHeaderToRead - 1].metaType.IsPlp)) {
if (stateObj._longlen != 0) {
ulong ignored;
if (!TrySkipPlpValue(UInt64.MaxValue, stateObj, out ignored)) {
throw SQL.SynchronousCallMayNotPend();
}
}
}
else if (0 < sharedState._columnDataBytesRemaining) {
if (!stateObj.TrySkipLongBytes(sharedState._columnDataBytesRemaining)) {
throw SQL.SynchronousCallMayNotPend();
}
}
}
// iib.
// now read the remaining values off the wire for this row
if (!stateObj.Parser.TrySkipRow(metadata, sharedState._nextColumnHeaderToRead, stateObj)) {
throw SQL.SynchronousCallMayNotPend();
}
}
}
Run(RunBehavior.Clean, null, null, null, stateObj);
}
catch {
_connHandler.DoomThisConnection();
throw;
}
}
#if DEBUG
finally {
tdsReliabilitySection.Stop();
}
#endif //DEBUG
}
catch (System.OutOfMemoryException) {
_connHandler.DoomThisConnection();
throw;
}
catch (System.StackOverflowException) {
_connHandler.DoomThisConnection();
throw;
}
catch (System.Threading.ThreadAbortException) {
_connHandler.DoomThisConnection();
throw;
}
}
internal void ThrowUnsupportedCollationEncountered(TdsParserStateObject stateObj) {
stateObj.AddError(new SqlError(0, 0, TdsEnums.MIN_ERROR_CLASS, _server, SQLMessage.CultureIdError(), "", 0));
if (null != stateObj) {
DrainData(stateObj);
stateObj._pendingData = false;
}
ThrowExceptionAndWarning(stateObj);
}
internal bool TryProcessAltMetaData(int cColumns, TdsParserStateObject stateObj, out _SqlMetaDataSet metaData) {
Debug.Assert(cColumns > 0, "should have at least 1 column in altMetaData!");
metaData = null;
_SqlMetaDataSet altMetaDataSet = new _SqlMetaDataSet(cColumns, null);
int[] indexMap = new int[cColumns];
if (!stateObj.TryReadUInt16(out altMetaDataSet.id)) {
return false;
}
byte byCols;
if (!stateObj.TryReadByte(out byCols)) {
return false;
}
while (byCols > 0) {
if (!stateObj.TrySkipBytes(2)) { // ignore ColNum ...
return false;
}
byCols--;
}
// pass 1, read the meta data off the wire
for (int i = 0; i < cColumns; i++) {
// internal meta data class
_SqlMetaData col = altMetaDataSet[i];
if (!stateObj.TryReadByte(out col.op)) {
return false;
}
if (!stateObj.TryReadUInt16(out col.operand)) {
return false;
}
// TCE is not applicable to AltMetadata.
if (!TryCommonProcessMetaData(stateObj, col, null, fColMD: false, columnEncryptionSetting: SqlCommandColumnEncryptionSetting.Disabled)) {
return false;
}
if (ADP.IsEmpty(col.column)) {
// create column name from op
switch (col.op) {
case TdsEnums.AOPAVG:
col.column = "avg";
break;
case TdsEnums.AOPCNT:
col.column = "cnt";
break;
case TdsEnums.AOPCNTB:
col.column = "cntb";
break;
case TdsEnums.AOPMAX:
col.column = "max";
break;
case TdsEnums.AOPMIN:
col.column = "min";
break;
case TdsEnums.AOPSUM:
col.column = "sum";
break;
case TdsEnums.AOPANY:
col.column = "any";
break;
case TdsEnums.AOPNOOP:
col.column = "noop";
break;
case TdsEnums.AOPSTDEV:
col.column = "stdev";
break;
case TdsEnums.AOPSTDEVP:
col.column = "stdevp";
break;
case TdsEnums.AOPVAR:
col.column = "var";
break;
case TdsEnums.AOPVARP:
col.column = "varp";
break;
}
}
indexMap[i] = i;
}
altMetaDataSet.indexMap = indexMap;
altMetaDataSet.visibleColumns = cColumns;
metaData = altMetaDataSet;
return true;
}
/// <summary>
/// <para> Parses the TDS message to read single CIPHER_INFO entry.</para>
/// </summary>
internal bool TryReadCipherInfoEntry (TdsParserStateObject stateObj, out SqlTceCipherInfoEntry entry) {
byte cekValueCount = 0;
entry = new SqlTceCipherInfoEntry(ordinal: 0);
// Read the DB ID
int dbId;
if (!stateObj.TryReadInt32(out dbId)) {
return false;
}
// Read the keyID
int keyId;
if (!stateObj.TryReadInt32(out keyId)) {
return false;
}
// Read the key version
int keyVersion;
if (!stateObj.TryReadInt32(out keyVersion)) {
return false;
}
// Read the key MD Version
byte[] keyMDVersion = new byte[8];
if (!stateObj.TryReadByteArray(keyMDVersion, 0, 8)) {
return false;
}
// Read the value count
if (!stateObj.TryReadByte (out cekValueCount)) {
return false;
}
for (int i = 0; i < cekValueCount; i++) {
// Read individual CEK values
byte[] encryptedCek;
string keyPath;
string keyStoreName;
byte algorithmLength;
string algorithmName;
ushort shortValue;
byte byteValue;
int length;
// Read the length of encrypted CEK
if (!stateObj.TryReadUInt16 (out shortValue)) {
return false;
}
length = shortValue;
encryptedCek = new byte[length];
// Read the actual encrypted CEK
if (!stateObj.TryReadByteArray (encryptedCek, 0, length)) {
return false;
}
// Read the length of key store name
if (!stateObj.TryReadByte (out byteValue)) {
return false;
}
length = byteValue;
// And read the key store name now
if (!stateObj.TryReadString(length, out keyStoreName)) {
return false;
}
// Read the length of key Path
if (!stateObj.TryReadUInt16 (out shortValue)) {
return false;
}
length = shortValue;
// Read the key path string
if (!stateObj.TryReadString(length, out keyPath)) {
return false;
}
// Read the length of the string carrying the encryption algo
if (!stateObj.TryReadByte(out algorithmLength)) {
return false;
}
length = (int)algorithmLength;
// Read the string carrying the encryption algo (eg. RSA_PKCS_OAEP)
if (!stateObj.TryReadString(length, out algorithmName)) {
return false;
}
// Add this encrypted CEK blob to our list of encrypted values for the CEK
entry.Add(encryptedCek,
databaseId: dbId,
cekId: keyId,
cekVersion: keyVersion,
cekMdVersion: keyMDVersion,
keyPath: keyPath,
keyStoreName: keyStoreName,
algorithmName: algorithmName);
}
return true;
}
/// <summary>
/// <para> Parses the TDS message to read a single CIPHER_INFO table.</para>
/// </summary>
internal bool TryProcessCipherInfoTable (TdsParserStateObject stateObj, out SqlTceCipherInfoTable? cipherTable) {
// Read count
short tableSize = 0;
cipherTable = null;
if (!stateObj.TryReadInt16(out tableSize)) {
return false;
}
if (0 != tableSize) {
SqlTceCipherInfoTable tempTable = new SqlTceCipherInfoTable(tableSize);
// Read individual entries
for (int i = 0; i < tableSize; i++) {
SqlTceCipherInfoEntry entry;
if (!TryReadCipherInfoEntry (stateObj, out entry)) {
return false;
}
tempTable[i] = entry;
}
cipherTable = tempTable;
}
return true;
}
internal bool TryProcessMetaData(int cColumns, TdsParserStateObject stateObj, out _SqlMetaDataSet metaData, SqlCommandColumnEncryptionSetting columnEncryptionSetting) {
Debug.Assert(cColumns > 0, "should have at least 1 column in metadata!");
// Read the cipher info table first
SqlTceCipherInfoTable? cipherTable = null;
if (_serverSupportsColumnEncryption) {
if (!TryProcessCipherInfoTable (stateObj, out cipherTable)) {
metaData = null;
return false;
}
}
// Read the ColumnData fields
_SqlMetaDataSet newMetaData = new _SqlMetaDataSet(cColumns, cipherTable);
for (int i = 0; i < cColumns; i++) {
if (!TryCommonProcessMetaData(stateObj, newMetaData[i], cipherTable, fColMD: true, columnEncryptionSetting: columnEncryptionSetting)) {
metaData = null;
return false;
}
}
// DEVNOTE: cipherTable is discarded at this point since its no longer needed.
metaData = newMetaData;
return true;
}
private bool IsVarTimeTds(byte tdsType) {
return tdsType == TdsEnums.SQLTIME || tdsType == TdsEnums.SQLDATETIME2 || tdsType == TdsEnums.SQLDATETIMEOFFSET;
}
private bool TryProcessTypeInfo (TdsParserStateObject stateObj, SqlMetaDataPriv col, UInt32 userType) {
byte byteLen;
byte tdsType;
if (!stateObj.TryReadByte(out tdsType)) {
return false;
}
if (tdsType == TdsEnums.SQLXMLTYPE)
col.length = TdsEnums.SQL_USHORTVARMAXLEN; //Use the same length as other plp datatypes
else if (IsVarTimeTds(tdsType))
col.length = 0; // placeholder until we read the scale, just make sure it's not SQL_USHORTVARMAXLEN
else if (tdsType == TdsEnums.SQLDATE) {
col.length = 3;
}
else {
if (!TryGetTokenLength(tdsType, stateObj, out col.length)) {
return false;
}
}
col.metaType = MetaType.GetSqlDataType(tdsType, userType, col.length);
col.type = col.metaType.SqlDbType;
// If sphinx, do not change to nullable type
if (_isShiloh)
col.tdsType = (col.isNullable ? col.metaType.NullableType : col.metaType.TDSType);
else
col.tdsType = tdsType;
if (_isYukon) {
if (TdsEnums.SQLUDT == tdsType) {
if (!TryProcessUDTMetaData((SqlMetaDataPriv) col, stateObj)) {
return false;
}
}
if (col.length == TdsEnums.SQL_USHORTVARMAXLEN) {
Debug.Assert(tdsType == TdsEnums.SQLXMLTYPE ||
tdsType == TdsEnums.SQLBIGVARCHAR ||
tdsType == TdsEnums.SQLBIGVARBINARY ||
tdsType == TdsEnums.SQLNVARCHAR ||
tdsType == TdsEnums.SQLUDT,
"Invalid streaming datatype");
col.metaType = MetaType.GetMaxMetaTypeFromMetaType(col.metaType);
Debug.Assert(col.metaType.IsLong, "Max datatype not IsLong");
col.length = Int32.MaxValue;
if (tdsType == TdsEnums.SQLXMLTYPE) {
byte schemapresent;
if (!stateObj.TryReadByte(out schemapresent)) {
return false;
}
if ((schemapresent & 1) != 0) {
if (!stateObj.TryReadByte(out byteLen)) {
return false;
}
if (byteLen != 0) {
if (!stateObj.TryReadString(byteLen, out col.xmlSchemaCollectionDatabase)) {
return false;
}
}
if (!stateObj.TryReadByte(out byteLen)) {
return false;
}
if (byteLen != 0) {
if (!stateObj.TryReadString(byteLen, out col.xmlSchemaCollectionOwningSchema)) {
return false;
}
}
short shortLen;
if (!stateObj.TryReadInt16(out shortLen)) {
return false;
}
if (byteLen != 0) {
if (!stateObj.TryReadString(shortLen, out col.xmlSchemaCollectionName)) {
return false;
}
}
}
}
}
}
if (col.type == SqlDbType.Decimal) {
if (!stateObj.TryReadByte(out col.precision)) {
return false;
}
if (!stateObj.TryReadByte(out col.scale)) {
return false;
}
}
if (col.metaType.IsVarTime) {
if (!stateObj.TryReadByte(out col.scale)) {
return false;
}
Debug.Assert(0 <= col.scale && col.scale <= 7);
// calculate actual column length here
//
switch (col.metaType.SqlDbType)
{
case SqlDbType.Time:
col.length = MetaType.GetTimeSizeFromScale(col.scale);
break;
case SqlDbType.DateTime2:
// Date in number of days (3 bytes) + time
col.length = 3 + MetaType.GetTimeSizeFromScale(col.scale);
break;
case SqlDbType.DateTimeOffset:
// Date in days (3 bytes) + offset in minutes (2 bytes) + time
col.length = 5 + MetaType.GetTimeSizeFromScale(col.scale);
break;
default:
Debug.Assert(false, "Unknown VariableTime type!");
break;
}
}
// read the collation for 7.x servers
if (_isShiloh && col.metaType.IsCharType && (tdsType != TdsEnums.SQLXMLTYPE)) {
if (!TryProcessCollation(stateObj, out col.collation)) {
return false;
}
int codePage = GetCodePage(col.collation, stateObj);
if (codePage == _defaultCodePage) {
col.codePage = _defaultCodePage;
col.encoding = _defaultEncoding;
}
else {
col.codePage = codePage;
col.encoding = System.Text.Encoding.GetEncoding(col.codePage);
}
}
return true;
}
private bool TryCommonProcessMetaData(TdsParserStateObject stateObj, _SqlMetaData col, SqlTceCipherInfoTable? cipherTable, bool fColMD, SqlCommandColumnEncryptionSetting columnEncryptionSetting) {
byte byteLen;
UInt32 userType;
// read user type - 4 bytes Yukon, 2 backwards
if (IsYukonOrNewer) {
if (!stateObj.TryReadUInt32(out userType)) {
return false;
}
}
else {
ushort userTypeShort;
if (!stateObj.TryReadUInt16(out userTypeShort)) {
return false;
}
userType = userTypeShort;
}
// read flags and set appropriate flags in structure
byte flags;
if (!stateObj.TryReadByte(out flags)) {
return false;
}
col.updatability = (byte)((flags & TdsEnums.Updatability) >> 2);
col.isNullable = (TdsEnums.Nullable == (flags & TdsEnums.Nullable));
col.isIdentity = (TdsEnums.Identity == (flags & TdsEnums.Identity));
// read second byte of column metadata flags
if (!stateObj.TryReadByte(out flags)) {
return false;
}
col.isColumnSet = (TdsEnums.IsColumnSet == (flags & TdsEnums.IsColumnSet));
if (fColMD && _serverSupportsColumnEncryption) {
col.isEncrypted = (TdsEnums.IsEncrypted == (flags & TdsEnums.IsEncrypted));
}
// Read TypeInfo
if (!TryProcessTypeInfo (stateObj, col, userType)) {
return false;
}
// Read tablename if present
if (col.metaType.IsLong && !col.metaType.IsPlp) {
if (_isYukon) {
int unusedLen = 0xFFFF; //We ignore this value
if (!TryProcessOneTable(stateObj, ref unusedLen, out col.multiPartTableName)) {
return false;
}
} else {
ushort shortLen;
if (!stateObj.TryReadUInt16(out shortLen)) {
return false;
}
string tableName;
if (!stateObj.TryReadString(shortLen, out tableName)) {
return false;
}
// with Sql2000 this is returned as an unquoted mix of catalog.owner.table
// all of which may contain "." and unable to parse correctly from the string alone
// example "select * from pubs..[A.B.C.D.E]" AND only when * will contain a image/text/ntext column
// by delay parsing from execute to SqlDataReader.GetSchemaTable to enable more scenarios
col.multiPartTableName = new MultiPartTableName(tableName);
}
}
// Read the TCE column cryptoinfo
if (fColMD && _serverSupportsColumnEncryption && col.isEncrypted) {
// If the column is encrypted, we should have a valid cipherTable
if (cipherTable.HasValue && !TryProcessTceCryptoMetadata (stateObj, col, cipherTable.Value, columnEncryptionSetting, isReturnValue: false)) {
return false;
}
}
// Read the column name
if (!stateObj.TryReadByte(out byteLen)) {
return false;
}
if (!stateObj.TryReadString(byteLen, out col.column)) {
return false;
}
// We get too many DONE COUNTs from the server, causing too meany StatementCompleted event firings.
// We only need to fire this event when we actually have a meta data stream with 0 or more rows.
stateObj._receivedColMetaData = true;
return true;
}
private bool TryProcessUDTMetaData(SqlMetaDataPriv metaData, TdsParserStateObject stateObj) {
ushort shortLength;
byte byteLength;
if (!stateObj.TryReadUInt16(out shortLength)) { // max byte size
return false;
}
metaData.length = shortLength;
// database name
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
if (byteLength != 0) {
if (!stateObj.TryReadString(byteLength, out metaData.udtDatabaseName)) {
return false;
}
}
// schema name
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
if (byteLength != 0) {
if (!stateObj.TryReadString(byteLength, out metaData.udtSchemaName)) {
return false;
}
}
// type name
if (!stateObj.TryReadByte(out byteLength)) {
return false;
}
if (byteLength != 0) {
if (!stateObj.TryReadString(byteLength, out metaData.udtTypeName)) {
return false;
}
}
if (!stateObj.TryReadUInt16(out shortLength)) {
return false;
}
if (shortLength != 0) {
if (!stateObj.TryReadString(shortLength, out metaData.udtAssemblyQualifiedName)) {
return false;
}
}
return true;
}
private void WriteUDTMetaData(object value, string database, string schema, string type,
TdsParserStateObject stateObj) {
// database
if (ADP.IsEmpty(database)) {
stateObj.WriteByte(0);
}
else {
stateObj.WriteByte((byte)database.Length);
WriteString(database, stateObj);
}
// schema
if (ADP.IsEmpty(schema)) {
stateObj.WriteByte(0);
}
else {
stateObj.WriteByte((byte)schema.Length);
WriteString(schema, stateObj);
}
// type
if (ADP.IsEmpty(type)) {
stateObj.WriteByte(0);
}
else {
stateObj.WriteByte((byte)type.Length);
WriteString(type, stateObj);
}
}
internal bool TryProcessTableName(int length, TdsParserStateObject stateObj, out MultiPartTableName[] multiPartTableNames) {
int tablesAdded = 0;
MultiPartTableName[] tables = new MultiPartTableName[1];
MultiPartTableName mpt;
while (length > 0) {
//
if (!TryProcessOneTable(stateObj, ref length, out mpt)) {
multiPartTableNames = null;
return false;
}
if (tablesAdded == 0) {
tables[tablesAdded] = mpt;
}
else {
MultiPartTableName[] newTables = new MultiPartTableName[tables.Length + 1];
Array.Copy(tables, 0, newTables, 0, tables.Length);
newTables[tables.Length] = mpt;
tables = newTables;
}
tablesAdded++;
}
multiPartTableNames = tables;
return true;
}
private bool TryProcessOneTable(TdsParserStateObject stateObj, ref int length, out MultiPartTableName multiPartTableName) {
ushort tableLen;
MultiPartTableName mpt;
string value;
multiPartTableName = default(MultiPartTableName);
if (_isShilohSP1) {
mpt = new MultiPartTableName();
byte nParts;
// Find out how many parts in the TDS stream
if (!stateObj.TryReadByte(out nParts)) {
return false;
}
length--;
if (nParts == 4) {
if (!stateObj.TryReadUInt16(out tableLen)) {
return false;
}
length -= 2;
if (!stateObj.TryReadString(tableLen, out value)) {
return false;
}
mpt.ServerName = value;
nParts--;
length -= (tableLen * 2); // wide bytes
}
if (nParts == 3) {
if (!stateObj.TryReadUInt16(out tableLen)) {
return false;
}
length -= 2;
if (!stateObj.TryReadString(tableLen, out value)) {
return false;
}
mpt.CatalogName = value;
length -= (tableLen * 2); // wide bytes
nParts--;
}
if (nParts == 2) {
if (!stateObj.TryReadUInt16(out tableLen)) {
return false;
}
length -= 2;
if (!stateObj.TryReadString(tableLen, out value)) {
return false;
}
mpt.SchemaName = value;
length -= (tableLen * 2); // wide bytes
nParts--;
}
if (nParts == 1) {
if (!stateObj.TryReadUInt16(out tableLen)) {
return false;
}
length -= 2;
if (!stateObj.TryReadString(tableLen, out value)) {
return false;
}
mpt.TableName = value;
length -= (tableLen * 2); // wide bytes
nParts--;
}
Debug.Assert(nParts == 0 , "ProcessTableName:Unidentified parts in the table name token stream!");
}
else {
if (!stateObj.TryReadUInt16(out tableLen)) {
return false;
}
length -= 2;
if (!stateObj.TryReadString(tableLen, out value)) {
return false;
}
string tableName = value;
length -= (tableLen * 2); // wide bytes
mpt = new MultiPartTableName(MultipartIdentifier.ParseMultipartIdentifier(tableName, "[\"", "]\"", Res.SQL_TDSParserTableName, false));
}
multiPartTableName = mpt;
return true;
}
// augments current metadata with table and key information
private bool TryProcessColInfo(_SqlMetaDataSet columns, SqlDataReader reader, TdsParserStateObject stateObj, out _SqlMetaDataSet metaData) {
Debug.Assert(columns != null && columns.Length > 0, "no metadata available!");
metaData = null;
for (int i = 0; i < columns.Length; i++) {
_SqlMetaData col = columns[i];
byte ignored;
if (!stateObj.TryReadByte(out ignored)) { // colnum, ignore
return false;
}
if (!stateObj.TryReadByte(out col.tableNum)) {
return false;
}
// interpret status
byte status;
if (!stateObj.TryReadByte(out status)) {
return false;
}
col.isDifferentName = (TdsEnums.SQLDifferentName == (status & TdsEnums.SQLDifferentName));
col.isExpression = (TdsEnums.SQLExpression == (status & TdsEnums.SQLExpression));
col.isKey = (TdsEnums.SQLKey == (status & TdsEnums.SQLKey));
col.isHidden = (TdsEnums.SQLHidden == (status & TdsEnums.SQLHidden));
// read off the base table name if it is different than the select list column name
if (col.isDifferentName) {
byte len;
if (!stateObj.TryReadByte(out len)) {
return false;
}
if (!stateObj.TryReadString(len, out col.baseColumn)) {
return false;
}
}
// Fixup column name - only if result of a table - that is if it was not the result of
// an expression.
if ((reader.TableNames != null) && (col.tableNum > 0)) {
Debug.Assert(reader.TableNames.Length >= col.tableNum, "invalid tableNames array!");
col.multiPartTableName = reader.TableNames[col.tableNum - 1];
}
// MDAC 60109: expressions are readonly
if (col.isExpression) {
col.updatability = 0;
}
}
// set the metadata so that the stream knows some metadata info has changed
metaData = columns;
return true;
}
// takes care of any per data header information:
// for long columns, reads off textptrs, reads length, check nullability
// for other columns, reads length, checks nullability
// returns length and nullability
internal bool TryProcessColumnHeader(SqlMetaDataPriv col, TdsParserStateObject stateObj, int columnOrdinal, out bool isNull, out ulong length) {
// query NBC row information first
if (stateObj.IsNullCompressionBitSet(columnOrdinal)) {
isNull = true;
// column information is not present in TDS if null compression bit is set, return now
length = 0;
return true;
}
return TryProcessColumnHeaderNoNBC(col, stateObj, out isNull, out length);
}
private bool TryProcessColumnHeaderNoNBC(SqlMetaDataPriv col, TdsParserStateObject stateObj, out bool isNull, out ulong length) {
if (col.metaType.IsLong && !col.metaType.IsPlp) {
//
// we don't care about TextPtrs, simply go after the data after it
//
byte textPtrLen;
if (!stateObj.TryReadByte(out textPtrLen)) {
isNull = false;
length = 0;
return false;
}
if (0 != textPtrLen) {
// read past text pointer
if (!stateObj.TrySkipBytes(textPtrLen)) {
isNull = false;
length = 0;
return false;
}
// read past timestamp
if (!stateObj.TrySkipBytes(TdsEnums.TEXT_TIME_STAMP_LEN)) {
isNull = false;
length = 0;
return false;
}
isNull = false;
return TryGetDataLength(col, stateObj, out length);
}
else {
isNull = true;
length = 0;
return true;
}
}
else {
// non-blob columns
ulong longlen;
if (!TryGetDataLength(col, stateObj, out longlen)) {
isNull = false;
length = 0;
return false;
}
isNull = IsNull(col.metaType, longlen);
length = (isNull ? 0 : longlen);
return true;
}
}
// assumes that the current position is at the start of an altrow!
internal bool TryGetAltRowId(TdsParserStateObject stateObj, out int id) {
byte token;
if (!stateObj.TryReadByte(out token)) { // skip over ALTROW token
id = 0;
return false;
}
Debug.Assert((token == TdsEnums.SQLALTROW), "");
// Start a fresh row - disable NBC since Alt Rows are never compressed
if (!stateObj.TryStartNewRow(isNullCompressed: false)) {
id = 0;
return false;
}
ushort shortId;
if (!stateObj.TryReadUInt16(out shortId)) {
id = 0;
return false;
}
id = shortId;
return true;
}
// Used internally by BulkCopy only
private bool TryProcessRow(_SqlMetaDataSet columns, object[] buffer, int[] map, TdsParserStateObject stateObj) {
SqlBuffer data = new SqlBuffer();
for (int i = 0; i < columns.Length; i++) {
_SqlMetaData md = columns[i];
Debug.Assert(md != null, "_SqlMetaData should not be null for column " + i.ToString(CultureInfo.InvariantCulture));
bool isNull;
ulong len;
if (!TryProcessColumnHeader(md, stateObj, i, out isNull, out len)) {
return false;
}
if (isNull) {
GetNullSqlValue(data, md, SqlCommandColumnEncryptionSetting.Disabled /*Column Encryption Disabled for Bulk Copy*/, _connHandler);
buffer[map[i]] = data.SqlValue;
}
else {
// We only read up to 2Gb. Throw if data is larger. Very large data
// should be read in chunks in sequential read mode
// For Plp columns, we may have gotten only the length of the first chunk
if (!TryReadSqlValue(data, md, md.metaType.IsPlp ? (Int32.MaxValue) : (int)len, stateObj,
SqlCommandColumnEncryptionSetting.Disabled /*Column Encryption Disabled for Bulk Copy*/,
md.column)) {
return false;
}
buffer[map[i]] = data.SqlValue;
if (stateObj._longlen != 0) {
throw new SqlTruncateException(Res.GetString(Res.SqlMisc_TruncationMaxDataMessage));
}
}
data.Clear();
}
return true;
}
/// <summary>
/// Determines if a column value should be transparently decrypted (based on SqlCommand and Connection String settings).
/// </summary>
/// <returns>true if the value should be transparently decrypted, false otherwise</returns>
internal static bool ShouldHonorTceForRead (SqlCommandColumnEncryptionSetting columnEncryptionSetting,
SqlInternalConnectionTds connection) {
// Command leve setting trumps all
switch (columnEncryptionSetting) {
case SqlCommandColumnEncryptionSetting.Disabled:
return false;
case SqlCommandColumnEncryptionSetting.Enabled:
return true;
case SqlCommandColumnEncryptionSetting.ResultSetOnly:
return true;
default:
// Check connection level setting!
Debug.Assert(SqlCommandColumnEncryptionSetting.UseConnectionSetting == columnEncryptionSetting,
"Unexpected value for command level override");
return (connection != null && connection.ConnectionOptions != null &&
connection.ConnectionOptions.ColumnEncryptionSetting == SqlConnectionColumnEncryptionSetting.Enabled);
}
}
internal static object GetNullSqlValue(
SqlBuffer nullVal,
SqlMetaDataPriv md,
SqlCommandColumnEncryptionSetting columnEncryptionSetting,
SqlInternalConnectionTds connection) {
SqlDbType type = md.type;
if (type == SqlDbType.VarBinary && // if its a varbinary
md.isEncrypted &&// and encrypted
ShouldHonorTceForRead(columnEncryptionSetting, connection)){
type = md.baseTI.type; // the use the actual (plaintext) type
}
switch (type) {
case SqlDbType.Real:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Single);
break;
case SqlDbType.Float:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Double);
break;
case SqlDbType.Udt:
case SqlDbType.Binary:
case SqlDbType.VarBinary:
case SqlDbType.Image:
nullVal.SqlBinary = SqlBinary.Null;
break;
case SqlDbType.UniqueIdentifier:
nullVal.SqlGuid = SqlGuid.Null;
break;
case SqlDbType.Bit:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Boolean);
break;
case SqlDbType.TinyInt:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Byte);
break;
case SqlDbType.SmallInt:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Int16);
break;
case SqlDbType.Int:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Int32);
break;
case SqlDbType.BigInt:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Int64);
break;
case SqlDbType.Char:
case SqlDbType.VarChar:
case SqlDbType.NChar:
case SqlDbType.NVarChar:
case SqlDbType.Text:
case SqlDbType.NText:
nullVal.SetToNullOfType(SqlBuffer.StorageType.String);
break;
case SqlDbType.Decimal:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Decimal);
break;
case SqlDbType.DateTime:
case SqlDbType.SmallDateTime:
nullVal.SetToNullOfType(SqlBuffer.StorageType.DateTime);
break;
case SqlDbType.Money:
case SqlDbType.SmallMoney:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Money);
break;
case SqlDbType.Variant:
// DBNull.Value will have to work here
nullVal.SetToNullOfType(SqlBuffer.StorageType.Empty);
break;
case SqlDbType.Xml:
nullVal.SqlCachedBuffer = SqlCachedBuffer.Null;
break;
case SqlDbType.Date:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Date);
break;
case SqlDbType.Time:
nullVal.SetToNullOfType(SqlBuffer.StorageType.Time);
break;
case SqlDbType.DateTime2:
nullVal.SetToNullOfType(SqlBuffer.StorageType.DateTime2);
break;
case SqlDbType.DateTimeOffset:
nullVal.SetToNullOfType(SqlBuffer.StorageType.DateTimeOffset);
break;
case SqlDbType.Timestamp:
// Dev10 Bug #479607 - this should have been the same as SqlDbType.Binary, but it's a rejected breaking change
// Dev10 Bug #752790 - don't assert when it does happen
break;
default:
Debug.Assert(false, "unknown null sqlType!" + md.type.ToString());
break;
}
return nullVal;
}
internal bool TrySkipRow(_SqlMetaDataSet columns, TdsParserStateObject stateObj) {
return TrySkipRow(columns, 0, stateObj);
}
internal bool TrySkipRow(_SqlMetaDataSet columns, int startCol, TdsParserStateObject stateObj) {
for (int i = startCol; i < columns.Length; i++) {
_SqlMetaData md = columns[i];
if (!TrySkipValue(md, i, stateObj)) {
return false;
}
}
return true;
}
/// <summary>
/// This method skips bytes of a single column value from the media. It supports NBCROW and handles all types of values, including PLP and long
/// </summary>
internal bool TrySkipValue(SqlMetaDataPriv md, int columnOrdinal, TdsParserStateObject stateObj) {
if (stateObj.IsNullCompressionBitSet(columnOrdinal)) {
return true;
}
if (md.metaType.IsPlp) {
ulong ignored;
if (!TrySkipPlpValue(UInt64.MaxValue, stateObj, out ignored)) {
return false;
}
}
else if (md.metaType.IsLong) {
Debug.Assert(!md.metaType.IsPlp, "Plp types must be handled using SkipPlpValue");
byte textPtrLen;
if (!stateObj.TryReadByte(out textPtrLen)) {
return false;
}
if (0 != textPtrLen) {
if (!stateObj.TrySkipBytes(textPtrLen + TdsEnums.TEXT_TIME_STAMP_LEN)) {
return false;
}
int length;
if (!TryGetTokenLength(md.tdsType, stateObj, out length)) {
return false;
}
if (!stateObj.TrySkipBytes(length)) {
return false;
}
}
}
else {
int length;
if (!TryGetTokenLength(md.tdsType, stateObj, out length)) {
return false;
}
// if false, no value to skip - it's null
if (!IsNull(md.metaType, (ulong)length)) {
if (!stateObj.TrySkipBytes(length)) {
return false;
}
}
}
return true;
}
private bool IsNull(MetaType mt, ulong length) {
// null bin and char types have a length of -1 to represent null
if (mt.IsPlp) {
return (TdsEnums.SQL_PLP_NULL == length);
}
// HOTFIX #50000415: for image/text, 0xFFFF is the length, not representing null
if ((TdsEnums.VARNULL == length) && !mt.IsLong) {
return true;
}
// other types have a length of 0 to represent null
// long and non-PLP types will always return false because these types are either char or binary
// this is expected since for long and non-plp types isnull is checked based on textptr field and not the length
return ((TdsEnums.FIXEDNULL == length) && !mt.IsCharType && !mt.IsBinType);
}
private bool TryReadSqlStringValue(SqlBuffer value, byte type, int length, Encoding encoding, bool isPlp, TdsParserStateObject stateObj) {
switch (type) {
case TdsEnums.SQLCHAR:
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLVARCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
// If bigvarchar(max), we only read the first chunk here,
// expecting the caller to read the rest
if (encoding == null) {
// if hitting 7.0 server, encoding will be null in metadata for columns or return values since
// 7.0 has no support for multiple code pages in data - single code page support only
encoding = _defaultEncoding;
}
string stringValue;
if (!stateObj.TryReadStringWithEncoding(length, encoding, isPlp, out stringValue)) {
return false;
}
value.SetToString(stringValue);
break;
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
{
String s = null;
if (isPlp) {
char[] cc = null;
if (!TryReadPlpUnicodeChars(ref cc, 0, length >> 1, stateObj, out length)) {
return false;
}
if (length > 0) {
s = new String(cc, 0, length);
}
else {
s = ADP.StrEmpty;
}
}
else {
if (!stateObj.TryReadString(length >> 1, out s)) {
return false;
}
}
value.SetToString(s);
break;
}
default:
Debug.Assert(false, "Unknown tds type for SqlString!" + type.ToString(CultureInfo.InvariantCulture));
break;
}
return true;
}
/// <summary>
/// Deserializes the unencrypted bytes into a value based on the target type info.
/// </summary>
internal bool DeserializeUnencryptedValue (SqlBuffer value, byte[] unencryptedBytes, SqlMetaDataPriv md, TdsParserStateObject stateObj, byte normalizationVersion) {
if (normalizationVersion != 0x01) {
throw SQL.UnsupportedNormalizationVersion(normalizationVersion);
}
byte tdsType = md.baseTI.tdsType;
int length = unencryptedBytes.Length;
// For normalized types, the length and scale of the actual type might be different than the value's.
int denormalizedLength = md.baseTI.length;
byte denormalizedScale = md.baseTI.scale;
Debug.Assert (false == md.baseTI.isEncrypted, "Double encryption detected");
switch (tdsType) {
// We normalize to allow conversion across data types. All data types below are serialized into a BIGINT.
case TdsEnums.SQLBIT:
case TdsEnums.SQLBITN:
case TdsEnums.SQLINTN:
case TdsEnums.SQLINT1:
case TdsEnums.SQLINT2:
case TdsEnums.SQLINT4:
case TdsEnums.SQLINT8:
Debug.Assert(length == 8, "invalid length for SqlInt64 type!");
byte byteValue;
long longValue;
if (unencryptedBytes.Length != 8) {
return false;
}
longValue = BitConverter.ToInt64(unencryptedBytes, 0);
if (tdsType == TdsEnums.SQLBIT ||
tdsType == TdsEnums.SQLBITN) {
value.Boolean = (longValue != 0);
break;
}
if (tdsType == TdsEnums.SQLINT1 || denormalizedLength == 1)
value.Byte = (byte)longValue;
else if (tdsType == TdsEnums.SQLINT2 || denormalizedLength == 2)
value.Int16 = (Int16)longValue;
else if (tdsType == TdsEnums.SQLINT4 || denormalizedLength == 4)
value.Int32 = (Int32)longValue;
else
value.Int64 = longValue;
break;
case TdsEnums.SQLFLTN:
if (length == 4) {
goto case TdsEnums.SQLFLT4;
}
else {
goto case TdsEnums.SQLFLT8;
}
case TdsEnums.SQLFLT4:
Debug.Assert(length == 4, "invalid length for SqlSingle type!");
float singleValue;
if (unencryptedBytes.Length != 4) {
return false;
}
singleValue = BitConverter.ToSingle(unencryptedBytes, 0);
value.Single = singleValue;
break;
case TdsEnums.SQLFLT8:
double doubleValue;
if (unencryptedBytes.Length != 8) {
return false;
}
doubleValue = BitConverter.ToDouble(unencryptedBytes, 0);
value.Double = doubleValue;
break;
// We normalize to allow conversion across data types. SMALLMONEY is serialized into a MONEY.
case TdsEnums.SQLMONEYN:
case TdsEnums.SQLMONEY4:
case TdsEnums.SQLMONEY:
{
int mid;
uint lo;
if (unencryptedBytes.Length != 8) {
return false;
}
mid = BitConverter.ToInt32(unencryptedBytes, 0);
lo = BitConverter.ToUInt32(unencryptedBytes, 4);
long l = (((long)mid) << 0x20) + ((long)lo);
value.SetToMoney(l);
break;
}
case TdsEnums.SQLDATETIMN:
if (length == 4) {
goto case TdsEnums.SQLDATETIM4;
}
else {
goto case TdsEnums.SQLDATETIME;
}
case TdsEnums.SQLDATETIM4:
ushort daypartShort, timepartShort;
if (unencryptedBytes.Length != 4) {
return false;
}
daypartShort = (UInt16)((unencryptedBytes[1] << 8) + unencryptedBytes[0]);
timepartShort = (UInt16)((unencryptedBytes[3] << 8) + unencryptedBytes[2]);
value.SetToDateTime(daypartShort, timepartShort * SqlDateTime.SQLTicksPerMinute);
break;
case TdsEnums.SQLDATETIME:
int daypart;
uint timepart;
if (unencryptedBytes.Length != 8) {
return false;
}
daypart = BitConverter.ToInt32(unencryptedBytes, 0);
timepart = BitConverter.ToUInt32(unencryptedBytes, 4);
value.SetToDateTime(daypart, (int)timepart);
break;
case TdsEnums.SQLUNIQUEID:
{
Debug.Assert(length == 16, "invalid length for SqlGuid type!");
value.SqlGuid = new SqlGuid(unencryptedBytes, true); // doesn't copy the byte array
break;
}
case TdsEnums.SQLBINARY:
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLVARBINARY:
case TdsEnums.SQLIMAGE:
{
// Note: Better not come here with plp data!!
Debug.Assert(length <= TdsEnums.MAXSIZE, "Plp data decryption attempted");
// If this is a fixed length type, pad with zeros to get to the fixed length size.
if (tdsType == TdsEnums.SQLBINARY || tdsType == TdsEnums.SQLBIGBINARY) {
byte[] bytes = new byte[md.baseTI.length];
Buffer.BlockCopy(unencryptedBytes, 0, bytes, 0, unencryptedBytes.Length);
unencryptedBytes = bytes;
}
value.SqlBinary = new SqlBinary(unencryptedBytes, true); // doesn't copy the byte array
break;
}
case TdsEnums.SQLDECIMALN:
case TdsEnums.SQLNUMERICN:
// Check the sign from the first byte.
int index = 0;
byteValue = unencryptedBytes[index++];
bool fPositive = (1 == byteValue);
// Now read the 4 next integers which contain the actual value.
length = checked((int)length-1);
int[] bits = new int[4];
int decLength = length>>2;
for (int i = 0; i < decLength; i++) {
// up to 16 bytes of data following the sign byte
bits[i] = BitConverter.ToInt32(unencryptedBytes, index);
index += 4;
}
value.SetToDecimal (md.baseTI.precision, md.baseTI.scale, fPositive, bits);
break;
case TdsEnums.SQLCHAR:
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLVARCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
{
System.Text.Encoding encoding = md.baseTI.encoding;
if (null == encoding) {
encoding = _defaultEncoding;
}
if (null == encoding) {
ThrowUnsupportedCollationEncountered(stateObj);
}
string strValue = encoding.GetString(unencryptedBytes, 0, length);
// If this is a fixed length type, pad with spaces to get to the fixed length size.
if (tdsType == TdsEnums.SQLCHAR || tdsType == TdsEnums.SQLBIGCHAR) {
strValue = strValue.PadRight(md.baseTI.length);
}
value.SetToString(strValue);
break;
}
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
{
string strValue = System.Text.Encoding.Unicode.GetString(unencryptedBytes, 0, length);
// If this is a fixed length type, pad with spaces to get to the fixed length size.
if (tdsType == TdsEnums.SQLNCHAR) {
strValue = strValue.PadRight(md.baseTI.length / ADP.CharSize);
}
value.SetToString(strValue);
break;
}
case TdsEnums.SQLDATE:
Debug.Assert(length == 3, "invalid length for date type!");
value.SetToDate(unencryptedBytes);
break;
case TdsEnums.SQLTIME:
// We normalize to maximum precision to allow conversion across different precisions.
Debug.Assert(length == 5, "invalid length for time type!");
value.SetToTime(unencryptedBytes, length, TdsEnums.MAX_TIME_SCALE, denormalizedScale);
break;
case TdsEnums.SQLDATETIME2:
// We normalize to maximum precision to allow conversion across different precisions.
Debug.Assert(length == 8, "invalid length for datetime2 type!");
value.SetToDateTime2(unencryptedBytes, length, TdsEnums.MAX_TIME_SCALE, denormalizedScale);
break;
case TdsEnums.SQLDATETIMEOFFSET:
// We normalize to maximum precision to allow conversion across different precisions.
Debug.Assert(length == 10, "invalid length for datetimeoffset type!");
value.SetToDateTimeOffset(unencryptedBytes, length, TdsEnums.MAX_TIME_SCALE, denormalizedScale);
break;
default:
MetaType metaType = md.baseTI.metaType;
// If we don't have a metatype already, construct one to get the proper type name.
if (metaType == null) {
metaType = MetaType.GetSqlDataType(tdsType, userType:0, length:length);
}
throw SQL.UnsupportedDatatypeEncryption(metaType.TypeName);
}
return true;
}
internal bool TryReadSqlValue(SqlBuffer value,
SqlMetaDataPriv md,
int length,
TdsParserStateObject stateObj,
SqlCommandColumnEncryptionSetting columnEncryptionOverride,
string columnName) {
bool isPlp = md.metaType.IsPlp;
byte tdsType = md.tdsType;
Debug.Assert(isPlp || !IsNull(md.metaType, (ulong)length), "null value should not get here!");
if (isPlp) {
// We must read the column value completely, no matter what length is passed in
length = Int32.MaxValue;
}
//DEVNOTE: When modifying the following routines (for deserialization) please pay attention to
// deserialization code in DecryptWithKey () method and modify it accordingly.
switch (tdsType) {
case TdsEnums.SQLDECIMALN:
case TdsEnums.SQLNUMERICN:
if (!TryReadSqlDecimal(value, length, md.precision, md.scale, stateObj)) {
return false;
}
break;
case TdsEnums.SQLUDT:
case TdsEnums.SQLBINARY:
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLVARBINARY:
case TdsEnums.SQLIMAGE:
byte[] b = null;
// If varbinary(max), we only read the first chunk here, expecting the caller to read the rest
if (isPlp) {
// If we are given -1 for length, then we read the entire value,
// otherwise only the requested amount, usually first chunk.
int ignored;
if (!stateObj.TryReadPlpBytes(ref b, 0, length, out ignored)) {
return false;
}
}
else {
//Debug.Assert(length > 0 && length < (long)(Int32.MaxValue), "Bad length for column");
b = new byte[length];
if (!stateObj.TryReadByteArray(b, 0, length)) {
return false;
}
}
if (md.isEncrypted
&& ((columnEncryptionOverride == SqlCommandColumnEncryptionSetting.Enabled
|| columnEncryptionOverride == SqlCommandColumnEncryptionSetting.ResultSetOnly)
|| (columnEncryptionOverride == SqlCommandColumnEncryptionSetting.UseConnectionSetting
&& _connHandler != null && _connHandler.ConnectionOptions != null
&& _connHandler.ConnectionOptions.ColumnEncryptionSetting == SqlConnectionColumnEncryptionSetting.Enabled))) {
try {
// CipherInfo is present, decrypt and read
byte[] unencryptedBytes = SqlSecurityUtility.DecryptWithKey(b, md.cipherMD, _connHandler.ConnectionOptions.DataSource);
if (unencryptedBytes != null) {
DeserializeUnencryptedValue(value, unencryptedBytes, md, stateObj, md.NormalizationRuleVersion);
}
}
catch (Exception e) {
throw SQL.ColumnDecryptionFailed(columnName, null, e);
}
}
else {
value.SqlBinary = new SqlBinary(b, true); // doesn't copy the byte array
}
break;
case TdsEnums.SQLCHAR:
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLVARCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
if (!TryReadSqlStringValue(value, tdsType, length, md.encoding, isPlp, stateObj)) {
return false;
}
break;
case TdsEnums.SQLXMLTYPE:
// We store SqlCachedBuffer here, so that we can return either SqlBinary, SqlString or SqlXmlReader.
SqlCachedBuffer sqlBuf;
if (!SqlCachedBuffer.TryCreate(md, this, stateObj, out sqlBuf)) {
return false;
}
value.SqlCachedBuffer = sqlBuf;
break;
case TdsEnums.SQLDATE:
case TdsEnums.SQLTIME:
case TdsEnums.SQLDATETIME2:
case TdsEnums.SQLDATETIMEOFFSET:
if (!TryReadSqlDateTime(value, tdsType, length, md.scale, stateObj)) {
return false;
}
break;
default:
Debug.Assert(!isPlp, "ReadSqlValue calling ReadSqlValueInternal with plp data");
if (!TryReadSqlValueInternal(value, tdsType, length, stateObj)) {
return false;
}
break;
}
Debug.Assert((stateObj._longlen == 0) && (stateObj._longlenleft == 0), "ReadSqlValue did not read plp field completely, longlen =" + stateObj._longlen.ToString((IFormatProvider)null) + ",longlenleft=" + stateObj._longlenleft.ToString((IFormatProvider)null));
return true;
}
private bool TryReadSqlDateTime(SqlBuffer value, byte tdsType, int length, byte scale, TdsParserStateObject stateObj) {
byte[] datetimeBuffer = new byte[length];
if (!stateObj.TryReadByteArray(datetimeBuffer, 0, length)) {
return false;
}
switch (tdsType) {
case TdsEnums.SQLDATE:
Debug.Assert(length == 3, "invalid length for date type!");
value.SetToDate(datetimeBuffer);
break;
case TdsEnums.SQLTIME:
Debug.Assert(3 <= length && length <= 5, "invalid length for time type!");
value.SetToTime(datetimeBuffer, length, scale, scale);
break;
case TdsEnums.SQLDATETIME2:
Debug.Assert(6 <= length && length <= 8, "invalid length for datetime2 type!");
value.SetToDateTime2(datetimeBuffer, length, scale, scale);
break;
case TdsEnums.SQLDATETIMEOFFSET:
Debug.Assert(8 <= length && length <= 10, "invalid length for datetimeoffset type!");
value.SetToDateTimeOffset(datetimeBuffer, length, scale, scale);
break;
default:
Debug.Assert(false, "ReadSqlDateTime is called with the wrong tdsType");
break;
}
return true;
}
internal bool TryReadSqlValueInternal(SqlBuffer value, byte tdsType, int length, TdsParserStateObject stateObj) {
switch (tdsType) {
case TdsEnums.SQLBIT:
case TdsEnums.SQLBITN:
Debug.Assert(length == 1, "invalid length for SqlBoolean type!");
byte byteValue;
if (!stateObj.TryReadByte(out byteValue)) {
return false;
}
value.Boolean = (byteValue != 0);
break;
case TdsEnums.SQLINTN:
if (length == 1) {
goto case TdsEnums.SQLINT1;
}
else if (length == 2) {
goto case TdsEnums.SQLINT2;
}
else if (length == 4) {
goto case TdsEnums.SQLINT4;
}
else {
goto case TdsEnums.SQLINT8;
}
case TdsEnums.SQLINT1:
Debug.Assert(length == 1, "invalid length for SqlByte type!");
if (!stateObj.TryReadByte(out byteValue)) {
return false;
}
value.Byte = byteValue;
break;
case TdsEnums.SQLINT2:
Debug.Assert(length == 2, "invalid length for SqlInt16 type!");
short shortValue;
if (!stateObj.TryReadInt16(out shortValue)) {
return false;
}
value.Int16 = shortValue;
break;
case TdsEnums.SQLINT4:
Debug.Assert(length == 4, "invalid length for SqlInt32 type!");
int intValue;
if (!stateObj.TryReadInt32(out intValue)) {
return false;
}
value.Int32 = intValue;
break;
case TdsEnums.SQLINT8:
Debug.Assert(length == 8, "invalid length for SqlInt64 type!");
long longValue;
if (!stateObj.TryReadInt64(out longValue)) {
return false;
}
value.Int64 = longValue;
break;
case TdsEnums.SQLFLTN:
if (length == 4) {
goto case TdsEnums.SQLFLT4;
}
else {
goto case TdsEnums.SQLFLT8;
}
case TdsEnums.SQLFLT4:
Debug.Assert(length == 4, "invalid length for SqlSingle type!");
float singleValue;
if (!stateObj.TryReadSingle(out singleValue)) {
return false;
}
value.Single = singleValue;
break;
case TdsEnums.SQLFLT8:
Debug.Assert(length == 8, "invalid length for SqlDouble type!");
double doubleValue;
if (!stateObj.TryReadDouble(out doubleValue)) {
return false;
}
value.Double = doubleValue;
break;
case TdsEnums.SQLMONEYN:
if (length == 4) {
goto case TdsEnums.SQLMONEY4;
}
else {
goto case TdsEnums.SQLMONEY;
}
case TdsEnums.SQLMONEY:
{
int mid;
uint lo;
if (!stateObj.TryReadInt32(out mid)) {
return false;
}
if (!stateObj.TryReadUInt32(out lo)) {
return false;
}
long l = (((long)mid) << 0x20) + ((long)lo);
value.SetToMoney(l);
break;
}
case TdsEnums.SQLMONEY4:
if (!stateObj.TryReadInt32(out intValue)) {
return false;
}
value.SetToMoney(intValue);
break;
case TdsEnums.SQLDATETIMN:
if (length == 4) {
goto case TdsEnums.SQLDATETIM4;
}
else {
goto case TdsEnums.SQLDATETIME;
}
case TdsEnums.SQLDATETIM4:
ushort daypartShort, timepartShort;
if (!stateObj.TryReadUInt16(out daypartShort)) {
return false;
}
if (!stateObj.TryReadUInt16(out timepartShort)) {
return false;
}
value.SetToDateTime(daypartShort, timepartShort * SqlDateTime.SQLTicksPerMinute);
break;
case TdsEnums.SQLDATETIME:
int daypart;
uint timepart;
if (!stateObj.TryReadInt32(out daypart)) {
return false;
}
if (!stateObj.TryReadUInt32(out timepart)) {
return false;
}
value.SetToDateTime(daypart, (int)timepart);
break;
case TdsEnums.SQLUNIQUEID:
{
Debug.Assert(length == 16, "invalid length for SqlGuid type!");
byte[] b = new byte[length];
if (!stateObj.TryReadByteArray(b, 0, length)) {
return false;
}
value.SqlGuid = new SqlGuid(b, true); // doesn't copy the byte array
break;
}
case TdsEnums.SQLBINARY:
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLVARBINARY:
case TdsEnums.SQLIMAGE:
{
// Note: Better not come here with plp data!!
Debug.Assert(length <= TdsEnums.MAXSIZE);
byte[] b = new byte[length];
if (!stateObj.TryReadByteArray(b, 0, length)) {
return false;
}
value.SqlBinary = new SqlBinary(b, true); // doesn't copy the byte array
break;
}
case TdsEnums.SQLVARIANT:
if (!TryReadSqlVariant(value, length, stateObj)) {
return false;
}
break;
default:
Debug.Assert(false, "Unknown SqlType!" + tdsType.ToString(CultureInfo.InvariantCulture));
break;
} // switch
return true;
}
//
// Read in a SQLVariant
//
// SQLVariant looks like:
// struct
// {
// BYTE TypeTag
// BYTE cbPropBytes
// BYTE[] Properties
// BYTE[] DataVal
// }
internal bool TryReadSqlVariant(SqlBuffer value, int lenTotal, TdsParserStateObject stateObj) {
Debug.Assert(_isShiloh == true, "Shouldn't be dealing with sql_variaint in pre-SQL2000 server!");
// get the SQLVariant type
byte type;
if (!stateObj.TryReadByte(out type)) {
return false;
}
ushort lenMax = 0; // maximum lenData of value inside variant
// read cbPropBytes
byte cbPropsActual;
if (!stateObj.TryReadByte(out cbPropsActual)) {
return false;
}
MetaType mt = MetaType.GetSqlDataType(type, 0 /*no user datatype*/, 0 /* no lenData, non-nullable type */);
byte cbPropsExpected = mt.PropBytes;
int lenConsumed = TdsEnums.SQLVARIANT_SIZE + cbPropsActual; // type, count of propBytes, and actual propBytes
int lenData = lenTotal - lenConsumed; // length of actual data
// read known properties and skip unknown properties
Debug.Assert(cbPropsActual >= cbPropsExpected, "cbPropsActual is less that cbPropsExpected!");
//
// now read the value
//
switch (type) {
case TdsEnums.SQLBIT:
case TdsEnums.SQLINT1:
case TdsEnums.SQLINT2:
case TdsEnums.SQLINT4:
case TdsEnums.SQLINT8:
case TdsEnums.SQLFLT4:
case TdsEnums.SQLFLT8:
case TdsEnums.SQLMONEY:
case TdsEnums.SQLMONEY4:
case TdsEnums.SQLDATETIME:
case TdsEnums.SQLDATETIM4:
case TdsEnums.SQLUNIQUEID:
if (!TryReadSqlValueInternal(value, type, lenData, stateObj)) {
return false;
}
break;
case TdsEnums.SQLDECIMALN:
case TdsEnums.SQLNUMERICN:
{
Debug.Assert(cbPropsExpected == 2, "SqlVariant: invalid PropBytes for decimal/numeric type!");
byte precision;
if (!stateObj.TryReadByte(out precision)) {
return false;
}
byte scale;
if (!stateObj.TryReadByte(out scale)) {
return false;
}
// skip over unknown properties
if (cbPropsActual > cbPropsExpected) {
if (!stateObj.TrySkipBytes(cbPropsActual - cbPropsExpected)) {
return false;
}
}
if (!TryReadSqlDecimal(value, TdsEnums.MAX_NUMERIC_LEN, precision, scale, stateObj)) {
return false;
}
break;
}
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
//Debug.Assert(TdsEnums.VARNULL == lenData, "SqlVariant: data length for Binary indicates null?");
Debug.Assert(cbPropsExpected == 2, "SqlVariant: invalid PropBytes for binary type!");
if (!stateObj.TryReadUInt16(out lenMax)) {
return false;
}
Debug.Assert(lenMax != TdsEnums.SQL_USHORTVARMAXLEN, "bigvarbinary(max) in a sqlvariant");
// skip over unknown properties
if (cbPropsActual > cbPropsExpected) {
if (!stateObj.TrySkipBytes(cbPropsActual - cbPropsExpected)) {
return false;
}
}
goto case TdsEnums.SQLBIT;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
{
Debug.Assert(cbPropsExpected == 7, "SqlVariant: invalid PropBytes for character type!");
SqlCollation collation;
if (!TryProcessCollation(stateObj, out collation)) {
return false;
}
if (!stateObj.TryReadUInt16(out lenMax)) {
return false;
}
Debug.Assert(lenMax != TdsEnums.SQL_USHORTVARMAXLEN, "bigvarchar(max) or nvarchar(max) in a sqlvariant");
// skip over unknown properties
if (cbPropsActual > cbPropsExpected) {
if (!stateObj.TrySkipBytes(cbPropsActual - cbPropsExpected)) {
return false;
}
}
Encoding encoding = Encoding.GetEncoding(GetCodePage(collation, stateObj));
if (!TryReadSqlStringValue(value, type, lenData, encoding, false, stateObj)) {
return false;
}
break;
}
case TdsEnums.SQLDATE:
if (!TryReadSqlDateTime(value, type, lenData, 0, stateObj)) {
return false;
}
break;
case TdsEnums.SQLTIME:
case TdsEnums.SQLDATETIME2:
case TdsEnums.SQLDATETIMEOFFSET:
{
Debug.Assert(cbPropsExpected == 1, "SqlVariant: invalid PropBytes for time/datetime2/datetimeoffset type!");
byte scale;
if (!stateObj.TryReadByte(out scale)) {
return false;
}
// skip over unknown properties
if (cbPropsActual > cbPropsExpected) {
if (!stateObj.TrySkipBytes(cbPropsActual - cbPropsExpected)) {
return false;
}
}
if (!TryReadSqlDateTime(value, type, lenData, scale, stateObj)) {
return false;
}
break;
}
default:
Debug.Assert(false, "Unknown tds type in SqlVariant!" + type.ToString(CultureInfo.InvariantCulture));
break;
} // switch
return true;
}
//
// Translates a com+ object -> SqlVariant
// when the type is ambiguous, we always convert to the bigger type
// note that we also write out the maxlen and actuallen members (4 bytes each)
// in addition to the SQLVariant structure
//
internal Task WriteSqlVariantValue(object value, int length, int offset, TdsParserStateObject stateObj, bool canAccumulate = true) {
Debug.Assert(_isShiloh == true, "Shouldn't be dealing with sql_variant in pre-SQL2000 server!");
// handle null values
if (ADP.IsNull(value)) {
WriteInt(TdsEnums.FIXEDNULL, stateObj); //maxlen
WriteInt(TdsEnums.FIXEDNULL, stateObj); //actuallen
return null;
}
MetaType mt = MetaType.GetMetaTypeFromValue(value);
// Special case data type correction for SqlMoney inside a SqlVariant.
if ((TdsEnums.SQLNUMERICN == mt.TDSType) && (8 == length)) {
// The caller will coerce all SqlTypes to native CLR types, which means SqlMoney will
// coerce to decimal/SQLNUMERICN (via SqlMoney.Value call). In the case where the original
// value was SqlMoney the caller will also pass in the metadata length for the SqlMoney type
// which is 8 bytes. To honor the intent of the caller here we coerce this special case
// input back to SqlMoney from decimal/SQLNUMERICN.
mt = MetaType.GetMetaTypeFromValue(new SqlMoney((decimal)value));
}
if (mt.IsAnsiType) {
length = GetEncodingCharLength((string)value, length, 0, _defaultEncoding);
}
// max and actual len are equal to
// SQLVARIANTSIZE {type (1 byte) + cbPropBytes (1 byte)} + cbPropBytes + length (actual length of data in bytes)
WriteInt(TdsEnums.SQLVARIANT_SIZE + mt.PropBytes + length, stateObj); // maxLen
WriteInt(TdsEnums.SQLVARIANT_SIZE + mt.PropBytes + length, stateObj); // actualLen
// write the SQLVariant header (type and cbPropBytes)
stateObj.WriteByte(mt.TDSType);
stateObj.WriteByte(mt.PropBytes);
// now write the actual PropBytes and data
switch (mt.TDSType) {
case TdsEnums.SQLFLT4:
WriteFloat((Single)value, stateObj);
break;
case TdsEnums.SQLFLT8:
WriteDouble((Double)value, stateObj);
break;
case TdsEnums.SQLINT8:
WriteLong((Int64)value, stateObj);
break;
case TdsEnums.SQLINT4:
WriteInt((Int32)value, stateObj);
break;
case TdsEnums.SQLINT2:
WriteShort((Int16)value, stateObj);
break;
case TdsEnums.SQLINT1:
stateObj.WriteByte((byte)value);
break;
case TdsEnums.SQLBIT:
if ((bool)value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
case TdsEnums.SQLBIGVARBINARY:
{
byte[] b = (byte[])value;
WriteShort(length, stateObj); // propbytes: varlen
return stateObj.WriteByteArray(b, length, offset, canAccumulate);
}
case TdsEnums.SQLBIGVARCHAR:
{
string s = (string)value;
WriteUnsignedInt(_defaultCollation.info, stateObj); // propbytes: collation.Info
stateObj.WriteByte(_defaultCollation.sortId); // propbytes: collation.SortId
WriteShort(length, stateObj); // propbyte: varlen
return WriteEncodingChar(s, _defaultEncoding, stateObj, canAccumulate);
}
case TdsEnums.SQLUNIQUEID:
{
System.Guid guid = (System.Guid)value;
byte[] b = guid.ToByteArray();
Debug.Assert((length == b.Length) && (length == 16), "Invalid length for guid type in com+ object");
stateObj.WriteByteArray(b, length, 0);
break;
}
case TdsEnums.SQLNVARCHAR:
{
string s = (string)value;
WriteUnsignedInt(_defaultCollation.info, stateObj); // propbytes: collation.Info
stateObj.WriteByte(_defaultCollation.sortId); // propbytes: collation.SortId
WriteShort(length, stateObj); // propbyte: varlen
// string takes cchar, not cbyte so convert
length >>= 1;
return WriteString(s, length, offset, stateObj, canAccumulate);
}
case TdsEnums.SQLDATETIME:
{
TdsDateTime dt = MetaType.FromDateTime((DateTime)value, 8);
WriteInt(dt.days, stateObj);
WriteInt(dt.time, stateObj);
break;
}
case TdsEnums.SQLMONEY:
{
WriteCurrency((Decimal)value, 8, stateObj);
break;
}
case TdsEnums.SQLNUMERICN:
{
stateObj.WriteByte(mt.Precision); //propbytes: precision
stateObj.WriteByte((byte)((Decimal.GetBits((Decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale
WriteDecimal((Decimal)value, stateObj);
break;
}
case TdsEnums.SQLTIME:
stateObj.WriteByte(mt.Scale); //propbytes: scale
WriteTime((TimeSpan)value, mt.Scale, length, stateObj);
break;
case TdsEnums.SQLDATETIMEOFFSET:
stateObj.WriteByte(mt.Scale); //propbytes: scale
WriteDateTimeOffset((DateTimeOffset)value, mt.Scale, length, stateObj);
break;
default:
Debug.Assert(false, "unknown tds type for sqlvariant!");
break;
} // switch
// return point for accumulated writes, note: non-accumulated writes returned from their case statements
return null;
}
// todo: since we now know the difference between SqlWriteVariantValue and SqlWriteRowDataVariant we should consider
// combining these tow methods.
//
// Translates a com+ object -> SqlVariant
// when the type is ambiguous, we always convert to the bigger type
// note that we also write out the maxlen and actuallen members (4 bytes each)
// in addition to the SQLVariant structure
//
// Devnote: DataRows are preceeded by Metadata. The Metadata includes the MaxLen value.
// Therefore the sql_variant value must not include the MaxLength. This is the major difference
// between this method and WriteSqlVariantValue above.
//
internal Task WriteSqlVariantDataRowValue(object value, TdsParserStateObject stateObj, bool canAccumulate = true) {
Debug.Assert(_isShiloh == true, "Shouldn't be dealing with sql_variant in pre-SQL2000 server!");
// handle null values
if ((null == value) || (DBNull.Value == value)) {
WriteInt(TdsEnums.FIXEDNULL, stateObj);
return null;
}
MetaType metatype = MetaType.GetMetaTypeFromValue(value);
int length = 0;
if (metatype.IsAnsiType) {
length = GetEncodingCharLength((string)value, length, 0, _defaultEncoding);
}
switch (metatype.TDSType) {
case TdsEnums.SQLFLT4:
WriteSqlVariantHeader(6, metatype.TDSType, metatype.PropBytes, stateObj);
WriteFloat((Single)value, stateObj);
break;
case TdsEnums.SQLFLT8:
WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj);
WriteDouble((Double)value, stateObj);
break;
case TdsEnums.SQLINT8:
WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj);
WriteLong((Int64)value, stateObj);
break;
case TdsEnums.SQLINT4:
WriteSqlVariantHeader(6, metatype.TDSType, metatype.PropBytes, stateObj);
WriteInt((Int32)value, stateObj);
break;
case TdsEnums.SQLINT2:
WriteSqlVariantHeader(4, metatype.TDSType, metatype.PropBytes, stateObj);
WriteShort((Int16)value, stateObj);
break;
case TdsEnums.SQLINT1:
WriteSqlVariantHeader(3, metatype.TDSType, metatype.PropBytes, stateObj);
stateObj.WriteByte((byte)value);
break;
case TdsEnums.SQLBIT:
WriteSqlVariantHeader(3, metatype.TDSType, metatype.PropBytes, stateObj);
if ((bool)value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
case TdsEnums.SQLBIGVARBINARY:
{
byte[] b = (byte[])value;
length = b.Length;
WriteSqlVariantHeader(4 + length, metatype.TDSType, metatype.PropBytes, stateObj);
WriteShort(length, stateObj); // propbytes: varlen
return stateObj.WriteByteArray(b, length, 0, canAccumulate);
}
case TdsEnums.SQLBIGVARCHAR:
{
string s = (string)value;
length = s.Length;
WriteSqlVariantHeader(9 + length, metatype.TDSType, metatype.PropBytes, stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); // propbytes: collation.Info
stateObj.WriteByte(_defaultCollation.sortId); // propbytes: collation.SortId
WriteShort(length, stateObj);
return WriteEncodingChar(s, _defaultEncoding, stateObj, canAccumulate);
}
case TdsEnums.SQLUNIQUEID:
{
System.Guid guid = (System.Guid)value;
byte[] b = guid.ToByteArray();
length = b.Length;
Debug.Assert(length == 16, "Invalid length for guid type in com+ object");
WriteSqlVariantHeader(18, metatype.TDSType, metatype.PropBytes, stateObj);
stateObj.WriteByteArray(b, length, 0);
break;
}
case TdsEnums.SQLNVARCHAR:
{
string s = (string)value;
length = s.Length * 2;
WriteSqlVariantHeader(9 + length, metatype.TDSType, metatype.PropBytes, stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); // propbytes: collation.Info
stateObj.WriteByte(_defaultCollation.sortId); // propbytes: collation.SortId
WriteShort(length, stateObj); // propbyte: varlen
// string takes cchar, not cbyte so convert
length >>= 1;
return WriteString(s, length, 0, stateObj, canAccumulate);
}
case TdsEnums.SQLDATETIME:
{
TdsDateTime dt = MetaType.FromDateTime((DateTime)value, 8);
WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj);
WriteInt(dt.days, stateObj);
WriteInt(dt.time, stateObj);
break;
}
case TdsEnums.SQLMONEY:
{
WriteSqlVariantHeader(10, metatype.TDSType, metatype.PropBytes, stateObj);
WriteCurrency((Decimal)value, 8, stateObj);
break;
}
case TdsEnums.SQLNUMERICN:
{
WriteSqlVariantHeader(21, metatype.TDSType, metatype.PropBytes, stateObj);
stateObj.WriteByte(metatype.Precision); //propbytes: precision
stateObj.WriteByte((byte)((Decimal.GetBits((Decimal)value)[3] & 0x00ff0000) >> 0x10)); // propbytes: scale
WriteDecimal((Decimal)value, stateObj);
break;
}
case TdsEnums.SQLTIME:
WriteSqlVariantHeader(8, metatype.TDSType, metatype.PropBytes, stateObj);
stateObj.WriteByte(metatype.Scale); //propbytes: scale
WriteTime((TimeSpan)value, metatype.Scale, 5, stateObj);
break;
case TdsEnums.SQLDATETIMEOFFSET:
WriteSqlVariantHeader(13, metatype.TDSType, metatype.PropBytes, stateObj);
stateObj.WriteByte(metatype.Scale); //propbytes: scale
WriteDateTimeOffset((DateTimeOffset)value, metatype.Scale, 10, stateObj);
break;
default:
Debug.Assert(false, "unknown tds type for sqlvariant!");
break;
} // switch
// return point for accumualated writes, note: non-accumulated writes returned from their case statements
return null;
}
internal void WriteSqlVariantHeader(int length, byte tdstype, byte propbytes, TdsParserStateObject stateObj) {
WriteInt(length, stateObj);
stateObj.WriteByte(tdstype);
stateObj.WriteByte(propbytes);
}
internal void WriteSqlVariantDateTime2(DateTime value, TdsParserStateObject stateObj)
{
MSS.SmiMetaData dateTime2MetaData = MSS.SmiMetaData.DefaultDateTime2;
// NOTE: 3 bytes added here to support additional header information for variant - internal type, scale prop, scale
WriteSqlVariantHeader((int)(dateTime2MetaData.MaxLength + 3), TdsEnums.SQLDATETIME2, 1 /* one scale prop */, stateObj);
stateObj.WriteByte(dateTime2MetaData.Scale); //scale property
WriteDateTime2(value, dateTime2MetaData.Scale, (int)(dateTime2MetaData.MaxLength), stateObj);
}
internal void WriteSqlVariantDate(DateTime value, TdsParserStateObject stateObj)
{
MSS.SmiMetaData dateMetaData = MSS.SmiMetaData.DefaultDate;
// NOTE: 2 bytes added here to support additional header information for variant - internal type, scale prop (ignoring scale here)
WriteSqlVariantHeader((int)(dateMetaData.MaxLength + 2), TdsEnums.SQLDATE, 0 /* one scale prop */, stateObj);
WriteDate(value, stateObj);
}
private byte[] SerializeSqlMoney(SqlMoney value, int length, TdsParserStateObject stateObj) {
return SerializeCurrency(value.Value, length, stateObj);
}
private void WriteSqlMoney(SqlMoney value, int length, TdsParserStateObject stateObj) {
//
int[] bits = Decimal.GetBits(value.Value);
// this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by)
bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000)));
long l = ((long)(uint)bits[1]) << 0x20 | (uint)bits[0];
if (isNeg)
l = -l;
if (length == 4) {
Decimal decimalValue = value.Value;
// validate the value can be represented as a small money
if (decimalValue < TdsEnums.SQL_SMALL_MONEY_MIN || decimalValue > TdsEnums.SQL_SMALL_MONEY_MAX) {
throw SQL.MoneyOverflow(decimalValue.ToString(CultureInfo.InvariantCulture));
}
WriteInt((int)l, stateObj);
}
else {
WriteInt((int)(l >> 0x20), stateObj);
WriteInt((int)l, stateObj);
}
}
private byte[] SerializeCurrency(Decimal value, int length, TdsParserStateObject stateObj) {
SqlMoney m = new SqlMoney(value);
int[] bits = Decimal.GetBits(m.Value);
// this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by)
bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000)));
long l = ((long)(uint)bits[1]) << 0x20 | (uint)bits[0];
if (isNeg)
l = -l;
if (length == 4) {
// validate the value can be represented as a small money
if (value < TdsEnums.SQL_SMALL_MONEY_MIN || value > TdsEnums.SQL_SMALL_MONEY_MAX) {
throw SQL.MoneyOverflow(value.ToString(CultureInfo.InvariantCulture));
}
// We normalize to allow conversion across data types. SMALLMONEY is serialized into a MONEY.
length = 8;
}
Debug.Assert (8 == length, "invalid length in SerializeCurrency");
if (null == stateObj._bLongBytes) {
stateObj._bLongBytes = new byte[8];
}
byte[] bytes = stateObj._bLongBytes;
int current = 0;
byte[] bytesPart = SerializeInt((int)(l >> 0x20), stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
current += 4;
bytesPart = SerializeInt((int)l, stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
return bytes;
}
private void WriteCurrency(Decimal value, int length, TdsParserStateObject stateObj) {
SqlMoney m = new SqlMoney(value);
int[] bits = Decimal.GetBits(m.Value);
// this decimal should be scaled by 10000 (regardless of what the incoming decimal was scaled by)
bool isNeg = (0 != (bits[3] & unchecked((int)0x80000000)));
long l = ((long)(uint)bits[1]) << 0x20 | (uint)bits[0];
if (isNeg)
l = -l;
if (length == 4) {
// validate the value can be represented as a small money
if (value < TdsEnums.SQL_SMALL_MONEY_MIN || value > TdsEnums.SQL_SMALL_MONEY_MAX) {
throw SQL.MoneyOverflow(value.ToString(CultureInfo.InvariantCulture));
}
WriteInt((int)l, stateObj);
}
else {
WriteInt((int)(l >> 0x20), stateObj);
WriteInt((int)l, stateObj);
}
}
private byte[] SerializeDate(DateTime value) {
long days = value.Subtract(DateTime.MinValue).Days;
return SerializePartialLong(days, 3);
}
private void WriteDate(DateTime value, TdsParserStateObject stateObj) {
long days = value.Subtract(DateTime.MinValue).Days;
WritePartialLong(days, 3, stateObj);
}
private byte[] SerializeTime(TimeSpan value, byte scale, int length) {
if (0 > value.Ticks || value.Ticks >= TimeSpan.TicksPerDay) {
throw SQL.TimeOverflow(value.ToString());
}
long time = value.Ticks / TdsEnums.TICKS_FROM_SCALE[scale];
// We normalize to maximum precision to allow conversion across different precisions.
time = time * TdsEnums.TICKS_FROM_SCALE[scale];
length = TdsEnums.MAX_TIME_LENGTH;
return SerializePartialLong(time, length);
}
private void WriteTime(TimeSpan value, byte scale, int length, TdsParserStateObject stateObj) {
if (0 > value.Ticks || value.Ticks >= TimeSpan.TicksPerDay) {
throw SQL.TimeOverflow(value.ToString());
}
long time = value.Ticks / TdsEnums.TICKS_FROM_SCALE[scale];
WritePartialLong(time, length, stateObj);
}
private byte[] SerializeDateTime2(DateTime value, byte scale, int length) {
long time = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[scale]; // DateTime.TimeOfDay always returns a valid TimeSpan for Time
// We normalize to maximum precision to allow conversion across different precisions.
time = time * TdsEnums.TICKS_FROM_SCALE[scale];
length = TdsEnums.MAX_DATETIME2_LENGTH;
byte[] bytes = new byte[length];
byte[] bytesPart;
int current = 0;
bytesPart = SerializePartialLong(time, length - 3);
Buffer.BlockCopy(bytesPart, 0, bytes, current, length - 3);
current += length - 3;
bytesPart = SerializeDate(value);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 3);
return bytes;
}
private void WriteDateTime2(DateTime value, byte scale, int length, TdsParserStateObject stateObj) {
long time = value.TimeOfDay.Ticks / TdsEnums.TICKS_FROM_SCALE[scale]; // DateTime.TimeOfDay always returns a valid TimeSpan for Time
WritePartialLong(time, length - 3, stateObj);
WriteDate(value, stateObj);
}
private byte[] SerializeDateTimeOffset(DateTimeOffset value, byte scale, int length) {
byte[] bytesPart;
int current = 0;
bytesPart = SerializeDateTime2(value.UtcDateTime, scale, length - 2);
// We need to allocate the array after we have received the length of the serialized value
// since it might be higher due to normalization.
length = bytesPart.Length + 2;
byte[] bytes = new byte[length];
Buffer.BlockCopy(bytesPart, 0, bytes, current, length - 2);
current += length - 2;
Int16 offset = (Int16)value.Offset.TotalMinutes;
bytes[current++] = (byte)(offset & 0xff);
bytes[current++] = (byte)((offset >> 8) & 0xff);
return bytes;
}
private void WriteDateTimeOffset(DateTimeOffset value, byte scale, int length, TdsParserStateObject stateObj) {
WriteDateTime2(value.UtcDateTime, scale, length - 2, stateObj);
Int16 offset = (Int16)value.Offset.TotalMinutes;
stateObj.WriteByte((byte)(offset & 0xff));
stateObj.WriteByte((byte)((offset >> 8) & 0xff));
}
private bool TryReadSqlDecimal(SqlBuffer value, int length, byte precision, byte scale, TdsParserStateObject stateObj) {
byte byteValue;
if (!stateObj.TryReadByte(out byteValue)) {
return false;
}
bool fPositive = (1 == byteValue);
length = checked((int)length-1);
int[] bits;
if (!TryReadDecimalBits(length, stateObj, out bits)) {
return false;
}
value.SetToDecimal(precision, scale, fPositive, bits);
return true;
}
// @devnote: length should be size of decimal without the sign
// @devnote: sign should have already been read off the wire
private bool TryReadDecimalBits(int length, TdsParserStateObject stateObj, out int[] bits) {
bits = stateObj._decimalBits; // used alloc'd array if we have one already
int i;
if (null == bits)
bits = new int[4];
else {
for (i = 0; i < bits.Length; i++)
bits[i] = 0;
}
Debug.Assert((length > 0) &&
(length <= TdsEnums.MAX_NUMERIC_LEN - 1) &&
(length % 4 == 0), "decimal should have 4, 8, 12, or 16 bytes of data");
int decLength = length >> 2;
for (i = 0; i < decLength; i++) {
// up to 16 bytes of data following the sign byte
if (!stateObj.TryReadInt32(out bits[i])) {
return false;
}
}
return true;
}
static internal SqlDecimal AdjustSqlDecimalScale(SqlDecimal d, int newScale) {
if (d.Scale != newScale) {
return SqlDecimal.AdjustScale(d, newScale - d.Scale, false /* Don't round, truncate. MDAC 69229 */);
}
return d;
}
static internal decimal AdjustDecimalScale(decimal value, int newScale) {
int oldScale = (Decimal.GetBits(value)[3] & 0x00ff0000) >> 0x10;
if (newScale != oldScale) {
SqlDecimal num = new SqlDecimal(value);
num = SqlDecimal.AdjustScale(num, newScale - oldScale, false /* Don't round, truncate. MDAC 69229 */);
return num.Value;
}
return value;
}
internal byte[] SerializeSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj) {
if (null == stateObj._bDecimalBytes) {
stateObj._bDecimalBytes = new byte[17];
}
byte[] bytes = stateObj._bDecimalBytes;
int current = 0;
// sign
if (d.IsPositive)
bytes[current++] = 1;
else
bytes[current++] = 0;
byte[] bytesPart = SerializeUnsignedInt(d.m_data1, stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
current += 4;
bytesPart = SerializeUnsignedInt(d.m_data2, stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
current += 4;
bytesPart = SerializeUnsignedInt(d.m_data3, stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
current += 4;
bytesPart = SerializeUnsignedInt(d.m_data4, stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
return bytes;
}
internal void WriteSqlDecimal(SqlDecimal d, TdsParserStateObject stateObj) {
// sign
if (d.IsPositive)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
WriteUnsignedInt(d.m_data1, stateObj);
WriteUnsignedInt(d.m_data2, stateObj);
WriteUnsignedInt(d.m_data3, stateObj);
WriteUnsignedInt(d.m_data4, stateObj);
}
private byte[] SerializeDecimal(decimal value, TdsParserStateObject stateObj) {
int[] decimalBits = Decimal.GetBits(value);
if (null == stateObj._bDecimalBytes) {
stateObj._bDecimalBytes = new byte[17];
}
byte[] bytes = stateObj._bDecimalBytes;
int current = 0;
/*
Returns a binary representation of a Decimal. The return value is an integer
array with four elements. Elements 0, 1, and 2 contain the low, middle, and
high 32 bits of the 96-bit integer part of the Decimal. Element 3 contains
the scale factor and sign of the Decimal: bits 0-15 (the lower word) are
unused; bits 16-23 contain a value between 0 and 28, indicating the power of
10 to divide the 96-bit integer part by to produce the Decimal value; bits 24-
30 are unused; and finally bit 31 indicates the sign of the Decimal value, 0
meaning positive and 1 meaning negative.
SQLDECIMAL/SQLNUMERIC has a byte stream of:
struct {
BYTE sign; // 1 if positive, 0 if negative
BYTE data[];
}
For TDS 7.0 and above, there are always 17 bytes of data
*/
// write the sign (note that COM and SQL are opposite)
if (0x80000000 == (decimalBits[3] & 0x80000000))
bytes[current++] = 0;
else
bytes[current++] = 1;
byte[] bytesPart = SerializeInt(decimalBits[0], stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
current += 4;
bytesPart = SerializeInt(decimalBits[1], stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
current += 4;
bytesPart = SerializeInt(decimalBits[2], stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
current += 4;
bytesPart = SerializeInt(0, stateObj);
Buffer.BlockCopy(bytesPart, 0, bytes, current, 4);
return bytes;
}
private void WriteDecimal(decimal value, TdsParserStateObject stateObj) {
stateObj._decimalBits = Decimal.GetBits(value);
Debug.Assert(null != stateObj._decimalBits, "decimalBits should be filled in at TdsExecuteRPC time");
/*
Returns a binary representation of a Decimal. The return value is an integer
array with four elements. Elements 0, 1, and 2 contain the low, middle, and
high 32 bits of the 96-bit integer part of the Decimal. Element 3 contains
the scale factor and sign of the Decimal: bits 0-15 (the lower word) are
unused; bits 16-23 contain a value between 0 and 28, indicating the power of
10 to divide the 96-bit integer part by to produce the Decimal value; bits 24-
30 are unused; and finally bit 31 indicates the sign of the Decimal value, 0
meaning positive and 1 meaning negative.
SQLDECIMAL/SQLNUMERIC has a byte stream of:
struct {
BYTE sign; // 1 if positive, 0 if negative
BYTE data[];
}
For TDS 7.0 and above, there are always 17 bytes of data
*/
// write the sign (note that COM and SQL are opposite)
if (0x80000000 == (stateObj._decimalBits[3] & 0x80000000))
stateObj.WriteByte(0);
else
stateObj.WriteByte(1);
WriteInt(stateObj._decimalBits[0], stateObj);
WriteInt(stateObj._decimalBits[1], stateObj);
WriteInt(stateObj._decimalBits[2], stateObj);
WriteInt(0, stateObj);
}
private void WriteIdentifier(string s, TdsParserStateObject stateObj) {
if (null != s) {
stateObj.WriteByte(checked((byte)s.Length));
WriteString(s, stateObj);
}
else {
stateObj.WriteByte((byte)0);
}
}
private void WriteIdentifierWithShortLength(string s, TdsParserStateObject stateObj) {
if (null != s) {
WriteShort(checked((short)s.Length), stateObj);
WriteString(s, stateObj);
}
else {
WriteShort(0, stateObj);
}
}
private Task WriteString(string s, TdsParserStateObject stateObj, bool canAccumulate = true) {
return WriteString(s, s.Length, 0, stateObj, canAccumulate);
}
internal byte[] SerializeCharArray(char[] carr, int length, int offset) {
int cBytes = ADP.CharSize * length;
byte[] bytes = new byte[cBytes];
CopyCharsToBytes(carr, offset, bytes, 0, length);
return bytes;
}
internal Task WriteCharArray(char[] carr, int length, int offset, TdsParserStateObject stateObj, bool canAccumulate = true) {
int cBytes = ADP.CharSize * length;
// Perf shortcut: If it fits, write directly to the outBuff
if(cBytes < (stateObj._outBuff.Length - stateObj._outBytesUsed)) {
CopyCharsToBytes(carr, offset, stateObj._outBuff, stateObj._outBytesUsed, length);
stateObj._outBytesUsed += cBytes;
return null;
}
else {
if (stateObj._bTmp == null || stateObj._bTmp.Length < cBytes) {
stateObj._bTmp = new byte[cBytes];
}
CopyCharsToBytes(carr, offset, stateObj._bTmp, 0, length);
return stateObj.WriteByteArray(stateObj._bTmp, cBytes, 0, canAccumulate);
}
}
internal byte[] SerializeString(string s, int length, int offset) {
int cBytes = ADP.CharSize * length;
byte[] bytes = new byte[cBytes];
CopyStringToBytes(s, offset, bytes, 0, length);
return bytes;
}
internal Task WriteString(string s, int length, int offset, TdsParserStateObject stateObj, bool canAccumulate = true) {
int cBytes = ADP.CharSize * length;
// Perf shortcut: If it fits, write directly to the outBuff
if(cBytes < (stateObj._outBuff.Length - stateObj._outBytesUsed)) {
CopyStringToBytes(s, offset, stateObj._outBuff, stateObj._outBytesUsed, length);
stateObj._outBytesUsed += cBytes;
return null;
}
else {
if (stateObj._bTmp == null || stateObj._bTmp.Length < cBytes) {
stateObj._bTmp = new byte[cBytes];
}
CopyStringToBytes(s, offset, stateObj._bTmp, 0, length);
return stateObj.WriteByteArray(stateObj._bTmp, cBytes, 0, canAccumulate);
}
}
private unsafe static void CopyCharsToBytes(char[] source, int sourceOffset, byte[] dest, int destOffset, int charLength) {
// DEVNOTE: BE EXTREMELY CAREFULL in this method. Since it pins the buffers and copies the memory
// directly, it bypasses all of the CLR's usual array-bounds checking. For maintainability, the checks
// here should NOT be removed just because some other code will check them ahead of time.
if (charLength < 0) {
throw ADP.InvalidDataLength(charLength);
}
if (checked(sourceOffset + charLength) > source.Length || sourceOffset < 0) {
throw ADP.IndexOutOfRange(sourceOffset);
}
// charLength >= 0 & checked conversion implies byteLength >= 0
int byteLength = checked(charLength * ADP.CharSize);
if (checked(destOffset + byteLength) > dest.Length || destOffset < 0) {
throw ADP.IndexOutOfRange(destOffset);
}
fixed (char* sourcePtr = source) {
char* srcPtr = sourcePtr; // Can't increment the target of a Fixed statement
srcPtr += sourceOffset; // char* increments by sizeof(char)
fixed (byte* destinationPtr = dest) {
byte* destPtr = destinationPtr;
destPtr += destOffset;
NativeOledbWrapper.MemoryCopy((IntPtr)destPtr, (IntPtr)srcPtr, byteLength);
}
}
}
private unsafe static void CopyStringToBytes(string source, int sourceOffset, byte[] dest, int destOffset, int charLength) {
// DEVNOTE: BE EXTREMELY CAREFULL in this method. Since it pins the buffers and copies the memory
// directly, it bypasses all of the CLR's usual array-bounds checking. For maintainability, the checks
// here should NOT be removed just because some other code will check them ahead of time.
if (charLength < 0) {
throw ADP.InvalidDataLength(charLength);
}
if (checked(sourceOffset + charLength) > source.Length || sourceOffset < 0) {
throw ADP.IndexOutOfRange(sourceOffset);
}
// charLength >= 0 & checked conversion implies byteLength >= 0
int byteLength = checked(charLength * ADP.CharSize);
if (checked(destOffset + byteLength) > dest.Length || destOffset < 0) {
throw ADP.IndexOutOfRange(destOffset);
}
fixed (char* sourcePtr = source) {
char* srcPtr = sourcePtr; // Can't increment the target of a Fixed statement
srcPtr += sourceOffset; // char* increments by sizeof(char)
fixed (byte* destinationPtr = dest) {
byte* destPtr = destinationPtr;
destPtr += destOffset;
NativeOledbWrapper.MemoryCopy((IntPtr)destPtr, (IntPtr)srcPtr, byteLength);
}
}
}
private Task WriteEncodingChar(string s, Encoding encoding, TdsParserStateObject stateObj, bool canAccumulate = true) {
return WriteEncodingChar(s, s.Length, 0, encoding, stateObj, canAccumulate);
}
private byte[] SerializeEncodingChar(string s, int numChars, int offset, Encoding encoding) {
char[] charData;
byte[] byteData = null;
// if hitting 7.0 server, encoding will be null in metadata for columns or return values since
// 7.0 has no support for multiple code pages in data - single code page support only
if (encoding == null)
encoding = _defaultEncoding;
charData = s.ToCharArray(offset, numChars);
byteData = new byte[encoding.GetByteCount(charData, 0, charData.Length)];
encoding.GetBytes(charData, 0, charData.Length, byteData, 0);
return byteData;
}
private Task WriteEncodingChar(string s, int numChars, int offset, Encoding encoding, TdsParserStateObject stateObj, bool canAccumulate = true) {
char[] charData;
byte[] byteData;
// if hitting 7.0 server, encoding will be null in metadata for columns or return values since
// 7.0 has no support for multiple code pages in data - single code page support only
if (encoding == null)
encoding = _defaultEncoding;
charData = s.ToCharArray(offset, numChars);
// Optimization: if the entire string fits in the current buffer, then copy it directly
int bytesLeft = stateObj._outBuff.Length - stateObj._outBytesUsed;
if ((numChars <= bytesLeft) && (encoding.GetMaxByteCount(charData.Length) <= bytesLeft)) {
int bytesWritten = encoding.GetBytes(charData, 0, charData.Length, stateObj._outBuff, stateObj._outBytesUsed);
stateObj._outBytesUsed += bytesWritten;
return null;
}
else {
byteData = encoding.GetBytes(charData, 0, numChars);
Debug.Assert(byteData != null, "no data from encoding");
return stateObj.WriteByteArray(byteData, byteData.Length, 0, canAccumulate);
}
}
internal int GetEncodingCharLength(string value, int numChars, int charOffset, Encoding encoding) {
//
if (value == null || value == ADP.StrEmpty) {
return 0;
}
// if hitting 7.0 server, encoding will be null in metadata for columns or return values since
// 7.0 has no support for multiple code pages in data - single code page support only
if (encoding == null) {
if (null == _defaultEncoding) {
ThrowUnsupportedCollationEncountered(null);
}
encoding = _defaultEncoding;
}
char[] charData = value.ToCharArray(charOffset, numChars);
return encoding.GetByteCount(charData, 0, numChars);
}
//
// Returns the data stream length of the data identified by tds type or SqlMetaData returns
// Returns either the total size or the size of the first chunk for partially length prefixed types.
//
internal bool TryGetDataLength(SqlMetaDataPriv colmeta, TdsParserStateObject stateObj, out ulong length) {
// Handle Yukon specific tokens
if (_isYukon && colmeta.metaType.IsPlp) {
Debug.Assert(colmeta.tdsType == TdsEnums.SQLXMLTYPE ||
colmeta.tdsType == TdsEnums.SQLBIGVARCHAR ||
colmeta.tdsType == TdsEnums.SQLBIGVARBINARY ||
colmeta.tdsType == TdsEnums.SQLNVARCHAR ||
// Large UDTs is WinFS-only
colmeta.tdsType == TdsEnums.SQLUDT,
"GetDataLength:Invalid streaming datatype");
return stateObj.TryReadPlpLength(true, out length);
}
else {
int intLength;
if (!TryGetTokenLength(colmeta.tdsType, stateObj, out intLength)) {
length = 0;
return false;
}
length = (ulong)intLength;
return true;
}
}
//
// returns the token length of the token or tds type
// Returns -1 for partially length prefixed (plp) types for metadata info.
// DOES NOT handle plp data streams correctly!!!
// Plp data streams length information should be obtained from GetDataLength
//
internal bool TryGetTokenLength(byte token, TdsParserStateObject stateObj, out int tokenLength) {
Debug.Assert(token != 0, "0 length token!");
switch (token) { // rules about SQLLenMask no longer apply to new tokens (as of 7.4)
case TdsEnums.SQLFEATUREEXTACK:
tokenLength = -1;
return true;
case TdsEnums.SQLSESSIONSTATE:
case TdsEnums.SQLFEDAUTHINFO:
return stateObj.TryReadInt32(out tokenLength);
}
if (_isYukon) { // Handle Yukon specific exceptions
if (token == TdsEnums.SQLUDT) { // special case for UDTs
tokenLength = -1; // Should we return -1 or not call GetTokenLength for UDTs?
return true;
}
else if (token == TdsEnums.SQLRETURNVALUE) {
tokenLength = -1; // In Yukon, the RETURNVALUE token stream no longer has length
return true;
}
else if (token == TdsEnums.SQLXMLTYPE) {
ushort value;
if (!stateObj.TryReadUInt16(out value)) {
tokenLength = 0;
return false;
}
tokenLength = (int)value;
Debug.Assert(tokenLength == TdsEnums.SQL_USHORTVARMAXLEN, "Invalid token stream for xml datatype");
return true;
}
}
switch (token & TdsEnums.SQLLenMask) {
case TdsEnums.SQLFixedLen:
tokenLength = ((0x01 << ((token & 0x0c) >> 2))) & 0xff;
return true;
case TdsEnums.SQLZeroLen:
tokenLength = 0;
return true;
case TdsEnums.SQLVarLen:
case TdsEnums.SQLVarCnt:
if (0 != (token & 0x80)) {
ushort value;
if (!stateObj.TryReadUInt16(out value)) {
tokenLength = 0;
return false;
}
tokenLength = value;
return true;
}
else if (0 == (token & 0x0c)) {
//
if (!stateObj.TryReadInt32(out tokenLength)) {
return false;
}
return true;
}
else {
byte value;
if (!stateObj.TryReadByte(out value)) {
tokenLength = 0;
return false;
}
tokenLength = value;
return true;
}
default:
Debug.Assert(false, "Unknown token length!");
tokenLength = 0;
return true;
}
}
private void ProcessAttention(TdsParserStateObject stateObj) {
if (_state == TdsParserState.Closed || _state == TdsParserState.Broken){
return;
}
Debug.Assert(stateObj._attentionSent, "invalid attempt to ProcessAttention, attentionSent == false!");
// Attention processing scenarios:
// 1) EOM packet with header ST_AACK bit plus DONE with status DONE_ATTN
// 2) Packet without ST_AACK header bit but has DONE with status DONE_ATTN
// 3) Secondary timeout occurs while reading, break connection
// Since errors can occur and we need to cancel prior to throwing those errors, we
// cache away error state and then process TDS for the attention. We restore those
// errors after processing.
stateObj.StoreErrorAndWarningForAttention();
try {
// Call run loop to process looking for attention ack.
Run(RunBehavior.Attention, null, null, null, stateObj);
}
catch (Exception e) {
//
if (!ADP.IsCatchableExceptionType(e)) {
throw;
}
// If an exception occurs - break the connection.
// Attention error will not be thrown in this case by Run(), but other failures may.
ADP.TraceExceptionWithoutRethrow(e);
_state = TdsParserState.Broken;
_connHandler.BreakConnection();
throw;
}
stateObj.RestoreErrorAndWarningAfterAttention();
Debug.Assert(!stateObj._attentionSent, "Invalid attentionSent state at end of ProcessAttention");
}
static private int StateValueLength(int dataLen) {
return dataLen < 0xFF ? (dataLen + 1) : (dataLen + 5);
}
internal int WriteSessionRecoveryFeatureRequest(SessionData reconnectData, bool write /* if false just calculates the length */) {
int len = 1;
if (write) {
_physicalStateObj.WriteByte(TdsEnums.FEATUREEXT_SRECOVERY);
}
if (reconnectData == null) {
if (write) {
WriteInt(0, _physicalStateObj);
}
len += 4;
}
else {
Debug.Assert(reconnectData._unrecoverableStatesCount == 0, "Unrecoverable state count should be 0");
int initialLength = 0; // sizeof(DWORD) - length itself
initialLength += 1 + 2 * TdsParserStaticMethods.NullAwareStringLength(reconnectData._initialDatabase);
initialLength += 1 + 2 * TdsParserStaticMethods.NullAwareStringLength(reconnectData._initialLanguage);
initialLength += (reconnectData._initialCollation == null) ? 1 : 6;
for (int i = 0; i < SessionData._maxNumberOfSessionStates; i++) {
if (reconnectData._initialState[i] != null) {
initialLength += 1 /* StateId*/ + StateValueLength(reconnectData._initialState[i].Length);
}
}
int currentLength = 0; // sizeof(DWORD) - length itself
currentLength += 1 + 2 * (reconnectData._initialDatabase == reconnectData._database ? 0 : TdsParserStaticMethods.NullAwareStringLength(reconnectData._database));
currentLength += 1 + 2 * (reconnectData._initialLanguage == reconnectData._language ? 0 : TdsParserStaticMethods.NullAwareStringLength(reconnectData._language));
currentLength += (reconnectData._collation != null && !SqlCollation.AreSame(reconnectData._collation, reconnectData._initialCollation)) ? 6 : 1;
bool[] writeState = new bool[SessionData._maxNumberOfSessionStates];
for (int i = 0; i < SessionData._maxNumberOfSessionStates; i++) {
if (reconnectData._delta[i] != null) {
Debug.Assert(reconnectData._delta[i]._recoverable, "State should be recoverable");
writeState[i] = true;
if (reconnectData._initialState[i] != null && reconnectData._initialState[i].Length == reconnectData._delta[i]._dataLength) {
writeState[i] = false;
for (int j = 0; j < reconnectData._delta[i]._dataLength; j++) {
if (reconnectData._initialState[i][j] != reconnectData._delta[i]._data[j]) {
writeState[i] = true;
break;
}
}
}
if (writeState[i]) {
currentLength += 1 /* StateId*/ + StateValueLength(reconnectData._delta[i]._dataLength);
}
}
}
if (write) {
WriteInt(8 + initialLength + currentLength, _physicalStateObj); // length of data w/o total length (initil+current+2*sizeof(DWORD))
WriteInt(initialLength, _physicalStateObj);
WriteIdentifier(reconnectData._initialDatabase, _physicalStateObj);
WriteCollation(reconnectData._initialCollation, _physicalStateObj);
WriteIdentifier(reconnectData._initialLanguage, _physicalStateObj);
for (int i = 0; i < SessionData._maxNumberOfSessionStates; i++) {
if (reconnectData._initialState[i] != null) {
_physicalStateObj.WriteByte((byte)i);
if (reconnectData._initialState[i].Length < 0xFF) {
_physicalStateObj.WriteByte((byte)reconnectData._initialState[i].Length);
}
else {
_physicalStateObj.WriteByte(0xFF);
WriteInt(reconnectData._initialState[i].Length, _physicalStateObj);
}
_physicalStateObj.WriteByteArray(reconnectData._initialState[i], reconnectData._initialState[i].Length, 0);
}
}
WriteInt(currentLength, _physicalStateObj);
WriteIdentifier(reconnectData._database != reconnectData._initialDatabase ? reconnectData._database : null, _physicalStateObj);
WriteCollation(SqlCollation.AreSame(reconnectData._initialCollation, reconnectData._collation) ? null : reconnectData._collation, _physicalStateObj);
WriteIdentifier(reconnectData._language != reconnectData._initialLanguage ? reconnectData._language : null, _physicalStateObj);
for (int i = 0; i < SessionData._maxNumberOfSessionStates; i++) {
if (writeState[i]) {
_physicalStateObj.WriteByte((byte)i);
if (reconnectData._delta[i]._dataLength < 0xFF) {
_physicalStateObj.WriteByte((byte)reconnectData._delta[i]._dataLength);
}
else {
_physicalStateObj.WriteByte(0xFF);
WriteInt(reconnectData._delta[i]._dataLength, _physicalStateObj);
}
_physicalStateObj.WriteByteArray(reconnectData._delta[i]._data, reconnectData._delta[i]._dataLength, 0);
}
}
}
len += initialLength + currentLength + 12 /* length fields (initial, current, total) */;
}
return len;
}
internal int WriteFedAuthFeatureRequest(FederatedAuthenticationFeatureExtensionData fedAuthFeatureData,
bool write /* if false just calculates the length */) {
Debug.Assert(fedAuthFeatureData.libraryType == TdsEnums.FedAuthLibrary.ADAL || fedAuthFeatureData.libraryType == TdsEnums.FedAuthLibrary.SecurityToken,
"only fed auth library type ADAL and Security Token are supported in writing feature request");
int dataLen = 0;
int totalLen = 0;
// set dataLen and totalLen
switch (fedAuthFeatureData.libraryType) {
case TdsEnums.FedAuthLibrary.ADAL:
dataLen = 2; // length of feature data = 1 byte for library and echo + 1 byte for workflow
break;
case TdsEnums.FedAuthLibrary.SecurityToken:
Debug.Assert(fedAuthFeatureData.accessToken != null, "AccessToken should not be null.");
dataLen = 1 + sizeof(int) + fedAuthFeatureData.accessToken.Length; // length of feature data = 1 byte for library and echo, security token length and sizeof(int) for token lengh itself
break;
default:
Debug.Assert(false, "Unrecognized library type for fedauth feature extension request");
break;
}
totalLen = dataLen + 5; // length of feature id (1 byte), data length field (4 bytes), and feature data (dataLen)
// write feature id
if (write) {
_physicalStateObj.WriteByte(TdsEnums.FEATUREEXT_FEDAUTH);
// set options
byte options = 0x00;
// set upper 7 bits of options to indicate fed auth library type
switch (fedAuthFeatureData.libraryType) {
case TdsEnums.FedAuthLibrary.ADAL:
Debug.Assert(_connHandler._federatedAuthenticationInfoRequested == true, "_federatedAuthenticationInfoRequested field should be true");
options |= TdsEnums.FEDAUTHLIB_ADAL << 1;
break;
case TdsEnums.FedAuthLibrary.SecurityToken:
Debug.Assert(_connHandler._federatedAuthenticationRequested == true, "_federatedAuthenticationRequested field should be true");
options |= TdsEnums.FEDAUTHLIB_SECURITYTOKEN << 1;
break;
default:
Debug.Assert(false, "Unrecognized FedAuthLibrary type for feature extension request");
break;
}
options |= (byte)(fedAuthFeatureData.fedAuthRequiredPreLoginResponse == true ? 0x01 : 0x00);
// write dataLen and options
WriteInt(dataLen, _physicalStateObj);
_physicalStateObj.WriteByte(options);
// write workflow for FedAuthLibrary.ADAL
// write accessToken for FedAuthLibrary.SecurityToken
switch (fedAuthFeatureData.libraryType) {
case TdsEnums.FedAuthLibrary.ADAL:
byte workflow = 0x00;
switch (fedAuthFeatureData.authentication) {
case SqlAuthenticationMethod.ActiveDirectoryPassword:
workflow = TdsEnums.ADALWORKFLOW_ACTIVEDIRECTORYPASSWORD;
break;
case SqlAuthenticationMethod.ActiveDirectoryIntegrated:
workflow = TdsEnums.ADALWORKFLOW_ACTIVEDIRECTORYINTEGRATED;
break;
default:
Debug.Assert(false, "Unrecognized Authentication type for fedauth ADAL request");
break;
}
_physicalStateObj.WriteByte(workflow);
break;
case TdsEnums.FedAuthLibrary.SecurityToken:
WriteInt(fedAuthFeatureData.accessToken.Length, _physicalStateObj);
_physicalStateObj.WriteByteArray(fedAuthFeatureData.accessToken, fedAuthFeatureData.accessToken.Length, 0);
break;
default:
Debug.Assert(false, "Unrecognized FedAuthLibrary type for feature extension request");
break;
}
}
return totalLen;
}
internal int WriteTceFeatureRequest (bool write /* if false just calculates the length */) {
int len = 6; // (1byte = featureID, 4bytes = featureData length, 1 bytes = Version
if (write) {
// Write Feature ID, legth of the version# field and TCE Version#
_physicalStateObj.WriteByte(TdsEnums.FEATUREEXT_TCE);
WriteInt (1, _physicalStateObj);
_physicalStateObj.WriteByte(TdsEnums.MAX_SUPPORTED_TCE_VERSION);
}
return len; // size of data written
}
internal int WriteGlobalTransactionsFeatureRequest(bool write /* if false just calculates the length */) {
int len = 5; // 1byte = featureID, 4bytes = featureData length
if (write) {
// Write Feature ID
_physicalStateObj.WriteByte(TdsEnums.FEATUREEXT_GLOBALTRANSACTIONS);
WriteInt(0, _physicalStateObj); // we don't send any data
}
return len;
}
internal void TdsLogin(SqlLogin rec,
TdsEnums.FeatureExtension requestedFeatures,
SessionData recoverySessionData,
FederatedAuthenticationFeatureExtensionData? fedAuthFeatureExtensionData) {
_physicalStateObj.SetTimeoutSeconds(rec.timeout);
Debug.Assert(recoverySessionData == null || (requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0, "Recovery session data without session recovery feature request");
Debug.Assert(TdsEnums.MAXLEN_HOSTNAME>=rec.hostName.Length, "_workstationId.Length exceeds the max length for this value");
Debug.Assert(!(rec.useSSPI && _connHandler._fedAuthRequired), "Cannot use SSPI when server has responded 0x01 for FedAuthRequired PreLogin Option.");
Debug.Assert(!rec.useSSPI || (requestedFeatures & TdsEnums.FeatureExtension.FedAuth) == 0, "Cannot use both SSPI and FedAuth");
Debug.Assert(fedAuthFeatureExtensionData == null || (requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0, "fedAuthFeatureExtensionData provided without fed auth feature request");
Debug.Assert(fedAuthFeatureExtensionData != null || (requestedFeatures & TdsEnums.FeatureExtension.FedAuth) == 0, "Fed Auth feature requested without specifying fedAuthFeatureExtensionData.");
Debug.Assert(rec.userName == null || (rec.userName != null && TdsEnums.MAXLEN_USERNAME >= rec.userName.Length), "_userID.Length exceeds the max length for this value");
Debug.Assert(rec.credential == null || (rec.credential != null && TdsEnums.MAXLEN_USERNAME >= rec.credential.UserId.Length), "_credential.UserId.Length exceeds the max length for this value");
Debug.Assert(rec.password == null || (rec.password != null && TdsEnums.MAXLEN_PASSWORD>=rec.password.Length), "_password.Length exceeds the max length for this value");
Debug.Assert(rec.credential == null || (rec.credential != null && TdsEnums.MAXLEN_PASSWORD >= rec.credential.Password.Length), "_credential.Password.Length exceeds the max length for this value");
Debug.Assert(rec.credential != null || rec.userName != null || rec.password != null, "cannot mix the new secure password system and the connection string based password");
Debug.Assert(rec.newSecurePassword != null || rec.newPassword != null, "cannot have both new secure change password and string based change password");
Debug.Assert(TdsEnums.MAXLEN_APPNAME>=rec.applicationName.Length, "_applicationName.Length exceeds the max length for this value");
Debug.Assert(TdsEnums.MAXLEN_SERVERNAME>=rec.serverName.Length, "_dataSource.Length exceeds the max length for this value");
Debug.Assert(TdsEnums.MAXLEN_LANGUAGE>=rec.language.Length, "_currentLanguage .Length exceeds the max length for this value");
Debug.Assert(TdsEnums.MAXLEN_DATABASE>=rec.database.Length, "_initialCatalog.Length exceeds the max length for this value");
Debug.Assert(TdsEnums.MAXLEN_ATTACHDBFILE>=rec.attachDBFilename.Length, "_attachDBFileName.Length exceeds the max length for this value");
Debug.Assert(_connHandler != null, "SqlConnectionInternalTds handler can not be null at this point.");
_connHandler.TimeoutErrorInternal.EndPhase(SqlConnectionTimeoutErrorPhase.LoginBegin);
_connHandler.TimeoutErrorInternal.SetAndBeginPhase(SqlConnectionTimeoutErrorPhase.ProcessConnectionAuth);
// get the password up front to use in sspi logic below
byte[] encryptedPassword = null;
byte[] encryptedChangePassword = null;
int encryptedPasswordLengthInBytes;
int encryptedChangePasswordLengthInBytes;
bool useFeatureExt = (requestedFeatures != TdsEnums.FeatureExtension.None);
string userName;
if (rec.credential != null) {
userName = rec.credential.UserId;
encryptedPasswordLengthInBytes = rec.credential.Password.Length * 2;
}
else {
userName = rec.userName;
encryptedPassword = TdsParserStaticMethods.EncryptPassword(rec.password);
encryptedPasswordLengthInBytes = encryptedPassword.Length; // password in clear text is already encrypted and its length is in byte
}
if (rec.newSecurePassword != null) {
encryptedChangePasswordLengthInBytes = rec.newSecurePassword.Length * 2;
}
else {
encryptedChangePassword = TdsParserStaticMethods.EncryptPassword(rec.newPassword);
encryptedChangePasswordLengthInBytes = encryptedChangePassword.Length;
}
// set the message type
_physicalStateObj._outputMessageType = TdsEnums.MT_LOGIN7;
// length in bytes
int length = TdsEnums.YUKON_LOG_REC_FIXED_LEN;
string clientInterfaceName = TdsEnums.SQL_PROVIDER_NAME;
Debug.Assert(TdsEnums.MAXLEN_CLIENTINTERFACE >= clientInterfaceName.Length, "cchCltIntName can specify at most 128 unicode characters. See Tds spec");
// add up variable-len portions (multiply by 2 for byte len of char strings)
//
checked {
length += (rec.hostName.Length + rec.applicationName.Length +
rec.serverName.Length + clientInterfaceName.Length +
rec.language.Length + rec.database.Length +
rec.attachDBFilename.Length) * 2;
if (useFeatureExt) {
length += 4;
}
}
// allocate memory for SSPI variables
byte[] outSSPIBuff = null;
UInt32 outSSPILength = 0;
// only add lengths of password and username if not using SSPI or requesting federated authentication info
if (!rec.useSSPI && !(_connHandler._federatedAuthenticationInfoRequested || _connHandler._federatedAuthenticationRequested)) {
checked {
length += (userName.Length * 2) + encryptedPasswordLengthInBytes
+ encryptedChangePasswordLengthInBytes;
}
}
else {
if (rec.useSSPI) {
// now allocate proper length of buffer, and set length
outSSPIBuff = new byte[s_maxSSPILength];
outSSPILength = s_maxSSPILength;
// Call helper function for SSPI data and actual length.
// Since we don't have SSPI data from the server, send null for the
// byte[] buffer and 0 for the int length.
Debug.Assert(SniContext.Snix_Login==_physicalStateObj.SniContext, String.Format((IFormatProvider)null, "Unexpected SniContext. Expecting Snix_Login, actual value is '{0}'", _physicalStateObj.SniContext));
_physicalStateObj.SniContext = SniContext.Snix_LoginSspi;
SSPIData(null, 0, outSSPIBuff, ref outSSPILength);
if (outSSPILength > Int32.MaxValue) {
throw SQL.InvalidSSPIPacketSize(); // SqlBu 332503
}
_physicalStateObj.SniContext=SniContext.Snix_Login;
checked {
length += (Int32)outSSPILength;
}
}
}
int feOffset = length;
if (useFeatureExt) {
checked {
if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0) {
length += WriteSessionRecoveryFeatureRequest(recoverySessionData, false);
};
if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) {
Debug.Assert(fedAuthFeatureExtensionData != null, "fedAuthFeatureExtensionData should not null.");
length += WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData.Value, write:false);
}
if ((requestedFeatures & TdsEnums.FeatureExtension.Tce) != 0) {
length += WriteTceFeatureRequest (false);
}
if ((requestedFeatures & TdsEnums.FeatureExtension.GlobalTransactions) != 0) {
length += WriteGlobalTransactionsFeatureRequest(false);
}
length++; // for terminator
}
}
try {
WriteInt(length, _physicalStateObj);
if (recoverySessionData == null) {
WriteInt((TdsEnums.DENALI_MAJOR << 24) | (TdsEnums.DENALI_INCREMENT << 16) | TdsEnums.DENALI_MINOR, _physicalStateObj);
}
else {
WriteUnsignedInt(recoverySessionData._tdsVersion, _physicalStateObj);
}
WriteInt(rec.packetSize, _physicalStateObj);
WriteInt(TdsEnums.CLIENT_PROG_VER, _physicalStateObj);
WriteInt(TdsParserStaticMethods.GetCurrentProcessIdForTdsLoginOnly(), _physicalStateObj); //MDAC 84718
WriteInt(0, _physicalStateObj); // connectionID is unused
// Log7Flags (DWORD)
int log7Flags = 0;
/*
Current snapshot from TDS spec with the offsets added:
0) fByteOrder:1, // byte order of numeric data types on client
1) fCharSet:1, // character set on client
2) fFloat:2, // Type of floating point on client
4) fDumpLoad:1, // Dump/Load and BCP enable
5) fUseDb:1, // USE notification
6) fDatabase:1, // Initial database fatal flag
7) fSetLang:1, // SET LANGUAGE notification
8) fLanguage:1, // Initial language fatal flag
9) fODBC:1, // Set if client is ODBC driver
10) fTranBoundary:1, // Transaction boundary notification
11) fDelegatedSec:1, // Security with delegation is available
12) fUserType:3, // Type of user
15) fIntegratedSecurity:1, // Set if client is using integrated security
16) fSQLType:4, // Type of SQL sent from client
20) fOLEDB:1, // Set if client is OLEDB driver
21) fSpare1:3, // first bit used for read-only intent, rest unused
24) fResetPassword:1, // set if client wants to reset password
25) fNoNBCAndSparse:1, // set if client does not support NBC and Sparse column
26) fUserInstance:1, // This connection wants to connect to a SQL "user instance"
27) fUnknownCollationHandling:1, // This connection can handle unknown collation correctly.
28) fExtension:1 // Extensions are used
32 - total
*/
// first byte
log7Flags |= TdsEnums.USE_DB_ON << 5;
log7Flags |= TdsEnums.INIT_DB_FATAL << 6;
log7Flags |= TdsEnums.SET_LANG_ON << 7;
// second byte
log7Flags |= TdsEnums.INIT_LANG_FATAL << 8;
log7Flags |= TdsEnums.ODBC_ON << 9;
if (rec.useReplication) {
log7Flags |= TdsEnums.REPL_ON << 12;
}
if (rec.useSSPI) {
log7Flags |= TdsEnums.SSPI_ON << 15;
}
// third byte
if (rec.readOnlyIntent) {
log7Flags |= TdsEnums.READONLY_INTENT_ON << 21; // read-only intent flag is a first bit of fSpare1
}
// 4th one
if (!ADP.IsEmpty(rec.newPassword) || (rec.newSecurePassword != null && rec.newSecurePassword.Length != 0)) {
log7Flags |= 1 << 24;
}
if (rec.userInstance) {
log7Flags |= 1 << 26;
}
if (useFeatureExt) {
log7Flags |= 1 << 28;
}
WriteInt(log7Flags, _physicalStateObj);
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.TdsLogin|ADV> %d#, TDS Login7 flags = %d:\n", ObjectID, log7Flags);
}
WriteInt(0, _physicalStateObj); // ClientTimeZone is not used
WriteInt(0, _physicalStateObj); // LCID is unused by server
// Start writing offset and length of variable length portions
int offset = TdsEnums.YUKON_LOG_REC_FIXED_LEN;
// write offset/length pairs
// note that you must always set ibHostName since it indicaters the beginning of the variable length section of the login record
WriteShort(offset, _physicalStateObj); // host name offset
WriteShort(rec.hostName.Length, _physicalStateObj);
offset += rec.hostName.Length * 2;
// Only send user/password over if not fSSPI or fed auth ADAL... If both user/password and SSPI are in login
// rec, only SSPI is used. Confirmed same bahavior as in luxor.
if (!rec.useSSPI && !(_connHandler._federatedAuthenticationInfoRequested || _connHandler._federatedAuthenticationRequested)) {
WriteShort(offset, _physicalStateObj); // userName offset
WriteShort(userName.Length, _physicalStateObj);
offset += userName.Length * 2;
// the encrypted password is a byte array - so length computations different than strings
WriteShort(offset, _physicalStateObj); // password offset
WriteShort(encryptedPasswordLengthInBytes / 2, _physicalStateObj);
offset += encryptedPasswordLengthInBytes;
}
else {
// case where user/password data is not used, send over zeros
WriteShort(0, _physicalStateObj); // userName offset
WriteShort(0, _physicalStateObj);
WriteShort(0, _physicalStateObj); // password offset
WriteShort(0, _physicalStateObj);
}
WriteShort(offset, _physicalStateObj); // app name offset
WriteShort(rec.applicationName.Length, _physicalStateObj);
offset += rec.applicationName.Length * 2;
WriteShort(offset, _physicalStateObj); // server name offset
WriteShort(rec.serverName.Length, _physicalStateObj);
offset += rec.serverName.Length * 2;
WriteShort(offset, _physicalStateObj);
if (useFeatureExt) {
WriteShort(4, _physicalStateObj); // length of ibFeatgureExtLong (which is a DWORD)
offset += 4;
}
else {
WriteShort(0, _physicalStateObj); // unused (was remote password ?)
}
WriteShort(offset, _physicalStateObj); // client interface name offset
WriteShort(clientInterfaceName.Length, _physicalStateObj);
offset += clientInterfaceName.Length * 2;
WriteShort(offset, _physicalStateObj); // language name offset
WriteShort(rec.language.Length, _physicalStateObj);
offset += rec.language.Length * 2;
WriteShort(offset, _physicalStateObj); // database name offset
WriteShort(rec.database.Length, _physicalStateObj);
offset += rec.database.Length * 2;
//
if (null == s_nicAddress)
s_nicAddress = TdsParserStaticMethods.GetNetworkPhysicalAddressForTdsLoginOnly();
_physicalStateObj.WriteByteArray(s_nicAddress, s_nicAddress.Length, 0);
WriteShort(offset, _physicalStateObj); // ibSSPI offset
if (rec.useSSPI) {
WriteShort((int)outSSPILength, _physicalStateObj);
offset += (int)outSSPILength;
}
else {
WriteShort(0, _physicalStateObj);
}
WriteShort(offset, _physicalStateObj); // DB filename offset
WriteShort(rec.attachDBFilename.Length, _physicalStateObj);
offset += rec.attachDBFilename.Length * 2;
WriteShort(offset, _physicalStateObj); // reset password offset
WriteShort(encryptedChangePasswordLengthInBytes / 2, _physicalStateObj);
WriteInt(0, _physicalStateObj); // reserved for chSSPI
// write variable length portion
WriteString(rec.hostName, _physicalStateObj);
// if we are using SSPI or fed auth ADAL, do not send over username/password, since we will use SSPI instead
// same behavior as Luxor
if (!rec.useSSPI && !(_connHandler._federatedAuthenticationInfoRequested || _connHandler._federatedAuthenticationRequested)) {
WriteString(userName, _physicalStateObj);
// Cache offset in packet for tracing.
_physicalStateObj._tracePasswordOffset = _physicalStateObj._outBytesUsed;
_physicalStateObj._tracePasswordLength = encryptedPasswordLengthInBytes;
if (rec.credential != null) {
_physicalStateObj.WriteSecureString(rec.credential.Password);
}
else {
_physicalStateObj.WriteByteArray(encryptedPassword, encryptedPasswordLengthInBytes, 0);
}
}
WriteString(rec.applicationName, _physicalStateObj);
WriteString(rec.serverName, _physicalStateObj);
// write ibFeatureExtLong
if (useFeatureExt) {
WriteInt(feOffset, _physicalStateObj);
}
WriteString(clientInterfaceName, _physicalStateObj);
WriteString(rec.language, _physicalStateObj);
WriteString(rec.database, _physicalStateObj);
// send over SSPI data if we are using SSPI
if (rec.useSSPI)
_physicalStateObj.WriteByteArray(outSSPIBuff, (int)outSSPILength, 0);
WriteString(rec.attachDBFilename, _physicalStateObj);
if (!rec.useSSPI && !(_connHandler._federatedAuthenticationInfoRequested || _connHandler._federatedAuthenticationRequested)) {
// Cache offset in packet for tracing.
_physicalStateObj._traceChangePasswordOffset = _physicalStateObj._outBytesUsed;
_physicalStateObj._traceChangePasswordLength = encryptedChangePasswordLengthInBytes;
if (rec.newSecurePassword != null) {
_physicalStateObj.WriteSecureString(rec.newSecurePassword);
}
else {
_physicalStateObj.WriteByteArray(encryptedChangePassword, encryptedChangePasswordLengthInBytes, 0);
}
}
if (useFeatureExt) {
if ((requestedFeatures & TdsEnums.FeatureExtension.SessionRecovery) != 0) {
WriteSessionRecoveryFeatureRequest(recoverySessionData, true);
};
if ((requestedFeatures & TdsEnums.FeatureExtension.FedAuth) != 0) {
Bid.Trace("<sc.TdsParser.TdsLogin|SEC> Sending federated authentication feature request\n");
Debug.Assert(fedAuthFeatureExtensionData != null, "fedAuthFeatureExtensionData should not null.");
WriteFedAuthFeatureRequest(fedAuthFeatureExtensionData.Value, write: true);
};
if ((requestedFeatures & TdsEnums.FeatureExtension.Tce) != 0) {
WriteTceFeatureRequest (true);
};
if ((requestedFeatures & TdsEnums.FeatureExtension.GlobalTransactions) != 0) {
WriteGlobalTransactionsFeatureRequest(true);
};
_physicalStateObj.WriteByte(0xFF); // terminator
}
} // try
catch (Exception e) {
//
if (ADP.IsCatchableExceptionType(e)) {
// be sure to wipe out our buffer if we started sending stuff
_physicalStateObj._outputPacketNumber = 1; // end of message - reset to 1 - per ramas
_physicalStateObj.ResetBuffer();
}
throw;
}
_physicalStateObj.WritePacket(TdsEnums.HARDFLUSH);
_physicalStateObj.ResetSecurePasswordsInfomation(); // Password information is needed only from Login process; done with writing login packet and should clear information
_physicalStateObj._pendingData = true;
_physicalStateObj._messageStatus = 0;
}// tdsLogin
/// <summary>
/// Send the access token to the server.
/// </summary>
/// <param name="fedAuthToken">Type encapuslating a Federated Authentication access token.</param>
internal void SendFedAuthToken(SqlFedAuthToken fedAuthToken) {
Debug.Assert(fedAuthToken != null, "fedAuthToken cannot be null");
Debug.Assert(fedAuthToken.accessToken != null, "fedAuthToken.accessToken cannot be null");
Bid.Trace("<sc.TdsParser.SendFedAuthToken|SEC> Sending federated authentication token\n");
_physicalStateObj._outputMessageType = TdsEnums.MT_FEDAUTH;
byte[] accessToken = fedAuthToken.accessToken;
// Send total length (length of token plus 4 bytes for the token length field)
// If we were sending a nonce, this would include that length as well
WriteUnsignedInt((uint)accessToken.Length + sizeof(uint), _physicalStateObj);
// Send length of token
WriteUnsignedInt((uint)accessToken.Length, _physicalStateObj);
// Send federated authentication access token
_physicalStateObj.WriteByteArray(accessToken, accessToken.Length, 0);
_physicalStateObj.WritePacket(TdsEnums.HARDFLUSH);
_physicalStateObj._pendingData = true;
_physicalStateObj._messageStatus = 0;
_connHandler._federatedAuthenticationRequested = true;
}
private void SSPIData(byte[] receivedBuff, UInt32 receivedLength, byte[] sendBuff, ref UInt32 sendLength) {
SNISSPIData(receivedBuff, receivedLength, sendBuff, ref sendLength);
}
private void SNISSPIData(byte[] receivedBuff, UInt32 receivedLength, byte[] sendBuff, ref UInt32 sendLength)
{
if (receivedBuff == null)
{
// we do not have SSPI data coming from server, so send over 0's for pointer and length
receivedLength = 0;
}
// we need to respond to the server's message with SSPI data
if(0 != SNINativeMethodWrapper.SNISecGenClientContext(_physicalStateObj.Handle, receivedBuff, receivedLength, sendBuff, ref sendLength, _sniSpnBuffer))
{
SSPIError(SQLMessage.SSPIGenerateError(), TdsEnums.GEN_CLIENT_CONTEXT);
}
}
private void ProcessSSPI(int receivedLength) {
SniContext outerContext=_physicalStateObj.SniContext;
_physicalStateObj.SniContext = SniContext.Snix_ProcessSspi;
// allocate received buffer based on length from SSPI message
byte[] receivedBuff = new byte[receivedLength];
// read SSPI data received from server
Debug.Assert(_physicalStateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
bool result = _physicalStateObj.TryReadByteArray(receivedBuff, 0, receivedLength);
if (!result) { throw SQL.SynchronousCallMayNotPend(); }
// allocate send buffer and initialize length
byte[] sendBuff = new byte[s_maxSSPILength];
UInt32 sendLength = s_maxSSPILength;
// make call for SSPI data
SSPIData(receivedBuff, (UInt32)receivedLength, sendBuff, ref sendLength);
// DO NOT SEND LENGTH - TDS DOC INCORRECT! JUST SEND SSPI DATA!
_physicalStateObj.WriteByteArray(sendBuff, (int)sendLength, 0);
// set message type so server knows its a SSPI response
_physicalStateObj._outputMessageType = TdsEnums.MT_SSPI;
// send to server
_physicalStateObj.WritePacket(TdsEnums.HARDFLUSH);
_physicalStateObj.SniContext=outerContext;
}
private void SSPIError(string error, string procedure) {
Debug.Assert(!ADP.IsEmpty(procedure), "TdsParser.SSPIError called with an empty or null procedure string");
Debug.Assert(!ADP.IsEmpty(error), "TdsParser.SSPIError called with an empty or null error string");
_physicalStateObj.AddError(new SqlError(0, (byte)0x00, (byte)TdsEnums.MIN_ERROR_CLASS, _server, error, procedure, 0));
ThrowExceptionAndWarning(_physicalStateObj);
}
private void LoadSSPILibrary() {
// Outer check so we don't acquire lock once once it's loaded.
if (!s_fSSPILoaded) {
lock (s_tdsParserLock) {
// re-check inside lock
if (!s_fSSPILoaded) {
// use local for ref param to defer setting s_maxSSPILength until we know the call succeeded.
UInt32 maxLength = 0;
if (0 != SNINativeMethodWrapper.SNISecInitPackage(ref maxLength))
SSPIError(SQLMessage.SSPIInitializeError(), TdsEnums.INIT_SSPI_PACKAGE);
s_maxSSPILength = maxLength;
s_fSSPILoaded = true;
}
}
}
if (s_maxSSPILength > Int32.MaxValue) {
throw SQL.InvalidSSPIPacketSize(); // SqlBu 332503
}
}
private void LoadADALLibrary() {
// Outer check so we don't acquire lock once once it's loaded.
if (!s_fADALLoaded) {
lock (s_tdsParserLock) {
// re-check inside lock
if (!s_fADALLoaded) {
int result = ADALNativeWrapper.ADALInitialize();
if (0 == result) {
s_fADALLoaded = true;
}
else {
s_fADALLoaded = false;
SqlAuthenticationMethod authentication = SqlAuthenticationMethod.NotSpecified;
if (_connHandler.ConnectionOptions != null)
{
authentication = _connHandler.ConnectionOptions.Authentication;
// Only the below connection string options should have ended up calling this function.
Debug.Assert(authentication == SqlAuthenticationMethod.ActiveDirectoryIntegrated || authentication == SqlAuthenticationMethod.ActiveDirectoryPassword);
}
_physicalStateObj.AddError(new SqlError(0, (byte)0x00, (byte)TdsEnums.MIN_ERROR_CLASS, _server, Res.GetString(Res.SQL_ADALInitializeError, authentication.ToString("G"), result.ToString("X")), TdsEnums.INIT_ADAL_PACKAGE, 0));
ThrowExceptionAndWarning(_physicalStateObj);
}
}
}
}
}
internal byte[] GetDTCAddress(int timeout, TdsParserStateObject stateObj) {
// If this fails, the server will return a server error - Sameet Agarwal confirmed.
// Success: DTCAddress returned. Failure: SqlError returned.
byte[] dtcAddr = null;
using (SqlDataReader dtcReader = TdsExecuteTransactionManagerRequest(
null,
TdsEnums.TransactionManagerRequestType.GetDTCAddress,
null,
TdsEnums.TransactionManagerIsolationLevel.Unspecified,
timeout, null, stateObj, true)) {
Debug.Assert(SniContext.Snix_Read==stateObj.SniContext, String.Format((IFormatProvider)null, "The SniContext should be Snix_Read but it actually is {0}", stateObj.SniContext));
if (null != dtcReader && dtcReader.Read()) {
Debug.Assert(dtcReader.GetName(0) == "TM Address", "TdsParser: GetDTCAddress did not return 'TM Address'");
// DTCAddress is of variable size, and does not have a maximum. So we call GetBytes
// to get the length of the dtcAddress, then allocate a byte array of that length,
// then call GetBytes again on that byte[] with the length
long dtcLength = dtcReader.GetBytes(0, 0, null, 0, 0);
//
if (dtcLength <= Int32.MaxValue) {
int cb = (int)dtcLength;
dtcAddr = new byte[cb];
dtcReader.GetBytes(0, 0, dtcAddr, 0, cb);
}
#if DEBUG
else {
Debug.Assert(false, "unexpected length (> Int32.MaxValue) returned from dtcReader.GetBytes");
// if we hit this case we'll just return a null address so that the user
// will get a transcaction enlistment error in the upper layers
}
#endif
}
}
return dtcAddr;
}
// Propagate the dtc cookie to the server, enlisting the connection.
internal void PropagateDistributedTransaction(byte[] buffer, int timeout, TdsParserStateObject stateObj) {
// if this fails, the server will return a server error - Sameet Agarwal confirmed
// Success: server will return done token. Failure: SqlError returned.
TdsExecuteTransactionManagerRequest(buffer,
TdsEnums.TransactionManagerRequestType.Propagate, null,
TdsEnums.TransactionManagerIsolationLevel.Unspecified, timeout, null, stateObj, true);
}
internal SqlDataReader TdsExecuteTransactionManagerRequest(
byte[] buffer,
TdsEnums.TransactionManagerRequestType request,
string transactionName,
TdsEnums.TransactionManagerIsolationLevel isoLevel,
int timeout,
SqlInternalTransaction transaction,
TdsParserStateObject stateObj,
bool isDelegateControlRequest) {
Debug.Assert(this == stateObj.Parser, "different parsers");
if (TdsParserState.Broken == State || TdsParserState.Closed == State) {
return null;
}
// SQLBUDT #20010853 - Promote, Commit and Rollback requests for
// delegated transactions often happen while there is an open result
// set, so we need to handle them by using a different MARS session,
// otherwise we'll write on the physical state objects while someone
// else is using it. When we don't have MARS enabled, we need to
// lock the physical state object to syncronize it's use at least
// until we increment the open results count. Once it's been
// incremented the delegated transaction requests will fail, so they
// won't stomp on anything.
Debug.Assert(!_connHandler.ThreadHasParserLockForClose || _connHandler._parserLock.ThreadMayHaveLock(), "Thread claims to have parser lock, but lock is not taken");
bool callerHasConnectionLock = _connHandler.ThreadHasParserLockForClose; // If the thread already claims to have the parser lock, then we will let the caller handle releasing it
if (!callerHasConnectionLock) {
_connHandler._parserLock.Wait(canReleaseFromAnyThread:false);
_connHandler.ThreadHasParserLockForClose = true;
}
// Capture _asyncWrite (after taking lock) to restore it afterwards
bool hadAsyncWrites = _asyncWrite;
try {
// Temprarily disable async writes
_asyncWrite = false;
// This validation step MUST be done after locking the connection to guarantee we don't
// accidentally execute after the transaction has completed on a different thread.
if (!isDelegateControlRequest) {
_connHandler.CheckEnlistedTransactionBinding();
}
stateObj._outputMessageType = TdsEnums.MT_TRANS; // set message type
stateObj.SetTimeoutSeconds(timeout);
stateObj.SniContext = SniContext.Snix_Execute;
if (_isYukon) {
const int marsHeaderSize = 18; // 4 + 2 + 8 + 4
const int totalHeaderLength = 22; // 4 + 4 + 2 + 8 + 4
Debug.Assert(stateObj._outBytesUsed == stateObj._outputHeaderLen, "Output bytes written before total header length");
// Write total header length
WriteInt(totalHeaderLength, stateObj);
// Write mars header length
WriteInt(marsHeaderSize, stateObj);
WriteMarsHeaderData(stateObj, _currentTransaction);
}
WriteShort((short)request, stateObj); // write TransactionManager Request type
bool returnReader = false;
switch (request) {
case TdsEnums.TransactionManagerRequestType.GetDTCAddress:
WriteShort(0, stateObj);
returnReader = true;
break;
case TdsEnums.TransactionManagerRequestType.Propagate:
if (null != buffer) {
WriteShort(buffer.Length, stateObj);
stateObj.WriteByteArray(buffer, buffer.Length, 0);
}
else {
WriteShort(0, stateObj);
}
break;
case TdsEnums.TransactionManagerRequestType.Begin:
Debug.Assert(IsYukonOrNewer, "Should not be calling TdsExecuteTransactionManagerRequest on pre-Yukon clients for BeginTransaction!");
Debug.Assert(null != transaction, "Should have specified an internalTransaction when doing a BeginTransaction request!");
// Only assign the passed in transaction if it is not equal to the current transaction.
// And, if it is not equal, the current actually should be null. Anything else
// is a unexpected state. The concern here is mainly for the mixed use of
// T-SQL and API transactions. See SQL BU DT 345300 for full details and repro.
// Expected states:
// 1) _pendingTransaction = null, _currentTransaction = null, non null transaction
// passed in on BeginTransaction API call.
// 2) _currentTransaction != null, _pendingTransaction = null, non null transaction
// passed in but equivalent to _currentTransaction.
// #1 will occur on standard BeginTransactionAPI call. #2 should only occur if
// t-sql transaction started followed by a call to SqlConnection.BeginTransaction.
// Any other state is unknown.
if (_currentTransaction != transaction) {
Debug.Assert(_currentTransaction == null || true == _fResetConnection, "We should not have a current Tx at this point");
PendingTransaction = transaction;
}
stateObj.WriteByte((byte)isoLevel);
stateObj.WriteByte((byte)(transactionName.Length * 2)); // Write number of bytes (unicode string).
WriteString(transactionName, stateObj);
break;
case TdsEnums.TransactionManagerRequestType.Promote:
Debug.Assert(IsYukonOrNewer, "Should not be calling TdsExecuteTransactionManagerRequest on pre-Yukon clients for PromoteTransaction!");
// No payload - except current transaction in header
// Promote returns a DTC cookie. However, the transaction cookie we use for the
// connection does not change after a promote.
break;
case TdsEnums.TransactionManagerRequestType.Commit:
Debug.Assert(IsYukonOrNewer, "Should not be calling TdsExecuteTransactionManagerRequest on pre-Yukon clients for CommitTransaction!");
Debug.Assert(transactionName.Length == 0, "Should not have a transaction name on Commit");
stateObj.WriteByte((byte)0); // No xact name
stateObj.WriteByte(0); // No flags
Debug.Assert(isoLevel == TdsEnums.TransactionManagerIsolationLevel.Unspecified, "Should not have isolation level other than unspecified on Commit!");
// WriteByte((byte) 0, stateObj); // IsolationLevel
// WriteByte((byte) 0, stateObj); // No begin xact name
break;
case TdsEnums.TransactionManagerRequestType.Rollback:
Debug.Assert(IsYukonOrNewer, "Should not be calling TdsExecuteTransactionManagerRequest on pre-Yukon clients for RollbackTransaction!");
stateObj.WriteByte((byte)(transactionName.Length * 2)); // Write number of bytes (unicode string).
WriteString(transactionName, stateObj);
stateObj.WriteByte(0); // No flags
Debug.Assert(isoLevel == TdsEnums.TransactionManagerIsolationLevel.Unspecified, "Should not have isolation level other than unspecified on Commit!");
// WriteByte((byte) 0, stateObj); // IsolationLevel
// WriteByte((byte) 0, stateObj); // No begin xact name
break;
case TdsEnums.TransactionManagerRequestType.Save:
Debug.Assert(IsYukonOrNewer, "Should not be calling TdsExecuteTransactionManagerRequest on pre-Yukon clients for SaveTransaction!");
stateObj.WriteByte((byte)(transactionName.Length * 2)); // Write number of bytes (unicode string).
WriteString(transactionName, stateObj);
break;
default:
Debug.Assert(false, "Unexpected TransactionManagerRequest");
break;
}
Task writeTask = stateObj.WritePacket(TdsEnums.HARDFLUSH);
Debug.Assert(writeTask == null, "Writes should not pend when writing sync");
stateObj._pendingData = true;
stateObj._messageStatus = 0;
SqlDataReader dtcReader = null;
stateObj.SniContext = SniContext.Snix_Read;
if (returnReader) {
dtcReader = new SqlDataReader(null, CommandBehavior.Default);
Debug.Assert(this == stateObj.Parser, "different parser");
#if DEBUG
// Remove the current owner of stateObj - otherwise we will hit asserts
stateObj.Owner = null;
#endif
dtcReader.Bind(stateObj);
// force consumption of metadata
_SqlMetaDataSet metaData = dtcReader.MetaData;
}
else {
Run(RunBehavior.UntilDone, null, null, null, stateObj);
}
// If the retained ID is no longer valid (because we are enlisting in null or a new transaction) then it should be cleared
if (((request == TdsEnums.TransactionManagerRequestType.Begin) || (request == TdsEnums.TransactionManagerRequestType.Propagate)) && ((transaction == null) || (transaction.TransactionId != _retainedTransactionId))) {
_retainedTransactionId = SqlInternalTransaction.NullTransactionId;
}
return dtcReader;
}
catch (Exception e) {
//
if (!ADP.IsCatchableExceptionType(e)) {
throw;
}
FailureCleanup(stateObj, e);
throw;
}
finally {
// SQLHotfix 50000518
// make sure we don't leave temporary fields set when leaving this function
_pendingTransaction = null;
_asyncWrite = hadAsyncWrites;
if (!callerHasConnectionLock) {
_connHandler.ThreadHasParserLockForClose = false;
_connHandler._parserLock.Release();
}
}
}
internal void FailureCleanup(TdsParserStateObject stateObj, Exception e) {
int old_outputPacketNumber = stateObj._outputPacketNumber;
if (Bid.TraceOn) {
Bid.Trace("<sc.TdsParser.FailureCleanup|ERR> Exception caught on ExecuteXXX: '%ls' \n", e.ToString());
}
if (stateObj.HasOpenResult) { // SQL BU DT 383773 - need to decrement openResultCount if operation failed.
stateObj.DecrementOpenResultCount();
}
// be sure to wipe out our buffer if we started sending stuff
stateObj.ResetBuffer();
stateObj._outputPacketNumber = 1; // end of message - reset to 1 - per ramas
if (old_outputPacketNumber != 1 && _state == TdsParserState.OpenLoggedIn) {
Debug.Assert(_connHandler._parserLock.ThreadMayHaveLock(), "Should not be calling into FailureCleanup without first taking the parser lock");
bool originalThreadHasParserLock = _connHandler.ThreadHasParserLockForClose;
try {
// Dev11 Bug 385286 : ExecuteNonQueryAsync hangs when trying to write a parameter which generates ArgumentException and while handling that exception the server disconnects the connection
// Need to set this to true such that if we have an error sending\processing the attention, we won't deadlock ourselves
_connHandler.ThreadHasParserLockForClose = true;
// If _outputPacketNumber prior to ResetBuffer was not equal to 1, a packet was already
// sent to the server and so we need to send an attention and process the attention ack.
stateObj.SendAttention();
ProcessAttention(stateObj);
}
finally {
// Reset the ThreadHasParserLock value incase our caller expects it to be set\not set
_connHandler.ThreadHasParserLockForClose = originalThreadHasParserLock;
}
}
Bid.Trace("<sc.TdsParser.FailureCleanup|ERR> Exception rethrown. \n");
}
internal Task TdsExecuteSQLBatch(string text, int timeout, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, bool sync, bool callerHasConnectionLock = false) {
if (TdsParserState.Broken == State || TdsParserState.Closed == State) {
return null;
}
if (stateObj.BcpLock) {
throw SQL.ConnectionLockedForBcpEvent();
}
// SQLBUDT #20010853 - Promote, Commit and Rollback requests for
// delegated transactions often happen while there is an open result
// set, so we need to handle them by using a different MARS session,
// otherwise we'll write on the physical state objects while someone
// else is using it. When we don't have MARS enabled, we need to
// lock the physical state object to syncronize it's use at least
// until we increment the open results count. Once it's been
// incremented the delegated transaction requests will fail, so they
// won't stomp on anything.
// Only need to take the lock if neither the thread nor the caller claims to already have it
bool needToTakeParserLock = (!callerHasConnectionLock) && (!_connHandler.ThreadHasParserLockForClose);
Debug.Assert(!_connHandler.ThreadHasParserLockForClose || sync, "Thread shouldn't claim to have the parser lock if we are doing async writes"); // Since we have the possibility of pending with async writes, make sure the thread doesn't claim to already have the lock
Debug.Assert(needToTakeParserLock || _connHandler._parserLock.ThreadMayHaveLock(), "Thread or caller claims to have connection lock, but lock is not taken");
bool releaseConnectionLock = false;
if (needToTakeParserLock) {
_connHandler._parserLock.Wait(canReleaseFromAnyThread: !sync);
releaseConnectionLock = true;
}
// Switch the writing mode
// NOTE: We are not turning off async writes when we complete since SqlBulkCopy uses this method and expects _asyncWrite to not change
_asyncWrite = !sync;
try {
// Check that the connection is still alive
if ((_state == TdsParserState.Closed) || (_state == TdsParserState.Broken)) {
throw ADP.ClosedConnectionError();
}
// This validation step MUST be done after locking the connection to guarantee we don't
// accidentally execute after the transaction has completed on a different thread.
_connHandler.CheckEnlistedTransactionBinding();
stateObj.SetTimeoutSeconds(timeout);
if ((!_fMARS) && (_physicalStateObj.HasOpenResult))
{
Bid.Trace("<sc.TdsParser.TdsExecuteSQLBatch|ERR> Potential multi-threaded misuse of connection, non-MARs connection with an open result %d#\n", ObjectID);
}
stateObj.SniContext = SniContext.Snix_Execute;
if (_isYukon) {
WriteRPCBatchHeaders(stateObj, notificationRequest);
}
stateObj._outputMessageType = TdsEnums.MT_SQL;
WriteString(text, text.Length, 0, stateObj);
Task executeTask = stateObj.ExecuteFlush();
if (executeTask == null) {
stateObj.SniContext = SniContext.Snix_Read;
}
else {
Debug.Assert(!sync, "Should not have gotten a Task when writing in sync mode");
// Need to wait for flush - continuation will unlock the connection
bool taskReleaseConnectionLock = releaseConnectionLock;
releaseConnectionLock = false;
return executeTask.ContinueWith(t => {
Debug.Assert(!t.IsCanceled, "Task should not be canceled");
try {
if (t.IsFaulted) {
FailureCleanup(stateObj, t.Exception.InnerException);
throw t.Exception.InnerException;
}
else {
stateObj.SniContext = SniContext.Snix_Read;
}
}
finally {
if (taskReleaseConnectionLock) {
_connHandler._parserLock.Release();
}
}
}, TaskScheduler.Default);
}
// Finished sync
return null;
}
catch (Exception e) {
//Debug.Assert(_state == TdsParserState.Broken, "Caught exception in TdsExecuteSQLBatch but connection was not broken!");
//
if (!ADP.IsCatchableExceptionType(e)) {
throw;
}
FailureCleanup(stateObj, e);
throw;
}
finally {
if (releaseConnectionLock) {
_connHandler._parserLock.Release();
}
}
}
internal Task TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, int timeout, bool inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, bool isCommandProc, bool sync = true,
TaskCompletionSource<object> completion = null, int startRpc = 0, int startParam = 0) {
bool firstCall = (completion == null);
bool releaseConnectionLock = false;
Debug.Assert(cmd != null, @"cmd cannot be null inside TdsExecuteRPC");
Debug.Assert(!firstCall || startRpc == 0, "startRpc is not 0 on first call");
Debug.Assert(!firstCall || startParam == 0, "startParam is not 0 on first call");
Debug.Assert(!firstCall || !_connHandler.ThreadHasParserLockForClose, "Thread should not already have connection lock");
Debug.Assert(firstCall || _connHandler._parserLock.ThreadMayHaveLock(), "Connection lock not taken after the first call");
try {
_SqlRPC rpcext = null;
int tempLen;
// SQLBUDT #20010853 - Promote, Commit and Rollback requests for
// delegated transactions often happen while there is an open result
// set, so we need to handle them by using a different MARS session,
// otherwise we'll write on the physical state objects while someone
// else is using it. When we don't have MARS enabled, we need to
// lock the physical state object to syncronize it's use at least
// until we increment the open results count. Once it's been
// incremented the delegated transaction requests will fail, so they
// won't stomp on anything.
if (firstCall) {
_connHandler._parserLock.Wait(canReleaseFromAnyThread:!sync);
releaseConnectionLock = true;
}
try {
// Ensure that connection is alive
if ((TdsParserState.Broken == State) || (TdsParserState.Closed == State)) {
throw ADP.ClosedConnectionError();
}
// This validation step MUST be done after locking the connection to guarantee we don't
// accidentally execute after the transaction has completed on a different thread.
if (firstCall) {
_asyncWrite = !sync;
_connHandler.CheckEnlistedTransactionBinding();
stateObj.SetTimeoutSeconds(timeout);
if ((!_fMARS) && (_physicalStateObj.HasOpenResult))
{
Bid.Trace("<sc.TdsParser.TdsExecuteRPC|ERR> Potential multi-threaded misuse of connection, non-MARs connection with an open result %d#\n", ObjectID);
}
stateObj.SniContext = SniContext.Snix_Execute;
if (_isYukon) {
WriteRPCBatchHeaders(stateObj, notificationRequest);
}
stateObj._outputMessageType = TdsEnums.MT_RPC;
}
for (int ii = startRpc; ii < rpcArray.Length; ii++) {
rpcext = rpcArray[ii];
if (startParam == 0 || ii > startRpc) {
if (rpcext.ProcID != 0 && _isShiloh) {
// Perf optimization for Shiloh and later,
Debug.Assert(rpcext.ProcID < 255, "rpcExec:ProcID can't be larger than 255");
WriteShort(0xffff, stateObj);
WriteShort((short)(rpcext.ProcID), stateObj);
}
else {
Debug.Assert(!ADP.IsEmpty(rpcext.rpcName), "must have an RPC name");
tempLen = rpcext.rpcName.Length;
WriteShort(tempLen, stateObj);
WriteString(rpcext.rpcName, tempLen, 0, stateObj);
}
// Options
WriteShort((short)rpcext.options, stateObj);
}
// Stream out parameters
SqlParameter[] parameters = rpcext.parameters;
for (int i = (ii == startRpc) ? startParam : 0; i < parameters.Length; i++) {
// Debug.WriteLine("i: " + i.ToString(CultureInfo.InvariantCulture));
// parameters can be unnamed
SqlParameter param = parameters[i];
// Since we are reusing the parameters array, we cannot rely on length to indicate no of parameters.
if (param == null)
break; // End of parameters for this execute
// Throw an exception if ForceColumnEncryption is set on a parameter and the ColumnEncryption is not enabled on SqlConnection or SqlCommand
if (param.ForceColumnEncryption &&
!(cmd.ColumnEncryptionSetting == SqlCommandColumnEncryptionSetting.Enabled ||
(cmd.ColumnEncryptionSetting == SqlCommandColumnEncryptionSetting.UseConnectionSetting && cmd.Connection.IsColumnEncryptionSettingEnabled))) {
throw SQL.ParamInvalidForceColumnEncryptionSetting(param.ParameterName, rpcext.GetCommandTextOrRpcName());
}
// Check if the applications wants to force column encryption to avoid sending sensitive data to server
if (param.ForceColumnEncryption && param.CipherMetadata == null
&& (param.Direction == ParameterDirection.Input || param.Direction == ParameterDirection.InputOutput)) {
// Application wants a parameter to be encrypted before sending it to server, however server doesnt think this parameter needs encryption.
throw SQL.ParamUnExpectedEncryptionMetadata(param.ParameterName, rpcext.GetCommandTextOrRpcName());
}
// Validate parameters are not variable length without size and with null value. MDAC 66522
param.Validate(i, isCommandProc);
// type (parameter record stores the MetaType class which is a helper that encapsulates all the type information we need here)
MetaType mt = param.InternalMetaType;
if (mt.IsNewKatmaiType) {
WriteSmiParameter(param, i, 0 != (rpcext.paramoptions[i] & TdsEnums.RPC_PARAM_DEFAULT), stateObj);
continue;
}
if ((!_isShiloh && !mt.Is70Supported) ||
(!_isYukon && !mt.Is80Supported) ||
(!_isKatmai && !mt.Is90Supported)) {
throw ADP.VersionDoesNotSupportDataType(mt.TypeName);
}
object value = null;
bool isNull = true;
bool isSqlVal = false;
bool isDataFeed = false;
// if we have an output param, set the value to null so we do not send it across to the server
if (param.Direction == ParameterDirection.Output) {
isSqlVal = param.ParamaterIsSqlType; // We have to forward the TYPE info, we need to know what type we are returning. Once we null the paramater we will no longer be able to distinguish what type were seeing.
param.Value = null;
param.ParamaterIsSqlType = isSqlVal;
}
else {
value = param.GetCoercedValue();
isNull = param.IsNull;
if (!isNull) {
isSqlVal = param.CoercedValueIsSqlType;
isDataFeed = param.CoercedValueIsDataFeed;
}
}
WriteParameterName(param.ParameterNameFixed, stateObj);
// Write parameter status
stateObj.WriteByte(rpcext.paramoptions[i]);
// MaxLen field is only written out for non-fixed length data types
// use the greater of the two sizes for maxLen
int actualSize;
int size = mt.IsSizeInCharacters ? param.GetParameterSize() * 2 : param.GetParameterSize();
//for UDTs, we calculate the length later when we get the bytes. This is a really expensive operation
if (mt.TDSType != TdsEnums.SQLUDT)
// getting the actualSize is expensive, cache here and use below
actualSize = param.GetActualSize();
else
actualSize = 0; //get this later
byte precision = 0;
byte scale = 0;
// scale and precision are only relevant for numeric and decimal types
// adjust the actual value scale and precision to match the user specified
if (mt.SqlDbType == SqlDbType.Decimal) {
precision = param.GetActualPrecision();
scale = param.GetActualScale();
if (precision > TdsEnums.MAX_NUMERIC_PRECISION) {
throw SQL.PrecisionValueOutOfRange(precision);
}
// bug 49512, make sure the value matches the scale the user enters
if (!isNull) {
if (isSqlVal) {
value = AdjustSqlDecimalScale((SqlDecimal)value, scale);
// If Precision is specified, verify value precision vs param precision
if (precision != 0) {
if (precision < ((SqlDecimal)value).Precision) {
throw ADP.ParameterValueOutOfRange((SqlDecimal)value);
}
}
}
else {
value = AdjustDecimalScale((Decimal)value, scale);
SqlDecimal sqlValue = new SqlDecimal((Decimal)value);
// If Precision is specified, verify value precision vs param precision
if (precision != 0) {
if (precision < sqlValue.Precision) {
throw ADP.ParameterValueOutOfRange((Decimal)value);
}
}
}
}
}
bool isParameterEncrypted = 0 != (rpcext.paramoptions[i] & TdsEnums.RPC_PARAM_ENCRYPTED);
// Additional information we need to send over wire to the server when writing encrypted parameters.
SqlColumnEncryptionInputParameterInfo encryptedParameterInfoToWrite = null;
// If the parameter is encrypted, we need to encrypt the value.
if (isParameterEncrypted) {
Debug.Assert(mt.TDSType != TdsEnums.SQLVARIANT &&
mt.TDSType != TdsEnums.SQLUDT &&
mt.TDSType != TdsEnums.SQLXMLTYPE &&
mt.TDSType != TdsEnums.SQLIMAGE &&
mt.TDSType != TdsEnums.SQLTEXT &&
mt.TDSType != TdsEnums.SQLNTEXT, "Type unsupported for encryption");
byte[] serializedValue = null;
byte[] encryptedValue = null;
if (!isNull) {
try {
if (isSqlVal) {
serializedValue = SerializeUnencryptedSqlValue(value, mt, actualSize, param.Offset, param.NormalizationRuleVersion, stateObj);
}
else {
// for codePageEncoded types, WriteValue simply expects the number of characters
// For plp types, we also need the encoded byte size
serializedValue = SerializeUnencryptedValue(value, mt, param.GetActualScale(), actualSize, param.Offset, isDataFeed, param.NormalizationRuleVersion, stateObj);
}
Debug.Assert(serializedValue != null, "serializedValue should not be null in TdsExecuteRPC.");
encryptedValue = SqlSecurityUtility.EncryptWithKey(serializedValue, param.CipherMetadata, _connHandler.ConnectionOptions.DataSource);
}
catch (Exception e) {
throw SQL.ParamEncryptionFailed(param.ParameterName, null, e);
}
Debug.Assert(encryptedValue != null && encryptedValue.Length > 0,
"encryptedValue should not be null or empty in TdsExecuteRPC.");
}
else {
encryptedValue = null;
}
// Change the datatype to varbinary(max).
// Since we don't know the size of the encrypted parameter on the server side, always set to (max).
//
mt = MetaType.MetaMaxVarBinary;
size = -1;
actualSize = (encryptedValue == null) ? 0 : encryptedValue.Length;
encryptedParameterInfoToWrite = new SqlColumnEncryptionInputParameterInfo(param.GetMetadataForTypeInfo(),
param.CipherMetadata);
// Set the value to the encrypted value and mark isSqlVal as false for VARBINARY encrypted value.
value = encryptedValue;
isSqlVal = false;
}
Debug.Assert(isParameterEncrypted == (encryptedParameterInfoToWrite != null),
"encryptedParameterInfoToWrite can be not null if and only if isParameterEncrypted is true.");
Debug.Assert(!isSqlVal || !isParameterEncrypted, "isParameterEncrypted can be true only if isSqlVal is false.");
//
// fixup the types by using the NullableType property of the MetaType class
//
// following rules should be followed based on feedback from the M-SQL team
// 1) always use the BIG* types (ex: instead of SQLCHAR use SQLBIGCHAR)
// 2) always use nullable types (ex: instead of SQLINT use SQLINTN)
// 3) DECIMALN should always be sent as NUMERICN
//
stateObj.WriteByte(mt.NullableType);
// handle variants here: the SQLVariant writing routine will write the maxlen and actual len columns
if (mt.TDSType == TdsEnums.SQLVARIANT) {
// devnote: Do we ever hit this codepath? Yes, when a null value is being writen out via a sql variant
// param.GetActualSize is not used
WriteSqlVariantValue(isSqlVal ? MetaType.GetComValueFromSqlVariant(value) : value, param.GetActualSize(), param.Offset, stateObj);
continue;
}
int codePageByteSize = 0;
int maxsize = 0;
if (mt.IsAnsiType) {
// Avoid the following code block if ANSI but unfilled LazyMat blob
if ((!isNull) && (!isDataFeed)) {
string s;
if (isSqlVal) {
if (value is SqlString) {
s = ((SqlString)value).Value;
}
else {
Debug.Assert(value is SqlChars, "Unknown value for Ansi datatype");
s = new String(((SqlChars)value).Value);
}
}
else {
s = (string)value;
}
codePageByteSize = GetEncodingCharLength(s, actualSize, param.Offset, _defaultEncoding);
}
if (mt.IsPlp) {
WriteShort(TdsEnums.SQL_USHORTVARMAXLEN, stateObj);
}
else {
maxsize = (size > codePageByteSize) ? size : codePageByteSize;
if (maxsize == 0) {
// Yukon doesn't like 0 as MaxSize. Change it to 2 for unicode types (SQL9 - 682322)
if (mt.IsNCharType)
maxsize = 2;
else
maxsize = 1;
}
WriteParameterVarLen(mt, maxsize, false/*IsNull*/, stateObj);
}
}
else {
// If type timestamp - treat as fixed type and always send over timestamp length, which is 8.
// For fixed types, we either send null or fixed length for type length. We want to match that
// behavior for timestamps. However, in the case of null, we still must send 8 because if we
// send null we will not receive a output val. You can send null for fixed types and still
// receive a output value, but not for variable types. So, always send 8 for timestamp because
// while the user sees it as a fixed type, we are actually representing it as a bigbinary which
// is variable.
if (mt.SqlDbType == SqlDbType.Timestamp) {
WriteParameterVarLen(mt, TdsEnums.TEXT_TIME_STAMP_LEN, false, stateObj);
}
else if (mt.SqlDbType == SqlDbType.Udt) {
byte[] udtVal = null;
Microsoft.SqlServer.Server.Format format = Microsoft.SqlServer.Server.Format.Native;
Debug.Assert(_isYukon, "Invalid DataType UDT for non-Yukon or later server!");
if (!isNull) {
udtVal = _connHandler.Connection.GetBytes(value, out format, out maxsize);
Debug.Assert(null != udtVal, "GetBytes returned null instance. Make sure that it always returns non-null value");
size = udtVal.Length;
//it may be legitimate, but we dont support it yet
if (size < 0 || (size >= UInt16.MaxValue && maxsize != -1))
throw new IndexOutOfRangeException();
}
//if this is NULL value, write special null value
byte[] lenBytes = BitConverter.GetBytes((Int64)size);
if (ADP.IsEmpty(param.UdtTypeName))
throw SQL.MustSetUdtTypeNameForUdtParams();
// Split the input name. TypeName is returned as single 3 part name during DeriveParameters.
// NOTE: ParseUdtTypeName throws if format is incorrect
String[] names = SqlParameter.ParseTypeName(param.UdtTypeName, true /* is UdtTypeName */);
if (!ADP.IsEmpty(names[0]) && TdsEnums.MAX_SERVERNAME < names[0].Length) {
throw ADP.ArgumentOutOfRange("names");
}
if (!ADP.IsEmpty(names[1]) && TdsEnums.MAX_SERVERNAME < names[names.Length - 2].Length) {
throw ADP.ArgumentOutOfRange("names");
}
if (TdsEnums.MAX_SERVERNAME < names[2].Length) {
throw ADP.ArgumentOutOfRange("names");
}
WriteUDTMetaData(value, names[0], names[1], names[2], stateObj);
//
if (!isNull) {
WriteUnsignedLong((ulong)udtVal.Length, stateObj); // PLP length
if (udtVal.Length > 0) { // Only write chunk length if its value is greater than 0
WriteInt(udtVal.Length, stateObj); // Chunk length
stateObj.WriteByteArray(udtVal, udtVal.Length, 0); // Value
}
WriteInt(0, stateObj); // Terminator
}
else {
WriteUnsignedLong(TdsEnums.SQL_PLP_NULL, stateObj); // PLP Null.
}
continue; // End of UDT - continue to next parameter.
//
}
else if (mt.IsPlp) {
if (mt.SqlDbType != SqlDbType.Xml)
WriteShort(TdsEnums.SQL_USHORTVARMAXLEN, stateObj);
}
else if ((!mt.IsVarTime) && (mt.SqlDbType != SqlDbType.Date)) { // Time, Date, DateTime2, DateTimeoffset do not have the size written out
maxsize = (size > actualSize) ? size : actualSize;
if (maxsize == 0 && IsYukonOrNewer) {
// Yukon doesn't like 0 as MaxSize. Change it to 2 for unicode types (SQL9 - 682322)
if (mt.IsNCharType)
maxsize = 2;
else
maxsize = 1;
}
WriteParameterVarLen(mt, maxsize, false/*IsNull*/, stateObj);
}
}
// scale and precision are only relevant for numeric and decimal types
if (mt.SqlDbType == SqlDbType.Decimal) {
if (0 == precision) {
if (_isShiloh)
stateObj.WriteByte(TdsEnums.DEFAULT_NUMERIC_PRECISION);
else
stateObj.WriteByte(TdsEnums.SPHINX_DEFAULT_NUMERIC_PRECISION);
}
else
stateObj.WriteByte(precision);
stateObj.WriteByte(scale);
}
else if (mt.IsVarTime) {
stateObj.WriteByte(param.GetActualScale());
}
// write out collation or xml metadata
if (_isYukon && (mt.SqlDbType == SqlDbType.Xml)) {
if (((param.XmlSchemaCollectionDatabase != null) && (param.XmlSchemaCollectionDatabase != ADP.StrEmpty)) ||
((param.XmlSchemaCollectionOwningSchema != null) && (param.XmlSchemaCollectionOwningSchema != ADP.StrEmpty)) ||
((param.XmlSchemaCollectionName != null) && (param.XmlSchemaCollectionName != ADP.StrEmpty))) {
stateObj.WriteByte(1); //Schema present flag
if ((param.XmlSchemaCollectionDatabase != null) && (param.XmlSchemaCollectionDatabase != ADP.StrEmpty)) {
tempLen = (param.XmlSchemaCollectionDatabase).Length;
stateObj.WriteByte((byte)(tempLen));
WriteString(param.XmlSchemaCollectionDatabase, tempLen, 0, stateObj);
}
else {
stateObj.WriteByte(0); // No dbname
}
if ((param.XmlSchemaCollectionOwningSchema != null) && (param.XmlSchemaCollectionOwningSchema != ADP.StrEmpty)) {
tempLen = (param.XmlSchemaCollectionOwningSchema).Length;
stateObj.WriteByte((byte)(tempLen));
WriteString(param.XmlSchemaCollectionOwningSchema, tempLen, 0, stateObj);
}
else {
stateObj.WriteByte(0); // no xml schema name
}
if ((param.XmlSchemaCollectionName != null) && (param.XmlSchemaCollectionName != ADP.StrEmpty)) {
tempLen = (param.XmlSchemaCollectionName).Length;
WriteShort((short)(tempLen), stateObj);
WriteString(param.XmlSchemaCollectionName, tempLen, 0, stateObj);
}
else {
WriteShort(0, stateObj); // No xml schema collection name
}
}
else {
stateObj.WriteByte(0); // No schema
}
}
else if (_isShiloh && mt.IsCharType) {
// if it is not supplied, simply write out our default collation, otherwise, write out the one attached to the parameter
SqlCollation outCollation = (param.Collation != null) ? param.Collation : _defaultCollation;
Debug.Assert(_defaultCollation != null, "_defaultCollation is null!");
WriteUnsignedInt(outCollation.info, stateObj);
stateObj.WriteByte(outCollation.sortId);
}
if (0 == codePageByteSize)
WriteParameterVarLen(mt, actualSize, isNull, stateObj, isDataFeed);
else
WriteParameterVarLen(mt, codePageByteSize, isNull, stateObj, isDataFeed);
Task writeParamTask = null;
// write the value now
if (!isNull) {
if (isSqlVal) {
writeParamTask = WriteSqlValue(value, mt, actualSize, codePageByteSize, param.Offset, stateObj);
}
else {
// for codePageEncoded types, WriteValue simply expects the number of characters
// For plp types, we also need the encoded byte size
writeParamTask = WriteValue(value, mt, isParameterEncrypted ? (byte)0 : param.GetActualScale(), actualSize, codePageByteSize, isParameterEncrypted ? 0 : param.Offset, stateObj, isParameterEncrypted ? 0 : param.Size, isDataFeed);
}
}
// Send encryption metadata for encrypted parameters.
if (isParameterEncrypted) {
writeParamTask = WriteEncryptionMetadata(writeParamTask, encryptedParameterInfoToWrite, stateObj);
}
if (!sync) {
if (writeParamTask == null) {
writeParamTask = stateObj.WaitForAccumulatedWrites();
}
if (writeParamTask != null) {
Task task = null;
if (completion == null) {
completion = new TaskCompletionSource<object>();
task = completion.Task;
}
AsyncHelper.ContinueTask(writeParamTask, completion,
() => TdsExecuteRPC(cmd, rpcArray, timeout, inSchema, notificationRequest, stateObj, isCommandProc, sync, completion,
startRpc: ii, startParam: i + 1),
connectionToDoom: _connHandler,
onFailure: exc => TdsExecuteRPC_OnFailure(exc, stateObj));
// Take care of releasing the locks
if (releaseConnectionLock) {
task.ContinueWith(_ => {
_connHandler._parserLock.Release();
}, TaskScheduler.Default);
releaseConnectionLock = false;
}
return task;
}
}
#if DEBUG
else {
Debug.Assert(writeParamTask == null, "Should not have a task when executing sync");
}
#endif
} // parameter for loop
// If this is not the last RPC we are sending, add the batch flag
if (ii < (rpcArray.Length - 1)) {
if (_isYukon) {
stateObj.WriteByte(TdsEnums.YUKON_RPCBATCHFLAG);
}
else {
stateObj.WriteByte(TdsEnums.SHILOH_RPCBATCHFLAG);
}
}
} // rpc for loop
Task execFlushTask = stateObj.ExecuteFlush();
Debug.Assert(!sync || execFlushTask == null, "Should not get a task when executing sync");
if (execFlushTask != null) {
Task task = null;
if (completion == null) {
completion = new TaskCompletionSource<object>();
task = completion.Task;
}
bool taskReleaseConnectionLock = releaseConnectionLock;
execFlushTask.ContinueWith(tsk => ExecuteFlushTaskCallback(tsk, stateObj, completion, taskReleaseConnectionLock), TaskScheduler.Default);
// ExecuteFlushTaskCallback will take care of the locks for us
releaseConnectionLock = false;
return task;
}
}
catch (Exception e) {
//
if (!ADP.IsCatchableExceptionType(e)) {
throw;
}
FailureCleanup(stateObj, e);
throw;
}
FinalizeExecuteRPC(stateObj);
if (completion != null) {
completion.SetResult(null);
}
return null;
}
catch (Exception e) {
FinalizeExecuteRPC(stateObj);
if (completion != null) {
completion.SetException(e);
return null;
}
else {
throw e;
}
}
finally {
Debug.Assert(firstCall || !releaseConnectionLock, "Shouldn't be releasing locks synchronously after the first call");
if (releaseConnectionLock) {
_connHandler._parserLock.Release();
}
}
}
private void FinalizeExecuteRPC(TdsParserStateObject stateObj) {
stateObj.SniContext = SniContext.Snix_Read;
_asyncWrite = false;
}
private void TdsExecuteRPC_OnFailure(Exception exc, TdsParserStateObject stateObj) {
RuntimeHelpers.PrepareConstrainedRegions();
try {
#if DEBUG
TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
RuntimeHelpers.PrepareConstrainedRegions();
try {
tdsReliabilitySection.Start();
#else
{
#endif //DEBUG
FailureCleanup(stateObj, exc);
}
#if DEBUG
finally {
tdsReliabilitySection.Stop();
}
#endif //DEBUG
}
catch (System.OutOfMemoryException) {
_connHandler.DoomThisConnection();
throw;
}
catch (System.StackOverflowException) {
_connHandler.DoomThisConnection();
throw;
}
catch (System.Threading.ThreadAbortException) {
_connHandler.DoomThisConnection();
throw;
}
}
private void ExecuteFlushTaskCallback(Task tsk, TdsParserStateObject stateObj, TaskCompletionSource<object> completion, bool releaseConnectionLock) {
try {
FinalizeExecuteRPC(stateObj);
if (tsk.Exception != null) {
Exception exc = tsk.Exception.InnerException;
RuntimeHelpers.PrepareConstrainedRegions();
try {
#if DEBUG
TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
RuntimeHelpers.PrepareConstrainedRegions();
try {
tdsReliabilitySection.Start();
#else
{
#endif //DEBUG
FailureCleanup(stateObj, tsk.Exception);
}
#if DEBUG
finally {
tdsReliabilitySection.Stop();
}
#endif //DEBUG
}
catch (System.OutOfMemoryException e) {
_connHandler.DoomThisConnection();
completion.SetException(e);
throw;
}
catch (System.StackOverflowException e) {
_connHandler.DoomThisConnection();
completion.SetException(e);
throw;
}
catch (System.Threading.ThreadAbortException e) {
_connHandler.DoomThisConnection();
completion.SetException(e);
throw;
}
catch (Exception e) {
exc = e;
}
completion.SetException(exc);
}
else {
completion.SetResult(null);
}
}
finally {
if (releaseConnectionLock) {
_connHandler._parserLock.Release();
}
}
}
private void WriteParameterName(string parameterName, TdsParserStateObject stateObj) {
// paramLen
// paramName
if (!ADP.IsEmpty(parameterName)) {
Debug.Assert(parameterName.Length <= 0xff, "parameter name can only be 255 bytes, shouldn't get to TdsParser!");
int tempLen = parameterName.Length & 0xff;
stateObj.WriteByte((byte)tempLen);
WriteString(parameterName, tempLen, 0, stateObj);
}
else {
stateObj.WriteByte(0);
}
}
private static readonly IEnumerable<MSS.SqlDataRecord> __tvpEmptyValue = new List<MSS.SqlDataRecord>().AsReadOnly();
private void WriteSmiParameter(SqlParameter param, int paramIndex, bool sendDefault, TdsParserStateObject stateObj) {
//
// Determine Metadata
//
ParameterPeekAheadValue peekAhead;
MSS.SmiParameterMetaData metaData = param.MetaDataForSmi(out peekAhead);
if (!_isKatmai) {
MetaType mt = MetaType.GetMetaTypeFromSqlDbType(metaData.SqlDbType, metaData.IsMultiValued);
throw ADP.VersionDoesNotSupportDataType(mt.TypeName);
}
//
// Determine value to send
//
object value;
MSS.ExtendedClrTypeCode typeCode;
// if we have an output or default param, set the value to null so we do not send it across to the server
if (sendDefault) {
// Value for TVP default is empty list, not NULL
if (SqlDbType.Structured == metaData.SqlDbType && metaData.IsMultiValued) {
value = __tvpEmptyValue;
typeCode = MSS.ExtendedClrTypeCode.IEnumerableOfSqlDataRecord;
}
else {
// Need to send null value for default
value = null;
typeCode = MSS.ExtendedClrTypeCode.DBNull;
}
}
else if (param.Direction == ParameterDirection.Output) {
bool isCLRType = param.ParamaterIsSqlType; // We have to forward the TYPE info, we need to know what type we are returning. Once we null the paramater we will no longer be able to distinguish what type were seeing.
param.Value = null;
value = null;
typeCode = MSS.ExtendedClrTypeCode.DBNull;
param.ParamaterIsSqlType = isCLRType;
}
else {
value = param.GetCoercedValue();
typeCode = MSS.MetaDataUtilsSmi.DetermineExtendedTypeCodeForUseWithSqlDbType(
metaData.SqlDbType, metaData.IsMultiValued, value, null, MSS.SmiContextFactory.KatmaiVersion);
}
if (Bid.AdvancedOn) {
Bid.Trace("<sc.TdsParser.WriteSmiParameter|ADV> %d#, Sending parameter '%ls', default flag=%d, metadata:\n", ObjectID, param.ParameterName, sendDefault?1:0);
Bid.PutStr(metaData.TraceString(3));
Bid.Trace("\n");
}
//
// Write parameter metadata
//
WriteSmiParameterMetaData(metaData, sendDefault, stateObj);
//
// Now write the value
//
TdsParameterSetter paramSetter = new TdsParameterSetter(stateObj, metaData);
MSS.ValueUtilsSmi.SetCompatibleValueV200(
new MSS.SmiEventSink_Default(), // TDS Errors/events dealt with at lower level for now, just need an object for processing
paramSetter,
0, // ordinal. TdsParameterSetter only handles one parameter at a time
metaData,
value,
typeCode,
param.Offset,
0 < param.Size ? param.Size : -1,
peekAhead);
}
// Writes metadata portion of parameter stream from an SmiParameterMetaData object.
private void WriteSmiParameterMetaData(MSS.SmiParameterMetaData metaData, bool sendDefault, TdsParserStateObject stateObj) {
// Determine status
byte status = 0;
if (ParameterDirection.Output == metaData.Direction || ParameterDirection.InputOutput == metaData.Direction) {
status |= TdsEnums.RPC_PARAM_BYREF;
}
if (sendDefault) {
status |= TdsEnums.RPC_PARAM_DEFAULT;
}
// Write everything out
WriteParameterName(metaData.Name, stateObj);
stateObj.WriteByte(status);
WriteSmiTypeInfo(metaData, stateObj);
}
// Write a TypeInfo stream
// Devnote: we remap the legacy types (text, ntext, and image) to SQLBIGVARCHAR, SQLNVARCHAR, and SQLBIGVARBINARY
private void WriteSmiTypeInfo(MSS.SmiExtendedMetaData metaData, TdsParserStateObject stateObj) {
switch(metaData.SqlDbType) {
case SqlDbType.BigInt:
stateObj.WriteByte(TdsEnums.SQLINTN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.Binary:
stateObj.WriteByte(TdsEnums.SQLBIGBINARY);
WriteUnsignedShort(checked((ushort)metaData.MaxLength), stateObj);
break;
case SqlDbType.Bit:
stateObj.WriteByte(TdsEnums.SQLBITN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.Char:
stateObj.WriteByte(TdsEnums.SQLBIGCHAR);
WriteUnsignedShort(checked((ushort)(metaData.MaxLength)), stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); //
stateObj.WriteByte(_defaultCollation.sortId);
break;
case SqlDbType.DateTime:
stateObj.WriteByte(TdsEnums.SQLDATETIMN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.Decimal:
stateObj.WriteByte(TdsEnums.SQLNUMERICN);
stateObj.WriteByte(checked((byte)MetaType.MetaDecimal.FixedLength)); // SmiMetaData's length and actual wire format's length are different
stateObj.WriteByte(0 == metaData.Precision ? (byte)1 : metaData.Precision);
stateObj.WriteByte(metaData.Scale);
break;
case SqlDbType.Float:
stateObj.WriteByte(TdsEnums.SQLFLTN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.Image:
stateObj.WriteByte(TdsEnums.SQLBIGVARBINARY);
WriteUnsignedShort(unchecked((ushort)MSS.SmiMetaData.UnlimitedMaxLengthIndicator), stateObj);
break;
case SqlDbType.Int:
stateObj.WriteByte(TdsEnums.SQLINTN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.Money:
stateObj.WriteByte(TdsEnums.SQLMONEYN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.NChar:
stateObj.WriteByte(TdsEnums.SQLNCHAR);
WriteUnsignedShort(checked((ushort)(metaData.MaxLength*2)), stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); //
stateObj.WriteByte(_defaultCollation.sortId);
break;
case SqlDbType.NText:
stateObj.WriteByte(TdsEnums.SQLNVARCHAR);
WriteUnsignedShort(unchecked((ushort)MSS.SmiMetaData.UnlimitedMaxLengthIndicator), stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); //
stateObj.WriteByte(_defaultCollation.sortId);
break;
case SqlDbType.NVarChar:
stateObj.WriteByte(TdsEnums.SQLNVARCHAR);
if (MSS.SmiMetaData.UnlimitedMaxLengthIndicator == metaData.MaxLength) {
WriteUnsignedShort(unchecked((ushort)MSS.SmiMetaData.UnlimitedMaxLengthIndicator), stateObj);
}
else {
WriteUnsignedShort(checked((ushort)(metaData.MaxLength*2)), stateObj);
}
WriteUnsignedInt(_defaultCollation.info, stateObj); //
stateObj.WriteByte(_defaultCollation.sortId);
break;
case SqlDbType.Real:
stateObj.WriteByte(TdsEnums.SQLFLTN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.UniqueIdentifier:
stateObj.WriteByte(TdsEnums.SQLUNIQUEID);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.SmallDateTime:
stateObj.WriteByte(TdsEnums.SQLDATETIMN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.SmallInt:
stateObj.WriteByte(TdsEnums.SQLINTN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.SmallMoney:
stateObj.WriteByte(TdsEnums.SQLMONEYN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.Text:
stateObj.WriteByte(TdsEnums.SQLBIGVARCHAR);
WriteUnsignedShort(unchecked((ushort)MSS.SmiMetaData.UnlimitedMaxLengthIndicator), stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); //
stateObj.WriteByte(_defaultCollation.sortId);
break;
case SqlDbType.Timestamp:
stateObj.WriteByte(TdsEnums.SQLBIGBINARY);
WriteShort(checked((int)metaData.MaxLength), stateObj);
break;
case SqlDbType.TinyInt:
stateObj.WriteByte(TdsEnums.SQLINTN);
stateObj.WriteByte(checked((byte)metaData.MaxLength));
break;
case SqlDbType.VarBinary:
stateObj.WriteByte(TdsEnums.SQLBIGVARBINARY);
WriteUnsignedShort(unchecked((ushort)metaData.MaxLength), stateObj);
break;
case SqlDbType.VarChar:
stateObj.WriteByte(TdsEnums.SQLBIGVARCHAR);
WriteUnsignedShort(unchecked((ushort)metaData.MaxLength), stateObj);
WriteUnsignedInt(_defaultCollation.info, stateObj); //
stateObj.WriteByte(_defaultCollation.sortId);
break;
case SqlDbType.Variant:
stateObj.WriteByte(TdsEnums.SQLVARIANT);
WriteInt(checked((int)metaData.MaxLength), stateObj);
break;
case SqlDbType.Xml:
stateObj.WriteByte(TdsEnums.SQLXMLTYPE);
// Is there a schema
if (ADP.IsEmpty(metaData.TypeSpecificNamePart1) && ADP.IsEmpty(metaData.TypeSpecificNamePart2) &&
ADP.IsEmpty(metaData.TypeSpecificNamePart3)) {
stateObj.WriteByte(0); // schema not present
}
else {
stateObj.WriteByte(1); // schema present
WriteIdentifier(metaData.TypeSpecificNamePart1, stateObj);
WriteIdentifier(metaData.TypeSpecificNamePart2, stateObj);
WriteIdentifierWithShortLength(metaData.TypeSpecificNamePart3, stateObj);
}
break;
case SqlDbType.Udt:
stateObj.WriteByte(TdsEnums.SQLUDT);
WriteIdentifier(metaData.TypeSpecificNamePart1, stateObj);
WriteIdentifier(metaData.TypeSpecificNamePart2, stateObj);
WriteIdentifier(metaData.TypeSpecificNamePart3, stateObj);
break;
case SqlDbType.Structured:
if (metaData.IsMultiValued) {
WriteTvpTypeInfo(metaData, stateObj);
}
else {
Debug.Assert(false, "SUDTs not yet supported.");
}
break;
case SqlDbType.Date:
stateObj.WriteByte(TdsEnums.SQLDATE);
break;
case SqlDbType.Time:
stateObj.WriteByte(TdsEnums.SQLTIME);
stateObj.WriteByte(metaData.Scale);
break;
case SqlDbType.DateTime2:
stateObj.WriteByte(TdsEnums.SQLDATETIME2);
stateObj.WriteByte(metaData.Scale);
break;
case SqlDbType.DateTimeOffset:
stateObj.WriteByte(TdsEnums.SQLDATETIMEOFFSET);
stateObj.WriteByte(metaData.Scale);
break;
default:
Debug.Assert(false, "Unknown SqlDbType should have been caught earlier!");
break;
}
}
private void WriteTvpTypeInfo(MSS.SmiExtendedMetaData metaData, TdsParserStateObject stateObj) {
Debug.Assert(SqlDbType.Structured == metaData.SqlDbType && metaData.IsMultiValued,
"Invalid metadata for TVPs. Type=" + metaData.SqlDbType);
// Type token
stateObj.WriteByte((byte)TdsEnums.SQLTABLE);
// 3-part name (DB, Schema, TypeName)
WriteIdentifier(metaData.TypeSpecificNamePart1, stateObj);
WriteIdentifier(metaData.TypeSpecificNamePart2, stateObj);
WriteIdentifier(metaData.TypeSpecificNamePart3, stateObj);
// TVP_COLMETADATA
if (0 == metaData.FieldMetaData.Count) {
WriteUnsignedShort((ushort)TdsEnums.TVP_NOMETADATA_TOKEN, stateObj);
}
else {
// COUNT of columns
WriteUnsignedShort(checked((ushort) metaData.FieldMetaData.Count), stateObj);
// TvpColumnMetaData for each column (look for defaults in this loop
MSS.SmiDefaultFieldsProperty defaults = (MSS.SmiDefaultFieldsProperty) metaData.ExtendedProperties[MSS.SmiPropertySelector.DefaultFields];
for(int i=0; i<metaData.FieldMetaData.Count; i++) {
WriteTvpColumnMetaData(metaData.FieldMetaData[i], defaults[i], stateObj);
}
// optional OrderUnique metadata
WriteTvpOrderUnique(metaData, stateObj);
}
// END of optional metadata
stateObj.WriteByte(TdsEnums.TVP_END_TOKEN);
}
// Write a single TvpColumnMetaData stream to the server
private void WriteTvpColumnMetaData(MSS.SmiExtendedMetaData md, bool isDefault, TdsParserStateObject stateObj) {
// User Type
if (SqlDbType.Timestamp == md.SqlDbType) {
WriteUnsignedInt(TdsEnums.SQLTIMESTAMP, stateObj);
} else {
WriteUnsignedInt(0, stateObj);
}
// Flags
ushort status = TdsEnums.Nullable;
if (isDefault) {
status |= TdsEnums.TVP_DEFAULT_COLUMN;
}
WriteUnsignedShort(status, stateObj);
// Type info
WriteSmiTypeInfo(md, stateObj);
// Column name
// per spec, "ColName is never sent to server or client for TVP, it is required within a TVP to be zero length."
WriteIdentifier(null, stateObj);
}
// temporary-results structure used only by WriteTvpOrderUnique
// use class to avoid List<T>'s per-struct-instantiated memory costs.
private class TdsOrderUnique {
internal short ColumnOrdinal;
internal byte Flags;
internal TdsOrderUnique(short ordinal, byte flags) {
ColumnOrdinal = ordinal;
Flags = flags;
}
}
private void WriteTvpOrderUnique(MSS.SmiExtendedMetaData metaData, TdsParserStateObject stateObj) {
// TVP_ORDER_UNIQUE token (uniqueness and sort order)
// Merge order and unique keys into a single token stream
MSS.SmiOrderProperty orderProperty = (MSS.SmiOrderProperty) metaData.ExtendedProperties[MSS.SmiPropertySelector.SortOrder];
MSS.SmiUniqueKeyProperty uniqueKeyProperty = (MSS.SmiUniqueKeyProperty) metaData.ExtendedProperties[MSS.SmiPropertySelector.UniqueKey];
// Build list from
List<TdsOrderUnique> columnList = new List<TdsOrderUnique>(metaData.FieldMetaData.Count);
for(int i=0; i<metaData.FieldMetaData.Count; i++) {
// Add appropriate SortOrder flag
byte flags = 0;
MSS.SmiOrderProperty.SmiColumnOrder columnOrder = orderProperty[i];
if (SortOrder.Ascending == columnOrder.Order) {
flags = TdsEnums.TVP_ORDERASC_FLAG;
}
else if (SortOrder.Descending == columnOrder.Order) {
flags = TdsEnums.TVP_ORDERDESC_FLAG;
}
// Add unique key flage if appropriate
if (uniqueKeyProperty[i]) {
flags |= TdsEnums.TVP_UNIQUE_FLAG;
}
// Remember this column if any flags were set
if (0 != flags) {
columnList.Add(new TdsOrderUnique(checked((short)(i+1)), flags));
}
}
// Write flagged columns to wire...
if (0 < columnList.Count) {
stateObj.WriteByte(TdsEnums.TVP_ORDER_UNIQUE_TOKEN);
WriteShort(columnList.Count, stateObj);
foreach(TdsOrderUnique column in columnList) {
WriteShort(column.ColumnOrdinal, stateObj);
stateObj.WriteByte(column.Flags);
}
}
}
internal Task WriteBulkCopyDone(TdsParserStateObject stateObj) {
// Write DONE packet
//
if (!(State == TdsParserState.OpenNotLoggedIn || State == TdsParserState.OpenLoggedIn)) {
throw ADP.ClosedConnectionError();
}
stateObj.WriteByte(TdsEnums.SQLDONE);
WriteShort(0, stateObj);
WriteShort(0, stateObj);
WriteInt(0, stateObj);
stateObj._pendingData = true;
stateObj._messageStatus = 0;
return stateObj.WritePacket(TdsEnums.HARDFLUSH);
}
/// <summary>
/// Loads the column encryptions keys into cache. This will read the master key info,
/// decrypt the CEK and keep it ready for encryption.
/// </summary>
/// <returns></returns>
internal void LoadColumnEncryptionKeys (_SqlMetaDataSet metadataCollection, string serverName) {
if (_serverSupportsColumnEncryption && ShouldEncryptValuesForBulkCopy()) {
for (int col = 0; col < metadataCollection.Length; col++) {
if (null != metadataCollection[col]) {
_SqlMetaData md = metadataCollection[col];
if (md.isEncrypted) {
SqlSecurityUtility.DecryptSymmetricKey(md.cipherMD, serverName);
}
}
}
}
}
/// <summary>
/// Writes a single entry of CEK Table into TDS Stream (for bulk copy).
/// </summary>
/// <returns></returns>
internal void WriteEncryptionEntries (ref SqlTceCipherInfoTable cekTable, TdsParserStateObject stateObj) {
for (int i =0; i < cekTable.Size; i++) {
// Write Db ID
WriteInt(cekTable[i].DatabaseId, stateObj);
// Write Key ID
WriteInt(cekTable[i].CekId, stateObj);
// Write Key Version
WriteInt(cekTable[i].CekVersion, stateObj);
// Write 8 bytes of key MD Version
Debug.Assert (8 == cekTable[i].CekMdVersion.Length);
stateObj.WriteByteArray (cekTable[i].CekMdVersion, 8, 0);
// We don't really need to send the keys
stateObj.WriteByte(0x00);
}
}
/// <summary>
/// Writes a CEK Table (as part of COLMETADATA token) for bulk copy.
/// </summary>
/// <returns></returns>
internal void WriteCekTable (_SqlMetaDataSet metadataCollection, TdsParserStateObject stateObj) {
if (!_serverSupportsColumnEncryption) {
return;
}
// If no cek table is present, send a count of 0 for table size
// Note- Cek table (with 0 entries) will be present if TCE
// was enabled and server supports it!
// OR if encryption was disabled in connection options
if (!metadataCollection.cekTable.HasValue ||
!ShouldEncryptValuesForBulkCopy()) {
WriteShort(0x00, stateObj);
return;
}
SqlTceCipherInfoTable cekTable = metadataCollection.cekTable.Value;
ushort count = (ushort)cekTable.Size;
WriteShort(count, stateObj);
WriteEncryptionEntries(ref cekTable, stateObj);
}
/// <summary>
/// Writes the UserType and TYPE_INFO values for CryptoMetadata (for bulk copy).
/// </summary>
/// <returns></returns>
internal void WriteTceUserTypeAndTypeInfo(SqlMetaDataPriv mdPriv, TdsParserStateObject stateObj) {
// Write the UserType (4 byte value)
WriteInt(0x0, stateObj); //
Debug.Assert(SqlDbType.Xml != mdPriv.type);
Debug.Assert(SqlDbType.Udt != mdPriv.type);
stateObj.WriteByte(mdPriv.tdsType);
switch (mdPriv.type) {
case SqlDbType.Decimal:
WriteTokenLength(mdPriv.tdsType, mdPriv.length, stateObj);
stateObj.WriteByte(mdPriv.precision);
stateObj.WriteByte(mdPriv.scale);
break;
case SqlDbType.Date:
// Nothing more to write!
break;
case SqlDbType.Time:
case SqlDbType.DateTime2:
case SqlDbType.DateTimeOffset:
stateObj.WriteByte(mdPriv.scale);
break;
default:
WriteTokenLength(mdPriv.tdsType, mdPriv.length, stateObj);
if (mdPriv.metaType.IsCharType && _isShiloh) {
WriteUnsignedInt(mdPriv.collation.info, stateObj);
stateObj.WriteByte(mdPriv.collation.sortId);
}
break;
}
}
/// <summary>
/// Writes the crypto metadata (as part of COLMETADATA token) for encrypted columns.
/// </summary>
/// <returns></returns>
internal void WriteCryptoMetadata(_SqlMetaData md, TdsParserStateObject stateObj) {
if (!_serverSupportsColumnEncryption || // TCE Feature supported
!md.isEncrypted || // Column is not encrypted
!ShouldEncryptValuesForBulkCopy()) { // TCE disabled on connection string
return;
}
// Write the ordinal
WriteShort (md.cipherMD.CekTableOrdinal, stateObj);
// Write UserType and TYPEINFO
WriteTceUserTypeAndTypeInfo(md.baseTI, stateObj);
// Write Encryption Algo
stateObj.WriteByte(md.cipherMD.CipherAlgorithmId);
if (TdsEnums.CustomCipherAlgorithmId == md.cipherMD.CipherAlgorithmId) {
// Write the algorithm name
Debug.Assert (md.cipherMD.CipherAlgorithmName.Length < 256);
stateObj.WriteByte((byte)md.cipherMD.CipherAlgorithmName.Length);
WriteString(md.cipherMD.CipherAlgorithmName, stateObj);
}
// Write Encryption Algo Type
stateObj.WriteByte(md.cipherMD.EncryptionType);
// Write Normalization Version
stateObj.WriteByte(md.cipherMD.NormalizationRuleVersion);
}
internal void WriteBulkCopyMetaData(_SqlMetaDataSet metadataCollection, int count, TdsParserStateObject stateObj) {
if (!(State == TdsParserState.OpenNotLoggedIn || State == TdsParserState.OpenLoggedIn)) {
throw ADP.ClosedConnectionError();
}
stateObj.WriteByte(TdsEnums.SQLCOLMETADATA);
WriteShort(count, stateObj);
// Write CEK table - 0 count
WriteCekTable(metadataCollection, stateObj);
for (int i = 0; i < metadataCollection.Length; i++) {
if (metadataCollection[i] != null) {
_SqlMetaData md = metadataCollection[i];
// read user type - 4 bytes Yukon, 2 backwards
if (IsYukonOrNewer) {
WriteInt(0x0, stateObj);
}
else {
WriteShort(0x0000, stateObj);
}
// Write the flags
UInt16 flags;
flags = (UInt16)(md.updatability << 2);
flags |= (UInt16)(md.isNullable ? (UInt16)TdsEnums.Nullable : (UInt16)0);
flags |= (UInt16)(md.isIdentity ? (UInt16)TdsEnums.Identity : (UInt16)0);
// Write the next byte of flags
if (_serverSupportsColumnEncryption) { // TCE Supported
if (ShouldEncryptValuesForBulkCopy()) { // TCE enabled on connection options
flags |= (UInt16)(md.isEncrypted ? (UInt16)(TdsEnums.IsEncrypted << 8) : (UInt16)0);
}
}
WriteShort(flags, stateObj);// write the flags
// todo:
// for xml WriteTokenLength results in a no-op
// discuss this with blaine ...
// (Microsoft) xml datatype does not have token length in its metadata. So it should be a noop.
switch (md.type) {
case SqlDbType.Decimal:
stateObj.WriteByte(md.tdsType);
WriteTokenLength(md.tdsType, md.length, stateObj);
stateObj.WriteByte(md.precision);
stateObj.WriteByte(md.scale);
break;
case SqlDbType.Xml:
//
stateObj.WriteByteArray(s_xmlMetadataSubstituteSequence, s_xmlMetadataSubstituteSequence.Length, 0);
break;
case SqlDbType.Udt:
stateObj.WriteByte(TdsEnums.SQLBIGVARBINARY);
WriteTokenLength(TdsEnums.SQLBIGVARBINARY, md.length, stateObj);
break;
case SqlDbType.Date:
stateObj.WriteByte(md.tdsType);
break;
case SqlDbType.Time:
case SqlDbType.DateTime2:
case SqlDbType.DateTimeOffset:
stateObj.WriteByte(md.tdsType);
stateObj.WriteByte(md.scale);
break;
default:
stateObj.WriteByte(md.tdsType);
WriteTokenLength(md.tdsType, md.length, stateObj);
if (md.metaType.IsCharType && _isShiloh) {
WriteUnsignedInt(md.collation.info, stateObj);
stateObj.WriteByte(md.collation.sortId);
}
break;
}
if (md.metaType.IsLong && !md.metaType.IsPlp) {
WriteShort(md.tableName.Length, stateObj);
WriteString(md.tableName, stateObj);
}
WriteCryptoMetadata(md, stateObj);
stateObj.WriteByte((byte)md.column.Length);
WriteString(md.column, stateObj);
}
} // end for loop
}
/// <summary>
/// Determines if a column value should be encrypted when using BulkCopy (based on connectionstring setting).
/// </summary>
/// <returns></returns>
internal bool ShouldEncryptValuesForBulkCopy () {
if (null != _connHandler &&
null != _connHandler.ConnectionOptions &&
SqlConnectionColumnEncryptionSetting.Enabled == _connHandler.ConnectionOptions.ColumnEncryptionSetting) {
return true;
}
return false;
}
/// <summary>
/// Encrypts a column value (for SqlBulkCopy)
/// </summary>
/// <returns></returns>
internal object EncryptColumnValue (object value, SqlMetaDataPriv metadata, string column, TdsParserStateObject stateObj, bool isDataFeed, bool isSqlType) {
Debug.Assert (_serverSupportsColumnEncryption, "Server doesn't support encryption, yet we received encryption metadata");
Debug.Assert (ShouldEncryptValuesForBulkCopy(), "Encryption attempted when not requested");
if (isDataFeed) { // can't encrypt a stream column
SQL.StreamNotSupportOnEncryptedColumn(column);
}
int actualLengthInBytes;
switch(metadata.baseTI.metaType.NullableType) {
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
// For some datatypes, engine does truncation before storing the value. (For example, when
// trying to insert a varbinary(7000) into a varbinary(3000) column). Since we encrypt the
// column values, engine has no way to tell the size of the plaintext datatype. Therefore,
// we truncate the values based on target column sizes here before encrypting them. This
// truncation is only needed if we exceed the max column length or if the target column is
// not a blob type (eg. varbinary(max)). The actual work of truncating the column happens
// when we normalize and serialize the data buffers. The serialization routine expects us
// to report the size of data to be copied out (for serialization). If we underreport the
// size, truncation will happen for us!
actualLengthInBytes = (isSqlType) ? ((SqlBinary)value).Length : ((byte[])value).Length;
if (metadata.baseTI.length > 0 &&
actualLengthInBytes > metadata.baseTI.length) { // see comments agove
actualLengthInBytes = metadata.baseTI.length;
}
break;
case TdsEnums.SQLUNIQUEID:
actualLengthInBytes = GUID_SIZE; // that's a constant for guid
break;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
if (null == _defaultEncoding)
{
ThrowUnsupportedCollationEncountered(null); // stateObject only when reading
}
string stringValue = (isSqlType) ? ((SqlString)value).Value : (string)value;
actualLengthInBytes = _defaultEncoding.GetByteCount(stringValue);
// If the string length is > max length, then use the max length (see comments above)
if (metadata.baseTI.length > 0 &&
actualLengthInBytes > metadata.baseTI.length) {
actualLengthInBytes = metadata.baseTI.length; // this ensure truncation!
}
break;
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
actualLengthInBytes = ((isSqlType) ? ((SqlString)value).Value.Length : ((string)value).Length) * 2;
if (metadata.baseTI.length > 0 &&
actualLengthInBytes > metadata.baseTI.length) { // see comments above
actualLengthInBytes = metadata.baseTI.length;
}
break;
default:
actualLengthInBytes = metadata.baseTI.length;
break;
}
byte[] serializedValue;
if (isSqlType) {
// SqlType
serializedValue = SerializeUnencryptedSqlValue (value,
metadata.baseTI.metaType,
actualLengthInBytes,
offset : 0,
normalizationVersion: metadata.cipherMD.NormalizationRuleVersion,
stateObj: stateObj);
}
else {
serializedValue = SerializeUnencryptedValue (value,
metadata.baseTI.metaType,
metadata.baseTI.scale,
actualLengthInBytes,
offset: 0,
isDataFeed: isDataFeed,
normalizationVersion: metadata.cipherMD.NormalizationRuleVersion,
stateObj: stateObj);
}
Debug.Assert(serializedValue != null, "serializedValue should not be null in TdsExecuteRPC.");
return SqlSecurityUtility.EncryptWithKey(
serializedValue,
metadata.cipherMD,
_connHandler.ConnectionOptions.DataSource);
}
internal Task WriteBulkCopyValue(object value, SqlMetaDataPriv metadata, TdsParserStateObject stateObj, bool isSqlType, bool isDataFeed, bool isNull) {
Debug.Assert(!isSqlType || value is INullable, "isSqlType is true, but value can not be type cast to an INullable");
Debug.Assert(!isDataFeed ^ value is DataFeed, "Incorrect value for isDataFeed");
Encoding saveEncoding = _defaultEncoding;
SqlCollation saveCollation = _defaultCollation;
int saveCodePage = _defaultCodePage;
int saveLCID = _defaultLCID;
Task resultTask = null;
Task internalWriteTask = null;
if (!(State == TdsParserState.OpenNotLoggedIn || State == TdsParserState.OpenLoggedIn)) {
throw ADP.ClosedConnectionError();
}
try {
if (metadata.encoding != null) {
_defaultEncoding = metadata.encoding;
}
if (metadata.collation != null) {
_defaultCollation = metadata.collation;
_defaultLCID = _defaultCollation.LCID;
}
_defaultCodePage = metadata.codePage;
MetaType metatype = metadata.metaType;
int ccb = 0;
int ccbStringBytes = 0;
if (isNull) {
// For UDT, remember we treat as binary even though it is a PLP
if (metatype.IsPlp && (metatype.NullableType != TdsEnums.SQLUDT || metatype.IsLong)) {
WriteLong(unchecked((long)TdsEnums.SQL_PLP_NULL), stateObj);
}
else if (!metatype.IsFixed && !metatype.IsLong && !metatype.IsVarTime) {
WriteShort(TdsEnums.VARNULL, stateObj);
}
else {
stateObj.WriteByte(TdsEnums.FIXEDNULL);
}
return resultTask;
}
if (!isDataFeed) {
switch (metatype.NullableType) {
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
case TdsEnums.SQLUDT:
ccb = (isSqlType) ? ((SqlBinary)value).Length : ((byte[])value).Length;
break;
case TdsEnums.SQLUNIQUEID:
ccb = GUID_SIZE; // that's a constant for guid
break;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
if (null == _defaultEncoding) {
ThrowUnsupportedCollationEncountered(null); // stateObject only when reading
}
string stringValue = null;
if (isSqlType) {
stringValue = ((SqlString)value).Value;
}
else {
stringValue = (string)value;
}
ccb = stringValue.Length;
ccbStringBytes = _defaultEncoding.GetByteCount(stringValue);
break;
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
ccb = ((isSqlType) ? ((SqlString)value).Value.Length : ((string)value).Length) * 2;
break;
case TdsEnums.SQLXMLTYPE:
// Value here could be string or XmlReader
if (value is XmlReader) {
value = MetaType.GetStringFromXml((XmlReader)value);
}
ccb = ((isSqlType) ? ((SqlString)value).Value.Length : ((string)value).Length) * 2;
break;
default:
ccb = metadata.length;
break;
}
}
else {
Debug.Assert(metatype.IsLong &&
((metatype.SqlDbType == SqlDbType.VarBinary && value is StreamDataFeed) ||
((metatype.SqlDbType == SqlDbType.VarChar || metatype.SqlDbType == SqlDbType.NVarChar) && value is TextDataFeed) ||
(metatype.SqlDbType == SqlDbType.Xml && value is XmlDataFeed)),
"Stream data feed should only be assigned to VarBinary(max), Text data feed should only be assigned to [N]VarChar(max), Xml data feed should only be assigned to XML(max)");
}
// Expected the text length in data stream for bulk copy of text, ntext, or image data.
//
if (metatype.IsLong) {
switch (metatype.SqlDbType) {
case SqlDbType.Text:
case SqlDbType.NText:
case SqlDbType.Image:
stateObj.WriteByteArray(s_longDataHeader, s_longDataHeader.Length, 0);
WriteTokenLength(metadata.tdsType, ccbStringBytes == 0 ? ccb : ccbStringBytes, stateObj);
break;
case SqlDbType.VarChar:
case SqlDbType.NVarChar:
case SqlDbType.VarBinary:
case SqlDbType.Xml:
case SqlDbType.Udt:
// plp data
WriteUnsignedLong(TdsEnums.SQL_PLP_UNKNOWNLEN, stateObj);
break;
}
}
else {
WriteTokenLength(metadata.tdsType, ccbStringBytes == 0 ? ccb : ccbStringBytes, stateObj);
}
if (isSqlType) {
internalWriteTask = WriteSqlValue(value, metatype, ccb, ccbStringBytes, 0, stateObj);
}
else if (metatype.SqlDbType != SqlDbType.Udt || metatype.IsLong) {
internalWriteTask = WriteValue(value, metatype, metadata.scale, ccb, ccbStringBytes, 0, stateObj, metadata.length, isDataFeed);
if ((internalWriteTask == null) && (_asyncWrite)) {
internalWriteTask = stateObj.WaitForAccumulatedWrites();
}
Debug.Assert(_asyncWrite || stateObj.WaitForAccumulatedWrites() == null, "Should not have accumulated writes when writing sync");
}
else {
WriteShort(ccb, stateObj);
internalWriteTask = stateObj.WriteByteArray((byte[])value, ccb, 0);
}
#if DEBUG
//In DEBUG mode, when SetAlwaysTaskOnWrite is true, we create a task. Allows us to verify async execution paths.
if (_asyncWrite && internalWriteTask == null && SqlBulkCopy.SetAlwaysTaskOnWrite == true) {
internalWriteTask = Task.FromResult<object>(null);
}
#endif
if (internalWriteTask != null) { //i.e. the write was async.
resultTask = WriteBulkCopyValueSetupContinuation(internalWriteTask, saveEncoding, saveCollation, saveCodePage, saveLCID);
}
}
finally {
if (internalWriteTask == null) {
_defaultEncoding = saveEncoding;
_defaultCollation = saveCollation;
_defaultCodePage = saveCodePage;
_defaultLCID = saveLCID;
}
}
return resultTask;
}
// This is in its own method to avoid always allocating the lambda in WriteBulkCopyValue
private Task WriteBulkCopyValueSetupContinuation(Task internalWriteTask, Encoding saveEncoding, SqlCollation saveCollation, int saveCodePage, int saveLCID) {
return internalWriteTask.ContinueWith<Task>(t => {
_defaultEncoding = saveEncoding;
_defaultCollation = saveCollation;
_defaultCodePage = saveCodePage;
_defaultLCID = saveLCID;
return t;
}, TaskScheduler.Default).Unwrap();
}
// Write mars header data, not including the mars header length
private void WriteMarsHeaderData(TdsParserStateObject stateObj, SqlInternalTransaction transaction) {
// Function to send over additional payload header data for Yukon and beyond only.
Debug.Assert(_isYukon, "WriteMarsHeaderData called on a non-Yukon server");
// These are not necessary - can have local started in distributed.
// Debug.Assert(!(null != sqlTransaction && null != distributedTransaction), "Error to have local (api started) and distributed transaction at the same time!");
// Debug.Assert(!(null != _userStartedLocalTransaction && null != distributedTransaction), "Error to have local (started outside of the api) and distributed transaction at the same time!");
// We may need to update the mars header length if mars header is changed in the future
WriteShort(TdsEnums.HEADERTYPE_MARS, stateObj);
if (null != transaction && SqlInternalTransaction.NullTransactionId != transaction.TransactionId) {
WriteLong(transaction.TransactionId, stateObj);
WriteInt(stateObj.IncrementAndObtainOpenResultCount(transaction), stateObj);
}
else {
// If no transaction, send over retained transaction descriptor (empty if none retained)
// and always 1 for result count.
WriteLong(_retainedTransactionId, stateObj);
WriteInt(stateObj.IncrementAndObtainOpenResultCount(null), stateObj);
}
}
private int GetNotificationHeaderSize(SqlNotificationRequest notificationRequest) {
if (null != notificationRequest) {
string callbackId = notificationRequest.UserData;
string service = notificationRequest.Options;
int timeout = notificationRequest.Timeout;
if (null == callbackId) {
throw ADP.ArgumentNull("CallbackId");
}
else if (UInt16.MaxValue < callbackId.Length) {
throw ADP.ArgumentOutOfRange("CallbackId");
}
if (null == service) {
throw ADP.ArgumentNull("Service");
}
else if (UInt16.MaxValue < service.Length) {
throw ADP.ArgumentOutOfRange("Service");
}
if (-1 > timeout) {
throw ADP.ArgumentOutOfRange("Timeout");
}
// Header Length (uint) (included in size) (already written to output buffer)
// Header Type (ushort)
// NotifyID Length (ushort)
// NotifyID UnicodeStream (unicode text)
// SSBDeployment Length (ushort)
// SSBDeployment UnicodeStream (unicode text)
// Timeout (uint) -- optional
// WEBDATA 102263: Don't send timeout value if it is 0
int headerLength = 4 + 2 + 2 + (callbackId.Length * 2) + 2 + (service.Length * 2);
if (timeout > 0)
headerLength += 4;
return headerLength;
}
else {
return 0;
}
}
// Write query notificaiton header data, not including the notificaiton header length
private void WriteQueryNotificationHeaderData(SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj) {
Debug.Assert(_isYukon, "WriteQueryNotificationHeaderData called on a non-Yukon server");
// We may need to update the notification header length if the header is changed in the future
Debug.Assert (null != notificationRequest, "notificaitonRequest is null");
string callbackId = notificationRequest.UserData;
string service = notificationRequest.Options;
int timeout = notificationRequest.Timeout;
// we did verification in GetNotificationHeaderSize, so just assert here.
Debug.Assert(null != callbackId, "CallbackId is null");
Debug.Assert(UInt16.MaxValue >= callbackId.Length, "CallbackId length is out of range");
Debug.Assert(null != service, "Service is null");
Debug.Assert(UInt16.MaxValue >= service.Length, "Service length is out of range");
Debug.Assert(-1 <= timeout, "Timeout");
Bid.NotificationsTrace("<sc.TdsParser.WriteQueryNotificationHeader|DEP> NotificationRequest: userData: '%ls', options: '%ls', timeout: '%d'\n", notificationRequest.UserData, notificationRequest.Options, notificationRequest.Timeout);
WriteShort(TdsEnums.HEADERTYPE_QNOTIFICATION, stateObj); // Query notifications Type
WriteShort(callbackId.Length * 2, stateObj); // Length in bytes
WriteString(callbackId, stateObj);
WriteShort(service.Length * 2, stateObj); // Length in bytes
WriteString(service, stateObj);
if (timeout > 0)
WriteInt(timeout, stateObj);
}
// Write the trace header data, not including the trace header length
private void WriteTraceHeaderData(TdsParserStateObject stateObj) {
Debug.Assert(this.IncludeTraceHeader, "WriteTraceHeaderData can only be called on a Denali or higher version server and bid trace with the control bit are on");
// We may need to update the trace header length if trace header is changed in the future
ActivityCorrelator.ActivityId actId = ActivityCorrelator.Current;
WriteShort(TdsEnums.HEADERTYPE_TRACE, stateObj); // Trace Header Type
stateObj.WriteByteArray(actId.Id.ToByteArray(), GUID_SIZE, 0); // Id (Guid)
WriteUnsignedInt(actId.Sequence, stateObj); // sequence number
Bid.Trace("<sc.TdsParser.WriteTraceHeaderData|INFO> ActivityID %ls\n", actId.ToString());
}
private void WriteRPCBatchHeaders(TdsParserStateObject stateObj, SqlNotificationRequest notificationRequest) {
Debug.Assert(_isYukon, "WriteRPCBatchHeaders can only be called on Yukon or higher version servers");
/* Header:
TotalLength - DWORD - including all headers and lengths, including itself
Each Data Session:
{
HeaderLength - DWORD - including all header length fields, including itself
HeaderType - USHORT
HeaderData
}
*/
int notificationHeaderSize = GetNotificationHeaderSize(notificationRequest);
const int marsHeaderSize = 18; // 4 + 2 + 8 + 4
// Header Length (DWORD)
// Header Type (ushort)
// Trace Data Guid
// Trace Data Sequence Number (uint)
const int traceHeaderSize = 26; // 4 + 2 + GUID_SIZE + sizeof(UInt32);
// TotalLength - DWORD - including all headers and lengths, including itself
int totalHeaderLength = this.IncludeTraceHeader ? (4 + marsHeaderSize + notificationHeaderSize + traceHeaderSize) : (4 + marsHeaderSize + notificationHeaderSize);
Debug.Assert(stateObj._outBytesUsed == stateObj._outputHeaderLen, "Output bytes written before total header length");
// Write total header length
WriteInt(totalHeaderLength, stateObj);
// Write Mars header length
WriteInt(marsHeaderSize, stateObj);
// Write Mars header data
WriteMarsHeaderData(stateObj, CurrentTransaction);
if (0 != notificationHeaderSize) {
// Write Notification header length
WriteInt(notificationHeaderSize, stateObj);
// Write notificaiton header data
WriteQueryNotificationHeaderData(notificationRequest, stateObj);
}
if (IncludeTraceHeader) {
// Write trace header length
WriteInt(traceHeaderSize, stateObj);
// Write trace header data
WriteTraceHeaderData(stateObj);
}
}
//
// Reverse function of GetTokenLength
//
private void WriteTokenLength(byte token, int length, TdsParserStateObject stateObj) {
int tokenLength = 0;
Debug.Assert(token != 0, "0 length token!");
// For Plp fields, this should only be used when writing to metadata header.
// For actual data length, WriteDataLength should be used.
// For Xml fields, there is no token length field. For MAX fields it is 0xffff.
if (_isYukon) { // Handle Yukon specific exceptions
if (TdsEnums.SQLUDT == token) {
tokenLength = 8;
}
else if (token == TdsEnums.SQLXMLTYPE) {
tokenLength = 8;
}
}
if (tokenLength == 0) {
switch (token & TdsEnums.SQLLenMask) {
case TdsEnums.SQLFixedLen:
Debug.Assert(length == 0x01 << ((token & 0x0c) >> 2), "length does not match encoded length in token");
tokenLength = 0;
break;
case TdsEnums.SQLZeroLen:
tokenLength = 0;
break;
case TdsEnums.SQLVarLen:
case TdsEnums.SQLVarCnt:
if (0 != (token & 0x80))
tokenLength = 2;
else if (0 == (token & 0x0c))
//
tokenLength = 4;
else
tokenLength = 1;
break;
default:
Debug.Assert(false, "Unknown token length!");
break;
}
switch (tokenLength) {
case 1:
stateObj.WriteByte((byte)length);
break;
case 2:
WriteShort(length, stateObj);
break;
case 4:
WriteInt(length, stateObj);
break;
case 8:
// In the metadata case we write 0xffff for partial length prefixed types.
// For actual data length preceding data, WriteDataLength should be used.
WriteShort(TdsEnums.SQL_USHORTVARMAXLEN, stateObj);
break;
} // end switch
}
}
// Returns true if BOM byte mark is needed for an XML value
private bool IsBOMNeeded(MetaType type, object value) {
if (type.NullableType == TdsEnums.SQLXMLTYPE) {
Type currentType = value.GetType();
if(currentType == typeof(SqlString)) {
if (!((SqlString)value).IsNull && ((((SqlString)value).Value).Length > 0)) {
if ((((SqlString)value).Value[0] & 0xff) != 0xff)
return true;
}
}
else if ((currentType == typeof(String)) && (((String)value).Length > 0)) {
if ((value != null) && (((String)value)[0] & 0xff) != 0xff)
return true;
}
else if (currentType == typeof(SqlXml)) {
if (!((SqlXml)value).IsNull)
return true;
}
else if (currentType == typeof(XmlDataFeed)) {
return true; // Values will eventually converted to unicode string here
}
}
return false;
}
private Task GetTerminationTask(Task unterminatedWriteTask, object value, MetaType type, int actualLength, TdsParserStateObject stateObj, bool isDataFeed) {
if (type.IsPlp && ((actualLength > 0) || isDataFeed)) {
if (unterminatedWriteTask == null) {
WriteInt(0, stateObj);
return null;
}
else {
return AsyncHelper.CreateContinuationTask<int, TdsParserStateObject>(unterminatedWriteTask,
WriteInt, 0, stateObj,
connectionToDoom: _connHandler);
}
}
else {
return unterminatedWriteTask;
}
}
private Task WriteSqlValue(object value, MetaType type, int actualLength, int codePageByteSize, int offset, TdsParserStateObject stateObj) {
return GetTerminationTask(
WriteUnterminatedSqlValue(value, type, actualLength, codePageByteSize, offset, stateObj),
value, type, actualLength, stateObj, false);
}
// For MAX types, this method can only write everything in one big chunk. If multiple
// chunk writes needed, please use WritePlpBytes/WritePlpChars
private Task WriteUnterminatedSqlValue(object value, MetaType type, int actualLength, int codePageByteSize, int offset, TdsParserStateObject stateObj) {
Debug.Assert(((type.NullableType == TdsEnums.SQLXMLTYPE) ||
(value is INullable && !((INullable)value).IsNull)),
"unexpected null SqlType!");
// parameters are always sent over as BIG or N types
switch (type.NullableType) {
case TdsEnums.SQLFLTN:
if (type.FixedLength == 4)
WriteFloat(((SqlSingle)value).Value, stateObj);
else {
Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!");
WriteDouble(((SqlDouble)value).Value, stateObj);
}
break;
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
{
if (type.IsPlp) {
WriteInt(actualLength, stateObj); // chunk length
}
if (value is SqlBinary) {
return stateObj.WriteByteArray(((SqlBinary)value).Value, actualLength, offset, canAccumulate:false);
}
else {
Debug.Assert(value is SqlBytes);
return stateObj.WriteByteArray(((SqlBytes)value).Value, actualLength, offset, canAccumulate:false);
}
}
case TdsEnums.SQLUNIQUEID:
{
byte[] b = ((SqlGuid)value).ToByteArray();
Debug.Assert((actualLength == b.Length) && (actualLength == 16), "Invalid length for guid type in com+ object");
stateObj.WriteByteArray(b, actualLength, 0);
break;
}
case TdsEnums.SQLBITN:
{
Debug.Assert(type.FixedLength == 1, "Invalid length for SqlBoolean type");
if (((SqlBoolean)value).Value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
}
case TdsEnums.SQLINTN:
if (type.FixedLength == 1)
stateObj.WriteByte(((SqlByte)value).Value);
else
if (type.FixedLength == 2)
WriteShort(((SqlInt16)value).Value, stateObj);
else
if (type.FixedLength == 4)
WriteInt(((SqlInt32)value).Value, stateObj);
else {
Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture));
WriteLong(((SqlInt64)value).Value, stateObj);
}
break;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
if (type.IsPlp) {
WriteInt(codePageByteSize, stateObj); // chunk length
}
if (value is SqlChars) {
String sch = new String(((SqlChars)value).Value);
return WriteEncodingChar(sch, actualLength, offset, _defaultEncoding, stateObj, canAccumulate:false);
}
else {
Debug.Assert(value is SqlString);
return WriteEncodingChar(((SqlString)value).Value, actualLength, offset, _defaultEncoding, stateObj, canAccumulate:false);
}
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
case TdsEnums.SQLXMLTYPE:
if (type.IsPlp) {
if(IsBOMNeeded(type, value)) {
WriteInt(actualLength+2, stateObj); // chunk length
WriteShort(TdsEnums.XMLUNICODEBOM , stateObj);
} else {
WriteInt(actualLength, stateObj); // chunk length
}
}
// convert to cchars instead of cbytes
// Xml type is already converted to string through GetCoercedValue
if (actualLength != 0)
actualLength >>= 1;
if (value is SqlChars) {
return WriteCharArray(((SqlChars)value).Value, actualLength, offset, stateObj, canAccumulate:false);
}
else {
Debug.Assert(value is SqlString);
return WriteString(((SqlString)value).Value, actualLength, offset, stateObj, canAccumulate:false);
}
case TdsEnums.SQLNUMERICN:
Debug.Assert(type.FixedLength <= 17, "Decimal length cannot be greater than 17 bytes");
WriteSqlDecimal((SqlDecimal)value, stateObj);
break;
case TdsEnums.SQLDATETIMN:
SqlDateTime dt = (SqlDateTime)value;
if (type.FixedLength == 4) {
if (0 > dt.DayTicks || dt.DayTicks > UInt16.MaxValue)
throw SQL.SmallDateTimeOverflow(dt.ToString());
WriteShort(dt.DayTicks, stateObj);
WriteShort(dt.TimeTicks / SqlDateTime.SQLTicksPerMinute, stateObj);
}
else {
WriteInt(dt.DayTicks, stateObj);
WriteInt(dt.TimeTicks, stateObj);
}
break;
case TdsEnums.SQLMONEYN:
{
WriteSqlMoney((SqlMoney)value, type.FixedLength, stateObj);
break;
}
case TdsEnums.SQLUDT:
Debug.Assert(false, "Called WriteSqlValue on UDT param.Should have already been handled");
throw SQL.UDTUnexpectedResult(value.GetType().AssemblyQualifiedName);
default:
Debug.Assert(false, "Unknown TdsType!" + type.NullableType.ToString("x2", (IFormatProvider)null));
break;
} // switch
// return point for accumualated writes, note: non-accumulated writes returned from their case statements
return null;
}
private class TdsOutputStream : Stream {
TdsParser _parser;
TdsParserStateObject _stateObj;
byte[] _preambleToStrip;
public TdsOutputStream(TdsParser parser, TdsParserStateObject stateObj, byte[] preambleToStrip) {
_parser = parser;
_stateObj = stateObj;
_preambleToStrip = preambleToStrip;
}
public override bool CanRead {
get { return false; }
}
public override bool CanSeek {
get { return false; }
}
public override bool CanWrite {
get { return true; }
}
public override void Flush() {
// NOOP
}
public override long Length {
get { throw new NotSupportedException(); }
}
public override long Position {
get {
throw new NotSupportedException();
}
set {
throw new NotSupportedException();
}
}
public override int Read(byte[] buffer, int offset, int count) {
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin) {
throw new NotSupportedException();
}
public override void SetLength(long value) {
throw new NotSupportedException();
}
private void StripPreamble(byte[] buffer, ref int offset, ref int count) {
if (_preambleToStrip != null && count >= _preambleToStrip.Length) {
for (int idx = 0; idx < _preambleToStrip.Length; idx++) {
if (_preambleToStrip[idx] != buffer[idx]) {
_preambleToStrip = null;
return;
}
}
offset += _preambleToStrip.Length;
count -= _preambleToStrip.Length;
}
_preambleToStrip = null;
}
public override void Write(byte[] buffer, int offset, int count) {
Debug.Assert(!_parser._asyncWrite);
ValidateWriteParameters(buffer, offset, count);
StripPreamble(buffer, ref offset, ref count);
if (count > 0) {
_parser.WriteInt(count, _stateObj); // write length of chunk
_stateObj.WriteByteArray(buffer, count, offset);
}
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) {
Debug.Assert(_parser._asyncWrite);
ValidateWriteParameters(buffer, offset, count);
StripPreamble(buffer, ref offset, ref count);
RuntimeHelpers.PrepareConstrainedRegions();
try {
#if DEBUG
TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();
RuntimeHelpers.PrepareConstrainedRegions();
try {
tdsReliabilitySection.Start();
#else
{
#endif //DEBUG
Task task = null;
if (count > 0) {
_parser.WriteInt(count, _stateObj); // write length of chunk
task = _stateObj.WriteByteArray(buffer, count, offset, canAccumulate: false);
}
if (task == null) {
return CompletedTask;
}
else {
return task;
}
}
#if DEBUG
finally {
tdsReliabilitySection.Stop();
}
#endif //DEBUG
}
catch (System.OutOfMemoryException) {
_parser._connHandler.DoomThisConnection();
throw;
}
catch (System.StackOverflowException) {
_parser._connHandler.DoomThisConnection();
throw;
}
catch (System.Threading.ThreadAbortException) {
_parser._connHandler.DoomThisConnection();
throw;
}
}
internal static void ValidateWriteParameters(byte[] buffer, int offset, int count) {
if (buffer == null) {
throw ADP.ArgumentNull(ADP.ParameterBuffer);
}
if (offset < 0) {
throw ADP.ArgumentOutOfRange(ADP.ParameterOffset);
}
if (count < 0) {
throw ADP.ArgumentOutOfRange(ADP.ParameterCount);
}
try {
if (checked(offset + count) > buffer.Length) {
throw ExceptionBuilder.InvalidOffsetLength();
}
}
catch (OverflowException) {
// If we've overflowed when adding offset and count, then they never would have fit into buffer anyway
throw ExceptionBuilder.InvalidOffsetLength();
}
}
}
private class ConstrainedTextWriter : TextWriter {
TextWriter _next;
int _size;
int _written;
public ConstrainedTextWriter(TextWriter next, int size) {
_next = next;
_size = size;
_written = 0;
if (_size < 1) {
_size = int.MaxValue;
}
}
public bool IsComplete {
get {
return _size > 0 && _written >= _size;
}
}
public override Encoding Encoding {
get { return _next.Encoding; }
}
public override void Flush() {
_next.Flush();
}
public override Task FlushAsync() {
return _next.FlushAsync();
}
public override void Write(char value) {
if (_written < _size) {
_next.Write(value);
_written++;
}
Debug.Assert(_size < 0 || _written <= _size, string.Format("Length of data written exceeds specified length. Written: {0}, specified: {1}", _written, _size));
}
public override void Write(char[] buffer, int index, int count) {
ValidateWriteParameters(buffer, index, count);
Debug.Assert(_size >= _written);
count = Math.Min(_size - _written, count);
if (count > 0) {
_next.Write(buffer, index, count);
}
_written += count;
}
public override Task WriteAsync(char value) {
if (_written < _size) {
_written++;
return _next.WriteAsync(value);
}
return CompletedTask;
}
public override Task WriteAsync(char[] buffer, int index, int count) {
ValidateWriteParameters(buffer, index, count);
Debug.Assert(_size >= _written);
count = Math.Min(_size - _written, count);
if (count > 0) {
_written += count;
return _next.WriteAsync(buffer, index, count);
}
return CompletedTask;
}
public override Task WriteAsync(string value) {
return WriteAsync(value.ToCharArray());
}
internal static void ValidateWriteParameters(char[] buffer, int offset, int count) {
if (buffer == null) {
throw ADP.ArgumentNull(ADP.ParameterBuffer);
}
if (offset < 0) {
throw ADP.ArgumentOutOfRange(ADP.ParameterOffset);
}
if (count < 0) {
throw ADP.ArgumentOutOfRange(ADP.ParameterCount);
}
try {
if (checked(offset + count) > buffer.Length) {
throw ExceptionBuilder.InvalidOffsetLength();
}
}
catch (OverflowException) {
// If we've overflowed when adding offset and count, then they never would have fit into buffer anyway
throw ExceptionBuilder.InvalidOffsetLength();
}
}
}
private async Task WriteXmlFeed(XmlDataFeed feed, TdsParserStateObject stateObj, bool needBom, Encoding encoding, int size) {
byte[] preambleToSkip = null;
if (!needBom) {
preambleToSkip = encoding.GetPreamble();
}
ConstrainedTextWriter writer = new ConstrainedTextWriter(new StreamWriter(new TdsOutputStream(this, stateObj, preambleToSkip), encoding), size);
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.CloseOutput = false; // don't close the memory stream
writerSettings.ConformanceLevel = ConformanceLevel.Fragment;
if (_asyncWrite) {
writerSettings.Async = true;
}
XmlWriter ww = XmlWriter.Create(writer, writerSettings);
if (feed._source.ReadState == ReadState.Initial) {
feed._source.Read();
}
while (!feed._source.EOF && !writer.IsComplete) {
// We are copying nodes from a reader to a writer. This will cause the
// XmlDeclaration to be emitted despite ConformanceLevel.Fragment above.
// Therefore, we filter out the XmlDeclaration while copying.
if (feed._source.NodeType == XmlNodeType.XmlDeclaration) {
feed._source.Read();
continue;
}
if (_asyncWrite) {
await ww.WriteNodeAsync(feed._source, true).ConfigureAwait(false);
}
else {
ww.WriteNode(feed._source, true);
}
}
if (_asyncWrite) {
await ww.FlushAsync().ConfigureAwait(false);
}
else {
ww.Flush();
}
}
private async Task WriteTextFeed(TextDataFeed feed, Encoding encoding, bool needBom, TdsParserStateObject stateObj, int size) {
Debug.Assert(encoding == null || !needBom);
char[] inBuff = new char[constTextBufferSize];
encoding = encoding ?? new UnicodeEncoding(false, false);
ConstrainedTextWriter writer = new ConstrainedTextWriter(new StreamWriter(new TdsOutputStream(this, stateObj, null), encoding), size);
if (needBom) {
if (_asyncWrite) {
await writer.WriteAsync((char)TdsEnums.XMLUNICODEBOM).ConfigureAwait(false);
}
else {
writer.Write((char)TdsEnums.XMLUNICODEBOM);
}
}
int nWritten = 0;
do {
int nRead = 0;
if (_asyncWrite) {
nRead = await feed._source.ReadBlockAsync(inBuff, 0, constTextBufferSize).ConfigureAwait(false);
}
else {
nRead = feed._source.ReadBlock(inBuff, 0, constTextBufferSize);
}
if (nRead == 0) {
break;
}
if (_asyncWrite) {
await writer.WriteAsync(inBuff, 0, nRead).ConfigureAwait(false);
}
else {
writer.Write(inBuff, 0, nRead);
}
nWritten += nRead;
} while (!writer.IsComplete);
if (_asyncWrite) {
await writer.FlushAsync().ConfigureAwait(false);
}
else {
writer.Flush();
}
}
private async Task WriteStreamFeed(StreamDataFeed feed, TdsParserStateObject stateObj, int len) {
TdsOutputStream output = new TdsOutputStream(this, stateObj, null);
byte[] buff = new byte[constBinBufferSize];
int nWritten = 0;
do {
int nRead = 0;
int readSize = constBinBufferSize;
if (len > 0 && nWritten + readSize > len) {
readSize = len - nWritten;
}
Debug.Assert(readSize >= 0);
if (_asyncWrite) {
nRead = await feed._source.ReadAsync(buff, 0, readSize).ConfigureAwait(false);
}
else {
nRead = feed._source.Read(buff, 0, readSize);
}
if (nRead == 0) {
return;
}
if (_asyncWrite) {
await output.WriteAsync(buff, 0, nRead).ConfigureAwait(false);
}
else {
output.Write(buff, 0, nRead);
}
nWritten += nRead;
} while (len <= 0 || nWritten < len);
}
private Task NullIfCompletedWriteTask(Task task) {
if (task == null) {
return null;
}
switch (task.Status) {
case TaskStatus.RanToCompletion:
return null;
case TaskStatus.Faulted:
throw task.Exception.InnerException;
case TaskStatus.Canceled:
throw SQL.OperationCancelled();
default:
return task;
}
}
private Task WriteValue(object value, MetaType type, byte scale, int actualLength, int encodingByteSize, int offset, TdsParserStateObject stateObj, int paramSize, bool isDataFeed) {
return GetTerminationTask(WriteUnterminatedValue(value, type, scale, actualLength, encodingByteSize, offset, stateObj, paramSize, isDataFeed),
value, type, actualLength, stateObj, isDataFeed);
}
// For MAX types, this method can only write everything in one big chunk. If multiple
// chunk writes needed, please use WritePlpBytes/WritePlpChars
private Task WriteUnterminatedValue(object value, MetaType type, byte scale, int actualLength, int encodingByteSize, int offset, TdsParserStateObject stateObj, int paramSize, bool isDataFeed)
{
Debug.Assert((null != value) && (DBNull.Value != value), "unexpected missing or empty object");
// parameters are always sent over as BIG or N types
switch (type.NullableType) {
case TdsEnums.SQLFLTN:
if (type.FixedLength == 4)
WriteFloat((Single)value, stateObj);
else {
Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!");
WriteDouble((Double)value, stateObj);
}
break;
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
case TdsEnums.SQLUDT: {
// An array should be in the object
Debug.Assert(isDataFeed || value is byte[], "Value should be an array of bytes");
Debug.Assert(!isDataFeed || value is StreamDataFeed, "Value should be a stream");
if (isDataFeed) {
Debug.Assert(type.IsPlp,"Stream assigned to non-PLP was not converted!");
return NullIfCompletedWriteTask(WriteStreamFeed((StreamDataFeed)value, stateObj, paramSize));
}
else {
if (type.IsPlp) {
WriteInt(actualLength, stateObj); // chunk length
}
return stateObj.WriteByteArray((byte[])value, actualLength, offset, canAccumulate: false);
}
}
case TdsEnums.SQLUNIQUEID: {
System.Guid guid = (System.Guid)value;
byte[] b = guid.ToByteArray();
Debug.Assert((actualLength == b.Length) && (actualLength == 16), "Invalid length for guid type in com+ object");
stateObj.WriteByteArray(b, actualLength, 0);
break;
}
case TdsEnums.SQLBITN: {
Debug.Assert(type.FixedLength == 1, "Invalid length for SqlBoolean type");
if ((bool)value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
}
case TdsEnums.SQLINTN:
if (type.FixedLength == 1)
stateObj.WriteByte((byte)value);
else if (type.FixedLength == 2)
WriteShort((Int16)value, stateObj);
else if (type.FixedLength == 4)
WriteInt((Int32)value, stateObj);
else {
Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture));
WriteLong((Int64)value, stateObj);
}
break;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT: {
Debug.Assert(!isDataFeed || (value is TextDataFeed || value is XmlDataFeed), "Value must be a TextReader or XmlReader");
Debug.Assert(isDataFeed || (value is string || value is byte[]), "Value is a byte array or string");
if (isDataFeed) {
Debug.Assert(type.IsPlp, "Stream assigned to non-PLP was not converted!");
TextDataFeed tdf = value as TextDataFeed;
if (tdf == null) {
return NullIfCompletedWriteTask(WriteXmlFeed((XmlDataFeed)value, stateObj, needBom:true, encoding:_defaultEncoding, size:paramSize));
}
else {
return NullIfCompletedWriteTask(WriteTextFeed(tdf, _defaultEncoding, false, stateObj, paramSize));
}
}
else {
if (type.IsPlp) {
WriteInt(encodingByteSize, stateObj); // chunk length
}
if (value is byte[]) { // If LazyMat non-filled blob, send cookie rather than value
return stateObj.WriteByteArray((byte[])value, actualLength, 0, canAccumulate: false);
}
else {
return WriteEncodingChar((string)value, actualLength, offset, _defaultEncoding, stateObj, canAccumulate: false);
}
}
}
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
case TdsEnums.SQLXMLTYPE: {
Debug.Assert(!isDataFeed || (value is TextDataFeed || value is XmlDataFeed), "Value must be a TextReader or XmlReader");
Debug.Assert(isDataFeed || (value is string || value is byte[]), "Value is a byte array or string");
if (isDataFeed) {
Debug.Assert(type.IsPlp, "Stream assigned to non-PLP was not converted!");
TextDataFeed tdf = value as TextDataFeed;
if (tdf == null) {
return NullIfCompletedWriteTask(WriteXmlFeed((XmlDataFeed)value, stateObj, IsBOMNeeded(type, value), Encoding.Unicode, paramSize));
}
else {
return NullIfCompletedWriteTask(WriteTextFeed(tdf, null, IsBOMNeeded(type, value), stateObj, paramSize));
}
}
else {
if (type.IsPlp) {
if (IsBOMNeeded(type, value)) {
WriteInt(actualLength + 2, stateObj); // chunk length
WriteShort(TdsEnums.XMLUNICODEBOM, stateObj);
}
else {
WriteInt(actualLength, stateObj); // chunk length
}
}
if (value is byte[]) { // If LazyMat non-filled blob, send cookie rather than value
return stateObj.WriteByteArray((byte[])value, actualLength, 0, canAccumulate: false);
}
else {
// convert to cchars instead of cbytes
actualLength >>= 1;
return WriteString((string)value, actualLength, offset, stateObj, canAccumulate: false);
}
}
}
case TdsEnums.SQLNUMERICN:
Debug.Assert(type.FixedLength <= 17, "Decimal length cannot be greater than 17 bytes");
WriteDecimal((Decimal)value, stateObj);
break;
case TdsEnums.SQLDATETIMN:
Debug.Assert(type.FixedLength <= 0xff, "Invalid Fixed Length");
TdsDateTime dt = MetaType.FromDateTime((DateTime)value, (byte)type.FixedLength);
if (type.FixedLength == 4) {
if (0 > dt.days || dt.days > UInt16.MaxValue)
throw SQL.SmallDateTimeOverflow(MetaType.ToDateTime(dt.days, dt.time, 4).ToString(CultureInfo.InvariantCulture));
WriteShort(dt.days, stateObj);
WriteShort(dt.time, stateObj);
}
else {
WriteInt(dt.days, stateObj);
WriteInt(dt.time, stateObj);
}
break;
case TdsEnums.SQLMONEYN: {
WriteCurrency((Decimal)value, type.FixedLength, stateObj);
break;
}
case TdsEnums.SQLDATE: {
WriteDate((DateTime)value, stateObj);
break;
}
case TdsEnums.SQLTIME:
if (scale > TdsEnums.DEFAULT_VARTIME_SCALE) {
throw SQL.TimeScaleValueOutOfRange(scale);
}
WriteTime((TimeSpan)value, scale, actualLength, stateObj);
break;
case TdsEnums.SQLDATETIME2:
if (scale > TdsEnums.DEFAULT_VARTIME_SCALE) {
throw SQL.TimeScaleValueOutOfRange(scale);
}
WriteDateTime2((DateTime)value, scale, actualLength, stateObj);
break;
case TdsEnums.SQLDATETIMEOFFSET:
WriteDateTimeOffset((DateTimeOffset)value, scale, actualLength, stateObj);
break;
default:
Debug.Assert(false, "Unknown TdsType!" + type.NullableType.ToString("x2", (IFormatProvider)null));
break;
} // switch
// return point for accumualated writes, note: non-accumulated writes returned from their case statements
return null;
// Debug.WriteLine("value: " + value.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Write parameter encryption metadata and returns a task if necessary.
/// </summary>
private Task WriteEncryptionMetadata(Task terminatedWriteTask, SqlColumnEncryptionInputParameterInfo columnEncryptionParameterInfo, TdsParserStateObject stateObj) {
Debug.Assert(columnEncryptionParameterInfo != null, @"columnEncryptionParameterInfo cannot be null");
Debug.Assert(stateObj != null, @"stateObj cannot be null");
// If there is not task already, simply write the encryption metadata synchronously.
if (terminatedWriteTask == null) {
WriteEncryptionMetadata(columnEncryptionParameterInfo, stateObj);
return null;
}
else {
// Otherwise, create a continuation task to write the encryption metadata after the previous write completes.
return AsyncHelper.CreateContinuationTask<SqlColumnEncryptionInputParameterInfo, TdsParserStateObject>(terminatedWriteTask,
WriteEncryptionMetadata, columnEncryptionParameterInfo, stateObj,
connectionToDoom: _connHandler);
}
}
/// <summary>
/// Write parameter encryption metadata.
/// </summary>
private void WriteEncryptionMetadata(SqlColumnEncryptionInputParameterInfo columnEncryptionParameterInfo, TdsParserStateObject stateObj) {
Debug.Assert(columnEncryptionParameterInfo != null, @"columnEncryptionParameterInfo cannot be null");
Debug.Assert(stateObj != null, @"stateObj cannot be null");
// Write the TypeInfo.
WriteSmiTypeInfo(columnEncryptionParameterInfo.ParameterMetadata, stateObj);
// Write the serialized array in columnEncryptionParameterInfo.
stateObj.WriteByteArray(columnEncryptionParameterInfo.SerializedWireFormat,
columnEncryptionParameterInfo.SerializedWireFormat.Length,
offsetBuffer: 0);
}
// For MAX types, this method can only write everything in one big chunk. If multiple
// chunk writes needed, please use WritePlpBytes/WritePlpChars
private byte[] SerializeUnencryptedValue(object value, MetaType type, byte scale, int actualLength, int offset, bool isDataFeed, byte normalizationVersion, TdsParserStateObject stateObj) {
Debug.Assert((null != value) && (DBNull.Value != value), "unexpected missing or empty object");
if (normalizationVersion != 0x01) {
throw SQL.UnsupportedNormalizationVersion(normalizationVersion);
}
// parameters are always sent over as BIG or N types
switch (type.NullableType) {
case TdsEnums.SQLFLTN:
if (type.FixedLength == 4)
return SerializeFloat((Single)value);
else {
Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!");
return SerializeDouble((Double)value);
}
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
case TdsEnums.SQLUDT: {
Debug.Assert(!isDataFeed, "We cannot seriliaze streams");
Debug.Assert(value is byte[], "Value should be an array of bytes");
byte[] b = new byte[actualLength];
Buffer.BlockCopy((byte[])value, offset, b, 0, actualLength);
return b;
}
case TdsEnums.SQLUNIQUEID: {
System.Guid guid = (System.Guid)value;
byte[] b = guid.ToByteArray();
Debug.Assert((actualLength == b.Length) && (actualLength == 16), "Invalid length for guid type in com+ object");
return b;
}
case TdsEnums.SQLBITN: {
Debug.Assert(type.FixedLength == 1, "Invalid length for SqlBoolean type");
// We normalize to allow conversion across data types. BIT is serialized into a BIGINT.
return SerializeLong((bool)value == true ? 1 : 0, stateObj);
}
case TdsEnums.SQLINTN:
if (type.FixedLength == 1)
return SerializeLong((byte)value, stateObj);
if (type.FixedLength == 2)
return SerializeLong((Int16)value, stateObj);
if (type.FixedLength == 4)
return SerializeLong((Int32)value, stateObj);
Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture));
return SerializeLong((Int64)value, stateObj);
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT: {
Debug.Assert(!isDataFeed, "We cannot seriliaze streams");
Debug.Assert((value is string || value is byte[]), "Value is a byte array or string");
if (value is byte[]) { // If LazyMat non-filled blob, send cookie rather than value
byte[] b = new byte[actualLength];
Buffer.BlockCopy((byte[])value, 0, b, 0, actualLength);
return b;
}
else {
return SerializeEncodingChar((string)value, actualLength, offset, _defaultEncoding);
}
}
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
case TdsEnums.SQLXMLTYPE: {
Debug.Assert(!isDataFeed, "We cannot seriliaze streams");
Debug.Assert((value is string || value is byte[]), "Value is a byte array or string");
if (value is byte[]) { // If LazyMat non-filled blob, send cookie rather than value
byte[] b = new byte[actualLength];
Buffer.BlockCopy((byte[])value, 0, b, 0, actualLength);
return b;
}
else { // convert to cchars instead of cbytes
actualLength >>= 1;
return SerializeString((string)value, actualLength, offset);
}
}
case TdsEnums.SQLNUMERICN:
Debug.Assert(type.FixedLength <= 17, "Decimal length cannot be greater than 17 bytes");
return SerializeDecimal((Decimal)value, stateObj);
case TdsEnums.SQLDATETIMN:
Debug.Assert(type.FixedLength <= 0xff, "Invalid Fixed Length");
TdsDateTime dt = MetaType.FromDateTime((DateTime)value, (byte)type.FixedLength);
if (type.FixedLength == 4) {
if (0 > dt.days || dt.days > UInt16.MaxValue)
throw SQL.SmallDateTimeOverflow(MetaType.ToDateTime(dt.days, dt.time, 4).ToString(CultureInfo.InvariantCulture));
if (null == stateObj._bIntBytes) {
stateObj._bIntBytes = new byte[4];
}
byte[] b = stateObj._bIntBytes;
int current = 0;
byte[] bPart = SerializeShort(dt.days, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 2);
current += 2;
bPart = SerializeShort(dt.time, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 2);
return b;
}
else {
if (null == stateObj._bLongBytes) {
stateObj._bLongBytes = new byte[8];
}
byte[] b = stateObj._bLongBytes;
int current = 0;
byte[] bPart = SerializeInt(dt.days, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 4);
current += 4;
bPart = SerializeInt(dt.time, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 4);
return b;
}
case TdsEnums.SQLMONEYN: {
return SerializeCurrency((Decimal)value, type.FixedLength, stateObj);
}
case TdsEnums.SQLDATE: {
return SerializeDate((DateTime)value);
}
case TdsEnums.SQLTIME:
if (scale > TdsEnums.DEFAULT_VARTIME_SCALE) {
throw SQL.TimeScaleValueOutOfRange(scale);
}
return SerializeTime((TimeSpan)value, scale, actualLength);
case TdsEnums.SQLDATETIME2:
if (scale > TdsEnums.DEFAULT_VARTIME_SCALE) {
throw SQL.TimeScaleValueOutOfRange(scale);
}
return SerializeDateTime2((DateTime)value, scale, actualLength);
case TdsEnums.SQLDATETIMEOFFSET:
if (scale > TdsEnums.DEFAULT_VARTIME_SCALE) {
throw SQL.TimeScaleValueOutOfRange(scale);
}
return SerializeDateTimeOffset((DateTimeOffset)value, scale, actualLength);
default:
throw SQL.UnsupportedDatatypeEncryption(type.TypeName);
} // switch
// Debug.WriteLine("value: " + value.ToString(CultureInfo.InvariantCulture));
}
// For MAX types, this method can only write everything in one big chunk. If multiple
// chunk writes needed, please use WritePlpBytes/WritePlpChars
private byte[] SerializeUnencryptedSqlValue(object value, MetaType type, int actualLength, int offset, byte normalizationVersion, TdsParserStateObject stateObj) {
Debug.Assert(((type.NullableType == TdsEnums.SQLXMLTYPE) ||
(value is INullable && !((INullable)value).IsNull)),
"unexpected null SqlType!");
if (normalizationVersion != 0x01) {
throw SQL.UnsupportedNormalizationVersion(normalizationVersion);
}
// parameters are always sent over as BIG or N types
switch (type.NullableType) {
case TdsEnums.SQLFLTN:
if (type.FixedLength == 4)
return SerializeFloat(((SqlSingle)value).Value);
else {
Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!");
return SerializeDouble(((SqlDouble)value).Value);
}
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE: {
byte[] b = new byte[actualLength];
if (value is SqlBinary) {
Buffer.BlockCopy(((SqlBinary)value).Value, offset, b, 0, actualLength);
}
else {
Debug.Assert(value is SqlBytes);
Buffer.BlockCopy(((SqlBytes)value).Value, offset, b, 0, actualLength);
}
return b;
}
case TdsEnums.SQLUNIQUEID: {
byte[] b = ((SqlGuid)value).ToByteArray();
Debug.Assert((actualLength == b.Length) && (actualLength == 16), "Invalid length for guid type in com+ object");
return b;
}
case TdsEnums.SQLBITN: {
Debug.Assert(type.FixedLength == 1, "Invalid length for SqlBoolean type");
// We normalize to allow conversion across data types. BIT is serialized into a BIGINT.
return SerializeLong(((SqlBoolean)value).Value == true ? 1 : 0, stateObj);
}
case TdsEnums.SQLINTN:
// We normalize to allow conversion across data types. All data types below are serialized into a BIGINT.
if (type.FixedLength == 1)
return SerializeLong(((SqlByte)value).Value, stateObj);
if (type.FixedLength == 2)
return SerializeLong(((SqlInt16)value).Value, stateObj);
if (type.FixedLength == 4)
return SerializeLong(((SqlInt32)value).Value, stateObj);
else {
Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture));
return SerializeLong(((SqlInt64)value).Value, stateObj);
}
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
if (value is SqlChars) {
String sch = new String(((SqlChars)value).Value);
return SerializeEncodingChar(sch, actualLength, offset, _defaultEncoding);
}
else {
Debug.Assert(value is SqlString);
return SerializeEncodingChar(((SqlString)value).Value, actualLength, offset, _defaultEncoding);
}
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
case TdsEnums.SQLXMLTYPE:
// convert to cchars instead of cbytes
// Xml type is already converted to string through GetCoercedValue
if (actualLength != 0)
actualLength >>= 1;
if (value is SqlChars) {
return SerializeCharArray(((SqlChars)value).Value, actualLength, offset);
}
else {
Debug.Assert(value is SqlString);
return SerializeString(((SqlString)value).Value, actualLength, offset);
}
case TdsEnums.SQLNUMERICN:
Debug.Assert(type.FixedLength <= 17, "Decimal length cannot be greater than 17 bytes");
return SerializeSqlDecimal((SqlDecimal)value, stateObj);
case TdsEnums.SQLDATETIMN:
SqlDateTime dt = (SqlDateTime)value;
if (type.FixedLength == 4) {
if (0 > dt.DayTicks || dt.DayTicks > UInt16.MaxValue)
throw SQL.SmallDateTimeOverflow(dt.ToString());
if (null == stateObj._bIntBytes) {
stateObj._bIntBytes = new byte[4];
}
byte[] b = stateObj._bIntBytes;
int current = 0;
byte[] bPart = SerializeShort(dt.DayTicks, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 2);
current += 2;
bPart = SerializeShort(dt.TimeTicks / SqlDateTime.SQLTicksPerMinute, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 2);
return b;
}
else {
if (null == stateObj._bLongBytes) {
stateObj._bLongBytes = new byte[8];
}
byte[] b = stateObj._bLongBytes;
int current = 0;
byte[] bPart = SerializeInt(dt.DayTicks, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 4);
current += 4;
bPart = SerializeInt(dt.TimeTicks, stateObj);
Buffer.BlockCopy(bPart, 0, b, current, 4);
return b;
}
case TdsEnums.SQLMONEYN: {
return SerializeSqlMoney((SqlMoney)value, type.FixedLength, stateObj);
}
default:
throw SQL.UnsupportedDatatypeEncryption(type.TypeName);
} // switch
}
//
// we always send over nullable types for parameters so we always write the varlen fields
//
internal void WriteParameterVarLen(MetaType type, int size, bool isNull, TdsParserStateObject stateObj, bool unknownLength=false) {
if (type.IsLong) { // text/image/SQLVariant have a 4 byte length, plp datatypes have 8 byte lengths
if (isNull) {
if (type.IsPlp) {
WriteLong(unchecked((long)TdsEnums.SQL_PLP_NULL), stateObj);
}
else {
WriteInt(unchecked((int)TdsEnums.VARLONGNULL), stateObj);
}
}
else if (type.NullableType == TdsEnums.SQLXMLTYPE || unknownLength) {
WriteUnsignedLong(TdsEnums.SQL_PLP_UNKNOWNLEN, stateObj);
}
else if (type.IsPlp) {
// Non-xml plp types
WriteLong((long)size, stateObj);
}
else {
WriteInt(size, stateObj);
}
}
else if (type.IsVarTime) {
if (isNull) {
stateObj.WriteByte(TdsEnums.FIXEDNULL);
}
else {
stateObj.WriteByte((byte)size);
}
}
else if (false == type.IsFixed) { // non-long but variable length column, must be a BIG* type: 2 byte length
if (isNull) {
WriteShort(TdsEnums.VARNULL, stateObj);
}
else {
WriteShort(size, stateObj);
}
}
else {
if (isNull) {
stateObj.WriteByte(TdsEnums.FIXEDNULL);
}
else {
Debug.Assert(type.FixedLength <= 0xff, "WriteParameterVarLen: invalid one byte length!");
stateObj.WriteByte((byte)(type.FixedLength & 0xff)); // 1 byte for everything else
}
}
}
// Reads the next chunk in a nvarchar(max) data stream.
// This call must be preceeded by a call to ReadPlpLength or ReadDataLength.
// Will not start reading into the next chunk if bytes requested is larger than
// the current chunk length. Do another ReadPlpLength, ReadPlpUnicodeChars in that case.
// Returns the actual chars read
private bool TryReadPlpUnicodeCharsChunk(char[] buff, int offst, int len, TdsParserStateObject stateObj, out int charsRead) {
Debug.Assert((buff == null && len == 0) || (buff.Length >= offst + len), "Invalid length sent to ReadPlpUnicodeChars()!");
Debug.Assert((stateObj._longlen != 0) && (stateObj._longlen != TdsEnums.SQL_PLP_NULL),
"Out of sync plp read request");
if (stateObj._longlenleft == 0) {
Debug.Assert(false, "Out of sync read request");
charsRead = 0;
return true;
}
charsRead = len;
// stateObj._longlenleft is in bytes
if ((stateObj._longlenleft >> 1) < (ulong)len)
charsRead = (int)(stateObj._longlenleft >> 1);
for (int ii = 0; ii < charsRead; ii++) {
if (!stateObj.TryReadChar(out buff[offst + ii])) {
return false;
}
}
stateObj._longlenleft -= ((ulong)charsRead << 1);
return true;
}
internal int ReadPlpUnicodeChars(ref char[] buff, int offst, int len, TdsParserStateObject stateObj) {
int charsRead;
Debug.Assert(stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
bool result = TryReadPlpUnicodeChars(ref buff, offst, len, stateObj, out charsRead);
if (!result) { throw SQL.SynchronousCallMayNotPend(); }
return charsRead;
}
// Reads the requested number of chars from a plp data stream, or the entire data if
// requested length is -1 or larger than the actual length of data. First call to this method
// should be preceeded by a call to ReadPlpLength or ReadDataLength.
// Returns the actual chars read.
internal bool TryReadPlpUnicodeChars(ref char[] buff, int offst, int len, TdsParserStateObject stateObj, out int totalCharsRead) {
int charsRead = 0;
int charsLeft = 0;
char[] newbuf;
if (stateObj._longlen == 0) {
Debug.Assert(stateObj._longlenleft == 0);
totalCharsRead = 0;
return true; // No data
}
Debug.Assert(((ulong)stateObj._longlen != TdsEnums.SQL_PLP_NULL),
"Out of sync plp read request");
Debug.Assert((buff == null && offst == 0) || (buff.Length >= offst + len), "Invalid length sent to ReadPlpUnicodeChars()!");
charsLeft = len;
// If total length is known up front, allocate the whole buffer in one shot instead of realloc'ing and copying over each time
if (buff == null && stateObj._longlen != TdsEnums.SQL_PLP_UNKNOWNLEN) {
buff = new char[(int)Math.Min((int)stateObj._longlen, len)];
}
if (stateObj._longlenleft == 0) {
ulong ignored;
if (!stateObj.TryReadPlpLength(false, out ignored)) {
totalCharsRead = 0;
return false;
}
if (stateObj._longlenleft == 0) { // Data read complete
totalCharsRead = 0;
return true;
}
}
totalCharsRead = 0;
while (charsLeft > 0) {
charsRead = (int)Math.Min((stateObj._longlenleft + 1) >> 1, (ulong)charsLeft);
if ((buff == null) || (buff.Length < (offst + charsRead))) {
// Grow the array
newbuf = new char[offst + charsRead];
if (buff != null) {
Buffer.BlockCopy(buff, 0, newbuf, 0, offst*2);
}
buff = newbuf;
}
if (charsRead > 0) {
if (!TryReadPlpUnicodeCharsChunk(buff, offst, charsRead, stateObj, out charsRead)) {
return false;
}
charsLeft -= charsRead;
offst += charsRead;
totalCharsRead += charsRead;
}
// Special case single byte left
if (stateObj._longlenleft == 1 && (charsLeft > 0)) {
byte b1;
if (!stateObj.TryReadByte(out b1)) {
return false;
}
stateObj._longlenleft--;
ulong ignored;
if (!stateObj.TryReadPlpLength(false, out ignored)) {
return false;
}
Debug.Assert((stateObj._longlenleft != 0), "ReadPlpUnicodeChars: Odd byte left at the end!");
byte b2;
if (!stateObj.TryReadByte(out b2)) {
return false;
}
stateObj._longlenleft--;
// Put it at the end of the array. At this point we know we have an extra byte.
buff[offst] = (char)(((b2 & 0xff) << 8) + (b1 & 0xff));
offst = checked((int)offst + 1);
charsRead++;
charsLeft--;
totalCharsRead++;
}
if (stateObj._longlenleft == 0) { // Read the next chunk or cleanup state if hit the end
ulong ignored;
if (!stateObj.TryReadPlpLength(false, out ignored)) {
return false;
}
}
if (stateObj._longlenleft == 0) // Data read complete
break;
}
return true;
}
internal int ReadPlpAnsiChars(ref char[] buff, int offst, int len, SqlMetaDataPriv metadata, TdsParserStateObject stateObj) {
int charsRead = 0;
int charsLeft = 0;
int bytesRead = 0;
int totalcharsRead = 0;
if (stateObj._longlen == 0) {
Debug.Assert(stateObj._longlenleft == 0);
return 0; // No data
}
Debug.Assert(((ulong)stateObj._longlen != TdsEnums.SQL_PLP_NULL),
"Out of sync plp read request");
Debug.Assert((buff == null && offst == 0) || (buff.Length >= offst + len), "Invalid length sent to ReadPlpAnsiChars()!");
charsLeft = len;
if (stateObj._longlenleft == 0) {
stateObj.ReadPlpLength(false);
if (stateObj._longlenleft == 0) {// Data read complete
stateObj._plpdecoder = null;
return 0;
}
}
if (stateObj._plpdecoder == null) {
Encoding enc = metadata.encoding;
if (enc == null)
{
if (null == _defaultEncoding) {
ThrowUnsupportedCollationEncountered(stateObj);
}
enc = _defaultEncoding;
}
stateObj._plpdecoder = enc.GetDecoder();
}
while (charsLeft > 0) {
bytesRead = (int)Math.Min(stateObj._longlenleft, (ulong)charsLeft);
if ((stateObj._bTmp == null) || (stateObj._bTmp.Length < bytesRead)) {
// Grow the array
stateObj._bTmp = new byte[bytesRead];
}
bytesRead = stateObj.ReadPlpBytesChunk(stateObj._bTmp, 0, bytesRead);
charsRead = stateObj._plpdecoder.GetChars(stateObj._bTmp, 0, bytesRead, buff, offst);
charsLeft -= charsRead;
offst += charsRead;
totalcharsRead += charsRead;
if (stateObj._longlenleft == 0) // Read the next chunk or cleanup state if hit the end
stateObj.ReadPlpLength(false);
if (stateObj._longlenleft == 0) { // Data read complete
stateObj._plpdecoder = null;
break;
}
}
return (totalcharsRead);
}
// ensure value is not null and does not have an NBC bit set for it before using this method
internal ulong SkipPlpValue(ulong cb, TdsParserStateObject stateObj) {
ulong skipped;
Debug.Assert(stateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
bool result = TrySkipPlpValue(cb, stateObj, out skipped);
if (!result) { throw SQL.SynchronousCallMayNotPend(); }
return skipped;
}
internal bool TrySkipPlpValue(ulong cb, TdsParserStateObject stateObj, out ulong totalBytesSkipped) {
// Read and skip cb bytes or until ReadPlpLength returns 0.
int bytesSkipped;
totalBytesSkipped = 0;
if (stateObj._longlenleft == 0) {
ulong ignored;
if (!stateObj.TryReadPlpLength(false, out ignored)) {
return false;
}
}
while ((totalBytesSkipped < cb) &&
(stateObj._longlenleft > 0)) {
if (stateObj._longlenleft > Int32.MaxValue)
bytesSkipped = Int32.MaxValue;
else
bytesSkipped = (int)stateObj._longlenleft;
bytesSkipped = ((cb - totalBytesSkipped) < (ulong) bytesSkipped) ? (int)(cb - totalBytesSkipped) : bytesSkipped;
if (!stateObj.TrySkipBytes(bytesSkipped)) {
return false;
}
stateObj._longlenleft -= (ulong)bytesSkipped;
totalBytesSkipped += (ulong)bytesSkipped;
if (stateObj._longlenleft == 0) {
ulong ignored;
if (!stateObj.TryReadPlpLength(false, out ignored)) {
return false;
}
}
}
return true;
}
internal ulong PlpBytesLeft(TdsParserStateObject stateObj) {
if ((stateObj._longlen != 0) && (stateObj._longlenleft == 0))
stateObj.ReadPlpLength(false);
return stateObj._longlenleft;
}
internal bool TryPlpBytesLeft(TdsParserStateObject stateObj, out ulong left) {
if ((stateObj._longlen != 0) && (stateObj._longlenleft == 0)) {
if (!stateObj.TryReadPlpLength(false, out left)) {
return false;
}
}
left = stateObj._longlenleft;
return true;
}
private const ulong _indeterminateSize = 0xffffffffffffffff; // Represents unknown size
internal ulong PlpBytesTotalLength(TdsParserStateObject stateObj) {
if (stateObj._longlen == TdsEnums.SQL_PLP_UNKNOWNLEN)
return _indeterminateSize;
else if (stateObj._longlen == TdsEnums.SQL_PLP_NULL)
return 0;
return stateObj._longlen;
}
const string StateTraceFormatString = "\n\t"
+ " _physicalStateObj = {0}\n\t"
+ " _pMarsPhysicalConObj = {1}\n\t"
+ " _state = {2}\n\t"
+ " _server = {3}\n\t"
+ " _fResetConnection = {4}\n\t"
+ " _defaultCollation = {5}\n\t"
+ " _defaultCodePage = {6}\n\t"
+ " _defaultLCID = {7}\n\t"
+ " _defaultEncoding = {8}\n\t"
+ " _encryptionOption = {10}\n\t"
+ " _currentTransaction = {11}\n\t"
+ " _pendingTransaction = {12}\n\t"
+ " _retainedTransactionId = {13}\n\t"
+ " _nonTransactedOpenResultCount = {14}\n\t"
+ " _connHandler = {15}\n\t"
+ " _fMARS = {16}\n\t"
+ " _sessionPool = {17}\n\t"
+ " _isShiloh = {18}\n\t"
+ " _isShilohSP1 = {19}\n\t"
+ " _isYukon = {20}\n\t"
+ " _sniSpnBuffer = {21}\n\t"
+ " _errors = {22}\n\t"
+ " _warnings = {23}\n\t"
+ " _attentionErrors = {24}\n\t"
+ " _attentionWarnings = {25}\n\t"
+ " _statistics = {26}\n\t"
+ " _statisticsIsInTransaction = {27}\n\t"
+ " _fPreserveTransaction = {28}"
+ " _fParallel = {29}"
;
internal string TraceString() {
return String.Format(/*IFormatProvider*/ null,
StateTraceFormatString,
null == _physicalStateObj,
null == _pMarsPhysicalConObj,
_state,
_server,
_fResetConnection,
null == _defaultCollation ? "(null)" : _defaultCollation.TraceString(),
_defaultCodePage,
_defaultLCID,
TraceObjectClass(_defaultEncoding),
"",
_encryptionOption,
null == _currentTransaction ? "(null)" : _currentTransaction.TraceString(),
null == _pendingTransaction ? "(null)" : _pendingTransaction.TraceString(),
_retainedTransactionId,
_nonTransactedOpenResultCount,
null == _connHandler ? "(null)" : _connHandler.ObjectID.ToString((IFormatProvider)null),
_fMARS,
null == _sessionPool ? "(null)" : _sessionPool.TraceString(),
_isShiloh,
_isShilohSP1,
_isYukon,
null == _sniSpnBuffer ? "(null)" : _sniSpnBuffer.Length.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.ErrorCount.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.WarningCount.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionErrorCount.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionWarningCount.ToString((IFormatProvider)null),
null == _statistics,
_statisticsIsInTransaction,
_fPreserveTransaction,
null == _connHandler ? "(null)" : _connHandler.ConnectionOptions.MultiSubnetFailover.ToString((IFormatProvider)null),
null == _connHandler ? "(null)" : _connHandler.ConnectionOptions.TransparentNetworkIPResolution.ToString((IFormatProvider)null));
}
private string TraceObjectClass(object instance) {
if (null == instance) {
return "(null)";
}
else {
return instance.GetType().ToString();
}
}
} // tdsparser
}//namespace
|