aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMatthew Hoops2012-05-28 16:54:49 -0400
committerMatthew Hoops2012-05-28 16:54:49 -0400
commitab45e72e67c7effae9a9fecbaf5a77f9427d8df4 (patch)
tree344bc13beb3fddce3134aa0cb953b1523c133251 /common
parentf7e515a361dac041e6693ed9a1056df6ad05975e (diff)
downloadscummvm-rg350-ab45e72e67c7effae9a9fecbaf5a77f9427d8df4.tar.gz
scummvm-rg350-ab45e72e67c7effae9a9fecbaf5a77f9427d8df4.tar.bz2
scummvm-rg350-ab45e72e67c7effae9a9fecbaf5a77f9427d8df4.zip
COMMON: Move InstallShield code to common
The code also now works for both data compressed with sync bytes and without
Diffstat (limited to 'common')
-rw-r--r--common/installshield_cab.cpp212
-rw-r--r--common/installshield_cab.h43
-rw-r--r--common/module.mk1
-rw-r--r--common/zlib.cpp54
-rw-r--r--common/zlib.h19
5 files changed, 329 insertions, 0 deletions
diff --git a/common/installshield_cab.cpp b/common/installshield_cab.cpp
new file mode 100644
index 0000000000..e25d14741a
--- /dev/null
+++ b/common/installshield_cab.cpp
@@ -0,0 +1,212 @@
+/* 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 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.
+ *
+ */
+
+// 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/installshield_cab.h"
+#include "common/memstream.h"
+#include "common/zlib.h"
+
+namespace Common {
+
+class InstallShieldCabinet : public Archive {
+public:
+ InstallShieldCabinet(SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse);
+ ~InstallShieldCabinet();
+
+ // Archive API implementation
+ bool hasFile(const String &name) const;
+ int listMembers(ArchiveMemberList &list) const;
+ const ArchiveMemberPtr getMember(const String &name) const;
+ SeekableReadStream *createReadStreamForMember(const String &name) const;
+
+private:
+ struct FileEntry {
+ uint32 uncompressedSize;
+ uint32 compressedSize;
+ uint32 offset;
+ uint16 flags;
+ };
+
+ typedef HashMap<String, FileEntry, IgnoreCase_Hash, IgnoreCase_EqualTo> FileMap;
+ FileMap _map;
+ Common::SeekableReadStream *_stream;
+ DisposeAfterUse::Flag _disposeAfterUse;
+};
+
+InstallShieldCabinet::~InstallShieldCabinet() {
+ _map.clear();
+
+ if (_disposeAfterUse == DisposeAfterUse::YES)
+ delete _stream;
+}
+
+InstallShieldCabinet::InstallShieldCabinet(SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse) : _stream(stream), _disposeAfterUse(disposeAfterUse) {
+ // Note that we only support a limited subset of cabinet files
+ // Only single cabinet files and ones without data shared between
+ // cabinets.
+
+ // Check for the magic uint32
+ if (_stream->readUint32LE() != 0x28635349) {
+ warning("InstallShieldCabinet::InstallShieldCabinet(): Magic ID doesn't match");
+ return;
+ }
+
+ uint32 version = _stream->readUint32LE();
+
+ if (version != 0x01000004) {
+ warning("Unsupported CAB version %08x", version);
+ return;
+ }
+
+ /* uint32 volumeInfo = */ _stream->readUint32LE();
+ uint32 cabDescriptorOffset = _stream->readUint32LE();
+ /* uint32 cabDescriptorSize = */ _stream->readUint32LE();
+
+ _stream->seek(cabDescriptorOffset);
+
+ _stream->skip(12);
+ uint32 fileTableOffset = _stream->readUint32LE();
+ _stream->skip(4);
+ uint32 fileTableSize = _stream->readUint32LE();
+ uint32 fileTableSize2 = _stream->readUint32LE();
+ uint32 directoryCount = _stream->readUint32LE();
+ _stream->skip(8);
+ uint32 fileCount = _stream->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...
+
+ _stream->seek(cabDescriptorOffset + fileTableOffset);
+ uint32 fileTableCount = directoryCount + fileCount;
+ uint32 *fileTableOffsets = new uint32[fileTableCount];
+ for (uint32 i = 0; i < fileTableCount; i++)
+ fileTableOffsets[i] = _stream->readUint32LE();
+
+ for (uint32 i = directoryCount; i < fileCount + directoryCount; i++) {
+ _stream->seek(cabDescriptorOffset + fileTableOffset + fileTableOffsets[i]);
+ uint32 nameOffset = _stream->readUint32LE();
+ /* uint32 directoryIndex = */ _stream->readUint32LE();
+
+ // First read in data needed by us to get at the file data
+ FileEntry entry;
+ entry.flags = _stream->readUint16LE();
+ entry.uncompressedSize = _stream->readUint32LE();
+ entry.compressedSize = _stream->readUint32LE();
+ _stream->skip(20);
+ entry.offset = _stream->readUint32LE();
+
+ // Then let's get the string
+ _stream->seek(cabDescriptorOffset + fileTableOffset + nameOffset);
+ String fileName;
+
+ char c = _stream->readByte();
+ while (c) {
+ fileName += c;
+ c = _stream->readByte();
+ }
+ _map[fileName] = entry;
+ }
+
+ delete[] fileTableOffsets;
+}
+
+bool InstallShieldCabinet::hasFile(const String &name) const {
+ return _map.contains(name);
+}
+
+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 String &name) const {
+ return ArchiveMemberPtr(new GenericArchiveMember(name, this));
+}
+
+SeekableReadStream *InstallShieldCabinet::createReadStreamForMember(const String &name) const {
+ if (!_map.contains(name))
+ return 0;
+
+ const FileEntry &entry = _map[name];
+
+ _stream->seek(entry.offset);
+
+ if (!(entry.flags & 0x04)) // Not compressed
+ return _stream->readStream(entry.uncompressedSize);
+
+#ifdef USE_ZLIB
+ byte *src = (byte *)malloc(entry.compressedSize);
+ byte *dst = (byte *)malloc(entry.uncompressedSize);
+
+ _stream->read(src, entry.compressedSize);
+
+ bool result = inflateZlibInstallShield(dst, entry.uncompressedSize, src, entry.compressedSize);
+ free(src);
+
+ if (!result) {
+ warning("failed to inflate CAB file '%s'", name.c_str());
+ free(dst);
+ return 0;
+ }
+
+ return new MemoryReadStream(dst, entry.uncompressedSize, DisposeAfterUse::YES);
+#else
+ warning("zlib required to extract compressed CAB file '%s'", name.c_str());
+ return 0;
+#endif
+}
+
+Archive *makeInstallShieldArchive(SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse) {
+ return new InstallShieldCabinet(stream, disposeAfterUse);
+}
+
+} // End of namespace AGOS
diff --git a/common/installshield_cab.h b/common/installshield_cab.h
new file mode 100644
index 0000000000..7c4f294578
--- /dev/null
+++ b/common/installshield_cab.h
@@ -0,0 +1,43 @@
+/* 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 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.
+ *
+ */
+
+#ifndef COMMON_INSTALLSHIELD_CAB_H
+#define COMMON_INSTALLSHIELD_CAB_H
+
+#include "common/types.h"
+
+namespace Common {
+
+class Archive;
+class SeekableReadStream;
+
+/**
+ * This factory method creates an Archive instance corresponding to the content
+ * of the InstallShield compressed stream.
+ *
+ * May return 0 in case of a failure.
+ */
+Archive *makeInstallShieldArchive(SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES);
+
+} // End of namespace Common
+
+#endif
diff --git a/common/module.mk b/common/module.mk
index 92279740e5..d96b11ee40 100644
--- a/common/module.mk
+++ b/common/module.mk
@@ -16,6 +16,7 @@ MODULE_OBJS := \
gui_options.o \
hashmap.o \
iff_container.o \
+ installshield_cab.o \
language.o \
localization.o \
macresman.o \
diff --git a/common/zlib.cpp b/common/zlib.cpp
index 7d765fc539..98f319f351 100644
--- a/common/zlib.cpp
+++ b/common/zlib.cpp
@@ -85,6 +85,60 @@ bool inflateZlibHeaderless(byte *dst, uint dstLen, const byte *src, uint srcLen,
return true;
}
+enum {
+ kTempBufSize = 65536
+};
+
+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.
+
+ byte *temp = (byte *)malloc(kTempBufSize);
+
+ uint32 bytesRead = 0, bytesProcessed = 0;
+ while (bytesRead < srcLen) {
+ uint16 chunkSize = READ_LE_UINT16(src + bytesRead);
+ bytesRead += 2;
+
+ // Initialize zlib
+ z_stream stream;
+ stream.next_in = const_cast<byte *>(src + bytesRead);
+ stream.avail_in = chunkSize;
+ stream.next_out = temp;
+ stream.avail_out = kTempBufSize;
+ stream.zalloc = Z_NULL;
+ stream.zfree = Z_NULL;
+ stream.opaque = Z_NULL;
+
+ // Negative MAX_WBITS tells zlib there's no zlib header
+ int err = inflateInit2(&stream, -MAX_WBITS);
+ if (err != Z_OK)
+ return false;
+
+ err = inflate(&stream, Z_FINISH);
+ if (err != Z_OK && err != Z_STREAM_END) {
+ inflateEnd(&stream);
+ free(temp);
+ return false;
+ }
+
+ memcpy(dst + bytesProcessed, temp, stream.total_out);
+ bytesProcessed += stream.total_out;
+
+ inflateEnd(&stream);
+ bytesRead += chunkSize;
+ }
+
+ free(temp);
+ return true;
+}
+
/**
* A simple wrapper class which can be used to wrap around an arbitrary
* other SeekableReadStream and will then provide on-the-fly decompression support.
diff --git a/common/zlib.h b/common/zlib.h
index 61322c286a..8cfc5829ac 100644
--- a/common/zlib.h
+++ b/common/zlib.h
@@ -77,6 +77,25 @@ bool uncompress(byte *dst, unsigned long *dstLen, const byte *src, unsigned long
*/
bool inflateZlibHeaderless(byte *dst, uint dstLen, const byte *src, uint srcLen, const byte *dict = 0, uint dictLen = 0);
+/**
+ * Wrapper around zlib's inflate functions. This function will call the
+ * necessary inflate functions to uncompress data compressed for InstallShield
+ * cabinet files.
+ *
+ * Decompresses the src buffer into the dst buffer.
+ * srcLen is the byte length of the source buffer, dstLen is the byte
+ * length of the output buffer.
+ * It decompress as much data as possible, up to dstLen bytes.
+ *
+ * @param dst the buffer to store into.
+ * @param dstLen the size of the destination buffer.
+ * @param src the data to be decompressed.
+ * @param dstLen the size of the compressed data.
+ *
+ * @return true on success (Z_OK or Z_STREAM_END), false otherwise.
+ */
+bool inflateZlibInstallShield(byte *dst, uint dstLen, const byte *src, uint srcLen);
+
#endif
/**