-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacresfork.cpp
337 lines (253 loc) · 8.12 KB
/
macresfork.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* macresview - A simple Mac resource fork dumper
*
* macresview is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// Partially based on ScummVM's Mac resource fork parser (GPLv2+)
#include <stdio.h>
#include "macresfork.h"
ResourceFork::ResourceFork() {
_file = 0;
}
ResourceFork::~ResourceFork() {
close();
}
bool ResourceFork::load(const char *filename) {
if (loadFromMacBinary(filename))
return true;
if (loadFromAppleDouble(filename))
return true;
if (loadFromRawFork(filename))
return true;
if (loadFromMacBaseFilename(filename))
return true;
return false;
}
bool ResourceFork::loadFromRawFork(std::string filename) {
_file = fopen(filename.c_str(), "rb");
if (!_file)
return false;
return loadInternal();
}
bool ResourceFork::loadFromMacBaseFilename(std::string filename) {
#ifdef __APPLE__
// On Mac OS X, try to access the resource fork directly
return loadFromRawFork(filename + "/..namedfork/rsrc");
#else
return false;
#endif
}
#define MBI_INFOHDR 128
#define MBI_ZERO1 0
#define MBI_NAMELEN 1
#define MBI_ZERO2 74
#define MBI_ZERO3 82
#define MBI_DFLEN 83
#define MBI_RFLEN 87
#define MAXNAMELEN 63
bool ResourceFork::loadFromMacBinary(std::string filename) {
_file = fopen(filename.c_str(), "rb");
if (!_file)
return false;
byte infoHeader[MBI_INFOHDR];
fread(infoHeader, 1, MBI_INFOHDR, _file);
// Try to parse the MacBinary header
if (infoHeader[MBI_ZERO1] == 0 && infoHeader[MBI_ZERO2] == 0 &&
infoHeader[MBI_ZERO3] == 0 && infoHeader[MBI_NAMELEN] <= MAXNAMELEN) {
// Pull out the resource fork length
uint32 dataSize = READ_UINT32_BE(infoHeader + MBI_DFLEN);
uint32 rsrcSize = READ_UINT32_BE(infoHeader + MBI_RFLEN);
uint32 dataSizePad = (((dataSize + 127) >> 7) << 7);
uint32 rsrcSizePad = (((rsrcSize + 127) >> 7) << 7);
// Length check
if (MBI_INFOHDR + dataSizePad + rsrcSizePad == getFileSize(_file))
return loadInternal(MBI_INFOHDR + dataSizePad);
}
close();
return false;
}
bool ResourceFork::loadFromAppleDouble(std::string filename) {
_file = fopen(filename.c_str(), "rb");
if (!_file)
return false;
uint32 tag = readUint32BE(_file);
if (tag != 0x00051607 && tag != 0x00051600) { // AppleDouble and AppleSingle, respectively
close();
return false;
}
fseek(_file, 20, SEEK_CUR); // version + home file system
uint16 entryCount = readUint16BE(_file);
for (uint16 i = 0; i < entryCount; i++) {
uint32 id = readUint32BE(_file);
uint32 offset = readUint32BE(_file);
/* uint32 length = */ readUint32BE(_file);
if (id == 2) // Found the resource fork!
return loadInternal(offset);
}
close();
return false;
}
bool ResourceFork::loadInternal(uint32 startOffset) {
fseek(_file, startOffset, SEEK_SET);
uint32 fileSize = getFileSize(_file);
uint32 dataOffset = readUint32BE(_file) + startOffset;
uint32 mapOffset = readUint32BE(_file) + startOffset;
uint32 dataSize = readUint32BE(_file);
uint32 mapSize = readUint32BE(_file);
if (feof(_file) || dataOffset == 0 || mapOffset == 0 || dataOffset >= fileSize || mapOffset >= fileSize
|| dataOffset + dataSize > fileSize || mapOffset + mapSize > fileSize) {
close();
return false;
}
fseek(_file, mapOffset + 24, SEEK_SET);
uint16 typeOffset = readUint16BE(_file);
uint16 nameOffset = readUint16BE(_file);
uint16 typeCount = readUint16BE(_file) + 1;
if (feof(_file) || typeOffset == 0 || typeOffset >= fileSize) {
close();
return false;
}
_types.resize(typeCount);
for (uint16 i = 0; i < typeCount; i++) {
_types[i].tag = readUint32BE(_file);
uint16 idCount = readUint16BE(_file) + 1;
uint16 idOffset = readUint16BE(_file);
uint32 lastTypePos = ftell(_file);
fseek(_file, idOffset + mapOffset + typeOffset, SEEK_SET);
for (uint16 j = 0; j < idCount; j++) {
ResourceForkID id;
id.id = readUint16BE(_file);
uint16 idNameOffset = readUint16BE(_file);
id.offset = (readUint32BE(_file) & 0xffffff) + dataOffset;
readUint32BE(_file);
if (nameOffset != 0xffff) {
uint32 lastIDPos = ftell(_file);
fseek(_file, nameOffset + mapOffset + idNameOffset, SEEK_SET);
byte stringLength = readByte(_file);
char *subFilename = new char[stringLength + 1];
subFilename[stringLength] = 0;
fread(subFilename, 1, stringLength, _file);
id.filename = subFilename;
if (j != idCount - 1)
fseek(_file, lastIDPos, SEEK_SET);
}
_types[i].ids.push_back(id);
}
if (i != typeCount - 1)
fseek(_file, lastTypePos, SEEK_SET);
}
return true;
}
void ResourceFork::close() {
if (_file) {
fclose(_file);
_file = 0;
}
_types.clear();
}
bool ResourceFork::isOpen() const {
return _file != 0;
}
DataPair *ResourceFork::getResource(uint32 tag, uint16 id) {
for (uint32 i = 0; i < _types.size(); i++) {
if (_types[i].tag != tag)
continue;
for (uint32 j = 0; j < _types[i].ids.size(); j++) {
if (_types[i].ids[j].id != id)
continue;
fseek(_file, _types[i].ids[j].offset, SEEK_SET);
uint32 length = readUint32BE(_file);
byte *data = new byte[length];
fread(data, 1, length, _file);
return new DataPair(data, length);
}
}
return 0;
}
DataPair *ResourceFork::getResource(const std::string &filename) {
for (uint32 i = 0; i < _types.size(); i++) {
for (uint32 j = 0; j < _types[i].ids.size(); j++) {
if (compareStringIgnoreCase(_types[i].ids[j].filename.c_str(), filename.c_str()))
continue;
fseek(_file, _types[i].ids[j].offset, SEEK_SET);
uint32 length = readUint32BE(_file);
byte *data = new byte[length];
fread(data, 1, length, _file);
return new DataPair(data, length);
}
}
return 0;
}
DataPair *ResourceFork::getResource(uint32 tag, const std::string &filename) {
for (uint32 i = 0; i < _types.size(); i++) {
if (_types[i].tag != tag)
continue;
for (uint32 j = 0; j < _types[i].ids.size(); j++) {
if (compareStringIgnoreCase(_types[i].ids[j].filename.c_str(), filename.c_str()))
continue;
fseek(_file, _types[i].ids[j].offset, SEEK_SET);
uint32 length = readUint32BE(_file);
byte *data = new byte[length];
fread(data, 1, length, _file);
return new DataPair(data, length);
}
}
return 0;
}
std::string ResourceFork::getFilename(uint32 tag, uint16 id) {
for (uint32 i = 0; i < _types.size(); i++) {
if (_types[i].tag != tag)
continue;
for (uint32 j = 0; j < _types[i].ids.size(); j++) {
if (_types[i].ids[j].id != id)
continue;
if (!_types[i].ids[j].filename.empty())
return _types[i].ids[j].filename;
}
}
return "";
}
const char *ResourceFork::createOutputFilename(bool useInternalName, uint32 tag, uint16 id) {
std::string actualName = getFilename(tag, id);
if (useInternalName && !actualName.empty())
return actualName.c_str();
static char filename[14];
sprintf(filename, "%c%c%c%c_%04x.dat", tag >> 24, (tag >> 16) & 0xff, (tag >> 8) & 0xff, tag & 0xff, id);
return filename;
}
std::vector<uint32> ResourceFork::getTagArray() {
std::vector<uint32> tagArray;
if (!isOpen())
return tagArray;
tagArray.resize(_types.size());
for (uint32 i = 0; i < _types.size(); i++)
tagArray[i] = _types[i].tag;
return tagArray;
}
std::vector<uint16> ResourceFork::getIDArray(uint32 tag) {
std::vector<uint16> idArray;
for (uint32 i = 0; i < _types.size(); i++)
if (_types[i].tag == tag) {
idArray.resize(_types[i].ids.size());
for (uint32 j = 0; j < _types[i].ids.size(); j++)
idArray[j] = _types[i].ids[j].id;
return idArray;
}
return idArray;
}