-
Notifications
You must be signed in to change notification settings - Fork 3
/
jibby.go
616 lines (547 loc) · 17.1 KB
/
jibby.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright 2020 by David A. Golden. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package jibby
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"sync"
)
// ErrUnsupportedBOM means that a UTF-16 or UTF-32 byte order mark was found.
var ErrUnsupportedBOM = errors.New("unsupported byte order mark")
// Decoder reads and decodes JSON objects to BSON from a buffered input stream.
// Objects may be separated by optional white space or may be in a well-formed
// JSON array at the top-level.
type Decoder struct {
arrayFinished bool
arrayStarted bool
curDepth int
extJSONAllowed bool
json *bufio.Reader
maxDepth int
scratchPool *sync.Pool
}
// NewDecoder returns a new decoder. If a UTF-8 byte-order-mark (BOM) exists,
// it will be stripped. Because only UTF-8 is supported, other BOMs are error
// and will return ErrUnsupportedBOM. This function consumes leading white
// space and checks if the first character is '['. If so, the input format is
// expected to be a single JSON array of objects and the stream will consist of
// the objects in the array. Any read error (including io.EOF) during these
// checks will be returned.
//
// If the the bufio.Reader's size is less than 8192, it will be rebuffered.
// This is necessary to account for lookahead for long decimals to minimize
// copying.
func NewDecoder(json *bufio.Reader) (*Decoder, error) {
if json.Size() < 8192 {
json = bufio.NewReaderSize(json, 8192)
}
err := handleBOM(json)
if err != nil {
return nil, err
}
d := &Decoder{
json: json,
maxDepth: 200,
scratchPool: &sync.Pool{
New: func() interface{} { buf := make([]byte, 0, 256); return &buf },
},
}
ch, err := d.readAfterWS()
if err != nil {
// Before an object is read, EOF is a valid response that
// shouldn't be wrapped.
if err == io.EOF {
return nil, err
}
return nil, newReadError(err)
}
switch ch {
case '[':
d.arrayStarted = true
default:
_ = d.json.UnreadByte()
}
return d, nil
}
// ExtJSON toggles whether extended JSON is interpreted by the decoder.
// See https://docs.mongodb.com/manual/reference/mongodb-extended-json/index.html
// Jibby has limited support for the legacy extended JSON format.
func (d *Decoder) ExtJSON(b bool) {
d.extJSONAllowed = b
}
// MaxDepth sets the maximum allowed depth of a JSON object. The default is
// 200.
func (d *Decoder) MaxDepth(n int) {
d.maxDepth = n
}
// Decode converts a single JSON object from the input stream into BSON object.
// The function takes an output buffer as an argument. If the buffer is not
// large enough, a new buffer will be allocated when needed. The final buffer
// is returned, just like with `append`. The function returns io.EOF if no
// objects remain in the stream.
func (d *Decoder) Decode(buf []byte) ([]byte, error) {
if d.arrayFinished {
return nil, io.EOF
}
ch, err := d.readAfterWS()
if err != nil {
// Before an object is read, EOF is a valid response that
// shouldn't be wrapped.
if err == io.EOF {
return nil, err
}
return nil, newReadError(err)
}
switch ch {
case '{':
_ = d.json.UnreadByte()
case ']':
// This case will only occur for an empty top-level array: `[]`.
// Otherwise, the closing array bracket is read after an object.
if d.arrayStarted {
d.arrayFinished = true
return nil, io.EOF
}
return nil, d.parseError([]byte{ch}, "Decode only supports object decoding")
default:
return nil, d.parseError([]byte{ch}, "Decode only supports object decoding")
}
buf, err = d.convertValue(buf, topContainer)
if err != nil {
return nil, err
}
// If in comma mode, consume comma or ']', otherwise, put the
// the character back to be
// After ']', terminate stream?
if d.arrayStarted {
ch, err := d.readAfterWS()
if err != nil {
return nil, newReadError(err)
}
switch ch {
case ',':
// nothing
case ']':
d.arrayFinished = true
default:
return nil, d.parseError([]byte{ch}, "expecting value-separator or end of array")
}
}
return buf, nil
}
// readAfterWS discards JSON white space and returns the next character.
// Any error that occurs is returned without wrapping.
func (d *Decoder) readAfterWS() (byte, error) {
var ch byte
var err error
for {
ch, err = d.json.ReadByte()
if err != nil {
// Don't use newReadError here as we don't know if there must be
// another character. Let the caller decide.
return 0, err
}
switch ch {
case ' ', '\t', '\n', '\r':
default:
return ch, nil
}
}
}
// skipWS consumes white space but leaves the next character in the input
// stream. Any error that occurs is returned without wrapping.
func (d *Decoder) skipWS() error {
_, err := d.readAfterWS()
if err != nil {
return err
}
_ = d.json.UnreadByte()
return nil
}
// readCharAfterWS reads a specific character after white space or errors
// if the character is not available. Any read error is returned, with
// EOF upgraded to io.ErrUnexpectedEOF.
func (d *Decoder) readCharAfterWS(b byte) error {
ch, err := d.readAfterWS()
if err != nil {
return newReadError(err)
}
if ch != b {
return d.parseError([]byte{ch}, fmt.Sprintf("expecting '%c'", b))
}
return nil
}
// readNextChar expects a specific character to be next in the input stream and
// errors otherwise. Any read error is returned, with EOF upgraded to
// io.ErrUnexpectedEOF.
func (d *Decoder) readNextChar(b byte) error {
ch, err := d.json.ReadByte()
if err != nil {
return newReadError(err)
}
if ch != b {
return d.parseError([]byte{ch}, fmt.Sprintf("expecting '%c'", b))
}
return nil
}
// readNameSeparator expects the ':' character after optional white space and
// errors if it not found. It handles other errors like readCharAfterWS.
func (d *Decoder) readNameSeparator() error {
err := d.readCharAfterWS(':')
if err != nil {
return err
}
return nil
}
// readObjectTerminator expects the '}' character after optional white space and
// errors if it is not found. It handles other errors like readCharAfterWS.
func (d *Decoder) readObjectTerminator() error {
err := d.readCharAfterWS('}')
if err != nil {
return err
}
return nil
}
// readQuoteStart expects the '"' character after optional white space and
// errors if it is not found. It handles other errors like readCharAfterWS.
func (d *Decoder) readQuoteStart() error {
err := d.readCharAfterWS('"')
if err != nil {
return err
}
return nil
}
// peekBoundedQuote peeks into the input stream for a series of non-quote
// characters terminated by a closing quote. The function takes a minimum and
// maximum length (including closing quote) and errors if it can't find a
// sequence plus quote within those boundaries. The byte slice returned
// *excludes* the closing quote. Nothing is consumed from the input stream.
//
// NOTE: JSON string escapes (`\n`, etc.) are not supported/interpreted.
func (d *Decoder) peekBoundedQuote(minLen, maxLen int, label string) ([]byte, error) {
buf, err := d.json.Peek(maxLen)
if err != nil {
if err != io.EOF {
return nil, newReadError(err)
}
}
if len(buf) < minLen {
return nil, newReadError(io.ErrUnexpectedEOF)
}
quotePos := bytes.IndexByte(buf, '"')
if quotePos < 0 {
return nil, d.parseError(nil, fmt.Sprintf("string exceeds expected maximum length %d for %s", maxLen-1, label))
} else if quotePos < minLen-1 {
return nil, d.parseError(nil, fmt.Sprintf("string shorter than expected minimum length %d for %s", minLen-1, label))
}
return buf[0:quotePos], nil
}
// readSpecificKey expects and consumes a specific series of bytes representing
// an object key, and errors if it is not found. The starting quote character
// must have already been consumed from the input stream. The closing quote and
// subsequent name separator will also be consumed from the input stream. It
// handles other errors like readCharAfterWS.
func (d *Decoder) readSpecificKey(expected []byte) error {
charsNeeded := len(expected) + 1
key, err := d.peekBoundedQuote(charsNeeded, charsNeeded, string(expected))
if err != nil {
return err
}
if !bytes.Equal(key, expected) {
return d.parseError(nil, fmt.Sprintf("expected %q", string(expected)))
}
_, _ = d.json.Discard(len(key) + 1)
err = d.readNameSeparator()
if err != nil {
return err
}
return nil
}
// peekNumber peeks into the input stream and returns a slice that might be
// parsable as a number, a boolean hint whether it should be treated as a
// floating point number, and any error that is found. The input stream is
// not consumed.
//
// The slice returned will contain characters that could appear in a JSON
// floating point number (excluding "NaN" and "Inf", which are not legal JSON)
// without regard to legal arrangment. For example, `123-eee` is a possible
// return value. This function only only parses out a string to be passed to a
// numeric conversion function.
func (d *Decoder) peekNumber() ([]byte, bool, error) {
var isFloat bool
var terminated bool
buf, err := d.json.Peek(doublePeekWidth)
if err != nil {
// here, io.EOF is OK, since we're peeking and may hit end of
// object within the peek width.
if err != io.EOF {
return nil, false, newReadError(err)
}
}
// Find where the number appears to ends and if it's a float. A
// number ends at white space, a separator, or a terminator.
var i int
LOOP:
for i = 0; i < len(buf); i++ {
switch buf[i] {
case 'e', 'E':
isFloat = true
case '.':
isFloat = true
if i < len(buf)-1 && (buf[i+1] < '0' || buf[i+1] > '9') {
_, _ = d.json.Discard(i)
return nil, false, d.parseError(nil, "decimal must be followed by digit")
}
case ' ', '\t', '\n', '\r', ',', ']', '}':
terminated = true
break LOOP
case '_':
_, _ = d.json.Discard(i)
return nil, false, d.parseError(nil, "invalid character in number")
}
}
if !terminated {
if len(buf) < doublePeekWidth {
return nil, false, newReadError(io.ErrUnexpectedEOF)
}
return nil, false, d.parseError(nil, "number too long")
}
// Do some validation before ParseInt/ParseFloat for basic sanity and for
// things that ParseInt/ParseFloat are liberal about.
num := buf[0:i]
// Check for optional leading minus; skip it for other validation
if len(num) >= 1 && num[0] == '-' {
num = num[1:]
}
// Check for empty string
if len(num) == 0 {
return nil, false, d.parseError(nil, "number not found")
}
// Check for number
if num[0] < '0' || num[0] > '9' {
return nil, false, d.parseError(nil, "invalid character in number")
}
if num[0] == '0' && len(num) > 1 && num[1] != '.' && num[1] != 'e' && num[1] != 'E' {
return nil, false, d.parseError(nil, "leading zeros not allowed")
}
// Return the slice without the terminating character.
return buf[0:i], isFloat, nil
}
// peekUint32 works like like peekNumber but only for characters valid
// for a Uint32.
func (d *Decoder) peekUint32() ([]byte, error) {
buf, err := d.peekInt(uint32PeekWidth)
if err != nil {
return nil, err
}
if buf[0] == '-' {
return nil, d.parseError(nil, "negative number not allowed")
}
return buf, nil
}
// peekInt64 works like like peekNumber but only for characters valid
// for a Int64.
func (d *Decoder) peekInt64() ([]byte, error) {
buf, err := d.peekInt(int64PeekWidth)
if err != nil {
return nil, err
}
return buf, nil
}
func (d *Decoder) peekInt(intWidth int) ([]byte, error) {
var terminated bool
buf, err := d.json.Peek(intWidth)
if err != nil {
// here, io.EOF is OK, since we're peeking and may hit end of
// object
if err != io.EOF {
return nil, newReadError(err)
}
}
// Find where the number appears to ends.
var i int
LOOP:
for i = 0; i < len(buf); i++ {
switch buf[i] {
case ' ', '\t', '\n', '\r', ',', ']', '}':
terminated = true
break LOOP
}
}
// Do some validation before ParseInt/ParseFloat for basic sanity and for
// things that ParseInt/ParseFloat are liberal about.
num := buf[0:i]
// Check for empty string
if len(num) == 0 {
return nil, d.parseError(nil, "number not found")
}
// Remove a leading negative sign, if any, before further validation.
if num[0] == '-' {
num = num[1:]
if len(num) == 0 {
return nil, d.parseError(nil, "number not found")
}
}
// Check for number
if num[0] < '0' || num[0] > '9' {
return nil, d.parseError(nil, "invalid character in number")
}
if num[0] == '0' && len(num) > 1 {
return nil, d.parseError(nil, "leading zeros not allowed")
}
if !terminated {
if len(buf) < intWidth {
return nil, newReadError(io.ErrUnexpectedEOF)
}
return nil, d.parseError(nil, "number too long")
}
return buf[0:i], nil
}
// readUint32 consumes a Uint32 value from the input stream or an error
// if the input stream doesn't begin with a Uint32 value.
func (d *Decoder) readUint32() (uint32, error) {
buf, err := d.peekUint32()
if err != nil {
return 0, err
}
n, err := strconv.ParseUint(string(buf), 10, 32)
if err != nil {
return 0, d.parseError(nil, fmt.Sprintf("uint conversion: %v", err))
}
_, _ = d.json.Discard(len(buf))
return uint32(n), nil
}
// readInt64 consumes a Int64 value from the input stream or an error
// if the input stream doesn't begin with an Int64 value.
func (d *Decoder) readInt64() (int64, error) {
buf, err := d.peekInt64()
if err != nil {
return 0, err
}
n, err := strconv.ParseInt(string(buf), 10, 64)
if err != nil {
return 0, d.parseError(nil, fmt.Sprintf("int conversion: %v", err))
}
_, _ = d.json.Discard(len(buf))
return int64(n), nil
}
const parseErrorContextLength = 10
// parseError returns an error with a message and some context for where it occurs.
func (d *Decoder) parseError(startingAt []byte, msg string) error {
if len(startingAt) < parseErrorContextLength {
after, err := d.json.Peek(parseErrorContextLength - len(startingAt))
if len(after) > 0 {
startingAt = append(startingAt, after...)
}
if err == nil {
// Add ellipsis to show there is more
startingAt = append(startingAt, '.', '.', '.')
}
}
if len(startingAt) > 0 {
return &ParseError{msg: fmt.Sprintf("parse error at `%s`: %s", startingAt, msg)}
}
return &ParseError{fmt.Sprintf("parse error: %s", msg)}
}
// copyPeek returns a copy of a Peek into the start of the buffer.
func (d *Decoder) copyPeek(length int) []byte {
buf, _ := d.json.Peek(length)
if len(buf) == 0 {
return nil
}
var out []byte
scratchP := d.scratchPool.Get().(*[]byte)
defer func() { d.scratchPool.Put(scratchP) }()
if len(*scratchP) < len(buf) {
out = make([]byte, len(buf))
scratchP = &out
} else {
out = (*scratchP)[0:len(buf)]
}
copy(out, buf)
return out
}
// Unmarshal converts a single JSON object to a BSON document. The function
// takes an output buffer as an argument. If the buffer is not large enough, a
// new buffer will be allocated on demand. The final buffer is returned, just
// like with `append`. The function returns io.EOF if the input is empty.
func Unmarshal(in []byte, out []byte) ([]byte, error) {
jsonReader := bufio.NewReaderSize(bytes.NewReader([]byte(in)), 8192)
jib, err := NewDecoder(jsonReader)
if err != nil {
return nil, err
}
return jib.Decode(out)
}
// UnmarshalExtJSON converts a single Extended JSON object to a BSON document.
// It otherwise works like `Unmarshal`.
func UnmarshalExtJSON(in []byte, out []byte) ([]byte, error) {
jsonReader := bufio.NewReaderSize(bytes.NewReader([]byte(in)), 8192)
jib, err := NewDecoder(jsonReader)
if err != nil {
return nil, err
}
jib.ExtJSON(true)
return jib.Decode(out)
}
// overwriteTypeByte is a helper for writing a type byte that
// no-ops for a top-level container.
func overwriteTypeByte(out []byte, pos int, bsonType byte) {
// Top-level containers don't have a type byte preceding them
if pos == topContainer {
return
}
out[pos] = bsonType
}
// overwriteLength is a readability helper to write a length to a a particular buffer
// location as little-endian int32.
func overwriteLength(out []byte, pos int, n int) {
binary.LittleEndian.PutUint32(out[pos:pos+4], uint32(n))
}
// handleBOM will detect/discard/error based on the BOM. Inability to peek a BOM is a
// no-op, not an error so it can be handled by the normal parser. Only UTF-8
// BOM is supported; others will error.
func handleBOM(r *bufio.Reader) error {
// Peek 2 byte BOMs
preamble, err := r.Peek(2)
if err != nil {
return nil
}
if bytes.Equal(preamble, utf16BEBOM) || bytes.Equal(preamble, utf16LEBOM) {
return ErrUnsupportedBOM
}
// Peek 3 byte BOM; UTF-8 is supported, so discard them if found.
preamble, err = r.Peek(3)
if err != nil {
return nil
}
if bytes.Equal(preamble, utf8BOM) {
_, _ = r.Discard(3)
}
// Peek 4 byte BOMs
preamble, err = r.Peek(4)
if err != nil {
return nil
}
if bytes.Equal(preamble, utf32BEBOM) || bytes.Equal(preamble, utf32LEBOM) {
return ErrUnsupportedBOM
}
return nil
}
// newReadError is used when we expect to be able to read and fail. If the
// error is EOF, we convert it to UnexpectedEOF because we aren't between
// top-level object.
func newReadError(err error) error {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return fmt.Errorf("read error while parsing: %w", err)
}