forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDFatFile.m
More file actions
141 lines (108 loc) · 3.33 KB
/
CDFatFile.m
File metadata and controls
141 lines (108 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2006 Steve Nygard
#import "CDFatFile.h"
#include <mach-o/arch.h>
#include <mach-o/swap.h>
#import <Foundation/Foundation.h>
#import "CDFatArch.h"
#import "CDMachOFile.h"
// The fat_header and fat_arch structures are stored in big endian-byte order.
// TODO (2005-07-27): For Intel, we'll need to swap those structs.
@implementation CDFatFile
- (id)initWithFilename:(NSString *)aFilename;
{
BOOL shouldSwapBytes;
if ([super init] == nil)
return nil;
shouldSwapBytes = NO;
filename = [aFilename retain];
data = [[NSData alloc] initWithContentsOfMappedFile:filename];
if (data == nil) {
NSLog(@"Couldn't read file: %@", filename);
[self release];
return nil;
}
header = *(struct fat_header *)[data bytes];
if (header.magic == FAT_CIGAM) {
shouldSwapBytes = YES;
swap_fat_header(&header, CD_THIS_BYTE_ORDER);
}
if (header.magic != FAT_MAGIC) {
[self release];
return nil;
}
arches = [[NSMutableArray alloc] init];
[self _processFatArchesWithPointer:[data bytes] + sizeof(struct fat_header) swapBytes:shouldSwapBytes];
return self;
}
- (void)dealloc;
{
[filename release];
[data release];
[arches release];
[super dealloc];
}
- (void)_processFatArchesWithPointer:(const void *)ptr swapBytes:(BOOL)shouldSwapBytes;
{
unsigned int count, index;
count = [self fatCount];
for (index = 0; index < count; index++) {
CDFatArch *fatArch;
fatArch = [[CDFatArch alloc] initWithPointer:ptr swapBytes:shouldSwapBytes];
[arches addObject:fatArch];
[fatArch release];
ptr += sizeof(struct fat_arch);
}
}
- (NSString *)filename;
{
return filename;
}
- (unsigned int)fatCount;
{
return header.nfat_arch;
}
- (CDFatArch *)fatArchWithCPUType:(cpu_type_t)aCPUType;
{
if (aCPUType == CPU_TYPE_ANY) {
CDFatArch *fatArch;
fatArch = [self localArchitecture];
if (fatArch == nil && [arches count] > 0)
fatArch = [arches objectAtIndex:0];
return fatArch;
}
return [self _fatArchWithCPUType:aCPUType];
}
- (CDFatArch *)_fatArchWithCPUType:(cpu_type_t)aCPUType;
{
unsigned int count, index;
assert(aCPUType != CPU_TYPE_ANY);
count = [arches count];
for (index = 0; index < count; index++) {
CDFatArch *fatArch;
fatArch = [arches objectAtIndex:index];
if ([fatArch cpuType] == aCPUType)
return fatArch;
}
return nil;
}
- (CDFatArch *)localArchitecture;
{
const NXArchInfo *archInfo;
archInfo = NXGetLocalArchInfo();
if (archInfo == NULL) {
NSLog(@"Couldn't get local architecture");
return nil;
}
//NSLog(@"Local arch: %d, %s (%s)", archInfo->cputype, archInfo->description, archInfo->name);
return [self _fatArchWithCPUType:archInfo->cputype];
}
- (NSString *)description;
{
return @"fat file...";
#if 0
return [NSString stringWithFormat:@"magic: 0x%08x, cputype: %d, cpusubtype: %d, filetype: %d, ncmds: %d, sizeofcmds: %d, flags: 0x%x",
header.magic, header.cputype, header.cpusubtype, header.filetype, header.ncmds, header.sizeofcmds, header.flags];
#endif
}
@end