1 /* 2 * Copyright 2014-2017 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 #ifndef PHONGO_BSON_H 18 #define PHONGO_BSON_H 19 20 #include <bson/bson.h> 21 22 /* PHP Core stuff */ 23 #include <php.h> 24 25 #define BSON_UNSERIALIZE_FUNC_NAME "bsonUnserialize" 26 #define BSON_SERIALIZE_FUNC_NAME "bsonSerialize" 27 28 #define PHONGO_ODM_FIELD_NAME "__pclass" 29 30 typedef enum { 31 PHONGO_BSON_NONE = 0x00, 32 PHONGO_BSON_ADD_ID = 0x01, 33 PHONGO_BSON_RETURN_ID = 0x02 34 } php_phongo_bson_flags_t; 35 36 typedef enum { 37 PHONGO_TYPEMAP_NONE, 38 PHONGO_TYPEMAP_NATIVE_ARRAY, 39 PHONGO_TYPEMAP_NATIVE_OBJECT, 40 PHONGO_TYPEMAP_CLASS 41 } php_phongo_bson_typemap_types; 42 43 typedef enum { 44 PHONGO_FIELD_PATH_ITEM_NONE, 45 PHONGO_FIELD_PATH_ITEM_ARRAY, 46 PHONGO_FIELD_PATH_ITEM_DOCUMENT 47 } php_phongo_bson_field_path_item_types; 48 49 typedef struct { 50 char** elements; 51 php_phongo_bson_field_path_item_types* element_types; 52 size_t allocated_size; 53 size_t size; 54 size_t ref_count; 55 bool owns_elements; 56 } php_phongo_field_path; 57 58 typedef struct _php_phongo_field_path_map_element { 59 php_phongo_field_path* entry; 60 php_phongo_bson_typemap_types node_type; 61 zend_class_entry* node_ce; 62 } php_phongo_field_path_map_element; 63 64 typedef struct { 65 php_phongo_bson_typemap_types document_type; 66 zend_class_entry* document; 67 php_phongo_bson_typemap_types array_type; 68 zend_class_entry* array; 69 php_phongo_bson_typemap_types root_type; 70 zend_class_entry* root; 71 struct { 72 php_phongo_field_path_map_element** map; 73 size_t allocated_size; 74 size_t size; 75 } field_paths; 76 } php_phongo_bson_typemap; 77 78 typedef struct { 79 zval zchild; 80 php_phongo_bson_typemap map; 81 zend_class_entry* odm; 82 bool is_visiting_array; 83 php_phongo_field_path* field_path; 84 } php_phongo_bson_state; 85 86 #define PHONGO_BSON_INIT_STATE(s) \ 87 do { \ 88 memset(&(s), 0, sizeof(php_phongo_bson_state)); \ 89 } while (0) 90 #define PHONGO_BSON_INIT_DEBUG_STATE(s) \ 91 do { \ 92 memset(&(s), 0, sizeof(php_phongo_bson_state)); \ 93 s.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \ 94 s.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \ 95 } while (0) 96 97 void php_phongo_zval_to_bson(zval* data, php_phongo_bson_flags_t flags, bson_t* bson, bson_t** bson_out); 98 bool php_phongo_bson_to_zval_ex(const unsigned char* data, int data_len, php_phongo_bson_state* state); 99 bool php_phongo_bson_to_zval(const unsigned char* data, int data_len, zval* out); 100 bool php_phongo_bson_value_to_zval(const bson_value_t* value, zval* zv); 101 void php_phongo_zval_to_bson_value(zval* data, php_phongo_bson_flags_t flags, bson_value_t* value); 102 bool php_phongo_bson_typemap_to_state(zval* typemap, php_phongo_bson_typemap* map); 103 void php_phongo_bson_state_ctor(php_phongo_bson_state* state); 104 void php_phongo_bson_state_dtor(php_phongo_bson_state* state); 105 void php_phongo_bson_state_copy_ctor(php_phongo_bson_state* dst, php_phongo_bson_state* src); 106 void php_phongo_bson_typemap_dtor(php_phongo_bson_typemap* map); 107 108 void php_phongo_bson_new_timestamp_from_increment_and_timestamp(zval* object, uint32_t increment, uint32_t timestamp); 109 void php_phongo_bson_new_int64(zval* object, int64_t integer); 110 111 php_phongo_field_path* php_phongo_field_path_alloc(bool owns_elements); 112 void php_phongo_field_path_free(php_phongo_field_path* field_path); 113 void php_phongo_field_path_write_item_at_current_level(php_phongo_field_path* field_path, const char* element); 114 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); 115 bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element, php_phongo_bson_field_path_item_types element_type); 116 bool php_phongo_field_path_pop(php_phongo_field_path* field_path); 117 118 char* php_phongo_field_path_as_string(php_phongo_field_path* field_path); 119 120 #endif /* PHONGO_BSON_H */ 121 122 /* 123 * Local variables: 124 * tab-width: 4 125 * c-basic-offset: 4 126 * End: 127 * vim600: noet sw=4 ts=4 fdm=marker 128 * vim<600: noet sw=4 ts=4 129 */ 130