rpm  5.4.15
bson.c
Go to the documentation of this file.
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 "system.h"
19 #include <stdarg.h>
20 
21 #include "bson.h"
22 
23 #include "debug.h"
24 
25 /* big endian is only used for OID generation. little is used everywhere else */
26 
27 /*@-redef@*/
28 union _dbswap {
29  uint32_t ui;
30  uint8_t uc[4];
31 };
32 /*@=redef@*/
33 
34 void bson_little_endian64(void* outp, const void* inp)
35 {
36  union _dbswap _endian;
37  _endian.ui = 0x11223344; /* XXX static init? */
38  if (_endian.uc[0] == 0x44)
39  memcpy(outp, inp, 8);
40  else {
41  const char *in = (const char *)inp;
42  char *out = (char *)outp;
43  out[0] = in[7];
44  out[1] = in[6];
45  out[2] = in[5];
46  out[3] = in[4];
47  out[4] = in[3];
48  out[5] = in[2];
49  out[6] = in[1];
50  out[7] = in[0];
51  }
52 
53 }
54 
55 void bson_little_endian32(void* outp, const void* inp)
56 {
57  union _dbswap _endian;
58  _endian.ui = 0x11223344; /* XXX static init? */
59  if (_endian.uc[0] == 0x44)
60  memcpy(outp, inp, 4);
61  else {
62  const char *in = (const char *)inp;
63  char *out = (char *)outp;
64  out[0] = in[3];
65  out[1] = in[2];
66  out[2] = in[1];
67  out[3] = in[0];
68  }
69 }
70 
71 void bson_big_endian64(void* outp, const void* inp)
72 {
73  union _dbswap _endian;
74  _endian.ui = 0x11223344; /* XXX static init? */
75  if (_endian.uc[0] == 0x11)
76  memcpy(outp, inp, 8);
77  else {
78  const char *in = (const char *)inp;
79  char *out = (char *)outp;
80  out[0] = in[7];
81  out[1] = in[6];
82  out[2] = in[5];
83  out[3] = in[4];
84  out[4] = in[3];
85  out[5] = in[2];
86  out[6] = in[1];
87  out[7] = in[0];
88  }
89 
90 }
91 
92 void bson_big_endian32(void* outp, const void* inp)
93 {
94  union _dbswap _endian;
95  _endian.ui = 0x11223344; /* XXX static init? */
96  if (_endian.uc[0] == 0x11)
97  memcpy(outp, inp, 4);
98  else {
99  const char *in = (const char *)inp;
100  char *out = (char *)outp;
101  out[0] = in[3];
102  out[1] = in[2];
103  out[2] = in[1];
104  out[3] = in[0];
105  }
106 }
107 
108 static const int initialBufferSize = 128;
109 
110 /* only need one of these */
111 static const int zero = 0;
112 
113 /*==============================================================*/
114 /* --- encoding.c */
115 
116 /*
117  * Index into the table below with the first byte of a UTF-8 sequence to
118  * get the number of trailing bytes that are supposed to follow it.
119  */
120 static const char trailingBytesForUTF8[256] = {
121  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,
122  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,
123  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,
124  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,
125  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,
126  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,
127  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,
128  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
129 };
130 
131 /* --------------------------------------------------------------------- */
132 
133 /*
134  * Utility routine to tell whether a sequence of bytes is legal UTF-8.
135  * This must be called with the length pre-determined by the first byte.
136  * The length can be set by:
137  * length = trailingBytesForUTF8[*source]+1;
138  * and the sequence is illegal right away if there aren't that many bytes
139  * available.
140  * If presented with a length > 4, this returns 0. The Unicode
141  * definition of UTF-8 goes up to 4-byte sequences.
142  */
143 static int isLegalUTF8( const unsigned char *source, int length ) {
144  unsigned char a;
145  const unsigned char *srcptr = source + length;
146  switch ( length ) {
147  default:
148  return 0;
149  /* Everything else falls through when "true"... */
150  case 4:
151  if ( ( a = ( *--srcptr ) ) < 0x80 || a > 0xBF ) return 0;
152  case 3:
153  if ( ( a = ( *--srcptr ) ) < 0x80 || a > 0xBF ) return 0;
154  case 2:
155  if ( ( a = ( *--srcptr ) ) > 0xBF ) return 0;
156  switch ( *source ) {
157  /* no fall-through in this inner switch */
158  case 0xE0:
159  if ( a < 0xA0 ) return 0;
160  break;
161  case 0xF0:
162  if ( a < 0x90 ) return 0;
163  break;
164  case 0xF4:
165  if ( a > 0x8F ) return 0;
166  break;
167  default:
168  if ( a < 0x80 ) return 0;
169  }
170  case 1:
171  if ( *source >= 0x80 && *source < 0xC2 ) return 0;
172  if ( *source > 0xF4 ) return 0;
173  }
174  return 1;
175 }
176 
177 /* If the name is part of a db ref ($ref, $db, or $id), then return true. */
178 static int bson_string_is_db_ref( const unsigned char *string, const size_t length ) {
179  int result = 0;
180 
181  if( length >= 4 ) {
182  if( string[1] == 'r' && string[2] == 'e' && string[3] == 'f' )
183  result = 1;
184  }
185  else if( length >= 3 ) {
186  if( string[1] == 'i' && string[2] == 'd' )
187  result = 1;
188  else if( string[1] == 'd' && string[2] == 'b' )
189  result = 1;
190  }
191 
192  return result;
193 }
194 
195 static int bson_validate_string( bson *b, const unsigned char *string,
196  const size_t length, const char check_utf8, const char check_dot,
197  const char check_dollar ) {
198 
199  size_t position = 0;
200  int sequence_length = 1;
201 
202  if( check_dollar && string[0] == '$' ) {
203  if( !bson_string_is_db_ref( string, length ) )
205  }
206 
207  while ( position < length ) {
208  if ( check_dot && *( string + position ) == '.' ) {
209  b->err |= BSON_FIELD_HAS_DOT;
210  }
211 
212  if ( check_utf8 ) {
213  sequence_length = trailingBytesForUTF8[*( string + position )] + 1;
214  if ( ( position + sequence_length ) > length ) {
215  b->err |= BSON_NOT_UTF8;
216  return BSON_ERROR;
217  }
218  if ( !isLegalUTF8( string + position, sequence_length ) ) {
219  b->err |= BSON_NOT_UTF8;
220  return BSON_ERROR;
221  }
222  }
223  position += sequence_length;
224  }
225 
226  return BSON_OK;
227 }
228 
229 
230 static int bson_check_string( bson *b, const char *string,
231  const size_t length ) {
232 
233  return bson_validate_string( b, ( const unsigned char * )string, length, 1, 0, 0 );
234 }
235 
236 static int bson_check_field_name( bson *b, const char *string,
237  const size_t length ) {
238 
239  return bson_validate_string( b, ( const unsigned char * )string, length, 1, 1, 1 );
240 }
241 
242 /*==============================================================*/
243 /* --- numbers.c */
244 
245 /* all the numbers that fit in a 4 byte string */
246 const char bson_numstrs[1000][4] = {
247  "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
248  "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
249  "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
250  "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
251  "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
252  "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
253  "60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
254  "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
255  "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
256  "90", "91", "92", "93", "94", "95", "96", "97", "98", "99",
257 
258  "100", "101", "102", "103", "104", "105", "106", "107", "108", "109",
259  "110", "111", "112", "113", "114", "115", "116", "117", "118", "119",
260  "120", "121", "122", "123", "124", "125", "126", "127", "128", "129",
261  "130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
262  "140", "141", "142", "143", "144", "145", "146", "147", "148", "149",
263  "150", "151", "152", "153", "154", "155", "156", "157", "158", "159",
264  "160", "161", "162", "163", "164", "165", "166", "167", "168", "169",
265  "170", "171", "172", "173", "174", "175", "176", "177", "178", "179",
266  "180", "181", "182", "183", "184", "185", "186", "187", "188", "189",
267  "190", "191", "192", "193", "194", "195", "196", "197", "198", "199",
268 
269  "200", "201", "202", "203", "204", "205", "206", "207", "208", "209",
270  "210", "211", "212", "213", "214", "215", "216", "217", "218", "219",
271  "220", "221", "222", "223", "224", "225", "226", "227", "228", "229",
272  "230", "231", "232", "233", "234", "235", "236", "237", "238", "239",
273  "240", "241", "242", "243", "244", "245", "246", "247", "248", "249",
274  "250", "251", "252", "253", "254", "255", "256", "257", "258", "259",
275  "260", "261", "262", "263", "264", "265", "266", "267", "268", "269",
276  "270", "271", "272", "273", "274", "275", "276", "277", "278", "279",
277  "280", "281", "282", "283", "284", "285", "286", "287", "288", "289",
278  "290", "291", "292", "293", "294", "295", "296", "297", "298", "299",
279 
280  "300", "301", "302", "303", "304", "305", "306", "307", "308", "309",
281  "310", "311", "312", "313", "314", "315", "316", "317", "318", "319",
282  "320", "321", "322", "323", "324", "325", "326", "327", "328", "329",
283  "330", "331", "332", "333", "334", "335", "336", "337", "338", "339",
284  "340", "341", "342", "343", "344", "345", "346", "347", "348", "349",
285  "350", "351", "352", "353", "354", "355", "356", "357", "358", "359",
286  "360", "361", "362", "363", "364", "365", "366", "367", "368", "369",
287  "370", "371", "372", "373", "374", "375", "376", "377", "378", "379",
288  "380", "381", "382", "383", "384", "385", "386", "387", "388", "389",
289  "390", "391", "392", "393", "394", "395", "396", "397", "398", "399",
290 
291  "400", "401", "402", "403", "404", "405", "406", "407", "408", "409",
292  "410", "411", "412", "413", "414", "415", "416", "417", "418", "419",
293  "420", "421", "422", "423", "424", "425", "426", "427", "428", "429",
294  "430", "431", "432", "433", "434", "435", "436", "437", "438", "439",
295  "440", "441", "442", "443", "444", "445", "446", "447", "448", "449",
296  "450", "451", "452", "453", "454", "455", "456", "457", "458", "459",
297  "460", "461", "462", "463", "464", "465", "466", "467", "468", "469",
298  "470", "471", "472", "473", "474", "475", "476", "477", "478", "479",
299  "480", "481", "482", "483", "484", "485", "486", "487", "488", "489",
300  "490", "491", "492", "493", "494", "495", "496", "497", "498", "499",
301 
302  "500", "501", "502", "503", "504", "505", "506", "507", "508", "509",
303  "510", "511", "512", "513", "514", "515", "516", "517", "518", "519",
304  "520", "521", "522", "523", "524", "525", "526", "527", "528", "529",
305  "530", "531", "532", "533", "534", "535", "536", "537", "538", "539",
306  "540", "541", "542", "543", "544", "545", "546", "547", "548", "549",
307  "550", "551", "552", "553", "554", "555", "556", "557", "558", "559",
308  "560", "561", "562", "563", "564", "565", "566", "567", "568", "569",
309  "570", "571", "572", "573", "574", "575", "576", "577", "578", "579",
310  "580", "581", "582", "583", "584", "585", "586", "587", "588", "589",
311  "590", "591", "592", "593", "594", "595", "596", "597", "598", "599",
312 
313  "600", "601", "602", "603", "604", "605", "606", "607", "608", "609",
314  "610", "611", "612", "613", "614", "615", "616", "617", "618", "619",
315  "620", "621", "622", "623", "624", "625", "626", "627", "628", "629",
316  "630", "631", "632", "633", "634", "635", "636", "637", "638", "639",
317  "640", "641", "642", "643", "644", "645", "646", "647", "648", "649",
318  "650", "651", "652", "653", "654", "655", "656", "657", "658", "659",
319  "660", "661", "662", "663", "664", "665", "666", "667", "668", "669",
320  "670", "671", "672", "673", "674", "675", "676", "677", "678", "679",
321  "680", "681", "682", "683", "684", "685", "686", "687", "688", "689",
322  "690", "691", "692", "693", "694", "695", "696", "697", "698", "699",
323 
324  "700", "701", "702", "703", "704", "705", "706", "707", "708", "709",
325  "710", "711", "712", "713", "714", "715", "716", "717", "718", "719",
326  "720", "721", "722", "723", "724", "725", "726", "727", "728", "729",
327  "730", "731", "732", "733", "734", "735", "736", "737", "738", "739",
328  "740", "741", "742", "743", "744", "745", "746", "747", "748", "749",
329  "750", "751", "752", "753", "754", "755", "756", "757", "758", "759",
330  "760", "761", "762", "763", "764", "765", "766", "767", "768", "769",
331  "770", "771", "772", "773", "774", "775", "776", "777", "778", "779",
332  "780", "781", "782", "783", "784", "785", "786", "787", "788", "789",
333  "790", "791", "792", "793", "794", "795", "796", "797", "798", "799",
334 
335  "800", "801", "802", "803", "804", "805", "806", "807", "808", "809",
336  "810", "811", "812", "813", "814", "815", "816", "817", "818", "819",
337  "820", "821", "822", "823", "824", "825", "826", "827", "828", "829",
338  "830", "831", "832", "833", "834", "835", "836", "837", "838", "839",
339  "840", "841", "842", "843", "844", "845", "846", "847", "848", "849",
340  "850", "851", "852", "853", "854", "855", "856", "857", "858", "859",
341  "860", "861", "862", "863", "864", "865", "866", "867", "868", "869",
342  "870", "871", "872", "873", "874", "875", "876", "877", "878", "879",
343  "880", "881", "882", "883", "884", "885", "886", "887", "888", "889",
344  "890", "891", "892", "893", "894", "895", "896", "897", "898", "899",
345 
346  "900", "901", "902", "903", "904", "905", "906", "907", "908", "909",
347  "910", "911", "912", "913", "914", "915", "916", "917", "918", "919",
348  "920", "921", "922", "923", "924", "925", "926", "927", "928", "929",
349  "930", "931", "932", "933", "934", "935", "936", "937", "938", "939",
350  "940", "941", "942", "943", "944", "945", "946", "947", "948", "949",
351  "950", "951", "952", "953", "954", "955", "956", "957", "958", "959",
352  "960", "961", "962", "963", "964", "965", "966", "967", "968", "969",
353  "970", "971", "972", "973", "974", "975", "976", "977", "978", "979",
354  "980", "981", "982", "983", "984", "985", "986", "987", "988", "989",
355  "990", "991", "992", "993", "994", "995", "996", "997", "998", "999",
356 };
357 
358 /*==============================================================*/
359 
360 /* Static data to use with bson_init_empty( ) and bson_shared_empty( ) */
361 static char bson_shared_empty_data[] = {5,0,0,0,0};
362 
363 /* Custom standard function pointers. */
364 void *( *bson_malloc_func )( size_t ) = malloc;
365 void *( *bson_realloc_func )( void *, size_t ) = realloc;
366 void ( *bson_free_func )( void * ) = free;
367 #ifdef R_SAFETY_NET
369 #else
371 #endif
374 
375 static int _bson_errprintf( const char *, ... );
377 
378 static void _bson_zero( bson *b );
379 static size_t _bson_position( const bson *b );
380 
381 /* ObjectId fuzz functions. */
382 static int ( *oid_fuzz_func )( void ) = NULL;
383 static int ( *oid_inc_func )( void ) = NULL;
384 
385 /* ----------------------------
386  READING
387  ------------------------------ */
388 
389 MONGO_EXPORT void bson_init_zero(bson* b) {
390  memset(b, 0, sizeof(bson) - sizeof(b->stack));
391 }
392 
393 MONGO_EXPORT bson* bson_alloc( void ) {
394  return ( bson* )bson_malloc( sizeof( bson ) );
395 }
396 
397 MONGO_EXPORT void bson_dealloc( bson* b ) {
398  bson_free( b );
399 }
400 
401 /* When passed a char * of a BSON data block, returns its reported size */
402 static int bson_finished_data_size( const char *data ) {
403  int i;
404  bson_little_endian32( &i, data );
405  return i;
406 }
407 
408 int bson_init_finished_data( bson *b, char *data, bson_bool_t ownsData ) {
409  _bson_zero( b );
410  b->data = data;
411  b->dataSize = bson_finished_data_size( data );
412  b->ownsData = ownsData;
413  b->finished = 1;
414  return BSON_OK;
415 }
416 
418  int dataSize = bson_finished_data_size( data );
419  if ( bson_init_size( b, dataSize ) == BSON_ERROR ) return BSON_ERROR;
420  memcpy( b->data, data, dataSize );
421  b->finished = 1;
422  return BSON_OK;
423 }
424 
425 MONGO_EXPORT bson_bool_t bson_init_empty( bson *obj ) {
427  return BSON_OK;
428 }
429 
430 MONGO_EXPORT const bson *bson_shared_empty( void ) {
431  static const bson shared_empty = { bson_shared_empty_data, bson_shared_empty_data, 128, 1, 0 };
432  return &shared_empty;
433 }
434 
435 MONGO_EXPORT int bson_copy( bson *out, const bson *in ) {
436  if ( !out || !in ) return BSON_ERROR;
437  if ( !in->finished ) return BSON_ERROR;
438  return bson_init_finished_data_with_copy( out, in->data );
439 }
440 
441 MONGO_EXPORT int bson_size( const bson *b ) {
442  int i;
443  if ( ! b || ! b->data )
444  return 0;
445  bson_little_endian32( &i, b->data );
446  return i;
447 }
448 
449 static size_t _bson_position( const bson *b ) {
450  return b->cur - b->data;
451 }
452 
453 MONGO_EXPORT size_t bson_buffer_size( const bson *b ) {
454  return _bson_position(b) + 1;
455 }
456 
457 
458 MONGO_EXPORT const char *bson_data( const bson *b ) {
459  return (const char *)b->data;
460 }
461 
462 static char hexbyte( char hex ) {
463  if (hex >= '0' && hex <= '9')
464  return (hex - '0');
465  else if (hex >= 'A' && hex <= 'F')
466  return (hex - 'A' + 10);
467  else if (hex >= 'a' && hex <= 'f')
468  return (hex - 'a' + 10);
469  else
470  return 0x0;
471 }
472 
473 MONGO_EXPORT void bson_oid_from_string( bson_oid_t *oid, const char *str ) {
474  int i;
475  for ( i=0; i<12; i++ ) {
476  oid->bytes[i] = ( hexbyte( str[2*i] ) << 4 ) | hexbyte( str[2*i + 1] );
477  }
478 }
479 
480 MONGO_EXPORT void bson_oid_to_string( const bson_oid_t *oid, char *str ) {
481  static const char hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
482  int i;
483  for ( i=0; i<12; i++ ) {
484  str[2*i] = hex[( oid->bytes[i] & 0xf0 ) >> 4];
485  str[2*i + 1] = hex[ oid->bytes[i] & 0x0f ];
486  }
487  str[24] = '\0';
488 }
489 
490 MONGO_EXPORT void bson_set_oid_fuzz( int ( *func )( void ) ) {
491  oid_fuzz_func = func;
492 }
493 
494 MONGO_EXPORT void bson_set_oid_inc( int ( *func )( void ) ) {
495  oid_inc_func = func;
496 }
497 
498 MONGO_EXPORT void bson_oid_gen( bson_oid_t *oid ) {
499  static int incr = 0;
500  static int fuzz = 0;
501  int i;
502  time_t t = time( NULL );
503 
504  if( oid_inc_func )
505  i = oid_inc_func();
506  else
507  i = incr++;
508 
509  if ( !fuzz ) {
510  if ( oid_fuzz_func )
511  fuzz = oid_fuzz_func();
512  else {
513  srand( ( int )t );
514  fuzz = rand();
515  }
516  }
517 
518  bson_big_endian32( &oid->ints[0], &t );
519  oid->ints[1] = fuzz;
520  bson_big_endian32( &oid->ints[2], &i );
521 }
522 
523 MONGO_EXPORT time_t bson_oid_generated_time( bson_oid_t *oid ) {
524  time_t out = 0;
525  bson_big_endian32( &out, &oid->ints[0] );
526 
527  return out;
528 }
529 
530 MONGO_EXPORT void bson_print( const bson *b ) {
531  bson_print_raw( b->data , 0 );
532 }
533 
534 MONGO_EXPORT void bson_print_raw( const char *data , int depth ) {
536  const char *key;
537  int temp;
539  char oidhex[25];
540  bson scope;
541  bson_iterator_from_buffer( &i, data );
542 
543  while ( bson_iterator_next( &i ) ) {
544  bson_type t = bson_iterator_type( &i );
545  if ( t == 0 )
546  break;
547  key = bson_iterator_key( &i );
548 
549  for ( temp=0; temp<=depth; temp++ )
550  bson_printf( "\t" );
551  bson_printf( "%s : %d \t " , key , t );
552  switch ( t ) {
553  case BSON_DOUBLE:
554  bson_printf( "%f" , bson_iterator_double( &i ) );
555  break;
556  case BSON_STRING:
557  bson_printf( "%s" , bson_iterator_string( &i ) );
558  break;
559  case BSON_SYMBOL:
560  bson_printf( "SYMBOL: %s" , bson_iterator_string( &i ) );
561  break;
562  case BSON_OID:
563  bson_oid_to_string( bson_iterator_oid( &i ), oidhex );
564  bson_printf( "%s" , oidhex );
565  break;
566  case BSON_BOOL:
567  bson_printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" );
568  break;
569  case BSON_DATE:
570  bson_printf( "%ld" , ( long int )bson_iterator_date( &i ) );
571  break;
572  case BSON_BINDATA:
573  bson_printf( "BSON_BINDATA" );
574  break;
575  case BSON_UNDEFINED:
576  bson_printf( "BSON_UNDEFINED" );
577  break;
578  case BSON_NULL:
579  bson_printf( "BSON_NULL" );
580  break;
581  case BSON_REGEX:
582  bson_printf( "BSON_REGEX: %s", bson_iterator_regex( &i ) );
583  break;
584  case BSON_CODE:
585  bson_printf( "BSON_CODE: %s", bson_iterator_code( &i ) );
586  break;
587  case BSON_CODEWSCOPE:
588  bson_printf( "BSON_CODE_W_SCOPE: %s", bson_iterator_code( &i ) );
589  bson_iterator_code_scope_init( &i, &scope, 0 );
590  bson_printf( "\n\t SCOPE: " );
591  bson_print( &scope );
592  bson_destroy( &scope );
593  break;
594  case BSON_INT:
595  bson_printf( "%d" , bson_iterator_int( &i ) );
596  break;
597  case BSON_LONG:
598  bson_printf( "%lld" , ( uint64_t )bson_iterator_long( &i ) );
599  break;
600  case BSON_TIMESTAMP:
601  ts = bson_iterator_timestamp( &i );
602  bson_printf( "i: %d, t: %d", ts.i, ts.t );
603  break;
604  case BSON_OBJECT:
605  case BSON_ARRAY:
606  bson_printf( "\n" );
607  bson_print_raw( bson_iterator_value( &i ) , depth + 1 );
608  break;
609  default:
610  bson_errprintf( "can't print type : %d\n" , t );
611  }
612  bson_printf( "\n" );
613  }
614 }
615 
616 /* ----------------------------
617  ITERATOR
618  ------------------------------ */
619 
620 MONGO_EXPORT bson_iterator* bson_iterator_alloc( void ) {
621  return ( bson_iterator* )bson_malloc( sizeof( bson_iterator ) );
622 }
623 
624 MONGO_EXPORT void bson_iterator_dealloc( bson_iterator* i ) {
625  bson_free( i );
626 }
627 
628 MONGO_EXPORT void bson_iterator_init( bson_iterator *i, const bson *b ) {
629  i->cur = b->data + 4;
630  i->first = 1;
631 }
632 
633 MONGO_EXPORT void bson_iterator_from_buffer( bson_iterator *i, const char *buffer ) {
634  i->cur = buffer + 4;
635  i->first = 1;
636 }
637 
638 MONGO_EXPORT bson_type bson_find( bson_iterator *it, const bson *obj, const char *name ) {
639  bson_iterator_init( it, (bson *)obj );
640  while( bson_iterator_next( it ) ) {
641  if ( strcmp( name, bson_iterator_key( it ) ) == 0 )
642  break;
643  }
644  return bson_iterator_type( it );
645 }
646 
647 MONGO_EXPORT bson_bool_t bson_iterator_more( const bson_iterator *i ) {
648  return *( i->cur );
649 }
650 
651 MONGO_EXPORT bson_type bson_iterator_next( bson_iterator *i ) {
652  size_t ds;
653 
654  if ( i->first ) {
655  i->first = 0;
656  return ( bson_type )( *i->cur );
657  }
658 
659  switch ( bson_iterator_type( i ) ) {
660  case BSON_EOO:
661  return BSON_EOO; /* don't advance */
662  case BSON_UNDEFINED:
663  case BSON_NULL:
664  case BSON_MINKEY:
665  case BSON_MAXKEY:
666  ds = 0;
667  break;
668  case BSON_BOOL:
669  ds = 1;
670  break;
671  case BSON_INT:
672  ds = 4;
673  break;
674  case BSON_LONG:
675  case BSON_DOUBLE:
676  case BSON_TIMESTAMP:
677  case BSON_DATE:
678  ds = 8;
679  break;
680  case BSON_OID:
681  ds = 12;
682  break;
683  case BSON_STRING:
684  case BSON_SYMBOL:
685  case BSON_CODE:
686  ds = 4 + bson_iterator_int_raw( i );
687  break;
688  case BSON_BINDATA:
689  ds = 5 + bson_iterator_int_raw( i );
690  break;
691  case BSON_OBJECT:
692  case BSON_ARRAY:
693  case BSON_CODEWSCOPE:
694  ds = bson_iterator_int_raw( i );
695  break;
696  case BSON_DBREF:
697  ds = 4+12 + bson_iterator_int_raw( i );
698  break;
699  case BSON_REGEX: {
700  const char *s = bson_iterator_value( i );
701  const char *p = s;
702  p += strlen( p )+1;
703  p += strlen( p )+1;
704  ds = p-s;
705  break;
706  }
707 
708  default: {
709  char msg[] = "unknown type: 000000000000";
710  bson_numstr( msg+14, ( unsigned )( i->cur[0] ) );
711  bson_fatal_msg( 0, msg );
712  return 0;
713  }
714  }
715 
716  i->cur += 1 + strlen( i->cur + 1 ) + 1 + ds;
717 
718  return ( bson_type )( *i->cur );
719 }
720 
721 MONGO_EXPORT bson_type bson_iterator_type( const bson_iterator *i ) {
722  // problem to convert 0xFF to 255
723  return ( bson_type )( unsigned char )i->cur[0];
724 }
725 
726 MONGO_EXPORT const char *bson_iterator_key( const bson_iterator *i ) {
727  return i->cur + 1;
728 }
729 
730 MONGO_EXPORT const char *bson_iterator_value( const bson_iterator *i ) {
731  const char *t = i->cur + 1;
732  t += strlen( t ) + 1;
733  return t;
734 }
735 
736 /* types */
737 
739  int out;
740  bson_little_endian32( &out, bson_iterator_value( i ) );
741  return out;
742 }
743 
745  double out;
746  bson_little_endian64( &out, bson_iterator_value( i ) );
747  return out;
748 }
749 
751  int64_t out;
752  bson_little_endian64( &out, bson_iterator_value( i ) );
753  return out;
754 }
755 
757  return bson_iterator_value( i )[0];
758 }
759 
760 MONGO_EXPORT bson_oid_t *bson_iterator_oid( const bson_iterator *i ) {
761  return ( bson_oid_t * )bson_iterator_value( i );
762 }
763 
764 MONGO_EXPORT int bson_iterator_int( const bson_iterator *i ) {
765  switch ( bson_iterator_type( i ) ) {
766  case BSON_INT:
767  return bson_iterator_int_raw( i );
768  case BSON_LONG:
769  return ( int )bson_iterator_long_raw( i );
770  case BSON_DOUBLE:
771  return ( int )bson_iterator_double_raw( i );
772  default:
773  return 0;
774  }
775 }
776 
777 MONGO_EXPORT double bson_iterator_double( const bson_iterator *i ) {
778  switch ( bson_iterator_type( i ) ) {
779  case BSON_INT:
780  return bson_iterator_int_raw( i );
781  case BSON_LONG:
782  return ( double )bson_iterator_long_raw( i );
783  case BSON_DOUBLE:
784  return bson_iterator_double_raw( i );
785  default:
786  return 0;
787  }
788 }
789 
790 MONGO_EXPORT int64_t bson_iterator_long( const bson_iterator *i ) {
791  switch ( bson_iterator_type( i ) ) {
792  case BSON_INT:
793  return bson_iterator_int_raw( i );
794  case BSON_LONG:
795  return bson_iterator_long_raw( i );
796  case BSON_DOUBLE:
797  return ( int64_t)bson_iterator_double_raw( i );
798  default:
799  return 0;
800  }
801 }
802 
803 MONGO_EXPORT bson_timestamp_t bson_iterator_timestamp( const bson_iterator *i ) {
805  bson_little_endian32( &( ts.i ), bson_iterator_value( i ) );
806  bson_little_endian32( &( ts.t ), bson_iterator_value( i ) + 4 );
807  return ts;
808 }
809 
810 
811 MONGO_EXPORT int bson_iterator_timestamp_time( const bson_iterator *i ) {
812  int time;
813  bson_little_endian32( &time, bson_iterator_value( i ) + 4 );
814  return time;
815 }
816 
817 
818 MONGO_EXPORT int bson_iterator_timestamp_increment( const bson_iterator *i ) {
819  int increment;
820  bson_little_endian32( &increment, bson_iterator_value( i ) );
821  return increment;
822 }
823 
824 
825 MONGO_EXPORT bson_bool_t bson_iterator_bool( const bson_iterator *i ) {
826  switch ( bson_iterator_type( i ) ) {
827  case BSON_BOOL:
828  return bson_iterator_bool_raw( i );
829  case BSON_INT:
830  return bson_iterator_int_raw( i ) != 0;
831  case BSON_LONG:
832  return bson_iterator_long_raw( i ) != 0;
833  case BSON_DOUBLE:
834  return bson_iterator_double_raw( i ) != 0;
835  case BSON_EOO:
836  case BSON_NULL:
837  return 0;
838  default:
839  return 1;
840  }
841 }
842 
843 MONGO_EXPORT const char *bson_iterator_string( const bson_iterator *i ) {
844  switch ( bson_iterator_type( i ) ) {
845  case BSON_STRING:
846  case BSON_SYMBOL:
847  return bson_iterator_value( i ) + 4;
848  default:
849  return "";
850  }
851 }
852 
854  return bson_iterator_int_raw( i );
855 }
856 
857 MONGO_EXPORT const char *bson_iterator_code( const bson_iterator *i ) {
858  switch ( bson_iterator_type( i ) ) {
859  case BSON_STRING:
860  case BSON_CODE:
861  return bson_iterator_value( i ) + 4;
862  case BSON_CODEWSCOPE:
863  return bson_iterator_value( i ) + 8;
864  default:
865  return NULL;
866  }
867 }
868 
869 MONGO_EXPORT void bson_iterator_code_scope_init( const bson_iterator *i, bson *scope, bson_bool_t copyData ) {
870  if ( bson_iterator_type( i ) == BSON_CODEWSCOPE ) {
871  int codeLen = bson_finished_data_size( bson_iterator_value( i )+4 );
872  const char * scopeData = bson_iterator_value( i )+8+codeLen;
873  if( copyData )
874  bson_init_finished_data_with_copy( scope, scopeData );
875  else
876  bson_init_finished_data( scope, (char *)scopeData, 0 );
877  }
878  else {
879  bson_init_empty( scope );
880  }
881 }
882 
883 MONGO_EXPORT bson_date_t bson_iterator_date( const bson_iterator *i ) {
884  return bson_iterator_long_raw( i );
885 }
886 
887 MONGO_EXPORT time_t bson_iterator_time_t( const bson_iterator *i ) {
888  return bson_iterator_date( i ) / 1000;
889 }
890 
891 MONGO_EXPORT int bson_iterator_bin_len( const bson_iterator *i ) {
892  return ( bson_iterator_bin_type( i ) == BSON_BIN_BINARY_OLD )
893  ? bson_iterator_int_raw( i ) - 4
894  : bson_iterator_int_raw( i );
895 }
896 
897 MONGO_EXPORT char bson_iterator_bin_type( const bson_iterator *i ) {
898  return bson_iterator_value( i )[4];
899 }
900 
901 MONGO_EXPORT const char *bson_iterator_bin_data( const bson_iterator *i ) {
902  return ( bson_iterator_bin_type( i ) == BSON_BIN_BINARY_OLD )
903  ? bson_iterator_value( i ) + 9
904  : bson_iterator_value( i ) + 5;
905 }
906 
907 MONGO_EXPORT const char *bson_iterator_regex( const bson_iterator *i ) {
908  return bson_iterator_value( i );
909 }
910 
911 MONGO_EXPORT const char *bson_iterator_regex_opts( const bson_iterator *i ) {
912  const char *p = bson_iterator_value( i );
913  return p + strlen( p ) + 1;
914 
915 }
916 
917 MONGO_EXPORT void bson_iterator_subobject_init( const bson_iterator *i, bson *sub, bson_bool_t copyData ) {
918  const char *data = bson_iterator_value( i );
919  if( copyData )
921  else
922  bson_init_finished_data( sub, (char *)data, 0 );
923 }
924 
925 MONGO_EXPORT void bson_iterator_subiterator( const bson_iterator *i, bson_iterator *sub ) {
926  bson_iterator_from_buffer( sub, bson_iterator_value( i ) );
927 }
928 
929 /* ----------------------------
930  BUILDING
931  ------------------------------ */
932 
933 static void _bson_zero( bson *b ) {
934  memset( b, 0, sizeof( bson ) );
935 }
936 
937 MONGO_EXPORT int bson_init( bson *b ) {
938  return bson_init_size( b, initialBufferSize );
939 }
940 
941 int bson_init_size( bson *b, int size ) {
942  _bson_zero( b );
943  if( size != 0 )
944  {
945  char * data = (char *) bson_malloc( size );
946  if (data == NULL) return BSON_ERROR;
947  b->data = data;
948  b->dataSize = size;
949  }
950  b->ownsData = 1;
951  b->cur = b->data + 4;
952  return BSON_OK;
953 }
954 
955 int bson_init_unfinished_data( bson *b, char *data, int dataSize, bson_bool_t ownsData ) {
956  _bson_zero( b );
957  b->data = data;
958  b->dataSize = dataSize;
959  b->ownsData = ownsData;
960  return BSON_OK;
961 }
962 
963 static int _bson_append_grow_stack( bson * b ) {
964  if ( !b->stackPtr ) {
965  // If this is an empty bson structure, initially use the struct-local (fixed-size) stack
966  b->stackPtr = b->stack;
967  b->stackSize = sizeof( b->stack ) / sizeof( size_t );
968  }
969  else if ( b->stackPtr == b->stack ) {
970  // Once we require additional capacity, set up a dynamically resized stack
971  size_t *new_stack = ( size_t * ) bson_malloc( 2 * sizeof( b->stack ) );
972  if ( new_stack ) {
973  b->stackPtr = new_stack;
974  b->stackSize = 2 * sizeof( b->stack ) / sizeof( size_t );
975  memcpy( b->stackPtr, b->stack, sizeof( b->stack ) );
976  }
977  else {
978  return BSON_ERROR;
979  }
980  }
981  else {
982  // Double the capacity of the dynamically-resized stack
983  size_t *new_stack = ( size_t * ) bson_realloc( b->stackPtr, ( b->stackSize * 2 ) * sizeof( size_t ) );
984  if ( new_stack ) {
985  b->stackPtr = new_stack;
986  b->stackSize *= 2;
987  }
988  else {
989  return BSON_ERROR;
990  }
991  }
992  return BSON_OK;
993 }
994 
995 static void bson_append_byte( bson *b, char c ) {
996  b->cur[0] = c;
997  b->cur++;
998 }
999 
1000 static void bson_append( bson *b, const void *data, size_t len ) {
1001  memcpy( b->cur , data , len );
1002  b->cur += len;
1003 }
1004 
1005 static void bson_append32( bson *b, const void *data ) {
1006  bson_little_endian32( b->cur, data );
1007  b->cur += 4;
1008 }
1009 
1010 static void bson_append32_as_int( bson *b, int data ) {
1011  bson_little_endian32( b->cur, &data );
1012  b->cur += 4;
1013 }
1014 
1015 static void bson_append64( bson *b, const void *data ) {
1016  bson_little_endian64( b->cur, data );
1017  b->cur += 8;
1018 }
1019 
1020 int bson_ensure_space( bson *b, const size_t bytesNeeded ) {
1021  size_t pos = _bson_position(b);
1022  char *orig = b->data;
1023  int new_size;
1024 
1025  if ( pos + bytesNeeded <= (size_t) b->dataSize )
1026  return BSON_OK;
1027 
1028  new_size = (int) ( 1.5 * ( b->dataSize + bytesNeeded ) );
1029 
1030  if( new_size < b->dataSize ) {
1031  if( ( b->dataSize + bytesNeeded ) < INT_MAX )
1032  new_size = INT_MAX;
1033  else {
1034  b->err = BSON_SIZE_OVERFLOW;
1035  return BSON_ERROR;
1036  }
1037  }
1038 
1039  if ( ! b->ownsData ) {
1041  return BSON_ERROR;
1042  }
1043 
1044  b->data = bson_realloc( b->data, new_size );
1045  if ( !b->data )
1046  bson_fatal_msg( !!b->data, "realloc() failed" );
1047 
1048  b->dataSize = new_size;
1049  b->cur += b->data - orig;
1050 
1051  return BSON_OK;
1052 }
1053 
1054 MONGO_EXPORT int bson_finish( bson *b ) {
1055  int i;
1056 
1057  if( b->err & BSON_NOT_UTF8 )
1058  return BSON_ERROR;
1059 
1060  if ( ! b->finished ) {
1061  bson_fatal_msg(!b->stackPos, "Subobject not finished before bson_finish().");
1062  if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
1063  bson_append_byte( b, 0 );
1064  if ( _bson_position(b) >= INT32_MAX ) {
1065  b->err = BSON_SIZE_OVERFLOW;
1066  return BSON_ERROR;
1067  }
1068  i = ( int ) _bson_position(b);
1069  bson_little_endian32( b->data, &i );
1070  b->finished = 1;
1071  }
1072 
1073  return BSON_OK;
1074 }
1075 
1076 MONGO_EXPORT void bson_destroy( bson *b ) {
1077  if ( b ) {
1078  if ( b->ownsData && b->data != NULL ) {
1079  bson_free( b->data );
1080  }
1081  b->data = NULL;
1082  b->dataSize = 0;
1083  b->ownsData = 0;
1084  if ( b->stackPtr && b->stackPtr != b->stack ) {
1085  bson_free( b->stackPtr );
1086  b->stackPtr = NULL;
1087  }
1088  b->stackSize = 0;
1089  b->stackPos = 0;
1090  b->err = 0;
1091  b->cur = 0;
1092  b->finished = 1;
1093  }
1094 }
1095 
1096 static int bson_append_estart( bson *b, int type, const char *name, const size_t dataSize ) {
1097  const size_t len = strlen( name ) + 1;
1098 
1099  if ( b->finished ) {
1100  b->err |= BSON_ALREADY_FINISHED;
1101  return BSON_ERROR;
1102  }
1103 
1104  if ( bson_ensure_space( b, 1 + len + dataSize ) == BSON_ERROR ) {
1105  return BSON_ERROR;
1106  }
1107 
1108  if( bson_check_field_name( b, ( const char * )name, len - 1 ) == BSON_ERROR ) {
1109  bson_builder_error( b );
1110  return BSON_ERROR;
1111  }
1112 
1113  bson_append_byte( b, ( char )type );
1114  bson_append( b, name, len );
1115  return BSON_OK;
1116 }
1117 
1118 /* ----------------------------
1119  BUILDING TYPES
1120  ------------------------------ */
1121 
1122 MONGO_EXPORT int bson_append_int( bson *b, const char *name, const int i ) {
1123  if ( bson_append_estart( b, BSON_INT, name, 4 ) == BSON_ERROR )
1124  return BSON_ERROR;
1125  bson_append32( b , &i );
1126  return BSON_OK;
1127 }
1128 
1129 MONGO_EXPORT int bson_append_long( bson *b, const char *name, const int64_t i ) {
1130  if ( bson_append_estart( b , BSON_LONG, name, 8 ) == BSON_ERROR )
1131  return BSON_ERROR;
1132  bson_append64( b , &i );
1133  return BSON_OK;
1134 }
1135 
1136 MONGO_EXPORT int bson_append_double( bson *b, const char *name, const double d ) {
1137  if ( bson_append_estart( b, BSON_DOUBLE, name, 8 ) == BSON_ERROR )
1138  return BSON_ERROR;
1139  bson_append64( b , &d );
1140  return BSON_OK;
1141 }
1142 
1143 MONGO_EXPORT int bson_append_bool( bson *b, const char *name, const bson_bool_t i ) {
1144  if ( bson_append_estart( b, BSON_BOOL, name, 1 ) == BSON_ERROR )
1145  return BSON_ERROR;
1146  bson_append_byte( b , i != 0 );
1147  return BSON_OK;
1148 }
1149 
1150 MONGO_EXPORT int bson_append_null( bson *b, const char *name ) {
1151  if ( bson_append_estart( b , BSON_NULL, name, 0 ) == BSON_ERROR )
1152  return BSON_ERROR;
1153  return BSON_OK;
1154 }
1155 
1156 MONGO_EXPORT int bson_append_undefined( bson *b, const char *name ) {
1157  if ( bson_append_estart( b, BSON_UNDEFINED, name, 0 ) == BSON_ERROR )
1158  return BSON_ERROR;
1159  return BSON_OK;
1160 }
1161 
1162 MONGO_EXPORT int bson_append_maxkey( bson *b, const char *name ) {
1163  if ( bson_append_estart( b, BSON_MAXKEY, name, 0 ) == BSON_ERROR )
1164  return BSON_ERROR;
1165  return BSON_OK;
1166 }
1167 
1168 MONGO_EXPORT int bson_append_minkey( bson *b, const char *name ) {
1169  if ( bson_append_estart( b, BSON_MINKEY, name, 0 ) == BSON_ERROR )
1170  return BSON_ERROR;
1171  return BSON_OK;
1172 }
1173 
1174 static int bson_append_string_base( bson *b, const char *name,
1175  const char *value, size_t len, bson_type type ) {
1176 
1177  size_t sl = len + 1;
1178  if ( sl > INT32_MAX ) {
1179  b->err = BSON_SIZE_OVERFLOW;
1180  /* string too long */
1181  return BSON_ERROR;
1182  }
1183  if ( bson_check_string( b, ( const char * )value, sl - 1 ) == BSON_ERROR )
1184  return BSON_ERROR;
1185  if ( bson_append_estart( b, type, name, 4 + sl ) == BSON_ERROR ) {
1186  return BSON_ERROR;
1187  }
1188  bson_append32_as_int( b , ( int )sl );
1189  bson_append( b , value , sl - 1 );
1190  bson_append( b , "\0" , 1 );
1191  return BSON_OK;
1192 }
1193 
1194 MONGO_EXPORT int bson_append_string( bson *b, const char *name, const char *value ) {
1195  return bson_append_string_base( b, name, value, strlen ( value ), BSON_STRING );
1196 }
1197 
1198 MONGO_EXPORT int bson_append_symbol( bson *b, const char *name, const char *value ) {
1199  return bson_append_string_base( b, name, value, strlen ( value ), BSON_SYMBOL );
1200 }
1201 
1202 MONGO_EXPORT int bson_append_code( bson *b, const char *name, const char *value ) {
1203  return bson_append_string_base( b, name, value, strlen ( value ), BSON_CODE );
1204 }
1205 
1206 MONGO_EXPORT int bson_append_string_n( bson *b, const char *name, const char *value, size_t len ) {
1207  return bson_append_string_base( b, name, value, len, BSON_STRING );
1208 }
1209 
1210 MONGO_EXPORT int bson_append_symbol_n( bson *b, const char *name, const char *value, size_t len ) {
1211  return bson_append_string_base( b, name, value, len, BSON_SYMBOL );
1212 }
1213 
1214 MONGO_EXPORT int bson_append_code_n( bson *b, const char *name, const char *value, size_t len ) {
1215  return bson_append_string_base( b, name, value, len, BSON_CODE );
1216 }
1217 
1218 MONGO_EXPORT int bson_append_code_w_scope_n( bson *b, const char *name,
1219  const char *code, size_t len, const bson *scope ) {
1220 
1221  size_t sl, size;
1222  if ( !scope ) return BSON_ERROR;
1223  sl = len + 1;
1224  size = 4 + 4 + sl + bson_size( scope );
1225  if ( size > (size_t)INT32_MAX ) {
1226  b->err = BSON_SIZE_OVERFLOW;
1227  return BSON_ERROR;
1228  }
1229  if ( bson_append_estart( b, BSON_CODEWSCOPE, name, size ) == BSON_ERROR )
1230  return BSON_ERROR;
1231  bson_append32_as_int( b, ( int )size );
1232  bson_append32( b, &sl );
1233  bson_append( b, code, sl );
1234  bson_append( b, scope->data, bson_size( scope ) );
1235  return BSON_OK;
1236 }
1237 
1238 MONGO_EXPORT int bson_append_code_w_scope( bson *b, const char *name, const char *code, const bson *scope ) {
1239  return bson_append_code_w_scope_n( b, name, code, strlen ( code ), scope );
1240 }
1241 
1242 MONGO_EXPORT int bson_append_binary( bson *b, const char *name, char type, const char *str, size_t len ) {
1243  if ( type == BSON_BIN_BINARY_OLD ) {
1244  size_t subtwolen = len + 4;
1245  if ( bson_append_estart( b, BSON_BINDATA, name, 4+1+4+len ) == BSON_ERROR )
1246  return BSON_ERROR;
1247  bson_append32_as_int( b, ( int )subtwolen );
1248  bson_append_byte( b, type );
1249  bson_append32_as_int( b, ( int )len );
1250  bson_append( b, str, len );
1251  }
1252  else {
1253  if ( bson_append_estart( b, BSON_BINDATA, name, 4+1+len ) == BSON_ERROR )
1254  return BSON_ERROR;
1255  bson_append32_as_int( b, ( int )len );
1256  bson_append_byte( b, type );
1257  bson_append( b, str, len );
1258  }
1259  return BSON_OK;
1260 }
1261 
1262 MONGO_EXPORT int bson_append_oid( bson *b, const char *name, const bson_oid_t *oid ) {
1263  if ( bson_append_estart( b, BSON_OID, name, 12 ) == BSON_ERROR )
1264  return BSON_ERROR;
1265  bson_append( b , oid , 12 );
1266  return BSON_OK;
1267 }
1268 
1269 MONGO_EXPORT int bson_append_new_oid( bson *b, const char *name ) {
1270  bson_oid_t oid;
1271  bson_oid_gen( &oid );
1272  return bson_append_oid( b, name, &oid );
1273 }
1274 
1275 MONGO_EXPORT int bson_append_regex( bson *b, const char *name, const char *pattern, const char *opts ) {
1276  const size_t plen = strlen( pattern )+1;
1277  const size_t olen = strlen( opts )+1;
1278  if ( bson_append_estart( b, BSON_REGEX, name, plen + olen ) == BSON_ERROR )
1279  return BSON_ERROR;
1280  if ( bson_check_string( b, pattern, plen - 1 ) == BSON_ERROR )
1281  return BSON_ERROR;
1282  bson_append( b , pattern , plen );
1283  bson_append( b , opts , olen );
1284  return BSON_OK;
1285 }
1286 
1287 MONGO_EXPORT int bson_append_bson( bson *b, const char *name, const bson *bson ) {
1288  if ( !bson ) return BSON_ERROR;
1289  if ( bson_append_estart( b, BSON_OBJECT, name, bson_size( bson ) ) == BSON_ERROR )
1290  return BSON_ERROR;
1291  bson_append( b , bson->data , bson_size( bson ) );
1292  return BSON_OK;
1293 }
1294 
1295 MONGO_EXPORT int bson_append_element( bson *b, const char *name_or_null, const bson_iterator *elem ) {
1296  bson_iterator next = *elem;
1297  size_t size;
1298 
1299  bson_iterator_next( &next );
1300  size = next.cur - elem->cur;
1301 
1302  if ( name_or_null == NULL ) {
1303  if( bson_ensure_space( b, size ) == BSON_ERROR )
1304  return BSON_ERROR;
1305  bson_append( b, elem->cur, size );
1306  }
1307  else {
1308  size_t data_size = size - 2 - strlen( bson_iterator_key( elem ) );
1309  if ( bson_append_estart( b, elem->cur[0], name_or_null, data_size ) == BSON_ERROR )
1310  return BSON_ERROR;
1311  bson_append( b, bson_iterator_value( elem ), data_size );
1312  }
1313 
1314  return BSON_OK;
1315 }
1316 
1317 MONGO_EXPORT int bson_append_timestamp( bson *b, const char *name, bson_timestamp_t *ts ) {
1318  if ( bson_append_estart( b, BSON_TIMESTAMP, name, 8 ) == BSON_ERROR ) return BSON_ERROR;
1319 
1320  bson_append32( b , &( ts->i ) );
1321  bson_append32( b , &( ts->t ) );
1322 
1323  return BSON_OK;
1324 }
1325 
1326 MONGO_EXPORT int bson_append_timestamp2( bson *b, const char *name, int time, int increment ) {
1327  if ( bson_append_estart( b, BSON_TIMESTAMP, name, 8 ) == BSON_ERROR ) return BSON_ERROR;
1328 
1329  bson_append32( b , &increment );
1330  bson_append32( b , &time );
1331  return BSON_OK;
1332 }
1333 
1334 MONGO_EXPORT int bson_append_date( bson *b, const char *name, bson_date_t millis ) {
1335  if ( bson_append_estart( b, BSON_DATE, name, 8 ) == BSON_ERROR ) return BSON_ERROR;
1336  bson_append64( b , &millis );
1337  return BSON_OK;
1338 }
1339 
1340 MONGO_EXPORT int bson_append_time_t( bson *b, const char *name, time_t secs ) {
1341  return bson_append_date( b, name, ( bson_date_t )secs * 1000 );
1342 }
1343 
1344 MONGO_EXPORT int bson_append_start_object( bson *b, const char *name ) {
1345  if ( bson_append_estart( b, BSON_OBJECT, name, 5 ) == BSON_ERROR ) return BSON_ERROR;
1346  if ( b->stackPos >= b->stackSize && _bson_append_grow_stack( b ) == BSON_ERROR ) return BSON_ERROR;
1347  b->stackPtr[ b->stackPos++ ] = _bson_position(b);
1348  bson_append32( b , &zero );
1349  return BSON_OK;
1350 }
1351 
1352 MONGO_EXPORT int bson_append_start_array( bson *b, const char *name ) {
1353  if ( bson_append_estart( b, BSON_ARRAY, name, 5 ) == BSON_ERROR ) return BSON_ERROR;
1354  if ( b->stackPos >= b->stackSize && _bson_append_grow_stack( b ) == BSON_ERROR ) return BSON_ERROR;
1355  b->stackPtr[ b->stackPos++ ] = _bson_position(b);
1356  bson_append32( b , &zero );
1357  return BSON_OK;
1358 }
1359 
1360 MONGO_EXPORT int bson_append_finish_object( bson *b ) {
1361  char *start;
1362  int i;
1363  if (!b) return BSON_ERROR;
1364  if (!b->stackPos) { b->err = BSON_NOT_IN_SUBOBJECT; return BSON_ERROR; }
1365  if ( bson_ensure_space( b, 1 ) == BSON_ERROR ) return BSON_ERROR;
1366  bson_append_byte( b , 0 );
1367 
1368  start = b->data + b->stackPtr[ --b->stackPos ];
1369  if ( b->cur - start >= INT32_MAX ) {
1370  b->err = BSON_SIZE_OVERFLOW;
1371  return BSON_ERROR;
1372  }
1373  i = ( int )( b->cur - start );
1374  bson_little_endian32( start, &i );
1375 
1376  return BSON_OK;
1377 }
1378 
1379 MONGO_EXPORT double bson_int64_to_double( int64_t i64 ) {
1380  return (double)i64;
1381 }
1382 
1383 MONGO_EXPORT int bson_append_finish_array( bson *b ) {
1384  return bson_append_finish_object( b );
1385 }
1386 
1387 /* Error handling and allocators. */
1388 
1390 
1391 MONGO_EXPORT bson_err_handler set_bson_err_handler( bson_err_handler func ) {
1393  err_handler = func;
1394  return old;
1395 }
1396 
1397 MONGO_EXPORT void bson_free( void *ptr ) {
1398  bson_free_func( ptr );
1399 }
1400 
1401 MONGO_EXPORT void *bson_malloc( size_t size ) {
1402  void *p;
1403  p = bson_malloc_func( size );
1404  bson_fatal_msg( !!p, "malloc() failed" );
1405  return p;
1406 }
1407 
1408 void *bson_realloc( void *ptr, size_t size ) {
1409  void *p;
1410  p = bson_realloc_func( ptr, size );
1411  bson_fatal_msg( !!p, "realloc() failed" );
1412  return p;
1413 }
1414 
1415 int _bson_errprintf( const char *format, ... ) {
1416  va_list ap;
1417  int ret = 0;
1418  va_start( ap, format );
1419 #ifndef R_SAFETY_NET
1420  ret = vfprintf( stderr, format, ap );
1421 #endif
1422  va_end( ap );
1423 
1424  return ret;
1425 }
1426 
1433 void bson_builder_error( bson *b ) {
1434  if( err_handler )
1435  err_handler( "BSON error." );
1436 }
1437 
1438 void bson_fatal( int ok ) {
1439  bson_fatal_msg( ok, "" );
1440 }
1441 
1442 void bson_fatal_msg( int ok , const char *msg ) {
1443  if ( ok )
1444  return;
1445 
1446  if ( err_handler ) {
1447  err_handler( msg );
1448  }
1449 #ifndef R_SAFETY_NET
1450  bson_errprintf( "error: %s\n" , msg );
1451  exit( -5 );
1452 #endif
1453 }
1454 
1455 
1456 /* Efficiently copy an integer to a string. */
1457 extern const char bson_numstrs[1000][4];
1458 
1459 void bson_numstr( char *str, int i ) {
1460  if( i < 1000 )
1461  memcpy( str, bson_numstrs[i], 4 );
1462  else
1463  bson_sprintf( str,"%d", i );
1464 }
1465 
1466 MONGO_EXPORT void bson_swap_endian64( void *outp, const void *inp ) {
1467  const char *in = ( const char * )inp;
1468  char *out = ( char * )outp;
1469 
1470  out[0] = in[7];
1471  out[1] = in[6];
1472  out[2] = in[5];
1473  out[3] = in[4];
1474  out[4] = in[3];
1475  out[5] = in[2];
1476  out[6] = in[1];
1477  out[7] = in[0];
1478 
1479 }
1480 
1481 MONGO_EXPORT void bson_swap_endian32( void *outp, const void *inp ) {
1482  const char *in = ( const char * )inp;
1483  char *out = ( char * )outp;
1484 
1485  out[0] = in[3];
1486  out[1] = in[2];
1487  out[2] = in[1];
1488  out[3] = in[0];
1489 }
const bson * b
Definition: bson.h:280
static void bson_append32_as_int(bson *b, int data)
Definition: bson.c:1010
const char const double d
Definition: bson.h:800
int bson_init_unfinished_data(bson *b, char *data, int dataSize, bson_bool_t ownsData)
Initialize a BSON object for building, using the provided char* of the given size.
Definition: bson.c:955
static int isLegalUTF8(const unsigned char *source, int length)
Definition: bson.c:143
const char const char size_t len
Definition: bson.h:823
const char bson_timestamp_t * ts
Definition: bson.h:1004
static int bson_string_is_db_ref(const unsigned char *string, const size_t length)
Definition: bson.c:178
A key or a string is not valid UTF-8.
Definition: bson.h:68
static const int zero
Definition: bson.c:111
int err
Bitfield representing errors or warnings on this buffer.
Definition: bson.h:119
static char bson_shared_empty_data[]
Definition: bson.c:361
Definition: bson.h:91
bson_printf_func bson_printf
Definition: bson.c:370
const bson * obj
Definition: bson.h:269
bson_sprintf_func bson_sprintf
Definition: bson.c:373
uint8_t uc[8]
Definition: db3.c:48
Definition: bson.h:93
const char int time
Definition: bson.h:1005
Definition: bson.h:90
void bson_numstr(char *str, int i)
Definition: bson.c:1459
int bson_iterator_string_len(const bson_iterator *i)
Get the string length of the BSON object currently pointed to by the iterator.
Definition: bson.c:853
Trying to expand a BSON object which does not own its data block.
Definition: bson.h:63
int bson_init_size(bson *b, int size)
Initialize a BSON object for building and allocate a data buffer of a given size. ...
Definition: bson.c:941
static int _bson_errprintf(const char *,...)
Definition: bson.c:1415
const char bson_date_t millis
Definition: bson.h:1017
static int bson_finished_data_size(const char *data)
Definition: bson.c:402
bson bson_bool_t copyData
Definition: bson.h:491
const char * buffer
Definition: bson.h:289
double bson_iterator_double_raw(const bson_iterator *i)
Get the double value of the BSON object currently pointed to by the iterator.
Definition: bson.c:744
#define BSON_OK
Definition: bson.h:56
int bson_init_finished_data_with_copy(bson *b, const char *data)
Initialize a BSON object for reading and copy finalized BSON data from the provided char*...
Definition: bson.c:417
int dataSize
The number of bytes allocated to char *data.
Definition: bson.h:116
static void _bson_zero(bson *b)
Definition: bson.c:933
int ints[3]
Definition: bson.h:130
Definition: bson.h:92
char * data
Pointer to a block of data in this BSON object.
Definition: bson.h:114
Trying bson_append_finish_object() and not in sub.
Definition: bson.h:62
bson * sub
Definition: bson.h:576
static int(* oid_fuzz_func)(void)
Definition: bson.c:382
#define MONGO_EXPORT
Definition: bson.h:42
void bson_fatal(int ok)
Exit fatally.
Definition: bson.c:1438
static int bson_check_field_name(bson *b, const char *string, const size_t length)
Definition: bson.c:236
static const char trailingBytesForUTF8[256]
Definition: bson.c:120
const char * str
Definition: bson.h:593
static void bson_append_byte(bson *b, char c)
Definition: bson.c:995
void *(* bson_realloc_func)(void *, size_t)
Definition: bson.c:365
static void bson_append32(bson *b, const void *data)
Definition: bson.c:1005
static int(* oid_inc_func)(void)
Definition: bson.c:383
void bson_big_endian64(void *outp, const void *inp)
Definition: bson.c:71
bson_printf_func bson_errprintf
Definition: bson.c:376
static int bson_append_estart(bson *b, int type, const char *name, const size_t dataSize)
Definition: bson.c:1096
const void * inp
Definition: bson.h:1157
const char const bson_oid_t * oid
Definition: bson.h:757
const char const bson * data
Definition: mongo.h:463
bson_bool_t ownsData
Whether destroying this object will deallocate its data block.
Definition: bson.h:118
int(* bson_printf_func)(const char *,...)
Definition: bson.h:1079
static int bson_append_string_base(bson *b, const char *name, const char *value, size_t len, bson_type type)
Definition: bson.c:1174
int(* bson_fprintf_func)(FILE *, const char *,...)
Definition: bson.h:1080
Definition: bson.h:113
char bytes[12]
Definition: bson.h:129
static int bson_check_string(bson *b, const char *string, const size_t length)
Definition: bson.c:230
static bson_err_handler err_handler
Definition: bson.c:1389
static size_t _bson_position(const bson *b)
Definition: bson.c:449
static char hexbyte(char hex)
Definition: bson.c:462
const char time_t secs
Definition: bson.h:1028
void(* bson_free_func)(void *)
Definition: bson.c:366
static const int initialBufferSize
Definition: bson.c:108
int stackPos
Index of current stack position.
Definition: bson.h:121
void bson_little_endian32(void *outp, const void *inp)
Definition: bson.c:55
BSON Declarations.
void bson_builder_error(bson *b)
This method is invoked when a non-fatal bson error is encountered.
Definition: bson.c:1433
int depth
Definition: bson.h:258
int bson_iterator_int_raw(const bson_iterator *i)
Get the int value of the BSON object currently pointed to by the iterator.
Definition: bson.c:738
bson_bool_t bson_iterator_bool_raw(const bson_iterator *i)
Get the bson_bool_t value of the BSON object currently pointed to by the iterator.
Definition: bson.c:756
size_t stack[32]
A stack used to keep track of nested BSON elements.
Definition: bson.h:123
static void bson_append(bson *b, const void *data, size_t len)
Definition: bson.c:1000
const char * name_or_null
Definition: bson.h:993
void bson_big_endian32(void *outp, const void *inp)
Definition: bson.c:92
Definition: bson.h:96
void bson_fatal_msg(int ok, const char *msg)
Exit fatally with an error message.
Definition: bson.c:1442
Warning: key starts with '$' character.
Definition: bson.h:70
bson_bool_t first
Definition: bson.h:110
bson_fprintf_func bson_fprintf
Definition: bson.c:372
const char const char const char * opts
Definition: bson.h:971
int stackSize
Number of elements in the current stack.
Definition: bson.h:120
const char const int i
Definition: bson.h:778
bson_bool_t finished
When finished, the BSON object can no longer be modified.
Definition: bson.h:117
const char int int increment
Definition: bson.h:1005
const char const bson * key
Definition: mongo.h:717
const char const bson const bson bson * out
Definition: mongo.h:678
const char const char size_t size
Definition: bson.h:895
const char bson_numstrs[1000][4]
Definition: bson.c:246
bson_type
Definition: bson.h:82
int bson_bool_t
Definition: bson.h:106
void(* bson_err_handler)(const char *errmsg)
Definition: bson.h:1077
const bson * in
Definition: bson.h:746
Definition: bson.h:99
int bson_ensure_space(bson *b, const size_t bytesNeeded)
Grow a bson object.
Definition: bson.c:1020
Deprecated.
Definition: bson.h:95
static int bson_validate_string(bson *b, const unsigned char *string, const size_t length, const char check_utf8, const char check_dot, const char check_dollar)
Definition: bson.c:195
const char char type
Definition: bson.h:908
const char * cur
Definition: bson.h:109
Definition: bson.h:83
#define BSON_ERROR
Definition: bson.h:57
const char const char * code
Definition: bson.h:882
bson * scope
Definition: bson.h:491
uint32_t ui
Definition: db3.c:46
static const char * name
static int _bson_append_grow_stack(bson *b)
Definition: bson.c:963
void bson_little_endian64(void *outp, const void *inp)
Definition: bson.c:34
Warning: key contains '.
Definition: bson.h:69
const char const char * pattern
Definition: bson.h:971
void *(* bson_malloc_func)(size_t)
Definition: bson.c:364
static void bson_append64(bson *b, const void *data)
Definition: bson.c:1015
int64_t bson_iterator_long_raw(const bson_iterator *i)
Get the long value of the BSON object currently pointed to by the iterator.
Definition: bson.c:750
Definition: db3.c:44
int64_t bson_date_t
Definition: bson.h:134
Trying to create a BSON object larger than INT_MAX.
Definition: bson.h:60
int bson_init_finished_data(bson *b, char *data, bson_bool_t ownsData)
Initialize a BSON object for reading and set its data pointer to the provided char*.
Definition: bson.c:408
int(* bson_sprintf_func)(char *, const char *,...)
Definition: bson.h:1081
size_t * stackPtr
Pointer to the current stack.
Definition: bson.h:122
const char const bson_iterator * elem
Definition: bson.h:993
char * cur
Pointer to the current position.
Definition: bson.h:115
void * bson_realloc(void *ptr, size_t size)
Changes the size of allocated memory and checks return value, exiting fatally if realloc() fails...
Definition: bson.c:1408
Trying to modify a finished BSON object.
Definition: bson.h:61