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