1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 //go:generate go run decgen.go -output dec_helpers.go
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")
25 type decHelper func(state *decoderState, v reflect.Value, length int, ovfl error) bool
27 // decoderState is the execution state of an instance of the decoder. A new state
28 // is created for nested objects.
29 type decoderState struct {
31 // The buffer is stored with an extra indirection because it may be replaced
32 // if we load a type during decode (when reading an interface value).
34 fieldnum int // the last field number read.
35 next *decoderState // for free list
38 // decBuffer is an extremely simple, fast implementation of a read-only byte buffer.
39 // It is initialized by calling Size and then copying the data into the slice returned by Bytes().
40 type decBuffer struct {
42 offset int // Read offset.
45 func (d *decBuffer) Read(p []byte) (int, error) {
46 n := copy(p, d.data[d.offset:])
47 if n == 0 && len(p) != 0 {
54 func (d *decBuffer) Drop(n int) {
61 func (d *decBuffer) ReadByte() (byte, error) {
62 if d.offset >= len(d.data) {
70 func (d *decBuffer) Len() int {
71 return len(d.data) - d.offset
74 func (d *decBuffer) Bytes() []byte {
75 return d.data[d.offset:]
78 // SetBytes sets the buffer to the bytes, discarding any existing data.
79 func (d *decBuffer) SetBytes(data []byte) {
84 func (d *decBuffer) Reset() {
89 // We pass the bytes.Buffer separately for easier testing of the infrastructure
90 // without requiring a full Decoder.
91 func (dec *Decoder) newDecoderState(buf *decBuffer) *decoderState {
103 func (dec *Decoder) freeDecoderState(d *decoderState) {
104 d.next = dec.freeList
108 func overflow(name string) error {
109 return errors.New(`value for "` + name + `" out of range`)
112 // decodeUintReader reads an encoded unsigned integer from an io.Reader.
113 // Used only by the Decoder to read the message length.
114 func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err error) {
116 n, err := io.ReadFull(r, buf[0:width])
122 return uint64(b), width, nil
129 width, err = io.ReadFull(r, buf[0:n])
132 err = io.ErrUnexpectedEOF
136 // Could check that the high byte is zero but it's not worth it.
137 for _, b := range buf[0:width] {
140 width++ // +1 for length byte
144 // decodeUint reads an encoded unsigned integer from state.r.
145 // Does not check for overflow.
146 func (state *decoderState) decodeUint() (x uint64) {
147 b, err := state.b.ReadByte()
158 buf := state.b.Bytes()
160 errorf("invalid uint data length %d: exceeds input size %d", n, len(buf))
162 // Don't need to check error; it's safe to loop regardless.
163 // Could check that the high byte is zero but it's not worth it.
164 for _, b := range buf[0:n] {
171 // decodeInt reads an encoded signed integer from state.r.
172 // Does not check for overflow.
173 func (state *decoderState) decodeInt() int64 {
174 x := state.decodeUint()
176 return ^int64(x >> 1)
181 // getLength decodes the next uint and makes sure it is a possible
182 // size for a data item that follows, which means it must fit in a
183 // non-negative int and fit in the buffer.
184 func (state *decoderState) getLength() (int, bool) {
185 n := int(state.decodeUint())
186 if n < 0 || state.b.Len() < n || tooBig <= n {
192 // decOp is the signature of a decoding operator for a given type.
193 type decOp func(i *decInstr, state *decoderState, v reflect.Value)
195 // The 'instructions' of the decoding machine
196 type decInstr struct {
198 field int // field number of the wire type
199 index []int // field access indices for destination type
200 ovfl error // error message for overflow/underflow (for arrays, of the elements)
203 // ignoreUint discards a uint value with no destination.
204 func ignoreUint(i *decInstr, state *decoderState, v reflect.Value) {
208 // ignoreTwoUints discards a uint value with no destination. It's used to skip
210 func ignoreTwoUints(i *decInstr, state *decoderState, v reflect.Value) {
215 // Since the encoder writes no zeros, if we arrive at a decoder we have
216 // a value to extract and store. The field number has already been read
217 // (it's how we knew to call this decoder).
218 // Each decoder is responsible for handling any indirections associated
219 // with the data structure. If any pointer so reached is nil, allocation must
222 // decAlloc takes a value and returns a settable value that can
223 // be assigned to. If the value is a pointer, decAlloc guarantees it points to storage.
224 // The callers to the individual decoders are expected to have used decAlloc.
225 // The individual decoders don't need to it.
226 func decAlloc(v reflect.Value) reflect.Value {
227 for v.Kind() == reflect.Pointer {
229 v.Set(reflect.New(v.Type().Elem()))
236 // decBool decodes a uint and stores it as a boolean in value.
237 func decBool(i *decInstr, state *decoderState, value reflect.Value) {
238 value.SetBool(state.decodeUint() != 0)
241 // decInt8 decodes an integer and stores it as an int8 in value.
242 func decInt8(i *decInstr, state *decoderState, value reflect.Value) {
243 v := state.decodeInt()
244 if v < math.MinInt8 || math.MaxInt8 < v {
250 // decUint8 decodes an unsigned integer and stores it as a uint8 in value.
251 func decUint8(i *decInstr, state *decoderState, value reflect.Value) {
252 v := state.decodeUint()
253 if math.MaxUint8 < v {
259 // decInt16 decodes an integer and stores it as an int16 in value.
260 func decInt16(i *decInstr, state *decoderState, value reflect.Value) {
261 v := state.decodeInt()
262 if v < math.MinInt16 || math.MaxInt16 < v {
268 // decUint16 decodes an unsigned integer and stores it as a uint16 in value.
269 func decUint16(i *decInstr, state *decoderState, value reflect.Value) {
270 v := state.decodeUint()
271 if math.MaxUint16 < v {
277 // decInt32 decodes an integer and stores it as an int32 in value.
278 func decInt32(i *decInstr, state *decoderState, value reflect.Value) {
279 v := state.decodeInt()
280 if v < math.MinInt32 || math.MaxInt32 < v {
286 // decUint32 decodes an unsigned integer and stores it as a uint32 in value.
287 func decUint32(i *decInstr, state *decoderState, value reflect.Value) {
288 v := state.decodeUint()
289 if math.MaxUint32 < v {
295 // decInt64 decodes an integer and stores it as an int64 in value.
296 func decInt64(i *decInstr, state *decoderState, value reflect.Value) {
297 v := state.decodeInt()
301 // decUint64 decodes an unsigned integer and stores it as a uint64 in value.
302 func decUint64(i *decInstr, state *decoderState, value reflect.Value) {
303 v := state.decodeUint()
307 // Floating-point numbers are transmitted as uint64s holding the bits
308 // of the underlying representation. They are sent byte-reversed, with
309 // the exponent end coming out first, so integer floating point numbers
310 // (for example) transmit more compactly. This routine does the
312 func float64FromBits(u uint64) float64 {
313 v := bits.ReverseBytes64(u)
314 return math.Float64frombits(v)
317 // float32FromBits decodes an unsigned integer, treats it as a 32-bit floating-point
318 // number, and returns it. It's a helper function for float32 and complex64.
319 // It returns a float64 because that's what reflection needs, but its return
320 // value is known to be accurately representable in a float32.
321 func float32FromBits(u uint64, ovfl error) float64 {
322 v := float64FromBits(u)
327 // +Inf is OK in both 32- and 64-bit floats. Underflow is always OK.
328 if math.MaxFloat32 < av && av <= math.MaxFloat64 {
334 // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
335 // number, and stores it in value.
336 func decFloat32(i *decInstr, state *decoderState, value reflect.Value) {
337 value.SetFloat(float32FromBits(state.decodeUint(), i.ovfl))
340 // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point
341 // number, and stores it in value.
342 func decFloat64(i *decInstr, state *decoderState, value reflect.Value) {
343 value.SetFloat(float64FromBits(state.decodeUint()))
346 // decComplex64 decodes a pair of unsigned integers, treats them as a
347 // pair of floating point numbers, and stores them as a complex64 in value.
348 // The real part comes first.
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))
355 // decComplex128 decodes a pair of unsigned integers, treats them as a
356 // pair of floating point numbers, and stores them as a complex128 in value.
357 // The real part comes first.
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))
364 // decUint8Slice decodes a byte slice and stores in value a slice header
365 // describing the data.
366 // uint8 slices are encoded as an unsigned count followed by the raw bytes.
367 func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) {
368 n, ok := state.getLength()
370 errorf("bad %s slice length: %d", value.Type(), n)
373 safe := saferio.SliceCap((*byte)(nil), uint64(n))
375 errorf("%s slice too big: %d elements", value.Type(), n)
377 value.Set(reflect.MakeSlice(value.Type(), safe, safe))
382 // We didn't allocate the entire slice,
383 // due to using saferio.SliceCap.
384 // Grow the slice for one more element.
385 // The slice is full, so this should
386 // bump up the capacity.
389 // Copy into s up to the capacity or n,
390 // whichever is less.
396 sub := value.Slice(i, ln)
397 if _, err := state.b.Read(sub.Bytes()); err != nil {
398 errorf("error decoding []byte at %d: %s", err, i)
404 if _, err := state.b.Read(value.Bytes()); err != nil {
405 errorf("error decoding []byte: %s", err)
410 // decString decodes byte array and stores in value a string header
411 // describing the data.
412 // Strings are encoded as an unsigned count followed by the raw bytes.
413 func decString(i *decInstr, state *decoderState, value reflect.Value) {
414 n, ok := state.getLength()
416 errorf("bad %s slice length: %d", value.Type(), n)
419 data := state.b.Bytes()
421 errorf("invalid string length %d: exceeds input size %d", n, len(data))
423 s := string(data[:n])
428 // ignoreUint8Array skips over the data for a byte slice value with no destination.
429 func ignoreUint8Array(i *decInstr, state *decoderState, value reflect.Value) {
430 n, ok := state.getLength()
432 errorf("slice length too large")
436 errorf("invalid slice length %d: exceeds input size %d", n, bn)
443 // The encoder engine is an array of instructions indexed by field number of the incoming
444 // decoder. It is executed with random access according to field number.
445 type decEngine struct {
447 numInstr int // the number of active instructions
450 // decodeSingle decodes a top-level value that is not a struct and stores it in value.
451 // Such values are preceded by a zero, making them have the memory layout of a
452 // struct field (although with an illegal field number).
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")
460 instr := &engine.instr[singletonField]
461 instr.op(instr, state, value)
464 // decodeStruct decodes a top-level struct and stores it in value.
465 // Indir is for the value, not the type. At the time of the call it may
466 // differ from ut.indir, which was computed when the engine was built.
467 // This state cannot arise for decodeSingle, which is called directly
468 // from the user's value, not from the innards of an engine.
469 func (dec *Decoder) decodeStruct(engine *decEngine, value reflect.Value) {
470 state := dec.newDecoderState(&dec.buf)
471 defer dec.freeDecoderState(state)
473 for state.b.Len() > 0 {
474 delta := int(state.decodeUint())
476 errorf("decode: corrupted data: negative delta")
478 if delta == 0 { // struct terminator is zero delta fieldnum
481 if state.fieldnum >= len(engine.instr)-delta { // subtract to compare without overflow
484 fieldnum := state.fieldnum + delta
485 instr := &engine.instr[fieldnum]
486 var field reflect.Value
487 if instr.index != nil {
488 // Otherwise the field is unknown to us and instr.op is an ignore op.
489 field = value.FieldByIndex(instr.index)
490 if field.Kind() == reflect.Pointer {
491 field = decAlloc(field)
494 instr.op(instr, state, field)
495 state.fieldnum = fieldnum
499 var noValue reflect.Value
501 // ignoreStruct discards the data for a struct with no destination.
502 func (dec *Decoder) ignoreStruct(engine *decEngine) {
503 state := dec.newDecoderState(&dec.buf)
504 defer dec.freeDecoderState(state)
506 for state.b.Len() > 0 {
507 delta := int(state.decodeUint())
509 errorf("ignore decode: corrupted data: negative delta")
511 if delta == 0 { // struct terminator is zero delta fieldnum
514 fieldnum := state.fieldnum + delta
515 if fieldnum >= len(engine.instr) {
518 instr := &engine.instr[fieldnum]
519 instr.op(instr, state, noValue)
520 state.fieldnum = fieldnum
524 // ignoreSingle discards the data for a top-level non-struct value with no
525 // destination. It's used when calling Decode with a nil value.
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())
532 errorf("decode: corrupted data: non-zero delta for singleton")
534 instr := &engine.instr[singletonField]
535 instr.op(instr, state, noValue)
538 // decodeArrayHelper does the work for decoding arrays and slices.
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) {
543 instr := &decInstr{elemOp, 0, nil, ovfl}
544 isPtr := value.Type().Elem().Kind() == reflect.Pointer
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)
551 // This is a slice that we only partially allocated.
552 // Grow it up to length.
565 elemOp(instr, state, v)
569 // decodeArray decodes an array and stores it in value.
570 // The length is an unsigned integer preceding the elements. Even though the length is redundant
571 // (it's part of the type), it's a useful check and is included in the encoding.
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")
576 dec.decodeArrayHelper(state, value, elemOp, length, ovfl, helper)
579 // decodeIntoValue is a helper for map decoding.
580 func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Value, instr *decInstr) reflect.Value {
590 // decodeMap decodes a map and stores it in value.
591 // Maps are encoded as a length followed by key:value pairs.
592 // Because the internals of maps are not visible to us, we must
593 // use reflection rather than pointer magic.
594 func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value reflect.Value, keyOp, elemOp decOp, ovfl error) {
595 n := int(state.decodeUint())
597 value.Set(reflect.MakeMapWithSize(mtyp, n))
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()
614 // ignoreArrayHelper does the work for discarding arrays and slices.
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)
621 elemOp(instr, state, noValue)
625 // ignoreArray discards the data for an array value with no destination.
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")
630 dec.ignoreArrayHelper(state, elemOp, length)
633 // ignoreMap discards the data for a map value with no destination.
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)
644 // decodeSlice decodes a slice and stores it in value.
645 // Slices are encoded as an unsigned length followed by the elements.
646 func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp decOp, ovfl error, helper decHelper) {
647 u := state.decodeUint()
649 size := uint64(typ.Elem().Size())
652 // Take care with overflow in this calculation.
653 if n < 0 || uint64(n) != u || nBytes > tooBig || (size > 0 && nBytes/size != u) {
654 // We don't check n against buffer length here because if it's a slice
655 // of interfaces, there will be buffer reloads.
656 errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size)
659 safe := saferio.SliceCap(reflect.Zero(reflect.PointerTo(typ.Elem())).Interface(), uint64(n))
661 errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size)
663 value.Set(reflect.MakeSlice(typ, safe, safe))
667 dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper)
670 // ignoreSlice skips over the data for a slice value with no destination.
671 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) {
672 dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint()))
675 // decodeInterface decodes an interface value and stores it in value.
676 // Interfaces are encoded as the name of a concrete type followed by a value.
677 // If the name is empty, the value is nil and no value is sent.
678 func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, value reflect.Value) {
679 // Read the name of the concrete type.
680 nr := state.decodeUint()
681 if nr > 1<<31 { // zero is permissible for anonymous types
682 errorf("invalid type name length %d", nr)
684 if nr > uint64(state.b.Len()) {
685 errorf("invalid type name length %d: exceeds input size", nr)
688 name := state.b.Bytes()[:n]
690 // Allocate the destination interface value.
692 // Copy the nil interface value to the target.
696 if len(name) > 1024 {
697 errorf("name too long (%d bytes): %.20q...", len(name), name)
699 // The concrete type must be registered.
700 typi, ok := nameToConcreteType.Load(string(name))
702 errorf("name not registered for interface: %q", name)
704 typ := typi.(reflect.Type)
706 // Read the type id of the concrete value.
707 concreteId := dec.decodeTypeSequence(true)
711 // Byte count of value is next; we don't care what it is (it's there
712 // in case we want to ignore the value by skipping it completely).
714 // Read the concrete value.
716 dec.decodeValue(concreteId, v)
720 // Assign the concrete value to the interface.
721 // Tread carefully; it might not satisfy the interface.
722 if !typ.AssignableTo(ityp) {
723 errorf("%s is not assignable to type %s", typ, ityp)
725 // Copy the interface value to the target.
729 // ignoreInterface discards the data for an interface value with no destination.
730 func (dec *Decoder) ignoreInterface(state *decoderState) {
731 // Read the name of the concrete type.
732 n, ok := state.getLength()
734 errorf("bad interface encoding: name too large for buffer")
738 errorf("invalid interface value length %d: exceeds input size %d", n, bn)
741 id := dec.decodeTypeSequence(true)
745 // At this point, the decoder buffer contains a delimited value. Just toss it.
746 n, ok = state.getLength()
748 errorf("bad interface encoding: data length too large for buffer")
753 // decodeGobDecoder decodes something implementing the GobDecoder interface.
754 // The data is encoded as a byte slice.
755 func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, value reflect.Value) {
756 // Read the bytes for the value.
757 n, ok := state.getLength()
759 errorf("GobDecoder: length too large for buffer")
763 errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, len(b))
768 // We know it's one of these.
769 switch ut.externalDec {
771 err = value.Interface().(GobDecoder).GobDecode(b)
773 err = value.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b)
775 err = value.Interface().(encoding.TextUnmarshaler).UnmarshalText(b)
782 // ignoreGobDecoder discards the data for a GobDecoder value with no destination.
783 func (dec *Decoder) ignoreGobDecoder(state *decoderState) {
784 // Read the bytes for the value.
785 n, ok := state.getLength()
787 errorf("GobDecoder: length too large for buffer")
791 errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, bn)
796 // Index by Go types.
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,
814 // Indexed by gob types. tComplex will be added during type.init().
815 var decIgnoreOpMap = map[typeId]decOp{
820 tBytes: ignoreUint8Array,
821 tString: ignoreUint8Array,
822 tComplex: ignoreTwoUints,
825 // decOpFor returns the decoding op for the base type under rt and
826 // the indirection count to reach it.
827 func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) *decOp {
829 // If the type implements GobEncoder, we handle it without further processing.
830 if ut.externalDec != 0 {
831 return dec.gobDecodeOpFor(ut)
834 // If this type is already in progress, it's a recursive type (e.g. map[string]*T).
835 // Return the pointer to the op we're already building.
836 if opPtr := inProgress[rt]; opPtr != nil {
842 if int(k) < len(decOpTable) {
848 switch t := typ; t.Kind() {
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)
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)
870 name = "element of " + name
871 if t.Elem().Kind() == reflect.Uint8 {
876 if tt := builtinIdToType(wireId); tt != nil {
877 elemId = tt.(*sliceType).Elem
879 elemId = dec.wireType[wireId].SliceT.Elem
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)
889 // Generate a closure that calls out to the engine for the nested type.
891 enginePtr, err := dec.getDecEnginePtr(wireId, ut)
895 op = func(i *decInstr, state *decoderState, value reflect.Value) {
896 // indirect through enginePtr to delay evaluation for recursive structs.
897 dec.decodeStruct(*enginePtr, value)
899 case reflect.Interface:
900 op = func(i *decInstr, state *decoderState, value reflect.Value) {
901 state.dec.decodeInterface(t, state, value)
906 errorf("decode can't handle type %s", rt)
911 var maxIgnoreNestingDepth = 10000
913 // decIgnoreOpFor returns the decoding op for a field that has no destination.
914 func (dec *Decoder) decIgnoreOpFor(wireId typeId, inProgress map[typeId]*decOp, depth int) *decOp {
915 if depth > maxIgnoreNestingDepth {
916 error_(errors.New("invalid nesting depth"))
918 // If this type is already in progress, it's a recursive type (e.g. map[string]*T).
919 // Return the pointer to the op we're already building.
920 if opPtr := inProgress[wireId]; opPtr != nil {
923 op, ok := decIgnoreOpMap[wireId]
925 inProgress[wireId] = &op
926 if wireId == tInterface {
927 // Special case because it's a method: the ignored item might
928 // define types and we need to record their state in the decoder.
929 op = func(i *decInstr, state *decoderState, value reflect.Value) {
930 state.dec.ignoreInterface(state)
935 wire := dec.wireType[wireId]
938 errorf("bad data: undefined type %s", wireId.string())
939 case wire.ArrayT != nil:
940 elemId := wire.ArrayT.Elem
941 elemOp := dec.decIgnoreOpFor(elemId, inProgress, depth+1)
942 op = func(i *decInstr, state *decoderState, value reflect.Value) {
943 state.dec.ignoreArray(state, *elemOp, wire.ArrayT.Len)
946 case wire.MapT != nil:
947 keyId := dec.wireType[wireId].MapT.Key
948 elemId := dec.wireType[wireId].MapT.Elem
949 keyOp := dec.decIgnoreOpFor(keyId, inProgress, depth+1)
950 elemOp := dec.decIgnoreOpFor(elemId, inProgress, depth+1)
951 op = func(i *decInstr, state *decoderState, value reflect.Value) {
952 state.dec.ignoreMap(state, *keyOp, *elemOp)
955 case wire.SliceT != nil:
956 elemId := wire.SliceT.Elem
957 elemOp := dec.decIgnoreOpFor(elemId, inProgress, depth+1)
958 op = func(i *decInstr, state *decoderState, value reflect.Value) {
959 state.dec.ignoreSlice(state, *elemOp)
962 case wire.StructT != nil:
963 // Generate a closure that calls out to the engine for the nested type.
964 enginePtr, err := dec.getIgnoreEnginePtr(wireId)
968 op = func(i *decInstr, state *decoderState, value reflect.Value) {
969 // indirect through enginePtr to delay evaluation for recursive structs
970 state.dec.ignoreStruct(*enginePtr)
973 case wire.GobEncoderT != nil, wire.BinaryMarshalerT != nil, wire.TextMarshalerT != nil:
974 op = func(i *decInstr, state *decoderState, value reflect.Value) {
975 state.dec.ignoreGobDecoder(state)
980 errorf("bad data: ignore can't handle type %s", wireId.string())
985 // gobDecodeOpFor returns the op for a type that is known to implement
987 func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp {
989 if ut.decIndir == -1 {
990 rcvrType = reflect.PointerTo(rcvrType)
991 } else if ut.decIndir > 0 {
992 for i := int8(0); i < ut.decIndir; i++ {
993 rcvrType = rcvrType.Elem()
997 op = func(i *decInstr, state *decoderState, value reflect.Value) {
998 // We now have the base type. We need its address if the receiver is a pointer.
999 if value.Kind() != reflect.Pointer && rcvrType.Kind() == reflect.Pointer {
1000 value = value.Addr()
1002 state.dec.decodeGobDecoder(ut, state, value)
1007 // compatibleType asks: Are these two gob Types compatible?
1008 // Answers the question for basic types, arrays, maps and slices, plus
1009 // GobEncoder/Decoder pairs.
1010 // Structs are considered ok; fields will be checked later.
1011 func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool {
1012 if rhs, ok := inProgress[fr]; ok {
1017 wire, ok := dec.wireType[fw]
1018 // If wire was encoded with an encoding method, fr must have that method.
1019 // And if not, it must not.
1020 // At most one of the booleans in ut is set.
1021 // We could possibly relax this constraint in the future in order to
1022 // choose the decoding method using the data in the wireType.
1023 // The parentheses look odd but are correct.
1024 if (ut.externalDec == xGob) != (ok && wire.GobEncoderT != nil) ||
1025 (ut.externalDec == xBinary) != (ok && wire.BinaryMarshalerT != nil) ||
1026 (ut.externalDec == xText) != (ok && wire.TextMarshalerT != nil) {
1029 if ut.externalDec != 0 { // This test trumps all others.
1032 switch t := ut.base; t.Kind() {
1034 // chan, etc: cannot handle.
1038 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1040 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
1042 case reflect.Float32, reflect.Float64:
1044 case reflect.Complex64, reflect.Complex128:
1045 return fw == tComplex
1046 case reflect.String:
1047 return fw == tString
1048 case reflect.Interface:
1049 return fw == tInterface
1051 if !ok || wire.ArrayT == nil {
1054 array := wire.ArrayT
1055 return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress)
1057 if !ok || wire.MapT == nil {
1060 MapType := wire.MapT
1061 return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress)
1063 // Is it an array of bytes?
1064 if t.Elem().Kind() == reflect.Uint8 {
1067 // Extract and compare element types.
1069 if tt := builtinIdToType(fw); tt != nil {
1070 sw, _ = tt.(*sliceType)
1071 } else if wire != nil {
1074 elem := userType(t.Elem()).base
1075 return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress)
1076 case reflect.Struct:
1081 // typeString returns a human-readable description of the type identified by remoteId.
1082 func (dec *Decoder) typeString(remoteId typeId) string {
1084 defer typeLock.Unlock()
1085 if t := idToType[remoteId]; t != nil {
1086 // globally known type.
1089 return dec.wireType[remoteId].string()
1092 // compileSingle compiles the decoder engine for a non-struct top-level value, including
1094 func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
1096 engine = new(decEngine)
1097 engine.instr = make([]decInstr, 1) // one item
1098 name := rt.String() // best we can do
1099 if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) {
1100 remoteType := dec.typeString(remoteId)
1101 // Common confusing case: local interface type, remote concrete type.
1102 if ut.base.Kind() == reflect.Interface && remoteId != tInterface {
1103 return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType)
1105 return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType)
1107 op := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp))
1108 ovfl := errors.New(`value for "` + name + `" out of range`)
1109 engine.instr[singletonField] = decInstr{*op, singletonField, nil, ovfl}
1114 // compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
1115 func (dec *Decoder) compileIgnoreSingle(remoteId typeId) *decEngine {
1116 engine := new(decEngine)
1117 engine.instr = make([]decInstr, 1) // one item
1118 op := dec.decIgnoreOpFor(remoteId, make(map[typeId]*decOp), 0)
1119 ovfl := overflow(dec.typeString(remoteId))
1120 engine.instr[0] = decInstr{*op, 0, nil, ovfl}
1125 // compileDec compiles the decoder engine for a value. If the value is not a struct,
1126 // it calls out to compileSingle.
1127 func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
1128 defer catchError(&err)
1131 if srt.Kind() != reflect.Struct || ut.externalDec != 0 {
1132 return dec.compileSingle(remoteId, ut)
1134 var wireStruct *structType
1135 // Builtin types can come from global pool; the rest must be defined by the decoder.
1136 // Also we know we're decoding a struct now, so the client must have sent one.
1137 if t := builtinIdToType(remoteId); t != nil {
1138 wireStruct, _ = t.(*structType)
1140 wire := dec.wireType[remoteId]
1144 wireStruct = wire.StructT
1146 if wireStruct == nil {
1147 errorf("type mismatch in decoder: want struct type %s; got non-struct", rt)
1149 engine = new(decEngine)
1150 engine.instr = make([]decInstr, len(wireStruct.Field))
1151 seen := make(map[reflect.Type]*decOp)
1152 // Loop over the fields of the wire type.
1153 for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ {
1154 wireField := wireStruct.Field[fieldnum]
1155 if wireField.Name == "" {
1156 errorf("empty name for remote field of type %s", wireStruct.Name)
1158 ovfl := overflow(wireField.Name)
1159 // Find the field of the local type with the same name.
1160 localField, present := srt.FieldByName(wireField.Name)
1161 // TODO(r): anonymous names
1162 if !present || !isExported(wireField.Name) {
1163 op := dec.decIgnoreOpFor(wireField.Id, make(map[typeId]*decOp), 0)
1164 engine.instr[fieldnum] = decInstr{*op, fieldnum, nil, ovfl}
1167 if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) {
1168 errorf("wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name)
1170 op := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen)
1171 engine.instr[fieldnum] = decInstr{*op, fieldnum, localField.Index, ovfl}
1177 // getDecEnginePtr returns the engine for the specified type.
1178 func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err error) {
1180 decoderMap, ok := dec.decoderCache[rt]
1182 decoderMap = make(map[typeId]**decEngine)
1183 dec.decoderCache[rt] = decoderMap
1185 if enginePtr, ok = decoderMap[remoteId]; !ok {
1186 // To handle recursive types, mark this engine as underway before compiling.
1187 enginePtr = new(*decEngine)
1188 decoderMap[remoteId] = enginePtr
1189 *enginePtr, err = dec.compileDec(remoteId, ut)
1191 delete(decoderMap, remoteId)
1197 // emptyStruct is the type we compile into when ignoring a struct value.
1198 type emptyStruct struct{}
1200 var emptyStructType = reflect.TypeOf((*emptyStruct)(nil)).Elem()
1202 // getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded.
1203 func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) {
1205 if enginePtr, ok = dec.ignorerCache[wireId]; !ok {
1206 // To handle recursive types, mark this engine as underway before compiling.
1207 enginePtr = new(*decEngine)
1208 dec.ignorerCache[wireId] = enginePtr
1209 wire := dec.wireType[wireId]
1210 if wire != nil && wire.StructT != nil {
1211 *enginePtr, err = dec.compileDec(wireId, userType(emptyStructType))
1213 *enginePtr = dec.compileIgnoreSingle(wireId)
1216 delete(dec.ignorerCache, wireId)
1222 // decodeValue decodes the data stream representing a value and stores it in value.
1223 func (dec *Decoder) decodeValue(wireId typeId, value reflect.Value) {
1224 defer catchError(&dec.err)
1225 // If the value is nil, it means we should just ignore this item.
1226 if !value.IsValid() {
1227 dec.decodeIgnoredValue(wireId)
1230 // Dereference down to the underlying type.
1231 ut := userType(value.Type())
1233 var enginePtr **decEngine
1234 enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut)
1238 value = decAlloc(value)
1239 engine := *enginePtr
1240 if st := base; st.Kind() == reflect.Struct && ut.externalDec == 0 {
1241 wt := dec.wireType[wireId]
1242 if engine.numInstr == 0 && st.NumField() > 0 &&
1243 wt != nil && len(wt.StructT.Field) > 0 {
1245 errorf("type mismatch: no fields matched compiling decoder for %s", name)
1247 dec.decodeStruct(engine, value)
1249 dec.decodeSingle(engine, value)
1253 // decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it.
1254 func (dec *Decoder) decodeIgnoredValue(wireId typeId) {
1255 var enginePtr **decEngine
1256 enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId)
1260 wire := dec.wireType[wireId]
1261 if wire != nil && wire.StructT != nil {
1262 dec.ignoreStruct(*enginePtr)
1264 dec.ignoreSingle(*enginePtr)
1269 intBits = 32 << (^uint(0) >> 63)
1270 uintptrBits = 32 << (^uintptr(0) >> 63)
1283 panic("gob: unknown size of int/uint")
1285 decOpTable[reflect.Int] = iop
1286 decOpTable[reflect.Uint] = uop
1289 switch uintptrBits {
1295 panic("gob: unknown size of uintptr")
1297 decOpTable[reflect.Uintptr] = uop
1300 // Gob depends on being able to take the address
1301 // of zeroed Values it creates, so use this wrapper instead
1302 // of the standard reflect.Zero.
1303 // Each call allocates once.
1304 func allocValue(t reflect.Type) reflect.Value {
1305 return reflect.New(t).Elem()