This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
decode.go
340 lines (321 loc) · 7.92 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package lcs
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
"strconv"
)
type Decoder struct {
r io.Reader
enums map[reflect.Type]map[string]map[EnumKeyType]reflect.Type
}
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
r: r,
enums: make(map[reflect.Type]map[string]map[EnumKeyType]reflect.Type),
}
}
func (d *Decoder) Decode(v interface{}) error {
err := d.decode(reflect.Indirect(reflect.ValueOf(v)), nil, 0)
if err != nil {
return err
}
return nil
}
func (d *Decoder) EOF() bool {
_, err := d.r.Read(make([]byte, 1))
if err == io.EOF {
return true
}
return false
}
func (d *Decoder) decode(rv reflect.Value, enumVariants map[EnumKeyType]reflect.Type, fixedLen int) (err error) {
switch rv.Kind() {
case reflect.Bool:
if !rv.CanSet() {
return errors.New("bool value cannot set")
}
v8 := uint8(0)
if err = binary.Read(d.r, binary.LittleEndian, &v8); err != nil {
return
}
if v8 == 1 {
rv.SetBool(true)
} else if v8 == 0 {
rv.SetBool(false)
} else {
return errors.New("unexpected value for bool")
}
case /*reflect.Int,*/ reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
/*reflect.Uint,*/ reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if !rv.CanSet() {
return errors.New("integer value cannot set")
}
err = binary.Read(d.r, binary.LittleEndian, rv.Addr().Interface())
case reflect.Slice:
err = d.decodeSlice(rv, enumVariants, fixedLen)
case reflect.Array:
err = d.decodeArray(rv, enumVariants, fixedLen)
case reflect.String:
err = d.decodeString(rv, fixedLen)
case reflect.Struct:
err = d.decodeStruct(rv)
case reflect.Map:
err = d.decodeMap(rv)
case reflect.Ptr:
if rv.IsNil() {
rv.Set(reflect.New(rv.Type().Elem()))
}
err = d.decode(rv.Elem(), enumVariants, fixedLen)
case reflect.Interface:
err = d.decodeInterface(rv, enumVariants)
default:
err = errors.New("not supported kind: " + rv.Kind().String())
}
return
}
func (d *Decoder) decodeByteSlice(fixedLen int) (b []byte, err error) {
l := uint32(fixedLen)
if l == 0 {
l1, err := readVarUint(d.r, 28)
if err != nil {
return nil, err
}
l = uint32(l1)
if l > maxByteSliceSize {
return nil, errors.New("byte slice longer than 100MB not supported")
}
}
b = make([]byte, l)
if _, err = io.ReadFull(d.r, b); err != nil {
return
}
return
}
func (d *Decoder) decodeSlice(rv reflect.Value, enumVariants map[EnumKeyType]reflect.Type, fixedLen int) (err error) {
if !rv.CanSet() {
return errors.New("slice cannot set")
}
if rv.Type() == reflect.TypeOf([]byte{}) {
var b []byte
if b, err = d.decodeByteSlice(fixedLen); err != nil {
return
}
rv.SetBytes(b)
return
}
l := uint32(fixedLen)
var cap int
if l == 0 {
l1, err := readVarUint(d.r, 28)
if err != nil {
return err
}
l = uint32(l1)
cap = int(l)
if cap > sliceAndMapInitSize {
cap = sliceAndMapInitSize
}
}
s := reflect.MakeSlice(rv.Type(), 0, cap)
for i := 0; i < int(l); i++ {
v := reflect.New(rv.Type().Elem())
if err = d.decode(v.Elem(), enumVariants, 0); err != nil {
return
}
s = reflect.Append(s, v.Elem())
}
rv.Set(s)
return
}
func (d *Decoder) decodeMap(rv reflect.Value) (err error) {
if !rv.CanSet() {
return errors.New("map cannot set")
}
l1, err := readVarUint(d.r, 28)
if err != nil {
return
}
l := uint32(l1)
cap := int(l)
if cap > sliceAndMapInitSize {
cap = sliceAndMapInitSize
}
m := reflect.MakeMapWithSize(rv.Type(), cap)
for i := 0; i < int(l); i++ {
k := reflect.New(rv.Type().Key())
v := reflect.New(rv.Type().Elem())
if err = d.decode(k, nil, 0); err != nil {
return
}
if err = d.decode(v, nil, 0); err != nil {
return
}
m.SetMapIndex(k.Elem(), v.Elem())
}
rv.Set(m)
return
}
func (d *Decoder) decodeArray(rv reflect.Value, enumVariants map[EnumKeyType]reflect.Type, fixedLen int) (err error) {
if !rv.CanSet() {
return errors.New("array cannot set")
}
if rv.Kind() == reflect.Array {
fixedLen = rv.Len()
}
if rv.Type().Elem() == reflect.TypeOf(byte(0)) {
var b []byte
if b, err = d.decodeByteSlice(fixedLen); err != nil {
return
}
if len(b) != rv.Len() {
return errors.New("length mismatch")
}
reflect.Copy(rv, reflect.ValueOf(b))
return
}
l := uint32(fixedLen)
if l == 0 {
l1, err := readVarUint(d.r, 28)
if err != nil {
return err
}
l = uint32(l1)
}
if int(l) != rv.Len() {
return errors.New("length mismatch")
}
for i := 0; i < int(l); i++ {
if err = d.decode(rv.Index(i), enumVariants, 0); err != nil {
return
}
}
return
}
func (d *Decoder) decodeString(rv reflect.Value, fixedLen int) (err error) {
if !rv.CanSet() {
return errors.New("string cannot set")
}
var b []byte
if b, err = d.decodeByteSlice(fixedLen); err != nil {
return
}
rv.SetString(string(b))
return
}
func (d *Decoder) decodeInterface(rv reflect.Value, enumVariants map[EnumKeyType]reflect.Type) (err error) {
typeVal, err := readVarUint(d.r, 28)
if err != nil {
return
}
tpl, ok := enumGetTypeByIdx(rv.Type(), typeVal)
if !ok {
tpl, ok = enumVariants[typeVal]
if !ok {
return fmt.Errorf("enum variant value %d unknown for interface: %s", typeVal, rv.Type())
}
}
if tpl.Kind() == reflect.Ptr {
rv1 := reflect.New(tpl.Elem())
if err = d.decode(rv1, nil, 0); err != nil {
return
}
rv.Set(rv1)
} else {
rv1 := reflect.New(tpl)
if err = d.decode(rv1, nil, 0); err != nil {
return
}
rv.Set(rv1.Elem())
}
return nil
}
func (d *Decoder) decodeStruct(rv reflect.Value) (err error) {
if !rv.CanSet() {
return errors.New("struct cannot set")
}
rt := rv.Type()
for i := 0; i < rv.NumField(); i++ {
fv := rv.Field(i)
if !fv.CanSet() {
continue
}
if rt.Field(i).Tag.Get(lcsTagName) == "-" {
continue
}
tag := parseTag(rt.Field(i).Tag.Get(lcsTagName))
var evs map[EnumKeyType]reflect.Type
if enumName, ok := tag["enum"]; ok {
evsAll, ok := d.enums[rv.Type()]
if !ok {
if evsAll = d.getEnumVariants(rv); evsAll != nil {
d.enums[rv.Type()] = evsAll
}
}
if evsAll == nil {
return fmt.Errorf("struct (%s) does not implement EnumTypeUser", rv.Type())
}
evs, ok = evsAll[enumName]
if !ok {
return errors.New("enum variants not defined for enum name: " + enumName)
}
}
if _, ok := tag["optional"]; ok &&
(fv.Kind() == reflect.Ptr ||
fv.Kind() == reflect.Slice ||
fv.Kind() == reflect.Map ||
fv.Kind() == reflect.Interface) {
rb := reflect.New(reflect.TypeOf(false))
if err = d.decode(rb, nil, 0); err != nil {
return
}
if !rb.Elem().Bool() {
fv.Set(reflect.Zero(fv.Type()))
continue
}
}
fixedLen := 0
if fixedLenStr, ok := tag["len"]; ok && (fv.Kind() == reflect.Slice || fv.Kind() == reflect.String) {
fixedLen, err = strconv.Atoi(fixedLenStr)
if err != nil {
return errors.New("tag len parse error: " + err.Error())
}
}
if err = d.decode(fv, evs, fixedLen); err != nil {
return
}
}
return
}
func (d *Decoder) getEnumVariants(rv reflect.Value) map[string]map[EnumKeyType]reflect.Type {
vv, ok := rv.Interface().(EnumTypeUser)
if !ok {
vv, ok = reflect.New(reflect.PtrTo(rv.Type())).Elem().Interface().(EnumTypeUser)
if !ok {
return nil
}
}
r := make(map[string]map[EnumKeyType]reflect.Type)
evs := vv.EnumTypes()
for _, ev := range evs {
evt := reflect.TypeOf(ev.Template)
if r[ev.Name] == nil {
r[ev.Name] = make(map[EnumKeyType]reflect.Type)
}
r[ev.Name][ev.Value] = evt
}
return r
}
func Unmarshal(data []byte, v interface{}) error {
d := NewDecoder(bytes.NewReader(data))
if err := d.Decode(v); err != nil {
return err
}
if !d.EOF() {
return errors.New("unexpected data")
}
return nil
}