forked from mongodb/mongo-php-driver
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbson.c
1547 lines (1266 loc) · 46.9 KB
/
bson.c
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
/*
* Copyright 2014-2017 MongoDB, Inc.
*
* 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.
*/
#include <bson/bson.h>
#include <php.h>
#include <Zend/zend_hash.h>
#include <Zend/zend_interfaces.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_phongo.h"
#include "php_bson.h"
#include "phongo_compat.h"
#include "php_array_api.h"
#define DEBUG 0
#undef MONGOC_LOG_DOMAIN
#define MONGOC_LOG_DOMAIN "PHONGO-BSON"
#define PHONGO_IS_CLASS_INSTANTIATABLE(ce) \
(!(ce->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)))
#if PHP_VERSION_ID >= 70000
#define PHONGO_BSON_STATE_ZCHILD(state) (&((php_phongo_bson_state*) (state))->zchild)
#else
#define PHONGO_BSON_STATE_ZCHILD(state) (((php_phongo_bson_state*) (state))->zchild)
#endif
#define PHONGO_FIELD_PATH_EXPANSION 8
/* Forward declarations */
static bool php_phongo_bson_visit_document(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_t* v_document, void* data);
static bool php_phongo_bson_visit_array(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_t* v_document, void* data);
/* Path builder */
char* php_phongo_field_path_as_string(php_phongo_field_path* field_path)
{
size_t length = 1; /* NULL character */
size_t i;
char* path;
char* ptr;
if (!field_path) {
return estrdup("");
}
if (!field_path->elements) {
return estrdup("");
}
for (i = 0; i <= field_path->size; i++) {
if (!field_path->elements[i]) {
continue;
}
length += (1 + strlen(field_path->elements[i]));
}
path = emalloc(length);
ptr = path;
for (i = 0; i <= field_path->size; i++) {
if (!field_path->elements[i]) {
continue;
}
strcpy(ptr, field_path->elements[i]);
ptr += strlen(field_path->elements[i]);
ptr[0] = '.';
ptr++;
}
ptr[-1] = '\0';
return path;
}
php_phongo_field_path* php_phongo_field_path_alloc(bool owns_elements)
{
php_phongo_field_path* tmp = ecalloc(1, sizeof(php_phongo_field_path));
tmp->ref_count = 1;
tmp->owns_elements = owns_elements;
return tmp;
}
void php_phongo_field_path_free(php_phongo_field_path* field_path)
{
if (field_path->owns_elements) {
size_t i;
for (i = 0; i < field_path->size; i++) {
efree(field_path->elements[i]);
}
}
if (field_path->elements) {
efree(field_path->elements);
}
if (field_path->element_types) {
efree(field_path->element_types);
}
efree(field_path);
}
static void php_phongo_field_path_ensure_allocation(php_phongo_field_path* field_path, size_t level)
{
if (level >= field_path->allocated_size) {
size_t i;
field_path->allocated_size = field_path->size + PHONGO_FIELD_PATH_EXPANSION;
field_path->elements = erealloc(field_path->elements, sizeof(char*) * field_path->allocated_size);
field_path->element_types = erealloc(field_path->element_types, sizeof(php_phongo_bson_field_path_item_types) * field_path->allocated_size);
for (i = level; i < field_path->allocated_size; i++) {
field_path->elements[i] = NULL;
field_path->element_types[i] = PHONGO_FIELD_PATH_ITEM_NONE;
}
}
}
void php_phongo_field_path_write_item_at_current_level(php_phongo_field_path* field_path, const char* element)
{
php_phongo_field_path_ensure_allocation(field_path, field_path->size);
if (field_path->owns_elements) {
field_path->elements[field_path->size] = estrdup(element);
} else {
field_path->elements[field_path->size] = (char*) element;
}
}
void php_phongo_field_path_write_type_at_current_level(php_phongo_field_path* field_path, php_phongo_bson_field_path_item_types element_type)
{
php_phongo_field_path_ensure_allocation(field_path, field_path->size);
field_path->element_types[field_path->size] = element_type;
}
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element, php_phongo_bson_field_path_item_types element_type)
{
php_phongo_field_path_write_item_at_current_level(field_path, element);
php_phongo_field_path_write_type_at_current_level(field_path, element_type);
field_path->size++;
return true;
}
bool php_phongo_field_path_pop(php_phongo_field_path* field_path)
{
php_phongo_field_path_ensure_allocation(field_path, field_path->size);
field_path->elements[field_path->size] = NULL;
field_path->element_types[field_path->size] = PHONGO_FIELD_PATH_ITEM_NONE;
field_path->size--;
field_path->elements[field_path->size] = NULL;
field_path->element_types[field_path->size] = PHONGO_FIELD_PATH_ITEM_NONE;
return true;
}
inline static bool php_phongo_bson_state_is_initialized(php_phongo_bson_state* state)
{
return state->field_path != NULL;
}
void php_phongo_bson_state_ctor(php_phongo_bson_state* state)
{
state->field_path = php_phongo_field_path_alloc(false);
}
void php_phongo_bson_state_copy_ctor(php_phongo_bson_state* dst, php_phongo_bson_state* src)
{
dst->map = src->map;
if (src->field_path) {
src->field_path->ref_count++;
}
dst->field_path = src->field_path;
}
void php_phongo_bson_state_dtor(php_phongo_bson_state* state)
{
if (state->field_path) {
state->field_path->ref_count--;
if (state->field_path->ref_count < 1) {
php_phongo_field_path_free(state->field_path);
}
state->field_path = NULL;
}
} /* }}} */
static void php_phongo_bson_visit_corrupt(const bson_iter_t* iter ARG_UNUSED, void* data ARG_UNUSED) /* {{{ */
{
mongoc_log(MONGOC_LOG_LEVEL_WARNING, MONGOC_LOG_DOMAIN, "Corrupt BSON data detected!");
} /* }}} */
static void php_phongo_bson_visit_unsupported_type(const bson_iter_t* iter ARG_UNUSED, const char* key, uint32_t v_type_code, void* data ARG_UNUSED) /* {{{ */
{
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
char* path_string;
TSRMLS_FETCH();
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
path_string = php_phongo_field_path_as_string(state->field_path);
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE TSRMLS_CC, "Detected unknown BSON type 0x%02hhx for field path \"%s\". Are you using the latest driver?", (unsigned char) v_type_code, path_string);
efree(path_string);
} /* }}} */
static bool php_phongo_bson_visit_double(const bson_iter_t* iter ARG_UNUSED, const char* key, double v_double, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
if (state->is_visiting_array) {
add_next_index_double(retval, v_double);
} else {
add_assoc_double(retval, key, v_double);
}
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_utf8(const bson_iter_t* iter ARG_UNUSED, const char* key, size_t v_utf8_len, const char* v_utf8, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
if (state->is_visiting_array) {
ADD_NEXT_INDEX_STRINGL(retval, v_utf8, v_utf8_len);
} else {
ADD_ASSOC_STRING_EX(retval, key, strlen(key), v_utf8, v_utf8_len);
}
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_binary(const bson_iter_t* iter ARG_UNUSED, const char* key, bson_subtype_t v_subtype, size_t v_binary_len, const uint8_t* v_binary, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
TSRMLS_FETCH();
if (v_subtype == 0x80 && strcmp(key, PHONGO_ODM_FIELD_NAME) == 0) {
#if PHP_VERSION_ID >= 70000
zend_string* zs_classname = zend_string_init((const char*) v_binary, v_binary_len, 0);
zend_class_entry* found_ce = zend_fetch_class(zs_classname, ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
zend_string_release(zs_classname);
#else
zend_class_entry* found_ce = zend_fetch_class((const char*) v_binary, v_binary_len, ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_SILENT TSRMLS_CC);
#endif
if (found_ce && PHONGO_IS_CLASS_INSTANTIATABLE(found_ce) && instanceof_function(found_ce, php_phongo_persistable_ce TSRMLS_CC)) {
((php_phongo_bson_state*) data)->odm = found_ce;
}
}
{
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_binary_from_binary_and_type(&zchild, (const char*) v_binary, v_binary_len, v_subtype TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
MAKE_STD_ZVAL(zchild);
php_phongo_new_binary_from_binary_and_type(zchild, (const char*) v_binary, v_binary_len, v_subtype TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
}
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_undefined(const bson_iter_t* iter, const char* key, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000 /* PHP_VERSION_ID >= 70000 */
zval zchild;
object_init_ex(&zchild, php_phongo_undefined_ce);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
object_init_ex(zchild, php_phongo_undefined_ce);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_oid(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_oid_t* v_oid, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_objectid_new_from_oid(&zchild, v_oid TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_objectid_new_from_oid(zchild, v_oid TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_bool(const bson_iter_t* iter ARG_UNUSED, const char* key, bool v_bool, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
if (state->is_visiting_array) {
add_next_index_bool(retval, v_bool);
} else {
add_assoc_bool(retval, key, v_bool);
}
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_date_time(const bson_iter_t* iter ARG_UNUSED, const char* key, int64_t msec_since_epoch, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_utcdatetime_from_epoch(&zchild, msec_since_epoch TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_utcdatetime_from_epoch(zchild, msec_since_epoch TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_decimal128(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_decimal128_t* decimal, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_decimal128(&zchild, decimal TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_decimal128(zchild, decimal TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_null(const bson_iter_t* iter ARG_UNUSED, const char* key, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
if (state->is_visiting_array) {
add_next_index_null(retval);
} else {
add_assoc_null(retval, key);
}
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_regex(const bson_iter_t* iter ARG_UNUSED, const char* key, const char* v_regex, const char* v_options, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_regex_from_regex_and_options(&zchild, v_regex, v_options TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_regex_from_regex_and_options(zchild, v_regex, v_options TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_symbol(const bson_iter_t* iter, const char* key, size_t v_symbol_len, const char* v_symbol, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_symbol(&zchild, v_symbol, v_symbol_len TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_symbol(zchild, v_symbol, v_symbol_len TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_code(const bson_iter_t* iter ARG_UNUSED, const char* key, size_t v_code_len, const char* v_code, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_javascript_from_javascript(1, &zchild, v_code, v_code_len TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_javascript_from_javascript(1, zchild, v_code, v_code_len TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_dbpointer(const bson_iter_t* iter, const char* key, size_t namespace_len, const char* namespace, const bson_oid_t* oid, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_dbpointer(&zchild, namespace, namespace_len, oid TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_dbpointer(zchild, namespace, namespace_len, oid TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_codewscope(const bson_iter_t* iter ARG_UNUSED, const char* key, size_t v_code_len, const char* v_code, const bson_t* v_scope, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_javascript_from_javascript_and_scope(1, &zchild, v_code, v_code_len, v_scope TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_javascript_from_javascript_and_scope(1, zchild, v_code, v_code_len, v_scope TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_int32(const bson_iter_t* iter ARG_UNUSED, const char* key, int32_t v_int32, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
if (state->is_visiting_array) {
add_next_index_long(retval, v_int32);
} else {
add_assoc_long(retval, key, v_int32);
}
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_timestamp(const bson_iter_t* iter ARG_UNUSED, const char* key, uint32_t v_timestamp, uint32_t v_increment, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
php_phongo_new_timestamp_from_increment_and_timestamp(&zchild, v_increment, v_timestamp TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
php_phongo_new_timestamp_from_increment_and_timestamp(zchild, v_increment, v_timestamp TSRMLS_CC);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_int64(const bson_iter_t* iter ARG_UNUSED, const char* key, int64_t v_int64, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if SIZEOF_PHONGO_LONG == 4
TSRMLS_FETCH();
#endif
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
if (state->is_visiting_array) {
ADD_NEXT_INDEX_INT64(retval, v_int64);
} else {
ADD_ASSOC_INT64(retval, key, v_int64);
}
return false;
} /* }}} */
static bool php_phongo_bson_visit_maxkey(const bson_iter_t* iter ARG_UNUSED, const char* key, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
object_init_ex(&zchild, php_phongo_maxkey_ce);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
object_init_ex(zchild, php_phongo_maxkey_ce);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static bool php_phongo_bson_visit_minkey(const bson_iter_t* iter ARG_UNUSED, const char* key, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
php_phongo_bson_state* state = (php_phongo_bson_state*) data;
#if PHP_VERSION_ID >= 70000
zval zchild;
object_init_ex(&zchild, php_phongo_minkey_ce);
if (state->is_visiting_array) {
add_next_index_zval(retval, &zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
zval* zchild = NULL;
TSRMLS_FETCH();
MAKE_STD_ZVAL(zchild);
object_init_ex(zchild, php_phongo_minkey_ce);
if (state->is_visiting_array) {
add_next_index_zval(retval, zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
php_phongo_field_path_write_item_at_current_level(state->field_path, key);
return false;
} /* }}} */
static const bson_visitor_t php_bson_visitors = {
NULL /* php_phongo_bson_visit_before*/,
NULL /*php_phongo_bson_visit_after*/,
php_phongo_bson_visit_corrupt,
php_phongo_bson_visit_double,
php_phongo_bson_visit_utf8,
php_phongo_bson_visit_document,
php_phongo_bson_visit_array,
php_phongo_bson_visit_binary,
php_phongo_bson_visit_undefined,
php_phongo_bson_visit_oid,
php_phongo_bson_visit_bool,
php_phongo_bson_visit_date_time,
php_phongo_bson_visit_null,
php_phongo_bson_visit_regex,
php_phongo_bson_visit_dbpointer,
php_phongo_bson_visit_code,
php_phongo_bson_visit_symbol,
php_phongo_bson_visit_codewscope,
php_phongo_bson_visit_int32,
php_phongo_bson_visit_timestamp,
php_phongo_bson_visit_int64,
php_phongo_bson_visit_maxkey,
php_phongo_bson_visit_minkey,
php_phongo_bson_visit_unsupported_type,
php_phongo_bson_visit_decimal128,
{ NULL }
};
static inline bool map_element_matches_field_path(php_phongo_field_path_map_element* map_element, php_phongo_field_path* current)
{
size_t i;
if (map_element->entry->size != current->size) {
return false;
}
for (i = 0; i < current->size; i++) {
if (strcmp(map_element->entry->elements[i], "$") == 0) {
continue;
}
if (strcmp(map_element->entry->elements[i], current->elements[i]) != 0) {
return false;
}
}
return true;
}
static php_phongo_field_path_map_element* map_find_field_path_entry(php_phongo_bson_state* state)
{
size_t i;
/* Loop over all field path mappings, and for each, try to see whether it matches the current path */
for (i = 0; i < state->map.field_paths.size; i++) {
if (map_element_matches_field_path(state->map.field_paths.map[i], state->field_path)) {
return state->map.field_paths.map[i];
}
}
return NULL;
}
static void php_phongo_handle_field_path_entry_for_compound_type(php_phongo_bson_state* state, php_phongo_bson_typemap_types* type, zend_class_entry** ce)
{
php_phongo_field_path_map_element* entry = map_find_field_path_entry(state);
if (entry) {
switch (entry->node_type) {
case PHONGO_TYPEMAP_NATIVE_ARRAY:
case PHONGO_TYPEMAP_NATIVE_OBJECT:
*type = entry->node_type;
break;
case PHONGO_TYPEMAP_CLASS:
*type = entry->node_type;
*ce = entry->node_ce;
break;
default:
/* Do nothing - pacify compiler */
break;
}
}
}
static bool php_phongo_bson_visit_document(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_t* v_document, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
bson_iter_t child;
php_phongo_bson_state* parent_state = (php_phongo_bson_state*) data;
TSRMLS_FETCH();
php_phongo_field_path_push(parent_state->field_path, key, PHONGO_FIELD_PATH_ITEM_DOCUMENT);
if (bson_iter_init(&child, v_document)) {
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
php_phongo_bson_state_copy_ctor(&state, parent_state);
#if PHP_VERSION_ID >= 70000
array_init(&state.zchild);
#else
MAKE_STD_ZVAL(state.zchild);
array_init(state.zchild);
#endif
if (!bson_iter_visit_all(&child, &php_bson_visitors, &state) && !child.err_off) {
/* Check for entries in the fieldPath type map key, and use them to
* override the default ones for this type */
php_phongo_handle_field_path_entry_for_compound_type(&state, &state.map.document_type, &state.map.document);
/* If php_phongo_bson_visit_binary() finds an ODM class, it should
* supersede a default type map and named document class. */
if (state.odm && state.map.document_type == PHONGO_TYPEMAP_NONE) {
state.map.document_type = PHONGO_TYPEMAP_CLASS;
}
switch (state.map.document_type) {
case PHONGO_TYPEMAP_NATIVE_ARRAY:
#if PHP_VERSION_ID >= 70000
if (((php_phongo_bson_state*) data)->is_visiting_array) {
add_next_index_zval(retval, &state.zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &state.zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
if (((php_phongo_bson_state*) data)->is_visiting_array) {
add_next_index_zval(retval, state.zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, state.zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
break;
case PHONGO_TYPEMAP_CLASS: {
#if PHP_VERSION_ID >= 70000
zval obj;
object_init_ex(&obj, state.odm ? state.odm : state.map.document);
zend_call_method_with_1_params(&obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, &state.zchild);
if (((php_phongo_bson_state*) data)->is_visiting_array) {
add_next_index_zval(retval, &obj);
} else {
ADD_ASSOC_ZVAL(retval, key, &obj);
}
zval_ptr_dtor(&state.zchild);
#else /* PHP_VERSION_ID >= 70000 */
zval* obj = NULL;
MAKE_STD_ZVAL(obj);
object_init_ex(obj, state.odm ? state.odm : state.map.document);
zend_call_method_with_1_params(&obj, NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, state.zchild);
if (((php_phongo_bson_state*) data)->is_visiting_array) {
add_next_index_zval(retval, obj);
} else {
ADD_ASSOC_ZVAL(retval, key, obj);
}
zval_ptr_dtor(&state.zchild);
#endif /* PHP_VERSION_ID >= 70000 */
break;
}
case PHONGO_TYPEMAP_NATIVE_OBJECT:
default:
#if PHP_VERSION_ID >= 70000
convert_to_object(&state.zchild);
if (((php_phongo_bson_state*) data)->is_visiting_array) {
add_next_index_zval(retval, &state.zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, &state.zchild);
}
#else /* PHP_VERSION_ID >= 70000 */
convert_to_object(state.zchild);
if (((php_phongo_bson_state*) data)->is_visiting_array) {
add_next_index_zval(retval, state.zchild);
} else {
ADD_ASSOC_ZVAL(retval, key, state.zchild);
}
#endif /* PHP_VERSION_ID >= 70000 */
}
} else {
/* Iteration stopped prematurely due to corruption or a failed
* visitor. Free state.zchild, which we just initialized, and return
* true to stop iteration for our parent context. */
zval_ptr_dtor(&state.zchild);
php_phongo_bson_state_dtor(&state);
return true;
}
php_phongo_bson_state_dtor(&state);
php_phongo_field_path_pop(parent_state->field_path);
}
return false;
} /* }}} */
static bool php_phongo_bson_visit_array(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_t* v_array, void* data) /* {{{ */
{
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
bson_iter_t child;
php_phongo_bson_state* parent_state = (php_phongo_bson_state*) data;
TSRMLS_FETCH();
php_phongo_field_path_push(parent_state->field_path, key, PHONGO_FIELD_PATH_ITEM_ARRAY);
if (bson_iter_init(&child, v_array)) {
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
php_phongo_bson_state_copy_ctor(&state, parent_state);
/* Note that we are visiting an array, so element visitors know to use
* add_next_index() (i.e. disregard BSON keys) instead of add_assoc()
* when building the PHP array.
*/
state.is_visiting_array = true;
#if PHP_VERSION_ID >= 70000
array_init(&state.zchild);
#else
MAKE_STD_ZVAL(state.zchild);
array_init(state.zchild);
#endif
if (!bson_iter_visit_all(&child, &php_bson_visitors, &state) && !child.err_off) {
/* Check for entries in the fieldPath type map key, and use them to
* override the default ones for this type */
php_phongo_handle_field_path_entry_for_compound_type(&state, &state.map.array_type, &state.map.array);
switch (state.map.array_type) {
case PHONGO_TYPEMAP_CLASS: {
#if PHP_VERSION_ID >= 70000
zval obj;