blob: 3e77080a31cb5755e5cde0102abbcab2efe81f9f [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"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00009#include "structmember.h"
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#include "pythread.h"
14#define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019
Martin Panter3f0ee832016-06-05 10:48:34 +000020#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000021# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000022#endif
23
Guido van Rossumfb221561997-04-29 15:38:09 +000024/* The following parameters are copied from zutil.h, version 0.95 */
25#define DEFLATED 8
26#if MAX_MEM_LEVEL >= 8
27# define DEF_MEM_LEVEL 8
28#else
29# define DEF_MEM_LEVEL MAX_MEM_LEVEL
30#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000031
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020032/* Initial buffer size. */
33#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000034
Dino Viehlanda1ffad02019-09-10 11:27:03 +010035static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +000036
Dino Viehlanda1ffad02019-09-10 11:27:03 +010037typedef struct {
38 PyTypeObject *Comptype;
39 PyTypeObject *Decomptype;
40 PyObject *ZlibError;
41} _zlibstate;
42
43#define _zlibstate(o) ((_zlibstate *)PyModule_GetState(o))
44#define _zlibstate_global ((_zlibstate *)PyModule_GetState(PyState_FindModule(&zlibmodule)))
Guido van Rossumfb221561997-04-29 15:38:09 +000045
Tim Peters977e5402001-10-17 03:57:20 +000046typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000047{
Jeremy Hylton9714f992001-10-16 21:19:45 +000048 PyObject_HEAD
49 z_stream zst;
50 PyObject *unused_data;
51 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020052 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000053 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020054 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020055 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000056} compobject;
57
Jeremy Hylton0965e082001-10-16 21:56:09 +000058static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020059zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000060{
Nadeem Vawda524148a2011-08-28 11:26:46 +020061 const char *zmsg = Z_NULL;
62 /* In case of a version mismatch, zst.msg won't be initialized.
63 Check for this case first, before looking at zst.msg. */
64 if (err == Z_VERSION_ERROR)
65 zmsg = "library version mismatch";
66 if (zmsg == Z_NULL)
67 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000068 if (zmsg == Z_NULL) {
69 switch (err) {
70 case Z_BUF_ERROR:
71 zmsg = "incomplete or truncated stream";
72 break;
73 case Z_STREAM_ERROR:
74 zmsg = "inconsistent stream state";
75 break;
76 case Z_DATA_ERROR:
77 zmsg = "invalid input data";
78 break;
79 }
80 }
81 if (zmsg == Z_NULL)
Dino Viehlanda1ffad02019-09-10 11:27:03 +010082 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000083 else
Dino Viehlanda1ffad02019-09-10 11:27:03 +010084 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000085}
86
Larry Hastings61272b72014-01-07 12:41:53 -080087/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080088module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080089class zlib.Compress "compobject *" "&Comptype"
90class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080091[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030092/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080093
Guido van Rossumfb221561997-04-29 15:38:09 +000094static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000095newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000096{
Tim Peters977e5402001-10-17 03:57:20 +000097 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000098 self = PyObject_New(compobject, type);
99 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200101 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200103 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000104 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000106 Py_DECREF(self);
107 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000109 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000110 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000111 Py_DECREF(self);
112 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000113 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000114 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200115 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000116 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200117 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
118 return NULL;
119 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000120 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000121}
122
Victor Stinner5064a522013-07-07 16:50:27 +0200123static void*
124PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
125{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300126 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200127 return NULL;
128 /* PyMem_Malloc() cannot be used: the GIL is not held when
129 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300130 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200131}
132
133static void
134PyZlib_Free(voidpf ctx, void *ptr)
135{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200136 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200137}
138
Martin Panter84544c12016-07-23 03:02:07 +0000139static void
140arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
141{
Segev Finer679b5662017-07-27 01:17:57 +0300142 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000143 *remains -= zst->avail_in;
144}
145
146static Py_ssize_t
147arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
148 Py_ssize_t length,
149 Py_ssize_t max_length)
150{
151 Py_ssize_t occupied;
152
153 if (*buffer == NULL) {
154 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
155 return -1;
156 occupied = 0;
157 }
158 else {
159 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
160
161 if (length == occupied) {
162 Py_ssize_t new_length;
163 assert(length <= max_length);
164 /* can not scale the buffer over max_length */
165 if (length == max_length)
166 return -2;
167 if (length <= (max_length >> 1))
168 new_length = length << 1;
169 else
170 new_length = max_length;
171 if (_PyBytes_Resize(buffer, new_length) < 0)
172 return -1;
173 length = new_length;
174 }
175 }
176
Segev Finer679b5662017-07-27 01:17:57 +0300177 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000178 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
179
180 return length;
181}
182
183static Py_ssize_t
184arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
185{
186 Py_ssize_t ret;
187
188 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
189 PY_SSIZE_T_MAX);
190 if (ret == -2)
191 PyErr_NoMemory();
192
193 return ret;
194}
195
Larry Hastings61272b72014-01-07 12:41:53 -0800196/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800197zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200198
Martin Panter1fe0d132016-02-10 10:06:36 +0000199 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800200 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300201 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200202 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000203 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800204
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200205Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800206[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800207
Guido van Rossumfb221561997-04-29 15:38:09 +0000208static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300209zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
210/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000211{
Martin Panter84544c12016-07-23 03:02:07 +0000212 PyObject *RetVal = NULL;
213 Byte *ibuf;
214 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
215 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000216 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000217
Martin Panter525a9492016-07-23 03:39:49 +0000218 ibuf = data->buf;
219 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000220
Victor Stinner5064a522013-07-07 16:50:27 +0200221 zst.opaque = NULL;
222 zst.zalloc = PyZlib_Malloc;
223 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000224 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000225 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000226
Martin Panter84544c12016-07-23 03:02:07 +0000227 switch (err) {
228 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000229 break;
Martin Panter84544c12016-07-23 03:02:07 +0000230 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000231 PyErr_SetString(PyExc_MemoryError,
232 "Out of memory while compressing data");
233 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000234 case Z_STREAM_ERROR:
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100235 PyErr_SetString(_zlibstate_global->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000237 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000238 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 zlib_error(zst, err, "while compressing data");
240 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000241 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000242
Martin Panter84544c12016-07-23 03:02:07 +0000243 do {
244 arrange_input_buffer(&zst, &ibuflen);
245 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000246
Martin Panter84544c12016-07-23 03:02:07 +0000247 do {
248 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
249 if (obuflen < 0) {
250 deflateEnd(&zst);
251 goto error;
252 }
253
254 Py_BEGIN_ALLOW_THREADS
255 err = deflate(&zst, flush);
256 Py_END_ALLOW_THREADS
257
258 if (err == Z_STREAM_ERROR) {
259 deflateEnd(&zst);
260 zlib_error(zst, err, "while compressing data");
261 goto error;
262 }
263
264 } while (zst.avail_out == 0);
265 assert(zst.avail_in == 0);
266
267 } while (flush != Z_FINISH);
268 assert(err == Z_STREAM_END);
269
270 err = deflateEnd(&zst);
271 if (err == Z_OK) {
272 if (_PyBytes_Resize(&RetVal, zst.next_out -
273 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
274 goto error;
275 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000276 }
Tim Peters977e5402001-10-17 03:57:20 +0000277 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000278 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000279 error:
Martin Panter84544c12016-07-23 03:02:07 +0000280 Py_XDECREF(RetVal);
281 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000282}
283
Larry Hastings61272b72014-01-07 12:41:53 -0800284/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100285
Martin Panter84544c12016-07-23 03:02:07 +0000286class ssize_t_converter(CConverter):
287 type = 'Py_ssize_t'
288 converter = 'ssize_t_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200289 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100290
Larry Hastings61272b72014-01-07 12:41:53 -0800291[python start generated code]*/
Martin Panter84544c12016-07-23 03:02:07 +0000292/*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100293
294static int
Martin Panter84544c12016-07-23 03:02:07 +0000295ssize_t_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100296{
Martin Pantere99e9772015-11-20 08:13:35 +0000297 PyObject *long_obj;
298 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100299
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200300 /* XXX Should be replaced with PyNumber_AsSsize_t after the end of the
301 deprecation period. */
302 long_obj = _PyLong_FromNbIndexOrNbInt(obj);
Martin Pantere99e9772015-11-20 08:13:35 +0000303 if (long_obj == NULL) {
304 return 0;
305 }
306 val = PyLong_AsSsize_t(long_obj);
307 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100308 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000309 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100310 }
Martin Panter84544c12016-07-23 03:02:07 +0000311 *(Py_ssize_t *)ptr = val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100312 return 1;
313}
314
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200315/*[clinic input]
316zlib.decompress
317
318 data: Py_buffer
319 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300320 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200321 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000322 The window buffer size and container format.
Martin Panter84544c12016-07-23 03:02:07 +0000323 bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200324 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200325
326Returns a bytes object containing the uncompressed data.
327[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000328
Guido van Rossumfb221561997-04-29 15:38:09 +0000329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300330zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000331 Py_ssize_t bufsize)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300332/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000333{
Martin Panter84544c12016-07-23 03:02:07 +0000334 PyObject *RetVal = NULL;
335 Byte *ibuf;
336 Py_ssize_t ibuflen;
337 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000338 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000339
Martin Panter84544c12016-07-23 03:02:07 +0000340 if (bufsize < 0) {
341 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
342 return NULL;
343 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100344 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000345 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000346
Martin Panter84544c12016-07-23 03:02:07 +0000347 ibuf = data->buf;
348 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000349
Victor Stinner5064a522013-07-07 16:50:27 +0200350 zst.opaque = NULL;
351 zst.zalloc = PyZlib_Malloc;
352 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000353 zst.avail_in = 0;
354 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200355 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000356
Martin Panter84544c12016-07-23 03:02:07 +0000357 switch (err) {
358 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000359 break;
Martin Panter84544c12016-07-23 03:02:07 +0000360 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000361 PyErr_SetString(PyExc_MemoryError,
362 "Out of memory while decompressing data");
363 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000364 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000365 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 zlib_error(zst, err, "while preparing to decompress data");
367 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000368 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000369
Jeremy Hylton9714f992001-10-16 21:19:45 +0000370 do {
Martin Panter84544c12016-07-23 03:02:07 +0000371 arrange_input_buffer(&zst, &ibuflen);
372 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000373
Martin Panter84544c12016-07-23 03:02:07 +0000374 do {
375 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
376 if (bufsize < 0) {
377 inflateEnd(&zst);
378 goto error;
379 }
380
381 Py_BEGIN_ALLOW_THREADS
382 err = inflate(&zst, flush);
383 Py_END_ALLOW_THREADS
384
385 switch (err) {
386 case Z_OK: /* fall through */
387 case Z_BUF_ERROR: /* fall through */
388 case Z_STREAM_END:
389 break;
390 case Z_MEM_ERROR:
391 inflateEnd(&zst);
392 PyErr_SetString(PyExc_MemoryError,
393 "Out of memory while decompressing data");
394 goto error;
395 default:
396 inflateEnd(&zst);
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000397 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000398 goto error;
399 }
Martin Panter84544c12016-07-23 03:02:07 +0000400
401 } while (zst.avail_out == 0);
402
403 } while (err != Z_STREAM_END && ibuflen != 0);
404
405
406 if (err != Z_STREAM_END) {
407 inflateEnd(&zst);
408 zlib_error(zst, err, "while decompressing data");
409 goto error;
410 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000411
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000412 err = inflateEnd(&zst);
413 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200414 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000415 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000416 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000417
Martin Panter84544c12016-07-23 03:02:07 +0000418 if (_PyBytes_Resize(&RetVal, zst.next_out -
419 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000420 goto error;
421
Martin Panter84544c12016-07-23 03:02:07 +0000422 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000423
424 error:
Martin Panter84544c12016-07-23 03:02:07 +0000425 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000426 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000427}
428
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200429/*[clinic input]
430zlib.compressobj
431
432 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000433 The compression level (an integer in the range 0-9 or -1; default is
434 currently equivalent to 6). Higher compression levels are slower,
435 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200436 method: int(c_default="DEFLATED") = DEFLATED
437 The compression algorithm. If given, this must be DEFLATED.
438 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000439 +9 to +15: The base-two logarithm of the window size. Include a zlib
440 container.
441 -9 to -15: Generate a raw stream.
442 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200443 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
444 Controls the amount of memory used for internal compression state.
445 Valid values range from 1 to 9. Higher values result in higher memory
446 usage, faster compression, and smaller output.
447 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
448 Used to tune the compression algorithm. Possible values are
449 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
450 zdict: Py_buffer = None
451 The predefined compression dictionary - a sequence of bytes
452 containing subsequences that are likely to occur in the input data.
453
454Return a compressor object.
455[clinic start generated code]*/
456
Guido van Rossumfb221561997-04-29 15:38:09 +0000457static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300458zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400459 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300460/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000461{
Victor Stinnere079edd2013-11-21 22:33:21 +0100462 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200463 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000464
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200465 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100466 PyErr_SetString(PyExc_OverflowError,
467 "zdict length does not fit in an unsigned int");
468 goto error;
469 }
470
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100471 self = newcompobject(_zlibstate_global->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000472 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200473 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200474 self->zst.opaque = NULL;
475 self->zst.zalloc = PyZlib_Malloc;
476 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000477 self->zst.next_in = NULL;
478 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000479 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000480 switch (err) {
481 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200483 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484 goto success;
485 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100486 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200487 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200488 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000489 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200490 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000491 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200492 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
493 goto error;
494 default:
495 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
496 goto error;
497 }
498 }
Martin Panter84544c12016-07-23 03:02:07 +0000499 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000500 PyErr_SetString(PyExc_MemoryError,
501 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200502 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000503 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000504 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200505 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000506 default:
507 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200508 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000509 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200510
511 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200512 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200513 success:
Martin Panter84544c12016-07-23 03:02:07 +0000514 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000515}
516
Martin Panter3f0ee832016-06-05 10:48:34 +0000517static int
518set_inflate_zdict(compobject *self)
519{
520 Py_buffer zdict_buf;
521 int err;
522
523 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
524 return -1;
525 }
526 if ((size_t)zdict_buf.len > UINT_MAX) {
527 PyErr_SetString(PyExc_OverflowError,
528 "zdict length does not fit in an unsigned int");
529 PyBuffer_Release(&zdict_buf);
530 return -1;
531 }
Martin Panter84544c12016-07-23 03:02:07 +0000532 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000533 zdict_buf.buf, (unsigned int)zdict_buf.len);
534 PyBuffer_Release(&zdict_buf);
535 if (err != Z_OK) {
536 zlib_error(self->zst, err, "while setting zdict");
537 return -1;
538 }
539 return 0;
540}
541
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200542/*[clinic input]
543zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200544
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200545 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000546 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200547 zdict: object(c_default="NULL") = b''
548 The predefined compression dictionary. This must be the same
549 dictionary as used by the compressor that produced the input data.
550
551Return a decompressor object.
552[clinic start generated code]*/
553
554static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300555zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
556/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200557{
558 int err;
559 compobject *self;
560
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200561 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
562 PyErr_SetString(PyExc_TypeError,
563 "zdict argument must support the buffer protocol");
564 return NULL;
565 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000566
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100567 self = newcompobject(_zlibstate_global->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000568 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000569 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200570 self->zst.opaque = NULL;
571 self->zst.zalloc = PyZlib_Malloc;
572 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000573 self->zst.next_in = NULL;
574 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200575 if (zdict != NULL) {
576 Py_INCREF(zdict);
577 self->zdict = zdict;
578 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000579 err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000580 switch (err) {
581 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000582 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000583 if (self->zdict != NULL && wbits < 0) {
584#ifdef AT_LEAST_ZLIB_1_2_2_1
585 if (set_inflate_zdict(self) < 0) {
586 Py_DECREF(self);
587 return NULL;
588 }
589#else
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100590 PyErr_Format(_zlibstate_global->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000591 "zlib version %s does not allow raw inflate with dictionary",
592 ZLIB_VERSION);
593 Py_DECREF(self);
594 return NULL;
595#endif
596 }
Martin Panter84544c12016-07-23 03:02:07 +0000597 return (PyObject *)self;
598 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000599 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000600 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
601 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000602 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000603 Py_DECREF(self);
604 PyErr_SetString(PyExc_MemoryError,
605 "Can't allocate memory for decompression object");
606 return NULL;
607 default:
608 zlib_error(self->zst, err, "while creating decompression object");
609 Py_DECREF(self);
610 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000611 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000612}
613
614static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000615Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000616{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100617 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000618 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000619 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000620 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200621 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000622 PyObject_Del(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100623 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000624}
625
626static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000627Comp_dealloc(compobject *self)
628{
629 if (self->is_initialised)
630 deflateEnd(&self->zst);
631 Dealloc(self);
632}
633
634static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000635Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000636{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000637 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000638 inflateEnd(&self->zst);
639 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000640}
641
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200642/*[clinic input]
643zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000644
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200645 data: Py_buffer
646 Binary data to be compressed.
647 /
648
649Returns a bytes object containing compressed data.
650
651After calling this function, some of the input data may still
652be stored in internal buffers for later processing.
653Call the flush() method to clear these buffers.
654[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000655
Guido van Rossumfb221561997-04-29 15:38:09 +0000656static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200657zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800658/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000659{
Martin Panter84544c12016-07-23 03:02:07 +0000660 PyObject *RetVal = NULL;
661 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200662 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000663
Martin Panter84544c12016-07-23 03:02:07 +0000664 self->zst.next_in = data->buf;
665 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000666
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000667 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000668
Martin Panter84544c12016-07-23 03:02:07 +0000669 do {
670 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000671
Martin Panter84544c12016-07-23 03:02:07 +0000672 do {
673 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
674 if (obuflen < 0)
675 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000676
Martin Panter84544c12016-07-23 03:02:07 +0000677 Py_BEGIN_ALLOW_THREADS
678 err = deflate(&self->zst, Z_NO_FLUSH);
679 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000680
Martin Panter84544c12016-07-23 03:02:07 +0000681 if (err == Z_STREAM_ERROR) {
682 zlib_error(self->zst, err, "while compressing data");
683 goto error;
684 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000685
Martin Panter84544c12016-07-23 03:02:07 +0000686 } while (self->zst.avail_out == 0);
687 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000688
Martin Panter84544c12016-07-23 03:02:07 +0000689 } while (ibuflen != 0);
690
691 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
692 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
693 goto success;
694
695 error:
696 Py_CLEAR(RetVal);
697 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000698 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000699 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000700}
701
Martin Panter84544c12016-07-23 03:02:07 +0000702/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100703 self->unused_data or self->unconsumed_tail, as appropriate. */
704static int
Martin Panter84544c12016-07-23 03:02:07 +0000705save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100706{
707 if (err == Z_STREAM_END) {
708 /* The end of the compressed data has been reached. Store the leftover
709 input data in self->unused_data. */
710 if (self->zst.avail_in > 0) {
711 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000712 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100713 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000714 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
715 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100716 PyErr_NoMemory();
717 return -1;
718 }
Martin Panter84544c12016-07-23 03:02:07 +0000719 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100720 new_data = PyBytes_FromStringAndSize(NULL, new_size);
721 if (new_data == NULL)
722 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200723 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100724 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200725 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000726 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300727 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100728 self->zst.avail_in = 0;
729 }
730 }
Martin Panter84544c12016-07-23 03:02:07 +0000731
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100732 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
733 /* This code handles two distinct cases:
734 1. Output limit was reached. Save leftover input in unconsumed_tail.
735 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000736 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100737 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000738 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100739 if (new_data == NULL)
740 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300741 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100742 }
Martin Panter84544c12016-07-23 03:02:07 +0000743
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100744 return 0;
745}
746
Larry Hastings61272b72014-01-07 12:41:53 -0800747/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800748zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700749
750 data: Py_buffer
751 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300752 /
Martin Panter84544c12016-07-23 03:02:07 +0000753 max_length: ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700754 The maximum allowable length of the decompressed data.
755 Unconsumed input data will be stored in
756 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700757
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200758Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700759
760After calling this function, some of the input data may still be stored in
761internal buffers for later processing.
762Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800763[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700764
Larry Hastings31826802013-10-19 00:09:25 -0700765static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400766zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000767 Py_ssize_t max_length)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300768/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
Larry Hastings31826802013-10-19 00:09:25 -0700769{
Martin Panter84544c12016-07-23 03:02:07 +0000770 int err = Z_OK;
771 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200772 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000773
Martin Panter84544c12016-07-23 03:02:07 +0000774 if (max_length < 0) {
775 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700776 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000777 } else if (max_length == 0)
778 hard_limit = PY_SSIZE_T_MAX;
779 else
780 hard_limit = max_length;
781
782 self->zst.next_in = data->buf;
783 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000784
Jeremy Hylton9714f992001-10-16 21:19:45 +0000785 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000786 if (max_length && obuflen > max_length)
787 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000788
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800789 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000790
Martin Panter84544c12016-07-23 03:02:07 +0000791 do {
792 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000793
Martin Panter84544c12016-07-23 03:02:07 +0000794 do {
795 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
796 obuflen, hard_limit);
797 if (obuflen == -2) {
798 if (max_length > 0) {
799 goto save;
800 }
801 PyErr_NoMemory();
802 }
803 if (obuflen < 0) {
804 goto abort;
805 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000806
Martin Panter84544c12016-07-23 03:02:07 +0000807 Py_BEGIN_ALLOW_THREADS
808 err = inflate(&self->zst, Z_SYNC_FLUSH);
809 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100810
Martin Panter84544c12016-07-23 03:02:07 +0000811 switch (err) {
812 case Z_OK: /* fall through */
813 case Z_BUF_ERROR: /* fall through */
814 case Z_STREAM_END:
815 break;
816 default:
817 if (err == Z_NEED_DICT && self->zdict != NULL) {
818 if (set_inflate_zdict(self) < 0)
819 goto abort;
820 else
821 break;
822 }
823 goto save;
824 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200825
Martin Panter84544c12016-07-23 03:02:07 +0000826 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000827
Martin Panter84544c12016-07-23 03:02:07 +0000828 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000829
Martin Panter84544c12016-07-23 03:02:07 +0000830 save:
831 if (save_unconsumed_input(self, data, err) < 0)
832 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000833
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000834 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100835 /* This is the logical place to call inflateEnd, but the old behaviour
836 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800837 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100838 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000839 /* We will only get Z_BUF_ERROR if the output buffer was full
840 but there wasn't more output when we tried again, so it is
841 not an error condition.
842 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800843 zlib_error(self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000844 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000845 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000846
Martin Panter84544c12016-07-23 03:02:07 +0000847 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
848 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
849 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000850
Martin Panter84544c12016-07-23 03:02:07 +0000851 abort:
852 Py_CLEAR(RetVal);
853 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800854 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000855 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000856}
857
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200858/*[clinic input]
859zlib.Compress.flush
860
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200861 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200862 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
863 If mode == Z_FINISH, the compressor object can no longer be
864 used after calling the flush() method. Otherwise, more data
865 can still be compressed.
866 /
867
868Return a bytes object containing any remaining compressed data.
869[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000870
Guido van Rossumfb221561997-04-29 15:38:09 +0000871static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200872zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200873/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000874{
Victor Stinnere079edd2013-11-21 22:33:21 +0100875 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000876 Py_ssize_t length = DEF_BUF_SIZE;
877 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000878
Jeremy Hylton9714f992001-10-16 21:19:45 +0000879 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
880 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200881 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000882 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000883 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000884
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000885 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000886
Jeremy Hylton9714f992001-10-16 21:19:45 +0000887 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000888
Martin Panter84544c12016-07-23 03:02:07 +0000889 do {
890 length = arrange_output_buffer(&self->zst, &RetVal, length);
891 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200892 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000894 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000897 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000899
900 if (err == Z_STREAM_ERROR) {
901 zlib_error(self->zst, err, "while flushing");
902 Py_CLEAR(RetVal);
903 goto error;
904 }
905 } while (self->zst.avail_out == 0);
906 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000907
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200908 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000909 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200910 mode is Z_FINISH, but checking both for safety*/
911 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000912 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000913 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200914 zlib_error(self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000915 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000916 goto error;
917 }
918 else
919 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000920
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000921 /* We will only get Z_BUF_ERROR if the output buffer was full
922 but there wasn't more output when we tried again, so it is
923 not an error condition.
924 */
Martin Panter84544c12016-07-23 03:02:07 +0000925 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000926 zlib_error(self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000927 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000928 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000929 }
Tim Peters977e5402001-10-17 03:57:20 +0000930
Martin Panter84544c12016-07-23 03:02:07 +0000931 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
932 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200933 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000934
Tim Peters977e5402001-10-17 03:57:20 +0000935 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000936 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000937 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000938}
939
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000940#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700941
Larry Hastings61272b72014-01-07 12:41:53 -0800942/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800943zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700944
945Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800946[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700947
Larry Hastings3cceb382014-01-04 11:09:09 -0800948static PyObject *
949zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800950/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951{
952 compobject *retval = NULL;
953 int err;
954
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100955 retval = newcompobject(_zlibstate_global->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 if (!retval) return NULL;
957
958 /* Copy the zstream state
959 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
960 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800961 ENTER_ZLIB(self);
962 err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000963 switch (err) {
964 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000965 break;
Martin Panter84544c12016-07-23 03:02:07 +0000966 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000967 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
968 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000969 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970 PyErr_SetString(PyExc_MemoryError,
971 "Can't allocate memory for compression object");
972 goto error;
973 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800974 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000975 goto error;
976 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800977 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300978 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800979 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300980 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800981 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300982 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800983 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000984
985 /* Mark it as being initialized */
986 retval->is_initialised = 1;
987
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800988 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000989 return (PyObject *)retval;
990
991error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800992 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993 Py_XDECREF(retval);
994 return NULL;
995}
996
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200997/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600998zlib.Compress.__copy__
999[clinic start generated code]*/
1000
1001static PyObject *
1002zlib_Compress___copy___impl(compobject *self)
1003/*[clinic end generated code: output=1875e6791975442e input=be97a05a788dfd83]*/
1004{
1005 return zlib_Compress_copy_impl(self);
1006}
1007
1008/*[clinic input]
1009zlib.Compress.__deepcopy__
1010
1011 memo: object
1012 /
1013
1014[clinic start generated code]*/
1015
1016static PyObject *
1017zlib_Compress___deepcopy__(compobject *self, PyObject *memo)
1018/*[clinic end generated code: output=f47a2213282c9eb0 input=a9a8b0b40d83388e]*/
1019{
1020 return zlib_Compress_copy_impl(self);
1021}
1022
1023/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001024zlib.Decompress.copy
1025
1026Return a copy of the decompression object.
1027[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001028
1029static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001030zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08001031/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032{
1033 compobject *retval = NULL;
1034 int err;
1035
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001036 retval = newcompobject(_zlibstate_global->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001037 if (!retval) return NULL;
1038
1039 /* Copy the zstream state
1040 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1041 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001042 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043 err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001044 switch (err) {
1045 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001046 break;
Martin Panter84544c12016-07-23 03:02:07 +00001047 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001048 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1049 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001050 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001051 PyErr_SetString(PyExc_MemoryError,
1052 "Can't allocate memory for decompression object");
1053 goto error;
1054 default:
1055 zlib_error(self->zst, err, "while copying decompression object");
1056 goto error;
1057 }
1058
1059 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001060 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001061 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001062 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001063 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001064 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001065 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001066
1067 /* Mark it as being initialized */
1068 retval->is_initialised = 1;
1069
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001070 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071 return (PyObject *)retval;
1072
1073error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001074 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001075 Py_XDECREF(retval);
1076 return NULL;
1077}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001078
1079/*[clinic input]
1080zlib.Decompress.__copy__
1081[clinic start generated code]*/
1082
1083static PyObject *
1084zlib_Decompress___copy___impl(compobject *self)
1085/*[clinic end generated code: output=80bae8bc43498ad4 input=efcb98b5472c13d2]*/
1086{
1087 return zlib_Decompress_copy_impl(self);
1088}
1089
1090/*[clinic input]
1091zlib.Decompress.__deepcopy__
1092
1093 memo: object
1094 /
1095
1096[clinic start generated code]*/
1097
1098static PyObject *
1099zlib_Decompress___deepcopy__(compobject *self, PyObject *memo)
1100/*[clinic end generated code: output=1f77286ab490124b input=6e99bd0ac4b9cd8b]*/
1101{
1102 return zlib_Decompress_copy_impl(self);
1103}
1104
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001105#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001106
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001107/*[clinic input]
1108zlib.Decompress.flush
1109
Martin Panter84544c12016-07-23 03:02:07 +00001110 length: ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001111 the initial size of the output buffer.
1112 /
1113
1114Return a bytes object containing any remaining decompressed data.
1115[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001116
Guido van Rossumfb221561997-04-29 15:38:09 +00001117static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +00001118zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1119/*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
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
Martin Panter84544c12016-07-23 03:02:07 +00001126 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001127 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1128 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001129 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001130
Martin Panter84544c12016-07-23 03:02:07 +00001131 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001132 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001133
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001134 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001135
Martin Panter84544c12016-07-23 03:02:07 +00001136 self->zst.next_in = data.buf;
1137 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001138
Martin Panter84544c12016-07-23 03:02:07 +00001139 do {
1140 arrange_input_buffer(&self->zst, &ibuflen);
1141 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001142
Martin Panter84544c12016-07-23 03:02:07 +00001143 do {
1144 length = arrange_output_buffer(&self->zst, &RetVal, length);
1145 if (length < 0)
1146 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001147
Martin Panter84544c12016-07-23 03:02:07 +00001148 Py_BEGIN_ALLOW_THREADS
1149 err = inflate(&self->zst, flush);
1150 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001151
Martin Panter84544c12016-07-23 03:02:07 +00001152 switch (err) {
1153 case Z_OK: /* fall through */
1154 case Z_BUF_ERROR: /* fall through */
1155 case Z_STREAM_END:
1156 break;
1157 default:
1158 if (err == Z_NEED_DICT && self->zdict != NULL) {
1159 if (set_inflate_zdict(self) < 0)
1160 goto abort;
1161 else
1162 break;
1163 }
1164 goto save;
1165 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001166
Martin Panter84544c12016-07-23 03:02:07 +00001167 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1168
1169 } while (err != Z_STREAM_END && ibuflen != 0);
1170
1171 save:
1172 if (save_unconsumed_input(self, &data, err) < 0)
1173 goto abort;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001174
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001175 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001176 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001177 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001178 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001179 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001180 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001181 zlib_error(self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001182 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001183 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001184 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001185
Martin Panter84544c12016-07-23 03:02:07 +00001186 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1187 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1188 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001189
Martin Panter84544c12016-07-23 03:02:07 +00001190 abort:
1191 Py_CLEAR(RetVal);
1192 success:
1193 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001194 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001195 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001196}
1197
Christian Heimes936e2f32014-01-27 01:06:57 +01001198#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001199
Guido van Rossumfb221561997-04-29 15:38:09 +00001200static PyMethodDef comp_methods[] =
1201{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001202 ZLIB_COMPRESS_COMPRESS_METHODDEF
1203 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001204 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001205 ZLIB_COMPRESS___COPY___METHODDEF
1206 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001207 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001208};
1209
1210static PyMethodDef Decomp_methods[] =
1211{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001212 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001213 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001214 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001215 ZLIB_DECOMPRESS___COPY___METHODDEF
1216 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001217 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001218};
1219
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001220#define COMP_OFF(x) offsetof(compobject, x)
1221static PyMemberDef Decomp_members[] = {
1222 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1223 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001224 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001225 {NULL},
1226};
Guido van Rossumfb221561997-04-29 15:38:09 +00001227
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001228/*[clinic input]
1229zlib.adler32
1230
1231 data: Py_buffer
1232 value: unsigned_int(bitwise=True) = 1
1233 Starting value of the checksum.
1234 /
1235
1236Compute an Adler-32 checksum of data.
1237
1238The returned checksum is an integer.
1239[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001240
Guido van Rossumfb221561997-04-29 15:38:09 +00001241static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001242zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1243/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001244{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001245 /* Releasing the GIL for very small buffers is inefficient
1246 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001247 if (data->len > 1024*5) {
1248 unsigned char *buf = data->buf;
1249 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001250
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001251 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001252 /* Avoid truncation of length for very large buffers. adler32() takes
1253 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001254 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001255 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001256 buf += (size_t) UINT_MAX;
1257 len -= (size_t) UINT_MAX;
1258 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001259 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001260 Py_END_ALLOW_THREADS
1261 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001262 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001263 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001264 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001265}
Tim Peters977e5402001-10-17 03:57:20 +00001266
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001267/*[clinic input]
1268zlib.crc32
1269
1270 data: Py_buffer
1271 value: unsigned_int(bitwise=True) = 0
1272 Starting value of the checksum.
1273 /
1274
1275Compute a CRC-32 checksum of data.
1276
1277The returned checksum is an integer.
1278[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001279
1280static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001281zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1282/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001283{
Martin v. Löwis423be952008-08-13 15:53:07 +00001284 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001285
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001286 /* Releasing the GIL for very small buffers is inefficient
1287 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001288 if (data->len > 1024*5) {
1289 unsigned char *buf = data->buf;
1290 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001291
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001292 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001293 /* Avoid truncation of length for very large buffers. crc32() takes
1294 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001295 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001296 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001297 buf += (size_t) UINT_MAX;
1298 len -= (size_t) UINT_MAX;
1299 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001300 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001301 Py_END_ALLOW_THREADS
1302 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001303 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001304 }
Christian Heimescc47b052008-03-25 14:56:36 +00001305 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001306}
Tim Peters977e5402001-10-17 03:57:20 +00001307
Guido van Rossumfb221561997-04-29 15:38:09 +00001308
1309static PyMethodDef zlib_methods[] =
1310{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001311 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001312 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001313 ZLIB_COMPRESSOBJ_METHODDEF
1314 ZLIB_CRC32_METHODDEF
1315 ZLIB_DECOMPRESS_METHODDEF
1316 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001317 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001318};
1319
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001320static PyType_Slot Comptype_slots[] = {
1321 {Py_tp_dealloc, Comp_dealloc},
1322 {Py_tp_methods, comp_methods},
1323 {0, 0},
1324};
1325
1326static PyType_Spec Comptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001327 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001328 sizeof(compobject),
1329 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001330 Py_TPFLAGS_DEFAULT,
1331 Comptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001332};
1333
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001334static PyType_Slot Decomptype_slots[] = {
1335 {Py_tp_dealloc, Decomp_dealloc},
1336 {Py_tp_methods, Decomp_methods},
1337 {Py_tp_members, Decomp_members},
1338 {0, 0},
1339};
1340
1341static PyType_Spec Decomptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001342 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001343 sizeof(compobject),
1344 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001345 Py_TPFLAGS_DEFAULT,
1346 Decomptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001347};
1348
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001349PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001350"The functions in this module allow compression and decompression using the\n"
1351"zlib library, which is based on GNU zip.\n"
1352"\n"
1353"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001354"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001355"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001356"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001357"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001358"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001359"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001360"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001361"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001362"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001363
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001364static int
1365zlib_clear(PyObject *m)
1366{
1367 _zlibstate *state = _zlibstate(m);
1368 Py_CLEAR(state->Comptype);
1369 Py_CLEAR(state->Decomptype);
1370 Py_CLEAR(state->ZlibError);
1371 return 0;
1372}
1373
1374static int
1375zlib_traverse(PyObject *m, visitproc visit, void *arg)
1376{
1377 _zlibstate *state = _zlibstate(m);
1378 Py_VISIT(state->Comptype);
1379 Py_VISIT(state->Decomptype);
1380 Py_VISIT(state->ZlibError);
1381 return 0;
1382}
1383
1384static void
1385zlib_free(void *m)
1386{
1387 zlib_clear((PyObject *)m);
1388}
1389
Martin v. Löwis1a214512008-06-11 05:26:20 +00001390static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001391 PyModuleDef_HEAD_INIT,
1392 "zlib",
1393 zlib_module_documentation,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001394 sizeof(_zlibstate),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001395 zlib_methods,
1396 NULL,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001397 zlib_traverse,
1398 zlib_clear,
1399 zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001400};
1401
Mark Hammond62b1ab12002-07-23 06:31:15 +00001402PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001403PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001404{
Fred Drake4baedc12002-04-01 14:53:37 +00001405 PyObject *m, *ver;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001406 m = PyState_FindModule(&zlibmodule);
1407 if (m != NULL) {
1408 Py_INCREF(m);
1409 return m;
1410 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001411 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001412 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001413 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001414
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001415 PyTypeObject *Comptype = (PyTypeObject *)PyType_FromSpec(&Comptype_spec);
1416 if (Comptype == NULL)
1417 return NULL;
1418 _zlibstate(m)->Comptype = Comptype;
1419
1420 PyTypeObject *Decomptype = (PyTypeObject *)PyType_FromSpec(&Decomptype_spec);
1421 if (Decomptype == NULL)
1422 return NULL;
1423 _zlibstate(m)->Decomptype = Decomptype;
1424
1425 PyObject *ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Fred Drake4baedc12002-04-01 14:53:37 +00001426 if (ZlibError != NULL) {
1427 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001428 PyModule_AddObject(m, "error", ZlibError);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001429 _zlibstate(m)->ZlibError = ZlibError;
Fred Drake4baedc12002-04-01 14:53:37 +00001430 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001431 PyModule_AddIntMacro(m, MAX_WBITS);
1432 PyModule_AddIntMacro(m, DEFLATED);
1433 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001434 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001435 // compression levels
1436 PyModule_AddIntMacro(m, Z_NO_COMPRESSION);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001437 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1438 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1439 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001440 // compression strategies
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001441 PyModule_AddIntMacro(m, Z_FILTERED);
1442 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001443#ifdef Z_RLE // 1.2.0.1
1444 PyModule_AddIntMacro(m, Z_RLE);
1445#endif
1446#ifdef Z_FIXED // 1.2.2.2
1447 PyModule_AddIntMacro(m, Z_FIXED);
1448#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001449 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001450 // allowed flush values
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001451 PyModule_AddIntMacro(m, Z_NO_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001452 PyModule_AddIntMacro(m, Z_PARTIAL_FLUSH);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001453 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1454 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001455 PyModule_AddIntMacro(m, Z_FINISH);
1456#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1457 PyModule_AddIntMacro(m, Z_BLOCK);
1458#endif
1459#ifdef Z_TREES // 1.2.3.4, only for inflate
1460 PyModule_AddIntMacro(m, Z_TREES);
1461#endif
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001462 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001463 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001464 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001465
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001466 ver = PyUnicode_FromString(zlibVersion());
1467 if (ver != NULL)
1468 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1469
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001470 PyModule_AddStringConstant(m, "__version__", "1.0");
1471
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001472 PyState_AddModule(m, &zlibmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001473 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001474}