-
Notifications
You must be signed in to change notification settings - Fork 0
/
unserializer.go
656 lines (625 loc) · 13.6 KB
/
unserializer.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
package codec
import (
"errors"
"fmt"
"io"
"math"
"math/bits"
"reflect"
"unsafe"
)
type Unserializer struct {
refs map[uint32]reflect.Value
rels map[uint32]uint32
cnt uint32
data []byte
pos int
size int
}
func NewUnserializer() *Unserializer {
return &Unserializer{}
}
func (u *Unserializer) Decode(data []byte) (value any, err error) {
if data == nil {
return nil, io.ErrUnexpectedEOF
}
defer func() {
if e := recover(); e != nil {
err = errors.New(e.(string))
}
}()
u.refs = make(map[uint32]reflect.Value)
u.rels = make(map[uint32]uint32)
u.cnt = 0
u.data = data
u.pos = 1 // skip version for now
u.size = len(data)
var v reflect.Value
v, err = u.decode(v)
if err == nil && v.IsValid() {
value = v.Interface()
}
return value, err
}
func (u *Unserializer) decode(value reflect.Value) (v reflect.Value, err error) {
isNil := false
t := u.data[u.pos]
if t&mask == tType {
if t&null != 0 {
isNil = true
}
if t&custom == 0 {
t = u.data[u.pos+int(t&0b11)+2]
}
}
cnt := u.cnt
proceed := true
switch t & mask {
case tType:
v, err = u.decodeSerializable()
case tNil:
v = u.decodeNil()
case tBool:
v, err = u.decodeBool()
case tInt8:
v, err = u.decodeFixedInt()
case tInt:
v, err = u.decodeInt()
case tFloat:
v, err = u.decodeFloat()
case tComplex:
v, err = u.decodeComplex()
case tString:
v, err = u.decodeString()
case tUintptr:
v, err = u.decodeUintptr()
case tChan:
if t&tFunc == tFunc {
v, err = u.decodeFunc()
} else {
v, err = u.decodeChan(isNil)
}
case tList:
v, err = u.decodeList(isNil)
proceed = false
case tMap:
v, err = u.decodeMap(isNil)
proceed = false
case tStruct:
v, err = u.decodeStruct()
proceed = false
case tPointer:
v, err = u.decodePointer(value)
proceed = false
case tRef:
v, err = u.decodeReference(value)
proceed = false
case tInterface:
v, err = u.decodeInterface(isNil || t&null != 0)
proceed = false
default:
return value, fmt.Errorf("unrecognized type %b", t)
}
if err != nil {
return v, err
}
if value.IsValid() {
setValue(value, v)
} else {
value = v
}
if proceed {
u.refs[u.cnt] = value
u.cnt++
} else {
u.refs[cnt] = value
}
return value, nil
}
func (u *Unserializer) decodeSerializable() (reflect.Value, error) {
v, l, _, err := u.decodeTypeWithLength(false)
if err != nil {
return v, err
}
length := int(l)
u.pos += length
if u.pos > u.size {
return v, io.ErrUnexpectedEOF
}
if !isSerializable(v) {
return v, fmt.Errorf("struct %q does not implement Serializable interface", v.Type().Name())
}
s := v.MethodByName("Unserialize").Call([]reflect.Value{reflect.ValueOf(u.data[u.pos-length : u.pos])})
e := s[1].Interface()
if err != nil {
return v, e.(error)
}
return reflect.ValueOf(s[0].Interface()).Convert(v.Type()), nil
}
func (u *Unserializer) decodeNil() reflect.Value {
u.pos++
return reflect.Value{}
}
func (u *Unserializer) decodeBool() (reflect.Value, error) {
v, err := u.decodeType()
isTrue := (u.data[u.pos-1] & tru) != 0
if err != nil {
return v, err
}
if isTrue {
changeValue(v, true)
} else {
changeValue(v, false)
}
return v, nil
}
func (u *Unserializer) decodeString() (reflect.Value, error) {
v, length, _, err := u.decodeTypeWithLength(false)
if err != nil {
return v, err
}
if length > 0 {
start := u.pos
u.pos += int(length)
if u.pos > u.size {
return v, io.ErrUnexpectedEOF
}
str := u.data[start:u.pos]
changeValue(v, *(*string)(unsafe.Pointer(&str)))
}
return v, nil
}
func (u *Unserializer) decodeFixedInt() (reflect.Value, error) {
v, err := u.decodeType()
if err != nil {
return v, err
}
t := u.data[u.pos-1]
bitSize := 8 << (t & 0b11)
hasMeta := t&meta != 0
var size int
si := u.pos
sb := u.data[si]
if hasMeta {
if bitSize == 64 {
size = int((u.data[si] & 0b11100000) >> 5)
u.data[si] &= 0b00011111
} else if bitSize == 32 {
size = int((u.data[si] & 0b11000000) >> 6)
u.data[si] &= 0b00111111
} else if bitSize == 16 {
if hasMeta {
size = 1
} else {
size = 2
}
} else {
size = 1
}
} else {
size = bitSize >> 3
}
i, err := u.readUint(size)
u.data[si] = sb // fix the changed first byte
if err != nil {
return v, err
}
if t&signed == 0 {
switch bitSize {
case 64:
changeValue(v, i)
case 32:
changeValue(v, uint32(i))
case 16:
changeValue(v, uint16(i))
default:
changeValue(v, uint8(i))
}
} else {
neg := i&1 != 0
i >>= 1
switch bitSize {
case 64:
if neg {
changeValue(v, ^int64(i))
} else {
changeValue(v, int64(i))
}
case 32:
if neg {
changeValue(v, ^int32(i))
} else {
changeValue(v, int32(i))
}
case 16:
if neg {
changeValue(v, ^int16(i))
} else {
changeValue(v, int16(i))
}
default:
if neg {
changeValue(v, ^int8(i))
} else {
changeValue(v, int8(i))
}
}
}
return v, nil
}
func (u *Unserializer) decodeInt() (reflect.Value, error) {
v, i, t, err := u.decodeTypeWithLength(false)
if err != nil {
return v, err
}
if t&signed == 0 {
changeValue(v, i)
} else if i&1 != 0 {
changeValue(v, ^int(i>>1))
} else {
changeValue(v, int(i>>1))
}
return v, nil
}
func (u *Unserializer) decodeFloat() (reflect.Value, error) {
v, f, t, err := u.decodeTypeWithLength(false)
if err != nil {
return v, err
}
if t&wide != 0 {
changeValue(v, math.Float64frombits(bits.ReverseBytes64(f)))
} else {
changeValue(v, math.Float32frombits(bits.ReverseBytes32(uint32(f))))
}
return v, nil
}
func (u *Unserializer) decodeComplex() (v reflect.Value, err error) {
v, err = u.decodeType()
if err != nil {
return v, err
}
t := u.data[u.pos-1]
var r, i reflect.Value
if r, err = u.decodeFloat(); err != nil {
return r, err
}
if i, err = u.decodeFloat(); err != nil {
return i, err
}
if t&wide != 0 {
changeValue(v, complex(r.Interface().(float64), i.Interface().(float64)))
} else {
changeValue(v, complex(r.Interface().(float32), i.Interface().(float32)))
}
return v, nil
}
func (u *Unserializer) decodeUintptr() (reflect.Value, error) {
v, ptr, t, err := u.decodeTypeWithLength(false)
if err != nil {
return v, err
}
if t&raw != 0 {
changeValue(v, unsafe.Pointer(uintptr(ptr)))
} else {
changeValue(v, uintptr(ptr))
}
return v, nil
}
func (u *Unserializer) decodeChan(isNil bool) (reflect.Value, error) {
v, err := u.decodeType()
if err != nil {
return v, err
}
if isNil {
return v, nil
}
size := int(u.data[u.pos]&0b111) + 1
u.pos++
buffSize, err := u.readUint(size)
if err != nil {
return v, err
}
t := v.Type()
if t.ChanDir() == reflect.BothDir {
return reflect.MakeChan(t, int(buffSize)), nil
}
el := t.Elem()
biDirChan := reflect.ChanOf(reflect.BothDir, el)
unDirChan := reflect.ChanOf(t.ChanDir(), el)
v = reflect.MakeChan(biDirChan, int(buffSize))
v = v.Convert(unDirChan).Convert(t)
return v, nil
}
func (u *Unserializer) decodeFunc() (reflect.Value, error) {
v, err := u.decodeType()
return v, err
}
func (u *Unserializer) decodeList(isNil bool) (reflect.Value, error) {
v, l, t, err := u.decodeTypeWithLength(isNil)
if err != nil {
return v, err
}
length := int(l)
if t&fixed == 0 && !isNil {
v = reflect.MakeSlice(v.Type(), length, length)
}
u.refs[u.cnt] = v
u.cnt++
for i := 0; i < length; i++ {
if _, err = u.decode(v.Index(i)); err != nil {
return reflect.Value{}, err
}
}
return v, nil
}
func (u *Unserializer) decodeMap(isNil bool) (reflect.Value, error) {
mp, l, _, err := u.decodeTypeWithLength(isNil)
if err != nil {
return mp, err
}
length := int(l)
if !isNil {
mp = reflect.MakeMapWithSize(mp.Type(), length)
}
u.refs[u.cnt] = mp
u.cnt++
var k, v reflect.Value
for i := 0; i < length; i++ {
if k, err = u.decode(reflect.Value{}); err != nil {
return v, err
}
if v, err = u.decode(reflect.Value{}); err != nil {
return v, err
}
mp.SetMapIndex(k, v)
}
return mp, nil
}
func (u *Unserializer) decodeStruct() (reflect.Value, error) {
v, l, _, err := u.decodeTypeWithLength(false)
if err != nil {
return v, err
}
u.refs[u.cnt] = v
u.cnt++
length := int(l)
switch GetStructCodingMode() {
case StructCodingModeIndex:
var field, fieldIndex reflect.Value
for i := 0; i < length; i++ {
fieldIndex, err = u.decodeInt()
if err != nil {
return v, err
}
field = v.Field(int(fieldIndex.Uint()))
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
_, err = u.decode(field)
if err != nil {
return v, err
}
}
case StructCodingModeName:
var field, fieldIndex, fieldName reflect.Value
for i := 0; i < length; i++ {
fieldName, err = u.decodeString()
if err != nil {
return v, err
}
if fieldName.String() == "_" {
fieldIndex, err = u.decodeInt()
if err != nil {
return v, err
}
field = v.Field(int(fieldIndex.Uint()))
} else {
field = v.FieldByName(fieldName.String())
}
if field.IsValid() {
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
}
_, err = u.decode(field)
if err != nil {
return v, err
}
}
default:
for i := 0; i < length; i++ {
field := v.Field(i)
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
_, err = u.decode(field)
if err != nil {
return v, err
}
}
}
return v, nil
}
func (u *Unserializer) decodePointer(value reflect.Value) (reflect.Value, error) {
v, err := u.decodeType()
if err != nil {
return v, err
}
cnt := u.cnt
u.refs[cnt] = v
u.cnt++
u.rels[u.cnt] = cnt
el, err := u.decode(reflect.Value{})
if err != nil {
return v, err
}
if v.IsValid() {
if el.IsValid() {
changeValue(v, u.pointerTo(el).Interface())
}
} else if el.IsValid() {
v = u.pointerTo(el)
} else if value.IsValid() {
if value.Kind() == reflect.Interface {
v = u.pointerTo(reflect.Zero(value.Type().Elem()))
} else {
v = reflect.Zero(value.Type())
}
} else {
v = reflect.ValueOf((*any)(nil))
}
u.refs[cnt] = v
return v, nil
}
func (u *Unserializer) decodeReference(value reflect.Value) (v reflect.Value, err error) {
t := u.data[u.pos]
if t&val != 0 { // forward references
return u.decodePointedValue(value)
}
// back references
var cnt uint32
if v, cnt, err = u.extractReferenceValue(); err != nil {
return v, err
}
v = u.pointerTo(v)
u.refs[u.cnt] = v
u.rels[cnt] = u.cnt
u.cnt++
return v, nil
}
func (u *Unserializer) decodePointedValue(value reflect.Value) (v reflect.Value, err error) {
var cnt uint32
if v, cnt, err = u.extractReferenceValue(); err != nil {
return v, err
}
if value.IsValid() {
value.Set(v)
v = value
}
relCnt, exists := u.rels[cnt]
if exists {
r := u.refs[relCnt]
r.Set(u.pointerTo(v))
}
u.refs[cnt] = v
return v, nil
}
func (u *Unserializer) extractReferenceValue() (reflect.Value, uint32, error) {
size := (u.data[u.pos] & 0b111) + 1
u.pos++
cnt, err := u.readUint(int(size))
if err != nil {
return reflect.Value{}, 0, err
}
c := uint32(cnt)
v, ok := u.refs[c]
if !ok {
return v, 0, fmt.Errorf("invalid reference #%d", c)
}
return v, c, nil
}
func (u *Unserializer) pointerTo(v reflect.Value) reflect.Value {
var p reflect.Value
if v.IsValid() {
if v.CanAddr() {
p = reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr()))
} else {
p = reflect.New(v.Type())
p.Elem().Set(v)
}
} else {
p = reflect.ValueOf((*any)(nil))
}
return p
}
func (u *Unserializer) decodeInterface(isNil bool) (reflect.Value, error) {
v, err := u.decodeType()
if err != nil {
return v, err
}
u.refs[u.cnt] = v
if isNil {
return v, nil
}
return u.decode(v)
}
func (u *Unserializer) decodeType() (reflect.Value, error) {
typeSignature, err := u.readTypeSignature()
if err != nil || len(typeSignature) == 1 && typeSignature[0] == tPointer {
return reflect.Value{}, err
}
return tChecker.valueOf(typeSignature)
}
func (u *Unserializer) decodeTypeWithLength(isNil bool) (reflect.Value, uint64, byte, error) {
v, err := u.decodeType()
if err != nil {
return v, 0, 0, err
}
t := u.data[u.pos-1]
if isNil {
return v, 0, t, nil
}
size := int((t & 0b111) + 1)
length, err := u.readLength(size)
if err != nil {
return v, 0, 0, err
}
return v, length, t, nil
}
func (u *Unserializer) readLength(lengthSize int) (uint64, error) {
length, err := u.readUint(lengthSize)
if lengthSize > 4 && math.MaxUint == math.MaxUint32 {
return 0, errors.New("int type has 32 bit size but serialized value has 64 bit size")
}
if err != nil {
return 0, err
}
return length, nil
}
func (u *Unserializer) readUint(size int) (uint64, error) {
var i uint64
for ; size > 0; size-- {
if u.pos >= u.size {
return 0, io.ErrUnexpectedEOF
}
i = i<<8 | uint64(u.data[u.pos])
u.pos++
}
return i, nil
}
func (u *Unserializer) readTypeSignature() ([]byte, error) {
if u.pos >= u.size {
return nil, io.ErrUnexpectedEOF
}
t := u.data[u.pos]
u.pos++
switch t & mask {
case tType:
size := int((t & 0b11) + 1)
u.pos += size
if u.pos > u.size {
return nil, io.ErrUnexpectedEOF
}
b := append([]byte{}, u.data[u.pos-size-1:u.pos]...)
ut, err := u.readTypeSignature()
if err != nil {
return nil, err
}
b[0] &= mask
return append(b, ut...), nil
case tList:
return []byte{t & (mask | fixed)}, nil
case tInt8, tInt16, tInt32, tInt64:
return []byte{t & (mask | 0b11 | signed)}, nil
case tInt:
return []byte{t & (mask | signed)}, nil
case tFloat, tComplex:
return []byte{t & (mask | wide)}, nil
case tUintptr:
return []byte{t & (mask | raw)}, nil
case tChan:
if t&tFunc == tFunc {
return []byte{tFunc}, nil
}
return []byte{t & (mask | 0b11)}, nil
default:
return []byte{t & mask}, nil
}
}
func Unserialize(bytes []byte) (any, error) {
return NewUnserializer().Decode(bytes)
}