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