-
Notifications
You must be signed in to change notification settings - Fork 2
/
decoder.go
905 lines (853 loc) · 20.6 KB
/
decoder.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
package degob
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
// keep track of things we've named already so anonymous types don't get the
// same name somehow
var anonTypes map[string]struct{} = nil
func init() {
if os.Getenv("DEGOB_NORAND") == "" {
anonTypes = make(map[string]struct{})
if seed := os.Getenv("DEGOB_SEED"); seed != "" {
s, err := strconv.ParseInt(seed, 10, 64)
if err != nil {
panic(fmt.Errorf("Tried to use custom seed %s which cannot be parsed to int64: %v", seed, err))
}
rand.Seed(s)
} else {
rand.Seed(time.Now().UnixNano())
}
}
}
// This is a gob
// (byteCount (-type id, encoding of a wireType)* (type id, encoding of a value))*
const (
// how many types are in a uint64
uintByteSize = 8
// typeIds for user defined types can't actually go below this
smallestUserTypeId = 64
)
// Decoder reads and decodes gobs into our `Gob` type. A decoder can be
// used to stream using DecodeStream or just decode a reader containing
// any number of Gobs
type Decoder struct {
r io.Reader // The base reader
gobBuf gobBuf // Holds the current gob
buf [9]byte // a buffer for reading uints
seenTypes map[typeId]*WireType
decodedValue Value
bytesProcessed uint64
err *Error
inValue bool // are we currently reading a value? this is for streaming errors
}
type gobType uint8
const (
valueGob gobType = iota
typeGob
)
// NewDecoder returns a ready to use decoder for the underlying Reader
func NewDecoder(r io.Reader) *Decoder {
dec := new(Decoder)
dec.r = bufio.NewReader(r)
dec.seenTypes = make(map[typeId]*WireType)
dec.decodedValue = nil
return dec
}
// Decode reads all data on the input reader and decodes the Gobs returning
// a slice of `Gob`s. This
func (dec *Decoder) Decode() ([]*Gob, error) {
dec.bytesProcessed = 0
dec.clearGob()
gobs := make([]*Gob, 0, 5)
for {
dec.getGobPiece()
if dec.err != nil {
if dec.err.Err == io.EOF {
break
}
return nil, dec.err
}
dec.decodeGobPiece()
if dec.err != nil {
return nil, dec.err
}
// we read the value of one of the input gobs and now we're on
// to the next
if dec.decodedValue != nil {
g := new(Gob)
dec.setGob(g)
gobs = append(gobs, g)
dec.clearGob()
}
}
return gobs, nil
}
func (dec *Decoder) setGob(g *Gob) {
g.Value = dec.decodedValue
if len(dec.seenTypes) > 0 {
for _, t := range dec.seenTypes {
switch {
case t.SliceT != nil:
t.SliceT.ElemTypeString = dec.getName(t.SliceT.Elem)
case t.ArrayT != nil:
t.ArrayT.ElemTypeString = dec.getName(t.ArrayT.Elem)
case t.MapT != nil:
t.MapT.KeyTypeString = dec.getName(t.MapT.Key)
t.MapT.ElemTypeString = dec.getName(t.MapT.Elem)
case t.StructT != nil:
for _, f := range t.StructT.Field {
f.TypeString = dec.getName(typeId(f.Id))
}
}
}
g.Types = dec.seenTypes
}
}
// Result is used for streaming
type Result struct {
Gob *Gob
Err *Error
}
// streamError cleans up after a streaming error and returns whether
// or not we can continue
//
// I don't know enough to be 100% certain that this will always work but
// according to gob structure once I hit a non negative type id I've left
// my gob. The problem I have is that second star they put in that defintion
//
// (byteCount (-type id, encoding of a wireType)* (type id, encoding of a value))*
// here ~~~~~~^
// it would seem that that be an issue with this method of dealing with
// finding a problematic gob. How do I know that I'm actually in the last one?
func (dec *Decoder) streamError(c chan<- Result, stop <-chan struct{}) bool {
c <- Result{Err: dec.err}
if dec.err.Err == io.ErrUnexpectedEOF {
return false
}
if dec.inValue {
dec.decodedValue = nil
dec.err = nil
dec.clearGob()
return true
}
for {
// set it to nil so we can just keep eating
dec.err = nil
// we're jsut going to ignore errors until
// we are in a value piece and then break
dec.getGobPiece()
id := dec.readTypeId()
if id > 0 {
break
}
// if we ever hit EOF we have to leave
if dec.err.Err == io.EOF || dec.err.Err == io.ErrUnexpectedEOF {
return false
}
select {
case <-stop:
return false
default:
}
}
dec.decodedValue = nil
dec.err = nil
dec.clearGob()
return true
}
// DecodeStream keeps reading from the underlying reader and returning
// on the returned channel. Errors do not stop the decoding. You can
// stop the decoding by closing the passed stop struct. If it is nil
// DecodeStream doesn't stop until EOF.
//
// Note that we attempt to simply drop any gob that causes an error, but in
// practice it isn't particularly easy to test that. If you found that
// you're getting multiple errors in a row it could be easier to just
// stop the streamer and restart it.
//
// If you don't want any buffering just send buffer as 0.
func (dec *Decoder) DecodeStream(stop <-chan struct{}, buffer int) <-chan Result {
dec.bytesProcessed = 0
dec.clearGob()
c := make(chan Result, buffer)
go func() {
defer close(c)
for {
dec.getGobPiece()
if dec.err != nil {
if dec.err.Err == io.EOF {
return
}
if !dec.streamError(c, stop) {
return
}
}
dec.decodeGobPiece()
if dec.err != nil {
if !dec.streamError(c, stop) {
return
}
}
if dec.decodedValue != nil {
g := new(Gob)
dec.setGob(g)
c <- Result{Gob: g}
select {
case <-stop:
return
default:
dec.clearGob()
}
}
}
}()
return c
}
func (dec *Decoder) genError(err error) *Error {
return &Error{
Processed: dec.bytesProcessed,
Err: err,
RawGob: dec.gobBuf.Data(),
}
}
// Loads a gob from the reader into the current gob buffer
func (dec *Decoder) getGobPiece() {
if dec.err != nil {
return
}
size, width, err := readUint(dec.r, dec.buf[:], &dec.bytesProcessed)
if err != nil {
if err.Err == io.ErrUnexpectedEOF {
// we actually are OK with an EOF here
err.Err = io.EOF
}
dec.err = err
return
}
dec.gobBuf.Reset()
dec.gobBuf.Grow(width + int(size))
_, err_ := dec.gobBuf.Write(dec.buf[:width])
if err != nil {
dec.err = dec.genError(err_)
return
}
dec.gobBuf.Consumed(width)
// read the entire gob into the gob buffer
_, err_ = io.ReadFull(dec.r, dec.gobBuf.Bytes())
if err_ != nil {
if err_ == io.EOF {
dec.err = dec.genError(io.ErrUnexpectedEOF)
return
}
dec.err = dec.genError(err_)
return
}
}
// main decoding entrypoint, decodes the gob in the gobBuf
//
// this function consumes the entire gobBuf until EOF
func (dec *Decoder) decodeGobPiece() {
if dec.err != nil {
return
}
for dec.err == nil && dec.gobBuf.Len() > 0 {
id := dec.readTypeId()
if id >= 0 {
dec.inValue = true
dec.decodedValue = dec.valueForType(id)
if dec.err != nil {
return
}
w, ok := dec.seenTypes[id]
if !ok || w.StructT == nil {
dec.consumeNextUint(0)
}
// each gob will have a value so after we read it
// let's return and add it to the returned *Gob's
dec.readValue(id, &dec.decodedValue)
return
}
dec.inValue = false
// we have a type definition
dec.readType(-id)
}
}
func (dec *Decoder) readTypeId() typeId {
if dec.err != nil {
return 0
}
n, _, err := readUint(&dec.gobBuf, dec.buf[:], &dec.bytesProcessed)
if err != nil {
dec.err = err
return 0
}
return typeId(uintToInt(n))
}
// valueForWireType creates a new value for the wiretype set as
// the default
func (dec *Decoder) valueForWireType(w *WireType) Value {
switch {
case w.StructT != nil:
v := new(structValue)
v.name = w.StructT.CommonType.Name
//v.fields = make(map[string]Value)
v.fields = make(structFields, len(w.StructT.Field))
for i, f := range w.StructT.Field {
v.fields[i] = structField{
name: f.Name,
value: dec.valueForType(typeId(f.Id)),
}
}
/*
for _, f := range w.StructT.Field {
v.fields[f.Name] = dec.valueForType(typeId(f.Id))
}
*/
return v
case w.SliceT != nil:
v := new(sliceValue)
v.elemType = dec.getName(w.SliceT.Elem)
return v
case w.ArrayT != nil:
v := new(arrayValue)
v.length = w.ArrayT.Len
v.elemType = dec.getName(w.ArrayT.Elem)
v.values = make([]Value, v.length)
for i := 0; i < v.length; i++ {
v.values[i] = dec.valueForType(w.ArrayT.Elem)
}
return v
case w.MapT != nil:
v := new(mapValue)
v.keyType = dec.getName(w.MapT.Key)
v.elemType = dec.getName(w.MapT.Elem)
//v.values = make(map[Value]Value)
return v
// These next types ruin everything. The person who designed the type
// gets to encode the value however they want.
case w.BinaryMarshalerT != nil:
v := new(opaqueEncodedValue)
v.name = w.BinaryMarshalerT.CommonType.Name
return v
case w.TextMarshalerT != nil:
v := new(opaqueEncodedValue)
v.name = w.TextMarshalerT.CommonType.Name
return v
case w.GobEncoderT != nil:
v := new(opaqueEncodedValue)
v.name = w.GobEncoderT.CommonType.Name
return v
default:
panic("all nil in wiretype")
}
}
func (dec *Decoder) valueForType(id typeId) Value {
if isBuiltin(id) {
return valueFor(id)
}
if w, ok := dec.seenTypes[id]; ok {
return dec.valueForWireType(w)
}
dec.err = dec.genError(errors.New("gob had value for unknown type"))
return nil
}
func (dec *Decoder) readValue(id typeId, v *Value) {
if dec.err != nil {
return
}
wire, ok := dec.seenTypes[id]
if !ok {
if !isBuiltin(id) {
dec.err = dec.genError(errors.New("unexpected type"))
return
}
dec.readBuiltinValue(id, v)
} else {
switch {
case wire.StructT != nil:
dec.readStructValue(wire, v)
case wire.MapT != nil:
dec.readMapValue(wire, v)
case wire.SliceT != nil:
dec.readSliceValue(wire, v)
case wire.ArrayT != nil:
dec.readArrayValue(wire, v)
case wire.GobEncoderT != nil:
dec.readGobEncoderValue(wire, v)
case wire.BinaryMarshalerT != nil:
dec.readBinaryMarshalerValue(wire, v)
case wire.TextMarshalerT != nil:
dec.readTextMarshalerValue(wire, v)
}
}
}
func (dec *Decoder) readBuiltinValue(id typeId, val *Value) {
if dec.err != nil {
return
}
switch id {
case _bool_id:
b := dec.nextUint()
// should I check that it is 0 or 1?
if b == 0 {
*val = _bool_type(false)
} else {
*val = _bool_type(true)
}
case _int_id:
v := dec.nextUint()
if v&1 != 0 {
*val = _int_type(^int64(v >> 1))
} else {
*val = _int_type(int64(v >> 1))
}
case _uint_id:
v := dec.nextUint()
*val = _uint_type(v)
case _float_id:
v := dec.nextUint()
*val = _float_type(uintToFloat(v))
case _complex_id:
r := dec.nextUint()
i := dec.nextUint()
*val = _complex_type(uintToComplex(r, i))
case _bytes_id:
l := int(dec.nextUint())
b := make([]byte, l)
dec.gobBuf.Read(b)
*val = _bytes_type(b)
case _string_id:
l := int(dec.nextUint())
b := make([]byte, l)
dec.gobBuf.Read(b)
*val = _string_type(b)
case _interface_id:
nameLen := int(dec.nextUint())
if nameLen == 0 {
dec.readNilInterface(val)
} else {
dec.readNonNilInterface(val, nameLen)
}
default:
panic("id was not a builtin id")
}
}
func (dec *Decoder) readNilInterface(val *Value) {
var into interfaceValue
into.value = _nil_value{}
*val = into
}
func (dec *Decoder) readNonNilInterface(v *Value, nl int) {
if dec.err != nil {
return
}
var into interfaceValue
nameB := make([]byte, nl)
dec.gobBuf.Read(nameB)
// interface names are kinda weird in that they will include
// the entire path to the interface type including package.
// We don't actually want that
into.name = string(nameB)
if strings.Contains(into.name, ".") {
tmp := strings.Split(into.name, ".")
into.name = tmp[len(tmp)-1]
}
for {
id := dec.readTypeId()
if id < 0 {
dec.readType(-id)
dec.getGobPiece()
} else {
// hmm
// TODO: What is this next uint telling me?
// I think it is a length of the next block to read
_ = dec.nextUint()
w, ok := dec.seenTypes[id]
if !ok || w.StructT == nil {
dec.consumeNextUint(0)
}
dec.readValue(id, &into.value)
break
}
}
*v = into
}
func (dec *Decoder) readGobEncoderValue(wire *WireType, val *Value) {
if dec.err != nil {
return
}
into := dec.valueForWireType(wire).(*opaqueEncodedValue)
dec.readOpaqueEncodedValue(into, val)
}
func (dec *Decoder) readBinaryMarshalerValue(wire *WireType, val *Value) {
if dec.err != nil {
return
}
into := dec.valueForWireType(wire).(*opaqueEncodedValue)
dec.readOpaqueEncodedValue(into, val)
}
func (dec *Decoder) readTextMarshalerValue(wire *WireType, val *Value) {
if dec.err != nil {
return
}
into := dec.valueForWireType(wire).(*opaqueEncodedValue)
dec.readOpaqueEncodedValue(into, val)
}
func (dec *Decoder) readOpaqueEncodedValue(val *opaqueEncodedValue, into *Value) {
if dec.err != nil {
return
}
l := dec.nextUint()
b := make([]byte, l)
dec.gobBuf.Read(b)
val.value = _bytes_type(b)
*into = val
}
func (dec *Decoder) readMapValue(wire *WireType, val *Value) {
if dec.err != nil {
return
}
into := dec.valueForWireType(wire).(*mapValue)
length := int(dec.nextUint())
into.values = make([]mapEntry, length)
for i := 0; i < length; i++ {
kVal := new(Value)
eVal := new(Value)
dec.readValue(wire.MapT.Key, kVal)
dec.readValue(wire.MapT.Elem, eVal)
into.values[i] = mapEntry{key: *kVal, elem: *eVal}
//into.values[*kVal] = *eVal
}
*val = into
}
func (dec *Decoder) readSliceValue(wire *WireType, val *Value) {
if dec.err != nil {
return
}
length := int(dec.nextUint())
into := dec.valueForWireType(wire).(*sliceValue)
into.values = make([]Value, length)
for i := 0; i < length; i++ {
dec.readValue(wire.SliceT.Elem, &into.values[i])
}
*val = into
}
func (dec *Decoder) readArrayValue(wire *WireType, val *Value) {
if dec.err != nil {
return
}
into := dec.valueForWireType(wire).(*arrayValue)
length := int(dec.nextUint())
for i := 0; i < length; i++ {
dec.readValue(wire.ArrayT.Elem, &into.values[i])
}
*val = into
}
func (dec *Decoder) readStructValue(wire *WireType, val *Value) {
if dec.err != nil {
return
}
into := dec.valueForWireType(wire).(*structValue)
fields := wire.StructT.Field
fieldNum := -1
for {
delta := int(dec.nextUint())
if delta == 0 || dec.err != nil {
break
}
fieldNum += delta
if fieldNum < 0 || fieldNum >= len(fields) {
dec.err = dec.genError(errors.New("bad fieldnum"))
return
}
id := typeId(fields[fieldNum].Id)
var v Value
if isBuiltin(id) {
v = valueFor(id)
}
dec.readValue(id, &v)
into.fields[fieldNum].value = v
//into.fields[fields[fieldNum].Name] = v
}
*val = into
}
// reads newly defined types. These will always come as WireType structs
func (dec *Decoder) readType(id typeId) {
if dec.err != nil {
return
}
if id < smallestUserTypeId || dec.seenTypes[id] != nil {
dec.err = errDuplicateType(dec.bytesProcessed, nil)
return
}
wire := new(WireType)
dec.decodeType(id, wire)
// Every type definition will be followed by two null bytes
dec.consumeNextUint(0)
dec.consumeNextUint(0)
dec.seenTypes[id] = wire
}
// reads the gobBuf and stores the read WireType only operates one
// WireType at a time
func (dec *Decoder) decodeType(id typeId, w *WireType) {
if dec.err != nil {
return
}
delta := int(dec.nextUint())
fieldNum := delta - 1
dec.consumeNextUint(1)
switch fieldNum {
case 0:
dec.decodeArray(id, w)
case 1:
dec.decodeSlice(id, w)
case 2:
dec.decodeStruct(id, w)
case 3:
dec.decodeMap(id, w)
case 4:
dec.decodeGobEncoder(id, w)
case 5:
dec.decodeBinaryMarshaler(id, w)
case 6:
dec.decodeTextMarshaler(id, w)
default:
dec.err = errUnknownDelta(dec.bytesProcessed, dec.gobBuf.Bytes())
return
}
}
func (dec *Decoder) consumeNextUint(expected int) {
if dec.err != nil {
return
}
delta := int(dec.nextUint())
if delta != expected {
dec.err = dec.genError(fmt.Errorf("expected delta %d but got %d", expected, delta))
return
}
}
func (dec *Decoder) nextUint() uint64 {
if dec.err != nil {
return 0
}
delta, _, err := readUint(&dec.gobBuf, dec.buf[:], &dec.bytesProcessed)
if err != nil {
dec.err = err
return 0
}
return delta
}
func (dec *Decoder) decodeArray(id typeId, w *WireType) {
if dec.err != nil {
return
}
common := dec.decodeCommon()
dec.consumeNextUint(1)
elemId := dec.readTypeId()
dec.consumeNextUint(1)
l := int(uintToInt(dec.nextUint()))
w.ArrayT = &ArrayType{
CommonType: common,
Elem: elemId,
Len: l,
}
}
func (dec *Decoder) decodeSlice(id typeId, w *WireType) {
if dec.err != nil {
return
}
common := dec.decodeCommon()
dec.consumeNextUint(1)
elemId := dec.readTypeId()
w.SliceT = &SliceType{
CommonType: common,
Elem: elemId,
}
}
func (dec *Decoder) decodeStruct(id typeId, w *WireType) {
if dec.err != nil {
return
}
common := dec.decodeCommon()
fields := dec.decodeFields()
w.StructT = &StructType{
CommonType: common,
Field: fields,
}
// set the name if it is anonymous
if common.Name == "" {
dec.anonymousStructTypeName(w)
}
}
func (dec *Decoder) decodeMap(id typeId, w *WireType) {
if dec.err != nil {
return
}
common := dec.decodeCommon()
dec.consumeNextUint(1)
keyId := dec.readTypeId()
dec.consumeNextUint(1)
elemId := dec.readTypeId()
w.MapT = &MapType{
CommonType: common,
Key: keyId,
Elem: elemId,
}
}
func (dec *Decoder) decodeBinaryMarshaler(id typeId, w *WireType) {
if dec.err != nil {
return
}
common := dec.decodeCommon()
w.BinaryMarshalerT = &BinaryMarshalerType{
CommonType: common,
}
}
func (dec *Decoder) decodeTextMarshaler(id typeId, w *WireType) {
if dec.err != nil {
return
}
common := dec.decodeCommon()
w.TextMarshalerT = &TextMarshalerType{
CommonType: common,
}
}
func (dec *Decoder) decodeGobEncoder(id typeId, w *WireType) {
if dec.err != nil {
return
}
common := dec.decodeCommon()
w.GobEncoderT = &GobEncoderType{
CommonType: common,
}
}
func (dec *Decoder) decodeFields() []*FieldType {
if dec.err != nil {
return nil
}
// TODO: potential bug here for empty structs?
dec.consumeNextUint(1)
nfields := int(dec.nextUint())
fields := make([]*FieldType, nfields)
for i := 0; i < nfields; i++ {
fields[i] = new(FieldType)
dec.consumeNextUint(1)
dec.decodeString(&fields[i].Name)
dec.consumeNextUint(1)
fields[i].Id = int(dec.readTypeId())
dec.consumeNextUint(0)
}
return fields
}
func (dec *Decoder) decodeCommon() CommonType {
var c CommonType
if dec.err != nil {
return c
}
fieldNum := -1
for {
delta := int(dec.nextUint())
// the end is noted with delta 0
if delta == 0 {
break
}
fieldNum += delta
switch fieldNum {
case 0:
dec.decodeString(&c.Name)
case 1:
c.Id = int(dec.readTypeId())
default:
dec.err = errCorruptCommonType(dec.bytesProcessed, dec.gobBuf.Bytes())
return c
}
}
return c
}
func (dec *Decoder) decodeString(into *string) {
if dec.err != nil {
return
}
l := dec.nextUint()
b := make([]byte, l)
r, err_ := dec.gobBuf.Read(b)
if err_ != nil {
if err_ == io.EOF {
dec.err = dec.genError(io.ErrUnexpectedEOF)
return
}
dec.err = dec.genError(err_)
return
}
dec.bytesProcessed += uint64(r)
if uint64(r) != l {
dec.err = errBadString(dec.bytesProcessed, dec.gobBuf.Bytes())
return
}
*into = string(b)
}
// clears any gob that is already seenTypes
func (dec *Decoder) clearGob() {
dec.seenTypes = make(map[typeId]*WireType)
dec.decodedValue = nil
}
func (dec *Decoder) getName(id typeId) string {
if isBuiltin(id) {
return strings.TrimSpace(id.name())
}
if v, ok := dec.seenTypes[id]; ok {
switch {
case v.StructT != nil:
if v.StructT.CommonType.Name == "" {
return strings.TrimSpace(dec.anonymousStructTypeName(v))
}
return strings.TrimSpace(v.StructT.CommonType.Name)
case v.SliceT != nil:
return strings.TrimSpace(v.SliceT.CommonType.Name)
case v.MapT != nil:
return strings.TrimSpace(v.MapT.CommonType.Name)
case v.ArrayT != nil:
return strings.TrimSpace(v.ArrayT.CommonType.Name)
case v.BinaryMarshalerT != nil:
return strings.TrimSpace(v.BinaryMarshalerT.CommonType.Name)
case v.GobEncoderT != nil:
return strings.TrimSpace(v.GobEncoderT.CommonType.Name)
case v.TextMarshalerT != nil:
return strings.TrimSpace(v.TextMarshalerT.CommonType.Name)
default:
panic("empty wiretype at chosen id")
}
} else {
panic("something has gone very wrong")
}
}
func (dec *Decoder) anonymousStructTypeName(w *WireType) string {
s := fmt.Sprintf("Anon%d", w.StructT.Id)
if anonTypes != nil {
follow := make([]byte, 4)
_, _ = rand.Read(follow)
var followString string
for {
followString = hex.EncodeToString(follow)
if _, ok := anonTypes[followString]; ok {
// this shouldn't happen much
_, _ = rand.Read(follow)
} else {
anonTypes[followString] = struct{}{}
break
}
}
s += fmt.Sprintf("_%s", followString)
}
w.StructT.CommonType.Name = s
return s
}