Skip to content

Commit 8d82e9b

Browse files
committed
refactor: php_brotli_context struct
1 parent c897fd2 commit 8d82e9b

File tree

2 files changed

+59
-58
lines changed

2 files changed

+59
-58
lines changed

brotli.c

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,50 @@ static int php_brotli_decoder_create(BrotliDecoderState **decoder)
131131
return SUCCESS;
132132
}
133133

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+
134178
#define PHP_BROTLI_OUTPUT_HANDLER "ob_brotli_handler"
135179

136180
static int php_brotli_output_encoding(void)
@@ -157,23 +201,6 @@ static int php_brotli_output_encoding(void)
157201
return BROTLI_G(compression_coding);
158202
}
159203

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-
177204
static int php_brotli_output_handler(void **handler_context,
178205
php_output_context *output_context)
179206
{
@@ -201,7 +228,7 @@ static int php_brotli_output_handler(void **handler_context,
201228
}
202229

203230
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,
205232
quality, 0, 0) != SUCCESS) {
206233
return FAILURE;
207234
}
@@ -235,7 +262,7 @@ static int php_brotli_output_handler(void **handler_context,
235262
ctx->next_in = NULL;
236263
}
237264

238-
if (!BrotliEncoderCompressStream(ctx->state.encoder,
265+
if (!BrotliEncoderCompressStream(ctx->encoder,
239266
(output_context->op
240267
& PHP_OUTPUT_HANDLER_FINAL)
241268
? BROTLI_OPERATION_FINISH
@@ -274,7 +301,7 @@ static int php_brotli_output_handler(void **handler_context,
274301
return SUCCESS;
275302
} else {
276303
// restart
277-
if (php_brotli_encoder_create(&ctx->state.encoder,
304+
if (php_brotli_encoder_create(&ctx->encoder,
278305
quality, 0, 0) != SUCCESS) {
279306
return FAILURE;
280307
}
@@ -288,13 +315,7 @@ static php_brotli_context *php_brotli_output_handler_context_init(void)
288315
{
289316
php_brotli_context *ctx
290317
= (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);
298319
return ctx;
299320
}
300321

@@ -834,13 +855,7 @@ php_stream_wrapper php_stream_brotli_wrapper = {
834855
php_brotli_context *ctx; \
835856
object_init_ex(return_value, ce); \
836857
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)
844859

845860
static php_brotli_context *php_brotli_context_from_obj(zend_object *obj)
846861
{
@@ -1192,7 +1207,7 @@ static ZEND_FUNCTION(brotli_compress_init)
11921207

11931208
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS(php_brotli_compress_context_ce);
11941209

1195-
if (php_brotli_encoder_create(&ctx->state.encoder,
1210+
if (php_brotli_encoder_create(&ctx->encoder,
11961211
quality, 0, mode) != SUCCESS) {
11971212
zval_ptr_dtor(return_value);
11981213
php_error_docref(NULL, E_WARNING,
@@ -1232,7 +1247,7 @@ static ZEND_FUNCTION(brotli_compress_add)
12321247
#else
12331248
ctx = php_brotli_context_from_obj(Z_OBJ_P(obj));
12341249
#endif
1235-
if (ctx == NULL || ctx->state.encoder == NULL) {
1250+
if (ctx == NULL || ctx->encoder == NULL) {
12361251
php_error_docref(NULL, E_WARNING,
12371252
"Brotli incremental compress resource failed");
12381253
RETURN_FALSE;
@@ -1245,11 +1260,10 @@ static ZEND_FUNCTION(brotli_compress_add)
12451260
ctx->next_in = in_buf;
12461261
ctx->available_in = in_size;
12471262

1248-
while (ctx->available_in
1249-
|| BrotliEncoderHasMoreOutput(ctx->state.encoder)) {
1263+
while (ctx->available_in || BrotliEncoderHasMoreOutput(ctx->encoder)) {
12501264
ctx->available_out = buffer_size;
12511265
ctx->next_out = buffer;
1252-
if (BrotliEncoderCompressStream(ctx->state.encoder,
1266+
if (BrotliEncoderCompressStream(ctx->encoder,
12531267
mode,
12541268
&ctx->available_in,
12551269
&ctx->next_in,
@@ -1270,10 +1284,10 @@ static ZEND_FUNCTION(brotli_compress_add)
12701284
}
12711285

12721286
if (mode == BROTLI_OPERATION_FINISH) {
1273-
while (!BrotliEncoderIsFinished(ctx->state.encoder)) {
1287+
while (!BrotliEncoderIsFinished(ctx->encoder)) {
12741288
ctx->available_out = buffer_size;
12751289
ctx->next_out = buffer;
1276-
if (BrotliEncoderCompressStream(ctx->state.encoder,
1290+
if (BrotliEncoderCompressStream(ctx->encoder,
12771291
BROTLI_OPERATION_FINISH,
12781292
&ctx->available_in,
12791293
&ctx->next_in,
@@ -1364,7 +1378,7 @@ static ZEND_FUNCTION(brotli_uncompress_init)
13641378

13651379
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS(php_brotli_uncompress_context_ce);
13661380

1367-
if (php_brotli_decoder_create(&ctx->state.decoder) != SUCCESS) {
1381+
if (php_brotli_decoder_create(&ctx->decoder) != SUCCESS) {
13681382
php_error_docref(NULL, E_WARNING,
13691383
"Brotli incremental uncompress init failed");
13701384
RETURN_FALSE;
@@ -1402,7 +1416,7 @@ static ZEND_FUNCTION(brotli_uncompress_add)
14021416
#else
14031417
ctx = php_brotli_context_from_obj(Z_OBJ_P(obj));
14041418
#endif
1405-
if (ctx == NULL || ctx->state.decoder == NULL) {
1419+
if (ctx == NULL || ctx->decoder == NULL) {
14061420
php_error_docref(NULL, E_WARNING,
14071421
"Brotli incremental uncompress resource failed");
14081422
RETURN_FALSE;
@@ -1422,7 +1436,7 @@ static ZEND_FUNCTION(brotli_uncompress_add)
14221436
while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
14231437
ctx->available_out = buffer_size;
14241438
ctx->next_out = buffer;
1425-
result = BrotliDecoderDecompressStream(ctx->state.decoder,
1439+
result = BrotliDecoderDecompressStream(ctx->decoder,
14261440
&ctx->available_in,
14271441
&ctx->next_in,
14281442
&ctx->available_out,

php_brotli.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,7 @@ extern zend_module_entry brotli_module_entry;
2828
# include "TSRM.h"
2929
#endif
3030

31-
typedef struct _php_brotli_state_context {
32-
BrotliEncoderState *encoder;
33-
BrotliDecoderState *decoder;
34-
} php_brotli_state_context;
35-
36-
typedef struct _php_brotli_context {
37-
php_brotli_state_context state;
38-
size_t available_in;
39-
const uint8_t *next_in;
40-
size_t available_out;
41-
uint8_t *next_out;
42-
uint8_t *output;
43-
zend_object std;
44-
} php_brotli_context;
31+
typedef struct _php_brotli_context php_brotli_context;
4532

4633
ZEND_BEGIN_MODULE_GLOBALS(brotli)
4734
zend_long output_compression;

0 commit comments

Comments
 (0)