diff --git a/src/BSON/Javascript.c b/src/BSON/Javascript.c index a39e5173f..1dd2ddc12 100644 --- a/src/BSON/Javascript.c +++ b/src/BSON/Javascript.c @@ -94,7 +94,7 @@ HashTable* php_phongo_javascript_get_properties_hash(phongo_compat_object_handle php_phongo_bson_state state; PHONGO_BSON_INIT_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) { zval_ptr_dtor(&state.zchild); goto failure; } @@ -194,7 +194,7 @@ static PHP_METHOD(MongoDB_BSON_Javascript, getScope) PHONGO_BSON_INIT_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -220,7 +220,7 @@ static PHP_METHOD(MongoDB_BSON_Javascript, jsonSerialize) php_phongo_bson_state state; PHONGO_BSON_INIT_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -244,7 +244,7 @@ static PHP_METHOD(MongoDB_BSON_Javascript, serialize) PHONGO_PARSE_PARAMETERS_NONE(); if (intern->scope && intern->scope->len) { - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->scope, &state)) { zval_ptr_dtor(&state.zchild); return; } diff --git a/src/BSON/functions.c b/src/BSON/functions.c index 8c3fbb940..572f9ff08 100644 --- a/src/BSON/functions.c +++ b/src/BSON/functions.c @@ -65,7 +65,7 @@ PHP_FUNCTION(toPHP) return; } - if (!php_phongo_bson_to_zval_ex((const unsigned char*) data, data_len, &state)) { + if (!php_phongo_bson_data_to_zval_ex((const unsigned char*) data, data_len, &state)) { zval_ptr_dtor(&state.zchild); php_phongo_bson_typemap_dtor(&state.map); RETURN_NULL(); diff --git a/src/MongoDB/BulkWrite.c b/src/MongoDB/BulkWrite.c index 3de0df69b..030f16336 100644 --- a/src/MongoDB/BulkWrite.c +++ b/src/MongoDB/BulkWrite.c @@ -40,9 +40,9 @@ static void php_phongo_bulkwrite_extract_id(bson_t* doc, zval** return_value) php_phongo_bson_state state; PHONGO_BSON_INIT_STATE(state); - state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; + state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY; - if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &state)) { + if (!php_phongo_bson_to_zval_ex(doc, &state)) { goto cleanup; } @@ -632,7 +632,7 @@ static HashTable* php_phongo_bulkwrite_get_debug_info(phongo_compat_object_handl if (intern->let) { zval zv; - if (!php_phongo_bson_to_zval(bson_get_data(intern->let), intern->let->len, &zv)) { + if (!php_phongo_bson_to_zval(intern->let, &zv)) { zval_ptr_dtor(&zv); goto done; } diff --git a/src/MongoDB/ClientEncryption.c b/src/MongoDB/ClientEncryption.c index 2c5a06580..d96e436f0 100644 --- a/src/MongoDB/ClientEncryption.c +++ b/src/MongoDB/ClientEncryption.c @@ -40,15 +40,15 @@ static void phongo_clientencryption_create_datakey(php_phongo_clientencryption_t static void phongo_clientencryption_encrypt(php_phongo_clientencryption_t* clientencryption, zval* zvalue, zval* zciphertext, zval* options); static void phongo_clientencryption_decrypt(php_phongo_clientencryption_t* clientencryption, zval* zciphertext, zval* zvalue); -#define RETVAL_BSON_T(reply) \ - do { \ - php_phongo_bson_state state; \ - PHONGO_BSON_INIT_STATE(state); \ - if (!php_phongo_bson_to_zval_ex(bson_get_data(&(reply)), (reply).len, &state)) { \ - zval_ptr_dtor(&state.zchild); \ - goto cleanup; \ - } \ - RETVAL_ZVAL(&state.zchild, 0, 1); \ +#define RETVAL_BSON_T(reply) \ + do { \ + php_phongo_bson_state state; \ + PHONGO_BSON_INIT_STATE(state); \ + if (!php_phongo_bson_to_zval_ex(&(reply), &state)) { \ + zval_ptr_dtor(&state.zchild); \ + goto cleanup; \ + } \ + RETVAL_ZVAL(&state.zchild, 0, 1); \ } while (0) #define RETVAL_OPTIONAL_BSON_T(reply) \ diff --git a/src/MongoDB/Command.c b/src/MongoDB/Command.c index 147378cf0..9402eff65 100644 --- a/src/MongoDB/Command.c +++ b/src/MongoDB/Command.c @@ -156,7 +156,7 @@ static HashTable* php_phongo_command_get_debug_info(phongo_compat_object_handler if (intern->bson) { zval zv; - if (!php_phongo_bson_to_zval(bson_get_data(intern->bson), intern->bson->len, &zv)) { + if (!php_phongo_bson_to_zval(intern->bson, &zv)) { zval_ptr_dtor(&zv); goto done; } diff --git a/src/MongoDB/Cursor.c b/src/MongoDB/Cursor.c index 272cd861c..1d270c110 100644 --- a/src/MongoDB/Cursor.c +++ b/src/MongoDB/Cursor.c @@ -90,7 +90,7 @@ static PHP_METHOD(MongoDB_Driver_Cursor, setTypeMap) if (restore_current_element && mongoc_cursor_current(intern->cursor)) { const bson_t* doc = mongoc_cursor_current(intern->cursor); - if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &intern->visitor_data)) { + if (!php_phongo_bson_to_zval_ex(doc, &intern->visitor_data)) { php_phongo_cursor_free_current(intern); } } @@ -223,7 +223,7 @@ static PHP_METHOD(MongoDB_Driver_Cursor, next) } if (mongoc_cursor_next(intern->cursor, &doc)) { - if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &intern->visitor_data)) { + if (!php_phongo_bson_to_zval_ex(doc, &intern->visitor_data)) { /* Free invalid result, but don't return as we want to free the * session if the intern is exhausted. */ php_phongo_cursor_free_current(intern); @@ -278,7 +278,7 @@ static PHP_METHOD(MongoDB_Driver_Cursor, rewind) doc = mongoc_cursor_current(intern->cursor); if (doc) { - if (!php_phongo_bson_to_zval_ex(bson_get_data(doc), doc->len, &intern->visitor_data)) { + if (!php_phongo_bson_to_zval_ex(doc, &intern->visitor_data)) { /* Free invalid result, but don't return as we want to free the * session if the intern is exhausted. */ php_phongo_cursor_free_current(intern); diff --git a/src/MongoDB/Monitoring/CommandFailedEvent.c b/src/MongoDB/Monitoring/CommandFailedEvent.c index df9b7595c..082de846b 100644 --- a/src/MongoDB/Monitoring/CommandFailedEvent.c +++ b/src/MongoDB/Monitoring/CommandFailedEvent.c @@ -94,7 +94,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getReply) PHONGO_PARSE_PARAMETERS_NONE(); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->reply, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -223,7 +223,7 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(phongo_compat_obj sprintf(operation_id, "%" PRIu64, intern->operation_id); ADD_ASSOC_STRING(&retval, "operationId", operation_id); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state)) { + if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) { zval_ptr_dtor(&reply_state.zchild); goto done; } diff --git a/src/MongoDB/Monitoring/CommandStartedEvent.c b/src/MongoDB/Monitoring/CommandStartedEvent.c index 685744820..326448929 100644 --- a/src/MongoDB/Monitoring/CommandStartedEvent.c +++ b/src/MongoDB/Monitoring/CommandStartedEvent.c @@ -44,7 +44,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandStartedEvent, getCommand) PHONGO_PARSE_PARAMETERS_NONE(); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->command), intern->command->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->command, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -202,7 +202,7 @@ static HashTable* php_phongo_commandstartedevent_get_debug_info(phongo_compat_ob *is_temp = 1; array_init_size(&retval, 6); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->command), intern->command->len, &command_state)) { + if (!php_phongo_bson_to_zval_ex(intern->command, &command_state)) { zval_ptr_dtor(&command_state.zchild); goto done; } diff --git a/src/MongoDB/Monitoring/CommandSucceededEvent.c b/src/MongoDB/Monitoring/CommandSucceededEvent.c index 2f2e35fa0..9d51491b8 100644 --- a/src/MongoDB/Monitoring/CommandSucceededEvent.c +++ b/src/MongoDB/Monitoring/CommandSucceededEvent.c @@ -82,7 +82,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandSucceededEvent, getReply) PHONGO_PARSE_PARAMETERS_NONE(); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->reply, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -204,7 +204,7 @@ static HashTable* php_phongo_commandsucceededevent_get_debug_info(phongo_compat_ sprintf(operation_id, "%" PRIu64, intern->operation_id); ADD_ASSOC_STRING(&retval, "operationId", operation_id); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state)) { + if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) { zval_ptr_dtor(&reply_state.zchild); goto done; } diff --git a/src/MongoDB/Monitoring/ServerHeartbeatSucceededEvent.c b/src/MongoDB/Monitoring/ServerHeartbeatSucceededEvent.c index b9427ca14..909a13959 100644 --- a/src/MongoDB/Monitoring/ServerHeartbeatSucceededEvent.c +++ b/src/MongoDB/Monitoring/ServerHeartbeatSucceededEvent.c @@ -66,7 +66,7 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_ServerHeartbeatSucceededEvent, getRe PHONGO_PARSE_PARAMETERS_NONE(); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &state)) { + if (!php_phongo_bson_to_zval_ex(intern->reply, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -126,7 +126,7 @@ static HashTable* php_phongo_serverheartbeatsucceededevent_get_debug_info(phongo ADD_ASSOC_LONG_EX(&retval, "port", intern->host.port); ADD_ASSOC_BOOL_EX(&retval, "awaited", intern->awaited); - if (!php_phongo_bson_to_zval_ex(bson_get_data(intern->reply), intern->reply->len, &reply_state)) { + if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) { zval_ptr_dtor(&reply_state.zchild); goto done; } diff --git a/src/MongoDB/Query.c b/src/MongoDB/Query.c index 9c4d2f1bf..e8cfa5a31 100644 --- a/src/MongoDB/Query.c +++ b/src/MongoDB/Query.c @@ -471,7 +471,7 @@ static HashTable* php_phongo_query_get_debug_info(phongo_compat_object_handler_t if (intern->filter) { zval zv; - if (!php_phongo_bson_to_zval(bson_get_data(intern->filter), intern->filter->len, &zv)) { + if (!php_phongo_bson_to_zval(intern->filter, &zv)) { zval_ptr_dtor(&zv); goto done; } @@ -484,7 +484,7 @@ static HashTable* php_phongo_query_get_debug_info(phongo_compat_object_handler_t if (intern->opts) { zval zv; - if (!php_phongo_bson_to_zval(bson_get_data(intern->opts), intern->opts->len, &zv)) { + if (!php_phongo_bson_to_zval(intern->opts, &zv)) { zval_ptr_dtor(&zv); goto done; } diff --git a/src/MongoDB/ReadPreference.c b/src/MongoDB/ReadPreference.c index 0e1678cfb..fa7adbc67 100644 --- a/src/MongoDB/ReadPreference.c +++ b/src/MongoDB/ReadPreference.c @@ -341,7 +341,7 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, getHedge) PHONGO_BSON_INIT_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(hedge), hedge->len, &state)) { + if (!php_phongo_bson_to_zval_ex(hedge, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -412,7 +412,7 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, getTagSets) PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state)) { + if (!php_phongo_bson_to_zval_ex(tags, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -458,9 +458,9 @@ static HashTable* php_phongo_readpreference_get_properties_hash(phongo_compat_ob /* Use PHONGO_TYPEMAP_NATIVE_ARRAY for the root type since tags is an * array; however, inner documents and arrays can use the default. */ PHONGO_BSON_INIT_STATE(state); - state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; + state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY; - if (!php_phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state)) { + if (!php_phongo_bson_to_zval_ex(tags, &state)) { zval_ptr_dtor(&state.zchild); goto done; } @@ -483,7 +483,7 @@ static HashTable* php_phongo_readpreference_get_properties_hash(phongo_compat_ob PHONGO_BSON_INIT_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(hedge), hedge->len, &state)) { + if (!php_phongo_bson_to_zval_ex(hedge, &state)) { zval_ptr_dtor(&state.zchild); goto done; } @@ -540,7 +540,7 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, serialize) PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(tags), tags->len, &state)) { + if (!php_phongo_bson_to_zval_ex(tags, &state)) { zval_ptr_dtor(&state.zchild); return; } @@ -557,7 +557,7 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, serialize) PHONGO_BSON_INIT_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(hedge), hedge->len, &state)) { + if (!php_phongo_bson_to_zval_ex(hedge, &state)) { zval_ptr_dtor(&state.zchild); return; } diff --git a/src/MongoDB/Server.c b/src/MongoDB/Server.c index 7ed27f371..f2f5ed4dc 100644 --- a/src/MongoDB/Server.c +++ b/src/MongoDB/Server.c @@ -257,7 +257,7 @@ static PHP_METHOD(MongoDB_Driver_Server, getTags) PHONGO_BSON_INIT_DEBUG_STATE(state); bson_iter_document(&iter, &len, &bytes); - if (!php_phongo_bson_to_zval_ex(bytes, len, &state)) { + if (!php_phongo_bson_data_to_zval_ex(bytes, len, &state)) { /* Exception should already have been thrown */ zval_ptr_dtor(&state.zchild); mongoc_server_description_destroy(sd); @@ -316,7 +316,7 @@ static PHP_METHOD(MongoDB_Driver_Server, getInfo) PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(hello_response), hello_response->len, &state)) { + if (!php_phongo_bson_to_zval_ex(hello_response, &state)) { /* Exception should already have been thrown */ zval_ptr_dtor(&state.zchild); goto cleanup; @@ -640,7 +640,7 @@ bool php_phongo_server_to_zval(zval* retval, mongoc_client_t* client, mongoc_ser PHONGO_BSON_INIT_DEBUG_STATE(state); bson_iter_document(&iter, &len, &bytes); - if (!php_phongo_bson_to_zval_ex(bytes, len, &state)) { + if (!php_phongo_bson_data_to_zval_ex(bytes, len, &state)) { /* Exception already thrown */ zval_ptr_dtor(&state.zchild); return false; @@ -666,7 +666,7 @@ bool php_phongo_server_to_zval(zval* retval, mongoc_client_t* client, mongoc_ser PHONGO_BSON_INIT_DEBUG_STATE(state); handshake_response = mongoc_server_description_hello_response(handshake_sd); - if (!php_phongo_bson_to_zval_ex(bson_get_data(handshake_response), handshake_response->len, &state)) { + if (!php_phongo_bson_to_zval_ex(handshake_response, &state)) { /* Exception already thrown */ mongoc_server_description_destroy(handshake_sd); zval_ptr_dtor(&state.zchild); @@ -680,7 +680,7 @@ bool php_phongo_server_to_zval(zval* retval, mongoc_client_t* client, mongoc_ser PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(hello_response), hello_response->len, &state)) { + if (!php_phongo_bson_to_zval_ex(hello_response, &state)) { /* Exception already thrown */ zval_ptr_dtor(&state.zchild); return false; diff --git a/src/MongoDB/ServerDescription.c b/src/MongoDB/ServerDescription.c index cf7beb7f6..37f735608 100644 --- a/src/MongoDB/ServerDescription.c +++ b/src/MongoDB/ServerDescription.c @@ -67,7 +67,7 @@ static PHP_METHOD(MongoDB_Driver_ServerDescription, getHelloResponse) PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(helloResponse), helloResponse->len, &state)) { + if (!php_phongo_bson_to_zval_ex(helloResponse, &state)) { /* Exception should already have been thrown */ zval_ptr_dtor(&state.zchild); return; @@ -214,7 +214,7 @@ HashTable* php_phongo_serverdescription_get_properties_hash(phongo_compat_object PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(hello_response), hello_response->len, &state)) { + if (!php_phongo_bson_to_zval_ex(hello_response, &state)) { zval_ptr_dtor(&state.zchild); goto done; } diff --git a/src/MongoDB/Session.c b/src/MongoDB/Session.c index 05e900e36..69b35b7cc 100644 --- a/src/MongoDB/Session.c +++ b/src/MongoDB/Session.c @@ -226,7 +226,7 @@ static PHP_METHOD(MongoDB_Driver_Session, getClusterTime) RETURN_NULL(); } - if (!php_phongo_bson_to_zval_ex(bson_get_data(cluster_time), cluster_time->len, &state)) { + if (!php_phongo_bson_to_zval_ex(cluster_time, &state)) { /* Exception should already have been thrown */ zval_ptr_dtor(&state.zchild); return; @@ -251,7 +251,7 @@ static PHP_METHOD(MongoDB_Driver_Session, getLogicalSessionId) lsid = mongoc_client_session_get_lsid(intern->client_session); - if (!php_phongo_bson_to_zval_ex(bson_get_data(lsid), lsid->len, &state)) { + if (!php_phongo_bson_to_zval_ex(lsid, &state)) { /* Exception should already have been thrown */ zval_ptr_dtor(&state.zchild); return; @@ -602,7 +602,7 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(lsid), lsid->len, &state)) { + if (!php_phongo_bson_to_zval_ex(lsid, &state)) { zval_ptr_dtor(&state.zchild); goto done; } @@ -618,7 +618,7 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler PHONGO_BSON_INIT_DEBUG_STATE(state); - if (!php_phongo_bson_to_zval_ex(bson_get_data(cluster_time), cluster_time->len, &state)) { + if (!php_phongo_bson_to_zval_ex(cluster_time, &state)) { zval_ptr_dtor(&state.zchild); goto done; } diff --git a/src/MongoDB/WriteConcernError.c b/src/MongoDB/WriteConcernError.c index 315d24452..53bc84b62 100644 --- a/src/MongoDB/WriteConcernError.c +++ b/src/MongoDB/WriteConcernError.c @@ -158,7 +158,7 @@ zend_bool phongo_writeconcernerror_init(zval* return_value, bson_t* bson) bson_iter_document(&iter, &len, &data); - if (!php_phongo_bson_to_zval(data, len, &intern->info)) { + if (!php_phongo_bson_data_to_zval(data, len, &intern->info)) { zval_ptr_dtor(&intern->info); ZVAL_UNDEF(&intern->info); diff --git a/src/MongoDB/WriteError.c b/src/MongoDB/WriteError.c index c6c09f24c..4cd574f98 100644 --- a/src/MongoDB/WriteError.c +++ b/src/MongoDB/WriteError.c @@ -173,7 +173,7 @@ zend_bool phongo_writeerror_init(zval* return_value, bson_t* bson) bson_iter_document(&iter, &len, &data); - if (!php_phongo_bson_to_zval(data, len, &intern->info)) { + if (!php_phongo_bson_data_to_zval(data, len, &intern->info)) { zval_ptr_dtor(&intern->info); ZVAL_UNDEF(&intern->info); diff --git a/src/MongoDB/WriteResult.c b/src/MongoDB/WriteResult.c index 5c0f78f40..f9736f0e3 100644 --- a/src/MongoDB/WriteResult.c +++ b/src/MongoDB/WriteResult.c @@ -230,7 +230,7 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getUpsertedIds) /* Use PHONGO_TYPEMAP_NATIVE_ARRAY for the root type so we can * easily access the "index" and "_id" fields. */ PHONGO_BSON_INIT_STATE(state); - state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; + state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY; if (!BSON_ITER_HOLDS_DOCUMENT(&child)) { continue; @@ -238,7 +238,7 @@ static PHP_METHOD(MongoDB_Driver_WriteResult, getUpsertedIds) bson_iter_document(&child, &data_len, &data); - if (php_phongo_bson_to_zval_ex(data, data_len, &state)) { + if (php_phongo_bson_data_to_zval_ex(data, data_len, &state)) { zval* zid = php_array_fetchc(&state.zchild, "_id"); add_index_zval(return_value, php_array_fetchc_long(&state.zchild, "index"), zid); zval_add_ref(zid); @@ -351,7 +351,7 @@ static HashTable* php_phongo_writeresult_get_debug_info(phongo_compat_object_han PHONGO_BSON_INIT_DEBUG_STATE(state); bson_iter_array(&iter, &len, &data); - if (!php_phongo_bson_to_zval_ex(data, len, &state)) { + if (!php_phongo_bson_data_to_zval_ex(data, len, &state)) { zval_ptr_dtor(&state.zchild); goto done; } diff --git a/src/phongo_bson.c b/src/phongo_bson.c index c79d9c9c2..a3e1fb8a1 100644 --- a/src/phongo_bson.c +++ b/src/phongo_bson.c @@ -37,7 +37,7 @@ /* 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); +static bool php_phongo_bson_visit_array(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_t* v_array, void* data); /* Path builder */ char* php_phongo_field_path_as_string(php_phongo_field_path* field_path) @@ -502,7 +502,7 @@ static bool php_phongo_bson_new_javascript_from_javascript_and_scope(zval* objec PHONGO_BSON_INIT_STATE(state); - valid_scope = php_phongo_bson_to_zval_ex(bson_get_data(scope), scope->len, &state); + valid_scope = php_phongo_bson_to_zval_ex(scope, &state); zval_ptr_dtor(&state.zchild); if (!valid_scope) { @@ -747,19 +747,19 @@ static php_phongo_field_path_map_element* map_find_field_path_entry(php_phongo_b 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) +static void php_phongo_handle_field_path_entry_for_compound_type(php_phongo_bson_state* state, php_phongo_bson_typemap_element* element) { php_phongo_field_path_map_element* entry = map_find_field_path_entry(state); if (entry) { - switch (entry->node_type) { + switch (entry->node.type) { case PHONGO_TYPEMAP_NATIVE_ARRAY: case PHONGO_TYPEMAP_NATIVE_OBJECT: - *type = entry->node_type; + element->type = entry->node.type; break; case PHONGO_TYPEMAP_CLASS: - *type = entry->node_type; - *ce = entry->node_ce; + element->type = entry->node.type; + element->ce = entry->node.ce; break; default: /* Do nothing - pacify compiler */ @@ -817,120 +817,121 @@ static zval* resolve_enum_case(zend_class_entry* ce, const char* case_name) } #endif /* PHP_VERSION_ID >= 80100 */ +static bool php_phongo_bson_init_document_object(zval* src, zend_class_entry* obj_ce, zval* obj) +{ +#if PHP_VERSION_ID >= 80100 + /* Enums require special handling for instantiation */ + if (obj_ce->ce_flags & ZEND_ACC_ENUM) { + int plen; + zend_bool pfree; + char* case_name; + zval* enum_case; + + case_name = php_array_fetchc_string(src, "name", &plen, &pfree); + + if (!case_name) { + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Missing 'name' field to infer enum case for %s", ZSTR_VAL(obj_ce->name)); + + return false; + } + + enum_case = resolve_enum_case(obj_ce, case_name); + + if (pfree) { + efree(case_name); + } + + if (!enum_case) { + /* Exception already thrown */ + return false; + } + + ZVAL_COPY(obj, enum_case); + } else { + object_init_ex(obj, obj_ce); + } +#else /* PHP_VERSION_ID < 80100 */ + object_init_ex(obj, obj_ce); +#endif /* PHP_VERSION_ID */ + + return true; +} + 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; + php_phongo_bson_state state; - php_phongo_field_path_push(parent_state->field_path, key, PHONGO_FIELD_PATH_ITEM_DOCUMENT); + if (!bson_iter_init(&child, v_document)) { + return false; + } - if (bson_iter_init(&child, v_document)) { - php_phongo_bson_state state; + php_phongo_field_path_push(parent_state->field_path, key, PHONGO_FIELD_PATH_ITEM_DOCUMENT); - PHONGO_BSON_INIT_STATE(state); - php_phongo_bson_state_copy_ctor(&state, parent_state); + PHONGO_BSON_INIT_STATE(state); + php_phongo_bson_state_copy_ctor(&state, parent_state); - array_init(&state.zchild); + array_init(&state.zchild); - 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 (bson_iter_visit_all(&child, &php_bson_visitors, &state) || child.err_off) { + /* 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; + } - /* 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; - } + /* 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); - switch (state.map.document_type) { - case PHONGO_TYPEMAP_NATIVE_ARRAY: - if (((php_phongo_bson_state*) data)->is_visiting_array) { - add_next_index_zval(retval, &state.zchild); - } else { - ADD_ASSOC_ZVAL(retval, key, &state.zchild); - } - break; + /* 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; + } - case PHONGO_TYPEMAP_CLASS: { - zval obj; - zend_class_entry* obj_ce = state.odm ? state.odm : state.map.document; + switch (state.map.document.type) { + case PHONGO_TYPEMAP_NATIVE_ARRAY: + /* Do nothing, item will be added later */ + break; -#if PHP_VERSION_ID >= 80100 - /* Enums require special handling for instantiation */ - if (obj_ce->ce_flags & ZEND_ACC_ENUM) { - int plen; - zend_bool pfree; - char* case_name; - zval* enum_case; - - case_name = php_array_fetchc_string(&state.zchild, "name", &plen, &pfree); - - if (!case_name) { - phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Missing 'name' field to infer enum case for %s", ZSTR_VAL(obj_ce->name)); - - /* Clean up and return true to stop iteration for - * our parent context. */ - zval_ptr_dtor(&state.zchild); - php_phongo_bson_state_dtor(&state); - return true; - } - - enum_case = resolve_enum_case(obj_ce, case_name); - - if (pfree) { - efree(case_name); - } - - if (!enum_case) { - /* Exception already thrown. Clean up and return - * true to stop iteration for our parent context. */ - zval_ptr_dtor(&state.zchild); - php_phongo_bson_state_dtor(&state); - return true; - } - - ZVAL_COPY(&obj, enum_case); - } else { - object_init_ex(&obj, obj_ce); - } -#else /* PHP_VERSION_ID < 80100 */ - object_init_ex(&obj, obj_ce); -#endif /* PHP_VERSION_ID */ + case PHONGO_TYPEMAP_CLASS: { + zval obj; + zend_class_entry* obj_ce = state.odm ? state.odm : state.map.document.ce; + + if (!php_phongo_bson_init_document_object(&state.zchild, obj_ce, &obj)) { + /* Exception already thrown. Clean up and return + * true to stop iteration for our parent context. */ + zval_ptr_dtor(&state.zchild); + php_phongo_bson_state_dtor(&state); + return true; + } - zend_call_method_with_1_params(PHONGO_COMPAT_OBJ_P(&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); - break; - } + zend_call_method_with_1_params(PHONGO_COMPAT_OBJ_P(&obj), NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, &state.zchild); - case PHONGO_TYPEMAP_NATIVE_OBJECT: - default: - 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 { - /* 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; + ZVAL_COPY_VALUE(&state.zchild, &obj); + break; } - php_phongo_bson_state_dtor(&state); - php_phongo_field_path_pop(parent_state->field_path); + case PHONGO_TYPEMAP_NATIVE_OBJECT: + default: + convert_to_object(&state.zchild); } + if (parent_state->is_visiting_array) { + add_next_index_zval(retval, &state.zchild); + } else { + ADD_ASSOC_ZVAL(retval, key, &state.zchild); + } + + php_phongo_bson_state_dtor(&state); + php_phongo_field_path_pop(parent_state->field_path); + return false; } @@ -939,86 +940,94 @@ static bool php_phongo_bson_visit_array(const bson_iter_t* iter ARG_UNUSED, cons zval* retval = PHONGO_BSON_STATE_ZCHILD(data); bson_iter_t child; php_phongo_bson_state* parent_state = (php_phongo_bson_state*) data; + php_phongo_bson_state state; + + if (!bson_iter_init(&child, v_array)) { + return false; + } 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_INIT_STATE(state); + php_phongo_bson_state_copy_ctor(&state, parent_state); - PHONGO_BSON_INIT_STATE(state); - 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; - - array_init(&state.zchild); - - 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: { - zval obj; - - object_init_ex(&obj, state.map.array); - zend_call_method_with_1_params(PHONGO_COMPAT_OBJ_P(&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); - break; - } + /* 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; - case PHONGO_TYPEMAP_NATIVE_OBJECT: - 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); - } - break; - - case PHONGO_TYPEMAP_NATIVE_ARRAY: - default: - if (((php_phongo_bson_state*) data)->is_visiting_array) { - add_next_index_zval(retval, &state.zchild); - } else { - ADD_ASSOC_ZVAL(retval, key, &state.zchild); - } - break; - } - } 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. */ + array_init(&state.zchild); + + if (bson_iter_visit_all(&child, &php_bson_visitors, &state) || child.err_off) { + /* 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; + } + + /* 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); + + switch (state.map.array.type) { + case PHONGO_TYPEMAP_CLASS: { + zval obj; + + object_init_ex(&obj, state.map.array.ce); + zend_call_method_with_1_params(PHONGO_COMPAT_OBJ_P(&obj), NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, &state.zchild); zval_ptr_dtor(&state.zchild); - php_phongo_bson_state_dtor(&state); - return true; + ZVAL_COPY_VALUE(&state.zchild, &obj); + break; } - php_phongo_bson_state_dtor(&state); - php_phongo_field_path_pop(parent_state->field_path); + case PHONGO_TYPEMAP_NATIVE_OBJECT: + convert_to_object(&state.zchild); + break; + + case PHONGO_TYPEMAP_NATIVE_ARRAY: + default: + /* Do nothing, will be added later */ + break; + } + + if (parent_state->is_visiting_array) { + add_next_index_zval(retval, &state.zchild); + } else { + ADD_ASSOC_ZVAL(retval, key, &state.zchild); } + php_phongo_bson_state_dtor(&state); + php_phongo_field_path_pop(parent_state->field_path); + return false; } /* Converts a BSON document to a PHP value using the default typemap. */ -bool php_phongo_bson_to_zval(const unsigned char* data, int data_len, zval* zv) +bool php_phongo_bson_to_zval(const bson_t* b, zval* zv) +{ + bool retval; + php_phongo_bson_state state; + + PHONGO_BSON_INIT_STATE(state); + + retval = php_phongo_bson_to_zval_ex(b, &state); + ZVAL_ZVAL(zv, &state.zchild, 1, 1); + + return retval; +} + +/* Converts BSON data to a PHP value using the default typemap. */ +bool php_phongo_bson_data_to_zval(const unsigned char* data, int data_len, zval* zv) { bool retval; php_phongo_bson_state state; PHONGO_BSON_INIT_STATE(state); - retval = php_phongo_bson_to_zval_ex(data, data_len, &state); + retval = php_phongo_bson_data_to_zval_ex(data, data_len, &state); ZVAL_ZVAL(zv, &state.zchild, 1, 1); return retval; @@ -1033,10 +1042,10 @@ bool php_phongo_bson_value_to_zval(const bson_value_t* value, zval* zv) bool retval = false; PHONGO_BSON_INIT_STATE(state); - state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; + state.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY; bson_append_value(&bson, "data", 4, value); - if (!php_phongo_bson_to_zval_ex(bson_get_data(&bson), bson.len, &state)) { + if (!php_phongo_bson_to_zval_ex(&bson, &state)) { /* Exception already thrown */ goto cleanup; } @@ -1066,28 +1075,17 @@ bool php_phongo_bson_value_to_zval(const bson_value_t* value, zval* zv) * as-is on PHP 7; however, it should have the type undefined if the state * was initialized to zero. */ -bool php_phongo_bson_to_zval_ex(const unsigned char* data, int data_len, php_phongo_bson_state* state) +bool php_phongo_bson_to_zval_ex(const bson_t* b, php_phongo_bson_state* state) { - bson_reader_t* reader = NULL; - bson_iter_t iter; - const bson_t* b; - bool eof = false; - bool retval = false; - bool must_dtor_state = false; + bson_iter_t iter; + bool retval = false; + bool must_dtor_state = false; if (!php_phongo_bson_state_is_initialized(state)) { php_phongo_bson_state_ctor(state); must_dtor_state = true; } - reader = bson_reader_new_from_data(data, data_len); - - if (!(b = bson_reader_read(reader, NULL))) { - phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Could not read document from BSON reader"); - - goto cleanup; - } - if (!bson_iter_init(&iter, b)) { phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Could not initialize BSON iterator"); @@ -1116,18 +1114,18 @@ bool php_phongo_bson_to_zval_ex(const unsigned char* data, int data_len, php_pho /* If php_phongo_bson_visit_binary() finds an ODM class, it should supersede * a default type map and named root class. */ - if (state->odm && state->map.root_type == PHONGO_TYPEMAP_NONE) { - state->map.root_type = PHONGO_TYPEMAP_CLASS; + if (state->odm && state->map.root.type == PHONGO_TYPEMAP_NONE) { + state->map.root.type = PHONGO_TYPEMAP_CLASS; } - switch (state->map.root_type) { + switch (state->map.root.type) { case PHONGO_TYPEMAP_NATIVE_ARRAY: /* Nothing to do here */ break; case PHONGO_TYPEMAP_CLASS: { zval obj; - zend_class_entry* obj_ce = state->odm ? state->odm : state->map.root; + zend_class_entry* obj_ce = state->odm ? state->odm : state->map.root.ce; #if PHP_VERSION_ID >= 80100 /* Enums require special handling for instantiation */ @@ -1176,21 +1174,56 @@ bool php_phongo_bson_to_zval_ex(const unsigned char* data, int data_len, php_pho convert_to_object(&state->zchild); } + retval = true; + +cleanup: + if (must_dtor_state) { + php_phongo_bson_state_dtor(state); + } + + return retval; +} + +/* Converts a BSON document to a PHP value according to the typemap specified in + * the state argument. + * + * On success, the result will be set on the state argument and true will be + * returned. On error, an exception will have been thrown and false will be + * returned. + * + * Note: the result zval in the state argument will always be initialized for + * PHP 5.x so that the caller may always zval_ptr_dtor() it. The zval is left + * as-is on PHP 7; however, it should have the type undefined if the state + * was initialized to zero. + */ +bool php_phongo_bson_data_to_zval_ex(const unsigned char* data, int data_len, php_phongo_bson_state* state) +{ + bson_reader_t* reader = NULL; + const bson_t* b; + bool eof = false; + bool retval = false; + + reader = bson_reader_new_from_data(data, data_len); + + if (!(b = bson_reader_read(reader, NULL))) { + phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Could not read document from BSON reader"); + + goto cleanup; + } + + retval = php_phongo_bson_to_zval_ex(b, state); + if (bson_reader_read(reader, &eof) || !eof) { phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Reading document did not exhaust input buffer"); + retval = false; goto cleanup; } - retval = true; - cleanup: if (reader) { bson_reader_destroy(reader); } - if (must_dtor_state) { - php_phongo_bson_state_dtor(state); - } return retval; } @@ -1220,7 +1253,7 @@ static zend_class_entry* php_phongo_bson_state_fetch_class(const char* classname /* Parses a BSON type (i.e. array, document, or root). On success, the type and * type_ce output arguments will be assigned and true will be returned; * otherwise, false is returned and an exception is thrown. */ -static bool php_phongo_bson_state_parse_type(zval* options, const char* name, php_phongo_bson_typemap_types* type, zend_class_entry** type_ce) +static bool php_phongo_bson_state_parse_type(zval* options, const char* name, php_phongo_bson_typemap_element* element) { char* classname; int classname_len; @@ -1234,14 +1267,14 @@ static bool php_phongo_bson_state_parse_type(zval* options, const char* name, ph } if (!strcasecmp(classname, "array")) { - *type = PHONGO_TYPEMAP_NATIVE_ARRAY; - *type_ce = NULL; + element->type = PHONGO_TYPEMAP_NATIVE_ARRAY; + element->ce = NULL; } else if (!strcasecmp(classname, "stdclass") || !strcasecmp(classname, "object")) { - *type = PHONGO_TYPEMAP_NATIVE_OBJECT; - *type_ce = NULL; + element->type = PHONGO_TYPEMAP_NATIVE_OBJECT; + element->ce = NULL; } else { - if ((*type_ce = php_phongo_bson_state_fetch_class(classname, classname_len, php_phongo_unserializable_ce))) { - *type = PHONGO_TYPEMAP_CLASS; + if ((element->ce = php_phongo_bson_state_fetch_class(classname, classname_len, php_phongo_unserializable_ce))) { + element->type = PHONGO_TYPEMAP_CLASS; } else { retval = false; } @@ -1255,10 +1288,10 @@ static bool php_phongo_bson_state_parse_type(zval* options, const char* name, ph return retval; } -static void field_path_map_element_set_info(php_phongo_field_path_map_element* element, php_phongo_bson_typemap_types type, zend_class_entry* ce) +static void field_path_map_element_set_info(php_phongo_field_path_map_element* element, php_phongo_bson_typemap_element* typemap_element) { - element->node_type = type; - element->node_ce = ce; + element->node.type = typemap_element->type; + element->node.ce = typemap_element->ce; } static void map_add_field_path_element(php_phongo_bson_typemap* map, php_phongo_field_path_map_element* element) @@ -1288,7 +1321,7 @@ static void field_path_map_element_dtor(php_phongo_field_path_map_element* eleme efree(element); } -static bool php_phongo_bson_state_add_field_path(php_phongo_bson_typemap* map, char* field_path_original, php_phongo_bson_typemap_types type, zend_class_entry* ce) +static bool php_phongo_bson_state_add_field_path(php_phongo_bson_typemap* map, char* field_path_original, php_phongo_bson_typemap_element* typemap_element) { char* ptr = NULL; char* segment_end = NULL; @@ -1329,7 +1362,7 @@ static bool php_phongo_bson_state_add_field_path(php_phongo_bson_typemap* map, c /* Add the last (or single) element */ php_phongo_field_path_push(field_path_map_element->entry, ptr, PHONGO_FIELD_PATH_ITEM_NONE); - field_path_map_element_set_info(field_path_map_element, type, ce); + field_path_map_element_set_info(field_path_map_element, typemap_element); map_add_field_path_element(map, field_path_map_element); return true; @@ -1376,8 +1409,7 @@ static bool php_phongo_bson_state_parse_fieldpaths(zval* typemap, php_phongo_bso ZEND_HASH_FOREACH_KEY_VAL(ht_data, num_key, string_key, property) { - zend_class_entry* map_ce = NULL; - php_phongo_bson_typemap_types map_type; + php_phongo_bson_typemap_element element; if (!string_key) { phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "The 'fieldPaths' element is not an associative array"); @@ -1389,11 +1421,11 @@ static bool php_phongo_bson_state_parse_fieldpaths(zval* typemap, php_phongo_bso return false; } - if (!php_phongo_bson_state_parse_type(fieldpaths, ZSTR_VAL(string_key), &map_type, &map_ce)) { + if (!php_phongo_bson_state_parse_type(fieldpaths, ZSTR_VAL(string_key), &element)) { return false; } - if (!php_phongo_bson_state_add_field_path(map, ZSTR_VAL(string_key), map_type, map_ce)) { + if (!php_phongo_bson_state_add_field_path(map, ZSTR_VAL(string_key), &element)) { return false; } } @@ -1411,9 +1443,9 @@ bool php_phongo_bson_typemap_to_state(zval* typemap, php_phongo_bson_typemap* ma return true; } - if (!php_phongo_bson_state_parse_type(typemap, "array", &map->array_type, &map->array) || - !php_phongo_bson_state_parse_type(typemap, "document", &map->document_type, &map->document) || - !php_phongo_bson_state_parse_type(typemap, "root", &map->root_type, &map->root) || + if (!php_phongo_bson_state_parse_type(typemap, "array", &map->array) || + !php_phongo_bson_state_parse_type(typemap, "document", &map->document) || + !php_phongo_bson_state_parse_type(typemap, "root", &map->root) || !php_phongo_bson_state_parse_fieldpaths(typemap, map)) { /* Exception should already have been thrown */ diff --git a/src/phongo_bson.h b/src/phongo_bson.h index 7732979a8..57398eb8b 100644 --- a/src/phongo_bson.h +++ b/src/phongo_bson.h @@ -48,18 +48,19 @@ typedef enum { } php_phongo_bson_typemap_types; typedef struct { - php_phongo_field_path* entry; - php_phongo_bson_typemap_types node_type; - zend_class_entry* node_ce; + php_phongo_bson_typemap_types type; + zend_class_entry* ce; +} php_phongo_bson_typemap_element; + +typedef struct { + php_phongo_field_path* entry; + php_phongo_bson_typemap_element node; } php_phongo_field_path_map_element; typedef struct { - php_phongo_bson_typemap_types document_type; - zend_class_entry* document; - php_phongo_bson_typemap_types array_type; - zend_class_entry* array; - php_phongo_bson_typemap_types root_type; - zend_class_entry* root; + php_phongo_bson_typemap_element document; + php_phongo_bson_typemap_element array; + php_phongo_bson_typemap_element root; struct { php_phongo_field_path_map_element** map; size_t allocated_size; @@ -83,8 +84,8 @@ typedef struct { #define PHONGO_BSON_INIT_DEBUG_STATE(s) \ do { \ memset(&(s), 0, sizeof(php_phongo_bson_state)); \ - s.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \ - s.map.document_type = PHONGO_TYPEMAP_NATIVE_ARRAY; \ + s.map.root.type = PHONGO_TYPEMAP_NATIVE_ARRAY; \ + s.map.document.type = PHONGO_TYPEMAP_NATIVE_ARRAY; \ } while (0) char* php_phongo_field_path_as_string(php_phongo_field_path* field_path); @@ -95,8 +96,10 @@ void php_phongo_field_path_write_type_at_current_level(php_pho bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element, php_phongo_bson_field_path_item_types element_type); bool php_phongo_field_path_pop(php_phongo_field_path* field_path); -bool php_phongo_bson_to_zval(const unsigned char* data, int data_len, zval* out); -bool php_phongo_bson_to_zval_ex(const unsigned char* data, int data_len, php_phongo_bson_state* state); +bool php_phongo_bson_to_zval(const bson_t* b, zval* zv); +bool php_phongo_bson_to_zval_ex(const bson_t* b, php_phongo_bson_state* state); +bool php_phongo_bson_data_to_zval(const unsigned char* data, int data_len, zval* zv); +bool php_phongo_bson_data_to_zval_ex(const unsigned char* data, int data_len, php_phongo_bson_state* state); bool php_phongo_bson_value_to_zval(const bson_value_t* value, zval* zv); diff --git a/src/phongo_error.c b/src/phongo_error.c index 25ac25635..76be9f81b 100644 --- a/src/phongo_error.c +++ b/src/phongo_error.c @@ -194,7 +194,7 @@ void phongo_throw_exception_from_bson_error_t_and_reply(bson_error_t* error, con zval zv; zend_throw_exception(php_phongo_commandexception_ce, error->message, error->code); - if (php_phongo_bson_to_zval(bson_get_data(reply), reply->len, &zv)) { + if (php_phongo_bson_to_zval(reply, &zv)) { phongo_add_exception_prop(ZEND_STRL("resultDocument"), &zv); }