-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
decode.go
236 lines (214 loc) · 5.14 KB
/
decode.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package bin
import (
"encoding/binary"
"errors"
"fmt"
"io"
"math"
)
// PeekID returns next type id in Buffer, but does not consume it.
func (b *Buffer) PeekID() (uint32, error) {
if len(b.Buf) < Word {
return 0, io.ErrUnexpectedEOF
}
v := binary.LittleEndian.Uint32(b.Buf)
return v, nil
}
// PeekN returns n bytes from Buffer to target, but does not consume it.
//
// Returns io.ErrUnexpectedEOF if buffer contains less that n bytes.
// Expects that len(target) >= n.
func (b *Buffer) PeekN(target []byte, n int) error {
if len(b.Buf) < n {
return io.ErrUnexpectedEOF
}
copy(target, b.Buf[:n])
return nil
}
// ID decodes type id from Buffer.
func (b *Buffer) ID() (uint32, error) {
return b.Uint32()
}
// Uint32 decodes unsigned 32-bit integer from Buffer.
func (b *Buffer) Uint32() (uint32, error) {
v, err := b.PeekID()
if err != nil {
return 0, err
}
b.Buf = b.Buf[Word:]
return v, nil
}
// Int32 decodes signed 32-bit integer from Buffer.
func (b *Buffer) Int32() (int32, error) {
if len(b.Buf) < Word {
return 0, io.ErrUnexpectedEOF
}
v := binary.LittleEndian.Uint32(b.Buf)
b.Buf = b.Buf[Word:]
return int32(v), nil
}
// ConsumeN consumes n bytes from buffer, writing them to target.
//
// Returns io.ErrUnexpectedEOF if buffer contains less that n bytes.
// Expects that len(target) >= n.
func (b *Buffer) ConsumeN(target []byte, n int) error {
if err := b.PeekN(target, n); err != nil {
return err
}
b.Buf = b.Buf[n:]
return nil
}
// ErrNonZeroPadding means that padding value contained non-zero byte which
// is unexpected and can be a sign of incorrect protocol implementation or
// data loss.
var ErrNonZeroPadding = errors.New("bin: non-zero byte in padding")
// ConsumePadding consumes n zero bytes from buffer.
//
// If consumed value is non-zero, ErrNonZeroPadding is returned.
// If not enough bytes to consume, io.ErrUnexpectedEOF is returned.
func (b *Buffer) ConsumePadding(n int) error {
if len(b.Buf) < n {
return io.ErrUnexpectedEOF
}
for _, v := range b.Buf[:n] {
if v != 0 {
return ErrNonZeroPadding
}
}
// Probably we should check that padding is actually zeroes.
b.Buf = b.Buf[n:]
return nil
}
// UnexpectedIDErr means that unknown or unexpected type id was decoded.
type UnexpectedIDErr struct {
ID uint32
}
func (e UnexpectedIDErr) Error() string {
return fmt.Sprintf("unexpected id 0x%x", e.ID)
}
// NewUnexpectedID return new UnexpectedIDErr.
func NewUnexpectedID(id uint32) error {
return &UnexpectedIDErr{ID: id}
}
// Bool decodes bare boolean from Buffer.
func (b *Buffer) Bool() (bool, error) {
v, err := b.PeekID()
if err != nil {
return false, err
}
switch v {
case TypeTrue:
b.Buf = b.Buf[Word:]
return true, nil
case TypeFalse:
b.Buf = b.Buf[Word:]
return false, nil
default:
return false, NewUnexpectedID(v)
}
}
// ConsumeID decodes type id from Buffer. If id differs from provided,
// the *UnexpectedIDErr{ID: gotID} will be returned and buffer will be
// not consumed.
func (b *Buffer) ConsumeID(id uint32) error {
v, err := b.PeekID()
if err != nil {
return err
}
if v != id {
return NewUnexpectedID(v)
}
b.Buf = b.Buf[Word:]
return nil
}
// VectorHeader decodes vector length from Buffer.
func (b *Buffer) VectorHeader() (int, error) {
id, err := b.PeekID()
if err != nil {
return 0, err
}
if id != TypeVector {
return 0, NewUnexpectedID(id)
}
b.Buf = b.Buf[Word:]
n, err := b.Int32()
if err != nil {
return 0, err
}
return int(n), nil
}
// String decodes string from Buffer.
func (b *Buffer) String() (string, error) {
n, v, err := decodeString(b.Buf)
if err != nil {
return "", err
}
if len(b.Buf) < n {
return "", io.ErrUnexpectedEOF
}
b.Buf = b.Buf[n:]
return v, nil
}
// Bytes decodes byte slice from Buffer.
//
// NB: returning value is slice of buffer, it is not safe
// to retain or modify. User should copy value if needed.
func (b *Buffer) Bytes() ([]byte, error) {
n, v, err := decodeBytes(b.Buf)
if err != nil {
return nil, err
}
if len(b.Buf) < n {
return nil, io.ErrUnexpectedEOF
}
b.Buf = b.Buf[n:]
return v, nil
}
// Int decodes integer from Buffer.
func (b *Buffer) Int() (int, error) {
v, err := b.Int32()
if err != nil {
return 0, err
}
return int(v), nil
}
// Double decodes 64-bit floating point from Buffer.
func (b *Buffer) Double() (float64, error) {
v, err := b.Long()
if err != nil {
return 0, err
}
return math.Float64frombits(uint64(v)), nil
}
// Long decodes 64-bit signed integer from Buffer.
func (b *Buffer) Long() (int64, error) {
const size = Word * 2
if len(b.Buf) < size {
return 0, io.ErrUnexpectedEOF
}
v := binary.LittleEndian.Uint64(b.Buf)
b.Buf = b.Buf[size:]
return int64(v), nil
}
// Int128 decodes 128-bit signed integer from Buffer.
func (b *Buffer) Int128() (Int128, error) {
v := Int128{}
size := len(v)
if len(b.Buf) < size {
return Int128{}, io.ErrUnexpectedEOF
}
copy(v[:size], b.Buf[:size])
b.Buf = b.Buf[size:]
return v, nil
}
// Int256 decodes 256-bit unsigned integer from Buffer.
func (b *Buffer) Int256() (Int256, error) {
v := Int256{}
size := len(v)
if len(b.Buf) < size {
return Int256{}, io.ErrUnexpectedEOF
}
copy(v[:size], b.Buf[:size])
b.Buf = b.Buf[size:]
return v, nil
}