1 /*
2  * Copyright 2013-2014 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 #include "bson-iter.h"
19 #include "bson-config.h"
20 #include "bson-decimal128.h"
21 
22 
23 #define ITER_TYPE(i) ((bson_type_t) *((i)->raw + (i)->type))
24 
25 
26 /*
27  *--------------------------------------------------------------------------
28  *
29  * bson_iter_init --
30  *
31  *       Initializes @iter to be used to iterate @bson.
32  *
33  * Returns:
34  *       true if bson_iter_t was initialized. otherwise false.
35  *
36  * Side effects:
37  *       @iter is initialized.
38  *
39  *--------------------------------------------------------------------------
40  */
41 
42 bool
bson_iter_init(bson_iter_t * iter,const bson_t * bson)43 bson_iter_init (bson_iter_t  *iter, /* OUT */
44                 const bson_t *bson) /* IN */
45 {
46    BSON_ASSERT (iter);
47    BSON_ASSERT (bson);
48 
49    if (BSON_UNLIKELY (bson->len < 5)) {
50       memset (iter, 0, sizeof *iter);
51       return false;
52    }
53 
54    iter->raw = bson_get_data (bson);
55    iter->len = bson->len;
56    iter->off = 0;
57    iter->type = 0;
58    iter->key = 0;
59    iter->d1 = 0;
60    iter->d2 = 0;
61    iter->d3 = 0;
62    iter->d4 = 0;
63    iter->next_off = 4;
64    iter->err_off = 0;
65 
66    return true;
67 }
68 
69 
70 /*
71  *--------------------------------------------------------------------------
72  *
73  * bson_iter_recurse --
74  *
75  *       Creates a new sub-iter looking at the document or array that @iter
76  *       is currently pointing at.
77  *
78  * Returns:
79  *       true if successful and @child was initialized.
80  *
81  * Side effects:
82  *       @child is initialized.
83  *
84  *--------------------------------------------------------------------------
85  */
86 
87 bool
bson_iter_recurse(const bson_iter_t * iter,bson_iter_t * child)88 bson_iter_recurse (const bson_iter_t *iter,  /* IN */
89                    bson_iter_t       *child) /* OUT */
90 {
91    const uint8_t *data = NULL;
92    uint32_t len = 0;
93 
94    BSON_ASSERT (iter);
95    BSON_ASSERT (child);
96 
97    if (ITER_TYPE (iter) == BSON_TYPE_DOCUMENT) {
98       bson_iter_document (iter, &len, &data);
99    } else if (ITER_TYPE (iter) == BSON_TYPE_ARRAY) {
100       bson_iter_array (iter, &len, &data);
101    } else {
102       return false;
103    }
104 
105    child->raw = data;
106    child->len = len;
107    child->off = 0;
108    child->type = 0;
109    child->key = 0;
110    child->d1 = 0;
111    child->d2 = 0;
112    child->d3 = 0;
113    child->d4 = 0;
114    child->next_off = 4;
115    child->err_off = 0;
116 
117    return true;
118 }
119 
120 
121 /*
122  *--------------------------------------------------------------------------
123  *
124  * bson_iter_init_find --
125  *
126  *       Initializes a #bson_iter_t and moves the iter to the first field
127  *       matching @key.
128  *
129  * Returns:
130  *       true if the field named @key was found; otherwise false.
131  *
132  * Side effects:
133  *       None.
134  *
135  *--------------------------------------------------------------------------
136  */
137 
138 bool
bson_iter_init_find(bson_iter_t * iter,const bson_t * bson,const char * key)139 bson_iter_init_find (bson_iter_t  *iter, /* INOUT */
140                      const bson_t *bson, /* IN */
141                      const char   *key)  /* IN */
142 {
143    BSON_ASSERT (iter);
144    BSON_ASSERT (bson);
145    BSON_ASSERT (key);
146 
147    return bson_iter_init (iter, bson) && bson_iter_find (iter, key);
148 }
149 
150 
151 /*
152  *--------------------------------------------------------------------------
153  *
154  * bson_iter_init_find_case --
155  *
156  *       A case-insensitive version of bson_iter_init_find().
157  *
158  * Returns:
159  *       true if the field was found and @iter is observing that field.
160  *
161  * Side effects:
162  *       None.
163  *
164  *--------------------------------------------------------------------------
165  */
166 
167 bool
bson_iter_init_find_case(bson_iter_t * iter,const bson_t * bson,const char * key)168 bson_iter_init_find_case (bson_iter_t  *iter, /* INOUT */
169                           const bson_t *bson, /* IN */
170                           const char   *key)  /* IN */
171 {
172    BSON_ASSERT (iter);
173    BSON_ASSERT (bson);
174    BSON_ASSERT (key);
175 
176    return bson_iter_init (iter, bson) && bson_iter_find_case (iter, key);
177 }
178 
179 
180 /*
181  *--------------------------------------------------------------------------
182  *
183  * _bson_iter_find_with_len --
184  *
185  *       Internal helper for finding an exact key.
186  *
187  * Returns:
188  *       true if the field @key was found.
189  *
190  * Side effects:
191  *       None.
192  *
193  *--------------------------------------------------------------------------
194  */
195 
196 static bool
_bson_iter_find_with_len(bson_iter_t * iter,const char * key,int keylen)197 _bson_iter_find_with_len (bson_iter_t *iter,   /* INOUT */
198                           const char  *key,    /* IN */
199                           int          keylen) /* IN */
200 {
201    const char *ikey;
202 
203    if (keylen == 0) {
204       return false;
205    }
206 
207    if (keylen < 0) {
208       keylen = (int)strlen (key);
209    }
210 
211    while (bson_iter_next (iter)) {
212       ikey = bson_iter_key (iter);
213 
214       if ((0 == strncmp (key, ikey, keylen)) && (ikey [keylen] == '\0')) {
215          return true;
216       }
217    }
218 
219    return false;
220 }
221 
222 
223 /*
224  *--------------------------------------------------------------------------
225  *
226  * bson_iter_find --
227  *
228  *       Searches through @iter starting from the current position for a key
229  *       matching @key. This is a case-sensitive search meaning "KEY" and
230  *       "key" would NOT match.
231  *
232  * Returns:
233  *       true if @key is found.
234  *
235  * Side effects:
236  *       None.
237  *
238  *--------------------------------------------------------------------------
239  */
240 
241 bool
bson_iter_find(bson_iter_t * iter,const char * key)242 bson_iter_find (bson_iter_t *iter, /* INOUT */
243                 const char  *key)  /* IN */
244 {
245    BSON_ASSERT (iter);
246    BSON_ASSERT (key);
247 
248    return _bson_iter_find_with_len (iter, key, -1);
249 }
250 
251 
252 /*
253  *--------------------------------------------------------------------------
254  *
255  * bson_iter_find_case --
256  *
257  *       Searches through @iter starting from the current position for a key
258  *       matching @key. This is a case-insensitive search meaning "KEY" and
259  *       "key" would match.
260  *
261  * Returns:
262  *       true if @key is found.
263  *
264  * Side effects:
265  *       None.
266  *
267  *--------------------------------------------------------------------------
268  */
269 
270 bool
bson_iter_find_case(bson_iter_t * iter,const char * key)271 bson_iter_find_case (bson_iter_t *iter, /* INOUT */
272                      const char  *key)  /* IN */
273 {
274    BSON_ASSERT (iter);
275    BSON_ASSERT (key);
276 
277    while (bson_iter_next (iter)) {
278 #ifdef BSON_OS_WIN32
279       if (!_stricmp(key, bson_iter_key (iter))) {
280 #else
281       if (!strcasecmp (key, bson_iter_key (iter))) {
282 #endif
283          return true;
284       }
285    }
286 
287    return false;
288 }
289 
290 
291 /*
292  *--------------------------------------------------------------------------
293  *
294  * bson_iter_find_descendant --
295  *
296  *       Locates a descendant using the "parent.child.key" notation. This
297  *       operates similar to bson_iter_find() except that it can recurse
298  *       into children documents using the dot notation.
299  *
300  * Returns:
301  *       true if the descendant was found and @descendant was initialized.
302  *
303  * Side effects:
304  *       @descendant may be initialized.
305  *
306  *--------------------------------------------------------------------------
307  */
308 
309 bool
310 bson_iter_find_descendant (bson_iter_t *iter,       /* INOUT */
311                            const char  *dotkey,     /* IN */
312                            bson_iter_t *descendant) /* OUT */
313 {
314    bson_iter_t tmp;
315    const char *dot;
316    size_t sublen;
317 
318    BSON_ASSERT (iter);
319    BSON_ASSERT (dotkey);
320    BSON_ASSERT (descendant);
321 
322    if ((dot = strchr (dotkey, '.'))) {
323       sublen = dot - dotkey;
324    } else {
325       sublen = strlen (dotkey);
326    }
327 
328    if (_bson_iter_find_with_len (iter, dotkey, (int)sublen)) {
329       if (!dot) {
330          *descendant = *iter;
331          return true;
332       }
333 
334       if (BSON_ITER_HOLDS_DOCUMENT (iter) || BSON_ITER_HOLDS_ARRAY (iter)) {
335          if (bson_iter_recurse (iter, &tmp)) {
336             return bson_iter_find_descendant (&tmp, dot + 1, descendant);
337          }
338       }
339    }
340 
341    return false;
342 }
343 
344 
345 /*
346  *--------------------------------------------------------------------------
347  *
348  * bson_iter_key --
349  *
350  *       Retrieves the key of the current field. The resulting key is valid
351  *       while @iter is valid.
352  *
353  * Returns:
354  *       A string that should not be modified or freed.
355  *
356  * Side effects:
357  *       None.
358  *
359  *--------------------------------------------------------------------------
360  */
361 
362 const char *
363 bson_iter_key (const bson_iter_t *iter) /* IN */
364 {
365    BSON_ASSERT (iter);
366 
367    return bson_iter_key_unsafe (iter);
368 }
369 
370 
371 /*
372  *--------------------------------------------------------------------------
373  *
374  * bson_iter_type --
375  *
376  *       Retrieves the type of the current field.  It may be useful to check
377  *       the type using the BSON_ITER_HOLDS_*() macros.
378  *
379  * Returns:
380  *       A bson_type_t.
381  *
382  * Side effects:
383  *       None.
384  *
385  *--------------------------------------------------------------------------
386  */
387 
388 bson_type_t
389 bson_iter_type (const bson_iter_t *iter) /* IN */
390 {
391    BSON_ASSERT (iter);
392    BSON_ASSERT (iter->raw);
393    BSON_ASSERT (iter->len);
394 
395    return bson_iter_type_unsafe (iter);
396 }
397 
398 
399 /*
400  *--------------------------------------------------------------------------
401  *
402  * _bson_iter_next_internal --
403  *
404  *       Internal function to advance @iter to the next field and retrieve
405  *       the key and BSON type before error-checking.
406  *
407  * Return:
408  *       true if an element was decoded, else false.
409  *
410  * Side effects:
411  *       @key and @bson_type are set.
412  *
413  *       If the return value is false:
414  *        - @iter is invalidated: @iter->raw is NULLed
415  *        - @unsupported is set to true if the bson type is unsupported
416  *        - otherwise if the BSON is corrupt, @iter->err_off is nonzero
417  *        - otherwise @bson_type is set to BSON_TYPE_EOD
418  *
419  *--------------------------------------------------------------------------
420  */
421 
422 static bool
423 _bson_iter_next_internal (bson_iter_t  *iter,         /* INOUT */
424                           const char  **key,          /* OUT */
425                           uint32_t     *bson_type,    /* OUT */
426                           bool         *unsupported)  /* OUT */
427 {
428    const uint8_t *data;
429    uint32_t o;
430    unsigned int len;
431 
432    BSON_ASSERT (iter);
433 
434    *unsupported = false;
435 
436    if (!iter->raw) {
437       *key = NULL;
438       *bson_type = BSON_TYPE_EOD;
439       return false;
440    }
441 
442    data = iter->raw;
443    len = iter->len;
444 
445    iter->off = iter->next_off;
446    iter->type = iter->off;
447    iter->key = iter->off + 1;
448    iter->d1 = 0;
449    iter->d2 = 0;
450    iter->d3 = 0;
451    iter->d4 = 0;
452 
453    for (o = iter->off + 1; o < len; o++) {
454       if (!data [o]) {
455          iter->d1 = ++o;
456          goto fill_data_fields;
457       }
458    }
459 
460    goto mark_invalid;
461 
462 fill_data_fields:
463 
464    *key = bson_iter_key_unsafe (iter);
465    *bson_type = ITER_TYPE (iter);
466 
467    switch (*bson_type) {
468    case BSON_TYPE_DATE_TIME:
469    case BSON_TYPE_DOUBLE:
470    case BSON_TYPE_INT64:
471    case BSON_TYPE_TIMESTAMP:
472       iter->next_off = o + 8;
473       break;
474    case BSON_TYPE_CODE:
475    case BSON_TYPE_SYMBOL:
476    case BSON_TYPE_UTF8:
477       {
478          uint32_t l;
479 
480          if ((o + 4) >= len) {
481             iter->err_off = o;
482             goto mark_invalid;
483          }
484 
485          iter->d2 = o + 4;
486          memcpy (&l, iter->raw + iter->d1, sizeof (l));
487          l = BSON_UINT32_FROM_LE (l);
488 
489          if (l > (len - (o + 4))) {
490             iter->err_off = o;
491             goto mark_invalid;
492          }
493 
494          iter->next_off = o + 4 + l;
495 
496          /*
497           * Make sure the string length includes the NUL byte.
498           */
499          if (BSON_UNLIKELY ((l == 0) || (iter->next_off >= len))) {
500             iter->err_off = o;
501             goto mark_invalid;
502          }
503 
504          /*
505           * Make sure the last byte is a NUL byte.
506           */
507          if (BSON_UNLIKELY ((iter->raw + iter->d2)[l - 1] != '\0')) {
508             iter->err_off = o + 4 + l - 1;
509             goto mark_invalid;
510          }
511       }
512       break;
513    case BSON_TYPE_BINARY:
514       {
515          bson_subtype_t subtype;
516          uint32_t l;
517 
518          if (o >= (len - 4)) {
519             iter->err_off = o;
520             goto mark_invalid;
521          }
522 
523          iter->d2 = o + 4;
524          iter->d3 = o + 5;
525 
526          memcpy (&l, iter->raw + iter->d1, sizeof (l));
527          l = BSON_UINT32_FROM_LE (l);
528 
529          if (l >= (len - o)) {
530             iter->err_off = o;
531             goto mark_invalid;
532          }
533 
534          subtype = *(iter->raw + iter->d2);
535 
536          if (subtype == BSON_SUBTYPE_BINARY_DEPRECATED) {
537             if (l < 4) {
538                iter->err_off = o;
539                goto mark_invalid;
540             }
541          }
542 
543          iter->next_off = o + 5 + l;
544       }
545       break;
546    case BSON_TYPE_ARRAY:
547    case BSON_TYPE_DOCUMENT:
548       {
549          uint32_t l;
550 
551          if (o >= (len - 4)) {
552             iter->err_off = o;
553             goto mark_invalid;
554          }
555 
556          memcpy (&l, iter->raw + iter->d1, sizeof (l));
557          l = BSON_UINT32_FROM_LE (l);
558 
559          if ((l > len) || (l > (len - o))) {
560             iter->err_off = o;
561             goto mark_invalid;
562          }
563 
564          iter->next_off = o + l;
565       }
566       break;
567    case BSON_TYPE_OID:
568       iter->next_off = o + 12;
569       break;
570    case BSON_TYPE_BOOL:
571       {
572           char val;
573           memcpy (&val, iter->raw + iter->d1, 1);
574           if ( val != 0x00 && val != 0x01 ) {
575               iter->err_off = o;
576               goto mark_invalid;
577           }
578 
579           iter->next_off = o + 1;
580       }
581       break;
582    case BSON_TYPE_REGEX:
583       {
584          bool eor = false;
585          bool eoo = false;
586 
587          for (; o < len; o++) {
588             if (!data [o]) {
589                iter->d2 = ++o;
590                eor = true;
591                break;
592             }
593          }
594 
595          if (!eor) {
596             iter->err_off = iter->next_off;
597             goto mark_invalid;
598          }
599 
600          for (; o < len; o++) {
601             if (!data [o]) {
602                eoo = true;
603                break;
604             }
605          }
606 
607          if (!eoo) {
608             iter->err_off = iter->next_off;
609             goto mark_invalid;
610          }
611 
612          iter->next_off = o + 1;
613       }
614       break;
615    case BSON_TYPE_DBPOINTER:
616       {
617          uint32_t l;
618 
619          if (o >= (len - 4)) {
620             iter->err_off = o;
621             goto mark_invalid;
622          }
623 
624          iter->d2 = o + 4;
625          memcpy (&l, iter->raw + iter->d1, sizeof (l));
626          l = BSON_UINT32_FROM_LE (l);
627 
628          if ((l > len) || (l > (len - o))) {
629             iter->err_off = o;
630             goto mark_invalid;
631          }
632 
633          if ( *(iter->raw + o + l + 3) ) {
634             /* not null terminated */
635             iter->err_off = o + l + 3;
636             goto mark_invalid;
637          }
638 
639          iter->d3 = o + 4 + l;
640          iter->next_off = o + 4 + l + 12;
641       }
642       break;
643    case BSON_TYPE_CODEWSCOPE:
644       {
645          uint32_t l;
646          uint32_t doclen;
647 
648          if ((len < 19) || (o >= (len - 14))) {
649             iter->err_off = o;
650             goto mark_invalid;
651          }
652 
653          iter->d2 = o + 4;
654          iter->d3 = o + 8;
655 
656          memcpy (&l, iter->raw + iter->d1, sizeof (l));
657          l = BSON_UINT32_FROM_LE (l);
658 
659          if ((l < 14) || (l >= (len - o))) {
660             iter->err_off = o;
661             goto mark_invalid;
662          }
663 
664          iter->next_off = o + l;
665 
666          if (iter->next_off >= len) {
667             iter->err_off = o;
668             goto mark_invalid;
669          }
670 
671          memcpy (&l, iter->raw + iter->d2, sizeof (l));
672          l = BSON_UINT32_FROM_LE (l);
673 
674          if (l >= (len - o - 4 - 4)) {
675             iter->err_off = o;
676             goto mark_invalid;
677          }
678 
679          if ((o + 4 + 4 + l + 4) >= iter->next_off) {
680             iter->err_off = o + 4;
681             goto mark_invalid;
682          }
683 
684          iter->d4 = o + 4 + 4 + l;
685          memcpy (&doclen, iter->raw + iter->d4, sizeof (doclen));
686          doclen = BSON_UINT32_FROM_LE (doclen);
687 
688          if ((o + 4 + 4 + l + doclen) != iter->next_off) {
689             iter->err_off = o + 4 + 4 + l;
690             goto mark_invalid;
691          }
692       }
693       break;
694    case BSON_TYPE_INT32:
695       iter->next_off = o + 4;
696       break;
697    case BSON_TYPE_DECIMAL128:
698       iter->next_off = o + 16;
699       break;
700    case BSON_TYPE_MAXKEY:
701    case BSON_TYPE_MINKEY:
702    case BSON_TYPE_NULL:
703    case BSON_TYPE_UNDEFINED:
704       iter->d1 = -1;
705       iter->next_off = o;
706       break;
707    default:
708       *unsupported = true;
709       /* FALL THROUGH */
710    case BSON_TYPE_EOD:
711       iter->err_off = o;
712       goto mark_invalid;
713    }
714 
715    /*
716     * Check to see if any of the field locations would overflow the
717     * current BSON buffer. If so, set the error location to the offset
718     * of where the field starts.
719     */
720    if (iter->next_off >= len) {
721       iter->err_off = o;
722       goto mark_invalid;
723    }
724 
725    iter->err_off = 0;
726 
727    return true;
728 
729 mark_invalid:
730    iter->raw = NULL;
731    iter->len = 0;
732    iter->next_off = 0;
733 
734    return false;
735 }
736 
737 
738 /*
739  *--------------------------------------------------------------------------
740  *
741  * bson_iter_next --
742  *
743  *       Advances @iter to the next field of the underlying BSON document.
744  *       If all fields have been exhausted, then %false is returned.
745  *
746  *       It is a programming error to use @iter after this function has
747  *       returned false.
748  *
749  * Returns:
750  *       true if the iter was advanced to the next record.
751  *       otherwise false and @iter should be considered invalid.
752  *
753  * Side effects:
754  *       @iter may be invalidated.
755  *
756  *--------------------------------------------------------------------------
757  */
758 
759 bool
760 bson_iter_next (bson_iter_t *iter) /* INOUT */
761 {
762    uint32_t bson_type;
763    const char *key;
764    bool unsupported;
765 
766    return _bson_iter_next_internal (iter, &key, &bson_type, &unsupported);
767 }
768 
769 
770 /*
771  *--------------------------------------------------------------------------
772  *
773  * bson_iter_binary --
774  *
775  *       Retrieves the BSON_TYPE_BINARY field. The subtype is stored in
776  *       @subtype.  The length of @binary in bytes is stored in @binary_len.
777  *
778  *       @binary should not be modified or freed and is only valid while
779  *       @iter's bson_t is valid and unmodified.
780  *
781  * Parameters:
782  *       @iter: A bson_iter_t
783  *       @subtype: A location for the binary subtype.
784  *       @binary_len: A location for the length of @binary.
785  *       @binary: A location for a pointer to the binary data.
786  *
787  * Returns:
788  *       None.
789  *
790  * Side effects:
791  *       None.
792  *
793  *--------------------------------------------------------------------------
794  */
795 
796 void
797 bson_iter_binary (const bson_iter_t  *iter,        /* IN */
798                   bson_subtype_t     *subtype,     /* OUT */
799                   uint32_t           *binary_len,  /* OUT */
800                   const uint8_t     **binary)      /* OUT */
801 {
802    bson_subtype_t backup;
803 
804    BSON_ASSERT (iter);
805    BSON_ASSERT (!binary || binary_len);
806 
807    if (ITER_TYPE (iter) == BSON_TYPE_BINARY) {
808       if (!subtype) {
809          subtype = &backup;
810       }
811 
812       *subtype = (bson_subtype_t) *(iter->raw + iter->d2);
813 
814       if (binary) {
815          memcpy (binary_len, (iter->raw + iter->d1), sizeof (*binary_len));
816          *binary_len = BSON_UINT32_FROM_LE (*binary_len);
817          *binary = iter->raw + iter->d3;
818 
819          if (*subtype == BSON_SUBTYPE_BINARY_DEPRECATED) {
820             *binary_len -= sizeof (int32_t);
821             *binary += sizeof (int32_t);
822          }
823       }
824 
825       return;
826    }
827 
828    if (binary) {
829       *binary = NULL;
830    }
831 
832    if (binary_len) {
833       *binary_len = 0;
834    }
835 
836    if (subtype) {
837       *subtype = BSON_SUBTYPE_BINARY;
838    }
839 }
840 
841 
842 /*
843  *--------------------------------------------------------------------------
844  *
845  * bson_iter_bool --
846  *
847  *       Retrieves the current field of type BSON_TYPE_BOOL.
848  *
849  * Returns:
850  *       true or false, dependent on bson document.
851  *
852  * Side effects:
853  *       None.
854  *
855  *--------------------------------------------------------------------------
856  */
857 
858 bool
859 bson_iter_bool (const bson_iter_t *iter) /* IN */
860 {
861    BSON_ASSERT (iter);
862 
863    if (ITER_TYPE (iter) == BSON_TYPE_BOOL) {
864       return bson_iter_bool_unsafe (iter);
865    }
866 
867    return false;
868 }
869 
870 
871 /*
872  *--------------------------------------------------------------------------
873  *
874  * bson_iter_as_bool --
875  *
876  *       If @iter is on a boolean field, returns the boolean. If it is on a
877  *       non-boolean field such as int32, int64, or double, it will convert
878  *       the value to a boolean.
879  *
880  *       Zero is false, and non-zero is true.
881  *
882  * Returns:
883  *       true or false, dependent on field type.
884  *
885  * Side effects:
886  *       None.
887  *
888  *--------------------------------------------------------------------------
889  */
890 
891 bool
892 bson_iter_as_bool (const bson_iter_t *iter) /* IN */
893 {
894    BSON_ASSERT (iter);
895 
896    switch ((int)ITER_TYPE (iter)) {
897    case BSON_TYPE_BOOL:
898       return bson_iter_bool (iter);
899    case BSON_TYPE_DOUBLE:
900       return !(bson_iter_double (iter) == 0.0);
901    case BSON_TYPE_INT64:
902       return !(bson_iter_int64 (iter) == 0);
903    case BSON_TYPE_INT32:
904       return !(bson_iter_int32 (iter) == 0);
905    case BSON_TYPE_UTF8:
906       return true;
907    case BSON_TYPE_NULL:
908    case BSON_TYPE_UNDEFINED:
909       return false;
910    default:
911       return true;
912    }
913 }
914 
915 
916 /*
917  *--------------------------------------------------------------------------
918  *
919  * bson_iter_double --
920  *
921  *       Retrieves the current field of type BSON_TYPE_DOUBLE.
922  *
923  * Returns:
924  *       A double.
925  *
926  * Side effects:
927  *       None.
928  *
929  *--------------------------------------------------------------------------
930  */
931 
932 double
933 bson_iter_double (const bson_iter_t *iter) /* IN */
934 {
935    BSON_ASSERT (iter);
936 
937    if (ITER_TYPE (iter) == BSON_TYPE_DOUBLE) {
938       return bson_iter_double_unsafe (iter);
939    }
940 
941    return 0;
942 }
943 
944 
945 /*
946  *--------------------------------------------------------------------------
947  *
948  * bson_iter_int32 --
949  *
950  *       Retrieves the value of the field of type BSON_TYPE_INT32.
951  *
952  * Returns:
953  *       A 32-bit signed integer.
954  *
955  * Side effects:
956  *       None.
957  *
958  *--------------------------------------------------------------------------
959  */
960 
961 int32_t
962 bson_iter_int32 (const bson_iter_t *iter) /* IN */
963 {
964    BSON_ASSERT (iter);
965 
966    if (ITER_TYPE (iter) == BSON_TYPE_INT32) {
967       return bson_iter_int32_unsafe (iter);
968    }
969 
970    return 0;
971 }
972 
973 
974 /*
975  *--------------------------------------------------------------------------
976  *
977  * bson_iter_int64 --
978  *
979  *       Retrieves a 64-bit signed integer for the current BSON_TYPE_INT64
980  *       field.
981  *
982  * Returns:
983  *       A 64-bit signed integer.
984  *
985  * Side effects:
986  *       None.
987  *
988  *--------------------------------------------------------------------------
989  */
990 
991 int64_t
992 bson_iter_int64 (const bson_iter_t *iter) /* IN */
993 {
994    BSON_ASSERT (iter);
995 
996    if (ITER_TYPE (iter) == BSON_TYPE_INT64) {
997       return bson_iter_int64_unsafe (iter);
998    }
999 
1000    return 0;
1001 }
1002 
1003 
1004 /*
1005  *--------------------------------------------------------------------------
1006  *
1007  * bson_iter_as_int64 --
1008  *
1009  *       If @iter is not an int64 field, it will try to convert the value to
1010  *       an int64. Such field types include:
1011  *
1012  *        - bool
1013  *        - double
1014  *        - int32
1015  *
1016  * Returns:
1017  *       An int64_t.
1018  *
1019  * Side effects:
1020  *       None.
1021  *
1022  *--------------------------------------------------------------------------
1023  */
1024 
1025 int64_t
1026 bson_iter_as_int64 (const bson_iter_t *iter) /* IN */
1027 {
1028    BSON_ASSERT (iter);
1029 
1030    switch ((int)ITER_TYPE (iter)) {
1031    case BSON_TYPE_BOOL:
1032       return (int64_t)bson_iter_bool (iter);
1033    case BSON_TYPE_DOUBLE:
1034       return (int64_t)bson_iter_double (iter);
1035    case BSON_TYPE_INT64:
1036       return bson_iter_int64 (iter);
1037    case BSON_TYPE_INT32:
1038       return (int64_t)bson_iter_int32 (iter);
1039    default:
1040       return 0;
1041    }
1042 }
1043 
1044 
1045 /*
1046  *--------------------------------------------------------------------------
1047  *
1048  * bson_iter_decimal128 --
1049  *
1050  *       This function retrieves the current field of type %BSON_TYPE_DECIMAL128.
1051  *       The result is valid while @iter is valid, and is stored in @dec.
1052  *
1053  * Returns:
1054  *
1055  *       True on success, false on failure.
1056  *
1057  * Side Effects:
1058  *    None.
1059  *
1060  *--------------------------------------------------------------------------
1061  */
1062 bool
1063 bson_iter_decimal128 (const bson_iter_t *iter,      /* IN */
1064                       bson_decimal128_t *dec)       /* OUT */
1065 {
1066    BSON_ASSERT (iter);
1067 
1068    if (ITER_TYPE (iter) == BSON_TYPE_DECIMAL128) {
1069       bson_iter_decimal128_unsafe (iter, dec);
1070       return true;
1071    }
1072 
1073    return false;
1074 }
1075 
1076 
1077 /*
1078  *--------------------------------------------------------------------------
1079  *
1080  * bson_iter_oid --
1081  *
1082  *       Retrieves the current field of type %BSON_TYPE_OID. The result is
1083  *       valid while @iter is valid.
1084  *
1085  * Returns:
1086  *       A bson_oid_t that should not be modified or freed.
1087  *
1088  * Side effects:
1089  *       None.
1090  *
1091  *--------------------------------------------------------------------------
1092  */
1093 
1094 const bson_oid_t *
1095 bson_iter_oid (const bson_iter_t *iter) /* IN */
1096 {
1097    BSON_ASSERT (iter);
1098 
1099    if (ITER_TYPE (iter) == BSON_TYPE_OID) {
1100       return bson_iter_oid_unsafe (iter);
1101    }
1102 
1103    return NULL;
1104 }
1105 
1106 
1107 /*
1108  *--------------------------------------------------------------------------
1109  *
1110  * bson_iter_regex --
1111  *
1112  *       Fetches the current field from the iter which should be of type
1113  *       BSON_TYPE_REGEX.
1114  *
1115  * Returns:
1116  *       Regex from @iter. This should not be modified or freed.
1117  *
1118  * Side effects:
1119  *       None.
1120  *
1121  *--------------------------------------------------------------------------
1122  */
1123 
1124 const char *
1125 bson_iter_regex (const bson_iter_t *iter,    /* IN */
1126                  const char       **options) /* IN */
1127 {
1128    const char *ret = NULL;
1129    const char *ret_options = NULL;
1130 
1131    BSON_ASSERT (iter);
1132 
1133    if (ITER_TYPE (iter) == BSON_TYPE_REGEX) {
1134       ret = (const char *)(iter->raw + iter->d1);
1135       ret_options = (const char *)(iter->raw + iter->d2);
1136    }
1137 
1138    if (options) {
1139       *options = ret_options;
1140    }
1141 
1142    return ret;
1143 }
1144 
1145 
1146 /*
1147  *--------------------------------------------------------------------------
1148  *
1149  * bson_iter_utf8 --
1150  *
1151  *       Retrieves the current field of type %BSON_TYPE_UTF8 as a UTF-8
1152  *       encoded string.
1153  *
1154  * Parameters:
1155  *       @iter: A bson_iter_t.
1156  *       @length: A location for the length of the string.
1157  *
1158  * Returns:
1159  *       A string that should not be modified or freed.
1160  *
1161  * Side effects:
1162  *       @length will be set to the result strings length if non-NULL.
1163  *
1164  *--------------------------------------------------------------------------
1165  */
1166 
1167 const char *
1168 bson_iter_utf8 (const bson_iter_t *iter,   /* IN */
1169                 uint32_t          *length) /* OUT */
1170 {
1171    BSON_ASSERT (iter);
1172 
1173    if (ITER_TYPE (iter) == BSON_TYPE_UTF8) {
1174       if (length) {
1175          *length = bson_iter_utf8_len_unsafe (iter);
1176       }
1177 
1178       return (const char *)(iter->raw + iter->d2);
1179    }
1180 
1181    if (length) {
1182       *length = 0;
1183    }
1184 
1185    return NULL;
1186 }
1187 
1188 
1189 /*
1190  *--------------------------------------------------------------------------
1191  *
1192  * bson_iter_dup_utf8 --
1193  *
1194  *       Copies the current UTF-8 element into a newly allocated string. The
1195  *       string should be freed using bson_free() when the caller is
1196  *       finished with it.
1197  *
1198  * Returns:
1199  *       A newly allocated char* that should be freed with bson_free().
1200  *
1201  * Side effects:
1202  *       @length will be set to the result strings length if non-NULL.
1203  *
1204  *--------------------------------------------------------------------------
1205  */
1206 
1207 char *
1208 bson_iter_dup_utf8 (const bson_iter_t *iter,   /* IN */
1209                     uint32_t          *length) /* OUT */
1210 {
1211    uint32_t local_length = 0;
1212    const char *str;
1213    char *ret = NULL;
1214 
1215    BSON_ASSERT (iter);
1216 
1217    if ((str = bson_iter_utf8 (iter, &local_length))) {
1218       ret = bson_malloc0 (local_length + 1);
1219       memcpy (ret, str, local_length);
1220       ret[local_length] = '\0';
1221    }
1222 
1223    if (length) {
1224       *length = local_length;
1225    }
1226 
1227    return ret;
1228 }
1229 
1230 
1231 /*
1232  *--------------------------------------------------------------------------
1233  *
1234  * bson_iter_code --
1235  *
1236  *       Retrieves the current field of type %BSON_TYPE_CODE. The length of
1237  *       the resulting string is stored in @length.
1238  *
1239  * Parameters:
1240  *       @iter: A bson_iter_t.
1241  *       @length: A location for the code length.
1242  *
1243  * Returns:
1244  *       A NUL-terminated string containing the code which should not be
1245  *       modified or freed.
1246  *
1247  * Side effects:
1248  *       None.
1249  *
1250  *--------------------------------------------------------------------------
1251  */
1252 
1253 const char *
1254 bson_iter_code (const bson_iter_t *iter,   /* IN */
1255                 uint32_t          *length) /* OUT */
1256 {
1257    BSON_ASSERT (iter);
1258 
1259    if (ITER_TYPE (iter) == BSON_TYPE_CODE) {
1260       if (length) {
1261          *length = bson_iter_utf8_len_unsafe (iter);
1262       }
1263 
1264       return (const char *)(iter->raw + iter->d2);
1265    }
1266 
1267    if (length) {
1268       *length = 0;
1269    }
1270 
1271    return NULL;
1272 }
1273 
1274 
1275 /*
1276  *--------------------------------------------------------------------------
1277  *
1278  * bson_iter_codewscope --
1279  *
1280  *       Similar to bson_iter_code() but with a scope associated encoded as
1281  *       a BSON document. @scope should not be modified or freed. It is
1282  *       valid while @iter is valid.
1283  *
1284  * Parameters:
1285  *       @iter: A #bson_iter_t.
1286  *       @length: A location for the length of resulting string.
1287  *       @scope_len: A location for the length of @scope.
1288  *       @scope: A location for the scope encoded as BSON.
1289  *
1290  * Returns:
1291  *       A NUL-terminated string that should not be modified or freed.
1292  *
1293  * Side effects:
1294  *       @length is set to the resulting string length in bytes.
1295  *       @scope_len is set to the length of @scope in bytes.
1296  *       @scope is set to the scope documents buffer which can be
1297  *       turned into a bson document with bson_init_static().
1298  *
1299  *--------------------------------------------------------------------------
1300  */
1301 
1302 const char *
1303 bson_iter_codewscope (const bson_iter_t  *iter,      /* IN */
1304                       uint32_t           *length,    /* OUT */
1305                       uint32_t           *scope_len, /* OUT */
1306                       const uint8_t     **scope)     /* OUT */
1307 {
1308    uint32_t len;
1309 
1310    BSON_ASSERT (iter);
1311 
1312    if (ITER_TYPE (iter) == BSON_TYPE_CODEWSCOPE) {
1313       if (length) {
1314          memcpy (&len, iter->raw + iter->d2, sizeof (len));
1315          *length = BSON_UINT32_FROM_LE (len) - 1;
1316       }
1317 
1318       memcpy (&len, iter->raw + iter->d4, sizeof (len));
1319       *scope_len = BSON_UINT32_FROM_LE (len);
1320       *scope = iter->raw + iter->d4;
1321       return (const char *)(iter->raw + iter->d3);
1322    }
1323 
1324    if (length) {
1325       *length = 0;
1326    }
1327 
1328    if (scope_len) {
1329       *scope_len = 0;
1330    }
1331 
1332    if (scope) {
1333       *scope = NULL;
1334    }
1335 
1336    return NULL;
1337 }
1338 
1339 
1340 /*
1341  *--------------------------------------------------------------------------
1342  *
1343  * bson_iter_dbpointer --
1344  *
1345  *       Retrieves a BSON_TYPE_DBPOINTER field. @collection_len will be set
1346  *       to the length of the collection name. The collection name will be
1347  *       placed into @collection. The oid will be placed into @oid.
1348  *
1349  *       @collection and @oid should not be modified.
1350  *
1351  * Parameters:
1352  *       @iter: A #bson_iter_t.
1353  *       @collection_len: A location for the length of @collection.
1354  *       @collection: A location for the collection name.
1355  *       @oid: A location for the oid.
1356  *
1357  * Returns:
1358  *       None.
1359  *
1360  * Side effects:
1361  *       @collection_len is set to the length of @collection in bytes
1362  *       excluding the null byte.
1363  *       @collection is set to the collection name, including a terminating
1364  *       null byte.
1365  *       @oid is initialized with the oid.
1366  *
1367  *--------------------------------------------------------------------------
1368  */
1369 
1370 void
1371 bson_iter_dbpointer (const bson_iter_t  *iter,           /* IN */
1372                      uint32_t           *collection_len, /* OUT */
1373                      const char        **collection,     /* OUT */
1374                      const bson_oid_t  **oid)            /* OUT */
1375 {
1376    BSON_ASSERT (iter);
1377 
1378    if (collection) {
1379       *collection = NULL;
1380    }
1381 
1382    if (oid) {
1383       *oid = NULL;
1384    }
1385 
1386    if (ITER_TYPE (iter) == BSON_TYPE_DBPOINTER) {
1387       if (collection_len) {
1388          memcpy (collection_len, (iter->raw + iter->d1), sizeof (*collection_len));
1389          *collection_len = BSON_UINT32_FROM_LE (*collection_len);
1390 
1391          if ((*collection_len) > 0) {
1392             (*collection_len)--;
1393          }
1394       }
1395 
1396       if (collection) {
1397          *collection = (const char *)(iter->raw + iter->d2);
1398       }
1399 
1400       if (oid) {
1401          *oid = (const bson_oid_t *)(iter->raw + iter->d3);
1402       }
1403    }
1404 }
1405 
1406 
1407 /*
1408  *--------------------------------------------------------------------------
1409  *
1410  * bson_iter_symbol --
1411  *
1412  *       Retrieves the symbol of the current field of type BSON_TYPE_SYMBOL.
1413  *
1414  * Parameters:
1415  *       @iter: A bson_iter_t.
1416  *       @length: A location for the length of the symbol.
1417  *
1418  * Returns:
1419  *       A string containing the symbol as UTF-8. The value should not be
1420  *       modified or freed.
1421  *
1422  * Side effects:
1423  *       @length is set to the resulting strings length in bytes,
1424  *       excluding the null byte.
1425  *
1426  *--------------------------------------------------------------------------
1427  */
1428 
1429 const char *
1430 bson_iter_symbol (const bson_iter_t *iter,   /* IN */
1431                   uint32_t          *length) /* OUT */
1432 {
1433    const char *ret = NULL;
1434    uint32_t ret_length = 0;
1435 
1436    BSON_ASSERT (iter);
1437 
1438    if (ITER_TYPE (iter) == BSON_TYPE_SYMBOL) {
1439       ret = (const char *)(iter->raw + iter->d2);
1440       ret_length = bson_iter_utf8_len_unsafe (iter);
1441    }
1442 
1443    if (length) {
1444       *length = ret_length;
1445    }
1446 
1447    return ret;
1448 }
1449 
1450 
1451 /*
1452  *--------------------------------------------------------------------------
1453  *
1454  * bson_iter_date_time --
1455  *
1456  *       Fetches the number of milliseconds elapsed since the UNIX epoch.
1457  *       This value can be negative as times before 1970 are valid.
1458  *
1459  * Returns:
1460  *       A signed 64-bit integer containing the number of milliseconds.
1461  *
1462  * Side effects:
1463  *       None.
1464  *
1465  *--------------------------------------------------------------------------
1466  */
1467 
1468 int64_t
1469 bson_iter_date_time (const bson_iter_t *iter) /* IN */
1470 {
1471    BSON_ASSERT (iter);
1472 
1473    if (ITER_TYPE (iter) == BSON_TYPE_DATE_TIME) {
1474       return bson_iter_int64_unsafe (iter);
1475    }
1476 
1477    return 0;
1478 }
1479 
1480 
1481 /*
1482  *--------------------------------------------------------------------------
1483  *
1484  * bson_iter_time_t --
1485  *
1486  *       Retrieves the current field of type BSON_TYPE_DATE_TIME as a
1487  *       time_t.
1488  *
1489  * Returns:
1490  *       A #time_t of the number of seconds since UNIX epoch in UTC.
1491  *
1492  * Side effects:
1493  *       None.
1494  *
1495  *--------------------------------------------------------------------------
1496  */
1497 
1498 time_t
1499 bson_iter_time_t (const bson_iter_t *iter) /* IN */
1500 {
1501    BSON_ASSERT (iter);
1502 
1503    if (ITER_TYPE (iter) == BSON_TYPE_DATE_TIME) {
1504       return bson_iter_time_t_unsafe (iter);
1505    }
1506 
1507    return 0;
1508 }
1509 
1510 
1511 /*
1512  *--------------------------------------------------------------------------
1513  *
1514  * bson_iter_timestamp --
1515  *
1516  *       Fetches the current field if it is a BSON_TYPE_TIMESTAMP.
1517  *
1518  * Parameters:
1519  *       @iter: A #bson_iter_t.
1520  *       @timestamp: a location for the timestamp.
1521  *       @increment: A location for the increment.
1522  *
1523  * Returns:
1524  *       None.
1525  *
1526  * Side effects:
1527  *       @timestamp is initialized.
1528  *       @increment is initialized.
1529  *
1530  *--------------------------------------------------------------------------
1531  */
1532 
1533 void
1534 bson_iter_timestamp (const bson_iter_t *iter,      /* IN */
1535                      uint32_t          *timestamp, /* OUT */
1536                      uint32_t          *increment) /* OUT */
1537 {
1538    uint64_t encoded;
1539    uint32_t ret_timestamp = 0;
1540    uint32_t ret_increment = 0;
1541 
1542    BSON_ASSERT (iter);
1543 
1544    if (ITER_TYPE (iter) == BSON_TYPE_TIMESTAMP) {
1545       memcpy (&encoded, iter->raw + iter->d1, sizeof (encoded));
1546       encoded = BSON_UINT64_FROM_LE (encoded);
1547       ret_timestamp = (encoded >> 32) & 0xFFFFFFFF;
1548       ret_increment = encoded & 0xFFFFFFFF;
1549    }
1550 
1551    if (timestamp) {
1552       *timestamp = ret_timestamp;
1553    }
1554 
1555    if (increment) {
1556       *increment = ret_increment;
1557    }
1558 }
1559 
1560 
1561 /*
1562  *--------------------------------------------------------------------------
1563  *
1564  * bson_iter_timeval --
1565  *
1566  *       Retrieves the current field of type BSON_TYPE_DATE_TIME and stores
1567  *       it into the struct timeval provided. tv->tv_sec is set to the
1568  *       number of seconds since the UNIX epoch in UTC.
1569  *
1570  *       Since BSON_TYPE_DATE_TIME does not support fractions of a second,
1571  *       tv->tv_usec will always be set to zero.
1572  *
1573  * Returns:
1574  *       None.
1575  *
1576  * Side effects:
1577  *       @tv is initialized.
1578  *
1579  *--------------------------------------------------------------------------
1580  */
1581 
1582 void
1583 bson_iter_timeval (const bson_iter_t *iter,  /* IN */
1584                    struct timeval    *tv)    /* OUT */
1585 {
1586    BSON_ASSERT (iter);
1587 
1588    if (ITER_TYPE (iter) == BSON_TYPE_DATE_TIME) {
1589       bson_iter_timeval_unsafe (iter, tv);
1590       return;
1591    }
1592 
1593    memset (tv, 0, sizeof *tv);
1594 }
1595 
1596 
1597 /**
1598  * bson_iter_document:
1599  * @iter: a bson_iter_t.
1600  * @document_len: A location for the document length.
1601  * @document: A location for a pointer to the document buffer.
1602  *
1603  */
1604 /*
1605  *--------------------------------------------------------------------------
1606  *
1607  * bson_iter_document --
1608  *
1609  *       Retrieves the data to the document BSON structure and stores the
1610  *       length of the document buffer in @document_len and the document
1611  *       buffer in @document.
1612  *
1613  *       If you would like to iterate over the child contents, you might
1614  *       consider creating a bson_t on the stack such as the following. It
1615  *       allows you to call functions taking a const bson_t* only.
1616  *
1617  *          bson_t b;
1618  *          uint32_t len;
1619  *          const uint8_t *data;
1620  *
1621  *          bson_iter_document(iter, &len, &data);
1622  *
1623  *          if (bson_init_static (&b, data, len)) {
1624  *             ...
1625  *          }
1626  *
1627  *       There is no need to cleanup the bson_t structure as no data can be
1628  *       modified in the process of its use (as it is static/const).
1629  *
1630  * Returns:
1631  *       None.
1632  *
1633  * Side effects:
1634  *       @document_len is initialized.
1635  *       @document is initialized.
1636  *
1637  *--------------------------------------------------------------------------
1638  */
1639 
1640 void
1641 bson_iter_document (const bson_iter_t  *iter,         /* IN */
1642                     uint32_t           *document_len, /* OUT */
1643                     const uint8_t     **document)     /* OUT */
1644 {
1645    BSON_ASSERT (iter);
1646    BSON_ASSERT (document_len);
1647    BSON_ASSERT (document);
1648 
1649    *document = NULL;
1650    *document_len = 0;
1651 
1652    if (ITER_TYPE (iter) == BSON_TYPE_DOCUMENT) {
1653       memcpy (document_len, (iter->raw + iter->d1), sizeof (*document_len));
1654       *document_len = BSON_UINT32_FROM_LE (*document_len);
1655       *document = (iter->raw + iter->d1);
1656    }
1657 }
1658 
1659 
1660 /**
1661  * bson_iter_array:
1662  * @iter: a #bson_iter_t.
1663  * @array_len: A location for the array length.
1664  * @array: A location for a pointer to the array buffer.
1665  */
1666 /*
1667  *--------------------------------------------------------------------------
1668  *
1669  * bson_iter_array --
1670  *
1671  *       Retrieves the data to the array BSON structure and stores the
1672  *       length of the array buffer in @array_len and the array buffer in
1673  *       @array.
1674  *
1675  *       If you would like to iterate over the child contents, you might
1676  *       consider creating a bson_t on the stack such as the following. It
1677  *       allows you to call functions taking a const bson_t* only.
1678  *
1679  *          bson_t b;
1680  *          uint32_t len;
1681  *          const uint8_t *data;
1682  *
1683  *          bson_iter_array (iter, &len, &data);
1684  *
1685  *          if (bson_init_static (&b, data, len)) {
1686  *             ...
1687  *          }
1688  *
1689  *       There is no need to cleanup the #bson_t structure as no data can be
1690  *       modified in the process of its use.
1691  *
1692  * Returns:
1693  *       None.
1694  *
1695  * Side effects:
1696  *       @array_len is initialized.
1697  *       @array is initialized.
1698  *
1699  *--------------------------------------------------------------------------
1700  */
1701 
1702 void
1703 bson_iter_array (const bson_iter_t  *iter,      /* IN */
1704                  uint32_t           *array_len, /* OUT */
1705                  const uint8_t     **array)     /* OUT */
1706 {
1707    BSON_ASSERT (iter);
1708    BSON_ASSERT (array_len);
1709    BSON_ASSERT (array);
1710 
1711    *array = NULL;
1712    *array_len = 0;
1713 
1714    if (ITER_TYPE (iter) == BSON_TYPE_ARRAY) {
1715       memcpy (array_len, (iter->raw + iter->d1), sizeof (*array_len));
1716       *array_len = BSON_UINT32_FROM_LE (*array_len);
1717       *array = (iter->raw + iter->d1);
1718    }
1719 }
1720 
1721 
1722 #define VISIT_FIELD(name) visitor->visit_##name && visitor->visit_##name
1723 #define VISIT_AFTER VISIT_FIELD (after)
1724 #define VISIT_BEFORE VISIT_FIELD (before)
1725 #define VISIT_CORRUPT if (visitor->visit_corrupt) visitor->visit_corrupt
1726 #define VISIT_DOUBLE VISIT_FIELD (double)
1727 #define VISIT_UTF8 VISIT_FIELD (utf8)
1728 #define VISIT_DOCUMENT VISIT_FIELD (document)
1729 #define VISIT_ARRAY VISIT_FIELD (array)
1730 #define VISIT_BINARY VISIT_FIELD (binary)
1731 #define VISIT_UNDEFINED VISIT_FIELD (undefined)
1732 #define VISIT_OID VISIT_FIELD (oid)
1733 #define VISIT_BOOL VISIT_FIELD (bool)
1734 #define VISIT_DATE_TIME VISIT_FIELD (date_time)
1735 #define VISIT_NULL VISIT_FIELD (null)
1736 #define VISIT_REGEX VISIT_FIELD (regex)
1737 #define VISIT_DBPOINTER VISIT_FIELD (dbpointer)
1738 #define VISIT_CODE VISIT_FIELD (code)
1739 #define VISIT_SYMBOL VISIT_FIELD (symbol)
1740 #define VISIT_CODEWSCOPE VISIT_FIELD (codewscope)
1741 #define VISIT_INT32 VISIT_FIELD (int32)
1742 #define VISIT_TIMESTAMP VISIT_FIELD (timestamp)
1743 #define VISIT_INT64 VISIT_FIELD (int64)
1744 #define VISIT_DECIMAL128 VISIT_FIELD (decimal128)
1745 #define VISIT_MAXKEY VISIT_FIELD (maxkey)
1746 #define VISIT_MINKEY VISIT_FIELD (minkey)
1747 
1748 
1749 /**
1750  * bson_iter_visit_all:
1751  * @iter: A #bson_iter_t.
1752  * @visitor: A #bson_visitor_t containing the visitors.
1753  * @data: User data for @visitor data parameters.
1754  *
1755  *
1756  * Returns: true if the visitor was pre-maturely ended; otherwise false.
1757  */
1758 /*
1759  *--------------------------------------------------------------------------
1760  *
1761  * bson_iter_visit_all --
1762  *
1763  *       Visits all fields forward from the current position of @iter. For
1764  *       each field found a function in @visitor will be called. Typically
1765  *       you will use this immediately after initializing a bson_iter_t.
1766  *
1767  *          bson_iter_init (&iter, b);
1768  *          bson_iter_visit_all (&iter, my_visitor, NULL);
1769  *
1770  *       @iter will no longer be valid after this function has executed and
1771  *       will need to be reinitialized if intending to reuse.
1772  *
1773  * Returns:
1774  *       true if successfully visited all fields or callback requested
1775  *       early termination, otherwise false.
1776  *
1777  * Side effects:
1778  *       None.
1779  *
1780  *--------------------------------------------------------------------------
1781  */
1782 
1783 bool
1784 bson_iter_visit_all (bson_iter_t          *iter,    /* INOUT */
1785                      const bson_visitor_t *visitor, /* IN */
1786                      void                 *data)    /* IN */
1787 {
1788    uint32_t bson_type;
1789    const char *key;
1790    bool unsupported;
1791 
1792    BSON_ASSERT (iter);
1793    BSON_ASSERT (visitor);
1794 
1795    while (_bson_iter_next_internal (iter, &key, &bson_type, &unsupported)) {
1796       if (*key && !bson_utf8_validate (key, strlen (key), false)) {
1797          iter->err_off = iter->off;
1798          break;
1799       }
1800 
1801       if (VISIT_BEFORE (iter, key, data)) {
1802          return true;
1803       }
1804 
1805       switch (bson_type) {
1806       case BSON_TYPE_DOUBLE:
1807 
1808          if (VISIT_DOUBLE (iter, key, bson_iter_double (iter), data)) {
1809             return true;
1810          }
1811 
1812          break;
1813       case BSON_TYPE_UTF8:
1814          {
1815             uint32_t utf8_len;
1816             const char *utf8;
1817 
1818             utf8 = bson_iter_utf8 (iter, &utf8_len);
1819 
1820             if (!bson_utf8_validate (utf8, utf8_len, true)) {
1821                iter->err_off = iter->off;
1822                return true;
1823             }
1824 
1825             if (VISIT_UTF8 (iter, key, utf8_len, utf8, data)) {
1826                return true;
1827             }
1828          }
1829          break;
1830       case BSON_TYPE_DOCUMENT:
1831          {
1832             const uint8_t *docbuf = NULL;
1833             uint32_t doclen = 0;
1834             bson_t b;
1835 
1836             bson_iter_document (iter, &doclen, &docbuf);
1837 
1838             if (bson_init_static (&b, docbuf, doclen) &&
1839                 VISIT_DOCUMENT (iter, key, &b, data)) {
1840                return true;
1841             }
1842          }
1843          break;
1844       case BSON_TYPE_ARRAY:
1845          {
1846             const uint8_t *docbuf = NULL;
1847             uint32_t doclen = 0;
1848             bson_t b;
1849 
1850             bson_iter_array (iter, &doclen, &docbuf);
1851 
1852             if (bson_init_static (&b, docbuf, doclen)
1853                 && VISIT_ARRAY (iter, key, &b, data)) {
1854                return true;
1855             }
1856          }
1857          break;
1858       case BSON_TYPE_BINARY:
1859          {
1860             const uint8_t *binary = NULL;
1861             bson_subtype_t subtype = BSON_SUBTYPE_BINARY;
1862             uint32_t binary_len = 0;
1863 
1864             bson_iter_binary (iter, &subtype, &binary_len, &binary);
1865 
1866             if (VISIT_BINARY (iter, key, subtype, binary_len, binary, data)) {
1867                return true;
1868             }
1869          }
1870          break;
1871       case BSON_TYPE_UNDEFINED:
1872 
1873          if (VISIT_UNDEFINED (iter, key, data)) {
1874             return true;
1875          }
1876 
1877          break;
1878       case BSON_TYPE_OID:
1879 
1880          if (VISIT_OID (iter, key, bson_iter_oid (iter), data)) {
1881             return true;
1882          }
1883 
1884          break;
1885       case BSON_TYPE_BOOL:
1886 
1887          if (VISIT_BOOL (iter, key, bson_iter_bool (iter), data)) {
1888             return true;
1889          }
1890 
1891          break;
1892       case BSON_TYPE_DATE_TIME:
1893 
1894          if (VISIT_DATE_TIME (iter, key, bson_iter_date_time (iter), data)) {
1895             return true;
1896          }
1897 
1898          break;
1899       case BSON_TYPE_NULL:
1900 
1901          if (VISIT_NULL (iter, key, data)) {
1902             return true;
1903          }
1904 
1905          break;
1906       case BSON_TYPE_REGEX:
1907          {
1908             const char *regex = NULL;
1909             const char *options = NULL;
1910             regex = bson_iter_regex (iter, &options);
1911 
1912             if (VISIT_REGEX (iter, key, regex, options, data)) {
1913                return true;
1914             }
1915          }
1916          break;
1917       case BSON_TYPE_DBPOINTER:
1918          {
1919             uint32_t collection_len = 0;
1920             const char *collection = NULL;
1921             const bson_oid_t *oid = NULL;
1922 
1923             bson_iter_dbpointer (iter, &collection_len, &collection, &oid);
1924 
1925             if (VISIT_DBPOINTER (iter, key, collection_len, collection, oid,
1926                                  data)) {
1927                return true;
1928             }
1929          }
1930          break;
1931       case BSON_TYPE_CODE:
1932          {
1933             uint32_t code_len;
1934             const char *code;
1935 
1936             code = bson_iter_code (iter, &code_len);
1937 
1938             if (VISIT_CODE (iter, key, code_len, code, data)) {
1939                return true;
1940             }
1941          }
1942          break;
1943       case BSON_TYPE_SYMBOL:
1944          {
1945             uint32_t symbol_len;
1946             const char *symbol;
1947 
1948             symbol = bson_iter_symbol (iter, &symbol_len);
1949 
1950             if (VISIT_SYMBOL (iter, key, symbol_len, symbol, data)) {
1951                return true;
1952             }
1953          }
1954          break;
1955       case BSON_TYPE_CODEWSCOPE:
1956          {
1957             uint32_t length = 0;
1958             const char *code;
1959             const uint8_t *docbuf = NULL;
1960             uint32_t doclen = 0;
1961             bson_t b;
1962 
1963             code = bson_iter_codewscope (iter, &length, &doclen, &docbuf);
1964 
1965             if (bson_init_static (&b, docbuf, doclen) &&
1966                 VISIT_CODEWSCOPE (iter, key, length, code, &b, data)) {
1967                return true;
1968             }
1969          }
1970          break;
1971       case BSON_TYPE_INT32:
1972 
1973          if (VISIT_INT32 (iter, key, bson_iter_int32 (iter), data)) {
1974             return true;
1975          }
1976 
1977          break;
1978       case BSON_TYPE_TIMESTAMP:
1979          {
1980             uint32_t timestamp;
1981             uint32_t increment;
1982             bson_iter_timestamp (iter, &timestamp, &increment);
1983 
1984             if (VISIT_TIMESTAMP (iter, key, timestamp, increment, data)) {
1985                return true;
1986             }
1987          }
1988          break;
1989       case BSON_TYPE_INT64:
1990 
1991          if (VISIT_INT64 (iter, key, bson_iter_int64 (iter), data)) {
1992             return true;
1993          }
1994 
1995          break;
1996       case BSON_TYPE_DECIMAL128:
1997          {
1998             bson_decimal128_t dec;
1999             bson_iter_decimal128 (iter, &dec);
2000 
2001             if (VISIT_DECIMAL128 (iter, key, &dec, data)) {
2002                return true;
2003             }
2004          }
2005          break;
2006       case BSON_TYPE_MAXKEY:
2007 
2008          if (VISIT_MAXKEY (iter, bson_iter_key_unsafe (iter), data)) {
2009             return true;
2010          }
2011 
2012          break;
2013       case BSON_TYPE_MINKEY:
2014 
2015          if (VISIT_MINKEY (iter, bson_iter_key_unsafe (iter), data)) {
2016             return true;
2017          }
2018 
2019          break;
2020       case BSON_TYPE_EOD:
2021       default:
2022          break;
2023       }
2024 
2025       if (VISIT_AFTER (iter, bson_iter_key_unsafe (iter), data)) {
2026          return true;
2027       }
2028    }
2029 
2030    if (iter->err_off) {
2031       if (unsupported &&
2032             visitor->visit_unsupported_type &&
2033             bson_utf8_validate (key, strlen (key), false)) {
2034          visitor->visit_unsupported_type (iter, key, bson_type, data);
2035          return false;
2036       }
2037 
2038       VISIT_CORRUPT (iter, data);
2039    }
2040 
2041 #undef VISIT_FIELD
2042 
2043    return false;
2044 }
2045 
2046 
2047 /*
2048  *--------------------------------------------------------------------------
2049  *
2050  * bson_iter_overwrite_bool --
2051  *
2052  *       Overwrites the current BSON_TYPE_BOOLEAN field with a new value.
2053  *       This is performed in-place and therefore no keys are moved.
2054  *
2055  * Returns:
2056  *       None.
2057  *
2058  * Side effects:
2059  *       None.
2060  *
2061  *--------------------------------------------------------------------------
2062  */
2063 
2064 void
2065 bson_iter_overwrite_bool (bson_iter_t *iter,  /* IN */
2066                           bool         value) /* IN */
2067 {
2068    BSON_ASSERT (iter);
2069    value = !!value;
2070 
2071    if (ITER_TYPE (iter) == BSON_TYPE_BOOL) {
2072       memcpy ((void *)(iter->raw + iter->d1), &value, 1);
2073    }
2074 }
2075 
2076 
2077 /*
2078  *--------------------------------------------------------------------------
2079  *
2080  * bson_iter_overwrite_int32 --
2081  *
2082  *       Overwrites the current BSON_TYPE_INT32 field with a new value.
2083  *       This is performed in-place and therefore no keys are moved.
2084  *
2085  * Returns:
2086  *       None.
2087  *
2088  * Side effects:
2089  *       None.
2090  *
2091  *--------------------------------------------------------------------------
2092  */
2093 
2094 void
2095 bson_iter_overwrite_int32 (bson_iter_t *iter,  /* IN */
2096                            int32_t      value) /* IN */
2097 {
2098    BSON_ASSERT (iter);
2099 
2100    if (ITER_TYPE (iter) == BSON_TYPE_INT32) {
2101 #if BSON_BYTE_ORDER != BSON_LITTLE_ENDIAN
2102       value = BSON_UINT32_TO_LE (value);
2103 #endif
2104       memcpy ((void *)(iter->raw + iter->d1), &value, sizeof (value));
2105    }
2106 }
2107 
2108 
2109 /*
2110  *--------------------------------------------------------------------------
2111  *
2112  * bson_iter_overwrite_int64 --
2113  *
2114  *       Overwrites the current BSON_TYPE_INT64 field with a new value.
2115  *       This is performed in-place and therefore no keys are moved.
2116  *
2117  * Returns:
2118  *       None.
2119  *
2120  * Side effects:
2121  *       None.
2122  *
2123  *--------------------------------------------------------------------------
2124  */
2125 
2126 void
2127 bson_iter_overwrite_int64 (bson_iter_t *iter,   /* IN */
2128                            int64_t      value)  /* IN */
2129 {
2130    BSON_ASSERT (iter);
2131 
2132    if (ITER_TYPE (iter) == BSON_TYPE_INT64) {
2133 #if BSON_BYTE_ORDER != BSON_LITTLE_ENDIAN
2134       value = BSON_UINT64_TO_LE (value);
2135 #endif
2136       memcpy ((void *)(iter->raw + iter->d1), &value, sizeof (value));
2137    }
2138 }
2139 
2140 
2141 /*
2142  *--------------------------------------------------------------------------
2143  *
2144  * bson_iter_overwrite_double --
2145  *
2146  *       Overwrites the current BSON_TYPE_DOUBLE field with a new value.
2147  *       This is performed in-place and therefore no keys are moved.
2148  *
2149  * Returns:
2150  *       None.
2151  *
2152  * Side effects:
2153  *       None.
2154  *
2155  *--------------------------------------------------------------------------
2156  */
2157 
2158 void
2159 bson_iter_overwrite_double (bson_iter_t *iter,  /* IN */
2160                             double       value) /* IN */
2161 {
2162    BSON_ASSERT (iter);
2163 
2164    if (ITER_TYPE (iter) == BSON_TYPE_DOUBLE) {
2165       value = BSON_DOUBLE_TO_LE (value);
2166       memcpy ((void *)(iter->raw + iter->d1), &value, sizeof (value));
2167    }
2168 }
2169 
2170 
2171 /*
2172  *--------------------------------------------------------------------------
2173  *
2174  * bson_iter_overwrite_decimal128 --
2175  *
2176  *       Overwrites the current BSON_TYPE_DECIMAL128 field with a new value.
2177  *       This is performed in-place and therefore no keys are moved.
2178  *
2179  * Returns:
2180  *    None.
2181  *
2182  * Side effects:
2183  *    None.
2184  *
2185  *--------------------------------------------------------------------------
2186  */
2187 void
2188 bson_iter_overwrite_decimal128 (bson_iter_t       *iter,   /* IN */
2189                                 bson_decimal128_t *value)  /* IN */
2190 {
2191    BSON_ASSERT (iter);
2192 
2193    if (ITER_TYPE (iter) == BSON_TYPE_DECIMAL128) {
2194 #if BSON_BYTE_ORDER != BSON_LITTLE_ENDIAN
2195       uint64_t data[2];
2196       data[0] = BSON_UINT64_TO_LE (value->low);
2197       data[1] = BSON_UINT64_TO_LE (value->high);
2198       memcpy ((void *)(iter->raw + iter->d1), data, sizeof (data));
2199 #else
2200       memcpy ((void *)(iter->raw + iter->d1), value, sizeof (*value));
2201 #endif
2202    }
2203 }
2204 
2205 
2206 /*
2207  *--------------------------------------------------------------------------
2208  *
2209  * bson_iter_value --
2210  *
2211  *       Retrieves a bson_value_t containing the boxed value of the current
2212  *       element. The result of this function valid until the state of
2213  *       iter has been changed (through the use of bson_iter_next()).
2214  *
2215  * Returns:
2216  *       A bson_value_t that should not be modified or freed. If you need
2217  *       to hold on to the value, use bson_value_copy().
2218  *
2219  * Side effects:
2220  *       None.
2221  *
2222  *--------------------------------------------------------------------------
2223  */
2224 
2225 const bson_value_t *
2226 bson_iter_value (bson_iter_t *iter) /* IN */
2227 {
2228    bson_value_t *value;
2229 
2230    BSON_ASSERT (iter);
2231 
2232    value = &iter->value;
2233    value->value_type = ITER_TYPE (iter);
2234 
2235    switch (value->value_type) {
2236    case BSON_TYPE_DOUBLE:
2237       value->value.v_double = bson_iter_double (iter);
2238       break;
2239    case BSON_TYPE_UTF8:
2240       value->value.v_utf8.str =
2241          (char *)bson_iter_utf8 (iter, &value->value.v_utf8.len);
2242       break;
2243    case BSON_TYPE_DOCUMENT:
2244       bson_iter_document (iter,
2245                           &value->value.v_doc.data_len,
2246                           (const uint8_t **)&value->value.v_doc.data);
2247       break;
2248    case BSON_TYPE_ARRAY:
2249       bson_iter_array (iter,
2250                        &value->value.v_doc.data_len,
2251                        (const uint8_t **)&value->value.v_doc.data);
2252       break;
2253    case BSON_TYPE_BINARY:
2254       bson_iter_binary (iter,
2255                         &value->value.v_binary.subtype,
2256                         &value->value.v_binary.data_len,
2257                         (const uint8_t **)&value->value.v_binary.data);
2258       break;
2259    case BSON_TYPE_OID:
2260       bson_oid_copy (bson_iter_oid (iter), &value->value.v_oid);
2261       break;
2262    case BSON_TYPE_BOOL:
2263       value->value.v_bool = bson_iter_bool (iter);
2264       break;
2265    case BSON_TYPE_DATE_TIME:
2266       value->value.v_datetime = bson_iter_date_time (iter);
2267       break;
2268    case BSON_TYPE_REGEX:
2269       value->value.v_regex.regex = (char *)bson_iter_regex (
2270             iter,
2271             (const char **)&value->value.v_regex.options);
2272       break;
2273    case BSON_TYPE_DBPOINTER: {
2274       const bson_oid_t *oid;
2275 
2276       bson_iter_dbpointer (iter,
2277                            &value->value.v_dbpointer.collection_len,
2278                            (const char **)&value->value.v_dbpointer.collection,
2279                            &oid);
2280       bson_oid_copy (oid, &value->value.v_dbpointer.oid);
2281       break;
2282    }
2283    case BSON_TYPE_CODE:
2284       value->value.v_code.code =
2285          (char *)bson_iter_code (
2286             iter,
2287             &value->value.v_code.code_len);
2288       break;
2289    case BSON_TYPE_SYMBOL:
2290       value->value.v_symbol.symbol =
2291          (char *)bson_iter_symbol (
2292             iter,
2293             &value->value.v_symbol.len);
2294       break;
2295    case BSON_TYPE_CODEWSCOPE:
2296       value->value.v_codewscope.code =
2297          (char *)bson_iter_codewscope (
2298             iter,
2299             &value->value.v_codewscope.code_len,
2300             &value->value.v_codewscope.scope_len,
2301             (const uint8_t **)&value->value.v_codewscope.scope_data);
2302       break;
2303    case BSON_TYPE_INT32:
2304       value->value.v_int32 = bson_iter_int32 (iter);
2305       break;
2306    case BSON_TYPE_TIMESTAMP:
2307       bson_iter_timestamp (iter,
2308                            &value->value.v_timestamp.timestamp,
2309                            &value->value.v_timestamp.increment);
2310       break;
2311    case BSON_TYPE_INT64:
2312       value->value.v_int64 = bson_iter_int64 (iter);
2313       break;
2314    case BSON_TYPE_DECIMAL128:
2315       bson_iter_decimal128 (iter, &(value->value.v_decimal128));
2316       break;
2317    case BSON_TYPE_NULL:
2318    case BSON_TYPE_UNDEFINED:
2319    case BSON_TYPE_MAXKEY:
2320    case BSON_TYPE_MINKEY:
2321       break;
2322    case BSON_TYPE_EOD:
2323    default:
2324       return NULL;
2325    }
2326 
2327    return value;
2328 }
2329