-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutil.go
46 lines (34 loc) · 1.23 KB
/
util.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
package gopcap
// getUint16 takes a two-element byte slice and returns the uint16 contained within it. If flipped
// is set, assumes the byte order is reversed.
func getUint16(buf []byte, flipped bool) uint16 {
num := uint16(0)
first, second := 0, 1
if flipped {
first, second = 1, 0
}
num = (uint16(buf[first]) << 8) + uint16(buf[second])
return num
}
// getUint32 takes a four-element byte slice and returns the uint32 contained within it. If flipped
// is set, assumes the byte order is reversed.
func getUint32(buf []byte, flipped bool) uint32 {
num := uint32(0)
first, second, third, fourth := 0, 1, 2, 3
if flipped {
first, second, third, fourth = 3, 2, 1, 0
}
num = (uint32(buf[first]) << 24) + (uint32(buf[second]) << 16) + (uint32(buf[third]) << 8) + uint32(buf[fourth])
return num
}
// getInt32 takes a four-element byte slice and returns the Int32 contained within it. If flipped
// is set, assumes the byte order is reversed.
func getInt32(buf []byte, flipped bool) int32 {
num := int32(0)
first, second, third, fourth := 0, 1, 2, 3
if flipped {
first, second, third, fourth = 3, 2, 1, 0
}
num = (int32(buf[first]) << 24) + (int32(buf[second]) << 16) + (int32(buf[third]) << 8) + int32(buf[fourth])
return num
}