blob: ecbcd154b3b89e6692077b0ebd89fbb58e97eff4 [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
Guido van Rossumfb221561997-04-29 15:38:09 +000025/* The following parameters are copied from zutil.h, version 0.95 */
26#define DEFLATED 8
27#if MAX_MEM_LEVEL >= 8
28# define DEF_MEM_LEVEL 8
29#else
30# define DEF_MEM_LEVEL MAX_MEM_LEVEL
31#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000032
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020033/* Initial buffer size. */
34#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Jeremy Hylton938ace62002-07-17 16:30:39 +000036static PyTypeObject Comptype;
37static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000038
39static PyObject *ZlibError;
40
Tim Peters977e5402001-10-17 03:57:20 +000041typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000042{
Jeremy Hylton9714f992001-10-16 21:19:45 +000043 PyObject_HEAD
44 z_stream zst;
45 PyObject *unused_data;
46 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020047 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000048 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020049 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000050 #ifdef WITH_THREAD
51 PyThread_type_lock lock;
52 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000053} compobject;
54
Jeremy Hylton0965e082001-10-16 21:56:09 +000055static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020056zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000057{
Nadeem Vawda524148a2011-08-28 11:26:46 +020058 const char *zmsg = Z_NULL;
59 /* In case of a version mismatch, zst.msg won't be initialized.
60 Check for this case first, before looking at zst.msg. */
61 if (err == Z_VERSION_ERROR)
62 zmsg = "library version mismatch";
63 if (zmsg == Z_NULL)
64 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000065 if (zmsg == Z_NULL) {
66 switch (err) {
67 case Z_BUF_ERROR:
68 zmsg = "incomplete or truncated stream";
69 break;
70 case Z_STREAM_ERROR:
71 zmsg = "inconsistent stream state";
72 break;
73 case Z_DATA_ERROR:
74 zmsg = "invalid input data";
75 break;
76 }
77 }
78 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000080 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000081 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000082}
83
Larry Hastings61272b72014-01-07 12:41:53 -080084/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080085module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080086class zlib.Compress "compobject *" "&Comptype"
87class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080088[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030089/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080090
Guido van Rossumfb221561997-04-29 15:38:09 +000091static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000092newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000093{
Tim Peters977e5402001-10-17 03:57:20 +000094 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000095 self = PyObject_New(compobject, type);
96 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020098 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200100 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000101 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000103 Py_DECREF(self);
104 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000106 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000107 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000108 Py_DECREF(self);
109 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000110 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000111#ifdef WITH_THREAD
112 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200113 if (self->lock == NULL) {
114 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
115 return NULL;
116 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000117#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000118 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000119}
120
Victor Stinner5064a522013-07-07 16:50:27 +0200121static void*
122PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
123{
124 if (items > (size_t)PY_SSIZE_T_MAX / size)
125 return NULL;
126 /* PyMem_Malloc() cannot be used: the GIL is not held when
127 inflate() and deflate() are called */
128 return PyMem_RawMalloc(items * size);
129}
130
131static void
132PyZlib_Free(voidpf ctx, void *ptr)
133{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200134 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200135}
136
Larry Hastings61272b72014-01-07 12:41:53 -0800137/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800138zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200139
Martin Panter1fe0d132016-02-10 10:06:36 +0000140 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800141 Binary data to be compressed.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200142 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000143 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800144
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200145Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800146[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800147
Guido van Rossumfb221561997-04-29 15:38:09 +0000148static PyObject *
Martin Panter1fe0d132016-02-10 10:06:36 +0000149zlib_compress_impl(PyModuleDef *module, Py_buffer *data, int level)
Martin Panterb0cb42d2016-02-10 10:45:54 +0000150/*[clinic end generated code: output=1b97589132b203b4 input=abed30f4fa14e213]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000151{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000152 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200153 Byte *input, *output = NULL;
154 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800155 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000156 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000157
Martin Panter1fe0d132016-02-10 10:06:36 +0000158 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200159 PyErr_SetString(PyExc_OverflowError,
160 "Size does not fit in an unsigned int");
161 goto error;
162 }
Martin Panter1fe0d132016-02-10 10:06:36 +0000163 input = data->buf;
164 length = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000165
Jeremy Hylton9714f992001-10-16 21:19:45 +0000166 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000167
Victor Stinnerb6404912013-07-07 16:21:41 +0200168 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000169 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000170 PyErr_SetString(PyExc_MemoryError,
171 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200172 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000173 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000174
Jeremy Hylton9714f992001-10-16 21:19:45 +0000175 /* Past the point of no return. From here on out, we need to make sure
176 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000177
Victor Stinner5064a522013-07-07 16:50:27 +0200178 zst.opaque = NULL;
179 zst.zalloc = PyZlib_Malloc;
180 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000181 zst.next_out = (Byte *)output;
182 zst.next_in = (Byte *)input;
183 zst.avail_in = length;
184 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000185
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000186 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000187 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000188 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000189 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 PyErr_SetString(PyExc_MemoryError,
191 "Out of memory while compressing data");
192 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000193 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000194 PyErr_SetString(ZlibError,
195 "Bad compression level");
196 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000197 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000198 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000199 zlib_error(zst, err, "while compressing data");
200 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000201 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000202
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000203 Py_BEGIN_ALLOW_THREADS;
204 err = deflate(&zst, Z_FINISH);
205 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000206
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000207 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000208 zlib_error(zst, err, "while compressing data");
209 deflateEnd(&zst);
210 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000211 }
Tim Peters977e5402001-10-17 03:57:20 +0000212
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000213 err=deflateEnd(&zst);
214 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000215 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000216 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000217 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000218 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000219
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000220 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200221 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000222
Jeremy Hylton9714f992001-10-16 21:19:45 +0000223 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000224}
225
Larry Hastings61272b72014-01-07 12:41:53 -0800226/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100227
Martin Pantere99e9772015-11-20 08:13:35 +0000228class capped_uint_converter(CConverter):
Victor Stinnere079edd2013-11-21 22:33:21 +0100229 type = 'unsigned int'
Martin Pantere99e9772015-11-20 08:13:35 +0000230 converter = 'capped_uint_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200231 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100232
Larry Hastings61272b72014-01-07 12:41:53 -0800233[python start generated code]*/
Martin Pantere99e9772015-11-20 08:13:35 +0000234/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100235
236static int
Martin Pantere99e9772015-11-20 08:13:35 +0000237capped_uint_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100238{
Martin Pantere99e9772015-11-20 08:13:35 +0000239 PyObject *long_obj;
240 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100241
Martin Pantere99e9772015-11-20 08:13:35 +0000242 long_obj = (PyObject *)_PyLong_FromNbInt(obj);
243 if (long_obj == NULL) {
244 return 0;
245 }
246 val = PyLong_AsSsize_t(long_obj);
247 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100248 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000249 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100250 }
Martin Pantere99e9772015-11-20 08:13:35 +0000251 if (val < 0) {
252 PyErr_SetString(PyExc_ValueError,
253 "value must be positive");
Victor Stinner5c867332014-01-03 12:26:12 +0100254 return 0;
255 }
256
Martin Pantere99e9772015-11-20 08:13:35 +0000257 if ((size_t)val > UINT_MAX) {
258 *(unsigned int *)ptr = UINT_MAX;
259 }
260 else {
261 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(val, Py_ssize_t,
262 unsigned int);
263 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100264 return 1;
265}
266
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200267/*[clinic input]
268zlib.decompress
269
270 data: Py_buffer
271 Compressed data.
272 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000273 The window buffer size and container format.
Martin Pantere99e9772015-11-20 08:13:35 +0000274 bufsize: capped_uint(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200275 The initial output buffer size.
276 /
277
278Returns a bytes object containing the uncompressed data.
279[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000280
Guido van Rossumfb221561997-04-29 15:38:09 +0000281static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400282zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits,
283 unsigned int bufsize)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000284/*[clinic end generated code: output=444d0987f3429574 input=75123b0d4ff0541d]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000285{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200286 PyObject *result_str = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000287 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200288 unsigned int length;
289 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200290 unsigned int new_bufsize;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000291 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000292
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200293 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200294 PyErr_SetString(PyExc_OverflowError,
295 "Size does not fit in an unsigned int");
296 goto error;
297 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200298 input = data->buf;
299 length = (unsigned int)data->len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000300
Victor Stinnere079edd2013-11-21 22:33:21 +0100301 if (bufsize == 0)
302 bufsize = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000303
Jeremy Hylton9714f992001-10-16 21:19:45 +0000304 zst.avail_in = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100305 zst.avail_out = bufsize;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000306
Victor Stinnere079edd2013-11-21 22:33:21 +0100307 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200308 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000309
Victor Stinner5064a522013-07-07 16:50:27 +0200310 zst.opaque = NULL;
311 zst.zalloc = PyZlib_Malloc;
312 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000313 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000314 zst.next_in = (Byte *)input;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200315 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000316
Jeremy Hylton9714f992001-10-16 21:19:45 +0000317 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000318 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000319 break;
Tim Peters977e5402001-10-17 03:57:20 +0000320 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000321 PyErr_SetString(PyExc_MemoryError,
322 "Out of memory while decompressing data");
323 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000324 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000325 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000326 zlib_error(zst, err, "while preparing to decompress data");
327 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000328 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000329
Jeremy Hylton9714f992001-10-16 21:19:45 +0000330 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 Py_BEGIN_ALLOW_THREADS
332 err=inflate(&zst, Z_FINISH);
333 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000334
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 switch(err) {
336 case(Z_STREAM_END):
337 break;
338 case(Z_BUF_ERROR):
339 /*
340 * If there is at least 1 byte of room according to zst.avail_out
341 * and we get this error, assume that it means zlib cannot
342 * process the inflate call() due to an error in the data.
343 */
344 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000345 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000346 inflateEnd(&zst);
347 goto error;
348 }
349 /* fall through */
350 case(Z_OK):
351 /* need more memory */
Victor Stinnere079edd2013-11-21 22:33:21 +0100352 if (bufsize <= (UINT_MAX >> 1))
353 new_bufsize = bufsize << 1;
354 else
355 new_bufsize = UINT_MAX;
356 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000357 inflateEnd(&zst);
358 goto error;
359 }
360 zst.next_out =
Victor Stinnere079edd2013-11-21 22:33:21 +0100361 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
362 zst.avail_out = bufsize;
363 bufsize = new_bufsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000364 break;
365 default:
366 inflateEnd(&zst);
367 zlib_error(zst, err, "while decompressing data");
368 goto error;
369 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000370 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000371
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000372 err = inflateEnd(&zst);
373 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200374 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000375 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000376 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000377
Gregory P. Smith693fc462008-09-06 20:13:06 +0000378 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000379 goto error;
380
Jeremy Hylton9714f992001-10-16 21:19:45 +0000381 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000382
383 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000384 Py_XDECREF(result_str);
385 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000386}
387
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200388/*[clinic input]
389zlib.compressobj
390
391 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000392 The compression level (an integer in the range 0-9 or -1; default is
393 currently equivalent to 6). Higher compression levels are slower,
394 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200395 method: int(c_default="DEFLATED") = DEFLATED
396 The compression algorithm. If given, this must be DEFLATED.
397 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000398 +9 to +15: The base-two logarithm of the window size. Include a zlib
399 container.
400 -9 to -15: Generate a raw stream.
401 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200402 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
403 Controls the amount of memory used for internal compression state.
404 Valid values range from 1 to 9. Higher values result in higher memory
405 usage, faster compression, and smaller output.
406 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
407 Used to tune the compression algorithm. Possible values are
408 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
409 zdict: Py_buffer = None
410 The predefined compression dictionary - a sequence of bytes
411 containing subsequences that are likely to occur in the input data.
412
413Return a compressor object.
414[clinic start generated code]*/
415
Guido van Rossumfb221561997-04-29 15:38:09 +0000416static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400417zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
418 int memLevel, int strategy, Py_buffer *zdict)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000419/*[clinic end generated code: output=2949bbb9a5723ccd input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000420{
Victor Stinnere079edd2013-11-21 22:33:21 +0100421 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200422 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000423
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200424 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100425 PyErr_SetString(PyExc_OverflowError,
426 "zdict length does not fit in an unsigned int");
427 goto error;
428 }
429
Jeremy Hylton499000002001-10-16 21:59:35 +0000430 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000431 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200432 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200433 self->zst.opaque = NULL;
434 self->zst.zalloc = PyZlib_Malloc;
435 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000436 self->zst.next_in = NULL;
437 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000438 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
439 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000440 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000441 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200442 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200443 goto success;
444 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100445 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200446 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200447 switch (err) {
448 case (Z_OK):
449 goto success;
450 case (Z_STREAM_ERROR):
451 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
452 goto error;
453 default:
454 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
455 goto error;
456 }
457 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000458 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 PyErr_SetString(PyExc_MemoryError,
460 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200461 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200464 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000465 default:
466 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200467 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000468 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200469
470 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200471 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200472 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200473 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000474}
475
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200476/*[clinic input]
477zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200478
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200479 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000480 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200481 zdict: object(c_default="NULL") = b''
482 The predefined compression dictionary. This must be the same
483 dictionary as used by the compressor that produced the input data.
484
485Return a decompressor object.
486[clinic start generated code]*/
487
488static PyObject *
489zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000490/*[clinic end generated code: output=8ccd583fbd631798 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200491{
492 int err;
493 compobject *self;
494
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200495 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
496 PyErr_SetString(PyExc_TypeError,
497 "zdict argument must support the buffer protocol");
498 return NULL;
499 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000500
501 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000502 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000503 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200504 self->zst.opaque = NULL;
505 self->zst.zalloc = PyZlib_Malloc;
506 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000507 self->zst.next_in = NULL;
508 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200509 if (zdict != NULL) {
510 Py_INCREF(zdict);
511 self->zdict = zdict;
512 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000513 err = inflateInit2(&self->zst, wbits);
514 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000515 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000516 self->is_initialised = 1;
517 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000518 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000519 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000520 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
521 return NULL;
522 case (Z_MEM_ERROR):
523 Py_DECREF(self);
524 PyErr_SetString(PyExc_MemoryError,
525 "Can't allocate memory for decompression object");
526 return NULL;
527 default:
528 zlib_error(self->zst, err, "while creating decompression object");
529 Py_DECREF(self);
530 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000531 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000532}
533
534static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000535Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000536{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000537#ifdef WITH_THREAD
538 PyThread_free_lock(self->lock);
539#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000540 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000541 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200542 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000543 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000544}
545
546static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000547Comp_dealloc(compobject *self)
548{
549 if (self->is_initialised)
550 deflateEnd(&self->zst);
551 Dealloc(self);
552}
553
554static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000555Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000556{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000557 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000558 inflateEnd(&self->zst);
559 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000560}
561
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200562/*[clinic input]
563zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000564
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200565 data: Py_buffer
566 Binary data to be compressed.
567 /
568
569Returns a bytes object containing compressed data.
570
571After calling this function, some of the input data may still
572be stored in internal buffers for later processing.
573Call the flush() method to clear these buffers.
574[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000575
Guido van Rossumfb221561997-04-29 15:38:09 +0000576static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200577zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800578/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000579{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200580 int err;
581 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200582 unsigned int length = DEF_BUF_SIZE, new_length;
583 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000584 Byte *input;
585 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000586
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200587 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200588 PyErr_SetString(PyExc_OverflowError,
589 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200590 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200591 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200592 input = data->buf;
593 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000594
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200595 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200596 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000597
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000598 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000599
Jeremy Hylton9714f992001-10-16 21:19:45 +0000600 start_total_out = self->zst.total_out;
601 self->zst.avail_in = inplen;
602 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000603 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000604 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000605
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000606 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000607 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000608 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000609
Jeremy Hylton9714f992001-10-16 21:19:45 +0000610 /* while Z_OK and the output buffer is full, there might be more output,
611 so extend the output buffer and try again */
612 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100613 if (length <= (UINT_MAX >> 1))
614 new_length = length << 1;
615 else
616 new_length = UINT_MAX;
617 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200618 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200619 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000620 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000621 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000622 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000623 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100624 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000625
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000626 Py_BEGIN_ALLOW_THREADS
627 err = deflate(&(self->zst), Z_NO_FLUSH);
628 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000629 }
Tim Peters977e5402001-10-17 03:57:20 +0000630 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000631 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000632 condition.
633 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000634
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000635 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200636 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200637 Py_CLEAR(RetVal);
638 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000639 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000640 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200641 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000642 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000643
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200644 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000645 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000646 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000647}
648
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100649/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
650 self->unused_data or self->unconsumed_tail, as appropriate. */
651static int
652save_unconsumed_input(compobject *self, int err)
653{
654 if (err == Z_STREAM_END) {
655 /* The end of the compressed data has been reached. Store the leftover
656 input data in self->unused_data. */
657 if (self->zst.avail_in > 0) {
658 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
659 Py_ssize_t new_size;
660 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100661 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100662 PyErr_NoMemory();
663 return -1;
664 }
665 new_size = old_size + self->zst.avail_in;
666 new_data = PyBytes_FromStringAndSize(NULL, new_size);
667 if (new_data == NULL)
668 return -1;
669 Py_MEMCPY(PyBytes_AS_STRING(new_data),
670 PyBytes_AS_STRING(self->unused_data), old_size);
671 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
672 self->zst.next_in, self->zst.avail_in);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300673 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100674 self->zst.avail_in = 0;
675 }
676 }
677 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
678 /* This code handles two distinct cases:
679 1. Output limit was reached. Save leftover input in unconsumed_tail.
680 2. All input data was consumed. Clear unconsumed_tail. */
681 PyObject *new_data = PyBytes_FromStringAndSize(
682 (char *)self->zst.next_in, self->zst.avail_in);
683 if (new_data == NULL)
684 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300685 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100686 }
687 return 0;
688}
689
Larry Hastings61272b72014-01-07 12:41:53 -0800690/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800691zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700692
693 data: Py_buffer
694 The binary data to decompress.
Martin Pantere99e9772015-11-20 08:13:35 +0000695 max_length: capped_uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700696 The maximum allowable length of the decompressed data.
697 Unconsumed input data will be stored in
698 the unconsumed_tail attribute.
699 /
700
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200701Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700702
703After calling this function, some of the input data may still be stored in
704internal buffers for later processing.
705Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800706[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700707
Larry Hastings31826802013-10-19 00:09:25 -0700708static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400709zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
710 unsigned int max_length)
Martin Pantere99e9772015-11-20 08:13:35 +0000711/*[clinic end generated code: output=b82e2a2c19f5fe7b input=68b6508ab07c2cf0]*/
Larry Hastings31826802013-10-19 00:09:25 -0700712{
Larry Hastings31826802013-10-19 00:09:25 -0700713 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200714 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200715 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000716 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000717
Victor Stinnere079edd2013-11-21 22:33:21 +0100718 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200719 PyErr_SetString(PyExc_OverflowError,
720 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700721 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200722 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000723
Jeremy Hylton9714f992001-10-16 21:19:45 +0000724 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000725 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000726 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200727 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700728 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000729
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800730 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000731
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800732 start_total_out = self->zst.total_out;
733 self->zst.avail_in = (unsigned int)data->len;
734 self->zst.next_in = data->buf;
735 self->zst.avail_out = length;
736 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000737
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000738 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800739 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000740 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000741
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800742 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200743 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800744 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200745 Py_DECREF(RetVal);
746 RetVal = NULL;
747 goto error;
748 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100749
750 if ((size_t)zdict_buf.len > UINT_MAX) {
751 PyErr_SetString(PyExc_OverflowError,
752 "zdict length does not fit in an unsigned int");
753 PyBuffer_Release(&zdict_buf);
754 Py_CLEAR(RetVal);
755 goto error;
756 }
757
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800758 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100759 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200760 PyBuffer_Release(&zdict_buf);
761 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800762 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100763 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200764 goto error;
765 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200766 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200767 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800768 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200769 Py_END_ALLOW_THREADS
770 }
771
Jeremy Hylton9714f992001-10-16 21:19:45 +0000772 /* While Z_OK and the output buffer is full, there might be more output.
773 So extend the output buffer and try again.
774 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800775 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000776 /* If max_length set, don't continue decompressing if we've already
777 reached the limit.
778 */
779 if (max_length && length >= max_length)
780 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000781
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 /* otherwise, ... */
783 old_length = length;
784 length = length << 1;
785 if (max_length && length > max_length)
786 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000787
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000788 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200789 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000790 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000791 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800792 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000793 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800794 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000795
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000796 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800797 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000798 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000799 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000800
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800801 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200802 Py_DECREF(RetVal);
803 RetVal = NULL;
804 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000805 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000806
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000807 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100808 /* This is the logical place to call inflateEnd, but the old behaviour
809 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800810 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100811 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000812 /* We will only get Z_BUF_ERROR if the output buffer was full
813 but there wasn't more output when we tried again, so it is
814 not an error condition.
815 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800816 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000817 Py_DECREF(RetVal);
818 RetVal = NULL;
819 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000820 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000821
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800822 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200823 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000824 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000825
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000826 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800827 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000828 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000829}
830
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200831/*[clinic input]
832zlib.Compress.flush
833
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200834 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200835 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
836 If mode == Z_FINISH, the compressor object can no longer be
837 used after calling the flush() method. Otherwise, more data
838 can still be compressed.
839 /
840
841Return a bytes object containing any remaining compressed data.
842[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000843
Guido van Rossumfb221561997-04-29 15:38:09 +0000844static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200845zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200846/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000847{
Victor Stinnere079edd2013-11-21 22:33:21 +0100848 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200849 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000850 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000851 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000852
Jeremy Hylton9714f992001-10-16 21:19:45 +0000853 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
854 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200855 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000856 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000857 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000858
Gregory P. Smith693fc462008-09-06 20:13:06 +0000859 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000861
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000862 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000863
Jeremy Hylton9714f992001-10-16 21:19:45 +0000864 start_total_out = self->zst.total_out;
865 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000866 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000867 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000868
869 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200870 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000871 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000872
Jeremy Hylton9714f992001-10-16 21:19:45 +0000873 /* while Z_OK and the output buffer is full, there might be more output,
874 so extend the output buffer and try again */
875 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100876 if (length <= (UINT_MAX >> 1))
877 new_length = length << 1;
878 else
879 new_length = UINT_MAX;
880 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200881 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000882 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000883 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000885 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100887 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000888
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000889 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200890 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000891 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000892 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000893
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200894 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000895 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200896 mode is Z_FINISH, but checking both for safety*/
897 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 err = deflateEnd(&(self->zst));
899 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200900 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000901 Py_DECREF(RetVal);
902 RetVal = NULL;
903 goto error;
904 }
905 else
906 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000907
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 /* We will only get Z_BUF_ERROR if the output buffer was full
909 but there wasn't more output when we tried again, so it is
910 not an error condition.
911 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000912 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000913 zlib_error(self->zst, err, "while flushing");
914 Py_DECREF(RetVal);
915 RetVal = NULL;
916 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000917 }
Tim Peters977e5402001-10-17 03:57:20 +0000918
Gregory P. Smith693fc462008-09-06 20:13:06 +0000919 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200920 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000921 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000922
Tim Peters977e5402001-10-17 03:57:20 +0000923 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000924 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000925
926 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000927}
928
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700930
Larry Hastings61272b72014-01-07 12:41:53 -0800931/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800932zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700933
934Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800935[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700936
Larry Hastings3cceb382014-01-04 11:09:09 -0800937static PyObject *
938zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800939/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940{
941 compobject *retval = NULL;
942 int err;
943
944 retval = newcompobject(&Comptype);
945 if (!retval) return NULL;
946
947 /* Copy the zstream state
948 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
949 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800950 ENTER_ZLIB(self);
951 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000952 switch(err) {
953 case(Z_OK):
954 break;
955 case(Z_STREAM_ERROR):
956 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
957 goto error;
958 case(Z_MEM_ERROR):
959 PyErr_SetString(PyExc_MemoryError,
960 "Can't allocate memory for compression object");
961 goto error;
962 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800963 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 goto error;
965 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800966 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300967 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800968 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300969 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800970 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300971 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800972 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973
974 /* Mark it as being initialized */
975 retval->is_initialised = 1;
976
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800977 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978 return (PyObject *)retval;
979
980error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800981 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000982 Py_XDECREF(retval);
983 return NULL;
984}
985
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200986/*[clinic input]
987zlib.Decompress.copy
988
989Return a copy of the decompression object.
990[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000991
992static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200993zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800994/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000995{
996 compobject *retval = NULL;
997 int err;
998
999 retval = newcompobject(&Decomptype);
1000 if (!retval) return NULL;
1001
1002 /* Copy the zstream state
1003 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1004 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001005 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006 err = inflateCopy(&retval->zst, &self->zst);
1007 switch(err) {
1008 case(Z_OK):
1009 break;
1010 case(Z_STREAM_ERROR):
1011 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1012 goto error;
1013 case(Z_MEM_ERROR):
1014 PyErr_SetString(PyExc_MemoryError,
1015 "Can't allocate memory for decompression object");
1016 goto error;
1017 default:
1018 zlib_error(self->zst, err, "while copying decompression object");
1019 goto error;
1020 }
1021
1022 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001023 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001025 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001026 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001027 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001028 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001029
1030 /* Mark it as being initialized */
1031 retval->is_initialised = 1;
1032
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001033 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034 return (PyObject *)retval;
1035
1036error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001037 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038 Py_XDECREF(retval);
1039 return NULL;
1040}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001041#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001042
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001043/*[clinic input]
1044zlib.Decompress.flush
1045
Martin Pantere99e9772015-11-20 08:13:35 +00001046 length: capped_uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001047 the initial size of the output buffer.
1048 /
1049
1050Return a bytes object containing any remaining decompressed data.
1051[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001052
Guido van Rossumfb221561997-04-29 15:38:09 +00001053static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001054zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Martin Pantere99e9772015-11-20 08:13:35 +00001055/*[clinic end generated code: output=db6fb753ab698e22 input=1bb961eb21b62aa0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001056{
Victor Stinnere079edd2013-11-21 22:33:21 +01001057 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001058 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001059 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001060 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001061 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001062
Victor Stinnere079edd2013-11-21 22:33:21 +01001063 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001064 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1065 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001066 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001067
Gregory P. Smith693fc462008-09-06 20:13:06 +00001068 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001069 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001070
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001071
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001072 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001073
Victor Stinnere079edd2013-11-21 22:33:21 +01001074 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1075
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001076 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001077 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1078 or equal than UINT_MAX */
1079 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001080 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001081 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001082 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001083
1084 Py_BEGIN_ALLOW_THREADS
1085 err = inflate(&(self->zst), Z_FINISH);
1086 Py_END_ALLOW_THREADS
1087
1088 /* while Z_OK and the output buffer is full, there might be more output,
1089 so extend the output buffer and try again */
1090 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001091 if (length <= (UINT_MAX >> 1))
1092 new_length = length << 1;
1093 else
1094 new_length = UINT_MAX;
1095 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001096 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001097 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001098 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001099 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1100 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001101 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001102
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001103 Py_BEGIN_ALLOW_THREADS
1104 err = inflate(&(self->zst), Z_FINISH);
1105 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001106 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001107
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001108 if (save_unconsumed_input(self, err) < 0) {
1109 Py_DECREF(retval);
1110 retval = NULL;
1111 goto error;
1112 }
1113
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001114 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001115 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001116 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001117 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001118 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001119 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001120 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001121 Py_DECREF(retval);
1122 retval = NULL;
1123 goto error;
1124 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001125 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001126
Gregory P. Smith693fc462008-09-06 20:13:06 +00001127 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001128 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001129 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001130
1131error:
1132
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001133 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001134
Jeremy Hylton9714f992001-10-16 21:19:45 +00001135 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001136}
1137
Christian Heimes936e2f32014-01-27 01:06:57 +01001138#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001139
Guido van Rossumfb221561997-04-29 15:38:09 +00001140static PyMethodDef comp_methods[] =
1141{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001142 ZLIB_COMPRESS_COMPRESS_METHODDEF
1143 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001144#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001145 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001146#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001147 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001148};
1149
1150static PyMethodDef Decomp_methods[] =
1151{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001152 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001153 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001154#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001155 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001156#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001157 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001158};
1159
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001160#define COMP_OFF(x) offsetof(compobject, x)
1161static PyMemberDef Decomp_members[] = {
1162 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1163 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001164 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001165 {NULL},
1166};
Guido van Rossumfb221561997-04-29 15:38:09 +00001167
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001168/*[clinic input]
1169zlib.adler32
1170
1171 data: Py_buffer
1172 value: unsigned_int(bitwise=True) = 1
1173 Starting value of the checksum.
1174 /
1175
1176Compute an Adler-32 checksum of data.
1177
1178The returned checksum is an integer.
1179[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001180
Guido van Rossumfb221561997-04-29 15:38:09 +00001181static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001182zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001183/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001184{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001185 /* Releasing the GIL for very small buffers is inefficient
1186 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001187 if (data->len > 1024*5) {
1188 unsigned char *buf = data->buf;
1189 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001190
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001191 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001192 /* Avoid truncation of length for very large buffers. adler32() takes
1193 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001194 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001195 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001196 buf += (size_t) UINT_MAX;
1197 len -= (size_t) UINT_MAX;
1198 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001199 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001200 Py_END_ALLOW_THREADS
1201 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001202 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001203 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001204 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001205}
Tim Peters977e5402001-10-17 03:57:20 +00001206
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001207/*[clinic input]
1208zlib.crc32
1209
1210 data: Py_buffer
1211 value: unsigned_int(bitwise=True) = 0
1212 Starting value of the checksum.
1213 /
1214
1215Compute a CRC-32 checksum of data.
1216
1217The returned checksum is an integer.
1218[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001219
1220static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001221zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001222/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001223{
Martin v. Löwis423be952008-08-13 15:53:07 +00001224 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001225
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001226 /* Releasing the GIL for very small buffers is inefficient
1227 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001228 if (data->len > 1024*5) {
1229 unsigned char *buf = data->buf;
1230 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001231
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001232 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001233 /* Avoid truncation of length for very large buffers. crc32() takes
1234 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001235 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001236 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001237 buf += (size_t) UINT_MAX;
1238 len -= (size_t) UINT_MAX;
1239 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001240 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001241 Py_END_ALLOW_THREADS
1242 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001243 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001244 }
Christian Heimescc47b052008-03-25 14:56:36 +00001245 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001246}
Tim Peters977e5402001-10-17 03:57:20 +00001247
Guido van Rossumfb221561997-04-29 15:38:09 +00001248
1249static PyMethodDef zlib_methods[] =
1250{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001251 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001252 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001253 ZLIB_COMPRESSOBJ_METHODDEF
1254 ZLIB_CRC32_METHODDEF
1255 ZLIB_DECOMPRESS_METHODDEF
1256 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001257 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001258};
1259
Tim Peters0c322792002-07-17 16:49:03 +00001260static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001261 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001262 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001263 sizeof(compobject),
1264 0,
1265 (destructor)Comp_dealloc, /*tp_dealloc*/
1266 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001267 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001268 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001269 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001270 0, /*tp_repr*/
1271 0, /*tp_as_number*/
1272 0, /*tp_as_sequence*/
1273 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001274 0, /*tp_hash*/
1275 0, /*tp_call*/
1276 0, /*tp_str*/
1277 0, /*tp_getattro*/
1278 0, /*tp_setattro*/
1279 0, /*tp_as_buffer*/
1280 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1281 0, /*tp_doc*/
1282 0, /*tp_traverse*/
1283 0, /*tp_clear*/
1284 0, /*tp_richcompare*/
1285 0, /*tp_weaklistoffset*/
1286 0, /*tp_iter*/
1287 0, /*tp_iternext*/
1288 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001289};
1290
Tim Peters0c322792002-07-17 16:49:03 +00001291static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001292 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001293 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001294 sizeof(compobject),
1295 0,
1296 (destructor)Decomp_dealloc, /*tp_dealloc*/
1297 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001298 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001299 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001300 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001301 0, /*tp_repr*/
1302 0, /*tp_as_number*/
1303 0, /*tp_as_sequence*/
1304 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001305 0, /*tp_hash*/
1306 0, /*tp_call*/
1307 0, /*tp_str*/
1308 0, /*tp_getattro*/
1309 0, /*tp_setattro*/
1310 0, /*tp_as_buffer*/
1311 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1312 0, /*tp_doc*/
1313 0, /*tp_traverse*/
1314 0, /*tp_clear*/
1315 0, /*tp_richcompare*/
1316 0, /*tp_weaklistoffset*/
1317 0, /*tp_iter*/
1318 0, /*tp_iternext*/
1319 Decomp_methods, /*tp_methods*/
1320 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001321};
1322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001323PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001324"The functions in this module allow compression and decompression using the\n"
1325"zlib library, which is based on GNU zip.\n"
1326"\n"
1327"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001328"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001329"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001330"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001331"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001332"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001333"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001334"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001335"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001336"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001337
Martin v. Löwis1a214512008-06-11 05:26:20 +00001338static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001339 PyModuleDef_HEAD_INIT,
1340 "zlib",
1341 zlib_module_documentation,
1342 -1,
1343 zlib_methods,
1344 NULL,
1345 NULL,
1346 NULL,
1347 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001348};
1349
Mark Hammond62b1ab12002-07-23 06:31:15 +00001350PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001351PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001352{
Fred Drake4baedc12002-04-01 14:53:37 +00001353 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001354 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001355 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001356 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001357 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001358 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001359 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001360 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001361
Fred Drake4baedc12002-04-01 14:53:37 +00001362 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1363 if (ZlibError != NULL) {
1364 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001365 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001366 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001367 PyModule_AddIntMacro(m, MAX_WBITS);
1368 PyModule_AddIntMacro(m, DEFLATED);
1369 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001370 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001371 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1372 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1373 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1374 PyModule_AddIntMacro(m, Z_FILTERED);
1375 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1376 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001377
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001378 PyModule_AddIntMacro(m, Z_FINISH);
1379 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1380 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1381 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001382
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001383 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001384 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001385 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001386
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001387 ver = PyUnicode_FromString(zlibVersion());
1388 if (ver != NULL)
1389 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1390
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001391 PyModule_AddStringConstant(m, "__version__", "1.0");
1392
Martin v. Löwis1a214512008-06-11 05:26:20 +00001393 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001394}