Skip to content

Commit

Permalink
temp fix for decoder (Layer 173)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Feb 4, 2024
1 parent c8119af commit 4a84ac4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
7 changes: 3 additions & 4 deletions internal/encoding/tl/cursor_r.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"math"
"reflect"

Expand All @@ -26,7 +25,7 @@ type Decoder struct {
// 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)
data, err := io.ReadAll(r)
if err != nil {
return nil, errors.Wrap(err, "reading data before decoding")
}
Expand Down Expand Up @@ -152,11 +151,11 @@ func (d *Decoder) PopInt() int32 {
}

func (d *Decoder) GetRestOfMessage() ([]byte, error) {
return ioutil.ReadAll(d.buf)
return io.ReadAll(d.buf)
}

func (d *Decoder) DumpWithoutRead() ([]byte, error) {
data, err := ioutil.ReadAll(d.buf)
data, err := io.ReadAll(d.buf)
if err != nil {
return nil, err
}
Expand Down
60 changes: 39 additions & 21 deletions internal/encoding/tl/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,20 +277,28 @@ func (d *Decoder) decodeValueGeneral(value reflect.Value) any {
func (d *Decoder) decodeRegisteredObject() Object {
crc := d.PopCRC()
if d.err != nil {
d.err = errors.Wrap(d.err, "read crc")
d.err = errors.Wrap(d.err, "reading crc")
}

var _typ reflect.Type

switch crc {
case CrcVector:
_parsedExplicit := false
if len(d.expectedTypes) == 0 {
d.expectedTypes = append(d.expectedTypes, reflect.TypeOf([]bool{}))
_parsedExplicit = true
//d.err = &ErrMustParseSlicesExplicitly{}
//return nil
d.PopUint() // pop vector length
vecCrc := d.PopCRC() // read the crc of the vector<...>

if vecCrc == CrcTrue || vecCrc == CrcFalse {
d.expectedTypes = append(d.expectedTypes, reflect.TypeOf([]bool{}))
} else {
d.expectedTypes = append(d.expectedTypes, reflect.TypeOf([]Object{}))
crc = vecCrc
}

// seek back to initial position
d.buf.Seek(-8, 1)
}

_typ = d.expectedTypes[0]
d.expectedTypes = d.expectedTypes[1:]

Expand All @@ -299,21 +307,32 @@ func (d *Decoder) decodeRegisteredObject() Object {
return nil
}

if _parsedExplicit {
switch res := res.(type) {
case []bool:
if len(res) > 0 {
switch res[0] {
case true:
return &PseudoTrue{}
case false:
return &PseudoFalse{}
}
switch res := res.(type) {
case []bool:
if len(res) > 0 {
switch res[0] {
case true:
return &PseudoTrue{}
case false:
return &PseudoFalse{}
}
}
}
case []Object:
if len(res) == 0 {
return &PseudoNil{}
}

return &WrappedSlice{res}
if _typ, ok := objectByCrc[crc]; ok {
_v := reflect.MakeSlice(reflect.SliceOf(_typ), 0, 0)
for _, o := range res {
_v = reflect.Append(_v, reflect.ValueOf(o).Convert(_typ))
}

return &WrappedSlice{data: _v.Interface()}
}
default:
return &WrappedSlice{data: res}
}

case CrcFalse:
return &PseudoFalse{}
Expand All @@ -325,10 +344,9 @@ func (d *Decoder) decodeRegisteredObject() Object {
return &PseudoNil{}
}

var ok bool
_typ, ok = objectByCrc[crc]
if !ok {
_typ, ok := objectByCrc[crc]

if !ok {
msg, err := d.DumpWithoutRead()
if err != nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion telegram/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package telegram
import "regexp"

const (
ApiVersion = 172
ApiVersion = 173
Version = "v2.3.7"

LogDebug = "debug"
Expand Down
8 changes: 8 additions & 0 deletions telegram/methods_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4a84ac4

Please sign in to comment.