1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 /*
24 * This code is based on original Sfinx source code
25 * Copyright (c) 1994-1997 Janusz B. Wisniewski and L.K. Avalon
26 */
27
28 #include "common/system.h"
29 #include "common/str.h"
30 #include "common/debug.h"
31 #include "common/debug-channels.h"
32 #include "common/memstream.h"
33 #include "cge2/cge2.h"
34 #include "cge2/fileio.h"
35
36 namespace CGE2 {
37
38 /*-----------------------------------------------------------------------
39 * BtPage
40 *-----------------------------------------------------------------------*/
readBTree(Common::ReadStream & s)41 void BtPage::readBTree(Common::ReadStream &s) {
42 _header._count = s.readUint16LE();
43 _header._down = s.readUint16LE();
44
45 if (_header._down == kBtValNone) {
46 // Leaf list
47 for (int i = 0; i < kBtLeafCount; ++i) {
48 s.read(_leaf[i]._key, kBtKeySize);
49 _leaf[i]._pos = s.readUint32LE();
50 _leaf[i]._size = s.readUint32LE();
51 }
52 } else {
53 // Root index
54 for (int i = 0; i < kBtInnerCount; ++i) {
55 s.read(_inner[i]._key, kBtKeySize);
56 _inner[i]._down = s.readUint16LE();
57 }
58 }
59 }
60
61 /*-----------------------------------------------------------------------
62 * ResourceManager
63 *-----------------------------------------------------------------------*/
ResourceManager()64 ResourceManager::ResourceManager() {
65 _datFile = new Common::File();
66 _datFile->open(kDatName);
67
68 _catFile = new Common::File();
69 _catFile->open(kCatName);
70
71 if (!_datFile->isOpen() || !_catFile->isOpen())
72 error("Unable to open data files");
73
74 for (int i = 0; i < kBtLevel; i++) {
75 _buff[i]._page = new BtPage;
76 _buff[i]._pageNo = kBtValNone;
77 _buff[i]._index = -1;
78 assert(_buff[i]._page != nullptr);
79 }
80 }
81
~ResourceManager()82 ResourceManager::~ResourceManager() {
83 _datFile->close();
84 delete _datFile;
85
86 _catFile->close();
87 delete _catFile;
88
89 for (int i = 0; i < kBtLevel; i++)
90 delete _buff[i]._page;
91 }
92
xCrypt(byte * buf,uint16 length)93 void ResourceManager::xCrypt(byte *buf, uint16 length) {
94 byte *b = buf;
95
96 for (uint16 i = 0; i < length; i++)
97 *b++ ^= kCryptSeed;
98 }
99
seek(int32 offs,int whence)100 bool ResourceManager::seek(int32 offs, int whence) {
101 return _datFile->seek(offs, whence);
102 }
103
read(byte * buf,uint16 length)104 uint16 ResourceManager::read(byte *buf, uint16 length) {
105 if (!_datFile->isOpen())
106 return 0;
107
108 uint16 bytesRead = _datFile->read(buf, length);
109 if (!bytesRead)
110 error("Read %s - %d bytes", _datFile->getName(), length);
111 xCrypt(buf, length);
112 return bytesRead;
113 }
114
getPage(int level,uint16 pageId)115 BtPage *ResourceManager::getPage(int level, uint16 pageId) {
116 if (_buff[level]._pageNo != pageId) {
117 int32 pos = pageId * kBtSize;
118 _buff[level]._pageNo = pageId;
119
120 if (_catFile->size() <= pos)
121 return nullptr;
122
123 // In the original, there was a check verifying if the
124 // purpose was to write a new file. This should only be
125 // to create a new file, thus it was removed.
126 _catFile->seek(pageId * kBtSize, SEEK_SET);
127
128 // Read in the page
129 byte buffer[kBtSize];
130 int bytesRead = catRead(buffer, kBtSize);
131
132 // Unpack it into the page structure
133 Common::MemoryReadStream stream(buffer, bytesRead, DisposeAfterUse::NO);
134 _buff[level]._page->readBTree(stream);
135 _buff[level]._index = -1;
136 }
137 return _buff[level]._page;
138 }
139
find(const char * key)140 BtKeypack *ResourceManager::find(const char *key) {
141 int lev = 0;
142 uint16 nxt = kBtValRoot;
143 while (!_catFile->eos()) {
144 BtPage *pg = getPage(lev, nxt);
145 if (!pg)
146 return nullptr;
147
148 // search
149 if (pg->_header._down != kBtValNone) {
150 int i;
151 for (i = 0; i < pg->_header._count; i++) {
152 // Does this work, or does it have to compare the entire buffer?
153 if (scumm_strnicmp((const char *)key, (const char*)pg->_inner[i]._key, kBtKeySize) < 0)
154 break;
155 }
156 nxt = (i) ? pg->_inner[i - 1]._down : pg->_header._down;
157 _buff[lev]._index = i - 1;
158 lev++;
159 } else {
160 int i;
161 for (i = 0; i < pg->_header._count - 1; i++) {
162 if (scumm_stricmp((const char *)key, (const char *)pg->_leaf[i]._key) <= 0)
163 break;
164 }
165
166 // Hack to work around a mix between 24piram_ and 24pirami
167 if (!strcmp(key, "24piram_.SPR") && (scumm_stricmp((const char *)key, (const char *)pg->_leaf[i]._key) < 0))
168 ++i;
169 //
170
171 _buff[lev]._index = i;
172 return &pg->_leaf[i];
173 }
174 }
175 return nullptr;
176 }
177
exist(const char * name)178 bool ResourceManager::exist(const char *name) {
179 BtKeypack *result = find(name);
180 if (!result)
181 return false;
182
183 return scumm_stricmp(result->_key, name) == 0;
184 }
185
catRead(byte * buf,uint16 length)186 uint16 ResourceManager::catRead(byte *buf, uint16 length) {
187 if (!_catFile->isOpen())
188 return 0;
189
190 uint16 bytesRead = _catFile->read(buf, length);
191 if (!bytesRead)
192 error("Read %s - %d bytes", _catFile->getName(), length);
193 xCrypt(buf, length);
194 return bytesRead;
195 }
196
197 /*-----------------------------------------------------------------------
198 * EncryptedStream
199 *-----------------------------------------------------------------------*/
EncryptedStream(CGE2Engine * vm,const char * name)200 EncryptedStream::EncryptedStream(CGE2Engine *vm, const char *name) : _vm(vm), _lineCount(0) {
201 _error = false;
202 BtKeypack *kp = _vm->_resman->find(name);
203 if (scumm_stricmp(kp->_key, name) != 0)
204 _error = true;
205
206 _vm->_resman->seek(kp->_pos);
207 byte *dataBuffer;
208 int bufSize;
209
210 if ((strlen(name) > 4) && (scumm_stricmp(name + strlen(name) - 4, ".SPR") == 0)) {
211 // SPR files have some inconsistencies. Some have extra 0x1A at the end, some others
212 // do not have a carriage return at the end of the last line
213 // Therefore, we remove this ending 0x1A and add extra new lines.
214 // This fixes bug #6060
215 dataBuffer = (byte *)malloc(kp->_size + 2);
216 _vm->_resman->read(dataBuffer, kp->_size);
217 if (dataBuffer[kp->_size - 1] == 0x1A)
218 dataBuffer[kp->_size - 1] = '\n';
219 dataBuffer[kp->_size] = '\n';
220 dataBuffer[kp->_size + 1] = '\n';
221 bufSize = kp->_size + 2;
222 } else {
223 dataBuffer = (byte *)malloc(kp->_size);
224 _vm->_resman->read(dataBuffer, kp->_size);
225 bufSize = kp->_size;
226 }
227
228 _readStream = new Common::MemoryReadStream(dataBuffer, bufSize, DisposeAfterUse::YES);
229 }
230
read(byte * dataPtr,uint32 dataSize)231 uint32 EncryptedStream::read(byte *dataPtr, uint32 dataSize) {
232 return _readStream->read(dataPtr, dataSize);
233 }
234
readSint16LE()235 int16 EncryptedStream::readSint16LE() {
236 return _readStream->readSint16LE();
237 }
238
readUint32LE()239 uint32 EncryptedStream::readUint32LE() {
240 return _readStream->readUint32LE();
241 }
242
err()243 bool EncryptedStream::err() {
244 return (_error || _readStream->err());
245 }
246
eos()247 bool EncryptedStream::eos() {
248 return _readStream->eos();
249 }
250
seek(int32 offset)251 bool EncryptedStream::seek(int32 offset) {
252 return _readStream->seek(offset);
253 }
254
readLine()255 Common::String EncryptedStream::readLine() {
256 _lineCount++;
257 Common::String line = _readStream->readLine();
258 if (!line.empty() && (line[0] == ';' || line[0] == '.' || line[0] == '*'))
259 line.clear(); // Returns an empty string, if the line is invalid.
260 return line;
261 }
262
size()263 int32 EncryptedStream::size() {
264 return _readStream->size();
265 }
266
pos()267 int32 EncryptedStream::pos() {
268 return _readStream->pos();
269 }
270
~EncryptedStream()271 EncryptedStream::~EncryptedStream() {
272 delete _readStream;
273 }
274
275 const char *EncryptedStream::kIdTab[] = {
276 "[near]", "[mtake]", "[ftake]", "[phase]", "[seq]",
277 "Name", "Type", "Front", "East",
278 "Portable", "Transparent",
279 nullptr
280 };
281
282 } // End of namespace CGE2
283