]> Dogcows Code - chaz/thecheat/blob - CheatServer.m
The Cheat 1.1
[chaz/thecheat] / CheatServer.m
1
2 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 // Project: The Cheat
4 //
5 // File: CheatServer.m
6 // Created: Sun Sep 07 2003
7 //
8 // Copyright: 2003 Chaz McGarvey. All rights reserved.
9 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10
11 #import "CheatServer.h"
12
13 #import "SearchResults.h"
14
15 // for comparing floats
16 #import <Chaz/Misc.h>
17
18 #include <string.h>
19 #include <math.h>
20 #include <errno.h>
21
22
23 // Internal Functions
24 int bmsearch( char *pat, int m, char *text, int n, void *base, void *loc[] );
25 //BOOL inline compare_float( float a, float b );
26 //BOOL inline compare_double( double a, double b );
27
28
29 @implementation CheatServer
30
31
32 + (NSConnection *)serverWithDelegate:(id)delegate socket:(int)sock
33 {
34 NSPort *rPort = [NSPort port], *sPort = [NSPort port];
35 NSConnection *connection;
36 NSArray *array;
37
38 connection = [[NSConnection alloc] initWithReceivePort:rPort sendPort:sPort];
39 [connection setRootObject:delegate];
40
41 array = [NSArray arrayWithObjects:sPort, rPort, [NSNumber numberWithInt:sock], nil];
42 [NSThread detachNewThreadSelector:@selector(serverThread:) toTarget:self withObject:array];
43
44 return [connection autorelease];
45 }
46
47 + (void)serverThread:(NSArray *)array
48 {
49 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
50 NSConnection *connection = [NSConnection connectionWithReceivePort:[array objectAtIndex:0] sendPort:[array objectAtIndex:1]];
51 CheatServer *object = [[self alloc] initWithRootProxy:[connection rootProxy]];
52
53 [object handleSocket:[[array objectAtIndex:2] intValue]];
54 [object run];
55
56 [object release];
57 [pool release];
58 }
59
60
61 - (id)initWithRootProxy:(id)proxy
62 {
63 if ( self = [super init] )
64 {
65 NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter];
66
67 rootProxy = proxy;
68
69 [nc addObserver:self selector:@selector(processListChanged:) name:@"NSWorkspaceDidLaunchApplicationNotification" object:nil];
70 [nc addObserver:self selector:@selector(processListChanged:) name:@"NSWorkspaceDidTerminateApplicationNotification" object:nil];
71
72 [self setPID:[rootProxy serverFirstProcess]];
73
74 processPaused = NO;
75
76 searchResults = [[NSMutableArray alloc] init];
77 searchResultsUndone = [[NSMutableArray alloc] init];
78 }
79
80 return self;
81 }
82
83
84 - (void)handleSocket:(int)sock
85 {
86 struct sockaddr identifier;
87 int addrLen = sizeof(identifier);
88
89 NSString *address;
90 char *addressCString;
91
92 sockfd = sock;
93
94 if ( getpeername( sockfd, &identifier, &addrLen ) == -1 )
95 {
96 NSLog( @"ERROR: getpeername() failed" );
97 }
98
99 if ( identifier.sa_family == AF_INET )
100 {
101 struct sockaddr_in addr;
102
103 addrLen = sizeof(addr);
104
105 if ( getpeername( sockfd, (struct sockaddr *)(&addr), &addrLen ) == -1 )
106 {
107 NSLog( @"ERROR: getpeername() failed" );
108 }
109
110 if ( (addressCString = inet_ntoa( addr.sin_addr )) == NULL )
111 {
112 NSLog( @"ERROR: inet_ntoa() failed" );
113 }
114
115 address = [NSString stringWithCString:addressCString];
116 }
117 else
118 {
119 struct sockaddr_un addr;
120
121 addrLen = sizeof(addr);
122
123 if ( getpeername( sockfd, (struct sockaddr *)(&addr), &addrLen ) == -1 )
124 {
125 NSLog( @"ERROR: getpeername() failed" );
126 }
127
128 NSLog( @"client connection: %s", addr.sun_path );
129
130 address = [NSString stringWithString:@"127.0.0.1"];
131 }
132
133 [rootProxy server:self connectedWithSocket:sockfd];
134
135 [self setAddress:address];
136 [self setAction:nil];
137 }
138
139 - (void)run
140 {
141 struct timeval tv;
142 fd_set fdset, master;
143 int numfds;
144
145 int result;
146
147 PacketHeader header;
148 char *data = NULL;
149
150 tv.tv_sec = 2;
151 tv.tv_usec = 0;
152
153 FD_ZERO( &fdset );
154 FD_ZERO( &master );
155 FD_SET( sockfd, &master );
156
157 numfds = sockfd + 1;
158
159 NSLog( @"SERVER start" );
160
161 for (;;)
162 {
163 fdset = master;
164
165 select( numfds, &fdset, NULL, NULL, &tv );
166
167 if ( FD_ISSET( sockfd, &fdset ) )
168 {
169 if ( (result = ReadBuffer( sockfd, (char *)(&header), sizeof(header) )) != sizeof(header) )
170 {
171 break;
172 }
173
174 if ( !VerifyChecksum( header.checksum ) )
175 {
176 NSLog( @"checksum failed" );
177 }
178
179 if ( header.size != 0 )
180 {
181 if ( (data = (char *)malloc( header.size )) == NULL )
182 {
183 NSLog( @"failed to allocate buffer for reading a network packet" );
184 break;
185 }
186
187 if ( (result = ReadBuffer( sockfd, data, header.size )) != header.size )
188 {
189 NSLog( @"failed to read the data of a network packet" );
190 free( data );
191 break;
192 }
193 }
194
195 //NSLog( @"SERVER message %i/%i/%i", header.checksum, header.function, header.size );
196
197 switch ( header.function )
198 {
199 case 1:
200 [self sendProcessList];
201 break;
202
203 case 3:
204 [self handleClearSearch];
205 break;
206
207 case 5:
208 [self handleSearch:data size:header.size];
209 break;
210
211 case 8:
212 [self handleChange:data size:header.size];
213 break;
214
215 case 10:
216 [self handlePauseTarget];
217 break;
218
219 case 14:
220 [self handleUndo];
221 break;
222
223 case 16:
224 [self handleRedo];
225 break;
226
227 case 18:
228 [self handleSetTargetPID:data size:header.size];
229 break;
230
231 }
232
233 if ( header.size != 0 )
234 {
235 free( data );
236 }
237 }
238 }
239
240 close( sockfd );
241
242 NSLog( @"SERVER close" );
243
244 [rootProxy serverDisconnected:self];
245 }
246
247
248 - (void)setAddress:(NSString *)address
249 {
250 [rootProxy server:self changedAddress:address];
251 }
252
253 - (void)setAction:(NSString *)action
254 {
255 if ( action == nil )
256 {
257 [rootProxy server:self changedAction:@"Idle"];
258 }
259 else
260 {
261 [rootProxy server:self changedAction:action];
262 }
263 }
264
265 - (void)firstSearchString8bit:(char const *)value size:(int)vsize
266 {
267 kern_return_t result;
268
269 vm_address_t address = 0x0;
270 vm_size_t size = 0;
271 vm_region_basic_info_data_t info;
272 mach_msg_type_number_t infoCnt = 8;
273 mach_port_t object_name = 0;
274
275 char *data;
276 vm_size_t dataLength;
277
278 TCaddress *results = NULL;
279 int resultsAmount = 0;
280
281 for (;;)
282 {
283 if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
284 {
285 if ( result != KERN_INVALID_ADDRESS )
286 {
287 NSLog( @"vm_region returned error: %i", result );
288 }
289 break;
290 }
291
292 if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
293 {
294 data = (char *)malloc( size );
295 dataLength = size;
296
297 if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
298 {
299 NSLog( @"vm_read_overwrite returned error: %i", result );
300 free( data );
301 break;
302 }
303
304 if ( result == KERN_SUCCESS )
305 {
306 //int i, top = dataLength - vsize;
307
308 if ( (results = realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
309 {
310 NSLog( @"ERROR: could not expand buffer" );
311 exit(0);
312 }
313
314 resultsAmount += bmsearch( (char *)value, vsize, (char *)data, dataLength, (void *)address, (void **)((char *)results+TCAddressSize*resultsAmount) );
315 //resultsAmount += TBM( (char *)value, vsize, data, dataLength, (void **)((char *)results+TCAddressSize*resultsAmount) );
316 //resultsAmount += SMITH( data, dataLength, (char *)value, vsize, (void **)((char *)results+TCAddressSize*resultsAmount) );
317
318 /*for ( i = 0; i < top; i++ )
319 {
320 if ( strncmp( value, data+i, vsize ) == 0 )
321 {
322 results[resultsAmount++] = (TCaddress)address + i;
323 }
324 }*/
325 }
326
327 free( data );
328 }
329
330 address += size;
331 }
332
333 realloc( results, TCAddressSize*resultsAmount );
334 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_8_BIT data:results amount:resultsAmount]];
335
336 NSLog( @"found %i of %i", resultsAmount, value );
337 }
338
339 - (void)firstSearchIntegerChar:(int8_t)value
340 {
341 kern_return_t result;
342
343 vm_address_t address = 0x0;
344 vm_size_t size = 0;
345 vm_region_basic_info_data_t info;
346 mach_msg_type_number_t infoCnt = 8;
347 mach_port_t object_name = 0;
348
349 int8_t *data;
350 vm_size_t dataLength;
351
352 TCaddress *results = NULL;
353 int resultsAmount = 0;
354
355 for (;;)
356 {
357 if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
358 {
359 if ( result != KERN_INVALID_ADDRESS )
360 {
361 NSLog( @"vm_region returned error: %i", result );
362 }
363 break;
364 }
365
366 if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
367 {
368 data = (int8_t *)malloc( size );
369 dataLength = size;
370
371 if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
372 {
373 NSLog( @"vm_read_overwrite returned error: %i", result );
374 free( data );
375 break;
376 }
377
378 if ( result == KERN_SUCCESS )
379 {
380 int i;
381
382 if ( (results = (TCaddress *)realloc( results, TCAddressSize*resultsAmount + TCAddressSize*dataLength )) == NULL )
383 {
384 NSLog( @"ERROR: could not expand buffer" );
385 exit(0);
386 }
387
388 for ( i = 0; i < dataLength; i++ )
389 {
390 if ( *(data+i) == value )
391 {
392 results[resultsAmount++] = (TCaddress)address + i;
393 }
394 }
395 }
396
397 free( data );
398 }
399
400 address += size;
401 }
402
403 realloc( results, TCAddressSize*resultsAmount );
404 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_8_BIT data:results amount:resultsAmount]];
405
406 NSLog( @"found %i of %i", resultsAmount, value );
407 }
408
409 - (void)firstSearchIntegerShort:(int16_t)value
410 {
411 kern_return_t result;
412
413 vm_address_t address = 0x0;
414 vm_size_t size = 0;
415 vm_region_basic_info_data_t info;
416 mach_msg_type_number_t infoCnt = 8;
417 mach_port_t object_name = 0;
418
419 int16_t *data;
420 vm_size_t dataLength;
421
422 TCaddress *results = NULL;
423 int resultsAmount = 0;
424
425 for (;;)
426 {
427 if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
428 {
429 if ( result != KERN_INVALID_ADDRESS )
430 {
431 NSLog( @"vm_region returned error: %i", result );
432 }
433 break;
434 }
435
436 if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
437 {
438 data = (int16_t *)malloc( size );
439 dataLength = size;
440
441 if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
442 {
443 NSLog( @"vm_read_overwrite returned error: %i", result );
444 free( data );
445 break;
446 }
447
448 if ( result == KERN_SUCCESS )
449 {
450 int i, top = dataLength / sizeof(value);
451
452 if ( (results = (TCaddress *)realloc( results, TCAddressSize*resultsAmount + 2*dataLength )) == NULL )
453 {
454 NSLog( @"ERROR: could not expand buffer" );
455 exit(0);
456 }
457
458 for ( i = 0; i < top; i++ )
459 {
460 if ( *(data+i) == value )
461 {
462 results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
463 }
464 }
465 }
466
467 free( data );
468 }
469
470 address += size;
471 }
472
473 realloc( results, TCAddressSize*resultsAmount );
474 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_16_BIT data:results amount:resultsAmount]];
475
476 NSLog( @"found %i of %i", resultsAmount, value );
477 }
478
479 - (void)firstSearchIntegerLong:(int32_t)value
480 {
481 kern_return_t result;
482
483 vm_address_t address = 0x0;
484 vm_size_t size = 0;
485 vm_region_basic_info_data_t info;
486 mach_msg_type_number_t infoCnt = 8;
487 mach_port_t object_name = 0;
488
489 int32_t *data;
490 vm_size_t dataLength;
491
492 TCaddress *results = NULL;
493 int resultsAmount = 0;
494
495 /*unsigned zone_count = 10;
496 vm_address_t *zones = (vm_address_t *)malloc( zone_count * sizeof(vm_address_t) );
497 //memory_reader_t reader;
498
499 if ( (result = malloc_get_all_zones( processTask, NULL, &zones, &zone_count )) != KERN_SUCCESS )
500 {
501 NSLog( @"malloc_get_all_zones error: %i", result );
502 }
503 else
504 {
505 //address = zones[0];
506
507 int i;
508
509 for ( i = 0; i < 10; i++ )
510 {
511 NSLog( @"malloc_get_all_zones[%i] = %X", i, (vm_address_t)zones[i] );
512 }
513 }*/
514
515 for (;;)
516 {
517 if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
518 {
519 if ( result != KERN_INVALID_ADDRESS )
520 {
521 NSLog( @"vm_region returned error: %i", result );
522 }
523 break;
524 }
525
526 if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE) )
527 {
528 data = (int32_t *)malloc( size );
529 dataLength = size;
530
531 //NSLog( @"address: %.8X size: %i", address, size );
532
533 if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
534 {
535 NSLog( @"vm_read_overwrite returned error: %i", result );
536 free( data );
537 break;
538 }
539
540 if ( result == KERN_SUCCESS )
541 {
542 int i, top = dataLength / sizeof(value);
543
544 if ( (results = (TCaddress *)realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
545 {
546 NSLog( @"ERROR: could not expand buffer" );
547 exit(0);
548 }
549
550 for ( i = 0; i < top; i++ )
551 {
552 if ( *(data+i) == value )
553 {
554 results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
555 }
556 }
557 }
558
559 free( data );
560 }
561
562 address += size;
563 }
564
565 realloc( results, TCAddressSize*resultsAmount );
566 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_32_BIT data:results amount:resultsAmount]];
567
568 NSLog( @"found %i of %i", resultsAmount, value );
569 }
570
571 - (void)firstSearchDecimalFloat:(float)value
572 {
573 kern_return_t result;
574
575 vm_address_t address = 0x0;
576 vm_size_t size = 0;
577 vm_region_basic_info_data_t info;
578 mach_msg_type_number_t infoCnt = 8;
579 mach_port_t object_name = 0;
580
581 float *data;
582 vm_size_t dataLength;
583
584 TCaddress *results = NULL;
585 int resultsAmount = 0;
586
587 for (;;)
588 {
589 if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
590 {
591 if ( result != KERN_INVALID_ADDRESS )
592 {
593 NSLog( @"vm_region returned error: %i", result );
594 }
595 break;
596 }
597
598 if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
599 {
600 data = (float *)malloc( size );
601 dataLength = size;
602
603 if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
604 {
605 NSLog( @"vm_read_overwrite returned error: %i", result );
606 free( data );
607 break;
608 }
609
610 if ( result == KERN_SUCCESS )
611 {
612 int i, top = dataLength / sizeof(value);
613
614 if ( (results = realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
615 {
616 NSLog( @"ERROR: could not expand buffer" );
617 exit(0);
618 }
619
620 for ( i = 0; i < top; i++ )
621 {
622 if ( CMCompareFloatsWithEpsilon( *(data+i), value, 0.1f ) == 0 )
623 {
624 results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
625 }
626 }
627 }
628
629 free( data );
630 }
631
632 address += size;
633 }
634
635 realloc( results, TCAddressSize*resultsAmount );
636 [searchResults addObject:[SearchResults resultsWithType:TYPE_DECIMAL size:SIZE_32_BIT data:results amount:resultsAmount]];
637
638 NSLog( @"found %i of %i", resultsAmount, value );
639 }
640
641 - (void)firstSearchDecimalDouble:(double)value
642 {
643 kern_return_t result;
644
645 vm_address_t address = 0x0;
646 vm_size_t size = 0;
647 vm_region_basic_info_data_t info;
648 mach_msg_type_number_t infoCnt = 8;
649 mach_port_t object_name = 0;
650
651 double *data;
652 vm_size_t dataLength;
653
654 TCaddress *results = NULL;
655 int resultsAmount = 0;
656
657 NSLog( @"float search" );
658
659 for (;;)
660 {
661 if ( (result = vm_region( processTask, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)(&info), &infoCnt, &object_name )) != KERN_SUCCESS )
662 {
663 if ( result != KERN_INVALID_ADDRESS )
664 {
665 NSLog( @"vm_region returned error: %i", result );
666 }
667 break;
668 }
669
670 if ( (info.protection & VM_PROT_READ) && (info.protection & VM_PROT_WRITE))
671 {
672 data = (double *)malloc( size );
673 dataLength = size;
674
675 if ( (result = vm_read_overwrite( processTask, address, size, (vm_address_t)data, &dataLength )) != KERN_SUCCESS && result != KERN_PROTECTION_FAILURE )
676 {
677 NSLog( @"vm_read_overwrite returned error: %i", result );
678 free( data );
679 break;
680 }
681
682 if ( result == KERN_SUCCESS )
683 {
684 int i, top = dataLength / sizeof(value);
685
686 if ( (results = realloc( results, TCAddressSize*resultsAmount + dataLength )) == NULL )
687 {
688 NSLog( @"ERROR: could not expand buffer" );
689 exit(0);
690 }
691
692 for ( i = 0; i < top; i++ )
693 {
694 if ( CMCompareDoublesWithEpsilon( *(data+i), value, 0.1 ) == 0 )
695 {
696 results[resultsAmount++] = (TCaddress)address + i * sizeof(value);
697 }
698 }
699 }
700
701 free( data );
702 }
703
704 address += size;
705 }
706
707 realloc( results, TCAddressSize*resultsAmount );
708 [searchResults addObject:[SearchResults resultsWithType:TYPE_DECIMAL size:SIZE_64_BIT data:results amount:resultsAmount]];
709
710 NSLog( @"found %i of %i", resultsAmount, value );
711 }
712
713
714 - (void)searchString8bit:(char const *)value size:(int)vsize
715 {
716 kern_return_t result;
717
718 char *data;
719 vm_size_t dataLength;
720
721 TCaddress *results;
722 int resultsAmount = 0;
723
724 SearchResults *lastResults = [searchResults lastObject];
725 TCaddress *lastResultsData = [lastResults data];
726 int i, lastResultsAmount = [lastResults amount];
727
728 if ( [lastResults type] != TYPE_INTEGER || [lastResults size] != SIZE_8_BIT )
729 {
730 [self sendError:@"This search is incompatible with the previous search." fatal:NO];
731 return;
732 }
733
734 if ( (data = (char *)malloc( vsize )) == NULL )
735 {
736 NSLog( @"ERROR: could not create buffer" );
737
738 [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
739 return;
740 }
741
742 if ( (results = (TCaddress *)malloc( TCAddressSize*lastResultsAmount )) == NULL )
743 {
744 NSLog( @"ERROR: could not create buffer" );
745
746 [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
747 free( data );
748 return;
749 }
750
751 for ( i = 0; i < lastResultsAmount; i++ )
752 {
753 TCaddress address = lastResultsData[i];
754
755 //dataLength = sizeof(data);
756
757 if ( (result = vm_read_overwrite( processTask, address, vsize, (vm_address_t)(data), &dataLength )) == KERN_SUCCESS )
758 {
759 if ( memcmp( data, value, dataLength ) == 0 )
760 {
761 results[resultsAmount++] = address;
762 }
763 }
764 else
765 {
766 if ( result != KERN_PROTECTION_FAILURE )
767 {
768 NSLog( @"vm_read_overwrite returned error: %i", result );
769 break;
770 }
771 }
772 }
773
774 realloc( results, TCAddressSize*resultsAmount );
775 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_8_BIT data:results amount:resultsAmount]];
776
777 free( data );
778
779 NSLog( @"found %i of %i", resultsAmount, value );
780 }
781
782 - (void)searchIntegerChar:(int8_t)value
783 {
784 kern_return_t result;
785
786 int8_t data;
787 vm_size_t dataLength;
788
789 TCaddress *results;
790 int resultsAmount = 0;
791
792 SearchResults *lastResults = [searchResults lastObject];
793 TCaddress *lastResultsData = [lastResults data];
794 int i, lastResultsAmount = [lastResults amount];
795
796 if ( [lastResults type] != TYPE_INTEGER || [lastResults size] != SIZE_8_BIT )
797 {
798 [self sendError:@"This search is incompatible with the previous search." fatal:NO];
799 return;
800 }
801
802 if ( (results = (TCaddress *)malloc( TCAddressSize*lastResultsAmount )) == NULL )
803 {
804 NSLog( @"ERROR: could not create buffer" );
805
806 [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
807 return;
808 }
809
810 for ( i = 0; i < lastResultsAmount; i++ )
811 {
812 TCaddress address = lastResultsData[i];
813
814 dataLength = sizeof(data);
815
816 if ( (result = vm_read_overwrite( processTask, address, sizeof(data), (vm_address_t)(&data), &dataLength )) == KERN_SUCCESS )
817 {
818 if ( data == value )
819 {
820 results[resultsAmount++] = address;
821 }
822 }
823 else
824 {
825 if ( result != KERN_PROTECTION_FAILURE )
826 {
827 NSLog( @"vm_read_overwrite returned error: %i", result );
828 break;
829 }
830 }
831 }
832
833 realloc( results, TCAddressSize*resultsAmount );
834 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_8_BIT data:results amount:resultsAmount]];
835
836 NSLog( @"found %i of %i", resultsAmount, value );
837 }
838
839 - (void)searchIntegerShort:(int16_t)value
840 {
841 kern_return_t result;
842
843 int16_t data;
844 vm_size_t dataLength;
845
846 TCaddress *results;
847 int resultsAmount = 0;
848
849 SearchResults *lastResults = [searchResults lastObject];
850 TCaddress *lastResultsData = [lastResults data];
851 int i, lastResultsAmount = [lastResults amount];
852
853 if ( [lastResults type] != TYPE_INTEGER || [lastResults size] != SIZE_16_BIT )
854 {
855 [self sendError:@"This search is incompatible with the previous search." fatal:NO];
856 return;
857 }
858
859 if ( (results = (TCaddress *)malloc( TCAddressSize*lastResultsAmount )) == NULL )
860 {
861 NSLog( @"ERROR: could not create buffer" );
862
863 [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
864 return;
865 }
866
867 for ( i = 0; i < lastResultsAmount; i++ )
868 {
869 TCaddress address = lastResultsData[i];
870
871 dataLength = sizeof(data);
872
873 if ( (result = vm_read_overwrite( processTask, address, sizeof(data), (vm_address_t)(&data), &dataLength )) == KERN_SUCCESS )
874 {
875 if ( data == value )
876 {
877 results[resultsAmount++] = address;
878 }
879 }
880 else
881 {
882 if ( result != KERN_PROTECTION_FAILURE )
883 {
884 NSLog( @"vm_read_overwrite returned error: %i", result );
885 break;
886 }
887 }
888 }
889
890 realloc( results, TCAddressSize*resultsAmount );
891 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_16_BIT data:results amount:resultsAmount]];
892
893 NSLog( @"found %i of %i", resultsAmount, value );
894 }
895
896 - (void)searchIntegerLong:(int32_t)value
897 {
898 kern_return_t result;
899
900 int32_t data;
901 vm_size_t dataLength;
902
903 TCaddress *results;
904 int resultsAmount = 0;
905
906 SearchResults *lastResults = [searchResults lastObject];
907 TCaddress *lastResultsData = [lastResults data];
908 int i, lastResultsAmount = [lastResults amount];
909
910 if ( [lastResults type] != TYPE_INTEGER || [lastResults size] != SIZE_32_BIT )
911 {
912 [self sendError:@"This search is incompatible with the previous search." fatal:NO];
913 return;
914 }
915
916 if ( (results = (TCaddress *)malloc( TCAddressSize*lastResultsAmount )) == NULL )
917 {
918 NSLog( @"ERROR: could not create buffer" );
919
920 [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
921 return;
922 }
923
924 for ( i = 0; i < lastResultsAmount; i++ )
925 {
926 TCaddress address = lastResultsData[i];
927
928 dataLength = sizeof(data);
929
930 if ( (result = vm_read_overwrite( processTask, address, sizeof(data), (vm_address_t)(&data), &dataLength )) == KERN_SUCCESS )
931 {
932 if ( data == value )
933 {
934 results[resultsAmount++] = address;
935 }
936 }
937 else
938 {
939 if ( result != KERN_PROTECTION_FAILURE )
940 {
941 NSLog( @"vm_read_overwrite returned error: %i", result );
942 break;
943 }
944 }
945 }
946
947 realloc( results, TCAddressSize*resultsAmount );
948 [searchResults addObject:[SearchResults resultsWithType:TYPE_INTEGER size:SIZE_32_BIT data:results amount:resultsAmount]];
949
950 NSLog( @"found %i of %i", resultsAmount, value );
951 }
952
953 - (void)searchDecimalFloat:(float)value
954 {
955 kern_return_t result;
956
957 float data;
958 vm_size_t dataLength;
959
960 TCaddress *results;
961 int resultsAmount = 0;
962
963 SearchResults *lastResults = [searchResults lastObject];
964 TCaddress *lastResultsData = [lastResults data];
965 int i, lastResultsAmount = [lastResults amount];
966
967 if ( [lastResults type] != TYPE_DECIMAL || [lastResults size] != SIZE_32_BIT )
968 {
969 [self sendError:@"This search is incompatible with the previous search." fatal:NO];
970 return;
971 }
972
973 if ( (results = (TCaddress *)malloc( TCAddressSize*lastResultsAmount )) == NULL )
974 {
975 NSLog( @"ERROR: could not create buffer" );
976
977 [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
978 return;
979 }
980
981 for ( i = 0; i < lastResultsAmount; i++ )
982 {
983 TCaddress address = lastResultsData[i];
984
985 dataLength = sizeof(data);
986
987 if ( (result = vm_read_overwrite( processTask, address, sizeof(data), (vm_address_t)(&data), &dataLength )) == KERN_SUCCESS )
988 {
989 if ( CMCompareFloatsWithEpsilon( data, value, 0.1f ) == 0 )
990 {
991 results[resultsAmount++] = address;
992 }
993 }
994 else
995 {
996 if ( result != KERN_PROTECTION_FAILURE )
997 {
998 NSLog( @"vm_read_overwrite returned error: %i", result );
999 break;
1000 }
1001 }
1002 }
1003
1004 realloc( results, TCAddressSize*resultsAmount );
1005 [searchResults addObject:[SearchResults resultsWithType:TYPE_DECIMAL size:SIZE_32_BIT data:results amount:resultsAmount]];
1006
1007 NSLog( @"found %i of %i", resultsAmount, value );
1008 }
1009
1010 - (void)searchDecimalDouble:(double)value
1011 {
1012 kern_return_t result;
1013
1014 double data;
1015 vm_size_t dataLength;
1016
1017 TCaddress *results;
1018 int resultsAmount = 0;
1019
1020 SearchResults *lastResults = [searchResults lastObject];
1021 TCaddress *lastResultsData = [lastResults data];
1022 int i, lastResultsAmount = [lastResults amount];
1023
1024 if ( [lastResults type] != TYPE_DECIMAL || [lastResults size] != SIZE_64_BIT )
1025 {
1026 [self sendError:@"This search is incompatible with the previous search." fatal:NO];
1027 return;
1028 }
1029
1030 if ( (results = (TCaddress *)malloc( TCAddressSize*lastResultsAmount )) == NULL )
1031 {
1032 NSLog( @"ERROR: could not create buffer" );
1033
1034 [self sendError:@"The server cancelled the search because it ran out of memory." fatal:NO];
1035 return;
1036 }
1037
1038 for ( i = 0; i < lastResultsAmount; i++ )
1039 {
1040 TCaddress address = lastResultsData[i];
1041
1042 dataLength = sizeof(data);
1043
1044 if ( (result = vm_read_overwrite( processTask, address, sizeof(data), (vm_address_t)(&data), &dataLength )) == KERN_SUCCESS )
1045 {
1046 if ( CMCompareDoublesWithEpsilon( data, value, 0.1 ) == 0 )
1047 {
1048 results[resultsAmount++] = address;
1049 }
1050 }
1051 else
1052 {
1053 if ( result != KERN_PROTECTION_FAILURE )
1054 {
1055 NSLog( @"vm_read_overwrite returned error: %i", result );
1056 break;
1057 }
1058 }
1059 }
1060
1061 realloc( results, TCAddressSize*resultsAmount );
1062 [searchResults addObject:[SearchResults resultsWithType:TYPE_DECIMAL size:SIZE_64_BIT data:results amount:resultsAmount]];
1063
1064 NSLog( @"found %i of %i", resultsAmount, value );
1065 }
1066
1067
1068 - (void)changeString8bit:(char const *)value size:(int)vsize addresses:(TCaddress *)addresses count:(int)count
1069 {
1070 int failCount = 0;
1071 int i;
1072
1073 for ( i = 0; i < count; i++ )
1074 {
1075 if ( vm_write( processTask, (vm_address_t)addresses[i], (vm_offset_t)value, vsize ) != KERN_SUCCESS )
1076 {
1077 failCount++;
1078 }
1079 }
1080
1081 if ( failCount > 0 )
1082 {
1083 [self sendError:[NSString stringWithFormat:@"%i of the selected variables could not be changed.", failCount] fatal:NO];
1084 }
1085 }
1086
1087 - (void)changeIntegerChar:(int8_t)value addresses:(TCaddress *)addresses count:(int)count
1088 {
1089 int failCount = 0;
1090 int i;
1091
1092 for ( i = 0; i < count; i++ )
1093 {
1094 if ( vm_write( processTask, (vm_address_t)addresses[i], (vm_offset_t)(&value), sizeof(value) ) != KERN_SUCCESS )
1095 {
1096 failCount++;
1097 }
1098 }
1099
1100 if ( failCount > 0 )
1101 {
1102 [self sendError:[NSString stringWithFormat:@"%i of the selected variables could not be changed.", failCount] fatal:NO];
1103 }
1104 }
1105
1106 - (void)changeIntegerShort:(int16_t)value addresses:(TCaddress *)addresses count:(int)count
1107 {
1108 int failCount = 0;
1109 int i;
1110
1111 for ( i = 0; i < count; i++ )
1112 {
1113 if ( vm_write( processTask, (vm_address_t)addresses[i], (vm_offset_t)(&value), sizeof(value) ) != KERN_SUCCESS )
1114 {
1115 failCount++;
1116 }
1117 }
1118
1119 if ( failCount > 0 )
1120 {
1121 [self sendError:[NSString stringWithFormat:@"%i of the selected variables could not be changed.", failCount] fatal:NO];
1122 }
1123 }
1124
1125 - (void)changeIntegerLong:(int32_t)value addresses:(TCaddress *)addresses count:(int)count
1126 {
1127 int failCount = 0;
1128 int i;
1129
1130 for ( i = 0; i < count; i++ )
1131 {
1132 if ( vm_write( processTask, (vm_address_t)addresses[i], (vm_offset_t)(&value), sizeof(value) ) != KERN_SUCCESS )
1133 {
1134 failCount++;
1135 }
1136 }
1137
1138 if ( failCount > 0 )
1139 {
1140 [self sendError:[NSString stringWithFormat:@"%i of the selected variables could not be changed.", failCount] fatal:NO];
1141 }
1142 }
1143
1144 - (void)changeDecimalFloat:(float)value addresses:(TCaddress *)addresses count:(int)count
1145 {
1146 int failCount = 0;
1147 int i;
1148
1149 for ( i = 0; i < count; i++ )
1150 {
1151 if ( vm_write( processTask, (vm_address_t)addresses[i], (vm_offset_t)(&value), sizeof(value) ) != KERN_SUCCESS )
1152 {
1153 failCount++;
1154 }
1155 }
1156
1157 if ( failCount > 0 )
1158 {
1159 [self sendError:[NSString stringWithFormat:@"%i of the selected variables could not be changed.", failCount] fatal:NO];
1160 }
1161 }
1162
1163 - (void)changeDecimalDouble:(double)value addresses:(TCaddress *)addresses count:(int)count
1164 {
1165 int failCount = 0;
1166 int i;
1167
1168 for ( i = 0; i < count; i++ )
1169 {
1170 if ( vm_write( processTask, (vm_address_t)addresses[i], (vm_offset_t)(&value), sizeof(value) ) != KERN_SUCCESS )
1171 {
1172 failCount++;
1173 }
1174 }
1175
1176 if ( failCount > 0 )
1177 {
1178 [self sendError:[NSString stringWithFormat:@"%i of the selected variables could not be changed.", failCount] fatal:NO];
1179 }
1180 }
1181
1182
1183 - (void)sendProcessList
1184 {
1185 NSArray *processList = [rootProxy serverProcessList];
1186
1187 NSNumber *pid;
1188 u_int32_t pidNum;
1189 NSString *name;
1190
1191 PacketHeader header;
1192
1193 char *buffer, *ptr;
1194
1195 // PROCESS COUNT
1196 int length = sizeof(u_int32_t);
1197 int lengthAfter;
1198
1199 u_int32_t processCount = [processList count];
1200
1201 int i, max = processCount;
1202
1203 header.checksum = RandomChecksum();
1204 header.function = 2;
1205
1206 for ( i = 0; i < max; i++ )
1207 {
1208 pid = [[processList objectAtIndex:i] objectForKey:@"NSApplicationProcessIdentifier"];
1209 name = [[processList objectAtIndex:i] objectForKey:@"NSApplicationName"];
1210
1211 // PID NAME
1212 length += sizeof(u_int32_t) + [name length] + 1;
1213 }
1214
1215 header.size = length;
1216 length += sizeof(header);
1217 // HEADER
1218
1219 if ( (buffer = (char *)malloc( length ))==NULL )
1220 {
1221 NSLog( @"sendProcessList failed" );
1222 return;
1223 }
1224
1225 ptr = buffer;
1226
1227 COPY_TO_BUFFER( ptr, &header, sizeof(header) );
1228 COPY_TO_BUFFER( ptr, &processCount, sizeof(processCount) );
1229
1230 for ( i = 0; i < max; i++ )
1231 {
1232 pidNum = [[[processList objectAtIndex:i] objectForKey:@"NSApplicationProcessIdentifier"] unsignedLongValue];
1233 name = [[processList objectAtIndex:i] objectForKey:@"NSApplicationName"];
1234
1235 COPY_TO_BUFFER( ptr, &pidNum, sizeof(pid) );
1236 COPY_TO_BUFFER( ptr, [name lossyCString], [name length] + 1 );
1237 }
1238
1239 lengthAfter = length;
1240
1241 if ( SendBuffer( sockfd, buffer, &lengthAfter ) == -1 || lengthAfter != length )
1242 {
1243 NSLog( @"sendProcessList failed" );
1244 }
1245
1246 free( buffer );
1247 }
1248
1249
1250 - (void)sendSearchFinished
1251 {
1252 PacketHeader header;
1253 int length = sizeof(header);
1254
1255 header.checksum = RandomChecksum();
1256 header.function = 6;
1257 header.size = 0;
1258
1259 if ( SendBuffer( sockfd, (char *)(&header), &length ) == -1 || length != sizeof(header) )
1260 {
1261 NSLog( @"sendSearchFinished failed" );
1262 }
1263 }
1264
1265 - (void)sendVariableList:(TCaddress const *)data amount:(int)amount
1266 {
1267 PacketHeader header;
1268 int length;
1269 int lengthAfter;
1270
1271 char *buffer, *ptr;
1272
1273 header.checksum = RandomChecksum();
1274 header.function = 7;
1275 header.size = sizeof(amount) + sizeof(maxSearchResultsAmount) + TCAddressSize*maxSearchResultsAmount;
1276 // AMOUNT MAX AMOUNT DATA
1277
1278 lengthAfter = length = header.size + sizeof(header);
1279
1280 if ( (buffer = (char *)malloc( length )) == NULL )
1281 {
1282 NSLog( @"sendVariableList:amount: failed" );
1283 return;
1284 }
1285
1286 ptr = buffer;
1287
1288 COPY_TO_BUFFER( ptr, &header, sizeof(header) );
1289 COPY_TO_BUFFER( ptr, &amount, sizeof(amount) );
1290 COPY_TO_BUFFER( ptr, &maxSearchResultsAmount, sizeof(maxSearchResultsAmount) );
1291 COPY_TO_BUFFER( ptr, data, TCAddressSize*maxSearchResultsAmount );
1292
1293 if ( SendBuffer( sockfd, buffer, &length ) == -1 || lengthAfter != length )
1294 {
1295 NSLog( @"sendVariableList:amount: failed" );
1296 }
1297
1298 free( buffer );
1299 }
1300
1301 - (void)sendChangeFinished
1302 {
1303 PacketHeader header;
1304 int length = sizeof(header);
1305
1306 header.checksum = RandomChecksum();
1307 header.function = 9;
1308 header.size = 0;
1309
1310 if ( SendBuffer( sockfd, (char *)(&header), &length ) == -1 || length != sizeof(header) )
1311 {
1312 NSLog( @"sendChangeFinished failed" );
1313 }
1314 }
1315
1316 - (void)sendError:(NSString *)msg fatal:(BOOL)fatal
1317 {
1318 PacketHeader header;
1319 int length;
1320 int lengthAfter;
1321
1322 u_int32_t type = (fatal)? 1:0;
1323
1324 char *buffer, *ptr;
1325
1326 header.checksum = RandomChecksum();
1327 header.function = 11;
1328 header.size = sizeof(type) + [msg length] + 1;
1329 // FATAL STRING
1330
1331 lengthAfter = length = header.size + sizeof(header);
1332
1333 if ( (buffer = (char *)malloc( length )) == NULL )
1334 {
1335 NSLog( @"sendError:fatal: failed" );
1336 return;
1337 }
1338
1339 ptr = buffer;
1340
1341 COPY_TO_BUFFER( ptr, &header, sizeof(header) );
1342 COPY_TO_BUFFER( ptr, &type, sizeof(type) );
1343 COPY_TO_BUFFER( ptr, [msg lossyCString], [msg length] + 1 );
1344
1345 if ( SendBuffer( sockfd, buffer, &length ) == -1 || lengthAfter != length )
1346 {
1347 NSLog( @"sendError:fatal: failed" );
1348 }
1349
1350 free( buffer );
1351 }
1352
1353 - (void)sendVariableValue:(u_int32_t)index
1354 {
1355
1356 }
1357
1358 - (void)sendUndoFinished
1359 {
1360 PacketHeader header;
1361 int length = sizeof(header);
1362
1363 header.checksum = RandomChecksum();
1364 header.function = 15;
1365 header.size = 0;
1366
1367 if ( SendBuffer( sockfd, (char *)(&header), &length ) == -1 || length != sizeof(header) )
1368 {
1369 NSLog( @"sendUndoFinished failed" );
1370 }
1371 }
1372
1373 - (void)sendRedoFinished
1374 {
1375 PacketHeader header;
1376 int length = sizeof(header);
1377
1378 header.checksum = RandomChecksum();
1379 header.function = 17;
1380 header.size = 0;
1381
1382 if ( SendBuffer( sockfd, (char *)(&header), &length ) == -1 || length != sizeof(header) )
1383 {
1384 NSLog( @"sendRedoFinished failed" );
1385 }
1386 }
1387
1388 - (void)sendUndoRedoStatus
1389 {
1390 PacketHeader header;
1391 int length;
1392 int lengthAfter;
1393
1394 u_int32_t undoCount = (u_int32_t)[searchResults count];
1395 u_int32_t redoCount = (u_int32_t)[searchResultsUndone count];
1396
1397 char *buffer, *ptr;
1398
1399 header.checksum = RandomChecksum();
1400 header.function = 19;
1401 header.size = 2 * sizeof(u_int32_t);
1402
1403 length = lengthAfter = sizeof(header) + header.size;
1404
1405 if ( (buffer = (char *)malloc( length )) == NULL )
1406 {
1407 NSLog( @"sendSetTargetPID: failed" );
1408 }
1409
1410 ptr = buffer;
1411
1412 COPY_TO_BUFFER( ptr, &header, sizeof(header) );
1413 COPY_TO_BUFFER( ptr, &undoCount, sizeof(undoCount) );
1414 COPY_TO_BUFFER( ptr, &redoCount, sizeof(redoCount) );
1415
1416 if ( SendBuffer( sockfd, buffer, &lengthAfter ) == -1 || lengthAfter != length )
1417 {
1418 NSLog( @"sendUndoRedoStatus: failed" );
1419 }
1420
1421 free( buffer );
1422 }
1423
1424 - (void)sendAppLaunched:(NSDictionary *)appInfo
1425 {
1426 PacketHeader header;
1427
1428 char *buffer, *ptr;
1429
1430 int length = 0;
1431 int lengthAfter;
1432
1433 u_int32_t pid = [[appInfo objectForKey:@"NSApplicationProcessIdentifier"] unsignedLongValue];
1434 NSString *name = [appInfo objectForKey:@"NSApplicationName"];
1435
1436 // PID NAME
1437 length += sizeof(u_int32_t) + [name length] + 1;
1438
1439 header.checksum = RandomChecksum();
1440 header.function = 21;
1441 header.size = length;
1442
1443 length += sizeof(header);
1444 // HEADER
1445
1446 if ( (buffer = (char *)malloc( length ))==NULL )
1447 {
1448 NSLog( @"sendAppLaunched: failed" );
1449
1450 return;
1451 }
1452
1453 ptr = buffer;
1454
1455 COPY_TO_BUFFER( ptr, &header, sizeof(header) );
1456 COPY_TO_BUFFER( ptr, &pid, sizeof(pid) );
1457 COPY_TO_BUFFER( ptr, [name lossyCString], [name length] + 1 );
1458
1459 lengthAfter = length;
1460
1461 if ( SendBuffer( sockfd, buffer, &lengthAfter ) == -1 || lengthAfter != length )
1462 {
1463 NSLog( @"sendAppLaunched: failed" );
1464 }
1465
1466 free( buffer );
1467 }
1468
1469
1470 - (void)sendAppQuit:(NSDictionary *)appInfo
1471 {
1472 PacketHeader header;
1473
1474 char *buffer, *ptr;
1475
1476 int length = 0;
1477 int lengthAfter;
1478
1479 u_int32_t pid = [[appInfo objectForKey:@"NSApplicationProcessIdentifier"] unsignedLongValue];
1480
1481 // PID
1482 length += sizeof(pid);
1483
1484 header.checksum = RandomChecksum();
1485 header.function = 22;
1486 header.size = length;
1487
1488 length += sizeof(header);
1489 // HEADER
1490
1491 if ( (buffer = (char *)malloc( length ))==NULL )
1492 {
1493 NSLog( @"sendAppQuit: failed" );
1494
1495 return;
1496 }
1497
1498 ptr = buffer;
1499
1500 COPY_TO_BUFFER( ptr, &header, sizeof(header) );
1501 COPY_TO_BUFFER( ptr, &pid, sizeof(pid) );
1502
1503 lengthAfter = length;
1504
1505 if ( SendBuffer( sockfd, buffer, &lengthAfter ) == -1 || lengthAfter != length )
1506 {
1507 NSLog( @"sendAppQuit: failed" );
1508 }
1509
1510 free( buffer );
1511 }
1512
1513 - (void)sendTargetAppQuit
1514 {
1515 PacketHeader header;
1516 int length = sizeof(header);
1517
1518 header.checksum = RandomChecksum();
1519 header.function = 23;
1520 header.size = 0;
1521
1522 if ( SendBuffer( sockfd, (char *)(&header), &length ) == -1 || length != sizeof(header) )
1523 {
1524 NSLog( @"sendTargetAppQuit failed" );
1525 }
1526 }
1527
1528 - (void)sendPauseFinished:(BOOL)paused
1529 {
1530 PacketHeader header;
1531
1532 char *buffer, *ptr;
1533
1534 // PAUSED
1535 int length = sizeof(paused);
1536 int lengthAfter;
1537
1538 header.checksum = RandomChecksum();
1539 header.function = 24;
1540 header.size = length;
1541
1542 length += sizeof(header);
1543 // HEADER
1544
1545 if ( (buffer = (char *)malloc( length ))==NULL )
1546 {
1547 NSLog( @"sendPauseFinished: failed" );
1548
1549 return;
1550 }
1551
1552 ptr = buffer;
1553
1554 COPY_TO_BUFFER( ptr, &header, sizeof(header) );
1555 COPY_TO_BUFFER( ptr, &paused, sizeof(paused) );
1556
1557 lengthAfter = length;
1558
1559 if ( SendBuffer( sockfd, buffer, &lengthAfter ) == -1 || lengthAfter != length )
1560 {
1561 NSLog( @"sendPauseFinished: failed" );
1562 }
1563
1564 free( buffer );
1565 }
1566
1567
1568 - (void)handleClearSearch
1569 {
1570 [searchResults removeAllObjects];
1571 [searchResultsUndone removeAllObjects];
1572
1573 [self unpause];
1574 }
1575
1576 - (void)handleSearch:(char const *)data size:(int)dataSize
1577 {
1578 TCtype type;
1579 TCsize size;
1580
1581 char *ptr = (char *)data;
1582
1583 [self setAction:@"Searching"];
1584
1585 COPY_FROM_BUFFER( &type, ptr, sizeof(type) );
1586 COPY_FROM_BUFFER( &size, ptr, sizeof(size) );
1587
1588 COPY_FROM_BUFFER( &maxSearchResultsAmount, ptr, sizeof(maxSearchResultsAmount) );
1589
1590 if ( ![searchResults lastObject] )
1591 {
1592 switch ( type )
1593 {
1594 case TYPE_STRING:
1595 {
1596 switch ( size )
1597 {
1598 case SIZE_8_BIT:
1599 {
1600 [self firstSearchString8bit:ptr size:(dataSize - (ptr - data) - 1)];
1601 }
1602 break;
1603 }
1604 }
1605 break;
1606
1607 case TYPE_INTEGER:
1608 {
1609 switch ( size )
1610 {
1611 case SIZE_8_BIT:
1612 {
1613 int8_t value;
1614
1615 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1616 [self firstSearchIntegerChar:value];
1617 }
1618 break;
1619
1620 case SIZE_16_BIT:
1621 {
1622 int16_t value;
1623
1624 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1625 [self firstSearchIntegerShort:value];
1626 }
1627 break;
1628
1629 case SIZE_32_BIT:
1630 {
1631 int32_t value;
1632
1633 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1634 [self firstSearchIntegerLong:value];
1635 }
1636 break;
1637 }
1638 }
1639 break;
1640
1641 case TYPE_DECIMAL:
1642 {
1643 switch ( size )
1644 {
1645 case SIZE_32_BIT:
1646 {
1647 float value;
1648
1649 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1650 [self firstSearchDecimalFloat:value];
1651 }
1652 break;
1653
1654 case SIZE_64_BIT:
1655 {
1656 double value;
1657
1658 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1659 [self firstSearchDecimalDouble:value];
1660 }
1661 break;
1662 }
1663 }
1664 break;
1665 }
1666 }
1667 else
1668 {
1669 switch ( type )
1670 {
1671 case TYPE_STRING:
1672 {
1673 switch ( size )
1674 {
1675 case SIZE_8_BIT:
1676 {
1677 [self searchString8bit:ptr size:(dataSize - (ptr - data))];
1678
1679 break;
1680 }
1681 }
1682 }
1683 break;
1684
1685 case TYPE_INTEGER:
1686 {
1687 switch ( size )
1688 {
1689 case SIZE_8_BIT:
1690 {
1691 int8_t value;
1692
1693 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1694 [self searchIntegerChar:value];
1695
1696 break;
1697 }
1698
1699 case SIZE_16_BIT:
1700 {
1701 int16_t value;
1702
1703 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1704 [self searchIntegerShort:value];
1705
1706 break;
1707 }
1708
1709 case SIZE_32_BIT:
1710 {
1711 int32_t value;
1712
1713 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1714 [self searchIntegerLong:value];
1715
1716 break;
1717 }
1718 }
1719 }
1720 break;
1721
1722 case TYPE_DECIMAL:
1723 {
1724 switch ( size )
1725 {
1726 case SIZE_32_BIT:
1727 {
1728 float value;
1729
1730 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1731 [self searchDecimalFloat:value];
1732 }
1733 break;
1734
1735 case SIZE_64_BIT:
1736 {
1737 double value;
1738
1739 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1740 [self searchDecimalDouble:value];
1741 }
1742 break;
1743 }
1744 }
1745 break;
1746 }
1747 }
1748
1749 [self sendVariableList:[(SearchResults *)[searchResults lastObject] data] amount:[[searchResults lastObject] amount]];
1750 [self sendSearchFinished];
1751 [self sendUndoRedoStatus];
1752
1753 [self setAction:nil];
1754 }
1755
1756 - (void)handleChange:(char const *)data size:(int)dataSize
1757 {
1758 TCtype type;
1759 TCsize size;
1760
1761 TCaddress *addresses = NULL;
1762 int count;
1763
1764 char *ptr = (char *)data;
1765
1766 [self setAction:@"Changing"];
1767
1768 // read out the type and size of the variable.
1769 COPY_FROM_BUFFER( &type, ptr, sizeof(type) );
1770 COPY_FROM_BUFFER( &size, ptr, sizeof(size) );
1771
1772 // read the amount of addresses.
1773 COPY_FROM_BUFFER( &count, ptr, sizeof(count) );
1774
1775 // save the pointer to the addresses.
1776 addresses = (TCaddress *)ptr;
1777 ptr += TCAddressSize*count;
1778
1779 switch ( type )
1780 {
1781 case TYPE_STRING:
1782 {
1783 switch ( size )
1784 {
1785 case SIZE_8_BIT:
1786 {
1787 [self changeString8bit:ptr size:(dataSize - (ptr - data)) addresses:addresses count:count];
1788 }
1789 break;
1790 }
1791 }
1792 break;
1793
1794 case TYPE_INTEGER:
1795 {
1796 switch ( size )
1797 {
1798 case SIZE_8_BIT:
1799 {
1800 int8_t value;
1801
1802 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1803 [self changeIntegerChar:value addresses:addresses count:count];
1804 }
1805 break;
1806
1807 case SIZE_16_BIT:
1808 {
1809 int16_t value;
1810
1811 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1812 [self changeIntegerShort:value addresses:addresses count:count];
1813 }
1814 break;
1815
1816 case SIZE_32_BIT:
1817 {
1818 int32_t value;
1819
1820 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1821 [self changeIntegerLong:value addresses:addresses count:count];
1822 }
1823 break;
1824 }
1825 }
1826 break;
1827
1828 case TYPE_DECIMAL:
1829 {
1830 switch ( size )
1831 {
1832 case SIZE_32_BIT:
1833 {
1834 float value;
1835
1836 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1837 [self changeDecimalFloat:value addresses:addresses count:count];
1838 }
1839 break;
1840
1841 case SIZE_64_BIT:
1842 {
1843 double value;
1844
1845 COPY_FROM_BUFFER( &value, ptr, sizeof(value) );
1846 [self changeDecimalDouble:value addresses:addresses count:count];
1847 }
1848 break;
1849 }
1850 }
1851 break;
1852 }
1853
1854 [self sendChangeFinished];
1855
1856 [self setAction:nil];
1857 }
1858
1859 - (void)handlePauseTarget
1860 {
1861 if ( !processPaused )
1862 {
1863 int wait_status;
1864
1865 NS_DURING
1866 {
1867 if ( ptrace( PT_ATTACH, processID, 0, 0 ) != -1 )
1868 {
1869 if ( waitpid( processID, &wait_status, WUNTRACED ) == processID )
1870 {
1871 if ( WIFSTOPPED(wait_status) )
1872 {
1873 processPaused = YES;
1874 [self sendPauseFinished:YES];
1875 }
1876 else
1877 {
1878 NSLog( @"ERROR: process couldn't be paused" );
1879 [self sendPauseFinished:NO];
1880 [self sendError:@"Could not pause target because of an unknown error." fatal:NO];
1881 }
1882 }
1883 else
1884 {
1885 NSLog( @"ERROR: process couldn't be paused" );
1886 [self sendPauseFinished:NO];
1887 [self sendError:@"Could not pause target because of an unknown error." fatal:NO];
1888 }
1889 }
1890 else
1891 {
1892 NSLog( @"ERROR: process couldn't be paused" );
1893 [self sendPauseFinished:NO];
1894
1895 switch ( errno )
1896 {
1897 case ESRCH:
1898 [self sendError:@"Could not pause target because there is no valid target to pause." fatal:NO];
1899 break;
1900
1901 case EINVAL:
1902 [self sendError:@"Could not pause target because a process cannot pause itself." fatal:NO];
1903 break;
1904
1905 case EBUSY:
1906 [self sendError:@"Could not pause target because the target is being controlled by another instance or application." fatal:NO];
1907 break;
1908
1909 case EPERM:
1910 [self sendError:@"Could not pause target because this type of application cannot be paused." fatal:NO];
1911 break;
1912
1913 default:
1914 [self sendError:@"Could not pause target because of an unknown error." fatal:NO];
1915 break;
1916 }
1917 }
1918 }
1919 NS_HANDLER
1920 {
1921 [self sendPauseFinished:NO];
1922 [self sendError:[NSString stringWithFormat:@"Could not pause target because an exception (%@) was raised: %@", [localException name], [localException reason]] fatal:NO];
1923 }
1924 NS_ENDHANDLER
1925 }
1926 else
1927 {
1928 ptrace( PT_DETACH, processID, 0, 0 );
1929
1930 processPaused = NO;
1931 [self sendPauseFinished:NO];
1932 }
1933 }
1934
1935 - (void)handleUndo
1936 {
1937 SearchResults *results = [searchResults lastObject];
1938
1939 if ( results )
1940 {
1941 [searchResultsUndone addObject:results];
1942 [searchResults removeLastObject];
1943 }
1944
1945 results = [searchResults lastObject];
1946 if ( results )
1947 {
1948 [self sendVariableList:[results data] amount:[results amount]];
1949 }
1950 else
1951 {
1952 [self sendVariableList:NULL amount:0];
1953 }
1954
1955 [self sendUndoFinished];
1956 [self sendUndoRedoStatus];
1957 }
1958
1959 - (void)handleRedo
1960 {
1961 SearchResults *results = [searchResultsUndone lastObject];
1962
1963 if ( results )
1964 {
1965 [searchResults addObject:results];
1966 [searchResultsUndone removeLastObject];
1967 }
1968
1969 results = [searchResults lastObject];
1970 if ( results )
1971 {
1972 [self sendVariableList:[results data] amount:[results amount]];
1973 }
1974 else
1975 {
1976 [self sendVariableList:NULL amount:0];
1977 }
1978
1979 [self sendRedoFinished];
1980 [self sendUndoRedoStatus];
1981 }
1982
1983 - (void)handleSetTargetPID:(char const *)data size:(int)size
1984 {
1985 char *ptr = (char *)data;
1986
1987 pid_t pid;
1988
1989 COPY_FROM_BUFFER( &pid, ptr, sizeof(pid) );
1990
1991 [self setPID:pid];
1992 }
1993
1994
1995 - (void)unpause
1996 {
1997 if ( processPaused )
1998 {
1999 [self handlePauseTarget];
2000 }
2001 }
2002
2003 - (void)setPID:(pid_t)pid
2004 {
2005 kern_return_t result;
2006
2007 [self unpause];
2008
2009 processID = pid;
2010
2011 if ( (result = task_for_pid( current_task(), processID, &processTask)) != KERN_SUCCESS )
2012 {
2013 NSLog( @"task_for_pid returned error: %i", result );
2014 }
2015 }
2016
2017
2018 - (void)dealloc
2019 {
2020 [self unpause];
2021
2022 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
2023
2024 [searchResults release];
2025 [searchResultsUndone release];
2026
2027 [super dealloc];
2028 }
2029
2030
2031 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032 %%%%%%%%%%%%%%%%%%%%%% NSWorkspaceDidLaunchApplicationNotification Notification
2033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
2034
2035
2036 - (void)processListChanged:(NSNotification *)note
2037 {
2038 pid_t pid = [[[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"] intValue];
2039
2040 if ( /*pid != getpid()*/ sockfd != -1 )
2041 {
2042 if ( [[note name] isEqualToString:@"NSWorkspaceDidLaunchApplicationNotification"] )
2043 {
2044 [self sendAppLaunched:[note userInfo]];
2045 }
2046 else
2047 {
2048 [self sendAppQuit:[note userInfo]];
2049
2050 if ( pid == processID )
2051 {
2052 [self sendTargetAppQuit];
2053
2054 // we can't set the new target here because this method is not called
2055 // in the server thread. the client will have to change it.
2056 //[self setPID:[rootProxy serverFirstProcess]];
2057 processPaused = NO;
2058 }
2059 }
2060 }
2061 }
2062
2063
2064 @end
2065
2066
2067 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2068 %%%%%%%%%%%%%%%%%%%%%% Internal Functions
2069 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
2070
2071
2072 #define ASIZE 256
2073 int bmsearch( char *pat, int m, char *text, int n, void *base, void *loc[] )
2074 {
2075 int count = 0;
2076 int i, j, k, skip[ASIZE];
2077
2078 if( m==0 ) return 0;
2079 for( k=0; k<ASIZE; k++ ) skip[k] = m;
2080 for( k=0; k<m-1; k++ ) skip[(int)pat[k]] = m-k-1;
2081
2082 for( k=m-1; k < n; k += skip[(int)text[k] & (ASIZE-1)] ) {
2083 for( j=m-1, i=k; j>=0 && text[i] == pat[j]; j-- ) i--;
2084 if( j == (-1) )
2085 /* SAVE LOCATION */
2086 loc[count++] = (void *)( base+i+1 );
2087 //return( text+i+1 );
2088 }
2089 return count;
2090 }
2091
2092 /*
2093 BOOL compare_float( float a, float b )
2094 {
2095 float const feps = 0.0001f;
2096
2097 return feps > fabsf( a - b );
2098 }
2099
2100 BOOL compare_double( double a, double b )
2101 {
2102 double const deps = 0.0000001;
2103
2104 return deps > fabs( a - b );
2105 }*/
This page took 0.119773 seconds and 5 git commands to generate.