@@ -131,6 +131,50 @@ static int php_brotli_decoder_create(BrotliDecoderState **decoder)
131
131
return SUCCESS ;
132
132
}
133
133
134
+ struct _php_brotli_context {
135
+ BrotliEncoderState * encoder ;
136
+ BrotliDecoderState * decoder ;
137
+ size_t available_in ;
138
+ const uint8_t * next_in ;
139
+ size_t available_out ;
140
+ uint8_t * next_out ;
141
+ uint8_t * output ;
142
+ zend_object std ;
143
+ };
144
+
145
+ static void php_brotli_context_init (php_brotli_context * ctx )
146
+ {
147
+ ctx -> encoder = NULL ;
148
+ ctx -> decoder = NULL ;
149
+ ctx -> available_in = 0 ;
150
+ ctx -> next_in = NULL ;
151
+ ctx -> available_out = 0 ;
152
+ ctx -> next_out = NULL ;
153
+ ctx -> output = NULL ;
154
+ }
155
+
156
+ static void php_brotli_context_close (php_brotli_context * ctx )
157
+ {
158
+ if (ctx -> encoder ) {
159
+ BrotliEncoderDestroyInstance (ctx -> encoder );
160
+ ctx -> encoder = NULL ;
161
+ }
162
+ if (ctx -> decoder ) {
163
+ BrotliDecoderDestroyInstance (ctx -> decoder );
164
+ ctx -> decoder = NULL ;
165
+ }
166
+
167
+ if (ctx -> output ) {
168
+ efree (ctx -> output );
169
+ ctx -> output = NULL ;
170
+ }
171
+
172
+ ctx -> available_in = 0 ;
173
+ ctx -> next_in = NULL ;
174
+ ctx -> available_out = 0 ;
175
+ ctx -> next_out = NULL ;
176
+ }
177
+
134
178
#define PHP_BROTLI_OUTPUT_HANDLER "ob_brotli_handler"
135
179
136
180
static int php_brotli_output_encoding (void )
@@ -157,23 +201,6 @@ static int php_brotli_output_encoding(void)
157
201
return BROTLI_G (compression_coding );
158
202
}
159
203
160
- static void php_brotli_context_close (php_brotli_context * ctx )
161
- {
162
- if (ctx -> state .encoder ) {
163
- BrotliEncoderDestroyInstance (ctx -> state .encoder );
164
- ctx -> state .encoder = NULL ;
165
- }
166
- if (ctx -> output ) {
167
- efree (ctx -> output );
168
- ctx -> output = NULL ;
169
- }
170
-
171
- ctx -> available_in = 0 ;
172
- ctx -> next_in = NULL ;
173
- ctx -> available_out = 0 ;
174
- ctx -> next_out = NULL ;
175
- }
176
-
177
204
static int php_brotli_output_handler (void * * handler_context ,
178
205
php_output_context * output_context )
179
206
{
@@ -201,7 +228,7 @@ static int php_brotli_output_handler(void **handler_context,
201
228
}
202
229
203
230
if (output_context -> op & PHP_OUTPUT_HANDLER_START ) {
204
- if (php_brotli_encoder_create (& ctx -> state . encoder ,
231
+ if (php_brotli_encoder_create (& ctx -> encoder ,
205
232
quality , 0 , 0 ) != SUCCESS ) {
206
233
return FAILURE ;
207
234
}
@@ -235,7 +262,7 @@ static int php_brotli_output_handler(void **handler_context,
235
262
ctx -> next_in = NULL ;
236
263
}
237
264
238
- if (!BrotliEncoderCompressStream (ctx -> state . encoder ,
265
+ if (!BrotliEncoderCompressStream (ctx -> encoder ,
239
266
(output_context -> op
240
267
& PHP_OUTPUT_HANDLER_FINAL )
241
268
? BROTLI_OPERATION_FINISH
@@ -274,7 +301,7 @@ static int php_brotli_output_handler(void **handler_context,
274
301
return SUCCESS ;
275
302
} else {
276
303
// restart
277
- if (php_brotli_encoder_create (& ctx -> state . encoder ,
304
+ if (php_brotli_encoder_create (& ctx -> encoder ,
278
305
quality , 0 , 0 ) != SUCCESS ) {
279
306
return FAILURE ;
280
307
}
@@ -288,13 +315,7 @@ static php_brotli_context *php_brotli_output_handler_context_init(void)
288
315
{
289
316
php_brotli_context * ctx
290
317
= (php_brotli_context * )ecalloc (1 , sizeof (php_brotli_context ));
291
- ctx -> state .encoder = NULL ;
292
- ctx -> state .decoder = NULL ;
293
- ctx -> available_in = 0 ;
294
- ctx -> next_in = NULL ;
295
- ctx -> available_out = 0 ;
296
- ctx -> next_out = NULL ;
297
- ctx -> output = NULL ;
318
+ php_brotli_context_init (ctx );
298
319
return ctx ;
299
320
}
300
321
@@ -834,13 +855,7 @@ php_stream_wrapper php_stream_brotli_wrapper = {
834
855
php_brotli_context *ctx; \
835
856
object_init_ex(return_value, ce); \
836
857
ctx = php_brotli_context_from_obj(Z_OBJ_P(return_value)); \
837
- ctx->state.encoder = NULL; \
838
- ctx->state.decoder = NULL; \
839
- ctx->available_in = 0; \
840
- ctx->next_in = NULL; \
841
- ctx->available_out = 0; \
842
- ctx->next_out = NULL; \
843
- ctx->output = NULL;
858
+ php_brotli_context_init(ctx)
844
859
845
860
static php_brotli_context * php_brotli_context_from_obj (zend_object * obj )
846
861
{
@@ -1192,7 +1207,7 @@ static ZEND_FUNCTION(brotli_compress_init)
1192
1207
1193
1208
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS (php_brotli_compress_context_ce );
1194
1209
1195
- if (php_brotli_encoder_create (& ctx -> state . encoder ,
1210
+ if (php_brotli_encoder_create (& ctx -> encoder ,
1196
1211
quality , 0 , mode ) != SUCCESS ) {
1197
1212
zval_ptr_dtor (return_value );
1198
1213
php_error_docref (NULL , E_WARNING ,
@@ -1232,7 +1247,7 @@ static ZEND_FUNCTION(brotli_compress_add)
1232
1247
#else
1233
1248
ctx = php_brotli_context_from_obj (Z_OBJ_P (obj ));
1234
1249
#endif
1235
- if (ctx == NULL || ctx -> state . encoder == NULL ) {
1250
+ if (ctx == NULL || ctx -> encoder == NULL ) {
1236
1251
php_error_docref (NULL , E_WARNING ,
1237
1252
"Brotli incremental compress resource failed" );
1238
1253
RETURN_FALSE ;
@@ -1245,11 +1260,10 @@ static ZEND_FUNCTION(brotli_compress_add)
1245
1260
ctx -> next_in = in_buf ;
1246
1261
ctx -> available_in = in_size ;
1247
1262
1248
- while (ctx -> available_in
1249
- || BrotliEncoderHasMoreOutput (ctx -> state .encoder )) {
1263
+ while (ctx -> available_in || BrotliEncoderHasMoreOutput (ctx -> encoder )) {
1250
1264
ctx -> available_out = buffer_size ;
1251
1265
ctx -> next_out = buffer ;
1252
- if (BrotliEncoderCompressStream (ctx -> state . encoder ,
1266
+ if (BrotliEncoderCompressStream (ctx -> encoder ,
1253
1267
mode ,
1254
1268
& ctx -> available_in ,
1255
1269
& ctx -> next_in ,
@@ -1270,10 +1284,10 @@ static ZEND_FUNCTION(brotli_compress_add)
1270
1284
}
1271
1285
1272
1286
if (mode == BROTLI_OPERATION_FINISH ) {
1273
- while (!BrotliEncoderIsFinished (ctx -> state . encoder )) {
1287
+ while (!BrotliEncoderIsFinished (ctx -> encoder )) {
1274
1288
ctx -> available_out = buffer_size ;
1275
1289
ctx -> next_out = buffer ;
1276
- if (BrotliEncoderCompressStream (ctx -> state . encoder ,
1290
+ if (BrotliEncoderCompressStream (ctx -> encoder ,
1277
1291
BROTLI_OPERATION_FINISH ,
1278
1292
& ctx -> available_in ,
1279
1293
& ctx -> next_in ,
@@ -1364,7 +1378,7 @@ static ZEND_FUNCTION(brotli_uncompress_init)
1364
1378
1365
1379
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS (php_brotli_uncompress_context_ce );
1366
1380
1367
- if (php_brotli_decoder_create (& ctx -> state . decoder ) != SUCCESS ) {
1381
+ if (php_brotli_decoder_create (& ctx -> decoder ) != SUCCESS ) {
1368
1382
php_error_docref (NULL , E_WARNING ,
1369
1383
"Brotli incremental uncompress init failed" );
1370
1384
RETURN_FALSE ;
@@ -1402,7 +1416,7 @@ static ZEND_FUNCTION(brotli_uncompress_add)
1402
1416
#else
1403
1417
ctx = php_brotli_context_from_obj (Z_OBJ_P (obj ));
1404
1418
#endif
1405
- if (ctx == NULL || ctx -> state . decoder == NULL ) {
1419
+ if (ctx == NULL || ctx -> decoder == NULL ) {
1406
1420
php_error_docref (NULL , E_WARNING ,
1407
1421
"Brotli incremental uncompress resource failed" );
1408
1422
RETURN_FALSE ;
@@ -1422,7 +1436,7 @@ static ZEND_FUNCTION(brotli_uncompress_add)
1422
1436
while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT ) {
1423
1437
ctx -> available_out = buffer_size ;
1424
1438
ctx -> next_out = buffer ;
1425
- result = BrotliDecoderDecompressStream (ctx -> state . decoder ,
1439
+ result = BrotliDecoderDecompressStream (ctx -> decoder ,
1426
1440
& ctx -> available_in ,
1427
1441
& ctx -> next_in ,
1428
1442
& ctx -> available_out ,
0 commit comments