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.
4
5//go:generate go run decgen.go -output dec_helpers.go
6
7package gob
8
9import (
10	"encoding"
11	"errors"
12	"io"
13	"math"
14	"reflect"
15)
16
17var (
18	errBadUint = errors.New("gob: encoded unsigned integer out of range")
19	errBadType = errors.New("gob: unknown type id or corrupted data")
20	errRange   = errors.New("gob: bad data: field numbers out of bounds")
21)
22
23type decHelper func(state *decoderState, v reflect.Value, length int, ovfl error) bool
24
25// decoderState is the execution state of an instance of the decoder. A new state
26// is created for nested objects.
27type decoderState struct {
28	dec *Decoder
29	// The buffer is stored with an extra indirection because it may be replaced
30	// if we load a type during decode (when reading an interface value).
31	b        *decBuffer
32	fieldnum int           // the last field number read.
33	next     *decoderState // for free list
34}
35
36// decBuffer is an extremely simple, fast implementation of a read-only byte buffer.
37// It is initialized by calling Size and then copying the data into the slice returned by Bytes().
38type decBuffer struct {
39	data   []byte
40	offset int // Read offset.
41}
42
43func (d *decBuffer) Read(p []byte) (int, error) {
44	n := copy(p, d.data[d.offset:])
45	if n == 0 && len(p) != 0 {
46		return 0, io.EOF
47	}
48	d.offset += n
49	return n, nil
50}
51
52func (d *decBuffer) Drop(n int) {
53	if n > d.Len() {
54		panic("drop")
55	}
56	d.offset += n
57}
58
59// Size grows the buffer to exactly n bytes, so d.Bytes() will
60// return a slice of length n. Existing data is first discarded.
61func (d *decBuffer) Size(n int) {
62	d.Reset()
63	if cap(d.data) < n {
64		d.data = make([]byte, n)
65	} else {
66		d.data = d.data[0:n]
67	}
68}
69
70func (d *decBuffer) ReadByte() (byte, error) {
71	if d.offset >= len(d.data) {
72		return 0, io.EOF
73	}
74	c := d.data[d.offset]
75	d.offset++
76	return c, nil
77}
78
79func (d *decBuffer) Len() int {
80	return len(d.data) - d.offset
81}
82
83func (d *decBuffer) Bytes() []byte {
84	return d.data[d.offset:]
85}
86
87func (d *decBuffer) Reset() {
88	d.data = d.data[0:0]
89	d.offset = 0
90}
91
92// We pass the bytes.Buffer separately for easier testing of the infrastructure
93// without requiring a full Decoder.
94func (dec *Decoder) newDecoderState(buf *decBuffer) *decoderState {
95	d := dec.freeList
96	if d == nil {
97		d = new(decoderState)
98		d.dec = dec
99	} else {
100		dec.freeList = d.next
101	}
102	d.b = buf
103	return d
104}
105
106func (dec *Decoder) freeDecoderState(d *decoderState) {
107	d.next = dec.freeList
108	dec.freeList = d
109}
110
111func overflow(name string) error {
112	return errors.New(`value for "` + name + `" out of range`)
113}
114
115// decodeUintReader reads an encoded unsigned integer from an io.Reader.
116// Used only by the Decoder to read the message length.
117func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err error) {
118	width = 1
119	n, err := io.ReadFull(r, buf[0:width])
120	if n == 0 {
121		return
122	}
123	b := buf[0]
124	if b <= 0x7f {
125		return uint64(b), width, nil
126	}
127	n = -int(int8(b))
128	if n > uint64Size {
129		err = errBadUint
130		return
131	}
132	width, err = io.ReadFull(r, buf[0:n])
133	if err != nil {
134		if err == io.EOF {
135			err = io.ErrUnexpectedEOF
136		}
137		return
138	}
139	// Could check that the high byte is zero but it's not worth it.
140	for _, b := range buf[0:width] {
141		x = x<<8 | uint64(b)
142	}
143	width++ // +1 for length byte
144	return
145}
146
147// decodeUint reads an encoded unsigned integer from state.r.
148// Does not check for overflow.
149func (state *decoderState) decodeUint() (x uint64) {
150	b, err := state.b.ReadByte()
151	if err != nil {
152		error_(err)
153	}
154	if b <= 0x7f {
155		return uint64(b)
156	}
157	n := -int(int8(b))
158	if n > uint64Size {
159		error_(errBadUint)
160	}
161	buf := state.b.Bytes()
162	if len(buf) < n {
163		errorf("invalid uint data length %d: exceeds input size %d", n, len(buf))
164	}
165	// Don't need to check error; it's safe to loop regardless.
166	// Could check that the high byte is zero but it's not worth it.
167	for _, b := range buf[0:n] {
168		x = x<<8 | uint64(b)
169	}
170	state.b.Drop(n)
171	return x
172}
173
174// decodeInt reads an encoded signed integer from state.r.
175// Does not check for overflow.
176func (state *decoderState) decodeInt() int64 {
177	x := state.decodeUint()
178	if x&1 != 0 {
179		return ^int64(x >> 1)
180	}
181	return int64(x >> 1)
182}
183
184// getLength decodes the next uint and makes sure it is a possible
185// size for a data item that follows, which means it must fit in a
186// non-negative int and fit in the buffer.
187func (state *decoderState) getLength() (int, bool) {
188	n := int(state.decodeUint())
189	if n < 0 || state.b.Len() < n || tooBig <= n {
190		return 0, false
191	}
192	return n, true
193}
194
195// decOp is the signature of a decoding operator for a given type.
196type decOp func(i *decInstr, state *decoderState, v reflect.Value)
197
198// The 'instructions' of the decoding machine
199type decInstr struct {
200	op    decOp
201	field int   // field number of the wire type
202	index []int // field access indices for destination type
203	ovfl  error // error message for overflow/underflow (for arrays, of the elements)
204}
205
206// ignoreUint discards a uint value with no destination.
207func ignoreUint(i *decInstr, state *decoderState, v reflect.Value) {
208	state.decodeUint()
209}
210
211// ignoreTwoUints discards a uint value with no destination. It's used to skip
212// complex values.
213func ignoreTwoUints(i *decInstr, state *decoderState, v reflect.Value) {
214	state.decodeUint()
215	state.decodeUint()
216}
217
218// Since the encoder writes no zeros, if we arrive at a decoder we have
219// a value to extract and store.  The field number has already been read
220// (it's how we knew to call this decoder).
221// Each decoder is responsible for handling any indirections associated
222// with the data structure.  If any pointer so reached is nil, allocation must
223// be done.
224
225// decAlloc takes a value and returns a settable value that can
226// be assigned to. If the value is a pointer, decAlloc guarantees it points to storage.
227// The callers to the individual decoders are expected to have used decAlloc.
228// The individual decoders don't need to it.
229func decAlloc(v reflect.Value) reflect.Value {
230	for v.Kind() == reflect.Ptr {
231		if v.IsNil() {
232			v.Set(reflect.New(v.Type().Elem()))
233		}
234		v = v.Elem()
235	}
236	return v
237}
238
239// decBool decodes a uint and stores it as a boolean in value.
240func decBool(i *decInstr, state *decoderState, value reflect.Value) {
241	value.SetBool(state.decodeUint() != 0)
242}
243
244// decInt8 decodes an integer and stores it as an int8 in value.
245func decInt8(i *decInstr, state *decoderState, value reflect.Value) {
246	v := state.decodeInt()
247	if v < math.MinInt8 || math.MaxInt8 < v {
248		error_(i.ovfl)
249	}
250	value.SetInt(v)
251}
252
253// decUint8 decodes an unsigned integer and stores it as a uint8 in value.
254func decUint8(i *decInstr, state *decoderState, value reflect.Value) {
255	v := state.decodeUint()
256	if math.MaxUint8 < v {
257		error_(i.ovfl)
258	}
259	value.SetUint(v)
260}
261
262// decInt16 decodes an integer and stores it as an int16 in value.
263func decInt16(i *decInstr, state *decoderState, value reflect.Value) {
264	v := state.decodeInt()
265	if v < math.MinInt16 || math.MaxInt16 < v {
266		error_(i.ovfl)
267	}
268	value.SetInt(v)
269}
270
271// decUint16 decodes an unsigned integer and stores it as a uint16 in value.
272func decUint16(i *decInstr, state *decoderState, value reflect.Value) {
273	v := state.decodeUint()
274	if math.MaxUint16 < v {
275		error_(i.ovfl)
276	}
277	value.SetUint(v)
278}
279
280// decInt32 decodes an integer and stores it as an int32 in value.
281func decInt32(i *decInstr, state *decoderState, value reflect.Value) {
282	v := state.decodeInt()
283	if v < math.MinInt32 || math.MaxInt32 < v {
284		error_(i.ovfl)
285	}
286	value.SetInt(v)
287}
288
289// decUint32 decodes an unsigned integer and stores it as a uint32 in value.
290func decUint32(i *decInstr, state *decoderState, value reflect.Value) {
291	v := state.decodeUint()
292	if math.MaxUint32 < v {
293		error_(i.ovfl)
294	}
295	value.SetUint(v)
296}
297
298// decInt64 decodes an integer and stores it as an int64 in value.
299func decInt64(i *decInstr, state *decoderState, value reflect.Value) {
300	v := state.decodeInt()
301	value.SetInt(v)
302}
303
304// decUint64 decodes an unsigned integer and stores it as a uint64 in value.
305func decUint64(i *decInstr, state *decoderState, value reflect.Value) {
306	v := state.decodeUint()
307	value.SetUint(v)
308}
309
310// Floating-point numbers are transmitted as uint64s holding the bits
311// of the underlying representation.  They are sent byte-reversed, with
312// the exponent end coming out first, so integer floating point numbers
313// (for example) transmit more compactly.  This routine does the
314// unswizzling.
315func float64FromBits(u uint64) float64 {
316	var v uint64
317	for i := 0; i < 8; i++ {
318		v <<= 8
319		v |= u & 0xFF
320		u >>= 8
321	}
322	return math.Float64frombits(v)
323}
324
325// float32FromBits decodes an unsigned integer, treats it as a 32-bit floating-point
326// number, and returns it. It's a helper function for float32 and complex64.
327// It returns a float64 because that's what reflection needs, but its return
328// value is known to be accurately representable in a float32.
329func float32FromBits(u uint64, ovfl error) float64 {
330	v := float64FromBits(u)
331	av := v
332	if av < 0 {
333		av = -av
334	}
335	// +Inf is OK in both 32- and 64-bit floats.  Underflow is always OK.
336	if math.MaxFloat32 < av && av <= math.MaxFloat64 {
337		error_(ovfl)
338	}
339	return v
340}
341
342// decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
343// number, and stores it in value.
344func decFloat32(i *decInstr, state *decoderState, value reflect.Value) {
345	value.SetFloat(float32FromBits(state.decodeUint(), i.ovfl))
346}
347
348// decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point
349// number, and stores it in value.
350func decFloat64(i *decInstr, state *decoderState, value reflect.Value) {
351	value.SetFloat(float64FromBits(state.decodeUint()))
352}
353
354// decComplex64 decodes a pair of unsigned integers, treats them as a
355// pair of floating point numbers, and stores them as a complex64 in value.
356// The real part comes first.
357func decComplex64(i *decInstr, state *decoderState, value reflect.Value) {
358	real := float32FromBits(state.decodeUint(), i.ovfl)
359	imag := float32FromBits(state.decodeUint(), i.ovfl)
360	value.SetComplex(complex(real, imag))
361}
362
363// decComplex128 decodes a pair of unsigned integers, treats them as a
364// pair of floating point numbers, and stores them as a complex128 in value.
365// The real part comes first.
366func decComplex128(i *decInstr, state *decoderState, value reflect.Value) {
367	real := float64FromBits(state.decodeUint())
368	imag := float64FromBits(state.decodeUint())
369	value.SetComplex(complex(real, imag))
370}
371
372// decUint8Slice decodes a byte slice and stores in value a slice header
373// describing the data.
374// uint8 slices are encoded as an unsigned count followed by the raw bytes.
375func decUint8Slice(i *decInstr, state *decoderState, value reflect.Value) {
376	n, ok := state.getLength()
377	if !ok {
378		errorf("bad %s slice length: %d", value.Type(), n)
379	}
380	if value.Cap() < n {
381		value.Set(reflect.MakeSlice(value.Type(), n, n))
382	} else {
383		value.Set(value.Slice(0, n))
384	}
385	if _, err := state.b.Read(value.Bytes()); err != nil {
386		errorf("error decoding []byte: %s", err)
387	}
388}
389
390// decString decodes byte array and stores in value a string header
391// describing the data.
392// Strings are encoded as an unsigned count followed by the raw bytes.
393func decString(i *decInstr, state *decoderState, value reflect.Value) {
394	n, ok := state.getLength()
395	if !ok {
396		errorf("bad %s slice length: %d", value.Type(), n)
397	}
398	// Read the data.
399	data := state.b.Bytes()
400	if len(data) < n {
401		errorf("invalid string length %d: exceeds input size %d", n, len(data))
402	}
403	s := string(data[:n])
404	state.b.Drop(n)
405	value.SetString(s)
406}
407
408// ignoreUint8Array skips over the data for a byte slice value with no destination.
409func ignoreUint8Array(i *decInstr, state *decoderState, value reflect.Value) {
410	n, ok := state.getLength()
411	if !ok {
412		errorf("slice length too large")
413	}
414	bn := state.b.Len()
415	if bn < n {
416		errorf("invalid slice length %d: exceeds input size %d", n, bn)
417	}
418	state.b.Drop(n)
419}
420
421// Execution engine
422
423// The encoder engine is an array of instructions indexed by field number of the incoming
424// decoder.  It is executed with random access according to field number.
425type decEngine struct {
426	instr    []decInstr
427	numInstr int // the number of active instructions
428}
429
430// decodeSingle decodes a top-level value that is not a struct and stores it in value.
431// Such values are preceded by a zero, making them have the memory layout of a
432// struct field (although with an illegal field number).
433func (dec *Decoder) decodeSingle(engine *decEngine, ut *userTypeInfo, value reflect.Value) {
434	state := dec.newDecoderState(&dec.buf)
435	defer dec.freeDecoderState(state)
436	state.fieldnum = singletonField
437	if state.decodeUint() != 0 {
438		errorf("decode: corrupted data: non-zero delta for singleton")
439	}
440	instr := &engine.instr[singletonField]
441	instr.op(instr, state, value)
442}
443
444// decodeStruct decodes a top-level struct and stores it in value.
445// Indir is for the value, not the type.  At the time of the call it may
446// differ from ut.indir, which was computed when the engine was built.
447// This state cannot arise for decodeSingle, which is called directly
448// from the user's value, not from the innards of an engine.
449func (dec *Decoder) decodeStruct(engine *decEngine, ut *userTypeInfo, value reflect.Value) {
450	state := dec.newDecoderState(&dec.buf)
451	defer dec.freeDecoderState(state)
452	state.fieldnum = -1
453	for state.b.Len() > 0 {
454		delta := int(state.decodeUint())
455		if delta < 0 {
456			errorf("decode: corrupted data: negative delta")
457		}
458		if delta == 0 { // struct terminator is zero delta fieldnum
459			break
460		}
461		fieldnum := state.fieldnum + delta
462		if fieldnum >= len(engine.instr) {
463			error_(errRange)
464			break
465		}
466		instr := &engine.instr[fieldnum]
467		var field reflect.Value
468		if instr.index != nil {
469			// Otherwise the field is unknown to us and instr.op is an ignore op.
470			field = value.FieldByIndex(instr.index)
471			if field.Kind() == reflect.Ptr {
472				field = decAlloc(field)
473			}
474		}
475		instr.op(instr, state, field)
476		state.fieldnum = fieldnum
477	}
478}
479
480var noValue reflect.Value
481
482// ignoreStruct discards the data for a struct with no destination.
483func (dec *Decoder) ignoreStruct(engine *decEngine) {
484	state := dec.newDecoderState(&dec.buf)
485	defer dec.freeDecoderState(state)
486	state.fieldnum = -1
487	for state.b.Len() > 0 {
488		delta := int(state.decodeUint())
489		if delta < 0 {
490			errorf("ignore decode: corrupted data: negative delta")
491		}
492		if delta == 0 { // struct terminator is zero delta fieldnum
493			break
494		}
495		fieldnum := state.fieldnum + delta
496		if fieldnum >= len(engine.instr) {
497			error_(errRange)
498		}
499		instr := &engine.instr[fieldnum]
500		instr.op(instr, state, noValue)
501		state.fieldnum = fieldnum
502	}
503}
504
505// ignoreSingle discards the data for a top-level non-struct value with no
506// destination. It's used when calling Decode with a nil value.
507func (dec *Decoder) ignoreSingle(engine *decEngine) {
508	state := dec.newDecoderState(&dec.buf)
509	defer dec.freeDecoderState(state)
510	state.fieldnum = singletonField
511	delta := int(state.decodeUint())
512	if delta != 0 {
513		errorf("decode: corrupted data: non-zero delta for singleton")
514	}
515	instr := &engine.instr[singletonField]
516	instr.op(instr, state, noValue)
517}
518
519// decodeArrayHelper does the work for decoding arrays and slices.
520func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) {
521	if helper != nil && helper(state, value, length, ovfl) {
522		return
523	}
524	instr := &decInstr{elemOp, 0, nil, ovfl}
525	isPtr := value.Type().Elem().Kind() == reflect.Ptr
526	for i := 0; i < length; i++ {
527		if state.b.Len() == 0 {
528			errorf("decoding array or slice: length exceeds input size (%d elements)", length)
529		}
530		v := value.Index(i)
531		if isPtr {
532			v = decAlloc(v)
533		}
534		elemOp(instr, state, v)
535	}
536}
537
538// decodeArray decodes an array and stores it in value.
539// The length is an unsigned integer preceding the elements.  Even though the length is redundant
540// (it's part of the type), it's a useful check and is included in the encoding.
541func (dec *Decoder) decodeArray(atyp reflect.Type, state *decoderState, value reflect.Value, elemOp decOp, length int, ovfl error, helper decHelper) {
542	if n := state.decodeUint(); n != uint64(length) {
543		errorf("length mismatch in decodeArray")
544	}
545	dec.decodeArrayHelper(state, value, elemOp, length, ovfl, helper)
546}
547
548// decodeIntoValue is a helper for map decoding.
549func decodeIntoValue(state *decoderState, op decOp, isPtr bool, value reflect.Value, ovfl error) reflect.Value {
550	instr := &decInstr{op, 0, nil, ovfl}
551	v := value
552	if isPtr {
553		v = decAlloc(value)
554	}
555	op(instr, state, v)
556	return value
557}
558
559// decodeMap decodes a map and stores it in value.
560// Maps are encoded as a length followed by key:value pairs.
561// Because the internals of maps are not visible to us, we must
562// use reflection rather than pointer magic.
563func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value reflect.Value, keyOp, elemOp decOp, ovfl error) {
564	if value.IsNil() {
565		// Allocate map.
566		value.Set(reflect.MakeMap(mtyp))
567	}
568	n := int(state.decodeUint())
569	keyIsPtr := mtyp.Key().Kind() == reflect.Ptr
570	elemIsPtr := mtyp.Elem().Kind() == reflect.Ptr
571	for i := 0; i < n; i++ {
572		key := decodeIntoValue(state, keyOp, keyIsPtr, allocValue(mtyp.Key()), ovfl)
573		elem := decodeIntoValue(state, elemOp, elemIsPtr, allocValue(mtyp.Elem()), ovfl)
574		value.SetMapIndex(key, elem)
575	}
576}
577
578// ignoreArrayHelper does the work for discarding arrays and slices.
579func (dec *Decoder) ignoreArrayHelper(state *decoderState, elemOp decOp, length int) {
580	instr := &decInstr{elemOp, 0, nil, errors.New("no error")}
581	for i := 0; i < length; i++ {
582		if state.b.Len() == 0 {
583			errorf("decoding array or slice: length exceeds input size (%d elements)", length)
584		}
585		elemOp(instr, state, noValue)
586	}
587}
588
589// ignoreArray discards the data for an array value with no destination.
590func (dec *Decoder) ignoreArray(state *decoderState, elemOp decOp, length int) {
591	if n := state.decodeUint(); n != uint64(length) {
592		errorf("length mismatch in ignoreArray")
593	}
594	dec.ignoreArrayHelper(state, elemOp, length)
595}
596
597// ignoreMap discards the data for a map value with no destination.
598func (dec *Decoder) ignoreMap(state *decoderState, keyOp, elemOp decOp) {
599	n := int(state.decodeUint())
600	keyInstr := &decInstr{keyOp, 0, nil, errors.New("no error")}
601	elemInstr := &decInstr{elemOp, 0, nil, errors.New("no error")}
602	for i := 0; i < n; i++ {
603		keyOp(keyInstr, state, noValue)
604		elemOp(elemInstr, state, noValue)
605	}
606}
607
608// decodeSlice decodes a slice and stores it in value.
609// Slices are encoded as an unsigned length followed by the elements.
610func (dec *Decoder) decodeSlice(state *decoderState, value reflect.Value, elemOp decOp, ovfl error, helper decHelper) {
611	u := state.decodeUint()
612	typ := value.Type()
613	size := uint64(typ.Elem().Size())
614	nBytes := u * size
615	n := int(u)
616	// Take care with overflow in this calculation.
617	if n < 0 || uint64(n) != u || nBytes > tooBig || (size > 0 && nBytes/size != u) {
618		// We don't check n against buffer length here because if it's a slice
619		// of interfaces, there will be buffer reloads.
620		errorf("%s slice too big: %d elements of %d bytes", typ.Elem(), u, size)
621	}
622	if value.Cap() < n {
623		value.Set(reflect.MakeSlice(typ, n, n))
624	} else {
625		value.Set(value.Slice(0, n))
626	}
627	dec.decodeArrayHelper(state, value, elemOp, n, ovfl, helper)
628}
629
630// ignoreSlice skips over the data for a slice value with no destination.
631func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) {
632	dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint()))
633}
634
635// decodeInterface decodes an interface value and stores it in value.
636// Interfaces are encoded as the name of a concrete type followed by a value.
637// If the name is empty, the value is nil and no value is sent.
638func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, value reflect.Value) {
639	// Read the name of the concrete type.
640	nr := state.decodeUint()
641	if nr > 1<<31 { // zero is permissible for anonymous types
642		errorf("invalid type name length %d", nr)
643	}
644	if nr > uint64(state.b.Len()) {
645		errorf("invalid type name length %d: exceeds input size", nr)
646	}
647	n := int(nr)
648	name := string(state.b.Bytes()[:n])
649	state.b.Drop(n)
650	// Allocate the destination interface value.
651	if name == "" {
652		// Copy the nil interface value to the target.
653		value.Set(reflect.Zero(value.Type()))
654		return
655	}
656	if len(name) > 1024 {
657		errorf("name too long (%d bytes): %.20q...", len(name), name)
658	}
659	// The concrete type must be registered.
660	registerLock.RLock()
661	typ, ok := nameToConcreteType[name]
662	registerLock.RUnlock()
663	if !ok {
664		errorf("name not registered for interface: %q", name)
665	}
666	// Read the type id of the concrete value.
667	concreteId := dec.decodeTypeSequence(true)
668	if concreteId < 0 {
669		error_(dec.err)
670	}
671	// Byte count of value is next; we don't care what it is (it's there
672	// in case we want to ignore the value by skipping it completely).
673	state.decodeUint()
674	// Read the concrete value.
675	v := allocValue(typ)
676	dec.decodeValue(concreteId, v)
677	if dec.err != nil {
678		error_(dec.err)
679	}
680	// Assign the concrete value to the interface.
681	// Tread carefully; it might not satisfy the interface.
682	if !typ.AssignableTo(ityp) {
683		errorf("%s is not assignable to type %s", typ, ityp)
684	}
685	// Copy the interface value to the target.
686	value.Set(v)
687}
688
689// ignoreInterface discards the data for an interface value with no destination.
690func (dec *Decoder) ignoreInterface(state *decoderState) {
691	// Read the name of the concrete type.
692	n, ok := state.getLength()
693	if !ok {
694		errorf("bad interface encoding: name too large for buffer")
695	}
696	bn := state.b.Len()
697	if bn < n {
698		errorf("invalid interface value length %d: exceeds input size %d", n, bn)
699	}
700	state.b.Drop(n)
701	id := dec.decodeTypeSequence(true)
702	if id < 0 {
703		error_(dec.err)
704	}
705	// At this point, the decoder buffer contains a delimited value. Just toss it.
706	n, ok = state.getLength()
707	if !ok {
708		errorf("bad interface encoding: data length too large for buffer")
709	}
710	state.b.Drop(n)
711}
712
713// decodeGobDecoder decodes something implementing the GobDecoder interface.
714// The data is encoded as a byte slice.
715func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, value reflect.Value) {
716	// Read the bytes for the value.
717	n, ok := state.getLength()
718	if !ok {
719		errorf("GobDecoder: length too large for buffer")
720	}
721	b := state.b.Bytes()
722	if len(b) < n {
723		errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, len(b))
724	}
725	b = b[:n]
726	state.b.Drop(n)
727	var err error
728	// We know it's one of these.
729	switch ut.externalDec {
730	case xGob:
731		err = value.Interface().(GobDecoder).GobDecode(b)
732	case xBinary:
733		err = value.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b)
734	case xText:
735		err = value.Interface().(encoding.TextUnmarshaler).UnmarshalText(b)
736	}
737	if err != nil {
738		error_(err)
739	}
740}
741
742// ignoreGobDecoder discards the data for a GobDecoder value with no destination.
743func (dec *Decoder) ignoreGobDecoder(state *decoderState) {
744	// Read the bytes for the value.
745	n, ok := state.getLength()
746	if !ok {
747		errorf("GobDecoder: length too large for buffer")
748	}
749	bn := state.b.Len()
750	if bn < n {
751		errorf("GobDecoder: invalid data length %d: exceeds input size %d", n, bn)
752	}
753	state.b.Drop(n)
754}
755
756// Index by Go types.
757var decOpTable = [...]decOp{
758	reflect.Bool:       decBool,
759	reflect.Int8:       decInt8,
760	reflect.Int16:      decInt16,
761	reflect.Int32:      decInt32,
762	reflect.Int64:      decInt64,
763	reflect.Uint8:      decUint8,
764	reflect.Uint16:     decUint16,
765	reflect.Uint32:     decUint32,
766	reflect.Uint64:     decUint64,
767	reflect.Float32:    decFloat32,
768	reflect.Float64:    decFloat64,
769	reflect.Complex64:  decComplex64,
770	reflect.Complex128: decComplex128,
771	reflect.String:     decString,
772}
773
774// Indexed by gob types.  tComplex will be added during type.init().
775var decIgnoreOpMap = map[typeId]decOp{
776	tBool:    ignoreUint,
777	tInt:     ignoreUint,
778	tUint:    ignoreUint,
779	tFloat:   ignoreUint,
780	tBytes:   ignoreUint8Array,
781	tString:  ignoreUint8Array,
782	tComplex: ignoreTwoUints,
783}
784
785// decOpFor returns the decoding op for the base type under rt and
786// the indirection count to reach it.
787func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) *decOp {
788	ut := userType(rt)
789	// If the type implements GobEncoder, we handle it without further processing.
790	if ut.externalDec != 0 {
791		return dec.gobDecodeOpFor(ut)
792	}
793
794	// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
795	// Return the pointer to the op we're already building.
796	if opPtr := inProgress[rt]; opPtr != nil {
797		return opPtr
798	}
799	typ := ut.base
800	var op decOp
801	k := typ.Kind()
802	if int(k) < len(decOpTable) {
803		op = decOpTable[k]
804	}
805	if op == nil {
806		inProgress[rt] = &op
807		// Special cases
808		switch t := typ; t.Kind() {
809		case reflect.Array:
810			name = "element of " + name
811			elemId := dec.wireType[wireId].ArrayT.Elem
812			elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress)
813			ovfl := overflow(name)
814			helper := decArrayHelper[t.Elem().Kind()]
815			op = func(i *decInstr, state *decoderState, value reflect.Value) {
816				state.dec.decodeArray(t, state, value, *elemOp, t.Len(), ovfl, helper)
817			}
818
819		case reflect.Map:
820			keyId := dec.wireType[wireId].MapT.Key
821			elemId := dec.wireType[wireId].MapT.Elem
822			keyOp := dec.decOpFor(keyId, t.Key(), "key of "+name, inProgress)
823			elemOp := dec.decOpFor(elemId, t.Elem(), "element of "+name, inProgress)
824			ovfl := overflow(name)
825			op = func(i *decInstr, state *decoderState, value reflect.Value) {
826				state.dec.decodeMap(t, state, value, *keyOp, *elemOp, ovfl)
827			}
828
829		case reflect.Slice:
830			name = "element of " + name
831			if t.Elem().Kind() == reflect.Uint8 {
832				op = decUint8Slice
833				break
834			}
835			var elemId typeId
836			if tt, ok := builtinIdToType[wireId]; ok {
837				elemId = tt.(*sliceType).Elem
838			} else {
839				elemId = dec.wireType[wireId].SliceT.Elem
840			}
841			elemOp := dec.decOpFor(elemId, t.Elem(), name, inProgress)
842			ovfl := overflow(name)
843			helper := decSliceHelper[t.Elem().Kind()]
844			op = func(i *decInstr, state *decoderState, value reflect.Value) {
845				state.dec.decodeSlice(state, value, *elemOp, ovfl, helper)
846			}
847
848		case reflect.Struct:
849			// Generate a closure that calls out to the engine for the nested type.
850			ut := userType(typ)
851			enginePtr, err := dec.getDecEnginePtr(wireId, ut)
852			if err != nil {
853				error_(err)
854			}
855			op = func(i *decInstr, state *decoderState, value reflect.Value) {
856				// indirect through enginePtr to delay evaluation for recursive structs.
857				dec.decodeStruct(*enginePtr, ut, value)
858			}
859		case reflect.Interface:
860			op = func(i *decInstr, state *decoderState, value reflect.Value) {
861				state.dec.decodeInterface(t, state, value)
862			}
863		}
864	}
865	if op == nil {
866		errorf("decode can't handle type %s", rt)
867	}
868	return &op
869}
870
871// decIgnoreOpFor returns the decoding op for a field that has no destination.
872func (dec *Decoder) decIgnoreOpFor(wireId typeId, inProgress map[typeId]*decOp) *decOp {
873	// If this type is already in progress, it's a recursive type (e.g. map[string]*T).
874	// Return the pointer to the op we're already building.
875	if opPtr := inProgress[wireId]; opPtr != nil {
876		return opPtr
877	}
878	op, ok := decIgnoreOpMap[wireId]
879	if !ok {
880		inProgress[wireId] = &op
881		if wireId == tInterface {
882			// Special case because it's a method: the ignored item might
883			// define types and we need to record their state in the decoder.
884			op = func(i *decInstr, state *decoderState, value reflect.Value) {
885				state.dec.ignoreInterface(state)
886			}
887			return &op
888		}
889		// Special cases
890		wire := dec.wireType[wireId]
891		switch {
892		case wire == nil:
893			errorf("bad data: undefined type %s", wireId.string())
894		case wire.ArrayT != nil:
895			elemId := wire.ArrayT.Elem
896			elemOp := dec.decIgnoreOpFor(elemId, inProgress)
897			op = func(i *decInstr, state *decoderState, value reflect.Value) {
898				state.dec.ignoreArray(state, *elemOp, wire.ArrayT.Len)
899			}
900
901		case wire.MapT != nil:
902			keyId := dec.wireType[wireId].MapT.Key
903			elemId := dec.wireType[wireId].MapT.Elem
904			keyOp := dec.decIgnoreOpFor(keyId, inProgress)
905			elemOp := dec.decIgnoreOpFor(elemId, inProgress)
906			op = func(i *decInstr, state *decoderState, value reflect.Value) {
907				state.dec.ignoreMap(state, *keyOp, *elemOp)
908			}
909
910		case wire.SliceT != nil:
911			elemId := wire.SliceT.Elem
912			elemOp := dec.decIgnoreOpFor(elemId, inProgress)
913			op = func(i *decInstr, state *decoderState, value reflect.Value) {
914				state.dec.ignoreSlice(state, *elemOp)
915			}
916
917		case wire.StructT != nil:
918			// Generate a closure that calls out to the engine for the nested type.
919			enginePtr, err := dec.getIgnoreEnginePtr(wireId)
920			if err != nil {
921				error_(err)
922			}
923			op = func(i *decInstr, state *decoderState, value reflect.Value) {
924				// indirect through enginePtr to delay evaluation for recursive structs
925				state.dec.ignoreStruct(*enginePtr)
926			}
927
928		case wire.GobEncoderT != nil, wire.BinaryMarshalerT != nil, wire.TextMarshalerT != nil:
929			op = func(i *decInstr, state *decoderState, value reflect.Value) {
930				state.dec.ignoreGobDecoder(state)
931			}
932		}
933	}
934	if op == nil {
935		errorf("bad data: ignore can't handle type %s", wireId.string())
936	}
937	return &op
938}
939
940// gobDecodeOpFor returns the op for a type that is known to implement
941// GobDecoder.
942func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp {
943	rcvrType := ut.user
944	if ut.decIndir == -1 {
945		rcvrType = reflect.PtrTo(rcvrType)
946	} else if ut.decIndir > 0 {
947		for i := int8(0); i < ut.decIndir; i++ {
948			rcvrType = rcvrType.Elem()
949		}
950	}
951	var op decOp
952	op = func(i *decInstr, state *decoderState, value reflect.Value) {
953		// We now have the base type. We need its address if the receiver is a pointer.
954		if value.Kind() != reflect.Ptr && rcvrType.Kind() == reflect.Ptr {
955			value = value.Addr()
956		}
957		state.dec.decodeGobDecoder(ut, state, value)
958	}
959	return &op
960}
961
962// compatibleType asks: Are these two gob Types compatible?
963// Answers the question for basic types, arrays, maps and slices, plus
964// GobEncoder/Decoder pairs.
965// Structs are considered ok; fields will be checked later.
966func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool {
967	if rhs, ok := inProgress[fr]; ok {
968		return rhs == fw
969	}
970	inProgress[fr] = fw
971	ut := userType(fr)
972	wire, ok := dec.wireType[fw]
973	// If wire was encoded with an encoding method, fr must have that method.
974	// And if not, it must not.
975	// At most one of the booleans in ut is set.
976	// We could possibly relax this constraint in the future in order to
977	// choose the decoding method using the data in the wireType.
978	// The parentheses look odd but are correct.
979	if (ut.externalDec == xGob) != (ok && wire.GobEncoderT != nil) ||
980		(ut.externalDec == xBinary) != (ok && wire.BinaryMarshalerT != nil) ||
981		(ut.externalDec == xText) != (ok && wire.TextMarshalerT != nil) {
982		return false
983	}
984	if ut.externalDec != 0 { // This test trumps all others.
985		return true
986	}
987	switch t := ut.base; t.Kind() {
988	default:
989		// chan, etc: cannot handle.
990		return false
991	case reflect.Bool:
992		return fw == tBool
993	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
994		return fw == tInt
995	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
996		return fw == tUint
997	case reflect.Float32, reflect.Float64:
998		return fw == tFloat
999	case reflect.Complex64, reflect.Complex128:
1000		return fw == tComplex
1001	case reflect.String:
1002		return fw == tString
1003	case reflect.Interface:
1004		return fw == tInterface
1005	case reflect.Array:
1006		if !ok || wire.ArrayT == nil {
1007			return false
1008		}
1009		array := wire.ArrayT
1010		return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress)
1011	case reflect.Map:
1012		if !ok || wire.MapT == nil {
1013			return false
1014		}
1015		MapType := wire.MapT
1016		return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress)
1017	case reflect.Slice:
1018		// Is it an array of bytes?
1019		if t.Elem().Kind() == reflect.Uint8 {
1020			return fw == tBytes
1021		}
1022		// Extract and compare element types.
1023		var sw *sliceType
1024		if tt, ok := builtinIdToType[fw]; ok {
1025			sw, _ = tt.(*sliceType)
1026		} else if wire != nil {
1027			sw = wire.SliceT
1028		}
1029		elem := userType(t.Elem()).base
1030		return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress)
1031	case reflect.Struct:
1032		return true
1033	}
1034}
1035
1036// typeString returns a human-readable description of the type identified by remoteId.
1037func (dec *Decoder) typeString(remoteId typeId) string {
1038	if t := idToType[remoteId]; t != nil {
1039		// globally known type.
1040		return t.string()
1041	}
1042	return dec.wireType[remoteId].string()
1043}
1044
1045// compileSingle compiles the decoder engine for a non-struct top-level value, including
1046// GobDecoders.
1047func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
1048	rt := ut.user
1049	engine = new(decEngine)
1050	engine.instr = make([]decInstr, 1) // one item
1051	name := rt.String()                // best we can do
1052	if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) {
1053		remoteType := dec.typeString(remoteId)
1054		// Common confusing case: local interface type, remote concrete type.
1055		if ut.base.Kind() == reflect.Interface && remoteId != tInterface {
1056			return nil, errors.New("gob: local interface type " + name + " can only be decoded from remote interface type; received concrete type " + remoteType)
1057		}
1058		return nil, errors.New("gob: decoding into local type " + name + ", received remote type " + remoteType)
1059	}
1060	op := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp))
1061	ovfl := errors.New(`value for "` + name + `" out of range`)
1062	engine.instr[singletonField] = decInstr{*op, singletonField, nil, ovfl}
1063	engine.numInstr = 1
1064	return
1065}
1066
1067// compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
1068func (dec *Decoder) compileIgnoreSingle(remoteId typeId) (engine *decEngine, err error) {
1069	engine = new(decEngine)
1070	engine.instr = make([]decInstr, 1) // one item
1071	op := dec.decIgnoreOpFor(remoteId, make(map[typeId]*decOp))
1072	ovfl := overflow(dec.typeString(remoteId))
1073	engine.instr[0] = decInstr{*op, 0, nil, ovfl}
1074	engine.numInstr = 1
1075	return
1076}
1077
1078// compileDec compiles the decoder engine for a value.  If the value is not a struct,
1079// it calls out to compileSingle.
1080func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err error) {
1081	defer catchError(&err)
1082	rt := ut.base
1083	srt := rt
1084	if srt.Kind() != reflect.Struct || ut.externalDec != 0 {
1085		return dec.compileSingle(remoteId, ut)
1086	}
1087	var wireStruct *structType
1088	// Builtin types can come from global pool; the rest must be defined by the decoder.
1089	// Also we know we're decoding a struct now, so the client must have sent one.
1090	if t, ok := builtinIdToType[remoteId]; ok {
1091		wireStruct, _ = t.(*structType)
1092	} else {
1093		wire := dec.wireType[remoteId]
1094		if wire == nil {
1095			error_(errBadType)
1096		}
1097		wireStruct = wire.StructT
1098	}
1099	if wireStruct == nil {
1100		errorf("type mismatch in decoder: want struct type %s; got non-struct", rt)
1101	}
1102	engine = new(decEngine)
1103	engine.instr = make([]decInstr, len(wireStruct.Field))
1104	seen := make(map[reflect.Type]*decOp)
1105	// Loop over the fields of the wire type.
1106	for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ {
1107		wireField := wireStruct.Field[fieldnum]
1108		if wireField.Name == "" {
1109			errorf("empty name for remote field of type %s", wireStruct.Name)
1110		}
1111		ovfl := overflow(wireField.Name)
1112		// Find the field of the local type with the same name.
1113		localField, present := srt.FieldByName(wireField.Name)
1114		// TODO(r): anonymous names
1115		if !present || !isExported(wireField.Name) {
1116			op := dec.decIgnoreOpFor(wireField.Id, make(map[typeId]*decOp))
1117			engine.instr[fieldnum] = decInstr{*op, fieldnum, nil, ovfl}
1118			continue
1119		}
1120		if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) {
1121			errorf("wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name)
1122		}
1123		op := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen)
1124		engine.instr[fieldnum] = decInstr{*op, fieldnum, localField.Index, ovfl}
1125		engine.numInstr++
1126	}
1127	return
1128}
1129
1130// getDecEnginePtr returns the engine for the specified type.
1131func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err error) {
1132	rt := ut.user
1133	decoderMap, ok := dec.decoderCache[rt]
1134	if !ok {
1135		decoderMap = make(map[typeId]**decEngine)
1136		dec.decoderCache[rt] = decoderMap
1137	}
1138	if enginePtr, ok = decoderMap[remoteId]; !ok {
1139		// To handle recursive types, mark this engine as underway before compiling.
1140		enginePtr = new(*decEngine)
1141		decoderMap[remoteId] = enginePtr
1142		*enginePtr, err = dec.compileDec(remoteId, ut)
1143		if err != nil {
1144			delete(decoderMap, remoteId)
1145		}
1146	}
1147	return
1148}
1149
1150// emptyStruct is the type we compile into when ignoring a struct value.
1151type emptyStruct struct{}
1152
1153var emptyStructType = reflect.TypeOf(emptyStruct{})
1154
1155// getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded.
1156func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) {
1157	var ok bool
1158	if enginePtr, ok = dec.ignorerCache[wireId]; !ok {
1159		// To handle recursive types, mark this engine as underway before compiling.
1160		enginePtr = new(*decEngine)
1161		dec.ignorerCache[wireId] = enginePtr
1162		wire := dec.wireType[wireId]
1163		if wire != nil && wire.StructT != nil {
1164			*enginePtr, err = dec.compileDec(wireId, userType(emptyStructType))
1165		} else {
1166			*enginePtr, err = dec.compileIgnoreSingle(wireId)
1167		}
1168		if err != nil {
1169			delete(dec.ignorerCache, wireId)
1170		}
1171	}
1172	return
1173}
1174
1175// decodeValue decodes the data stream representing a value and stores it in value.
1176func (dec *Decoder) decodeValue(wireId typeId, value reflect.Value) {
1177	defer catchError(&dec.err)
1178	// If the value is nil, it means we should just ignore this item.
1179	if !value.IsValid() {
1180		dec.decodeIgnoredValue(wireId)
1181		return
1182	}
1183	// Dereference down to the underlying type.
1184	ut := userType(value.Type())
1185	base := ut.base
1186	var enginePtr **decEngine
1187	enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut)
1188	if dec.err != nil {
1189		return
1190	}
1191	value = decAlloc(value)
1192	engine := *enginePtr
1193	if st := base; st.Kind() == reflect.Struct && ut.externalDec == 0 {
1194		wt := dec.wireType[wireId]
1195		if engine.numInstr == 0 && st.NumField() > 0 &&
1196			wt != nil && len(wt.StructT.Field) > 0 {
1197			name := base.Name()
1198			errorf("type mismatch: no fields matched compiling decoder for %s", name)
1199		}
1200		dec.decodeStruct(engine, ut, value)
1201	} else {
1202		dec.decodeSingle(engine, ut, value)
1203	}
1204}
1205
1206// decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it.
1207func (dec *Decoder) decodeIgnoredValue(wireId typeId) {
1208	var enginePtr **decEngine
1209	enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId)
1210	if dec.err != nil {
1211		return
1212	}
1213	wire := dec.wireType[wireId]
1214	if wire != nil && wire.StructT != nil {
1215		dec.ignoreStruct(*enginePtr)
1216	} else {
1217		dec.ignoreSingle(*enginePtr)
1218	}
1219}
1220
1221func init() {
1222	var iop, uop decOp
1223	switch reflect.TypeOf(int(0)).Bits() {
1224	case 32:
1225		iop = decInt32
1226		uop = decUint32
1227	case 64:
1228		iop = decInt64
1229		uop = decUint64
1230	default:
1231		panic("gob: unknown size of int/uint")
1232	}
1233	decOpTable[reflect.Int] = iop
1234	decOpTable[reflect.Uint] = uop
1235
1236	// Finally uintptr
1237	switch reflect.TypeOf(uintptr(0)).Bits() {
1238	case 32:
1239		uop = decUint32
1240	case 64:
1241		uop = decUint64
1242	default:
1243		panic("gob: unknown size of uintptr")
1244	}
1245	decOpTable[reflect.Uintptr] = uop
1246}
1247
1248// Gob depends on being able to take the address
1249// of zeroed Values it creates, so use this wrapper instead
1250// of the standard reflect.Zero.
1251// Each call allocates once.
1252func allocValue(t reflect.Type) reflect.Value {
1253	return reflect.New(t).Elem()
1254}
1255