-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
decode.go
210 lines (191 loc) · 4.31 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
package bin
import (
"encoding/binary"
"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
}
// Uint64 decodes 64-bit unsigned integer from Buffer.
func (b *Buffer) Uint64() (uint64, 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 v, nil
}
// Int32 decodes signed 32-bit integer from Buffer.
func (b *Buffer) Int32() (int32, error) {
v, err := b.Uint32()
if err != nil {
return 0, err
}
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
}
// 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) {
if err := b.ConsumeID(TypeVector); err != nil {
return 0, err
}
n, err := b.Int()
if err != nil {
return 0, err
}
if n < 0 {
return 0, &InvalidLengthError{
Length: n,
Where: "vector",
}
}
return 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 a copy, it's safe to modify it.
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 append([]byte(nil), 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
}
// Int53 decodes 53-bit signed integer from Buffer.
func (b *Buffer) Int53() (int64, error) {
return b.Long()
}
// Long decodes 64-bit signed integer from Buffer.
func (b *Buffer) Long() (int64, error) {
v, err := b.Uint64()
if err != nil {
return 0, err
}
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 signed 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
}