Skip to content

Commit

Permalink
Merge CDLCSegment32 and CDLCSegment64 into CDLCSegment
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Mar 12, 2014
1 parent dbc8e49 commit d19f77e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 301 deletions.
94 changes: 52 additions & 42 deletions Source/CDLCSegment.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

@implementation CDLCSegment
{
struct segment_command_64 _segmentCommand; // 64-bit, also holding 32-bit

NSString *_name;
NSArray *_sections;

Expand All @@ -31,67 +33,76 @@ @implementation CDLCSegment
- (id)initWithDataCursor:(CDMachOFileDataCursor *)cursor;
{
if ((self = [super initWithDataCursor:cursor])) {
_name = nil;
_sections = nil;
_decryptedData = nil;
_segmentCommand.cmd = [cursor readInt32];
_segmentCommand.cmdsize = [cursor readInt32];

_name = [cursor readStringOfLength:16 encoding:NSASCIIStringEncoding];
memcpy(_segmentCommand.segname, [_name UTF8String], sizeof(_segmentCommand.segname));
_segmentCommand.vmaddr = [cursor readPtr];
_segmentCommand.vmsize = [cursor readPtr];
_segmentCommand.fileoff = [cursor readPtr];
_segmentCommand.filesize = [cursor readPtr];
_segmentCommand.maxprot = [cursor readInt32];
_segmentCommand.initprot = [cursor readInt32];
_segmentCommand.nsects = [cursor readInt32];
_segmentCommand.flags = [cursor readInt32];

NSMutableArray *sections = [[NSMutableArray alloc] init];
for (NSUInteger index = 0; index < _segmentCommand.nsects; index++) {
CDSection *section = [[CDSection alloc] initWithDataCursor:cursor segment:self];
[sections addObject:section];
}
_sections = [sections copy];
}

return self;
}

#pragma mark - Debugging

- (NSString *)description;
- (NSString *)extraDescription;
{
NSString *extra = [self extraDescription];

if (extra == nil) {
return [NSString stringWithFormat:@"<%@:%p> name: %@",
NSStringFromClass([self class]), self,
self.name];
}

return [NSString stringWithFormat:@"<%@:%p> name: %@, %@",
NSStringFromClass([self class]), self,
self.name, extra];
int padding = (int)self.machOFile.ptrSize * 2;
return [NSString stringWithFormat:@"vmaddr: 0x%0*llx - 0x%0*llx [0x%0*llx], offset: %lld, flags: 0x%x (%@), nsects: %d, sections: %@",
padding, _segmentCommand.vmaddr, padding, _segmentCommand.vmaddr + _segmentCommand.vmsize - 1, padding, _segmentCommand.vmsize,
_segmentCommand.fileoff, self.flags, [self flagDescription], _segmentCommand.nsects, self.sections.count > 0 ? self.sections : @"N/A"];
}

- (NSString *)extraDescription;
#pragma mark -

- (uint32_t)cmd;
{
// Implement in subclasses
return nil;
return _segmentCommand.cmd;
}

#pragma mark -
- (uint32_t)cmdsize;
{
return _segmentCommand.cmdsize;
}

- (NSUInteger)vmaddr;
{
// Implement in subclasses.
return 0;
return _segmentCommand.vmaddr;
}

- (NSUInteger)fileoff;
{
// Implement in subclasses.
return 0;
return _segmentCommand.fileoff;
}

- (NSUInteger)filesize;
{
// Implement in subclasses.
return 0;
return _segmentCommand.filesize;
}

- (vm_prot_t)initprot;
{
// Implement in subclsses.
return 0;
return _segmentCommand.initprot;
}

- (uint32_t)flags;
{
// Implement in subclsses.
return 0;
return _segmentCommand.flags;
}

- (BOOL)isProtected;
Expand Down Expand Up @@ -143,15 +154,14 @@ - (NSString *)flagDescription;
if (flags & SG_PROTECTED_VERSION_1) [setFlags addObject:@"PROTECTED_VERSION_1"];

if ([setFlags count] == 0)
return @"(none)";
return @"none";

return [setFlags componentsJoinedByString:@" "];
}

- (BOOL)containsAddress:(NSUInteger)address;
{
// Implement in subclasses
return NO;
return (address >= _segmentCommand.vmaddr) && (address < _segmentCommand.vmaddr + _segmentCommand.vmsize);
}

- (CDSection *)sectionContainingAddress:(NSUInteger)address;
Expand Down Expand Up @@ -188,21 +198,21 @@ - (void)appendToString:(NSMutableString *)resultString verbose:(BOOL)isVerbose;
{
[super appendToString:resultString verbose:isVerbose];
#if 0
[resultString appendFormat:@" segname %@\n", self.name];
[resultString appendFormat:@" vmaddr 0x%08x\n", segmentCommand.vmaddr];
[resultString appendFormat:@" vmsize 0x%08x\n", segmentCommand.vmsize];
[resultString appendFormat:@" fileoff %d\n", segmentCommand.fileoff];
[resultString appendFormat:@" filesize %d\n", segmentCommand.filesize];
[resultString appendFormat:@" maxprot 0x%08x\n", segmentCommand.maxprot];
[resultString appendFormat:@" initprot 0x%08x\n", segmentCommand.initprot];
[resultString appendFormat:@" nsects %d\n", segmentCommand.nsects];
int padding = (int)self.machOFile.ptrSize * 2;
[resultString appendFormat:@" segname %@\n", self.name];
[resultString appendFormat:@" vmaddr 0x%0*llx\n", padding, _segmentCommand.vmaddr];
[resultString appendFormat:@" vmsize 0x%0*llx\n", padding, _segmentCommand.vmsize];
[resultString appendFormat:@" fileoff %lld\n", _segmentCommand.fileoff];
[resultString appendFormat:@" filesize %lld\n", _segmentCommand.filesize];
[resultString appendFormat:@" maxprot 0x%08x\n", _segmentCommand.maxprot];
[resultString appendFormat:@" initprot 0x%08x\n", _segmentCommand.initprot];
[resultString appendFormat:@" nsects %d\n", _segmentCommand.nsects];

if (isVerbose)
[resultString appendFormat:@" flags %@\n", [self flagDescription]];
else
[resultString appendFormat:@" flags 0x%x\n", segmentCommand.flags];
[resultString appendFormat:@" flags 0x%x\n", _segmentCommand.flags];
#endif
// Implement in subclasses
}

- (void)writeSectionData;
Expand Down
10 changes: 0 additions & 10 deletions Source/CDLCSegment32.h

This file was deleted.

121 changes: 0 additions & 121 deletions Source/CDLCSegment32.m

This file was deleted.

10 changes: 0 additions & 10 deletions Source/CDLCSegment64.h

This file was deleted.

Loading

0 comments on commit d19f77e

Please sign in to comment.