-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.go
More file actions
167 lines (149 loc) · 4.02 KB
/
binary.go
File metadata and controls
167 lines (149 loc) · 4.02 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package faroe
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
)
func (binarySequence *binarySequenceType) encode() []byte {
buffer := bytes.Buffer{}
itemSizeBytes := [10]byte{}
for _, item := range *binarySequence {
itemSizeBytesSize := binary.PutUvarint(itemSizeBytes[:], uint64(len(item)))
buffer.Write(itemSizeBytes[:itemSizeBytesSize])
buffer.Write(item)
}
return buffer.Bytes()
}
func (binarySequence *binarySequenceType) add(value []byte) {
*binarySequence = append(*binarySequence, value)
}
func (binarySequence *binarySequenceType) addString(value string) {
binarySequence.add([]byte(value))
}
func (binarySequence *binarySequenceType) addInt64(value int64) {
valueUint64 := uint64(value) << 1
if value < 0 {
valueUint64 = ^valueUint64
}
encoded := [8]byte{}
binary.BigEndian.PutUint64(encoded[:], valueUint64)
offset := 0
for _, b := range encoded {
if b != 0 {
break
}
offset++
}
binarySequence.add(encoded[offset:])
}
func (binarySequence *binarySequenceType) addInt32(value int32) {
valueUint32 := uint32(value) << 1
if value < 0 {
valueUint32 = ^valueUint32
}
encoded := [4]byte{}
binary.BigEndian.PutUint32(encoded[:], valueUint32)
offset := 0
for _, b := range encoded {
if b != 0 {
break
}
offset++
}
binarySequence.add(encoded[offset:])
}
func (binarySequence *binarySequenceType) addBool(value bool) {
if value {
binarySequence.add([]byte{1})
} else {
binarySequence.add([]byte{0})
}
}
func parseBinarySequenceBytes(encoded []byte) (binarySequenceType, error) {
binarySequence := binarySequenceType{}
offset := 0
for offset < len(encoded) {
sizeUint64, bytesRead := binary.Uvarint(encoded[offset:])
if bytesRead < 1 {
return nil, fmt.Errorf("invalid item length header")
}
offset += int(bytesRead)
if sizeUint64 > math.MaxInt {
return nil, fmt.Errorf("item size too big")
}
size := int(sizeUint64)
if offset+size > len(encoded) {
return nil, fmt.Errorf("item size too big")
}
binarySequence = append(binarySequence, encoded[offset:offset+int(size)])
offset += int(size)
}
return binarySequence, nil
}
type binarySequenceType [][]byte
func (binarySequence *binarySequenceType) get(index int) ([]byte, error) {
if index >= len(*binarySequence) {
return nil, fmt.Errorf("out of range")
}
value := (*binarySequence)[index]
return value, nil
}
func (binarySequence *binarySequenceType) getString(index int) (string, error) {
encoded, err := binarySequence.get(index)
if err != nil {
return "", fmt.Errorf("failed to get value: %s", err.Error())
}
return string(encoded), nil
}
func (binarySequence *binarySequenceType) getInt64(index int) (int64, error) {
encoded, err := binarySequence.get(index)
if err != nil {
return 0, fmt.Errorf("failed to get value: %s", err.Error())
}
if len(encoded) > 8 {
return 0, errors.New("invalid int64 encoding")
}
encodedUint64 := [8]byte{}
copy(encodedUint64[8-len(encoded):], encoded)
decodedUint64 := binary.BigEndian.Uint64(encodedUint64[:])
decoded := int64(decodedUint64 >> 1)
if decodedUint64&1 == 1 {
decoded = ^decoded
}
return decoded, nil
}
func (binarySequence *binarySequenceType) getInt32(index int) (int32, error) {
encoded, err := binarySequence.get(index)
if err != nil {
return 0, fmt.Errorf("failed to get value: %s", err.Error())
}
if len(encoded) > 4 {
return 0, errors.New("invalid int64 encoding")
}
encodedUint32 := [4]byte{}
copy(encodedUint32[4-len(encoded):], encoded)
decodedUint32 := binary.BigEndian.Uint32(encodedUint32[:])
decoded := int32(decodedUint32 >> 1)
if decodedUint32&1 == 1 {
decoded = ^decoded
}
return decoded, nil
}
func (binarySequence *binarySequenceType) getBool(index int) (bool, error) {
encoded, err := binarySequence.get(index)
if err != nil {
return false, fmt.Errorf("failed to get value: %s", err.Error())
}
if len(encoded) != 1 {
return false, errors.New("invalid bool encoding")
}
if encoded[0] == 0 {
return false, nil
}
if encoded[0] == 1 {
return true, nil
}
return false, errors.New("invalid bool encoding")
}