blob: 183f01e91a15377272f8622ecca500feec5de9a4 [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
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000538 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000539 of specified size. Return the unconsumed tail in an attribute.*/
540 if(max_length) {
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);
544 if(!self->unconsumed_tail) {
545 Py_DECREF(RetVal);
546 RetVal = NULL;
547 goto error;
548 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000549 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000550
Tim Peters977e5402001-10-17 03:57:20 +0000551 /* The end of the compressed data has been reached, so set the
552 unused_data attribute to a string containing the remainder of the
553 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000554 inflateEnd, but the old behaviour of only calling it on flush() is
555 preserved.
556 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000557 if (err == Z_STREAM_END) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000558 Py_XDECREF(self->unused_data); /* Free original empty string */
559 self->unused_data = PyString_FromStringAndSize(
560 (char *)self->zst.next_in, self->zst.avail_in);
561 if (self->unused_data == NULL) {
562 Py_DECREF(RetVal);
563 goto error;
564 }
565 /* We will only get Z_BUF_ERROR if the output buffer was full
566 but there wasn't more output when we tried again, so it is
567 not an error condition.
568 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000569 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000570 zlib_error(self->zst, err, "while decompressing");
571 Py_DECREF(RetVal);
572 RetVal = NULL;
573 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000574 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000575
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000576 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000577
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000578 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000579 LEAVE_ZLIB
580
581 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000582}
583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000584PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000585"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000586"\n"
587"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000588"default value used when mode is not specified is Z_FINISH.\n"
589"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000590"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000591
Guido van Rossumfb221561997-04-29 15:38:09 +0000592static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000593PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000594{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595 int err, length = DEFAULTALLOC;
596 PyObject *RetVal;
597 int flushmode = Z_FINISH;
598 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000599
Jeremy Hylton9714f992001-10-16 21:19:45 +0000600 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000601 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000602
Jeremy Hylton9714f992001-10-16 21:19:45 +0000603 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
604 doing any work at all; just return an empty string. */
605 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000607 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000608
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000609 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000610 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000611
612 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000613
Jeremy Hylton9714f992001-10-16 21:19:45 +0000614 start_total_out = self->zst.total_out;
615 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000616 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000617 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000618
619 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000620 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000621 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000622
Jeremy Hylton9714f992001-10-16 21:19:45 +0000623 /* while Z_OK and the output buffer is full, there might be more output,
624 so extend the output buffer and try again */
625 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000626 if (_PyString_Resize(&RetVal, length << 1) < 0)
627 goto error;
628 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
629 + length;
630 self->zst.avail_out = length;
631 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000632
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633 Py_BEGIN_ALLOW_THREADS
634 err = deflate(&(self->zst), flushmode);
635 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000636 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000637
Jeremy Hylton9714f992001-10-16 21:19:45 +0000638 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000639 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000640 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000641 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000642 err = deflateEnd(&(self->zst));
643 if (err != Z_OK) {
644 zlib_error(self->zst, err, "from deflateEnd()");
645 Py_DECREF(RetVal);
646 RetVal = NULL;
647 goto error;
648 }
649 else
650 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000651
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 /* We will only get Z_BUF_ERROR if the output buffer was full
653 but there wasn't more output when we tried again, so it is
654 not an error condition.
655 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000656 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 zlib_error(self->zst, err, "while flushing");
658 Py_DECREF(RetVal);
659 RetVal = NULL;
660 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000661 }
Tim Peters977e5402001-10-17 03:57:20 +0000662
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000663 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000664
Tim Peters977e5402001-10-17 03:57:20 +0000665 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000666 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000667
668 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000669}
670
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000671#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000672PyDoc_STRVAR(comp_copy__doc__,
673"copy() -- Return a copy of the compression object.");
674
675static PyObject *
676PyZlib_copy(compobject *self)
677{
678 compobject *retval = NULL;
679 int err;
680
681 retval = newcompobject(&Comptype);
682 if (!retval) return NULL;
683
684 /* Copy the zstream state
685 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
686 */
687 ENTER_ZLIB
688 err = deflateCopy(&retval->zst, &self->zst);
689 switch(err) {
690 case(Z_OK):
691 break;
692 case(Z_STREAM_ERROR):
693 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
694 goto error;
695 case(Z_MEM_ERROR):
696 PyErr_SetString(PyExc_MemoryError,
697 "Can't allocate memory for compression object");
698 goto error;
699 default:
700 zlib_error(self->zst, err, "while copying compression object");
701 goto error;
702 }
703
Tim Peters402cc242006-05-17 01:30:11 +0000704 Py_INCREF(self->unused_data);
705 Py_INCREF(self->unconsumed_tail);
706 Py_XDECREF(retval->unused_data);
707 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000708 retval->unused_data = self->unused_data;
709 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000710
711 /* Mark it as being initialized */
712 retval->is_initialised = 1;
713
714 LEAVE_ZLIB
715 return (PyObject *)retval;
716
717error:
718 LEAVE_ZLIB
Tim Peters402cc242006-05-17 01:30:11 +0000719 Py_XDECREF(retval);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000720 return NULL;
721}
722
723PyDoc_STRVAR(decomp_copy__doc__,
724"copy() -- Return a copy of the decompression object.");
725
726static PyObject *
727PyZlib_uncopy(compobject *self)
728{
729 compobject *retval = NULL;
730 int err;
731
732 retval = newcompobject(&Decomptype);
733 if (!retval) return NULL;
734
735 /* Copy the zstream state
736 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
737 */
738 ENTER_ZLIB
739 err = inflateCopy(&retval->zst, &self->zst);
740 switch(err) {
741 case(Z_OK):
742 break;
743 case(Z_STREAM_ERROR):
744 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
745 goto error;
746 case(Z_MEM_ERROR):
747 PyErr_SetString(PyExc_MemoryError,
748 "Can't allocate memory for decompression object");
749 goto error;
750 default:
751 zlib_error(self->zst, err, "while copying decompression object");
752 goto error;
753 }
754
Tim Peters402cc242006-05-17 01:30:11 +0000755 Py_INCREF(self->unused_data);
756 Py_INCREF(self->unconsumed_tail);
757 Py_XDECREF(retval->unused_data);
758 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000759 retval->unused_data = self->unused_data;
760 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000761
762 /* Mark it as being initialized */
763 retval->is_initialised = 1;
764
765 LEAVE_ZLIB
766 return (PyObject *)retval;
767
768error:
769 LEAVE_ZLIB
770 Py_XDECREF(retval);
771 return NULL;
772}
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000773#endif
Georg Brandl8d3342b2006-05-16 07:38:27 +0000774
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000775PyDoc_STRVAR(decomp_flush__doc__,
Georg Brandl22a9dc82006-04-01 07:39:41 +0000776"flush( [length] ) -- Return a string containing any remaining\n"
777"decompressed data. length, if given, is the initial size of the\n"
778"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000779"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000780"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000781
Guido van Rossumfb221561997-04-29 15:38:09 +0000782static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000783PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000784{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000785 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000786 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000787 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000788
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000789 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000790 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000791 if (length <= 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000792 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
793 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000794 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000795 if (!(retval = PyString_FromStringAndSize(NULL, length)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000797
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000798
Jeremy Hylton9714f992001-10-16 21:19:45 +0000799 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000800
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000801 start_total_out = self->zst.total_out;
802 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000803 self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000804
805 Py_BEGIN_ALLOW_THREADS
806 err = inflate(&(self->zst), Z_FINISH);
807 Py_END_ALLOW_THREADS
808
809 /* while Z_OK and the output buffer is full, there might be more output,
810 so extend the output buffer and try again */
811 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000812 if (_PyString_Resize(&retval, length << 1) < 0)
813 goto error;
814 self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
815 self->zst.avail_out = length;
816 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000817
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 Py_BEGIN_ALLOW_THREADS
819 err = inflate(&(self->zst), Z_FINISH);
820 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000821 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000822
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000823 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
824 various data structures. Note we should only get Z_STREAM_END when
825 flushmode is Z_FINISH */
826 if (err == Z_STREAM_END) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000827 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000828 self->is_initialised = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000829 if (err != Z_OK) {
830 zlib_error(self->zst, err, "from inflateEnd()");
831 Py_DECREF(retval);
832 retval = NULL;
833 goto error;
834 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000835 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000836 _PyString_Resize(&retval, self->zst.total_out - start_total_out);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000837
838error:
839
Jeremy Hylton9714f992001-10-16 21:19:45 +0000840 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000841
Jeremy Hylton9714f992001-10-16 21:19:45 +0000842 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000843}
844
845static PyMethodDef comp_methods[] =
846{
Tim Peters977e5402001-10-17 03:57:20 +0000847 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000848 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000849 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000850 comp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000851#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000852 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
853 comp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000854#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000855 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000856};
857
858static PyMethodDef Decomp_methods[] =
859{
Tim Peters977e5402001-10-17 03:57:20 +0000860 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000861 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000862 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000863 decomp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000864#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000865 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
866 decomp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000867#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000868 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000869};
870
871static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000872Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000873{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000874 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
875 internal data. */
876
877 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000878}
879
880static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000881Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000882{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000883 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000884
Jeremy Hylton9714f992001-10-16 21:19:45 +0000885 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000886
Tim Peters977e5402001-10-17 03:57:20 +0000887 if (strcmp(name, "unused_data") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000888 Py_INCREF(self->unused_data);
889 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000890 } else if (strcmp(name, "unconsumed_tail") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000891 Py_INCREF(self->unconsumed_tail);
892 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000893 } else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000895
Jeremy Hylton9714f992001-10-16 21:19:45 +0000896 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000897
Jeremy Hylton9714f992001-10-16 21:19:45 +0000898 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000899}
900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000901PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000902"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
903"\n"
904"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000905"a signed integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000906
Guido van Rossumfb221561997-04-29 15:38:09 +0000907static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000908PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000909{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000910 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000911 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000912 int len, signed_val;
Tim Peters977e5402001-10-17 03:57:20 +0000913
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000914 if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000915 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000916 /* In Python 2.x we return a signed integer regardless of native platform
917 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
918 * extended into a 64-bit long inside the integer object). 3.0 does the
919 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
920 signed_val = adler32(adler32val, buf, len);
921 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000922}
Tim Peters977e5402001-10-17 03:57:20 +0000923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000924PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000925"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
926"\n"
927"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000928"a signed integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000929
930static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000931PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000932{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000933 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000934 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000935 int len, signed_val;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000936
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000937 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000938 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000939 /* In Python 2.x we return a signed integer regardless of native platform
940 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
941 * extended into a 64-bit long inside the integer object). 3.0 does the
942 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
943 signed_val = crc32(crc32val, buf, len);
944 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000945}
Tim Peters977e5402001-10-17 03:57:20 +0000946
Guido van Rossumfb221561997-04-29 15:38:09 +0000947
948static PyMethodDef zlib_methods[] =
949{
Tim Peters977e5402001-10-17 03:57:20 +0000950 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000951 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000952 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000953 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000954 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000955 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000956 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
957 crc32__doc__},
958 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000959 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000960 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000961 decompressobj__doc__},
962 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000963};
964
Tim Peters0c322792002-07-17 16:49:03 +0000965static PyTypeObject Comptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000966 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000967 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000968 sizeof(compobject),
969 0,
970 (destructor)Comp_dealloc, /*tp_dealloc*/
971 0, /*tp_print*/
972 (getattrfunc)Comp_getattr, /*tp_getattr*/
973 0, /*tp_setattr*/
974 0, /*tp_compare*/
975 0, /*tp_repr*/
976 0, /*tp_as_number*/
977 0, /*tp_as_sequence*/
978 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000979};
980
Tim Peters0c322792002-07-17 16:49:03 +0000981static PyTypeObject Decomptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000982 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000983 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000984 sizeof(compobject),
985 0,
986 (destructor)Decomp_dealloc, /*tp_dealloc*/
987 0, /*tp_print*/
988 (getattrfunc)Decomp_getattr, /*tp_getattr*/
989 0, /*tp_setattr*/
990 0, /*tp_compare*/
991 0, /*tp_repr*/
992 0, /*tp_as_number*/
993 0, /*tp_as_sequence*/
994 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000995};
996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000997PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +0000998"The functions in this module allow compression and decompression using the\n"
999"zlib library, which is based on GNU zip.\n"
1000"\n"
1001"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1002"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001003"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001004"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001005"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001006"decompressobj([wbits]) -- Return a decompressor object.\n"
1007"\n"
1008"'wbits' is window buffer size.\n"
1009"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001010"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001011
Mark Hammond62b1ab12002-07-23 06:31:15 +00001012PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001013PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001014{
Fred Drake4baedc12002-04-01 14:53:37 +00001015 PyObject *m, *ver;
Christian Heimese93237d2007-12-19 02:37:44 +00001016 Py_TYPE(&Comptype) = &PyType_Type;
1017 Py_TYPE(&Decomptype) = &PyType_Type;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001018 m = Py_InitModule4("zlib", zlib_methods,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001019 zlib_module_documentation,
1020 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001021 if (m == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001022 return;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001023
Fred Drake4baedc12002-04-01 14:53:37 +00001024 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1025 if (ZlibError != NULL) {
1026 Py_INCREF(ZlibError);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001027 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001028 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001029 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1030 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1031 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1032 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1033 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1034 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1035 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1036 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1037 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001038
Jeremy Hylton9714f992001-10-16 21:19:45 +00001039 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1040 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1041 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1042 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001043
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001044 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001045 if (ver != NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001046 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001047
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001048 PyModule_AddStringConstant(m, "__version__", "1.0");
1049
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001050#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001051 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001052#endif /* WITH_THREAD */
Guido van Rossumfb221561997-04-29 15:38:09 +00001053}