blob: 02c747ee8221b74e8caf777417dc0e407a99ea95 [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Victor Stinnerf18f8712014-07-01 16:48:12 +02006#define PY_SSIZE_T_CLEAN
Guido van Rossumfb221561997-04-29 15:38:09 +00007
Guido van Rossum97b54571997-06-03 22:21:47 +00008#include "Python.h"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00009#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +000010#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000011
Larry Hastings31826802013-10-19 00:09:25 -070012
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000013#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000014 #include "pythread.h"
15 #define ENTER_ZLIB(obj) \
16 Py_BEGIN_ALLOW_THREADS; \
17 PyThread_acquire_lock((obj)->lock, 1); \
18 Py_END_ALLOW_THREADS;
19 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000020#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000021 #define ENTER_ZLIB(obj)
22 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000023#endif
24
Guido van Rossumfb221561997-04-29 15:38:09 +000025/* The following parameters are copied from zutil.h, version 0.95 */
26#define DEFLATED 8
27#if MAX_MEM_LEVEL >= 8
28# define DEF_MEM_LEVEL 8
29#else
30# define DEF_MEM_LEVEL MAX_MEM_LEVEL
31#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000032
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020033/* Initial buffer size. */
34#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Jeremy Hylton938ace62002-07-17 16:30:39 +000036static PyTypeObject Comptype;
37static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000038
39static PyObject *ZlibError;
40
Tim Peters977e5402001-10-17 03:57:20 +000041typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000042{
Jeremy Hylton9714f992001-10-16 21:19:45 +000043 PyObject_HEAD
44 z_stream zst;
45 PyObject *unused_data;
46 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020047 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000048 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020049 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000050 #ifdef WITH_THREAD
51 PyThread_type_lock lock;
52 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000053} compobject;
54
Jeremy Hylton0965e082001-10-16 21:56:09 +000055static void
56zlib_error(z_stream zst, int err, char *msg)
57{
Nadeem Vawda524148a2011-08-28 11:26:46 +020058 const char *zmsg = Z_NULL;
59 /* In case of a version mismatch, zst.msg won't be initialized.
60 Check for this case first, before looking at zst.msg. */
61 if (err == Z_VERSION_ERROR)
62 zmsg = "library version mismatch";
63 if (zmsg == Z_NULL)
64 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000065 if (zmsg == Z_NULL) {
66 switch (err) {
67 case Z_BUF_ERROR:
68 zmsg = "incomplete or truncated stream";
69 break;
70 case Z_STREAM_ERROR:
71 zmsg = "inconsistent stream state";
72 break;
73 case Z_DATA_ERROR:
74 zmsg = "invalid input data";
75 break;
76 }
77 }
78 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000080 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000081 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000082}
83
Larry Hastings61272b72014-01-07 12:41:53 -080084/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080085module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080086class zlib.Compress "compobject *" "&Comptype"
87class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080088[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030089/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080090
Guido van Rossumfb221561997-04-29 15:38:09 +000091static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000092newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000093{
Tim Peters977e5402001-10-17 03:57:20 +000094 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000095 self = PyObject_New(compobject, type);
96 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020098 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200100 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000101 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000103 Py_DECREF(self);
104 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000106 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000107 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000108 Py_DECREF(self);
109 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000110 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000111#ifdef WITH_THREAD
112 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200113 if (self->lock == NULL) {
114 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
115 return NULL;
116 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000117#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000118 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000119}
120
Victor Stinner5064a522013-07-07 16:50:27 +0200121static void*
122PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
123{
124 if (items > (size_t)PY_SSIZE_T_MAX / size)
125 return NULL;
126 /* PyMem_Malloc() cannot be used: the GIL is not held when
127 inflate() and deflate() are called */
128 return PyMem_RawMalloc(items * size);
129}
130
131static void
132PyZlib_Free(voidpf ctx, void *ptr)
133{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200134 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200135}
136
Larry Hastings61272b72014-01-07 12:41:53 -0800137/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800138zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200139
Larry Hastingsebdcb502013-11-23 14:54:00 -0800140 bytes: Py_buffer
141 Binary data to be compressed.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200142 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Larry Hastingsebdcb502013-11-23 14:54:00 -0800143 Compression level, in 0-9.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800144 /
145
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200146Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800147[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800148
Guido van Rossumfb221561997-04-29 15:38:09 +0000149static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200150zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int level)
Larry Hastings581ee362014-01-28 05:00:08 -0800151/*[clinic end generated code: output=5d7dd4588788efd3 input=be3abe9934bda4b3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000152{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000153 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200154 Byte *input, *output = NULL;
155 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800156 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000157 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000158
Larry Hastingsebdcb502013-11-23 14:54:00 -0800159 if ((size_t)bytes->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200160 PyErr_SetString(PyExc_OverflowError,
161 "Size does not fit in an unsigned int");
162 goto error;
163 }
Larry Hastingsebdcb502013-11-23 14:54:00 -0800164 input = bytes->buf;
165 length = (unsigned int)bytes->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000166
Jeremy Hylton9714f992001-10-16 21:19:45 +0000167 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000168
Victor Stinnerb6404912013-07-07 16:21:41 +0200169 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000170 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 PyErr_SetString(PyExc_MemoryError,
172 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200173 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000174 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000175
Jeremy Hylton9714f992001-10-16 21:19:45 +0000176 /* Past the point of no return. From here on out, we need to make sure
177 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000178
Victor Stinner5064a522013-07-07 16:50:27 +0200179 zst.opaque = NULL;
180 zst.zalloc = PyZlib_Malloc;
181 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 zst.next_out = (Byte *)output;
183 zst.next_in = (Byte *)input;
184 zst.avail_in = length;
185 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000186
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000187 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000188 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000190 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000191 PyErr_SetString(PyExc_MemoryError,
192 "Out of memory while compressing data");
193 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000194 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000195 PyErr_SetString(ZlibError,
196 "Bad compression level");
197 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000198 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000199 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000200 zlib_error(zst, err, "while compressing data");
201 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000202 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000203
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000204 Py_BEGIN_ALLOW_THREADS;
205 err = deflate(&zst, Z_FINISH);
206 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000208 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000209 zlib_error(zst, err, "while compressing data");
210 deflateEnd(&zst);
211 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000212 }
Tim Peters977e5402001-10-17 03:57:20 +0000213
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000214 err=deflateEnd(&zst);
215 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000217 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000218 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000219 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000220
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000221 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200222 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Jeremy Hylton9714f992001-10-16 21:19:45 +0000224 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000225}
226
Larry Hastings61272b72014-01-07 12:41:53 -0800227/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100228
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
Martin Panter0fdf41d2016-05-27 07:32:11 +0000274 The window buffer size and container format.
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 Panter0fdf41d2016-05-27 07:32:11 +0000285/*[clinic end generated code: output=444d0987f3429574 input=75123b0d4ff0541d]*/
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
Martin Panter0fdf41d2016-05-27 07:32:11 +0000399 +9 to +15: The base-two logarithm of the window size. Include a zlib
400 container.
401 -9 to -15: Generate a raw stream.
402 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200403 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
404 Controls the amount of memory used for internal compression state.
405 Valid values range from 1 to 9. Higher values result in higher memory
406 usage, faster compression, and smaller output.
407 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
408 Used to tune the compression algorithm. Possible values are
409 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
410 zdict: Py_buffer = None
411 The predefined compression dictionary - a sequence of bytes
412 containing subsequences that are likely to occur in the input data.
413
414Return a compressor object.
415[clinic start generated code]*/
416
Guido van Rossumfb221561997-04-29 15:38:09 +0000417static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400418zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
419 int memLevel, int strategy, Py_buffer *zdict)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000420/*[clinic end generated code: output=2949bbb9a5723ccd input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000421{
Victor Stinnere079edd2013-11-21 22:33:21 +0100422 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200423 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000424
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200425 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100426 PyErr_SetString(PyExc_OverflowError,
427 "zdict length does not fit in an unsigned int");
428 goto error;
429 }
430
Jeremy Hylton499000002001-10-16 21:59:35 +0000431 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000432 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200433 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200434 self->zst.opaque = NULL;
435 self->zst.zalloc = PyZlib_Malloc;
436 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000437 self->zst.next_in = NULL;
438 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000439 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
440 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000441 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000442 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200443 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200444 goto success;
445 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100446 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200447 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200448 switch (err) {
449 case (Z_OK):
450 goto success;
451 case (Z_STREAM_ERROR):
452 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
453 goto error;
454 default:
455 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
456 goto error;
457 }
458 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000459 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000460 PyErr_SetString(PyExc_MemoryError,
461 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200462 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200465 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000466 default:
467 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200468 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000469 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200470
471 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200472 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200473 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200474 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000475}
476
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200477/*[clinic input]
478zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200479
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200480 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000481 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200482 zdict: object(c_default="NULL") = b''
483 The predefined compression dictionary. This must be the same
484 dictionary as used by the compressor that produced the input data.
485
486Return a decompressor object.
487[clinic start generated code]*/
488
489static PyObject *
490zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000491/*[clinic end generated code: output=8ccd583fbd631798 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200492{
493 int err;
494 compobject *self;
495
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200496 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
497 PyErr_SetString(PyExc_TypeError,
498 "zdict argument must support the buffer protocol");
499 return NULL;
500 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000501
502 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000503 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000504 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200505 self->zst.opaque = NULL;
506 self->zst.zalloc = PyZlib_Malloc;
507 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000508 self->zst.next_in = NULL;
509 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200510 if (zdict != NULL) {
511 Py_INCREF(zdict);
512 self->zdict = zdict;
513 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000514 err = inflateInit2(&self->zst, wbits);
515 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000516 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000517 self->is_initialised = 1;
518 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000519 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000520 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000521 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
522 return NULL;
523 case (Z_MEM_ERROR):
524 Py_DECREF(self);
525 PyErr_SetString(PyExc_MemoryError,
526 "Can't allocate memory for decompression object");
527 return NULL;
528 default:
529 zlib_error(self->zst, err, "while creating decompression object");
530 Py_DECREF(self);
531 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000532 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000533}
534
535static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000536Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000537{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000538#ifdef WITH_THREAD
539 PyThread_free_lock(self->lock);
540#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000541 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000542 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200543 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000544 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000545}
546
547static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000548Comp_dealloc(compobject *self)
549{
550 if (self->is_initialised)
551 deflateEnd(&self->zst);
552 Dealloc(self);
553}
554
555static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000556Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000557{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000558 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000559 inflateEnd(&self->zst);
560 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000561}
562
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200563/*[clinic input]
564zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000565
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200566 data: Py_buffer
567 Binary data to be compressed.
568 /
569
570Returns a bytes object containing compressed data.
571
572After calling this function, some of the input data may still
573be stored in internal buffers for later processing.
574Call the flush() method to clear these buffers.
575[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000576
Guido van Rossumfb221561997-04-29 15:38:09 +0000577static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200578zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800579/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000580{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200581 int err;
582 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200583 unsigned int length = DEF_BUF_SIZE, new_length;
584 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000585 Byte *input;
586 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000587
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200588 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200589 PyErr_SetString(PyExc_OverflowError,
590 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200591 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200592 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200593 input = data->buf;
594 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000595
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200596 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200597 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000598
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000599 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000600
Jeremy Hylton9714f992001-10-16 21:19:45 +0000601 start_total_out = self->zst.total_out;
602 self->zst.avail_in = inplen;
603 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000604 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000605 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000606
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000607 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000608 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000609 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000610
Jeremy Hylton9714f992001-10-16 21:19:45 +0000611 /* while Z_OK and the output buffer is full, there might be more output,
612 so extend the output buffer and try again */
613 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100614 if (length <= (UINT_MAX >> 1))
615 new_length = length << 1;
616 else
617 new_length = UINT_MAX;
618 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200619 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200620 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000621 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000623 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000624 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100625 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000626
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000627 Py_BEGIN_ALLOW_THREADS
628 err = deflate(&(self->zst), Z_NO_FLUSH);
629 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000630 }
Tim Peters977e5402001-10-17 03:57:20 +0000631 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000632 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000633 condition.
634 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000635
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000636 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200637 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200638 Py_CLEAR(RetVal);
639 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000640 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000641 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200642 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000643 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000644
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200645 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000646 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000648}
649
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100650/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
651 self->unused_data or self->unconsumed_tail, as appropriate. */
652static int
653save_unconsumed_input(compobject *self, int err)
654{
655 if (err == Z_STREAM_END) {
656 /* The end of the compressed data has been reached. Store the leftover
657 input data in self->unused_data. */
658 if (self->zst.avail_in > 0) {
659 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
660 Py_ssize_t new_size;
661 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100662 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100663 PyErr_NoMemory();
664 return -1;
665 }
666 new_size = old_size + self->zst.avail_in;
667 new_data = PyBytes_FromStringAndSize(NULL, new_size);
668 if (new_data == NULL)
669 return -1;
670 Py_MEMCPY(PyBytes_AS_STRING(new_data),
671 PyBytes_AS_STRING(self->unused_data), old_size);
672 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
673 self->zst.next_in, self->zst.avail_in);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300674 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100675 self->zst.avail_in = 0;
676 }
677 }
678 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
679 /* This code handles two distinct cases:
680 1. Output limit was reached. Save leftover input in unconsumed_tail.
681 2. All input data was consumed. Clear unconsumed_tail. */
682 PyObject *new_data = PyBytes_FromStringAndSize(
683 (char *)self->zst.next_in, self->zst.avail_in);
684 if (new_data == NULL)
685 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300686 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100687 }
688 return 0;
689}
690
Larry Hastings61272b72014-01-07 12:41:53 -0800691/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800692zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700693
694 data: Py_buffer
695 The binary data to decompress.
Martin Pantere99e9772015-11-20 08:13:35 +0000696 max_length: capped_uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700697 The maximum allowable length of the decompressed data.
698 Unconsumed input data will be stored in
699 the unconsumed_tail attribute.
700 /
701
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200702Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700703
704After calling this function, some of the input data may still be stored in
705internal buffers for later processing.
706Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800707[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700708
Larry Hastings31826802013-10-19 00:09:25 -0700709static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400710zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
711 unsigned int max_length)
Martin Pantere99e9772015-11-20 08:13:35 +0000712/*[clinic end generated code: output=b82e2a2c19f5fe7b input=68b6508ab07c2cf0]*/
Larry Hastings31826802013-10-19 00:09:25 -0700713{
Larry Hastings31826802013-10-19 00:09:25 -0700714 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200715 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200716 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000717 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000718
Victor Stinnere079edd2013-11-21 22:33:21 +0100719 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200720 PyErr_SetString(PyExc_OverflowError,
721 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700722 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200723 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000724
Jeremy Hylton9714f992001-10-16 21:19:45 +0000725 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000726 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000727 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200728 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700729 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000730
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800731 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000732
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800733 start_total_out = self->zst.total_out;
734 self->zst.avail_in = (unsigned int)data->len;
735 self->zst.next_in = data->buf;
736 self->zst.avail_out = length;
737 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000738
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000739 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800740 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000741 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000742
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800743 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200744 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800745 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200746 Py_DECREF(RetVal);
747 RetVal = NULL;
748 goto error;
749 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100750
751 if ((size_t)zdict_buf.len > UINT_MAX) {
752 PyErr_SetString(PyExc_OverflowError,
753 "zdict length does not fit in an unsigned int");
754 PyBuffer_Release(&zdict_buf);
755 Py_CLEAR(RetVal);
756 goto error;
757 }
758
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800759 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100760 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200761 PyBuffer_Release(&zdict_buf);
762 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800763 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100764 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200765 goto error;
766 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200767 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200768 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800769 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200770 Py_END_ALLOW_THREADS
771 }
772
Jeremy Hylton9714f992001-10-16 21:19:45 +0000773 /* While Z_OK and the output buffer is full, there might be more output.
774 So extend the output buffer and try again.
775 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800776 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000777 /* If max_length set, don't continue decompressing if we've already
778 reached the limit.
779 */
780 if (max_length && length >= max_length)
781 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000782
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000783 /* otherwise, ... */
784 old_length = length;
785 length = length << 1;
786 if (max_length && length > max_length)
787 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000788
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000789 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200790 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000791 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000792 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800793 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000794 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800795 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000796
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000797 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800798 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000799 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000800 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000801
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800802 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200803 Py_DECREF(RetVal);
804 RetVal = NULL;
805 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000806 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000807
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000808 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100809 /* This is the logical place to call inflateEnd, but the old behaviour
810 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800811 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100812 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 /* We will only get Z_BUF_ERROR if the output buffer was full
814 but there wasn't more output when we tried again, so it is
815 not an error condition.
816 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800817 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000818 Py_DECREF(RetVal);
819 RetVal = NULL;
820 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000821 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000822
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800823 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200824 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000825 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000826
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000827 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800828 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000829 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000830}
831
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200832/*[clinic input]
833zlib.Compress.flush
834
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200835 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200836 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
837 If mode == Z_FINISH, the compressor object can no longer be
838 used after calling the flush() method. Otherwise, more data
839 can still be compressed.
840 /
841
842Return a bytes object containing any remaining compressed data.
843[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000844
Guido van Rossumfb221561997-04-29 15:38:09 +0000845static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200846zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200847/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000848{
Victor Stinnere079edd2013-11-21 22:33:21 +0100849 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200850 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000851 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000852 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000853
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
855 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200856 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000857 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000858 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000859
Gregory P. Smith693fc462008-09-06 20:13:06 +0000860 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000861 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000862
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000863 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000864
Jeremy Hylton9714f992001-10-16 21:19:45 +0000865 start_total_out = self->zst.total_out;
866 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000867 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000868 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000869
870 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200871 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000872 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000873
Jeremy Hylton9714f992001-10-16 21:19:45 +0000874 /* while Z_OK and the output buffer is full, there might be more output,
875 so extend the output buffer and try again */
876 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100877 if (length <= (UINT_MAX >> 1))
878 new_length = length << 1;
879 else
880 new_length = UINT_MAX;
881 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200882 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000884 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000886 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000887 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100888 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000889
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200891 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000892 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000893 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000894
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200895 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000896 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200897 mode is Z_FINISH, but checking both for safety*/
898 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 err = deflateEnd(&(self->zst));
900 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200901 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 Py_DECREF(RetVal);
903 RetVal = NULL;
904 goto error;
905 }
906 else
907 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000908
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000909 /* We will only get Z_BUF_ERROR if the output buffer was full
910 but there wasn't more output when we tried again, so it is
911 not an error condition.
912 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000913 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 zlib_error(self->zst, err, "while flushing");
915 Py_DECREF(RetVal);
916 RetVal = NULL;
917 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000918 }
Tim Peters977e5402001-10-17 03:57:20 +0000919
Gregory P. Smith693fc462008-09-06 20:13:06 +0000920 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200921 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000922 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000923
Tim Peters977e5402001-10-17 03:57:20 +0000924 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000925 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000926
927 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000928}
929
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000930#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700931
Larry Hastings61272b72014-01-07 12:41:53 -0800932/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800933zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700934
935Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800936[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700937
Larry Hastings3cceb382014-01-04 11:09:09 -0800938static PyObject *
939zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800940/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000941{
942 compobject *retval = NULL;
943 int err;
944
945 retval = newcompobject(&Comptype);
946 if (!retval) return NULL;
947
948 /* Copy the zstream state
949 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
950 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800951 ENTER_ZLIB(self);
952 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 switch(err) {
954 case(Z_OK):
955 break;
956 case(Z_STREAM_ERROR):
957 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
958 goto error;
959 case(Z_MEM_ERROR):
960 PyErr_SetString(PyExc_MemoryError,
961 "Can't allocate memory for compression object");
962 goto error;
963 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800964 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000965 goto error;
966 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800967 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300968 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800969 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300970 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800971 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300972 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800973 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974
975 /* Mark it as being initialized */
976 retval->is_initialised = 1;
977
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800978 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979 return (PyObject *)retval;
980
981error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800982 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000983 Py_XDECREF(retval);
984 return NULL;
985}
986
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200987/*[clinic input]
988zlib.Decompress.copy
989
990Return a copy of the decompression object.
991[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000992
993static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200994zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800995/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000996{
997 compobject *retval = NULL;
998 int err;
999
1000 retval = newcompobject(&Decomptype);
1001 if (!retval) return NULL;
1002
1003 /* Copy the zstream state
1004 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1005 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001006 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001007 err = inflateCopy(&retval->zst, &self->zst);
1008 switch(err) {
1009 case(Z_OK):
1010 break;
1011 case(Z_STREAM_ERROR):
1012 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1013 goto error;
1014 case(Z_MEM_ERROR):
1015 PyErr_SetString(PyExc_MemoryError,
1016 "Can't allocate memory for decompression object");
1017 goto error;
1018 default:
1019 zlib_error(self->zst, err, "while copying decompression object");
1020 goto error;
1021 }
1022
1023 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001024 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001026 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001027 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001028 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001029 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030
1031 /* Mark it as being initialized */
1032 retval->is_initialised = 1;
1033
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001034 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035 return (PyObject *)retval;
1036
1037error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001038 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039 Py_XDECREF(retval);
1040 return NULL;
1041}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001042#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001044/*[clinic input]
1045zlib.Decompress.flush
1046
Martin Pantere99e9772015-11-20 08:13:35 +00001047 length: capped_uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001048 the initial size of the output buffer.
1049 /
1050
1051Return a bytes object containing any remaining decompressed data.
1052[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001053
Guido van Rossumfb221561997-04-29 15:38:09 +00001054static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001055zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Martin Pantere99e9772015-11-20 08:13:35 +00001056/*[clinic end generated code: output=db6fb753ab698e22 input=1bb961eb21b62aa0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001057{
Victor Stinnere079edd2013-11-21 22:33:21 +01001058 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001059 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001060 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001061 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001062 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001063
Victor Stinnere079edd2013-11-21 22:33:21 +01001064 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001065 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1066 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001067 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001068
Gregory P. Smith693fc462008-09-06 20:13:06 +00001069 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001070 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001071
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001072
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001073 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001074
Victor Stinnere079edd2013-11-21 22:33:21 +01001075 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1076
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001077 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001078 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1079 or equal than UINT_MAX */
1080 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001081 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001082 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001083 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001084
1085 Py_BEGIN_ALLOW_THREADS
1086 err = inflate(&(self->zst), Z_FINISH);
1087 Py_END_ALLOW_THREADS
1088
1089 /* while Z_OK and the output buffer is full, there might be more output,
1090 so extend the output buffer and try again */
1091 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001092 if (length <= (UINT_MAX >> 1))
1093 new_length = length << 1;
1094 else
1095 new_length = UINT_MAX;
1096 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001097 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001098 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001099 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001100 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1101 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001102 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001103
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001104 Py_BEGIN_ALLOW_THREADS
1105 err = inflate(&(self->zst), Z_FINISH);
1106 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001107 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001108
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001109 if (save_unconsumed_input(self, err) < 0) {
1110 Py_DECREF(retval);
1111 retval = NULL;
1112 goto error;
1113 }
1114
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001115 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001116 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001117 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001118 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001119 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001120 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001121 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001122 Py_DECREF(retval);
1123 retval = NULL;
1124 goto error;
1125 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001126 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001127
Gregory P. Smith693fc462008-09-06 20:13:06 +00001128 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001129 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001130 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001131
1132error:
1133
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001134 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001135
Jeremy Hylton9714f992001-10-16 21:19:45 +00001136 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001137}
1138
Christian Heimes936e2f32014-01-27 01:06:57 +01001139#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001140
Guido van Rossumfb221561997-04-29 15:38:09 +00001141static PyMethodDef comp_methods[] =
1142{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001143 ZLIB_COMPRESS_COMPRESS_METHODDEF
1144 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001145#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001146 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001147#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001148 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001149};
1150
1151static PyMethodDef Decomp_methods[] =
1152{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001153 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001154 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001155#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001156 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001157#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001158 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001159};
1160
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001161#define COMP_OFF(x) offsetof(compobject, x)
1162static PyMemberDef Decomp_members[] = {
1163 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1164 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001165 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001166 {NULL},
1167};
Guido van Rossumfb221561997-04-29 15:38:09 +00001168
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001169/*[clinic input]
1170zlib.adler32
1171
1172 data: Py_buffer
1173 value: unsigned_int(bitwise=True) = 1
1174 Starting value of the checksum.
1175 /
1176
1177Compute an Adler-32 checksum of data.
1178
1179The returned checksum is an integer.
1180[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001181
Guido van Rossumfb221561997-04-29 15:38:09 +00001182static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001183zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001184/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001185{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001186 /* Releasing the GIL for very small buffers is inefficient
1187 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001188 if (data->len > 1024*5) {
1189 unsigned char *buf = data->buf;
1190 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001191
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001192 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001193 /* Avoid truncation of length for very large buffers. adler32() takes
1194 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001195 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001196 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001197 buf += (size_t) UINT_MAX;
1198 len -= (size_t) UINT_MAX;
1199 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001200 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001201 Py_END_ALLOW_THREADS
1202 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001203 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001204 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001205 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001206}
Tim Peters977e5402001-10-17 03:57:20 +00001207
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001208/*[clinic input]
1209zlib.crc32
1210
1211 data: Py_buffer
1212 value: unsigned_int(bitwise=True) = 0
1213 Starting value of the checksum.
1214 /
1215
1216Compute a CRC-32 checksum of data.
1217
1218The returned checksum is an integer.
1219[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001220
1221static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001222zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001223/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001224{
Martin v. Löwis423be952008-08-13 15:53:07 +00001225 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001226
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001227 /* Releasing the GIL for very small buffers is inefficient
1228 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001229 if (data->len > 1024*5) {
1230 unsigned char *buf = data->buf;
1231 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001232
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001233 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001234 /* Avoid truncation of length for very large buffers. crc32() takes
1235 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001236 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001237 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001238 buf += (size_t) UINT_MAX;
1239 len -= (size_t) UINT_MAX;
1240 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001241 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001242 Py_END_ALLOW_THREADS
1243 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001244 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001245 }
Christian Heimescc47b052008-03-25 14:56:36 +00001246 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001247}
Tim Peters977e5402001-10-17 03:57:20 +00001248
Guido van Rossumfb221561997-04-29 15:38:09 +00001249
1250static PyMethodDef zlib_methods[] =
1251{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001252 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001253 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001254 ZLIB_COMPRESSOBJ_METHODDEF
1255 ZLIB_CRC32_METHODDEF
1256 ZLIB_DECOMPRESS_METHODDEF
1257 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001258 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001259};
1260
Tim Peters0c322792002-07-17 16:49:03 +00001261static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001262 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001263 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001264 sizeof(compobject),
1265 0,
1266 (destructor)Comp_dealloc, /*tp_dealloc*/
1267 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001268 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001269 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001270 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001271 0, /*tp_repr*/
1272 0, /*tp_as_number*/
1273 0, /*tp_as_sequence*/
1274 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001275 0, /*tp_hash*/
1276 0, /*tp_call*/
1277 0, /*tp_str*/
1278 0, /*tp_getattro*/
1279 0, /*tp_setattro*/
1280 0, /*tp_as_buffer*/
1281 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1282 0, /*tp_doc*/
1283 0, /*tp_traverse*/
1284 0, /*tp_clear*/
1285 0, /*tp_richcompare*/
1286 0, /*tp_weaklistoffset*/
1287 0, /*tp_iter*/
1288 0, /*tp_iternext*/
1289 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001290};
1291
Tim Peters0c322792002-07-17 16:49:03 +00001292static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001293 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001294 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001295 sizeof(compobject),
1296 0,
1297 (destructor)Decomp_dealloc, /*tp_dealloc*/
1298 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001299 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001300 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001301 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001302 0, /*tp_repr*/
1303 0, /*tp_as_number*/
1304 0, /*tp_as_sequence*/
1305 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001306 0, /*tp_hash*/
1307 0, /*tp_call*/
1308 0, /*tp_str*/
1309 0, /*tp_getattro*/
1310 0, /*tp_setattro*/
1311 0, /*tp_as_buffer*/
1312 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1313 0, /*tp_doc*/
1314 0, /*tp_traverse*/
1315 0, /*tp_clear*/
1316 0, /*tp_richcompare*/
1317 0, /*tp_weaklistoffset*/
1318 0, /*tp_iter*/
1319 0, /*tp_iternext*/
1320 Decomp_methods, /*tp_methods*/
1321 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001322};
1323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001324PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001325"The functions in this module allow compression and decompression using the\n"
1326"zlib library, which is based on GNU zip.\n"
1327"\n"
1328"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001329"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001330"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001331"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001332"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001333"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001334"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001335"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001336"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001338
Martin v. Löwis1a214512008-06-11 05:26:20 +00001339static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001340 PyModuleDef_HEAD_INIT,
1341 "zlib",
1342 zlib_module_documentation,
1343 -1,
1344 zlib_methods,
1345 NULL,
1346 NULL,
1347 NULL,
1348 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001349};
1350
Mark Hammond62b1ab12002-07-23 06:31:15 +00001351PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001352PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001353{
Fred Drake4baedc12002-04-01 14:53:37 +00001354 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001355 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001356 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001357 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001358 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001359 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001360 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001361 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001362
Fred Drake4baedc12002-04-01 14:53:37 +00001363 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1364 if (ZlibError != NULL) {
1365 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001366 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001367 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001368 PyModule_AddIntMacro(m, MAX_WBITS);
1369 PyModule_AddIntMacro(m, DEFLATED);
1370 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001371 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001372 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1373 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1374 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1375 PyModule_AddIntMacro(m, Z_FILTERED);
1376 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1377 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001378
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001379 PyModule_AddIntMacro(m, Z_FINISH);
1380 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1381 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1382 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001383
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001384 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001385 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001386 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001387
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001388 ver = PyUnicode_FromString(zlibVersion());
1389 if (ver != NULL)
1390 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1391
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001392 PyModule_AddStringConstant(m, "__version__", "1.0");
1393
Martin v. Löwis1a214512008-06-11 05:26:20 +00001394 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001395}