blob: bf8c8e4d1212a604c699c6fa346912489b792ce6 [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Guido van Rossumfb221561997-04-29 15:38:09 +00006
Guido van Rossum97b54571997-06-03 22:21:47 +00007#include "Python.h"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00008#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +00009#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000012#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000013 #include "pythread.h"
14 #define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000020 #define ENTER_ZLIB(obj)
21 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000022#endif
23
Guido van Rossumfb221561997-04-29 15:38:09 +000024/* The following parameters are copied from zutil.h, version 0.95 */
25#define DEFLATED 8
26#if MAX_MEM_LEVEL >= 8
27# define DEF_MEM_LEVEL 8
28#else
29# define DEF_MEM_LEVEL MAX_MEM_LEVEL
30#endif
31#define DEF_WBITS MAX_WBITS
32
Guido van Rossumb729a1d1999-04-07 20:23:17 +000033/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
34#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Jeremy Hylton938ace62002-07-17 16:30:39 +000036static PyTypeObject Comptype;
37static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000038
39static PyObject *ZlibError;
40
Tim Peters977e5402001-10-17 03:57:20 +000041typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000042{
Jeremy Hylton9714f992001-10-16 21:19:45 +000043 PyObject_HEAD
44 z_stream zst;
45 PyObject *unused_data;
46 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020047 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000048 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020049 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000050 #ifdef WITH_THREAD
51 PyThread_type_lock lock;
52 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000053} compobject;
54
Jeremy Hylton0965e082001-10-16 21:56:09 +000055static void
56zlib_error(z_stream zst, int err, char *msg)
57{
Nadeem Vawda524148a2011-08-28 11:26:46 +020058 const char *zmsg = Z_NULL;
59 /* In case of a version mismatch, zst.msg won't be initialized.
60 Check for this case first, before looking at zst.msg. */
61 if (err == Z_VERSION_ERROR)
62 zmsg = "library version mismatch";
63 if (zmsg == Z_NULL)
64 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000065 if (zmsg == Z_NULL) {
66 switch (err) {
67 case Z_BUF_ERROR:
68 zmsg = "incomplete or truncated stream";
69 break;
70 case Z_STREAM_ERROR:
71 zmsg = "inconsistent stream state";
72 break;
73 case Z_DATA_ERROR:
74 zmsg = "invalid input data";
75 break;
76 }
77 }
78 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000080 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000081 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000082}
83
Larry Hastingsebdcb502013-11-23 14:54:00 -080084/*[clinic]
85module zlib
86class zlib.Compress
87class zlib.Decompress
88[clinic]*/
89/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
90
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091PyDoc_STRVAR(compressobj__doc__,
Nadeem Vawda2180c972012-06-22 01:40:49 +020092"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
93" strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020094" -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +000095"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020096"level is the compression level (an integer in the range 0-9; default is 6).\n"
97"Higher compression levels are slower, but produce smaller results.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020098"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020099"method is the compression algorithm. If given, this must be DEFLATED.\n"
100"\n"
101"wbits is the base two logarithm of the window size (range: 8..15).\n"
102"\n"
103"memlevel controls the amount of memory used for internal compression state.\n"
104"Valid values range from 1 to 9. Higher values result in higher memory usage,\n"
105"faster compression, and smaller output.\n"
106"\n"
107"strategy is used to tune the compression algorithm. Possible values are\n"
108"Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
109"\n"
110"zdict is the predefined compression dictionary - a sequence of bytes\n"
111"containing subsequences that are likely to occur in the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000112
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000113PyDoc_STRVAR(decompressobj__doc__,
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200114"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000115"\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200116"Optional arg wbits is the window buffer size.\n"
117"\n"
118"Optional arg zdict is the predefined compression dictionary. This must be\n"
119"the same dictionary as used by the compressor that produced the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000120
Guido van Rossumfb221561997-04-29 15:38:09 +0000121static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000122newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000123{
Tim Peters977e5402001-10-17 03:57:20 +0000124 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 self = PyObject_New(compobject, type);
126 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000127 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200128 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200130 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000131 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000132 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000133 Py_DECREF(self);
134 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000135 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000136 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000137 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000138 Py_DECREF(self);
139 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000140 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000141#ifdef WITH_THREAD
142 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200143 if (self->lock == NULL) {
144 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
145 return NULL;
146 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000147#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000148 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000149}
150
Victor Stinner5064a522013-07-07 16:50:27 +0200151static void*
152PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
153{
154 if (items > (size_t)PY_SSIZE_T_MAX / size)
155 return NULL;
156 /* PyMem_Malloc() cannot be used: the GIL is not held when
157 inflate() and deflate() are called */
158 return PyMem_RawMalloc(items * size);
159}
160
161static void
162PyZlib_Free(voidpf ctx, void *ptr)
163{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200164 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200165}
166
Larry Hastingsebdcb502013-11-23 14:54:00 -0800167/*[clinic]
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800168
Larry Hastingsebdcb502013-11-23 14:54:00 -0800169zlib.compress
170 bytes: Py_buffer
171 Binary data to be compressed.
172 [
173 level: int
174 Compression level, in 0-9.
175 ]
176 /
177
178Returns compressed string.
179
180[clinic]*/
181
182PyDoc_STRVAR(zlib_compress__doc__,
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800183"compress(bytes, [level])\n"
Larry Hastingsebdcb502013-11-23 14:54:00 -0800184"Returns compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000185"\n"
Larry Hastingsebdcb502013-11-23 14:54:00 -0800186" bytes\n"
187" Binary data to be compressed.\n"
188" level\n"
189" Compression level, in 0-9.");
190
191#define ZLIB_COMPRESS_METHODDEF \
192 {"compress", (PyCFunction)zlib_compress, METH_VARARGS, zlib_compress__doc__},
Guido van Rossum3c540301997-06-03 22:21:03 +0000193
Guido van Rossumfb221561997-04-29 15:38:09 +0000194static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800195zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level);
196
197static PyObject *
198zlib_compress(PyModuleDef *module, PyObject *args)
199{
200 PyObject *return_value = NULL;
201 Py_buffer bytes;
202 int group_right_1 = 0;
203 int level = 0;
204
205 switch (PyTuple_Size(args)) {
206 case 1:
207 if (!PyArg_ParseTuple(args, "y*:compress", &bytes))
208 return NULL;
209 break;
210 case 2:
211 if (!PyArg_ParseTuple(args, "y*i:compress", &bytes, &level))
212 return NULL;
213 group_right_1 = 1;
214 break;
215 default:
216 PyErr_SetString(PyExc_TypeError, "zlib.compress requires 1 to 2 arguments");
217 return NULL;
218 }
219 return_value = zlib_compress_impl(module, &bytes, group_right_1, level);
220
221 /* Cleanup for bytes */
222 if (bytes.buf)
223 PyBuffer_Release(&bytes);
224
225 return return_value;
226}
227
228static PyObject *
229zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level)
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800230/*[clinic checksum: f490708eff84be652b5ebe7fe622ab73ac12c888]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000231{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000232 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200233 Byte *input, *output = NULL;
234 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800235 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000236 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000237
Larry Hastingsebdcb502013-11-23 14:54:00 -0800238 if (!group_right_1)
239 level = Z_DEFAULT_COMPRESSION;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200240
Larry Hastingsebdcb502013-11-23 14:54:00 -0800241 if ((size_t)bytes->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200242 PyErr_SetString(PyExc_OverflowError,
243 "Size does not fit in an unsigned int");
244 goto error;
245 }
Larry Hastingsebdcb502013-11-23 14:54:00 -0800246 input = bytes->buf;
247 length = (unsigned int)bytes->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000248
Jeremy Hylton9714f992001-10-16 21:19:45 +0000249 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000250
Victor Stinnerb6404912013-07-07 16:21:41 +0200251 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000252 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000253 PyErr_SetString(PyExc_MemoryError,
254 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200255 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000256 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000257
Jeremy Hylton9714f992001-10-16 21:19:45 +0000258 /* Past the point of no return. From here on out, we need to make sure
259 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000260
Victor Stinner5064a522013-07-07 16:50:27 +0200261 zst.opaque = NULL;
262 zst.zalloc = PyZlib_Malloc;
263 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000264 zst.next_out = (Byte *)output;
265 zst.next_in = (Byte *)input;
266 zst.avail_in = length;
267 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000268
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000269 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000270 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000271 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000272 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000273 PyErr_SetString(PyExc_MemoryError,
274 "Out of memory while compressing data");
275 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000276 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000277 PyErr_SetString(ZlibError,
278 "Bad compression level");
279 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000280 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000281 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000282 zlib_error(zst, err, "while compressing data");
283 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000284 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000285
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000286 Py_BEGIN_ALLOW_THREADS;
287 err = deflate(&zst, Z_FINISH);
288 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000289
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000290 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 zlib_error(zst, err, "while compressing data");
292 deflateEnd(&zst);
293 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000294 }
Tim Peters977e5402001-10-17 03:57:20 +0000295
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000296 err=deflateEnd(&zst);
297 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000298 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000299 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000300 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000301 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000302
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000303 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200304 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000305
Jeremy Hylton9714f992001-10-16 21:19:45 +0000306 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000307}
308
Victor Stinnere079edd2013-11-21 22:33:21 +0100309/*[python]
310
311class uint_converter(CConverter):
312 type = 'unsigned int'
313 converter = 'uint_converter'
314
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800315class compobject_converter(self_converter):
316 type = "compobject *"
317
Victor Stinnere079edd2013-11-21 22:33:21 +0100318[python]*/
319/*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
320
321static int
322uint_converter(PyObject *obj, void *ptr)
323{
324 long val;
325 unsigned long uval;
326
327 val = PyLong_AsLong(obj);
328 if (val == -1 && PyErr_Occurred()) {
329 uval = PyLong_AsUnsignedLong(obj);
330 if (uval == (unsigned long)-1 && PyErr_Occurred())
331 return 0;
332 if (uval > UINT_MAX) {
333 PyErr_SetString(PyExc_OverflowError,
334 "Python int too large for C unsigned int");
335 return 0;
336 }
337 }
338 else {
339 if (val < 0) {
340 PyErr_SetString(PyExc_ValueError,
341 "value must be positive");
342 return 0;
343 }
344 uval = (unsigned long)val;
345 }
346
347 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int);
348 return 1;
349}
350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000351PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000352"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
353"\n"
354"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000355"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000356
Guido van Rossumfb221561997-04-29 15:38:09 +0000357static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000358PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000359{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200360 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000361 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000362 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200363 unsigned int length;
364 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000365 int wsize=DEF_WBITS;
Victor Stinnere079edd2013-11-21 22:33:21 +0100366 unsigned int bufsize = DEFAULTALLOC, new_bufsize;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000367 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000368
Victor Stinnere079edd2013-11-21 22:33:21 +0100369 if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
370 &pinput, &wsize, uint_converter, &bufsize))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200372
Victor Stinnere079edd2013-11-21 22:33:21 +0100373 if ((size_t)pinput.len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200374 PyErr_SetString(PyExc_OverflowError,
375 "Size does not fit in an unsigned int");
376 goto error;
377 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000378 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100379 length = (unsigned int)pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000380
Victor Stinnere079edd2013-11-21 22:33:21 +0100381 if (bufsize == 0)
382 bufsize = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000383
Jeremy Hylton9714f992001-10-16 21:19:45 +0000384 zst.avail_in = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100385 zst.avail_out = bufsize;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000386
Victor Stinnere079edd2013-11-21 22:33:21 +0100387 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200388 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000389
Victor Stinner5064a522013-07-07 16:50:27 +0200390 zst.opaque = NULL;
391 zst.zalloc = PyZlib_Malloc;
392 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000393 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000394 zst.next_in = (Byte *)input;
395 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000396
Jeremy Hylton9714f992001-10-16 21:19:45 +0000397 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000398 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000399 break;
Tim Peters977e5402001-10-17 03:57:20 +0000400 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 PyErr_SetString(PyExc_MemoryError,
402 "Out of memory while decompressing data");
403 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000404 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000405 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000406 zlib_error(zst, err, "while preparing to decompress data");
407 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000408 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000409
Jeremy Hylton9714f992001-10-16 21:19:45 +0000410 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000411 Py_BEGIN_ALLOW_THREADS
412 err=inflate(&zst, Z_FINISH);
413 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000414
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000415 switch(err) {
416 case(Z_STREAM_END):
417 break;
418 case(Z_BUF_ERROR):
419 /*
420 * If there is at least 1 byte of room according to zst.avail_out
421 * and we get this error, assume that it means zlib cannot
422 * process the inflate call() due to an error in the data.
423 */
424 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000425 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000426 inflateEnd(&zst);
427 goto error;
428 }
429 /* fall through */
430 case(Z_OK):
431 /* need more memory */
Victor Stinnere079edd2013-11-21 22:33:21 +0100432 if (bufsize <= (UINT_MAX >> 1))
433 new_bufsize = bufsize << 1;
434 else
435 new_bufsize = UINT_MAX;
436 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000437 inflateEnd(&zst);
438 goto error;
439 }
440 zst.next_out =
Victor Stinnere079edd2013-11-21 22:33:21 +0100441 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
442 zst.avail_out = bufsize;
443 bufsize = new_bufsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000444 break;
445 default:
446 inflateEnd(&zst);
447 zlib_error(zst, err, "while decompressing data");
448 goto error;
449 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000450 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000451
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000452 err = inflateEnd(&zst);
453 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200454 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000456 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000457
Gregory P. Smith693fc462008-09-06 20:13:06 +0000458 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000459 goto error;
460
Martin v. Löwis423be952008-08-13 15:53:07 +0000461 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000462 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000463
464 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000465 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000466 Py_XDECREF(result_str);
467 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000468}
469
470static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200471PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000472{
Victor Stinnere079edd2013-11-21 22:33:21 +0100473 compobject *self = NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000474 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
475 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200476 Py_buffer zdict;
477 static char *kwlist[] = {"level", "method", "wbits",
478 "memLevel", "strategy", "zdict", NULL};
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000479
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200480 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
481 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
482 kwlist, &level, &method, &wbits,
483 &memLevel, &strategy, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000484 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000485
Victor Stinnere079edd2013-11-21 22:33:21 +0100486 if (zdict.buf != NULL && (size_t)zdict.len > UINT_MAX) {
487 PyErr_SetString(PyExc_OverflowError,
488 "zdict length does not fit in an unsigned int");
489 goto error;
490 }
491
Jeremy Hylton499000002001-10-16 21:59:35 +0000492 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000493 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200494 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200495 self->zst.opaque = NULL;
496 self->zst.zalloc = PyZlib_Malloc;
497 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000498 self->zst.next_in = NULL;
499 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000500 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
501 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000502 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000503 self->is_initialised = 1;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200504 if (zdict.buf == NULL) {
505 goto success;
506 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100507 err = deflateSetDictionary(&self->zst,
508 zdict.buf, (unsigned int)zdict.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200509 switch (err) {
510 case (Z_OK):
511 goto success;
512 case (Z_STREAM_ERROR):
513 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
514 goto error;
515 default:
516 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
517 goto error;
518 }
519 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000520 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000521 PyErr_SetString(PyExc_MemoryError,
522 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200523 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000524 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000525 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200526 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000527 default:
528 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200529 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000530 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200531
532 error:
533 Py_XDECREF(self);
534 self = NULL;
535 success:
536 if (zdict.buf != NULL)
537 PyBuffer_Release(&zdict);
538 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000539}
540
541static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200542PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000543{
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200544 static char *kwlist[] = {"wbits", "zdict", NULL};
Jeremy Hylton499000002001-10-16 21:59:35 +0000545 int wbits=DEF_WBITS, err;
546 compobject *self;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200547 PyObject *zdict=NULL;
548
549 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
550 kwlist, &wbits, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000551 return NULL;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200552 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
553 PyErr_SetString(PyExc_TypeError,
554 "zdict argument must support the buffer protocol");
555 return NULL;
556 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000557
558 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000559 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200561 self->zst.opaque = NULL;
562 self->zst.zalloc = PyZlib_Malloc;
563 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000564 self->zst.next_in = NULL;
565 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200566 if (zdict != NULL) {
567 Py_INCREF(zdict);
568 self->zdict = zdict;
569 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000570 err = inflateInit2(&self->zst, wbits);
571 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000572 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000573 self->is_initialised = 1;
574 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000575 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000576 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
578 return NULL;
579 case (Z_MEM_ERROR):
580 Py_DECREF(self);
581 PyErr_SetString(PyExc_MemoryError,
582 "Can't allocate memory for decompression object");
583 return NULL;
584 default:
585 zlib_error(self->zst, err, "while creating decompression object");
586 Py_DECREF(self);
587 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000588 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000589}
590
591static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000592Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000593{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000594#ifdef WITH_THREAD
595 PyThread_free_lock(self->lock);
596#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000597 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000598 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200599 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000600 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000601}
602
603static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000604Comp_dealloc(compobject *self)
605{
606 if (self->is_initialised)
607 deflateEnd(&self->zst);
608 Dealloc(self);
609}
610
611static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000612Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000613{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000614 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000615 inflateEnd(&self->zst);
616 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000617}
618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000619PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000620"compress(data) -- Return a string containing data compressed.\n"
621"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000622"After calling this function, some of the input data may still\n"
623"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000624"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000625
626
Guido van Rossumfb221561997-04-29 15:38:09 +0000627static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000628PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000629{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200630 int err;
631 unsigned int inplen;
Victor Stinnere079edd2013-11-21 22:33:21 +0100632 unsigned int length = DEFAULTALLOC, new_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200633 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000634 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000635 Byte *input;
636 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000637
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000638 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000639 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +0100640 if ((size_t)pinput.len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200641 PyErr_SetString(PyExc_OverflowError,
642 "Size does not fit in an unsigned int");
643 goto error_outer;
644 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000645 input = pinput.buf;
Victor Stinnere079edd2013-11-21 22:33:21 +0100646 inplen = (unsigned int)pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000647
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200648 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
649 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000650
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000651 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000652
Jeremy Hylton9714f992001-10-16 21:19:45 +0000653 start_total_out = self->zst.total_out;
654 self->zst.avail_in = inplen;
655 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000656 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000657 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000658
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000659 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000660 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000661 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000662
Jeremy Hylton9714f992001-10-16 21:19:45 +0000663 /* while Z_OK and the output buffer is full, there might be more output,
664 so extend the output buffer and try again */
665 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100666 if (length <= (UINT_MAX >> 1))
667 new_length = length << 1;
668 else
669 new_length = UINT_MAX;
670 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200671 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000672 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000673 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000674 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000675 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000676 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100677 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000678
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000679 Py_BEGIN_ALLOW_THREADS
680 err = deflate(&(self->zst), Z_NO_FLUSH);
681 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000682 }
Tim Peters977e5402001-10-17 03:57:20 +0000683 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000684 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000685 condition.
686 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000687
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000688 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200689 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000690 Py_DECREF(RetVal);
691 RetVal = NULL;
692 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000693 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000694 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200695 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000696 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000697
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000698 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000699 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200700 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000701 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000702 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000703}
704
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100705/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
706 self->unused_data or self->unconsumed_tail, as appropriate. */
707static int
708save_unconsumed_input(compobject *self, int err)
709{
710 if (err == Z_STREAM_END) {
711 /* The end of the compressed data has been reached. Store the leftover
712 input data in self->unused_data. */
713 if (self->zst.avail_in > 0) {
714 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
715 Py_ssize_t new_size;
716 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100717 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100718 PyErr_NoMemory();
719 return -1;
720 }
721 new_size = old_size + self->zst.avail_in;
722 new_data = PyBytes_FromStringAndSize(NULL, new_size);
723 if (new_data == NULL)
724 return -1;
725 Py_MEMCPY(PyBytes_AS_STRING(new_data),
726 PyBytes_AS_STRING(self->unused_data), old_size);
727 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
728 self->zst.next_in, self->zst.avail_in);
729 Py_DECREF(self->unused_data);
730 self->unused_data = new_data;
731 self->zst.avail_in = 0;
732 }
733 }
734 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
735 /* This code handles two distinct cases:
736 1. Output limit was reached. Save leftover input in unconsumed_tail.
737 2. All input data was consumed. Clear unconsumed_tail. */
738 PyObject *new_data = PyBytes_FromStringAndSize(
739 (char *)self->zst.next_in, self->zst.avail_in);
740 if (new_data == NULL)
741 return -1;
742 Py_DECREF(self->unconsumed_tail);
743 self->unconsumed_tail = new_data;
744 }
745 return 0;
746}
747
Larry Hastings31826802013-10-19 00:09:25 -0700748/*[clinic]
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800749
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800750zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700751
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800752 self: compobject
753
Larry Hastings31826802013-10-19 00:09:25 -0700754 data: Py_buffer
755 The binary data to decompress.
Victor Stinnere079edd2013-11-21 22:33:21 +0100756 max_length: uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700757 The maximum allowable length of the decompressed data.
758 Unconsumed input data will be stored in
759 the unconsumed_tail attribute.
760 /
761
762Return a string containing the decompressed version of the data.
763
764After calling this function, some of the input data may still be stored in
765internal buffers for later processing.
766Call the flush() method to clear these buffers.
767[clinic]*/
768
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800769PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800770"decompress(data, max_length=0)\n"
Larry Hastings31826802013-10-19 00:09:25 -0700771"Return a string containing the decompressed version of the data.\n"
772"\n"
Larry Hastings31826802013-10-19 00:09:25 -0700773" data\n"
774" The binary data to decompress.\n"
775" max_length\n"
776" The maximum allowable length of the decompressed data.\n"
777" Unconsumed input data will be stored in\n"
778" the unconsumed_tail attribute.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000779"\n"
780"After calling this function, some of the input data may still be stored in\n"
781"internal buffers for later processing.\n"
Larry Hastings31826802013-10-19 00:09:25 -0700782"Call the flush() method to clear these buffers.");
783
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800784#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
785 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
Guido van Rossum3c540301997-06-03 22:21:03 +0000786
Guido van Rossumfb221561997-04-29 15:38:09 +0000787static PyObject *
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800788zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length);
Larry Hastings31826802013-10-19 00:09:25 -0700789
790static PyObject *
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800791zlib_Decompress_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000792{
Larry Hastings31826802013-10-19 00:09:25 -0700793 PyObject *return_value = NULL;
794 Py_buffer data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100795 unsigned int max_length = 0;
Larry Hastings31826802013-10-19 00:09:25 -0700796
797 if (!PyArg_ParseTuple(args,
Victor Stinnere079edd2013-11-21 22:33:21 +0100798 "y*|O&:decompress",
799 &data, uint_converter, &max_length))
Larry Hastings31826802013-10-19 00:09:25 -0700800 goto exit;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800801 return_value = zlib_Decompress_decompress_impl((compobject *)self, &data, max_length);
Larry Hastings31826802013-10-19 00:09:25 -0700802
803exit:
804 /* Cleanup for data */
Larry Hastingsebdcb502013-11-23 14:54:00 -0800805 if (data.buf)
806 PyBuffer_Release(&data);
Larry Hastings31826802013-10-19 00:09:25 -0700807
808 return return_value;
809}
810
811static PyObject *
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800812zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length)
813/*[clinic checksum: 3599698948f5a712f5a8309491671cc2ce969d2c]*/
Larry Hastings31826802013-10-19 00:09:25 -0700814{
Larry Hastings31826802013-10-19 00:09:25 -0700815 int err;
Victor Stinnere079edd2013-11-21 22:33:21 +0100816 unsigned int old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200817 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000818 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000819
Victor Stinnere079edd2013-11-21 22:33:21 +0100820 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200821 PyErr_SetString(PyExc_OverflowError,
822 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700823 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200824 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000825
Jeremy Hylton9714f992001-10-16 21:19:45 +0000826 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000827 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000828 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200829 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700830 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000831
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800832 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000833
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800834 start_total_out = self->zst.total_out;
835 self->zst.avail_in = (unsigned int)data->len;
836 self->zst.next_in = data->buf;
837 self->zst.avail_out = length;
838 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000839
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000840 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800841 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000842 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000843
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800844 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200845 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800846 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200847 Py_DECREF(RetVal);
848 RetVal = NULL;
849 goto error;
850 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100851
852 if ((size_t)zdict_buf.len > UINT_MAX) {
853 PyErr_SetString(PyExc_OverflowError,
854 "zdict length does not fit in an unsigned int");
855 PyBuffer_Release(&zdict_buf);
856 Py_CLEAR(RetVal);
857 goto error;
858 }
859
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800860 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100861 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200862 PyBuffer_Release(&zdict_buf);
863 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800864 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100865 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200866 goto error;
867 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200868 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200869 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800870 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200871 Py_END_ALLOW_THREADS
872 }
873
Jeremy Hylton9714f992001-10-16 21:19:45 +0000874 /* While Z_OK and the output buffer is full, there might be more output.
875 So extend the output buffer and try again.
876 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800877 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 /* If max_length set, don't continue decompressing if we've already
879 reached the limit.
880 */
881 if (max_length && length >= max_length)
882 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000883
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884 /* otherwise, ... */
885 old_length = length;
886 length = length << 1;
887 if (max_length && length > max_length)
888 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000889
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200891 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000892 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000893 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800894 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000895 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800896 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000897
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800899 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000900 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000901 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000902
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800903 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200904 Py_DECREF(RetVal);
905 RetVal = NULL;
906 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000907 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000908
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000909 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100910 /* This is the logical place to call inflateEnd, but the old behaviour
911 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800912 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100913 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 /* We will only get Z_BUF_ERROR if the output buffer was full
915 but there wasn't more output when we tried again, so it is
916 not an error condition.
917 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800918 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000919 Py_DECREF(RetVal);
920 RetVal = NULL;
921 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000922 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000923
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800924 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200925 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000926 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000927
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000928 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800929 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000930 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000931}
932
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000933PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000934"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000935"\n"
936"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000937"default value used when mode is not specified is Z_FINISH.\n"
938"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000939"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000940
Guido van Rossumfb221561997-04-29 15:38:09 +0000941static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000942PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000943{
Victor Stinnere079edd2013-11-21 22:33:21 +0100944 int err;
945 unsigned int length = DEFAULTALLOC, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000946 PyObject *RetVal;
947 int flushmode = Z_FINISH;
948 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000949
Jeremy Hylton9714f992001-10-16 21:19:45 +0000950 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000951 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000952
Jeremy Hylton9714f992001-10-16 21:19:45 +0000953 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
954 doing any work at all; just return an empty string. */
955 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000956 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000957 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000958
Gregory P. Smith693fc462008-09-06 20:13:06 +0000959 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000960 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000961
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000962 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000963
Jeremy Hylton9714f992001-10-16 21:19:45 +0000964 start_total_out = self->zst.total_out;
965 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000966 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000967 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000968
969 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000970 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000971 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000972
Jeremy Hylton9714f992001-10-16 21:19:45 +0000973 /* while Z_OK and the output buffer is full, there might be more output,
974 so extend the output buffer and try again */
975 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100976 if (length <= (UINT_MAX >> 1))
977 new_length = length << 1;
978 else
979 new_length = UINT_MAX;
980 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200981 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000982 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000983 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000984 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000985 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000986 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100987 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000988
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000989 Py_BEGIN_ALLOW_THREADS
990 err = deflate(&(self->zst), flushmode);
991 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000992 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000993
Jeremy Hylton9714f992001-10-16 21:19:45 +0000994 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000995 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000996 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000997 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000998 err = deflateEnd(&(self->zst));
999 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001000 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001001 Py_DECREF(RetVal);
1002 RetVal = NULL;
1003 goto error;
1004 }
1005 else
1006 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +00001007
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001008 /* We will only get Z_BUF_ERROR if the output buffer was full
1009 but there wasn't more output when we tried again, so it is
1010 not an error condition.
1011 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001012 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001013 zlib_error(self->zst, err, "while flushing");
1014 Py_DECREF(RetVal);
1015 RetVal = NULL;
1016 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001017 }
Tim Peters977e5402001-10-17 03:57:20 +00001018
Gregory P. Smith693fc462008-09-06 20:13:06 +00001019 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001020 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +00001021 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001022
Tim Peters977e5402001-10-17 03:57:20 +00001023 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001024 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001025
1026 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001027}
1028
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001029#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -07001030
1031/*[clinic]
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001032zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -07001033
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001034 self: compobject
1035
Larry Hastings31826802013-10-19 00:09:25 -07001036Return a copy of the compression object.
1037[clinic]*/
1038
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001039PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings44e2eaa2013-11-23 15:37:55 -08001040"copy()\n"
1041"Return a copy of the compression object.");
Larry Hastings31826802013-10-19 00:09:25 -07001042
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001043#define ZLIB_COMPRESS_COPY_METHODDEF \
1044 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +00001045
1046static PyObject *
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001047zlib_Compress_copy(compobject *self)
1048/*[clinic checksum: 0b37c07f8f27deb7d4769951fbecf600a1006ef8]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049{
1050 compobject *retval = NULL;
1051 int err;
1052
1053 retval = newcompobject(&Comptype);
1054 if (!retval) return NULL;
1055
1056 /* Copy the zstream state
1057 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1058 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001059 ENTER_ZLIB(self);
1060 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001061 switch(err) {
1062 case(Z_OK):
1063 break;
1064 case(Z_STREAM_ERROR):
1065 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1066 goto error;
1067 case(Z_MEM_ERROR):
1068 PyErr_SetString(PyExc_MemoryError,
1069 "Can't allocate memory for compression object");
1070 goto error;
1071 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001072 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073 goto error;
1074 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001075 Py_INCREF(self->unused_data);
1076 Py_INCREF(self->unconsumed_tail);
1077 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001078 Py_XDECREF(retval->unused_data);
1079 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001080 Py_XDECREF(retval->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001081 retval->unused_data = self->unused_data;
1082 retval->unconsumed_tail = self->unconsumed_tail;
1083 retval->zdict = self->zdict;
1084 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001085
1086 /* Mark it as being initialized */
1087 retval->is_initialised = 1;
1088
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001089 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001090 return (PyObject *)retval;
1091
1092error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001093 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001094 Py_XDECREF(retval);
1095 return NULL;
1096}
1097
1098PyDoc_STRVAR(decomp_copy__doc__,
1099"copy() -- Return a copy of the decompression object.");
1100
1101static PyObject *
1102PyZlib_uncopy(compobject *self)
1103{
1104 compobject *retval = NULL;
1105 int err;
1106
1107 retval = newcompobject(&Decomptype);
1108 if (!retval) return NULL;
1109
1110 /* Copy the zstream state
1111 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1112 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001113 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001114 err = inflateCopy(&retval->zst, &self->zst);
1115 switch(err) {
1116 case(Z_OK):
1117 break;
1118 case(Z_STREAM_ERROR):
1119 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1120 goto error;
1121 case(Z_MEM_ERROR):
1122 PyErr_SetString(PyExc_MemoryError,
1123 "Can't allocate memory for decompression object");
1124 goto error;
1125 default:
1126 zlib_error(self->zst, err, "while copying decompression object");
1127 goto error;
1128 }
1129
1130 Py_INCREF(self->unused_data);
1131 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001132 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001133 Py_XDECREF(retval->unused_data);
1134 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001135 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001136 retval->unused_data = self->unused_data;
1137 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001138 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001139 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140
1141 /* Mark it as being initialized */
1142 retval->is_initialised = 1;
1143
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001144 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001145 return (PyObject *)retval;
1146
1147error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001148 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001149 Py_XDECREF(retval);
1150 return NULL;
1151}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001152#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001154PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155"flush( [length] ) -- Return a string containing any remaining\n"
1156"decompressed data. length, if given, is the initial size of the\n"
1157"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001158"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001159"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001160
Guido van Rossumfb221561997-04-29 15:38:09 +00001161static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001162PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001163{
Victor Stinnere079edd2013-11-21 22:33:21 +01001164 int err;
1165 unsigned int length = DEFAULTALLOC, new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001166 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001167 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001168 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001169
Victor Stinnere079edd2013-11-21 22:33:21 +01001170 if (!PyArg_ParseTuple(args, "|O&:flush", uint_converter, &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001171 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +01001172 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001173 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1174 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001175 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001176
Gregory P. Smith693fc462008-09-06 20:13:06 +00001177 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001178 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001179
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001180
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001181 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001182
Victor Stinnere079edd2013-11-21 22:33:21 +01001183 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1184
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001185 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001186 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1187 or equal than UINT_MAX */
1188 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001189 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001190 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001191 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001192
1193 Py_BEGIN_ALLOW_THREADS
1194 err = inflate(&(self->zst), Z_FINISH);
1195 Py_END_ALLOW_THREADS
1196
1197 /* while Z_OK and the output buffer is full, there might be more output,
1198 so extend the output buffer and try again */
1199 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001200 if (length <= (UINT_MAX >> 1))
1201 new_length = length << 1;
1202 else
1203 new_length = UINT_MAX;
1204 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001205 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001206 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001207 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001208 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1209 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001210 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001211
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001212 Py_BEGIN_ALLOW_THREADS
1213 err = inflate(&(self->zst), Z_FINISH);
1214 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001215 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001216
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001217 if (save_unconsumed_input(self, err) < 0) {
1218 Py_DECREF(retval);
1219 retval = NULL;
1220 goto error;
1221 }
1222
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001223 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001224 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001225 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001226 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001227 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001228 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001229 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001230 Py_DECREF(retval);
1231 retval = NULL;
1232 goto error;
1233 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001234 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001235
Gregory P. Smith693fc462008-09-06 20:13:06 +00001236 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001237 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001238 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001239
1240error:
1241
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001242 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001243
Jeremy Hylton9714f992001-10-16 21:19:45 +00001244 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001245}
1246
1247static PyMethodDef comp_methods[] =
1248{
Tim Peters977e5402001-10-17 03:57:20 +00001249 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001250 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001251 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001252 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001253#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001254 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001255#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001256 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001257};
1258
1259static PyMethodDef Decomp_methods[] =
1260{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001261 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Tim Peters977e5402001-10-17 03:57:20 +00001262 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001263 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001264#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001265 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1266 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001267#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001268 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001269};
1270
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001271#define COMP_OFF(x) offsetof(compobject, x)
1272static PyMemberDef Decomp_members[] = {
1273 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1274 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001275 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001276 {NULL},
1277};
Guido van Rossumfb221561997-04-29 15:38:09 +00001278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001279PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001280"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1281"\n"
1282"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001283"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001284
Guido van Rossumfb221561997-04-29 15:38:09 +00001285static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001286PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001287{
Christian Heimescc47b052008-03-25 14:56:36 +00001288 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001289 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +00001290
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001291 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1292 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001293 /* Releasing the GIL for very small buffers is inefficient
1294 and may lower performance */
1295 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001296 unsigned char *buf = pbuf.buf;
1297 Py_ssize_t len = pbuf.len;
1298
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001299 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001300 /* Avoid truncation of length for very large buffers. adler32() takes
1301 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001302 while ((size_t)len > UINT_MAX) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001303 adler32val = adler32(adler32val, buf, UINT_MAX);
1304 buf += (size_t) UINT_MAX;
1305 len -= (size_t) UINT_MAX;
1306 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001307 adler32val = adler32(adler32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001308 Py_END_ALLOW_THREADS
1309 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001310 adler32val = adler32(adler32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001311 }
1312 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001313 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001314}
Tim Peters977e5402001-10-17 03:57:20 +00001315
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001316PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001317"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1318"\n"
1319"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001320"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001321
1322static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001323PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001324{
Christian Heimescc47b052008-03-25 14:56:36 +00001325 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001326 Py_buffer pbuf;
1327 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001328
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001329 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001330 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001331 /* Releasing the GIL for very small buffers is inefficient
1332 and may lower performance */
1333 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001334 unsigned char *buf = pbuf.buf;
1335 Py_ssize_t len = pbuf.len;
1336
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001337 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001338 /* Avoid truncation of length for very large buffers. crc32() takes
1339 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001340 while ((size_t)len > UINT_MAX) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001341 crc32val = crc32(crc32val, buf, UINT_MAX);
1342 buf += (size_t) UINT_MAX;
1343 len -= (size_t) UINT_MAX;
1344 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001345 signed_val = crc32(crc32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001346 Py_END_ALLOW_THREADS
1347 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001348 signed_val = crc32(crc32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001349 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001350 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001351 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001352}
Tim Peters977e5402001-10-17 03:57:20 +00001353
Guido van Rossumfb221561997-04-29 15:38:09 +00001354
1355static PyMethodDef zlib_methods[] =
1356{
Tim Peters977e5402001-10-17 03:57:20 +00001357 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001358 adler32__doc__},
Larry Hastingsebdcb502013-11-23 14:54:00 -08001359 ZLIB_COMPRESS_METHODDEF
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001360 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001361 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001362 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1363 crc32__doc__},
1364 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001365 decompress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001366 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001367 decompressobj__doc__},
1368 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001369};
1370
Tim Peters0c322792002-07-17 16:49:03 +00001371static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001372 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001373 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001374 sizeof(compobject),
1375 0,
1376 (destructor)Comp_dealloc, /*tp_dealloc*/
1377 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001378 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001379 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001380 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001381 0, /*tp_repr*/
1382 0, /*tp_as_number*/
1383 0, /*tp_as_sequence*/
1384 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001385 0, /*tp_hash*/
1386 0, /*tp_call*/
1387 0, /*tp_str*/
1388 0, /*tp_getattro*/
1389 0, /*tp_setattro*/
1390 0, /*tp_as_buffer*/
1391 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1392 0, /*tp_doc*/
1393 0, /*tp_traverse*/
1394 0, /*tp_clear*/
1395 0, /*tp_richcompare*/
1396 0, /*tp_weaklistoffset*/
1397 0, /*tp_iter*/
1398 0, /*tp_iternext*/
1399 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001400};
1401
Tim Peters0c322792002-07-17 16:49:03 +00001402static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001403 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001404 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001405 sizeof(compobject),
1406 0,
1407 (destructor)Decomp_dealloc, /*tp_dealloc*/
1408 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001409 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001410 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001411 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001412 0, /*tp_repr*/
1413 0, /*tp_as_number*/
1414 0, /*tp_as_sequence*/
1415 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001416 0, /*tp_hash*/
1417 0, /*tp_call*/
1418 0, /*tp_str*/
1419 0, /*tp_getattro*/
1420 0, /*tp_setattro*/
1421 0, /*tp_as_buffer*/
1422 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1423 0, /*tp_doc*/
1424 0, /*tp_traverse*/
1425 0, /*tp_clear*/
1426 0, /*tp_richcompare*/
1427 0, /*tp_weaklistoffset*/
1428 0, /*tp_iter*/
1429 0, /*tp_iternext*/
1430 Decomp_methods, /*tp_methods*/
1431 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001432};
1433
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001434PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001435"The functions in this module allow compression and decompression using the\n"
1436"zlib library, which is based on GNU zip.\n"
1437"\n"
1438"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001439"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001440"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001441"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001442"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001443"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001444"\n"
1445"'wbits' is window buffer size.\n"
1446"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001447"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001448
Martin v. Löwis1a214512008-06-11 05:26:20 +00001449static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001450 PyModuleDef_HEAD_INIT,
1451 "zlib",
1452 zlib_module_documentation,
1453 -1,
1454 zlib_methods,
1455 NULL,
1456 NULL,
1457 NULL,
1458 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001459};
1460
Mark Hammond62b1ab12002-07-23 06:31:15 +00001461PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001462PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001463{
Fred Drake4baedc12002-04-01 14:53:37 +00001464 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001465 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001466 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001467 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001468 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001469 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001470 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001471 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001472
Fred Drake4baedc12002-04-01 14:53:37 +00001473 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1474 if (ZlibError != NULL) {
1475 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001476 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001477 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001478 PyModule_AddIntMacro(m, MAX_WBITS);
1479 PyModule_AddIntMacro(m, DEFLATED);
1480 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
1481 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1482 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1483 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1484 PyModule_AddIntMacro(m, Z_FILTERED);
1485 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1486 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001487
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001488 PyModule_AddIntMacro(m, Z_FINISH);
1489 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1490 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1491 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001492
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001493 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001494 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001495 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001496
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001497 ver = PyUnicode_FromString(zlibVersion());
1498 if (ver != NULL)
1499 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1500
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001501 PyModule_AddStringConstant(m, "__version__", "1.0");
1502
Martin v. Löwis1a214512008-06-11 05:26:20 +00001503 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001504}