@@ -94,6 +94,20 @@ cdef Py_ssize_t Py_ssize_t_min(Py_ssize_t a, Py_ssize_t b):
94
94
else :
95
95
return b
96
96
97
+ ctypedef fused stream_or_state:
98
+ isal_zstream
99
+ inflate_state
100
+
101
+ cdef unsigned long unsigned_long_min(unsigned long a, unsigned long b):
102
+ if a <= b:
103
+ return a
104
+ else :
105
+ return b
106
+
107
+ cdef void arrange_input_buffer(stream_or_state * stream, Py_ssize_t * remains):
108
+ stream.avail_in = unsigned_long_min(< unsigned long > remains[0 ], UINT32_MAX)
109
+ remains[0 ] -= stream.avail_in
110
+
97
111
cpdef bytes compress(data,
98
112
int level = ISAL_DEFAULT_COMPRESSION_I,
99
113
int wbits = ISAL_DEF_MAX_HIST_BITS):
@@ -118,12 +132,9 @@ cpdef bytes compress(data,
118
132
out = []
119
133
120
134
# initialise input
121
- cdef Py_ssize_t max_input_buffer = UINT32_MAX
122
- cdef Py_ssize_t total_length = len (data)
123
- cdef Py_ssize_t remains = total_length
124
- cdef Py_ssize_t ibuflen = total_length
125
- cdef Py_ssize_t position = 0
126
- cdef bytes ibuf
135
+ cdef Py_ssize_t ibuflen = len (data)
136
+ cdef unsigned char * ibuf = data
137
+ stream.next_in = ibuf
127
138
128
139
# initialise helper variables
129
140
cdef int err
@@ -134,12 +145,7 @@ cpdef bytes compress(data,
134
145
# This loop runs n times (at least twice). n-1 times to fill the input
135
146
# buffer with data. The nth time the input is empty. In that case
136
147
# stream.flush is set to FULL_FLUSH and the end_of_stream is activated.
137
- ibuflen = Py_ssize_t_min(remains, max_input_buffer)
138
- ibuf = data[position: position + ibuflen]
139
- position += ibuflen
140
- stream.next_in = ibuf
141
- remains -= ibuflen
142
- stream.avail_in = ibuflen
148
+ arrange_input_buffer(& stream, & ibuflen)
143
149
if ibuflen == 0 :
144
150
stream.flush = FULL_FLUSH
145
151
stream.end_of_stream = 1
@@ -184,12 +190,9 @@ cpdef decompress(data,
184
190
& stream.crc_flag)
185
191
186
192
# initialise input
187
- cdef Py_ssize_t max_input_buffer = UINT32_MAX
188
- cdef Py_ssize_t total_length = len (data)
189
- cdef Py_ssize_t remains = total_length
190
- cdef Py_ssize_t ibuflen = total_length
191
- cdef Py_ssize_t position = 0
192
- cdef bytes ibuf
193
+ cdef Py_ssize_t ibuflen = len (data)
194
+ cdef unsigned char * ibuf = data
195
+ stream.next_in = ibuf
193
196
194
197
# Initialise output buffer
195
198
cdef unsigned long obuflen = bufsize
@@ -199,13 +202,8 @@ cpdef decompress(data,
199
202
200
203
# Implementation imitated from CPython's zlibmodule.c
201
204
try :
202
- while ibuflen != 0 and stream.block_state != ISAL_BLOCK_FINISH:
203
- ibuflen = Py_ssize_t_min(remains, max_input_buffer)
204
- ibuf = data[position: position + ibuflen]
205
- position += ibuflen
206
- stream.next_in = ibuf
207
- remains -= ibuflen
208
- stream.avail_in = ibuflen
205
+ while ibuflen != 0 or stream.block_state != ISAL_BLOCK_FINISH:
206
+ arrange_input_buffer(& stream, & ibuflen)
209
207
210
208
# This loop reads all the input bytes. The check is at the end,
211
209
# because when the block state is not at FINISH, the function needs
0 commit comments