1 /*
2  * Copyright 2013 MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef BSON_H
19 #define BSON_H
20 
21 #define BSON_INSIDE
22 
23 #include "bson-compat.h"
24 
25 #include <string.h>
26 #include <time.h>
27 
28 #include "bson-macros.h"
29 #include "bson-config.h"
30 #include "bson-atomic.h"
31 #include "bson-context.h"
32 #include "bson-clock.h"
33 #include "bson-decimal128.h"
34 #include "bson-error.h"
35 #include "bson-iter.h"
36 #include "bson-json.h"
37 #include "bson-keys.h"
38 #include "bson-md5.h"
39 #include "bson-memory.h"
40 #include "bson-oid.h"
41 #include "bson-reader.h"
42 #include "bson-string.h"
43 #include "bson-types.h"
44 #include "bson-utf8.h"
45 #include "bson-value.h"
46 #include "bson-version.h"
47 #include "bson-version-functions.h"
48 #include "bson-writer.h"
49 #include "bcon.h"
50 
51 #undef BSON_INSIDE
52 
53 
54 BSON_BEGIN_DECLS
55 
56 
57 /**
58  * bson_empty:
59  * @b: a bson_t.
60  *
61  * Checks to see if @b is an empty BSON document. An empty BSON document is
62  * a 5 byte document which contains the length (4 bytes) and a single NUL
63  * byte indicating end of fields.
64  */
65 #define bson_empty(b) (((b)->len == 5) || !bson_get_data ((b))[4])
66 
67 
68 /**
69  * bson_empty0:
70  *
71  * Like bson_empty() but treats NULL the same as an empty bson_t document.
72  */
73 #define bson_empty0(b) (!(b) || bson_empty (b))
74 
75 
76 /**
77  * bson_clear:
78  *
79  * Easily free a bson document and set it to NULL. Use like:
80  *
81  * bson_t *doc = bson_new();
82  * bson_clear (&doc);
83  * BSON_ASSERT (doc == NULL);
84  */
85 #define bson_clear(bptr)         \
86    do {                          \
87       if (*(bptr)) {             \
88          bson_destroy (*(bptr)); \
89          *(bptr) = NULL;         \
90       }                          \
91    } while (0)
92 
93 
94 /**
95  * BSON_MAX_SIZE:
96  *
97  * The maximum size in bytes of a BSON document.
98  */
99 #define BSON_MAX_SIZE ((size_t) ((1U << 31) - 1))
100 
101 
102 #define BSON_APPEND_ARRAY(b, key, val) \
103    bson_append_array (b, key, (int) strlen (key), val)
104 
105 #define BSON_APPEND_ARRAY_BEGIN(b, key, child) \
106    bson_append_array_begin (b, key, (int) strlen (key), child)
107 
108 #define BSON_APPEND_BINARY(b, key, subtype, val, len) \
109    bson_append_binary (b, key, (int) strlen (key), subtype, val, len)
110 
111 #define BSON_APPEND_BOOL(b, key, val) \
112    bson_append_bool (b, key, (int) strlen (key), val)
113 
114 #define BSON_APPEND_CODE(b, key, val) \
115    bson_append_code (b, key, (int) strlen (key), val)
116 
117 #define BSON_APPEND_CODE_WITH_SCOPE(b, key, val, scope) \
118    bson_append_code_with_scope (b, key, (int) strlen (key), val, scope)
119 
120 #define BSON_APPEND_DBPOINTER(b, key, coll, oid) \
121    bson_append_dbpointer (b, key, (int) strlen (key), coll, oid)
122 
123 #define BSON_APPEND_DOCUMENT_BEGIN(b, key, child) \
124    bson_append_document_begin (b, key, (int) strlen (key), child)
125 
126 #define BSON_APPEND_DOUBLE(b, key, val) \
127    bson_append_double (b, key, (int) strlen (key), val)
128 
129 #define BSON_APPEND_DOCUMENT(b, key, val) \
130    bson_append_document (b, key, (int) strlen (key), val)
131 
132 #define BSON_APPEND_INT32(b, key, val) \
133    bson_append_int32 (b, key, (int) strlen (key), val)
134 
135 #define BSON_APPEND_INT64(b, key, val) \
136    bson_append_int64 (b, key, (int) strlen (key), val)
137 
138 #define BSON_APPEND_MINKEY(b, key) \
139    bson_append_minkey (b, key, (int) strlen (key))
140 
141 #define BSON_APPEND_DECIMAL128(b, key, val) \
142    bson_append_decimal128 (b, key, (int) strlen (key), val)
143 
144 #define BSON_APPEND_MAXKEY(b, key) \
145    bson_append_maxkey (b, key, (int) strlen (key))
146 
147 #define BSON_APPEND_NULL(b, key) bson_append_null (b, key, (int) strlen (key))
148 
149 #define BSON_APPEND_OID(b, key, val) \
150    bson_append_oid (b, key, (int) strlen (key), val)
151 
152 #define BSON_APPEND_REGEX(b, key, val, opt) \
153    bson_append_regex (b, key, (int) strlen (key), val, opt)
154 
155 #define BSON_APPEND_UTF8(b, key, val) \
156    bson_append_utf8 (b, key, (int) strlen (key), val, (int) strlen (val))
157 
158 #define BSON_APPEND_SYMBOL(b, key, val) \
159    bson_append_symbol (b, key, (int) strlen (key), val, (int) strlen (val))
160 
161 #define BSON_APPEND_TIME_T(b, key, val) \
162    bson_append_time_t (b, key, (int) strlen (key), val)
163 
164 #define BSON_APPEND_TIMEVAL(b, key, val) \
165    bson_append_timeval (b, key, (int) strlen (key), val)
166 
167 #define BSON_APPEND_DATE_TIME(b, key, val) \
168    bson_append_date_time (b, key, (int) strlen (key), val)
169 
170 #define BSON_APPEND_TIMESTAMP(b, key, val, inc) \
171    bson_append_timestamp (b, key, (int) strlen (key), val, inc)
172 
173 #define BSON_APPEND_UNDEFINED(b, key) \
174    bson_append_undefined (b, key, (int) strlen (key))
175 
176 #define BSON_APPEND_VALUE(b, key, val) \
177    bson_append_value (b, key, (int) strlen (key), (val))
178 
179 
180 /**
181  * bson_new:
182  *
183  * Allocates a new bson_t structure. Call the various bson_append_*()
184  * functions to add fields to the bson. You can iterate the bson_t at any
185  * time using a bson_iter_t and bson_iter_init().
186  *
187  * Returns: A newly allocated bson_t that should be freed with bson_destroy().
188  */
189 BSON_EXPORT (bson_t *)
190 bson_new (void);
191 
192 
193 BSON_EXPORT (bson_t *)
194 bson_new_from_json (const uint8_t *data, ssize_t len, bson_error_t *error);
195 
196 
197 BSON_EXPORT (bool)
198 bson_init_from_json (bson_t *bson,
199                      const char *data,
200                      ssize_t len,
201                      bson_error_t *error);
202 
203 
204 /**
205  * bson_init_static:
206  * @b: A pointer to a bson_t.
207  * @data: The data buffer to use.
208  * @length: The length of @data.
209  *
210  * Initializes a bson_t using @data and @length. This is ideal if you would
211  * like to use a stack allocation for your bson and do not need to grow the
212  * buffer. @data must be valid for the life of @b.
213  *
214  * Returns: true if initialized successfully; otherwise false.
215  */
216 BSON_EXPORT (bool)
217 bson_init_static (bson_t *b, const uint8_t *data, size_t length);
218 
219 
220 /**
221  * bson_init:
222  * @b: A pointer to a bson_t.
223  *
224  * Initializes a bson_t for use. This function is useful to those that want a
225  * stack allocated bson_t. The usefulness of a stack allocated bson_t is
226  * marginal as the target buffer for content will still require heap
227  * allocations. It can help reduce heap fragmentation on allocators that do
228  * not employ SLAB/magazine semantics.
229  *
230  * You must call bson_destroy() with @b to release resources when you are done
231  * using @b.
232  */
233 BSON_EXPORT (void)
234 bson_init (bson_t *b);
235 
236 
237 /**
238  * bson_reinit:
239  * @b: (inout): A bson_t.
240  *
241  * This is equivalent to calling bson_destroy() and bson_init() on a #bson_t.
242  * However, it will try to persist the existing malloc'd buffer if one exists.
243  * This is useful in cases where you want to reduce malloc overhead while
244  * building many documents.
245  */
246 BSON_EXPORT (void)
247 bson_reinit (bson_t *b);
248 
249 
250 /**
251  * bson_new_from_data:
252  * @data: A buffer containing a serialized bson document.
253  * @length: The length of the document in bytes.
254  *
255  * Creates a new bson_t structure using the data provided. @data should contain
256  * at least @length bytes that can be copied into the new bson_t structure.
257  *
258  * Returns: A newly allocated bson_t that should be freed with bson_destroy().
259  *   If the first four bytes (little-endian) of data do not match @length,
260  *   then NULL will be returned.
261  */
262 BSON_EXPORT (bson_t *)
263 bson_new_from_data (const uint8_t *data, size_t length);
264 
265 
266 /**
267  * bson_new_from_buffer:
268  * @buf: A pointer to a buffer containing a serialized bson document.
269  * @buf_len: The length of the buffer in bytes.
270  * @realloc_fun: a realloc like function
271  * @realloc_fun_ctx: a context for the realloc function
272  *
273  * Creates a new bson_t structure using the data provided. @buf should contain
274  * a bson document, or null pointer should be passed for new allocations.
275  *
276  * Returns: A newly allocated bson_t that should be freed with bson_destroy().
277  *          The underlying buffer will be used and not be freed in destroy.
278  */
279 BSON_EXPORT (bson_t *)
280 bson_new_from_buffer (uint8_t **buf,
281                       size_t *buf_len,
282                       bson_realloc_func realloc_func,
283                       void *realloc_func_ctx);
284 
285 
286 /**
287  * bson_sized_new:
288  * @size: A size_t containing the number of bytes to allocate.
289  *
290  * This will allocate a new bson_t with enough bytes to hold a buffer
291  * sized @size. @size must be smaller than INT_MAX bytes.
292  *
293  * Returns: A newly allocated bson_t that should be freed with bson_destroy().
294  */
295 BSON_EXPORT (bson_t *)
296 bson_sized_new (size_t size);
297 
298 
299 /**
300  * bson_copy:
301  * @bson: A bson_t.
302  *
303  * Copies @bson into a newly allocated bson_t. You must call bson_destroy()
304  * when you are done with the resulting value to free its resources.
305  *
306  * Returns: A newly allocated bson_t that should be free'd with bson_destroy()
307  */
308 BSON_EXPORT (bson_t *)
309 bson_copy (const bson_t *bson);
310 
311 
312 /**
313  * bson_copy_to:
314  * @src: The source bson_t.
315  * @dst: The destination bson_t.
316  *
317  * Initializes @dst and copies the content from @src into @dst.
318  */
319 BSON_EXPORT (void)
320 bson_copy_to (const bson_t *src, bson_t *dst);
321 
322 
323 /**
324  * bson_copy_to_excluding:
325  * @src: A bson_t.
326  * @dst: A bson_t to initialize and copy into.
327  * @first_exclude: First field name to exclude.
328  *
329  * Copies @src into @dst excluding any field that is provided.
330  * This is handy for situations when you need to remove one or
331  * more fields in a bson_t. Note that bson_init() will be called
332  * on dst.
333  */
334 BSON_EXPORT (void)
335 bson_copy_to_excluding (const bson_t *src,
336                         bson_t *dst,
337                         const char *first_exclude,
338                         ...) BSON_GNUC_NULL_TERMINATED
339    BSON_GNUC_DEPRECATED_FOR (bson_copy_to_excluding_noinit);
340 
341 /**
342  * bson_copy_to_excluding_noinit:
343  * @src: A bson_t.
344  * @dst: A bson_t to initialize and copy into.
345  * @first_exclude: First field name to exclude.
346  *
347  * The same as bson_copy_to_excluding, but does not call bson_init()
348  * on the dst. This version should be preferred in new code, but the
349  * old function is left for backwards compatibility.
350  */
351 BSON_EXPORT (void)
352 bson_copy_to_excluding_noinit (const bson_t *src,
353                                bson_t *dst,
354                                const char *first_exclude,
355                                ...) BSON_GNUC_NULL_TERMINATED;
356 
357 /**
358  * bson_destroy:
359  * @bson: A bson_t.
360  *
361  * Frees the resources associated with @bson.
362  */
363 BSON_EXPORT (void)
364 bson_destroy (bson_t *bson);
365 
366 BSON_EXPORT (uint8_t *)
367 bson_reserve_buffer (bson_t *bson, uint32_t size);
368 
369 BSON_EXPORT (bool)
370 bson_steal (bson_t *dst, bson_t *src);
371 
372 
373 /**
374  * bson_destroy_with_steal:
375  * @bson: A #bson_t.
376  * @steal: If ownership of the data buffer should be transferred to caller.
377  * @length: (out): location for the length of the buffer.
378  *
379  * Destroys @bson similar to calling bson_destroy() except that the underlying
380  * buffer will be returned and ownership transferred to the caller if @steal
381  * is non-zero.
382  *
383  * If length is non-NULL, the length of @bson will be stored in @length.
384  *
385  * It is a programming error to call this function with any bson that has
386  * been initialized static, or is being used to create a subdocument with
387  * functions such as bson_append_document_begin() or bson_append_array_begin().
388  *
389  * Returns: a buffer owned by the caller if @steal is true. Otherwise NULL.
390  *    If there was an error, NULL is returned.
391  */
392 BSON_EXPORT (uint8_t *)
393 bson_destroy_with_steal (bson_t *bson, bool steal, uint32_t *length);
394 
395 
396 /**
397  * bson_get_data:
398  * @bson: A bson_t.
399  *
400  * Fetched the data buffer for @bson of @bson->len bytes in length.
401  *
402  * Returns: A buffer that should not be modified or freed.
403  */
404 BSON_EXPORT (const uint8_t *)
405 bson_get_data (const bson_t *bson);
406 
407 
408 /**
409  * bson_count_keys:
410  * @bson: A bson_t.
411  *
412  * Counts the number of elements found in @bson.
413  */
414 BSON_EXPORT (uint32_t)
415 bson_count_keys (const bson_t *bson);
416 
417 
418 /**
419  * bson_has_field:
420  * @bson: A bson_t.
421  * @key: The key to lookup.
422  *
423  * Checks to see if @bson contains a field named @key.
424  *
425  * This function is case-sensitive.
426  *
427  * Returns: true if @key exists in @bson; otherwise false.
428  */
429 BSON_EXPORT (bool)
430 bson_has_field (const bson_t *bson, const char *key);
431 
432 
433 /**
434  * bson_compare:
435  * @bson: A bson_t.
436  * @other: A bson_t.
437  *
438  * Compares @bson to @other in a qsort() style comparison.
439  * See qsort() for information on how this function works.
440  *
441  * Returns: Less than zero, zero, or greater than zero.
442  */
443 BSON_EXPORT (int)
444 bson_compare (const bson_t *bson, const bson_t *other);
445 
446 /*
447  * bson_compare:
448  * @bson: A bson_t.
449  * @other: A bson_t.
450  *
451  * Checks to see if @bson and @other are equal.
452  *
453  * Returns: true if equal; otherwise false.
454  */
455 BSON_EXPORT (bool)
456 bson_equal (const bson_t *bson, const bson_t *other);
457 
458 
459 /**
460  * bson_validate:
461  * @bson: A bson_t.
462  * @offset: A location for the error offset.
463  *
464  * Validates a BSON document by walking through the document and inspecting
465  * the fields for valid content.
466  *
467  * Returns: true if @bson is valid; otherwise false and @offset is set.
468  */
469 BSON_EXPORT (bool)
470 bson_validate (const bson_t *bson, bson_validate_flags_t flags, size_t *offset);
471 
472 
473 /**
474  * bson_validate_with_error:
475  * @bson: A bson_t.
476  * @error: A location for the error info.
477  *
478  * Validates a BSON document by walking through the document and inspecting
479  * the fields for valid content.
480  *
481  * Returns: true if @bson is valid; otherwise false and @error is filled out.
482  */
483 BSON_EXPORT (bool)
484 bson_validate_with_error (const bson_t *bson,
485                           bson_validate_flags_t flags,
486                           bson_error_t *error);
487 
488 
489 /**
490  * bson_as_canonical_extended_json:
491  * @bson: A bson_t.
492  * @length: A location for the string length, or NULL.
493  *
494  * Creates a new string containing @bson in canonical extended JSON format,
495  * conforming to the MongoDB Extended JSON Spec:
496  *
497  * github.com/mongodb/specifications/blob/master/source/extended-json.rst
498  *
499  * The caller is responsible for freeing the resulting string. If @length is
500  * non-NULL, then the length of the resulting string will be placed in @length.
501  *
502  * See http://docs.mongodb.org/manual/reference/mongodb-extended-json/ for
503  * more information on extended JSON.
504  *
505  * Returns: A newly allocated string that should be freed with bson_free().
506  */
507 BSON_EXPORT (char *)
508 bson_as_canonical_extended_json (const bson_t *bson, size_t *length);
509 
510 
511 /**
512  * bson_as_json:
513  * @bson: A bson_t.
514  * @length: A location for the string length, or NULL.
515  *
516  * Creates a new string containing @bson in libbson's legacy JSON format.
517  * Superseded by bson_as_canonical_extended_json and
518  * bson_as_relaxed_extended_json. The caller is
519  * responsible for freeing the resulting string. If @length is non-NULL, then
520  * the length of the resulting string will be placed in @length.
521  *
522  * Returns: A newly allocated string that should be freed with bson_free().
523  */
524 BSON_EXPORT (char *)
525 bson_as_json (const bson_t *bson, size_t *length);
526 
527 
528 /**
529  * bson_as_relaxed_extended_json:
530  * @bson: A bson_t.
531  * @length: A location for the string length, or NULL.
532  *
533  * Creates a new string containing @bson in relaxed extended JSON format,
534  * conforming to the MongoDB Extended JSON Spec:
535  *
536  * github.com/mongodb/specifications/blob/master/source/extended-json.rst
537  *
538  * The caller is responsible for freeing the resulting string. If @length is
539  * non-NULL, then the length of the resulting string will be placed in @length.
540  *
541  * See http://docs.mongodb.org/manual/reference/mongodb-extended-json/ for
542  * more information on extended JSON.
543  *
544  * Returns: A newly allocated string that should be freed with bson_free().
545  */
546 BSON_EXPORT (char *)
547 bson_as_relaxed_extended_json (const bson_t *bson, size_t *length);
548 
549 
550 /* like bson_as_json() but for outermost arrays. */
551 BSON_EXPORT (char *)
552 bson_array_as_json (const bson_t *bson, size_t *length);
553 
554 
555 BSON_EXPORT (bool)
556 bson_append_value (bson_t *bson,
557                    const char *key,
558                    int key_length,
559                    const bson_value_t *value);
560 
561 
562 /**
563  * bson_append_array:
564  * @bson: A bson_t.
565  * @key: The key for the field.
566  * @array: A bson_t containing the array.
567  *
568  * Appends a BSON array to @bson. BSON arrays are like documents where the
569  * key is the string version of the index. For example, the first item of the
570  * array would have the key "0". The second item would have the index "1".
571  *
572  * Returns: true if successful; false if append would overflow max size.
573  */
574 BSON_EXPORT (bool)
575 bson_append_array (bson_t *bson,
576                    const char *key,
577                    int key_length,
578                    const bson_t *array);
579 
580 
581 /**
582  * bson_append_binary:
583  * @bson: A bson_t to append.
584  * @key: The key for the field.
585  * @subtype: The bson_subtype_t of the binary.
586  * @binary: The binary buffer to append.
587  * @length: The length of @binary.
588  *
589  * Appends a binary buffer to the BSON document.
590  *
591  * Returns: true if successful; false if append would overflow max size.
592  */
593 BSON_EXPORT (bool)
594 bson_append_binary (bson_t *bson,
595                     const char *key,
596                     int key_length,
597                     bson_subtype_t subtype,
598                     const uint8_t *binary,
599                     uint32_t length);
600 
601 
602 /**
603  * bson_append_bool:
604  * @bson: A bson_t.
605  * @key: The key for the field.
606  * @value: The boolean value.
607  *
608  * Appends a new field to @bson of type BSON_TYPE_BOOL.
609  *
610  * Returns: true if successful; false if append would overflow max size.
611  */
612 BSON_EXPORT (bool)
613 bson_append_bool (bson_t *bson, const char *key, int key_length, bool value);
614 
615 
616 /**
617  * bson_append_code:
618  * @bson: A bson_t.
619  * @key: The key for the document.
620  * @javascript: JavaScript code to be executed.
621  *
622  * Appends a field of type BSON_TYPE_CODE to the BSON document. @javascript
623  * should contain a script in javascript to be executed.
624  *
625  * Returns: true if successful; false if append would overflow max size.
626  */
627 BSON_EXPORT (bool)
628 bson_append_code (bson_t *bson,
629                   const char *key,
630                   int key_length,
631                   const char *javascript);
632 
633 
634 /**
635  * bson_append_code_with_scope:
636  * @bson: A bson_t.
637  * @key: The key for the document.
638  * @javascript: JavaScript code to be executed.
639  * @scope: A bson_t containing the scope for @javascript.
640  *
641  * Appends a field of type BSON_TYPE_CODEWSCOPE to the BSON document.
642  * @javascript should contain a script in javascript to be executed.
643  *
644  * Returns: true if successful; false if append would overflow max size.
645  */
646 BSON_EXPORT (bool)
647 bson_append_code_with_scope (bson_t *bson,
648                              const char *key,
649                              int key_length,
650                              const char *javascript,
651                              const bson_t *scope);
652 
653 
654 /**
655  * bson_append_dbpointer:
656  * @bson: A bson_t.
657  * @key: The key for the field.
658  * @collection: The collection name.
659  * @oid: The oid to the reference.
660  *
661  * Appends a new field of type BSON_TYPE_DBPOINTER. This datum type is
662  * deprecated in the BSON spec and should not be used in new code.
663  *
664  * Returns: true if successful; false if append would overflow max size.
665  */
666 BSON_EXPORT (bool)
667 bson_append_dbpointer (bson_t *bson,
668                        const char *key,
669                        int key_length,
670                        const char *collection,
671                        const bson_oid_t *oid);
672 
673 
674 /**
675  * bson_append_double:
676  * @bson: A bson_t.
677  * @key: The key for the field.
678  *
679  * Appends a new field to @bson of the type BSON_TYPE_DOUBLE.
680  *
681  * Returns: true if successful; false if append would overflow max size.
682  */
683 BSON_EXPORT (bool)
684 bson_append_double (bson_t *bson,
685                     const char *key,
686                     int key_length,
687                     double value);
688 
689 
690 /**
691  * bson_append_document:
692  * @bson: A bson_t.
693  * @key: The key for the field.
694  * @value: A bson_t containing the subdocument.
695  *
696  * Appends a new field to @bson of the type BSON_TYPE_DOCUMENT.
697  * The documents contents will be copied into @bson.
698  *
699  * Returns: true if successful; false if append would overflow max size.
700  */
701 BSON_EXPORT (bool)
702 bson_append_document (bson_t *bson,
703                       const char *key,
704                       int key_length,
705                       const bson_t *value);
706 
707 
708 /**
709  * bson_append_document_begin:
710  * @bson: A bson_t.
711  * @key: The key for the field.
712  * @key_length: The length of @key in bytes not including NUL or -1
713  *    if @key_length is NUL terminated.
714  * @child: A location to an uninitialized bson_t.
715  *
716  * Appends a new field named @key to @bson. The field is, however,
717  * incomplete.  @child will be initialized so that you may add fields to the
718  * child document.  Child will use a memory buffer owned by @bson and
719  * therefore grow the parent buffer as additional space is used. This allows
720  * a single malloc'd buffer to be used when building documents which can help
721  * reduce memory fragmentation.
722  *
723  * Returns: true if successful; false if append would overflow max size.
724  */
725 BSON_EXPORT (bool)
726 bson_append_document_begin (bson_t *bson,
727                             const char *key,
728                             int key_length,
729                             bson_t *child);
730 
731 
732 /**
733  * bson_append_document_end:
734  * @bson: A bson_t.
735  * @child: A bson_t supplied to bson_append_document_begin().
736  *
737  * Finishes the appending of a document to a @bson. @child is considered
738  * disposed after this call and should not be used any further.
739  *
740  * Returns: true if successful; false if append would overflow max size.
741  */
742 BSON_EXPORT (bool)
743 bson_append_document_end (bson_t *bson, bson_t *child);
744 
745 
746 /**
747  * bson_append_array_begin:
748  * @bson: A bson_t.
749  * @key: The key for the field.
750  * @key_length: The length of @key in bytes not including NUL or -1
751  *    if @key_length is NUL terminated.
752  * @child: A location to an uninitialized bson_t.
753  *
754  * Appends a new field named @key to @bson. The field is, however,
755  * incomplete. @child will be initialized so that you may add fields to the
756  * child array. Child will use a memory buffer owned by @bson and
757  * therefore grow the parent buffer as additional space is used. This allows
758  * a single malloc'd buffer to be used when building arrays which can help
759  * reduce memory fragmentation.
760  *
761  * The type of @child will be BSON_TYPE_ARRAY and therefore the keys inside
762  * of it MUST be "0", "1", etc.
763  *
764  * Returns: true if successful; false if append would overflow max size.
765  */
766 BSON_EXPORT (bool)
767 bson_append_array_begin (bson_t *bson,
768                          const char *key,
769                          int key_length,
770                          bson_t *child);
771 
772 
773 /**
774  * bson_append_array_end:
775  * @bson: A bson_t.
776  * @child: A bson_t supplied to bson_append_array_begin().
777  *
778  * Finishes the appending of a array to a @bson. @child is considered
779  * disposed after this call and should not be used any further.
780  *
781  * Returns: true if successful; false if append would overflow max size.
782  */
783 BSON_EXPORT (bool)
784 bson_append_array_end (bson_t *bson, bson_t *child);
785 
786 
787 /**
788  * bson_append_int32:
789  * @bson: A bson_t.
790  * @key: The key for the field.
791  * @value: The int32_t 32-bit integer value.
792  *
793  * Appends a new field of type BSON_TYPE_INT32 to @bson.
794  *
795  * Returns: true if successful; false if append would overflow max size.
796  */
797 BSON_EXPORT (bool)
798 bson_append_int32 (bson_t *bson,
799                    const char *key,
800                    int key_length,
801                    int32_t value);
802 
803 
804 /**
805  * bson_append_int64:
806  * @bson: A bson_t.
807  * @key: The key for the field.
808  * @value: The int64_t 64-bit integer value.
809  *
810  * Appends a new field of type BSON_TYPE_INT64 to @bson.
811  *
812  * Returns: true if successful; false if append would overflow max size.
813  */
814 BSON_EXPORT (bool)
815 bson_append_int64 (bson_t *bson,
816                    const char *key,
817                    int key_length,
818                    int64_t value);
819 
820 
821 /**
822  * bson_append_decimal128:
823  * @bson: A bson_t.
824  * @key: The key for the field.
825  * @value: The bson_decimal128_t decimal128 value.
826  *
827  * Appends a new field of type BSON_TYPE_DECIMAL128 to @bson.
828  *
829  * Returns: true if successful; false if append would overflow max size.
830  */
831 BSON_EXPORT (bool)
832 bson_append_decimal128 (bson_t *bson,
833                         const char *key,
834                         int key_length,
835                         const bson_decimal128_t *value);
836 
837 
838 /**
839  * bson_append_iter:
840  * @bson: A bson_t to append to.
841  * @key: The key name or %NULL to take current key from @iter.
842  * @key_length: The key length or -1 to use strlen().
843  * @iter: The iter located on the position of the element to append.
844  *
845  * Appends a new field to @bson that is equivalent to the field currently
846  * pointed to by @iter.
847  *
848  * Returns: true if successful; false if append would overflow max size.
849  */
850 BSON_EXPORT (bool)
851 bson_append_iter (bson_t *bson,
852                   const char *key,
853                   int key_length,
854                   const bson_iter_t *iter);
855 
856 
857 /**
858  * bson_append_minkey:
859  * @bson: A bson_t.
860  * @key: The key for the field.
861  *
862  * Appends a new field of type BSON_TYPE_MINKEY to @bson. This is a special
863  * type that compares lower than all other possible BSON element values.
864  *
865  * See http://bsonspec.org for more information on this type.
866  *
867  * Returns: true if successful; false if append would overflow max size.
868  */
869 BSON_EXPORT (bool)
870 bson_append_minkey (bson_t *bson, const char *key, int key_length);
871 
872 
873 /**
874  * bson_append_maxkey:
875  * @bson: A bson_t.
876  * @key: The key for the field.
877  *
878  * Appends a new field of type BSON_TYPE_MAXKEY to @bson. This is a special
879  * type that compares higher than all other possible BSON element values.
880  *
881  * See http://bsonspec.org for more information on this type.
882  *
883  * Returns: true if successful; false if append would overflow max size.
884  */
885 BSON_EXPORT (bool)
886 bson_append_maxkey (bson_t *bson, const char *key, int key_length);
887 
888 
889 /**
890  * bson_append_null:
891  * @bson: A bson_t.
892  * @key: The key for the field.
893  *
894  * Appends a new field to @bson with NULL for the value.
895  *
896  * Returns: true if successful; false if append would overflow max size.
897  */
898 BSON_EXPORT (bool)
899 bson_append_null (bson_t *bson, const char *key, int key_length);
900 
901 
902 /**
903  * bson_append_oid:
904  * @bson: A bson_t.
905  * @key: The key for the field.
906  * @oid: bson_oid_t.
907  *
908  * Appends a new field to the @bson of type BSON_TYPE_OID using the contents of
909  * @oid.
910  *
911  * Returns: true if successful; false if append would overflow max size.
912  */
913 BSON_EXPORT (bool)
914 bson_append_oid (bson_t *bson,
915                  const char *key,
916                  int key_length,
917                  const bson_oid_t *oid);
918 
919 
920 /**
921  * bson_append_regex:
922  * @bson: A bson_t.
923  * @key: The key of the field.
924  * @regex: The regex to append to the bson.
925  * @options: Options for @regex.
926  *
927  * Appends a new field to @bson of type BSON_TYPE_REGEX. @regex should
928  * be the regex string. @options should contain the options for the regex.
929  *
930  * Valid options for @options are:
931  *
932  *   'i' for case-insensitive.
933  *   'm' for multiple matching.
934  *   'x' for verbose mode.
935  *   'l' to make \w and \W locale dependent.
936  *   's' for dotall mode ('.' matches everything)
937  *   'u' to make \w and \W match unicode.
938  *
939  * For more information on what comprimises a BSON regex, see bsonspec.org.
940  *
941  * Returns: true if successful; false if append would overflow max size.
942  */
943 BSON_EXPORT (bool)
944 bson_append_regex (bson_t *bson,
945                    const char *key,
946                    int key_length,
947                    const char *regex,
948                    const char *options);
949 
950 
951 /**
952  * bson_append_utf8:
953  * @bson: A bson_t.
954  * @key: The key for the field.
955  * @value: A UTF-8 encoded string.
956  * @length: The length of @value or -1 if it is NUL terminated.
957  *
958  * Appends a new field to @bson using @key as the key and @value as the UTF-8
959  * encoded value.
960  *
961  * It is the callers responsibility to ensure @value is valid UTF-8. You can
962  * use bson_utf8_validate() to perform this check.
963  *
964  * Returns: true if successful; false if append would overflow max size.
965  */
966 BSON_EXPORT (bool)
967 bson_append_utf8 (bson_t *bson,
968                   const char *key,
969                   int key_length,
970                   const char *value,
971                   int length);
972 
973 
974 /**
975  * bson_append_symbol:
976  * @bson: A bson_t.
977  * @key: The key for the field.
978  * @value: The symbol as a string.
979  * @length: The length of @value or -1 if NUL-terminated.
980  *
981  * Appends a new field to @bson of type BSON_TYPE_SYMBOL. This BSON type is
982  * deprecated and should not be used in new code.
983  *
984  * See http://bsonspec.org for more information on this type.
985  *
986  * Returns: true if successful; false if append would overflow max size.
987  */
988 BSON_EXPORT (bool)
989 bson_append_symbol (bson_t *bson,
990                     const char *key,
991                     int key_length,
992                     const char *value,
993                     int length);
994 
995 
996 /**
997  * bson_append_time_t:
998  * @bson: A bson_t.
999  * @key: The key for the field.
1000  * @value: A time_t.
1001  *
1002  * Appends a BSON_TYPE_DATE_TIME field to @bson using the time_t @value for the
1003  * number of seconds since UNIX epoch in UTC.
1004  *
1005  * Returns: true if successful; false if append would overflow max size.
1006  */
1007 BSON_EXPORT (bool)
1008 bson_append_time_t (bson_t *bson,
1009                     const char *key,
1010                     int key_length,
1011                     time_t value);
1012 
1013 
1014 /**
1015  * bson_append_timeval:
1016  * @bson: A bson_t.
1017  * @key: The key for the field.
1018  * @value: A struct timeval containing the date and time.
1019  *
1020  * Appends a BSON_TYPE_DATE_TIME field to @bson using the struct timeval
1021  * provided. The time is persisted in milliseconds since the UNIX epoch in UTC.
1022  *
1023  * Returns: true if successful; false if append would overflow max size.
1024  */
1025 BSON_EXPORT (bool)
1026 bson_append_timeval (bson_t *bson,
1027                      const char *key,
1028                      int key_length,
1029                      struct timeval *value);
1030 
1031 
1032 /**
1033  * bson_append_date_time:
1034  * @bson: A bson_t.
1035  * @key: The key for the field.
1036  * @key_length: The length of @key in bytes or -1 if \0 terminated.
1037  * @value: The number of milliseconds elapsed since UNIX epoch.
1038  *
1039  * Appends a new field to @bson of type BSON_TYPE_DATE_TIME.
1040  *
1041  * Returns: true if sucessful; otherwise false.
1042  */
1043 BSON_EXPORT (bool)
1044 bson_append_date_time (bson_t *bson,
1045                        const char *key,
1046                        int key_length,
1047                        int64_t value);
1048 
1049 
1050 /**
1051  * bson_append_now_utc:
1052  * @bson: A bson_t.
1053  * @key: The key for the field.
1054  * @key_length: The length of @key or -1 if it is NULL terminated.
1055  *
1056  * Appends a BSON_TYPE_DATE_TIME field to @bson using the current time in UTC
1057  * as the field value.
1058  *
1059  * Returns: true if successful; false if append would overflow max size.
1060  */
1061 BSON_EXPORT (bool)
1062 bson_append_now_utc (bson_t *bson, const char *key, int key_length);
1063 
1064 /**
1065  * bson_append_timestamp:
1066  * @bson: A bson_t.
1067  * @key: The key for the field.
1068  * @timestamp: 4 byte timestamp.
1069  * @increment: 4 byte increment for timestamp.
1070  *
1071  * Appends a field of type BSON_TYPE_TIMESTAMP to @bson. This is a special type
1072  * used by MongoDB replication and sharding. If you need generic time and date
1073  * fields use bson_append_time_t() or bson_append_timeval().
1074  *
1075  * Setting @increment and @timestamp to zero has special semantics. See
1076  * http://bsonspec.org for more information on this field type.
1077  *
1078  * Returns: true if successful; false if append would overflow max size.
1079  */
1080 BSON_EXPORT (bool)
1081 bson_append_timestamp (bson_t *bson,
1082                        const char *key,
1083                        int key_length,
1084                        uint32_t timestamp,
1085                        uint32_t increment);
1086 
1087 
1088 /**
1089  * bson_append_undefined:
1090  * @bson: A bson_t.
1091  * @key: The key for the field.
1092  *
1093  * Appends a field of type BSON_TYPE_UNDEFINED. This type is deprecated in the
1094  * spec and should not be used for new code. However, it is provided for those
1095  * needing to interact with legacy systems.
1096  *
1097  * Returns: true if successful; false if append would overflow max size.
1098  */
1099 BSON_EXPORT (bool)
1100 bson_append_undefined (bson_t *bson, const char *key, int key_length);
1101 
1102 
1103 BSON_EXPORT (bool)
1104 bson_concat (bson_t *dst, const bson_t *src);
1105 
1106 
1107 BSON_END_DECLS
1108 
1109 
1110 #endif /* BSON_H */
1111