-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCInPacket.go
More file actions
234 lines (209 loc) · 4.67 KB
/
CInPacket.go
File metadata and controls
234 lines (209 loc) · 4.67 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
227
228
229
230
231
232
233
234
package msnet
import (
"fmt"
"log/slog"
"github.com/zhyonc/msnet/enum"
"github.com/zhyonc/msnet/internal/crypt"
"strings"
"time"
)
type iPacket struct {
RawSeq uint16
DataLen int
RecvBuff []byte
Length int
Offset int
}
func NewCInPacket(buf []byte) CInPacket {
p := &iPacket{}
p.RecvBuff = buf
p.Length = len(buf)
return p
}
// AppendBuffer implements CInPacket.
func (p *iPacket) AppendBuffer(pBuff []byte, bEnc bool) {
// Decode packet length
p.RecvBuff = pBuff
p.Length = len(pBuff)
p.Offset = 0
p.RawSeq = uint16(p.Decode2())
temp := uint16(p.Decode2())
if !gSetting.IsXORCipher && bEnc {
temp ^= p.RawSeq
}
p.DataLen = int(temp)
}
// DecryptData implements CInPacket.
func (p *iPacket) DecryptData(dwKey []byte) {
if p.Length <= 0 && p.Length > maxDataLength {
slog.Warn("Invalid data length")
return
}
if gSetting.IsXORCipher {
(*crypt.XORCipher).Decrypt(nil, p.RecvBuff, dwKey)
return
}
// Switch AESKey
var aesKey [32]byte
if gSetting.IsCycleAESKey {
aesKey = crypt.GetCycleAESKey(gSetting.MSRegion, gSetting.MSVersion)
} else {
aesKey = gSetting.AESKeyDecrypt
}
// Decrypt packet data
(*crypt.CAESCipher).Decrypt(nil, aesKey, p.RecvBuff, dwKey)
if gSetting.MSRegion > enum.TMS || (gSetting.MSRegion == enum.CMS && gSetting.MSVersion < 86) {
(*crypt.CIOBufferManipulator).De(nil, p.RecvBuff)
}
}
// GetType implements CInPacket.
func (p *iPacket) GetType() uint16 {
if len(p.RecvBuff) >= 2 {
return uint16(p.RecvBuff[0]) | uint16(p.RecvBuff[1])<<8
}
return 0
}
// GetTypeByte implements CInPacket.
func (p *iPacket) GetTypeByte() uint8 {
if len(p.RecvBuff) >= 1 {
return uint8(p.RecvBuff[0])
}
return 0
}
// GetRemain implements CInPacket.
func (p *iPacket) GetRemain() int {
return p.Length - p.Offset
}
// GetOffset implements CInPacket.
func (p *iPacket) GetOffset() int {
return p.Offset
}
// GetLength implements CInPacket.
func (p *iPacket) GetLength() int {
return p.Length
}
// DecodeBool implements CInPacket.
func (p *iPacket) DecodeBool() bool {
return p.Decode1() == 1
}
// Decode1 implements CInPacket.
func (p *iPacket) Decode1() int8 {
if p.GetRemain() <= 0 {
return 0
}
result := int8(p.RecvBuff[p.Offset])
p.Offset += 1
return result
}
// Decode2 implements CInPacket.
func (p *iPacket) Decode2() int16 {
if p.GetRemain() < 2 {
return 0
}
var result int16
for i := range 2 {
index := p.Offset + i
result |= int16(p.RecvBuff[index]) << (i * 8)
}
p.Offset += 2
return result
}
// Decode4 implements CInPacket.
func (p *iPacket) Decode4() int32 {
if p.GetRemain() < 4 {
return 0
}
var result int32
for i := range 4 {
index := p.Offset + i
result |= int32(p.RecvBuff[index]) << (i * 8)
}
p.Offset += 4
return result
}
// Decode8 implements CInPacket.
func (p *iPacket) Decode8() int64 {
if p.GetRemain() < 8 {
return 0
}
var result int64
for i := range 8 {
index := p.Offset + i
result |= int64(p.RecvBuff[index]) << (i * 8)
}
p.Offset += 8
return result
}
// DecodeFT implements CInPacket.
func (p *iPacket) DecodeFT() time.Time {
// FileTime is in 100-nanosecond intervals
// Convert to nanoseconds by multiplying by 100
// FileTime epoch is January 1, 1601
// Unix epoch is January 1, 1970
// Calculate the difference between the two in nanoseconds
ft := p.Decode8()
nano := (ft - fileTimeEpochDiff) * 100
return time.Unix(0, nano)
}
// DecodeStr implements CInPacket.
func (p *iPacket) DecodeStr() string {
if p.GetRemain() < 2 {
return ""
}
strLen := p.Decode2()
if p.GetRemain() < int(strLen) {
return ""
}
start := p.Offset
end := p.Offset + int(strLen)
str := string(p.RecvBuff[start:end])
p.Offset = end
return str
}
// DecodeLocalStr implements CInPacket.
func (p *iPacket) DecodeLocalStr() string {
strLen := p.Decode2()
buf := p.DecodeBuffer(int(strLen))
return GetLangStr(buf)
}
// DecodeLocalName implements CInPacket.
func (p *iPacket) DecodeLocalName() string {
buf := p.DecodeBuffer(13)
return GetLangStr(buf)
}
// DecodeBuffer implements CInPacket.
func (p *iPacket) DecodeBuffer(uSize int) []byte {
if p.GetRemain() < uSize {
return nil
}
if uSize < 0 {
uSize = 0
}
result := make([]byte, uSize)
for i := range uSize {
result[i] = byte(p.Decode1())
}
return result
}
// DumpString implements CInPacket.
func (p *iPacket) DumpString(nSize int) string {
length := len(p.RecvBuff)
if nSize <= 0 || nSize > length {
nSize = length
}
var builder strings.Builder
for i := range nSize {
v := p.RecvBuff[i]
builder.WriteString(fmt.Sprintf("%02X", v))
if i < nSize-1 {
builder.WriteString(" ")
}
}
return builder.String()
}
// Clear implements CInPacket.
func (p *iPacket) Clear() {
p.Length = 0
p.Offset = 0
p.RecvBuff = p.RecvBuff[:0]
}