blob: fd3064952869bf8eb39f0b08d2e53e80bf419e49 [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;
40} _zlibstate;
41
Hai Shif707d942020-03-16 21:15:01 +080042static inline _zlibstate*
43get_zlib_state(PyObject *module)
44{
45 void *state = PyModule_GetState(module);
46 assert(state != NULL);
47 return (_zlibstate *)state;
48}
49
Dino Viehlanda1ffad02019-09-10 11:27:03 +010050#define _zlibstate_global ((_zlibstate *)PyModule_GetState(PyState_FindModule(&zlibmodule)))
Guido van Rossumfb221561997-04-29 15:38:09 +000051
Tim Peters977e5402001-10-17 03:57:20 +000052typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000053{
Jeremy Hylton9714f992001-10-16 21:19:45 +000054 PyObject_HEAD
55 z_stream zst;
56 PyObject *unused_data;
57 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020058 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000059 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020060 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020061 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000062} compobject;
63
Jeremy Hylton0965e082001-10-16 21:56:09 +000064static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020065zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000066{
Nadeem Vawda524148a2011-08-28 11:26:46 +020067 const char *zmsg = Z_NULL;
68 /* In case of a version mismatch, zst.msg won't be initialized.
69 Check for this case first, before looking at zst.msg. */
70 if (err == Z_VERSION_ERROR)
71 zmsg = "library version mismatch";
72 if (zmsg == Z_NULL)
73 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000074 if (zmsg == Z_NULL) {
75 switch (err) {
76 case Z_BUF_ERROR:
77 zmsg = "incomplete or truncated stream";
78 break;
79 case Z_STREAM_ERROR:
80 zmsg = "inconsistent stream state";
81 break;
82 case Z_DATA_ERROR:
83 zmsg = "invalid input data";
84 break;
85 }
86 }
87 if (zmsg == Z_NULL)
Dino Viehlanda1ffad02019-09-10 11:27:03 +010088 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000089 else
Dino Viehlanda1ffad02019-09-10 11:27:03 +010090 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000091}
92
Larry Hastings61272b72014-01-07 12:41:53 -080093/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080094module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080095class zlib.Compress "compobject *" "&Comptype"
96class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080097[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030098/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080099
Guido van Rossumfb221561997-04-29 15:38:09 +0000100static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000101newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000102{
Tim Peters977e5402001-10-17 03:57:20 +0000103 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 self = PyObject_New(compobject, type);
105 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000106 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200107 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200109 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000110 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 Py_DECREF(self);
113 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000115 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000116 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 Py_DECREF(self);
118 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000120 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200121 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000122 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200123 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
124 return NULL;
125 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000126 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000127}
128
Victor Stinner5064a522013-07-07 16:50:27 +0200129static void*
130PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
131{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300132 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200133 return NULL;
134 /* PyMem_Malloc() cannot be used: the GIL is not held when
135 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300136 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200137}
138
139static void
140PyZlib_Free(voidpf ctx, void *ptr)
141{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200142 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200143}
144
Martin Panter84544c12016-07-23 03:02:07 +0000145static void
146arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
147{
Segev Finer679b5662017-07-27 01:17:57 +0300148 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000149 *remains -= zst->avail_in;
150}
151
152static Py_ssize_t
153arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
154 Py_ssize_t length,
155 Py_ssize_t max_length)
156{
157 Py_ssize_t occupied;
158
159 if (*buffer == NULL) {
160 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
161 return -1;
162 occupied = 0;
163 }
164 else {
165 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
166
167 if (length == occupied) {
168 Py_ssize_t new_length;
169 assert(length <= max_length);
170 /* can not scale the buffer over max_length */
171 if (length == max_length)
172 return -2;
173 if (length <= (max_length >> 1))
174 new_length = length << 1;
175 else
176 new_length = max_length;
177 if (_PyBytes_Resize(buffer, new_length) < 0)
178 return -1;
179 length = new_length;
180 }
181 }
182
Segev Finer679b5662017-07-27 01:17:57 +0300183 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000184 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
185
186 return length;
187}
188
189static Py_ssize_t
190arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
191{
192 Py_ssize_t ret;
193
194 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
195 PY_SSIZE_T_MAX);
196 if (ret == -2)
197 PyErr_NoMemory();
198
199 return ret;
200}
201
Larry Hastings61272b72014-01-07 12:41:53 -0800202/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800203zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200204
Martin Panter1fe0d132016-02-10 10:06:36 +0000205 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800206 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300207 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200208 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000209 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800210
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200211Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800212[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800213
Guido van Rossumfb221561997-04-29 15:38:09 +0000214static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300215zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
216/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000217{
Martin Panter84544c12016-07-23 03:02:07 +0000218 PyObject *RetVal = NULL;
219 Byte *ibuf;
220 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
221 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000222 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000223
Martin Panter525a9492016-07-23 03:39:49 +0000224 ibuf = data->buf;
225 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000226
Victor Stinner5064a522013-07-07 16:50:27 +0200227 zst.opaque = NULL;
228 zst.zalloc = PyZlib_Malloc;
229 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000230 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000231 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000232
Martin Panter84544c12016-07-23 03:02:07 +0000233 switch (err) {
234 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000235 break;
Martin Panter84544c12016-07-23 03:02:07 +0000236 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000237 PyErr_SetString(PyExc_MemoryError,
238 "Out of memory while compressing data");
239 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000240 case Z_STREAM_ERROR:
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100241 PyErr_SetString(_zlibstate_global->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000242 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000243 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 zlib_error(zst, err, "while compressing data");
246 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000247 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000248
Martin Panter84544c12016-07-23 03:02:07 +0000249 do {
250 arrange_input_buffer(&zst, &ibuflen);
251 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000252
Martin Panter84544c12016-07-23 03:02:07 +0000253 do {
254 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
255 if (obuflen < 0) {
256 deflateEnd(&zst);
257 goto error;
258 }
259
260 Py_BEGIN_ALLOW_THREADS
261 err = deflate(&zst, flush);
262 Py_END_ALLOW_THREADS
263
264 if (err == Z_STREAM_ERROR) {
265 deflateEnd(&zst);
266 zlib_error(zst, err, "while compressing data");
267 goto error;
268 }
269
270 } while (zst.avail_out == 0);
271 assert(zst.avail_in == 0);
272
273 } while (flush != Z_FINISH);
274 assert(err == Z_STREAM_END);
275
276 err = deflateEnd(&zst);
277 if (err == Z_OK) {
278 if (_PyBytes_Resize(&RetVal, zst.next_out -
279 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
280 goto error;
281 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000282 }
Tim Peters977e5402001-10-17 03:57:20 +0000283 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000284 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000285 error:
Martin Panter84544c12016-07-23 03:02:07 +0000286 Py_XDECREF(RetVal);
287 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000288}
289
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200290/*[clinic input]
291zlib.decompress
292
293 data: Py_buffer
294 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300295 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200296 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000297 The window buffer size and container format.
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300298 bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200299 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200300
301Returns a bytes object containing the uncompressed data.
302[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000303
Guido van Rossumfb221561997-04-29 15:38:09 +0000304static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300305zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000306 Py_ssize_t bufsize)
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300307/*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000308{
Martin Panter84544c12016-07-23 03:02:07 +0000309 PyObject *RetVal = NULL;
310 Byte *ibuf;
311 Py_ssize_t ibuflen;
312 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000313 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000314
Martin Panter84544c12016-07-23 03:02:07 +0000315 if (bufsize < 0) {
316 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
317 return NULL;
318 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100319 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000320 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000321
Martin Panter84544c12016-07-23 03:02:07 +0000322 ibuf = data->buf;
323 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000324
Victor Stinner5064a522013-07-07 16:50:27 +0200325 zst.opaque = NULL;
326 zst.zalloc = PyZlib_Malloc;
327 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000328 zst.avail_in = 0;
329 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200330 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000331
Martin Panter84544c12016-07-23 03:02:07 +0000332 switch (err) {
333 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000334 break;
Martin Panter84544c12016-07-23 03:02:07 +0000335 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 PyErr_SetString(PyExc_MemoryError,
337 "Out of memory while decompressing data");
338 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000339 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000340 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000341 zlib_error(zst, err, "while preparing to decompress data");
342 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000343 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000344
Jeremy Hylton9714f992001-10-16 21:19:45 +0000345 do {
Martin Panter84544c12016-07-23 03:02:07 +0000346 arrange_input_buffer(&zst, &ibuflen);
347 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000348
Martin Panter84544c12016-07-23 03:02:07 +0000349 do {
350 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
351 if (bufsize < 0) {
352 inflateEnd(&zst);
353 goto error;
354 }
355
356 Py_BEGIN_ALLOW_THREADS
357 err = inflate(&zst, flush);
358 Py_END_ALLOW_THREADS
359
360 switch (err) {
361 case Z_OK: /* fall through */
362 case Z_BUF_ERROR: /* fall through */
363 case Z_STREAM_END:
364 break;
365 case Z_MEM_ERROR:
366 inflateEnd(&zst);
367 PyErr_SetString(PyExc_MemoryError,
368 "Out of memory while decompressing data");
369 goto error;
370 default:
371 inflateEnd(&zst);
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000372 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 goto error;
374 }
Martin Panter84544c12016-07-23 03:02:07 +0000375
376 } while (zst.avail_out == 0);
377
378 } while (err != Z_STREAM_END && ibuflen != 0);
379
380
381 if (err != Z_STREAM_END) {
382 inflateEnd(&zst);
383 zlib_error(zst, err, "while decompressing data");
384 goto error;
385 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000386
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000387 err = inflateEnd(&zst);
388 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200389 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000390 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000391 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000392
Martin Panter84544c12016-07-23 03:02:07 +0000393 if (_PyBytes_Resize(&RetVal, zst.next_out -
394 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000395 goto error;
396
Martin Panter84544c12016-07-23 03:02:07 +0000397 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000398
399 error:
Martin Panter84544c12016-07-23 03:02:07 +0000400 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000401 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000402}
403
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200404/*[clinic input]
405zlib.compressobj
406
407 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000408 The compression level (an integer in the range 0-9 or -1; default is
409 currently equivalent to 6). Higher compression levels are slower,
410 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200411 method: int(c_default="DEFLATED") = DEFLATED
412 The compression algorithm. If given, this must be DEFLATED.
413 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000414 +9 to +15: The base-two logarithm of the window size. Include a zlib
415 container.
416 -9 to -15: Generate a raw stream.
417 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200418 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
419 Controls the amount of memory used for internal compression state.
420 Valid values range from 1 to 9. Higher values result in higher memory
421 usage, faster compression, and smaller output.
422 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
423 Used to tune the compression algorithm. Possible values are
424 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
425 zdict: Py_buffer = None
426 The predefined compression dictionary - a sequence of bytes
427 containing subsequences that are likely to occur in the input data.
428
429Return a compressor object.
430[clinic start generated code]*/
431
Guido van Rossumfb221561997-04-29 15:38:09 +0000432static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300433zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400434 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300435/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000436{
Victor Stinnere079edd2013-11-21 22:33:21 +0100437 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200438 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000439
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200440 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100441 PyErr_SetString(PyExc_OverflowError,
442 "zdict length does not fit in an unsigned int");
443 goto error;
444 }
445
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100446 self = newcompobject(_zlibstate_global->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000447 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200448 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200449 self->zst.opaque = NULL;
450 self->zst.zalloc = PyZlib_Malloc;
451 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000452 self->zst.next_in = NULL;
453 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000454 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000455 switch (err) {
456 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000457 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200458 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200459 goto success;
460 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100461 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200462 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200463 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000464 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200465 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000466 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200467 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
468 goto error;
469 default:
470 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
471 goto error;
472 }
473 }
Martin Panter84544c12016-07-23 03:02:07 +0000474 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000475 PyErr_SetString(PyExc_MemoryError,
476 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200477 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000478 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000479 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200480 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000481 default:
482 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200483 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000484 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200485
486 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200487 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200488 success:
Martin Panter84544c12016-07-23 03:02:07 +0000489 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000490}
491
Martin Panter3f0ee832016-06-05 10:48:34 +0000492static int
493set_inflate_zdict(compobject *self)
494{
495 Py_buffer zdict_buf;
496 int err;
497
498 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
499 return -1;
500 }
501 if ((size_t)zdict_buf.len > UINT_MAX) {
502 PyErr_SetString(PyExc_OverflowError,
503 "zdict length does not fit in an unsigned int");
504 PyBuffer_Release(&zdict_buf);
505 return -1;
506 }
Martin Panter84544c12016-07-23 03:02:07 +0000507 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000508 zdict_buf.buf, (unsigned int)zdict_buf.len);
509 PyBuffer_Release(&zdict_buf);
510 if (err != Z_OK) {
511 zlib_error(self->zst, err, "while setting zdict");
512 return -1;
513 }
514 return 0;
515}
516
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200517/*[clinic input]
518zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200519
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200520 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000521 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200522 zdict: object(c_default="NULL") = b''
523 The predefined compression dictionary. This must be the same
524 dictionary as used by the compressor that produced the input data.
525
526Return a decompressor object.
527[clinic start generated code]*/
528
529static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300530zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
531/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200532{
533 int err;
534 compobject *self;
535
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200536 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
537 PyErr_SetString(PyExc_TypeError,
538 "zdict argument must support the buffer protocol");
539 return NULL;
540 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000541
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100542 self = newcompobject(_zlibstate_global->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000543 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000544 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200545 self->zst.opaque = NULL;
546 self->zst.zalloc = PyZlib_Malloc;
547 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000548 self->zst.next_in = NULL;
549 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200550 if (zdict != NULL) {
551 Py_INCREF(zdict);
552 self->zdict = zdict;
553 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000554 err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000555 switch (err) {
556 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000557 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000558 if (self->zdict != NULL && wbits < 0) {
559#ifdef AT_LEAST_ZLIB_1_2_2_1
560 if (set_inflate_zdict(self) < 0) {
561 Py_DECREF(self);
562 return NULL;
563 }
564#else
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100565 PyErr_Format(_zlibstate_global->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000566 "zlib version %s does not allow raw inflate with dictionary",
567 ZLIB_VERSION);
568 Py_DECREF(self);
569 return NULL;
570#endif
571 }
Martin Panter84544c12016-07-23 03:02:07 +0000572 return (PyObject *)self;
573 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000574 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
576 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000577 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000578 Py_DECREF(self);
579 PyErr_SetString(PyExc_MemoryError,
580 "Can't allocate memory for decompression object");
581 return NULL;
582 default:
583 zlib_error(self->zst, err, "while creating decompression object");
584 Py_DECREF(self);
585 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000586 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000587}
588
589static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000590Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000591{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100592 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000593 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000594 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000595 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200596 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000597 PyObject_Del(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100598 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000599}
600
601static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000602Comp_dealloc(compobject *self)
603{
604 if (self->is_initialised)
605 deflateEnd(&self->zst);
606 Dealloc(self);
607}
608
609static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000610Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000611{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000612 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000613 inflateEnd(&self->zst);
614 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000615}
616
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200617/*[clinic input]
618zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000619
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200620 data: Py_buffer
621 Binary data to be compressed.
622 /
623
624Returns a bytes object containing compressed data.
625
626After calling this function, some of the input data may still
627be stored in internal buffers for later processing.
628Call the flush() method to clear these buffers.
629[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000630
Guido van Rossumfb221561997-04-29 15:38:09 +0000631static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200632zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800633/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000634{
Martin Panter84544c12016-07-23 03:02:07 +0000635 PyObject *RetVal = NULL;
636 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200637 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000638
Martin Panter84544c12016-07-23 03:02:07 +0000639 self->zst.next_in = data->buf;
640 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000641
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000642 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643
Martin Panter84544c12016-07-23 03:02:07 +0000644 do {
645 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000646
Martin Panter84544c12016-07-23 03:02:07 +0000647 do {
648 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
649 if (obuflen < 0)
650 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000651
Martin Panter84544c12016-07-23 03:02:07 +0000652 Py_BEGIN_ALLOW_THREADS
653 err = deflate(&self->zst, Z_NO_FLUSH);
654 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000655
Martin Panter84544c12016-07-23 03:02:07 +0000656 if (err == Z_STREAM_ERROR) {
657 zlib_error(self->zst, err, "while compressing data");
658 goto error;
659 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000660
Martin Panter84544c12016-07-23 03:02:07 +0000661 } while (self->zst.avail_out == 0);
662 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000663
Martin Panter84544c12016-07-23 03:02:07 +0000664 } while (ibuflen != 0);
665
666 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
667 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
668 goto success;
669
670 error:
671 Py_CLEAR(RetVal);
672 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000673 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000674 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000675}
676
Martin Panter84544c12016-07-23 03:02:07 +0000677/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100678 self->unused_data or self->unconsumed_tail, as appropriate. */
679static int
Martin Panter84544c12016-07-23 03:02:07 +0000680save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100681{
682 if (err == Z_STREAM_END) {
683 /* The end of the compressed data has been reached. Store the leftover
684 input data in self->unused_data. */
685 if (self->zst.avail_in > 0) {
686 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000687 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100688 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000689 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
690 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100691 PyErr_NoMemory();
692 return -1;
693 }
Martin Panter84544c12016-07-23 03:02:07 +0000694 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100695 new_data = PyBytes_FromStringAndSize(NULL, new_size);
696 if (new_data == NULL)
697 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200698 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100699 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200700 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000701 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300702 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100703 self->zst.avail_in = 0;
704 }
705 }
Martin Panter84544c12016-07-23 03:02:07 +0000706
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100707 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
708 /* This code handles two distinct cases:
709 1. Output limit was reached. Save leftover input in unconsumed_tail.
710 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000711 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100712 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000713 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100714 if (new_data == NULL)
715 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300716 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100717 }
Martin Panter84544c12016-07-23 03:02:07 +0000718
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100719 return 0;
720}
721
Larry Hastings61272b72014-01-07 12:41:53 -0800722/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800723zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700724
725 data: Py_buffer
726 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300727 /
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300728 max_length: Py_ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700729 The maximum allowable length of the decompressed data.
730 Unconsumed input data will be stored in
731 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700732
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200733Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700734
735After calling this function, some of the input data may still be stored in
736internal buffers for later processing.
737Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800738[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700739
Larry Hastings31826802013-10-19 00:09:25 -0700740static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400741zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000742 Py_ssize_t max_length)
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300743/*[clinic end generated code: output=6e5173c74e710352 input=0a95d05a3bceaeaa]*/
Larry Hastings31826802013-10-19 00:09:25 -0700744{
Martin Panter84544c12016-07-23 03:02:07 +0000745 int err = Z_OK;
746 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200747 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000748
Martin Panter84544c12016-07-23 03:02:07 +0000749 if (max_length < 0) {
750 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700751 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000752 } else if (max_length == 0)
753 hard_limit = PY_SSIZE_T_MAX;
754 else
755 hard_limit = max_length;
756
757 self->zst.next_in = data->buf;
758 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000759
Jeremy Hylton9714f992001-10-16 21:19:45 +0000760 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000761 if (max_length && obuflen > max_length)
762 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000763
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800764 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000765
Martin Panter84544c12016-07-23 03:02:07 +0000766 do {
767 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000768
Martin Panter84544c12016-07-23 03:02:07 +0000769 do {
770 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
771 obuflen, hard_limit);
772 if (obuflen == -2) {
773 if (max_length > 0) {
774 goto save;
775 }
776 PyErr_NoMemory();
777 }
778 if (obuflen < 0) {
779 goto abort;
780 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000781
Martin Panter84544c12016-07-23 03:02:07 +0000782 Py_BEGIN_ALLOW_THREADS
783 err = inflate(&self->zst, Z_SYNC_FLUSH);
784 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100785
Martin Panter84544c12016-07-23 03:02:07 +0000786 switch (err) {
787 case Z_OK: /* fall through */
788 case Z_BUF_ERROR: /* fall through */
789 case Z_STREAM_END:
790 break;
791 default:
792 if (err == Z_NEED_DICT && self->zdict != NULL) {
793 if (set_inflate_zdict(self) < 0)
794 goto abort;
795 else
796 break;
797 }
798 goto save;
799 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200800
Martin Panter84544c12016-07-23 03:02:07 +0000801 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000802
Martin Panter84544c12016-07-23 03:02:07 +0000803 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000804
Martin Panter84544c12016-07-23 03:02:07 +0000805 save:
806 if (save_unconsumed_input(self, data, err) < 0)
807 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000808
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000809 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100810 /* This is the logical place to call inflateEnd, but the old behaviour
811 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800812 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100813 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000814 /* We will only get Z_BUF_ERROR if the output buffer was full
815 but there wasn't more output when we tried again, so it is
816 not an error condition.
817 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800818 zlib_error(self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000819 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000820 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000821
Martin Panter84544c12016-07-23 03:02:07 +0000822 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
823 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
824 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000825
Martin Panter84544c12016-07-23 03:02:07 +0000826 abort:
827 Py_CLEAR(RetVal);
828 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800829 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000830 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000831}
832
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200833/*[clinic input]
834zlib.Compress.flush
835
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200836 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200837 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
838 If mode == Z_FINISH, the compressor object can no longer be
839 used after calling the flush() method. Otherwise, more data
840 can still be compressed.
841 /
842
843Return a bytes object containing any remaining compressed data.
844[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000845
Guido van Rossumfb221561997-04-29 15:38:09 +0000846static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200847zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200848/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000849{
Victor Stinnere079edd2013-11-21 22:33:21 +0100850 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000851 Py_ssize_t length = DEF_BUF_SIZE;
852 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000853
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
855 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200856 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000857 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000858 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000859
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000860 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000861
Jeremy Hylton9714f992001-10-16 21:19:45 +0000862 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000863
Martin Panter84544c12016-07-23 03:02:07 +0000864 do {
865 length = arrange_output_buffer(&self->zst, &RetVal, length);
866 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200867 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000868 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000869 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000870
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000871 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000872 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000873 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000874
875 if (err == Z_STREAM_ERROR) {
876 zlib_error(self->zst, err, "while flushing");
877 Py_CLEAR(RetVal);
878 goto error;
879 }
880 } while (self->zst.avail_out == 0);
881 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000882
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200883 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000884 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200885 mode is Z_FINISH, but checking both for safety*/
886 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000887 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200889 zlib_error(self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000890 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000891 goto error;
892 }
893 else
894 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 /* We will only get Z_BUF_ERROR if the output buffer was full
897 but there wasn't more output when we tried again, so it is
898 not an error condition.
899 */
Martin Panter84544c12016-07-23 03:02:07 +0000900 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000901 zlib_error(self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000902 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000903 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000904 }
Tim Peters977e5402001-10-17 03:57:20 +0000905
Martin Panter84544c12016-07-23 03:02:07 +0000906 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
907 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200908 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000909
Tim Peters977e5402001-10-17 03:57:20 +0000910 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000911 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000912 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000913}
914
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000915#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700916
Larry Hastings61272b72014-01-07 12:41:53 -0800917/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800918zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700919
920Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800921[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700922
Larry Hastings3cceb382014-01-04 11:09:09 -0800923static PyObject *
924zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800925/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926{
927 compobject *retval = NULL;
928 int err;
929
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100930 retval = newcompobject(_zlibstate_global->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931 if (!retval) return NULL;
932
933 /* Copy the zstream state
934 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
935 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800936 ENTER_ZLIB(self);
937 err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000938 switch (err) {
939 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940 break;
Martin Panter84544c12016-07-23 03:02:07 +0000941 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000942 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
943 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000944 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000945 PyErr_SetString(PyExc_MemoryError,
946 "Can't allocate memory for compression object");
947 goto error;
948 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800949 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950 goto error;
951 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800952 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300953 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800954 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300955 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800956 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300957 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800958 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000959
960 /* Mark it as being initialized */
961 retval->is_initialised = 1;
962
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800963 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 return (PyObject *)retval;
965
966error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800967 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968 Py_XDECREF(retval);
969 return NULL;
970}
971
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200972/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600973zlib.Compress.__copy__
974[clinic start generated code]*/
975
976static PyObject *
977zlib_Compress___copy___impl(compobject *self)
978/*[clinic end generated code: output=1875e6791975442e input=be97a05a788dfd83]*/
979{
980 return zlib_Compress_copy_impl(self);
981}
982
983/*[clinic input]
984zlib.Compress.__deepcopy__
985
986 memo: object
987 /
988
989[clinic start generated code]*/
990
991static PyObject *
992zlib_Compress___deepcopy__(compobject *self, PyObject *memo)
993/*[clinic end generated code: output=f47a2213282c9eb0 input=a9a8b0b40d83388e]*/
994{
995 return zlib_Compress_copy_impl(self);
996}
997
998/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200999zlib.Decompress.copy
1000
1001Return a copy of the decompression object.
1002[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003
1004static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001005zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08001006/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001007{
1008 compobject *retval = NULL;
1009 int err;
1010
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001011 retval = newcompobject(_zlibstate_global->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001012 if (!retval) return NULL;
1013
1014 /* Copy the zstream state
1015 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1016 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001017 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001018 err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001019 switch (err) {
1020 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001021 break;
Martin Panter84544c12016-07-23 03:02:07 +00001022 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001023 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1024 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001025 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026 PyErr_SetString(PyExc_MemoryError,
1027 "Can't allocate memory for decompression object");
1028 goto error;
1029 default:
1030 zlib_error(self->zst, err, "while copying decompression object");
1031 goto error;
1032 }
1033
1034 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001035 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001036 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001037 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001038 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001039 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001040 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001041
1042 /* Mark it as being initialized */
1043 retval->is_initialised = 1;
1044
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001045 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001046 return (PyObject *)retval;
1047
1048error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001049 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050 Py_XDECREF(retval);
1051 return NULL;
1052}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001053
1054/*[clinic input]
1055zlib.Decompress.__copy__
1056[clinic start generated code]*/
1057
1058static PyObject *
1059zlib_Decompress___copy___impl(compobject *self)
1060/*[clinic end generated code: output=80bae8bc43498ad4 input=efcb98b5472c13d2]*/
1061{
1062 return zlib_Decompress_copy_impl(self);
1063}
1064
1065/*[clinic input]
1066zlib.Decompress.__deepcopy__
1067
1068 memo: object
1069 /
1070
1071[clinic start generated code]*/
1072
1073static PyObject *
1074zlib_Decompress___deepcopy__(compobject *self, PyObject *memo)
1075/*[clinic end generated code: output=1f77286ab490124b input=6e99bd0ac4b9cd8b]*/
1076{
1077 return zlib_Decompress_copy_impl(self);
1078}
1079
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001080#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001082/*[clinic input]
1083zlib.Decompress.flush
1084
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001085 length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001086 the initial size of the output buffer.
1087 /
1088
1089Return a bytes object containing any remaining decompressed data.
1090[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001091
Guido van Rossumfb221561997-04-29 15:38:09 +00001092static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +00001093zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001094/*[clinic end generated code: output=68c75ea127cbe654 input=427f2a05a8c2113a]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001095{
Martin Panter84544c12016-07-23 03:02:07 +00001096 int err, flush;
1097 Py_buffer data;
1098 PyObject *RetVal = NULL;
1099 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001100
Martin Panter84544c12016-07-23 03:02:07 +00001101 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001102 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1103 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001104 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001105
Martin Panter84544c12016-07-23 03:02:07 +00001106 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001107 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001108
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001109 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001110
Martin Panter84544c12016-07-23 03:02:07 +00001111 self->zst.next_in = data.buf;
1112 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001113
Martin Panter84544c12016-07-23 03:02:07 +00001114 do {
1115 arrange_input_buffer(&self->zst, &ibuflen);
1116 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001117
Martin Panter84544c12016-07-23 03:02:07 +00001118 do {
1119 length = arrange_output_buffer(&self->zst, &RetVal, length);
1120 if (length < 0)
1121 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001122
Martin Panter84544c12016-07-23 03:02:07 +00001123 Py_BEGIN_ALLOW_THREADS
1124 err = inflate(&self->zst, flush);
1125 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001126
Martin Panter84544c12016-07-23 03:02:07 +00001127 switch (err) {
1128 case Z_OK: /* fall through */
1129 case Z_BUF_ERROR: /* fall through */
1130 case Z_STREAM_END:
1131 break;
1132 default:
1133 if (err == Z_NEED_DICT && self->zdict != NULL) {
1134 if (set_inflate_zdict(self) < 0)
1135 goto abort;
1136 else
1137 break;
1138 }
1139 goto save;
1140 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001141
Martin Panter84544c12016-07-23 03:02:07 +00001142 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1143
1144 } while (err != Z_STREAM_END && ibuflen != 0);
1145
1146 save:
1147 if (save_unconsumed_input(self, &data, err) < 0)
1148 goto abort;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001149
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001150 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001151 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001152 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001153 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001154 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001155 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001156 zlib_error(self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001157 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001158 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001159 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001160
Martin Panter84544c12016-07-23 03:02:07 +00001161 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1162 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1163 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001164
Martin Panter84544c12016-07-23 03:02:07 +00001165 abort:
1166 Py_CLEAR(RetVal);
1167 success:
1168 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001169 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001170 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001171}
1172
Christian Heimes936e2f32014-01-27 01:06:57 +01001173#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001174
Guido van Rossumfb221561997-04-29 15:38:09 +00001175static PyMethodDef comp_methods[] =
1176{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001177 ZLIB_COMPRESS_COMPRESS_METHODDEF
1178 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001179 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001180 ZLIB_COMPRESS___COPY___METHODDEF
1181 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001182 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001183};
1184
1185static PyMethodDef Decomp_methods[] =
1186{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001187 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001188 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001189 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001190 ZLIB_DECOMPRESS___COPY___METHODDEF
1191 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001192 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001193};
1194
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001195#define COMP_OFF(x) offsetof(compobject, x)
1196static PyMemberDef Decomp_members[] = {
1197 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1198 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001199 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001200 {NULL},
1201};
Guido van Rossumfb221561997-04-29 15:38:09 +00001202
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001203/*[clinic input]
1204zlib.adler32
1205
1206 data: Py_buffer
1207 value: unsigned_int(bitwise=True) = 1
1208 Starting value of the checksum.
1209 /
1210
1211Compute an Adler-32 checksum of data.
1212
1213The returned checksum is an integer.
1214[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001215
Guido van Rossumfb221561997-04-29 15:38:09 +00001216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001217zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1218/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001219{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001220 /* Releasing the GIL for very small buffers is inefficient
1221 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001222 if (data->len > 1024*5) {
1223 unsigned char *buf = data->buf;
1224 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001225
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001226 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001227 /* Avoid truncation of length for very large buffers. adler32() takes
1228 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001229 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001230 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001231 buf += (size_t) UINT_MAX;
1232 len -= (size_t) UINT_MAX;
1233 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001234 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001235 Py_END_ALLOW_THREADS
1236 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001237 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001238 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001239 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001240}
Tim Peters977e5402001-10-17 03:57:20 +00001241
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001242/*[clinic input]
1243zlib.crc32
1244
1245 data: Py_buffer
1246 value: unsigned_int(bitwise=True) = 0
1247 Starting value of the checksum.
1248 /
1249
1250Compute a CRC-32 checksum of data.
1251
1252The returned checksum is an integer.
1253[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001254
1255static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001256zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1257/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001258{
Martin v. Löwis423be952008-08-13 15:53:07 +00001259 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001260
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001261 /* Releasing the GIL for very small buffers is inefficient
1262 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001263 if (data->len > 1024*5) {
1264 unsigned char *buf = data->buf;
1265 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001266
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001267 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001268 /* Avoid truncation of length for very large buffers. crc32() takes
1269 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001270 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001271 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001272 buf += (size_t) UINT_MAX;
1273 len -= (size_t) UINT_MAX;
1274 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001275 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001276 Py_END_ALLOW_THREADS
1277 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001278 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001279 }
Christian Heimescc47b052008-03-25 14:56:36 +00001280 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001281}
Tim Peters977e5402001-10-17 03:57:20 +00001282
Guido van Rossumfb221561997-04-29 15:38:09 +00001283
1284static PyMethodDef zlib_methods[] =
1285{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001286 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001287 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001288 ZLIB_COMPRESSOBJ_METHODDEF
1289 ZLIB_CRC32_METHODDEF
1290 ZLIB_DECOMPRESS_METHODDEF
1291 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001292 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001293};
1294
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001295static PyType_Slot Comptype_slots[] = {
1296 {Py_tp_dealloc, Comp_dealloc},
1297 {Py_tp_methods, comp_methods},
1298 {0, 0},
1299};
1300
1301static PyType_Spec Comptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001302 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001303 sizeof(compobject),
1304 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001305 Py_TPFLAGS_DEFAULT,
1306 Comptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001307};
1308
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001309static PyType_Slot Decomptype_slots[] = {
1310 {Py_tp_dealloc, Decomp_dealloc},
1311 {Py_tp_methods, Decomp_methods},
1312 {Py_tp_members, Decomp_members},
1313 {0, 0},
1314};
1315
1316static PyType_Spec Decomptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001317 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001318 sizeof(compobject),
1319 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001320 Py_TPFLAGS_DEFAULT,
1321 Decomptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001322};
1323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001324PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001325"The functions in this module allow compression and decompression using the\n"
1326"zlib library, which is based on GNU zip.\n"
1327"\n"
1328"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001329"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001330"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001331"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001332"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001333"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001334"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001335"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001336"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001338
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001339static int
1340zlib_clear(PyObject *m)
1341{
Hai Shif707d942020-03-16 21:15:01 +08001342 _zlibstate *state = get_zlib_state(m);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001343 Py_CLEAR(state->Comptype);
1344 Py_CLEAR(state->Decomptype);
1345 Py_CLEAR(state->ZlibError);
1346 return 0;
1347}
1348
1349static int
1350zlib_traverse(PyObject *m, visitproc visit, void *arg)
1351{
Hai Shif707d942020-03-16 21:15:01 +08001352 _zlibstate *state = get_zlib_state(m);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001353 Py_VISIT(state->Comptype);
1354 Py_VISIT(state->Decomptype);
1355 Py_VISIT(state->ZlibError);
1356 return 0;
1357}
1358
1359static void
1360zlib_free(void *m)
1361{
1362 zlib_clear((PyObject *)m);
1363}
1364
Martin v. Löwis1a214512008-06-11 05:26:20 +00001365static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001366 PyModuleDef_HEAD_INIT,
1367 "zlib",
1368 zlib_module_documentation,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001369 sizeof(_zlibstate),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001370 zlib_methods,
1371 NULL,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001372 zlib_traverse,
1373 zlib_clear,
1374 zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001375};
1376
Mark Hammond62b1ab12002-07-23 06:31:15 +00001377PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001378PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001379{
Fred Drake4baedc12002-04-01 14:53:37 +00001380 PyObject *m, *ver;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001381 m = PyState_FindModule(&zlibmodule);
1382 if (m != NULL) {
1383 Py_INCREF(m);
1384 return m;
1385 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001386 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001387 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001388 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001389
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001390 PyTypeObject *Comptype = (PyTypeObject *)PyType_FromSpec(&Comptype_spec);
1391 if (Comptype == NULL)
1392 return NULL;
Hai Shif707d942020-03-16 21:15:01 +08001393 get_zlib_state(m)->Comptype = Comptype;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001394
1395 PyTypeObject *Decomptype = (PyTypeObject *)PyType_FromSpec(&Decomptype_spec);
1396 if (Decomptype == NULL)
1397 return NULL;
Hai Shif707d942020-03-16 21:15:01 +08001398 get_zlib_state(m)->Decomptype = Decomptype;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001399
1400 PyObject *ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Fred Drake4baedc12002-04-01 14:53:37 +00001401 if (ZlibError != NULL) {
1402 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001403 PyModule_AddObject(m, "error", ZlibError);
Hai Shif707d942020-03-16 21:15:01 +08001404 get_zlib_state(m)->ZlibError = ZlibError;
Fred Drake4baedc12002-04-01 14:53:37 +00001405 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001406 PyModule_AddIntMacro(m, MAX_WBITS);
1407 PyModule_AddIntMacro(m, DEFLATED);
1408 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001409 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001410 // compression levels
1411 PyModule_AddIntMacro(m, Z_NO_COMPRESSION);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001412 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1413 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1414 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001415 // compression strategies
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001416 PyModule_AddIntMacro(m, Z_FILTERED);
1417 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001418#ifdef Z_RLE // 1.2.0.1
1419 PyModule_AddIntMacro(m, Z_RLE);
1420#endif
1421#ifdef Z_FIXED // 1.2.2.2
1422 PyModule_AddIntMacro(m, Z_FIXED);
1423#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001424 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001425 // allowed flush values
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001426 PyModule_AddIntMacro(m, Z_NO_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001427 PyModule_AddIntMacro(m, Z_PARTIAL_FLUSH);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001428 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1429 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001430 PyModule_AddIntMacro(m, Z_FINISH);
1431#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1432 PyModule_AddIntMacro(m, Z_BLOCK);
1433#endif
1434#ifdef Z_TREES // 1.2.3.4, only for inflate
1435 PyModule_AddIntMacro(m, Z_TREES);
1436#endif
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001437 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001438 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001439 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001440
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001441 ver = PyUnicode_FromString(zlibVersion());
1442 if (ver != NULL)
1443 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1444
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001445 PyModule_AddStringConstant(m, "__version__", "1.0");
1446
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001447 PyState_AddModule(m, &zlibmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001448 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001449}