diff --git a/core/libs/prx/libc/src/Strings.cpp b/core/libs/prx/libc/src/Strings.cpp index 62c41a926..533611a17 100644 --- a/core/libs/prx/libc/src/Strings.cpp +++ b/core/libs/prx/libc/src/Strings.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "prx/libc/include/General.hpp" @@ -28,6 +29,21 @@ size_t Length(const char16_t* s) { return length; } +std::string AsciiPrefix(const char16_t* text) { + std::string prefix; + for (; *text != 0 && *text < 0x80; ++text) prefix.push_back(static_cast(*text)); + return prefix; +} + +template +auto ParseAsciiPrefix(const char16_t* text, char16_t** end, TParse parse) { + const std::string prefix = AsciiPrefix(text); + char* parsedEnd = nullptr; + const auto value = parse(prefix.c_str(), &parsedEnd); + if (end != nullptr) *end = const_cast(text) + (parsedEnd - prefix.c_str()); + return value; +} + } extern "C" { @@ -378,42 +394,44 @@ char16_t* APS5_VABI wmemset_nid_postfix(char16_t* s, char16_t c, size_t n) { return s; } -double APS5_VABI wcstod_nid_postfix(const wchar_t* str, wchar_t** endptr) { - return std::wcstod(str, endptr); +double APS5_VABI wcstod_nid_postfix(const char16_t* str, char16_t** endptr) { + return ParseAsciiPrefix(str, endptr, [](const char* text, char** end) { return std::strtod(text, end); }); } -float APS5_VABI wcstof_nid_postfix(const wchar_t* str, wchar_t** endptr) { - return std::wcstof(str, endptr); +float APS5_VABI wcstof_nid_postfix(const char16_t* str, char16_t** endptr) { + return ParseAsciiPrefix(str, endptr, [](const char* text, char** end) { return std::strtof(text, end); }); } -long double APS5_VABI wcstold_nid_postfix(const wchar_t* str, wchar_t** endptr) { +long double APS5_VABI wcstold_nid_postfix(const char16_t* str, char16_t** endptr) { static_assert(sizeof(long double) == 16); static_assert(std::numeric_limits::digits == 64); - return std::wcstold(str, endptr); + return ParseAsciiPrefix(str, endptr, [](const char* text, char** end) { return std::strtold(text, end); }); } -long long APS5_VABI wcstol_nid_postfix(const wchar_t* str, wchar_t** endptr, int base) { - return std::wcstoll(str, endptr, base); +long long APS5_VABI wcstol_nid_postfix(const char16_t* str, char16_t** endptr, int base) { + return ParseAsciiPrefix(str, endptr, [base](const char* text, char** end) { return std::strtoll(text, end, base); }); } -long long APS5_VABI wcstoll_nid_postfix(const wchar_t* str, wchar_t** endptr, int base) { - return std::wcstoll(str, endptr, base); +long long APS5_VABI wcstoll_nid_postfix(const char16_t* str, char16_t** endptr, int base) { + return ParseAsciiPrefix(str, endptr, [base](const char* text, char** end) { return std::strtoll(text, end, base); }); } -unsigned long long APS5_VABI wcstoul_nid_postfix(const wchar_t* str, wchar_t** endptr, int base) { - return std::wcstoull(str, endptr, base); +unsigned long long APS5_VABI wcstoul_nid_postfix(const char16_t* str, char16_t** endptr, int base) { + return ParseAsciiPrefix(str, endptr, [base](const char* text, char** end) { return std::strtoull(text, end, base); }); } -unsigned long long APS5_VABI wcstoull_nid_postfix(const wchar_t* str, wchar_t** endptr, int base) { - return std::wcstoull(str, endptr, base); +unsigned long long APS5_VABI wcstoull_nid_postfix(const char16_t* str, char16_t** endptr, int base) { + return ParseAsciiPrefix(str, endptr, [base](const char* text, char** end) { return std::strtoull(text, end, base); }); } -int APS5_VABI wcscoll_nid_postfix(const wchar_t* first, const wchar_t* second) { - return std::wcscoll(first, second); +int APS5_VABI wcscoll_nid_postfix(const char16_t* first, const char16_t* second) { + return wcscmp_nid_postfix(first, second); } -size_t APS5_VABI wcsxfrm_nid_postfix(wchar_t* destination, const wchar_t* source, size_t count) { - return std::wcsxfrm(destination, source, count); +size_t APS5_VABI wcsxfrm_nid_postfix(char16_t* destination, const char16_t* source, size_t count) { + const size_t length = Length(source); + if (length < count) std::memcpy(destination, source, (length + 1) * sizeof(char16_t)); + return length; } size_t APS5_VABI strxfrm_nid_postfix(char* destination, const char* source, size_t count) { diff --git a/core/libs/tests/GuestWideStrings.cpp b/core/libs/tests/GuestWideStrings.cpp index 497e3d4d5..e08349ab4 100644 --- a/core/libs/tests/GuestWideStrings.cpp +++ b/core/libs/tests/GuestWideStrings.cpp @@ -1,4 +1,5 @@ #include "prx/libc/include/general/VabiMacros.hpp" +#include #include #include @@ -18,6 +19,15 @@ const char16_t* APS5_VABI wcsstr_nid_postfix(const char16_t* haystack, const cha const char16_t* APS5_VABI wcspbrk_nid_postfix(const char16_t* s, const char16_t* accept); std::size_t APS5_VABI wcsspn_nid_postfix(const char16_t* s, const char16_t* accept); char16_t* APS5_VABI wmemset_nid_postfix(char16_t* s, char16_t c, std::size_t n); +double APS5_VABI wcstod_nid_postfix(const char16_t* str, char16_t** endptr); +float APS5_VABI wcstof_nid_postfix(const char16_t* str, char16_t** endptr); +long double APS5_VABI wcstold_nid_postfix(const char16_t* str, char16_t** endptr); +long long APS5_VABI wcstol_nid_postfix(const char16_t* str, char16_t** endptr, int base); +long long APS5_VABI wcstoll_nid_postfix(const char16_t* str, char16_t** endptr, int base); +unsigned long long APS5_VABI wcstoul_nid_postfix(const char16_t* str, char16_t** endptr, int base); +unsigned long long APS5_VABI wcstoull_nid_postfix(const char16_t* str, char16_t** endptr, int base); +int APS5_VABI wcscoll_nid_postfix(const char16_t* first, const char16_t* second); +std::size_t APS5_VABI wcsxfrm_nid_postfix(char16_t* destination, const char16_t* source, std::size_t count); } namespace { @@ -101,5 +111,36 @@ int main() { const char16_t shifted[] = {u'1', u'1', u'2', u'3', u'4'}; require(same(overlap, shifted, 5)); require(wmemset_nid_postfix(copy, u'世', 3) == copy && copy[0] == u'世' && copy[2] == u'世' && copy[3] == u'y'); + + char16_t* end = nullptr; + const char16_t* negative = u" -42xyz"; + require(wcstol_nid_postfix(negative, &end, 10) == -42 && end == negative + 5); + const char16_t* hex = u"0x1F!"; + require(wcstoll_nid_postfix(hex, &end, 16) == 31 && end == hex + 4); + require(wcstoll_nid_postfix(hex, &end, 0) == 31 && end == hex + 4); + const char16_t* wideSpace = u" 12"; + require(wcstoul_nid_postfix(wideSpace, &end, 10) == 0 && end == wideSpace); + const char16_t* wideDigit = u"12١"; + require(wcstoll_nid_postfix(wideDigit, &end, 10) == 12 && end == wideDigit + 2); + require(wcstoull_nid_postfix(u"18446744073709551615", nullptr, 10) == ULLONG_MAX); + const char16_t* letters = u"abc"; + require(wcstol_nid_postfix(letters, &end, 10) == 0 && end == letters); + + const char16_t* scientific = u"3.5e2!"; + require(wcstod_nid_postfix(scientific, &end) == 350.0 && end == scientific + 5); + require(wcstod_nid_postfix(letters, &end) == 0.0 && end == letters); + require(wcstof_nid_postfix(u"0.25", nullptr) == 0.25f); + const char16_t* half = u"-1.5é"; + require(wcstold_nid_postfix(half, &end) == -1.5L && end == half + 4); + + require(wcscoll_nid_postfix(u"a", u"b") < 0); + require(wcscoll_nid_postfix(u"￿", u"a") > 0); + require(wcscoll_nid_postfix(u"same", u"same") == 0); + + char16_t transformed[8]; + wmemset_nid_postfix(transformed, 0xaaaa, 8); + require(wcsxfrm_nid_postfix(transformed, u"wide", 8) == 4 && same(transformed, u"wide", 5) && transformed[5] == 0xaaaa); + require(wcsxfrm_nid_postfix(transformed, u"much too long", 4) == 13); + require(wcsxfrm_nid_postfix(nullptr, u"abc", 0) == 3); return 0; } diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 56beb23c1..dc3cb67a7 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -227,7 +227,7 @@ Throughout the project, every function at every stage either **does exactly what - [libSceHttp2 asynchronous requests](../../core/libs/prx/libSceHttp2/Export.cpp) (libSceHttp2) never reach the network: `sceHttp2SendRequestAsync` accepts the request, queues a completion whose result is the library's network error and triggers the user event named by the kqueue option (equeue at offset 0, event id at 8, user data at 16, layout inferred from PPSA17221); `sceHttp2WaitAsync` returns that completion. The completion's `event_type` value is unknown and left 0. PPSA17221 completes its request object only after `sceHttp2SendRequestAsync` returns, and its handler for a synchronous failure calls `shared_from_this` before that, so a send must not fail synchronously. A kqueue option or user event that is missing, an extra option, and a wait with no completed operation throw - [sceKernelWriteThrottlingStatus](../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) reports no write throttling: the host SSD has no write budget, so the first qword of the 32-byte status is set to `UINT32_MAX` and the rest is zeroed. PPSA17221 reads only that qword: it multiplies it by 64 KiB and compares the product with byte thresholds up to 0x4AF00000 to pick one of five tiers. When the call fails the title assumes 0x4B000001 bytes, which selects the same top tier as `UINT32_MAX`. What the value measures on hardware (a remaining budget or a rate) and the meaning of the other three qwords are unknown -- [Wide-character conversions](../../core/libs/prx/libc/src/Strings.cpp) (libc) - `wcstod`, `wcstof`, `wcstold`, `wcstol`, `wcstoll`, `wcstoul`, `wcstoull`, `wcscoll`, `wcsxfrm`, `wcsrtombs` and [`swprintf`](../../core/libs/prx/libc/src/Formatting.cpp) still pass guest strings to the host's `wchar_t` functions. The guest's `wchar_t` is 16 bits, so on Linux, where the host's is 32 bits, they misread and miswrite guest strings +- [Wide-character conversions](../../core/libs/prx/libc/src/Strings.cpp) (libc) - `wcsrtombs` and [`swprintf`](../../core/libs/prx/libc/src/Formatting.cpp) still pass guest strings to the host's `wchar_t` functions. The guest's `wchar_t` is 16 bits, so on Linux, where the host's is 32 bits, they misread and miswrite guest strings. The wide `wcsto*` functions parse the ASCII prefix of the guest string, as the C locale accepts only ASCII white space and digits - [sceNetSendmsg and sceNetRecvmsg](../../core/libs/prx/libSceNet/Export.cpp) (libSceNet) copy the scatter/gather list through one host buffer and accept only the flags `sceNetSend`/`sceNetRecv` accept. Sending control data throws; a receive never returns control data (`msg_controllen` 0), since no socket option that queues it is supported - [Additional content](../../core/libs/prx/libSceAppContent/Export.cpp) (libSceAppContent) is not installed or mounted: `sceAppContentAddcontMount` answers `NOT_FOUND` for every entitlement label and `sceAppContentAddcontUnmount` for every mount point, as no add-content mount is ever handed out; `sceAppContentGetAddcontInfo` answers `DRM_NO_ENTITLEMENT` for every label and `sceAppContentGetAddcontInfoList` reports 0 entries - [sceKernelPread, sceKernelPwrite, sceKernelPreadv, sceKernelPwritev](../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) on Windows transfer at the given offset with an overlapped `ReadFile`/`WriteFile` and restore the descriptor's file pointer afterwards, so a concurrent `read`/`write` on the same descriptor can see the moved pointer in between, unlike on the console. Handles that are not disk files return `ESPIPE`. `readv` on a pipe or console handle stops after the first iovec read, a short read the console is assumed to allow; each iovec is transferred separately, so a failure after the first transfer returns the bytes already done