aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/scicore/versions.cpp
blob: 06a1c1b431179a518bd783ae3ad13f6d9cf18ce5 (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
/* 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$
 *
 */

#define NEED_SCI_VERSIONS
#include "sci/include/versions.h"
#include "sci/include/engine.h"
#include "sci/include/resource.h"
#include "sci/scicore/games.h"
#include "sci/scicore/exe.h"

/* Maxmimum number of bytes to hash from start of file */
#define VERSION_DETECT_HASH_SIZE 1000000

#define VERSION_DETECT_BUF_SIZE 4096

static int
scan_file(char *filename, sci_version_t *version) {
	char buf[VERSION_DETECT_BUF_SIZE];
	char result_string[10]; /* string-encoded result, copied from buf */
	int characters_left;
	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.
	*/

	exe_file_t *f = exe_open(filename);

	if (!f)
		return 1;

	do {
		int i;
		int accept;

		characters_left = exe_read(f, buf, VERSION_DETECT_BUF_SIZE);

		for (i = 0; i < characters_left; i++) {
			const char ch = buf[i];
			accept = 0; /* By default, we don't like this character */

			if (isalnum((unsigned char) ch)) {
				accept = (state != 1
				          && state != 5
				          && state != 9);
			} else if (ch == '.') {
				accept = (state == 1
				          || state == 5);
			} else if (state == 9) {
				result_string[9] = 0; /* terminate string */

				if (!version_parse(result_string, version)) {
					exe_close(f);
					return 0; /* success! */
				}

				/* Continue searching. */
			}

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

		}

	} while (characters_left == VERSION_DETECT_BUF_SIZE);

	exe_close(f);
	return 1; /* failure */
}

static guint32
read_uint32(byte *data) {
	return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
}

static guint16
read_uint16(byte *data) {
	return (data[0] << 8) | data[1];
}

static int
is_mac_exe(char *filename) {
	FILE *file;
	byte buf[4];
	guint32 val;
	unsigned int i;

	/* Mac executables have no extension */
	if (strchr(filename, '.'))
		return 0;

	file = fopen(filename, "rb");
	if (!file)
		return 0;

	if (fseek(file, 4, SEEK_SET) == -1) {
		fclose(file);
		return 0;
	}

	/* Read resource map offset */
	if (fread(buf, 1, 4, file) < 4) {
		fclose(file);
		return 0;
	}

	val = read_uint32(buf);

	if (fseek(file, val + 28, SEEK_SET) == -1) {
		fclose(file);
		return 0;
	}

	/* Read number of types in map */
	if (fread(buf, 1, 2, file) < 2) {
		fclose(file);
		return 0;
	}

	val = read_uint16(buf) + 1;

	for (i = 0; i < val; i++) {
		if (fread(buf, 1, 4, file) < 4) {
			fclose(file);
			return 0;
		}

		/* Look for executable code */
		if (!memcmp(buf, "CODE", 4)) {
			fclose(file);
			return 1;
		}

		/* Skip to next list entry */
		if (fseek(file, 4, SEEK_CUR) == -1) {
			fclose(file);
			return 0;
		}
	}

	fclose(file);
	return 0;
}

static int
is_exe(char *filename) {
	FILE *file;
	char buf[4];
	unsigned char header[] = {0x00, 0x00, 0x03, 0xf3};

	/* PC and Atari ST executable extensions */
	if (strstr(filename, ".exe") || strstr(filename, ".EXE")
	        || strstr(filename, ".prg") || strstr(filename, ".PRG"))
		return 1;

	/* Check for Amiga executable */
	if (strchr(filename, '.'))
		return 0;

	file = fopen(filename, "rb");
	if (!file)
		return 0;

	if (fread(buf, 1, 4, file) < 4) {
		fclose(file);
		return 0;
	}

	fclose(file);

	/* Check header bytes */
	return memcmp(buf, header, 4) == 0;
}

void
version_require_earlier_than(state_t *s, sci_version_t version) {
	if (s->version_lock_flag)
		return;

	if (version <= s->min_version) {
		sciprintf("Version autodetect conflict: Less than %d.%03d.%03d was requested, but "
		          "%d.%03d.%03d is the current minimum\n",
		          SCI_VERSION_MAJOR(version), SCI_VERSION_MINOR(version), SCI_VERSION_PATCHLEVEL(version),
		          SCI_VERSION_MAJOR(s->min_version), SCI_VERSION_MINOR(s->min_version),
		          SCI_VERSION_PATCHLEVEL(s->min_version));
		return;
	} else if (version < s->max_version) {
		s->max_version = version - 1;
		if (s->max_version < s->version)
			s->version = s->max_version;
	}
}

void
version_require_later_than(state_t *s, sci_version_t version) {
	if (s->version_lock_flag)
		return;

	if (version > s->max_version) {
		sciprintf("Version autodetect conflict: More than %d.%03d.%03d was requested, but less than"
		          "%d.%03d.%03d is required ATM\n",
		          SCI_VERSION_MAJOR(version), SCI_VERSION_MINOR(version), SCI_VERSION_PATCHLEVEL(version),
		          SCI_VERSION_MAJOR(s->max_version), SCI_VERSION_MINOR(s->max_version),
		          SCI_VERSION_PATCHLEVEL(s->max_version));
		return;
	} else if (version > s->min_version) {
		s->min_version = version;
		if (s->min_version > s->version)
			s->version = s->min_version;
	}
}

int
version_parse(const char *vn, sci_version_t *result) {
	char *endptr[3];
	int major = strtol(vn, &endptr[0], 10);
	int minor = strtol(vn + 2, &endptr[1], 10);
	int patchlevel = strtol(vn + 6, &endptr[2], 10);

	if (endptr[0] != vn + 1 || endptr[1] != vn + 5
	        || *endptr[2] != '\0') {
		sciprintf("Warning: Failed to parse version string '%s'\n", vn);
		return 1;
	}

	*result = SCI_VERSION(major, minor, patchlevel);
	return 0;
}

int
version_detect_from_executable(sci_version_t *result) {
	sci_dir_t dir;
	char *filename;
	int mac = 0;

	/* For Mac versions we need to search the resource fork */
	mac = !chdir(".rsrc");

	sci_init_dir(&dir);

	filename = sci_find_first(&dir, "*");

	while (filename) {
		if (mac ? is_mac_exe(filename) : is_exe(filename))
			if (!scan_file(filename, result)) {
				sci_finish_find(&dir);

				if (mac)
					chdir("..");

				return 0;
			}

		filename = sci_find_next(&dir);
	}

	if (mac)
		chdir("..");

	return 1;
}

#define HASHCODE_MAGIC_RESOURCE_000 0x55555555
#define HASHCODE_MAGIC_RESOURCE_001 0x00000001

const char *  /* Original version by Solomon Peachy */
version_guess_from_hashcode(sci_version_t *result, int *res_version, guint32 *code) {
	int i;
	int fd = -1;
	int left = VERSION_DETECT_HASH_SIZE;
	guint32 hash_code;
	guint8 buf[VERSION_DETECT_BUF_SIZE];

	if (IS_VALID_FD(fd = sci_open("resource.001", O_RDONLY | O_BINARY))) {
		hash_code = HASHCODE_MAGIC_RESOURCE_001;
	} else if (IS_VALID_FD(fd = sci_open("resource.000", O_RDONLY | O_BINARY))) {
		hash_code = HASHCODE_MAGIC_RESOURCE_000;
	} else {
		sciprintf("Warning: Could not find RESOURCE.000 or RESOURCE.001, cannot determine hash code\n");
		*code = 0;
		/* complete and utter failure */
		return NULL;
	}

	while (left > 0) {
		int len = read(fd, buf, left < VERSION_DETECT_BUF_SIZE ? left : VERSION_DETECT_BUF_SIZE);

		if (len == -1) {
			sciprintf("Warning: read error while computing hash code for resource file\n");
			*code = 0;
			return NULL;
		}

		if (len == 0)
			/* EOF */
			break;

		for (i = 0; i < len; i++)
			hash_code = (hash_code * 19) + *(buf + i);

		/* This is the string hashing algorithm used by Objective Caml 3.08; the general idea
		** of multiplying the previous hash code with a prime number between 5 and 23 appears
		** to be generally considered to be a "good" approach to exhausting the entire 32 bit
		** number space in a somewhat equal distribution. For large chunks of data, such as
		** SCI resource files, this should both perform well and yield a good distribution,
		** or at least that's what standard library designers have been assuming for quite a
		** while. */

		left -= len;
	}

	close(fd);

	*code = hash_code;

	for (i = 0 ; sci_games[i].name ; i++) {
		if ((unsigned int)sci_games[i].id == hash_code) {
			*result = sci_games[i].version;
			*res_version = sci_games[i].res_version;
			return sci_games[i].name;
		}
	}

	return NULL; /* Failed to find matching game */
}


#undef VERSION_DETECT_BUF_SIZE