Skip to content

Commit 03a2ffa

Browse files
committed
Be fully compliant with wbits values
1 parent 4681ce6 commit 03a2ffa

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/isal/isal_zlib.pyx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ def decompress(data,
256256

257257
wbits_to_flag_and_hist_bits_inflate(wbits,
258258
&stream.hist_bits,
259-
&stream.crc_flag)
259+
&stream.crc_flag,
260+
data[:2] == b"\037\213")
260261

261262
# initialise input
262263
cdef Py_buffer buffer_data
@@ -310,7 +311,6 @@ def decompress(data,
310311

311312
def decompressobj(int wbits=ISAL_DEF_MAX_HIST_BITS,
312313
zdict = None):
313-
314314
return Decompress.__new__(Decompress, wbits, zdict)
315315

316316

@@ -464,13 +464,18 @@ cdef class Decompress:
464464
cdef inflate_state stream
465465
cdef unsigned char * obuf
466466
cdef unsigned int obuflen
467+
cdef bint method_set
467468

468469
def __cinit__(self, wbits=ISAL_DEF_MAX_HIST_BITS, zdict = None):
469470
isal_inflate_init(&self.stream)
470471

471472
wbits_to_flag_and_hist_bits_inflate(wbits,
472473
&self.stream.hist_bits,
473474
&self.stream.crc_flag)
475+
if 40 <= wbits <= 47:
476+
self.method_set = 0
477+
else:
478+
self.method_set = 1
474479

475480
cdef Py_ssize_t zdict_length
476481
if zdict:
@@ -516,6 +521,11 @@ cdef class Decompress:
516521
elif max_length < 0:
517522
raise ValueError("max_length can not be smaller than 0")
518523

524+
if not self.method_set:
525+
# Try to detect method from the first two bytes of the data.
526+
self.stream.crc_flag = ISAL_GZIP if data[:2] == b"\037\213" else ISAL_ZLIB
527+
self.method_set = 1
528+
519529
# initialise input
520530
cdef Py_buffer buffer_data
521531
cdef Py_buffer* buffer = &buffer_data
@@ -634,7 +644,8 @@ cdef wbits_to_flag_and_hist_bits_deflate(int wbits,
634644

635645
cdef wbits_to_flag_and_hist_bits_inflate(int wbits,
636646
unsigned int * hist_bits,
637-
unsigned int * crc_flag):
647+
unsigned int * crc_flag,
648+
bint gzip = 0):
638649
if wbits == 0:
639650
hist_bits[0] = 0
640651
crc_flag[0] = ISAL_ZLIB
@@ -647,6 +658,9 @@ cdef wbits_to_flag_and_hist_bits_inflate(int wbits,
647658
elif -15 <= wbits <= -8: # raw compressed stream
648659
hist_bits[0] = -wbits
649660
crc_flag[0] = ISAL_DEFLATE
661+
elif 40 <= wbits <= 47: # Accept gzip or zlib
662+
hist_bits[0] = wbits - 32
663+
crc_flag[0] = ISAL_GZIP if gzip else ISAL_ZLIB
650664
elif 72 <=wbits <= 79:
651665
hist_bits[0] = wbits - 64
652666
crc_flag[0] = ISAL_GZIP_NO_HDR_VER

tests/test_zlib_compliance.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -828,14 +828,12 @@ def test_wbits(self):
828828
isal_zlib15 = co.compress(HAMLET_SCENE) + co.flush()
829829
self.assertEqual(isal_zlib.decompress(isal_zlib15, 15), HAMLET_SCENE)
830830
self.assertEqual(isal_zlib.decompress(isal_zlib15, 0), HAMLET_SCENE)
831-
# This behaviour is not defined in either the zlib documentation or the
832-
# python documentation.
833-
# self.assertEqual(isal_zlib.decompress(isal_zlib15, 32 + 15),
834-
# HAMLET_SCENE)
831+
self.assertEqual(isal_zlib.decompress(isal_zlib15, 32 + 15),
832+
HAMLET_SCENE)
835833
with self.assertRaisesRegex(isal_zlib.error, 'nvalid'):
836834
isal_zlib.decompress(isal_zlib15, 9)
837-
# dco = isal_zlib.decompressobj(wbits=32 + 15)
838-
# self.assertEqual(dco.decompress(isal_zlib15), HAMLET_SCENE)
835+
dco = isal_zlib.decompressobj(wbits=32 + 15)
836+
self.assertEqual(dco.decompress(isal_zlib15), HAMLET_SCENE)
839837
dco = isal_zlib.decompressobj(wbits=9)
840838
with self.assertRaisesRegex(isal_zlib.error, 'nvalid'):
841839
dco.decompress(isal_zlib15)
@@ -845,10 +843,10 @@ def test_wbits(self):
845843
self.assertEqual(isal_zlib.decompress(isal_zlib9, 9), HAMLET_SCENE)
846844
self.assertEqual(isal_zlib.decompress(isal_zlib9, 15), HAMLET_SCENE)
847845
self.assertEqual(isal_zlib.decompress(isal_zlib9, 0), HAMLET_SCENE)
848-
# self.assertEqual(isal_zlib.decompress(isal_zlib9, 32 + 9),
849-
# HAMLET_SCENE)
850-
# dco = isal_zlib.decompressobj(wbits=32 + 9)
851-
# self.assertEqual(dco.decompress(isal_zlib9), HAMLET_SCENE)
846+
self.assertEqual(isal_zlib.decompress(isal_zlib9, 32 + 9),
847+
HAMLET_SCENE)
848+
dco = isal_zlib.decompressobj(wbits=32 + 9)
849+
self.assertEqual(dco.decompress(isal_zlib9), HAMLET_SCENE)
852850

853851
co = isal_zlib.compressobj(level=1, wbits=-15)
854852
deflate15 = co.compress(HAMLET_SCENE) + co.flush()
@@ -866,9 +864,9 @@ def test_wbits(self):
866864
co = isal_zlib.compressobj(level=1, wbits=16 + 15)
867865
gzip = co.compress(HAMLET_SCENE) + co.flush()
868866
self.assertEqual(isal_zlib.decompress(gzip, 16 + 15), HAMLET_SCENE)
869-
# self.assertEqual(isal_zlib.decompress(gzip, 32 + 15), HAMLET_SCENE)
870-
# dco = isal_zlib.decompressobj(32 + 15)
871-
# self.assertEqual(dco.decompress(gzip), HAMLET_SCENE)
867+
self.assertEqual(isal_zlib.decompress(gzip, 32 + 15), HAMLET_SCENE)
868+
dco = isal_zlib.decompressobj(32 + 15)
869+
self.assertEqual(dco.decompress(gzip), HAMLET_SCENE)
872870

873871

874872
def choose_lines(source, number, seed=None, generator=random):

0 commit comments

Comments
 (0)