Skip to content

Commit c64e46d

Browse files
committed
Merge branch 'develop' of github.com:pycompression/python-isal into develop
2 parents 415bd45 + d4f1af3 commit c64e46d

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Changelog
99
1010
version 0.5.0-dev
1111
-----------------
12+
+ Fix a bug where negative integers were not allowed for the ``adler32`` and
13+
``crc32`` functions in ``isal_zlib``.
1214
+ Provided stubs (type-hint files) for ``isal_zlib`` and ``_isal`` modules.
1315
Package is now tested with mypy to ensure correct type information.
1416
+ The command-line interface now reads in blocks of 32K instead of 8K. This

README.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ Differences with zlib and gzip modules
129129
in ``isal_zlib`` and ``igzip`` this is the **lowest compression level**.
130130
This is a design choice that was inherited from the ISA-L library.
131131
+ Compression levels range from 0 to 3, not 1 to 9.
132-
+ ``isal_zlib.crc32`` and ``isal_zlib.adler32`` do not support negative
133-
numbers for the value parameter.
134132
+ ``zlib.Z_DEFAULT_STRATEGY``, ``zlib.Z_RLE`` etc. are exposed as
135133
``isal_zlib.Z_DEFAULT_STRATEGY``, ``isal_zlib.Z_RLE`` etc. for compatibility
136134
reasons. However, ``isal_zlib`` only supports a default strategy and will

src/isal/isal_zlib.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ from cpython.mem cimport PyMem_Malloc, PyMem_Free
3434
from cpython.buffer cimport PyBUF_READ, PyBUF_C_CONTIGUOUS, PyObject_GetBuffer, \
3535
PyBuffer_Release
3636
from cpython.bytes cimport PyBytes_FromStringAndSize
37-
37+
from cpython.long cimport PyLong_AsUnsignedLongMask
3838

3939
cdef extern from "<Python.h>":
4040
const Py_ssize_t PY_SSIZE_T_MAX
@@ -86,41 +86,43 @@ if ISAL_DEF_MAX_HIST_BITS > zlib.MAX_WBITS:
8686
"Please contact the developers.")
8787

8888

89-
def adler32(data, unsigned int value = 1):
89+
def adler32(data, value = 1):
9090
"""
9191
Computes an Adler-32 checksum of *data*. Returns the checksum as unsigned
9292
32-bit integer.
9393
9494
:param data: Binary data (bytes, bytearray, memoryview).
9595
:param value: The starting value of the checksum.
9696
"""
97+
cdef unsigned long init = PyLong_AsUnsignedLongMask(value)
9798
cdef Py_buffer buffer_data
9899
cdef Py_buffer* buffer = &buffer_data
99100
if PyObject_GetBuffer(data, buffer, PyBUF_READ & PyBUF_C_CONTIGUOUS) != 0:
100101
raise TypeError("Failed to get buffer")
101102
try:
102103
if buffer.len > UINT64_MAX:
103104
raise ValueError("Data too big for adler32")
104-
return isal_adler32(value, <unsigned char*>buffer.buf, buffer.len)
105+
return isal_adler32(init, <unsigned char*>buffer.buf, buffer.len)
105106
finally:
106107
PyBuffer_Release(buffer)
107108

108-
def crc32(data, unsigned int value = 0):
109+
def crc32(data, value = 0):
109110
"""
110111
Computes a CRC-32 checksum of *data*. Returns the checksum as unsigned
111112
32-bit integer.
112113
113114
:param data: Binary data (bytes, bytearray, memoryview).
114115
:param value: The starting value of the checksum.
115116
"""
117+
cdef unsigned long init = PyLong_AsUnsignedLongMask(value)
116118
cdef Py_buffer buffer_data
117119
cdef Py_buffer* buffer = &buffer_data
118120
if PyObject_GetBuffer(data, buffer, PyBUF_READ & PyBUF_C_CONTIGUOUS) != 0:
119121
raise TypeError("Failed to get buffer")
120122
try:
121123
if buffer.len > UINT64_MAX:
122124
raise ValueError("Data too big for adler32")
123-
return crc32_gzip_refl(value, <unsigned char*>buffer.buf, buffer.len)
125+
return crc32_gzip_refl(init, <unsigned char*>buffer.buf, buffer.len)
124126
finally:
125127
PyBuffer_Release(buffer)
126128

tests/test_isal.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
DATA_SIZES = [2**i for i in range(3, 20)]
3636
# 100 seeds generated with random.randint(0, 2**32-1)
3737
SEEDS_FILE = DATA_DIR / "seeds.txt"
38-
# Create seeds 0, 1 and 20 seeds from the seeds file.
39-
SEEDS = [0, 1] + [int(seed) for seed in SEEDS_FILE.read_text().splitlines()]
38+
INT_OVERFLOW = 211928379812738912738917238971289378912379823871932719823798123
39+
# Get some negative ints and some really big ints into the mix.
40+
SEEDS = [-INT_OVERFLOW, -3, -1, 0, 1, INT_OVERFLOW] + [
41+
int(seed) for seed in SEEDS_FILE.read_text().splitlines()]
4042

4143
# Wbits for ZLIB compression, GZIP compression, and RAW compressed streams
4244
WBITS_RANGE = list(range(9, 16)) + list(range(25, 32)) + list(range(-15, -8))

0 commit comments

Comments
 (0)