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