Skip to content

Commit 0572eea

Browse files
committed
refactor: php_brotli_encoder_create() -> php_brotli_context_create_encoder()
1 parent 7fb22f3 commit 0572eea

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

brotli.c

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -86,41 +86,6 @@ static zend_function_entry brotli_functions[] = {
8686

8787
static const size_t PHP_BROTLI_BUFFER_SIZE = 1 << 19;
8888

89-
static int php_brotli_encoder_create(BrotliEncoderState **encoder,
90-
long quality, int lgwin, long mode)
91-
{
92-
*encoder = BrotliEncoderCreateInstance(NULL, NULL, NULL);
93-
if (!*encoder) {
94-
return FAILURE;
95-
}
96-
97-
if (quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY) {
98-
php_error_docref(NULL, E_WARNING,
99-
"brotli: compression level (%ld) "
100-
"must be within %d..%d",
101-
(long)quality, BROTLI_MIN_QUALITY, BROTLI_MAX_QUALITY);
102-
quality = BROTLI_DEFAULT_QUALITY;
103-
}
104-
if (lgwin == 0) {
105-
lgwin = BROTLI_DEFAULT_WINDOW;
106-
}
107-
if (mode != BROTLI_MODE_GENERIC &&
108-
mode != BROTLI_MODE_TEXT &&
109-
mode != BROTLI_MODE_FONT) {
110-
php_error_docref(NULL, E_WARNING,
111-
"brotli: compression mode (%ld) must be %d, %d, %d",
112-
(long)mode, BROTLI_MODE_GENERIC, BROTLI_MODE_TEXT,
113-
BROTLI_MODE_FONT);
114-
mode = BROTLI_MODE_GENERIC;
115-
}
116-
117-
BrotliEncoderSetParameter(*encoder, BROTLI_PARAM_QUALITY, quality);
118-
BrotliEncoderSetParameter(*encoder, BROTLI_PARAM_LGWIN, lgwin);
119-
BrotliEncoderSetParameter(*encoder, BROTLI_PARAM_MODE, mode);
120-
121-
return SUCCESS;
122-
}
123-
12489
static int php_brotli_decoder_create(BrotliDecoderState **decoder)
12590
{
12691
*decoder = BrotliDecoderCreateInstance(NULL, NULL, NULL);
@@ -153,6 +118,41 @@ static void php_brotli_context_init(php_brotli_context *ctx)
153118
ctx->output = NULL;
154119
}
155120

121+
static int php_brotli_context_create_encoder(php_brotli_context *ctx,
122+
long quality, int lgwin, long mode)
123+
{
124+
ctx->encoder = BrotliEncoderCreateInstance(NULL, NULL, NULL);
125+
if (!ctx->encoder) {
126+
return FAILURE;
127+
}
128+
129+
if (quality < BROTLI_MIN_QUALITY || quality > BROTLI_MAX_QUALITY) {
130+
php_error_docref(NULL, E_WARNING,
131+
"brotli: compression level (%ld) "
132+
"must be within %d..%d",
133+
(long)quality, BROTLI_MIN_QUALITY, BROTLI_MAX_QUALITY);
134+
quality = BROTLI_DEFAULT_QUALITY;
135+
}
136+
if (lgwin == 0) {
137+
lgwin = BROTLI_DEFAULT_WINDOW;
138+
}
139+
if (mode != BROTLI_MODE_GENERIC &&
140+
mode != BROTLI_MODE_TEXT &&
141+
mode != BROTLI_MODE_FONT) {
142+
php_error_docref(NULL, E_WARNING,
143+
"brotli: compression mode (%ld) must be %d, %d, %d",
144+
(long)mode, BROTLI_MODE_GENERIC, BROTLI_MODE_TEXT,
145+
BROTLI_MODE_FONT);
146+
mode = BROTLI_MODE_GENERIC;
147+
}
148+
149+
BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_QUALITY, quality);
150+
BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_LGWIN, lgwin);
151+
BrotliEncoderSetParameter(ctx->encoder, BROTLI_PARAM_MODE, mode);
152+
153+
return SUCCESS;
154+
}
155+
156156
static void php_brotli_context_close(php_brotli_context *ctx)
157157
{
158158
if (ctx->encoder) {
@@ -228,8 +228,7 @@ static int php_brotli_output_handler(void **handler_context,
228228
}
229229

230230
if (output_context->op & PHP_OUTPUT_HANDLER_START) {
231-
if (php_brotli_encoder_create(&ctx->encoder,
232-
quality, 0, 0) != SUCCESS) {
231+
if (php_brotli_context_create_encoder(ctx, quality, 0, 0) != SUCCESS) {
233232
return FAILURE;
234233
}
235234
}
@@ -301,8 +300,8 @@ static int php_brotli_output_handler(void **handler_context,
301300
return SUCCESS;
302301
} else {
303302
// restart
304-
if (php_brotli_encoder_create(&ctx->encoder,
305-
quality, 0, 0) != SUCCESS) {
303+
if (php_brotli_context_create_encoder(ctx,
304+
quality, 0, 0) != SUCCESS) {
306305
return FAILURE;
307306
}
308307
}
@@ -778,8 +777,8 @@ php_stream_brotli_opener(
778777

779778
/* File */
780779
if (compress) {
781-
if (php_brotli_encoder_create(&self->ctx.encoder,
782-
level, 0, 0) != SUCCESS) {
780+
if (php_brotli_context_create_encoder(&self->ctx,
781+
level, 0, 0) != SUCCESS) {
783782
php_error_docref(NULL, E_WARNING,
784783
"brotli: compression context failed");
785784
php_stream_close(self->stream);
@@ -1181,8 +1180,7 @@ static ZEND_FUNCTION(brotli_compress_init)
11811180

11821181
PHP_BROTLI_CONTEXT_OBJ_INIT_OF_CLASS(php_brotli_compress_context_ce);
11831182

1184-
if (php_brotli_encoder_create(&ctx->encoder,
1185-
quality, 0, mode) != SUCCESS) {
1183+
if (php_brotli_context_create_encoder(ctx, quality, 0, mode) != SUCCESS) {
11861184
zval_ptr_dtor(return_value);
11871185
php_error_docref(NULL, E_WARNING,
11881186
"Brotli incremental compress init failed");

0 commit comments

Comments
 (0)