blob: 888ef5388d63b18e984ecf2692d9b2e6526078f7 [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();
135#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000136 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000137}
138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000140"compress(string[, level]) -- Returned compressed string.\n"
141"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000142"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000143
Guido van Rossumfb221561997-04-29 15:38:09 +0000144static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000145PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000146{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000147 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000148 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200149 Byte *input, *output = NULL;
150 unsigned int length;
151 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000152 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000153
Jeremy Hylton9714f992001-10-16 21:19:45 +0000154 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000155 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000156 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200157
158 if (pinput.len > UINT_MAX) {
159 PyErr_SetString(PyExc_OverflowError,
160 "Size does not fit in an unsigned int");
161 goto error;
162 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000163 input = pinput.buf;
164 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000165
Jeremy Hylton9714f992001-10-16 21:19:45 +0000166 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000167
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000168 output = (Byte*)malloc(zst.avail_out);
169 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000170 PyErr_SetString(PyExc_MemoryError,
171 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200172 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000173 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000174
Jeremy Hylton9714f992001-10-16 21:19:45 +0000175 /* Past the point of no return. From here on out, we need to make sure
176 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000177
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000178 zst.zalloc = (alloc_func)NULL;
179 zst.zfree = (free_func)Z_NULL;
180 zst.next_out = (Byte *)output;
181 zst.next_in = (Byte *)input;
182 zst.avail_in = length;
183 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000184
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000185 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000186 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000188 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 PyErr_SetString(PyExc_MemoryError,
190 "Out of memory while compressing data");
191 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000192 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000193 PyErr_SetString(ZlibError,
194 "Bad compression level");
195 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000196 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000197 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000198 zlib_error(zst, err, "while compressing data");
199 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000200 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000201
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000202 Py_BEGIN_ALLOW_THREADS;
203 err = deflate(&zst, Z_FINISH);
204 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000205
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000206 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000207 zlib_error(zst, err, "while compressing data");
208 deflateEnd(&zst);
209 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000210 }
Tim Peters977e5402001-10-17 03:57:20 +0000211
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000212 err=deflateEnd(&zst);
213 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000215 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000216 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000218
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000219 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000220 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000221 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000222
Jeremy Hylton9714f992001-10-16 21:19:45 +0000223 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000224}
225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000226PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000227"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
228"\n"
229"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000230"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000231
Guido van Rossumfb221561997-04-29 15:38:09 +0000232static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000233PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000234{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200235 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000236 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200238 unsigned int length;
239 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000240 int wsize=DEF_WBITS;
241 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000242 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000243
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000244 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 &pinput, &wsize, &r_strlen))
246 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200247
248 if (pinput.len > UINT_MAX) {
249 PyErr_SetString(PyExc_OverflowError,
250 "Size does not fit in an unsigned int");
251 goto error;
252 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000253 input = pinput.buf;
254 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000255
Jeremy Hylton9714f992001-10-16 21:19:45 +0000256 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000257 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000258
Jeremy Hylton9714f992001-10-16 21:19:45 +0000259 zst.avail_in = length;
260 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000261
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200262 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
263 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000264
Jeremy Hylton9714f992001-10-16 21:19:45 +0000265 zst.zalloc = (alloc_func)NULL;
266 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000267 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000268 zst.next_in = (Byte *)input;
269 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000270
Jeremy Hylton9714f992001-10-16 21:19:45 +0000271 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000272 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000273 break;
Tim Peters977e5402001-10-17 03:57:20 +0000274 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000275 PyErr_SetString(PyExc_MemoryError,
276 "Out of memory while decompressing data");
277 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000278 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000279 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000280 zlib_error(zst, err, "while preparing to decompress data");
281 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000282 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000283
Jeremy Hylton9714f992001-10-16 21:19:45 +0000284 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 Py_BEGIN_ALLOW_THREADS
286 err=inflate(&zst, Z_FINISH);
287 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000288
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000289 switch(err) {
290 case(Z_STREAM_END):
291 break;
292 case(Z_BUF_ERROR):
293 /*
294 * If there is at least 1 byte of room according to zst.avail_out
295 * and we get this error, assume that it means zlib cannot
296 * process the inflate call() due to an error in the data.
297 */
298 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000299 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000300 inflateEnd(&zst);
301 goto error;
302 }
303 /* fall through */
304 case(Z_OK):
305 /* need more memory */
306 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
307 inflateEnd(&zst);
308 goto error;
309 }
310 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000311 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 zst.avail_out = r_strlen;
313 r_strlen = r_strlen << 1;
314 break;
315 default:
316 inflateEnd(&zst);
317 zlib_error(zst, err, "while decompressing data");
318 goto error;
319 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000320 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000321
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000322 err = inflateEnd(&zst);
323 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200324 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000326 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000327
Gregory P. Smith693fc462008-09-06 20:13:06 +0000328 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000329 goto error;
330
Martin v. Löwis423be952008-08-13 15:53:07 +0000331 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000332 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000333
334 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000335 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000336 Py_XDECREF(result_str);
337 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000338}
339
340static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200341PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000342{
Jeremy Hylton499000002001-10-16 21:59:35 +0000343 compobject *self;
344 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
345 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200346 Py_buffer zdict;
347 static char *kwlist[] = {"level", "method", "wbits",
348 "memLevel", "strategy", "zdict", NULL};
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000349
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200350 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
351 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
352 kwlist, &level, &method, &wbits,
353 &memLevel, &strategy, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000354 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000355
Jeremy Hylton499000002001-10-16 21:59:35 +0000356 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000357 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200358 goto error;
Jeremy Hylton499000002001-10-16 21:59:35 +0000359 self->zst.zalloc = (alloc_func)NULL;
360 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000361 self->zst.next_in = NULL;
362 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000363 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
364 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000365 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 self->is_initialised = 1;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200367 if (zdict.buf == NULL) {
368 goto success;
369 } else {
370 err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len);
371 switch (err) {
372 case (Z_OK):
373 goto success;
374 case (Z_STREAM_ERROR):
375 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
376 goto error;
377 default:
378 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
379 goto error;
380 }
381 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000382 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000383 PyErr_SetString(PyExc_MemoryError,
384 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200385 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000386 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000387 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200388 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000389 default:
390 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200391 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000392 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200393
394 error:
395 Py_XDECREF(self);
396 self = NULL;
397 success:
398 if (zdict.buf != NULL)
399 PyBuffer_Release(&zdict);
400 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000401}
402
403static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200404PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000405{
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200406 static char *kwlist[] = {"wbits", "zdict", NULL};
Jeremy Hylton499000002001-10-16 21:59:35 +0000407 int wbits=DEF_WBITS, err;
408 compobject *self;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200409 PyObject *zdict=NULL;
410
411 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
412 kwlist, &wbits, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000413 return NULL;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200414 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
415 PyErr_SetString(PyExc_TypeError,
416 "zdict argument must support the buffer protocol");
417 return NULL;
418 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000419
420 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000421 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000422 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000423 self->zst.zalloc = (alloc_func)NULL;
424 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000425 self->zst.next_in = NULL;
426 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200427 if (zdict != NULL) {
428 Py_INCREF(zdict);
429 self->zdict = zdict;
430 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000431 err = inflateInit2(&self->zst, wbits);
432 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000433 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000434 self->is_initialised = 1;
435 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000436 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000437 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000438 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
439 return NULL;
440 case (Z_MEM_ERROR):
441 Py_DECREF(self);
442 PyErr_SetString(PyExc_MemoryError,
443 "Can't allocate memory for decompression object");
444 return NULL;
445 default:
446 zlib_error(self->zst, err, "while creating decompression object");
447 Py_DECREF(self);
448 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000449 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000450}
451
452static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000453Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000454{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000455#ifdef WITH_THREAD
456 PyThread_free_lock(self->lock);
457#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000458 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000459 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200460 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000461 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000462}
463
464static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000465Comp_dealloc(compobject *self)
466{
467 if (self->is_initialised)
468 deflateEnd(&self->zst);
469 Dealloc(self);
470}
471
472static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000473Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000474{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000475 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000476 inflateEnd(&self->zst);
477 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000478}
479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000481"compress(data) -- Return a string containing data compressed.\n"
482"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000483"After calling this function, some of the input data may still\n"
484"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000485"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000486
487
Guido van Rossumfb221561997-04-29 15:38:09 +0000488static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000489PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000490{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200491 int err;
492 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000493 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200494 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000495 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000496 Byte *input;
497 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000498
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000499 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000500 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200501 if (pinput.len > UINT_MAX) {
502 PyErr_SetString(PyExc_OverflowError,
503 "Size does not fit in an unsigned int");
504 goto error_outer;
505 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000506 input = pinput.buf;
507 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000508
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200509 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
510 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000511
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000512 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000513
Jeremy Hylton9714f992001-10-16 21:19:45 +0000514 start_total_out = self->zst.total_out;
515 self->zst.avail_in = inplen;
516 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000517 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000518 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000519
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000520 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000521 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000522 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000523
Jeremy Hylton9714f992001-10-16 21:19:45 +0000524 /* while Z_OK and the output buffer is full, there might be more output,
525 so extend the output buffer and try again */
526 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000527 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000528 Py_DECREF(RetVal);
529 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000530 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000531 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000532 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000533 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000534 self->zst.avail_out = length;
535 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000536
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000537 Py_BEGIN_ALLOW_THREADS
538 err = deflate(&(self->zst), Z_NO_FLUSH);
539 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000540 }
Tim Peters977e5402001-10-17 03:57:20 +0000541 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000542 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000543 condition.
544 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000545
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000546 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200547 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000548 Py_DECREF(RetVal);
549 RetVal = NULL;
550 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000551 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000552 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000553 Py_DECREF(RetVal);
554 RetVal = NULL;
555 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000556
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000557 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000558 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200559 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000560 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000561 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000562}
563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000564PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000565"decompress(data, max_length) -- Return a string containing the decompressed\n"
566"version of the data.\n"
567"\n"
568"After calling this function, some of the input data may still be stored in\n"
569"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000570"Call the flush() method to clear these buffers.\n"
571"If the max_length parameter is specified then the return value will be\n"
572"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000573"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000574
Guido van Rossumfb221561997-04-29 15:38:09 +0000575static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000576PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000577{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200578 int err, max_length = 0;
579 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000580 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200581 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000582 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000583 Byte *input;
584 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000585
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000586 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000587 &max_length))
588 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200589 if (pinput.len > UINT_MAX) {
590 PyErr_SetString(PyExc_OverflowError,
591 "Size does not fit in an unsigned int");
592 goto error_outer;
593 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000594 input = pinput.buf;
595 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000596 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 PyErr_SetString(PyExc_ValueError,
598 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200599 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000600 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000601
Jeremy Hylton9714f992001-10-16 21:19:45 +0000602 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000603 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000604 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200605 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
606 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000607
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000608 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000609
Jeremy Hylton9714f992001-10-16 21:19:45 +0000610 start_total_out = self->zst.total_out;
611 self->zst.avail_in = inplen;
612 self->zst.next_in = input;
613 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000614 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000615
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000616 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000617 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000618 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000619
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200620 if (err == Z_NEED_DICT && self->zdict != NULL) {
621 Py_buffer zdict_buf;
622 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
623 Py_DECREF(RetVal);
624 RetVal = NULL;
625 goto error;
626 }
627 err = inflateSetDictionary(&(self->zst), zdict_buf.buf, zdict_buf.len);
628 PyBuffer_Release(&zdict_buf);
629 if (err != Z_OK) {
630 zlib_error(self->zst, err, "while decompressing data");
631 Py_DECREF(RetVal);
632 RetVal = NULL;
633 goto error;
634 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200635 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200636 Py_BEGIN_ALLOW_THREADS
637 err = inflate(&(self->zst), Z_SYNC_FLUSH);
638 Py_END_ALLOW_THREADS
639 }
640
Jeremy Hylton9714f992001-10-16 21:19:45 +0000641 /* While Z_OK and the output buffer is full, there might be more output.
642 So extend the output buffer and try again.
643 */
Tim Peters977e5402001-10-17 03:57:20 +0000644 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 /* If max_length set, don't continue decompressing if we've already
646 reached the limit.
647 */
648 if (max_length && length >= max_length)
649 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000650
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000651 /* otherwise, ... */
652 old_length = length;
653 length = length << 1;
654 if (max_length && length > max_length)
655 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000656
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000657 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000658 Py_DECREF(RetVal);
659 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000660 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000661 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000663 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000664 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000665
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 Py_BEGIN_ALLOW_THREADS
667 err = inflate(&(self->zst), Z_SYNC_FLUSH);
668 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000669 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000670
Jeremy Hylton9714f992001-10-16 21:19:45 +0000671 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200672 /* Not all of the compressed data could be accommodated in a buffer of
673 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000674 Py_DECREF(self->unconsumed_tail);
675 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
676 self->zst.avail_in);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200677 }
678 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
679 /* All of the compressed data was consumed. Clear unconsumed_tail. */
680 Py_DECREF(self->unconsumed_tail);
681 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
682 }
683 if (self->unconsumed_tail == NULL) {
684 Py_DECREF(RetVal);
685 RetVal = NULL;
686 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000687 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000688
Tim Peters977e5402001-10-17 03:57:20 +0000689 /* The end of the compressed data has been reached, so set the
690 unused_data attribute to a string containing the remainder of the
691 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000692 inflateEnd, but the old behaviour of only calling it on flush() is
693 preserved.
694 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000695 if (err == Z_STREAM_END) {
Nadeem Vawda39079942012-11-05 00:37:42 +0100696 if (self->zst.avail_in > 0) {
697 /* Append the leftover data to the existing value of unused_data. */
698 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
699 Py_ssize_t new_size = old_size + self->zst.avail_in;
700 PyObject *new_data;
701 if (new_size <= old_size) { /* Check for overflow. */
702 PyErr_NoMemory();
703 Py_DECREF(RetVal);
704 RetVal = NULL;
705 goto error;
706 }
707 new_data = PyBytes_FromStringAndSize(NULL, new_size);
708 if (new_data == NULL) {
709 Py_DECREF(RetVal);
710 RetVal = NULL;
711 goto error;
712 }
713 Py_MEMCPY(PyBytes_AS_STRING(new_data),
714 PyBytes_AS_STRING(self->unused_data), old_size);
715 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
716 self->zst.next_in, self->zst.avail_in);
717 Py_DECREF(self->unused_data);
718 self->unused_data = new_data;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 }
Nadeem Vawda1c385462011-08-13 15:22:40 +0200720 self->eof = 1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000721 /* We will only get Z_BUF_ERROR if the output buffer was full
722 but there wasn't more output when we tried again, so it is
723 not an error condition.
724 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000725 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200726 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000727 Py_DECREF(RetVal);
728 RetVal = NULL;
729 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000730 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000731
Gregory P. Smith693fc462008-09-06 20:13:06 +0000732 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000733 Py_DECREF(RetVal);
734 RetVal = NULL;
735 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000736
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000737 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000738 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200739 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000740 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000741 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000742}
743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000744PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000745"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000746"\n"
747"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000748"default value used when mode is not specified is Z_FINISH.\n"
749"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000750"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000751
Guido van Rossumfb221561997-04-29 15:38:09 +0000752static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000753PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000754{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000755 int err, length = DEFAULTALLOC;
756 PyObject *RetVal;
757 int flushmode = Z_FINISH;
758 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000759
Jeremy Hylton9714f992001-10-16 21:19:45 +0000760 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000761 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000762
Jeremy Hylton9714f992001-10-16 21:19:45 +0000763 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
764 doing any work at all; just return an empty string. */
765 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000766 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000767 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000768
Gregory P. Smith693fc462008-09-06 20:13:06 +0000769 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000770 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000771
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000772 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000773
Jeremy Hylton9714f992001-10-16 21:19:45 +0000774 start_total_out = self->zst.total_out;
775 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000776 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000777 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000778
779 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000780 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000781 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000782
Jeremy Hylton9714f992001-10-16 21:19:45 +0000783 /* while Z_OK and the output buffer is full, there might be more output,
784 so extend the output buffer and try again */
785 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000786 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000787 Py_DECREF(RetVal);
788 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000789 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000790 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000791 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000792 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000793 self->zst.avail_out = length;
794 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000795
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000796 Py_BEGIN_ALLOW_THREADS
797 err = deflate(&(self->zst), flushmode);
798 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000799 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000800
Jeremy Hylton9714f992001-10-16 21:19:45 +0000801 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000802 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000803 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000804 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000805 err = deflateEnd(&(self->zst));
806 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200807 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000808 Py_DECREF(RetVal);
809 RetVal = NULL;
810 goto error;
811 }
812 else
813 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000814
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000815 /* We will only get Z_BUF_ERROR if the output buffer was full
816 but there wasn't more output when we tried again, so it is
817 not an error condition.
818 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000819 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000820 zlib_error(self->zst, err, "while flushing");
821 Py_DECREF(RetVal);
822 RetVal = NULL;
823 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000824 }
Tim Peters977e5402001-10-17 03:57:20 +0000825
Gregory P. Smith693fc462008-09-06 20:13:06 +0000826 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000827 Py_DECREF(RetVal);
828 RetVal = NULL;
829 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000830
Tim Peters977e5402001-10-17 03:57:20 +0000831 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000832 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000833
834 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000835}
836
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000837#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838PyDoc_STRVAR(comp_copy__doc__,
839"copy() -- Return a copy of the compression object.");
840
841static PyObject *
842PyZlib_copy(compobject *self)
843{
844 compobject *retval = NULL;
845 int err;
846
847 retval = newcompobject(&Comptype);
848 if (!retval) return NULL;
849
850 /* Copy the zstream state
851 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
852 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000853 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854 err = deflateCopy(&retval->zst, &self->zst);
855 switch(err) {
856 case(Z_OK):
857 break;
858 case(Z_STREAM_ERROR):
859 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
860 goto error;
861 case(Z_MEM_ERROR):
862 PyErr_SetString(PyExc_MemoryError,
863 "Can't allocate memory for compression object");
864 goto error;
865 default:
866 zlib_error(self->zst, err, "while copying compression object");
867 goto error;
868 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000869 Py_INCREF(self->unused_data);
870 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200871 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872 Py_XDECREF(retval->unused_data);
873 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200874 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875 retval->unused_data = self->unused_data;
876 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200877 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200878 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879
880 /* Mark it as being initialized */
881 retval->is_initialised = 1;
882
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000883 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000884 return (PyObject *)retval;
885
886error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000887 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000888 Py_XDECREF(retval);
889 return NULL;
890}
891
892PyDoc_STRVAR(decomp_copy__doc__,
893"copy() -- Return a copy of the decompression object.");
894
895static PyObject *
896PyZlib_uncopy(compobject *self)
897{
898 compobject *retval = NULL;
899 int err;
900
901 retval = newcompobject(&Decomptype);
902 if (!retval) return NULL;
903
904 /* Copy the zstream state
905 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
906 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000907 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908 err = inflateCopy(&retval->zst, &self->zst);
909 switch(err) {
910 case(Z_OK):
911 break;
912 case(Z_STREAM_ERROR):
913 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
914 goto error;
915 case(Z_MEM_ERROR):
916 PyErr_SetString(PyExc_MemoryError,
917 "Can't allocate memory for decompression object");
918 goto error;
919 default:
920 zlib_error(self->zst, err, "while copying decompression object");
921 goto error;
922 }
923
924 Py_INCREF(self->unused_data);
925 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200926 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000927 Py_XDECREF(retval->unused_data);
928 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200929 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930 retval->unused_data = self->unused_data;
931 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200932 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200933 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000934
935 /* Mark it as being initialized */
936 retval->is_initialised = 1;
937
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000938 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939 return (PyObject *)retval;
940
941error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000942 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943 Py_XDECREF(retval);
944 return NULL;
945}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000946#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000948PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000949"flush( [length] ) -- Return a string containing any remaining\n"
950"decompressed data. length, if given, is the initial size of the\n"
951"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000952"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000953"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000954
Guido van Rossumfb221561997-04-29 15:38:09 +0000955static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000956PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000957{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000958 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000959 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000960 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000961
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000962 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000963 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000964 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000965 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
966 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000967 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000968 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000969 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000970
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000971
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000972 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000973
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000974 start_total_out = self->zst.total_out;
975 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000976 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000977
978 Py_BEGIN_ALLOW_THREADS
979 err = inflate(&(self->zst), Z_FINISH);
980 Py_END_ALLOW_THREADS
981
982 /* while Z_OK and the output buffer is full, there might be more output,
983 so extend the output buffer and try again */
984 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000985 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000986 Py_DECREF(retval);
987 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000988 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000989 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000990 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
991 self->zst.avail_out = length;
992 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000993
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000994 Py_BEGIN_ALLOW_THREADS
995 err = inflate(&(self->zst), Z_FINISH);
996 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000997 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000998
Nadeem Vawda3bf71c52011-08-13 15:42:50 +0200999 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001000 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001001 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001002 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001003 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001004 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001005 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001006 Py_DECREF(retval);
1007 retval = NULL;
1008 goto error;
1009 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001010 }
Gregory P. Smith693fc462008-09-06 20:13:06 +00001011 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +00001012 Py_DECREF(retval);
1013 retval = NULL;
1014 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001015
1016error:
1017
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001018 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001019
Jeremy Hylton9714f992001-10-16 21:19:45 +00001020 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001021}
1022
1023static PyMethodDef comp_methods[] =
1024{
Tim Peters977e5402001-10-17 03:57:20 +00001025 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001026 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001027 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001028 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001029#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
1031 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001032#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001033 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001034};
1035
1036static PyMethodDef Decomp_methods[] =
1037{
Tim Peters977e5402001-10-17 03:57:20 +00001038 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001039 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001040 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001041 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001042#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1044 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001045#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001046 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001047};
1048
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001049#define COMP_OFF(x) offsetof(compobject, x)
1050static PyMemberDef Decomp_members[] = {
1051 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1052 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001053 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001054 {NULL},
1055};
Guido van Rossumfb221561997-04-29 15:38:09 +00001056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001057PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001058"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1059"\n"
1060"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001061"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001062
Guido van Rossumfb221561997-04-29 15:38:09 +00001063static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001064PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001065{
Christian Heimescc47b052008-03-25 14:56:36 +00001066 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001067 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +00001068
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001069 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1070 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001071 /* Releasing the GIL for very small buffers is inefficient
1072 and may lower performance */
1073 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001074 unsigned char *buf = pbuf.buf;
1075 Py_ssize_t len = pbuf.len;
1076
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001077 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001078 /* Avoid truncation of length for very large buffers. adler32() takes
1079 length as an unsigned int, which may be narrower than Py_ssize_t. */
1080 while (len > (size_t) UINT_MAX) {
1081 adler32val = adler32(adler32val, buf, UINT_MAX);
1082 buf += (size_t) UINT_MAX;
1083 len -= (size_t) UINT_MAX;
1084 }
1085 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001086 Py_END_ALLOW_THREADS
1087 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001088 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001089 }
1090 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001091 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001092}
Tim Peters977e5402001-10-17 03:57:20 +00001093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001094PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001095"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1096"\n"
1097"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001098"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001099
1100static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001101PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001102{
Christian Heimescc47b052008-03-25 14:56:36 +00001103 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001104 Py_buffer pbuf;
1105 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001106
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001107 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001108 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001109 /* Releasing the GIL for very small buffers is inefficient
1110 and may lower performance */
1111 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001112 unsigned char *buf = pbuf.buf;
1113 Py_ssize_t len = pbuf.len;
1114
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001115 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001116 /* Avoid truncation of length for very large buffers. crc32() takes
1117 length as an unsigned int, which may be narrower than Py_ssize_t. */
1118 while (len > (size_t) UINT_MAX) {
1119 crc32val = crc32(crc32val, buf, UINT_MAX);
1120 buf += (size_t) UINT_MAX;
1121 len -= (size_t) UINT_MAX;
1122 }
1123 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001124 Py_END_ALLOW_THREADS
1125 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001126 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001127 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001128 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001129 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001130}
Tim Peters977e5402001-10-17 03:57:20 +00001131
Guido van Rossumfb221561997-04-29 15:38:09 +00001132
1133static PyMethodDef zlib_methods[] =
1134{
Tim Peters977e5402001-10-17 03:57:20 +00001135 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001136 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001137 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001138 compress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001139 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001140 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001141 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1142 crc32__doc__},
1143 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001144 decompress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001145 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001146 decompressobj__doc__},
1147 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001148};
1149
Tim Peters0c322792002-07-17 16:49:03 +00001150static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001151 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001152 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001153 sizeof(compobject),
1154 0,
1155 (destructor)Comp_dealloc, /*tp_dealloc*/
1156 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001157 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001158 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001159 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001160 0, /*tp_repr*/
1161 0, /*tp_as_number*/
1162 0, /*tp_as_sequence*/
1163 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001164 0, /*tp_hash*/
1165 0, /*tp_call*/
1166 0, /*tp_str*/
1167 0, /*tp_getattro*/
1168 0, /*tp_setattro*/
1169 0, /*tp_as_buffer*/
1170 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1171 0, /*tp_doc*/
1172 0, /*tp_traverse*/
1173 0, /*tp_clear*/
1174 0, /*tp_richcompare*/
1175 0, /*tp_weaklistoffset*/
1176 0, /*tp_iter*/
1177 0, /*tp_iternext*/
1178 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001179};
1180
Tim Peters0c322792002-07-17 16:49:03 +00001181static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001182 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001183 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001184 sizeof(compobject),
1185 0,
1186 (destructor)Decomp_dealloc, /*tp_dealloc*/
1187 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001188 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001189 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001190 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001191 0, /*tp_repr*/
1192 0, /*tp_as_number*/
1193 0, /*tp_as_sequence*/
1194 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001195 0, /*tp_hash*/
1196 0, /*tp_call*/
1197 0, /*tp_str*/
1198 0, /*tp_getattro*/
1199 0, /*tp_setattro*/
1200 0, /*tp_as_buffer*/
1201 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1202 0, /*tp_doc*/
1203 0, /*tp_traverse*/
1204 0, /*tp_clear*/
1205 0, /*tp_richcompare*/
1206 0, /*tp_weaklistoffset*/
1207 0, /*tp_iter*/
1208 0, /*tp_iternext*/
1209 Decomp_methods, /*tp_methods*/
1210 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001211};
1212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001213PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001214"The functions in this module allow compression and decompression using the\n"
1215"zlib library, which is based on GNU zip.\n"
1216"\n"
1217"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1218"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001219"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001220"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001221"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001222"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001223"\n"
1224"'wbits' is window buffer size.\n"
1225"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001226"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001227
Martin v. Löwis1a214512008-06-11 05:26:20 +00001228static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001229 PyModuleDef_HEAD_INIT,
1230 "zlib",
1231 zlib_module_documentation,
1232 -1,
1233 zlib_methods,
1234 NULL,
1235 NULL,
1236 NULL,
1237 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001238};
1239
Mark Hammond62b1ab12002-07-23 06:31:15 +00001240PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001241PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001242{
Fred Drake4baedc12002-04-01 14:53:37 +00001243 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001244 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001245 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001246 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001247 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001248 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001249 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001250 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001251
Fred Drake4baedc12002-04-01 14:53:37 +00001252 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1253 if (ZlibError != NULL) {
1254 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001255 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001256 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001257 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1258 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1259 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1260 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1261 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1262 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1263 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1264 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1265 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001266
Jeremy Hylton9714f992001-10-16 21:19:45 +00001267 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1268 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1269 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1270 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001271
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001272 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001273 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001274 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001275
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001276 ver = PyUnicode_FromString(zlibVersion());
1277 if (ver != NULL)
1278 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1279
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001280 PyModule_AddStringConstant(m, "__version__", "1.0");
1281
Martin v. Löwis1a214512008-06-11 05:26:20 +00001282 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001283}