Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
- Added more patterns for ignored files.
Browse files Browse the repository at this point in the history
- Fixed the remainder of the issues in MHKKit caused by removing MHKKit's copy of PHSErrorMacros.
  • Loading branch information
jfroy committed Dec 26, 2008
1 parent fb29f92 commit c185bde
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 67 deletions.
4 changes: 4 additions & 0 deletions .bzrignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
build
.DS_Store
*.mode?v*
*.pbxuser

2 changes: 1 addition & 1 deletion mhk/MHKADPCMDecompressor.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ - (id)initWithChannelCount:(UInt32)channels frameCount:(SInt64)frames samplingRa

// ADPCM only works for 1 or 2 channels
if (channels != 1 && channels != 2)
ReturnFromInitWithError(MHKErrorDomain, errInvalidChannelCount, nil, errorPtr)
ReturnFromInitWithError(MHKErrorDomain, errInvalidChannelCount, nil, errorPtr);

channel_count = channels;
data_source = [fh retain];
Expand Down
26 changes: 13 additions & 13 deletions mhk/MHKArchive.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#import "PHSErrorMacros.h"


struct __descriptor_binary_tree {
struct descriptor_binary_tree {
uint16_t resource_id;
NSDictionary *descriptor;
NSDictionary* descriptor;
};

static int __descriptor_binary_tree_compare(const void* v1, const void* v2) {
struct __descriptor_binary_tree* descriptor_1 = (struct __descriptor_binary_tree*)v1;
struct __descriptor_binary_tree* descriptor_2 = (struct __descriptor_binary_tree*)v2;
struct descriptor_binary_tree* descriptor_1 = (struct descriptor_binary_tree*)v1;
struct descriptor_binary_tree* descriptor_2 = (struct descriptor_binary_tree*)v2;

if (descriptor_1->resource_id < descriptor_2->resource_id)
return -1;
Expand Down Expand Up @@ -208,7 +208,7 @@ - (BOOL)load_mhk_type:(uint32_t)type_index {
}

// allocate the descriptor binary tree entries
struct __descriptor_binary_tree* descriptor_tree = calloc(rsrc_table_header.count, sizeof(struct __descriptor_binary_tree));
struct descriptor_binary_tree* descriptor_tree = calloc(rsrc_table_header.count, sizeof(struct descriptor_binary_tree));

// iterate over the resources
uint16_t resource_index = 0;
Expand Down Expand Up @@ -277,10 +277,10 @@ - (BOOL)load_mhk_type:(uint32_t)type_index {

// in order to be able to perform binary searching, we need to sort the descriptor tree entries
// i'm guessing they will most likely already be sorted, so mergesort should be the fastest (and we've got plenty of ram now)
mergesort(descriptor_tree, rsrc_table_header.count, sizeof(struct __descriptor_binary_tree), &__descriptor_binary_tree_compare);
mergesort(descriptor_tree, rsrc_table_header.count, sizeof(struct descriptor_binary_tree), &__descriptor_binary_tree_compare);

// store the sorted array in a NSData, then associate to type with the trees dictionary
NSData* descriptor_tree_data = [[NSData alloc] initWithBytesNoCopy:descriptor_tree length:rsrc_table_header.count * sizeof(struct __descriptor_binary_tree) freeWhenDone:YES];
NSData* descriptor_tree_data = [[NSData alloc] initWithBytesNoCopy:descriptor_tree length:rsrc_table_header.count * sizeof(struct descriptor_binary_tree) freeWhenDone:YES];
[file_descriptor_trees setObject:descriptor_tree_data forKey:type_key];
[descriptor_tree_data release];

Expand Down Expand Up @@ -481,18 +481,18 @@ - (id)initWithURL:(NSURL*)url error:(NSError**)errorPtr {
HFSUniStr255 dataForkName;
err = FSGetDataForkName(&dataForkName);
if (err)
ReturnFromInitWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnFromInitWithError(NSOSStatusErrorDomain, err, nil, errorPtr);

// get an FSRef for the Mohak archive
FSRef archiveRef;
if (!CFURLGetFSRef((CFURLRef)mhk_url, &archiveRef))
ReturnFromInitWithError(NSOSStatusErrorDomain, fnfErr, nil, errorPtr)
ReturnFromInitWithError(NSOSStatusErrorDomain, fnfErr, nil, errorPtr);

// open the data fork in read-only mode
forkRef = 0;
err = FSOpenFork(&archiveRef, dataForkName.length, dataForkName.unicode, fsRdPerm, &forkRef);
if (err)
ReturnFromInitWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnFromInitWithError(NSOSStatusErrorDomain, err, nil, errorPtr);

// get the file size
SInt64 fork_size = 0;
Expand All @@ -502,12 +502,12 @@ - (id)initWithURL:(NSURL*)url error:(NSError**)errorPtr {

// we only support 32 bits for archive sizes
if (fork_size > ULONG_MAX)
ReturnFromInitWithError(MHKErrorDomain, errFileTooLarge, nil, errorPtr)
ReturnFromInitWithError(MHKErrorDomain, errFileTooLarge, nil, errorPtr);
archive_size = (uint32_t)fork_size;

// process the archive
if (![self load_mhk])
ReturnFromInitWithError(MHKErrorDomain, errBadArchive, nil, errorPtr)
ReturnFromInitWithError(MHKErrorDomain, errBadArchive, nil, errorPtr);

// allocate the sound descriptor cache and its rw lock
pthread_rwlock_init(&__cached_sound_descriptors_rwlock, NULL);
Expand Down Expand Up @@ -553,7 +553,7 @@ - (NSDictionary*)resourceDescriptorWithResourceType:(NSString*)type ID:(uint16_t
return nil;

uint16_t n = [[file_descriptor_arrays objectForKey:type] count];
const struct __descriptor_binary_tree* binary_tree = [binary_tree_data bytes];
const struct descriptor_binary_tree* binary_tree = [binary_tree_data bytes];

if (n == 0)
return nil;
Expand Down
14 changes: 7 additions & 7 deletions mhk/MHKArchiveBitmapAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ - (NSDictionary*)bitmapDescriptorWithID:(uint16_t)bitmapID error:(NSError**)erro
// get a resource descriptor
NSDictionary* descriptor = [self resourceDescriptorWithResourceType:@"tBMP" ID:bitmapID];
if (!descriptor)
ReturnNULLWithError(MHKErrorDomain, errUnknownID, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errUnknownID, nil, errorPtr);

// seek to the tBMP resource
SInt64 resource_offset = [[descriptor objectForKey:@"Offset"] longLongValue];
Expand All @@ -31,7 +31,7 @@ - (NSDictionary*)bitmapDescriptorWithID:(uint16_t)bitmapID error:(NSError**)erro
ByteCount bytes_read = 0;
OSStatus err = FSReadFork(forkRef, fsFromStart, resource_offset, sizeof(MHK_BITMAP_header), &bitmap_header, &bytes_read);
if (err)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr);
MHK_BITMAP_header_fton(&bitmap_header);

// make the bitmap descriptor
Expand All @@ -47,7 +47,7 @@ - (BOOL)loadBitmapWithID:(uint16_t)bitmapID buffer:(void*)pixels format:(MHK_BIT
// get a resource descriptor
NSDictionary *descriptor = [self resourceDescriptorWithResourceType:@"tBMP" ID:bitmapID];
if (!descriptor)
ReturnValueWithError(NO, MHKErrorDomain, errUnknownID, nil, errorPtr)
ReturnValueWithError(NO, MHKErrorDomain, errUnknownID, nil, errorPtr);

// seek to the tBMP resource
SInt64 resource_offset = [[descriptor objectForKey:@"Offset"] longLongValue];
Expand All @@ -57,13 +57,13 @@ - (BOOL)loadBitmapWithID:(uint16_t)bitmapID buffer:(void*)pixels format:(MHK_BIT
ByteCount bytes_read = 0;
OSStatus err = FSReadFork(forkRef, fsFromStart, resource_offset, sizeof(MHK_BITMAP_header), &bitmap_header, &bytes_read);
if (err)
ReturnValueWithError(NO, NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnValueWithError(NO, NSOSStatusErrorDomain, err, nil, errorPtr);
MHK_BITMAP_header_fton(&bitmap_header);

if (bitmap_header.truecolor_flag == 4) {
err = read_raw_bgr_pixels(forkRef, resource_offset + bytes_read, &bitmap_header, pixels, format);
if (err)
ReturnValueWithError(NO, NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnValueWithError(NO, NSOSStatusErrorDomain, err, nil, errorPtr);
return YES;
}

Expand All @@ -76,9 +76,9 @@ - (BOOL)loadBitmapWithID:(uint16_t)bitmapID buffer:(void*)pixels format:(MHK_BIT
else if (bitmap_header.compression_flag == MHK_BITMAP_COMPRESSED)
err = read_compressed_indexed_pixels(forkRef, resource_offset, &bitmap_header, pixels, format);
else
ReturnValueWithError(NO, MHKErrorDomain, errInvalidBitmapCompression, nil, errorPtr)
ReturnValueWithError(NO, MHKErrorDomain, errInvalidBitmapCompression, nil, errorPtr);
if (err)
ReturnValueWithError(NO, NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnValueWithError(NO, NSOSStatusErrorDomain, err, nil, errorPtr);

// we're done
return YES;
Expand Down
4 changes: 2 additions & 2 deletions mhk/MHKArchiveQuickTimeAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ - (Movie)movieWithID:(uint16_t)movieID error:(NSError **)errorPtr {
// get the movie resource descriptor
NSDictionary *descriptor = [self resourceDescriptorWithResourceType:@"tMOV" ID:movieID];
if (!descriptor)
ReturnNULLWithError(MHKErrorDomain, errUnknownID, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errUnknownID, nil, errorPtr);

// store the movie offset in a variable
SInt64 qt_offset = [[descriptor objectForKey:@"Offset"] longLongValue];
Expand Down Expand Up @@ -48,7 +48,7 @@ - (Movie)movieWithID:(uint16_t)movieID error:(NSError **)errorPtr {
Movie aMovie = NULL;
err = NewMovieFromProperties(sizeof(newMovieProperties) / sizeof(newMovieProperties[0]), newMovieProperties, 0, NULL, &aMovie);
if (err != noErr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr);
return aMovie;
}

Expand Down
42 changes: 21 additions & 21 deletions mhk/MHKArchiveWAVAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


@interface MHKFileHandle (Private)
- (id)_initWithArchive:(MHKArchive *)archive fork:(SInt16)fork soundDescriptor:(NSDictionary *)sdesc;
- (id)_initWithArchive:(MHKArchive*)archive fork:(SInt16)fork soundDescriptor:(NSDictionary*)sdesc;
@end


Expand All @@ -27,21 +27,21 @@ - (NSDictionary*)soundDescriptorWithID:(uint16_t)soundID error:(NSError**)errorP
OSStatus err = noErr;
ByteCount bytes_read = 0;
SInt64 file_offset;
NSNumber *soundIDNumber = [NSNumber numberWithUnsignedShort:soundID];
NSNumber* soundIDNumber = [NSNumber numberWithUnsignedShort:soundID];

// check for a cached value
pthread_rwlock_rdlock(&__cached_sound_descriptors_rwlock);
NSDictionary *soundDescriptor = [__cached_sound_descriptors objectForKey:soundIDNumber];
NSDictionary* soundDescriptor = [__cached_sound_descriptors objectForKey:soundIDNumber];
if (soundDescriptor) {
pthread_rwlock_unlock(&__cached_sound_descriptors_rwlock);
return soundDescriptor;
}
pthread_rwlock_unlock(&__cached_sound_descriptors_rwlock);

// get the wave resource descriptor
NSDictionary *descriptor = [self resourceDescriptorWithResourceType:@"tWAV" ID:soundID];
NSDictionary* descriptor = [self resourceDescriptorWithResourceType:@"tWAV" ID:soundID];
if (!descriptor)
ReturnNULLWithError(MHKErrorDomain, errUnknownID, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errUnknownID, nil, errorPtr);

// seek to the tWAV resource then seek to the data chunk
SInt64 resource_offset = [[descriptor objectForKey:@"Offset"] longLongValue];
Expand All @@ -57,13 +57,13 @@ - (NSDictionary*)soundDescriptorWithID:(uint16_t)soundID error:(NSError**)errorP
// we need to have a standard MHWK chunk first
err = FSReadFork(forkRef, fsFromStart, file_offset, sizeof(MHK_chunk_header), &chunk_header, &bytes_read);
if (err)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr);
file_offset += bytes_read;

// handle byte order and check header
MHK_chunk_header_fton(&chunk_header);
if (*(uint32_t *)chunk_header.signature != MHK_MHWK_signature_integer)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr);

// since resource lengths are computed in the archive, we need to do some checking here
UInt32 resource_length = chunk_header.content_length + sizeof(MHK_chunk_header);
Expand All @@ -74,17 +74,18 @@ - (NSDictionary*)soundDescriptorWithID:(uint16_t)soundID error:(NSError**)errorP
#endif

// if the resource length is greater than the one from the archive, take the one from the archive since it was computed to be the biggest size possible w/o resource overlap
if (resource_length > resource_length_from_archive) resource_length = resource_length_from_archive;
if (resource_length > resource_length_from_archive)
resource_length = resource_length_from_archive;
}

// must have the WAVE signature next
uint32_t wave_signature = 0;
uint32_t wave_signature;
err = FSReadFork(forkRef, fsFromStart, file_offset, sizeof(uint32_t), &wave_signature, &bytes_read);
if (err)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr);
file_offset += bytes_read;
if (wave_signature != MHK_WAVE_signature_integer)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr);

// compute the resource limit
SInt64 resource_eof = resource_offset + chunk_header.content_length + sizeof(MHK_chunk_header);
Expand All @@ -94,7 +95,7 @@ - (NSDictionary*)soundDescriptorWithID:(uint16_t)soundID error:(NSError**)errorP
// read a chunk header structure
err = FSReadFork(forkRef, fsFromStart, file_offset, sizeof(MHK_chunk_header), &chunk_header, &bytes_read);
if (err)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr);
file_offset += bytes_read;
MHK_chunk_header_fton(&chunk_header);

Expand All @@ -108,13 +109,13 @@ - (NSDictionary*)soundDescriptorWithID:(uint16_t)soundID error:(NSError**)errorP

// did we score?
if (*(uint32_t *)chunk_header.signature != MHK_Data_signature_integer)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr);

// read the Data chunk content header
MHK_WAVE_Data_chunk_header data_header;
err = FSReadFork(forkRef, fsFromStart, file_offset, sizeof(MHK_WAVE_Data_chunk_header), &data_header, &bytes_read);
if (err)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr);
file_offset += bytes_read;
MHK_WAVE_Data_chunk_header_fton(&data_header);

Expand Down Expand Up @@ -142,7 +143,7 @@ - (NSDictionary*)soundDescriptorWithID:(uint16_t)soundID error:(NSError**)errorP
for (unsigned char packet_index = 0; packet_index < 3; packet_index++) {
err = FSReadFork(forkRef, fsFromStart, file_offset, sizeof(uint32_t), &mpeg_header, NULL);
if (err)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnNULLWithError(NSOSStatusErrorDomain, err, nil, errorPtr);

// WE OMIT TO UPDATE file_offset ON PURPOSE SO THAT IT STILL POINTS AT THE BEGINNING OF THE FIRST MPEG FRAME

Expand All @@ -151,19 +152,18 @@ - (NSDictionary*)soundDescriptorWithID:(uint16_t)soundID error:(NSError**)errorP

// first 11 bits have to be 1s (MPEG packet sync)
if ((mpeg_header & 0xFFE00000) != 0xFFE00000)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr);

// 2 bits - type must be 10 for MPEG v2
if ((mpeg_header & 0x00180000) != 0x00100000)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr);

// 2 bits - layer must be 10 for layer II
if ((mpeg_header & 0x00060000) != 0x00040000)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr)
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr);
}
} else {
ReturnNULLWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr)
return nil;
ReturnNILWithError(MHKErrorDomain, errDamagedResource, nil, errorPtr);
}

#if defined(DEBUG) && DEBUG > 2
Expand Down Expand Up @@ -235,7 +235,7 @@ - (MHKFileHandle *)openSoundWithID:(uint16_t)soundID error:(NSError **)errorPtr
else if (compression_type == MHK_WAVE_MP2)
decompressor_class = [MHKMP2Decompressor class];
else
ReturnNILWithError(MHKErrorDomain, errInvalidSoundDescriptor, nil, errorPtr)
ReturnNILWithError(MHKErrorDomain, errInvalidSoundDescriptor, nil, errorPtr);

// return a decompressor
return [[[decompressor_class alloc] initWithChannelCount:channels frameCount:frames samplingRate:sr fileHandle:fh error:errorPtr] autorelease];
Expand Down
12 changes: 7 additions & 5 deletions mhk/MHKFileHandle.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ - (id)init {

- (id)_initWithArchive:(MHKArchive*)archive fork:(SInt16)forkRef descriptor:(NSDictionary*)desc {
self = [super init];
if (!self) return nil;
if (!self)
return nil;

// weak reference, MHKArchive ensures it won't go away until all its files have dealloc-ed
__owner = archive;
Expand All @@ -35,7 +36,8 @@ - (id)_initWithArchive:(MHKArchive*)archive fork:(SInt16)forkRef descriptor:(NSD

- (id)_initWithArchive:(MHKArchive*)archive fork:(SInt16)forkRef soundDescriptor:(NSDictionary*)sdesc {
self = [super init];
if (!self) return nil;
if (!self)
return nil;

// weak reference, MHKArchive ensures it won't go away until all its files have dealloc-ed
__owner = archive;
Expand All @@ -56,7 +58,7 @@ - (void)dealloc {
- (ssize_t)readDataOfLength:(size_t)length inBuffer:(void*)buffer error:(NSError**)errorPtr {
// is the request valid?
if (__position == __length)
ReturnValueWithError(-1, NSOSStatusErrorDomain, eofErr, nil, errorPtr)
ReturnValueWithError(-1, NSOSStatusErrorDomain, eofErr, nil, errorPtr);

if (__length - __position < length)
length = __length - __position;
Expand All @@ -65,13 +67,13 @@ - (ssize_t)readDataOfLength:(size_t)length inBuffer:(void*)buffer error:(NSError
UInt32 bytes_read = 0;
OSStatus err = FSReadFork(__forkRef, fsFromStart | forceReadMask, __offset + __position, length, buffer, &bytes_read);
if (err && err != eofErr)
ReturnValueWithError(-1, NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnValueWithError(-1, NSOSStatusErrorDomain, err, nil, errorPtr);

// update the position
__position += bytes_read;

if (err)
ReturnValueWithError(bytes_read, NSOSStatusErrorDomain, err, nil, errorPtr)
ReturnValueWithError(bytes_read, NSOSStatusErrorDomain, err, nil, errorPtr);
return bytes_read;
}

Expand Down
Loading

0 comments on commit c185bde

Please sign in to comment.