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-07-15 16:10:17 -07:00
|
|
|
"bytes";
|
2009-07-11 15:45:54 -07:00
|
|
|
"io";
|
|
|
|
|
"os";
|
2009-11-16 23:32:30 -08:00
|
|
|
"reflect";
|
2009-07-11 15:45:54 -07:00
|
|
|
"sync";
|
|
|
|
|
)
|
|
|
|
|
|
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-11-16 23:32:30 -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
|
2009-10-06 19:41:51 -07:00
|
|
|
buf []byte;
|
|
|
|
|
oneByte []byte;
|
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 {
|
|
|
|
|
dec := new(Decoder);
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.r = r;
|
2009-11-16 23:32:30 -08:00
|
|
|
dec.wireType = make(map[typeId]*wireType);
|
2009-07-28 17:20:19 -07:00
|
|
|
dec.state = newDecodeState(nil); // buffer set in Decode(); rest is unimportant
|
2009-11-16 23:32:30 -08:00
|
|
|
dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine);
|
|
|
|
|
dec.ignorerCache = make(map[typeId]**decEngine);
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.oneByte = make([]byte, 1);
|
2009-07-11 15:45:54 -07:00
|
|
|
|
|
|
|
|
return dec;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2009-11-16 23:32:30 -08:00
|
|
|
if _, alreadySeen := dec.wireType[id]; alreadySeen {
|
2009-07-11 15:45:54 -07:00
|
|
|
dec.state.err = os.ErrorString("gob: duplicate type received");
|
2009-10-06 19:41:51 -07:00
|
|
|
return;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Type:
|
|
|
|
|
wire := new(wireType);
|
2009-11-16 23:32:30 -08:00
|
|
|
dec.state.err = dec.decode(tWireType, wire);
|
2009-07-11 15:45:54 -07:00
|
|
|
// Remember we've seen this type.
|
2009-11-16 23:32:30 -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
|
2009-07-27 11:02:06 -07:00
|
|
|
// data item received.
|
2009-07-11 15:45:54 -07:00
|
|
|
func (dec *Decoder) Decode(e interface{}) os.Error {
|
|
|
|
|
// Make sure we're single-threaded through here.
|
2009-07-27 11:02:06 -07:00
|
|
|
dec.mutex.Lock();
|
|
|
|
|
defer dec.mutex.Unlock();
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.state.err = nil;
|
|
|
|
|
for {
|
|
|
|
|
// Read a count.
|
2009-07-29 15:10:29 -07:00
|
|
|
var nbytes uint64;
|
|
|
|
|
nbytes, dec.state.err = decodeUintReader(dec.r, dec.oneByte);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
dec.state.b = bytes.NewBuffer(dec.buf[0:nbytes]);
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// Read the data
|
2009-09-14 17:20:29 -07: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-07-29 15:10:29 -07:00
|
|
|
break;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-11 15:45:54 -07:00
|
|
|
// Receive a type id.
|
2009-07-27 11:02:06 -07: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-07-15 16:10:17 -07: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-07-15 16:10:17 -07:00
|
|
|
dec.recvType(-id);
|
|
|
|
|
if dec.state.err != nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
break
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
|
|
|
|
continue;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 13:05:46 -07:00
|
|
|
// No, it's a value.
|
2009-11-16 23:32:30 -08:00
|
|
|
// Make sure the type has been defined already.
|
|
|
|
|
_, ok := dec.wireType[id];
|
|
|
|
|
if !ok {
|
|
|
|
|
dec.state.err = errBadType;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
dec.state.err = dec.decode(id, e);
|
2009-07-16 23:01:10 -07:00
|
|
|
break;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return dec.state.err;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|