aboutsummaryrefslogtreecommitdiff
path: root/devtools/md5table.c
blob: de084121bb642215618808aa4210fa2adb4032d2 (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
/* md5table - Convert a MD5 table to either PHP or C++ code
 *
 * 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 <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void error(const char *s, ...) {
	char buf[1024];
	va_list va;

	va_start(va, s);
	vsnprintf(buf, 1024, s, va);
	va_end(va);

	fprintf(stderr, "ERROR: %s!\n", buf);

	exit(1);
}

void warning(const char *s, ...) {
	char buf[1024];
	va_list va;

	va_start(va, s);
	vsnprintf(buf, 1024, s, va);
	va_end(va);

	fprintf(stderr, "WARNING: %s!\n", buf);
}

typedef struct {
	const char *key;
	const char *value;
} StringMap;

typedef struct {
	const char *md5;
	const char *language;
	const char *platform;
	const char *variant;
	const char *extra;
	const char *size;
	const char *desc;
	const char *comments;
	const char *infoSource;
} Entry;

/* Map MD5 table platform names to ScummVM constant names.
 * Note: Currently not many constants are defined within ScummVM. However, more
 * will probably be added eventually (see also commented out constants in
 * common/util.h).
 */
static const StringMap platformMap[] = {
	{ "2gs",		"kPlatformApple2GS" },
	{ "3DO",		"kPlatform3DO" },
	{ "Amiga",		"kPlatformAmiga" },
	{ "Atari",		"kPlatformAtariST" },
	{ "C64",		"kPlatformC64" },
	{ "DOS",		"kPlatformDOS" },
	{ "FM-TOWNS",		"kPlatformFMTowns" },
	{ "iOS",		"kPlatformIOS" },
	{ "Mac",		"kPlatformMacintosh" },
	{ "NES",		"kPlatformNES" },
	{ "PC-Engine",		"kPlatformPCEngine" },
	{ "SEGA",		"kPlatformSegaCD" },
	{ "Windows",		"kPlatformWindows" },
	{ "Wii",		"kPlatformWii" },

	{ "All?",		"kPlatformUnknown" },
	{ "All",		"kPlatformUnknown" },

	{ 0,			"kPlatformUnknown" }
};

static const StringMap langMap[] = {
	{ "en",		"EN_ANY" },
	{ "us",		"EN_USA" },
	{ "gb",		"EN_GRB" },
	{ "de",		"DE_DEU" },
	{ "fr",		"FR_FRA" },
	{ "it",		"IT_ITA" },
	{ "br",		"PT_BRA" },
	{ "es",		"ES_ESP" },
	{ "jp",		"JA_JPN" },
	{ "zh",		"ZH_TWN" },
	{ "ko",		"KO_KOR" },
	{ "se",		"SE_SWE" },
	{ "en",		"EN_GRB" },
	{ "he",		"HE_ISR" },
	{ "ru",		"RU_RUS" },
	{ "cz",		"CZ_CZE" },
	{ "nl",		"NL_NLD" },
	{ "nb",		"NB_NOR" },
	{ "pl",		"PL_POL" },

	{ "All",	"UNK_LANG" },
	{ "All?",	"UNK_LANG" },

	{ 0,		"UNK_LANG" }
};

static const char *php_header =
	"<!--\n"
	"  This file was generated by the md5table tool on %s"
	"  DO NOT EDIT MANUALLY!\n"
	" -->\n"
	"<?php\n"
	"\n";

static const char *c_header =
	"/*\n"
	"  This file was generated by the md5table tool on %s"
	"  DO NOT EDIT MANUALLY!\n"
	" */\n"
	"\n"
	"struct MD5Table {\n"
	"	const char *md5;\n"
	"	const char *gameid;\n"
	"	const char *variant;\n"
	"	const char *extra;\n"
	"	int32 filesize;\n"
	"	Common::Language language;\n"
	"	Common::Platform platform;\n"
	"};\n"
	"\n"
	"static const MD5Table md5table[] = {\n";

static const char *c_footer =
	"	{ 0, 0, 0, 0, 0, Common::UNK_LANG, Common::kPlatformUnknown }\n"
	"};\n";

static void parseEntry(Entry *entry, char *line) {
	assert(entry);
	assert(line);

	/* Split at the tabs */
	entry->md5 = strtok(line, "\t\n\r");
	entry->size = strtok(NULL, "\t\n\r");
	entry->language = strtok(NULL, "\t\n\r");
	entry->platform = strtok(NULL, "\t\n\r");
	entry->variant = strtok(NULL, "\t\n\r");
	entry->extra = strtok(NULL, "\t\n\r");
	entry->desc = strtok(NULL, "\t\n\r");
	entry->infoSource = strtok(NULL, "\t\n\r");
}

static int isEmptyLine(const char *line) {
	const char *whitespace = " \t\n\r";
	while (*line) {
		if (!strchr(whitespace, *line))
			return 0;
		line++;
	}
	return 1;
}

static const char *mapStr(const char *str, const StringMap *map) {
	assert(str);
	assert(map);
	while (map->key) {
		if (0 == strcmp(map->key, str))
			return map->value;
		map++;
	}
	warning("mapStr: unknown string '%s', defaulting to '%s'", str, map->value);
	return map->value;
}

void showhelp(const char *exename)
{
	printf("\nUsage: %s <params>\n", exename);
	printf("\nParams:\n");
	printf(" --c++   output C++ code for inclusion in ScummVM (default)\n");
	printf(" --php   output PHP code for the web site\n");
	printf(" --txt   output TXT file (should be identical to input file)\n");
	exit(2);
}

/* needed to call from qsort */
int strcmp_wrapper(const void *s1, const void *s2)
{
	return strcmp((const char *)s1, (const char *)s2);
}

int main(int argc, char *argv[])
{
	FILE *inFile = stdin;
	FILE *outFile = stdout;
	char buffer[1024];
	char section[256];
	char gameid[32];
	char *line;
	int err;
	int i;
	time_t theTime;
	char *generationDate;

	const int entrySize = 256;
	int numEntries = 0, maxEntries = 1;
	char *entriesBuffer = (char *)malloc(maxEntries * entrySize);

	typedef enum {
		kCPPOutput,
		kPHPOutput,
		kTXTOutput
	} OutputMode;

	OutputMode outputMode = kCPPOutput;

	if (argc != 2)
		showhelp(argv[0]);
	if (strcmp(argv[1], "--c++") == 0) {
		outputMode = kCPPOutput;
	} else if (strcmp(argv[1], "--php") == 0) {
		outputMode = kPHPOutput;
	} else if (strcmp(argv[1], "--txt") == 0) {
		outputMode = kTXTOutput;
	} else {
		showhelp(argv[0]);
	}

	time(&theTime);
	generationDate = strdup(asctime(gmtime(&theTime)));

	if (outputMode == kPHPOutput)
		fprintf(outFile, php_header, generationDate);

	section[0] = 0;
	gameid[0] = 0;
	while ((line = fgets(buffer, sizeof(buffer), inFile))) {
		/* Parse line */
		if (line[0] == '#' || isEmptyLine(line)) {
			if (outputMode == kTXTOutput)
				fprintf(outFile, "%s", line);
			continue;	/* Skip comments & empty lines */
		}
		if (line[0] == '\t') {
			Entry entry;
			assert(section[0]);
			parseEntry(&entry, line+1);
			if (outputMode == kPHPOutput) {
				fprintf(outFile, "\taddEntry(");

				// Print the description string
				fprintf(outFile, "\"");
				if (entry.extra && strcmp(entry.extra, "-")) {
					fprintf(outFile, "%s", entry.extra);
					if (entry.desc && strcmp(entry.desc, "-"))
						fprintf(outFile, " (%s)", entry.desc);
				}
				fprintf(outFile, "\", ");

				fprintf(outFile, "\"%s\", ", entry.platform);
				fprintf(outFile, "\"%s\", ", entry.language);
				fprintf(outFile, "\"%s\"", entry.md5);
				if (entry.infoSource)
					fprintf(outFile, ", \"%s\"", entry.infoSource);
				fprintf(outFile, ");\n");
			} else if (outputMode == kTXTOutput) {
				fprintf(outFile, "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
					entry.md5,
					entry.size,
					entry.language,
					entry.platform,
					entry.variant,
					entry.extra,
					entry.desc,
					entry.infoSource ? entry.infoSource : ""
					);
			} else if (entry.md5) {
				if (numEntries >= maxEntries) {
					maxEntries *= 2;
					entriesBuffer = (char *)realloc(entriesBuffer, maxEntries * entrySize);
				}
				if (0 == strcmp(entry.variant, "-"))
					entry.variant = "";
				if (0 == strcmp(entry.extra, "-"))
					entry.extra = "";
				snprintf(entriesBuffer + numEntries * entrySize, entrySize,
					"\t{ \"%s\", \"%s\", \"%s\", \"%s\", %s, Common::%s, Common::%s },\n",
					entry.md5,
					gameid,
					entry.variant,
					entry.extra,
					entry.size,
					mapStr(entry.language, langMap),
					mapStr(entry.platform, platformMap));
				numEntries++;
			}
		} else {
			if (outputMode == kPHPOutput && gameid[0] != 0) {
				// If there is an active section, close it now
				fprintf(outFile, "endSection();\n");
			}
			// Read the gameid, followed by a tab
			for (i = 0; *line && *line != '\t'; ++i)
				gameid[i] = *line++;
			assert(i > 0);
			gameid[i] = 0;
			assert(*line != 0);
			line++;

			// Read the section header (usually the full game name)
			for (i = 0; *line && *line != '\n'; ++i)
				section[i] = *line++;
			assert(i > 0);
			section[i] = 0;

			// If in PHP or TXT mode, we write the output immediately
			if (outputMode == kPHPOutput) {
				fprintf(outFile, "beginSection(\"%s\", \"%s\");\n", section, gameid);
			} else if (outputMode == kTXTOutput) {
				fprintf(outFile, "%s\t%s\n", gameid, section);
			}
		}
	}

	err = ferror(inFile);
	if (err)
		error("Failed reading from input file, error %d", err);

	if (outputMode == kPHPOutput) {
		if (gameid[0] != 0)		// If there is an active section, close it now
			fprintf(outFile, "endSection();\n");

		fprintf(outFile, "?>\n");
	}

	if (outputMode == kCPPOutput) {
		/* Printf header */
		fprintf(outFile, c_header, generationDate);
		/* Now sort the MD5 table (this allows for binary searches) */
		qsort(entriesBuffer, numEntries, entrySize, strcmp_wrapper);
		/* Output the table and emit warnings if duplicate md5s are found */
		buffer[0] = '\0';
		for (i = 0; i < numEntries; ++i) {
			const char *currentEntry = entriesBuffer + i * entrySize;
			fprintf(outFile, "%s", currentEntry);
			if (strncmp(currentEntry + 4, buffer, 32) == 0) {
				warning("Duplicate MD5 found '%.32s'", buffer);
			} else {
				strncpy(buffer, currentEntry + 4, 32);
			}
		}
		/* Finally, print the footer */
		fprintf(outFile, "%s", c_footer);
	}

	free(entriesBuffer);
	free(generationDate);

	return 0;
}