...
1
2
3
4
5
6
7 package route
8
9
10
11
12
13
14
15 var (
16 littleEndian binaryLittleEndian
17 bigEndian binaryBigEndian
18 )
19
20 type binaryByteOrder interface {
21 Uint16([]byte) uint16
22 Uint32([]byte) uint32
23 PutUint16([]byte, uint16)
24 PutUint32([]byte, uint32)
25 Uint64([]byte) uint64
26 }
27
28 type binaryLittleEndian struct{}
29
30 func (binaryLittleEndian) Uint16(b []byte) uint16 {
31 _ = b[1]
32 return uint16(b[0]) | uint16(b[1])<<8
33 }
34
35 func (binaryLittleEndian) PutUint16(b []byte, v uint16) {
36 _ = b[1]
37 b[0] = byte(v)
38 b[1] = byte(v >> 8)
39 }
40
41 func (binaryLittleEndian) Uint32(b []byte) uint32 {
42 _ = b[3]
43 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
44 }
45
46 func (binaryLittleEndian) PutUint32(b []byte, v uint32) {
47 _ = b[3]
48 b[0] = byte(v)
49 b[1] = byte(v >> 8)
50 b[2] = byte(v >> 16)
51 b[3] = byte(v >> 24)
52 }
53
54 func (binaryLittleEndian) Uint64(b []byte) uint64 {
55 _ = b[7]
56 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
57 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
58 }
59
60 type binaryBigEndian struct{}
61
62 func (binaryBigEndian) Uint16(b []byte) uint16 {
63 _ = b[1]
64 return uint16(b[1]) | uint16(b[0])<<8
65 }
66
67 func (binaryBigEndian) PutUint16(b []byte, v uint16) {
68 _ = b[1]
69 b[0] = byte(v >> 8)
70 b[1] = byte(v)
71 }
72
73 func (binaryBigEndian) Uint32(b []byte) uint32 {
74 _ = b[3]
75 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
76 }
77
78 func (binaryBigEndian) PutUint32(b []byte, v uint32) {
79 _ = b[3]
80 b[0] = byte(v >> 24)
81 b[1] = byte(v >> 16)
82 b[2] = byte(v >> 8)
83 b[3] = byte(v)
84 }
85
86 func (binaryBigEndian) Uint64(b []byte) uint64 {
87 _ = b[7]
88 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
89 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
90 }
91
View as plain text