blob: 4f78dbcb15dabaf2952bee247abfc9d3127558f1 [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 \
Tim Peters6605c642001-10-17 03:43:54 +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 \
Tim Peters6605c642001-10-17 03:43:54 +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{
75 if (zst.msg == Z_NULL)
76 PyErr_Format(ZlibError, "Error %d %s", err, msg);
77 else
78 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
79}
80
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000082"compressobj([level]) -- Return a compressor object.\n"
83"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000086PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000087"decompressobj([wbits]) -- Return a decompressor object.\n"
88"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000089"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000090
Guido van Rossumfb221561997-04-29 15:38:09 +000091static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000092newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000093{
Tim Peters977e5402001-10-17 03:57:20 +000094 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000095 self = PyObject_New(compobject, type);
96 if (self == NULL)
97 return NULL;
98 self->is_initialised = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +000099 self->unused_data = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000100 if (self->unused_data == NULL) {
101 Py_DECREF(self);
102 return NULL;
103 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000104 self->unconsumed_tail = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 if (self->unconsumed_tail == NULL) {
106 Py_DECREF(self);
107 return NULL;
108 }
109 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000110}
111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000113"compress(string[, level]) -- Returned compressed string.\n"
114"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000116
Guido van Rossumfb221561997-04-29 15:38:09 +0000117static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000118PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000119{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000120 PyObject *ReturnVal = NULL;
121 Byte *input, *output;
122 int length, level=Z_DEFAULT_COMPRESSION, err;
123 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000124
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 /* require Python string object, optional 'level' arg */
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000126 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000127 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000128
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000130
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000131 output = (Byte*)malloc(zst.avail_out);
132 if (output == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000133 PyErr_SetString(PyExc_MemoryError,
134 "Can't allocate memory to compress data");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000135 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000136 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000137
Jeremy Hylton9714f992001-10-16 21:19:45 +0000138 /* Past the point of no return. From here on out, we need to make sure
139 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000140
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000141 zst.zalloc = (alloc_func)NULL;
142 zst.zfree = (free_func)Z_NULL;
143 zst.next_out = (Byte *)output;
144 zst.next_in = (Byte *)input;
145 zst.avail_in = length;
146 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000147
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000148 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000149 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000150 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000151 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000152 PyErr_SetString(PyExc_MemoryError,
153 "Out of memory while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000154 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000155 case(Z_STREAM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000156 PyErr_SetString(ZlibError,
157 "Bad compression level");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000158 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000159 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000160 deflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000161 zlib_error(zst, err, "while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000162 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000163 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000164
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000165 Py_BEGIN_ALLOW_THREADS;
166 err = deflate(&zst, Z_FINISH);
167 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000168
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000169 if (err != Z_STREAM_END) {
170 zlib_error(zst, err, "while compressing data");
171 deflateEnd(&zst);
172 goto error;
173 }
Tim Peters977e5402001-10-17 03:57:20 +0000174
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000175 err=deflateEnd(&zst);
176 if (err == Z_OK)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000177 ReturnVal = PyString_FromStringAndSize((char *)output,
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000178 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000179 else
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000180 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000181
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000183 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000184
Jeremy Hylton9714f992001-10-16 21:19:45 +0000185 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000186}
187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000189"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
190"\n"
191"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000192"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000193
Guido van Rossumfb221561997-04-29 15:38:09 +0000194static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000195PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000196{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000197 PyObject *result_str;
198 Byte *input;
199 int length, err;
Christian Heimes901071b2007-11-21 00:46:21 +0000200 int wsize=DEF_WBITS;
201 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000202 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000203
Christian Heimes901071b2007-11-21 00:46:21 +0000204 if (!PyArg_ParseTuple(args, "s#|in:decompress",
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000205 &input, &length, &wsize, &r_strlen))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000206 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000207
Jeremy Hylton9714f992001-10-16 21:19:45 +0000208 if (r_strlen <= 0)
209 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000210
Jeremy Hylton9714f992001-10-16 21:19:45 +0000211 zst.avail_in = length;
212 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000213
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000214 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000215 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000216
Jeremy Hylton9714f992001-10-16 21:19:45 +0000217 zst.zalloc = (alloc_func)NULL;
218 zst.zfree = (free_func)Z_NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000219 zst.next_out = (Byte *)PyString_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000220 zst.next_in = (Byte *)input;
221 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000222
Jeremy Hylton9714f992001-10-16 21:19:45 +0000223 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000224 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000225 break;
Tim Peters977e5402001-10-17 03:57:20 +0000226 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000227 PyErr_SetString(PyExc_MemoryError,
228 "Out of memory while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000229 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000230 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000231 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000232 zlib_error(zst, err, "while preparing to decompress data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000233 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000234 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000235
Jeremy Hylton9714f992001-10-16 21:19:45 +0000236 do {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 Py_BEGIN_ALLOW_THREADS
238 err=inflate(&zst, Z_FINISH);
239 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000240
Jeremy Hylton9714f992001-10-16 21:19:45 +0000241 switch(err) {
242 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000243 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000244 case(Z_BUF_ERROR):
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000245 /*
246 * If there is at least 1 byte of room according to zst.avail_out
247 * and we get this error, assume that it means zlib cannot
248 * process the inflate call() due to an error in the data.
249 */
Jeremy Hylton0965e082001-10-16 21:56:09 +0000250 if (zst.avail_out > 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000251 PyErr_Format(ZlibError, "Error %i while decompressing data",
252 err);
253 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000254 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000255 }
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000256 /* fall through */
257 case(Z_OK):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000258 /* need more memory */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000259 if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000260 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000261 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000262 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000263 zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000264 + r_strlen;
265 zst.avail_out = r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000266 r_strlen = r_strlen << 1;
267 break;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000268 default:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000269 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000270 zlib_error(zst, err, "while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000271 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000272 }
273 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000274
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000275 err = inflateEnd(&zst);
276 if (err != Z_OK) {
277 zlib_error(zst, err, "while finishing data decompression");
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000278 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000279 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000280
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000281 _PyString_Resize(&result_str, zst.total_out);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000282 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000283
284 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000285 Py_XDECREF(result_str);
286 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000287}
288
289static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000290PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000291{
Jeremy Hylton499000002001-10-16 21:59:35 +0000292 compobject *self;
293 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
294 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000295
Jeremy Hylton499000002001-10-16 21:59:35 +0000296 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
297 &memLevel, &strategy))
298 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000299
Jeremy Hylton499000002001-10-16 21:59:35 +0000300 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000301 if (self==NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000302 return(NULL);
303 self->zst.zalloc = (alloc_func)NULL;
304 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000305 self->zst.next_in = NULL;
306 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000307 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
308 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000309 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000310 self->is_initialised = 1;
311 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000312 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000313 Py_DECREF(self);
314 PyErr_SetString(PyExc_MemoryError,
315 "Can't allocate memory for compression object");
316 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000317 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000318 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000319 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000320 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000321 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000322 zlib_error(self->zst, err, "while creating compression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000323 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000324 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000325 }
326}
327
328static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000329PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000330{
Jeremy Hylton499000002001-10-16 21:59:35 +0000331 int wbits=DEF_WBITS, err;
332 compobject *self;
333 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
334 return NULL;
335
336 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000337 if (self == NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000338 return(NULL);
339 self->zst.zalloc = (alloc_func)NULL;
340 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000341 self->zst.next_in = NULL;
342 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000343 err = inflateInit2(&self->zst, wbits);
344 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000345 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000346 self->is_initialised = 1;
347 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000348 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000349 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000350 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000351 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000352 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000353 Py_DECREF(self);
354 PyErr_SetString(PyExc_MemoryError,
355 "Can't allocate memory for decompression object");
356 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000357 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000358 zlib_error(self->zst, err, "while creating decompression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000359 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000360 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000361 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000362}
363
364static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000365Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000366{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000367 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000368 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000369 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000370 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000371 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000372}
373
374static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000375Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000376{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000377 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000378 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000379 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000380 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000381 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000382}
383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000385"compress(data) -- Return a string containing data compressed.\n"
386"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000387"After calling this function, some of the input data may still\n"
388"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000389"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000390
391
Guido van Rossumfb221561997-04-29 15:38:09 +0000392static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000393PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000394{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000395 int err, inplen, length = DEFAULTALLOC;
396 PyObject *RetVal;
397 Byte *input;
398 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000399
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000400 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000401 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000402
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000403 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000404 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000405
406 ENTER_ZLIB
407
Jeremy Hylton9714f992001-10-16 21:19:45 +0000408 start_total_out = self->zst.total_out;
409 self->zst.avail_in = inplen;
410 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000411 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000412 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000413
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000414 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000415 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000416 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000417
Jeremy Hylton9714f992001-10-16 21:19:45 +0000418 /* while Z_OK and the output buffer is full, there might be more output,
419 so extend the output buffer and try again */
420 while (err == Z_OK && self->zst.avail_out == 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000421 if (_PyString_Resize(&RetVal, length << 1) < 0)
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000422 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000423 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000424 + length;
425 self->zst.avail_out = length;
426 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000427
Jeremy Hylton9714f992001-10-16 21:19:45 +0000428 Py_BEGIN_ALLOW_THREADS
429 err = deflate(&(self->zst), Z_NO_FLUSH);
430 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000431 }
Tim Peters977e5402001-10-17 03:57:20 +0000432 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000433 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000434 condition.
435 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000436
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000437 if (err != Z_OK && err != Z_BUF_ERROR) {
438 zlib_error(self->zst, err, "while compressing");
439 Py_DECREF(RetVal);
440 RetVal = NULL;
441 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000442 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000443 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000444
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000445 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000446 LEAVE_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000447 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000448}
449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000451"decompress(data, max_length) -- Return a string containing the decompressed\n"
452"version of the data.\n"
453"\n"
454"After calling this function, some of the input data may still be stored in\n"
455"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000456"Call the flush() method to clear these buffers.\n"
457"If the max_length parameter is specified then the return value will be\n"
458"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000459"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000460
Guido van Rossumfb221561997-04-29 15:38:09 +0000461static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000462PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000463{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000464 int err, inplen, old_length, length = DEFAULTALLOC;
465 int max_length = 0;
466 PyObject *RetVal;
467 Byte *input;
468 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000469
Tim Peters977e5402001-10-17 03:57:20 +0000470 if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000471 &inplen, &max_length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000472 return NULL;
473 if (max_length < 0) {
474 PyErr_SetString(PyExc_ValueError,
475 "max_length must be greater than zero");
476 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000477 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000478
Jeremy Hylton9714f992001-10-16 21:19:45 +0000479 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000480 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000481 length = max_length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000482 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000483 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000484
485 ENTER_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000486
Jeremy Hylton9714f992001-10-16 21:19:45 +0000487 start_total_out = self->zst.total_out;
488 self->zst.avail_in = inplen;
489 self->zst.next_in = input;
490 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000491 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000492
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000493 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000494 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000495 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000496
Jeremy Hylton9714f992001-10-16 21:19:45 +0000497 /* While Z_OK and the output buffer is full, there might be more output.
498 So extend the output buffer and try again.
499 */
Tim Peters977e5402001-10-17 03:57:20 +0000500 while (err == Z_OK && self->zst.avail_out == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000501 /* If max_length set, don't continue decompressing if we've already
502 reached the limit.
503 */
504 if (max_length && length >= max_length)
505 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000506
Jeremy Hylton9714f992001-10-16 21:19:45 +0000507 /* otherwise, ... */
508 old_length = length;
509 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000510 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000511 length = max_length;
512
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000513 if (_PyString_Resize(&RetVal, length) < 0)
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000514 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000515 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000516 + old_length;
517 self->zst.avail_out = length - old_length;
518
519 Py_BEGIN_ALLOW_THREADS
520 err = inflate(&(self->zst), Z_SYNC_FLUSH);
521 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000522 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000523
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000524 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000525 of specified size. Return the unconsumed tail in an attribute.*/
526 if(max_length) {
527 Py_DECREF(self->unconsumed_tail);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000528 self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000529 self->zst.avail_in);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000530 if(!self->unconsumed_tail) {
531 Py_DECREF(RetVal);
532 RetVal = NULL;
533 goto error;
534 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000535 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000536
Tim Peters977e5402001-10-17 03:57:20 +0000537 /* The end of the compressed data has been reached, so set the
538 unused_data attribute to a string containing the remainder of the
539 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000540 inflateEnd, but the old behaviour of only calling it on flush() is
541 preserved.
542 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000543 if (err == Z_STREAM_END) {
544 Py_XDECREF(self->unused_data); /* Free original empty string */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000545 self->unused_data = PyString_FromStringAndSize(
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000546 (char *)self->zst.next_in, self->zst.avail_in);
547 if (self->unused_data == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000548 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000549 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000550 }
Tim Peters977e5402001-10-17 03:57:20 +0000551 /* We will only get Z_BUF_ERROR if the output buffer was full
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000552 but there wasn't more output when we tried again, so it is
553 not an error condition.
554 */
555 } else if (err != Z_OK && err != Z_BUF_ERROR) {
556 zlib_error(self->zst, err, "while decompressing");
557 Py_DECREF(RetVal);
558 RetVal = NULL;
559 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000560 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000561
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000562 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000563
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000564 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000565 LEAVE_ZLIB
566
567 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000568}
569
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000570PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000571"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000572"\n"
573"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000574"default value used when mode is not specified is Z_FINISH.\n"
575"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000576"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000577
Guido van Rossumfb221561997-04-29 15:38:09 +0000578static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000579PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000580{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000581 int err, length = DEFAULTALLOC;
582 PyObject *RetVal;
583 int flushmode = Z_FINISH;
584 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000585
Jeremy Hylton9714f992001-10-16 21:19:45 +0000586 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
587 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000588
Jeremy Hylton9714f992001-10-16 21:19:45 +0000589 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
590 doing any work at all; just return an empty string. */
591 if (flushmode == Z_NO_FLUSH) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000592 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000593 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000594
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000595 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000596 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000597
598 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000599
Jeremy Hylton9714f992001-10-16 21:19:45 +0000600 start_total_out = self->zst.total_out;
601 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000602 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000603 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000604
605 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000606 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000607 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000608
Jeremy Hylton9714f992001-10-16 21:19:45 +0000609 /* while Z_OK and the output buffer is full, there might be more output,
610 so extend the output buffer and try again */
611 while (err == Z_OK && self->zst.avail_out == 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000612 if (_PyString_Resize(&RetVal, length << 1) < 0)
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000613 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000614 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000615 + length;
616 self->zst.avail_out = length;
617 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000618
Jeremy Hylton9714f992001-10-16 21:19:45 +0000619 Py_BEGIN_ALLOW_THREADS
620 err = deflate(&(self->zst), flushmode);
621 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000622 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000623
Jeremy Hylton9714f992001-10-16 21:19:45 +0000624 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000625 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000626 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000627 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
628 err = deflateEnd(&(self->zst));
629 if (err != Z_OK) {
630 zlib_error(self->zst, err, "from deflateEnd()");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000631 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000632 RetVal = NULL;
633 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000634 }
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000635 else
636 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000637
638 /* We will only get Z_BUF_ERROR if the output buffer was full
639 but there wasn't more output when we tried again, so it is
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000640 not an error condition.
641 */
642 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
643 zlib_error(self->zst, err, "while flushing");
644 Py_DECREF(RetVal);
645 RetVal = NULL;
Jeremy Hyltonc72737e2002-04-19 14:37:07 +0000646 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647 }
Tim Peters977e5402001-10-17 03:57:20 +0000648
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000649 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000650
Tim Peters977e5402001-10-17 03:57:20 +0000651 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000652 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000653
654 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000655}
656
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000657#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000658PyDoc_STRVAR(comp_copy__doc__,
659"copy() -- Return a copy of the compression object.");
660
661static PyObject *
662PyZlib_copy(compobject *self)
663{
664 compobject *retval = NULL;
665 int err;
666
667 retval = newcompobject(&Comptype);
668 if (!retval) return NULL;
669
670 /* Copy the zstream state
671 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
672 */
673 ENTER_ZLIB
674 err = deflateCopy(&retval->zst, &self->zst);
675 switch(err) {
676 case(Z_OK):
677 break;
678 case(Z_STREAM_ERROR):
679 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
680 goto error;
681 case(Z_MEM_ERROR):
682 PyErr_SetString(PyExc_MemoryError,
683 "Can't allocate memory for compression object");
684 goto error;
685 default:
686 zlib_error(self->zst, err, "while copying compression object");
687 goto error;
688 }
689
Tim Peters402cc242006-05-17 01:30:11 +0000690 Py_INCREF(self->unused_data);
691 Py_INCREF(self->unconsumed_tail);
692 Py_XDECREF(retval->unused_data);
693 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000694 retval->unused_data = self->unused_data;
695 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000696
697 /* Mark it as being initialized */
698 retval->is_initialised = 1;
699
700 LEAVE_ZLIB
701 return (PyObject *)retval;
702
703error:
704 LEAVE_ZLIB
Tim Peters402cc242006-05-17 01:30:11 +0000705 Py_XDECREF(retval);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000706 return NULL;
707}
708
709PyDoc_STRVAR(decomp_copy__doc__,
710"copy() -- Return a copy of the decompression object.");
711
712static PyObject *
713PyZlib_uncopy(compobject *self)
714{
715 compobject *retval = NULL;
716 int err;
717
718 retval = newcompobject(&Decomptype);
719 if (!retval) return NULL;
720
721 /* Copy the zstream state
722 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
723 */
724 ENTER_ZLIB
725 err = inflateCopy(&retval->zst, &self->zst);
726 switch(err) {
727 case(Z_OK):
728 break;
729 case(Z_STREAM_ERROR):
730 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
731 goto error;
732 case(Z_MEM_ERROR):
733 PyErr_SetString(PyExc_MemoryError,
734 "Can't allocate memory for decompression object");
735 goto error;
736 default:
737 zlib_error(self->zst, err, "while copying decompression object");
738 goto error;
739 }
740
Tim Peters402cc242006-05-17 01:30:11 +0000741 Py_INCREF(self->unused_data);
742 Py_INCREF(self->unconsumed_tail);
743 Py_XDECREF(retval->unused_data);
744 Py_XDECREF(retval->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000745 retval->unused_data = self->unused_data;
746 retval->unconsumed_tail = self->unconsumed_tail;
Georg Brandl8d3342b2006-05-16 07:38:27 +0000747
748 /* Mark it as being initialized */
749 retval->is_initialised = 1;
750
751 LEAVE_ZLIB
752 return (PyObject *)retval;
753
754error:
755 LEAVE_ZLIB
756 Py_XDECREF(retval);
757 return NULL;
758}
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000759#endif
Georg Brandl8d3342b2006-05-16 07:38:27 +0000760
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000761PyDoc_STRVAR(decomp_flush__doc__,
Georg Brandl22a9dc82006-04-01 07:39:41 +0000762"flush( [length] ) -- Return a string containing any remaining\n"
763"decompressed data. length, if given, is the initial size of the\n"
764"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000765"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000766"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000767
Guido van Rossumfb221561997-04-29 15:38:09 +0000768static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000769PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000770{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000771 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000772 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000773 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000774
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000775 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000776 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000777 if (length <= 0) {
778 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
779 return NULL;
780 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000781 if (!(retval = PyString_FromStringAndSize(NULL, length)))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000782 return NULL;
783
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000784
Jeremy Hylton9714f992001-10-16 21:19:45 +0000785 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000786
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000787 start_total_out = self->zst.total_out;
788 self->zst.avail_out = length;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000789 self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000790
791 Py_BEGIN_ALLOW_THREADS
792 err = inflate(&(self->zst), Z_FINISH);
793 Py_END_ALLOW_THREADS
794
795 /* while Z_OK and the output buffer is full, there might be more output,
796 so extend the output buffer and try again */
797 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000798 if (_PyString_Resize(&retval, length << 1) < 0)
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000799 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000800 self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000801 self->zst.avail_out = length;
802 length = length << 1;
803
804 Py_BEGIN_ALLOW_THREADS
805 err = inflate(&(self->zst), Z_FINISH);
806 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000807 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000808
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000809 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
810 various data structures. Note we should only get Z_STREAM_END when
811 flushmode is Z_FINISH */
812 if (err == Z_STREAM_END) {
813 err = inflateEnd(&(self->zst));
814 self->is_initialised = 0;
815 if (err != Z_OK) {
816 zlib_error(self->zst, err, "from inflateEnd()");
817 Py_DECREF(retval);
818 retval = NULL;
819 goto error;
820 }
821 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000822 _PyString_Resize(&retval, self->zst.total_out - start_total_out);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000823
824error:
825
Jeremy Hylton9714f992001-10-16 21:19:45 +0000826 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000827
Jeremy Hylton9714f992001-10-16 21:19:45 +0000828 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000829}
830
831static PyMethodDef comp_methods[] =
832{
Tim Peters977e5402001-10-17 03:57:20 +0000833 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000834 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000835 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000836 comp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000837#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000838 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
839 comp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000840#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000841 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000842};
843
844static PyMethodDef Decomp_methods[] =
845{
Tim Peters977e5402001-10-17 03:57:20 +0000846 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000847 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000848 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000849 decomp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000850#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000851 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
852 decomp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000853#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000855};
856
857static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000858Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000859{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000860 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
861 internal data. */
862
863 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000864}
865
866static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000867Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000868{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000869 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000870
Jeremy Hylton9714f992001-10-16 21:19:45 +0000871 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000872
Tim Peters977e5402001-10-17 03:57:20 +0000873 if (strcmp(name, "unused_data") == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000874 Py_INCREF(self->unused_data);
875 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000876 } else if (strcmp(name, "unconsumed_tail") == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000877 Py_INCREF(self->unconsumed_tail);
878 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000879 } else
Jeremy Hylton9714f992001-10-16 21:19:45 +0000880 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000881
Jeremy Hylton9714f992001-10-16 21:19:45 +0000882 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000883
Jeremy Hylton9714f992001-10-16 21:19:45 +0000884 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000885}
886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000887PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000888"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
889"\n"
890"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000891"a signed integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000892
Guido van Rossumfb221561997-04-29 15:38:09 +0000893static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000894PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000895{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000896 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000897 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000898 int len, signed_val;
Tim Peters977e5402001-10-17 03:57:20 +0000899
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000900 if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000901 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000902 /* In Python 2.x we return a signed integer regardless of native platform
903 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
904 * extended into a 64-bit long inside the integer object). 3.0 does the
905 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
906 signed_val = adler32(adler32val, buf, len);
907 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000908}
Tim Peters977e5402001-10-17 03:57:20 +0000909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000910PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000911"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
912"\n"
913"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000914"a signed integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000915
916static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000917PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000918{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000919 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000920 Byte *buf;
Gregory P. Smith73f57b02008-03-23 20:31:23 +0000921 int len, signed_val;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000922
Gregory P. Smith1fa588e2008-03-25 07:31:28 +0000923 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000924 return NULL;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000925 /* In Python 2.x we return a signed integer regardless of native platform
926 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
927 * extended into a 64-bit long inside the integer object). 3.0 does the
928 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
929 signed_val = crc32(crc32val, buf, len);
930 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000931}
Tim Peters977e5402001-10-17 03:57:20 +0000932
Guido van Rossumfb221561997-04-29 15:38:09 +0000933
934static PyMethodDef zlib_methods[] =
935{
Tim Peters977e5402001-10-17 03:57:20 +0000936 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000937 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000938 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000939 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000940 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000941 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000942 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
943 crc32__doc__},
944 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000945 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000946 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000947 decompressobj__doc__},
948 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000949};
950
Tim Peters0c322792002-07-17 16:49:03 +0000951static PyTypeObject Comptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000952 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000953 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000954 sizeof(compobject),
955 0,
956 (destructor)Comp_dealloc, /*tp_dealloc*/
957 0, /*tp_print*/
958 (getattrfunc)Comp_getattr, /*tp_getattr*/
959 0, /*tp_setattr*/
960 0, /*tp_compare*/
961 0, /*tp_repr*/
962 0, /*tp_as_number*/
963 0, /*tp_as_sequence*/
964 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000965};
966
Tim Peters0c322792002-07-17 16:49:03 +0000967static PyTypeObject Decomptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000968 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000969 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000970 sizeof(compobject),
971 0,
972 (destructor)Decomp_dealloc, /*tp_dealloc*/
973 0, /*tp_print*/
974 (getattrfunc)Decomp_getattr, /*tp_getattr*/
975 0, /*tp_setattr*/
976 0, /*tp_compare*/
977 0, /*tp_repr*/
978 0, /*tp_as_number*/
979 0, /*tp_as_sequence*/
980 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000981};
982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000983PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +0000984"The functions in this module allow compression and decompression using the\n"
985"zlib library, which is based on GNU zip.\n"
986"\n"
987"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
988"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000989"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000990"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +0000991"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000992"decompressobj([wbits]) -- Return a decompressor object.\n"
993"\n"
994"'wbits' is window buffer size.\n"
995"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000996"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +0000997
Mark Hammond62b1ab12002-07-23 06:31:15 +0000998PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000999PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001000{
Fred Drake4baedc12002-04-01 14:53:37 +00001001 PyObject *m, *ver;
Christian Heimese93237d2007-12-19 02:37:44 +00001002 Py_TYPE(&Comptype) = &PyType_Type;
1003 Py_TYPE(&Decomptype) = &PyType_Type;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001004 m = Py_InitModule4("zlib", zlib_methods,
1005 zlib_module_documentation,
1006 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001007 if (m == NULL)
1008 return;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001009
Fred Drake4baedc12002-04-01 14:53:37 +00001010 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1011 if (ZlibError != NULL) {
1012 Py_INCREF(ZlibError);
1013 PyModule_AddObject(m, "error", ZlibError);
1014 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001015 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1016 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1017 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1018 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1019 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1020 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1021 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1022 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1023 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001024
Jeremy Hylton9714f992001-10-16 21:19:45 +00001025 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1026 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1027 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1028 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001029
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001030 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001031 if (ver != NULL)
1032 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001033
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001034 PyModule_AddStringConstant(m, "__version__", "1.0");
1035
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001036#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001037 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001038#endif /* WITH_THREAD */
Guido van Rossumfb221561997-04-29 15:38:09 +00001039}