1
2
3
4
5
6
7 package gob
8
9 import (
10 "encoding"
11 "errors"
12 "internal/saferio"
13 "io"
14 "math"
15 "math/bits"
16 "reflect"
17 )
18
19 var (
20 errBadUint = errors.New("gob: encoded unsigned integer out of range")
21 errBadType = errors.New("gob: unknown type id or corrupted data")
22 errRange = errors.New("gob: bad data: field numbers out of bounds")
23 )
24
25 type decHelper func(state *decoderState, v reflect.Value, length int, ovfl error) bool
26
27
28
29 type decoderState struct {
30 dec *Decoder
31
32
33 b *decBuffer
34 fieldnum int
35 next *decoderState
36 }
37
38
39
40 type decBuffer struct {
41 data []byte
42 offset int
43 }
44
45 func (d *decBuffer) Read(p []byte) (int, error) {
46 n := copy(p, d.data[d.offset:])
47 if n == 0 && len(p) != 0 {
48 return 0, io.EOF
49 }
50 d.offset += n
51 return n, nil
52 }
53
54 func (d *decBuffer) Drop(n int) {
55 if n > d.Len() {
56 panic("drop")
57 }
58 d.offset += n
59 }
60
61 func (d *decBuffer) ReadByte() (byte, error) {
62 if d.offset >= len(d.data) {
63 return 0, io.EOF
64 }
65 c := d.data[d.offset]
66 d.offset++
67 return c, nil
68 }
69
70 func (d *decBuffer) Len() int {
71 return len(d.data) - d.offset
72 }
73
74 func (d *decBuffer) Bytes() []byte {
75 return d.data[d.offset:]
76 }
77
78
79 func (d *decBuffer) SetBytes(data []byte) {
80 d.data = data
81 d.offset = 0
82 }
83
84 func (d *decBuffer) Reset() {
85 d.data = d.data[0:0]
86 d.offset = 0
87 }
88
89
90
91 func (dec *Decoder) newDecoderState(buf *decBuffer) *decoderState {
92 d := dec.freeList
93 if d == nil {
94 d = new(decoderState)
95 d.dec = dec
96 } else {
97 dec.freeList = d.next
98 }
99 d.b = buf
100 return d
101 }
102
103 func (dec *Decoder) freeDecoderState(d *decoderState) {
104 d.next = dec.freeList
105 dec.freeList = d
106 }
107
108 func overflow(name string) error {
109 return errors.New(`value for "` + name + `" out of range`)
110 }
111
112
113
114 func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err error) {
115 width = 1
116 n, err := io.ReadFull(r, buf[0:width])
117 if n == 0 {
118 return
119 }
120 b := buf[0]
121 if b <= 0x7f {
122 return uint64(b), width, nil
123 }
124 n = -int(int8(b))
125 if n > uint64Size {
126 err = errBadUint
127 return
128 }
129 width, err = io.ReadFull(r, buf[0:n])
130 if err != nil {
131 if err == io.EOF {
132 err = io.ErrUnexpectedEOF
133 }
134 return
135 }
136
137 for _, b := range buf[0:width] {
138 x = x<<8 | uint64(b)
139 }
140 width++
141 return
142 }
143
144
145
146 func (state *decoderState) decodeUint() (x uint64) {
147 b, err := state.b.ReadByte()
148 if err != nil {
149 error_(err)
150 }
151 if b <= 0x7f {
152 return uint64(b)
153 }
154 n := -int(int8(b))
155 if n > uint64Size {
156 error_(errBadUint)
157 }
158 buf := state.b.Bytes()
159 if len(buf) < n {
160 errorf("invalid uint data length %d: exceeds input size %d", n, len(buf))
161 }
162
163
164 for _, b := range buf[0:n] {
165 x = x<<8 | uint64(b)
166 }
167 state.b.Drop(n)
168 return x
169 }
170
171
172
173 func (state *decoderState) decodeInt() int64 {
174 x := state.decodeUint()
175 if x&1 != 0 {
176 return ^int64(x >> 1)
177 }
178 return int64(x >> 1)
179 }
180
181
182
183
184 func (state *decoderState) getLength() (int, bool) {
185 n := int(state.decodeUint())
186 if n < 0 || state.b.Len() < n || tooBig <= n {
187 return 0, false
188 }
189 return n, true
190 }
191
192
193 type decOp func(i *decInstr, state *decoderState, v reflect.Value)
194
195
196 type decInstr struct {
197 op decOp
198 field int
199 index []int
200 ovfl error
201 }
202
203
204 func ignoreUint(i *decInstr, state *decoderState, v reflect.Value) {
205 state.decodeUint()
206 }
207
208
209
210 func ignoreTwoUints(i *decInstr, state *decoderState, v reflect.Value) {
211 state.decodeUint()
212 state.decodeUint()
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226 func decAlloc(v reflect.Value) reflect.Value {
227 for v.Kind() == reflect.Pointer {
228 if v.IsNil() {
229 v.Set(reflect.New(v.Type().Elem()))
230 }
231 v = v.Elem()
232 }
233 return v
234 }
235
236
237 func decBool(i *decInstr, state *decoderState, value reflect.Value) {
238 value.SetBool(state.decodeUint() != 0)
239 }
240
241
242 func decInt8(i *decInstr, state *decoderState, value reflect.Value) {
243 v := state.decodeInt()
244 if v < math.MinInt8 || math.MaxInt8 < v {
245 error_(i.ovfl)
246 }
247 value.SetInt(v)
248 }
249
250
251 func decUint8(i *decInstr, state *decoderState, value reflect.Value) {
252 v := state.decodeUint()
253 if math.MaxUint8 < v {
254 error_(i.ovfl)
255 }
256 value.SetUint(v)
257 }
258
259
260 func decInt16(i *decInstr, state *decoderState, value reflect.Value) {
261 v := state.decodeInt()
262 if v < math.MinInt16 || math.MaxInt16 < v {
263 error_(i.ovfl)
264 }
265 value.SetInt(v)
266 }
267
268
269 func decUint16(i *decInstr, state *decoderState, value reflect.Value) {
270 v := state.decodeUint()
271 if math.MaxUint16 < v {
272 error_(i.ovfl)
273 }
274 value.SetUint(v)
275 }
276
277
278 func decInt32(i *decInstr, state *decoderState, value reflect.Value) {
279 v := state.decodeInt()
280 if v < math.MinInt32 || math.MaxInt32 < v {
281 error_(i.ovfl)
282 }
283 value.SetInt(v)
284 }
285
286
287 func decUint32(i *decInstr, state *decoderState, value reflect.Value) {
288 v := state.decodeUint()
289 if math.MaxUint32 < v {
290 error_(i.ovfl)
291 }
292 value.SetUint(v)
293 }
294
295
296 func decInt64(i *decInstr, state *decoderState, value reflect.Value) {
297 v := state.decodeInt()
298 value.SetInt(v)
299 }
300
301
302 func decUint64(i *decInstr, state *decoderState, value reflect.Value) {
303 v := state.decodeUint()
304 value.SetUint(v)
305 }
306
307
308
309
310
311
312 func float64FromBits(u uint64) float64 {
313 v := bits.ReverseBytes64(u)
314 return math.Float64frombits(v)
315 }
316
317
318
319
320
321 func float32FromBits(u uint64, ovfl error) float64 {
322 v := float64FromBits(u)
323 av := v
324 if av < 0 {
325 av = -av
326 }
327
328 if math.MaxFloat32 < av && av <= math.MaxFloat64 {
329 error_(ovfl)
330 }
331 return v
332 }
333
334
335
336 func decFloat32(i *decInstr, state *decoderState, value reflect.Value) {
337 value.SetFloat(float32FromBits(state.decodeUint(), i.ovfl))
338 }
339
340
341
342 func decFloat64(i *decInstr, state *decoderState, value reflect.Value) {
343 value.SetFloat(float64FromBits(state.decodeUint()))
344 }
345
346
347
348
349 func decComplex64(i *decInstr, state *decoderState, value reflect.Value) {
350 real := float32FromBits(state.decodeUint(), i.ovfl)
351 imag := float32FromBits(state.decodeUint(), i.ovfl)
352 value.SetComplex(complex(real, imag))
353 }
354
355
356
357
358 func decComplex128(i *decInstr, state *decoderState, value reflect.Value) {
359 real := float64FromBits(state.decodeUint())
360 imag := float64FromBits(state.decodeUint())
361 value.SetComplex(complex(real, imag))
362 }
363
364
365
366
367 func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) {
368 n, ok := state.getLength()
369 if !ok {
370 errorf("bad %s slice length: %d", value.Type(), n)
371 }
372 if value.Cap() < n {
373 safe := saferio.SliceCap[byte](uint64(n))
374 if safe < 0 {
375 errorf("%s slice too big: %d elements", value.Type(), n)
376 }
377 value.Set(reflect.MakeSlice(value.Type(), safe, safe))
378 ln := safe
379 i := 0
380 for i < n {
381 if i >= ln {
382
383
384
385
386
387 value.Grow(1)
388 }
389
390
391 ln = value.Cap()
392 if ln > n {
393 ln = n
394 }
395 value.SetLen(ln)
396 sub := value.Slice(i, ln)
397 if _, err := state.b.Read(sub.Bytes()); err != nil {
398 errorf("error decoding []byte at %d: %s", i, err)
399 }
400 i = ln
401 }
402 } else {
403 value.SetLen(n)
404 if _, err := state.b.Read(value.Bytes()); err != nil {
405 errorf("error decoding []byte: %s", err)
406 }
407 }
408 }
409
410
411
412
413 func decString(i *decInstr, state *decoderState, value reflect.Value) {
414 n, ok := state.getLength()
415 if !ok {
416 errorf("bad %s slice length: %d", value.Type(), n)
417 }
418
419 data := state.b.Bytes()
420 if len(data) < n {
421 errorf("invalid string length %d: exceeds input size %d", n, len(data))
422 }
423 s := string(data[:n])
424 state.b.Drop(n)
425 value.SetString(s)
426 }
427
428
429 func ignoreUint8Array(i *decInstr, state *decoderState, value reflect.Value) {
430 n, ok := state.getLength()
431 if !ok {
432 errorf("slice length too large")
433 }
434 bn := state.b.Len()
435 if bn < n {
436 errorf("invalid slice length %d: exceeds input size %d", n, bn)
437 }
438 state.b.Drop(n)
439 }
440
441
442
443
444
445 type decEngine struct {
446 instr []decInstr
447 numInstr int
448 }
449
450
451
452
453 func (dec *Decoder) decodeSingle(engine *decEngine, value reflect.Value) {
454 state := dec.newDecoderState(&dec.buf)
455 defer dec.freeDecoderState(state)
456 state.fieldnum = singletonField
457 if state.decodeUint() != 0 {
458 errorf("decode: corrupted data: non-zero delta for singleton")
459 }
460 instr := &engine.instr[singletonField]
461 instr.op(instr, state, value)
462 }
463
464
465
466
467
468
469 func (dec *Decoder) decodeStruct(engine *decEngine, value reflect.Value) {
470 state := dec.newDecoderState(&dec.buf)
471 defer dec.freeDecoderState(state)
472 state.fieldnum = -1
473 for state.b.Len() > 0 {
474 delta := int(state.decodeUint())
475 if delta < 0 {
476 errorf("decode: corrupted data: negative delta")
477 }
478 if delta == 0 {
479 break
480 }
481 if state.fieldnum >= len(engine.instr)-delta {
482 error_(errRange)
483 }
484 fieldnum := state.fieldnum + delta
485 instr := &engine.instr[fieldnum]
486 var field reflect.Value
487 if instr.index != nil {
488
489 field = value.FieldByIndex(instr.index)
490 if field.Kind() == reflect.Pointer {
491 field = decAlloc(field)
492 }
493 }
494 instr.op(instr, state, field)
495 state.fieldnum = fieldnum
496 }
497 }
498
499 var noValue reflect.Value
500
501
502 func (dec *Decoder) ignoreStruct(engine *decEngine) {
503 state := dec.newDecoderState(&dec.buf)
504 defer dec.freeDecoderState(state)
505 state.fieldnum = -1
506 for state.b.Len() > 0 {
507 delta := int(state.decodeUint())
508 if delta < 0 {
509 errorf("ignore decode: corrupted data: negative delta")
510 }
511 if delta == 0 {
512 break
513 }
514 fieldnum := state.fieldnum + delta
515 if fieldnum >= len(engine.instr) {
516 error_(errRange)
517 }
518 instr := &engine.instr[fieldnum]
519 instr.op(instr, state, noValue)
520 state.fieldnum = fieldnum
521 }
522 }
523
524
525
526 func (dec *Decoder) ignoreSingle(engine *decEngine) {
527 state := dec.newDecoderState(&dec.buf)
528 defer dec.freeDecoderState(state)
529 state.fieldnum = singletonField
530 delta := int(state.decodeUint())
531 if delta != 0 {
532 errorf("decode: corrupted data: non-zero delta for singleton")
533 }
534 instr := &engine.instr[singletonField]
535 instr.op(instr, state, noValue)
536 }
537
538
539 func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) {
540 if helper != nil && helper(state, value, length, ovfl) {
541 return
542 }
543 instr := &decInstr{elemOp, 0, nil, ovfl}
544 isPtr := value.Type().Elem().Kind() == reflect.Pointer
545 ln := value.Len()
546 for i := 0; i < length; i++ {
547 if state.b.Len() == 0 {
548 errorf("decoding array or slice: length exceeds input size (%d elements)", length)
549 }
550 if i >= ln {
551
552
553 value.Grow(1)
554 cp := value.Cap()
555 if cp > length {
556 cp = length
557 }
558 value.SetLen(cp)
559 ln = cp
560 }
561 v := value.Index(i)
562 if isPtr {
563 v = decAlloc(v)
564 }
565 elemOp(instr, state, v)
566 }
567 }
568
569
570
571
572 func (dec *Decoder) decodeArray(state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) {
573 if n := state.decodeUint(); n != uint64(length) {
574 errorf("length mismatch in decodeArray")
575 }
576 dec.decodeArrayHelper(state, value, elemOp, length, ovfl, helper)
577 }
578
579
580 func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Value, instr *decInstr) reflect.Value {
581 v := value
582 if isPtr {
583 v = decAlloc(value)
584 }
585
586 op(instr, state, v)
587 return value
588 }
589
590
591
592
593
594 func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value reflect.Value, keyOp, elemOp decOp, ovfl error) {
595 n := int(state.decodeUint())
596 if value.IsNil() {
597 value.Set(reflect.MakeMapWithSize(mtyp, n))
598 }
599 keyIsPtr := mtyp.Key().Kind() == reflect.Pointer
600 elemIsPtr := mtyp.Elem().Kind() == reflect.Pointer
601 keyInstr := &decInstr{keyOp, 0, nil, ovfl}
602 elemInstr := &decInstr{elemOp, 0, nil, ovfl}
603 keyP := reflect.New(mtyp.Key())
604 elemP := reflect.New(mtyp.Elem())
605 for i := 0; i < n; i++ {
606 key := decodeIntoValue(state, keyOp, keyIsPtr, keyP.Elem(), keyInstr)
607 elem := decodeIntoValue(state, elemOp, elemIsPtr, elemP.Elem(), elemInstr)
608 value.SetMapIndex(key, elem)
609 keyP.Elem().SetZero()
610 elemP.Elem().SetZero()
611 }
612 }
613
614
615 func (dec *Decoder) ignoreArrayHelper(state *decoderState, elemOp decOp, length int) {
616 instr := &decInstr{elemOp, 0, nil, errors.New("no error")}
617 for i := 0; i < length; i++ {
618 if state.b.Len() == 0 {
619 errorf("decoding array or slice: length exceeds input size (%d elements)", length)
620 }
621 elemOp(instr, state, noValue)
622 }
623 }
624
625
626 func (dec *Decoder) ignoreArray(state *decoderState, elemOp decOp, length int) {
627 if n := state.decodeUint(); n != uint64(length) {
628 errorf("length mismatch in ignoreArray")
629 }
630 dec.ignoreArrayHelper(state, elemOp, length)
631 }
632
633
634 func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) {
635 n := int(state.decodeUint())
636 keyInstr := &decInstr{keyOp, 0, nil, errors.New("no error")}
637 elemInstr := &decInstr{elemOp, 0, nil, errors.New("no error")}
638 for i := 0; i < n; i++ {
639 keyOp(keyInstr, state, noValue)
640 elemOp(elemInstr, state, noValue)
641 }
642 }
643
644
645
646 func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp decOp, ovfl error, helper decHelper) {
647 u := state.decodeUint()
648 typ := value.Type()
649 size := uint64(typ.Elem().Size())
650 nBytes := u * size
651 n := int(u)
652
653 if n < 0 || uint64(n) != u || nBytes > tooBig || (size > 0 && nBytes/size != u) {
654
655
656 errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size)
657 }
658 if value.Cap() < n {
659 safe := saferio.SliceCapWithSize(size, uint64(n))
660 if safe < 0 {
661 errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size)
662 }
663 value.Set(reflect.MakeSlice(typ, safe, safe))
664 } else {
665 value.SetLen(n)
666 }
667 dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper)
668 }
669
670
671 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) {
672 dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint()))
673 }
674
675
676
677
678 func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, value reflect.Value) {
679
680 nr := state.decodeUint()
681 if nr > 1<<31 {
682 errorf("invalid type name length %d", nr)
683 }
684 if nr > uint64(state.b.Len()) {
685 errorf("invalid type name length %d: exceeds input size", nr)
686 }
687 n := int(nr)
688 name := state.b.Bytes()[:n]
689 state.b.Drop(n)
690
691 if len(name) == 0 {
692
693 value.SetZero()
694 return
695 }
696 if len(name) > 1024 {
697 errorf("name too long (%d bytes): %.20q...", len(name), name)
698 }
699
700 typi, ok := nameToConcreteType.Load(string(name))
701 if !ok {
702 errorf("name not registered for interface: %q", name)
703 }
704 typ := typi.(reflect.Type)
705
706
707 concreteId := dec.decodeTypeSequence(true)
708 if concreteId < 0 {
709 error_(dec.err)
710 }
711
712
713 state.decodeUint()
714
715 v := allocValue(typ)
716 dec.decodeValue(concreteId, v)
717 if dec.err != nil {
718 error_(dec.err)
719 }
720
721
722 if !typ.AssignableTo(ityp) {
723 errorf("%s is not assignable to type %s", typ, ityp)
724 }
725
726 value.Set(v)
727 }
728
729
730 func (dec *Decoder) ignoreInterface(state *decoderState) {
731
732 n, ok := state.getLength()
733 if !ok {
734 errorf("bad interface encoding: name too large for buffer")
735 }
736 bn := state.b.Len()
737 if bn < n {
738 errorf("invalid interface value length %d: exceeds input size %d", n, bn)
739 }
740 state.b.Drop(n)
741 id := dec.decodeTypeSequence(true)
742 if id < 0 {
743 error_(dec.err)
744 }
745
746 n, ok = state.getLength()
747 if !ok {
748 errorf("bad interface encoding: data length too large for buffer")
749 }
750 state.b.Drop(n)
751 }
752
753
754
755 func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, value reflect.Value) {
756
757 n, ok := state.getLength()
758 if !ok {
759 errorf("GobDecoder: length too large for buffer")
760 }
761 b := state.b.Bytes()
762 if len(b) < n {
763 errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, len(b))
764 }
765 b = b[:n]
766 state.b.Drop(n)
767 var err error
768
769 switch ut.externalDec {
770 case xGob:
771 err = value.Interface().(GobDecoder).GobDecode(b)
772 case xBinary:
773 err = value.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b)
774 case xText:
775 err = value.Interface().(encoding.TextUnmarshaler).UnmarshalText(b)
776 }
777 if err != nil {
778 error_(err)
779 }
780 }
781
782
783 func (dec *Decoder) ignoreGobDecoder(state *decoderState) {
784
785 n, ok := state.getLength()
786 if !ok {
787 errorf("GobDecoder: length too large for buffer")
788 }
789 bn := state.b.Len()
790 if bn < n {
791 errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, bn)
792 }
793 state.b.Drop(n)
794 }
795
796
797 var decOpTable = [...]decOp{
798 reflect.Bool: decBool,
799 reflect.Int8: decInt8,
800 reflect.Int16: decInt16,
801 reflect.Int32: decInt32,
802 reflect.Int64: decInt64,
803 reflect.Uint8: decUint8,
804 reflect.Uint16: decUint16,
805 reflect.Uint32: decUint32,
806 reflect.Uint64: decUint64,
807 reflect.Float32: decFloat32,
808 reflect.Float64: decFloat64,
809 reflect.Complex64: decComplex64,
810 reflect.Complex128: decComplex128,
811 reflect.String: decString,
812 }
813
814
815 var decIgnoreOpMap = map[typeId]decOp{
816 tBool: ignoreUint,
817 tInt: ignoreUint,
818 tUint: ignoreUint,
819 tFloat: ignoreUint,
820 tBytes: ignoreUint8Array,
821 tString: ignoreUint8Array,
822 tComplex: ignoreTwoUints,
823 }
824
825
826
827 func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) *decOp {
828 ut := userType(rt)
829
830 if ut.externalDec != 0 {
831 return dec.gobDecodeOpFor(ut)
832 }
833
834
835
836 if opPtr := inProgress[rt]; opPtr != nil {
837 return opPtr
838 }
839 typ := ut.base
840 var op decOp
841 k := typ.Kind()
842 if int(k) < len(decOpTable) {
843 op = decOpTable[k]
844 }
845 if op == nil {
846 inProgress[rt] = &op
847
848 switch t := typ; t.Kind() {
849 case reflect.Array:
850 name = "element of " + name
851 elemId := dec.wireType[wireId].ArrayT.Elem
852 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress)
853 ovfl := overflow(name)
854 helper := decArrayHelper[t.Elem().Kind()]
855 op = func(i *decInstr, state *decoderState, value reflect.Value) {
856 state.dec.decodeArray(state, value, *elemOp, t.Len(), ovfl, helper)
857 }
858
859 case reflect.Map:
860 keyId := dec.wireType[wireId].MapT.Key
861 elemId := dec.wireType[wireId].MapT.Elem
862 keyOp := dec.decOpFor(keyId, t.Key(), "key of "+name, inProgress)
863 elemOp := dec.decOpFor(elemId, t.Elem(), "element of "+name, inProgress)
864 ovfl := overflow(name)
865 op = func(i *decInstr, state *decoderState, value reflect.Value) {
866 state.dec.decodeMap(t, state, value, *keyOp, *elemOp, ovfl)
867 }
868
869 case reflect.Slice:
870 name = "element of " + name
871 if t.Elem().Kind() == reflect.Uint8 {
872 op = decUint8Slice
873 break
874 }
875 var elemId typeId
876 if tt := builtinIdToType(wireId); tt != nil {
877 elemId = tt.(*sliceType).Elem
878 } else {
879 elemId = dec.wireType[wireId].SliceT.Elem
880 }
881 elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress)
882 ovfl := overflow(name)
883 helper := decSliceHelper[t.Elem().Kind()]
884 op = func(i *decInstr, state *decoderState, value reflect.Value) {
885 state.dec.decodeSlice(state, value, *elemOp, ovfl, helper)
886 }
887
888 case reflect.Struct:
889
890 ut := userType(typ)
891 enginePtr, err := dec.getDecEnginePtr(wireId, ut)
892 if err != nil {
893 error_(err)
894 }
895 op = func(i *decInstr, state *decoderState, value reflect.Value) {
896
897 dec.decodeStruct(*enginePtr, value)
898 }
899 case reflect.Interface:
900 op = func(i *decInstr, state *decoderState, value reflect.Value) {
901 state.dec.decodeInterface(t, state, value)
902 }
903 }
904 }
905 if op == nil {
906 errorf("decode can't handle type %s", rt)
907 }
908 return &op
909 }
910
911 var maxIgnoreNestingDepth = 10000
912
913
914 func (dec *Decoder) decIgnoreOpFor(wireId typeId, inProgress map[typeId]*decOp) *decOp {
915
916 dec.ignoreDepth++
917 defer func() { dec.ignoreDepth-- }()
918 if dec.ignoreDepth > maxIgnoreNestingDepth {
919 error_(errors.New("invalid nesting depth"))
920 }
921
922
923 if opPtr := inProgress[wireId]; opPtr != nil {
924 return opPtr
925 }
926 op, ok := decIgnoreOpMap[wireId]
927 if !ok {
928 inProgress[wireId] = &op
929 if wireId == tInterface {
930
931
932 op = func(i *decInstr, state *decoderState, value reflect.Value) {
933 state.dec.ignoreInterface(state)
934 }
935 return &op
936 }
937
938 wire := dec.wireType[wireId]
939 switch {
940 case wire == nil:
941 errorf("bad data: undefined type %s", wireId.string())
942 case wire.ArrayT != nil:
943 elemId := wire.ArrayT.Elem
944 elemOp := dec.decIgnoreOpFor(elemId, inProgress)
945 op = func(i *decInstr, state *decoderState, value reflect.Value) {
946 state.dec.ignoreArray(state, *elemOp, wire.ArrayT.Len)
947 }
948
949 case wire.MapT != nil:
950 keyId := dec.wireType[wireId].MapT.Key
951 elemId := dec.wireType[wireId].MapT.Elem
952 keyOp := dec.decIgnoreOpFor(keyId, inProgress)
953 elemOp := dec.decIgnoreOpFor(elemId, inProgress)
954 op = func(i *decInstr, state *decoderState, value reflect.Value) {
955 state.dec.ignoreMap(state, *keyOp, *elemOp)
956 }
957
958 case wire.SliceT != nil:
959 elemId := wire.SliceT.Elem
960 elemOp := dec.decIgnoreOpFor(elemId, inProgress)
961 op = func(i *decInstr, state *decoderState, value reflect.Value) {
962 state.dec.ignoreSlice(state, *elemOp)
963 }
964
965 case wire.StructT != nil:
966
967 enginePtr, err := dec.getIgnoreEnginePtr(wireId)
968 if err != nil {
969 error_(err)
970 }
971 op = func(i *decInstr, state *decoderState, value reflect.Value) {
972
973 state.dec.ignoreStruct(*enginePtr)
974 }
975
976 case wire.GobEncoderT != nil, wire.BinaryMarshalerT != nil, wire.TextMarshalerT != nil:
977 op = func(i *decInstr, state *decoderState, value reflect.Value) {
978 state.dec.ignoreGobDecoder(state)
979 }
980 }
981 }
982 if op == nil {
983 errorf("bad data: ignore can't handle type %s", wireId.string())
984 }
985 return &op
986 }
987
988
989
990 func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp {
991 rcvrType := ut.user
992 if ut.decIndir == -1 {
993 rcvrType = reflect.PointerTo(rcvrType)
994 } else if ut.decIndir > 0 {
995 for i := int8(0); i < ut.decIndir; i++ {
996 rcvrType = rcvrType.Elem()
997 }
998 }
999 var op decOp
1000 op = func(i *decInstr, state *decoderState, value reflect.Value) {
1001
1002 if value.Kind() != reflect.Pointer && rcvrType.Kind() == reflect.Pointer {
1003 value = value.Addr()
1004 }
1005 state.dec.decodeGobDecoder(ut, state, value)
1006 }
1007 return &op
1008 }
1009
1010
1011
1012
1013
1014 func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool {
1015 if rhs, ok := inProgress[fr]; ok {
1016 return rhs == fw
1017 }
1018 inProgress[fr] = fw
1019 ut := userType(fr)
1020 wire, ok := dec.wireType[fw]
1021
1022
1023
1024
1025
1026
1027 if (ut.externalDec == xGob) != (ok && wire.GobEncoderT != nil) ||
1028 (ut.externalDec == xBinary) != (ok && wire.BinaryMarshalerT != nil) ||
1029 (ut.externalDec == xText) != (ok && wire.TextMarshalerT != nil) {
1030 return false
1031 }
1032 if ut.externalDec != 0 {
1033 return true
1034 }
1035 switch t := ut.base; t.Kind() {
1036 default:
1037
1038 return false
1039 case reflect.Bool:
1040 return fw == tBool
1041 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1042 return fw == tInt
1043 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
1044 return fw == tUint
1045 case reflect.Float32, reflect.Float64:
1046 return fw == tFloat
1047 case reflect.Complex64, reflect.Complex128:
1048 return fw == tComplex
1049 case reflect.String:
1050 return fw == tString
1051 case reflect.Interface:
1052 return fw == tInterface
1053 case reflect.Array:
1054 if !ok || wire.ArrayT == nil {
1055 return false
1056 }
1057 array := wire.ArrayT
1058 return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress)
1059 case reflect.Map:
1060 if !ok || wire.MapT == nil {
1061 return false
1062 }
1063 MapType := wire.MapT
1064 return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress)
1065 case reflect.Slice:
1066
1067 if t.Elem().Kind() == reflect.Uint8 {
1068 return fw == tBytes
1069 }
1070
1071 var sw *sliceType
1072 if tt := builtinIdToType(fw); tt != nil {
1073 sw, _ = tt.(*sliceType)
1074 } else if wire != nil {
1075 sw = wire.SliceT
1076 }
1077 elem := userType(t.Elem()).base
1078 return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress)
1079 case reflect.Struct:
1080 return true
1081 }
1082 }
1083
1084
1085 func (dec *Decoder) typeString(remoteId typeId) string {
1086 typeLock.Lock()
1087 defer typeLock.Unlock()
1088 if t := idToType(remoteId); t != nil {
1089
1090 return t.string()
1091 }
1092 return dec.wireType[remoteId].string()
1093 }
1094
1095
1096
1097 func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
1098 rt := ut.user
1099 engine = new(decEngine)
1100 engine.instr = make([]decInstr, 1)
1101 name := rt.String()
1102 if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) {
1103 remoteType := dec.typeString(remoteId)
1104
1105 if ut.base.Kind() == reflect.Interface && remoteId != tInterface {
1106 return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType)
1107 }
1108 return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType)
1109 }
1110 op := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp))
1111 ovfl := errors.New(`value for "` + name + `" out of range`)
1112 engine.instr[singletonField] = decInstr{*op, singletonField, nil, ovfl}
1113 engine.numInstr = 1
1114 return
1115 }
1116
1117
1118 func (dec *Decoder) compileIgnoreSingle(remoteId typeId) *decEngine {
1119 engine := new(decEngine)
1120 engine.instr = make([]decInstr, 1)
1121 op := dec.decIgnoreOpFor(remoteId, make(map[typeId]*decOp))
1122 ovfl := overflow(dec.typeString(remoteId))
1123 engine.instr[0] = decInstr{*op, 0, nil, ovfl}
1124 engine.numInstr = 1
1125 return engine
1126 }
1127
1128
1129
1130 func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
1131 defer catchError(&err)
1132 rt := ut.base
1133 srt := rt
1134 if srt.Kind() != reflect.Struct || ut.externalDec != 0 {
1135 return dec.compileSingle(remoteId, ut)
1136 }
1137 var wireStruct *structType
1138
1139
1140 if t := builtinIdToType(remoteId); t != nil {
1141 wireStruct, _ = t.(*structType)
1142 } else {
1143 wire := dec.wireType[remoteId]
1144 if wire == nil {
1145 error_(errBadType)
1146 }
1147 wireStruct = wire.StructT
1148 }
1149 if wireStruct == nil {
1150 errorf("type mismatch in decoder: want struct type %s; got non-struct", rt)
1151 }
1152 engine = new(decEngine)
1153 engine.instr = make([]decInstr, len(wireStruct.Field))
1154 seen := make(map[reflect.Type]*decOp)
1155
1156 for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ {
1157 wireField := wireStruct.Field[fieldnum]
1158 if wireField.Name == "" {
1159 errorf("empty name for remote field of type %s", wireStruct.Name)
1160 }
1161 ovfl := overflow(wireField.Name)
1162
1163 localField, present := srt.FieldByName(wireField.Name)
1164
1165 if !present || !isExported(wireField.Name) {
1166 op := dec.decIgnoreOpFor(wireField.Id, make(map[typeId]*decOp))
1167 engine.instr[fieldnum] = decInstr{*op, fieldnum, nil, ovfl}
1168 continue
1169 }
1170 if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) {
1171 errorf("wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name)
1172 }
1173 op := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen)
1174 engine.instr[fieldnum] = decInstr{*op, fieldnum, localField.Index, ovfl}
1175 engine.numInstr++
1176 }
1177 return
1178 }
1179
1180
1181 func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err error) {
1182 rt := ut.user
1183 decoderMap, ok := dec.decoderCache[rt]
1184 if !ok {
1185 decoderMap = make(map[typeId]**decEngine)
1186 dec.decoderCache[rt] = decoderMap
1187 }
1188 if enginePtr, ok = decoderMap[remoteId]; !ok {
1189
1190 enginePtr = new(*decEngine)
1191 decoderMap[remoteId] = enginePtr
1192 *enginePtr, err = dec.compileDec(remoteId, ut)
1193 if err != nil {
1194 delete(decoderMap, remoteId)
1195 }
1196 }
1197 return
1198 }
1199
1200
1201 type emptyStruct struct{}
1202
1203 var emptyStructType = reflect.TypeFor[emptyStruct]()
1204
1205
1206 func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) {
1207 var ok bool
1208 if enginePtr, ok = dec.ignorerCache[wireId]; !ok {
1209
1210 enginePtr = new(*decEngine)
1211 dec.ignorerCache[wireId] = enginePtr
1212 wire := dec.wireType[wireId]
1213 if wire != nil && wire.StructT != nil {
1214 *enginePtr, err = dec.compileDec(wireId, userType(emptyStructType))
1215 } else {
1216 *enginePtr = dec.compileIgnoreSingle(wireId)
1217 }
1218 if err != nil {
1219 delete(dec.ignorerCache, wireId)
1220 }
1221 }
1222 return
1223 }
1224
1225
1226 func (dec *Decoder) decodeValue(wireId typeId, value reflect.Value) {
1227 defer catchError(&dec.err)
1228
1229 if !value.IsValid() {
1230 dec.decodeIgnoredValue(wireId)
1231 return
1232 }
1233
1234 ut := userType(value.Type())
1235 base := ut.base
1236 var enginePtr **decEngine
1237 enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut)
1238 if dec.err != nil {
1239 return
1240 }
1241 value = decAlloc(value)
1242 engine := *enginePtr
1243 if st := base; st.Kind() == reflect.Struct && ut.externalDec == 0 {
1244 wt := dec.wireType[wireId]
1245 if engine.numInstr == 0 && st.NumField() > 0 &&
1246 wt != nil && len(wt.StructT.Field) > 0 {
1247 name := base.Name()
1248 errorf("type mismatch: no fields matched compiling decoder for %s", name)
1249 }
1250 dec.decodeStruct(engine, value)
1251 } else {
1252 dec.decodeSingle(engine, value)
1253 }
1254 }
1255
1256
1257 func (dec *Decoder) decodeIgnoredValue(wireId typeId) {
1258 var enginePtr **decEngine
1259 enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId)
1260 if dec.err != nil {
1261 return
1262 }
1263 wire := dec.wireType[wireId]
1264 if wire != nil && wire.StructT != nil {
1265 dec.ignoreStruct(*enginePtr)
1266 } else {
1267 dec.ignoreSingle(*enginePtr)
1268 }
1269 }
1270
1271 const (
1272 intBits = 32 << (^uint(0) >> 63)
1273 uintptrBits = 32 << (^uintptr(0) >> 63)
1274 )
1275
1276 func init() {
1277 var iop, uop decOp
1278 switch intBits {
1279 case 32:
1280 iop = decInt32
1281 uop = decUint32
1282 case 64:
1283 iop = decInt64
1284 uop = decUint64
1285 default:
1286 panic("gob: unknown size of int/uint")
1287 }
1288 decOpTable[reflect.Int] = iop
1289 decOpTable[reflect.Uint] = uop
1290
1291
1292 switch uintptrBits {
1293 case 32:
1294 uop = decUint32
1295 case 64:
1296 uop = decUint64
1297 default:
1298 panic("gob: unknown size of uintptr")
1299 }
1300 decOpTable[reflect.Uintptr] = uop
1301 }
1302
1303
1304
1305
1306
1307 func allocValue(t reflect.Type) reflect.Value {
1308 return reflect.New(t).Elem()
1309 }
1310
View as plain text