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