Skip to content

Commit ebfd459

Browse files
committed
Add more comments and simplify save_unconsumed_input function
1 parent 22d09aa commit ebfd459

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/isal/isal_zlib.pyx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -540,29 +540,32 @@ cdef class Decompress:
540540
if self.obuf is not NULL:
541541
PyMem_Free(self.obuf)
542542

543-
cdef bytes consume_read_in(self):
543+
def _view_bitbuffer(self):
544+
"""Shows the 64-bitbuffer of the internal inflate_state. It contains
545+
a maximum of 8 bytes. This data is already read-in so is not part
546+
of the unconsumed tail."""
544547
read_in_length = self.stream.read_in_length // 8
545548
if read_in_length == 0:
546549
return b""
547550
read_in = self.stream.read_in
548-
result = read_in.to_bytes(8, "little")[:read_in_length]
551+
# The bytes are added by bitshifting, so in reverse order. Reading the
552+
# 64-bit integer into 8 bytes little-endian provides the characters in
553+
# the correct order.
549554
return read_in.to_bytes(8, "little")[:read_in_length]
550555

551556
cdef save_unconsumed_input(self, Py_buffer *data):
552557
cdef Py_ssize_t old_size, new_size, left_size
553558
cdef bytes new_data
559+
left_size = <unsigned char*>data.buf + data.len - self.stream.next_in
560+
new_data = PyBytes_FromStringAndSize(<char *>self.stream.next_in, left_size)
554561
if self.stream.block_state == ISAL_BLOCK_FINISH:
555562
self.eof = 1
556-
if self.stream.avail_in > 0:
557-
old_size = len(self.unused_data)
558-
left_size = <unsigned char*>data.buf + data.len - self.stream.next_in
559-
if left_size > (PY_SSIZE_T_MAX - old_size):
560-
raise MemoryError()
561-
new_data = PyBytes_FromStringAndSize(<char *>self.stream.next_in, left_size)
562-
self.unused_data = self.consume_read_in() + new_data
563-
if self.stream.avail_in > 0 or self.unconsumed_tail:
564-
left_size = <unsigned char*>data.buf + data.len - self.stream.next_in
565-
new_data = PyBytes_FromStringAndSize(<char *>self.stream.next_in, left_size)
563+
if self.stream.avail_in > 0 or self._view_bitbuffer():
564+
# The block is finished and this decompressobject can not be
565+
# used anymore. Some unused data is in the bitbuffer and has to
566+
# be recovered.
567+
self.unused_data = self._view_bitbuffer() + new_data
568+
if self.stream.avail_in > 0:
566569
self.unconsumed_tail = new_data
567570

568571
def decompress(self, data, Py_ssize_t max_length = 0):

0 commit comments

Comments
 (0)