From 1211958b4eb38256cc0c5c7ba92d668820c05389 Mon Sep 17 00:00:00 2001 From: nmzik Date: Thu, 17 Sep 2026 19:50:21 +0400 Subject: [PATCH] clean up runtime helpers --- src/kernel/memory.cpp | 9 ++--- src/kernel/pthread.cpp | 7 ---- src/libs/ime.cpp | 55 +++++++++++++-------------- src/libs/imeDialog.cpp | 47 +++++++++++------------ src/libs/libKernel.cpp | 11 ++---- src/libs/libNet.cpp | 13 ------- src/libs/libPsml.cpp | 14 +++---- src/libs/network.cpp | 9 ----- src/loader/redZonePatcher.cpp | 10 ++--- src/loader/timer.cpp | 8 ---- src/loader/timer.h | 2 - src/loader/x64InstructionEmulator.cpp | 32 ++++++---------- 12 files changed, 74 insertions(+), 143 deletions(-) diff --git a/src/kernel/memory.cpp b/src/kernel/memory.cpp index 5580e045f..b53f27bf9 100644 --- a/src/kernel/memory.cpp +++ b/src/kernel/memory.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -3879,10 +3880,6 @@ static bool IsAligned(uint64_t value, uint64_t alignment) { return alignment == 0 || (value & (alignment - 1u)) == 0; } -static bool IsPowerOfTwo(uint64_t value) { - return value != 0 && (value & (value - 1u)) == 0; -} - static void MemoryPoolSubtractCommitted(uint64_t len) { auto current = g_memory_pool_committed.load(std::memory_order_relaxed); while (current != 0) { @@ -3904,7 +3901,7 @@ int KYTY_SYSV_ABI KernelMemoryPoolExpand(int64_t search_start, int64_t search_en if (search_start < 0 || search_end <= search_start || len == 0 || (len & (POOL_PAGE_SIZE - 1u)) != 0 || phys_addr_out == nullptr || (alignment != 0 && - (!IsPowerOfTwo(alignment) || (alignment & (POOL_PAGE_SIZE - 1u)) != 0))) { + (!std::has_single_bit(alignment) || (alignment & (POOL_PAGE_SIZE - 1u)) != 0))) { return KERNEL_ERROR_EINVAL; } if (static_cast(search_end - search_start) < len) { @@ -3953,7 +3950,7 @@ int KYTY_SYSV_ABI KernelMemoryPoolReserve(void* addr_in, size_t len, size_t alig return KERNEL_ERROR_EINVAL; } if (alignment != 0 && - (!IsPowerOfTwo(alignment) || !IsAligned(alignment, POOL_RESERVE_ALIGNMENT))) { + (!std::has_single_bit(alignment) || !IsAligned(alignment, POOL_RESERVE_ALIGNMENT))) { return KERNEL_ERROR_EINVAL; } diff --git a/src/kernel/pthread.cpp b/src/kernel/pthread.cpp index ea0206d46..a80fa00ab 100644 --- a/src/kernel/pthread.cpp +++ b/src/kernel/pthread.cpp @@ -1202,13 +1202,6 @@ static int32_t GetDstSeconds() { #endif } -#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS -static void sec_to_timeval(KernelTimeval* ts, double sec) { - ts->tv_sec = static_cast(sec); - ts->tv_usec = static_cast((sec - static_cast(ts->tv_sec)) * 1000000.0); -} -#endif - static bool GetPosixClockId(KernelClockid clock_id, clockid_t* out) { EXIT_IF(out == nullptr); diff --git a/src/libs/ime.cpp b/src/libs/ime.cpp index 2879d8dd1..08c196095 100644 --- a/src/libs/ime.cpp +++ b/src/libs/ime.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -18,35 +19,31 @@ namespace Libs::Ime { namespace { -constexpr int Error(uint32_t value) { - return static_cast(value); -} - -constexpr int ERROR_BUSY = Error(0x80bc0001); -constexpr int ERROR_NOT_OPENED = Error(0x80bc0002); -constexpr int ERROR_CONNECTION_FAILED = Error(0x80bc0004); -constexpr int ERROR_EVENT_OVERFLOW = Error(0x80bc0007); -constexpr int ERROR_INVALID_TEXT = Error(0x80bc0006); -constexpr int ERROR_INVALID_USER_ID = Error(0x80bc0010); -constexpr int ERROR_INVALID_TYPE = Error(0x80bc0011); -constexpr int ERROR_INVALID_LANGUAGES = Error(0x80bc0012); -constexpr int ERROR_INVALID_ENTER_LABEL = Error(0x80bc0013); -constexpr int ERROR_INVALID_INPUT_METHOD = Error(0x80bc0014); -constexpr int ERROR_INVALID_OPTION = Error(0x80bc0015); -constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = Error(0x80bc0016); -constexpr int ERROR_INVALID_TEXT_BUFFER = Error(0x80bc0017); -constexpr int ERROR_INVALID_POSX = Error(0x80bc0018); -constexpr int ERROR_INVALID_POSY = Error(0x80bc0019); -constexpr int ERROR_INVALID_HALIGN = Error(0x80bc001a); -constexpr int ERROR_INVALID_VALIGN = Error(0x80bc001b); -constexpr int ERROR_INVALID_EXTENDED = Error(0x80bc001c); -constexpr int ERROR_INVALID_WORK = Error(0x80bc0020); -constexpr int ERROR_INVALID_HANDLER = Error(0x80bc0022); -constexpr int ERROR_NO_RESOURCE_ID = Error(0x80bc0023); -constexpr int ERROR_INVALID_MODE = Error(0x80bc0024); -constexpr int ERROR_INVALID_PARAM = Error(0x80bc0030); -constexpr int ERROR_INVALID_ADDRESS = Error(0x80bc0031); -constexpr int ERROR_INVALID_RESERVED = Error(0x80bc0032); +constexpr int ERROR_BUSY = static_cast(0x80bc0001); +constexpr int ERROR_NOT_OPENED = static_cast(0x80bc0002); +constexpr int ERROR_CONNECTION_FAILED = static_cast(0x80bc0004); +constexpr int ERROR_EVENT_OVERFLOW = static_cast(0x80bc0007); +constexpr int ERROR_INVALID_TEXT = static_cast(0x80bc0006); +constexpr int ERROR_INVALID_USER_ID = static_cast(0x80bc0010); +constexpr int ERROR_INVALID_TYPE = static_cast(0x80bc0011); +constexpr int ERROR_INVALID_LANGUAGES = static_cast(0x80bc0012); +constexpr int ERROR_INVALID_ENTER_LABEL = static_cast(0x80bc0013); +constexpr int ERROR_INVALID_INPUT_METHOD = static_cast(0x80bc0014); +constexpr int ERROR_INVALID_OPTION = static_cast(0x80bc0015); +constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = static_cast(0x80bc0016); +constexpr int ERROR_INVALID_TEXT_BUFFER = static_cast(0x80bc0017); +constexpr int ERROR_INVALID_POSX = static_cast(0x80bc0018); +constexpr int ERROR_INVALID_POSY = static_cast(0x80bc0019); +constexpr int ERROR_INVALID_HALIGN = static_cast(0x80bc001a); +constexpr int ERROR_INVALID_VALIGN = static_cast(0x80bc001b); +constexpr int ERROR_INVALID_EXTENDED = static_cast(0x80bc001c); +constexpr int ERROR_INVALID_WORK = static_cast(0x80bc0020); +constexpr int ERROR_INVALID_HANDLER = static_cast(0x80bc0022); +constexpr int ERROR_NO_RESOURCE_ID = static_cast(0x80bc0023); +constexpr int ERROR_INVALID_MODE = static_cast(0x80bc0024); +constexpr int ERROR_INVALID_PARAM = static_cast(0x80bc0030); +constexpr int ERROR_INVALID_ADDRESS = static_cast(0x80bc0031); +constexpr int ERROR_INVALID_RESERVED = static_cast(0x80bc0032); constexpr uint32_t VALID_OPTIONS = 0x00007bff; constexpr uint32_t VALID_EXTENDED_OPTIONS = 0x00004fde; diff --git a/src/libs/imeDialog.cpp b/src/libs/imeDialog.cpp index fb777f571..5e765519e 100644 --- a/src/libs/imeDialog.cpp +++ b/src/libs/imeDialog.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -13,31 +14,27 @@ namespace Libs::Dialog::ImeDialog { namespace { -constexpr int Error(uint32_t value) { - return static_cast(value); -} - -constexpr int ERROR_BUSY = Error(0x80bc0001); -constexpr int ERROR_INVALID_USER_ID = Error(0x80bc0010); -constexpr int ERROR_INVALID_TYPE = Error(0x80bc0011); -constexpr int ERROR_INVALID_LANGUAGES = Error(0x80bc0012); -constexpr int ERROR_INVALID_ENTER_LABEL = Error(0x80bc0013); -constexpr int ERROR_INVALID_INPUT_METHOD = Error(0x80bc0014); -constexpr int ERROR_INVALID_OPTION = Error(0x80bc0015); -constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = Error(0x80bc0016); -constexpr int ERROR_INVALID_TEXT_BUFFER = Error(0x80bc0017); -constexpr int ERROR_INVALID_POSX = Error(0x80bc0018); -constexpr int ERROR_INVALID_POSY = Error(0x80bc0019); -constexpr int ERROR_INVALID_HALIGN = Error(0x80bc001a); -constexpr int ERROR_INVALID_VALIGN = Error(0x80bc001b); -constexpr int ERROR_INVALID_EXTENDED = Error(0x80bc001c); -constexpr int ERROR_INVALID_PARAM = Error(0x80bc0030); -constexpr int ERROR_INVALID_ADDRESS = Error(0x80bc0031); -constexpr int ERROR_INVALID_RESERVED = Error(0x80bc0032); -constexpr int ERROR_INVALID_TITLE = Error(0x80bc0101); -constexpr int ERROR_NOT_RUNNING = Error(0x80bc0105); -constexpr int ERROR_NOT_FINISHED = Error(0x80bc0106); -constexpr int ERROR_NOT_IN_USE = Error(0x80bc0107); +constexpr int ERROR_BUSY = static_cast(0x80bc0001); +constexpr int ERROR_INVALID_USER_ID = static_cast(0x80bc0010); +constexpr int ERROR_INVALID_TYPE = static_cast(0x80bc0011); +constexpr int ERROR_INVALID_LANGUAGES = static_cast(0x80bc0012); +constexpr int ERROR_INVALID_ENTER_LABEL = static_cast(0x80bc0013); +constexpr int ERROR_INVALID_INPUT_METHOD = static_cast(0x80bc0014); +constexpr int ERROR_INVALID_OPTION = static_cast(0x80bc0015); +constexpr int ERROR_INVALID_MAX_TEXT_LENGTH = static_cast(0x80bc0016); +constexpr int ERROR_INVALID_TEXT_BUFFER = static_cast(0x80bc0017); +constexpr int ERROR_INVALID_POSX = static_cast(0x80bc0018); +constexpr int ERROR_INVALID_POSY = static_cast(0x80bc0019); +constexpr int ERROR_INVALID_HALIGN = static_cast(0x80bc001a); +constexpr int ERROR_INVALID_VALIGN = static_cast(0x80bc001b); +constexpr int ERROR_INVALID_EXTENDED = static_cast(0x80bc001c); +constexpr int ERROR_INVALID_PARAM = static_cast(0x80bc0030); +constexpr int ERROR_INVALID_ADDRESS = static_cast(0x80bc0031); +constexpr int ERROR_INVALID_RESERVED = static_cast(0x80bc0032); +constexpr int ERROR_INVALID_TITLE = static_cast(0x80bc0101); +constexpr int ERROR_NOT_RUNNING = static_cast(0x80bc0105); +constexpr int ERROR_NOT_FINISHED = static_cast(0x80bc0106); +constexpr int ERROR_NOT_IN_USE = static_cast(0x80bc0107); constexpr uint32_t VALID_OPTIONS = 0x00007bff; constexpr uint32_t VALID_EXTENDED_OPTIONS = 0x00005fde; diff --git a/src/libs/libKernel.cpp b/src/libs/libKernel.cpp index 0c5784cdf..19460cb73 100644 --- a/src/libs/libKernel.cpp +++ b/src/libs/libKernel.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -68,10 +69,6 @@ using KernelModule = int32_t; using get_thread_atexit_count_func_t = KYTY_SYSV_ABI int (*)(KernelModule); using thread_atexit_report_func_t = KYTY_SYSV_ABI void (*)(KernelModule); -static uint32_t sha1_rol(uint32_t value, uint32_t bits) { - return (value << bits) | (value >> (32u - bits)); -} - static std::array sha1_digest(const uint8_t* data, size_t size) { uint32_t h0 = 0x67452301u; uint32_t h1 = 0xefcdab89u; @@ -99,7 +96,7 @@ static std::array sha1_digest(const uint8_t* data, size_t size) { (static_cast(msg[j + 2]) << 8u) | static_cast(msg[j + 3]); } for (int i = 16; i < 80; i++) { - w[i] = sha1_rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); + w[i] = std::rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); } uint32_t a = h0; @@ -125,10 +122,10 @@ static std::array sha1_digest(const uint8_t* data, size_t size) { k = 0xca62c1d6u; } - const auto temp = sha1_rol(a, 5) + f + e + k + w[i]; + const auto temp = std::rotl(a, 5) + f + e + k + w[i]; e = d; d = c; - c = sha1_rol(b, 30); + c = std::rotl(b, 30); b = a; a = temp; } diff --git a/src/libs/libNet.cpp b/src/libs/libNet.cpp index e3d73ffbc..10c13acb9 100644 --- a/src/libs/libNet.cpp +++ b/src/libs/libNet.cpp @@ -3118,16 +3118,6 @@ static std::map& NpWebApi2Requests() { return requests; } -[[maybe_unused]] static std::string NpWebApi2MakeResponse(const NpWebApi2Request& request) { - if (request.api_group.find("sessionManager") != std::string::npos || - request.path.find("sessions") != std::string::npos || - request.path.find("Sessions") != std::string::npos) { - return R"({"gameSessions":[],"playerSessions":[]})"; - } - - return "{}"; -} - static int KYTY_SYSV_ABI NpWebApi2Initialize(int lib_http_ctx_id, size_t pool_size) { PRINT_NAME(); @@ -3224,12 +3214,10 @@ NpWebApi2SendRequest(int64_t request_id, const void* data, size_t data_size, return NP_WEBAPI2_ERROR_REQUEST_NOT_FOUND; } - // request->second.response = NpWebApi2MakeResponse(request->second); request->second.response.clear(); request->second.read_offset = 0; if (response_info_option != nullptr) { - // response_info_option->http_status = 200; response_info_option->http_status = 0; response_info_option->response_data_size = request->second.response.size(); if (response_info_option->error_object != nullptr && @@ -3238,7 +3226,6 @@ NpWebApi2SendRequest(int64_t request_id, const void* data, size_t data_size, } } - // return 0; return NP_WEBAPI2_ERROR_NOT_SIGNED_IN; } diff --git a/src/libs/libPsml.cpp b/src/libs/libPsml.cpp index 6c9ef71be..8f642435c 100644 --- a/src/libs/libPsml.cpp +++ b/src/libs/libPsml.cpp @@ -13,15 +13,11 @@ LIB_VERSION("Psml", 1, "Psml", 1, 1); namespace Psml { -constexpr int Error(uint32_t value) { - return static_cast(value); -} - -constexpr int PSML_ERROR_NOT_INITIALIZED = Error(0x8a810001u); -constexpr int PSML_ERROR_INVALID_OBJECT = Error(0x8a810005u); -constexpr int PSML_ERROR_INVALID_POINTER = Error(0x8a810009u); -constexpr int PSML_ERROR_INVALID_VALUE = Error(0x8a81000du); -constexpr int PSML_ERROR_NULL_OBJECT = Error(0x8a810014u); +constexpr int PSML_ERROR_NOT_INITIALIZED = static_cast(0x8a810001u); +constexpr int PSML_ERROR_INVALID_OBJECT = static_cast(0x8a810005u); +constexpr int PSML_ERROR_INVALID_POINTER = static_cast(0x8a810009u); +constexpr int PSML_ERROR_INVALID_VALUE = static_cast(0x8a81000du); +constexpr int PSML_ERROR_NULL_OBJECT = static_cast(0x8a810014u); constexpr uint32_t SHARED_RESOURCES_MAGIC = 0xa9c4; constexpr uint32_t CONTEXT_MAGIC = 0x9231; diff --git a/src/libs/network.cpp b/src/libs/network.cpp index 72ab4a6c7..8c84340d9 100644 --- a/src/libs/network.cpp +++ b/src/libs/network.cpp @@ -3103,17 +3103,10 @@ static HostNetworkInfo QueryHostNetworkInfo() { #endif } -[[maybe_unused]] static bool HostNetworkConnected() { - return QueryHostNetworkInfo().connected; -} - static bool NetCtlConnected() { if (!g_net_ctl_status_initialized.load()) { - // g_net_ctl_connected = HostNetworkConnected(); g_net_ctl_connected = false; g_net_ctl_status_initialized = true; - // LOGF("\t host network connected = %s\n", (g_net_ctl_connected.load() ? "true" : - // "false")); LOGF("\t host network connected = false (forced offline)\n"); } @@ -3123,10 +3116,8 @@ static bool NetCtlConnected() { int KYTY_SYSV_ABI NetCtlInit() { PRINT_NAME(); - // g_net_ctl_connected = HostNetworkConnected(); g_net_ctl_connected = false; g_net_ctl_status_initialized = true; - // LOGF("\t host network connected = %s\n", (g_net_ctl_connected.load() ? "true" : "false")); LOGF("\t host network connected = false (forced offline)\n"); return OK; diff --git a/src/loader/redZonePatcher.cpp b/src/loader/redZonePatcher.cpp index eff93a15c..70382d259 100644 --- a/src/loader/redZonePatcher.cpp +++ b/src/loader/redZonePatcher.cpp @@ -103,11 +103,6 @@ static ZydisDecoder& GetDecoder() { return decoder; } -static ZyanStatus DecodeInstruction(ZydisDecodedInstruction& instruction, - ZydisDecodedOperand* operands, void* data, u64 size) { - return ZydisDecoderDecodeFull(&GetDecoder(), data, size, &instruction, operands); -} - #endif #if defined(_WIN32) @@ -177,8 +172,9 @@ uintptr_t GetRelativeTarget(const DecodedCodeInstruction& decoded) { DecodedCodeInstruction DecodeCodeInstruction(uintptr_t address, uintptr_t end) { DecodedCodeInstruction decoded {.address = address}; - const auto status = DecodeInstruction(decoded.instruction, decoded.operands.data(), - reinterpret_cast(address), end - address); + const auto status = ZydisDecoderDecodeFull(&GetDecoder(), reinterpret_cast(address), + end - address, &decoded.instruction, + decoded.operands.data()); if (!ZYAN_SUCCESS(status)) { decoded.instruction.length = 0; return decoded; diff --git a/src/loader/timer.cpp b/src/loader/timer.cpp index 0d21f74a0..00539ea7a 100644 --- a/src/loader/timer.cpp +++ b/src/loader/timer.cpp @@ -20,12 +20,4 @@ Common::Time GetTime() { return Common::Time(static_cast(GetTimeMs())); } -uint64_t GetCounter() { - return g_timer.GetTicks(); -} - -uint64_t GetFrequency() { - return g_timer.GetFrequency(); -} - } // namespace Loader::Timer diff --git a/src/loader/timer.h b/src/loader/timer.h index f42426174..30bb82186 100644 --- a/src/loader/timer.h +++ b/src/loader/timer.h @@ -9,8 +9,6 @@ namespace Loader::Timer { void Start(); double GetTimeMs(); Common::Time GetTime(); -uint64_t GetCounter(); -uint64_t GetFrequency(); struct Lifecycle { static constexpr const char* name = "Timer"; diff --git a/src/loader/x64InstructionEmulator.cpp b/src/loader/x64InstructionEmulator.cpp index 8c570c344..8f3b1c0e6 100644 --- a/src/loader/x64InstructionEmulator.cpp +++ b/src/loader/x64InstructionEmulator.cpp @@ -70,16 +70,6 @@ struct XmmWords { uint32_t w[4]; }; -static uint32_t Rol32(uint32_t value, unsigned int shift) { - shift &= 31u; - return (value << shift) | (value >> (32u - shift)); -} - -static uint32_t Rotr32(uint32_t value, unsigned int shift) { - shift &= 31u; - return (value >> shift) | (value << (32u - shift)); -} - static void Sha1Msg1(XmmWords& dest, const XmmWords& src2) { const uint32_t w0 = dest.w[3]; const uint32_t w1 = dest.w[2]; @@ -97,10 +87,10 @@ static void Sha1Msg2(XmmWords& dest, const XmmWords& src2) { const uint32_t w13 = src2.w[2]; const uint32_t w14 = src2.w[1]; const uint32_t w15 = src2.w[0]; - const uint32_t w16 = Rol32(dest.w[3] ^ w13, 1u); - const uint32_t w17 = Rol32(dest.w[2] ^ w14, 1u); - const uint32_t w18 = Rol32(dest.w[1] ^ w15, 1u); - const uint32_t w19 = Rol32(dest.w[0] ^ w16, 1u); + const uint32_t w16 = std::rotl(dest.w[3] ^ w13, 1); + const uint32_t w17 = std::rotl(dest.w[2] ^ w14, 1); + const uint32_t w18 = std::rotl(dest.w[1] ^ w15, 1); + const uint32_t w19 = std::rotl(dest.w[0] ^ w16, 1); dest.w[3] = w16; dest.w[2] = w17; dest.w[1] = w18; @@ -108,7 +98,7 @@ static void Sha1Msg2(XmmWords& dest, const XmmWords& src2) { } static void Sha1Nexte(XmmWords& dest, const XmmWords& src2) { - const uint32_t tmp = Rol32(dest.w[3], 30u); + const uint32_t tmp = std::rotl(dest.w[3], 30); dest.w[3] = src2.w[3] + tmp; dest.w[2] = src2.w[2]; dest.w[1] = src2.w[1]; @@ -145,14 +135,14 @@ static void Sha1Rnds4(XmmWords& dest, const XmmWords& src2, uint8_t imm8) { uint32_t e = 0; for (unsigned int round = 0; round < 4u; round++) { - uint32_t term = Sha1RoundFunc(group, b, c, d) + Rol32(a, 5u) + w[round] + k; + uint32_t term = Sha1RoundFunc(group, b, c, d) + std::rotl(a, 5) + w[round] + k; if (round > 0u) { term += e; } const uint32_t a1 = term; e = d; d = c; - c = Rol32(b, 30u); + c = std::rotl(b, 30); b = a; a = a1; } @@ -164,19 +154,19 @@ static void Sha1Rnds4(XmmWords& dest, const XmmWords& src2, uint8_t imm8) { } static uint32_t Sha256Sigma0(uint32_t x) { - return Rotr32(x, 7u) ^ Rotr32(x, 18u) ^ (x >> 3u); + return std::rotr(x, 7) ^ std::rotr(x, 18) ^ (x >> 3u); } static uint32_t Sha256Sigma1(uint32_t x) { - return Rotr32(x, 17u) ^ Rotr32(x, 19u) ^ (x >> 10u); + return std::rotr(x, 17) ^ std::rotr(x, 19) ^ (x >> 10u); } static uint32_t Sha256Sum0(uint32_t x) { - return Rotr32(x, 2u) ^ Rotr32(x, 13u) ^ Rotr32(x, 22u); + return std::rotr(x, 2) ^ std::rotr(x, 13) ^ std::rotr(x, 22); } static uint32_t Sha256Sum1(uint32_t x) { - return Rotr32(x, 6u) ^ Rotr32(x, 11u) ^ Rotr32(x, 25u); + return std::rotr(x, 6) ^ std::rotr(x, 11) ^ std::rotr(x, 25); } static uint32_t Sha256Ch(uint32_t e, uint32_t f, uint32_t g) {