forked from Cistern/sflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.go
98 lines (81 loc) · 1.69 KB
/
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sflow
import (
"encoding/binary"
"errors"
"math"
)
var (
ErrInvalidSliceLength = errors.New("sflow: invalid slice length")
ErrInvalidFieldType = errors.New("sflow: field type")
)
// readFields reads big-endian encoded numbers from b into
// elements of fields, which should be pointers to numbers,
// e.g. *uint32 or *float32.
func readFields(b []byte, fields []interface{}) error {
for len(b) > 0 && len(fields) > 0 {
field := fields[0]
size := 0
switch field.(type) {
case *int8, *uint8:
// 1 byte
if len(b) < 1 {
return ErrInvalidSliceLength
}
size = 1
switch field := field.(type) {
case *int8:
*field = int8(b[0])
case *uint8:
*field = b[0]
}
case *int16, *uint16:
// 2 bytes
if len(b) < 2 {
return ErrInvalidSliceLength
}
size = 2
n := binary.BigEndian.Uint16(b[:size])
switch field := field.(type) {
case *int16:
*field = int16(n)
case *uint16:
*field = n
}
case *float32, *int32, *uint32:
// 4 bytes
if len(b) < 4 {
return ErrInvalidSliceLength
}
size = 4
n := binary.BigEndian.Uint32(b[:size])
switch field := field.(type) {
case *float32:
*field = math.Float32frombits(n)
case *int32:
*field = int32(n)
case *uint32:
*field = n
}
case *float64, *int64, *uint64:
// 8 bytes
if len(b) < 8 {
return ErrInvalidSliceLength
}
size = 8
n := binary.BigEndian.Uint64(b[:size])
switch field := field.(type) {
case *float64:
*field = math.Float64frombits(n)
case *int64:
*field = int64(n)
case *uint64:
*field = n
}
default:
return ErrInvalidFieldType
}
b = b[size:]
fields = fields[1:]
}
return nil
}