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
|
/*
* Rtl string functions
*
* Copyright (C) 1996-1998 Marcus Meissner
* Copyright (C) 2000 Alexandre Julliard
* Copyright (C) 2003 Thomas Mertes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "winsock2.h"
#include "windef.h"
#include "winnt.h"
#include "winternl.h"
#include "ddk/ntddk.h"
#include "in6addr.h"
#include "wine/debug.h"
#include "ntdll_misc.h"
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
#define GUID_STRING_LENGTH 38
/**************************************************************************
* RtlInitAnsiString (NTDLL.@)
*
* Initializes a buffered ansi string.
*
* RETURNS
* Nothing.
*
* NOTES
* Assigns source to target->Buffer. The length of source is assigned to
* target->Length and target->MaximumLength. If source is NULL the length
* of source is assumed to be 0.
*/
void WINAPI RtlInitAnsiString(
PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
PCSZ source) /* [I] '\0' terminated string used to initialize target */
{
if ((target->Buffer = (PCHAR) source))
{
target->Length = strlen(source);
target->MaximumLength = target->Length + 1;
}
else target->Length = target->MaximumLength = 0;
}
/**************************************************************************
* RtlInitAnsiStringEx (NTDLL.@)
*
* Initializes a buffered ansi string.
*
* RETURNS
* An appropriate NTSTATUS value.
*
* NOTES
* Assigns source to target->Buffer. The length of source is assigned to
* target->Length and target->MaximumLength. If source is NULL the length
* of source is assumed to be 0.
*/
NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
{
if (source)
{
unsigned int len = strlen(source);
if (len+1 > 0xffff)
return STATUS_NAME_TOO_LONG;
target->Buffer = (PCHAR) source;
target->Length = len;
target->MaximumLength = len + 1;
}
else
{
target->Buffer = NULL;
target->Length = 0;
target->MaximumLength = 0;
}
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlInitString (NTDLL.@)
*
* Initializes a buffered string.
*
* RETURNS
* Nothing.
*
* NOTES
* Assigns source to target->Buffer. The length of source is assigned to
* target->Length and target->MaximumLength. If source is NULL the length
* of source is assumed to be 0.
*/
void WINAPI RtlInitString(
PSTRING target, /* [I/O] Buffered string to be initialized */
PCSZ source) /* [I] '\0' terminated string used to initialize target */
{
RtlInitAnsiString( target, source );
}
/**************************************************************************
* RtlFreeAnsiString (NTDLL.@)
*/
void WINAPI RtlFreeAnsiString( PSTRING str )
{
if (str->Buffer)
{
RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
RtlZeroMemory( str, sizeof(*str) );
}
}
/**************************************************************************
* RtlFreeOemString (NTDLL.@)
*/
void WINAPI RtlFreeOemString( PSTRING str )
{
RtlFreeAnsiString( str );
}
/**************************************************************************
* RtlCopyString (NTDLL.@)
*/
void WINAPI RtlCopyString( STRING *dst, const STRING *src )
{
if (src)
{
unsigned int len = min( src->Length, dst->MaximumLength );
memcpy( dst->Buffer, src->Buffer, len );
dst->Length = len;
}
else dst->Length = 0;
}
/**************************************************************************
* RtlInitUnicodeString (NTDLL.@)
*
* Initializes a buffered unicode string.
*
* RETURNS
* Nothing.
*
* NOTES
* Assigns source to target->Buffer. The length of source is assigned to
* target->Length and target->MaximumLength. If source is NULL the length
* of source is assumed to be 0.
*/
void WINAPI RtlInitUnicodeString(
PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
{
if ((target->Buffer = (PWSTR) source))
{
unsigned int length = wcslen(source) * sizeof(WCHAR);
if (length > 0xfffc)
length = 0xfffc;
target->Length = length;
target->MaximumLength = target->Length + sizeof(WCHAR);
}
else target->Length = target->MaximumLength = 0;
}
/**************************************************************************
* RtlInitUnicodeStringEx (NTDLL.@)
*
* Initializes a buffered unicode string.
*
* RETURNS
* Success: STATUS_SUCCESS. target is initialized.
* Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
*
* NOTES
* Assigns source to target->Buffer. The length of source is assigned to
* target->Length and target->MaximumLength. If source is NULL the length
* of source is assumed to be 0.
*/
NTSTATUS WINAPI RtlInitUnicodeStringEx(
PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
{
if (source != NULL) {
unsigned int len = wcslen(source) * sizeof(WCHAR);
if (len > 0xFFFC) {
return STATUS_NAME_TOO_LONG;
} else {
target->Length = len;
target->MaximumLength = len + sizeof(WCHAR);
target->Buffer = (PWSTR) source;
} /* if */
} else {
target->Length = 0;
target->MaximumLength = 0;
target->Buffer = NULL;
} /* if */
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlCreateUnicodeString (NTDLL.@)
*
* Creates a UNICODE_STRING from a null-terminated Unicode string.
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
{
int len = (wcslen(src) + 1) * sizeof(WCHAR);
if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
memcpy( target->Buffer, src, len );
target->MaximumLength = len;
target->Length = len - sizeof(WCHAR);
return TRUE;
}
/**************************************************************************
* RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
*
* Creates a UNICODE_STRING from a null-terminated Ascii string.
*
* RETURNS
* Success: TRUE
* Failure: FALSE
*/
BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
{
STRING ansi;
RtlInitAnsiString( &ansi, src );
return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
}
/**************************************************************************
* RtlFreeUnicodeString (NTDLL.@)
*
* Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
* RtlCreateUnicodeStringFromAsciiz().
*
* RETURNS
* nothing
*/
void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
{
if (str->Buffer)
{
RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
RtlZeroMemory( str, sizeof(*str) );
}
}
/**************************************************************************
* RtlCopyUnicodeString (NTDLL.@)
*
* Copies from one UNICODE_STRING to another.
*
* RETURNS
* nothing
*/
void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
{
if (src)
{
unsigned int len = min( src->Length, dst->MaximumLength );
memcpy( dst->Buffer, src->Buffer, len );
dst->Length = len;
/* append terminating '\0' if enough space */
if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
}
else dst->Length = 0;
}
/**************************************************************************
* RtlDuplicateUnicodeString (NTDLL.@)
*
* Duplicates a unicode string.
*
* RETURNS
* Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
* Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
* STATUS_NO_MEMORY, if the allocation fails.
*
* NOTES
* For add_nul there are several possible values:
* 0 = destination will not be '\0' terminated,
* 1 = destination will be '\0' terminated,
* 3 = like 1 but for an empty source string produce '\0' terminated empty
* Buffer instead of assigning NULL to the Buffer.
* Other add_nul values are invalid.
*/
NTSTATUS WINAPI RtlDuplicateUnicodeString(
int add_nul, /* [I] flag */
const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
{
if (source == NULL || destination == NULL ||
source->Length > source->MaximumLength ||
(source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
add_nul == 2 || add_nul >= 4 || add_nul < 0) {
return STATUS_INVALID_PARAMETER;
} else {
if (source->Length == 0 && add_nul != 3) {
destination->Length = 0;
destination->MaximumLength = 0;
destination->Buffer = NULL;
} else {
unsigned int destination_max_len = source->Length;
if (add_nul) {
destination_max_len += sizeof(WCHAR);
} /* if */
destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
if (destination->Buffer == NULL) {
return STATUS_NO_MEMORY;
} else {
memcpy(destination->Buffer, source->Buffer, source->Length);
destination->Length = source->Length;
destination->MaximumLength = source->Length;
/* append terminating '\0' if enough space */
if (add_nul) {
destination->MaximumLength = destination_max_len;
destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
} /* if */
} /* if */
} /* if */
} /* if */
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlEraseUnicodeString (NTDLL.@)
*
* Overwrites a UNICODE_STRING with zeros.
*
* RETURNS
* nothing
*/
void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
{
if (str->Buffer)
{
memset( str->Buffer, 0, str->MaximumLength );
str->Length = 0;
}
}
/*
COMPARISON FUNCTIONS
*/
/******************************************************************************
* RtlCompareString (NTDLL.@)
*/
LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
{
unsigned int len;
LONG ret = 0;
LPCSTR p1, p2;
len = min(s1->Length, s2->Length);
p1 = s1->Buffer;
p2 = s2->Buffer;
if (CaseInsensitive)
{
while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
}
else
{
while (!ret && len--) ret = *p1++ - *p2++;
}
if (!ret) ret = s1->Length - s2->Length;
return ret;
}
/******************************************************************************
* RtlCompareUnicodeString (NTDLL.@)
*/
LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
BOOLEAN CaseInsensitive )
{
return RtlCompareUnicodeStrings( s1->Buffer, s1->Length / sizeof(WCHAR),
s2->Buffer, s2->Length / sizeof(WCHAR), CaseInsensitive );
}
/**************************************************************************
* RtlEqualString (NTDLL.@)
*
* Determine if two strings are equal.
*
* PARAMS
* s1 [I] Source string
* s2 [I] String to compare to s1
* CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
*
* RETURNS
* Non-zero if s1 is equal to s2, 0 otherwise.
*/
BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
{
if (s1->Length != s2->Length) return FALSE;
return !RtlCompareString( s1, s2, CaseInsensitive );
}
/**************************************************************************
* RtlEqualUnicodeString (NTDLL.@)
*
* Unicode version of RtlEqualString.
*/
BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
BOOLEAN CaseInsensitive )
{
if (s1->Length != s2->Length) return FALSE;
return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
}
/**************************************************************************
* RtlPrefixString (NTDLL.@)
*
* Determine if one string is a prefix of another.
*
* PARAMS
* s1 [I] Prefix to look for in s2
* s2 [I] String that may contain s1 as a prefix
* ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
*
* RETURNS
* TRUE if s2 contains s1 as a prefix, FALSE otherwise.
*/
BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
{
unsigned int i;
if (s1->Length > s2->Length) return FALSE;
if (ignore_case)
{
for (i = 0; i < s1->Length; i++)
if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
}
else
{
for (i = 0; i < s1->Length; i++)
if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
}
return TRUE;
}
/**************************************************************************
* RtlEqualComputerName (NTDLL.@)
*
* Determine if two computer names are the same.
*
* PARAMS
* left [I] First computer name
* right [I] Second computer name
*
* RETURNS
* 0 if the names are equal, non-zero otherwise.
*
* NOTES
* The comparison is case insensitive.
*/
NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
const UNICODE_STRING *right)
{
NTSTATUS ret;
STRING upLeft, upRight;
if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
{
if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
{
ret = RtlEqualString( &upLeft, &upRight, FALSE );
RtlFreeOemString( &upRight );
}
RtlFreeOemString( &upLeft );
}
return ret;
}
/**************************************************************************
* RtlEqualDomainName (NTDLL.@)
*
* Determine if two domain names are the same.
*
* PARAMS
* left [I] First domain name
* right [I] Second domain name
*
* RETURNS
* 0 if the names are equal, non-zero otherwise.
*
* NOTES
* The comparison is case insensitive.
*/
NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
const UNICODE_STRING *right)
{
return RtlEqualComputerName(left, right);
}
/*
COPY BETWEEN ANSI_STRING or UNICODE_STRING
there is no parameter checking, it just crashes
*/
/**************************************************************************
* RtlAnsiStringToUnicodeString (NTDLL.@)
*
* Converts an ansi string to a unicode string.
*
* RETURNS
* Success: STATUS_SUCCESS. uni contains the converted string
* Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
* STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
* STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
*
* NOTES
* This function always writes a terminating '\0'.
*/
NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
PCANSI_STRING ansi, /* [I] Ansi string to be converted */
BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
{
DWORD total = RtlAnsiStringToUnicodeSize( ansi );
if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
uni->Length = total - sizeof(WCHAR);
if (doalloc)
{
uni->MaximumLength = total;
if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
return STATUS_NO_MEMORY;
}
else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlOemStringToUnicodeString (NTDLL.@)
*
* Converts an oem string to a unicode string.
*
* RETURNS
* Success: STATUS_SUCCESS. uni contains the converted string
* Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
* STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
* STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
*
* NOTES
* This function always writes a terminating '\0'.
*/
NTSTATUS WINAPI RtlOemStringToUnicodeString(
UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
const STRING *oem, /* [I] Oem string to be converted */
BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
{
DWORD total = RtlOemStringToUnicodeSize( oem );
if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
uni->Length = total - sizeof(WCHAR);
if (doalloc)
{
uni->MaximumLength = total;
if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
return STATUS_NO_MEMORY;
}
else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlUnicodeStringToAnsiString (NTDLL.@)
*
* Converts a unicode string to an ansi string.
*
* RETURNS
* Success: STATUS_SUCCESS. ansi contains the converted string
* Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
* STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
*
* NOTES
* This function always writes a terminating '\0'.
* It performs a partial copy if ansi is too small.
*/
NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
STRING *ansi, /* [I/O] Destination for the ansi string */
const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
{
NTSTATUS ret = STATUS_SUCCESS;
DWORD len = RtlUnicodeStringToAnsiSize( uni );
ansi->Length = len - 1;
if (doalloc)
{
ansi->MaximumLength = len;
if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
return STATUS_NO_MEMORY;
}
else if (ansi->MaximumLength < len)
{
if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
ansi->Length = ansi->MaximumLength - 1;
ret = STATUS_BUFFER_OVERFLOW;
}
RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
ansi->Buffer[ansi->Length] = 0;
return ret;
}
/**************************************************************************
* RtlUnicodeStringToOemString (NTDLL.@)
*
* Converts a Rtl Unicode string to an OEM string.
*
* PARAMS
* oem [O] Destination for OEM string
* uni [I] Source Unicode string
* doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
*
* RETURNS
* Success: STATUS_SUCCESS. oem contains the converted string
* Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
* STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
*
* NOTES
* If doalloc is TRUE, the length allocated is uni->Length + 1.
* This function always '\0' terminates the string returned.
*/
NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
const UNICODE_STRING *uni,
BOOLEAN doalloc )
{
NTSTATUS ret = STATUS_SUCCESS;
DWORD len = RtlUnicodeStringToOemSize( uni );
oem->Length = len - 1;
if (doalloc)
{
oem->MaximumLength = len;
if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
return STATUS_NO_MEMORY;
}
else if (oem->MaximumLength < len)
{
if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
oem->Length = oem->MaximumLength - 1;
ret = STATUS_BUFFER_OVERFLOW;
}
RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
oem->Buffer[oem->Length] = 0;
return ret;
}
/*
CASE CONVERSIONS
*/
/**************************************************************************
* RtlUpperChar (NTDLL.@)
*
* Converts an Ascii character to uppercase.
*
* PARAMS
* ch [I] Character to convert
*
* RETURNS
* The uppercase character value.
*
* NOTES
* For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
* All other input characters are returned unchanged. The locale and
* multibyte characters are not taken into account (as native DLL).
*/
CHAR WINAPI RtlUpperChar( CHAR ch )
{
if (ch >= 'a' && ch <= 'z') {
return ch - 'a' + 'A';
} else {
return ch;
} /* if */
}
/**************************************************************************
* RtlUpperString (NTDLL.@)
*
* Converts an Ascii string to uppercase.
*
* PARAMS
* dst [O] Destination for converted string
* src [I] Source string to convert
*
* RETURNS
* Nothing.
*
* NOTES
* For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
* All other src characters are copied unchanged to dst. The locale and
* multibyte characters are not taken into account (as native DLL).
* The number of character copied is the minimum of src->Length and
* the dst->MaximumLength.
*/
void WINAPI RtlUpperString( STRING *dst, const STRING *src )
{
unsigned int i, len = min(src->Length, dst->MaximumLength);
for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
dst->Length = len;
}
/**************************************************************************
* RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
*
* Converts a Unicode string to the equivalent ANSI upper-case representation.
*
* RETURNS
* NTSTATUS code
*
* NOTES
* writes terminating 0
*/
NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *ansi,
const UNICODE_STRING *uni,
BOOLEAN doalloc )
{
NTSTATUS ret = STATUS_SUCCESS;
DWORD len = RtlUnicodeStringToAnsiSize( uni );
ansi->Length = len - 1;
if (doalloc)
{
ansi->MaximumLength = len;
if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
return STATUS_NO_MEMORY;
}
else if (ansi->MaximumLength < len)
{
if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
ansi->Length = ansi->MaximumLength - 1;
ret = STATUS_BUFFER_OVERFLOW;
}
RtlUpcaseUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
ansi->Buffer[ansi->Length] = 0;
return ret;
}
/**************************************************************************
* RtlUpcaseUnicodeStringToOemString (NTDLL.@)
*
* Converts a UNICODE_STRING to the equivalent OEM upper-case representation
* stored in STRING format.
*
* RETURNS
* NTSTATUS code
*
* NOTES
* writes terminating 0
*/
NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *oem,
const UNICODE_STRING *uni,
BOOLEAN doalloc )
{
NTSTATUS ret = STATUS_SUCCESS;
DWORD len = RtlUnicodeStringToAnsiSize( uni );
oem->Length = len - 1;
if (doalloc)
{
oem->MaximumLength = len;
if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
return STATUS_NO_MEMORY;
}
else if (oem->MaximumLength < len)
{
if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
oem->Length = oem->MaximumLength - 1;
ret = STATUS_BUFFER_OVERFLOW;
}
RtlUpcaseUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
oem->Buffer[oem->Length] = 0;
return ret;
}
/**************************************************************************
* RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
*
* Converts a UNICODE_STRING to the equivalent OEM upper-case representation
* stored in STRING format.
*
* RETURNS
* NTSTATUS code
*
* NOTES
* Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
*/
NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
const UNICODE_STRING *uni,
BOOLEAN doalloc )
{
NTSTATUS ret = STATUS_SUCCESS;
DWORD len = RtlUnicodeStringToOemSize( uni ) - 1;
oem->Length = len;
if (doalloc)
{
oem->MaximumLength = len;
if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return STATUS_NO_MEMORY;
}
else if (oem->MaximumLength < len)
{
ret = STATUS_BUFFER_OVERFLOW;
oem->Length = oem->MaximumLength;
if (!oem->MaximumLength) return ret;
}
RtlUpcaseUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
return ret;
}
/*
STRING SIZE
*/
/**************************************************************************
* RtlAnsiStringToUnicodeSize (NTDLL.@)
* RtlxAnsiStringToUnicodeSize (NTDLL.@)
*
* Calculate the size in bytes necessary for the Unicode conversion of str,
* including the terminating '\0'.
*
* PARAMS
* str [I] String to calculate the size of
*
* RETURNS
* The calculated size.
*/
DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
{
DWORD ret;
RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
return ret + sizeof(WCHAR);
}
/**************************************************************************
* RtlUnicodeStringToAnsiSize (NTDLL.@)
* RtlxUnicodeStringToAnsiSize (NTDLL.@)
*
* Calculate the size in bytes necessary for the Ansi conversion of str,
* including the terminating '\0'.
*
* PARAMS
* str [I] String to calculate the size of
*
* RETURNS
* The calculated size.
*/
DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
{
DWORD ret;
RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
return ret + 1;
}
/**************************************************************************
* RtlAppendAsciizToString (NTDLL.@)
*
* Concatenates a buffered character string and a '\0' terminated character
* string
*
* RETURNS
* Success: STATUS_SUCCESS. src is appended to dest.
* Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
* to hold the concatenated string.
*
* NOTES
* if src is NULL dest is unchanged.
* dest is never '\0' terminated.
*/
NTSTATUS WINAPI RtlAppendAsciizToString(
STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
{
if (src != NULL) {
unsigned int src_len = strlen(src);
unsigned int dest_len = src_len + dest->Length;
if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
memcpy(dest->Buffer + dest->Length, src, src_len);
dest->Length = dest_len;
} /* if */
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlAppendStringToString (NTDLL.@)
*
* Concatenates two buffered character strings
*
* RETURNS
* Success: STATUS_SUCCESS. src is appended to dest.
* Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
* to hold the concatenated string.
*
* NOTES
* if src->length is zero dest is unchanged.
* dest is never '\0' terminated.
*/
NTSTATUS WINAPI RtlAppendStringToString(
STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
const STRING *src) /* [I] Buffered character string to be concatenated */
{
if (src->Length != 0) {
unsigned int dest_len = src->Length + dest->Length;
if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
dest->Length = dest_len;
} /* if */
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlAppendUnicodeToString (NTDLL.@)
*
* Concatenates a buffered unicode string and a '\0' terminated unicode
* string
*
* RETURNS
* Success: STATUS_SUCCESS. src is appended to dest.
* Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
* to hold the concatenated string.
*
* NOTES
* if src is NULL dest is unchanged.
* dest is '\0' terminated when the MaximumLength allows it.
* When dest fits exactly in MaximumLength characters the '\0' is omitted.
*
* DIFFERENCES
* Does not write in the src->Buffer beyond MaximumLength when
* MaximumLength is odd as the native function does.
*/
NTSTATUS WINAPI RtlAppendUnicodeToString(
UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
{
if (src != NULL) {
unsigned int src_len = wcslen(src) * sizeof(WCHAR);
unsigned int dest_len = src_len + dest->Length;
if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
dest->Length = dest_len;
/* append terminating '\0' if enough space */
if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
} /* if */
} /* if */
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlAppendUnicodeStringToString (NTDLL.@)
*
* Concatenates two buffered unicode strings
*
* RETURNS
* Success: STATUS_SUCCESS. src is appended to dest.
* Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
* to hold the concatenated string.
*
* NOTES
* if src->length is zero dest is unchanged.
* dest is '\0' terminated when the MaximumLength allows it.
* When dest fits exactly in MaximumLength characters the '\0' is omitted.
*
* DIFFERENCES
* Does not write in the src->Buffer beyond MaximumLength when
* MaximumLength is odd as the native function does.
*/
NTSTATUS WINAPI RtlAppendUnicodeStringToString(
UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
{
if (src->Length != 0) {
unsigned int dest_len = src->Length + dest->Length;
if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
dest->Length = dest_len;
/* append terminating '\0' if enough space */
if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
} /* if */
} /* if */
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlFindCharInUnicodeString (NTDLL.@)
*
* Searches for one of several unicode characters in a unicode string.
*
* RETURNS
* Success: STATUS_SUCCESS. pos contains the position after the character found.
* Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
*/
NTSTATUS WINAPI RtlFindCharInUnicodeString(
int flags, /* [I] Flags */
const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
USHORT *pos) /* [O] Position of the first character found + 2 */
{
unsigned int main_idx, search_idx;
switch (flags) {
case 0:
for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
*pos = (main_idx + 1) * sizeof(WCHAR);
return STATUS_SUCCESS;
}
}
}
*pos = 0;
return STATUS_NOT_FOUND;
case 1:
main_idx = main_str->Length / sizeof(WCHAR);
while (main_idx-- > 0) {
for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
*pos = main_idx * sizeof(WCHAR);
return STATUS_SUCCESS;
}
}
}
*pos = 0;
return STATUS_NOT_FOUND;
case 2:
for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
search_idx = 0;
while (search_idx < search_chars->Length / sizeof(WCHAR) &&
main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
search_idx++;
}
if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
*pos = (main_idx + 1) * sizeof(WCHAR);
return STATUS_SUCCESS;
}
}
*pos = 0;
return STATUS_NOT_FOUND;
case 3:
main_idx = main_str->Length / sizeof(WCHAR);
while (main_idx-- > 0) {
search_idx = 0;
while (search_idx < search_chars->Length / sizeof(WCHAR) &&
main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
search_idx++;
}
if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
*pos = main_idx * sizeof(WCHAR);
return STATUS_SUCCESS;
}
}
*pos = 0;
return STATUS_NOT_FOUND;
} /* switch */
return STATUS_NOT_FOUND;
}
/*
MISC
*/
/**************************************************************************
* RtlIsTextUnicode (NTDLL.@)
*
* Attempt to guess whether a text buffer is Unicode.
*
* PARAMS
* buf [I] Text buffer to test
* len [I] Length of buf
* pf [O] Destination for test results
*
* RETURNS
* TRUE if the buffer is likely Unicode, FALSE otherwise.
*
* FIXME
* Should implement more tests.
*/
BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
{
const WCHAR *s = buf;
int i;
unsigned int flags = ~0U, out_flags = 0;
if (len < sizeof(WCHAR))
{
/* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
if (pf) *pf = 0;
return FALSE;
}
if (pf)
flags = *pf;
/*
* Apply various tests to the text string. According to the
* docs, each test "passed" sets the corresponding flag in
* the output flags. But some of the tests are mutually
* exclusive, so I don't see how you could pass all tests ...
*/
/* Check for an odd length ... pass if even. */
if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
if (((const char *)buf)[len - 1] == 0)
len--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
len /= sizeof(WCHAR);
/* Windows only checks the first 256 characters */
if (len > 256) len = 256;
/* Check for the special byte order unicode marks. */
if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
/* apply some statistical analysis */
if (flags & IS_TEXT_UNICODE_STATISTICS)
{
int stats = 0;
/* FIXME: checks only for ASCII characters in the unicode stream */
for (i = 0; i < len; i++)
{
if (s[i] <= 255) stats++;
}
if (stats > len / 2)
out_flags |= IS_TEXT_UNICODE_STATISTICS;
}
/* Check for unicode NULL chars */
if (flags & IS_TEXT_UNICODE_NULL_BYTES)
{
for (i = 0; i < len; i++)
{
if (!(s[i] & 0xff) || !(s[i] >> 8))
{
out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
break;
}
}
}
if (flags & IS_TEXT_UNICODE_CONTROLS)
{
for (i = 0; i < len; i++)
{
if (wcschr(L"\r\n\t \x3000", s[i]))
{
out_flags |= IS_TEXT_UNICODE_CONTROLS;
break;
}
}
}
if (flags & IS_TEXT_UNICODE_REVERSE_CONTROLS)
{
for (i = 0; i < len; i++)
{
if (wcschr(L"\x0d00\x0a00\x0900\x2000", s[i]))
{
out_flags |= IS_TEXT_UNICODE_REVERSE_CONTROLS;
break;
}
}
}
if (pf)
{
out_flags &= *pf;
*pf = out_flags;
}
/* check for flags that indicate it's definitely not valid Unicode */
if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
/* now check for invalid ASCII, and assume Unicode if so */
if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
/* now check for Unicode flags */
if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
/* no flags set */
return FALSE;
}
/**************************************************************************
* RtlCharToInteger (NTDLL.@)
*
* Converts a character string into its integer equivalent.
*
* RETURNS
* Success: STATUS_SUCCESS. value contains the converted number
* Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
* STATUS_ACCESS_VIOLATION, if value is NULL.
*
* NOTES
* For base 0 it uses 10 as base and the string should be in the format
* "{whitespace} [+|-] [0[x|o|b]] {digits}".
* For other bases the string should be in the format
* "{whitespace} [+|-] {digits}".
* No check is made for value overflow, only the lower 32 bits are assigned.
* If str is NULL it crashes, as the native function does.
*
* DIFFERENCES
* This function does not read garbage behind '\0' as the native version does.
*/
NTSTATUS WINAPI RtlCharToInteger(
PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
ULONG *value) /* [O] Destination for the converted value */
{
CHAR chCurrent;
int digit;
ULONG RunningTotal = 0;
BOOL bMinus = FALSE;
while (*str != '\0' && *str <= ' ') {
str++;
} /* while */
if (*str == '+') {
str++;
} else if (*str == '-') {
bMinus = TRUE;
str++;
} /* if */
if (base == 0) {
base = 10;
if (str[0] == '0') {
if (str[1] == 'b') {
str += 2;
base = 2;
} else if (str[1] == 'o') {
str += 2;
base = 8;
} else if (str[1] == 'x') {
str += 2;
base = 16;
} /* if */
} /* if */
} else if (base != 2 && base != 8 && base != 10 && base != 16) {
return STATUS_INVALID_PARAMETER;
} /* if */
if (value == NULL) {
return STATUS_ACCESS_VIOLATION;
} /* if */
while (*str != '\0') {
chCurrent = *str;
if (chCurrent >= '0' && chCurrent <= '9') {
digit = chCurrent - '0';
} else if (chCurrent >= 'A' && chCurrent <= 'Z') {
digit = chCurrent - 'A' + 10;
} else if (chCurrent >= 'a' && chCurrent <= 'z') {
digit = chCurrent - 'a' + 10;
} else {
digit = -1;
} /* if */
if (digit < 0 || digit >= base) {
*value = bMinus ? -RunningTotal : RunningTotal;
return STATUS_SUCCESS;
} /* if */
RunningTotal = RunningTotal * base + digit;
str++;
} /* while */
*value = bMinus ? -RunningTotal : RunningTotal;
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlIntegerToChar (NTDLL.@)
*
* Converts an unsigned integer to a character string.
*
* RETURNS
* Success: STATUS_SUCCESS. str contains the converted number
* Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
* STATUS_BUFFER_OVERFLOW, if str would be larger than length.
* STATUS_ACCESS_VIOLATION, if str is NULL.
*
* NOTES
* Instead of base 0 it uses 10 as base.
* Writes at most length characters to the string str.
* Str is '\0' terminated when length allows it.
* When str fits exactly in length characters the '\0' is omitted.
*/
NTSTATUS WINAPI RtlIntegerToChar(
ULONG value, /* [I] Value to be converted */
ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
ULONG length, /* [I] Length of the str buffer in bytes */
PCHAR str) /* [O] Destination for the converted value */
{
CHAR buffer[33];
PCHAR pos;
CHAR digit;
ULONG len;
if (base == 0) {
base = 10;
} else if (base != 2 && base != 8 && base != 10 && base != 16) {
return STATUS_INVALID_PARAMETER;
} /* if */
pos = &buffer[32];
*pos = '\0';
do {
pos--;
digit = value % base;
value = value / base;
if (digit < 10) {
*pos = '0' + digit;
} else {
*pos = 'A' + digit - 10;
} /* if */
} while (value != 0L);
len = &buffer[32] - pos;
if (len > length) {
return STATUS_BUFFER_OVERFLOW;
} else if (str == NULL) {
return STATUS_ACCESS_VIOLATION;
} else if (len == length) {
memcpy(str, pos, len);
} else {
memcpy(str, pos, len + 1);
} /* if */
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlUnicodeStringToInteger (NTDLL.@)
*
* Converts a unicode string into its integer equivalent.
*
* RETURNS
* Success: STATUS_SUCCESS. value contains the converted number
* Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
* STATUS_ACCESS_VIOLATION, if value is NULL.
*
* NOTES
* For base 0 it uses 10 as base and the string should be in the format
* "{whitespace} [+|-] [0[x|o|b]] {digits}".
* For other bases the string should be in the format
* "{whitespace} [+|-] {digits}".
* No check is made for value overflow, only the lower 32 bits are assigned.
* If str is NULL it crashes, as the native function does.
*
* DIFFERENCES
* This function does not read garbage on string length 0 as the native
* version does.
*/
NTSTATUS WINAPI RtlUnicodeStringToInteger(
const UNICODE_STRING *str, /* [I] Unicode string to be converted */
ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
ULONG *value) /* [O] Destination for the converted value */
{
LPWSTR lpwstr = str->Buffer;
USHORT CharsRemaining = str->Length / sizeof(WCHAR);
WCHAR wchCurrent;
int digit;
ULONG RunningTotal = 0;
BOOL bMinus = FALSE;
while (CharsRemaining >= 1 && *lpwstr <= ' ') {
lpwstr++;
CharsRemaining--;
} /* while */
if (CharsRemaining >= 1) {
if (*lpwstr == '+') {
lpwstr++;
CharsRemaining--;
} else if (*lpwstr == '-') {
bMinus = TRUE;
lpwstr++;
CharsRemaining--;
} /* if */
} /* if */
if (base == 0) {
base = 10;
if (CharsRemaining >= 2 && lpwstr[0] == '0') {
if (lpwstr[1] == 'b') {
lpwstr += 2;
CharsRemaining -= 2;
base = 2;
} else if (lpwstr[1] == 'o') {
lpwstr += 2;
CharsRemaining -= 2;
base = 8;
} else if (lpwstr[1] == 'x') {
lpwstr += 2;
CharsRemaining -= 2;
base = 16;
} /* if */
} /* if */
} else if (base != 2 && base != 8 && base != 10 && base != 16) {
return STATUS_INVALID_PARAMETER;
} /* if */
if (value == NULL) {
return STATUS_ACCESS_VIOLATION;
} /* if */
while (CharsRemaining >= 1) {
wchCurrent = *lpwstr;
if (wchCurrent >= '0' && wchCurrent <= '9') {
digit = wchCurrent - '0';
} else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
digit = wchCurrent - 'A' + 10;
} else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
digit = wchCurrent - 'a' + 10;
} else {
digit = -1;
} /* if */
if (digit < 0 || digit >= base) {
*value = bMinus ? -RunningTotal : RunningTotal;
return STATUS_SUCCESS;
} /* if */
RunningTotal = RunningTotal * base + digit;
lpwstr++;
CharsRemaining--;
} /* while */
*value = bMinus ? -RunningTotal : RunningTotal;
return STATUS_SUCCESS;
}
/**************************************************************************
* RtlIntegerToUnicodeString (NTDLL.@)
*
* Converts an unsigned integer to a '\0' terminated unicode string.
*
* RETURNS
* Success: STATUS_SUCCESS. str contains the converted number
* Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
* STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
* (with the '\0' termination). In this case str->Length
* is set to the length, the string would have (which can
* be larger than the MaximumLength).
*
* NOTES
* Instead of base 0 it uses 10 as base.
* If str is NULL it crashes, as the native function does.
*
* DIFFERENCES
* Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
* The native function does this when the string would be longer than 16
* characters even when the string parameter is long enough.
*/
NTSTATUS WINAPI RtlIntegerToUnicodeString(
ULONG value, /* [I] Value to be converted */
ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
UNICODE_STRING *str) /* [O] Destination for the converted value */
{
WCHAR buffer[33];
PWCHAR pos;
WCHAR digit;
if (base == 0) {
base = 10;
} else if (base != 2 && base != 8 && base != 10 && base != 16) {
return STATUS_INVALID_PARAMETER;
} /* if */
pos = &buffer[32];
*pos = '\0';
do {
pos--;
digit = value % base;
value = value / base;
if (digit < 10) {
*pos = '0' + digit;
} else {
*pos = 'A' + digit - 10;
} /* if */
} while (value != 0L);
str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
if (str->Length >= str->MaximumLength) {
return STATUS_BUFFER_OVERFLOW;
} else {
memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
} /* if */
return STATUS_SUCCESS;
}
/*************************************************************************
* RtlGUIDFromString (NTDLL.@)
*
* Convert a string representation of a GUID into a GUID.
*
* PARAMS
* str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
* guid [O] Destination for the converted GUID
*
* RETURNS
* Success: STATUS_SUCCESS. guid contains the converted value.
* Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
*
* SEE ALSO
* See RtlStringFromGUID.
*/
NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
{
int i = 0;
const WCHAR *lpszCLSID = str->Buffer;
BYTE* lpOut = (BYTE*)guid;
TRACE("(%s,%p)\n", debugstr_us(str), guid);
/* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
* to memory: DWORD... WORD WORD BYTES............
*/
while (i <= 37)
{
switch (i)
{
case 0:
if (*lpszCLSID != '{')
return STATUS_INVALID_PARAMETER;
break;
case 9: case 14: case 19: case 24:
if (*lpszCLSID != '-')
return STATUS_INVALID_PARAMETER;
break;
case 37:
if (*lpszCLSID != '}')
return STATUS_INVALID_PARAMETER;
break;
default:
{
WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
unsigned char byte;
/* Read two hex digits as a byte value */
if (ch >= '0' && ch <= '9') ch = ch - '0';
else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
else return STATUS_INVALID_PARAMETER;
if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
else return STATUS_INVALID_PARAMETER;
byte = ch << 4 | ch2;
switch (i)
{
#ifndef WORDS_BIGENDIAN
/* For Big Endian machines, we store the data such that the
* dword/word members can be read as DWORDS and WORDS correctly. */
/* Dword */
case 1: lpOut[3] = byte; break;
case 3: lpOut[2] = byte; break;
case 5: lpOut[1] = byte; break;
case 7: lpOut[0] = byte; lpOut += 4; break;
/* Word */
case 10: case 15: lpOut[1] = byte; break;
case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
#endif
/* Byte */
default: lpOut[0] = byte; lpOut++; break;
}
lpszCLSID++; /* Skip 2nd character of byte */
i++;
}
}
lpszCLSID++;
i++;
}
return STATUS_SUCCESS;
}
/*************************************************************************
* RtlStringFromGUID (NTDLL.@)
*
* Convert a GUID into a string representation of a GUID.
*
* PARAMS
* guid [I] GUID to convert
* str [O] Destination for the converted string
*
* RETURNS
* Success: STATUS_SUCCESS. str contains the converted value.
* Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
*
* SEE ALSO
* See RtlGUIDFromString.
*/
NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
{
TRACE("(%p,%p)\n", guid, str);
str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
str->MaximumLength = str->Length + sizeof(WCHAR);
str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
if (!str->Buffer)
{
str->Length = str->MaximumLength = 0;
return STATUS_NO_MEMORY;
}
swprintf(str->Buffer, str->MaximumLength/sizeof(WCHAR),
L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", guid->Data1, guid->Data2, guid->Data3,
guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
return STATUS_SUCCESS;
}
/***********************************************************************
* Message formatting
***********************************************************************/
struct format_message_args
{
int last; /* last used arg */
ULONG_PTR *array; /* args array */
va_list *list; /* args va_list */
UINT64 arglist[102]; /* arguments fetched from va_list */
};
static NTSTATUS add_chars( WCHAR **buffer, WCHAR *end, const WCHAR *str, ULONG len )
{
if (len > end - *buffer) return STATUS_BUFFER_OVERFLOW;
memcpy( *buffer, str, len * sizeof(WCHAR) );
*buffer += len;
return STATUS_SUCCESS;
}
static UINT64 get_arg( int nr, struct format_message_args *args_data, BOOL is64 )
{
if (nr == -1) nr = args_data->last + 1;
while (nr > args_data->last)
args_data->arglist[args_data->last++] = is64 ? va_arg( *args_data->list, UINT64 )
: va_arg( *args_data->list, ULONG_PTR );
return args_data->arglist[nr - 1];
}
static NTSTATUS add_format( WCHAR **buffer, WCHAR *end, const WCHAR **src, int insert, BOOLEAN ansi,
struct format_message_args *args_data )
{
const WCHAR *format = *src;
WCHAR *p, fmt[32];
ULONG_PTR args[5] = { 0 };
BOOL is_64 = FALSE;
UINT64 val;
int len, stars = 0, nb_args = 0;
p = fmt;
*p++ = '%';
if (*format++ == '!')
{
const WCHAR *end = wcschr( format, '!' );
if (!end || end - format > ARRAY_SIZE(fmt) - 2) return STATUS_INVALID_PARAMETER;
*src = end + 1;
while (wcschr( L"0123456789 +-*#.", *format ))
{
if (*format == '*') stars++;
*p++ = *format++;
}
if (stars > 2) return STATUS_INVALID_PARAMETER;
switch (*format)
{
case 'c': case 'C':
case 's': case 'S':
if (ansi) *p++ = *format++ ^ ('s' - 'S');
break;
case 'I':
if (sizeof(void *) == sizeof(int) && format[1] == '6' && format[2] == '4') is_64 = TRUE;
break;
}
while (format != end) *p++ = *format++;
}
else *p++ = ansi ? 'S' : 's'; /* simple string */
*p = 0;
if (args_data->list)
{
get_arg( insert - 1, args_data, is_64 ); /* make sure previous args have been fetched */
while (stars--)
{
args[nb_args++] = get_arg( insert, args_data, FALSE );
insert = -1;
}
/* replicate MS bug: drop an argument when using va_list with width/precision */
if (insert == -1) args_data->last--;
val = get_arg( insert, args_data, is_64 );
args[nb_args++] = val;
args[nb_args] = val >> 32;
}
else if (args_data->array)
{
args[nb_args++] = args_data->array[insert - 1];
if (args_data->last < insert) args_data->last = insert;
/* replicate MS bug: first arg is considered 64-bit, even if it's actually width or precision */
if (is_64) nb_args++;
while (stars--) args[nb_args++] = args_data->array[args_data->last++];
}
else return STATUS_INVALID_PARAMETER;
len = _snwprintf_s( *buffer, end - *buffer, end - *buffer - 1, fmt,
args[0], args[1], args[2], args[3], args[4] );
if (len == -1) return STATUS_BUFFER_OVERFLOW;
*buffer += len;
return STATUS_SUCCESS;
}
/**********************************************************************
* RtlFormatMessage (NTDLL.@)
*/
NTSTATUS WINAPI RtlFormatMessage( const WCHAR *src, ULONG width, BOOLEAN ignore_inserts,
BOOLEAN ansi, BOOLEAN is_array, va_list *args,
WCHAR *buffer, ULONG size, ULONG *retsize )
{
return RtlFormatMessageEx( src, width, ignore_inserts, ansi, is_array, args, buffer, size, retsize, 0 );
}
/**********************************************************************
* RtlFormatMessageEx (NTDLL.@)
*/
NTSTATUS WINAPI RtlFormatMessageEx( const WCHAR *src, ULONG width, BOOLEAN ignore_inserts,
BOOLEAN ansi, BOOLEAN is_array, va_list *args,
WCHAR *buffer, ULONG size, ULONG *retsize, ULONG flags )
{
static const WCHAR spaceW = ' ';
static const WCHAR crW = '\r';
static const WCHAR tabW = '\t';
static const WCHAR crlfW[] = {'\r','\n'};
struct format_message_args args_data;
NTSTATUS status = STATUS_SUCCESS;
WCHAR *start = buffer; /* start of buffer */
WCHAR *end = buffer + size / sizeof(WCHAR); /* end of buffer */
WCHAR *line = buffer; /* start of last line */
WCHAR *space = NULL; /* last space */
if (flags) FIXME( "%s unknown flags %lx\n", debugstr_w(src), flags );
args_data.last = 0;
args_data.array = is_array ? (ULONG_PTR *)args : NULL;
args_data.list = is_array ? NULL : args;
for ( ; *src; src++)
{
switch (*src)
{
case '\r':
if (src[1] == '\n') src++;
/* fall through */
case '\n':
if (!width)
{
status = add_chars( &buffer, end, crlfW, 2 );
line = buffer;
space = NULL;
break;
}
/* fall through */
case ' ':
space = buffer;
status = add_chars( &buffer, end, &spaceW, 1 );
break;
case '\t':
if (space == buffer - 1) space = buffer;
status = add_chars( &buffer, end, &tabW, 1 );
break;
case '%':
src++;
switch (*src)
{
case 0:
return STATUS_INVALID_PARAMETER;
case 't':
if (!width)
{
status = add_chars( &buffer, end, &tabW, 1 );
break;
}
/* fall through */
case 'n':
status = add_chars( &buffer, end, crlfW, 2 );
line = buffer;
space = NULL;
break;
case 'r':
status = add_chars( &buffer, end, &crW, 1 );
line = buffer;
space = NULL;
break;
case '0':
while (src[1]) src++;
break;
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
if (!ignore_inserts)
{
int nr = *src++ - '0';
if (*src >= '0' && *src <= '9') nr = nr * 10 + *src++ - '0';
status = add_format( &buffer, end, &src, nr, ansi, &args_data );
src--;
break;
}
/* fall through */
default:
if (ignore_inserts) status = add_chars( &buffer, end, src - 1, 2 );
else status = add_chars( &buffer, end, src, 1 );
break;
}
break;
default:
status = add_chars( &buffer, end, src, 1 );
break;
}
if (status) return status;
if (width && buffer - line >= width)
{
LONG_PTR diff = 2;
WCHAR *next;
if (space) /* split line at the last space */
{
next = space + 1;
while (space > line && (space[-1] == ' ' || space[-1] == '\t')) space--;
diff -= next - space;
}
else space = next = buffer; /* split at the end of the buffer */
if (diff > 0 && end - buffer < diff) return STATUS_BUFFER_OVERFLOW;
memmove( space + 2, next, (buffer - next) * sizeof(WCHAR) );
buffer += diff;
memcpy( space, crlfW, sizeof(crlfW) );
line = space + 2;
space = NULL;
}
}
if ((status = add_chars( &buffer, end, L"", 1 ))) return status;
*retsize = (buffer - start) * sizeof(WCHAR);
return STATUS_SUCCESS;
}
/***********************************************************************
* IP addresses
***********************************************************************/
#define htons(x) RtlUshortByteSwap(x)
#define ntohs(x) RtlUshortByteSwap(x)
static const signed char hex_table[] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2F */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5F */
-1, 10, 11, 12, 13, 14, 15 /* 0x60-0x66 */
};
static BOOL parse_ipv4_component(const WCHAR **str, BOOL strict, ULONG *value)
{
int base = 10, d;
WCHAR c;
ULONG cur_value, prev_value = 0;
BOOL success = FALSE;
if (**str == '.')
{
*str += 1;
return FALSE;
}
if ((*str)[0] == '0')
{
if ((*str)[1] == 'x' || (*str)[1] == 'X')
{
*str += 2;
if (strict) return FALSE;
base = 16;
}
else if ((*str)[1] >= '0' && (*str)[1] <= '9')
{
*str += 1;
if (strict) return FALSE;
base = 8;
}
}
for (cur_value = 0; **str; *str += 1)
{
c = **str;
if (c >= ARRAY_SIZE(hex_table)) break;
d = hex_table[c];
if (d == -1 || d >= base) break;
cur_value = cur_value * base + d;
success = TRUE;
if (cur_value < prev_value) return FALSE; /* overflow */
prev_value = cur_value;
}
if (success) *value = cur_value;
return success;
}
static NTSTATUS ipv4_string_to_address(const WCHAR *str, BOOL strict,
const WCHAR **terminator, IN_ADDR *address, USHORT *port)
{
ULONG fields[4];
int n = 0;
for (;;)
{
if (!parse_ipv4_component(&str, strict, &fields[n]))
goto error;
n++;
if (*str != '.')
break;
if (n == 4)
goto error;
str++;
}
if (strict && n < 4)
goto error;
switch (n)
{
case 4:
if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFF || fields[3] > 0xFF)
goto error;
address->S_un.S_un_b.s_b1 = fields[0];
address->S_un.S_un_b.s_b2 = fields[1];
address->S_un.S_un_b.s_b3 = fields[2];
address->S_un.S_un_b.s_b4 = fields[3];
break;
case 3:
if (fields[0] > 0xFF || fields[1] > 0xFF || fields[2] > 0xFFFF)
goto error;
address->S_un.S_un_b.s_b1 = fields[0];
address->S_un.S_un_b.s_b2 = fields[1];
address->S_un.S_un_b.s_b3 = (fields[2] & 0xFF00) >> 8;
address->S_un.S_un_b.s_b4 = (fields[2] & 0x00FF);
break;
case 2:
if (fields[0] > 0xFF || fields[1] > 0xFFFFFF)
goto error;
address->S_un.S_un_b.s_b1 = fields[0];
address->S_un.S_un_b.s_b2 = (fields[1] & 0xFF0000) >> 16;
address->S_un.S_un_b.s_b3 = (fields[1] & 0x00FF00) >> 8;
address->S_un.S_un_b.s_b4 = (fields[1] & 0x0000FF);
break;
case 1:
address->S_un.S_un_b.s_b1 = (fields[0] & 0xFF000000) >> 24;
address->S_un.S_un_b.s_b2 = (fields[0] & 0x00FF0000) >> 16;
address->S_un.S_un_b.s_b3 = (fields[0] & 0x0000FF00) >> 8;
address->S_un.S_un_b.s_b4 = (fields[0] & 0x000000FF);
break;
default:
goto error;
}
if (terminator) *terminator = str;
if (*str == ':')
{
str++;
if (!parse_ipv4_component(&str, FALSE, &fields[0]))
goto error;
if (!fields[0] || fields[0] > 0xFFFF || *str)
goto error;
if (port)
{
*port = htons(fields[0]);
if (terminator) *terminator = str;
}
}
if (!terminator && *str)
return STATUS_INVALID_PARAMETER;
return STATUS_SUCCESS;
error:
if (terminator) *terminator = str;
return STATUS_INVALID_PARAMETER;
}
/***********************************************************************
* RtlIpv4StringToAddressExW [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv4StringToAddressExW(const WCHAR *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
{
TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, address, port);
if (!str || !address || !port) return STATUS_INVALID_PARAMETER;
return ipv4_string_to_address(str, strict, NULL, address, port);
}
/***********************************************************************
* RtlIpv4StringToAddressW [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv4StringToAddressW(const WCHAR *str, BOOLEAN strict, const WCHAR **terminator, IN_ADDR *address)
{
TRACE("(%s, %u, %p, %p)\n", debugstr_w(str), strict, terminator, address);
return ipv4_string_to_address(str, strict, terminator, address, NULL);
}
/***********************************************************************
* RtlIpv4StringToAddressExA [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv4StringToAddressExA(const char *str, BOOLEAN strict, IN_ADDR *address, USHORT *port)
{
WCHAR wstr[32];
if (!str) return STATUS_INVALID_PARAMETER;
RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
wstr[ARRAY_SIZE(wstr) - 1] = 0;
return RtlIpv4StringToAddressExW(wstr, strict, address, port);
}
/***********************************************************************
* RtlIpv4StringToAddressA [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv4StringToAddressA(const char *str, BOOLEAN strict, const char **terminator, IN_ADDR *address)
{
WCHAR wstr[32];
const WCHAR *wterminator;
NTSTATUS ret;
RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
wstr[ARRAY_SIZE(wstr) - 1] = 0;
ret = RtlIpv4StringToAddressW(wstr, strict, &wterminator, address);
if (terminator) *terminator = str + (wterminator - wstr);
return ret;
}
static BOOL parse_ipv6_component(const WCHAR **str, int base, ULONG *value)
{
WCHAR *terminator;
if (**str >= ARRAY_SIZE(hex_table) || hex_table[**str] == -1) return FALSE;
*value = min(wcstoul(*str, &terminator, base), 0x7FFFFFFF);
if (*terminator == '0') terminator++; /* "0x" but nothing valid after */
else if (terminator == *str) return FALSE;
*str = terminator;
return TRUE;
}
static NTSTATUS ipv6_string_to_address(const WCHAR *str, BOOL ex,
const WCHAR **terminator, IN6_ADDR *address, ULONG *scope, USHORT *port)
{
BOOL expecting_port = FALSE, has_0x = FALSE, too_big = FALSE;
int n_bytes = 0, n_ipv4_bytes = 0, gap = -1;
ULONG ip_component, scope_component = 0, port_component = 0;
const WCHAR *prev_str;
if (str[0] == '[')
{
if (!ex) goto error;
expecting_port = TRUE;
str++;
}
if (str[0] == ':')
{
if (str[1] != ':') goto error;
str++;
address->u.Word[0] = 0;
}
for (;;)
{
if (!n_ipv4_bytes && *str == ':')
{
/* double colon */
if (gap != -1) goto error;
str++;
prev_str = str;
gap = n_bytes;
if (n_bytes == 14 || !parse_ipv6_component(&str, 16, &ip_component)) break;
str = prev_str;
}
else
{
prev_str = str;
}
if (!n_ipv4_bytes && n_bytes <= (gap != -1 ? 10 : 12))
{
if (parse_ipv6_component(&str, 10, &ip_component) && *str == '.')
n_ipv4_bytes = 1;
str = prev_str;
}
if (n_ipv4_bytes)
{
/* IPv4 component */
if (!parse_ipv6_component(&str, 10, &ip_component)) goto error;
if (str - prev_str > 3 || ip_component > 255)
{
too_big = TRUE;
}
else
{
if (*str != '.' && (n_ipv4_bytes < 4 || (n_bytes < 15 && gap == -1))) goto error;
address->u.Byte[n_bytes] = ip_component;
n_bytes++;
}
if (n_ipv4_bytes == 4 || *str != '.') break;
n_ipv4_bytes++;
}
else
{
/* IPv6 component */
if (!parse_ipv6_component(&str, 16, &ip_component)) goto error;
if (prev_str[0] == '0' && (prev_str[1] == 'x' || prev_str[1] == 'X'))
{
/* Windows "feature": the last IPv6 component can start with "0x" and be longer than 4 digits */
if (terminator) *terminator = prev_str + 1; /* Windows says that the "x" is the terminator */
if (n_bytes < 14 && gap == -1) return STATUS_INVALID_PARAMETER;
address->u.Word[n_bytes/2] = htons(ip_component);
n_bytes += 2;
has_0x = TRUE;
goto fill_gap;
}
if (*str != ':' && n_bytes < 14 && gap == -1) goto error;
if (str - prev_str > 4)
too_big = TRUE;
else
address->u.Word[n_bytes/2] = htons(ip_component);
n_bytes += 2;
if (*str != ':' || (gap != -1 && str[1] == ':')) break;
}
if (n_bytes == (gap != -1 ? 14 : 16)) break;
if (too_big) return STATUS_INVALID_PARAMETER;
str++;
}
if (terminator) *terminator = str;
if (too_big) return STATUS_INVALID_PARAMETER;
fill_gap:
if (gap == -1)
{
if (n_bytes < 16) goto error;
}
else
{
memmove(address->u.Byte + 16 - (n_bytes - gap), address->u.Byte + gap, n_bytes - gap);
memset(address->u.Byte + gap, 0, 16 - n_bytes);
}
if (ex)
{
if (has_0x) goto error;
if (*str == '%')
{
str++;
if (!parse_ipv4_component(&str, TRUE, &scope_component)) goto error;
}
if (expecting_port)
{
if (*str != ']') goto error;
str++;
if (*str == ':')
{
str++;
if (!parse_ipv4_component(&str, FALSE, &port_component)) goto error;
if (!port_component || port_component > 0xFFFF || *str) goto error;
port_component = htons(port_component);
}
}
}
if (!terminator && *str) return STATUS_INVALID_PARAMETER;
if (scope) *scope = scope_component;
if (port) *port = port_component;
return STATUS_SUCCESS;
error:
if (terminator) *terminator = str;
return STATUS_INVALID_PARAMETER;
}
/***********************************************************************
* RtlIpv6StringToAddressExW [NTDLL.@]
*/
NTSTATUS NTAPI RtlIpv6StringToAddressExW(const WCHAR *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
{
TRACE("(%s, %p, %p, %p)\n", debugstr_w(str), address, scope, port);
if (!str || !address || !scope || !port) return STATUS_INVALID_PARAMETER;
return ipv6_string_to_address(str, TRUE, NULL, address, scope, port);
}
/***********************************************************************
* RtlIpv6StringToAddressW [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv6StringToAddressW(const WCHAR *str, const WCHAR **terminator, IN6_ADDR *address)
{
TRACE("(%s, %p, %p)\n", debugstr_w(str), terminator, address);
return ipv6_string_to_address(str, FALSE, terminator, address, NULL, NULL);
}
/***********************************************************************
* RtlIpv6StringToAddressExA [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv6StringToAddressExA(const char *str, IN6_ADDR *address, ULONG *scope, USHORT *port)
{
WCHAR wstr[128];
if (!str) return STATUS_INVALID_PARAMETER;
RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
wstr[ARRAY_SIZE(wstr) - 1] = 0;
return RtlIpv6StringToAddressExW(wstr, address, scope, port);
}
/***********************************************************************
* RtlIpv6StringToAddressA [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv6StringToAddressA(const char *str, const char **terminator, IN6_ADDR *address)
{
WCHAR wstr[128];
const WCHAR *wterminator = NULL;
NTSTATUS ret;
RtlMultiByteToUnicodeN(wstr, sizeof(wstr), NULL, str, strlen(str) + 1);
wstr[ARRAY_SIZE(wstr) - 1] = 0;
ret = RtlIpv6StringToAddressW(wstr, &wterminator, address);
if (terminator && wterminator) *terminator = str + (wterminator - wstr);
return ret;
}
/***********************************************************************
* RtlIpv4AddressToStringExW [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv4AddressToStringExW(const IN_ADDR *pin, USHORT port, LPWSTR buffer, PULONG psize)
{
WCHAR tmp_ip[32];
ULONG needed;
if (!pin || !buffer || !psize)
return STATUS_INVALID_PARAMETER;
TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
needed = swprintf(tmp_ip, ARRAY_SIZE(tmp_ip), L"%u.%u.%u.%u",
pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
if (port) needed += swprintf(tmp_ip + needed, ARRAY_SIZE(tmp_ip) - needed, L":%u", ntohs(port));
if (*psize > needed) {
*psize = needed + 1;
wcscpy(buffer, tmp_ip);
return STATUS_SUCCESS;
}
*psize = needed + 1;
return STATUS_INVALID_PARAMETER;
}
/***********************************************************************
* RtlIpv4AddressToStringExA [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv4AddressToStringExA(const IN_ADDR *pin, USHORT port, LPSTR buffer, PULONG psize)
{
CHAR tmp_ip[32];
ULONG needed;
if (!pin || !buffer || !psize)
return STATUS_INVALID_PARAMETER;
TRACE("(%p:0x%lx, %d, %p, %p:%ld)\n", pin, pin->S_un.S_addr, port, buffer, psize, *psize);
needed = sprintf(tmp_ip, "%u.%u.%u.%u",
pin->S_un.S_un_b.s_b1, pin->S_un.S_un_b.s_b2,
pin->S_un.S_un_b.s_b3, pin->S_un.S_un_b.s_b4);
if (port) needed += sprintf(tmp_ip + needed, ":%u", ntohs(port));
if (*psize > needed) {
*psize = needed + 1;
strcpy(buffer, tmp_ip);
return STATUS_SUCCESS;
}
*psize = needed + 1;
return STATUS_INVALID_PARAMETER;
}
/***********************************************************************
* RtlIpv4AddressToStringW [NTDLL.@]
*/
WCHAR * WINAPI RtlIpv4AddressToStringW(const IN_ADDR *pin, LPWSTR buffer)
{
ULONG size = 16;
if (RtlIpv4AddressToStringExW(pin, 0, buffer, &size)) size = 0;
return buffer + size - 1;
}
/***********************************************************************
* RtlIpv4AddressToStringA [NTDLL.@]
*/
CHAR * WINAPI RtlIpv4AddressToStringA(const IN_ADDR *pin, LPSTR buffer)
{
ULONG size = 16;
if (RtlIpv4AddressToStringExA(pin, 0, buffer, &size)) size = 0;
return buffer + size - 1;
}
static BOOL is_ipv4_in_ipv6(const IN6_ADDR *address)
{
if (address->s6_words[5] == htons(0x5efe) && (address->s6_words[4] & ~htons(0x200)) == 0)
return TRUE;
if (*(UINT64 *)address != 0)
return FALSE;
if (address->s6_words[4] != 0 && address->s6_words[4] != 0xffff)
return FALSE;
if (address->s6_words[4] == 0 && address->s6_words[5] != 0 && address->s6_words[5] != 0xffff)
return FALSE;
if (address->s6_words[4] == 0xffff && address->s6_words[5] != 0)
return FALSE;
if (address->s6_words[6] == 0)
return FALSE;
return TRUE;
}
/***********************************************************************
* RtlIpv6AddressToStringExA [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv6AddressToStringExA(const IN6_ADDR *address, ULONG scope, USHORT port,
char *str, ULONG *size)
{
char buffer[64], *p = buffer;
int i, len, gap = -1, gap_len = 1, ipv6_end = 8;
ULONG needed;
NTSTATUS ret;
TRACE("(%p %lu %u %p %p)\n", address, scope, port, str, size);
if (!address || !str || !size)
return STATUS_INVALID_PARAMETER;
if (is_ipv4_in_ipv6(address))
ipv6_end = 6;
for (i = 0; i < ipv6_end; i++)
{
len = 0;
while (!address->s6_words[i] && i < ipv6_end)
{
i++;
len++;
}
if (len > gap_len)
{
gap = i - len;
gap_len = len;
}
}
if (port) p += sprintf(p, "[");
i = 0;
while (i < ipv6_end)
{
if (i == gap)
{
p += sprintf(p, ":");
i += gap_len;
if (i == ipv6_end) p += sprintf(p, ":");
continue;
}
if (i > 0) p += sprintf(p, ":");
p += sprintf(p, "%x", ntohs(address->s6_words[i]));
i++;
}
if (ipv6_end == 6)
{
if (p[-1] != ':') p += sprintf(p, ":");
p = RtlIpv4AddressToStringA((IN_ADDR *)(address->s6_words + 6), p);
}
if (scope) p += sprintf(p, "%%%lu", scope);
if (port) p += sprintf(p, "]:%u", ntohs(port));
needed = p - buffer + 1;
if (*size >= needed)
{
strcpy(str, buffer);
ret = STATUS_SUCCESS;
}
else
{
ret = STATUS_INVALID_PARAMETER;
}
*size = needed;
return ret;
}
/***********************************************************************
* RtlIpv6AddressToStringA [NTDLL.@]
*/
char * WINAPI RtlIpv6AddressToStringA(const IN6_ADDR *address, char *str)
{
ULONG size = 46;
if (!address || !str) return str - 1;
str[45] = 0; /* this byte is set even though the string is always shorter */
RtlIpv6AddressToStringExA(address, 0, 0, str, &size);
return str + size - 1;
}
/***********************************************************************
* RtlIpv6AddressToStringExW [NTDLL.@]
*/
NTSTATUS WINAPI RtlIpv6AddressToStringExW(const IN6_ADDR *address, ULONG scope, USHORT port,
WCHAR *str, ULONG *size)
{
char cstr[64];
NTSTATUS ret = RtlIpv6AddressToStringExA(address, scope, port, cstr, size);
if (ret == STATUS_SUCCESS) RtlMultiByteToUnicodeN(str, *size * sizeof(WCHAR), NULL, cstr, *size);
return ret;
}
/***********************************************************************
* RtlIpv6AddressToStringW [NTDLL.@]
*/
WCHAR * WINAPI RtlIpv6AddressToStringW(const IN6_ADDR *address, WCHAR *str)
{
ULONG size = 46;
if (!address || !str) return str;
str[45] = 0; /* this word is set even though the string is always shorter */
if (RtlIpv6AddressToStringExW(address, 0, 0, str, &size) != STATUS_SUCCESS)
return str;
return str + size - 1;
}
|