File: installshield_cab.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 (536 lines) | stat: -rw-r--r-- 17,483 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* 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/>.
 *
 */

// The following code is based on unshield
// Original copyright:

// Copyright (c) 2003 David Eriksson <twogood@users.sourceforge.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "common/archive.h"
#include "common/debug.h"
#include "common/hash-str.h"
#include "common/compression/installshield_cab.h"
#include "common/memstream.h"
#include "common/substream.h"
#include "common/ptr.h"
#include "common/compression/deflate.h"
#include "common/file.h"

namespace Common {

namespace {

bool inflateZlibInstallShield(byte *dst, uint dstLen, const byte *src, uint srcLen) {
	if (!dst || !dstLen || !src || !srcLen)
		return false;

	// See if we have sync bytes. If so, just use our function for that.
	if (srcLen >= 4 && READ_BE_UINT32(src + srcLen - 4) == 0xFFFF)
		return inflateZlibHeaderless(dst, &dstLen, src, srcLen);

	// Otherwise, we have some custom code we get to use here.

	uint32 bytesRead = 0, bytesProcessed = 0;
	while (dstLen > 0 && bytesRead < srcLen) {
		uint16 chunkSize = READ_LE_UINT16(src + bytesRead);
		bytesRead += 2;

		uint zlibLen = dstLen;
		if (!inflateZlibHeaderless(dst + bytesProcessed, &zlibLen, src + bytesRead, chunkSize)) {
			return false;
		}

		bytesProcessed += zlibLen;
		dstLen -= zlibLen;
		bytesRead += chunkSize;
	}
	return true;
}

class InstallShieldCabinet : public Archive {
public:
	InstallShieldCabinet();

	bool open(const Path *baseName, Common::Archive *archive, const FSNode *node);
	void close();

	// Archive API implementation
	bool hasFile(const Path &path) const override;
	int listMembers(ArchiveMemberList &list) const override;
	const ArchiveMemberPtr getMember(const Path &path) const override;
	SeekableReadStream *createReadStreamForMember(const Path &path) const override;

private:
	enum Flags { kSplit = 1, kObfuscated = 2, kCompressed = 4, kInvalid = 8 };

	struct FileEntry {
		uint32 uncompressedSize;
		uint32 compressedSize;
		uint32 offset;
		uint16 flags;
		uint16 volume;
	};

	struct VolumeHeader {
		int version;
		uint32 cabDescriptorOffset;

		uint32 dataOffset;
		uint32 firstFileIndex;
		uint32 lastFileIndex;
		uint32 firstFileOffset;
		uint32 firstFileSizeUncompressed;
		uint32 firstFileSizeCompressed;
		uint32 lastFileOffset;
		uint32 lastFileSizeUncompressed;
		uint32 lastFileSizeCompressed;
	};

	int _version;
	typedef HashMap<Path, FileEntry, Path::IgnoreCase_Hash, Path::IgnoreCase_EqualTo> FileMap;
	FileMap _map;
	Path _baseName;
	Common::Array<VolumeHeader> _volumeHeaders;
	Common::Archive *_archive;

	static bool readVolumeHeader(SeekableReadStream *volumeStream, VolumeHeader &inVolumeHeader);

	Path getHeaderName() const;
	Path getVolumeName(uint volume) const;
};

InstallShieldCabinet::InstallShieldCabinet() : _version(0), _archive(nullptr) {
}

bool InstallShieldCabinet::open(const Path *baseName, Common::Archive *archive, const FSNode *node) {
	// Store the base name so we can generate file names
	if (baseName) {
		_baseName = *baseName;
		_archive = archive;
	} else if (node) {
		_baseName = node->getPath();
		_archive = nullptr;
	} else {
		return false;
	}

	String strippedName = _baseName.baseName();
	if (strippedName.hasSuffix(".cab") || strippedName.hasSuffix(".hdr")) {
		strippedName.erase(strippedName.size() - 5, String::npos);
		_baseName = _baseName.getParent().appendComponent(strippedName);
	}

	uint fileIndex = 0;
	ScopedPtr<SeekableReadStream> file;

	// First, open all the .cab files and read their headers
	uint volume = 1;
	for (;;) {
		if (_archive) {
			file.reset(_archive->createReadStreamForMember(getVolumeName(volume++)));
			if (!file.get()) {
				break;
			}
		} else {
			file.reset(new Common::File());
			if (!((Common::File *)file.get())->open(Common::FSNode(getVolumeName(volume++)))) {
				break;
			}
		}

		_volumeHeaders.push_back(VolumeHeader());
		readVolumeHeader(file.get(), _volumeHeaders.back());
	}

	// Try to open a header (.hdr) file to get the file list
	if (_archive) {
		file.reset(_archive->createReadStreamForMember(getHeaderName()));
		if (!file) {
			// No header file is present, file list is in first .cab file
			file.reset(_archive->createReadStreamForMember(getVolumeName(1)));
		}
	} else {
		file.reset(new Common::File());
		if (!((Common::File *)file.get())->open(Common::FSNode(getHeaderName()))) {
			// No header file is present, file list is in first .cab file
			if (!((Common::File *)file.get())->open(Common::FSNode(getVolumeName(1)))) {
				file.reset(nullptr);
			}
		}
	}

	if (!file) {
		close();
		return false;
	}

	VolumeHeader headerHeader;
	if (!readVolumeHeader(file.get(), headerHeader)) {
		close();
		return false;
	}

	_version = headerHeader.version;

	file->seek(headerHeader.cabDescriptorOffset);

	file->skip(12);
	uint32 fileTableOffset = file->readUint32LE();
	file->skip(4);
	uint32 fileTableSize = file->readUint32LE();
	uint32 fileTableSize2 = file->readUint32LE();
	uint32 directoryCount = file->readUint32LE();
	file->skip(8);
	uint32 fileCount = file->readUint32LE();

	if (fileTableSize != fileTableSize2)
		warning("file table sizes do not match");

	// We're ignoring file groups and components since we
	// should not need them. Moving on to the files...

	if (_version >= 6) {
		uint32 fileTableOffset2 = file->readUint32LE();

		for (uint32 j = 0; j < fileCount; j++) {
			file->seek(headerHeader.cabDescriptorOffset + fileTableOffset + fileTableOffset2 + j * 0x57);
			FileEntry entry;
			entry.flags = file->readUint16LE();
			entry.uncompressedSize = file->readUint32LE();
			file->skip(4);
			entry.compressedSize = file->readUint32LE();
			file->skip(4);
			entry.offset = file->readUint32LE();
			file->skip(36);
			uint32 nameOffset = file->readUint32LE();
			/* uint32 directoryIndex = */ file->readUint16LE();
			file->skip(12);
			/* entry.linkPrev = */ file->readUint32LE();
			/* entry.linkNext = */ file->readUint32LE();
			/* entry.linkFlags = */ file->readByte();
			entry.volume = file->readUint16LE();

			// Make sure the entry has a name and data inside the cab
			if (nameOffset == 0 || entry.offset == 0 || (entry.flags & kInvalid))
				continue;

			// Then let's get the string
			file->seek(headerHeader.cabDescriptorOffset + fileTableOffset + nameOffset);
			Path fileName(file->readString(), '\\');

			// Entries can appear in multiple volumes (sometimes erroneously).
			// We keep the one with the lowest volume ID
			if (!_map.contains(fileName) || _map[fileName].volume > entry.volume)
				_map[fileName] = entry;
		}
	} else {
		file->seek(headerHeader.cabDescriptorOffset + fileTableOffset);
		uint32 fileTableCount = directoryCount + fileCount;
		Array<uint32> fileTableOffsets;
		fileTableOffsets.resize(fileTableCount);
		for (uint32 j = 0; j < fileTableCount; j++)
			fileTableOffsets[j] = file->readUint32LE();

		for (uint32 j = directoryCount; j < fileCount + directoryCount; j++) {
			file->seek(headerHeader.cabDescriptorOffset + fileTableOffset + fileTableOffsets[j]);
			uint32 nameOffset = file->readUint32LE();
			/* uint32 directoryIndex = */ file->readUint32LE();

			// First read in data needed by us to get at the file data
			FileEntry entry;
			entry.flags = file->readUint16LE();
			entry.uncompressedSize = file->readUint32LE();
			entry.compressedSize = file->readUint32LE();
			file->skip(20);
			entry.offset = file->readUint32LE();
			entry.volume = 0;

			// Make sure the entry has a name and data inside the cab
			if (nameOffset == 0 || entry.offset == 0 || (entry.flags & kInvalid))
				continue;

			for (uint i = 1; i < _volumeHeaders.size() + 1; ++i) {
				// Check which volume the file is in
				VolumeHeader &volumeHeader = _volumeHeaders[i - 1];
				if (fileIndex >= volumeHeader.firstFileIndex && fileIndex <= volumeHeader.lastFileIndex) {
					entry.volume = i;

					// Check if the file is split across volumes
					if (fileIndex == volumeHeader.lastFileIndex &&
						entry.compressedSize != headerHeader.lastFileSizeCompressed &&
						headerHeader.lastFileSizeCompressed != 0) {
						
						entry.flags |= kSplit;
					}

					break;
				}
			}

			// Then let's get the string
			file->seek(headerHeader.cabDescriptorOffset + fileTableOffset + nameOffset);
			Path fileName(file->readString(), '\\');

			if (entry.volume == 0) {
				warning("Couldn't find the volume for file %s", fileName.toString('\\').c_str());
				close();
				return false;
			}

			++fileIndex;

			// Entries can appear in multiple volumes (sometimes erroneously).
			// We keep the one with the lowest volume ID
			if (!_map.contains(fileName) || _map[fileName].volume > entry.volume)
				_map[fileName] = entry;
		}
	}

	return true;
}

void InstallShieldCabinet::close() {
	_baseName.clear();
	_map.clear();
	_volumeHeaders.clear();
	_version = 0;
}

bool InstallShieldCabinet::hasFile(const Path &path) const {
	return _map.contains(path);
}

int InstallShieldCabinet::listMembers(ArchiveMemberList &list) const {
	for (FileMap::const_iterator it = _map.begin(); it != _map.end(); it++)
		list.push_back(getMember(it->_key));

	return _map.size();
}

const ArchiveMemberPtr InstallShieldCabinet::getMember(const Path &path) const {
	return ArchiveMemberPtr(new GenericArchiveMember(path, *this));
}

SeekableReadStream *InstallShieldCabinet::createReadStreamForMember(const Path &path) const {
	if (!_map.contains(path))
		return nullptr;

	const FileEntry &entry = _map[path];

	if (entry.flags & kObfuscated) {
		warning("Cannot extract obfuscated file %s", path.toString().c_str());
		return nullptr;
	}

	ScopedPtr<SeekableReadStream> stream;
	if (_archive) {
		stream.reset(_archive->createReadStreamForMember(getVolumeName((entry.volume))));
	} else {
		stream.reset(new Common::File());
		if (!((Common::File *)stream.get())->open(Common::FSNode(getVolumeName((entry.volume))))) {
			stream.reset(nullptr);
		}
	}

	if (!stream) {
		warning("Failed to open volume for file '%s'", path.toString().c_str());
		return nullptr;
	}

	byte *src = nullptr;
	if (entry.flags & kSplit) {
		// File is split across volumes
		src = (byte *)malloc(entry.compressedSize);
		uint bytesRead = 0;
		uint volume = entry.volume;

		// Read the first part of the split file
		stream->seek(entry.offset);
		stream->read(src, _volumeHeaders[volume - 1].lastFileSizeCompressed);
		bytesRead += _volumeHeaders[volume - 1].lastFileSizeCompressed;

		// Then, iterate through the next volumes until we've read all the data for the file
		while (bytesRead < entry.compressedSize) {
			if (_archive) {
				stream.reset(_archive->createReadStreamForMember(getVolumeName((++volume))));
			} else {
				if (!((Common::File *)stream.get())->open(Common::FSNode(getVolumeName((++volume))))) {
					stream.reset(nullptr);
				}
			}
			
			if (!stream.get()) {
				warning("Failed to read split file %s", path.toString().c_str());
				free(src);
				return nullptr;
			}
			stream->seek(_volumeHeaders[volume - 1].firstFileOffset);
			stream->read(src + bytesRead, _volumeHeaders[volume - 1].firstFileSizeCompressed);
			bytesRead += _volumeHeaders[volume - 1].firstFileSizeCompressed;
		}
	}

	// Uncompressed file
	if (!(entry.flags & kCompressed)) {
		if (src == nullptr) {
			// File not split, return a substream
			return new SeekableSubReadStream(stream.release(), entry.offset, entry.offset + entry.uncompressedSize, DisposeAfterUse::YES);
		} else {
			// File split, return the assembled data
			return new MemoryReadStream(src, entry.uncompressedSize, DisposeAfterUse::YES);
		}		
	}

	byte *dst = (byte *)malloc(entry.uncompressedSize);

	if (!src) {
		src = (byte *)malloc(entry.compressedSize);
		stream->seek(entry.offset);
		stream->read(src, entry.compressedSize);
	}

	// Entries with size 0 are valid, and do not need to be inflated
	if (entry.compressedSize != 0) {
		if (!inflateZlibInstallShield(dst, entry.uncompressedSize, src, entry.compressedSize)) {
			warning("failed to inflate CAB file '%s'", path.toString().c_str());
			free(dst);
			free(src);
			return nullptr;
		}
	}

	free(src);

	return new MemoryReadStream(dst, entry.uncompressedSize, DisposeAfterUse::YES);
}

bool InstallShieldCabinet::readVolumeHeader(SeekableReadStream *volumeStream, InstallShieldCabinet::VolumeHeader &inVolumeHeader) {
	// Check for the cab signature
	volumeStream->seek(0);
	uint32 signature = volumeStream->readUint32LE();
	if (signature != 0x28635349) {
		warning("InstallShieldCabinet signature doesn't match: expecting %x but got %x", 0x28635349, signature);
		return false;
	}

	// We support cabinet versions 5 - 13, but do not deobfuscate obfuscated files

	uint32 magicBytes = volumeStream->readUint32LE();
	int shift = magicBytes >> 24;
	inVolumeHeader.version = shift == 1 ? (magicBytes >> 12) & 0xf : (magicBytes & 0xffff) / 100;
	inVolumeHeader.version = (inVolumeHeader.version == 0) ? 5 : inVolumeHeader.version;

	if (inVolumeHeader.version < 5 || inVolumeHeader.version > 13) {
		warning("Unsupported CAB version %d, magic bytes %08x", inVolumeHeader.version, magicBytes);
		return false;
	}

	/* uint32 volumeInfo = */ volumeStream->readUint32LE();
	inVolumeHeader.cabDescriptorOffset = volumeStream->readUint32LE();
	/* uint32 cabDescriptorSize = */ volumeStream->readUint32LE();

	// Read the version-specific part of the header
	if (inVolumeHeader.version == 5) {
		inVolumeHeader.dataOffset = volumeStream->readUint32LE();
		volumeStream->skip(4);
		inVolumeHeader.firstFileIndex = volumeStream->readUint32LE();
		inVolumeHeader.lastFileIndex = volumeStream->readUint32LE();
		inVolumeHeader.firstFileOffset = volumeStream->readUint32LE();
		inVolumeHeader.firstFileSizeUncompressed = volumeStream->readUint32LE();
		inVolumeHeader.firstFileSizeCompressed = volumeStream->readUint32LE();
		inVolumeHeader.lastFileOffset = volumeStream->readUint32LE();
		inVolumeHeader.lastFileSizeUncompressed = volumeStream->readUint32LE();
		inVolumeHeader.lastFileSizeCompressed = volumeStream->readUint32LE();
	} else {
		inVolumeHeader.dataOffset = volumeStream->readUint32LE();
		volumeStream->skip(4);
		inVolumeHeader.firstFileIndex = volumeStream->readUint32LE();
		inVolumeHeader.lastFileIndex = volumeStream->readUint32LE();
		inVolumeHeader.firstFileOffset = volumeStream->readUint32LE();
		volumeStream->skip(4);
		inVolumeHeader.firstFileSizeUncompressed = volumeStream->readUint32LE();
		volumeStream->skip(4);
		inVolumeHeader.firstFileSizeCompressed = volumeStream->readUint32LE();
		volumeStream->skip(4);
		inVolumeHeader.lastFileOffset = volumeStream->readUint32LE();
		volumeStream->skip(4);
		inVolumeHeader.lastFileSizeUncompressed = volumeStream->readUint32LE();
		volumeStream->skip(4);
		inVolumeHeader.lastFileSizeCompressed = volumeStream->readUint32LE();
		volumeStream->skip(4);
	}

	return true;
}

Path InstallShieldCabinet::getHeaderName() const {
	return _baseName.append("1.hdr");
}

Path InstallShieldCabinet::getVolumeName(uint volume) const {
	return _baseName.append(String::format("%d.cab", volume));
}

} // End of anonymous namespace

Archive *makeInstallShieldArchive(const Path &baseName) {
	return makeInstallShieldArchive(baseName, SearchMan);
}

Archive *makeInstallShieldArchive(const Common::Path &baseName, Common::Archive &archive) {
	InstallShieldCabinet *cab = new InstallShieldCabinet();
	if (!cab->open(&baseName, &archive, nullptr)) {
		delete cab;
		return nullptr;
	}

	return cab;
}

Archive *makeInstallShieldArchive(const FSNode &baseName) {
	InstallShieldCabinet *cab = new InstallShieldCabinet();
	if (!cab->open(nullptr, nullptr, &baseName)) {
		delete cab;
		return nullptr;
	}

	return cab;
}

} // End of namespace Common