Light optimizations for string utils

* Changed 'CreateDirectories' to only copy sanitized path if pointer to string is passed.
* Changed 'IsValidBase64' to only tokenize base64 value if pointer to string is passed.
This commit is contained in:
Kawe Mazidjatari 2023-02-04 15:59:45 +01:00
parent 588cb146bf
commit 60ab35b5c4
5 changed files with 21 additions and 17 deletions

View File

@ -65,14 +65,13 @@ int NET_SendDatagram(SOCKET s, void* pPayload, int iLenght, netadr_t* pAdr, bool
// Purpose: sets the user specified encryption key
// Input : svNetKey -
//-----------------------------------------------------------------------------
void NET_SetKey(string svNetKey)
void NET_SetKey(const string& svNetKey)
{
std::lock_guard<std::mutex> l(g_NetKeyMutex);
if (svNetKey.size() == AES_128_B64_ENCODED_SIZE &&
IsValidBase64(svNetKey))
IsValidBase64(svNetKey, &g_svNetKey)) // Results are tokenized by 'IsValidBase64()'.
{
g_svNetKey = svNetKey; // Results are tokenized by 'IsValidBase64()'.
v_NET_SetKey(g_pNetKey, g_svNetKey.c_str());
DevMsg(eDLL_T::ENGINE, "Installed NetKey: '%s%s%s'\n",

View File

@ -39,7 +39,7 @@ inline auto v_NET_PrintFunc = p_NET_PrintFunc.RCast<void(*)(const char* fmt)>();
///////////////////////////////////////////////////////////////////////////////
bool NET_ReceiveDatagram(int iSocket, netpacket_s* pInpacket, bool bRaw);
int NET_SendDatagram(SOCKET s, void* pPayload, int iLenght, netadr_t* pAdr, bool bEncrypted);
void NET_SetKey(string svNetKey);
void NET_SetKey(const string& svNetKey);
void NET_GenerateKey();
void NET_PrintFunc(const char* fmt, ...);
void NET_Shutdown(void* thisptr, const char* szReason, uint8_t bBadRep, bool bRemoveNow);

View File

@ -374,7 +374,7 @@ string CreateTimedFileName()
///////////////////////////////////////////////////////////////////////////////
// For creating directories for output streams.
string CreateDirectories(string svInput, bool bWindows)
void CreateDirectories(string svInput, string* pszOutput, bool bWindows)
{
if (bWindows)
{
@ -388,12 +388,14 @@ string CreateDirectories(string svInput, bool bWindows)
}
fs::path fspPathOut(svInput);
string result = fspPathOut.u8string();
if (pszOutput)
{
*pszOutput = fspPathOut.u8string();
}
fspPathOut = fspPathOut.parent_path();
fs::create_directories(fspPathOut);
return result;
}
///////////////////////////////////////////////////////////////////////////////
@ -432,14 +434,17 @@ string ConvertToUnixPath(const string& svInput)
///////////////////////////////////////////////////////////////////////////////
// For checking if input is a valid Base64.
bool IsValidBase64(string& svInput)
bool IsValidBase64(const string& svInput, string* psvOutput)
{
static const std::regex rx(R"((?:[A-Za-z0-9+\/]{4}?)*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=))");
std::smatch mh;
if (std::regex_search(svInput, mh, rx))
{
svInput = mh[0].str();
if (psvOutput)
{
*psvOutput = mh[0].str();
}
return true;
}
return false;

View File

@ -30,12 +30,12 @@ string GetFileName(const string& svInput, bool bRemoveExtension = false, bool bW
string RemoveFileName(const string& svInput, bool bWindows = false);
string CreateTimedFileName();
string CreateDirectories(string svInput, bool bWindows = false);
void CreateDirectories(string svInput, string* pszOutput = nullptr, bool bWindows = false);
string ConvertToWinPath(const string& svInput);
string ConvertToUnixPath(const string& svInput);
bool IsValidBase64(string& svInput);
bool IsValidBase64(const string& svInput, string* psvOutput = nullptr);
string Base64Encode(const string& svInput);
string Base64Decode(const string& svInput);

View File

@ -140,7 +140,7 @@ vector<VPKKeyValues_t> CPackedStore::GetEntryValues(const string& svWorkspace, K
if (!pManifestKV)
{
Warning(eDLL_T::FS, "Invalid VPK manifest KV; unable to build entry list\n");
Warning(eDLL_T::FS, "Invalid VPK build manifest KV; unable to parse entry list\n");
return vEntryValues;
}
@ -317,7 +317,7 @@ void CPackedStore::BuildManifest(const vector<VPKEntryBlock_t>& vBlock, const st
kv.RecursiveSaveToFile(uBuf, 0);
FileSystem()->CreateDirHierarchy(string(svWorkspace + "manifest/").c_str(), "GAME");
FileSystem()->CreateDirHierarchy(fmt::format("{:s}{:s}", svWorkspace, "manifest/").c_str(), "GAME");
FileSystem()->WriteFile(svPathOut.c_str(), "GAME", uBuf);
}
@ -510,7 +510,8 @@ void CPackedStore::UnpackWorkspace(const VPKDir_t& vDirectory, const string& svW
}
else // Chunk belongs to this block.
{
const string svFilePath = CreateDirectories(svWorkspace + vEntryBlock.m_svEntryPath);
string svFilePath;
CreateDirectories(svWorkspace + vEntryBlock.m_svEntryPath, &svFilePath);
FileHandle_t hAsset = FileSystem()->Open(svFilePath.c_str(), "wb", "GAME");
if (!hAsset)
@ -597,13 +598,12 @@ VPKEntryBlock_t::VPKEntryBlock_t(FileHandle_t hDirectoryFile, const string& svEn
{
m_svEntryPath = svEntryPath; // Set the entry path.
StringReplace(m_svEntryPath, "\\", "/"); // Flip windows-style backslash to forward slash.
StringReplace(m_svEntryPath, " /", ""); // Remove space character representing VPK root.
StringReplace(m_svEntryPath, " /", ""); // Remove space character representing VPK root.
FileSystem()->Read(&m_nFileCRC, sizeof(uint32_t), hDirectoryFile); //
FileSystem()->Read(&m_iPreloadSize, sizeof(uint16_t), hDirectoryFile); //
FileSystem()->Read(&m_iPackFileIndex, sizeof(uint16_t), hDirectoryFile); //
uint16_t nMarker = 0;
do // Loop through all chunks in the entry and add to list.
{