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