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
|
#!/usr/bin/python
##
## license:BSD-3-Clause
## copyright-holders:Vas Crabb
from . import dbaccess
import codecs
import hashlib
import os
import os.path
import struct
import sys
import zlib
class _Identifier(object):
def __init__(self, dbcurs, **kwargs):
super(_Identifier, self).__init__(**kwargs)
self.dbcurs = dbcurs
self.shortnamewidth = 0
self.pathwidth = 0
self.labelwidth = 0
self.machines = { }
self.software = { }
self.unmatched = [ ]
def processPath(self, path, depth=0):
try:
if not os.path.isdir(path):
self.processFile(path)
elif depth > 5:
sys.stderr.write('Not examining \'%s\' - maximum depth exceeded\n')
else:
for name in os.listdir(path):
self.processPath(os.path.join(path, name), depth + 1)
except BaseException as e:
sys.stderr.write('Error identifying \'%s\': %s\n' % (path, e))
def printResults(self):
nw = self.shortnamewidth - (self.shortnamewidth % 4) + 4
pw = self.pathwidth - (self.pathwidth % 4) + 4
lw = self.labelwidth - (self.labelwidth % 4) + 4
first = True
for shortname, romset in sorted(self.machines.items()):
if first:
first = False
else:
sys.stdout.write('\n')
sys.stdout.write('%-*s%s\n' % (nw, shortname, romset[0]))
self.printMatches(romset[1], pw, lw)
for softwarelist, listinfo in sorted(self.software.items()):
for shortname, softwareinfo in sorted(listinfo[1].items()):
if first:
first = False
else:
sys.stdout.write('\n')
sys.stdout.write('%-*s%s\n' % (nw, '%s:%s' % (softwarelist, shortname), softwareinfo[0]))
for part, partinfo in sorted(softwareinfo[1].items()):
if partinfo[0] is not None:
sys.stdout.write('%-*s%s\n' % (nw, ' ' + part, partinfo[0]))
else:
sys.stdout.write(' %s\n' % (part, ))
self.printMatches(partinfo[1], pw, lw)
if self.unmatched:
if first:
first = False
else:
sys.stdout.write('\n')
sys.stdout.write('Unmatched\n')
for path, crc, sha1 in self.unmatched:
if crc is not None:
sys.stdout.write(' %-*sCRC(%08x) SHA1(%s)\n' % (pw, path, crc, sha1))
else:
sys.stdout.write(' %-*sSHA1(%s)\n' % (pw, path, sha1))
def getMachineMatches(self, shortname, description):
result = self.machines.get(shortname)
if result is None:
result = (description, [ ])
self.machines[shortname] = result
return result[1]
def getSoftwareMatches(self, softwarelist, softwarelistdescription, shortname, description, part, part_id):
listinfo = self.software.get(softwarelist)
if listinfo is None:
listinfo = (softwarelistdescription, { })
self.software[softwarelist] = listinfo
softwareinfo = listinfo[1].get(shortname)
if softwareinfo is None:
softwareinfo = (description, { })
listinfo[1][shortname] = softwareinfo
partinfo = softwareinfo[1].get(part)
if partinfo is None:
partinfo = (part_id, [ ])
softwareinfo[1][part] = partinfo
return partinfo[1]
def processRomFile(self, path, f):
crc, sha1 = self.digestRom(f)
matched = False
for shortname, description, label, bad in self.dbcurs.get_rom_dumps(crc, sha1):
matched = True
self.shortnamewidth = max(len(shortname), self.shortnamewidth)
self.labelwidth = max(len(label), self.labelwidth)
self.getMachineMatches(shortname, description).append((path, label, bad))
for softwarelist, softwarelistdescription, shortname, description, part, part_id, label, bad in self.dbcurs.get_software_rom_dumps(crc, sha1):
matched = True
self.shortnamewidth = max(len(softwarelist) + 1 + len(shortname), 2 + len(part), self.shortnamewidth)
self.labelwidth = max(len(label), self.labelwidth)
self.getSoftwareMatches(softwarelist, softwarelistdescription, shortname, description, part, part_id).append((path, label, bad))
if not matched:
self.unmatched.append((path, crc, sha1))
def processChd(self, path, sha1):
matched = False
for shortname, description, label, bad in self.dbcurs.get_disk_dumps(sha1):
matched = True
self.shortnamewidth = max(len(shortname), self.shortnamewidth)
self.labelwidth = max(len(label), self.labelwidth)
self.getMachineMatches(shortname, description).append((path, label, bad))
for softwarelist, softwarelistdescription, shortname, description, part, part_id, label, bad in self.dbcurs.get_software_disk_dumps(sha1):
matched = True
self.shortnamewidth = max(len(softwarelist) + 1 + len(shortname), 2 + len(part), self.shortnamewidth)
self.labelwidth = max(len(label), self.labelwidth)
self.getSoftwareMatches(softwarelist, softwarelistdescription, shortname, description, part, part_id).append((path, label, bad))
if not matched:
self.unmatched.append((path, None, sha1))
def processFile(self, path):
if os.path.splitext(path)[1].lower() != '.chd':
with open(path, mode='rb', buffering=0) as f:
self.processRomFile(path, f)
else:
with open(path, mode='rb') as f:
sha1 = self.probeChd(f)
if sha1 is None:
f.seek(0)
self.processRomFile(path, f)
else:
self.processChd(path, sha1)
self.pathwidth = max(len(path), self.pathwidth)
@staticmethod
def iterateBlocks(f, s=65536):
while True:
buf = f.read(s)
if buf:
yield buf
else:
break
@staticmethod
def digestRom(f):
crc = zlib.crc32(bytes())
sha = hashlib.sha1()
for block in _Identifier.iterateBlocks(f):
crc = zlib.crc32(block, crc)
sha.update(block)
return crc & 0xffffffff, sha.hexdigest()
@staticmethod
def probeChd(f):
buf = f.read(16)
if (len(buf) != 16) or (buf[:8] != b'MComprHD'):
return None
headerlen, version = struct.unpack('>II', buf[8:])
if version == 3:
if headerlen != 120:
return None
sha1offs = 80
elif version == 4:
if headerlen != 108:
return None
sha1offs = 48
elif version == 5:
if headerlen != 124:
return None
sha1offs = 84
else:
return None
f.seek(sha1offs)
if f.tell() != sha1offs:
return None
buf = f.read(20)
if len(buf) != 20:
return None
return codecs.getencoder('hex_codec')(buf)[0].decode('ascii')
@staticmethod
def printMatches(matches, pathwidth, labelwidth):
for path, label, bad in matches:
if bad:
sys.stdout.write(' %-*s= %-*s(BAD)\n' % (pathwidth, path, labelwidth, label))
else:
sys.stdout.write(' %-*s= %s\n' % (pathwidth, path, label))
def do_listfull(options):
dbconn = dbaccess.QueryConnection(options.database)
dbcurs = dbconn.cursor()
first = True
for shortname, description in dbcurs.listfull(options.pattern):
if first:
sys.stdout.write('Name: Description:\n')
first = False
sys.stdout.write('%-16s "%s"\n' % (shortname, description))
if first:
sys.stderr.write('No matching systems found for \'%s\'\n' % (options.pattern, ))
dbcurs.close()
dbconn.close()
def do_listsource(options):
dbconn = dbaccess.QueryConnection(options.database)
dbcurs = dbconn.cursor()
shortname = None
for shortname, sourcefile in dbcurs.listsource(options.pattern):
sys.stdout.write('%-16s %s\n' % (shortname, sourcefile))
if shortname is None:
sys.stderr.write('No matching systems found for \'%s\'\n' % (options.pattern, ))
dbcurs.close()
dbconn.close()
def do_listclones(options):
dbconn = dbaccess.QueryConnection(options.database)
dbcurs = dbconn.cursor()
first = True
for shortname, parent in dbcurs.listclones(options.pattern):
if first:
sys.stdout.write('Name: Clone of:\n')
first = False
sys.stdout.write('%-16s %s\n' % (shortname, parent))
if first:
count = dbcurs.count_systems(options.pattern).fetchone()[0]
if count:
sys.stderr.write('Found %d match(es) for \'%s\' but none were clones\n' % (count, options.pattern))
else:
sys.stderr.write('No matching systems found for \'%s\'\n' % (options.pattern, ))
dbcurs.close()
dbconn.close()
def do_listbrothers(options):
dbconn = dbaccess.QueryConnection(options.database)
dbcurs = dbconn.cursor()
first = True
for sourcefile, shortname, parent in dbcurs.listbrothers(options.pattern):
if first:
sys.stdout.write('%-20s %-16s %s\n' % ('Source file:', 'Name:', 'Parent:'))
first = False
sys.stdout.write('%-20s %-16s %s\n' % (sourcefile, shortname, parent or ''))
if first:
sys.stderr.write('No matching systems found for \'%s\'\n' % (options.pattern, ))
dbcurs.close()
dbconn.close()
def do_listaffected(options):
dbconn = dbaccess.QueryConnection(options.database)
dbcurs = dbconn.cursor()
first = True
for shortname, description in dbcurs.listaffected(*options.pattern):
if first:
sys.stdout.write('Name: Description:\n')
first = False
sys.stdout.write('%-16s "%s"\n' % (shortname, description))
if first:
sys.stderr.write('No matching systems found for \'%s\'\n' % (options.pattern, ))
dbcurs.close()
dbconn.close()
def do_romident(options):
dbconn = dbaccess.QueryConnection(options.database)
dbcurs = dbconn.cursor()
ident = _Identifier(dbcurs)
for path in options.path:
ident.processPath(path)
ident.printResults()
dbcurs.close()
dbconn.close()
|