Initial commit
This commit is contained in:
490
engines/gob/dataio.cpp
Normal file
490
engines/gob/dataio.cpp
Normal file
@@ -0,0 +1,490 @@
|
||||
/* 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/>.
|
||||
*
|
||||
*
|
||||
* This file is dual-licensed.
|
||||
* In addition to the GPLv3 license mentioned above, this code is also
|
||||
* licensed under LGPL 2.1. See LICENSES/COPYING.LGPL file for the
|
||||
* full text of the license.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common/endian.h"
|
||||
#include "common/types.h"
|
||||
#include "common/bufferedstream.h"
|
||||
#include "common/memstream.h"
|
||||
#include "common/substream.h"
|
||||
|
||||
#include "gob/dataio.h"
|
||||
|
||||
namespace Gob {
|
||||
|
||||
DataIO::File::File() : size(0), offset(0), compression(0), archive(nullptr) {
|
||||
}
|
||||
|
||||
DataIO::File::File(const Common::String &n, uint32 s, uint32 o, uint8 c, Archive &a) :
|
||||
name(n), size(s), offset(o), compression(c), archive(&a) {
|
||||
}
|
||||
|
||||
|
||||
DataIO::DataIO() {
|
||||
// Reserve memory for the standard max amount of archives
|
||||
_archives.reserve(kMaxArchives);
|
||||
for (int i = 0; i < kMaxArchives; i++)
|
||||
_archives.push_back(0);
|
||||
}
|
||||
|
||||
DataIO::~DataIO() {
|
||||
// Close all archives
|
||||
for (Common::Array<Archive *>::iterator it = _archives.begin(); it != _archives.end(); ++it) {
|
||||
if (!*it)
|
||||
continue;
|
||||
|
||||
closeArchive(**it);
|
||||
delete *it;
|
||||
}
|
||||
}
|
||||
|
||||
void DataIO::getArchiveInfo(Common::Array<ArchiveInfo> &info) const {
|
||||
info.resize(_archives.size());
|
||||
|
||||
for (uint i = 0; i < _archives.size(); i++) {
|
||||
if (!_archives[i])
|
||||
continue;
|
||||
|
||||
info[i].name = _archives[i]->name;
|
||||
info[i].base = _archives[i]->base;
|
||||
info[i].fileCount = _archives[i]->files.size();
|
||||
}
|
||||
}
|
||||
|
||||
uint32 DataIO::getSizeChunks(Common::SeekableReadStream &src) {
|
||||
uint32 size = 0;
|
||||
|
||||
uint32 chunkSize = 2, realSize;
|
||||
while (chunkSize != 0xFFFF) {
|
||||
src.skip(chunkSize - 2);
|
||||
|
||||
chunkSize = src.readUint16LE();
|
||||
realSize = src.readUint16LE();
|
||||
|
||||
assert(chunkSize >= 4);
|
||||
|
||||
size += realSize;
|
||||
}
|
||||
|
||||
assert(!src.eos());
|
||||
|
||||
src.seek(0);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
byte *DataIO::unpack(Common::SeekableReadStream &src, int32 &size, uint8 compression, bool useMalloc) {
|
||||
assert((compression == 1) || (compression == 2));
|
||||
|
||||
if (compression == 1)
|
||||
size = src.readUint32LE();
|
||||
else if (compression == 2)
|
||||
size = getSizeChunks(src);
|
||||
else
|
||||
size = 0;
|
||||
|
||||
assert(size > 0);
|
||||
|
||||
byte *data = nullptr;
|
||||
if (useMalloc)
|
||||
data = (byte *) malloc(size);
|
||||
else
|
||||
data = new byte[size];
|
||||
|
||||
if (compression == 1)
|
||||
unpackChunk(src, data, size);
|
||||
else if (compression == 2)
|
||||
unpackChunks(src, data, size);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
byte *DataIO::unpack(const byte *src, uint32 srcSize, int32 &size, uint8 compression) {
|
||||
Common::MemoryReadStream srcStream(src, srcSize);
|
||||
|
||||
return unpack(srcStream, size, compression, false);
|
||||
}
|
||||
|
||||
Common::SeekableReadStream *DataIO::unpack(Common::SeekableReadStream &src, uint8 compression) {
|
||||
int32 size;
|
||||
|
||||
byte *data = unpack(src, size, compression, true);
|
||||
if (!data)
|
||||
return nullptr;
|
||||
|
||||
return new Common::MemoryReadStream(data, size, DisposeAfterUse::YES);
|
||||
}
|
||||
|
||||
void DataIO::unpackChunks(Common::SeekableReadStream &src, byte *dest, uint32 size) {
|
||||
uint32 chunkSize = 0, realSize;
|
||||
while (chunkSize != 0xFFFF) {
|
||||
uint32 pos = src.pos();
|
||||
|
||||
chunkSize = src.readUint16LE();
|
||||
realSize = src.readUint16LE();
|
||||
|
||||
assert(chunkSize >= 4);
|
||||
assert(size >= realSize);
|
||||
|
||||
src.skip(2);
|
||||
|
||||
unpackChunk(src, dest, realSize);
|
||||
|
||||
if (chunkSize != 0xFFFF)
|
||||
src.seek(pos + chunkSize + 2);
|
||||
|
||||
size -= realSize;
|
||||
dest += realSize;
|
||||
}
|
||||
}
|
||||
|
||||
void DataIO::unpackChunk(Common::SeekableReadStream &src, byte *dest, uint32 size) {
|
||||
byte *tmpBuf = new byte[4370]; // 4096 + (256 + 18) = 4096 + (max string length)
|
||||
assert(tmpBuf);
|
||||
|
||||
uint32 counter = size;
|
||||
|
||||
uint16 magic1 = src.readUint16LE();
|
||||
uint16 magic2 = src.readUint16LE();
|
||||
|
||||
int16 tmpIndex, extendedLenCmd;
|
||||
if ((magic1 == 0x1234) && (magic2 == 0x5678)) {
|
||||
// Extended format allowing to copy larger strings
|
||||
// from the window (up to 256 + 18 = 274 bytes).
|
||||
extendedLenCmd = 18;
|
||||
tmpIndex = 273;
|
||||
} else {
|
||||
// Standard format allowing to copy short strings
|
||||
// (up to 18 bytes) from the window.
|
||||
extendedLenCmd = 100; // Cannot be matched
|
||||
tmpIndex = 4078;
|
||||
src.seek(-4, SEEK_CUR);
|
||||
}
|
||||
|
||||
memset(tmpBuf, 0x20, tmpIndex); // Fill initial window with spaces
|
||||
|
||||
|
||||
uint16 cmd = 0;
|
||||
while (1) {
|
||||
cmd >>= 1;
|
||||
if ((cmd & 0x0100) == 0)
|
||||
cmd = src.readByte() | 0xFF00;
|
||||
|
||||
if ((cmd & 1) != 0) { /* copy */
|
||||
byte tmp = src.readByte();
|
||||
|
||||
*dest++ = tmp;
|
||||
tmpBuf[tmpIndex] = tmp;
|
||||
|
||||
tmpIndex++;
|
||||
tmpIndex %= 4096;
|
||||
counter--;
|
||||
if (counter == 0)
|
||||
break;
|
||||
} else { /* copy string */
|
||||
byte tmp1 = src.readByte();
|
||||
byte tmp2 = src.readByte();
|
||||
|
||||
int16 off = tmp1 | ((tmp2 & 0xF0) << 4);
|
||||
int16 len = (tmp2 & 0x0F) + 3;
|
||||
|
||||
if (len == extendedLenCmd)
|
||||
len = src.readByte() + 18;
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
*dest++ = tmpBuf[(off + i) % 4096];
|
||||
counter--;
|
||||
if (counter == 0) {
|
||||
delete[] tmpBuf;
|
||||
return;
|
||||
}
|
||||
tmpBuf[tmpIndex] = tmpBuf[(off + i) % 4096];
|
||||
tmpIndex++;
|
||||
tmpIndex %= 4096;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delete[] tmpBuf;
|
||||
}
|
||||
|
||||
bool DataIO::openArchive(Common::String name, bool base) {
|
||||
// Look for a free archive slot
|
||||
Archive **archive = nullptr;
|
||||
int i = 0;
|
||||
for (Common::Array<Archive *>::iterator it = _archives.begin(); it != _archives.end(); ++it, i++) {
|
||||
if (!*it) {
|
||||
archive = &*it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!archive) {
|
||||
// No free slot, create a new one
|
||||
|
||||
warning("DataIO::openArchive(): Need to increase archive count to %d", _archives.size() + 1);
|
||||
_archives.push_back(0);
|
||||
|
||||
Common::Array<Archive *>::iterator it = _archives.end();
|
||||
archive = &*(--it);
|
||||
}
|
||||
|
||||
// Add extension if necessary
|
||||
if (!name.contains('.'))
|
||||
name += ".stk";
|
||||
|
||||
// Try to open
|
||||
*archive = openArchive(Common::Path(name));
|
||||
if (!*archive)
|
||||
return false;
|
||||
|
||||
(*archive)->base = base;
|
||||
return true;
|
||||
}
|
||||
|
||||
// A copy of replaceChar utility function from util.cpp
|
||||
static void replaceChar(char *str, char c1, char c2) {
|
||||
while ((str = strchr(str, c1)))
|
||||
*str = c2;
|
||||
}
|
||||
|
||||
DataIO::Archive *DataIO::openArchive(const Common::Path &name) {
|
||||
Archive *archive = new Archive;
|
||||
if (!archive->file.open(name)) {
|
||||
delete archive;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
archive->name = name.toString('/');
|
||||
|
||||
uint16 fileCount = archive->file.readUint16LE();
|
||||
for (uint16 i = 0; i < fileCount; i++) {
|
||||
File file;
|
||||
|
||||
char fileName[14];
|
||||
|
||||
archive->file.read(fileName, 13);
|
||||
fileName[13] = '\0';
|
||||
|
||||
file.size = archive->file.readUint32LE();
|
||||
file.offset = archive->file.readUint32LE();
|
||||
file.compression = archive->file.readByte() != 0;
|
||||
|
||||
// Replacing cyrillic characters
|
||||
replaceChar(fileName, (char) 0x85, 'E');
|
||||
replaceChar(fileName, (char) 0x8A, 'K');
|
||||
replaceChar(fileName, (char) 0x8E, 'O');
|
||||
replaceChar(fileName, (char) 0x91, 'C');
|
||||
replaceChar(fileName, (char) 0x92, 'T');
|
||||
|
||||
file.name = fileName;
|
||||
|
||||
// Geisha use 0ot files, which are compressed TOT files without the packed byte set.
|
||||
if (file.name.hasSuffix(".0OT")) {
|
||||
file.name.setChar('T', file.name.size() - 3);
|
||||
file.compression = 2;
|
||||
}
|
||||
|
||||
file.archive = archive;
|
||||
archive->files.setVal(file.name, file);
|
||||
}
|
||||
|
||||
return archive;
|
||||
}
|
||||
|
||||
bool DataIO::closeArchive(bool base) {
|
||||
// Look for a matching archive and close it
|
||||
for (int archive = _archives.size() - 1; archive >= 0; archive--) {
|
||||
if (_archives[archive] && (_archives[archive]->base == base)) {
|
||||
closeArchive(*_archives[archive]);
|
||||
delete _archives[archive];
|
||||
_archives[archive] = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DataIO::closeArchive(Archive &archive) {
|
||||
archive.file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DataIO::hasFile(const Common::String &name){
|
||||
// Look up the files in the opened archives
|
||||
if (findFile(name))
|
||||
return true;
|
||||
|
||||
// Else, look if a plain file that matches exists
|
||||
return Common::File::exists(Common::Path(name));
|
||||
}
|
||||
|
||||
int32 DataIO::fileSize(const Common::String &name) {
|
||||
// Try to find the file in the archives
|
||||
File *file = findFile(name);
|
||||
if (file) {
|
||||
if (file->compression == 0)
|
||||
return file->size;
|
||||
|
||||
// Sanity checks
|
||||
assert(file->size >= 4);
|
||||
assert(file->archive);
|
||||
assert(file->archive->file.isOpen());
|
||||
|
||||
// Read the full, unpacked size
|
||||
file->archive->file.seek(file->offset);
|
||||
|
||||
if (file->compression == 2)
|
||||
file->archive->file.skip(4);
|
||||
|
||||
return file->archive->file.readUint32LE();
|
||||
}
|
||||
|
||||
// Else, try to find a matching plain file
|
||||
Common::File f;
|
||||
if (!f.open(Common::Path(name)))
|
||||
return -1;
|
||||
|
||||
return f.size();
|
||||
}
|
||||
|
||||
Common::SeekableReadStream *DataIO::getFile(const Common::String &name) {
|
||||
// Try to open the file in the archives
|
||||
File *file = findFile(name);
|
||||
if (file) {
|
||||
Common::SeekableReadStream *data = getFile(*file);
|
||||
if (data)
|
||||
return data;
|
||||
}
|
||||
|
||||
// Else, try to open a matching plain file
|
||||
Common::File f;
|
||||
if (!f.open(Common::Path(name)))
|
||||
return nullptr;
|
||||
|
||||
return f.readStream(f.size());
|
||||
}
|
||||
|
||||
byte *DataIO::getFile(const Common::String &name, int32 &size) {
|
||||
// Try to open the file in the archives
|
||||
File *file = findFile(name);
|
||||
if (file) {
|
||||
byte *data = getFile(*file, size);
|
||||
if (data)
|
||||
return data;
|
||||
}
|
||||
|
||||
// Else, try to open a matching plain file
|
||||
Common::File f;
|
||||
if (!f.open(Common::Path(name)))
|
||||
return nullptr;
|
||||
|
||||
size = f.size();
|
||||
|
||||
byte *data = new byte[size];
|
||||
if (f.read(data, size) != ((uint32) size)) {
|
||||
delete[] data;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
DataIO::File *DataIO::findFile(const Common::String &name) {
|
||||
for (int i = _archives.size() - 1; i >= 0; i--) {
|
||||
Archive *archive = _archives[i];
|
||||
if (!archive)
|
||||
// Empty slot
|
||||
continue;
|
||||
|
||||
// Look up the file in the file map
|
||||
FileMap::iterator file = archive->files.find(name);
|
||||
if (file != archive->files.end())
|
||||
return &file->_value;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Common::SeekableReadStream *DataIO::getFile(File &file) {
|
||||
if (!file.archive)
|
||||
return nullptr;
|
||||
|
||||
if (!file.archive->file.isOpen())
|
||||
return nullptr;
|
||||
|
||||
if (!file.archive->file.seek(file.offset))
|
||||
return nullptr;
|
||||
|
||||
Common::SeekableReadStream *rawData =
|
||||
new Common::SafeSeekableSubReadStream(&file.archive->file, file.offset, file.offset + file.size);
|
||||
|
||||
Common::SeekableReadStream *bufferedRawData =
|
||||
Common::wrapBufferedSeekableReadStream(rawData, 4096, DisposeAfterUse::YES);
|
||||
|
||||
if (file.compression == 0)
|
||||
return bufferedRawData;
|
||||
|
||||
Common::SeekableReadStream *unpackedData = unpack(*bufferedRawData, file.compression);
|
||||
|
||||
delete bufferedRawData;
|
||||
|
||||
return unpackedData;
|
||||
}
|
||||
|
||||
byte *DataIO::getFile(File &file, int32 &size) {
|
||||
if (!file.archive)
|
||||
return nullptr;
|
||||
|
||||
if (!file.archive->file.isOpen())
|
||||
return nullptr;
|
||||
|
||||
if (!file.archive->file.seek(file.offset))
|
||||
return nullptr;
|
||||
|
||||
size = file.size;
|
||||
|
||||
byte *rawData = new byte[file.size];
|
||||
if (file.archive->file.read(rawData, file.size) != file.size) {
|
||||
delete[] rawData;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (file.compression == 0)
|
||||
return rawData;
|
||||
|
||||
byte *unpackedData = unpack(rawData, file.size, size, file.compression);
|
||||
|
||||
delete[] rawData;
|
||||
|
||||
return unpackedData;
|
||||
}
|
||||
|
||||
} // End of namespace Gob
|
||||
Reference in New Issue
Block a user