blob: cf086de7573b311e3f0240ce4b0b8d68e2fb3af8 [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin Panter84544c12016-07-23 03:02:07 +00002/* See http://zlib.net/ */
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
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020013#include "pythread.h"
14#define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019
Martin Panter3f0ee832016-06-05 10:48:34 +000020#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000021# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000022#endif
23
Guido van Rossumfb221561997-04-29 15:38:09 +000024/* The following parameters are copied from zutil.h, version 0.95 */
25#define DEFLATED 8
26#if MAX_MEM_LEVEL >= 8
27# define DEF_MEM_LEVEL 8
28#else
29# define DEF_MEM_LEVEL MAX_MEM_LEVEL
30#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000031
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020032/* Initial buffer size. */
33#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000034
Jeremy Hylton938ace62002-07-17 16:30:39 +000035static PyTypeObject Comptype;
36static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000037
38static PyObject *ZlibError;
39
Tim Peters977e5402001-10-17 03:57:20 +000040typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000041{
Jeremy Hylton9714f992001-10-16 21:19:45 +000042 PyObject_HEAD
43 z_stream zst;
44 PyObject *unused_data;
45 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020046 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000047 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020048 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020049 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000050} compobject;
51
Jeremy Hylton0965e082001-10-16 21:56:09 +000052static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020053zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000054{
Nadeem Vawda524148a2011-08-28 11:26:46 +020055 const char *zmsg = Z_NULL;
56 /* In case of a version mismatch, zst.msg won't be initialized.
57 Check for this case first, before looking at zst.msg. */
58 if (err == Z_VERSION_ERROR)
59 zmsg = "library version mismatch";
60 if (zmsg == Z_NULL)
61 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000062 if (zmsg == Z_NULL) {
63 switch (err) {
64 case Z_BUF_ERROR:
65 zmsg = "incomplete or truncated stream";
66 break;
67 case Z_STREAM_ERROR:
68 zmsg = "inconsistent stream state";
69 break;
70 case Z_DATA_ERROR:
71 zmsg = "invalid input data";
72 break;
73 }
74 }
75 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000076 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000077 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000078 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000079}
80
Larry Hastings61272b72014-01-07 12:41:53 -080081/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080082module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080083class zlib.Compress "compobject *" "&Comptype"
84class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080085[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030086/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080087
Guido van Rossumfb221561997-04-29 15:38:09 +000088static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000089newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000090{
Tim Peters977e5402001-10-17 03:57:20 +000091 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000092 self = PyObject_New(compobject, type);
93 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000094 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020095 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +000096 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020097 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +000098 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 Py_DECREF(self);
101 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000103 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000105 Py_DECREF(self);
106 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000107 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000108 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200109 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000110 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200111 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
112 return NULL;
113 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000115}
116
Victor Stinner5064a522013-07-07 16:50:27 +0200117static void*
118PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
119{
120 if (items > (size_t)PY_SSIZE_T_MAX / size)
121 return NULL;
122 /* PyMem_Malloc() cannot be used: the GIL is not held when
123 inflate() and deflate() are called */
124 return PyMem_RawMalloc(items * size);
125}
126
127static void
128PyZlib_Free(voidpf ctx, void *ptr)
129{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200130 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200131}
132
Martin Panter84544c12016-07-23 03:02:07 +0000133static void
134arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
135{
Segev Finer679b5662017-07-27 01:17:57 +0300136 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000137 *remains -= zst->avail_in;
138}
139
140static Py_ssize_t
141arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
142 Py_ssize_t length,
143 Py_ssize_t max_length)
144{
145 Py_ssize_t occupied;
146
147 if (*buffer == NULL) {
148 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
149 return -1;
150 occupied = 0;
151 }
152 else {
153 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
154
155 if (length == occupied) {
156 Py_ssize_t new_length;
157 assert(length <= max_length);
158 /* can not scale the buffer over max_length */
159 if (length == max_length)
160 return -2;
161 if (length <= (max_length >> 1))
162 new_length = length << 1;
163 else
164 new_length = max_length;
165 if (_PyBytes_Resize(buffer, new_length) < 0)
166 return -1;
167 length = new_length;
168 }
169 }
170
Segev Finer679b5662017-07-27 01:17:57 +0300171 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000172 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
173
174 return length;
175}
176
177static Py_ssize_t
178arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
179{
180 Py_ssize_t ret;
181
182 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
183 PY_SSIZE_T_MAX);
184 if (ret == -2)
185 PyErr_NoMemory();
186
187 return ret;
188}
189
Larry Hastings61272b72014-01-07 12:41:53 -0800190/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800191zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200192
Martin Panter1fe0d132016-02-10 10:06:36 +0000193 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800194 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300195 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200196 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000197 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800198
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200199Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800200[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800201
Guido van Rossumfb221561997-04-29 15:38:09 +0000202static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300203zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
204/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000205{
Martin Panter84544c12016-07-23 03:02:07 +0000206 PyObject *RetVal = NULL;
207 Byte *ibuf;
208 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
209 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000210 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000211
Martin Panter525a9492016-07-23 03:39:49 +0000212 ibuf = data->buf;
213 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000214
Victor Stinner5064a522013-07-07 16:50:27 +0200215 zst.opaque = NULL;
216 zst.zalloc = PyZlib_Malloc;
217 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000218 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000219 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000220
Martin Panter84544c12016-07-23 03:02:07 +0000221 switch (err) {
222 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000223 break;
Martin Panter84544c12016-07-23 03:02:07 +0000224 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000225 PyErr_SetString(PyExc_MemoryError,
226 "Out of memory while compressing data");
227 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000228 case Z_STREAM_ERROR:
229 PyErr_SetString(ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000230 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000231 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000232 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000233 zlib_error(zst, err, "while compressing data");
234 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000235 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000236
Martin Panter84544c12016-07-23 03:02:07 +0000237 do {
238 arrange_input_buffer(&zst, &ibuflen);
239 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000240
Martin Panter84544c12016-07-23 03:02:07 +0000241 do {
242 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
243 if (obuflen < 0) {
244 deflateEnd(&zst);
245 goto error;
246 }
247
248 Py_BEGIN_ALLOW_THREADS
249 err = deflate(&zst, flush);
250 Py_END_ALLOW_THREADS
251
252 if (err == Z_STREAM_ERROR) {
253 deflateEnd(&zst);
254 zlib_error(zst, err, "while compressing data");
255 goto error;
256 }
257
258 } while (zst.avail_out == 0);
259 assert(zst.avail_in == 0);
260
261 } while (flush != Z_FINISH);
262 assert(err == Z_STREAM_END);
263
264 err = deflateEnd(&zst);
265 if (err == Z_OK) {
266 if (_PyBytes_Resize(&RetVal, zst.next_out -
267 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
268 goto error;
269 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000270 }
Tim Peters977e5402001-10-17 03:57:20 +0000271 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000273 error:
Martin Panter84544c12016-07-23 03:02:07 +0000274 Py_XDECREF(RetVal);
275 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000276}
277
Larry Hastings61272b72014-01-07 12:41:53 -0800278/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100279
Martin Panter84544c12016-07-23 03:02:07 +0000280class ssize_t_converter(CConverter):
281 type = 'Py_ssize_t'
282 converter = 'ssize_t_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200283 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100284
Larry Hastings61272b72014-01-07 12:41:53 -0800285[python start generated code]*/
Martin Panter84544c12016-07-23 03:02:07 +0000286/*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100287
288static int
Martin Panter84544c12016-07-23 03:02:07 +0000289ssize_t_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100290{
Martin Pantere99e9772015-11-20 08:13:35 +0000291 PyObject *long_obj;
292 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100293
Martin Pantere99e9772015-11-20 08:13:35 +0000294 long_obj = (PyObject *)_PyLong_FromNbInt(obj);
295 if (long_obj == NULL) {
296 return 0;
297 }
298 val = PyLong_AsSsize_t(long_obj);
299 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100300 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000301 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100302 }
Martin Panter84544c12016-07-23 03:02:07 +0000303 *(Py_ssize_t *)ptr = val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100304 return 1;
305}
306
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200307/*[clinic input]
308zlib.decompress
309
310 data: Py_buffer
311 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300312 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200313 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000314 The window buffer size and container format.
Martin Panter84544c12016-07-23 03:02:07 +0000315 bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200316 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200317
318Returns a bytes object containing the uncompressed data.
319[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000320
Guido van Rossumfb221561997-04-29 15:38:09 +0000321static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300322zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000323 Py_ssize_t bufsize)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300324/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000325{
Martin Panter84544c12016-07-23 03:02:07 +0000326 PyObject *RetVal = NULL;
327 Byte *ibuf;
328 Py_ssize_t ibuflen;
329 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000330 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000331
Martin Panter84544c12016-07-23 03:02:07 +0000332 if (bufsize < 0) {
333 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
334 return NULL;
335 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100336 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000337 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000338
Martin Panter84544c12016-07-23 03:02:07 +0000339 ibuf = data->buf;
340 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000341
Victor Stinner5064a522013-07-07 16:50:27 +0200342 zst.opaque = NULL;
343 zst.zalloc = PyZlib_Malloc;
344 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000345 zst.avail_in = 0;
346 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200347 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000348
Martin Panter84544c12016-07-23 03:02:07 +0000349 switch (err) {
350 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 break;
Martin Panter84544c12016-07-23 03:02:07 +0000352 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000353 PyErr_SetString(PyExc_MemoryError,
354 "Out of memory while decompressing data");
355 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000356 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000357 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000358 zlib_error(zst, err, "while preparing to decompress data");
359 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000360 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000361
Jeremy Hylton9714f992001-10-16 21:19:45 +0000362 do {
Martin Panter84544c12016-07-23 03:02:07 +0000363 arrange_input_buffer(&zst, &ibuflen);
364 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000365
Martin Panter84544c12016-07-23 03:02:07 +0000366 do {
367 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
368 if (bufsize < 0) {
369 inflateEnd(&zst);
370 goto error;
371 }
372
373 Py_BEGIN_ALLOW_THREADS
374 err = inflate(&zst, flush);
375 Py_END_ALLOW_THREADS
376
377 switch (err) {
378 case Z_OK: /* fall through */
379 case Z_BUF_ERROR: /* fall through */
380 case Z_STREAM_END:
381 break;
382 case Z_MEM_ERROR:
383 inflateEnd(&zst);
384 PyErr_SetString(PyExc_MemoryError,
385 "Out of memory while decompressing data");
386 goto error;
387 default:
388 inflateEnd(&zst);
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000389 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000390 goto error;
391 }
Martin Panter84544c12016-07-23 03:02:07 +0000392
393 } while (zst.avail_out == 0);
394
395 } while (err != Z_STREAM_END && ibuflen != 0);
396
397
398 if (err != Z_STREAM_END) {
399 inflateEnd(&zst);
400 zlib_error(zst, err, "while decompressing data");
401 goto error;
402 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000403
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000404 err = inflateEnd(&zst);
405 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200406 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000407 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000408 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000409
Martin Panter84544c12016-07-23 03:02:07 +0000410 if (_PyBytes_Resize(&RetVal, zst.next_out -
411 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000412 goto error;
413
Martin Panter84544c12016-07-23 03:02:07 +0000414 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000415
416 error:
Martin Panter84544c12016-07-23 03:02:07 +0000417 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000418 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000419}
420
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200421/*[clinic input]
422zlib.compressobj
423
424 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000425 The compression level (an integer in the range 0-9 or -1; default is
426 currently equivalent to 6). Higher compression levels are slower,
427 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200428 method: int(c_default="DEFLATED") = DEFLATED
429 The compression algorithm. If given, this must be DEFLATED.
430 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000431 +9 to +15: The base-two logarithm of the window size. Include a zlib
432 container.
433 -9 to -15: Generate a raw stream.
434 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200435 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
436 Controls the amount of memory used for internal compression state.
437 Valid values range from 1 to 9. Higher values result in higher memory
438 usage, faster compression, and smaller output.
439 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
440 Used to tune the compression algorithm. Possible values are
441 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
442 zdict: Py_buffer = None
443 The predefined compression dictionary - a sequence of bytes
444 containing subsequences that are likely to occur in the input data.
445
446Return a compressor object.
447[clinic start generated code]*/
448
Guido van Rossumfb221561997-04-29 15:38:09 +0000449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300450zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400451 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300452/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000453{
Victor Stinnere079edd2013-11-21 22:33:21 +0100454 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200455 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000456
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200457 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100458 PyErr_SetString(PyExc_OverflowError,
459 "zdict length does not fit in an unsigned int");
460 goto error;
461 }
462
Jeremy Hylton499000002001-10-16 21:59:35 +0000463 self = newcompobject(&Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000464 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200465 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200466 self->zst.opaque = NULL;
467 self->zst.zalloc = PyZlib_Malloc;
468 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000469 self->zst.next_in = NULL;
470 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000471 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000472 switch (err) {
473 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000474 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200475 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200476 goto success;
477 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100478 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200479 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200480 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000481 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200482 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000483 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
485 goto error;
486 default:
487 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
488 goto error;
489 }
490 }
Martin Panter84544c12016-07-23 03:02:07 +0000491 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000492 PyErr_SetString(PyExc_MemoryError,
493 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200494 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000495 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000496 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200497 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000498 default:
499 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200500 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000501 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200502
503 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200504 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200505 success:
Martin Panter84544c12016-07-23 03:02:07 +0000506 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000507}
508
Martin Panter3f0ee832016-06-05 10:48:34 +0000509static int
510set_inflate_zdict(compobject *self)
511{
512 Py_buffer zdict_buf;
513 int err;
514
515 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
516 return -1;
517 }
518 if ((size_t)zdict_buf.len > UINT_MAX) {
519 PyErr_SetString(PyExc_OverflowError,
520 "zdict length does not fit in an unsigned int");
521 PyBuffer_Release(&zdict_buf);
522 return -1;
523 }
Martin Panter84544c12016-07-23 03:02:07 +0000524 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000525 zdict_buf.buf, (unsigned int)zdict_buf.len);
526 PyBuffer_Release(&zdict_buf);
527 if (err != Z_OK) {
528 zlib_error(self->zst, err, "while setting zdict");
529 return -1;
530 }
531 return 0;
532}
533
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200534/*[clinic input]
535zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200536
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200537 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000538 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200539 zdict: object(c_default="NULL") = b''
540 The predefined compression dictionary. This must be the same
541 dictionary as used by the compressor that produced the input data.
542
543Return a decompressor object.
544[clinic start generated code]*/
545
546static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300547zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
548/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200549{
550 int err;
551 compobject *self;
552
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200553 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
554 PyErr_SetString(PyExc_TypeError,
555 "zdict argument must support the buffer protocol");
556 return NULL;
557 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000558
559 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000560 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000561 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200562 self->zst.opaque = NULL;
563 self->zst.zalloc = PyZlib_Malloc;
564 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000565 self->zst.next_in = NULL;
566 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200567 if (zdict != NULL) {
568 Py_INCREF(zdict);
569 self->zdict = zdict;
570 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000571 err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000572 switch (err) {
573 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000574 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000575 if (self->zdict != NULL && wbits < 0) {
576#ifdef AT_LEAST_ZLIB_1_2_2_1
577 if (set_inflate_zdict(self) < 0) {
578 Py_DECREF(self);
579 return NULL;
580 }
581#else
582 PyErr_Format(ZlibError,
583 "zlib version %s does not allow raw inflate with dictionary",
584 ZLIB_VERSION);
585 Py_DECREF(self);
586 return NULL;
587#endif
588 }
Martin Panter84544c12016-07-23 03:02:07 +0000589 return (PyObject *)self;
590 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000591 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000592 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
593 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000594 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 Py_DECREF(self);
596 PyErr_SetString(PyExc_MemoryError,
597 "Can't allocate memory for decompression object");
598 return NULL;
599 default:
600 zlib_error(self->zst, err, "while creating decompression object");
601 Py_DECREF(self);
602 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000603 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000604}
605
606static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000607Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000608{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000609 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000610 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000611 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200612 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000613 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000614}
615
616static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000617Comp_dealloc(compobject *self)
618{
619 if (self->is_initialised)
620 deflateEnd(&self->zst);
621 Dealloc(self);
622}
623
624static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000625Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000626{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000627 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000628 inflateEnd(&self->zst);
629 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000630}
631
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200632/*[clinic input]
633zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000634
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200635 data: Py_buffer
636 Binary data to be compressed.
637 /
638
639Returns a bytes object containing compressed data.
640
641After calling this function, some of the input data may still
642be stored in internal buffers for later processing.
643Call the flush() method to clear these buffers.
644[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000645
Guido van Rossumfb221561997-04-29 15:38:09 +0000646static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200647zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800648/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000649{
Martin Panter84544c12016-07-23 03:02:07 +0000650 PyObject *RetVal = NULL;
651 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200652 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000653
Martin Panter84544c12016-07-23 03:02:07 +0000654 self->zst.next_in = data->buf;
655 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000656
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000657 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000658
Martin Panter84544c12016-07-23 03:02:07 +0000659 do {
660 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000661
Martin Panter84544c12016-07-23 03:02:07 +0000662 do {
663 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
664 if (obuflen < 0)
665 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000666
Martin Panter84544c12016-07-23 03:02:07 +0000667 Py_BEGIN_ALLOW_THREADS
668 err = deflate(&self->zst, Z_NO_FLUSH);
669 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000670
Martin Panter84544c12016-07-23 03:02:07 +0000671 if (err == Z_STREAM_ERROR) {
672 zlib_error(self->zst, err, "while compressing data");
673 goto error;
674 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000675
Martin Panter84544c12016-07-23 03:02:07 +0000676 } while (self->zst.avail_out == 0);
677 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000678
Martin Panter84544c12016-07-23 03:02:07 +0000679 } while (ibuflen != 0);
680
681 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
682 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
683 goto success;
684
685 error:
686 Py_CLEAR(RetVal);
687 success:
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
Martin Panter84544c12016-07-23 03:02:07 +0000692/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100693 self->unused_data or self->unconsumed_tail, as appropriate. */
694static int
Martin Panter84544c12016-07-23 03:02:07 +0000695save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100696{
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);
Martin Panter84544c12016-07-23 03:02:07 +0000702 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100703 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000704 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
705 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100706 PyErr_NoMemory();
707 return -1;
708 }
Martin Panter84544c12016-07-23 03:02:07 +0000709 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100710 new_data = PyBytes_FromStringAndSize(NULL, new_size);
711 if (new_data == NULL)
712 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200713 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100714 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200715 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000716 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300717 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100718 self->zst.avail_in = 0;
719 }
720 }
Martin Panter84544c12016-07-23 03:02:07 +0000721
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100722 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
723 /* This code handles two distinct cases:
724 1. Output limit was reached. Save leftover input in unconsumed_tail.
725 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000726 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100727 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000728 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100729 if (new_data == NULL)
730 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300731 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100732 }
Martin Panter84544c12016-07-23 03:02:07 +0000733
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100734 return 0;
735}
736
Larry Hastings61272b72014-01-07 12:41:53 -0800737/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800738zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700739
740 data: Py_buffer
741 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300742 /
Martin Panter84544c12016-07-23 03:02:07 +0000743 max_length: ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700744 The maximum allowable length of the decompressed data.
745 Unconsumed input data will be stored in
746 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700747
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200748Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700749
750After calling this function, some of the input data may still be stored in
751internal buffers for later processing.
752Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800753[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700754
Larry Hastings31826802013-10-19 00:09:25 -0700755static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400756zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000757 Py_ssize_t max_length)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300758/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
Larry Hastings31826802013-10-19 00:09:25 -0700759{
Martin Panter84544c12016-07-23 03:02:07 +0000760 int err = Z_OK;
761 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200762 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000763
Martin Panter84544c12016-07-23 03:02:07 +0000764 if (max_length < 0) {
765 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700766 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000767 } else if (max_length == 0)
768 hard_limit = PY_SSIZE_T_MAX;
769 else
770 hard_limit = max_length;
771
772 self->zst.next_in = data->buf;
773 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000774
Jeremy Hylton9714f992001-10-16 21:19:45 +0000775 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000776 if (max_length && obuflen > max_length)
777 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000778
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800779 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000780
Martin Panter84544c12016-07-23 03:02:07 +0000781 do {
782 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000783
Martin Panter84544c12016-07-23 03:02:07 +0000784 do {
785 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
786 obuflen, hard_limit);
787 if (obuflen == -2) {
788 if (max_length > 0) {
789 goto save;
790 }
791 PyErr_NoMemory();
792 }
793 if (obuflen < 0) {
794 goto abort;
795 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000796
Martin Panter84544c12016-07-23 03:02:07 +0000797 Py_BEGIN_ALLOW_THREADS
798 err = inflate(&self->zst, Z_SYNC_FLUSH);
799 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100800
Martin Panter84544c12016-07-23 03:02:07 +0000801 switch (err) {
802 case Z_OK: /* fall through */
803 case Z_BUF_ERROR: /* fall through */
804 case Z_STREAM_END:
805 break;
806 default:
807 if (err == Z_NEED_DICT && self->zdict != NULL) {
808 if (set_inflate_zdict(self) < 0)
809 goto abort;
810 else
811 break;
812 }
813 goto save;
814 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200815
Martin Panter84544c12016-07-23 03:02:07 +0000816 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000817
Martin Panter84544c12016-07-23 03:02:07 +0000818 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000819
Martin Panter84544c12016-07-23 03:02:07 +0000820 save:
821 if (save_unconsumed_input(self, data, err) < 0)
822 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000823
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000824 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100825 /* This is the logical place to call inflateEnd, but the old behaviour
826 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800827 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100828 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000829 /* We will only get Z_BUF_ERROR if the output buffer was full
830 but there wasn't more output when we tried again, so it is
831 not an error condition.
832 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800833 zlib_error(self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000834 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000835 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000836
Martin Panter84544c12016-07-23 03:02:07 +0000837 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
838 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
839 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000840
Martin Panter84544c12016-07-23 03:02:07 +0000841 abort:
842 Py_CLEAR(RetVal);
843 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800844 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000845 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000846}
847
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200848/*[clinic input]
849zlib.Compress.flush
850
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200851 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200852 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
853 If mode == Z_FINISH, the compressor object can no longer be
854 used after calling the flush() method. Otherwise, more data
855 can still be compressed.
856 /
857
858Return a bytes object containing any remaining compressed data.
859[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000860
Guido van Rossumfb221561997-04-29 15:38:09 +0000861static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200862zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200863/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000864{
Victor Stinnere079edd2013-11-21 22:33:21 +0100865 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000866 Py_ssize_t length = DEF_BUF_SIZE;
867 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000868
Jeremy Hylton9714f992001-10-16 21:19:45 +0000869 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
870 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200871 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000872 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000873 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000874
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000875 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000876
Jeremy Hylton9714f992001-10-16 21:19:45 +0000877 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000878
Martin Panter84544c12016-07-23 03:02:07 +0000879 do {
880 length = arrange_output_buffer(&self->zst, &RetVal, length);
881 if (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 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000885
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000887 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000889
890 if (err == Z_STREAM_ERROR) {
891 zlib_error(self->zst, err, "while flushing");
892 Py_CLEAR(RetVal);
893 goto error;
894 }
895 } while (self->zst.avail_out == 0);
896 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000897
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200898 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000899 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200900 mode is Z_FINISH, but checking both for safety*/
901 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000902 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000903 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200904 zlib_error(self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000905 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000906 goto error;
907 }
908 else
909 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000910
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000911 /* We will only get Z_BUF_ERROR if the output buffer was full
912 but there wasn't more output when we tried again, so it is
913 not an error condition.
914 */
Martin Panter84544c12016-07-23 03:02:07 +0000915 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000916 zlib_error(self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000917 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000918 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000919 }
Tim Peters977e5402001-10-17 03:57:20 +0000920
Martin Panter84544c12016-07-23 03:02:07 +0000921 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
922 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200923 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000924
Tim Peters977e5402001-10-17 03:57:20 +0000925 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000926 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000927 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);
Martin Panter84544c12016-07-23 03:02:07 +0000953 switch (err) {
954 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955 break;
Martin Panter84544c12016-07-23 03:02:07 +0000956 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
958 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000959 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960 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);
Martin Panter84544c12016-07-23 03:02:07 +00001008 switch (err) {
1009 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010 break;
Martin Panter84544c12016-07-23 03:02:07 +00001011 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001012 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1013 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001014 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001015 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 Panter84544c12016-07-23 03:02:07 +00001047 length: ssize_t(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 *
Martin Panter84544c12016-07-23 03:02:07 +00001055zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1056/*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001057{
Martin Panter84544c12016-07-23 03:02:07 +00001058 int err, flush;
1059 Py_buffer data;
1060 PyObject *RetVal = NULL;
1061 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001062
Martin Panter84544c12016-07-23 03:02:07 +00001063 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001064 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1065 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001066 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001067
Martin Panter84544c12016-07-23 03:02:07 +00001068 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001069 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001070
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001071 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001072
Martin Panter84544c12016-07-23 03:02:07 +00001073 self->zst.next_in = data.buf;
1074 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001075
Martin Panter84544c12016-07-23 03:02:07 +00001076 do {
1077 arrange_input_buffer(&self->zst, &ibuflen);
1078 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001079
Martin Panter84544c12016-07-23 03:02:07 +00001080 do {
1081 length = arrange_output_buffer(&self->zst, &RetVal, length);
1082 if (length < 0)
1083 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001084
Martin Panter84544c12016-07-23 03:02:07 +00001085 Py_BEGIN_ALLOW_THREADS
1086 err = inflate(&self->zst, flush);
1087 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001088
Martin Panter84544c12016-07-23 03:02:07 +00001089 switch (err) {
1090 case Z_OK: /* fall through */
1091 case Z_BUF_ERROR: /* fall through */
1092 case Z_STREAM_END:
1093 break;
1094 default:
1095 if (err == Z_NEED_DICT && self->zdict != NULL) {
1096 if (set_inflate_zdict(self) < 0)
1097 goto abort;
1098 else
1099 break;
1100 }
1101 goto save;
1102 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001103
Martin Panter84544c12016-07-23 03:02:07 +00001104 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1105
1106 } while (err != Z_STREAM_END && ibuflen != 0);
1107
1108 save:
1109 if (save_unconsumed_input(self, &data, err) < 0)
1110 goto abort;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001111
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001112 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001113 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001114 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001115 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001116 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001117 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001118 zlib_error(self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001119 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001120 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001121 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001122
Martin Panter84544c12016-07-23 03:02:07 +00001123 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1124 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1125 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001126
Martin Panter84544c12016-07-23 03:02:07 +00001127 abort:
1128 Py_CLEAR(RetVal);
1129 success:
1130 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001131 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001132 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001133}
1134
Christian Heimes936e2f32014-01-27 01:06:57 +01001135#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001136
Guido van Rossumfb221561997-04-29 15:38:09 +00001137static PyMethodDef comp_methods[] =
1138{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001139 ZLIB_COMPRESS_COMPRESS_METHODDEF
1140 ZLIB_COMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001141#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001142 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001143#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001144 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001145};
1146
1147static PyMethodDef Decomp_methods[] =
1148{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001149 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001150 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001151#ifdef HAVE_ZLIB_COPY
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001152 ZLIB_DECOMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001153#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001154 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001155};
1156
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001157#define COMP_OFF(x) offsetof(compobject, x)
1158static PyMemberDef Decomp_members[] = {
1159 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1160 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001161 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001162 {NULL},
1163};
Guido van Rossumfb221561997-04-29 15:38:09 +00001164
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001165/*[clinic input]
1166zlib.adler32
1167
1168 data: Py_buffer
1169 value: unsigned_int(bitwise=True) = 1
1170 Starting value of the checksum.
1171 /
1172
1173Compute an Adler-32 checksum of data.
1174
1175The returned checksum is an integer.
1176[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001177
Guido van Rossumfb221561997-04-29 15:38:09 +00001178static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001179zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1180/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001181{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001182 /* Releasing the GIL for very small buffers is inefficient
1183 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001184 if (data->len > 1024*5) {
1185 unsigned char *buf = data->buf;
1186 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001187
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001188 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001189 /* Avoid truncation of length for very large buffers. adler32() takes
1190 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001191 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001192 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001193 buf += (size_t) UINT_MAX;
1194 len -= (size_t) UINT_MAX;
1195 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001196 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001197 Py_END_ALLOW_THREADS
1198 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001199 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001200 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001201 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001202}
Tim Peters977e5402001-10-17 03:57:20 +00001203
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001204/*[clinic input]
1205zlib.crc32
1206
1207 data: Py_buffer
1208 value: unsigned_int(bitwise=True) = 0
1209 Starting value of the checksum.
1210 /
1211
1212Compute a CRC-32 checksum of data.
1213
1214The returned checksum is an integer.
1215[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001216
1217static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001218zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1219/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001220{
Martin v. Löwis423be952008-08-13 15:53:07 +00001221 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001222
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001223 /* Releasing the GIL for very small buffers is inefficient
1224 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001225 if (data->len > 1024*5) {
1226 unsigned char *buf = data->buf;
1227 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001228
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001229 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001230 /* Avoid truncation of length for very large buffers. crc32() takes
1231 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001232 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001233 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001234 buf += (size_t) UINT_MAX;
1235 len -= (size_t) UINT_MAX;
1236 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001237 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001238 Py_END_ALLOW_THREADS
1239 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001240 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001241 }
Christian Heimescc47b052008-03-25 14:56:36 +00001242 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001243}
Tim Peters977e5402001-10-17 03:57:20 +00001244
Guido van Rossumfb221561997-04-29 15:38:09 +00001245
1246static PyMethodDef zlib_methods[] =
1247{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001248 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001249 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001250 ZLIB_COMPRESSOBJ_METHODDEF
1251 ZLIB_CRC32_METHODDEF
1252 ZLIB_DECOMPRESS_METHODDEF
1253 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001254 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001255};
1256
Tim Peters0c322792002-07-17 16:49:03 +00001257static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001258 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001259 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001260 sizeof(compobject),
1261 0,
1262 (destructor)Comp_dealloc, /*tp_dealloc*/
1263 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001264 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001265 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001266 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001267 0, /*tp_repr*/
1268 0, /*tp_as_number*/
1269 0, /*tp_as_sequence*/
1270 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001271 0, /*tp_hash*/
1272 0, /*tp_call*/
1273 0, /*tp_str*/
1274 0, /*tp_getattro*/
1275 0, /*tp_setattro*/
1276 0, /*tp_as_buffer*/
1277 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1278 0, /*tp_doc*/
1279 0, /*tp_traverse*/
1280 0, /*tp_clear*/
1281 0, /*tp_richcompare*/
1282 0, /*tp_weaklistoffset*/
1283 0, /*tp_iter*/
1284 0, /*tp_iternext*/
1285 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001286};
1287
Tim Peters0c322792002-07-17 16:49:03 +00001288static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001289 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001290 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001291 sizeof(compobject),
1292 0,
1293 (destructor)Decomp_dealloc, /*tp_dealloc*/
1294 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001295 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001296 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001297 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001298 0, /*tp_repr*/
1299 0, /*tp_as_number*/
1300 0, /*tp_as_sequence*/
1301 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001302 0, /*tp_hash*/
1303 0, /*tp_call*/
1304 0, /*tp_str*/
1305 0, /*tp_getattro*/
1306 0, /*tp_setattro*/
1307 0, /*tp_as_buffer*/
1308 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1309 0, /*tp_doc*/
1310 0, /*tp_traverse*/
1311 0, /*tp_clear*/
1312 0, /*tp_richcompare*/
1313 0, /*tp_weaklistoffset*/
1314 0, /*tp_iter*/
1315 0, /*tp_iternext*/
1316 Decomp_methods, /*tp_methods*/
1317 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001318};
1319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001320PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001321"The functions in this module allow compression and decompression using the\n"
1322"zlib library, which is based on GNU zip.\n"
1323"\n"
1324"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001325"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001326"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001327"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001328"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001329"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001330"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001331"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001332"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001333"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001334
Martin v. Löwis1a214512008-06-11 05:26:20 +00001335static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001336 PyModuleDef_HEAD_INIT,
1337 "zlib",
1338 zlib_module_documentation,
1339 -1,
1340 zlib_methods,
1341 NULL,
1342 NULL,
1343 NULL,
1344 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001345};
1346
Mark Hammond62b1ab12002-07-23 06:31:15 +00001347PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001348PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001349{
Fred Drake4baedc12002-04-01 14:53:37 +00001350 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001351 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001352 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001353 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001354 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001355 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001356 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001357 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001358
Fred Drake4baedc12002-04-01 14:53:37 +00001359 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1360 if (ZlibError != NULL) {
1361 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001362 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001363 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001364 PyModule_AddIntMacro(m, MAX_WBITS);
1365 PyModule_AddIntMacro(m, DEFLATED);
1366 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001367 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001368 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1369 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1370 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1371 PyModule_AddIntMacro(m, Z_FILTERED);
1372 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1373 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001374
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001375 PyModule_AddIntMacro(m, Z_FINISH);
1376 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1377 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1378 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001379
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001380 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001381 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001382 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001383
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001384 ver = PyUnicode_FromString(zlibVersion());
1385 if (ver != NULL)
1386 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1387
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001388 PyModule_AddStringConstant(m, "__version__", "1.0");
1389
Martin v. Löwis1a214512008-06-11 05:26:20 +00001390 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001391}