Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/zlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ The available exception and functions in this module are:
platforms, use ``adler32(data) & 0xffffffff``.


.. function:: adler32_combine(adler_1, adler_2, len_2)

Combines two Adler-32 checksums *adler_1* and *adler_2* into one. *len_2* is the length of the string used to generate *adler_2*.


.. function:: compress(data, /, level=-1)

Compresses the bytes in *data*, returning a bytes object containing compressed data.
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def test_adler32empty(self):
self.assertEqual(zlib.adler32(b"", 1), 1)
self.assertEqual(zlib.adler32(b"", 432), 432)

def test_adler32_combine(self):
self.assertEqual(zlib.adler32_combine(zlib.adler32(b"penguin"), zlib.adler32(b"banana"), len(b"banana")), 636028248)

def test_penguins(self):
self.assertEqual(zlib.crc32(b"penguin", 0), 0x0e5c1a120)
self.assertEqual(zlib.crc32(b"penguin", 1), 0x43b6aa94)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1896,3 +1896,4 @@ Robert Leenders
Tim Hopper
Dan Lidral-Porter
Ngalim Siregar
Callum Attryde
68 changes: 67 additions & 1 deletion Modules/clinic/zlibmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Modules/zlibmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,31 @@ zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
return PyLong_FromUnsignedLong(value & 0xffffffffU);
}

/*[clinic input]
zlib.adler32_combine

adler_1: unsigned_int(bitwise=True)
First checksum.
adler_2: unsigned_int(bitwise=True)
Second checksum.
len_2: unsigned_int(bitwise=True)
Length of the buffer used to generate the second checksum.
/

Combine two Adler-32 Checksums into one.

The returned checksum is an integer.
[clinic start generated code]*/

static PyObject *
zlib_adler32_combine_impl(PyObject *module, unsigned int adler_1,
unsigned int adler_2, unsigned int len_2)
/*[clinic end generated code: output=af156f2b847877ae input=c436395ab679bbc2]*/
{
unsigned long value = adler32_combine64(adler_1, adler_2, len_2);
return PyLong_FromUnsignedLong(value & 0xffffffffU);
}

/*[clinic input]
zlib.crc32

Expand Down Expand Up @@ -1309,6 +1334,7 @@ zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
static PyMethodDef zlib_methods[] =
{
ZLIB_ADLER32_METHODDEF
ZLIB_ADLER32_COMBINE_METHODDEF
ZLIB_COMPRESS_METHODDEF
ZLIB_COMPRESSOBJ_METHODDEF
ZLIB_CRC32_METHODDEF
Expand Down Expand Up @@ -1351,6 +1377,7 @@ PyDoc_STRVAR(zlib_module_documentation,
"zlib library, which is based on GNU zip.\n"
"\n"
"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
"adler32_combine(adler1, adler2, len2) -- Combine two Adler-32 checksums.\n"
"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
"compressobj([level[, ...]]) -- Return a compressor object.\n"
"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Expand Down