Refactoring the Arj decoder code, phase 2
svn-id: r34923
This commit is contained in:
parent
060aa5b002
commit
81431f18cd
2 changed files with 14 additions and 27 deletions
|
@ -179,10 +179,9 @@ static uint32 GetCRC(byte *data, int len) {
|
||||||
return CRC ^ 0xFFFFFFFF;
|
return CRC ^ 0xFFFFFFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArjFile::ArjFile() : _uncompressedData(NULL) {
|
ArjFile::ArjFile() : _uncompressed(0) {
|
||||||
_decoder = new ArjDecoder;
|
_decoder = new ArjDecoder;
|
||||||
InitCRC();
|
InitCRC();
|
||||||
_isOpen = false;
|
|
||||||
_fallBack = false;
|
_fallBack = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,15 +341,12 @@ ArjHeader *ArjDecoder::readHeader(SeekableReadStream &stream) {
|
||||||
|
|
||||||
|
|
||||||
bool ArjFile::open(const Common::String &filename) {
|
bool ArjFile::open(const Common::String &filename) {
|
||||||
if (_isOpen)
|
if (_uncompressed)
|
||||||
error("Attempt to open another instance of archive");
|
error("Attempt to open another instance of archive");
|
||||||
|
|
||||||
_isOpen = false;
|
|
||||||
|
|
||||||
if (_fallBack) {
|
if (_fallBack) {
|
||||||
_currArchive.open(filename);
|
_currArchive.open(filename);
|
||||||
if (_currArchive.isOpen()) {
|
if (_currArchive.isOpen()) {
|
||||||
_isOpen = true;
|
|
||||||
_uncompressed = &_currArchive;
|
_uncompressed = &_currArchive;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -359,19 +355,13 @@ bool ArjFile::open(const Common::String &filename) {
|
||||||
if (!_fileMap.contains(filename))
|
if (!_fileMap.contains(filename))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
_isOpen = true;
|
|
||||||
|
|
||||||
ArjHeader *hdr = _headers[_fileMap[filename]];
|
ArjHeader *hdr = _headers[_fileMap[filename]];
|
||||||
|
|
||||||
_decoder->_compsize = hdr->compSize;
|
_decoder->_compsize = hdr->compSize;
|
||||||
_decoder->_origsize = hdr->origSize;
|
_decoder->_origsize = hdr->origSize;
|
||||||
|
|
||||||
// FIXME: This hotfix prevents Drascula from leaking memory.
|
byte *uncompressedData = (byte *)malloc(_decoder->_origsize);
|
||||||
// As far as sanity checks go this is not bad, but the engine should be fixed.
|
_decoder->_outstream = new MemoryWriteStream(uncompressedData, _decoder->_origsize);
|
||||||
free(_uncompressedData);
|
|
||||||
|
|
||||||
_uncompressedData = (byte *)malloc(_decoder->_origsize);
|
|
||||||
_decoder->_outstream = new MemoryWriteStream(_uncompressedData, _decoder->_origsize);
|
|
||||||
|
|
||||||
_currArchive.open(_archMap[filename]);
|
_currArchive.open(_archMap[filename]);
|
||||||
_currArchive.seek(hdr->pos, SEEK_SET);
|
_currArchive.seek(hdr->pos, SEEK_SET);
|
||||||
|
@ -379,7 +369,7 @@ bool ArjFile::open(const Common::String &filename) {
|
||||||
printf("Arj archive method %d, file '%s'\n", hdr->method, filename.c_str());
|
printf("Arj archive method %d, file '%s'\n", hdr->method, filename.c_str());
|
||||||
|
|
||||||
if (hdr->method == 0) { // store
|
if (hdr->method == 0) { // store
|
||||||
_currArchive.read(_uncompressedData, _decoder-> _origsize);
|
_currArchive.read(uncompressedData, _decoder-> _origsize);
|
||||||
} else {
|
} else {
|
||||||
byte *_compressedData = (byte *)malloc(_decoder->_compsize);
|
byte *_compressedData = (byte *)malloc(_decoder->_compsize);
|
||||||
_currArchive.read(_compressedData, _decoder->_compsize);
|
_currArchive.read(_compressedData, _decoder->_compsize);
|
||||||
|
@ -399,47 +389,47 @@ printf("Arj archive method %d, file '%s'\n", hdr->method, filename.c_str());
|
||||||
delete _decoder->_outstream;
|
delete _decoder->_outstream;
|
||||||
_decoder->_outstream = NULL;
|
_decoder->_outstream = NULL;
|
||||||
|
|
||||||
_uncompressed = new MemoryReadStream(_uncompressedData, _decoder->_origsize);
|
_uncompressed = new MemoryReadStream(uncompressedData, _decoder->_origsize, true);
|
||||||
|
assert(_uncompressed);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArjFile::close() {
|
void ArjFile::close() {
|
||||||
if (!_isOpen)
|
if (!_uncompressed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_isOpen = false;
|
|
||||||
|
|
||||||
if (_fallBack) {
|
if (_fallBack) {
|
||||||
_currArchive.close();
|
_currArchive.close();
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
delete _uncompressed;
|
delete _uncompressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
_uncompressed = NULL;
|
_uncompressed = NULL;
|
||||||
|
|
||||||
free(_uncompressedData);
|
|
||||||
_uncompressedData = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32 ArjFile::read(void *dataPtr, uint32 dataSize) {
|
uint32 ArjFile::read(void *dataPtr, uint32 dataSize) {
|
||||||
|
assert(_uncompressed);
|
||||||
return _uncompressed->read(dataPtr, dataSize);
|
return _uncompressed->read(dataPtr, dataSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ArjFile::eos() const {
|
bool ArjFile::eos() const {
|
||||||
|
assert(_uncompressed);
|
||||||
return _uncompressed->eos();
|
return _uncompressed->eos();
|
||||||
}
|
}
|
||||||
|
|
||||||
int32 ArjFile::pos() const {
|
int32 ArjFile::pos() const {
|
||||||
|
assert(_uncompressed);
|
||||||
return _uncompressed->pos();
|
return _uncompressed->pos();
|
||||||
}
|
}
|
||||||
|
|
||||||
int32 ArjFile::size() const {
|
int32 ArjFile::size() const {
|
||||||
|
assert(_uncompressed);
|
||||||
return _uncompressed->size();
|
return _uncompressed->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ArjFile::seek(int32 offset, int whence) {
|
bool ArjFile::seek(int32 offset, int whence) {
|
||||||
|
assert(_uncompressed);
|
||||||
return _uncompressed->seek(offset, whence);
|
return _uncompressed->seek(offset, whence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ public:
|
||||||
int32 pos() const;
|
int32 pos() const;
|
||||||
int32 size() const;
|
int32 size() const;
|
||||||
bool seek(int32 offset, int whence = SEEK_SET);
|
bool seek(int32 offset, int whence = SEEK_SET);
|
||||||
bool isOpen() { return _isOpen; }
|
bool isOpen() { return _uncompressed != 0; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _fallBack;
|
bool _fallBack;
|
||||||
|
@ -63,11 +63,8 @@ private:
|
||||||
ArjFilesMap _fileMap;
|
ArjFilesMap _fileMap;
|
||||||
StringMap _archMap;
|
StringMap _archMap;
|
||||||
|
|
||||||
byte *_uncompressedData;
|
|
||||||
SeekableReadStream *_uncompressed;
|
SeekableReadStream *_uncompressed;
|
||||||
|
|
||||||
bool _isOpen;
|
|
||||||
|
|
||||||
ArjDecoder *_decoder;
|
ArjDecoder *_decoder;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue