-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_binary.go
73 lines (60 loc) · 1.17 KB
/
decode_binary.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
package msgpack
func UnmarshalBinary(data []byte, v *[]byte) error {
return NewBytesDecoder(data).DecodeBinary(v)
}
func (d *Decoder) DecodeBinary(v *[]byte) error {
if v == nil {
return ErrDecodeNil
}
return d.decodeBinary(&v)
}
func UnmarshalNullableBinary(data []byte, v **[]byte) error {
return NewBytesDecoder(data).DecodeNullableBinary(v)
}
func (d *Decoder) DecodeNullableBinary(v **[]byte) error {
if v == nil {
return ErrDecodeNil
}
if *v != nil {
*v = nil
}
return d.decodeBinary(v)
}
func (d *Decoder) decodeBinaryHeader(c byte) (int, error) {
switch c {
case Binary8:
n, err := d.nextByte()
return int(n), err
case Binary16:
n, err := d.nextUint16()
return int(n), err
case Binary32:
n, err := d.nextUint32()
return int(n), err
default:
return 0, d.unexpectedByteErr(c)
}
}
func (d *Decoder) decodeBinary(v **[]byte) error {
c, err := d.nextByte()
if err != nil {
return err
}
if *v == nil {
if c != Nil {
*v = new([]byte)
} else {
return nil
}
}
dataLen, err := d.decodeBinaryHeader(c)
if err != nil {
return err
}
data, err := d.nextByteN(dataLen)
if err != nil {
return err
}
**v = data
return nil
}