-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
226 lines (201 loc) · 5.22 KB
/
util.go
File metadata and controls
226 lines (201 loc) · 5.22 KB
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
//
// Copyright (C) 2023 Quan Chen <chenquan_act@163.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package mdx
import (
"bytes"
"compress/zlib"
"fmt"
"io"
"os"
"regexp"
"strings"
"unicode/utf16"
"github.com/c0mm4nd/go-ripemd"
"golang.org/x/text/encoding/unicode"
)
var comparableKeyPattern = regexp.MustCompile(`[\s:.,\-_'"()#<>!]+`)
var resourceSeparatorReplacer = strings.NewReplacer("/", `\`)
func decodeLittleEndianUtf16(raw []byte) (string, error) {
decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
bs2, err := decoder.Bytes(raw[:])
if err != nil {
return "", err
}
return string(bs2), nil
}
func littleEndianBinUTF16ToUTF8(bytes []byte, offset int, length int) string {
cbytes := make([]byte, length)
copy(cbytes, bytes[offset:])
wcbytes := make([]uint16, len(cbytes)/2)
for i := 0; i < len(wcbytes); i++ {
wcbytes[i] = uint16(cbytes[i*2]) | uint16(cbytes[i*2+1])<<8
}
u8 := utf16.Decode(wcbytes)
return string(u8)
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
func bigEndianBinToUTF8(bytes []byte, offset int, length int) string {
cbytes := make([]byte, length)
rawLen := len(bytes)
length = min(rawLen, length)
copy(cbytes, bytes[offset:offset+length])
return string(cbytes)
}
func beBinToU64(bin []byte) uint64 {
var n uint64 = 0
for i := 0; i < 7; i++ {
n = n | uint64(bin[i])
n = n << 8
}
n = n | uint64(bin[7])
return n
}
func beBinToU32(bin []byte) uint32 {
var n uint32 = 0
for i := 0; i < 3; i++ {
n = n | uint32(bin[i])
n = n << 8
}
n = n | uint32(bin[3])
return n
}
func beBinToU16(bin []byte) uint16 {
var n uint16 = 0
for i := 0; i < 1; i++ {
n = n | uint16(bin[i])
n = n << 8
}
n = n | uint16(bin[1])
return n
}
func beBinToU8(bin []byte) uint8 {
return uint8(bin[0] & 255)
}
func closeFile(file *os.File) {
if err := file.Close(); err != nil {
log.Warningf("failed to close file %s: %v", file.Name(), err)
}
}
func readFileFromPos(file *os.File, start, length int64) ([]byte, error) {
if start < 0 {
return nil, fmt.Errorf("invalid read start offset %d", start)
}
if length < 0 {
return nil, fmt.Errorf("invalid read length %d", length)
}
info, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("stat file before read: %w", err)
}
fileSize := info.Size()
if start > fileSize {
return nil, fmt.Errorf("read start offset %d exceeds file size %d", start, fileSize)
}
if length > fileSize-start {
return nil, fmt.Errorf(
"read length %d from offset %d exceeds file size %d",
length,
start,
fileSize,
)
}
// Set the pointer to the requested byte from the start of the file.
_, err = file.Seek(start, io.SeekStart)
if err != nil {
return nil, err
}
// Read length bytes from the current pointer position.
data := make([]byte, length)
_, err = io.ReadFull(file, data)
if err != nil {
return nil, err
}
return data, nil
}
func zlibDecompress(data []byte, from, len int64) ([]byte, error) {
b := bytes.NewReader(data[from : from+len])
z, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
p, readErr := io.ReadAll(z)
closeErr := z.Close()
if readErr != nil {
return nil, readErr
}
if closeErr != nil {
return nil, closeErr
}
return p, nil
}
/**
*
* decrypt the data, this is a helper function to invoke the fast_decrypt
* note: don't forget free comp_block !!
*
* @param comp_block compressed block buffer
* @param comp_block_len compressed block buffer size
* @return the decrypted compressed block
*/
func mdxDecrypt(compBlock []byte, compBlockLen int64) []byte {
keyBuffer := make([]byte, 8)
copy(keyBuffer, compBlock[4:8])
keyBuffer[4] = 0x95
keyBuffer[5] = 0x36
key := ripemd128bytes(keyBuffer)
fastDecrypt(compBlock[8:], key, compBlockLen-8, 16)
return compBlock
}
func fastDecrypt(data []byte, k []byte, dataLen int64, keyLen int64) {
key := k
b := data
previous := byte(0x36)
for i := int64(0); i < dataLen; i++ {
t := byte((b[i]>>4 | b[i]<<4) & 0xff)
t = t ^ previous ^ byte(i&0xff) ^ key[i%keyLen]
previous = b[i]
b[i] = t
}
}
func ripemd128bytes(data []byte) []byte {
md := ripemd.New128()
md.Write(data)
out := md.Sum(nil)
md.Reset()
return out
}
func normalizeComparableKey(value string) string {
trimmed := strings.TrimSpace(strings.ToLower(value))
if trimmed == "" {
return ""
}
return comparableKeyPattern.ReplaceAllString(trimmed, "")
}
func normalizeResourceComparableKey(value string) string {
trimmed := strings.TrimSpace(strings.ToLower(value))
if trimmed == "" {
return ""
}
trimmed = strings.TrimPrefix(trimmed, "/")
trimmed = strings.TrimPrefix(trimmed, `\`)
trimmed = resourceSeparatorReplacer.Replace(trimmed)
return trimmed
}