Skip to content

Commit

Permalink
Swap these structures in memory. They are alway stored in big-endian …
Browse files Browse the repository at this point in the history
…(ppc) format,

so the changes only affect intel.  Not tested on intel yet.
  • Loading branch information
nygard committed Jul 28, 2005
1 parent cc9f009 commit 2594d75
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
4 changes: 2 additions & 2 deletions CDFatArch.h
Expand Up @@ -7,10 +7,10 @@

@interface CDFatArch : NSObject
{
const struct fat_arch *arch;
struct fat_arch arch;
}

- (id)initWithPointer:(const void *)ptr;
- (id)initWithPointer:(const void *)ptr swapBytes:(BOOL)shouldSwapBytes;
- (void)dealloc;

- (cpu_type_t)cpuType;
Expand Down
20 changes: 12 additions & 8 deletions CDFatArch.m
Expand Up @@ -4,17 +4,21 @@

#import "CDFatArch.h"

#import <mach-o/swap.h>
#import <Foundation/Foundation.h>
#import "CDFatFile.h"
#import "CDMachOFile.h"

@implementation CDFatArch

- (id)initWithPointer:(const void *)ptr;
- (id)initWithPointer:(const void *)ptr swapBytes:(BOOL)shouldSwapBytes;
{
if ([super init] == nil)
return nil;

arch = ptr;
arch = *(struct fat_arch *)ptr;
if (shouldSwapBytes == YES)
swap_fat_arch(&arch, 1, CD_THIS_BYTE_ORDER);

return self;
}
Expand All @@ -26,33 +30,33 @@ - (void)dealloc;

- (cpu_type_t)cpuType;
{
return arch->cputype;
return arch.cputype;
}

- (cpu_subtype_t)cpuSubtype;
{
return arch->cpusubtype;
return arch.cpusubtype;
}

- (uint32_t)offset;
{
return arch->offset;
return arch.offset;
}

- (uint32_t)size;
{
return arch->size;
return arch.size;
}

- (uint32_t)align;
{
return arch->align;
return arch.align;
}

- (NSString *)description;
{
return [NSString stringWithFormat:@"cputype: %d, cpusubtype: %d, offset: 0x%x, size: 0x%x, align: %d",
arch->cputype, arch->cpusubtype, arch->offset, arch->size, arch->align];
arch.cputype, arch.cpusubtype, arch.offset, arch.size, arch.align];
}

@end
4 changes: 2 additions & 2 deletions CDFatFile.h
Expand Up @@ -20,14 +20,14 @@
{
NSString *filename;
NSData *data;
const struct fat_header *header;
struct fat_header header;
NSMutableArray *arches;
}

- (id)initWithFilename:(NSString *)aFilename;
- (void)dealloc;

- (void)_processFatArches;
- (void)_processFatArchesWithPointer:(const void *)ptr swapBytes:(BOOL)shouldSwapBytes;

- (NSString *)filename;

Expand Down
28 changes: 17 additions & 11 deletions CDFatFile.m
Expand Up @@ -5,6 +5,7 @@
#import "CDFatFile.h"

#include <mach-o/arch.h>
#include <mach-o/swap.h>
#import <Foundation/Foundation.h>
#import "CDFatArch.h"
#import "CDMachOFile.h"
Expand All @@ -16,9 +17,12 @@ @implementation CDFatFile

- (id)initWithFilename:(NSString *)aFilename;
{
BOOL shouldSwapBytes;

if ([super init] == nil)
return nil;

shouldSwapBytes = NO;
filename = [aFilename retain];

data = [[NSData alloc] initWithContentsOfMappedFile:filename];
Expand All @@ -28,14 +32,19 @@ - (id)initWithFilename:(NSString *)aFilename;
return nil;
}

header = [data bytes];
if (header->magic != CD_FAT_MAGIC) {
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 _processFatArches];
[self _processFatArchesWithPointer:[data bytes] + sizeof(struct fat_header) swapBytes:shouldSwapBytes];

return self;
}
Expand All @@ -49,21 +58,18 @@ - (void)dealloc;
[super dealloc];
}

- (void)_processFatArches;
- (void)_processFatArchesWithPointer:(const void *)ptr swapBytes:(BOOL)shouldSwapBytes;
{
unsigned int count, index;
const struct fat_arch *ptr;

ptr = (struct fat_arch *)(header + 1);

count = [self fatCount];
for (index = 0; index < count; index++) {
CDFatArch *fatArch;

fatArch = [[CDFatArch alloc] initWithPointer:ptr];
fatArch = [[CDFatArch alloc] initWithPointer:ptr swapBytes:shouldSwapBytes];
[arches addObject:fatArch];
[fatArch release];
ptr++;
ptr += sizeof(struct fat_arch);
}
}

Expand All @@ -74,7 +80,7 @@ - (NSString *)filename;

- (unsigned int)fatCount;
{
return header->nfat_arch;
return header.nfat_arch;
}

- (CDFatArch *)fatArchWithCPUType:(cpu_type_t)aCPUType;
Expand Down Expand Up @@ -129,7 +135,7 @@ - (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];
header.magic, header.cputype, header.cpusubtype, header.filetype, header.ncmds, header.sizeofcmds, header.flags];
#endif
}

Expand Down

0 comments on commit 2594d75

Please sign in to comment.