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