From e9bd4651ca14f7c5fe8b3aaad05f7a1d8fa3e4f3 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 26 Dec 2020 14:17:50 +0100 Subject: [PATCH] Clean up and formatting --- source/PluginManagement.cpp | 30 ++++--- source/globals.cpp | 7 +- source/globals.h | 8 +- source/hooks.cpp | 33 ++++---- source/main.cpp | 29 +++---- source/patcher/hooks_patcher_static.cpp | 9 +-- source/plugin/DynamicLinkingHelper.cpp | 34 ++++---- source/plugin/DynamicLinkingHelper.h | 7 +- source/plugin/FunctionData.h | 26 +++--- source/plugin/HookData.h | 8 +- source/plugin/ImportRPLInformation.h | 11 ++- source/plugin/PluginContainer.h | 18 ++--- source/plugin/PluginContainerPersistence.cpp | 46 ++++++----- source/plugin/PluginContainerPersistence.h | 4 +- source/plugin/PluginData.cpp | 25 +++--- source/plugin/PluginData.h | 19 ++--- source/plugin/PluginDataFactory.cpp | 16 ++-- source/plugin/PluginDataPersistence.cpp | 4 +- source/plugin/PluginInformation.cpp | 12 +-- source/plugin/PluginInformation.h | 27 +++---- source/plugin/PluginInformationFactory.cpp | 29 +++---- source/plugin/PluginInformationFactory.h | 2 +- source/plugin/PluginMetaInformation.h | 49 ++++++----- .../plugin/PluginMetaInformationFactory.cpp | 11 +-- source/plugin/PluginMetaInformationFactory.h | 2 +- source/plugin/RelocationData.h | 26 +++--- source/plugin/SectionInfo.h | 25 +++--- source/utils/exports.cpp | 81 ++++++++++--------- source/utils/exports.h | 2 +- source/utils/logger.h | 8 +- 30 files changed, 283 insertions(+), 325 deletions(-) diff --git a/source/PluginManagement.cpp b/source/PluginManagement.cpp index b7bae25..3f4194a 100644 --- a/source/PluginManagement.cpp +++ b/source/PluginManagement.cpp @@ -1,10 +1,14 @@ -#include -#include #include -#include -#include #include +#include + #include "patcher/hooks_patcher_static.h" +#include "plugin/PluginContainer.h" +#include "plugin/PluginMetaInformationFactory.h" +#include "plugin/PluginInformationFactory.h" + +#include "utils/logger.h" +#include "utils/ElfUtils.h" #include "PluginManagement.h" #include "hooks.h" @@ -12,17 +16,17 @@ bool PluginManagement::doRelocation(const std::vector &relocData std::map moduleHandleCache; for (auto const &cur : relocData) { uint32_t functionAddress = 0; - std::string functionName = cur.getName(); + const std::string &functionName = cur.getName(); - if (functionName.compare("MEMAllocFromDefaultHeap") == 0) { + if (functionName == "MEMAllocFromDefaultHeap") { OSDynLoad_Module rplHandle; OSDynLoad_Acquire("homebrew_memorymapping", &rplHandle); OSDynLoad_FindExport(rplHandle, 1, "MEMAllocFromMappedMemory", (void **) &functionAddress); - } else if (functionName.compare("MEMAllocFromDefaultHeapEx") == 0) { + } else if (functionName == "MEMAllocFromDefaultHeapEx") { OSDynLoad_Module rplHandle; OSDynLoad_Acquire("homebrew_memorymapping", &rplHandle); OSDynLoad_FindExport(rplHandle, 1, "MEMAllocFromMappedMemoryEx", (void **) &functionAddress); - } else if (functionName.compare("MEMFreeToDefaultHeap") == 0) { + } else if (functionName == "MEMFreeToDefaultHeap") { OSDynLoad_Module rplHandle; OSDynLoad_Acquire("homebrew_memorymapping", &rplHandle); OSDynLoad_FindExport(rplHandle, 1, "MEMFreeToMappedMemory", (void **) &functionAddress); @@ -31,7 +35,7 @@ bool PluginManagement::doRelocation(const std::vector &relocData if (functionAddress == 0) { std::string rplName = cur.getImportRPLInformation().getName(); int32_t isData = cur.getImportRPLInformation().isData(); - OSDynLoad_Module rplHandle = 0; + OSDynLoad_Module rplHandle = nullptr; if (moduleHandleCache.count(rplName) > 0) { rplHandle = moduleHandleCache[rplName]; } else { @@ -138,10 +142,10 @@ void PluginManagement::unloadPlugins(plugin_information_t *gPluginInformation, M DEBUG_FUNCTION_LINE("Freed %08X", plugin->info.allocatedDataMemoryAddress); } - for (uint32_t i = 0; i < DYN_LINK_TRAMPOLIN_LIST_LENGTH; i++) { - if (gPluginInformation->trampolines[i].id == plugin->info.trampolinId) { - gPluginInformation->trampolines[i].id = 0; - gPluginInformation->trampolines[i].status = RELOC_TRAMP_FREE; + for (auto &trampoline : gPluginInformation->trampolines) { + if (trampoline.id == plugin->info.trampolinId) { + trampoline.id = 0; + trampoline.status = RELOC_TRAMP_FREE; } } } diff --git a/source/globals.cpp b/source/globals.cpp index 937b000..1517ff9 100644 --- a/source/globals.cpp +++ b/source/globals.cpp @@ -1,6 +1,7 @@ #include "globals.h" -MEMHeapHandle pluginDataHeap __attribute__((section(".data"))) = 0; -plugin_information_t *gPluginInformation __attribute__((section(".data"))) = NULL; +MEMHeapHandle pluginDataHeap __attribute__((section(".data"))) = nullptr; +plugin_information_t *gPluginInformation __attribute__((section(".data"))) = nullptr; +plugin_information_on_reload_t gLinkOnReload __attribute__((section(".data"))); -plugin_information_on_reload_t gLinkOnReload __attribute__((section(".data"))); \ No newline at end of file +module_information_t *gModuleData __attribute__((section(".data"))) = nullptr; \ No newline at end of file diff --git a/source/globals.h b/source/globals.h index 509bd35..090d60b 100644 --- a/source/globals.h +++ b/source/globals.h @@ -1,10 +1,14 @@ #pragma once -#include -#include +#include + +#include "plugin/PluginContainer.h" +#include "common/plugin_defines.h" extern plugin_information_t *gPluginInformation; extern MEMHeapHandle pluginDataHeap; extern plugin_information_on_reload_t gLinkOnReload; +extern module_information_t *gModuleData; + extern "C" void __init_wut(); extern "C" void __fini_wut(); \ No newline at end of file diff --git a/source/hooks.cpp b/source/hooks.cpp index 2cb57a1..cde2240 100644 --- a/source/hooks.cpp +++ b/source/hooks.cpp @@ -1,7 +1,6 @@ #include "hooks.h" #include "utils/logger.h" - void CallHook(plugin_information_t *pluginInformation, wups_loader_hook_type_t hook_type) { CallHookEx(pluginInformation, hook_type, -1); } @@ -66,41 +65,41 @@ void CallHookEx(plugin_information_t *pluginInformation, wups_loader_hook_type_t DEBUG_FUNCTION_LINE("Calling hook of type %s for plugin %s [%d]", hook_names[hook_data->type], plugin_data->meta.name, hook_type); } void *func_ptr = hook_data->func_pointer; - if (func_ptr != NULL) { + if (func_ptr != nullptr) { //DEBUG_FUNCTION_LINE("function pointer is %08x\n",func_ptr); if (hook_type == WUPS_LOADER_HOOK_INIT_PLUGIN) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_DEINIT_PLUGIN) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_APPLICATION_START) { wups_loader_app_started_args_t args; ((void (*)(wups_loader_app_started_args_t)) ((uint32_t *) func_ptr))(args); } else if (hook_type == WUPS_LOADER_HOOK_FUNCTIONS_PATCHED) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_APPLICATION_END) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_INIT_WUT_MALLOC) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_FINI_WUT_MALLOC) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_FINI_WUT_DEVOPTAB) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_INIT_WUT_NEWLIB) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_FINI_WUT_NEWLIB) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_INIT_WUT_STDCPP) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_FINI_WUT_STDCPP) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_RELEASE_FOREGROUND) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_ACQUIRED_FOREGROUND) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else if (hook_type == WUPS_LOADER_HOOK_VSYNC) { - ((void (*)(void)) ((uint32_t *) func_ptr))(); + ((void (*)()) ((uint32_t *) func_ptr))(); } else { DEBUG_FUNCTION_LINE("######################################"); DEBUG_FUNCTION_LINE("Hook is not implemented %s [%d]", hook_names[hook_type], hook_type); diff --git a/source/main.cpp b/source/main.cpp index cd3d4c9..c8e9624 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,34 +1,28 @@ -#include #include +#include #include -#include #include -#include "plugin/PluginDataFactory.h" -#include "plugin/PluginContainerPersistence.h" -#include "plugin/PluginInformationFactory.h" -#include "plugin/PluginMetaInformationFactory.h" -#include "utils/utils.h" - -#include "PluginManagement.h" +#include "plugin/PluginContainer.h" #include "globals.h" -#include -#include -#include -#include +#include "plugin/PluginDataFactory.h" +#include "plugin/PluginDataPersistence.h" +#include "plugin/PluginContainerPersistence.h" +#include "PluginManagement.h" +#include "utils/logger.h" WUMS_MODULE_EXPORT_NAME("homebrew_wupsbackend"); std::vector loadPlugins(const std::vector &pluginList, MEMHeapHandle heapHandle); -module_information_t *gModuleData = NULL; WUMS_INITIALIZE(args) { __init_wut(); WHBLogUdpInit(); + gModuleData = args.module_information; - if(gModuleData == NULL){ + if (gModuleData == nullptr) { OSFatal("WUPS-Backend: Failed to get gModuleData pointer."); } - if(gModuleData->version != MODULE_INFORMATION_VERSION){ + if (gModuleData->version != MODULE_INFORMATION_VERSION) { OSFatal("WUPS-Backend: The module information struct version does not match."); } WHBLogPrintf("Init successful"); @@ -43,8 +37,7 @@ WUMS_APPLICATION_STARTS() { return; } bool initNeeded = false; - if (pluginDataHeap == NULL) { - + if (pluginDataHeap == nullptr) { DEBUG_FUNCTION_LINE("gModuleData = %08X", gModuleData); DCFlushRange((void *) gModuleData, sizeof(module_information_t)); uint32_t endAddress = 0; diff --git a/source/patcher/hooks_patcher_static.cpp b/source/patcher/hooks_patcher_static.cpp index fc85499..eae3611 100644 --- a/source/patcher/hooks_patcher_static.cpp +++ b/source/patcher/hooks_patcher_static.cpp @@ -1,14 +1,13 @@ -#include "utils/logger.h" #include "hooks_patcher_static.h" #include #include #include #include #include -#include -#include "hooks.h" -extern plugin_information_t *gPluginInformation; +#include "../utils/logger.h" +#include "../globals.h" +#include "../hooks.h" DECL_FUNCTION(void, GX2WaitForVsync, void) { CallHook(gPluginInformation, WUPS_LOADER_HOOK_VSYNC); @@ -251,7 +250,7 @@ static uint32_t lastData0 = 0; DECL_FUNCTION(uint32_t, OSReceiveMessage, OSMessageQueue *queue, OSMessage *message, uint32_t flags) { int32_t res = real_OSReceiveMessage(queue, message, flags); if (queue == OSGetSystemMessageQueue()) { - if (message != NULL && res) { + if (message != nullptr && res) { if (lastData0 != message->args[0]) { if (message->args[0] == 0xFACEF000) { CallHook(gPluginInformation, WUPS_LOADER_HOOK_ACQUIRED_FOREGROUND); diff --git a/source/plugin/DynamicLinkingHelper.cpp b/source/plugin/DynamicLinkingHelper.cpp index e7f3e9f..c4ae45d 100644 --- a/source/plugin/DynamicLinkingHelper.cpp +++ b/source/plugin/DynamicLinkingHelper.cpp @@ -1,25 +1,21 @@ #include "DynamicLinkingHelper.h" -#include -#include -#include -#include -#include "utils/logger.h" -#include "common/plugin_defines.h" +#include +#include "../utils/logger.h" dyn_linking_function_t *DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_linking_relocation_data_t *data, const char *functionName) { - if (data == NULL) { - return NULL; + if (data == nullptr) { + return nullptr; } - if (functionName == NULL) { - return NULL; + if (functionName == nullptr) { + return nullptr; } - dyn_linking_function_t *result = NULL; + dyn_linking_function_t *result = nullptr; for (int32_t i = 0; i < DYN_LINK_FUNCTION_LIST_LENGTH; i++) { dyn_linking_function_t *curEntry = &(data->functions[i]); if (strlen(curEntry->functionName) == 0) { if (strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) { DEBUG_FUNCTION_LINE("Failed to add function name, it's too long.\n"); - return NULL; + return nullptr; } strncpy(curEntry->functionName, functionName, DYN_LINK_FUNCTION_NAME_LENGTH); result = curEntry; @@ -42,16 +38,16 @@ dyn_linking_import_t *DynamicLinkingHelper::getOrAddDataImportByName(dyn_linking } dyn_linking_import_t *DynamicLinkingHelper::getOrAddImport(dyn_linking_relocation_data_t *data, const char *importName, bool isData) { - if (importName == NULL || data == NULL) { - return NULL; + if (importName == nullptr || data == nullptr) { + return nullptr; } - dyn_linking_import_t *result = NULL; + dyn_linking_import_t *result = nullptr; for (int32_t i = 0; i < DYN_LINK_IMPORT_LIST_LENGTH; i++) { dyn_linking_import_t *curEntry = &(data->imports[i]); if (strlen(curEntry->importName) == 0) { if (strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) { DEBUG_FUNCTION_LINE("Failed to add Import, it's too long.\n"); - return NULL; + return nullptr; } strncpy(curEntry->importName, importName, DYN_LINK_IMPORT_NAME_LENGTH); curEntry->isData = isData; @@ -73,13 +69,13 @@ bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t *link bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t *linking_data, dyn_linking_relocation_entry_t *linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, const void *destination, const std::string &name, const ImportRPLInformation &rplInfo) { dyn_linking_import_t *importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo.getName().c_str(), rplInfo.isData()); - if (importInfoGbl == NULL) { + if (importInfoGbl == nullptr) { DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d rpl files to import reached.\n", DYN_LINK_IMPORT_LIST_LENGTH); return false; } dyn_linking_function_t *functionInfo = DynamicLinkingHelper::getOrAddFunctionEntryByName(linking_data, name.c_str()); - if (functionInfo == NULL) { + if (functionInfo == nullptr) { DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d function to be relocated reached.\n", DYN_LINK_FUNCTION_LIST_LENGTH); return false; } @@ -91,7 +87,7 @@ bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_entry_t *lin dyn_linking_import_t *importInfo) { for (uint32_t i = 0; i < linking_entry_length; i++) { dyn_linking_relocation_entry_t *curEntry = &(linking_entries[i]); - if (curEntry->functionEntry != NULL) { + if (curEntry->functionEntry != nullptr) { continue; } curEntry->type = type; diff --git a/source/plugin/DynamicLinkingHelper.h b/source/plugin/DynamicLinkingHelper.h index ecbc702..ea48fab 100644 --- a/source/plugin/DynamicLinkingHelper.h +++ b/source/plugin/DynamicLinkingHelper.h @@ -1,7 +1,6 @@ #pragma once #include -#include "utils/logger.h" #include #include #include "RelocationData.h" @@ -57,9 +56,7 @@ public: dyn_linking_import_t *importInfo); private: - DynamicLinkingHelper() { - } + DynamicLinkingHelper() = default; - ~DynamicLinkingHelper() { - } + ~DynamicLinkingHelper() = default; }; diff --git a/source/plugin/FunctionData.h b/source/plugin/FunctionData.h index a5355d6..d1b2025 100644 --- a/source/plugin/FunctionData.h +++ b/source/plugin/FunctionData.h @@ -33,45 +33,43 @@ public: this->replaceCall = replaceCall; } - ~FunctionData() { + ~FunctionData() = default; - } - - const std::string &getName() const { + [[nodiscard]] const std::string &getName() const { return this->name; } - function_replacement_library_type_t getLibrary() const { + [[nodiscard]] function_replacement_library_type_t getLibrary() const { return this->library; } - const void *getPhysicalAddress() const { + [[nodiscard]] const void *getPhysicalAddress() const { return paddress; } - const void *getVirtualAddress() const { + [[nodiscard]] const void *getVirtualAddress() const { return vaddress; } - const void *getReplaceAddress() const { + [[nodiscard]] const void *getReplaceAddress() const { return replaceAddr; } - const void *getReplaceCall() const { + [[nodiscard]] const void *getReplaceCall() const { return replaceCall; } - const FunctionPatcherTargetProcess getTargetProcess() const { + [[nodiscard]] FunctionPatcherTargetProcess getTargetProcess() const { return targetProcess; } private: - void *paddress = NULL; - void *vaddress = NULL; + void *paddress = nullptr; + void *vaddress = nullptr; std::string name; function_replacement_library_type_t library; FunctionPatcherTargetProcess targetProcess; - void *replaceAddr = NULL; - void *replaceCall = NULL; + void *replaceAddr = nullptr; + void *replaceCall = nullptr; }; diff --git a/source/plugin/HookData.h b/source/plugin/HookData.h index 47ec0c6..599b580 100644 --- a/source/plugin/HookData.h +++ b/source/plugin/HookData.h @@ -28,15 +28,13 @@ public: this->type = type; } - ~HookData() { + ~HookData() = default; - } - - void *getFunctionPointer() const { + [[nodiscard]] void *getFunctionPointer() const { return function_pointer; } - wups_loader_hook_type_t getType() const { + [[nodiscard]] wups_loader_hook_type_t getType() const { return this->type; } diff --git a/source/plugin/ImportRPLInformation.h b/source/plugin/ImportRPLInformation.h index b106a9d..883b3c9 100644 --- a/source/plugin/ImportRPLInformation.h +++ b/source/plugin/ImportRPLInformation.h @@ -18,24 +18,23 @@ #pragma once #include -#include "utils/logger.h" +#include "../utils/logger.h" class ImportRPLInformation { public: - ImportRPLInformation(std::string name, bool isData = false) { + explicit ImportRPLInformation(std::string name, bool isData = false) { this->name = name; this->_isData = isData; } - ~ImportRPLInformation() { - } + ~ImportRPLInformation() = default; - std::string getName() const { + [[nodiscard]] std::string getName() const { return name; } - bool isData() const { + [[nodiscard]] bool isData() const { return _isData; } diff --git a/source/plugin/PluginContainer.h b/source/plugin/PluginContainer.h index 94008a4..bf0c2bb 100644 --- a/source/plugin/PluginContainer.h +++ b/source/plugin/PluginContainer.h @@ -29,11 +29,9 @@ public: this->metaInformation = other.metaInformation; } - PluginContainer() { + PluginContainer() = default; - } - - const PluginMetaInformation &getMetaInformation() const { + [[nodiscard]] const PluginMetaInformation &getMetaInformation() const { return this->metaInformation; } @@ -41,20 +39,20 @@ public: this->metaInformation = metaInfo; } - const PluginInformation &getPluginInformation() const { + [[nodiscard]] const PluginInformation &getPluginInformation() const { return pluginInformation; } - void setPluginInformation(const PluginInformation &pluginInformation) { - this->pluginInformation = pluginInformation; + void setPluginInformation(const PluginInformation &_pluginInformation) { + this->pluginInformation = _pluginInformation; } - const PluginData &getPluginData() const { + [[nodiscard]] const PluginData &getPluginData() const { return pluginData; } - void setPluginData(const PluginData &pluginData) { - this->pluginData = pluginData; + void setPluginData(const PluginData &_pluginData) { + this->pluginData = _pluginData; } PluginData pluginData; diff --git a/source/plugin/PluginContainerPersistence.cpp b/source/plugin/PluginContainerPersistence.cpp index 1490ce8..00d3e0a 100644 --- a/source/plugin/PluginContainerPersistence.cpp +++ b/source/plugin/PluginContainerPersistence.cpp @@ -6,7 +6,7 @@ #include "PluginContainerPersistence.h" #include "PluginDataPersistence.h" #include "DynamicLinkingHelper.h" -#include "common/plugin_defines.h" +#include "../common/plugin_defines.h" #include "PluginInformation.h" #include "RelocationData.h" @@ -28,7 +28,7 @@ bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformat //plugin_data = {}; memset((void *) plugin_data, 0, sizeof(plugin_information_single_t)); - auto pluginMetaInfo = plugin.getMetaInformation(); + const auto &pluginMetaInfo = plugin.getMetaInformation(); auto plugin_meta_data = &plugin_data->meta; if (pluginMetaInfo.getName().size() >= MAXIMUM_PLUGIN_META_FIELD_LENGTH) { @@ -180,7 +180,7 @@ bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformat std::vector PluginContainerPersistence::loadPlugins(plugin_information_t *pluginInformation) { std::vector result; - if (pluginInformation == NULL) { + if (pluginInformation == nullptr) { DEBUG_FUNCTION_LINE("pluginInformation == NULL"); return result; } @@ -211,19 +211,20 @@ std::vector PluginContainerPersistence::loadPlugins(plugin_info PluginData pluginData = PluginDataPersistence::load(data); - PluginInformation pluginInformation; + PluginInformation curPluginInformation; - pluginInformation.setTrampolinId(plugin_data->info.trampolinId); - pluginInformation.allocatedTextMemoryAddress = plugin_data->info.allocatedTextMemoryAddress; - pluginInformation.allocatedDataMemoryAddress = plugin_data->info.allocatedDataMemoryAddress; + curPluginInformation.setTrampolinId(plugin_data->info.trampolinId); + curPluginInformation.allocatedTextMemoryAddress = plugin_data->info.allocatedTextMemoryAddress; + curPluginInformation.allocatedDataMemoryAddress = plugin_data->info.allocatedDataMemoryAddress; - for (uint32_t i = 0; i < MAXIMUM_PLUGIN_SECTION_LENGTH; i++) { - plugin_section_info_t *sectionInfo = &(plugin_data->info.sectionInfos[i]); + for (auto & curItem : plugin_data->info.sectionInfos) { + plugin_section_info_t *sectionInfo = &curItem; if (sectionInfo->addr == 0 && sectionInfo->size == 0) { continue; } DEBUG_FUNCTION_LINE("Add SectionInfo %s", sectionInfo->name); - pluginInformation.addSectionInfo(SectionInfo(sectionInfo->name, sectionInfo->addr, sectionInfo->size)); + std::string name(sectionInfo->name); + curPluginInformation.addSectionInfo(SectionInfo(name, sectionInfo->addr, sectionInfo->size)); } /* load hook data */ @@ -237,7 +238,7 @@ std::vector PluginContainerPersistence::loadPlugins(plugin_info for (uint32_t j = 0; j < hookCount; j++) { replacement_data_hook_t *hook_entry = &(plugin_data->info.hooks[j]); HookData curHook(hook_entry->func_pointer, hook_entry->type); - pluginInformation.addHookData(curHook); + curPluginInformation.addHookData(curHook); } /* load function replacement data */ @@ -251,43 +252,44 @@ std::vector PluginContainerPersistence::loadPlugins(plugin_info for (uint32_t j = 0; j < functionReplaceCount; j++) { function_replacement_data_t *entry = &(plugin_data->info.functions[j]); FunctionData func((void *) entry->physicalAddr, (void *) entry->virtualAddr, entry->function_name, (function_replacement_library_type_t) entry->library, (void *) entry->replaceAddr, (void *) entry->replaceCall, entry->targetProcess); - pluginInformation.addFunctionData(func); + curPluginInformation.addFunctionData(func); } /* load relocation data */ - for (uint32_t j = 0; j < PLUGIN_DYN_LINK_RELOCATION_LIST_LENGTH; j++) { - dyn_linking_relocation_entry_t *linking_entry = &(plugin_data->info.linking_entries[j]); - if (linking_entry->destination == NULL) { + for (auto & linking_entrie : plugin_data->info.linking_entries) { + dyn_linking_relocation_entry_t *linking_entry = &linking_entrie; + if (linking_entry->destination == nullptr) { break; } dyn_linking_import_t *importEntry = linking_entry->importEntry; - if (importEntry == NULL) { + if (importEntry == nullptr) { DEBUG_FUNCTION_LINE("importEntry was NULL, skipping relocation entry"); continue; } - if (importEntry->importName == NULL) { + if (importEntry->importName == nullptr) { DEBUG_FUNCTION_LINE("importEntry->importName was NULL, skipping relocation entry"); continue; } dyn_linking_function_t *functionEntry = linking_entry->functionEntry; - if (functionEntry == NULL) { + if (functionEntry == nullptr) { DEBUG_FUNCTION_LINE("functionEntry was NULL, skipping relocation entry"); continue; } - if (functionEntry->functionName == NULL) { + if (functionEntry->functionName == nullptr) { DEBUG_FUNCTION_LINE("functionEntry->functionName was NULL, skipping relocation entry"); continue; } ImportRPLInformation rplInfo(importEntry->importName, importEntry->isData); - RelocationData reloc(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionEntry->functionName, rplInfo); - pluginInformation.addRelocationData(reloc); + std::string functionName(functionEntry->functionName); + RelocationData reloc(linking_entry->type, linking_entry->offset, linking_entry->addend, linking_entry->destination, functionName, rplInfo); + curPluginInformation.addRelocationData(reloc); } PluginContainer container; container.setMetaInformation(metaInformation); container.setPluginData(pluginData); - container.setPluginInformation(pluginInformation); + container.setPluginInformation(curPluginInformation); result.push_back(container); } return result; diff --git a/source/plugin/PluginContainerPersistence.h b/source/plugin/PluginContainerPersistence.h index 5d6b26e..1cccf5f 100644 --- a/source/plugin/PluginContainerPersistence.h +++ b/source/plugin/PluginContainerPersistence.h @@ -1,6 +1,6 @@ #pragma once -#include "common/plugin_defines.h" +#include "../common/plugin_defines.h" #include "PluginContainer.h" class PluginContainerPersistence { @@ -8,6 +8,4 @@ public: static bool savePlugin(plugin_information_t *pluginInformation, PluginContainer &plugin); static std::vector loadPlugins(plugin_information_t *pluginInformation); - - static bool savePluginData(plugin_data_t *pluginDataStruct, PluginData &plugin); }; diff --git a/source/plugin/PluginData.cpp b/source/plugin/PluginData.cpp index 0d5c49b..ac20e85 100644 --- a/source/plugin/PluginData.cpp +++ b/source/plugin/PluginData.cpp @@ -1,5 +1,8 @@ #include "PluginData.h" +#include +#include +#include "../utils/logger.h" PluginData::PluginData(const PluginData &obj) { this->buffer = obj.buffer; @@ -9,7 +12,7 @@ PluginData::PluginData(const PluginData &obj) { } void PluginData::freeMemory() { - if (buffer == NULL) { + if (buffer == nullptr) { return; } @@ -17,29 +20,29 @@ void PluginData::freeMemory() { default: case eMemTypeExpHeap: MEMFreeToExpHeap(this->heapHandle, buffer); - this->buffer = NULL; + this->buffer = nullptr; break; case eMemTypeMEM2: free(this->buffer); - this->buffer = NULL; + this->buffer = nullptr; break; } } -PluginData::PluginData(std::vector buffer) : PluginData(buffer, 0, eMemTypeMEM2) { +PluginData::PluginData(const std::vector& buffer) : PluginData(buffer, nullptr, eMemTypeMEM2) { } -PluginData::PluginData(std::vector input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) : +PluginData::PluginData(const std::vector& input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) : heapHandle(heapHandle), memoryType(memoryType), length(input.size()) { - void *data_copy = NULL; + void *data_copy = nullptr; switch (memoryType) { default: case eMemTypeExpHeap: data_copy = MEMAllocFromExpHeapEx(heapHandle, length, 4); DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", length / 1024); - if (data_copy == NULL) { + if (data_copy == nullptr) { DEBUG_FUNCTION_LINE("Failed to allocate space on exp heap"); } else { memcpy(data_copy, &input[0], length); @@ -49,7 +52,7 @@ PluginData::PluginData(std::vector input, MEMHeapHandle heapHandle, eMe break; case eMemTypeMEM2: data_copy = memalign(length, 4); - if (data_copy == NULL) { + if (data_copy == nullptr) { DEBUG_FUNCTION_LINE("Failed to allocate space on default heap"); } else { memcpy(data_copy, &input[0], length); @@ -57,8 +60,4 @@ PluginData::PluginData(std::vector input, MEMHeapHandle heapHandle, eMe this->buffer = data_copy; break; } -} - -std::optional PluginData::createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length) { - return std::nullopt; -} +} \ No newline at end of file diff --git a/source/plugin/PluginData.h b/source/plugin/PluginData.h index f4a5d8d..ff25c55 100644 --- a/source/plugin/PluginData.h +++ b/source/plugin/PluginData.h @@ -22,7 +22,7 @@ #include #include -#include "elfio/elfio.hpp" +#include "../elfio/elfio.hpp" using namespace ELFIO; @@ -33,28 +33,23 @@ enum eMemoryTypes { class PluginData { public: - ~PluginData() { - } + ~PluginData() = default; void freeMemory(); PluginData(const PluginData &obj); - PluginData() { - } + PluginData() = default; - void *buffer = NULL; - MEMHeapHandle heapHandle; + void *buffer = nullptr; + MEMHeapHandle heapHandle{}; eMemoryTypes memoryType; size_t length = 0; private: - PluginData(std::vector buffer); - - PluginData(std::vector input, MEMHeapHandle heapHandle, eMemoryTypes memoryType); - - static std::optional createFromExistingData(const void *buffer, MEMHeapHandle heapHandle, eMemoryTypes memoryType, const size_t length); + explicit PluginData(const std::vector &buffer); + PluginData(const std::vector& input, MEMHeapHandle heapHandle, eMemoryTypes memoryType); friend class PluginDataFactory; diff --git a/source/plugin/PluginDataFactory.cpp b/source/plugin/PluginDataFactory.cpp index c79185e..8a2148b 100644 --- a/source/plugin/PluginDataFactory.cpp +++ b/source/plugin/PluginDataFactory.cpp @@ -15,32 +15,30 @@ * along with this program. If not, see . ****************************************************************************/ #include -#include -#include #include #include #include "PluginDataFactory.h" -#include "utils/logger.h" -#include "utils/StringTools.h" +#include "../utils/logger.h" +#include "../utils/StringTools.h" std::vector PluginDataFactory::loadDir(const std::string &path, MEMHeapHandle heapHandle) { std::vector result; struct dirent *dp; - DIR *dfd = NULL; + DIR *dfd = nullptr; if (path.empty()) { DEBUG_FUNCTION_LINE("Path was empty\n"); return result; } - if ((dfd = opendir(path.c_str())) == NULL) { + if ((dfd = opendir(path.c_str())) == nullptr) { DEBUG_FUNCTION_LINE("Couldn't open dir %s\n", path.c_str()); return result; } - while ((dp = readdir(dfd)) != NULL) { - struct stat stbuf; + while ((dp = readdir(dfd)) != nullptr) { + struct stat stbuf{}; std::string full_file_path = StringTools::strfmt("%s/%s", path.c_str(), dp->d_name); StringTools::RemoveDoubleSlashs(full_file_path); if (stat(full_file_path.c_str(), &stbuf) == -1) { @@ -58,7 +56,7 @@ std::vector PluginDataFactory::loadDir(const std::string &path, MEMH } } } - if (dfd != NULL) { + if (dfd != nullptr) { closedir(dfd); } diff --git a/source/plugin/PluginDataPersistence.cpp b/source/plugin/PluginDataPersistence.cpp index 64506da..44b2b48 100644 --- a/source/plugin/PluginDataPersistence.cpp +++ b/source/plugin/PluginDataPersistence.cpp @@ -1,9 +1,9 @@ -#include +#include "../common/plugin_defines.h" #include "PluginDataPersistence.h" #include "PluginData.h" bool PluginDataPersistence::save(plugin_data_t *pluginDataStruct, PluginData &plugin) { - if (pluginDataStruct == NULL) { + if (pluginDataStruct == nullptr) { return false; } pluginDataStruct->buffer = (char *) plugin.buffer; diff --git a/source/plugin/PluginInformation.cpp b/source/plugin/PluginInformation.cpp index dfedbf5..beeb90c 100644 --- a/source/plugin/PluginInformation.cpp +++ b/source/plugin/PluginInformation.cpp @@ -1,14 +1,14 @@ #include "PluginInformation.h" PluginInformation::PluginInformation(const PluginInformation &other) { - for (size_t i = 0; i < other.hook_data_list.size(); i++) { - hook_data_list.push_back(other.hook_data_list[i]); + for (const auto &i : other.hook_data_list) { + hook_data_list.push_back(i); } - for (size_t i = 0; i < other.function_data_list.size(); i++) { - function_data_list.push_back(other.function_data_list[i]); + for (const auto &i : other.function_data_list) { + function_data_list.push_back(i); } - for (size_t i = 0; i < other.relocation_data_list.size(); i++) { - relocation_data_list.push_back(other.relocation_data_list[i]); + for (const auto &i : other.relocation_data_list) { + relocation_data_list.push_back(i); } section_info_list = other.section_info_list; trampolinId = other.trampolinId; diff --git a/source/plugin/PluginInformation.h b/source/plugin/PluginInformation.h index ea3bb44..9ef1d54 100644 --- a/source/plugin/PluginInformation.h +++ b/source/plugin/PluginInformation.h @@ -31,17 +31,15 @@ class PluginInformation { public: PluginInformation(const PluginInformation &other); - PluginInformation() { - } + PluginInformation() = default; - virtual ~PluginInformation() { - } + virtual ~PluginInformation() = default; void addHookData(const HookData &hook_data) { hook_data_list.push_back(hook_data); } - const std::vector &getHookDataList() const { + [[nodiscard]] const std::vector &getHookDataList() const { return hook_data_list; } @@ -49,7 +47,7 @@ public: function_data_list.push_back(function_data); } - const std::vector &getFunctionDataList() const { + [[nodiscard]] const std::vector &getFunctionDataList() const { return function_data_list; } @@ -57,7 +55,7 @@ public: relocation_data_list.push_back(relocation_data); } - const std::vector &getRelocationDataList() const { + [[nodiscard]] const std::vector &getRelocationDataList() const { return relocation_data_list; } @@ -65,27 +63,26 @@ public: section_info_list[sectionInfo.getName()] = sectionInfo; } - const std::map &getSectionInfoList() const { + [[nodiscard]] const std::map &getSectionInfoList() const { return section_info_list; } - std::optional getSectionInfo(const std::string §ionName) const { + [[nodiscard]] std::optional getSectionInfo(const std::string §ionName) const { if (getSectionInfoList().count(sectionName) > 0) { return section_info_list.at(sectionName); } return std::nullopt; } - void setTrampolinId(uint8_t trampolinId) { - this->trampolinId = trampolinId; + void setTrampolinId(uint8_t _trampolinId) { + this->trampolinId = _trampolinId; } - uint8_t getTrampolinId() const { + [[nodiscard]] uint8_t getTrampolinId() const { return trampolinId; } private: - std::vector hook_data_list; std::vector function_data_list; std::vector relocation_data_list; @@ -93,8 +90,8 @@ private: uint8_t trampolinId = 0; - void *allocatedTextMemoryAddress = 0; - void *allocatedDataMemoryAddress = 0; + void *allocatedTextMemoryAddress = nullptr; + void *allocatedDataMemoryAddress = nullptr; friend class PluginInformationFactory; diff --git a/source/plugin/PluginInformationFactory.cpp b/source/plugin/PluginInformationFactory.cpp index 60f9cdf..a857b47 100644 --- a/source/plugin/PluginInformationFactory.cpp +++ b/source/plugin/PluginInformationFactory.cpp @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (C) 2018 Maschell + * Copyright (C) 2018-2020 Maschell * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,20 +26,20 @@ #include "PluginInformationFactory.h" #include "HookData.h" #include "SectionInfo.h" -#include "elfio/elfio.hpp" -#include "utils/utils.h" -#include "utils/ElfUtils.h" -#include "utils/StringTools.h" +#include "../elfio/elfio.hpp" +#include "../utils/utils.h" +#include "../utils/ElfUtils.h" +#include "../utils/StringTools.h" using namespace ELFIO; std::optional PluginInformationFactory::load(const PluginData &pluginData, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length, uint8_t trampolinId) { - if(pluginData.buffer == NULL){ + if (pluginData.buffer == nullptr) { DEBUG_FUNCTION_LINE("Buffer was NULL"); return std::nullopt; } elfio reader; - if (! reader.load((char*) pluginData.buffer, pluginData.length)) { + if (!reader.load((char *) pluginData.buffer, pluginData.length)) { DEBUG_FUNCTION_LINE("Can't process PluginData in elfio"); return std::nullopt; } @@ -47,7 +47,7 @@ std::optional PluginInformationFactory::load(const PluginData PluginInformation pluginInfo; uint32_t sec_num = reader.sections.size(); - uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num); + auto **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num); uint32_t totalSize = 0; @@ -74,14 +74,14 @@ std::optional PluginInformationFactory::load(const PluginData } } void *text_data = MEMAllocFromExpHeapEx(heapHandle, text_size, 0x1000); - if (text_data == NULL) { + if (text_data == nullptr) { DEBUG_FUNCTION_LINE("Failed to alloc memory for the .text section (%d bytes)\n", text_size); return std::nullopt; } DEBUG_FUNCTION_LINE("Allocated %d kb from ExpHeap", text_size / 1024); void *data_data = MEMAllocFromExpHeapEx(heapHandle, data_size, 0x1000); - if (data_data == NULL) { + if (data_data == nullptr) { DEBUG_FUNCTION_LINE("Failed to alloc memory for the .data section (%d bytes)\n", data_size); MEMFreeToExpHeap(heapHandle, text_data); @@ -133,7 +133,8 @@ std::optional PluginInformationFactory::load(const PluginData memcpy((void *) destination, p, sectionSize); } - pluginInfo.addSectionInfo(SectionInfo(psec->get_name(), destination, sectionSize)); + std::string sectionName(psec->get_name()); + pluginInfo.addSectionInfo(SectionInfo(sectionName, destination, sectionSize)); DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); totalSize += sectionSize; @@ -177,8 +178,8 @@ std::optional PluginInformationFactory::load(const PluginData std::optional secInfo = pluginInfo.getSectionInfo(".wups.hooks"); if (secInfo && secInfo->getSize() > 0) { size_t entries_count = secInfo->getSize() / sizeof(wups_loader_hook_t); - wups_loader_hook_t *entries = (wups_loader_hook_t *) secInfo->getAddress(); - if (entries != NULL) { + auto *entries = (wups_loader_hook_t *) secInfo->getAddress(); + if (entries != nullptr) { for (size_t j = 0; j < entries_count; j++) { wups_loader_hook_t *hook = &entries[j]; DEBUG_FUNCTION_LINE("Saving hook of plugin Type: %08X, target: %08X"/*,pluginData.getPluginInformation()->getName().c_str()*/, hook->type, (void *) hook->target); @@ -191,7 +192,7 @@ std::optional PluginInformationFactory::load(const PluginData secInfo = pluginInfo.getSectionInfo(".wups.load"); if (secInfo && secInfo->getSize() > 0) { size_t entries_count = secInfo->getSize() / sizeof(wups_loader_entry_t); - wups_loader_entry_t *entries = (wups_loader_entry_t *) secInfo->getAddress(); + auto *entries = (wups_loader_entry_t *) secInfo->getAddress(); if (entries != NULL) { for (size_t j = 0; j < entries_count; j++) { wups_loader_entry_t *cur_function = &entries[j]; diff --git a/source/plugin/PluginInformationFactory.h b/source/plugin/PluginInformationFactory.h index 5708230..a0596ca 100644 --- a/source/plugin/PluginInformationFactory.h +++ b/source/plugin/PluginInformationFactory.h @@ -25,7 +25,7 @@ #include #include "PluginInformation.h" #include "PluginContainer.h" -#include "elfio/elfio.hpp" +#include "../elfio/elfio.hpp" class PluginInformationFactory { public: diff --git a/source/plugin/PluginMetaInformation.h b/source/plugin/PluginMetaInformation.h index 167c8b5..604c4d6 100644 --- a/source/plugin/PluginMetaInformation.h +++ b/source/plugin/PluginMetaInformation.h @@ -24,64 +24,63 @@ class PluginMetaInformation { public: PluginMetaInformation(const PluginMetaInformation &other); - const std::string getName() const { + [[nodiscard]] std::string getName() const { return name; } - const std::string getAuthor() const { + [[nodiscard]] std::string getAuthor() const { return this->author; } - const std::string getVersion() const { + [[nodiscard]] std::string getVersion() const { return this->version; } - const std::string getLicense() const { + [[nodiscard]] std::string getLicense() const { return this->license; } - const std::string getBuildTimestamp() const { + [[nodiscard]] std::string getBuildTimestamp() const { return this->buildtimestamp; } - const std::string getDescription() const { + [[nodiscard]] std::string getDescription() const { return this->description; } - const size_t getSize() const { + [[nodiscard]] size_t getSize() const { return this->size; } private: - PluginMetaInformation() { + PluginMetaInformation() = default; + + void setName(const std::string &_name) { + this->name = _name; } - void setName(const std::string &name) { - this->name = name; + void setAuthor(const std::string &_author) { + this->author = _author; } - void setAuthor(const std::string &author) { - this->author = author; + void setVersion(const std::string &_version) { + this->version = _version; } - void setVersion(const std::string &version) { - this->version = version; + void setLicense(const std::string &_license) { + this->license = _license; } - void setLicense(const std::string &license) { - this->license = license; + void setBuildTimestamp(const std::string &_buildtimestamp) { + this->buildtimestamp = _buildtimestamp; } - void setBuildTimestamp(const std::string &buildtimestamp) { - this->buildtimestamp = buildtimestamp; + void setDescription(const std::string &_description) { + this->description = _description; } - void setDescription(const std::string &description) { - this->description = description; - } - - void setSize(size_t size) { - this->size = size; + void setSize(size_t _size) { + this->size = _size; } std::string name; @@ -90,7 +89,7 @@ private: std::string license; std::string buildtimestamp; std::string description; - size_t size; + size_t size{}; friend class PluginMetaInformationFactory; diff --git a/source/plugin/PluginMetaInformationFactory.cpp b/source/plugin/PluginMetaInformationFactory.cpp index a3e02d5..7d1356c 100644 --- a/source/plugin/PluginMetaInformationFactory.cpp +++ b/source/plugin/PluginMetaInformationFactory.cpp @@ -19,27 +19,28 @@ #include #include #include -#include "utils/StringTools.h" +#include "../utils/StringTools.h" #include "PluginMetaInformationFactory.h" #include "PluginMetaInformation.h" -#include "elfio/elfio.hpp" +#include "../elfio/elfio.hpp" +#include "../utils/logger.h" using namespace ELFIO; std::optional PluginMetaInformationFactory::loadPlugin(const PluginData &pluginData) { - if(pluginData.buffer == NULL){ + if (pluginData.buffer == NULL) { DEBUG_FUNCTION_LINE("Buffer was NULL"); return std::nullopt; } elfio reader; - if (! reader.load((char*) pluginData.buffer, pluginData.length)) { + if (!reader.load((char *) pluginData.buffer, pluginData.length)) { DEBUG_FUNCTION_LINE("Can't process PluginData in elfio"); return std::nullopt; } return loadPlugin(reader); } -std::optional PluginMetaInformationFactory::loadPlugin(const std::string filePath) { +std::optional PluginMetaInformationFactory::loadPlugin(std::string &filePath) { elfio reader; if (!reader.load(filePath)) { DEBUG_FUNCTION_LINE("Can't find or process ELF file\n"); diff --git a/source/plugin/PluginMetaInformationFactory.h b/source/plugin/PluginMetaInformationFactory.h index 6c8de5e..e74e39b 100644 --- a/source/plugin/PluginMetaInformationFactory.h +++ b/source/plugin/PluginMetaInformationFactory.h @@ -27,7 +27,7 @@ class PluginMetaInformationFactory { public: static std::optional loadPlugin(const PluginData &pluginData); - static std::optional loadPlugin(const std::string filePath); + static std::optional loadPlugin(std::string &filePath); static std::optional loadPlugin(char *buffer, size_t size); diff --git a/source/plugin/RelocationData.h b/source/plugin/RelocationData.h index 370b508..54d10cf 100644 --- a/source/plugin/RelocationData.h +++ b/source/plugin/RelocationData.h @@ -23,7 +23,7 @@ class RelocationData { public: - RelocationData(const char type, size_t offset, int32_t addend, void *destination, std::string name, ImportRPLInformation rplInfo) : + RelocationData(const char type, size_t offset, int32_t addend, void *destination, std::string &name, const ImportRPLInformation &rplInfo) : type(type), offset(offset), addend(addend), @@ -32,39 +32,31 @@ public: rplInfo(rplInfo) { } - RelocationData(const RelocationData &o2) : - type(o2.type), - offset(o2.offset), - addend(o2.addend), - destination(o2.destination), - name(o2.name), - rplInfo(o2.rplInfo) { - } + RelocationData(const RelocationData &o2) = default; - virtual ~RelocationData() { - } + virtual ~RelocationData() = default; - const char getType() const { + [[nodiscard]] char getType() const { return type; } - const size_t getOffset() const { + [[nodiscard]] size_t getOffset() const { return offset; } - const int32_t getAddend() const { + [[nodiscard]] int32_t getAddend() const { return addend; } - const void *getDestination() const { + [[nodiscard]] const void *getDestination() const { return destination; } - const std::string &getName() const { + [[nodiscard]] const std::string &getName() const { return name; } - const ImportRPLInformation &getImportRPLInformation() const { + [[nodiscard]] const ImportRPLInformation &getImportRPLInformation() const { return rplInfo; } diff --git a/source/plugin/SectionInfo.h b/source/plugin/SectionInfo.h index c7ea72b..b8c4a13 100644 --- a/source/plugin/SectionInfo.h +++ b/source/plugin/SectionInfo.h @@ -22,40 +22,33 @@ class SectionInfo { public: - SectionInfo(std::string name, uint32_t address, uint32_t sectionSize) : + SectionInfo(std::string &name, uint32_t address, uint32_t sectionSize) : name(name), address(address), sectionSize(sectionSize) { } - SectionInfo() { - } + SectionInfo() = default; - SectionInfo(const SectionInfo &o2) : - name(o2.name), - address(o2.address), - sectionSize(o2.sectionSize) { - } + SectionInfo(const SectionInfo &o2) = default; - virtual ~SectionInfo() { + virtual ~SectionInfo() = default; - } - - const std::string &getName() const { + [[nodiscard]] const std::string &getName() const { return name; } - const uint32_t getAddress() const { + [[nodiscard]] uint32_t getAddress() const { return address; } - const uint32_t getSize() const { + [[nodiscard]] uint32_t getSize() const { return sectionSize; } private: std::string name; - uint32_t address; - uint32_t sectionSize; + uint32_t address{}; + uint32_t sectionSize{}; }; diff --git a/source/utils/exports.cpp b/source/utils/exports.cpp index c82f2cb..afc5d0d 100644 --- a/source/utils/exports.cpp +++ b/source/utils/exports.cpp @@ -1,11 +1,11 @@ #include -#include -#include -#include -#include -#include -#include -#include +#include "../plugin/PluginMetaInformationFactory.h" +#include "../plugin/PluginContainer.h" +#include "../plugin/PluginContainerPersistence.h" +#include "../plugin/PluginDataFactory.h" +#include "../PluginManagement.h" +#include "../globals.h" +#include "../plugin/PluginDataPersistence.h" #include "exports.h" #include @@ -19,7 +19,7 @@ void fillPluginInformation(plugin_information *out, PluginMetaInformation *metaI out->size = metaInformation->getSize(); } -int32_t WUPSLoadAndLinkByDataHandle(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size) { +extern "C" int32_t WUPSLoadAndLinkByDataHandle(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size) { gLinkOnReload.number_used_plugins = 0; if (plugin_data_handle_list != NULL && plugin_data_handle_list_size != 0) { for (uint32_t i = 0; i < plugin_data_handle_list_size; i++) { @@ -40,7 +40,7 @@ int32_t WUPSLoadAndLinkByDataHandle(const plugin_data_handle *plugin_data_handle return ERROR_NONE; } -int32_t WUPSDeletePluginContainer(const plugin_container_handle *handle_list, uint32_t handle_list_size) { +extern "C" int32_t WUPSDeletePluginContainer(const plugin_container_handle *handle_list, uint32_t handle_list_size) { if (handle_list != NULL && handle_list_size != 0) { for (uint32_t i = 0; i < handle_list_size; i++) { auto handle = handle_list[i]; @@ -52,11 +52,11 @@ int32_t WUPSDeletePluginContainer(const plugin_container_handle *handle_list, ui return ERROR_NONE; } -int32_t WUPSDeletePluginData(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size) { - if (plugin_data_handle_list != NULL && plugin_data_handle_list_size != 0) { +extern "C" int32_t WUPSDeletePluginData(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size) { + if (plugin_data_handle_list != nullptr && plugin_data_handle_list_size != 0) { for (uint32_t i = 0; i < plugin_data_handle_list_size; i++) { auto handle = plugin_data_handle_list[i]; - PluginData *pluginData = (PluginData *) handle; + auto *pluginData = (PluginData *) handle; DEBUG_FUNCTION_LINE("delete plugin data %08X", pluginData); delete pluginData; } @@ -64,7 +64,7 @@ int32_t WUPSDeletePluginData(const plugin_data_handle *plugin_data_handle_list, return ERROR_NONE; } -int32_t WUPSLoadPluginAsData(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_data_handle *out) { +extern "C" int32_t WUPSLoadPluginAsData(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_data_handle *out) { std::optional pluginData; if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_PATH) { pluginData = PluginDataFactory::load(path, pluginDataHeap); @@ -81,32 +81,33 @@ int32_t WUPSLoadPluginAsData(GetPluginInformationInputType inputType, const char return ERROR_FAILED_ALLOC; } - if (out == NULL) { + if (out == nullptr) { DEBUG_FUNCTION_LINE("out was NULL"); return ERROR_INVALID_ARG; } else { - PluginData *pluginDataHeap = new PluginData(pluginData.value()); - DEBUG_FUNCTION_LINE("Saving handle %08X", pluginDataHeap); - *out = (uint32_t) pluginDataHeap; + auto *pluginDataHandle = new PluginData(pluginData.value()); + DEBUG_FUNCTION_LINE("Saving handle %08X", pluginDataHandle); + *out = (uint32_t) pluginDataHandle; } return ERROR_NONE; } -int32_t WUPSLoadPluginAsDataByPath(plugin_data_handle *output, const char *path) { - return WUPSLoadPluginAsData(PLUGIN_INFORMATION_INPUT_TYPE_PATH, path, NULL, 0, output); +extern "C" int32_t WUPSLoadPluginAsDataByPath(plugin_data_handle *output, const char *path) { + return WUPSLoadPluginAsData(PLUGIN_INFORMATION_INPUT_TYPE_PATH, path, nullptr, 0, output); } -int32_t WUPSLoadPluginAsDataByBuffer(plugin_data_handle *output, char *buffer, size_t size) { - return WUPSLoadPluginAsData(PLUGIN_INFORMATION_INPUT_TYPE_BUFFER, NULL, buffer, size, output); +extern "C" int32_t WUPSLoadPluginAsDataByBuffer(plugin_data_handle *output, char *buffer, size_t size) { + return WUPSLoadPluginAsData(PLUGIN_INFORMATION_INPUT_TYPE_BUFFER, nullptr, buffer, size, output); } -int32_t WUPSGetPluginMetaInformation(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_information *output) { +extern "C" int32_t WUPSGetPluginMetaInformation(GetPluginInformationInputType inputType, const char* path, char *buffer, size_t size, plugin_information *output) { std::optional pluginInfo; - if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_PATH && path != NULL) { + if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_PATH && path != nullptr) { + std::string pathStr(path); DEBUG_FUNCTION_LINE("PLUGIN_INFORMATION_INPUT_TYPE_PATH %s", path); - pluginInfo = PluginMetaInformationFactory::loadPlugin(path); - } else if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_BUFFER && buffer != NULL && size > 0) { + pluginInfo = PluginMetaInformationFactory::loadPlugin(pathStr); + } else if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_BUFFER && buffer != nullptr && size > 0) { DEBUG_FUNCTION_LINE("PLUGIN_INFORMATION_INPUT_TYPE_BUFFER %08X %d", buffer, size); pluginInfo = PluginMetaInformationFactory::loadPlugin(buffer, size); } else { @@ -120,7 +121,7 @@ int32_t WUPSGetPluginMetaInformation(GetPluginInformationInputType inputType, co DEBUG_FUNCTION_LINE("loaded plugin"); - if (output == NULL) { + if (output == nullptr) { return ERROR_INVALID_ARG; } else { fillPluginInformation(output, &pluginInfo.value()); @@ -128,21 +129,21 @@ int32_t WUPSGetPluginMetaInformation(GetPluginInformationInputType inputType, co return ERROR_NONE; } -int32_t WUPSGetPluginMetaInformationByPath(plugin_information *output, const char *path) { - return WUPSGetPluginMetaInformation(PLUGIN_INFORMATION_INPUT_TYPE_PATH, path, NULL, 0, output); +extern "C" int32_t WUPSGetPluginMetaInformationByPath(plugin_information *output, const char *path) { + return WUPSGetPluginMetaInformation(PLUGIN_INFORMATION_INPUT_TYPE_PATH, path, nullptr, 0, output); } -int32_t WUPSGetPluginMetaInformationByBuffer(plugin_information *output, char *buffer, size_t size) { - return WUPSGetPluginMetaInformation(PLUGIN_INFORMATION_INPUT_TYPE_BUFFER, NULL, buffer, size, output); +extern "C" int32_t WUPSGetPluginMetaInformationByBuffer(plugin_information *output, char *buffer, size_t size) { + return WUPSGetPluginMetaInformation(PLUGIN_INFORMATION_INPUT_TYPE_BUFFER, nullptr, buffer, size, output); } -int32_t WUPSGetPluginDataForContainerHandles(const plugin_container_handle *plugin_container_handle_list, plugin_data_handle *plugin_data_list, uint32_t buffer_size) { +extern "C" int32_t WUPSGetPluginDataForContainerHandles(const plugin_container_handle *plugin_container_handle_list, plugin_data_handle *plugin_data_list, uint32_t buffer_size) { int res = ERROR_NONE; - if (plugin_container_handle_list != NULL && buffer_size != 0) { + if (plugin_container_handle_list != nullptr && buffer_size != 0) { for (uint32_t i = 0; i < buffer_size; i++) { auto handle = plugin_container_handle_list[i]; - PluginContainer *container = (PluginContainer *) handle; - PluginData *pluginData = new PluginData(container->getPluginData()); + auto *container = (PluginContainer *) handle; + auto *pluginData = new PluginData(container->getPluginData()); DEBUG_FUNCTION_LINE("Created pluginData [%08X]", pluginData); plugin_data_list[i] = (uint32_t) pluginData; } @@ -152,12 +153,12 @@ int32_t WUPSGetPluginDataForContainerHandles(const plugin_container_handle *plug return res; } -int32_t WUPSGetMetaInformation(plugin_container_handle *plugin_container_handle_list, plugin_information *plugin_information_list, uint32_t buffer_size) { +extern "C" int32_t WUPSGetMetaInformation(const plugin_container_handle *plugin_container_handle_list, plugin_information *plugin_information_list, uint32_t buffer_size) { int res = ERROR_NONE; - if (plugin_container_handle_list != NULL && buffer_size != 0) { + if (plugin_container_handle_list != nullptr && buffer_size != 0) { for (uint32_t i = 0; i < buffer_size; i++) { auto handle = plugin_container_handle_list[i]; - PluginContainer *container = (PluginContainer *) handle; + auto *container = (PluginContainer *) handle; strncpy(plugin_information_list[i].author, container->metaInformation.getAuthor().c_str(), 255); strncpy(plugin_information_list[i].buildTimestamp, container->metaInformation.getBuildTimestamp().c_str(), 255); @@ -173,12 +174,12 @@ int32_t WUPSGetMetaInformation(plugin_container_handle *plugin_container_handle_ return res; } -int32_t WUPSGetLoadedPlugins(plugin_container_handle *io_handles, uint32_t buffer_size, uint32_t *outSize) { +extern "C" int32_t WUPSGetLoadedPlugins(plugin_container_handle *io_handles, uint32_t buffer_size, uint32_t *outSize) { auto plugins = PluginContainerPersistence::loadPlugins(gPluginInformation); uint32_t counter = 0; for (auto &plugin: plugins) { if (counter < buffer_size) { - PluginContainer *container = new PluginContainer(plugin); + auto *container = new PluginContainer(plugin); DEBUG_FUNCTION_LINE("Created container [%08X]", container); io_handles[counter] = (uint32_t) container; counter++; @@ -186,7 +187,7 @@ int32_t WUPSGetLoadedPlugins(plugin_container_handle *io_handles, uint32_t buffe break; } } - if (outSize != NULL) { + if (outSize != nullptr) { *outSize = counter; } return 0; diff --git a/source/utils/exports.h b/source/utils/exports.h index 6c72f85..baf43ed 100644 --- a/source/utils/exports.h +++ b/source/utils/exports.h @@ -53,7 +53,7 @@ int32_t WUPSGetPluginMetaInformationByBuffer(plugin_information *output, char *b int32_t WUPSGetPluginDataForContainerHandles(const plugin_container_handle *plugin_container_handle_list, plugin_data_handle *plugin_data_list, uint32_t buffer_size); -int32_t WUPSGetMetaInformation(plugin_container_handle *plugin_container_handle_list, plugin_information *plugin_information_list, uint32_t buffer_size); +int32_t WUPSGetMetaInformation(const plugin_container_handle *plugin_container_handle_list, plugin_information *plugin_information_list, uint32_t buffer_size); int32_t WUPSGetLoadedPlugins(plugin_container_handle *io_handles, uint32_t buffer_size, uint32_t *outSize); diff --git a/source/utils/logger.h b/source/utils/logger.h index ceb3111..901689c 100644 --- a/source/utils/logger.h +++ b/source/utils/logger.h @@ -1,6 +1,4 @@ -#ifndef __LOGGER_H_ -#define __LOGGER_H_ - +#pragma once #ifdef __cplusplus extern "C" { #endif @@ -25,6 +23,4 @@ extern "C" { #ifdef __cplusplus } -#endif - -#endif +#endif \ No newline at end of file