Skip to content

refactor: php_brotli_context struct #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 58 additions & 44 deletions brotli.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,50 @@ static int php_brotli_decoder_create(BrotliDecoderState **decoder)
return SUCCESS;
}

struct _php_brotli_context {
BrotliEncoderState *encoder;
BrotliDecoderState *decoder;
size_t available_in;
const uint8_t *next_in;
size_t available_out;
uint8_t *next_out;
uint8_t *output;
zend_object std;
};

static void php_brotli_context_init(php_brotli_context *ctx)
{
ctx->encoder = NULL;
ctx->decoder = NULL;
ctx->available_in = 0;
ctx->next_in = NULL;
ctx->available_out = 0;
ctx->next_out = NULL;
ctx->output = NULL;
}

static void php_brotli_context_close(php_brotli_context *ctx)
{
if (ctx->encoder) {
BrotliEncoderDestroyInstance(ctx->encoder);
ctx->encoder = NULL;
}
if (ctx->decoder) {
BrotliDecoderDestroyInstance(ctx->decoder);
ctx->decoder = NULL;
}

if (ctx->output) {
efree(ctx->output);
ctx->output = NULL;
}

ctx->available_in = 0;
ctx->next_in = NULL;
ctx->available_out = 0;
ctx->next_out = NULL;
}

#define PHP_BROTLI_OUTPUT_HANDLER "ob_brotli_handler"

static int php_brotli_output_encoding(void)
Expand All @@ -157,23 +201,6 @@ static int php_brotli_output_encoding(void)
return BROTLI_G(compression_coding);
}

static void php_brotli_context_close(php_brotli_context *ctx)
{
if (ctx->state.encoder) {
BrotliEncoderDestroyInstance(ctx->state.encoder);
ctx->state.encoder = NULL;
}
if (ctx->output) {
efree(ctx->output);
ctx->output = NULL;
}

ctx->available_in = 0;
ctx->next_in = NULL;
ctx->available_out = 0;
ctx->next_out = NULL;
}

static int php_brotli_output_handler(void **handler_context,
php_output_context *output_context)
{
Expand Down Expand Up @@ -201,7 +228,7 @@ static int php_brotli_output_handler(void **handler_context,
}

if (output_context->op & PHP_OUTPUT_HANDLER_START) {
if (php_brotli_encoder_create(&ctx->state.encoder,
if (php_brotli_encoder_create(&ctx->encoder,
quality, 0, 0) != SUCCESS) {
return FAILURE;
}
Expand Down Expand Up @@ -235,7 +262,7 @@ static int php_brotli_output_handler(void **handler_context,
ctx->next_in = NULL;
}

if (!BrotliEncoderCompressStream(ctx->state.encoder,
if (!BrotliEncoderCompressStream(ctx->encoder,
(output_context->op
& PHP_OUTPUT_HANDLER_FINAL)
? BROTLI_OPERATION_FINISH
Expand Down Expand Up @@ -274,7 +301,7 @@ static int php_brotli_output_handler(void **handler_context,
return SUCCESS;
} else {
// restart
if (php_brotli_encoder_create(&ctx->state.encoder,
if (php_brotli_encoder_create(&ctx->encoder,
quality, 0, 0) != SUCCESS) {
return FAILURE;
}
Expand All @@ -288,13 +315,7 @@ static php_brotli_context *php_brotli_output_handler_context_init(void)
{
php_brotli_context *ctx
= (php_brotli_context *)ecalloc(1, sizeof(php_brotli_context));
ctx->state.encoder = NULL;
ctx->state.decoder = NULL;
ctx->available_in = 0;
ctx->next_in = NULL;
ctx->available_out = 0;
ctx->next_out = NULL;
ctx->output = NULL;
php_brotli_context_init(ctx);
return ctx;
}

Expand Down Expand Up @@ -834,13 +855,7 @@ php_stream_wrapper php_stream_brotli_wrapper = {
php_brotli_context *ctx; \
object_init_ex(return_value, ce); \
ctx = php_brotli_context_from_obj(Z_OBJ_P(return_value)); \
ctx->state.encoder = NULL; \
ctx->state.decoder = NULL; \
ctx->available_in = 0; \
ctx->next_in = NULL; \
ctx->available_out = 0; \
ctx->next_out = NULL; \
ctx->output = NULL;
php_brotli_context_init(ctx)

static php_brotli_context *php_brotli_context_from_obj(zend_object *obj)
{
Expand Down Expand Up @@ -1192,7 +1207,7 @@ static ZEND_FUNCTION(brotli_compress_init)

PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS(php_brotli_compress_context_ce);

if (php_brotli_encoder_create(&ctx->state.encoder,
if (php_brotli_encoder_create(&ctx->encoder,
quality, 0, mode) != SUCCESS) {
zval_ptr_dtor(return_value);
php_error_docref(NULL, E_WARNING,
Expand Down Expand Up @@ -1232,7 +1247,7 @@ static ZEND_FUNCTION(brotli_compress_add)
#else
ctx = php_brotli_context_from_obj(Z_OBJ_P(obj));
#endif
if (ctx == NULL || ctx->state.encoder == NULL) {
if (ctx == NULL || ctx->encoder == NULL) {
php_error_docref(NULL, E_WARNING,
"Brotli incremental compress resource failed");
RETURN_FALSE;
Expand All @@ -1245,11 +1260,10 @@ static ZEND_FUNCTION(brotli_compress_add)
ctx->next_in = in_buf;
ctx->available_in = in_size;

while (ctx->available_in
|| BrotliEncoderHasMoreOutput(ctx->state.encoder)) {
while (ctx->available_in || BrotliEncoderHasMoreOutput(ctx->encoder)) {
ctx->available_out = buffer_size;
ctx->next_out = buffer;
if (BrotliEncoderCompressStream(ctx->state.encoder,
if (BrotliEncoderCompressStream(ctx->encoder,
mode,
&ctx->available_in,
&ctx->next_in,
Expand All @@ -1270,10 +1284,10 @@ static ZEND_FUNCTION(brotli_compress_add)
}

if (mode == BROTLI_OPERATION_FINISH) {
while (!BrotliEncoderIsFinished(ctx->state.encoder)) {
while (!BrotliEncoderIsFinished(ctx->encoder)) {
ctx->available_out = buffer_size;
ctx->next_out = buffer;
if (BrotliEncoderCompressStream(ctx->state.encoder,
if (BrotliEncoderCompressStream(ctx->encoder,
BROTLI_OPERATION_FINISH,
&ctx->available_in,
&ctx->next_in,
Expand Down Expand Up @@ -1364,7 +1378,7 @@ static ZEND_FUNCTION(brotli_uncompress_init)

PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS(php_brotli_uncompress_context_ce);

if (php_brotli_decoder_create(&ctx->state.decoder) != SUCCESS) {
if (php_brotli_decoder_create(&ctx->decoder) != SUCCESS) {
php_error_docref(NULL, E_WARNING,
"Brotli incremental uncompress init failed");
RETURN_FALSE;
Expand Down Expand Up @@ -1402,7 +1416,7 @@ static ZEND_FUNCTION(brotli_uncompress_add)
#else
ctx = php_brotli_context_from_obj(Z_OBJ_P(obj));
#endif
if (ctx == NULL || ctx->state.decoder == NULL) {
if (ctx == NULL || ctx->decoder == NULL) {
php_error_docref(NULL, E_WARNING,
"Brotli incremental uncompress resource failed");
RETURN_FALSE;
Expand All @@ -1422,7 +1436,7 @@ static ZEND_FUNCTION(brotli_uncompress_add)
while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
ctx->available_out = buffer_size;
ctx->next_out = buffer;
result = BrotliDecoderDecompressStream(ctx->state.decoder,
result = BrotliDecoderDecompressStream(ctx->decoder,
&ctx->available_in,
&ctx->next_in,
&ctx->available_out,
Expand Down
15 changes: 1 addition & 14 deletions php_brotli.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,7 @@ extern zend_module_entry brotli_module_entry;
# include "TSRM.h"
#endif

typedef struct _php_brotli_state_context {
BrotliEncoderState *encoder;
BrotliDecoderState *decoder;
} php_brotli_state_context;

typedef struct _php_brotli_context {
php_brotli_state_context state;
size_t available_in;
const uint8_t *next_in;
size_t available_out;
uint8_t *next_out;
uint8_t *output;
zend_object std;
} php_brotli_context;
typedef struct _php_brotli_context php_brotli_context;

ZEND_BEGIN_MODULE_GLOBALS(brotli)
zend_long output_compression;
Expand Down