tinybson.cpp
13.8 KB
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
617
618
619
620
621
622
623
624
625
626
627
628
/****************************************************************************
*
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file tinybson.c
*
* A simple subset SAX-style BSON parser and generator.
*/
#include <px4_platform_common/posix.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <systemlib/err.h>
#include "tinybson.h"
#if 0
# define debug(fmt, args...) do { PX4_INFO("BSON: " fmt, ##args); } while(0)
#else
# define debug(fmt, args...) do { } while(0)
#endif
#define CODER_CHECK(_c) do { if (_c->dead) { debug("coder dead"); return -1; }} while(0)
#define CODER_KILL(_c, _reason) do { debug("killed: %s", _reason); _c->dead = true; return -1; } while(0)
#define BSON_READ read
#define BSON_WRITE write
#define BSON_FSYNC fsync
static int
read_x(bson_decoder_t decoder, void *p, size_t s)
{
CODER_CHECK(decoder);
if (decoder->fd > -1) {
return (BSON_READ(decoder->fd, p, s) == s) ? 0 : -1;
}
if (decoder->buf != nullptr) {
/* staged operations to avoid integer overflow for corrupt data */
if (s >= decoder->bufsize) {
CODER_KILL(decoder, "buffer too small for read");
}
if ((decoder->bufsize - s) < decoder->bufpos) {
CODER_KILL(decoder, "not enough data for read");
}
memcpy(p, (decoder->buf + decoder->bufpos), s);
decoder->bufpos += s;
return 0;
}
debug("no source");
return -1;
}
static int
read_int8(bson_decoder_t decoder, int8_t *b)
{
return read_x(decoder, b, sizeof(*b));
}
static int
read_int32(bson_decoder_t decoder, int32_t *i)
{
return read_x(decoder, i, sizeof(*i));
}
static int
read_int64(bson_decoder_t decoder, int64_t *i)
{
return read_x(decoder, i, sizeof(*i));
}
static int
read_double(bson_decoder_t decoder, double *d)
{
return read_x(decoder, d, sizeof(*d));
}
int
bson_decoder_init_file(bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *priv)
{
int32_t junk;
decoder->fd = fd;
decoder->buf = nullptr;
decoder->dead = false;
decoder->callback = callback;
decoder->priv = priv;
decoder->nesting = 1;
decoder->pending = 0;
decoder->node.type = BSON_UNDEFINED;
/* read and discard document size */
if (read_int32(decoder, &junk)) {
CODER_KILL(decoder, "failed discarding length");
}
/* ready for decoding */
return 0;
}
int
bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback,
void *priv)
{
int32_t len;
/* argument sanity */
if ((buf == nullptr) || (callback == nullptr)) {
return -1;
}
decoder->fd = -1;
decoder->buf = (uint8_t *)buf;
decoder->dead = false;
if (bufsize == 0) {
decoder->bufsize = *(uint32_t *)buf;
debug("auto-detected %u byte object", decoder->bufsize);
} else {
decoder->bufsize = bufsize;
}
decoder->bufpos = 0;
decoder->callback = callback;
decoder->priv = priv;
decoder->nesting = 1;
decoder->pending = 0;
decoder->node.type = BSON_UNDEFINED;
/* read and discard document size */
if (read_int32(decoder, &len)) {
CODER_KILL(decoder, "failed reading length");
}
if ((len > 0) && (len > (int)decoder->bufsize)) {
CODER_KILL(decoder, "document length larger than buffer");
}
/* ready for decoding */
return 0;
}
int
bson_decoder_next(bson_decoder_t decoder)
{
int8_t tbyte;
int32_t tint;
unsigned nlen;
CODER_CHECK(decoder);
/* if the previous node was EOO, pop a nesting level */
if (decoder->node.type == BSON_EOO) {
if (decoder->nesting > 0) {
decoder->nesting--;
}
/* if the nesting level is now zero, the top-level document is done */
if (decoder->nesting == 0) {
/* like kill but not an error */
debug("nesting is zero, document is done");
decoder->fd = -1;
/* return end-of-file to the caller */
return 0;
}
}
/* if there are unread bytes pending in the stream, discard them */
while (decoder->pending > 0) {
if (read_int8(decoder, &tbyte)) {
CODER_KILL(decoder, "read error discarding pending bytes");
}
decoder->pending--;
}
/* get the type byte */
if (read_int8(decoder, &tbyte)) {
CODER_KILL(decoder, "read error on type byte");
}
decoder->node.type = (bson_type_t)tbyte;
decoder->pending = 0;
debug("got type byte 0x%02x", decoder->node.type);
/* EOO is special; it has no name/data following */
if (decoder->node.type == BSON_EOO) {
decoder->node.name[0] = '\0';
} else {
/* get the node name */
nlen = 0;
for (;;) {
if (nlen >= BSON_MAXNAME) {
CODER_KILL(decoder, "node name overflow");
}
if (read_int8(decoder, (int8_t *)&decoder->node.name[nlen])) {
CODER_KILL(decoder, "read error on node name");
}
if (decoder->node.name[nlen] == '\0') {
break;
}
nlen++;
}
debug("got name '%s'", decoder->node.name);
switch (decoder->node.type) {
case BSON_BOOL:
if (read_int8(decoder, &tbyte)) {
CODER_KILL(decoder, "read error on BSON_BOOL");
}
decoder->node.b = (tbyte != 0);
break;
case BSON_INT32:
if (read_int32(decoder, &tint)) {
CODER_KILL(decoder, "read error on BSON_INT");
}
decoder->node.i = tint;
break;
case BSON_INT64:
if (read_int64(decoder, &decoder->node.i)) {
CODER_KILL(decoder, "read error on BSON_INT");
}
break;
case BSON_DOUBLE:
if (read_double(decoder, &decoder->node.d)) {
CODER_KILL(decoder, "read error on BSON_DOUBLE");
}
break;
case BSON_STRING:
if (read_int32(decoder, &decoder->pending)) {
CODER_KILL(decoder, "read error on BSON_STRING length");
}
break;
case BSON_BINDATA:
if (read_int32(decoder, &decoder->pending)) {
CODER_KILL(decoder, "read error on BSON_BINDATA size");
}
if (read_int8(decoder, &tbyte)) {
CODER_KILL(decoder, "read error on BSON_BINDATA subtype");
}
decoder->node.subtype = (bson_binary_subtype_t)tbyte;
break;
/* XXX currently not supporting other types */
default:
CODER_KILL(decoder, "unsupported node type");
}
}
/* call the callback and pass its results back */
return decoder->callback(decoder, decoder->priv, &decoder->node);
}
int
bson_decoder_copy_data(bson_decoder_t decoder, void *buf)
{
int result;
CODER_CHECK(decoder);
/* copy data */
result = read_x(decoder, buf, decoder->pending);
if (result != 0) {
CODER_KILL(decoder, "read error on copy_data");
}
/* pending count is discharged */
decoder->pending = 0;
return 0;
}
size_t
bson_decoder_data_pending(bson_decoder_t decoder)
{
return decoder->pending;
}
static int
write_x(bson_encoder_t encoder, const void *p, size_t s)
{
CODER_CHECK(encoder);
/* bson file encoder (non-buffered) */
if (encoder->fd > -1 && encoder->buf == nullptr) {
return (BSON_WRITE(encoder->fd, p, s) == (int)s) ? 0 : -1;
}
/* do we need to extend the buffer? */
while ((encoder->bufpos + s) > encoder->bufsize) {
/* bson buffered file encoder */
if (encoder->fd > -1) {
// write to disk
debug("writing buffer (%d) to disk", encoder->bufpos);
int ret = BSON_WRITE(encoder->fd, encoder->buf, encoder->bufpos);
if (ret == (int)encoder->bufpos) {
// reset buffer to beginning and continue
encoder->bufpos = 0;
if ((encoder->bufpos + s) > encoder->bufsize) {
CODER_KILL(encoder, "fixed-size buffer overflow");
}
break;
} else {
CODER_KILL(encoder, "file write error");
}
}
if (!encoder->realloc_ok) {
CODER_KILL(encoder, "fixed-size buffer overflow");
}
uint8_t *newbuf = (uint8_t *)realloc(encoder->buf, encoder->bufsize + BSON_BUF_INCREMENT);
if (newbuf == nullptr) {
CODER_KILL(encoder, "could not grow buffer");
}
encoder->buf = newbuf;
encoder->bufsize += BSON_BUF_INCREMENT;
debug("allocated %d bytes", BSON_BUF_INCREMENT);
}
memcpy(encoder->buf + encoder->bufpos, p, s);
encoder->bufpos += s;
debug("appended %d bytes", s);
return 0;
}
static int
write_int8(bson_encoder_t encoder, int8_t b)
{
return write_x(encoder, &b, sizeof(b));
}
static int
write_int32(bson_encoder_t encoder, int32_t i)
{
return write_x(encoder, &i, sizeof(i));
}
static int
write_int64(bson_encoder_t encoder, int64_t i)
{
return write_x(encoder, &i, sizeof(i));
}
static int
write_double(bson_encoder_t encoder, double d)
{
return write_x(encoder, &d, sizeof(d));
}
static int
write_name(bson_encoder_t encoder, const char *name)
{
size_t len = strlen(name);
if (len > BSON_MAXNAME) {
CODER_KILL(encoder, "node name too long");
}
return write_x(encoder, name, len + 1);
}
int
bson_encoder_init_file(bson_encoder_t encoder, int fd)
{
encoder->fd = fd;
encoder->buf = nullptr;
encoder->dead = false;
if (write_int32(encoder, 0)) {
CODER_KILL(encoder, "write error on document length");
}
return 0;
}
int
bson_encoder_init_buf_file(bson_encoder_t encoder, int fd, void *buf, unsigned bufsize)
{
encoder->fd = fd;
encoder->buf = (uint8_t *)buf;
encoder->bufpos = 0;
encoder->bufsize = bufsize;
encoder->dead = false;
encoder->realloc_ok = false;
if (write_int32(encoder, 0)) {
CODER_KILL(encoder, "write error on document length");
}
return 0;
}
int
bson_encoder_init_buf(bson_encoder_t encoder, void *buf, unsigned bufsize)
{
encoder->fd = -1;
encoder->buf = (uint8_t *)buf;
encoder->bufpos = 0;
encoder->dead = false;
if (encoder->buf == nullptr) {
encoder->bufsize = 0;
encoder->realloc_ok = true;
} else {
encoder->bufsize = bufsize;
encoder->realloc_ok = false;
}
if (write_int32(encoder, 0)) {
CODER_KILL(encoder, "write error on document length");
}
return 0;
}
int
bson_encoder_fini(bson_encoder_t encoder)
{
CODER_CHECK(encoder);
if (write_int8(encoder, BSON_EOO)) {
CODER_KILL(encoder, "write error on document terminator");
}
if (encoder->fd > -1 && encoder->buf != nullptr) {
/* write final buffer to disk */
int ret = BSON_WRITE(encoder->fd, encoder->buf, encoder->bufpos);
if (ret != (int)encoder->bufpos) {
CODER_KILL(encoder, "write error");
}
} else if (encoder->buf != nullptr) {
/* update buffer length */
int32_t len = bson_encoder_buf_size(encoder);
memcpy(encoder->buf, &len, sizeof(len));
}
/* sync file */
if (encoder->fd > -1) {
BSON_FSYNC(encoder->fd);
}
return 0;
}
int
bson_encoder_buf_size(bson_encoder_t encoder)
{
CODER_CHECK(encoder);
if (encoder->fd > -1) {
return -1;
}
return encoder->bufpos;
}
void *
bson_encoder_buf_data(bson_encoder_t encoder)
{
/* note, no CODER_CHECK here as the caller has to clean up dead buffers */
if (encoder->fd > -1) {
return nullptr;
}
return encoder->buf;
}
int bson_encoder_append_bool(bson_encoder_t encoder, const char *name, bool value)
{
CODER_CHECK(encoder);
if (write_int8(encoder, BSON_BOOL) ||
write_name(encoder, name) ||
write_int8(encoder, value ? 1 : 0)) {
CODER_KILL(encoder, "write error on BSON_BOOL");
}
return 0;
}
int
bson_encoder_append_int(bson_encoder_t encoder, const char *name, int64_t value)
{
bool result;
CODER_CHECK(encoder);
/* use the smallest encoding that will hold the value */
if (value == (int32_t)value) {
debug("encoding %lld as int32", value);
result = write_int8(encoder, BSON_INT32) ||
write_name(encoder, name) ||
write_int32(encoder, value);
} else {
debug("encoding %lld as int64", value);
result = write_int8(encoder, BSON_INT64) ||
write_name(encoder, name) ||
write_int64(encoder, value);
}
if (result) {
CODER_KILL(encoder, "write error on BSON_INT");
}
return 0;
}
int
bson_encoder_append_double(bson_encoder_t encoder, const char *name, double value)
{
CODER_CHECK(encoder);
if (write_int8(encoder, BSON_DOUBLE) ||
write_name(encoder, name) ||
write_double(encoder, value)) {
CODER_KILL(encoder, "write error on BSON_DOUBLE");
}
return 0;
}
int
bson_encoder_append_string(bson_encoder_t encoder, const char *name, const char *string)
{
size_t len;
CODER_CHECK(encoder);
len = strlen(string) + 1; /* include trailing nul */
if (write_int8(encoder, BSON_STRING) ||
write_name(encoder, name) ||
write_int32(encoder, len) ||
write_x(encoder, string, len)) {
CODER_KILL(encoder, "write error on BSON_STRING");
}
return 0;
}
int
bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype, size_t size,
const void *data)
{
CODER_CHECK(encoder);
if (write_int8(encoder, BSON_BINDATA) ||
write_name(encoder, name) ||
write_int32(encoder, size) ||
write_int8(encoder, subtype) ||
write_x(encoder, data, size)) {
CODER_KILL(encoder, "write error on BSON_BINDATA");
}
return 0;
}