blob: 4941a788b0524609af8ca5e302255a54a0c20985 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
// 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 (
"gob";
"io";
"os";
"reflect";
"sync";
)
type Decoder struct {
sync.Mutex; // each item must be received atomically
seen map[TypeId] *wireType; // which types we've already seen described
state *DecState; // so we can encode integers, strings directly
}
func NewDecoder(r io.Reader) *Decoder {
dec := new(Decoder);
dec.seen = make(map[TypeId] *wireType);
dec.state = new(DecState);
dec.state.r = r; // the rest isn't important; all we need is buffer and reader
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);
Decode(dec.state.r, wire);
// 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();
var id TypeId;
for dec.state.err == nil {
// Receive a type id.
id = TypeId(DecodeInt(dec.state));
// If the id is positive, we have a value. 0 is the error state
if id >= 0 {
break;
}
// The id is negative; a type descriptor follows.
dec.recvType(-id);
}
if dec.state.err != nil {
return dec.state.err
}
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");
return dec.state.err
}
// Receive a value.
Decode(dec.state.r, e);
// Release and return.
return dec.state.err
}
|