blob: 0d2e188b433d19dd656283547535a07628f5d0c3 [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
56zlib_error(z_stream zst, int err, char *msg)
57{
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]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020085output preset file
Larry Hastingsebdcb502013-11-23 14:54:00 -080086module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080087class zlib.Compress "compobject *" "&Comptype"
88class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080089[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080090/*[clinic end generated code: output=da39a3ee5e6b4b0d input=bfd4c340573ba91d]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080091
Guido van Rossumfb221561997-04-29 15:38:09 +000092static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000093newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000094{
Tim Peters977e5402001-10-17 03:57:20 +000095 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000096 self = PyObject_New(compobject, type);
97 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000098 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020099 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000100 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200101 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000102 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000103 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000104 Py_DECREF(self);
105 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000106 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000107 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000109 Py_DECREF(self);
110 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000112#ifdef WITH_THREAD
113 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200114 if (self->lock == NULL) {
115 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
116 return NULL;
117 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000118#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000120}
121
Victor Stinner5064a522013-07-07 16:50:27 +0200122static void*
123PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
124{
125 if (items > (size_t)PY_SSIZE_T_MAX / size)
126 return NULL;
127 /* PyMem_Malloc() cannot be used: the GIL is not held when
128 inflate() and deflate() are called */
129 return PyMem_RawMalloc(items * size);
130}
131
132static void
133PyZlib_Free(voidpf ctx, void *ptr)
134{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200135 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200136}
137
Larry Hastings61272b72014-01-07 12:41:53 -0800138/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800139zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200140
Larry Hastingsebdcb502013-11-23 14:54:00 -0800141 bytes: Py_buffer
142 Binary data to be compressed.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200143 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Larry Hastingsebdcb502013-11-23 14:54:00 -0800144 Compression level, in 0-9.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800145 /
146
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200147Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800148[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800149
Guido van Rossumfb221561997-04-29 15:38:09 +0000150static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200151zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int level)
Larry Hastings581ee362014-01-28 05:00:08 -0800152/*[clinic end generated code: output=5d7dd4588788efd3 input=be3abe9934bda4b3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000153{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000154 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200155 Byte *input, *output = NULL;
156 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800157 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000158 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000159
Larry Hastingsebdcb502013-11-23 14:54:00 -0800160 if ((size_t)bytes->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200161 PyErr_SetString(PyExc_OverflowError,
162 "Size does not fit in an unsigned int");
163 goto error;
164 }
Larry Hastingsebdcb502013-11-23 14:54:00 -0800165 input = bytes->buf;
166 length = (unsigned int)bytes->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000167
Jeremy Hylton9714f992001-10-16 21:19:45 +0000168 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000169
Victor Stinnerb6404912013-07-07 16:21:41 +0200170 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000171 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000172 PyErr_SetString(PyExc_MemoryError,
173 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200174 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000175 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000176
Jeremy Hylton9714f992001-10-16 21:19:45 +0000177 /* Past the point of no return. From here on out, we need to make sure
178 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000179
Victor Stinner5064a522013-07-07 16:50:27 +0200180 zst.opaque = NULL;
181 zst.zalloc = PyZlib_Malloc;
182 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000183 zst.next_out = (Byte *)output;
184 zst.next_in = (Byte *)input;
185 zst.avail_in = length;
186 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000187
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000188 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000189 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000191 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 PyErr_SetString(PyExc_MemoryError,
193 "Out of memory while compressing data");
194 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000195 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000196 PyErr_SetString(ZlibError,
197 "Bad compression level");
198 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000199 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000200 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000201 zlib_error(zst, err, "while compressing data");
202 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000203 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000204
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000205 Py_BEGIN_ALLOW_THREADS;
206 err = deflate(&zst, Z_FINISH);
207 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000208
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000209 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000210 zlib_error(zst, err, "while compressing data");
211 deflateEnd(&zst);
212 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000213 }
Tim Peters977e5402001-10-17 03:57:20 +0000214
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000215 err=deflateEnd(&zst);
216 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000218 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000219 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000221
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000222 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200223 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000224
Jeremy Hylton9714f992001-10-16 21:19:45 +0000225 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000226}
227
Larry Hastings61272b72014-01-07 12:41:53 -0800228/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100229
230class uint_converter(CConverter):
231 type = 'unsigned int'
232 converter = 'uint_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200233 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100234
Larry Hastings61272b72014-01-07 12:41:53 -0800235[python start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800236/*[python end generated code: output=da39a3ee5e6b4b0d input=22263855f7a3ebfd]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100237
238static int
239uint_converter(PyObject *obj, void *ptr)
240{
241 long val;
242 unsigned long uval;
243
244 val = PyLong_AsLong(obj);
245 if (val == -1 && PyErr_Occurred()) {
246 uval = PyLong_AsUnsignedLong(obj);
247 if (uval == (unsigned long)-1 && PyErr_Occurred())
248 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100249 }
250 else {
251 if (val < 0) {
252 PyErr_SetString(PyExc_ValueError,
253 "value must be positive");
254 return 0;
255 }
256 uval = (unsigned long)val;
257 }
258
Victor Stinner5c867332014-01-03 12:26:12 +0100259 if (uval > UINT_MAX) {
260 PyErr_SetString(PyExc_OverflowError,
261 "Python int too large for C unsigned int");
262 return 0;
263 }
264
Victor Stinnere079edd2013-11-21 22:33:21 +0100265 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int);
266 return 1;
267}
268
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200269/*[clinic input]
270zlib.decompress
271
272 data: Py_buffer
273 Compressed data.
274 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
275 The window buffer size.
276 bufsize: uint(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
277 The initial output buffer size.
278 /
279
280Returns a bytes object containing the uncompressed data.
281[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000282
Guido van Rossumfb221561997-04-29 15:38:09 +0000283static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200284zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits, unsigned int bufsize)
Larry Hastings581ee362014-01-28 05:00:08 -0800285/*[clinic end generated code: output=9e5464e72df9cb5f input=0f4b9abb7103f50e]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000286{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200287 PyObject *result_str = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000288 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200289 unsigned int length;
290 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200291 unsigned int new_bufsize;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000292 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000293
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200294 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200295 PyErr_SetString(PyExc_OverflowError,
296 "Size does not fit in an unsigned int");
297 goto error;
298 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200299 input = data->buf;
300 length = (unsigned int)data->len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000301
Victor Stinnere079edd2013-11-21 22:33:21 +0100302 if (bufsize == 0)
303 bufsize = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000304
Jeremy Hylton9714f992001-10-16 21:19:45 +0000305 zst.avail_in = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100306 zst.avail_out = bufsize;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000307
Victor Stinnere079edd2013-11-21 22:33:21 +0100308 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200309 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000310
Victor Stinner5064a522013-07-07 16:50:27 +0200311 zst.opaque = NULL;
312 zst.zalloc = PyZlib_Malloc;
313 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000314 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000315 zst.next_in = (Byte *)input;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200316 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000317
Jeremy Hylton9714f992001-10-16 21:19:45 +0000318 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000319 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000320 break;
Tim Peters977e5402001-10-17 03:57:20 +0000321 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000322 PyErr_SetString(PyExc_MemoryError,
323 "Out of memory while decompressing data");
324 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000325 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000326 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000327 zlib_error(zst, err, "while preparing to decompress data");
328 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000329 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000330
Jeremy Hylton9714f992001-10-16 21:19:45 +0000331 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 Py_BEGIN_ALLOW_THREADS
333 err=inflate(&zst, Z_FINISH);
334 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000335
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 switch(err) {
337 case(Z_STREAM_END):
338 break;
339 case(Z_BUF_ERROR):
340 /*
341 * If there is at least 1 byte of room according to zst.avail_out
342 * and we get this error, assume that it means zlib cannot
343 * process the inflate call() due to an error in the data.
344 */
345 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000346 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000347 inflateEnd(&zst);
348 goto error;
349 }
350 /* fall through */
351 case(Z_OK):
352 /* need more memory */
Victor Stinnere079edd2013-11-21 22:33:21 +0100353 if (bufsize <= (UINT_MAX >> 1))
354 new_bufsize = bufsize << 1;
355 else
356 new_bufsize = UINT_MAX;
357 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000358 inflateEnd(&zst);
359 goto error;
360 }
361 zst.next_out =
Victor Stinnere079edd2013-11-21 22:33:21 +0100362 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
363 zst.avail_out = bufsize;
364 bufsize = new_bufsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000365 break;
366 default:
367 inflateEnd(&zst);
368 zlib_error(zst, err, "while decompressing data");
369 goto error;
370 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000371 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000372
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000373 err = inflateEnd(&zst);
374 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200375 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000376 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000377 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000378
Gregory P. Smith693fc462008-09-06 20:13:06 +0000379 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000380 goto error;
381
Jeremy Hylton9714f992001-10-16 21:19:45 +0000382 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000383
384 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000385 Py_XDECREF(result_str);
386 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000387}
388
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200389/*[clinic input]
390zlib.compressobj
391
392 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
393 The compression level (an integer in the range 0-9; default is 6).
394 Higher compression levels are slower, but produce smaller results.
395 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 *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200414zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits, int memLevel, int strategy, Py_buffer *zdict)
Larry Hastings581ee362014-01-28 05:00:08 -0800415/*[clinic end generated code: output=89e5a6c1449caa9e input=b034847f8821f6af]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000416{
Victor Stinnere079edd2013-11-21 22:33:21 +0100417 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200418 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000419
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200420 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100421 PyErr_SetString(PyExc_OverflowError,
422 "zdict length does not fit in an unsigned int");
423 goto error;
424 }
425
Jeremy Hylton499000002001-10-16 21:59:35 +0000426 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000427 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200428 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200429 self->zst.opaque = NULL;
430 self->zst.zalloc = PyZlib_Malloc;
431 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000432 self->zst.next_in = NULL;
433 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000434 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
435 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000436 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000437 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200438 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200439 goto success;
440 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100441 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200442 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200443 switch (err) {
444 case (Z_OK):
445 goto success;
446 case (Z_STREAM_ERROR):
447 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
448 goto error;
449 default:
450 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
451 goto error;
452 }
453 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000454 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 PyErr_SetString(PyExc_MemoryError,
456 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200457 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200460 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000461 default:
462 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200463 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000464 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200465
466 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200467 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200468 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200469 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000470}
471
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200472/*[clinic input]
473zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200474
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200475 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
476 The window buffer size.
477 zdict: object(c_default="NULL") = b''
478 The predefined compression dictionary. This must be the same
479 dictionary as used by the compressor that produced the input data.
480
481Return a decompressor object.
482[clinic start generated code]*/
483
484static PyObject *
485zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict)
Larry Hastings581ee362014-01-28 05:00:08 -0800486/*[clinic end generated code: output=8ccd583fbd631798 input=67f05145a6920127]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200487{
488 int err;
489 compobject *self;
490
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200491 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
492 PyErr_SetString(PyExc_TypeError,
493 "zdict argument must support the buffer protocol");
494 return NULL;
495 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000496
497 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000498 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200500 self->zst.opaque = NULL;
501 self->zst.zalloc = PyZlib_Malloc;
502 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000503 self->zst.next_in = NULL;
504 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200505 if (zdict != NULL) {
506 Py_INCREF(zdict);
507 self->zdict = zdict;
508 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000509 err = inflateInit2(&self->zst, wbits);
510 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000511 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000512 self->is_initialised = 1;
513 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000514 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000515 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000516 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
517 return NULL;
518 case (Z_MEM_ERROR):
519 Py_DECREF(self);
520 PyErr_SetString(PyExc_MemoryError,
521 "Can't allocate memory for decompression object");
522 return NULL;
523 default:
524 zlib_error(self->zst, err, "while creating decompression object");
525 Py_DECREF(self);
526 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000527 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000528}
529
530static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000531Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000532{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000533#ifdef WITH_THREAD
534 PyThread_free_lock(self->lock);
535#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000536 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000537 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200538 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000539 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000540}
541
542static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000543Comp_dealloc(compobject *self)
544{
545 if (self->is_initialised)
546 deflateEnd(&self->zst);
547 Dealloc(self);
548}
549
550static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000551Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000552{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000553 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000554 inflateEnd(&self->zst);
555 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000556}
557
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200558/*[clinic input]
559zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000560
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200561 data: Py_buffer
562 Binary data to be compressed.
563 /
564
565Returns a bytes object containing compressed data.
566
567After calling this function, some of the input data may still
568be stored in internal buffers for later processing.
569Call the flush() method to clear these buffers.
570[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000571
Guido van Rossumfb221561997-04-29 15:38:09 +0000572static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200573zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800574/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000575{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200576 int err;
577 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200578 unsigned int length = DEF_BUF_SIZE, new_length;
579 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000580 Byte *input;
581 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000582
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200583 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200584 PyErr_SetString(PyExc_OverflowError,
585 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200586 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200587 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200588 input = data->buf;
589 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000590
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200591 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200592 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000593
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000594 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595
Jeremy Hylton9714f992001-10-16 21:19:45 +0000596 start_total_out = self->zst.total_out;
597 self->zst.avail_in = inplen;
598 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000599 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000600 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000601
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000602 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000603 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000604 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000605
Jeremy Hylton9714f992001-10-16 21:19:45 +0000606 /* while Z_OK and the output buffer is full, there might be more output,
607 so extend the output buffer and try again */
608 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100609 if (length <= (UINT_MAX >> 1))
610 new_length = length << 1;
611 else
612 new_length = UINT_MAX;
613 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200614 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200615 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000616 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000618 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000619 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100620 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000621
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 Py_BEGIN_ALLOW_THREADS
623 err = deflate(&(self->zst), Z_NO_FLUSH);
624 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000625 }
Tim Peters977e5402001-10-17 03:57:20 +0000626 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000627 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000628 condition.
629 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000630
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000631 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200632 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200633 Py_CLEAR(RetVal);
634 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000635 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000636 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200637 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000638 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000639
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200640 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000641 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000642 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000643}
644
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100645/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
646 self->unused_data or self->unconsumed_tail, as appropriate. */
647static int
648save_unconsumed_input(compobject *self, int err)
649{
650 if (err == Z_STREAM_END) {
651 /* The end of the compressed data has been reached. Store the leftover
652 input data in self->unused_data. */
653 if (self->zst.avail_in > 0) {
654 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
655 Py_ssize_t new_size;
656 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100657 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100658 PyErr_NoMemory();
659 return -1;
660 }
661 new_size = old_size + self->zst.avail_in;
662 new_data = PyBytes_FromStringAndSize(NULL, new_size);
663 if (new_data == NULL)
664 return -1;
665 Py_MEMCPY(PyBytes_AS_STRING(new_data),
666 PyBytes_AS_STRING(self->unused_data), old_size);
667 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
668 self->zst.next_in, self->zst.avail_in);
669 Py_DECREF(self->unused_data);
670 self->unused_data = new_data;
671 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;
682 Py_DECREF(self->unconsumed_tail);
683 self->unconsumed_tail = new_data;
684 }
685 return 0;
686}
687
Larry Hastings61272b72014-01-07 12:41:53 -0800688/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800689zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700690
691 data: Py_buffer
692 The binary data to decompress.
Victor Stinnere079edd2013-11-21 22:33:21 +0100693 max_length: uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700694 The maximum allowable length of the decompressed data.
695 Unconsumed input data will be stored in
696 the unconsumed_tail attribute.
697 /
698
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200699Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700700
701After calling this function, some of the input data may still be stored in
702internal buffers for later processing.
703Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800704[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700705
Larry Hastings31826802013-10-19 00:09:25 -0700706static PyObject *
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800707zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length)
Larry Hastings581ee362014-01-28 05:00:08 -0800708/*[clinic end generated code: output=755cccc9087bfe55 input=02cfc047377cec86]*/
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);
964 Py_INCREF(self->unconsumed_tail);
965 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966 Py_XDECREF(retval->unused_data);
967 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200968 Py_XDECREF(retval->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800969 retval->unused_data = self->unused_data;
970 retval->unconsumed_tail = self->unconsumed_tail;
971 retval->zdict = self->zdict;
972 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);
1023 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001024 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025 Py_XDECREF(retval->unused_data);
1026 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001027 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001028 retval->unused_data = self->unused_data;
1029 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001030 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001031 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032
1033 /* Mark it as being initialized */
1034 retval->is_initialised = 1;
1035
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001036 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001037 return (PyObject *)retval;
1038
1039error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001040 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001041 Py_XDECREF(retval);
1042 return NULL;
1043}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001044#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001045
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001046/*[clinic input]
1047zlib.Decompress.flush
1048
Serhiy Storchaka54c13912014-02-05 13:34:01 +02001049 length: uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001050 the initial size of the output buffer.
1051 /
1052
1053Return a bytes object containing any remaining decompressed data.
1054[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001055
Guido van Rossumfb221561997-04-29 15:38:09 +00001056static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001057zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Serhiy Storchaka54c13912014-02-05 13:34:01 +02001058/*[clinic end generated code: output=db6fb753ab698e22 input=1580956505978993]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001059{
Victor Stinnere079edd2013-11-21 22:33:21 +01001060 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001061 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001062 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001063 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001064 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001065
Victor Stinnere079edd2013-11-21 22:33:21 +01001066 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001067 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1068 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001069 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001070
Gregory P. Smith693fc462008-09-06 20:13:06 +00001071 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001072 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001073
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001074
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001075 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001076
Victor Stinnere079edd2013-11-21 22:33:21 +01001077 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1078
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001079 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001080 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1081 or equal than UINT_MAX */
1082 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001083 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001084 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001085 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001086
1087 Py_BEGIN_ALLOW_THREADS
1088 err = inflate(&(self->zst), Z_FINISH);
1089 Py_END_ALLOW_THREADS
1090
1091 /* while Z_OK and the output buffer is full, there might be more output,
1092 so extend the output buffer and try again */
1093 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001094 if (length <= (UINT_MAX >> 1))
1095 new_length = length << 1;
1096 else
1097 new_length = UINT_MAX;
1098 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001099 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001100 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001101 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001102 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1103 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001104 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001105
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001106 Py_BEGIN_ALLOW_THREADS
1107 err = inflate(&(self->zst), Z_FINISH);
1108 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001109 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001110
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001111 if (save_unconsumed_input(self, err) < 0) {
1112 Py_DECREF(retval);
1113 retval = NULL;
1114 goto error;
1115 }
1116
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001117 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001118 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001119 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001120 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001121 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001122 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001123 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001124 Py_DECREF(retval);
1125 retval = NULL;
1126 goto error;
1127 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001128 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001129
Gregory P. Smith693fc462008-09-06 20:13:06 +00001130 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001131 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001132 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001133
1134error:
1135
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001136 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001137
Jeremy Hylton9714f992001-10-16 21:19:45 +00001138 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001139}
1140
Christian Heimes936e2f32014-01-27 01:06:57 +01001141#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001142
Guido van Rossumfb221561997-04-29 15:38:09 +00001143static PyMethodDef comp_methods[] =
1144{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001145 ZLIB_COMPRESS_COMPRESS_METHODDEF
1146 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001147#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001148 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001149#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001150 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001151};
1152
1153static PyMethodDef Decomp_methods[] =
1154{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001155 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001156 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001157#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001158 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001159#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001160 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001161};
1162
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001163#define COMP_OFF(x) offsetof(compobject, x)
1164static PyMemberDef Decomp_members[] = {
1165 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1166 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001167 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001168 {NULL},
1169};
Guido van Rossumfb221561997-04-29 15:38:09 +00001170
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001171/*[clinic input]
1172zlib.adler32
1173
1174 data: Py_buffer
1175 value: unsigned_int(bitwise=True) = 1
1176 Starting value of the checksum.
1177 /
1178
1179Compute an Adler-32 checksum of data.
1180
1181The returned checksum is an integer.
1182[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001183
Guido van Rossumfb221561997-04-29 15:38:09 +00001184static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001185zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001186/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001187{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001188 /* Releasing the GIL for very small buffers is inefficient
1189 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001190 if (data->len > 1024*5) {
1191 unsigned char *buf = data->buf;
1192 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001193
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001194 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001195 /* Avoid truncation of length for very large buffers. adler32() takes
1196 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001197 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001198 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001199 buf += (size_t) UINT_MAX;
1200 len -= (size_t) UINT_MAX;
1201 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001202 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001203 Py_END_ALLOW_THREADS
1204 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001205 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001206 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001207 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001208}
Tim Peters977e5402001-10-17 03:57:20 +00001209
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001210/*[clinic input]
1211zlib.crc32
1212
1213 data: Py_buffer
1214 value: unsigned_int(bitwise=True) = 0
1215 Starting value of the checksum.
1216 /
1217
1218Compute a CRC-32 checksum of data.
1219
1220The returned checksum is an integer.
1221[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001222
1223static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001224zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001225/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001226{
Martin v. Löwis423be952008-08-13 15:53:07 +00001227 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001228
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001229 /* Releasing the GIL for very small buffers is inefficient
1230 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001231 if (data->len > 1024*5) {
1232 unsigned char *buf = data->buf;
1233 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001234
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001235 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001236 /* Avoid truncation of length for very large buffers. crc32() takes
1237 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001238 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001239 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001240 buf += (size_t) UINT_MAX;
1241 len -= (size_t) UINT_MAX;
1242 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001243 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001244 Py_END_ALLOW_THREADS
1245 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001246 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001247 }
Christian Heimescc47b052008-03-25 14:56:36 +00001248 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001249}
Tim Peters977e5402001-10-17 03:57:20 +00001250
Guido van Rossumfb221561997-04-29 15:38:09 +00001251
1252static PyMethodDef zlib_methods[] =
1253{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001254 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001255 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001256 ZLIB_COMPRESSOBJ_METHODDEF
1257 ZLIB_CRC32_METHODDEF
1258 ZLIB_DECOMPRESS_METHODDEF
1259 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001260 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001261};
1262
Tim Peters0c322792002-07-17 16:49:03 +00001263static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001264 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001265 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001266 sizeof(compobject),
1267 0,
1268 (destructor)Comp_dealloc, /*tp_dealloc*/
1269 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001270 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001271 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001272 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001273 0, /*tp_repr*/
1274 0, /*tp_as_number*/
1275 0, /*tp_as_sequence*/
1276 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001277 0, /*tp_hash*/
1278 0, /*tp_call*/
1279 0, /*tp_str*/
1280 0, /*tp_getattro*/
1281 0, /*tp_setattro*/
1282 0, /*tp_as_buffer*/
1283 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1284 0, /*tp_doc*/
1285 0, /*tp_traverse*/
1286 0, /*tp_clear*/
1287 0, /*tp_richcompare*/
1288 0, /*tp_weaklistoffset*/
1289 0, /*tp_iter*/
1290 0, /*tp_iternext*/
1291 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001292};
1293
Tim Peters0c322792002-07-17 16:49:03 +00001294static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001295 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001296 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001297 sizeof(compobject),
1298 0,
1299 (destructor)Decomp_dealloc, /*tp_dealloc*/
1300 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001301 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001302 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001303 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001304 0, /*tp_repr*/
1305 0, /*tp_as_number*/
1306 0, /*tp_as_sequence*/
1307 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001308 0, /*tp_hash*/
1309 0, /*tp_call*/
1310 0, /*tp_str*/
1311 0, /*tp_getattro*/
1312 0, /*tp_setattro*/
1313 0, /*tp_as_buffer*/
1314 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1315 0, /*tp_doc*/
1316 0, /*tp_traverse*/
1317 0, /*tp_clear*/
1318 0, /*tp_richcompare*/
1319 0, /*tp_weaklistoffset*/
1320 0, /*tp_iter*/
1321 0, /*tp_iternext*/
1322 Decomp_methods, /*tp_methods*/
1323 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001324};
1325
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001326PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001327"The functions in this module allow compression and decompression using the\n"
1328"zlib library, which is based on GNU zip.\n"
1329"\n"
1330"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001331"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001332"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001333"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001334"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001335"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001336"\n"
1337"'wbits' is window buffer size.\n"
1338"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001339"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001340
Martin v. Löwis1a214512008-06-11 05:26:20 +00001341static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001342 PyModuleDef_HEAD_INIT,
1343 "zlib",
1344 zlib_module_documentation,
1345 -1,
1346 zlib_methods,
1347 NULL,
1348 NULL,
1349 NULL,
1350 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001351};
1352
Mark Hammond62b1ab12002-07-23 06:31:15 +00001353PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001354PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001355{
Fred Drake4baedc12002-04-01 14:53:37 +00001356 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001357 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001358 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001359 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001360 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001361 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001362 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001363 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001364
Fred Drake4baedc12002-04-01 14:53:37 +00001365 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1366 if (ZlibError != NULL) {
1367 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001368 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001369 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001370 PyModule_AddIntMacro(m, MAX_WBITS);
1371 PyModule_AddIntMacro(m, DEFLATED);
1372 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001373 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001374 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1375 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1376 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1377 PyModule_AddIntMacro(m, Z_FILTERED);
1378 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1379 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001380
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001381 PyModule_AddIntMacro(m, Z_FINISH);
1382 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1383 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1384 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001385
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001386 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001387 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001388 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001389
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001390 ver = PyUnicode_FromString(zlibVersion());
1391 if (ver != NULL)
1392 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1393
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001394 PyModule_AddStringConstant(m, "__version__", "1.0");
1395
Martin v. Löwis1a214512008-06-11 05:26:20 +00001396 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001397}