summaryrefslogtreecommitdiff
path: root/src/bson/bson.h
blob: 662af64d1af72b9ef958601dbcc7320dad0cc9ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
/**
 * @file bson.h
 * @brief BSON Declarations
 */

/*    Copyright 2009-2012 10gen Inc.
 *    Copyright (C) 2012-2015 Softmotions Ltd <info@softmotions.com>
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#ifndef BSON_H_
#define BSON_H_

#include "basedefs.h"
#include <stdio.h>
#include <stdint.h>
#if ! defined(__cplusplus)
#include <stdbool.h>
#endif
#include <time.h>
#include "tcutil.h"

#define BSON_IS_NUM_TYPE(atype) (atype == BSON_INT || atype == BSON_LONG || atype == BSON_DOUBLE)
#define BSON_IS_NULL_TYPE(atype) (atype == BSON_UNDEFINED || atype == BSON_NULL)
#define BSON_IS_STRING_TYPE(atype) ((atype) == BSON_STRING || (atype) == BSON_SYMBOL) 

EJDB_EXTERN_C_START

#define BSON_OK 0
#define BSON_ERROR -1

//Maximum field path length allocated on stack
#define BSON_MAX_FPATH_LEN (255)

enum bson_error_t {
    BSON_SIZE_OVERFLOW = 1 /**< Trying to create a BSON object larger than INT_MAX. */
};

enum bson_validity_t {
    BSON_VALID = 0, /**< BSON is valid and UTF-8 compliant. */
    BSON_NOT_UTF8 = (1 << 1), /**< A key or a string is not valid UTF-8. */
    BSON_FIELD_HAS_DOT = (1 << 2), /**< Warning: key contains '.' character. */
    BSON_FIELD_INIT_DOLLAR = (1 << 3), /**< Warning: key starts with '$' character. */
    BSON_ALREADY_FINISHED = (1 << 4), /**< Trying to modify a finished BSON object. */
    BSON_ERROR_ANY = (1 << 5), /**< Unspecified error */
    BSON_NOT_FINISHED = (1 << 6) /**< BSON object not finished */
};

enum bson_binary_subtype_t {
    BSON_BIN_BINARY = 0,
    BSON_BIN_FUNC = 1,
    BSON_BIN_BINARY_OLD = 2,
    BSON_BIN_UUID = 3,
    BSON_BIN_MD5 = 5,
    BSON_BIN_USER = 128
};

enum bson_flags_t {
    BSON_FLAG_QUERY_MODE = 1,
    BSON_FLAG_STACK_ALLOCATED = 1 << 1 /**< If it set BSON data is allocated on stack and realloc should deal with this case */
};

typedef enum {
    BSON_EOO = 0,
    BSON_DOUBLE = 1,
    BSON_STRING = 2,
    BSON_OBJECT = 3,
    BSON_ARRAY = 4,
    BSON_BINDATA = 5,
    BSON_UNDEFINED = 6,
    BSON_OID = 7,
    BSON_BOOL = 8,
    BSON_DATE = 9,
    BSON_NULL = 10,
    BSON_REGEX = 11,
    BSON_DBREF = 12, /**< Deprecated. */
    BSON_CODE = 13,
    BSON_SYMBOL = 14,
    BSON_CODEWSCOPE = 15,
    BSON_INT = 16,
    BSON_TIMESTAMP = 17,
    BSON_LONG = 18
} bson_type;

typedef int bson_bool_t;

typedef struct {
    const char *cur;
    bson_bool_t first;
} bson_iterator;

typedef struct {
    char *data; /**< Pointer to a block of data in this BSON object. */
    char *cur; /**< Pointer to the current position. */
    int dataSize; /**< The number of bytes allocated to char *data. */
    bson_bool_t finished; /**< When finished, the BSON object can no longer be modified. */
    int stack[32]; /**< A stack used to keep track of nested BSON elements. */
    int stackPos; /**< Index of current stack position. */
    int err; /**< Bitfield representing errors or warnings on this buffer */
    char *errstr; /**< A string representation of the most recent error or warning. */
    int flags;
} bson;

#pragma pack(1)

typedef union {
    char bytes[12];
    int ints[3];
} bson_oid_t;
#pragma pack()

typedef int64_t bson_date_t; /* milliseconds since epoch UTC */

typedef struct {
    int i; /* increment */
    int t; /* time in seconds */
} bson_timestamp_t;


EJDB_EXPORT const char* bson_first_errormsg(bson *bson);


#define BSON_ITERATOR_FROM_BUFFER(_bs_I, _bs_B) \
    (_bs_I)->cur = ((char*) (_bs_B)) + 4;       \
    (_bs_I)->first = 1;

#define BSON_ITERATOR_SUBITERATOR(_bs_I, _bs_S) \
    BSON_ITERATOR_FROM_BUFFER((_bs_S), bson_iterator_value(_bs_I))

#define BSON_ITERATOR_TYPE(_bs_I) \
    ((bson_type) (_bs_I)->cur[0])

#define BSON_ITERATOR_KEY(_bs_I) \
    ((_bs_I)->cur + 1)

#define BSON_ITERATOR_INIT(_bs_I, _bs) \
    (_bs_I)->cur = (_bs)->data + 4; \
    (_bs_I)->first = 1;

#define BSON_ITERATOR_CLONE(_bs_I_S, _bs_I_T) \
    (_bs_I_T)->cur = (_bs_I_S)->cur; \
    (_bs_I_T)->first = (_bs_I_S)->first;

/* --------------------------------
   READING
   ------------------------------ */

EJDB_EXPORT bson* bson_create(void);
EJDB_EXPORT void bson_dispose(bson* b);

/**
 * Size of a BSON object.
 *
 * @param b the BSON object.
 *
 * @return the size.
 */
EJDB_EXPORT int bson_size(const bson *b);
EJDB_EXPORT int bson_size2(const void *bsdata);
EJDB_EXPORT int bson_buffer_size(const bson *b);

/**
 * Return a pointer to the raw buffer stored by this bson object.
 *
 * @param b a BSON object
 */
EJDB_EXPORT const char *bson_data(const bson *b);
EJDB_EXPORT const char* bson_data2(const bson *b, int *bsize);

/**
 * Print a string representation of a BSON object.
 *
 * @param bson the raw data to print.
 * @param depth the depth to recurse the object.x
 */
EJDB_EXPORT void bson_print_raw(const char *bson, int depth);

/**
 * Advance a bson_iterator to the named field.
 *
 * @param it the bson_iterator to use.
 * @param obj the BSON object to use.
 * @param name the name of the field to find.
 *
 * @return the type of the found object or BSON_EOO if it is not found.
 */
EJDB_EXPORT bson_type bson_find(bson_iterator *it, const bson *obj, const char *name);

EJDB_EXPORT bson_type bson_find_from_buffer(bson_iterator *it, const char *buffer, const char *name);

typedef struct { /**< Find field path context */
    const char* fpath;
    int fplen;
    bson_iterator *input;
    int stopos;
    bool stopnestedarr;
    int mpos; /**< Array index of the first matched array field */
    int dpos; /**< Position of `$` in array projection fieldpath. */
} FFPCTX;


/**
 * Advance specified iterator 'it' to field value pointing by 'fieldpath'/
 * Field path string format: 'field1.nestedfield1.nestedfield.2'.
 * If specified field not found BSON_EOO will be returned.
 *
 * @param fieldpath Path specification to the BSON field.
 * @param it the bson_iterator to use.
 * @return the type of the found object or BSON_EOO if it is not found.
 */
EJDB_EXPORT bson_type bson_find_fieldpath_value(const char *fieldpath, bson_iterator *it);
EJDB_EXPORT bson_type bson_find_fieldpath_value2(const char *fpath, int fplen, bson_iterator *it);
EJDB_EXPORT bson_type bson_find_fieldpath_value3(FFPCTX* ffctx);

/**
 * BSON object visitor
 * @param it bson iterator to traverse
 * @param visitor Visitor function
 * @param op Opaque data for visitor
 */
typedef enum {
    BSON_TRAVERSE_ARRAYS_EXCLUDED = 1,
    BSON_TRAVERSE_OBJECTS_EXCLUDED = 1 << 1
} bson_traverse_flags_t;

typedef enum {
    BSON_VCMD_OK = 0,
    BSON_VCMD_TERMINATE = 1,
    BSON_VCMD_SKIP_NESTED = 1 << 1,
    BSON_VCMD_SKIP_AFTER = 1 << 2
} bson_visitor_cmd_t;

typedef bson_visitor_cmd_t(*BSONVISITOR)(const char *ipath, int ipathlen, 
                                         const char *key, int keylen, 
                                         const bson_iterator *it, 
                                         bool after, void *op);

EJDB_EXPORT void bson_visit_fields(bson_iterator *it, 
                                   bson_traverse_flags_t flags, 
                                   BSONVISITOR visitor, void *op);


EJDB_EXPORT bson_iterator* bson_iterator_create(void);
EJDB_EXPORT void bson_iterator_dispose(bson_iterator*);
/**
 * Initialize a bson_iterator.
 *
 * @param i the bson_iterator to initialize.
 * @param bson the BSON object to associate with the iterator.
 */
EJDB_EXPORT void bson_iterator_init(bson_iterator *i, const bson *b);

/**
 * Initialize a bson iterator from a const char* buffer. Note
 * that this is mostly used internally.
 *
 * @param i the bson_iterator to initialize.
 * @param buffer the buffer to point to.
 */
EJDB_EXPORT void bson_iterator_from_buffer(bson_iterator *i, const char *buffer);

/* more returns true for eoo. best to loop with bson_iterator_next(&it) */
/**
 * Check to see if the bson_iterator has more data.
 *
 * @param i the iterator.
 *
 * @return  returns true if there is more data.
 */
EJDB_EXPORT bson_bool_t bson_iterator_more(const bson_iterator *i);

/**
 * Point the iterator at the next BSON object.
 *
 * @param i the bson_iterator.
 *
 * @return the type of the next BSON object.
 */
EJDB_EXPORT bson_type bson_iterator_next(bson_iterator *i);

/**
 * Get the type of the BSON object currently pointed to by the iterator.
 *
 * @param i the bson_iterator
 *
 * @return  the type of the current BSON object.
 */
EJDB_EXPORT bson_type bson_iterator_type(const bson_iterator *i);

/**
 * Get the key of the BSON object currently pointed to by the iterator.
 *
 * @param i the bson_iterator
 *
 * @return the key of the current BSON object.
 */
EJDB_EXPORT const char *bson_iterator_key(const bson_iterator *i);

/**
 * Get the value of the BSON object currently pointed to by the iterator.
 *
 * @param i the bson_iterator
 *
 * @return  the value of the current BSON object.
 */
EJDB_EXPORT const char *bson_iterator_value(const bson_iterator *i);

/* these convert to the right type (return 0 if non-numeric) */
/**
 * Get the double value of the BSON object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return  the value of the current BSON object.
 */
EJDB_EXPORT double bson_iterator_double(const bson_iterator *i);

/**
 * Get the int value of the BSON object currently pointed to by the iterator.
 *
 * @param i the bson_iterator
 *
 * @return  the value of the current BSON object.
 */
EJDB_EXPORT int bson_iterator_int(const bson_iterator *i);

/**
 * Get the long value of the BSON object currently pointed to by the iterator.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
EJDB_EXPORT int64_t bson_iterator_long(const bson_iterator *i);

/* return the bson timestamp as a whole or in parts */
/**
 * Get the timestamp value of the BSON object currently pointed to by
 * the iterator.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
EJDB_EXPORT bson_timestamp_t bson_iterator_timestamp(const bson_iterator *i);
EJDB_EXPORT int bson_iterator_timestamp_time(const bson_iterator *i);
EJDB_EXPORT int bson_iterator_timestamp_increment(const bson_iterator *i);

/**
 * Get the boolean value of the BSON object currently pointed to by
 * the iterator.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
/* false: boolean false, 0 in any type, or null */
/* true: anything else (even empty strings and objects) */
EJDB_EXPORT bson_bool_t bson_iterator_bool(const bson_iterator *i);

/**
 * Get the double value of the BSON object currently pointed to by the
 * iterator. Assumes the correct type is used.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
/* these assume you are using the right type */
EJDB_EXPORT double bson_iterator_double_raw(const bson_iterator *i);

/**
 * Get the int value of the BSON object currently pointed to by the
 * iterator. Assumes the correct type is used.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
EJDB_EXPORT int bson_iterator_int_raw(const bson_iterator *i);

/**
 * Get the long value of the BSON object currently pointed to by the
 * iterator. Assumes the correct type is used.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
EJDB_EXPORT int64_t bson_iterator_long_raw(const bson_iterator *i);

/**
 * Get the bson_bool_t value of the BSON object currently pointed to by the
 * iterator. Assumes the correct type is used.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
EJDB_EXPORT bson_bool_t bson_iterator_bool_raw(const bson_iterator *i);

/**
 * Get the bson_oid_t value of the BSON object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON object.
 */
EJDB_EXPORT bson_oid_t *bson_iterator_oid(const bson_iterator *i);

/**
 * Get the string value of the BSON object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return  the value of the current BSON object.
 */
/* these can also be used with bson_code and bson_symbol*/
EJDB_EXPORT const char *bson_iterator_string(const bson_iterator *i);

/**
 * Get the string length of the BSON object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the length of the current BSON object.
 */
EJDB_EXPORT int bson_iterator_string_len(const bson_iterator *i);

/**
 * Get the code value of the BSON object currently pointed to by the
 * iterator. Works with bson_code, bson_codewscope, and BSON_STRING
 * returns NULL for everything else.
 *
 * @param i the bson_iterator
 *
 * @return the code value of the current BSON object.
 */
/* works with bson_code, bson_codewscope, and BSON_STRING */
/* returns NULL for everything else */
EJDB_EXPORT const char *bson_iterator_code(const bson_iterator *i);

/**
 * Calls bson_empty on scope if not a bson_codewscope
 *
 * @param i the bson_iterator.
 * @param scope the bson scope.
 */
/* calls bson_empty on scope if not a bson_codewscope */
EJDB_EXPORT void bson_iterator_code_scope(const bson_iterator *i, bson *scope);

/**
 * Get the date value of the BSON object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the date value of the current BSON object.
 */
/* both of these only work with bson_date */
EJDB_EXPORT bson_date_t bson_iterator_date(const bson_iterator *i);

/**
 * Get the time value of the BSON object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the time value of the current BSON object.
 */
EJDB_EXPORT time_t bson_iterator_time_t(const bson_iterator *i);

/**
 * Get the length of the BSON binary object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the length of the current BSON binary object.
 */
EJDB_EXPORT int bson_iterator_bin_len(const bson_iterator *i);

/**
 * Get the type of the BSON binary object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the type of the current BSON binary object.
 */
EJDB_EXPORT char bson_iterator_bin_type(const bson_iterator *i);

/**
 * Get the value of the BSON binary object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON binary object.
 */
EJDB_EXPORT const char *bson_iterator_bin_data(const bson_iterator *i);

/**
 * Get the value of the BSON regex object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator
 *
 * @return the value of the current BSON regex object.
 */
EJDB_EXPORT const char *bson_iterator_regex(const bson_iterator *i);

/**
 * Get the options of the BSON regex object currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator.
 *
 * @return the options of the current BSON regex object.
 */
EJDB_EXPORT const char *bson_iterator_regex_opts(const bson_iterator *i);

/* these work with BSON_OBJECT and BSON_ARRAY */
/**
 * Get the BSON subobject currently pointed to by the
 * iterator.
 *
 * @param i the bson_iterator.
 * @param sub the BSON subobject destination.
 */
EJDB_EXPORT void bson_iterator_subobject(const bson_iterator *i, bson *sub);

/**
 * Get a bson_iterator that on the BSON subobject.
 *
 * @param i the bson_iterator.
 * @param sub the iterator to point at the BSON subobject.
 */
EJDB_EXPORT void bson_iterator_subiterator(const bson_iterator *i, bson_iterator *sub);

/* str must be at least 24 hex chars + null byte */
/**
 * Create a bson_oid_t from a string.
 *
 * @param oid the bson_oid_t destination.
 * @param str a null terminated string comprised of at least 24 hex chars.
 */
EJDB_EXPORT void bson_oid_from_string(bson_oid_t *oid, const char *str);

/**
 * Create a string representation of the bson_oid_t.
 *
 * @param oid the bson_oid_t source.
 * @param str the string representation destination.
 */
EJDB_EXPORT void bson_oid_to_string(const bson_oid_t *oid, char *str);

/**
 * Create a bson_oid object.
 *
 * @param oid the destination for the newly created bson_oid_t.
 */
EJDB_EXPORT void bson_oid_gen(bson_oid_t *oid);

/**
 * Set a function to be used to generate the second four bytes
 * of an object id.
 *
 * @param func a pointer to a function that returns an int.
 */
EJDB_EXPORT void bson_set_oid_fuzz(int ( *func)(void));

/**
 * Set a function to be used to generate the incrementing part
 * of an object id (last four bytes). If you need thread-safety
 * in generating object ids, you should set this function.
 *
 * @param func a pointer to a function that returns an int.
 */
EJDB_EXPORT void bson_set_oid_inc(int ( *func)(void));

/**
 * Get the time a bson_oid_t was created.
 *
 * @param oid the bson_oid_t.
 */
EJDB_EXPORT time_t bson_oid_generated_time(bson_oid_t *oid); /* Gives the time the OID was created */

/* ----------------------------
   BUILDING
   ------------------------------ */


EJDB_EXPORT void bson_append(bson *b, const void *data, int len);

/**
 *  Initialize a new bson object. If not created
 *  with bson_new, you must initialize each new bson
 *  object using this function.
 *
 *  @note When finished, you must pass the bson object to
 *      bson_del( ).
 */
EJDB_EXPORT void bson_init(bson *b);


/**
 * Intialize a new bson object. In query contruction mode allowing
 * dot and dollar chars in field names.
 * @param b
 */
EJDB_EXPORT void bson_init_as_query(bson *b);

/**
 * Initialize a BSON object, and point its data
 * pointer to the provided char*.
 *
 * @param b the BSON object to initialize.
 * @param data the raw BSON data.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_init_data(bson *b, char *data);
EJDB_EXPORT int bson_init_finished_data(bson *b, const char *data);

/**
 * Initialize a BSON object, and set its
 * buffer to the given size.
 *
 * @param b the BSON object to initialize.
 * @param size the initial size of the buffer.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT void bson_init_size(bson *b, int size);

EJDB_EXPORT void bson_init_on_stack(bson *b, char *bstack, int mincapacity, int maxonstack);

/**
 * Grow a bson object.
 *
 * @param b the bson to grow.
 * @param bytesNeeded the additional number of bytes needed.
 *
 * @return BSON_OK or BSON_ERROR with the bson error object set.
 *   Exits if allocation fails.
 */
EJDB_EXPORT int bson_ensure_space(bson *b, const int bytesNeeded);

/**
 * Finalize a bson object.
 *
 * @param b the bson object to finalize.
 *
 * @return the standard error code. To deallocate memory,
 *   call bson_del on the bson object.
 */
EJDB_EXPORT int bson_finish(bson *b);

/**
 * Destroy a bson object.
 * Clears bson object and frees internal memory buffers held by bson
 * object BUT does not delete bson object itself
 * @param b the bson object to destroy.
 */
EJDB_EXPORT void bson_destroy(bson *b);

/**
 * The bson_del() performs bson_destroy() then frees bson object itself.
 * @param b
 */
EJDB_EXPORT void bson_del(bson *b);

EJDB_EXPORT void bson_reset(bson *b);

/**
 * Returns a pointer to a static empty BSON object.
 *
 * @param obj the BSON object to initialize.
 *
 * @return the empty initialized BSON object.
 */
/* returns pointer to static empty bson object */
EJDB_EXPORT bson *bson_empty(bson *obj);

/**
 * Make a complete copy of the a BSON object.
 * The source bson object must be in a finished
 * state; otherwise, the copy will fail.
 *
 * @param out the copy destination BSON object.
 * @param in the copy source BSON object.
 */
EJDB_EXPORT int bson_copy(bson *out, const bson *in); /* puts data in new buffer. NOOP if out==NULL */

/**
 * Append a previously created bson_oid_t to a bson object.
 *
 * @param b the bson to append to.
 * @param name the key for the bson_oid_t.
 * @param oid the bson_oid_t to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_oid(bson *b, const char *name, const bson_oid_t *oid);

/**
 * Append a bson_oid_t to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the bson_oid_t.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_new_oid(bson *b, const char *name);

/**
 * Append an int to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the int.
 * @param i the int to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_int(bson *b, const char *name, const int i);

/**
 * Append an long to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the long.
 * @param i the long to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_long(bson *b, const char *name, const int64_t i);

/**
 * Append an double to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the double.
 * @param d the double to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_double(bson *b, const char *name, const double d);

/**
 * Append a string to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the string.
 * @param str the string to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_string(bson *b, const char *name, const char *str);

/**
 * Append len bytes of a string to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the string.
 * @param str the string to append.
 * @param len the number of bytes from str to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_string_n(bson *b, const char *name, const char *str, int len);

/**
 * Append a symbol to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the symbol.
 * @param str the symbol to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_symbol(bson *b, const char *name, const char *str);

/**
 * Append len bytes of a symbol to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the symbol.
 * @param str the symbol to append.
 * @param len the number of bytes from str to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_symbol_n(bson *b, const char *name, const char *str, int len);

/**
 * Append code to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the code.
 * @param str the code to append.
 * @param len the number of bytes from str to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_code(bson *b, const char *name, const char *str);

/**
 * Append len bytes of code to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the code.
 * @param str the code to append.
 * @param len the number of bytes from str to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_code_n(bson *b, const char *name, const char *str, int len);

/**
 * Append code to a bson with scope.
 *
 * @param b the bson to append to.
 * @param name the key for the code.
 * @param str the string to append.
 * @param scope a BSON object containing the scope.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_code_w_scope(bson *b, const char *name, const char *code, const bson *scope);

/**
 * Append len bytes of code to a bson with scope.
 *
 * @param b the bson to append to.
 * @param name the key for the code.
 * @param str the string to append.
 * @param len the number of bytes from str to append.
 * @param scope a BSON object containing the scope.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_code_w_scope_n(bson *b, const char *name, const char *code, int size, const bson *scope);

/**
 * Append binary data to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the data.
 * @param type the binary data type.
 * @param str the binary data.
 * @param len the length of the data.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_binary(bson *b, const char *name, char type, const char *str, int len);

/**
 * Append a bson_bool_t to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the boolean value.
 * @param v the bson_bool_t to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_bool(bson *b, const char *name, const bson_bool_t v);

/**
 * Append a null value to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the null value.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_null(bson *b, const char *name);

/**
 * Append an undefined value to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the undefined value.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_undefined(bson *b, const char *name);

/**
 * Append a regex value to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the regex value.
 * @param pattern the regex pattern to append.
 * @param the regex options.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_regex(bson *b, const char *name, const char *pattern, const char *opts);

/**
 * Append bson data to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the bson data.
 * @param bson the bson object to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_bson(bson *b, const char *name, const bson *bson);

/**
 * Append a BSON element to a bson from the current point of an iterator.
 *
 * @param b the bson to append to.
 * @param name_or_null the key for the BSON element, or NULL.
 * @param elem the bson_iterator.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_element(bson *b, const char *name_or_null, const bson_iterator *elem);

/**
 * Append a bson_timestamp_t value to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the timestampe value.
 * @param ts the bson_timestamp_t value to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_timestamp(bson *b, const char *name, bson_timestamp_t *ts);
EJDB_EXPORT int bson_append_timestamp2(bson *b, const char *name, int time, int increment);

/* these both append a bson_date */
/**
 * Append a bson_date_t value to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the date value.
 * @param millis the bson_date_t to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_date(bson *b, const char *name, bson_date_t millis);

/**
 * Append a time_t value to a bson.
 *
 * @param b the bson to append to.
 * @param name the key for the date value.
 * @param secs the time_t to append.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_time_t(bson *b, const char *name, time_t secs);

/**
 * Start appending a new object to a bson.
 *
 * @param b the bson to append to.
 * @param name the name of the new object.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_start_object(bson *b, const char *name);
EJDB_EXPORT int bson_append_start_object2(bson *b, const char *name, int namelen);

/**
 * Start appending a new array to a bson.
 *
 * @param b the bson to append to.
 * @param name the name of the new array.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_start_array(bson *b, const char *name);
EJDB_EXPORT int bson_append_start_array2(bson *b, const char *name, int namelen);

/**
 * Finish appending a new object or array to a bson.
 *
 * @param b the bson to append to.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_finish_object(bson *b);

/**
 * Finish appending a new object or array to a bson. This
 * is simply an alias for bson_append_finish_object.
 *
 * @param b the bson to append to.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_finish_array(bson *b);

EJDB_EXPORT int bson_merge_recursive(const bson *b1, const bson *b2, bson_bool_t overwrite, bson *out);

/**
 * Check duplicate keys
 * @return true if bson contains duplicate keys
 */
EJDB_EXPORT bson_bool_t bson_check_duplicate_keys(const bson *bs);

/**
 * Remove duplicate keys from bson:
 *  - merge objects and arrays with same key:
 *   { a : { b : "value 1" }, a : { c : "value 2" } } -> { a : { b : "value 1", c : "value 2" } }
 *  - keep last value for non object and non array values
 *   { a : "value 1", a : "value 2" } -> { a : "value 2" }
 *
 * Example:
 * {
 *   a : {
 *     b : 1,
 *     c : "c"
 *   },
 *   b : NULL,
 *   c : [
 *     {
 *       a : 1,
 *       b : 2,
 *       a : 0
 *     },
 *     {
 *       a : 0,
 *       b : 1,
 *       c : 3
 *     }
 *   ],
 *   a : {
 *     d : 0,
 *     c : 1
 *   }
 * }
 *
 * =>
 *
 * {
 *   a : {
 *     b : 1,
 *     c : 1,
 *     d : 0
 *   },
 *   b : NULL,
 *   c : [
 *     {
 *       a : 0,
 *       b : 2
 *     },
 *     {
 *       a : 0,
         b : 1,
         c : 3
 *     }
 *   ]
 * }
 */
EJDB_EXPORT void bson_fix_duplicate_keys(const bson *bsi, bson *bso);

EJDB_EXPORT void bson_numstr(char *str, int64_t i);
EJDB_EXPORT int bson_numstrn(char *str, int maxbuf, int64_t i);

//void bson_incnumstr(char *str);

/* Error handling and standard library function over-riding. */
/* -------------------------------------------------------- */

/* bson_err_handlers shouldn't return!!! */
typedef void( *bson_err_handler)(const char *errmsg);
typedef int (*bson_printf_func)(const char *, ...);

void bson_free(void *ptr);

/**
 * Allocates memory and checks return value, exiting fatally if malloc() fails.
 *
 * @param size bytes to allocate.
 *
 * @return a pointer to the allocated memory.
 *
 * @sa malloc(3)
 */
void *bson_malloc(int size);

/**
 * Changes the size of allocated memory and checks return value,
 * exiting fatally if realloc() fails.
 *
 * @param ptr pointer to the space to reallocate.
 * @param size bytes to allocate.
 *
 * @return a pointer to the allocated memory.
 *
 * @sa realloc()
 */
void *bson_realloc(void *ptr, int size);

/**
 * Set a function for error handling.
 *
 * @param func a bson_err_handler function.
 *
 * @return the old error handling function, or NULL.
 */
EJDB_EXPORT bson_err_handler set_bson_err_handler(bson_err_handler func);

/* does nothing if ok != 0 */
/**
 * Exit fatally.
 *
 * @param ok exits if ok is equal to 0.
 */
void bson_fatal(int ok);

/**
 * Exit fatally with an error message.
 *
 * @param ok exits if ok is equal to 0.
 * @param msg prints to stderr before exiting.
 */
void bson_fatal_msg(int ok, const char *msg);

/**
 * Invoke the error handler, but do not exit.
 *
 * @param b the buffer object.
 */
void bson_builder_error(bson *b);

/**
 * Cast an int64_t to double. This is necessary for embedding in
 * certain environments.
 *
 */
EJDB_EXPORT double bson_int64_to_double(int64_t i64);
EJDB_EXPORT void bson_swap_endian32(void *outp, const void *inp);
EJDB_EXPORT void bson_swap_endian64(void *outp, const void *inp);


/**
 * Append current field from iterator into bson object.
 *
 * @param from
 * @param into
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_field_from_iterator(const bson_iterator *from, bson *into);

/**
 * Append current field value from iterator into bson object under specified string key.
 *
 * @param key Key name.
 * @param from Source iterator value
 * @param into Target bsob object
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_append_field_from_iterator2(const char *key, const bson_iterator *from, bson *into);
EJDB_EXPORT int bson_append_object_from_iterator(const char *key, bson_iterator *from, bson *into);
EJDB_EXPORT int bson_append_array_from_iterator(const char *key, bson_iterator *from, bson *into);


/**
 * Merge bson  `b2` into `b1` saving result the 'out' object.
 * `b1` & `b2` bson must be finished BSONS.
 * Resulting 'out' bson must be allocated and not finished.
 *
 * Nested object skipped and usupported.
 *
 * @param b1 BSON to to be merged in `out`
 * @param b2 Second BSON to to be merged in `out`
 * @param overwrite if `true` all `b1` fields will be overwriten by corresponding `b2` fields
 * @param out
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_merge(const bson *b1, const bson *b2, bson_bool_t overwrite, bson *out);
EJDB_EXPORT int bson_merge2(const void *b1data, const void *b2data, bson_bool_t overwrite, bson *out);

/**
 * Recursive merge bson  `b2` into `b1` saving result the 'out' object.
 * `b1` & `b2` bson must be finished BSONS.
 * Resulting 'out' bson must be allocated and not finished.
 *
 * NOTE. Arrays with same paths joined.
 *
 * @param b1 BSON to to be merged in `out`
 * @param b2 Second BSON to to be merged in `out`
 * @param overwrite if `true` all `b1` fields will be overwriten by corresponding `b2` fields
 * @param out
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_merge_recursive(const bson *b1, const bson *b2, bson_bool_t overwrite, bson *out);
EJDB_EXPORT int bson_merge_recursive2(const void *b1data, const void *b2data, bson_bool_t overwrite, bson *out);

/**
 * Merge bsons.
 * `bsdata2` may contain field path keys (eg: 'foo.bar').
 * 
 * @param bsdata1 BSON data to to be merged in `out`
 * @param bsdata2 Second BSON data to to be merged in `out`
 * @param out Resulting `out` bson must be allocated and not finished.
 *
 * @return BSON_OK or BSON_ERROR.
 */
EJDB_EXPORT int bson_merge_fieldpaths(const void *bsdata1, const void *bsdata2, bson *out);

EJDB_EXPORT int bson_inplace_set_bool(bson_iterator *pos, bson_bool_t val);
EJDB_EXPORT int bson_inplace_set_long(bson_iterator *pos, int64_t val);
EJDB_EXPORT int bson_inplace_set_double(bson_iterator *pos, double val);

typedef struct {
    TCMAP *ifields;     //Required Map of fieldpaths. Map values are a simple boolean bufs.
    bool imode;         //Required If true fpaths will be included. Otherwise fpaths will be excluded from bson.
    const void *bsbuf;  //Required BSON buffer to process.
    bson *bsout;        //Required Allocated output not finished bson* object.
    TCMAP *fkfields;    //Optional: Map (fpath => bson key) used to force specific bson keys for selected fpaths.
    int matched;        //Output: number of matched fieldpaths
    bson *collector;    //Optional: Collector for excluded data (fieldpath -> excluded value)
} BSONSTRIPCTX;

/**
 * Include or exclude fpaths in the specified BSON and put resulting data into `bsout`.
 * On completion it finishes `bsout` object.
 *
 * @param ifields Map of fieldpaths. Map values are a simple boolean bufs.
 * @param imode If true fpaths will be included. Otherwise fpaths will be excluded from bson.
 * @param bsbuf BSON buffer to process.
 * @param bsout Allocated and not finished output bson* object
 * @param matched[out] Number of matched include/exclude fieldpaths.
 * @return BSON error code
 */
EJDB_EXPORT int bson_strip(TCMAP *ifields, bool imode, const void *bsbuf, bson *bsout, int *matched);
EJDB_EXPORT int bson_strip2(BSONSTRIPCTX *sctx);

/**
 * @brief Rename a fields specified by `fields` rename mapping.
 * 
 * This operation unsets both all and new fieldpaths and then sets 
 * new fieldpath values. 
 * 
 * @param fields Rename mapping old `fieldpath` to new `fieldpath`.
 * @param bsbuf BSON buffer to process.
 * @param bsout Allocated and not finished output bson* object
 * @param rencnt A number of fieldpaths actually renamed.
 */
EJDB_EXPORT int bson_rename(TCMAP *fields, const void *bsbuf, bson *bsout, int *rencnt);


/**
 * Compares field path primitive values of two BSONs
 * @param bsdata1 BSON raw data
 * @param bsdata2 BSON raw data
 * @param fpath Field path to the field
 * @param fplen Length of fpath value
 */
EJDB_EXPORT int bson_compare(const void *bsdata1, const void *bsdata2, const char* fpath, int fplen);
EJDB_EXPORT int bson_compare_fpaths(const void *bsdata1, const void *bsdata2, 
                                    const char *fpath1, int fplen1, 
                                    const char *fpath2, int fplen2);
EJDB_EXPORT int bson_compare_it_current(const bson_iterator *it1, const bson_iterator *it2);
EJDB_EXPORT int bson_compare_string(const char* cv, const void *bsdata, const char *fpath);
EJDB_EXPORT int bson_compare_long(const int64_t cv, const void *bsdata, const char *fpath);
EJDB_EXPORT int bson_compare_double(double cv, const void *bsdata, const char *fpath);
EJDB_EXPORT int bson_compare_bool(bson_bool_t cv, const void *bsdata, const char *fpath);


/**
 * Duplicates BSON object.
 * @param src BSON
 * @return Finished copy of src
 */
EJDB_EXPORT bson* bson_dup(const bson *src);


EJDB_EXPORT bson* bson_create_from_iterator(bson_iterator *from);
EJDB_EXPORT bson* bson_create_from_buffer(const void *buf, int bufsz);
EJDB_EXPORT bson* bson_create_from_buffer2(bson *bs, const void *buf, int bufsz);
EJDB_EXPORT void bson_init_with_data(bson *bs, const void *bsdata);

typedef enum {
    BSON_MERGE_ARRAY_ADDSET = 0,
    BSON_MERGE_ARRAY_PULL = 1,
    BSON_MERGE_ARRAY_PUSH = 2
} bson_merge_array_mode;

EJDB_EXPORT int bson_merge_arrays(const void *mbuf, const void *inbuf, 
                                  bson_merge_array_mode mode, 
                                  bool expandall, bson *bsout);
                                  
EJDB_EXPORT bool bson_find_unmerged_arrays(const void *mbuf, const void *inbuf);
EJDB_EXPORT bool bson_find_merged_arrays(const void *mbuf, const void *inbuf, bool expandall);

/**
 * Convert BSON into JSON buffer.
 * 
 * A caller should free an allocated `*buf` 
 * if value of `*buf` is not `NULL` after function completion. 
 * 
 * @param src BSON data
 * @param buf Allocated buffer with resulting JSON data
 * @param sp JSON data length will be stored into
 * @return BSON_OK or BSON_ERROR
 */
EJDB_EXPORT int bson2json(const char *bsdata, char **buf, int *sp);

/**
 * Convert JSON into BSON object.
 * @param jsonstr NULL terminated JSON string
 * @return Allocated BSON object filled with given JSON data or NULL on error
 */
EJDB_EXPORT bson* json2bson(const char *jsonstr);


/**
 * @brief Validate bson object.
 * Set the bs->err bitmask as validation result.
 * 
 * @param bs Bson object to be validated.
 * @param checkdots Check what keys contain dot(.) characters
 * @param checkdollar Check what keys contain dollar($) characters
 * @return BSON_OK if all checks passed otherwise return BSON_ERROR
 */
EJDB_EXPORT int bson_validate(bson *bs, bool checkdots, bool checkdollar);


EJDB_EXTERN_C_END
#endif