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.
7 // TODO(rsc): When garbage collector changes, revisit
8 // the allocations in this file that use unsafe.Pointer.
20 errBadUint = os.ErrorString("gob: encoded unsigned integer out of range")
21 errBadType = os.ErrorString("gob: unknown type id or corrupted data")
22 errRange = os.ErrorString("gob: bad data: field numbers out of bounds")
25 // decoderState is the execution state of an instance of the decoder. A new state
26 // is created for nested objects.
27 type decoderState struct {
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).
32 fieldnum int // the last field number read.
34 next *decoderState // for free list
37 // We pass the bytes.Buffer separately for easier testing of the infrastructure
38 // without requiring a full Decoder.
39 func (dec *Decoder) newDecoderState(buf *bytes.Buffer) *decoderState {
44 d.buf = make([]byte, uint64Size)
52 func (dec *Decoder) freeDecoderState(d *decoderState) {
57 func overflow(name string) os.ErrorString {
58 return os.ErrorString(`value for "` + name + `" out of range`)
61 // decodeUintReader reads an encoded unsigned integer from an io.Reader.
62 // Used only by the Decoder to read the message length.
63 func decodeUintReader(r io.Reader, buf []byte) (x uint64, width int, err os.Error) {
65 _, err = r.Read(buf[0:width])
71 return uint64(b), width, nil
79 n, err = io.ReadFull(r, buf[0:nb])
82 err = io.ErrUnexpectedEOF
86 // Could check that the high byte is zero but it's not worth it.
87 for i := 0; i < n; i++ {
95 // decodeUint reads an encoded unsigned integer from state.r.
96 // Does not check for overflow.
97 func (state *decoderState) decodeUint() (x uint64) {
98 b, err := state.b.ReadByte()
109 n, err := state.b.Read(state.buf[0:nb])
113 // Don't need to check error; it's safe to loop regardless.
114 // Could check that the high byte is zero but it's not worth it.
115 for i := 0; i < n; i++ {
117 x |= uint64(state.buf[i])
122 // decodeInt reads an encoded signed integer from state.r.
123 // Does not check for overflow.
124 func (state *decoderState) decodeInt() int64 {
125 x := state.decodeUint()
127 return ^int64(x >> 1)
132 // decOp is the signature of a decoding operator for a given type.
133 type decOp func(i *decInstr, state *decoderState, p unsafe.Pointer)
135 // The 'instructions' of the decoding machine
136 type decInstr struct {
138 field int // field number of the wire type
139 indir int // how many pointer indirections to reach the value in the struct
140 offset uintptr // offset in the structure of the field to encode
141 ovfl os.ErrorString // error message for overflow/underflow (for arrays, of the elements)
144 // Since the encoder writes no zeros, if we arrive at a decoder we have
145 // a value to extract and store. The field number has already been read
146 // (it's how we knew to call this decoder).
147 // Each decoder is responsible for handling any indirections associated
148 // with the data structure. If any pointer so reached is nil, allocation must
151 // Walk the pointer hierarchy, allocating if we find a nil. Stop one before the end.
152 func decIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
153 for ; indir > 1; indir-- {
154 if *(*unsafe.Pointer)(p) == nil {
155 // Allocation required
156 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(unsafe.Pointer))
158 p = *(*unsafe.Pointer)(p)
163 // ignoreUint discards a uint value with no destination.
164 func ignoreUint(i *decInstr, state *decoderState, p unsafe.Pointer) {
168 // ignoreTwoUints discards a uint value with no destination. It's used to skip
170 func ignoreTwoUints(i *decInstr, state *decoderState, p unsafe.Pointer) {
175 // decBool decodes a uiint and stores it as a boolean through p.
176 func decBool(i *decInstr, state *decoderState, p unsafe.Pointer) {
178 if *(*unsafe.Pointer)(p) == nil {
179 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(bool))
181 p = *(*unsafe.Pointer)(p)
183 *(*bool)(p) = state.decodeUint() != 0
186 // decInt8 decodes an integer and stores it as an int8 through p.
187 func decInt8(i *decInstr, state *decoderState, p unsafe.Pointer) {
189 if *(*unsafe.Pointer)(p) == nil {
190 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int8))
192 p = *(*unsafe.Pointer)(p)
194 v := state.decodeInt()
195 if v < math.MinInt8 || math.MaxInt8 < v {
198 *(*int8)(p) = int8(v)
202 // decUint8 decodes an unsigned integer and stores it as a uint8 through p.
203 func decUint8(i *decInstr, state *decoderState, p unsafe.Pointer) {
205 if *(*unsafe.Pointer)(p) == nil {
206 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint8))
208 p = *(*unsafe.Pointer)(p)
210 v := state.decodeUint()
211 if math.MaxUint8 < v {
214 *(*uint8)(p) = uint8(v)
218 // decInt16 decodes an integer and stores it as an int16 through p.
219 func decInt16(i *decInstr, state *decoderState, p unsafe.Pointer) {
221 if *(*unsafe.Pointer)(p) == nil {
222 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int16))
224 p = *(*unsafe.Pointer)(p)
226 v := state.decodeInt()
227 if v < math.MinInt16 || math.MaxInt16 < v {
230 *(*int16)(p) = int16(v)
234 // decUint16 decodes an unsigned integer and stores it as a uint16 through p.
235 func decUint16(i *decInstr, state *decoderState, p unsafe.Pointer) {
237 if *(*unsafe.Pointer)(p) == nil {
238 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint16))
240 p = *(*unsafe.Pointer)(p)
242 v := state.decodeUint()
243 if math.MaxUint16 < v {
246 *(*uint16)(p) = uint16(v)
250 // decInt32 decodes an integer and stores it as an int32 through p.
251 func decInt32(i *decInstr, state *decoderState, p unsafe.Pointer) {
253 if *(*unsafe.Pointer)(p) == nil {
254 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int32))
256 p = *(*unsafe.Pointer)(p)
258 v := state.decodeInt()
259 if v < math.MinInt32 || math.MaxInt32 < v {
262 *(*int32)(p) = int32(v)
266 // decUint32 decodes an unsigned integer and stores it as a uint32 through p.
267 func decUint32(i *decInstr, state *decoderState, p unsafe.Pointer) {
269 if *(*unsafe.Pointer)(p) == nil {
270 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint32))
272 p = *(*unsafe.Pointer)(p)
274 v := state.decodeUint()
275 if math.MaxUint32 < v {
278 *(*uint32)(p) = uint32(v)
282 // decInt64 decodes an integer and stores it as an int64 through p.
283 func decInt64(i *decInstr, state *decoderState, p unsafe.Pointer) {
285 if *(*unsafe.Pointer)(p) == nil {
286 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(int64))
288 p = *(*unsafe.Pointer)(p)
290 *(*int64)(p) = int64(state.decodeInt())
293 // decUint64 decodes an unsigned integer and stores it as a uint64 through p.
294 func decUint64(i *decInstr, state *decoderState, p unsafe.Pointer) {
296 if *(*unsafe.Pointer)(p) == nil {
297 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint64))
299 p = *(*unsafe.Pointer)(p)
301 *(*uint64)(p) = uint64(state.decodeUint())
304 // Floating-point numbers are transmitted as uint64s holding the bits
305 // of the underlying representation. They are sent byte-reversed, with
306 // the exponent end coming out first, so integer floating point numbers
307 // (for example) transmit more compactly. This routine does the
309 func floatFromBits(u uint64) float64 {
311 for i := 0; i < 8; i++ {
316 return math.Float64frombits(v)
319 // storeFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
320 // number, and stores it through p. It's a helper function for float32 and complex64.
321 func storeFloat32(i *decInstr, state *decoderState, p unsafe.Pointer) {
322 v := floatFromBits(state.decodeUint())
327 // +Inf is OK in both 32- and 64-bit floats. Underflow is always OK.
328 if math.MaxFloat32 < av && av <= math.MaxFloat64 {
331 *(*float32)(p) = float32(v)
335 // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
336 // number, and stores it through p.
337 func decFloat32(i *decInstr, state *decoderState, p unsafe.Pointer) {
339 if *(*unsafe.Pointer)(p) == nil {
340 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(float32))
342 p = *(*unsafe.Pointer)(p)
344 storeFloat32(i, state, p)
347 // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point
348 // number, and stores it through p.
349 func decFloat64(i *decInstr, state *decoderState, p unsafe.Pointer) {
351 if *(*unsafe.Pointer)(p) == nil {
352 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(float64))
354 p = *(*unsafe.Pointer)(p)
356 *(*float64)(p) = floatFromBits(uint64(state.decodeUint()))
359 // decComplex64 decodes a pair of unsigned integers, treats them as a
360 // pair of floating point numbers, and stores them as a complex64 through p.
361 // The real part comes first.
362 func decComplex64(i *decInstr, state *decoderState, p unsafe.Pointer) {
364 if *(*unsafe.Pointer)(p) == nil {
365 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(complex64))
367 p = *(*unsafe.Pointer)(p)
369 storeFloat32(i, state, p)
370 storeFloat32(i, state, unsafe.Pointer(uintptr(p)+uintptr(unsafe.Sizeof(float32(0)))))
373 // decComplex128 decodes a pair of unsigned integers, treats them as a
374 // pair of floating point numbers, and stores them as a complex128 through p.
375 // The real part comes first.
376 func decComplex128(i *decInstr, state *decoderState, p unsafe.Pointer) {
378 if *(*unsafe.Pointer)(p) == nil {
379 *(*unsafe.Pointer)(p) = unsafe.Pointer(new(complex128))
381 p = *(*unsafe.Pointer)(p)
383 real := floatFromBits(uint64(state.decodeUint()))
384 imag := floatFromBits(uint64(state.decodeUint()))
385 *(*complex128)(p) = complex(real, imag)
388 // decUint8Array decodes byte array and stores through p a slice header
389 // describing the data.
390 // uint8 arrays are encoded as an unsigned count followed by the raw bytes.
391 func decUint8Array(i *decInstr, state *decoderState, p unsafe.Pointer) {
393 if *(*unsafe.Pointer)(p) == nil {
394 *(*unsafe.Pointer)(p) = unsafe.Pointer(new([]uint8))
396 p = *(*unsafe.Pointer)(p)
398 b := make([]uint8, state.decodeUint())
403 // decString decodes byte array and stores through p a string header
404 // describing the data.
405 // Strings are encoded as an unsigned count followed by the raw bytes.
406 func decString(i *decInstr, state *decoderState, p unsafe.Pointer) {
408 if *(*unsafe.Pointer)(p) == nil {
409 *(*unsafe.Pointer)(p) = unsafe.Pointer(new([]byte))
411 p = *(*unsafe.Pointer)(p)
413 b := make([]byte, state.decodeUint())
415 // It would be a shame to do the obvious thing here,
416 // *(*string)(p) = string(b)
417 // because we've already allocated the storage and this would
418 // allocate again and copy. So we do this ugly hack, which is even
419 // even more unsafe than it looks as it depends the memory
420 // representation of a string matching the beginning of the memory
421 // representation of a byte slice (a byte slice is longer).
422 *(*string)(p) = *(*string)(unsafe.Pointer(&b))
425 // ignoreUint8Array skips over the data for a byte slice value with no destination.
426 func ignoreUint8Array(i *decInstr, state *decoderState, p unsafe.Pointer) {
427 b := make([]byte, state.decodeUint())
433 // The encoder engine is an array of instructions indexed by field number of the incoming
434 // decoder. It is executed with random access according to field number.
435 type decEngine struct {
437 numInstr int // the number of active instructions
440 // allocate makes sure storage is available for an object of underlying type rtyp
441 // that is indir levels of indirection through p.
442 func allocate(rtyp reflect.Type, p uintptr, indir int) uintptr {
446 up := unsafe.Pointer(p)
448 up = decIndirect(up, indir)
450 if *(*unsafe.Pointer)(up) == nil {
452 *(*unsafe.Pointer)(up) = unsafe.New(rtyp)
454 return *(*uintptr)(up)
457 // decodeSingle decodes a top-level value that is not a struct and stores it through p.
458 // Such values are preceded by a zero, making them have the memory layout of a
459 // struct field (although with an illegal field number).
460 func (dec *Decoder) decodeSingle(engine *decEngine, ut *userTypeInfo, p uintptr) (err os.Error) {
463 indir = int(ut.decIndir)
465 p = allocate(ut.base, p, indir)
466 state := dec.newDecoderState(&dec.buf)
467 state.fieldnum = singletonField
469 delta := int(state.decodeUint())
471 errorf("gob decode: corrupted data: non-zero delta for singleton")
473 instr := &engine.instr[singletonField]
474 ptr := unsafe.Pointer(basep) // offset will be zero
476 ptr = decIndirect(ptr, instr.indir)
478 instr.op(instr, state, ptr)
479 dec.freeDecoderState(state)
483 // decodeSingle decodes a top-level struct and stores it through p.
484 // Indir is for the value, not the type. At the time of the call it may
485 // differ from ut.indir, which was computed when the engine was built.
486 // This state cannot arise for decodeSingle, which is called directly
487 // from the user's value, not from the innards of an engine.
488 func (dec *Decoder) decodeStruct(engine *decEngine, ut *userTypeInfo, p uintptr, indir int) {
489 p = allocate(ut.base.(*reflect.StructType), p, indir)
490 state := dec.newDecoderState(&dec.buf)
493 for state.b.Len() > 0 {
494 delta := int(state.decodeUint())
496 errorf("gob decode: corrupted data: negative delta")
498 if delta == 0 { // struct terminator is zero delta fieldnum
501 fieldnum := state.fieldnum + delta
502 if fieldnum >= len(engine.instr) {
506 instr := &engine.instr[fieldnum]
507 p := unsafe.Pointer(basep + instr.offset)
509 p = decIndirect(p, instr.indir)
511 instr.op(instr, state, p)
512 state.fieldnum = fieldnum
514 dec.freeDecoderState(state)
517 // ignoreStruct discards the data for a struct with no destination.
518 func (dec *Decoder) ignoreStruct(engine *decEngine) {
519 state := dec.newDecoderState(&dec.buf)
521 for state.b.Len() > 0 {
522 delta := int(state.decodeUint())
524 errorf("gob ignore decode: corrupted data: negative delta")
526 if delta == 0 { // struct terminator is zero delta fieldnum
529 fieldnum := state.fieldnum + delta
530 if fieldnum >= len(engine.instr) {
533 instr := &engine.instr[fieldnum]
534 instr.op(instr, state, unsafe.Pointer(nil))
535 state.fieldnum = fieldnum
537 dec.freeDecoderState(state)
540 // ignoreSingle discards the data for a top-level non-struct value with no
541 // destination. It's used when calling Decode with a nil value.
542 func (dec *Decoder) ignoreSingle(engine *decEngine) {
543 state := dec.newDecoderState(&dec.buf)
544 state.fieldnum = singletonField
545 delta := int(state.decodeUint())
547 errorf("gob decode: corrupted data: non-zero delta for singleton")
549 instr := &engine.instr[singletonField]
550 instr.op(instr, state, unsafe.Pointer(nil))
551 dec.freeDecoderState(state)
554 // decodeArrayHelper does the work for decoding arrays and slices.
555 func (dec *Decoder) decodeArrayHelper(state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, elemIndir int, ovfl os.ErrorString) {
556 instr := &decInstr{elemOp, 0, elemIndir, 0, ovfl}
557 for i := 0; i < length; i++ {
558 up := unsafe.Pointer(p)
560 up = decIndirect(up, elemIndir)
562 elemOp(instr, state, up)
563 p += uintptr(elemWid)
567 // decodeArray decodes an array and stores it through p, that is, p points to the zeroth element.
568 // The length is an unsigned integer preceding the elements. Even though the length is redundant
569 // (it's part of the type), it's a useful check and is included in the encoding.
570 func (dec *Decoder) decodeArray(atyp *reflect.ArrayType, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, length, indir, elemIndir int, ovfl os.ErrorString) {
572 p = allocate(atyp, p, 1) // All but the last level has been allocated by dec.Indirect
574 if n := state.decodeUint(); n != uint64(length) {
575 errorf("gob: length mismatch in decodeArray")
577 dec.decodeArrayHelper(state, p, elemOp, elemWid, length, elemIndir, ovfl)
580 // decodeIntoValue is a helper for map decoding. Since maps are decoded using reflection,
581 // unlike the other items we can't use a pointer directly.
582 func decodeIntoValue(state *decoderState, op decOp, indir int, v reflect.Value, ovfl os.ErrorString) reflect.Value {
583 instr := &decInstr{op, 0, indir, 0, ovfl}
584 up := unsafe.Pointer(v.UnsafeAddr())
586 up = decIndirect(up, indir)
592 // decodeMap decodes a map and stores its header through p.
593 // Maps are encoded as a length followed by key:value pairs.
594 // Because the internals of maps are not visible to us, we must
595 // use reflection rather than pointer magic.
596 func (dec *Decoder) decodeMap(mtyp *reflect.MapType, state *decoderState, p uintptr, keyOp, elemOp decOp, indir, keyIndir, elemIndir int, ovfl os.ErrorString) {
598 p = allocate(mtyp, p, 1) // All but the last level has been allocated by dec.Indirect
600 up := unsafe.Pointer(p)
601 if *(*unsafe.Pointer)(up) == nil { // maps are represented as a pointer in the runtime
603 *(*unsafe.Pointer)(up) = unsafe.Pointer(reflect.MakeMap(mtyp).Get())
605 // Maps cannot be accessed by moving addresses around the way
606 // that slices etc. can. We must recover a full reflection value for
608 v := reflect.NewValue(unsafe.Unreflect(mtyp, unsafe.Pointer(p))).(*reflect.MapValue)
609 n := int(state.decodeUint())
610 for i := 0; i < n; i++ {
611 key := decodeIntoValue(state, keyOp, keyIndir, reflect.MakeZero(mtyp.Key()), ovfl)
612 elem := decodeIntoValue(state, elemOp, elemIndir, reflect.MakeZero(mtyp.Elem()), ovfl)
617 // ignoreArrayHelper does the work for discarding arrays and slices.
618 func (dec *Decoder) ignoreArrayHelper(state *decoderState, elemOp decOp, length int) {
619 instr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")}
620 for i := 0; i < length; i++ {
621 elemOp(instr, state, nil)
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("gob: 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, 0, 0, os.ErrorString("no error")}
637 elemInstr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")}
638 for i := 0; i < n; i++ {
639 keyOp(keyInstr, state, nil)
640 elemOp(elemInstr, state, nil)
644 // decodeSlice decodes a slice and stores the slice header through p.
645 // Slices are encoded as an unsigned length followed by the elements.
646 func (dec *Decoder) decodeSlice(atyp *reflect.SliceType, state *decoderState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.ErrorString) {
647 n := int(uintptr(state.decodeUint()))
649 up := unsafe.Pointer(p)
650 if *(*unsafe.Pointer)(up) == nil {
651 // Allocate the slice header.
652 *(*unsafe.Pointer)(up) = unsafe.Pointer(new([]unsafe.Pointer))
656 // Allocate storage for the slice elements, that is, the underlying array.
657 // Always write a header at p.
658 hdrp := (*reflect.SliceHeader)(unsafe.Pointer(p))
659 hdrp.Data = uintptr(unsafe.NewArray(atyp.Elem(), n))
662 dec.decodeArrayHelper(state, hdrp.Data, elemOp, elemWid, n, elemIndir, ovfl)
665 // ignoreSlice skips over the data for a slice value with no destination.
666 func (dec *Decoder) ignoreSlice(state *decoderState, elemOp decOp) {
667 dec.ignoreArrayHelper(state, elemOp, int(state.decodeUint()))
670 // setInterfaceValue sets an interface value to a concrete value through
671 // reflection. If the concrete value does not implement the interface, the
672 // setting will panic. This routine turns the panic into an error return.
673 // This dance avoids manually checking that the value satisfies the
675 // TODO(rsc): avoid panic+recover after fixing issue 327.
676 func setInterfaceValue(ivalue *reflect.InterfaceValue, value reflect.Value) {
678 if e := recover(); e != nil {
685 // decodeInterface decodes an interface value and stores it through p.
686 // Interfaces are encoded as the name of a concrete type followed by a value.
687 // If the name is empty, the value is nil and no value is sent.
688 func (dec *Decoder) decodeInterface(ityp *reflect.InterfaceType, state *decoderState, p uintptr, indir int) {
689 // Create an interface reflect.Value. We need one even for the nil case.
690 ivalue := reflect.MakeZero(ityp).(*reflect.InterfaceValue)
691 // Read the name of the concrete type.
692 b := make([]byte, state.decodeUint())
696 // Copy the representation of the nil interface value to the target.
697 // This is horribly unsafe and special.
698 *(*[2]uintptr)(unsafe.Pointer(p)) = ivalue.Get()
701 // The concrete type must be registered.
702 typ, ok := nameToConcreteType[name]
704 errorf("gob: name not registered for interface: %q", name)
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.
715 value := reflect.MakeZero(typ)
716 dec.decodeValue(concreteId, value)
720 // Allocate the destination interface value.
722 p = allocate(ityp, p, 1) // All but the last level has been allocated by dec.Indirect
724 // Assign the concrete value to the interface.
725 // Tread carefully; it might not satisfy the interface.
726 setInterfaceValue(ivalue, value)
727 // Copy the representation of the interface value to the target.
728 // This is horribly unsafe and special.
729 *(*[2]uintptr)(unsafe.Pointer(p)) = ivalue.Get()
732 // ignoreInterface discards the data for an interface value with no destination.
733 func (dec *Decoder) ignoreInterface(state *decoderState) {
734 // Read the name of the concrete type.
735 b := make([]byte, state.decodeUint())
736 _, err := state.b.Read(b)
740 id := dec.decodeTypeSequence(true)
744 // At this point, the decoder buffer contains a delimited value. Just toss it.
745 state.b.Next(int(state.decodeUint()))
748 // decodeGobDecoder decodes something implementing the GobDecoder interface.
749 // The data is encoded as a byte slice.
750 func (dec *Decoder) decodeGobDecoder(state *decoderState, v reflect.Value, index int) {
751 // Read the bytes for the value.
752 b := make([]byte, state.decodeUint())
753 _, err := state.b.Read(b)
757 // We know it's a GobDecoder, so just call the method directly.
758 err = v.Interface().(GobDecoder).GobDecode(b)
764 // ignoreGobDecoder discards the data for a GobDecoder value with no destination.
765 func (dec *Decoder) ignoreGobDecoder(state *decoderState) {
766 // Read the bytes for the value.
767 b := make([]byte, state.decodeUint())
768 _, err := state.b.Read(b)
774 // Index by Go types.
775 var decOpTable = [...]decOp{
776 reflect.Bool: decBool,
777 reflect.Int8: decInt8,
778 reflect.Int16: decInt16,
779 reflect.Int32: decInt32,
780 reflect.Int64: decInt64,
781 reflect.Uint8: decUint8,
782 reflect.Uint16: decUint16,
783 reflect.Uint32: decUint32,
784 reflect.Uint64: decUint64,
785 reflect.Float32: decFloat32,
786 reflect.Float64: decFloat64,
787 reflect.Complex64: decComplex64,
788 reflect.Complex128: decComplex128,
789 reflect.String: decString,
792 // Indexed by gob types. tComplex will be added during type.init().
793 var decIgnoreOpMap = map[typeId]decOp{
798 tBytes: ignoreUint8Array,
799 tString: ignoreUint8Array,
800 tComplex: ignoreTwoUints,
803 // decOpFor returns the decoding op for the base type under rt and
804 // the indirection count to reach it.
805 func (dec *Decoder) decOpFor(wireId typeId, rt reflect.Type, name string, inProgress map[reflect.Type]*decOp) (*decOp, int) {
807 // If the type implements GobEncoder, we handle it without further processing.
809 return dec.gobDecodeOpFor(ut)
811 // If this type is already in progress, it's a recursive type (e.g. map[string]*T).
812 // Return the pointer to the op we're already building.
813 if opPtr := inProgress[rt]; opPtr != nil {
814 return opPtr, ut.indir
820 if int(k) < len(decOpTable) {
826 switch t := typ.(type) {
827 case *reflect.ArrayType:
828 name = "element of " + name
829 elemId := dec.wireType[wireId].ArrayT.Elem
830 elemOp, elemIndir := dec.decOpFor(elemId, t.Elem(), name, inProgress)
831 ovfl := overflow(name)
832 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
833 state.dec.decodeArray(t, state, uintptr(p), *elemOp, t.Elem().Size(), t.Len(), i.indir, elemIndir, ovfl)
836 case *reflect.MapType:
837 name = "element of " + name
838 keyId := dec.wireType[wireId].MapT.Key
839 elemId := dec.wireType[wireId].MapT.Elem
840 keyOp, keyIndir := dec.decOpFor(keyId, t.Key(), name, inProgress)
841 elemOp, elemIndir := dec.decOpFor(elemId, t.Elem(), name, inProgress)
842 ovfl := overflow(name)
843 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
844 up := unsafe.Pointer(p)
845 state.dec.decodeMap(t, state, uintptr(up), *keyOp, *elemOp, i.indir, keyIndir, elemIndir, ovfl)
848 case *reflect.SliceType:
849 name = "element of " + name
850 if t.Elem().Kind() == reflect.Uint8 {
855 if tt, ok := builtinIdToType[wireId]; ok {
856 elemId = tt.(*sliceType).Elem
858 elemId = dec.wireType[wireId].SliceT.Elem
860 elemOp, elemIndir := dec.decOpFor(elemId, t.Elem(), name, inProgress)
861 ovfl := overflow(name)
862 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
863 state.dec.decodeSlice(t, state, uintptr(p), *elemOp, t.Elem().Size(), i.indir, elemIndir, ovfl)
866 case *reflect.StructType:
867 // Generate a closure that calls out to the engine for the nested type.
868 enginePtr, err := dec.getDecEnginePtr(wireId, userType(typ))
872 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
873 // indirect through enginePtr to delay evaluation for recursive structs.
874 dec.decodeStruct(*enginePtr, userType(typ), uintptr(p), i.indir)
876 case *reflect.InterfaceType:
877 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
878 state.dec.decodeInterface(t, state, uintptr(p), i.indir)
883 errorf("gob: decode can't handle type %s", rt.String())
888 // decIgnoreOpFor returns the decoding op for a field that has no destination.
889 func (dec *Decoder) decIgnoreOpFor(wireId typeId) decOp {
890 op, ok := decIgnoreOpMap[wireId]
892 if wireId == tInterface {
893 // Special case because it's a method: the ignored item might
894 // define types and we need to record their state in the decoder.
895 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
896 state.dec.ignoreInterface(state)
901 wire := dec.wireType[wireId]
904 errorf("gob: bad data: undefined type %s", wireId.string())
905 case wire.ArrayT != nil:
906 elemId := wire.ArrayT.Elem
907 elemOp := dec.decIgnoreOpFor(elemId)
908 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
909 state.dec.ignoreArray(state, elemOp, wire.ArrayT.Len)
912 case wire.MapT != nil:
913 keyId := dec.wireType[wireId].MapT.Key
914 elemId := dec.wireType[wireId].MapT.Elem
915 keyOp := dec.decIgnoreOpFor(keyId)
916 elemOp := dec.decIgnoreOpFor(elemId)
917 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
918 state.dec.ignoreMap(state, keyOp, elemOp)
921 case wire.SliceT != nil:
922 elemId := wire.SliceT.Elem
923 elemOp := dec.decIgnoreOpFor(elemId)
924 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
925 state.dec.ignoreSlice(state, elemOp)
928 case wire.StructT != nil:
929 // Generate a closure that calls out to the engine for the nested type.
930 enginePtr, err := dec.getIgnoreEnginePtr(wireId)
934 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
935 // indirect through enginePtr to delay evaluation for recursive structs
936 state.dec.ignoreStruct(*enginePtr)
939 case wire.GobEncoderT != nil:
940 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
941 state.dec.ignoreGobDecoder(state)
946 errorf("gob: bad data: ignore can't handle type %s", wireId.string())
951 // gobDecodeOpFor returns the op for a type that is known to implement
953 func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) (*decOp, int) {
955 if ut.decIndir == -1 {
956 rt = reflect.PtrTo(rt)
957 } else if ut.decIndir > 0 {
958 for i := int8(0); i < ut.decIndir; i++ {
959 rt = rt.(*reflect.PtrType).Elem()
963 op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
964 // Allocate the underlying data, but hold on to the address we have,
965 // since we need it to get to the receiver's address.
966 allocate(ut.base, uintptr(p), ut.indir)
968 if ut.decIndir == -1 {
969 // Need to climb up one level to turn value into pointer.
970 v = reflect.NewValue(unsafe.Unreflect(rt, unsafe.Pointer(&p)))
973 p = decIndirect(p, int(ut.decIndir))
975 v = reflect.NewValue(unsafe.Unreflect(rt, p))
977 state.dec.decodeGobDecoder(state, v, methodIndex(rt, gobDecodeMethodName))
979 return &op, int(ut.decIndir)
983 // compatibleType asks: Are these two gob Types compatible?
984 // Answers the question for basic types, arrays, maps and slices, plus
985 // GobEncoder/Decoder pairs.
986 // Structs are considered ok; fields will be checked later.
987 func (dec *Decoder) compatibleType(fr reflect.Type, fw typeId, inProgress map[reflect.Type]typeId) bool {
988 if rhs, ok := inProgress[fr]; ok {
993 wire, ok := dec.wireType[fw]
994 // If fr is a GobDecoder, the wire type must be GobEncoder.
995 // And if fr is not a GobDecoder, the wire type must not be either.
996 if ut.isGobDecoder != (ok && wire.GobEncoderT != nil) { // the parentheses look odd but are correct.
999 if ut.isGobDecoder { // This test trumps all others.
1002 switch t := ut.base.(type) {
1004 // chan, etc: cannot handle.
1006 case *reflect.BoolType:
1008 case *reflect.IntType:
1010 case *reflect.UintType:
1012 case *reflect.FloatType:
1014 case *reflect.ComplexType:
1015 return fw == tComplex
1016 case *reflect.StringType:
1017 return fw == tString
1018 case *reflect.InterfaceType:
1019 return fw == tInterface
1020 case *reflect.ArrayType:
1021 if !ok || wire.ArrayT == nil {
1024 array := wire.ArrayT
1025 return t.Len() == array.Len && dec.compatibleType(t.Elem(), array.Elem, inProgress)
1026 case *reflect.MapType:
1027 if !ok || wire.MapT == nil {
1030 MapType := wire.MapT
1031 return dec.compatibleType(t.Key(), MapType.Key, inProgress) && dec.compatibleType(t.Elem(), MapType.Elem, inProgress)
1032 case *reflect.SliceType:
1033 // Is it an array of bytes?
1034 if t.Elem().Kind() == reflect.Uint8 {
1037 // Extract and compare element types.
1039 if tt, ok := builtinIdToType[fw]; ok {
1040 sw = tt.(*sliceType)
1042 sw = dec.wireType[fw].SliceT
1044 elem := userType(t.Elem()).base
1045 return sw != nil && dec.compatibleType(elem, sw.Elem, inProgress)
1046 case *reflect.StructType:
1052 // typeString returns a human-readable description of the type identified by remoteId.
1053 func (dec *Decoder) typeString(remoteId typeId) string {
1054 if t := idToType[remoteId]; t != nil {
1055 // globally known type.
1058 return dec.wireType[remoteId].string()
1061 // compileSingle compiles the decoder engine for a non-struct top-level value, including
1063 func (dec *Decoder) compileSingle(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err os.Error) {
1065 if ut.isGobDecoder {
1068 engine = new(decEngine)
1069 engine.instr = make([]decInstr, 1) // one item
1070 name := rt.String() // best we can do
1071 if !dec.compatibleType(rt, remoteId, make(map[reflect.Type]typeId)) {
1072 return nil, os.ErrorString("gob: wrong type received for local value " + name + ": " + dec.typeString(remoteId))
1074 op, indir := dec.decOpFor(remoteId, rt, name, make(map[reflect.Type]*decOp))
1075 ovfl := os.ErrorString(`value for "` + name + `" out of range`)
1076 engine.instr[singletonField] = decInstr{*op, singletonField, indir, 0, ovfl}
1081 // compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
1082 func (dec *Decoder) compileIgnoreSingle(remoteId typeId) (engine *decEngine, err os.Error) {
1083 engine = new(decEngine)
1084 engine.instr = make([]decInstr, 1) // one item
1085 op := dec.decIgnoreOpFor(remoteId)
1086 ovfl := overflow(dec.typeString(remoteId))
1087 engine.instr[0] = decInstr{op, 0, 0, 0, ovfl}
1092 // compileDec compiles the decoder engine for a value. If the value is not a struct,
1093 // it calls out to compileSingle.
1094 func (dec *Decoder) compileDec(remoteId typeId, ut *userTypeInfo) (engine *decEngine, err os.Error) {
1096 srt, ok := rt.(*reflect.StructType)
1097 if !ok || ut.isGobDecoder {
1098 return dec.compileSingle(remoteId, ut)
1100 var wireStruct *structType
1101 // Builtin types can come from global pool; the rest must be defined by the decoder.
1102 // Also we know we're decoding a struct now, so the client must have sent one.
1103 if t, ok := builtinIdToType[remoteId]; ok {
1104 wireStruct, _ = t.(*structType)
1106 wire := dec.wireType[remoteId]
1110 wireStruct = wire.StructT
1112 if wireStruct == nil {
1113 errorf("gob: type mismatch in decoder: want struct type %s; got non-struct", rt.String())
1115 engine = new(decEngine)
1116 engine.instr = make([]decInstr, len(wireStruct.Field))
1117 seen := make(map[reflect.Type]*decOp)
1118 // Loop over the fields of the wire type.
1119 for fieldnum := 0; fieldnum < len(wireStruct.Field); fieldnum++ {
1120 wireField := wireStruct.Field[fieldnum]
1121 if wireField.Name == "" {
1122 errorf("gob: empty name for remote field of type %s", wireStruct.Name)
1124 ovfl := overflow(wireField.Name)
1125 // Find the field of the local type with the same name.
1126 localField, present := srt.FieldByName(wireField.Name)
1127 // TODO(r): anonymous names
1128 if !present || !isExported(wireField.Name) {
1129 op := dec.decIgnoreOpFor(wireField.Id)
1130 engine.instr[fieldnum] = decInstr{op, fieldnum, 0, 0, ovfl}
1133 if !dec.compatibleType(localField.Type, wireField.Id, make(map[reflect.Type]typeId)) {
1134 errorf("gob: wrong type (%s) for received field %s.%s", localField.Type, wireStruct.Name, wireField.Name)
1136 op, indir := dec.decOpFor(wireField.Id, localField.Type, localField.Name, seen)
1137 engine.instr[fieldnum] = decInstr{*op, fieldnum, indir, uintptr(localField.Offset), ovfl}
1143 // getDecEnginePtr returns the engine for the specified type.
1144 func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePtr **decEngine, err os.Error) {
1146 decoderMap, ok := dec.decoderCache[rt]
1148 decoderMap = make(map[typeId]**decEngine)
1149 dec.decoderCache[rt] = decoderMap
1151 if enginePtr, ok = decoderMap[remoteId]; !ok {
1152 // To handle recursive types, mark this engine as underway before compiling.
1153 enginePtr = new(*decEngine)
1154 decoderMap[remoteId] = enginePtr
1155 *enginePtr, err = dec.compileDec(remoteId, ut)
1157 decoderMap[remoteId] = nil, false
1163 // emptyStruct is the type we compile into when ignoring a struct value.
1164 type emptyStruct struct{}
1166 var emptyStructType = reflect.Typeof(emptyStruct{})
1168 // getDecEnginePtr returns the engine for the specified type when the value is to be discarded.
1169 func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err os.Error) {
1171 if enginePtr, ok = dec.ignorerCache[wireId]; !ok {
1172 // To handle recursive types, mark this engine as underway before compiling.
1173 enginePtr = new(*decEngine)
1174 dec.ignorerCache[wireId] = enginePtr
1175 wire := dec.wireType[wireId]
1176 if wire != nil && wire.StructT != nil {
1177 *enginePtr, err = dec.compileDec(wireId, userType(emptyStructType))
1179 *enginePtr, err = dec.compileIgnoreSingle(wireId)
1182 dec.ignorerCache[wireId] = nil, false
1188 // decodeValue decodes the data stream representing a value and stores it in val.
1189 func (dec *Decoder) decodeValue(wireId typeId, val reflect.Value) {
1190 defer catchError(&dec.err)
1191 // If the value is nil, it means we should just ignore this item.
1193 dec.decodeIgnoredValue(wireId)
1196 // Dereference down to the underlying struct type.
1197 ut := userType(val.Type())
1200 if ut.isGobDecoder {
1201 indir = int(ut.decIndir)
1203 var enginePtr **decEngine
1204 enginePtr, dec.err = dec.getDecEnginePtr(wireId, ut)
1208 engine := *enginePtr
1209 if st, ok := base.(*reflect.StructType); ok && !ut.isGobDecoder {
1210 if engine.numInstr == 0 && st.NumField() > 0 && len(dec.wireType[wireId].StructT.Field) > 0 {
1212 errorf("gob: type mismatch: no fields matched compiling decoder for %s", name)
1214 dec.decodeStruct(engine, ut, uintptr(val.UnsafeAddr()), indir)
1216 dec.decodeSingle(engine, ut, uintptr(val.UnsafeAddr()))
1220 // decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it.
1221 func (dec *Decoder) decodeIgnoredValue(wireId typeId) {
1222 var enginePtr **decEngine
1223 enginePtr, dec.err = dec.getIgnoreEnginePtr(wireId)
1227 wire := dec.wireType[wireId]
1228 if wire != nil && wire.StructT != nil {
1229 dec.ignoreStruct(*enginePtr)
1231 dec.ignoreSingle(*enginePtr)
1237 switch reflect.Typeof(int(0)).Bits() {
1245 panic("gob: unknown size of int/uint")
1247 decOpTable[reflect.Int] = iop
1248 decOpTable[reflect.Uint] = uop
1251 switch reflect.Typeof(uintptr(0)).Bits() {
1257 panic("gob: unknown size of uintptr")
1259 decOpTable[reflect.Uintptr] = uop