-
Notifications
You must be signed in to change notification settings - Fork 7
/
decoder.go
executable file
·264 lines (235 loc) · 5.76 KB
/
decoder.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package wkproto
import "fmt"
// var (
// decOne = sync.Pool{
// New: func() interface{} {
// return make([]byte, 1)
// },
// }
// decTwo = sync.Pool{
// New: func() interface{} {
// return make([]byte, 2)
// },
// }
// decFour = sync.Pool{
// New: func() interface{} {
// return make([]byte, 4)
// },
// }
// decEight = sync.Pool{
// New: func() interface{} {
// return make([]byte, 8)
// },
// }
// )
// var decoderPool = sync.Pool{
// New: func() any {
// return &Decoder{}
// },
// }
// Decoder 解码
type Decoder struct {
p []byte
offset int
}
// NewDecoder NewDecoder
func NewDecoder(p []byte) *Decoder {
return &Decoder{
p: p,
}
}
// Len 长度
func (d *Decoder) Len() int {
return len(d.p) - d.offset
}
// Int Int
// func (d *Decoder) Int() (int, error) {
// b := decOne.Get().([]byte)
// defer func() {
// decOne.Put(b)
// }()
// if n, err := d.r.Read(b); err != nil {
// return 0, err
// } else if n != 1 {
// return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of 1", n)
// }
// return int(b[0]), nil
// }
// Uint8 Uint8
func (d *Decoder) Uint8() (uint8, error) {
if d.offset+1 > len(d.p) {
return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", d.offset+1, len(d.p))
}
b := d.p[d.offset]
d.offset += 1
return b, nil
}
// Int16 Int16
func (d *Decoder) Int16() (int16, error) {
if d.offset+2 > len(d.p) {
return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", d.offset+2, len(d.p))
}
b := d.p[d.offset : d.offset+2]
d.offset += 2
return (int16(b[0]) << 8) | int16(b[1]), nil
}
// Uint16 Uint16
func (d *Decoder) Uint16() (uint16, error) {
if i, err := d.Int16(); err != nil {
return 0, err
} else {
return uint16(i), nil
}
}
// Bytes Bytes
func (d *Decoder) Bytes(num int) ([]byte, error) {
if d.offset+num > len(d.p) {
return nil, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", d.offset+num, len(d.p))
}
b := d.p[d.offset : d.offset+num]
d.offset += num
return b, nil
}
// Int64 Int64
func (d *Decoder) Int64() (int64, error) {
if d.offset+8 > len(d.p) {
return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", d.offset+8, len(d.p))
}
b := d.p[d.offset : d.offset+8]
d.offset += 8
return (int64(b[0]) << 56) | (int64(b[1]) << 48) | (int64(b[2]) << 40) | int64(b[3])<<32 | int64(b[4])<<24 | int64(b[5])<<16 | int64(b[6])<<8 | int64(b[7]), nil
}
// Uint64 Uint64
func (d *Decoder) Uint64() (uint64, error) {
if d.offset+8 > len(d.p) {
return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", d.offset+8, len(d.p))
}
b := d.p[d.offset : d.offset+8]
d.offset += 8
return (uint64(b[0]) << 56) | (uint64(b[1]) << 48) | (uint64(b[2]) << 40) | uint64(b[3])<<32 | uint64(b[4])<<24 | uint64(b[5])<<16 | uint64(b[6])<<8 | uint64(b[7]), nil
}
// Int32 Int32
func (d *Decoder) Int32() (int32, error) {
if d.offset+4 > len(d.p) {
return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", d.offset+4, len(d.p))
}
b := d.p[d.offset : d.offset+4]
d.offset += 4
return (int32(b[0]) << 24) | (int32(b[1]) << 16) | (int32(b[2]) << 8) | int32(b[3]), nil
}
// Uint32 Uint32
func (d *Decoder) Uint32() (uint32, error) {
i, err := d.Int32()
if err != nil {
return 0, err
}
return uint32(i), nil
}
func (d *Decoder) String() (string, error) {
if buf, err := d.Binary(); err != nil {
return "", err
} else {
return string(buf), nil
}
}
// StringAll StringAll
func (d *Decoder) StringAll() (string, error) {
if buf, err := d.BinaryAll(); err != nil {
return "", err
} else {
return string(buf), nil
}
}
// Binary Binary
func (d *Decoder) Binary() ([]byte, error) {
size, err := d.Int16()
if err != nil {
return nil, err
}
if size < 0 {
return nil, fmt.Errorf("size is less than 0, size: %d", size)
}
if d.offset+int(size) > len(d.p) {
return nil, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", d.offset+int(size), len(d.p))
}
b := d.p[d.offset : d.offset+int(size)]
d.offset += int(size)
return b, nil
}
// BinaryAll BinaryAll
func (d *Decoder) BinaryAll() ([]byte, error) {
remains := d.Len()
b := d.p[d.offset:]
d.offset += remains
return b, nil
}
// Variable Variable
func (d *Decoder) Variable() (uint64, error) {
var (
size uint64
mul uint64 = 1
)
for {
i, err := d.Uint8()
if err != nil {
return 0, err
}
size += uint64(i&0x7F) * mul
mul *= 0x80
if i&0x80 == 0 {
break
}
}
return size, nil
}
// // Int16 Int16
// func Int16(reader io.Reader) (int16, error) {
// b := decTwo.Get().([]byte)
// defer func() {
// decTwo.Put(b)
// }()
// if n, err := reader.Read(b); err != nil {
// return 0, err
// } else if n != 2 {
// return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of 2", n)
// }
// return (int16(b[0]) << 8) | int16(b[1]), nil
// }
// // Binary Binary
// func Binary(reader io.Reader) ([]byte, error) {
// size, err := Int16(reader)
// if err != nil {
// return nil, err
// }
// buf := make([]byte, uint16(size))
// if size == 0 {
// return buf, nil
// }
// if n, err := reader.Read(buf); err != nil {
// return nil, err
// } else if n != int(size) {
// return nil, fmt.Errorf("Decoder couldn't read expect bytes %d of %d", n, size)
// }
// return buf, nil
// }
// // Int32 Int32
// func Int32(reader io.Reader) (int32, error) {
// b := decFour.Get().([]byte)
// defer func() {
// decFour.Put(b)
// }()
// if n, err := reader.Read(b); err != nil {
// return 0, err
// } else if n != 4 {
// return 0, fmt.Errorf("Decoder couldn't read expect bytes %d of 4", n)
// }
// return (int32(b[0]) << 24) | (int32(b[1]) << 16) | (int32(b[2]) << 8) | int32(b[3]), nil
// }
// Uint32 Uint32
// func Uint32(reader io.Reader) (uint32, error) {
// if i, err := Int32(reader); err != nil {
// return 0, err
// } else {
// return uint32(i), nil
// }
// }