blob: ad6f28d7bc6bd639be0e6f3197bf138953dcaa45 [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
Martin Panter3f0ee832016-06-05 10:48:34 +000025#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
26#define AT_LEAST_ZLIB_1_2_2_1
27#endif
28
Guido van Rossumfb221561997-04-29 15:38:09 +000029/* The following parameters are copied from zutil.h, version 0.95 */
30#define DEFLATED 8
31#if MAX_MEM_LEVEL >= 8
32# define DEF_MEM_LEVEL 8
33#else
34# define DEF_MEM_LEVEL MAX_MEM_LEVEL
35#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000036
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020037/* Initial buffer size. */
38#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000039
Jeremy Hylton938ace62002-07-17 16:30:39 +000040static PyTypeObject Comptype;
41static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000042
43static PyObject *ZlibError;
44
Tim Peters977e5402001-10-17 03:57:20 +000045typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000046{
Jeremy Hylton9714f992001-10-16 21:19:45 +000047 PyObject_HEAD
48 z_stream zst;
49 PyObject *unused_data;
50 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020051 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000052 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020053 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000054 #ifdef WITH_THREAD
55 PyThread_type_lock lock;
56 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000057} compobject;
58
Jeremy Hylton0965e082001-10-16 21:56:09 +000059static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020060zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000061{
Nadeem Vawda524148a2011-08-28 11:26:46 +020062 const char *zmsg = Z_NULL;
63 /* In case of a version mismatch, zst.msg won't be initialized.
64 Check for this case first, before looking at zst.msg. */
65 if (err == Z_VERSION_ERROR)
66 zmsg = "library version mismatch";
67 if (zmsg == Z_NULL)
68 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000069 if (zmsg == Z_NULL) {
70 switch (err) {
71 case Z_BUF_ERROR:
72 zmsg = "incomplete or truncated stream";
73 break;
74 case Z_STREAM_ERROR:
75 zmsg = "inconsistent stream state";
76 break;
77 case Z_DATA_ERROR:
78 zmsg = "invalid input data";
79 break;
80 }
81 }
82 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000084 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000085 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000086}
87
Larry Hastings61272b72014-01-07 12:41:53 -080088/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080089module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080090class zlib.Compress "compobject *" "&Comptype"
91class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080092[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030093/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080094
Guido van Rossumfb221561997-04-29 15:38:09 +000095static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000096newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000097{
Tim Peters977e5402001-10-17 03:57:20 +000098 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 self = PyObject_New(compobject, type);
100 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000101 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200102 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000103 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200104 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000105 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000106 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000107 Py_DECREF(self);
108 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000109 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000110 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 Py_DECREF(self);
113 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000115#ifdef WITH_THREAD
116 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200117 if (self->lock == NULL) {
118 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
119 return NULL;
120 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000121#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000123}
124
Victor Stinner5064a522013-07-07 16:50:27 +0200125static void*
126PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
127{
128 if (items > (size_t)PY_SSIZE_T_MAX / size)
129 return NULL;
130 /* PyMem_Malloc() cannot be used: the GIL is not held when
131 inflate() and deflate() are called */
132 return PyMem_RawMalloc(items * size);
133}
134
135static void
136PyZlib_Free(voidpf ctx, void *ptr)
137{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200138 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200139}
140
Larry Hastings61272b72014-01-07 12:41:53 -0800141/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800142zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200143
Martin Panter1fe0d132016-02-10 10:06:36 +0000144 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800145 Binary data to be compressed.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200146 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000147 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800148
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200149Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800150[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800151
Guido van Rossumfb221561997-04-29 15:38:09 +0000152static PyObject *
Martin Panter1fe0d132016-02-10 10:06:36 +0000153zlib_compress_impl(PyModuleDef *module, Py_buffer *data, int level)
Martin Panterb0cb42d2016-02-10 10:45:54 +0000154/*[clinic end generated code: output=1b97589132b203b4 input=abed30f4fa14e213]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000155{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000156 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200157 Byte *input, *output = NULL;
158 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800159 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000160 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000161
Martin Panter1fe0d132016-02-10 10:06:36 +0000162 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200163 PyErr_SetString(PyExc_OverflowError,
164 "Size does not fit in an unsigned int");
165 goto error;
166 }
Martin Panter1fe0d132016-02-10 10:06:36 +0000167 input = data->buf;
168 length = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000169
Jeremy Hylton9714f992001-10-16 21:19:45 +0000170 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000171
Victor Stinnerb6404912013-07-07 16:21:41 +0200172 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000173 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000174 PyErr_SetString(PyExc_MemoryError,
175 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200176 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000177 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000178
Jeremy Hylton9714f992001-10-16 21:19:45 +0000179 /* Past the point of no return. From here on out, we need to make sure
180 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000181
Victor Stinner5064a522013-07-07 16:50:27 +0200182 zst.opaque = NULL;
183 zst.zalloc = PyZlib_Malloc;
184 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000185 zst.next_out = (Byte *)output;
186 zst.next_in = (Byte *)input;
187 zst.avail_in = length;
188 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000189
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000190 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000191 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000193 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000194 PyErr_SetString(PyExc_MemoryError,
195 "Out of memory while compressing data");
196 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000197 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000198 PyErr_SetString(ZlibError,
199 "Bad compression level");
200 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000201 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000202 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000203 zlib_error(zst, err, "while compressing data");
204 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000205 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000206
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000207 Py_BEGIN_ALLOW_THREADS;
208 err = deflate(&zst, Z_FINISH);
209 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000210
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000211 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000212 zlib_error(zst, err, "while compressing data");
213 deflateEnd(&zst);
214 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000215 }
Tim Peters977e5402001-10-17 03:57:20 +0000216
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000217 err=deflateEnd(&zst);
218 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000219 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000220 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000221 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000222 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000224 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200225 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000226
Jeremy Hylton9714f992001-10-16 21:19:45 +0000227 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000228}
229
Larry Hastings61272b72014-01-07 12:41:53 -0800230/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100231
Martin Pantere99e9772015-11-20 08:13:35 +0000232class capped_uint_converter(CConverter):
Victor Stinnere079edd2013-11-21 22:33:21 +0100233 type = 'unsigned int'
Martin Pantere99e9772015-11-20 08:13:35 +0000234 converter = 'capped_uint_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200235 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100236
Larry Hastings61272b72014-01-07 12:41:53 -0800237[python start generated code]*/
Martin Pantere99e9772015-11-20 08:13:35 +0000238/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100239
240static int
Martin Pantere99e9772015-11-20 08:13:35 +0000241capped_uint_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100242{
Martin Pantere99e9772015-11-20 08:13:35 +0000243 PyObject *long_obj;
244 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100245
Martin Pantere99e9772015-11-20 08:13:35 +0000246 long_obj = (PyObject *)_PyLong_FromNbInt(obj);
247 if (long_obj == NULL) {
248 return 0;
249 }
250 val = PyLong_AsSsize_t(long_obj);
251 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100252 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000253 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100254 }
Martin Pantere99e9772015-11-20 08:13:35 +0000255 if (val < 0) {
256 PyErr_SetString(PyExc_ValueError,
257 "value must be positive");
Victor Stinner5c867332014-01-03 12:26:12 +0100258 return 0;
259 }
260
Martin Pantere99e9772015-11-20 08:13:35 +0000261 if ((size_t)val > UINT_MAX) {
262 *(unsigned int *)ptr = UINT_MAX;
263 }
264 else {
265 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(val, Py_ssize_t,
266 unsigned int);
267 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100268 return 1;
269}
270
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200271/*[clinic input]
272zlib.decompress
273
274 data: Py_buffer
275 Compressed data.
276 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000277 The window buffer size and container format.
Martin Pantere99e9772015-11-20 08:13:35 +0000278 bufsize: capped_uint(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200279 The initial output buffer size.
280 /
281
282Returns a bytes object containing the uncompressed data.
283[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000284
Guido van Rossumfb221561997-04-29 15:38:09 +0000285static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400286zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits,
287 unsigned int bufsize)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000288/*[clinic end generated code: output=444d0987f3429574 input=75123b0d4ff0541d]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000289{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200290 PyObject *result_str = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000291 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200292 unsigned int length;
293 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200294 unsigned int new_bufsize;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000295 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000296
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200297 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200298 PyErr_SetString(PyExc_OverflowError,
299 "Size does not fit in an unsigned int");
300 goto error;
301 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200302 input = data->buf;
303 length = (unsigned int)data->len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000304
Victor Stinnere079edd2013-11-21 22:33:21 +0100305 if (bufsize == 0)
306 bufsize = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000307
Jeremy Hylton9714f992001-10-16 21:19:45 +0000308 zst.avail_in = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100309 zst.avail_out = bufsize;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000310
Victor Stinnere079edd2013-11-21 22:33:21 +0100311 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200312 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000313
Victor Stinner5064a522013-07-07 16:50:27 +0200314 zst.opaque = NULL;
315 zst.zalloc = PyZlib_Malloc;
316 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000317 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000318 zst.next_in = (Byte *)input;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200319 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000320
Jeremy Hylton9714f992001-10-16 21:19:45 +0000321 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000322 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 break;
Tim Peters977e5402001-10-17 03:57:20 +0000324 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 PyErr_SetString(PyExc_MemoryError,
326 "Out of memory while decompressing data");
327 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000328 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000329 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000330 zlib_error(zst, err, "while preparing to decompress data");
331 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000332 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000333
Jeremy Hylton9714f992001-10-16 21:19:45 +0000334 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 Py_BEGIN_ALLOW_THREADS
336 err=inflate(&zst, Z_FINISH);
337 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000338
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000339 switch(err) {
340 case(Z_STREAM_END):
341 break;
342 case(Z_BUF_ERROR):
343 /*
344 * If there is at least 1 byte of room according to zst.avail_out
345 * and we get this error, assume that it means zlib cannot
346 * process the inflate call() due to an error in the data.
347 */
348 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000349 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000350 inflateEnd(&zst);
351 goto error;
352 }
353 /* fall through */
354 case(Z_OK):
355 /* need more memory */
Victor Stinnere079edd2013-11-21 22:33:21 +0100356 if (bufsize <= (UINT_MAX >> 1))
357 new_bufsize = bufsize << 1;
358 else
359 new_bufsize = UINT_MAX;
360 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000361 inflateEnd(&zst);
362 goto error;
363 }
364 zst.next_out =
Victor Stinnere079edd2013-11-21 22:33:21 +0100365 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
366 zst.avail_out = bufsize;
367 bufsize = new_bufsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000368 break;
369 default:
370 inflateEnd(&zst);
371 zlib_error(zst, err, "while decompressing data");
372 goto error;
373 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000374 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000375
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000376 err = inflateEnd(&zst);
377 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200378 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000379 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000380 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000381
Gregory P. Smith693fc462008-09-06 20:13:06 +0000382 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000383 goto error;
384
Jeremy Hylton9714f992001-10-16 21:19:45 +0000385 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000386
387 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000388 Py_XDECREF(result_str);
389 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000390}
391
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200392/*[clinic input]
393zlib.compressobj
394
395 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000396 The compression level (an integer in the range 0-9 or -1; default is
397 currently equivalent to 6). Higher compression levels are slower,
398 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200399 method: int(c_default="DEFLATED") = DEFLATED
400 The compression algorithm. If given, this must be DEFLATED.
401 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000402 +9 to +15: The base-two logarithm of the window size. Include a zlib
403 container.
404 -9 to -15: Generate a raw stream.
405 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200406 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
407 Controls the amount of memory used for internal compression state.
408 Valid values range from 1 to 9. Higher values result in higher memory
409 usage, faster compression, and smaller output.
410 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
411 Used to tune the compression algorithm. Possible values are
412 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
413 zdict: Py_buffer = None
414 The predefined compression dictionary - a sequence of bytes
415 containing subsequences that are likely to occur in the input data.
416
417Return a compressor object.
418[clinic start generated code]*/
419
Guido van Rossumfb221561997-04-29 15:38:09 +0000420static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400421zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
422 int memLevel, int strategy, Py_buffer *zdict)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000423/*[clinic end generated code: output=2949bbb9a5723ccd input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000424{
Victor Stinnere079edd2013-11-21 22:33:21 +0100425 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200426 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000427
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200428 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100429 PyErr_SetString(PyExc_OverflowError,
430 "zdict length does not fit in an unsigned int");
431 goto error;
432 }
433
Jeremy Hylton499000002001-10-16 21:59:35 +0000434 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000435 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200436 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200437 self->zst.opaque = NULL;
438 self->zst.zalloc = PyZlib_Malloc;
439 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000440 self->zst.next_in = NULL;
441 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000442 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
443 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000444 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000445 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200446 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200447 goto success;
448 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100449 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200450 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200451 switch (err) {
452 case (Z_OK):
453 goto success;
454 case (Z_STREAM_ERROR):
455 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
456 goto error;
457 default:
458 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
459 goto error;
460 }
461 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000462 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 PyErr_SetString(PyExc_MemoryError,
464 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200465 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000466 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000467 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200468 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000469 default:
470 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200471 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000472 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200473
474 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200475 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200476 success:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200477 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000478}
479
Martin Panter3f0ee832016-06-05 10:48:34 +0000480static int
481set_inflate_zdict(compobject *self)
482{
483 Py_buffer zdict_buf;
484 int err;
485
486 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
487 return -1;
488 }
489 if ((size_t)zdict_buf.len > UINT_MAX) {
490 PyErr_SetString(PyExc_OverflowError,
491 "zdict length does not fit in an unsigned int");
492 PyBuffer_Release(&zdict_buf);
493 return -1;
494 }
495 err = inflateSetDictionary(&(self->zst),
496 zdict_buf.buf, (unsigned int)zdict_buf.len);
497 PyBuffer_Release(&zdict_buf);
498 if (err != Z_OK) {
499 zlib_error(self->zst, err, "while setting zdict");
500 return -1;
501 }
502 return 0;
503}
504
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200505/*[clinic input]
506zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200507
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200508 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000509 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200510 zdict: object(c_default="NULL") = b''
511 The predefined compression dictionary. This must be the same
512 dictionary as used by the compressor that produced the input data.
513
514Return a decompressor object.
515[clinic start generated code]*/
516
517static PyObject *
518zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict)
Martin Panter0fdf41d2016-05-27 07:32:11 +0000519/*[clinic end generated code: output=8ccd583fbd631798 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200520{
521 int err;
522 compobject *self;
523
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200524 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
525 PyErr_SetString(PyExc_TypeError,
526 "zdict argument must support the buffer protocol");
527 return NULL;
528 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000529
530 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000531 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000532 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200533 self->zst.opaque = NULL;
534 self->zst.zalloc = PyZlib_Malloc;
535 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000536 self->zst.next_in = NULL;
537 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200538 if (zdict != NULL) {
539 Py_INCREF(zdict);
540 self->zdict = zdict;
541 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000542 err = inflateInit2(&self->zst, wbits);
543 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000544 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000545 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000546 if (self->zdict != NULL && wbits < 0) {
547#ifdef AT_LEAST_ZLIB_1_2_2_1
548 if (set_inflate_zdict(self) < 0) {
549 Py_DECREF(self);
550 return NULL;
551 }
552#else
553 PyErr_Format(ZlibError,
554 "zlib version %s does not allow raw inflate with dictionary",
555 ZLIB_VERSION);
556 Py_DECREF(self);
557 return NULL;
558#endif
559 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000561 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000562 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000563 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
564 return NULL;
565 case (Z_MEM_ERROR):
566 Py_DECREF(self);
567 PyErr_SetString(PyExc_MemoryError,
568 "Can't allocate memory for decompression object");
569 return NULL;
570 default:
571 zlib_error(self->zst, err, "while creating decompression object");
572 Py_DECREF(self);
573 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000574 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000575}
576
577static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000578Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000579{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000580#ifdef WITH_THREAD
581 PyThread_free_lock(self->lock);
582#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000583 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000584 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200585 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000586 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000587}
588
589static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000590Comp_dealloc(compobject *self)
591{
592 if (self->is_initialised)
593 deflateEnd(&self->zst);
594 Dealloc(self);
595}
596
597static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000598Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000599{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000600 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000601 inflateEnd(&self->zst);
602 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000603}
604
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200605/*[clinic input]
606zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000607
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200608 data: Py_buffer
609 Binary data to be compressed.
610 /
611
612Returns a bytes object containing compressed data.
613
614After calling this function, some of the input data may still
615be stored in internal buffers for later processing.
616Call the flush() method to clear these buffers.
617[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000618
Guido van Rossumfb221561997-04-29 15:38:09 +0000619static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200620zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800621/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000622{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200623 int err;
624 unsigned int inplen;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200625 unsigned int length = DEF_BUF_SIZE, new_length;
626 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000627 Byte *input;
628 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000629
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200630 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200631 PyErr_SetString(PyExc_OverflowError,
632 "Size does not fit in an unsigned int");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200633 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200634 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200635 input = data->buf;
636 inplen = (unsigned int)data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000637
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200638 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200639 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000640
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000641 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000642
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643 start_total_out = self->zst.total_out;
644 self->zst.avail_in = inplen;
645 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000646 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000647 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000648
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000649 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000650 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000651 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000652
Jeremy Hylton9714f992001-10-16 21:19:45 +0000653 /* while Z_OK and the output buffer is full, there might be more output,
654 so extend the output buffer and try again */
655 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100656 if (length <= (UINT_MAX >> 1))
657 new_length = length << 1;
658 else
659 new_length = UINT_MAX;
660 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200661 Py_CLEAR(RetVal);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200662 goto done;
Guido van Rossum776152b2007-05-22 22:44:07 +0000663 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000664 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000665 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100667 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000668
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000669 Py_BEGIN_ALLOW_THREADS
670 err = deflate(&(self->zst), Z_NO_FLUSH);
671 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000672 }
Tim Peters977e5402001-10-17 03:57:20 +0000673 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000674 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000675 condition.
676 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000677
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000678 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200679 zlib_error(self->zst, err, "while compressing data");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200680 Py_CLEAR(RetVal);
681 goto done;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000682 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000683 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200684 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000685 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000686
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200687 done:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000688 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000689 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000690}
691
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100692/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
693 self->unused_data or self->unconsumed_tail, as appropriate. */
694static int
695save_unconsumed_input(compobject *self, int err)
696{
697 if (err == Z_STREAM_END) {
698 /* The end of the compressed data has been reached. Store the leftover
699 input data in self->unused_data. */
700 if (self->zst.avail_in > 0) {
701 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
702 Py_ssize_t new_size;
703 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100704 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100705 PyErr_NoMemory();
706 return -1;
707 }
708 new_size = old_size + self->zst.avail_in;
709 new_data = PyBytes_FromStringAndSize(NULL, new_size);
710 if (new_data == NULL)
711 return -1;
712 Py_MEMCPY(PyBytes_AS_STRING(new_data),
713 PyBytes_AS_STRING(self->unused_data), old_size);
714 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
715 self->zst.next_in, self->zst.avail_in);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300716 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100717 self->zst.avail_in = 0;
718 }
719 }
720 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
721 /* This code handles two distinct cases:
722 1. Output limit was reached. Save leftover input in unconsumed_tail.
723 2. All input data was consumed. Clear unconsumed_tail. */
724 PyObject *new_data = PyBytes_FromStringAndSize(
725 (char *)self->zst.next_in, self->zst.avail_in);
726 if (new_data == NULL)
727 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300728 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100729 }
730 return 0;
731}
732
Larry Hastings61272b72014-01-07 12:41:53 -0800733/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800734zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700735
736 data: Py_buffer
737 The binary data to decompress.
Martin Pantere99e9772015-11-20 08:13:35 +0000738 max_length: capped_uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700739 The maximum allowable length of the decompressed data.
740 Unconsumed input data will be stored in
741 the unconsumed_tail attribute.
742 /
743
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200744Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700745
746After calling this function, some of the input data may still be stored in
747internal buffers for later processing.
748Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800749[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700750
Larry Hastings31826802013-10-19 00:09:25 -0700751static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400752zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
753 unsigned int max_length)
Martin Pantere99e9772015-11-20 08:13:35 +0000754/*[clinic end generated code: output=b82e2a2c19f5fe7b input=68b6508ab07c2cf0]*/
Larry Hastings31826802013-10-19 00:09:25 -0700755{
Larry Hastings31826802013-10-19 00:09:25 -0700756 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200757 unsigned int old_length, length = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200758 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000759 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000760
Victor Stinnere079edd2013-11-21 22:33:21 +0100761 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200762 PyErr_SetString(PyExc_OverflowError,
763 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700764 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200765 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000766
Jeremy Hylton9714f992001-10-16 21:19:45 +0000767 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000768 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000769 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200770 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700771 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000772
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800773 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000774
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800775 start_total_out = self->zst.total_out;
776 self->zst.avail_in = (unsigned int)data->len;
777 self->zst.next_in = data->buf;
778 self->zst.avail_out = length;
779 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000780
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000781 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800782 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000783 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000784
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800785 if (err == Z_NEED_DICT && self->zdict != NULL) {
Martin Panter3f0ee832016-06-05 10:48:34 +0000786 if (set_inflate_zdict(self) < 0) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200787 Py_DECREF(RetVal);
788 RetVal = NULL;
789 goto error;
790 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100791
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200792 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200793 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800794 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200795 Py_END_ALLOW_THREADS
796 }
797
Jeremy Hylton9714f992001-10-16 21:19:45 +0000798 /* While Z_OK and the output buffer is full, there might be more output.
799 So extend the output buffer and try again.
800 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800801 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000802 /* If max_length set, don't continue decompressing if we've already
803 reached the limit.
804 */
805 if (max_length && length >= max_length)
806 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000807
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000808 /* otherwise, ... */
809 old_length = length;
810 length = length << 1;
811 if (max_length && length > max_length)
812 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000813
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000814 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200815 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000816 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000817 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800818 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000819 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800820 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000821
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000822 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800823 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000824 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000825 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000826
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800827 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200828 Py_DECREF(RetVal);
829 RetVal = NULL;
830 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000831 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000832
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000833 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100834 /* This is the logical place to call inflateEnd, but the old behaviour
835 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800836 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100837 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 /* We will only get Z_BUF_ERROR if the output buffer was full
839 but there wasn't more output when we tried again, so it is
840 not an error condition.
841 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800842 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000843 Py_DECREF(RetVal);
844 RetVal = NULL;
845 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000846 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000847
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800848 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200849 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000850 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000851
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000852 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800853 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000855}
856
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200857/*[clinic input]
858zlib.Compress.flush
859
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200860 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200861 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
862 If mode == Z_FINISH, the compressor object can no longer be
863 used after calling the flush() method. Otherwise, more data
864 can still be compressed.
865 /
866
867Return a bytes object containing any remaining compressed data.
868[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000869
Guido van Rossumfb221561997-04-29 15:38:09 +0000870static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200871zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200872/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000873{
Victor Stinnere079edd2013-11-21 22:33:21 +0100874 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200875 unsigned int length = DEF_BUF_SIZE, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000876 PyObject *RetVal;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000877 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000878
Jeremy Hylton9714f992001-10-16 21:19:45 +0000879 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
880 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200881 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000882 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000883 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000884
Gregory P. Smith693fc462008-09-06 20:13:06 +0000885 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000887
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000888 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000889
Jeremy Hylton9714f992001-10-16 21:19:45 +0000890 start_total_out = self->zst.total_out;
891 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000892 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000893 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000894
895 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200896 err = deflate(&(self->zst), mode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000897 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000898
Jeremy Hylton9714f992001-10-16 21:19:45 +0000899 /* while Z_OK and the output buffer is full, there might be more output,
900 so extend the output buffer and try again */
901 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100902 if (length <= (UINT_MAX >> 1))
903 new_length = length << 1;
904 else
905 new_length = UINT_MAX;
906 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200907 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000909 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000910 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000911 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000912 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100913 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000914
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000915 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200916 err = deflate(&(self->zst), mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000917 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000918 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000919
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200920 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000921 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200922 mode is Z_FINISH, but checking both for safety*/
923 if (err == Z_STREAM_END && mode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000924 err = deflateEnd(&(self->zst));
925 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200926 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000927 Py_DECREF(RetVal);
928 RetVal = NULL;
929 goto error;
930 }
931 else
932 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000933
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000934 /* We will only get Z_BUF_ERROR if the output buffer was full
935 but there wasn't more output when we tried again, so it is
936 not an error condition.
937 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000938 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000939 zlib_error(self->zst, err, "while flushing");
940 Py_DECREF(RetVal);
941 RetVal = NULL;
942 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000943 }
Tim Peters977e5402001-10-17 03:57:20 +0000944
Gregory P. Smith693fc462008-09-06 20:13:06 +0000945 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200946 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000947 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000948
Tim Peters977e5402001-10-17 03:57:20 +0000949 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000950 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000951
952 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000953}
954
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000955#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700956
Larry Hastings61272b72014-01-07 12:41:53 -0800957/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800958zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700959
960Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800961[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700962
Larry Hastings3cceb382014-01-04 11:09:09 -0800963static PyObject *
964zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800965/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000966{
967 compobject *retval = NULL;
968 int err;
969
970 retval = newcompobject(&Comptype);
971 if (!retval) return NULL;
972
973 /* Copy the zstream state
974 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
975 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800976 ENTER_ZLIB(self);
977 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978 switch(err) {
979 case(Z_OK):
980 break;
981 case(Z_STREAM_ERROR):
982 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
983 goto error;
984 case(Z_MEM_ERROR):
985 PyErr_SetString(PyExc_MemoryError,
986 "Can't allocate memory for compression object");
987 goto error;
988 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800989 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000990 goto error;
991 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800992 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300993 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800994 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300995 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800996 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300997 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800998 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000999
1000 /* Mark it as being initialized */
1001 retval->is_initialised = 1;
1002
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001003 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001004 return (PyObject *)retval;
1005
1006error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001007 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008 Py_XDECREF(retval);
1009 return NULL;
1010}
1011
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001012/*[clinic input]
1013zlib.Decompress.copy
1014
1015Return a copy of the decompression object.
1016[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001017
1018static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001019zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08001020/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001021{
1022 compobject *retval = NULL;
1023 int err;
1024
1025 retval = newcompobject(&Decomptype);
1026 if (!retval) return NULL;
1027
1028 /* Copy the zstream state
1029 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1030 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001031 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032 err = inflateCopy(&retval->zst, &self->zst);
1033 switch(err) {
1034 case(Z_OK):
1035 break;
1036 case(Z_STREAM_ERROR):
1037 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1038 goto error;
1039 case(Z_MEM_ERROR):
1040 PyErr_SetString(PyExc_MemoryError,
1041 "Can't allocate memory for decompression object");
1042 goto error;
1043 default:
1044 zlib_error(self->zst, err, "while copying decompression object");
1045 goto error;
1046 }
1047
1048 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001049 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001051 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001052 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001053 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001054 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055
1056 /* Mark it as being initialized */
1057 retval->is_initialised = 1;
1058
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001059 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001060 return (PyObject *)retval;
1061
1062error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001063 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064 Py_XDECREF(retval);
1065 return NULL;
1066}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001067#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001069/*[clinic input]
1070zlib.Decompress.flush
1071
Martin Pantere99e9772015-11-20 08:13:35 +00001072 length: capped_uint(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001073 the initial size of the output buffer.
1074 /
1075
1076Return a bytes object containing any remaining decompressed data.
1077[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001078
Guido van Rossumfb221561997-04-29 15:38:09 +00001079static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001080zlib_Decompress_flush_impl(compobject *self, unsigned int length)
Martin Pantere99e9772015-11-20 08:13:35 +00001081/*[clinic end generated code: output=db6fb753ab698e22 input=1bb961eb21b62aa0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001082{
Victor Stinnere079edd2013-11-21 22:33:21 +01001083 int err;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001084 unsigned int new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001085 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001086 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001087 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001088
Victor Stinnere079edd2013-11-21 22:33:21 +01001089 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001090 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1091 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001092 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001093
Gregory P. Smith693fc462008-09-06 20:13:06 +00001094 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001095 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001096
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001097
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001098 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001099
Victor Stinnere079edd2013-11-21 22:33:21 +01001100 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1101
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001102 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001103 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1104 or equal than UINT_MAX */
1105 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001106 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001107 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001108 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001109
1110 Py_BEGIN_ALLOW_THREADS
1111 err = inflate(&(self->zst), Z_FINISH);
1112 Py_END_ALLOW_THREADS
1113
1114 /* while Z_OK and the output buffer is full, there might be more output,
1115 so extend the output buffer and try again */
1116 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001117 if (length <= (UINT_MAX >> 1))
1118 new_length = length << 1;
1119 else
1120 new_length = UINT_MAX;
1121 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001122 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001123 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001124 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001125 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1126 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001127 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001128
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001129 Py_BEGIN_ALLOW_THREADS
1130 err = inflate(&(self->zst), Z_FINISH);
1131 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001132 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001133
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001134 if (save_unconsumed_input(self, err) < 0) {
1135 Py_DECREF(retval);
1136 retval = NULL;
1137 goto error;
1138 }
1139
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001140 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001141 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001142 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001143 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001144 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001145 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001146 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001147 Py_DECREF(retval);
1148 retval = NULL;
1149 goto error;
1150 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001151 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001152
Gregory P. Smith693fc462008-09-06 20:13:06 +00001153 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001154 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001155 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001156
1157error:
1158
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001159 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001160
Jeremy Hylton9714f992001-10-16 21:19:45 +00001161 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001162}
1163
Christian Heimes936e2f32014-01-27 01:06:57 +01001164#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001165
Guido van Rossumfb221561997-04-29 15:38:09 +00001166static PyMethodDef comp_methods[] =
1167{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001168 ZLIB_COMPRESS_COMPRESS_METHODDEF
1169 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001170#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001171 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001172#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001173 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001174};
1175
1176static PyMethodDef Decomp_methods[] =
1177{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001178 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001179 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001180#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001181 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001182#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001183 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001184};
1185
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001186#define COMP_OFF(x) offsetof(compobject, x)
1187static PyMemberDef Decomp_members[] = {
1188 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1189 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001190 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001191 {NULL},
1192};
Guido van Rossumfb221561997-04-29 15:38:09 +00001193
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001194/*[clinic input]
1195zlib.adler32
1196
1197 data: Py_buffer
1198 value: unsigned_int(bitwise=True) = 1
1199 Starting value of the checksum.
1200 /
1201
1202Compute an Adler-32 checksum of data.
1203
1204The returned checksum is an integer.
1205[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001206
Guido van Rossumfb221561997-04-29 15:38:09 +00001207static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001208zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001209/*[clinic end generated code: output=51d6d75ee655c78a input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001210{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001211 /* Releasing the GIL for very small buffers is inefficient
1212 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001213 if (data->len > 1024*5) {
1214 unsigned char *buf = data->buf;
1215 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001216
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001217 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001218 /* Avoid truncation of length for very large buffers. adler32() takes
1219 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001220 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001221 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001222 buf += (size_t) UINT_MAX;
1223 len -= (size_t) UINT_MAX;
1224 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001225 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001226 Py_END_ALLOW_THREADS
1227 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001228 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001229 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001230 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001231}
Tim Peters977e5402001-10-17 03:57:20 +00001232
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001233/*[clinic input]
1234zlib.crc32
1235
1236 data: Py_buffer
1237 value: unsigned_int(bitwise=True) = 0
1238 Starting value of the checksum.
1239 /
1240
1241Compute a CRC-32 checksum of data.
1242
1243The returned checksum is an integer.
1244[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001245
1246static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001247zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value)
Larry Hastings581ee362014-01-28 05:00:08 -08001248/*[clinic end generated code: output=c1e986e74fe7b623 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001249{
Martin v. Löwis423be952008-08-13 15:53:07 +00001250 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001251
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001252 /* Releasing the GIL for very small buffers is inefficient
1253 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001254 if (data->len > 1024*5) {
1255 unsigned char *buf = data->buf;
1256 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001257
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001258 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001259 /* Avoid truncation of length for very large buffers. crc32() takes
1260 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001261 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001262 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001263 buf += (size_t) UINT_MAX;
1264 len -= (size_t) UINT_MAX;
1265 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001266 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001267 Py_END_ALLOW_THREADS
1268 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001269 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001270 }
Christian Heimescc47b052008-03-25 14:56:36 +00001271 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001272}
Tim Peters977e5402001-10-17 03:57:20 +00001273
Guido van Rossumfb221561997-04-29 15:38:09 +00001274
1275static PyMethodDef zlib_methods[] =
1276{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001277 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001278 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001279 ZLIB_COMPRESSOBJ_METHODDEF
1280 ZLIB_CRC32_METHODDEF
1281 ZLIB_DECOMPRESS_METHODDEF
1282 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001283 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001284};
1285
Tim Peters0c322792002-07-17 16:49:03 +00001286static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001287 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001288 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001289 sizeof(compobject),
1290 0,
1291 (destructor)Comp_dealloc, /*tp_dealloc*/
1292 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001293 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001294 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001295 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001296 0, /*tp_repr*/
1297 0, /*tp_as_number*/
1298 0, /*tp_as_sequence*/
1299 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001300 0, /*tp_hash*/
1301 0, /*tp_call*/
1302 0, /*tp_str*/
1303 0, /*tp_getattro*/
1304 0, /*tp_setattro*/
1305 0, /*tp_as_buffer*/
1306 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1307 0, /*tp_doc*/
1308 0, /*tp_traverse*/
1309 0, /*tp_clear*/
1310 0, /*tp_richcompare*/
1311 0, /*tp_weaklistoffset*/
1312 0, /*tp_iter*/
1313 0, /*tp_iternext*/
1314 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001315};
1316
Tim Peters0c322792002-07-17 16:49:03 +00001317static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001318 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001319 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001320 sizeof(compobject),
1321 0,
1322 (destructor)Decomp_dealloc, /*tp_dealloc*/
1323 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001324 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001325 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001326 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001327 0, /*tp_repr*/
1328 0, /*tp_as_number*/
1329 0, /*tp_as_sequence*/
1330 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001331 0, /*tp_hash*/
1332 0, /*tp_call*/
1333 0, /*tp_str*/
1334 0, /*tp_getattro*/
1335 0, /*tp_setattro*/
1336 0, /*tp_as_buffer*/
1337 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1338 0, /*tp_doc*/
1339 0, /*tp_traverse*/
1340 0, /*tp_clear*/
1341 0, /*tp_richcompare*/
1342 0, /*tp_weaklistoffset*/
1343 0, /*tp_iter*/
1344 0, /*tp_iternext*/
1345 Decomp_methods, /*tp_methods*/
1346 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001347};
1348
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001349PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001350"The functions in this module allow compression and decompression using the\n"
1351"zlib library, which is based on GNU zip.\n"
1352"\n"
1353"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001354"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001355"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001356"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001357"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001358"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001359"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001360"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001361"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001362"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001363
Martin v. Löwis1a214512008-06-11 05:26:20 +00001364static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001365 PyModuleDef_HEAD_INIT,
1366 "zlib",
1367 zlib_module_documentation,
1368 -1,
1369 zlib_methods,
1370 NULL,
1371 NULL,
1372 NULL,
1373 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001374};
1375
Mark Hammond62b1ab12002-07-23 06:31:15 +00001376PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001377PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001378{
Fred Drake4baedc12002-04-01 14:53:37 +00001379 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001380 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001381 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001382 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001383 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001384 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001385 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001386 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001387
Fred Drake4baedc12002-04-01 14:53:37 +00001388 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1389 if (ZlibError != NULL) {
1390 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001391 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001392 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001393 PyModule_AddIntMacro(m, MAX_WBITS);
1394 PyModule_AddIntMacro(m, DEFLATED);
1395 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001396 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001397 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1398 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1399 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1400 PyModule_AddIntMacro(m, Z_FILTERED);
1401 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1402 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001403
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001404 PyModule_AddIntMacro(m, Z_FINISH);
1405 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1406 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1407 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001408
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001409 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001410 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001411 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001412
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001413 ver = PyUnicode_FromString(zlibVersion());
1414 if (ver != NULL)
1415 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1416
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001417 PyModule_AddStringConstant(m, "__version__", "1.0");
1418
Martin v. Löwis1a214512008-06-11 05:26:20 +00001419 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001420}