From fb6580ae36bf807a0550f3d15553f186ea3a078b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Thu, 1 Oct 2026 18:41:25 +0200 Subject: [PATCH] feat(libSceCesCs): convert single characters between CP1252 and UTF-8 sceCesRefersUcsProfileCp1252 returns the CP1252 profile, and sceCesSbcToUtf8 / sceCesUtf8ToSbc convert one character in each direction. The five bytes CP1252 leaves undefined map to the matching C1 controls, so every byte round-trips. Malformed UTF-8 and code points outside CP1252 return the library's error codes. Co-Authored-By: Claude --- core/libs/prx/libSceCesCs/CMakeLists.txt | 20 ++++ core/libs/prx/libSceCesCs/Export.cpp | 112 +++++++++++++++++++---- core/libs/tests/GuestCes.cpp | 83 +++++++++++++++++ 3 files changed, 199 insertions(+), 16 deletions(-) create mode 100644 core/libs/prx/libSceCesCs/CMakeLists.txt create mode 100644 core/libs/tests/GuestCes.cpp diff --git a/core/libs/prx/libSceCesCs/CMakeLists.txt b/core/libs/prx/libSceCesCs/CMakeLists.txt new file mode 100644 index 000000000..78187b9e4 --- /dev/null +++ b/core/libs/prx/libSceCesCs/CMakeLists.txt @@ -0,0 +1,20 @@ +add_library(libSceCesCs SHARED EXCLUDE_FROM_ALL Export.cpp) +target_include_directories(libSceCesCs PRIVATE ${LIBS_INCLUDE_DIR}) +configure_windows_unwind(libSceCesCs) + +if(MINGW) + foreach(runtimeLinkFlag IN ITEMS -static-libgcc -static-libstdc++ -static) + string(REPLACE "${runtimeLinkFlag}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") + endforeach() +endif() + +if(BUILD_TESTING) + add_test_executable(guest_ces_tests ${LIBS_INCLUDE_DIR}/tests/GuestCes.cpp) + target_include_directories(guest_ces_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_ces_tests PRIVATE libSceCesCs) + add_test(NAME guest_ces COMMAND guest_ces_tests) + if(WIN32) + set_tests_properties(guest_ces PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceCesCs/Export.cpp b/core/libs/prx/libSceCesCs/Export.cpp index aaa150eef..a2649662d 100644 --- a/core/libs/prx/libSceCesCs/Export.cpp +++ b/core/libs/prx/libSceCesCs/Export.cpp @@ -3,31 +3,111 @@ #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" +namespace { + +constexpr int CES_ERROR_INVALID_PARAMETER = static_cast(0x805C0001); +constexpr int CES_ERROR_INVALID_SRC_BUFFER = static_cast(0x805C0010); +constexpr int CES_ERROR_SRC_BUFFER_END = static_cast(0x805C0011); +constexpr int CES_ERROR_INVALID_ENCODE = static_cast(0x805C0014); +constexpr int CES_ERROR_UNASSIGNED_CODE = static_cast(0x805C0020); +constexpr int CES_ERROR_INVALID_DST_BUFFER = static_cast(0x805C0030); +constexpr int CES_ERROR_DST_BUFFER_END = static_cast(0x805C0031); + +constexpr uint8_t CP1252_PROFILE = 0; + +constexpr uint16_t CP1252_HIGH[32] = { + 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F, + 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178, +}; + +uint32_t cp1252ToUnicode(uint8_t sbc) { + return sbc >= 0x80 && sbc < 0xA0 ? CP1252_HIGH[sbc - 0x80] : sbc; +} + +bool unicodeToCp1252(uint32_t code, uint8_t& sbc) { + if (code < 0x80 || (code >= 0xA0 && code <= 0xFF)) { + sbc = static_cast(code); + return true; + } + for (uint8_t i = 0; i < 32; ++i) { + if (CP1252_HIGH[i] == code) { + sbc = static_cast(0x80 + i); + return true; + } + } + return false; +} + +int decodeUtf8(const uint8_t* utf8, uint32_t utf8max, uint32_t& length, uint32_t& code) { + const uint8_t lead = utf8[0]; + uint32_t minimum = 0; + if (lead < 0x80) { + length = 1; + code = lead; + return 0; + } + if ((lead & 0xE0) == 0xC0) { length = 2; code = lead & 0x1F; minimum = 0x80; } + else if ((lead & 0xF0) == 0xE0) { length = 3; code = lead & 0x0F; minimum = 0x800; } + else if ((lead & 0xF8) == 0xF0) { length = 4; code = lead & 0x07; minimum = 0x10000; } + else return CES_ERROR_INVALID_ENCODE; + + for (uint32_t i = 1; i < length; ++i) { + if (i >= utf8max) return CES_ERROR_SRC_BUFFER_END; + if ((utf8[i] & 0xC0) != 0x80) return CES_ERROR_INVALID_ENCODE; + code = code << 6 | (utf8[i] & 0x3F); + } + if (code < minimum || code > 0x10FFFF || (code >= 0xD800 && code <= 0xDFFF)) return CES_ERROR_INVALID_ENCODE; + return 0; +} + +uint32_t encodeUtf8(uint32_t code, uint8_t* utf8) { + if (code < 0x80) { + utf8[0] = static_cast(code); + return 1; + } + if (code < 0x800) { + utf8[0] = static_cast(0xC0 | code >> 6); + utf8[1] = static_cast(0x80 | (code & 0x3F)); + return 2; + } + utf8[0] = static_cast(0xE0 | code >> 12); + utf8[1] = static_cast(0x80 | (code >> 6 & 0x3F)); + utf8[2] = static_cast(0x80 | (code & 0x3F)); + return 3; +} + +} + extern "C" { const uint8_t* APS5_VABI sceCesRefersUcsProfileCp1252(void) { - NotImplemented_nid_no_patch(__func__); - return nullptr; + return &CP1252_PROFILE; } int APS5_VABI sceCesSbcToUtf8(const uint8_t* profile, uint8_t sbc, uint8_t* utf8, uint32_t utf8max, uint32_t* utf8_len) { - (void)profile; - (void)sbc; - (void)utf8; - (void)utf8max; - (void)utf8_len; - NotImplemented_nid_no_patch(__func__); - return 0; + if (profile != &CP1252_PROFILE) return CES_ERROR_INVALID_PARAMETER; + if (!utf8) return CES_ERROR_INVALID_DST_BUFFER; + + uint8_t encoded[3]; + const uint32_t length = encodeUtf8(cp1252ToUnicode(sbc), encoded); + if (utf8max < length) return CES_ERROR_DST_BUFFER_END; + for (uint32_t i = 0; i < length; ++i) utf8[i] = encoded[i]; + if (utf8_len) *utf8_len = length; + return 0; } int APS5_VABI sceCesUtf8ToSbc(const uint8_t* utf8, uint32_t utf8max, uint32_t* utf8_len, const uint8_t* profile, uint8_t* sbc) { - (void)utf8; - (void)utf8max; - (void)utf8_len; - (void)profile; - (void)sbc; - NotImplemented_nid_no_patch(__func__); - return 0; + if (profile != &CP1252_PROFILE) return CES_ERROR_INVALID_PARAMETER; + if (!utf8) return CES_ERROR_INVALID_SRC_BUFFER; + if (utf8max == 0) return CES_ERROR_SRC_BUFFER_END; + if (!sbc) return CES_ERROR_INVALID_DST_BUFFER; + + uint32_t length = 0; + uint32_t code = 0; + if (const int result = decodeUtf8(utf8, utf8max, length, code); result != 0) return result; + if (!unicodeToCp1252(code, *sbc)) return CES_ERROR_UNASSIGNED_CODE; + if (utf8_len) *utf8_len = length; + return 0; } } diff --git a/core/libs/tests/GuestCes.cpp b/core/libs/tests/GuestCes.cpp new file mode 100644 index 000000000..985713bcb --- /dev/null +++ b/core/libs/tests/GuestCes.cpp @@ -0,0 +1,83 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include + +extern "C" { +const std::uint8_t* APS5_VABI sceCesRefersUcsProfileCp1252(void); +int APS5_VABI sceCesSbcToUtf8(const std::uint8_t*, std::uint8_t, std::uint8_t*, std::uint32_t, std::uint32_t*); +int APS5_VABI sceCesUtf8ToSbc(const std::uint8_t*, std::uint32_t, std::uint32_t*, const std::uint8_t*, std::uint8_t*); +} + +static void Require(bool value) { if (!value) std::abort(); } + +static void RequireSbcToUtf8(const std::uint8_t* profile, std::uint8_t sbc, const char* expected) { + std::uint8_t utf8[4] = {}; + std::uint32_t length = 0; + Require(sceCesSbcToUtf8(profile, sbc, utf8, sizeof(utf8), &length) == 0); + Require(length == std::strlen(expected) && std::memcmp(utf8, expected, length) == 0); +} + +static void RequireUtf8ToSbc(const std::uint8_t* profile, const char* utf8, std::uint8_t expected) { + const auto size = static_cast(std::strlen(utf8)); + std::uint32_t length = 0; + std::uint8_t sbc = 0; + Require(sceCesUtf8ToSbc(reinterpret_cast(utf8), size + 4, &length, profile, &sbc) == 0); + Require(length == size && sbc == expected); +} + +static int Utf8ToSbc(const std::uint8_t* profile, const char* utf8, std::uint32_t utf8max) { + std::uint32_t length = 0; + std::uint8_t sbc = 0; + return sceCesUtf8ToSbc(reinterpret_cast(utf8), utf8max, &length, profile, &sbc); +} + +int main() { + constexpr int invalidParameter = static_cast(0x805C0001); + constexpr int invalidSrcBuffer = static_cast(0x805C0010); + constexpr int srcBufferEnd = static_cast(0x805C0011); + constexpr int invalidEncode = static_cast(0x805C0014); + constexpr int unassignedCode = static_cast(0x805C0020); + constexpr int invalidDstBuffer = static_cast(0x805C0030); + constexpr int dstBufferEnd = static_cast(0x805C0031); + + const std::uint8_t* profile = sceCesRefersUcsProfileCp1252(); + Require(profile != nullptr && profile == sceCesRefersUcsProfileCp1252()); + + RequireSbcToUtf8(profile, 'A', "A"); + RequireSbcToUtf8(profile, 0x80, "\xE2\x82\xAC"); + RequireSbcToUtf8(profile, 0x9F, "\xC5\xB8"); + RequireSbcToUtf8(profile, 0x81, "\xC2\x81"); + RequireSbcToUtf8(profile, 0xE9, "\xC3\xA9"); + RequireSbcToUtf8(profile, 0xFF, "\xC3\xBF"); + + RequireUtf8ToSbc(profile, "A", 'A'); + RequireUtf8ToSbc(profile, "\xE2\x82\xAC", 0x80); + RequireUtf8ToSbc(profile, "\xE2\x84\xA2", 0x99); + RequireUtf8ToSbc(profile, "\xC3\xA9", 0xE9); + RequireUtf8ToSbc(profile, "\xC2\x8D", 0x8D); + for (int sbc = 1; sbc < 256; ++sbc) { + std::uint8_t utf8[4] = {}; + std::uint32_t length = 0; + std::uint8_t back = 0; + Require(sceCesSbcToUtf8(profile, static_cast(sbc), utf8, sizeof(utf8), &length) == 0); + Require(sceCesUtf8ToSbc(utf8, length, &length, profile, &back) == 0 && back == sbc); + } + + std::uint8_t utf8[4] = {}; + std::uint32_t length = 0; + std::uint8_t sbc = 0; + Require(sceCesSbcToUtf8(nullptr, 'A', utf8, sizeof(utf8), &length) == invalidParameter); + Require(sceCesSbcToUtf8(profile, 'A', nullptr, sizeof(utf8), &length) == invalidDstBuffer); + Require(sceCesSbcToUtf8(profile, 0x80, utf8, 2, &length) == dstBufferEnd); + Require(sceCesUtf8ToSbc(utf8, 1, &length, nullptr, &sbc) == invalidParameter); + Require(sceCesUtf8ToSbc(nullptr, 1, &length, profile, &sbc) == invalidSrcBuffer); + Require(sceCesUtf8ToSbc(utf8, 1, &length, profile, nullptr) == invalidDstBuffer); + Require(Utf8ToSbc(profile, "A", 0) == srcBufferEnd); + Require(Utf8ToSbc(profile, "\xE2\x82\xAC", 2) == srcBufferEnd); + Require(Utf8ToSbc(profile, "\x80", 1) == invalidEncode); + Require(Utf8ToSbc(profile, "\xC0\xAF", 2) == invalidEncode); + Require(Utf8ToSbc(profile, "\xED\xA0\x80", 3) == invalidEncode); + Require(Utf8ToSbc(profile, "\xE3\x81\x82", 3) == unassignedCode); + Require(Utf8ToSbc(profile, "\xF0\x9F\x98\x80", 4) == unassignedCode); +}