-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSearchContext.m
300 lines (266 loc) · 7.92 KB
/
SearchContext.m
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* The Cheat - The legendary universal game trainer for Mac OS X.
* http://www.brokenzipper.com/trac/wiki/TheCheat
*
* Copyright (c) 2003-2011, Charles McGarvey et al.
*
* Distributable under the terms and conditions of the 2-clause BSD
* license; see the file COPYING for the legal text of the license.
*/
#import "SearchContext.h"
@implementation SearchContext
#pragma mark Initialization
/*
* There isn't really a designated initializer because the initialization of the types
* of searches vary way too much.
*/
- (id)initWithPID:(pid_t)pid searchOperator:(TCSearchOperator)op value:(Variable *)val
{
unsigned valueSize;
if ( [super init] ) {
value = [val retain];
if ( !value ) {
[self release];
return nil;
}
valueSize = [value valueSize];
process = pid;
_variableType = [value type];
_integerSign = [value integerSign];
_operator = op;
_searchType = TCGivenValue;
compareFunc = [self compareFunction];
// allocate the memory objects which will be used during the search
regionCount = VMCountRegionsWithAttributes( process, VMREGION_READABLE | VMREGION_WRITABLE );
addresses = TCMakeArray( TC_BUFFER_SIZE / sizeof(TCAddress), sizeof(TCAddress) );
values = TCMakeArray( TC_BUFFER_SIZE / valueSize, valueSize );
regions = TCMakeArray( 0, sizeof(TCAddress) );
perRegion = TCMakeArray( 0, sizeof(unsigned) );
addressPtr = TCArrayBytes( addresses );
valuePtr = TCArrayBytes( values );
ChazLog( @"SearchContext: varType=%i, intSign=%i, op=%i, value=%@", _variableType, _integerSign, _operator, [value stringValue] );
}
return self;
}
- (id)initWithLastContext:(SearchContext *)context searchOperator:(TCSearchOperator)op
{
unsigned valueSize;
if ( [super init] ) {
if ( !context ) {
[self release];
return nil;
}
valueSize = TCArrayElementSize(context->values);
process = context->process;
_variableType = [context variableType];
_integerSign = [context integerSign];
_operator = op;
_searchType = TCLastValue;
compareFunc = [self compareFunction];
regionCount = TCArrayElementCount( context->regions );
addresses = TCMakeArray( TC_BUFFER_SIZE / sizeof(TCAddress), sizeof(TCAddress) );
values = TCMakeArray( TC_BUFFER_SIZE / valueSize, valueSize );
regions = TCMakeArray( 0, sizeof(TCAddress) );
perRegion = TCMakeArray( 0, sizeof(unsigned) );
lastAddresses = context->addresses;
lastValues = context->values;
lastRegions = context->regions;
lastPerRegion = context->perRegion;
addressPtr = TCArrayBytes( addresses );
valuePtr = TCArrayBytes( values );
lastAddressPtr = TCArrayBytes( lastAddresses );
lastValuePtr = TCArrayBytes( lastValues );
lastRegionPtr = TCArrayBytes( lastRegions );
lastPerRegionPtr = TCArrayBytes( lastPerRegion );
ChazLog( @"SearchContext: varType=%i, intSign=%i, op=%i", _variableType, _integerSign, _operator );
}
return self;
}
- (id)initWithLastContext:(SearchContext *)context searchOperator:(TCSearchOperator)op value:(Variable *)val
{
unsigned valueSize;
if ( [super init] ) {
if ( !context || !val || ([val type] != [context variableType]) // and search values can't be bigger than the last time.
|| (([context variableType] == TCString) && ([val valueSize] > TCArrayElementSize(context->values))) ) {
[self release];
return nil;
}
value = [val retain];
valueSize = [value valueSize];
process = context->process;
_variableType = [context variableType];
_integerSign = [context integerSign];
_operator = op;
_searchType = TCGivenValue;
compareFunc = [self compareFunction];
regionCount = TCArrayElementCount( context->regions );
addresses = TCMakeArray( TC_BUFFER_SIZE / sizeof(TCAddress), sizeof(TCAddress) );
values = TCMakeArray( TC_BUFFER_SIZE / valueSize, valueSize );
regions = TCMakeArray( 0, sizeof(TCAddress) );
perRegion = TCMakeArray( 0, sizeof(unsigned) );
lastAddresses = context->addresses;
lastValues = context->values;
lastRegions = context->regions;
lastPerRegion = context->perRegion;
addressPtr = TCArrayBytes( addresses );
valuePtr = TCArrayBytes( values );
lastAddressPtr = TCArrayBytes( lastAddresses );
lastValuePtr = TCArrayBytes( lastValues );
lastRegionPtr = TCArrayBytes( lastRegions );
lastPerRegionPtr = TCArrayBytes( lastPerRegion );
ChazLog( @"SearchContext: varType=%i, intSign=%i, op=%i, value=%@", _variableType, _integerSign, _operator, [value stringValue] );
}
return self;
}
- (void)dealloc
{
ChazLog( @"SearchContext %p dealloc", self );
TCReleaseArray( addresses );
TCReleaseArray( values );
TCReleaseArray( regions );
TCReleaseArray( perRegion );
if ( buffer ) {
free( buffer );
}
[value release];
[super dealloc];
}
#pragma mark Accessors
- (TCVariableType)variableType
{
return _variableType;
}
- (TCIntegerSign)integerSign
{
return _integerSign;
}
- (TCSearchOperator)searchOperator
{
return _operator;
}
- (BOOL (*)(void const *, void const *))compareFunction
{
// here begins a very pretty collection of switch and if statements. enjoy!
switch ( _operator ) {
case TCEqual:
switch ( _variableType ) {
case TCFloat: return EqualFloat;
case TCDouble: return EqualDouble;
}
if ( _integerSign == TCSigned ) {
switch ( _variableType ) {
case TCInt64: return EqualInt64;
case TCInt32: return EqualInt32;
case TCInt16: return EqualInt16;
case TCInt8: return EqualInt8;
}
}
else {
switch ( _variableType ) {
case TCInt64: return EqualUInt64;
case TCInt32: return EqualUInt32;
case TCInt16: return EqualUInt16;
case TCInt8: return EqualUInt8;
}
}
break;
case TCNotEqual:
switch ( _variableType ) {
case TCFloat: return NotEqualFloat;
case TCDouble: return NotEqualDouble;
}
if ( _integerSign == TCSigned ) {
switch ( _variableType ) {
case TCInt64: return NotEqualInt64;
case TCInt32: return NotEqualInt32;
case TCInt16: return NotEqualInt16;
case TCInt8: return NotEqualInt8;
}
}
else {
switch ( _variableType ) {
case TCInt64: return NotEqualUInt64;
case TCInt32: return NotEqualUInt32;
case TCInt16: return NotEqualUInt16;
case TCInt8: return NotEqualUInt8;
}
}
break;
case TCLessThan:
switch ( _variableType ) {
case TCFloat: return LessThanFloat;
case TCDouble: return LessThanDouble;
}
if ( _integerSign == TCSigned ) {
switch ( _variableType ) {
case TCInt64: return LessThanInt64;
case TCInt32: return LessThanInt32;
case TCInt16: return LessThanInt16;
case TCInt8: return LessThanInt8;
}
}
else {
switch ( _variableType ) {
case TCInt64: return LessThanUInt64;
case TCInt32: return LessThanUInt32;
case TCInt16: return LessThanUInt16;
case TCInt8: return LessThanUInt8;
}
}
break;
case TCGreaterThan:
switch ( _variableType ) {
case TCFloat: return GreaterThanFloat;
case TCDouble: return GreaterThanDouble;
}
if ( _integerSign == TCSigned ) {
switch ( _variableType ) {
case TCInt64: return GreaterThanInt64;
case TCInt32: return GreaterThanInt32;
case TCInt16: return GreaterThanInt16;
case TCInt8: return GreaterThanInt8;
}
}
else {
switch ( _variableType ) {
case TCInt64: return GreaterThanUInt64;
case TCInt32: return GreaterThanUInt32;
case TCInt16: return GreaterThanUInt16;
case TCInt8: return GreaterThanUInt8;
}
}
break;
}
return NULL;
}
- (int (*)(id, unsigned))iterationFunction
{
if ( _searchType == TCGivenValue ) {
if ( !lastAddresses ) {
if ( _variableType == TCString ) {
return SearchStringIteration;
}
else {
return SearchIteration;
}
}
else {
if ( _variableType == TCString ) {
return SearchStringIterationAgain;
}
else {
return SearchIterationAgain;
}
}
}
else if ( _searchType == TCLastValue ) {
if ( _variableType == TCString ) {
return SearchStringIterationLastValue;
}
else {
return SearchIterationLastValue;
}
}
return NULL;
}
@end