blob: 519af940c7e5ec7e1e488d528d67ffcb8f3d33c7 [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"
8#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +00009
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000010#ifdef WITH_THREAD
11#include "pythread.h"
12
13/* #defs ripped off from _tkinter.c, even though the situation here is much
14 simpler, because we don't have to worry about waiting for Tcl
15 events! And, since zlib itself is threadsafe, we don't need to worry
16 about re-entering zlib functions.
17
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018 N.B.
19
20 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
21 that modify the components of preexisting de/compress objects, it
22 could prove to be a performance gain on multiprocessor machines if
23 there was an de/compress object-specific lock. However, for the
24 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
25 de/compress objects.
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000026 */
27
28static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
29
30#define ENTER_ZLIB \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000031 Py_BEGIN_ALLOW_THREADS \
32 PyThread_acquire_lock(zlib_lock, 1); \
33 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000034
35#define LEAVE_ZLIB \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 PyThread_release_lock(zlib_lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000037
38#else
39
40#define ENTER_ZLIB
41#define LEAVE_ZLIB
42
43#endif
44
Guido van Rossumfb221561997-04-29 15:38:09 +000045/* The following parameters are copied from zutil.h, version 0.95 */
46#define DEFLATED 8
47#if MAX_MEM_LEVEL >= 8
48# define DEF_MEM_LEVEL 8
49#else
50# define DEF_MEM_LEVEL MAX_MEM_LEVEL
51#endif
52#define DEF_WBITS MAX_WBITS
53
Guido van Rossumb729a1d1999-04-07 20:23:17 +000054/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
55#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000056#define PyInit_zlib initzlib
57
Jeremy Hylton938ace62002-07-17 16:30:39 +000058static PyTypeObject Comptype;
59static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000060
61static PyObject *ZlibError;
62
Tim Peters977e5402001-10-17 03:57:20 +000063typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000064{
Jeremy Hylton9714f992001-10-16 21:19:45 +000065 PyObject_HEAD
66 z_stream zst;
67 PyObject *unused_data;
68 PyObject *unconsumed_tail;
69 int is_initialised;
Guido van Rossumfb221561997-04-29 15:38:09 +000070} compobject;
71
Jeremy Hylton0965e082001-10-16 21:56:09 +000072static void
73zlib_error(z_stream zst, int err, char *msg)
74{
Nadeem Vawdabbabbae2011-08-28 11:23:57 +020075 const char *zmsg = Z_NULL;
76 /* In case of a version mismatch, zst.msg won't be initialized.
77 Check for this case first, before looking at zst.msg. */
78 if (err == Z_VERSION_ERROR)
79 zmsg = "library version mismatch";
80 if (zmsg == Z_NULL)
81 zmsg = zst.msg;
Antoine Pitroufc3bfad2010-05-11 23:42:28 +000082 if (zmsg == Z_NULL) {
83 switch (err) {
84 case Z_BUF_ERROR:
85 zmsg = "incomplete or truncated stream";
86 break;
87 case Z_STREAM_ERROR:
88 zmsg = "inconsistent stream state";
89 break;
90 case Z_DATA_ERROR:
91 zmsg = "invalid input data";
92 break;
93 }
94 }
95 if (zmsg == Z_NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000096 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000097 else
Antoine Pitroufc3bfad2010-05-11 23:42:28 +000098 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000099}
100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000102"compressobj([level]) -- Return a compressor object.\n"
103"\n"
Nadeem Vawda99f9b8d2012-11-11 14:01:23 +0100104"Optional arg level is the compression level, in 0-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000107"decompressobj([wbits]) -- Return a decompressor object.\n"
108"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000110
Guido van Rossumfb221561997-04-29 15:38:09 +0000111static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000112newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000113{
Tim Peters977e5402001-10-17 03:57:20 +0000114 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000115 self = PyObject_New(compobject, type);
116 if (self == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000117 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000118 self->is_initialised = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000119 self->unused_data = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000120 if (self->unused_data == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 Py_DECREF(self);
122 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000123 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000124 self->unconsumed_tail = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 if (self->unconsumed_tail == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000126 Py_DECREF(self);
127 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000128 }
129 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000130}
131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000132PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000133"compress(string[, level]) -- Returned compressed string.\n"
134"\n"
Nadeem Vawda99f9b8d2012-11-11 14:01:23 +0100135"Optional arg level is the compression level, in 0-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000136
Guido van Rossumfb221561997-04-29 15:38:09 +0000137static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000138PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000139{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000140 PyObject *ReturnVal = NULL;
141 Byte *input, *output;
142 int length, level=Z_DEFAULT_COMPRESSION, err;
143 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000144
Jeremy Hylton9714f992001-10-16 21:19:45 +0000145 /* require Python string object, optional 'level' arg */
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000146 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000148
Jeremy Hylton9714f992001-10-16 21:19:45 +0000149 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000150
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000151 output = (Byte*)malloc(zst.avail_out);
152 if (output == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 PyErr_SetString(PyExc_MemoryError,
154 "Can't allocate memory to compress data");
155 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000156 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000157
Jeremy Hylton9714f992001-10-16 21:19:45 +0000158 /* Past the point of no return. From here on out, we need to make sure
159 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000160
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000161 zst.zalloc = (alloc_func)NULL;
162 zst.zfree = (free_func)Z_NULL;
163 zst.next_out = (Byte *)output;
164 zst.next_in = (Byte *)input;
165 zst.avail_in = length;
166 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000167
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000168 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000169 case(Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000171 case(Z_MEM_ERROR):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 PyErr_SetString(PyExc_MemoryError,
173 "Out of memory while compressing data");
174 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000175 case(Z_STREAM_ERROR):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000176 PyErr_SetString(ZlibError,
177 "Bad compression level");
178 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000179 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000180 deflateEnd(&zst);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000181 zlib_error(zst, err, "while compressing data");
182 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000183 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000184
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000185 Py_BEGIN_ALLOW_THREADS;
186 err = deflate(&zst, Z_FINISH);
187 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000188
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000189 if (err != Z_STREAM_END) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 zlib_error(zst, err, "while compressing data");
191 deflateEnd(&zst);
192 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000193 }
Tim Peters977e5402001-10-17 03:57:20 +0000194
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000195 err=deflateEnd(&zst);
196 if (err == Z_OK)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 ReturnVal = PyString_FromStringAndSize((char *)output,
198 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000199 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000201
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000202 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000203 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000204
Jeremy Hylton9714f992001-10-16 21:19:45 +0000205 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000206}
207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000209"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
210"\n"
211"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000213
Guido van Rossumfb221561997-04-29 15:38:09 +0000214static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000215PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000216{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000217 PyObject *result_str;
218 Byte *input;
219 int length, err;
Christian Heimes901071b2007-11-21 00:46:21 +0000220 int wsize=DEF_WBITS;
221 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000222 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Christian Heimes901071b2007-11-21 00:46:21 +0000224 if (!PyArg_ParseTuple(args, "s#|in:decompress",
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000225 &input, &length, &wsize, &r_strlen))
226 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000227
Jeremy Hylton9714f992001-10-16 21:19:45 +0000228 if (r_strlen <= 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000229 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000230
Jeremy Hylton9714f992001-10-16 21:19:45 +0000231 zst.avail_in = length;
232 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000233
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000234 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000235 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000236
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 zst.zalloc = (alloc_func)NULL;
238 zst.zfree = (free_func)Z_NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000239 zst.next_out = (Byte *)PyString_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000240 zst.next_in = (Byte *)input;
241 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000242
Jeremy Hylton9714f992001-10-16 21:19:45 +0000243 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 case(Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 break;
Tim Peters977e5402001-10-17 03:57:20 +0000246 case(Z_MEM_ERROR):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 PyErr_SetString(PyExc_MemoryError,
248 "Out of memory while decompressing data");
249 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000250 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000251 inflateEnd(&zst);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000252 zlib_error(zst, err, "while preparing to decompress data");
253 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000254 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000255
Jeremy Hylton9714f992001-10-16 21:19:45 +0000256 do {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000257 Py_BEGIN_ALLOW_THREADS
258 err=inflate(&zst, Z_FINISH);
259 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000260
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000261 switch(err) {
262 case(Z_STREAM_END):
263 break;
264 case(Z_BUF_ERROR):
265 /*
266 * If there is at least 1 byte of room according to zst.avail_out
267 * and we get this error, assume that it means zlib cannot
268 * process the inflate call() due to an error in the data.
269 */
270 if (zst.avail_out > 0) {
Antoine Pitroufc3bfad2010-05-11 23:42:28 +0000271 zlib_error(zst, err, "while decompressing data");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272 inflateEnd(&zst);
273 goto error;
274 }
275 /* fall through */
276 case(Z_OK):
277 /* need more memory */
278 if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
279 inflateEnd(&zst);
280 goto error;
281 }
282 zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
283 + r_strlen;
284 zst.avail_out = r_strlen;
285 r_strlen = r_strlen << 1;
286 break;
287 default:
288 inflateEnd(&zst);
289 zlib_error(zst, err, "while decompressing data");
290 goto error;
291 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000292 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000293
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000294 err = inflateEnd(&zst);
295 if (err != Z_OK) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000296 zlib_error(zst, err, "while finishing data decompression");
297 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000298 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000299
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000300 _PyString_Resize(&result_str, zst.total_out);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000301 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000302
303 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000304 Py_XDECREF(result_str);
305 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000306}
307
308static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000309PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000310{
Jeremy Hylton499000002001-10-16 21:59:35 +0000311 compobject *self;
312 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
313 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000314
Jeremy Hylton499000002001-10-16 21:59:35 +0000315 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000316 &memLevel, &strategy))
317 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000318
Jeremy Hylton499000002001-10-16 21:59:35 +0000319 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000320 if (self==NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000321 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000322 self->zst.zalloc = (alloc_func)NULL;
323 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000324 self->zst.next_in = NULL;
325 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000326 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
327 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000328 case (Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000329 self->is_initialised = 1;
330 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000331 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000332 Py_DECREF(self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000333 PyErr_SetString(PyExc_MemoryError,
334 "Can't allocate memory for compression object");
335 return NULL;
336 case(Z_STREAM_ERROR):
337 Py_DECREF(self);
338 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
339 return NULL;
340 default:
341 zlib_error(self->zst, err, "while creating compression object");
342 Py_DECREF(self);
343 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000344 }
345}
346
347static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000348PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000349{
Jeremy Hylton499000002001-10-16 21:59:35 +0000350 int wbits=DEF_WBITS, err;
351 compobject *self;
352 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000354
355 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000356 if (self == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000357 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000358 self->zst.zalloc = (alloc_func)NULL;
359 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000360 self->zst.next_in = NULL;
361 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000362 err = inflateInit2(&self->zst, wbits);
363 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000364 case (Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 self->is_initialised = 1;
366 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000367 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000368 Py_DECREF(self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000369 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
370 return NULL;
371 case (Z_MEM_ERROR):
372 Py_DECREF(self);
373 PyErr_SetString(PyExc_MemoryError,
374 "Can't allocate memory for decompression object");
375 return NULL;
376 default:
377 zlib_error(self->zst, err, "while creating decompression object");
378 Py_DECREF(self);
379 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000380 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000381}
382
383static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000384Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000385{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000386 if (self->is_initialised)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000387 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000388 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000389 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000390 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000391}
392
393static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000394Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000395{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000396 if (self->is_initialised)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000398 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000399 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000400 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000401}
402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000403PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000404"compress(data) -- Return a string containing data compressed.\n"
405"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000406"After calling this function, some of the input data may still\n"
407"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000408"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000409
410
Guido van Rossumfb221561997-04-29 15:38:09 +0000411static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000412PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000413{
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000414 int err, inplen;
415 Py_ssize_t length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000416 PyObject *RetVal;
417 Byte *input;
418 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000419
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000420 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000421 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000422
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000423 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000424 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000425
426 ENTER_ZLIB
427
Jeremy Hylton9714f992001-10-16 21:19:45 +0000428 start_total_out = self->zst.total_out;
429 self->zst.avail_in = inplen;
430 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000431 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000432 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000433
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000434 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000435 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000436 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000437
Jeremy Hylton9714f992001-10-16 21:19:45 +0000438 /* while Z_OK and the output buffer is full, there might be more output,
439 so extend the output buffer and try again */
440 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000441 if (_PyString_Resize(&RetVal, length << 1) < 0)
442 goto error;
443 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
444 + length;
445 self->zst.avail_out = length;
446 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000447
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000448 Py_BEGIN_ALLOW_THREADS
449 err = deflate(&(self->zst), Z_NO_FLUSH);
450 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000451 }
Tim Peters977e5402001-10-17 03:57:20 +0000452 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000453 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000454 condition.
455 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000456
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000457 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 zlib_error(self->zst, err, "while compressing");
459 Py_DECREF(RetVal);
460 RetVal = NULL;
461 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000462 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000463 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000464
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000465 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000466 LEAVE_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000467 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000468}
469
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100470/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
471 self->unused_data or self->unconsumed_tail, as appropriate. */
472static int
473save_unconsumed_input(compobject *self, int err)
474{
475 if (err == Z_STREAM_END) {
476 /* The end of the compressed data has been reached. Store the leftover
477 input data in self->unused_data. */
478 if (self->zst.avail_in > 0) {
479 Py_ssize_t old_size = PyString_GET_SIZE(self->unused_data);
480 Py_ssize_t new_size;
481 PyObject *new_data;
482 if (self->zst.avail_in > PY_SSIZE_T_MAX - old_size) {
483 PyErr_NoMemory();
484 return -1;
485 }
486 new_size = old_size + self->zst.avail_in;
487 new_data = PyString_FromStringAndSize(NULL, new_size);
488 if (new_data == NULL)
489 return -1;
490 Py_MEMCPY(PyString_AS_STRING(new_data),
491 PyString_AS_STRING(self->unused_data), old_size);
492 Py_MEMCPY(PyString_AS_STRING(new_data) + old_size,
493 self->zst.next_in, self->zst.avail_in);
494 Py_DECREF(self->unused_data);
495 self->unused_data = new_data;
496 self->zst.avail_in = 0;
497 }
498 }
499 if (self->zst.avail_in > 0 || PyString_GET_SIZE(self->unconsumed_tail)) {
500 /* This code handles two distinct cases:
501 1. Output limit was reached. Save leftover input in unconsumed_tail.
502 2. All input data was consumed. Clear unconsumed_tail. */
503 PyObject *new_data = PyString_FromStringAndSize(
504 (char *)self->zst.next_in, self->zst.avail_in);
505 if (new_data == NULL)
506 return -1;
507 Py_DECREF(self->unconsumed_tail);
508 self->unconsumed_tail = new_data;
509 }
510 return 0;
511}
512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000513PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000514"decompress(data, max_length) -- Return a string containing the decompressed\n"
515"version of the data.\n"
516"\n"
517"After calling this function, some of the input data may still be stored in\n"
518"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000519"Call the flush() method to clear these buffers.\n"
520"If the max_length parameter is specified then the return value will be\n"
521"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000523
Guido van Rossumfb221561997-04-29 15:38:09 +0000524static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000525PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000526{
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000527 int err, inplen, max_length = 0;
528 Py_ssize_t old_length, length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000529 PyObject *RetVal;
530 Byte *input;
531 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000532
Tim Peters977e5402001-10-17 03:57:20 +0000533 if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000534 &inplen, &max_length))
535 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000536 if (max_length < 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000537 PyErr_SetString(PyExc_ValueError,
538 "max_length must be greater than zero");
539 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000540 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000541
Jeremy Hylton9714f992001-10-16 21:19:45 +0000542 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000543 if (max_length && length > max_length)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000544 length = max_length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000545 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000547
548 ENTER_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000549
Jeremy Hylton9714f992001-10-16 21:19:45 +0000550 start_total_out = self->zst.total_out;
551 self->zst.avail_in = inplen;
552 self->zst.next_in = input;
553 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000554 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000555
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000556 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000557 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000558 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000559
Jeremy Hylton9714f992001-10-16 21:19:45 +0000560 /* While Z_OK and the output buffer is full, there might be more output.
561 So extend the output buffer and try again.
562 */
Tim Peters977e5402001-10-17 03:57:20 +0000563 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000564 /* If max_length set, don't continue decompressing if we've already
565 reached the limit.
566 */
567 if (max_length && length >= max_length)
568 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000569
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000570 /* otherwise, ... */
571 old_length = length;
572 length = length << 1;
573 if (max_length && length > max_length)
574 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000575
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000576 if (_PyString_Resize(&RetVal, length) < 0)
577 goto error;
578 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
579 + old_length;
580 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000581
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000582 Py_BEGIN_ALLOW_THREADS
583 err = inflate(&(self->zst), Z_SYNC_FLUSH);
584 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000585 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000586
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100587 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda0cc4fd92011-05-14 14:29:07 +0200588 Py_DECREF(RetVal);
589 RetVal = NULL;
590 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000591 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000592
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100593 /* This is the logical place to call inflateEnd, but the old behaviour of
594 only calling it on flush() is preserved. */
595
596 if (err != Z_STREAM_END && err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000597 /* We will only get Z_BUF_ERROR if the output buffer was full
598 but there wasn't more output when we tried again, so it is
599 not an error condition.
600 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000601 zlib_error(self->zst, err, "while decompressing");
602 Py_DECREF(RetVal);
603 RetVal = NULL;
604 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000605 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000606
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000607 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000608
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000609 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000610 LEAVE_ZLIB
611
612 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000613}
614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000615PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000616"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000617"\n"
618"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000619"default value used when mode is not specified is Z_FINISH.\n"
620"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000621"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000622
Guido van Rossumfb221561997-04-29 15:38:09 +0000623static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000624PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000625{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000626 int err, length = DEFAULTALLOC;
627 PyObject *RetVal;
628 int flushmode = Z_FINISH;
629 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000630
Jeremy Hylton9714f992001-10-16 21:19:45 +0000631 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000632 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000633
Jeremy Hylton9714f992001-10-16 21:19:45 +0000634 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
635 doing any work at all; just return an empty string. */
636 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000638 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000639
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000640 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000641 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000642
643 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000644
Jeremy Hylton9714f992001-10-16 21:19:45 +0000645 start_total_out = self->zst.total_out;
646 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000647 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000648 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000649
650 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000651 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000652 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000653
Jeremy Hylton9714f992001-10-16 21:19:45 +0000654 /* while Z_OK and the output buffer is full, there might be more output,
655 so extend the output buffer and try again */
656 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 if (_PyString_Resize(&RetVal, length << 1) < 0)
658 goto error;
659 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
660 + length;
661 self->zst.avail_out = length;
662 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000663
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 Py_BEGIN_ALLOW_THREADS
665 err = deflate(&(self->zst), flushmode);
666 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000667 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000668
Jeremy Hylton9714f992001-10-16 21:19:45 +0000669 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000670 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000671 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000672 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000673 err = deflateEnd(&(self->zst));
674 if (err != Z_OK) {
675 zlib_error(self->zst, err, "from deflateEnd()");
676 Py_DECREF(RetVal);
677 RetVal = NULL;
678 goto error;
679 }
680 else
681 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000682
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000683 /* We will only get Z_BUF_ERROR if the output buffer was full
684 but there wasn't more output when we tried again, so it is
685 not an error condition.
686 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000687 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000688 zlib_error(self->zst, err, "while flushing");
689 Py_DECREF(RetVal);
690 RetVal = NULL;
691 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000692 }
Tim Peters977e5402001-10-17 03:57:20 +0000693
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000694 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000695
Tim Peters977e5402001-10-17 03:57:20 +0000696 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000697 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000698
699 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000700}
701
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000702#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000703PyDoc_STRVAR(comp_copy__doc__,
704"copy() -- Return a copy of the compression object.");
705
706static PyObject *
707PyZlib_copy(compobject *self)
708{
709 compobject *retval = NULL;
710 int err;
711
712 retval = newcompobject(&Comptype);
713 if (!retval) return NULL;
714
715 /* Copy the zstream state
716 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
717 */
718 ENTER_ZLIB
719 err = deflateCopy(&retval->zst, &self->zst);
720 switch(err) {
721 case(Z_OK):
722 break;
723 case(Z_STREAM_ERROR):
724 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
725 goto error;
726 case(Z_MEM_ERROR):
727 PyErr_SetString(PyExc_MemoryError,
728 "Can't allocate memory for compression object");
729 goto error;
730 default:
731 zlib_error(self->zst, err, "while copying compression object");
732 goto error;
733 }
734
Tim Peters402cc242006-05-17 01:30:11 +0000735 Py_INCREF(self->unused_data);
736 Py_INCREF(self->unconsumed_tail);
737 Py_XDECREF(retval->unused_data);
738 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000739 retval->unused_data = self->unused_data;
740 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000741
742 /* Mark it as being initialized */
743 retval->is_initialised = 1;
744
745 LEAVE_ZLIB
746 return (PyObject *)retval;
747
748error:
749 LEAVE_ZLIB
Tim Peters402cc242006-05-17 01:30:11 +0000750 Py_XDECREF(retval);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000751 return NULL;
752}
753
754PyDoc_STRVAR(decomp_copy__doc__,
755"copy() -- Return a copy of the decompression object.");
756
757static PyObject *
758PyZlib_uncopy(compobject *self)
759{
760 compobject *retval = NULL;
761 int err;
762
763 retval = newcompobject(&Decomptype);
764 if (!retval) return NULL;
765
766 /* Copy the zstream state
767 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
768 */
769 ENTER_ZLIB
770 err = inflateCopy(&retval->zst, &self->zst);
771 switch(err) {
772 case(Z_OK):
773 break;
774 case(Z_STREAM_ERROR):
775 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
776 goto error;
777 case(Z_MEM_ERROR):
778 PyErr_SetString(PyExc_MemoryError,
779 "Can't allocate memory for decompression object");
780 goto error;
781 default:
782 zlib_error(self->zst, err, "while copying decompression object");
783 goto error;
784 }
785
Tim Peters402cc242006-05-17 01:30:11 +0000786 Py_INCREF(self->unused_data);
787 Py_INCREF(self->unconsumed_tail);
788 Py_XDECREF(retval->unused_data);
789 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000790 retval->unused_data = self->unused_data;
791 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000792
793 /* Mark it as being initialized */
794 retval->is_initialised = 1;
795
796 LEAVE_ZLIB
797 return (PyObject *)retval;
798
799error:
800 LEAVE_ZLIB
801 Py_XDECREF(retval);
802 return NULL;
803}
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000804#endif
Georg Brandl8d3342b2006-05-16 07:38:27 +0000805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000806PyDoc_STRVAR(decomp_flush__doc__,
Georg Brandl22a9dc82006-04-01 07:39:41 +0000807"flush( [length] ) -- Return a string containing any remaining\n"
808"decompressed data. length, if given, is the initial size of the\n"
809"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000810"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000811"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000812
Guido van Rossumfb221561997-04-29 15:38:09 +0000813static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000814PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000815{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000816 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000817 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000818 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000819
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000820 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000822 if (length <= 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000823 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
824 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000825 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000826 if (!(retval = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000827 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000828
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000829
Jeremy Hylton9714f992001-10-16 21:19:45 +0000830 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000831
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000832 start_total_out = self->zst.total_out;
Nadeem Vawdaacfdfda2012-11-11 03:28:21 +0100833 self->zst.avail_in = PyString_GET_SIZE(self->unconsumed_tail);
834 self->zst.next_in = (Byte *)PyString_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000835 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000836 self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000837
838 Py_BEGIN_ALLOW_THREADS
839 err = inflate(&(self->zst), Z_FINISH);
840 Py_END_ALLOW_THREADS
841
842 /* while Z_OK and the output buffer is full, there might be more output,
843 so extend the output buffer and try again */
844 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000845 if (_PyString_Resize(&retval, length << 1) < 0)
846 goto error;
847 self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
848 self->zst.avail_out = length;
849 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000850
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000851 Py_BEGIN_ALLOW_THREADS
852 err = inflate(&(self->zst), Z_FINISH);
853 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000855
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100856 if (save_unconsumed_input(self, err) < 0) {
857 Py_DECREF(retval);
858 retval = NULL;
859 goto error;
860 }
861
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000862 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
863 various data structures. Note we should only get Z_STREAM_END when
864 flushmode is Z_FINISH */
865 if (err == Z_STREAM_END) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000866 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000867 self->is_initialised = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000868 if (err != Z_OK) {
869 zlib_error(self->zst, err, "from inflateEnd()");
870 Py_DECREF(retval);
871 retval = NULL;
872 goto error;
873 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000874 }
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100875
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000876 _PyString_Resize(&retval, self->zst.total_out - start_total_out);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000877
878error:
879
Jeremy Hylton9714f992001-10-16 21:19:45 +0000880 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000881
Jeremy Hylton9714f992001-10-16 21:19:45 +0000882 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000883}
884
885static PyMethodDef comp_methods[] =
886{
Tim Peters977e5402001-10-17 03:57:20 +0000887 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000888 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000889 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000890 comp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000891#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000892 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
893 comp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000894#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000895 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000896};
897
898static PyMethodDef Decomp_methods[] =
899{
Tim Peters977e5402001-10-17 03:57:20 +0000900 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000901 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000902 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000903 decomp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000904#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000905 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
906 decomp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000907#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000908 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000909};
910
911static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000912Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000913{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000914 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
915 internal data. */
916
917 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000918}
919
920static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000921Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000922{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000923 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000924
Jeremy Hylton9714f992001-10-16 21:19:45 +0000925 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000926
Tim Peters977e5402001-10-17 03:57:20 +0000927 if (strcmp(name, "unused_data") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000928 Py_INCREF(self->unused_data);
929 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000930 } else if (strcmp(name, "unconsumed_tail") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000931 Py_INCREF(self->unconsumed_tail);
932 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000933 } else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000935
Jeremy Hylton9714f992001-10-16 21:19:45 +0000936 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000937
Jeremy Hylton9714f992001-10-16 21:19:45 +0000938 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000939}
940
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000941PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000942"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
943"\n"
944"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000945"a signed integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000946
Guido van Rossumfb221561997-04-29 15:38:09 +0000947static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000948PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000949{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000950 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000951 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000952 int len, signed_val;
Tim Peters977e5402001-10-17 03:57:20 +0000953
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000954 if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000956 /* In Python 2.x we return a signed integer regardless of native platform
957 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
958 * extended into a 64-bit long inside the integer object). 3.0 does the
959 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
960 signed_val = adler32(adler32val, buf, len);
961 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000962}
Tim Peters977e5402001-10-17 03:57:20 +0000963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000964PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000965"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
966"\n"
967"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000968"a signed integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000969
970static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000971PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000972{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000973 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000974 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000975 int len, signed_val;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000976
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000977 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000978 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000979 /* In Python 2.x we return a signed integer regardless of native platform
980 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
981 * extended into a 64-bit long inside the integer object). 3.0 does the
982 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
983 signed_val = crc32(crc32val, buf, len);
984 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000985}
Tim Peters977e5402001-10-17 03:57:20 +0000986
Guido van Rossumfb221561997-04-29 15:38:09 +0000987
988static PyMethodDef zlib_methods[] =
989{
Tim Peters977e5402001-10-17 03:57:20 +0000990 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000991 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000992 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000993 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000994 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000995 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000996 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
997 crc32__doc__},
998 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000999 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001000 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001001 decompressobj__doc__},
1002 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001003};
1004
Tim Peters0c322792002-07-17 16:49:03 +00001005static PyTypeObject Comptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001006 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001007 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001008 sizeof(compobject),
1009 0,
1010 (destructor)Comp_dealloc, /*tp_dealloc*/
1011 0, /*tp_print*/
1012 (getattrfunc)Comp_getattr, /*tp_getattr*/
1013 0, /*tp_setattr*/
1014 0, /*tp_compare*/
1015 0, /*tp_repr*/
1016 0, /*tp_as_number*/
1017 0, /*tp_as_sequence*/
1018 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001019};
1020
Tim Peters0c322792002-07-17 16:49:03 +00001021static PyTypeObject Decomptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001022 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001023 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001024 sizeof(compobject),
1025 0,
1026 (destructor)Decomp_dealloc, /*tp_dealloc*/
1027 0, /*tp_print*/
1028 (getattrfunc)Decomp_getattr, /*tp_getattr*/
1029 0, /*tp_setattr*/
1030 0, /*tp_compare*/
1031 0, /*tp_repr*/
1032 0, /*tp_as_number*/
1033 0, /*tp_as_sequence*/
1034 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001035};
1036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001037PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001038"The functions in this module allow compression and decompression using the\n"
1039"zlib library, which is based on GNU zip.\n"
1040"\n"
1041"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda99f9b8d2012-11-11 14:01:23 +01001042"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001043"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001044"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001045"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001046"decompressobj([wbits]) -- Return a decompressor object.\n"
1047"\n"
1048"'wbits' is window buffer size.\n"
1049"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001050"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001051
Mark Hammond62b1ab12002-07-23 06:31:15 +00001052PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001053PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001054{
Fred Drake4baedc12002-04-01 14:53:37 +00001055 PyObject *m, *ver;
Christian Heimese93237d2007-12-19 02:37:44 +00001056 Py_TYPE(&Comptype) = &PyType_Type;
1057 Py_TYPE(&Decomptype) = &PyType_Type;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001058 m = Py_InitModule4("zlib", zlib_methods,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001059 zlib_module_documentation,
1060 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001061 if (m == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001062 return;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001063
Fred Drake4baedc12002-04-01 14:53:37 +00001064 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1065 if (ZlibError != NULL) {
1066 Py_INCREF(ZlibError);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001067 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001068 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001069 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1070 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1071 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1072 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1073 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1074 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1075 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1076 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1077 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001078
Jeremy Hylton9714f992001-10-16 21:19:45 +00001079 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1080 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1081 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1082 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001083
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001084 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001085 if (ver != NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001086 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001087
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001088 PyModule_AddStringConstant(m, "__version__", "1.0");
1089
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001090#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001091 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001092#endif /* WITH_THREAD */
Guido van Rossumfb221561997-04-29 15:38:09 +00001093}