Skip to content

Commit 94ec602

Browse files
rhpvordermangbtucker
authored andcommitted
Create headers based on compression parameters.
Instead of using a constant as default zlib header, create the header on the fly. Both zlib header bytes depend on the wbits and compression level used. Make sure that ISA-L compression level 0 is advertised as the fastest compression in both the gzip header (setting xfl flag to 0x04) and the zlib header (as 0, fastest, other levels are 1, fast). Change-Id: I1f30e4397a0f5fcf6df593c40178e7d6f6c05328 Signed-off-by: Ruben Vorderman <[email protected]>
1 parent 1db0363 commit 94ec602

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

igzip/igzip.c

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,23 +1706,55 @@ int isal_deflate(struct isal_zstream *stream)
17061706
return ret;
17071707
}
17081708

1709+
// Helper function to avoid code duplication.
1710+
static void _zlib_header_in_buffer(struct isal_zstream *stream, uint8_t * buffer)
1711+
{
1712+
uint8_t hist_bits, info, level, cmf, flg;
1713+
uint8_t dict_flag = 0;
1714+
1715+
if (stream->hist_bits == 0) // default hist_bits
1716+
hist_bits = ISAL_DEF_MAX_HIST_BITS;
1717+
else
1718+
hist_bits = stream->hist_bits;
1719+
if (hist_bits > 8)
1720+
info = hist_bits - 8;
1721+
else
1722+
info = 0; // For low window sizes ensure correct cmf flag.
1723+
if (stream->level == 0)
1724+
level = 0; // Fastest algorithm
1725+
else
1726+
level = 1; // ISA-L levels 1-3 are fast algorithms.
1727+
1728+
cmf = DEFLATE_METHOD | (info << 4);
1729+
flg = (level << 6) | dict_flag;
1730+
flg += 31 - ((256 * cmf + flg) % 31);
1731+
buffer[0] = cmf;
1732+
buffer[1] = flg;
1733+
return;
1734+
}
1735+
17091736
static int write_stream_header_stateless(struct isal_zstream *stream)
17101737
{
17111738
uint32_t hdr_bytes;
1712-
const uint8_t *hdr;
1739+
// Create a 10-byte buffer. Since the gzip header is almost fixed (9 of 10
1740+
// bytes are fixed) use it to initialize the buffer.
1741+
uint8_t buffer[10] = {
1742+
0x1f, 0x8b, 0x08, 0x00, 0x00,
1743+
0x00, 0x00, 0x00, 0x00, 0xff
1744+
};
17131745
uint32_t next_flag;
17141746

17151747
if (stream->internal_state.has_wrap_hdr)
17161748
return COMP_OK;
17171749

17181750
if (stream->gzip_flag == IGZIP_ZLIB) {
17191751
hdr_bytes = zlib_hdr_bytes;
1720-
hdr = zlib_hdr;
1752+
_zlib_header_in_buffer(stream, buffer);
17211753
next_flag = IGZIP_ZLIB_NO_HDR;
1722-
17231754
} else {
17241755
hdr_bytes = gzip_hdr_bytes;
1725-
hdr = gzip_hdr;
1756+
if (stream->level == 0)
1757+
buffer[8] = 0x04; // Fastest algorithm in xfl flag
17261758
next_flag = IGZIP_GZIP_NO_HDR;
17271759
}
17281760

@@ -1732,7 +1764,7 @@ static int write_stream_header_stateless(struct isal_zstream *stream)
17321764
stream->avail_out -= hdr_bytes;
17331765
stream->total_out += hdr_bytes;
17341766

1735-
memcpy(stream->next_out, hdr, hdr_bytes);
1767+
memcpy(stream->next_out, buffer, hdr_bytes);
17361768

17371769
stream->next_out += hdr_bytes;
17381770
stream->internal_state.has_wrap_hdr = 1;
@@ -1746,17 +1778,23 @@ static void write_stream_header(struct isal_zstream *stream)
17461778
struct isal_zstate *state = &stream->internal_state;
17471779
int bytes_to_write;
17481780
uint32_t hdr_bytes;
1749-
const uint8_t *hdr;
1781+
// Create a 10-byte buffer. Since the gzip header is almost fixed (9 of 10
1782+
// bytes are fixed) use it to initialize the buffer.
1783+
uint8_t buffer[10] = {
1784+
0x1f, 0x8b, 0x08, 0x00, 0x00,
1785+
0x00, 0x00, 0x00, 0x00, 0xff
1786+
};
17501787

17511788
if (stream->internal_state.has_wrap_hdr)
17521789
return;
17531790

17541791
if (stream->gzip_flag == IGZIP_ZLIB) {
17551792
hdr_bytes = zlib_hdr_bytes;
1756-
hdr = zlib_hdr;
1793+
_zlib_header_in_buffer(stream, buffer);
17571794
} else {
1795+
if (stream->level == 0)
1796+
buffer[8] = 0x04; // Fastest algorithm in xfl flag
17581797
hdr_bytes = gzip_hdr_bytes;
1759-
hdr = gzip_hdr;
17601798
}
17611799

17621800
bytes_to_write = hdr_bytes;
@@ -1765,7 +1803,7 @@ static void write_stream_header(struct isal_zstream *stream)
17651803
if (bytes_to_write > stream->avail_out)
17661804
bytes_to_write = stream->avail_out;
17671805

1768-
memcpy(stream->next_out, hdr + state->count, bytes_to_write);
1806+
memcpy(stream->next_out, buffer + state->count, bytes_to_write);
17691807
state->count += bytes_to_write;
17701808

17711809
if (state->count == hdr_bytes) {

0 commit comments

Comments
 (0)