blob: 5778dbb715f92c4acd43bd80764beba394ed3c85 [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
Jeremy Hylton938ace62002-07-17 16:30:39 +000035static PyTypeObject Comptype;
36static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000037
38static PyObject *ZlibError;
39
Tim Peters977e5402001-10-17 03:57:20 +000040typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000041{
Jeremy Hylton9714f992001-10-16 21:19:45 +000042 PyObject_HEAD
43 z_stream zst;
44 PyObject *unused_data;
45 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020046 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000047 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020048 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020049 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000050} compobject;
51
Jeremy Hylton0965e082001-10-16 21:56:09 +000052static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020053zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000054{
Nadeem Vawda524148a2011-08-28 11:26:46 +020055 const char *zmsg = Z_NULL;
56 /* In case of a version mismatch, zst.msg won't be initialized.
57 Check for this case first, before looking at zst.msg. */
58 if (err == Z_VERSION_ERROR)
59 zmsg = "library version mismatch";
60 if (zmsg == Z_NULL)
61 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000062 if (zmsg == Z_NULL) {
63 switch (err) {
64 case Z_BUF_ERROR:
65 zmsg = "incomplete or truncated stream";
66 break;
67 case Z_STREAM_ERROR:
68 zmsg = "inconsistent stream state";
69 break;
70 case Z_DATA_ERROR:
71 zmsg = "invalid input data";
72 break;
73 }
74 }
75 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000076 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000077 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000078 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000079}
80
Larry Hastings61272b72014-01-07 12:41:53 -080081/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080082module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080083class zlib.Compress "compobject *" "&Comptype"
84class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080085[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030086/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080087
Guido van Rossumfb221561997-04-29 15:38:09 +000088static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000089newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000090{
Tim Peters977e5402001-10-17 03:57:20 +000091 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000092 self = PyObject_New(compobject, type);
93 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000094 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020095 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +000096 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020097 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +000098 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 Py_DECREF(self);
101 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000103 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000105 Py_DECREF(self);
106 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000107 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000108 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200109 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000110 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200111 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
112 return NULL;
113 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000115}
116
Victor Stinner5064a522013-07-07 16:50:27 +0200117static void*
118PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
119{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300120 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200121 return NULL;
122 /* PyMem_Malloc() cannot be used: the GIL is not held when
123 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300124 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200125}
126
127static void
128PyZlib_Free(voidpf ctx, void *ptr)
129{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200130 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200131}
132
Martin Panter84544c12016-07-23 03:02:07 +0000133static void
134arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
135{
Segev Finer679b5662017-07-27 01:17:57 +0300136 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000137 *remains -= zst->avail_in;
138}
139
140static Py_ssize_t
141arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
142 Py_ssize_t length,
143 Py_ssize_t max_length)
144{
145 Py_ssize_t occupied;
146
147 if (*buffer == NULL) {
148 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
149 return -1;
150 occupied = 0;
151 }
152 else {
153 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
154
155 if (length == occupied) {
156 Py_ssize_t new_length;
157 assert(length <= max_length);
158 /* can not scale the buffer over max_length */
159 if (length == max_length)
160 return -2;
161 if (length <= (max_length >> 1))
162 new_length = length << 1;
163 else
164 new_length = max_length;
165 if (_PyBytes_Resize(buffer, new_length) < 0)
166 return -1;
167 length = new_length;
168 }
169 }
170
Segev Finer679b5662017-07-27 01:17:57 +0300171 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000172 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
173
174 return length;
175}
176
177static Py_ssize_t
178arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
179{
180 Py_ssize_t ret;
181
182 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
183 PY_SSIZE_T_MAX);
184 if (ret == -2)
185 PyErr_NoMemory();
186
187 return ret;
188}
189
Larry Hastings61272b72014-01-07 12:41:53 -0800190/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800191zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200192
Martin Panter1fe0d132016-02-10 10:06:36 +0000193 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800194 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300195 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200196 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000197 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800198
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200199Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800200[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800201
Guido van Rossumfb221561997-04-29 15:38:09 +0000202static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300203zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
204/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000205{
Martin Panter84544c12016-07-23 03:02:07 +0000206 PyObject *RetVal = NULL;
207 Byte *ibuf;
208 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
209 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000210 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000211
Martin Panter525a9492016-07-23 03:39:49 +0000212 ibuf = data->buf;
213 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000214
Victor Stinner5064a522013-07-07 16:50:27 +0200215 zst.opaque = NULL;
216 zst.zalloc = PyZlib_Malloc;
217 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000218 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000219 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000220
Martin Panter84544c12016-07-23 03:02:07 +0000221 switch (err) {
222 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000223 break;
Martin Panter84544c12016-07-23 03:02:07 +0000224 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000225 PyErr_SetString(PyExc_MemoryError,
226 "Out of memory while compressing data");
227 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000228 case Z_STREAM_ERROR:
229 PyErr_SetString(ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000230 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000231 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000232 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000233 zlib_error(zst, err, "while compressing data");
234 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000235 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000236
Martin Panter84544c12016-07-23 03:02:07 +0000237 do {
238 arrange_input_buffer(&zst, &ibuflen);
239 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000240
Martin Panter84544c12016-07-23 03:02:07 +0000241 do {
242 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
243 if (obuflen < 0) {
244 deflateEnd(&zst);
245 goto error;
246 }
247
248 Py_BEGIN_ALLOW_THREADS
249 err = deflate(&zst, flush);
250 Py_END_ALLOW_THREADS
251
252 if (err == Z_STREAM_ERROR) {
253 deflateEnd(&zst);
254 zlib_error(zst, err, "while compressing data");
255 goto error;
256 }
257
258 } while (zst.avail_out == 0);
259 assert(zst.avail_in == 0);
260
261 } while (flush != Z_FINISH);
262 assert(err == Z_STREAM_END);
263
264 err = deflateEnd(&zst);
265 if (err == Z_OK) {
266 if (_PyBytes_Resize(&RetVal, zst.next_out -
267 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
268 goto error;
269 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000270 }
Tim Peters977e5402001-10-17 03:57:20 +0000271 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000273 error:
Martin Panter84544c12016-07-23 03:02:07 +0000274 Py_XDECREF(RetVal);
275 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000276}
277
Larry Hastings61272b72014-01-07 12:41:53 -0800278/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100279
Martin Panter84544c12016-07-23 03:02:07 +0000280class ssize_t_converter(CConverter):
281 type = 'Py_ssize_t'
282 converter = 'ssize_t_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200283 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100284
Larry Hastings61272b72014-01-07 12:41:53 -0800285[python start generated code]*/
Martin Panter84544c12016-07-23 03:02:07 +0000286/*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100287
288static int
Martin Panter84544c12016-07-23 03:02:07 +0000289ssize_t_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100290{
Martin Pantere99e9772015-11-20 08:13:35 +0000291 PyObject *long_obj;
292 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100293
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200294 /* XXX Should be replaced with PyNumber_AsSsize_t after the end of the
295 deprecation period. */
296 long_obj = _PyLong_FromNbIndexOrNbInt(obj);
Martin Pantere99e9772015-11-20 08:13:35 +0000297 if (long_obj == NULL) {
298 return 0;
299 }
300 val = PyLong_AsSsize_t(long_obj);
301 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100302 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000303 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100304 }
Martin Panter84544c12016-07-23 03:02:07 +0000305 *(Py_ssize_t *)ptr = val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100306 return 1;
307}
308
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200309/*[clinic input]
310zlib.decompress
311
312 data: Py_buffer
313 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300314 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200315 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000316 The window buffer size and container format.
Martin Panter84544c12016-07-23 03:02:07 +0000317 bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200318 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200319
320Returns a bytes object containing the uncompressed data.
321[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000322
Guido van Rossumfb221561997-04-29 15:38:09 +0000323static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300324zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000325 Py_ssize_t bufsize)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300326/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000327{
Martin Panter84544c12016-07-23 03:02:07 +0000328 PyObject *RetVal = NULL;
329 Byte *ibuf;
330 Py_ssize_t ibuflen;
331 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000332 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000333
Martin Panter84544c12016-07-23 03:02:07 +0000334 if (bufsize < 0) {
335 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
336 return NULL;
337 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100338 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000339 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000340
Martin Panter84544c12016-07-23 03:02:07 +0000341 ibuf = data->buf;
342 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000343
Victor Stinner5064a522013-07-07 16:50:27 +0200344 zst.opaque = NULL;
345 zst.zalloc = PyZlib_Malloc;
346 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000347 zst.avail_in = 0;
348 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200349 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000350
Martin Panter84544c12016-07-23 03:02:07 +0000351 switch (err) {
352 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000353 break;
Martin Panter84544c12016-07-23 03:02:07 +0000354 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000355 PyErr_SetString(PyExc_MemoryError,
356 "Out of memory while decompressing data");
357 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000358 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000359 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000360 zlib_error(zst, err, "while preparing to decompress data");
361 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000362 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000363
Jeremy Hylton9714f992001-10-16 21:19:45 +0000364 do {
Martin Panter84544c12016-07-23 03:02:07 +0000365 arrange_input_buffer(&zst, &ibuflen);
366 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000367
Martin Panter84544c12016-07-23 03:02:07 +0000368 do {
369 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
370 if (bufsize < 0) {
371 inflateEnd(&zst);
372 goto error;
373 }
374
375 Py_BEGIN_ALLOW_THREADS
376 err = inflate(&zst, flush);
377 Py_END_ALLOW_THREADS
378
379 switch (err) {
380 case Z_OK: /* fall through */
381 case Z_BUF_ERROR: /* fall through */
382 case Z_STREAM_END:
383 break;
384 case Z_MEM_ERROR:
385 inflateEnd(&zst);
386 PyErr_SetString(PyExc_MemoryError,
387 "Out of memory while decompressing data");
388 goto error;
389 default:
390 inflateEnd(&zst);
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000391 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000392 goto error;
393 }
Martin Panter84544c12016-07-23 03:02:07 +0000394
395 } while (zst.avail_out == 0);
396
397 } while (err != Z_STREAM_END && ibuflen != 0);
398
399
400 if (err != Z_STREAM_END) {
401 inflateEnd(&zst);
402 zlib_error(zst, err, "while decompressing data");
403 goto error;
404 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000405
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000406 err = inflateEnd(&zst);
407 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200408 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000409 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000410 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000411
Martin Panter84544c12016-07-23 03:02:07 +0000412 if (_PyBytes_Resize(&RetVal, zst.next_out -
413 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000414 goto error;
415
Martin Panter84544c12016-07-23 03:02:07 +0000416 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000417
418 error:
Martin Panter84544c12016-07-23 03:02:07 +0000419 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000420 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000421}
422
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200423/*[clinic input]
424zlib.compressobj
425
426 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000427 The compression level (an integer in the range 0-9 or -1; default is
428 currently equivalent to 6). Higher compression levels are slower,
429 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200430 method: int(c_default="DEFLATED") = DEFLATED
431 The compression algorithm. If given, this must be DEFLATED.
432 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000433 +9 to +15: The base-two logarithm of the window size. Include a zlib
434 container.
435 -9 to -15: Generate a raw stream.
436 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200437 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
438 Controls the amount of memory used for internal compression state.
439 Valid values range from 1 to 9. Higher values result in higher memory
440 usage, faster compression, and smaller output.
441 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
442 Used to tune the compression algorithm. Possible values are
443 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
444 zdict: Py_buffer = None
445 The predefined compression dictionary - a sequence of bytes
446 containing subsequences that are likely to occur in the input data.
447
448Return a compressor object.
449[clinic start generated code]*/
450
Guido van Rossumfb221561997-04-29 15:38:09 +0000451static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300452zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400453 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300454/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000455{
Victor Stinnere079edd2013-11-21 22:33:21 +0100456 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200457 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000458
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200459 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100460 PyErr_SetString(PyExc_OverflowError,
461 "zdict length does not fit in an unsigned int");
462 goto error;
463 }
464
Jeremy Hylton499000002001-10-16 21:59:35 +0000465 self = newcompobject(&Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000466 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200467 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200468 self->zst.opaque = NULL;
469 self->zst.zalloc = PyZlib_Malloc;
470 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000471 self->zst.next_in = NULL;
472 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000473 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000474 switch (err) {
475 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000476 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200477 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200478 goto success;
479 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100480 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200481 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200482 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000483 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000485 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200486 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
487 goto error;
488 default:
489 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
490 goto error;
491 }
492 }
Martin Panter84544c12016-07-23 03:02:07 +0000493 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000494 PyErr_SetString(PyExc_MemoryError,
495 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200496 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000497 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000498 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200499 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000500 default:
501 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200502 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000503 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200504
505 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200506 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200507 success:
Martin Panter84544c12016-07-23 03:02:07 +0000508 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000509}
510
Martin Panter3f0ee832016-06-05 10:48:34 +0000511static int
512set_inflate_zdict(compobject *self)
513{
514 Py_buffer zdict_buf;
515 int err;
516
517 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
518 return -1;
519 }
520 if ((size_t)zdict_buf.len > UINT_MAX) {
521 PyErr_SetString(PyExc_OverflowError,
522 "zdict length does not fit in an unsigned int");
523 PyBuffer_Release(&zdict_buf);
524 return -1;
525 }
Martin Panter84544c12016-07-23 03:02:07 +0000526 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000527 zdict_buf.buf, (unsigned int)zdict_buf.len);
528 PyBuffer_Release(&zdict_buf);
529 if (err != Z_OK) {
530 zlib_error(self->zst, err, "while setting zdict");
531 return -1;
532 }
533 return 0;
534}
535
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200536/*[clinic input]
537zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200538
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200539 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000540 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200541 zdict: object(c_default="NULL") = b''
542 The predefined compression dictionary. This must be the same
543 dictionary as used by the compressor that produced the input data.
544
545Return a decompressor object.
546[clinic start generated code]*/
547
548static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300549zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
550/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200551{
552 int err;
553 compobject *self;
554
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200555 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
556 PyErr_SetString(PyExc_TypeError,
557 "zdict argument must support the buffer protocol");
558 return NULL;
559 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000560
561 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000562 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000563 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200564 self->zst.opaque = NULL;
565 self->zst.zalloc = PyZlib_Malloc;
566 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000567 self->zst.next_in = NULL;
568 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200569 if (zdict != NULL) {
570 Py_INCREF(zdict);
571 self->zdict = zdict;
572 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000573 err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000574 switch (err) {
575 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000576 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000577 if (self->zdict != NULL && wbits < 0) {
578#ifdef AT_LEAST_ZLIB_1_2_2_1
579 if (set_inflate_zdict(self) < 0) {
580 Py_DECREF(self);
581 return NULL;
582 }
583#else
584 PyErr_Format(ZlibError,
585 "zlib version %s does not allow raw inflate with dictionary",
586 ZLIB_VERSION);
587 Py_DECREF(self);
588 return NULL;
589#endif
590 }
Martin Panter84544c12016-07-23 03:02:07 +0000591 return (PyObject *)self;
592 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000593 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000594 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
595 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000596 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 Py_DECREF(self);
598 PyErr_SetString(PyExc_MemoryError,
599 "Can't allocate memory for decompression object");
600 return NULL;
601 default:
602 zlib_error(self->zst, err, "while creating decompression object");
603 Py_DECREF(self);
604 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000605 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000606}
607
608static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000609Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000610{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000611 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000612 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000613 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200614 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000615 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000616}
617
618static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000619Comp_dealloc(compobject *self)
620{
621 if (self->is_initialised)
622 deflateEnd(&self->zst);
623 Dealloc(self);
624}
625
626static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000627Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000628{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000629 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000630 inflateEnd(&self->zst);
631 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000632}
633
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200634/*[clinic input]
635zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000636
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200637 data: Py_buffer
638 Binary data to be compressed.
639 /
640
641Returns a bytes object containing compressed data.
642
643After calling this function, some of the input data may still
644be stored in internal buffers for later processing.
645Call the flush() method to clear these buffers.
646[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000647
Guido van Rossumfb221561997-04-29 15:38:09 +0000648static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200649zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800650/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000651{
Martin Panter84544c12016-07-23 03:02:07 +0000652 PyObject *RetVal = NULL;
653 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200654 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000655
Martin Panter84544c12016-07-23 03:02:07 +0000656 self->zst.next_in = data->buf;
657 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000658
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000659 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000660
Martin Panter84544c12016-07-23 03:02:07 +0000661 do {
662 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000663
Martin Panter84544c12016-07-23 03:02:07 +0000664 do {
665 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
666 if (obuflen < 0)
667 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000668
Martin Panter84544c12016-07-23 03:02:07 +0000669 Py_BEGIN_ALLOW_THREADS
670 err = deflate(&self->zst, Z_NO_FLUSH);
671 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000672
Martin Panter84544c12016-07-23 03:02:07 +0000673 if (err == Z_STREAM_ERROR) {
674 zlib_error(self->zst, err, "while compressing data");
675 goto error;
676 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000677
Martin Panter84544c12016-07-23 03:02:07 +0000678 } while (self->zst.avail_out == 0);
679 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000680
Martin Panter84544c12016-07-23 03:02:07 +0000681 } while (ibuflen != 0);
682
683 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
684 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
685 goto success;
686
687 error:
688 Py_CLEAR(RetVal);
689 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000690 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000691 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000692}
693
Martin Panter84544c12016-07-23 03:02:07 +0000694/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100695 self->unused_data or self->unconsumed_tail, as appropriate. */
696static int
Martin Panter84544c12016-07-23 03:02:07 +0000697save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100698{
699 if (err == Z_STREAM_END) {
700 /* The end of the compressed data has been reached. Store the leftover
701 input data in self->unused_data. */
702 if (self->zst.avail_in > 0) {
703 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000704 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100705 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000706 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
707 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100708 PyErr_NoMemory();
709 return -1;
710 }
Martin Panter84544c12016-07-23 03:02:07 +0000711 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100712 new_data = PyBytes_FromStringAndSize(NULL, new_size);
713 if (new_data == NULL)
714 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200715 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100716 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200717 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000718 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300719 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100720 self->zst.avail_in = 0;
721 }
722 }
Martin Panter84544c12016-07-23 03:02:07 +0000723
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100724 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
725 /* This code handles two distinct cases:
726 1. Output limit was reached. Save leftover input in unconsumed_tail.
727 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000728 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100729 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000730 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100731 if (new_data == NULL)
732 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300733 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100734 }
Martin Panter84544c12016-07-23 03:02:07 +0000735
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100736 return 0;
737}
738
Larry Hastings61272b72014-01-07 12:41:53 -0800739/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800740zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700741
742 data: Py_buffer
743 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300744 /
Martin Panter84544c12016-07-23 03:02:07 +0000745 max_length: ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700746 The maximum allowable length of the decompressed data.
747 Unconsumed input data will be stored in
748 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700749
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200750Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700751
752After calling this function, some of the input data may still be stored in
753internal buffers for later processing.
754Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800755[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700756
Larry Hastings31826802013-10-19 00:09:25 -0700757static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400758zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000759 Py_ssize_t max_length)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300760/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
Larry Hastings31826802013-10-19 00:09:25 -0700761{
Martin Panter84544c12016-07-23 03:02:07 +0000762 int err = Z_OK;
763 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200764 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000765
Martin Panter84544c12016-07-23 03:02:07 +0000766 if (max_length < 0) {
767 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700768 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000769 } else if (max_length == 0)
770 hard_limit = PY_SSIZE_T_MAX;
771 else
772 hard_limit = max_length;
773
774 self->zst.next_in = data->buf;
775 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000776
Jeremy Hylton9714f992001-10-16 21:19:45 +0000777 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000778 if (max_length && obuflen > max_length)
779 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000780
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800781 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000782
Martin Panter84544c12016-07-23 03:02:07 +0000783 do {
784 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000785
Martin Panter84544c12016-07-23 03:02:07 +0000786 do {
787 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
788 obuflen, hard_limit);
789 if (obuflen == -2) {
790 if (max_length > 0) {
791 goto save;
792 }
793 PyErr_NoMemory();
794 }
795 if (obuflen < 0) {
796 goto abort;
797 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000798
Martin Panter84544c12016-07-23 03:02:07 +0000799 Py_BEGIN_ALLOW_THREADS
800 err = inflate(&self->zst, Z_SYNC_FLUSH);
801 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100802
Martin Panter84544c12016-07-23 03:02:07 +0000803 switch (err) {
804 case Z_OK: /* fall through */
805 case Z_BUF_ERROR: /* fall through */
806 case Z_STREAM_END:
807 break;
808 default:
809 if (err == Z_NEED_DICT && self->zdict != NULL) {
810 if (set_inflate_zdict(self) < 0)
811 goto abort;
812 else
813 break;
814 }
815 goto save;
816 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200817
Martin Panter84544c12016-07-23 03:02:07 +0000818 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000819
Martin Panter84544c12016-07-23 03:02:07 +0000820 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000821
Martin Panter84544c12016-07-23 03:02:07 +0000822 save:
823 if (save_unconsumed_input(self, data, err) < 0)
824 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000825
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000826 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100827 /* This is the logical place to call inflateEnd, but the old behaviour
828 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800829 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100830 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000831 /* We will only get Z_BUF_ERROR if the output buffer was full
832 but there wasn't more output when we tried again, so it is
833 not an error condition.
834 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800835 zlib_error(self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000836 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000837 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000838
Martin Panter84544c12016-07-23 03:02:07 +0000839 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
840 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
841 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000842
Martin Panter84544c12016-07-23 03:02:07 +0000843 abort:
844 Py_CLEAR(RetVal);
845 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800846 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000847 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000848}
849
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200850/*[clinic input]
851zlib.Compress.flush
852
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200853 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200854 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
855 If mode == Z_FINISH, the compressor object can no longer be
856 used after calling the flush() method. Otherwise, more data
857 can still be compressed.
858 /
859
860Return a bytes object containing any remaining compressed data.
861[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000862
Guido van Rossumfb221561997-04-29 15:38:09 +0000863static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200864zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200865/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000866{
Victor Stinnere079edd2013-11-21 22:33:21 +0100867 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000868 Py_ssize_t length = DEF_BUF_SIZE;
869 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000870
Jeremy Hylton9714f992001-10-16 21:19:45 +0000871 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
872 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200873 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000874 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000875 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000876
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000877 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000878
Jeremy Hylton9714f992001-10-16 21:19:45 +0000879 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000880
Martin Panter84544c12016-07-23 03:02:07 +0000881 do {
882 length = arrange_output_buffer(&self->zst, &RetVal, length);
883 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200884 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000886 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000887
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000889 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000891
892 if (err == Z_STREAM_ERROR) {
893 zlib_error(self->zst, err, "while flushing");
894 Py_CLEAR(RetVal);
895 goto error;
896 }
897 } while (self->zst.avail_out == 0);
898 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000899
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200900 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000901 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200902 mode is Z_FINISH, but checking both for safety*/
903 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000904 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000905 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200906 zlib_error(self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000907 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 goto error;
909 }
910 else
911 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000912
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000913 /* We will only get Z_BUF_ERROR if the output buffer was full
914 but there wasn't more output when we tried again, so it is
915 not an error condition.
916 */
Martin Panter84544c12016-07-23 03:02:07 +0000917 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000918 zlib_error(self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000919 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000920 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000921 }
Tim Peters977e5402001-10-17 03:57:20 +0000922
Martin Panter84544c12016-07-23 03:02:07 +0000923 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
924 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200925 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000926
Tim Peters977e5402001-10-17 03:57:20 +0000927 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000928 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000929 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000930}
931
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000932#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700933
Larry Hastings61272b72014-01-07 12:41:53 -0800934/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800935zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700936
937Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800938[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700939
Larry Hastings3cceb382014-01-04 11:09:09 -0800940static PyObject *
941zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800942/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943{
944 compobject *retval = NULL;
945 int err;
946
947 retval = newcompobject(&Comptype);
948 if (!retval) return NULL;
949
950 /* Copy the zstream state
951 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
952 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800953 ENTER_ZLIB(self);
954 err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000955 switch (err) {
956 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957 break;
Martin Panter84544c12016-07-23 03:02:07 +0000958 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000959 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
960 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000961 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962 PyErr_SetString(PyExc_MemoryError,
963 "Can't allocate memory for compression object");
964 goto error;
965 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800966 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000967 goto error;
968 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800969 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300970 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800971 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300972 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800973 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300974 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800975 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976
977 /* Mark it as being initialized */
978 retval->is_initialised = 1;
979
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800980 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000981 return (PyObject *)retval;
982
983error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800984 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000985 Py_XDECREF(retval);
986 return NULL;
987}
988
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200989/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600990zlib.Compress.__copy__
991[clinic start generated code]*/
992
993static PyObject *
994zlib_Compress___copy___impl(compobject *self)
995/*[clinic end generated code: output=1875e6791975442e input=be97a05a788dfd83]*/
996{
997 return zlib_Compress_copy_impl(self);
998}
999
1000/*[clinic input]
1001zlib.Compress.__deepcopy__
1002
1003 memo: object
1004 /
1005
1006[clinic start generated code]*/
1007
1008static PyObject *
1009zlib_Compress___deepcopy__(compobject *self, PyObject *memo)
1010/*[clinic end generated code: output=f47a2213282c9eb0 input=a9a8b0b40d83388e]*/
1011{
1012 return zlib_Compress_copy_impl(self);
1013}
1014
1015/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001016zlib.Decompress.copy
1017
1018Return a copy of the decompression object.
1019[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020
1021static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001022zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08001023/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024{
1025 compobject *retval = NULL;
1026 int err;
1027
1028 retval = newcompobject(&Decomptype);
1029 if (!retval) return NULL;
1030
1031 /* Copy the zstream state
1032 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1033 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001034 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035 err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001036 switch (err) {
1037 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038 break;
Martin Panter84544c12016-07-23 03:02:07 +00001039 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1041 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001042 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043 PyErr_SetString(PyExc_MemoryError,
1044 "Can't allocate memory for decompression object");
1045 goto error;
1046 default:
1047 zlib_error(self->zst, err, "while copying decompression object");
1048 goto error;
1049 }
1050
1051 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001052 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001054 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001055 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001056 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001057 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058
1059 /* Mark it as being initialized */
1060 retval->is_initialised = 1;
1061
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001062 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063 return (PyObject *)retval;
1064
1065error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001066 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 Py_XDECREF(retval);
1068 return NULL;
1069}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001070
1071/*[clinic input]
1072zlib.Decompress.__copy__
1073[clinic start generated code]*/
1074
1075static PyObject *
1076zlib_Decompress___copy___impl(compobject *self)
1077/*[clinic end generated code: output=80bae8bc43498ad4 input=efcb98b5472c13d2]*/
1078{
1079 return zlib_Decompress_copy_impl(self);
1080}
1081
1082/*[clinic input]
1083zlib.Decompress.__deepcopy__
1084
1085 memo: object
1086 /
1087
1088[clinic start generated code]*/
1089
1090static PyObject *
1091zlib_Decompress___deepcopy__(compobject *self, PyObject *memo)
1092/*[clinic end generated code: output=1f77286ab490124b input=6e99bd0ac4b9cd8b]*/
1093{
1094 return zlib_Decompress_copy_impl(self);
1095}
1096
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001097#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001098
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001099/*[clinic input]
1100zlib.Decompress.flush
1101
Martin Panter84544c12016-07-23 03:02:07 +00001102 length: ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001103 the initial size of the output buffer.
1104 /
1105
1106Return a bytes object containing any remaining decompressed data.
1107[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001108
Guido van Rossumfb221561997-04-29 15:38:09 +00001109static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +00001110zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1111/*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001112{
Martin Panter84544c12016-07-23 03:02:07 +00001113 int err, flush;
1114 Py_buffer data;
1115 PyObject *RetVal = NULL;
1116 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001117
Martin Panter84544c12016-07-23 03:02:07 +00001118 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001119 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1120 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001121 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001122
Martin Panter84544c12016-07-23 03:02:07 +00001123 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001124 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001125
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001126 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001127
Martin Panter84544c12016-07-23 03:02:07 +00001128 self->zst.next_in = data.buf;
1129 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001130
Martin Panter84544c12016-07-23 03:02:07 +00001131 do {
1132 arrange_input_buffer(&self->zst, &ibuflen);
1133 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001134
Martin Panter84544c12016-07-23 03:02:07 +00001135 do {
1136 length = arrange_output_buffer(&self->zst, &RetVal, length);
1137 if (length < 0)
1138 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001139
Martin Panter84544c12016-07-23 03:02:07 +00001140 Py_BEGIN_ALLOW_THREADS
1141 err = inflate(&self->zst, flush);
1142 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001143
Martin Panter84544c12016-07-23 03:02:07 +00001144 switch (err) {
1145 case Z_OK: /* fall through */
1146 case Z_BUF_ERROR: /* fall through */
1147 case Z_STREAM_END:
1148 break;
1149 default:
1150 if (err == Z_NEED_DICT && self->zdict != NULL) {
1151 if (set_inflate_zdict(self) < 0)
1152 goto abort;
1153 else
1154 break;
1155 }
1156 goto save;
1157 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001158
Martin Panter84544c12016-07-23 03:02:07 +00001159 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1160
1161 } while (err != Z_STREAM_END && ibuflen != 0);
1162
1163 save:
1164 if (save_unconsumed_input(self, &data, err) < 0)
1165 goto abort;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001166
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001167 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001168 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001169 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001170 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001171 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001172 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001173 zlib_error(self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001174 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001175 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001176 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001177
Martin Panter84544c12016-07-23 03:02:07 +00001178 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1179 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1180 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001181
Martin Panter84544c12016-07-23 03:02:07 +00001182 abort:
1183 Py_CLEAR(RetVal);
1184 success:
1185 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001186 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001187 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001188}
1189
Christian Heimes936e2f32014-01-27 01:06:57 +01001190#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001191
Guido van Rossumfb221561997-04-29 15:38:09 +00001192static PyMethodDef comp_methods[] =
1193{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001194 ZLIB_COMPRESS_COMPRESS_METHODDEF
1195 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001196 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001197 ZLIB_COMPRESS___COPY___METHODDEF
1198 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001199 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001200};
1201
1202static PyMethodDef Decomp_methods[] =
1203{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001204 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001205 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001206 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001207 ZLIB_DECOMPRESS___COPY___METHODDEF
1208 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001209 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001210};
1211
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001212#define COMP_OFF(x) offsetof(compobject, x)
1213static PyMemberDef Decomp_members[] = {
1214 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1215 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001216 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001217 {NULL},
1218};
Guido van Rossumfb221561997-04-29 15:38:09 +00001219
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001220/*[clinic input]
1221zlib.adler32
1222
1223 data: Py_buffer
1224 value: unsigned_int(bitwise=True) = 1
1225 Starting value of the checksum.
1226 /
1227
1228Compute an Adler-32 checksum of data.
1229
1230The returned checksum is an integer.
1231[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001232
Guido van Rossumfb221561997-04-29 15:38:09 +00001233static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001234zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1235/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001236{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001237 /* Releasing the GIL for very small buffers is inefficient
1238 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001239 if (data->len > 1024*5) {
1240 unsigned char *buf = data->buf;
1241 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001242
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001243 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001244 /* Avoid truncation of length for very large buffers. adler32() takes
1245 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001246 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001247 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001248 buf += (size_t) UINT_MAX;
1249 len -= (size_t) UINT_MAX;
1250 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001251 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001252 Py_END_ALLOW_THREADS
1253 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001254 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001255 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001256 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001257}
Tim Peters977e5402001-10-17 03:57:20 +00001258
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001259/*[clinic input]
1260zlib.crc32
1261
1262 data: Py_buffer
1263 value: unsigned_int(bitwise=True) = 0
1264 Starting value of the checksum.
1265 /
1266
1267Compute a CRC-32 checksum of data.
1268
1269The returned checksum is an integer.
1270[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001271
1272static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001273zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1274/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001275{
Martin v. Löwis423be952008-08-13 15:53:07 +00001276 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001277
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001278 /* Releasing the GIL for very small buffers is inefficient
1279 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001280 if (data->len > 1024*5) {
1281 unsigned char *buf = data->buf;
1282 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001283
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001284 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001285 /* Avoid truncation of length for very large buffers. crc32() takes
1286 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001287 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001288 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001289 buf += (size_t) UINT_MAX;
1290 len -= (size_t) UINT_MAX;
1291 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001292 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001293 Py_END_ALLOW_THREADS
1294 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001295 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001296 }
Christian Heimescc47b052008-03-25 14:56:36 +00001297 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001298}
Tim Peters977e5402001-10-17 03:57:20 +00001299
Guido van Rossumfb221561997-04-29 15:38:09 +00001300
1301static PyMethodDef zlib_methods[] =
1302{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001303 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001304 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001305 ZLIB_COMPRESSOBJ_METHODDEF
1306 ZLIB_CRC32_METHODDEF
1307 ZLIB_DECOMPRESS_METHODDEF
1308 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001309 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001310};
1311
Tim Peters0c322792002-07-17 16:49:03 +00001312static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001313 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001314 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001315 sizeof(compobject),
1316 0,
1317 (destructor)Comp_dealloc, /*tp_dealloc*/
1318 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001319 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001320 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001321 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001322 0, /*tp_repr*/
1323 0, /*tp_as_number*/
1324 0, /*tp_as_sequence*/
1325 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001326 0, /*tp_hash*/
1327 0, /*tp_call*/
1328 0, /*tp_str*/
1329 0, /*tp_getattro*/
1330 0, /*tp_setattro*/
1331 0, /*tp_as_buffer*/
1332 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1333 0, /*tp_doc*/
1334 0, /*tp_traverse*/
1335 0, /*tp_clear*/
1336 0, /*tp_richcompare*/
1337 0, /*tp_weaklistoffset*/
1338 0, /*tp_iter*/
1339 0, /*tp_iternext*/
1340 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001341};
1342
Tim Peters0c322792002-07-17 16:49:03 +00001343static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001344 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001345 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001346 sizeof(compobject),
1347 0,
1348 (destructor)Decomp_dealloc, /*tp_dealloc*/
1349 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001350 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001351 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001352 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001353 0, /*tp_repr*/
1354 0, /*tp_as_number*/
1355 0, /*tp_as_sequence*/
1356 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001357 0, /*tp_hash*/
1358 0, /*tp_call*/
1359 0, /*tp_str*/
1360 0, /*tp_getattro*/
1361 0, /*tp_setattro*/
1362 0, /*tp_as_buffer*/
1363 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1364 0, /*tp_doc*/
1365 0, /*tp_traverse*/
1366 0, /*tp_clear*/
1367 0, /*tp_richcompare*/
1368 0, /*tp_weaklistoffset*/
1369 0, /*tp_iter*/
1370 0, /*tp_iternext*/
1371 Decomp_methods, /*tp_methods*/
1372 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001373};
1374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001375PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001376"The functions in this module allow compression and decompression using the\n"
1377"zlib library, which is based on GNU zip.\n"
1378"\n"
1379"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001380"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001381"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001382"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001383"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001384"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001385"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001386"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001387"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001388"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001389
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,
1394 -1,
1395 zlib_methods,
1396 NULL,
1397 NULL,
1398 NULL,
1399 NULL
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;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001406 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001407 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001408 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001409 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001410 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001411 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001412 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001413
Fred Drake4baedc12002-04-01 14:53:37 +00001414 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1415 if (ZlibError != NULL) {
1416 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001417 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001418 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001419 PyModule_AddIntMacro(m, MAX_WBITS);
1420 PyModule_AddIntMacro(m, DEFLATED);
1421 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001422 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001423 // compression levels
1424 PyModule_AddIntMacro(m, Z_NO_COMPRESSION);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001425 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1426 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1427 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001428 // compression strategies
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001429 PyModule_AddIntMacro(m, Z_FILTERED);
1430 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001431#ifdef Z_RLE // 1.2.0.1
1432 PyModule_AddIntMacro(m, Z_RLE);
1433#endif
1434#ifdef Z_FIXED // 1.2.2.2
1435 PyModule_AddIntMacro(m, Z_FIXED);
1436#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001437 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001438 // allowed flush values
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001439 PyModule_AddIntMacro(m, Z_NO_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001440 PyModule_AddIntMacro(m, Z_PARTIAL_FLUSH);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001441 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1442 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001443 PyModule_AddIntMacro(m, Z_FINISH);
1444#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1445 PyModule_AddIntMacro(m, Z_BLOCK);
1446#endif
1447#ifdef Z_TREES // 1.2.3.4, only for inflate
1448 PyModule_AddIntMacro(m, Z_TREES);
1449#endif
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001450 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001451 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001452 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001453
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001454 ver = PyUnicode_FromString(zlibVersion());
1455 if (ver != NULL)
1456 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1457
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001458 PyModule_AddStringConstant(m, "__version__", "1.0");
1459
Martin v. Löwis1a214512008-06-11 05:26:20 +00001460 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001461}