blob: 1997b40d18274f67c762a6728a15bfea724dc74c [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]
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
Larry Hastingsebdcb502013-11-23 14:54:00 -0800140 bytes: Py_buffer
141 Binary data to be compressed.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200142 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Larry Hastingsebdcb502013-11-23 14:54:00 -0800143 Compression level, in 0-9.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800144 /
145
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200146Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800147[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800148
Guido van Rossumfb221561997-04-29 15:38:09 +0000149static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200150zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int level)
Larry Hastings581ee362014-01-28 05:00:08 -0800151/*[clinic end generated code: output=5d7dd4588788efd3 input=be3abe9934bda4b3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000152{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000153 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200154 Byte *input, *output = NULL;
155 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800156 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000157 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000158
Larry Hastingsebdcb502013-11-23 14:54:00 -0800159 if ((size_t)bytes->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200160 PyErr_SetString(PyExc_OverflowError,
161 "Size does not fit in an unsigned int");
162 goto error;
163 }
Larry Hastingsebdcb502013-11-23 14:54:00 -0800164 input = bytes->buf;
165 length = (unsigned int)bytes->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000166
Jeremy Hylton9714f992001-10-16 21:19:45 +0000167 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000168
Victor Stinnerb6404912013-07-07 16:21:41 +0200169 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000170 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 PyErr_SetString(PyExc_MemoryError,
172 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200173 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000174 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000175
Jeremy Hylton9714f992001-10-16 21:19:45 +0000176 /* Past the point of no return. From here on out, we need to make sure
177 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000178
Victor Stinner5064a522013-07-07 16:50:27 +0200179 zst.opaque = NULL;
180 zst.zalloc = PyZlib_Malloc;
181 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 zst.next_out = (Byte *)output;
183 zst.next_in = (Byte *)input;
184 zst.avail_in = length;
185 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000186
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000187 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000188 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000190 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000191 PyErr_SetString(PyExc_MemoryError,
192 "Out of memory while compressing data");
193 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000194 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000195 PyErr_SetString(ZlibError,
196 "Bad compression level");
197 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000198 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000199 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000200 zlib_error(zst, err, "while compressing data");
201 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000202 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000203
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000204 Py_BEGIN_ALLOW_THREADS;
205 err = deflate(&zst, Z_FINISH);
206 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000208 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000209 zlib_error(zst, err, "while compressing data");
210 deflateEnd(&zst);
211 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000212 }
Tim Peters977e5402001-10-17 03:57:20 +0000213
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000214 err=deflateEnd(&zst);
215 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000217 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000218 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000219 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000220
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000221 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200222 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Jeremy Hylton9714f992001-10-16 21:19:45 +0000224 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000225}
226
Larry Hastings61272b72014-01-07 12:41:53 -0800227/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100228
229class uint_converter(CConverter):
230 type = 'unsigned int'
231 converter = 'uint_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200232 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100233
Larry Hastings61272b72014-01-07 12:41:53 -0800234[python start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800235/*[python end generated code: output=da39a3ee5e6b4b0d input=22263855f7a3ebfd]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100236
237static int
238uint_converter(PyObject *obj, void *ptr)
239{
240 long val;
241 unsigned long uval;
242
243 val = PyLong_AsLong(obj);
244 if (val == -1 && PyErr_Occurred()) {
245 uval = PyLong_AsUnsignedLong(obj);
246 if (uval == (unsigned long)-1 && PyErr_Occurred())
247 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100248 }
249 else {
250 if (val < 0) {
251 PyErr_SetString(PyExc_ValueError,
252 "value must be positive");
253 return 0;
254 }
255 uval = (unsigned long)val;
256 }
257
Victor Stinner5c867332014-01-03 12:26:12 +0100258 if (uval > UINT_MAX) {
259 PyErr_SetString(PyExc_OverflowError,
260 "Python int too large for C unsigned int");
261 return 0;
262 }
263
Victor Stinnere079edd2013-11-21 22:33:21 +0100264 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int);
265 return 1;
266}
267
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200268/*[clinic input]
269zlib.decompress
270
271 data: Py_buffer
272 Compressed data.
273 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
274 The window buffer size.
275 bufsize: uint(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
276 The initial output buffer size.
277 /
278
279Returns a bytes object containing the uncompressed data.
280[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000281
Guido van Rossumfb221561997-04-29 15:38:09 +0000282static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400283zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits,
284 unsigned int bufsize)
285/*[clinic end generated code: output=444d0987f3429574 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 *
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)
416/*[clinic end generated code: output=2949bbb9a5723ccd input=b034847f8821f6af]*/
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);
670 Py_DECREF(self->unused_data);
671 self->unused_data = new_data;
672 self->zst.avail_in = 0;
673 }
674 }
675 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
676 /* This code handles two distinct cases:
677 1. Output limit was reached. Save leftover input in unconsumed_tail.
678 2. All input data was consumed. Clear unconsumed_tail. */
679 PyObject *new_data = PyBytes_FromStringAndSize(
680 (char *)self->zst.next_in, self->zst.avail_in);
681 if (new_data == NULL)
682 return -1;
683 Py_DECREF(self->unconsumed_tail);
684 self->unconsumed_tail = new_data;
685 }
686 return 0;
687}
688
Larry Hastings61272b72014-01-07 12:41:53 -0800689/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800690zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700691
692 data: Py_buffer
693 The binary data to decompress.
Victor Stinnere079edd2013-11-21 22:33:21 +0100694 max_length: uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700695 The maximum allowable length of the decompressed data.
696 Unconsumed input data will be stored in
697 the unconsumed_tail attribute.
698 /
699
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200700Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700701
702After calling this function, some of the input data may still be stored in
703internal buffers for later processing.
704Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800705[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700706
Larry Hastings31826802013-10-19 00:09:25 -0700707static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400708zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
709 unsigned int max_length)
710/*[clinic end generated code: output=b82e2a2c19f5fe7b input=02cfc047377cec86]*/
Larry Hastings31826802013-10-19 00:09:25 -0700711{
Larry Hastings31826802013-10-19 00:09:25 -0700712 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200713 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200714 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000715 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000716
Victor Stinnere079edd2013-11-21 22:33:21 +0100717 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200718 PyErr_SetString(PyExc_OverflowError,
719 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700720 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200721 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000722
Jeremy Hylton9714f992001-10-16 21:19:45 +0000723 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000724 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000725 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200726 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700727 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000728
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800729 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000730
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800731 start_total_out = self->zst.total_out;
732 self->zst.avail_in = (unsigned int)data->len;
733 self->zst.next_in = data->buf;
734 self->zst.avail_out = length;
735 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000736
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000737 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800738 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000739 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000740
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800741 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200742 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800743 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200744 Py_DECREF(RetVal);
745 RetVal = NULL;
746 goto error;
747 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100748
749 if ((size_t)zdict_buf.len > UINT_MAX) {
750 PyErr_SetString(PyExc_OverflowError,
751 "zdict length does not fit in an unsigned int");
752 PyBuffer_Release(&zdict_buf);
753 Py_CLEAR(RetVal);
754 goto error;
755 }
756
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800757 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100758 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200759 PyBuffer_Release(&zdict_buf);
760 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800761 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100762 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200763 goto error;
764 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200765 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200766 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800767 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200768 Py_END_ALLOW_THREADS
769 }
770
Jeremy Hylton9714f992001-10-16 21:19:45 +0000771 /* While Z_OK and the output buffer is full, there might be more output.
772 So extend the output buffer and try again.
773 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800774 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000775 /* If max_length set, don't continue decompressing if we've already
776 reached the limit.
777 */
778 if (max_length && length >= max_length)
779 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000780
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000781 /* otherwise, ... */
782 old_length = length;
783 length = length << 1;
784 if (max_length && length > max_length)
785 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000786
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000787 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200788 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000789 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000790 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800791 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000792 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800793 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000794
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000795 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800796 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000797 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000798 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000799
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800800 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200801 Py_DECREF(RetVal);
802 RetVal = NULL;
803 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000804 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000805
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000806 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100807 /* This is the logical place to call inflateEnd, but the old behaviour
808 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800809 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100810 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000811 /* We will only get Z_BUF_ERROR if the output buffer was full
812 but there wasn't more output when we tried again, so it is
813 not an error condition.
814 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800815 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000816 Py_DECREF(RetVal);
817 RetVal = NULL;
818 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000819 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000820
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800821 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200822 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000823 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000824
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000825 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800826 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000827 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000828}
829
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200830/*[clinic input]
831zlib.Compress.flush
832
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200833 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200834 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
835 If mode == Z_FINISH, the compressor object can no longer be
836 used after calling the flush() method. Otherwise, more data
837 can still be compressed.
838 /
839
840Return a bytes object containing any remaining compressed data.
841[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000842
Guido van Rossumfb221561997-04-29 15:38:09 +0000843static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200844zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200845/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000846{
Victor Stinnere079edd2013-11-21 22:33:21 +0100847 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200848 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000849 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000850 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000851
Jeremy Hylton9714f992001-10-16 21:19:45 +0000852 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
853 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200854 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000855 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000856 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000857
Gregory P. Smith693fc462008-09-06 20:13:06 +0000858 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000860
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000861 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000862
Jeremy Hylton9714f992001-10-16 21:19:45 +0000863 start_total_out = self->zst.total_out;
864 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000865 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000866 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000867
868 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200869 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000870 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000871
Jeremy Hylton9714f992001-10-16 21:19:45 +0000872 /* while Z_OK and the output buffer is full, there might be more output,
873 so extend the output buffer and try again */
874 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100875 if (length <= (UINT_MAX >> 1))
876 new_length = length << 1;
877 else
878 new_length = UINT_MAX;
879 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200880 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000882 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000884 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100886 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000887
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200889 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000891 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000892
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200893 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000894 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200895 mode is Z_FINISH, but checking both for safety*/
896 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 err = deflateEnd(&(self->zst));
898 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200899 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000900 Py_DECREF(RetVal);
901 RetVal = NULL;
902 goto error;
903 }
904 else
905 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000906
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000907 /* We will only get Z_BUF_ERROR if the output buffer was full
908 but there wasn't more output when we tried again, so it is
909 not an error condition.
910 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000911 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000912 zlib_error(self->zst, err, "while flushing");
913 Py_DECREF(RetVal);
914 RetVal = NULL;
915 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000916 }
Tim Peters977e5402001-10-17 03:57:20 +0000917
Gregory P. Smith693fc462008-09-06 20:13:06 +0000918 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200919 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000920 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000921
Tim Peters977e5402001-10-17 03:57:20 +0000922 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000923 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000924
925 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000926}
927
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000928#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700929
Larry Hastings61272b72014-01-07 12:41:53 -0800930/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800931zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700932
933Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800934[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700935
Larry Hastings3cceb382014-01-04 11:09:09 -0800936static PyObject *
937zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800938/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939{
940 compobject *retval = NULL;
941 int err;
942
943 retval = newcompobject(&Comptype);
944 if (!retval) return NULL;
945
946 /* Copy the zstream state
947 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
948 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800949 ENTER_ZLIB(self);
950 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 switch(err) {
952 case(Z_OK):
953 break;
954 case(Z_STREAM_ERROR):
955 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
956 goto error;
957 case(Z_MEM_ERROR):
958 PyErr_SetString(PyExc_MemoryError,
959 "Can't allocate memory for compression object");
960 goto error;
961 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800962 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963 goto error;
964 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800965 Py_INCREF(self->unused_data);
966 Py_INCREF(self->unconsumed_tail);
967 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968 Py_XDECREF(retval->unused_data);
969 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200970 Py_XDECREF(retval->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800971 retval->unused_data = self->unused_data;
972 retval->unconsumed_tail = self->unconsumed_tail;
973 retval->zdict = self->zdict;
974 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000975
976 /* Mark it as being initialized */
977 retval->is_initialised = 1;
978
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800979 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000980 return (PyObject *)retval;
981
982error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800983 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000984 Py_XDECREF(retval);
985 return NULL;
986}
987
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200988/*[clinic input]
989zlib.Decompress.copy
990
991Return a copy of the decompression object.
992[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993
994static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200995zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800996/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000997{
998 compobject *retval = NULL;
999 int err;
1000
1001 retval = newcompobject(&Decomptype);
1002 if (!retval) return NULL;
1003
1004 /* Copy the zstream state
1005 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1006 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001007 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008 err = inflateCopy(&retval->zst, &self->zst);
1009 switch(err) {
1010 case(Z_OK):
1011 break;
1012 case(Z_STREAM_ERROR):
1013 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1014 goto error;
1015 case(Z_MEM_ERROR):
1016 PyErr_SetString(PyExc_MemoryError,
1017 "Can't allocate memory for decompression object");
1018 goto error;
1019 default:
1020 zlib_error(self->zst, err, "while copying decompression object");
1021 goto error;
1022 }
1023
1024 Py_INCREF(self->unused_data);
1025 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001026 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027 Py_XDECREF(retval->unused_data);
1028 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001029 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 retval->unused_data = self->unused_data;
1031 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001032 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001033 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034
1035 /* Mark it as being initialized */
1036 retval->is_initialised = 1;
1037
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001038 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039 return (PyObject *)retval;
1040
1041error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001042 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043 Py_XDECREF(retval);
1044 return NULL;
1045}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001046#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001048/*[clinic input]
1049zlib.Decompress.flush
1050
Serhiy Storchaka54c13912014-02-05 13:34:01 +02001051 length: uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001052 the initial size of the output buffer.
1053 /
1054
1055Return a bytes object containing any remaining decompressed data.
1056[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001057
Guido van Rossumfb221561997-04-29 15:38:09 +00001058static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001059zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Serhiy Storchaka54c13912014-02-05 13:34:01 +02001060/*[clinic end generated code: output=db6fb753ab698e22 input=1580956505978993]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001061{
Victor Stinnere079edd2013-11-21 22:33:21 +01001062 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001063 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001064 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001065 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001066 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001067
Victor Stinnere079edd2013-11-21 22:33:21 +01001068 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001069 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1070 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001071 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001072
Gregory P. Smith693fc462008-09-06 20:13:06 +00001073 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001074 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001075
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001076
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001077 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001078
Victor Stinnere079edd2013-11-21 22:33:21 +01001079 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1080
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001081 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001082 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1083 or equal than UINT_MAX */
1084 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001085 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001086 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001087 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001088
1089 Py_BEGIN_ALLOW_THREADS
1090 err = inflate(&(self->zst), Z_FINISH);
1091 Py_END_ALLOW_THREADS
1092
1093 /* while Z_OK and the output buffer is full, there might be more output,
1094 so extend the output buffer and try again */
1095 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001096 if (length <= (UINT_MAX >> 1))
1097 new_length = length << 1;
1098 else
1099 new_length = UINT_MAX;
1100 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001101 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001102 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001103 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001104 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1105 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001106 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001107
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001108 Py_BEGIN_ALLOW_THREADS
1109 err = inflate(&(self->zst), Z_FINISH);
1110 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001111 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001112
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001113 if (save_unconsumed_input(self, err) < 0) {
1114 Py_DECREF(retval);
1115 retval = NULL;
1116 goto error;
1117 }
1118
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001119 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001120 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001121 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001122 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001123 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001124 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001125 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001126 Py_DECREF(retval);
1127 retval = NULL;
1128 goto error;
1129 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001130 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001131
Gregory P. Smith693fc462008-09-06 20:13:06 +00001132 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001133 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001134 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001135
1136error:
1137
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001138 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001139
Jeremy Hylton9714f992001-10-16 21:19:45 +00001140 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001141}
1142
Christian Heimes936e2f32014-01-27 01:06:57 +01001143#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001144
Guido van Rossumfb221561997-04-29 15:38:09 +00001145static PyMethodDef comp_methods[] =
1146{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001147 ZLIB_COMPRESS_COMPRESS_METHODDEF
1148 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001149#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001150 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001151#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001152 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001153};
1154
1155static PyMethodDef Decomp_methods[] =
1156{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001157 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001158 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001159#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001160 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001161#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001162 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001163};
1164
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001165#define COMP_OFF(x) offsetof(compobject, x)
1166static PyMemberDef Decomp_members[] = {
1167 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1168 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001169 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001170 {NULL},
1171};
Guido van Rossumfb221561997-04-29 15:38:09 +00001172
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001173/*[clinic input]
1174zlib.adler32
1175
1176 data: Py_buffer
1177 value: unsigned_int(bitwise=True) = 1
1178 Starting value of the checksum.
1179 /
1180
1181Compute an Adler-32 checksum of data.
1182
1183The returned checksum is an integer.
1184[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001185
Guido van Rossumfb221561997-04-29 15:38:09 +00001186static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001187zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001188/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001189{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001190 /* Releasing the GIL for very small buffers is inefficient
1191 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001192 if (data->len > 1024*5) {
1193 unsigned char *buf = data->buf;
1194 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001195
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001196 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001197 /* Avoid truncation of length for very large buffers. adler32() takes
1198 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001199 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001200 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001201 buf += (size_t) UINT_MAX;
1202 len -= (size_t) UINT_MAX;
1203 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001204 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001205 Py_END_ALLOW_THREADS
1206 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001207 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001208 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001209 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001210}
Tim Peters977e5402001-10-17 03:57:20 +00001211
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001212/*[clinic input]
1213zlib.crc32
1214
1215 data: Py_buffer
1216 value: unsigned_int(bitwise=True) = 0
1217 Starting value of the checksum.
1218 /
1219
1220Compute a CRC-32 checksum of data.
1221
1222The returned checksum is an integer.
1223[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001224
1225static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001226zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001227/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001228{
Martin v. Löwis423be952008-08-13 15:53:07 +00001229 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001230
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001231 /* Releasing the GIL for very small buffers is inefficient
1232 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001233 if (data->len > 1024*5) {
1234 unsigned char *buf = data->buf;
1235 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001236
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001237 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001238 /* Avoid truncation of length for very large buffers. crc32() takes
1239 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001240 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001241 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001242 buf += (size_t) UINT_MAX;
1243 len -= (size_t) UINT_MAX;
1244 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001245 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001246 Py_END_ALLOW_THREADS
1247 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001248 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001249 }
Christian Heimescc47b052008-03-25 14:56:36 +00001250 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001251}
Tim Peters977e5402001-10-17 03:57:20 +00001252
Guido van Rossumfb221561997-04-29 15:38:09 +00001253
1254static PyMethodDef zlib_methods[] =
1255{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001256 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001257 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001258 ZLIB_COMPRESSOBJ_METHODDEF
1259 ZLIB_CRC32_METHODDEF
1260 ZLIB_DECOMPRESS_METHODDEF
1261 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001262 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001263};
1264
Tim Peters0c322792002-07-17 16:49:03 +00001265static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001266 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001267 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001268 sizeof(compobject),
1269 0,
1270 (destructor)Comp_dealloc, /*tp_dealloc*/
1271 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001272 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001273 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001274 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001275 0, /*tp_repr*/
1276 0, /*tp_as_number*/
1277 0, /*tp_as_sequence*/
1278 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001279 0, /*tp_hash*/
1280 0, /*tp_call*/
1281 0, /*tp_str*/
1282 0, /*tp_getattro*/
1283 0, /*tp_setattro*/
1284 0, /*tp_as_buffer*/
1285 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1286 0, /*tp_doc*/
1287 0, /*tp_traverse*/
1288 0, /*tp_clear*/
1289 0, /*tp_richcompare*/
1290 0, /*tp_weaklistoffset*/
1291 0, /*tp_iter*/
1292 0, /*tp_iternext*/
1293 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001294};
1295
Tim Peters0c322792002-07-17 16:49:03 +00001296static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001297 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001298 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001299 sizeof(compobject),
1300 0,
1301 (destructor)Decomp_dealloc, /*tp_dealloc*/
1302 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001303 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001304 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001305 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001306 0, /*tp_repr*/
1307 0, /*tp_as_number*/
1308 0, /*tp_as_sequence*/
1309 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001310 0, /*tp_hash*/
1311 0, /*tp_call*/
1312 0, /*tp_str*/
1313 0, /*tp_getattro*/
1314 0, /*tp_setattro*/
1315 0, /*tp_as_buffer*/
1316 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1317 0, /*tp_doc*/
1318 0, /*tp_traverse*/
1319 0, /*tp_clear*/
1320 0, /*tp_richcompare*/
1321 0, /*tp_weaklistoffset*/
1322 0, /*tp_iter*/
1323 0, /*tp_iternext*/
1324 Decomp_methods, /*tp_methods*/
1325 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001326};
1327
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001328PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001329"The functions in this module allow compression and decompression using the\n"
1330"zlib library, which is based on GNU zip.\n"
1331"\n"
1332"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001333"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001334"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001335"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001336"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001337"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001338"\n"
1339"'wbits' is window buffer size.\n"
1340"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001341"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001342
Martin v. Löwis1a214512008-06-11 05:26:20 +00001343static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001344 PyModuleDef_HEAD_INIT,
1345 "zlib",
1346 zlib_module_documentation,
1347 -1,
1348 zlib_methods,
1349 NULL,
1350 NULL,
1351 NULL,
1352 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001353};
1354
Mark Hammond62b1ab12002-07-23 06:31:15 +00001355PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001356PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001357{
Fred Drake4baedc12002-04-01 14:53:37 +00001358 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001359 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001360 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001361 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001362 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001363 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001364 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001365 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001366
Fred Drake4baedc12002-04-01 14:53:37 +00001367 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1368 if (ZlibError != NULL) {
1369 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001370 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001371 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001372 PyModule_AddIntMacro(m, MAX_WBITS);
1373 PyModule_AddIntMacro(m, DEFLATED);
1374 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001375 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001376 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1377 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1378 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1379 PyModule_AddIntMacro(m, Z_FILTERED);
1380 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1381 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001382
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001383 PyModule_AddIntMacro(m, Z_FINISH);
1384 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1385 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1386 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001387
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001388 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001389 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001390 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001391
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001392 ver = PyUnicode_FromString(zlibVersion());
1393 if (ver != NULL)
1394 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1395
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001396 PyModule_AddStringConstant(m, "__version__", "1.0");
1397
Martin v. Löwis1a214512008-06-11 05:26:20 +00001398 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001399}