Skip to content

Commit 5fdc378

Browse files
committed
Working decompression for gzip files
1 parent 257b73a commit 5fdc378

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/isal/igzip.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import argparse
2525
import io
2626
import os
27+
import _compression
2728

2829
# We import a copy of the gzip module so we can use that to define the classes
2930
# here and replace the zlib module with the isal_zlib module. This way the
@@ -142,15 +143,12 @@ def write(self,data):
142143
return length
143144

144145

145-
class _IGzipReader(gzip._GzipReader):
146+
class _IGzipReader(_compression.DecompressReader):
146147
def __init__(self, fp):
147-
super().__init__(fp)
148-
self._decompfactory = isal_zlib.decompressobj
149-
self._decompressor = self._decompfactory(**self._decomp_args)
148+
super().__init__(fp, isal_zlib.decompressobj,
149+
wbits=16 + isal_zlib.MAX_WBITS)
150150

151-
def _add_read_data(self, data):
152-
self._crc = isal_zlib.crc32(data, self._crc)
153-
self._stream_size += len(data)
151+
154152

155153
# Plagiarized from gzip.py from python's stdlib.
156154
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):

src/isal/isal_zlib.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ cdef class Decompress:
353353
cdef public bytes unused_data
354354
cdef public unconsumed_tail
355355
cdef public bint eof
356+
cdef public bint needs_input
356357
cdef bint is_initialised
357358
cdef inflate_state stream
358359
cdef unsigned char * obuf
@@ -379,6 +380,7 @@ cdef class Decompress:
379380
self.unconsumed_tail = b""
380381
self.eof = 0
381382
self.is_initialised = 1
383+
self.needs_input = 1
382384

383385
def __dealloc__(self):
384386
if self.obuf is not NULL:
@@ -396,6 +398,8 @@ cdef class Decompress:
396398
elif max_length < 0:
397399
raise ValueError("max_length can not be smaller than 0")
398400

401+
if data == b"":
402+
data = self.unconsumed_tail
399403
cdef Py_ssize_t total_length = len(data)
400404
if total_length > UINT32_MAX:
401405
# Zlib allows a maximum of 64 KB (16-bit length) and python has
@@ -454,6 +458,7 @@ cdef class Decompress:
454458
# 2. All input data was consumed. Clear unconsumed_tail.
455459
unused_bytes = self.stream.avail_in
456460
self.unconsumed_tail = data[-unused_bytes:]
461+
self.needs_input = 0 if unused_bytes > 0 else 1
457462
return b"".join(out)
458463

459464
def flush(self, Py_ssize_t length = DEF_BUF_SIZE):

0 commit comments

Comments
 (0)