@@ -1706,23 +1706,55 @@ int isal_deflate(struct isal_zstream *stream)
1706
1706
return ret ;
1707
1707
}
1708
1708
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
+
1709
1736
static int write_stream_header_stateless (struct isal_zstream * stream )
1710
1737
{
1711
1738
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
+ };
1713
1745
uint32_t next_flag ;
1714
1746
1715
1747
if (stream -> internal_state .has_wrap_hdr )
1716
1748
return COMP_OK ;
1717
1749
1718
1750
if (stream -> gzip_flag == IGZIP_ZLIB ) {
1719
1751
hdr_bytes = zlib_hdr_bytes ;
1720
- hdr = zlib_hdr ;
1752
+ _zlib_header_in_buffer ( stream , buffer ) ;
1721
1753
next_flag = IGZIP_ZLIB_NO_HDR ;
1722
-
1723
1754
} else {
1724
1755
hdr_bytes = gzip_hdr_bytes ;
1725
- hdr = gzip_hdr ;
1756
+ if (stream -> level == 0 )
1757
+ buffer [8 ] = 0x04 ; // Fastest algorithm in xfl flag
1726
1758
next_flag = IGZIP_GZIP_NO_HDR ;
1727
1759
}
1728
1760
@@ -1732,7 +1764,7 @@ static int write_stream_header_stateless(struct isal_zstream *stream)
1732
1764
stream -> avail_out -= hdr_bytes ;
1733
1765
stream -> total_out += hdr_bytes ;
1734
1766
1735
- memcpy (stream -> next_out , hdr , hdr_bytes );
1767
+ memcpy (stream -> next_out , buffer , hdr_bytes );
1736
1768
1737
1769
stream -> next_out += hdr_bytes ;
1738
1770
stream -> internal_state .has_wrap_hdr = 1 ;
@@ -1746,17 +1778,23 @@ static void write_stream_header(struct isal_zstream *stream)
1746
1778
struct isal_zstate * state = & stream -> internal_state ;
1747
1779
int bytes_to_write ;
1748
1780
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
+ };
1750
1787
1751
1788
if (stream -> internal_state .has_wrap_hdr )
1752
1789
return ;
1753
1790
1754
1791
if (stream -> gzip_flag == IGZIP_ZLIB ) {
1755
1792
hdr_bytes = zlib_hdr_bytes ;
1756
- hdr = zlib_hdr ;
1793
+ _zlib_header_in_buffer ( stream , buffer ) ;
1757
1794
} else {
1795
+ if (stream -> level == 0 )
1796
+ buffer [8 ] = 0x04 ; // Fastest algorithm in xfl flag
1758
1797
hdr_bytes = gzip_hdr_bytes ;
1759
- hdr = gzip_hdr ;
1760
1798
}
1761
1799
1762
1800
bytes_to_write = hdr_bytes ;
@@ -1765,7 +1803,7 @@ static void write_stream_header(struct isal_zstream *stream)
1765
1803
if (bytes_to_write > stream -> avail_out )
1766
1804
bytes_to_write = stream -> avail_out ;
1767
1805
1768
- memcpy (stream -> next_out , hdr + state -> count , bytes_to_write );
1806
+ memcpy (stream -> next_out , buffer + state -> count , bytes_to_write );
1769
1807
state -> count += bytes_to_write ;
1770
1808
1771
1809
if (state -> count == hdr_bytes ) {
0 commit comments