blob: db3d8c9e1ea9ab6259f9eee3103066c5c6a8a5dd [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
273 The window buffer size.
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 Pantere99e9772015-11-20 08:13:35 +0000284/*[clinic end generated code: output=444d0987f3429574 input=da095118b3243b27]*/
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
398 The base two logarithm of the window size (range: 8..15).
399 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
400 Controls the amount of memory used for internal compression state.
401 Valid values range from 1 to 9. Higher values result in higher memory
402 usage, faster compression, and smaller output.
403 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
404 Used to tune the compression algorithm. Possible values are
405 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
406 zdict: Py_buffer = None
407 The predefined compression dictionary - a sequence of bytes
408 containing subsequences that are likely to occur in the input data.
409
410Return a compressor object.
411[clinic start generated code]*/
412
Guido van Rossumfb221561997-04-29 15:38:09 +0000413static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400414zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
415 int memLevel, int strategy, Py_buffer *zdict)
Martin Panter567d5132016-02-03 07:06:33 +0000416/*[clinic end generated code: output=2949bbb9a5723ccd input=de2ffab6e910cd8b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000417{
Victor Stinnere079edd2013-11-21 22:33:21 +0100418 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200419 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000420
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200421 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100422 PyErr_SetString(PyExc_OverflowError,
423 "zdict length does not fit in an unsigned int");
424 goto error;
425 }
426
Jeremy Hylton499000002001-10-16 21:59:35 +0000427 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000428 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200429 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200430 self->zst.opaque = NULL;
431 self->zst.zalloc = PyZlib_Malloc;
432 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000433 self->zst.next_in = NULL;
434 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000435 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
436 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000437 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000438 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200439 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200440 goto success;
441 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100442 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200443 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200444 switch (err) {
445 case (Z_OK):
446 goto success;
447 case (Z_STREAM_ERROR):
448 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
449 goto error;
450 default:
451 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
452 goto error;
453 }
454 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000455 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000456 PyErr_SetString(PyExc_MemoryError,
457 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200458 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000460 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200461 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 default:
463 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200464 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000465 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200466
467 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200468 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200469 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200470 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000471}
472
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200473/*[clinic input]
474zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200475
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200476 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
477 The window buffer size.
478 zdict: object(c_default="NULL") = b''
479 The predefined compression dictionary. This must be the same
480 dictionary as used by the compressor that produced the input data.
481
482Return a decompressor object.
483[clinic start generated code]*/
484
485static PyObject *
486zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict)
Larry Hastings581ee362014-01-28 05:00:08 -0800487/*[clinic end generated code: output=8ccd583fbd631798 input=67f05145a6920127]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200488{
489 int err;
490 compobject *self;
491
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200492 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
493 PyErr_SetString(PyExc_TypeError,
494 "zdict argument must support the buffer protocol");
495 return NULL;
496 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000497
498 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000499 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000500 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200501 self->zst.opaque = NULL;
502 self->zst.zalloc = PyZlib_Malloc;
503 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000504 self->zst.next_in = NULL;
505 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200506 if (zdict != NULL) {
507 Py_INCREF(zdict);
508 self->zdict = zdict;
509 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000510 err = inflateInit2(&self->zst, wbits);
511 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000512 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000513 self->is_initialised = 1;
514 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000515 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000516 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000517 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
518 return NULL;
519 case (Z_MEM_ERROR):
520 Py_DECREF(self);
521 PyErr_SetString(PyExc_MemoryError,
522 "Can't allocate memory for decompression object");
523 return NULL;
524 default:
525 zlib_error(self->zst, err, "while creating decompression object");
526 Py_DECREF(self);
527 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000528 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000529}
530
531static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000532Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000533{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000534#ifdef WITH_THREAD
535 PyThread_free_lock(self->lock);
536#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000537 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000538 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200539 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000540 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000541}
542
543static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000544Comp_dealloc(compobject *self)
545{
546 if (self->is_initialised)
547 deflateEnd(&self->zst);
548 Dealloc(self);
549}
550
551static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000552Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000553{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000554 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000555 inflateEnd(&self->zst);
556 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000557}
558
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200559/*[clinic input]
560zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000561
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200562 data: Py_buffer
563 Binary data to be compressed.
564 /
565
566Returns a bytes object containing compressed data.
567
568After calling this function, some of the input data may still
569be stored in internal buffers for later processing.
570Call the flush() method to clear these buffers.
571[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000572
Guido van Rossumfb221561997-04-29 15:38:09 +0000573static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200574zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800575/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000576{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200577 int err;
578 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200579 unsigned int length = DEF_BUF_SIZE, new_length;
580 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000581 Byte *input;
582 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000583
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200584 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200585 PyErr_SetString(PyExc_OverflowError,
586 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200587 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200588 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200589 input = data->buf;
590 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000591
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200592 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200593 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000594
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000595 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000596
Jeremy Hylton9714f992001-10-16 21:19:45 +0000597 start_total_out = self->zst.total_out;
598 self->zst.avail_in = inplen;
599 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000600 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000601 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000602
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000603 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000604 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000605 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000606
Jeremy Hylton9714f992001-10-16 21:19:45 +0000607 /* while Z_OK and the output buffer is full, there might be more output,
608 so extend the output buffer and try again */
609 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100610 if (length <= (UINT_MAX >> 1))
611 new_length = length << 1;
612 else
613 new_length = UINT_MAX;
614 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200615 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200616 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000617 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000618 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000619 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000620 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100621 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000622
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000623 Py_BEGIN_ALLOW_THREADS
624 err = deflate(&(self->zst), Z_NO_FLUSH);
625 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000626 }
Tim Peters977e5402001-10-17 03:57:20 +0000627 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000628 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000629 condition.
630 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000631
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000632 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200633 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200634 Py_CLEAR(RetVal);
635 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000636 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000637 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200638 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000639 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000640
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200641 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000642 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000644}
645
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100646/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
647 self->unused_data or self->unconsumed_tail, as appropriate. */
648static int
649save_unconsumed_input(compobject *self, int err)
650{
651 if (err == Z_STREAM_END) {
652 /* The end of the compressed data has been reached. Store the leftover
653 input data in self->unused_data. */
654 if (self->zst.avail_in > 0) {
655 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
656 Py_ssize_t new_size;
657 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100658 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100659 PyErr_NoMemory();
660 return -1;
661 }
662 new_size = old_size + self->zst.avail_in;
663 new_data = PyBytes_FromStringAndSize(NULL, new_size);
664 if (new_data == NULL)
665 return -1;
666 Py_MEMCPY(PyBytes_AS_STRING(new_data),
667 PyBytes_AS_STRING(self->unused_data), old_size);
668 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
669 self->zst.next_in, self->zst.avail_in);
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200670 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100671 self->zst.avail_in = 0;
672 }
673 }
674 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
675 /* This code handles two distinct cases:
676 1. Output limit was reached. Save leftover input in unconsumed_tail.
677 2. All input data was consumed. Clear unconsumed_tail. */
678 PyObject *new_data = PyBytes_FromStringAndSize(
679 (char *)self->zst.next_in, self->zst.avail_in);
680 if (new_data == NULL)
681 return -1;
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200682 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100683 }
684 return 0;
685}
686
Larry Hastings61272b72014-01-07 12:41:53 -0800687/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800688zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700689
690 data: Py_buffer
691 The binary data to decompress.
Martin Pantere99e9772015-11-20 08:13:35 +0000692 max_length: capped_uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700693 The maximum allowable length of the decompressed data.
694 Unconsumed input data will be stored in
695 the unconsumed_tail attribute.
696 /
697
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200698Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700699
700After calling this function, some of the input data may still be stored in
701internal buffers for later processing.
702Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800703[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700704
Larry Hastings31826802013-10-19 00:09:25 -0700705static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400706zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
707 unsigned int max_length)
Martin Pantere99e9772015-11-20 08:13:35 +0000708/*[clinic end generated code: output=b82e2a2c19f5fe7b input=68b6508ab07c2cf0]*/
Larry Hastings31826802013-10-19 00:09:25 -0700709{
Larry Hastings31826802013-10-19 00:09:25 -0700710 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200711 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200712 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000713 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000714
Victor Stinnere079edd2013-11-21 22:33:21 +0100715 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200716 PyErr_SetString(PyExc_OverflowError,
717 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700718 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200719 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000720
Jeremy Hylton9714f992001-10-16 21:19:45 +0000721 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000722 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000723 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200724 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700725 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000726
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800727 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000728
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800729 start_total_out = self->zst.total_out;
730 self->zst.avail_in = (unsigned int)data->len;
731 self->zst.next_in = data->buf;
732 self->zst.avail_out = length;
733 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000734
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000735 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800736 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000737 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000738
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800739 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200740 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800741 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200742 Py_DECREF(RetVal);
743 RetVal = NULL;
744 goto error;
745 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100746
747 if ((size_t)zdict_buf.len > UINT_MAX) {
748 PyErr_SetString(PyExc_OverflowError,
749 "zdict length does not fit in an unsigned int");
750 PyBuffer_Release(&zdict_buf);
751 Py_CLEAR(RetVal);
752 goto error;
753 }
754
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800755 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100756 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200757 PyBuffer_Release(&zdict_buf);
758 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800759 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100760 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200761 goto error;
762 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200763 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200764 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800765 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200766 Py_END_ALLOW_THREADS
767 }
768
Jeremy Hylton9714f992001-10-16 21:19:45 +0000769 /* While Z_OK and the output buffer is full, there might be more output.
770 So extend the output buffer and try again.
771 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800772 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000773 /* If max_length set, don't continue decompressing if we've already
774 reached the limit.
775 */
776 if (max_length && length >= max_length)
777 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000778
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000779 /* otherwise, ... */
780 old_length = length;
781 length = length << 1;
782 if (max_length && length > max_length)
783 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000784
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000785 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200786 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000787 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000788 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800789 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000790 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800791 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000792
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000793 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800794 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000795 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000796 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000797
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800798 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200799 Py_DECREF(RetVal);
800 RetVal = NULL;
801 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000802 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000803
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000804 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100805 /* This is the logical place to call inflateEnd, but the old behaviour
806 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800807 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100808 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000809 /* We will only get Z_BUF_ERROR if the output buffer was full
810 but there wasn't more output when we tried again, so it is
811 not an error condition.
812 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800813 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000814 Py_DECREF(RetVal);
815 RetVal = NULL;
816 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000817 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000818
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800819 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200820 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000821 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000822
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000823 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800824 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000825 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000826}
827
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200828/*[clinic input]
829zlib.Compress.flush
830
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200831 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200832 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
833 If mode == Z_FINISH, the compressor object can no longer be
834 used after calling the flush() method. Otherwise, more data
835 can still be compressed.
836 /
837
838Return a bytes object containing any remaining compressed data.
839[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000840
Guido van Rossumfb221561997-04-29 15:38:09 +0000841static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200842zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200843/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000844{
Victor Stinnere079edd2013-11-21 22:33:21 +0100845 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200846 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000847 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000848 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000849
Jeremy Hylton9714f992001-10-16 21:19:45 +0000850 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
851 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200852 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000853 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000854 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000855
Gregory P. Smith693fc462008-09-06 20:13:06 +0000856 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000857 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000858
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000859 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000860
Jeremy Hylton9714f992001-10-16 21:19:45 +0000861 start_total_out = self->zst.total_out;
862 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000863 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000864 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000865
866 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200867 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000868 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000869
Jeremy Hylton9714f992001-10-16 21:19:45 +0000870 /* while Z_OK and the output buffer is full, there might be more output,
871 so extend the output buffer and try again */
872 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100873 if (length <= (UINT_MAX >> 1))
874 new_length = length << 1;
875 else
876 new_length = UINT_MAX;
877 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200878 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000880 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000882 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100884 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000885
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200887 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000889 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000890
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200891 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000892 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200893 mode is Z_FINISH, but checking both for safety*/
894 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000895 err = deflateEnd(&(self->zst));
896 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200897 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 Py_DECREF(RetVal);
899 RetVal = NULL;
900 goto error;
901 }
902 else
903 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000904
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000905 /* We will only get Z_BUF_ERROR if the output buffer was full
906 but there wasn't more output when we tried again, so it is
907 not an error condition.
908 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000909 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000910 zlib_error(self->zst, err, "while flushing");
911 Py_DECREF(RetVal);
912 RetVal = NULL;
913 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000914 }
Tim Peters977e5402001-10-17 03:57:20 +0000915
Gregory P. Smith693fc462008-09-06 20:13:06 +0000916 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200917 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000918 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000919
Tim Peters977e5402001-10-17 03:57:20 +0000920 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000921 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000922
923 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000924}
925
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000926#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700927
Larry Hastings61272b72014-01-07 12:41:53 -0800928/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800929zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700930
931Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800932[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700933
Larry Hastings3cceb382014-01-04 11:09:09 -0800934static PyObject *
935zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800936/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000937{
938 compobject *retval = NULL;
939 int err;
940
941 retval = newcompobject(&Comptype);
942 if (!retval) return NULL;
943
944 /* Copy the zstream state
945 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
946 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800947 ENTER_ZLIB(self);
948 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949 switch(err) {
950 case(Z_OK):
951 break;
952 case(Z_STREAM_ERROR):
953 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
954 goto error;
955 case(Z_MEM_ERROR):
956 PyErr_SetString(PyExc_MemoryError,
957 "Can't allocate memory for compression object");
958 goto error;
959 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800960 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961 goto error;
962 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800963 Py_INCREF(self->unused_data);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200964 Py_SETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800965 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200966 Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800967 Py_XINCREF(self->zdict);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200968 Py_SETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800969 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970
971 /* Mark it as being initialized */
972 retval->is_initialised = 1;
973
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800974 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000975 return (PyObject *)retval;
976
977error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800978 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979 Py_XDECREF(retval);
980 return NULL;
981}
982
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200983/*[clinic input]
984zlib.Decompress.copy
985
986Return a copy of the decompression object.
987[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988
989static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200990zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800991/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000992{
993 compobject *retval = NULL;
994 int err;
995
996 retval = newcompobject(&Decomptype);
997 if (!retval) return NULL;
998
999 /* Copy the zstream state
1000 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1001 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001002 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003 err = inflateCopy(&retval->zst, &self->zst);
1004 switch(err) {
1005 case(Z_OK):
1006 break;
1007 case(Z_STREAM_ERROR):
1008 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1009 goto error;
1010 case(Z_MEM_ERROR):
1011 PyErr_SetString(PyExc_MemoryError,
1012 "Can't allocate memory for decompression object");
1013 goto error;
1014 default:
1015 zlib_error(self->zst, err, "while copying decompression object");
1016 goto error;
1017 }
1018
1019 Py_INCREF(self->unused_data);
Serhiy Storchaka191321d2015-12-27 15:41:34 +02001020 Py_SETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001021 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka191321d2015-12-27 15:41:34 +02001022 Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001023 Py_XINCREF(self->zdict);
Serhiy Storchaka191321d2015-12-27 15:41:34 +02001024 Py_SETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001025 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001026
1027 /* Mark it as being initialized */
1028 retval->is_initialised = 1;
1029
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001030 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001031 return (PyObject *)retval;
1032
1033error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001034 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035 Py_XDECREF(retval);
1036 return NULL;
1037}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001038#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001040/*[clinic input]
1041zlib.Decompress.flush
1042
Martin Pantere99e9772015-11-20 08:13:35 +00001043 length: capped_uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001044 the initial size of the output buffer.
1045 /
1046
1047Return a bytes object containing any remaining decompressed data.
1048[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001049
Guido van Rossumfb221561997-04-29 15:38:09 +00001050static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001051zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Martin Pantere99e9772015-11-20 08:13:35 +00001052/*[clinic end generated code: output=db6fb753ab698e22 input=1bb961eb21b62aa0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001053{
Victor Stinnere079edd2013-11-21 22:33:21 +01001054 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001055 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001056 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001057 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001058 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001059
Victor Stinnere079edd2013-11-21 22:33:21 +01001060 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001061 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1062 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001063 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001064
Gregory P. Smith693fc462008-09-06 20:13:06 +00001065 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001066 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001067
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001068
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001069 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001070
Victor Stinnere079edd2013-11-21 22:33:21 +01001071 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1072
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001073 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001074 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1075 or equal than UINT_MAX */
1076 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001077 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001078 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001079 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001080
1081 Py_BEGIN_ALLOW_THREADS
1082 err = inflate(&(self->zst), Z_FINISH);
1083 Py_END_ALLOW_THREADS
1084
1085 /* while Z_OK and the output buffer is full, there might be more output,
1086 so extend the output buffer and try again */
1087 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001088 if (length <= (UINT_MAX >> 1))
1089 new_length = length << 1;
1090 else
1091 new_length = UINT_MAX;
1092 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001093 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001094 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001095 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001096 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1097 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001098 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001099
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001100 Py_BEGIN_ALLOW_THREADS
1101 err = inflate(&(self->zst), Z_FINISH);
1102 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001103 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001104
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001105 if (save_unconsumed_input(self, err) < 0) {
1106 Py_DECREF(retval);
1107 retval = NULL;
1108 goto error;
1109 }
1110
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001111 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001112 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001113 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001114 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001115 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001116 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001117 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001118 Py_DECREF(retval);
1119 retval = NULL;
1120 goto error;
1121 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001122 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001123
Gregory P. Smith693fc462008-09-06 20:13:06 +00001124 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001125 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001126 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001127
1128error:
1129
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001130 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001131
Jeremy Hylton9714f992001-10-16 21:19:45 +00001132 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001133}
1134
Christian Heimes936e2f32014-01-27 01:06:57 +01001135#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001136
Guido van Rossumfb221561997-04-29 15:38:09 +00001137static PyMethodDef comp_methods[] =
1138{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001139 ZLIB_COMPRESS_COMPRESS_METHODDEF
1140 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001141#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001142 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001143#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001144 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001145};
1146
1147static PyMethodDef Decomp_methods[] =
1148{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001149 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001150 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001151#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001152 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001153#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001154 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001155};
1156
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001157#define COMP_OFF(x) offsetof(compobject, x)
1158static PyMemberDef Decomp_members[] = {
1159 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1160 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001161 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001162 {NULL},
1163};
Guido van Rossumfb221561997-04-29 15:38:09 +00001164
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001165/*[clinic input]
1166zlib.adler32
1167
1168 data: Py_buffer
1169 value: unsigned_int(bitwise=True) = 1
1170 Starting value of the checksum.
1171 /
1172
1173Compute an Adler-32 checksum of data.
1174
1175The returned checksum is an integer.
1176[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001177
Guido van Rossumfb221561997-04-29 15:38:09 +00001178static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001179zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001180/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001181{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001182 /* Releasing the GIL for very small buffers is inefficient
1183 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001184 if (data->len > 1024*5) {
1185 unsigned char *buf = data->buf;
1186 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001187
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001188 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001189 /* Avoid truncation of length for very large buffers. adler32() takes
1190 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001191 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001192 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001193 buf += (size_t) UINT_MAX;
1194 len -= (size_t) UINT_MAX;
1195 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001196 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001197 Py_END_ALLOW_THREADS
1198 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001199 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001200 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001201 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001202}
Tim Peters977e5402001-10-17 03:57:20 +00001203
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001204/*[clinic input]
1205zlib.crc32
1206
1207 data: Py_buffer
1208 value: unsigned_int(bitwise=True) = 0
1209 Starting value of the checksum.
1210 /
1211
1212Compute a CRC-32 checksum of data.
1213
1214The returned checksum is an integer.
1215[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001216
1217static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001218zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001219/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001220{
Martin v. Löwis423be952008-08-13 15:53:07 +00001221 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001222
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001223 /* Releasing the GIL for very small buffers is inefficient
1224 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001225 if (data->len > 1024*5) {
1226 unsigned char *buf = data->buf;
1227 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001228
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001229 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001230 /* Avoid truncation of length for very large buffers. crc32() takes
1231 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001232 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001233 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001234 buf += (size_t) UINT_MAX;
1235 len -= (size_t) UINT_MAX;
1236 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001237 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001238 Py_END_ALLOW_THREADS
1239 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001240 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001241 }
Christian Heimescc47b052008-03-25 14:56:36 +00001242 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001243}
Tim Peters977e5402001-10-17 03:57:20 +00001244
Guido van Rossumfb221561997-04-29 15:38:09 +00001245
1246static PyMethodDef zlib_methods[] =
1247{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001248 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001249 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001250 ZLIB_COMPRESSOBJ_METHODDEF
1251 ZLIB_CRC32_METHODDEF
1252 ZLIB_DECOMPRESS_METHODDEF
1253 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001254 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001255};
1256
Tim Peters0c322792002-07-17 16:49:03 +00001257static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001258 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001259 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001260 sizeof(compobject),
1261 0,
1262 (destructor)Comp_dealloc, /*tp_dealloc*/
1263 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001264 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001265 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001266 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001267 0, /*tp_repr*/
1268 0, /*tp_as_number*/
1269 0, /*tp_as_sequence*/
1270 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001271 0, /*tp_hash*/
1272 0, /*tp_call*/
1273 0, /*tp_str*/
1274 0, /*tp_getattro*/
1275 0, /*tp_setattro*/
1276 0, /*tp_as_buffer*/
1277 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1278 0, /*tp_doc*/
1279 0, /*tp_traverse*/
1280 0, /*tp_clear*/
1281 0, /*tp_richcompare*/
1282 0, /*tp_weaklistoffset*/
1283 0, /*tp_iter*/
1284 0, /*tp_iternext*/
1285 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001286};
1287
Tim Peters0c322792002-07-17 16:49:03 +00001288static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001289 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001290 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001291 sizeof(compobject),
1292 0,
1293 (destructor)Decomp_dealloc, /*tp_dealloc*/
1294 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001295 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001296 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001297 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001298 0, /*tp_repr*/
1299 0, /*tp_as_number*/
1300 0, /*tp_as_sequence*/
1301 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001302 0, /*tp_hash*/
1303 0, /*tp_call*/
1304 0, /*tp_str*/
1305 0, /*tp_getattro*/
1306 0, /*tp_setattro*/
1307 0, /*tp_as_buffer*/
1308 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1309 0, /*tp_doc*/
1310 0, /*tp_traverse*/
1311 0, /*tp_clear*/
1312 0, /*tp_richcompare*/
1313 0, /*tp_weaklistoffset*/
1314 0, /*tp_iter*/
1315 0, /*tp_iternext*/
1316 Decomp_methods, /*tp_methods*/
1317 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001318};
1319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001320PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001321"The functions in this module allow compression and decompression using the\n"
1322"zlib library, which is based on GNU zip.\n"
1323"\n"
1324"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001325"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001326"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001327"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001328"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001329"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001330"\n"
1331"'wbits' is window buffer size.\n"
1332"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001333"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001334
Martin v. Löwis1a214512008-06-11 05:26:20 +00001335static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001336 PyModuleDef_HEAD_INIT,
1337 "zlib",
1338 zlib_module_documentation,
1339 -1,
1340 zlib_methods,
1341 NULL,
1342 NULL,
1343 NULL,
1344 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001345};
1346
Mark Hammond62b1ab12002-07-23 06:31:15 +00001347PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001348PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001349{
Fred Drake4baedc12002-04-01 14:53:37 +00001350 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001351 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001352 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001353 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001354 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001355 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001356 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001357 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001358
Fred Drake4baedc12002-04-01 14:53:37 +00001359 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1360 if (ZlibError != NULL) {
1361 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001362 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001363 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001364 PyModule_AddIntMacro(m, MAX_WBITS);
1365 PyModule_AddIntMacro(m, DEFLATED);
1366 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001367 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001368 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1369 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1370 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1371 PyModule_AddIntMacro(m, Z_FILTERED);
1372 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1373 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001374
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001375 PyModule_AddIntMacro(m, Z_FINISH);
1376 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1377 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1378 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001379
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001380 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001381 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001382 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001383
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001384 ver = PyUnicode_FromString(zlibVersion());
1385 if (ver != NULL)
1386 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1387
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001388 PyModule_AddStringConstant(m, "__version__", "1.0");
1389
Martin v. Löwis1a214512008-06-11 05:26:20 +00001390 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001391}