Skip to content

Commit a52d46d

Browse files
committed
Custom parser for crc32, remove contiguity check
PyBUF_SIMPLE guarantees contiguous memory
1 parent 82cd0c9 commit a52d46d

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

src/isal/isal_zlib.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,6 @@ isal_zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
166166
if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
167167
return NULL;
168168
}
169-
if (!PyBuffer_IsContiguous(&data, 'C')) {
170-
PyErr_SetString(PyExc_ValueError, "data is not a contiguous buffer");
171-
PyBuffer_Release(&data);
172-
return NULL;
173-
}
174169
if (nargs > 1) {
175170
value = (uint32_t)PyLong_AsUnsignedLongMask(args[1]);
176171
if (value == (uint32_t)-1 && PyErr_Occurred()) {
@@ -205,32 +200,26 @@ isal_zlib_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
205200
Py_buffer data = {NULL, NULL};
206201
uint32_t value = 0;
207202

208-
if (!_PyArg_CheckPositional("crc32", nargs, 1, 2)) {
209-
goto exit;
203+
if (nargs < 1 || nargs > 2) {
204+
PyErr_Format(
205+
PyExc_TypeError,
206+
"crc32 takes exactly 1 or 2 arguments, got %d",
207+
nargs);
208+
return NULL;
210209
}
211210
if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
212-
goto exit;
213-
}
214-
if (!PyBuffer_IsContiguous(&data, 'C')) {
215-
_PyArg_BadArgument("crc32", "argument 1", "contiguous buffer", args[0]);
216-
goto exit;
217-
}
218-
if (nargs < 2) {
219-
goto skip_optional;
211+
return NULL;
220212
}
221-
value = (uint32_t)PyLong_AsUnsignedLongMask(args[1]);
222-
if (value == (uint32_t)-1 && PyErr_Occurred()) {
223-
goto exit;
213+
if (nargs > 1) {
214+
value = (uint32_t)PyLong_AsUnsignedLongMask(args[1]);
215+
if (value == (uint32_t)-1 && PyErr_Occurred()) {
216+
PyBuffer_Release(&data);
217+
return NULL;
218+
}
224219
}
225-
skip_optional:
226220
value = crc32_gzip_refl(value, data.buf, (uint64_t)data.len);
227221
return_value = PyLong_FromUnsignedLong(value & 0xffffffffU);
228-
229-
exit:
230-
/* Cleanup for data */
231-
if (data.obj) {
232-
PyBuffer_Release(&data);
233-
}
222+
PyBuffer_Release(&data);
234223
return return_value;
235224
}
236225
PyDoc_STRVAR(zlib_compress__doc__,

0 commit comments

Comments
 (0)