diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index 8256607b2..f00f78d4e 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -104,6 +104,15 @@ endforeach() add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS}) if(BUILD_TESTING) + add_executable(guest_character_tests tests/GuestCharacter.cpp) + target_include_directories(guest_character_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_character_tests PRIVATE libc) + configure_windows_unwind(guest_character_tests) + add_test(NAME guest_character COMMAND guest_character_tests) + if(WIN32) + set_tests_properties(guest_character PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_executable(guest_math_tests tests/GuestMath.cpp) target_include_directories(guest_math_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(guest_math_tests PRIVATE libc) @@ -248,12 +257,15 @@ if(BUILD_TESTING) set_tests_properties(guest_memory PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() - add_executable(guest_string_tests - tests/GuestStrings.cpp - prx/libc/src/StringExtensions.cpp - prx/libScePosixForWebKit/Export.cpp) + add_executable(guest_string_tests tests/GuestStrings.cpp) target_include_directories(guest_string_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_string_tests PRIVATE libc libScePosixForWebKit) + configure_windows_unwind(guest_string_tests) add_test(NAME guest_strings COMMAND guest_string_tests) + if(WIN32) + set_tests_properties(guest_strings PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() endif() add_executable(application_heap_tests EXCLUDE_FROM_ALL tests/ApplicationHeap.cpp prx/libc/src/ApplicationHeap.cpp) diff --git a/core/libs/prx/libc/src/LocaleSupport.cpp b/core/libs/prx/libc/src/LocaleSupport.cpp index b16a1bb7c..0d178f899 100644 --- a/core/libs/prx/libc/src/LocaleSupport.cpp +++ b/core/libs/prx/libc/src/LocaleSupport.cpp @@ -84,6 +84,25 @@ constexpr auto g_upperTable = MakeCaseTable(true); extern "C" { +// The runtime currently exposes the classic C locale. Keep byte classification +// independent of any locale selected by host-side libraries. +int APS5_VABI isupper_nid_postfix(int c) { return c >= 'A' && c <= 'Z'; } +int APS5_VABI islower_nid_postfix(int c) { return c >= 'a' && c <= 'z'; } +int APS5_VABI isalpha_nid_postfix(int c) { return isupper_nid_postfix(c) || islower_nid_postfix(c); } +int APS5_VABI isdigit_nid_postfix(int c) { return c >= '0' && c <= '9'; } +int APS5_VABI isalnum_nid_postfix(int c) { return isalpha_nid_postfix(c) || isdigit_nid_postfix(c); } +int APS5_VABI isspace_nid_postfix(int c) { return c == ' ' || (c >= '\t' && c <= '\r'); } +int APS5_VABI isblank_nid_postfix(int c) { return c == ' ' || c == '\t'; } +int APS5_VABI iscntrl_nid_postfix(int c) { return (c >= 0 && c < 32) || c == 127; } +int APS5_VABI isprint_nid_postfix(int c) { return c >= 32 && c <= 126; } +int APS5_VABI isgraph_nid_postfix(int c) { return c >= 33 && c <= 126; } +int APS5_VABI ispunct_nid_postfix(int c) { return isgraph_nid_postfix(c) && !isalnum_nid_postfix(c); } +int APS5_VABI isxdigit_nid_postfix(int c) { + return isdigit_nid_postfix(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +} +int APS5_VABI toupper_nid_postfix(int c) { return islower_nid_postfix(c) ? c - ('a' - 'A') : c; } +int APS5_VABI tolower_nid_postfix(int c) { return isupper_nid_postfix(c) ? c + ('a' - 'A') : c; } + std::uint64_t _ZNSt5ctypeIcE2idE_nid_postfix = 0; std::uint64_t _ZNSt5ctypeIwE2idE_nid_postfix = 0; std::uint64_t _ZNSt7collateIwE2idE_nid_postfix = 0; diff --git a/core/libs/prx/libc/src/StringExtensions.cpp b/core/libs/prx/libc/src/StringExtensions.cpp index 7289514f2..9533fd26b 100644 --- a/core/libs/prx/libc/src/StringExtensions.cpp +++ b/core/libs/prx/libc/src/StringExtensions.cpp @@ -1,9 +1,25 @@ #include "prx/libc/include/general/VabiMacros.hpp" #include #include +#include +#include extern "C" { +char* APS5_VABI basename_nid_postfix(const char* path) { + thread_local char buffer[1024]; + std::string_view name = path && *path ? path : "."; + while (name.size() > 1 && name.back() == '/') name.remove_suffix(1); + if (name != "/") { + const auto separator = name.find_last_of('/'); + if (separator != std::string_view::npos) name.remove_prefix(separator + 1); + } + if (name.size() >= sizeof(buffer)) { errno = 63; return nullptr; } + std::memcpy(buffer, name.data(), name.size()); + buffer[name.size()] = '\0'; + return buffer; +} + std::size_t APS5_VABI strnlen_nid_postfix(const char* text, std::size_t limit) { std::size_t length = 0; while (length < limit && text[length] != '\0') ++length; diff --git a/core/libs/tests/GuestCharacter.cpp b/core/libs/tests/GuestCharacter.cpp new file mode 100644 index 000000000..60331fc87 --- /dev/null +++ b/core/libs/tests/GuestCharacter.cpp @@ -0,0 +1,45 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include +extern "C" { +int APS5_VABI isupper_nid_postfix(int); +int APS5_VABI islower_nid_postfix(int); +int APS5_VABI isalpha_nid_postfix(int); +int APS5_VABI isdigit_nid_postfix(int); +int APS5_VABI isalnum_nid_postfix(int); +int APS5_VABI isspace_nid_postfix(int); +int APS5_VABI isblank_nid_postfix(int); +int APS5_VABI iscntrl_nid_postfix(int); +int APS5_VABI isprint_nid_postfix(int); +int APS5_VABI isgraph_nid_postfix(int); +int APS5_VABI ispunct_nid_postfix(int); +int APS5_VABI isxdigit_nid_postfix(int); +int APS5_VABI toupper_nid_postfix(int); +int APS5_VABI tolower_nid_postfix(int); +const short* APS5_VABI _Getptolower_nid_postfix(); +const short* APS5_VABI _Getptoupper_nid_postfix(); +} +static void Require(bool value) { if (!value) std::abort(); } +int main() { + Require(std::setlocale(LC_CTYPE, "C") != nullptr); + for (int c = EOF; c <= 255; ++c) { + Require(bool(isupper_nid_postfix(c)) == bool(std::isupper(c))); + Require(bool(islower_nid_postfix(c)) == bool(std::islower(c))); + Require(bool(isalpha_nid_postfix(c)) == bool(std::isalpha(c))); + Require(bool(isdigit_nid_postfix(c)) == bool(std::isdigit(c))); + Require(bool(isalnum_nid_postfix(c)) == bool(std::isalnum(c))); + Require(bool(isspace_nid_postfix(c)) == bool(std::isspace(c))); + Require(bool(isblank_nid_postfix(c)) == bool(std::isblank(c))); + Require(bool(iscntrl_nid_postfix(c)) == bool(std::iscntrl(c))); + Require(bool(isprint_nid_postfix(c)) == bool(std::isprint(c))); + Require(bool(isgraph_nid_postfix(c)) == bool(std::isgraph(c))); + Require(bool(ispunct_nid_postfix(c)) == bool(std::ispunct(c))); + Require(bool(isxdigit_nid_postfix(c)) == bool(std::isxdigit(c))); + Require(toupper_nid_postfix(c) == std::toupper(c)); + Require(tolower_nid_postfix(c) == std::tolower(c)); + Require(toupper_nid_postfix(c) == _Getptoupper_nid_postfix()[c]); + Require(tolower_nid_postfix(c) == _Getptolower_nid_postfix()[c]); + } +} diff --git a/core/libs/tests/GuestStrings.cpp b/core/libs/tests/GuestStrings.cpp index cd1c9dce8..cc8eefab6 100644 --- a/core/libs/tests/GuestStrings.cpp +++ b/core/libs/tests/GuestStrings.cpp @@ -2,8 +2,11 @@ #include #include #include +#include extern "C" { +char* APS5_VABI basename_nid_postfix(const char*); +int* APS5_VABI __error_nid_postfix(); std::size_t APS5_VABI strnlen_nid_postfix(const char*, std::size_t); char* APS5_VABI strncat_nid_postfix(char*, const char*, std::size_t); char* APS5_VABI strpbrk_nid_postfix(const char*, const char*); @@ -22,6 +25,15 @@ static void Require(bool condition) { } int main() { + Require(std::strcmp(basename_nid_postfix(nullptr), ".") == 0); + Require(std::strcmp(basename_nid_postfix(""), ".") == 0); + Require(std::strcmp(basename_nid_postfix("////"), "/") == 0); + const char path[] = "/one/two///"; + Require(std::strcmp(basename_nid_postfix(path), "two") == 0); + Require(std::strcmp(path, "/one/two///") == 0); + Require(std::strcmp(basename_nid_postfix("one\\two"), "one\\two") == 0); + const std::string longName(1024, 'x'); + Require(basename_nid_postfix(longName.c_str()) == nullptr && *__error_nid_postfix() == 63); const char bounded[] = {'a', 'b', 'c'}; Require(strnlen_nid_postfix(bounded, 0) == 0); Require(strnlen_nid_postfix(bounded, sizeof(bounded)) == 3); diff --git a/docs/YAMAGI_QUAKE2.md b/docs/YAMAGI_QUAKE2.md index b1881722e..10495b95a 100644 --- a/docs/YAMAGI_QUAKE2.md +++ b/docs/YAMAGI_QUAKE2.md @@ -356,3 +356,21 @@ semantics have not been validated. The unchanged game resolves fmodf and atof. Startup now stops at `TYE4irxSmko` (`toupper`), still before guest entry-point execution or rendered output. + +## Follow-up: character classification and basename + +Added fourteen byte-character classification and case-conversion exports for +the runtime's classic C locale. Tests compare every byte value and EOF against +C-locale classification and verify case conversion agrees with existing guest +locale tables. Non-C locales are not implemented by these exports. + +Added basename with non-mutating input handling, trailing-slash removal, +root/empty/null cases, and the SDK's 1024-byte component buffer limit. The +contract follows [FreeBSD 11 basename](https://raw.githubusercontent.com/freebsd/freebsd-src/releng/11.4/lib/libc/gen/basename.c); +result storage is thread-local. Tests include unchanged input, literal backslash +handling, and ENAMETOOLONG. String tests now link the built compatibility +libraries rather than compiling implementation copies into the test executable. + +All twenty-one suites pass. The unchanged converted game resolves toupper and +basename and now stops at `5qP1iVQkdck` (`ftello`). Guest entry-point execution and +rendered output remain unverified.