blob: 5cdab452b8ff714382f7a17cfd04dc8fbfdcb2ae [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Victor Stinnerf18f8712014-07-01 16:48:12 +02006#define PY_SSIZE_T_CLEAN
Guido van Rossumfb221561997-04-29 15:38:09 +00007
Guido van Rossum97b54571997-06-03 22:21:47 +00008#include "Python.h"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00009#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +000010#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000011
Larry Hastings31826802013-10-19 00:09:25 -070012
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000013#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000014 #include "pythread.h"
15 #define ENTER_ZLIB(obj) \
16 Py_BEGIN_ALLOW_THREADS; \
17 PyThread_acquire_lock((obj)->lock, 1); \
18 Py_END_ALLOW_THREADS;
19 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000020#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000021 #define ENTER_ZLIB(obj)
22 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000023#endif
24
Guido van Rossumfb221561997-04-29 15:38:09 +000025/* The following parameters are copied from zutil.h, version 0.95 */
26#define DEFLATED 8
27#if MAX_MEM_LEVEL >= 8
28# define DEF_MEM_LEVEL 8
29#else
30# define DEF_MEM_LEVEL MAX_MEM_LEVEL
31#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000032
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020033/* Initial buffer size. */
34#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Jeremy Hylton938ace62002-07-17 16:30:39 +000036static PyTypeObject Comptype;
37static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000038
39static PyObject *ZlibError;
40
Tim Peters977e5402001-10-17 03:57:20 +000041typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000042{
Jeremy Hylton9714f992001-10-16 21:19:45 +000043 PyObject_HEAD
44 z_stream zst;
45 PyObject *unused_data;
46 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020047 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000048 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020049 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000050 #ifdef WITH_THREAD
51 PyThread_type_lock lock;
52 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000053} compobject;
54
Jeremy Hylton0965e082001-10-16 21:56:09 +000055static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020056zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000057{
Nadeem Vawda524148a2011-08-28 11:26:46 +020058 const char *zmsg = Z_NULL;
59 /* In case of a version mismatch, zst.msg won't be initialized.
60 Check for this case first, before looking at zst.msg. */
61 if (err == Z_VERSION_ERROR)
62 zmsg = "library version mismatch";
63 if (zmsg == Z_NULL)
64 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000065 if (zmsg == Z_NULL) {
66 switch (err) {
67 case Z_BUF_ERROR:
68 zmsg = "incomplete or truncated stream";
69 break;
70 case Z_STREAM_ERROR:
71 zmsg = "inconsistent stream state";
72 break;
73 case Z_DATA_ERROR:
74 zmsg = "invalid input data";
75 break;
76 }
77 }
78 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000080 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000081 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000082}
83
Larry Hastings61272b72014-01-07 12:41:53 -080084/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080085module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080086class zlib.Compress "compobject *" "&Comptype"
87class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080088[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030089/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080090
Guido van Rossumfb221561997-04-29 15:38:09 +000091static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000092newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000093{
Tim Peters977e5402001-10-17 03:57:20 +000094 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000095 self = PyObject_New(compobject, type);
96 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020098 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200100 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000101 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000103 Py_DECREF(self);
104 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000106 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000107 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000108 Py_DECREF(self);
109 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000110 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000111#ifdef WITH_THREAD
112 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200113 if (self->lock == NULL) {
114 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
115 return NULL;
116 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000117#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000118 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000119}
120
Victor Stinner5064a522013-07-07 16:50:27 +0200121static void*
122PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
123{
124 if (items > (size_t)PY_SSIZE_T_MAX / size)
125 return NULL;
126 /* PyMem_Malloc() cannot be used: the GIL is not held when
127 inflate() and deflate() are called */
128 return PyMem_RawMalloc(items * size);
129}
130
131static void
132PyZlib_Free(voidpf ctx, void *ptr)
133{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200134 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200135}
136
Larry Hastings61272b72014-01-07 12:41:53 -0800137/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800138zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200139
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
Martin Pantere99e9772015-11-20 08:13:35 +0000229class capped_uint_converter(CConverter):
Victor Stinnere079edd2013-11-21 22:33:21 +0100230 type = 'unsigned int'
Martin Pantere99e9772015-11-20 08:13:35 +0000231 converter = 'capped_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]*/
Martin Pantere99e9772015-11-20 08:13:35 +0000235/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100236
237static int
Martin Pantere99e9772015-11-20 08:13:35 +0000238capped_uint_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100239{
Martin Pantere99e9772015-11-20 08:13:35 +0000240 PyObject *long_obj;
241 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100242
Martin Pantere99e9772015-11-20 08:13:35 +0000243 long_obj = (PyObject *)_PyLong_FromNbInt(obj);
244 if (long_obj == NULL) {
245 return 0;
246 }
247 val = PyLong_AsSsize_t(long_obj);
248 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100249 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000250 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100251 }
Martin Pantere99e9772015-11-20 08:13:35 +0000252 if (val < 0) {
253 PyErr_SetString(PyExc_ValueError,
254 "value must be positive");
Victor Stinner5c867332014-01-03 12:26:12 +0100255 return 0;
256 }
257
Martin Pantere99e9772015-11-20 08:13:35 +0000258 if ((size_t)val > UINT_MAX) {
259 *(unsigned int *)ptr = UINT_MAX;
260 }
261 else {
262 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(val, Py_ssize_t,
263 unsigned int);
264 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100265 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.
Martin Pantere99e9772015-11-20 08:13:35 +0000275 bufsize: capped_uint(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200276 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)
Martin Pantere99e9772015-11-20 08:13:35 +0000285/*[clinic end generated code: output=444d0987f3429574 input=da095118b3243b27]*/
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
Martin Panter567d5132016-02-03 07:06:33 +0000393 The compression level (an integer in the range 0-9 or -1; default is
394 currently equivalent to 6). Higher compression levels are slower,
395 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200396 method: int(c_default="DEFLATED") = DEFLATED
397 The compression algorithm. If given, this must be DEFLATED.
398 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
399 The base two logarithm of the window size (range: 8..15).
400 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
401 Controls the amount of memory used for internal compression state.
402 Valid values range from 1 to 9. Higher values result in higher memory
403 usage, faster compression, and smaller output.
404 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
405 Used to tune the compression algorithm. Possible values are
406 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
407 zdict: Py_buffer = None
408 The predefined compression dictionary - a sequence of bytes
409 containing subsequences that are likely to occur in the input data.
410
411Return a compressor object.
412[clinic start generated code]*/
413
Guido van Rossumfb221561997-04-29 15:38:09 +0000414static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400415zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
416 int memLevel, int strategy, Py_buffer *zdict)
Martin Panter567d5132016-02-03 07:06:33 +0000417/*[clinic end generated code: output=2949bbb9a5723ccd input=de2ffab6e910cd8b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000418{
Victor Stinnere079edd2013-11-21 22:33:21 +0100419 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200420 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000421
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200422 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100423 PyErr_SetString(PyExc_OverflowError,
424 "zdict length does not fit in an unsigned int");
425 goto error;
426 }
427
Jeremy Hylton499000002001-10-16 21:59:35 +0000428 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000429 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200430 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200431 self->zst.opaque = NULL;
432 self->zst.zalloc = PyZlib_Malloc;
433 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000434 self->zst.next_in = NULL;
435 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000436 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
437 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000438 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000439 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200440 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200441 goto success;
442 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100443 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200444 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200445 switch (err) {
446 case (Z_OK):
447 goto success;
448 case (Z_STREAM_ERROR):
449 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
450 goto error;
451 default:
452 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
453 goto error;
454 }
455 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000456 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000457 PyErr_SetString(PyExc_MemoryError,
458 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200459 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000460 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000461 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200462 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 default:
464 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200465 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000466 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200467
468 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200469 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200470 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200471 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000472}
473
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200474/*[clinic input]
475zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200476
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200477 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
478 The window buffer size.
479 zdict: object(c_default="NULL") = b''
480 The predefined compression dictionary. This must be the same
481 dictionary as used by the compressor that produced the input data.
482
483Return a decompressor object.
484[clinic start generated code]*/
485
486static PyObject *
487zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict)
Larry Hastings581ee362014-01-28 05:00:08 -0800488/*[clinic end generated code: output=8ccd583fbd631798 input=67f05145a6920127]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200489{
490 int err;
491 compobject *self;
492
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200493 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
494 PyErr_SetString(PyExc_TypeError,
495 "zdict argument must support the buffer protocol");
496 return NULL;
497 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000498
499 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000500 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000501 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200502 self->zst.opaque = NULL;
503 self->zst.zalloc = PyZlib_Malloc;
504 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000505 self->zst.next_in = NULL;
506 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200507 if (zdict != NULL) {
508 Py_INCREF(zdict);
509 self->zdict = zdict;
510 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000511 err = inflateInit2(&self->zst, wbits);
512 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000513 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000514 self->is_initialised = 1;
515 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000516 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000517 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000518 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
519 return NULL;
520 case (Z_MEM_ERROR):
521 Py_DECREF(self);
522 PyErr_SetString(PyExc_MemoryError,
523 "Can't allocate memory for decompression object");
524 return NULL;
525 default:
526 zlib_error(self->zst, err, "while creating decompression object");
527 Py_DECREF(self);
528 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000529 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000530}
531
532static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000533Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000534{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000535#ifdef WITH_THREAD
536 PyThread_free_lock(self->lock);
537#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000538 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000539 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200540 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000541 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000542}
543
544static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000545Comp_dealloc(compobject *self)
546{
547 if (self->is_initialised)
548 deflateEnd(&self->zst);
549 Dealloc(self);
550}
551
552static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000553Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000554{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000555 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000556 inflateEnd(&self->zst);
557 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000558}
559
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200560/*[clinic input]
561zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000562
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200563 data: Py_buffer
564 Binary data to be compressed.
565 /
566
567Returns a bytes object containing compressed data.
568
569After calling this function, some of the input data may still
570be stored in internal buffers for later processing.
571Call the flush() method to clear these buffers.
572[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000573
Guido van Rossumfb221561997-04-29 15:38:09 +0000574static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200575zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800576/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000577{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200578 int err;
579 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200580 unsigned int length = DEF_BUF_SIZE, new_length;
581 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000582 Byte *input;
583 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000584
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200585 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200586 PyErr_SetString(PyExc_OverflowError,
587 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200588 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200589 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200590 input = data->buf;
591 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000592
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200593 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200594 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000596 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000597
Jeremy Hylton9714f992001-10-16 21:19:45 +0000598 start_total_out = self->zst.total_out;
599 self->zst.avail_in = inplen;
600 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000601 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000602 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000603
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000604 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000605 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000606 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000607
Jeremy Hylton9714f992001-10-16 21:19:45 +0000608 /* while Z_OK and the output buffer is full, there might be more output,
609 so extend the output buffer and try again */
610 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100611 if (length <= (UINT_MAX >> 1))
612 new_length = length << 1;
613 else
614 new_length = UINT_MAX;
615 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200616 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200617 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000618 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000619 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000620 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000621 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100622 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000623
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000624 Py_BEGIN_ALLOW_THREADS
625 err = deflate(&(self->zst), Z_NO_FLUSH);
626 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000627 }
Tim Peters977e5402001-10-17 03:57:20 +0000628 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000629 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000630 condition.
631 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000632
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000633 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200634 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200635 Py_CLEAR(RetVal);
636 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000637 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000638 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200639 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000640 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000641
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200642 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000643 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000644 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000645}
646
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100647/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
648 self->unused_data or self->unconsumed_tail, as appropriate. */
649static int
650save_unconsumed_input(compobject *self, int err)
651{
652 if (err == Z_STREAM_END) {
653 /* The end of the compressed data has been reached. Store the leftover
654 input data in self->unused_data. */
655 if (self->zst.avail_in > 0) {
656 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
657 Py_ssize_t new_size;
658 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100659 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100660 PyErr_NoMemory();
661 return -1;
662 }
663 new_size = old_size + self->zst.avail_in;
664 new_data = PyBytes_FromStringAndSize(NULL, new_size);
665 if (new_data == NULL)
666 return -1;
667 Py_MEMCPY(PyBytes_AS_STRING(new_data),
668 PyBytes_AS_STRING(self->unused_data), old_size);
669 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
670 self->zst.next_in, self->zst.avail_in);
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200671 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100672 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;
Serhiy Storchaka5a57ade2015-12-24 10:35:59 +0200683 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100684 }
685 return 0;
686}
687
Larry Hastings61272b72014-01-07 12:41:53 -0800688/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800689zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700690
691 data: Py_buffer
692 The binary data to decompress.
Martin Pantere99e9772015-11-20 08:13:35 +0000693 max_length: capped_uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700694 The maximum allowable length of the decompressed data.
695 Unconsumed input data will be stored in
696 the unconsumed_tail attribute.
697 /
698
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200699Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700700
701After calling this function, some of the input data may still be stored in
702internal buffers for later processing.
703Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800704[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700705
Larry Hastings31826802013-10-19 00:09:25 -0700706static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400707zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
708 unsigned int max_length)
Martin Pantere99e9772015-11-20 08:13:35 +0000709/*[clinic end generated code: output=b82e2a2c19f5fe7b input=68b6508ab07c2cf0]*/
Larry Hastings31826802013-10-19 00:09:25 -0700710{
Larry Hastings31826802013-10-19 00:09:25 -0700711 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200712 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200713 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000714 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000715
Victor Stinnere079edd2013-11-21 22:33:21 +0100716 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200717 PyErr_SetString(PyExc_OverflowError,
718 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700719 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200720 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000721
Jeremy Hylton9714f992001-10-16 21:19:45 +0000722 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000723 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000724 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200725 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700726 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000727
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800728 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000729
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800730 start_total_out = self->zst.total_out;
731 self->zst.avail_in = (unsigned int)data->len;
732 self->zst.next_in = data->buf;
733 self->zst.avail_out = length;
734 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000735
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000736 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800737 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000738 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000739
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800740 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200741 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800742 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200743 Py_DECREF(RetVal);
744 RetVal = NULL;
745 goto error;
746 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100747
748 if ((size_t)zdict_buf.len > UINT_MAX) {
749 PyErr_SetString(PyExc_OverflowError,
750 "zdict length does not fit in an unsigned int");
751 PyBuffer_Release(&zdict_buf);
752 Py_CLEAR(RetVal);
753 goto error;
754 }
755
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800756 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100757 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200758 PyBuffer_Release(&zdict_buf);
759 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800760 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100761 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200762 goto error;
763 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200764 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200765 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800766 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200767 Py_END_ALLOW_THREADS
768 }
769
Jeremy Hylton9714f992001-10-16 21:19:45 +0000770 /* While Z_OK and the output buffer is full, there might be more output.
771 So extend the output buffer and try again.
772 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800773 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000774 /* If max_length set, don't continue decompressing if we've already
775 reached the limit.
776 */
777 if (max_length && length >= max_length)
778 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000779
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000780 /* otherwise, ... */
781 old_length = length;
782 length = length << 1;
783 if (max_length && length > max_length)
784 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000785
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000786 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200787 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000788 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000789 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800790 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000791 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800792 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000793
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000794 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800795 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000796 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000797 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000798
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800799 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200800 Py_DECREF(RetVal);
801 RetVal = NULL;
802 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000803 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000804
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000805 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100806 /* This is the logical place to call inflateEnd, but the old behaviour
807 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800808 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100809 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000810 /* We will only get Z_BUF_ERROR if the output buffer was full
811 but there wasn't more output when we tried again, so it is
812 not an error condition.
813 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800814 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 Py_DECREF(RetVal);
816 RetVal = NULL;
817 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000818 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000819
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800820 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200821 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000822 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000823
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000824 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800825 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000826 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000827}
828
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200829/*[clinic input]
830zlib.Compress.flush
831
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200832 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200833 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
834 If mode == Z_FINISH, the compressor object can no longer be
835 used after calling the flush() method. Otherwise, more data
836 can still be compressed.
837 /
838
839Return a bytes object containing any remaining compressed data.
840[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000841
Guido van Rossumfb221561997-04-29 15:38:09 +0000842static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200843zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200844/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000845{
Victor Stinnere079edd2013-11-21 22:33:21 +0100846 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200847 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000848 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000849 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000850
Jeremy Hylton9714f992001-10-16 21:19:45 +0000851 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
852 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200853 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000854 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000855 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000856
Gregory P. Smith693fc462008-09-06 20:13:06 +0000857 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000858 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000859
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000860 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000861
Jeremy Hylton9714f992001-10-16 21:19:45 +0000862 start_total_out = self->zst.total_out;
863 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000864 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000865 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000866
867 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200868 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000869 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000870
Jeremy Hylton9714f992001-10-16 21:19:45 +0000871 /* while Z_OK and the output buffer is full, there might be more output,
872 so extend the output buffer and try again */
873 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100874 if (length <= (UINT_MAX >> 1))
875 new_length = length << 1;
876 else
877 new_length = UINT_MAX;
878 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200879 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000880 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000881 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000882 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000883 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100885 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000886
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000887 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200888 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000889 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000890 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000891
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200892 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000893 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200894 mode is Z_FINISH, but checking both for safety*/
895 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 err = deflateEnd(&(self->zst));
897 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200898 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 Py_DECREF(RetVal);
900 RetVal = NULL;
901 goto error;
902 }
903 else
904 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000905
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000906 /* We will only get Z_BUF_ERROR if the output buffer was full
907 but there wasn't more output when we tried again, so it is
908 not an error condition.
909 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000910 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000911 zlib_error(self->zst, err, "while flushing");
912 Py_DECREF(RetVal);
913 RetVal = NULL;
914 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000915 }
Tim Peters977e5402001-10-17 03:57:20 +0000916
Gregory P. Smith693fc462008-09-06 20:13:06 +0000917 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200918 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000919 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000920
Tim Peters977e5402001-10-17 03:57:20 +0000921 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000922 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000923
924 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000925}
926
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000927#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700928
Larry Hastings61272b72014-01-07 12:41:53 -0800929/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800930zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700931
932Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800933[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700934
Larry Hastings3cceb382014-01-04 11:09:09 -0800935static PyObject *
936zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800937/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000938{
939 compobject *retval = NULL;
940 int err;
941
942 retval = newcompobject(&Comptype);
943 if (!retval) return NULL;
944
945 /* Copy the zstream state
946 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
947 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800948 ENTER_ZLIB(self);
949 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950 switch(err) {
951 case(Z_OK):
952 break;
953 case(Z_STREAM_ERROR):
954 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
955 goto error;
956 case(Z_MEM_ERROR):
957 PyErr_SetString(PyExc_MemoryError,
958 "Can't allocate memory for compression object");
959 goto error;
960 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800961 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962 goto error;
963 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800964 Py_INCREF(self->unused_data);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200965 Py_SETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800966 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200967 Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800968 Py_XINCREF(self->zdict);
Serhiy Storchaka191321d2015-12-27 15:41:34 +0200969 Py_SETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800970 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000971
972 /* Mark it as being initialized */
973 retval->is_initialised = 1;
974
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800975 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976 return (PyObject *)retval;
977
978error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800979 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000980 Py_XDECREF(retval);
981 return NULL;
982}
983
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200984/*[clinic input]
985zlib.Decompress.copy
986
987Return a copy of the decompression object.
988[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000989
990static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200991zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800992/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993{
994 compobject *retval = NULL;
995 int err;
996
997 retval = newcompobject(&Decomptype);
998 if (!retval) return NULL;
999
1000 /* Copy the zstream state
1001 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1002 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001003 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001004 err = inflateCopy(&retval->zst, &self->zst);
1005 switch(err) {
1006 case(Z_OK):
1007 break;
1008 case(Z_STREAM_ERROR):
1009 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1010 goto error;
1011 case(Z_MEM_ERROR):
1012 PyErr_SetString(PyExc_MemoryError,
1013 "Can't allocate memory for decompression object");
1014 goto error;
1015 default:
1016 zlib_error(self->zst, err, "while copying decompression object");
1017 goto error;
1018 }
1019
1020 Py_INCREF(self->unused_data);
Serhiy Storchaka191321d2015-12-27 15:41:34 +02001021 Py_SETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka191321d2015-12-27 15:41:34 +02001023 Py_SETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001024 Py_XINCREF(self->zdict);
Serhiy Storchaka191321d2015-12-27 15:41:34 +02001025 Py_SETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001026 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027
1028 /* Mark it as being initialized */
1029 retval->is_initialised = 1;
1030
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001031 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032 return (PyObject *)retval;
1033
1034error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001035 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001036 Py_XDECREF(retval);
1037 return NULL;
1038}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001039#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001041/*[clinic input]
1042zlib.Decompress.flush
1043
Martin Pantere99e9772015-11-20 08:13:35 +00001044 length: capped_uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001045 the initial size of the output buffer.
1046 /
1047
1048Return a bytes object containing any remaining decompressed data.
1049[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001050
Guido van Rossumfb221561997-04-29 15:38:09 +00001051static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001052zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Martin Pantere99e9772015-11-20 08:13:35 +00001053/*[clinic end generated code: output=db6fb753ab698e22 input=1bb961eb21b62aa0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001054{
Victor Stinnere079edd2013-11-21 22:33:21 +01001055 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001056 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001057 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001058 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001059 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001060
Victor Stinnere079edd2013-11-21 22:33:21 +01001061 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001062 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1063 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001064 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001065
Gregory P. Smith693fc462008-09-06 20:13:06 +00001066 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001067 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001068
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001069
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001070 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001071
Victor Stinnere079edd2013-11-21 22:33:21 +01001072 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1073
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001074 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001075 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1076 or equal than UINT_MAX */
1077 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001078 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001079 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001080 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001081
1082 Py_BEGIN_ALLOW_THREADS
1083 err = inflate(&(self->zst), Z_FINISH);
1084 Py_END_ALLOW_THREADS
1085
1086 /* while Z_OK and the output buffer is full, there might be more output,
1087 so extend the output buffer and try again */
1088 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001089 if (length <= (UINT_MAX >> 1))
1090 new_length = length << 1;
1091 else
1092 new_length = UINT_MAX;
1093 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001094 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001095 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001096 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001097 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1098 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001099 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001100
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001101 Py_BEGIN_ALLOW_THREADS
1102 err = inflate(&(self->zst), Z_FINISH);
1103 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001104 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001105
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001106 if (save_unconsumed_input(self, err) < 0) {
1107 Py_DECREF(retval);
1108 retval = NULL;
1109 goto error;
1110 }
1111
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001112 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001113 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001114 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001115 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001116 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001117 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001118 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001119 Py_DECREF(retval);
1120 retval = NULL;
1121 goto error;
1122 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001123 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001124
Gregory P. Smith693fc462008-09-06 20:13:06 +00001125 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001126 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001127 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001128
1129error:
1130
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001131 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001132
Jeremy Hylton9714f992001-10-16 21:19:45 +00001133 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001134}
1135
Christian Heimes936e2f32014-01-27 01:06:57 +01001136#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001137
Guido van Rossumfb221561997-04-29 15:38:09 +00001138static PyMethodDef comp_methods[] =
1139{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001140 ZLIB_COMPRESS_COMPRESS_METHODDEF
1141 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001142#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001143 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001144#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001145 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001146};
1147
1148static PyMethodDef Decomp_methods[] =
1149{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001150 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001151 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001152#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001153 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001154#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001155 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001156};
1157
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001158#define COMP_OFF(x) offsetof(compobject, x)
1159static PyMemberDef Decomp_members[] = {
1160 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1161 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001162 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001163 {NULL},
1164};
Guido van Rossumfb221561997-04-29 15:38:09 +00001165
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001166/*[clinic input]
1167zlib.adler32
1168
1169 data: Py_buffer
1170 value: unsigned_int(bitwise=True) = 1
1171 Starting value of the checksum.
1172 /
1173
1174Compute an Adler-32 checksum of data.
1175
1176The returned checksum is an integer.
1177[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001178
Guido van Rossumfb221561997-04-29 15:38:09 +00001179static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001180zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001181/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001182{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001183 /* Releasing the GIL for very small buffers is inefficient
1184 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001185 if (data->len > 1024*5) {
1186 unsigned char *buf = data->buf;
1187 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001188
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001189 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001190 /* Avoid truncation of length for very large buffers. adler32() takes
1191 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001192 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001193 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001194 buf += (size_t) UINT_MAX;
1195 len -= (size_t) UINT_MAX;
1196 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001197 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001198 Py_END_ALLOW_THREADS
1199 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001200 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001201 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001202 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001203}
Tim Peters977e5402001-10-17 03:57:20 +00001204
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001205/*[clinic input]
1206zlib.crc32
1207
1208 data: Py_buffer
1209 value: unsigned_int(bitwise=True) = 0
1210 Starting value of the checksum.
1211 /
1212
1213Compute a CRC-32 checksum of data.
1214
1215The returned checksum is an integer.
1216[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001217
1218static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001219zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001220/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001221{
Martin v. Löwis423be952008-08-13 15:53:07 +00001222 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001223
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001224 /* Releasing the GIL for very small buffers is inefficient
1225 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001226 if (data->len > 1024*5) {
1227 unsigned char *buf = data->buf;
1228 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001229
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001230 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001231 /* Avoid truncation of length for very large buffers. crc32() takes
1232 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001233 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001234 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001235 buf += (size_t) UINT_MAX;
1236 len -= (size_t) UINT_MAX;
1237 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001238 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001239 Py_END_ALLOW_THREADS
1240 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001241 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001242 }
Christian Heimescc47b052008-03-25 14:56:36 +00001243 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001244}
Tim Peters977e5402001-10-17 03:57:20 +00001245
Guido van Rossumfb221561997-04-29 15:38:09 +00001246
1247static PyMethodDef zlib_methods[] =
1248{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001249 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001250 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001251 ZLIB_COMPRESSOBJ_METHODDEF
1252 ZLIB_CRC32_METHODDEF
1253 ZLIB_DECOMPRESS_METHODDEF
1254 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001255 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001256};
1257
Tim Peters0c322792002-07-17 16:49:03 +00001258static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001259 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001260 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001261 sizeof(compobject),
1262 0,
1263 (destructor)Comp_dealloc, /*tp_dealloc*/
1264 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001265 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001266 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001267 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001268 0, /*tp_repr*/
1269 0, /*tp_as_number*/
1270 0, /*tp_as_sequence*/
1271 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001272 0, /*tp_hash*/
1273 0, /*tp_call*/
1274 0, /*tp_str*/
1275 0, /*tp_getattro*/
1276 0, /*tp_setattro*/
1277 0, /*tp_as_buffer*/
1278 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1279 0, /*tp_doc*/
1280 0, /*tp_traverse*/
1281 0, /*tp_clear*/
1282 0, /*tp_richcompare*/
1283 0, /*tp_weaklistoffset*/
1284 0, /*tp_iter*/
1285 0, /*tp_iternext*/
1286 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001287};
1288
Tim Peters0c322792002-07-17 16:49:03 +00001289static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001290 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001291 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001292 sizeof(compobject),
1293 0,
1294 (destructor)Decomp_dealloc, /*tp_dealloc*/
1295 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001296 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001297 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001298 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001299 0, /*tp_repr*/
1300 0, /*tp_as_number*/
1301 0, /*tp_as_sequence*/
1302 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001303 0, /*tp_hash*/
1304 0, /*tp_call*/
1305 0, /*tp_str*/
1306 0, /*tp_getattro*/
1307 0, /*tp_setattro*/
1308 0, /*tp_as_buffer*/
1309 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1310 0, /*tp_doc*/
1311 0, /*tp_traverse*/
1312 0, /*tp_clear*/
1313 0, /*tp_richcompare*/
1314 0, /*tp_weaklistoffset*/
1315 0, /*tp_iter*/
1316 0, /*tp_iternext*/
1317 Decomp_methods, /*tp_methods*/
1318 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001319};
1320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001321PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001322"The functions in this module allow compression and decompression using the\n"
1323"zlib library, which is based on GNU zip.\n"
1324"\n"
1325"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001326"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001327"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001328"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001329"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001330"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001331"\n"
1332"'wbits' is window buffer size.\n"
1333"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001334"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001335
Martin v. Löwis1a214512008-06-11 05:26:20 +00001336static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001337 PyModuleDef_HEAD_INIT,
1338 "zlib",
1339 zlib_module_documentation,
1340 -1,
1341 zlib_methods,
1342 NULL,
1343 NULL,
1344 NULL,
1345 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001346};
1347
Mark Hammond62b1ab12002-07-23 06:31:15 +00001348PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001349PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001350{
Fred Drake4baedc12002-04-01 14:53:37 +00001351 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001352 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001353 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001354 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001355 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001356 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001357 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001358 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001359
Fred Drake4baedc12002-04-01 14:53:37 +00001360 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1361 if (ZlibError != NULL) {
1362 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001363 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001364 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001365 PyModule_AddIntMacro(m, MAX_WBITS);
1366 PyModule_AddIntMacro(m, DEFLATED);
1367 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001368 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001369 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1370 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1371 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1372 PyModule_AddIntMacro(m, Z_FILTERED);
1373 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1374 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001375
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001376 PyModule_AddIntMacro(m, Z_FINISH);
1377 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1378 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1379 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001380
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001381 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001382 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001383 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001384
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001385 ver = PyUnicode_FromString(zlibVersion());
1386 if (ver != NULL)
1387 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1388
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001389 PyModule_AddStringConstant(m, "__version__", "1.0");
1390
Martin v. Löwis1a214512008-06-11 05:26:20 +00001391 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001392}