blob: 169903e7ea9c08a5665c44af33d83cf8974ccaa9 [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
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000011#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000012 #include "pythread.h"
13 #define ENTER_ZLIB(obj) \
14 Py_BEGIN_ALLOW_THREADS; \
15 PyThread_acquire_lock((obj)->lock, 1); \
16 Py_END_ALLOW_THREADS;
17 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000019 #define ENTER_ZLIB(obj)
20 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000021#endif
22
Guido van Rossumfb221561997-04-29 15:38:09 +000023/* The following parameters are copied from zutil.h, version 0.95 */
24#define DEFLATED 8
25#if MAX_MEM_LEVEL >= 8
26# define DEF_MEM_LEVEL 8
27#else
28# define DEF_MEM_LEVEL MAX_MEM_LEVEL
29#endif
30#define DEF_WBITS MAX_WBITS
31
Guido van Rossumb729a1d1999-04-07 20:23:17 +000032/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
33#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000034
Jeremy Hylton938ace62002-07-17 16:30:39 +000035static PyTypeObject Comptype;
36static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000037
38static PyObject *ZlibError;
39
Tim Peters977e5402001-10-17 03:57:20 +000040typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000041{
Jeremy Hylton9714f992001-10-16 21:19:45 +000042 PyObject_HEAD
43 z_stream zst;
44 PyObject *unused_data;
45 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020046 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000047 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020048 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000049 #ifdef WITH_THREAD
50 PyThread_type_lock lock;
51 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000052} compobject;
53
Jeremy Hylton0965e082001-10-16 21:56:09 +000054static void
55zlib_error(z_stream zst, int err, char *msg)
56{
Nadeem Vawda524148a2011-08-28 11:26:46 +020057 const char *zmsg = Z_NULL;
58 /* In case of a version mismatch, zst.msg won't be initialized.
59 Check for this case first, before looking at zst.msg. */
60 if (err == Z_VERSION_ERROR)
61 zmsg = "library version mismatch";
62 if (zmsg == Z_NULL)
63 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000064 if (zmsg == Z_NULL) {
65 switch (err) {
66 case Z_BUF_ERROR:
67 zmsg = "incomplete or truncated stream";
68 break;
69 case Z_STREAM_ERROR:
70 zmsg = "inconsistent stream state";
71 break;
72 case Z_DATA_ERROR:
73 zmsg = "invalid input data";
74 break;
75 }
76 }
77 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000079 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000080 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000081}
82
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083PyDoc_STRVAR(compressobj__doc__,
Nadeem Vawda2180c972012-06-22 01:40:49 +020084"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
85" strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020086" -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +000087"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020088"level is the compression level (an integer in the range 0-9; default is 6).\n"
89"Higher compression levels are slower, but produce smaller results.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020090"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020091"method is the compression algorithm. If given, this must be DEFLATED.\n"
92"\n"
93"wbits is the base two logarithm of the window size (range: 8..15).\n"
94"\n"
95"memlevel controls the amount of memory used for internal compression state.\n"
96"Valid values range from 1 to 9. Higher values result in higher memory usage,\n"
97"faster compression, and smaller output.\n"
98"\n"
99"strategy is used to tune the compression algorithm. Possible values are\n"
100"Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
101"\n"
102"zdict is the predefined compression dictionary - a sequence of bytes\n"
103"containing subsequences that are likely to occur in the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105PyDoc_STRVAR(decompressobj__doc__,
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200106"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000107"\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200108"Optional arg wbits is the window buffer size.\n"
109"\n"
110"Optional arg zdict is the predefined compression dictionary. This must be\n"
111"the same dictionary as used by the compressor that produced the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000112
Guido van Rossumfb221561997-04-29 15:38:09 +0000113static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000114newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000115{
Tim Peters977e5402001-10-17 03:57:20 +0000116 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000117 self = PyObject_New(compobject, type);
118 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000119 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200120 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000121 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200122 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000123 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000124 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000125 Py_DECREF(self);
126 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000127 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000128 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000130 Py_DECREF(self);
131 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000132 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000133#ifdef WITH_THREAD
134 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200135 if (self->lock == NULL) {
136 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
137 return NULL;
138 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000139#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000140 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000141}
142
Victor Stinner5064a522013-07-07 16:50:27 +0200143static void*
144PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
145{
146 if (items > (size_t)PY_SSIZE_T_MAX / size)
147 return NULL;
148 /* PyMem_Malloc() cannot be used: the GIL is not held when
149 inflate() and deflate() are called */
150 return PyMem_RawMalloc(items * size);
151}
152
153static void
154PyZlib_Free(voidpf ctx, void *ptr)
155{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200156 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200157}
158
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000160"compress(string[, level]) -- Returned compressed string.\n"
161"\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +0100162"Optional arg level is the compression level, in 0-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000163
Guido van Rossumfb221561997-04-29 15:38:09 +0000164static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000165PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000166{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000167 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000168 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200169 Byte *input, *output = NULL;
170 unsigned int length;
171 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000172 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000173
Jeremy Hylton9714f992001-10-16 21:19:45 +0000174 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000175 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200177
178 if (pinput.len > UINT_MAX) {
179 PyErr_SetString(PyExc_OverflowError,
180 "Size does not fit in an unsigned int");
181 goto error;
182 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000183 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100184 length = (unsigned int)pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000185
Jeremy Hylton9714f992001-10-16 21:19:45 +0000186 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000187
Victor Stinnerb6404912013-07-07 16:21:41 +0200188 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000189 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 PyErr_SetString(PyExc_MemoryError,
191 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200192 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000193 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000194
Jeremy Hylton9714f992001-10-16 21:19:45 +0000195 /* Past the point of no return. From here on out, we need to make sure
196 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000197
Victor Stinner5064a522013-07-07 16:50:27 +0200198 zst.opaque = NULL;
199 zst.zalloc = PyZlib_Malloc;
200 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000201 zst.next_out = (Byte *)output;
202 zst.next_in = (Byte *)input;
203 zst.avail_in = length;
204 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000205
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000206 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000207 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000208 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000209 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000210 PyErr_SetString(PyExc_MemoryError,
211 "Out of memory while compressing data");
212 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000213 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 PyErr_SetString(ZlibError,
215 "Bad compression level");
216 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000217 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000218 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000219 zlib_error(zst, err, "while compressing data");
220 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000221 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000222
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000223 Py_BEGIN_ALLOW_THREADS;
224 err = deflate(&zst, Z_FINISH);
225 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000226
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000227 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 zlib_error(zst, err, "while compressing data");
229 deflateEnd(&zst);
230 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000231 }
Tim Peters977e5402001-10-17 03:57:20 +0000232
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000233 err=deflateEnd(&zst);
234 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000235 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000236 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000237 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000239
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000240 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000241 PyBuffer_Release(&pinput);
Victor Stinnerb6404912013-07-07 16:21:41 +0200242 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000243
Jeremy Hylton9714f992001-10-16 21:19:45 +0000244 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000245}
246
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000247PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000248"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
249"\n"
250"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000251"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000252
Guido van Rossumfb221561997-04-29 15:38:09 +0000253static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000254PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000255{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200256 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000257 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000258 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200259 unsigned int length;
260 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000261 int wsize=DEF_WBITS;
262 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000263 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000264
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000265 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000266 &pinput, &wsize, &r_strlen))
267 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200268
269 if (pinput.len > UINT_MAX) {
270 PyErr_SetString(PyExc_OverflowError,
271 "Size does not fit in an unsigned int");
272 goto error;
273 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000274 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100275 length = (unsigned int)pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000276
Jeremy Hylton9714f992001-10-16 21:19:45 +0000277 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000278 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000279
Jeremy Hylton9714f992001-10-16 21:19:45 +0000280 zst.avail_in = length;
281 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000282
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200283 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
284 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000285
Victor Stinner5064a522013-07-07 16:50:27 +0200286 zst.opaque = NULL;
287 zst.zalloc = PyZlib_Malloc;
288 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000289 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000290 zst.next_in = (Byte *)input;
291 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000292
Jeremy Hylton9714f992001-10-16 21:19:45 +0000293 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000294 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000295 break;
Tim Peters977e5402001-10-17 03:57:20 +0000296 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000297 PyErr_SetString(PyExc_MemoryError,
298 "Out of memory while decompressing data");
299 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000300 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000301 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000302 zlib_error(zst, err, "while preparing to decompress data");
303 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000304 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000305
Jeremy Hylton9714f992001-10-16 21:19:45 +0000306 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000307 Py_BEGIN_ALLOW_THREADS
308 err=inflate(&zst, Z_FINISH);
309 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000310
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000311 switch(err) {
312 case(Z_STREAM_END):
313 break;
314 case(Z_BUF_ERROR):
315 /*
316 * If there is at least 1 byte of room according to zst.avail_out
317 * and we get this error, assume that it means zlib cannot
318 * process the inflate call() due to an error in the data.
319 */
320 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000321 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000322 inflateEnd(&zst);
323 goto error;
324 }
325 /* fall through */
326 case(Z_OK):
327 /* need more memory */
328 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
329 inflateEnd(&zst);
330 goto error;
331 }
332 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000333 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000334 zst.avail_out = r_strlen;
335 r_strlen = r_strlen << 1;
336 break;
337 default:
338 inflateEnd(&zst);
339 zlib_error(zst, err, "while decompressing data");
340 goto error;
341 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000342 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000343
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000344 err = inflateEnd(&zst);
345 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200346 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000347 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000348 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000349
Gregory P. Smith693fc462008-09-06 20:13:06 +0000350 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000351 goto error;
352
Martin v. Löwis423be952008-08-13 15:53:07 +0000353 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000354 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000355
356 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000357 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000358 Py_XDECREF(result_str);
359 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000360}
361
362static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200363PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000364{
Jeremy Hylton499000002001-10-16 21:59:35 +0000365 compobject *self;
366 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
367 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200368 Py_buffer zdict;
369 static char *kwlist[] = {"level", "method", "wbits",
370 "memLevel", "strategy", "zdict", NULL};
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000371
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200372 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
373 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
374 kwlist, &level, &method, &wbits,
375 &memLevel, &strategy, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000376 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000377
Jeremy Hylton499000002001-10-16 21:59:35 +0000378 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000379 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200380 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200381 self->zst.opaque = NULL;
382 self->zst.zalloc = PyZlib_Malloc;
383 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000384 self->zst.next_in = NULL;
385 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000386 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
387 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000388 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000389 self->is_initialised = 1;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200390 if (zdict.buf == NULL) {
391 goto success;
392 } else {
393 err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len);
394 switch (err) {
395 case (Z_OK):
396 goto success;
397 case (Z_STREAM_ERROR):
398 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
399 goto error;
400 default:
401 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
402 goto error;
403 }
404 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000405 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000406 PyErr_SetString(PyExc_MemoryError,
407 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200408 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000409 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000410 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200411 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000412 default:
413 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200414 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000415 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200416
417 error:
418 Py_XDECREF(self);
419 self = NULL;
420 success:
421 if (zdict.buf != NULL)
422 PyBuffer_Release(&zdict);
423 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000424}
425
426static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200427PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000428{
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200429 static char *kwlist[] = {"wbits", "zdict", NULL};
Jeremy Hylton499000002001-10-16 21:59:35 +0000430 int wbits=DEF_WBITS, err;
431 compobject *self;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200432 PyObject *zdict=NULL;
433
434 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
435 kwlist, &wbits, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 return NULL;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200437 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
438 PyErr_SetString(PyExc_TypeError,
439 "zdict argument must support the buffer protocol");
440 return NULL;
441 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000442
443 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000444 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000445 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200446 self->zst.opaque = NULL;
447 self->zst.zalloc = PyZlib_Malloc;
448 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000449 self->zst.next_in = NULL;
450 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200451 if (zdict != NULL) {
452 Py_INCREF(zdict);
453 self->zdict = zdict;
454 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000455 err = inflateInit2(&self->zst, wbits);
456 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000457 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458 self->is_initialised = 1;
459 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000460 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000461 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
463 return NULL;
464 case (Z_MEM_ERROR):
465 Py_DECREF(self);
466 PyErr_SetString(PyExc_MemoryError,
467 "Can't allocate memory for decompression object");
468 return NULL;
469 default:
470 zlib_error(self->zst, err, "while creating decompression object");
471 Py_DECREF(self);
472 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000473 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000474}
475
476static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000477Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000478{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000479#ifdef WITH_THREAD
480 PyThread_free_lock(self->lock);
481#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000482 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000483 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200484 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000485 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000486}
487
488static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000489Comp_dealloc(compobject *self)
490{
491 if (self->is_initialised)
492 deflateEnd(&self->zst);
493 Dealloc(self);
494}
495
496static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000497Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000498{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000499 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000500 inflateEnd(&self->zst);
501 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000505"compress(data) -- Return a string containing data compressed.\n"
506"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000507"After calling this function, some of the input data may still\n"
508"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000509"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000510
511
Guido van Rossumfb221561997-04-29 15:38:09 +0000512static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000513PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000514{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200515 int err;
516 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000517 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200518 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000519 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000520 Byte *input;
521 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000522
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000523 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000524 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200525 if (pinput.len > UINT_MAX) {
526 PyErr_SetString(PyExc_OverflowError,
527 "Size does not fit in an unsigned int");
528 goto error_outer;
529 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000530 input = pinput.buf;
531 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000532
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200533 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
534 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000535
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000536 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000537
Jeremy Hylton9714f992001-10-16 21:19:45 +0000538 start_total_out = self->zst.total_out;
539 self->zst.avail_in = inplen;
540 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000541 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000542 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000543
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000544 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000545 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000546 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000547
Jeremy Hylton9714f992001-10-16 21:19:45 +0000548 /* while Z_OK and the output buffer is full, there might be more output,
549 so extend the output buffer and try again */
550 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000551 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200552 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000553 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000554 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000555 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000556 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000557 self->zst.avail_out = length;
558 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000559
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 Py_BEGIN_ALLOW_THREADS
561 err = deflate(&(self->zst), Z_NO_FLUSH);
562 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000563 }
Tim Peters977e5402001-10-17 03:57:20 +0000564 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000565 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000566 condition.
567 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000568
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000569 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200570 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000571 Py_DECREF(RetVal);
572 RetVal = NULL;
573 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000574 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000575 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200576 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000577 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000578
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000579 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000580 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200581 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000582 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000583 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000584}
585
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100586/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
587 self->unused_data or self->unconsumed_tail, as appropriate. */
588static int
589save_unconsumed_input(compobject *self, int err)
590{
591 if (err == Z_STREAM_END) {
592 /* The end of the compressed data has been reached. Store the leftover
593 input data in self->unused_data. */
594 if (self->zst.avail_in > 0) {
595 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
596 Py_ssize_t new_size;
597 PyObject *new_data;
Victor Stinnere8289612013-05-07 23:50:21 +0200598 if ((Py_ssize_t)self->zst.avail_in > PY_SSIZE_T_MAX - old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100599 PyErr_NoMemory();
600 return -1;
601 }
602 new_size = old_size + self->zst.avail_in;
603 new_data = PyBytes_FromStringAndSize(NULL, new_size);
604 if (new_data == NULL)
605 return -1;
606 Py_MEMCPY(PyBytes_AS_STRING(new_data),
607 PyBytes_AS_STRING(self->unused_data), old_size);
608 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
609 self->zst.next_in, self->zst.avail_in);
610 Py_DECREF(self->unused_data);
611 self->unused_data = new_data;
612 self->zst.avail_in = 0;
613 }
614 }
615 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
616 /* This code handles two distinct cases:
617 1. Output limit was reached. Save leftover input in unconsumed_tail.
618 2. All input data was consumed. Clear unconsumed_tail. */
619 PyObject *new_data = PyBytes_FromStringAndSize(
620 (char *)self->zst.next_in, self->zst.avail_in);
621 if (new_data == NULL)
622 return -1;
623 Py_DECREF(self->unconsumed_tail);
624 self->unconsumed_tail = new_data;
625 }
626 return 0;
627}
628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000629PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000630"decompress(data, max_length) -- Return a string containing the decompressed\n"
631"version of the data.\n"
632"\n"
633"After calling this function, some of the input data may still be stored in\n"
634"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000635"Call the flush() method to clear these buffers.\n"
636"If the max_length parameter is specified then the return value will be\n"
637"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000638"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000639
Guido van Rossumfb221561997-04-29 15:38:09 +0000640static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000641PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000642{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200643 int err, max_length = 0;
644 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000645 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200646 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000647 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000648 Byte *input;
649 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000650
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000651 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000652 &max_length))
653 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200654 if (pinput.len > UINT_MAX) {
655 PyErr_SetString(PyExc_OverflowError,
656 "Size does not fit in an unsigned int");
657 goto error_outer;
658 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000659 input = pinput.buf;
660 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000661 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 PyErr_SetString(PyExc_ValueError,
663 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200664 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000665 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000666
Jeremy Hylton9714f992001-10-16 21:19:45 +0000667 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000668 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000669 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200670 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
671 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000672
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000673 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000674
Jeremy Hylton9714f992001-10-16 21:19:45 +0000675 start_total_out = self->zst.total_out;
676 self->zst.avail_in = inplen;
677 self->zst.next_in = input;
678 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000679 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000680
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000681 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000682 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000683 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000684
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200685 if (err == Z_NEED_DICT && self->zdict != NULL) {
686 Py_buffer zdict_buf;
687 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
688 Py_DECREF(RetVal);
689 RetVal = NULL;
690 goto error;
691 }
692 err = inflateSetDictionary(&(self->zst), zdict_buf.buf, zdict_buf.len);
693 PyBuffer_Release(&zdict_buf);
694 if (err != Z_OK) {
695 zlib_error(self->zst, err, "while decompressing data");
696 Py_DECREF(RetVal);
697 RetVal = NULL;
698 goto error;
699 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200700 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200701 Py_BEGIN_ALLOW_THREADS
702 err = inflate(&(self->zst), Z_SYNC_FLUSH);
703 Py_END_ALLOW_THREADS
704 }
705
Jeremy Hylton9714f992001-10-16 21:19:45 +0000706 /* While Z_OK and the output buffer is full, there might be more output.
707 So extend the output buffer and try again.
708 */
Tim Peters977e5402001-10-17 03:57:20 +0000709 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 /* If max_length set, don't continue decompressing if we've already
711 reached the limit.
712 */
713 if (max_length && length >= max_length)
714 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000715
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000716 /* otherwise, ... */
717 old_length = length;
718 length = length << 1;
719 if (max_length && length > max_length)
720 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000721
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000722 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200723 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000724 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000725 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000726 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000727 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000728 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000729
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000730 Py_BEGIN_ALLOW_THREADS
731 err = inflate(&(self->zst), Z_SYNC_FLUSH);
732 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000733 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000734
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100735 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200736 Py_DECREF(RetVal);
737 RetVal = NULL;
738 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000739 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000740
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000741 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100742 /* This is the logical place to call inflateEnd, but the old behaviour
743 of only calling it on flush() is preserved. */
Nadeem Vawda1c385462011-08-13 15:22:40 +0200744 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100745 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000746 /* We will only get Z_BUF_ERROR if the output buffer was full
747 but there wasn't more output when we tried again, so it is
748 not an error condition.
749 */
Nadeem Vawda1c385462011-08-13 15:22:40 +0200750 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000751 Py_DECREF(RetVal);
752 RetVal = NULL;
753 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000754 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000755
Gregory P. Smith693fc462008-09-06 20:13:06 +0000756 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200757 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000758 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000759
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000760 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000761 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200762 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000763 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000764 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000765}
766
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000767PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000768"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000769"\n"
770"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000771"default value used when mode is not specified is Z_FINISH.\n"
772"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000773"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000774
Guido van Rossumfb221561997-04-29 15:38:09 +0000775static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000776PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000777{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000778 int err, length = DEFAULTALLOC;
779 PyObject *RetVal;
780 int flushmode = Z_FINISH;
781 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000782
Jeremy Hylton9714f992001-10-16 21:19:45 +0000783 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000784 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000785
Jeremy Hylton9714f992001-10-16 21:19:45 +0000786 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
787 doing any work at all; just return an empty string. */
788 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000789 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000790 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000791
Gregory P. Smith693fc462008-09-06 20:13:06 +0000792 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000793 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000794
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000795 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000796
Jeremy Hylton9714f992001-10-16 21:19:45 +0000797 start_total_out = self->zst.total_out;
798 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000799 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000800 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000801
802 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000803 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000804 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000805
Jeremy Hylton9714f992001-10-16 21:19:45 +0000806 /* while Z_OK and the output buffer is full, there might be more output,
807 so extend the output buffer and try again */
808 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000809 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200810 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000811 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000812 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000814 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 self->zst.avail_out = length;
816 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000817
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000818 Py_BEGIN_ALLOW_THREADS
819 err = deflate(&(self->zst), flushmode);
820 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000821 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000822
Jeremy Hylton9714f992001-10-16 21:19:45 +0000823 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000824 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000825 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000826 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000827 err = deflateEnd(&(self->zst));
828 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200829 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000830 Py_DECREF(RetVal);
831 RetVal = NULL;
832 goto error;
833 }
834 else
835 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000836
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000837 /* We will only get Z_BUF_ERROR if the output buffer was full
838 but there wasn't more output when we tried again, so it is
839 not an error condition.
840 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000841 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000842 zlib_error(self->zst, err, "while flushing");
843 Py_DECREF(RetVal);
844 RetVal = NULL;
845 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000846 }
Tim Peters977e5402001-10-17 03:57:20 +0000847
Gregory P. Smith693fc462008-09-06 20:13:06 +0000848 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200849 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000850 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000851
Tim Peters977e5402001-10-17 03:57:20 +0000852 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000853 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000854
855 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000856}
857
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000858#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859PyDoc_STRVAR(comp_copy__doc__,
860"copy() -- Return a copy of the compression object.");
861
862static PyObject *
863PyZlib_copy(compobject *self)
864{
865 compobject *retval = NULL;
866 int err;
867
868 retval = newcompobject(&Comptype);
869 if (!retval) return NULL;
870
871 /* Copy the zstream state
872 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
873 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000874 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875 err = deflateCopy(&retval->zst, &self->zst);
876 switch(err) {
877 case(Z_OK):
878 break;
879 case(Z_STREAM_ERROR):
880 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
881 goto error;
882 case(Z_MEM_ERROR):
883 PyErr_SetString(PyExc_MemoryError,
884 "Can't allocate memory for compression object");
885 goto error;
886 default:
887 zlib_error(self->zst, err, "while copying compression object");
888 goto error;
889 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890 Py_INCREF(self->unused_data);
891 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200892 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000893 Py_XDECREF(retval->unused_data);
894 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200895 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896 retval->unused_data = self->unused_data;
897 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200898 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200899 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900
901 /* Mark it as being initialized */
902 retval->is_initialised = 1;
903
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000904 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905 return (PyObject *)retval;
906
907error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000908 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909 Py_XDECREF(retval);
910 return NULL;
911}
912
913PyDoc_STRVAR(decomp_copy__doc__,
914"copy() -- Return a copy of the decompression object.");
915
916static PyObject *
917PyZlib_uncopy(compobject *self)
918{
919 compobject *retval = NULL;
920 int err;
921
922 retval = newcompobject(&Decomptype);
923 if (!retval) return NULL;
924
925 /* Copy the zstream state
926 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
927 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000928 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000929 err = inflateCopy(&retval->zst, &self->zst);
930 switch(err) {
931 case(Z_OK):
932 break;
933 case(Z_STREAM_ERROR):
934 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
935 goto error;
936 case(Z_MEM_ERROR):
937 PyErr_SetString(PyExc_MemoryError,
938 "Can't allocate memory for decompression object");
939 goto error;
940 default:
941 zlib_error(self->zst, err, "while copying decompression object");
942 goto error;
943 }
944
945 Py_INCREF(self->unused_data);
946 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200947 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948 Py_XDECREF(retval->unused_data);
949 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200950 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 retval->unused_data = self->unused_data;
952 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200953 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200954 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955
956 /* Mark it as being initialized */
957 retval->is_initialised = 1;
958
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000959 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960 return (PyObject *)retval;
961
962error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000963 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 Py_XDECREF(retval);
965 return NULL;
966}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000967#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000969PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970"flush( [length] ) -- Return a string containing any remaining\n"
971"decompressed data. length, if given, is the initial size of the\n"
972"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000973"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000974"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000975
Guido van Rossumfb221561997-04-29 15:38:09 +0000976static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000977PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000978{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000979 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000980 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000981 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000982
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000983 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000984 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000985 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000986 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
987 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000988 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000989 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000990 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000991
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000992
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000993 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000994
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000995 start_total_out = self->zst.total_out;
Nadeem Vawda7ee95552012-11-11 03:15:32 +0100996 self->zst.avail_in = PyBytes_GET_SIZE(self->unconsumed_tail);
997 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000998 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000999 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001000
1001 Py_BEGIN_ALLOW_THREADS
1002 err = inflate(&(self->zst), Z_FINISH);
1003 Py_END_ALLOW_THREADS
1004
1005 /* while Z_OK and the output buffer is full, there might be more output,
1006 so extend the output buffer and try again */
1007 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001008 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001009 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001010 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001011 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001012 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1013 self->zst.avail_out = length;
1014 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001015
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001016 Py_BEGIN_ALLOW_THREADS
1017 err = inflate(&(self->zst), Z_FINISH);
1018 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001019 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001020
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001021 if (save_unconsumed_input(self, err) < 0) {
1022 Py_DECREF(retval);
1023 retval = NULL;
1024 goto error;
1025 }
1026
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001027 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001028 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001029 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001030 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001031 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001032 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001033 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001034 Py_DECREF(retval);
1035 retval = NULL;
1036 goto error;
1037 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001038 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001039
Gregory P. Smith693fc462008-09-06 20:13:06 +00001040 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001041 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001042 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001043
1044error:
1045
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001046 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001047
Jeremy Hylton9714f992001-10-16 21:19:45 +00001048 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001049}
1050
1051static PyMethodDef comp_methods[] =
1052{
Tim Peters977e5402001-10-17 03:57:20 +00001053 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001054 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001055 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001056 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001057#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
1059 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001060#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001061 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001062};
1063
1064static PyMethodDef Decomp_methods[] =
1065{
Tim Peters977e5402001-10-17 03:57:20 +00001066 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001067 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001068 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001069 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001070#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1072 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001073#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001074 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001075};
1076
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001077#define COMP_OFF(x) offsetof(compobject, x)
1078static PyMemberDef Decomp_members[] = {
1079 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1080 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001081 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001082 {NULL},
1083};
Guido van Rossumfb221561997-04-29 15:38:09 +00001084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001085PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001086"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1087"\n"
1088"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001089"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001090
Guido van Rossumfb221561997-04-29 15:38:09 +00001091static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001092PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001093{
Christian Heimescc47b052008-03-25 14:56:36 +00001094 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001095 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +00001096
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001097 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1098 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001099 /* Releasing the GIL for very small buffers is inefficient
1100 and may lower performance */
1101 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001102 unsigned char *buf = pbuf.buf;
1103 Py_ssize_t len = pbuf.len;
1104
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001105 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001106 /* Avoid truncation of length for very large buffers. adler32() takes
1107 length as an unsigned int, which may be narrower than Py_ssize_t. */
1108 while (len > (size_t) UINT_MAX) {
1109 adler32val = adler32(adler32val, buf, UINT_MAX);
1110 buf += (size_t) UINT_MAX;
1111 len -= (size_t) UINT_MAX;
1112 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001113 adler32val = adler32(adler32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001114 Py_END_ALLOW_THREADS
1115 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001116 adler32val = adler32(adler32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001117 }
1118 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001119 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001120}
Tim Peters977e5402001-10-17 03:57:20 +00001121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001123"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1124"\n"
1125"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001126"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001127
1128static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001129PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001130{
Christian Heimescc47b052008-03-25 14:56:36 +00001131 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001132 Py_buffer pbuf;
1133 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001134
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001135 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001136 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001137 /* Releasing the GIL for very small buffers is inefficient
1138 and may lower performance */
1139 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001140 unsigned char *buf = pbuf.buf;
1141 Py_ssize_t len = pbuf.len;
1142
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001143 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001144 /* Avoid truncation of length for very large buffers. crc32() takes
1145 length as an unsigned int, which may be narrower than Py_ssize_t. */
1146 while (len > (size_t) UINT_MAX) {
1147 crc32val = crc32(crc32val, buf, UINT_MAX);
1148 buf += (size_t) UINT_MAX;
1149 len -= (size_t) UINT_MAX;
1150 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001151 signed_val = crc32(crc32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001152 Py_END_ALLOW_THREADS
1153 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001154 signed_val = crc32(crc32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001155 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001156 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001157 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001158}
Tim Peters977e5402001-10-17 03:57:20 +00001159
Guido van Rossumfb221561997-04-29 15:38:09 +00001160
1161static PyMethodDef zlib_methods[] =
1162{
Tim Peters977e5402001-10-17 03:57:20 +00001163 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001164 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001165 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001166 compress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001167 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001168 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001169 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1170 crc32__doc__},
1171 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001172 decompress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001173 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001174 decompressobj__doc__},
1175 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001176};
1177
Tim Peters0c322792002-07-17 16:49:03 +00001178static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001179 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001180 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001181 sizeof(compobject),
1182 0,
1183 (destructor)Comp_dealloc, /*tp_dealloc*/
1184 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001185 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001186 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001187 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001188 0, /*tp_repr*/
1189 0, /*tp_as_number*/
1190 0, /*tp_as_sequence*/
1191 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001192 0, /*tp_hash*/
1193 0, /*tp_call*/
1194 0, /*tp_str*/
1195 0, /*tp_getattro*/
1196 0, /*tp_setattro*/
1197 0, /*tp_as_buffer*/
1198 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1199 0, /*tp_doc*/
1200 0, /*tp_traverse*/
1201 0, /*tp_clear*/
1202 0, /*tp_richcompare*/
1203 0, /*tp_weaklistoffset*/
1204 0, /*tp_iter*/
1205 0, /*tp_iternext*/
1206 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001207};
1208
Tim Peters0c322792002-07-17 16:49:03 +00001209static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001210 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001211 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001212 sizeof(compobject),
1213 0,
1214 (destructor)Decomp_dealloc, /*tp_dealloc*/
1215 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001216 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001217 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001218 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001219 0, /*tp_repr*/
1220 0, /*tp_as_number*/
1221 0, /*tp_as_sequence*/
1222 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001223 0, /*tp_hash*/
1224 0, /*tp_call*/
1225 0, /*tp_str*/
1226 0, /*tp_getattro*/
1227 0, /*tp_setattro*/
1228 0, /*tp_as_buffer*/
1229 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1230 0, /*tp_doc*/
1231 0, /*tp_traverse*/
1232 0, /*tp_clear*/
1233 0, /*tp_richcompare*/
1234 0, /*tp_weaklistoffset*/
1235 0, /*tp_iter*/
1236 0, /*tp_iternext*/
1237 Decomp_methods, /*tp_methods*/
1238 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001239};
1240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001241PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001242"The functions in this module allow compression and decompression using the\n"
1243"zlib library, which is based on GNU zip.\n"
1244"\n"
1245"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001246"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001247"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001248"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001249"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001250"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001251"\n"
1252"'wbits' is window buffer size.\n"
1253"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001254"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001255
Martin v. Löwis1a214512008-06-11 05:26:20 +00001256static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001257 PyModuleDef_HEAD_INIT,
1258 "zlib",
1259 zlib_module_documentation,
1260 -1,
1261 zlib_methods,
1262 NULL,
1263 NULL,
1264 NULL,
1265 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001266};
1267
Mark Hammond62b1ab12002-07-23 06:31:15 +00001268PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001269PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001270{
Fred Drake4baedc12002-04-01 14:53:37 +00001271 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001272 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001273 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001274 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001275 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001276 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001277 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001278 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001279
Fred Drake4baedc12002-04-01 14:53:37 +00001280 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1281 if (ZlibError != NULL) {
1282 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001283 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001284 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001285 PyModule_AddIntMacro(m, MAX_WBITS);
1286 PyModule_AddIntMacro(m, DEFLATED);
1287 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
1288 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1289 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1290 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1291 PyModule_AddIntMacro(m, Z_FILTERED);
1292 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1293 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001294
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001295 PyModule_AddIntMacro(m, Z_FINISH);
1296 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1297 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1298 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001299
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001300 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001301 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001302 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001303
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001304 ver = PyUnicode_FromString(zlibVersion());
1305 if (ver != NULL)
1306 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1307
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001308 PyModule_AddStringConstant(m, "__version__", "1.0");
1309
Martin v. Löwis1a214512008-06-11 05:26:20 +00001310 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001311}