plugify 1.2.8
Loading...
Searching...
No Matches
mem_addr.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <type_traits>
6
7namespace plugify {
12 class MemAddr {
13 public:
17 constexpr MemAddr() noexcept
18 : _ptr{ 0 } {
19 }
20
25 constexpr MemAddr(const uintptr_t ptr) noexcept
26 : _ptr{ ptr } {
27 }
28
34 template <typename T>
35 requires(std::is_pointer_v<T> or std::is_null_pointer_v<T>)
36 MemAddr(T ptr) noexcept
37 : _ptr{ reinterpret_cast<uintptr_t>(ptr) } {
38 }
39
44 constexpr MemAddr(const MemAddr& other) noexcept = default;
45
50 constexpr MemAddr(MemAddr&& other) noexcept = default;
51
57 constexpr MemAddr& operator=(const MemAddr& other) noexcept = default;
58
64 constexpr MemAddr& operator=(MemAddr&& other) noexcept = default;
65
70 constexpr operator uintptr_t() const noexcept {
71 return _ptr;
72 }
73
78 constexpr operator void*() const noexcept {
79 return _addr;
80 }
81
86 constexpr explicit operator bool() const noexcept {
87 return _ptr != 0;
88 }
89
95 constexpr bool operator!=(const MemAddr addr) const noexcept {
96 return _ptr != addr._ptr;
97 }
98
104 constexpr bool operator<(const MemAddr addr) const noexcept {
105 return _ptr < addr._ptr;
106 }
107
113 constexpr bool operator==(const MemAddr addr) const noexcept {
114 return _ptr == addr._ptr;
115 }
116
122 constexpr bool operator==(const uintptr_t addr) const noexcept {
123 return _ptr == addr;
124 }
125
130 constexpr uintptr_t GetPtr() const noexcept {
131 return _ptr;
132 }
133
139 template <class T>
140 constexpr T GetValue() const noexcept {
141 return *reinterpret_cast<T*>(_ptr);
142 }
143
149 template <typename T>
150 constexpr T CCast() const noexcept {
151 return (T) _ptr;
152 }
153
159 template <typename T>
160 constexpr T RCast() const noexcept {
161 return reinterpret_cast<T>(_ptr);
162 }
163
169 template <typename T>
170 constexpr T UCast() const noexcept {
171 union {
172 uintptr_t ptr;
173 T val;
174 } cast;
175
176 return cast.ptr = _ptr, cast.val;
177 }
178
184 constexpr MemAddr Offset(const ptrdiff_t offset) const noexcept {
185 return _ptr + static_cast<uintptr_t>(offset);
186 }
187
193 constexpr MemAddr& OffsetSelf(const ptrdiff_t offset) noexcept {
194 _ptr += static_cast<uintptr_t>(offset);
195 return *this;
196 }
197
203 constexpr MemAddr Deref(ptrdiff_t deref = 1) const {
204 uintptr_t reference = _ptr;
205
206 while (deref--) {
207 if (reference) {
208 reference = *reinterpret_cast<uintptr_t*>(reference);
209 }
210 }
211
212 return reference;
213 }
214
220 constexpr MemAddr& DerefSelf(ptrdiff_t deref = 1) {
221 while (deref--) {
222 if (_ptr) {
223 _ptr = *reinterpret_cast<uintptr_t*>(_ptr);
224 }
225 }
226
227 return *this;
228 }
229
237 const ptrdiff_t opcodeOffset = 0x1,
238 const ptrdiff_t nextInstructionOffset = 0x5
239 ) const {
240 return ResolveRelativeAddress(opcodeOffset, nextInstructionOffset);
241 }
242
250 const ptrdiff_t opcodeOffset = 0x1,
251 const ptrdiff_t nextInstructionOffset = 0x5
252 ) {
253 return ResolveRelativeAddressSelf(opcodeOffset, nextInstructionOffset);
254 }
255
263 const ptrdiff_t registerOffset = 0x0,
264 const ptrdiff_t nextInstructionOffset = 0x4
265 ) const {
266 const uintptr_t skipRegister = _ptr + static_cast<uintptr_t>(registerOffset);
267 const int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
268 const uintptr_t nextInstruction = _ptr + static_cast<uintptr_t>(nextInstructionOffset);
269 return nextInstruction + static_cast<uintptr_t>(relativeAddress);
270 }
271
279 const ptrdiff_t registerOffset = 0x0,
280 const ptrdiff_t nextInstructionOffset = 0x4
281 ) {
282 const uintptr_t skipRegister = _ptr + static_cast<uintptr_t>(registerOffset);
283 const int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
284 const uintptr_t nextInstruction = _ptr + static_cast<uintptr_t>(nextInstructionOffset);
285 _ptr = nextInstruction + static_cast<uintptr_t>(relativeAddress);
286 return *this;
287 }
288
289 private:
290 union {
291 void* _addr;
292 uintptr_t _ptr;
293 };
294 };
295} // namespace plugify
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition mem_addr.hpp:12
MemAddr & FollowNearCallSelf(const ptrdiff_t opcodeOffset=0x1, const ptrdiff_t nextInstructionOffset=0x5)
Follows a near call to resolve the address in-place.
Definition mem_addr.hpp:249
MemAddr ResolveRelativeAddress(const ptrdiff_t registerOffset=0x0, const ptrdiff_t nextInstructionOffset=0x4) const
Resolves a relative address.
Definition mem_addr.hpp:262
constexpr bool operator<(const MemAddr addr) const noexcept
Less-than comparison operator.
Definition mem_addr.hpp:104
constexpr MemAddr & OffsetSelf(const ptrdiff_t offset) noexcept
Offsets the memory address by a specified amount in-place.
Definition mem_addr.hpp:193
constexpr MemAddr(MemAddr &&other) noexcept=default
Move constructor initializing the pointer with another MemAddr.
constexpr bool operator==(const MemAddr addr) const noexcept
Equality operator.
Definition mem_addr.hpp:113
constexpr uintptr_t GetPtr() const noexcept
Returns the uintptr_t representation of the pointer.
Definition mem_addr.hpp:130
constexpr T CCast() const noexcept
Casts the pointer to a specified type using C-style cast.
Definition mem_addr.hpp:150
constexpr T RCast() const noexcept
Casts the pointer to a specified type using reinterpret_cast.
Definition mem_addr.hpp:160
constexpr MemAddr(const uintptr_t ptr) noexcept
Constructor initializing the pointer with a uintptr_t value.
Definition mem_addr.hpp:25
constexpr MemAddr & operator=(MemAddr &&other) noexcept=default
Move assignment operator to move another MemAddr.
constexpr MemAddr & operator=(const MemAddr &other) noexcept=default
Assignment operator to copy another MemAddr.
constexpr MemAddr() noexcept
Default constructor initializing the pointer to 0.
Definition mem_addr.hpp:17
constexpr MemAddr Offset(const ptrdiff_t offset) const noexcept
Offsets the memory address by a specified amount.
Definition mem_addr.hpp:184
constexpr bool operator!=(const MemAddr addr) const noexcept
Inequality operator.
Definition mem_addr.hpp:95
constexpr bool operator==(const uintptr_t addr) const noexcept
Equality operator for comparing with uintptr_t.
Definition mem_addr.hpp:122
constexpr T UCast() const noexcept
Casts the pointer to a specified type using a union cast.
Definition mem_addr.hpp:170
constexpr T GetValue() const noexcept
Retrieves the value at the memory address.
Definition mem_addr.hpp:140
MemAddr(T ptr) noexcept
Template constructor initializing the pointer with a typed pointer.
Definition mem_addr.hpp:36
MemAddr & ResolveRelativeAddressSelf(const ptrdiff_t registerOffset=0x0, const ptrdiff_t nextInstructionOffset=0x4)
Resolves a relative address in-place.
Definition mem_addr.hpp:278
constexpr MemAddr(const MemAddr &other) noexcept=default
Constructor initializing the pointer with a void pointer.
MemAddr FollowNearCall(const ptrdiff_t opcodeOffset=0x1, const ptrdiff_t nextInstructionOffset=0x5) const
Follows a near call to resolve the address.
Definition mem_addr.hpp:236
constexpr MemAddr Deref(ptrdiff_t deref=1) const
Dereferences the memory address a specified number of times.
Definition mem_addr.hpp:203
constexpr MemAddr & DerefSelf(ptrdiff_t deref=1)
Dereferences the memory address a specified number of times in-place.
Definition mem_addr.hpp:220