mirror of
https://github.com/intel/isa-l.git
synced 2025-01-31 10:31:12 +01:00
igzip: Modify test to test zlib compression
Change-Id: I52979c9e572ef9703995adf8d2163ba1797b8f53 Signed-off-by: Roy Oursler <roy.j.oursler@intel.com>
This commit is contained in:
parent
4259169107
commit
0cc3d93758
@ -78,4 +78,20 @@ uint32_t find_crc(uint8_t * start, uint32_t length)
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
#define ADLER_MOD 65521
|
||||
uint32_t find_adler(uint8_t * start, uint32_t length)
|
||||
{
|
||||
uint32_t A = 1;
|
||||
uint32_t B = 0;
|
||||
uint8_t *end = start + length;
|
||||
|
||||
while (start < end) {
|
||||
A = (A + *start) % ADLER_MOD;
|
||||
B = (B + A) % ADLER_MOD;
|
||||
start++;
|
||||
}
|
||||
|
||||
return (B << 16) | A;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
106
igzip/igzip.c
106
igzip/igzip.c
@ -1210,61 +1210,71 @@ void write_header(struct isal_zstream *stream, uint8_t * deflate_hdr,
|
||||
void write_trailer(struct isal_zstream *stream)
|
||||
{
|
||||
struct isal_zstate *state = &stream->internal_state;
|
||||
unsigned int bytes;
|
||||
unsigned int bytes = 0;
|
||||
uint32_t crc = state->crc;
|
||||
|
||||
if (stream->avail_out >= 8) {
|
||||
set_buf(&state->bitbuf, stream->next_out, stream->avail_out);
|
||||
set_buf(&state->bitbuf, stream->next_out, stream->avail_out);
|
||||
|
||||
if (!state->has_eob_hdr) {
|
||||
/* If the final header has not been written, write a
|
||||
* final block. This block is a static huffman block
|
||||
* which only contains the end of block symbol. The code
|
||||
* that happens to do this is the fist 10 bits of
|
||||
* 0x003 */
|
||||
if (stream->avail_out < 8)
|
||||
return;
|
||||
|
||||
state->has_eob_hdr = 1;
|
||||
write_bits(&state->bitbuf, 0x003, 10);
|
||||
if (is_full(&state->bitbuf)) {
|
||||
stream->next_out = buffer_ptr(&state->bitbuf);
|
||||
bytes = buffer_used(&state->bitbuf);
|
||||
stream->avail_out -= bytes;
|
||||
stream->total_out += bytes;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (state->bitbuf.m_bit_count) {
|
||||
/* the flush() will pad to the next byte and write up to 8 bytes
|
||||
* to the output stream/buffer.
|
||||
*/
|
||||
if (!state->has_eob_hdr) {
|
||||
/* If the final header has not been written, write a
|
||||
* final block. This block is a static huffman block
|
||||
* which only contains the end of block symbol. The code
|
||||
* that happens to do this is the fist 10 bits of
|
||||
* 0x003 */
|
||||
state->has_eob_hdr = 1;
|
||||
write_bits(&state->bitbuf, 0x003, 10);
|
||||
if (is_full(&state->bitbuf)) {
|
||||
stream->next_out = buffer_ptr(&state->bitbuf);
|
||||
bytes = buffer_used(&state->bitbuf);
|
||||
stream->avail_out -= bytes;
|
||||
stream->total_out += bytes;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (stream->avail_out < 8)
|
||||
return;
|
||||
|
||||
flush(&state->bitbuf);
|
||||
stream->next_out = buffer_ptr(&state->bitbuf);
|
||||
bytes = buffer_used(&state->bitbuf);
|
||||
|
||||
if (stream->gzip_flag) {
|
||||
if (!is_full(&state->bitbuf)) {
|
||||
switch (stream->gzip_flag) {
|
||||
case IGZIP_GZIP:
|
||||
case IGZIP_GZIP_NO_HDR:
|
||||
*(uint64_t *) stream->next_out =
|
||||
((uint64_t) stream->total_in << 32) | crc;
|
||||
stream->next_out += gzip_trl_bytes;
|
||||
bytes += gzip_trl_bytes;
|
||||
break;
|
||||
case IGZIP_ZLIB:
|
||||
case IGZIP_ZLIB_NO_HDR:
|
||||
*(uint32_t *) stream->next_out =
|
||||
to_be32((crc & 0xFFFF0000)
|
||||
| ((crc & 0xFFFF) + 1) % ADLER_MOD);
|
||||
stream->next_out += zlib_trl_bytes;
|
||||
bytes += zlib_trl_bytes;
|
||||
break;
|
||||
}
|
||||
state->state = ZSTATE_END;
|
||||
}
|
||||
} else
|
||||
state->state = ZSTATE_END;
|
||||
|
||||
stream->avail_out -= bytes;
|
||||
stream->total_out += bytes;
|
||||
}
|
||||
|
||||
stream->next_out = buffer_ptr(&state->bitbuf);
|
||||
bytes = buffer_used(&state->bitbuf);
|
||||
|
||||
switch (stream->gzip_flag) {
|
||||
case IGZIP_GZIP:
|
||||
case IGZIP_GZIP_NO_HDR:
|
||||
if (stream->avail_out - bytes >= gzip_trl_bytes) {
|
||||
*(uint64_t *) stream->next_out =
|
||||
((uint64_t) stream->total_in << 32) | crc;
|
||||
stream->next_out += gzip_trl_bytes;
|
||||
bytes += gzip_trl_bytes;
|
||||
state->state = ZSTATE_END;
|
||||
}
|
||||
break;
|
||||
|
||||
case IGZIP_ZLIB:
|
||||
case IGZIP_ZLIB_NO_HDR:
|
||||
if (stream->avail_out - bytes >= zlib_trl_bytes) {
|
||||
*(uint32_t *) stream->next_out =
|
||||
to_be32((crc & 0xFFFF0000) | ((crc & 0xFFFF) + 1) % ADLER_MOD);
|
||||
stream->next_out += zlib_trl_bytes;
|
||||
bytes += zlib_trl_bytes;
|
||||
state->state = ZSTATE_END;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
state->state = ZSTATE_END;
|
||||
}
|
||||
|
||||
stream->avail_out -= bytes;
|
||||
stream->total_out += bytes;
|
||||
}
|
||||
|
@ -82,6 +82,9 @@ enum IGZIP_TEST_ERROR_CODES {
|
||||
INFLATE_INVALID_LOOK_BACK_DISTANCE,
|
||||
INVALID_GZIP_HEADER,
|
||||
INCORRECT_GZIP_TRAILER,
|
||||
INVALID_ZLIB_HEADER,
|
||||
INCORRECT_ZLIB_TRAILER,
|
||||
|
||||
INFLATE_GENERAL_ERROR,
|
||||
|
||||
INVALID_FLUSH_ERROR,
|
||||
@ -97,10 +100,16 @@ static const uint8_t gzip_hdr[10] = {
|
||||
0x00, 0xff
|
||||
};
|
||||
|
||||
static const uint32_t gzip_hdr_bytes = 10;
|
||||
/* static const uint32_t gzip_trl_bytes = 8; */
|
||||
static const uint8_t zlib_hdr[2] = {
|
||||
0x78, 0x01
|
||||
};
|
||||
|
||||
static const uint32_t gzip_hdr_bytes = 10;
|
||||
static const uint32_t zlib_hdr_bytes = 2;
|
||||
static const uint32_t gzip_trl_bytes = 8;
|
||||
static const uint32_t zlib_trl_bytes = 4;
|
||||
static const int gzip_extra_bytes = 18; /* gzip_hdr_bytes + gzip_trl_bytes */
|
||||
static const int zlib_extra_bytes = 6; /* zlib_hdr_bytes + zlib_trl_bytes */
|
||||
|
||||
int inflate_type = 0;
|
||||
|
||||
@ -230,6 +239,12 @@ void print_error(int error_code)
|
||||
case INCORRECT_GZIP_TRAILER:
|
||||
printf("error: incorrect gzip trailer found when inflating data\n");
|
||||
break;
|
||||
case INVALID_ZLIB_HEADER:
|
||||
printf("error: incorrect zlib header found when inflating data\n");
|
||||
break;
|
||||
case INCORRECT_ZLIB_TRAILER:
|
||||
printf("error: incorrect zlib trailer found when inflating data\n");
|
||||
break;
|
||||
case INVALID_FLUSH_ERROR:
|
||||
printf("error: invalid flush did not cause compression to error\n");
|
||||
break;
|
||||
@ -293,6 +308,18 @@ uint32_t check_gzip_header(uint8_t * z_buf)
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t check_zlib_header(uint8_t * z_buf)
|
||||
{
|
||||
/* These values are defined in RFC 1952 page 4 */
|
||||
uint32_t ret = 0;
|
||||
int i;
|
||||
/* Verify that the gzip header is the one used in hufftables_c.c */
|
||||
for (i = 0; i < zlib_hdr_bytes; i++)
|
||||
if (z_buf[i] != zlib_hdr[i])
|
||||
ret = INVALID_ZLIB_HEADER;
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t check_gzip_trl(uint64_t gzip_trl, uint32_t inflate_crc, uint8_t * uncompress_buf,
|
||||
uint32_t uncompress_len)
|
||||
{
|
||||
@ -308,6 +335,22 @@ uint32_t check_gzip_trl(uint64_t gzip_trl, uint32_t inflate_crc, uint8_t * uncom
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t check_zlib_trl(uint32_t zlib_trl, uint32_t inflate_crc, uint8_t * uncompress_buf,
|
||||
uint32_t uncompress_len)
|
||||
{
|
||||
uint32_t trl, ret = 0;
|
||||
uint32_t crc;
|
||||
|
||||
crc = find_adler(uncompress_buf, uncompress_len);
|
||||
|
||||
trl = (crc >> 24) | ((crc >> 8) & 0xFF00) | (crc << 24) | ((crc & 0xFF00) << 8);
|
||||
|
||||
if (trl != zlib_trl)
|
||||
ret = INCORRECT_ZLIB_TRAILER;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int inflate_stateless_pass(uint8_t * compress_buf, uint64_t compress_len,
|
||||
uint8_t * uncompress_buf, uint32_t * uncompress_len,
|
||||
uint32_t gzip_flag)
|
||||
@ -326,11 +369,21 @@ int inflate_stateless_pass(uint8_t * compress_buf, uint64_t compress_len,
|
||||
*uncompress_len = state.total_out;
|
||||
|
||||
if (gzip_flag) {
|
||||
if (!ret)
|
||||
ret =
|
||||
check_gzip_trl(*(uint64_t *) state.next_in, state.crc,
|
||||
uncompress_buf, *uncompress_len);
|
||||
state.avail_in -= 8;
|
||||
if (gzip_flag == IGZIP_GZIP || gzip_flag == IGZIP_GZIP_NO_HDR) {
|
||||
if (!ret)
|
||||
ret =
|
||||
check_gzip_trl(*(uint64_t *) state.next_in, state.crc,
|
||||
uncompress_buf, *uncompress_len);
|
||||
state.avail_in -= gzip_trl_bytes;
|
||||
} else if (gzip_flag == IGZIP_ZLIB || gzip_flag == IGZIP_ZLIB_NO_HDR) {
|
||||
if (!ret)
|
||||
ret =
|
||||
check_zlib_trl(*(uint32_t *) state.next_in, state.crc,
|
||||
uncompress_buf, *uncompress_len);
|
||||
state.avail_in -= zlib_trl_bytes;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (ret == 0 && state.avail_in != 0)
|
||||
@ -363,8 +416,10 @@ int inflate_multi_pass(uint8_t * compress_buf, uint64_t compress_len,
|
||||
state->avail_out = 0;
|
||||
state->crc_flag = gzip_flag;
|
||||
|
||||
if (gzip_flag)
|
||||
compress_len -= 8;
|
||||
if (gzip_flag == IGZIP_GZIP || gzip_flag == IGZIP_GZIP_NO_HDR)
|
||||
compress_len -= gzip_trl_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB || gzip_flag == IGZIP_ZLIB_NO_HDR)
|
||||
compress_len -= zlib_trl_bytes;
|
||||
|
||||
while (1) {
|
||||
if (state->avail_in == 0) {
|
||||
@ -453,10 +508,19 @@ int inflate_multi_pass(uint8_t * compress_buf, uint64_t compress_len,
|
||||
}
|
||||
|
||||
if (gzip_flag) {
|
||||
if (!ret)
|
||||
ret =
|
||||
check_gzip_trl(*(uint64_t *) & compress_buf[compress_len],
|
||||
state->crc, uncompress_buf, *uncompress_len);
|
||||
if (!ret) {
|
||||
if (gzip_flag == IGZIP_GZIP || gzip_flag == IGZIP_GZIP_NO_HDR) {
|
||||
ret =
|
||||
check_gzip_trl(*(uint64_t *) & compress_buf[compress_len],
|
||||
state->crc, uncompress_buf,
|
||||
*uncompress_len);
|
||||
} else if (gzip_flag == IGZIP_ZLIB || gzip_flag == IGZIP_ZLIB_NO_HDR) {
|
||||
ret =
|
||||
check_zlib_trl(*(uint32_t *) & compress_buf[compress_len],
|
||||
state->crc, uncompress_buf,
|
||||
*uncompress_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == 0 && state->avail_in != 0)
|
||||
ret = INFLATE_LEFTOVER_INPUT;
|
||||
@ -501,6 +565,10 @@ int inflate_check(uint8_t * z_buf, int z_size, uint8_t * in_buf, int in_size,
|
||||
gzip_hdr_result = check_gzip_header(z_buf);
|
||||
z_buf += gzip_hdr_bytes;
|
||||
z_size -= gzip_hdr_bytes;
|
||||
} else if (gzip_flag == IGZIP_ZLIB) {
|
||||
gzip_hdr_result = check_zlib_header(z_buf);
|
||||
z_buf += zlib_hdr_bytes;
|
||||
z_size -= zlib_hdr_bytes;
|
||||
}
|
||||
|
||||
if (inflate_type == 0) {
|
||||
@ -553,6 +621,9 @@ int inflate_check(uint8_t * z_buf, int z_size, uint8_t * in_buf, int in_size,
|
||||
case INCORRECT_GZIP_TRAILER:
|
||||
gzip_trl_result = INCORRECT_GZIP_TRAILER;
|
||||
break;
|
||||
case INCORRECT_ZLIB_TRAILER:
|
||||
gzip_trl_result = INCORRECT_ZLIB_TRAILER;
|
||||
break;
|
||||
|
||||
default:
|
||||
return INFLATE_GENERAL_ERROR;
|
||||
@ -565,13 +636,17 @@ int inflate_check(uint8_t * z_buf, int z_size, uint8_t * in_buf, int in_size,
|
||||
if (mem_result)
|
||||
return RESULT_ERROR;
|
||||
|
||||
if (gzip_flag) {
|
||||
if (gzip_hdr_result)
|
||||
return INVALID_GZIP_HEADER;
|
||||
if (gzip_hdr_result == INVALID_GZIP_HEADER)
|
||||
return INVALID_GZIP_HEADER;
|
||||
|
||||
if (gzip_trl_result)
|
||||
return INCORRECT_GZIP_TRAILER;
|
||||
}
|
||||
else if (gzip_hdr_result == INVALID_ZLIB_HEADER)
|
||||
return INVALID_ZLIB_HEADER;
|
||||
|
||||
if (gzip_trl_result == INCORRECT_GZIP_TRAILER)
|
||||
return INCORRECT_GZIP_TRAILER;
|
||||
|
||||
else if (gzip_trl_result == INCORRECT_ZLIB_TRAILER)
|
||||
return INCORRECT_ZLIB_TRAILER;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1253,7 +1328,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() % 3;
|
||||
gzip_flag = rand() % 5;
|
||||
level = rand() % 2;
|
||||
|
||||
if (in_size != 0) {
|
||||
@ -1267,8 +1342,14 @@ int test_compress_stateless(uint8_t * in_data, uint32_t in_size, uint32_t flush_
|
||||
|
||||
/* Test non-overflow case where a type 0 block is not written */
|
||||
z_size = 2 * in_size + hdr_bytes;
|
||||
if (gzip_flag)
|
||||
if (gzip_flag == IGZIP_GZIP)
|
||||
z_size += gzip_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_GZIP_NO_HDR)
|
||||
z_size += gzip_trl_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB)
|
||||
z_size += zlib_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
|
||||
z_size += zlib_trl_bytes;
|
||||
|
||||
z_buf = malloc(z_size);
|
||||
|
||||
@ -1324,10 +1405,17 @@ int test_compress_stateless(uint8_t * in_data, uint32_t in_size, uint32_t flush_
|
||||
|
||||
/*Test non-overflow case where a type 0 block is possible to be written */
|
||||
z_size = TYPE0_HDR_SIZE * ((in_size + TYPE0_MAX_SIZE - 1) / TYPE0_MAX_SIZE) + in_size;
|
||||
if (gzip_flag)
|
||||
z_size += gzip_extra_bytes;
|
||||
|
||||
if (z_size == gzip_extra_bytes)
|
||||
if (gzip_flag == IGZIP_GZIP)
|
||||
z_size += gzip_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_GZIP_NO_HDR)
|
||||
z_size += gzip_trl_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB)
|
||||
z_size += zlib_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
|
||||
z_size += zlib_trl_bytes;
|
||||
|
||||
if (z_size <= gzip_extra_bytes)
|
||||
z_size += TYPE0_HDR_SIZE;
|
||||
|
||||
if (z_size < 8)
|
||||
@ -1453,7 +1541,7 @@ int test_compress(uint8_t * in_buf, uint32_t in_size, uint32_t flush_type)
|
||||
{
|
||||
int ret = IGZIP_COMP_OK, fin_ret = IGZIP_COMP_OK;
|
||||
uint32_t overflow = 0, gzip_flag, level;
|
||||
uint32_t z_size, z_size_max, z_compressed_size;
|
||||
uint32_t z_size = 0, z_size_max = 0, z_compressed_size;
|
||||
uint8_t *z_buf = NULL;
|
||||
|
||||
/* Test a non overflow case */
|
||||
@ -1466,13 +1554,20 @@ int test_compress(uint8_t * in_buf, uint32_t in_size, uint32_t flush_type)
|
||||
return COMPRESS_GENERAL_ERROR;
|
||||
}
|
||||
|
||||
gzip_flag = rand() % 3;
|
||||
gzip_flag = rand() % 5;
|
||||
level = rand() % 2;
|
||||
if (gzip_flag)
|
||||
z_size_max += gzip_extra_bytes;
|
||||
|
||||
z_size = z_size_max;
|
||||
|
||||
if (gzip_flag == IGZIP_GZIP)
|
||||
z_size += gzip_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_GZIP_NO_HDR)
|
||||
z_size += gzip_trl_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB)
|
||||
z_size += zlib_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
|
||||
z_size += zlib_trl_bytes;
|
||||
|
||||
z_buf = malloc(z_size);
|
||||
if (z_buf == NULL) {
|
||||
print_error(MALLOC_FAILED);
|
||||
@ -1613,12 +1708,19 @@ int test_flush(uint8_t * in_buf, uint32_t in_size)
|
||||
uint32_t z_size, flush_type = 0, gzip_flag, level;
|
||||
uint8_t *z_buf = NULL;
|
||||
|
||||
gzip_flag = rand() % 3;
|
||||
gzip_flag = rand() % 5;
|
||||
level = rand() % 2;
|
||||
|
||||
z_size = 2 * in_size + 2 * hdr_bytes + 8;
|
||||
if (gzip_flag)
|
||||
if (gzip_flag == IGZIP_GZIP)
|
||||
z_size += gzip_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_GZIP_NO_HDR)
|
||||
z_size += gzip_trl_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB)
|
||||
z_size += zlib_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
|
||||
z_size += zlib_trl_bytes;
|
||||
|
||||
z_buf = malloc(z_size);
|
||||
|
||||
if (z_buf == NULL)
|
||||
@ -1676,12 +1778,18 @@ int test_full_flush(uint8_t * in_buf, uint32_t in_size)
|
||||
uint32_t z_size, gzip_flag, level;
|
||||
uint8_t *z_buf = NULL;
|
||||
|
||||
gzip_flag = rand() % 3;
|
||||
gzip_flag = rand() % 5;
|
||||
level = rand() % 2;
|
||||
z_size = 2 * in_size + MAX_LOOPS * (hdr_bytes + 5);
|
||||
|
||||
if (gzip_flag)
|
||||
if (gzip_flag == IGZIP_GZIP)
|
||||
z_size += gzip_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_GZIP_NO_HDR)
|
||||
z_size += gzip_trl_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB)
|
||||
z_size += zlib_extra_bytes;
|
||||
else if (gzip_flag == IGZIP_ZLIB_NO_HDR)
|
||||
z_size += zlib_trl_bytes;
|
||||
|
||||
z_buf = malloc(z_size);
|
||||
if (z_buf == NULL) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user