-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdecode.go
275 lines (224 loc) · 4.87 KB
/
decode.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package bert
import (
"bytes"
"encoding/binary"
"errors"
"io"
"io/ioutil"
"reflect"
"strconv"
)
var ErrBadMagic error = errors.New("bad magic")
var ErrUnknownType error = errors.New("unknown type")
func read1(r io.Reader) (int, error) {
bits, err := ioutil.ReadAll(io.LimitReader(r, 1))
if err != nil {
return 0, err
}
ui8 := uint8(bits[0])
return int(ui8), nil
}
func read2(r io.Reader) (int, error) {
bits, err := ioutil.ReadAll(io.LimitReader(r, 2))
if err != nil {
return 0, err
}
ui16 := binary.BigEndian.Uint16(bits)
return int(ui16), nil
}
func read4(r io.Reader) (int, error) {
bits, err := ioutil.ReadAll(io.LimitReader(r, 4))
if err != nil {
return 0, err
}
ui32 := binary.BigEndian.Uint32(bits)
return int(ui32), nil
}
func readSmallInt(r io.Reader) (int, error) {
return read1(r)
}
func readInt(r io.Reader) (int, error) { return read4(r) }
func readFloat(r io.Reader) (float32, error) {
bits, err := ioutil.ReadAll(io.LimitReader(r, 31))
if err != nil {
return 0, err
}
// ParseFloat doesn't like trailing 0s
var i int
for i = 0; i < len(bits); i++ {
if bits[i] == 0 {
break
}
}
f, err := strconv.ParseFloat(string(bits[0:i]), 32)
if err != nil {
return 0, err
}
return float32(f), nil
}
func readAtom(r io.Reader) (Atom, error) {
str, err := readString(r)
return Atom(str), err
}
func readSmallTuple(r io.Reader) (Term, error) {
size, err := read1(r)
if err != nil {
return nil, err
}
tuple := make([]Term, size)
for i := 0; i < size; i++ {
term, err := readTag(r)
if err != nil {
return nil, err
}
switch a := term.(type) {
case Atom:
if a == BertAtom {
return readComplex(r)
}
}
tuple[i] = term
}
return tuple, nil
}
func readNil(r io.Reader) ([]Term, error) {
_, err := ioutil.ReadAll(io.LimitReader(r, 1))
if err != nil {
return nil, err
}
list := make([]Term, 0)
return list, nil
}
func readString(r io.Reader) (string, error) {
size, err := read2(r)
if err != nil {
return "", err
}
str, err := ioutil.ReadAll(io.LimitReader(r, int64(size)))
if err != nil {
return "", err
}
return string(str), nil
}
func readList(r io.Reader) ([]Term, error) {
size, err := read4(r)
if err != nil {
return nil, err
}
list := make([]Term, size)
for i := 0; i < size; i++ {
term, err := readTag(r)
if err != nil {
return nil, err
}
list[i] = term
}
read1(r)
return list, nil
}
func readBin(r io.Reader) ([]uint8, error) {
size, err := read4(r)
if err != nil {
return []uint8{}, err
}
bytes, err := ioutil.ReadAll(io.LimitReader(r, int64(size)))
if err != nil {
return []uint8{}, err
}
return bytes, nil
}
func readComplex(r io.Reader) (Term, error) {
term, err := readTag(r)
if err != nil {
return term, err
}
switch kind := term.(type) {
case Atom:
switch kind {
case NilAtom:
return nil, nil
case TrueAtom:
return true, nil
case FalseAtom:
return false, nil
}
}
return term, nil
}
func readTag(r io.Reader) (Term, error) {
tag, err := read1(r)
if err != nil {
return nil, err
}
switch tag {
case SmallIntTag:
return readSmallInt(r)
case IntTag:
return readInt(r)
case SmallBignumTag:
return nil, ErrUnknownType
case LargeBignumTag:
return nil, ErrUnknownType
case FloatTag:
return readFloat(r)
case AtomTag:
return readAtom(r)
case SmallTupleTag:
return readSmallTuple(r)
case LargeTupleTag:
return nil, ErrUnknownType
case NilTag:
return readNil(r)
case StringTag:
return readString(r)
case ListTag:
return readList(r)
case BinTag:
return readBin(r)
}
return nil, ErrUnknownType
}
// DecodeFrom decodes a Term from r and returns it or an error.
func DecodeFrom(r io.Reader) (Term, error) {
version, err := read1(r)
if err != nil {
return nil, err
}
// check protocol version
if version != VersionTag {
return nil, ErrBadMagic
}
return readTag(r)
}
// Decode decodes a Term from data and returns it or an error.
func Decode(data []byte) (Term, error) { return DecodeFrom(bytes.NewBuffer(data)) }
// UnmarshalFrom decodes a value from r, stores it in val, and returns any
// error encountered.
func UnmarshalFrom(r io.Reader, val interface{}) (err error) {
result, _ := DecodeFrom(r)
value := reflect.ValueOf(val).Elem()
switch v := value; v.Kind() {
case reflect.Struct:
slice := reflect.ValueOf(result)
for i := 0; i < slice.Len(); i++ {
e := slice.Index(i).Elem()
v.Field(i).Set(e)
}
}
return nil
}
// Unmarshal decodes a value from data, stores it in val, and returns any error
// encountered.
func Unmarshal(data []byte, val interface{}) (err error) {
return UnmarshalFrom(bytes.NewBuffer(data), val)
}
// UnmarshalRequest decodes a BURP from r and returns it as a Request.
func UnmarshalRequest(r io.Reader) (Request, error) {
var req Request
size, err := read4(r)
if err != nil {
return req, err
}
err = UnmarshalFrom(io.LimitReader(r, int64(size)), &req)
return req, err
}