forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryreader.go
487 lines (417 loc) · 8.86 KB
/
binaryreader.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package ion
import (
"bufio"
"fmt"
)
// A binaryReader reads binary Ion.
type binaryReader struct {
reader
bits bitstream
cat Catalog
lst SymbolTable
}
func newBinaryReaderBuf(in *bufio.Reader, cat Catalog) Reader {
r := &binaryReader{
cat: cat,
}
r.bits.Init(in)
return r
}
// SymbolTable returns the current symbol table.
func (r *binaryReader) SymbolTable() SymbolTable {
return r.lst
}
// Next moves the reader to the next value.
func (r *binaryReader) Next() bool {
if r.eof || r.err != nil {
return false
}
r.clear()
done := false
for !done {
done, r.err = r.next()
if r.err != nil {
return false
}
}
return !r.eof
}
// Next consumes the next raw value from the stream, returning true if it
// represents a user-facing value and false if it does not.
func (r *binaryReader) next() (bool, error) {
if err := r.bits.Next(); err != nil {
return false, err
}
code := r.bits.Code()
switch code {
case bitcodeEOF:
r.eof = true
return true, nil
case bitcodeBVM:
err := r.readBVM()
return false, err
case bitcodeFieldID:
err := r.readFieldName()
return false, err
case bitcodeAnnotation:
err := r.readAnnotations()
return false, err
case bitcodeNull:
if !r.bits.IsNull() {
// NOP padding; skip it and keep going.
err := r.bits.SkipValue()
return false, err
}
r.valueType = NullType
return true, nil
case bitcodeFalse, bitcodeTrue:
r.valueType = BoolType
if !r.bits.IsNull() {
r.value = (r.bits.Code() == bitcodeTrue)
}
return true, nil
case bitcodeInt, bitcodeNegInt:
r.valueType = IntType
if !r.bits.IsNull() {
val, err := r.bits.ReadInt()
if err != nil {
return false, err
}
r.value = val
}
return true, nil
case bitcodeFloat:
r.valueType = FloatType
if !r.bits.IsNull() {
val, err := r.bits.ReadFloat()
if err != nil {
return false, err
}
r.value = val
}
return true, nil
case bitcodeDecimal:
r.valueType = DecimalType
if !r.bits.IsNull() {
val, err := r.bits.ReadDecimal()
if err != nil {
return false, err
}
r.value = val
}
return true, nil
case bitcodeTimestamp:
r.valueType = TimestampType
if !r.bits.IsNull() {
val, err := r.bits.ReadTimestamp()
if err != nil {
return false, err
}
r.value = val
}
return true, nil
case bitcodeSymbol:
r.valueType = SymbolType
if !r.bits.IsNull() {
id, err := r.bits.ReadSymbolID()
if err != nil {
return false, err
}
r.value = r.resolve(id)
}
return true, nil
case bitcodeString:
r.valueType = StringType
if !r.bits.IsNull() {
val, err := r.bits.ReadString()
if err != nil {
return false, err
}
r.value = val
}
return true, nil
case bitcodeClob:
r.valueType = ClobType
if !r.bits.IsNull() {
val, err := r.bits.ReadBytes()
if err != nil {
return false, err
}
r.value = val
}
return true, nil
case bitcodeBlob:
r.valueType = BlobType
if !r.bits.IsNull() {
val, err := r.bits.ReadBytes()
if err != nil {
return false, err
}
r.value = val
}
return true, nil
case bitcodeList:
r.valueType = ListType
if !r.bits.IsNull() {
r.value = ListType
}
return true, nil
case bitcodeSexp:
r.valueType = SexpType
if !r.bits.IsNull() {
r.value = SexpType
}
return true, nil
case bitcodeStruct:
r.valueType = StructType
if !r.bits.IsNull() {
r.value = StructType
}
// If it's a local symbol table, install it and keep going.
if r.ctx.peek() == ctxAtTopLevel && isIonSymbolTable(r.annotations) {
err := r.readLocalSymbolTable()
return false, err
}
return true, nil
}
panic(fmt.Sprintf("invalid bitcode %v", code))
}
func isIonSymbolTable(as []string) bool {
return len(as) > 0 && as[0] == "$ion_symbol_table"
}
// ReadBVM reads a BVM, validates it, and resets the local symbol table.
func (r *binaryReader) readBVM() error {
major, minor, err := r.bits.ReadBVM()
if err != nil {
return err
}
switch major {
case 1:
switch minor {
case 0:
r.lst = V1SystemSymbolTable
return nil
}
}
return &UnsupportedVersionError{
int(major),
int(minor),
r.bits.Pos() - 4,
}
}
// ReadLocalSymbolTable reads and installs a new local symbol table.
func (r *binaryReader) readLocalSymbolTable() error {
if r.IsNull() {
r.clear()
r.lst = V1SystemSymbolTable
return nil
}
if err := r.StepIn(); err != nil {
return err
}
imps := []SharedSymbolTable{}
syms := []string{}
for r.Next() {
var err error
switch r.FieldName() {
case "imports":
imps, err = r.readImports()
case "symbols":
syms, err = r.readSymbols()
}
if err != nil {
return err
}
}
if err := r.StepOut(); err != nil {
return err
}
r.lst = NewLocalSymbolTable(imps, syms)
return nil
}
// ReadImports reads the imports field of a local symbol table.
func (r *binaryReader) readImports() ([]SharedSymbolTable, error) {
if r.valueType == SymbolType && r.value == "$ion_symbol_table" {
// Special case that imports the current local symbol table.
if r.lst == nil || r.lst == V1SystemSymbolTable {
return nil, nil
}
imps := r.lst.Imports()
lsst := NewSharedSymbolTable("", 0, r.lst.Symbols())
return append(imps, lsst), nil
}
if r.Type() != ListType || r.IsNull() {
return nil, nil
}
if err := r.StepIn(); err != nil {
return nil, err
}
imps := []SharedSymbolTable{}
for r.Next() {
imp, err := r.readImport()
if err != nil {
return nil, err
}
if imp != nil {
imps = append(imps, imp)
}
}
err := r.StepOut()
return imps, err
}
// ReadImport reads an import definition.
func (r *binaryReader) readImport() (SharedSymbolTable, error) {
if r.Type() != StructType || r.IsNull() {
return nil, nil
}
if err := r.StepIn(); err != nil {
return nil, err
}
name := ""
version := 0
maxID := uint64(0)
for r.Next() {
var err error
switch r.FieldName() {
case "name":
if r.Type() == StringType {
name, err = r.StringValue()
}
case "version":
if r.Type() == IntType {
version, err = r.IntValue()
}
case "max_id":
if r.Type() == IntType {
var i int64
i, err = r.Int64Value()
if i < 0 {
i = 0
}
maxID = uint64(i)
}
}
if err != nil {
return nil, err
}
}
if err := r.StepOut(); err != nil {
return nil, err
}
if name == "" || name == "$ion" {
return nil, nil
}
if version < 1 {
version = 1
}
var imp SharedSymbolTable
if r.cat != nil {
imp = r.cat.FindExact(name, version)
if imp == nil {
imp = r.cat.FindLatest(name)
}
}
if maxID == 0 {
if imp == nil || version != imp.Version() {
return nil, fmt.Errorf("ion: import of shared table %v/%v lacks a valid max_id, but an exact "+
"match was not found in the catalog", name, version)
}
maxID = imp.MaxID()
}
if imp == nil {
imp = &bogusSST{
name: name,
version: version,
maxID: maxID,
}
} else {
imp = imp.Adjust(maxID)
}
return imp, nil
}
// ReadSymbols reads the symbols from a symbol table.
func (r *binaryReader) readSymbols() ([]string, error) {
if r.Type() != ListType {
return nil, nil
}
if err := r.StepIn(); err != nil {
return nil, err
}
syms := []string{}
for r.Next() {
if r.Type() == StringType {
sym, err := r.StringValue()
if err != nil {
return nil, err
}
syms = append(syms, sym)
} else {
syms = append(syms, "")
}
}
err := r.StepOut()
return syms, err
}
// ReadFieldName reads and resolves a field name.
func (r *binaryReader) readFieldName() error {
id, err := r.bits.ReadFieldID()
if err != nil {
return err
}
r.fieldName = r.resolve(id)
return nil
}
// ReadAnnotations reads and resolves a set of annotations.
func (r *binaryReader) readAnnotations() error {
ids, err := r.bits.ReadAnnotationIDs()
if err != nil {
return err
}
as := make([]string, len(ids))
for i, id := range ids {
as[i] = r.resolve(id)
}
r.annotations = as
return nil
}
// Resolve resolves a symbol ID to a symbol value (possibly ${id} if we're
// missing the appropriate symbol table).
func (r *binaryReader) resolve(id uint64) string {
s, ok := r.lst.FindByID(id)
if !ok {
return fmt.Sprintf("$%v", id)
}
return s
}
// StepIn steps in to a container-type value
func (r *binaryReader) StepIn() error {
if r.err != nil {
return r.err
}
if r.valueType != ListType && r.valueType != SexpType && r.valueType != StructType {
return &UsageError{"Reader.StepIn", fmt.Sprintf("cannot step in to a %v", r.valueType)}
}
if r.value == nil {
return &UsageError{"Reader.StepIn", "cannot step in to a null container"}
}
r.ctx.push(containerTypeToCtx(r.valueType))
r.clear()
r.bits.StepIn()
return nil
}
// StepOut steps out of a container-type value.
func (r *binaryReader) StepOut() error {
if r.err != nil {
return r.err
}
if r.ctx.peek() == ctxAtTopLevel {
return &UsageError{"Reader.StepOut", "cannot step out of top-level datagram"}
}
if err := r.bits.StepOut(); err != nil {
return err
}
r.clear()
r.ctx.pop()
r.eof = false
return nil
}