-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpack.go
767 lines (700 loc) · 19 KB
/
unpack.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
package erlpack
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"github.com/jakemakesstuff/structs"
"io"
"reflect"
"unsafe"
)
// Contains all the types we want internally for our reader.
type unpackReader interface {
io.ByteReader
io.Reader
}
var errorInterface = reflect.TypeOf((*error)(nil)).Elem()
var uncastedResultType = reflect.TypeOf((*UncastedResult)(nil))
// Atom is used to define an atom within the codebase.
type Atom string
// RawData is used to define data which was within an Erlpack array but has not been parsed yet.
// This is different to UncastedResult since it has not been processed yet.
type RawData []byte
// Cast is used to cast the result to a pointer.
func (r RawData) Cast(Ptr interface{}) error {
v := &pointerSetter{ptr: reflect.ValueOf(Ptr)}
if v.ptr.Kind() != reflect.Ptr {
return errors.New("invalid pointer")
}
return processItem(v, bytes.NewReader(r))
}
// UncastedResult is used to define a result which has not been casted yet.
// You can call Cast on this to cast the item after the initial unpacking.
type UncastedResult struct {
item interface{}
}
// Cast is used to cast the result to a pointer.
func (u *UncastedResult) Cast(Ptr interface{}) error {
v := &pointerSetter{ptr: reflect.ValueOf(Ptr)}
if v.ptr.Kind() != reflect.Ptr {
return errors.New("invalid pointer")
}
return handleItemCasting(u.item, v)
}
// Used to cast the item.
func handleItemCasting(Item interface{}, setter *pointerSetter) error {
// Get the base pointer.
Ptr := setter.getBasePtr()
// Handle a interface or uncasted result.
switch Ptr.(type) {
case *interface{}:
return setter.set(reflect.ValueOf(&Item))
case *UncastedResult:
return setter.set(reflect.ValueOf(&UncastedResult{item: Item}))
}
// Handle specific type casting.
switch x := Item.(type) {
case Atom:
switch Ptr.(type) {
case *Atom:
return setter.set(reflect.ValueOf(&x))
}
case int64:
switch Ptr.(type) {
case *int:
p := int(x)
return setter.set(reflect.ValueOf(&p))
case *int64:
return setter.set(reflect.ValueOf(&x))
default:
return errors.New("could not de-serialize into int")
}
case int32:
switch Ptr.(type) {
case *int:
p := int(x)
return setter.set(reflect.ValueOf(&p))
case *int32:
return setter.set(reflect.ValueOf(&x))
default:
return errors.New("could not de-serialize into int")
}
case float64:
switch Ptr.(type) {
case *float64:
return setter.set(reflect.ValueOf(&x))
default:
return errors.New("could not de-serialize into float64")
}
case uint8:
switch Ptr.(type) {
case *uint:
p := uint(x)
return setter.set(reflect.ValueOf(&p))
case *uint8:
return setter.set(reflect.ValueOf(&x))
case *int:
p := int(x)
return setter.set(reflect.ValueOf(&p))
default:
return errors.New("could not de-serialize into uint8")
}
case string:
// Map key.
switch Ptr.(type) {
case *string:
p := x
return setter.set(reflect.ValueOf(&p))
default:
return errors.New("could not de-serialize into string")
}
case []byte:
// We should try and string-ify this if possible.
switch Ptr.(type) {
case *string:
p := string(x)
return setter.set(reflect.ValueOf(&p))
case *[]byte:
return setter.set(reflect.ValueOf(&x))
default:
return errors.New("could not de-serialize into string")
}
case bool:
// This should cast into either a string or a boolean.
switch Ptr.(type) {
case *Atom:
// Set it to a string representation of the value.
var p Atom
if x {
p = "true"
} else {
p = "false"
}
return setter.set(reflect.ValueOf(&p))
case *bool:
// Set it to the raw value.
return setter.set(reflect.ValueOf(&x))
}
case nil:
// This should zero any data types other than atoms.
switch Ptr.(type) {
case *Atom:
// We should set this to "nil".
nils := (Atom)("nil")
return setter.set(reflect.ValueOf(&nils))
default:
// Set to nil.
return setter.set(reflect.ValueOf(Ptr))
}
case []interface{}:
// We should handle this array.
switch Ptr.(type) {
case *[]interface{}:
// This is simple.
return setter.set(reflect.ValueOf(&x))
default:
// Get the reflect value.
r := reflect.MakeSlice(reflect.ValueOf(Ptr).Type().Elem(), len(x), len(x))
// Set all the items.
for i, v := range x {
indexItem := r.Index(i)
x := reflect.New(indexItem.Type())
t := x.Interface()
err := handleItemCasting(v, &pointerSetter{ptr: reflect.ValueOf(t)})
if err != nil {
return err
}
indexItem.Set(x.Elem())
}
// Create the pointer.
ptr := reflect.New(reflect.PtrTo(r.Type()).Elem())
ptr.Elem().Set(r)
return setter.set(ptr)
}
case map[interface{}]interface{}:
// Maps are complicated since they can serialize into a lot of different types.
switch Ptr.(type) {
case *map[interface{}]interface{}:
// This is the first thing we check for since it is by far the best situation.
return setter.set(reflect.ValueOf(&x))
}
// Check the type of the pointer.
switch e := reflect.ValueOf(Ptr).Type().Elem(); e.Kind() {
case reflect.Struct:
// Make the new struct.
i := reflect.New(e)
// Check if the struct has a "UncastedErlpack" function. If so, call that and return any errors.
function := i.MethodByName("UncastedErlpack")
if function.IsValid() {
if function.Type().NumIn() != 1 {
return errors.New("only *UncastedResult is expected as an argument")
}
if function.Type().In(0) != uncastedResultType {
return errors.New("only *UncastedResult is expected as a result")
}
if function.Type().NumOut() != 1 {
return errors.New("only error is expected as a result")
}
if !function.Type().Out(0).Implements(errorInterface) {
return errors.New("result is not error")
}
f := function.Interface().(func(*UncastedResult) error)
return f(&UncastedResult{item: Item})
}
// Get the struct object.
s := structs.New(i.Interface())
s.TagName = "erlpack"
// Set tag > field.
tag2field := map[string]string{}
for _, field := range s.Fields() {
t := field.Tag("erlpack")
if t != "-" {
if t == "" {
tag2field[field.Name()] = field.Name()
continue
}
tag2field[t] = field.Name()
}
}
// Iterate through the map.
for k, v := range x {
switch str := k.(type) {
case string:
fieldName, ok := tag2field[str]
if !ok {
continue
}
field, ok := s.FieldOk(fieldName)
if !ok {
return errors.New("failed to get field")
}
r := reflect.New(field.Type())
x := r.Interface()
err := handleItemCasting(v, &pointerSetter{ptr: reflect.ValueOf(x)})
if err != nil {
return err
}
err = field.Set(r.Elem().Interface())
if err != nil {
return err
}
default:
return errors.New("key must be string")
}
}
// Create the pointer.
ptr := reflect.New(reflect.PtrTo(e).Elem())
ptr.Elem().Set(i.Elem())
return setter.set(ptr)
case reflect.Map:
// Make the new map.
m := reflect.MakeMap(e)
// Get the key type.
keyType := m.Type().Key()
// Get the value type.
valueType := m.Type().Elem()
// Iterate through the map.
for k, v := range x {
// Create a new version of the key with the reflect type.
reflectKey := reflect.Zero(keyType)
pptr := reflect.New(reflect.PtrTo(reflectKey.Type()).Elem())
pptr.Elem().Set(reflectKey)
// Handle the item casting for the key.
err := handleItemCasting(k, &pointerSetter{ptr: pptr})
if err != nil {
return err
}
reflectKey = pptr.Elem()
// Create a new version of the value with the reflect type.
reflectValue := reflect.Zero(valueType)
pptr = reflect.New(reflect.PtrTo(reflectValue.Type()).Elem())
pptr.Elem().Set(reflectValue)
// Handle the item casting for the value.
err = handleItemCasting(v, &pointerSetter{ptr: pptr})
if err != nil {
return err
}
reflectValue = pptr.Elem()
// Set the item.
if !pptr.IsNil() {
m.SetMapIndex(reflectKey, reflectValue)
}
}
// Create the pointer.
ptr := reflect.New(reflect.PtrTo(e).Elem())
ptr.Elem().Set(m)
return setter.set(ptr)
}
}
// Return unknown type error.
return errors.New("unable to unpack to pointer specified")
}
// Used to process an atom during unpacking.
func processAtom(Data []byte) interface{} {
matchRest := func(d []byte) bool {
if len(d) > len(Data)-1 {
return false
}
for i := 0; i < len(d); i++ {
if Data[i+1] != d[i] {
return false
}
}
return true
}
switch Data[0] {
case 't':
matched := matchRest([]byte("rue"))
if !matched {
return Atom(Data)
}
return true
case 'f':
matched := matchRest([]byte("alse"))
if !matched {
return Atom(Data)
}
return false
case 'n':
matched := matchRest([]byte("il"))
if !matched {
return Atom(Data)
}
return nil
default:
return Atom(Data)
}
}
// Process the raw data.
func processRawData(DataType byte, setter *pointerSetter, r unpackReader, jsonType bool) error {
// Defines the byte array it'll go into.
var bytes []byte
// Get the right data type.
switch DataType {
case 's': // atom
b, err := r.ReadByte()
if err != nil {
return err
}
Len := int(b)
bytes = make([]byte, Len+2)
bytes[0] = 's'
bytes[1] = b
for Total := 0; Total != Len; Total++ {
b, err := r.ReadByte()
if err != nil {
return errors.New("atom size larger than remainder of array")
}
bytes[Total+2] = b
}
case 'j': // blank list
bytes = []byte{'j'}
case 'l': // list
// Get the length of the list.
lengthBytes := make([]byte, 4)
_, err := r.Read(lengthBytes)
if err != nil {
return errors.New("not enough bytes for list length")
}
l := binary.BigEndian.Uint32(lengthBytes)
bytes = make([]byte, 5, (l*3)+5)
bytes[0] = 'l'
for i := uint8(0); i < 4; i++ {
bytes[i+1] = lengthBytes[i]
}
// Try and get each item from the list.
for i := 0; i < int(l); i++ {
DataType, err := r.ReadByte()
if err != nil {
return errors.New("not long enough to include data type")
}
var raw RawData
itemSetter := &pointerSetter{ptr: reflect.ValueOf(&raw)}
if err = processRawData(DataType, itemSetter, r, false); err != nil {
return err
}
bytes = append(bytes, raw...)
}
case 'm': // string
// Get the length of the string.
lengthBytes := make([]byte, 4)
_, err := r.Read(lengthBytes)
if err != nil {
return errors.New("not enough bytes for list length")
}
l := binary.BigEndian.Uint32(lengthBytes)
// Create the byte array.
Len := int(l)
bytes = make([]byte, Len+5)
bytes[0] = 'm'
for i := uint8(0); i < 4; i++ {
bytes[i+1] = lengthBytes[i]
}
// Write each byte.
for Total := 0; Total != Len; Total++ {
b, err := r.ReadByte()
if err != nil {
return errors.New("atom size larger than remainder of array")
}
bytes[Total+5] = b
}
case 'a': // small int
i, err := r.ReadByte()
if err != nil {
return errors.New("failed to read small int")
}
bytes = []byte{'a', i}
case 'b': // int32
b := make([]byte, 4)
_, err := r.Read(b)
if err != nil {
return errors.New("not enough bytes for int32")
}
bytes = append([]byte{'b'}, b...)
case 'n': // int64
// Get the number of encoded bytes.
encodedBytes, err := r.ReadByte()
if err != nil {
return errors.New("unable to read int64 byte count")
}
// Create the byte array.
bytes = make([]byte, encodedBytes+2)
bytes[0] = 'n'
bytes[1] = encodedBytes
// Write each byte.
for Total := uint8(0); Total != encodedBytes; Total++ {
b, err := r.ReadByte()
if err != nil {
return errors.New("int size larger than remainder of array")
}
bytes[Total+2] = b
}
case 'F': // float
// Get the next 8 bytes.
bytes = make([]byte, 9)
bytes[0] = 'F'
for i := uint8(0); i < 8; i++ {
b, err := r.ReadByte()
if err != nil {
return errors.New("float size larger than remainder of array")
}
bytes[i+1] = b
}
case 't': // map
// Get the length of the map.
lengthBytes := make([]byte, 4)
_, err := r.Read(lengthBytes)
if err != nil {
return errors.New("not enough bytes for list length")
}
l := binary.BigEndian.Uint32(lengthBytes)
bytes = make([]byte, 5, (l*6)+5)
bytes[0] = 't'
for i := uint8(0); i < 4; i++ {
bytes[i+1] = lengthBytes[i]
}
// Try and get each item from the map.
for i := 0; i < int(l); i++ {
DataType, err := r.ReadByte()
if err != nil {
return errors.New("not long enough to include data type")
}
var raw RawData
itemSetter := &pointerSetter{ptr: reflect.ValueOf(&raw)}
if err = processRawData(DataType, itemSetter, r, false); err != nil {
return err
}
bytes = append(bytes, raw...)
DataType, err = r.ReadByte()
if err != nil {
return errors.New("not long enough to include data type")
}
if err = processRawData(DataType, itemSetter, r, false); err != nil {
return err
}
bytes = append(bytes, raw...)
}
default:
return errors.New("unknown data type")
}
// Handle processing the pointer.
if jsonType {
x := (json.RawMessage)(bytes)
return setter.set(reflect.ValueOf(&x))
} else {
x := (RawData)(bytes)
return setter.set(reflect.ValueOf(&x))
}
}
// Processes a item.
func processItem(setter *pointerSetter, r unpackReader) error {
// Gets the type of data.
DataType, err := r.ReadByte()
if err != nil {
return errors.New("not long enough to include data type")
}
// Check if this is meant to be raw data and process that differently if so.
switch setter.getBasePtr().(type) {
case *json.RawMessage:
return processRawData(DataType, setter, r, true)
case *RawData:
return processRawData(DataType, setter, r, false)
}
// Handle the various different data types.
var Item interface{}
switch DataType {
case 's': // atom
// Get the atom information.
b, err := r.ReadByte()
if err != nil {
return err
}
Len := int(b)
Data := make([]byte, Len)
for Total := 0; Total != Len; Total++ {
b, err := r.ReadByte()
if err != nil {
return errors.New("atom size larger than remainder of array")
}
Data[Total] = b
}
Item = processAtom(Data)
case 'j': // blank list
Item = []interface{}{}
case 'l': // list
// Get the length of the list.
lengthBytes := make([]byte, 4)
_, err := r.Read(lengthBytes)
if err != nil {
return errors.New("not enough bytes for list length")
}
l := binary.BigEndian.Uint32(lengthBytes)
// Try and get each item from the list.
Item = make([]interface{}, l)
for i := 0; i < int(l); i++ {
var x interface{}
err := processItem(&pointerSetter{ptr: reflect.ValueOf(&x)}, r)
if err != nil {
return err
}
Item.([]interface{})[i] = x
}
case 'm': // string
// Get the length of the string.
lengthBytes := make([]byte, 4)
_, err := r.Read(lengthBytes)
if err != nil {
return errors.New("not enough bytes for list length")
}
l := binary.BigEndian.Uint32(lengthBytes)
// Make an array of the specified length.
Item = make([]byte, l)
// Write into it if we can.
_, err = r.Read(Item.([]byte))
if err != nil {
return errors.New("string length is longer than remainder of array")
}
case 'a': // small int
i, err := r.ReadByte()
if err != nil {
return errors.New("failed to read small int")
}
Item = i
case 'b': // int32
b := make([]byte, 4)
_, err := r.Read(b)
if err != nil {
return errors.New("not enough bytes for int32")
}
l := binary.BigEndian.Uint32(b)
Item = *(*int32)(unsafe.Pointer(&l))
case 'n': // int64
// Get the number of encoded bytes.
encodedBytes, err := r.ReadByte()
if err != nil {
return errors.New("unable to read int64 byte count")
}
// Get the signature.
signatureChar, err := r.ReadByte()
if err != nil {
return errors.New("unable to read int64 signature")
}
negative := signatureChar == 1
// Create the uint64.
u := uint64(0)
// Decode the int64.
x := uint64(0)
for i := 0; i < int(encodedBytes); i++ {
// Read the next byte.
b, err := r.ReadByte()
if err != nil {
return errors.New("int64 length greater than array")
}
// Add the byte.
u += uint64(b) * x
x <<= 8
}
// Turn the uint64 into a int64.
if negative {
Item = int64(u) * -1
} else {
Item = int64(u)
}
case 'F': // float
// Get the next 8 bytes.
encodedBytes := make([]byte, 8)
// Read said encoded bytes.
_, err := r.Read(encodedBytes)
if err != nil {
return errors.New("not enough bytes to decode")
}
// Get the item as a uint64.
i := binary.BigEndian.Uint64(encodedBytes)
// Turn it into a float64.
Item = *(*float64)(unsafe.Pointer(&i))
case 't': // map
// Get the length.
b := make([]byte, 4)
_, err := r.Read(b)
if err != nil {
return errors.New("not enough bytes for int32")
}
l := binary.BigEndian.Uint32(b)
// Create the map.
m := make(map[interface{}]interface{}, l)
// Get each item from the map.
for i := uint32(0); i < l; i++ {
// Get the key.
var Key interface{}
err := processItem(&pointerSetter{ptr: reflect.ValueOf(&Key)}, r)
if err != nil {
return err
}
switch x := Key.(type) {
case []byte:
// bytes should be stored as strings for maps
Key = string(x)
}
// Get the value.
var Value interface{}
err = processItem(&pointerSetter{ptr: reflect.ValueOf(&Value)}, r)
if err != nil {
return err
}
// Set the key to the value specified.
m[Key] = Value
}
// Set the item to the map.
Item = m
default: // Don't know this data type.
return errors.New("unknown data type")
}
// Handle the item casting.
return handleItemCasting(Item, setter)
}
// Create a special reader that handles all types we need for ease of user.
// If the reader doesn't contain io.ByteReader, this contains the code to do this for you.
type byteReaderUpgrader struct {
io.Reader
}
// ReadByte is used to read a byte.
func (r *byteReaderUpgrader) ReadByte() (byte, error) {
if reader, ok := r.Reader.(io.ByteReader); ok {
return reader.ReadByte()
}
ob := make([]byte, 1)
_, err := r.Reader.Read(ob)
return ob[0], err
}
// UnpackReader is used to unpack a value to a pointer from a reader.
// Note that to ensure compatibility in codebases where you have both erlpack and json, json.RawMessage is treated the same as erlpack.RawData.
func UnpackReader(reader io.Reader, Ptr interface{}) error {
// Check if the ptr is actually a pointer.
v := &pointerSetter{ptr: reflect.ValueOf(Ptr)}
if v.ptr.Kind() != reflect.Ptr {
return errors.New("invalid pointer")
}
// The invalid erlpack handler.
err := func() error {
return errors.New("invalid erlpack bytes")
}
// Get the reader.
r := &byteReaderUpgrader{reader}
// Check the version.
Version, _ := r.ReadByte()
if Version != 131 {
return err()
}
// Return the data unpacking.
return processItem(v, r)
}
// Unpack is used to unpack a value to a pointer.
// Note that to ensure compatibility in codebases where you have both erlpack and json, json.RawMessage is treated the same as erlpack.RawData.
func Unpack(Data []byte, Ptr interface{}) error {
l := len(Data)
if 2 > l {
return errors.New("erlpack bytes cannot be shorter than 2 bytes")
}
return UnpackReader(bytes.NewReader(Data), Ptr)
}