From c09b4a02e11f1a5e313c0a2b3153c462dd38c003 Mon Sep 17 00:00:00 2001 From: Quentin Kaiser Date: Fri, 13 Oct 2023 16:36:36 +0200 Subject: [PATCH] fix(handlers): improve gzip reader according to recent changes in cpython. The gzip._GzipReader we're inheriting from had some changes to stay up to date with changes in zlib library and to perform some optimization. Specifically: - GzipFile.read has been optimized. There is no longer a unconsumed_tail member to write back to padded file. This is instead handled by the ZlibDecompressor itself, which has an internal buffer. - _add_read_data has been inlined, as it was just two calls. We've adapted our own code to reflect these changes. More info: https://github.com/python/cpython/pull/97664 --- unblob/handlers/compression/_gzip_reader.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/unblob/handlers/compression/_gzip_reader.py b/unblob/handlers/compression/_gzip_reader.py index a745b89684..eca36f7f79 100644 --- a/unblob/handlers/compression/_gzip_reader.py +++ b/unblob/handlers/compression/_gzip_reader.py @@ -1,4 +1,5 @@ import gzip +import zlib from ...file_utils import DEFAULT_BUFSIZE @@ -10,6 +11,10 @@ def read_header(self): self._init_read() return self._read_gzip_header() + def _add_read_data(self, data): + self._crc = zlib.crc32(data, self._crc) + self._stream_size = self._stream_size + len(data) + def read(self): uncompress = b"" @@ -17,7 +22,8 @@ def read(self): buf = self._fp.read(DEFAULT_BUFSIZE) uncompress = self._decompressor.decompress(buf, DEFAULT_BUFSIZE) - self._fp.prepend(self._decompressor.unconsumed_tail) + if hasattr(self._decompressor, "unconsumed_tail"): + self._fp.prepend(self._decompressor.unconsumed_tail) self._fp.prepend(self._decompressor.unused_data) if uncompress != b"":