Properly normalize string to UTF-8

This commit is contained in:
Meltie2013 2022-06-30 08:33:46 +01:00 committed by billy1arm
parent f9a203e0da
commit fd0175b981
No known key found for this signature in database
GPG Key ID: 0DF907270598C85F
8 changed files with 37 additions and 42 deletions

View File

@ -27,6 +27,7 @@
#include "World.h" #include "World.h"
#include "AccountMgr.h" #include "AccountMgr.h"
#include "Util.h" #include "Util.h"
#include "ObjectMgr.h"
/********************************************************************** /**********************************************************************
CommandTable : banCommandTable CommandTable : banCommandTable
@ -150,7 +151,7 @@ bool ChatHandler::HandleBanHelper(BanMode mode, char* args)
switch (mode) switch (mode)
{ {
case BAN_ACCOUNT: case BAN_ACCOUNT:
if (!AccountMgr::normalizeString(nameOrIP)) if (!Utf8ToUpperOnlyLatin(nameOrIP))
{ {
PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, nameOrIP.c_str()); PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, nameOrIP.c_str());
SetSentErrorMessage(true); SetSentErrorMessage(true);
@ -487,7 +488,7 @@ bool ChatHandler::HandleUnBanHelper(BanMode mode, char* args)
switch (mode) switch (mode)
{ {
case BAN_ACCOUNT: case BAN_ACCOUNT:
if (!AccountMgr::normalizeString(nameOrIP)) if (!Utf8ToUpperOnlyLatin(nameOrIP))
{ {
PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, nameOrIP.c_str()); PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, nameOrIP.c_str());
SetSentErrorMessage(true); SetSentErrorMessage(true);

View File

@ -29,6 +29,7 @@
#include "World.h" #include "World.h"
#include "ObjectMgr.h" #include "ObjectMgr.h"
#include "SQLStorages.h" #include "SQLStorages.h"
#include "Util.h"
/********************************************************************** /**********************************************************************
CommandTable : lookupCommandTable CommandTable : lookupCommandTable
@ -503,7 +504,7 @@ bool ChatHandler::HandleLookupAccountNameCommand(char* args)
} }
std::string account = accountStr; std::string account = accountStr;
if (!AccountMgr::normalizeString(account)) if (!Utf8ToUpperOnlyLatin(account))
{ {
return false; return false;
} }
@ -610,7 +611,7 @@ bool ChatHandler::HandleLookupPlayerAccountCommand(char* args)
} }
std::string account = accountStr; std::string account = accountStr;
if (!AccountMgr::normalizeString(account)) if (!Utf8ToUpperOnlyLatin(account))
{ {
return false; return false;
} }

View File

@ -67,8 +67,8 @@ AccountOpResult AccountMgr::CreateAccount(std::string username, std::string pass
return AOR_PASS_TOO_LONG; // password too long return AOR_PASS_TOO_LONG; // password too long
} }
normalizeString(username); Utf8ToUpperOnlyLatin(username);
normalizeString(password); Utf8ToUpperOnlyLatin(password);
if (GetId(username)) if (GetId(username))
{ {
@ -168,8 +168,8 @@ AccountOpResult AccountMgr::ChangeUsername(uint32 accid, std::string new_uname,
return AOR_PASS_TOO_LONG; return AOR_PASS_TOO_LONG;
} }
normalizeString(new_uname); Utf8ToUpperOnlyLatin(new_uname);
normalizeString(new_passwd); Utf8ToUpperOnlyLatin(new_passwd);
std::string safe_new_uname = new_uname; std::string safe_new_uname = new_uname;
LoginDatabase.escape_string(safe_new_uname); LoginDatabase.escape_string(safe_new_uname);
@ -205,8 +205,8 @@ AccountOpResult AccountMgr::ChangePassword(uint32 accid, std::string new_passwd)
return AOR_PASS_TOO_LONG; return AOR_PASS_TOO_LONG;
} }
normalizeString(username); Utf8ToUpperOnlyLatin(username);
normalizeString(new_passwd); Utf8ToUpperOnlyLatin(new_passwd);
// also reset s and v to force update at next realmd login // also reset s and v to force update at next realmd login
if (!LoginDatabase.PExecute("UPDATE `account` SET `v`='0', `s`='0', `sha_pass_hash`='%s' WHERE `id`='%u'", if (!LoginDatabase.PExecute("UPDATE `account` SET `v`='0', `s`='0', `sha_pass_hash`='%s' WHERE `id`='%u'",
@ -322,8 +322,8 @@ bool AccountMgr::CheckPassword(uint32 accid, std::string passwd)
return false; return false;
} }
normalizeString(passwd); Utf8ToUpperOnlyLatin(passwd);
normalizeString(username); Utf8ToUpperOnlyLatin(username);
QueryResult* result = LoginDatabase.PQuery("SELECT 1 FROM `account` WHERE `id`='%u' AND `sha_pass_hash`='%s'", accid, CalculateShaPassHash(username, passwd).c_str()); QueryResult* result = LoginDatabase.PQuery("SELECT 1 FROM `account` WHERE `id`='%u' AND `sha_pass_hash`='%s'", accid, CalculateShaPassHash(username, passwd).c_str());
if (result) if (result)
@ -335,31 +335,6 @@ bool AccountMgr::CheckPassword(uint32 accid, std::string passwd)
return false; return false;
} }
/**
* It converts a string to uppercase, but only if it's a latin character
*
* @param utf8str The string to be normalized.
*
* @return A boolean value.
*/
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))
{
return false;
}
for (uint32 i = 0; i <= wstr_len; ++i)
{
wstr_buf[i] = wcharToUpperOnlyLatin(wstr_buf[i]);
}
return WStrToUtf8(wstr_buf, wstr_len, utf8str);
}
/** /**
* It takes a username and password, concatenates them with a colon, and then hashes the result with * It takes a username and password, concatenates them with a colon, and then hashes the result with
* SHA1 * SHA1

View File

@ -59,11 +59,8 @@ class AccountMgr
bool GetName(uint32 acc_id, std::string& name); bool GetName(uint32 acc_id, std::string& name);
uint32 GetCharactersCount(uint32 acc_id); uint32 GetCharactersCount(uint32 acc_id);
std::string CalculateShaPassHash(std::string& name, std::string& password); std::string CalculateShaPassHash(std::string& name, std::string& password);
static bool normalizeString(std::string& utf8str);
}; };
/* A macro that creates a global variable called `sAccountMgr` that is an instance of the `AccountMgr` class. */ /* A macro that creates a global variable called `sAccountMgr` that is an instance of the `AccountMgr` class. */
#define sAccountMgr MaNGOS::Singleton<AccountMgr>::Instance() #define sAccountMgr MaNGOS::Singleton<AccountMgr>::Instance()
#endif #endif

View File

@ -3570,7 +3570,7 @@ uint32 ChatHandler::ExtractAccountId(char** args, std::string* accountName /*= N
else else
{ {
account_name = account_str; account_name = account_str;
if (!AccountMgr::normalizeString(account_name)) if (!Utf8ToUpperOnlyLatin(account_name))
{ {
PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, account_name.c_str()); PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, account_name.c_str());
SetSentErrorMessage(true); SetSentErrorMessage(true);

@ -1 +1 @@
Subproject commit c7b51c226f78088bbe51c129ca41c03728634b45 Subproject commit 8c648614e143948b2b2da76bef759d90542ae975

View File

@ -415,6 +415,19 @@ void utf8truncate(std::string& utf8str, size_t len)
} }
} }
bool Utf8ToUpperOnlyLatin(std::string& utf8String)
{
std::wstring wstr;
if (!Utf8toWStr(utf8String, wstr))
{
return false;
}
std::transform(wstr.begin(), wstr.end(), wstr.begin(), wcharToUpperOnlyLatin);
return WStrToUtf8(wstr, utf8String);
}
size_t utf8limit(std::string& utf8str, size_t bytes) size_t utf8limit(std::string& utf8str, size_t bytes)
{ {
if (utf8str.size() > bytes) if (utf8str.size() > bytes)

View File

@ -288,6 +288,14 @@ inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
var *= (apply ? (100.0f + val) / 100.0f : 100.0f / (100.0f + val)); var *= (apply ? (100.0f + val) / 100.0f : 100.0f / (100.0f + val));
} }
/**
* @brief
*
* @param utf8String
* @return bool
*/
bool Utf8ToUpperOnlyLatin(std::string& utf8String);
/** /**
* @brief * @brief
* *