@@ -540,29 +540,32 @@ cdef class Decompress:
540
540
if self .obuf is not NULL :
541
541
PyMem_Free(self .obuf)
542
542
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."""
544
547
read_in_length = self .stream.read_in_length // 8
545
548
if read_in_length == 0 :
546
549
return b" "
547
550
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.
549
554
return read_in.to_bytes(8 , " little" )[:read_in_length]
550
555
551
556
cdef save_unconsumed_input(self , Py_buffer * data):
552
557
cdef Py_ssize_t old_size, new_size, left_size
553
558
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)
554
561
if self .stream.block_state == ISAL_BLOCK_FINISH:
555
562
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 :
566
569
self .unconsumed_tail = new_data
567
570
568
571
def decompress (self , data , Py_ssize_t max_length = 0 ):
0 commit comments