// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:generate go run decgen.go -output dec_helpers.go

package gob

import (
	
	
	
	
	
	
	
)

var (
	errBadUint = errors.New("gob: encoded unsigned integer out of range")
	errBadType = errors.New("gob: unknown type id or corrupted data")
	errRange   = errors.New("gob: bad data: field numbers out of bounds")
)

type decHelper func(state *decoderState, v reflect.Value, length int, ovfl error) bool

// decoderState is the execution state of an instance of the decoder. A new state
// is created for nested objects.
type decoderState struct {
	dec *Decoder
	// The buffer is stored with an extra indirection because it may be replaced
	// if we load a type during decode (when reading an interface value).
	b        *decBuffer
	fieldnum int           // the last field number read.
	next     *decoderState // for free list
}

// decBuffer is an extremely simple, fast implementation of a read-only byte buffer.
// It is initialized by calling Size and then copying the data into the slice returned by Bytes().
type decBuffer struct {
	data   []byte
	offset int // Read offset.
}

func ( *decBuffer) ( []byte) (int, error) {
	 := copy(, .data[.offset:])
	if  == 0 && len() != 0 {
		return 0, io.EOF
	}
	.offset += 
	return , nil
}

func ( *decBuffer) ( int) {
	if  > .Len() {
		panic("drop")
	}
	.offset += 
}

func ( *decBuffer) () (byte, error) {
	if .offset >= len(.data) {
		return 0, io.EOF
	}
	 := .data[.offset]
	.offset++
	return , nil
}

func ( *decBuffer) () int {
	return len(.data) - .offset
}

func ( *decBuffer) () []byte {
	return .data[.offset:]
}

// SetBytes sets the buffer to the bytes, discarding any existing data.
func ( *decBuffer) ( []byte) {
	.data = 
	.offset = 0
}

func ( *decBuffer) () {
	.data = .data[0:0]
	.offset = 0
}

// We pass the bytes.Buffer separately for easier testing of the infrastructure
// without requiring a full Decoder.
func ( *Decoder) ( *decBuffer) *decoderState {
	 := .freeList
	if  == nil {
		 = new(decoderState)
		.dec = 
	} else {
		.freeList = .next
	}
	.b = 
	return 
}

func ( *Decoder) ( *decoderState) {
	.next = .freeList
	.freeList = 
}

func overflow( string) error {
	return errors.New(`value for "` +  + `" out of range`)
}

// decodeUintReader reads an encoded unsigned integer from an io.Reader.
// Used only by the Decoder to read the message length.
func decodeUintReader( io.Reader,  []byte) ( uint64,  int,  error) {
	 = 1
	,  := io.ReadFull(, [0:])
	if  == 0 {
		return
	}
	 := [0]
	if  <= 0x7f {
		return uint64(), , nil
	}
	 = -int(int8())
	if  > uint64Size {
		 = errBadUint
		return
	}
	,  = io.ReadFull(, [0:])
	if  != nil {
		if  == io.EOF {
			 = io.ErrUnexpectedEOF
		}
		return
	}
	// Could check that the high byte is zero but it's not worth it.
	for ,  := range [0:] {
		 = <<8 | uint64()
	}
	++ // +1 for length byte
	return
}

// decodeUint reads an encoded unsigned integer from state.r.
// Does not check for overflow.
func ( *decoderState) () ( uint64) {
	,  := .b.ReadByte()
	if  != nil {
		error_()
	}
	if  <= 0x7f {
		return uint64()
	}
	 := -int(int8())
	if  > uint64Size {
		error_(errBadUint)
	}
	 := .b.Bytes()
	if len() <  {
		errorf("invalid uint data length %d: exceeds input size %d", , len())
	}
	// Don't need to check error; it's safe to loop regardless.
	// Could check that the high byte is zero but it's not worth it.
	for ,  := range [0:] {
		 = <<8 | uint64()
	}
	.b.Drop()
	return 
}

// decodeInt reads an encoded signed integer from state.r.
// Does not check for overflow.
func ( *decoderState) () int64 {
	 := .decodeUint()
	if &1 != 0 {
		return ^int64( >> 1)
	}
	return int64( >> 1)
}

// getLength decodes the next uint and makes sure it is a possible
// size for a data item that follows, which means it must fit in a
// non-negative int and fit in the buffer.
func ( *decoderState) () (int, bool) {
	 := int(.decodeUint())
	if  < 0 || .b.Len() <  || tooBig <=  {
		return 0, false
	}
	return , true
}

// decOp is the signature of a decoding operator for a given type.
type decOp func(i *decInstr, state *decoderState, v reflect.Value)

// The 'instructions' of the decoding machine
type decInstr struct {
	op    decOp
	field int   // field number of the wire type
	index []int // field access indices for destination type
	ovfl  error // error message for overflow/underflow (for arrays, of the elements)
}

// ignoreUint discards a uint value with no destination.
func ignoreUint( *decInstr,  *decoderState,  reflect.Value) {
	.decodeUint()
}

// ignoreTwoUints discards a uint value with no destination. It's used to skip
// complex values.
func ignoreTwoUints( *decInstr,  *decoderState,  reflect.Value) {
	.decodeUint()
	.decodeUint()
}

// Since the encoder writes no zeros, if we arrive at a decoder we have
// a value to extract and store. The field number has already been read
// (it's how we knew to call this decoder).
// Each decoder is responsible for handling any indirections associated
// with the data structure. If any pointer so reached is nil, allocation must
// be done.

// decAlloc takes a value and returns a settable value that can
// be assigned to. If the value is a pointer, decAlloc guarantees it points to storage.
// The callers to the individual decoders are expected to have used decAlloc.
// The individual decoders don't need it.
func decAlloc( reflect.Value) reflect.Value {
	for .Kind() == reflect.Pointer {
		if .IsNil() {
			.Set(reflect.New(.Type().Elem()))
		}
		 = .Elem()
	}
	return 
}

// decBool decodes a uint and stores it as a boolean in value.
func decBool( *decInstr,  *decoderState,  reflect.Value) {
	.SetBool(.decodeUint() != 0)
}

// decInt8 decodes an integer and stores it as an int8 in value.
func decInt8( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeInt()
	if  < math.MinInt8 || math.MaxInt8 <  {
		error_(.ovfl)
	}
	.SetInt()
}

// decUint8 decodes an unsigned integer and stores it as a uint8 in value.
func decUint8( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeUint()
	if math.MaxUint8 <  {
		error_(.ovfl)
	}
	.SetUint()
}

// decInt16 decodes an integer and stores it as an int16 in value.
func decInt16( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeInt()
	if  < math.MinInt16 || math.MaxInt16 <  {
		error_(.ovfl)
	}
	.SetInt()
}

// decUint16 decodes an unsigned integer and stores it as a uint16 in value.
func decUint16( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeUint()
	if math.MaxUint16 <  {
		error_(.ovfl)
	}
	.SetUint()
}

// decInt32 decodes an integer and stores it as an int32 in value.
func decInt32( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeInt()
	if  < math.MinInt32 || math.MaxInt32 <  {
		error_(.ovfl)
	}
	.SetInt()
}

// decUint32 decodes an unsigned integer and stores it as a uint32 in value.
func decUint32( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeUint()
	if math.MaxUint32 <  {
		error_(.ovfl)
	}
	.SetUint()
}

// decInt64 decodes an integer and stores it as an int64 in value.
func decInt64( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeInt()
	.SetInt()
}

// decUint64 decodes an unsigned integer and stores it as a uint64 in value.
func decUint64( *decInstr,  *decoderState,  reflect.Value) {
	 := .decodeUint()
	.SetUint()
}

// Floating-point numbers are transmitted as uint64s holding the bits
// of the underlying representation. They are sent byte-reversed, with
// the exponent end coming out first, so integer floating point numbers
// (for example) transmit more compactly. This routine does the
// unswizzling.
func float64FromBits( uint64) float64 {
	 := bits.ReverseBytes64()
	return math.Float64frombits()
}

// float32FromBits decodes an unsigned integer, treats it as a 32-bit floating-point
// number, and returns it. It's a helper function for float32 and complex64.
// It returns a float64 because that's what reflection needs, but its return
// value is known to be accurately representable in a float32.
func float32FromBits( uint64,  error) float64 {
	 := float64FromBits()
	 := 
	if  < 0 {
		 = -
	}
	// +Inf is OK in both 32- and 64-bit floats. Underflow is always OK.
	if math.MaxFloat32 <  &&  <= math.MaxFloat64 {
		error_()
	}
	return 
}

// decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
// number, and stores it in value.
func decFloat32( *decInstr,  *decoderState,  reflect.Value) {
	.SetFloat(float32FromBits(.decodeUint(), .ovfl))
}

// decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point
// number, and stores it in value.
func decFloat64( *decInstr,  *decoderState,  reflect.Value) {
	.SetFloat(float64FromBits(.decodeUint()))
}

// decComplex64 decodes a pair of unsigned integers, treats them as a
// pair of floating point numbers, and stores them as a complex64 in value.
// The real part comes first.
func decComplex64( *decInstr,  *decoderState,  reflect.Value) {
	 := float32FromBits(.decodeUint(), .ovfl)
	 := float32FromBits(.decodeUint(), .ovfl)
	.SetComplex(complex(, ))
}

// decComplex128 decodes a pair of unsigned integers, treats them as a
// pair of floating point numbers, and stores them as a complex128 in value.
// The real part comes first.
func decComplex128( *decInstr,  *decoderState,  reflect.Value) {
	 := float64FromBits(.decodeUint())
	 := float64FromBits(.decodeUint())
	.SetComplex(complex(, ))
}

// decUint8Slice decodes a byte slice and stores in value a slice header
// describing the data.
// uint8 slices are encoded as an unsigned count followed by the raw bytes.
func decUint8Slice( *decInstr,  *decoderState,  reflect.Value) {
	,  := .getLength()
	if ! {
		errorf("bad %s slice length: %d", .Type(), )
	}
	if .Cap() <  {
		 := saferio.SliceCap[byte](uint64())
		if  < 0 {
			errorf("%s slice too big: %d elements", .Type(), )
		}
		.Set(reflect.MakeSlice(.Type(), , ))
		 := 
		 := 0
		for  <  {
			if  >=  {
				// We didn't allocate the entire slice,
				// due to using saferio.SliceCap.
				// Grow the slice for one more element.
				// The slice is full, so this should
				// bump up the capacity.
				.Grow(1)
			}
			// Copy into s up to the capacity or n,
			// whichever is less.
			 = .Cap()
			if  >  {
				 = 
			}
			.SetLen()
			 := .Slice(, )
			if ,  := .b.Read(.Bytes());  != nil {
				errorf("error decoding []byte at %d: %s", , )
			}
			 = 
		}
	} else {
		.SetLen()
		if ,  := .b.Read(.Bytes());  != nil {
			errorf("error decoding []byte: %s", )
		}
	}
}

// decString decodes byte array and stores in value a string header
// describing the data.
// Strings are encoded as an unsigned count followed by the raw bytes.
func decString( *decInstr,  *decoderState,  reflect.Value) {
	,  := .getLength()
	if ! {
		errorf("bad %s slice length: %d", .Type(), )
	}
	// Read the data.
	 := .b.Bytes()
	if len() <  {
		errorf("invalid string length %d: exceeds input size %d", , len())
	}
	 := string([:])
	.b.Drop()
	.SetString()
}

// ignoreUint8Array skips over the data for a byte slice value with no destination.
func ignoreUint8Array( *decInstr,  *decoderState,  reflect.Value) {
	,  := .getLength()
	if ! {
		errorf("slice length too large")
	}
	 := .b.Len()
	if  <  {
		errorf("invalid slice length %d: exceeds input size %d", , )
	}
	.b.Drop()
}

// Execution engine

// The encoder engine is an array of instructions indexed by field number of the incoming
// decoder. It is executed with random access according to field number.
type decEngine struct {
	instr    []decInstr
	numInstr int // the number of active instructions
}

// decodeSingle decodes a top-level value that is not a struct and stores it in value.
// Such values are preceded by a zero, making them have the memory layout of a
// struct field (although with an illegal field number).
func ( *Decoder) ( *decEngine,  reflect.Value) {
	 := .newDecoderState(&.buf)
	defer .freeDecoderState()
	.fieldnum = singletonField
	if .decodeUint() != 0 {
		errorf("decode: corrupted data: non-zero delta for singleton")
	}
	 := &.instr[singletonField]
	.op(, , )
}

// decodeStruct decodes a top-level struct and stores it in value.
// Indir is for the value, not the type. At the time of the call it may
// differ from ut.indir, which was computed when the engine was built.
// This state cannot arise for decodeSingle, which is called directly
// from the user's value, not from the innards of an engine.
func ( *Decoder) ( *decEngine,  reflect.Value) {
	 := .newDecoderState(&.buf)
	defer .freeDecoderState()
	.fieldnum = -1
	for .b.Len() > 0 {
		 := int(.decodeUint())
		if  < 0 {
			errorf("decode: corrupted data: negative delta")
		}
		if  == 0 { // struct terminator is zero delta fieldnum
			break
		}
		if .fieldnum >= len(.instr)- { // subtract to compare without overflow
			error_(errRange)
		}
		 := .fieldnum + 
		 := &.instr[]
		var  reflect.Value
		if .index != nil {
			// Otherwise the field is unknown to us and instr.op is an ignore op.
			 = .FieldByIndex(.index)
			if .Kind() == reflect.Pointer {
				 = decAlloc()
			}
		}
		.op(, , )
		.fieldnum = 
	}
}

var noValue reflect.Value

// ignoreStruct discards the data for a struct with no destination.
func ( *Decoder) ( *decEngine) {
	 := .newDecoderState(&.buf)
	defer .freeDecoderState()
	.fieldnum = -1
	for .b.Len() > 0 {
		 := int(.decodeUint())
		if  < 0 {
			errorf("ignore decode: corrupted data: negative delta")
		}
		if  == 0 { // struct terminator is zero delta fieldnum
			break
		}
		 := .fieldnum + 
		if  >= len(.instr) {
			error_(errRange)
		}
		 := &.instr[]
		.op(, , noValue)
		.fieldnum = 
	}
}

// ignoreSingle discards the data for a top-level non-struct value with no
// destination. It's used when calling Decode with a nil value.
func ( *Decoder) ( *decEngine) {
	 := .newDecoderState(&.buf)
	defer .freeDecoderState()
	.fieldnum = singletonField
	 := int(.decodeUint())
	if  != 0 {
		errorf("decode: corrupted data: non-zero delta for singleton")
	}
	 := &.instr[singletonField]
	.op(, , noValue)
}

// decodeArrayHelper does the work for decoding arrays and slices.
func ( *Decoder) ( *decoderState,  reflect.Value,  decOp,  int,  error,  decHelper) {
	if  != nil && (, , , ) {
		return
	}
	 := &decInstr{, 0, nil, }
	 := .Type().Elem().Kind() == reflect.Pointer
	 := .Len()
	for  := 0;  < ; ++ {
		if .b.Len() == 0 {
			errorf("decoding array or slice: length exceeds input size (%d elements)", )
		}
		if  >=  {
			// This is a slice that we only partially allocated.
			// Grow it up to length.
			.Grow(1)
			 := .Cap()
			if  >  {
				 = 
			}
			.SetLen()
			 = 
		}
		 := .Index()
		if  {
			 = decAlloc()
		}
		(, , )
	}
}

// decodeArray decodes an array and stores it in value.
// The length is an unsigned integer preceding the elements. Even though the length is redundant
// (it's part of the type), it's a useful check and is included in the encoding.
func ( *Decoder) ( *decoderState,  reflect.Value,  decOp,  int,  error,  decHelper) {
	if  := .decodeUint();  != uint64() {
		errorf("length mismatch in decodeArray")
	}
	.decodeArrayHelper(, , , , , )
}

// decodeIntoValue is a helper for map decoding.
func decodeIntoValue( *decoderState,  decOp,  bool,  reflect.Value,  *decInstr) reflect.Value {
	 := 
	if  {
		 = decAlloc()
	}

	(, , )
	return 
}

// decodeMap decodes a map and stores it in value.
// Maps are encoded as a length followed by key:value pairs.
// Because the internals of maps are not visible to us, we must
// use reflection rather than pointer magic.
func ( *Decoder) ( reflect.Type,  *decoderState,  reflect.Value, ,  decOp,  error) {
	 := int(.decodeUint())
	if .IsNil() {
		.Set(reflect.MakeMapWithSize(, ))
	}
	 := .Key().Kind() == reflect.Pointer
	 := .Elem().Kind() == reflect.Pointer
	 := &decInstr{, 0, nil, }
	 := &decInstr{, 0, nil, }
	 := reflect.New(.Key())
	 := reflect.New(.Elem())
	for  := 0;  < ; ++ {
		 := decodeIntoValue(, , , .Elem(), )
		 := decodeIntoValue(, , , .Elem(), )
		.SetMapIndex(, )
		.Elem().SetZero()
		.Elem().SetZero()
	}
}

// ignoreArrayHelper does the work for discarding arrays and slices.
func ( *Decoder) ( *decoderState,  decOp,  int) {
	 := &decInstr{, 0, nil, errors.New("no error")}
	for  := 0;  < ; ++ {
		if .b.Len() == 0 {
			errorf("decoding array or slice: length exceeds input size (%d elements)", )
		}
		(, , noValue)
	}
}

// ignoreArray discards the data for an array value with no destination.
func ( *Decoder) ( *decoderState,  decOp,  int) {
	if  := .decodeUint();  != uint64() {
		errorf("length mismatch in ignoreArray")
	}
	.ignoreArrayHelper(, , )
}

// ignoreMap discards the data for a map value with no destination.
func ( *Decoder) ( *decoderState, ,  decOp) {
	 := int(.decodeUint())
	 := &decInstr{, 0, nil, errors.New("no error")}
	 := &decInstr{, 0, nil, errors.New("no error")}
	for  := 0;  < ; ++ {
		(, , noValue)
		(, , noValue)
	}
}

// decodeSlice decodes a slice and stores it in value.
// Slices are encoded as an unsigned length followed by the elements.
func ( *Decoder) ( *decoderState,  reflect.Value,  decOp,  error,  decHelper) {
	 := .decodeUint()
	 := .Type()
	 := uint64(.Elem().Size())
	 :=  * 
	 := int()
	// Take care with overflow in this calculation.
	if  < 0 || uint64() !=  ||  > tooBig || ( > 0 && / != ) {
		// We don't check n against buffer length here because if it's a slice
		// of interfaces, there will be buffer reloads.
		errorf("%s slice too big: %d elements of %d bytes", .Elem(), , )
	}
	if .Cap() <  {
		 := saferio.SliceCapWithSize(, uint64())
		if  < 0 {
			errorf("%s slice too big: %d elements of %d bytes", .Elem(), , )
		}
		.Set(reflect.MakeSlice(, , ))
	} else {
		.SetLen()
	}
	.decodeArrayHelper(, , , , , )
}

// ignoreSlice skips over the data for a slice value with no destination.
func ( *Decoder) ( *decoderState,  decOp) {
	.ignoreArrayHelper(, , int(.decodeUint()))
}

// decodeInterface decodes an interface value and stores it in value.
// Interfaces are encoded as the name of a concrete type followed by a value.
// If the name is empty, the value is nil and no value is sent.
func ( *Decoder) ( reflect.Type,  *decoderState,  reflect.Value) {
	// Read the name of the concrete type.
	 := .decodeUint()
	if  > 1<<31 { // zero is permissible for anonymous types
		errorf("invalid type name length %d", )
	}
	if  > uint64(.b.Len()) {
		errorf("invalid type name length %d: exceeds input size", )
	}
	 := int()
	 := .b.Bytes()[:]
	.b.Drop()
	// Allocate the destination interface value.
	if len() == 0 {
		// Copy the nil interface value to the target.
		.SetZero()
		return
	}
	if len() > 1024 {
		errorf("name too long (%d bytes): %.20q...", len(), )
	}
	// The concrete type must be registered.
	,  := nameToConcreteType.Load(string())
	if ! {
		errorf("name not registered for interface: %q", )
	}
	 := .(reflect.Type)

	// Read the type id of the concrete value.
	 := .decodeTypeSequence(true)
	if  < 0 {
		error_(.err)
	}
	// Byte count of value is next; we don't care what it is (it's there
	// in case we want to ignore the value by skipping it completely).
	.decodeUint()
	// Read the concrete value.
	 := allocValue()
	.decodeValue(, )
	if .err != nil {
		error_(.err)
	}
	// Assign the concrete value to the interface.
	// Tread carefully; it might not satisfy the interface.
	if !.AssignableTo() {
		errorf("%s is not assignable to type %s", , )
	}
	// Copy the interface value to the target.
	.Set()
}

// ignoreInterface discards the data for an interface value with no destination.
func ( *Decoder) ( *decoderState) {
	// Read the name of the concrete type.
	,  := .getLength()
	if ! {
		errorf("bad interface encoding: name too large for buffer")
	}
	 := .b.Len()
	if  <  {
		errorf("invalid interface value length %d: exceeds input size %d", , )
	}
	.b.Drop()
	 := .decodeTypeSequence(true)
	if  < 0 {
		error_(.err)
	}
	// At this point, the decoder buffer contains a delimited value. Just toss it.
	,  = .getLength()
	if ! {
		errorf("bad interface encoding: data length too large for buffer")
	}
	.b.Drop()
}

// decodeGobDecoder decodes something implementing the GobDecoder interface.
// The data is encoded as a byte slice.
func ( *Decoder) ( *userTypeInfo,  *decoderState,  reflect.Value) {
	// Read the bytes for the value.
	,  := .getLength()
	if ! {
		errorf("GobDecoder: length too large for buffer")
	}
	 := .b.Bytes()
	if len() <  {
		errorf("GobDecoder: invalid data length %d: exceeds input size %d", , len())
	}
	 = [:]
	.b.Drop()
	var  error
	// We know it's one of these.
	switch .externalDec {
	case xGob:
		 = .Interface().(GobDecoder).GobDecode()
	case xBinary:
		 = .Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary()
	case xText:
		 = .Interface().(encoding.TextUnmarshaler).UnmarshalText()
	}
	if  != nil {
		error_()
	}
}

// ignoreGobDecoder discards the data for a GobDecoder value with no destination.
func ( *Decoder) ( *decoderState) {
	// Read the bytes for the value.
	,  := .getLength()
	if ! {
		errorf("GobDecoder: length too large for buffer")
	}
	 := .b.Len()
	if  <  {
		errorf("GobDecoder: invalid data length %d: exceeds input size %d", , )
	}
	.b.Drop()
}

// Index by Go types.
var decOpTable = [...]decOp{
	reflect.Bool:       decBool,
	reflect.Int8:       decInt8,
	reflect.Int16:      decInt16,
	reflect.Int32:      decInt32,
	reflect.Int64:      decInt64,
	reflect.Uint8:      decUint8,
	reflect.Uint16:     decUint16,
	reflect.Uint32:     decUint32,
	reflect.Uint64:     decUint64,
	reflect.Float32:    decFloat32,
	reflect.Float64:    decFloat64,
	reflect.Complex64:  decComplex64,
	reflect.Complex128: decComplex128,
	reflect.String:     decString,
}

// Indexed by gob types.  tComplex will be added during type.init().
var decIgnoreOpMap = map[typeId]decOp{
	tBool:    ignoreUint,
	tInt:     ignoreUint,
	tUint:    ignoreUint,
	tFloat:   ignoreUint,
	tBytes:   ignoreUint8Array,
	tString:  ignoreUint8Array,
	tComplex: ignoreTwoUints,
}

// decOpFor returns the decoding op for the base type under rt and
// the indirection count to reach it.
func ( *Decoder) ( typeId,  reflect.Type,  string,  map[reflect.Type]*decOp) *decOp {
	 := userType()
	// If the type implements GobEncoder, we handle it without further processing.
	if .externalDec != 0 {
		return .gobDecodeOpFor()
	}

	// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
	// Return the pointer to the op we're already building.
	if  := [];  != nil {
		return 
	}
	 := .base
	var  decOp
	 := .Kind()
	if int() < len(decOpTable) {
		 = decOpTable[]
	}
	if  == nil {
		[] = &
		// Special cases
		switch  := ; .Kind() {
		case reflect.Array:
			 = "element of " + 
			 := .wireType[].ArrayT.Elem
			 := .(, .Elem(), , )
			 := overflow()
			 := decArrayHelper[.Elem().Kind()]
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.decodeArray(, , *, .Len(), , )
			}

		case reflect.Map:
			 := .wireType[].MapT.Key
			 := .wireType[].MapT.Elem
			 := .(, .Key(), "key of "+, )
			 := .(, .Elem(), "element of "+, )
			 := overflow()
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.decodeMap(, , , *, *, )
			}

		case reflect.Slice:
			 = "element of " + 
			if .Elem().Kind() == reflect.Uint8 {
				 = decUint8Slice
				break
			}
			var  typeId
			if  := builtinIdToType();  != nil {
				 = .(*sliceType).Elem
			} else {
				 = .wireType[].SliceT.Elem
			}
			 := .(, .Elem(), , )
			 := overflow()
			 := decSliceHelper[.Elem().Kind()]
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.decodeSlice(, , *, , )
			}

		case reflect.Struct:
			// Generate a closure that calls out to the engine for the nested type.
			 := userType()
			,  := .getDecEnginePtr(, )
			if  != nil {
				error_()
			}
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				// indirect through enginePtr to delay evaluation for recursive structs.
				.decodeStruct(*, )
			}
		case reflect.Interface:
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.decodeInterface(, , )
			}
		}
	}
	if  == nil {
		errorf("decode can't handle type %s", )
	}
	return &
}

var maxIgnoreNestingDepth = 10000

// decIgnoreOpFor returns the decoding op for a field that has no destination.
func ( *Decoder) ( typeId,  map[typeId]*decOp,  int) *decOp {
	if  > maxIgnoreNestingDepth {
		error_(errors.New("invalid nesting depth"))
	}
	// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
	// Return the pointer to the op we're already building.
	if  := [];  != nil {
		return 
	}
	,  := decIgnoreOpMap[]
	if ! {
		[] = &
		if  == tInterface {
			// Special case because it's a method: the ignored item might
			// define types and we need to record their state in the decoder.
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.ignoreInterface()
			}
			return &
		}
		// Special cases
		 := .wireType[]
		switch {
		case  == nil:
			errorf("bad data: undefined type %s", .string())
		case .ArrayT != nil:
			 := .ArrayT.Elem
			 := .(, , +1)
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.ignoreArray(, *, .ArrayT.Len)
			}

		case .MapT != nil:
			 := .wireType[].MapT.Key
			 := .wireType[].MapT.Elem
			 := .(, , +1)
			 := .(, , +1)
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.ignoreMap(, *, *)
			}

		case .SliceT != nil:
			 := .SliceT.Elem
			 := .(, , +1)
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.ignoreSlice(, *)
			}

		case .StructT != nil:
			// Generate a closure that calls out to the engine for the nested type.
			,  := .getIgnoreEnginePtr()
			if  != nil {
				error_()
			}
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				// indirect through enginePtr to delay evaluation for recursive structs
				.dec.ignoreStruct(*)
			}

		case .GobEncoderT != nil, .BinaryMarshalerT != nil, .TextMarshalerT != nil:
			 = func( *decInstr,  *decoderState,  reflect.Value) {
				.dec.ignoreGobDecoder()
			}
		}
	}
	if  == nil {
		errorf("bad data: ignore can't handle type %s", .string())
	}
	return &
}

// gobDecodeOpFor returns the op for a type that is known to implement
// GobDecoder.
func ( *Decoder) ( *userTypeInfo) *decOp {
	 := .user
	if .decIndir == -1 {
		 = reflect.PointerTo()
	} else if .decIndir > 0 {
		for  := int8(0);  < .decIndir; ++ {
			 = .Elem()
		}
	}
	var  decOp
	 = func( *decInstr,  *decoderState,  reflect.Value) {
		// We now have the base type. We need its address if the receiver is a pointer.
		if .Kind() != reflect.Pointer && .Kind() == reflect.Pointer {
			 = .Addr()
		}
		.dec.decodeGobDecoder(, , )
	}
	return &
}

// compatibleType asks: Are these two gob Types compatible?
// Answers the question for basic types, arrays, maps and slices, plus
// GobEncoder/Decoder pairs.
// Structs are considered ok; fields will be checked later.
func ( *Decoder) ( reflect.Type,  typeId,  map[reflect.Type]typeId) bool {
	if ,  := [];  {
		return  == 
	}
	[] = 
	 := userType()
	,  := .wireType[]
	// If wire was encoded with an encoding method, fr must have that method.
	// And if not, it must not.
	// At most one of the booleans in ut is set.
	// We could possibly relax this constraint in the future in order to
	// choose the decoding method using the data in the wireType.
	// The parentheses look odd but are correct.
	if (.externalDec == xGob) != ( && .GobEncoderT != nil) ||
		(.externalDec == xBinary) != ( && .BinaryMarshalerT != nil) ||
		(.externalDec == xText) != ( && .TextMarshalerT != nil) {
		return false
	}
	if .externalDec != 0 { // This test trumps all others.
		return true
	}
	switch  := .base; .Kind() {
	default:
		// chan, etc: cannot handle.
		return false
	case reflect.Bool:
		return  == tBool
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return  == tInt
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return  == tUint
	case reflect.Float32, reflect.Float64:
		return  == tFloat
	case reflect.Complex64, reflect.Complex128:
		return  == tComplex
	case reflect.String:
		return  == tString
	case reflect.Interface:
		return  == tInterface
	case reflect.Array:
		if ! || .ArrayT == nil {
			return false
		}
		 := .ArrayT
		return .Len() == .Len && .(.Elem(), .Elem, )
	case reflect.Map:
		if ! || .MapT == nil {
			return false
		}
		 := .MapT
		return .(.Key(), .Key, ) && .(.Elem(), .Elem, )
	case reflect.Slice:
		// Is it an array of bytes?
		if .Elem().Kind() == reflect.Uint8 {
			return  == tBytes
		}
		// Extract and compare element types.
		var  *sliceType
		if  := builtinIdToType();  != nil {
			, _ = .(*sliceType)
		} else if  != nil {
			 = .SliceT
		}
		 := userType(.Elem()).base
		return  != nil && .(, .Elem, )
	case reflect.Struct:
		return true
	}
}

// typeString returns a human-readable description of the type identified by remoteId.
func ( *Decoder) ( typeId) string {
	typeLock.Lock()
	defer typeLock.Unlock()
	if  := idToType();  != nil {
		// globally known type.
		return .string()
	}
	return .wireType[].string()
}

// compileSingle compiles the decoder engine for a non-struct top-level value, including
// GobDecoders.
func ( *Decoder) ( typeId,  *userTypeInfo) ( *decEngine,  error) {
	 := .user
	 = new(decEngine)
	.instr = make([]decInstr, 1) // one item
	 := .String()                // best we can do
	if !.compatibleType(, , make(map[reflect.Type]typeId)) {
		 := .typeString()
		// Common confusing case: local interface type, remote concrete type.
		if .base.Kind() == reflect.Interface &&  != tInterface {
			return nil, errors.New("gob: local interface type " +  + " can only be decoded from remote interface type; received concrete type " + )
		}
		return nil, errors.New("gob: decoding into local type " +  + ", received remote type " + )
	}
	 := .decOpFor(, , , make(map[reflect.Type]*decOp))
	 := errors.New(`value for "` +  + `" out of range`)
	.instr[singletonField] = decInstr{*, singletonField, nil, }
	.numInstr = 1
	return
}

// compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
func ( *Decoder) ( typeId) *decEngine {
	 := new(decEngine)
	.instr = make([]decInstr, 1) // one item
	 := .decIgnoreOpFor(, make(map[typeId]*decOp), 0)
	 := overflow(.typeString())
	.instr[0] = decInstr{*, 0, nil, }
	.numInstr = 1
	return 
}

// compileDec compiles the decoder engine for a value. If the value is not a struct,
// it calls out to compileSingle.
func ( *Decoder) ( typeId,  *userTypeInfo) ( *decEngine,  error) {
	defer catchError(&)
	 := .base
	 := 
	if .Kind() != reflect.Struct || .externalDec != 0 {
		return .compileSingle(, )
	}
	var  *structType
	// Builtin types can come from global pool; the rest must be defined by the decoder.
	// Also we know we're decoding a struct now, so the client must have sent one.
	if  := builtinIdToType();  != nil {
		, _ = .(*structType)
	} else {
		 := .wireType[]
		if  == nil {
			error_(errBadType)
		}
		 = .StructT
	}
	if  == nil {
		errorf("type mismatch in decoder: want struct type %s; got non-struct", )
	}
	 = new(decEngine)
	.instr = make([]decInstr, len(.Field))
	 := make(map[reflect.Type]*decOp)
	// Loop over the fields of the wire type.
	for  := 0;  < len(.Field); ++ {
		 := .Field[]
		if .Name == "" {
			errorf("empty name for remote field of type %s", .Name)
		}
		 := overflow(.Name)
		// Find the field of the local type with the same name.
		,  := .FieldByName(.Name)
		// TODO(r): anonymous names
		if ! || !isExported(.Name) {
			 := .decIgnoreOpFor(.Id, make(map[typeId]*decOp), 0)
			.instr[] = decInstr{*, , nil, }
			continue
		}
		if !.compatibleType(.Type, .Id, make(map[reflect.Type]typeId)) {
			errorf("wrong type (%s) for received field %s.%s", .Type, .Name, .Name)
		}
		 := .decOpFor(.Id, .Type, .Name, )
		.instr[] = decInstr{*, , .Index, }
		.numInstr++
	}
	return
}

// getDecEnginePtr returns the engine for the specified type.
func ( *Decoder) ( typeId,  *userTypeInfo) ( **decEngine,  error) {
	 := .user
	,  := .decoderCache[]
	if ! {
		 = make(map[typeId]**decEngine)
		.decoderCache[] = 
	}
	if ,  = []; ! {
		// To handle recursive types, mark this engine as underway before compiling.
		 = new(*decEngine)
		[] = 
		*,  = .compileDec(, )
		if  != nil {
			delete(, )
		}
	}
	return
}

// emptyStruct is the type we compile into when ignoring a struct value.
type emptyStruct struct{}

var emptyStructType = reflect.TypeFor[emptyStruct]()

// getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded.
func ( *Decoder) ( typeId) ( **decEngine,  error) {
	var  bool
	if ,  = .ignorerCache[]; ! {
		// To handle recursive types, mark this engine as underway before compiling.
		 = new(*decEngine)
		.ignorerCache[] = 
		 := .wireType[]
		if  != nil && .StructT != nil {
			*,  = .compileDec(, userType(emptyStructType))
		} else {
			* = .compileIgnoreSingle()
		}
		if  != nil {
			delete(.ignorerCache, )
		}
	}
	return
}

// decodeValue decodes the data stream representing a value and stores it in value.
func ( *Decoder) ( typeId,  reflect.Value) {
	defer catchError(&.err)
	// If the value is nil, it means we should just ignore this item.
	if !.IsValid() {
		.decodeIgnoredValue()
		return
	}
	// Dereference down to the underlying type.
	 := userType(.Type())
	 := .base
	var  **decEngine
	, .err = .getDecEnginePtr(, )
	if .err != nil {
		return
	}
	 = decAlloc()
	 := *
	if  := ; .Kind() == reflect.Struct && .externalDec == 0 {
		 := .wireType[]
		if .numInstr == 0 && .NumField() > 0 &&
			 != nil && len(.StructT.Field) > 0 {
			 := .Name()
			errorf("type mismatch: no fields matched compiling decoder for %s", )
		}
		.decodeStruct(, )
	} else {
		.decodeSingle(, )
	}
}

// decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it.
func ( *Decoder) ( typeId) {
	var  **decEngine
	, .err = .getIgnoreEnginePtr()
	if .err != nil {
		return
	}
	 := .wireType[]
	if  != nil && .StructT != nil {
		.ignoreStruct(*)
	} else {
		.ignoreSingle(*)
	}
}

const (
	intBits     = 32 << (^uint(0) >> 63)
	uintptrBits = 32 << (^uintptr(0) >> 63)
)

func init() {
	var ,  decOp
	switch intBits {
	case 32:
		 = decInt32
		 = decUint32
	case 64:
		 = decInt64
		 = decUint64
	default:
		panic("gob: unknown size of int/uint")
	}
	decOpTable[reflect.Int] = 
	decOpTable[reflect.Uint] = 

	// Finally uintptr
	switch uintptrBits {
	case 32:
		 = decUint32
	case 64:
		 = decUint64
	default:
		panic("gob: unknown size of uintptr")
	}
	decOpTable[reflect.Uintptr] = 
}

// Gob depends on being able to take the address
// of zeroed Values it creates, so use this wrapper instead
// of the standard reflect.Zero.
// Each call allocates once.
func allocValue( reflect.Type) reflect.Value {
	return reflect.New().Elem()
}