-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
256 lines (244 loc) · 5.6 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package arq
import (
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
"time"
)
var (
ErrUnimplemented = errors.New("Unimplemented")
ErrUnknownSliceLength = errors.New("ErrUnknownSliceLength")
ErrTooLong = errors.New("value contains too many elements")
ErrInvalidNotNull = errors.New("ErrInvalidNotNull")
)
type ArqUnmarshaler interface {
UnmarshalArq(io.Reader) error
}
func DecodeArq(r io.Reader, i interface{}) error {
// If this type knows how to decode itself, let it.
if um, ok := i.(ArqUnmarshaler); ok {
return um.UnmarshalArq(r)
}
return decodeArqValue(r, reflect.ValueOf(i).Elem(), "")
}
func decodeArqValue(r io.Reader, v reflect.Value, tag string) error {
switch v.Kind() {
case reflect.Int8:
var n int8
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
v.SetInt(int64(n))
return nil
case reflect.Int32:
var n int32
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
v.SetInt(int64(n))
return nil
case reflect.Int64:
var n int64
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
v.SetInt(n)
return nil
case reflect.Uint8:
var n uint8
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
v.SetUint(uint64(n))
return nil
case reflect.Uint32:
var n uint32
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
v.SetUint(uint64(n))
return nil
case reflect.Uint64:
var n uint64
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
v.SetUint(n)
return nil
case reflect.Bool:
var n uint8
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
v.SetBool(n == 1)
return nil
case reflect.Array:
// byte arrays are special cased because we use them so often.
if v.Type().Elem().Kind() == reflect.Uint8 {
return decodeByteArray(r, v)
}
// Otherwise, recurse for each element.
for i := 0; i < v.Len(); i++ {
err := decodeArqValue(r, v.Index(i), "")
if err != nil {
return err
}
}
return nil
case reflect.String:
return decodeString(r, v)
case reflect.Struct:
if u := indirect(v); u != nil {
return u.UnmarshalArq(r)
}
switch v.Interface().(type) {
case time.Time:
return decodeTime(r, v, tag)
default:
return decodeStruct(r, v, tag)
}
case reflect.Slice:
return decodeSlice(r, v, tag)
case reflect.Ptr:
return fmt.Errorf("decoding pointers %w", ErrUnimplemented)
}
return fmt.Errorf("decoding '%s' %w", v.Type().String(), ErrUnimplemented)
}
func decodeByteArray(r io.Reader, v reflect.Value) error {
buf := make([]byte, v.Len())
_, err := io.ReadAtLeast(r, buf, v.Len())
if err != nil {
return err
}
reflect.Copy(v, reflect.ValueOf(buf))
return nil
}
func decodeString(r io.Reader, v reflect.Value) error {
notNull := byte(0)
if err := binary.Read(r, binary.BigEndian, ¬Null); err != nil {
return err
}
if notNull > 1 {
return ErrInvalidNotNull
}
if notNull != 1 {
return nil
}
length := uint64(0)
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
return err
}
if length > 4096 {
return ErrTooLong
}
buf := make([]byte, length)
if _, err := io.ReadAtLeast(r, buf, int(length)); err != nil {
return err
}
v.SetString(string(buf))
return nil
}
func decodeStruct(r io.Reader, v reflect.Value, tag string) error {
for i := 0; i < v.NumField(); i++ {
if !v.Field(i).CanSet() {
continue
}
err := decodeArqValue(r, v.Field(i), v.Type().Field(i).Tag.Get("arq"))
if err != nil {
return err
}
}
return nil
}
func decodeSlice(r io.Reader, v reflect.Value, tag string) error {
// Determine the number of elements in the slice.
var n uint64
switch tag {
case "len-uint32":
var n32 uint32
err := binary.Read(r, binary.BigEndian, &n32)
if err != nil {
return err
}
n = uint64(n32)
case "len-uint64":
err := binary.Read(r, binary.BigEndian, &n)
if err != nil {
return err
}
default:
return ErrUnknownSliceLength
}
if n > 4096 {
return ErrTooLong
}
// Fast path for bytes to avoid recursing.
if v.Type().Elem().Kind() == reflect.Uint8 {
buf := make([]byte, int(n))
_, err := io.ReadAtLeast(r, buf, int(n))
if err != nil {
return err
}
v.Set(reflect.AppendSlice(v, reflect.ValueOf(buf)))
return nil
}
for i := 0; i < int(n); i++ {
elem := reflect.New(v.Type().Elem()).Elem()
if err := decodeArqValue(r, elem, ""); err != nil {
return err
}
v.Set(reflect.Append(v, elem))
}
return nil
}
func decodeTime(r io.Reader, v reflect.Value, tag string) error {
if tag == "nsec" {
var sec int64
var nsec int64
if err := binary.Read(r, binary.BigEndian, &sec); err != nil {
return err
}
if err := binary.Read(r, binary.BigEndian, &nsec); err != nil {
return err
}
t := time.Unix(sec, nsec)
v.Set(reflect.ValueOf(t))
return nil
}
var isNotNull uint8
if err := binary.Read(r, binary.BigEndian, &isNotNull); err != nil {
return err
}
if isNotNull != 1 {
v.Set(reflect.Zero(v.Type()))
return nil
}
var millis int64
if err := binary.Read(r, binary.BigEndian, &millis); err != nil {
return err
}
tm := time.Unix(0, millis*int64(time.Millisecond))
v.Set(reflect.ValueOf(tm))
return nil
}
// Inspired by this golang JSON decoder: https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/encoding/json/decode.go#L420-L425
func indirect(v reflect.Value) ArqUnmarshaler {
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
v = v.Addr()
}
if v.Type().NumMethod() > 0 && v.CanInterface() {
if u, ok := v.Interface().(ArqUnmarshaler); ok {
return u
}
}
return nil
}