mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libc): implement __inet_aton
PICO-8 for PS5 (PPSA99808) stops at start-up on the unresolved import a7ToDPsIQrc from libSceLibcInternal. The parser follows FreeBSD's inet_aton (lib/libc/inet/inet_addr.c): one to four parts in decimal, octal or hex, the last part filling the remaining bytes, input ending at a NUL or whitespace, and a null address only validating. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <cerrno>
|
||||
#include <string_view>
|
||||
@@ -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<char>(c | 0x20);
|
||||
if (c >= '0' && c <= '9') {
|
||||
if (base == 8 && c >= '8') return 0;
|
||||
value = value * base + static_cast<unsigned>(c - '0');
|
||||
} else if (base == 16 && lower >= 'a' && lower <= 'f') {
|
||||
value = (value << 4) | static_cast<unsigned>(lower - 'a' + 10);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (c != '.') break;
|
||||
if (count == 3 || value > 0xff) return 0;
|
||||
parts[count++] = static_cast<std::uint8_t>(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<std::uint8_t>(value >> 24), static_cast<std::uint8_t>(value >> 16),
|
||||
static_cast<std::uint8_t>(value >> 8), static_cast<std::uint8_t>(value)};
|
||||
std::memcpy(address, bytes, sizeof(bytes));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user