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.
21 errBadUint
= errors
.New("gob: encoded unsigned integer out of range")
22 errBadType
= errors
.New("gob: unknown type id or corrupted data")
23 errRange
= errors
.New("gob: bad data: field numbers out of bounds")
26 // decoderState is the execution state of an instance of the decoder. A new state
27 // is created for nested objects.
28 type decoderState
struct {
30 // The buffer is stored with an extra indirection because it may be replaced
31 // if we load a type during decode (when reading an interface value).
33 fieldnum
int // the last field number read.
35 next
*decoderState
// for free list
38 // We pass the bytes.Buffer separately for easier testing of the infrastructure
39 // without requiring a full Decoder.
40 func (dec
*Decoder
) newDecoderState(buf
*bytes
.Buffer
) *decoderState
{
45 d
.buf
= make([]byte, uint64Size
)
53 func (dec
*Decoder
) freeDecoderState(d
*decoderState
) {
58 func overflow(name
string) error
{
59 return errors
.New(`value for "` + name
+ `" out of range`)
62 // decodeUintReader reads an encoded unsigned integer from an io.Reader.
63 // Used only by the Decoder to read the message length.
64 func decodeUintReader(r io
.Reader
, buf
[]byte) (x
uint64, width
int, err error
) {
66 n
, err
:= io
.ReadFull(r
, buf
[0:width
])
72 return uint64(b
), width
, nil
79 width
, err
= io
.ReadFull(r
, buf
[0:n
])
82 err
= io
.ErrUnexpectedEOF
86 // Could check that the high byte is zero but it's not worth it.
87 for _
, b
:= range buf
[0:width
] {
90 width
++ // +1 for length byte
94 // decodeUint reads an encoded unsigned integer from state.r.
95 // Does not check for overflow.
96 func (state
*decoderState
) decodeUint() (x
uint64) {
97 b
, err
:= state
.b
.ReadByte()
108 width
, err
:= state
.b
.Read(state
.buf
[0:n
])
112 // Don't need to check error; it's safe to loop regardless.
113 // Could check that the high byte is zero but it's not worth it.
114 for _
, b
:= range state
.buf
[0:width
] {
120 // decodeInt reads an encoded signed integer from state.r.
121 // Does not check for overflow.
122 func (state
*decoderState
) decodeInt() int64 {
123 x
:= state
.decodeUint()
125 return ^int64(x
>> 1)
130 // decOp is the signature of a decoding operator for a given type.
131 type decOp
func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
)
133 // The 'instructions' of the decoding machine
134 type decInstr
struct {
136 field
int // field number of the wire type
137 indir
int // how many pointer indirections to reach the value in the struct
138 offset
uintptr // offset in the structure of the field to encode
139 ovfl error
// error message for overflow/underflow (for arrays, of the elements)
142 // Since the encoder writes no zeros, if we arrive at a decoder we have
143 // a value to extract and store. The field number has already been read
144 // (it's how we knew to call this decoder).
145 // Each decoder is responsible for handling any indirections associated
146 // with the data structure. If any pointer so reached is nil, allocation must
149 // Walk the pointer hierarchy, allocating if we find a nil. Stop one before the end.
150 func decIndirect(p unsafe
.Pointer
, indir
int) unsafe
.Pointer
{
151 for ; indir
> 1; indir
-- {
152 if *(*unsafe
.Pointer
)(p
) == nil {
153 // Allocation required
154 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(unsafe
.Pointer
))
156 p
= *(*unsafe
.Pointer
)(p
)
161 // ignoreUint discards a uint value with no destination.
162 func ignoreUint(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
166 // ignoreTwoUints discards a uint value with no destination. It's used to skip
168 func ignoreTwoUints(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
173 // decBool decodes a uint and stores it as a boolean through p.
174 func decBool(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
176 if *(*unsafe
.Pointer
)(p
) == nil {
177 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(bool))
179 p
= *(*unsafe
.Pointer
)(p
)
181 *(*bool)(p
) = state
.decodeUint() != 0
184 // decInt8 decodes an integer and stores it as an int8 through p.
185 func decInt8(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
187 if *(*unsafe
.Pointer
)(p
) == nil {
188 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(int8))
190 p
= *(*unsafe
.Pointer
)(p
)
192 v
:= state
.decodeInt()
193 if v
< math
.MinInt8 || math
.MaxInt8
< v
{
196 *(*int8)(p
) = int8(v
)
200 // decUint8 decodes an unsigned integer and stores it as a uint8 through p.
201 func decUint8(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
203 if *(*unsafe
.Pointer
)(p
) == nil {
204 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(uint8))
206 p
= *(*unsafe
.Pointer
)(p
)
208 v
:= state
.decodeUint()
209 if math
.MaxUint8
< v
{
212 *(*uint8)(p
) = uint8(v
)
216 // decInt16 decodes an integer and stores it as an int16 through p.
217 func decInt16(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
219 if *(*unsafe
.Pointer
)(p
) == nil {
220 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(int16))
222 p
= *(*unsafe
.Pointer
)(p
)
224 v
:= state
.decodeInt()
225 if v
< math
.MinInt16 || math
.MaxInt16
< v
{
228 *(*int16)(p
) = int16(v
)
232 // decUint16 decodes an unsigned integer and stores it as a uint16 through p.
233 func decUint16(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
235 if *(*unsafe
.Pointer
)(p
) == nil {
236 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(uint16))
238 p
= *(*unsafe
.Pointer
)(p
)
240 v
:= state
.decodeUint()
241 if math
.MaxUint16
< v
{
244 *(*uint16)(p
) = uint16(v
)
248 // decInt32 decodes an integer and stores it as an int32 through p.
249 func decInt32(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
251 if *(*unsafe
.Pointer
)(p
) == nil {
252 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(int32))
254 p
= *(*unsafe
.Pointer
)(p
)
256 v
:= state
.decodeInt()
257 if v
< math
.MinInt32 || math
.MaxInt32
< v
{
260 *(*int32)(p
) = int32(v
)
264 // decUint32 decodes an unsigned integer and stores it as a uint32 through p.
265 func decUint32(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
267 if *(*unsafe
.Pointer
)(p
) == nil {
268 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(uint32))
270 p
= *(*unsafe
.Pointer
)(p
)
272 v
:= state
.decodeUint()
273 if math
.MaxUint32
< v
{
276 *(*uint32)(p
) = uint32(v
)
280 // decInt64 decodes an integer and stores it as an int64 through p.
281 func decInt64(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
283 if *(*unsafe
.Pointer
)(p
) == nil {
284 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(int64))
286 p
= *(*unsafe
.Pointer
)(p
)
288 *(*int64)(p
) = int64(state
.decodeInt())
291 // decUint64 decodes an unsigned integer and stores it as a uint64 through p.
292 func decUint64(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
294 if *(*unsafe
.Pointer
)(p
) == nil {
295 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(uint64))
297 p
= *(*unsafe
.Pointer
)(p
)
299 *(*uint64)(p
) = uint64(state
.decodeUint())
302 // Floating-point numbers are transmitted as uint64s holding the bits
303 // of the underlying representation. They are sent byte-reversed, with
304 // the exponent end coming out first, so integer floating point numbers
305 // (for example) transmit more compactly. This routine does the
307 func floatFromBits(u
uint64) float64 {
309 for i
:= 0; i
< 8; i
++ {
314 return math
.Float64frombits(v
)
317 // storeFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
318 // number, and stores it through p. It's a helper function for float32 and complex64.
319 func storeFloat32(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
320 v
:= floatFromBits(state
.decodeUint())
325 // +Inf is OK in both 32- and 64-bit floats. Underflow is always OK.
326 if math
.MaxFloat32
< av
&& av
<= math
.MaxFloat64
{
329 *(*float32)(p
) = float32(v
)
333 // decFloat32 decodes an unsigned integer, treats it as a 32-bit floating-point
334 // number, and stores it through p.
335 func decFloat32(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
337 if *(*unsafe
.Pointer
)(p
) == nil {
338 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(float32))
340 p
= *(*unsafe
.Pointer
)(p
)
342 storeFloat32(i
, state
, p
)
345 // decFloat64 decodes an unsigned integer, treats it as a 64-bit floating-point
346 // number, and stores it through p.
347 func decFloat64(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
349 if *(*unsafe
.Pointer
)(p
) == nil {
350 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(float64))
352 p
= *(*unsafe
.Pointer
)(p
)
354 *(*float64)(p
) = floatFromBits(uint64(state
.decodeUint()))
357 // decComplex64 decodes a pair of unsigned integers, treats them as a
358 // pair of floating point numbers, and stores them as a complex64 through p.
359 // The real part comes first.
360 func decComplex64(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
362 if *(*unsafe
.Pointer
)(p
) == nil {
363 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(complex64
))
365 p
= *(*unsafe
.Pointer
)(p
)
367 storeFloat32(i
, state
, p
)
368 storeFloat32(i
, state
, unsafe
.Pointer(uintptr(p
)+unsafe
.Sizeof(float32(0))))
371 // decComplex128 decodes a pair of unsigned integers, treats them as a
372 // pair of floating point numbers, and stores them as a complex128 through p.
373 // The real part comes first.
374 func decComplex128(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
376 if *(*unsafe
.Pointer
)(p
) == nil {
377 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(complex128
))
379 p
= *(*unsafe
.Pointer
)(p
)
381 real
:= floatFromBits(uint64(state
.decodeUint()))
382 imag
:= floatFromBits(uint64(state
.decodeUint()))
383 *(*complex128
)(p
) = complex(real
, imag
)
386 // decUint8Slice decodes a byte slice and stores through p a slice header
387 // describing the data.
388 // uint8 slices are encoded as an unsigned count followed by the raw bytes.
389 func decUint8Slice(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
391 if *(*unsafe
.Pointer
)(p
) == nil {
392 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new([]uint8))
394 p
= *(*unsafe
.Pointer
)(p
)
396 n
:= state
.decodeUint()
397 if n
> uint64(state
.b
.Len()) {
398 errorf("length of []byte exceeds input size (%d bytes)", n
)
400 slice
:= (*[]uint8)(p
)
401 if uint64(cap(*slice
)) < n
{
402 *slice
= make([]uint8, n
)
404 *slice
= (*slice
)[0:n
]
406 if _
, err
:= state
.b
.Read(*slice
); err
!= nil {
407 errorf("error decoding []byte: %s", err
)
411 // decString decodes byte array and stores through p a string header
412 // describing the data.
413 // Strings are encoded as an unsigned count followed by the raw bytes.
414 func decString(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
416 if *(*unsafe
.Pointer
)(p
) == nil {
417 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new(string))
419 p
= *(*unsafe
.Pointer
)(p
)
421 n
:= state
.decodeUint()
422 if n
> uint64(state
.b
.Len()) {
423 errorf("string length exceeds input size (%d bytes)", n
)
427 // It would be a shame to do the obvious thing here,
428 // *(*string)(p) = string(b)
429 // because we've already allocated the storage and this would
430 // allocate again and copy. So we do this ugly hack, which is even
431 // even more unsafe than it looks as it depends the memory
432 // representation of a string matching the beginning of the memory
433 // representation of a byte slice (a byte slice is longer).
434 *(*string)(p
) = *(*string)(unsafe
.Pointer(&b
))
437 // ignoreUint8Array skips over the data for a byte slice value with no destination.
438 func ignoreUint8Array(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
439 b
:= make([]byte, state
.decodeUint())
445 // The encoder engine is an array of instructions indexed by field number of the incoming
446 // decoder. It is executed with random access according to field number.
447 type decEngine
struct {
449 numInstr
int // the number of active instructions
452 // allocate makes sure storage is available for an object of underlying type rtyp
453 // that is indir levels of indirection through p.
454 func allocate(rtyp reflect
.Type
, p unsafe
.Pointer
, indir
int) unsafe
.Pointer
{
460 up
= decIndirect(up
, indir
)
462 if *(*unsafe
.Pointer
)(up
) == nil {
464 *(*unsafe
.Pointer
)(up
) = unsafe
.Pointer(reflect
.New(rtyp
).Pointer())
466 return *(*unsafe
.Pointer
)(up
)
469 // decodeSingle decodes a top-level value that is not a struct and stores it through p.
470 // Such values are preceded by a zero, making them have the memory layout of a
471 // struct field (although with an illegal field number).
472 func (dec
*Decoder
) decodeSingle(engine
*decEngine
, ut
*userTypeInfo
, basep unsafe
.Pointer
) {
473 state
:= dec
.newDecoderState(&dec
.buf
)
474 state
.fieldnum
= singletonField
475 delta
:= int(state
.decodeUint())
477 errorf("decode: corrupted data: non-zero delta for singleton")
479 instr
:= &engine
.instr
[singletonField
]
480 if instr
.indir
!= ut
.indir
{
481 errorf("internal error: inconsistent indirection instr %d ut %d", instr
.indir
, ut
.indir
)
483 ptr
:= basep
// offset will be zero
485 ptr
= decIndirect(ptr
, instr
.indir
)
487 instr
.op(instr
, state
, ptr
)
488 dec
.freeDecoderState(state
)
491 // decodeStruct decodes a top-level struct and stores it through p.
492 // Indir is for the value, not the type. At the time of the call it may
493 // differ from ut.indir, which was computed when the engine was built.
494 // This state cannot arise for decodeSingle, which is called directly
495 // from the user's value, not from the innards of an engine.
496 func (dec
*Decoder
) decodeStruct(engine
*decEngine
, ut
*userTypeInfo
, p unsafe
.Pointer
, indir
int) {
497 p
= allocate(ut
.base
, p
, indir
)
498 state
:= dec
.newDecoderState(&dec
.buf
)
501 for state
.b
.Len() > 0 {
502 delta
:= int(state
.decodeUint())
504 errorf("decode: corrupted data: negative delta")
506 if delta
== 0 { // struct terminator is zero delta fieldnum
509 fieldnum
:= state
.fieldnum
+ delta
510 if fieldnum
>= len(engine
.instr
) {
514 instr
:= &engine
.instr
[fieldnum
]
515 p
:= unsafe
.Pointer(uintptr(basep
) + instr
.offset
)
517 p
= decIndirect(p
, instr
.indir
)
519 instr
.op(instr
, state
, p
)
520 state
.fieldnum
= fieldnum
522 dec
.freeDecoderState(state
)
525 // ignoreStruct discards the data for a struct with no destination.
526 func (dec
*Decoder
) ignoreStruct(engine
*decEngine
) {
527 state
:= dec
.newDecoderState(&dec
.buf
)
529 for state
.b
.Len() > 0 {
530 delta
:= int(state
.decodeUint())
532 errorf("ignore decode: corrupted data: negative delta")
534 if delta
== 0 { // struct terminator is zero delta fieldnum
537 fieldnum
:= state
.fieldnum
+ delta
538 if fieldnum
>= len(engine
.instr
) {
541 instr
:= &engine
.instr
[fieldnum
]
542 instr
.op(instr
, state
, unsafe
.Pointer(nil))
543 state
.fieldnum
= fieldnum
545 dec
.freeDecoderState(state
)
548 // ignoreSingle discards the data for a top-level non-struct value with no
549 // destination. It's used when calling Decode with a nil value.
550 func (dec
*Decoder
) ignoreSingle(engine
*decEngine
) {
551 state
:= dec
.newDecoderState(&dec
.buf
)
552 state
.fieldnum
= singletonField
553 delta
:= int(state
.decodeUint())
555 errorf("decode: corrupted data: non-zero delta for singleton")
557 instr
:= &engine
.instr
[singletonField
]
558 instr
.op(instr
, state
, unsafe
.Pointer(nil))
559 dec
.freeDecoderState(state
)
562 // decodeArrayHelper does the work for decoding arrays and slices.
563 func (dec
*Decoder
) decodeArrayHelper(state
*decoderState
, p unsafe
.Pointer
, elemOp decOp
, elemWid
uintptr, length
, elemIndir
int, ovfl error
) {
564 instr
:= &decInstr
{elemOp
, 0, elemIndir
, 0, ovfl
}
565 for i
:= 0; i
< length
; i
++ {
566 if state
.b
.Len() == 0 {
567 errorf("decoding array or slice: length exceeds input size (%d elements)", length
)
571 up
= decIndirect(up
, elemIndir
)
573 elemOp(instr
, state
, up
)
574 p
= unsafe
.Pointer(uintptr(p
) + elemWid
)
578 // decodeArray decodes an array and stores it through p, that is, p points to the zeroth element.
579 // The length is an unsigned integer preceding the elements. Even though the length is redundant
580 // (it's part of the type), it's a useful check and is included in the encoding.
581 func (dec
*Decoder
) decodeArray(atyp reflect
.Type
, state
*decoderState
, p unsafe
.Pointer
, elemOp decOp
, elemWid
uintptr, length
, indir
, elemIndir
int, ovfl error
) {
583 p
= allocate(atyp
, p
, 1) // All but the last level has been allocated by dec.Indirect
585 if n
:= state
.decodeUint(); n
!= uint64(length
) {
586 errorf("length mismatch in decodeArray")
588 dec
.decodeArrayHelper(state
, p
, elemOp
, elemWid
, length
, elemIndir
, ovfl
)
591 // decodeIntoValue is a helper for map decoding. Since maps are decoded using reflection,
592 // unlike the other items we can't use a pointer directly.
593 func decodeIntoValue(state
*decoderState
, op decOp
, indir
int, v reflect
.Value
, ovfl error
) reflect
.Value
{
594 instr
:= &decInstr
{op
, 0, indir
, 0, ovfl
}
597 up
= decIndirect(up
, indir
)
603 // decodeMap decodes a map and stores its header through p.
604 // Maps are encoded as a length followed by key:value pairs.
605 // Because the internals of maps are not visible to us, we must
606 // use reflection rather than pointer magic.
607 func (dec
*Decoder
) decodeMap(mtyp reflect
.Type
, state
*decoderState
, p unsafe
.Pointer
, keyOp
, elemOp decOp
, indir
, keyIndir
, elemIndir
int, ovfl error
) {
609 p
= allocate(mtyp
, p
, 1) // All but the last level has been allocated by dec.Indirect
611 up
:= unsafe
.Pointer(p
)
612 if *(*unsafe
.Pointer
)(up
) == nil { // maps are represented as a pointer in the runtime
614 *(*unsafe
.Pointer
)(up
) = unsafe
.Pointer(reflect
.MakeMap(mtyp
).Pointer())
616 // Maps cannot be accessed by moving addresses around the way
617 // that slices etc. can. We must recover a full reflection value for
619 v
:= reflect
.NewAt(mtyp
, unsafe
.Pointer(p
)).Elem()
620 n
:= int(state
.decodeUint())
621 for i
:= 0; i
< n
; i
++ {
622 key
:= decodeIntoValue(state
, keyOp
, keyIndir
, allocValue(mtyp
.Key()), ovfl
)
623 elem
:= decodeIntoValue(state
, elemOp
, elemIndir
, allocValue(mtyp
.Elem()), ovfl
)
624 v
.SetMapIndex(key
, elem
)
628 // ignoreArrayHelper does the work for discarding arrays and slices.
629 func (dec
*Decoder
) ignoreArrayHelper(state
*decoderState
, elemOp decOp
, length
int) {
630 instr
:= &decInstr
{elemOp
, 0, 0, 0, errors
.New("no error")}
631 for i
:= 0; i
< length
; i
++ {
632 elemOp(instr
, state
, nil)
636 // ignoreArray discards the data for an array value with no destination.
637 func (dec
*Decoder
) ignoreArray(state
*decoderState
, elemOp decOp
, length
int) {
638 if n
:= state
.decodeUint(); n
!= uint64(length
) {
639 errorf("length mismatch in ignoreArray")
641 dec
.ignoreArrayHelper(state
, elemOp
, length
)
644 // ignoreMap discards the data for a map value with no destination.
645 func (dec
*Decoder
) ignoreMap(state
*decoderState
, keyOp
, elemOp decOp
) {
646 n
:= int(state
.decodeUint())
647 keyInstr
:= &decInstr
{keyOp
, 0, 0, 0, errors
.New("no error")}
648 elemInstr
:= &decInstr
{elemOp
, 0, 0, 0, errors
.New("no error")}
649 for i
:= 0; i
< n
; i
++ {
650 keyOp(keyInstr
, state
, nil)
651 elemOp(elemInstr
, state
, nil)
655 // decodeSlice decodes a slice and stores the slice header through p.
656 // Slices are encoded as an unsigned length followed by the elements.
657 func (dec
*Decoder
) decodeSlice(atyp reflect
.Type
, state
*decoderState
, p unsafe
.Pointer
, elemOp decOp
, elemWid
uintptr, indir
, elemIndir
int, ovfl error
) {
658 nr
:= state
.decodeUint()
661 if *(*unsafe
.Pointer
)(p
) == nil {
662 // Allocate the slice header.
663 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(new([]unsafe
.Pointer
))
665 p
= *(*unsafe
.Pointer
)(p
)
667 // Allocate storage for the slice elements, that is, the underlying array,
668 // if the existing slice does not have the capacity.
669 // Always write a header at p.
670 hdrp
:= (*reflect
.SliceHeader
)(p
)
672 hdrp
.Data
= reflect
.MakeSlice(atyp
, n
, n
).Pointer()
676 dec
.decodeArrayHelper(state
, unsafe
.Pointer(hdrp
.Data
), elemOp
, elemWid
, n
, elemIndir
, ovfl
)
679 // ignoreSlice skips over the data for a slice value with no destination.
680 func (dec
*Decoder
) ignoreSlice(state
*decoderState
, elemOp decOp
) {
681 dec
.ignoreArrayHelper(state
, elemOp
, int(state
.decodeUint()))
684 // setInterfaceValue sets an interface value to a concrete value,
685 // but first it checks that the assignment will succeed.
686 func setInterfaceValue(ivalue reflect
.Value
, value reflect
.Value
) {
687 if !value
.Type().AssignableTo(ivalue
.Type()) {
688 errorf("cannot assign value of type %s to %s", value
.Type(), ivalue
.Type())
693 // decodeInterface decodes an interface value and stores it through p.
694 // Interfaces are encoded as the name of a concrete type followed by a value.
695 // If the name is empty, the value is nil and no value is sent.
696 func (dec
*Decoder
) decodeInterface(ityp reflect
.Type
, state
*decoderState
, p unsafe
.Pointer
, indir
int) {
697 // Create a writable interface reflect.Value. We need one even for the nil case.
698 ivalue
:= allocValue(ityp
)
699 // Read the name of the concrete type.
700 nr
:= state
.decodeUint()
701 if nr
< 0 || nr
> 1<<31 { // zero is permissible for anonymous types
702 errorf("invalid type name length %d", nr
)
704 b
:= make([]byte, nr
)
708 // Copy the representation of the nil interface value to the target.
709 // This is horribly unsafe and special.
711 p
= allocate(ityp
, p
, 1) // All but the last level has been allocated by dec.Indirect
713 *(*[2]uintptr)(unsafe
.Pointer(p
)) = ivalue
.InterfaceData()
716 if len(name
) > 1024 {
717 errorf("name too long (%d bytes): %.20q...", len(name
), name
)
719 // The concrete type must be registered.
721 typ
, ok
:= nameToConcreteType
[name
]
722 registerLock
.RUnlock()
724 errorf("name not registered for interface: %q", name
)
726 // Read the type id of the concrete value.
727 concreteId
:= dec
.decodeTypeSequence(true)
731 // Byte count of value is next; we don't care what it is (it's there
732 // in case we want to ignore the value by skipping it completely).
734 // Read the concrete value.
735 value
:= allocValue(typ
)
736 dec
.decodeValue(concreteId
, value
)
740 // Allocate the destination interface value.
742 p
= allocate(ityp
, p
, 1) // All but the last level has been allocated by dec.Indirect
744 // Assign the concrete value to the interface.
745 // Tread carefully; it might not satisfy the interface.
746 setInterfaceValue(ivalue
, value
)
747 // Copy the representation of the interface value to the target.
748 // This is horribly unsafe and special.
749 *(*[2]uintptr)(unsafe
.Pointer(p
)) = ivalue
.InterfaceData()
752 // ignoreInterface discards the data for an interface value with no destination.
753 func (dec
*Decoder
) ignoreInterface(state
*decoderState
) {
754 // Read the name of the concrete type.
755 b
:= make([]byte, state
.decodeUint())
756 _
, err
:= state
.b
.Read(b
)
760 id
:= dec
.decodeTypeSequence(true)
764 // At this point, the decoder buffer contains a delimited value. Just toss it.
765 state
.b
.Next(int(state
.decodeUint()))
768 // decodeGobDecoder decodes something implementing the GobDecoder interface.
769 // The data is encoded as a byte slice.
770 func (dec
*Decoder
) decodeGobDecoder(ut
*userTypeInfo
, state
*decoderState
, v reflect
.Value
) {
771 // Read the bytes for the value.
772 b
:= make([]byte, state
.decodeUint())
773 _
, err
:= state
.b
.Read(b
)
777 // We know it's one of these.
778 switch ut
.externalDec
{
780 err
= v
.Interface().(GobDecoder
).GobDecode(b
)
782 err
= v
.Interface().(encoding
.BinaryUnmarshaler
).UnmarshalBinary(b
)
784 err
= v
.Interface().(encoding
.TextUnmarshaler
).UnmarshalText(b
)
791 // ignoreGobDecoder discards the data for a GobDecoder value with no destination.
792 func (dec
*Decoder
) ignoreGobDecoder(state
*decoderState
) {
793 // Read the bytes for the value.
794 b
:= make([]byte, state
.decodeUint())
795 _
, err
:= state
.b
.Read(b
)
801 // Index by Go types.
802 var decOpTable
= [...]decOp
{
803 reflect
.Bool
: decBool
,
804 reflect
.Int8
: decInt8
,
805 reflect
.Int16
: decInt16
,
806 reflect
.Int32
: decInt32
,
807 reflect
.Int64
: decInt64
,
808 reflect
.Uint8
: decUint8
,
809 reflect
.Uint16
: decUint16
,
810 reflect
.Uint32
: decUint32
,
811 reflect
.Uint64
: decUint64
,
812 reflect
.Float32
: decFloat32
,
813 reflect
.Float64
: decFloat64
,
814 reflect
.Complex64
: decComplex64
,
815 reflect
.Complex128
: decComplex128
,
816 reflect
.String
: decString
,
819 // Indexed by gob types. tComplex will be added during type.init().
820 var decIgnoreOpMap
= map[typeId
]decOp
{
825 tBytes
: ignoreUint8Array
,
826 tString
: ignoreUint8Array
,
827 tComplex
: ignoreTwoUints
,
830 // decOpFor returns the decoding op for the base type under rt and
831 // the indirection count to reach it.
832 func (dec
*Decoder
) decOpFor(wireId typeId
, rt reflect
.Type
, name
string, inProgress
map[reflect
.Type
]*decOp
) (*decOp
, int) {
834 // If the type implements GobEncoder, we handle it without further processing.
835 if ut
.externalDec
!= 0 {
836 return dec
.gobDecodeOpFor(ut
)
839 // If this type is already in progress, it's a recursive type (e.g. map[string]*T).
840 // Return the pointer to the op we're already building.
841 if opPtr
:= inProgress
[rt
]; opPtr
!= nil {
842 return opPtr
, ut
.indir
848 if int(k
) < len(decOpTable
) {
854 switch t
:= typ
; t
.Kind() {
856 name
= "element of " + name
857 elemId
:= dec
.wireType
[wireId
].ArrayT
.Elem
858 elemOp
, elemIndir
:= dec
.decOpFor(elemId
, t
.Elem(), name
, inProgress
)
859 ovfl
:= overflow(name
)
860 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
861 state
.dec
.decodeArray(t
, state
, p
, *elemOp
, t
.Elem().Size(), t
.Len(), i
.indir
, elemIndir
, ovfl
)
865 keyId
:= dec
.wireType
[wireId
].MapT
.Key
866 elemId
:= dec
.wireType
[wireId
].MapT
.Elem
867 keyOp
, keyIndir
:= dec
.decOpFor(keyId
, t
.Key(), "key of "+name
, inProgress
)
868 elemOp
, elemIndir
:= dec
.decOpFor(elemId
, t
.Elem(), "element of "+name
, inProgress
)
869 ovfl
:= overflow(name
)
870 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
871 state
.dec
.decodeMap(t
, state
, p
, *keyOp
, *elemOp
, i
.indir
, keyIndir
, elemIndir
, ovfl
)
875 name
= "element of " + name
876 if t
.Elem().Kind() == reflect
.Uint8
{
881 if tt
, ok
:= builtinIdToType
[wireId
]; ok
{
882 elemId
= tt
.(*sliceType
).Elem
884 elemId
= dec
.wireType
[wireId
].SliceT
.Elem
886 elemOp
, elemIndir
:= dec
.decOpFor(elemId
, t
.Elem(), name
, inProgress
)
887 ovfl
:= overflow(name
)
888 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
889 state
.dec
.decodeSlice(t
, state
, p
, *elemOp
, t
.Elem().Size(), i
.indir
, elemIndir
, ovfl
)
893 // Generate a closure that calls out to the engine for the nested type.
894 enginePtr
, err
:= dec
.getDecEnginePtr(wireId
, userType(typ
))
898 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
899 // indirect through enginePtr to delay evaluation for recursive structs.
900 dec
.decodeStruct(*enginePtr
, userType(typ
), p
, i
.indir
)
902 case reflect
.Interface
:
903 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
904 state
.dec
.decodeInterface(t
, state
, p
, i
.indir
)
909 errorf("decode can't handle type %s", rt
)
914 // decIgnoreOpFor returns the decoding op for a field that has no destination.
915 func (dec
*Decoder
) decIgnoreOpFor(wireId typeId
) decOp
{
916 op
, ok
:= decIgnoreOpMap
[wireId
]
918 if wireId
== tInterface
{
919 // Special case because it's a method: the ignored item might
920 // define types and we need to record their state in the decoder.
921 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
922 state
.dec
.ignoreInterface(state
)
927 wire
:= dec
.wireType
[wireId
]
930 errorf("bad data: undefined type %s", wireId
.string())
931 case wire
.ArrayT
!= nil:
932 elemId
:= wire
.ArrayT
.Elem
933 elemOp
:= dec
.decIgnoreOpFor(elemId
)
934 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
935 state
.dec
.ignoreArray(state
, elemOp
, wire
.ArrayT
.Len
)
938 case wire
.MapT
!= nil:
939 keyId
:= dec
.wireType
[wireId
].MapT
.Key
940 elemId
:= dec
.wireType
[wireId
].MapT
.Elem
941 keyOp
:= dec
.decIgnoreOpFor(keyId
)
942 elemOp
:= dec
.decIgnoreOpFor(elemId
)
943 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
944 state
.dec
.ignoreMap(state
, keyOp
, elemOp
)
947 case wire
.SliceT
!= nil:
948 elemId
:= wire
.SliceT
.Elem
949 elemOp
:= dec
.decIgnoreOpFor(elemId
)
950 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
951 state
.dec
.ignoreSlice(state
, elemOp
)
954 case wire
.StructT
!= nil:
955 // Generate a closure that calls out to the engine for the nested type.
956 enginePtr
, err
:= dec
.getIgnoreEnginePtr(wireId
)
960 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
961 // indirect through enginePtr to delay evaluation for recursive structs
962 state
.dec
.ignoreStruct(*enginePtr
)
965 case wire
.GobEncoderT
!= nil, wire
.BinaryMarshalerT
!= nil, wire
.TextMarshalerT
!= nil:
966 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
967 state
.dec
.ignoreGobDecoder(state
)
972 errorf("bad data: ignore can't handle type %s", wireId
.string())
977 // gobDecodeOpFor returns the op for a type that is known to implement
979 func (dec
*Decoder
) gobDecodeOpFor(ut
*userTypeInfo
) (*decOp
, int) {
981 if ut
.decIndir
== -1 {
982 rcvrType
= reflect
.PtrTo(rcvrType
)
983 } else if ut
.decIndir
> 0 {
984 for i
:= int8(0); i
< ut
.decIndir
; i
++ {
985 rcvrType
= rcvrType
.Elem()
989 op
= func(i
*decInstr
, state
*decoderState
, p unsafe
.Pointer
) {
990 // Caller has gotten us to within one indirection of our value.
992 if *(*unsafe
.Pointer
)(p
) == nil {
993 *(*unsafe
.Pointer
)(p
) = unsafe
.Pointer(reflect
.New(ut
.base
).Pointer())
996 // Now p is a pointer to the base type. Do we need to climb out to
997 // get to the receiver type?
999 if ut
.decIndir
== -1 {
1000 v
= reflect
.NewAt(rcvrType
, unsafe
.Pointer(&p
)).Elem()
1002 v
= reflect
.NewAt(rcvrType
, p
).Elem()
1004 state
.dec
.decodeGobDecoder(ut
, state
, v
)
1006 return &op
, int(ut
.indir
)
1010 // compatibleType asks: Are these two gob Types compatible?
1011 // Answers the question for basic types, arrays, maps and slices, plus
1012 // GobEncoder/Decoder pairs.
1013 // Structs are considered ok; fields will be checked later.
1014 func (dec
*Decoder
) compatibleType(fr reflect
.Type
, fw typeId
, inProgress
map[reflect
.Type
]typeId
) bool {
1015 if rhs
, ok
:= inProgress
[fr
]; ok
{
1020 wire
, ok
:= dec
.wireType
[fw
]
1021 // If wire was encoded with an encoding method, fr must have that method.
1022 // And if not, it must not.
1023 // At most one of the booleans in ut is set.
1024 // We could possibly relax this constraint in the future in order to
1025 // choose the decoding method using the data in the wireType.
1026 // The parentheses look odd but are correct.
1027 if (ut
.externalDec
== xGob
) != (ok
&& wire
.GobEncoderT
!= nil) ||
1028 (ut
.externalDec
== xBinary
) != (ok
&& wire
.BinaryMarshalerT
!= nil) ||
1029 (ut
.externalDec
== xText
) != (ok
&& wire
.TextMarshalerT
!= nil) {
1032 if ut
.externalDec
!= 0 { // This test trumps all others.
1035 switch t
:= ut
.base
; t
.Kind() {
1037 // chan, etc: cannot handle.
1041 case reflect
.Int
, reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
:
1043 case reflect
.Uint
, reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
, reflect
.Uintptr
:
1045 case reflect
.Float32
, reflect
.Float64
:
1047 case reflect
.Complex64
, reflect
.Complex128
:
1048 return fw
== tComplex
1049 case reflect
.String
:
1050 return fw
== tString
1051 case reflect
.Interface
:
1052 return fw
== tInterface
1054 if !ok || wire
.ArrayT
== nil {
1057 array
:= wire
.ArrayT
1058 return t
.Len() == array
.Len
&& dec
.compatibleType(t
.Elem(), array
.Elem
, inProgress
)
1060 if !ok || wire
.MapT
== nil {
1063 MapType
:= wire
.MapT
1064 return dec
.compatibleType(t
.Key(), MapType
.Key
, inProgress
) && dec
.compatibleType(t
.Elem(), MapType
.Elem
, inProgress
)
1066 // Is it an array of bytes?
1067 if t
.Elem().Kind() == reflect
.Uint8
{
1070 // Extract and compare element types.
1072 if tt
, ok
:= builtinIdToType
[fw
]; ok
{
1073 sw
, _
= tt
.(*sliceType
)
1074 } else if wire
!= nil {
1077 elem
:= userType(t
.Elem()).base
1078 return sw
!= nil && dec
.compatibleType(elem
, sw
.Elem
, inProgress
)
1079 case reflect
.Struct
:
1084 // typeString returns a human-readable description of the type identified by remoteId.
1085 func (dec
*Decoder
) typeString(remoteId typeId
) string {
1086 if t
:= idToType
[remoteId
]; t
!= nil {
1087 // globally known type.
1090 return dec
.wireType
[remoteId
].string()
1093 // compileSingle compiles the decoder engine for a non-struct top-level value, including
1095 func (dec
*Decoder
) compileSingle(remoteId typeId
, ut
*userTypeInfo
) (engine
*decEngine
, err error
) {
1097 engine
= new(decEngine
)
1098 engine
.instr
= make([]decInstr
, 1) // one item
1099 name
:= rt
.String() // best we can do
1100 if !dec
.compatibleType(rt
, remoteId
, make(map[reflect
.Type
]typeId
)) {
1101 remoteType
:= dec
.typeString(remoteId
)
1102 // Common confusing case: local interface type, remote concrete type.
1103 if ut
.base
.Kind() == reflect
.Interface
&& remoteId
!= tInterface
{
1104 return nil, errors
.New("gob: local interface type " + name
+ " can only be decoded from remote interface type; received concrete type " + remoteType
)
1106 return nil, errors
.New("gob: decoding into local type " + name
+ ", received remote type " + remoteType
)
1108 op
, indir
:= dec
.decOpFor(remoteId
, rt
, name
, make(map[reflect
.Type
]*decOp
))
1109 ovfl
:= errors
.New(`value for "` + name
+ `" out of range`)
1110 engine
.instr
[singletonField
] = decInstr
{*op
, singletonField
, indir
, 0, ovfl
}
1115 // compileIgnoreSingle compiles the decoder engine for a non-struct top-level value that will be discarded.
1116 func (dec
*Decoder
) compileIgnoreSingle(remoteId typeId
) (engine
*decEngine
, err error
) {
1117 engine
= new(decEngine
)
1118 engine
.instr
= make([]decInstr
, 1) // one item
1119 op
:= dec
.decIgnoreOpFor(remoteId
)
1120 ovfl
:= overflow(dec
.typeString(remoteId
))
1121 engine
.instr
[0] = decInstr
{op
, 0, 0, 0, ovfl
}
1126 // compileDec compiles the decoder engine for a value. If the value is not a struct,
1127 // it calls out to compileSingle.
1128 func (dec
*Decoder
) compileDec(remoteId typeId
, ut
*userTypeInfo
) (engine
*decEngine
, err error
) {
1131 if srt
.Kind() != reflect
.Struct || ut
.externalDec
!= 0 {
1132 return dec
.compileSingle(remoteId
, ut
)
1134 var wireStruct
*structType
1135 // Builtin types can come from global pool; the rest must be defined by the decoder.
1136 // Also we know we're decoding a struct now, so the client must have sent one.
1137 if t
, ok
:= builtinIdToType
[remoteId
]; ok
{
1138 wireStruct
, _
= t
.(*structType
)
1140 wire
:= dec
.wireType
[remoteId
]
1144 wireStruct
= wire
.StructT
1146 if wireStruct
== nil {
1147 errorf("type mismatch in decoder: want struct type %s; got non-struct", rt
)
1149 engine
= new(decEngine
)
1150 engine
.instr
= make([]decInstr
, len(wireStruct
.Field
))
1151 seen
:= make(map[reflect
.Type
]*decOp
)
1152 // Loop over the fields of the wire type.
1153 for fieldnum
:= 0; fieldnum
< len(wireStruct
.Field
); fieldnum
++ {
1154 wireField
:= wireStruct
.Field
[fieldnum
]
1155 if wireField
.Name
== "" {
1156 errorf("empty name for remote field of type %s", wireStruct
.Name
)
1158 ovfl
:= overflow(wireField
.Name
)
1159 // Find the field of the local type with the same name.
1160 localField
, present
:= srt
.FieldByName(wireField
.Name
)
1161 // TODO(r): anonymous names
1162 if !present ||
!isExported(wireField
.Name
) {
1163 op
:= dec
.decIgnoreOpFor(wireField
.Id
)
1164 engine
.instr
[fieldnum
] = decInstr
{op
, fieldnum
, 0, 0, ovfl
}
1167 if !dec
.compatibleType(localField
.Type
, wireField
.Id
, make(map[reflect
.Type
]typeId
)) {
1168 errorf("wrong type (%s) for received field %s.%s", localField
.Type
, wireStruct
.Name
, wireField
.Name
)
1170 op
, indir
:= dec
.decOpFor(wireField
.Id
, localField
.Type
, localField
.Name
, seen
)
1171 engine
.instr
[fieldnum
] = decInstr
{*op
, fieldnum
, indir
, uintptr(localField
.Offset
), ovfl
}
1177 // getDecEnginePtr returns the engine for the specified type.
1178 func (dec
*Decoder
) getDecEnginePtr(remoteId typeId
, ut
*userTypeInfo
) (enginePtr
**decEngine
, err error
) {
1180 decoderMap
, ok
:= dec
.decoderCache
[rt
]
1182 decoderMap
= make(map[typeId
]**decEngine
)
1183 dec
.decoderCache
[rt
] = decoderMap
1185 if enginePtr
, ok
= decoderMap
[remoteId
]; !ok
{
1186 // To handle recursive types, mark this engine as underway before compiling.
1187 enginePtr
= new(*decEngine
)
1188 decoderMap
[remoteId
] = enginePtr
1189 *enginePtr
, err
= dec
.compileDec(remoteId
, ut
)
1191 delete(decoderMap
, remoteId
)
1197 // emptyStruct is the type we compile into when ignoring a struct value.
1198 type emptyStruct
struct{}
1200 var emptyStructType
= reflect
.TypeOf(emptyStruct
{})
1202 // getDecEnginePtr returns the engine for the specified type when the value is to be discarded.
1203 func (dec
*Decoder
) getIgnoreEnginePtr(wireId typeId
) (enginePtr
**decEngine
, err error
) {
1205 if enginePtr
, ok
= dec
.ignorerCache
[wireId
]; !ok
{
1206 // To handle recursive types, mark this engine as underway before compiling.
1207 enginePtr
= new(*decEngine
)
1208 dec
.ignorerCache
[wireId
] = enginePtr
1209 wire
:= dec
.wireType
[wireId
]
1210 if wire
!= nil && wire
.StructT
!= nil {
1211 *enginePtr
, err
= dec
.compileDec(wireId
, userType(emptyStructType
))
1213 *enginePtr
, err
= dec
.compileIgnoreSingle(wireId
)
1216 delete(dec
.ignorerCache
, wireId
)
1222 // decodeValue decodes the data stream representing a value and stores it in val.
1223 func (dec
*Decoder
) decodeValue(wireId typeId
, val reflect
.Value
) {
1224 defer catchError(&dec
.err
)
1225 // If the value is nil, it means we should just ignore this item.
1227 dec
.decodeIgnoredValue(wireId
)
1230 // Dereference down to the underlying type.
1231 ut
:= userType(val
.Type())
1233 var enginePtr
**decEngine
1234 enginePtr
, dec
.err
= dec
.getDecEnginePtr(wireId
, ut
)
1238 engine
:= *enginePtr
1239 if st
:= base
; st
.Kind() == reflect
.Struct
&& ut
.externalDec
== 0 {
1240 if engine
.numInstr
== 0 && st
.NumField() > 0 && len(dec
.wireType
[wireId
].StructT
.Field
) > 0 {
1242 errorf("type mismatch: no fields matched compiling decoder for %s", name
)
1244 dec
.decodeStruct(engine
, ut
, unsafeAddr(val
), ut
.indir
)
1246 dec
.decodeSingle(engine
, ut
, unsafeAddr(val
))
1250 // decodeIgnoredValue decodes the data stream representing a value of the specified type and discards it.
1251 func (dec
*Decoder
) decodeIgnoredValue(wireId typeId
) {
1252 var enginePtr
**decEngine
1253 enginePtr
, dec
.err
= dec
.getIgnoreEnginePtr(wireId
)
1257 wire
:= dec
.wireType
[wireId
]
1258 if wire
!= nil && wire
.StructT
!= nil {
1259 dec
.ignoreStruct(*enginePtr
)
1261 dec
.ignoreSingle(*enginePtr
)
1267 switch reflect
.TypeOf(int(0)).Bits() {
1275 panic("gob: unknown size of int/uint")
1277 decOpTable
[reflect
.Int
] = iop
1278 decOpTable
[reflect
.Uint
] = uop
1281 switch reflect
.TypeOf(uintptr(0)).Bits() {
1287 panic("gob: unknown size of uintptr")
1289 decOpTable
[reflect
.Uintptr
] = uop
1292 // Gob assumes it can call UnsafeAddr on any Value
1293 // in order to get a pointer it can copy data from.
1294 // Values that have just been created and do not point
1295 // into existing structs or slices cannot be addressed,
1296 // so simulate it by returning a pointer to a copy.
1297 // Each call allocates once.
1298 func unsafeAddr(v reflect
.Value
) unsafe
.Pointer
{
1300 return unsafe
.Pointer(v
.UnsafeAddr())
1302 x
:= reflect
.New(v
.Type()).Elem()
1304 return unsafe
.Pointer(x
.UnsafeAddr())
1307 // Gob depends on being able to take the address
1308 // of zeroed Values it creates, so use this wrapper instead
1309 // of the standard reflect.Zero.
1310 // Each call allocates once.
1311 func allocValue(t reflect
.Type
) reflect
.Value
{
1312 return reflect
.New(t
).Elem()