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
|
|
|
"gob";
|
|
|
|
|
"io";
|
|
|
|
|
"os";
|
|
|
|
|
"reflect";
|
|
|
|
|
"sync";
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Decoder struct {
|
|
|
|
|
sync.Mutex; // each item must be received atomically
|
2009-07-15 16:10:17 -07:00
|
|
|
r io.Reader; // source of the data
|
2009-07-11 15:45:54 -07:00
|
|
|
seen map[TypeId] *wireType; // which types we've already seen described
|
2009-07-15 16:10:17 -07:00
|
|
|
state *decodeState; // reads data from in-memory buffer
|
|
|
|
|
countState *decodeState; // reads counts from wire
|
2009-07-16 13:05:46 -07:00
|
|
|
buf []byte;
|
2009-07-15 16:10:17 -07:00
|
|
|
oneByte []byte;
|
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-07-11 15:45:54 -07:00
|
|
|
dec.seen = make(map[TypeId] *wireType);
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.state = new(decodeState); // buffer set in Decode(); rest is unimportant
|
|
|
|
|
dec.oneByte = make([]byte, 1);
|
2009-07-11 15:45:54 -07:00
|
|
|
|
|
|
|
|
return dec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dec *Decoder) recvType(id TypeId) {
|
|
|
|
|
// Have we already seen this type? That's an error
|
|
|
|
|
if wt_, alreadySeen := dec.seen[id]; alreadySeen {
|
|
|
|
|
dec.state.err = os.ErrorString("gob: duplicate type received");
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Type:
|
|
|
|
|
wire := new(wireType);
|
2009-07-15 16:10:17 -07:00
|
|
|
decode(dec.state.b, wire);
|
2009-07-11 15:45:54 -07:00
|
|
|
// Remember we've seen this type.
|
|
|
|
|
dec.seen[id] = wire;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The value underlying e must be the correct type for the next
|
|
|
|
|
// value to be received for this decoder.
|
|
|
|
|
func (dec *Decoder) Decode(e interface{}) os.Error {
|
|
|
|
|
rt, indir := indirect(reflect.Typeof(e));
|
|
|
|
|
|
|
|
|
|
// Make sure we're single-threaded through here.
|
|
|
|
|
dec.Lock();
|
|
|
|
|
defer dec.Unlock();
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.state.err = nil;
|
|
|
|
|
for {
|
|
|
|
|
// Read a count.
|
|
|
|
|
nbytes, err := decodeUintReader(dec.r, dec.oneByte);
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-16 13:05:46 -07:00
|
|
|
// Allocate the buffer.
|
|
|
|
|
if nbytes > uint64(len(dec.buf)) {
|
|
|
|
|
dec.buf = make([]byte, nbytes + 1000);
|
|
|
|
|
}
|
|
|
|
|
dec.state.b = bytes.NewBuffer(dec.buf[0:nbytes]);
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// Read the data
|
|
|
|
|
var n int;
|
2009-07-16 13:05:46 -07:00
|
|
|
n, err = dec.r.Read(dec.buf[0:nbytes]);
|
2009-07-15 16:10:17 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
if n < int(nbytes) {
|
|
|
|
|
return os.ErrorString("gob decode: short read");
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-11 15:45:54 -07:00
|
|
|
// Receive a type id.
|
2009-07-15 16:10:17 -07:00
|
|
|
id := TypeId(decodeInt(dec.state));
|
|
|
|
|
if dec.state.err != nil {
|
|
|
|
|
return dec.state.err
|
|
|
|
|
}
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-07-16 13:05:46 -07:00
|
|
|
// Is it a type?
|
2009-07-15 16:10:17 -07:00
|
|
|
if id < 0 { // 0 is the error state, handled above
|
|
|
|
|
// If the id is negative, we have a type.
|
|
|
|
|
dec.recvType(-id);
|
|
|
|
|
if dec.state.err != nil {
|
|
|
|
|
return dec.state.err
|
|
|
|
|
}
|
|
|
|
|
continue;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 13:05:46 -07:00
|
|
|
// No, it's a value.
|
2009-07-15 16:10:17 -07:00
|
|
|
info := getTypeInfo(rt);
|
|
|
|
|
|
|
|
|
|
// Check type compatibility.
|
|
|
|
|
// TODO(r): need to make the decoder work correctly if the wire type is compatible
|
|
|
|
|
// but not equal to the local type (e.g, extra fields).
|
|
|
|
|
if info.wire.name() != dec.seen[id].name() {
|
|
|
|
|
dec.state.err = os.ErrorString("gob decode: incorrect type for wire value: want " + info.wire.name() + "; received " + dec.seen[id].name());
|
|
|
|
|
return dec.state.err
|
|
|
|
|
}
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// Receive a value.
|
|
|
|
|
decode(dec.state.b, e);
|
2009-07-11 15:45:54 -07:00
|
|
|
|
|
|
|
|
return dec.state.err
|
|
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
return nil // silence compiler
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|