blob: bd6d6e256bc919855079ef2cda7ee0794e66aa4e [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{
Antoine Pitroufc3bfad2010-05-11 23:42:28 +000075 const char *zmsg = zst.msg;
76 if (zmsg == Z_NULL) {
77 switch (err) {
78 case Z_BUF_ERROR:
79 zmsg = "incomplete or truncated stream";
80 break;
81 case Z_STREAM_ERROR:
82 zmsg = "inconsistent stream state";
83 break;
84 case Z_DATA_ERROR:
85 zmsg = "invalid input data";
86 break;
87 }
88 }
89 if (zmsg == Z_NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000090 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000091 else
Antoine Pitroufc3bfad2010-05-11 23:42:28 +000092 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000093}
94
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000095PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000096"compressobj([level]) -- Return a compressor object.\n"
97"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000100PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000101"decompressobj([wbits]) -- Return a decompressor object.\n"
102"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000103"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000104
Guido van Rossumfb221561997-04-29 15:38:09 +0000105static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000106newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000107{
Tim Peters977e5402001-10-17 03:57:20 +0000108 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000109 self = PyObject_New(compobject, type);
110 if (self == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000112 self->is_initialised = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000113 self->unused_data = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 if (self->unused_data == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000115 Py_DECREF(self);
116 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000117 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000118 self->unconsumed_tail = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 if (self->unconsumed_tail == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000120 Py_DECREF(self);
121 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 }
123 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000124}
125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000126PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000127"compress(string[, level]) -- Returned compressed string.\n"
128"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000129"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000130
Guido van Rossumfb221561997-04-29 15:38:09 +0000131static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000132PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000133{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000134 PyObject *ReturnVal = NULL;
135 Byte *input, *output;
136 int length, level=Z_DEFAULT_COMPRESSION, err;
137 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000138
Jeremy Hylton9714f992001-10-16 21:19:45 +0000139 /* require Python string object, optional 'level' arg */
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000140 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000142
Jeremy Hylton9714f992001-10-16 21:19:45 +0000143 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000144
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000145 output = (Byte*)malloc(zst.avail_out);
146 if (output == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 PyErr_SetString(PyExc_MemoryError,
148 "Can't allocate memory to compress data");
149 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000150 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000151
Jeremy Hylton9714f992001-10-16 21:19:45 +0000152 /* Past the point of no return. From here on out, we need to make sure
153 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000154
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000155 zst.zalloc = (alloc_func)NULL;
156 zst.zfree = (free_func)Z_NULL;
157 zst.next_out = (Byte *)output;
158 zst.next_in = (Byte *)input;
159 zst.avail_in = length;
160 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000161
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000162 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000163 case(Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000164 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000165 case(Z_MEM_ERROR):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000166 PyErr_SetString(PyExc_MemoryError,
167 "Out of memory while compressing data");
168 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000169 case(Z_STREAM_ERROR):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 PyErr_SetString(ZlibError,
171 "Bad compression level");
172 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000173 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000174 deflateEnd(&zst);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 zlib_error(zst, err, "while compressing data");
176 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000177 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000178
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000179 Py_BEGIN_ALLOW_THREADS;
180 err = deflate(&zst, Z_FINISH);
181 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000182
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000183 if (err != Z_STREAM_END) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 zlib_error(zst, err, "while compressing data");
185 deflateEnd(&zst);
186 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000187 }
Tim Peters977e5402001-10-17 03:57:20 +0000188
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000189 err=deflateEnd(&zst);
190 if (err == Z_OK)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 ReturnVal = PyString_FromStringAndSize((char *)output,
192 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000193 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000194 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000195
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000196 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000197 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000198
Jeremy Hylton9714f992001-10-16 21:19:45 +0000199 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000200}
201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000202PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000203"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
204"\n"
205"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000206"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000207
Guido van Rossumfb221561997-04-29 15:38:09 +0000208static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000209PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000210{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000211 PyObject *result_str;
212 Byte *input;
213 int length, err;
Christian Heimes901071b2007-11-21 00:46:21 +0000214 int wsize=DEF_WBITS;
215 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000216 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000217
Christian Heimes901071b2007-11-21 00:46:21 +0000218 if (!PyArg_ParseTuple(args, "s#|in:decompress",
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000219 &input, &length, &wsize, &r_strlen))
220 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000221
Jeremy Hylton9714f992001-10-16 21:19:45 +0000222 if (r_strlen <= 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000224
Jeremy Hylton9714f992001-10-16 21:19:45 +0000225 zst.avail_in = length;
226 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000227
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000228 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000229 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000230
Jeremy Hylton9714f992001-10-16 21:19:45 +0000231 zst.zalloc = (alloc_func)NULL;
232 zst.zfree = (free_func)Z_NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000233 zst.next_out = (Byte *)PyString_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000234 zst.next_in = (Byte *)input;
235 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000236
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000238 case(Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 break;
Tim Peters977e5402001-10-17 03:57:20 +0000240 case(Z_MEM_ERROR):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 PyErr_SetString(PyExc_MemoryError,
242 "Out of memory while decompressing data");
243 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000244 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000245 inflateEnd(&zst);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000246 zlib_error(zst, err, "while preparing to decompress data");
247 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000248 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000249
Jeremy Hylton9714f992001-10-16 21:19:45 +0000250 do {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 Py_BEGIN_ALLOW_THREADS
252 err=inflate(&zst, Z_FINISH);
253 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000254
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000255 switch(err) {
256 case(Z_STREAM_END):
257 break;
258 case(Z_BUF_ERROR):
259 /*
260 * If there is at least 1 byte of room according to zst.avail_out
261 * and we get this error, assume that it means zlib cannot
262 * process the inflate call() due to an error in the data.
263 */
264 if (zst.avail_out > 0) {
Antoine Pitroufc3bfad2010-05-11 23:42:28 +0000265 zlib_error(zst, err, "while decompressing data");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 inflateEnd(&zst);
267 goto error;
268 }
269 /* fall through */
270 case(Z_OK):
271 /* need more memory */
272 if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
273 inflateEnd(&zst);
274 goto error;
275 }
276 zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
277 + r_strlen;
278 zst.avail_out = r_strlen;
279 r_strlen = r_strlen << 1;
280 break;
281 default:
282 inflateEnd(&zst);
283 zlib_error(zst, err, "while decompressing data");
284 goto error;
285 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000286 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000287
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000288 err = inflateEnd(&zst);
289 if (err != Z_OK) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000290 zlib_error(zst, err, "while finishing data decompression");
291 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000292 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000293
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000294 _PyString_Resize(&result_str, zst.total_out);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000295 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000296
297 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000298 Py_XDECREF(result_str);
299 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000300}
301
302static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000303PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000304{
Jeremy Hylton499000002001-10-16 21:59:35 +0000305 compobject *self;
306 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
307 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000308
Jeremy Hylton499000002001-10-16 21:59:35 +0000309 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310 &memLevel, &strategy))
311 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000312
Jeremy Hylton499000002001-10-16 21:59:35 +0000313 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000314 if (self==NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000315 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000316 self->zst.zalloc = (alloc_func)NULL;
317 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000318 self->zst.next_in = NULL;
319 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000320 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
321 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000322 case (Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000323 self->is_initialised = 1;
324 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000325 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000326 Py_DECREF(self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000327 PyErr_SetString(PyExc_MemoryError,
328 "Can't allocate memory for compression object");
329 return NULL;
330 case(Z_STREAM_ERROR):
331 Py_DECREF(self);
332 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
333 return NULL;
334 default:
335 zlib_error(self->zst, err, "while creating compression object");
336 Py_DECREF(self);
337 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000338 }
339}
340
341static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000342PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000343{
Jeremy Hylton499000002001-10-16 21:59:35 +0000344 int wbits=DEF_WBITS, err;
345 compobject *self;
346 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000348
349 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000350 if (self == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000351 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000352 self->zst.zalloc = (alloc_func)NULL;
353 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000354 self->zst.next_in = NULL;
355 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000356 err = inflateInit2(&self->zst, wbits);
357 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000358 case (Z_OK):
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000359 self->is_initialised = 1;
360 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000361 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000362 Py_DECREF(self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
364 return NULL;
365 case (Z_MEM_ERROR):
366 Py_DECREF(self);
367 PyErr_SetString(PyExc_MemoryError,
368 "Can't allocate memory for decompression object");
369 return NULL;
370 default:
371 zlib_error(self->zst, err, "while creating decompression object");
372 Py_DECREF(self);
373 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000374 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000375}
376
377static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000378Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000379{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000380 if (self->is_initialised)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000381 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000382 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000383 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000384 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000385}
386
387static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000388Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000389{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000390 if (self->is_initialised)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000391 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000392 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000393 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000394 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000395}
396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000398"compress(data) -- Return a string containing data compressed.\n"
399"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000400"After calling this function, some of the input data may still\n"
401"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000402"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000403
404
Guido van Rossumfb221561997-04-29 15:38:09 +0000405static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000406PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000407{
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000408 int err, inplen;
409 Py_ssize_t length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000410 PyObject *RetVal;
411 Byte *input;
412 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000413
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000414 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000416
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000417 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000419
420 ENTER_ZLIB
421
Jeremy Hylton9714f992001-10-16 21:19:45 +0000422 start_total_out = self->zst.total_out;
423 self->zst.avail_in = inplen;
424 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000425 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000426 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000427
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000428 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000429 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000430 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000431
Jeremy Hylton9714f992001-10-16 21:19:45 +0000432 /* while Z_OK and the output buffer is full, there might be more output,
433 so extend the output buffer and try again */
434 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000435 if (_PyString_Resize(&RetVal, length << 1) < 0)
436 goto error;
437 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
438 + length;
439 self->zst.avail_out = length;
440 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000441
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000442 Py_BEGIN_ALLOW_THREADS
443 err = deflate(&(self->zst), Z_NO_FLUSH);
444 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000445 }
Tim Peters977e5402001-10-17 03:57:20 +0000446 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000447 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000448 condition.
449 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000450
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000451 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 zlib_error(self->zst, err, "while compressing");
453 Py_DECREF(RetVal);
454 RetVal = NULL;
455 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000456 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000457 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000458
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000459 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000460 LEAVE_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000461 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000462}
463
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000464PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000465"decompress(data, max_length) -- Return a string containing the decompressed\n"
466"version of the data.\n"
467"\n"
468"After calling this function, some of the input data may still be stored in\n"
469"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000470"Call the flush() method to clear these buffers.\n"
471"If the max_length parameter is specified then the return value will be\n"
472"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000473"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000474
Guido van Rossumfb221561997-04-29 15:38:09 +0000475static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000476PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000477{
Antoine Pitrou3843cd82010-05-07 16:50:34 +0000478 int err, inplen, max_length = 0;
479 Py_ssize_t old_length, length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000480 PyObject *RetVal;
481 Byte *input;
482 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000483
Tim Peters977e5402001-10-17 03:57:20 +0000484 if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 &inplen, &max_length))
486 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000487 if (max_length < 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 PyErr_SetString(PyExc_ValueError,
489 "max_length must be greater than zero");
490 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000491 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000492
Jeremy Hylton9714f992001-10-16 21:19:45 +0000493 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000494 if (max_length && length > max_length)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 length = max_length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000496 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000497 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000498
499 ENTER_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000500
Jeremy Hylton9714f992001-10-16 21:19:45 +0000501 start_total_out = self->zst.total_out;
502 self->zst.avail_in = inplen;
503 self->zst.next_in = input;
504 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000505 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000506
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000507 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000508 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000509 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000510
Jeremy Hylton9714f992001-10-16 21:19:45 +0000511 /* While Z_OK and the output buffer is full, there might be more output.
512 So extend the output buffer and try again.
513 */
Tim Peters977e5402001-10-17 03:57:20 +0000514 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 /* If max_length set, don't continue decompressing if we've already
516 reached the limit.
517 */
518 if (max_length && length >= max_length)
519 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000520
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000521 /* otherwise, ... */
522 old_length = length;
523 length = length << 1;
524 if (max_length && length > max_length)
525 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000526
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000527 if (_PyString_Resize(&RetVal, length) < 0)
528 goto error;
529 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
530 + old_length;
531 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000532
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 Py_BEGIN_ALLOW_THREADS
534 err = inflate(&(self->zst), Z_SYNC_FLUSH);
535 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000536 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000537
Jeremy Hylton9714f992001-10-16 21:19:45 +0000538 if(max_length) {
Nadeem Vawda0cc4fd92011-05-14 14:29:07 +0200539 /* Not all of the compressed data could be accommodated in a buffer of
540 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000541 Py_DECREF(self->unconsumed_tail);
542 self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
543 self->zst.avail_in);
Nadeem Vawda0cc4fd92011-05-14 14:29:07 +0200544 }
545 else if (PyString_GET_SIZE(self->unconsumed_tail) > 0) {
546 /* All of the compressed data was consumed. Clear unconsumed_tail. */
547 Py_DECREF(self->unconsumed_tail);
548 self->unconsumed_tail = PyString_FromStringAndSize("", 0);
549 }
550 if(!self->unconsumed_tail) {
551 Py_DECREF(RetVal);
552 RetVal = NULL;
553 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000554 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000555
Tim Peters977e5402001-10-17 03:57:20 +0000556 /* The end of the compressed data has been reached, so set the
557 unused_data attribute to a string containing the remainder of the
558 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000559 inflateEnd, but the old behaviour of only calling it on flush() is
560 preserved.
561 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000562 if (err == Z_STREAM_END) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000563 Py_XDECREF(self->unused_data); /* Free original empty string */
564 self->unused_data = PyString_FromStringAndSize(
565 (char *)self->zst.next_in, self->zst.avail_in);
566 if (self->unused_data == NULL) {
567 Py_DECREF(RetVal);
568 goto error;
569 }
570 /* We will only get Z_BUF_ERROR if the output buffer was full
571 but there wasn't more output when we tried again, so it is
572 not an error condition.
573 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000574 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000575 zlib_error(self->zst, err, "while decompressing");
576 Py_DECREF(RetVal);
577 RetVal = NULL;
578 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000579 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000580
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000581 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000582
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000583 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000584 LEAVE_ZLIB
585
586 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000587}
588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000589PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000590"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000591"\n"
592"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000593"default value used when mode is not specified is Z_FINISH.\n"
594"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000595"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000596
Guido van Rossumfb221561997-04-29 15:38:09 +0000597static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000598PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000599{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000600 int err, length = DEFAULTALLOC;
601 PyObject *RetVal;
602 int flushmode = Z_FINISH;
603 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000604
Jeremy Hylton9714f992001-10-16 21:19:45 +0000605 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000607
Jeremy Hylton9714f992001-10-16 21:19:45 +0000608 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
609 doing any work at all; just return an empty string. */
610 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000611 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000612 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000613
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000614 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000615 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000616
617 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000618
Jeremy Hylton9714f992001-10-16 21:19:45 +0000619 start_total_out = self->zst.total_out;
620 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000621 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000622 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000623
624 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000625 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000626 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000627
Jeremy Hylton9714f992001-10-16 21:19:45 +0000628 /* while Z_OK and the output buffer is full, there might be more output,
629 so extend the output buffer and try again */
630 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 if (_PyString_Resize(&RetVal, length << 1) < 0)
632 goto error;
633 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
634 + length;
635 self->zst.avail_out = length;
636 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000637
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000638 Py_BEGIN_ALLOW_THREADS
639 err = deflate(&(self->zst), flushmode);
640 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000641 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000642
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000644 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000645 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000646 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000647 err = deflateEnd(&(self->zst));
648 if (err != Z_OK) {
649 zlib_error(self->zst, err, "from deflateEnd()");
650 Py_DECREF(RetVal);
651 RetVal = NULL;
652 goto error;
653 }
654 else
655 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000656
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 /* We will only get Z_BUF_ERROR if the output buffer was full
658 but there wasn't more output when we tried again, so it is
659 not an error condition.
660 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000661 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000662 zlib_error(self->zst, err, "while flushing");
663 Py_DECREF(RetVal);
664 RetVal = NULL;
665 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000666 }
Tim Peters977e5402001-10-17 03:57:20 +0000667
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000668 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000669
Tim Peters977e5402001-10-17 03:57:20 +0000670 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000671 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000672
673 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000674}
675
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000676#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000677PyDoc_STRVAR(comp_copy__doc__,
678"copy() -- Return a copy of the compression object.");
679
680static PyObject *
681PyZlib_copy(compobject *self)
682{
683 compobject *retval = NULL;
684 int err;
685
686 retval = newcompobject(&Comptype);
687 if (!retval) return NULL;
688
689 /* Copy the zstream state
690 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
691 */
692 ENTER_ZLIB
693 err = deflateCopy(&retval->zst, &self->zst);
694 switch(err) {
695 case(Z_OK):
696 break;
697 case(Z_STREAM_ERROR):
698 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
699 goto error;
700 case(Z_MEM_ERROR):
701 PyErr_SetString(PyExc_MemoryError,
702 "Can't allocate memory for compression object");
703 goto error;
704 default:
705 zlib_error(self->zst, err, "while copying compression object");
706 goto error;
707 }
708
Tim Peters402cc242006-05-17 01:30:11 +0000709 Py_INCREF(self->unused_data);
710 Py_INCREF(self->unconsumed_tail);
711 Py_XDECREF(retval->unused_data);
712 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000713 retval->unused_data = self->unused_data;
714 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000715
716 /* Mark it as being initialized */
717 retval->is_initialised = 1;
718
719 LEAVE_ZLIB
720 return (PyObject *)retval;
721
722error:
723 LEAVE_ZLIB
Tim Peters402cc242006-05-17 01:30:11 +0000724 Py_XDECREF(retval);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000725 return NULL;
726}
727
728PyDoc_STRVAR(decomp_copy__doc__,
729"copy() -- Return a copy of the decompression object.");
730
731static PyObject *
732PyZlib_uncopy(compobject *self)
733{
734 compobject *retval = NULL;
735 int err;
736
737 retval = newcompobject(&Decomptype);
738 if (!retval) return NULL;
739
740 /* Copy the zstream state
741 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
742 */
743 ENTER_ZLIB
744 err = inflateCopy(&retval->zst, &self->zst);
745 switch(err) {
746 case(Z_OK):
747 break;
748 case(Z_STREAM_ERROR):
749 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
750 goto error;
751 case(Z_MEM_ERROR):
752 PyErr_SetString(PyExc_MemoryError,
753 "Can't allocate memory for decompression object");
754 goto error;
755 default:
756 zlib_error(self->zst, err, "while copying decompression object");
757 goto error;
758 }
759
Tim Peters402cc242006-05-17 01:30:11 +0000760 Py_INCREF(self->unused_data);
761 Py_INCREF(self->unconsumed_tail);
762 Py_XDECREF(retval->unused_data);
763 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000764 retval->unused_data = self->unused_data;
765 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000766
767 /* Mark it as being initialized */
768 retval->is_initialised = 1;
769
770 LEAVE_ZLIB
771 return (PyObject *)retval;
772
773error:
774 LEAVE_ZLIB
775 Py_XDECREF(retval);
776 return NULL;
777}
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000778#endif
Georg Brandl8d3342b2006-05-16 07:38:27 +0000779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000780PyDoc_STRVAR(decomp_flush__doc__,
Georg Brandl22a9dc82006-04-01 07:39:41 +0000781"flush( [length] ) -- Return a string containing any remaining\n"
782"decompressed data. length, if given, is the initial size of the\n"
783"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000784"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000785"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000786
Guido van Rossumfb221561997-04-29 15:38:09 +0000787static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000788PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000789{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000790 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000791 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000792 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000793
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000794 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000795 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000796 if (length <= 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000797 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
798 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000799 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000800 if (!(retval = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000801 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000802
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000803
Jeremy Hylton9714f992001-10-16 21:19:45 +0000804 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000805
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000806 start_total_out = self->zst.total_out;
807 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000808 self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000809
810 Py_BEGIN_ALLOW_THREADS
811 err = inflate(&(self->zst), Z_FINISH);
812 Py_END_ALLOW_THREADS
813
814 /* while Z_OK and the output buffer is full, there might be more output,
815 so extend the output buffer and try again */
816 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000817 if (_PyString_Resize(&retval, length << 1) < 0)
818 goto error;
819 self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
820 self->zst.avail_out = length;
821 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000822
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000823 Py_BEGIN_ALLOW_THREADS
824 err = inflate(&(self->zst), Z_FINISH);
825 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000826 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000827
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000828 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
829 various data structures. Note we should only get Z_STREAM_END when
830 flushmode is Z_FINISH */
831 if (err == Z_STREAM_END) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000832 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000833 self->is_initialised = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000834 if (err != Z_OK) {
835 zlib_error(self->zst, err, "from inflateEnd()");
836 Py_DECREF(retval);
837 retval = NULL;
838 goto error;
839 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000840 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000841 _PyString_Resize(&retval, self->zst.total_out - start_total_out);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000842
843error:
844
Jeremy Hylton9714f992001-10-16 21:19:45 +0000845 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000846
Jeremy Hylton9714f992001-10-16 21:19:45 +0000847 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000848}
849
850static PyMethodDef comp_methods[] =
851{
Tim Peters977e5402001-10-17 03:57:20 +0000852 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000853 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000854 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000855 comp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000856#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000857 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
858 comp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000859#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000860 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000861};
862
863static PyMethodDef Decomp_methods[] =
864{
Tim Peters977e5402001-10-17 03:57:20 +0000865 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000866 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000867 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000868 decomp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000869#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000870 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
871 decomp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000872#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000873 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000874};
875
876static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000877Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000878{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000879 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
880 internal data. */
881
882 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000883}
884
885static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000886Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000887{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000888 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000889
Jeremy Hylton9714f992001-10-16 21:19:45 +0000890 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000891
Tim Peters977e5402001-10-17 03:57:20 +0000892 if (strcmp(name, "unused_data") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000893 Py_INCREF(self->unused_data);
894 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000895 } else if (strcmp(name, "unconsumed_tail") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000896 Py_INCREF(self->unconsumed_tail);
897 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000898 } else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000899 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000900
Jeremy Hylton9714f992001-10-16 21:19:45 +0000901 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000902
Jeremy Hylton9714f992001-10-16 21:19:45 +0000903 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000904}
905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000907"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
908"\n"
909"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000910"a signed integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000911
Guido van Rossumfb221561997-04-29 15:38:09 +0000912static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000913PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000914{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000915 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000916 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000917 int len, signed_val;
Tim Peters977e5402001-10-17 03:57:20 +0000918
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000919 if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000920 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000921 /* In Python 2.x we return a signed integer regardless of native platform
922 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
923 * extended into a 64-bit long inside the integer object). 3.0 does the
924 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
925 signed_val = adler32(adler32val, buf, len);
926 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000927}
Tim Peters977e5402001-10-17 03:57:20 +0000928
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000929PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000930"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
931"\n"
932"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000933"a signed integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000934
935static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000936PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000937{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000938 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000939 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000940 int len, signed_val;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000941
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000942 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000943 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000944 /* In Python 2.x we return a signed integer regardless of native platform
945 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
946 * extended into a 64-bit long inside the integer object). 3.0 does the
947 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
948 signed_val = crc32(crc32val, buf, len);
949 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000950}
Tim Peters977e5402001-10-17 03:57:20 +0000951
Guido van Rossumfb221561997-04-29 15:38:09 +0000952
953static PyMethodDef zlib_methods[] =
954{
Tim Peters977e5402001-10-17 03:57:20 +0000955 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000956 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000957 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000958 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000959 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000960 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000961 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
962 crc32__doc__},
963 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000964 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000965 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000966 decompressobj__doc__},
967 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000968};
969
Tim Peters0c322792002-07-17 16:49:03 +0000970static PyTypeObject Comptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000971 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000972 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000973 sizeof(compobject),
974 0,
975 (destructor)Comp_dealloc, /*tp_dealloc*/
976 0, /*tp_print*/
977 (getattrfunc)Comp_getattr, /*tp_getattr*/
978 0, /*tp_setattr*/
979 0, /*tp_compare*/
980 0, /*tp_repr*/
981 0, /*tp_as_number*/
982 0, /*tp_as_sequence*/
983 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000984};
985
Tim Peters0c322792002-07-17 16:49:03 +0000986static PyTypeObject Decomptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000987 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000988 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000989 sizeof(compobject),
990 0,
991 (destructor)Decomp_dealloc, /*tp_dealloc*/
992 0, /*tp_print*/
993 (getattrfunc)Decomp_getattr, /*tp_getattr*/
994 0, /*tp_setattr*/
995 0, /*tp_compare*/
996 0, /*tp_repr*/
997 0, /*tp_as_number*/
998 0, /*tp_as_sequence*/
999 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001000};
1001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001002PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001003"The functions in this module allow compression and decompression using the\n"
1004"zlib library, which is based on GNU zip.\n"
1005"\n"
1006"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1007"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001008"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001009"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001010"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001011"decompressobj([wbits]) -- Return a decompressor object.\n"
1012"\n"
1013"'wbits' is window buffer size.\n"
1014"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001015"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001016
Mark Hammond62b1ab12002-07-23 06:31:15 +00001017PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001018PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001019{
Fred Drake4baedc12002-04-01 14:53:37 +00001020 PyObject *m, *ver;
Christian Heimese93237d2007-12-19 02:37:44 +00001021 Py_TYPE(&Comptype) = &PyType_Type;
1022 Py_TYPE(&Decomptype) = &PyType_Type;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001023 m = Py_InitModule4("zlib", zlib_methods,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 zlib_module_documentation,
1025 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001026 if (m == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001027 return;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001028
Fred Drake4baedc12002-04-01 14:53:37 +00001029 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1030 if (ZlibError != NULL) {
1031 Py_INCREF(ZlibError);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001032 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001033 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001034 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1035 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1036 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1037 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1038 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1039 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1040 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1041 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1042 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001043
Jeremy Hylton9714f992001-10-16 21:19:45 +00001044 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1045 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1046 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1047 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001048
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001049 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001050 if (ver != NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001051 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001052
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001053 PyModule_AddStringConstant(m, "__version__", "1.0");
1054
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001055#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001056 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001057#endif /* WITH_THREAD */
Guido van Rossumfb221561997-04-29 15:38:09 +00001058}