Skip to content

Commit

Permalink
Remove offset and size parameters from CDFile initializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nygard committed Sep 18, 2012
1 parent 1e19b08 commit 337b906
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 53 deletions.
3 changes: 1 addition & 2 deletions Source/CDClassDump.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ - (CDMachOFile *)machOFileWithName:(NSString *)name;

CDMachOFile *machOFile = _machOFilesByName[adjustedName];
if (machOFile == nil) {
NSData *data = [[NSData alloc] initWithContentsOfMappedFile:adjustedName];
CDFile *file = [CDFile fileWithData:data filename:adjustedName searchPathState:self.searchPathState];
CDFile *file = [CDFile fileWithContentsOfFile:adjustedName searchPathState:self.searchPathState];

if (file == nil || [self loadFile:file] == NO)
NSLog(@"Warning: Failed to load: %@", adjustedName);
Expand Down
1 change: 0 additions & 1 deletion Source/CDDataCursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@interface CDDataCursor : NSObject

- (id)initWithData:(NSData *)data;
- (id)initWithData:(NSData *)data offset:(NSUInteger)offset;

@property (readonly) NSData *data;
- (const void *)bytes;
Expand Down
7 changes: 1 addition & 6 deletions Source/CDDataCursor.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ @implementation CDDataCursor
}

- (id)initWithData:(NSData *)data;
{
return [self initWithData:data offset:0];
}

- (id)initWithData:(NSData *)data offset:(NSUInteger)offset;
{
if ((self = [super init])) {
_data = data;
_offset = offset;
_offset = 0;
}

return self;
Expand Down
3 changes: 2 additions & 1 deletion Source/CDFatArch.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ - (NSString *)archName;
- (CDMachOFile *)machOFile;
{
if (_machOFile == nil) {
_machOFile = [CDFile fileWithData:self.fatFile.data archOffset:_fatArch.offset archSize:_fatArch.size filename:self.fatFile.filename searchPathState:self.fatFile.searchPathState];
NSData *data = [NSData dataWithBytesNoCopy:((uint8_t *)[self.fatFile.data bytes] + self.offset) length:self.size freeWhenDone:NO];
_machOFile = [[CDMachOFile alloc] initWithData:data filename:self.fatFile.filename searchPathState:self.fatFile.searchPathState];
}

return _machOFile;
Expand Down
6 changes: 3 additions & 3 deletions Source/CDFatFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ @implementation CDFatFile
NSArray *_arches;
}

- (id)initWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInteger)size filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
- (id)initWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
if ((self = [super initWithData:data archOffset:offset archSize:size filename:filename searchPathState:searchPathState])) {
CDDataCursor *cursor = [[CDDataCursor alloc] initWithData:data offset:self.archOffset];
if ((self = [super initWithData:data filename:filename searchPathState:searchPathState])) {
CDDataCursor *cursor = [[CDDataCursor alloc] initWithData:data];

struct fat_header header;
header.magic = [cursor readBigInt32];
Expand Down
8 changes: 4 additions & 4 deletions Source/CDFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ extern BOOL CDArchUses64BitABI(CDArch arch);

@interface CDFile : NSObject

// Returns CDFatFile or CDMachOFile.
+ (id)fileWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
+ (id)fileWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInteger)size filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
- (id)initWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInteger)size filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
- (id)initWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;

// Returns CDFatFile or CDMachOFile
+ (id)fileWithContentsOfFile:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;

@property (readonly) NSString *filename;
@property (readonly) NSData *data;
Expand Down
37 changes: 17 additions & 20 deletions Source/CDFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ BOOL CDArchUses64BitABI(CDArch arch)

#pragma mark -

@interface CDFile ()
@end

#pragma mark -

@implementation CDFile
{
NSString *_filename;
Expand All @@ -83,24 +88,16 @@ @implementation CDFile
CDSearchPathState *_searchPathState;
}

+ (id)fileWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
// Returns CDFatFile or CDMachOFile
+ (id)fileWithContentsOfFile:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
return [self fileWithData:data archOffset:0 archSize:[data length] filename:filename searchPathState:searchPathState];
}

+ (id)fileWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInteger)size filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
CDFatFile *fatFile = nil;

if (offset == 0)
fatFile = [[CDFatFile alloc] initWithData:data archOffset:offset archSize:size filename:filename searchPathState:searchPathState];

if (fatFile == nil) {
CDMachOFile *machOFile = [[CDMachOFile alloc] initWithData:data archOffset:offset archSize:size filename:filename searchPathState:searchPathState];
return machOFile;
}

return fatFile;
NSData *data = [NSData dataWithContentsOfMappedFile:filename];
CDFatFile *fatFile = [[CDFatFile alloc] initWithData:data filename:filename searchPathState:searchPathState];
if (fatFile != nil)
return fatFile;

CDMachOFile *machOFile = [[CDMachOFile alloc] initWithData:data filename:filename searchPathState:searchPathState];
return machOFile;
}

- (id)init;
Expand All @@ -109,7 +106,7 @@ - (id)init;
return nil;
}

- (id)initWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInteger)size filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
- (id)initWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
if ((self = [super init])) {
// Otherwise reading the magic number fails.
Expand All @@ -119,8 +116,8 @@ - (id)initWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInt

_filename = filename;
_data = data;
_archOffset = offset;
_archSize = size;
_archOffset = 0;
_archSize = [_data length];
_searchPathState = searchPathState;
}

Expand Down
16 changes: 8 additions & 8 deletions Source/CDMachOFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ @implementation CDMachOFile
} _flags;
}

- (id)initWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInteger)size filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
- (id)initWithData:(NSData *)data filename:(NSString *)filename searchPathState:(CDSearchPathState *)searchPathState;
{
if ((self = [super initWithData:data archOffset:offset archSize:size filename:filename searchPathState:searchPathState])) {
if ((self = [super initWithData:data filename:filename searchPathState:searchPathState])) {
_byteOrder = CDByteOrder_LittleEndian;
_loadCommands = nil;
_dylibLoadCommands = nil;
Expand All @@ -83,7 +83,7 @@ - (id)initWithData:(NSData *)data archOffset:(NSUInteger)offset archSize:(NSUInt
_dyldEnvironment = nil;
_reExportedDylibs = nil;

CDDataCursor *cursor = [[CDDataCursor alloc] initWithData:data offset:self.archOffset];
CDDataCursor *cursor = [[CDDataCursor alloc] initWithData:data];
_header.magic = [cursor readBigInt32];
if (_header.magic == MH_MAGIC || _header.magic == MH_MAGIC_64) {
_byteOrder = CDByteOrder_BigEndian;
Expand Down Expand Up @@ -155,12 +155,12 @@ - (void)_readLoadCommands:(CDMachOFileDataCursor *)cursor count:(uint32_t)count;
}
//NSLog(@"loadCommand: %@", loadCommand);
}
_loadCommands = [loadCommands copy];
_loadCommands = [loadCommands copy];
_dylibLoadCommands = [dylibLoadCommands copy];
_segments = [segments copy];
_runPaths = [runPaths copy];
_dyldEnvironment = [dyldEnvironment copy];
_reExportedDylibs = [reExportedDylibs copy];
_segments = [segments copy];
_runPaths = [runPaths copy];
_dyldEnvironment = [dyldEnvironment copy];
_reExportedDylibs = [reExportedDylibs copy];

for (CDLoadCommand *loadCommand in _loadCommands) {
[loadCommand machOFileDidReadLoadCommands:self];
Expand Down
9 changes: 4 additions & 5 deletions class-dump.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ int main(int argc, char *argv[])
} else {
CDSearchPathState *searchPathState = [[CDSearchPathState alloc] init];
searchPathState.executablePath = executablePath;
NSData *data = [[NSData alloc] initWithContentsOfMappedFile:executablePath];
id macho = [CDFile fileWithData:data filename:executablePath searchPathState:searchPathState];
id macho = [CDFile fileWithContentsOfFile:executablePath searchPathState:searchPathState];
if (macho == nil) {
printf("none\n");
} else {
Expand All @@ -247,7 +246,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "class-dump: Input file (%s) doesn't contain an executable.\n", [arg fileSystemRepresentation]);
exit(1);
}

#if 0
NSData *data = [[NSData alloc] initWithContentsOfMappedFile:executablePath];
if (data == nil) {
NSFileManager *defaultManager = [NSFileManager defaultManager];
Expand All @@ -260,9 +259,9 @@ int main(int argc, char *argv[])

exit(1);
}

#endif
classDump.searchPathState.executablePath = [executablePath stringByDeletingLastPathComponent];
CDFile *file = [CDFile fileWithData:data filename:executablePath searchPathState:classDump.searchPathState];
CDFile *file = [CDFile fileWithContentsOfFile:executablePath searchPathState:classDump.searchPathState];
if (file == nil) {
fprintf(stderr, "class-dump: Input file (%s) is neither a Mach-O file nor a fat archive.\n", [executablePath UTF8String]);
exit(1);
Expand Down
5 changes: 2 additions & 3 deletions deprotect.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <getopt.h>
#include <stdlib.h>
#include <sysexits.h>
#include <mach-o/arch.h>

#import <Foundation/Foundation.h>

Expand Down Expand Up @@ -126,9 +127,7 @@ int main(int argc, char *argv[])
NSString *inputFile = [NSString stringWithFileSystemRepresentation:argv[0]];
NSString *outputFile = [NSString stringWithFileSystemRepresentation:argv[1]];

NSData *inputData = [[NSData alloc] initWithContentsOfMappedFile:inputFile];

CDFile *file = [CDFile fileWithData:inputData filename:inputFile searchPathState:nil];
CDFile *file = [CDFile fileWithContentsOfFile:inputFile searchPathState:nil];
if (file == nil) {
fprintf(stderr, "Error: input file is neither a Mach-O file nor a fat archive.\n");
exit(EX_DATAERR);
Expand Down

0 comments on commit 337b906

Please sign in to comment.