-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstl_address.hpp
113 lines (86 loc) · 2.62 KB
/
mstl_address.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include "mstl_inc.hpp"
class Address {
protected:
uintptr_t m_ptr;
public:
// default c/dtor
__forceinline Address() : m_ptr{} { };
__forceinline ~Address() { };
__forceinline Address(uintptr_t a) :
m_ptr{a} { }
__forceinline Address(const void* a) :
m_ptr{(uintptr_t) a} { }
__forceinline operator uintptr_t() { return m_ptr; }
__forceinline operator void*() { return (void*) m_ptr; }
__forceinline operator const void*() { return (const void*) m_ptr; }
// to is like as but dereferences.
template< typename t = Address >
__forceinline t to() const {
return *(t*) m_ptr;
}
template< typename t = Address >
__forceinline t as() const {
return (t) m_ptr;
}
template< typename t = Address >
__forceinline t at(ptrdiff_t offset) const {
return *(t*) (m_ptr + offset);
}
template< typename t = Address >
__forceinline t add(ptrdiff_t offset) const {
return (t) (m_ptr + offset);
}
template< typename t = Address >
__forceinline t sub(ptrdiff_t offset) const {
return (t) (m_ptr - offset);
}
template<typename t = Address>
__forceinline t get(size_t dereferences = 1) {
return (t) get_(dereferences);
}
template<typename t = Address>
__forceinline void set(t val) {
*(t*) m_ptr = val;
}
// for when we need to deal with RVAs
template<typename t = Address>
__forceinline static t RVA(uintptr_t start, size_t at) {
if (!start || !at)
return t{};
return (t) (start + at);
}
template<typename t = Address>
__forceinline t rel(size_t offset = 0) {
uintptr_t out;
uint32_t rel;
out = m_ptr + offset;
rel = *(uint32_t *) out;
if (!rel)
return t{};
out = (out + 0x4) + rel;
return (t) out;
}
__forceinline static bool safe(Address to_check) {
static MEMORY_BASIC_INFORMATION32 mbi{};
if (!to_check
|| to_check < 0x10000
|| to_check > 0xFFE00000
|| !VirtualQuery(to_check, (PMEMORY_BASIC_INFORMATION) &mbi, sizeof(mbi)))
return false;
if (!mbi.AllocationBase
|| mbi.State != MEM_COMMIT
|| mbi.Protect == PAGE_NOACCESS
|| mbi.Protect & PAGE_GUARD)
return false;
return true;
}
private:
__forceinline uintptr_t get_(size_t dereferences)
{
uintptr_t temp = m_ptr;
while (dereferences-- && safe(temp))
temp = *(uintptr_t*) temp;
return temp;
}
};