1 /* bson.c */
2
3 /* Copyright 2009, 2010 10gen Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <cstdlib>
19 #include <cstring>
20 #include <cstdio>
21 #include <ctime>
22 #include <climits>
23
24 #include "BSON.h"
25
26 const int initialBufferSize = 128;
27
28 /* only need one of these */
29 static const int zero = 0;
30
31 /* Custom standard function pointers. */
32 void *( *bson_malloc_func )( size_t ) = malloc;
33 void *( *bson_realloc_func )( void *, size_t ) = realloc;
34 void ( *bson_free )( void * ) = free;
35 bson_printf_func bson_printf = printf;
36 bson_fprintf_func bson_fprintf = fprintf;
37 bson_sprintf_func bson_sprintf = sprintf;
38
39 static int _bson_errprintf( const char *, ... );
40 bson_printf_func bson_errprintf = _bson_errprintf;
41
42 /* ObjectId fuzz functions. */
43 static int ( *oid_fuzz_func )( void ) = NULL;
44 static int ( *oid_inc_func )( void ) = NULL;
45
46 /* ----------------------------
47 READING
48 ------------------------------ */
49
bson_empty(bson * obj)50 bson *bson_empty( bson *obj ) {
51 static char data[] = "\005\0\0\0\0";
52 bson_init_data( obj, data );
53 obj->finished = 1;
54 obj->err = 0;
55 obj->stackPos = 0;
56 return obj;
57 }
58
bson_copy(bson * out,const bson * in)59 int bson_copy( bson *out, const bson *in ) {
60 if ( !out ) return BSON_ERROR;
61 if ( !in->finished ) return BSON_ERROR;
62 bson_init_size( out, bson_size( in ) );
63 memcpy( out->data, in->data, bson_size( in ) );
64 out->finished = 1;
65
66 return BSON_OK;
67 }
68
bson_init_data(bson * b,char * data)69 int bson_init_data( bson *b, char *data ) {
70 b->data = data;
71 b->dataSize = INT_MAX; // no overflow detection for bson_iterator_next
72 return BSON_OK;
73 }
74
bson_init_data_size(bson * b,char * data,int size)75 int bson_init_data_size( bson *b, char *data, int size ) {
76 b->data = data;
77 b->dataSize = size; // used for overflow detection for bson_iterator_next
78 return BSON_OK;
79 }
80
bson_init_finished_data(bson * b,char * data)81 int bson_init_finished_data( bson *b, char *data ) {
82 bson_init_data( b, data );
83 b->stackPos = 0;
84 b->finished = 1;
85 return BSON_OK;
86 }
87
_bson_reset(bson * b)88 static void _bson_reset( bson *b ) {
89 b->finished = 0;
90 b->stackPos = 0;
91 b->err = 0;
92 b->errstr = NULL;
93 }
94
bson_size(const bson * b)95 int bson_size( const bson *b ) {
96 int i;
97 if ( ! b || ! b->data )
98 return 0;
99 bson_little_endian32( &i, b->data );
100 return i;
101 }
102
bson_data(bson * b)103 const char *bson_data( bson *b ) {
104 return (const char *)b->data;
105 }
106
hexbyte(char hex)107 static char hexbyte( char hex ) {
108 switch ( hex ) {
109 case '0':
110 return 0x0;
111 case '1':
112 return 0x1;
113 case '2':
114 return 0x2;
115 case '3':
116 return 0x3;
117 case '4':
118 return 0x4;
119 case '5':
120 return 0x5;
121 case '6':
122 return 0x6;
123 case '7':
124 return 0x7;
125 case '8':
126 return 0x8;
127 case '9':
128 return 0x9;
129 case 'a':
130 case 'A':
131 return 0xa;
132 case 'b':
133 case 'B':
134 return 0xb;
135 case 'c':
136 case 'C':
137 return 0xc;
138 case 'd':
139 case 'D':
140 return 0xd;
141 case 'e':
142 case 'E':
143 return 0xe;
144 case 'f':
145 case 'F':
146 return 0xf;
147 default:
148 return 0x0; /* something smarter? */
149 }
150 }
151
bson_oid_from_string(bson_oid_t * oid,const char * str)152 void bson_oid_from_string( bson_oid_t *oid, const char *str ) {
153 int i;
154 for ( i=0; i<12; i++ ) {
155 oid->bytes[i] = ( hexbyte( str[2*i] ) << 4 ) | hexbyte( str[2*i + 1] );
156 }
157 }
158
bson_oid_to_string(const bson_oid_t * oid,char * str)159 void bson_oid_to_string( const bson_oid_t *oid, char *str ) {
160 static const char hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
161 int i;
162 for ( i=0; i<12; i++ ) {
163 str[2*i] = hex[( oid->bytes[i] & 0xf0 ) >> 4];
164 str[2*i + 1] = hex[ oid->bytes[i] & 0x0f ];
165 }
166 str[24] = '\0';
167 }
168
bson_set_oid_fuzz(int (* func)(void))169 void bson_set_oid_fuzz( int ( *func )( void ) ) {
170 oid_fuzz_func = func;
171 }
172
bson_set_oid_inc(int (* func)(void))173 void bson_set_oid_inc( int ( *func )( void ) ) {
174 oid_inc_func = func;
175 }
176
bson_oid_gen(bson_oid_t * oid)177 void bson_oid_gen( bson_oid_t *oid ) {
178 static int incr = 0;
179 static int fuzz = 0;
180 int i;
181 int t = time( NULL );
182
183 if( oid_inc_func )
184 i = oid_inc_func();
185 else
186 i = incr++;
187
188 if ( !fuzz ) {
189 if ( oid_fuzz_func )
190 fuzz = oid_fuzz_func();
191 else {
192 srand( t );
193 fuzz = rand();
194 }
195 }
196
197 bson_big_endian32( &oid->ints[0], &t );
198 oid->ints[1] = fuzz;
199 bson_big_endian32( &oid->ints[2], &i );
200 }
201
bson_oid_generated_time(bson_oid_t * oid)202 time_t bson_oid_generated_time( bson_oid_t *oid ) {
203 time_t out;
204 bson_big_endian32( &out, &oid->ints[0] );
205
206 return out;
207 }
208
bson_print(bson * b)209 void bson_print( bson *b ) {
210 bson_print_raw( b->data , 0 );
211 }
212
bson_print_raw(const char * data,int depth)213 void bson_print_raw( const char *data , int depth ) {
214 bson_iterator i;
215 const char *key;
216 int temp;
217 bson_timestamp_t ts;
218 char oidhex[25];
219 bson scope;
220 bson_iterator_from_buffer( &i, data );
221
222 while ( bson_iterator_next( &i ) ) {
223 bson_type t = bson_iterator_type( &i );
224 if ( t == 0 )
225 break;
226 key = bson_iterator_key( &i );
227
228 for ( temp=0; temp<=depth; temp++ )
229 bson_printf( "\t" );
230 bson_printf( "%s : %d \t " , key , t );
231 switch ( t ) {
232 case BSON_DOUBLE:
233 bson_printf( "%f" , bson_iterator_double( &i ) );
234 break;
235 case BSON_STRING:
236 bson_printf( "%s" , bson_iterator_string( &i ) );
237 break;
238 case BSON_SYMBOL:
239 bson_printf( "SYMBOL: %s" , bson_iterator_string( &i ) );
240 break;
241 case BSON_OID:
242 bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
243 bson_printf( "%s" , oidhex );
244 break;
245 case BSON_BOOL:
246 bson_printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" );
247 break;
248 case BSON_DATE:
249 bson_printf( "%ld" , ( long int )bson_iterator_date( &i ) );
250 break;
251 case BSON_BINDATA:
252 bson_printf( "BSON_BINDATA" );
253 break;
254 case BSON_UNDEFINED:
255 bson_printf( "BSON_UNDEFINED" );
256 break;
257 case BSON_NULL:
258 bson_printf( "BSON_NULL" );
259 break;
260 case BSON_REGEX:
261 bson_printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) );
262 break;
263 case BSON_CODE:
264 bson_printf( "BSON_CODE: %s", bson_iterator_code( &i ) );
265 break;
266 case BSON_CODEWSCOPE:
267 bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
268 bson_init( &scope );
269 bson_iterator_code_scope( &i, &scope );
270 bson_printf( "\n\t SCOPE: " );
271 bson_print( &scope );
272 break;
273 case BSON_INT:
274 bson_printf( "%d" , bson_iterator_int( &i ) );
275 break;
276 case BSON_LONG:
277 bson_printf( "%lld" , ( uint64_t )bson_iterator_long( &i ) );
278 break;
279 case BSON_TIMESTAMP:
280 ts = bson_iterator_timestamp( &i );
281 bson_printf( "i: %d, t: %d", ts.i, ts.t );
282 break;
283 case BSON_OBJECT:
284 case BSON_ARRAY:
285 bson_printf( "\n" );
286 bson_print_raw( bson_iterator_value( &i ) , depth + 1 );
287 break;
288 default:
289 bson_errprintf( "can't print type : %d\n" , t );
290 }
291 bson_printf( "\n" );
292 }
293 }
294
295 /* ----------------------------
296 ITERATOR
297 ------------------------------ */
298
bson_iterator_init(bson_iterator * i,const bson * b)299 void bson_iterator_init( bson_iterator *i, const bson *b ) {
300 i->cur = b->data + 4;
301 i->first = 1;
302 i->last = b->data + b->dataSize;
303 }
304
bson_iterator_from_buffer(bson_iterator * i,const char * buffer)305 void bson_iterator_from_buffer( bson_iterator *i, const char *buffer ) {
306 i->cur = buffer + 4;
307 i->first = 1;
308 i->last = NULL;
309 }
310
bson_find(bson_iterator * it,const bson * obj,const char * name)311 bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) {
312 bson_iterator_init( it, (bson *)obj );
313 while( bson_iterator_next( it ) ) {
314 if ( strcmp( name, bson_iterator_key( it ) ) == 0 )
315 break;
316 }
317 return bson_iterator_type( it );
318 }
319
bson_iterator_more(const bson_iterator * i)320 bson_bool_t bson_iterator_more( const bson_iterator *i ) {
321 if (i->last && i->cur >= i->last)
322 return BSON_EOO;
323 return *( i->cur );
324 }
325
bson_iterator_next(bson_iterator * i)326 bson_type bson_iterator_next( bson_iterator *i ) {
327 int ds;
328
329 if ( i->first ) {
330 i->first = 0;
331 return ( bson_type )( *i->cur );
332 }
333
334 switch ( bson_iterator_type( i ) ) {
335 case BSON_EOO:
336 return BSON_EOO; /* don't advance */
337 case BSON_UNDEFINED:
338 case BSON_NULL:
339 ds = 0;
340 break;
341 case BSON_BOOL:
342 ds = 1;
343 break;
344 case BSON_INT:
345 ds = 4;
346 break;
347 case BSON_LONG:
348 case BSON_DOUBLE:
349 case BSON_TIMESTAMP:
350 case BSON_DATE:
351 ds = 8;
352 break;
353 case BSON_OID:
354 ds = 12;
355 break;
356 case BSON_STRING:
357 case BSON_SYMBOL:
358 case BSON_CODE:
359 ds = 4 + bson_iterator_int_raw( i );
360 break;
361 case BSON_BINDATA:
362 ds = 5 + bson_iterator_int_raw( i );
363 break;
364 case BSON_OBJECT:
365 case BSON_ARRAY:
366 case BSON_CODEWSCOPE:
367 ds = bson_iterator_int_raw( i );
368 break;
369 case BSON_DBREF:
370 ds = 4+12 + bson_iterator_int_raw( i );
371 break;
372 case BSON_REGEX: {
373 const char *s = bson_iterator_value( i );
374 const char *p = s;
375 p += strlen( p )+1;
376 p += strlen( p )+1;
377 ds = p-s;
378 break;
379 }
380
381 default: {
382 char msg[] = "unknown type: 000000000000";
383 bson_numstr( msg+14, ( unsigned )( i->cur[0] ) );
384 bson_fatal_msg( 0, msg );
385 return (bson_type)0;
386 }
387 }
388
389 i->cur += 1 + strlen( i->cur + 1 ) + 1 + ds;
390
391 if (i->last && i->cur >= i->last)
392 return BSON_EOO;
393 return ( bson_type )( *i->cur );
394 }
395
bson_iterator_type(const bson_iterator * i)396 bson_type bson_iterator_type( const bson_iterator *i ) {
397 return ( bson_type )i->cur[0];
398 }
399
bson_iterator_key(const bson_iterator * i)400 const char *bson_iterator_key( const bson_iterator *i ) {
401 return i->cur + 1;
402 }
403
bson_iterator_value(const bson_iterator * i)404 const char *bson_iterator_value( const bson_iterator *i ) {
405 const char *t = i->cur + 1;
406 t += strlen( t ) + 1;
407 return t;
408 }
409
410 /* types */
411
bson_iterator_int_raw(const bson_iterator * i)412 int bson_iterator_int_raw( const bson_iterator *i ) {
413 int out;
414 bson_little_endian32( &out, bson_iterator_value( i ) );
415 return out;
416 }
417
bson_iterator_double_raw(const bson_iterator * i)418 double bson_iterator_double_raw( const bson_iterator *i ) {
419 double out;
420 bson_little_endian64( &out, bson_iterator_value( i ) );
421 return out;
422 }
423
bson_iterator_long_raw(const bson_iterator * i)424 int64_t bson_iterator_long_raw( const bson_iterator *i ) {
425 int64_t out;
426 bson_little_endian64( &out, bson_iterator_value( i ) );
427 return out;
428 }
429
bson_iterator_bool_raw(const bson_iterator * i)430 bson_bool_t bson_iterator_bool_raw( const bson_iterator *i ) {
431 return bson_iterator_value( i )[0];
432 }
433
bson_iterator_oid(const bson_iterator * i)434 bson_oid_t *bson_iterator_oid( const bson_iterator *i ) {
435 return ( bson_oid_t * )bson_iterator_value( i );
436 }
437
bson_iterator_int(const bson_iterator * i)438 int bson_iterator_int( const bson_iterator *i ) {
439 switch ( bson_iterator_type( i ) ) {
440 case BSON_INT:
441 return bson_iterator_int_raw( i );
442 case BSON_LONG:
443 return bson_iterator_long_raw( i );
444 case BSON_DOUBLE:
445 return bson_iterator_double_raw( i );
446 default:
447 return 0;
448 }
449 }
450
bson_iterator_double(const bson_iterator * i)451 double bson_iterator_double( const bson_iterator *i ) {
452 switch ( bson_iterator_type( i ) ) {
453 case BSON_INT:
454 return bson_iterator_int_raw( i );
455 case BSON_LONG:
456 return bson_iterator_long_raw( i );
457 case BSON_DOUBLE:
458 return bson_iterator_double_raw( i );
459 default:
460 return 0;
461 }
462 }
463
bson_iterator_long(const bson_iterator * i)464 int64_t bson_iterator_long( const bson_iterator *i ) {
465 switch ( bson_iterator_type( i ) ) {
466 case BSON_INT:
467 return bson_iterator_int_raw( i );
468 case BSON_LONG:
469 return bson_iterator_long_raw( i );
470 case BSON_DOUBLE:
471 return bson_iterator_double_raw( i );
472 default:
473 return 0;
474 }
475 }
476
bson_iterator_timestamp(const bson_iterator * i)477 bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ) {
478 bson_timestamp_t ts;
479 bson_little_endian32( &( ts.i ), bson_iterator_value( i ) );
480 bson_little_endian32( &( ts.t ), bson_iterator_value( i ) + 4 );
481 return ts;
482 }
483
bson_iterator_bool(const bson_iterator * i)484 bson_bool_t bson_iterator_bool( const bson_iterator *i ) {
485 switch ( bson_iterator_type( i ) ) {
486 case BSON_BOOL:
487 return bson_iterator_bool_raw( i );
488 case BSON_INT:
489 return bson_iterator_int_raw( i ) != 0;
490 case BSON_LONG:
491 return bson_iterator_long_raw( i ) != 0;
492 case BSON_DOUBLE:
493 return bson_iterator_double_raw( i ) != 0;
494 case BSON_EOO:
495 case BSON_NULL:
496 return 0;
497 default:
498 return 1;
499 }
500 }
501
bson_iterator_string(const bson_iterator * i)502 const char *bson_iterator_string( const bson_iterator *i ) {
503 return bson_iterator_value( i ) + 4;
504 }
505
bson_iterator_string_len(const bson_iterator * i)506 int bson_iterator_string_len( const bson_iterator *i ) {
507 return bson_iterator_int_raw( i );
508 }
509
bson_iterator_code(const bson_iterator * i)510 const char *bson_iterator_code( const bson_iterator *i ) {
511 switch ( bson_iterator_type( i ) ) {
512 case BSON_STRING:
513 case BSON_CODE:
514 return bson_iterator_value( i ) + 4;
515 case BSON_CODEWSCOPE:
516 return bson_iterator_value( i ) + 8;
517 default:
518 return NULL;
519 }
520 }
521
bson_iterator_code_scope(const bson_iterator * i,bson * scope)522 void bson_iterator_code_scope( const bson_iterator *i, bson *scope ) {
523 if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) {
524 int code_len;
525 bson_little_endian32( &code_len, bson_iterator_value( i )+4 );
526 bson_init_data( scope, (char*)((void *)(bson_iterator_value(i)+8+code_len )));
527 _bson_reset( scope );
528 scope->finished = 1;
529 } else {
530 bson_empty( scope );
531 }
532 }
533
bson_iterator_date(const bson_iterator * i)534 bson_date_t bson_iterator_date( const bson_iterator *i ) {
535 return bson_iterator_long_raw( i );
536 }
537
bson_iterator_time_t(const bson_iterator * i)538 time_t bson_iterator_time_t( const bson_iterator *i ) {
539 return bson_iterator_date( i ) / 1000;
540 }
541
bson_iterator_bin_len(const bson_iterator * i)542 int bson_iterator_bin_len( const bson_iterator *i ) {
543 return ( bson_iterator_bin_type( i ) == BSON_BIN_BINARY_OLD )
544 ? bson_iterator_int_raw( i ) - 4
545 : bson_iterator_int_raw( i );
546 }
547
bson_iterator_bin_type(const bson_iterator * i)548 char bson_iterator_bin_type( const bson_iterator *i ) {
549 return bson_iterator_value( i )[4];
550 }
551
bson_iterator_bin_data(const bson_iterator * i)552 const char *bson_iterator_bin_data( const bson_iterator *i ) {
553 return ( bson_iterator_bin_type( i ) == BSON_BIN_BINARY_OLD )
554 ? bson_iterator_value( i ) + 9
555 : bson_iterator_value( i ) + 5;
556 }
557
bson_iterator_regex(const bson_iterator * i)558 const char *bson_iterator_regex( const bson_iterator *i ) {
559 return bson_iterator_value( i );
560 }
561
bson_iterator_regex_opts(const bson_iterator * i)562 const char *bson_iterator_regex_opts( const bson_iterator *i ) {
563 const char *p = bson_iterator_value( i );
564 return p + strlen( p ) + 1;
565
566 }
567
bson_iterator_subobject(const bson_iterator * i,bson * sub)568 void bson_iterator_subobject( const bson_iterator *i, bson *sub ) {
569 bson_init_data( sub, ( char * )bson_iterator_value( i ) );
570 _bson_reset( sub );
571 sub->finished = 1;
572 }
573
bson_iterator_subiterator(const bson_iterator * i,bson_iterator * sub)574 void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ) {
575 bson_iterator_from_buffer( sub, bson_iterator_value( i ) );
576 }
577
578 /* ----------------------------
579 BUILDING
580 ------------------------------ */
581
_bson_init_size(bson * b,int size)582 static void _bson_init_size( bson *b, int size ) {
583 if( size == 0 )
584 b->data = NULL;
585 else
586 b->data = ( char * )bson_malloc( size );
587 b->dataSize = size;
588 b->cur = b->data + 4;
589 _bson_reset( b );
590 }
591
bson_init(bson * b)592 void bson_init( bson *b ) {
593 _bson_init_size( b, initialBufferSize );
594 }
595
bson_init_size(bson * b,int size)596 void bson_init_size( bson *b, int size ) {
597 _bson_init_size( b, size );
598 }
599
bson_append_byte(bson * b,char c)600 static void bson_append_byte( bson *b, char c ) {
601 b->cur[0] = c;
602 b->cur++;
603 }
604
bson_append(bson * b,const void * data,int len)605 static void bson_append( bson *b, const void *data, int len ) {
606 memcpy( b->cur , data , len );
607 b->cur += len;
608 }
609
bson_append32(bson * b,const void * data)610 static void bson_append32( bson *b, const void *data ) {
611 bson_little_endian32( b->cur, data );
612 b->cur += 4;
613 }
614
bson_append64(bson * b,const void * data)615 static void bson_append64( bson *b, const void *data ) {
616 bson_little_endian64( b->cur, data );
617 b->cur += 8;
618 }
619
bson_ensure_space(bson * b,const int bytesNeeded)620 int bson_ensure_space( bson *b, const int bytesNeeded ) {
621 int pos = b->cur - b->data;
622 char *orig = b->data;
623 int new_size;
624
625 if ( pos + bytesNeeded <= b->dataSize )
626 return BSON_OK;
627
628 new_size = 1.5 * ( b->dataSize + bytesNeeded );
629
630 if( new_size < b->dataSize ) {
631 if( ( b->dataSize + bytesNeeded ) < INT_MAX )
632 new_size = INT_MAX;
633 else {
634 b->err = BSON_SIZE_OVERFLOW;
635 return BSON_ERROR;
636 }
637 }
638
639 b->data = (char*)bson_realloc( b->data, new_size );
640 if ( !b->data )
641 bson_fatal_msg( !!b->data, "realloc() failed" );
642
643 b->dataSize = new_size;
644 b->cur += b->data - orig;
645
646 return BSON_OK;
647 }
648
bson_finish(bson * b)649 int bson_finish( bson *b ) {
650 int i;
651
652 if( b->err & BSON_NOT_UTF8 )
653 return BSON_ERROR;
654
655 if ( ! b->finished ) {
656 if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
657 bson_append_byte( b, 0 );
658 i = b->cur - b->data;
659 bson_little_endian32( b->data, &i );
660 b->finished = 1;
661 }
662
663 return BSON_OK;
664 }
665
bson_destroy(bson * b)666 void bson_destroy( bson *b ) {
667 if (b->data)
668 bson_free( b->data );
669 b->err = 0;
670 b->data = 0;
671 b->cur = 0;
672 b->finished = 1;
673 }
674
bson_append_estart(bson * b,int type,const char * name,const int dataSize)675 static int bson_append_estart( bson *b, int type, const char *name, const int dataSize ) {
676 const int len = strlen( name ) + 1;
677
678 if ( b->finished ) {
679 b->err |= BSON_ALREADY_FINISHED;
680 return BSON_ERROR;
681 }
682
683 if ( bson_ensure_space( b, 1 + len + dataSize ) == BSON_ERROR ) {
684 return BSON_ERROR;
685 }
686
687 if( bson_check_field_name( b, ( const char * )name, len - 1 ) == BSON_ERROR ) {
688 bson_builder_error( b );
689 return BSON_ERROR;
690 }
691
692 bson_append_byte( b, ( char )type );
693 bson_append( b, name, len );
694 return BSON_OK;
695 }
696
697 /* ----------------------------
698 BUILDING TYPES
699 ------------------------------ */
700
bson_append_int(bson * b,const char * name,const int i)701 int bson_append_int( bson *b, const char *name, const int i ) {
702 if ( bson_append_estart( b, BSON_INT, name, 4 ) == BSON_ERROR )
703 return BSON_ERROR;
704 bson_append32( b , &i );
705 return BSON_OK;
706 }
707
bson_append_long(bson * b,const char * name,const int64_t i)708 int bson_append_long( bson *b, const char *name, const int64_t i ) {
709 if ( bson_append_estart( b , BSON_LONG, name, 8 ) == BSON_ERROR )
710 return BSON_ERROR;
711 bson_append64( b , &i );
712 return BSON_OK;
713 }
714
bson_append_double(bson * b,const char * name,const double d)715 int bson_append_double( bson *b, const char *name, const double d ) {
716 if ( bson_append_estart( b, BSON_DOUBLE, name, 8 ) == BSON_ERROR )
717 return BSON_ERROR;
718 bson_append64( b , &d );
719 return BSON_OK;
720 }
721
bson_append_bool(bson * b,const char * name,const bson_bool_t i)722 int bson_append_bool( bson *b, const char *name, const bson_bool_t i ) {
723 if ( bson_append_estart( b, BSON_BOOL, name, 1 ) == BSON_ERROR )
724 return BSON_ERROR;
725 bson_append_byte( b , i != 0 );
726 return BSON_OK;
727 }
728
bson_append_null(bson * b,const char * name)729 int bson_append_null( bson *b, const char *name ) {
730 if ( bson_append_estart( b , BSON_NULL, name, 0 ) == BSON_ERROR )
731 return BSON_ERROR;
732 return BSON_OK;
733 }
734
bson_append_undefined(bson * b,const char * name)735 int bson_append_undefined( bson *b, const char *name ) {
736 if ( bson_append_estart( b, BSON_UNDEFINED, name, 0 ) == BSON_ERROR )
737 return BSON_ERROR;
738 return BSON_OK;
739 }
740
bson_append_string_base(bson * b,const char * name,const char * value,int len,bson_type type)741 static int bson_append_string_base( bson *b, const char *name,
742 const char *value, int len, bson_type type ) {
743
744 int sl = len + 1;
745 if ( bson_check_string( b, ( const char * )value, sl - 1 ) == BSON_ERROR )
746 return BSON_ERROR;
747 if ( bson_append_estart( b, type, name, 4 + sl ) == BSON_ERROR ) {
748 return BSON_ERROR;
749 }
750 bson_append32( b , &sl );
751 bson_append( b , value , sl - 1 );
752 bson_append( b , "\0" , 1 );
753 return BSON_OK;
754 }
755
bson_append_string(bson * b,const char * name,const char * value)756 int bson_append_string( bson *b, const char *name, const char *value ) {
757 return bson_append_string_base( b, name, value, strlen ( value ), BSON_STRING );
758 }
759
bson_append_symbol(bson * b,const char * name,const char * value)760 int bson_append_symbol( bson *b, const char *name, const char *value ) {
761 return bson_append_string_base( b, name, value, strlen ( value ), BSON_SYMBOL );
762 }
763
bson_append_code(bson * b,const char * name,const char * value)764 int bson_append_code( bson *b, const char *name, const char *value ) {
765 return bson_append_string_base( b, name, value, strlen ( value ), BSON_CODE );
766 }
767
bson_append_string_n(bson * b,const char * name,const char * value,int len)768 int bson_append_string_n( bson *b, const char *name, const char *value, int len ) {
769 return bson_append_string_base( b, name, value, len, BSON_STRING );
770 }
771
bson_append_symbol_n(bson * b,const char * name,const char * value,int len)772 int bson_append_symbol_n( bson *b, const char *name, const char *value, int len ) {
773 return bson_append_string_base( b, name, value, len, BSON_SYMBOL );
774 }
775
bson_append_code_n(bson * b,const char * name,const char * value,int len)776 int bson_append_code_n( bson *b, const char *name, const char *value, int len ) {
777 return bson_append_string_base( b, name, value, len, BSON_CODE );
778 }
779
bson_append_code_w_scope_n(bson * b,const char * name,const char * code,int len,const bson * scope)780 int bson_append_code_w_scope_n( bson *b, const char *name,
781 const char *code, int len, const bson *scope ) {
782
783 int sl = len + 1;
784 int size = 4 + 4 + sl + bson_size( scope );
785 if ( bson_append_estart( b, BSON_CODEWSCOPE, name, size ) == BSON_ERROR )
786 return BSON_ERROR;
787 bson_append32( b, &size );
788 bson_append32( b, &sl );
789 bson_append( b, code, sl );
790 bson_append( b, scope->data, bson_size( scope ) );
791 return BSON_OK;
792 }
793
bson_append_code_w_scope(bson * b,const char * name,const char * code,const bson * scope)794 int bson_append_code_w_scope( bson *b, const char *name, const char *code, const bson *scope ) {
795 return bson_append_code_w_scope_n( b, name, code, strlen ( code ), scope );
796 }
797
bson_append_binary(bson * b,const char * name,char type,const char * str,int len)798 int bson_append_binary( bson *b, const char *name, char type, const char *str, int len ) {
799 if ( type == BSON_BIN_BINARY_OLD ) {
800 int subtwolen = len + 4;
801 if ( bson_append_estart( b, BSON_BINDATA, name, 4+1+4+len ) == BSON_ERROR )
802 return BSON_ERROR;
803 bson_append32( b, &subtwolen );
804 bson_append_byte( b, type );
805 bson_append32( b, &len );
806 bson_append( b, str, len );
807 } else {
808 if ( bson_append_estart( b, BSON_BINDATA, name, 4+1+len ) == BSON_ERROR )
809 return BSON_ERROR;
810 bson_append32( b, &len );
811 bson_append_byte( b, type );
812 bson_append( b, str, len );
813 }
814 return BSON_OK;
815 }
816
bson_append_oid(bson * b,const char * name,const bson_oid_t * oid)817 int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid ) {
818 if ( bson_append_estart( b, BSON_OID, name, 12 ) == BSON_ERROR )
819 return BSON_ERROR;
820 bson_append( b , oid , 12 );
821 return BSON_OK;
822 }
823
bson_append_new_oid(bson * b,const char * name)824 int bson_append_new_oid( bson *b, const char *name ) {
825 bson_oid_t oid;
826 bson_oid_gen( &oid );
827 return bson_append_oid( b, name, &oid );
828 }
829
bson_append_regex(bson * b,const char * name,const char * pattern,const char * opts)830 int bson_append_regex( bson *b, const char *name, const char *pattern, const char *opts ) {
831 const int plen = strlen( pattern )+1;
832 const int olen = strlen( opts )+1;
833 if ( bson_append_estart( b, BSON_REGEX, name, plen + olen ) == BSON_ERROR )
834 return BSON_ERROR;
835 if ( bson_check_string( b, pattern, plen - 1 ) == BSON_ERROR )
836 return BSON_ERROR;
837 bson_append( b , pattern , plen );
838 bson_append( b , opts , olen );
839 return BSON_OK;
840 }
841
bson_append_bson(bson * b,const char * name,const bson * bson)842 int bson_append_bson( bson *b, const char *name, const bson *bson ) {
843 if ( bson_append_estart( b, BSON_OBJECT, name, bson_size( bson ) ) == BSON_ERROR )
844 return BSON_ERROR;
845 bson_append( b , bson->data , bson_size( bson ) );
846 return BSON_OK;
847 }
848
bson_append_element(bson * b,const char * name_or_null,const bson_iterator * elem)849 int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ) {
850 bson_iterator next = *elem;
851 int size;
852
853 bson_iterator_next( &next );
854 size = next.cur - elem->cur;
855
856 if ( name_or_null == NULL ) {
857 if( bson_ensure_space( b, size ) == BSON_ERROR )
858 return BSON_ERROR;
859 bson_append( b, elem->cur, size );
860 } else {
861 int data_size = size - 2 - strlen( bson_iterator_key( elem ) );
862 bson_append_estart( b, elem->cur[0], name_or_null, data_size );
863 bson_append( b, bson_iterator_value( elem ), data_size );
864 }
865
866 return BSON_OK;
867 }
868
bson_append_timestamp(bson * b,const char * name,bson_timestamp_t * ts)869 int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ) {
870 if ( bson_append_estart( b, BSON_TIMESTAMP, name, 8 ) == BSON_ERROR ) return BSON_ERROR;
871
872 bson_append32( b , &( ts->i ) );
873 bson_append32( b , &( ts->t ) );
874
875 return BSON_OK;
876 }
877
bson_append_date(bson * b,const char * name,bson_date_t millis)878 int bson_append_date( bson *b, const char *name, bson_date_t millis ) {
879 if ( bson_append_estart( b, BSON_DATE, name, 8 ) == BSON_ERROR ) return BSON_ERROR;
880 bson_append64( b , &millis );
881 return BSON_OK;
882 }
883
bson_append_time_t(bson * b,const char * name,time_t secs)884 int bson_append_time_t( bson *b, const char *name, time_t secs ) {
885 return bson_append_date( b, name, ( bson_date_t )secs * 1000 );
886 }
887
bson_append_start_object(bson * b,const char * name)888 int bson_append_start_object( bson *b, const char *name ) {
889 if ( bson_append_estart( b, BSON_OBJECT, name, 5 ) == BSON_ERROR ) return BSON_ERROR;
890 b->stack[ b->stackPos++ ] = b->cur - b->data;
891 bson_append32( b , &zero );
892 return BSON_OK;
893 }
894
bson_append_start_array(bson * b,const char * name)895 int bson_append_start_array( bson *b, const char *name ) {
896 if ( bson_append_estart( b, BSON_ARRAY, name, 5 ) == BSON_ERROR ) return BSON_ERROR;
897 b->stack[ b->stackPos++ ] = b->cur - b->data;
898 bson_append32( b , &zero );
899 return BSON_OK;
900 }
901
bson_append_finish_object(bson * b)902 int bson_append_finish_object( bson *b ) {
903 char *start;
904 int i;
905 if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
906 bson_append_byte( b , 0 );
907
908 start = b->data + b->stack[ --b->stackPos ];
909 i = b->cur - start;
910 bson_little_endian32( start, &i );
911
912 return BSON_OK;
913 }
914
bson_append_finish_array(bson * b)915 int bson_append_finish_array( bson *b ) {
916 return bson_append_finish_object( b );
917 }
918
919
920 /* Error handling and allocators. */
921
922 static bson_err_handler err_handler = NULL;
923
set_bson_err_handler(bson_err_handler func)924 bson_err_handler set_bson_err_handler( bson_err_handler func ) {
925 bson_err_handler old = err_handler;
926 err_handler = func;
927 return old;
928 }
929
bson_malloc(int size)930 void *bson_malloc( int size ) {
931 void *p;
932 p = bson_malloc_func( size );
933 bson_fatal_msg( !!p, "malloc() failed" );
934 return p;
935 }
936
bson_realloc(void * ptr,int size)937 void *bson_realloc( void *ptr, int size ) {
938 void *p;
939 p = bson_realloc_func( ptr, size );
940 bson_fatal_msg( !!p, "realloc() failed" );
941 return p;
942 }
943
_bson_errprintf(const char * format,...)944 int _bson_errprintf( const char *format, ... ) {
945 va_list ap;
946 int ret;
947 va_start( ap, format );
948 ret = vfprintf( stderr, format, ap );
949 va_end( ap );
950
951 return ret;
952 }
953
954 /**
955 * This method is invoked when a non-fatal bson error is encountered.
956 * Calls the error handler if available.
957 *
958 * @param
959 */
bson_builder_error(bson * b)960 void bson_builder_error( bson *b ) {
961 if( err_handler )
962 err_handler( "BSON error." );
963 }
964
bson_fatal(int ok)965 void bson_fatal( int ok ) {
966 bson_fatal_msg( ok, "" );
967 }
968
bson_fatal_msg(int ok,const char * msg)969 void bson_fatal_msg( int ok , const char *msg ) {
970 if ( ok )
971 return;
972
973 if ( err_handler ) {
974 err_handler( msg );
975 }
976
977 bson_errprintf( "error: %s\n" , msg );
978 exit( -5 );
979 }
980
981
982 /* Efficiently copy an integer to a string. */
983
bson_numstr(char * str,int i)984 void bson_numstr( char *str, int i ) {
985 if( i < 1000 )
986 memcpy( str, bson_numstrs[i], 4 );
987 else
988 bson_sprintf( str,"%d", i );
989 }
990
991 /* encoding.c */
992
993 /*
994 * Copyright 2009-2011 10gen, Inc.
995 *
996 * Licensed under the Apache License, Version 2.0 (the "License");
997 * you may not use this file except in compliance with the License.
998 * You may obtain a copy of the License at
999 *
1000 * http://www.apache.org/licenses/LICENSE-2.0
1001 *
1002 * Unless required by applicable law or agreed to in writing, software
1003 * distributed under the License is distributed on an "AS IS" BASIS,
1004 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1005 * See the License for the specific language governing permissions and
1006 * limitations under the License.
1007 */
1008
1009 /*
1010 * Portions Copyright 2001 Unicode, Inc.
1011 *
1012 * Disclaimer
1013 *
1014 * This source code is provided as is by Unicode, Inc. No claims are
1015 * made as to fitness for any particular purpose. No warranties of any
1016 * kind are expressed or implied. The recipient agrees to determine
1017 * applicability of information provided. If this file has been
1018 * purchased on magnetic or optical media from Unicode, Inc., the
1019 * sole remedy for any claim will be exchange of defective media
1020 * within 90 days of receipt.
1021 *
1022 * Limitations on Rights to Redistribute This Code
1023 *
1024 * Unicode, Inc. hereby grants the right to freely use the information
1025 * supplied in this file in the creation of products supporting the
1026 * Unicode Standard, and to make copies of this file in any form
1027 * for internal or external distribution as long as this notice
1028 * remains attached.
1029 */
1030
1031 /*
1032 * Index into the table below with the first byte of a UTF-8 sequence to
1033 * get the number of trailing bytes that are supposed to follow it.
1034 */
1035 static const char trailingBytesForUTF8[256] = {
1036 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1037 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1038 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1039 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1040 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1041 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1042 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1043 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
1044 };
1045
1046 /* --------------------------------------------------------------------- */
1047
1048 /*
1049 * Utility routine to tell whether a sequence of bytes is legal UTF-8.
1050 * This must be called with the length pre-determined by the first byte.
1051 * The length can be set by:
1052 * length = trailingBytesForUTF8[*source]+1;
1053 * and the sequence is illegal right away if there aren't that many bytes
1054 * available.
1055 * If presented with a length > 4, this returns 0. The Unicode
1056 * definition of UTF-8 goes up to 4-byte sequences.
1057 */
isLegalUTF8(const unsigned char * source,int length)1058 static int isLegalUTF8( const unsigned char *source, int length ) {
1059 unsigned char a;
1060 const unsigned char *srcptr = source + length;
1061 switch ( length ) {
1062 default:
1063 return 0;
1064 /* Everything else falls through when "true"... */
1065 case 4:
1066 if ( ( a = ( *--srcptr ) ) < 0x80 || a > 0xBF ) return 0;
1067 case 3:
1068 if ( ( a = ( *--srcptr ) ) < 0x80 || a > 0xBF ) return 0;
1069 case 2:
1070 if ( ( a = ( *--srcptr ) ) > 0xBF ) return 0;
1071 switch ( *source ) {
1072 /* no fall-through in this inner switch */
1073 case 0xE0:
1074 if ( a < 0xA0 ) return 0;
1075 break;
1076 case 0xF0:
1077 if ( a < 0x90 ) return 0;
1078 break;
1079 case 0xF4:
1080 if ( a > 0x8F ) return 0;
1081 break;
1082 default:
1083 if ( a < 0x80 ) return 0;
1084 }
1085 case 1:
1086 if ( *source >= 0x80 && *source < 0xC2 ) return 0;
1087 if ( *source > 0xF4 ) return 0;
1088 }
1089 return 1;
1090 }
1091
bson_validate_string(bson * b,const unsigned char * string,const int length,const char check_utf8,const char check_dot,const char check_dollar)1092 static int bson_validate_string( bson *b, const unsigned char *string,
1093 const int length, const char check_utf8, const char check_dot,
1094 const char check_dollar ) {
1095
1096 int position = 0;
1097 int sequence_length = 1;
1098
1099 if( check_dollar && string[0] == '$' ) {
1100 b->err |= BSON_FIELD_INIT_DOLLAR;
1101 }
1102
1103 while ( position < length ) {
1104 if ( check_dot && *( string + position ) == '.' ) {
1105 b->err |= BSON_FIELD_HAS_DOT;
1106 }
1107
1108 if ( check_utf8 ) {
1109 sequence_length = trailingBytesForUTF8[*( string + position )] + 1;
1110 if ( ( position + sequence_length ) > length ) {
1111 b->err |= BSON_NOT_UTF8;
1112 return BSON_ERROR;
1113 }
1114 if ( !isLegalUTF8( string + position, sequence_length ) ) {
1115 b->err |= BSON_NOT_UTF8;
1116 return BSON_ERROR;
1117 }
1118 }
1119 position += sequence_length;
1120 }
1121
1122 return BSON_OK;
1123 }
1124
1125
bson_check_string(bson * b,const char * string,const int length)1126 int bson_check_string( bson *b, const char *string,
1127 const int length ) {
1128
1129 return bson_validate_string( b, ( const unsigned char * )string, length, 1, 0, 0 );
1130 }
1131
bson_check_field_name(bson * b,const char * string,const int length)1132 int bson_check_field_name( bson *b, const char *string,
1133 const int length ) {
1134
1135 return bson_validate_string( b, ( const unsigned char * )string, length, 1, 1, 1 );
1136 }
1137