aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engines/titanic/compressed_file.cpp115
-rw-r--r--engines/titanic/compressed_file.h25
-rw-r--r--engines/titanic/compression.cpp291
-rw-r--r--engines/titanic/compression.h106
-rw-r--r--engines/titanic/simple_file.cpp5
-rw-r--r--engines/titanic/simple_file.h5
6 files changed, 24 insertions, 523 deletions
diff --git a/engines/titanic/compressed_file.cpp b/engines/titanic/compressed_file.cpp
index c200370eae..d0cd37671a 100644
--- a/engines/titanic/compressed_file.cpp
+++ b/engines/titanic/compressed_file.cpp
@@ -27,13 +27,8 @@ namespace Titanic {
#define BUFFER_SIZE 1024
CompressedFile::CompressedFile() : SimpleFile() {
- _fileMode = COMPMODE_NONE;
- Common::fill(&_writeBuffer[0], &_writeBuffer[516], 0);
- _dataStartPtr = nullptr;
- _dataPtr = nullptr;
- _dataRemaining = 0;
- _dataMaxSize = 0;
- _dataCount = 0;
+ _readStream = nullptr;
+ _writeStream = nullptr;
}
CompressedFile::~CompressedFile() {
@@ -41,119 +36,39 @@ CompressedFile::~CompressedFile() {
void CompressedFile::open(const Common::String &name) {
SimpleFile::open(name);
-
- _compression.initDecompress();
- _fileMode = COMPMODE_READ;
- _dataPtr = _dataStartPtr = new byte[BUFFER_SIZE];
- _dataMaxSize = BUFFER_SIZE;
- _dataRemaining = 0;
- _dataCount = 0;
+ _readStream = Common::wrapCompressedReadStream(&_file);
}
void CompressedFile::open(Common::SeekableReadStream *stream) {
SimpleFile::open(stream);
-
- _compression.initDecompress();
- _fileMode = COMPMODE_READ;
- _dataPtr = _dataStartPtr = new byte[BUFFER_SIZE];
- _dataMaxSize = BUFFER_SIZE;
- _dataRemaining = 0;
- _dataCount = 0;
+ _readStream = Common::wrapCompressedReadStream(&_file);
}
void CompressedFile::open(Common::OutSaveFile *stream) {
SimpleFile::open(stream);
-
- _compression.initCompress();
- _fileMode = COMPMODE_WRITE;
+ _writeStream = Common::wrapCompressedWriteStream(stream);
}
void CompressedFile::close() {
- int result;
-
- switch (_fileMode) {
- case COMPMODE_WRITE:
- do {
- _compression._destPtr = _writeBuffer;
- _compression._destCount = 512;
- result = _compression.compress(4);
- int count = 512 - _compression._destCount;
+ delete _readStream;
+ delete _writeStream;
+ _readStream = nullptr;
+ _writeStream = nullptr;
- if (count)
- write(_writeBuffer, count);
- } while (!result);
- break;
- case COMPMODE_READ:
- _compression.close();
- delete[] _dataStartPtr;
- _dataStartPtr = _dataPtr = nullptr;
- _dataRemaining = _dataMaxSize = 0;
-
- SimpleFile::close();
- break;
- default:
- break;
- }
+ SimpleFile::close();
}
size_t CompressedFile::unsafeRead(void *dst, size_t count) {
- assert(_file.isOpen());
+ assert(_readStream);
if (count == 0)
return 0;
- // Ensure there's enough data queued in the buffer
- decompress();
-
- // Pass the data to the output buffer
- size_t bytesRead = 0;
- byte *dataPtr = (byte *)dst;
-
- while (count > 0) {
- if (!_dataRemaining) {
- decompress();
- if (!_dataRemaining)
- break;
- }
-
- *dataPtr++ = *_dataPtr++;
- --_dataRemaining;
- }
-
- return bytesRead;
+ // Read data and decompress
+ return _readStream->read(dst, count);
}
-void CompressedFile::decompress() {
- const size_t COUNT = 1;
- byte fileByte;
- int count;
-
- _dataPtr = _dataStartPtr;
- _compression._destPtr = _dataStartPtr;
- _compression._destCount = _dataMaxSize;
-
- if (_dataMaxSize < 0x100)
- return;
-
- // Loop to get data from the file as needed and decompress
- do {
- if (!_compression._srcCount) {
- // Read in next byte from the source file
- if (!SimpleFile::unsafeRead(&fileByte, 1))
- break;
-
- // Set up the decompressor to process the data
- _compression._srcCount = COUNT;
- _compression._srcPtr = &fileByte;
- }
-
- int count = _compression.decompress(COUNT);
- _dataRemaining = _dataMaxSize - _compression._destCount;
-
- if (count == COUNT) {
- _dataCount = COUNT;
- break;
- }
- } while (!count && _compression._destCount > 0x100);
+size_t CompressedFile::write(const void *src, size_t count) {
+ return _writeStream->write(src, count);
}
} // End of namespace Titanic
diff --git a/engines/titanic/compressed_file.h b/engines/titanic/compressed_file.h
index 30b925d9b6..4b1051be5c 100644
--- a/engines/titanic/compressed_file.h
+++ b/engines/titanic/compressed_file.h
@@ -25,32 +25,20 @@
#include "common/scummsys.h"
#include "common/file.h"
-#include "titanic/compression.h"
+#include "common/memstream.h"
+#include "common/zlib.h"
#include "titanic/simple_file.h"
#include "titanic/string.h"
namespace Titanic {
-enum CompressedFileMode { COMPMODE_NONE, COMPMODE_WRITE, COMPMODE_READ };
-
/**
* Derived file that handles compressed files
*/
class CompressedFile : public SimpleFile {
private:
- Compression _compression;
- CompressedFileMode _fileMode;
- byte _writeBuffer[516];
- byte *_dataStartPtr;
- byte *_dataPtr;
- int _dataRemaining;
- int _dataMaxSize;
- int _dataCount;
-
- /**
- * Decompress data from the source file
- */
- void decompress();
+ Common::SeekableReadStream *_readStream;
+ Common::WriteStream *_writeStream;
public:
CompressedFile();
virtual ~CompressedFile();
@@ -79,6 +67,11 @@ public:
* Read from the file
*/
virtual size_t unsafeRead(void *dst, size_t count);
+
+ /**
+ * Write out data
+ */
+ virtual size_t write(const void *src, size_t count);
};
} // End of namespace Titanic
diff --git a/engines/titanic/compression.cpp b/engines/titanic/compression.cpp
deleted file mode 100644
index 0c12ee089f..0000000000
--- a/engines/titanic/compression.cpp
+++ /dev/null
@@ -1,291 +0,0 @@
-/* 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.
- *
- */
-
-#include "titanic/compression.h"
-#include "common/textconsole.h"
-
-namespace Titanic {
-
-CompressionData::CompressionData() {
- _commandNum = 0;
- _field4 = 0;
- _field8 = 0;
- _fieldC = 0;
- _field10 = 0;
- _field14 = 0;
-}
-
-/*------------------------------------------------------------------------*/
-
-Compression::Compression() {
- _srcPtr = nullptr;
- _srcCount = 0;
- _field8 = 0;
- _destPtr = nullptr;
- _destCount = 0;
- _field14 = 0;
- _errorMessage = nullptr;
- _compressionData = nullptr;
- _createFn = nullptr;
- _destroyFn = nullptr;
- _field28 = 0;
- _field2C = 0;
- _field30 = 0;
- _field34 = 0;
-}
-
-Compression::~Compression() {
- close();
-}
-
-void Compression::initDecompress(const char *version, int v) {
- if (!version || *version != '1')
- error("Bad version");
-
- _errorMessage = nullptr;
- if (!_createFn) {
- _createFn = &Compression::createMethod;
- _field28 = 0;
- }
-
- if (!_destroyFn) {
- _destroyFn = &Compression::destroyMethod;
- }
-
- _compressionData = (this->*_createFn)(_field28, 1);
- _compressionData->_field14 = 0;
- _compressionData->_fieldC = 0;
- if (v < 0) {
- v = -v;
- _compressionData->_fieldC = 1;
- }
-
- if (v < 8 || v > 15)
- error("Bad parameter");
-
- _compressionData->_field10 = v;
- _compressionData->_field14 = sub1(_compressionData->_fieldC ? nullptr : &Compression::method3, 1 << v);
-
- if (_compressionData->_field14)
- sub2();
- else
- close();
-}
-
-void Compression::initCompress(const char *version, int v) {
- error("TODO");
-}
-
-
-int Compression::sub1(Method3Fn fn, int v) {
- return 0;
-}
-
-void Compression::close() {
- if (_destroyFn)
- (this->*_destroyFn)(_compressionData);
-}
-
-CompressionData *Compression::createMethod(int v1, int v2) {
- return new CompressionData();
-}
-
-void Compression::destroyMethod(CompressionData *ptr) {
- delete ptr;
-}
-
-int Compression::compress(int v) {
- return 0;
-}
-
-int Compression::decompress(size_t count) {
- if (!_compressionData || !_srcPtr || !count)
- // Needed fields aren't set
- return -2;
-
- int result = -5;
- int ebx = 5;
- uint v;
-
- for (;;) {
- switch (_compressionData->_commandNum) {
- case 0:
- if (!_srcCount)
- return result;
-
- result = 0;
- --_srcCount;
- ++_field8;
- _compressionData->_field4 = *_srcPtr++;
-
- if ((_compressionData->_field4 & 0xf) == 8) {
- _compressionData->_commandNum = 13;
- _compressionData->_field4 = ebx;
- _errorMessage = "unknown compression method";
- } else {
- if ((_compressionData->_field4 / 16 + 8) > _compressionData->_field10) {
- _compressionData->_commandNum = 13;
- _compressionData->_field4 = ebx;
- _errorMessage = "invalid window size";
- } else {
- _compressionData->_commandNum = 1;
- }
- }
- break;
-
- case 1:
- if (!_srcCount)
- return result;
-
- result = 0;
- --_srcCount;
- ++_field8;
- v = *_srcPtr++;
- if ((_compressionData->_field4 * 256 + v) % 31) {
- _compressionData->_commandNum = 13;
- _compressionData->_field4 = ebx;
- ebx = 5;
- _errorMessage = "incorrect header check";
- } else if (!(v & 0x20)) {
- _compressionData->_commandNum = 7;
- ebx = 5;
- } else {
- _compressionData->_commandNum = 2;
- ebx = 5;
- }
- break;
-
- case 2:
- if (!_srcCount)
- return result;
-
- result = 0;
- --_srcCount;
- ++_field8;
- _compressionData->_field8 = (uint)*_srcPtr++ << 24;
- _compressionData->_commandNum = 3;
- break;
-
- case 3:
- if (!_srcCount)
- return result;
-
- result = 0;
- --_srcCount;
- ++_field8;
- _compressionData->_field8 += (uint)*_srcPtr++ << 16;
- _compressionData->_commandNum = 4;
- break;
-
- case 4:
- if (!_srcCount)
- return result;
-
- result = 0;
- --_srcCount;
- ++_field8;
- _compressionData->_field8 = (uint)*_srcPtr++ << 8;
- _compressionData->_commandNum = ebx;
- break;
-
- case 5:
- if (!_srcCount)
- return result;
-
- --_srcCount;
- ++_field8;
- _compressionData->_field8 += *_srcPtr++;
- _compressionData->_commandNum = ebx;
- _field30 = _compressionData->_field8;
- _compressionData->_commandNum = 6;
- return 2;
-
- case 6:
- _compressionData->_commandNum = 13;
- _compressionData->_field4 = 0;
- _errorMessage = "need dictionary";
- return -2;
-
- case 7:
- error("TODO");
- break;
-
- case 8:
- if (!_srcCount)
- return result;
-
- --_srcCount;
- ++_field8;
- _compressionData->_field8 += *_srcPtr++ << 24;
- _compressionData->_commandNum = 9;
- break;
-
- case 9:
- if (!_srcCount)
- return result;
-
- --_srcCount;
- ++_field8;
- _compressionData->_field8 += *_srcPtr++ << 16;
- _compressionData->_commandNum = 10;
- break;
-
- case 10:
- if (!_srcCount)
- return result;
-
- --_srcCount;
- ++_field8;
- _compressionData->_field8 += *_srcPtr++ << 8;
- _compressionData->_commandNum = 11;
- break;
-
- case 11:
- if (!_srcCount)
- return result;
-
- --_srcCount;
- ++_field8;
- _compressionData->_field8 += *_srcPtr++ << 8;
-
- if (_compressionData->_field4 == _compressionData->_field8) {
- _compressionData->_commandNum = 12;
- } else {
- _compressionData->_commandNum = 13;
- _compressionData->_field4 = ebx;
- _errorMessage = "incorrect data check";
- }
- break;
-
- case 12:
- return 1;
-
- case 13:
- return -3;
-
- default:
- return -2;
- }
- }
-}
-
-} // End of namespace Titanic
diff --git a/engines/titanic/compression.h b/engines/titanic/compression.h
deleted file mode 100644
index fb784ce031..0000000000
--- a/engines/titanic/compression.h
+++ /dev/null
@@ -1,106 +0,0 @@
-/* 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 TITANIC_COMPRESSION_H
-#define TITANIC_COMPRESSION_H
-
-#include "common/scummsys.h"
-
-namespace Titanic {
-
-class Compression;
-class CompressionData;
-
-typedef CompressionData *(Compression::*CompressionCreateFn)(int v1, int v2);
-typedef void(Compression::*CompressionDestroyFn)(CompressionData *ptr);
-typedef void(Compression::*Method3Fn)();
-
-class CompressionData {
-public:
- int _commandNum;
- int _field4;
- int _field8;
- int _fieldC;
- int _field10;
- int _field14;
-public:
- CompressionData();
-};
-
-class Compression {
-private:
- CompressionData *createMethod(int v1, int v2);
-
- void destroyMethod(CompressionData *ptr);
-
- void method3() {
- // TODO
- }
-
- int sub1(Method3Fn fn, int v);
-
- void sub2() {}
-public:
- byte *_srcPtr;
- int _srcCount;
- int _field8;
- byte *_destPtr;
- int _destCount;
- int _field14;
- const char *_errorMessage;
- CompressionData *_compressionData;
- CompressionCreateFn _createFn;
- CompressionDestroyFn _destroyFn;
- int _field28;
- int _field2C;
- int _field30;
- int _field34;
-public:
- Compression();
- ~Compression();
-
- /**
- * Initialize for decompression
- */
- void initDecompress(const char *version = "1.0.4", int v = 15);
-
- /**
- * Initialize for compression
- */
- void initCompress(const char *version = "1.0.4", int v = -1);
-
- void close();
-
- /**
- * Compress data
- */
- int compress(int v);
-
- /**
- * Decompress data
- */
- int decompress(size_t count);
-};
-
-} // End of namespace Titanic
-
-#endif /* TITANIC_COMPRESSION_H */
diff --git a/engines/titanic/simple_file.cpp b/engines/titanic/simple_file.cpp
index cf95aca269..521f9e9b99 100644
--- a/engines/titanic/simple_file.cpp
+++ b/engines/titanic/simple_file.cpp
@@ -74,11 +74,6 @@ size_t SimpleFile::write(const void *src, size_t count) {
return _outStream->write(src, count);
}
-bool SimpleFile::eof() const {
- assert(_inStream);
- return _inStream->pos() >= _inStream->size();
-}
-
CString SimpleFile::readString() {
char c;
CString result;
diff --git a/engines/titanic/simple_file.h b/engines/titanic/simple_file.h
index 65b0ac6f31..852c75b0f7 100644
--- a/engines/titanic/simple_file.h
+++ b/engines/titanic/simple_file.h
@@ -80,11 +80,6 @@ public:
virtual size_t write(const void *src, size_t count);
/**
- * Return true if the end of the file has been reached
- */
- bool eof() const;
-
- /**
* Read a string from the file
*/
CString readString();