blob: 1b65eb0be7a761ab5d3f056d8eb94580e2da1f35 [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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084PyDoc_STRVAR(compressobj__doc__,
Nadeem Vawda2180c972012-06-22 01:40:49 +020085"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
86" strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020087" -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +000088"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020089"level is the compression level (an integer in the range 0-9; default is 6).\n"
90"Higher compression levels are slower, but produce smaller results.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020091"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020092"method is the compression algorithm. If given, this must be DEFLATED.\n"
93"\n"
94"wbits is the base two logarithm of the window size (range: 8..15).\n"
95"\n"
96"memlevel controls the amount of memory used for internal compression state.\n"
97"Valid values range from 1 to 9. Higher values result in higher memory usage,\n"
98"faster compression, and smaller output.\n"
99"\n"
100"strategy is used to tune the compression algorithm. Possible values are\n"
101"Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
102"\n"
103"zdict is the predefined compression dictionary - a sequence of bytes\n"
104"containing subsequences that are likely to occur in the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106PyDoc_STRVAR(decompressobj__doc__,
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200107"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000108"\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200109"Optional arg wbits is the window buffer size.\n"
110"\n"
111"Optional arg zdict is the predefined compression dictionary. This must be\n"
112"the same dictionary as used by the compressor that produced the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000113
Guido van Rossumfb221561997-04-29 15:38:09 +0000114static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000115newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000116{
Tim Peters977e5402001-10-17 03:57:20 +0000117 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000118 self = PyObject_New(compobject, type);
119 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000120 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200121 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200123 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000124 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 Py_DECREF(self);
127 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000128 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000129 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000130 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000131 Py_DECREF(self);
132 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000133 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000134#ifdef WITH_THREAD
135 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200136 if (self->lock == NULL) {
137 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
138 return NULL;
139 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000140#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000141 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000142}
143
Victor Stinner5064a522013-07-07 16:50:27 +0200144static void*
145PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
146{
147 if (items > (size_t)PY_SSIZE_T_MAX / size)
148 return NULL;
149 /* PyMem_Malloc() cannot be used: the GIL is not held when
150 inflate() and deflate() are called */
151 return PyMem_RawMalloc(items * size);
152}
153
154static void
155PyZlib_Free(voidpf ctx, void *ptr)
156{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200157 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200158}
159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000161"compress(string[, level]) -- Returned compressed string.\n"
162"\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +0100163"Optional arg level is the compression level, in 0-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000164
Guido van Rossumfb221561997-04-29 15:38:09 +0000165static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000166PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000167{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000168 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000169 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200170 Byte *input, *output = NULL;
171 unsigned int length;
172 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000173 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000174
Jeremy Hylton9714f992001-10-16 21:19:45 +0000175 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000176 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000177 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200178
179 if (pinput.len > UINT_MAX) {
180 PyErr_SetString(PyExc_OverflowError,
181 "Size does not fit in an unsigned int");
182 goto error;
183 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000184 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100185 length = (unsigned int)pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000186
Jeremy Hylton9714f992001-10-16 21:19:45 +0000187 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000188
Victor Stinnerb6404912013-07-07 16:21:41 +0200189 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000190 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000191 PyErr_SetString(PyExc_MemoryError,
192 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200193 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000194 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000195
Jeremy Hylton9714f992001-10-16 21:19:45 +0000196 /* Past the point of no return. From here on out, we need to make sure
197 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000198
Victor Stinner5064a522013-07-07 16:50:27 +0200199 zst.opaque = NULL;
200 zst.zalloc = PyZlib_Malloc;
201 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000202 zst.next_out = (Byte *)output;
203 zst.next_in = (Byte *)input;
204 zst.avail_in = length;
205 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000206
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000207 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000208 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000209 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000210 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000211 PyErr_SetString(PyExc_MemoryError,
212 "Out of memory while compressing data");
213 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000214 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000215 PyErr_SetString(ZlibError,
216 "Bad compression level");
217 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000218 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000219 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 zlib_error(zst, err, "while compressing data");
221 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000222 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000224 Py_BEGIN_ALLOW_THREADS;
225 err = deflate(&zst, Z_FINISH);
226 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000227
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000228 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000229 zlib_error(zst, err, "while compressing data");
230 deflateEnd(&zst);
231 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000232 }
Tim Peters977e5402001-10-17 03:57:20 +0000233
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000234 err=deflateEnd(&zst);
235 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000237 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000238 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000240
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000241 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000242 PyBuffer_Release(&pinput);
Victor Stinnerb6404912013-07-07 16:21:41 +0200243 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000244
Jeremy Hylton9714f992001-10-16 21:19:45 +0000245 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000246}
247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000249"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
250"\n"
251"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000253
Guido van Rossumfb221561997-04-29 15:38:09 +0000254static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000255PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000256{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200257 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000258 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000259 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200260 unsigned int length;
261 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000262 int wsize=DEF_WBITS;
263 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000264 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000265
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000266 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000267 &pinput, &wsize, &r_strlen))
268 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200269
270 if (pinput.len > UINT_MAX) {
271 PyErr_SetString(PyExc_OverflowError,
272 "Size does not fit in an unsigned int");
273 goto error;
274 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000275 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100276 length = (unsigned int)pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000277
Jeremy Hylton9714f992001-10-16 21:19:45 +0000278 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000279 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000280
Jeremy Hylton9714f992001-10-16 21:19:45 +0000281 zst.avail_in = length;
282 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000283
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200284 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
285 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000286
Victor Stinner5064a522013-07-07 16:50:27 +0200287 zst.opaque = NULL;
288 zst.zalloc = PyZlib_Malloc;
289 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000290 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000291 zst.next_in = (Byte *)input;
292 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000293
Jeremy Hylton9714f992001-10-16 21:19:45 +0000294 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000295 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000296 break;
Tim Peters977e5402001-10-17 03:57:20 +0000297 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000298 PyErr_SetString(PyExc_MemoryError,
299 "Out of memory while decompressing data");
300 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000301 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000302 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 zlib_error(zst, err, "while preparing to decompress data");
304 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000305 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000306
Jeremy Hylton9714f992001-10-16 21:19:45 +0000307 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000308 Py_BEGIN_ALLOW_THREADS
309 err=inflate(&zst, Z_FINISH);
310 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000311
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 switch(err) {
313 case(Z_STREAM_END):
314 break;
315 case(Z_BUF_ERROR):
316 /*
317 * If there is at least 1 byte of room according to zst.avail_out
318 * and we get this error, assume that it means zlib cannot
319 * process the inflate call() due to an error in the data.
320 */
321 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000322 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 inflateEnd(&zst);
324 goto error;
325 }
326 /* fall through */
327 case(Z_OK):
328 /* need more memory */
329 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
330 inflateEnd(&zst);
331 goto error;
332 }
333 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000334 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 zst.avail_out = r_strlen;
336 r_strlen = r_strlen << 1;
337 break;
338 default:
339 inflateEnd(&zst);
340 zlib_error(zst, err, "while decompressing data");
341 goto error;
342 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000343 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000344
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000345 err = inflateEnd(&zst);
346 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200347 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000348 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000349 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000350
Gregory P. Smith693fc462008-09-06 20:13:06 +0000351 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000352 goto error;
353
Martin v. Löwis423be952008-08-13 15:53:07 +0000354 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000355 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000356
357 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000358 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000359 Py_XDECREF(result_str);
360 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000361}
362
363static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200364PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000365{
Jeremy Hylton499000002001-10-16 21:59:35 +0000366 compobject *self;
367 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
368 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200369 Py_buffer zdict;
370 static char *kwlist[] = {"level", "method", "wbits",
371 "memLevel", "strategy", "zdict", NULL};
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000372
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200373 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
374 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
375 kwlist, &level, &method, &wbits,
376 &memLevel, &strategy, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000378
Jeremy Hylton499000002001-10-16 21:59:35 +0000379 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000380 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200381 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200382 self->zst.opaque = NULL;
383 self->zst.zalloc = PyZlib_Malloc;
384 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000385 self->zst.next_in = NULL;
386 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000387 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
388 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000389 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000390 self->is_initialised = 1;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200391 if (zdict.buf == NULL) {
392 goto success;
393 } else {
394 err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len);
395 switch (err) {
396 case (Z_OK):
397 goto success;
398 case (Z_STREAM_ERROR):
399 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
400 goto error;
401 default:
402 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
403 goto error;
404 }
405 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000406 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000407 PyErr_SetString(PyExc_MemoryError,
408 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200409 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000410 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000411 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200412 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000413 default:
414 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200415 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000416 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200417
418 error:
419 Py_XDECREF(self);
420 self = NULL;
421 success:
422 if (zdict.buf != NULL)
423 PyBuffer_Release(&zdict);
424 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000425}
426
427static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200428PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000429{
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200430 static char *kwlist[] = {"wbits", "zdict", NULL};
Jeremy Hylton499000002001-10-16 21:59:35 +0000431 int wbits=DEF_WBITS, err;
432 compobject *self;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200433 PyObject *zdict=NULL;
434
435 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
436 kwlist, &wbits, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000437 return NULL;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200438 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
439 PyErr_SetString(PyExc_TypeError,
440 "zdict argument must support the buffer protocol");
441 return NULL;
442 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000443
444 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000445 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000446 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200447 self->zst.opaque = NULL;
448 self->zst.zalloc = PyZlib_Malloc;
449 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000450 self->zst.next_in = NULL;
451 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200452 if (zdict != NULL) {
453 Py_INCREF(zdict);
454 self->zdict = zdict;
455 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000456 err = inflateInit2(&self->zst, wbits);
457 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000458 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 self->is_initialised = 1;
460 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000461 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000462 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
464 return NULL;
465 case (Z_MEM_ERROR):
466 Py_DECREF(self);
467 PyErr_SetString(PyExc_MemoryError,
468 "Can't allocate memory for decompression object");
469 return NULL;
470 default:
471 zlib_error(self->zst, err, "while creating decompression object");
472 Py_DECREF(self);
473 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000474 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000475}
476
477static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000478Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000479{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000480#ifdef WITH_THREAD
481 PyThread_free_lock(self->lock);
482#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000483 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000484 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200485 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000486 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000487}
488
489static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000490Comp_dealloc(compobject *self)
491{
492 if (self->is_initialised)
493 deflateEnd(&self->zst);
494 Dealloc(self);
495}
496
497static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000498Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000499{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000500 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000501 inflateEnd(&self->zst);
502 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000503}
504
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000505PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000506"compress(data) -- Return a string containing data compressed.\n"
507"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000508"After calling this function, some of the input data may still\n"
509"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000510"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000511
512
Guido van Rossumfb221561997-04-29 15:38:09 +0000513static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000514PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000515{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200516 int err;
517 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000518 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200519 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000520 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000521 Byte *input;
522 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000523
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000524 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000525 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200526 if (pinput.len > UINT_MAX) {
527 PyErr_SetString(PyExc_OverflowError,
528 "Size does not fit in an unsigned int");
529 goto error_outer;
530 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000531 input = pinput.buf;
532 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000533
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200534 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
535 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000536
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000537 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000538
Jeremy Hylton9714f992001-10-16 21:19:45 +0000539 start_total_out = self->zst.total_out;
540 self->zst.avail_in = inplen;
541 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000542 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000543 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000544
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000545 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000546 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000547 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000548
Jeremy Hylton9714f992001-10-16 21:19:45 +0000549 /* while Z_OK and the output buffer is full, there might be more output,
550 so extend the output buffer and try again */
551 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000552 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200553 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000555 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000557 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000558 self->zst.avail_out = length;
559 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000560
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 Py_BEGIN_ALLOW_THREADS
562 err = deflate(&(self->zst), Z_NO_FLUSH);
563 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000564 }
Tim Peters977e5402001-10-17 03:57:20 +0000565 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000566 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000567 condition.
568 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000569
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000570 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200571 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000572 Py_DECREF(RetVal);
573 RetVal = NULL;
574 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000575 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000576 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200577 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000578 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000579
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000580 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000581 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200582 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000583 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000584 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000585}
586
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100587/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
588 self->unused_data or self->unconsumed_tail, as appropriate. */
589static int
590save_unconsumed_input(compobject *self, int err)
591{
592 if (err == Z_STREAM_END) {
593 /* The end of the compressed data has been reached. Store the leftover
594 input data in self->unused_data. */
595 if (self->zst.avail_in > 0) {
596 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
597 Py_ssize_t new_size;
598 PyObject *new_data;
Victor Stinnere8289612013-05-07 23:50:21 +0200599 if ((Py_ssize_t)self->zst.avail_in > PY_SSIZE_T_MAX - old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100600 PyErr_NoMemory();
601 return -1;
602 }
603 new_size = old_size + self->zst.avail_in;
604 new_data = PyBytes_FromStringAndSize(NULL, new_size);
605 if (new_data == NULL)
606 return -1;
607 Py_MEMCPY(PyBytes_AS_STRING(new_data),
608 PyBytes_AS_STRING(self->unused_data), old_size);
609 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
610 self->zst.next_in, self->zst.avail_in);
611 Py_DECREF(self->unused_data);
612 self->unused_data = new_data;
613 self->zst.avail_in = 0;
614 }
615 }
616 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
617 /* This code handles two distinct cases:
618 1. Output limit was reached. Save leftover input in unconsumed_tail.
619 2. All input data was consumed. Clear unconsumed_tail. */
620 PyObject *new_data = PyBytes_FromStringAndSize(
621 (char *)self->zst.next_in, self->zst.avail_in);
622 if (new_data == NULL)
623 return -1;
624 Py_DECREF(self->unconsumed_tail);
625 self->unconsumed_tail = new_data;
626 }
627 return 0;
628}
629
Larry Hastings31826802013-10-19 00:09:25 -0700630/*[clinic]
631
632module zlib
633
634zlib.decompress
635
636 data: Py_buffer
637 The binary data to decompress.
638 max_length: int = 0
639 The maximum allowable length of the decompressed data.
640 Unconsumed input data will be stored in
641 the unconsumed_tail attribute.
642 /
643
644Return a string containing the decompressed version of the data.
645
646After calling this function, some of the input data may still be stored in
647internal buffers for later processing.
648Call the flush() method to clear these buffers.
649[clinic]*/
650
651PyDoc_STRVAR(zlib_decompress__doc__,
652"Return a string containing the decompressed version of the data.\n"
653"\n"
654"zlib.decompress(data, max_length=0)\n"
655" data\n"
656" The binary data to decompress.\n"
657" max_length\n"
658" The maximum allowable length of the decompressed data.\n"
659" Unconsumed input data will be stored in\n"
660" the unconsumed_tail attribute.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000661"\n"
662"After calling this function, some of the input data may still be stored in\n"
663"internal buffers for later processing.\n"
Larry Hastings31826802013-10-19 00:09:25 -0700664"Call the flush() method to clear these buffers.");
665
666#define ZLIB_DECOMPRESS_METHODDEF \
667 {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__},
Guido van Rossum3c540301997-06-03 22:21:03 +0000668
Guido van Rossumfb221561997-04-29 15:38:09 +0000669static PyObject *
Larry Hastings31826802013-10-19 00:09:25 -0700670zlib_decompress_impl(PyObject *self, Py_buffer *data, int max_length);
671
672static PyObject *
673zlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000674{
Larry Hastings31826802013-10-19 00:09:25 -0700675 PyObject *return_value = NULL;
676 Py_buffer data;
677 int max_length = 0;
678
679 if (!PyArg_ParseTuple(args,
680 "y*|i:decompress",
681 &data, &max_length))
682 goto exit;
683 return_value = zlib_decompress_impl(self, &data, max_length);
684
685exit:
686 /* Cleanup for data */
687 PyBuffer_Release(&data);
688
689 return return_value;
690}
691
692static PyObject *
693zlib_decompress_impl(PyObject *self, Py_buffer *data, int max_length)
694/*[clinic checksum: 168d093d400739dde947cca1f4fb0f9d51cdc2c9]*/
695{
696 compobject *zself = (compobject *)self;
697 int err;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200698 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000699 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200700 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000701 Byte *input;
702 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000703
Larry Hastings31826802013-10-19 00:09:25 -0700704 if (data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200705 PyErr_SetString(PyExc_OverflowError,
706 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700707 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200708 }
Larry Hastings31826802013-10-19 00:09:25 -0700709 input = data->buf;
710 inplen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000711 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000712 PyErr_SetString(PyExc_ValueError,
713 "max_length must be greater than zero");
Larry Hastings31826802013-10-19 00:09:25 -0700714 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000715 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000716
Jeremy Hylton9714f992001-10-16 21:19:45 +0000717 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000718 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200720 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700721 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000722
Larry Hastings31826802013-10-19 00:09:25 -0700723 ENTER_ZLIB(zself);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000724
Larry Hastings31826802013-10-19 00:09:25 -0700725 start_total_out = zself->zst.total_out;
726 zself->zst.avail_in = inplen;
727 zself->zst.next_in = input;
728 zself->zst.avail_out = length;
729 zself->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000730
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000731 Py_BEGIN_ALLOW_THREADS
Larry Hastings31826802013-10-19 00:09:25 -0700732 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000733 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000734
Larry Hastings31826802013-10-19 00:09:25 -0700735 if (err == Z_NEED_DICT && zself->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200736 Py_buffer zdict_buf;
Larry Hastings31826802013-10-19 00:09:25 -0700737 if (PyObject_GetBuffer(zself->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200738 Py_DECREF(RetVal);
739 RetVal = NULL;
740 goto error;
741 }
Larry Hastings31826802013-10-19 00:09:25 -0700742 err = inflateSetDictionary(&(zself->zst), zdict_buf.buf, zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200743 PyBuffer_Release(&zdict_buf);
744 if (err != Z_OK) {
Larry Hastings31826802013-10-19 00:09:25 -0700745 zlib_error(zself->zst, err, "while decompressing data");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200746 Py_DECREF(RetVal);
747 RetVal = NULL;
748 goto error;
749 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200750 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200751 Py_BEGIN_ALLOW_THREADS
Larry Hastings31826802013-10-19 00:09:25 -0700752 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200753 Py_END_ALLOW_THREADS
754 }
755
Jeremy Hylton9714f992001-10-16 21:19:45 +0000756 /* While Z_OK and the output buffer is full, there might be more output.
757 So extend the output buffer and try again.
758 */
Larry Hastings31826802013-10-19 00:09:25 -0700759 while (err == Z_OK && zself->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000760 /* If max_length set, don't continue decompressing if we've already
761 reached the limit.
762 */
763 if (max_length && length >= max_length)
764 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000765
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000766 /* otherwise, ... */
767 old_length = length;
768 length = length << 1;
769 if (max_length && length > max_length)
770 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000771
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000772 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200773 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000774 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000775 }
Larry Hastings31826802013-10-19 00:09:25 -0700776 zself->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000777 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastings31826802013-10-19 00:09:25 -0700778 zself->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000779
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000780 Py_BEGIN_ALLOW_THREADS
Larry Hastings31826802013-10-19 00:09:25 -0700781 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000783 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000784
Larry Hastings31826802013-10-19 00:09:25 -0700785 if (save_unconsumed_input(zself, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200786 Py_DECREF(RetVal);
787 RetVal = NULL;
788 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000789 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000790
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000791 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100792 /* This is the logical place to call inflateEnd, but the old behaviour
793 of only calling it on flush() is preserved. */
Larry Hastings31826802013-10-19 00:09:25 -0700794 zself->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100795 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000796 /* We will only get Z_BUF_ERROR if the output buffer was full
797 but there wasn't more output when we tried again, so it is
798 not an error condition.
799 */
Larry Hastings31826802013-10-19 00:09:25 -0700800 zlib_error(zself->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000801 Py_DECREF(RetVal);
802 RetVal = NULL;
803 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000804 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000805
Larry Hastings31826802013-10-19 00:09:25 -0700806 if (_PyBytes_Resize(&RetVal, zself->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200807 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000808 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000809
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000810 error:
Larry Hastings31826802013-10-19 00:09:25 -0700811 LEAVE_ZLIB(zself);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000812 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000813}
814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000815PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000816"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000817"\n"
818"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000819"default value used when mode is not specified is Z_FINISH.\n"
820"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000821"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000822
Guido van Rossumfb221561997-04-29 15:38:09 +0000823static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000824PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000825{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000826 int err, length = DEFAULTALLOC;
827 PyObject *RetVal;
828 int flushmode = Z_FINISH;
829 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000830
Jeremy Hylton9714f992001-10-16 21:19:45 +0000831 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000832 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000833
Jeremy Hylton9714f992001-10-16 21:19:45 +0000834 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
835 doing any work at all; just return an empty string. */
836 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000837 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000838 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000839
Gregory P. Smith693fc462008-09-06 20:13:06 +0000840 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000841 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000842
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000843 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000844
Jeremy Hylton9714f992001-10-16 21:19:45 +0000845 start_total_out = self->zst.total_out;
846 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000847 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000848 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000849
850 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000851 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000852 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000853
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854 /* while Z_OK and the output buffer is full, there might be more output,
855 so extend the output buffer and try again */
856 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000857 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200858 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000860 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000861 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000862 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000863 self->zst.avail_out = length;
864 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000865
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000866 Py_BEGIN_ALLOW_THREADS
867 err = deflate(&(self->zst), flushmode);
868 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000869 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000870
Jeremy Hylton9714f992001-10-16 21:19:45 +0000871 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000872 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000873 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000874 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000875 err = deflateEnd(&(self->zst));
876 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200877 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 Py_DECREF(RetVal);
879 RetVal = NULL;
880 goto error;
881 }
882 else
883 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 /* We will only get Z_BUF_ERROR if the output buffer was full
886 but there wasn't more output when we tried again, so it is
887 not an error condition.
888 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000889 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 zlib_error(self->zst, err, "while flushing");
891 Py_DECREF(RetVal);
892 RetVal = NULL;
893 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894 }
Tim Peters977e5402001-10-17 03:57:20 +0000895
Gregory P. Smith693fc462008-09-06 20:13:06 +0000896 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200897 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000898 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000899
Tim Peters977e5402001-10-17 03:57:20 +0000900 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000901 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000902
903 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000904}
905
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000906#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700907
908/*[clinic]
909
910zlib.copy
911
912Return a copy of the compression object.
913[clinic]*/
914
915PyDoc_STRVAR(zlib_copy__doc__,
916"Return a copy of the compression object.\n"
917"\n"
918"zlib.copy()");
919
920#define ZLIB_COPY_METHODDEF \
921 {"copy", (PyCFunction)zlib_copy, METH_NOARGS, zlib_copy__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922
923static PyObject *
Larry Hastings31826802013-10-19 00:09:25 -0700924zlib_copy(PyObject *self)
925/*[clinic checksum: 7b648de2c1f933ba2b9fa17331ff1a44d9a4a740]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926{
Larry Hastings31826802013-10-19 00:09:25 -0700927 compobject *zself = (compobject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928 compobject *retval = NULL;
929 int err;
930
931 retval = newcompobject(&Comptype);
932 if (!retval) return NULL;
933
934 /* Copy the zstream state
935 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
936 */
Larry Hastings31826802013-10-19 00:09:25 -0700937 ENTER_ZLIB(zself);
938 err = deflateCopy(&retval->zst, &zself->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939 switch(err) {
940 case(Z_OK):
941 break;
942 case(Z_STREAM_ERROR):
943 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
944 goto error;
945 case(Z_MEM_ERROR):
946 PyErr_SetString(PyExc_MemoryError,
947 "Can't allocate memory for compression object");
948 goto error;
949 default:
Larry Hastings31826802013-10-19 00:09:25 -0700950 zlib_error(zself->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 goto error;
952 }
Larry Hastings31826802013-10-19 00:09:25 -0700953 Py_INCREF(zself->unused_data);
954 Py_INCREF(zself->unconsumed_tail);
955 Py_XINCREF(zself->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 Py_XDECREF(retval->unused_data);
957 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200958 Py_XDECREF(retval->zdict);
Larry Hastings31826802013-10-19 00:09:25 -0700959 retval->unused_data = zself->unused_data;
960 retval->unconsumed_tail = zself->unconsumed_tail;
961 retval->zdict = zself->zdict;
962 retval->eof = zself->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963
964 /* Mark it as being initialized */
965 retval->is_initialised = 1;
966
Larry Hastings31826802013-10-19 00:09:25 -0700967 LEAVE_ZLIB(zself);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968 return (PyObject *)retval;
969
970error:
Larry Hastings31826802013-10-19 00:09:25 -0700971 LEAVE_ZLIB(zself);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972 Py_XDECREF(retval);
973 return NULL;
974}
975
976PyDoc_STRVAR(decomp_copy__doc__,
977"copy() -- Return a copy of the decompression object.");
978
979static PyObject *
980PyZlib_uncopy(compobject *self)
981{
982 compobject *retval = NULL;
983 int err;
984
985 retval = newcompobject(&Decomptype);
986 if (!retval) return NULL;
987
988 /* Copy the zstream state
989 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
990 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000991 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000992 err = inflateCopy(&retval->zst, &self->zst);
993 switch(err) {
994 case(Z_OK):
995 break;
996 case(Z_STREAM_ERROR):
997 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
998 goto error;
999 case(Z_MEM_ERROR):
1000 PyErr_SetString(PyExc_MemoryError,
1001 "Can't allocate memory for decompression object");
1002 goto error;
1003 default:
1004 zlib_error(self->zst, err, "while copying decompression object");
1005 goto error;
1006 }
1007
1008 Py_INCREF(self->unused_data);
1009 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001010 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001011 Py_XDECREF(retval->unused_data);
1012 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001013 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001014 retval->unused_data = self->unused_data;
1015 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001016 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001017 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001018
1019 /* Mark it as being initialized */
1020 retval->is_initialised = 1;
1021
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001022 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001023 return (PyObject *)retval;
1024
1025error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001026 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027 Py_XDECREF(retval);
1028 return NULL;
1029}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001030#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001032PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001033"flush( [length] ) -- Return a string containing any remaining\n"
1034"decompressed data. length, if given, is the initial size of the\n"
1035"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001036"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001037"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001038
Guido van Rossumfb221561997-04-29 15:38:09 +00001039static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001040PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001041{
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001042 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001043 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001044 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +00001045
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001046 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001047 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001048 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001049 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1050 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001051 }
Gregory P. Smith693fc462008-09-06 20:13:06 +00001052 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001053 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001054
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001055
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001056 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001057
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001058 start_total_out = self->zst.total_out;
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001059 self->zst.avail_in = PyBytes_GET_SIZE(self->unconsumed_tail);
1060 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001061 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001062 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001063
1064 Py_BEGIN_ALLOW_THREADS
1065 err = inflate(&(self->zst), Z_FINISH);
1066 Py_END_ALLOW_THREADS
1067
1068 /* while Z_OK and the output buffer is full, there might be more output,
1069 so extend the output buffer and try again */
1070 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001071 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001072 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001073 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001074 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001075 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1076 self->zst.avail_out = length;
1077 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001078
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001079 Py_BEGIN_ALLOW_THREADS
1080 err = inflate(&(self->zst), Z_FINISH);
1081 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001082 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001083
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001084 if (save_unconsumed_input(self, err) < 0) {
1085 Py_DECREF(retval);
1086 retval = NULL;
1087 goto error;
1088 }
1089
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001090 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001091 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001092 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001093 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001094 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001095 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001096 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001097 Py_DECREF(retval);
1098 retval = NULL;
1099 goto error;
1100 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001101 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001102
Gregory P. Smith693fc462008-09-06 20:13:06 +00001103 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001104 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001105 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001106
1107error:
1108
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001109 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001110
Jeremy Hylton9714f992001-10-16 21:19:45 +00001111 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001112}
1113
1114static PyMethodDef comp_methods[] =
1115{
Tim Peters977e5402001-10-17 03:57:20 +00001116 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001117 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001118 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001119 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001120#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -07001121 ZLIB_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001122#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001123 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001124};
1125
1126static PyMethodDef Decomp_methods[] =
1127{
Larry Hastings31826802013-10-19 00:09:25 -07001128 ZLIB_DECOMPRESS_METHODDEF
Tim Peters977e5402001-10-17 03:57:20 +00001129 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001130 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001131#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001132 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1133 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001134#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001135 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001136};
1137
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001138#define COMP_OFF(x) offsetof(compobject, x)
1139static PyMemberDef Decomp_members[] = {
1140 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1141 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001142 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001143 {NULL},
1144};
Guido van Rossumfb221561997-04-29 15:38:09 +00001145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001146PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001147"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1148"\n"
1149"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001150"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001151
Guido van Rossumfb221561997-04-29 15:38:09 +00001152static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001153PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001154{
Christian Heimescc47b052008-03-25 14:56:36 +00001155 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001156 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +00001157
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001158 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1159 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001160 /* Releasing the GIL for very small buffers is inefficient
1161 and may lower performance */
1162 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001163 unsigned char *buf = pbuf.buf;
1164 Py_ssize_t len = pbuf.len;
1165
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001166 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001167 /* Avoid truncation of length for very large buffers. adler32() takes
1168 length as an unsigned int, which may be narrower than Py_ssize_t. */
1169 while (len > (size_t) UINT_MAX) {
1170 adler32val = adler32(adler32val, buf, UINT_MAX);
1171 buf += (size_t) UINT_MAX;
1172 len -= (size_t) UINT_MAX;
1173 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001174 adler32val = adler32(adler32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001175 Py_END_ALLOW_THREADS
1176 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001177 adler32val = adler32(adler32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001178 }
1179 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001180 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001181}
Tim Peters977e5402001-10-17 03:57:20 +00001182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001183PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001184"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1185"\n"
1186"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001187"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001188
1189static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001190PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001191{
Christian Heimescc47b052008-03-25 14:56:36 +00001192 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001193 Py_buffer pbuf;
1194 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001195
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001196 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001197 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001198 /* Releasing the GIL for very small buffers is inefficient
1199 and may lower performance */
1200 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001201 unsigned char *buf = pbuf.buf;
1202 Py_ssize_t len = pbuf.len;
1203
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001204 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001205 /* Avoid truncation of length for very large buffers. crc32() takes
1206 length as an unsigned int, which may be narrower than Py_ssize_t. */
1207 while (len > (size_t) UINT_MAX) {
1208 crc32val = crc32(crc32val, buf, UINT_MAX);
1209 buf += (size_t) UINT_MAX;
1210 len -= (size_t) UINT_MAX;
1211 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001212 signed_val = crc32(crc32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001213 Py_END_ALLOW_THREADS
1214 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001215 signed_val = crc32(crc32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001216 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001217 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001218 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001219}
Tim Peters977e5402001-10-17 03:57:20 +00001220
Guido van Rossumfb221561997-04-29 15:38:09 +00001221
1222static PyMethodDef zlib_methods[] =
1223{
Tim Peters977e5402001-10-17 03:57:20 +00001224 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001225 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001226 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001227 compress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001228 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001229 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001230 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1231 crc32__doc__},
1232 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001233 decompress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001234 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001235 decompressobj__doc__},
1236 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001237};
1238
Tim Peters0c322792002-07-17 16:49:03 +00001239static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001240 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001241 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001242 sizeof(compobject),
1243 0,
1244 (destructor)Comp_dealloc, /*tp_dealloc*/
1245 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001246 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001247 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001248 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001249 0, /*tp_repr*/
1250 0, /*tp_as_number*/
1251 0, /*tp_as_sequence*/
1252 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001253 0, /*tp_hash*/
1254 0, /*tp_call*/
1255 0, /*tp_str*/
1256 0, /*tp_getattro*/
1257 0, /*tp_setattro*/
1258 0, /*tp_as_buffer*/
1259 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1260 0, /*tp_doc*/
1261 0, /*tp_traverse*/
1262 0, /*tp_clear*/
1263 0, /*tp_richcompare*/
1264 0, /*tp_weaklistoffset*/
1265 0, /*tp_iter*/
1266 0, /*tp_iternext*/
1267 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001268};
1269
Tim Peters0c322792002-07-17 16:49:03 +00001270static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001271 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001272 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001273 sizeof(compobject),
1274 0,
1275 (destructor)Decomp_dealloc, /*tp_dealloc*/
1276 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001277 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001278 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001279 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001280 0, /*tp_repr*/
1281 0, /*tp_as_number*/
1282 0, /*tp_as_sequence*/
1283 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001284 0, /*tp_hash*/
1285 0, /*tp_call*/
1286 0, /*tp_str*/
1287 0, /*tp_getattro*/
1288 0, /*tp_setattro*/
1289 0, /*tp_as_buffer*/
1290 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1291 0, /*tp_doc*/
1292 0, /*tp_traverse*/
1293 0, /*tp_clear*/
1294 0, /*tp_richcompare*/
1295 0, /*tp_weaklistoffset*/
1296 0, /*tp_iter*/
1297 0, /*tp_iternext*/
1298 Decomp_methods, /*tp_methods*/
1299 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001300};
1301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001302PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001303"The functions in this module allow compression and decompression using the\n"
1304"zlib library, which is based on GNU zip.\n"
1305"\n"
1306"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001307"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001308"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001309"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001310"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001311"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001312"\n"
1313"'wbits' is window buffer size.\n"
1314"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001315"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001316
Martin v. Löwis1a214512008-06-11 05:26:20 +00001317static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001318 PyModuleDef_HEAD_INIT,
1319 "zlib",
1320 zlib_module_documentation,
1321 -1,
1322 zlib_methods,
1323 NULL,
1324 NULL,
1325 NULL,
1326 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001327};
1328
Mark Hammond62b1ab12002-07-23 06:31:15 +00001329PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001330PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001331{
Fred Drake4baedc12002-04-01 14:53:37 +00001332 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001333 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001334 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001335 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001336 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001337 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001338 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001339 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001340
Fred Drake4baedc12002-04-01 14:53:37 +00001341 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1342 if (ZlibError != NULL) {
1343 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001344 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001345 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001346 PyModule_AddIntMacro(m, MAX_WBITS);
1347 PyModule_AddIntMacro(m, DEFLATED);
1348 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
1349 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1350 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1351 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1352 PyModule_AddIntMacro(m, Z_FILTERED);
1353 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1354 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001355
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001356 PyModule_AddIntMacro(m, Z_FINISH);
1357 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1358 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1359 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001360
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001361 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001362 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001363 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001364
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001365 ver = PyUnicode_FromString(zlibVersion());
1366 if (ver != NULL)
1367 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1368
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001369 PyModule_AddStringConstant(m, "__version__", "1.0");
1370
Martin v. Löwis1a214512008-06-11 05:26:20 +00001371 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001372}