-
Notifications
You must be signed in to change notification settings - Fork 0
/
romident.c
231 lines (202 loc) · 4.93 KB
/
romident.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
/*
ROMIDENT v2.0 release 2.1
Thierry Lescot, 1998/99.
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <dirent.h>
#include <sys/types.h>
#include "unzip.h"
#include "defs.h"
typedef struct
{
short int game;
char rom[ROMNAME_SIZE];
unsigned int crc32;
int size;
} romid_t;
romid_t simple_entry;
romid_t *rtab;
struct { char title[80]; } *gtab;
char *wbuf;
char unk = 0;
int r_entry = 0, g_entry = 0;
int load_datafile(char *fn)
{
char *gamfile = "romident.gam";
char *romfile = "romident.rom";
char path[256];
int i = strlen(fn);
FILE *f;
while ((fn[i-1]!='/')&&(fn[i-1]!='\\')) i--;
sprintf(path, "%s", fn);
sprintf(&path[i], "%s", gamfile);
printf("GAM FILE = '%s'\n", path);
if ((g_entry = get_file_size(path)) == -1) return 1;
g_entry = g_entry / 80;
printf("Games in database = %d games.\n", g_entry);
if (!(f = fopen(path, "rb"))) return 1;
// load games datas
if (!(gtab = (void *)malloc(g_entry * 80))) {
printf("Error: Not enough memory to load games datas !\n");
return 1;
};
fread(gtab, 80, g_entry, f);
fclose(f);
sprintf(&path[i], "%s", romfile);
printf("ROM FILE = '%s'\n", path);
if ((r_entry = get_file_size(path)) == -1) return 1;
r_entry = r_entry / sizeof(romid_t);
printf("number of entries in rom file = %d\n", r_entry);
if (!(f = fopen(path, "rb"))) return 2;
// load roms datas
if (!(rtab = (void *)malloc(r_entry * sizeof(romid_t)))) {
printf("Error: Not enough memory to load games datas !\n");
return 1;
};
fread(rtab, sizeof(romid_t), r_entry, f);
fclose(f);
return 0;
}
int romident(char *rom, unsigned int crc32, int size)
{
int i, f = 0;
if (size%32) return -1;
printf("%-12s [%08x] ", rom, crc32);
for (i=0;i!=r_entry;i++) {
if (rtab[i].crc32 == crc32) {
if (f) printf(" ");
f++;
printf("= %-12s from %s\n", rtab[i].rom, gtab[rtab[i].game].title);
};
};
if (!f) {
printf("NOT FOUND!\n");
unk = 5;
};
return f;
};
int ident_crc(unsigned int crc32)
{
int i, f = 0;
printf("Checking crc 0x%08x ... ", crc32);
for (i=0;i!=r_entry;i++) {
if (rtab[i].crc32 == crc32) {
if (f) printf(" ");
f++;
printf("%-12s from %s\n", rtab[i].rom, gtab[rtab[i].game].title);
};
};
if (!f) {
printf("NOT FOUND IN DATABASE!\n");
unk = 5;
};
return f;
};
int ident_file(char *path, char *fn, int size)
{
unsigned int crc;
FILE *f;
char fpath[256];
//sprintf(fpath, "%s\\%s", path, fn);
sprintf(fpath, "%s", fn);
if (size == -1) {
printf("fpath = %s\n", fpath);
size = get_file_size(fpath);
if (size < 1) {
printf("Error, '%s' size incorrect (%s) !\n", fn, size);
return 1;
};
};
wbuf = (char *)malloc(size);
if (!wbuf) {
printf("Error, not enough memory to '%s' the file into memory !\n", fn);
return 1;
};
f = fopen(fpath, "rb");
if (!f) {
printf("Error, cannot open file '%s' !\n", fpath);
return 1;
};
fread(wbuf, 1, size, f);
crc = crc32(0L, wbuf, size);
fclose(f);
free(wbuf);
romident(fn, crc, size);
return 0;
}
int ident_zip(char *fn)
{
ZIP* zip;
struct zipent* zipf;
printf("Zip file to ident = '%s'\n", fn);
if ((zip = openzip(fn)) == 0) {
printf("Error, cannot open zip file '%s' !\n", fn);
return 1;
};
while (zipf = readzip(zip)) {
upper_case(zipf->name);
romident(zipf->name, zipf->crc32, zipf->uncompressed_size);
};
closezip(zip);
return 0;
}
int ident_dir(char *fn)
{
int count,i = 0;
struct dirent **files;
int file_select = NULL;
count = scandir(fn, &files, file_select, alphasort);
if (count > 0) {
for (i = 0; i < count; ++i) {
ident(files[i]->d_name);
}
}
return 0;
}
int ident(char *fn)
{
int l;
if (!is_directory(fn)) {
l = strlen(fn);
if (l>4) {
if (strcmp(&fn[l-4], ".ZIP") == 0) {
ident_zip(fn);
} else {
ident_file(".", fn, -1);
}
} else {
ident_file(".", fn, -1);
};
} else {
ident_dir(fn);
};
};
int main(int argc, char **argv)
{
int i, nf;
printf("ROMIDENT v2.1\nThierry Lescot, 1998/99.\n\n");
load_datafile(argv[0]);
printf("DAT file revision %d.\n", g_entry);
if (argc<2) {
printf("Error, specify at least one file name !\n");
return 1;
};
for (nf=1;nf<argc;nf++) {
if (argv[nf][0] == '-') {
switch(argv[nf][1]) {
case '&':
ident_crc(hex2int(&argv[nf][2]));
break;
};
} else {
if ((i = checkfile(argv[nf])) == -1) {
printf("Error, '%s' doesn't exist !\n", argv[1]);
return 1;
};
ident(argv[nf]);
};
};
return unk;
}