2009-07-11 15:45:54 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package gob
|
|
|
|
|
|
|
|
|
|
import (
|
2009-12-15 15:35:38 -08:00
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"reflect"
|
|
|
|
|
"sync"
|
2009-07-11 15:45:54 -07:00
|
|
|
)
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
// A Decoder manages the receipt of type and data information read from the
|
|
|
|
|
// remote side of a connection.
|
2009-07-11 15:45:54 -07:00
|
|
|
type Decoder struct {
|
2009-12-15 15:35:38 -08:00
|
|
|
mutex sync.Mutex // each item must be received atomically
|
|
|
|
|
r io.Reader // source of the data
|
|
|
|
|
wireType map[typeId]*wireType // map from remote ID to local description
|
|
|
|
|
decoderCache map[reflect.Type]map[typeId]**decEngine // cache of compiled engines
|
|
|
|
|
ignorerCache map[typeId]**decEngine // ditto for ignored objects
|
|
|
|
|
state *decodeState // reads data from in-memory buffer
|
|
|
|
|
countState *decodeState // reads counts from wire
|
|
|
|
|
buf []byte
|
|
|
|
|
countBuf [9]byte // counts may be uint64s (unlikely!), require 9 bytes
|
2010-10-22 11:17:40 -07:00
|
|
|
byteBuffer *bytes.Buffer
|
2010-10-22 15:16:34 -07:00
|
|
|
err os.Error
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
// NewDecoder returns a new decoder that reads from the io.Reader.
|
2009-07-11 15:45:54 -07:00
|
|
|
func NewDecoder(r io.Reader) *Decoder {
|
2009-12-15 15:35:38 -08:00
|
|
|
dec := new(Decoder)
|
|
|
|
|
dec.r = r
|
|
|
|
|
dec.wireType = make(map[typeId]*wireType)
|
2010-10-31 13:41:30 -07:00
|
|
|
dec.state = newDecodeState(dec, &dec.byteBuffer) // buffer set in Decode()
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
|
|
|
|
|
dec.ignorerCache = make(map[typeId]**decEngine)
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-12-15 15:35:38 -08:00
|
|
|
return dec
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2010-10-22 11:17:40 -07:00
|
|
|
// recvType loads the definition of a type and reloads the Decoder's buffer.
|
2009-07-27 11:02:06 -07:00
|
|
|
func (dec *Decoder) recvType(id typeId) {
|
2009-07-11 15:45:54 -07:00
|
|
|
// Have we already seen this type? That's an error
|
2010-03-30 10:51:11 -07:00
|
|
|
if dec.wireType[id] != nil {
|
2010-10-22 15:16:34 -07:00
|
|
|
dec.err = os.ErrorString("gob: duplicate type received")
|
2009-12-15 15:35:38 -08:00
|
|
|
return
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Type:
|
2009-12-15 15:35:38 -08:00
|
|
|
wire := new(wireType)
|
2010-10-22 15:16:34 -07:00
|
|
|
dec.err = dec.decode(tWireType, reflect.NewValue(wire))
|
|
|
|
|
if dec.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2009-07-11 15:45:54 -07:00
|
|
|
// Remember we've seen this type.
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.wireType[id] = wire
|
2010-10-22 11:17:40 -07:00
|
|
|
|
|
|
|
|
// Load the next parcel.
|
|
|
|
|
dec.recv()
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
// Decode reads the next value from the connection and stores
|
|
|
|
|
// it in the data represented by the empty interface value.
|
2009-07-11 15:45:54 -07:00
|
|
|
// The value underlying e must be the correct type for the next
|
2010-06-29 10:14:32 -07:00
|
|
|
// data item received, and must be a pointer.
|
2009-07-11 15:45:54 -07:00
|
|
|
func (dec *Decoder) Decode(e interface{}) os.Error {
|
2010-06-29 10:14:32 -07:00
|
|
|
value := reflect.NewValue(e)
|
|
|
|
|
// If e represents a value as opposed to a pointer, the answer won't
|
|
|
|
|
// get back to the caller. Make sure it's a pointer.
|
|
|
|
|
if value.Type().Kind() != reflect.Ptr {
|
2010-10-22 15:16:34 -07:00
|
|
|
dec.err = os.ErrorString("gob: attempt to decode into a non-pointer")
|
|
|
|
|
return dec.err
|
2009-12-01 21:47:00 -08:00
|
|
|
}
|
2010-06-29 10:14:32 -07:00
|
|
|
return dec.DecodeValue(value)
|
2010-06-28 17:11:54 -07:00
|
|
|
}
|
2009-12-01 21:47:00 -08:00
|
|
|
|
2010-10-22 11:17:40 -07:00
|
|
|
// recv reads the next count-delimited item from the input. It is the converse
|
|
|
|
|
// of Encoder.send.
|
|
|
|
|
func (dec *Decoder) recv() {
|
|
|
|
|
// Read a count.
|
|
|
|
|
var nbytes uint64
|
2010-10-22 15:16:34 -07:00
|
|
|
nbytes, dec.err = decodeUintReader(dec.r, dec.countBuf[0:])
|
|
|
|
|
if dec.err != nil {
|
2010-10-22 11:17:40 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Allocate the buffer.
|
|
|
|
|
if nbytes > uint64(len(dec.buf)) {
|
|
|
|
|
dec.buf = make([]byte, nbytes+1000)
|
|
|
|
|
}
|
|
|
|
|
dec.byteBuffer = bytes.NewBuffer(dec.buf[0:nbytes])
|
2009-07-16 13:05:46 -07:00
|
|
|
|
2010-10-22 11:17:40 -07:00
|
|
|
// Read the data
|
2010-10-22 15:16:34 -07:00
|
|
|
_, dec.err = io.ReadFull(dec.r, dec.buf[0:nbytes])
|
|
|
|
|
if dec.err != nil {
|
|
|
|
|
if dec.err == os.EOF {
|
|
|
|
|
dec.err = io.ErrUnexpectedEOF
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
2010-10-22 11:17:40 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
|
2010-10-22 11:17:40 -07:00
|
|
|
// decodeValueFromBuffer grabs the next value from the input. The Decoder's
|
|
|
|
|
// buffer already contains data. If the next item in the buffer is a type
|
|
|
|
|
// descriptor, it may be necessary to reload the buffer, but recvType does that.
|
2010-10-31 13:41:30 -07:00
|
|
|
func (dec *Decoder) decodeValueFromBuffer(value reflect.Value, ignoreInterfaceValue, countPresent bool) {
|
2010-10-22 11:17:40 -07:00
|
|
|
for dec.state.b.Len() > 0 {
|
2009-07-11 15:45:54 -07:00
|
|
|
// Receive a type id.
|
2011-01-11 17:56:45 -08:00
|
|
|
id := typeId(dec.state.decodeInt())
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-07-16 17:55:16 -07:00
|
|
|
// Is it a new type?
|
2009-12-15 15:35:38 -08:00
|
|
|
if id < 0 { // 0 is the error state, handled above
|
2009-11-05 17:02:55 -08:00
|
|
|
// If the id is negative, we have a type.
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.recvType(-id)
|
2010-10-22 15:16:34 -07:00
|
|
|
if dec.err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
break
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
continue
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-28 14:09:47 -07:00
|
|
|
// Make sure the type has been defined already or is a builtin type (for
|
|
|
|
|
// top-level singleton values).
|
|
|
|
|
if dec.wireType[id] == nil && builtinIdToType[id] == nil {
|
2010-10-22 15:16:34 -07:00
|
|
|
dec.err = errBadType
|
2009-12-15 15:35:38 -08:00
|
|
|
break
|
2009-11-16 23:32:30 -08:00
|
|
|
}
|
2010-10-31 13:41:30 -07:00
|
|
|
// An interface value is preceded by a byte count.
|
|
|
|
|
if countPresent {
|
2011-01-11 17:56:45 -08:00
|
|
|
count := int(dec.state.decodeUint())
|
2010-10-31 13:41:30 -07:00
|
|
|
if ignoreInterfaceValue {
|
|
|
|
|
// An interface value is preceded by a byte count. Just skip that many bytes.
|
|
|
|
|
dec.state.b.Next(int(count))
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
// Otherwise fall through and decode it.
|
|
|
|
|
}
|
2010-10-22 15:16:34 -07:00
|
|
|
dec.err = dec.decode(id, value)
|
2009-12-15 15:35:38 -08:00
|
|
|
break
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DecodeValue reads the next value from the connection and stores
|
|
|
|
|
// it in the data represented by the reflection value.
|
|
|
|
|
// The value must be the correct type for the next
|
|
|
|
|
// data item received.
|
|
|
|
|
func (dec *Decoder) DecodeValue(value reflect.Value) os.Error {
|
|
|
|
|
// Make sure we're single-threaded through here.
|
|
|
|
|
dec.mutex.Lock()
|
|
|
|
|
defer dec.mutex.Unlock()
|
|
|
|
|
|
2010-10-22 15:16:34 -07:00
|
|
|
dec.err = nil
|
2010-10-22 11:17:40 -07:00
|
|
|
dec.recv()
|
2010-10-22 15:16:34 -07:00
|
|
|
if dec.err != nil {
|
|
|
|
|
return dec.err
|
2010-10-22 11:17:40 -07:00
|
|
|
}
|
2010-10-31 13:41:30 -07:00
|
|
|
dec.decodeValueFromBuffer(value, false, false)
|
2010-10-22 15:16:34 -07:00
|
|
|
return dec.err
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
2010-10-29 15:07:56 -07:00
|
|
|
|
2010-10-29 15:52:25 -07:00
|
|
|
// If debug.go is compiled into the program , debugFunc prints a human-readable
|
|
|
|
|
// representation of the gob data read from r by calling that file's Debug function.
|
|
|
|
|
// Otherwise it is nil.
|
2010-10-29 15:07:56 -07:00
|
|
|
var debugFunc func(io.Reader)
|