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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/* 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.
*
*/
/*
* This code is based on original Sfinx source code
* Copyright (c) 1994-1997 Janus B. Wisniewski and L.K. Avalon
*/
#include "common/system.h"
#include "common/str.h"
#include "common/debug.h"
#include "common/debug-channels.h"
#include "common/memstream.h"
#include "cge2/cge2.h"
#include "cge2/fileio.h"
namespace CGE2 {
/*-----------------------------------------------------------------------
* BtPage
*-----------------------------------------------------------------------*/
void BtPage::readBTree(Common::ReadStream &s) {
_header._count = s.readUint16LE();
_header._down = s.readUint16LE();
if (_header._down == kBtValNone) {
// Leaf list
for (int i = 0; i < kBtLeafCount; ++i) {
s.read(_leaf[i]._key, kBtKeySize);
_leaf[i]._pos = s.readUint32LE();
_leaf[i]._size = s.readUint32LE();
}
} else {
// Root index
for (int i = 0; i < kBtInnerCount; ++i) {
s.read(_inner[i]._key, kBtKeySize);
_inner[i]._down = s.readUint16LE();
}
}
}
/*-----------------------------------------------------------------------
* ResourceManager
*-----------------------------------------------------------------------*/
ResourceManager::ResourceManager() {
_datFile = new Common::File();
_datFile->open(kDatName);
_catFile = new Common::File();
_catFile->open(kCatName);
if (!_datFile->isOpen() || !_catFile->isOpen())
error("Unable to open data files");
for (int i = 0; i < kBtLevel; i++) {
_buff[i]._page = new BtPage;
_buff[i]._pageNo = kBtValNone;
_buff[i]._index = -1;
assert(_buff[i]._page != nullptr);
}
}
ResourceManager::~ResourceManager() {
_datFile->close();
delete _datFile;
_catFile->close();
delete _catFile;
for (int i = 0; i < kBtLevel; i++)
delete _buff[i]._page;
}
void ResourceManager::xCrypt(byte *buf, uint16 length) {
byte *b = buf;
for (uint16 i = 0; i < length; i++)
*b++ ^= kCryptSeed;
}
bool ResourceManager::seek(int32 offs, int whence) {
return _datFile->seek(offs, whence);
}
uint16 ResourceManager::read(byte *buf, uint16 length) {
if (!_datFile->isOpen())
return 0;
uint16 bytesRead = _datFile->read(buf, length);
if (!bytesRead)
error("Read %s - %d bytes", _datFile->getName(), length);
xCrypt(buf, length);
return bytesRead;
}
BtPage *ResourceManager::getPage(int level, uint16 pageId) {
if (_buff[level]._pageNo != pageId) {
int32 pos = pageId * kBtSize;
_buff[level]._pageNo = pageId;
assert(_catFile->size() > pos);
// In the original, there was a check verifying if the
// purpose was to write a new file. This should only be
// to create a new file, thus it was removed.
_catFile->seek(pageId * kBtSize, SEEK_SET);
// Read in the page
byte buffer[kBtSize];
int bytesRead = catRead(buffer, kBtSize);
// Unpack it into the page structure
Common::MemoryReadStream stream(buffer, bytesRead, DisposeAfterUse::NO);
_buff[level]._page->readBTree(stream);
_buff[level]._index = -1;
}
return _buff[level]._page;
}
BtKeypack *ResourceManager::find(const char *key) {
int lev = 0;
uint16 nxt = kBtValRoot;
while (!_catFile->eos()) {
BtPage *pg = getPage(lev, nxt);
// search
if (pg->_header._down != kBtValNone) {
int i;
for (i = 0; i < pg->_header._count; i++) {
// Does this work, or does it have to compare the entire buffer?
if (scumm_strnicmp((const char *)key, (const char*)pg->_inner[i]._key, kBtKeySize) < 0)
break;
}
nxt = (i) ? pg->_inner[i - 1]._down : pg->_header._down;
_buff[lev]._index = i - 1;
lev++;
} else {
int i;
for (i = 0; i < pg->_header._count - 1; i++) {
if (scumm_stricmp((const char *)key, (const char *)pg->_leaf[i]._key) <= 0)
break;
}
// Hack to work around a mix between 24piram_ and 24pirami
if (!strcmp(key, "24piram_.SPR") && (scumm_stricmp((const char *)key, (const char *)pg->_leaf[i]._key) < 0))
++i;
//
_buff[lev]._index = i;
return &pg->_leaf[i];
}
}
return nullptr;
}
bool ResourceManager::exist(const char *name) {
return scumm_stricmp(find(name)->_key, name) == 0;
}
uint16 ResourceManager::catRead(byte *buf, uint16 length) {
if (!_catFile->isOpen())
return 0;
uint16 bytesRead = _catFile->read(buf, length);
if (!bytesRead)
error("Read %s - %d bytes", _catFile->getName(), length);
xCrypt(buf, length);
return bytesRead;
}
/*-----------------------------------------------------------------------
* EncryptedStream
*-----------------------------------------------------------------------*/
EncryptedStream::EncryptedStream(CGE2Engine *vm, const char *name) : _vm(vm), _lineCount(0) {
_error = false;
BtKeypack *kp = _vm->_resman->find(name);
if (scumm_stricmp(kp->_key, name) != 0)
_error = true;
_vm->_resman->seek(kp->_pos);
byte *dataBuffer;
int bufSize;
if ((strlen(name) > 4) && (scumm_stricmp(name + strlen(name) - 4, ".SPR") == 0)) {
// SPR files have some inconsistencies. Some have extra 0x1A at the end, some others
// do not have a carriage return at the end of the last line
// Therefore, we remove this ending 0x1A and add extra new lines.
// This fixes bug #3537527
dataBuffer = (byte *)malloc(kp->_size + 2);
_vm->_resman->read(dataBuffer, kp->_size);
if (dataBuffer[kp->_size - 1] == 0x1A)
dataBuffer[kp->_size - 1] = '\n';
dataBuffer[kp->_size] = '\n';
dataBuffer[kp->_size + 1] = '\n';
bufSize = kp->_size + 2;
} else {
dataBuffer = (byte *)malloc(kp->_size);
_vm->_resman->read(dataBuffer, kp->_size);
bufSize = kp->_size;
}
_readStream = new Common::MemoryReadStream(dataBuffer, bufSize, DisposeAfterUse::YES);
}
uint32 EncryptedStream::read(byte *dataPtr, uint32 dataSize) {
return _readStream->read(dataPtr, dataSize);
}
int16 EncryptedStream::readSint16LE() {
return _readStream->readSint16LE();
}
uint32 EncryptedStream::readUint32LE() {
return _readStream->readUint32LE();
}
bool EncryptedStream::err() {
return (_error || _readStream->err());
}
bool EncryptedStream::eos() {
return _readStream->eos();
}
bool EncryptedStream::seek(int32 offset) {
return _readStream->seek(offset);
}
Common::String EncryptedStream::readLine() {
_lineCount++;
Common::String line = _readStream->readLine();
if (!line.empty() && (line[0] == ';' || line[0] == '.' || line[0] == '*'))
line.clear(); // Returns an empty string, if the line is invalid.
return line;
}
int32 EncryptedStream::size() {
return _readStream->size();
}
int32 EncryptedStream::pos() {
return _readStream->pos();
}
EncryptedStream::~EncryptedStream() {
delete _readStream;
}
const char *EncryptedStream::kIdTab[] = {
"[near]", "[mtake]", "[ftake]", "[phase]", "[seq]",
"Name", "Type", "Front", "East",
"Portable", "Transparent",
nullptr
};
} // End of namespace CGE2
|