-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenc.go
429 lines (396 loc) · 10 KB
/
enc.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package syrup
import (
"encoding/binary"
"errors"
"fmt"
"math"
"math/big"
"strconv"
"unicode"
)
type Encoding struct {
fmtString func(s string) []byte
fmtBigInt func(i *big.Int) []byte
fmtInt func(i int64) []byte
fmtUint func(i uint64) []byte
fmtBool func(b bool) []byte
fmtFloat64 func(f float64) []byte
fmtFloat32 func(f float32) []byte
fmtBytes func(b []byte) []byte
fmtSymbol func(s string) []byte
listOpen func() []byte
listClose func() []byte
dictOpen func() []byte
dictClose func() []byte
setOpen func() []byte
setClose func() []byte
recordOpen func() []byte
recordClose func() []byte
mustFindToken func(b byte) (scanState, op, bool, error)
scanTokenLen func(b byte) (scanState, op, bool, error)
scanFirstIntToken func(b byte) (scanState, op, bool, error)
scanIntToken func(b byte) (scanState, op, bool, error)
scanFloat64Token func(b byte) (scanState, op, bool, error)
scanFloat32Token func(b byte) (scanState, op, bool, error)
parseLen func(s string, next scanState) (op, uint64, error)
boolVal func(b []byte) (bool, error)
symbolVal func(b []byte) (Symbol, error)
stringVal func(b []byte) (string, error)
int64Val func(b []byte) (int64, *big.Int, error)
float32Val func(b []byte) (float32, error)
float64Val func(b []byte) (float64, error)
nlen uint8 // For counting lengths of floating point encodings.
}
// NewPrototypeEncoding returns the prototypical syrup encoding proposed.
func NewPrototypeEncoding() *Encoding {
e := &Encoding{
fmtString: syrupProtoString,
fmtBigInt: syrupProtoBigInt,
fmtInt: syrupProtoInt,
fmtUint: syrupProtoUint,
fmtBool: syrupProtoBool,
fmtFloat64: syrupProtoFloat64,
fmtFloat32: syrupProtoFloat32,
fmtBytes: syrupProtoBytes,
fmtSymbol: syrupProtoSymbol,
listOpen: syrupProtoListOpen,
listClose: syrupProtoListClose,
dictOpen: syrupProtoDictOpen,
dictClose: syrupProtoDictClose,
setOpen: syrupProtoSetOpen,
setClose: syrupProtoSetClose,
recordOpen: syrupProtoRecordOpen,
recordClose: syrupProtoRecordClose,
scanTokenLen: syrupProtoScanTokenLen,
scanFirstIntToken: syrupProtoScanFirstIntToken,
scanIntToken: syrupProtoScanIntToken,
parseLen: syrupProtoParsedLen,
boolVal: syrupProtoBoolVal,
symbolVal: syrupProtoSymbolVal,
stringVal: syrupProtoStringVal,
int64Val: syrupProtoInt64Val,
float32Val: syrupProtoFloat32Val,
float64Val: syrupProtoFloat64Val,
nlen: 0,
}
e.mustFindToken = func(b byte) (scanState, op, bool, error) {
s, o, in, err := syrupProtoMustFindToken(b)
if s == scanFloat32 {
e.nlen = 4
} else if s == scanFloat64 {
e.nlen = 8
}
return s, o, in, err
}
e.scanFloat64Token = func(b byte) (scanState, op, bool, error) {
if e.nlen == 0 {
return scanFindToken, noop, false, errors.New("missing float64 syrup delimiter or too many calls to parse float64")
}
e.nlen--
return syrupProtoScanFloat64Token(e.nlen)
}
e.scanFloat32Token = func(b byte) (scanState, op, bool, error) {
if e.nlen == 0 {
return scanFindToken, noop, false, errors.New("missing float32 syrup delimiter or too many calls to parse float32")
}
e.nlen--
return syrupProtoScanFloat32Token(e.nlen)
}
return e
}
func syrupProtoString(s string) []byte {
b := append([]byte(strconv.FormatInt(int64(len(s)), 10)), '"')
b = append(b, []byte(s)...)
return b
}
func syrupProtoBigInt(i *big.Int) []byte {
b := append([]byte{'i'},
[]byte(i.Text(10))...)
b = append(b, 'e')
return b
}
func syrupProtoInt(i int64) []byte {
b := append([]byte{'i'},
[]byte(strconv.FormatInt(i, 10))...)
b = append(b, 'e')
return b
}
func syrupProtoUint(i uint64) []byte {
b := append([]byte{'i'},
[]byte(strconv.FormatUint(i, 10))...)
b = append(b, 'e')
return b
}
func syrupProtoBool(b bool) []byte {
if b {
return []byte{'t'}
} else {
return []byte{'f'}
}
}
func syrupProtoFloat64(f float64) []byte {
b := make([]byte, 9)
b[0] = 'D'
binary.BigEndian.PutUint64(b[1:], math.Float64bits(f))
return b
}
func syrupProtoFloat32(f float32) []byte {
b := make([]byte, 5)
b[0] = 'F'
binary.BigEndian.PutUint32(b[1:], math.Float32bits(f))
return b
}
func syrupProtoBytes(s []byte) []byte {
b := append([]byte(strconv.FormatInt(int64(len(s)), 10)), ':')
b = append(b, s...)
return b
}
func syrupProtoListOpen() []byte {
return []byte{'['}
}
func syrupProtoListClose() []byte {
return []byte{']'}
}
func syrupProtoDictOpen() []byte {
return []byte{'{'}
}
func syrupProtoDictClose() []byte {
return []byte{'}'}
}
func syrupProtoSymbol(s string) []byte {
b := append([]byte(strconv.FormatInt(int64(len(s)), 10)), '\'')
b = append(b, []byte(s)...)
return b
}
func syrupProtoSetOpen() []byte {
return []byte{'#'}
}
func syrupProtoSetClose() []byte {
return []byte{'$'}
}
func syrupProtoRecordOpen() []byte {
return []byte{'<'}
}
func syrupProtoRecordClose() []byte {
return []byte{'>'}
}
// Determines the next scan state, whether to use the passed-in byte as part of
// further processing, and any errors.
func syrupProtoMustFindToken(b byte) (scanState, op, bool, error) {
if unicode.IsSpace(rune(b)) {
return scanFindToken, noop, false, nil
}
switch b {
case '0':
fallthrough
case '1':
fallthrough
case '2':
fallthrough
case '3':
fallthrough
case '4':
fallthrough
case '5':
fallthrough
case '6':
fallthrough
case '7':
fallthrough
case '8':
fallthrough
case '9':
return scanTokenLen, noop, true, nil
case 'i':
return scanFirstInt, noop, false, nil
case 't':
fallthrough
case 'f':
return scanFindToken, valBoolop, true, nil
case 'F':
return scanFloat32, noop, false, nil
case 'D':
return scanFloat64, noop, false, nil
case '[':
return scanFindToken, openListOp, false, nil
case '{':
return scanFindToken, openDictOp, false, nil
case '#':
return scanFindToken, openSetOp, false, nil
case '<':
return scanFindToken, openRecordOp, false, nil
case ']':
return scanFindToken, closeListOp, false, nil
case '}':
return scanFindToken, closeDictOp, false, nil
case '$':
return scanFindToken, closeSetOp, false, nil
case '>':
return scanFindToken, closeRecordOp, false, nil
default:
return scanFindToken, noop, false, fmt.Errorf("could not determine token for byte: %v", b)
}
}
func syrupProtoScanTokenLen(b byte) (scanState, op, bool, error) {
switch b {
case '0':
fallthrough
case '1':
fallthrough
case '2':
fallthrough
case '3':
fallthrough
case '4':
fallthrough
case '5':
fallthrough
case '6':
fallthrough
case '7':
fallthrough
case '8':
fallthrough
case '9':
return scanTokenLen, noop, true, nil
case '\'':
return scanSymbol, noop, false, nil
case '"':
return scanString, noop, false, nil
case ':':
return scanByteArr, noop, false, nil
default:
return scanFindToken, noop, false, fmt.Errorf("malformed input during len scanning: %v", b)
}
}
func syrupProtoScanFirstIntToken(b byte) (scanState, op, bool, error) {
switch b {
case '-':
fallthrough
case '0':
fallthrough
case '1':
fallthrough
case '2':
fallthrough
case '3':
fallthrough
case '4':
fallthrough
case '5':
fallthrough
case '6':
fallthrough
case '7':
fallthrough
case '8':
fallthrough
case '9':
return scanInt, noop, true, nil
case 'e':
return scanFindToken, valIntOp, false, nil
default:
return scanFindToken, noop, false, fmt.Errorf("malformed input during int scanning: %v", b)
}
}
func syrupProtoScanIntToken(b byte) (scanState, op, bool, error) {
switch b {
case '0':
fallthrough
case '1':
fallthrough
case '2':
fallthrough
case '3':
fallthrough
case '4':
fallthrough
case '5':
fallthrough
case '6':
fallthrough
case '7':
fallthrough
case '8':
fallthrough
case '9':
return scanInt, noop, true, nil
case 'e':
return scanFindToken, valIntOp, false, nil
default:
return scanFindToken, noop, false, fmt.Errorf("malformed input during int scanning: %v", b)
}
}
func syrupProtoScanFloat32Token(n uint8) (scanState, op, bool, error) {
if n == 0 {
return scanFindToken, valFloat32Op, true, nil
} else {
return scanFloat32, noop, true, nil
}
}
func syrupProtoScanFloat64Token(n uint8) (scanState, op, bool, error) {
if n == 0 {
return scanFindToken, valFloat64Op, true, nil
} else {
return scanFloat64, noop, true, nil
}
}
func syrupProtoParsedLen(s string, next scanState) (do op, l uint64, err error) {
l, err = strconv.ParseUint(s, 10, 64)
if err != nil {
return
}
if l == 0 {
switch next {
case scanSymbol:
do = valSymbolOp
case scanString:
do = valStringOp
case scanByteArr:
do = valByteArrOp
default:
err = fmt.Errorf("syrup len parsing bad state: %v", next)
}
}
return
}
func syrupProtoBoolVal(b []byte) (bool, error) {
if len(b) != 1 {
return false, fmt.Errorf("syrup bool val len %d", len(b))
}
if b[0] == 't' {
return true, nil
} else if b[0] == 'f' {
return false, nil
}
return false, fmt.Errorf("syrup bool unknown value: %v", b)
}
func syrupProtoSymbolVal(b []byte) (Symbol, error) {
return Symbol(string(b)), nil
}
func syrupProtoStringVal(b []byte) (string, error) {
return string(b), nil
}
func syrupProtoInt64Val(b []byte) (int64, *big.Int, error) {
i, err := strconv.ParseInt(string(b), 10, 64)
if numErr, ok := err.(*strconv.NumError); ok && numErr.Err == strconv.ErrRange {
var ok bool
bi := big.NewInt(0)
bi, ok = bi.SetString(string(b), 10)
if !ok {
return 0, nil, errors.New("syrup: could not convert value to big int")
} else {
return 0, bi, nil
}
} else {
return i, nil, err
}
}
func syrupProtoFloat32Val(b []byte) (float32, error) {
u := binary.BigEndian.Uint32(b)
return math.Float32frombits(u), nil
}
func syrupProtoFloat64Val(b []byte) (float64, error) {
u := binary.BigEndian.Uint64(b)
return math.Float64frombits(u), nil
}