igzip: Allow user to write a custom gzip header before compressing with gzip

Modify gzip_flag so that if set to IGZIP_GZIP_NO_HDR, no gzip hdr is written.

Change-Id: I79dbd5287cf471777b10412c17d23dc7f5a57bf6
Signed-off-by: Roy Oursler <roy.j.oursler@intel.com>
This commit is contained in:
Roy Oursler 2016-09-28 11:16:00 -07:00 committed by Greg Tucker
parent d4c6067d28
commit 266ac91a6e
5 changed files with 35 additions and 16 deletions

View File

@ -80,7 +80,6 @@ FIELD _count, 4, 4
FIELD _tmp_out_buff, 16, 1
FIELD _tmp_out_start, 4, 4
FIELD _tmp_out_end, 4, 4
FIELD _has_gzip_hdr, 4, 4
FIELD _has_eob, 4, 4
FIELD _has_eob_hdr, 4, 4
FIELD _left_over, 4, 4
@ -128,7 +127,6 @@ _internal_state_count equ _internal_state+_count
_internal_state_tmp_out_buff equ _internal_state+_tmp_out_buff
_internal_state_tmp_out_start equ _internal_state+_tmp_out_start
_internal_state_tmp_out_end equ _internal_state+_tmp_out_end
_internal_state_has_gzip_hdr equ _internal_state+_has_gzip_hdr
_internal_state_has_eob equ _internal_state+_has_eob
_internal_state_has_eob_hdr equ _internal_state+_has_eob_hdr
_internal_state_left_over equ _internal_state+_left_over

View File

@ -404,7 +404,7 @@ static int isal_deflate_int_stateless(struct isal_zstream *stream)
uint32_t repeated_char_length;
struct isal_zstate *state = &stream->internal_state;
if (stream->gzip_flag)
if (stream->gzip_flag == IGZIP_GZIP)
if (write_gzip_header_stateless(stream))
return STATELESS_OVERFLOW;
@ -473,9 +473,10 @@ static int write_stored_block_stateless(struct isal_zstream *stream,
stream->total_out += stored_len;
avail_in = stream->avail_in;
if (stream->gzip_flag) {
if (stream->gzip_flag == IGZIP_GZIP) {
memcpy(stream->next_out, gzip_hdr, gzip_hdr_bytes);
stream->next_out += gzip_hdr_bytes;
stream->gzip_flag = IGZIP_GZIP_NO_HDR;
}
do {
@ -549,7 +550,6 @@ void isal_deflate_init(struct isal_zstream *stream)
state->has_eob = 0;
state->has_eob_hdr = 0;
state->left_over = 0;
state->has_gzip_hdr = 0;
state->state = ZSTATE_NEW_HDR;
state->count = 0;
@ -596,6 +596,7 @@ int isal_deflate_stateless(struct isal_zstream *stream)
uint8_t *next_out = stream->next_out;
const uint32_t avail_out = stream->avail_out;
const uint32_t total_out = stream->total_out;
const uint32_t gzip_flag = stream->gzip_flag;
uint32_t crc32 = 0;
uint32_t stored_len;
@ -629,9 +630,13 @@ int isal_deflate_stateless(struct isal_zstream *stream)
dyn_min_len = stream->hufftables->deflate_hdr_count + 1;
if (stream->gzip_flag) {
if (stream->gzip_flag == IGZIP_GZIP) {
dyn_min_len += gzip_hdr_bytes + gzip_trl_bytes + 1;
stored_len += gzip_hdr_bytes + gzip_trl_bytes;
} else if (stream->gzip_flag == IGZIP_GZIP_NO_HDR) {
dyn_min_len += gzip_trl_bytes + 1;
stored_len += gzip_trl_bytes;
}
min_len = dyn_min_len;
@ -672,6 +677,8 @@ int isal_deflate_stateless(struct isal_zstream *stream)
stream->avail_out = avail_out;
stream->total_out = total_out;
stream->gzip_flag = gzip_flag;
if (stream->gzip_flag)
crc32 = crc32_gzip(0x0, next_in, avail_in);
@ -786,6 +793,7 @@ static int write_gzip_header_stateless(struct isal_zstream *stream)
memcpy(stream->next_out, gzip_hdr, gzip_hdr_bytes);
stream->next_out += gzip_hdr_bytes;
stream->gzip_flag = IGZIP_GZIP_NO_HDR;
return COMP_OK;
}
@ -805,7 +813,7 @@ static void write_gzip_header(struct isal_zstream *stream)
if (state->count == gzip_hdr_bytes) {
state->count = 0;
state->has_gzip_hdr = 1;
stream->gzip_flag = IGZIP_GZIP_NO_HDR;
}
stream->avail_out -= bytes_to_write;
@ -937,7 +945,7 @@ void write_header(struct isal_zstream *stream)
stream->total_out += count;
}
if (stream->gzip_flag && !state->has_gzip_hdr)
if (stream->gzip_flag == IGZIP_GZIP)
write_gzip_header(stream);
count = hufftables->deflate_hdr_count - state->count;

View File

@ -495,7 +495,7 @@ int inflate_check(uint8_t * z_buf, int z_size, uint8_t * in_buf, int in_size,
if (test_buf != NULL)
memset(test_buf, 0xff, test_size);
if (gzip_flag) {
if (gzip_flag == IGZIP_GZIP) {
gzip_hdr_result = check_gzip_header(z_buf);
z_buf += gzip_hdr_bytes;
z_size -= gzip_hdr_bytes;
@ -880,8 +880,9 @@ int compress_stateless(uint8_t * data, uint32_t data_size, uint8_t * compressed_
return COMPRESS_INPUT_STREAM_INTEGRITY_ERROR;
if (stream.next_out - compressed_buf != stream.total_out ||
stream.total_out + stream.avail_out != *compressed_size)
stream.total_out + stream.avail_out != *compressed_size) {
return COMPRESS_OUTPUT_STREAM_INTEGRITY_ERROR;
}
if (ret != IGZIP_COMP_OK) {
if (ret == STATELESS_OVERFLOW)
@ -1171,7 +1172,7 @@ int test_compress_stateless(uint8_t * in_data, uint32_t in_size, uint32_t flush_
uint8_t *z_buf = NULL;
uint8_t *in_buf = NULL;
gzip_flag = rand() % 2;
gzip_flag = rand() % 3;
if (in_size != 0) {
in_buf = malloc(in_size);
@ -1377,7 +1378,7 @@ int test_compress(uint8_t * in_buf, uint32_t in_size, uint32_t flush_type)
return COMPRESS_GENERAL_ERROR;
}
gzip_flag = rand() % 2;
gzip_flag = rand() % 3;
if (gzip_flag)
z_size_max += gzip_extra_bytes;
@ -1519,7 +1520,7 @@ int test_flush(uint8_t * in_buf, uint32_t in_size)
uint32_t z_size, flush_type = 0, gzip_flag;
uint8_t *z_buf = NULL;
gzip_flag = rand() % 2;
gzip_flag = rand() % 3;
z_size = 2 * in_size + 2 * (hdr_bytes + trl_bytes) + 8;
if (gzip_flag)
z_size += gzip_extra_bytes;
@ -1579,7 +1580,7 @@ int test_full_flush(uint8_t * in_buf, uint32_t in_size)
uint32_t z_size, gzip_flag;
uint8_t *z_buf = NULL;
gzip_flag = rand() % 2;
gzip_flag = rand() % 3;
z_size = 2 * in_size + MAX_LOOPS * (hdr_bytes + trl_bytes + 5);
if (gzip_flag)

View File

@ -36,7 +36,7 @@
#define BUF_SIZE 1024
#define MIN_TEST_LOOPS 10
#ifndef RUN_MEM_SIZE
# define RUN_MEM_SIZE 5000000000
# define RUN_MEM_SIZE 500000000
#endif
struct isal_zstream stream;

View File

@ -135,6 +135,11 @@ enum {IGZIP_LIT_TABLE_SIZE = ISAL_DEF_LIT_SYMBOLS};
#define FULL_FLUSH 2
#define FINISH_FLUSH 0 /* Deprecated */
/* Gzip Flags */
#define IGZIP_DEFLATE 0 /* Default */
#define IGZIP_GZIP 1
#define IGZIP_GZIP_NO_HDR 2
/* Compression Return values */
#define COMP_OK 0
#define INVALID_FLUSH -7
@ -233,7 +238,6 @@ struct isal_zstate {
uint8_t tmp_out_buff[16]; //!< temporary array
uint32_t tmp_out_start; //!< temporary variable
uint32_t tmp_out_end; //!< temporary variable
uint32_t has_gzip_hdr; //!< keeps track of if the gzip header has been written.
uint32_t has_eob; //!< keeps track of eob on the last deflate block
uint32_t has_eob_hdr; //!< keeps track of eob hdr (with BFINAL set)
uint32_t left_over; //!< keeps track of overflow bytes
@ -451,6 +455,10 @@ void isal_deflate_stateless_init(struct isal_zstream *stream);
* look back history does not include previous blocks so new blocks are fully
* independent. Switching between flush types is supported.
*
* If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip
* trailer are written around the deflate compressed data. If gzip_flag is set
* to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written.
*
* @param stream Structure holding state information on the compression streams.
* @return COMP_OK (if everything is ok),
* INVALID_FLUSH (if an invalid FLUSH is selected),
@ -471,6 +479,10 @@ int isal_deflate(struct isal_zstream *stream);
* FULL_FLUSH will byte align the output deflate block so additional blocks can
* be easily appended.
*
* If the gzip_flag is set to IGZIP_GZIP, a generic gzip header and the gzip
* trailer are written around the deflate compressed data. If gzip_flag is set
* to IGZIP_GZIP_NO_HDR, then only the gzip trailer is written.
*
* @param stream Structure holding state information on the compression streams.
* @return COMP_OK (if everything is ok),
* STATELESS_OVERFLOW (if output buffer will not fit output).