Index: include/ndk/rtlfuncs.h =================================================================== --- include/ndk/rtlfuncs.h (revision 56842) +++ include/ndk/rtlfuncs.h (working copy) @@ -3761,16 +3761,46 @@ NTSYSAPI NTSTATUS NTAPI +RtlIpv4StringToAddressA( + IN PCSTR String, + IN BOOLEAN Strict, + OUT PCSTR *Terminator, + OUT struct in_addr *Addr +); + +NTSYSAPI +NTSTATUS +NTAPI RtlIpv4StringToAddressW( IN PCWSTR String, IN BOOLEAN Strict, - OUT LPWSTR *Terminator, + OUT PCWSTR *Terminator, OUT struct in_addr *Addr ); NTSYSAPI NTSTATUS NTAPI +RtlIpv4StringToAddressExA( + IN PCSTR AddressString, + IN BOOLEAN Strict, + OUT struct in_addr *Address, + OUT PUSHORT Port +); + +NTSYSAPI +NTSTATUS +NTAPI +RtlIpv4StringToAddressExW( + IN PCWSTR AddressString, + IN BOOLEAN Strict, + OUT struct in_addr *Address, + OUT PUSHORT Port +); + +NTSYSAPI +NTSTATUS +NTAPI RtlIpv6StringToAddressA( IN PCHAR Name, OUT PCHAR *Terminator, Index: lib/rtl/network.c =================================================================== --- lib/rtl/network.c (revision 56842) +++ lib/rtl/network.c (working copy) @@ -21,8 +21,105 @@ /* network to host order conversion for little endian machines */ #define WN2H(w) (((w & 0xFF00) >> 8) | ((w & 0x00FF) << 8)) -/* FUNCTIONS ***************************************************************/ +/* PRIVATE FUNCTIONS **********************************************************/ +static +NTSTATUS +RtlpStringToUlong(IN PCWSTR String, + IN BOOLEAN Strict, + OUT PCWSTR *Terminator, + OUT PULONG Out) +{ + NTSTATUS Status = STATUS_INVALID_PARAMETER; + ULONG Result = 0; + INT Digit; + INT Base = 10; + BOOLEAN FirstDigit = TRUE; + + if (*String == L'0') + { + String++; + + /* Simply zero */ + if ((*String < L'0' || *String > L'9') && + (*String != L'x' && *String != L'X')) + { + Status = STATUS_SUCCESS; + goto Done; + } + + if (*String == L'x' || *String == L'X') + { + String++; + Base = 16; + } + else + Base = 8; + + /* Strict forbids anything but decimal */ + if (Strict) + { + Status = STATUS_INVALID_PARAMETER; + goto Done; + } + } + + while (1) + { + if (*String >= L'0' && *String <= L'7') + { + /* Octal digit */ + Digit = *String - L'0'; + } + else if (*String >= L'0' && *String <= L'9' && Base >= 10) + { + /* Decimal digit */ + Digit = *String - L'0'; + } + else if (*String >= L'A' && *String <= L'F' && Base >= 16) + { + /* Capital hex digit */ + Digit = 10 + (*String - L'A'); + } + else if (*String >= L'a' && *String <= L'f' && Base >= 16) + { + /* Lowercase hex digit */ + Digit = 10 + (*String - L'a'); + } + else + { + /* We're done. Fail if we haven't read at least one digit */ + Status = FirstDigit ? STATUS_INVALID_PARAMETER : STATUS_SUCCESS; + break; + } + + FirstDigit = FALSE; + + Status = RtlULongMult(Result, Base, &Result); + if (!NT_SUCCESS(Status)) + { + Status = STATUS_INVALID_PARAMETER; + break; + } + + Status = RtlULongAdd(Result, Digit, &Result); + if (!NT_SUCCESS(Status)) + { + Status = STATUS_INVALID_PARAMETER; + break; + } + + String++; + } + +Done: + *Terminator = String; + *Out = Result; + return Status; +} + +/* PUBLIC FUNCTIONS ***********************************************************/ + /* * @implemented */ @@ -131,45 +228,162 @@ } /* - * @unimplemented + * @implemented */ NTSTATUS NTAPI -RtlIpv4StringToAddressA(IN PCHAR String, +RtlIpv4StringToAddressA(IN PCSTR String, IN BOOLEAN Strict, - OUT PCHAR *Terminator, + OUT PCSTR *Terminator, OUT struct in_addr *Addr) { - UNIMPLEMENTED; - return STATUS_NOT_IMPLEMENTED; + NTSTATUS Status; + ANSI_STRING AddressA; + UNICODE_STRING AddressW; + PCWSTR TerminatorW = NULL; + + Status = RtlInitAnsiStringEx(&AddressA, String); + if (!NT_SUCCESS(Status)) return Status; + + Status = RtlAnsiStringToUnicodeString(&AddressW, &AddressA, TRUE); + if (!NT_SUCCESS(Status)) return Status; + + Status = RtlIpv4StringToAddressW(AddressW.Buffer, + Strict, + &TerminatorW, + Addr); + + ASSERT(TerminatorW >= AddressW.Buffer); + *Terminator = String + (TerminatorW - AddressW.Buffer); + + RtlFreeUnicodeString(&AddressW); + + return Status; } /* -* @unimplemented +* @implemented */ NTSTATUS NTAPI -RtlIpv4StringToAddressExA(IN PCHAR AddressString, +RtlIpv4StringToAddressExA(IN PCSTR AddressString, IN BOOLEAN Strict, OUT struct in_addr *Address, - IN PUSHORT Port) + OUT PUSHORT Port) { - UNIMPLEMENTED; - return STATUS_NOT_IMPLEMENTED; + NTSTATUS Status; + ANSI_STRING AddressA; + UNICODE_STRING AddressW; + + Status = RtlInitAnsiStringEx(&AddressA, AddressString); + if (!NT_SUCCESS(Status)) return Status; + + Status = RtlAnsiStringToUnicodeString(&AddressW, &AddressA, TRUE); + if (!NT_SUCCESS(Status)) return Status; + + Status = RtlIpv4StringToAddressExW(AddressW.Buffer, Strict, Address, Port); + + RtlFreeUnicodeString(&AddressW); + + return Status; } /* -* @unimplemented +* @implemented */ NTSTATUS NTAPI RtlIpv4StringToAddressW(IN PCWSTR String, IN BOOLEAN Strict, - OUT LPWSTR *Terminator, + OUT PCWSTR *Terminator, OUT struct in_addr *Addr) { - UNIMPLEMENTED; - return STATUS_NOT_IMPLEMENTED; + NTSTATUS Status; + ULONG Result[4]; + INT Parts = 0; + INT i; + + do + { + Status = RtlpStringToUlong(String, Strict, &String, &Result[Parts]); + Parts++; + + if (*String != L'.') break; + + /* Already four parts, but a dot follows? */ + if (Parts == 4) + { + Status = STATUS_INVALID_PARAMETER; + goto Done; + } + + /* Skip the dot */ + String++; + } while (NT_SUCCESS(Status)); + + if (Strict && Parts < 4) Status = STATUS_INVALID_PARAMETER; + + if (!NT_SUCCESS(Status)) goto Done; + + /* Verify all but the last part */ + for (i = 0; i < Parts - 1; i++) + if (Result[i] > 0xFF) + { + Status = STATUS_INVALID_PARAMETER; + goto Done; + } + + /* Now handle the last part, depending on how many parts there are */ + switch (Parts) + { + case 1: + Result[3] = Result[0] >> 0 & 0xFF; + Result[2] = Result[0] >> 8 & 0xFF; + Result[1] = Result[0] >> 16 & 0xFF; + Result[0] = Result[0] >> 24 & 0xFF; + break; + case 2: + if (Result[1] > 0xFFFFFF) + { + Status = STATUS_INVALID_PARAMETER; + goto Done; + } + + Result[3] = Result[1] >> 0 & 0xFF; + Result[2] = Result[1] >> 8 & 0xFF; + Result[1] = Result[1] >> 16 & 0xFF; + break; + case 3: + if (Result[2] > 0xFFFF) + { + Status = STATUS_INVALID_PARAMETER; + goto Done; + } + + Result[3] = Result[2] >> 0 & 0xFF; + Result[2] = Result[2] >> 8 & 0xFF; + break; + case 4: + if (Result[3] > 0xFF) + { + Status = STATUS_INVALID_PARAMETER; + goto Done; + } + break; + } + + /* Finally, assign the result */ + if (NT_SUCCESS(Status)) + { + Addr->S_un.S_un_b.s_b1 = (UCHAR)Result[0]; + Addr->S_un.S_un_b.s_b2 = (UCHAR)Result[1]; + Addr->S_un.S_un_b.s_b3 = (UCHAR)Result[2]; + Addr->S_un.S_un_b.s_b4 = (UCHAR)Result[3]; + } + +Done: + *Terminator = String; + return Status; } /* @@ -177,13 +391,24 @@ */ NTSTATUS NTAPI -RtlIpv4StringToAddressExW(IN PWCHAR AddressString, +RtlIpv4StringToAddressExW(IN PCWSTR AddressString, IN BOOLEAN Strict, OUT struct in_addr *Address, OUT PUSHORT Port) { - UNIMPLEMENTED; - return STATUS_NOT_IMPLEMENTED; + NTSTATUS Status; + PCWSTR Terminator; + + Status = RtlIpv4StringToAddressW(AddressString, + Strict, + &Terminator, + Address); + if (!NT_SUCCESS(Status)) + return Status; + + /* TODO: parse port */ + + return Status; } /* Index: lib/rtl/rtl.h =================================================================== --- lib/rtl/rtl.h (revision 56842) +++ lib/rtl/rtl.h (working copy) @@ -22,6 +22,7 @@ /* PSDK/NDK Headers */ #include +#include #include #include #include Index: modules/rostests/winetests/ntdll/rtl.c =================================================================== --- modules/rostests/winetests/ntdll/rtl.c (revision 56828) +++ modules/rostests/winetests/ntdll/rtl.c (working copy) @@ -88,6 +88,8 @@ static IMAGE_BASE_RELOCATION *(WINAPI *pLdrProcessRelocationBlock)(void*,UINT,USHORT*,INT_PTR); static CHAR * (WINAPI *pRtlIpv4AddressToStringA)(const IN_ADDR *, LPSTR); static NTSTATUS (WINAPI *pRtlIpv4AddressToStringExA)(const IN_ADDR *, USHORT, LPSTR, PULONG); +static NTSTATUS (WINAPI *pRtlIpv4StringToAddressA)(PCSTR, BOOLEAN, PCSTR *, IN_ADDR *); +static NTSTATUS (WINAPI *pRtlIpv4StringToAddressExA)(PCSTR, BOOLEAN, IN_ADDR *, PUSHORT); static HMODULE hkernel32 = 0; static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL); @@ -131,6 +133,8 @@ pLdrProcessRelocationBlock = (void *)GetProcAddress(hntdll, "LdrProcessRelocationBlock"); pRtlIpv4AddressToStringA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringA"); pRtlIpv4AddressToStringExA = (void *)GetProcAddress(hntdll, "RtlIpv4AddressToStringExA"); + pRtlIpv4StringToAddressA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressA"); + pRtlIpv4StringToAddressExA = (void *)GetProcAddress(hntdll, "RtlIpv4StringToAddressExA"); } hkernel32 = LoadLibraryA("kernel32.dll"); ok(hkernel32 != 0, "LoadLibrary failed\n"); @@ -1315,7 +1319,178 @@ res, size, buffer); } +static void test_RtlIpv4StringToAddress(void) +{ + NTSTATUS res; + IN_ADDR ip, expected_ip; + PCSTR terminator; + CHAR dummy; + struct + { + PCSTR address; + NTSTATUS res; + int terminator_offset; + int ip[4]; + BOOL strict_is_different; + NTSTATUS res_strict; + int terminator_offset_strict; + int ip_strict[4]; + } tests[] = + { + { "", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { " ", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { "1.1.1.1", STATUS_SUCCESS, 7, { 1, 1, 1, 1 } }, + { "0.0.0.0", STATUS_SUCCESS, 7, { 0, 0, 0, 0 } }, + { "255.255.255.255", STATUS_SUCCESS, 15, { 255, 255, 255, 255 } }, + { "255.255.255.255:123", + STATUS_SUCCESS, 15, { 255, 255, 255, 255 } }, + { "255.255.255.256", STATUS_INVALID_PARAMETER, 15, { -1 } }, + { "255.255.255.4294967295", + STATUS_INVALID_PARAMETER, 22, { -1 } }, + { "255.255.255.4294967296", + STATUS_INVALID_PARAMETER, 21, { -1 } }, + { "255.255.255.4294967297", + STATUS_INVALID_PARAMETER, 21, { -1 } }, + { "a", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { "1.1.1.0xaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 }, + TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "1.1.1.0XaA", STATUS_SUCCESS, 10, { 1, 1, 1, 170 }, + TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "1.1.1.0x", STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "1.1.1.0xff", STATUS_SUCCESS, 10, { 1, 1, 1, 255 }, + TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "1.1.1.0x100", STATUS_INVALID_PARAMETER, 11, { -1 }, + TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "1.1.1.0xffffffff",STATUS_INVALID_PARAMETER, 16, { -1 }, + TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "1.1.1.0x100000000", + STATUS_INVALID_PARAMETER, 16, { -1, 0, 0, 0 }, + TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "1.1.1.010", STATUS_SUCCESS, 9, { 1, 1, 1, 8 }, + TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "1.1.1.00", STATUS_SUCCESS, 8, { 1, 1, 1, 0 }, + TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "1.1.1.007", STATUS_SUCCESS, 9, { 1, 1, 1, 7 }, + TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "1.1.1.08", STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "1.1.1.008", STATUS_SUCCESS, 8, { 1, 1, 1, 0 }, + TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "1.1.1.0a", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } }, + { "1.1.1.0o10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } }, + { "1.1.1.0b10", STATUS_SUCCESS, 7, { 1, 1, 1, 0 } }, + { "1.1.1.-2", STATUS_INVALID_PARAMETER, 6, { -1 } }, + { "1", STATUS_SUCCESS, 1, { 0, 0, 0, 1 }, + TRUE, STATUS_INVALID_PARAMETER, 1, { -1 } }, + { "-1", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { "203569230", STATUS_SUCCESS, 9, { 12, 34, 56, 78 }, + TRUE, STATUS_INVALID_PARAMETER, 9, { -1 } }, + { "1.223756", STATUS_SUCCESS, 8, { 1, 3, 106, 12 }, + TRUE, STATUS_INVALID_PARAMETER, 8, { -1 } }, + { "3.4.756", STATUS_SUCCESS, 7, { 3, 4, 2, 244 }, + TRUE, STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "3.4.756.1", STATUS_INVALID_PARAMETER, 9, { -1 } }, + { "3.4.65536", STATUS_INVALID_PARAMETER, 9, { -1 } }, + { "3.4.5.6.7", STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "3.4.5.+6", STATUS_INVALID_PARAMETER, 6, { -1 } }, + { " 3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { "\t3.4.5.6", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { "3.4.5.6 ", STATUS_SUCCESS, 7, { 3, 4, 5, 6 } }, + { "3. 4.5.6", STATUS_INVALID_PARAMETER, 2, { -1 } }, + { ".", STATUS_INVALID_PARAMETER, 1, { -1 } }, + { "..", STATUS_INVALID_PARAMETER, 1, { -1 } }, + { "1.", STATUS_INVALID_PARAMETER, 2, { -1 } }, + { "1..", STATUS_INVALID_PARAMETER, 3, { -1 } }, + { ".1", STATUS_INVALID_PARAMETER, 1, { -1 } }, + { ".1.", STATUS_INVALID_PARAMETER, 1, { -1 } }, + { ".1.2.3", STATUS_INVALID_PARAMETER, 1, { -1 } }, + { "0.1.2.3", STATUS_SUCCESS, 7, { 0, 1, 2, 3 } }, + { "0.1.2.3.", STATUS_INVALID_PARAMETER, 7, { -1 } }, + { "[0.1.2.3]", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { "::1", STATUS_INVALID_PARAMETER, 0, { -1 } }, + { ":1", STATUS_INVALID_PARAMETER, 0, { -1 } }, + }; + const int testcount = sizeof(tests) / sizeof(tests[0]); + int i; + if (!pRtlIpv4StringToAddressA) + { + win_skip("RtlIpv4StringToAddress not available\n"); + return; + } + + if (0) + { + /* leaving either parameter NULL crashes on Windows */ + res = pRtlIpv4StringToAddressA(NULL, FALSE, &terminator, &ip); + res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, NULL, &ip); + res = pRtlIpv4StringToAddressA("1.1.1.1", FALSE, &terminator, NULL); + /* same for the wide char version */ + /* + res = pRtlIpv4StringToAddressW(NULL, FALSE, &terminatorW, &ip); + res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, NULL, &ip); + res = pRtlIpv4StringToAddressW(L"1.1.1.1", FALSE, &terminatorW, NULL); + */ + } + + for (i = 0; i < testcount; i++) + { + /* non-strict */ + terminator = &dummy; + ip.S_un.S_addr = 0xabababab; + res = pRtlIpv4StringToAddressA(tests[i].address, FALSE, &terminator, &ip); + ok(res == tests[i].res, + "[%s] res = 0x%08x, expected 0x%08x\n", + tests[i].address, res, tests[i].res); + ok(terminator == tests[i].address + tests[i].terminator_offset, + "[%s] terminator = %p, expected %p\n", + tests[i].address, terminator, tests[i].address + tests[i].terminator_offset); + if (tests[i].ip[0] == -1) + expected_ip.S_un.S_addr = 0xabababab; + else + { + expected_ip.S_un.S_un_b.s_b1 = tests[i].ip[0]; + expected_ip.S_un.S_un_b.s_b2 = tests[i].ip[1]; + expected_ip.S_un.S_un_b.s_b3 = tests[i].ip[2]; + expected_ip.S_un.S_un_b.s_b4 = tests[i].ip[3]; + } + ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, + "[%s] ip = %08x, expected %08x\n", + tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr); + + if (!tests[i].strict_is_different) + { + tests[i].res_strict = tests[i].res; + tests[i].terminator_offset_strict = tests[i].terminator_offset; + tests[i].ip_strict[0] = tests[i].ip[0]; + tests[i].ip_strict[1] = tests[i].ip[1]; + tests[i].ip_strict[2] = tests[i].ip[2]; + tests[i].ip_strict[3] = tests[i].ip[3]; + } + /* strict */ + terminator = &dummy; + ip.S_un.S_addr = 0xabababab; + res = pRtlIpv4StringToAddressA(tests[i].address, TRUE, &terminator, &ip); + ok(res == tests[i].res_strict, + "[%s] res = 0x%08x, expected 0x%08x\n", + tests[i].address, res, tests[i].res_strict); + ok(terminator == tests[i].address + tests[i].terminator_offset_strict, + "[%s] terminator = %p, expected %p\n", + tests[i].address, terminator, tests[i].address + tests[i].terminator_offset_strict); + if (tests[i].ip_strict[0] == -1) + expected_ip.S_un.S_addr = 0xabababab; + else + { + expected_ip.S_un.S_un_b.s_b1 = tests[i].ip_strict[0]; + expected_ip.S_un.S_un_b.s_b2 = tests[i].ip_strict[1]; + expected_ip.S_un.S_un_b.s_b3 = tests[i].ip_strict[2]; + expected_ip.S_un.S_un_b.s_b4 = tests[i].ip_strict[3]; + } + ok(ip.S_un.S_addr == expected_ip.S_un.S_addr, + "[%s] ip = %08x, expected %08x\n", + tests[i].address, ip.S_un.S_addr, expected_ip.S_un.S_addr); + } +} + START_TEST(rtl) { InitFunctionPtrs(); @@ -1339,4 +1514,5 @@ test_LdrProcessRelocationBlock(); test_RtlIpv4AddressToString(); test_RtlIpv4AddressToStringEx(); + test_RtlIpv4StringToAddress(); }