forked from prysmaticlabs/go-ssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
414 lines (392 loc) · 12.8 KB
/
unmarshal.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package ssz
import (
"encoding/binary"
"errors"
"fmt"
"reflect"
)
// Unmarshal SSZ encoded data and output it into the object pointed by pointer val.
// Given a struct with the following fields, and some encoded bytes of type []byte,
// one can then unmarshal the bytes into a pointer of the struct as follows:
// type exampleStruct1 struct {
// Field1 uint8
// Field2 []byte
// }
//
// var targetStruct exampleStruct1
// if err := Unmarshal(encodedBytes, &targetStruct); err != nil {
// return fmt.Errorf("failed to unmarshal: %v", err)
// }
func Unmarshal(input []byte, val interface{}) error {
if val == nil {
return errors.New("cannot unmarshal into untyped, nil value")
}
rval := reflect.ValueOf(val)
rtyp := rval.Type()
// val must be a pointer, otherwise we refuse to unmarshal
if rtyp.Kind() != reflect.Ptr {
return errors.New("can only unmarshal into a pointer target")
}
if rval.IsNil() {
return errors.New("cannot output to pointer of nil value")
}
sszUtils, err := cachedSSZUtils(rval.Elem().Type())
if err != nil {
return fmt.Errorf("could not initialize unmarshaler for type: %v, %v", rval.Elem().Type(), err)
}
if _, err = sszUtils.unmarshaler(input, rval.Elem(), 0); err != nil {
return fmt.Errorf("could not unmarshal input into type: %v, %v", rval.Elem().Type(), err)
}
return nil
}
func makeUnmarshaler(typ reflect.Type) (dec unmarshaler, err error) {
kind := typ.Kind()
switch {
case kind == reflect.Bool:
return unmarshalBool, nil
case kind == reflect.Uint8:
return unmarshalUint8, nil
case kind == reflect.Uint16:
return unmarshalUint16, nil
case kind == reflect.Uint32:
return unmarshalUint32, nil
case kind == reflect.Int32:
return unmarshalUint32, nil
case kind == reflect.Uint64:
return unmarshalUint64, nil
case kind == reflect.Slice && typ.Elem().Kind() == reflect.Uint8:
return makeByteSliceUnmarshaler()
case kind == reflect.Array && typ.Elem().Kind() == reflect.Uint8:
return makeBasicArrayUnmarshaler(typ)
case kind == reflect.Slice && isBasicTypeArray(typ.Elem(), typ.Elem().Kind()):
return makeBasicSliceUnmarshaler(typ)
case kind == reflect.Slice && isBasicType(typ.Elem().Kind()):
return makeBasicSliceUnmarshaler(typ)
case kind == reflect.Slice && !isVariableSizeType(typ.Elem()):
return makeBasicSliceUnmarshaler(typ)
case kind == reflect.Array && !isVariableSizeType(typ.Elem()):
return makeBasicArrayUnmarshaler(typ)
case kind == reflect.Slice:
return makeCompositeSliceUnmarshaler(typ)
case kind == reflect.Array:
return makeCompositeArrayUnmarshaler(typ)
case kind == reflect.Struct:
return makeStructUnmarshaler(typ)
case kind == reflect.Ptr:
return makePtrUnmarshaler(typ)
default:
return nil, fmt.Errorf("type %v is not deserializable", typ)
}
}
func unmarshalBool(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
v := uint8(input[startOffset])
if v == 0 {
val.SetBool(false)
} else if v == 1 {
val.SetBool(true)
} else {
return 0, fmt.Errorf("expected 0 or 1 but received %d", v)
}
return startOffset + 1, nil
}
func unmarshalUint8(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
val.SetUint(uint64(input[startOffset]))
return startOffset + 1, nil
}
func unmarshalUint16(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
offset := startOffset + 2
buf := make([]byte, 2)
copy(buf, input[startOffset:offset])
val.SetUint(uint64(binary.LittleEndian.Uint16(buf)))
return offset, nil
}
func unmarshalUint32(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
offset := startOffset + 4
buf := make([]byte, 4)
copy(buf, input[startOffset:offset])
val.SetUint(uint64(binary.LittleEndian.Uint32(buf)))
return offset, nil
}
func unmarshalUint64(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
offset := startOffset + 8
buf := make([]byte, 8)
copy(buf, input[startOffset:offset])
val.SetUint(binary.LittleEndian.Uint64(buf))
return offset, nil
}
func makeByteSliceUnmarshaler() (unmarshaler, error) {
unmarshaler := func(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
offset := startOffset + uint64(len(input))
val.SetBytes(input[startOffset:offset])
return offset, nil
}
return unmarshaler, nil
}
func makeBasicSliceUnmarshaler(typ reflect.Type) (unmarshaler, error) {
elemSSZUtils, err := cachedSSZUtilsNoAcquireLock(typ.Elem())
if err != nil {
return nil, err
}
unmarshaler := func(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
if len(input) == 0 {
newVal := reflect.MakeSlice(val.Type(), 0, 0)
val.Set(newVal)
return 0, nil
}
// If there are struct tags that specify a different type, we handle accordingly.
if val.Type() != typ {
sizes := []uint64{1}
innerElement := typ.Elem()
for {
if innerElement.Kind() == reflect.Slice {
sizes = append(sizes, 0)
innerElement = innerElement.Elem()
} else if innerElement.Kind() == reflect.Array {
sizes = append(sizes, uint64(innerElement.Len()))
innerElement = innerElement.Elem()
} else {
break
}
}
// If the item is a slice, we grow it accordingly based on the size tags.
result := growSliceFromSizeTags(val, sizes)
reflect.Copy(result, val)
val.Set(result)
} else {
growConcreteSliceType(val, val.Type(), 1)
}
index := startOffset
index, err = elemSSZUtils.unmarshaler(input, val.Index(0), index)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal element of slice: %v", err)
}
elementSize := index - startOffset
endOffset := uint64(len(input)) / elementSize
if val.Type() != typ {
sizes := []uint64{endOffset}
innerElement := typ.Elem()
for {
if innerElement.Kind() == reflect.Slice {
sizes = append(sizes, 0)
innerElement = innerElement.Elem()
} else if innerElement.Kind() == reflect.Array {
sizes = append(sizes, uint64(innerElement.Len()))
innerElement = innerElement.Elem()
} else {
break
}
}
// If the item is a slice, we grow it accordingly based on the size tags.
result := growSliceFromSizeTags(val, sizes)
reflect.Copy(result, val)
val.Set(result)
}
i := uint64(1)
for i < endOffset {
if val.Type() == typ {
growConcreteSliceType(val, val.Type(), int(i)+1)
}
index, err = elemSSZUtils.unmarshaler(input, val.Index(int(i)), index)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal element of slice: %v", err)
}
i++
}
return index, nil
}
return unmarshaler, nil
}
func makeCompositeSliceUnmarshaler(typ reflect.Type) (unmarshaler, error) {
elemType := typ.Elem()
elemSSZUtils, err := cachedSSZUtilsNoAcquireLock(elemType)
if err != nil {
return nil, err
}
unmarshaler := func(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
if len(input) == 0 {
newVal := reflect.MakeSlice(val.Type(), 0, 0)
val.Set(newVal)
return 0, nil
}
growConcreteSliceType(val, typ, 1)
endOffset := uint64(len(input))
currentIndex := startOffset
nextIndex := currentIndex
offsetVal := input[startOffset : startOffset+BytesPerLengthOffset]
firstOffset := startOffset + uint64(binary.LittleEndian.Uint32(offsetVal))
currentOffset := firstOffset
nextOffset := currentOffset
i := 0
for currentIndex < firstOffset {
nextIndex = currentIndex + BytesPerLengthOffset
if nextIndex == firstOffset {
nextOffset = endOffset
} else {
nextOffsetVal := input[nextIndex : nextIndex+BytesPerLengthOffset]
nextOffset = startOffset + uint64(binary.LittleEndian.Uint32(nextOffsetVal))
}
// We grow the slice's size to accommodate a new element being unmarshaled.
growConcreteSliceType(val, typ, i+1)
if _, err := elemSSZUtils.unmarshaler(input[currentOffset:nextOffset], val.Index(i), 0); err != nil {
return 0, fmt.Errorf("failed to unmarshal element of slice: %v", err)
}
i++
currentIndex = nextIndex
currentOffset = nextOffset
}
return currentIndex, nil
}
return unmarshaler, nil
}
func makeBasicArrayUnmarshaler(typ reflect.Type) (unmarshaler, error) {
elemType := typ.Elem()
elemSSZUtils, err := cachedSSZUtilsNoAcquireLock(elemType)
if err != nil {
return nil, err
}
unmarshaler := func(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
i := 0
index := startOffset
size := val.Len()
for i < size {
if val.Index(i).Kind() == reflect.Ptr {
instantiateConcreteTypeForElement(val.Index(i), typ.Elem().Elem())
}
index, err = elemSSZUtils.unmarshaler(input, val.Index(i), index)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal element of array: %v", err)
}
i++
}
return index, nil
}
return unmarshaler, nil
}
func makeCompositeArrayUnmarshaler(typ reflect.Type) (unmarshaler, error) {
elemType := typ.Elem()
elemSSZUtils, err := cachedSSZUtilsNoAcquireLock(elemType)
if err != nil {
return nil, err
}
unmarshaler := func(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
currentIndex := startOffset
nextIndex := currentIndex
offsetVal := input[startOffset : startOffset+BytesPerLengthOffset]
firstOffset := startOffset + uint64(binary.LittleEndian.Uint32(offsetVal))
currentOffset := firstOffset
nextOffset := currentOffset
endOffset := uint64(len(input))
i := 0
for currentIndex < firstOffset {
nextIndex = currentIndex + BytesPerLengthOffset
if nextIndex == firstOffset {
nextOffset = endOffset
} else {
nextOffsetVal := input[nextIndex : nextIndex+BytesPerLengthOffset]
nextOffset = startOffset + uint64(binary.LittleEndian.Uint32(nextOffsetVal))
}
if val.Index(i).Kind() == reflect.Ptr {
instantiateConcreteTypeForElement(val.Index(i), typ.Elem().Elem())
}
if _, err := elemSSZUtils.unmarshaler(input[currentOffset:nextOffset], val.Index(i), 0); err != nil {
return 0, fmt.Errorf("failed to unmarshal element of slice: %v", err)
}
i++
currentIndex = nextIndex
currentOffset = nextOffset
}
return currentIndex, nil
}
return unmarshaler, nil
}
func makeStructUnmarshaler(typ reflect.Type) (unmarshaler, error) {
fields, err := structFields(typ)
if err != nil {
return nil, err
}
unmarshaler := func(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
endOffset := uint64(len(input))
currentIndex := startOffset
nextIndex := currentIndex
fixedSizes := make([]uint64, len(fields))
for i := 0; i < len(fixedSizes); i++ {
if !isVariableSizeType(fields[i].typ) {
if val.Field(i).Kind() == reflect.Ptr {
instantiateConcreteTypeForElement(val.Field(i), fields[i].typ.Elem())
}
concreteVal := val.Field(i)
sszSizeTags, hasTags, err := parseSSZFieldTags(typ.Field(i))
if err != nil {
return 0, err
}
if hasTags {
concreteType := inferFieldTypeFromSizeTags(typ.Field(i), sszSizeTags)
concreteVal = reflect.New(concreteType).Elem()
// If the item is a slice, we grow it accordingly based on the size tags.
if val.Field(i).Kind() == reflect.Slice {
result := growSliceFromSizeTags(val.Field(i), sszSizeTags)
val.Field(i).Set(result)
}
}
fixedSz := determineFixedSize(concreteVal, fields[i].typ)
if fixedSz > 0 {
fixedSizes[i] = fixedSz
}
} else {
fixedSizes[i] = 0
}
}
offsets := make([]uint64, 0)
offsetIndexCounter := startOffset
for _, item := range fixedSizes {
if item > 0 {
offsetIndexCounter += item
} else {
offsetVal := input[offsetIndexCounter : offsetIndexCounter+BytesPerLengthOffset]
offsets = append(offsets, startOffset+uint64(binary.LittleEndian.Uint32(offsetVal)))
offsetIndexCounter += BytesPerLengthOffset
}
}
offsets = append(offsets, endOffset)
offsetIndex := uint64(0)
for i := 0; i < len(fields); i++ {
f := fields[i]
fieldSize := fixedSizes[i]
if val.Field(i).Kind() == reflect.Ptr {
instantiateConcreteTypeForElement(val.Field(i), fields[i].typ.Elem())
}
if fieldSize > 0 {
nextIndex = currentIndex + fieldSize
if _, err := f.sszUtils.unmarshaler(input[currentIndex:nextIndex], val.Field(i), 0); err != nil {
return 0, err
}
currentIndex = nextIndex
} else {
firstOff := offsets[offsetIndex]
nextOff := offsets[offsetIndex+1]
if _, err := f.sszUtils.unmarshaler(input[firstOff:nextOff], val.Field(i), 0); err != nil {
return 0, err
}
offsetIndex++
currentIndex += BytesPerLengthOffset
}
}
return currentIndex, nil
}
return unmarshaler, nil
}
func makePtrUnmarshaler(typ reflect.Type) (unmarshaler, error) {
elemType := typ.Elem()
elemSSZUtils, err := cachedSSZUtilsNoAcquireLock(elemType)
if err != nil {
return nil, err
}
unmarshaler := func(input []byte, val reflect.Value, startOffset uint64) (uint64, error) {
elemSize, err := elemSSZUtils.unmarshaler(input, val.Elem(), startOffset)
if err != nil {
return 0, fmt.Errorf("failed to unmarshal to object pointed by pointer: %v", err)
}
return elemSize, nil
}
return unmarshaler, nil
}