-
Notifications
You must be signed in to change notification settings - Fork 1
/
BIFFData.m
222 lines (210 loc) · 6.13 KB
/
BIFFData.m
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright © 2010-2018, Brian "Moses" Hall
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#import "ACMRenderer.h"
#import "BIFFData.h"
#import <zlib.h>
@implementation BIFFFile
@synthesize loc = _loc;
@synthesize off = _off;
@synthesize len = _len;
-(id)initWithLocator:(uint32_t)loc offset:(uint32_t)off length:(uint32_t)len
{
self = [super init];
_loc = loc;
_off = off;
_len = len;
return self;
}
@end
@interface BIFFData (Private)
-(void)setBIFFData:(NSData*)data;
-(void)setBIFData:(NSData*)data;
-(void)setBIFCData:(NSData*)data;
@end
@implementation BIFFData
@synthesize strings = _strings;
@synthesize loaded = _loaded;
-(id)init
{
self = [super init];
_loaded = 0.0;
_strings = [[NSMutableArray alloc] init];
return self;
}
-(void)dealloc
{
if (_strings) [_strings release];
if (_data) [_data release];
[super dealloc];
}
-(void)setData:(NSData*)data/* path:(NSString*)path*/
{
_loaded = 0.0;
if (data)
{
char* buff = calloc(1, 12);
_data = data;
[data retain];
_bytes = [data bytes];
memcpy(buff, _bytes, 4);
//NSLog(@"Signature: '%s'", buff);
if (0 == strncmp(buff, "BIFF", 4)) [self setBIFFData:data];
else if (0 == strncmp(buff, "BIF ", 4)) [self setBIFData:data];
else if (0 == strncmp(buff, "BIFC", 4)) [self setBIFCData:data];
free(buff);
}
_loaded = 1.0;
}
-(void)setBIFFData:(NSData*)data
{
_data = data;
[data retain];
_bytes = [data bytes];
uint32_t nf = EndianU32_LtoN(*(uint32_t*)(_bytes + 0x0008));
_offset = EndianU32_LtoN(*(uint32_t*)(_bytes + 0x0010));
const unsigned char* p = _bytes + _offset;
for (uint32_t i = 0; i < nf; i++)
{
BIFFFile* info = nil;
uint32_t loc = EndianU32_LtoN(*(uint32_t*)p);
p += 4;
uint32_t off = EndianU32_LtoN(*(uint32_t*)p);
p += 4;
uint32_t len = EndianU32_LtoN(*(uint32_t*)p);
p += 4;
uint16_t type = EndianU16_LtoN(*(uint16_t*)p);
p += 4;
if (type == 4)
{
// Check for a RIFF header
if (*(_bytes + off) == 'R' &&
*(_bytes + off + 1) == 'I' &&
*(_bytes + off + 2) == 'F' &&
*(_bytes + off + 3) == 'F')
{
//NSLog(@"RIFF file detected, skipping");
}
else
{
info = [[BIFFFile alloc] initWithLocator:loc
offset:off
length:len];
@synchronized(self)
{
[_strings addObject:info];
_loaded = (double)i / (double)nf;
}
[info release];
}
}
}
}
/*
0x0000 4 (char array) Signature ('BIF ')
0x0004 4 (char array) Version ('V1.0')
0x0008 4 (dword) Length of filename
0x000c (ASCIIZ char array) Filename (length specified by previous field)
sizeof(filename)+0x0010 4 (dword) Uncompressed data length
sizeof(filename)+0x0014 4 (dword) Compressed data length
sizeof(filename)+0x0018 Variable (raw data) Compressed data
*/
-(void)setBIFData:(NSData*)data
{
_bytes = [data bytes];
uint32_t fnlen = EndianU32_LtoN(*(uint32_t*)(_bytes + 0x0008));
uint32_t uncmplen = EndianU32_LtoN(*(uint32_t*)(_bytes + fnlen + 0x0010));
uint32_t cmplen = EndianU32_LtoN(*(uint32_t*)(_bytes + fnlen + 0x0014));
//NSLog(@"Filename length: %d (%s); uncompressed: %d; compressed: %d", fnlen, _bytes + 0x000C, uncmplen, cmplen);
unsigned char* decomp = malloc(uncmplen);
const unsigned char* p = _bytes + fnlen + 0x0014;
uint32_t bytes = 0; // Amount decompressed
uLongf zuncmplen = uncmplen;
if (uncmplen > 0 && cmplen > 0)
{
int status = uncompress(decomp, &zuncmplen, p, cmplen);
if (status != Z_OK)
{
NSLog(@"Status: %d", status);
goto Done;
}
}
//NSLog(@"Bytes read: %d of %d", bytes, sz);
//hexdump(decomp, bytes);
NSData* newData = [[NSData alloc] initWithBytes:decomp length:bytes];
free(decomp);
decomp = NULL;
[self setData:newData];
[newData release];
Done:
if (decomp) free(decomp);
}
/*Header
Offset Size (data type) Description
0x0000 4 (char array) Signature ('BIFC')
0x0004 4 (char array) Version ('V1.0')
0x0008 4 (dword) Uncompressed BIF size
Compressed Blocks
Offset Size (data type) Description
0x0000 4 (dword) Decompressed size
0x0004 4 (dword) Compressed size
0x0008 varies (bytes) Compressed data*/
-(void)setBIFCData:(NSData*)data
{
_bytes = [data bytes];
uint32_t sz = EndianU32_LtoN(*(uint32_t*)(_bytes + 0x0008));
//NSLog(@"Size: %d", sz);
unsigned char* decomp = malloc(sz);
const unsigned char* p = _bytes + 12;
uint32_t bytes = 0; // Amount decompressed
while (bytes < sz)
{
// read block header
uint32_t uncmplen = EndianU32_LtoN(*(uint32_t*)p);
uLongf zuncmplen = uncmplen;
p += 4;
uint32_t cmplen = EndianU32_LtoN(*(uint32_t*)p);
p += 4;
//NSLog(@"Compressed: %d, uncompressed: %d", cmplen, uncmplen);
if (uncmplen > 0 && cmplen > 0)
{
int status = uncompress(decomp + bytes, &zuncmplen, p, cmplen);
if (status != Z_OK)
{
NSLog(@"Status: %d", status);
goto Done;
}
p += cmplen;
bytes += uncmplen;
}
//NSLog(@"Bytes read: %d of %d", bytes, sz);
}
//hexdump(decomp, bytes);
NSData* newData = [[NSData alloc] initWithBytes:decomp length:bytes];
free(decomp);
decomp = NULL;
[self setData:newData];
[newData release];
Done:
if (decomp) free(decomp);
}
-(NSData*)dataAtIndex:(uint32_t)i
{
BIFFFile* str = [_strings objectAtIndex:i];
NSData* val = [[NSData alloc] initWithBytes:_bytes + str.off
length:str.len];
return [val autorelease];
}
@end