Skip to content

Commit 663de30

Browse files
committed
refactor: change use php_brotli_context in brotli_uncompress()
1 parent 2d4c1ee commit 663de30

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

brotli.c

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,31 @@ static int php_brotli_context_create_encoder_ex(php_brotli_context *ctx,
210210
#define php_brotli_context_create_encoder(ctx, level, lgwin, mode) \
211211
php_brotli_context_create_encoder_ex(ctx, level, lgwin, mode, 0)
212212

213-
static int php_brotli_context_create_decoder(php_brotli_context *ctx)
213+
static int php_brotli_context_create_decoder_ex(php_brotli_context *ctx,
214+
int fail)
214215
{
215216
ctx->decoder = BrotliDecoderCreateInstance(NULL, NULL, NULL);
216217
if (!ctx->decoder) {
218+
php_error_docref(NULL, E_WARNING, "%sfailed to prepare uncompression",
219+
(fail ? "" : "brotli: "));
220+
221+
return FAILURE;
222+
}
223+
224+
if (!BrotliDecoderSetParameter(ctx->decoder,
225+
BROTLI_DECODER_PARAM_LARGE_WINDOW, 1u)) {
226+
php_error_docref(NULL, E_WARNING,
227+
"%sfailed to set uncompression parameters",
228+
(fail ? "" : "brotli: "));
217229
return FAILURE;
218230
}
219231

220232
return SUCCESS;
221233
}
222234

235+
#define php_brotli_context_create_decoder(ctx) \
236+
php_brotli_context_create_decoder_ex(ctx, 0)
237+
223238
static void php_brotli_context_close(php_brotli_context *ctx)
224239
{
225240
if (ctx->encoder) {
@@ -1406,33 +1421,34 @@ static ZEND_FUNCTION(brotli_uncompress)
14061421
in_size = max_size;
14071422
}
14081423

1409-
BrotliDecoderState *state = BrotliDecoderCreateInstance(NULL, NULL, NULL);
1410-
if (!state) {
1411-
php_error_docref(NULL, E_WARNING, "failed to prepare uncompress");
1424+
php_brotli_context ctx;
1425+
php_brotli_context_init(&ctx);
1426+
if (php_brotli_context_create_decoder_ex(&ctx, 1) != SUCCESS) {
1427+
php_brotli_context_close(&ctx);
14121428
RETURN_FALSE;
14131429
}
14141430

1415-
BrotliDecoderSetParameter(state, BROTLI_DECODER_PARAM_LARGE_WINDOW, 1u);
1416-
1417-
size_t available_in = in_size;
1418-
const uint8_t *next_in = (const uint8_t *)in;
1431+
ctx.available_in = in_size;
1432+
ctx.next_in = (const uint8_t *)in;
14191433
size_t buffer_size = PHP_BROTLI_BUFFER_SIZE;
14201434
uint8_t *buffer = (uint8_t *)emalloc(buffer_size);
1421-
14221435
BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
14231436
while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
1424-
size_t available_out = buffer_size;
1425-
uint8_t *next_out = buffer;
1426-
result = BrotliDecoderDecompressStream(state, &available_in, &next_in,
1427-
&available_out, &next_out,
1437+
ctx.available_out = buffer_size;
1438+
ctx.next_out = buffer;
1439+
result = BrotliDecoderDecompressStream(ctx.decoder,
1440+
&ctx.available_in,
1441+
&ctx.next_in,
1442+
&ctx.available_out,
1443+
&ctx.next_out,
14281444
0);
1429-
size_t used_out = buffer_size - available_out;
1445+
size_t used_out = (size_t)(buffer_size - ctx.available_out);
14301446
if (used_out != 0) {
14311447
smart_string_appendl(&out, buffer, used_out);
14321448
}
14331449
}
14341450

1435-
BrotliDecoderDestroyInstance(state);
1451+
php_brotli_context_close(&ctx);
14361452
efree(buffer);
14371453

14381454
if (result != BROTLI_DECODER_RESULT_SUCCESS) {
@@ -1453,9 +1469,8 @@ static ZEND_FUNCTION(brotli_uncompress_init)
14531469

14541470
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS(php_brotli_uncompress_context_ce);
14551471

1456-
if (php_brotli_context_create_decoder(ctx) != SUCCESS) {
1457-
php_error_docref(NULL, E_WARNING,
1458-
"failed to prepare incremental uncompress");
1472+
if (php_brotli_context_create_decoder_ex(ctx, 1) != SUCCESS) {
1473+
zval_ptr_dtor(return_value);
14591474
RETURN_FALSE;
14601475
}
14611476
}

0 commit comments

Comments
 (0)