diff --git a/Source/Network/TextEncoding.cpp b/Source/Network/TextEncoding.cpp index e551549..08dc9d4 100644 --- a/Source/Network/TextEncoding.cpp +++ b/Source/Network/TextEncoding.cpp @@ -1,6 +1,8 @@ #include "Network/TextEncoding.h" #include "Utility/StackAlloc.h" +#include + #include #include #include @@ -138,3 +140,52 @@ std::string ToUtf8(const std::string& text, UINT codePage) { SysFreeString(wide); return result; } + +// FUNCTION: fn_18257 +void LowercaseText(std::string& text) { + for (unsigned index = 0; index < text.size(); ++index) { + if (static_cast(text[index]) <= 0) { + ++index; + } else if (std::isupper(text[index]) != 0) { + text[index] += 32; + } + } +} + +// FUNCTION: fn_18258 +std::wstring DecodeText(UINT codePage, const std::string& text) { + const int length = MultiByteToWideChar(codePage, 0, text.data(), static_cast(text.size()), nullptr, 0); + if (length == 0) { + return {}; + } + BSTR converted = SysAllocStringLen(nullptr, length); + if (converted == nullptr) { + return {}; + } + if (MultiByteToWideChar(codePage, 0, text.data(), static_cast(text.size()), converted, length) == 0) { + SysFreeString(converted); + return {}; + } + std::wstring result(converted); + SysFreeString(converted); + return result; +} + +// FUNCTION: fn_18265 +std::string EncodeText(UINT codePage, const std::wstring& text) { + const int length = WideCharToMultiByte(codePage, 0, text.c_str(), -1, nullptr, 0, nullptr, nullptr); + if (length == 0) { + return {}; + } + char* converted = static_cast(std::malloc(length)); + if (converted == nullptr) { + return {}; + } + if (WideCharToMultiByte(codePage, 0, text.c_str(), -1, converted, length, nullptr, nullptr) == 0) { + std::free(converted); + return {}; + } + std::string result(converted, std::strlen(converted)); + std::free(converted); + return result; +} diff --git a/Source/Network/TextEncoding.h b/Source/Network/TextEncoding.h index 2e1e750..5889221 100644 --- a/Source/Network/TextEncoding.h +++ b/Source/Network/TextEncoding.h @@ -12,4 +12,8 @@ std::string FromUtf8(std::string text); std::string FromUtf8(const std::string& text, UINT codePage); std::string ToUtf8(const std::string& text, UINT codePage); +void LowercaseText(std::string& text); +std::wstring DecodeText(UINT codePage, const std::string& text); +std::string EncodeText(UINT codePage, const std::wstring& text); + #endif