SequoiaDB
 All Classes Files Functions Variables Enumerations Enumerator
bson.h
Go to the documentation of this file.
1 
5 /* Copyright 2009-2012 10gen Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef BSON_H_
21 #define BSON_H_
22 #include <time.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 
28 #include "common_decimal.h"
29 
30 #if defined(__GNUC__) || defined(__xlC__)
31  #define SDB_EXPORT
32 #else
33  #ifdef SDB_STATIC_BUILD
34  #define SDB_EXPORT
35  #elif defined(SDB_DLL_BUILD)
36  #define SDB_EXPORT __declspec(dllexport)
37  #else
38  #define SDB_EXPORT __declspec(dllimport)
39  #endif
40 #endif
41 
42 #ifdef __cplusplus
43 #define SDB_EXTERN_C_START extern "C" {
44 #define SDB_EXTERN_C_END }
45 #else
46 #define SDB_EXTERN_C_START
47 #define SDB_EXTERN_C_END
48 #endif
49 
50 //#if defined(SDB_HAVE_STDINT) || __STDC_VERSION__ >= 199901L
51 #include <stdint.h>
52 //#elif defined(SDB_HAVE_UNISTD)
53 //#include <unistd.h>
54 //#elif defined(SDB_USE__INT64)
55 //typedef __int64 int64_t;
56 //typedef unsigned __int64 uint64_t;
57 //#elif defined(SDB_USE_LONG_LONG_INT)
58 //typedef long long int int64_t;
59 //typedef unsigned long long int uint64_t;
60 //#else
61 //#error Must compile with c99 or define SDB_HAVE_STDINT, SDB_HAVE_UNISTD, SDB_USE__INT64, or SDB_USE_LONG_LONG_INT.
62 //#endif
63 
64 //#ifdef SDB_BIG_ENDIAN
65 //#define bson_little_endian64(out, in) ( bson_swap_endian64(out, in) )
66 //#define bson_little_endian32(out, in) ( bson_swap_endian32(out, in) )
67 //#define bson_big_endian64(out, in) ( memcpy(out, in, 8) )
68 //#define bson_big_endian32(out, in) ( memcpy(out, in, 4) )
69 //#else
70 #define bson_little_endian64(out, in) ( memcpy(out, in, 8) )
71 #define bson_little_endian32(out, in) ( memcpy(out, in, 4) )
72 #define bson_little_endian16(out, in) ( memcpy(out, in, 2) )
73 #define bson_big_endian64(out, in) ( bson_swap_endian64(out, in) )
74 #define bson_big_endian32(out, in) ( bson_swap_endian32(out, in) )
75 #define bson_big_endian16(out, in) ( bson_swap_endian16(out, in) )
76 //#endif
77 
78 SDB_EXTERN_C_START
79 
80 #define BSON_OK 0
81 #define BSON_ERROR -1
82 
85 };
86 
87 
89  BSON_VALID = 0,
90  BSON_NOT_UTF8 = ( 1<<1 ),
91  BSON_FIELD_HAS_DOT = ( 1<<2 ),
94 };
95 
103 };
104 
105 typedef enum {
106  BSON_MINKEY = -1,
107  BSON_EOO = 0,
114  BSON_OID = 7,
115  BSON_BOOL = 8,
116  BSON_DATE = 9,
117  BSON_NULL = 10,
118  BSON_REGEX = 11,
119  BSON_DBREF = 12,
120  BSON_CODE = 13,
121  BSON_SYMBOL = 14,
123  BSON_INT = 16,
125  BSON_LONG = 18,
127  BSON_DECIMAL = 100,
128  BSON_MAXKEY = 127
129 } bson_type;
130 
131 typedef int bson_bool_t;
132 
133 typedef struct {
134  const char *cur;
135  bson_bool_t first;
136 } bson_iterator;
137 
138 #define BSON_MAX_STACK_SIZE 32
139 typedef struct {
140  char *data;
141  char *cur;
142  int dataSize;
143  bson_bool_t finished;
144  int stack[BSON_MAX_STACK_SIZE];
145  char stackType[BSON_MAX_STACK_SIZE];
146  int stackPos;
147  int err;
148  char *errstr;
149  int ownmem;
150 } bson;
151 
152 #pragma pack(1)
153 typedef union {
154  char bytes[12];
155  int ints[3];
156 } bson_oid_t;
157 #pragma pack()
158 
159 typedef int64_t bson_date_t; /* milliseconds since epoch UTC */
160 
161 typedef struct {
162  int i;
163  int t;
165 
166 /* ----------------------------
167  READING
168  ------------------------------ */
169 
175 SDB_EXPORT bson* bson_create( void );
176 
182 SDB_EXPORT void bson_dispose(bson* b);
183 
191 SDB_EXPORT int bson_size( const bson *b );
192 
200 SDB_EXPORT int bson_buffer_size( const bson *b );
201 
207 SDB_EXPORT void bson_print( const bson *b );
208 
215 SDB_EXPORT int bson_sprint_iterator ( char **pbuf, int *left, bson_iterator *i,
216  char delChar ) ;
217 
223 SDB_EXPORT int bson_sprint( char *buffer, int bufsize, const bson *b );
224 
231 SDB_EXPORT int bson_sprint_length_iterator ( bson_iterator *i ) ;
232 
241 SDB_EXPORT int bson_sprint_length( const bson *b );
242 
248 SDB_EXPORT const char *bson_data( const bson *b );
249 
256 //SDB_EXPORT void bson_print_raw( const char *bson , int depth );
257 
266 SDB_EXPORT int bson_sprint_raw ( char **pbuf, int *left, const char *data, int isobj );
267 
277 SDB_EXPORT int bson_sprint_length_raw( const char *data, int isobj );
278 
288 SDB_EXPORT bson_type bson_find( bson_iterator *it, const bson *obj, const char *name );
289 
295 SDB_EXPORT bson_iterator* bson_iterator_create( void );
296 
300 SDB_EXPORT void bson_iterator_dispose(bson_iterator*);
307 SDB_EXPORT void bson_iterator_init( bson_iterator *i , const bson *b );
308 
316 SDB_EXPORT void bson_iterator_from_buffer( bson_iterator *i, const char *buffer );
317 
318 /* more returns true for eoo. best to loop with bson_iterator_next(&it) */
326 SDB_EXPORT bson_bool_t bson_iterator_more( const bson_iterator *i );
327 
336 
344 SDB_EXPORT bson_type bson_iterator_type( const bson_iterator *i );
345 
353 SDB_EXPORT const char *bson_iterator_key( const bson_iterator *i );
354 
362 SDB_EXPORT const char *bson_iterator_value( const bson_iterator *i );
363 
364 /* these convert to the right type (return 0 if non-numeric) */
373 SDB_EXPORT double bson_iterator_double( const bson_iterator *i );
374 
382 SDB_EXPORT int bson_iterator_int( const bson_iterator *i );
383 
391 SDB_EXPORT int64_t bson_iterator_long( const bson_iterator *i );
392 
400 SDB_EXPORT int bson_iterator_decimal_scale( const bson_iterator *i,
401  int *sign, int *scale ) ;
402 
410 SDB_EXPORT int bson_iterator_decimal_typemod( const bson_iterator *i,
411  int *typemod ) ;
412 
420 SDB_EXPORT int bson_iterator_decimal_weight( const bson_iterator *i,
421  int *weight ) ;
422 
430 SDB_EXPORT int bson_iterator_decimal_size( const bson_iterator *i,
431  int *size ) ;
432 
440 SDB_EXPORT int bson_iterator_decimal( const bson_iterator *i,
441  bson_decimal *decimal ) ;
442 
443 
444 /* return the bson timestamp as a whole or in parts */
454 SDB_EXPORT int bson_iterator_timestamp_time( const bson_iterator *i );
455 SDB_EXPORT int bson_iterator_timestamp_increment( const bson_iterator *i );
456 
465 /* false: boolean false, 0 in any type, or null */
466 /* true: anything else (even empty strings and objects) */
467 SDB_EXPORT bson_bool_t bson_iterator_bool( const bson_iterator *i );
468 
477 /* these assume you are using the right type */
478 double bson_iterator_double_raw( const bson_iterator *i );
479 
488 int bson_iterator_int_raw( const bson_iterator *i );
489 
498 int64_t bson_iterator_long_raw( const bson_iterator *i );
499 
508 bson_bool_t bson_iterator_bool_raw( const bson_iterator *i );
509 
518 SDB_EXPORT bson_oid_t *bson_iterator_oid( const bson_iterator *i );
519 
528 /* these can also be used with bson_code and bson_symbol*/
529 SDB_EXPORT const char *bson_iterator_string( const bson_iterator *i );
530 
540 
550 /* works with bson_code, bson_codewscope, and BSON_STRING */
551 /* returns NULL for everything else */
552 SDB_EXPORT const char *bson_iterator_code( const bson_iterator *i );
553 
560 /* calls bson_empty on scope if not a bson_codewscope */
561 SDB_EXPORT void bson_iterator_code_scope( const bson_iterator *i, bson *scope );
562 
571 /* both of these only work with bson_date */
572 SDB_EXPORT bson_date_t bson_iterator_date( const bson_iterator *i );
573 
582 SDB_EXPORT time_t bson_iterator_time_t( const bson_iterator *i );
583 
592 SDB_EXPORT int bson_iterator_bin_len( const bson_iterator *i );
593 
602 SDB_EXPORT char bson_iterator_bin_type( const bson_iterator *i );
603 
612 SDB_EXPORT const char *bson_iterator_bin_data( const bson_iterator *i );
613 
622 SDB_EXPORT const char *bson_iterator_regex( const bson_iterator *i );
623 
632 SDB_EXPORT const char *bson_iterator_regex_opts( const bson_iterator *i );
633 
642 SDB_EXPORT const char *bson_iterator_dbref( const bson_iterator *i );
643 
652 SDB_EXPORT bson_oid_t *bson_iterator_dbref_oid( const bson_iterator *i );
653 
654 /* these work with BSON_OBJECT and BSON_ARRAY */
662 SDB_EXPORT void bson_iterator_subobject( const bson_iterator *i, bson *sub );
663 
670 SDB_EXPORT void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub );
671 
672 /* str must be at least 24 hex chars + null byte */
679 SDB_EXPORT void bson_oid_from_string( bson_oid_t *oid, const char *str );
680 
687 SDB_EXPORT void bson_oid_to_string( const bson_oid_t *oid, char *str );
688 
694 SDB_EXPORT void bson_oid_gen( bson_oid_t *oid );
695 
702 SDB_EXPORT void bson_set_oid_fuzz( int ( *func )( void ) );
703 
711 SDB_EXPORT void bson_set_oid_inc( int ( *func )( void ) );
712 
718 SDB_EXPORT time_t bson_oid_generated_time( bson_oid_t *oid ); /* Gives the time the OID was created */
719 
720 /* ----------------------------
721  BUILDING
722  ------------------------------ */
723 
732 SDB_EXPORT void bson_init( bson *b );
733 
743 int bson_init_data( bson *b , const char *data );
744 
755 int bson_init_finished_data( bson *b, const char *data ) ;
756 
766 SDB_EXPORT void bson_init_size( bson *b, int size );
767 
777 int bson_ensure_space( bson *b, const int bytesNeeded );
778 
787 SDB_EXPORT int bson_finish( bson *b );
788 
795 SDB_EXPORT void bson_destroy( bson *b );
796 
804 /* returns pointer to static empty bson object */
805 SDB_EXPORT bson *bson_empty( bson *obj );
806 
814 SDB_EXPORT bson_bool_t bson_is_empty( bson *obj );
815 
824 SDB_EXPORT int bson_copy( bson *out, const bson *in ); /* puts data in new buffer. NOOP if out==NULL */
825 
831 SDB_EXPORT int bson_init_by_reset( bson *obj );
832 
842 SDB_EXPORT int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid );
843 
852 SDB_EXPORT int bson_append_new_oid( bson *b, const char *name );
853 
863 SDB_EXPORT int bson_append_int( bson *b, const char *name, const int i );
864 
874 SDB_EXPORT int bson_append_long( bson *b, const char *name, const int64_t i );
875 
885 SDB_EXPORT int bson_append_decimal( bson *b, const char *name,
886  const bson_decimal *decimal ) ;
887 
899 SDB_EXPORT int bson_append_decimal2( bson *b, const char *name,
900  const char *value, int precision,
901  int scale ) ;
911 SDB_EXPORT int bson_append_decimal3( bson *b, const char *name,
912  const char *value ) ;
913 
923 SDB_EXPORT int bson_append_double( bson *b, const char *name, const double d );
924 
934 SDB_EXPORT int bson_append_string( bson *b, const char *name, const char *str );
935 
946 SDB_EXPORT int bson_append_string_n( bson *b, const char *name, const char *str, int len );
947 
957 SDB_EXPORT int bson_append_symbol( bson *b, const char *name, const char *str );
958 
969 SDB_EXPORT int bson_append_symbol_n( bson *b, const char *name, const char *str, int len );
970 
981 SDB_EXPORT int bson_append_code( bson *b, const char *name, const char *str );
982 
993 SDB_EXPORT int bson_append_code_n( bson *b, const char *name, const char *str, int len );
994 
1005 SDB_EXPORT int bson_append_code_w_scope( bson *b, const char *name, const char *code, const bson *scope );
1006 
1018 SDB_EXPORT int bson_append_code_w_scope_n( bson *b, const char *name, const char *code, int size, const bson *scope );
1019 
1031 SDB_EXPORT int bson_append_binary( bson *b, const char *name, char type, const char *str, int len );
1032 
1042 SDB_EXPORT int bson_append_bool( bson *b, const char *name, const bson_bool_t v );
1043 
1052 SDB_EXPORT int bson_append_null( bson *b, const char *name );
1053 
1062 SDB_EXPORT int bson_append_undefined( bson *b, const char *name );
1063 
1074 SDB_EXPORT int bson_append_regex( bson *b, const char *name, const char *pattern, const char *opts );
1075 
1084 SDB_EXPORT int bson_append_minkey( bson *b, const char *name ) ;
1085 
1094 SDB_EXPORT int bson_append_maxkey( bson *b, const char *name ) ;
1095 
1105 SDB_EXPORT int bson_append_bson( bson *b, const char *name, const bson *bson );
1106 
1116 SDB_EXPORT int bson_append_array( bson *b, const char *name, const bson *bson );
1117 
1127 SDB_EXPORT int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem );
1128 
1137 SDB_EXPORT int bson_append_elements( bson *dst, const bson *src );
1138 
1148 SDB_EXPORT int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts );
1149 
1160 SDB_EXPORT int bson_append_timestamp2( bson *b, const char *name, int time, int increment );
1161 
1162 /* these both append a bson_date */
1172 SDB_EXPORT int bson_append_date( bson *b, const char *name, bson_date_t millis );
1173 
1183 SDB_EXPORT int bson_append_time_t( bson *b, const char *name, time_t secs );
1184 
1193 SDB_EXPORT int bson_append_start_object( bson *b, const char *name );
1194 
1203 SDB_EXPORT int bson_append_start_array( bson *b, const char *name );
1204 
1212 SDB_EXPORT int bson_append_finish_object( bson *b );
1213 
1222 SDB_EXPORT int bson_append_finish_array( bson *b );
1223 
1224 void bson_numstr( char *str, int i );
1225 
1226 void bson_incnumstr( char *str );
1227 
1228 /* Error handling and standard library function over-riding. */
1229 /* -------------------------------------------------------- */
1230 
1231 /* bson_err_handlers shouldn't return!!! */
1232 typedef void( *bson_err_handler )( const char *errmsg );
1233 
1234 typedef int (*bson_printf_func)( const char *, ... );
1235 typedef int (*bson_fprintf_func)( FILE *, const char *, ... );
1236 typedef int (*bson_sprintf_func)( char *, const char *, ... );
1237 
1238 typedef void* (*bson_malloc_func_p)( size_t ) ;
1239 typedef void* (*bson_realloc_func_p)( void*, size_t ) ;
1240 typedef void (*bson_free_func_p)( void* ) ;
1241 
1242 void bson_set_malloc_func( bson_malloc_func_p func ) ;
1243 void bson_set_realloc_func( bson_realloc_func_p func ) ;
1244 void bson_set_free_func( bson_free_func_p func ) ;
1245 
1246 extern bson_printf_func bson_printf;
1247 extern bson_fprintf_func bson_fprintf;
1248 extern bson_sprintf_func bson_sprintf;
1249 extern bson_printf_func bson_errprintf;
1250 
1251 SDB_EXPORT void bson_free( void *ptr );
1252 
1262 SDB_EXPORT void *bson_malloc( int size );
1263 
1275 void *bson_realloc( void *ptr, int size );
1276 
1284 SDB_EXPORT bson_err_handler set_bson_err_handler( bson_err_handler func );
1285 
1286 /* does nothing if ok != 0 */
1292 void bson_fatal( int ok );
1293 
1300 void bson_fatal_msg( int ok, const char *msg );
1301 
1307 void bson_builder_error( bson *b );
1308 
1314 SDB_EXPORT double bson_int64_to_double( int64_t i64 );
1315 
1316 SDB_EXPORT void bson_swap_endian32( void *outp, const void *inp );
1317 SDB_EXPORT void bson_swap_endian64( void *outp, const void *inp );
1318 
1319 SDB_EXPORT bson_bool_t bson_is_inf( double d, int *pSign ) ;
1320 
1327 SDB_EXPORT void bson_set_js_compatibility(int compatible);
1328 
1333 SDB_EXPORT int bson_get_js_compatibility();
1334 
1335 SDB_EXTERN_C_END
1336 #endif