File Coverage

bson/bson-types.h
Criterion Covered Total %
statement 0 12 0.0
branch 0 4 0.0
condition n/a
subroutine n/a
pod n/a
total 0 16 0.0


line stmt bran cond sub pod time code
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_TYPES_H
19             #define BSON_TYPES_H
20              
21              
22             #if !defined (BSON_INSIDE) && !defined (BSON_COMPILATION)
23             #error "Only can be included directly."
24             #endif
25              
26              
27             #include
28             #include
29              
30             #include "bson-macros.h"
31             #include "bson-config.h"
32             #include "bson-compat.h"
33             #include "bson-endian.h"
34              
35             BSON_BEGIN_DECLS
36              
37              
38             /*
39             *--------------------------------------------------------------------------
40             *
41             * bson_unichar_t --
42             *
43             * bson_unichar_t provides an unsigned 32-bit type for containing
44             * unicode characters. When iterating UTF-8 sequences, this should
45             * be used to avoid losing the high-bits of non-ascii characters.
46             *
47             * See also:
48             * bson_string_append_unichar()
49             *
50             *--------------------------------------------------------------------------
51             */
52              
53             typedef uint32_t bson_unichar_t;
54              
55              
56             /**
57             * bson_context_flags_t:
58             *
59             * This enumeration is used to configure a bson_context_t.
60             *
61             * %BSON_CONTEXT_NONE: Use default options.
62             * %BSON_CONTEXT_THREAD_SAFE: Context will be called from multiple threads.
63             * %BSON_CONTEXT_DISABLE_PID_CACHE: Call getpid() instead of caching the
64             * result of getpid() when initializing the context.
65             * %BSON_CONTEXT_DISABLE_HOST_CACHE: Call gethostname() instead of caching the
66             * result of gethostname() when initializing the context.
67             */
68             typedef enum
69             {
70             BSON_CONTEXT_NONE = 0,
71             BSON_CONTEXT_THREAD_SAFE = (1 << 0),
72             BSON_CONTEXT_DISABLE_HOST_CACHE = (1 << 1),
73             BSON_CONTEXT_DISABLE_PID_CACHE = (1 << 2),
74             #if defined(__linux__)
75             BSON_CONTEXT_USE_TASK_ID = (1 << 3),
76             #endif
77             } bson_context_flags_t;
78              
79              
80             /**
81             * bson_context_t:
82             *
83             * This structure manages context for the bson library. It handles
84             * configuration for thread-safety and other performance related requirements.
85             * Consumers will create a context and may use multiple under a variety of
86             * situations.
87             *
88             * If your program calls fork(), you should initialize a new bson_context_t
89             * using bson_context_init().
90             *
91             * If you are using threading, it is suggested that you use a bson_context_t
92             * per thread for best performance. Alternatively, you can initialize the
93             * bson_context_t with BSON_CONTEXT_THREAD_SAFE, although a performance penalty
94             * will be incurred.
95             *
96             * Many functions will require that you provide a bson_context_t such as OID
97             * generation.
98             *
99             * This structure is oqaque in that you cannot see the contents of the
100             * structure. However, it is stack allocatable in that enough padding is
101             * provided in _bson_context_t to hold the structure.
102             */
103             typedef struct _bson_context_t bson_context_t;
104              
105              
106             /**
107             * bson_t:
108             *
109             * This structure manages a buffer whose contents are a properly formatted
110             * BSON document. You may perform various transforms on the BSON documents.
111             * Additionally, it can be iterated over using bson_iter_t.
112             *
113             * See bson_iter_init() for iterating the contents of a bson_t.
114             *
115             * When building a bson_t structure using the various append functions,
116             * memory allocations may occur. That is performed using power of two
117             * allocations and realloc().
118             *
119             * See http://bsonspec.org for the BSON document spec.
120             *
121             * This structure is meant to fit in two sequential 64-byte cachelines.
122             */
123             BSON_ALIGNED_BEGIN (128)
124             typedef struct _bson_t
125             {
126             uint32_t flags; /* Internal flags for the bson_t. */
127             uint32_t len; /* Length of BSON data. */
128             uint8_t padding[120]; /* Padding for stack allocation. */
129             } bson_t
130             BSON_ALIGNED_END (128);
131              
132              
133             /**
134             * BSON_INITIALIZER:
135             *
136             * This macro can be used to initialize a #bson_t structure on the stack
137             * without calling bson_init().
138             *
139             * |[
140             * bson_t b = BSON_INITIALIZER;
141             * ]|
142             */
143             #define BSON_INITIALIZER { 3, 5, { 5 } }
144              
145              
146             BSON_STATIC_ASSERT (sizeof (bson_t) == 128);
147              
148              
149             /**
150             * bson_oid_t:
151             *
152             * This structure contains the binary form of a BSON Object Id as specified
153             * on http://bsonspec.org. If you would like the bson_oid_t in string form
154             * see bson_oid_to_string() or bson_oid_to_string_r().
155             */
156             typedef struct
157             {
158             uint8_t bytes[12];
159             } bson_oid_t;
160              
161              
162             /**
163             * bson_decimal128_t:
164             *
165             * @high The high-order bytes of the decimal128. This field contains sign,
166             * combination bits, exponent, and part of the coefficient continuation.
167             * @low The low-order bytes of the decimal128. This field contains the second
168             * part of the coefficient continuation.
169             *
170             * This structure is a boxed type containing the value for the BSON decimal128
171             * type. The structure stores the 128 bits such that they correspond to the
172             * native format for the IEEE decimal128 type, if it is implemented.
173             **/
174             typedef struct
175             {
176             #if BSON_BYTE_ORDER == BSON_LITTLE_ENDIAN
177             uint64_t low;
178             uint64_t high;
179             #elif BSON_BYTE_ORDER == BSON_BIG_ENDIAN
180             uint64_t high;
181             uint64_t low;
182             #endif
183             } bson_decimal128_t;
184              
185              
186             BSON_STATIC_ASSERT (sizeof (bson_oid_t) == 12);
187              
188              
189             /**
190             * bson_validate_flags_t:
191             *
192             * This enumeration is used for validation of BSON documents. It allows
193             * selective control on what you wish to validate.
194             *
195             * %BSON_VALIDATE_NONE: No additional validation occurs.
196             * %BSON_VALIDATE_UTF8: Check that strings are valid UTF-8.
197             * %BSON_VALIDATE_DOLLAR_KEYS: Check that keys do not start with $.
198             * %BSON_VALIDATE_DOT_KEYS: Check that keys do not contain a period.
199             * %BSON_VALIDATE_UTF8_ALLOW_NULL: Allow NUL bytes in UTF-8 text.
200             */
201             typedef enum
202             {
203             BSON_VALIDATE_NONE = 0,
204             BSON_VALIDATE_UTF8 = (1 << 0),
205             BSON_VALIDATE_DOLLAR_KEYS = (1 << 1),
206             BSON_VALIDATE_DOT_KEYS = (1 << 2),
207             BSON_VALIDATE_UTF8_ALLOW_NULL = (1 << 3),
208             } bson_validate_flags_t;
209              
210              
211             /**
212             * bson_type_t:
213             *
214             * This enumeration contains all of the possible types within a BSON document.
215             * Use bson_iter_type() to fetch the type of a field while iterating over it.
216             */
217             typedef enum
218             {
219             BSON_TYPE_EOD = 0x00,
220             BSON_TYPE_DOUBLE = 0x01,
221             BSON_TYPE_UTF8 = 0x02,
222             BSON_TYPE_DOCUMENT = 0x03,
223             BSON_TYPE_ARRAY = 0x04,
224             BSON_TYPE_BINARY = 0x05,
225             BSON_TYPE_UNDEFINED = 0x06,
226             BSON_TYPE_OID = 0x07,
227             BSON_TYPE_BOOL = 0x08,
228             BSON_TYPE_DATE_TIME = 0x09,
229             BSON_TYPE_NULL = 0x0A,
230             BSON_TYPE_REGEX = 0x0B,
231             BSON_TYPE_DBPOINTER = 0x0C,
232             BSON_TYPE_CODE = 0x0D,
233             BSON_TYPE_SYMBOL = 0x0E,
234             BSON_TYPE_CODEWSCOPE = 0x0F,
235             BSON_TYPE_INT32 = 0x10,
236             BSON_TYPE_TIMESTAMP = 0x11,
237             BSON_TYPE_INT64 = 0x12,
238             BSON_TYPE_DECIMAL128 = 0x13,
239             BSON_TYPE_MAXKEY = 0x7F,
240             BSON_TYPE_MINKEY = 0xFF,
241             } bson_type_t;
242              
243              
244             /**
245             * bson_subtype_t:
246             *
247             * This enumeration contains the various subtypes that may be used in a binary
248             * field. See http://bsonspec.org for more information.
249             */
250             typedef enum
251             {
252             BSON_SUBTYPE_BINARY = 0x00,
253             BSON_SUBTYPE_FUNCTION = 0x01,
254             BSON_SUBTYPE_BINARY_DEPRECATED = 0x02,
255             BSON_SUBTYPE_UUID_DEPRECATED = 0x03,
256             BSON_SUBTYPE_UUID = 0x04,
257             BSON_SUBTYPE_MD5 = 0x05,
258             BSON_SUBTYPE_USER = 0x80,
259             } bson_subtype_t;
260              
261              
262             /*
263             *--------------------------------------------------------------------------
264             *
265             * bson_value_t --
266             *
267             * A boxed type to contain various bson_type_t types.
268             *
269             * See also:
270             * bson_value_copy()
271             * bson_value_destroy()
272             *
273             *--------------------------------------------------------------------------
274             */
275              
276             BSON_ALIGNED_BEGIN (8)
277             typedef struct _bson_value_t
278             {
279             bson_type_t value_type;
280             int32_t padding;
281             union {
282             bson_oid_t v_oid;
283             int64_t v_int64;
284             int32_t v_int32;
285             int8_t v_int8;
286             double v_double;
287             bool v_bool;
288             int64_t v_datetime;
289             struct {
290             uint32_t timestamp;
291             uint32_t increment;
292             } v_timestamp;
293             struct {
294             char *str;
295             uint32_t len;
296             } v_utf8;
297             struct {
298             uint8_t *data;
299             uint32_t data_len;
300             } v_doc;
301             struct {
302             uint8_t *data;
303             uint32_t data_len;
304             bson_subtype_t subtype;
305             } v_binary;
306             struct {
307             char *regex;
308             char *options;
309             } v_regex;
310             struct {
311             char *collection;
312             uint32_t collection_len;
313             bson_oid_t oid;
314             } v_dbpointer;
315             struct {
316             char *code;
317             uint32_t code_len;
318             } v_code;
319             struct {
320             char *code;
321             uint8_t *scope_data;
322             uint32_t code_len;
323             uint32_t scope_len;
324             } v_codewscope;
325             struct {
326             char *symbol;
327             uint32_t len;
328             } v_symbol;
329             bson_decimal128_t v_decimal128;
330             } value;
331             } bson_value_t
332             BSON_ALIGNED_END (8);
333              
334              
335             /**
336             * bson_iter_t:
337             *
338             * This structure manages iteration over a bson_t structure. It keeps track
339             * of the location of the current key and value within the buffer. Using the
340             * various functions to get the value of the iter will read from these
341             * locations.
342             *
343             * This structure is safe to discard on the stack. No cleanup is necessary
344             * after using it.
345             */
346             BSON_ALIGNED_BEGIN (128)
347             typedef struct
348             {
349             const uint8_t *raw; /* The raw buffer being iterated. */
350             uint32_t len; /* The length of raw. */
351             uint32_t off; /* The offset within the buffer. */
352             uint32_t type; /* The offset of the type byte. */
353             uint32_t key; /* The offset of the key byte. */
354             uint32_t d1; /* The offset of the first data byte. */
355             uint32_t d2; /* The offset of the second data byte. */
356             uint32_t d3; /* The offset of the third data byte. */
357             uint32_t d4; /* The offset of the fourth data byte. */
358             uint32_t next_off; /* The offset of the next field. */
359             uint32_t err_off; /* The offset of the error. */
360             bson_value_t value; /* Internal value for various state. */
361             } bson_iter_t
362             BSON_ALIGNED_END (128);
363              
364              
365             /**
366             * bson_reader_t:
367             *
368             * This structure is used to iterate over a sequence of BSON documents. It
369             * allows for them to be iterated with the possibility of no additional
370             * memory allocations under certain circumstances such as reading from an
371             * incoming mongo packet.
372             */
373              
374             BSON_ALIGNED_BEGIN (BSON_ALIGN_OF_PTR)
375             typedef struct
376             {
377             uint32_t type;
378             /*< private >*/
379             } bson_reader_t
380             BSON_ALIGNED_END (BSON_ALIGN_OF_PTR);
381              
382              
383             /**
384             * bson_visitor_t:
385             *
386             * This structure contains a series of pointers that can be executed for
387             * each field of a BSON document based on the field type.
388             *
389             * For example, if an int32 field is found, visit_int32 will be called.
390             *
391             * When visiting each field using bson_iter_visit_all(), you may provide a
392             * data pointer that will be provided with each callback. This might be useful
393             * if you are marshaling to another language.
394             *
395             * You may pre-maturely stop the visitation of fields by returning true in your
396             * visitor. Returning false will continue visitation to further fields.
397             */
398             BSON_ALIGNED_BEGIN (8)
399             typedef struct
400             {
401             /* run before / after descending into a document */
402             bool (*visit_before) (const bson_iter_t *iter,
403             const char *key,
404             void *data);
405             bool (*visit_after) (const bson_iter_t *iter,
406             const char *key,
407             void *data);
408             /* corrupt BSON, or unsupported type and visit_unsupported_type not set */
409             void (*visit_corrupt) (const bson_iter_t *iter,
410             void *data);
411             /* normal bson field callbacks */
412             bool (*visit_double) (const bson_iter_t *iter,
413             const char *key,
414             double v_double,
415             void *data);
416             bool (*visit_utf8) (const bson_iter_t *iter,
417             const char *key,
418             size_t v_utf8_len,
419             const char *v_utf8,
420             void *data);
421             bool (*visit_document) (const bson_iter_t *iter,
422             const char *key,
423             const bson_t *v_document,
424             void *data);
425             bool (*visit_array) (const bson_iter_t *iter,
426             const char *key,
427             const bson_t *v_array,
428             void *data);
429             bool (*visit_binary) (const bson_iter_t *iter,
430             const char *key,
431             bson_subtype_t v_subtype,
432             size_t v_binary_len,
433             const uint8_t *v_binary,
434             void *data);
435             /* normal field with deprecated "Undefined" BSON type */
436             bool (*visit_undefined) (const bson_iter_t *iter,
437             const char *key,
438             void *data);
439             bool (*visit_oid) (const bson_iter_t *iter,
440             const char *key,
441             const bson_oid_t *v_oid,
442             void *data);
443             bool (*visit_bool) (const bson_iter_t *iter,
444             const char *key,
445             bool v_bool,
446             void *data);
447             bool (*visit_date_time) (const bson_iter_t *iter,
448             const char *key,
449             int64_t msec_since_epoch,
450             void *data);
451             bool (*visit_null) (const bson_iter_t *iter,
452             const char *key,
453             void *data);
454             bool (*visit_regex) (const bson_iter_t *iter,
455             const char *key,
456             const char *v_regex,
457             const char *v_options,
458             void *data);
459             bool (*visit_dbpointer) (const bson_iter_t *iter,
460             const char *key,
461             size_t v_collection_len,
462             const char *v_collection,
463             const bson_oid_t *v_oid,
464             void *data);
465             bool (*visit_code) (const bson_iter_t *iter,
466             const char *key,
467             size_t v_code_len,
468             const char *v_code,
469             void *data);
470             bool (*visit_symbol) (const bson_iter_t *iter,
471             const char *key,
472             size_t v_symbol_len,
473             const char *v_symbol,
474             void *data);
475             bool (*visit_codewscope) (const bson_iter_t *iter,
476             const char *key,
477             size_t v_code_len,
478             const char *v_code,
479             const bson_t *v_scope,
480             void *data);
481             bool (*visit_int32) (const bson_iter_t *iter,
482             const char *key,
483             int32_t v_int32,
484             void *data);
485             bool (*visit_timestamp) (const bson_iter_t *iter,
486             const char *key,
487             uint32_t v_timestamp,
488             uint32_t v_increment,
489             void *data);
490             bool (*visit_int64) (const bson_iter_t *iter,
491             const char *key,
492             int64_t v_int64,
493             void *data);
494             bool (*visit_maxkey) (const bson_iter_t *iter,
495             const char *key,
496             void *data);
497             bool (*visit_minkey) (const bson_iter_t *iter,
498             const char *key,
499             void *data);
500             /* if set, called instead of visit_corrupt when an apparently valid BSON
501             * includes an unrecognized field type (reading future version of BSON) */
502             void (*visit_unsupported_type) (const bson_iter_t *iter,
503             const char *key,
504             uint32_t type_code,
505             void *data);
506             bool (*visit_decimal128) (const bson_iter_t *iter,
507             const char *key,
508             const bson_decimal128_t *v_decimal128,
509             void *data);
510              
511             void *padding[7];
512             } bson_visitor_t
513             BSON_ALIGNED_END (8);
514              
515             #define BSON_ERROR_BUFFER_SIZE 504
516              
517             BSON_ALIGNED_BEGIN (8)
518             typedef struct _bson_error_t
519             {
520             uint32_t domain;
521             uint32_t code;
522             char message[BSON_ERROR_BUFFER_SIZE];
523             } bson_error_t
524             BSON_ALIGNED_END (8);
525              
526              
527             BSON_STATIC_ASSERT (sizeof (bson_error_t) == 512);
528              
529              
530             /**
531             * bson_next_power_of_two:
532             * @v: A 32-bit unsigned integer of required bytes.
533             *
534             * Determines the next larger power of two for the value of @v
535             * in a constant number of operations.
536             *
537             * It is up to the caller to guarantee this will not overflow.
538             *
539             * Returns: The next power of 2 from @v.
540             */
541             static BSON_INLINE size_t
542 0           bson_next_power_of_two (size_t v)
543             {
544 0           v--;
545 0           v |= v >> 1;
546 0           v |= v >> 2;
547 0           v |= v >> 4;
548 0           v |= v >> 8;
549 0           v |= v >> 16;
550             #if BSON_WORD_SIZE == 64
551 0           v |= v >> 32;
552             #endif
553 0           v++;
554              
555 0           return v;
556             }
557              
558              
559             static BSON_INLINE bool
560 0           bson_is_power_of_two (uint32_t v)
561             {
562 0 0         return ((v != 0) && ((v & (v - 1)) == 0));
    0          
563             }
564              
565              
566             BSON_END_DECLS
567              
568              
569             #endif /* BSON_TYPES_H */