From f0a1379e5a9ebc954e95d07c1ad5d71587adc6bc Mon Sep 17 00:00:00 2001 From: est31 Date: Fri, 17 Apr 2015 06:10:30 +0200 Subject: [PATCH] Refactor around translatePassword Change types for passed password from wstring to string, which removes converting back and forth in most cases. Move the narrow_to_wide conversion, where its neccessary, closer to irrlicht. Remove trailing spaces in guiPasswordChange.cpp. Make parameters for translatePassword passed as const reference. --- src/client.cpp | 4 ++-- src/client.h | 4 ++-- src/client/clientlauncher.cpp | 2 +- src/guiPasswordChange.cpp | 17 +++++++++-------- src/network/serverpackethandler.cpp | 8 ++++---- src/script/lua_api/l_util.cpp | 3 +-- src/util/string.cpp | 5 +++-- src/util/string.h | 3 ++- 8 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 99ef3c7e1..ceea56ba0 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1062,8 +1062,8 @@ void Client::sendChatMessage(const std::wstring &message) Send(&pkt); } -void Client::sendChangePassword(const std::wstring &oldpassword, - const std::wstring &newpassword) +void Client::sendChangePassword(const std::string &oldpassword, + const std::string &newpassword) { Player *player = m_env.getLocalPlayer(); if(player == NULL) diff --git a/src/client.h b/src/client.h index 7ec405fa6..47b3c2e00 100644 --- a/src/client.h +++ b/src/client.h @@ -407,8 +407,8 @@ public: const std::map &fields); void sendInventoryAction(InventoryAction *a); void sendChatMessage(const std::wstring &message); - void sendChangePassword(const std::wstring &oldpassword, - const std::wstring &newpassword); + void sendChangePassword(const std::string &oldpassword, + const std::string &newpassword); void sendDamage(u8 damage); void sendBreath(u16 breath); void sendRespawn(); diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index fe4dec40f..5307ccd4b 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -392,7 +392,7 @@ bool ClientLauncher::launch_game(std::string &error_message, else playername = menudata.name; - password = translatePassword(playername, narrow_to_wide(menudata.password)); + password = translatePassword(playername, menudata.password); g_settings->set("name", playername); diff --git a/src/guiPasswordChange.cpp b/src/guiPasswordChange.cpp index 0e19f571d..1c1a1f06e 100644 --- a/src/guiPasswordChange.cpp +++ b/src/guiPasswordChange.cpp @@ -79,7 +79,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize) Remove stuff */ removeChildren(); - + /* Calculate new sizes and positions */ @@ -89,7 +89,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize) screensize.X/2 + 580/2, screensize.Y/2 + 300/2 ); - + DesiredRect = rect; recalculateAbsolutePosition(false); @@ -112,7 +112,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize) { core::rect rect(0, 0, 230, 30); rect += topleft_client + v2s32(160, ypos); - gui::IGUIEditBox *e = + gui::IGUIEditBox *e = Environment->addEditBox(L"", rect, true, this, ID_oldPassword); Environment->setFocus(e); e->setPasswordBox(true); @@ -128,7 +128,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize) { core::rect rect(0, 0, 230, 30); rect += topleft_client + v2s32(160, ypos); - gui::IGUIEditBox *e = + gui::IGUIEditBox *e = Environment->addEditBox(L"", rect, true, this, ID_newPassword1); e->setPasswordBox(true); } @@ -143,7 +143,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize) { core::rect rect(0, 0, 230, 30); rect += topleft_client + v2s32(160, ypos); - gui::IGUIEditBox *e = + gui::IGUIEditBox *e = Environment->addEditBox(L"", rect, true, this, ID_newPassword2); e->setPasswordBox(true); } @@ -162,7 +162,7 @@ void GUIPasswordChange::regenerateGui(v2u32 screensize) core::rect rect(0, 0, 300, 20); rect += topleft_client + v2s32(35, ypos); text = wgettext("Passwords do not match!"); - IGUIElement *e = + IGUIElement *e = Environment->addStaticText( text, rect, false, true, this, ID_message); @@ -177,7 +177,7 @@ void GUIPasswordChange::drawMenu() if (!skin) return; video::IVideoDriver* driver = Environment->getVideoDriver(); - + video::SColor bgcolor(140,0,0,0); driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect); @@ -203,7 +203,8 @@ bool GUIPasswordChange::acceptInput() e->setVisible(true); return false; } - m_client->sendChangePassword(oldpass, newpass); + m_client->sendChangePassword(wide_to_narrow(oldpass), + wide_to_narrow(newpass)); return true; } diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 2f0e0b30c..955b1a7dc 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -267,8 +267,8 @@ void Server::handleCommand_Auth(NetworkPacket* pkt) DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_EMPTY_PASSWORD); return; } - std::wstring raw_default_password = - narrow_to_wide(g_settings->get("default_password")); + std::string raw_default_password = + g_settings->get("default_password"); std::string initial_password = translatePassword(playername, raw_default_password); @@ -571,8 +571,8 @@ void Server::handleCommand_Init_Legacy(NetworkPacket* pkt) L"disallowed. Set a password and try again."); return; } - std::wstring raw_default_password = - narrow_to_wide(g_settings->get("default_password")); + std::string raw_default_password = + g_settings->get("default_password"); std::string initial_password = translatePassword(playername, raw_default_password); diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 79a5b252a..0464f46d9 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -255,8 +255,7 @@ int ModApiUtil::l_get_password_hash(lua_State *L) NO_MAP_LOCK_REQUIRED; std::string name = luaL_checkstring(L, 1); std::string raw_password = luaL_checkstring(L, 2); - std::string hash = translatePassword(name, - narrow_to_wide(raw_password)); + std::string hash = translatePassword(name, raw_password); lua_pushstring(L, hash.c_str()); return 1; } diff --git a/src/util/string.cpp b/src/util/string.cpp index 02a0586e7..c0f0e97fd 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -181,12 +181,13 @@ std::string wide_to_narrow(const std::wstring &wcs) // their password. (Exception : if the password field is // blank, we send a blank password - this is for backwards // compatibility with password-less players). -std::string translatePassword(std::string playername, std::wstring password) +std::string translatePassword(const std::string &playername, + const std::string &password) { if (password.length() == 0) return ""; - std::string slt = playername + wide_to_narrow(password); + std::string slt = playername + password; SHA1 sha1; sha1.addBytes(slt.c_str(), slt.length()); unsigned char *digest = sha1.getDigest(); diff --git a/src/util/string.h b/src/util/string.h index dc520e3a8..d4bbafd9f 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -43,7 +43,8 @@ wchar_t *narrow_to_wide_c(const char *str); std::wstring narrow_to_wide(const std::string &mbs); std::string wide_to_narrow(const std::wstring &wcs); -std::string translatePassword(std::string playername, std::wstring password); +std::string translatePassword(const std::string &playername, + const std::string &password); std::string urlencode(std::string str); std::string urldecode(std::string str); u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);