-
Notifications
You must be signed in to change notification settings - Fork 3
/
primitive_decoder.go
64 lines (50 loc) · 1.73 KB
/
primitive_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
package y3
import (
"errors"
"fmt"
"github.com/yomorun/y3-codec-golang/pkg/encoding"
"github.com/yomorun/y3-codec-golang/internal/mark"
"github.com/yomorun/y3-codec-golang/internal/utils"
)
// DecodePrimitivePacket parse out whole buffer to a PrimitivePacket
//
// Examples:
// [0x01, 0x01, 0x01] -> Key=0x01, Value=0x01
// [0x41, 0x06, 0x03, 0x01, 0x61, 0x04, 0x01, 0x62] -> key=0x03, value=0x61; key=0x04, value=0x62
func DecodePrimitivePacket(buf []byte) (packet *PrimitivePacket, endPos int, sizeL int, err error) {
logger := utils.Logger.WithPrefix(utils.DefaultLogger, "BasePacket::Decode")
logger.Debugf("buf=%#X", buf)
if buf == nil || len(buf) < primitivePacketBufferMinimalLength {
return nil, 0, 0, errors.New("invalid y3 packet minimal size")
}
p := &PrimitivePacket{valBuf: buf}
var pos = 0
// first byte is `Tag`
p.tag = mark.NewTag(buf[pos])
pos++
// read `Varint` from buf for `Length of value`
tmpBuf := buf[pos:]
var bufLen int32
codec := encoding.VarCodec{}
err = codec.DecodePVarInt32(tmpBuf, &bufLen)
if err != nil {
return nil, 0, 0, err
}
sizeL = codec.Size
if sizeL < 1 {
return nil, 0, sizeL, errors.New("malformed, size of Length can not smaller than 1")
}
// 根据文档表述,p.length指的是value的长度,所以修改为bufLen的值
//p.length = uint32(len)
//pos += int(bufLen)
p.length = uint32(bufLen)
pos += sizeL
endPos = pos + int(p.length)
logger.Debugf(">>> sizeL=%v, length=%v, pos=%v, endPos=%v", sizeL, p.length, pos, endPos)
if pos > endPos || endPos > len(buf) || pos > len(buf) {
return nil, 0, sizeL, fmt.Errorf("beyond the boundary, pos=%v, endPos=%v", pos, endPos)
}
p.valBuf = buf[pos:endPos]
logger.Debugf("valBuf = %#X", p.valBuf)
return p, endPos, sizeL, nil
}