blob: b17136b4616b8315f26c55df8462567e87016fdd [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Guido van Rossumfb221561997-04-29 15:38:09 +00006
Guido van Rossum97b54571997-06-03 22:21:47 +00007#include "Python.h"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00008#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +00009#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000012#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000013 #include "pythread.h"
14 #define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000020 #define ENTER_ZLIB(obj)
21 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000022#endif
23
Guido van Rossumfb221561997-04-29 15:38:09 +000024/* The following parameters are copied from zutil.h, version 0.95 */
25#define DEFLATED 8
26#if MAX_MEM_LEVEL >= 8
27# define DEF_MEM_LEVEL 8
28#else
29# define DEF_MEM_LEVEL MAX_MEM_LEVEL
30#endif
31#define DEF_WBITS MAX_WBITS
32
Guido van Rossumb729a1d1999-04-07 20:23:17 +000033/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
34#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Jeremy Hylton938ace62002-07-17 16:30:39 +000036static PyTypeObject Comptype;
37static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000038
39static PyObject *ZlibError;
40
Tim Peters977e5402001-10-17 03:57:20 +000041typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000042{
Jeremy Hylton9714f992001-10-16 21:19:45 +000043 PyObject_HEAD
44 z_stream zst;
45 PyObject *unused_data;
46 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020047 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000048 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020049 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000050 #ifdef WITH_THREAD
51 PyThread_type_lock lock;
52 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000053} compobject;
54
Jeremy Hylton0965e082001-10-16 21:56:09 +000055static void
56zlib_error(z_stream zst, int err, char *msg)
57{
Nadeem Vawda524148a2011-08-28 11:26:46 +020058 const char *zmsg = Z_NULL;
59 /* In case of a version mismatch, zst.msg won't be initialized.
60 Check for this case first, before looking at zst.msg. */
61 if (err == Z_VERSION_ERROR)
62 zmsg = "library version mismatch";
63 if (zmsg == Z_NULL)
64 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000065 if (zmsg == Z_NULL) {
66 switch (err) {
67 case Z_BUF_ERROR:
68 zmsg = "incomplete or truncated stream";
69 break;
70 case Z_STREAM_ERROR:
71 zmsg = "inconsistent stream state";
72 break;
73 case Z_DATA_ERROR:
74 zmsg = "invalid input data";
75 break;
76 }
77 }
78 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000080 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000081 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000082}
83
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084PyDoc_STRVAR(compressobj__doc__,
Nadeem Vawda2180c972012-06-22 01:40:49 +020085"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
86" strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020087" -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +000088"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020089"level is the compression level (an integer in the range 0-9; default is 6).\n"
90"Higher compression levels are slower, but produce smaller results.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020091"\n"
Nadeem Vawda2180c972012-06-22 01:40:49 +020092"method is the compression algorithm. If given, this must be DEFLATED.\n"
93"\n"
94"wbits is the base two logarithm of the window size (range: 8..15).\n"
95"\n"
96"memlevel controls the amount of memory used for internal compression state.\n"
97"Valid values range from 1 to 9. Higher values result in higher memory usage,\n"
98"faster compression, and smaller output.\n"
99"\n"
100"strategy is used to tune the compression algorithm. Possible values are\n"
101"Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
102"\n"
103"zdict is the predefined compression dictionary - a sequence of bytes\n"
104"containing subsequences that are likely to occur in the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106PyDoc_STRVAR(decompressobj__doc__,
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200107"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000108"\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200109"Optional arg wbits is the window buffer size.\n"
110"\n"
111"Optional arg zdict is the predefined compression dictionary. This must be\n"
112"the same dictionary as used by the compressor that produced the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000113
Guido van Rossumfb221561997-04-29 15:38:09 +0000114static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000115newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000116{
Tim Peters977e5402001-10-17 03:57:20 +0000117 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000118 self = PyObject_New(compobject, type);
119 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000120 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200121 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200123 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000124 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 Py_DECREF(self);
127 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000128 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000129 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000130 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000131 Py_DECREF(self);
132 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000133 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000134#ifdef WITH_THREAD
135 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200136 if (self->lock == NULL) {
137 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
138 return NULL;
139 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000140#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000141 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000142}
143
Victor Stinner5064a522013-07-07 16:50:27 +0200144static void*
145PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
146{
147 if (items > (size_t)PY_SSIZE_T_MAX / size)
148 return NULL;
149 /* PyMem_Malloc() cannot be used: the GIL is not held when
150 inflate() and deflate() are called */
151 return PyMem_RawMalloc(items * size);
152}
153
154static void
155PyZlib_Free(voidpf ctx, void *ptr)
156{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200157 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200158}
159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000161"compress(string[, level]) -- Returned compressed string.\n"
162"\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +0100163"Optional arg level is the compression level, in 0-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000164
Guido van Rossumfb221561997-04-29 15:38:09 +0000165static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000166PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000167{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000168 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000169 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200170 Byte *input, *output = NULL;
171 unsigned int length;
172 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000173 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000174
Jeremy Hylton9714f992001-10-16 21:19:45 +0000175 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000176 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000177 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200178
179 if (pinput.len > UINT_MAX) {
180 PyErr_SetString(PyExc_OverflowError,
181 "Size does not fit in an unsigned int");
182 goto error;
183 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000184 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100185 length = (unsigned int)pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000186
Jeremy Hylton9714f992001-10-16 21:19:45 +0000187 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000188
Victor Stinnerb6404912013-07-07 16:21:41 +0200189 output = (Byte*)PyMem_Malloc(zst.avail_out);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000190 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000191 PyErr_SetString(PyExc_MemoryError,
192 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200193 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000194 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000195
Jeremy Hylton9714f992001-10-16 21:19:45 +0000196 /* Past the point of no return. From here on out, we need to make sure
197 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000198
Victor Stinner5064a522013-07-07 16:50:27 +0200199 zst.opaque = NULL;
200 zst.zalloc = PyZlib_Malloc;
201 zst.zfree = PyZlib_Free;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000202 zst.next_out = (Byte *)output;
203 zst.next_in = (Byte *)input;
204 zst.avail_in = length;
205 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000206
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000207 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000208 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000209 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000210 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000211 PyErr_SetString(PyExc_MemoryError,
212 "Out of memory while compressing data");
213 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000214 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000215 PyErr_SetString(ZlibError,
216 "Bad compression level");
217 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000218 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000219 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 zlib_error(zst, err, "while compressing data");
221 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000222 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000224 Py_BEGIN_ALLOW_THREADS;
225 err = deflate(&zst, Z_FINISH);
226 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000227
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000228 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000229 zlib_error(zst, err, "while compressing data");
230 deflateEnd(&zst);
231 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000232 }
Tim Peters977e5402001-10-17 03:57:20 +0000233
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000234 err=deflateEnd(&zst);
235 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000237 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000238 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000240
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000241 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000242 PyBuffer_Release(&pinput);
Victor Stinnerb6404912013-07-07 16:21:41 +0200243 PyMem_Free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000244
Jeremy Hylton9714f992001-10-16 21:19:45 +0000245 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000246}
247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000249"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
250"\n"
251"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000252"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000253
Guido van Rossumfb221561997-04-29 15:38:09 +0000254static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000255PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000256{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200257 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000258 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000259 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200260 unsigned int length;
261 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000262 int wsize=DEF_WBITS;
263 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000264 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000265
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000266 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000267 &pinput, &wsize, &r_strlen))
268 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200269
270 if (pinput.len > UINT_MAX) {
271 PyErr_SetString(PyExc_OverflowError,
272 "Size does not fit in an unsigned int");
273 goto error;
274 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000275 input = pinput.buf;
Victor Stinner56cb1252012-10-31 00:33:57 +0100276 length = (unsigned int)pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000277
Jeremy Hylton9714f992001-10-16 21:19:45 +0000278 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000279 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000280
Jeremy Hylton9714f992001-10-16 21:19:45 +0000281 zst.avail_in = length;
282 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000283
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200284 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
285 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000286
Victor Stinner5064a522013-07-07 16:50:27 +0200287 zst.opaque = NULL;
288 zst.zalloc = PyZlib_Malloc;
289 zst.zfree = PyZlib_Free;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000290 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000291 zst.next_in = (Byte *)input;
292 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000293
Jeremy Hylton9714f992001-10-16 21:19:45 +0000294 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000295 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000296 break;
Tim Peters977e5402001-10-17 03:57:20 +0000297 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000298 PyErr_SetString(PyExc_MemoryError,
299 "Out of memory while decompressing data");
300 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000301 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000302 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 zlib_error(zst, err, "while preparing to decompress data");
304 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000305 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000306
Jeremy Hylton9714f992001-10-16 21:19:45 +0000307 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000308 Py_BEGIN_ALLOW_THREADS
309 err=inflate(&zst, Z_FINISH);
310 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000311
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 switch(err) {
313 case(Z_STREAM_END):
314 break;
315 case(Z_BUF_ERROR):
316 /*
317 * If there is at least 1 byte of room according to zst.avail_out
318 * and we get this error, assume that it means zlib cannot
319 * process the inflate call() due to an error in the data.
320 */
321 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000322 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 inflateEnd(&zst);
324 goto error;
325 }
326 /* fall through */
327 case(Z_OK):
328 /* need more memory */
329 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
330 inflateEnd(&zst);
331 goto error;
332 }
333 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000334 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 zst.avail_out = r_strlen;
336 r_strlen = r_strlen << 1;
337 break;
338 default:
339 inflateEnd(&zst);
340 zlib_error(zst, err, "while decompressing data");
341 goto error;
342 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000343 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000344
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000345 err = inflateEnd(&zst);
346 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200347 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000348 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000349 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000350
Gregory P. Smith693fc462008-09-06 20:13:06 +0000351 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000352 goto error;
353
Martin v. Löwis423be952008-08-13 15:53:07 +0000354 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000355 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000356
357 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000358 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000359 Py_XDECREF(result_str);
360 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000361}
362
363static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200364PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000365{
Jeremy Hylton499000002001-10-16 21:59:35 +0000366 compobject *self;
367 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
368 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200369 Py_buffer zdict;
370 static char *kwlist[] = {"level", "method", "wbits",
371 "memLevel", "strategy", "zdict", NULL};
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000372
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200373 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
374 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
375 kwlist, &level, &method, &wbits,
376 &memLevel, &strategy, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000378
Jeremy Hylton499000002001-10-16 21:59:35 +0000379 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000380 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200381 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200382 self->zst.opaque = NULL;
383 self->zst.zalloc = PyZlib_Malloc;
384 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000385 self->zst.next_in = NULL;
386 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000387 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
388 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000389 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000390 self->is_initialised = 1;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200391 if (zdict.buf == NULL) {
392 goto success;
393 } else {
394 err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len);
395 switch (err) {
396 case (Z_OK):
397 goto success;
398 case (Z_STREAM_ERROR):
399 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
400 goto error;
401 default:
402 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
403 goto error;
404 }
405 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000406 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000407 PyErr_SetString(PyExc_MemoryError,
408 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200409 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000410 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000411 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200412 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000413 default:
414 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200415 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000416 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200417
418 error:
419 Py_XDECREF(self);
420 self = NULL;
421 success:
422 if (zdict.buf != NULL)
423 PyBuffer_Release(&zdict);
424 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000425}
426
427static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200428PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000429{
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200430 static char *kwlist[] = {"wbits", "zdict", NULL};
Jeremy Hylton499000002001-10-16 21:59:35 +0000431 int wbits=DEF_WBITS, err;
432 compobject *self;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200433 PyObject *zdict=NULL;
434
435 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
436 kwlist, &wbits, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000437 return NULL;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200438 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
439 PyErr_SetString(PyExc_TypeError,
440 "zdict argument must support the buffer protocol");
441 return NULL;
442 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000443
444 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000445 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000446 return(NULL);
Victor Stinner5064a522013-07-07 16:50:27 +0200447 self->zst.opaque = NULL;
448 self->zst.zalloc = PyZlib_Malloc;
449 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000450 self->zst.next_in = NULL;
451 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200452 if (zdict != NULL) {
453 Py_INCREF(zdict);
454 self->zdict = zdict;
455 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000456 err = inflateInit2(&self->zst, wbits);
457 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000458 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 self->is_initialised = 1;
460 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000461 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000462 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
464 return NULL;
465 case (Z_MEM_ERROR):
466 Py_DECREF(self);
467 PyErr_SetString(PyExc_MemoryError,
468 "Can't allocate memory for decompression object");
469 return NULL;
470 default:
471 zlib_error(self->zst, err, "while creating decompression object");
472 Py_DECREF(self);
473 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000474 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000475}
476
477static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000478Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000479{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000480#ifdef WITH_THREAD
481 PyThread_free_lock(self->lock);
482#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000483 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000484 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200485 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000486 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000487}
488
489static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000490Comp_dealloc(compobject *self)
491{
492 if (self->is_initialised)
493 deflateEnd(&self->zst);
494 Dealloc(self);
495}
496
497static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000498Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000499{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000500 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000501 inflateEnd(&self->zst);
502 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000503}
504
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000505PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000506"compress(data) -- Return a string containing data compressed.\n"
507"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000508"After calling this function, some of the input data may still\n"
509"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000510"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000511
512
Guido van Rossumfb221561997-04-29 15:38:09 +0000513static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000514PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000515{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200516 int err;
517 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000518 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200519 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000520 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000521 Byte *input;
522 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000523
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000524 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000525 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200526 if (pinput.len > UINT_MAX) {
527 PyErr_SetString(PyExc_OverflowError,
528 "Size does not fit in an unsigned int");
529 goto error_outer;
530 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000531 input = pinput.buf;
532 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000533
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200534 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
535 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000536
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000537 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000538
Jeremy Hylton9714f992001-10-16 21:19:45 +0000539 start_total_out = self->zst.total_out;
540 self->zst.avail_in = inplen;
541 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000542 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000543 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000544
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000545 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000546 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000547 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000548
Jeremy Hylton9714f992001-10-16 21:19:45 +0000549 /* while Z_OK and the output buffer is full, there might be more output,
550 so extend the output buffer and try again */
551 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000552 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200553 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000555 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000557 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000558 self->zst.avail_out = length;
559 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000560
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 Py_BEGIN_ALLOW_THREADS
562 err = deflate(&(self->zst), Z_NO_FLUSH);
563 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000564 }
Tim Peters977e5402001-10-17 03:57:20 +0000565 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000566 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000567 condition.
568 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000569
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000570 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200571 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000572 Py_DECREF(RetVal);
573 RetVal = NULL;
574 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000575 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000576 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200577 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000578 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000579
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000580 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000581 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200582 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000583 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000584 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000585}
586
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100587/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
588 self->unused_data or self->unconsumed_tail, as appropriate. */
589static int
590save_unconsumed_input(compobject *self, int err)
591{
592 if (err == Z_STREAM_END) {
593 /* The end of the compressed data has been reached. Store the leftover
594 input data in self->unused_data. */
595 if (self->zst.avail_in > 0) {
596 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
597 Py_ssize_t new_size;
598 PyObject *new_data;
Victor Stinnere8289612013-05-07 23:50:21 +0200599 if ((Py_ssize_t)self->zst.avail_in > PY_SSIZE_T_MAX - old_size) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100600 PyErr_NoMemory();
601 return -1;
602 }
603 new_size = old_size + self->zst.avail_in;
604 new_data = PyBytes_FromStringAndSize(NULL, new_size);
605 if (new_data == NULL)
606 return -1;
607 Py_MEMCPY(PyBytes_AS_STRING(new_data),
608 PyBytes_AS_STRING(self->unused_data), old_size);
609 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
610 self->zst.next_in, self->zst.avail_in);
611 Py_DECREF(self->unused_data);
612 self->unused_data = new_data;
613 self->zst.avail_in = 0;
614 }
615 }
616 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
617 /* This code handles two distinct cases:
618 1. Output limit was reached. Save leftover input in unconsumed_tail.
619 2. All input data was consumed. Clear unconsumed_tail. */
620 PyObject *new_data = PyBytes_FromStringAndSize(
621 (char *)self->zst.next_in, self->zst.avail_in);
622 if (new_data == NULL)
623 return -1;
624 Py_DECREF(self->unconsumed_tail);
625 self->unconsumed_tail = new_data;
626 }
627 return 0;
628}
629
Larry Hastings31826802013-10-19 00:09:25 -0700630/*[clinic]
631
632module zlib
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800633class zlib.Decompress
Larry Hastings31826802013-10-19 00:09:25 -0700634
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800635zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700636
637 data: Py_buffer
638 The binary data to decompress.
639 max_length: int = 0
640 The maximum allowable length of the decompressed data.
641 Unconsumed input data will be stored in
642 the unconsumed_tail attribute.
643 /
644
645Return a string containing the decompressed version of the data.
646
647After calling this function, some of the input data may still be stored in
648internal buffers for later processing.
649Call the flush() method to clear these buffers.
650[clinic]*/
651
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800652PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Larry Hastings31826802013-10-19 00:09:25 -0700653"Return a string containing the decompressed version of the data.\n"
654"\n"
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800655"zlib.Decompress.decompress(data, max_length=0)\n"
Larry Hastings31826802013-10-19 00:09:25 -0700656" data\n"
657" The binary data to decompress.\n"
658" max_length\n"
659" The maximum allowable length of the decompressed data.\n"
660" Unconsumed input data will be stored in\n"
661" the unconsumed_tail attribute.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000662"\n"
663"After calling this function, some of the input data may still be stored in\n"
664"internal buffers for later processing.\n"
Larry Hastings31826802013-10-19 00:09:25 -0700665"Call the flush() method to clear these buffers.");
666
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800667#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
668 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
Guido van Rossum3c540301997-06-03 22:21:03 +0000669
Guido van Rossumfb221561997-04-29 15:38:09 +0000670static PyObject *
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800671zlib_Decompress_decompress_impl(PyObject *self, Py_buffer *data, int max_length);
Larry Hastings31826802013-10-19 00:09:25 -0700672
673static PyObject *
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800674zlib_Decompress_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000675{
Larry Hastings31826802013-10-19 00:09:25 -0700676 PyObject *return_value = NULL;
677 Py_buffer data;
678 int max_length = 0;
679
680 if (!PyArg_ParseTuple(args,
681 "y*|i:decompress",
682 &data, &max_length))
683 goto exit;
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800684 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
Larry Hastings31826802013-10-19 00:09:25 -0700685
686exit:
687 /* Cleanup for data */
688 PyBuffer_Release(&data);
689
690 return return_value;
691}
692
693static PyObject *
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800694zlib_Decompress_decompress_impl(PyObject *self, Py_buffer *data, int max_length)
695/*[clinic checksum: bfac7a0f07e891869d87c665a76dc2611014420f]*/
Larry Hastings31826802013-10-19 00:09:25 -0700696{
697 compobject *zself = (compobject *)self;
698 int err;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200699 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000700 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200701 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000702 Byte *input;
703 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000704
Larry Hastings31826802013-10-19 00:09:25 -0700705 if (data->len > UINT_MAX) {
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200706 PyErr_SetString(PyExc_OverflowError,
707 "Size does not fit in an unsigned int");
Larry Hastings31826802013-10-19 00:09:25 -0700708 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200709 }
Larry Hastings31826802013-10-19 00:09:25 -0700710 input = data->buf;
711 inplen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000712 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000713 PyErr_SetString(PyExc_ValueError,
714 "max_length must be greater than zero");
Larry Hastings31826802013-10-19 00:09:25 -0700715 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000716 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000717
Jeremy Hylton9714f992001-10-16 21:19:45 +0000718 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000719 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000720 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200721 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Larry Hastings31826802013-10-19 00:09:25 -0700722 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000723
Larry Hastings31826802013-10-19 00:09:25 -0700724 ENTER_ZLIB(zself);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000725
Larry Hastings31826802013-10-19 00:09:25 -0700726 start_total_out = zself->zst.total_out;
727 zself->zst.avail_in = inplen;
728 zself->zst.next_in = input;
729 zself->zst.avail_out = length;
730 zself->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000731
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000732 Py_BEGIN_ALLOW_THREADS
Larry Hastings31826802013-10-19 00:09:25 -0700733 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000734 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000735
Larry Hastings31826802013-10-19 00:09:25 -0700736 if (err == Z_NEED_DICT && zself->zdict != NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200737 Py_buffer zdict_buf;
Larry Hastings31826802013-10-19 00:09:25 -0700738 if (PyObject_GetBuffer(zself->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200739 Py_DECREF(RetVal);
740 RetVal = NULL;
741 goto error;
742 }
Larry Hastings31826802013-10-19 00:09:25 -0700743 err = inflateSetDictionary(&(zself->zst), zdict_buf.buf, zdict_buf.len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200744 PyBuffer_Release(&zdict_buf);
745 if (err != Z_OK) {
Larry Hastings31826802013-10-19 00:09:25 -0700746 zlib_error(zself->zst, err, "while decompressing data");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200747 Py_DECREF(RetVal);
748 RetVal = NULL;
749 goto error;
750 }
Nadeem Vawdacf5e1d82012-06-22 00:35:57 +0200751 /* Repeat the call to inflate. */
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200752 Py_BEGIN_ALLOW_THREADS
Larry Hastings31826802013-10-19 00:09:25 -0700753 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200754 Py_END_ALLOW_THREADS
755 }
756
Jeremy Hylton9714f992001-10-16 21:19:45 +0000757 /* While Z_OK and the output buffer is full, there might be more output.
758 So extend the output buffer and try again.
759 */
Larry Hastings31826802013-10-19 00:09:25 -0700760 while (err == Z_OK && zself->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000761 /* If max_length set, don't continue decompressing if we've already
762 reached the limit.
763 */
764 if (max_length && length >= max_length)
765 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000766
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000767 /* otherwise, ... */
768 old_length = length;
769 length = length << 1;
770 if (max_length && length > max_length)
771 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000772
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000773 if (_PyBytes_Resize(&RetVal, length) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200774 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000775 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000776 }
Larry Hastings31826802013-10-19 00:09:25 -0700777 zself->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000778 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Larry Hastings31826802013-10-19 00:09:25 -0700779 zself->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000780
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000781 Py_BEGIN_ALLOW_THREADS
Larry Hastings31826802013-10-19 00:09:25 -0700782 err = inflate(&(zself->zst), Z_SYNC_FLUSH);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000783 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000784 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000785
Larry Hastings31826802013-10-19 00:09:25 -0700786 if (save_unconsumed_input(zself, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200787 Py_DECREF(RetVal);
788 RetVal = NULL;
789 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000790 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000791
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000792 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100793 /* This is the logical place to call inflateEnd, but the old behaviour
794 of only calling it on flush() is preserved. */
Larry Hastings31826802013-10-19 00:09:25 -0700795 zself->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100796 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000797 /* We will only get Z_BUF_ERROR if the output buffer was full
798 but there wasn't more output when we tried again, so it is
799 not an error condition.
800 */
Larry Hastings31826802013-10-19 00:09:25 -0700801 zlib_error(zself->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000802 Py_DECREF(RetVal);
803 RetVal = NULL;
804 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000805 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000806
Larry Hastings31826802013-10-19 00:09:25 -0700807 if (_PyBytes_Resize(&RetVal, zself->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200808 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000809 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000810
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000811 error:
Larry Hastings31826802013-10-19 00:09:25 -0700812 LEAVE_ZLIB(zself);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000813 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000814}
815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000816PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000817"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000818"\n"
819"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000820"default value used when mode is not specified is Z_FINISH.\n"
821"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000823
Guido van Rossumfb221561997-04-29 15:38:09 +0000824static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000825PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000826{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000827 int err, length = DEFAULTALLOC;
828 PyObject *RetVal;
829 int flushmode = Z_FINISH;
830 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000831
Jeremy Hylton9714f992001-10-16 21:19:45 +0000832 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000833 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000834
Jeremy Hylton9714f992001-10-16 21:19:45 +0000835 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
836 doing any work at all; just return an empty string. */
837 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000839 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000840
Gregory P. Smith693fc462008-09-06 20:13:06 +0000841 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000842 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000843
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000844 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000845
Jeremy Hylton9714f992001-10-16 21:19:45 +0000846 start_total_out = self->zst.total_out;
847 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000848 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000849 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000850
851 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000852 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000853 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000854
Jeremy Hylton9714f992001-10-16 21:19:45 +0000855 /* while Z_OK and the output buffer is full, there might be more output,
856 so extend the output buffer and try again */
857 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000858 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200859 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000861 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000862 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000863 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000864 self->zst.avail_out = length;
865 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000866
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000867 Py_BEGIN_ALLOW_THREADS
868 err = deflate(&(self->zst), flushmode);
869 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000870 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000871
Jeremy Hylton9714f992001-10-16 21:19:45 +0000872 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000873 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000874 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000875 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000876 err = deflateEnd(&(self->zst));
877 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200878 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 Py_DECREF(RetVal);
880 RetVal = NULL;
881 goto error;
882 }
883 else
884 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000885
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 /* We will only get Z_BUF_ERROR if the output buffer was full
887 but there wasn't more output when we tried again, so it is
888 not an error condition.
889 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000890 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000891 zlib_error(self->zst, err, "while flushing");
892 Py_DECREF(RetVal);
893 RetVal = NULL;
894 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000895 }
Tim Peters977e5402001-10-17 03:57:20 +0000896
Gregory P. Smith693fc462008-09-06 20:13:06 +0000897 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200898 Py_CLEAR(RetVal);
Guido van Rossum776152b2007-05-22 22:44:07 +0000899 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000900
Tim Peters977e5402001-10-17 03:57:20 +0000901 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000902 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000903
904 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000905}
906
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000907#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700908
909/*[clinic]
910
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800911class zlib.Compress
912zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700913
914Return a copy of the compression object.
915[clinic]*/
916
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800917PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings31826802013-10-19 00:09:25 -0700918"Return a copy of the compression object.\n"
919"\n"
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800920"zlib.Compress.copy()");
Larry Hastings31826802013-10-19 00:09:25 -0700921
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800922#define ZLIB_COMPRESS_COPY_METHODDEF \
923 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924
925static PyObject *
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800926zlib_Compress_copy(PyObject *self)
927/*[clinic checksum: 2551952e72329f0f2beb48a1dde3780e485a220b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928{
Larry Hastings31826802013-10-19 00:09:25 -0700929 compobject *zself = (compobject *)self;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930 compobject *retval = NULL;
931 int err;
932
933 retval = newcompobject(&Comptype);
934 if (!retval) return NULL;
935
936 /* Copy the zstream state
937 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
938 */
Larry Hastings31826802013-10-19 00:09:25 -0700939 ENTER_ZLIB(zself);
940 err = deflateCopy(&retval->zst, &zself->zst);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000941 switch(err) {
942 case(Z_OK):
943 break;
944 case(Z_STREAM_ERROR):
945 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
946 goto error;
947 case(Z_MEM_ERROR):
948 PyErr_SetString(PyExc_MemoryError,
949 "Can't allocate memory for compression object");
950 goto error;
951 default:
Larry Hastings31826802013-10-19 00:09:25 -0700952 zlib_error(zself->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 goto error;
954 }
Larry Hastings31826802013-10-19 00:09:25 -0700955 Py_INCREF(zself->unused_data);
956 Py_INCREF(zself->unconsumed_tail);
957 Py_XINCREF(zself->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958 Py_XDECREF(retval->unused_data);
959 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200960 Py_XDECREF(retval->zdict);
Larry Hastings31826802013-10-19 00:09:25 -0700961 retval->unused_data = zself->unused_data;
962 retval->unconsumed_tail = zself->unconsumed_tail;
963 retval->zdict = zself->zdict;
964 retval->eof = zself->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000965
966 /* Mark it as being initialized */
967 retval->is_initialised = 1;
968
Larry Hastings31826802013-10-19 00:09:25 -0700969 LEAVE_ZLIB(zself);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970 return (PyObject *)retval;
971
972error:
Larry Hastings31826802013-10-19 00:09:25 -0700973 LEAVE_ZLIB(zself);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974 Py_XDECREF(retval);
975 return NULL;
976}
977
978PyDoc_STRVAR(decomp_copy__doc__,
979"copy() -- Return a copy of the decompression object.");
980
981static PyObject *
982PyZlib_uncopy(compobject *self)
983{
984 compobject *retval = NULL;
985 int err;
986
987 retval = newcompobject(&Decomptype);
988 if (!retval) return NULL;
989
990 /* Copy the zstream state
991 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
992 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000993 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000994 err = inflateCopy(&retval->zst, &self->zst);
995 switch(err) {
996 case(Z_OK):
997 break;
998 case(Z_STREAM_ERROR):
999 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1000 goto error;
1001 case(Z_MEM_ERROR):
1002 PyErr_SetString(PyExc_MemoryError,
1003 "Can't allocate memory for decompression object");
1004 goto error;
1005 default:
1006 zlib_error(self->zst, err, "while copying decompression object");
1007 goto error;
1008 }
1009
1010 Py_INCREF(self->unused_data);
1011 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001012 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001013 Py_XDECREF(retval->unused_data);
1014 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001015 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001016 retval->unused_data = self->unused_data;
1017 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001018 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001019 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020
1021 /* Mark it as being initialized */
1022 retval->is_initialised = 1;
1023
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001024 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025 return (PyObject *)retval;
1026
1027error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001028 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001029 Py_XDECREF(retval);
1030 return NULL;
1031}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001032#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001033
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001034PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001035"flush( [length] ) -- Return a string containing any remaining\n"
1036"decompressed data. length, if given, is the initial size of the\n"
1037"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001038"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001039"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001040
Guido van Rossumfb221561997-04-29 15:38:09 +00001041static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001042PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001043{
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001044 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001045 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001046 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +00001047
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001048 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001049 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001050 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001051 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1052 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001053 }
Gregory P. Smith693fc462008-09-06 20:13:06 +00001054 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001055 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001056
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001057
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001058 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001059
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001060 start_total_out = self->zst.total_out;
Nadeem Vawda7ee95552012-11-11 03:15:32 +01001061 self->zst.avail_in = PyBytes_GET_SIZE(self->unconsumed_tail);
1062 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001063 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +00001064 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001065
1066 Py_BEGIN_ALLOW_THREADS
1067 err = inflate(&(self->zst), Z_FINISH);
1068 Py_END_ALLOW_THREADS
1069
1070 /* while Z_OK and the output buffer is full, there might be more output,
1071 so extend the output buffer and try again */
1072 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001073 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001074 Py_CLEAR(retval);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001075 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +00001076 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001077 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
1078 self->zst.avail_out = length;
1079 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001080
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001081 Py_BEGIN_ALLOW_THREADS
1082 err = inflate(&(self->zst), Z_FINISH);
1083 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +00001084 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001085
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001086 if (save_unconsumed_input(self, err) < 0) {
1087 Py_DECREF(retval);
1088 retval = NULL;
1089 goto error;
1090 }
1091
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001092 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001093 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001094 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001095 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +02001096 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001097 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001098 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001099 Py_DECREF(retval);
1100 retval = NULL;
1101 goto error;
1102 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001103 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001104
Gregory P. Smith693fc462008-09-06 20:13:06 +00001105 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Victor Stinner79799262013-07-09 00:35:22 +02001106 Py_CLEAR(retval);
Guido van Rossum776152b2007-05-22 22:44:07 +00001107 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001108
1109error:
1110
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001111 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001112
Jeremy Hylton9714f992001-10-16 21:19:45 +00001113 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +00001114}
1115
1116static PyMethodDef comp_methods[] =
1117{
Tim Peters977e5402001-10-17 03:57:20 +00001118 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001119 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001120 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001121 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001122#ifdef HAVE_ZLIB_COPY
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001123 ZLIB_COMPRESS_COPY_METHODDEF
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001124#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001125 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001126};
1127
1128static PyMethodDef Decomp_methods[] =
1129{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001130 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Tim Peters977e5402001-10-17 03:57:20 +00001131 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001132 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001133#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001134 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1135 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001136#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001137 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001138};
1139
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001140#define COMP_OFF(x) offsetof(compobject, x)
1141static PyMemberDef Decomp_members[] = {
1142 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1143 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001144 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001145 {NULL},
1146};
Guido van Rossumfb221561997-04-29 15:38:09 +00001147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001148PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001149"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1150"\n"
1151"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001152"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001153
Guido van Rossumfb221561997-04-29 15:38:09 +00001154static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001155PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001156{
Christian Heimescc47b052008-03-25 14:56:36 +00001157 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001158 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +00001159
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001160 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1161 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001162 /* Releasing the GIL for very small buffers is inefficient
1163 and may lower performance */
1164 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001165 unsigned char *buf = pbuf.buf;
1166 Py_ssize_t len = pbuf.len;
1167
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001168 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001169 /* Avoid truncation of length for very large buffers. adler32() takes
1170 length as an unsigned int, which may be narrower than Py_ssize_t. */
1171 while (len > (size_t) UINT_MAX) {
1172 adler32val = adler32(adler32val, buf, UINT_MAX);
1173 buf += (size_t) UINT_MAX;
1174 len -= (size_t) UINT_MAX;
1175 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001176 adler32val = adler32(adler32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001177 Py_END_ALLOW_THREADS
1178 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001179 adler32val = adler32(adler32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001180 }
1181 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001182 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001183}
Tim Peters977e5402001-10-17 03:57:20 +00001184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001185PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001186"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1187"\n"
1188"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001189"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001190
1191static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001192PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001193{
Christian Heimescc47b052008-03-25 14:56:36 +00001194 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001195 Py_buffer pbuf;
1196 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001197
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001198 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001199 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001200 /* Releasing the GIL for very small buffers is inefficient
1201 and may lower performance */
1202 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001203 unsigned char *buf = pbuf.buf;
1204 Py_ssize_t len = pbuf.len;
1205
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001206 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001207 /* Avoid truncation of length for very large buffers. crc32() takes
1208 length as an unsigned int, which may be narrower than Py_ssize_t. */
1209 while (len > (size_t) UINT_MAX) {
1210 crc32val = crc32(crc32val, buf, UINT_MAX);
1211 buf += (size_t) UINT_MAX;
1212 len -= (size_t) UINT_MAX;
1213 }
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001214 signed_val = crc32(crc32val, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001215 Py_END_ALLOW_THREADS
1216 } else {
Victor Stinnerbc8ccce2013-06-24 23:02:51 +02001217 signed_val = crc32(crc32val, pbuf.buf, (unsigned int)pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001218 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001219 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001220 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001221}
Tim Peters977e5402001-10-17 03:57:20 +00001222
Guido van Rossumfb221561997-04-29 15:38:09 +00001223
1224static PyMethodDef zlib_methods[] =
1225{
Tim Peters977e5402001-10-17 03:57:20 +00001226 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001227 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001228 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001229 compress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001230 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001231 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001232 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1233 crc32__doc__},
1234 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001235 decompress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001236 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001237 decompressobj__doc__},
1238 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001239};
1240
Tim Peters0c322792002-07-17 16:49:03 +00001241static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001242 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001243 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001244 sizeof(compobject),
1245 0,
1246 (destructor)Comp_dealloc, /*tp_dealloc*/
1247 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001248 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001249 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001250 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001251 0, /*tp_repr*/
1252 0, /*tp_as_number*/
1253 0, /*tp_as_sequence*/
1254 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001255 0, /*tp_hash*/
1256 0, /*tp_call*/
1257 0, /*tp_str*/
1258 0, /*tp_getattro*/
1259 0, /*tp_setattro*/
1260 0, /*tp_as_buffer*/
1261 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1262 0, /*tp_doc*/
1263 0, /*tp_traverse*/
1264 0, /*tp_clear*/
1265 0, /*tp_richcompare*/
1266 0, /*tp_weaklistoffset*/
1267 0, /*tp_iter*/
1268 0, /*tp_iternext*/
1269 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001270};
1271
Tim Peters0c322792002-07-17 16:49:03 +00001272static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001273 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001274 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001275 sizeof(compobject),
1276 0,
1277 (destructor)Decomp_dealloc, /*tp_dealloc*/
1278 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001279 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001280 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001281 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001282 0, /*tp_repr*/
1283 0, /*tp_as_number*/
1284 0, /*tp_as_sequence*/
1285 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001286 0, /*tp_hash*/
1287 0, /*tp_call*/
1288 0, /*tp_str*/
1289 0, /*tp_getattro*/
1290 0, /*tp_setattro*/
1291 0, /*tp_as_buffer*/
1292 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1293 0, /*tp_doc*/
1294 0, /*tp_traverse*/
1295 0, /*tp_clear*/
1296 0, /*tp_richcompare*/
1297 0, /*tp_weaklistoffset*/
1298 0, /*tp_iter*/
1299 0, /*tp_iternext*/
1300 Decomp_methods, /*tp_methods*/
1301 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001302};
1303
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001304PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001305"The functions in this module allow compression and decompression using the\n"
1306"zlib library, which is based on GNU zip.\n"
1307"\n"
1308"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001309"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001310"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001311"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001312"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001313"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001314"\n"
1315"'wbits' is window buffer size.\n"
1316"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001317"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001318
Martin v. Löwis1a214512008-06-11 05:26:20 +00001319static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001320 PyModuleDef_HEAD_INIT,
1321 "zlib",
1322 zlib_module_documentation,
1323 -1,
1324 zlib_methods,
1325 NULL,
1326 NULL,
1327 NULL,
1328 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001329};
1330
Mark Hammond62b1ab12002-07-23 06:31:15 +00001331PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001332PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001333{
Fred Drake4baedc12002-04-01 14:53:37 +00001334 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001335 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001336 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001337 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001338 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001339 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001340 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001341 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001342
Fred Drake4baedc12002-04-01 14:53:37 +00001343 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1344 if (ZlibError != NULL) {
1345 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001346 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001347 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001348 PyModule_AddIntMacro(m, MAX_WBITS);
1349 PyModule_AddIntMacro(m, DEFLATED);
1350 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
1351 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1352 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1353 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1354 PyModule_AddIntMacro(m, Z_FILTERED);
1355 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1356 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001357
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001358 PyModule_AddIntMacro(m, Z_FINISH);
1359 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1360 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1361 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001362
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001363 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001364 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001365 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001366
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001367 ver = PyUnicode_FromString(zlibVersion());
1368 if (ver != NULL)
1369 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1370
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001371 PyModule_AddStringConstant(m, "__version__", "1.0");
1372
Martin v. Löwis1a214512008-06-11 05:26:20 +00001373 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001374}