blob: 28f7f151ac7ef31432f21f97ffa2b66f6eb74089 [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
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
26#define AT_LEAST_ZLIB_1_2_2_1
27#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) {
118 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
119 return NULL;
120 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000121#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000123}
124
Victor Stinner5064a522013-07-07 16:50:27 +0200125static void*
126PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
127{
128 if (items > (size_t)PY_SSIZE_T_MAX / size)
129 return NULL;
130 /* PyMem_Malloc() cannot be used: the GIL is not held when
131 inflate() and deflate() are called */
132 return PyMem_RawMalloc(items * size);
133}
134
135static void
136PyZlib_Free(voidpf ctx, void *ptr)
137{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200138 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200139}
140
Larry Hastings61272b72014-01-07 12:41:53 -0800141/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800142zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200143
Martin Panter1fe0d132016-02-10 10:06:36 +0000144 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800145 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300146 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200147 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000148 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800149
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200150Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800151[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800152
Guido van Rossumfb221561997-04-29 15:38:09 +0000153static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300154zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
155/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000156{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000157 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200158 Byte *input, *output = NULL;
159 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800160 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000161 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000162
Martin Panter1fe0d132016-02-10 10:06:36 +0000163 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200164 PyErr_SetString(PyExc_OverflowError,
165 "Size does not fit in an unsigned int");
166 goto error;
167 }
Martin Panter1fe0d132016-02-10 10:06:36 +0000168 input = data->buf;
169 length = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000170
Jeremy Hylton9714f992001-10-16 21:19:45 +0000171 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000172
Victor Stinnerb6404912013-07-07 16:21:41 +0200173 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000174 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 PyErr_SetString(PyExc_MemoryError,
176 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200177 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000178 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000179
Jeremy Hylton9714f992001-10-16 21:19:45 +0000180 /* Past the point of no return. From here on out, we need to make sure
181 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000182
Victor Stinner5064a522013-07-07 16:50:27 +0200183 zst.opaque = NULL;
184 zst.zalloc = PyZlib_Malloc;
185 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000186 zst.next_out = (Byte *)output;
187 zst.next_in = (Byte *)input;
188 zst.avail_in = length;
189 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000190
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000191 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000192 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000193 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000194 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000195 PyErr_SetString(PyExc_MemoryError,
196 "Out of memory while compressing data");
197 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000198 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000199 PyErr_SetString(ZlibError,
200 "Bad compression level");
201 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000202 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000203 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000204 zlib_error(zst, err, "while compressing data");
205 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000206 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000207
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000208 Py_BEGIN_ALLOW_THREADS;
209 err = deflate(&zst, Z_FINISH);
210 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000211
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000212 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000213 zlib_error(zst, err, "while compressing data");
214 deflateEnd(&zst);
215 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000216 }
Tim Peters977e5402001-10-17 03:57:20 +0000217
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000218 err=deflateEnd(&zst);
219 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000221 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000222 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000223 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000224
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000225 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200226 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000227
Jeremy Hylton9714f992001-10-16 21:19:45 +0000228 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000229}
230
Larry Hastings61272b72014-01-07 12:41:53 -0800231/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100232
Martin Pantere99e9772015-11-20 08:13:35 +0000233class capped_uint_converter(CConverter):
Victor Stinnere079edd2013-11-21 22:33:21 +0100234 type = 'unsigned int'
Martin Pantere99e9772015-11-20 08:13:35 +0000235 converter = 'capped_uint_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200236 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100237
Larry Hastings61272b72014-01-07 12:41:53 -0800238[python start generated code]*/
Martin Pantere99e9772015-11-20 08:13:35 +0000239/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100240
241static int
Martin Pantere99e9772015-11-20 08:13:35 +0000242capped_uint_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100243{
Martin Pantere99e9772015-11-20 08:13:35 +0000244 PyObject *long_obj;
245 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100246
Martin Pantere99e9772015-11-20 08:13:35 +0000247 long_obj = (PyObject *)_PyLong_FromNbInt(obj);
248 if (long_obj == NULL) {
249 return 0;
250 }
251 val = PyLong_AsSsize_t(long_obj);
252 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100253 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000254 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100255 }
Martin Pantere99e9772015-11-20 08:13:35 +0000256 if (val < 0) {
257 PyErr_SetString(PyExc_ValueError,
258 "value must be positive");
Victor Stinner5c867332014-01-03 12:26:12 +0100259 return 0;
260 }
261
Martin Pantere99e9772015-11-20 08:13:35 +0000262 if ((size_t)val > UINT_MAX) {
263 *(unsigned int *)ptr = UINT_MAX;
264 }
265 else {
266 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(val, Py_ssize_t,
267 unsigned int);
268 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100269 return 1;
270}
271
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200272/*[clinic input]
273zlib.decompress
274
275 data: Py_buffer
276 Compressed data.
277 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000278 The window buffer size and container format.
Martin Pantere99e9772015-11-20 08:13:35 +0000279 bufsize: capped_uint(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200280 The initial output buffer size.
281 /
282
283Returns a bytes object containing the uncompressed data.
284[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000285
Guido van Rossumfb221561997-04-29 15:38:09 +0000286static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300287zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400288 unsigned int bufsize)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300289/*[clinic end generated code: output=475b36ead58b243d input=75123b0d4ff0541d]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000290{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200291 PyObject *result_str = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000292 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200293 unsigned int length;
294 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200295 unsigned int new_bufsize;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000296 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000297
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200298 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200299 PyErr_SetString(PyExc_OverflowError,
300 "Size does not fit in an unsigned int");
301 goto error;
302 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200303 input = data->buf;
304 length = (unsigned int)data->len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000305
Victor Stinnere079edd2013-11-21 22:33:21 +0100306 if (bufsize == 0)
307 bufsize = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000308
Jeremy Hylton9714f992001-10-16 21:19:45 +0000309 zst.avail_in = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100310 zst.avail_out = bufsize;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000311
Victor Stinnere079edd2013-11-21 22:33:21 +0100312 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200313 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000314
Victor Stinner5064a522013-07-07 16:50:27 +0200315 zst.opaque = NULL;
316 zst.zalloc = PyZlib_Malloc;
317 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000318 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000319 zst.next_in = (Byte *)input;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200320 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000321
Jeremy Hylton9714f992001-10-16 21:19:45 +0000322 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000323 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000324 break;
Tim Peters977e5402001-10-17 03:57:20 +0000325 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000326 PyErr_SetString(PyExc_MemoryError,
327 "Out of memory while decompressing data");
328 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000329 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000330 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 zlib_error(zst, err, "while preparing to decompress data");
332 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000333 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000334
Jeremy Hylton9714f992001-10-16 21:19:45 +0000335 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 Py_BEGIN_ALLOW_THREADS
337 err=inflate(&zst, Z_FINISH);
338 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000339
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000340 switch(err) {
341 case(Z_STREAM_END):
342 break;
343 case(Z_BUF_ERROR):
344 /*
345 * If there is at least 1 byte of room according to zst.avail_out
346 * and we get this error, assume that it means zlib cannot
347 * process the inflate call() due to an error in the data.
348 */
349 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000350 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 inflateEnd(&zst);
352 goto error;
353 }
354 /* fall through */
355 case(Z_OK):
356 /* need more memory */
Victor Stinnere079edd2013-11-21 22:33:21 +0100357 if (bufsize <= (UINT_MAX >> 1))
358 new_bufsize = bufsize << 1;
359 else
360 new_bufsize = UINT_MAX;
361 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000362 inflateEnd(&zst);
363 goto error;
364 }
365 zst.next_out =
Victor Stinnere079edd2013-11-21 22:33:21 +0100366 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
367 zst.avail_out = bufsize;
368 bufsize = new_bufsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000369 break;
370 default:
371 inflateEnd(&zst);
372 zlib_error(zst, err, "while decompressing data");
373 goto error;
374 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000375 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000376
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000377 err = inflateEnd(&zst);
378 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200379 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000380 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000381 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000382
Gregory P. Smith693fc462008-09-06 20:13:06 +0000383 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000384 goto error;
385
Jeremy Hylton9714f992001-10-16 21:19:45 +0000386 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000387
388 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000389 Py_XDECREF(result_str);
390 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000391}
392
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200393/*[clinic input]
394zlib.compressobj
395
396 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000397 The compression level (an integer in the range 0-9 or -1; default is
398 currently equivalent to 6). Higher compression levels are slower,
399 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200400 method: int(c_default="DEFLATED") = DEFLATED
401 The compression algorithm. If given, this must be DEFLATED.
402 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000403 +9 to +15: The base-two logarithm of the window size. Include a zlib
404 container.
405 -9 to -15: Generate a raw stream.
406 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200407 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
408 Controls the amount of memory used for internal compression state.
409 Valid values range from 1 to 9. Higher values result in higher memory
410 usage, faster compression, and smaller output.
411 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
412 Used to tune the compression algorithm. Possible values are
413 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
414 zdict: Py_buffer = None
415 The predefined compression dictionary - a sequence of bytes
416 containing subsequences that are likely to occur in the input data.
417
418Return a compressor object.
419[clinic start generated code]*/
420
Guido van Rossumfb221561997-04-29 15:38:09 +0000421static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300422zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400423 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300424/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000425{
Victor Stinnere079edd2013-11-21 22:33:21 +0100426 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200427 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000428
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200429 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100430 PyErr_SetString(PyExc_OverflowError,
431 "zdict length does not fit in an unsigned int");
432 goto error;
433 }
434
Jeremy Hylton499000002001-10-16 21:59:35 +0000435 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000436 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200437 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200438 self->zst.opaque = NULL;
439 self->zst.zalloc = PyZlib_Malloc;
440 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000441 self->zst.next_in = NULL;
442 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000443 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
444 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000445 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000446 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200447 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200448 goto success;
449 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100450 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200451 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200452 switch (err) {
453 case (Z_OK):
454 goto success;
455 case (Z_STREAM_ERROR):
456 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
457 goto error;
458 default:
459 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
460 goto error;
461 }
462 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000463 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 PyErr_SetString(PyExc_MemoryError,
465 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200466 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000467 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000468 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200469 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000470 default:
471 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200472 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000473 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200474
475 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200476 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200477 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200478 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000479}
480
Martin Panter3f0ee832016-06-05 10:48:34 +0000481static int
482set_inflate_zdict(compobject *self)
483{
484 Py_buffer zdict_buf;
485 int err;
486
487 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
488 return -1;
489 }
490 if ((size_t)zdict_buf.len > UINT_MAX) {
491 PyErr_SetString(PyExc_OverflowError,
492 "zdict length does not fit in an unsigned int");
493 PyBuffer_Release(&zdict_buf);
494 return -1;
495 }
496 err = inflateSetDictionary(&(self->zst),
497 zdict_buf.buf, (unsigned int)zdict_buf.len);
498 PyBuffer_Release(&zdict_buf);
499 if (err != Z_OK) {
500 zlib_error(self->zst, err, "while setting zdict");
501 return -1;
502 }
503 return 0;
504}
505
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200506/*[clinic input]
507zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200508
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200509 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000510 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200511 zdict: object(c_default="NULL") = b''
512 The predefined compression dictionary. This must be the same
513 dictionary as used by the compressor that produced the input data.
514
515Return a decompressor object.
516[clinic start generated code]*/
517
518static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300519zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
520/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200521{
522 int err;
523 compobject *self;
524
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200525 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
526 PyErr_SetString(PyExc_TypeError,
527 "zdict argument must support the buffer protocol");
528 return NULL;
529 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000530
531 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000532 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000533 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200534 self->zst.opaque = NULL;
535 self->zst.zalloc = PyZlib_Malloc;
536 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000537 self->zst.next_in = NULL;
538 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200539 if (zdict != NULL) {
540 Py_INCREF(zdict);
541 self->zdict = zdict;
542 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000543 err = inflateInit2(&self->zst, wbits);
544 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000545 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000546 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000547 if (self->zdict != NULL && wbits < 0) {
548#ifdef AT_LEAST_ZLIB_1_2_2_1
549 if (set_inflate_zdict(self) < 0) {
550 Py_DECREF(self);
551 return NULL;
552 }
553#else
554 PyErr_Format(ZlibError,
555 "zlib version %s does not allow raw inflate with dictionary",
556 ZLIB_VERSION);
557 Py_DECREF(self);
558 return NULL;
559#endif
560 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000562 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000563 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000564 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
565 return NULL;
566 case (Z_MEM_ERROR):
567 Py_DECREF(self);
568 PyErr_SetString(PyExc_MemoryError,
569 "Can't allocate memory for decompression object");
570 return NULL;
571 default:
572 zlib_error(self->zst, err, "while creating decompression object");
573 Py_DECREF(self);
574 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000575 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000576}
577
578static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000579Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000580{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000581#ifdef WITH_THREAD
582 PyThread_free_lock(self->lock);
583#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000584 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000585 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200586 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000587 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000588}
589
590static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000591Comp_dealloc(compobject *self)
592{
593 if (self->is_initialised)
594 deflateEnd(&self->zst);
595 Dealloc(self);
596}
597
598static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000599Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000600{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000601 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000602 inflateEnd(&self->zst);
603 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000604}
605
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200606/*[clinic input]
607zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000608
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200609 data: Py_buffer
610 Binary data to be compressed.
611 /
612
613Returns a bytes object containing compressed data.
614
615After calling this function, some of the input data may still
616be stored in internal buffers for later processing.
617Call the flush() method to clear these buffers.
618[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000619
Guido van Rossumfb221561997-04-29 15:38:09 +0000620static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200621zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800622/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000623{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200624 int err;
625 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200626 unsigned int length = DEF_BUF_SIZE, new_length;
627 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000628 Byte *input;
629 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000630
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200631 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200632 PyErr_SetString(PyExc_OverflowError,
633 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200634 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200635 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200636 input = data->buf;
637 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000638
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200639 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200640 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000641
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000642 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643
Jeremy Hylton9714f992001-10-16 21:19:45 +0000644 start_total_out = self->zst.total_out;
645 self->zst.avail_in = inplen;
646 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000647 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000648 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000649
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000650 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000651 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000652 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000653
Jeremy Hylton9714f992001-10-16 21:19:45 +0000654 /* while Z_OK and the output buffer is full, there might be more output,
655 so extend the output buffer and try again */
656 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100657 if (length <= (UINT_MAX >> 1))
658 new_length = length << 1;
659 else
660 new_length = UINT_MAX;
661 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200662 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200663 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000664 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000666 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000667 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100668 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000669
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000670 Py_BEGIN_ALLOW_THREADS
671 err = deflate(&(self->zst), Z_NO_FLUSH);
672 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000673 }
Tim Peters977e5402001-10-17 03:57:20 +0000674 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000675 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000676 condition.
677 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000678
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000679 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200680 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200681 Py_CLEAR(RetVal);
682 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000683 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000684 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200685 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000686 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000687
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200688 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000689 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000690 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000691}
692
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100693/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
694 self->unused_data or self->unconsumed_tail, as appropriate. */
695static int
696save_unconsumed_input(compobject *self, int err)
697{
698 if (err == Z_STREAM_END) {
699 /* The end of the compressed data has been reached. Store the leftover
700 input data in self->unused_data. */
701 if (self->zst.avail_in > 0) {
702 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
703 Py_ssize_t new_size;
704 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100705 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100706 PyErr_NoMemory();
707 return -1;
708 }
709 new_size = old_size + self->zst.avail_in;
710 new_data = PyBytes_FromStringAndSize(NULL, new_size);
711 if (new_data == NULL)
712 return -1;
713 Py_MEMCPY(PyBytes_AS_STRING(new_data),
714 PyBytes_AS_STRING(self->unused_data), old_size);
715 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
716 self->zst.next_in, self->zst.avail_in);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300717 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100718 self->zst.avail_in = 0;
719 }
720 }
721 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
722 /* This code handles two distinct cases:
723 1. Output limit was reached. Save leftover input in unconsumed_tail.
724 2. All input data was consumed. Clear unconsumed_tail. */
725 PyObject *new_data = PyBytes_FromStringAndSize(
726 (char *)self->zst.next_in, self->zst.avail_in);
727 if (new_data == NULL)
728 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300729 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100730 }
731 return 0;
732}
733
Larry Hastings61272b72014-01-07 12:41:53 -0800734/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800735zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700736
737 data: Py_buffer
738 The binary data to decompress.
Martin Pantere99e9772015-11-20 08:13:35 +0000739 max_length: capped_uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700740 The maximum allowable length of the decompressed data.
741 Unconsumed input data will be stored in
742 the unconsumed_tail attribute.
743 /
744
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200745Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700746
747After calling this function, some of the input data may still be stored in
748internal buffers for later processing.
749Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800750[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700751
Larry Hastings31826802013-10-19 00:09:25 -0700752static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400753zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
754 unsigned int max_length)
Martin Pantere99e9772015-11-20 08:13:35 +0000755/*[clinic end generated code: output=b82e2a2c19f5fe7b input=68b6508ab07c2cf0]*/
Larry Hastings31826802013-10-19 00:09:25 -0700756{
Larry Hastings31826802013-10-19 00:09:25 -0700757 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200758 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200759 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000760 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000761
Victor Stinnere079edd2013-11-21 22:33:21 +0100762 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200763 PyErr_SetString(PyExc_OverflowError,
764 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700765 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200766 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000767
Jeremy Hylton9714f992001-10-16 21:19:45 +0000768 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000769 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000770 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200771 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700772 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000773
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800774 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000775
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800776 start_total_out = self->zst.total_out;
777 self->zst.avail_in = (unsigned int)data->len;
778 self->zst.next_in = data->buf;
779 self->zst.avail_out = length;
780 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000781
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000782 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800783 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000784 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000785
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800786 if (err == Z_NEED_DICT && self->zdict != NULL) {
Martin Panter3f0ee832016-06-05 10:48:34 +0000787 if (set_inflate_zdict(self) < 0) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200788 Py_DECREF(RetVal);
789 RetVal = NULL;
790 goto error;
791 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100792
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200793 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200794 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800795 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200796 Py_END_ALLOW_THREADS
797 }
798
Jeremy Hylton9714f992001-10-16 21:19:45 +0000799 /* While Z_OK and the output buffer is full, there might be more output.
800 So extend the output buffer and try again.
801 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800802 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000803 /* If max_length set, don't continue decompressing if we've already
804 reached the limit.
805 */
806 if (max_length && length >= max_length)
807 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000808
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000809 /* otherwise, ... */
810 old_length = length;
811 length = length << 1;
812 if (max_length && length > max_length)
813 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000814
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200816 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000817 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000818 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800819 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000820 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800821 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000822
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000823 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800824 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000825 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000826 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000827
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800828 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200829 Py_DECREF(RetVal);
830 RetVal = NULL;
831 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000832 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000833
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000834 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100835 /* This is the logical place to call inflateEnd, but the old behaviour
836 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800837 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100838 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000839 /* We will only get Z_BUF_ERROR if the output buffer was full
840 but there wasn't more output when we tried again, so it is
841 not an error condition.
842 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800843 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000844 Py_DECREF(RetVal);
845 RetVal = NULL;
846 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000847 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000848
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800849 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200850 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000851 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000852
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000853 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800854 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000855 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000856}
857
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200858/*[clinic input]
859zlib.Compress.flush
860
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200861 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200862 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
863 If mode == Z_FINISH, the compressor object can no longer be
864 used after calling the flush() method. Otherwise, more data
865 can still be compressed.
866 /
867
868Return a bytes object containing any remaining compressed data.
869[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000870
Guido van Rossumfb221561997-04-29 15:38:09 +0000871static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200872zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200873/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000874{
Victor Stinnere079edd2013-11-21 22:33:21 +0100875 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200876 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000877 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000878 unsigned long start_total_out;
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
Gregory P. Smith693fc462008-09-06 20:13:06 +0000886 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000887 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000888
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000889 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000890
Jeremy Hylton9714f992001-10-16 21:19:45 +0000891 start_total_out = self->zst.total_out;
892 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000893 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000894 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000895
896 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200897 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000898 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000899
Jeremy Hylton9714f992001-10-16 21:19:45 +0000900 /* while Z_OK and the output buffer is full, there might be more output,
901 so extend the output buffer and try again */
902 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100903 if (length <= (UINT_MAX >> 1))
904 new_length = length << 1;
905 else
906 new_length = UINT_MAX;
907 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200908 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000909 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000910 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000911 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000912 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000913 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100914 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000915
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000916 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200917 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000918 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000919 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000920
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200921 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000922 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200923 mode is Z_FINISH, but checking both for safety*/
924 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000925 err = deflateEnd(&(self->zst));
926 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200927 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000928 Py_DECREF(RetVal);
929 RetVal = NULL;
930 goto error;
931 }
932 else
933 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000934
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000935 /* We will only get Z_BUF_ERROR if the output buffer was full
936 but there wasn't more output when we tried again, so it is
937 not an error condition.
938 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000939 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000940 zlib_error(self->zst, err, "while flushing");
941 Py_DECREF(RetVal);
942 RetVal = NULL;
943 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000944 }
Tim Peters977e5402001-10-17 03:57:20 +0000945
Gregory P. Smith693fc462008-09-06 20:13:06 +0000946 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200947 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000948 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000949
Tim Peters977e5402001-10-17 03:57:20 +0000950 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000951 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000952
953 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000954}
955
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000956#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700957
Larry Hastings61272b72014-01-07 12:41:53 -0800958/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800959zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700960
961Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800962[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700963
Larry Hastings3cceb382014-01-04 11:09:09 -0800964static PyObject *
965zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800966/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000967{
968 compobject *retval = NULL;
969 int err;
970
971 retval = newcompobject(&Comptype);
972 if (!retval) return NULL;
973
974 /* Copy the zstream state
975 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
976 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800977 ENTER_ZLIB(self);
978 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979 switch(err) {
980 case(Z_OK):
981 break;
982 case(Z_STREAM_ERROR):
983 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
984 goto error;
985 case(Z_MEM_ERROR):
986 PyErr_SetString(PyExc_MemoryError,
987 "Can't allocate memory for compression object");
988 goto error;
989 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800990 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000991 goto error;
992 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800993 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300994 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800995 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300996 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800997 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300998 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800999 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000
1001 /* Mark it as being initialized */
1002 retval->is_initialised = 1;
1003
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001004 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005 return (PyObject *)retval;
1006
1007error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001008 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001009 Py_XDECREF(retval);
1010 return NULL;
1011}
1012
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001013/*[clinic input]
1014zlib.Decompress.copy
1015
1016Return a copy of the decompression object.
1017[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001018
1019static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001020zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08001021/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022{
1023 compobject *retval = NULL;
1024 int err;
1025
1026 retval = newcompobject(&Decomptype);
1027 if (!retval) return NULL;
1028
1029 /* Copy the zstream state
1030 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1031 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001032 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001033 err = inflateCopy(&retval->zst, &self->zst);
1034 switch(err) {
1035 case(Z_OK):
1036 break;
1037 case(Z_STREAM_ERROR):
1038 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1039 goto error;
1040 case(Z_MEM_ERROR):
1041 PyErr_SetString(PyExc_MemoryError,
1042 "Can't allocate memory for decompression object");
1043 goto error;
1044 default:
1045 zlib_error(self->zst, err, "while copying decompression object");
1046 goto error;
1047 }
1048
1049 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001050 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001051 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001052 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001053 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001054 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001055 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056
1057 /* Mark it as being initialized */
1058 retval->is_initialised = 1;
1059
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001060 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001061 return (PyObject *)retval;
1062
1063error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001064 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065 Py_XDECREF(retval);
1066 return NULL;
1067}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001068#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001069
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001070/*[clinic input]
1071zlib.Decompress.flush
1072
Martin Pantere99e9772015-11-20 08:13:35 +00001073 length: capped_uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001074 the initial size of the output buffer.
1075 /
1076
1077Return a bytes object containing any remaining decompressed data.
1078[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001079
Guido van Rossumfb221561997-04-29 15:38:09 +00001080static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001081zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Martin Pantere99e9772015-11-20 08:13:35 +00001082/*[clinic end generated code: output=db6fb753ab698e22 input=1bb961eb21b62aa0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001083{
Victor Stinnere079edd2013-11-21 22:33:21 +01001084 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001085 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001086 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001087 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001088 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001089
Victor Stinnere079edd2013-11-21 22:33:21 +01001090 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001091 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1092 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001093 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001094
Gregory P. Smith693fc462008-09-06 20:13:06 +00001095 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001096 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001097
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001098
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001099 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001100
Victor Stinnere079edd2013-11-21 22:33:21 +01001101 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1102
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001103 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001104 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1105 or equal than UINT_MAX */
1106 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001107 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001108 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001109 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001110
1111 Py_BEGIN_ALLOW_THREADS
1112 err = inflate(&(self->zst), Z_FINISH);
1113 Py_END_ALLOW_THREADS
1114
1115 /* while Z_OK and the output buffer is full, there might be more output,
1116 so extend the output buffer and try again */
1117 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001118 if (length <= (UINT_MAX >> 1))
1119 new_length = length << 1;
1120 else
1121 new_length = UINT_MAX;
1122 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001123 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001124 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001125 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001126 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1127 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001128 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001129
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001130 Py_BEGIN_ALLOW_THREADS
1131 err = inflate(&(self->zst), Z_FINISH);
1132 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001133 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001134
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001135 if (save_unconsumed_input(self, err) < 0) {
1136 Py_DECREF(retval);
1137 retval = NULL;
1138 goto error;
1139 }
1140
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001141 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001142 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001143 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001144 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001145 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001146 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001147 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001148 Py_DECREF(retval);
1149 retval = NULL;
1150 goto error;
1151 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001152 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001153
Gregory P. Smith693fc462008-09-06 20:13:06 +00001154 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001155 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001156 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001157
1158error:
1159
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001160 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001161
Jeremy Hylton9714f992001-10-16 21:19:45 +00001162 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001163}
1164
Christian Heimes936e2f32014-01-27 01:06:57 +01001165#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001166
Guido van Rossumfb221561997-04-29 15:38:09 +00001167static PyMethodDef comp_methods[] =
1168{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001169 ZLIB_COMPRESS_COMPRESS_METHODDEF
1170 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001171#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001172 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001173#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001174 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001175};
1176
1177static PyMethodDef Decomp_methods[] =
1178{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001179 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001180 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001181#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001182 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001183#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001184 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001185};
1186
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001187#define COMP_OFF(x) offsetof(compobject, x)
1188static PyMemberDef Decomp_members[] = {
1189 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1190 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001191 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001192 {NULL},
1193};
Guido van Rossumfb221561997-04-29 15:38:09 +00001194
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001195/*[clinic input]
1196zlib.adler32
1197
1198 data: Py_buffer
1199 value: unsigned_int(bitwise=True) = 1
1200 Starting value of the checksum.
1201 /
1202
1203Compute an Adler-32 checksum of data.
1204
1205The returned checksum is an integer.
1206[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001207
Guido van Rossumfb221561997-04-29 15:38:09 +00001208static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001209zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1210/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001211{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001212 /* Releasing the GIL for very small buffers is inefficient
1213 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001214 if (data->len > 1024*5) {
1215 unsigned char *buf = data->buf;
1216 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001217
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001218 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001219 /* Avoid truncation of length for very large buffers. adler32() takes
1220 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001221 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001222 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001223 buf += (size_t) UINT_MAX;
1224 len -= (size_t) UINT_MAX;
1225 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001226 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001227 Py_END_ALLOW_THREADS
1228 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001229 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001230 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001231 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001232}
Tim Peters977e5402001-10-17 03:57:20 +00001233
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001234/*[clinic input]
1235zlib.crc32
1236
1237 data: Py_buffer
1238 value: unsigned_int(bitwise=True) = 0
1239 Starting value of the checksum.
1240 /
1241
1242Compute a CRC-32 checksum of data.
1243
1244The returned checksum is an integer.
1245[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001246
1247static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001248zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1249/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001250{
Martin v. Löwis423be952008-08-13 15:53:07 +00001251 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001252
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001253 /* Releasing the GIL for very small buffers is inefficient
1254 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001255 if (data->len > 1024*5) {
1256 unsigned char *buf = data->buf;
1257 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001258
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001259 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001260 /* Avoid truncation of length for very large buffers. crc32() takes
1261 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001262 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001263 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001264 buf += (size_t) UINT_MAX;
1265 len -= (size_t) UINT_MAX;
1266 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001267 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001268 Py_END_ALLOW_THREADS
1269 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001270 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001271 }
Christian Heimescc47b052008-03-25 14:56:36 +00001272 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001273}
Tim Peters977e5402001-10-17 03:57:20 +00001274
Guido van Rossumfb221561997-04-29 15:38:09 +00001275
1276static PyMethodDef zlib_methods[] =
1277{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001278 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001279 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001280 ZLIB_COMPRESSOBJ_METHODDEF
1281 ZLIB_CRC32_METHODDEF
1282 ZLIB_DECOMPRESS_METHODDEF
1283 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001284 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001285};
1286
Tim Peters0c322792002-07-17 16:49:03 +00001287static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001288 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001289 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001290 sizeof(compobject),
1291 0,
1292 (destructor)Comp_dealloc, /*tp_dealloc*/
1293 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001294 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001295 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001296 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001297 0, /*tp_repr*/
1298 0, /*tp_as_number*/
1299 0, /*tp_as_sequence*/
1300 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001301 0, /*tp_hash*/
1302 0, /*tp_call*/
1303 0, /*tp_str*/
1304 0, /*tp_getattro*/
1305 0, /*tp_setattro*/
1306 0, /*tp_as_buffer*/
1307 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1308 0, /*tp_doc*/
1309 0, /*tp_traverse*/
1310 0, /*tp_clear*/
1311 0, /*tp_richcompare*/
1312 0, /*tp_weaklistoffset*/
1313 0, /*tp_iter*/
1314 0, /*tp_iternext*/
1315 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001316};
1317
Tim Peters0c322792002-07-17 16:49:03 +00001318static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001319 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001320 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001321 sizeof(compobject),
1322 0,
1323 (destructor)Decomp_dealloc, /*tp_dealloc*/
1324 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001325 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001326 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001327 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001328 0, /*tp_repr*/
1329 0, /*tp_as_number*/
1330 0, /*tp_as_sequence*/
1331 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001332 0, /*tp_hash*/
1333 0, /*tp_call*/
1334 0, /*tp_str*/
1335 0, /*tp_getattro*/
1336 0, /*tp_setattro*/
1337 0, /*tp_as_buffer*/
1338 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1339 0, /*tp_doc*/
1340 0, /*tp_traverse*/
1341 0, /*tp_clear*/
1342 0, /*tp_richcompare*/
1343 0, /*tp_weaklistoffset*/
1344 0, /*tp_iter*/
1345 0, /*tp_iternext*/
1346 Decomp_methods, /*tp_methods*/
1347 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001348};
1349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001350PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001351"The functions in this module allow compression and decompression using the\n"
1352"zlib library, which is based on GNU zip.\n"
1353"\n"
1354"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001355"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001356"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001357"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001358"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001359"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001360"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001361"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001362"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001363"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001364
Martin v. Löwis1a214512008-06-11 05:26:20 +00001365static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001366 PyModuleDef_HEAD_INIT,
1367 "zlib",
1368 zlib_module_documentation,
1369 -1,
1370 zlib_methods,
1371 NULL,
1372 NULL,
1373 NULL,
1374 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001375};
1376
Mark Hammond62b1ab12002-07-23 06:31:15 +00001377PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001378PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001379{
Fred Drake4baedc12002-04-01 14:53:37 +00001380 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001381 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001382 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001383 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001384 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001385 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001386 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001387 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001388
Fred Drake4baedc12002-04-01 14:53:37 +00001389 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1390 if (ZlibError != NULL) {
1391 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001392 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001393 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001394 PyModule_AddIntMacro(m, MAX_WBITS);
1395 PyModule_AddIntMacro(m, DEFLATED);
1396 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001397 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001398 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1399 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1400 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1401 PyModule_AddIntMacro(m, Z_FILTERED);
1402 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1403 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001404
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001405 PyModule_AddIntMacro(m, Z_FINISH);
1406 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1407 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1408 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001409
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001410 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001411 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001412 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001413
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001414 ver = PyUnicode_FromString(zlibVersion());
1415 if (ver != NULL)
1416 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1417
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001418 PyModule_AddStringConstant(m, "__version__", "1.0");
1419
Martin v. Löwis1a214512008-06-11 05:26:20 +00001420 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001421}