Skip to content

Commit

Permalink
manually merged swift support in from BlueCocoa fork of class-dump OG
Browse files Browse the repository at this point in the history
  • Loading branch information
lechium committed Apr 10, 2020
1 parent 48bedec commit 3f5c567
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 67 deletions.
122 changes: 87 additions & 35 deletions CDDataCursor.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,27 @@ - (const void *)bytes;

- (void)setOffset:(NSUInteger)newOffset;
{
if (newOffset <= [_data length]) {
_offset = newOffset;
} else {
[NSException raise:NSRangeException format:@"Trying to seek past end of data."];
if (newOffset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
_offset = -'S';
}
else
{
if (newOffset <= [_data length]) {
_offset = newOffset;
} else {
[NSException raise:NSRangeException format:@"Trying to seek past end of data."];
}
}
}

- (void)advanceByLength:(NSUInteger)length;
{
if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
_offset += 10;
return;
}
if (_offset + length <= [_data length]) {
_offset += length;
} else {
Expand All @@ -56,107 +68,128 @@ - (NSUInteger)remaining;
- (uint8_t)readByte;
{
uint8_t result;

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return 0;
}
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadLittleInt16([_data bytes], _offset) & 0xFF;
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}

return result;
}

- (uint16_t)readLittleInt16;
{
uint16_t result;

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return 0;
}
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadLittleInt16([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}

return result;
}

- (uint32_t)readLittleInt32;
{
uint32_t result;

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return 0;
}
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadLittleInt32([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}

return result;
}

- (uint64_t)readLittleInt64;
{
uint64_t result;

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return 0;
}
if (_offset + sizeof(result) <= [_data length]) {
// uint8_t *ptr = [_data bytes] + _offset;
// NSLog(@"%016llx: %02x %02x %02x %02x %02x %02x %02x %02x", _offset, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]);
// uint8_t *ptr = [_data bytes] + _offset;
// NSLog(@"%016llx: %02x %02x %02x %02x %02x %02x %02x %02x", _offset, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]);
result = OSReadLittleInt64([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}

return result;
}

- (uint16_t)readBigInt16;
{
uint16_t result;

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return 0;
}
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadBigInt16([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}

return result;
}

- (uint32_t)readBigInt32;
{
uint32_t result;

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return 0;
}
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadBigInt32([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}

return result;
}

- (uint64_t)readBigInt64;
{
uint64_t result;

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return 0;
}
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadBigInt64([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}

return result;
}

Expand Down Expand Up @@ -192,21 +225,29 @@ - (double)readLittleFloat64;

- (void)appendBytesOfLength:(NSUInteger)length intoData:(NSMutableData *)data;
{
if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return;
}
if (_offset + length <= [_data length]) {
[data appendBytes:(uint8_t *)[_data bytes] + _offset length:length];
_offset += length;
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
}
}

- (void)readBytesOfLength:(NSUInteger)length intoBuffer:(void *)buf;
{
if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return;
}
if (_offset + length <= [_data length]) {
memcpy(buf, (uint8_t *)[_data bytes] + _offset, length);
_offset += length;
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
}
}

Expand All @@ -217,27 +258,34 @@ - (BOOL)isAtEnd;

- (NSString *)readCString;
{
if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
return @"Swift";
}
return [self readStringOfLength:strlen((const char *)[_data bytes] + _offset) encoding:NSASCIIStringEncoding];
}

- (NSString *)readStringOfLength:(NSUInteger)length encoding:(NSStringEncoding)encoding;
{
if (_offset + length <= [_data length]) {
NSString *str;

if (encoding == NSASCIIStringEncoding) {
char *buf;

// Jump through some hoops if the length is padded with zero bytes, as in the case of 10.5's Property List Editor and iSync Plug-in Maker.
buf = malloc(length + 1);
if (buf == NULL) {
NSLog(@"Error: malloc() failed.");
return nil;
}

if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at 1 of %s",__cmd);
return @"Swift";
}
strncpy(buf, (const char *)[_data bytes] + _offset, length);
buf[length] = 0;

str = [[NSString alloc] initWithBytes:buf length:strlen(buf) encoding:encoding];
_offset += length;
free(buf);
Expand All @@ -248,9 +296,13 @@ - (NSString *)readStringOfLength:(NSUInteger)length encoding:(NSStringEncoding)e
return str;
}
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
if (_offset == -'S') {
NSLog(@"Warning: Maybe meet a Swift object at 2 of %s",__cmd);
return @"Swift";
}
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
}

return nil;
}

Expand Down
5 changes: 5 additions & 0 deletions CDLCDyldInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ - (id)initWithDataCursor:(CDMachOFileDataCursor *)cursor;
_dyldInfoCommand.export_off = [cursor readInt32];
_dyldInfoCommand.export_size = [cursor readInt32];

if (_dyldInfoCommand.cmd == -'S' || _dyldInfoCommand.cmdsize == -'S' || _dyldInfoCommand.rebase_off == -'S' || _dyldInfoCommand.rebase_size == -'S' || _dyldInfoCommand.bind_off == -'S' || _dyldInfoCommand.bind_size == -'S' || _dyldInfoCommand.weak_bind_off == -'S' || _dyldInfoCommand.weak_bind_size == -'S' || _dyldInfoCommand.lazy_bind_off == -'S' || _dyldInfoCommand.lazy_bind_size == -'S' || _dyldInfoCommand.export_off == -'S' || _dyldInfoCommand.export_size == -'S') {
NSLog(@"Warning: Meet Swift object at %s",__cmd);
return nil;
}

#if 0
NSLog(@" cmdsize: %08x", _dyldInfoCommand.cmdsize);
NSLog(@" rebase_off: %08x", _dyldInfoCommand.rebase_off);
Expand Down
43 changes: 24 additions & 19 deletions CDMachOFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -325,52 +325,57 @@ - (void)showWarning:(NSString *)warning;
- (NSString *)stringAtAddress:(NSUInteger)address;
{
const void *ptr;

if (address == 0)
return nil;

CDLCSegment *segment = [self segmentContainingAddress:address];
if (segment == nil) {
NSLog(@"Error: Cannot find offset for address 0x%08lx in stringAtAddress:", address);
exit(5);
return nil;
NSLog(@"Warning: Cannot find offset for address 0x%08lx in stringAtAddress:", address);
// exit(5);
return @"Swift";
}

if ([segment isProtected]) {
NSData *d2 = [segment decryptedData];
NSUInteger d2Offset = [segment segmentOffsetForAddress:address];
if (d2Offset == 0)
return nil;

ptr = (uint8_t *)[d2 bytes] + d2Offset;
return [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encoding:NSASCIIStringEncoding];
}

NSUInteger offset = [self dataOffsetForAddress:address];
if (offset == 0)
return nil;

if (offset == -'S') {
NSLog(@"Warning: Meet Swift object at %s",__cmd);
return @"Swift";
}
ptr = (uint8_t *)[self.data bytes] + offset;

return [[NSString alloc] initWithBytes:ptr length:strlen(ptr) encoding:NSASCIIStringEncoding];
}

- (NSUInteger)dataOffsetForAddress:(NSUInteger)address;
{
if (address == 0)
return 0;

CDLCSegment *segment = [self segmentContainingAddress:address];
if (segment == nil) {
NSLog(@"Error: Cannot find offset for address 0x%08lx in dataOffsetForAddress:", address);
exit(5);
NSLog(@"Warning: Cannot find offset for address 0x%08lx in dataOffsetForAddress:", address);
NSLog(@"Warning: Maybe meet a Swift object at %s",__cmd);
// exit(5);
return -'S';
}

// if ([segment isProtected]) {
// NSLog(@"Error: Segment is protected.");
// exit(5);
// }

// if ([segment isProtected]) {
// NSLog(@"Error: Segment is protected.");
// exit(5);
// }
#if 0
NSLog(@"---------->");
NSLog(@"segment is: %@", segment);
Expand Down
Loading

0 comments on commit 3f5c567

Please sign in to comment.