Skip to content

Commit 0b6e4ef

Browse files
committed
change: brotli.state resource in brotli_uncompress_{init,add}() to Btorli\UnCompress\Context class
1 parent 8affbed commit 0b6e4ef

File tree

1 file changed

+99
-18
lines changed

1 file changed

+99
-18
lines changed

brotli.c

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,48 @@ static zend_class_entry *php_brotli_compress_context_register_class(void)
947947
return class_entry;
948948
}
949949

950+
/* Brotli UnCompress Context */
951+
zend_class_entry *php_brotli_uncompress_context_ce;
952+
static zend_object_handlers php_brotli_uncompress_context_object_handlers;
953+
954+
static zend_object *
955+
php_brotli_uncompress_context_create_object(zend_class_entry *class_type)
956+
{
957+
return php_brotli_context_create_object(
958+
class_type,
959+
&php_brotli_uncompress_context_object_handlers);
960+
}
961+
962+
static zend_function *
963+
php_brotli_uncompress_context_get_constructor(zend_object *object)
964+
{
965+
zend_throw_error(NULL,
966+
"Cannot directly construct Brotli\\UnCompress\\Context, "
967+
"use brotli_uncompress_init() instead");
968+
return NULL;
969+
}
970+
971+
static zend_class_entry *php_brotli_uncompress_context_register_class(void)
972+
{
973+
zend_class_entry ce, *class_entry;
974+
975+
INIT_NS_CLASS_ENTRY(ce, "Brotli\\UnCompress", "Context", NULL);
976+
#if PHP_VERSION_ID >= 80000
977+
class_entry = zend_register_internal_class_ex(&ce, NULL);
978+
#if PHP_VERSION_ID >= 80100
979+
class_entry->ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES|ZEND_ACC_NOT_SERIALIZABLE;
980+
#else
981+
class_entry->ce_flags |= ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES;
982+
#endif
983+
#else
984+
ce.create_object = php_brotli_uncompress_context_create_object;
985+
class_entry = zend_register_internal_class(&ce);
986+
class_entry->ce_flags |= ZEND_ACC_FINAL;
987+
#endif
988+
989+
return class_entry;
990+
}
991+
950992
ZEND_MINIT_FUNCTION(brotli)
951993
{
952994
ZEND_INIT_MODULE_GLOBALS(brotli, php_brotli_init_globals, NULL);
@@ -1010,6 +1052,34 @@ ZEND_MINIT_FUNCTION(brotli)
10101052
= zend_objects_not_comparable;
10111053
#endif
10121054

1055+
php_brotli_uncompress_context_ce
1056+
= php_brotli_uncompress_context_register_class();
1057+
#if PHP_VERSION_ID >= 80000
1058+
php_brotli_uncompress_context_ce->create_object
1059+
= php_brotli_uncompress_context_create_object;
1060+
#if PHP_VERSION_ID >= 80300
1061+
php_brotli_uncompress_context_ce->default_object_handlers
1062+
= &php_brotli_uncompress_context_object_handlers;
1063+
#endif
1064+
#if PHP_VERSION_ID < 80100
1065+
php_brotli_uncompress_context_ce->serialize = zend_class_serialize_deny;
1066+
php_brotli_uncompress_context_ce->unserialize = zend_class_unserialize_deny;
1067+
#endif
1068+
#endif
1069+
memcpy(&php_brotli_uncompress_context_object_handlers,
1070+
&std_object_handlers, sizeof(zend_object_handlers));
1071+
php_brotli_uncompress_context_object_handlers.offset
1072+
= XtOffsetOf(php_brotli_context, std);
1073+
php_brotli_uncompress_context_object_handlers.free_obj
1074+
= php_brotli_context_free_obj;
1075+
php_brotli_uncompress_context_object_handlers.get_constructor
1076+
= php_brotli_uncompress_context_get_constructor;
1077+
php_brotli_uncompress_context_object_handlers.clone_obj = NULL;
1078+
#if PHP_VERSION_ID >= 80000
1079+
php_brotli_uncompress_context_object_handlers.compare
1080+
= zend_objects_not_comparable;
1081+
#endif
1082+
10131083
REGISTER_INI_ENTRIES();
10141084

10151085
php_register_url_stream_wrapper(STREAM_NAME,
@@ -1323,42 +1393,51 @@ static ZEND_FUNCTION(brotli_uncompress)
13231393

13241394
static ZEND_FUNCTION(brotli_uncompress_init)
13251395
{
1326-
php_brotli_state_context *ctx;
1327-
13281396
if (zend_parse_parameters_none() == FAILURE) {
13291397
RETURN_FALSE;
13301398
}
13311399

1332-
ctx = php_brotli_state_init();
1400+
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS(php_brotli_uncompress_context_ce);
13331401

1334-
if (php_brotli_decoder_create(&ctx->decoder) != SUCCESS) {
1402+
if (php_brotli_decoder_create(&ctx->state.decoder) != SUCCESS) {
13351403
php_error_docref(NULL, E_WARNING,
13361404
"Brotli incremental uncompress init failed");
13371405
RETURN_FALSE;
13381406
}
1339-
1340-
RETURN_RES(zend_register_resource(ctx, le_state));
13411407
}
13421408

13431409
static ZEND_FUNCTION(brotli_uncompress_add)
13441410
{
13451411
zval *res;
1346-
php_brotli_state_context *ctx;
1412+
php_brotli_context *ctx;
13471413
size_t buffer_size;
13481414
zend_long mode = BROTLI_OPERATION_FLUSH;
1415+
#if PHP_VERSION_ID >= 80000
1416+
zend_object *obj;
1417+
#else
1418+
zval *obj;
1419+
#endif
13491420
char *in_buf;
13501421
size_t in_size;
13511422
smart_string out = {0};
13521423

13531424
ZEND_PARSE_PARAMETERS_START(2, 3)
1354-
Z_PARAM_RESOURCE(res)
1425+
#if PHP_VERSION_ID >= 80000
1426+
Z_PARAM_OBJ_OF_CLASS(obj, php_brotli_uncompress_context_ce)
1427+
#else
1428+
Z_PARAM_OBJECT_OF_CLASS(obj, php_brotli_uncompress_context_ce)
1429+
#endif
13551430
Z_PARAM_STRING(in_buf, in_size)
13561431
Z_PARAM_OPTIONAL
13571432
Z_PARAM_LONG(mode)
13581433
ZEND_PARSE_PARAMETERS_END();
13591434

1360-
ctx = zend_fetch_resource(Z_RES_P(res), NULL, le_state);
1361-
if (ctx == NULL || ctx->decoder == NULL) {
1435+
#if PHP_VERSION_ID >= 80000
1436+
ctx = php_brotli_context_from_obj(obj);
1437+
#else
1438+
ctx = php_brotli_context_from_obj(Z_OBJ_P(obj));
1439+
#endif
1440+
if (ctx == NULL || ctx->state.decoder == NULL) {
13621441
php_error_docref(NULL, E_WARNING,
13631442
"Brotli incremental uncompress resource failed");
13641443
RETURN_FALSE;
@@ -1371,18 +1450,20 @@ static ZEND_FUNCTION(brotli_uncompress_add)
13711450
buffer_size = PHP_BROTLI_BUFFER_SIZE;
13721451
uint8_t *buffer = (uint8_t *)emalloc(buffer_size);
13731452

1374-
const uint8_t *next_in = (const uint8_t *)in_buf;
1375-
size_t available_in = in_size;
1453+
ctx->next_in = (const uint8_t *)in_buf;
1454+
ctx->available_in = in_size;
13761455

13771456
BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
13781457
while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
1379-
size_t available_out = buffer_size;
1380-
uint8_t *next_out = buffer;
1381-
result = BrotliDecoderDecompressStream(ctx->decoder,
1382-
&available_in, &next_in,
1383-
&available_out, &next_out,
1458+
ctx->available_out = buffer_size;
1459+
ctx->next_out = buffer;
1460+
result = BrotliDecoderDecompressStream(ctx->state.decoder,
1461+
&ctx->available_in,
1462+
&ctx->next_in,
1463+
&ctx->available_out,
1464+
&ctx->next_out,
13841465
0);
1385-
size_t buffer_used = buffer_size - available_out;
1466+
size_t buffer_used = buffer_size - ctx->available_out;
13861467
if (buffer_used) {
13871468
smart_string_appendl(&out, buffer, buffer_used);
13881469
}

0 commit comments

Comments
 (0)