-
Notifications
You must be signed in to change notification settings - Fork 79
/
binaryReader.go
203 lines (173 loc) · 4.55 KB
/
binaryReader.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package io
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"reflect"
)
// MaxArraySize is the maximum size of an array which can be decoded.
// It is taken from https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs#L130
const MaxArraySize = 0x1000000
// BinReader is a convenient wrapper around a io.Reader and err object.
// Used to simplify error handling when reading into a struct with many fields.
type BinReader struct {
r io.Reader
uv [8]byte
Err error
}
// NewBinReaderFromIO makes a BinReader from io.Reader.
func NewBinReaderFromIO(ior io.Reader) *BinReader {
return &BinReader{r: ior}
}
// NewBinReaderFromBuf makes a BinReader from byte buffer.
func NewBinReaderFromBuf(b []byte) *BinReader {
r := bytes.NewReader(b)
return NewBinReaderFromIO(r)
}
// Len returns the number of bytes of the unread portion of the buffer if
// reading from bytes.Reader or -1 otherwise.
func (r *BinReader) Len() int {
var res = -1
byteReader, ok := r.r.(*bytes.Reader)
if ok {
res = byteReader.Len()
}
return res
}
// ReadU64LE reads a little-endian encoded uint64 value from the underlying
// io.Reader. On read failures it returns zero.
func (r *BinReader) ReadU64LE() uint64 {
r.ReadBytes(r.uv[:8])
if r.Err != nil {
return 0
}
return binary.LittleEndian.Uint64(r.uv[:8])
}
// ReadU32LE reads a little-endian encoded uint32 value from the underlying
// io.Reader. On read failures it returns zero.
func (r *BinReader) ReadU32LE() uint32 {
r.ReadBytes(r.uv[:4])
if r.Err != nil {
return 0
}
return binary.LittleEndian.Uint32(r.uv[:4])
}
// ReadU16LE reads a little-endian encoded uint16 value from the underlying
// io.Reader. On read failures it returns zero.
func (r *BinReader) ReadU16LE() uint16 {
r.ReadBytes(r.uv[:2])
if r.Err != nil {
return 0
}
return binary.LittleEndian.Uint16(r.uv[:2])
}
// ReadU16BE reads a big-endian encoded uint16 value from the underlying
// io.Reader. On read failures it returns zero.
func (r *BinReader) ReadU16BE() uint16 {
r.ReadBytes(r.uv[:2])
if r.Err != nil {
return 0
}
return binary.BigEndian.Uint16(r.uv[:2])
}
// ReadB reads a byte from the underlying io.Reader. On read failures it
// returns zero.
func (r *BinReader) ReadB() byte {
r.ReadBytes(r.uv[:1])
if r.Err != nil {
return 0
}
return r.uv[0]
}
// ReadBool reads a boolean value encoded in a zero/non-zero byte from the
// underlying io.Reader. On read failures it returns false.
func (r *BinReader) ReadBool() bool {
return r.ReadB() != 0
}
// ReadArray reads array into value which must be
// a pointer to a slice.
func (r *BinReader) ReadArray(t interface{}, maxSize ...int) {
value := reflect.ValueOf(t)
if value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Slice {
panic(value.Type().String() + " is not a pointer to a slice")
}
if r.Err != nil {
return
}
sliceType := value.Elem().Type()
elemType := sliceType.Elem()
isPtr := elemType.Kind() == reflect.Ptr
ms := MaxArraySize
if len(maxSize) != 0 {
ms = maxSize[0]
}
lu := r.ReadVarUint()
if lu > uint64(ms) {
r.Err = fmt.Errorf("array is too big (%d)", lu)
return
}
l := int(lu)
arr := reflect.MakeSlice(sliceType, l, l)
for i := 0; i < l; i++ {
var elem reflect.Value
if isPtr {
elem = reflect.New(elemType.Elem())
arr.Index(i).Set(elem)
} else {
elem = arr.Index(i).Addr()
}
el, ok := elem.Interface().(decodable)
if !ok {
panic(elemType.String() + "is not decodable")
}
el.DecodeBinary(r)
}
value.Elem().Set(arr)
}
// ReadVarUint reads a variable-length-encoded integer from the
// underlying reader.
func (r *BinReader) ReadVarUint() uint64 {
if r.Err != nil {
return 0
}
var b = r.ReadB()
if b == 0xfd {
return uint64(r.ReadU16LE())
}
if b == 0xfe {
return uint64(r.ReadU32LE())
}
if b == 0xff {
return r.ReadU64LE()
}
return uint64(b)
}
// ReadVarBytes reads the next set of bytes from the underlying reader.
// ReadVarUInt() is used to determine how large that slice is.
func (r *BinReader) ReadVarBytes(maxSize ...int) []byte {
n := r.ReadVarUint()
ms := MaxArraySize
if len(maxSize) != 0 {
ms = maxSize[0]
}
if n > uint64(ms) {
r.Err = fmt.Errorf("byte-slice is too big (%d)", n)
return nil
}
b := make([]byte, n)
r.ReadBytes(b)
return b
}
// ReadBytes copies fixed-size buffer from the reader to provided slice.
func (r *BinReader) ReadBytes(buf []byte) {
if r.Err != nil {
return
}
_, r.Err = io.ReadFull(r.r, buf)
}
// ReadString calls ReadVarBytes and casts the results as a string.
func (r *BinReader) ReadString(maxSize ...int) string {
b := r.ReadVarBytes(maxSize...)
return string(b)
}