File: vise.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (434 lines) | stat: -rw-r--r-- 14,765 bytes parent folder | download | duplicates (2)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/compression/vise.h"

#include "common/macresman.h"
#include "common/memstream.h"
#include "common/compression/deflate.h"

// Installer VISE archive loader.
//
// Supported versions:
//
// Macintosh:
//     Installer VISE 3.5 Lite
//     Installer VISE 3.6 Lite
//
// Windows:
//     None

namespace Common {

class MacVISEArchive : public Common::Archive {
private:
	struct FileDesc {
		FileDesc();

		byte type[4];
		byte creator[4];
		uint32 compressedDataSize;
		uint32 uncompressedDataSize;
		uint32 compressedResSize;
		uint32 uncompressedResSize;
		uint32 positionInArchive;

		uint16 containingDirectory;

		Common::String name;
		Common::Path fullPath;
	};

	struct DirectoryDesc {
		uint16 containingDirectory;

		Common::String name;
		Common::Path fullPath;
	};

	class ArchiveMember : public Common::ArchiveMember {
	public:
		ArchiveMember(Common::SeekableReadStream *archiveStream, const FileDesc *fileDesc);

		Common::SeekableReadStream *createReadStream() const override;
		Common::SeekableReadStream *createReadStreamForAltStream(Common::AltStreamType altStreamType) const override;
		Common::String getName() const override;
		Common::Path getPathInArchive() const override;
		Common::String getFileName() const override;
		bool isInMacArchive() const override;

	private:
		Common::SeekableReadStream *createReadStreamForDataStream(bool isResFork) const;

		Common::SeekableReadStream *_archiveStream;
		const FileDesc *_fileDesc;
	};

public:
	explicit MacVISEArchive(Common::SeekableReadStream *archiveStream);

	bool loadCatalog();

	const FileDesc *getFileDesc(const Common::Path &path) const;

	bool hasFile(const Common::Path &path) const override;
	int listMembers(Common::ArchiveMemberList &list) const override;
	const Common::ArchiveMemberPtr getMember(const Common::Path &path) const override;
	Common::SeekableReadStream *createReadStreamForMember(const Common::Path &path) const override;
	Common::SeekableReadStream *createReadStreamForMemberAltStream(const Common::Path &path, Common::AltStreamType altStreamType) const override;
	char getPathSeparator() const override;

private:
	bool getFileDescIndex(const Common::Path &path, uint &outIndex) const;

	Common::SeekableReadStream *_archiveStream;
	Common::Array<FileDesc> _fileDescs;
	Common::Array<DirectoryDesc> _directoryDescs;
};

MacVISEArchive::ArchiveMember::ArchiveMember(Common::SeekableReadStream *archiveStream, const FileDesc *fileDesc)
	: _archiveStream(archiveStream), _fileDesc(fileDesc) {
}

Common::SeekableReadStream *MacVISEArchive::ArchiveMember::createReadStream() const {
	return createReadStreamForDataStream(false);
}

Common::SeekableReadStream *MacVISEArchive::ArchiveMember::createReadStreamForAltStream(Common::AltStreamType altStreamType) const {
	if (altStreamType == AltStreamType::MacFinderInfo) {
		Common::MacFinderInfoData *finfoData = static_cast<Common::MacFinderInfoData *>(malloc(sizeof(Common::MacFinderInfoData)));

		if (!finfoData)
			return nullptr;

		Common::MacFinderInfo finfo;
		memcpy(finfo.type, _fileDesc->type, 4);
		memcpy(finfo.creator, _fileDesc->creator, 4);

		*finfoData = finfo.toData();

		return new Common::MemoryReadStream(reinterpret_cast<const byte *>(finfoData), sizeof(Common::MacFinderInfoData), DisposeAfterUse::YES);
	}

	if (altStreamType == AltStreamType::MacResourceFork)
		return createReadStreamForDataStream(true);

	return nullptr;
}

Common::SeekableReadStream *MacVISEArchive::ArchiveMember::createReadStreamForDataStream(bool isResFork) const {
	static const uint8 vl3DeobfuscationTable[] = {
		0x6a, 0xb7, 0x36, 0xec, 0x15, 0xd9, 0xc8, 0x73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0e, 0xb2, 0x63,
		0x91, 0xcb, 0xff, 0x80, 0x71, 0xe7, 0xd4, 0x00, 0xdb, 0x75, 0x2c, 0xbd, 0x39, 0x33, 0x94, 0xbc,
		0x8c, 0x3b, 0xb6, 0x20, 0x85, 0x24, 0x88, 0x2b, 0x70, 0x83, 0x6e, 0x7b, 0x9c, 0xbe, 0x14, 0x47,
		0x65, 0x4b, 0x56, 0x81, 0xf8, 0x12, 0x11, 0x28, 0xeb, 0x55, 0x74, 0xa1, 0x31, 0xf7, 0xb0, 0x13,
		0x86, 0xdd, 0x5f, 0x42, 0xd3, 0x02, 0x61, 0x95, 0x0c, 0x5c, 0xa5, 0xcd, 0xc0, 0x07, 0xe2, 0xf3,
	};

	uint32 uncompressedSize = isResFork ? _fileDesc->uncompressedResSize : _fileDesc->uncompressedDataSize;
	uint32 compressedSize = isResFork ? _fileDesc->compressedResSize : _fileDesc->compressedDataSize;
	uint32 filePosition = _fileDesc->positionInArchive;

	if (uncompressedSize == 0 && !isResFork) {
		// Always return a stream for the data fork, even if it's empty
		return new Common::MemoryReadStream(nullptr, 0, DisposeAfterUse::NO);
	}

	if (isResFork)
		filePosition += _fileDesc->compressedDataSize;

	if (uncompressedSize == 0)
		return nullptr;

	Common::Array<byte> compressedData;
	compressedData.resize(compressedSize);

	_archiveStream->seek(filePosition, SEEK_SET);
	if (_archiveStream->read(&compressedData[0], compressedSize) != compressedSize)
		return nullptr;

	// Undo byte swapping
	for (uint i = 1; i < compressedSize; i += 2) {
		byte temp = compressedData[i];
		compressedData[i] = compressedData[i - 1];
		compressedData[i - 1] = temp;
	}

	// Undo obfuscation
	for (byte &b : compressedData)
		b = vl3DeobfuscationTable[b];

	byte *decompressedData = static_cast<byte *>(malloc(uncompressedSize));
	if (!decompressedData)
		return nullptr;


	// WARNING/TODO: Based on reverse engineering of the "Dcmp" resource from the installer, which contains the decompression code,
	// the bitstream format is usually just deflate, however there is one difference: Stored blocks are flushed to a 2-byte boundary
	// instead of 1-byte boundary, because the decompressor reads 2 bytes at a time.  This doesn't usually matter because stored
	// blocks are very rare in practice on compressible data, and small files with only 1 compressible block are already 2-byte
	// aligned on the first block.
	//
	// If this turns out to be significant, then this will need to be updated to pass information to the deflate decompressor to
	// handle the non-standard behavior.
	if (!Common::inflateZlibHeaderless(decompressedData, uncompressedSize, &compressedData[0], compressedSize)) {
		free(decompressedData);
		return nullptr;
	}

	return new Common::MemoryReadStream(decompressedData, uncompressedSize, DisposeAfterUse::YES);
}

Common::String MacVISEArchive::ArchiveMember::getName() const {
	return getPathInArchive().getLastComponent().toString(':');
}

Common::Path MacVISEArchive::ArchiveMember::getPathInArchive() const {
	return _fileDesc->fullPath;
}

Common::String MacVISEArchive::ArchiveMember::getFileName() const {
	return _fileDesc->name;
}

bool MacVISEArchive::ArchiveMember::isInMacArchive() const {
	return true;
}

MacVISEArchive::FileDesc::FileDesc() : type{0, 0, 0, 0}, creator{0, 0, 0, 0}, compressedDataSize(0), uncompressedDataSize(0), compressedResSize(0), uncompressedResSize(0), positionInArchive(0) {
}

MacVISEArchive::MacVISEArchive(Common::SeekableReadStream *archiveStream) : _archiveStream(archiveStream) {
}

bool MacVISEArchive::loadCatalog() {
	uint8 vl3Header[44];
	if (_archiveStream->read(vl3Header, 44) != 44 || memcmp(vl3Header, "SVCT", 4)) {
		debug(1, "Failed to read VISE header");
		return false;
	}

	uint32 catalogPosition = READ_BE_UINT32(vl3Header + 36);

	uint32 archiveVersion = READ_BE_UINT32(vl3Header + 16);

	if (archiveVersion == 0x80010202)
		debug(3, "Detected VISE archive as 3.5 Lite");
	else if (archiveVersion == 0x80010300)
		debug(3, "Detected VISE archive as 3.6 Lite");
	else {
		debug(1, "Unrecognized VISE archive version");
		return false;
	}

	if (!_archiveStream->seek(catalogPosition))
		error("Failed to seek to VISE catalog");

	uint8 vl3Catalog[20];
	if (_archiveStream->read(vl3Catalog, 20) != 20 || memcmp(vl3Catalog, "CVCT", 4)) {
		debug(1, "Failed to read VISE catalog");
		return false;
	}

	uint16 numEntries = READ_BE_UINT16(vl3Catalog + 16);

	for (uint16 i = 0; i < numEntries; i++) {
		uint8 entryMagic[4];
		if (_archiveStream->read(entryMagic, 4) != 4 || memcmp(entryMagic + 1, "VCT", 3)) {
			debug(1, "Failed to read VISE catalog item");
			return false;
		}

		if (entryMagic[0] == 'D') {
			uint8 directoryData[78];
			if (_archiveStream->read(directoryData, 78) != 78) {
				debug(1, "Failed to read VISE directory");
				return false;
			}

			// 3.6 Lite archives have an additional 6 bytes before the name in directory entries
			if (archiveVersion == 0x80010300)
				_archiveStream->seek(6, SEEK_CUR);

			DirectoryDesc desc;
			desc.containingDirectory = READ_BE_UINT16(directoryData + 68);

			uint8 nameLength = directoryData[76];

			if (nameLength > 0) {
				char fileNameChars[256];
				if (_archiveStream->read(fileNameChars, nameLength) != nameLength) {
					debug(1, "Failed to read VISE directory name");
					return false;
				}
				desc.name = Common::String(fileNameChars, nameLength);
			}

			_directoryDescs.push_back(desc);
		} else if (entryMagic[0] == 'F') {
			uint8 fileData[120];
			if (_archiveStream->read(fileData, 120) != 120) {
				debug(1, "Failed to read VISE file");
				return false;
			}

			FileDesc desc;
			memcpy(desc.type, fileData + 40, 4);
			memcpy(desc.creator, fileData + 44, 4);
			desc.compressedDataSize = READ_BE_UINT32(fileData + 64);
			desc.uncompressedDataSize = READ_BE_UINT32(fileData + 68);
			desc.compressedResSize = READ_BE_UINT32(fileData + 72);
			desc.uncompressedResSize = READ_BE_UINT32(fileData + 76);
			desc.containingDirectory = READ_BE_UINT16(fileData + 92);
			desc.positionInArchive = READ_BE_UINT32(fileData + 96);

			uint8 nameLength = fileData[118];

			if (nameLength > 0) {
				char fileNameChars[256];
				if (_archiveStream->read(fileNameChars, nameLength) != nameLength) {
					debug(1, "Failed to read VISE file name");
					return false;
				}
				desc.name = Common::String(fileNameChars, nameLength);
			}

			_fileDescs.push_back(desc);
		} else {
			debug(1, "Unknown VISE catalog entry item type");
			return false;
		}
	}

	// Generate full paths
	for (DirectoryDesc &dirDesc : _directoryDescs) {
		if (dirDesc.containingDirectory == 0)
			dirDesc.fullPath = dirDesc.name;
		else {
			if (dirDesc.containingDirectory > _directoryDescs.size())
				error("VISE 3 containing directory index was invalid");

			dirDesc.fullPath = _directoryDescs[dirDesc.containingDirectory - 1].fullPath.appendComponent(dirDesc.name);
		}
	}

	for (FileDesc &fileDesc : _fileDescs) {
		if (fileDesc.containingDirectory == 0)
			fileDesc.fullPath = fileDesc.name;
		else {
			if (fileDesc.containingDirectory > _directoryDescs.size())
				error("VISE 3 containing directory index was invalid");

			fileDesc.fullPath = _directoryDescs[fileDesc.containingDirectory - 1].fullPath.appendComponent(fileDesc.name);
		}
	}

	return true;
}

const MacVISEArchive::FileDesc *MacVISEArchive::getFileDesc(const Common::Path &path) const {
	for (const FileDesc &desc : _fileDescs) {
		if (desc.fullPath == path)
			return &desc;
	}

	return nullptr;
}

bool MacVISEArchive::hasFile(const Common::Path &path) const {
	uint index = 0;
	return getFileDescIndex(path, index);
}

int MacVISEArchive::listMembers(Common::ArchiveMemberList &list) const {
	int numMembers = 0;
	for (uint fileIndex = 0; fileIndex < _fileDescs.size(); fileIndex++) {
		const FileDesc &desc = _fileDescs[fileIndex];

		list.push_back(Common::ArchiveMemberPtr(new ArchiveMember(_archiveStream, &desc)));
		numMembers++;
	}
	return numMembers;
}

const Common::ArchiveMemberPtr MacVISEArchive::getMember(const Common::Path &path) const {
	uint descIndex = 0;
	if (!getFileDescIndex(path, descIndex))
		return nullptr;

	return Common::ArchiveMemberPtr(new ArchiveMember(_archiveStream, &_fileDescs[descIndex]));
}

Common::SeekableReadStream *MacVISEArchive::createReadStreamForMember(const Common::Path &path) const {
	Common::ArchiveMemberPtr archiveMember = getMember(path);
	if (!archiveMember)
		return nullptr;

	return archiveMember->createReadStream();
}

Common::SeekableReadStream *MacVISEArchive::createReadStreamForMemberAltStream(const Common::Path &path, Common::AltStreamType altStreamType) const {
	Common::ArchiveMemberPtr archiveMember = getMember(path);
	if (!archiveMember)
		return nullptr;

	return archiveMember->createReadStreamForAltStream(altStreamType);
}

char MacVISEArchive::getPathSeparator() const {
	return ':';
}

bool MacVISEArchive::getFileDescIndex(const Common::Path &path, uint &outIndex) const {
	for (uint descIndex = 0; descIndex < _fileDescs.size(); descIndex++) {
		const FileDesc &desc = _fileDescs[descIndex];

		if (desc.fullPath == path) {
			outIndex = descIndex;
			return true;
		}
	}

	return false;
}

Common::Archive *createMacVISEArchive(Common::SeekableReadStream *stream) {
	MacVISEArchive *archive = new MacVISEArchive(stream);
	if (!archive->loadCatalog()) {
		delete archive;
		return nullptr;
	}

	return archive;
}

} // End of namespace Common