-
Notifications
You must be signed in to change notification settings - Fork 8
/
rom.c
363 lines (299 loc) · 8.96 KB
/
rom.c
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
/*
This file is part of CrabEmu.
Copyright (C) 2012, 2013, 2015 Lawrence Sebald
CrabEmu is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
CrabEmu 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 CrabEmu; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _arch_dreamcast
#include <zlib/zlib.h>
#include <bzlib/bzlib.h>
#else
#ifndef NO_ZLIB
#include <zlib.h>
#endif
#ifndef NO_BZ2
#include <bzlib.h>
#endif
#endif
#include "rom.h"
#include "sms.h"
#include "colecovision.h"
#include "nes.h"
#ifndef NO_ZLIB
#include "unzip.h"
#endif
/* Special "console" values */
#define TYPE_GZIP -100
#define TYPE_BZIP2 -101
#define TYPE_ZIP -102
static int guess_on_ext(const char *fn, int chop) {
char *fn2 = NULL;
char *ext;
int rv = -1;
/* Grab the extension of the file... */
if(chop) {
if(!(fn2 = strdup(fn)))
return -1;
if(!(ext = strrchr(fn2, '.'))) {
free(fn2);
return -1;
}
*ext = '\0';
ext = strrchr(fn2, '.');
}
else {
ext = strrchr(fn, '.');
}
/* If there's no extension, we can't really take a guess... */
if(!ext)
rv = -1;
else if(!strcasecmp(ext, ".sms"))
return CONSOLE_SMS;
else if(!strcasecmp(ext, ".gg"))
return CONSOLE_GG;
else if(!strcasecmp(ext, ".sc"))
return CONSOLE_SC3000;
else if(!strcasecmp(ext, ".sg"))
return CONSOLE_SG1000;
else if(!strcasecmp(ext, ".rom") || !strcasecmp(ext, ".col"))
return CONSOLE_COLECOVISION;
else if(!strcasecmp(ext, ".nes"))
return CONSOLE_NES;
else if(!strcasecmp(ext, ".ch8") || !strcasecmp(ext, ".c8"))
return CONSOLE_CHIP8;
#ifndef NO_ZLIB
else if(!strcasecmp(ext, ".gz"))
return TYPE_GZIP;
else if(!strcasecmp(ext, ".zip"))
return TYPE_ZIP;
#endif
#ifndef NO_BZ2
else if(!strcasecmp(ext, ".bz2"))
return TYPE_BZIP2;
#endif
if(fn2)
free(fn2);
return rv;
}
#ifndef NO_ZLIB
static int detect_gz(const char *fn) {
FILE *fp;
uint8 buf[2];
int seek_amt, fn_len = 0, max_fn_len = 4096, ch;
char *filename;
void *tmp;
/* Open up the file and try to see if the original filename is in the file's
header. If it is, use that to try to guess further. Otherwise, we'll try
to guess based on the current filename... */
if(!(fp = fopen(fn, "rb"))) {
return -2;
}
/* Read the first two bytes to make sure this is actually a gzip file. */
if(fread(buf, 1, 2, fp) != 2) {
fclose(fp);
return -2;
}
if(buf[0] != 0x1F || buf[1] != 0x8B) {
fclose(fp);
return -2;
}
/* Read in the next two bytes so we can grab the filename, if its there. */
if(fread(buf, 1, 2, fp) != 2) {
fclose(fp);
return -2;
}
/* Is the FNAME bit set? */
if(buf[1] & 0x08) {
/* Skip the rest of the header... */
if(fseek(fp, 6, SEEK_CUR)) {
fclose(fp);
return -2;
}
/* If the FEXTRA bit is set, we have to skip past that too. */
if(buf[1] & 0x04) {
if(fread(buf, 1, 2, fp) != 2) {
fclose(fp);
return -2;
}
seek_amt = buf[0] | (buf[1] << 8);
if(fseek(fp, seek_amt, SEEK_CUR)) {
fclose(fp);
return -2;
}
}
/* We should be at the filename field now. Read it in... */
if(!(filename = (char *)malloc(4096))) {
fclose(fp);
return -2;
}
for(;;) {
ch = fgetc(fp);
if(ch == EOF) {
free(filename);
fclose(fp);
return -2;
}
else if(ch == 0) {
filename[fn_len] = 0;
break;
}
else {
filename[fn_len++] = (char)ch;
if(fn_len == max_fn_len) {
if(!(tmp = realloc(filename, max_fn_len << 1))) {
free(filename);
fclose(fp);
return -2;
}
max_fn_len <<= 1;
}
}
}
/* We've got the filename, take our guess and clean up. */
ch = guess_on_ext(filename, 0);
free(filename);
fclose(fp);
return ch;
}
fclose(fp);
/* If we're still here, we have to punt and use the method of trying to
parse out the current filename... */
return guess_on_ext(fn, 1);
}
#endif
#ifndef NO_BZ2
static int detect_bz2(const char *fn) {
/* We really don't have a choice here... Guess based on the extension before
.bz2. */
return guess_on_ext(fn, 1);
}
#endif
#ifndef NO_ZLIB
static int detect_zip(const char *fn) {
int console = 0;
unzFile fp;
unz_file_info inf;
char fn2[4096];
/* Open up the file in question. */
if(!(fp = unzOpen(fn))) {
#ifdef DEBUG
fprintf(stderr, "rom_detect_console: Could not open ROM: %s!\n", fn);
#endif
return -2;
}
if(unzGoToFirstFile(fp) != UNZ_OK) {
#ifdef DEBUG
fprintf(stderr, "rom_detect_console: Couldn't find file in zip!\n");
#endif
unzClose(fp);
return -2;
}
/* Keep looking at files until we find a rom. */
while(console <= 0) {
if(unzGetCurrentFileInfo(fp, &inf, fn2, 4096, NULL, 0, NULL,
0) != UNZ_OK) {
#ifdef DEBUG
fprintf(stderr, "rom_detect_console: Error parsing zip file!\n");
#endif
unzClose(fp);
return -2;
}
/* Check if this file looks like a rom. */
console = guess_on_ext(fn2, 0);
/* If we haven't found a rom yet, move to the next file. */
if(console <= 0) {
if(unzGoToNextFile(fp) != UNZ_OK) {
#ifdef DEBUG
fprintf(stderr, "rom_detect_console: End of zip file!\n");
#endif
unzClose(fp);
return -2;
}
}
}
/* If we get here, we have our console. Return it! */
return console;
}
#endif
int rom_detect_console(const char *fn) {
int rv;
rv = guess_on_ext(fn, 0);
switch(rv) {
#ifndef NO_ZLIB
case TYPE_GZIP:
return detect_gz(fn);
case TYPE_ZIP:
return detect_zip(fn);
#endif
#ifndef NO_BZ2
case TYPE_BZIP2:
return detect_bz2(fn);
#endif
default:
return rv;
}
}
uint32 rom_crc32(const uint8 *data, int size) {
int i;
uint32 rv = 0xFFFFFFFF;
for(i = 0; i < size; ++i) {
rv ^= data[i];
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
rv = (0xEDB88320 & (-(rv & 1))) ^(rv >> 1);
}
return ~rv;
}
/* Public-domain adler32 checksum. Borrowed from stb-2.23, which has the
following comment at the top of it:
stb-2.23 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h
no warranty is offered or implied; use this code at your own risk
This is a single header file with a bunch of useful utilities
for getting stuff done in C/C++.
Email bug reports, feature requests, etc. to 'sean' at the same site.
Documentation: http://nothings.org/stb/stb_h.html
Unit tests: http://nothings.org/stb/stb.c
*/
#include <stdint.h>
#define STB_ADLER32_SEED 1
uint32 rom_adler32(const uint8 *buffer, uint32 buflen) {
const unsigned long ADLER_MOD = 65521;
unsigned long s1 = STB_ADLER32_SEED & 0xffff, s2 = STB_ADLER32_SEED >> 16;
unsigned long blocklen, i;
blocklen = buflen % 5552;
while (buflen) {
for (i=0; i + 7 < blocklen; i += 8) {
s1 += buffer[0], s2 += s1;
s1 += buffer[1], s2 += s1;
s1 += buffer[2], s2 += s1;
s1 += buffer[3], s2 += s1;
s1 += buffer[4], s2 += s1;
s1 += buffer[5], s2 += s1;
s1 += buffer[6], s2 += s1;
s1 += buffer[7], s2 += s1;
buffer += 8;
}
for (; i < blocklen; ++i)
s1 += *buffer++, s2 += s1;
s1 %= ADLER_MOD, s2 %= ADLER_MOD;
buflen -= blocklen;
blocklen = 5552;
}
return (s2 << 16) + s1;
}