/
convert.go
51 lines (40 loc) · 913 Bytes
/
convert.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
package net
import (
"encoding/binary"
"unsafe"
"github.com/bio-routing/tflow2/convert"
)
var (
localEndianness binary.ByteOrder
)
func init() {
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)
switch buf {
case [2]byte{0xCD, 0xAB}:
localEndianness = binary.LittleEndian
case [2]byte{0xAB, 0xCD}:
localEndianness = binary.BigEndian
default:
panic("Could not determine native endianness.")
}
}
// Htons converts a local short uint to an network order short uint
func Htons(x uint16) uint16 {
if localEndianness == binary.BigEndian {
return x
}
xp := unsafe.Pointer(&x)
b := (*[2]byte)(xp)
tmp := b[0]
b[0] = b[1]
b[1] = tmp
return *(*uint16)(xp)
}
// BigEndianToLocal converts input from big endian to "local" endian
func BigEndianToLocal(input []byte) []byte {
if localEndianness == binary.BigEndian {
return input
}
return convert.Reverse(input)
}