Skip to content

Commit 151c592

Browse files
committed
feat: dictionary support in streams
1 parent 897e94d commit 151c592

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

brotli.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ php_stream_brotli_opener(
883883
{
884884
php_brotli_stream_data *self;
885885
int level = BROTLI_DEFAULT_QUALITY;
886+
zend_string *dict = NULL;
886887
int compress;
887888

888889
if (strncasecmp(STREAM_NAME, path, sizeof(STREAM_NAME)-1) == 0) {
@@ -912,7 +913,13 @@ php_stream_brotli_opener(
912913
context, "brotli", "level"))) {
913914
level = zval_get_long(tmpzval);
914915
}
916+
917+
if (NULL != (tmpzval = php_stream_context_get_option(
918+
context, "brotli", "dict"))) {
919+
dict = zval_get_string(tmpzval);
920+
}
915921
}
922+
916923
if (level > BROTLI_MAX_QUALITY) {
917924
php_error_docref(NULL, E_WARNING,
918925
"brotli: set compression level (%d)"
@@ -934,32 +941,52 @@ php_stream_brotli_opener(
934941
options | REPORT_ERRORS, NULL);
935942
if (!self->stream) {
936943
efree(self);
944+
if (dict) {
945+
zend_string_release(dict);
946+
}
937947
return NULL;
938948
}
939949

940950
php_brotli_context_init(&self->ctx);
941951

942952
/* File */
943953
if (compress) {
944-
if (php_brotli_context_create_encoder(&self->ctx,
945-
level, 0, 0) != SUCCESS) {
954+
if (php_brotli_context_create_encoder_ex(&self->ctx, level, 0, 0,
955+
dict, 0) != SUCCESS) {
946956
php_error_docref(NULL, E_WARNING,
947957
"brotli: failed to prepare compression");
958+
php_brotli_context_close(&self->ctx);
948959
php_stream_close(self->stream);
949960
efree(self);
961+
if (dict) {
962+
zend_string_release(dict);
963+
}
950964
return NULL;
951965
}
952966

967+
if (dict) {
968+
zend_string_release(dict);
969+
}
970+
953971
return php_stream_alloc(&php_stream_brotli_write_ops, self, NULL, mode);
954972
} else {
955-
if (php_brotli_context_create_decoder(&self->ctx) != SUCCESS) {
973+
if (php_brotli_context_create_decoder_ex(&self->ctx,
974+
dict, 0) != SUCCESS) {
956975
php_error_docref(NULL, E_WARNING,
957976
"brotli: failed to prepare decompression");
977+
php_brotli_context_close(&self->ctx);
958978
php_stream_close(self->stream);
959979
efree(self);
980+
if (dict) {
981+
zend_string_release(dict);
982+
}
960983
return NULL;
961984
}
962985

986+
if (dict) {
987+
zend_string_release(dict);
988+
}
989+
963990
self->result = BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
964991

965992
return php_stream_alloc(&php_stream_brotli_read_ops, self, NULL, mode);

0 commit comments

Comments
 (0)