aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/dataio.cpp
blob: 1633a1edb7a2049807ce7f7941d2a5a50e0ecfb8 (plain)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* 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.
 *
 */

#include "common/endian.h"
#include "common/types.h"
#include "common/memstream.h"
#include "common/substream.h"

#include "gob/gob.h"
#include "gob/dataio.h"
#include "gob/global.h"
#include "gob/util.h"

namespace Gob {

DataIO::File::File() : size(0), offset(0), compression(0), archive(0) {
}

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);

	assert(size > 0);

	byte *data = 0;
	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 0;

	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[4114];
	assert(tmpBuf);

	uint32 counter = size;

	for (int i = 0; i < 4078; i++)
		tmpBuf[i] = 0x20;
	uint16 tmpIndex = 4078;

	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);
			byte  len =         (tmp2 & 0x0F) + 3;

			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 = 0;
	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(name);
	if (!*archive)
		return false;

	(*archive)->base = base;
	return true;
}

DataIO::Archive *DataIO::openArchive(const Common::String &name) {
	Archive *archive = new Archive;
	if (!archive->file.open(name)) {
		delete archive;
		return 0;
	}

	archive->name = name;

	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
		Util::replaceChar(fileName, (char) 0x85, 'E');
		Util::replaceChar(fileName, (char) 0x8A, 'K');
		Util::replaceChar(fileName, (char) 0x8E, 'O');
		Util::replaceChar(fileName, (char) 0x91, 'C');
		Util::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(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(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(name))
		return 0;

	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(name))
		return 0;

	size = f.size();

	byte *data = new byte[size];
	if (f.read(data, size) != ((uint32) size)) {
		delete[] data;
		return 0;
	}

	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 0;
}

Common::SeekableReadStream *DataIO::getFile(File &file) {
	if (!file.archive)
		return 0;

	if (!file.archive->file.isOpen())
		return 0;

	if (!file.archive->file.seek(file.offset))
		return 0;

	Common::SeekableReadStream *rawData =
		new Common::SafeSubReadStream(&file.archive->file, file.offset, file.offset + file.size);

	if (file.compression == 0)
		return rawData;

	Common::SeekableReadStream *unpackedData = unpack(*rawData, file.compression);

	delete rawData;

	return unpackedData;
}

byte *DataIO::getFile(File &file, int32 &size) {
	if (!file.archive)
		return 0;

	if (!file.archive->file.isOpen())
		return 0;

	if (!file.archive->file.seek(file.offset))
		return 0;

	size = file.size;

	byte *rawData = new byte[file.size];
	if (file.archive->file.read(rawData, file.size) != file.size) {
		delete[] rawData;
		return 0;
	}

	if (file.compression == 0)
		return rawData;

	byte *unpackedData = unpack(rawData, file.size, size, file.compression);

	delete[] rawData;

	return unpackedData;
}

} // End of namespace Gob