Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Base Encoding & Decoding #3312

Merged
merged 10 commits into from
Feb 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void CLuaCompatibilityDefs::LoadFunctions()
{"getControlState", CLuaPedDefs::GetPedControlState},
{"setCameraShakeLevel", ArgumentParserWarn<false, CLuaCameraDefs::SetCameraDrunkLevel>},
{"getCameraShakeLevel", ArgumentParserWarn<false, CLuaCameraDefs::GetCameraDrunkLevel>},
{"base64Encode", CLuaCryptDefs::EncodeString},
{"base64Decode", CLuaCryptDefs::DecodeString},
Nico8340 marked this conversation as resolved.
Show resolved Hide resolved
};

// Add functions
Expand Down
8 changes: 8 additions & 0 deletions Server/mods/deathmatch/logic/CResourceChecker.Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ namespace

// Ped jetpacks
//{false, "doesPedHaveJetPack", "isPedWearingJetpack"},

// Base Encoding & Decoding
{false, "base64Encode", "encodeString"},
{false, "base64Decode", "decodeString"}
};

SDeprecatedItem serverDeprecatedList[] = {
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "CLuaVehicleDefs.h"
#include "CLuaPedDefs.h"
#include "CLuaPlayerDefs.h"
#include "luadefs/CLuaCryptDefs.h"
#include "luadefs/CLuaXMLDefs.h"
#include <lua/CLuaFunctionParser.h>

Expand Down Expand Up @@ -88,6 +89,10 @@ void CLuaCompatibilityDefs::LoadFunctions()
lua_pushboolean(luaVM, false);
return 1;
}},

// Base Encoding & Decoding
{"base64Encode", CLuaCryptDefs::EncodeString},
{"base64Decode", CLuaCryptDefs::DecodeString},
};

// Add functions
Expand Down
2 changes: 2 additions & 0 deletions Shared/mods/deathmatch/logic/Enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
282 changes: 279 additions & 3 deletions Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void CLuaCryptDefs::LoadFunctions()
{"generateKeyPair", ArgumentParser<GenerateKeyPair>},
{"passwordVerify", PasswordVerify},
{"encodeString", EncodeString},
{"decodeString", DecodeString},
{"decodeString", DecodeString}
};

// Add functions
Expand Down Expand Up @@ -402,7 +402,11 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
CScriptArgReader argStream(luaVM);
argStream.ReadEnumString(algorithm);
argStream.ReadString(data);
argStream.ReadStringMap(options);

if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable())
tederis marked this conversation as resolved.
Show resolved Hide resolved
{
argStream.ReadStringMap(options);
}

argStream.ReadFunction(luaFunctionRef, LUA_REFNIL);
argStream.ReadFunctionComplete();
Expand Down Expand Up @@ -589,6 +593,140 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
}
return 1;
}
case StringEncodeFunction::BASE64:
{
const SString variant = options["variant"].ToUpper();

if (!variant.empty() && 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<SString, bool>& 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:
{
const SString variant = options["variant"].ToUpper();

if (!variant.empty() && 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<SString, bool>& 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");
Expand All @@ -614,7 +752,11 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
CScriptArgReader argStream(luaVM);
argStream.ReadEnumString(algorithm);
argStream.ReadString(data);
argStream.ReadStringMap(options);

if ((algorithm != StringEncodeFunction::BASE64 && algorithm != StringEncodeFunction::BASE32) || argStream.NextIsTable())
tederis marked this conversation as resolved.
Show resolved Hide resolved
{
argStream.ReadStringMap(options);
}

argStream.ReadFunction(luaFunctionRef, LUA_REFNIL);
argStream.ReadFunctionComplete();
Expand Down Expand Up @@ -808,6 +950,140 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
}
return 1;
}
case StringEncodeFunction::BASE64:
{
const SString variant = options["variant"].ToUpper();

if (!variant.empty() && 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<SString, bool>& 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:
{
const SString variant = options["variant"].ToUpper();

if (!variant.empty() && 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<SString, bool>& 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");
Expand Down
Loading