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
|
/* 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/>.
*
*/
#include "prince/archive.h"
#include "prince/decompress.h"
#include "common/stream.h"
#include "common/debug.h"
#include "common/memstream.h"
namespace Prince {
PtcArchive::PtcArchive() : _stream(nullptr) {
}
PtcArchive::~PtcArchive() {
close();
}
static void decrypt(byte *buffer, uint32 size) {
uint32 key = 0xDEADF00D;
while (size--) {
*buffer++ += key & 0xFF;
key ^= 0x2E84299A;
key += MKTAG('B', 'L', 'A', 'H');
key = ((key & 1) << 31) | (key >> 1);
}
}
bool PtcArchive::open(const Common::Path &filename) {
_stream = SearchMan.createReadStreamForMember(filename);
if (!_stream)
return false;
_stream->readUint32LE(); // magic
uint32 fileTableOffset = _stream->readUint32LE() ^ 0x4D4F4B2D; // MOK-
uint32 fileTableSize = _stream->readUint32LE() ^ 0x534F4654; // SOFT
debug(8, "fileTableOffset : %08X", fileTableOffset);
debug(8, "fileTableSize: %08X", fileTableSize);
_stream->seek(fileTableOffset);
byte *fileTable = (byte *)malloc(fileTableSize);
byte *fileTableEnd = fileTable + fileTableSize;
_stream->read(fileTable, fileTableSize);
decrypt(fileTable, fileTableSize);
for (byte *fileItem = fileTable; fileItem < fileTableEnd; fileItem += 32) {
FileEntry item;
Common::String name = (const char*)fileItem;
item._offset = READ_LE_UINT32(fileItem + 24);
item._size = READ_LE_UINT32(fileItem + 28);
debug(8, "%12s %8X %d", name.c_str(), item._offset, item._size);
_items[name] = item;
}
free(fileTable);
return true;
}
bool PtcArchive::openTranslation(const Common::Path &filename) {
_stream = SearchMan.createReadStreamForMember(filename);
if (!_stream)
return false;
Common::Array<Common::String> translationNames;
Common::String translationFileName;
const int kTranslationFiles = 5;
for (int i = 0; i < kTranslationFiles; i++) {
translationFileName = _stream->readLine();
translationNames.push_back(translationFileName);
}
FileEntry item;
for (int i = 0; i < kTranslationFiles; i++) {
item._offset = _stream->readUint32LE();
item._size = _stream->readUint32LE();
_items[translationNames[i]] = item;
}
if ((int32)_items[translationNames[0]]._offset == _stream->pos()) {
warning("v0 translation file detected, update is needed");
return true;
}
// We have latter versions of the file
if (_stream->readByte() != '\n') {
error("Malformed prince_translation.dat file");
}
Common::String version = _stream->readLine();
Common::String stamp = _stream->readLine();
warning("%s translation file detected, built on %s", version.c_str(), stamp.c_str());
if (version.equals("v1.0")) {
// No more data, we all fine
return true;
}
// Here we have format extension data
return true;
}
void PtcArchive::close() {
delete _stream;
_stream = nullptr;
_items.clear();
}
bool PtcArchive::hasFile(const Common::Path &path) const {
Common::String name = path.toString();
// TODO: check if path matching should be added
return _items.contains(name);
}
int PtcArchive::listMembers(Common::ArchiveMemberList &list) const {
int matches = 0;
for (FileMap::const_iterator it = _items.begin(); it != _items.end(); ++it) {
list.push_back(Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(it->_key, *this)));
matches++;
}
return matches;
}
const Common::ArchiveMemberPtr PtcArchive::getMember(const Common::Path &path) const {
if (!hasFile(path)) {
Common::ArchiveMemberPtr();
}
return Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(path, *this));
}
Common::SeekableReadStream *PtcArchive::createReadStreamForMember(const Common::Path &path) const {
Common::String name = path.toString();
if (!_items.contains(name)) {
return nullptr;
}
debug(8, "PtcArchive::createReadStreamForMember(%s)", name.c_str());
const FileEntry &entryHeader = _items[name];
if (entryHeader._size < 4)
return nullptr;
uint32 size = entryHeader._size;
_stream->seek(entryHeader._offset);
// This *HAS* to be malloc (not new[]) because MemoryReadStream uses free() to free the memory
byte *buffer = (byte *)malloc(size);
_stream->read(buffer, size);
if (READ_BE_UINT32(buffer) == MKTAG('M', 'A', 'S', 'M')) {
Decompressor dec;
uint32 decompLen = READ_BE_UINT32(buffer + 14);
byte *decompData = (byte *)malloc(decompLen);
dec.decompress(buffer + 18, decompData, decompLen);
free(buffer);
size = decompLen;
buffer = decompData;
debug(8, "PtcArchive::createReadStreamForMember: decompressed %d to %d bytes", entryHeader._size, decompLen);
}
return new Common::MemoryReadStream(buffer, size, DisposeAfterUse::YES);
}
} // End of namespace Prince
|