-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecoder.go
643 lines (522 loc) · 15.3 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Copyright (c) 2022-2024 Xelaj Software
//
// This file is a part of tl package.
// See https://github.com/xelaj/tl/blob/master/LICENSE_README.md for details.
package tl
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"reflect"
"strconv"
)
func Unmarshal(b []byte, res any) error {
return NewDecoder(bytes.NewBuffer(b)).Decode(res)
}
type Decoder interface {
SetRegistry(registry Registry) Decoder
Decode(res any) error
}
type UnmarshalState interface {
// ReadWords reads words from unmarshaler with fixed size of word.
io.Reader
ReadAll() ([]byte, error)
Peek(seek, size int) ([]byte, error)
SkipBytes(int)
PopBool() (bool, error)
PopInt() (int32, error)
PopLong() (int64, error)
PopCRC() (crc32, error)
PopMessage() ([]byte, error)
}
// A decoder reads and decodes TL values from an input stream.
type decoder struct { //nolint:govet // false positive for fieldalignment
r bufio.Reader
registry Registry
endianess binary.ByteOrder
}
// NewDecoder returns a new decoder that reads from r.
func NewDecoder(r io.Reader) Decoder { return NewDecoderWithSize(r, 0) }
// NewDecoderWithSize works absolutely like NewDecoder, but it sets buffer with
// size that you want. It could be useful, when you want to debug serialized
// data. You can combine this constructor with DumpWithoutRead method, which
// returns all data, until cache of bufio will be full, or until reader reach
// end of file.
//
// To make correct cache, in most of cases, you will get length of all
// serialized message. pass this length and few more bytes (50-60 bytes will be
// enough). Note that DumpWithoutRead can't guarantee to be stable, so use it
// only for debugging.
func NewDecoderWithSize(r io.Reader, bufSize int) Decoder {
// bufio doesn't export this constant, so, we must set it manually
const bufioDefaultBufSize = 4096
if bufSize <= 0 {
bufSize = bufioDefaultBufSize
}
return &decoder{
r: *bufio.NewReaderSize(r, bufSize),
registry: DefaultRegistry(),
endianess: binary.LittleEndian,
}
}
func (d *decoder) SetRegistry(registry Registry) Decoder {
d.registry = registry
return d
}
func (d *decoder) Decode(res any) error {
if res == nil {
return ErrUnexpectedNil
}
v := reflect.ValueOf(res)
if v.Kind() != reflect.Ptr {
return fmt.Errorf("res value is not pointer as expected. got %v", v.Type())
}
// decoding elem cause we are taking pointer in res, but we'll take
// additional step to call decodeValue again. Who needs that?
return d.decodeValue(v.Elem())
}
func (d *decoder) Peek(seek, size int) ([]byte, error) {
peeked, err := d.r.Peek(seek + size)
if err != nil {
return nil, err //nolint:wrapcheck // must not be wrapped
}
res := make([]byte, size)
if l := copy(res, peeked[seek:]); l != len(res) {
// just for test, who knows
panic(fmt.Sprintf("copying bytes: expected %v, got %v", size, l))
}
return res, nil
}
func (d *decoder) SkipBytes(n int) {
// discarded will be only exact number or less, BUT with returned error
if _, err := d.r.Discard(n); err != nil {
panic(fmt.Errorf("something wrong with peeking bytes, Discard must always be ok: %w", err))
}
}
func (d *decoder) decodeValue(value reflect.Value) error {
if v, ok := value.Interface().(Unmarshaler); ok {
return v.UnmarshalTL(d)
}
var val any
var err error
switch kind := value.Kind(); kind { //nolint:exhaustive
// pointer always diving into value
case reflect.Ptr:
// need to init firstly
if value.IsZero() {
value.Set(reflect.New(value.Type().Elem()))
}
return d.decodeValue(value.Elem())
// simple types
case reflect.Float64:
val, err = d.popDouble()
case reflect.Uint64:
val, err = d.popUint()
case reflect.Int64:
val, err = d.PopLong()
case reflect.Uint32:
if _, ok := value.Interface().(Object); ok {
crc, err := d.PopCRC()
if err != nil {
return err
}
return d.decodeEnum(value, crc)
}
val, err = d.PopUint()
case reflect.Int32:
val, err = d.PopInt()
case reflect.Bool:
val, err = d.PopBool()
case reflect.String:
val, err = d.popString()
case reflect.Slice:
switch value.Type() {
case byteSliceTyp: // []byte
val, err = d.PopMessage()
default:
return d.decodeVector(value, false)
}
case reflect.Array:
if value.Type().Elem() == byteTyp { // [N]byte
return d.decodeRaw(value)
}
return d.decodeVector(value, false)
// complex types
case reflect.Struct:
return d.decodeObject(value, false)
// struct but it's map. Maps are not defined by TL, so we use them as type
// values.
case reflect.Map:
// TODO: must implement map decoding
return errors.New("map is not ordered object (must order like structs)")
// interfaces in terms of TL is a set of allowed structs.
case reflect.Interface:
if err := d.decodeInterface(value); err != nil {
return fmt.Errorf("decoding interface: %w", err)
}
return nil
default:
// unsupported at all
// Chan, Func, Uintptr, UnsafePointer
//
// supported, but TL doesn't support 8 and 16 bit numbers
// Int, Int8, Int16, Uint, Uint8, Uint16, Uint64
//
// same: supported, but not in TL, so we can't understand, how much bytes
// we need to scan.
// Float32, Complex64, Complex128
return ErrUnsupportedType{Type: value.Type()}
}
if err != nil {
return err
}
v := reflect.ValueOf(&val).Elem().Elem()
value.Set(v.Convert(value.Type()))
return nil
}
func (d *decoder) decodeRaw(v reflect.Value) error {
if v.Kind() != reflect.Array {
panic("raw must be array")
} else if v.Type().Elem() != byteTyp {
panic("raw must be array of bytes")
} else if n := v.Len(); n%WordLen != 0 {
// special case: this means that we want to take exact N of bytes and pop it from reader
// n%WordLen == 0, cause we can't read less or more than word
return fmt.Errorf("array of bytes must be divided by %v, got %v", WordLen, n)
}
val, err := d.Peek(0, v.Len())
if err != nil {
return err
}
d.SkipBytes(v.Len())
valRaw := reflect.ValueOf(&val).Elem()
arr := reflect.New(reflect.ArrayOf(v.Len(), byteTyp)).Elem()
for i := 0; i < v.Len(); i++ {
arr.Index(i).Set(valRaw.Index(i))
}
v.Set(arr)
return nil
}
// allowed values are only slice and array.
func (d *decoder) decodeVector(v reflect.Value, ignoreCRC bool) error {
if !ignoreCRC {
crc, err := d.PopCRC()
if err != nil {
return errReadCRC{err}
}
if crc != crcVector {
return fmt.Errorf("not a vector: 0x%08x, want: 0x%08x", crc, crcVector)
}
}
size, err := d.PopUint()
if err != nil {
return fmt.Errorf("read vector size: %w", err)
}
switch v.Kind() { //nolint:exhaustive // default unreachable
case reflect.Array:
if v.Len() < int(size) {
return fmt.Errorf("array size is smaller than message: got %v, want %v", v.Len(), size)
}
case reflect.Slice:
v.Set(reflect.MakeSlice(v.Type(), int(size), int(size)))
default:
unreachable()
}
for i := 0; i < int(size); i++ {
if err := d.decodeValue(v.Index(i)); err != nil {
return wrapPath(err, "["+strconv.Itoa(i)+"]")
}
}
return nil
}
func (d *decoder) decodeInterface(v reflect.Value) error {
crc, err := d.PopCRC()
if err != nil {
return errReadCRC{err}
}
if crc == crcGzipPacked {
// ! special case for gzip packed objects
//
// serialized data MIGHT place gzipped objects (and only vectors and
// objects!) in a random places, where it looks like the message is "too
// big". To handle that, here we unzipping it, and replacing original
// reader with custom one.
//
// Important: according to (https://core.telegram.org/api/invoking, it's
// "recommended" to zip large messages from client side, but telegram
// is... Telegram, so it might argue sometimes. But in any case, it
// makes no sense to implement mtproto extension of "classic" tl
// serialization as required component)
gz, err := d.popZip(true)
if err != nil {
return err
}
// replacing reader with custom, which will read from gzip reader.
old := d.r
defer func() { d.r = old }()
d.r = *bufio.NewReader(gz)
crc, err = d.PopCRC()
if err != nil {
return errReadCRC{err}
}
}
obj, ok := d.registry.ConstructObject(crc)
if !ok {
return ErrObjectNotRegistered(crc)
}
o := reflect.ValueOf(&obj).Elem()
if o.Kind() != reflect.Interface {
panic("unexpected object which is not interface")
}
if !o.Elem().IsValid() {
panic("constructor must return non empty interface")
}
if o.Elem().Kind() == reflect.Ptr && o.Elem().IsNil() {
realType := o.Elem().Type().Elem()
o.Set(reflect.New(realType))
}
if unmarshaler, ok := o.Interface().(Unmarshaler); ok {
if err := unmarshaler.UnmarshalTL(d); err != nil {
return err
}
v.Set(o)
return nil
}
o = o.Elem().Elem()
if o.Kind() != reflect.Struct {
return fmt.Errorf("object must be struct, got %v", o.Type())
}
err = d.decodeObject(o, true)
if err != nil {
return err // no need to wrap
}
// set value of o into v with interface conversion
vtyp := reflect.New(v.Type()).Elem()
if !o.Type().Implements(vtyp.Type()) {
if !o.Addr().Type().Implements(vtyp.Type()) {
panic("can't find interface implementation")
}
o = o.Addr()
}
v.Set(o)
return nil
}
func (d *decoder) decodeEnum(v reflect.Value, crc crc32) error {
enum, ok := d.registry.ConstructObject(crc)
if !ok {
return fmt.Errorf("enum 0x%08x not found", crc)
}
value := reflect.ValueOf(enum)
if v.Type() != value.Type() {
return fmt.Errorf("invalid type of enum: want %v, got %v", v.Type(), value.Type())
} else if value.Kind() != reflect.Uint32 {
panic("internal error: enum must not have fields")
}
v.Set(value)
return nil
}
// decode EXACT object. means that v must always be struct.
func (d *decoder) decodeObject(v reflect.Value, ignoreCRC bool) error {
if v.Kind() != reflect.Struct {
panic(fmt.Errorf("expected struct, got %v with %v kind", v.Type(), v.Kind()))
}
crc, ok := getCRCofObject(v)
if !ok {
return errors.New("value must implement Object interface, got: " + v.Type().String())
}
if !ignoreCRC {
gotCrc, err := d.PopCRC()
if err != nil {
return errReadCRC{err}
}
if gotCrc != crc {
return ErrInvalidCRC{Got: gotCrc, Want: crc}
}
}
bitflags := map[string]uint32{}
for i := 0; i < v.NumField(); i++ {
typ := v.Type().Field(i)
tags, err := ParseTag(typ.Tag.Get(tagName), typ.Name)
if err != nil {
panic(fmt.Sprintf("invalid tag: %v", err))
}
if tags.Ignore() {
continue
}
if tags.isBitflag() {
bits, err := d.PopUint()
if err != nil {
return fmt.Errorf("getting %v flag: %w", tags.Name, err)
}
bitflags[tags.Name] = bits
continue
}
needToDecode := true
if tags.BitFlags != nil {
f, ok := bitflags[tags.BitFlags.TargetField]
if !ok {
panic("internal error: " +
"struct is not validated on register stage: " +
"optional field was called BEFORE bitflags")
}
bitflagValue := f&(1<<tags.BitFlags.BitPosition) > 0
if tags.isImplicit() {
// implicit can be only boolean, so leave this initialization alone
v.Field(i).Set(reflect.ValueOf(bitflagValue))
continue
}
needToDecode = bitflagValue
}
if needToDecode {
// normal field
if v.Field(i).Kind() == reflect.Ptr {
v.Field(i).Set(reflect.New(v.Field(i).Type().Elem()))
}
if err := d.decodeValue(v.Field(i)); err != nil {
return wrapPath(err, v.Type().Field(i).Name)
}
}
}
return nil
}
func (d *decoder) PopInt() (int32, error) {
val, err := d.Peek(0, WordLen)
if err != nil {
return 0, err
}
d.SkipBytes(WordLen)
return int32(binary.LittleEndian.Uint32(val)), nil
}
func (d *decoder) popUint() (uint64, error) {
val, err := d.Peek(0, LongLen)
if err != nil {
return 0, err
}
d.SkipBytes(LongLen)
return binary.LittleEndian.Uint64(val), nil
}
// popCRC just an alias for self documenting code.
func (d *decoder) PopCRC() (crc32, error) { return d.PopUint() }
func (d *decoder) PopUint() (uint32, error) { return convertNumErr[uint32](d.PopInt()) }
func (d *decoder) PopLong() (int64, error) { u, err := d.popUint(); return int64(u), err }
func (d *decoder) popDouble() (float64, error) {
val, err := d.PopLong()
return math.Float64frombits(uint64(val)), err
}
func (d *decoder) PopBool() (bool, error) {
crc, err := d.PopUint()
if err != nil {
return false, err
}
switch crc {
case crcTrue:
return true, nil
case crcFalse:
return false, nil
default:
return false, ErrInvalidBoolCRC(crc)
}
}
// https://core.telegram.org/type/bytes
func (d *decoder) popString() (string, error) { return convertStrErr[string](d.PopMessage()) }
func (d *decoder) PopMessage() ([]byte, error) {
readLen := 1
buf, err := d.Peek(0, 1)
if err != nil {
return nil, err
}
// how exact bytes there is a message
var realSize int
if firstByte := buf[0]; firstByte != fuckingMagicNumber {
// it's a tiny message, so real size is exact a first byte value
realSize = int(firstByte)
} else {
// otherwise it's a large message, next three bytes are size of message
readLen = WordLen
lenBuf, errPeek := d.Peek(1, WordLen-1)
if errPeek != nil {
return nil, fmt.Errorf("reading last %v bytes of message size: %w", WordLen-1, errPeek)
}
switch d.endianess {
case binary.LittleEndian:
lenBuf = append(lenBuf, 0)
case binary.BigEndian:
lenBuf = append([]byte{0}, lenBuf...)
default:
panic("wait, what?")
}
realSize = int(d.endianess.Uint32(lenBuf))
}
d.SkipBytes(readLen)
// this buf wil be real message
buf = make([]byte, realSize)
if _, err := io.ReadFull(&d.r, buf); err != nil {
return nil, fmt.Errorf("reading message data with len of %v: %w", realSize, err)
}
d.SkipBytes(pad(readLen+realSize, WordLen))
return buf, nil
}
// ! special case for gzip packed objects
//
// serialized data MIGHT have replacements from original objects to gzipped
// objects (and only vectors and objects!) in a random places, where it looks
// like the message is "too big". To handle that, here we unzipping it, and
// replacing original reader with custom one.
//
// Important: according to https://core.telegram.org/api/invoking, it's
// "recommended" to zip large messages from client side, but telegram
// is... Telegram, so it might argue sometimes. But in any case, it
// makes no sense to implement mtproto extension of "classic" tl
// serialization as required component).
func (d *decoder) popZip(ignoreCRC bool) (io.ReadCloser, error) {
if !ignoreCRC {
gotCrc, err := d.PopCRC()
if err != nil {
return nil, errReadCRC{err}
}
if gotCrc != crcGzipPacked {
return nil, ErrInvalidCRC{Got: gotCrc, Want: crcGzipPacked}
}
}
data, err := d.PopMessage()
if err != nil {
return nil, fmt.Errorf("reading gzip value: %w", err)
}
return gzip.NewReader(bytes.NewBuffer(data))
}
func (d *decoder) Read(b []byte) (int, error) {
if len(b)%WordLen != 0 {
return 0, errors.New("value can't be divided by word length")
}
read, err := d.Peek(0, len(b))
if err != nil {
return 0, err
}
d.SkipBytes(len(b))
return copy(b, read), nil
}
func (d *decoder) ReadAll() ([]byte, error) { return io.ReadAll(&d.r) }
/*
crcV := v.MapIndex(reflect.ValueOf(MapCrcKey))
if !crcV.IsValid() {
return errors.New("can't find " + MapCrcKey + " key")
}
if !crcV.Type().ConvertibleTo(uint32Typ) {
return errors.New(MapCrcKey + ": can't convert to uint32")
}
crc = crcV.Convert(uint32Typ).Interface().(uint32)
*/
func getCRCofObject(v reflect.Value) (crc32, bool) {
if _, ok := v.Interface().(Object); !ok {
v = v.Addr()
}
if v, ok := v.Interface().(Object); ok {
return v.CRC(), true
}
return 0, false
}