blob: 52744b967ebd607ab742ad079d06c96ad7f42c46 [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
Guido van Rossumfb221561997-04-29 15:38:09 +00006
Guido van Rossum97b54571997-06-03 22:21:47 +00007#include "Python.h"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00008#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +00009#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000012#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000013 #include "pythread.h"
14 #define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000020 #define ENTER_ZLIB(obj)
21 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000022#endif
23
Guido van Rossumfb221561997-04-29 15:38:09 +000024/* The following parameters are copied from zutil.h, version 0.95 */
25#define DEFLATED 8
26#if MAX_MEM_LEVEL >= 8
27# define DEF_MEM_LEVEL 8
28#else
29# define DEF_MEM_LEVEL MAX_MEM_LEVEL
30#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000031
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020032/* Initial buffer size. */
33#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000034
Jeremy Hylton938ace62002-07-17 16:30:39 +000035static PyTypeObject Comptype;
36static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000037
38static PyObject *ZlibError;
39
Tim Peters977e5402001-10-17 03:57:20 +000040typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000041{
Jeremy Hylton9714f992001-10-16 21:19:45 +000042 PyObject_HEAD
43 z_stream zst;
44 PyObject *unused_data;
45 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020046 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000047 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020048 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000049 #ifdef WITH_THREAD
50 PyThread_type_lock lock;
51 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000052} compobject;
53
Jeremy Hylton0965e082001-10-16 21:56:09 +000054static void
55zlib_error(z_stream zst, int err, char *msg)
56{
Nadeem Vawda524148a2011-08-28 11:26:46 +020057 const char *zmsg = Z_NULL;
58 /* In case of a version mismatch, zst.msg won't be initialized.
59 Check for this case first, before looking at zst.msg. */
60 if (err == Z_VERSION_ERROR)
61 zmsg = "library version mismatch";
62 if (zmsg == Z_NULL)
63 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000064 if (zmsg == Z_NULL) {
65 switch (err) {
66 case Z_BUF_ERROR:
67 zmsg = "incomplete or truncated stream";
68 break;
69 case Z_STREAM_ERROR:
70 zmsg = "inconsistent stream state";
71 break;
72 case Z_DATA_ERROR:
73 zmsg = "invalid input data";
74 break;
75 }
76 }
77 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000079 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000080 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000081}
82
Larry Hastings61272b72014-01-07 12:41:53 -080083/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020084output preset file
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]*/
Larry Hastings581ee362014-01-28 05:00:08 -080089/*[clinic end generated code: output=da39a3ee5e6b4b0d input=bfd4c340573ba91d]*/
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 *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200283zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits, unsigned int bufsize)
Larry Hastings581ee362014-01-28 05:00:08 -0800284/*[clinic end generated code: output=9e5464e72df9cb5f input=0f4b9abb7103f50e]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000285{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200286 PyObject *result_str = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000287 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200288 unsigned int length;
289 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200290 unsigned int new_bufsize;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000291 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000292
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200293 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200294 PyErr_SetString(PyExc_OverflowError,
295 "Size does not fit in an unsigned int");
296 goto error;
297 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200298 input = data->buf;
299 length = (unsigned int)data->len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000300
Victor Stinnere079edd2013-11-21 22:33:21 +0100301 if (bufsize == 0)
302 bufsize = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000303
Jeremy Hylton9714f992001-10-16 21:19:45 +0000304 zst.avail_in = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100305 zst.avail_out = bufsize;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000306
Victor Stinnere079edd2013-11-21 22:33:21 +0100307 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200308 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000309
Victor Stinner5064a522013-07-07 16:50:27 +0200310 zst.opaque = NULL;
311 zst.zalloc = PyZlib_Malloc;
312 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000313 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000314 zst.next_in = (Byte *)input;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200315 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000316
Jeremy Hylton9714f992001-10-16 21:19:45 +0000317 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000318 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000319 break;
Tim Peters977e5402001-10-17 03:57:20 +0000320 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000321 PyErr_SetString(PyExc_MemoryError,
322 "Out of memory while decompressing data");
323 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000324 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000325 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000326 zlib_error(zst, err, "while preparing to decompress data");
327 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000328 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000329
Jeremy Hylton9714f992001-10-16 21:19:45 +0000330 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 Py_BEGIN_ALLOW_THREADS
332 err=inflate(&zst, Z_FINISH);
333 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000334
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 switch(err) {
336 case(Z_STREAM_END):
337 break;
338 case(Z_BUF_ERROR):
339 /*
340 * If there is at least 1 byte of room according to zst.avail_out
341 * and we get this error, assume that it means zlib cannot
342 * process the inflate call() due to an error in the data.
343 */
344 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000345 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000346 inflateEnd(&zst);
347 goto error;
348 }
349 /* fall through */
350 case(Z_OK):
351 /* need more memory */
Victor Stinnere079edd2013-11-21 22:33:21 +0100352 if (bufsize <= (UINT_MAX >> 1))
353 new_bufsize = bufsize << 1;
354 else
355 new_bufsize = UINT_MAX;
356 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000357 inflateEnd(&zst);
358 goto error;
359 }
360 zst.next_out =
Victor Stinnere079edd2013-11-21 22:33:21 +0100361 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
362 zst.avail_out = bufsize;
363 bufsize = new_bufsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000364 break;
365 default:
366 inflateEnd(&zst);
367 zlib_error(zst, err, "while decompressing data");
368 goto error;
369 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000370 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000371
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000372 err = inflateEnd(&zst);
373 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200374 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000375 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000376 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000377
Gregory P. Smith693fc462008-09-06 20:13:06 +0000378 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000379 goto error;
380
Jeremy Hylton9714f992001-10-16 21:19:45 +0000381 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000382
383 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000384 Py_XDECREF(result_str);
385 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000386}
387
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200388/*[clinic input]
389zlib.compressobj
390
391 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
392 The compression level (an integer in the range 0-9; default is 6).
393 Higher compression levels are slower, but produce smaller results.
394 method: int(c_default="DEFLATED") = DEFLATED
395 The compression algorithm. If given, this must be DEFLATED.
396 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
397 The base two logarithm of the window size (range: 8..15).
398 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
399 Controls the amount of memory used for internal compression state.
400 Valid values range from 1 to 9. Higher values result in higher memory
401 usage, faster compression, and smaller output.
402 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
403 Used to tune the compression algorithm. Possible values are
404 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
405 zdict: Py_buffer = None
406 The predefined compression dictionary - a sequence of bytes
407 containing subsequences that are likely to occur in the input data.
408
409Return a compressor object.
410[clinic start generated code]*/
411
Guido van Rossumfb221561997-04-29 15:38:09 +0000412static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200413zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits, int memLevel, int strategy, Py_buffer *zdict)
Larry Hastings581ee362014-01-28 05:00:08 -0800414/*[clinic end generated code: output=89e5a6c1449caa9e input=b034847f8821f6af]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000415{
Victor Stinnere079edd2013-11-21 22:33:21 +0100416 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200417 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000418
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200419 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100420 PyErr_SetString(PyExc_OverflowError,
421 "zdict length does not fit in an unsigned int");
422 goto error;
423 }
424
Jeremy Hylton499000002001-10-16 21:59:35 +0000425 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000426 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200427 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200428 self->zst.opaque = NULL;
429 self->zst.zalloc = PyZlib_Malloc;
430 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000431 self->zst.next_in = NULL;
432 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000433 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
434 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000435 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200437 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200438 goto success;
439 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100440 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200441 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200442 switch (err) {
443 case (Z_OK):
444 goto success;
445 case (Z_STREAM_ERROR):
446 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
447 goto error;
448 default:
449 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
450 goto error;
451 }
452 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000453 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000454 PyErr_SetString(PyExc_MemoryError,
455 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200456 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000457 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200459 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000460 default:
461 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200462 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000463 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200464
465 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200466 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200467 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200468 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000469}
470
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200471/*[clinic input]
472zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200473
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200474 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
475 The window buffer size.
476 zdict: object(c_default="NULL") = b''
477 The predefined compression dictionary. This must be the same
478 dictionary as used by the compressor that produced the input data.
479
480Return a decompressor object.
481[clinic start generated code]*/
482
483static PyObject *
484zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict)
Larry Hastings581ee362014-01-28 05:00:08 -0800485/*[clinic end generated code: output=8ccd583fbd631798 input=67f05145a6920127]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200486{
487 int err;
488 compobject *self;
489
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200490 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
491 PyErr_SetString(PyExc_TypeError,
492 "zdict argument must support the buffer protocol");
493 return NULL;
494 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000495
496 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000497 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000498 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200499 self->zst.opaque = NULL;
500 self->zst.zalloc = PyZlib_Malloc;
501 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000502 self->zst.next_in = NULL;
503 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200504 if (zdict != NULL) {
505 Py_INCREF(zdict);
506 self->zdict = zdict;
507 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000508 err = inflateInit2(&self->zst, wbits);
509 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000510 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000511 self->is_initialised = 1;
512 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000513 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000514 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
516 return NULL;
517 case (Z_MEM_ERROR):
518 Py_DECREF(self);
519 PyErr_SetString(PyExc_MemoryError,
520 "Can't allocate memory for decompression object");
521 return NULL;
522 default:
523 zlib_error(self->zst, err, "while creating decompression object");
524 Py_DECREF(self);
525 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000526 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000527}
528
529static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000530Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000531{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000532#ifdef WITH_THREAD
533 PyThread_free_lock(self->lock);
534#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000535 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000536 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200537 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000538 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000539}
540
541static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000542Comp_dealloc(compobject *self)
543{
544 if (self->is_initialised)
545 deflateEnd(&self->zst);
546 Dealloc(self);
547}
548
549static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000550Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000551{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000552 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000553 inflateEnd(&self->zst);
554 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000555}
556
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200557/*[clinic input]
558zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000559
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200560 data: Py_buffer
561 Binary data to be compressed.
562 /
563
564Returns a bytes object containing compressed data.
565
566After calling this function, some of the input data may still
567be stored in internal buffers for later processing.
568Call the flush() method to clear these buffers.
569[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000570
Guido van Rossumfb221561997-04-29 15:38:09 +0000571static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200572zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800573/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000574{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200575 int err;
576 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200577 unsigned int length = DEF_BUF_SIZE, new_length;
578 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000579 Byte *input;
580 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000581
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200582 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200583 PyErr_SetString(PyExc_OverflowError,
584 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200585 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200586 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200587 input = data->buf;
588 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000589
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200590 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200591 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000592
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000593 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000594
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595 start_total_out = self->zst.total_out;
596 self->zst.avail_in = inplen;
597 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000598 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000599 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000600
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000601 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000602 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000603 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000604
Jeremy Hylton9714f992001-10-16 21:19:45 +0000605 /* while Z_OK and the output buffer is full, there might be more output,
606 so extend the output buffer and try again */
607 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100608 if (length <= (UINT_MAX >> 1))
609 new_length = length << 1;
610 else
611 new_length = UINT_MAX;
612 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200613 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200614 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000615 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000616 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000617 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000618 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100619 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000620
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000621 Py_BEGIN_ALLOW_THREADS
622 err = deflate(&(self->zst), Z_NO_FLUSH);
623 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000624 }
Tim Peters977e5402001-10-17 03:57:20 +0000625 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000626 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000627 condition.
628 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000629
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000630 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200631 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200632 Py_CLEAR(RetVal);
633 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000634 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000635 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200636 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000637 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000638
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200639 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000640 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000641 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000642}
643
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100644/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
645 self->unused_data or self->unconsumed_tail, as appropriate. */
646static int
647save_unconsumed_input(compobject *self, int err)
648{
649 if (err == Z_STREAM_END) {
650 /* The end of the compressed data has been reached. Store the leftover
651 input data in self->unused_data. */
652 if (self->zst.avail_in > 0) {
653 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
654 Py_ssize_t new_size;
655 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100656 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100657 PyErr_NoMemory();
658 return -1;
659 }
660 new_size = old_size + self->zst.avail_in;
661 new_data = PyBytes_FromStringAndSize(NULL, new_size);
662 if (new_data == NULL)
663 return -1;
664 Py_MEMCPY(PyBytes_AS_STRING(new_data),
665 PyBytes_AS_STRING(self->unused_data), old_size);
666 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
667 self->zst.next_in, self->zst.avail_in);
668 Py_DECREF(self->unused_data);
669 self->unused_data = new_data;
670 self->zst.avail_in = 0;
671 }
672 }
673 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
674 /* This code handles two distinct cases:
675 1. Output limit was reached. Save leftover input in unconsumed_tail.
676 2. All input data was consumed. Clear unconsumed_tail. */
677 PyObject *new_data = PyBytes_FromStringAndSize(
678 (char *)self->zst.next_in, self->zst.avail_in);
679 if (new_data == NULL)
680 return -1;
681 Py_DECREF(self->unconsumed_tail);
682 self->unconsumed_tail = new_data;
683 }
684 return 0;
685}
686
Larry Hastings61272b72014-01-07 12:41:53 -0800687/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800688zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700689
690 data: Py_buffer
691 The binary data to decompress.
Victor Stinnere079edd2013-11-21 22:33:21 +0100692 max_length: uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700693 The maximum allowable length of the decompressed data.
694 Unconsumed input data will be stored in
695 the unconsumed_tail attribute.
696 /
697
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200698Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700699
700After calling this function, some of the input data may still be stored in
701internal buffers for later processing.
702Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800703[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700704
Larry Hastings31826802013-10-19 00:09:25 -0700705static PyObject *
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800706zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length)
Larry Hastings581ee362014-01-28 05:00:08 -0800707/*[clinic end generated code: output=755cccc9087bfe55 input=02cfc047377cec86]*/
Larry Hastings31826802013-10-19 00:09:25 -0700708{
Larry Hastings31826802013-10-19 00:09:25 -0700709 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200710 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200711 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000712 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000713
Victor Stinnere079edd2013-11-21 22:33:21 +0100714 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200715 PyErr_SetString(PyExc_OverflowError,
716 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700717 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200718 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000719
Jeremy Hylton9714f992001-10-16 21:19:45 +0000720 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000721 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000722 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200723 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700724 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000725
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800726 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000727
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800728 start_total_out = self->zst.total_out;
729 self->zst.avail_in = (unsigned int)data->len;
730 self->zst.next_in = data->buf;
731 self->zst.avail_out = length;
732 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000733
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000734 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800735 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000736 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000737
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800738 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200739 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800740 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200741 Py_DECREF(RetVal);
742 RetVal = NULL;
743 goto error;
744 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100745
746 if ((size_t)zdict_buf.len > UINT_MAX) {
747 PyErr_SetString(PyExc_OverflowError,
748 "zdict length does not fit in an unsigned int");
749 PyBuffer_Release(&zdict_buf);
750 Py_CLEAR(RetVal);
751 goto error;
752 }
753
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800754 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100755 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200756 PyBuffer_Release(&zdict_buf);
757 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800758 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100759 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200760 goto error;
761 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200762 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200763 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800764 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200765 Py_END_ALLOW_THREADS
766 }
767
Jeremy Hylton9714f992001-10-16 21:19:45 +0000768 /* While Z_OK and the output buffer is full, there might be more output.
769 So extend the output buffer and try again.
770 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800771 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000772 /* If max_length set, don't continue decompressing if we've already
773 reached the limit.
774 */
775 if (max_length && length >= max_length)
776 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000777
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000778 /* otherwise, ... */
779 old_length = length;
780 length = length << 1;
781 if (max_length && length > max_length)
782 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000783
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000784 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200785 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000786 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000787 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800788 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000789 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800790 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000791
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000792 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800793 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000794 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000795 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000796
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800797 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200798 Py_DECREF(RetVal);
799 RetVal = NULL;
800 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000801 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000802
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000803 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100804 /* This is the logical place to call inflateEnd, but the old behaviour
805 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800806 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100807 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000808 /* We will only get Z_BUF_ERROR if the output buffer was full
809 but there wasn't more output when we tried again, so it is
810 not an error condition.
811 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800812 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 Py_DECREF(RetVal);
814 RetVal = NULL;
815 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000816 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000817
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800818 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200819 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000820 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000821
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000822 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800823 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000824 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000825}
826
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200827/*[clinic input]
828zlib.Compress.flush
829
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200830 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200831 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
832 If mode == Z_FINISH, the compressor object can no longer be
833 used after calling the flush() method. Otherwise, more data
834 can still be compressed.
835 /
836
837Return a bytes object containing any remaining compressed data.
838[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000839
Guido van Rossumfb221561997-04-29 15:38:09 +0000840static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200841zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200842/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000843{
Victor Stinnere079edd2013-11-21 22:33:21 +0100844 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200845 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000846 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000847 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000848
Jeremy Hylton9714f992001-10-16 21:19:45 +0000849 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
850 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200851 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000852 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000853 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854
Gregory P. Smith693fc462008-09-06 20:13:06 +0000855 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000856 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000857
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000858 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000859
Jeremy Hylton9714f992001-10-16 21:19:45 +0000860 start_total_out = self->zst.total_out;
861 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000862 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000863 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000864
865 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200866 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000867 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000868
Jeremy Hylton9714f992001-10-16 21:19:45 +0000869 /* while Z_OK and the output buffer is full, there might be more output,
870 so extend the output buffer and try again */
871 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100872 if (length <= (UINT_MAX >> 1))
873 new_length = length << 1;
874 else
875 new_length = UINT_MAX;
876 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200877 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000879 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000880 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000881 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000882 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100883 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200886 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000887 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000888 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000889
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200890 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000891 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200892 mode is Z_FINISH, but checking both for safety*/
893 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000894 err = deflateEnd(&(self->zst));
895 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200896 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 Py_DECREF(RetVal);
898 RetVal = NULL;
899 goto error;
900 }
901 else
902 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000903
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000904 /* We will only get Z_BUF_ERROR if the output buffer was full
905 but there wasn't more output when we tried again, so it is
906 not an error condition.
907 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000908 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000909 zlib_error(self->zst, err, "while flushing");
910 Py_DECREF(RetVal);
911 RetVal = NULL;
912 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000913 }
Tim Peters977e5402001-10-17 03:57:20 +0000914
Gregory P. Smith693fc462008-09-06 20:13:06 +0000915 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200916 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000917 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000918
Tim Peters977e5402001-10-17 03:57:20 +0000919 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000920 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000921
922 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000923}
924
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000925#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700926
Larry Hastings61272b72014-01-07 12:41:53 -0800927/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800928zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700929
930Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800931[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700932
Larry Hastings3cceb382014-01-04 11:09:09 -0800933static PyObject *
934zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800935/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936{
937 compobject *retval = NULL;
938 int err;
939
940 retval = newcompobject(&Comptype);
941 if (!retval) return NULL;
942
943 /* Copy the zstream state
944 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
945 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800946 ENTER_ZLIB(self);
947 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948 switch(err) {
949 case(Z_OK):
950 break;
951 case(Z_STREAM_ERROR):
952 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
953 goto error;
954 case(Z_MEM_ERROR):
955 PyErr_SetString(PyExc_MemoryError,
956 "Can't allocate memory for compression object");
957 goto error;
958 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800959 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960 goto error;
961 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800962 Py_INCREF(self->unused_data);
963 Py_INCREF(self->unconsumed_tail);
964 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000965 Py_XDECREF(retval->unused_data);
966 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200967 Py_XDECREF(retval->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800968 retval->unused_data = self->unused_data;
969 retval->unconsumed_tail = self->unconsumed_tail;
970 retval->zdict = self->zdict;
971 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972
973 /* Mark it as being initialized */
974 retval->is_initialised = 1;
975
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800976 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000977 return (PyObject *)retval;
978
979error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800980 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000981 Py_XDECREF(retval);
982 return NULL;
983}
984
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200985/*[clinic input]
986zlib.Decompress.copy
987
988Return a copy of the decompression object.
989[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000990
991static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200992zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800993/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000994{
995 compobject *retval = NULL;
996 int err;
997
998 retval = newcompobject(&Decomptype);
999 if (!retval) return NULL;
1000
1001 /* Copy the zstream state
1002 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1003 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001004 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005 err = inflateCopy(&retval->zst, &self->zst);
1006 switch(err) {
1007 case(Z_OK):
1008 break;
1009 case(Z_STREAM_ERROR):
1010 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1011 goto error;
1012 case(Z_MEM_ERROR):
1013 PyErr_SetString(PyExc_MemoryError,
1014 "Can't allocate memory for decompression object");
1015 goto error;
1016 default:
1017 zlib_error(self->zst, err, "while copying decompression object");
1018 goto error;
1019 }
1020
1021 Py_INCREF(self->unused_data);
1022 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001023 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024 Py_XDECREF(retval->unused_data);
1025 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001026 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027 retval->unused_data = self->unused_data;
1028 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001029 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001030 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001031
1032 /* Mark it as being initialized */
1033 retval->is_initialised = 1;
1034
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001035 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001036 return (PyObject *)retval;
1037
1038error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001039 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040 Py_XDECREF(retval);
1041 return NULL;
1042}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001043#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001045/*[clinic input]
1046zlib.Decompress.flush
1047
Serhiy Storchaka54c13912014-02-05 13:34:01 +02001048 length: uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001049 the initial size of the output buffer.
1050 /
1051
1052Return a bytes object containing any remaining decompressed data.
1053[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001054
Guido van Rossumfb221561997-04-29 15:38:09 +00001055static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001056zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Serhiy Storchaka54c13912014-02-05 13:34:01 +02001057/*[clinic end generated code: output=db6fb753ab698e22 input=1580956505978993]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001058{
Victor Stinnere079edd2013-11-21 22:33:21 +01001059 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001060 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001061 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001062 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001063 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001064
Victor Stinnere079edd2013-11-21 22:33:21 +01001065 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001066 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1067 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001068 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001069
Gregory P. Smith693fc462008-09-06 20:13:06 +00001070 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001071 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001072
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001073
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001074 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001075
Victor Stinnere079edd2013-11-21 22:33:21 +01001076 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1077
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001078 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001079 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1080 or equal than UINT_MAX */
1081 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001082 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001083 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001084 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001085
1086 Py_BEGIN_ALLOW_THREADS
1087 err = inflate(&(self->zst), Z_FINISH);
1088 Py_END_ALLOW_THREADS
1089
1090 /* while Z_OK and the output buffer is full, there might be more output,
1091 so extend the output buffer and try again */
1092 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001093 if (length <= (UINT_MAX >> 1))
1094 new_length = length << 1;
1095 else
1096 new_length = UINT_MAX;
1097 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001098 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001099 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001100 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001101 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1102 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001103 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001104
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001105 Py_BEGIN_ALLOW_THREADS
1106 err = inflate(&(self->zst), Z_FINISH);
1107 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001108 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001109
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001110 if (save_unconsumed_input(self, err) < 0) {
1111 Py_DECREF(retval);
1112 retval = NULL;
1113 goto error;
1114 }
1115
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001116 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001117 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001118 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001119 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001120 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001121 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001122 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001123 Py_DECREF(retval);
1124 retval = NULL;
1125 goto error;
1126 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001127 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001128
Gregory P. Smith693fc462008-09-06 20:13:06 +00001129 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001130 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001131 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001132
1133error:
1134
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001135 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001136
Jeremy Hylton9714f992001-10-16 21:19:45 +00001137 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001138}
1139
Christian Heimes936e2f32014-01-27 01:06:57 +01001140#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001141
Guido van Rossumfb221561997-04-29 15:38:09 +00001142static PyMethodDef comp_methods[] =
1143{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001144 ZLIB_COMPRESS_COMPRESS_METHODDEF
1145 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001146#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001147 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001148#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001149 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001150};
1151
1152static PyMethodDef Decomp_methods[] =
1153{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001154 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001155 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001156#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001157 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001158#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001159 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001160};
1161
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001162#define COMP_OFF(x) offsetof(compobject, x)
1163static PyMemberDef Decomp_members[] = {
1164 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1165 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001166 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001167 {NULL},
1168};
Guido van Rossumfb221561997-04-29 15:38:09 +00001169
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001170/*[clinic input]
1171zlib.adler32
1172
1173 data: Py_buffer
1174 value: unsigned_int(bitwise=True) = 1
1175 Starting value of the checksum.
1176 /
1177
1178Compute an Adler-32 checksum of data.
1179
1180The returned checksum is an integer.
1181[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001182
Guido van Rossumfb221561997-04-29 15:38:09 +00001183static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001184zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001185/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001186{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001187 /* Releasing the GIL for very small buffers is inefficient
1188 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001189 if (data->len > 1024*5) {
1190 unsigned char *buf = data->buf;
1191 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001192
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001193 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001194 /* Avoid truncation of length for very large buffers. adler32() takes
1195 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001196 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001197 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001198 buf += (size_t) UINT_MAX;
1199 len -= (size_t) UINT_MAX;
1200 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001201 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001202 Py_END_ALLOW_THREADS
1203 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001204 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001205 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001206 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001207}
Tim Peters977e5402001-10-17 03:57:20 +00001208
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001209/*[clinic input]
1210zlib.crc32
1211
1212 data: Py_buffer
1213 value: unsigned_int(bitwise=True) = 0
1214 Starting value of the checksum.
1215 /
1216
1217Compute a CRC-32 checksum of data.
1218
1219The returned checksum is an integer.
1220[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001221
1222static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001223zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001224/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001225{
Martin v. Löwis423be952008-08-13 15:53:07 +00001226 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001227
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001228 /* Releasing the GIL for very small buffers is inefficient
1229 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001230 if (data->len > 1024*5) {
1231 unsigned char *buf = data->buf;
1232 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001233
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001234 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001235 /* Avoid truncation of length for very large buffers. crc32() takes
1236 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001237 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001238 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001239 buf += (size_t) UINT_MAX;
1240 len -= (size_t) UINT_MAX;
1241 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001242 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001243 Py_END_ALLOW_THREADS
1244 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001245 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001246 }
Christian Heimescc47b052008-03-25 14:56:36 +00001247 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001248}
Tim Peters977e5402001-10-17 03:57:20 +00001249
Guido van Rossumfb221561997-04-29 15:38:09 +00001250
1251static PyMethodDef zlib_methods[] =
1252{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001253 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001254 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001255 ZLIB_COMPRESSOBJ_METHODDEF
1256 ZLIB_CRC32_METHODDEF
1257 ZLIB_DECOMPRESS_METHODDEF
1258 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001259 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001260};
1261
Tim Peters0c322792002-07-17 16:49:03 +00001262static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001263 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001264 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001265 sizeof(compobject),
1266 0,
1267 (destructor)Comp_dealloc, /*tp_dealloc*/
1268 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001269 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001270 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001271 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001272 0, /*tp_repr*/
1273 0, /*tp_as_number*/
1274 0, /*tp_as_sequence*/
1275 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001276 0, /*tp_hash*/
1277 0, /*tp_call*/
1278 0, /*tp_str*/
1279 0, /*tp_getattro*/
1280 0, /*tp_setattro*/
1281 0, /*tp_as_buffer*/
1282 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1283 0, /*tp_doc*/
1284 0, /*tp_traverse*/
1285 0, /*tp_clear*/
1286 0, /*tp_richcompare*/
1287 0, /*tp_weaklistoffset*/
1288 0, /*tp_iter*/
1289 0, /*tp_iternext*/
1290 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001291};
1292
Tim Peters0c322792002-07-17 16:49:03 +00001293static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001294 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001295 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001296 sizeof(compobject),
1297 0,
1298 (destructor)Decomp_dealloc, /*tp_dealloc*/
1299 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001300 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001301 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001302 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001303 0, /*tp_repr*/
1304 0, /*tp_as_number*/
1305 0, /*tp_as_sequence*/
1306 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001307 0, /*tp_hash*/
1308 0, /*tp_call*/
1309 0, /*tp_str*/
1310 0, /*tp_getattro*/
1311 0, /*tp_setattro*/
1312 0, /*tp_as_buffer*/
1313 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1314 0, /*tp_doc*/
1315 0, /*tp_traverse*/
1316 0, /*tp_clear*/
1317 0, /*tp_richcompare*/
1318 0, /*tp_weaklistoffset*/
1319 0, /*tp_iter*/
1320 0, /*tp_iternext*/
1321 Decomp_methods, /*tp_methods*/
1322 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001323};
1324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001325PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001326"The functions in this module allow compression and decompression using the\n"
1327"zlib library, which is based on GNU zip.\n"
1328"\n"
1329"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001330"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001331"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001332"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001333"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001334"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001335"\n"
1336"'wbits' is window buffer size.\n"
1337"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001338"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001339
Martin v. Löwis1a214512008-06-11 05:26:20 +00001340static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001341 PyModuleDef_HEAD_INIT,
1342 "zlib",
1343 zlib_module_documentation,
1344 -1,
1345 zlib_methods,
1346 NULL,
1347 NULL,
1348 NULL,
1349 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001350};
1351
Mark Hammond62b1ab12002-07-23 06:31:15 +00001352PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001353PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001354{
Fred Drake4baedc12002-04-01 14:53:37 +00001355 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001356 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001357 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001358 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001359 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001360 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001361 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001362 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001363
Fred Drake4baedc12002-04-01 14:53:37 +00001364 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1365 if (ZlibError != NULL) {
1366 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001367 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001368 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001369 PyModule_AddIntMacro(m, MAX_WBITS);
1370 PyModule_AddIntMacro(m, DEFLATED);
1371 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001372 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001373 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1374 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1375 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1376 PyModule_AddIntMacro(m, Z_FILTERED);
1377 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1378 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001379
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001380 PyModule_AddIntMacro(m, Z_FINISH);
1381 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1382 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1383 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001384
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001385 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001386 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001387 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001388
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001389 ver = PyUnicode_FromString(zlibVersion());
1390 if (ver != NULL)
1391 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1392
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001393 PyModule_AddStringConstant(m, "__version__", "1.0");
1394
Martin v. Löwis1a214512008-06-11 05:26:20 +00001395 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001396}