Skip to content

Commit

Permalink
Merge pull request Mrs4s#210 from fumiama/master
Browse files Browse the repository at this point in the history
fix(Mrs4s#209): OpenWriterF encode error
  • Loading branch information
Mrs4s committed Dec 4, 2021
2 parents a2e7548 + d3ab596 commit d094d04
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
1 change: 0 additions & 1 deletion binary/jce/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (r *JceReader) skipHead() {
l = 2
}
r.off += l
return
}

func (r *JceReader) skip(l int) {
Expand Down
7 changes: 2 additions & 5 deletions binary/jce/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,6 @@ func (pkt *RequestPacket) ToBytes() []byte {
}

func (pkt *RequestPacket) ReadFrom(r *JceReader) {
pkt.SBuffer = []byte{}
pkt.Context = make(map[string]string)
pkt.Status = make(map[string]string)
pkt.IVersion = r.ReadInt16(1)
pkt.CPacketType = r.ReadByte(2)
pkt.IMessageType = r.ReadInt32(3)
Expand Down Expand Up @@ -596,9 +593,9 @@ func (pkt *FileStoragePushFSSvcList) ReadFrom(r *JceReader) {
pkt.QZoneProxyServiceList = r.ReadFileStorageServerInfos(3)
pkt.UrlEncodeServiceList = r.ReadFileStorageServerInfos(4)
pkt.BigDataChannel = &BigDataChannel{}
pkt.VipEmotionList = r.ReadFileStorageServerInfos(5)
pkt.C2CPicDownList = r.ReadFileStorageServerInfos(7)
r.ReadJceStruct(pkt.BigDataChannel, 5)
pkt.VipEmotionList = r.ReadFileStorageServerInfos(6)
pkt.C2CPicDownList = r.ReadFileStorageServerInfos(7)
pkt.PttList = r.ReadBytes(10)
}

Expand Down
12 changes: 6 additions & 6 deletions binary/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

type TEA [4]uint32

// Uint32 returns a lock free uint32 value.
//go:linkname Uint32 runtime.fastrand
func Uint32() uint32
// randuint32 returns a lock free uint32 value.
//go:linkname randuint32 runtime.fastrand
func randuint32() uint32

// Encrypt tea 加密
// http://bbs.chinaunix.net/thread-583468-1-1.html
Expand All @@ -18,9 +18,9 @@ func (t TEA) Encrypt(src []byte) (dst []byte) {
lens := len(src)
fill := 10 - (lens+1)%8
dst = make([]byte, fill+lens+7)
binary.LittleEndian.PutUint32(dst, Uint32())
binary.LittleEndian.PutUint32(dst[4:], Uint32())
binary.LittleEndian.PutUint32(dst[8:], Uint32())
binary.LittleEndian.PutUint32(dst, randuint32())
binary.LittleEndian.PutUint32(dst[4:], randuint32())
binary.LittleEndian.PutUint32(dst[8:], randuint32())
dst[0] = byte(fill-3) | 0xF8 // 存储pad长度
copy(dst[fill:], src)

Expand Down
11 changes: 5 additions & 6 deletions binary/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ func NewWriterF(f func(writer *Writer)) []byte {
return b
}

// OpenWriterF must call func close
func OpenWriterF(f func(writer *Writer)) (b []byte, close func()) {
// OpenWriterF must call func cl to close
func OpenWriterF(f func(*Writer)) (b []byte, cl func()) {
w := SelectWriter()
w.WriteByte(0)
f(w)
return w.Bytes(), func() { PutWriter(w) }
}
Expand Down Expand Up @@ -80,11 +79,11 @@ func (w *Writer) EncryptAndWrite(key []byte, data []byte) {
w.Write(NewTeaCipher(key).Encrypt(data))
}

func (w *Writer) WriteIntLvPacket(offset int, f func(writer *Writer)) {
data, close := OpenWriterF(f)
func (w *Writer) WriteIntLvPacket(offset int, f func(*Writer)) {
data, cl := OpenWriterF(f)
w.WriteUInt32(uint32(len(data) + offset))
w.Write(data)
close()
cl()
}

func (w *Writer) WriteUniPacket(commandName string, sessionId, extraData, body []byte) {
Expand Down
24 changes: 12 additions & 12 deletions client/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ func (c *QQClient) buildQRCodeLoginPacket(t106, t16a, t318 []byte) (uint16, []by

w.Write(tlv.T18(16, uint32(c.Uin)))
w.Write(tlv.T1(uint32(c.Uin), c.deviceInfo.IpAddress))
wb, cl := binary.OpenWriterF(func(w *binary.Writer) {
w.WriteUInt16(0x106)
w.WriteBytesShort(t106)
wb, cl := binary.OpenWriterF(func(bw *binary.Writer) {
bw.WriteUInt16(0x106)
bw.WriteBytesShort(t106)
})
w.Write(wb)
cl()
Expand All @@ -196,9 +196,9 @@ func (c *QQClient) buildQRCodeLoginPacket(t106, t16a, t318 []byte) (uint16, []by

w.Write(tlv.T145(c.deviceInfo.Guid))
w.Write(tlv.T147(16, []byte(c.version.SortVersionName), c.version.ApkSign))
wb, cl = binary.OpenWriterF(func(w *binary.Writer) {
w.WriteUInt16(0x16A)
w.WriteBytesShort(t16a)
wb, cl = binary.OpenWriterF(func(bw *binary.Writer) {
bw.WriteUInt16(0x16A)
bw.WriteBytesShort(t16a)
})
w.Write(wb)
cl()
Expand All @@ -223,9 +223,9 @@ func (c *QQClient) buildQRCodeLoginPacket(t106, t16a, t318 []byte) (uint16, []by
w.Write(tlv.T516())
w.Write(tlv.T521(8))
// w.Write(tlv.T525(tlv.T536([]byte{0x01, 0x00})))
wb, cl = binary.OpenWriterF(func(w *binary.Writer) {
w.WriteUInt16(0x318)
w.WriteBytesShort(t318)
wb, cl = binary.OpenWriterF(func(bw *binary.Writer) {
bw.WriteUInt16(0x318)
bw.WriteBytesShort(t318)
})
w.Write(wb)
cl()
Expand Down Expand Up @@ -312,9 +312,9 @@ func (c *QQClient) buildRequestTgtgtNopicsigPacket() (uint16, []byte) {

w.Write(tlv.T18(16, uint32(c.Uin)))
w.Write(tlv.T1(uint32(c.Uin), c.deviceInfo.IpAddress))
wb, cl := binary.OpenWriterF(func(w *binary.Writer) {
w.WriteUInt16(0x106)
w.WriteBytesShort(c.sigInfo.encryptedA1)
wb, cl := binary.OpenWriterF(func(bw *binary.Writer) {
bw.WriteUInt16(0x106)
bw.WriteBytesShort(c.sigInfo.encryptedA1)
})
w.Write(wb)
cl()
Expand Down

0 comments on commit d094d04

Please sign in to comment.