@@ -210,16 +210,31 @@ static int php_brotli_context_create_encoder_ex(php_brotli_context *ctx,
210
210
#define php_brotli_context_create_encoder (ctx , level , lgwin , mode ) \
211
211
php_brotli_context_create_encoder_ex(ctx, level, lgwin, mode, 0)
212
212
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 )
214
215
{
215
216
ctx -> decoder = BrotliDecoderCreateInstance (NULL , NULL , NULL );
216
217
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: " ));
217
229
return FAILURE ;
218
230
}
219
231
220
232
return SUCCESS ;
221
233
}
222
234
235
+ #define php_brotli_context_create_decoder (ctx ) \
236
+ php_brotli_context_create_decoder_ex(ctx, 0)
237
+
223
238
static void php_brotli_context_close (php_brotli_context * ctx )
224
239
{
225
240
if (ctx -> encoder ) {
@@ -1406,33 +1421,34 @@ static ZEND_FUNCTION(brotli_uncompress)
1406
1421
in_size = max_size ;
1407
1422
}
1408
1423
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 );
1412
1428
RETURN_FALSE ;
1413
1429
}
1414
1430
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 ;
1419
1433
size_t buffer_size = PHP_BROTLI_BUFFER_SIZE ;
1420
1434
uint8_t * buffer = (uint8_t * )emalloc (buffer_size );
1421
-
1422
1435
BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT ;
1423
1436
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 ,
1428
1444
0 );
1429
- size_t used_out = buffer_size - available_out ;
1445
+ size_t used_out = ( size_t )( buffer_size - ctx . available_out ) ;
1430
1446
if (used_out != 0 ) {
1431
1447
smart_string_appendl (& out , buffer , used_out );
1432
1448
}
1433
1449
}
1434
1450
1435
- BrotliDecoderDestroyInstance ( state );
1451
+ php_brotli_context_close ( & ctx );
1436
1452
efree (buffer );
1437
1453
1438
1454
if (result != BROTLI_DECODER_RESULT_SUCCESS ) {
@@ -1453,9 +1469,8 @@ static ZEND_FUNCTION(brotli_uncompress_init)
1453
1469
1454
1470
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS (php_brotli_uncompress_context_ce );
1455
1471
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 );
1459
1474
RETURN_FALSE ;
1460
1475
}
1461
1476
}
0 commit comments