diff --git a/Source/Network/TextEncoding.cpp b/Source/Network/TextEncoding.cpp index 865340716..f0976e143 100644 --- a/Source/Network/TextEncoding.cpp +++ b/Source/Network/TextEncoding.cpp @@ -67,80 +67,32 @@ std::string FromUtf8(std::string text) { // OFFSET: 0x00AF87B0 // FUNCTION: fn_18264 std::string FromUtf8(const std::string& text, UINT codePage) { - int wideLength = MultiByteToWideChar(CP_UTF8, 0, text.data(), static_cast(text.size()), nullptr, 0); - if (!wideLength) { - return {}; + std::wstring wide = DecodeText(CP_UTF8, text); + if (wide.empty()) { + return ""; } - BSTR wide = SysAllocStringLen(nullptr, wideLength); - if (!wide) { - return {}; + std::string result = EncodeText(codePage, wide); + if (result.empty()) { + return ""; } - if (!MultiByteToWideChar(CP_UTF8, 0, text.data(), static_cast(text.size()), wide, wideLength)) { - SysFreeString(wide); - return {}; - } - - int length = WideCharToMultiByte(codePage, 0, wide, -1, nullptr, 0, nullptr, nullptr); - if (!length) { - SysFreeString(wide); - return {}; - } - - char* converted = static_cast(std::malloc(length)); - if (!converted) { - SysFreeString(wide); - return {}; - } - - std::string result; - if (WideCharToMultiByte(codePage, 0, wide, -1, converted, length, nullptr, nullptr)) { - result.assign(converted); - } - - std::free(converted); - SysFreeString(wide); return result; } // OFFSET: 0x00AF4050 // FUNCTION: fn_18243 std::string ToUtf8(const std::string& text, UINT codePage) { - int wideLength = MultiByteToWideChar(codePage, 0, text.data(), static_cast(text.size()), nullptr, 0); - if (!wideLength) { - return {}; + std::wstring wide = DecodeText(codePage, text); + if (wide.empty()) { + return ""; } - BSTR wide = SysAllocStringLen(nullptr, wideLength); - if (!wide) { - return {}; + std::string result = EncodeText(CP_UTF8, wide); + if (result.empty()) { + return ""; } - if (!MultiByteToWideChar(codePage, 0, text.data(), static_cast(text.size()), wide, wideLength)) { - SysFreeString(wide); - return {}; - } - - int length = WideCharToMultiByte(CP_UTF8, 0, wide, -1, nullptr, 0, nullptr, nullptr); - if (!length) { - SysFreeString(wide); - return {}; - } - - char* converted = static_cast(std::malloc(length)); - if (!converted) { - SysFreeString(wide); - return {}; - } - - std::string result; - if (WideCharToMultiByte(CP_UTF8, 0, wide, -1, converted, length, nullptr, nullptr)) { - result.assign(converted); - } - - std::free(converted); - SysFreeString(wide); return result; } @@ -148,10 +100,12 @@ std::string ToUtf8(const std::string& text, UINT codePage) { // FUNCTION: fn_18257 void LowercaseText(std::string& text) { for (unsigned index = 0; index < text.size(); ++index) { - if (static_cast(text[index]) <= 0) { + if (static_cast(text[index]) > 0) { + if (std::isupper(text[index]) != 0) { + text[index] += 32; + } + } else { ++index; - } else if (std::isupper(text[index]) != 0) { - text[index] += 32; } } } @@ -161,15 +115,15 @@ void LowercaseText(std::string& text) { 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 {}; + return L""; } BSTR converted = SysAllocStringLen(nullptr, length); if (converted == nullptr) { - return {}; + return L""; } if (MultiByteToWideChar(codePage, 0, text.data(), static_cast(text.size()), converted, length) == 0) { SysFreeString(converted); - return {}; + return L""; } std::wstring result(converted); SysFreeString(converted);