diff --git a/core/libs/prx/libc/src/StringExtensions.cpp b/core/libs/prx/libc/src/StringExtensions.cpp index a9a759763..c15d39ef7 100644 --- a/core/libs/prx/libc/src/StringExtensions.cpp +++ b/core/libs/prx/libc/src/StringExtensions.cpp @@ -1,5 +1,6 @@ #include "prx/libc/include/general/VabiMacros.hpp" #include +#include #include #include #include @@ -73,4 +74,55 @@ char* APS5_VABI strtok_nid_postfix(char* text, const char* delimiters) { return strtok_r_nid_postfix(text, delimiters, &state); } + +int APS5_VABI __inet_aton_nid_postfix(const char* text, void* address) { + std::uint8_t parts[3]; + std::size_t count = 0; + std::uint64_t value = 0; + bool digit = false; + char c = *text; + for (;;) { + if (c < '0' || c > '9') return 0; + value = 0; + unsigned base = 10; + digit = false; + if (c == '0') { + c = *++text; + if (c == 'x' || c == 'X') { + base = 16; + c = *++text; + } else { + base = 8; + digit = true; + } + } + for (;; c = *++text, digit = true) { + const char lower = static_cast(c | 0x20); + if (c >= '0' && c <= '9') { + if (base == 8 && c >= '8') return 0; + value = value * base + static_cast(c - '0'); + } else if (base == 16 && lower >= 'a' && lower <= 'f') { + value = (value << 4) | static_cast(lower - 'a' + 10); + } else { + break; + } + } + if (c != '.') break; + if (count == 3 || value > 0xff) return 0; + parts[count++] = static_cast(value); + c = *++text; + } + if (c != '\0' && c != ' ' && (c < '\t' || c > '\r')) return 0; + if (!digit) return 0; + constexpr std::uint64_t limits[] = {UINT64_MAX, 0xffffff, 0xffff, 0xff}; + if (value > limits[count]) return 0; + for (std::size_t index = 0; index < count; ++index) value |= std::uint64_t{parts[index]} << (24 - 8 * index); + if (address) { + const std::uint8_t bytes[4] = {static_cast(value >> 24), static_cast(value >> 16), + static_cast(value >> 8), static_cast(value)}; + std::memcpy(address, bytes, sizeof(bytes)); + } + return 1; +} + } diff --git a/core/libs/tests/GuestStrings.cpp b/core/libs/tests/GuestStrings.cpp index 019e4425c..a2f688d7d 100644 --- a/core/libs/tests/GuestStrings.cpp +++ b/core/libs/tests/GuestStrings.cpp @@ -25,6 +25,7 @@ int APS5_VABI memset_s_nid_postfix(void*, std::size_t, int, std::size_t); char* APS5_VABI strnstr_nid_postfix(const char*, const char*, std::size_t); int APS5_VABI snprintf_s_nid_postfix(char*, std::size_t, const char*, ...); int APS5_VABI sscanf_s_nid_postfix(const char*, const char*, ...); +int APS5_VABI __inet_aton_nid_postfix(const char*, void*); } static void Require(bool condition) { @@ -137,6 +138,28 @@ int main() { Require(strpbrk_nid_postfix(buffer, "ay") == buffer + 1); Require(strpbrk_nid_postfix(buffer, "") == nullptr); Require(strcspn_nid_postfix(buffer, "y") == 1); + const auto inetAton = [](const char* text, const char* expected) { + unsigned char address[4] = {0xA5, 0xA5, 0xA5, 0xA5}; + if (__inet_aton_nid_postfix(text, address) != 1) return false; + char formatted[16]; + std::snprintf(formatted, sizeof(formatted), "%u.%u.%u.%u", address[0], address[1], address[2], address[3]); + return std::strcmp(formatted, expected) == 0; + }; + Require(inetAton("192.0.2.42", "192.0.2.42")); + Require(inetAton("10.1.2", "10.1.0.2")); + Require(inetAton("127.1", "127.0.0.1")); + Require(inetAton("3232235777", "192.168.1.1")); + Require(inetAton("0x7f.0.0.0x1", "127.0.0.1")); + Require(inetAton("0377.0.0.010", "255.0.0.8")); + Require(inetAton("1.2.3.4 trailing", "1.2.3.4")); + Require(inetAton("1.2.3.4\n", "1.2.3.4")); + Require(__inet_aton_nid_postfix("1.2.3.4", nullptr) == 1); + for (const char* invalid : {"", " 1.2.3.4", "1.2.3.4.5", "256.1.1.1", "1.2.3.256", "1.2.65536", "08", "1..2", "a.b.c.d", + "1.2.3.4x", "0x", "1.2.3.", "-1"}) { + unsigned char address[4] = {0xA5, 0xA5, 0xA5, 0xA5}; + Require(__inet_aton_nid_postfix(invalid, address) == 0); + Require(address[0] == 0xA5 && address[3] == 0xA5); + } char first[] = ",a,,b,"; char second[] = "x:y"; char* firstState = nullptr;