blob: cfe7f88dc540ecb1edb4aa6b145af5075efa7dce [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
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000013#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000014 #include "pythread.h"
15 #define ENTER_ZLIB(obj) \
16 Py_BEGIN_ALLOW_THREADS; \
17 PyThread_acquire_lock((obj)->lock, 1); \
18 Py_END_ALLOW_THREADS;
19 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000020#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000021 #define ENTER_ZLIB(obj)
22 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000023#endif
24
Martin Panter3f0ee832016-06-05 10:48:34 +000025#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000026# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000027#endif
28
Guido van Rossumfb221561997-04-29 15:38:09 +000029/* The following parameters are copied from zutil.h, version 0.95 */
30#define DEFLATED 8
31#if MAX_MEM_LEVEL >= 8
32# define DEF_MEM_LEVEL 8
33#else
34# define DEF_MEM_LEVEL MAX_MEM_LEVEL
35#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000036
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020037/* Initial buffer size. */
38#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000039
Jeremy Hylton938ace62002-07-17 16:30:39 +000040static PyTypeObject Comptype;
41static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000042
43static PyObject *ZlibError;
44
Tim Peters977e5402001-10-17 03:57:20 +000045typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000046{
Jeremy Hylton9714f992001-10-16 21:19:45 +000047 PyObject_HEAD
48 z_stream zst;
49 PyObject *unused_data;
50 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020051 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000052 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020053 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000054 #ifdef WITH_THREAD
55 PyThread_type_lock lock;
56 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000057} compobject;
58
Jeremy Hylton0965e082001-10-16 21:56:09 +000059static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020060zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000061{
Nadeem Vawda524148a2011-08-28 11:26:46 +020062 const char *zmsg = Z_NULL;
63 /* In case of a version mismatch, zst.msg won't be initialized.
64 Check for this case first, before looking at zst.msg. */
65 if (err == Z_VERSION_ERROR)
66 zmsg = "library version mismatch";
67 if (zmsg == Z_NULL)
68 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000069 if (zmsg == Z_NULL) {
70 switch (err) {
71 case Z_BUF_ERROR:
72 zmsg = "incomplete or truncated stream";
73 break;
74 case Z_STREAM_ERROR:
75 zmsg = "inconsistent stream state";
76 break;
77 case Z_DATA_ERROR:
78 zmsg = "invalid input data";
79 break;
80 }
81 }
82 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000084 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000085 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000086}
87
Larry Hastings61272b72014-01-07 12:41:53 -080088/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080089module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080090class zlib.Compress "compobject *" "&Comptype"
91class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080092[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030093/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080094
Guido van Rossumfb221561997-04-29 15:38:09 +000095static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000096newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000097{
Tim Peters977e5402001-10-17 03:57:20 +000098 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 self = PyObject_New(compobject, type);
100 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000101 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200102 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000103 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200104 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000105 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000106 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000107 Py_DECREF(self);
108 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000109 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000110 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 Py_DECREF(self);
113 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000115#ifdef WITH_THREAD
116 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200117 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000118 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200119 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
120 return NULL;
121 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000122#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000123 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000124}
125
Victor Stinner5064a522013-07-07 16:50:27 +0200126static void*
127PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
128{
129 if (items > (size_t)PY_SSIZE_T_MAX / size)
130 return NULL;
131 /* PyMem_Malloc() cannot be used: the GIL is not held when
132 inflate() and deflate() are called */
133 return PyMem_RawMalloc(items * size);
134}
135
136static void
137PyZlib_Free(voidpf ctx, void *ptr)
138{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200139 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200140}
141
Martin Panter84544c12016-07-23 03:02:07 +0000142static void
143arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
144{
145 zst->avail_in = Py_MIN((size_t)*remains, UINT_MAX);
146 *remains -= zst->avail_in;
147}
148
149static Py_ssize_t
150arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
151 Py_ssize_t length,
152 Py_ssize_t max_length)
153{
154 Py_ssize_t occupied;
155
156 if (*buffer == NULL) {
157 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
158 return -1;
159 occupied = 0;
160 }
161 else {
162 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
163
164 if (length == occupied) {
165 Py_ssize_t new_length;
166 assert(length <= max_length);
167 /* can not scale the buffer over max_length */
168 if (length == max_length)
169 return -2;
170 if (length <= (max_length >> 1))
171 new_length = length << 1;
172 else
173 new_length = max_length;
174 if (_PyBytes_Resize(buffer, new_length) < 0)
175 return -1;
176 length = new_length;
177 }
178 }
179
180 zst->avail_out = Py_MIN((size_t)(length - occupied), UINT_MAX);
181 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
182
183 return length;
184}
185
186static Py_ssize_t
187arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
188{
189 Py_ssize_t ret;
190
191 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
192 PY_SSIZE_T_MAX);
193 if (ret == -2)
194 PyErr_NoMemory();
195
196 return ret;
197}
198
Larry Hastings61272b72014-01-07 12:41:53 -0800199/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800200zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200201
Martin Panter1fe0d132016-02-10 10:06:36 +0000202 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800203 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300204 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200205 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000206 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800207
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200208Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800209[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800210
Guido van Rossumfb221561997-04-29 15:38:09 +0000211static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300212zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
213/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000214{
Martin Panter84544c12016-07-23 03:02:07 +0000215 PyObject *RetVal = NULL;
216 Byte *ibuf;
217 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
218 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000219 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000220
Martin Panter525a9492016-07-23 03:39:49 +0000221 ibuf = data->buf;
222 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Victor Stinner5064a522013-07-07 16:50:27 +0200224 zst.opaque = NULL;
225 zst.zalloc = PyZlib_Malloc;
226 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000227 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000228 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000229
Martin Panter84544c12016-07-23 03:02:07 +0000230 switch (err) {
231 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232 break;
Martin Panter84544c12016-07-23 03:02:07 +0000233 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 PyErr_SetString(PyExc_MemoryError,
235 "Out of memory while compressing data");
236 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000237 case Z_STREAM_ERROR:
238 PyErr_SetString(ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000240 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000241 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000242 zlib_error(zst, err, "while compressing data");
243 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000245
Martin Panter84544c12016-07-23 03:02:07 +0000246 do {
247 arrange_input_buffer(&zst, &ibuflen);
248 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000249
Martin Panter84544c12016-07-23 03:02:07 +0000250 do {
251 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
252 if (obuflen < 0) {
253 deflateEnd(&zst);
254 goto error;
255 }
256
257 Py_BEGIN_ALLOW_THREADS
258 err = deflate(&zst, flush);
259 Py_END_ALLOW_THREADS
260
261 if (err == Z_STREAM_ERROR) {
262 deflateEnd(&zst);
263 zlib_error(zst, err, "while compressing data");
264 goto error;
265 }
266
267 } while (zst.avail_out == 0);
268 assert(zst.avail_in == 0);
269
270 } while (flush != Z_FINISH);
271 assert(err == Z_STREAM_END);
272
273 err = deflateEnd(&zst);
274 if (err == Z_OK) {
275 if (_PyBytes_Resize(&RetVal, zst.next_out -
276 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
277 goto error;
278 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000279 }
Tim Peters977e5402001-10-17 03:57:20 +0000280 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000281 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000282 error:
Martin Panter84544c12016-07-23 03:02:07 +0000283 Py_XDECREF(RetVal);
284 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000285}
286
Larry Hastings61272b72014-01-07 12:41:53 -0800287/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100288
Martin Panter84544c12016-07-23 03:02:07 +0000289class ssize_t_converter(CConverter):
290 type = 'Py_ssize_t'
291 converter = 'ssize_t_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200292 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100293
Larry Hastings61272b72014-01-07 12:41:53 -0800294[python start generated code]*/
Martin Panter84544c12016-07-23 03:02:07 +0000295/*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100296
297static int
Martin Panter84544c12016-07-23 03:02:07 +0000298ssize_t_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100299{
Martin Pantere99e9772015-11-20 08:13:35 +0000300 PyObject *long_obj;
301 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100302
Martin Pantere99e9772015-11-20 08:13:35 +0000303 long_obj = (PyObject *)_PyLong_FromNbInt(obj);
304 if (long_obj == NULL) {
305 return 0;
306 }
307 val = PyLong_AsSsize_t(long_obj);
308 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100309 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000310 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100311 }
Martin Panter84544c12016-07-23 03:02:07 +0000312 *(Py_ssize_t *)ptr = val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100313 return 1;
314}
315
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200316/*[clinic input]
317zlib.decompress
318
319 data: Py_buffer
320 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300321 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200322 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000323 The window buffer size and container format.
Martin Panter84544c12016-07-23 03:02:07 +0000324 bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200325 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200326
327Returns a bytes object containing the uncompressed data.
328[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000329
Guido van Rossumfb221561997-04-29 15:38:09 +0000330static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300331zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000332 Py_ssize_t bufsize)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300333/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000334{
Martin Panter84544c12016-07-23 03:02:07 +0000335 PyObject *RetVal = NULL;
336 Byte *ibuf;
337 Py_ssize_t ibuflen;
338 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000339 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000340
Martin Panter84544c12016-07-23 03:02:07 +0000341 if (bufsize < 0) {
342 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
343 return NULL;
344 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100345 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000346 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000347
Martin Panter84544c12016-07-23 03:02:07 +0000348 ibuf = data->buf;
349 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000350
Victor Stinner5064a522013-07-07 16:50:27 +0200351 zst.opaque = NULL;
352 zst.zalloc = PyZlib_Malloc;
353 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000354 zst.avail_in = 0;
355 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200356 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000357
Martin Panter84544c12016-07-23 03:02:07 +0000358 switch (err) {
359 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000360 break;
Martin Panter84544c12016-07-23 03:02:07 +0000361 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000362 PyErr_SetString(PyExc_MemoryError,
363 "Out of memory while decompressing data");
364 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000365 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000366 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 zlib_error(zst, err, "while preparing to decompress data");
368 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000369 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000370
Jeremy Hylton9714f992001-10-16 21:19:45 +0000371 do {
Martin Panter84544c12016-07-23 03:02:07 +0000372 arrange_input_buffer(&zst, &ibuflen);
373 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000374
Martin Panter84544c12016-07-23 03:02:07 +0000375 do {
376 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
377 if (bufsize < 0) {
378 inflateEnd(&zst);
379 goto error;
380 }
381
382 Py_BEGIN_ALLOW_THREADS
383 err = inflate(&zst, flush);
384 Py_END_ALLOW_THREADS
385
386 switch (err) {
387 case Z_OK: /* fall through */
388 case Z_BUF_ERROR: /* fall through */
389 case Z_STREAM_END:
390 break;
391 case Z_MEM_ERROR:
392 inflateEnd(&zst);
393 PyErr_SetString(PyExc_MemoryError,
394 "Out of memory while decompressing data");
395 goto error;
396 default:
397 inflateEnd(&zst);
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000398 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000399 goto error;
400 }
Martin Panter84544c12016-07-23 03:02:07 +0000401
402 } while (zst.avail_out == 0);
403
404 } while (err != Z_STREAM_END && ibuflen != 0);
405
406
407 if (err != Z_STREAM_END) {
408 inflateEnd(&zst);
409 zlib_error(zst, err, "while decompressing data");
410 goto error;
411 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000412
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000413 err = inflateEnd(&zst);
414 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200415 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000416 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000417 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000418
Martin Panter84544c12016-07-23 03:02:07 +0000419 if (_PyBytes_Resize(&RetVal, zst.next_out -
420 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000421 goto error;
422
Martin Panter84544c12016-07-23 03:02:07 +0000423 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000424
425 error:
Martin Panter84544c12016-07-23 03:02:07 +0000426 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000427 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000428}
429
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200430/*[clinic input]
431zlib.compressobj
432
433 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000434 The compression level (an integer in the range 0-9 or -1; default is
435 currently equivalent to 6). Higher compression levels are slower,
436 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200437 method: int(c_default="DEFLATED") = DEFLATED
438 The compression algorithm. If given, this must be DEFLATED.
439 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000440 +9 to +15: The base-two logarithm of the window size. Include a zlib
441 container.
442 -9 to -15: Generate a raw stream.
443 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200444 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
445 Controls the amount of memory used for internal compression state.
446 Valid values range from 1 to 9. Higher values result in higher memory
447 usage, faster compression, and smaller output.
448 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
449 Used to tune the compression algorithm. Possible values are
450 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
451 zdict: Py_buffer = None
452 The predefined compression dictionary - a sequence of bytes
453 containing subsequences that are likely to occur in the input data.
454
455Return a compressor object.
456[clinic start generated code]*/
457
Guido van Rossumfb221561997-04-29 15:38:09 +0000458static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300459zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400460 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300461/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000462{
Victor Stinnere079edd2013-11-21 22:33:21 +0100463 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200464 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000465
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200466 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100467 PyErr_SetString(PyExc_OverflowError,
468 "zdict length does not fit in an unsigned int");
469 goto error;
470 }
471
Jeremy Hylton499000002001-10-16 21:59:35 +0000472 self = newcompobject(&Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000473 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200474 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200475 self->zst.opaque = NULL;
476 self->zst.zalloc = PyZlib_Malloc;
477 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000478 self->zst.next_in = NULL;
479 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000480 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000481 switch (err) {
482 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000483 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200484 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200485 goto success;
486 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100487 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200488 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200489 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000490 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200491 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000492 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200493 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
494 goto error;
495 default:
496 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
497 goto error;
498 }
499 }
Martin Panter84544c12016-07-23 03:02:07 +0000500 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000501 PyErr_SetString(PyExc_MemoryError,
502 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200503 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000504 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000505 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200506 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 default:
508 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200509 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000510 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200511
512 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200513 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200514 success:
Martin Panter84544c12016-07-23 03:02:07 +0000515 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000516}
517
Martin Panter3f0ee832016-06-05 10:48:34 +0000518static int
519set_inflate_zdict(compobject *self)
520{
521 Py_buffer zdict_buf;
522 int err;
523
524 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
525 return -1;
526 }
527 if ((size_t)zdict_buf.len > UINT_MAX) {
528 PyErr_SetString(PyExc_OverflowError,
529 "zdict length does not fit in an unsigned int");
530 PyBuffer_Release(&zdict_buf);
531 return -1;
532 }
Martin Panter84544c12016-07-23 03:02:07 +0000533 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000534 zdict_buf.buf, (unsigned int)zdict_buf.len);
535 PyBuffer_Release(&zdict_buf);
536 if (err != Z_OK) {
537 zlib_error(self->zst, err, "while setting zdict");
538 return -1;
539 }
540 return 0;
541}
542
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200543/*[clinic input]
544zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200545
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200546 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000547 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200548 zdict: object(c_default="NULL") = b''
549 The predefined compression dictionary. This must be the same
550 dictionary as used by the compressor that produced the input data.
551
552Return a decompressor object.
553[clinic start generated code]*/
554
555static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300556zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
557/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200558{
559 int err;
560 compobject *self;
561
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200562 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
563 PyErr_SetString(PyExc_TypeError,
564 "zdict argument must support the buffer protocol");
565 return NULL;
566 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000567
568 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000569 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000570 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200571 self->zst.opaque = NULL;
572 self->zst.zalloc = PyZlib_Malloc;
573 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000574 self->zst.next_in = NULL;
575 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200576 if (zdict != NULL) {
577 Py_INCREF(zdict);
578 self->zdict = zdict;
579 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000580 err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000581 switch (err) {
582 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000584 if (self->zdict != NULL && wbits < 0) {
585#ifdef AT_LEAST_ZLIB_1_2_2_1
586 if (set_inflate_zdict(self) < 0) {
587 Py_DECREF(self);
588 return NULL;
589 }
590#else
591 PyErr_Format(ZlibError,
592 "zlib version %s does not allow raw inflate with dictionary",
593 ZLIB_VERSION);
594 Py_DECREF(self);
595 return NULL;
596#endif
597 }
Martin Panter84544c12016-07-23 03:02:07 +0000598 return (PyObject *)self;
599 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000600 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000601 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
602 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000603 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000604 Py_DECREF(self);
605 PyErr_SetString(PyExc_MemoryError,
606 "Can't allocate memory for decompression object");
607 return NULL;
608 default:
609 zlib_error(self->zst, err, "while creating decompression object");
610 Py_DECREF(self);
611 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000612 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000613}
614
615static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000616Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000617{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000618#ifdef WITH_THREAD
619 PyThread_free_lock(self->lock);
620#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000621 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000622 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200623 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000624 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000625}
626
627static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000628Comp_dealloc(compobject *self)
629{
630 if (self->is_initialised)
631 deflateEnd(&self->zst);
632 Dealloc(self);
633}
634
635static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000636Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000637{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000638 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000639 inflateEnd(&self->zst);
640 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000641}
642
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200643/*[clinic input]
644zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000645
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200646 data: Py_buffer
647 Binary data to be compressed.
648 /
649
650Returns a bytes object containing compressed data.
651
652After calling this function, some of the input data may still
653be stored in internal buffers for later processing.
654Call the flush() method to clear these buffers.
655[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000656
Guido van Rossumfb221561997-04-29 15:38:09 +0000657static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200658zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800659/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000660{
Martin Panter84544c12016-07-23 03:02:07 +0000661 PyObject *RetVal = NULL;
662 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200663 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000664
Martin Panter84544c12016-07-23 03:02:07 +0000665 self->zst.next_in = data->buf;
666 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000667
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000668 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000669
Martin Panter84544c12016-07-23 03:02:07 +0000670 do {
671 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000672
Martin Panter84544c12016-07-23 03:02:07 +0000673 do {
674 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
675 if (obuflen < 0)
676 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000677
Martin Panter84544c12016-07-23 03:02:07 +0000678 Py_BEGIN_ALLOW_THREADS
679 err = deflate(&self->zst, Z_NO_FLUSH);
680 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000681
Martin Panter84544c12016-07-23 03:02:07 +0000682 if (err == Z_STREAM_ERROR) {
683 zlib_error(self->zst, err, "while compressing data");
684 goto error;
685 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000686
Martin Panter84544c12016-07-23 03:02:07 +0000687 } while (self->zst.avail_out == 0);
688 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000689
Martin Panter84544c12016-07-23 03:02:07 +0000690 } while (ibuflen != 0);
691
692 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
693 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
694 goto success;
695
696 error:
697 Py_CLEAR(RetVal);
698 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000699 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000700 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000701}
702
Martin Panter84544c12016-07-23 03:02:07 +0000703/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100704 self->unused_data or self->unconsumed_tail, as appropriate. */
705static int
Martin Panter84544c12016-07-23 03:02:07 +0000706save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100707{
708 if (err == Z_STREAM_END) {
709 /* The end of the compressed data has been reached. Store the leftover
710 input data in self->unused_data. */
711 if (self->zst.avail_in > 0) {
712 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000713 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100714 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000715 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
716 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100717 PyErr_NoMemory();
718 return -1;
719 }
Martin Panter84544c12016-07-23 03:02:07 +0000720 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100721 new_data = PyBytes_FromStringAndSize(NULL, new_size);
722 if (new_data == NULL)
723 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200724 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100725 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200726 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000727 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300728 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100729 self->zst.avail_in = 0;
730 }
731 }
Martin Panter84544c12016-07-23 03:02:07 +0000732
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100733 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
734 /* This code handles two distinct cases:
735 1. Output limit was reached. Save leftover input in unconsumed_tail.
736 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000737 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100738 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000739 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100740 if (new_data == NULL)
741 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300742 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100743 }
Martin Panter84544c12016-07-23 03:02:07 +0000744
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100745 return 0;
746}
747
Larry Hastings61272b72014-01-07 12:41:53 -0800748/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800749zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700750
751 data: Py_buffer
752 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300753 /
Martin Panter84544c12016-07-23 03:02:07 +0000754 max_length: ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700755 The maximum allowable length of the decompressed data.
756 Unconsumed input data will be stored in
757 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700758
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200759Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700760
761After calling this function, some of the input data may still be stored in
762internal buffers for later processing.
763Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800764[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700765
Larry Hastings31826802013-10-19 00:09:25 -0700766static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400767zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000768 Py_ssize_t max_length)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300769/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
Larry Hastings31826802013-10-19 00:09:25 -0700770{
Martin Panter84544c12016-07-23 03:02:07 +0000771 int err = Z_OK;
772 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200773 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000774
Martin Panter84544c12016-07-23 03:02:07 +0000775 if (max_length < 0) {
776 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700777 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000778 } else if (max_length == 0)
779 hard_limit = PY_SSIZE_T_MAX;
780 else
781 hard_limit = max_length;
782
783 self->zst.next_in = data->buf;
784 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000785
Jeremy Hylton9714f992001-10-16 21:19:45 +0000786 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000787 if (max_length && obuflen > max_length)
788 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000789
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800790 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000791
Martin Panter84544c12016-07-23 03:02:07 +0000792 do {
793 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000794
Martin Panter84544c12016-07-23 03:02:07 +0000795 do {
796 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
797 obuflen, hard_limit);
798 if (obuflen == -2) {
799 if (max_length > 0) {
800 goto save;
801 }
802 PyErr_NoMemory();
803 }
804 if (obuflen < 0) {
805 goto abort;
806 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000807
Martin Panter84544c12016-07-23 03:02:07 +0000808 Py_BEGIN_ALLOW_THREADS
809 err = inflate(&self->zst, Z_SYNC_FLUSH);
810 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100811
Martin Panter84544c12016-07-23 03:02:07 +0000812 switch (err) {
813 case Z_OK: /* fall through */
814 case Z_BUF_ERROR: /* fall through */
815 case Z_STREAM_END:
816 break;
817 default:
818 if (err == Z_NEED_DICT && self->zdict != NULL) {
819 if (set_inflate_zdict(self) < 0)
820 goto abort;
821 else
822 break;
823 }
824 goto save;
825 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200826
Martin Panter84544c12016-07-23 03:02:07 +0000827 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000828
Martin Panter84544c12016-07-23 03:02:07 +0000829 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000830
Martin Panter84544c12016-07-23 03:02:07 +0000831 save:
832 if (save_unconsumed_input(self, data, err) < 0)
833 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000834
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000835 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100836 /* This is the logical place to call inflateEnd, but the old behaviour
837 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800838 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100839 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000840 /* We will only get Z_BUF_ERROR if the output buffer was full
841 but there wasn't more output when we tried again, so it is
842 not an error condition.
843 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800844 zlib_error(self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000845 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000846 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000847
Martin Panter84544c12016-07-23 03:02:07 +0000848 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
849 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
850 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000851
Martin Panter84544c12016-07-23 03:02:07 +0000852 abort:
853 Py_CLEAR(RetVal);
854 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800855 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000856 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000857}
858
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200859/*[clinic input]
860zlib.Compress.flush
861
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200862 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200863 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
864 If mode == Z_FINISH, the compressor object can no longer be
865 used after calling the flush() method. Otherwise, more data
866 can still be compressed.
867 /
868
869Return a bytes object containing any remaining compressed data.
870[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000871
Guido van Rossumfb221561997-04-29 15:38:09 +0000872static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200873zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200874/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000875{
Victor Stinnere079edd2013-11-21 22:33:21 +0100876 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000877 Py_ssize_t length = DEF_BUF_SIZE;
878 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000879
Jeremy Hylton9714f992001-10-16 21:19:45 +0000880 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
881 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200882 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000884 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000885
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000886 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000887
Jeremy Hylton9714f992001-10-16 21:19:45 +0000888 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000889
Martin Panter84544c12016-07-23 03:02:07 +0000890 do {
891 length = arrange_output_buffer(&self->zst, &RetVal, length);
892 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200893 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000894 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000895 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000898 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000900
901 if (err == Z_STREAM_ERROR) {
902 zlib_error(self->zst, err, "while flushing");
903 Py_CLEAR(RetVal);
904 goto error;
905 }
906 } while (self->zst.avail_out == 0);
907 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000908
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200909 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000910 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200911 mode is Z_FINISH, but checking both for safety*/
912 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000913 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200915 zlib_error(self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000916 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000917 goto error;
918 }
919 else
920 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000921
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 /* We will only get Z_BUF_ERROR if the output buffer was full
923 but there wasn't more output when we tried again, so it is
924 not an error condition.
925 */
Martin Panter84544c12016-07-23 03:02:07 +0000926 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000927 zlib_error(self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000928 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000929 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000930 }
Tim Peters977e5402001-10-17 03:57:20 +0000931
Martin Panter84544c12016-07-23 03:02:07 +0000932 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
933 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200934 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000935
Tim Peters977e5402001-10-17 03:57:20 +0000936 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000937 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000938 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000939}
940
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000941#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700942
Larry Hastings61272b72014-01-07 12:41:53 -0800943/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800944zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700945
946Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800947[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700948
Larry Hastings3cceb382014-01-04 11:09:09 -0800949static PyObject *
950zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800951/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000952{
953 compobject *retval = NULL;
954 int err;
955
956 retval = newcompobject(&Comptype);
957 if (!retval) return NULL;
958
959 /* Copy the zstream state
960 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
961 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800962 ENTER_ZLIB(self);
963 err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000964 switch (err) {
965 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966 break;
Martin Panter84544c12016-07-23 03:02:07 +0000967 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
969 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000970 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000971 PyErr_SetString(PyExc_MemoryError,
972 "Can't allocate memory for compression object");
973 goto error;
974 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800975 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976 goto error;
977 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800978 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300979 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800980 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300981 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800982 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300983 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800984 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000985
986 /* Mark it as being initialized */
987 retval->is_initialised = 1;
988
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800989 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000990 return (PyObject *)retval;
991
992error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800993 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000994 Py_XDECREF(retval);
995 return NULL;
996}
997
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200998/*[clinic input]
999zlib.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
1011 retval = newcompobject(&Decomptype);
1012 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}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001053#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001055/*[clinic input]
1056zlib.Decompress.flush
1057
Martin Panter84544c12016-07-23 03:02:07 +00001058 length: ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001059 the initial size of the output buffer.
1060 /
1061
1062Return a bytes object containing any remaining decompressed data.
1063[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001064
Guido van Rossumfb221561997-04-29 15:38:09 +00001065static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +00001066zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1067/*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001068{
Martin Panter84544c12016-07-23 03:02:07 +00001069 int err, flush;
1070 Py_buffer data;
1071 PyObject *RetVal = NULL;
1072 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001073
Martin Panter84544c12016-07-23 03:02:07 +00001074 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001075 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1076 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001077 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001078
Martin Panter84544c12016-07-23 03:02:07 +00001079 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001080 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001081
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001082 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001083
Martin Panter84544c12016-07-23 03:02:07 +00001084 self->zst.next_in = data.buf;
1085 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001086
Martin Panter84544c12016-07-23 03:02:07 +00001087 do {
1088 arrange_input_buffer(&self->zst, &ibuflen);
1089 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001090
Martin Panter84544c12016-07-23 03:02:07 +00001091 do {
1092 length = arrange_output_buffer(&self->zst, &RetVal, length);
1093 if (length < 0)
1094 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001095
Martin Panter84544c12016-07-23 03:02:07 +00001096 Py_BEGIN_ALLOW_THREADS
1097 err = inflate(&self->zst, flush);
1098 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001099
Martin Panter84544c12016-07-23 03:02:07 +00001100 switch (err) {
1101 case Z_OK: /* fall through */
1102 case Z_BUF_ERROR: /* fall through */
1103 case Z_STREAM_END:
1104 break;
1105 default:
1106 if (err == Z_NEED_DICT && self->zdict != NULL) {
1107 if (set_inflate_zdict(self) < 0)
1108 goto abort;
1109 else
1110 break;
1111 }
1112 goto save;
1113 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001114
Martin Panter84544c12016-07-23 03:02:07 +00001115 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1116
1117 } while (err != Z_STREAM_END && ibuflen != 0);
1118
1119 save:
1120 if (save_unconsumed_input(self, &data, err) < 0)
1121 goto abort;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001122
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001123 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001124 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001125 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001126 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001127 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001128 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001129 zlib_error(self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001130 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001131 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001132 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001133
Martin Panter84544c12016-07-23 03:02:07 +00001134 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1135 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1136 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001137
Martin Panter84544c12016-07-23 03:02:07 +00001138 abort:
1139 Py_CLEAR(RetVal);
1140 success:
1141 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001142 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001143 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001144}
1145
Christian Heimes936e2f32014-01-27 01:06:57 +01001146#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001147
Guido van Rossumfb221561997-04-29 15:38:09 +00001148static PyMethodDef comp_methods[] =
1149{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001150 ZLIB_COMPRESS_COMPRESS_METHODDEF
1151 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001152#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001153 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001154#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001155 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001156};
1157
1158static PyMethodDef Decomp_methods[] =
1159{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001160 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001161 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001162#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001163 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001164#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001165 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001166};
1167
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001168#define COMP_OFF(x) offsetof(compobject, x)
1169static PyMemberDef Decomp_members[] = {
1170 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1171 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001172 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001173 {NULL},
1174};
Guido van Rossumfb221561997-04-29 15:38:09 +00001175
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001176/*[clinic input]
1177zlib.adler32
1178
1179 data: Py_buffer
1180 value: unsigned_int(bitwise=True) = 1
1181 Starting value of the checksum.
1182 /
1183
1184Compute an Adler-32 checksum of data.
1185
1186The returned checksum is an integer.
1187[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001188
Guido van Rossumfb221561997-04-29 15:38:09 +00001189static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001190zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1191/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001192{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001193 /* Releasing the GIL for very small buffers is inefficient
1194 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001195 if (data->len > 1024*5) {
1196 unsigned char *buf = data->buf;
1197 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001198
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001199 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001200 /* Avoid truncation of length for very large buffers. adler32() takes
1201 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001202 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001203 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001204 buf += (size_t) UINT_MAX;
1205 len -= (size_t) UINT_MAX;
1206 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001207 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001208 Py_END_ALLOW_THREADS
1209 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001210 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001211 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001212 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001213}
Tim Peters977e5402001-10-17 03:57:20 +00001214
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001215/*[clinic input]
1216zlib.crc32
1217
1218 data: Py_buffer
1219 value: unsigned_int(bitwise=True) = 0
1220 Starting value of the checksum.
1221 /
1222
1223Compute a CRC-32 checksum of data.
1224
1225The returned checksum is an integer.
1226[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001227
1228static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001229zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1230/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001231{
Martin v. Löwis423be952008-08-13 15:53:07 +00001232 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001233
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001234 /* Releasing the GIL for very small buffers is inefficient
1235 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001236 if (data->len > 1024*5) {
1237 unsigned char *buf = data->buf;
1238 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001239
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001240 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001241 /* Avoid truncation of length for very large buffers. crc32() takes
1242 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001243 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001244 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001245 buf += (size_t) UINT_MAX;
1246 len -= (size_t) UINT_MAX;
1247 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001248 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001249 Py_END_ALLOW_THREADS
1250 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001251 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001252 }
Christian Heimescc47b052008-03-25 14:56:36 +00001253 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001254}
Tim Peters977e5402001-10-17 03:57:20 +00001255
Guido van Rossumfb221561997-04-29 15:38:09 +00001256
1257static PyMethodDef zlib_methods[] =
1258{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001259 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001260 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001261 ZLIB_COMPRESSOBJ_METHODDEF
1262 ZLIB_CRC32_METHODDEF
1263 ZLIB_DECOMPRESS_METHODDEF
1264 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001265 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001266};
1267
Tim Peters0c322792002-07-17 16:49:03 +00001268static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001269 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001270 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001271 sizeof(compobject),
1272 0,
1273 (destructor)Comp_dealloc, /*tp_dealloc*/
1274 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001275 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001276 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001277 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001278 0, /*tp_repr*/
1279 0, /*tp_as_number*/
1280 0, /*tp_as_sequence*/
1281 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001282 0, /*tp_hash*/
1283 0, /*tp_call*/
1284 0, /*tp_str*/
1285 0, /*tp_getattro*/
1286 0, /*tp_setattro*/
1287 0, /*tp_as_buffer*/
1288 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1289 0, /*tp_doc*/
1290 0, /*tp_traverse*/
1291 0, /*tp_clear*/
1292 0, /*tp_richcompare*/
1293 0, /*tp_weaklistoffset*/
1294 0, /*tp_iter*/
1295 0, /*tp_iternext*/
1296 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001297};
1298
Tim Peters0c322792002-07-17 16:49:03 +00001299static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001300 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001301 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001302 sizeof(compobject),
1303 0,
1304 (destructor)Decomp_dealloc, /*tp_dealloc*/
1305 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001306 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001307 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001308 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001309 0, /*tp_repr*/
1310 0, /*tp_as_number*/
1311 0, /*tp_as_sequence*/
1312 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001313 0, /*tp_hash*/
1314 0, /*tp_call*/
1315 0, /*tp_str*/
1316 0, /*tp_getattro*/
1317 0, /*tp_setattro*/
1318 0, /*tp_as_buffer*/
1319 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1320 0, /*tp_doc*/
1321 0, /*tp_traverse*/
1322 0, /*tp_clear*/
1323 0, /*tp_richcompare*/
1324 0, /*tp_weaklistoffset*/
1325 0, /*tp_iter*/
1326 0, /*tp_iternext*/
1327 Decomp_methods, /*tp_methods*/
1328 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001329};
1330
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001331PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001332"The functions in this module allow compression and decompression using the\n"
1333"zlib library, which is based on GNU zip.\n"
1334"\n"
1335"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001336"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001337"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001338"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001339"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001340"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001341"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001342"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001343"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001344"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001345
Martin v. Löwis1a214512008-06-11 05:26:20 +00001346static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001347 PyModuleDef_HEAD_INIT,
1348 "zlib",
1349 zlib_module_documentation,
1350 -1,
1351 zlib_methods,
1352 NULL,
1353 NULL,
1354 NULL,
1355 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001356};
1357
Mark Hammond62b1ab12002-07-23 06:31:15 +00001358PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001359PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001360{
Fred Drake4baedc12002-04-01 14:53:37 +00001361 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001362 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001363 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001364 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001365 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001366 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001367 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001368 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001369
Fred Drake4baedc12002-04-01 14:53:37 +00001370 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1371 if (ZlibError != NULL) {
1372 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001373 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001374 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001375 PyModule_AddIntMacro(m, MAX_WBITS);
1376 PyModule_AddIntMacro(m, DEFLATED);
1377 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001378 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001379 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1380 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1381 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1382 PyModule_AddIntMacro(m, Z_FILTERED);
1383 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1384 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001385
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001386 PyModule_AddIntMacro(m, Z_FINISH);
1387 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1388 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1389 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001390
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001391 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001392 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001393 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001394
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001395 ver = PyUnicode_FromString(zlibVersion());
1396 if (ver != NULL)
1397 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1398
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001399 PyModule_AddStringConstant(m, "__version__", "1.0");
1400
Martin v. Löwis1a214512008-06-11 05:26:20 +00001401 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001402}