Skip to content

Commit 257b73a

Browse files
committed
Return correct unused data
1 parent 39272f1 commit 257b73a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/isal/isal_zlib.pyx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ cdef class Decompress:
409409
self.stream.avail_out = 0
410410
cdef unsigned long prev_avail_out
411411
cdef unsigned long bytes_written
412+
cdef Py_ssize_t unused_bytes
412413
cdef int err
413414
cdef bint last_round = 0
414415
# This loop reads all the input bytes. If there are no input bytes
@@ -444,13 +445,15 @@ cdef class Decompress:
444445
# leftover input data in self->unused_data.
445446
self.eof = 1
446447
if self.stream.avail_in > 0:
447-
self.unused_data = data[total_bytes:]
448+
unused_bytes = self.stream.avail_in
449+
self.unused_data = data[-unused_bytes:]
448450
self.stream.avail_in = 0
449451
if self.stream.avail_in > 0 or self.unconsumed_tail:
450452
# This code handles two distinct cases:
451453
# 1. Output limit was reached. Save leftover input in unconsumed_tail.
452454
# 2. All input data was consumed. Clear unconsumed_tail.
453-
self.unconsumed_tail = data[total_bytes:]
455+
unused_bytes = self.stream.avail_in
456+
self.unconsumed_tail = data[-unused_bytes:]
454457
return b"".join(out)
455458

456459
def flush(self, Py_ssize_t length = DEF_BUF_SIZE):
@@ -471,6 +474,7 @@ cdef class Decompress:
471474
out = []
472475
cdef unsigned long obuflen = length
473476
cdef unsigned char * obuf = <unsigned char *>PyMem_Malloc(obuflen * sizeof(char))
477+
cdef Py_ssize_t unused_bytes
474478

475479
try:
476480
while (self.stream.block_state != ISAL_BLOCK_FINISH
@@ -495,13 +499,14 @@ cdef class Decompress:
495499
self.eof = 1
496500
self.is_initialised = 0
497501
if self.stream.avail_in > 0:
498-
self.unused_data = data[total_bytes:]
499-
self.stream.avail_in = 0
502+
unused_bytes = self.stream.avail_in
503+
self.unused_data = data[-unused_bytes:]
500504
if self.stream.avail_in > 0 or self.unconsumed_tail:
501505
# This code handles two distinct cases:
502506
# 1. Output limit was reached. Save leftover input in unconsumed_tail.
503507
# 2. All input data was consumed. Clear unconsumed_tail.
504-
self.unconsumed_tail = data[total_bytes:]
508+
unused_bytes = self.stream.avail_in
509+
self.unconsumed_tail = data[-unused_bytes:]
505510
return b"".join(out)
506511
finally:
507512
PyMem_Free(obuf)

0 commit comments

Comments
 (0)