]> Dogcows Code - chaz/thecheat/blob - SearchContext.m
update contact information and project URL
[chaz/thecheat] / SearchContext.m
1
2 /*
3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
5 *
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
7 *
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
10 */
11
12
13 #import "SearchContext.h"
14
15
16 @implementation SearchContext
17
18
19 #pragma mark Initialization
20
21 /*
22 * There isn't really a designated initializer because the initialization of the types
23 * of searches vary way too much.
24 */
25
26 - (id)initWithPID:(pid_t)pid searchOperator:(TCSearchOperator)op value:(Variable *)val
27 {
28 unsigned valueSize;
29
30 if ( [super init] ) {
31 value = [val retain];
32 if ( !value ) {
33 [self release];
34 return nil;
35 }
36 valueSize = [value valueSize];
37 process = pid;
38 _variableType = [value type];
39 _integerSign = [value integerSign];
40 _operator = op;
41 _searchType = TCGivenValue;
42 compareFunc = [self compareFunction];
43
44 // allocate the memory objects which will be used during the search
45 regionCount = VMCountRegionsWithAttributes( process, VMREGION_READABLE | VMREGION_WRITABLE );
46 addresses = TCMakeArray( TC_BUFFER_SIZE / sizeof(TCAddress), sizeof(TCAddress) );
47 values = TCMakeArray( TC_BUFFER_SIZE / valueSize, valueSize );
48 regions = TCMakeArray( 0, sizeof(TCAddress) );
49 perRegion = TCMakeArray( 0, sizeof(unsigned) );
50 addressPtr = TCArrayBytes( addresses );
51 valuePtr = TCArrayBytes( values );
52
53 ChazLog( @"SearchContext: varType=%i, intSign=%i, op=%i, value=%@", _variableType, _integerSign, _operator, [value stringValue] );
54 }
55 return self;
56 }
57
58 - (id)initWithLastContext:(SearchContext *)context searchOperator:(TCSearchOperator)op
59 {
60 unsigned valueSize;
61
62 if ( [super init] ) {
63 if ( !context ) {
64 [self release];
65 return nil;
66 }
67 valueSize = TCArrayElementSize(context->values);
68 process = context->process;
69 _variableType = [context variableType];
70 _integerSign = [context integerSign];
71 _operator = op;
72 _searchType = TCLastValue;
73 compareFunc = [self compareFunction];
74
75 regionCount = TCArrayElementCount( context->regions );
76 addresses = TCMakeArray( TC_BUFFER_SIZE / sizeof(TCAddress), sizeof(TCAddress) );
77 values = TCMakeArray( TC_BUFFER_SIZE / valueSize, valueSize );
78 regions = TCMakeArray( 0, sizeof(TCAddress) );
79 perRegion = TCMakeArray( 0, sizeof(unsigned) );
80 lastAddresses = context->addresses;
81 lastValues = context->values;
82 lastRegions = context->regions;
83 lastPerRegion = context->perRegion;
84 addressPtr = TCArrayBytes( addresses );
85 valuePtr = TCArrayBytes( values );
86 lastAddressPtr = TCArrayBytes( lastAddresses );
87 lastValuePtr = TCArrayBytes( lastValues );
88 lastRegionPtr = TCArrayBytes( lastRegions );
89 lastPerRegionPtr = TCArrayBytes( lastPerRegion );
90
91 ChazLog( @"SearchContext: varType=%i, intSign=%i, op=%i", _variableType, _integerSign, _operator );
92 }
93 return self;
94 }
95
96 - (id)initWithLastContext:(SearchContext *)context searchOperator:(TCSearchOperator)op value:(Variable *)val
97 {
98 unsigned valueSize;
99
100 if ( [super init] ) {
101 if ( !context || !val || ([val type] != [context variableType]) // and search values can't be bigger than the last time.
102 || (([context variableType] == TCString) && ([val valueSize] > TCArrayElementSize(context->values))) ) {
103 [self release];
104 return nil;
105 }
106 value = [val retain];
107 valueSize = [value valueSize];
108 process = context->process;
109 _variableType = [context variableType];
110 _integerSign = [context integerSign];
111 _operator = op;
112 _searchType = TCGivenValue;
113 compareFunc = [self compareFunction];
114
115 regionCount = TCArrayElementCount( context->regions );
116 addresses = TCMakeArray( TC_BUFFER_SIZE / sizeof(TCAddress), sizeof(TCAddress) );
117 values = TCMakeArray( TC_BUFFER_SIZE / valueSize, valueSize );
118 regions = TCMakeArray( 0, sizeof(TCAddress) );
119 perRegion = TCMakeArray( 0, sizeof(unsigned) );
120 lastAddresses = context->addresses;
121 lastValues = context->values;
122 lastRegions = context->regions;
123 lastPerRegion = context->perRegion;
124 addressPtr = TCArrayBytes( addresses );
125 valuePtr = TCArrayBytes( values );
126 lastAddressPtr = TCArrayBytes( lastAddresses );
127 lastValuePtr = TCArrayBytes( lastValues );
128 lastRegionPtr = TCArrayBytes( lastRegions );
129 lastPerRegionPtr = TCArrayBytes( lastPerRegion );
130
131 ChazLog( @"SearchContext: varType=%i, intSign=%i, op=%i, value=%@", _variableType, _integerSign, _operator, [value stringValue] );
132 }
133 return self;
134 }
135
136 - (void)dealloc
137 {
138 ChazLog( @"SearchContext %p dealloc", self );
139 TCReleaseArray( addresses );
140 TCReleaseArray( values );
141 TCReleaseArray( regions );
142 TCReleaseArray( perRegion );
143
144 if ( buffer ) {
145 free( buffer );
146 }
147 [value release];
148
149 [super dealloc];
150 }
151
152
153 #pragma mark Accessors
154
155 - (TCVariableType)variableType
156 {
157 return _variableType;
158 }
159
160 - (TCIntegerSign)integerSign
161 {
162 return _integerSign;
163 }
164
165 - (TCSearchOperator)searchOperator
166 {
167 return _operator;
168 }
169
170
171 - (BOOL (*)(void const *, void const *))compareFunction
172 {
173 // here begins a very pretty collection of switch and if statements. enjoy!
174 switch ( _operator ) {
175 case TCEqual:
176 switch ( _variableType ) {
177 case TCFloat: return EqualFloat;
178 case TCDouble: return EqualDouble;
179 }
180 if ( _integerSign == TCSigned ) {
181 switch ( _variableType ) {
182 case TCInt64: return EqualInt64;
183 case TCInt32: return EqualInt32;
184 case TCInt16: return EqualInt16;
185 case TCInt8: return EqualInt8;
186 }
187 }
188 else {
189 switch ( _variableType ) {
190 case TCInt64: return EqualUInt64;
191 case TCInt32: return EqualUInt32;
192 case TCInt16: return EqualUInt16;
193 case TCInt8: return EqualUInt8;
194 }
195 }
196 break;
197 case TCNotEqual:
198 switch ( _variableType ) {
199 case TCFloat: return NotEqualFloat;
200 case TCDouble: return NotEqualDouble;
201 }
202 if ( _integerSign == TCSigned ) {
203 switch ( _variableType ) {
204 case TCInt64: return NotEqualInt64;
205 case TCInt32: return NotEqualInt32;
206 case TCInt16: return NotEqualInt16;
207 case TCInt8: return NotEqualInt8;
208 }
209 }
210 else {
211 switch ( _variableType ) {
212 case TCInt64: return NotEqualUInt64;
213 case TCInt32: return NotEqualUInt32;
214 case TCInt16: return NotEqualUInt16;
215 case TCInt8: return NotEqualUInt8;
216 }
217 }
218 break;
219 case TCLessThan:
220 switch ( _variableType ) {
221 case TCFloat: return LessThanFloat;
222 case TCDouble: return LessThanDouble;
223 }
224 if ( _integerSign == TCSigned ) {
225 switch ( _variableType ) {
226 case TCInt64: return LessThanInt64;
227 case TCInt32: return LessThanInt32;
228 case TCInt16: return LessThanInt16;
229 case TCInt8: return LessThanInt8;
230 }
231 }
232 else {
233 switch ( _variableType ) {
234 case TCInt64: return LessThanUInt64;
235 case TCInt32: return LessThanUInt32;
236 case TCInt16: return LessThanUInt16;
237 case TCInt8: return LessThanUInt8;
238 }
239 }
240 break;
241 case TCGreaterThan:
242 switch ( _variableType ) {
243 case TCFloat: return GreaterThanFloat;
244 case TCDouble: return GreaterThanDouble;
245 }
246 if ( _integerSign == TCSigned ) {
247 switch ( _variableType ) {
248 case TCInt64: return GreaterThanInt64;
249 case TCInt32: return GreaterThanInt32;
250 case TCInt16: return GreaterThanInt16;
251 case TCInt8: return GreaterThanInt8;
252 }
253 }
254 else {
255 switch ( _variableType ) {
256 case TCInt64: return GreaterThanUInt64;
257 case TCInt32: return GreaterThanUInt32;
258 case TCInt16: return GreaterThanUInt16;
259 case TCInt8: return GreaterThanUInt8;
260 }
261 }
262 break;
263 }
264 return NULL;
265 }
266
267 - (int (*)(id, unsigned))iterationFunction
268 {
269 if ( _searchType == TCGivenValue ) {
270 if ( !lastAddresses ) {
271 if ( _variableType == TCString ) {
272 return SearchStringIteration;
273 }
274 else {
275 return SearchIteration;
276 }
277 }
278 else {
279 if ( _variableType == TCString ) {
280 return SearchStringIterationAgain;
281 }
282 else {
283 return SearchIterationAgain;
284 }
285 }
286 }
287 else if ( _searchType == TCLastValue ) {
288 if ( _variableType == TCString ) {
289 return SearchStringIterationLastValue;
290 }
291 else {
292 return SearchIterationLastValue;
293 }
294 }
295
296 return NULL;
297 }
298
299
300 @end
This page took 0.042247 seconds and 4 git commands to generate.