1 /* bson.h - libmongo-client's BSON implementation
2  * Copyright 2011, 2012 Gergely Nagy <algernon@balabit.hu>
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 /** @file src/bson.h
18  * The BSON API's public header.
19  */
20 
21 #ifndef LIBMONGO_CLIENT_BSON_H
22 #define LIBMONGO_CLIENT_BSON_H 1
23 
24 #include <glib.h>
25 #include <string.h>
26 
27 G_BEGIN_DECLS
28 
29 /** @defgroup bson_mod BSON
30  *
31  * The types, functions and everything else within this module is
32  * meant to allow one to work with BSON objects easily.
33  *
34  * @addtogroup bson_mod
35  * @{
36  */
37 
38 /** @defgroup bson_types Types
39  *
40  * @addtogroup bson_types
41  * @{
42  */
43 
44 /** An opaque BSON object.
45  * A BSON object represents a full BSON document, as specified at
46  * http://bsonspec.org/.
47  *
48  * Each object has two states: open and finished. While the document
49  * is open, it can be appended to, but it cannot be read from. While
50  * it is finished, it can be read from, and iterated over, but cannot
51  * be appended to.
52  */
53 typedef struct _bson bson;
54 
55 /** Opaque BSON cursor.
56  * Cursors are used to represent a single entry within a BSON object,
57  * and to help iterating over said document.
58  */
59 typedef struct _bson_cursor bson_cursor;
60 
61 /** Supported BSON object types.
62  */
63 typedef enum
64   {
65     BSON_TYPE_NONE = 0, /**< Only used for errors */
66     BSON_TYPE_DOUBLE = 0x01, /**< 8byte double */
67     BSON_TYPE_STRING, /**< 4byte length + NULL terminated string */
68     BSON_TYPE_DOCUMENT, /**< 4byte length + NULL terminated document */
69     BSON_TYPE_ARRAY, /**< 4byte length + NULL terminated document */
70     BSON_TYPE_BINARY, /**< 4byte length + 1byte subtype + data */
71     BSON_TYPE_UNDEFINED, /* Deprecated*/
72     BSON_TYPE_OID, /**< 12byte ObjectID */
73     BSON_TYPE_BOOLEAN, /**< 1byte boolean value */
74     BSON_TYPE_UTC_DATETIME, /**< 8byte timestamp; milliseconds since
75                                Unix epoch */
76     BSON_TYPE_NULL, /**< NULL value, No following data. */
77     BSON_TYPE_REGEXP, /**< Two NULL terminated C strings, the regex
78                          itself, and the options. */
79     BSON_TYPE_DBPOINTER, /* Deprecated */
80     BSON_TYPE_JS_CODE, /**< 4byte length + NULL terminated string */
81     BSON_TYPE_SYMBOL, /**< 4byte length + NULL terminated string */
82     BSON_TYPE_JS_CODE_W_SCOPE, /**< 4byte length, followed by a
83                                   string and a document */
84     BSON_TYPE_INT32, /**< 4byte integer */
85     BSON_TYPE_TIMESTAMP, /**< 4bytes increment + 4bytes timestamp */
86     BSON_TYPE_INT64, /**< 8byte integer */
87     BSON_TYPE_MIN = 0xff,
88     BSON_TYPE_MAX = 0x7f
89   } bson_type;
90 
91 /** Return a type's stringified name.
92  *
93  * @param type is the type to stringify.
94  *
95  * @returns The stringified type, or NULL on error.
96  */
97 const gchar *bson_type_as_string (bson_type type);
98 
99 /** Supported BSON binary subtypes.
100  */
101 typedef enum
102   {
103     BSON_BINARY_SUBTYPE_GENERIC = 0x00, /**< The Generic subtype, the
104                                            default. */
105     BSON_BINARY_SUBTYPE_FUNCTION = 0x01, /**< Binary representation
106                                             of a function. */
107     BSON_BINARY_SUBTYPE_BINARY = 0x02, /**< Obsolete, do not use. */
108     BSON_BINARY_SUBTYPE_UUID = 0x03, /**< Binary representation of an
109                                         UUID. */
110     BSON_BINARY_SUBTYPE_MD5 = 0x05, /**< Binary representation of an
111                                        MD5 sum. */
112     BSON_BINARY_SUBTYPE_USER_DEFINED = 0x80 /**< User defined data,
113                                                nothing's known about
114                                                the structure. */
115   } bson_binary_subtype;
116 
117 /** @} */
118 
119 /** @defgroup bson_object_access Object Access
120  *
121  * Functions that operate on whole BSON objects.
122  *
123  * @addtogroup bson_object_access
124  * @{
125  */
126 
127 /** Create a new BSON object.
128  *
129  * @note The created object will have no memory pre-allocated for data,
130  * resulting in possibly more reallocations than neccessary when
131  * appending elements.
132  *
133  * @note If at all possible, use bson_new_sized() instead.
134  *
135  * @returns A newly allocated object, or NULL on error.
136  */
137 bson *bson_new (void);
138 
139 /** Create a new BSON object, preallocating a given amount of space.
140  *
141  * Creates a new BSON object, pre-allocating @a size bytes of space
142  * for the data.
143  *
144  * @param size is the space to pre-allocate for data.
145  *
146  * @note It is not an error to pre-allocate either less, or more space
147  * than what will really end up being added. Pre-allocation does not
148  * set the size of the final object, it is merely a hint, a way to
149  * help the system avoid memory reallocations.
150  *
151  * @returns A newly allocated object, or NULL on error.
152  */
153 bson *bson_new_sized (gint32 size);
154 
155 /** Create a BSON object from existing data.
156  *
157  * In order to be able to parse existing BSON, one must load it up
158  * into a bson object - and this function does just that.
159  *
160  * @note Objects created by this function are not final objects, in
161  * order to be able to extend them. As such, when importing existing
162  * BSON data, which are terminated by a zero byte, specify the size as
163  * one smaller than the original data stream.
164  *
165  * @note This is because bson_finish() will append a zero byte, thus
166  * one would end up with an invalid document if it had an extra one.
167  *
168  * @param data is the BSON byte stream to import.
169  * @param size is the size of the byte stream.
170  *
171  * @returns A newly allocated object, with a copy of @a data as its
172  * contents.
173  */
174 bson *bson_new_from_data (const guint8 *data, gint32 size);
175 
176 /** Build a BSON object in one go, with full control.
177  *
178  * This function can be used to build a BSON object in one simple
179  * step, chaining all the elements together (including sub-documents,
180  * created by this same function - more about that later).
181  *
182  * One has to specify the type, the key name, and whether he wants to
183  * see the added object free'd after addition. Each element type is
184  * freed appropriately, and documents and arrays are finished before
185  * addition, if they're to be freed afterwards.
186  *
187  * This way of operation allows one to build a full BSON object, even
188  * with embedded documents, without leaking memory.
189  *
190  * After the three required parameters, one will need to list the data
191  * itself, in the same order as one would if he'd add with the
192  * bson_append family of functions.
193  *
194  * The list must be closed with a #BSON_TYPE_NONE element, and the @a
195  * name and @a free_after parameters are not needed for the closing
196  * entry.
197  *
198  * @param type is the element type we'll be adding.
199  * @param name is the key name.
200  * @param free_after determines whether the original variable will be
201  * freed after adding it to the BSON object.
202  *
203  * @returns A newly allocated, unfinished BSON object, which must be
204  * finalized and freed, once not needed anymore, by the caller. Or
205  * NULL on error.
206  */
207 bson *bson_build_full (bson_type type, const gchar *name,
208                        gboolean free_after, ...);
209 
210 /** Build a BSON object in one go.
211  *
212  * Very similar to bson_build_full(), so much so, that it's exactly
213  * the same, except that the @a free_after parameter is always FALSE,
214  * and must not be specified in this case.
215  *
216  * @param type is the element type we'll be adding.
217  * @param name is the key name.
218  *
219  * @returns A newly allocated, unfinished BSON object, which must be
220  * finalized and freed, once not needed anymore, by the caller. Or
221  * NULL on error.
222  */
223 bson *bson_build (bson_type type, const gchar *name, ...);
224 
225 /** Finish a BSON object.
226  *
227  * Terminate a BSON object. This includes appending the trailing zero
228  * byte and finalising the length of the object.
229  *
230  * The object cannot be appended to after it is finalised.
231  *
232  * @param b is the BSON object to close & finish.
233  *
234  * @returns TRUE on success, FALSE otherwise.
235  */
236 gboolean bson_finish (bson *b);
237 
238 /** Reset a BSON object.
239  *
240  * Resetting a BSON object clears the finished status, and sets its
241  * size to zero. Resetting is most useful when wants to keep the
242  * already allocated memory around for reuse.
243  *
244  * @param b is the BSON object to reset.
245  *
246  * @returns TRUE on success, FALSE otherwise.
247  */
248 gboolean bson_reset (bson *b);
249 
250 /** Free the memory associated with a BSON object.
251  *
252  * Frees up all memory associated with a BSON object. The variable
253  * shall not be used afterwards.
254  *
255  * @param b is the BSON object to free.
256  */
257 void bson_free (bson *b);
258 
259 /** Return the size of a finished BSON object.
260  *
261  * @param b is the finished BSON object.
262  *
263  * @returns The size of the document, or -1 on error.
264  *
265  * @note Trying to get the size of a BSON object that has not been
266  * closed by bson_finish() is considered an error.
267  */
268 gint32 bson_size (const bson *b);
269 
270 /** Return the raw bytestream form of the BSON object.
271  *
272  * @param b is the BSON object to retrieve data from.
273  *
274  * @returns The raw datastream or NULL on error. The stream s all not
275  * be freed.
276  *
277  * @note Trying to retrieve the data of an unfinished BSON object is
278  * considered an error.
279  */
280 const guint8 *bson_data (const bson *b);
281 
282 /** Validate a BSON key.
283  *
284  * Verifies that a given key is a valid BSON field name. Depending on
285  * context (togglable by the boolean flags) this means that the string
286  * must either be free of dots, or must not start with a dollar sign.
287  *
288  * @param key is the field name to validate.
289  * @param forbid_dots toggles whether to disallow dots in the name
290  * altogether.
291  * @param no_dollar toggles whether to forbid key names starting with
292  * a dollar sign.
293  *
294  * @returns TRUE if the field name is found to be valid, FALSE
295  * otherwise.
296  *
297  * @note This function does NOT do UTF-8 validation. That is left up
298  * to the application.
299  */
300 gboolean bson_validate_key (const gchar *key, gboolean forbid_dots,
301                             gboolean no_dollar);
302 
303 /** Reads out the 32-bit documents size from a BSON bytestream.
304  *
305  * This function can be used when reading data from a stream, and one
306  * wants to build a BSON object from the bytestream: for
307  * bson_new_from_data(), one needs the length. This function provides
308  * that.
309  *
310  * @param doc is the byte stream to check the size of.
311  * @param pos is the position in the bytestream to start reading at.
312  *
313  * @returns The size of the document at the appropriate position.
314  *
315  * @note The byte stream is expected to be in little-endian byte
316  * order.
317  */
bson_stream_doc_size(const guint8 * doc,gint32 pos)318 static __inline__ gint32 bson_stream_doc_size (const guint8 *doc, gint32 pos)
319 {
320   gint32 size;
321 
322   memcpy (&size, doc + pos, sizeof (gint32));
323   return GINT32_FROM_LE (size);
324 }
325 
326 /** @} */
327 
328 /** @defgroup bson_append Appending
329  *
330  * @brief Functions to append various kinds of elements to existing
331  * BSON objects.
332  *
333  * Every such function expects the BSON object to be open, and will
334  * return FALSE immediately if it finds that the object has had
335  * bson_finish() called on it before.
336  *
337  * The only way to append to a finished BSON object is to @a clone it
338  * with bson_new_from_data(), and append to the newly created object.
339  *
340  * @addtogroup bson_append
341  * @{
342  */
343 
344 /** Append a string to a BSON object.
345  *
346  * @param b is the BSON object to append to.
347  * @param name is the key name.
348  * @param val is the value to append.
349  * @param length is the length of value. Use @a -1 to use the full
350  * string supplied as @a name.
351  *
352  * @returns TRUE on success, FALSE otherwise.
353  */
354 gboolean bson_append_string (bson *b, const gchar *name, const gchar *val,
355                              gint32 length);
356 
357 /** Append a double to a BSON object.
358  *
359  * @param b is the BSON object to append to.
360  * @param name is the key name.
361  * @param d is the double value to append.
362  *
363  * @returns TRUE on success, FALSE otherwise.
364  */
365 gboolean bson_append_double (bson *b, const gchar *name, gdouble d);
366 
367 /** Append a BSON document to a BSON object.
368  *
369  * @param b is the BSON object to append to.
370  * @param name is the key name.
371  * @param doc is the BSON document to append.
372  *
373  * @note @a doc MUST be a finished BSON document.
374  *
375  * @returns TRUE on success, FALSE otherwise.
376  */
377 gboolean bson_append_document (bson *b, const gchar *name, const bson *doc);
378 
379 /** Append a BSON array to a BSON object.
380  *
381  * @param b is the BSON object to append to.
382  * @param name is the key name.
383  * @param array is the BSON array to append.
384  *
385  * @note @a array MUST be a finished BSON document.
386  *
387  * @note The difference between plain documents and arrays - as far as
388  * this library is concerned, and apart from the type - is that array
389  * keys must be numbers in increasing order. However, no extra care is
390  * taken to verify that: it is the responsibility of the caller to set
391  * the array up appropriately.
392  *
393  * @returns TRUE on success, FALSE otherwise.
394  */
395 gboolean bson_append_array (bson *b, const gchar *name, const bson *array);
396 
397 /** Append a BSON binary blob to a BSON object.
398  *
399  * @param b is the BSON object to append to.
400  * @param name is the key name.
401  * @param subtype is the BSON binary subtype to use.
402  * @param data is a pointer to the blob data.
403  * @param size is the size of the blob.
404  *
405  * @returns TRUE on success, FALSE otherwise.
406  */
407 gboolean bson_append_binary (bson *b, const gchar *name,
408                              bson_binary_subtype subtype,
409                              const guint8 *data, gint32 size);
410 
411 /** Append an ObjectID to a BSON object.
412  *
413  * ObjectIDs are 12 byte values, the first four being a timestamp in
414  * big endian byte order, the next three a machine ID, then two bytes
415  * for the PID, and finally three bytes of sequence number, in big
416  * endian byte order again.
417  *
418  * @param b is the BSON object to append to.
419  * @param name is the key name.
420  * @param oid is the ObjectID to append.
421  *
422  * @note The OID must be 12 bytes long, and formatting it
423  * appropriately is the responsiblity of the caller.
424  *
425  * @returns TRUE on success, FALSE otherwise.
426  */
427 gboolean bson_append_oid (bson *b, const gchar *name, const guint8 *oid);
428 
429 /** Append a boolean to a BSON object.
430  *
431  * @param b is the BSON object to append to.
432  * @param name is the key name.
433  * @param value is the boolean value to append.
434  *
435  * @returns TRUE on success, FALSE otherwise.
436  */
437 gboolean bson_append_boolean (bson *b, const gchar *name, gboolean value);
438 
439 /** Append an UTC datetime to a BSON object.
440  *
441  * @param b is the BSON object to append to.
442  * @param name is the key name.
443  * @param ts is the UTC timestamp: the number of milliseconds since
444  * the Unix epoch.
445  *
446  * @returns TRUE on success, FALSE otherwise.
447  */
448 gboolean bson_append_utc_datetime (bson *b, const gchar *name, gint64 ts);
449 
450 /** Append a NULL value to a BSON object.
451  *
452  * @param b is the BSON object to append to.
453  * @param name is the key name.
454  *
455  * @returns TRUE on success, FALSE otherwise.
456  */
457 gboolean bson_append_null (bson *b, const gchar *name);
458 
459 /** Append a regexp object to a BSON object.
460  *
461  * @param b is the BSON object to append to.
462  * @param name is the key name.
463  * @param regexp is the regexp string itself.
464  * @param options represents the regexp options, serialised to a
465  * string.
466  *
467  * @returns TRUE on success, FALSE otherwise.
468  */
469 gboolean bson_append_regex (bson *b, const gchar *name, const gchar *regexp,
470                             const gchar *options);
471 
472 /** Append Javascript code to a BSON object.
473  *
474  * @param b is the BSON object to append to.
475  * @param name is the key name.
476  * @param js is the javascript code as a C string.
477  * @param len is the length of the code, use @a -1 to use the full
478  * length of the string supplised in @a js.
479  *
480  * @returns TRUE on success, FALSE otherwise.
481  */
482 gboolean bson_append_javascript (bson *b, const gchar *name, const gchar *js,
483                                  gint32 len);
484 
485 /** Append a symbol to a BSON object.
486  *
487  * @param b is the BSON object to append to.
488  * @param name is the key name.
489  * @param symbol is the symbol to append.
490  * @param len is the length of the code, use @a -1 to use the full
491  * length of the string supplised in @a symbol.
492  *
493  * @returns TRUE on success, FALSE otherwise.
494  */
495 gboolean bson_append_symbol (bson *b, const gchar *name, const gchar *symbol,
496                              gint32 len);
497 
498 /** Append Javascript code (with scope) to a BSON object.
499  *
500  * @param b is the BSON object to append to.
501  * @param name is the key name.
502  * @param js is the javascript code as a C string.
503  * @param len is the length of the code, use @a -1 to use the full
504  * length of the string supplied in @a js.
505  * @param scope is scope to evaluate the javascript code in.
506  *
507  * @returns TRUE on success, FALSE otherwise.
508  */
509 gboolean bson_append_javascript_w_scope (bson *b, const gchar *name,
510                                          const gchar *js, gint32 len,
511                                          const bson *scope);
512 
513 /** Append a 32-bit integer to a BSON object.
514  *
515  * @param b is the BSON object to append to.
516  * @param name is the key name.
517  * @param i is the integer to append.
518  *
519  * @returns TRUE on success, FALSE otherwise.
520  */
521 gboolean bson_append_int32 (bson *b, const gchar *name, gint32 i);
522 
523 /** Append a timestamp to a BSON object.
524  *
525  * @param b is the BSON object to append to.
526  * @param name is the key name.
527  * @param ts is the timestamp to append.
528  *
529  * @note The ts param should consists of 4 bytes of increment,
530  * followed by 4 bytes of timestamp. It is the responsibility of the
531  * caller to set the variable up appropriately.
532  *
533  * @returns TRUE on success, FALSE otherwise.
534  */
535 gboolean bson_append_timestamp (bson *b, const gchar *name, gint64 ts);
536 
537 /** Append a 64-bit integer to a BSON object.
538  *
539  * @param b is the BSON object to append to.
540  * @param name is the key name.
541  * @param i is the integer to append.
542  *
543  * @returns TRUE on success, FALSE otherwise.
544  */
545 gboolean bson_append_int64 (bson *b, const gchar *name, gint64 i);
546 
547 /** @} */
548 
549 /** @defgroup bson_cursor Cursor & Retrieval
550  *
551  * This section documents the cursors, and the data retrieval
552  * functions. Each and every function here operates on finished BSON
553  * objects, and will return with an error if passed an open object.
554  *
555  * Data can be retrieved from cursors, which in turn point to a
556  * specific part of the BSON object.
557  *
558  * The idea is to place the cursor to the appropriate key first, then
559  * retrieve the data stored there. Trying to retrieve data that is of
560  * different type than what the cursor is results in an error.
561  *
562  * Functions to iterate to the next key, and retrieve the current
563  * keys name are also provided.
564  *
565  * @addtogroup bson_cursor
566  * @{
567  */
568 
569 /** Create a new cursor.
570  *
571  * Creates a new cursor, and positions it to the beginning of the
572  * supplied BSON object.
573  *
574  * @param b is the BSON object to create a cursor for.
575  *
576  * @returns A newly allocated cursor, or NULL on error.
577  */
578 bson_cursor *bson_cursor_new (const bson *b);
579 
580 /** Create a new cursor positioned at a given key.
581  *
582  * Creates a new cursor, and positions it to the supplied key within
583  * the BSON object.
584  *
585  * @param b is the BSON object to create a cursor for.
586  * @param name is the key name to position to.
587  *
588  * @returns A newly allocated cursor, or NULL on error.
589  */
590 bson_cursor *bson_find (const bson *b, const gchar *name);
591 
592 /** Delete a cursor, and free up all resources used by it.
593  *
594  * @param c is the cursor to free.
595  */
596 void bson_cursor_free (bson_cursor *c);
597 
598 /** Position the cursor to the next key.
599  *
600  * @param c is the cursor to move forward.
601  *
602  * @returns TRUE on success, FALSE otherwise.
603  */
604 gboolean bson_cursor_next (bson_cursor *c);
605 
606 /** Move the cursor to a given key, past the current one.
607  *
608  * Scans the BSON object past the current key, in search for the
609  * specified one, and positions the cursor there if found, leaves it
610  * in place if not.
611  *
612  * @param c is the cursor to move forward.
613  * @param name is the key name to position to.
614  *
615  * @returns TRUE on success, FALSE otherwise.
616  */
617 gboolean bson_cursor_find_next (bson_cursor *c, const gchar *name);
618 
619 /** Move the cursor to a given key
620  *
621  * Like bson_cursor_find_next(), this function will start scanning the
622  * BSON object at the current position. If the key is not found after
623  * it, it will wrap over and search up to the original position.
624  *
625  * @param c is the cursor to move.
626  * @param name is the key name to position to.
627  *
628  * @returns TRUE on success, FALSE otherwise.
629  */
630 gboolean bson_cursor_find (bson_cursor *c, const gchar *name);
631 
632 /** Determine the type of the current element.
633  *
634  * @param c is the cursor pointing at the appropriate element.
635  *
636  * @returns The type of the element, or #BSON_TYPE_NONE on error.
637  */
638 bson_type bson_cursor_type (const bson_cursor *c);
639 
640 /** Retrieve the type of the current element, as string.
641  *
642  * @param c is the cursor pointing at the appropriate element.
643  *
644  * @returns The type of the element, as string, or NULL on error.
645  *
646  * @note The string points to an internal structure, it should not be
647  * freed or modified.
648  */
649 const gchar *bson_cursor_type_as_string (const bson_cursor *c);
650 
651 /** Determine the name of the current elements key.
652  *
653  * @param c is the cursor pointing at the appropriate element.
654  *
655  * @returns The name of the key, or NULL on error.
656  *
657  * @note The name is a pointer to an internal string, one must NOT
658  * free it.
659  */
660 const gchar *bson_cursor_key (const bson_cursor *c);
661 
662 /** Get the value stored at the cursor, as string.
663  *
664  * @param c is the cursor pointing at the appropriate element.
665  * @param dest is a pointer to a variable where the value can be
666  * stored.
667  *
668  * @note The @a dest pointer will be set to point to an internal
669  * structure, and must not be freed or modified by the caller.
670  *
671  * @returns TRUE on success, FALSE otherwise.
672  */
673 gboolean bson_cursor_get_string (const bson_cursor *c, const gchar **dest);
674 
675 /** Get the value stored at the cursor, as a double.
676  *
677  * @param c is the cursor pointing at the appropriate element.
678  * @param dest is a pointer to a variable where the value can be
679  * stored.
680  *
681  * @returns TRUE on success, FALSE otherwise.
682  */
683 gboolean bson_cursor_get_double (const bson_cursor *c, gdouble *dest);
684 
685 /** Get the value stored at the cursor, as a BSON document.
686  *
687  * @param c is the cursor pointing at the appropriate element.
688  * @param dest is a pointer to a variable where the value can be
689  * stored.
690  *
691  * @note The @a dest pointer will be a newly allocated, finished
692  * object: it is the responsibility of the caller to free it.
693  *
694  * @returns TRUE on success, FALSE otherwise.
695  */
696 gboolean bson_cursor_get_document (const bson_cursor *c, bson **dest);
697 
698 /** Get the value stored at the cursor, as a BSON array.
699  *
700  * @param c is the cursor pointing at the appropriate element.
701  * @param dest is a pointer to a variable where the value can be
702  * stored.
703  *
704  * @note The @a dest pointer will be a newly allocated, finished
705  * object: it is the responsibility of the caller to free it.
706  *
707  * @returns TRUE on success, FALSE otherwise.
708  */
709 gboolean bson_cursor_get_array (const bson_cursor *c, bson **dest);
710 
711 /** Get the value stored at the cursor, as binary data.
712  *
713  * @param c is the cursor pointing at the appropriate element.
714  * @param subtype is a pointer to store the binary subtype at.
715  * @param data is a pointer to where the data shall be stored.
716  * @param size is a pointer to store the size at.
717  *
718  * @note The @a data pointer will be pointing to an internal
719  * structure, it must not be freed or modified.
720  *
721  * @returns TRUE on success, FALSE otherwise.
722  */
723 gboolean bson_cursor_get_binary (const bson_cursor *c,
724                                  bson_binary_subtype *subtype,
725                                  const guint8 **data, gint32 *size);
726 
727 /** Get the value stored at the cursor, as an ObjectID.
728  *
729  * @param c is the cursor pointing at the appropriate element.
730  * @param dest is a pointer to a variable where the value can be
731  * stored.
732  *
733  * @note The @a dest pointer will be set to point to an internal
734  * structure, and must not be freed or modified by the caller.
735  *
736  * @returns TRUE on success, FALSE otherwise.
737  */
738 gboolean bson_cursor_get_oid (const bson_cursor *c, const guint8 **dest);
739 
740 /** Get the value stored at the cursor, as a boolean.
741  *
742  * @param c is the cursor pointing at the appropriate element.
743  * @param dest is a pointer to a variable where the value can be
744  * stored.
745  *
746  * @returns TRUE on success, FALSE otherwise.
747  */
748 gboolean bson_cursor_get_boolean (const bson_cursor *c, gboolean *dest);
749 
750 /** Get the value stored at the cursor, as an UTC datetime.
751  *
752  * @param c is the cursor pointing at the appropriate element.
753  * @param dest is a pointer to a variable where the value can be
754  * stored.
755  *
756  * @returns TRUE on success, FALSE otherwise.
757  */
758 gboolean bson_cursor_get_utc_datetime (const bson_cursor *c, gint64 *dest);
759 
760 /** Get the value stored at the cursor, as a regexp.
761  *
762  * @param c is the cursor pointing at the appropriate element.
763  * @param regex is a pointer to a variable where the regex can be
764  * stored.
765  * @param options is a pointer to a variable where the options can be
766  * stored.
767  *
768  * @note Both the @a regex and @a options pointers will be set to
769  * point to an internal structure, and must not be freed or modified
770  * by the caller.
771  *
772  * @returns TRUE on success, FALSE otherwise.
773  */
774 gboolean bson_cursor_get_regex (const bson_cursor *c, const gchar **regex,
775                                 const gchar **options);
776 
777 /** Get the value stored at the cursor, as javascript code.
778  *
779  * @param c is the cursor pointing at the appropriate element.
780  * @param dest is a pointer to a variable where the value can be
781  * stored.
782  *
783  * @note The @a dest pointer will be set to point to an internal
784  * structure, and must not be freed or modified by the caller.
785  *
786  * @returns TRUE on success, FALSE otherwise.
787  */
788 gboolean bson_cursor_get_javascript (const bson_cursor *c, const gchar **dest);
789 
790 /** Get the value stored at the cursor, as a symbol.
791  *
792  * @param c is the cursor pointing at the appropriate element.
793  * @param dest is a pointer to a variable where the value can be
794  * stored.
795  *
796  * @note The @a dest pointer will be set to point to an internal
797  * structure, and must not be freed or modified by the caller.
798  *
799  * @returns TRUE on success, FALSE otherwise.
800  */
801 gboolean bson_cursor_get_symbol (const bson_cursor *c, const gchar **dest);
802 
803 /** Get the value stored at the cursor, as javascript code w/ scope.
804  *
805  * @param c is the cursor pointing at the appropriate element.
806  * @param js is a pointer to a variable where the javascript code can
807  * be stored.
808  * @param scope is a pointer to a variable where the scope can be
809  * stored.
810  *
811  * @note The @a scope pointer will be a newly allocated, finished
812  * BSON object: it is the responsibility of the caller to free it.
813  *
814  * @returns TRUE on success, FALSE otherwise.
815  */
816 gboolean bson_cursor_get_javascript_w_scope (const bson_cursor *c,
817                                              const gchar **js,
818                                              bson **scope);
819 
820 /** Get the value stored at the cursor, as a 32-bit integer.
821  *
822  * @param c is the cursor pointing at the appropriate element.
823  * @param dest is a pointer to a variable where the value can be
824  * stored.
825  *
826  * @returns TRUE on success, FALSE otherwise.
827  */
828 gboolean bson_cursor_get_int32 (const bson_cursor *c, gint32 *dest);
829 
830 /** Get the value stored at the cursor, as a timestamp.
831  *
832  * @param c is the cursor pointing at the appropriate element.
833  * @param dest is a pointer to a variable where the value can be
834  * stored.
835  *
836  * @returns TRUE on success, FALSE otherwise.
837  */
838 gboolean bson_cursor_get_timestamp (const bson_cursor *c, gint64 *dest);
839 
840 /** Get the value stored at the cursor, as a 64-bit integer.
841  *
842  * @param c is the cursor pointing at the appropriate element.
843  * @param dest is a pointer to a variable where the value can be
844  * stored.
845  *
846  * @returns TRUE on success, FALSE otherwise.
847  */
848 gboolean bson_cursor_get_int64 (const bson_cursor *c, gint64 *dest);
849 
850 /** @} */
851 
852 /** @} */
853 
854 G_END_DECLS
855 
856 #endif
857