-
Notifications
You must be signed in to change notification settings - Fork 0
/
title.go
120 lines (98 loc) · 2.05 KB
/
title.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"errors"
"io/ioutil"
"os"
)
var (
errInvalidMagic = errors.New("title: invalid file magic")
errUnknownVersion = errors.New("title: unknown version")
errSizeMismatch = errors.New("title: buffer size mismatch")
)
type rglTitle struct {
Magic []byte
Version uint32
Length uint32
Data []byte
}
func ReadTitleFromFile(filePath string) (*rglTitle, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return ReadTitleFromBuffer(content)
}
func ReadTitleFromBuffer(content []byte) (*rglTitle, error) {
buffer := bytes.NewBuffer(content)
reader := NewReader(buffer)
magic := make([]byte, 4)
_, err := reader.Read(magic)
if err != nil {
return nil, err
}
if string(magic) != "RGLM" {
return nil, errInvalidMagic
}
version, err := reader.ReadUint32()
if err != nil {
return nil, err
}
length, err := reader.ReadUint32()
if err != nil {
return nil, err
}
if version != 1 || length > uint32(len(content)) {
return nil, errUnknownVersion
}
// Hardcoded offset?
reader.SetOffset(0x50)
data := make([]byte, length)
size, err := reader.Read(data)
if err != nil {
return nil, err
}
if size != int(length) {
return nil, errSizeMismatch
}
return &rglTitle{
Magic: magic,
Version: version,
Length: length,
Data: data,
}, nil
}
func (title *rglTitle) decrypt() string {
// Key and IV are both empty
key := make([]byte, 32)
iv := make([]byte, 16)
block, err := aes.NewCipher(key)
if err != nil {
return ""
}
buffer := make([]byte, title.Length)
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(buffer, title.Data)
content := string(buffer)
// FIXME: hacky trimming
for i := 0; i < len(content); i += 1 {
if content[i] == '{' {
content = content[i : len(content)-1]
break
}
}
for i := len(content) - 1; i > 0; i -= 1 {
if content[i] == '}' {
content = content[:i+1]
break
}
}
return content
}