-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec_type_methods.go
407 lines (333 loc) · 8.47 KB
/
dec_type_methods.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package goetf
import (
"encoding/binary"
"math"
"math/big"
"reflect"
)
// parseStaticType parses a specific tag type from the input data
// and then returns the parsed value in the form of any.
func (d *Decoder) parseStaticType(kind reflect.Kind, tag ExternalTagType, data []byte) any {
switch tag {
case EttNil:
return nil
case EttString:
return d.parseString(data)
case EttAtom, EttAtomUTF8, EttSmallAtom, EttSmallAtomUTF8:
s := d.parseString(data)
switch {
case s == "true":
return true
case s == "false":
return false
case s == "nil":
return nil
}
return d.cache.Deduplicate(s)
case EttSmallInteger:
switch kind {
default:
return d.parseSmallInteger(data)
case reflect.Int:
return int(d.parseSmallInteger(data))
case reflect.Int32:
return int32(d.parseSmallInteger(data))
case reflect.Int64:
return int64(d.parseSmallInteger(data))
case reflect.Uint:
return uint(d.parseSmallInteger(data))
case reflect.Uint16:
return uint16(d.parseSmallInteger(data))
case reflect.Uint32:
return uint32(d.parseSmallInteger(data))
case reflect.Uint64:
return uint64(d.parseSmallInteger(data))
}
case EttInteger:
switch kind {
default:
return d.parseInteger(data)
case reflect.Int:
return int(d.parseInteger(data))
case reflect.Int64:
return int64(d.parseInteger(data))
}
case EttNewFloat:
switch kind {
default:
return d.parseNewFloat(data)
case reflect.Float32:
return float32(d.parseNewFloat(data))
}
case EttFloat:
switch kind {
default:
return d.parseFloat(data)
case reflect.Float32:
return float32(d.parseFloat(data))
}
case EttSmallBig:
switch kind {
default:
return d.parseSmallBig(data)
case reflect.Int:
return int(d.parseSmallBig(data))
}
case EttLargeBig:
switch kind {
default:
return d.parseLargeBig(data)
case reflect.Int, reflect.Int64:
return d.parseLargeBig(data).Int64()
}
case EttBinary:
switch kind {
default:
return data
case reflect.String:
return string(data)
}
case EttBitBinary:
switch kind {
default:
return data
case reflect.String:
return string(data)
}
}
return nil
}
func (d *Decoder) parseString(b []byte) string {
return string(b)
}
func (d *Decoder) parseSmallInteger(b []byte) uint8 {
return uint8(b[0])
}
func (d *Decoder) parseInteger(b []byte) int32 {
return int32(binary.BigEndian.Uint32(b))
}
func (d *Decoder) parseNewFloat(b []byte) float64 {
bits := binary.BigEndian.Uint64(b)
float := math.Float64frombits(bits)
return float
}
func (d *Decoder) parseFloat(b []byte) float64 {
// todo: change to use string float IEEE format
bits := binary.LittleEndian.Uint64(b)
float := math.Float64frombits(bits)
return float
}
func (d *Decoder) parseSmallBig(b []byte) int64 {
sign := b[0]
rest := b[1:]
bits := binary.LittleEndian.Uint64(rest)
smallBig := int64(bits)
if sign == 1 {
smallBig *= -1
}
return smallBig
}
func (d *Decoder) parseLargeBig(b []byte) *big.Int {
sign := b[0]
rest := b[1:]
bits := binary.LittleEndian.Uint64(rest)
largeBig := int64(bits)
if sign == 1 {
largeBig *= -1
}
return big.NewInt(largeBig)
}
// readStaticType reads a specific tag type from the underlying buffer,
// then returns the number of bytes read, a byte slice and an error, if any.
func (d *Decoder) readStaticType(tag ExternalTagType) (n int, b []byte, err error) {
switch tag {
default:
n, b, err = 0, nil, errMalformed
case EttNil:
n, b, err = 0, []byte{EttNil}, nil
case EttSmallInteger:
n, b, err = d.readSmallInteger()
case EttInteger:
n, b, err = d.readInteger()
case EttFloat:
n, b, err = d.readFloat()
case EttNewFloat:
n, b, err = d.readNewFloat()
case EttAtom, EttAtomUTF8:
n, b, err = d.readAtomUTF8()
case EttSmallAtom, EttSmallAtomUTF8:
n, b, err = d.readSmallAtomUTF8()
case EttString:
n, b, err = d.readString()
case EttSmallBig:
n, b, err = d.readSmallBig()
case EttLargeBig:
n, b, err = d.readLargeBig()
case EttBinary:
n, b, err = d.readBinary()
case EttBitBinary:
n, b, err = d.readBitBinary()
}
return
}
func (d *Decoder) readBitBinary() (int, []byte, error) {
n, bLen, err := d.scan.readN(SizeBitBinaryLen)
if err != nil {
return n, bLen, errMalformedBitBinary
}
if n < SizeBitBinaryLen {
return n, bLen, errMalformedBitBinary
}
length := int(binary.BigEndian.Uint32(bLen))
_, err = d.scan.readByte()
if err != nil {
return n + 1, bLen, errMalformedBitBinary
}
n, data, err := d.scan.readN(length)
if err != nil {
return n, data, errMalformedBitBinary
}
return n, data, nil
}
func (d *Decoder) readAtomUTF8() (int, []byte, error) {
n, bLen, err := d.scan.readN(SizeAtomUTF8)
if err != nil {
return n, bLen, errMalformedAtomUTF8
}
length := int(binary.BigEndian.Uint16(bLen))
// {..., 118, 0, 0, ...}
if length == 0 {
return n, bLen, errMalformedAtomUTF8
}
n, data, err := d.scan.readN(length)
if err != nil {
return n, data, errMalformedAtomUTF8
}
return n, data, nil
}
func (d *Decoder) readSmallAtomUTF8() (int, []byte, error) {
bLen, err := d.scan.readByte()
if err != nil {
return 1, nil, errMalformedSmallAtomUTF8
}
length := int(bLen)
if length == 0 {
return 1, nil, errMalformedSmallAtomUTF8
}
n, data, err := d.scan.readN(length)
if err != nil {
return n, data, errMalformedSmallAtomUTF8
}
return n, data, nil
}
func (d *Decoder) readLargeBig() (int, []byte, error) {
n, bN, err := d.scan.readN(SizeLargeBigN)
if err != nil {
return n, bN, errMalformedLargeBig
}
if n < SizeLargeBigN {
return n, bN, errMalformedLargeBig
}
N := int(binary.BigEndian.Uint32(bN))
sign, err := d.scan.readByte()
if err != nil {
return n, nil, errMalformedLargeBig
}
n, data, err := d.scan.readN(N) // N+1 to store internaly the sign
if err != nil {
return n, data, errMalformedSmallBig
}
if N < 8 {
N += 8 - N
}
largeBig := make([]byte, N+1)
largeBig[0] = sign
copy(largeBig[1:], data)
return len(largeBig) - 1, largeBig, nil
}
func (d *Decoder) readSmallBig() (int, []byte, error) {
bN, err := d.scan.readByte()
if err != nil {
return 1, nil, errMalformedSmallBig
}
// 'N' is the amount of bytes that are used for the small big
N := int(bN)
// positive or negative sign
sign, err := d.scan.readByte()
if err != nil {
return 1, nil, errMalformedSmallBig
}
// fill with 0 to allow parsing
n, data, err := d.scan.readN(N) // N+1 to store internaly the sign
if err != nil {
return n, data, errMalformedSmallBig
}
if N < 8 {
N += 8 - N
}
smallBig := make([]byte, N+1)
smallBig[0] = sign
copy(smallBig[1:], data)
return len(smallBig) - 1, smallBig, nil
}
func (d *Decoder) readBinary() (int, []byte, error) {
n, bLen, err := d.scan.readN(SizeBinaryLen)
if err != nil {
return n, bLen, errMalformedBinary
}
length := int(binary.BigEndian.Uint32(bLen))
n, binary, err := d.scan.readN(length)
if err != nil {
return n, binary, errMalformedBinary
}
if n < length {
return n, binary, errMalformedBinary
}
return n, binary, nil
}
func (d *Decoder) readString() (int, []byte, error) {
n, bLen, err := d.scan.readN(SizeStringLength)
if err != nil {
return n, bLen, errMalformedString
}
length := int(binary.BigEndian.Uint16(bLen))
if length == 0 {
return n, bLen, errMalformedString
}
n, bStr, err := d.scan.readN(length)
if err != nil {
return n, bStr, errMalformedString
}
return n, bStr, nil
}
func (d *Decoder) readSmallInteger() (int, []byte, error) {
num, err := d.scan.readByte()
if err != nil {
return 1, []byte{num}, errMalformedSmallInteger
}
return 1, []byte{num}, nil
}
func (d *Decoder) readInteger() (int, []byte, error) {
n, num, err := d.scan.readN(SizeInteger)
if err != nil {
return n, num, errMalformedInteger
}
return n, num, nil
}
func (d *Decoder) readNewFloat() (int, []byte, error) {
n, num, err := d.scan.readN(SizeNewFloat)
if err != nil {
return n, num, errMalformedNewFloat
}
if n < (SizeNewFloat) {
return n, num, errMalformedNewFloat
}
return n, num, nil
}
func (d *Decoder) readFloat() (int, []byte, error) {
n, num, err := d.scan.readN(SizeFloat)
if err != nil {
return n, num, errMalformedFloat
}
return n, num, nil
}