This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
decoder.go
300 lines (275 loc) · 6.42 KB
/
decoder.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package amino
import (
"encoding/binary"
"errors"
"fmt"
"math"
"time"
)
//----------------------------------------
// Signed
func DecodeInt8(bz []byte) (i int8, n int, err error) {
const size int = 1
if len(bz) < size {
err = errors.New("EOF decoding int8")
return
}
i = int8(bz[0])
n = size
return
}
func DecodeInt16(bz []byte) (i int16, n int, err error) {
const size int = 2
if len(bz) < size {
err = errors.New("EOF decoding int16")
return
}
i = int16(binary.LittleEndian.Uint16(bz[:size]))
n = size
return
}
func DecodeInt32(bz []byte) (i int32, n int, err error) {
const size int = 4
if len(bz) < size {
err = errors.New("EOF decoding int32")
return
}
i = int32(binary.LittleEndian.Uint32(bz[:size]))
n = size
return
}
func DecodeInt64(bz []byte) (i int64, n int, err error) {
const size int = 8
if len(bz) < size {
err = errors.New("EOF decoding int64")
return
}
i = int64(binary.LittleEndian.Uint64(bz[:size]))
n = size
return
}
func DecodeVarint(bz []byte) (i int64, n int, err error) {
i, n = binary.Varint(bz)
if n == 0 {
// buf too small
err = errors.New("buffer too small")
} else if n < 0 {
// value larger than 64 bits (overflow)
// and -n is the number of bytes read
n = -n
err = errors.New("EOF decoding varint")
}
return
}
//----------------------------------------
// Unsigned
func DecodeUint8(bz []byte) (u uint8, n int, err error) {
var u64 = uint64(0)
u64, n, err = DecodeUvarint(bz)
if err != nil {
return
}
if u64 > uint64(math.MaxUint8) {
err = errors.New("EOF decoding uint8")
return
}
u = uint8(u64)
return
}
func DecodeUint16(bz []byte) (u uint16, n int, err error) {
var u64 = uint64(0)
u64, n, err = DecodeUvarint(bz)
if err != nil {
return
}
if u64 > uint64(math.MaxUint16) {
err = errors.New("EOF decoding uint16")
return
}
u = uint16(u64)
return
}
func DecodeUint32(bz []byte) (u uint32, n int, err error) {
const size int = 4
if len(bz) < size {
err = errors.New("EOF decoding uint32")
return
}
u = binary.LittleEndian.Uint32(bz[:size])
n = size
return
}
func DecodeUint64(bz []byte) (u uint64, n int, err error) {
const size int = 8
if len(bz) < size {
err = errors.New("EOF decoding uint64")
return
}
u = binary.LittleEndian.Uint64(bz[:size])
n = size
return
}
func DecodeUvarint(bz []byte) (u uint64, n int, err error) {
u, n = binary.Uvarint(bz)
if n == 0 {
// buf too small
err = errors.New("buffer too small")
} else if n < 0 {
// value larger than 64 bits (overflow)
// and -n is the number of bytes read
n = -n
err = errors.New("EOF decoding uvarint")
}
return
}
//----------------------------------------
// Other
func DecodeBool(bz []byte) (b bool, n int, err error) {
const size int = 1
if len(bz) < size {
err = errors.New("EOF decoding bool")
return
}
switch bz[0] {
case 0:
b = false
case 1:
b = true
default:
err = errors.New("invalid bool")
}
n = size
return
}
// NOTE: UNSAFE
func DecodeFloat32(bz []byte) (f float32, n int, err error) {
const size int = 4
if len(bz) < size {
err = errors.New("EOF decoding float32")
return
}
i := binary.LittleEndian.Uint32(bz[:size])
f = math.Float32frombits(i)
n = size
return
}
// NOTE: UNSAFE
func DecodeFloat64(bz []byte) (f float64, n int, err error) {
const size int = 8
if len(bz) < size {
err = errors.New("EOF decoding float64")
return
}
i := binary.LittleEndian.Uint64(bz[:size])
f = math.Float64frombits(i)
n = size
return
}
// DecodeTime decodes seconds (int64) and nanoseconds (int32) since January 1,
// 1970 UTC, and returns the corresponding time. If nanoseconds is not in the
// range [0, 999999999], or if seconds is too large, the behavior is
// undefined.
// TODO return error if behavior is undefined.
func DecodeTime(bz []byte) (t time.Time, n int, err error) {
// Defensively set default to to zeroTime (1970, not 0001)
t = zeroTime
// Read sec and nanosec.
var sec int64
var nsec int32
if len(bz) > 0 {
sec, n, err = decodeSeconds(&bz)
if err != nil {
return
}
}
if len(bz) > 0 {
nsec, err = decodeNanos(&bz, &n)
if err != nil {
return
}
}
// Construct time.
t = time.Unix(sec, int64(nsec))
// Strip timezone and monotonic for deep equality.
t = t.UTC().Truncate(0)
return
}
func decodeSeconds(bz *[]byte) (int64, int, error) {
// Optionally decode field number 1 and Typ3 (8Byte).
// only slide if we need to:
n := 0
fieldNum, typ, _n, err := decodeFieldNumberAndTyp3(*bz)
if err != nil {
return 0, n, err
}
if fieldNum == 1 && typ == Typ3_Varint {
slide(bz, &n, _n)
_n = 0
sec, _n, err := DecodeUvarint(*bz)
if slide(bz, &n, _n) && err != nil {
return 0, n, err
}
// if seconds where negative before casting them to uint64, we yield
// the original signed value:
res := int64(sec)
if res < minSeconds || res >= maxSeconds {
return 0, n, InvalidTimeErr(fmt.Sprintf("seconds have to be > %d and < %d, got: %d",
minSeconds, maxSeconds, res))
}
return res, n, err
} else if fieldNum == 2 && typ == Typ3_Varint {
// skip: do not slide, no error, will read again
return 0, n, nil
} else {
return 0, n, fmt.Errorf("expected field number 1 <Varint> or field number 2 <Varint> , got %v", fieldNum)
}
}
func decodeNanos(bz *[]byte, n *int) (int32, error) {
// Optionally decode field number 2 and Typ3 (4Byte).
fieldNum, typ, _n, err := decodeFieldNumberAndTyp3(*bz)
if err != nil {
return 0, err
}
if fieldNum == 2 && typ == Typ3_Varint {
slide(bz, n, _n)
_n = 0
nsec, _n, err := DecodeUvarint(*bz)
if slide(bz, n, _n) && err != nil {
return 0, err
}
// Validation check.
if nsec < 0 || maxNanos < nsec {
return 0, InvalidTimeErr(fmt.Sprintf("nanoseconds not in interval [0, 999999999] %v", nsec))
}
// this cast from uint64 to int32 is OK, due to above restriction:
return int32(nsec), nil
}
// skip over (no error)
return 0, nil
}
func DecodeByteSlice(bz []byte) (bz2 []byte, n int, err error) {
var count uint64
var _n int
count, _n, err = DecodeUvarint(bz)
if slide(&bz, &n, _n) && err != nil {
return
}
if int(count) < 0 {
err = fmt.Errorf("invalid negative length %v decoding []byte", count)
return
}
if len(bz) < int(count) {
err = fmt.Errorf("insufficient bytes decoding []byte of length %v", count)
return
}
bz2 = make([]byte, count)
copy(bz2, bz[0:count])
n += int(count)
return
}
func DecodeString(bz []byte) (s string, n int, err error) {
var bz2 []byte
bz2, n, err = DecodeByteSlice(bz)
s = string(bz2)
return
}