-ZEND_RESULT_CODE php_http_encoding_enbrotli(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len)
-{
- BROTLI_BOOL rc;
- int q, win, mode;
-
- *encoded_len = PHP_HTTP_ENBROTLI_BUFFER_SIZE_GUESS(data_len);
- *encoded = emalloc(*encoded_len + 1);
-
- PHP_HTTP_ENBROTLI_LEVEL_SET(flags, q);
- PHP_HTTP_ENBROTLI_WBITS_SET(flags, win);
- PHP_HTTP_ENBROTLI_MODE_SET(flags, mode);
-
- rc = BrotliEncoderCompress(q, win, mode, data_len, (const unsigned char *) data, encoded_len, (unsigned char *) *encoded);
- if (rc) {
- return SUCCESS;
- }
-
- PTR_SET(*encoded, NULL);
- *encoded_len = 0;
-
- php_error_docref(NULL, E_WARNING, "Could not brotli encode data");
- return FAILURE;
-}
-
-ZEND_RESULT_CODE php_http_encoding_debrotli(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len)
-{
- BrotliDecoderState *br;
- BrotliDecoderResult rc;
- php_http_buffer_t buffer;
- unsigned char *ptr;
- size_t len;
- int round = 0;
-
- *decoded = NULL;
- *decoded_len = 0;
-
- br = BrotliDecoderCreateInstance(NULL, NULL, NULL);
- if (!br) {
- return FAILURE;
- }
-
- php_http_buffer_init_ex(&buffer, encoded_len, PHP_HTTP_BUFFER_INIT_PREALLOC);
-
- do {
- if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize_ex(&buffer, buffer.size, 0, 1)) {
- break;
- } else {
- len = buffer.free;
- ptr = (unsigned char *) &buffer.data[buffer.used];
-
- rc = BrotliDecoderDecompressStream(br, &encoded_len, (const unsigned char **) &encoded, &len, &ptr, NULL);
-
- php_http_buffer_account(&buffer, buffer.free - len);
- PHP_HTTP_DEBROTLI_BUFFER_SIZE_ALIGN(buffer.size);
- }
- } while ((BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT == rc) && ++round < PHP_HTTP_DEBROTLI_ROUNDS);
-
- BrotliDecoderDestroyInstance(br);
-
- if (rc == BROTLI_DECODER_RESULT_SUCCESS) {
- php_http_buffer_shrink(&buffer);
- php_http_buffer_fix(&buffer);
-
- *decoded = buffer.data;
- *decoded_len = buffer.used;
-
- return SUCCESS;
- }
-
- php_http_buffer_dtor(&buffer);
- php_error_docref(NULL, E_WARNING, "Could not brotli decode data: %s", BrotliDecoderErrorString(rc));
-
- return FAILURE;
-}
-
-struct enbrotli_ctx {
- BrotliEncoderState *br;
- php_http_buffer_t buffer;
-};
-