2009-06-29 15:15:07 -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
|
|
|
|
|
|
2009-07-01 23:04:27 -07:00
|
|
|
// TODO(rsc): When garbage collector changes, revisit
|
|
|
|
|
// the allocations in this file that use unsafe.Pointer.
|
|
|
|
|
|
2009-06-29 15:15:07 -07:00
|
|
|
import (
|
2009-07-15 16:10:17 -07:00
|
|
|
"bytes";
|
2009-06-29 15:15:07 -07:00
|
|
|
"io";
|
2009-06-30 17:59:41 -07:00
|
|
|
"math";
|
2009-06-29 15:15:07 -07:00
|
|
|
"os";
|
2009-07-01 18:25:13 -07:00
|
|
|
"reflect";
|
2009-06-30 16:20:31 -07:00
|
|
|
"unsafe";
|
2009-06-29 15:15:07 -07:00
|
|
|
)
|
|
|
|
|
|
2009-07-16 23:01:10 -07:00
|
|
|
var (
|
2009-10-06 19:41:51 -07:00
|
|
|
errBadUint = os.ErrorString("gob: encoded unsigned integer out of range");
|
|
|
|
|
errBadType = os.ErrorString("gob: unknown type id or corrupted data");
|
|
|
|
|
errRange = os.ErrorString("gob: internal error: field numbers out of bounds");
|
|
|
|
|
errNotStruct = os.ErrorString("gob: TODO: can only handle structs");
|
2009-07-16 23:01:10 -07:00
|
|
|
)
|
|
|
|
|
|
2009-06-30 16:20:31 -07:00
|
|
|
// The global execution state of an instance of the decoder.
|
2009-07-15 16:10:17 -07:00
|
|
|
type decodeState struct {
|
2009-10-06 19:41:51 -07:00
|
|
|
b *bytes.Buffer;
|
|
|
|
|
err os.Error;
|
2009-07-01 18:25:13 -07:00
|
|
|
fieldnum int; // the last field number read.
|
2009-10-06 19:41:51 -07:00
|
|
|
buf []byte;
|
2009-07-28 17:20:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newDecodeState(b *bytes.Buffer) *decodeState {
|
|
|
|
|
d := new(decodeState);
|
|
|
|
|
d.b = b;
|
|
|
|
|
d.buf = make([]byte, uint64Size);
|
|
|
|
|
return d;
|
2009-06-30 16:20:31 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-28 12:59:39 -07:00
|
|
|
func overflow(name string) os.ErrorString {
|
2009-08-12 13:19:27 -07:00
|
|
|
return os.ErrorString(`value for "` + name + `" out of range`);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// decodeUintReader reads an encoded unsigned integer from an io.Reader.
|
|
|
|
|
// Used only by the Decoder to read the message length.
|
2009-07-28 17:20:19 -07:00
|
|
|
func decodeUintReader(r io.Reader, buf []byte) (x uint64, err os.Error) {
|
2009-09-15 09:41:59 -07:00
|
|
|
_, err = r.Read(buf[0:1]);
|
2009-07-28 17:20:19 -07:00
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return;
|
2009-07-28 17:20:19 -07:00
|
|
|
}
|
|
|
|
|
b := buf[0];
|
|
|
|
|
if b <= 0x7f {
|
2009-10-06 19:41:51 -07:00
|
|
|
return uint64(b), nil;
|
2009-07-28 17:20:19 -07:00
|
|
|
}
|
|
|
|
|
nb := -int(int8(b));
|
|
|
|
|
if nb > uint64Size {
|
|
|
|
|
err = errBadUint;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var n int;
|
|
|
|
|
n, err = io.ReadFull(r, buf[0:nb]);
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == os.EOF {
|
2009-10-06 19:41:51 -07:00
|
|
|
err = io.ErrUnexpectedEOF;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return;
|
2009-07-28 17:20:19 -07:00
|
|
|
}
|
|
|
|
|
// Could check that the high byte is zero but it's not worth it.
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
x <<= 8;
|
|
|
|
|
x |= uint64(buf[i]);
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// decodeUint reads an encoded unsigned integer from state.r.
|
2009-06-30 16:20:31 -07:00
|
|
|
// Sets state.err. If state.err is already non-nil, it does nothing.
|
2009-07-28 12:59:39 -07:00
|
|
|
// Does not check for overflow.
|
2009-07-15 16:10:17 -07:00
|
|
|
func decodeUint(state *decodeState) (x uint64) {
|
2009-06-30 16:20:31 -07:00
|
|
|
if state.err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return;
|
2009-06-30 16:20:31 -07:00
|
|
|
}
|
2009-07-28 17:20:19 -07:00
|
|
|
var b uint8;
|
|
|
|
|
b, state.err = state.b.ReadByte();
|
|
|
|
|
if b <= 0x7f { // includes state.err != nil
|
2009-10-06 19:41:51 -07:00
|
|
|
return uint64(b);
|
2009-07-28 17:20:19 -07:00
|
|
|
}
|
|
|
|
|
nb := -int(int8(b));
|
|
|
|
|
if nb > uint64Size {
|
|
|
|
|
state.err = errBadUint;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var n int;
|
|
|
|
|
n, state.err = state.b.Read(state.buf[0:nb]);
|
|
|
|
|
// Don't need to check error; it's safe to loop regardless.
|
|
|
|
|
// Could check that the high byte is zero but it's not worth it.
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
x <<= 8;
|
|
|
|
|
x |= uint64(state.buf[i]);
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
2009-06-30 16:20:31 -07:00
|
|
|
return x;
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// decodeInt reads an encoded signed integer from state.r.
|
2009-06-30 16:20:31 -07:00
|
|
|
// Sets state.err. If state.err is already non-nil, it does nothing.
|
2009-07-28 12:59:39 -07:00
|
|
|
// Does not check for overflow.
|
2009-07-15 16:10:17 -07:00
|
|
|
func decodeInt(state *decodeState) int64 {
|
|
|
|
|
x := decodeUint(state);
|
2009-06-30 16:20:31 -07:00
|
|
|
if state.err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return 0;
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
if x&1 != 0 {
|
|
|
|
|
return ^int64(x>>1);
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return int64(x>>1);
|
2009-06-29 15:15:07 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
|
2009-10-06 19:41:51 -07:00
|
|
|
type decOp func(i *decInstr, state *decodeState, p unsafe.Pointer)
|
2009-07-01 23:04:27 -07:00
|
|
|
|
2009-06-30 17:59:41 -07:00
|
|
|
// The 'instructions' of the decoding machine
|
|
|
|
|
type decInstr struct {
|
2009-07-01 23:04:27 -07:00
|
|
|
op decOp;
|
2009-10-06 19:41:51 -07:00
|
|
|
field int; // field number of the wire type
|
|
|
|
|
indir int; // how many pointer indirections to reach the value in the struct
|
2009-06-30 17:59:41 -07:00
|
|
|
offset uintptr; // offset in the structure of the field to encode
|
2009-07-28 12:59:39 -07:00
|
|
|
ovfl os.ErrorString; // error message for overflow/underflow (for arrays, of the elements)
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Since the encoder writes no zeros, if we arrive at a decoder we have
|
|
|
|
|
// a value to extract and store. The field number has already been read
|
|
|
|
|
// (it's how we knew to call this decoder).
|
|
|
|
|
// Each decoder is responsible for handling any indirections associated
|
|
|
|
|
// with the data structure. If any pointer so reached is nil, allocation must
|
|
|
|
|
// be done.
|
|
|
|
|
|
|
|
|
|
// Walk the pointer hierarchy, allocating if we find a nil. Stop one before the end.
|
|
|
|
|
func decIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
|
|
|
|
|
for ; indir > 1; indir-- {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
|
|
|
|
// Allocation required
|
|
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(unsafe.Pointer));
|
|
|
|
|
}
|
|
|
|
|
p = *(*unsafe.Pointer)(p);
|
|
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return p;
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 17:55:16 -07:00
|
|
|
func ignoreUint(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
|
|
|
|
decodeUint(state);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decBool(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(bool));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
*(*bool)(p) = decodeInt(state) != 0;
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decInt8(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(int8));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
v := decodeInt(state);
|
|
|
|
|
if v < math.MinInt8 || math.MaxInt8 < v {
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = i.ovfl;
|
2009-07-28 12:59:39 -07:00
|
|
|
} else {
|
2009-10-06 19:41:51 -07:00
|
|
|
*(*int8)(p) = int8(v);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decUint8(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint8));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
v := decodeUint(state);
|
|
|
|
|
if math.MaxUint8 < v {
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = i.ovfl;
|
2009-07-28 12:59:39 -07:00
|
|
|
} else {
|
2009-10-06 19:41:51 -07:00
|
|
|
*(*uint8)(p) = uint8(v);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decInt16(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(int16));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
v := decodeInt(state);
|
|
|
|
|
if v < math.MinInt16 || math.MaxInt16 < v {
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = i.ovfl;
|
2009-07-28 12:59:39 -07:00
|
|
|
} else {
|
2009-10-06 19:41:51 -07:00
|
|
|
*(*int16)(p) = int16(v);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decUint16(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint16));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
v := decodeUint(state);
|
|
|
|
|
if math.MaxUint16 < v {
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = i.ovfl;
|
2009-07-28 12:59:39 -07:00
|
|
|
} else {
|
2009-10-06 19:41:51 -07:00
|
|
|
*(*uint16)(p) = uint16(v);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decInt32(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(int32));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
v := decodeInt(state);
|
|
|
|
|
if v < math.MinInt32 || math.MaxInt32 < v {
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = i.ovfl;
|
2009-07-28 12:59:39 -07:00
|
|
|
} else {
|
2009-10-06 19:41:51 -07:00
|
|
|
*(*int32)(p) = int32(v);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decUint32(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint32));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
v := decodeUint(state);
|
|
|
|
|
if math.MaxUint32 < v {
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = i.ovfl;
|
2009-07-28 12:59:39 -07:00
|
|
|
} else {
|
2009-10-06 19:41:51 -07:00
|
|
|
*(*uint32)(p) = uint32(v);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decInt64(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(int64));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
*(*int64)(p) = int64(decodeInt(state));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decUint64(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(uint64));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
*(*uint64)(p) = uint64(decodeUint(state));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Floating-point numbers are transmitted as uint64s holding the bits
|
|
|
|
|
// of the underlying representation. They are sent byte-reversed, with
|
|
|
|
|
// the exponent end coming out first, so integer floating point numbers
|
|
|
|
|
// (for example) transmit more compactly. This routine does the
|
|
|
|
|
// unswizzling.
|
|
|
|
|
func floatFromBits(u uint64) float64 {
|
|
|
|
|
var v uint64;
|
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
|
v <<= 8;
|
2009-10-06 19:41:51 -07:00
|
|
|
v |= u&0xFF;
|
2009-06-30 17:59:41 -07:00
|
|
|
u >>= 8;
|
|
|
|
|
}
|
|
|
|
|
return math.Float64frombits(v);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decFloat32(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(float32));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
v := floatFromBits(decodeUint(state));
|
|
|
|
|
av := v;
|
|
|
|
|
if av < 0 {
|
2009-10-06 19:41:51 -07:00
|
|
|
av = -av;
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
|
|
|
|
if math.MaxFloat32 < av { // underflow is OK
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = i.ovfl;
|
2009-07-28 12:59:39 -07:00
|
|
|
} else {
|
2009-10-06 19:41:51 -07:00
|
|
|
*(*float32)(p) = float32(v);
|
2009-07-28 12:59:39 -07:00
|
|
|
}
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decFloat64(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-06-30 17:59:41 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
2009-07-01 23:04:27 -07:00
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new(float64));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-02 08:21:42 -07:00
|
|
|
p = *(*unsafe.Pointer)(p);
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
*(*float64)(p) = floatFromBits(uint64(decodeUint(state)));
|
2009-06-30 17:59:41 -07:00
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
|
2009-07-02 11:21:01 -07:00
|
|
|
// uint8 arrays are encoded as an unsigned count followed by the raw bytes.
|
2009-07-15 16:10:17 -07:00
|
|
|
func decUint8Array(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-07-02 11:21:01 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
|
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new([]uint8));
|
|
|
|
|
}
|
|
|
|
|
p = *(*unsafe.Pointer)(p);
|
|
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
b := make([]uint8, decodeUint(state));
|
|
|
|
|
state.b.Read(b);
|
2009-07-02 11:21:01 -07:00
|
|
|
*(*[]uint8)(p) = b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Strings are encoded as an unsigned count followed by the raw bytes.
|
2009-07-15 16:10:17 -07:00
|
|
|
func decString(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-07-02 11:21:01 -07:00
|
|
|
if i.indir > 0 {
|
|
|
|
|
if *(*unsafe.Pointer)(p) == nil {
|
|
|
|
|
*(*unsafe.Pointer)(p) = unsafe.Pointer(new([]byte));
|
|
|
|
|
}
|
|
|
|
|
p = *(*unsafe.Pointer)(p);
|
|
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
b := make([]byte, decodeUint(state));
|
|
|
|
|
state.b.Read(b);
|
2009-07-02 11:21:01 -07:00
|
|
|
*(*string)(p) = string(b);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-16 17:55:16 -07:00
|
|
|
func ignoreUint8Array(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
|
|
|
|
b := make([]byte, decodeUint(state));
|
|
|
|
|
state.b.Read(b);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 18:25:13 -07:00
|
|
|
// Execution engine
|
|
|
|
|
|
|
|
|
|
// The encoder engine is an array of instructions indexed by field number of the incoming
|
|
|
|
|
// data. It is executed with random access according to field number.
|
|
|
|
|
type decEngine struct {
|
2009-10-06 19:41:51 -07:00
|
|
|
instr []decInstr;
|
2009-07-16 17:55:16 -07:00
|
|
|
numInstr int; // the number of active instructions
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
func decodeStruct(engine *decEngine, rtyp *reflect.StructType, b *bytes.Buffer, p uintptr, indir int) os.Error {
|
2009-07-02 16:43:46 -07:00
|
|
|
if indir > 0 {
|
|
|
|
|
up := unsafe.Pointer(p);
|
|
|
|
|
if *(*unsafe.Pointer)(up) == nil {
|
|
|
|
|
// Allocate the structure by making a slice of bytes and recording the
|
|
|
|
|
// address of the beginning of the array. TODO(rsc).
|
|
|
|
|
b := make([]byte, rtyp.Size());
|
|
|
|
|
*(*unsafe.Pointer)(up) = unsafe.Pointer(&b[0]);
|
|
|
|
|
}
|
|
|
|
|
p = *(*uintptr)(up);
|
|
|
|
|
}
|
2009-07-28 17:20:19 -07:00
|
|
|
state := newDecodeState(b);
|
2009-07-02 13:43:47 -07:00
|
|
|
state.fieldnum = -1;
|
2009-07-02 16:43:46 -07:00
|
|
|
basep := p;
|
2009-07-02 13:43:47 -07:00
|
|
|
for state.err == nil {
|
2009-07-15 16:10:17 -07:00
|
|
|
delta := int(decodeUint(state));
|
2009-07-06 10:58:55 -07:00
|
|
|
if delta < 0 {
|
|
|
|
|
state.err = os.ErrorString("gob decode: corrupted data: negative delta");
|
2009-10-06 19:41:51 -07:00
|
|
|
break;
|
2009-07-06 10:58:55 -07:00
|
|
|
}
|
2009-07-02 13:43:47 -07:00
|
|
|
if state.err != nil || delta == 0 { // struct terminator is zero delta fieldnum
|
2009-10-06 19:41:51 -07:00
|
|
|
break;
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
|
|
|
|
fieldnum := state.fieldnum + delta;
|
|
|
|
|
if fieldnum >= len(engine.instr) {
|
2009-07-27 11:02:06 -07:00
|
|
|
state.err = errRange;
|
2009-07-16 23:01:10 -07:00
|
|
|
break;
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
|
|
|
|
instr := &engine.instr[fieldnum];
|
2009-10-06 19:41:51 -07:00
|
|
|
p := unsafe.Pointer(basep + instr.offset);
|
2009-07-02 13:43:47 -07:00
|
|
|
if instr.indir > 1 {
|
|
|
|
|
p = decIndirect(p, instr.indir);
|
|
|
|
|
}
|
|
|
|
|
instr.op(instr, state, p);
|
|
|
|
|
state.fieldnum = fieldnum;
|
|
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return state.err;
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-17 11:38:31 -07:00
|
|
|
func ignoreStruct(engine *decEngine, b *bytes.Buffer) os.Error {
|
2009-07-28 17:20:19 -07:00
|
|
|
state := newDecodeState(b);
|
2009-07-17 11:38:31 -07:00
|
|
|
state.fieldnum = -1;
|
|
|
|
|
for state.err == nil {
|
|
|
|
|
delta := int(decodeUint(state));
|
|
|
|
|
if delta < 0 {
|
|
|
|
|
state.err = os.ErrorString("gob ignore decode: corrupted data: negative delta");
|
2009-10-06 19:41:51 -07:00
|
|
|
break;
|
2009-07-17 11:38:31 -07:00
|
|
|
}
|
|
|
|
|
if state.err != nil || delta == 0 { // struct terminator is zero delta fieldnum
|
2009-10-06 19:41:51 -07:00
|
|
|
break;
|
2009-07-17 11:38:31 -07:00
|
|
|
}
|
|
|
|
|
fieldnum := state.fieldnum + delta;
|
|
|
|
|
if fieldnum >= len(engine.instr) {
|
2009-07-27 11:02:06 -07:00
|
|
|
state.err = errRange;
|
2009-07-17 11:38:31 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
instr := &engine.instr[fieldnum];
|
|
|
|
|
instr.op(instr, state, unsafe.Pointer(nil));
|
|
|
|
|
state.fieldnum = fieldnum;
|
|
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return state.err;
|
2009-07-17 11:38:31 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-28 12:59:39 -07:00
|
|
|
func decodeArrayHelper(state *decodeState, p uintptr, elemOp decOp, elemWid uintptr, length, elemIndir int, ovfl os.ErrorString) os.Error {
|
|
|
|
|
instr := &decInstr{elemOp, 0, elemIndir, 0, ovfl};
|
2009-07-02 18:02:42 -07:00
|
|
|
for i := 0; i < length && state.err == nil; i++ {
|
|
|
|
|
up := unsafe.Pointer(p);
|
|
|
|
|
if elemIndir > 1 {
|
|
|
|
|
up = decIndirect(up, elemIndir);
|
|
|
|
|
}
|
|
|
|
|
elemOp(instr, state, up);
|
|
|
|
|
p += uintptr(elemWid);
|
|
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return state.err;
|
2009-07-02 18:02:42 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-28 12:59:39 -07:00
|
|
|
func decodeArray(atyp *reflect.ArrayType, state *decodeState, p uintptr, elemOp decOp, elemWid uintptr, length, indir, elemIndir int, ovfl os.ErrorString) os.Error {
|
2009-07-02 16:43:46 -07:00
|
|
|
if indir > 0 {
|
|
|
|
|
up := unsafe.Pointer(p);
|
|
|
|
|
if *(*unsafe.Pointer)(up) == nil {
|
2009-07-02 18:02:42 -07:00
|
|
|
// Allocate the array by making a slice of bytes of the correct size
|
|
|
|
|
// and taking the address of the beginning of the array. TODO(rsc).
|
2009-07-02 16:43:46 -07:00
|
|
|
b := make([]byte, atyp.Size());
|
2009-07-02 18:02:42 -07:00
|
|
|
*(**byte)(up) = &b[0];
|
2009-07-02 16:43:46 -07:00
|
|
|
}
|
|
|
|
|
p = *(*uintptr)(up);
|
|
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
if n := decodeUint(state); n != uint64(length) {
|
|
|
|
|
return os.ErrorString("gob: length mismatch in decodeArray");
|
2009-07-02 16:43:46 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
return decodeArrayHelper(state, p, elemOp, elemWid, length, elemIndir, ovfl);
|
2009-07-02 18:02:42 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 17:55:16 -07:00
|
|
|
func ignoreArrayHelper(state *decodeState, elemOp decOp, length int) os.Error {
|
2009-07-28 12:59:39 -07:00
|
|
|
instr := &decInstr{elemOp, 0, 0, 0, os.ErrorString("no error")};
|
2009-07-16 17:55:16 -07:00
|
|
|
for i := 0; i < length && state.err == nil; i++ {
|
|
|
|
|
elemOp(instr, state, nil);
|
|
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return state.err;
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ignoreArray(state *decodeState, elemOp decOp, length int) os.Error {
|
|
|
|
|
if n := decodeUint(state); n != uint64(length) {
|
|
|
|
|
return os.ErrorString("gob: length mismatch in ignoreArray");
|
|
|
|
|
}
|
|
|
|
|
return ignoreArrayHelper(state, elemOp, length);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-28 12:59:39 -07:00
|
|
|
func decodeSlice(atyp *reflect.SliceType, state *decodeState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.ErrorString) os.Error {
|
2009-07-15 16:10:17 -07:00
|
|
|
length := uintptr(decodeUint(state));
|
2009-07-02 18:02:42 -07:00
|
|
|
if indir > 0 {
|
2009-07-02 17:21:48 -07:00
|
|
|
up := unsafe.Pointer(p);
|
2009-07-02 18:02:42 -07:00
|
|
|
if *(*unsafe.Pointer)(up) == nil {
|
|
|
|
|
// Allocate the slice header.
|
|
|
|
|
*(*unsafe.Pointer)(up) = unsafe.Pointer(new(reflect.SliceHeader));
|
2009-07-02 17:21:48 -07:00
|
|
|
}
|
2009-07-02 18:02:42 -07:00
|
|
|
p = *(*uintptr)(up);
|
2009-07-02 16:43:46 -07:00
|
|
|
}
|
2009-07-02 18:02:42 -07:00
|
|
|
// Allocate storage for the slice elements, that is, the underlying array.
|
2009-10-06 19:41:51 -07:00
|
|
|
data := make([]byte, length * atyp.Elem().Size());
|
2009-07-02 18:02:42 -07:00
|
|
|
// Always write a header at p.
|
|
|
|
|
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(p));
|
|
|
|
|
hdrp.Data = uintptr(unsafe.Pointer(&data[0]));
|
2009-07-27 11:23:49 -07:00
|
|
|
hdrp.Len = int(length);
|
|
|
|
|
hdrp.Cap = int(length);
|
2009-07-28 12:59:39 -07:00
|
|
|
return decodeArrayHelper(state, hdrp.Data, elemOp, elemWid, int(length), elemIndir, ovfl);
|
2009-07-02 16:43:46 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 17:55:16 -07:00
|
|
|
func ignoreSlice(state *decodeState, elemOp decOp) os.Error {
|
|
|
|
|
return ignoreArrayHelper(state, elemOp, int(decodeUint(state)));
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-06 19:41:51 -07:00
|
|
|
var decOpMap = map[reflect.Type]decOp{
|
2009-07-28 12:59:39 -07:00
|
|
|
valueKind(false): decBool,
|
|
|
|
|
valueKind(int8(0)): decInt8,
|
|
|
|
|
valueKind(int16(0)): decInt16,
|
|
|
|
|
valueKind(int32(0)): decInt32,
|
|
|
|
|
valueKind(int64(0)): decInt64,
|
|
|
|
|
valueKind(uint8(0)): decUint8,
|
|
|
|
|
valueKind(uint16(0)): decUint16,
|
|
|
|
|
valueKind(uint32(0)): decUint32,
|
|
|
|
|
valueKind(uint64(0)): decUint64,
|
|
|
|
|
valueKind(float32(0)): decFloat32,
|
|
|
|
|
valueKind(float64(0)): decFloat64,
|
|
|
|
|
valueKind("x"): decString,
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
|
|
|
|
|
2009-10-06 19:41:51 -07:00
|
|
|
var decIgnoreOpMap = map[typeId]decOp{
|
2009-07-16 17:55:16 -07:00
|
|
|
tBool: ignoreUint,
|
|
|
|
|
tInt: ignoreUint,
|
|
|
|
|
tUint: ignoreUint,
|
|
|
|
|
tFloat: ignoreUint,
|
|
|
|
|
tBytes: ignoreUint8Array,
|
|
|
|
|
tString: ignoreUint8Array,
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-07 21:05:24 -07:00
|
|
|
// Return the decoding op for the base type under rt and
|
|
|
|
|
// the indirection count to reach it.
|
2009-07-28 12:59:39 -07:00
|
|
|
func decOpFor(wireId typeId, rt reflect.Type, name string) (decOp, int, os.Error) {
|
2009-07-07 21:05:24 -07:00
|
|
|
typ, indir := indirect(rt);
|
2009-07-07 11:04:42 -07:00
|
|
|
op, ok := decOpMap[reflect.Typeof(typ)];
|
2009-07-02 11:21:01 -07:00
|
|
|
if !ok {
|
|
|
|
|
// Special cases
|
2009-07-07 11:04:42 -07:00
|
|
|
switch t := typ.(type) {
|
|
|
|
|
case *reflect.SliceType:
|
2009-07-28 12:59:39 -07:00
|
|
|
name = "element of " + name;
|
2009-07-07 11:04:42 -07:00
|
|
|
if _, ok := t.Elem().(*reflect.Uint8Type); ok {
|
|
|
|
|
op = decUint8Array;
|
|
|
|
|
break;
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
2009-07-16 17:55:16 -07:00
|
|
|
elemId := wireId.gobType().(*sliceType).Elem;
|
2009-07-28 12:59:39 -07:00
|
|
|
elemOp, elemIndir, err := decOpFor(elemId, t.Elem(), name);
|
2009-07-16 23:01:10 -07:00
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, 0, err;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
ovfl := overflow(name);
|
2009-07-15 16:10:17 -07:00
|
|
|
op = func(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-07-28 12:59:39 -07:00
|
|
|
state.err = decodeSlice(t, state, uintptr(p), elemOp, t.Elem().Size(), i.indir, elemIndir, ovfl);
|
2009-07-07 11:04:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
case *reflect.ArrayType:
|
2009-07-28 12:59:39 -07:00
|
|
|
name = "element of " + name;
|
2009-07-16 17:55:16 -07:00
|
|
|
elemId := wireId.gobType().(*arrayType).Elem;
|
2009-07-28 12:59:39 -07:00
|
|
|
elemOp, elemIndir, err := decOpFor(elemId, t.Elem(), name);
|
2009-07-16 23:01:10 -07:00
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, 0, err;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
ovfl := overflow(name);
|
2009-07-15 16:10:17 -07:00
|
|
|
op = func(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-07-28 12:59:39 -07:00
|
|
|
state.err = decodeArray(t, state, uintptr(p), elemOp, t.Elem().Size(), t.Len(), i.indir, elemIndir, ovfl);
|
2009-07-07 11:04:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
case *reflect.StructType:
|
2009-07-02 13:43:47 -07:00
|
|
|
// Generate a closure that calls out to the engine for the nested type.
|
2009-07-16 23:01:10 -07:00
|
|
|
enginePtr, err := getDecEnginePtr(wireId, typ);
|
|
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, 0, err;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
op = func(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
2009-07-17 11:38:31 -07:00
|
|
|
// indirect through enginePtr to delay evaluation for recursive structs
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = decodeStruct(*enginePtr, t, state.b, uintptr(p), i.indir);
|
2009-07-02 13:43:47 -07:00
|
|
|
};
|
|
|
|
|
}
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
|
|
|
|
if op == nil {
|
2009-07-16 23:01:10 -07:00
|
|
|
return nil, 0, os.ErrorString("gob: decode can't handle type " + rt.String());
|
2009-07-02 11:21:01 -07:00
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return op, indir, nil;
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 17:55:16 -07:00
|
|
|
// Return the decoding op for a field that has no destination.
|
2009-07-27 11:02:06 -07:00
|
|
|
func decIgnoreOpFor(wireId typeId) (decOp, os.Error) {
|
2009-07-16 17:55:16 -07:00
|
|
|
op, ok := decIgnoreOpMap[wireId];
|
|
|
|
|
if !ok {
|
|
|
|
|
// Special cases
|
|
|
|
|
switch t := wireId.gobType().(type) {
|
|
|
|
|
case *sliceType:
|
|
|
|
|
elemId := wireId.gobType().(*sliceType).Elem;
|
2009-07-16 23:01:10 -07:00
|
|
|
elemOp, err := decIgnoreOpFor(elemId);
|
|
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, err;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-16 17:55:16 -07:00
|
|
|
op = func(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
|
|
|
|
state.err = ignoreSlice(state, elemOp);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
case *arrayType:
|
|
|
|
|
elemId := wireId.gobType().(*arrayType).Elem;
|
2009-07-16 23:01:10 -07:00
|
|
|
elemOp, err := decIgnoreOpFor(elemId);
|
|
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, err;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-16 17:55:16 -07:00
|
|
|
op = func(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
|
|
|
|
state.err = ignoreArray(state, elemOp, t.Len);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
case *structType:
|
2009-07-17 11:38:31 -07:00
|
|
|
// Generate a closure that calls out to the engine for the nested type.
|
|
|
|
|
enginePtr, err := getIgnoreEnginePtr(wireId);
|
|
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, err;
|
2009-07-17 11:38:31 -07:00
|
|
|
}
|
|
|
|
|
op = func(i *decInstr, state *decodeState, p unsafe.Pointer) {
|
|
|
|
|
// indirect through enginePtr to delay evaluation for recursive structs
|
2009-10-06 19:41:51 -07:00
|
|
|
state.err = ignoreStruct(*enginePtr, state.b);
|
2009-07-17 11:38:31 -07:00
|
|
|
};
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if op == nil {
|
2009-07-16 23:01:10 -07:00
|
|
|
return nil, os.ErrorString("ignore can't handle type " + wireId.String());
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
2009-07-16 23:01:10 -07:00
|
|
|
return op, nil;
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Are these two gob Types compatible?
|
2009-07-17 11:38:31 -07:00
|
|
|
// Answers the question for basic types, arrays, and slices.
|
|
|
|
|
// Structs are considered ok; fields will be checked later.
|
2009-07-27 11:02:06 -07:00
|
|
|
func compatibleType(fr reflect.Type, fw typeId) bool {
|
2009-07-16 17:55:16 -07:00
|
|
|
for {
|
|
|
|
|
if pt, ok := fr.(*reflect.PtrType); ok {
|
|
|
|
|
fr = pt.Elem();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch t := fr.(type) {
|
|
|
|
|
default:
|
|
|
|
|
// interface, map, chan, etc: cannot handle.
|
|
|
|
|
return false;
|
|
|
|
|
case *reflect.BoolType:
|
|
|
|
|
return fw == tBool;
|
|
|
|
|
case *reflect.IntType:
|
|
|
|
|
return fw == tInt;
|
|
|
|
|
case *reflect.Int8Type:
|
|
|
|
|
return fw == tInt;
|
|
|
|
|
case *reflect.Int16Type:
|
|
|
|
|
return fw == tInt;
|
|
|
|
|
case *reflect.Int32Type:
|
|
|
|
|
return fw == tInt;
|
|
|
|
|
case *reflect.Int64Type:
|
|
|
|
|
return fw == tInt;
|
|
|
|
|
case *reflect.UintType:
|
|
|
|
|
return fw == tUint;
|
|
|
|
|
case *reflect.Uint8Type:
|
|
|
|
|
return fw == tUint;
|
|
|
|
|
case *reflect.Uint16Type:
|
|
|
|
|
return fw == tUint;
|
|
|
|
|
case *reflect.Uint32Type:
|
|
|
|
|
return fw == tUint;
|
|
|
|
|
case *reflect.Uint64Type:
|
|
|
|
|
return fw == tUint;
|
|
|
|
|
case *reflect.UintptrType:
|
|
|
|
|
return fw == tUint;
|
|
|
|
|
case *reflect.FloatType:
|
|
|
|
|
return fw == tFloat;
|
|
|
|
|
case *reflect.Float32Type:
|
|
|
|
|
return fw == tFloat;
|
|
|
|
|
case *reflect.Float64Type:
|
|
|
|
|
return fw == tFloat;
|
|
|
|
|
case *reflect.StringType:
|
|
|
|
|
return fw == tString;
|
|
|
|
|
case *reflect.ArrayType:
|
|
|
|
|
aw, ok := fw.gobType().(*arrayType);
|
|
|
|
|
return ok && t.Len() == aw.Len && compatibleType(t.Elem(), aw.Elem);
|
|
|
|
|
case *reflect.SliceType:
|
|
|
|
|
// Is it an array of bytes?
|
|
|
|
|
et := t.Elem();
|
|
|
|
|
if _, ok := et.(*reflect.Uint8Type); ok {
|
2009-10-06 19:41:51 -07:00
|
|
|
return fw == tBytes;
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
|
|
|
|
sw, ok := fw.gobType().(*sliceType);
|
2009-07-17 11:38:31 -07:00
|
|
|
elem, _ := indirect(t.Elem());
|
|
|
|
|
return ok && compatibleType(elem, sw.Elem);
|
|
|
|
|
case *reflect.StructType:
|
|
|
|
|
return true;
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
func compileDec(wireId typeId, rt reflect.Type) (engine *decEngine, err os.Error) {
|
2009-07-07 11:04:42 -07:00
|
|
|
srt, ok1 := rt.(*reflect.StructType);
|
2009-07-16 17:55:16 -07:00
|
|
|
wireStruct, ok2 := wireId.gobType().(*structType);
|
2009-07-01 18:25:13 -07:00
|
|
|
if !ok1 || !ok2 {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, errNotStruct;
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2009-07-16 23:01:10 -07:00
|
|
|
engine = new(decEngine);
|
2009-07-16 17:55:16 -07:00
|
|
|
engine.instr = make([]decInstr, len(wireStruct.field));
|
|
|
|
|
// Loop over the fields of the wire type.
|
|
|
|
|
for fieldnum := 0; fieldnum < len(wireStruct.field); fieldnum++ {
|
|
|
|
|
wireField := wireStruct.field[fieldnum];
|
|
|
|
|
// Find the field of the local type with the same name.
|
2009-07-16 23:01:10 -07:00
|
|
|
localField, present := srt.FieldByName(wireField.name);
|
2009-07-28 12:59:39 -07:00
|
|
|
ovfl := overflow(wireField.name);
|
2009-07-16 17:55:16 -07:00
|
|
|
// TODO(r): anonymous names
|
2009-07-28 17:01:46 -07:00
|
|
|
if !present {
|
2009-07-27 11:02:06 -07:00
|
|
|
op, err := decIgnoreOpFor(wireField.id);
|
2009-07-16 23:01:10 -07:00
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, err;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
engine.instr[fieldnum] = decInstr{op, fieldnum, 0, 0, ovfl};
|
2009-07-16 17:55:16 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
2009-07-27 11:02:06 -07:00
|
|
|
if !compatibleType(localField.Type, wireField.id) {
|
2009-07-28 12:59:39 -07:00
|
|
|
details := " (" + wireField.id.String() + " incompatible with " + localField.Type.String() + ") in type " + wireId.Name();
|
|
|
|
|
return nil, os.ErrorString("gob: wrong type for field " + wireField.name + details);
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
op, indir, err := decOpFor(wireField.id, localField.Type, localField.Name);
|
2009-07-16 23:01:10 -07:00
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return nil, err;
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
engine.instr[fieldnum] = decInstr{op, fieldnum, indir, uintptr(localField.Offset), ovfl};
|
2009-07-16 17:55:16 -07:00
|
|
|
engine.numInstr++;
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2009-07-16 23:01:10 -07:00
|
|
|
return;
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
2009-10-06 19:41:51 -07:00
|
|
|
var decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
|
|
|
|
|
var ignorerCache = make(map[typeId]**decEngine)
|
2009-07-01 18:25:13 -07:00
|
|
|
|
2009-07-09 14:33:43 -07:00
|
|
|
// typeLock must be held.
|
2009-07-27 11:02:06 -07:00
|
|
|
func getDecEnginePtr(wireId typeId, rt reflect.Type) (enginePtr **decEngine, err os.Error) {
|
2009-07-17 11:38:31 -07:00
|
|
|
decoderMap, ok := decoderCache[rt];
|
|
|
|
|
if !ok {
|
2009-10-06 19:41:51 -07:00
|
|
|
decoderMap = make(map[typeId]**decEngine);
|
2009-07-17 11:38:31 -07:00
|
|
|
decoderCache[rt] = decoderMap;
|
|
|
|
|
}
|
|
|
|
|
if enginePtr, ok = decoderMap[wireId]; !ok {
|
|
|
|
|
// To handle recursive types, mark this engine as underway before compiling.
|
2009-07-16 17:55:16 -07:00
|
|
|
enginePtr = new(*decEngine);
|
2009-07-17 11:38:31 -07:00
|
|
|
decoderMap[wireId] = enginePtr;
|
2009-07-16 23:01:10 -07:00
|
|
|
*enginePtr, err = compileDec(wireId, rt);
|
|
|
|
|
if err != nil {
|
2009-07-17 11:38:31 -07:00
|
|
|
decoderMap[wireId] = nil, false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return;
|
2009-07-17 11:38:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When ignoring data, in effect we compile it into this type
|
2009-10-06 19:41:51 -07:00
|
|
|
type emptyStruct struct{}
|
|
|
|
|
|
2009-07-17 11:38:31 -07:00
|
|
|
var emptyStructType = reflect.Typeof(emptyStruct{})
|
|
|
|
|
|
|
|
|
|
// typeLock must be held.
|
2009-07-27 11:02:06 -07:00
|
|
|
func getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err os.Error) {
|
2009-07-17 11:38:31 -07:00
|
|
|
var ok bool;
|
|
|
|
|
if enginePtr, ok = ignorerCache[wireId]; !ok {
|
|
|
|
|
// To handle recursive types, mark this engine as underway before compiling.
|
|
|
|
|
enginePtr = new(*decEngine);
|
|
|
|
|
ignorerCache[wireId] = enginePtr;
|
|
|
|
|
*enginePtr, err = compileDec(wireId, emptyStructType);
|
|
|
|
|
if err != nil {
|
|
|
|
|
ignorerCache[wireId] = nil, false;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2009-10-06 19:41:51 -07:00
|
|
|
return;
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
func decode(b *bytes.Buffer, wireId typeId, e interface{}) os.Error {
|
2009-07-01 18:25:13 -07:00
|
|
|
// Dereference down to the underlying object.
|
2009-07-02 17:21:48 -07:00
|
|
|
rt, indir := indirect(reflect.Typeof(e));
|
2009-07-01 18:25:13 -07:00
|
|
|
v := reflect.NewValue(e);
|
2009-07-02 17:21:48 -07:00
|
|
|
for i := 0; i < indir; i++ {
|
2009-07-01 18:25:13 -07:00
|
|
|
v = reflect.Indirect(v);
|
|
|
|
|
}
|
2009-07-16 17:55:16 -07:00
|
|
|
var st *reflect.StructValue;
|
|
|
|
|
var ok bool;
|
|
|
|
|
if st, ok = v.(*reflect.StructValue); !ok {
|
2009-10-06 19:41:51 -07:00
|
|
|
return os.ErrorString("gob: decode can't handle " + rt.String());
|
2009-07-02 13:43:47 -07:00
|
|
|
}
|
2009-07-01 18:25:13 -07:00
|
|
|
typeLock.Lock();
|
2009-07-29 15:10:29 -07:00
|
|
|
if _, ok := idToType[wireId]; !ok {
|
|
|
|
|
typeLock.Unlock();
|
|
|
|
|
return errBadType;
|
|
|
|
|
}
|
2009-07-16 23:01:10 -07:00
|
|
|
enginePtr, err := getDecEnginePtr(wireId, rt);
|
2009-07-01 18:25:13 -07:00
|
|
|
typeLock.Unlock();
|
2009-07-16 23:01:10 -07:00
|
|
|
if err != nil {
|
2009-10-06 19:41:51 -07:00
|
|
|
return err;
|
2009-07-16 23:01:10 -07:00
|
|
|
}
|
|
|
|
|
engine := *enginePtr;
|
2009-07-17 11:38:31 -07:00
|
|
|
if engine.numInstr == 0 && st.NumField() > 0 && len(wireId.gobType().(*structType).field) > 0 {
|
2009-07-17 14:20:33 -07:00
|
|
|
name := rt.Name();
|
2009-10-06 19:41:51 -07:00
|
|
|
return os.ErrorString("gob: type mismatch: no fields matched compiling decoder for " + name);
|
2009-07-16 17:55:16 -07:00
|
|
|
}
|
2009-07-15 16:10:17 -07:00
|
|
|
return decodeStruct(engine, rt.(*reflect.StructType), b, uintptr(v.Addr()), 0);
|
2009-07-01 18:25:13 -07:00
|
|
|
}
|
2009-07-28 12:59:39 -07:00
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
// We assume that the size of float is sufficient to tell us whether it is
|
|
|
|
|
// equivalent to float32 or to float64. This is very unlikely to be wrong.
|
|
|
|
|
var op decOp;
|
|
|
|
|
switch unsafe.Sizeof(float(0)) {
|
|
|
|
|
case unsafe.Sizeof(float32(0)):
|
|
|
|
|
op = decFloat32;
|
|
|
|
|
case unsafe.Sizeof(float64(0)):
|
|
|
|
|
op = decFloat64;
|
|
|
|
|
default:
|
|
|
|
|
panic("gob: unknown size of float", unsafe.Sizeof(float(0)));
|
|
|
|
|
}
|
|
|
|
|
decOpMap[valueKind(float(0))] = op;
|
|
|
|
|
|
|
|
|
|
// A similar assumption about int and uint. Also assume int and uint have the same size.
|
|
|
|
|
var uop decOp;
|
|
|
|
|
switch unsafe.Sizeof(int(0)) {
|
|
|
|
|
case unsafe.Sizeof(int32(0)):
|
|
|
|
|
op = decInt32;
|
|
|
|
|
uop = decUint32;
|
|
|
|
|
case unsafe.Sizeof(int64(0)):
|
|
|
|
|
op = decInt64;
|
|
|
|
|
uop = decUint64;
|
|
|
|
|
default:
|
|
|
|
|
panic("gob: unknown size of int/uint", unsafe.Sizeof(int(0)));
|
|
|
|
|
}
|
|
|
|
|
decOpMap[valueKind(int(0))] = op;
|
|
|
|
|
decOpMap[valueKind(uint(0))] = uop;
|
|
|
|
|
|
|
|
|
|
// Finally uintptr
|
|
|
|
|
switch unsafe.Sizeof(uintptr(0)) {
|
|
|
|
|
case unsafe.Sizeof(uint32(0)):
|
|
|
|
|
uop = decUint32;
|
|
|
|
|
case unsafe.Sizeof(uint64(0)):
|
|
|
|
|
uop = decUint64;
|
|
|
|
|
default:
|
|
|
|
|
panic("gob: unknown size of uintptr", unsafe.Sizeof(uintptr(0)));
|
|
|
|
|
}
|
|
|
|
|
decOpMap[valueKind(uintptr(0))] = uop;
|
|
|
|
|
}
|