blob: 2a79d54b72c0de5a4f84c372c77f238cd0c9202f [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin Panter1e411c52016-07-23 04:22:09 +00002/* See http://zlib.net/ */
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
Martin Panter1e411c52016-07-23 04:22:09 +00006#define PY_SSIZE_T_CLEAN
Guido van Rossumfb221561997-04-29 15:38:09 +00007
Guido van Rossum97b54571997-06-03 22:21:47 +00008#include "Python.h"
9#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000010
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000011#ifdef WITH_THREAD
12#include "pythread.h"
13
14/* #defs ripped off from _tkinter.c, even though the situation here is much
15 simpler, because we don't have to worry about waiting for Tcl
16 events! And, since zlib itself is threadsafe, we don't need to worry
17 about re-entering zlib functions.
18
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019 N.B.
20
21 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
22 that modify the components of preexisting de/compress objects, it
23 could prove to be a performance gain on multiprocessor machines if
24 there was an de/compress object-specific lock. However, for the
25 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
26 de/compress objects.
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000027 */
28
29static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
30
31#define ENTER_ZLIB \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000032 Py_BEGIN_ALLOW_THREADS \
33 PyThread_acquire_lock(zlib_lock, 1); \
34 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000035
36#define LEAVE_ZLIB \
Antoine Pitrouc83ea132010-05-09 14:46:46 +000037 PyThread_release_lock(zlib_lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000038
39#else
40
41#define ENTER_ZLIB
42#define LEAVE_ZLIB
43
44#endif
45
Guido van Rossumfb221561997-04-29 15:38:09 +000046/* The following parameters are copied from zutil.h, version 0.95 */
47#define DEFLATED 8
48#if MAX_MEM_LEVEL >= 8
49# define DEF_MEM_LEVEL 8
50#else
51# define DEF_MEM_LEVEL MAX_MEM_LEVEL
52#endif
53#define DEF_WBITS MAX_WBITS
54
Guido van Rossumb729a1d1999-04-07 20:23:17 +000055/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
56#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000057#define PyInit_zlib initzlib
58
Jeremy Hylton938ace62002-07-17 16:30:39 +000059static PyTypeObject Comptype;
60static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000061
62static PyObject *ZlibError;
63
Tim Peters977e5402001-10-17 03:57:20 +000064typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000065{
Jeremy Hylton9714f992001-10-16 21:19:45 +000066 PyObject_HEAD
67 z_stream zst;
68 PyObject *unused_data;
69 PyObject *unconsumed_tail;
70 int is_initialised;
Guido van Rossumfb221561997-04-29 15:38:09 +000071} compobject;
72
Jeremy Hylton0965e082001-10-16 21:56:09 +000073static void
74zlib_error(z_stream zst, int err, char *msg)
75{
Nadeem Vawdabbabbae2011-08-28 11:23:57 +020076 const char *zmsg = Z_NULL;
77 /* In case of a version mismatch, zst.msg won't be initialized.
78 Check for this case first, before looking at zst.msg. */
79 if (err == Z_VERSION_ERROR)
80 zmsg = "library version mismatch";
81 if (zmsg == Z_NULL)
82 zmsg = zst.msg;
Antoine Pitroufc3bfad2010-05-11 23:42:28 +000083 if (zmsg == Z_NULL) {
84 switch (err) {
85 case Z_BUF_ERROR:
86 zmsg = "incomplete or truncated stream";
87 break;
88 case Z_STREAM_ERROR:
89 zmsg = "inconsistent stream state";
90 break;
91 case Z_DATA_ERROR:
92 zmsg = "invalid input data";
93 break;
94 }
95 }
96 if (zmsg == Z_NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000097 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000098 else
Antoine Pitroufc3bfad2010-05-11 23:42:28 +000099 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000100}
101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000102PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000103"compressobj([level]) -- Return a compressor object.\n"
104"\n"
Martin Panter1d269c12016-02-03 07:06:33 +0000105"Optional arg level is the compression level, in 0-9 or -1.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000107PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000108"decompressobj([wbits]) -- Return a decompressor object.\n"
109"\n"
Martin Panter9c946bb2016-05-27 07:32:11 +0000110"Optional arg wbits indicates the window buffer size and container format.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000111
Guido van Rossumfb221561997-04-29 15:38:09 +0000112static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000113newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000114{
Tim Peters977e5402001-10-17 03:57:20 +0000115 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000116 self = PyObject_New(compobject, type);
117 if (self == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000118 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 self->is_initialised = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000120 self->unused_data = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000121 if (self->unused_data == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000122 Py_DECREF(self);
123 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000124 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000125 self->unconsumed_tail = PyString_FromString("");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000126 if (self->unconsumed_tail == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000127 Py_DECREF(self);
128 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 }
130 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000131}
132
Martin Panter1e411c52016-07-23 04:22:09 +0000133static void
134arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
135{
136 if ((size_t)*remains > UINT_MAX) {
137 zst->avail_in = UINT_MAX;
138 } else {
139 zst->avail_in = *remains;
140 }
141 *remains -= zst->avail_in;
142}
143
144static Py_ssize_t
145arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
146 Py_ssize_t length,
147 Py_ssize_t max_length)
148{
149 Py_ssize_t occupied;
150
151 if (*buffer == NULL) {
152 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
153 return -1;
154 occupied = 0;
155 }
156 else {
157 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
158
159 if (length == occupied) {
160 Py_ssize_t new_length;
161 assert(length <= max_length);
162 /* can not scale the buffer over max_length */
163 if (length == max_length)
164 return -2;
165 if (length <= (max_length >> 1))
166 new_length = length << 1;
167 else
168 new_length = max_length;
169 if (_PyBytes_Resize(buffer, new_length) < 0)
170 return -1;
171 length = new_length;
172 }
173 }
174
175 if ((size_t)(length - occupied) > UINT_MAX) {
176 zst->avail_out = UINT_MAX;
177 } else {
178 zst->avail_out = length - occupied;
179 }
180 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
181
182 return length;
183}
184
185static Py_ssize_t
186arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
187{
188 Py_ssize_t ret;
189
190 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
191 PY_SSIZE_T_MAX);
192 if (ret == -2)
193 PyErr_NoMemory();
194
195 return ret;
196}
197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000199"compress(string[, level]) -- Returned compressed string.\n"
200"\n"
Nadeem Vawda99f9b8d2012-11-11 14:01:23 +0100201"Optional arg level is the compression level, in 0-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000202
Guido van Rossumfb221561997-04-29 15:38:09 +0000203static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000204PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000205{
Martin Panter1e411c52016-07-23 04:22:09 +0000206 PyObject *RetVal = NULL;
207 Byte *ibuf;
208 Py_ssize_t ibuflen, obuflen = DEFAULTALLOC;
209 int level=Z_DEFAULT_COMPRESSION;
210 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000211 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000212
Jeremy Hylton9714f992001-10-16 21:19:45 +0000213 /* require Python string object, optional 'level' arg */
Martin Panter1e411c52016-07-23 04:22:09 +0000214 if (!PyArg_ParseTuple(args, "s#|i:compress", &ibuf, &ibuflen, &level))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000216
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000217 zst.zalloc = (alloc_func)NULL;
218 zst.zfree = (free_func)Z_NULL;
Martin Panter1e411c52016-07-23 04:22:09 +0000219 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000220 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000221
Martin Panter1e411c52016-07-23 04:22:09 +0000222 switch (err) {
223 case Z_OK:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 break;
Martin Panter1e411c52016-07-23 04:22:09 +0000225 case Z_MEM_ERROR:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226 PyErr_SetString(PyExc_MemoryError,
227 "Out of memory while compressing data");
228 goto error;
Martin Panter1e411c52016-07-23 04:22:09 +0000229 case Z_STREAM_ERROR:
230 PyErr_SetString(ZlibError, "Bad compression level");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000232 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000233 deflateEnd(&zst);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 zlib_error(zst, err, "while compressing data");
235 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000236 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000237
Martin Panter1e411c52016-07-23 04:22:09 +0000238 do {
239 arrange_input_buffer(&zst, &ibuflen);
240 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000241
Martin Panter1e411c52016-07-23 04:22:09 +0000242 do {
243 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
244 if (obuflen < 0) {
245 deflateEnd(&zst);
246 goto error;
247 }
248
249 Py_BEGIN_ALLOW_THREADS
250 err = deflate(&zst, flush);
251 Py_END_ALLOW_THREADS
252
253 if (err == Z_STREAM_ERROR) {
254 deflateEnd(&zst);
255 zlib_error(zst, err, "while compressing data");
256 goto error;
257 }
258
259 } while (zst.avail_out == 0);
260 assert(zst.avail_in == 0);
261
262 } while (flush != Z_FINISH);
263 assert(err == Z_STREAM_END);
264
265 err = deflateEnd(&zst);
266 if (err == Z_OK) {
267 if (_PyBytes_Resize(&RetVal, zst.next_out -
268 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
269 goto error;
270 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000271 }
Tim Peters977e5402001-10-17 03:57:20 +0000272 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000274 error:
Martin Panter1e411c52016-07-23 04:22:09 +0000275 Py_XDECREF(RetVal);
276 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000277}
278
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000279PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000280"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
281"\n"
Martin Panter9c946bb2016-05-27 07:32:11 +0000282"Optional arg wbits indicates the window buffer size and container format.\n"
283"Optional arg bufsize is the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000284
Guido van Rossumfb221561997-04-29 15:38:09 +0000285static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000286PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000287{
Martin Panter1e411c52016-07-23 04:22:09 +0000288 PyObject *RetVal = NULL;
289 Byte *ibuf;
290 Py_ssize_t ibuflen;
291 int err, flush;
Christian Heimes901071b2007-11-21 00:46:21 +0000292 int wsize=DEF_WBITS;
293 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000294 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000295
Christian Heimes901071b2007-11-21 00:46:21 +0000296 if (!PyArg_ParseTuple(args, "s#|in:decompress",
Martin Panter1e411c52016-07-23 04:22:09 +0000297 &ibuf, &ibuflen, &wsize, &r_strlen))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000299
Martin Panter1e411c52016-07-23 04:22:09 +0000300 if (r_strlen <= 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 r_strlen = 1;
Martin Panter1e411c52016-07-23 04:22:09 +0000302 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000303
Jeremy Hylton9714f992001-10-16 21:19:45 +0000304 zst.zalloc = (alloc_func)NULL;
305 zst.zfree = (free_func)Z_NULL;
Martin Panter1e411c52016-07-23 04:22:09 +0000306 zst.avail_in = 0;
307 zst.next_in = ibuf;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000308 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000309
Martin Panter1e411c52016-07-23 04:22:09 +0000310 switch (err) {
311 case Z_OK:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000312 break;
Martin Panter1e411c52016-07-23 04:22:09 +0000313 case Z_MEM_ERROR:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000314 PyErr_SetString(PyExc_MemoryError,
315 "Out of memory while decompressing data");
316 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000317 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000318 inflateEnd(&zst);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000319 zlib_error(zst, err, "while preparing to decompress data");
320 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000321 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000322
Jeremy Hylton9714f992001-10-16 21:19:45 +0000323 do {
Martin Panter1e411c52016-07-23 04:22:09 +0000324 arrange_input_buffer(&zst, &ibuflen);
325 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000326
Martin Panter1e411c52016-07-23 04:22:09 +0000327 do {
328 r_strlen = arrange_output_buffer(&zst, &RetVal, r_strlen);
329 if (r_strlen < 0) {
330 inflateEnd(&zst);
331 goto error;
332 }
333
334 Py_BEGIN_ALLOW_THREADS
335 err = inflate(&zst, flush);
336 Py_END_ALLOW_THREADS
337
338 switch (err) {
339 case Z_OK: /* fall through */
340 case Z_BUF_ERROR: /* fall through */
341 case Z_STREAM_END:
342 break;
343 case Z_MEM_ERROR:
344 inflateEnd(&zst);
345 PyErr_SetString(PyExc_MemoryError,
346 "Out of memory while decompressing data");
347 goto error;
348 default:
349 inflateEnd(&zst);
Antoine Pitroufc3bfad2010-05-11 23:42:28 +0000350 zlib_error(zst, err, "while decompressing data");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000351 goto error;
352 }
Martin Panter1e411c52016-07-23 04:22:09 +0000353
354 } while (zst.avail_out == 0);
355
356 } while (err != Z_STREAM_END && ibuflen != 0);
357
358
359 if (err != Z_STREAM_END) {
360 inflateEnd(&zst);
361 zlib_error(zst, err, "while decompressing data");
362 goto error;
363 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000364
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000365 err = inflateEnd(&zst);
366 if (err != Z_OK) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000367 zlib_error(zst, err, "while finishing data decompression");
368 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000369 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000370
Martin Panter1e411c52016-07-23 04:22:09 +0000371 _PyString_Resize(&RetVal, zst.next_out -
372 (Byte *)PyBytes_AS_STRING(RetVal));
373 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000374
375 error:
Martin Panter1e411c52016-07-23 04:22:09 +0000376 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000377 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000378}
379
380static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000381PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000382{
Jeremy Hylton499000002001-10-16 21:59:35 +0000383 compobject *self;
384 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
385 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000386
Jeremy Hylton499000002001-10-16 21:59:35 +0000387 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 &memLevel, &strategy))
389 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000390
Jeremy Hylton499000002001-10-16 21:59:35 +0000391 self = newcompobject(&Comptype);
Martin Panter1e411c52016-07-23 04:22:09 +0000392 if (self == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000393 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000394 self->zst.zalloc = (alloc_func)NULL;
395 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000396 self->zst.next_in = NULL;
397 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000398 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter1e411c52016-07-23 04:22:09 +0000399 switch (err) {
400 case Z_OK:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000401 self->is_initialised = 1;
Martin Panter1e411c52016-07-23 04:22:09 +0000402 return (PyObject *)self;
403 case Z_MEM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000404 Py_DECREF(self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000405 PyErr_SetString(PyExc_MemoryError,
406 "Can't allocate memory for compression object");
407 return NULL;
Martin Panter1e411c52016-07-23 04:22:09 +0000408 case Z_STREAM_ERROR:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000409 Py_DECREF(self);
410 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
411 return NULL;
412 default:
413 zlib_error(self->zst, err, "while creating compression object");
414 Py_DECREF(self);
415 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000416 }
417}
418
419static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000420PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000421{
Jeremy Hylton499000002001-10-16 21:59:35 +0000422 int wbits=DEF_WBITS, err;
423 compobject *self;
424 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000425 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000426
427 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000428 if (self == NULL)
Martin Panter1e411c52016-07-23 04:22:09 +0000429 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000430 self->zst.zalloc = (alloc_func)NULL;
431 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000432 self->zst.next_in = NULL;
433 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000434 err = inflateInit2(&self->zst, wbits);
Martin Panter1e411c52016-07-23 04:22:09 +0000435 switch (err) {
436 case Z_OK:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000437 self->is_initialised = 1;
Martin Panter1e411c52016-07-23 04:22:09 +0000438 return (PyObject *)self;
439 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000440 Py_DECREF(self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000441 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
442 return NULL;
Martin Panter1e411c52016-07-23 04:22:09 +0000443 case Z_MEM_ERROR:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000444 Py_DECREF(self);
445 PyErr_SetString(PyExc_MemoryError,
446 "Can't allocate memory for decompression object");
447 return NULL;
448 default:
449 zlib_error(self->zst, err, "while creating decompression object");
450 Py_DECREF(self);
451 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000452 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000453}
454
455static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000456Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000457{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000458 if (self->is_initialised)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000460 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000461 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000462 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000463}
464
465static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000466Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000467{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000468 if (self->is_initialised)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000469 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000470 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000471 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000472 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000473}
474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000475PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000476"compress(data) -- Return a string containing data compressed.\n"
477"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000478"After calling this function, some of the input data may still\n"
479"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000481
482
Guido van Rossumfb221561997-04-29 15:38:09 +0000483static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000484PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000485{
Martin Panter1e411c52016-07-23 04:22:09 +0000486 PyObject *RetVal = NULL;
487 Py_ssize_t ibuflen, obuflen = DEFAULTALLOC;
488 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000489
Martin Panter1e411c52016-07-23 04:22:09 +0000490 if (!PyArg_ParseTuple(args, "s#:compress", &self->zst.next_in, &ibuflen))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000492
493 ENTER_ZLIB
494
Martin Panter1e411c52016-07-23 04:22:09 +0000495 do {
496 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000497
Martin Panter1e411c52016-07-23 04:22:09 +0000498 do {
499 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
500 if (obuflen < 0)
501 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000502
Martin Panter1e411c52016-07-23 04:22:09 +0000503 Py_BEGIN_ALLOW_THREADS
504 err = deflate(&self->zst, Z_NO_FLUSH);
505 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000506
Martin Panter1e411c52016-07-23 04:22:09 +0000507 if (err == Z_STREAM_ERROR) {
508 zlib_error(self->zst, err, "while compressing data");
509 goto error;
510 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000511
Martin Panter1e411c52016-07-23 04:22:09 +0000512 } while (self->zst.avail_out == 0);
513 assert(self->zst.avail_in == 0);
514
515 } while (ibuflen != 0);
516
517 _PyString_Resize(&RetVal, self->zst.next_out -
518 (Byte *)PyBytes_AS_STRING(RetVal));
519 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000520
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000521 error:
Martin Panter1e411c52016-07-23 04:22:09 +0000522 Py_CLEAR(RetVal);
523 success:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000524 LEAVE_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000525 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000526}
527
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100528/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
529 self->unused_data or self->unconsumed_tail, as appropriate. */
530static int
Martin Panter1e411c52016-07-23 04:22:09 +0000531save_unconsumed_input(compobject *self, Byte *input, Py_ssize_t inplen,
532 int err)
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100533{
534 if (err == Z_STREAM_END) {
535 /* The end of the compressed data has been reached. Store the leftover
536 input data in self->unused_data. */
537 if (self->zst.avail_in > 0) {
538 Py_ssize_t old_size = PyString_GET_SIZE(self->unused_data);
Martin Panter1e411c52016-07-23 04:22:09 +0000539 Py_ssize_t new_size, left_size;
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100540 PyObject *new_data;
Martin Panter1e411c52016-07-23 04:22:09 +0000541 left_size = input + inplen - self->zst.next_in;
542 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100543 PyErr_NoMemory();
544 return -1;
545 }
Martin Panter1e411c52016-07-23 04:22:09 +0000546 new_size = old_size + left_size;
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100547 new_data = PyString_FromStringAndSize(NULL, new_size);
548 if (new_data == NULL)
549 return -1;
550 Py_MEMCPY(PyString_AS_STRING(new_data),
551 PyString_AS_STRING(self->unused_data), old_size);
552 Py_MEMCPY(PyString_AS_STRING(new_data) + old_size,
Martin Panter1e411c52016-07-23 04:22:09 +0000553 self->zst.next_in, left_size);
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300554 Py_SETREF(self->unused_data, new_data);
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100555 self->zst.avail_in = 0;
556 }
557 }
Martin Panter1e411c52016-07-23 04:22:09 +0000558
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100559 if (self->zst.avail_in > 0 || PyString_GET_SIZE(self->unconsumed_tail)) {
560 /* This code handles two distinct cases:
561 1. Output limit was reached. Save leftover input in unconsumed_tail.
562 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter1e411c52016-07-23 04:22:09 +0000563 Py_ssize_t left_size = input + inplen - self->zst.next_in;
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100564 PyObject *new_data = PyString_FromStringAndSize(
Martin Panter1e411c52016-07-23 04:22:09 +0000565 (char *)self->zst.next_in, left_size);
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100566 if (new_data == NULL)
567 return -1;
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300568 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100569 }
Martin Panter1e411c52016-07-23 04:22:09 +0000570
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100571 return 0;
572}
573
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000574PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000575"decompress(data, max_length) -- Return a string containing the decompressed\n"
576"version of the data.\n"
577"\n"
578"After calling this function, some of the input data may still be stored in\n"
579"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000580"Call the flush() method to clear these buffers.\n"
581"If the max_length parameter is specified then the return value will be\n"
582"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000583"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000584
Guido van Rossumfb221561997-04-29 15:38:09 +0000585static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000586PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000587{
Martin Panter1e411c52016-07-23 04:22:09 +0000588 int err = Z_OK;
589 Py_ssize_t inplen, max_length = 0;
590 Py_ssize_t ibuflen, obuflen = DEFAULTALLOC, hard_limit;
591 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000592 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000593
Martin Panter1e411c52016-07-23 04:22:09 +0000594 if (!PyArg_ParseTuple(args, "s#|n:decompress", &input,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000595 &inplen, &max_length))
596 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000597 if (max_length < 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 PyErr_SetString(PyExc_ValueError,
599 "max_length must be greater than zero");
600 return NULL;
Martin Panter1e411c52016-07-23 04:22:09 +0000601 } else if (max_length == 0)
602 hard_limit = PY_SSIZE_T_MAX;
603 else
604 hard_limit = max_length;
605
606 self->zst.next_in = input;
607 ibuflen = inplen;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000608
Jeremy Hylton9714f992001-10-16 21:19:45 +0000609 /* limit amount of data allocated to max_length */
Martin Panter1e411c52016-07-23 04:22:09 +0000610 if (max_length && obuflen > max_length)
611 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000612
613 ENTER_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000614
Martin Panter1e411c52016-07-23 04:22:09 +0000615 do {
616 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000617
Martin Panter1e411c52016-07-23 04:22:09 +0000618 do {
619 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
620 obuflen, hard_limit);
621 if (obuflen == -2) {
622 if (max_length > 0) {
623 goto save;
624 }
625 PyErr_NoMemory();
626 }
627 if (obuflen < 0) {
628 goto abort;
629 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000630
Martin Panter1e411c52016-07-23 04:22:09 +0000631 Py_BEGIN_ALLOW_THREADS
632 err = inflate(&self->zst, Z_SYNC_FLUSH);
633 Py_END_ALLOW_THREADS
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000634
Martin Panter1e411c52016-07-23 04:22:09 +0000635 switch (err) {
636 case Z_OK: /* fall through */
637 case Z_BUF_ERROR: /* fall through */
638 case Z_STREAM_END:
639 break;
640 default:
641 goto save;
642 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643
Martin Panter1e411c52016-07-23 04:22:09 +0000644 } while (self->zst.avail_out == 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000645
Martin Panter1e411c52016-07-23 04:22:09 +0000646 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647
Martin Panter1e411c52016-07-23 04:22:09 +0000648 save:
649 if (save_unconsumed_input(self, input, inplen, err) < 0)
650 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000651
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100652 /* This is the logical place to call inflateEnd, but the old behaviour of
653 only calling it on flush() is preserved. */
654
655 if (err != Z_STREAM_END && err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 /* We will only get Z_BUF_ERROR if the output buffer was full
657 but there wasn't more output when we tried again, so it is
658 not an error condition.
659 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000660 zlib_error(self->zst, err, "while decompressing");
Martin Panter1e411c52016-07-23 04:22:09 +0000661 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000662 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000663
Martin Panter1e411c52016-07-23 04:22:09 +0000664 _PyString_Resize(&RetVal, self->zst.next_out -
665 (Byte *)PyBytes_AS_STRING(RetVal));
666 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000667
Martin Panter1e411c52016-07-23 04:22:09 +0000668 abort:
669 Py_CLEAR(RetVal);
670 success:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000671 LEAVE_ZLIB
672
673 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000674}
675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000677"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000678"\n"
679"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000680"default value used when mode is not specified is Z_FINISH.\n"
681"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000683
Guido van Rossumfb221561997-04-29 15:38:09 +0000684static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000685PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000686{
Martin Panter1e411c52016-07-23 04:22:09 +0000687 int err;
688 Py_ssize_t length = DEFAULTALLOC;
689 PyObject *RetVal = NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000690 int flushmode = Z_FINISH;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000691
Jeremy Hylton9714f992001-10-16 21:19:45 +0000692 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000693 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000694
Jeremy Hylton9714f992001-10-16 21:19:45 +0000695 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
696 doing any work at all; just return an empty string. */
697 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000698 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000699 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000700
Jeremy Hylton9714f992001-10-16 21:19:45 +0000701 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000702
Jeremy Hylton9714f992001-10-16 21:19:45 +0000703 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000704
Martin Panter1e411c52016-07-23 04:22:09 +0000705 do {
706 length = arrange_output_buffer(&self->zst, &RetVal, length);
707 if (length < 0) {
708 Py_CLEAR(RetVal);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 goto error;
Martin Panter1e411c52016-07-23 04:22:09 +0000710 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000711
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000712 Py_BEGIN_ALLOW_THREADS
Martin Panter1e411c52016-07-23 04:22:09 +0000713 err = deflate(&self->zst, flushmode);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000714 Py_END_ALLOW_THREADS
Martin Panter1e411c52016-07-23 04:22:09 +0000715
716 if (err == Z_STREAM_ERROR) {
717 zlib_error(self->zst, err, "while flushing");
718 Py_CLEAR(RetVal);
719 goto error;
720 }
721 } while (self->zst.avail_out == 0);
722 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000723
Jeremy Hylton9714f992001-10-16 21:19:45 +0000724 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000725 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000726 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000727 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Martin Panter1e411c52016-07-23 04:22:09 +0000728 err = deflateEnd(&self->zst);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 if (err != Z_OK) {
730 zlib_error(self->zst, err, "from deflateEnd()");
Martin Panter1e411c52016-07-23 04:22:09 +0000731 Py_CLEAR(RetVal);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000732 goto error;
733 }
734 else
735 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000736
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000737 /* We will only get Z_BUF_ERROR if the output buffer was full
738 but there wasn't more output when we tried again, so it is
739 not an error condition.
740 */
Martin Panter1e411c52016-07-23 04:22:09 +0000741 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000742 zlib_error(self->zst, err, "while flushing");
Martin Panter1e411c52016-07-23 04:22:09 +0000743 Py_CLEAR(RetVal);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000744 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000745 }
Tim Peters977e5402001-10-17 03:57:20 +0000746
Martin Panter1e411c52016-07-23 04:22:09 +0000747 _PyString_Resize(&RetVal, self->zst.next_out -
748 (Byte *)PyBytes_AS_STRING(RetVal));
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000749
Tim Peters977e5402001-10-17 03:57:20 +0000750 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000751 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000752 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000753}
754
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000755#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000756PyDoc_STRVAR(comp_copy__doc__,
757"copy() -- Return a copy of the compression object.");
758
759static PyObject *
760PyZlib_copy(compobject *self)
761{
762 compobject *retval = NULL;
763 int err;
764
765 retval = newcompobject(&Comptype);
766 if (!retval) return NULL;
767
768 /* Copy the zstream state
769 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
770 */
771 ENTER_ZLIB
772 err = deflateCopy(&retval->zst, &self->zst);
Martin Panter1e411c52016-07-23 04:22:09 +0000773 switch (err) {
774 case Z_OK:
Georg Brandl8d3342b2006-05-16 07:38:27 +0000775 break;
Martin Panter1e411c52016-07-23 04:22:09 +0000776 case Z_STREAM_ERROR:
Georg Brandl8d3342b2006-05-16 07:38:27 +0000777 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
778 goto error;
Martin Panter1e411c52016-07-23 04:22:09 +0000779 case Z_MEM_ERROR:
Georg Brandl8d3342b2006-05-16 07:38:27 +0000780 PyErr_SetString(PyExc_MemoryError,
781 "Can't allocate memory for compression object");
782 goto error;
783 default:
784 zlib_error(self->zst, err, "while copying compression object");
785 goto error;
786 }
787
Tim Peters402cc242006-05-17 01:30:11 +0000788 Py_INCREF(self->unused_data);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300789 Py_XSETREF(retval->unused_data, self->unused_data);
Tim Peters402cc242006-05-17 01:30:11 +0000790 Py_INCREF(self->unconsumed_tail);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300791 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000792
793 /* Mark it as being initialized */
794 retval->is_initialised = 1;
795
796 LEAVE_ZLIB
797 return (PyObject *)retval;
798
799error:
800 LEAVE_ZLIB
Tim Peters402cc242006-05-17 01:30:11 +0000801 Py_XDECREF(retval);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000802 return NULL;
803}
804
805PyDoc_STRVAR(decomp_copy__doc__,
806"copy() -- Return a copy of the decompression object.");
807
808static PyObject *
809PyZlib_uncopy(compobject *self)
810{
811 compobject *retval = NULL;
812 int err;
813
814 retval = newcompobject(&Decomptype);
815 if (!retval) return NULL;
816
817 /* Copy the zstream state
818 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
819 */
820 ENTER_ZLIB
821 err = inflateCopy(&retval->zst, &self->zst);
Martin Panter1e411c52016-07-23 04:22:09 +0000822 switch (err) {
823 case Z_OK:
Georg Brandl8d3342b2006-05-16 07:38:27 +0000824 break;
Martin Panter1e411c52016-07-23 04:22:09 +0000825 case Z_STREAM_ERROR:
Georg Brandl8d3342b2006-05-16 07:38:27 +0000826 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
827 goto error;
Martin Panter1e411c52016-07-23 04:22:09 +0000828 case Z_MEM_ERROR:
Georg Brandl8d3342b2006-05-16 07:38:27 +0000829 PyErr_SetString(PyExc_MemoryError,
830 "Can't allocate memory for decompression object");
831 goto error;
832 default:
833 zlib_error(self->zst, err, "while copying decompression object");
834 goto error;
835 }
836
Tim Peters402cc242006-05-17 01:30:11 +0000837 Py_INCREF(self->unused_data);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300838 Py_XSETREF(retval->unused_data, self->unused_data);
Tim Peters402cc242006-05-17 01:30:11 +0000839 Py_INCREF(self->unconsumed_tail);
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300840 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Georg Brandl8d3342b2006-05-16 07:38:27 +0000841
842 /* Mark it as being initialized */
843 retval->is_initialised = 1;
844
845 LEAVE_ZLIB
846 return (PyObject *)retval;
847
848error:
849 LEAVE_ZLIB
850 Py_XDECREF(retval);
851 return NULL;
852}
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000853#endif
Georg Brandl8d3342b2006-05-16 07:38:27 +0000854
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000855PyDoc_STRVAR(decomp_flush__doc__,
Georg Brandl22a9dc82006-04-01 07:39:41 +0000856"flush( [length] ) -- Return a string containing any remaining\n"
857"decompressed data. length, if given, is the initial size of the\n"
858"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000859"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000860"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000861
Guido van Rossumfb221561997-04-29 15:38:09 +0000862static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000863PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000864{
Martin Panter1e411c52016-07-23 04:22:09 +0000865 Py_ssize_t length = DEFAULTALLOC;
866 int err, flush;
867 PyObject *RetVal = NULL;
868 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +0000869
Martin Panter1e411c52016-07-23 04:22:09 +0000870 if (!PyArg_ParseTuple(args, "|n:flush", &length))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000871 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000872 if (length <= 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
874 return NULL;
Gregory P. Smith79e42a02008-04-09 00:25:17 +0000875 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000876
Jeremy Hylton9714f992001-10-16 21:19:45 +0000877 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000878
Nadeem Vawdaacfdfda2012-11-11 03:28:21 +0100879 self->zst.next_in = (Byte *)PyString_AS_STRING(self->unconsumed_tail);
Martin Panter1e411c52016-07-23 04:22:09 +0000880 ibuflen = PyString_GET_SIZE(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000881
Martin Panter1e411c52016-07-23 04:22:09 +0000882 do {
883 arrange_input_buffer(&self->zst, &ibuflen);
884 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000885
Martin Panter1e411c52016-07-23 04:22:09 +0000886 do {
887 length = arrange_output_buffer(&self->zst, &RetVal, length);
888 if (length < 0)
889 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000890
Martin Panter1e411c52016-07-23 04:22:09 +0000891 Py_BEGIN_ALLOW_THREADS
892 err = inflate(&self->zst, flush);
893 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000894
Martin Panter1e411c52016-07-23 04:22:09 +0000895 switch (err) {
896 case Z_OK: /* fall through */
897 case Z_BUF_ERROR: /* fall through */
898 case Z_STREAM_END:
899 break;
900 default:
901 goto save;
902 }
903
904 } while (self->zst.avail_out == 0);
905
906 } while (err != Z_STREAM_END && ibuflen != 0);
907
908 save:
909 if (save_unconsumed_input(self,
910 (Byte *)PyString_AS_STRING(self->unconsumed_tail),
911 PyString_GET_SIZE(self->unconsumed_tail), err) < 0)
912 goto abort;
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100913
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000914 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
915 various data structures. Note we should only get Z_STREAM_END when
916 flushmode is Z_FINISH */
917 if (err == Z_STREAM_END) {
Martin Panter1e411c52016-07-23 04:22:09 +0000918 err = inflateEnd(&self->zst);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000919 self->is_initialised = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000920 if (err != Z_OK) {
921 zlib_error(self->zst, err, "from inflateEnd()");
Martin Panter1e411c52016-07-23 04:22:09 +0000922 goto abort;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000923 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000924 }
Nadeem Vawda252f4dc2012-11-11 02:14:15 +0100925
Martin Panter1e411c52016-07-23 04:22:09 +0000926 _PyString_Resize(&RetVal, self->zst.next_out -
927 (Byte *)PyBytes_AS_STRING(RetVal));
928 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000929
Martin Panter1e411c52016-07-23 04:22:09 +0000930 abort:
931 Py_CLEAR(RetVal);
932 success:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000933 LEAVE_ZLIB
Martin Panter1e411c52016-07-23 04:22:09 +0000934 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000935}
936
937static PyMethodDef comp_methods[] =
938{
Tim Peters977e5402001-10-17 03:57:20 +0000939 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000940 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000941 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000942 comp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000943#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000944 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
945 comp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000946#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000947 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000948};
949
950static PyMethodDef Decomp_methods[] =
951{
Tim Peters977e5402001-10-17 03:57:20 +0000952 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000953 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000954 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000955 decomp_flush__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000956#ifdef HAVE_ZLIB_COPY
Georg Brandl8d3342b2006-05-16 07:38:27 +0000957 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
958 decomp_copy__doc__},
Neal Norwitz6e73aaa2006-06-12 03:33:09 +0000959#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000960 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000961};
962
963static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000964Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000965{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000966 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
967 internal data. */
968
969 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000970}
971
972static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000973Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000974{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000975 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000976
Jeremy Hylton9714f992001-10-16 21:19:45 +0000977 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000978
Tim Peters977e5402001-10-17 03:57:20 +0000979 if (strcmp(name, "unused_data") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000980 Py_INCREF(self->unused_data);
981 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000982 } else if (strcmp(name, "unconsumed_tail") == 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000983 Py_INCREF(self->unconsumed_tail);
984 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000985 } else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000986 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000987
Jeremy Hylton9714f992001-10-16 21:19:45 +0000988 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000989
Jeremy Hylton9714f992001-10-16 21:19:45 +0000990 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000991}
992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000993PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000994"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
995"\n"
996"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000997"a signed integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000998
Guido van Rossumfb221561997-04-29 15:38:09 +0000999static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001000PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001001{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +00001002 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001003 Byte *buf;
Martin Panter1e411c52016-07-23 04:22:09 +00001004 Py_ssize_t len;
1005 int signed_val;
Tim Peters977e5402001-10-17 03:57:20 +00001006
Gregory P. Smith1fa588e2008-03-25 07:31:28 +00001007 if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001008 return NULL;
Martin Panter1e411c52016-07-23 04:22:09 +00001009
1010 /* Avoid truncation of length for very large buffers. adler32() takes
1011 length as an unsigned int, which may be narrower than Py_ssize_t. */
1012 while ((size_t)len > UINT_MAX) {
1013 adler32val = adler32(adler32val, buf, UINT_MAX);
1014 buf += (size_t) UINT_MAX;
1015 len -= (size_t) UINT_MAX;
1016 }
Gregory P. Smithf48f9d32008-03-17 18:48:05 +00001017 /* In Python 2.x we return a signed integer regardless of native platform
1018 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
1019 * extended into a 64-bit long inside the integer object). 3.0 does the
1020 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
Martin Panter1e411c52016-07-23 04:22:09 +00001021 signed_val = adler32(adler32val, buf, (unsigned int)len);
Gregory P. Smithf48f9d32008-03-17 18:48:05 +00001022 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +00001023}
Tim Peters977e5402001-10-17 03:57:20 +00001024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001025PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001026"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1027"\n"
1028"An optional starting value can be specified. The returned checksum is\n"
Gregory P. Smithf48f9d32008-03-17 18:48:05 +00001029"a signed integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001030
1031static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001032PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001033{
Gregory P. Smith1fa588e2008-03-25 07:31:28 +00001034 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001035 Byte *buf;
Martin Panter1e411c52016-07-23 04:22:09 +00001036 Py_ssize_t len;
1037 int signed_val;
Gregory P. Smithf48f9d32008-03-17 18:48:05 +00001038
Gregory P. Smith1fa588e2008-03-25 07:31:28 +00001039 if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001040 return NULL;
Martin Panter1e411c52016-07-23 04:22:09 +00001041
1042 /* Avoid truncation of length for very large buffers. crc32() takes
1043 length as an unsigned int, which may be narrower than Py_ssize_t. */
1044 while ((size_t)len > UINT_MAX) {
1045 crc32val = crc32(crc32val, buf, UINT_MAX);
1046 buf += (size_t) UINT_MAX;
1047 len -= (size_t) UINT_MAX;
1048 }
Gregory P. Smithf48f9d32008-03-17 18:48:05 +00001049 /* In Python 2.x we return a signed integer regardless of native platform
1050 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
1051 * extended into a 64-bit long inside the integer object). 3.0 does the
1052 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
Martin Panter1e411c52016-07-23 04:22:09 +00001053 signed_val = crc32(crc32val, buf, (unsigned int)len);
Gregory P. Smithf48f9d32008-03-17 18:48:05 +00001054 return PyInt_FromLong(signed_val);
Guido van Rossumfb221561997-04-29 15:38:09 +00001055}
Tim Peters977e5402001-10-17 03:57:20 +00001056
Guido van Rossumfb221561997-04-29 15:38:09 +00001057
1058static PyMethodDef zlib_methods[] =
1059{
Tim Peters977e5402001-10-17 03:57:20 +00001060 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001061 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001062 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001063 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001064 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001065 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001066 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1067 crc32__doc__},
1068 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001069 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001070 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001071 decompressobj__doc__},
1072 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001073};
1074
Tim Peters0c322792002-07-17 16:49:03 +00001075static PyTypeObject Comptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001076 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001077 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001078 sizeof(compobject),
1079 0,
1080 (destructor)Comp_dealloc, /*tp_dealloc*/
1081 0, /*tp_print*/
1082 (getattrfunc)Comp_getattr, /*tp_getattr*/
1083 0, /*tp_setattr*/
1084 0, /*tp_compare*/
1085 0, /*tp_repr*/
1086 0, /*tp_as_number*/
1087 0, /*tp_as_sequence*/
1088 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001089};
1090
Tim Peters0c322792002-07-17 16:49:03 +00001091static PyTypeObject Decomptype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001092 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001093 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001094 sizeof(compobject),
1095 0,
1096 (destructor)Decomp_dealloc, /*tp_dealloc*/
1097 0, /*tp_print*/
1098 (getattrfunc)Decomp_getattr, /*tp_getattr*/
1099 0, /*tp_setattr*/
1100 0, /*tp_compare*/
1101 0, /*tp_repr*/
1102 0, /*tp_as_number*/
1103 0, /*tp_as_sequence*/
1104 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001105};
1106
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001107PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001108"The functions in this module allow compression and decompression using the\n"
1109"zlib library, which is based on GNU zip.\n"
1110"\n"
1111"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda99f9b8d2012-11-11 14:01:23 +01001112"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001113"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001114"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001115"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001116"decompressobj([wbits]) -- Return a decompressor object.\n"
1117"\n"
Martin Panter9c946bb2016-05-27 07:32:11 +00001118"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001119"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001120"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001121
Mark Hammond62b1ab12002-07-23 06:31:15 +00001122PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001123PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001124{
Fred Drake4baedc12002-04-01 14:53:37 +00001125 PyObject *m, *ver;
Christian Heimese93237d2007-12-19 02:37:44 +00001126 Py_TYPE(&Comptype) = &PyType_Type;
1127 Py_TYPE(&Decomptype) = &PyType_Type;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001128 m = Py_InitModule4("zlib", zlib_methods,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001129 zlib_module_documentation,
1130 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001131 if (m == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001132 return;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001133
Fred Drake4baedc12002-04-01 14:53:37 +00001134 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1135 if (ZlibError != NULL) {
1136 Py_INCREF(ZlibError);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001137 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001138 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001139 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1140 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1141 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1142 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1143 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1144 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1145 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1146 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1147 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001148
Jeremy Hylton9714f992001-10-16 21:19:45 +00001149 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1150 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1151 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1152 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001153
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001154 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001155 if (ver != NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001156 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001157
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001158 PyModule_AddStringConstant(m, "__version__", "1.0");
1159
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001160#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001161 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001162#endif /* WITH_THREAD */
Guido van Rossumfb221561997-04-29 15:38:09 +00001163}