plugify 1.2.6
Loading...
Searching...
No Matches
mem_addr.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6namespace plugify {
10 class MemAddr {
11 public:
15 constexpr MemAddr() noexcept : _ptr{0} {}
16
21 constexpr MemAddr(const uintptr_t ptr) noexcept : _ptr{ptr} {}
22
28 template<typename T> requires (std::is_pointer_v<T> or std::is_null_pointer_v<T>)
29 MemAddr(T ptr) noexcept : _ptr{reinterpret_cast<uintptr_t>(ptr)} {}
30
35 constexpr MemAddr(const MemAddr& other) noexcept = default;
36
41 constexpr MemAddr(MemAddr&& other) noexcept = default;
42
48 constexpr MemAddr& operator=(const MemAddr& other) noexcept = default;
49
55 constexpr MemAddr& operator=(MemAddr&& other) noexcept = default;
56
61 constexpr operator uintptr_t() const noexcept {
62 return _ptr;
63 }
64
69 constexpr operator void*() const noexcept {
70 return _addr;
71 }
72
77 constexpr explicit operator bool() const noexcept {
78 return _ptr != 0;
79 }
80
86 constexpr bool operator!=(const MemAddr addr) const noexcept {
87 return _ptr != addr._ptr;
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 uintptr_t addr) const noexcept {
114 return _ptr == addr;
115 }
116
121 constexpr uintptr_t GetPtr() const noexcept {
122 return _ptr;
123 }
124
130 template<class T>
131 constexpr T GetValue() const noexcept {
132 return *reinterpret_cast<T*>(_ptr);
133 }
134
140 template<typename T>
141 constexpr T CCast() const noexcept {
142 return (T) _ptr;
143 }
144
150 template<typename T>
151 constexpr T RCast() const noexcept {
152 return reinterpret_cast<T>(_ptr);
153 }
154
160 template<typename T>
161 constexpr T UCast() const noexcept {
162 union {
163 uintptr_t ptr;
164 T val;
165 } cast;
166 return cast.ptr = _ptr, cast.val;
167 }
168
174 constexpr MemAddr Offset(const ptrdiff_t offset) const noexcept {
175 return _ptr + static_cast<uintptr_t>(offset);
176 }
177
183 constexpr MemAddr& OffsetSelf(const ptrdiff_t offset) noexcept {
184 _ptr += static_cast<uintptr_t>(offset);
185 return *this;
186 }
187
193 constexpr MemAddr Deref(ptrdiff_t deref = 1) const {
194 uintptr_t reference = _ptr;
195
196 while (deref--) {
197 if (reference)
198 reference = *reinterpret_cast<uintptr_t*>(reference);
199 }
200
201 return reference;
202 }
203
209 constexpr MemAddr& DerefSelf(ptrdiff_t deref = 1) {
210 while (deref--) {
211 if (_ptr)
212 _ptr = *reinterpret_cast<uintptr_t*>(_ptr);
213 }
214
215 return *this;
216 }
217
224 MemAddr FollowNearCall(const ptrdiff_t opcodeOffset = 0x1, const ptrdiff_t nextInstructionOffset = 0x5) const {
225 return ResolveRelativeAddress(opcodeOffset, nextInstructionOffset);
226 }
227
234 MemAddr& FollowNearCallSelf(const ptrdiff_t opcodeOffset = 0x1, const ptrdiff_t nextInstructionOffset = 0x5) {
235 return ResolveRelativeAddressSelf(opcodeOffset, nextInstructionOffset);
236 }
237
244 MemAddr ResolveRelativeAddress(const ptrdiff_t registerOffset = 0x0, const ptrdiff_t nextInstructionOffset = 0x4) const {
245 const uintptr_t skipRegister = _ptr + static_cast<uintptr_t>(registerOffset);
246 const int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
247 const uintptr_t nextInstruction = _ptr + static_cast<uintptr_t>(nextInstructionOffset);
248 return nextInstruction + static_cast<uintptr_t>(relativeAddress);
249 }
250
257 MemAddr& ResolveRelativeAddressSelf(const ptrdiff_t registerOffset = 0x0, const ptrdiff_t nextInstructionOffset = 0x4) {
258 const uintptr_t skipRegister = _ptr + static_cast<uintptr_t>(registerOffset);
259 const int32_t relativeAddress = *reinterpret_cast<int32_t*>(skipRegister);
260 const uintptr_t nextInstruction = _ptr + static_cast<uintptr_t>(nextInstructionOffset);
261 _ptr = nextInstruction + static_cast<uintptr_t>(relativeAddress);
262 return *this;
263 }
264
265 private:
266 union {
267 void* _addr;
268 uintptr_t _ptr;
269 };
270 };
271}// namespace plugify
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition mem_addr.hpp:10
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:234
MemAddr ResolveRelativeAddress(const ptrdiff_t registerOffset=0x0, const ptrdiff_t nextInstructionOffset=0x4) const
Resolves a relative address.
Definition mem_addr.hpp:244
constexpr bool operator<(const MemAddr addr) const noexcept
Less-than comparison operator.
Definition mem_addr.hpp:95
constexpr MemAddr & OffsetSelf(const ptrdiff_t offset) noexcept
Offsets the memory address by a specified amount in-place.
Definition mem_addr.hpp:183
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:104
constexpr uintptr_t GetPtr() const noexcept
Returns the uintptr_t representation of the pointer.
Definition mem_addr.hpp:121
constexpr T CCast() const noexcept
Casts the pointer to a specified type using C-style cast.
Definition mem_addr.hpp:141
constexpr T RCast() const noexcept
Casts the pointer to a specified type using reinterpret_cast.
Definition mem_addr.hpp:151
constexpr MemAddr(const uintptr_t ptr) noexcept
Constructor initializing the pointer with a uintptr_t value.
Definition mem_addr.hpp:21
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:15
constexpr MemAddr Offset(const ptrdiff_t offset) const noexcept
Offsets the memory address by a specified amount.
Definition mem_addr.hpp:174
constexpr bool operator!=(const MemAddr addr) const noexcept
Inequality operator.
Definition mem_addr.hpp:86
constexpr bool operator==(const uintptr_t addr) const noexcept
Equality operator for comparing with uintptr_t.
Definition mem_addr.hpp:113
constexpr T UCast() const noexcept
Casts the pointer to a specified type using a union cast.
Definition mem_addr.hpp:161
constexpr T GetValue() const noexcept
Retrieves the value at the memory address.
Definition mem_addr.hpp:131
MemAddr(T ptr) noexcept
Template constructor initializing the pointer with a typed pointer.
Definition mem_addr.hpp:29
MemAddr & ResolveRelativeAddressSelf(const ptrdiff_t registerOffset=0x0, const ptrdiff_t nextInstructionOffset=0x4)
Resolves a relative address in-place.
Definition mem_addr.hpp:257
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:224
constexpr MemAddr Deref(ptrdiff_t deref=1) const
Dereferences the memory address a specified number of times.
Definition mem_addr.hpp:193
constexpr MemAddr & DerefSelf(ptrdiff_t deref=1)
Dereferences the memory address a specified number of times in-place.
Definition mem_addr.hpp:209