forked from AmarnathCJD/gogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor_r.go
279 lines (232 loc) · 6.47 KB
/
cursor_r.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) 2023 RoseLoverX
package tl
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"math"
"reflect"
"github.com/pkg/errors"
)
// A Decoder reads and decodes TL values from an input stream.
type Decoder struct {
buf *bytes.Reader
err error
// see Decoder.ExpectTypesInInterface description
expectedTypes []reflect.Type
}
// NewDecoder returns a new decoder that reads from r.
// Unfortunately, decoder can't work with part of data, so reader must be read all before decoding.
func NewDecoder(r io.Reader) (*Decoder, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, errors.Wrap(err, "reading data before decoding")
}
return &Decoder{buf: bytes.NewReader(data)}, nil
}
// ExpectTypesInInterface defines, how decoder must parse implicit objects.
// how does expectedTypes works:
// So, imagine: you want parse []int32, but also you can get []int64, or SomeCustomType, or even [][]bool.
// How to deal it?
// expectedTypes store your predictions (like "if you got unknown type, parse it as int32, not int64")
// also, if you have predictions deeper than first unknown type, you can say decoder to use predicted vals
//
// So, next time, when you'll have strucre object with interface{} which expect contains []float64 or sort
// of — use this feature via d.ExpectTypesInInterface()
func (d *Decoder) ExpectTypesInInterface(types ...reflect.Type) {
d.expectedTypes = types
}
func (d *Decoder) read(buf []byte) {
if d.err != nil {
return
}
n, err := d.buf.Read(buf)
if err != nil {
d.unread(n)
d.err = err
return
}
if n != len(buf) {
d.unread(n)
d.err = fmt.Errorf("buffer weren't fully read: want %v bytes, got %v", len(buf), n)
return
}
}
func (d *Decoder) unread(count int) {
for i := 0; i < count; i++ {
if d.buf.UnreadByte() != nil {
return
}
}
}
func (d *Decoder) PopLong() int64 {
val := make([]byte, LongLen)
d.read(val)
if d.err != nil {
return 0
}
return int64(binary.LittleEndian.Uint64(val))
}
func (d *Decoder) PopDouble() float64 {
val := make([]byte, DoubleLen)
d.read(val)
if d.err != nil {
return 0
}
return math.Float64frombits(binary.LittleEndian.Uint64(val))
}
func (d *Decoder) PopUint() uint32 {
val := make([]byte, WordLen)
d.read(val)
if d.err != nil {
return 0
}
return binary.LittleEndian.Uint32(val)
}
func (d *Decoder) PopRawBytes(size int) []byte {
val := make([]byte, size)
d.read(val)
if d.err != nil {
return nil
}
return val
}
func (d *Decoder) PopBool() bool {
crc := d.PopUint()
if d.err != nil {
return false
}
switch crc {
case CrcTrue:
return true
case CrcFalse:
return false
default:
d.err = fmt.Errorf("not a bool value, actually: %#v", crc)
return false
}
}
func (d *Decoder) PopNull() {
crc := d.PopUint()
if d.err != nil {
return
}
if crc != CrcNull {
d.err = fmt.Errorf("not a null value, actually: %#v", crc)
return
}
}
func (d *Decoder) PopCRC() uint32 {
return d.PopUint() // я так и не понял, кажется что crc это bigendian, но видимо нет
}
func (d *Decoder) PopInt() int32 {
return int32(d.PopUint())
}
func (d *Decoder) GetRestOfMessage() ([]byte, error) {
return ioutil.ReadAll(d.buf)
}
func (d *Decoder) DumpWithoutRead() ([]byte, error) {
data, err := ioutil.ReadAll(d.buf)
if err != nil {
return nil, err
}
d.unread(len(data))
return data, nil
}
func (d *Decoder) PopVector(as reflect.Type) any {
return d.popVector(as, false)
}
func (d *Decoder) popVector(as reflect.Type, ignoreCRC bool) any {
if d.err != nil {
return nil
}
if !ignoreCRC {
crc := d.PopCRC()
if d.err != nil {
d.err = errors.Wrap(d.err, "read crc")
return nil
}
if crc != CrcVector {
d.err = fmt.Errorf("not a vector: 0x%08x, want: 0x%08x", crc, CrcVector)
return nil
}
}
size := d.PopUint()
if d.err != nil {
d.err = errors.Wrap(d.err, "read vector size")
return nil
}
x := reflect.MakeSlice(reflect.SliceOf(as), int(size), int(size))
for i := 0; i < int(size); i++ {
var val reflect.Value
if as.Kind() == reflect.Ptr {
val = reflect.New(as.Elem())
} else {
val = reflect.New(as).Elem()
}
d.decodeValue(val)
if d.err != nil {
return nil
}
x.Index(i).Set(val)
}
return x.Interface()
}
func (d *Decoder) PopMessage() []byte {
val := []byte{0}
d.read(val)
if d.err != nil {
return nil
}
// значение первого байта
firstByte := val[0]
// сколько РЕАЛЬНЫХ байт занимает сообщение. от 0 до бесконечности
var realSize int
// сколько байт занимаем число обозначающее длину массива (1 или 4)
var lenNumberSize int
if firstByte != FuckingMagicNumber {
// это tinyMessage по сути, первый байт является 8битным числом, которое представляет длину сообщения
realSize = int(firstByte)
lenNumberSize = 1
} else {
// иначе это largeMessage с блядским магитческим числом 0xfe
val = make([]byte, WordLen-1) // WordLen-1 т.к. 1 байт уже прочитали
d.read(val)
if d.err != nil {
d.err = errors.Wrapf(d.err, "reading last %v bytes of message size", WordLen-1)
return nil
}
val = append(val, 0x0) // добиваем до WordLen
realSize = int(binary.LittleEndian.Uint32(val))
lenNumberSize = WordLen
}
// этот буффер и будет уже реальным собщением
buf := make([]byte, realSize)
d.read(buf)
if d.err != nil {
d.err = errors.Wrapf(d.err, "reading message data with len of %v", realSize)
return nil
}
// lenNumberSize это сколько байт мы УЖЕ прочитали. надо узнать, сколько еще байт нам нужно дочитать,
// что бы их количество делилось на размер слова (4 байта)
readLen := lenNumberSize + realSize
if readLen%WordLen != 0 {
// читаем оставшиеся пустые байты. пустые, потому что длина слова 4 байта, может остаться 1-3 лишних
// байта
voidBytes := make([]byte, WordLen-readLen%WordLen)
d.read(voidBytes)
if d.err != nil {
d.err = errors.Wrapf(d.err, "reading %v last void bytes", WordLen-readLen%WordLen)
return nil
}
for _, b := range voidBytes {
if b != 0 {
d.err = fmt.Errorf("some of void bytes doesn't equal zero: %#v", voidBytes)
return nil
}
}
}
return buf
}