blob: 1ddaefd8a6d792901819d72fb6882feb7ef85d54 [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin Panter84544c12016-07-23 03:02:07 +00002/* See http://zlib.net/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Victor Stinnerf18f8712014-07-01 16:48:12 +02006#define PY_SSIZE_T_CLEAN
Guido van Rossumfb221561997-04-29 15:38:09 +00007
Guido van Rossum97b54571997-06-03 22:21:47 +00008#include "Python.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02009#include "structmember.h" // PyMemberDef
Guido van Rossum97b54571997-06-03 22:21:47 +000010#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000011
Larry Hastings31826802013-10-19 00:09:25 -070012
Ma Lin93f41182021-04-27 16:37:11 +080013#define ENTER_ZLIB(obj) do { \
14 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
15 Py_BEGIN_ALLOW_THREADS \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS \
18 } } while (0)
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020019#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000020
Martin Panter3f0ee832016-06-05 10:48:34 +000021#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000022# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000023#endif
24
Guido van Rossumfb221561997-04-29 15:38:09 +000025/* The following parameters are copied from zutil.h, version 0.95 */
26#define DEFLATED 8
27#if MAX_MEM_LEVEL >= 8
28# define DEF_MEM_LEVEL 8
29#else
30# define DEF_MEM_LEVEL MAX_MEM_LEVEL
31#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000032
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020033/* Initial buffer size. */
34#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Dino Viehlanda1ffad02019-09-10 11:27:03 +010036static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +000037
Dino Viehlanda1ffad02019-09-10 11:27:03 +010038typedef struct {
39 PyTypeObject *Comptype;
40 PyTypeObject *Decomptype;
41 PyObject *ZlibError;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050042} zlibstate;
Dino Viehlanda1ffad02019-09-10 11:27:03 +010043
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050044static inline zlibstate*
Hai Shif707d942020-03-16 21:15:01 +080045get_zlib_state(PyObject *module)
46{
47 void *state = PyModule_GetState(module);
48 assert(state != NULL);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050049 return (zlibstate *)state;
Hai Shif707d942020-03-16 21:15:01 +080050}
51
Tim Peters977e5402001-10-17 03:57:20 +000052typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000053{
Jeremy Hylton9714f992001-10-16 21:19:45 +000054 PyObject_HEAD
55 z_stream zst;
56 PyObject *unused_data;
57 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020058 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000059 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020060 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020061 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000062} compobject;
63
Jeremy Hylton0965e082001-10-16 21:56:09 +000064static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050065zlib_error(zlibstate *state, z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000066{
Nadeem Vawda524148a2011-08-28 11:26:46 +020067 const char *zmsg = Z_NULL;
68 /* In case of a version mismatch, zst.msg won't be initialized.
69 Check for this case first, before looking at zst.msg. */
70 if (err == Z_VERSION_ERROR)
71 zmsg = "library version mismatch";
72 if (zmsg == Z_NULL)
73 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000074 if (zmsg == Z_NULL) {
75 switch (err) {
76 case Z_BUF_ERROR:
77 zmsg = "incomplete or truncated stream";
78 break;
79 case Z_STREAM_ERROR:
80 zmsg = "inconsistent stream state";
81 break;
82 case Z_DATA_ERROR:
83 zmsg = "invalid input data";
84 break;
85 }
86 }
87 if (zmsg == Z_NULL)
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050088 PyErr_Format(state->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000089 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050090 PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000091}
92
Larry Hastings61272b72014-01-07 12:41:53 -080093/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080094module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080095class zlib.Compress "compobject *" "&Comptype"
96class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080097[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030098/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080099
Guido van Rossumfb221561997-04-29 15:38:09 +0000100static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000101newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000102{
Tim Peters977e5402001-10-17 03:57:20 +0000103 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 self = PyObject_New(compobject, type);
105 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000106 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200107 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200109 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000110 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 Py_DECREF(self);
113 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000115 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000116 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 Py_DECREF(self);
118 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000120 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200121 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000122 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200123 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
124 return NULL;
125 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000126 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000127}
128
Victor Stinner5064a522013-07-07 16:50:27 +0200129static void*
130PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
131{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300132 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200133 return NULL;
134 /* PyMem_Malloc() cannot be used: the GIL is not held when
135 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300136 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200137}
138
139static void
140PyZlib_Free(voidpf ctx, void *ptr)
141{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200142 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200143}
144
Martin Panter84544c12016-07-23 03:02:07 +0000145static void
146arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
147{
Segev Finer679b5662017-07-27 01:17:57 +0300148 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000149 *remains -= zst->avail_in;
150}
151
152static Py_ssize_t
153arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
154 Py_ssize_t length,
155 Py_ssize_t max_length)
156{
157 Py_ssize_t occupied;
158
159 if (*buffer == NULL) {
160 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
161 return -1;
162 occupied = 0;
163 }
164 else {
165 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
166
167 if (length == occupied) {
168 Py_ssize_t new_length;
169 assert(length <= max_length);
170 /* can not scale the buffer over max_length */
171 if (length == max_length)
172 return -2;
173 if (length <= (max_length >> 1))
174 new_length = length << 1;
175 else
176 new_length = max_length;
177 if (_PyBytes_Resize(buffer, new_length) < 0)
178 return -1;
179 length = new_length;
180 }
181 }
182
Segev Finer679b5662017-07-27 01:17:57 +0300183 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000184 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
185
186 return length;
187}
188
189static Py_ssize_t
190arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
191{
192 Py_ssize_t ret;
193
194 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
195 PY_SSIZE_T_MAX);
196 if (ret == -2)
197 PyErr_NoMemory();
198
199 return ret;
200}
201
Larry Hastings61272b72014-01-07 12:41:53 -0800202/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800203zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200204
Martin Panter1fe0d132016-02-10 10:06:36 +0000205 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800206 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300207 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200208 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000209 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800210
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200211Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800212[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800213
Guido van Rossumfb221561997-04-29 15:38:09 +0000214static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300215zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
216/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000217{
Martin Panter84544c12016-07-23 03:02:07 +0000218 PyObject *RetVal = NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500219 Py_ssize_t obuflen = DEF_BUF_SIZE;
220 int flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000221 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000222
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500223 zlibstate *state = get_zlib_state(module);
224
225 Byte *ibuf = data->buf;
226 Py_ssize_t ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000227
Victor Stinner5064a522013-07-07 16:50:27 +0200228 zst.opaque = NULL;
229 zst.zalloc = PyZlib_Malloc;
230 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000231 zst.next_in = ibuf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500232 int err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000233
Martin Panter84544c12016-07-23 03:02:07 +0000234 switch (err) {
235 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 break;
Martin Panter84544c12016-07-23 03:02:07 +0000237 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238 PyErr_SetString(PyExc_MemoryError,
239 "Out of memory while compressing data");
240 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000241 case Z_STREAM_ERROR:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500242 PyErr_SetString(state->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000243 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000244 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000245 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500246 zlib_error(state, zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000247 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000248 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000249
Martin Panter84544c12016-07-23 03:02:07 +0000250 do {
251 arrange_input_buffer(&zst, &ibuflen);
252 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000253
Martin Panter84544c12016-07-23 03:02:07 +0000254 do {
255 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
256 if (obuflen < 0) {
257 deflateEnd(&zst);
258 goto error;
259 }
260
261 Py_BEGIN_ALLOW_THREADS
262 err = deflate(&zst, flush);
263 Py_END_ALLOW_THREADS
264
265 if (err == Z_STREAM_ERROR) {
266 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500267 zlib_error(state, zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000268 goto error;
269 }
270
271 } while (zst.avail_out == 0);
272 assert(zst.avail_in == 0);
273
274 } while (flush != Z_FINISH);
275 assert(err == Z_STREAM_END);
276
277 err = deflateEnd(&zst);
278 if (err == Z_OK) {
279 if (_PyBytes_Resize(&RetVal, zst.next_out -
280 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
281 goto error;
282 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000283 }
Tim Peters977e5402001-10-17 03:57:20 +0000284 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500285 zlib_error(state, zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000286 error:
Martin Panter84544c12016-07-23 03:02:07 +0000287 Py_XDECREF(RetVal);
288 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000289}
290
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200291/*[clinic input]
292zlib.decompress
293
294 data: Py_buffer
295 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300296 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200297 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000298 The window buffer size and container format.
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300299 bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200300 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200301
302Returns a bytes object containing the uncompressed data.
303[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000304
Guido van Rossumfb221561997-04-29 15:38:09 +0000305static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300306zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000307 Py_ssize_t bufsize)
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300308/*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000309{
Martin Panter84544c12016-07-23 03:02:07 +0000310 PyObject *RetVal = NULL;
311 Byte *ibuf;
312 Py_ssize_t ibuflen;
313 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000314 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000315
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500316 zlibstate *state = get_zlib_state(module);
317
Martin Panter84544c12016-07-23 03:02:07 +0000318 if (bufsize < 0) {
319 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
320 return NULL;
321 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100322 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000323 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000324
Martin Panter84544c12016-07-23 03:02:07 +0000325 ibuf = data->buf;
326 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000327
Victor Stinner5064a522013-07-07 16:50:27 +0200328 zst.opaque = NULL;
329 zst.zalloc = PyZlib_Malloc;
330 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000331 zst.avail_in = 0;
332 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200333 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000334
Martin Panter84544c12016-07-23 03:02:07 +0000335 switch (err) {
336 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 break;
Martin Panter84544c12016-07-23 03:02:07 +0000338 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000339 PyErr_SetString(PyExc_MemoryError,
340 "Out of memory while decompressing data");
341 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000342 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000343 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500344 zlib_error(state, zst, err, "while preparing to decompress data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000345 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000346 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000347
Jeremy Hylton9714f992001-10-16 21:19:45 +0000348 do {
Martin Panter84544c12016-07-23 03:02:07 +0000349 arrange_input_buffer(&zst, &ibuflen);
350 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000351
Martin Panter84544c12016-07-23 03:02:07 +0000352 do {
353 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
354 if (bufsize < 0) {
355 inflateEnd(&zst);
356 goto error;
357 }
358
359 Py_BEGIN_ALLOW_THREADS
360 err = inflate(&zst, flush);
361 Py_END_ALLOW_THREADS
362
363 switch (err) {
364 case Z_OK: /* fall through */
365 case Z_BUF_ERROR: /* fall through */
366 case Z_STREAM_END:
367 break;
368 case Z_MEM_ERROR:
369 inflateEnd(&zst);
370 PyErr_SetString(PyExc_MemoryError,
371 "Out of memory while decompressing data");
372 goto error;
373 default:
374 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500375 zlib_error(state, zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000376 goto error;
377 }
Martin Panter84544c12016-07-23 03:02:07 +0000378
379 } while (zst.avail_out == 0);
380
381 } while (err != Z_STREAM_END && ibuflen != 0);
382
383
384 if (err != Z_STREAM_END) {
385 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500386 zlib_error(state, zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000387 goto error;
388 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000389
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000390 err = inflateEnd(&zst);
391 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500392 zlib_error(state, zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000393 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000394 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000395
Martin Panter84544c12016-07-23 03:02:07 +0000396 if (_PyBytes_Resize(&RetVal, zst.next_out -
397 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000398 goto error;
399
Martin Panter84544c12016-07-23 03:02:07 +0000400 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000401
402 error:
Martin Panter84544c12016-07-23 03:02:07 +0000403 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000404 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000405}
406
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200407/*[clinic input]
408zlib.compressobj
409
410 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000411 The compression level (an integer in the range 0-9 or -1; default is
412 currently equivalent to 6). Higher compression levels are slower,
413 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200414 method: int(c_default="DEFLATED") = DEFLATED
415 The compression algorithm. If given, this must be DEFLATED.
416 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000417 +9 to +15: The base-two logarithm of the window size. Include a zlib
418 container.
419 -9 to -15: Generate a raw stream.
420 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200421 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
422 Controls the amount of memory used for internal compression state.
423 Valid values range from 1 to 9. Higher values result in higher memory
424 usage, faster compression, and smaller output.
425 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
426 Used to tune the compression algorithm. Possible values are
427 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
428 zdict: Py_buffer = None
429 The predefined compression dictionary - a sequence of bytes
430 containing subsequences that are likely to occur in the input data.
431
432Return a compressor object.
433[clinic start generated code]*/
434
Guido van Rossumfb221561997-04-29 15:38:09 +0000435static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300436zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400437 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300438/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000439{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500440 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200441 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100442 PyErr_SetString(PyExc_OverflowError,
443 "zdict length does not fit in an unsigned int");
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500444 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +0100445 }
446
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500447 compobject *self = newcompobject(state->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000448 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200449 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200450 self->zst.opaque = NULL;
451 self->zst.zalloc = PyZlib_Malloc;
452 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000453 self->zst.next_in = NULL;
454 self->zst.avail_in = 0;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500455 int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000456 switch (err) {
457 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200459 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200460 goto success;
461 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100462 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200463 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200464 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000465 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200466 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000467 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200468 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
469 goto error;
470 default:
471 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
472 goto error;
473 }
474 }
Martin Panter84544c12016-07-23 03:02:07 +0000475 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000476 PyErr_SetString(PyExc_MemoryError,
477 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200478 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000479 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000480 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200481 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500483 zlib_error(state, self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000485 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200486
487 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200488 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200489 success:
Martin Panter84544c12016-07-23 03:02:07 +0000490 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000491}
492
Martin Panter3f0ee832016-06-05 10:48:34 +0000493static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500494set_inflate_zdict(zlibstate *state, compobject *self)
Martin Panter3f0ee832016-06-05 10:48:34 +0000495{
496 Py_buffer zdict_buf;
Martin Panter3f0ee832016-06-05 10:48:34 +0000497 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
498 return -1;
499 }
500 if ((size_t)zdict_buf.len > UINT_MAX) {
501 PyErr_SetString(PyExc_OverflowError,
502 "zdict length does not fit in an unsigned int");
503 PyBuffer_Release(&zdict_buf);
504 return -1;
505 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500506 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000507 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000508 zdict_buf.buf, (unsigned int)zdict_buf.len);
509 PyBuffer_Release(&zdict_buf);
510 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500511 zlib_error(state, self->zst, err, "while setting zdict");
Martin Panter3f0ee832016-06-05 10:48:34 +0000512 return -1;
513 }
514 return 0;
515}
516
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200517/*[clinic input]
518zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200519
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200520 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000521 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200522 zdict: object(c_default="NULL") = b''
523 The predefined compression dictionary. This must be the same
524 dictionary as used by the compressor that produced the input data.
525
526Return a decompressor object.
527[clinic start generated code]*/
528
529static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300530zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
531/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200532{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500533 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200534
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200535 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
536 PyErr_SetString(PyExc_TypeError,
537 "zdict argument must support the buffer protocol");
538 return NULL;
539 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000540
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500541 compobject *self = newcompobject(state->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000542 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000543 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200544 self->zst.opaque = NULL;
545 self->zst.zalloc = PyZlib_Malloc;
546 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000547 self->zst.next_in = NULL;
548 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200549 if (zdict != NULL) {
550 Py_INCREF(zdict);
551 self->zdict = zdict;
552 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500553 int err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000554 switch (err) {
555 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000557 if (self->zdict != NULL && wbits < 0) {
558#ifdef AT_LEAST_ZLIB_1_2_2_1
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500559 if (set_inflate_zdict(state, self) < 0) {
Martin Panter3f0ee832016-06-05 10:48:34 +0000560 Py_DECREF(self);
561 return NULL;
562 }
563#else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500564 PyErr_Format(state->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000565 "zlib version %s does not allow raw inflate with dictionary",
566 ZLIB_VERSION);
567 Py_DECREF(self);
568 return NULL;
569#endif
570 }
Martin Panter84544c12016-07-23 03:02:07 +0000571 return (PyObject *)self;
572 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000573 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000574 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
575 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000576 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 Py_DECREF(self);
578 PyErr_SetString(PyExc_MemoryError,
579 "Can't allocate memory for decompression object");
580 return NULL;
581 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500582 zlib_error(state, self->zst, err, "while creating decompression object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 Py_DECREF(self);
584 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000585 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000586}
587
588static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000589Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000590{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100591 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000592 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000593 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000594 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200595 Py_XDECREF(self->zdict);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100596 PyObject_Free(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100597 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000598}
599
600static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000601Comp_dealloc(compobject *self)
602{
603 if (self->is_initialised)
604 deflateEnd(&self->zst);
605 Dealloc(self);
606}
607
608static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000609Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000610{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000611 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000612 inflateEnd(&self->zst);
613 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000614}
615
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200616/*[clinic input]
617zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000618
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500619 cls: defining_class
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200620 data: Py_buffer
621 Binary data to be compressed.
622 /
623
624Returns a bytes object containing compressed data.
625
626After calling this function, some of the input data may still
627be stored in internal buffers for later processing.
628Call the flush() method to clear these buffers.
629[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000630
Guido van Rossumfb221561997-04-29 15:38:09 +0000631static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500632zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
633 Py_buffer *data)
634/*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000635{
Martin Panter84544c12016-07-23 03:02:07 +0000636 PyObject *RetVal = NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500637 Py_ssize_t obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200638 int err;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500639 zlibstate *state = PyType_GetModuleState(cls);
640
Ma Lin93f41182021-04-27 16:37:11 +0800641 ENTER_ZLIB(self);
642
Martin Panter84544c12016-07-23 03:02:07 +0000643 self->zst.next_in = data->buf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500644 Py_ssize_t ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000645
Martin Panter84544c12016-07-23 03:02:07 +0000646 do {
647 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000648
Martin Panter84544c12016-07-23 03:02:07 +0000649 do {
650 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
651 if (obuflen < 0)
652 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000653
Martin Panter84544c12016-07-23 03:02:07 +0000654 Py_BEGIN_ALLOW_THREADS
655 err = deflate(&self->zst, Z_NO_FLUSH);
656 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000657
Martin Panter84544c12016-07-23 03:02:07 +0000658 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500659 zlib_error(state, self->zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000660 goto error;
661 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000662
Martin Panter84544c12016-07-23 03:02:07 +0000663 } while (self->zst.avail_out == 0);
664 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000665
Martin Panter84544c12016-07-23 03:02:07 +0000666 } while (ibuflen != 0);
667
668 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
669 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
670 goto success;
671
672 error:
673 Py_CLEAR(RetVal);
674 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000675 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000676 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000677}
678
Martin Panter84544c12016-07-23 03:02:07 +0000679/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100680 self->unused_data or self->unconsumed_tail, as appropriate. */
681static int
Martin Panter84544c12016-07-23 03:02:07 +0000682save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100683{
684 if (err == Z_STREAM_END) {
685 /* The end of the compressed data has been reached. Store the leftover
686 input data in self->unused_data. */
687 if (self->zst.avail_in > 0) {
688 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000689 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100690 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000691 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
692 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100693 PyErr_NoMemory();
694 return -1;
695 }
Martin Panter84544c12016-07-23 03:02:07 +0000696 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100697 new_data = PyBytes_FromStringAndSize(NULL, new_size);
698 if (new_data == NULL)
699 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200700 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100701 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200702 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000703 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300704 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100705 self->zst.avail_in = 0;
706 }
707 }
Martin Panter84544c12016-07-23 03:02:07 +0000708
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100709 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
710 /* This code handles two distinct cases:
711 1. Output limit was reached. Save leftover input in unconsumed_tail.
712 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000713 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100714 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000715 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100716 if (new_data == NULL)
717 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300718 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100719 }
Martin Panter84544c12016-07-23 03:02:07 +0000720
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100721 return 0;
722}
723
Larry Hastings61272b72014-01-07 12:41:53 -0800724/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800725zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700726
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500727 cls: defining_class
Larry Hastings31826802013-10-19 00:09:25 -0700728 data: Py_buffer
729 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300730 /
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300731 max_length: Py_ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700732 The maximum allowable length of the decompressed data.
733 Unconsumed input data will be stored in
734 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700735
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200736Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700737
738After calling this function, some of the input data may still be stored in
739internal buffers for later processing.
740Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800741[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700742
Larry Hastings31826802013-10-19 00:09:25 -0700743static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500744zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
745 Py_buffer *data, Py_ssize_t max_length)
746/*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/
Larry Hastings31826802013-10-19 00:09:25 -0700747{
Martin Panter84544c12016-07-23 03:02:07 +0000748 int err = Z_OK;
749 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200750 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000751
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500752 PyObject *module = PyType_GetModule(cls);
753 if (module == NULL)
754 return NULL;
755
756 zlibstate *state = get_zlib_state(module);
Martin Panter84544c12016-07-23 03:02:07 +0000757 if (max_length < 0) {
758 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700759 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000760 } else if (max_length == 0)
761 hard_limit = PY_SSIZE_T_MAX;
762 else
763 hard_limit = max_length;
764
Ma Lin93f41182021-04-27 16:37:11 +0800765 ENTER_ZLIB(self);
766
Martin Panter84544c12016-07-23 03:02:07 +0000767 self->zst.next_in = data->buf;
768 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000769
Jeremy Hylton9714f992001-10-16 21:19:45 +0000770 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000771 if (max_length && obuflen > max_length)
772 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000773
Martin Panter84544c12016-07-23 03:02:07 +0000774 do {
775 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000776
Martin Panter84544c12016-07-23 03:02:07 +0000777 do {
778 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
779 obuflen, hard_limit);
780 if (obuflen == -2) {
781 if (max_length > 0) {
782 goto save;
783 }
784 PyErr_NoMemory();
785 }
786 if (obuflen < 0) {
787 goto abort;
788 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000789
Martin Panter84544c12016-07-23 03:02:07 +0000790 Py_BEGIN_ALLOW_THREADS
791 err = inflate(&self->zst, Z_SYNC_FLUSH);
792 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100793
Martin Panter84544c12016-07-23 03:02:07 +0000794 switch (err) {
795 case Z_OK: /* fall through */
796 case Z_BUF_ERROR: /* fall through */
797 case Z_STREAM_END:
798 break;
799 default:
800 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500801 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +0000802 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500803 }
Martin Panter84544c12016-07-23 03:02:07 +0000804 else
805 break;
806 }
807 goto save;
808 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200809
Martin Panter84544c12016-07-23 03:02:07 +0000810 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000811
Martin Panter84544c12016-07-23 03:02:07 +0000812 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000813
Martin Panter84544c12016-07-23 03:02:07 +0000814 save:
815 if (save_unconsumed_input(self, data, err) < 0)
816 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000817
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000818 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100819 /* This is the logical place to call inflateEnd, but the old behaviour
820 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800821 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100822 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000823 /* We will only get Z_BUF_ERROR if the output buffer was full
824 but there wasn't more output when we tried again, so it is
825 not an error condition.
826 */
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500827 zlib_error(state, self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000828 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000829 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000830
Martin Panter84544c12016-07-23 03:02:07 +0000831 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
832 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
833 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000834
Martin Panter84544c12016-07-23 03:02:07 +0000835 abort:
836 Py_CLEAR(RetVal);
837 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800838 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000839 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000840}
841
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200842/*[clinic input]
843zlib.Compress.flush
844
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500845 cls: defining_class
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200846 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200847 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
848 If mode == Z_FINISH, the compressor object can no longer be
849 used after calling the flush() method. Otherwise, more data
850 can still be compressed.
851 /
852
853Return a bytes object containing any remaining compressed data.
854[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000855
Guido van Rossumfb221561997-04-29 15:38:09 +0000856static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500857zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
858/*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000859{
Victor Stinnere079edd2013-11-21 22:33:21 +0100860 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000861 Py_ssize_t length = DEF_BUF_SIZE;
862 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000863
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500864 zlibstate *state = PyType_GetModuleState(cls);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000865 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
866 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200867 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000868 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000869 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000870
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000871 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000872
Jeremy Hylton9714f992001-10-16 21:19:45 +0000873 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000874
Martin Panter84544c12016-07-23 03:02:07 +0000875 do {
876 length = arrange_output_buffer(&self->zst, &RetVal, length);
877 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200878 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000880 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000881
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000882 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000883 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000885
886 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500887 zlib_error(state, self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000888 Py_CLEAR(RetVal);
889 goto error;
890 }
891 } while (self->zst.avail_out == 0);
892 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000893
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200894 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000895 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200896 mode is Z_FINISH, but checking both for safety*/
897 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000898 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500900 zlib_error(state, self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000901 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 goto error;
903 }
904 else
905 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000906
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000907 /* We will only get Z_BUF_ERROR if the output buffer was full
908 but there wasn't more output when we tried again, so it is
909 not an error condition.
910 */
Martin Panter84544c12016-07-23 03:02:07 +0000911 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500912 zlib_error(state, self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000913 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000915 }
Tim Peters977e5402001-10-17 03:57:20 +0000916
Martin Panter84544c12016-07-23 03:02:07 +0000917 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
918 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200919 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000920
Tim Peters977e5402001-10-17 03:57:20 +0000921 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000922 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000923 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000924}
925
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000926#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700927
Larry Hastings61272b72014-01-07 12:41:53 -0800928/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800929zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700930
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500931 cls: defining_class
932
Larry Hastings31826802013-10-19 00:09:25 -0700933Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800934[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700935
Larry Hastings3cceb382014-01-04 11:09:09 -0800936static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500937zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
938/*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500940 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000941
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500942 compobject *retval = newcompobject(state->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943 if (!retval) return NULL;
944
945 /* Copy the zstream state
946 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
947 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800948 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500949 int err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000950 switch (err) {
951 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000952 break;
Martin Panter84544c12016-07-23 03:02:07 +0000953 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000954 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
955 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000956 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957 PyErr_SetString(PyExc_MemoryError,
958 "Can't allocate memory for compression object");
959 goto error;
960 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500961 zlib_error(state, self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962 goto error;
963 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800964 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300965 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800966 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300967 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800968 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300969 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800970 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000971
972 /* Mark it as being initialized */
973 retval->is_initialised = 1;
974
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800975 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976 return (PyObject *)retval;
977
978error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800979 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000980 Py_XDECREF(retval);
981 return NULL;
982}
983
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200984/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600985zlib.Compress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500986
987 cls: defining_class
988
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600989[clinic start generated code]*/
990
991static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500992zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
993/*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600994{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500995 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600996}
997
998/*[clinic input]
999zlib.Compress.__deepcopy__
1000
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001001 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001002 memo: object
1003 /
1004
1005[clinic start generated code]*/
1006
1007static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001008zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1009 PyObject *memo)
1010/*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001011{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001012 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001013}
1014
1015/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001016zlib.Decompress.copy
1017
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001018 cls: defining_class
1019
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001020Return a copy of the decompression object.
1021[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022
1023static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001024zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
1025/*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001027 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001028
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001029 compobject *retval = newcompobject(state->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 if (!retval) return NULL;
1031
1032 /* Copy the zstream state
1033 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1034 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001035 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001036 int err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001037 switch (err) {
1038 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039 break;
Martin Panter84544c12016-07-23 03:02:07 +00001040 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001041 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1042 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001043 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044 PyErr_SetString(PyExc_MemoryError,
1045 "Can't allocate memory for decompression object");
1046 goto error;
1047 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001048 zlib_error(state, self->zst, err, "while copying decompression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049 goto error;
1050 }
1051
1052 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001053 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001055 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001056 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001057 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001058 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001059
1060 /* Mark it as being initialized */
1061 retval->is_initialised = 1;
1062
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001063 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064 return (PyObject *)retval;
1065
1066error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001067 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068 Py_XDECREF(retval);
1069 return NULL;
1070}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001071
1072/*[clinic input]
1073zlib.Decompress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001074
1075 cls: defining_class
1076
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001077[clinic start generated code]*/
1078
1079static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001080zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
1081/*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001082{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001083 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001084}
1085
1086/*[clinic input]
1087zlib.Decompress.__deepcopy__
1088
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001089 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001090 memo: object
1091 /
1092
1093[clinic start generated code]*/
1094
1095static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001096zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1097 PyObject *memo)
1098/*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001099{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001100 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001101}
1102
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001103#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001104
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001105/*[clinic input]
1106zlib.Decompress.flush
1107
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001108 cls: defining_class
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001109 length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001110 the initial size of the output buffer.
1111 /
1112
1113Return a bytes object containing any remaining decompressed data.
1114[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001115
Guido van Rossumfb221561997-04-29 15:38:09 +00001116static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001117zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
1118 Py_ssize_t length)
1119/*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001120{
Martin Panter84544c12016-07-23 03:02:07 +00001121 int err, flush;
1122 Py_buffer data;
1123 PyObject *RetVal = NULL;
1124 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001125
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001126 PyObject *module = PyType_GetModule(cls);
1127 if (module == NULL) {
1128 return NULL;
1129 }
1130
1131 zlibstate *state = get_zlib_state(module);
1132
Martin Panter84544c12016-07-23 03:02:07 +00001133 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001134 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1135 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001136 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001137
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001138 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001139 return NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001140 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001141
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001142 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001143
Martin Panter84544c12016-07-23 03:02:07 +00001144 self->zst.next_in = data.buf;
1145 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001146
Martin Panter84544c12016-07-23 03:02:07 +00001147 do {
1148 arrange_input_buffer(&self->zst, &ibuflen);
1149 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001150
Martin Panter84544c12016-07-23 03:02:07 +00001151 do {
1152 length = arrange_output_buffer(&self->zst, &RetVal, length);
1153 if (length < 0)
1154 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001155
Martin Panter84544c12016-07-23 03:02:07 +00001156 Py_BEGIN_ALLOW_THREADS
1157 err = inflate(&self->zst, flush);
1158 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001159
Martin Panter84544c12016-07-23 03:02:07 +00001160 switch (err) {
1161 case Z_OK: /* fall through */
1162 case Z_BUF_ERROR: /* fall through */
1163 case Z_STREAM_END:
1164 break;
1165 default:
1166 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001167 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001168 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001169 }
Martin Panter84544c12016-07-23 03:02:07 +00001170 else
1171 break;
1172 }
1173 goto save;
1174 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001175
Martin Panter84544c12016-07-23 03:02:07 +00001176 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1177
1178 } while (err != Z_STREAM_END && ibuflen != 0);
1179
1180 save:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001181 if (save_unconsumed_input(self, &data, err) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001182 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001183 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001184
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001185 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001186 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001187 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001188 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001189 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001190 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001191 zlib_error(state, self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001192 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001193 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001194 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001195
Martin Panter84544c12016-07-23 03:02:07 +00001196 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001197 (Byte *)PyBytes_AS_STRING(RetVal)) == 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001198 goto success;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001199 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001200
Martin Panter84544c12016-07-23 03:02:07 +00001201 abort:
1202 Py_CLEAR(RetVal);
1203 success:
1204 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001205 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001206 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001207}
1208
Christian Heimes936e2f32014-01-27 01:06:57 +01001209#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001210
Guido van Rossumfb221561997-04-29 15:38:09 +00001211static PyMethodDef comp_methods[] =
1212{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001213 ZLIB_COMPRESS_COMPRESS_METHODDEF
1214 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001215 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001216 ZLIB_COMPRESS___COPY___METHODDEF
1217 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001218 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001219};
1220
1221static PyMethodDef Decomp_methods[] =
1222{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001223 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001224 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001225 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001226 ZLIB_DECOMPRESS___COPY___METHODDEF
1227 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001228 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001229};
1230
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001231#define COMP_OFF(x) offsetof(compobject, x)
1232static PyMemberDef Decomp_members[] = {
1233 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1234 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001235 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001236 {NULL},
1237};
Guido van Rossumfb221561997-04-29 15:38:09 +00001238
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001239/*[clinic input]
1240zlib.adler32
1241
1242 data: Py_buffer
1243 value: unsigned_int(bitwise=True) = 1
1244 Starting value of the checksum.
1245 /
1246
1247Compute an Adler-32 checksum of data.
1248
1249The returned checksum is an integer.
1250[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001251
Guido van Rossumfb221561997-04-29 15:38:09 +00001252static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001253zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1254/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001255{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001256 /* Releasing the GIL for very small buffers is inefficient
1257 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001258 if (data->len > 1024*5) {
1259 unsigned char *buf = data->buf;
1260 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001261
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001262 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001263 /* Avoid truncation of length for very large buffers. adler32() takes
1264 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001265 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001266 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001267 buf += (size_t) UINT_MAX;
1268 len -= (size_t) UINT_MAX;
1269 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001270 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001271 Py_END_ALLOW_THREADS
1272 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001273 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001274 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001275 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001276}
Tim Peters977e5402001-10-17 03:57:20 +00001277
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001278/*[clinic input]
1279zlib.crc32
1280
1281 data: Py_buffer
1282 value: unsigned_int(bitwise=True) = 0
1283 Starting value of the checksum.
1284 /
1285
1286Compute a CRC-32 checksum of data.
1287
1288The returned checksum is an integer.
1289[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001290
1291static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001292zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1293/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001294{
Martin v. Löwis423be952008-08-13 15:53:07 +00001295 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001296
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001297 /* Releasing the GIL for very small buffers is inefficient
1298 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001299 if (data->len > 1024*5) {
1300 unsigned char *buf = data->buf;
1301 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001302
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001303 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001304 /* Avoid truncation of length for very large buffers. crc32() takes
1305 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001306 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001307 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001308 buf += (size_t) UINT_MAX;
1309 len -= (size_t) UINT_MAX;
1310 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001311 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001312 Py_END_ALLOW_THREADS
1313 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001314 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001315 }
Christian Heimescc47b052008-03-25 14:56:36 +00001316 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001317}
Tim Peters977e5402001-10-17 03:57:20 +00001318
Guido van Rossumfb221561997-04-29 15:38:09 +00001319
1320static PyMethodDef zlib_methods[] =
1321{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001322 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001323 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001324 ZLIB_COMPRESSOBJ_METHODDEF
1325 ZLIB_CRC32_METHODDEF
1326 ZLIB_DECOMPRESS_METHODDEF
1327 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001328 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001329};
1330
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001331static PyType_Slot Comptype_slots[] = {
1332 {Py_tp_dealloc, Comp_dealloc},
1333 {Py_tp_methods, comp_methods},
1334 {0, 0},
1335};
1336
1337static PyType_Spec Comptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001338 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001339 sizeof(compobject),
1340 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001341 Py_TPFLAGS_DEFAULT,
1342 Comptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001343};
1344
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001345static PyType_Slot Decomptype_slots[] = {
1346 {Py_tp_dealloc, Decomp_dealloc},
1347 {Py_tp_methods, Decomp_methods},
1348 {Py_tp_members, Decomp_members},
1349 {0, 0},
1350};
1351
1352static PyType_Spec Decomptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001353 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001354 sizeof(compobject),
1355 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001356 Py_TPFLAGS_DEFAULT,
1357 Decomptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001358};
1359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001360PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001361"The functions in this module allow compression and decompression using the\n"
1362"zlib library, which is based on GNU zip.\n"
1363"\n"
1364"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001365"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001366"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001367"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001368"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001369"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001370"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001371"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001372"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001373"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001374
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001375static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001376zlib_clear(PyObject *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001377{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001378 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001379 Py_CLEAR(state->Comptype);
1380 Py_CLEAR(state->Decomptype);
1381 Py_CLEAR(state->ZlibError);
1382 return 0;
1383}
1384
1385static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001386zlib_traverse(PyObject *mod, visitproc visit, void *arg)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001387{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001388 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001389 Py_VISIT(state->Comptype);
1390 Py_VISIT(state->Decomptype);
1391 Py_VISIT(state->ZlibError);
1392 return 0;
1393}
1394
1395static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001396zlib_free(void *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001397{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001398 zlib_clear((PyObject *)mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001399}
1400
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001401static int
1402zlib_exec(PyObject *mod)
1403{
1404 zlibstate *state = get_zlib_state(mod);
1405
1406 state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1407 mod, &Comptype_spec, NULL);
1408 if (state->Comptype == NULL) {
1409 return -1;
1410 }
1411
1412 state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1413 mod, &Decomptype_spec, NULL);
1414 if (state->Decomptype == NULL) {
1415 return -1;
1416 }
1417
1418 state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1419 if (state->ZlibError == NULL) {
1420 return -1;
1421 }
1422
1423 Py_INCREF(state->ZlibError);
1424 if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
1425 Py_DECREF(state->ZlibError);
1426 return -1;
1427 }
1428
1429#define ZLIB_ADD_INT_MACRO(c) \
1430 do { \
1431 if ((PyModule_AddIntConstant(mod, #c, c)) < 0) { \
1432 return -1; \
1433 } \
1434 } while(0)
1435
1436 ZLIB_ADD_INT_MACRO(MAX_WBITS);
1437 ZLIB_ADD_INT_MACRO(DEFLATED);
1438 ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
1439 ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
1440 // compression levels
1441 ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
1442 ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
1443 ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
1444 ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
1445 // compression strategies
1446 ZLIB_ADD_INT_MACRO(Z_FILTERED);
1447 ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
1448#ifdef Z_RLE // 1.2.0.1
1449 ZLIB_ADD_INT_MACRO(Z_RLE);
1450#endif
1451#ifdef Z_FIXED // 1.2.2.2
1452 ZLIB_ADD_INT_MACRO(Z_FIXED);
1453#endif
1454 ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
1455 // allowed flush values
1456 ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
1457 ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
1458 ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
1459 ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
1460 ZLIB_ADD_INT_MACRO(Z_FINISH);
1461#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1462 ZLIB_ADD_INT_MACRO(Z_BLOCK);
1463#endif
1464#ifdef Z_TREES // 1.2.3.4, only for inflate
1465 ZLIB_ADD_INT_MACRO(Z_TREES);
1466#endif
1467 PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
1468 if (ver == NULL) {
1469 return -1;
1470 }
1471
1472 if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
1473 Py_DECREF(ver);
1474 return -1;
1475 }
1476
1477 ver = PyUnicode_FromString(zlibVersion());
1478 if (ver == NULL) {
1479 return -1;
1480 }
1481
1482 if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
1483 Py_DECREF(ver);
1484 return -1;
1485 }
1486
1487 if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
1488 return -1;
1489 }
1490 return 0;
1491}
1492
1493static PyModuleDef_Slot zlib_slots[] = {
1494 {Py_mod_exec, zlib_exec},
1495 {0, NULL}
1496};
1497
Martin v. Löwis1a214512008-06-11 05:26:20 +00001498static struct PyModuleDef zlibmodule = {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001499 PyModuleDef_HEAD_INIT,
1500 .m_name = "zlib",
1501 .m_doc = zlib_module_documentation,
1502 .m_size = sizeof(zlibstate),
1503 .m_methods = zlib_methods,
1504 .m_slots = zlib_slots,
1505 .m_traverse = zlib_traverse,
1506 .m_clear = zlib_clear,
1507 .m_free = zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001508};
1509
Mark Hammond62b1ab12002-07-23 06:31:15 +00001510PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001511PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001512{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001513 return PyModuleDef_Init(&zlibmodule);
Guido van Rossumfb221561997-04-29 15:38:09 +00001514}