-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgssadv1.go
142 lines (111 loc) · 3.03 KB
/
rgssadv1.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
package rpgo
import (
"encoding/binary"
"fmt"
"io"
)
//RPGMakerDecrypter.Decrypter/RGSSADv1.cs
type RGSSADv1 RGSSAD
// NewRGSSADv1
//
// Creates a new RGSSADv1 structure and configures it for use.
//
// Returns a pointer to the created structure, nil on success, nil and error
// otherwise.
func NewRGSSADv1(filepath string) (*RGSSADv1, error) {
realNew, err := NewRGSSAD(filepath)
if err != nil {
return nil, err
}
created := (*RGSSADv1)(realNew)
version, err := ((*RGSSAD)(created)).GetVersion()
if version != RGASSDv1 || err != nil {
return nil, fmt.Errorf("rpgo/rgssadv1: version not v1, or err'd to this:\n%s", err.Error())
}
created.readRGSSAD()
return created, nil
}
// Reads the encrypted RGSSADv1 archive and generates a ArchivedFile slice for
// the RGSSAD structure.
//
// This function is meant for internal use in NewRGSSADv1.
func (rpg *RGSSADv1) readRGSSAD() {
key := RGASSADv1Key
t := make([]byte, 4)
_, err := rpg.ByteReader.Seek(8, io.SeekStart)
if err != nil {
return
}
for {
newArchivedFile := new(RPGMakerArchivedFile)
// NAME
_, err = rpg.ByteReader.Read(t)
if err != nil {
return
}
num := int(binary.LittleEndian.Uint32(t))
nameLen := rpg.decryptInteger(num, &key)
u := make([]byte, nameLen)
_, err = rpg.ByteReader.Read(u)
if err != nil {
return
}
newArchivedFile.Name = rpg.decryptFilename(u, &key)
// SIZE
_, err = rpg.ByteReader.Read(t)
if err != nil {
return
}
num = int(binary.LittleEndian.Uint32(t))
newArchivedFile.Size = rpg.decryptInteger(num, &key)
// OFFSET, KEY
newArchivedFile.Offset, _ = rpg.ByteReader.Seek(0, io.SeekCurrent)
newArchivedFile.Key = key
rpg.ArchivedFiles = append(rpg.ArchivedFiles, *newArchivedFile)
_, err = rpg.ByteReader.Seek(int64(newArchivedFile.Size), io.SeekCurrent)
if err != nil {
return
}
if rpg.ByteReader.Len() == 0 {
break
}
}
}
// Decrypts an integer from the RGSSADv1 archive, modifying the key for the next
// decryption afterwards.
//
// This function is meant for internal use by readRGSSAD.
//
// Returns the decrypted integer.
func (*RGSSADv1) decryptInteger(value int, key *uint) int {
result := int64(value) ^ int64(*key)
*key *= 7
*key += 3
*key &= 0xFFFFFFFF
return int(result)
}
// Decrypts a filename from the RGSSADv1 archive, modifying the key for the next
// decryption afterwards.
//
// This function is meant for internal use by readRGSSAD.
//
// Returns the decrypted filename as a string.
func (*RGSSADv1) decryptFilename(encryptedName []byte, key *uint) (decryptedName string) {
i := 0
for i < len(encryptedName) {
decryptedName += string(rune(encryptedName[i] ^ byte(*key&0xFF)))
*key *= 7
*key += 3
*key &= 0xFFFFFFFF
i++
}
return decryptedName
}
// ExtractAllFiles
//
// See ExtractAllFiles in rgssad.go.
// A wrapper for ExtractAllFiles to remove the need for end-user casting to
// *RGSSAD.
func (rpg *RGSSADv1) ExtractAllFiles(outputDirectoryPath string, overrideExisting bool) error {
return (*RGSSAD)(rpg).ExtractAllFiles(outputDirectoryPath, overrideExisting)
}