Newer
Older
package decode
import (
"bytes"
"encoding/binary"
)
// Decode reads fields from a buffer
func Decode(buf *bytes.Buffer, fields []interface{}) error {
var err error
for _, field := range fields {
err = binary.Read(buf, binary.BigEndian, field)
if err != nil {
return errors.Wrap(err, "Unable to read from buffer")
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
func DecodeUint8(buf *bytes.Buffer, x *uint8) error {
y, err := buf.ReadByte()
if err != nil {
return err
}
*x = y
return nil
}
func DecodeUint16(buf *bytes.Buffer, x *uint16) error {
a, err := buf.ReadByte()
if err != nil {
return err
}
b, err := buf.ReadByte()
if err != nil {
return err
}
*x = uint16(a)*256 + uint16(b)
return nil
}
func DecodeUint32(buf *bytes.Buffer, x *uint32) error {
a, err := buf.ReadByte()
if err != nil {
return err
}
b, err := buf.ReadByte()
if err != nil {
return err
}
c, err := buf.ReadByte()
if err != nil {
return err
}
d, err := buf.ReadByte()
if err != nil {
return err
}
*x = uint32(a)*256*256*256 + uint32(b)*256*256*256 + uint32(c)*256 + uint32(d)
return nil
}