plugify 1.0.0.0
Loading...
Searching...
No Matches
assembly.hpp
1#pragma once
2
3#include <string>
4#include <vector>
5#include <filesystem>
6#include <plugify/load_flag.hpp>
7#include <plugify/mem_addr.hpp>
8#include <plugify_export.h>
9
10namespace plugify {
15 class Assembly {
16 public:
21 struct Section {
25 Section() : size{0} {}
26
33 Section(std::string_view sectionName, uintptr_t sectionBase, size_t sectionSize)
34 : name(sectionName), base{sectionBase}, size{sectionSize} {}
35
40 bool IsValid() const noexcept { return base; }
41
42 std::string name{};
44 size_t size;
45 };
46
50 Assembly() : _handle{nullptr} {}
51
56
57 // Delete copy constructor and copy assignment operator.
58 Assembly(const Assembly&) = delete;
59 Assembly& operator=(const Assembly&) = delete;
60 //Assembly& operator=(const Assembly&) && = delete;
61
62 // Delete move constructor and move assignment operator.
63 Assembly(Assembly&& rhs) noexcept = delete;
64 Assembly& operator=(Assembly&& rhs) noexcept = delete;
65 //Assembly& operator=(Assembly&&) && = delete;
66
67 using SearchDirs = std::vector<std::filesystem::path>;
68
76 explicit Assembly(std::string_view moduleName, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false);
77
85 explicit Assembly(const char* moduleName, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false)
86 : Assembly(std::string_view(moduleName), flags, additionalSearchDirectories, sections) {}
87
95 explicit Assembly(const std::string& moduleName, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false)
96 : Assembly(std::string_view(moduleName), flags, additionalSearchDirectories, sections) {}
97
105 explicit Assembly(const std::filesystem::path& modulePath, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false);
106
114 explicit Assembly(MemAddr moduleMemory, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false);
115
121 static std::pair<std::vector<uint8_t>, std::string> PatternToMaskedBytes(std::string_view input);
122
131 MemAddr FindPattern(MemAddr pattern, std::string_view mask, MemAddr startAddress = nullptr, Section* moduleSection = nullptr) const;
132
140 MemAddr FindPattern(std::string_view pattern, MemAddr startAddress = nullptr, Section* moduleSection = nullptr) const;
141
148 MemAddr GetVirtualTableByName(std::string_view tableName, bool decorated = false) const;
149
155 MemAddr GetFunctionByName(std::string_view functionName) const noexcept;
156
162 Section GetSectionByName(std::string_view sectionName) const noexcept;
163
168 void* GetHandle() const noexcept;
169
174 MemAddr GetBase() const noexcept;
175
180 const std::filesystem::path& GetPath() const noexcept;
181
186 const std::string& GetError() const noexcept;
187
192 bool IsValid() const noexcept { return _handle != nullptr; }
193
198 explicit operator bool() const noexcept { return _handle != nullptr; }
199
205 bool operator==(const Assembly& assembly) const noexcept { return _handle == assembly._handle; }
206
207 private:
216 bool Init(std::filesystem::path modulePath, LoadFlag flags, const SearchDirs& additionalSearchDirectories, bool sections);
217
227 bool InitFromName(std::string_view moduleName, LoadFlag flags, const SearchDirs& additionalSearchDirectories, bool sections, bool extension = false);
228
237 bool InitFromMemory(MemAddr moduleMemory, LoadFlag flags, const SearchDirs& additionalSearchDirectories, bool sections);
238
239 private:
240 void* _handle;
241 std::filesystem::path _path;
242 std::string _error;
243 Section _executableCode;
244 std::vector<Section> _sections;
245 };
246
253 int TranslateLoading(LoadFlag flags) noexcept;
254
261 LoadFlag TranslateLoading(int flags) noexcept;
262
263} // namespace plugify
Represents an assembly (module) within a process.
Definition assembly.hpp:15
Assembly()
Default constructor initializing handle to nullptr.
Definition assembly.hpp:50
const std::string & GetError() const noexcept
Returns the module error.
MemAddr GetFunctionByName(std::string_view functionName) const noexcept
Gets an address of a function by its name.
Section GetSectionByName(std::string_view sectionName) const noexcept
Gets a module section by name.
MemAddr FindPattern(MemAddr pattern, std::string_view mask, MemAddr startAddress=nullptr, Section *moduleSection=nullptr) const
Finds an array of bytes in process memory using SIMD instructions.
static std::pair< std::vector< uint8_t >, std::string > PatternToMaskedBytes(std::string_view input)
Converts a string pattern with wildcards to an array of bytes and mask.
void * GetHandle() const noexcept
Returns the module handle.
~Assembly()
Destructor.
Assembly(std::string_view moduleName, LoadFlag flags=LoadFlag::Default, const SearchDirs &additionalSearchDirectories={}, bool sections=false)
Constructs an Assembly object with the specified module name, flags, and sections.
bool operator==(const Assembly &assembly) const noexcept
Equality operator.
Definition assembly.hpp:205
Assembly(const char *moduleName, LoadFlag flags=LoadFlag::Default, const SearchDirs &additionalSearchDirectories={}, bool sections=false)
Constructs an Assembly object with a char pointer as module name.
Definition assembly.hpp:85
Assembly(MemAddr moduleMemory, LoadFlag flags=LoadFlag::Default, const SearchDirs &additionalSearchDirectories={}, bool sections=false)
Constructs an Assembly object with a memory address.
Assembly(const std::filesystem::path &modulePath, LoadFlag flags=LoadFlag::Default, const SearchDirs &additionalSearchDirectories={}, bool sections=false)
Constructs an Assembly object with a filesystem path as module path.
MemAddr FindPattern(std::string_view pattern, MemAddr startAddress=nullptr, Section *moduleSection=nullptr) const
Finds a string pattern in process memory using SIMD instructions.
const std::filesystem::path & GetPath() const noexcept
Returns the module path.
MemAddr GetVirtualTableByName(std::string_view tableName, bool decorated=false) const
Gets an address of a virtual method table by RTTI type descriptor name.
Assembly(const std::string &moduleName, LoadFlag flags=LoadFlag::Default, const SearchDirs &additionalSearchDirectories={}, bool sections=false)
Constructs an Assembly object with a string as module name.
Definition assembly.hpp:95
MemAddr GetBase() const noexcept
Returns the module base address.
bool IsValid() const noexcept
Checks if the assembly is valid.
Definition assembly.hpp:192
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition mem_addr.hpp:11
Represents a section of the assembly.
Definition assembly.hpp:21
Section(std::string_view sectionName, uintptr_t sectionBase, size_t sectionSize)
Parameterized constructor.
Definition assembly.hpp:33
bool IsValid() const noexcept
Checks if the section is valid.
Definition assembly.hpp:40
Section()
Default constructor initializing size to 0.
Definition assembly.hpp:25
std::string name
The name of the section.
Definition assembly.hpp:42
size_t size
The size of the section.
Definition assembly.hpp:44
MemAddr base
The base address of the section.
Definition assembly.hpp:43