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
|
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)
|
|
|
|
|
dec.state = newDecodeState(nil) // buffer set in Decode(); rest is unimportant
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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 {
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.state.err = os.ErrorString("gob: duplicate type received")
|
|
|
|
|
return
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Type:
|
2009-12-15 15:35:38 -08:00
|
|
|
wire := new(wireType)
|
2010-06-28 17:11:54 -07:00
|
|
|
dec.state.err = dec.decode(tWireType, reflect.NewValue(wire))
|
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
|
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-28 17:11:54 -07:00
|
|
|
// data item received, which must be a pointer.
|
2009-07-11 15:45:54 -07:00
|
|
|
func (dec *Decoder) Decode(e interface{}) os.Error {
|
2009-12-01 21:47:00 -08:00
|
|
|
// If e represents a value, the answer won't get back to the
|
|
|
|
|
// caller. Make sure it's a pointer.
|
|
|
|
|
if _, ok := reflect.Typeof(e).(*reflect.PtrType); !ok {
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.state.err = os.ErrorString("gob: attempt to decode into a non-pointer")
|
|
|
|
|
return dec.state.err
|
2009-12-01 21:47:00 -08:00
|
|
|
}
|
2010-06-28 17:11:54 -07:00
|
|
|
return dec.DecodeValue(reflect.NewValue(e))
|
|
|
|
|
}
|
2009-12-01 21:47:00 -08:00
|
|
|
|
2010-06-28 17:11:54 -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 {
|
2009-07-11 15:45:54 -07:00
|
|
|
// Make sure we're single-threaded through here.
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.mutex.Lock()
|
|
|
|
|
defer dec.mutex.Unlock()
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.state.err = nil
|
2009-07-15 16:10:17 -07:00
|
|
|
for {
|
|
|
|
|
// Read a count.
|
2009-12-15 15:35:38 -08:00
|
|
|
var nbytes uint64
|
|
|
|
|
nbytes, dec.state.err = decodeUintReader(dec.r, dec.countBuf[0:])
|
2009-07-29 15:10:29 -07:00
|
|
|
if dec.state.err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
break
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
2009-07-16 13:05:46 -07:00
|
|
|
// Allocate the buffer.
|
|
|
|
|
if nbytes > uint64(len(dec.buf)) {
|
2009-11-09 12:07:39 -08:00
|
|
|
dec.buf = make([]byte, nbytes+1000)
|
2009-07-16 13:05:46 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.state.b = bytes.NewBuffer(dec.buf[0:nbytes])
|
2009-07-16 13:05:46 -07:00
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// Read the data
|
2009-12-15 15:35:38 -08:00
|
|
|
_, dec.state.err = io.ReadFull(dec.r, dec.buf[0:nbytes])
|
2009-07-29 15:10:29 -07:00
|
|
|
if dec.state.err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
if dec.state.err == os.EOF {
|
2009-11-09 12:07:39 -08:00
|
|
|
dec.state.err = io.ErrUnexpectedEOF
|
2009-07-29 15:24:42 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
break
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-11 15:45:54 -07:00
|
|
|
// Receive a type id.
|
2009-12-15 15:35:38 -08:00
|
|
|
id := typeId(decodeInt(dec.state))
|
2009-07-15 16:10:17 -07:00
|
|
|
if dec.state.err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
break
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
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)
|
2009-07-15 16:10:17 -07:00
|
|
|
if dec.state.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
|
|
|
}
|
|
|
|
|
|
2009-07-16 13:05:46 -07:00
|
|
|
// No, it's a value.
|
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 {
|
2009-12-15 15:35:38 -08:00
|
|
|
dec.state.err = errBadType
|
|
|
|
|
break
|
2009-11-16 23:32:30 -08:00
|
|
|
}
|
2010-06-28 17:11:54 -07:00
|
|
|
dec.state.err = dec.decode(id, value)
|
2009-12-15 15:35:38 -08:00
|
|
|
break
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
2009-12-15 15:35:38 -08:00
|
|
|
return dec.state.err
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|