summaryrefslogtreecommitdiff
path: root/decoders/http/http_content_decompress.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'decoders/http/http_content_decompress.cpp')
-rw-r--r--decoders/http/http_content_decompress.cpp164
1 files changed, 89 insertions, 75 deletions
diff --git a/decoders/http/http_content_decompress.cpp b/decoders/http/http_content_decompress.cpp
index 3b71a40..cb90811 100644
--- a/decoders/http/http_content_decompress.cpp
+++ b/decoders/http/http_content_decompress.cpp
@@ -1,21 +1,13 @@
-/*
-**********************************************************************************************
-* File: http_content_decompress.c
-* Description:
-* Authors: LuWenPeng <[email protected]>
-* Date: 2022-10-31
-* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
-***********************************************************************************************
-*/
#include <zlib.h>
#include <string.h>
#include <assert.h>
#include <brotli/decode.h>
-#include "http_decoder_inc.h"
+#include "http_decoder_private.h"
#define HTTP_DECOMPRESS_BUFFER_SIZE (4096)
-struct http_content_decompress {
+struct http_content_decompress
+{
enum http_content_encoding encoding;
z_stream *z_stream_ptr;
BrotliDecoderState *br_state;
@@ -23,38 +15,41 @@ struct http_content_decompress {
size_t buffer_size;
};
-enum http_content_encoding
-http_content_encoding_str2int(const char *content_encoding)
+enum http_content_encoding http_content_encoding_str2int(const char *content_encoding)
{
- if (strcasestr(content_encoding, "gzip") != NULL) {
+ if (strcasestr(content_encoding, "gzip") != NULL)
+ {
return HTTP_CONTENT_ENCODING_GZIP;
}
- if (strcasestr(content_encoding, "deflate") != NULL) {
+ if (strcasestr(content_encoding, "deflate") != NULL)
+ {
return HTTP_CONTENT_ENCODING_DEFLATE;
}
- if (strcasestr(content_encoding, "br") != NULL) {
+ if (strcasestr(content_encoding, "br") != NULL)
+ {
return HTTP_CONTENT_ENCODING_BR;
}
return HTTP_CONTENT_ENCODING_NONE;
}
-const char *
-http_content_encoding_int2str(enum http_content_encoding content_encoding)
+const char *http_content_encoding_int2str(enum http_content_encoding content_encoding)
{
- if (content_encoding == HTTP_CONTENT_ENCODING_GZIP) {
+ if (content_encoding == HTTP_CONTENT_ENCODING_GZIP)
+ {
return "gzip";
}
- if (content_encoding == HTTP_CONTENT_ENCODING_DEFLATE) {
+ if (content_encoding == HTTP_CONTENT_ENCODING_DEFLATE)
+ {
return "deflate";
}
- if (content_encoding == HTTP_CONTENT_ENCODING_BR) {
+ if (content_encoding == HTTP_CONTENT_ENCODING_BR)
+ {
return "br";
}
return "unknown";
}
-struct http_content_decompress *
-http_content_decompress_create(enum http_content_encoding encoding)
+struct http_content_decompress *http_content_decompress_create(enum http_content_encoding encoding)
{
struct http_content_decompress *decompress =
CALLOC(struct http_content_decompress, 1);
@@ -64,8 +59,8 @@ http_content_decompress_create(enum http_content_encoding encoding)
decompress->z_stream_ptr = NULL;
decompress->br_state = NULL;
- if (encoding == HTTP_CONTENT_ENCODING_GZIP
- || encoding == HTTP_CONTENT_ENCODING_DEFLATE) {
+ if (encoding == HTTP_CONTENT_ENCODING_GZIP || encoding == HTTP_CONTENT_ENCODING_DEFLATE)
+ {
decompress->z_stream_ptr = CALLOC(z_stream, 1);
assert(decompress->z_stream_ptr);
@@ -75,22 +70,27 @@ http_content_decompress_create(enum http_content_encoding encoding)
decompress->z_stream_ptr->avail_in = 0;
decompress->z_stream_ptr->next_in = Z_NULL;
- if (encoding == HTTP_CONTENT_ENCODING_GZIP) {
- if (inflateInit2(decompress->z_stream_ptr, MAX_WBITS + 16)
- != Z_OK) {
+ if (encoding == HTTP_CONTENT_ENCODING_GZIP)
+ {
+ if (inflateInit2(decompress->z_stream_ptr, MAX_WBITS + 16) != Z_OK)
+ {
goto error;
}
}
- if (encoding == HTTP_CONTENT_ENCODING_DEFLATE) {
- if (inflateInit2(decompress->z_stream_ptr, -MAX_WBITS) != Z_OK) {
+ if (encoding == HTTP_CONTENT_ENCODING_DEFLATE)
+ {
+ if (inflateInit2(decompress->z_stream_ptr, -MAX_WBITS) != Z_OK)
+ {
goto error;
}
}
}
- if (encoding == HTTP_CONTENT_ENCODING_BR) {
+ if (encoding == HTTP_CONTENT_ENCODING_BR)
+ {
decompress->br_state = BrotliDecoderCreateInstance(NULL, NULL, NULL);
- if (decompress->br_state == NULL) {
+ if (decompress->br_state == NULL)
+ {
goto error;
}
}
@@ -103,14 +103,17 @@ error:
void http_content_decompress_destroy(struct http_content_decompress *decompress)
{
- if (NULL == decompress) {
+ if (NULL == decompress)
+ {
return;
}
- if (decompress->z_stream_ptr != NULL) {
+ if (decompress->z_stream_ptr != NULL)
+ {
inflateEnd(decompress->z_stream_ptr);
FREE(decompress->z_stream_ptr);
}
- if (decompress->br_state) {
+ if (decompress->br_state)
+ {
BrotliDecoderDestroyInstance(decompress->br_state);
decompress->br_state = NULL;
}
@@ -118,10 +121,9 @@ void http_content_decompress_destroy(struct http_content_decompress *decompress)
FREE(decompress);
}
-static int
-http_content_decompress_write_zlib(struct http_content_decompress *decompress,
- const char *indata, size_t indata_len,
- char **outdata, size_t *outdata_len)
+static int http_content_decompress_write_zlib(struct http_content_decompress *decompress,
+ const char *indata, size_t indata_len,
+ char **outdata, size_t *outdata_len)
{
z_stream *z_stream_ptr = decompress->z_stream_ptr;
z_stream_ptr->avail_in = (unsigned int)indata_len;
@@ -132,35 +134,40 @@ http_content_decompress_write_zlib(struct http_content_decompress *decompress,
*outdata = NULL;
*outdata_len = 0;
- do {
+ do
+ {
int ret = inflate(z_stream_ptr, Z_NO_FLUSH);
- if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT
- || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
+ if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
+ {
(void)inflateEnd(z_stream_ptr);
return -1;
}
size_t have = decompress->buffer_size - z_stream_ptr->avail_out;
- if (have > 0) {
- if (0 == z_stream_ptr->avail_out) {
- fprintf(stderr, "realloc outbuffer,before: %zu bytes, after :%zu B\n", decompress->buffer_size , decompress->buffer_size + have); ;
+ if (have > 0)
+ {
+ if (0 == z_stream_ptr->avail_out)
+ {
decompress->buffer_size += have;
decompress->buffer = REALLOC(char, decompress->buffer,
decompress->buffer_size);
*outdata = decompress->buffer;
*outdata_len = *outdata_len + have;
- // http_decoder_log(DEBUG, "%s realloc outbuffer %zu bytes",
- // http_content_encoding_int2str(decompress->encoding),
+ // http_decoder_log(DEBUG, "%s realloc outbuffer %zu bytes",
+ // http_content_encoding_int2str(decompress->encoding),
// decompress->buffer_size);
z_stream_ptr->avail_out = have;
- z_stream_ptr->next_out = (unsigned char *)decompress->buffer +
+ z_stream_ptr->next_out = (unsigned char *)decompress->buffer +
(decompress->buffer_size - have);
- } else {
+ }
+ else
+ {
*outdata = decompress->buffer;
*outdata_len = have;
}
}
- if(Z_STREAM_END == ret){
+ if (Z_STREAM_END == ret)
+ {
break;
}
} while (z_stream_ptr->avail_in != 0);
@@ -169,10 +176,9 @@ http_content_decompress_write_zlib(struct http_content_decompress *decompress,
return 0;
}
-static int
-http_content_decompress_write_br(struct http_content_decompress *decompress,
- const char *indata, size_t indata_len,
- char **outdata, size_t *outdata_len)
+static int http_content_decompress_write_br(struct http_content_decompress *decompress,
+ const char *indata, size_t indata_len,
+ char **outdata, size_t *outdata_len)
{
size_t available_in = indata_len;
const unsigned char *next_in = (const unsigned char *)indata;
@@ -182,12 +188,15 @@ http_content_decompress_write_br(struct http_content_decompress *decompress,
*outdata = NULL;
*outdata_len = 0;
- for (;;) {
- int ret = BrotliDecoderDecompressStream(decompress->br_state, &available_in,
- &next_in, &available_out, &next_out, 0);
+ for (;;)
+ {
+ int ret = BrotliDecoderDecompressStream(decompress->br_state, &available_in,
+ &next_in, &available_out, &next_out, 0);
size_t have = decompress->buffer_size - available_out;
- if (have > 0) {
- if (0 == available_out) {
+ if (have > 0)
+ {
+ if (0 == available_out)
+ {
decompress->buffer_size += have;
decompress->buffer = REALLOC(char, decompress->buffer,
decompress->buffer_size);
@@ -196,24 +205,28 @@ http_content_decompress_write_br(struct http_content_decompress *decompress,
available_out = have;
next_out = (unsigned char *)decompress->buffer +
(decompress->buffer_size - have);
- } else {
+ }
+ else
+ {
*outdata = decompress->buffer;
*outdata_len = have;
}
}
if (ret == BROTLI_DECODER_RESULT_SUCCESS ||
- ret == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
- decompress->buffer =NULL;
- decompress->buffer_size = 0;
+ ret == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT)
+ {
+ decompress->buffer = NULL;
+ decompress->buffer_size = 0;
return 0;
}
- if (ret == BROTLI_DECODER_RESULT_ERROR) {
- //BrotliDecoderErrorCode errcode =
- BrotliDecoderGetErrorCode(decompress->br_state);
- *outdata = NULL;
- *outdata_len = 0;
+ if (ret == BROTLI_DECODER_RESULT_ERROR)
+ {
+ // BrotliDecoderErrorCode errcode =
+ BrotliDecoderGetErrorCode(decompress->br_state);
+ *outdata = NULL;
+ *outdata_len = 0;
return -1;
}
@@ -234,20 +247,21 @@ int http_content_decompress_write(struct http_content_decompress *decompress,
*outdata = NULL;
*outdata_len = 0;
- if(NULL == decompress->buffer ){
+ if (NULL == decompress->buffer)
+ {
decompress->buffer = CALLOC(char, HTTP_DECOMPRESS_BUFFER_SIZE);
assert(decompress->buffer);
decompress->buffer_size = HTTP_DECOMPRESS_BUFFER_SIZE;
}
if (decompress->encoding == HTTP_CONTENT_ENCODING_GZIP ||
- decompress->encoding == HTTP_CONTENT_ENCODING_DEFLATE) {
- return http_content_decompress_write_zlib(decompress, indata, indata_len,
- outdata, outdata_len);
+ decompress->encoding == HTTP_CONTENT_ENCODING_DEFLATE)
+ {
+ return http_content_decompress_write_zlib(decompress, indata, indata_len, outdata, outdata_len);
}
- if (decompress->encoding == HTTP_CONTENT_ENCODING_BR) {
- return http_content_decompress_write_br(decompress, indata, indata_len,
- outdata, outdata_len);
+ if (decompress->encoding == HTTP_CONTENT_ENCODING_BR)
+ {
+ return http_content_decompress_write_br(decompress, indata, indata_len, outdata, outdata_len);
}
assert(0);
return -1;