-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_encoding_reader.go
More file actions
executable file
·175 lines (146 loc) · 3.33 KB
/
binary_encoding_reader.go
File metadata and controls
executable file
·175 lines (146 loc) · 3.33 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
168
169
170
171
172
173
174
175
package wax
import (
"encoding/binary"
"io"
"github.com/pkg/errors"
)
type BinaryEncodingReader struct {
r io.Reader
}
func NewBinaryEncodingReader(r io.Reader) *BinaryEncodingReader {
return &BinaryEncodingReader{
r: r,
}
}
func (ber *BinaryEncodingReader) Read(b []byte) (int, error) {
return ber.r.Read(b)
}
func (ber *BinaryEncodingReader) ReadU8() (uint8, error) {
b := make([]byte, 1)
n, err := ber.r.Read(b)
if err != nil {
return 0, err
}
if n != 1 {
return 0, errors.New("unable to read 1 byte")
}
return b[0], nil
}
func (ber *BinaryEncodingReader) ReadU16LE() (uint16, error) {
b := make([]byte, 2)
n, err := ber.r.Read(b)
if err != nil {
return 0, err
}
if n != 2 {
return 0, errors.New("unable to read 2 bytes")
}
return binary.LittleEndian.Uint16(b), nil
}
func (ber *BinaryEncodingReader) ReadU16BE() (uint16, error) {
b := make([]byte, 2)
n, err := ber.r.Read(b)
if err != nil {
return 0, err
}
if n != 2 {
return 0, errors.New("unable to read 2 bytes")
}
return binary.BigEndian.Uint16(b), nil
}
func (ber *BinaryEncodingReader) ReadU32LE() (uint32, error) {
b := make([]byte, 4)
n, err := ber.r.Read(b)
if err != nil {
return 0, err
}
if n != 4 {
return 0, errors.New("unable to read 4 bytes")
}
return binary.LittleEndian.Uint32(b), nil
}
func (ber *BinaryEncodingReader) ReadU32BE() (uint32, error) {
b := make([]byte, 4)
n, err := ber.r.Read(b)
if err != nil {
return 0, err
}
if n != 4 {
return 0, errors.New("unable to read 4 bytes")
}
return binary.BigEndian.Uint32(b), nil
}
func (ber *BinaryEncodingReader) ReadU64LE() (uint64, error) {
b := make([]byte, 8)
n, err := ber.r.Read(b)
if err != nil {
return 0, err
}
if n != 8 {
return 0, errors.New("unable to read 8 bytes")
}
return binary.LittleEndian.Uint64(b), nil
}
func (ber *BinaryEncodingReader) ReadU64BE() (uint64, error) {
b := make([]byte, 8)
n, err := ber.r.Read(b)
if err != nil {
return 0, err
}
if n != 8 {
return 0, errors.New("unable to read 8 bytes")
}
return binary.BigEndian.Uint64(b), nil
}
func (ber *BinaryEncodingReader) ReadVaruint() (uint64, []byte, error) {
const n = 64
var result uint64
var shift uint
consumedBytes := make([]byte, 0)
for {
b, err := ber.ReadU8()
if err != nil {
return 0, consumedBytes, err
}
if shift == 63 && b != 0 && b != 0x01 {
return 0, consumedBytes, errors.New("overflow")
}
consumedBytes = append(consumedBytes, b)
result |= (uint64(b&0x7f) << shift)
if b&0x80 == 0 {
return result, consumedBytes, nil
}
shift += 7
}
}
/*
Signed integers are encoded in signed LEB128 format, which uses a two’s complement representation.
As an additional constraint, the total number of bytes encoding a value of type sN must not exceed ceil(N/7) bytes.
*/
func (ber *BinaryEncodingReader) ReadVarint() (int64, []byte, error) {
const n = 64
var err error
var result int64
var shift uint
var b byte
consumedBytes := make([]byte, 0)
for {
b, err = ber.ReadU8()
if err != nil {
return 0, consumedBytes, err
}
if shift == 63 && b != 0 && b != 0x7f {
return 0, consumedBytes, errors.New("overflow")
}
consumedBytes = append(consumedBytes, b)
result |= int64(b&0x7f) << shift
shift += 7
if b&0x80 == 0 {
break
}
}
if shift < n && (b&0x40) == 0x40 {
result |= -1 << shift
}
return result, consumedBytes, nil
}