From 2ea490382b5bce06afd0b043237d67919da0b0c1 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Mon, 11 Jul 2016 17:37:36 +0200 Subject: [PATCH] More fixes and optimizations --- .../Modules/AssetInterfaces/IGfxImage.cpp | 4 +- .../Modules/AssetInterfaces/IMapEnts.cpp | 212 +++--- .../Modules/AssetInterfaces/IMaterial.cpp | 2 +- .../Modules/AssetInterfaces/IRawFile.cpp | 120 ++-- .../Modules/AssetInterfaces/IXModel.cpp | 4 +- src/Components/Modules/Localization.cpp | 4 +- src/Components/Modules/Menus.cpp | 8 +- src/Components/Modules/StringTable.cpp | 184 ++--- src/Components/Modules/Theatre.cpp | 2 +- src/Steam/Interfaces/SteamMatchmaking.cpp | 386 +++++------ src/Utils/Compression.cpp | 8 +- src/Utils/Cryptography.hpp | 648 +++++++++--------- src/Utils/Memory.cpp | 92 ++- src/Utils/Memory.hpp | 170 ++--- src/Utils/Utils.cpp | 16 - src/Utils/Utils.hpp | 1 - 16 files changed, 934 insertions(+), 927 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IGfxImage.cpp b/src/Components/Modules/AssetInterfaces/IGfxImage.cpp index e43e9453..feb08f60 100644 --- a/src/Components/Modules/AssetInterfaces/IGfxImage.cpp +++ b/src/Components/Modules/AssetInterfaces/IGfxImage.cpp @@ -7,7 +7,7 @@ namespace Assets Game::GfxImage* image = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_IMAGE, name.data()).image; if (image) return; // TODO: Check for default? - image = builder->GetAllocator()->AllocateArray(); + image = builder->GetAllocator()->Allocate(); if (!image) { Components::Logger::Error("Failed to allocate GfxImage structure!"); @@ -19,7 +19,7 @@ namespace Assets image->category = 0; image->cardMemory = 0; - image->texture = builder->GetAllocator()->AllocateArray(); + image->texture = builder->GetAllocator()->Allocate(); if (!image->texture) { Components::Logger::Error("Failed to allocate GfxImageLoadDef structure!"); diff --git a/src/Components/Modules/AssetInterfaces/IMapEnts.cpp b/src/Components/Modules/AssetInterfaces/IMapEnts.cpp index ef1351ed..70218d21 100644 --- a/src/Components/Modules/AssetInterfaces/IMapEnts.cpp +++ b/src/Components/Modules/AssetInterfaces/IMapEnts.cpp @@ -1,106 +1,106 @@ -#include - -namespace Assets -{ - void IMapEnts::Load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder) - { - Components::FileSystem::File ents(name + ".ents"); - if (ents.Exists()) - { - Game::MapEnts* entites = builder->GetAllocator()->AllocateArray(); - Game::MapEnts* orgEnts = Components::AssetHandler::FindOriginalAsset(this->GetType(), name.data()).mapEnts; - - if (orgEnts) - { - std::memcpy(entites, orgEnts, sizeof Game::MapEnts); - } - else - { - entites->name = builder->GetAllocator()->DuplicateString(name); - } - - entites->entityString = builder->GetAllocator()->DuplicateString(ents.GetBuffer()); - entites->numEntityChars = ents.GetBuffer().size() + 1; - - header->mapEnts = entites; - } - } - - void IMapEnts::Save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) - { - Assert_Size(Game::MapEnts, 44); - - Utils::Stream* buffer = builder->GetBuffer(); - Game::MapEnts* asset = header.mapEnts; - Game::MapEnts* dest = buffer->Dest(); - buffer->Save(asset); - - buffer->PushBlock(Game::XFILE_BLOCK_VIRTUAL); - - if (asset->name) - { - buffer->SaveString(builder->GetAssetName(this->GetType(), asset->name)); - Utils::Stream::ClearPointer(&dest->name); - } - - if (asset->entityString) - { - buffer->Save(asset->entityString, asset->numEntityChars); - Utils::Stream::ClearPointer(&dest->entityString); - } - - if (asset->trigger.models) - { - Assert_Size(Game::TriggerModel, 8); - buffer->Align(Utils::Stream::ALIGN_4); - - buffer->SaveArray(asset->trigger.models, asset->trigger.modelCount); - - Utils::Stream::ClearPointer(&dest->trigger.models); - } - - if (asset->trigger.hulls) - { - Assert_Size(Game::TriggerHull, 32); - buffer->Align(Utils::Stream::ALIGN_4); - - buffer->SaveArray(asset->trigger.hulls, asset->trigger.hullCount); - - Utils::Stream::ClearPointer(&dest->trigger.hulls); - } - - if (asset->trigger.slabs) - { - Assert_Size(Game::TriggerSlab, 20); - buffer->Align(Utils::Stream::ALIGN_4); - - buffer->SaveArray(asset->trigger.slabs, asset->trigger.slabCount); - - Utils::Stream::ClearPointer(&dest->trigger.slabs); - } - - if (asset->stages) - { - Assert_Size(Game::Stage, 20); - - buffer->Align(Utils::Stream::ALIGN_4); - - Game::Stage* destStages = buffer->Dest(); - buffer->SaveArray(asset->stages, asset->stageCount); - - for (int i = 0; i < asset->stageCount; ++i) - { - Game::Stage* destStage = &destStages[i]; - Game::Stage* stage = &asset->stages[i]; - - buffer->SaveString(stage->stageName); - - Utils::Stream::ClearPointer(&destStage->stageName); - } - - Utils::Stream::ClearPointer(&dest->stages); - } - - buffer->PopBlock(); - } -} +#include + +namespace Assets +{ + void IMapEnts::Load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder) + { + Components::FileSystem::File ents(name + ".ents"); + if (ents.Exists()) + { + Game::MapEnts* entites = builder->GetAllocator()->Allocate(); + Game::MapEnts* orgEnts = Components::AssetHandler::FindOriginalAsset(this->GetType(), name.data()).mapEnts; + + if (orgEnts) + { + std::memcpy(entites, orgEnts, sizeof Game::MapEnts); + } + else + { + entites->name = builder->GetAllocator()->DuplicateString(name); + } + + entites->entityString = builder->GetAllocator()->DuplicateString(ents.GetBuffer()); + entites->numEntityChars = ents.GetBuffer().size() + 1; + + header->mapEnts = entites; + } + } + + void IMapEnts::Save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) + { + Assert_Size(Game::MapEnts, 44); + + Utils::Stream* buffer = builder->GetBuffer(); + Game::MapEnts* asset = header.mapEnts; + Game::MapEnts* dest = buffer->Dest(); + buffer->Save(asset); + + buffer->PushBlock(Game::XFILE_BLOCK_VIRTUAL); + + if (asset->name) + { + buffer->SaveString(builder->GetAssetName(this->GetType(), asset->name)); + Utils::Stream::ClearPointer(&dest->name); + } + + if (asset->entityString) + { + buffer->Save(asset->entityString, asset->numEntityChars); + Utils::Stream::ClearPointer(&dest->entityString); + } + + if (asset->trigger.models) + { + Assert_Size(Game::TriggerModel, 8); + buffer->Align(Utils::Stream::ALIGN_4); + + buffer->SaveArray(asset->trigger.models, asset->trigger.modelCount); + + Utils::Stream::ClearPointer(&dest->trigger.models); + } + + if (asset->trigger.hulls) + { + Assert_Size(Game::TriggerHull, 32); + buffer->Align(Utils::Stream::ALIGN_4); + + buffer->SaveArray(asset->trigger.hulls, asset->trigger.hullCount); + + Utils::Stream::ClearPointer(&dest->trigger.hulls); + } + + if (asset->trigger.slabs) + { + Assert_Size(Game::TriggerSlab, 20); + buffer->Align(Utils::Stream::ALIGN_4); + + buffer->SaveArray(asset->trigger.slabs, asset->trigger.slabCount); + + Utils::Stream::ClearPointer(&dest->trigger.slabs); + } + + if (asset->stages) + { + Assert_Size(Game::Stage, 20); + + buffer->Align(Utils::Stream::ALIGN_4); + + Game::Stage* destStages = buffer->Dest(); + buffer->SaveArray(asset->stages, asset->stageCount); + + for (int i = 0; i < asset->stageCount; ++i) + { + Game::Stage* destStage = &destStages[i]; + Game::Stage* stage = &asset->stages[i]; + + buffer->SaveString(stage->stageName); + + Utils::Stream::ClearPointer(&destStage->stageName); + } + + Utils::Stream::ClearPointer(&dest->stages); + } + + buffer->PopBlock(); + } +} diff --git a/src/Components/Modules/AssetInterfaces/IMaterial.cpp b/src/Components/Modules/AssetInterfaces/IMaterial.cpp index 28e5a763..ed91fec7 100644 --- a/src/Components/Modules/AssetInterfaces/IMaterial.cpp +++ b/src/Components/Modules/AssetInterfaces/IMaterial.cpp @@ -33,7 +33,7 @@ namespace Assets return; } - Game::Material* material = builder->GetAllocator()->AllocateArray(); + Game::Material* material = builder->GetAllocator()->Allocate(); if (!material) { diff --git a/src/Components/Modules/AssetInterfaces/IRawFile.cpp b/src/Components/Modules/AssetInterfaces/IRawFile.cpp index 48b689ed..7be7d1f0 100644 --- a/src/Components/Modules/AssetInterfaces/IRawFile.cpp +++ b/src/Components/Modules/AssetInterfaces/IRawFile.cpp @@ -1,60 +1,60 @@ -#include - -namespace Assets -{ - void IRawFile::Load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder) - { - Components::FileSystem::File rawFile(name); - - if (rawFile.Exists()) - { - Game::RawFile* asset = builder->GetAllocator()->AllocateArray(); - - if (asset) - { - //std::string data = Utils::Compression::ZLib::Compress(rawFile.GetBuffer()); - - asset->name = builder->GetAllocator()->DuplicateString(name); - asset->compressedData = builder->GetAllocator()->DuplicateString(rawFile.GetBuffer()); - asset->sizeCompressed = 0;//data.size(); - asset->sizeUnCompressed = rawFile.GetBuffer().size(); - - header->rawfile = asset; - } - } - } - - void IRawFile::Save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) - { - Assert_Size(Game::RawFile, 16); - - Utils::Stream* buffer = builder->GetBuffer(); - Game::RawFile* asset = header.rawfile; - Game::RawFile* dest = buffer->Dest(); - buffer->Save(asset); - - buffer->PushBlock(Game::XFILE_BLOCK_VIRTUAL); - - if (asset->name) - { - buffer->SaveString(builder->GetAssetName(this->GetType(), asset->name)); - Utils::Stream::ClearPointer(&dest->name); - } - - if (asset->compressedData) - { - if (asset->sizeCompressed) - { - buffer->Save(asset->compressedData, asset->sizeCompressed); - } - else - { - buffer->Save(asset->compressedData, asset->sizeUnCompressed + 1); - } - - Utils::Stream::ClearPointer(&dest->compressedData); - } - - buffer->PopBlock(); - } -} +#include + +namespace Assets +{ + void IRawFile::Load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder) + { + Components::FileSystem::File rawFile(name); + + if (rawFile.Exists()) + { + Game::RawFile* asset = builder->GetAllocator()->Allocate(); + + if (asset) + { + //std::string data = Utils::Compression::ZLib::Compress(rawFile.GetBuffer()); + + asset->name = builder->GetAllocator()->DuplicateString(name); + asset->compressedData = builder->GetAllocator()->DuplicateString(rawFile.GetBuffer()); + asset->sizeCompressed = 0;//data.size(); + asset->sizeUnCompressed = rawFile.GetBuffer().size(); + + header->rawfile = asset; + } + } + } + + void IRawFile::Save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) + { + Assert_Size(Game::RawFile, 16); + + Utils::Stream* buffer = builder->GetBuffer(); + Game::RawFile* asset = header.rawfile; + Game::RawFile* dest = buffer->Dest(); + buffer->Save(asset); + + buffer->PushBlock(Game::XFILE_BLOCK_VIRTUAL); + + if (asset->name) + { + buffer->SaveString(builder->GetAssetName(this->GetType(), asset->name)); + Utils::Stream::ClearPointer(&dest->name); + } + + if (asset->compressedData) + { + if (asset->sizeCompressed) + { + buffer->Save(asset->compressedData, asset->sizeCompressed); + } + else + { + buffer->Save(asset->compressedData, asset->sizeUnCompressed + 1); + } + + Utils::Stream::ClearPointer(&dest->compressedData); + } + + buffer->PopBlock(); + } +} diff --git a/src/Components/Modules/AssetInterfaces/IXModel.cpp b/src/Components/Modules/AssetInterfaces/IXModel.cpp index 3c0b6a4e..bd509e9c 100644 --- a/src/Components/Modules/AssetInterfaces/IXModel.cpp +++ b/src/Components/Modules/AssetInterfaces/IXModel.cpp @@ -11,7 +11,7 @@ namespace Assets Game::XModel* baseModel = Components::AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_XMODEL, "viewmodel_mp5k").model; // Allocate new model and copy the base data to it - Game::XModel* model = builder->GetAllocator()->AllocateArray(); + Game::XModel* model = builder->GetAllocator()->Allocate(); std::memcpy(model, baseModel, sizeof(Game::XModel)); Utils::Stream::Reader reader(builder->GetAllocator(), modelFile.GetBuffer()); @@ -41,7 +41,7 @@ namespace Assets // Prepare surfaces Game::XSurface* baseSurface = &baseModel->lods[0].surfaces[0].surfaces[0]; - Game::XModelSurfs* surf = builder->GetAllocator()->AllocateArray(); + Game::XModelSurfs* surf = builder->GetAllocator()->Allocate(); std::memcpy(surf, baseModel->lods[0].surfaces, sizeof(Game::XModelSurfs)); surf->name = builder->GetAllocator()->DuplicateString(fmt::sprintf("%s1", model->name)); diff --git a/src/Components/Modules/Localization.cpp b/src/Components/Modules/Localization.cpp index 6a2793e1..70505bdf 100644 --- a/src/Components/Modules/Localization.cpp +++ b/src/Components/Modules/Localization.cpp @@ -19,7 +19,7 @@ namespace Components return; } - Game::LocalizedEntry* entry = Utils::Memory::AllocateArray(1); + Game::LocalizedEntry* entry = Utils::Memory::Allocate(); if (!entry) return; entry->name = Utils::Memory::DuplicateString(key); @@ -75,7 +75,7 @@ namespace Components } else { - Game::LocalizedEntry* entry = Utils::Memory::AllocateArray(1); + Game::LocalizedEntry* entry = Utils::Memory::Allocate(); if (!entry) return; entry->name = Utils::Memory::DuplicateString(key); diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index 896b8ba4..d95e5b9c 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -68,7 +68,7 @@ namespace Components script->next = NULL; - source = Utils::Memory::AllocateArray(1); + source = Utils::Memory::Allocate(); if (!source) { Game::FreeMemory(script); @@ -112,7 +112,7 @@ namespace Components Game::menuDef_t* Menus::ParseMenu(int handle) { - Game::menuDef_t* menu = Utils::Memory::AllocateArray(1); + Game::menuDef_t* menu = Utils::Memory::Allocate(); if (!menu) return nullptr; menu->items = Utils::Memory::AllocateArray(512); @@ -239,7 +239,7 @@ namespace Components if (menus.empty()) return nullptr; // Allocate new menu list - Game::MenuList* newList = Utils::Memory::AllocateArray(1); + Game::MenuList* newList = Utils::Memory::Allocate(); if (!newList) return nullptr; newList->menus = Utils::Memory::AllocateArray(menus.size()); @@ -285,7 +285,7 @@ namespace Components } // Allocate new menu list - Game::MenuList* newList = Utils::Memory::AllocateArray(1); + Game::MenuList* newList = Utils::Memory::Allocate(); if (!newList) return menuList; size_t size = menus.size(); diff --git a/src/Components/Modules/StringTable.cpp b/src/Components/Modules/StringTable.cpp index cc6785a4..30cfd183 100644 --- a/src/Components/Modules/StringTable.cpp +++ b/src/Components/Modules/StringTable.cpp @@ -1,92 +1,92 @@ -#include "STDInclude.hpp" - -namespace Components -{ - Utils::Memory::Allocator StringTable::MemAllocator; - std::map StringTable::StringTableMap; - - int StringTable::Hash(const char* data) - { - int hash = 0; - - while (*data != 0) - { - hash = tolower(*data) + (31 * hash); - - ++data; - } - - return hash; - } - - Game::StringTable* StringTable::LoadObject(std::string filename) - { - Game::StringTable* table = nullptr; - FileSystem::File rawTable(filename); - - if (rawTable.Exists()) - { - Utils::CSV parsedTable(rawTable.GetBuffer(), false, false); - - table = StringTable::MemAllocator.AllocateArray(1); - - if (table) - { - table->name = StringTable::MemAllocator.DuplicateString(filename); - table->columnCount = parsedTable.GetColumns(); - table->rowCount = parsedTable.GetRows(); - - table->values = StringTable::MemAllocator.AllocateArray(table->columnCount * table->rowCount); - - if (!table->values) - { - return nullptr; - } - - for (int i = 0; i < table->rowCount; ++i) - { - for (int j = 0; j < table->columnCount; ++j) - { - Game::StringTableCell* cell = &table->values[i * table->columnCount + j]; - cell->hash = StringTable::Hash(parsedTable.GetElementAt(i, j).data()); - cell->string = StringTable::MemAllocator.DuplicateString(parsedTable.GetElementAt(i, j)); - //if (!cell->string) cell->string = ""; // We have to assume it allocated successfully - } - } - - StringTable::StringTableMap[filename] = table; - } - } - else - { - StringTable::StringTableMap[filename] = nullptr; - } - - return table; - } - - StringTable::StringTable() - { - AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_STRINGTABLE, [] (Game::XAssetType, std::string filename) - { - Game::XAssetHeader header = { 0 }; - - if (StringTable::StringTableMap.find(filename) != StringTable::StringTableMap.end()) - { - header.stringTable = StringTable::StringTableMap[filename]; - } - else - { - header.stringTable = StringTable::LoadObject(filename); - } - - return header; - }); - } - - StringTable::~StringTable() - { - StringTable::StringTableMap.clear(); - StringTable::MemAllocator.Free(); - } -} +#include "STDInclude.hpp" + +namespace Components +{ + Utils::Memory::Allocator StringTable::MemAllocator; + std::map StringTable::StringTableMap; + + int StringTable::Hash(const char* data) + { + int hash = 0; + + while (*data != 0) + { + hash = tolower(*data) + (31 * hash); + + ++data; + } + + return hash; + } + + Game::StringTable* StringTable::LoadObject(std::string filename) + { + Game::StringTable* table = nullptr; + FileSystem::File rawTable(filename); + + if (rawTable.Exists()) + { + Utils::CSV parsedTable(rawTable.GetBuffer(), false, false); + + table = StringTable::MemAllocator.Allocate(); + + if (table) + { + table->name = StringTable::MemAllocator.DuplicateString(filename); + table->columnCount = parsedTable.GetColumns(); + table->rowCount = parsedTable.GetRows(); + + table->values = StringTable::MemAllocator.AllocateArray(table->columnCount * table->rowCount); + + if (!table->values) + { + return nullptr; + } + + for (int i = 0; i < table->rowCount; ++i) + { + for (int j = 0; j < table->columnCount; ++j) + { + Game::StringTableCell* cell = &table->values[i * table->columnCount + j]; + cell->hash = StringTable::Hash(parsedTable.GetElementAt(i, j).data()); + cell->string = StringTable::MemAllocator.DuplicateString(parsedTable.GetElementAt(i, j)); + //if (!cell->string) cell->string = ""; // We have to assume it allocated successfully + } + } + + StringTable::StringTableMap[filename] = table; + } + } + else + { + StringTable::StringTableMap[filename] = nullptr; + } + + return table; + } + + StringTable::StringTable() + { + AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_STRINGTABLE, [] (Game::XAssetType, std::string filename) + { + Game::XAssetHeader header = { 0 }; + + if (StringTable::StringTableMap.find(filename) != StringTable::StringTableMap.end()) + { + header.stringTable = StringTable::StringTableMap[filename]; + } + else + { + header.stringTable = StringTable::LoadObject(filename); + } + + return header; + }); + } + + StringTable::~StringTable() + { + StringTable::StringTableMap.clear(); + StringTable::MemAllocator.Free(); + } +} diff --git a/src/Components/Modules/Theatre.cpp b/src/Components/Modules/Theatre.cpp index 7f18439b..96e5fc8f 100644 --- a/src/Components/Modules/Theatre.cpp +++ b/src/Components/Modules/Theatre.cpp @@ -304,7 +304,7 @@ namespace Components FileSystem::DeleteFile("demos", fmt::sprintf("%s.json", files[i].data())); } - Command::Execute(fmt::sprintf("record auto_%I64d", time(0)), true); + Command::Execute(fmt::format("record auto_{}", time(0)), true); } return Utils::Hook::Call(0x42BBB0)(); diff --git a/src/Steam/Interfaces/SteamMatchmaking.cpp b/src/Steam/Interfaces/SteamMatchmaking.cpp index d432ea24..aab4776a 100644 --- a/src/Steam/Interfaces/SteamMatchmaking.cpp +++ b/src/Steam/Interfaces/SteamMatchmaking.cpp @@ -1,193 +1,193 @@ -#include "STDInclude.hpp" - -namespace Steam -{ - int Matchmaking::GetFavoriteGameCount() - { - return 0; - } - - bool Matchmaking::GetFavoriteGame(int iGame, unsigned int *pnAppID, unsigned int *pnIP, unsigned short *pnConnPort, unsigned short *pnQueryPort, unsigned int *punFlags, unsigned int *pRTime32LastPlayedOnServer) - { - return false; - } - - int Matchmaking::AddFavoriteGame(unsigned int nAppID, unsigned int nIP, unsigned short nConnPort, unsigned short nQueryPort, unsigned int unFlags, unsigned int rTime32LastPlayedOnServer) - { - return 0; - } - - bool Matchmaking::RemoveFavoriteGame(unsigned int nAppID, unsigned int nIP, unsigned short nConnPort, unsigned short nQueryPort, unsigned int unFlags) - { - return false; - } - - unsigned __int64 Matchmaking::RequestLobbyList() - { - return 0; - } - - void Matchmaking::AddRequestLobbyListStringFilter(const char *pchKeyToMatch, const char *pchValueToMatch, int eComparisonType) - { - } - - void Matchmaking::AddRequestLobbyListNumericalFilter(const char *pchKeyToMatch, int nValueToMatch, int eComparisonType) - { - } - - void Matchmaking::AddRequestLobbyListNearValueFilter(const char *pchKeyToMatch, int nValueToBeCloseTo) - { - } - - void Matchmaking::AddRequestLobbyListFilterSlotsAvailable(int nSlotsAvailable) - { - } - - SteamID Matchmaking::GetLobbyByIndex(int iLobby) - { - return SteamID(); - } - - unsigned __int64 Matchmaking::CreateLobby(int eLobbyType, int cMaxMembers) - { - uint64_t result = Callbacks::RegisterCall(); - LobbyCreated* retvals = ::Utils::Memory::AllocateArray(); - SteamID id; - - id.AccountID = 1337132; - id.Universe = 1; - id.AccountType = 8; - id.AccountInstance = 0x40000; - - retvals->m_eResult = 1; - retvals->m_ulSteamIDLobby = id; - - Callbacks::ReturnCall(retvals, sizeof(LobbyCreated), LobbyCreated::CallbackID, result); - - Matchmaking::JoinLobby(id); - - return result; - } - - unsigned __int64 Matchmaking::JoinLobby(SteamID steamIDLobby) - { - uint64_t result = Callbacks::RegisterCall(); - LobbyEnter* retvals = ::Utils::Memory::AllocateArray(); - retvals->m_bLocked = false; - retvals->m_EChatRoomEnterResponse = 1; - retvals->m_rgfChatPermissions = 0xFFFFFFFF; - retvals->m_ulSteamIDLobby = steamIDLobby; - - Callbacks::ReturnCall(retvals, sizeof(LobbyEnter), LobbyEnter::CallbackID, result); - - return result; - } - - void Matchmaking::LeaveLobby(SteamID steamIDLobby) - { - Components::Party::RemoveLobby(steamIDLobby); - } - - bool Matchmaking::InviteUserToLobby(SteamID steamIDLobby, SteamID steamIDInvitee) - { - return true; - } - - int Matchmaking::GetNumLobbyMembers(SteamID steamIDLobby) - { - return 1; - } - - SteamID Matchmaking::GetLobbyMemberByIndex(SteamID steamIDLobby, int iMember) - { - return SteamUser()->GetSteamID(); - } - - const char *Matchmaking::GetLobbyData(SteamID steamIDLobby, const char *pchKey) - { - return Components::Party::GetLobbyInfo(steamIDLobby, pchKey); - } - - bool Matchmaking::SetLobbyData(SteamID steamIDLobby, const char *pchKey, const char *pchValue) - { - return true; - } - - int Matchmaking::GetLobbyDataCount(SteamID steamIDLobby) - { - return 0; - } - - bool Matchmaking::GetLobbyDataByIndex(SteamID steamIDLobby, int iLobbyData, char *pchKey, int cchKeyBufferSize, char *pchValue, int cchValueBufferSize) - { - return false; - } - - bool Matchmaking::DeleteLobbyData(SteamID steamIDLobby, const char *pchKey) - { - return false; - } - - const char *Matchmaking::GetLobbyMemberData(SteamID steamIDLobby, SteamID steamIDUser, const char *pchKey) - { - return ""; - } - - void Matchmaking::SetLobbyMemberData(SteamID steamIDLobby, const char *pchKey, const char *pchValue) - { - } - - bool Matchmaking::SendLobbyChatMsg(SteamID steamIDLobby, const void *pvMsgBody, int cubMsgBody) - { - return true; - } - - int Matchmaking::GetLobbyChatEntry(SteamID steamIDLobby, int iChatID, SteamID *pSteamIDUser, void *pvData, int cubData, int *peChatEntryType) - { - return 0; - } - - bool Matchmaking::RequestLobbyData(SteamID steamIDLobby) - { - return false; - } - - void Matchmaking::SetLobbyGameServer(SteamID steamIDLobby, unsigned int unGameServerIP, unsigned short unGameServerPort, SteamID steamIDGameServer) - { - } - - bool Matchmaking::GetLobbyGameServer(SteamID steamIDLobby, unsigned int *punGameServerIP, unsigned short *punGameServerPort, SteamID *psteamIDGameServer) - { - return false; - } - - bool Matchmaking::SetLobbyMemberLimit(SteamID steamIDLobby, int cMaxMembers) - { - return true; - } - - int Matchmaking::GetLobbyMemberLimit(SteamID steamIDLobby) - { - return 0; - } - - bool Matchmaking::SetLobbyType(SteamID steamIDLobby, int eLobbyType) - { - return true; - } - - bool Matchmaking::SetLobbyJoinable(SteamID steamIDLobby, bool bLobbyJoinable) - { - return true; - } - - SteamID Matchmaking::GetLobbyOwner(SteamID steamIDLobby) - { - return SteamUser()->GetSteamID(); - } - - bool Matchmaking::SetLobbyOwner(SteamID steamIDLobby, SteamID steamIDNewOwner) - { - return true; - } -} +#include "STDInclude.hpp" + +namespace Steam +{ + int Matchmaking::GetFavoriteGameCount() + { + return 0; + } + + bool Matchmaking::GetFavoriteGame(int iGame, unsigned int *pnAppID, unsigned int *pnIP, unsigned short *pnConnPort, unsigned short *pnQueryPort, unsigned int *punFlags, unsigned int *pRTime32LastPlayedOnServer) + { + return false; + } + + int Matchmaking::AddFavoriteGame(unsigned int nAppID, unsigned int nIP, unsigned short nConnPort, unsigned short nQueryPort, unsigned int unFlags, unsigned int rTime32LastPlayedOnServer) + { + return 0; + } + + bool Matchmaking::RemoveFavoriteGame(unsigned int nAppID, unsigned int nIP, unsigned short nConnPort, unsigned short nQueryPort, unsigned int unFlags) + { + return false; + } + + unsigned __int64 Matchmaking::RequestLobbyList() + { + return 0; + } + + void Matchmaking::AddRequestLobbyListStringFilter(const char *pchKeyToMatch, const char *pchValueToMatch, int eComparisonType) + { + } + + void Matchmaking::AddRequestLobbyListNumericalFilter(const char *pchKeyToMatch, int nValueToMatch, int eComparisonType) + { + } + + void Matchmaking::AddRequestLobbyListNearValueFilter(const char *pchKeyToMatch, int nValueToBeCloseTo) + { + } + + void Matchmaking::AddRequestLobbyListFilterSlotsAvailable(int nSlotsAvailable) + { + } + + SteamID Matchmaking::GetLobbyByIndex(int iLobby) + { + return SteamID(); + } + + unsigned __int64 Matchmaking::CreateLobby(int eLobbyType, int cMaxMembers) + { + uint64_t result = Callbacks::RegisterCall(); + LobbyCreated* retvals = ::Utils::Memory::Allocate(); + SteamID id; + + id.AccountID = 1337132; + id.Universe = 1; + id.AccountType = 8; + id.AccountInstance = 0x40000; + + retvals->m_eResult = 1; + retvals->m_ulSteamIDLobby = id; + + Callbacks::ReturnCall(retvals, sizeof(LobbyCreated), LobbyCreated::CallbackID, result); + + Matchmaking::JoinLobby(id); + + return result; + } + + unsigned __int64 Matchmaking::JoinLobby(SteamID steamIDLobby) + { + uint64_t result = Callbacks::RegisterCall(); + LobbyEnter* retvals = ::Utils::Memory::Allocate(); + retvals->m_bLocked = false; + retvals->m_EChatRoomEnterResponse = 1; + retvals->m_rgfChatPermissions = 0xFFFFFFFF; + retvals->m_ulSteamIDLobby = steamIDLobby; + + Callbacks::ReturnCall(retvals, sizeof(LobbyEnter), LobbyEnter::CallbackID, result); + + return result; + } + + void Matchmaking::LeaveLobby(SteamID steamIDLobby) + { + Components::Party::RemoveLobby(steamIDLobby); + } + + bool Matchmaking::InviteUserToLobby(SteamID steamIDLobby, SteamID steamIDInvitee) + { + return true; + } + + int Matchmaking::GetNumLobbyMembers(SteamID steamIDLobby) + { + return 1; + } + + SteamID Matchmaking::GetLobbyMemberByIndex(SteamID steamIDLobby, int iMember) + { + return SteamUser()->GetSteamID(); + } + + const char *Matchmaking::GetLobbyData(SteamID steamIDLobby, const char *pchKey) + { + return Components::Party::GetLobbyInfo(steamIDLobby, pchKey); + } + + bool Matchmaking::SetLobbyData(SteamID steamIDLobby, const char *pchKey, const char *pchValue) + { + return true; + } + + int Matchmaking::GetLobbyDataCount(SteamID steamIDLobby) + { + return 0; + } + + bool Matchmaking::GetLobbyDataByIndex(SteamID steamIDLobby, int iLobbyData, char *pchKey, int cchKeyBufferSize, char *pchValue, int cchValueBufferSize) + { + return false; + } + + bool Matchmaking::DeleteLobbyData(SteamID steamIDLobby, const char *pchKey) + { + return false; + } + + const char *Matchmaking::GetLobbyMemberData(SteamID steamIDLobby, SteamID steamIDUser, const char *pchKey) + { + return ""; + } + + void Matchmaking::SetLobbyMemberData(SteamID steamIDLobby, const char *pchKey, const char *pchValue) + { + } + + bool Matchmaking::SendLobbyChatMsg(SteamID steamIDLobby, const void *pvMsgBody, int cubMsgBody) + { + return true; + } + + int Matchmaking::GetLobbyChatEntry(SteamID steamIDLobby, int iChatID, SteamID *pSteamIDUser, void *pvData, int cubData, int *peChatEntryType) + { + return 0; + } + + bool Matchmaking::RequestLobbyData(SteamID steamIDLobby) + { + return false; + } + + void Matchmaking::SetLobbyGameServer(SteamID steamIDLobby, unsigned int unGameServerIP, unsigned short unGameServerPort, SteamID steamIDGameServer) + { + } + + bool Matchmaking::GetLobbyGameServer(SteamID steamIDLobby, unsigned int *punGameServerIP, unsigned short *punGameServerPort, SteamID *psteamIDGameServer) + { + return false; + } + + bool Matchmaking::SetLobbyMemberLimit(SteamID steamIDLobby, int cMaxMembers) + { + return true; + } + + int Matchmaking::GetLobbyMemberLimit(SteamID steamIDLobby) + { + return 0; + } + + bool Matchmaking::SetLobbyType(SteamID steamIDLobby, int eLobbyType) + { + return true; + } + + bool Matchmaking::SetLobbyJoinable(SteamID steamIDLobby, bool bLobbyJoinable) + { + return true; + } + + SteamID Matchmaking::GetLobbyOwner(SteamID steamIDLobby) + { + return SteamUser()->GetSteamID(); + } + + bool Matchmaking::SetLobbyOwner(SteamID steamIDLobby, SteamID steamIDNewOwner) + { + return true; + } +} diff --git a/src/Utils/Compression.cpp b/src/Utils/Compression.cpp index 56be5ed3..02c85351 100644 --- a/src/Utils/Compression.cpp +++ b/src/Utils/Compression.cpp @@ -35,7 +35,9 @@ namespace Utils } int ret = 0; - uint8_t* dest = Utils::Memory::AllocateArray(CHUNK); + Utils::Memory::Allocator allocator; + + uint8_t* dest = allocator.AllocateArray(CHUNK); const char* dataPtr = data.data(); do @@ -52,7 +54,6 @@ namespace Utils if (ret == Z_STREAM_ERROR) { inflateEnd(&stream); - Utils::Memory::Free(dest); return ""; } @@ -63,9 +64,6 @@ namespace Utils } while (ret != Z_STREAM_END); inflateEnd(&stream); - - Utils::Memory::Free(dest); - return buffer; } }; diff --git a/src/Utils/Cryptography.hpp b/src/Utils/Cryptography.hpp index a69843b7..ecae703f 100644 --- a/src/Utils/Cryptography.hpp +++ b/src/Utils/Cryptography.hpp @@ -1,324 +1,324 @@ -namespace Utils -{ - namespace Cryptography - { - void Initialize(); - - class Token - { - public: - Token() { this->TokenString.clear(); }; - Token(const Token& obj) : TokenString(obj.TokenString) { }; - Token(std::string token) : TokenString(token.begin(), token.end()) { }; - Token(std::basic_string token) : TokenString(token.begin(), token.end()) { }; - - Token& operator++ () - { - if (this->TokenString.empty()) - { - this->TokenString.append(reinterpret_cast("\0"), 1); - } - else - { - for (unsigned int i = (this->TokenString.size() - 1); i >= 0; i--) - { - if (this->TokenString[i] == 0xFF) - { - this->TokenString[i] = 0; - - if (!i) - { - // Prepend here, as /dev/urandom says so ;) https://github.com/IW4x/iw4x-client-node/wikis/technical-information#incrementing-the-token - this->TokenString = std::basic_string(reinterpret_cast("\0"), 1) + this->TokenString; - break; - } - } - else - { - ++this->TokenString[i]; - break; - } - } - } - - return *this; - } - - Token operator++ (int) - { - Token result = *this; - this->operator++(); - return result; - } - - bool operator==(const Token& token) const - { - return (this->ToString() == token.ToString()); - } - - bool operator!=(const Token& token) const - { - return !(*this == token); - } - - bool operator<(const Token& token) const - { - if (*this == token) - { - return false; - } - else if (this->ToString().size() < token.ToString().size()) - { - return true; - } - else if (this->ToString().size() > token.ToString().size()) - { - return false; - } - else - { - auto lStr = this->ToString(); - auto rStr = token.ToString(); - - for (unsigned int i = 0; i < lStr.size(); ++i) - { - if (lStr[i] < rStr[i]) - { - return true; - } - } - } - - return false; - } - - bool operator>(const Token& token) const - { - return (token < *this && *this != token); - } - - bool operator<=(const Token& token) const - { - return !(*this > token); - } - - bool operator>=(const Token& token) const - { - return !(*this < token); - } - - std::string ToString() - { - return std::string(this->TokenString.begin(), this->TokenString.end()); - } - - const std::string ToString() const - { - return std::string(this->TokenString.begin(), this->TokenString.end()); - } - - std::basic_string ToUnsignedString() - { - return this->TokenString; - } - - void Clear() - { - this->TokenString.clear(); - } - - private: - std::basic_string TokenString; - }; - - class Rand - { - public: - static uint32_t GenerateInt(); - static void Initialize(); - - private: - static prng_state State; - }; - - class ECC - { - public: - class Key - { - public: - Key() : KeyStorage(new ecc_key) - { - ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); - }; - Key(ecc_key* key) : Key() { if(key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); }; - Key(ecc_key key) : Key(&key) {}; - ~Key() - { - if (this->KeyStorage.use_count() <= 1) - { - this->Free(); - } - }; - - bool IsValid() - { - return (!Utils::MemIsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr()))); - } - - ecc_key* GetKeyPtr() - { - return this->KeyStorage.get(); - } - - std::string GetPublicKey() - { - uint8_t buffer[512] = { 0 }; - DWORD length = sizeof(buffer); - - if (ecc_ansi_x963_export(this->GetKeyPtr(), buffer, &length) == CRYPT_OK) - { - return std::string(reinterpret_cast(buffer), length); - } - - return ""; - } - - void Set(std::string pubKeyBuffer) - { - this->Free(); - - if (ecc_ansi_x963_import(reinterpret_cast(pubKeyBuffer.data()), pubKeyBuffer.size(), this->GetKeyPtr()) != CRYPT_OK) - { - ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr())); - } - } - - void Import(std::string key, int type = PK_PRIVATE) - { - this->Free(); - - if (ecc_import(reinterpret_cast(key.data()), key.size(), this->GetKeyPtr()) != CRYPT_OK) - { - ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr())); - } - } - - std::string Export(int type = PK_PRIVATE) - { - uint8_t buffer[4096] = { 0 }; - DWORD length = sizeof(buffer); - - if (ecc_export(buffer, &length, type, this->GetKeyPtr()) == CRYPT_OK) - { - return std::string(reinterpret_cast(buffer), length); - } - - return ""; - } - - void Free() - { - if (this->IsValid()) - { - ecc_free(this->GetKeyPtr()); - } - - ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); - } - - private: - std::shared_ptr KeyStorage; - }; - - static Key GenerateKey(int bits); - static std::string SignMessage(Key key, std::string message); - static bool VerifyMessage(Key key, std::string message, std::string signature); - }; - - class RSA - { - public: - class Key - { - public: - Key() : KeyStorage(new rsa_key) - { - ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); - }; - Key(rsa_key* key) : Key() { if (key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); }; - Key(rsa_key key) : Key(&key) {}; - ~Key() - { - if (this->KeyStorage.use_count() <= 1) - { - this->Free(); - } - }; - - rsa_key* GetKeyPtr() - { - return this->KeyStorage.get(); - } - - bool IsValid() - { - return (!Utils::MemIsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr()))); - } - - void Free() - { - if (this->IsValid()) - { - rsa_free(this->GetKeyPtr()); - } - - ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); - } - - private: - std::shared_ptr KeyStorage; - }; - - static Key GenerateKey(int bits); - static std::string SignMessage(Key key, std::string message); - static bool VerifyMessage(Key key, std::string message, std::string signature); - }; - - class TDES - { - public: - static void Initialize(); - static std::string Encrypt(std::string text, std::string iv, std::string key); - static std::string Decrpyt(std::string text, std::string iv, std::string key); - }; - - class Tiger - { - public: - static std::string Compute(std::string data, bool hex = false); - static std::string Compute(const uint8_t* data, size_t length, bool hex = false); - }; - - class SHA256 - { - public: - static std::string Compute(std::string data, bool hex = false); - static std::string Compute(const uint8_t* data, size_t length, bool hex = false); - }; - - class SHA512 - { - public: - static std::string Compute(std::string data, bool hex = false); - static std::string Compute(const uint8_t* data, size_t length, bool hex = false); - }; - - class JenkinsOneAtATime - { - public: - static unsigned int Compute(std::string data); - static unsigned int Compute(const char *key, size_t len); - }; - } -} +namespace Utils +{ + namespace Cryptography + { + void Initialize(); + + class Token + { + public: + Token() { this->TokenString.clear(); }; + Token(const Token& obj) : TokenString(obj.TokenString) { }; + Token(std::string token) : TokenString(token.begin(), token.end()) { }; + Token(std::basic_string token) : TokenString(token.begin(), token.end()) { }; + + Token& operator++ () + { + if (this->TokenString.empty()) + { + this->TokenString.append(reinterpret_cast("\0"), 1); + } + else + { + for (unsigned int i = (this->TokenString.size() - 1); i >= 0; i--) + { + if (this->TokenString[i] == 0xFF) + { + this->TokenString[i] = 0; + + if (!i) + { + // Prepend here, as /dev/urandom says so ;) https://github.com/IW4x/iw4x-client-node/wikis/technical-information#incrementing-the-token + this->TokenString = std::basic_string(reinterpret_cast("\0"), 1) + this->TokenString; + break; + } + } + else + { + ++this->TokenString[i]; + break; + } + } + } + + return *this; + } + + Token operator++ (int) + { + Token result = *this; + this->operator++(); + return result; + } + + bool operator==(const Token& token) const + { + return (this->ToString() == token.ToString()); + } + + bool operator!=(const Token& token) const + { + return !(*this == token); + } + + bool operator<(const Token& token) const + { + if (*this == token) + { + return false; + } + else if (this->ToString().size() < token.ToString().size()) + { + return true; + } + else if (this->ToString().size() > token.ToString().size()) + { + return false; + } + else + { + auto lStr = this->ToString(); + auto rStr = token.ToString(); + + for (unsigned int i = 0; i < lStr.size(); ++i) + { + if (lStr[i] < rStr[i]) + { + return true; + } + } + } + + return false; + } + + bool operator>(const Token& token) const + { + return (token < *this && *this != token); + } + + bool operator<=(const Token& token) const + { + return !(*this > token); + } + + bool operator>=(const Token& token) const + { + return !(*this < token); + } + + std::string ToString() + { + return std::string(this->TokenString.begin(), this->TokenString.end()); + } + + const std::string ToString() const + { + return std::string(this->TokenString.begin(), this->TokenString.end()); + } + + std::basic_string ToUnsignedString() + { + return this->TokenString; + } + + void Clear() + { + this->TokenString.clear(); + } + + private: + std::basic_string TokenString; + }; + + class Rand + { + public: + static uint32_t GenerateInt(); + static void Initialize(); + + private: + static prng_state State; + }; + + class ECC + { + public: + class Key + { + public: + Key() : KeyStorage(new ecc_key) + { + ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); + }; + Key(ecc_key* key) : Key() { if(key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); }; + Key(ecc_key key) : Key(&key) {}; + ~Key() + { + if (this->KeyStorage.use_count() <= 1) + { + this->Free(); + } + }; + + bool IsValid() + { + return (!Utils::Memory::IsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr()))); + } + + ecc_key* GetKeyPtr() + { + return this->KeyStorage.get(); + } + + std::string GetPublicKey() + { + uint8_t buffer[512] = { 0 }; + DWORD length = sizeof(buffer); + + if (ecc_ansi_x963_export(this->GetKeyPtr(), buffer, &length) == CRYPT_OK) + { + return std::string(reinterpret_cast(buffer), length); + } + + return ""; + } + + void Set(std::string pubKeyBuffer) + { + this->Free(); + + if (ecc_ansi_x963_import(reinterpret_cast(pubKeyBuffer.data()), pubKeyBuffer.size(), this->GetKeyPtr()) != CRYPT_OK) + { + ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr())); + } + } + + void Import(std::string key, int type = PK_PRIVATE) + { + this->Free(); + + if (ecc_import(reinterpret_cast(key.data()), key.size(), this->GetKeyPtr()) != CRYPT_OK) + { + ZeroMemory(this->KeyStorage.get(), sizeof(*this->GetKeyPtr())); + } + } + + std::string Export(int type = PK_PRIVATE) + { + uint8_t buffer[4096] = { 0 }; + DWORD length = sizeof(buffer); + + if (ecc_export(buffer, &length, type, this->GetKeyPtr()) == CRYPT_OK) + { + return std::string(reinterpret_cast(buffer), length); + } + + return ""; + } + + void Free() + { + if (this->IsValid()) + { + ecc_free(this->GetKeyPtr()); + } + + ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); + } + + private: + std::shared_ptr KeyStorage; + }; + + static Key GenerateKey(int bits); + static std::string SignMessage(Key key, std::string message); + static bool VerifyMessage(Key key, std::string message, std::string signature); + }; + + class RSA + { + public: + class Key + { + public: + Key() : KeyStorage(new rsa_key) + { + ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); + }; + Key(rsa_key* key) : Key() { if (key) std::memmove(this->GetKeyPtr(), key, sizeof(*key)); }; + Key(rsa_key key) : Key(&key) {}; + ~Key() + { + if (this->KeyStorage.use_count() <= 1) + { + this->Free(); + } + }; + + rsa_key* GetKeyPtr() + { + return this->KeyStorage.get(); + } + + bool IsValid() + { + return (!Utils::Memory::IsSet(this->GetKeyPtr(), 0, sizeof(*this->GetKeyPtr()))); + } + + void Free() + { + if (this->IsValid()) + { + rsa_free(this->GetKeyPtr()); + } + + ZeroMemory(this->GetKeyPtr(), sizeof(*this->GetKeyPtr())); + } + + private: + std::shared_ptr KeyStorage; + }; + + static Key GenerateKey(int bits); + static std::string SignMessage(Key key, std::string message); + static bool VerifyMessage(Key key, std::string message, std::string signature); + }; + + class TDES + { + public: + static void Initialize(); + static std::string Encrypt(std::string text, std::string iv, std::string key); + static std::string Decrpyt(std::string text, std::string iv, std::string key); + }; + + class Tiger + { + public: + static std::string Compute(std::string data, bool hex = false); + static std::string Compute(const uint8_t* data, size_t length, bool hex = false); + }; + + class SHA256 + { + public: + static std::string Compute(std::string data, bool hex = false); + static std::string Compute(const uint8_t* data, size_t length, bool hex = false); + }; + + class SHA512 + { + public: + static std::string Compute(std::string data, bool hex = false); + static std::string Compute(const uint8_t* data, size_t length, bool hex = false); + }; + + class JenkinsOneAtATime + { + public: + static unsigned int Compute(std::string data); + static unsigned int Compute(const char *key, size_t len); + }; + } +} diff --git a/src/Utils/Memory.cpp b/src/Utils/Memory.cpp index f63b18be..4ed582bb 100644 --- a/src/Utils/Memory.cpp +++ b/src/Utils/Memory.cpp @@ -1,38 +1,54 @@ -#include "STDInclude.hpp" - -namespace Utils -{ - void* Memory::Allocate(size_t length) - { - void* data = new char[length]; - - assert(data != nullptr); - - if (data) - { - ZeroMemory(data, length); - } - - return data; - } - - char* Memory::DuplicateString(std::string string) - { - char* newString = Memory::AllocateArray(string.size() + 1); - std::memcpy(newString, string.data(), string.size()); - return newString; - } - - void Memory::Free(void* data) - { - if (data) - { - delete[] data; - } - } - - void Memory::Free(const void* data) - { - Memory::Free(const_cast(data)); - } -} +#include "STDInclude.hpp" + +namespace Utils +{ + void* Memory::Allocate(size_t length) + { + void* data = new char[length]; + + assert(data != nullptr); + + if (data) + { + ZeroMemory(data, length); + } + + return data; + } + + char* Memory::DuplicateString(std::string string) + { + char* newString = Memory::AllocateArray(string.size() + 1); + std::memcpy(newString, string.data(), string.size()); + return newString; + } + + void Memory::Free(void* data) + { + if (data) + { + delete[] data; + } + } + + void Memory::Free(const void* data) + { + Memory::Free(const_cast(data)); + } + + // Complementary function for memset, which checks if memory is filled with a char + bool Memory::IsSet(void* mem, char chr, size_t length) + { + char* memArr = reinterpret_cast(mem); + + for (size_t i = 0; i < length; ++i) + { + if (memArr[i] != chr) + { + return false; + } + } + + return true; + } +} diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index 68ca4661..110f8ddc 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -1,80 +1,90 @@ -namespace Utils -{ - class Memory - { - public: - class Allocator - { - public: - typedef void(*FreeCallback)(void*); - - Allocator() - { - this->Pool.clear(); - this->RefMemory.clear(); - } - ~Allocator() - { - this->Free(); - } - - void Free() - { - for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i) - { - if (i->first && i->second) - { - i->second(i->first); - } - } - - this->RefMemory.clear(); - - for (auto data : this->Pool) - { - Memory::Free(data); - } - - this->Pool.clear(); - } - - void Reference(void* memory, FreeCallback callback) - { - this->RefMemory[memory] = callback; - } - - void* Allocate(size_t length) - { - void* data = Memory::Allocate(length); - this->Pool.push_back(data); - return data; - } - template T* AllocateArray(size_t count = 1) - { - return static_cast(this->Allocate(count * sizeof(T))); - } - - char* DuplicateString(std::string string) - { - char* data = Memory::DuplicateString(string); - this->Pool.push_back(data); - return data; - } - - private: - std::vector Pool; - std::map RefMemory; - }; - - static void* Allocate(size_t length); - template static T* AllocateArray(size_t count = 1) - { - return static_cast(Allocate(count * sizeof(T))); - } - - static char* DuplicateString(std::string string); - - static void Free(void* data); - static void Free(const void* data); - }; -} +namespace Utils +{ + class Memory + { + public: + class Allocator + { + public: + typedef void(*FreeCallback)(void*); + + Allocator() + { + this->Pool.clear(); + this->RefMemory.clear(); + } + ~Allocator() + { + this->Free(); + } + + void Free() + { + for (auto i = this->RefMemory.begin(); i != this->RefMemory.end(); ++i) + { + if (i->first && i->second) + { + i->second(i->first); + } + } + + this->RefMemory.clear(); + + for (auto data : this->Pool) + { + Memory::Free(data); + } + + this->Pool.clear(); + } + + void Reference(void* memory, FreeCallback callback) + { + this->RefMemory[memory] = callback; + } + + void* Allocate(size_t length) + { + void* data = Memory::Allocate(length); + this->Pool.push_back(data); + return data; + } + template T* Allocate() + { + return this->AllocateArray(1); + } + template T* AllocateArray(size_t count = 1) + { + return static_cast(this->Allocate(count * sizeof(T))); + } + + char* DuplicateString(std::string string) + { + char* data = Memory::DuplicateString(string); + this->Pool.push_back(data); + return data; + } + + private: + std::vector Pool; + std::map RefMemory; + }; + + static void* Allocate(size_t length); + template static T* Allocate() + { + return AllocateArray(1); + } + template static T* AllocateArray(size_t count = 1) + { + return static_cast(Allocate(count * sizeof(T))); + } + + static char* DuplicateString(std::string string); + + static void Free(void* data); + static void Free(const void* data); + + static bool IsSet(void* mem, char chr, size_t length); + }; +} diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index ba31c55c..02c080ec 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -16,22 +16,6 @@ namespace Utils return "application/octet-stream"; } - // Complementary function for memset, which checks if a memory is set - bool MemIsSet(void* mem, char chr, size_t length) - { - char* memArr = reinterpret_cast(mem); - - for (size_t i = 0; i < length; ++i) - { - if (memArr[i] != chr) - { - return false; - } - } - - return true; - } - std::string ParseChallenge(std::string data) { auto pos = data.find_first_of("\n "); diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index 8479e31c..99a187a7 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -2,7 +2,6 @@ namespace Utils { std::string GetMimeType(std::string url); std::string ParseChallenge(std::string data); - bool MemIsSet(void* mem, char chr, size_t length); template void Merge(std::vector* target, T* source, size_t length) {