blob: 78ee10b5a6c9c1118a58e463501ecd2c1e922ad5 [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Guido van Rossumfb221561997-04-29 15:38:09 +00006
Guido van Rossum97b54571997-06-03 22:21:47 +00007#include "Python.h"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00008#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +00009#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000012#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000013 #include "pythread.h"
14 #define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000020 #define ENTER_ZLIB(obj)
21 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000022#endif
23
Guido van Rossumfb221561997-04-29 15:38:09 +000024/* The following parameters are copied from zutil.h, version 0.95 */
25#define DEFLATED 8
26#if MAX_MEM_LEVEL >= 8
27# define DEF_MEM_LEVEL 8
28#else
29# define DEF_MEM_LEVEL MAX_MEM_LEVEL
30#endif
31#define DEF_WBITS MAX_WBITS
32
Guido van Rossumb729a1d1999-04-07 20:23:17 +000033/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
34#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Jeremy Hylton938ace62002-07-17 16:30:39 +000036static PyTypeObject Comptype;
37static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000038
39static PyObject *ZlibError;
40
Tim Peters977e5402001-10-17 03:57:20 +000041typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000042{
Jeremy Hylton9714f992001-10-16 21:19:45 +000043 PyObject_HEAD
44 z_stream zst;
45 PyObject *unused_data;
46 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020047 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000048 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020049 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000050 #ifdef WITH_THREAD
51 PyThread_type_lock lock;
52 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000053} compobject;
54
Jeremy Hylton0965e082001-10-16 21:56:09 +000055static void
56zlib_error(z_stream zst, int err, char *msg)
57{
Nadeem Vawda524148a2011-08-28 11:26:46 +020058 const char *zmsg = Z_NULL;
59 /* In case of a version mismatch, zst.msg won't be initialized.
60 Check for this case first, before looking at zst.msg. */
61 if (err == Z_VERSION_ERROR)
62 zmsg = "library version mismatch";
63 if (zmsg == Z_NULL)
64 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000065 if (zmsg == Z_NULL) {
66 switch (err) {
67 case Z_BUF_ERROR:
68 zmsg = "incomplete or truncated stream";
69 break;
70 case Z_STREAM_ERROR:
71 zmsg = "inconsistent stream state";
72 break;
73 case Z_DATA_ERROR:
74 zmsg = "invalid input data";
75 break;
76 }
77 }
78 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000080 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000081 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000082}
83
Larry Hastings61272b72014-01-07 12:41:53 -080084/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080085module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080086class zlib.Compress "compobject *" "&Comptype"
87class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080088[clinic start generated code]*/
89/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080090
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091PyDoc_STRVAR(compressobj__doc__,
Nadeem Vawda2180c972012-06-22 01:40:49 +020092"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
93" strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020094" -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +000095"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020096"level is the compression level (an integer in the range 0-9; default is 6).\n"
97"Higher compression levels are slower, but produce smaller results.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020098"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020099"method is the compression algorithm. If given, this must be DEFLATED.\n"
100"\n"
101"wbits is the base two logarithm of the window size (range: 8..15).\n"
102"\n"
103"memlevel controls the amount of memory used for internal compression state.\n"
104"Valid values range from 1 to 9. Higher values result in higher memory usage,\n"
105"faster compression, and smaller output.\n"
106"\n"
107"strategy is used to tune the compression algorithm. Possible values are\n"
108"Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
109"\n"
110"zdict is the predefined compression dictionary - a sequence of bytes\n"
111"containing subsequences that are likely to occur in the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000112
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000113PyDoc_STRVAR(decompressobj__doc__,
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200114"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000115"\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200116"Optional arg wbits is the window buffer size.\n"
117"\n"
118"Optional arg zdict is the predefined compression dictionary. This must be\n"
119"the same dictionary as used by the compressor that produced the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000120
Guido van Rossumfb221561997-04-29 15:38:09 +0000121static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000122newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000123{
Tim Peters977e5402001-10-17 03:57:20 +0000124 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 self = PyObject_New(compobject, type);
126 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000127 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200128 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200130 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000131 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000132 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000133 Py_DECREF(self);
134 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000135 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000136 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000137 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000138 Py_DECREF(self);
139 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000140 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000141#ifdef WITH_THREAD
142 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200143 if (self->lock == NULL) {
144 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
145 return NULL;
146 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000147#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000148 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000149}
150
Victor Stinner5064a522013-07-07 16:50:27 +0200151static void*
152PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
153{
154 if (items > (size_t)PY_SSIZE_T_MAX / size)
155 return NULL;
156 /* PyMem_Malloc() cannot be used: the GIL is not held when
157 inflate() and deflate() are called */
158 return PyMem_RawMalloc(items * size);
159}
160
161static void
162PyZlib_Free(voidpf ctx, void *ptr)
163{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200164 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200165}
166
Larry Hastings61272b72014-01-07 12:41:53 -0800167/*[clinic input]
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800168
Larry Hastingsebdcb502013-11-23 14:54:00 -0800169zlib.compress
170 bytes: Py_buffer
171 Binary data to be compressed.
172 [
173 level: int
174 Compression level, in 0-9.
175 ]
176 /
177
178Returns compressed string.
179
Larry Hastings61272b72014-01-07 12:41:53 -0800180[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800181
182PyDoc_STRVAR(zlib_compress__doc__,
Larry Hastings5c661892014-01-24 06:17:25 -0800183"compress(module, bytes, [level])\n"
Larry Hastingsebdcb502013-11-23 14:54:00 -0800184"Returns compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000185"\n"
Larry Hastingsebdcb502013-11-23 14:54:00 -0800186" bytes\n"
187" Binary data to be compressed.\n"
188" level\n"
189" Compression level, in 0-9.");
190
191#define ZLIB_COMPRESS_METHODDEF \
192 {"compress", (PyCFunction)zlib_compress, METH_VARARGS, zlib_compress__doc__},
Guido van Rossum3c540301997-06-03 22:21:03 +0000193
Guido van Rossumfb221561997-04-29 15:38:09 +0000194static PyObject *
Larry Hastingsebdcb502013-11-23 14:54:00 -0800195zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level);
196
197static PyObject *
198zlib_compress(PyModuleDef *module, PyObject *args)
199{
200 PyObject *return_value = NULL;
Larry Hastings4a55fc52014-01-12 11:09:57 -0800201 Py_buffer bytes = {NULL, NULL};
Larry Hastingsebdcb502013-11-23 14:54:00 -0800202 int group_right_1 = 0;
203 int level = 0;
204
Larry Hastings2a727912014-01-16 11:32:01 -0800205 switch (PyTuple_GET_SIZE(args)) {
Larry Hastingsebdcb502013-11-23 14:54:00 -0800206 case 1:
207 if (!PyArg_ParseTuple(args, "y*:compress", &bytes))
Larry Hastings46258262014-01-22 03:05:49 -0800208 goto exit;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800209 break;
210 case 2:
211 if (!PyArg_ParseTuple(args, "y*i:compress", &bytes, &level))
Larry Hastings46258262014-01-22 03:05:49 -0800212 goto exit;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800213 group_right_1 = 1;
214 break;
215 default:
216 PyErr_SetString(PyExc_TypeError, "zlib.compress requires 1 to 2 arguments");
Larry Hastings46258262014-01-22 03:05:49 -0800217 goto exit;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800218 }
219 return_value = zlib_compress_impl(module, &bytes, group_right_1, level);
220
Larry Hastings46258262014-01-22 03:05:49 -0800221exit:
Larry Hastingsebdcb502013-11-23 14:54:00 -0800222 /* Cleanup for bytes */
Larry Hastings4a55fc52014-01-12 11:09:57 -0800223 if (bytes.obj)
Larry Hastingsebdcb502013-11-23 14:54:00 -0800224 PyBuffer_Release(&bytes);
225
226 return return_value;
227}
228
229static PyObject *
230zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level)
Larry Hastings5c661892014-01-24 06:17:25 -0800231/*[clinic end generated code: checksum=ce8d4c0a17ecd79c3ffcc032dcdf8ac6830ded1e]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000232{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000233 PyObject *ReturnVal = NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200234 Byte *input, *output = NULL;
235 unsigned int length;
Larry Hastingsebdcb502013-11-23 14:54:00 -0800236 int err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000238
Larry Hastingsebdcb502013-11-23 14:54:00 -0800239 if (!group_right_1)
240 level = Z_DEFAULT_COMPRESSION;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200241
Larry Hastingsebdcb502013-11-23 14:54:00 -0800242 if ((size_t)bytes->len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200243 PyErr_SetString(PyExc_OverflowError,
244 "Size does not fit in an unsigned int");
245 goto error;
246 }
Larry Hastingsebdcb502013-11-23 14:54:00 -0800247 input = bytes->buf;
248 length = (unsigned int)bytes->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000249
Jeremy Hylton9714f992001-10-16 21:19:45 +0000250 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000251
Victor Stinnerb6404912013-07-07 16:21:41 +0200252 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000253 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000254 PyErr_SetString(PyExc_MemoryError,
255 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200256 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000257 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000258
Jeremy Hylton9714f992001-10-16 21:19:45 +0000259 /* Past the point of no return. From here on out, we need to make sure
260 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000261
Victor Stinner5064a522013-07-07 16:50:27 +0200262 zst.opaque = NULL;
263 zst.zalloc = PyZlib_Malloc;
264 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000265 zst.next_out = (Byte *)output;
266 zst.next_in = (Byte *)input;
267 zst.avail_in = length;
268 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000269
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000270 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000271 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000273 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000274 PyErr_SetString(PyExc_MemoryError,
275 "Out of memory while compressing data");
276 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000277 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000278 PyErr_SetString(ZlibError,
279 "Bad compression level");
280 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000281 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000282 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000283 zlib_error(zst, err, "while compressing data");
284 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000285 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000286
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000287 Py_BEGIN_ALLOW_THREADS;
288 err = deflate(&zst, Z_FINISH);
289 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000290
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000291 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000292 zlib_error(zst, err, "while compressing data");
293 deflateEnd(&zst);
294 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000295 }
Tim Peters977e5402001-10-17 03:57:20 +0000296
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000297 err=deflateEnd(&zst);
298 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000299 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000300 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000301 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000302 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000303
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000304 error:
Victor Stinnerb6404912013-07-07 16:21:41 +0200305 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000306
Jeremy Hylton9714f992001-10-16 21:19:45 +0000307 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000308}
309
Larry Hastings61272b72014-01-07 12:41:53 -0800310/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100311
312class uint_converter(CConverter):
313 type = 'unsigned int'
314 converter = 'uint_converter'
315
Larry Hastings61272b72014-01-07 12:41:53 -0800316[python start generated code]*/
317/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100318
319static int
320uint_converter(PyObject *obj, void *ptr)
321{
322 long val;
323 unsigned long uval;
324
325 val = PyLong_AsLong(obj);
326 if (val == -1 && PyErr_Occurred()) {
327 uval = PyLong_AsUnsignedLong(obj);
328 if (uval == (unsigned long)-1 && PyErr_Occurred())
329 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100330 }
331 else {
332 if (val < 0) {
333 PyErr_SetString(PyExc_ValueError,
334 "value must be positive");
335 return 0;
336 }
337 uval = (unsigned long)val;
338 }
339
Victor Stinner5c867332014-01-03 12:26:12 +0100340 if (uval > UINT_MAX) {
341 PyErr_SetString(PyExc_OverflowError,
342 "Python int too large for C unsigned int");
343 return 0;
344 }
345
Victor Stinnere079edd2013-11-21 22:33:21 +0100346 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int);
347 return 1;
348}
349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000350PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000351"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
352"\n"
353"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000354"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000355
Guido van Rossumfb221561997-04-29 15:38:09 +0000356static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000357PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000358{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200359 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000360 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000361 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200362 unsigned int length;
363 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000364 int wsize=DEF_WBITS;
Victor Stinnere079edd2013-11-21 22:33:21 +0100365 unsigned int bufsize = DEFAULTALLOC, new_bufsize;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000366 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000367
Victor Stinnere079edd2013-11-21 22:33:21 +0100368 if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
369 &pinput, &wsize, uint_converter, &bufsize))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000370 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200371
Victor Stinnere079edd2013-11-21 22:33:21 +0100372 if ((size_t)pinput.len > UINT_MAX) {
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200373 PyErr_SetString(PyExc_OverflowError,
374 "Size does not fit in an unsigned int");
375 goto error;
376 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000377 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100378 length = (unsigned int)pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000379
Victor Stinnere079edd2013-11-21 22:33:21 +0100380 if (bufsize == 0)
381 bufsize = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000382
Jeremy Hylton9714f992001-10-16 21:19:45 +0000383 zst.avail_in = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100384 zst.avail_out = bufsize;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000385
Victor Stinnere079edd2013-11-21 22:33:21 +0100386 if (!(result_str = PyBytes_FromStringAndSize(NULL, bufsize)))
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200387 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000388
Victor Stinner5064a522013-07-07 16:50:27 +0200389 zst.opaque = NULL;
390 zst.zalloc = PyZlib_Malloc;
391 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000392 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000393 zst.next_in = (Byte *)input;
394 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000395
Jeremy Hylton9714f992001-10-16 21:19:45 +0000396 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000397 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000398 break;
Tim Peters977e5402001-10-17 03:57:20 +0000399 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000400 PyErr_SetString(PyExc_MemoryError,
401 "Out of memory while decompressing data");
402 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000403 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000404 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000405 zlib_error(zst, err, "while preparing to decompress data");
406 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000407 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000408
Jeremy Hylton9714f992001-10-16 21:19:45 +0000409 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000410 Py_BEGIN_ALLOW_THREADS
411 err=inflate(&zst, Z_FINISH);
412 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000413
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000414 switch(err) {
415 case(Z_STREAM_END):
416 break;
417 case(Z_BUF_ERROR):
418 /*
419 * If there is at least 1 byte of room according to zst.avail_out
420 * and we get this error, assume that it means zlib cannot
421 * process the inflate call() due to an error in the data.
422 */
423 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000424 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000425 inflateEnd(&zst);
426 goto error;
427 }
428 /* fall through */
429 case(Z_OK):
430 /* need more memory */
Victor Stinnere079edd2013-11-21 22:33:21 +0100431 if (bufsize <= (UINT_MAX >> 1))
432 new_bufsize = bufsize << 1;
433 else
434 new_bufsize = UINT_MAX;
435 if (_PyBytes_Resize(&result_str, new_bufsize) < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 inflateEnd(&zst);
437 goto error;
438 }
439 zst.next_out =
Victor Stinnere079edd2013-11-21 22:33:21 +0100440 (unsigned char *)PyBytes_AS_STRING(result_str) + bufsize;
441 zst.avail_out = bufsize;
442 bufsize = new_bufsize;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000443 break;
444 default:
445 inflateEnd(&zst);
446 zlib_error(zst, err, "while decompressing data");
447 goto error;
448 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000449 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000450
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000451 err = inflateEnd(&zst);
452 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200453 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000454 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000455 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000456
Gregory P. Smith693fc462008-09-06 20:13:06 +0000457 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000458 goto error;
459
Martin v. Löwis423be952008-08-13 15:53:07 +0000460 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000461 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000462
463 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000464 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000465 Py_XDECREF(result_str);
466 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000467}
468
469static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200470PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000471{
Victor Stinnere079edd2013-11-21 22:33:21 +0100472 compobject *self = NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000473 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
474 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200475 Py_buffer zdict;
476 static char *kwlist[] = {"level", "method", "wbits",
477 "memLevel", "strategy", "zdict", NULL};
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000478
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200479 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
480 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
481 kwlist, &level, &method, &wbits,
482 &memLevel, &strategy, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000483 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000484
Victor Stinnere079edd2013-11-21 22:33:21 +0100485 if (zdict.buf != NULL && (size_t)zdict.len > UINT_MAX) {
486 PyErr_SetString(PyExc_OverflowError,
487 "zdict length does not fit in an unsigned int");
488 goto error;
489 }
490
Jeremy Hylton499000002001-10-16 21:59:35 +0000491 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000492 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200493 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200494 self->zst.opaque = NULL;
495 self->zst.zalloc = PyZlib_Malloc;
496 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000497 self->zst.next_in = NULL;
498 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000499 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
500 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000501 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000502 self->is_initialised = 1;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200503 if (zdict.buf == NULL) {
504 goto success;
505 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100506 err = deflateSetDictionary(&self->zst,
507 zdict.buf, (unsigned int)zdict.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200508 switch (err) {
509 case (Z_OK):
510 goto success;
511 case (Z_STREAM_ERROR):
512 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
513 goto error;
514 default:
515 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
516 goto error;
517 }
518 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000519 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000520 PyErr_SetString(PyExc_MemoryError,
521 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200522 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000523 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000524 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200525 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000526 default:
527 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200528 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000529 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200530
531 error:
532 Py_XDECREF(self);
533 self = NULL;
534 success:
535 if (zdict.buf != NULL)
536 PyBuffer_Release(&zdict);
537 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000538}
539
540static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200541PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000542{
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200543 static char *kwlist[] = {"wbits", "zdict", NULL};
Jeremy Hylton499000002001-10-16 21:59:35 +0000544 int wbits=DEF_WBITS, err;
545 compobject *self;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200546 PyObject *zdict=NULL;
547
548 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
549 kwlist, &wbits, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000550 return NULL;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200551 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
552 PyErr_SetString(PyExc_TypeError,
553 "zdict argument must support the buffer protocol");
554 return NULL;
555 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000556
557 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000558 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000559 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200560 self->zst.opaque = NULL;
561 self->zst.zalloc = PyZlib_Malloc;
562 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000563 self->zst.next_in = NULL;
564 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200565 if (zdict != NULL) {
566 Py_INCREF(zdict);
567 self->zdict = zdict;
568 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000569 err = inflateInit2(&self->zst, wbits);
570 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000571 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000572 self->is_initialised = 1;
573 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000574 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000575 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000576 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
577 return NULL;
578 case (Z_MEM_ERROR):
579 Py_DECREF(self);
580 PyErr_SetString(PyExc_MemoryError,
581 "Can't allocate memory for decompression object");
582 return NULL;
583 default:
584 zlib_error(self->zst, err, "while creating decompression object");
585 Py_DECREF(self);
586 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000587 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000588}
589
590static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000591Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000592{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000593#ifdef WITH_THREAD
594 PyThread_free_lock(self->lock);
595#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000596 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000597 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200598 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000599 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000600}
601
602static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000603Comp_dealloc(compobject *self)
604{
605 if (self->is_initialised)
606 deflateEnd(&self->zst);
607 Dealloc(self);
608}
609
610static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000611Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000612{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000613 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000614 inflateEnd(&self->zst);
615 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000616}
617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000618PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000619"compress(data) -- Return a string containing data compressed.\n"
620"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000621"After calling this function, some of the input data may still\n"
622"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000623"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000624
625
Guido van Rossumfb221561997-04-29 15:38:09 +0000626static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000627PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000628{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200629 int err;
630 unsigned int inplen;
Victor Stinnere079edd2013-11-21 22:33:21 +0100631 unsigned int length = DEFAULTALLOC, new_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200632 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000633 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000634 Byte *input;
635 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000636
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000637 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000638 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +0100639 if ((size_t)pinput.len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200640 PyErr_SetString(PyExc_OverflowError,
641 "Size does not fit in an unsigned int");
642 goto error_outer;
643 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000644 input = pinput.buf;
Victor Stinnere079edd2013-11-21 22:33:21 +0100645 inplen = (unsigned int)pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000646
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200647 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
648 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000649
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000650 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000651
Jeremy Hylton9714f992001-10-16 21:19:45 +0000652 start_total_out = self->zst.total_out;
653 self->zst.avail_in = inplen;
654 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000655 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000656 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000657
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000658 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000659 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000660 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000661
Jeremy Hylton9714f992001-10-16 21:19:45 +0000662 /* while Z_OK and the output buffer is full, there might be more output,
663 so extend the output buffer and try again */
664 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100665 if (length <= (UINT_MAX >> 1))
666 new_length = length << 1;
667 else
668 new_length = UINT_MAX;
669 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200670 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000671 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000672 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000673 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000674 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000675 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100676 length = new_length;
Tim Peters977e5402001-10-17 03:57:20 +0000677
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000678 Py_BEGIN_ALLOW_THREADS
679 err = deflate(&(self->zst), Z_NO_FLUSH);
680 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000681 }
Tim Peters977e5402001-10-17 03:57:20 +0000682 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000683 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000684 condition.
685 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000686
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000687 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200688 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000689 Py_DECREF(RetVal);
690 RetVal = NULL;
691 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000692 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000693 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200694 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000695 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000696
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000697 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000698 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200699 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000700 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000701 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000702}
703
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100704/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
705 self->unused_data or self->unconsumed_tail, as appropriate. */
706static int
707save_unconsumed_input(compobject *self, int err)
708{
709 if (err == Z_STREAM_END) {
710 /* The end of the compressed data has been reached. Store the leftover
711 input data in self->unused_data. */
712 if (self->zst.avail_in > 0) {
713 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
714 Py_ssize_t new_size;
715 PyObject *new_data;
Victor Stinnere079edd2013-11-21 22:33:21 +0100716 if ((size_t)self->zst.avail_in > (size_t)UINT_MAX - (size_t)old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100717 PyErr_NoMemory();
718 return -1;
719 }
720 new_size = old_size + self->zst.avail_in;
721 new_data = PyBytes_FromStringAndSize(NULL, new_size);
722 if (new_data == NULL)
723 return -1;
724 Py_MEMCPY(PyBytes_AS_STRING(new_data),
725 PyBytes_AS_STRING(self->unused_data), old_size);
726 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
727 self->zst.next_in, self->zst.avail_in);
728 Py_DECREF(self->unused_data);
729 self->unused_data = new_data;
730 self->zst.avail_in = 0;
731 }
732 }
733 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
734 /* This code handles two distinct cases:
735 1. Output limit was reached. Save leftover input in unconsumed_tail.
736 2. All input data was consumed. Clear unconsumed_tail. */
737 PyObject *new_data = PyBytes_FromStringAndSize(
738 (char *)self->zst.next_in, self->zst.avail_in);
739 if (new_data == NULL)
740 return -1;
741 Py_DECREF(self->unconsumed_tail);
742 self->unconsumed_tail = new_data;
743 }
744 return 0;
745}
746
Larry Hastings61272b72014-01-07 12:41:53 -0800747/*[clinic input]
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800748
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800749zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700750
751 data: Py_buffer
752 The binary data to decompress.
Victor Stinnere079edd2013-11-21 22:33:21 +0100753 max_length: uint = 0
Larry Hastings31826802013-10-19 00:09:25 -0700754 The maximum allowable length of the decompressed data.
755 Unconsumed input data will be stored in
756 the unconsumed_tail attribute.
757 /
758
759Return a string containing the decompressed version of the data.
760
761After calling this function, some of the input data may still be stored in
762internal buffers for later processing.
763Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800764[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700765
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800766PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Larry Hastings5c661892014-01-24 06:17:25 -0800767"decompress(self, data, max_length=0)\n"
Larry Hastings31826802013-10-19 00:09:25 -0700768"Return a string containing the decompressed version of the data.\n"
769"\n"
Larry Hastings31826802013-10-19 00:09:25 -0700770" data\n"
771" The binary data to decompress.\n"
772" max_length\n"
773" The maximum allowable length of the decompressed data.\n"
774" Unconsumed input data will be stored in\n"
775" the unconsumed_tail attribute.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000776"\n"
777"After calling this function, some of the input data may still be stored in\n"
778"internal buffers for later processing.\n"
Larry Hastings31826802013-10-19 00:09:25 -0700779"Call the flush() method to clear these buffers.");
780
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800781#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
782 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
Guido van Rossum3c540301997-06-03 22:21:03 +0000783
Guido van Rossumfb221561997-04-29 15:38:09 +0000784static PyObject *
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800785zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length);
Larry Hastings31826802013-10-19 00:09:25 -0700786
787static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -0800788zlib_Decompress_decompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000789{
Larry Hastings31826802013-10-19 00:09:25 -0700790 PyObject *return_value = NULL;
Larry Hastings4a55fc52014-01-12 11:09:57 -0800791 Py_buffer data = {NULL, NULL};
Victor Stinnere079edd2013-11-21 22:33:21 +0100792 unsigned int max_length = 0;
Larry Hastings31826802013-10-19 00:09:25 -0700793
794 if (!PyArg_ParseTuple(args,
Victor Stinnere079edd2013-11-21 22:33:21 +0100795 "y*|O&:decompress",
796 &data, uint_converter, &max_length))
Larry Hastings31826802013-10-19 00:09:25 -0700797 goto exit;
Larry Hastings5c661892014-01-24 06:17:25 -0800798 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
Larry Hastings31826802013-10-19 00:09:25 -0700799
800exit:
801 /* Cleanup for data */
Larry Hastings4a55fc52014-01-12 11:09:57 -0800802 if (data.obj)
Larry Hastingsebdcb502013-11-23 14:54:00 -0800803 PyBuffer_Release(&data);
Larry Hastings31826802013-10-19 00:09:25 -0700804
805 return return_value;
806}
807
808static PyObject *
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800809zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length)
Larry Hastings5c661892014-01-24 06:17:25 -0800810/*[clinic end generated code: checksum=b7fd2e3b23430f57f5a84817189575bc46464901]*/
Larry Hastings31826802013-10-19 00:09:25 -0700811{
Larry Hastings31826802013-10-19 00:09:25 -0700812 int err;
Victor Stinnere079edd2013-11-21 22:33:21 +0100813 unsigned int old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200814 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000815 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000816
Victor Stinnere079edd2013-11-21 22:33:21 +0100817 if ((size_t)data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200818 PyErr_SetString(PyExc_OverflowError,
819 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700820 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200821 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000822
Jeremy Hylton9714f992001-10-16 21:19:45 +0000823 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000824 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000825 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200826 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700827 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000828
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800829 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000830
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800831 start_total_out = self->zst.total_out;
832 self->zst.avail_in = (unsigned int)data->len;
833 self->zst.next_in = data->buf;
834 self->zst.avail_out = length;
835 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000836
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000837 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800838 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000839 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000840
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800841 if (err == Z_NEED_DICT && self->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200842 Py_buffer zdict_buf;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800843 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200844 Py_DECREF(RetVal);
845 RetVal = NULL;
846 goto error;
847 }
Victor Stinnere079edd2013-11-21 22:33:21 +0100848
849 if ((size_t)zdict_buf.len > UINT_MAX) {
850 PyErr_SetString(PyExc_OverflowError,
851 "zdict length does not fit in an unsigned int");
852 PyBuffer_Release(&zdict_buf);
853 Py_CLEAR(RetVal);
854 goto error;
855 }
856
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800857 err = inflateSetDictionary(&(self->zst),
Victor Stinnere079edd2013-11-21 22:33:21 +0100858 zdict_buf.buf, (unsigned int)zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200859 PyBuffer_Release(&zdict_buf);
860 if (err != Z_OK) {
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800861 zlib_error(self->zst, err, "while decompressing data");
Victor Stinnere079edd2013-11-21 22:33:21 +0100862 Py_CLEAR(RetVal);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200863 goto error;
864 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200865 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200866 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800867 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200868 Py_END_ALLOW_THREADS
869 }
870
Jeremy Hylton9714f992001-10-16 21:19:45 +0000871 /* While Z_OK and the output buffer is full, there might be more output.
872 So extend the output buffer and try again.
873 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800874 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000875 /* If max_length set, don't continue decompressing if we've already
876 reached the limit.
877 */
878 if (max_length && length >= max_length)
879 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000880
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 /* otherwise, ... */
882 old_length = length;
883 length = length << 1;
884 if (max_length && length > max_length)
885 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000886
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000887 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200888 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000889 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000890 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800891 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000892 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800893 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000895 Py_BEGIN_ALLOW_THREADS
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800896 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000898 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000899
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800900 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200901 Py_DECREF(RetVal);
902 RetVal = NULL;
903 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000904 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000905
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000906 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100907 /* This is the logical place to call inflateEnd, but the old behaviour
908 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800909 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100910 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000911 /* We will only get Z_BUF_ERROR if the output buffer was full
912 but there wasn't more output when we tried again, so it is
913 not an error condition.
914 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800915 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000916 Py_DECREF(RetVal);
917 RetVal = NULL;
918 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000919 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000920
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800921 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200922 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000923 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000924
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000925 error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800926 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000927 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000928}
929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000930PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000931"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000932"\n"
933"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000934"default value used when mode is not specified is Z_FINISH.\n"
935"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000936"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000937
Guido van Rossumfb221561997-04-29 15:38:09 +0000938static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000939PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000940{
Victor Stinnere079edd2013-11-21 22:33:21 +0100941 int err;
942 unsigned int length = DEFAULTALLOC, new_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000943 PyObject *RetVal;
944 int flushmode = Z_FINISH;
945 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000946
Jeremy Hylton9714f992001-10-16 21:19:45 +0000947 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000948 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000949
Jeremy Hylton9714f992001-10-16 21:19:45 +0000950 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
951 doing any work at all; just return an empty string. */
952 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000953 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000954 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000955
Gregory P. Smith693fc462008-09-06 20:13:06 +0000956 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000957 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000958
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000959 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000960
Jeremy Hylton9714f992001-10-16 21:19:45 +0000961 start_total_out = self->zst.total_out;
962 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000963 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000964 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000965
966 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000967 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000968 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000969
Jeremy Hylton9714f992001-10-16 21:19:45 +0000970 /* while Z_OK and the output buffer is full, there might be more output,
971 so extend the output buffer and try again */
972 while (err == Z_OK && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100973 if (length <= (UINT_MAX >> 1))
974 new_length = length << 1;
975 else
976 new_length = UINT_MAX;
977 if (_PyBytes_Resize(&RetVal, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200978 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000979 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000980 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000981 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000982 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000983 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +0100984 length = new_length;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000985
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000986 Py_BEGIN_ALLOW_THREADS
987 err = deflate(&(self->zst), flushmode);
988 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000989 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000990
Jeremy Hylton9714f992001-10-16 21:19:45 +0000991 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000992 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000993 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000994 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000995 err = deflateEnd(&(self->zst));
996 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200997 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000998 Py_DECREF(RetVal);
999 RetVal = NULL;
1000 goto error;
1001 }
1002 else
1003 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +00001004
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001005 /* We will only get Z_BUF_ERROR if the output buffer was full
1006 but there wasn't more output when we tried again, so it is
1007 not an error condition.
1008 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001009 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001010 zlib_error(self->zst, err, "while flushing");
1011 Py_DECREF(RetVal);
1012 RetVal = NULL;
1013 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001014 }
Tim Peters977e5402001-10-17 03:57:20 +00001015
Gregory P. Smith693fc462008-09-06 20:13:06 +00001016 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001017 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +00001018 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001019
Tim Peters977e5402001-10-17 03:57:20 +00001020 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001021 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001022
1023 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001024}
1025
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001026#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -07001027
Larry Hastings61272b72014-01-07 12:41:53 -08001028/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001029zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -07001030
1031Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -08001032[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07001033
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001034PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings5c661892014-01-24 06:17:25 -08001035"copy(self)\n"
Larry Hastings44e2eaa2013-11-23 15:37:55 -08001036"Return a copy of the compression object.");
Larry Hastings31826802013-10-19 00:09:25 -07001037
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001038#define ZLIB_COMPRESS_COPY_METHODDEF \
1039 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040
1041static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08001042zlib_Compress_copy_impl(compobject *self);
1043
1044static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -08001045zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
Larry Hastings3cceb382014-01-04 11:09:09 -08001046{
Larry Hastings5c661892014-01-24 06:17:25 -08001047 return zlib_Compress_copy_impl(self);
Larry Hastings3cceb382014-01-04 11:09:09 -08001048}
1049
1050static PyObject *
1051zlib_Compress_copy_impl(compobject *self)
Larry Hastings5c661892014-01-24 06:17:25 -08001052/*[clinic end generated code: checksum=7aa841ad51297eb83250f511a76872e88fdc737e]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053{
1054 compobject *retval = NULL;
1055 int err;
1056
1057 retval = newcompobject(&Comptype);
1058 if (!retval) return NULL;
1059
1060 /* Copy the zstream state
1061 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1062 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001063 ENTER_ZLIB(self);
1064 err = deflateCopy(&retval->zst, &self->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065 switch(err) {
1066 case(Z_OK):
1067 break;
1068 case(Z_STREAM_ERROR):
1069 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1070 goto error;
1071 case(Z_MEM_ERROR):
1072 PyErr_SetString(PyExc_MemoryError,
1073 "Can't allocate memory for compression object");
1074 goto error;
1075 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001076 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001077 goto error;
1078 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001079 Py_INCREF(self->unused_data);
1080 Py_INCREF(self->unconsumed_tail);
1081 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001082 Py_XDECREF(retval->unused_data);
1083 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001084 Py_XDECREF(retval->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001085 retval->unused_data = self->unused_data;
1086 retval->unconsumed_tail = self->unconsumed_tail;
1087 retval->zdict = self->zdict;
1088 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001089
1090 /* Mark it as being initialized */
1091 retval->is_initialised = 1;
1092
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001093 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001094 return (PyObject *)retval;
1095
1096error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001097 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001098 Py_XDECREF(retval);
1099 return NULL;
1100}
1101
1102PyDoc_STRVAR(decomp_copy__doc__,
1103"copy() -- Return a copy of the decompression object.");
1104
1105static PyObject *
1106PyZlib_uncopy(compobject *self)
1107{
1108 compobject *retval = NULL;
1109 int err;
1110
1111 retval = newcompobject(&Decomptype);
1112 if (!retval) return NULL;
1113
1114 /* Copy the zstream state
1115 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1116 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001117 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001118 err = inflateCopy(&retval->zst, &self->zst);
1119 switch(err) {
1120 case(Z_OK):
1121 break;
1122 case(Z_STREAM_ERROR):
1123 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1124 goto error;
1125 case(Z_MEM_ERROR):
1126 PyErr_SetString(PyExc_MemoryError,
1127 "Can't allocate memory for decompression object");
1128 goto error;
1129 default:
1130 zlib_error(self->zst, err, "while copying decompression object");
1131 goto error;
1132 }
1133
1134 Py_INCREF(self->unused_data);
1135 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001136 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001137 Py_XDECREF(retval->unused_data);
1138 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001139 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140 retval->unused_data = self->unused_data;
1141 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001142 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001143 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001144
1145 /* Mark it as being initialized */
1146 retval->is_initialised = 1;
1147
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001148 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001149 return (PyObject *)retval;
1150
1151error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001152 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001153 Py_XDECREF(retval);
1154 return NULL;
1155}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001156#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001158PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001159"flush( [length] ) -- Return a string containing any remaining\n"
1160"decompressed data. length, if given, is the initial size of the\n"
1161"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001162"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001163"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001164
Guido van Rossumfb221561997-04-29 15:38:09 +00001165static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001166PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001167{
Victor Stinnere079edd2013-11-21 22:33:21 +01001168 int err;
1169 unsigned int length = DEFAULTALLOC, new_length;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001170 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001171 unsigned long start_total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001172 Py_ssize_t size;
Tim Peters977e5402001-10-17 03:57:20 +00001173
Victor Stinnere079edd2013-11-21 22:33:21 +01001174 if (!PyArg_ParseTuple(args, "|O&:flush", uint_converter, &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001175 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +01001176 if (length == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001177 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1178 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001179 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001180
Gregory P. Smith693fc462008-09-06 20:13:06 +00001181 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001182 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001183
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001184
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001185 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001186
Victor Stinnere079edd2013-11-21 22:33:21 +01001187 size = PyBytes_GET_SIZE(self->unconsumed_tail);
1188
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001189 start_total_out = self->zst.total_out;
Victor Stinnere079edd2013-11-21 22:33:21 +01001190 /* save_unconsumed_input() ensures that unconsumed_tail length is lesser
1191 or equal than UINT_MAX */
1192 self->zst.avail_in = Py_SAFE_DOWNCAST(size, Py_ssize_t, unsigned int);
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001193 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001194 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001195 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001196
1197 Py_BEGIN_ALLOW_THREADS
1198 err = inflate(&(self->zst), Z_FINISH);
1199 Py_END_ALLOW_THREADS
1200
1201 /* while Z_OK and the output buffer is full, there might be more output,
1202 so extend the output buffer and try again */
1203 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +01001204 if (length <= (UINT_MAX >> 1))
1205 new_length = length << 1;
1206 else
1207 new_length = UINT_MAX;
1208 if (_PyBytes_Resize(&retval, new_length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001209 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001210 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001211 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001212 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1213 self->zst.avail_out = length;
Victor Stinnere079edd2013-11-21 22:33:21 +01001214 length = new_length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001215
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001216 Py_BEGIN_ALLOW_THREADS
1217 err = inflate(&(self->zst), Z_FINISH);
1218 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001219 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001220
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001221 if (save_unconsumed_input(self, err) < 0) {
1222 Py_DECREF(retval);
1223 retval = NULL;
1224 goto error;
1225 }
1226
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001227 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001228 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001229 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001230 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001231 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001232 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001233 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001234 Py_DECREF(retval);
1235 retval = NULL;
1236 goto error;
1237 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001238 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001239
Gregory P. Smith693fc462008-09-06 20:13:06 +00001240 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001241 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001242 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001243
1244error:
1245
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001246 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001247
Jeremy Hylton9714f992001-10-16 21:19:45 +00001248 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001249}
1250
1251static PyMethodDef comp_methods[] =
1252{
Tim Peters977e5402001-10-17 03:57:20 +00001253 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001254 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001255 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001256 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001257#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001258 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001259#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001260 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001261};
1262
1263static PyMethodDef Decomp_methods[] =
1264{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001265 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Tim Peters977e5402001-10-17 03:57:20 +00001266 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001267 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001268#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001269 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1270 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001271#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001272 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001273};
1274
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001275#define COMP_OFF(x) offsetof(compobject, x)
1276static PyMemberDef Decomp_members[] = {
1277 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1278 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001279 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001280 {NULL},
1281};
Guido van Rossumfb221561997-04-29 15:38:09 +00001282
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001283PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001284"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1285"\n"
1286"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001287"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001288
Guido van Rossumfb221561997-04-29 15:38:09 +00001289static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001290PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001291{
Christian Heimescc47b052008-03-25 14:56:36 +00001292 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001293 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +00001294
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001295 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1296 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001297 /* Releasing the GIL for very small buffers is inefficient
1298 and may lower performance */
1299 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001300 unsigned char *buf = pbuf.buf;
1301 Py_ssize_t len = pbuf.len;
1302
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001303 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001304 /* Avoid truncation of length for very large buffers. adler32() takes
1305 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001306 while ((size_t)len > UINT_MAX) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001307 adler32val = adler32(adler32val, buf, UINT_MAX);
1308 buf += (size_t) UINT_MAX;
1309 len -= (size_t) UINT_MAX;
1310 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001311 adler32val = adler32(adler32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001312 Py_END_ALLOW_THREADS
1313 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001314 adler32val = adler32(adler32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001315 }
1316 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001317 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001318}
Tim Peters977e5402001-10-17 03:57:20 +00001319
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001320PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001321"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1322"\n"
1323"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001324"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001325
1326static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001327PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001328{
Christian Heimescc47b052008-03-25 14:56:36 +00001329 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001330 Py_buffer pbuf;
1331 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001332
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001333 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001334 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001335 /* Releasing the GIL for very small buffers is inefficient
1336 and may lower performance */
1337 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001338 unsigned char *buf = pbuf.buf;
1339 Py_ssize_t len = pbuf.len;
1340
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001341 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001342 /* Avoid truncation of length for very large buffers. crc32() takes
1343 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001344 while ((size_t)len > UINT_MAX) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001345 crc32val = crc32(crc32val, buf, UINT_MAX);
1346 buf += (size_t) UINT_MAX;
1347 len -= (size_t) UINT_MAX;
1348 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001349 signed_val = crc32(crc32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001350 Py_END_ALLOW_THREADS
1351 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001352 signed_val = crc32(crc32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001353 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001354 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001355 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001356}
Tim Peters977e5402001-10-17 03:57:20 +00001357
Guido van Rossumfb221561997-04-29 15:38:09 +00001358
1359static PyMethodDef zlib_methods[] =
1360{
Tim Peters977e5402001-10-17 03:57:20 +00001361 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001362 adler32__doc__},
Larry Hastingsebdcb502013-11-23 14:54:00 -08001363 ZLIB_COMPRESS_METHODDEF
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001364 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001365 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001366 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1367 crc32__doc__},
1368 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001369 decompress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001370 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001371 decompressobj__doc__},
1372 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001373};
1374
Tim Peters0c322792002-07-17 16:49:03 +00001375static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001376 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001377 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001378 sizeof(compobject),
1379 0,
1380 (destructor)Comp_dealloc, /*tp_dealloc*/
1381 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001382 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001383 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001384 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001385 0, /*tp_repr*/
1386 0, /*tp_as_number*/
1387 0, /*tp_as_sequence*/
1388 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001389 0, /*tp_hash*/
1390 0, /*tp_call*/
1391 0, /*tp_str*/
1392 0, /*tp_getattro*/
1393 0, /*tp_setattro*/
1394 0, /*tp_as_buffer*/
1395 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1396 0, /*tp_doc*/
1397 0, /*tp_traverse*/
1398 0, /*tp_clear*/
1399 0, /*tp_richcompare*/
1400 0, /*tp_weaklistoffset*/
1401 0, /*tp_iter*/
1402 0, /*tp_iternext*/
1403 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001404};
1405
Tim Peters0c322792002-07-17 16:49:03 +00001406static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001407 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001408 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001409 sizeof(compobject),
1410 0,
1411 (destructor)Decomp_dealloc, /*tp_dealloc*/
1412 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001413 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001414 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001415 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001416 0, /*tp_repr*/
1417 0, /*tp_as_number*/
1418 0, /*tp_as_sequence*/
1419 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001420 0, /*tp_hash*/
1421 0, /*tp_call*/
1422 0, /*tp_str*/
1423 0, /*tp_getattro*/
1424 0, /*tp_setattro*/
1425 0, /*tp_as_buffer*/
1426 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1427 0, /*tp_doc*/
1428 0, /*tp_traverse*/
1429 0, /*tp_clear*/
1430 0, /*tp_richcompare*/
1431 0, /*tp_weaklistoffset*/
1432 0, /*tp_iter*/
1433 0, /*tp_iternext*/
1434 Decomp_methods, /*tp_methods*/
1435 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001436};
1437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001438PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001439"The functions in this module allow compression and decompression using the\n"
1440"zlib library, which is based on GNU zip.\n"
1441"\n"
1442"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001443"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001444"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001445"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001446"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001447"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001448"\n"
1449"'wbits' is window buffer size.\n"
1450"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001451"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001452
Martin v. Löwis1a214512008-06-11 05:26:20 +00001453static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001454 PyModuleDef_HEAD_INIT,
1455 "zlib",
1456 zlib_module_documentation,
1457 -1,
1458 zlib_methods,
1459 NULL,
1460 NULL,
1461 NULL,
1462 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001463};
1464
Mark Hammond62b1ab12002-07-23 06:31:15 +00001465PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001466PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001467{
Fred Drake4baedc12002-04-01 14:53:37 +00001468 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001469 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001470 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001471 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001472 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001473 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001474 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001475 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001476
Fred Drake4baedc12002-04-01 14:53:37 +00001477 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1478 if (ZlibError != NULL) {
1479 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001480 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001481 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001482 PyModule_AddIntMacro(m, MAX_WBITS);
1483 PyModule_AddIntMacro(m, DEFLATED);
1484 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
1485 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1486 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1487 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1488 PyModule_AddIntMacro(m, Z_FILTERED);
1489 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1490 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001491
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001492 PyModule_AddIntMacro(m, Z_FINISH);
1493 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1494 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1495 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001496
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001497 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001498 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001499 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001500
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001501 ver = PyUnicode_FromString(zlibVersion());
1502 if (ver != NULL)
1503 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1504
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001505 PyModule_AddStringConstant(m, "__version__", "1.0");
1506
Martin v. Löwis1a214512008-06-11 05:26:20 +00001507 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001508}