From bc9f7bd5da4d0be5bf7d17fc62708557d79b4ad7 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Tue, 13 Feb 2024 01:24:38 +0100 Subject: [PATCH 1/8] Add new implementation --- Shared/mods/deathmatch/logic/Enums.cpp | 2 + .../logic/luadefs/CLuaCryptDefs.cpp | 316 +++++++++++++++++- Shared/sdk/SharedUtil.Crypto.h | 61 +++- Shared/sdk/SharedUtil.Hash.h | 2 + 4 files changed, 368 insertions(+), 13 deletions(-) diff --git a/Shared/mods/deathmatch/logic/Enums.cpp b/Shared/mods/deathmatch/logic/Enums.cpp index f81cc6a364..d08eee1450 100644 --- a/Shared/mods/deathmatch/logic/Enums.cpp +++ b/Shared/mods/deathmatch/logic/Enums.cpp @@ -67,6 +67,8 @@ IMPLEMENT_ENUM_CLASS_BEGIN(StringEncodeFunction) ADD_ENUM(StringEncodeFunction::TEA, "tea") ADD_ENUM(StringEncodeFunction::AES128, "aes128") ADD_ENUM(StringEncodeFunction::RSA, "rsa") +ADD_ENUM(StringEncodeFunction::BASE64, "base64") +ADD_ENUM(StringEncodeFunction::BASE32, "base32") IMPLEMENT_ENUM_CLASS_END("string-encode-function") IMPLEMENT_ENUM_CLASS_BEGIN(KeyPairAlgorithm) diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp index 29d5689bb5..f78f727b2c 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp @@ -31,7 +31,7 @@ void CLuaCryptDefs::LoadFunctions() {"generateKeyPair", ArgumentParser}, {"passwordVerify", PasswordVerify}, {"encodeString", EncodeString}, - {"decodeString", DecodeString}, + {"decodeString", DecodeString} }; // Add functions @@ -402,10 +402,17 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM) CScriptArgReader argStream(luaVM); argStream.ReadEnumString(algorithm); argStream.ReadString(data); - argStream.ReadStringMap(options); - argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); - argStream.ReadFunctionComplete(); + if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable()) + { + argStream.ReadStringMap(options); + } + + if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsFunction()) + { + argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); + argStream.ReadFunctionComplete(); + } if (!argStream.HasErrors()) { @@ -589,6 +596,150 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM) } return 1; } + case StringEncodeFunction::BASE64: + { + SString& variant = options["variant"]; + + if (!variant.empty()) + { + variant = variant.ToUpper(); + + if (variant != "URL") + { + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; + } + } + + // Async + if (VERIFY_FUNCTION(luaFunctionRef)) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (pLuaMain) + { + CLuaShared::GetAsyncTaskScheduler()->PushTask( + [data, variant] + { + try + { + return std::make_pair(SharedUtil::Base64encode(data, variant), true); + } + catch (const CryptoPP::Exception& ex) + { + return std::make_pair(SString(ex.GetWhat()), false); + } + }, + [luaFunctionRef](const std::pair& result) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM()); + if (pLuaMain) + { + CLuaArguments arguments; + if (result.second) + { + arguments.PushString(result.first); + arguments.Call(pLuaMain, luaFunctionRef); + } + else + { + m_pScriptDebugging->LogWarning(luaFunctionRef.GetLuaVM(), result.first.c_str()); + arguments.PushBoolean(false); + arguments.Call(pLuaMain, luaFunctionRef); + } + } + }); + + lua::Push(luaVM, true); + } + } + else // Sync + { + try + { + lua::Push(luaVM, SharedUtil::Base64encode(data, variant)); + } + catch (const CryptoPP::Exception& ex) + { + m_pScriptDebugging->LogWarning(luaVM, ex.what()); + lua::Push(luaVM, false); + } + return 1; + } + return 1; + } + case StringEncodeFunction::BASE32: + { + SString& variant = options["variant"]; + + if (!variant.empty()) + { + variant = variant.ToUpper(); + + if (variant != "HEX") + { + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; + } + } + + // Async + if (VERIFY_FUNCTION(luaFunctionRef)) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (pLuaMain) + { + CLuaShared::GetAsyncTaskScheduler()->PushTask( + [data, variant] + { + try + { + return std::make_pair(SharedUtil::Base32encode(data, variant), true); + } + catch (const CryptoPP::Exception& ex) + { + return std::make_pair(SString(ex.GetWhat()), false); + } + }, + [luaFunctionRef](const std::pair& result) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM()); + if (pLuaMain) + { + CLuaArguments arguments; + if (result.second) + { + arguments.PushString(result.first); + arguments.Call(pLuaMain, luaFunctionRef); + } + else + { + m_pScriptDebugging->LogWarning(luaFunctionRef.GetLuaVM(), result.first.c_str()); + arguments.PushBoolean(false); + arguments.Call(pLuaMain, luaFunctionRef); + } + } + }); + + lua::Push(luaVM, true); + } + } + else // Sync + { + try + { + lua::Push(luaVM, SharedUtil::Base32encode(data, variant)); + } + catch (const CryptoPP::Exception& ex) + { + m_pScriptDebugging->LogWarning(luaVM, ex.what()); + lua::Push(luaVM, false); + } + return 1; + } + return 1; + } default: { m_pScriptDebugging->LogCustom(luaVM, "Unknown encryption algorithm"); @@ -614,10 +765,17 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM) CScriptArgReader argStream(luaVM); argStream.ReadEnumString(algorithm); argStream.ReadString(data); - argStream.ReadStringMap(options); - argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); - argStream.ReadFunctionComplete(); + if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable()) + { + argStream.ReadStringMap(options); + } + + if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsFunction()) + { + argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); + argStream.ReadFunctionComplete(); + } if (!argStream.HasErrors()) { @@ -808,6 +966,150 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM) } return 1; } + case StringEncodeFunction::BASE64: + { + SString& variant = options["variant"]; + + if (!variant.empty()) + { + variant = variant.ToUpper(); + + if (variant != "URL") + { + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; + } + } + + // Async + if (VERIFY_FUNCTION(luaFunctionRef)) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (pLuaMain) + { + CLuaShared::GetAsyncTaskScheduler()->PushTask( + [data, variant] + { + try + { + return std::make_pair(SharedUtil::Base64decode(data, variant), true); + } + catch (const CryptoPP::Exception& ex) + { + return std::make_pair(SString(ex.GetWhat()), false); + } + }, + [luaFunctionRef](const std::pair& result) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM()); + if (pLuaMain) + { + CLuaArguments arguments; + if (result.second) + { + arguments.PushString(result.first); + arguments.Call(pLuaMain, luaFunctionRef); + } + else + { + m_pScriptDebugging->LogWarning(luaFunctionRef.GetLuaVM(), result.first.c_str()); + arguments.PushBoolean(false); + arguments.Call(pLuaMain, luaFunctionRef); + } + } + }); + + lua::Push(luaVM, true); + } + } + else // Sync + { + try + { + lua::Push(luaVM, SharedUtil::Base64decode(data, variant)); + } + catch (const CryptoPP::Exception& ex) + { + m_pScriptDebugging->LogWarning(luaVM, ex.what()); + lua::Push(luaVM, false); + } + return 1; + } + return 1; + } + case StringEncodeFunction::BASE32: + { + SString& variant = options["variant"]; + + if (!variant.empty()) + { + variant = variant.ToUpper(); + + if (variant != "HEX") + { + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; + } + } + + // Async + if (VERIFY_FUNCTION(luaFunctionRef)) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (pLuaMain) + { + CLuaShared::GetAsyncTaskScheduler()->PushTask( + [data, variant] + { + try + { + return std::make_pair(SharedUtil::Base32decode(data, variant), true); + } + catch (const CryptoPP::Exception& ex) + { + return std::make_pair(SString(ex.GetWhat()), false); + } + }, + [luaFunctionRef](const std::pair& result) + { + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM()); + if (pLuaMain) + { + CLuaArguments arguments; + if (result.second) + { + arguments.PushString(result.first); + arguments.Call(pLuaMain, luaFunctionRef); + } + else + { + m_pScriptDebugging->LogWarning(luaFunctionRef.GetLuaVM(), result.first.c_str()); + arguments.PushBoolean(false); + arguments.Call(pLuaMain, luaFunctionRef); + } + } + }); + + lua::Push(luaVM, true); + } + } + else // Sync + { + try + { + lua::Push(luaVM, SharedUtil::Base32decode(data, variant)); + } + catch (const CryptoPP::Exception& ex) + { + m_pScriptDebugging->LogWarning(luaVM, ex.what()); + lua::Push(luaVM, false); + } + return 1; + } + return 1; + } default: { m_pScriptDebugging->LogCustom(luaVM, "Unknown encryption algorithm"); diff --git a/Shared/sdk/SharedUtil.Crypto.h b/Shared/sdk/SharedUtil.Crypto.h index 7d5413c94c..be8837f700 100644 --- a/Shared/sdk/SharedUtil.Crypto.h +++ b/Shared/sdk/SharedUtil.Crypto.h @@ -9,6 +9,7 @@ *****************************************************************************/ #pragma once #include +#include #include #include #include @@ -25,18 +26,66 @@ namespace SharedUtil SString publicKey, privateKey; }; - inline SString Base64encode(const SString& data) + inline SString Base64encode(const SString& data, const SString& variant = SString()) { - SString result; - CryptoPP::StringSource ss(data, true, new CryptoPP::Base64Encoder(new CryptoPP::StringSink(result), false)); // Memory is freed automatically + SString result; + + if (variant == "URL") + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base64URLEncoder(new CryptoPP::StringSink(result), false)); + } + else + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base64Encoder(new CryptoPP::StringSink(result), false)); + } + + return result; + } + + inline SString Base64decode(const SString& data, const SString& variant = SString()) + { + std::string result; + + if (variant == "URL") + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base64URLDecoder(new CryptoPP::StringSink(result))); + } + else + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(result))); + } + + return result; + } + + inline SString Base32encode(const SString& data, const SString& variant = SString()) + { + std::string result; + + if (variant == "HEX") + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base32HexEncoder(new CryptoPP::StringSink(result), false)); + } + else + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base32Encoder(new CryptoPP::StringSink(result), false)); + } return result; } - inline SString Base64decode(const SString& data) + inline SString Base32decode(const SString& data, const SString& variant = SString()) { - SString result; - CryptoPP::StringSource ss(data, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(result))); // Memory is freed automatically + std::string result; + + if (variant == "HEX") + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base32HexDecoder(new CryptoPP::StringSink(result))); + } + else + { + CryptoPP::StringSource ss(data, true, new CryptoPP::Base32Decoder(new CryptoPP::StringSink(result))); + } return result; } diff --git a/Shared/sdk/SharedUtil.Hash.h b/Shared/sdk/SharedUtil.Hash.h index 29426c569a..1aa28e82ce 100644 --- a/Shared/sdk/SharedUtil.Hash.h +++ b/Shared/sdk/SharedUtil.Hash.h @@ -48,6 +48,8 @@ enum class StringEncodeFunction TEA, AES128, RSA, + BASE64, + BASE32 }; enum class KeyPairAlgorithm From aea2c79a4dbd4a17547cfa6179616d2506b1dba8 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Tue, 13 Feb 2024 01:47:14 +0100 Subject: [PATCH 2/8] Add old functions to deprecated list --- .../deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp | 2 ++ Server/mods/deathmatch/logic/CResourceChecker.Data.h | 8 ++++++++ .../deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp index 7dea8f95bf..7a189cdb9e 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp @@ -71,6 +71,8 @@ void CLuaCompatibilityDefs::LoadFunctions() {"getControlState", CLuaPedDefs::GetPedControlState}, {"setCameraShakeLevel", ArgumentParserWarn}, {"getCameraShakeLevel", ArgumentParserWarn}, + {"base64Encode", CLuaCryptDefs::EncodeString}, + {"base64Decode", CLuaCryptDefs::DecodeString}, }; // Add functions diff --git a/Server/mods/deathmatch/logic/CResourceChecker.Data.h b/Server/mods/deathmatch/logic/CResourceChecker.Data.h index 47292bb388..99ea3ecb0b 100644 --- a/Server/mods/deathmatch/logic/CResourceChecker.Data.h +++ b/Server/mods/deathmatch/logic/CResourceChecker.Data.h @@ -146,6 +146,10 @@ namespace // Ped jetpacks //{false, "doesPedHaveJetPack", "isPedWearingJetpack"}, + + // Base Encoding & Decoding + {false, "base64Encode", "encodeString"}, + {false, "base64Decode", "decodeString"} }; SDeprecatedItem serverDeprecatedList[] = { @@ -244,5 +248,9 @@ namespace // Old Discord implementation (see #2499) {true, "setPlayerDiscordJoinParams", "See GitHub PR #2499 for more details"}, + + // Base Encoding & Decoding + {false, "base64Encode", "encodeString"}, + {false, "base64Decode", "decodeString"} }; } // namespace diff --git a/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp b/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp index 368feac3c1..0ce798bb45 100644 --- a/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp +++ b/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp @@ -13,6 +13,7 @@ #include "CLuaVehicleDefs.h" #include "CLuaPedDefs.h" #include "CLuaPlayerDefs.h" +#include "luadefs/CLuaCryptDefs.h" #include "luadefs/CLuaXMLDefs.h" #include @@ -88,6 +89,10 @@ void CLuaCompatibilityDefs::LoadFunctions() lua_pushboolean(luaVM, false); return 1; }}, + + // Base Encoding & Decoding + {"base64Encode", CLuaCryptDefs::EncodeString}, + {"base64Decode", CLuaCryptDefs::DecodeString}, }; // Add functions From a107b1b25fb6ef7b79f113998cffc6e92b1d1e69 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Wed, 14 Feb 2024 06:14:35 +0100 Subject: [PATCH 3/8] Minify statements --- .../logic/luadefs/CLuaCryptDefs.cpp | 60 +++++++------------ 1 file changed, 20 insertions(+), 40 deletions(-) diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp index f78f727b2c..6c357469b8 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp @@ -598,18 +598,13 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM) } case StringEncodeFunction::BASE64: { - SString& variant = options["variant"]; + const SString variant = options["variant"].ToUpper(); - if (!variant.empty()) + if (!variant.empty() && variant != "URL") { - variant = variant.ToUpper(); - - if (variant != "URL") - { - m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); - lua::Push(luaVM, false); - return 1; - } + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; } // Async @@ -670,18 +665,13 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM) } case StringEncodeFunction::BASE32: { - SString& variant = options["variant"]; + const SString variant = options["variant"].ToUpper(); - if (!variant.empty()) + if (!variant.empty() && variant != "HEX") { - variant = variant.ToUpper(); - - if (variant != "HEX") - { - m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); - lua::Push(luaVM, false); - return 1; - } + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; } // Async @@ -968,18 +958,13 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM) } case StringEncodeFunction::BASE64: { - SString& variant = options["variant"]; + const SString variant = options["variant"].ToUpper(); - if (!variant.empty()) + if (!variant.empty() && variant != "URL") { - variant = variant.ToUpper(); - - if (variant != "URL") - { - m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); - lua::Push(luaVM, false); - return 1; - } + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; } // Async @@ -1040,18 +1025,13 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM) } case StringEncodeFunction::BASE32: { - SString& variant = options["variant"]; + const SString variant = options["variant"].ToUpper(); - if (!variant.empty()) + if (!variant.empty() && variant != "HEX") { - variant = variant.ToUpper(); - - if (variant != "HEX") - { - m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); - lua::Push(luaVM, false); - return 1; - } + m_pScriptDebugging->LogCustom(luaVM, "Invalid value for field 'variant'"); + lua::Push(luaVM, false); + return 1; } // Async From 908dda12d4d453169eadef57f0cf010964afe399 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Wed, 14 Feb 2024 06:25:26 +0100 Subject: [PATCH 4/8] Modify function reading --- .../deathmatch/logic/luadefs/CLuaCryptDefs.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp b/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp index 6c357469b8..ec63d4ce7f 100644 --- a/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp +++ b/Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp @@ -408,11 +408,8 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM) argStream.ReadStringMap(options); } - if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsFunction()) - { - argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); - argStream.ReadFunctionComplete(); - } + argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); + argStream.ReadFunctionComplete(); if (!argStream.HasErrors()) { @@ -761,11 +758,8 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM) argStream.ReadStringMap(options); } - if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsFunction()) - { - argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); - argStream.ReadFunctionComplete(); - } + argStream.ReadFunction(luaFunctionRef, LUA_REFNIL); + argStream.ReadFunctionComplete(); if (!argStream.HasErrors()) { From ebb88ef16217526c9abf5d1a67620018fb97b4c2 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Sat, 17 Feb 2024 20:15:00 +0100 Subject: [PATCH 5/8] Replace string with SString for NRVO optimization --- Shared/sdk/SharedUtil.Crypto.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Shared/sdk/SharedUtil.Crypto.h b/Shared/sdk/SharedUtil.Crypto.h index be8837f700..a44cb22c5a 100644 --- a/Shared/sdk/SharedUtil.Crypto.h +++ b/Shared/sdk/SharedUtil.Crypto.h @@ -44,7 +44,7 @@ namespace SharedUtil inline SString Base64decode(const SString& data, const SString& variant = SString()) { - std::string result; + SString result; if (variant == "URL") { @@ -60,7 +60,7 @@ namespace SharedUtil inline SString Base32encode(const SString& data, const SString& variant = SString()) { - std::string result; + SString result; if (variant == "HEX") { @@ -76,7 +76,7 @@ namespace SharedUtil inline SString Base32decode(const SString& data, const SString& variant = SString()) { - std::string result; + SString result; if (variant == "HEX") { From 484a0efa2dea9a052c700e4356e99bc562625024 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Sun, 18 Feb 2024 04:25:57 +0100 Subject: [PATCH 6/8] Move literals to a separate namespace --- Shared/sdk/SharedUtil.Crypto.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Shared/sdk/SharedUtil.Crypto.h b/Shared/sdk/SharedUtil.Crypto.h index a44cb22c5a..c70b09982a 100644 --- a/Shared/sdk/SharedUtil.Crypto.h +++ b/Shared/sdk/SharedUtil.Crypto.h @@ -19,6 +19,12 @@ #include #include "SString.h" +namespace +{ + const SString baseURLVariant = "URL"; + const SString baseHexVariant = "HEX"; +} + namespace SharedUtil { struct KeyPair @@ -30,7 +36,7 @@ namespace SharedUtil { SString result; - if (variant == "URL") + if (variant == baseURLVariant) { CryptoPP::StringSource ss(data, true, new CryptoPP::Base64URLEncoder(new CryptoPP::StringSink(result), false)); } @@ -46,7 +52,7 @@ namespace SharedUtil { SString result; - if (variant == "URL") + if (variant == baseURLVariant) { CryptoPP::StringSource ss(data, true, new CryptoPP::Base64URLDecoder(new CryptoPP::StringSink(result))); } @@ -62,7 +68,7 @@ namespace SharedUtil { SString result; - if (variant == "HEX") + if (variant == baseHexVariant) { CryptoPP::StringSource ss(data, true, new CryptoPP::Base32HexEncoder(new CryptoPP::StringSink(result), false)); } @@ -78,7 +84,7 @@ namespace SharedUtil { SString result; - if (variant == "HEX") + if (variant == baseHexVariant) { CryptoPP::StringSource ss(data, true, new CryptoPP::Base32HexDecoder(new CryptoPP::StringSink(result))); } From 122df1a53efe76fb0bf5fafc7d84db7c7beac723 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Sun, 18 Feb 2024 05:19:04 +0100 Subject: [PATCH 7/8] Revert "Move literals to a separate namespace" This reverts commit 484a0efa2dea9a052c700e4356e99bc562625024. --- Shared/sdk/SharedUtil.Crypto.h | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Shared/sdk/SharedUtil.Crypto.h b/Shared/sdk/SharedUtil.Crypto.h index c70b09982a..a44cb22c5a 100644 --- a/Shared/sdk/SharedUtil.Crypto.h +++ b/Shared/sdk/SharedUtil.Crypto.h @@ -19,12 +19,6 @@ #include #include "SString.h" -namespace -{ - const SString baseURLVariant = "URL"; - const SString baseHexVariant = "HEX"; -} - namespace SharedUtil { struct KeyPair @@ -36,7 +30,7 @@ namespace SharedUtil { SString result; - if (variant == baseURLVariant) + if (variant == "URL") { CryptoPP::StringSource ss(data, true, new CryptoPP::Base64URLEncoder(new CryptoPP::StringSink(result), false)); } @@ -52,7 +46,7 @@ namespace SharedUtil { SString result; - if (variant == baseURLVariant) + if (variant == "URL") { CryptoPP::StringSource ss(data, true, new CryptoPP::Base64URLDecoder(new CryptoPP::StringSink(result))); } @@ -68,7 +62,7 @@ namespace SharedUtil { SString result; - if (variant == baseHexVariant) + if (variant == "HEX") { CryptoPP::StringSource ss(data, true, new CryptoPP::Base32HexEncoder(new CryptoPP::StringSink(result), false)); } @@ -84,7 +78,7 @@ namespace SharedUtil { SString result; - if (variant == baseHexVariant) + if (variant == "HEX") { CryptoPP::StringSource ss(data, true, new CryptoPP::Base32HexDecoder(new CryptoPP::StringSink(result))); } From a2708dd76d7e45970e199310b54db32676cc2468 Mon Sep 17 00:00:00 2001 From: Nico <122193236+Nico8340@users.noreply.github.com> Date: Sun, 18 Feb 2024 18:18:47 +0100 Subject: [PATCH 8/8] Remove old functions from CLuaCompatibilityDefs.cpp --- .../mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp | 2 -- .../mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp | 4 ---- 2 files changed, 6 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp index 7a189cdb9e..7dea8f95bf 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp @@ -71,8 +71,6 @@ void CLuaCompatibilityDefs::LoadFunctions() {"getControlState", CLuaPedDefs::GetPedControlState}, {"setCameraShakeLevel", ArgumentParserWarn}, {"getCameraShakeLevel", ArgumentParserWarn}, - {"base64Encode", CLuaCryptDefs::EncodeString}, - {"base64Decode", CLuaCryptDefs::DecodeString}, }; // Add functions diff --git a/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp b/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp index 0ce798bb45..0336b375b0 100644 --- a/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp +++ b/Server/mods/deathmatch/logic/luadefs/CLuaCompatibilityDefs.cpp @@ -89,10 +89,6 @@ void CLuaCompatibilityDefs::LoadFunctions() lua_pushboolean(luaVM, false); return 1; }}, - - // Base Encoding & Decoding - {"base64Encode", CLuaCryptDefs::EncodeString}, - {"base64Decode", CLuaCryptDefs::DecodeString}, }; // Add functions