diff --git a/src/game/AccountMgr.cpp b/src/game/AccountMgr.cpp index 234bc6074..7476f2a58 100644 --- a/src/game/AccountMgr.cpp +++ b/src/game/AccountMgr.cpp @@ -288,15 +288,16 @@ bool AccountMgr::CheckPassword(uint32 accid, std::string passwd, std::string use bool AccountMgr::normalizeString(std::string& utf8str) { - wchar_t wstr_buf[MAX_ACCOUNT_STR + 1]; - - size_t wstr_len = MAX_ACCOUNT_STR; - if (!Utf8toWStr(utf8str, wstr_buf, wstr_len)) + std::wstring wstr_buf; + if (!Utf8toWStr(utf8str, wstr_buf)) return false; - std::transform(&wstr_buf[0], wstr_buf + wstr_len, &wstr_buf[0], wcharToUpperOnlyLatin); + if (wstr_buf.size() > MAX_ACCOUNT_STR) + return false; - return WStrToUtf8(wstr_buf, wstr_len, utf8str); + std::transform(wstr_buf.begin(), wstr_buf.end(), wstr_buf.begin(), wcharToUpperOnlyLatin); + + return WStrToUtf8(wstr_buf, utf8str); } std::string AccountMgr::CalculateShaPassHash(std::string& name, std::string& password) diff --git a/src/game/Chat/Channel.cpp b/src/game/Chat/Channel.cpp index eeef534a2..0addd1f5a 100644 --- a/src/game/Chat/Channel.cpp +++ b/src/game/Chat/Channel.cpp @@ -55,7 +55,12 @@ Channel::Channel(const std::string& name) } else // it's custom channel { - normalizePlayerName(m_name); + if (!normalizePlayerName(m_name, (size_t)128)) + { + m_name = "INVALIDCHANNEL"; + m_announce = false; + } + if (m_name == u8"World") { m_flags |= CHANNEL_FLAG_GENERAL; diff --git a/src/game/Chat/Chat.cpp b/src/game/Chat/Chat.cpp index 228010b77..178c173a4 100644 --- a/src/game/Chat/Chat.cpp +++ b/src/game/Chat/Chat.cpp @@ -269,6 +269,7 @@ ChatCommand * ChatHandler::getCommandTable() { NODE, "movemotion", SEC_DEVELOPPER, false, &ChatHandler::HandleDebugMoveCommand, "", nullptr }, { NODE, "factionchange_items", SEC_ADMINISTRATOR, true, &ChatHandler::HandleFactionChangeItemsCommand, "", nullptr }, { NODE, "loottable", SEC_DEVELOPPER, true, &ChatHandler::HandleDebugLootTableCommand, "", nullptr }, + { NODE, "utf8overflow", SEC_ADMINISTRATOR, true, &ChatHandler::HandleDebugOverflowCommand, "", nullptr }, { MSTR, nullptr, 0, false, nullptr, "", nullptr } }; diff --git a/src/game/Chat/Chat.h b/src/game/Chat/Chat.h index 72558c78e..75934d9ac 100644 --- a/src/game/Chat/Chat.h +++ b/src/game/Chat/Chat.h @@ -423,6 +423,7 @@ class MANGOS_DLL_SPEC ChatHandler bool HandleDebugSpellCoefsCommand(char* args); bool HandleDebugSpellModsCommand(char* args); bool HandleDebugUpdateWorldStateCommand(char* args); + bool HandleDebugOverflowCommand(char* args); bool HandleDebugPlayCinematicCommand(char* args); bool HandleDebugPlaySoundCommand(char* args); diff --git a/src/game/Commands/Level3.cpp b/src/game/Commands/Level3.cpp index 3b4e66289..d1bbf528b 100644 --- a/src/game/Commands/Level3.cpp +++ b/src/game/Commands/Level3.cpp @@ -8408,3 +8408,14 @@ bool ChatHandler::HandleDebugShowNearestGOInfo(char* args) return true; } + +bool ChatHandler::HandleDebugOverflowCommand(char* args) +{ + std::string name("\360\222\214\245\360\222\221\243\360\222\221\251\360\223\213\215\360\223\213\210\360\223\211\241\360\222\214\245\360\222\221\243\360\222\221\251\360\223\213\215\360\223\213\210\360\223\211\241"); + // Overflow: \xd808\xdf25\xd809\xdc63\xd809\xdc69\xd80c\xdecd\xd80c\xdec8\xd80c\xde61\000\xdf25\xd809\xdc63 + + normalizePlayerName(name); + + return true; +} + diff --git a/src/game/ObjectMgr.cpp b/src/game/ObjectMgr.cpp index 8a5e47dad..b175fd0a0 100644 --- a/src/game/ObjectMgr.cpp +++ b/src/game/ObjectMgr.cpp @@ -57,22 +57,26 @@ INSTANTIATE_SINGLETON_1(ObjectMgr); -bool normalizePlayerName(std::string& name) +#include "utf8cpp/utf8.h" + +bool normalizePlayerName(std::string& name, size_t max_len) { if (name.empty()) return false; - wchar_t wstr_buf[MAX_INTERNAL_PLAYER_NAME + 1]; - size_t wstr_len = MAX_INTERNAL_PLAYER_NAME; + std::wstring wstr_buf; + if (!Utf8toWStr(name, wstr_buf)) + return false; - if (!Utf8toWStr(name, &wstr_buf[0], wstr_len)) + size_t len = wstr_buf.size(); + if (len > max_len) return false; wstr_buf[0] = wcharToUpper(wstr_buf[0]); - for (size_t i = 1; i < wstr_len; ++i) + for (size_t i = 1; i < len; ++i) wstr_buf[i] = wcharToLower(wstr_buf[i]); - if (!WStrToUtf8(wstr_buf, wstr_len, name)) + if (!WStrToUtf8(wstr_buf, name)) return false; return true; diff --git a/src/game/ObjectMgr.h b/src/game/ObjectMgr.h index f446ea1fe..f2c5caa43 100644 --- a/src/game/ObjectMgr.h +++ b/src/game/ObjectMgr.h @@ -406,7 +406,7 @@ SkillRangeType GetSkillRangeType(SkillLineEntry const *pSkill, bool racial); #define MAX_PET_NAME 12 // max allowed by client name length #define MAX_CHARTER_NAME 24 // max allowed by client name length -bool normalizePlayerName(std::string& name); +bool normalizePlayerName(std::string& name, size_t max_len = MAX_INTERNAL_PLAYER_NAME); struct MANGOS_DLL_SPEC LanguageDesc { diff --git a/src/mangosd/CliRunnable.cpp b/src/mangosd/CliRunnable.cpp index 4c55bf516..451c460b3 100644 --- a/src/mangosd/CliRunnable.cpp +++ b/src/mangosd/CliRunnable.cpp @@ -43,14 +43,21 @@ void utf8print(void* arg, const char* str) { #if PLATFORM == PLATFORM_WINDOWS - wchar_t wtemp_buf[6000]; - size_t wtemp_len = 6000-1; - if(!Utf8toWStr(str,strlen(str),wtemp_buf,wtemp_len)) + std::wstring wtemp_buf; + std::string temp_buf(str); + + if (!Utf8toWStr(temp_buf, wtemp_buf, 6000)) return; - char temp_buf[6000]; - CharToOemBuffW(&wtemp_buf[0],&temp_buf[0],wtemp_len+1); - printf("%s", temp_buf); + // Guarantee null termination + if (!temp_buf.empty()) + { + wtemp_buf.push_back('\0'); + temp_buf.resize(wtemp_buf.size()); + CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_buf.size()); +} + + printf("%s", temp_buf.c_str()); #else printf("%s", str); #endif diff --git a/src/shared/Util.cpp b/src/shared/Util.cpp index a6015b79f..3747fea75 100644 --- a/src/shared/Util.cpp +++ b/src/shared/Util.cpp @@ -367,66 +367,35 @@ size_t utf8length(std::string& utf8str) } } -void utf8truncate(std::string& utf8str,size_t len) +bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr, size_t max_len) { + if (utf8str.empty()) + { + wstr = std::wstring(); + return true; + } + try { - size_t wlen = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size()); - if(wlen <= len) - return; + // A UTF8 string can have a maximum of 4 octets per character + // A 4 octet char can take up to two UTF16 characters (4*8 = 32 / 16 = 2) + // The UTF8 string may also actually be ASCII, in which case no truncation + // takes place! The final string length is therefore unknown. Reserve + // as long as the OG string, and back-insert + wstr.resize(utf8str.size()); - std::wstring wstr; - wstr.resize(wlen); - utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]); - wstr.resize(len); - char* oend = utf8::utf16to8(wstr.c_str(),wstr.c_str()+wstr.size(),&utf8str[0]); - utf8str.resize(oend-(&utf8str[0])); // remove unused tail - } - catch(std::exception) - { - utf8str = ""; - } -} + auto end = utf8::utf8to16(utf8str.cbegin(), utf8str.cend(), wstr.begin()); -bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize) -{ - try - { - size_t len = utf8::distance(utf8str,utf8str+csize); - if(len > wsize) + if (end != wstr.end()) + wstr.erase(end, wstr.end()); + + // truncate to max len + if (!!max_len && wstr.size() > max_len) { - if(wsize > 0) - wstr[0] = L'\0'; - wsize = 0; - return false; + wstr.resize(max_len); } - - wsize = len; - utf8::utf8to16(utf8str,utf8str+csize,wstr); - wstr[len] = L'\0'; } - catch(std::exception) - { - if(wsize > 0) - wstr[0] = L'\0'; - wsize = 0; - return false; - } - - return true; -} - -bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr) -{ - try - { - size_t len = utf8::distance(utf8str.c_str(),utf8str.c_str()+utf8str.size()); - wstr.resize(len); - - if (len) - utf8::utf8to16(utf8str.c_str(),utf8str.c_str()+utf8str.size(),&wstr[0]); - } - catch(std::exception) + catch (std::exception) { wstr = L""; return false; @@ -435,38 +404,20 @@ bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr) return true; } -bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str) +bool WStrToUtf8(std::wstring& wstr, std::string& utf8str) { try { std::string utf8str2; - utf8str2.resize(size*4); // allocate for most long case + utf8str2.resize(wstr.size() * 4); // allocate for most long case + + auto end = utf8::utf16to8(wstr.cbegin(), wstr.cend(), utf8str2.begin()); + if (end != utf8str2.end()) + utf8str2.erase(end, utf8str2.end()); - char* oend = utf8::utf16to8(wstr,wstr+size,&utf8str2[0]); - utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail utf8str = utf8str2; } - catch(std::exception) - { - utf8str = ""; - return false; - } - - return true; -} - -bool WStrToUtf8(std::wstring wstr, std::string& utf8str) -{ - try - { - std::string utf8str2; - utf8str2.resize(wstr.size()*4); // allocate for most long case - - char* oend = utf8::utf16to8(wstr.c_str(),wstr.c_str()+wstr.size(),&utf8str2[0]); - utf8str2.resize(oend-(&utf8str2[0])); // remove unused tail - utf8str = utf8str2; - } - catch(std::exception) + catch (std::exception) { utf8str = ""; return false; @@ -481,11 +432,11 @@ bool utf8ToConsole(const std::string& utf8str, std::string& conStr) { #if PLATFORM == PLATFORM_WINDOWS std::wstring wstr; - if(!Utf8toWStr(utf8str,wstr)) + if (!Utf8toWStr(utf8str, wstr)) return false; conStr.resize(wstr.size()); - CharToOemBuffW(&wstr[0],&conStr[0],wstr.size()); + CharToOemBuffW(&wstr[0], &conStr[0], wstr.size()); #else // not implemented yet conStr = utf8str; @@ -494,14 +445,14 @@ bool utf8ToConsole(const std::string& utf8str, std::string& conStr) return true; } -bool consoleToUtf8(const std::string& conStr,std::string& utf8str) +bool consoleToUtf8(const std::string& conStr, std::string& utf8str) { #if PLATFORM == PLATFORM_WINDOWS std::wstring wstr; wstr.resize(conStr.size()); - OemToCharBuffW(&conStr[0],&wstr[0],conStr.size()); + OemToCharBuffW(&conStr[0], &wstr[0], conStr.size()); - return WStrToUtf8(wstr,utf8str); + return WStrToUtf8(wstr, utf8str); #else // not implemented yet utf8str = conStr; @@ -513,13 +464,13 @@ bool Utf8FitTo(const std::string& str, std::wstring search) { std::wstring temp; - if(!Utf8toWStr(str,temp)) + if (!Utf8toWStr(str, temp)) return false; // converting to lower case - wstrToLower( temp ); + wstrToLower(temp); - if(temp.find(search) == std::wstring::npos) + if (temp.find(search) == std::wstring::npos) return false; return true; @@ -536,16 +487,21 @@ void utf8printf(FILE *out, const char *str, ...) void vutf8printf(FILE *out, const char *str, va_list* ap) { #if PLATFORM == PLATFORM_WINDOWS - char temp_buf[32*1024]; - wchar_t wtemp_buf[32*1024]; + std::string temp_buf; + temp_buf.resize(32 * 1024); + std::wstring wtemp_buf; - size_t temp_len = vsnprintf(temp_buf, 32*1024, str, *ap); + size_t temp_len = vsnprintf(&temp_buf[0], 32 * 1024, str, *ap); + temp_buf.resize(strlen(temp_buf.c_str())); // Resize to match the formatted string - size_t wtemp_len = 32*1024-1; - Utf8toWStr(temp_buf, temp_len, wtemp_buf, wtemp_len); + if (!temp_buf.empty()) + { + Utf8toWStr(temp_buf, wtemp_buf, 32 * 1024); + wtemp_buf.push_back('\0'); - CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len+1); - fprintf(out, "%s", temp_buf); + CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_buf.size()); + } + fprintf(out, "%s", temp_buf.c_str()); #else vfprintf(out, str, *ap); #endif @@ -554,16 +510,16 @@ void vutf8printf(FILE *out, const char *str, va_list* ap) void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result) { std::ostringstream ss; - for(uint32 i=0; i>((1-j)*4)); + unsigned char nibble = 0x0F & (bytes[i] >> ((1 - j) * 4)); char encodedNibble; - if(nibble < 0x0A) - encodedNibble = '0'+nibble; + if (nibble < 0x0A) + encodedNibble = '0' + nibble; else - encodedNibble = 'A'+nibble-0x0A; + encodedNibble = 'A' + nibble - 0x0A; ss << encodedNibble; } } diff --git a/src/shared/Util.h b/src/shared/Util.h index 863a37197..05f58ed34 100644 --- a/src/shared/Util.h +++ b/src/shared/Util.h @@ -140,17 +140,10 @@ inline void ApplyPercentModFloatVar(float& var, float val, bool apply) var *= (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val)); } -bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr); +bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr, size_t max_len = 0); // in wsize==max size of buffer, out wsize==real string size -bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize); -inline bool Utf8toWStr(const std::string& utf8str, wchar_t* wstr, size_t& wsize) -{ - return Utf8toWStr(utf8str.c_str(), utf8str.size(), wstr, wsize); -} -bool WStrToUtf8(std::wstring wstr, std::string& utf8str); -// size==real string size -bool WStrToUtf8(wchar_t* wstr, size_t size, std::string& utf8str); +bool WStrToUtf8(std::wstring& wstr, std::string& utf8str); size_t utf8length(std::string& utf8str); // set string to "" if invalid utf8 sequence void utf8truncate(std::string& utf8str,size_t len);