-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatches.go
119 lines (94 loc) · 2.25 KB
/
patches.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
package gc
import (
"bytes"
"fmt"
"io"
)
const (
lengthFZero = 0x8000
lengthPSO = 0x6000
)
//nolint:gomnd
func patchFZero(r io.Reader, mc *memoryCard) (io.Reader, error) {
serial1, serial2 := mc.serialNumbers()
b, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("unable to read save data: %w", err)
}
if len(b) < lengthFZero {
return nil, errInvalidLength
}
b[0x2066] = byte(serial1 >> 24)
b[0x2067] = byte(serial1 >> 16)
b[0x7580] = byte(serial2 >> 24)
b[0x7581] = byte(serial2 >> 16)
b[0x2060] = byte(serial1 >> 8)
b[0x2061] = byte(serial1)
b[0x2200] = byte(serial2 >> 8)
b[0x2201] = byte(serial2)
var chksum uint16 = 0xffff
for i := 2; i < 0x8000; i++ {
chksum ^= uint16(b[i])
for j := 8; j > 0; j-- {
if chksum&1 == 1 {
chksum = (chksum >> 1) ^ 0x8408
} else {
chksum >>= 1
}
}
}
b[0x0000] = byte(^chksum >> 8)
b[0x0001] = byte(^chksum)
return bytes.NewReader(b), nil
}
const (
offsetPSO12 = 0x00
offsetPSO3 = 0x10
)
func patchPSO12(r io.Reader, mc *memoryCard) (io.Reader, error) {
return patchPSO(r, mc, offsetPSO12)
}
func patchPSO3(r io.Reader, mc *memoryCard) (io.Reader, error) {
return patchPSO(r, mc, offsetPSO3)
}
//nolint:gomnd
func patchPSO(r io.Reader, mc *memoryCard, offset int) (io.Reader, error) {
serial1, serial2 := mc.serialNumbers()
b, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("unable to read save data: %w", err)
}
if len(b) < lengthPSO {
return nil, errInvalidLength
}
b[0x2158] = byte(serial1 >> 24)
b[0x2159] = byte(serial1 >> 16)
b[0x215a] = byte(serial1 >> 8)
b[0x215b] = byte(serial1)
b[0x215c] = byte(serial2 >> 24)
b[0x215d] = byte(serial2 >> 16)
b[0x215e] = byte(serial2 >> 8)
b[0x215f] = byte(serial2)
lut := make([]uint32, 256)
for i := 0; i < 256; i++ {
chksum := uint32(i)
for j := 8; j > 0; j-- {
if chksum&1 == 1 {
chksum = (chksum >> 1) ^ 0xedb88320
} else {
chksum >>= 1
}
}
lut[i] = chksum
}
var chksum uint32 = 0xdebb20e3
for i := 0x204c; i < 0x2164+offset; i++ {
chksum = (chksum>>8)&0xffffff ^ lut[byte(chksum)^b[i]]
}
chksum ^= 0xffffffff
b[0x2048] = byte(chksum >> 24)
b[0x2049] = byte(chksum >> 16)
b[0x204a] = byte(chksum >> 8)
b[0x204b] = byte(chksum)
return bytes.NewReader(b), nil
}