Skip to content

Commit

Permalink
Fix decode gzipPcaked bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
wubenqi committed Jan 24, 2019
1 parent 504e3e9 commit 191da44
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
8 changes: 5 additions & 3 deletions mtproto/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,17 @@ func (m *DecodeBuf) Object() (r TLObject) {

r = NewTLObjectByClassID(classID)
if r == nil {
glog.Errorf("Can't find registed classId: %d", classID)
m.err = fmt.Errorf("can't find registed classId: 0x%x", uint32(classID))
glog.Error(m.err)
return nil
}

glog.Infof("NewTLObjectByClassID, classID: %x", uint32(classID))
glog.Infof("newTLObjectByClassID, classID: 0x%x", uint32(classID))

err := r.(TLObject).Decode(m)
if err != nil {
glog.Error("Object(", classID, ") decode error: ", err, "")
m.err = err
glog.Errorf("object(0x%x) decode error: %v", uint32(classID), err)
}
return
}
Expand Down
47 changes: 26 additions & 21 deletions mtproto/parsed_manually_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/hex"
"fmt"
"github.com/golang/glog"
"io"
)

//const (
Expand Down Expand Up @@ -261,32 +262,36 @@ func (m *TLGzipPacked) EncodeToLayer(int) []byte {
}

func (m *TLGzipPacked) Decode(dbuf *DecodeBuf) error {
m.PackedData = make([]byte, 0, 4096)
data := dbuf.StringBytes()
if dbuf.err != nil {
return dbuf.err
}
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
dbuf.err = err
return fmt.Errorf("gzip read: %v", err)
}

var buf bytes.Buffer
_, _ = buf.Write(dbuf.StringBytes())
gz, _ := gzip.NewReader(&buf)

b := make([]byte, 4096)
for true {
n, _ := gz.Read(b)
if n <= 0 {
break
}
m.PackedData = append(m.PackedData, b[0:n]...)
_, err = io.Copy(&buf, gz)
clErr := gz.Close()

if err != nil {
dbuf.err = err
return fmt.Errorf("gzip read: %v", err)
}
if clErr != nil {
dbuf.err = clErr
return clErr
}

// decode
if dbuf.err == nil {
dbuf2 := NewDecodeBuf(m.PackedData)
m.Obj = dbuf2.Object()
if m.Obj == nil {
m.PackedData = buf.Bytes()

}
return dbuf2.err
} else {
return dbuf.err
}
dbuf2 := NewDecodeBuf(m.PackedData)
m.Obj = dbuf2.Object()
dbuf.err = dbuf2.err

return dbuf.err
}

///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 191da44

Please sign in to comment.