blob: a537087d19d8359dadd4c29dfe6859976a6eeef1 [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
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020013#define ENTER_ZLIB(obj) \
14 Py_BEGIN_ALLOW_THREADS; \
15 PyThread_acquire_lock((obj)->lock, 1); \
16 Py_END_ALLOW_THREADS;
17#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018
Martin Panter3f0ee832016-06-05 10:48:34 +000019#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000020# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000021#endif
22
Guido van Rossumfb221561997-04-29 15:38:09 +000023/* The following parameters are copied from zutil.h, version 0.95 */
24#define DEFLATED 8
25#if MAX_MEM_LEVEL >= 8
26# define DEF_MEM_LEVEL 8
27#else
28# define DEF_MEM_LEVEL MAX_MEM_LEVEL
29#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000030
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020031/* Initial buffer size. */
32#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000033
Dino Viehlanda1ffad02019-09-10 11:27:03 +010034static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Dino Viehlanda1ffad02019-09-10 11:27:03 +010036typedef struct {
37 PyTypeObject *Comptype;
38 PyTypeObject *Decomptype;
39 PyObject *ZlibError;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050040} zlibstate;
Dino Viehlanda1ffad02019-09-10 11:27:03 +010041
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050042static inline zlibstate*
Hai Shif707d942020-03-16 21:15:01 +080043get_zlib_state(PyObject *module)
44{
45 void *state = PyModule_GetState(module);
46 assert(state != NULL);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050047 return (zlibstate *)state;
Hai Shif707d942020-03-16 21:15:01 +080048}
49
Tim Peters977e5402001-10-17 03:57:20 +000050typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000051{
Jeremy Hylton9714f992001-10-16 21:19:45 +000052 PyObject_HEAD
53 z_stream zst;
54 PyObject *unused_data;
55 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020056 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000057 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020058 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020059 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000060} compobject;
61
Jeremy Hylton0965e082001-10-16 21:56:09 +000062static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050063zlib_error(zlibstate *state, z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000064{
Nadeem Vawda524148a2011-08-28 11:26:46 +020065 const char *zmsg = Z_NULL;
66 /* In case of a version mismatch, zst.msg won't be initialized.
67 Check for this case first, before looking at zst.msg. */
68 if (err == Z_VERSION_ERROR)
69 zmsg = "library version mismatch";
70 if (zmsg == Z_NULL)
71 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000072 if (zmsg == Z_NULL) {
73 switch (err) {
74 case Z_BUF_ERROR:
75 zmsg = "incomplete or truncated stream";
76 break;
77 case Z_STREAM_ERROR:
78 zmsg = "inconsistent stream state";
79 break;
80 case Z_DATA_ERROR:
81 zmsg = "invalid input data";
82 break;
83 }
84 }
85 if (zmsg == Z_NULL)
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050086 PyErr_Format(state->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000087 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -050088 PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000089}
90
Larry Hastings61272b72014-01-07 12:41:53 -080091/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080092module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080093class zlib.Compress "compobject *" "&Comptype"
94class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080095[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030096/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080097
Guido van Rossumfb221561997-04-29 15:38:09 +000098static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000099newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000100{
Tim Peters977e5402001-10-17 03:57:20 +0000101 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 self = PyObject_New(compobject, type);
103 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000104 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200105 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000106 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200107 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000108 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000109 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000110 Py_DECREF(self);
111 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000112 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000113 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000115 Py_DECREF(self);
116 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000117 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000118 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200119 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000120 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200121 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
122 return NULL;
123 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000124 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000125}
126
Victor Stinner5064a522013-07-07 16:50:27 +0200127static void*
128PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
129{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300130 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200131 return NULL;
132 /* PyMem_Malloc() cannot be used: the GIL is not held when
133 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300134 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200135}
136
137static void
138PyZlib_Free(voidpf ctx, void *ptr)
139{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200140 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200141}
142
Martin Panter84544c12016-07-23 03:02:07 +0000143static void
144arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
145{
Segev Finer679b5662017-07-27 01:17:57 +0300146 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000147 *remains -= zst->avail_in;
148}
149
150static Py_ssize_t
151arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
152 Py_ssize_t length,
153 Py_ssize_t max_length)
154{
155 Py_ssize_t occupied;
156
157 if (*buffer == NULL) {
158 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
159 return -1;
160 occupied = 0;
161 }
162 else {
163 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
164
165 if (length == occupied) {
166 Py_ssize_t new_length;
167 assert(length <= max_length);
168 /* can not scale the buffer over max_length */
169 if (length == max_length)
170 return -2;
171 if (length <= (max_length >> 1))
172 new_length = length << 1;
173 else
174 new_length = max_length;
175 if (_PyBytes_Resize(buffer, new_length) < 0)
176 return -1;
177 length = new_length;
178 }
179 }
180
Segev Finer679b5662017-07-27 01:17:57 +0300181 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000182 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
183
184 return length;
185}
186
187static Py_ssize_t
188arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
189{
190 Py_ssize_t ret;
191
192 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
193 PY_SSIZE_T_MAX);
194 if (ret == -2)
195 PyErr_NoMemory();
196
197 return ret;
198}
199
Larry Hastings61272b72014-01-07 12:41:53 -0800200/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800201zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200202
Martin Panter1fe0d132016-02-10 10:06:36 +0000203 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800204 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300205 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200206 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000207 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800208
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200209Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800210[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800211
Guido van Rossumfb221561997-04-29 15:38:09 +0000212static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300213zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
214/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000215{
Martin Panter84544c12016-07-23 03:02:07 +0000216 PyObject *RetVal = NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500217 Py_ssize_t obuflen = DEF_BUF_SIZE;
218 int flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000219 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000220
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500221 zlibstate *state = get_zlib_state(module);
222
223 Byte *ibuf = data->buf;
224 Py_ssize_t ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000225
Victor Stinner5064a522013-07-07 16:50:27 +0200226 zst.opaque = NULL;
227 zst.zalloc = PyZlib_Malloc;
228 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000229 zst.next_in = ibuf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500230 int err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000231
Martin Panter84544c12016-07-23 03:02:07 +0000232 switch (err) {
233 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 break;
Martin Panter84544c12016-07-23 03:02:07 +0000235 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 PyErr_SetString(PyExc_MemoryError,
237 "Out of memory while compressing data");
238 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000239 case Z_STREAM_ERROR:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500240 PyErr_SetString(state->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000241 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000242 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000243 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500244 zlib_error(state, zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000246 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000247
Martin Panter84544c12016-07-23 03:02:07 +0000248 do {
249 arrange_input_buffer(&zst, &ibuflen);
250 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000251
Martin Panter84544c12016-07-23 03:02:07 +0000252 do {
253 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
254 if (obuflen < 0) {
255 deflateEnd(&zst);
256 goto error;
257 }
258
259 Py_BEGIN_ALLOW_THREADS
260 err = deflate(&zst, flush);
261 Py_END_ALLOW_THREADS
262
263 if (err == Z_STREAM_ERROR) {
264 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500265 zlib_error(state, zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000266 goto error;
267 }
268
269 } while (zst.avail_out == 0);
270 assert(zst.avail_in == 0);
271
272 } while (flush != Z_FINISH);
273 assert(err == Z_STREAM_END);
274
275 err = deflateEnd(&zst);
276 if (err == Z_OK) {
277 if (_PyBytes_Resize(&RetVal, zst.next_out -
278 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
279 goto error;
280 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000281 }
Tim Peters977e5402001-10-17 03:57:20 +0000282 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500283 zlib_error(state, zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000284 error:
Martin Panter84544c12016-07-23 03:02:07 +0000285 Py_XDECREF(RetVal);
286 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000287}
288
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200289/*[clinic input]
290zlib.decompress
291
292 data: Py_buffer
293 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300294 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200295 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000296 The window buffer size and container format.
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300297 bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200298 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200299
300Returns a bytes object containing the uncompressed data.
301[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000302
Guido van Rossumfb221561997-04-29 15:38:09 +0000303static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300304zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000305 Py_ssize_t bufsize)
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300306/*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000307{
Martin Panter84544c12016-07-23 03:02:07 +0000308 PyObject *RetVal = NULL;
309 Byte *ibuf;
310 Py_ssize_t ibuflen;
311 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000312 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000313
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500314 zlibstate *state = get_zlib_state(module);
315
Martin Panter84544c12016-07-23 03:02:07 +0000316 if (bufsize < 0) {
317 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
318 return NULL;
319 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100320 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000321 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000322
Martin Panter84544c12016-07-23 03:02:07 +0000323 ibuf = data->buf;
324 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000325
Victor Stinner5064a522013-07-07 16:50:27 +0200326 zst.opaque = NULL;
327 zst.zalloc = PyZlib_Malloc;
328 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000329 zst.avail_in = 0;
330 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200331 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000332
Martin Panter84544c12016-07-23 03:02:07 +0000333 switch (err) {
334 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 break;
Martin Panter84544c12016-07-23 03:02:07 +0000336 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 PyErr_SetString(PyExc_MemoryError,
338 "Out of memory while decompressing data");
339 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000340 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000341 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500342 zlib_error(state, zst, err, "while preparing to decompress data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000343 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000344 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000345
Jeremy Hylton9714f992001-10-16 21:19:45 +0000346 do {
Martin Panter84544c12016-07-23 03:02:07 +0000347 arrange_input_buffer(&zst, &ibuflen);
348 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000349
Martin Panter84544c12016-07-23 03:02:07 +0000350 do {
351 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
352 if (bufsize < 0) {
353 inflateEnd(&zst);
354 goto error;
355 }
356
357 Py_BEGIN_ALLOW_THREADS
358 err = inflate(&zst, flush);
359 Py_END_ALLOW_THREADS
360
361 switch (err) {
362 case Z_OK: /* fall through */
363 case Z_BUF_ERROR: /* fall through */
364 case Z_STREAM_END:
365 break;
366 case Z_MEM_ERROR:
367 inflateEnd(&zst);
368 PyErr_SetString(PyExc_MemoryError,
369 "Out of memory while decompressing data");
370 goto error;
371 default:
372 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500373 zlib_error(state, zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000374 goto error;
375 }
Martin Panter84544c12016-07-23 03:02:07 +0000376
377 } while (zst.avail_out == 0);
378
379 } while (err != Z_STREAM_END && ibuflen != 0);
380
381
382 if (err != Z_STREAM_END) {
383 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500384 zlib_error(state, zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000385 goto error;
386 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000387
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000388 err = inflateEnd(&zst);
389 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500390 zlib_error(state, zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000391 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000392 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000393
Martin Panter84544c12016-07-23 03:02:07 +0000394 if (_PyBytes_Resize(&RetVal, zst.next_out -
395 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000396 goto error;
397
Martin Panter84544c12016-07-23 03:02:07 +0000398 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000399
400 error:
Martin Panter84544c12016-07-23 03:02:07 +0000401 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000402 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000403}
404
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200405/*[clinic input]
406zlib.compressobj
407
408 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000409 The compression level (an integer in the range 0-9 or -1; default is
410 currently equivalent to 6). Higher compression levels are slower,
411 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200412 method: int(c_default="DEFLATED") = DEFLATED
413 The compression algorithm. If given, this must be DEFLATED.
414 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000415 +9 to +15: The base-two logarithm of the window size. Include a zlib
416 container.
417 -9 to -15: Generate a raw stream.
418 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200419 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
420 Controls the amount of memory used for internal compression state.
421 Valid values range from 1 to 9. Higher values result in higher memory
422 usage, faster compression, and smaller output.
423 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
424 Used to tune the compression algorithm. Possible values are
425 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
426 zdict: Py_buffer = None
427 The predefined compression dictionary - a sequence of bytes
428 containing subsequences that are likely to occur in the input data.
429
430Return a compressor object.
431[clinic start generated code]*/
432
Guido van Rossumfb221561997-04-29 15:38:09 +0000433static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300434zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400435 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300436/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000437{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500438 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200439 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100440 PyErr_SetString(PyExc_OverflowError,
441 "zdict length does not fit in an unsigned int");
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500442 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +0100443 }
444
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500445 compobject *self = newcompobject(state->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000446 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200447 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200448 self->zst.opaque = NULL;
449 self->zst.zalloc = PyZlib_Malloc;
450 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000451 self->zst.next_in = NULL;
452 self->zst.avail_in = 0;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500453 int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000454 switch (err) {
455 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000456 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200457 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200458 goto success;
459 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100460 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200461 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200462 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000463 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200464 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000465 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200466 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
467 goto error;
468 default:
469 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
470 goto error;
471 }
472 }
Martin Panter84544c12016-07-23 03:02:07 +0000473 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000474 PyErr_SetString(PyExc_MemoryError,
475 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200476 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000477 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000478 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200479 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000480 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500481 zlib_error(state, self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200482 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000483 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484
485 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200486 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200487 success:
Martin Panter84544c12016-07-23 03:02:07 +0000488 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000489}
490
Martin Panter3f0ee832016-06-05 10:48:34 +0000491static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500492set_inflate_zdict(zlibstate *state, compobject *self)
Martin Panter3f0ee832016-06-05 10:48:34 +0000493{
494 Py_buffer zdict_buf;
Martin Panter3f0ee832016-06-05 10:48:34 +0000495 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
496 return -1;
497 }
498 if ((size_t)zdict_buf.len > UINT_MAX) {
499 PyErr_SetString(PyExc_OverflowError,
500 "zdict length does not fit in an unsigned int");
501 PyBuffer_Release(&zdict_buf);
502 return -1;
503 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500504 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000505 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000506 zdict_buf.buf, (unsigned int)zdict_buf.len);
507 PyBuffer_Release(&zdict_buf);
508 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500509 zlib_error(state, self->zst, err, "while setting zdict");
Martin Panter3f0ee832016-06-05 10:48:34 +0000510 return -1;
511 }
512 return 0;
513}
514
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200515/*[clinic input]
516zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200517
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200518 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000519 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200520 zdict: object(c_default="NULL") = b''
521 The predefined compression dictionary. This must be the same
522 dictionary as used by the compressor that produced the input data.
523
524Return a decompressor object.
525[clinic start generated code]*/
526
527static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300528zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
529/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200530{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500531 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200532
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200533 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
534 PyErr_SetString(PyExc_TypeError,
535 "zdict argument must support the buffer protocol");
536 return NULL;
537 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000538
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500539 compobject *self = newcompobject(state->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000540 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000541 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200542 self->zst.opaque = NULL;
543 self->zst.zalloc = PyZlib_Malloc;
544 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000545 self->zst.next_in = NULL;
546 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200547 if (zdict != NULL) {
548 Py_INCREF(zdict);
549 self->zdict = zdict;
550 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500551 int err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000552 switch (err) {
553 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000555 if (self->zdict != NULL && wbits < 0) {
556#ifdef AT_LEAST_ZLIB_1_2_2_1
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500557 if (set_inflate_zdict(state, self) < 0) {
Martin Panter3f0ee832016-06-05 10:48:34 +0000558 Py_DECREF(self);
559 return NULL;
560 }
561#else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500562 PyErr_Format(state->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000563 "zlib version %s does not allow raw inflate with dictionary",
564 ZLIB_VERSION);
565 Py_DECREF(self);
566 return NULL;
567#endif
568 }
Martin Panter84544c12016-07-23 03:02:07 +0000569 return (PyObject *)self;
570 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000571 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000572 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
573 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000574 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 Py_DECREF(self);
576 PyErr_SetString(PyExc_MemoryError,
577 "Can't allocate memory for decompression object");
578 return NULL;
579 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500580 zlib_error(state, self->zst, err, "while creating decompression object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000581 Py_DECREF(self);
582 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000583 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000584}
585
586static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000587Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000588{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100589 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000590 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000591 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000592 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200593 Py_XDECREF(self->zdict);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100594 PyObject_Free(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100595 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000596}
597
598static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000599Comp_dealloc(compobject *self)
600{
601 if (self->is_initialised)
602 deflateEnd(&self->zst);
603 Dealloc(self);
604}
605
606static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000607Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000608{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000609 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000610 inflateEnd(&self->zst);
611 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000612}
613
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200614/*[clinic input]
615zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000616
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500617 cls: defining_class
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200618 data: Py_buffer
619 Binary data to be compressed.
620 /
621
622Returns a bytes object containing compressed data.
623
624After calling this function, some of the input data may still
625be stored in internal buffers for later processing.
626Call the flush() method to clear these buffers.
627[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000628
Guido van Rossumfb221561997-04-29 15:38:09 +0000629static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500630zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
631 Py_buffer *data)
632/*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000633{
Martin Panter84544c12016-07-23 03:02:07 +0000634 PyObject *RetVal = NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500635 Py_ssize_t obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200636 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000637
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500638 zlibstate *state = PyType_GetModuleState(cls);
639
Martin Panter84544c12016-07-23 03:02:07 +0000640 self->zst.next_in = data->buf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500641 Py_ssize_t ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000642
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000643 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000644
Martin Panter84544c12016-07-23 03:02:07 +0000645 do {
646 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647
Martin Panter84544c12016-07-23 03:02:07 +0000648 do {
649 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
650 if (obuflen < 0)
651 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000652
Martin Panter84544c12016-07-23 03:02:07 +0000653 Py_BEGIN_ALLOW_THREADS
654 err = deflate(&self->zst, Z_NO_FLUSH);
655 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000656
Martin Panter84544c12016-07-23 03:02:07 +0000657 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500658 zlib_error(state, self->zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000659 goto error;
660 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000661
Martin Panter84544c12016-07-23 03:02:07 +0000662 } while (self->zst.avail_out == 0);
663 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000664
Martin Panter84544c12016-07-23 03:02:07 +0000665 } while (ibuflen != 0);
666
667 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
668 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
669 goto success;
670
671 error:
672 Py_CLEAR(RetVal);
673 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000674 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000675 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000676}
677
Martin Panter84544c12016-07-23 03:02:07 +0000678/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100679 self->unused_data or self->unconsumed_tail, as appropriate. */
680static int
Martin Panter84544c12016-07-23 03:02:07 +0000681save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100682{
683 if (err == Z_STREAM_END) {
684 /* The end of the compressed data has been reached. Store the leftover
685 input data in self->unused_data. */
686 if (self->zst.avail_in > 0) {
687 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000688 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100689 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000690 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
691 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100692 PyErr_NoMemory();
693 return -1;
694 }
Martin Panter84544c12016-07-23 03:02:07 +0000695 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100696 new_data = PyBytes_FromStringAndSize(NULL, new_size);
697 if (new_data == NULL)
698 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200699 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100700 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200701 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000702 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300703 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100704 self->zst.avail_in = 0;
705 }
706 }
Martin Panter84544c12016-07-23 03:02:07 +0000707
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100708 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
709 /* This code handles two distinct cases:
710 1. Output limit was reached. Save leftover input in unconsumed_tail.
711 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000712 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100713 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000714 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100715 if (new_data == NULL)
716 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300717 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100718 }
Martin Panter84544c12016-07-23 03:02:07 +0000719
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100720 return 0;
721}
722
Larry Hastings61272b72014-01-07 12:41:53 -0800723/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800724zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700725
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500726 cls: defining_class
Larry Hastings31826802013-10-19 00:09:25 -0700727 data: Py_buffer
728 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300729 /
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300730 max_length: Py_ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700731 The maximum allowable length of the decompressed data.
732 Unconsumed input data will be stored in
733 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700734
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200735Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700736
737After calling this function, some of the input data may still be stored in
738internal buffers for later processing.
739Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800740[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700741
Larry Hastings31826802013-10-19 00:09:25 -0700742static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500743zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
744 Py_buffer *data, Py_ssize_t max_length)
745/*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/
Larry Hastings31826802013-10-19 00:09:25 -0700746{
Martin Panter84544c12016-07-23 03:02:07 +0000747 int err = Z_OK;
748 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200749 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000750
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500751 PyObject *module = PyType_GetModule(cls);
752 if (module == NULL)
753 return NULL;
754
755 zlibstate *state = get_zlib_state(module);
Martin Panter84544c12016-07-23 03:02:07 +0000756 if (max_length < 0) {
757 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700758 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000759 } else if (max_length == 0)
760 hard_limit = PY_SSIZE_T_MAX;
761 else
762 hard_limit = max_length;
763
764 self->zst.next_in = data->buf;
765 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000766
Jeremy Hylton9714f992001-10-16 21:19:45 +0000767 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000768 if (max_length && obuflen > max_length)
769 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000770
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800771 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000772
Martin Panter84544c12016-07-23 03:02:07 +0000773 do {
774 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000775
Martin Panter84544c12016-07-23 03:02:07 +0000776 do {
777 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
778 obuflen, hard_limit);
779 if (obuflen == -2) {
780 if (max_length > 0) {
781 goto save;
782 }
783 PyErr_NoMemory();
784 }
785 if (obuflen < 0) {
786 goto abort;
787 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000788
Martin Panter84544c12016-07-23 03:02:07 +0000789 Py_BEGIN_ALLOW_THREADS
790 err = inflate(&self->zst, Z_SYNC_FLUSH);
791 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100792
Martin Panter84544c12016-07-23 03:02:07 +0000793 switch (err) {
794 case Z_OK: /* fall through */
795 case Z_BUF_ERROR: /* fall through */
796 case Z_STREAM_END:
797 break;
798 default:
799 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500800 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +0000801 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500802 }
Martin Panter84544c12016-07-23 03:02:07 +0000803 else
804 break;
805 }
806 goto save;
807 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200808
Martin Panter84544c12016-07-23 03:02:07 +0000809 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000810
Martin Panter84544c12016-07-23 03:02:07 +0000811 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000812
Martin Panter84544c12016-07-23 03:02:07 +0000813 save:
814 if (save_unconsumed_input(self, data, err) < 0)
815 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000816
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000817 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100818 /* This is the logical place to call inflateEnd, but the old behaviour
819 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800820 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100821 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000822 /* We will only get Z_BUF_ERROR if the output buffer was full
823 but there wasn't more output when we tried again, so it is
824 not an error condition.
825 */
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500826 zlib_error(state, self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000827 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000828 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000829
Martin Panter84544c12016-07-23 03:02:07 +0000830 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
831 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
832 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000833
Martin Panter84544c12016-07-23 03:02:07 +0000834 abort:
835 Py_CLEAR(RetVal);
836 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800837 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000838 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000839}
840
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200841/*[clinic input]
842zlib.Compress.flush
843
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500844 cls: defining_class
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200845 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200846 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
847 If mode == Z_FINISH, the compressor object can no longer be
848 used after calling the flush() method. Otherwise, more data
849 can still be compressed.
850 /
851
852Return a bytes object containing any remaining compressed data.
853[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000854
Guido van Rossumfb221561997-04-29 15:38:09 +0000855static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500856zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
857/*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000858{
Victor Stinnere079edd2013-11-21 22:33:21 +0100859 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000860 Py_ssize_t length = DEF_BUF_SIZE;
861 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000862
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500863 zlibstate *state = PyType_GetModuleState(cls);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000864 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
865 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200866 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000867 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000868 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000869
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000870 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000871
Jeremy Hylton9714f992001-10-16 21:19:45 +0000872 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000873
Martin Panter84544c12016-07-23 03:02:07 +0000874 do {
875 length = arrange_output_buffer(&self->zst, &RetVal, length);
876 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200877 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000879 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000880
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000882 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000884
885 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500886 zlib_error(state, self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000887 Py_CLEAR(RetVal);
888 goto error;
889 }
890 } while (self->zst.avail_out == 0);
891 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000892
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200893 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000894 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200895 mode is Z_FINISH, but checking both for safety*/
896 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000897 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500899 zlib_error(state, self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000900 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000901 goto error;
902 }
903 else
904 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000905
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000906 /* We will only get Z_BUF_ERROR if the output buffer was full
907 but there wasn't more output when we tried again, so it is
908 not an error condition.
909 */
Martin Panter84544c12016-07-23 03:02:07 +0000910 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500911 zlib_error(state, self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000912 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000913 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000914 }
Tim Peters977e5402001-10-17 03:57:20 +0000915
Martin Panter84544c12016-07-23 03:02:07 +0000916 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
917 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200918 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000919
Tim Peters977e5402001-10-17 03:57:20 +0000920 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000921 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000922 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000923}
924
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000925#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700926
Larry Hastings61272b72014-01-07 12:41:53 -0800927/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800928zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700929
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500930 cls: defining_class
931
Larry Hastings31826802013-10-19 00:09:25 -0700932Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800933[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700934
Larry Hastings3cceb382014-01-04 11:09:09 -0800935static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500936zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
937/*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000938{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500939 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500941 compobject *retval = newcompobject(state->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000942 if (!retval) return NULL;
943
944 /* Copy the zstream state
945 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
946 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800947 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500948 int err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000949 switch (err) {
950 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 break;
Martin Panter84544c12016-07-23 03:02:07 +0000952 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
954 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000955 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 PyErr_SetString(PyExc_MemoryError,
957 "Can't allocate memory for compression object");
958 goto error;
959 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500960 zlib_error(state, self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961 goto error;
962 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800963 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300964 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800965 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300966 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800967 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300968 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800969 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970
971 /* Mark it as being initialized */
972 retval->is_initialised = 1;
973
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800974 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000975 return (PyObject *)retval;
976
977error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800978 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979 Py_XDECREF(retval);
980 return NULL;
981}
982
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200983/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600984zlib.Compress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500985
986 cls: defining_class
987
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600988[clinic start generated code]*/
989
990static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500991zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
992/*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600993{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500994 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600995}
996
997/*[clinic input]
998zlib.Compress.__deepcopy__
999
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001000 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001001 memo: object
1002 /
1003
1004[clinic start generated code]*/
1005
1006static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001007zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1008 PyObject *memo)
1009/*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001010{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001011 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001012}
1013
1014/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001015zlib.Decompress.copy
1016
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001017 cls: defining_class
1018
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001019Return a copy of the decompression object.
1020[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001021
1022static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001023zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
1024/*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001026 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001028 compobject *retval = newcompobject(state->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001029 if (!retval) return NULL;
1030
1031 /* Copy the zstream state
1032 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1033 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001034 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001035 int err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001036 switch (err) {
1037 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038 break;
Martin Panter84544c12016-07-23 03:02:07 +00001039 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1041 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001042 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043 PyErr_SetString(PyExc_MemoryError,
1044 "Can't allocate memory for decompression object");
1045 goto error;
1046 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001047 zlib_error(state, self->zst, err, "while copying decompression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001048 goto error;
1049 }
1050
1051 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001052 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001054 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001055 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001056 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001057 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058
1059 /* Mark it as being initialized */
1060 retval->is_initialised = 1;
1061
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001062 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063 return (PyObject *)retval;
1064
1065error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001066 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 Py_XDECREF(retval);
1068 return NULL;
1069}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001070
1071/*[clinic input]
1072zlib.Decompress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001073
1074 cls: defining_class
1075
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001076[clinic start generated code]*/
1077
1078static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001079zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
1080/*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001081{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001082 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001083}
1084
1085/*[clinic input]
1086zlib.Decompress.__deepcopy__
1087
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001088 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001089 memo: object
1090 /
1091
1092[clinic start generated code]*/
1093
1094static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001095zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1096 PyObject *memo)
1097/*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001098{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001099 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001100}
1101
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001102#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001103
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001104/*[clinic input]
1105zlib.Decompress.flush
1106
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001107 cls: defining_class
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001108 length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001109 the initial size of the output buffer.
1110 /
1111
1112Return a bytes object containing any remaining decompressed data.
1113[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001114
Guido van Rossumfb221561997-04-29 15:38:09 +00001115static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001116zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
1117 Py_ssize_t length)
1118/*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001119{
Martin Panter84544c12016-07-23 03:02:07 +00001120 int err, flush;
1121 Py_buffer data;
1122 PyObject *RetVal = NULL;
1123 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001124
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001125 PyObject *module = PyType_GetModule(cls);
1126 if (module == NULL) {
1127 return NULL;
1128 }
1129
1130 zlibstate *state = get_zlib_state(module);
1131
Martin Panter84544c12016-07-23 03:02:07 +00001132 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001133 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1134 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001135 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001136
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001137 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001138 return NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001139 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001140
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001141 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001142
Martin Panter84544c12016-07-23 03:02:07 +00001143 self->zst.next_in = data.buf;
1144 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001145
Martin Panter84544c12016-07-23 03:02:07 +00001146 do {
1147 arrange_input_buffer(&self->zst, &ibuflen);
1148 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001149
Martin Panter84544c12016-07-23 03:02:07 +00001150 do {
1151 length = arrange_output_buffer(&self->zst, &RetVal, length);
1152 if (length < 0)
1153 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001154
Martin Panter84544c12016-07-23 03:02:07 +00001155 Py_BEGIN_ALLOW_THREADS
1156 err = inflate(&self->zst, flush);
1157 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001158
Martin Panter84544c12016-07-23 03:02:07 +00001159 switch (err) {
1160 case Z_OK: /* fall through */
1161 case Z_BUF_ERROR: /* fall through */
1162 case Z_STREAM_END:
1163 break;
1164 default:
1165 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001166 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001167 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001168 }
Martin Panter84544c12016-07-23 03:02:07 +00001169 else
1170 break;
1171 }
1172 goto save;
1173 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001174
Martin Panter84544c12016-07-23 03:02:07 +00001175 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1176
1177 } while (err != Z_STREAM_END && ibuflen != 0);
1178
1179 save:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001180 if (save_unconsumed_input(self, &data, err) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001181 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001182 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001183
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001184 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001185 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001186 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001187 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001188 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001189 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001190 zlib_error(state, self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001191 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001192 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001193 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001194
Martin Panter84544c12016-07-23 03:02:07 +00001195 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001196 (Byte *)PyBytes_AS_STRING(RetVal)) == 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001197 goto success;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001198 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001199
Martin Panter84544c12016-07-23 03:02:07 +00001200 abort:
1201 Py_CLEAR(RetVal);
1202 success:
1203 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001204 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001205 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001206}
1207
Christian Heimes936e2f32014-01-27 01:06:57 +01001208#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001209
Guido van Rossumfb221561997-04-29 15:38:09 +00001210static PyMethodDef comp_methods[] =
1211{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001212 ZLIB_COMPRESS_COMPRESS_METHODDEF
1213 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001214 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001215 ZLIB_COMPRESS___COPY___METHODDEF
1216 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001217 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001218};
1219
1220static PyMethodDef Decomp_methods[] =
1221{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001222 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001223 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001224 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001225 ZLIB_DECOMPRESS___COPY___METHODDEF
1226 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001227 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001228};
1229
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001230#define COMP_OFF(x) offsetof(compobject, x)
1231static PyMemberDef Decomp_members[] = {
1232 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1233 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001234 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001235 {NULL},
1236};
Guido van Rossumfb221561997-04-29 15:38:09 +00001237
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001238/*[clinic input]
1239zlib.adler32
1240
1241 data: Py_buffer
1242 value: unsigned_int(bitwise=True) = 1
1243 Starting value of the checksum.
1244 /
1245
1246Compute an Adler-32 checksum of data.
1247
1248The returned checksum is an integer.
1249[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001250
Guido van Rossumfb221561997-04-29 15:38:09 +00001251static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001252zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1253/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001254{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001255 /* Releasing the GIL for very small buffers is inefficient
1256 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001257 if (data->len > 1024*5) {
1258 unsigned char *buf = data->buf;
1259 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001260
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001261 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001262 /* Avoid truncation of length for very large buffers. adler32() takes
1263 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001264 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001265 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001266 buf += (size_t) UINT_MAX;
1267 len -= (size_t) UINT_MAX;
1268 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001269 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001270 Py_END_ALLOW_THREADS
1271 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001272 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001273 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001274 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001275}
Tim Peters977e5402001-10-17 03:57:20 +00001276
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001277/*[clinic input]
1278zlib.crc32
1279
1280 data: Py_buffer
1281 value: unsigned_int(bitwise=True) = 0
1282 Starting value of the checksum.
1283 /
1284
1285Compute a CRC-32 checksum of data.
1286
1287The returned checksum is an integer.
1288[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001289
1290static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001291zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1292/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001293{
Martin v. Löwis423be952008-08-13 15:53:07 +00001294 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001295
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001296 /* Releasing the GIL for very small buffers is inefficient
1297 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001298 if (data->len > 1024*5) {
1299 unsigned char *buf = data->buf;
1300 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001301
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001302 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001303 /* Avoid truncation of length for very large buffers. crc32() takes
1304 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001305 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001306 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001307 buf += (size_t) UINT_MAX;
1308 len -= (size_t) UINT_MAX;
1309 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001310 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001311 Py_END_ALLOW_THREADS
1312 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001313 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001314 }
Christian Heimescc47b052008-03-25 14:56:36 +00001315 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001316}
Tim Peters977e5402001-10-17 03:57:20 +00001317
Guido van Rossumfb221561997-04-29 15:38:09 +00001318
1319static PyMethodDef zlib_methods[] =
1320{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001321 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001322 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001323 ZLIB_COMPRESSOBJ_METHODDEF
1324 ZLIB_CRC32_METHODDEF
1325 ZLIB_DECOMPRESS_METHODDEF
1326 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001327 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001328};
1329
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001330static PyType_Slot Comptype_slots[] = {
1331 {Py_tp_dealloc, Comp_dealloc},
1332 {Py_tp_methods, comp_methods},
1333 {0, 0},
1334};
1335
1336static PyType_Spec Comptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001337 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001338 sizeof(compobject),
1339 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001340 Py_TPFLAGS_DEFAULT,
1341 Comptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001342};
1343
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001344static PyType_Slot Decomptype_slots[] = {
1345 {Py_tp_dealloc, Decomp_dealloc},
1346 {Py_tp_methods, Decomp_methods},
1347 {Py_tp_members, Decomp_members},
1348 {0, 0},
1349};
1350
1351static PyType_Spec Decomptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001352 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001353 sizeof(compobject),
1354 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001355 Py_TPFLAGS_DEFAULT,
1356 Decomptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001357};
1358
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001360"The functions in this module allow compression and decompression using the\n"
1361"zlib library, which is based on GNU zip.\n"
1362"\n"
1363"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001364"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001365"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001366"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001367"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001368"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001369"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001370"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001371"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001372"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001373
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001374static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001375zlib_clear(PyObject *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001376{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001377 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001378 Py_CLEAR(state->Comptype);
1379 Py_CLEAR(state->Decomptype);
1380 Py_CLEAR(state->ZlibError);
1381 return 0;
1382}
1383
1384static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001385zlib_traverse(PyObject *mod, visitproc visit, void *arg)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001386{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001387 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001388 Py_VISIT(state->Comptype);
1389 Py_VISIT(state->Decomptype);
1390 Py_VISIT(state->ZlibError);
1391 return 0;
1392}
1393
1394static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001395zlib_free(void *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001396{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001397 zlib_clear((PyObject *)mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001398}
1399
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001400static int
1401zlib_exec(PyObject *mod)
1402{
1403 zlibstate *state = get_zlib_state(mod);
1404
1405 state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1406 mod, &Comptype_spec, NULL);
1407 if (state->Comptype == NULL) {
1408 return -1;
1409 }
1410
1411 state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1412 mod, &Decomptype_spec, NULL);
1413 if (state->Decomptype == NULL) {
1414 return -1;
1415 }
1416
1417 state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1418 if (state->ZlibError == NULL) {
1419 return -1;
1420 }
1421
1422 Py_INCREF(state->ZlibError);
1423 if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
1424 Py_DECREF(state->ZlibError);
1425 return -1;
1426 }
1427
1428#define ZLIB_ADD_INT_MACRO(c) \
1429 do { \
1430 if ((PyModule_AddIntConstant(mod, #c, c)) < 0) { \
1431 return -1; \
1432 } \
1433 } while(0)
1434
1435 ZLIB_ADD_INT_MACRO(MAX_WBITS);
1436 ZLIB_ADD_INT_MACRO(DEFLATED);
1437 ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
1438 ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
1439 // compression levels
1440 ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
1441 ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
1442 ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
1443 ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
1444 // compression strategies
1445 ZLIB_ADD_INT_MACRO(Z_FILTERED);
1446 ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
1447#ifdef Z_RLE // 1.2.0.1
1448 ZLIB_ADD_INT_MACRO(Z_RLE);
1449#endif
1450#ifdef Z_FIXED // 1.2.2.2
1451 ZLIB_ADD_INT_MACRO(Z_FIXED);
1452#endif
1453 ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
1454 // allowed flush values
1455 ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
1456 ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
1457 ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
1458 ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
1459 ZLIB_ADD_INT_MACRO(Z_FINISH);
1460#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1461 ZLIB_ADD_INT_MACRO(Z_BLOCK);
1462#endif
1463#ifdef Z_TREES // 1.2.3.4, only for inflate
1464 ZLIB_ADD_INT_MACRO(Z_TREES);
1465#endif
1466 PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
1467 if (ver == NULL) {
1468 return -1;
1469 }
1470
1471 if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
1472 Py_DECREF(ver);
1473 return -1;
1474 }
1475
1476 ver = PyUnicode_FromString(zlibVersion());
1477 if (ver == NULL) {
1478 return -1;
1479 }
1480
1481 if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
1482 Py_DECREF(ver);
1483 return -1;
1484 }
1485
1486 if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
1487 return -1;
1488 }
1489 return 0;
1490}
1491
1492static PyModuleDef_Slot zlib_slots[] = {
1493 {Py_mod_exec, zlib_exec},
1494 {0, NULL}
1495};
1496
Martin v. Löwis1a214512008-06-11 05:26:20 +00001497static struct PyModuleDef zlibmodule = {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001498 PyModuleDef_HEAD_INIT,
1499 .m_name = "zlib",
1500 .m_doc = zlib_module_documentation,
1501 .m_size = sizeof(zlibstate),
1502 .m_methods = zlib_methods,
1503 .m_slots = zlib_slots,
1504 .m_traverse = zlib_traverse,
1505 .m_clear = zlib_clear,
1506 .m_free = zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001507};
1508
Mark Hammond62b1ab12002-07-23 06:31:15 +00001509PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001510PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001511{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001512 return PyModuleDef_Init(&zlibmodule);
Guido van Rossumfb221561997-04-29 15:38:09 +00001513}