1
2
3
4
5
6
7
8 package lif
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 Uint64([]byte) uint64
25 PutUint16([]byte, uint16)
26 PutUint32([]byte, uint32)
27 PutUint64([]byte, uint64)
28 }
29
30 type binaryLittleEndian struct{}
31
32 func (binaryLittleEndian) Uint16(b []byte) uint16 {
33 _ = b[1]
34 return uint16(b[0]) | uint16(b[1])<<8
35 }
36
37 func (binaryLittleEndian) PutUint16(b []byte, v uint16) {
38 _ = b[1]
39 b[0] = byte(v)
40 b[1] = byte(v >> 8)
41 }
42
43 func (binaryLittleEndian) Uint32(b []byte) uint32 {
44 _ = b[3]
45 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
46 }
47
48 func (binaryLittleEndian) PutUint32(b []byte, v uint32) {
49 _ = b[3]
50 b[0] = byte(v)
51 b[1] = byte(v >> 8)
52 b[2] = byte(v >> 16)
53 b[3] = byte(v >> 24)
54 }
55
56 func (binaryLittleEndian) Uint64(b []byte) uint64 {
57 _ = b[7]
58 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
59 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
60 }
61
62 func (binaryLittleEndian) PutUint64(b []byte, v uint64) {
63 _ = b[7]
64 b[0] = byte(v)
65 b[1] = byte(v >> 8)
66 b[2] = byte(v >> 16)
67 b[3] = byte(v >> 24)
68 b[4] = byte(v >> 32)
69 b[5] = byte(v >> 40)
70 b[6] = byte(v >> 48)
71 b[7] = byte(v >> 56)
72 }
73
74 type binaryBigEndian struct{}
75
76 func (binaryBigEndian) Uint16(b []byte) uint16 {
77 _ = b[1]
78 return uint16(b[1]) | uint16(b[0])<<8
79 }
80
81 func (binaryBigEndian) PutUint16(b []byte, v uint16) {
82 _ = b[1]
83 b[0] = byte(v >> 8)
84 b[1] = byte(v)
85 }
86
87 func (binaryBigEndian) Uint32(b []byte) uint32 {
88 _ = b[3]
89 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
90 }
91
92 func (binaryBigEndian) PutUint32(b []byte, v uint32) {
93 _ = b[3]
94 b[0] = byte(v >> 24)
95 b[1] = byte(v >> 16)
96 b[2] = byte(v >> 8)
97 b[3] = byte(v)
98 }
99
100 func (binaryBigEndian) Uint64(b []byte) uint64 {
101 _ = b[7]
102 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
103 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
104 }
105
106 func (binaryBigEndian) PutUint64(b []byte, v uint64) {
107 _ = b[7]
108 b[0] = byte(v >> 56)
109 b[1] = byte(v >> 48)
110 b[2] = byte(v >> 40)
111 b[3] = byte(v >> 32)
112 b[4] = byte(v >> 24)
113 b[5] = byte(v >> 16)
114 b[6] = byte(v >> 8)
115 b[7] = byte(v)
116 }
117
View as plain text