-
Notifications
You must be signed in to change notification settings - Fork 68
/
address.h
187 lines (146 loc) · 4.73 KB
/
address.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma once
// class size is only 4 bytes on x86-32 and 8 bytes on x86-64.
class Address {
protected:
uintptr_t m_addr;
public:
// default ctor/dtor.
__forceinline Address( ) : m_addr{} {};
__forceinline ~Address( ) {};
// ctors.
__forceinline Address( uintptr_t a ) : m_addr{ a } {}
__forceinline Address( const void* a ) : m_addr{ ( uintptr_t )a } {}
// arithmetic operators for native types.
__forceinline operator uintptr_t( ) { return m_addr; }
__forceinline operator void*( ) { return ( void* )m_addr; }
__forceinline operator const void*( ) { return ( const void* )m_addr; }
// is-equals-operator.
__forceinline bool operator==( Address a ) const {
return as< uintptr_t >( ) == a.as< uintptr_t >( );
}
__forceinline bool operator!=( Address a ) const {
return as< uintptr_t >( ) != a.as< uintptr_t >( );
}
// cast / add offset and cast.
template< typename t = Address >
__forceinline t as( ) const {
return ( m_addr ) ? ( t )m_addr : t{};
}
template< typename t = Address >
__forceinline t as( size_t offset ) const {
return ( m_addr ) ? ( t )( m_addr + offset ) : t{};
}
template< typename t = Address >
__forceinline t as( ptrdiff_t offset ) const {
return ( m_addr ) ? ( t )( m_addr + offset ) : t{};
}
// add offset and dereference.
template< typename t = Address >
__forceinline t at( size_t offset ) const {
return ( m_addr ) ? *( t * )( m_addr + offset ) : t{};
}
template< typename t = Address >
__forceinline t at( ptrdiff_t offset ) const {
return ( m_addr ) ? *( t * )( m_addr + offset ) : t{};
}
// add offset.
template< typename t = Address >
__forceinline t add( size_t offset ) const {
return ( m_addr ) ? ( t )( m_addr + offset ) : t{};
}
template< typename t = Address >
__forceinline t add( ptrdiff_t offset ) const {
return ( m_addr ) ? ( t )( m_addr + offset ) : t{};
}
// subtract offset.
template< typename t = Address >
__forceinline t sub( size_t offset ) const {
return ( m_addr ) ? ( t )( m_addr - offset ) : t{};
}
template< typename t = Address >
__forceinline t sub( ptrdiff_t offset ) const {
return ( m_addr ) ? ( t )( m_addr - offset ) : t{};
}
// dereference.
template< typename t = Address >
__forceinline t to( ) const {
return *( t * )m_addr;
}
// verify adddress and dereference n times.
template< typename t = Address >
__forceinline t get( size_t n = 1 ) {
uintptr_t out;
if( !m_addr )
return t{};
out = m_addr;
for( size_t i{ n }; i > 0; --i ) {
// can't dereference, return null.
if( !valid( out ) )
return t{};
out = *( uintptr_t * )out;
}
return ( t )out;
}
// follow relative8 and relative16/32 offsets.
template< typename t = Address >
__forceinline t rel8( size_t offset ) {
uintptr_t out;
uint8_t r;
if( !m_addr )
return t{};
out = m_addr + offset;
// get relative offset.
r = *( uint8_t * )out;
if( !r )
return t{};
// relative to address of next instruction.
// short jumps can go forward and backward depending on the size of the second byte.
// if the second byte is below 128, the jmp goes forwards.
// if the second byte is above 128, the jmp goes backwards ( subtract two's complement of the relative offset from the address of the next instruction ).
if( r < 128 )
out = ( out + 1 ) + r;
else
out = ( out + 1 ) - ( uint8_t )( ~r + 1 );
return ( t )out;
}
template< typename t = Address >
__forceinline t rel32( size_t offset ) {
uintptr_t out;
uint32_t r;
if( !m_addr )
return t{};
out = m_addr + offset;
// get rel32 offset.
r = *( uint32_t * )out;
if( !r )
return t{};
// relative to address of next instruction.
out = ( out + 4 ) + r;
return ( t )out;
}
// set.
template< typename t = uintptr_t > __forceinline void set( const t &value ) {
if( !m_addr )
return;
*(t *)m_addr = value;
}
// checks if address is not null and has correct page protection.
static __forceinline bool valid( uintptr_t addr ) {
MEMORY_BASIC_INFORMATION mbi;
// check for invalid address.
if( !addr )
return false;
// check for invalid page protection.
if( !g_winapi.VirtualQuery( ( const void * )addr, &mbi, sizeof( mbi ) ) )
return false;
// todo - dex; fix this, its wrong... check for rwe or something too
if( /*!( mbi.State & MEM_COMMIT ) ||*/ ( mbi.Protect & PAGE_NOACCESS ) || ( mbi.Protect & PAGE_GUARD ) )
return false;
return true;
}
// relative virtual address.
template< typename t = Address >
static __forceinline t RVA( Address base, size_t offset ) {
return base.as< t >( offset );
}
};