forked from sselph/scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
368 lines (338 loc) · 7.41 KB
/
hash.go
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
package hash
import (
"bytes"
"encoding/hex"
"fmt"
"hash"
"io"
"io/ioutil"
"os"
"path"
"strings"
)
type decoder func(io.ReadCloser, int64) (io.ReadCloser, error)
// Noop does nothong but return the passed in file.
func noop(f io.ReadCloser, s int64) (io.ReadCloser, error) {
return f, nil
}
func decodeLNX(f io.ReadCloser, s int64) (io.ReadCloser, error) {
if s < 4 {
return nil, fmt.Errorf("invalid ROM")
}
tmp := make([]byte, 64)
_, err := io.ReadFull(f, tmp)
if err != nil && err != io.ErrUnexpectedEOF {
return nil, err
}
if err == io.ErrUnexpectedEOF || !bytes.Equal(tmp[:4], []byte("LYNX")) {
return MultiReader(tmp, f), nil
}
return f, nil
}
type multireader struct {
r io.ReadCloser
mr io.Reader
}
func (mr *multireader) Read(b []byte) (int, error) {
return mr.mr.Read(b)
}
func (mr *multireader) Close() error {
return mr.r.Close()
}
func MultiReader(b []byte, r io.ReadCloser) io.ReadCloser {
return &multireader{r, io.MultiReader(bytes.NewReader(b), r)}
}
func decodeA78(f io.ReadCloser, s int64) (io.ReadCloser, error) {
tmp := make([]byte, 128)
_, err := io.ReadFull(f, tmp)
if err != nil && err != io.ErrUnexpectedEOF {
return nil, err
}
if err == io.ErrUnexpectedEOF || !bytes.Equal(tmp[1:10], []byte("ATARI7800")) {
return MultiReader(tmp, f), nil
}
return f, nil
}
func deinterleave(p []byte) []byte {
l := len(p)
m := l / 2
b := make([]byte, l)
for i, x := range p {
if i < m {
b[i*2+1] = x
} else {
b[i*2-l] = x
}
}
return b
}
func decodeSMD(f io.ReadCloser, s int64) (io.ReadCloser, error) {
return decodeMD(f, s, ".smd")
}
func decodeMGD(f io.ReadCloser, s int64) (io.ReadCloser, error) {
return decodeMD(f, s, ".mgd")
}
func decodeGEN(f io.ReadCloser, s int64) (io.ReadCloser, error) {
return decodeMD(f, s, ".gen")
}
func decodeMD(f io.ReadCloser, s int64, e string) (io.ReadCloser, error) {
if s%16384 == 512 {
tmp := make([]byte, 512)
_, err := io.ReadFull(f, tmp)
if err != nil {
return nil, err
}
s -= 512
}
if s%16384 != 0 {
return nil, fmt.Errorf("Invalid MD size")
}
b, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
return nil, err
}
if bytes.Equal(b[256:260], []byte("SEGA")) {
return ioutil.NopCloser(bytes.NewReader(b)), nil
}
if bytes.Equal(b[8320:8328], []byte("SG EEI ")) || bytes.Equal(b[8320:8328], []byte("SG EADIE")) {
for i := 0; int64(i) < (s / int64(16384)); i++ {
x := i * 16384
copy(b[x:x+16384], deinterleave(b[x:x+16384]))
}
return ioutil.NopCloser(bytes.NewReader(b)), nil
}
if bytes.Equal(b[128:135], []byte("EAGNSS ")) || bytes.Equal(b[128:135], []byte("EAMG RV")) {
b = deinterleave(b)
return ioutil.NopCloser(bytes.NewReader(b)), nil
}
switch e {
case ".smd":
for i := 0; int64(i) < (s / int64(16384)); i++ {
x := i * 16384
copy(b[x:x+16384], deinterleave(b[x:x+16384]))
}
return ioutil.NopCloser(bytes.NewReader(b)), nil
case ".mgd":
b = deinterleave(b)
return ioutil.NopCloser(bytes.NewReader(b)), nil
case ".gen":
return ioutil.NopCloser(bytes.NewReader(b)), nil
}
return nil, fmt.Errorf("Unknown MD Error")
}
func decodeN64(f io.ReadCloser, s int64) (io.ReadCloser, error) {
i := 0
r := swapReader{f, make([]byte, 4), &i, noSwap}
if s < 4 {
return nil, fmt.Errorf("invalid ROM")
}
_, err := io.ReadFull(r.f, r.b)
*r.r = 4
if err != nil {
return nil, err
}
switch {
case r.b[0] == 0x80:
r.s = zSwap
case r.b[3] == 0x80:
r.s = nSwap
}
return r, nil
}
func noSwap(b []byte) {}
func zSwap(b []byte) {
l := len(b)
for i := 0; i < l; i += 4 {
b[i+1], b[i] = b[i], b[i+1]
b[i+3], b[i+2] = b[i+2], b[i+3]
}
}
func nSwap(b []byte) {
l := len(b)
for i := 0; i < l; i += 4 {
b[i+2], b[i] = b[i], b[i+2]
b[i+3], b[i+1] = b[i+1], b[i+3]
}
}
type swapReader struct {
f io.ReadCloser
b []byte
r *int
s func([]byte)
}
func (r swapReader) Read(p []byte) (int, error) {
ll := len(p)
rl := ll - *r.r
l := rl + 4 - 1 - (rl-1)%4
copy(p, r.b[:*r.r])
if rl <= 0 {
*r.r = *r.r - ll
copy(r.b, r.b[ll:])
return ll, nil
}
n := *r.r
b := make([]byte, l)
x, err := io.ReadFull(r.f, b)
if x == 0 || err != nil && err != io.ErrUnexpectedEOF {
return n, err
}
copy(p[n:ll], b[:x])
n += x
if ll <= n {
r.s(p)
copy(r.b, b[x+ll-n:x])
*r.r = n - ll
return ll, nil
} else {
r.s(p[:n])
*r.r = 0
return n, nil
}
}
func (r swapReader) Close() error {
return r.f.Close()
}
type nesReader struct {
f io.ReadCloser
offset int64
size int64
start int64
end int64
}
func (r *nesReader) Read(p []byte) (int, error) {
n, err := r.f.Read(p)
if err != nil {
return n, err
}
if r.offset+int64(n) > r.end {
n = int(r.end - r.offset)
}
r.offset += int64(n)
return n, err
}
func (r *nesReader) Close() error {
return r.f.Close()
}
func decodeNES(f io.ReadCloser, s int64) (io.ReadCloser, error) {
header := make([]byte, 16)
n, err := io.ReadFull(f, header)
if err != nil {
return nil, err
}
if n < 16 {
return nil, fmt.Errorf("invalid header")
}
prgSize := int64(header[4])
chrSize := int64(header[5])
if header[7]&12 == 8 {
romSize := int64(header[9])
chrSize = romSize&0x0F<<8 + chrSize
prgSize = romSize&0xF0<<4 + prgSize
}
var prg, chr int64
prg = 16 * 1024 * prgSize
chr = 8 * 1024 * chrSize
hasTrainer := header[6]&4 == 4
var offset int64
offset = 16
if hasTrainer {
tmp := make([]byte, 512)
n, err := io.ReadFull(f, tmp)
if err != nil {
return nil, err
}
offset += int64(n)
}
return &nesReader{f, offset, prg + chr, offset, offset + prg + chr}, nil
}
func decodeSNES(f io.ReadCloser, s int64) (io.ReadCloser, error) {
if s%1024 == 512 {
tmp := make([]byte, 512)
_, err := io.ReadFull(f, tmp)
if err != nil {
return nil, err
}
}
return f, nil
}
func getDecoder(ext string) (decoder, bool) {
switch strings.ToLower(ext) {
case ".bin", ".a26", ".a52", ".rom", ".cue", ".gdi", ".gb", ".gba", ".gbc", ".32x", ".gg",
".pce", ".sms", ".col", ".ngp", ".ngc", ".sg", ".int", ".vb", ".vec", ".gam", ".j64",
".jag", ".mgw":
return noop, true
case ".a78":
return decodeA78, true
case ".lnx", ".lyx":
return decodeLNX, true
case ".smd":
return decodeSMD, true
case ".mgd":
return decodeMGD, true
case ".gen", ".md":
return decodeGEN, true
case ".n64", ".v64", ".z64":
return decodeN64, true
case ".nes":
return decodeNES, true
case ".smc", ".sfc", ".fig", ".swc":
return decodeSNES, true
default:
return noop, false
}
}
// KnownExt returns true if the ext is recognized.
func KnownExt(ext string) bool {
ext = strings.ToLower(ext)
if ext == ".zip" {
return true
}
if ext == ".gz" {
return true
}
_, ok := getDecoder(ext)
return ok
}
// decode takes a path and returns a reader for the inner rom data.
func decode(p string) (io.ReadCloser, error) {
ext := strings.ToLower(path.Ext(p))
if ext == ".zip" {
r, err := decodeZip(p)
return r, err
}
if ext == ".gz" {
r, err := decodeGZip(p)
return r, err
}
decode, _ := getDecoder(ext)
r, err := os.Open(p)
if err != nil {
return nil, err
}
fi, err := r.Stat()
if err != nil {
return nil, err
}
ret, err := decode(r, fi.Size())
return ret, err
}
// Hash returns the hash of a rom given a path to the file and hash function.
func Hash(p string, h hash.Hash, buf []byte) (string, error) {
r, err := decode(p)
if err != nil {
return "", err
}
defer r.Close()
for {
n, err := r.Read(buf)
if err != nil && err != io.EOF {
return "", err
}
if n == 0 {
break
}
h.Write(buf[:n])
}
return hex.EncodeToString(h.Sum(nil)), nil
}