blob: e63063fdcfdceb3e30df842034e9a1b2ff4b0fd0 [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
Jeremy Hylton938ace62002-07-17 16:30:39 +000057static PyTypeObject Comptype;
58static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000059
60static PyObject *ZlibError;
61
Tim Peters977e5402001-10-17 03:57:20 +000062typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000063{
Jeremy Hylton9714f992001-10-16 21:19:45 +000064 PyObject_HEAD
65 z_stream zst;
66 PyObject *unused_data;
67 PyObject *unconsumed_tail;
68 int is_initialised;
Guido van Rossumfb221561997-04-29 15:38:09 +000069} compobject;
70
Jeremy Hylton0965e082001-10-16 21:56:09 +000071static void
72zlib_error(z_stream zst, int err, char *msg)
73{
74 if (zst.msg == Z_NULL)
75 PyErr_Format(ZlibError, "Error %d %s", err, msg);
76 else
77 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
78}
79
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000081"compressobj([level]) -- Return a compressor object.\n"
82"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000086"decompressobj([wbits]) -- Return a decompressor object.\n"
87"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000088"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000089
Guido van Rossumfb221561997-04-29 15:38:09 +000090static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000091newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000092{
Tim Peters977e5402001-10-17 03:57:20 +000093 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000094 self = PyObject_New(compobject, type);
95 if (self == NULL)
96 return NULL;
97 self->is_initialised = 0;
Christian Heimes9c4756e2008-05-26 13:22:05 +000098 self->unused_data = PyByteArray_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 if (self->unused_data == NULL) {
100 Py_DECREF(self);
101 return NULL;
102 }
Christian Heimes9c4756e2008-05-26 13:22:05 +0000103 self->unconsumed_tail = PyByteArray_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 if (self->unconsumed_tail == NULL) {
105 Py_DECREF(self);
106 return NULL;
107 }
108 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000109}
110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000111PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000112"compress(string[, level]) -- Returned compressed string.\n"
113"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000114"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000115
Guido van Rossumfb221561997-04-29 15:38:09 +0000116static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000117PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000118{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 PyObject *ReturnVal = NULL;
120 Byte *input, *output;
121 int length, level=Z_DEFAULT_COMPRESSION, err;
122 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000123
Jeremy Hylton9714f992001-10-16 21:19:45 +0000124 /* require Python string object, optional 'level' arg */
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000125 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000126 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000127
Jeremy Hylton9714f992001-10-16 21:19:45 +0000128 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000129
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000130 output = (Byte*)malloc(zst.avail_out);
131 if (output == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000132 PyErr_SetString(PyExc_MemoryError,
133 "Can't allocate memory to compress data");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000134 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000135 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000136
Jeremy Hylton9714f992001-10-16 21:19:45 +0000137 /* Past the point of no return. From here on out, we need to make sure
138 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000139
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000140 zst.zalloc = (alloc_func)NULL;
141 zst.zfree = (free_func)Z_NULL;
142 zst.next_out = (Byte *)output;
143 zst.next_in = (Byte *)input;
144 zst.avail_in = length;
145 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000146
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000147 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000148 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000149 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000150 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000151 PyErr_SetString(PyExc_MemoryError,
152 "Out of memory while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000153 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000154 case(Z_STREAM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000155 PyErr_SetString(ZlibError,
156 "Bad compression level");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000157 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000158 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000159 deflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000160 zlib_error(zst, err, "while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000161 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000162 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000163
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000164 Py_BEGIN_ALLOW_THREADS;
165 err = deflate(&zst, Z_FINISH);
166 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000167
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000168 if (err != Z_STREAM_END) {
169 zlib_error(zst, err, "while compressing data");
170 deflateEnd(&zst);
171 goto error;
172 }
Tim Peters977e5402001-10-17 03:57:20 +0000173
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000174 err=deflateEnd(&zst);
175 if (err == Z_OK)
Christian Heimes9c4756e2008-05-26 13:22:05 +0000176 ReturnVal = PyByteArray_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000177 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000178 else
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000179 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000180
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000181 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000182 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000183
Jeremy Hylton9714f992001-10-16 21:19:45 +0000184 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000185}
186
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000187PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000188"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
189"\n"
190"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000191"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000192
Guido van Rossumfb221561997-04-29 15:38:09 +0000193static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000194PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000195{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000196 PyObject *result_str;
197 Byte *input;
198 int length, err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000199 int wsize=DEF_WBITS;
200 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000201 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000202
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000203 if (!PyArg_ParseTuple(args, "s#|in:decompress",
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000204 &input, &length, &wsize, &r_strlen))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000205 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000206
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207 if (r_strlen <= 0)
208 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000209
Jeremy Hylton9714f992001-10-16 21:19:45 +0000210 zst.avail_in = length;
211 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000212
Christian Heimes9c4756e2008-05-26 13:22:05 +0000213 if (!(result_str = PyByteArray_FromStringAndSize(NULL, r_strlen)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000214 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000215
Jeremy Hylton9714f992001-10-16 21:19:45 +0000216 zst.zalloc = (alloc_func)NULL;
217 zst.zfree = (free_func)Z_NULL;
Christian Heimes9c4756e2008-05-26 13:22:05 +0000218 zst.next_out = (Byte *)PyByteArray_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000219 zst.next_in = (Byte *)input;
220 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000221
Jeremy Hylton9714f992001-10-16 21:19:45 +0000222 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000223 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000224 break;
Tim Peters977e5402001-10-17 03:57:20 +0000225 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000226 PyErr_SetString(PyExc_MemoryError,
227 "Out of memory while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000228 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000229 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000230 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000231 zlib_error(zst, err, "while preparing to decompress data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000232 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000233 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000234
Jeremy Hylton9714f992001-10-16 21:19:45 +0000235 do {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000236 Py_BEGIN_ALLOW_THREADS
237 err=inflate(&zst, Z_FINISH);
238 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000239
Jeremy Hylton9714f992001-10-16 21:19:45 +0000240 switch(err) {
241 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000242 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000243 case(Z_BUF_ERROR):
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000244 /*
245 * If there is at least 1 byte of room according to zst.avail_out
246 * and we get this error, assume that it means zlib cannot
247 * process the inflate call() due to an error in the data.
248 */
Jeremy Hylton0965e082001-10-16 21:56:09 +0000249 if (zst.avail_out > 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000250 PyErr_Format(ZlibError, "Error %i while decompressing data",
251 err);
252 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000253 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000254 }
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000255 /* fall through */
256 case(Z_OK):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000257 /* need more memory */
Christian Heimes9c4756e2008-05-26 13:22:05 +0000258 if (PyByteArray_Resize(result_str, r_strlen << 1) < 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000259 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000260 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000261 }
Guido van Rossum776152b2007-05-22 22:44:07 +0000262 zst.next_out =
Christian Heimes9c4756e2008-05-26 13:22:05 +0000263 (unsigned char *)PyByteArray_AS_STRING(result_str) + r_strlen;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000264 zst.avail_out = r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000265 r_strlen = r_strlen << 1;
266 break;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000267 default:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000268 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000269 zlib_error(zst, err, "while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000270 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000271 }
272 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000273
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000274 err = inflateEnd(&zst);
275 if (err != Z_OK) {
276 zlib_error(zst, err, "while finishing data decompression");
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000277 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000278 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000279
Christian Heimes9c4756e2008-05-26 13:22:05 +0000280 if (PyByteArray_Resize(result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000281 goto error;
282
Jeremy Hylton9714f992001-10-16 21:19:45 +0000283 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000284
285 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000286 Py_XDECREF(result_str);
287 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000288}
289
290static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000291PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000292{
Jeremy Hylton499000002001-10-16 21:59:35 +0000293 compobject *self;
294 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
295 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000296
Jeremy Hylton499000002001-10-16 21:59:35 +0000297 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
298 &memLevel, &strategy))
299 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000300
Jeremy Hylton499000002001-10-16 21:59:35 +0000301 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000302 if (self==NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000303 return(NULL);
304 self->zst.zalloc = (alloc_func)NULL;
305 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000306 self->zst.next_in = NULL;
307 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000308 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
309 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000310 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000311 self->is_initialised = 1;
312 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000313 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000314 Py_DECREF(self);
315 PyErr_SetString(PyExc_MemoryError,
316 "Can't allocate memory for compression object");
317 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000318 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000319 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000320 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000321 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000322 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000323 zlib_error(self->zst, err, "while creating compression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000324 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000325 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000326 }
327}
328
329static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000330PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000331{
Jeremy Hylton499000002001-10-16 21:59:35 +0000332 int wbits=DEF_WBITS, err;
333 compobject *self;
334 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
335 return NULL;
336
337 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000338 if (self == NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000339 return(NULL);
340 self->zst.zalloc = (alloc_func)NULL;
341 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000342 self->zst.next_in = NULL;
343 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000344 err = inflateInit2(&self->zst, wbits);
345 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000346 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000347 self->is_initialised = 1;
348 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000349 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000350 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000351 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000352 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000353 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000354 Py_DECREF(self);
355 PyErr_SetString(PyExc_MemoryError,
356 "Can't allocate memory for decompression object");
357 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000358 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000359 zlib_error(self->zst, err, "while creating decompression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000360 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000361 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000362 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000363}
364
365static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000366Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000367{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000368 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000369 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000370 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000371 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000372 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000373}
374
375static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000376Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000377{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000378 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000379 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000380 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000381 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000382 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000383}
384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000385PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000386"compress(data) -- Return a string containing data compressed.\n"
387"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000388"After calling this function, some of the input data may still\n"
389"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000391
392
Guido van Rossumfb221561997-04-29 15:38:09 +0000393static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000394PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000395{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000396 int err, inplen, length = DEFAULTALLOC;
397 PyObject *RetVal;
398 Byte *input;
399 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000400
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000401 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000402 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000403
Christian Heimes9c4756e2008-05-26 13:22:05 +0000404 if (!(RetVal = PyByteArray_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000405 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000406
407 ENTER_ZLIB
408
Jeremy Hylton9714f992001-10-16 21:19:45 +0000409 start_total_out = self->zst.total_out;
410 self->zst.avail_in = inplen;
411 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000412 self->zst.avail_out = length;
Christian Heimes9c4756e2008-05-26 13:22:05 +0000413 self->zst.next_out = (unsigned char *)PyByteArray_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000414
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000415 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000416 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000417 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000418
Jeremy Hylton9714f992001-10-16 21:19:45 +0000419 /* while Z_OK and the output buffer is full, there might be more output,
420 so extend the output buffer and try again */
421 while (err == Z_OK && self->zst.avail_out == 0) {
Christian Heimes9c4756e2008-05-26 13:22:05 +0000422 if (PyByteArray_Resize(RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000423 Py_DECREF(RetVal);
424 RetVal = NULL;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000425 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000426 }
427 self->zst.next_out =
Christian Heimes9c4756e2008-05-26 13:22:05 +0000428 (unsigned char *)PyByteArray_AS_STRING(RetVal) + length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000429 self->zst.avail_out = length;
430 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000431
Jeremy Hylton9714f992001-10-16 21:19:45 +0000432 Py_BEGIN_ALLOW_THREADS
433 err = deflate(&(self->zst), Z_NO_FLUSH);
434 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000435 }
Tim Peters977e5402001-10-17 03:57:20 +0000436 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000437 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000438 condition.
439 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000440
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000441 if (err != Z_OK && err != Z_BUF_ERROR) {
442 zlib_error(self->zst, err, "while compressing");
443 Py_DECREF(RetVal);
444 RetVal = NULL;
445 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000446 }
Christian Heimes9c4756e2008-05-26 13:22:05 +0000447 if (PyByteArray_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000448 Py_DECREF(RetVal);
449 RetVal = NULL;
450 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000451
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000452 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000453 LEAVE_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000454 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000455}
456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000458"decompress(data, max_length) -- Return a string containing the decompressed\n"
459"version of the data.\n"
460"\n"
461"After calling this function, some of the input data may still be stored in\n"
462"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000463"Call the flush() method to clear these buffers.\n"
464"If the max_length parameter is specified then the return value will be\n"
465"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000466"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000467
Guido van Rossumfb221561997-04-29 15:38:09 +0000468static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000469PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000470{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000471 int err, inplen, old_length, length = DEFAULTALLOC;
472 int max_length = 0;
473 PyObject *RetVal;
474 Byte *input;
475 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000476
Tim Peters977e5402001-10-17 03:57:20 +0000477 if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000478 &inplen, &max_length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000479 return NULL;
480 if (max_length < 0) {
481 PyErr_SetString(PyExc_ValueError,
482 "max_length must be greater than zero");
483 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000484 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000485
Jeremy Hylton9714f992001-10-16 21:19:45 +0000486 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000487 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000488 length = max_length;
Christian Heimes9c4756e2008-05-26 13:22:05 +0000489 if (!(RetVal = PyByteArray_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000490 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000491
492 ENTER_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000493
Jeremy Hylton9714f992001-10-16 21:19:45 +0000494 start_total_out = self->zst.total_out;
495 self->zst.avail_in = inplen;
496 self->zst.next_in = input;
497 self->zst.avail_out = length;
Christian Heimes9c4756e2008-05-26 13:22:05 +0000498 self->zst.next_out = (unsigned char *)PyByteArray_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000499
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000500 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000501 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000502 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000503
Jeremy Hylton9714f992001-10-16 21:19:45 +0000504 /* While Z_OK and the output buffer is full, there might be more output.
505 So extend the output buffer and try again.
506 */
Tim Peters977e5402001-10-17 03:57:20 +0000507 while (err == Z_OK && self->zst.avail_out == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000508 /* If max_length set, don't continue decompressing if we've already
509 reached the limit.
510 */
511 if (max_length && length >= max_length)
512 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000513
Jeremy Hylton9714f992001-10-16 21:19:45 +0000514 /* otherwise, ... */
515 old_length = length;
516 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000517 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000518 length = max_length;
519
Christian Heimes9c4756e2008-05-26 13:22:05 +0000520 if (PyByteArray_Resize(RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000521 Py_DECREF(RetVal);
522 RetVal = NULL;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000523 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000524 }
525 self->zst.next_out =
Christian Heimes9c4756e2008-05-26 13:22:05 +0000526 (unsigned char *)PyByteArray_AS_STRING(RetVal) + old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000527 self->zst.avail_out = length - old_length;
528
529 Py_BEGIN_ALLOW_THREADS
530 err = inflate(&(self->zst), Z_SYNC_FLUSH);
531 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000532 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000533
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000534 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000535 of specified size. Return the unconsumed tail in an attribute.*/
536 if(max_length) {
537 Py_DECREF(self->unconsumed_tail);
Christian Heimes9c4756e2008-05-26 13:22:05 +0000538 self->unconsumed_tail = PyByteArray_FromStringAndSize((char *)self->zst.next_in,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000539 self->zst.avail_in);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000540 if(!self->unconsumed_tail) {
541 Py_DECREF(RetVal);
542 RetVal = NULL;
543 goto error;
544 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000545 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000546
Tim Peters977e5402001-10-17 03:57:20 +0000547 /* The end of the compressed data has been reached, so set the
548 unused_data attribute to a string containing the remainder of the
549 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000550 inflateEnd, but the old behaviour of only calling it on flush() is
551 preserved.
552 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000553 if (err == Z_STREAM_END) {
554 Py_XDECREF(self->unused_data); /* Free original empty string */
Christian Heimes9c4756e2008-05-26 13:22:05 +0000555 self->unused_data = PyByteArray_FromStringAndSize(
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000556 (char *)self->zst.next_in, self->zst.avail_in);
557 if (self->unused_data == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000558 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000559 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000560 }
Tim Peters977e5402001-10-17 03:57:20 +0000561 /* We will only get Z_BUF_ERROR if the output buffer was full
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000562 but there wasn't more output when we tried again, so it is
563 not an error condition.
564 */
565 } else if (err != Z_OK && err != Z_BUF_ERROR) {
566 zlib_error(self->zst, err, "while decompressing");
567 Py_DECREF(RetVal);
568 RetVal = NULL;
569 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000570 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000571
Christian Heimes9c4756e2008-05-26 13:22:05 +0000572 if (PyByteArray_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000573 Py_DECREF(RetVal);
574 RetVal = NULL;
575 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000576
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000577 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000578 LEAVE_ZLIB
579
580 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000581}
582
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000583PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000584"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000585"\n"
586"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000587"default value used when mode is not specified is Z_FINISH.\n"
588"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000589"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000590
Guido van Rossumfb221561997-04-29 15:38:09 +0000591static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000592PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000593{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000594 int err, length = DEFAULTALLOC;
595 PyObject *RetVal;
596 int flushmode = Z_FINISH;
597 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000598
Jeremy Hylton9714f992001-10-16 21:19:45 +0000599 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
600 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000601
Jeremy Hylton9714f992001-10-16 21:19:45 +0000602 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
603 doing any work at all; just return an empty string. */
604 if (flushmode == Z_NO_FLUSH) {
Christian Heimes9c4756e2008-05-26 13:22:05 +0000605 return PyByteArray_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000606 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000607
Christian Heimes9c4756e2008-05-26 13:22:05 +0000608 if (!(RetVal = PyByteArray_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000609 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000610
611 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000612
Jeremy Hylton9714f992001-10-16 21:19:45 +0000613 start_total_out = self->zst.total_out;
614 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000615 self->zst.avail_out = length;
Christian Heimes9c4756e2008-05-26 13:22:05 +0000616 self->zst.next_out = (unsigned char *)PyByteArray_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000617
618 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000619 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000620 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000621
Jeremy Hylton9714f992001-10-16 21:19:45 +0000622 /* while Z_OK and the output buffer is full, there might be more output,
623 so extend the output buffer and try again */
624 while (err == Z_OK && self->zst.avail_out == 0) {
Christian Heimes9c4756e2008-05-26 13:22:05 +0000625 if (PyByteArray_Resize(RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000626 Py_DECREF(RetVal);
627 RetVal = NULL;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000628 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000629 }
630 self->zst.next_out =
Christian Heimes9c4756e2008-05-26 13:22:05 +0000631 (unsigned char *)PyByteArray_AS_STRING(RetVal) + length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000632 self->zst.avail_out = length;
633 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000634
Jeremy Hylton9714f992001-10-16 21:19:45 +0000635 Py_BEGIN_ALLOW_THREADS
636 err = deflate(&(self->zst), flushmode);
637 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000638 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000639
Jeremy Hylton9714f992001-10-16 21:19:45 +0000640 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000641 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000642 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000643 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
644 err = deflateEnd(&(self->zst));
645 if (err != Z_OK) {
646 zlib_error(self->zst, err, "from deflateEnd()");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000648 RetVal = NULL;
649 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000650 }
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000651 else
652 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000653
654 /* We will only get Z_BUF_ERROR if the output buffer was full
655 but there wasn't more output when we tried again, so it is
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000656 not an error condition.
657 */
658 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
659 zlib_error(self->zst, err, "while flushing");
660 Py_DECREF(RetVal);
661 RetVal = NULL;
Jeremy Hyltonc72737e2002-04-19 14:37:07 +0000662 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000663 }
Tim Peters977e5402001-10-17 03:57:20 +0000664
Christian Heimes9c4756e2008-05-26 13:22:05 +0000665 if (PyByteArray_Resize(RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000666 Py_DECREF(RetVal);
667 RetVal = NULL;
668 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000669
Tim Peters977e5402001-10-17 03:57:20 +0000670 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000671 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000672
673 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000674}
675
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000676#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677PyDoc_STRVAR(comp_copy__doc__,
678"copy() -- Return a copy of the compression object.");
679
680static PyObject *
681PyZlib_copy(compobject *self)
682{
683 compobject *retval = NULL;
684 int err;
685
686 retval = newcompobject(&Comptype);
687 if (!retval) return NULL;
688
689 /* Copy the zstream state
690 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
691 */
692 ENTER_ZLIB
693 err = deflateCopy(&retval->zst, &self->zst);
694 switch(err) {
695 case(Z_OK):
696 break;
697 case(Z_STREAM_ERROR):
698 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
699 goto error;
700 case(Z_MEM_ERROR):
701 PyErr_SetString(PyExc_MemoryError,
702 "Can't allocate memory for compression object");
703 goto error;
704 default:
705 zlib_error(self->zst, err, "while copying compression object");
706 goto error;
707 }
708
709 Py_INCREF(self->unused_data);
710 Py_INCREF(self->unconsumed_tail);
711 Py_XDECREF(retval->unused_data);
712 Py_XDECREF(retval->unconsumed_tail);
713 retval->unused_data = self->unused_data;
714 retval->unconsumed_tail = self->unconsumed_tail;
715
716 /* Mark it as being initialized */
717 retval->is_initialised = 1;
718
719 LEAVE_ZLIB
720 return (PyObject *)retval;
721
722error:
723 LEAVE_ZLIB
724 Py_XDECREF(retval);
725 return NULL;
726}
727
728PyDoc_STRVAR(decomp_copy__doc__,
729"copy() -- Return a copy of the decompression object.");
730
731static PyObject *
732PyZlib_uncopy(compobject *self)
733{
734 compobject *retval = NULL;
735 int err;
736
737 retval = newcompobject(&Decomptype);
738 if (!retval) return NULL;
739
740 /* Copy the zstream state
741 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
742 */
743 ENTER_ZLIB
744 err = inflateCopy(&retval->zst, &self->zst);
745 switch(err) {
746 case(Z_OK):
747 break;
748 case(Z_STREAM_ERROR):
749 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
750 goto error;
751 case(Z_MEM_ERROR):
752 PyErr_SetString(PyExc_MemoryError,
753 "Can't allocate memory for decompression object");
754 goto error;
755 default:
756 zlib_error(self->zst, err, "while copying decompression object");
757 goto error;
758 }
759
760 Py_INCREF(self->unused_data);
761 Py_INCREF(self->unconsumed_tail);
762 Py_XDECREF(retval->unused_data);
763 Py_XDECREF(retval->unconsumed_tail);
764 retval->unused_data = self->unused_data;
765 retval->unconsumed_tail = self->unconsumed_tail;
766
767 /* Mark it as being initialized */
768 retval->is_initialised = 1;
769
770 LEAVE_ZLIB
771 return (PyObject *)retval;
772
773error:
774 LEAVE_ZLIB
775 Py_XDECREF(retval);
776 return NULL;
777}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000778#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000780PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000781"flush( [length] ) -- Return a string containing any remaining\n"
782"decompressed data. length, if given, is the initial size of the\n"
783"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000784"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000785"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000786
Guido van Rossumfb221561997-04-29 15:38:09 +0000787static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000788PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000789{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000790 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000791 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000792 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000793
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000794 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000795 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000796 if (length <= 0) {
797 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
798 return NULL;
799 }
Christian Heimes9c4756e2008-05-26 13:22:05 +0000800 if (!(retval = PyByteArray_FromStringAndSize(NULL, length)))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000801 return NULL;
802
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000803
Jeremy Hylton9714f992001-10-16 21:19:45 +0000804 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000805
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000806 start_total_out = self->zst.total_out;
807 self->zst.avail_out = length;
Christian Heimes9c4756e2008-05-26 13:22:05 +0000808 self->zst.next_out = (Byte *)PyByteArray_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000809
810 Py_BEGIN_ALLOW_THREADS
811 err = inflate(&(self->zst), Z_FINISH);
812 Py_END_ALLOW_THREADS
813
814 /* while Z_OK and the output buffer is full, there might be more output,
815 so extend the output buffer and try again */
816 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Christian Heimes9c4756e2008-05-26 13:22:05 +0000817 if (PyByteArray_Resize(retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000818 Py_DECREF(retval);
819 retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000820 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000821 }
Christian Heimes9c4756e2008-05-26 13:22:05 +0000822 self->zst.next_out = (Byte *)PyByteArray_AS_STRING(retval) + length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000823 self->zst.avail_out = length;
824 length = length << 1;
825
826 Py_BEGIN_ALLOW_THREADS
827 err = inflate(&(self->zst), Z_FINISH);
828 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000829 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000830
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000831 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
832 various data structures. Note we should only get Z_STREAM_END when
833 flushmode is Z_FINISH */
834 if (err == Z_STREAM_END) {
835 err = inflateEnd(&(self->zst));
836 self->is_initialised = 0;
837 if (err != Z_OK) {
838 zlib_error(self->zst, err, "from inflateEnd()");
839 Py_DECREF(retval);
840 retval = NULL;
841 goto error;
842 }
843 }
Christian Heimes9c4756e2008-05-26 13:22:05 +0000844 if (PyByteArray_Resize(retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000845 Py_DECREF(retval);
846 retval = NULL;
847 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000848
849error:
850
Jeremy Hylton9714f992001-10-16 21:19:45 +0000851 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000852
Jeremy Hylton9714f992001-10-16 21:19:45 +0000853 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000854}
855
856static PyMethodDef comp_methods[] =
857{
Tim Peters977e5402001-10-17 03:57:20 +0000858 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000859 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000860 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000861 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000862#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
864 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000865#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000866 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000867};
868
869static PyMethodDef Decomp_methods[] =
870{
Tim Peters977e5402001-10-17 03:57:20 +0000871 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000872 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000873 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000874 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000875#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000876 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
877 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000878#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000879 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000880};
881
882static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000883Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000884{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000885 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
886 internal data. */
887
888 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000889}
890
891static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000892Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000893{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000895
Jeremy Hylton9714f992001-10-16 21:19:45 +0000896 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000897
Tim Peters977e5402001-10-17 03:57:20 +0000898 if (strcmp(name, "unused_data") == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000899 Py_INCREF(self->unused_data);
900 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000901 } else if (strcmp(name, "unconsumed_tail") == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000902 Py_INCREF(self->unconsumed_tail);
903 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000904 } else
Jeremy Hylton9714f992001-10-16 21:19:45 +0000905 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000906
Jeremy Hylton9714f992001-10-16 21:19:45 +0000907 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000908
Jeremy Hylton9714f992001-10-16 21:19:45 +0000909 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000910}
911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000912PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000913"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
914"\n"
915"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000916"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000917
Guido van Rossumfb221561997-04-29 15:38:09 +0000918static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000919PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000920{
Christian Heimescc47b052008-03-25 14:56:36 +0000921 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000922 Byte *buf;
923 int len;
Tim Peters977e5402001-10-17 03:57:20 +0000924
Gregory P. Smith27275032008-03-20 06:20:09 +0000925 if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000926 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000927 adler32val = adler32(adler32val, buf, len);
Gregory P. Smith27275032008-03-20 06:20:09 +0000928 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000929}
Tim Peters977e5402001-10-17 03:57:20 +0000930
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000931PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000932"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
933"\n"
934"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000935"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000936
937static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000938PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000939{
Christian Heimescc47b052008-03-25 14:56:36 +0000940 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000941 Byte *buf;
Christian Heimescc47b052008-03-25 14:56:36 +0000942 int len, signed_val;
943
Gregory P. Smith27275032008-03-20 06:20:09 +0000944 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000945 return NULL;
Christian Heimescc47b052008-03-25 14:56:36 +0000946 signed_val = crc32(crc32val, buf, len);
947 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000948}
Tim Peters977e5402001-10-17 03:57:20 +0000949
Guido van Rossumfb221561997-04-29 15:38:09 +0000950
951static PyMethodDef zlib_methods[] =
952{
Tim Peters977e5402001-10-17 03:57:20 +0000953 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000954 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000955 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000956 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000957 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000958 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000959 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
960 crc32__doc__},
961 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000962 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000963 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000964 decompressobj__doc__},
965 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000966};
967
Tim Peters0c322792002-07-17 16:49:03 +0000968static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000969 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000970 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000971 sizeof(compobject),
972 0,
973 (destructor)Comp_dealloc, /*tp_dealloc*/
974 0, /*tp_print*/
975 (getattrfunc)Comp_getattr, /*tp_getattr*/
976 0, /*tp_setattr*/
977 0, /*tp_compare*/
978 0, /*tp_repr*/
979 0, /*tp_as_number*/
980 0, /*tp_as_sequence*/
981 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000982};
983
Tim Peters0c322792002-07-17 16:49:03 +0000984static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000985 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000986 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000987 sizeof(compobject),
988 0,
989 (destructor)Decomp_dealloc, /*tp_dealloc*/
990 0, /*tp_print*/
991 (getattrfunc)Decomp_getattr, /*tp_getattr*/
992 0, /*tp_setattr*/
993 0, /*tp_compare*/
994 0, /*tp_repr*/
995 0, /*tp_as_number*/
996 0, /*tp_as_sequence*/
997 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000998};
999
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001000PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001001"The functions in this module allow compression and decompression using the\n"
1002"zlib library, which is based on GNU zip.\n"
1003"\n"
1004"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1005"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001006"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001007"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001008"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001009"decompressobj([wbits]) -- Return a decompressor object.\n"
1010"\n"
1011"'wbits' is window buffer size.\n"
1012"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001014
Martin v. Löwis1a214512008-06-11 05:26:20 +00001015static struct PyModuleDef zlibmodule = {
1016 PyModuleDef_HEAD_INIT,
1017 "zlib",
1018 zlib_module_documentation,
1019 -1,
1020 zlib_methods,
1021 NULL,
1022 NULL,
1023 NULL,
1024 NULL
1025};
1026
Mark Hammond62b1ab12002-07-23 06:31:15 +00001027PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001028PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001029{
Fred Drake4baedc12002-04-01 14:53:37 +00001030 PyObject *m, *ver;
Christian Heimes90aa7642007-12-19 02:45:37 +00001031 Py_TYPE(&Comptype) = &PyType_Type;
1032 Py_TYPE(&Decomptype) = &PyType_Type;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001033 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001034 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001035 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001036
Fred Drake4baedc12002-04-01 14:53:37 +00001037 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1038 if (ZlibError != NULL) {
1039 Py_INCREF(ZlibError);
1040 PyModule_AddObject(m, "error", ZlibError);
1041 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001042 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1043 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1044 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1045 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1046 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1047 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1048 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1049 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1050 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001051
Jeremy Hylton9714f992001-10-16 21:19:45 +00001052 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1053 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1054 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1055 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001056
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001057 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001058 if (ver != NULL)
1059 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001060
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001061 PyModule_AddStringConstant(m, "__version__", "1.0");
1062
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001063#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001064 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001065#endif /* WITH_THREAD */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001066 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001067}