plugify 1.2.6
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 operator bool() const noexcept { return base; }
41
42 std::string name{};
44 size_t size;
45 };
46
51 struct Handle {
56 Handle(void* systemHandle) : handle{systemHandle} {}
57
62 operator bool() const noexcept { return handle; }
63
68 operator void*() const noexcept { return handle; }
69
70 void* handle{};
71 };
72
76 Assembly() : _handle{nullptr} {}
77
82
83 // Delete copy constructor and copy assignment operator.
84 Assembly(const Assembly&) = delete;
85 Assembly& operator=(const Assembly&) = delete;
86 //Assembly& operator=(const Assembly&) && = delete;
87
88 // Delete move constructor and move assignment operator.
89 Assembly(Assembly&& rhs) noexcept = delete;
90 Assembly& operator=(Assembly&& rhs) noexcept = delete;
91 //Assembly& operator=(Assembly&&) && = delete;
92
93 using SearchDirs = std::vector<std::filesystem::path>;
94
102 explicit Assembly(std::string_view moduleName, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false);
103
111 explicit Assembly(const char* moduleName, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false)
112 : Assembly(std::string_view(moduleName), flags, additionalSearchDirectories, sections) {}
113
121 explicit Assembly(const std::string& moduleName, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false)
122 : Assembly(std::string_view(moduleName), flags, additionalSearchDirectories, sections) {}
123
131 explicit Assembly(const std::filesystem::path& modulePath, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false);
132
140 explicit Assembly(MemAddr moduleMemory, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false);
141
149 explicit Assembly(Handle moduleHandle, LoadFlag flags = LoadFlag::Default, const SearchDirs& additionalSearchDirectories = {}, bool sections = false);
150
156 static std::pair<std::vector<uint8_t>, std::string> PatternToMaskedBytes(std::string_view input);
157
166 MemAddr FindPattern(MemAddr pattern, std::string_view mask, MemAddr startAddress = nullptr, const Section * moduleSection = nullptr) const;
167
175 MemAddr FindPattern(std::string_view pattern, MemAddr startAddress = nullptr, Section* moduleSection = nullptr) const;
176
183 MemAddr GetVirtualTableByName(std::string_view tableName, bool decorated = false) const;
184
190 MemAddr GetFunctionByName(std::string_view functionName) const noexcept;
191
197 Section GetSectionByName(std::string_view sectionName) const noexcept;
198
203 void* GetHandle() const noexcept;
204
209 MemAddr GetBase() const noexcept;
210
215 const std::filesystem::path& GetPath() const noexcept;
216
221 const std::string& GetError() const noexcept;
222
227 bool IsValid() const noexcept { return _handle != nullptr; }
228
233 explicit operator bool() const noexcept { return _handle != nullptr; }
234
240 bool operator==(const Assembly& assembly) const noexcept { return _handle == assembly._handle; }
241
242 private:
251 bool Init(std::filesystem::path modulePath, LoadFlag flags, const SearchDirs& additionalSearchDirectories, bool sections);
252
262 bool InitFromName(std::string_view moduleName, LoadFlag flags, const SearchDirs& additionalSearchDirectories, bool sections, bool extension = false);
263
272 bool InitFromMemory(MemAddr moduleMemory, LoadFlag flags, const SearchDirs& additionalSearchDirectories, bool sections);
273
282 bool InitFromHandle(Handle moduleHandle, LoadFlag flags, const SearchDirs& additionalSearchDirectories, bool sections);
283
295 bool LoadSections();
296
297 private:
298 void* _handle;
299 std::filesystem::path _path;
300 std::string _error;
301 Section _executableCode;
302 std::vector<Section> _sections;
303 };
304
311 int TranslateLoading(LoadFlag flags) noexcept;
312
319 LoadFlag TranslateLoading(int flags) noexcept;
320
321} // namespace plugify
Represents an assembly (module) within a process.
Definition assembly.hpp:15
Assembly()
Default constructor initializing handle to nullptr.
Definition assembly.hpp:76
MemAddr FindPattern(MemAddr pattern, std::string_view mask, MemAddr startAddress=nullptr, const Section *moduleSection=nullptr) const
Finds an array of bytes in process memory using SIMD instructions.
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.
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:240
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:111
Assembly(Handle moduleHandle, LoadFlag flags=LoadFlag::Default, const SearchDirs &additionalSearchDirectories={}, bool sections=false)
Constructs an Assembly object with a memory address.
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:121
MemAddr GetBase() const noexcept
Returns the module base address.
bool IsValid() const noexcept
Checks if the assembly is valid.
Definition assembly.hpp:227
A generic handle class that manages a pointer to an object of type T.
Definition handle.hpp:18
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition mem_addr.hpp:10
Represents a system handle.
Definition assembly.hpp:51
void * handle
The system handle.
Definition assembly.hpp:70
Handle(void *systemHandle)
Constructor to initialize the handle.
Definition assembly.hpp:56
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
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