forked from Mrs4s/MiraiGo
/
writer.go
137 lines (114 loc) · 2.56 KB
/
writer.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
package binary
import (
"bytes"
"encoding/binary"
"encoding/hex"
"github.com/Mrs4s/MiraiGo/utils"
)
// Writer 写入
type Writer bytes.Buffer
func NewWriterF(f func(writer *Writer)) []byte {
w := SelectWriter()
f(w)
b := append([]byte(nil), w.Bytes()...)
PutWriter(w)
return b
}
// OpenWriterF must call func cl to close
func OpenWriterF(f func(*Writer)) (b []byte, cl func()) {
w := SelectWriter()
f(w)
return w.Bytes(), func() { PutWriter(w) }
}
func (w *Writer) Write(b []byte) {
(*bytes.Buffer)(w).Write(b)
}
func (w *Writer) WriteHex(h string) {
b, _ := hex.DecodeString(h)
w.Write(b)
}
func (w *Writer) WriteByte(b byte) {
(*bytes.Buffer)(w).WriteByte(b)
}
func (w *Writer) WriteUInt16(v uint16) {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, v)
w.Write(b)
}
func (w *Writer) WriteUInt32(v uint32) {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, v)
w.Write(b)
}
func (w *Writer) WriteUInt64(v uint64) {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, v)
w.Write(b)
}
func (w *Writer) WriteString(v string) {
payload := utils.S2B(v)
w.WriteUInt32(uint32(len(payload) + 4))
w.Write(payload)
}
func (w *Writer) WriteStringShort(v string) {
w.WriteBytesShort(utils.S2B(v))
}
func (w *Writer) WriteBool(b bool) {
if b {
w.WriteByte(0x01)
} else {
w.WriteByte(0x00)
}
}
func (w *Writer) EncryptAndWrite(key []byte, data []byte) {
w.Write(NewTeaCipher(key).Encrypt(data))
}
func (w *Writer) WriteIntLvPacket(offset int, f func(*Writer)) {
data, cl := OpenWriterF(f)
w.WriteUInt32(uint32(len(data) + offset))
w.Write(data)
cl()
}
func (w *Writer) WriteUniPacket(commandName string, sessionId, extraData, body []byte) {
w1 := SelectWriter()
{ // WriteIntLvPacket
w1.WriteString(commandName)
w1.WriteUInt32(8)
w1.Write(sessionId)
if len(extraData) == 0 {
w1.WriteUInt32(0x04)
} else {
w1.WriteUInt32(uint32(len(extraData) + 4))
w1.Write(extraData)
}
}
data := w1.Bytes()
w.WriteUInt32(uint32(len(data) + 4))
w.Write(data)
PutWriter(w1)
w.WriteUInt32(uint32(len(body) + 4)) // WriteIntLvPacket
w.Write(body)
}
func (w *Writer) WriteBytesShort(data []byte) {
w.WriteUInt16(uint16(len(data)))
w.Write(data)
}
func (w *Writer) WriteTlvLimitedSize(data []byte, limit int) {
if len(data) <= limit {
w.WriteBytesShort(data)
return
}
w.WriteBytesShort(data[:limit])
}
func (w *Writer) Bytes() []byte {
return (*bytes.Buffer)(w).Bytes()
}
func (w *Writer) Cap() int {
return (*bytes.Buffer)(w).Cap()
}
func (w *Writer) Reset() {
(*bytes.Buffer)(w).Reset()
}
func (w *Writer) Grow(n int) {
(*bytes.Buffer)(w).Grow(n)
}