aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/exereader.cpp
blob: ce6bf184fb5968d4c722cb82f0c8e8b5d295391f (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/endian.h"

#include "sci/sci.h"
#include "sci/exereader.h"

namespace Sci {

int _bitCount;
uint16 _bits;

Common::Platform getGameExePlatform(Common::SeekableReadStream *exeStream) {
	byte magic[4];
	// Make sure that the executable is at least 4KB big
	if (exeStream->size() < 4096)
		return Common::kPlatformUnknown;

	// Read exe header
	exeStream->read(magic, 4);

	// Check if the header contains known magic bytes

	// Information obtained from http://magicdb.org/magic.db
	// Check if it's a DOS executable
	if (magic[0] == 'M' && magic[1] == 'Z') {
		return Common::kPlatformPC;
	}

	// Check if it's an Amiga executable
	if ((magic[2] == 0x03 && magic[3] == 0xF3) ||
		(magic[0] == 0x7F && magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F')) {
		return Common::kPlatformAmiga;
	}

	// Check if it's an Atari executable
	if ((magic[0] == 0x60 && magic[1] == 0x1A))
		return Common::kPlatformAtariST;

	// Check if it's a Mac exe

	// Resource map offset
	int32 offset = (int32)READ_BE_UINT32(magic);
	offset += 28;
	if (exeStream->size() <= offset)
		return Common::kPlatformUnknown;

	// Skip number of types in map
	exeStream->skip(2);
//	uint16 val = exeStream->readUint16BE() + 1;
	exeStream->skip(2);

	// Keep reading till we find the "CODE" bit
	while (!exeStream->eos()) {
		exeStream->skip(4);
		if (exeStream->eos())
			return Common::kPlatformUnknown;

		exeStream->read(magic, 4);
		if (exeStream->eos())
			return Common::kPlatformUnknown;

		if (!memcmp(magic, "CODE", 4)) {
			return Common::kPlatformMacintosh;
		}
		// Skip to the next list entry
		exeStream->skip(4);
		if (exeStream->eos())
			return Common::kPlatformUnknown;
	}

	// If we've reached here, the file type is unknown
	return Common::kPlatformUnknown;
}

bool isLZEXECompressed(Common::SeekableReadStream *exeStream) {
	uint32 filepos = 0;

	exeStream->seek(0, SEEK_SET);

	// First 2 bytes should be "MZ" (0x5A4D)
	if (exeStream->readUint16LE() != 0x5A4D)	// at pos 0, +2
		return false;

	exeStream->skip(6);

	// Header size should be 2
	filepos = exeStream->readUint16LE();
	if (filepos != 2)							// at pos 8, +2
		return false;

	exeStream->skip(12);

	// Calculate code segment offset in exe file
	filepos += exeStream->readUint16LE();		// at pos 22, +2
	filepos <<= 4;

	// First relocation item offset should be 0x1c
	if (exeStream->readUint16LE() != 0x1c)		// at pos 24, +2
		return false;

	// Number of overlays should be 0
	if (exeStream->readUint16LE() != 0)			// at pos 26, +2
		return false;

	// Look for LZEXE signature
	byte magic[4];
	exeStream->read(magic, 4);

	if (memcmp(magic, "LZ09", 4) && memcmp(magic, "LZ91", 4))
		return false;

	// Seek to offset 8 of info table at start of code segment
	exeStream->seek(filepos + 8, SEEK_SET);
	if (exeStream->err())
		return false;

	// Read size of compressed data in paragraphs
	uint16 size = exeStream->readUint16LE();

	// Move file pointer to start of compressed data
	filepos -= size << 4;
	exeStream->seek(filepos, SEEK_SET);
	if (exeStream->err())
		return false;

	// All conditions met, this is an LZEXE packed file
	// We are currently at the start of the compressed file data
	return true;
}

uint getBit(Common::SeekableReadStream *input) {
	uint bit = _bits & 1;
	_bitCount--;

	if (_bitCount <= 0) {
		_bits = input->readByte();
		_bits |= input->readByte() << 8;

		if (_bitCount == -1) { // special case for first bit word
			bit = _bits & 1;
			_bits >>= 1;
		}

		_bitCount += 16;
	} else
		_bits >>= 1;

	return bit;
}

Common::String readSciVersionFromExe(Common::SeekableReadStream *exeStream, Common::Platform platform) {
	int len = exeStream->size();
	unsigned char *buffer = NULL;

	// Read the executable
	bool isLZEXE = isLZEXECompressed(exeStream);

	if (!isLZEXE) {
		buffer = new unsigned char[exeStream->size()];

		exeStream->seek(0, SEEK_SET);
		exeStream->read(buffer, exeStream->size());
	} else {
		buffer = new unsigned char[exeStream->size() * 3];
		_bitCount = 0;

		// Skip LZEXE header
		exeStream->seek(32, SEEK_SET);

		int pos = 0;
		int repeat;
		short offset;

		while (1) {
			if (exeStream->ioFailed()) {
				warning("Error reading from input file");
				delete[] buffer;
				return NULL;
			}

			if (getBit(exeStream)) {
				buffer[pos++] = exeStream->readByte();
			} else {
				if (getBit(exeStream)) {
					byte tmp[2];
					exeStream->read(tmp, 2);
					repeat = (tmp[1] & 0x07);
					offset = ((tmp[1] & ~0x07) << 5) | tmp[0] | 0xE000;

					if (repeat == 0) {
						repeat = exeStream->readByte();

						if (repeat == 0) {
							len = pos;
							break;
						}
						else if (repeat == 1)
							continue;
						else
							repeat++;
					} else
						repeat += 2;
				} else {
					repeat = getBit(exeStream) << 1;
					repeat += getBit(exeStream) + 2;
					offset = exeStream->readByte() | 0xFF00;
				}

				while (repeat > 0) {
					buffer[pos] = buffer[pos + offset];
					pos++;
					repeat--;
				}
			}
		}
	}

	// Find SCI version number

	int state = 0;
	/* 'state' encodes how far we have matched the version pattern
	**   "n.nnn.nnn"
	**
	**   n.nnn.nnn
	**  0123456789
	**
	** Since we cannot be certain that the pattern does not begin with an
	** alphanumeric character, some states are ambiguous.
	** The pattern is expected to be terminated with a non-alphanumeric
	** character.
	*/


	int accept;
	unsigned char *buf = buffer;

	// String-encoded result, copied from buffer
	char currentString[10];

	for (int i = 0; i < len; i++) {
		unsigned char ch = *buf++;
		// By default, we don't like this character
		accept = 0;

		if (isalnum(ch)) {
			accept = (state != 1 && state != 5 && state != 9);
		} else if (ch == '.') {
			accept = (state == 1 || state == 5);
		} else if (state == 9) {
			// Terminate string
			currentString[9] = 0;

			// Return the current string
			return currentString;
		}

		if (accept)
			currentString[state++] = ch;
		else
			state = 0;
	}

	delete[] buffer;
	return "unknown";
}

} // End of namespace Sci