blob: ba0e59ce0641d2ab6d3154b9b611b89d76aa8eef [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"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00008#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +00009#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
Antoine Pitrou31f30b12009-01-02 17:34:35 +000012 #include "pythread.h"
13 #define ENTER_ZLIB(obj) \
14 Py_BEGIN_ALLOW_THREADS; \
15 PyThread_acquire_lock((obj)->lock, 1); \
16 Py_END_ALLOW_THREADS;
17 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000019 #define ENTER_ZLIB(obj)
20 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000021#endif
22
Guido van Rossumfb221561997-04-29 15:38:09 +000023/* The following parameters are copied from zutil.h, version 0.95 */
24#define DEFLATED 8
25#if MAX_MEM_LEVEL >= 8
26# define DEF_MEM_LEVEL 8
27#else
28# define DEF_MEM_LEVEL MAX_MEM_LEVEL
29#endif
30#define DEF_WBITS MAX_WBITS
31
Guido van Rossumb729a1d1999-04-07 20:23:17 +000032/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
33#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000034
Jeremy Hylton938ace62002-07-17 16:30:39 +000035static PyTypeObject Comptype;
36static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000037
38static PyObject *ZlibError;
39
Tim Peters977e5402001-10-17 03:57:20 +000040typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000041{
Jeremy Hylton9714f992001-10-16 21:19:45 +000042 PyObject_HEAD
43 z_stream zst;
44 PyObject *unused_data;
45 PyObject *unconsumed_tail;
46 int is_initialised;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000047 #ifdef WITH_THREAD
48 PyThread_type_lock lock;
49 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000050} compobject;
51
Jeremy Hylton0965e082001-10-16 21:56:09 +000052static void
53zlib_error(z_stream zst, int err, char *msg)
54{
Antoine Pitrou96f212b2010-05-11 23:49:58 +000055 const char *zmsg = zst.msg;
56 if (zmsg == Z_NULL) {
57 switch (err) {
58 case Z_BUF_ERROR:
59 zmsg = "incomplete or truncated stream";
60 break;
61 case Z_STREAM_ERROR:
62 zmsg = "inconsistent stream state";
63 break;
64 case Z_DATA_ERROR:
65 zmsg = "invalid input data";
66 break;
67 }
68 }
69 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000070 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000071 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000072 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000073}
74
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000076"compressobj([level]) -- Return a compressor object.\n"
77"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000078"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000081"decompressobj([wbits]) -- Return a decompressor object.\n"
82"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000084
Guido van Rossumfb221561997-04-29 15:38:09 +000085static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000086newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000087{
Tim Peters977e5402001-10-17 03:57:20 +000088 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000089 self = PyObject_New(compobject, type);
90 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +000092 self->is_initialised = 0;
Gregory P. Smith693fc462008-09-06 20:13:06 +000093 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000094 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000095 Py_DECREF(self);
96 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +000097 }
Gregory P. Smith693fc462008-09-06 20:13:06 +000098 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 Py_DECREF(self);
101 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000103#ifdef WITH_THREAD
104 self->lock = PyThread_allocate_lock();
105#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000106 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000107}
108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000110"compress(string[, level]) -- Returned compressed string.\n"
111"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000113
Guido van Rossumfb221561997-04-29 15:38:09 +0000114static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000115PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000116{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000117 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000118 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200119 Byte *input, *output = NULL;
120 unsigned int length;
121 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 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 */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000125 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200127
128 if (pinput.len > UINT_MAX) {
129 PyErr_SetString(PyExc_OverflowError,
130 "Size does not fit in an unsigned int");
131 goto error;
132 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000133 input = pinput.buf;
134 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000135
Jeremy Hylton9714f992001-10-16 21:19:45 +0000136 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000137
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000138 output = (Byte*)malloc(zst.avail_out);
139 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000140 PyErr_SetString(PyExc_MemoryError,
141 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200142 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000143 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000144
Jeremy Hylton9714f992001-10-16 21:19:45 +0000145 /* Past the point of no return. From here on out, we need to make sure
146 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000147
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000148 zst.zalloc = (alloc_func)NULL;
149 zst.zfree = (free_func)Z_NULL;
150 zst.next_out = (Byte *)output;
151 zst.next_in = (Byte *)input;
152 zst.avail_in = length;
153 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000154
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000155 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000156 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000157 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000158 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000159 PyErr_SetString(PyExc_MemoryError,
160 "Out of memory while compressing data");
161 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000162 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 PyErr_SetString(ZlibError,
164 "Bad compression level");
165 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000166 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000167 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 zlib_error(zst, err, "while compressing data");
169 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000170 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000171
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000172 Py_BEGIN_ALLOW_THREADS;
173 err = deflate(&zst, Z_FINISH);
174 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000175
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000176 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000177 zlib_error(zst, err, "while compressing data");
178 deflateEnd(&zst);
179 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000180 }
Tim Peters977e5402001-10-17 03:57:20 +0000181
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 err=deflateEnd(&zst);
183 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000184 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000185 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000186 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000188
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000189 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000190 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000191 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000192
Jeremy Hylton9714f992001-10-16 21:19:45 +0000193 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000194}
195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000197"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
198"\n"
199"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000201
Guido van Rossumfb221561997-04-29 15:38:09 +0000202static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000203PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000204{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200205 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000206 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200208 unsigned int length;
209 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000210 int wsize=DEF_WBITS;
211 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000212 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000213
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000214 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000215 &pinput, &wsize, &r_strlen))
216 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200217
218 if (pinput.len > UINT_MAX) {
219 PyErr_SetString(PyExc_OverflowError,
220 "Size does not fit in an unsigned int");
221 goto error;
222 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000223 input = pinput.buf;
224 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000225
Jeremy Hylton9714f992001-10-16 21:19:45 +0000226 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000227 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000228
Jeremy Hylton9714f992001-10-16 21:19:45 +0000229 zst.avail_in = length;
230 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000231
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200232 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
233 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000234
Jeremy Hylton9714f992001-10-16 21:19:45 +0000235 zst.zalloc = (alloc_func)NULL;
236 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000237 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000238 zst.next_in = (Byte *)input;
239 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000240
Jeremy Hylton9714f992001-10-16 21:19:45 +0000241 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000242 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000243 break;
Tim Peters977e5402001-10-17 03:57:20 +0000244 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 PyErr_SetString(PyExc_MemoryError,
246 "Out of memory while decompressing data");
247 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000248 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000249 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000250 zlib_error(zst, err, "while preparing to decompress data");
251 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000252 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000253
Jeremy Hylton9714f992001-10-16 21:19:45 +0000254 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000255 Py_BEGIN_ALLOW_THREADS
256 err=inflate(&zst, Z_FINISH);
257 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000258
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 switch(err) {
260 case(Z_STREAM_END):
261 break;
262 case(Z_BUF_ERROR):
263 /*
264 * If there is at least 1 byte of room according to zst.avail_out
265 * and we get this error, assume that it means zlib cannot
266 * process the inflate call() due to an error in the data.
267 */
268 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000269 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000270 inflateEnd(&zst);
271 goto error;
272 }
273 /* fall through */
274 case(Z_OK):
275 /* need more memory */
276 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
277 inflateEnd(&zst);
278 goto error;
279 }
280 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000281 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000282 zst.avail_out = r_strlen;
283 r_strlen = r_strlen << 1;
284 break;
285 default:
286 inflateEnd(&zst);
287 zlib_error(zst, err, "while decompressing data");
288 goto error;
289 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000290 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000291
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000292 err = inflateEnd(&zst);
293 if (err != Z_OK) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000294 zlib_error(zst, err, "while finishing data decompression");
295 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000296 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000297
Gregory P. Smith693fc462008-09-06 20:13:06 +0000298 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000299 goto error;
300
Martin v. Löwis423be952008-08-13 15:53:07 +0000301 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000302 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000303
304 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000305 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000306 Py_XDECREF(result_str);
307 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000308}
309
310static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000311PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000312{
Jeremy Hylton499000002001-10-16 21:59:35 +0000313 compobject *self;
314 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
315 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000316
Jeremy Hylton499000002001-10-16 21:59:35 +0000317 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000318 &memLevel, &strategy))
319 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000320
Jeremy Hylton499000002001-10-16 21:59:35 +0000321 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000322 if (self==NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000324 self->zst.zalloc = (alloc_func)NULL;
325 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000326 self->zst.next_in = NULL;
327 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000328 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
329 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000330 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 self->is_initialised = 1;
332 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000333 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000334 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 PyErr_SetString(PyExc_MemoryError,
336 "Can't allocate memory for compression object");
337 return NULL;
338 case(Z_STREAM_ERROR):
339 Py_DECREF(self);
340 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
341 return NULL;
342 default:
343 zlib_error(self->zst, err, "while creating compression object");
344 Py_DECREF(self);
345 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000346 }
347}
348
349static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000350PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000351{
Jeremy Hylton499000002001-10-16 21:59:35 +0000352 int wbits=DEF_WBITS, err;
353 compobject *self;
354 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000355 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000356
357 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000358 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000359 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000360 self->zst.zalloc = (alloc_func)NULL;
361 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000362 self->zst.next_in = NULL;
363 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000364 err = inflateInit2(&self->zst, wbits);
365 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000366 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 self->is_initialised = 1;
368 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000369 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000370 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
372 return NULL;
373 case (Z_MEM_ERROR):
374 Py_DECREF(self);
375 PyErr_SetString(PyExc_MemoryError,
376 "Can't allocate memory for decompression object");
377 return NULL;
378 default:
379 zlib_error(self->zst, err, "while creating decompression object");
380 Py_DECREF(self);
381 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000382 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000383}
384
385static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000386Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000387{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000388#ifdef WITH_THREAD
389 PyThread_free_lock(self->lock);
390#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000391 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000392 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000393 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000394}
395
396static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000397Comp_dealloc(compobject *self)
398{
399 if (self->is_initialised)
400 deflateEnd(&self->zst);
401 Dealloc(self);
402}
403
404static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000405Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000406{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000407 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000408 inflateEnd(&self->zst);
409 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000410}
411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000413"compress(data) -- Return a string containing data compressed.\n"
414"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000415"After calling this function, some of the input data may still\n"
416"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000418
419
Guido van Rossumfb221561997-04-29 15:38:09 +0000420static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000421PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000422{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200423 int err;
424 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000425 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200426 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000427 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000428 Byte *input;
429 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000430
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000431 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000432 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200433 if (pinput.len > UINT_MAX) {
434 PyErr_SetString(PyExc_OverflowError,
435 "Size does not fit in an unsigned int");
436 goto error_outer;
437 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000438 input = pinput.buf;
439 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000440
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200441 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
442 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000443
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000444 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000445
Jeremy Hylton9714f992001-10-16 21:19:45 +0000446 start_total_out = self->zst.total_out;
447 self->zst.avail_in = inplen;
448 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000449 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000450 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000451
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000452 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000453 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000454 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000455
Jeremy Hylton9714f992001-10-16 21:19:45 +0000456 /* while Z_OK and the output buffer is full, there might be more output,
457 so extend the output buffer and try again */
458 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000460 Py_DECREF(RetVal);
461 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000463 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000465 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000466 self->zst.avail_out = length;
467 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000468
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000469 Py_BEGIN_ALLOW_THREADS
470 err = deflate(&(self->zst), Z_NO_FLUSH);
471 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000472 }
Tim Peters977e5402001-10-17 03:57:20 +0000473 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000474 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000475 condition.
476 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000477
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000478 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000479 zlib_error(self->zst, err, "while compressing");
480 Py_DECREF(RetVal);
481 RetVal = NULL;
482 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000483 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000484 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000485 Py_DECREF(RetVal);
486 RetVal = NULL;
487 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000488
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000489 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000490 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200491 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000492 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000493 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000494}
495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000496PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000497"decompress(data, max_length) -- Return a string containing the decompressed\n"
498"version of the data.\n"
499"\n"
500"After calling this function, some of the input data may still be stored in\n"
501"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000502"Call the flush() method to clear these buffers.\n"
503"If the max_length parameter is specified then the return value will be\n"
504"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000505"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000506
Guido van Rossumfb221561997-04-29 15:38:09 +0000507static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000508PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000509{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200510 int err, max_length = 0;
511 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000512 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200513 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000514 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000515 Byte *input;
516 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000517
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000518 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000519 &max_length))
520 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200521 if (pinput.len > UINT_MAX) {
522 PyErr_SetString(PyExc_OverflowError,
523 "Size does not fit in an unsigned int");
524 goto error_outer;
525 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000526 input = pinput.buf;
527 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000528 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000529 PyErr_SetString(PyExc_ValueError,
530 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200531 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000532 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000533
Jeremy Hylton9714f992001-10-16 21:19:45 +0000534 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000535 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000536 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200537 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
538 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000539
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000540 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000541
Jeremy Hylton9714f992001-10-16 21:19:45 +0000542 start_total_out = self->zst.total_out;
543 self->zst.avail_in = inplen;
544 self->zst.next_in = input;
545 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000546 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000547
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000548 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000549 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000550 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000551
Jeremy Hylton9714f992001-10-16 21:19:45 +0000552 /* While Z_OK and the output buffer is full, there might be more output.
553 So extend the output buffer and try again.
554 */
Tim Peters977e5402001-10-17 03:57:20 +0000555 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 /* If max_length set, don't continue decompressing if we've already
557 reached the limit.
558 */
559 if (max_length && length >= max_length)
560 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000562 /* otherwise, ... */
563 old_length = length;
564 length = length << 1;
565 if (max_length && length > max_length)
566 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000567
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000568 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000569 Py_DECREF(RetVal);
570 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000571 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000572 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000573 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000574 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000576
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 Py_BEGIN_ALLOW_THREADS
578 err = inflate(&(self->zst), Z_SYNC_FLUSH);
579 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000580 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000581
Jeremy Hylton9714f992001-10-16 21:19:45 +0000582 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200583 /* Not all of the compressed data could be accommodated in a buffer of
584 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000585 Py_DECREF(self->unconsumed_tail);
586 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
587 self->zst.avail_in);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200588 }
589 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
590 /* All of the compressed data was consumed. Clear unconsumed_tail. */
591 Py_DECREF(self->unconsumed_tail);
592 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
593 }
594 if (self->unconsumed_tail == NULL) {
595 Py_DECREF(RetVal);
596 RetVal = NULL;
597 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000598 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000599
Tim Peters977e5402001-10-17 03:57:20 +0000600 /* The end of the compressed data has been reached, so set the
601 unused_data attribute to a string containing the remainder of the
602 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000603 inflateEnd, but the old behaviour of only calling it on flush() is
604 preserved.
605 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000606 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607 Py_XDECREF(self->unused_data); /* Free original empty string */
608 self->unused_data = PyBytes_FromStringAndSize(
609 (char *)self->zst.next_in, self->zst.avail_in);
610 if (self->unused_data == NULL) {
611 Py_DECREF(RetVal);
612 goto error;
613 }
614 /* We will only get Z_BUF_ERROR if the output buffer was full
615 but there wasn't more output when we tried again, so it is
616 not an error condition.
617 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000618 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000619 zlib_error(self->zst, err, "while decompressing");
620 Py_DECREF(RetVal);
621 RetVal = NULL;
622 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000623 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000624
Gregory P. Smith693fc462008-09-06 20:13:06 +0000625 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000626 Py_DECREF(RetVal);
627 RetVal = NULL;
628 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000629
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000630 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000631 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200632 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000633 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000634 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000635}
636
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000637PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000638"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000639"\n"
640"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000641"default value used when mode is not specified is Z_FINISH.\n"
642"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000644
Guido van Rossumfb221561997-04-29 15:38:09 +0000645static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000646PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000647{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000648 int err, length = DEFAULTALLOC;
649 PyObject *RetVal;
650 int flushmode = Z_FINISH;
651 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000652
Jeremy Hylton9714f992001-10-16 21:19:45 +0000653 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000654 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000655
Jeremy Hylton9714f992001-10-16 21:19:45 +0000656 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
657 doing any work at all; just return an empty string. */
658 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000659 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000660 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000661
Gregory P. Smith693fc462008-09-06 20:13:06 +0000662 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000663 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000664
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000665 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000666
Jeremy Hylton9714f992001-10-16 21:19:45 +0000667 start_total_out = self->zst.total_out;
668 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000669 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000670 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000671
672 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000673 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000674 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000675
Jeremy Hylton9714f992001-10-16 21:19:45 +0000676 /* while Z_OK and the output buffer is full, there might be more output,
677 so extend the output buffer and try again */
678 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000679 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000680 Py_DECREF(RetVal);
681 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000682 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000683 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000684 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000685 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000686 self->zst.avail_out = length;
687 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000688
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000689 Py_BEGIN_ALLOW_THREADS
690 err = deflate(&(self->zst), flushmode);
691 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000692 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000693
Jeremy Hylton9714f992001-10-16 21:19:45 +0000694 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000695 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000696 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000697 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000698 err = deflateEnd(&(self->zst));
699 if (err != Z_OK) {
700 zlib_error(self->zst, err, "from deflateEnd()");
701 Py_DECREF(RetVal);
702 RetVal = NULL;
703 goto error;
704 }
705 else
706 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000707
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000708 /* We will only get Z_BUF_ERROR if the output buffer was full
709 but there wasn't more output when we tried again, so it is
710 not an error condition.
711 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000712 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000713 zlib_error(self->zst, err, "while flushing");
714 Py_DECREF(RetVal);
715 RetVal = NULL;
716 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000717 }
Tim Peters977e5402001-10-17 03:57:20 +0000718
Gregory P. Smith693fc462008-09-06 20:13:06 +0000719 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000720 Py_DECREF(RetVal);
721 RetVal = NULL;
722 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000723
Tim Peters977e5402001-10-17 03:57:20 +0000724 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000725 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000726
727 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000728}
729
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000730#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000731PyDoc_STRVAR(comp_copy__doc__,
732"copy() -- Return a copy of the compression object.");
733
734static PyObject *
735PyZlib_copy(compobject *self)
736{
737 compobject *retval = NULL;
738 int err;
739
740 retval = newcompobject(&Comptype);
741 if (!retval) return NULL;
742
743 /* Copy the zstream state
744 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
745 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000746 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000747 err = deflateCopy(&retval->zst, &self->zst);
748 switch(err) {
749 case(Z_OK):
750 break;
751 case(Z_STREAM_ERROR):
752 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
753 goto error;
754 case(Z_MEM_ERROR):
755 PyErr_SetString(PyExc_MemoryError,
756 "Can't allocate memory for compression object");
757 goto error;
758 default:
759 zlib_error(self->zst, err, "while copying compression object");
760 goto error;
761 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000762 Py_INCREF(self->unused_data);
763 Py_INCREF(self->unconsumed_tail);
764 Py_XDECREF(retval->unused_data);
765 Py_XDECREF(retval->unconsumed_tail);
766 retval->unused_data = self->unused_data;
767 retval->unconsumed_tail = self->unconsumed_tail;
768
769 /* Mark it as being initialized */
770 retval->is_initialised = 1;
771
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000772 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000773 return (PyObject *)retval;
774
775error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000776 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777 Py_XDECREF(retval);
778 return NULL;
779}
780
781PyDoc_STRVAR(decomp_copy__doc__,
782"copy() -- Return a copy of the decompression object.");
783
784static PyObject *
785PyZlib_uncopy(compobject *self)
786{
787 compobject *retval = NULL;
788 int err;
789
790 retval = newcompobject(&Decomptype);
791 if (!retval) return NULL;
792
793 /* Copy the zstream state
794 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
795 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000796 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000797 err = inflateCopy(&retval->zst, &self->zst);
798 switch(err) {
799 case(Z_OK):
800 break;
801 case(Z_STREAM_ERROR):
802 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
803 goto error;
804 case(Z_MEM_ERROR):
805 PyErr_SetString(PyExc_MemoryError,
806 "Can't allocate memory for decompression object");
807 goto error;
808 default:
809 zlib_error(self->zst, err, "while copying decompression object");
810 goto error;
811 }
812
813 Py_INCREF(self->unused_data);
814 Py_INCREF(self->unconsumed_tail);
815 Py_XDECREF(retval->unused_data);
816 Py_XDECREF(retval->unconsumed_tail);
817 retval->unused_data = self->unused_data;
818 retval->unconsumed_tail = self->unconsumed_tail;
819
820 /* Mark it as being initialized */
821 retval->is_initialised = 1;
822
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000823 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824 return (PyObject *)retval;
825
826error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000827 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828 Py_XDECREF(retval);
829 return NULL;
830}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000831#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000833PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834"flush( [length] ) -- Return a string containing any remaining\n"
835"decompressed data. length, if given, is the initial size of the\n"
836"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000837"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000839
Guido van Rossumfb221561997-04-29 15:38:09 +0000840static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000841PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000842{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000843 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000844 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000845 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000846
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000847 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000848 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000849 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000850 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
851 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000852 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000853 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000854 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000855
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000856
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000857 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000858
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000859 start_total_out = self->zst.total_out;
860 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000861 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000862
863 Py_BEGIN_ALLOW_THREADS
864 err = inflate(&(self->zst), Z_FINISH);
865 Py_END_ALLOW_THREADS
866
867 /* while Z_OK and the output buffer is full, there might be more output,
868 so extend the output buffer and try again */
869 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000870 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000871 Py_DECREF(retval);
872 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000873 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000874 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000875 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
876 self->zst.avail_out = length;
877 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000878
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 Py_BEGIN_ALLOW_THREADS
880 err = inflate(&(self->zst), Z_FINISH);
881 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000882 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000883
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000884 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
885 various data structures. Note we should only get Z_STREAM_END when
886 flushmode is Z_FINISH */
887 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000889 self->is_initialised = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 if (err != Z_OK) {
891 zlib_error(self->zst, err, "from inflateEnd()");
892 Py_DECREF(retval);
893 retval = NULL;
894 goto error;
895 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000896 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000897 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000898 Py_DECREF(retval);
899 retval = NULL;
900 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000901
902error:
903
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000904 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000905
Jeremy Hylton9714f992001-10-16 21:19:45 +0000906 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000907}
908
909static PyMethodDef comp_methods[] =
910{
Tim Peters977e5402001-10-17 03:57:20 +0000911 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000912 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000913 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000914 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000915#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000916 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
917 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000918#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000919 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000920};
921
922static PyMethodDef Decomp_methods[] =
923{
Tim Peters977e5402001-10-17 03:57:20 +0000924 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000925 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000926 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000927 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000928#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000929 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
930 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000931#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000932 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000933};
934
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000935#define COMP_OFF(x) offsetof(compobject, x)
936static PyMemberDef Decomp_members[] = {
937 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
938 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
939 {NULL},
940};
Guido van Rossumfb221561997-04-29 15:38:09 +0000941
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000942PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000943"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
944"\n"
945"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000946"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000947
Guido van Rossumfb221561997-04-29 15:38:09 +0000948static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000949PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000950{
Christian Heimescc47b052008-03-25 14:56:36 +0000951 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000952 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000953
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000954 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
955 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000956 /* Releasing the GIL for very small buffers is inefficient
957 and may lower performance */
958 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000959 unsigned char *buf = pbuf.buf;
960 Py_ssize_t len = pbuf.len;
961
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000962 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000963 /* Avoid truncation of length for very large buffers. adler32() takes
964 length as an unsigned int, which may be narrower than Py_ssize_t. */
965 while (len > (size_t) UINT_MAX) {
966 adler32val = adler32(adler32val, buf, UINT_MAX);
967 buf += (size_t) UINT_MAX;
968 len -= (size_t) UINT_MAX;
969 }
970 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000971 Py_END_ALLOW_THREADS
972 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000973 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000974 }
975 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000976 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000977}
Tim Peters977e5402001-10-17 03:57:20 +0000978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000980"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
981"\n"
982"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000983"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000984
985static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000986PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000987{
Christian Heimescc47b052008-03-25 14:56:36 +0000988 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000989 Py_buffer pbuf;
990 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000991
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000992 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000993 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000994 /* Releasing the GIL for very small buffers is inefficient
995 and may lower performance */
996 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000997 unsigned char *buf = pbuf.buf;
998 Py_ssize_t len = pbuf.len;
999
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001000 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001001 /* Avoid truncation of length for very large buffers. crc32() takes
1002 length as an unsigned int, which may be narrower than Py_ssize_t. */
1003 while (len > (size_t) UINT_MAX) {
1004 crc32val = crc32(crc32val, buf, UINT_MAX);
1005 buf += (size_t) UINT_MAX;
1006 len -= (size_t) UINT_MAX;
1007 }
1008 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001009 Py_END_ALLOW_THREADS
1010 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001011 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001012 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001013 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001014 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001015}
Tim Peters977e5402001-10-17 03:57:20 +00001016
Guido van Rossumfb221561997-04-29 15:38:09 +00001017
1018static PyMethodDef zlib_methods[] =
1019{
Tim Peters977e5402001-10-17 03:57:20 +00001020 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001021 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001022 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001023 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001024 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001025 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001026 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1027 crc32__doc__},
1028 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001029 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001030 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001031 decompressobj__doc__},
1032 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001033};
1034
Tim Peters0c322792002-07-17 16:49:03 +00001035static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001036 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001037 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001038 sizeof(compobject),
1039 0,
1040 (destructor)Comp_dealloc, /*tp_dealloc*/
1041 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001042 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001043 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001044 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001045 0, /*tp_repr*/
1046 0, /*tp_as_number*/
1047 0, /*tp_as_sequence*/
1048 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001049 0, /*tp_hash*/
1050 0, /*tp_call*/
1051 0, /*tp_str*/
1052 0, /*tp_getattro*/
1053 0, /*tp_setattro*/
1054 0, /*tp_as_buffer*/
1055 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1056 0, /*tp_doc*/
1057 0, /*tp_traverse*/
1058 0, /*tp_clear*/
1059 0, /*tp_richcompare*/
1060 0, /*tp_weaklistoffset*/
1061 0, /*tp_iter*/
1062 0, /*tp_iternext*/
1063 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001064};
1065
Tim Peters0c322792002-07-17 16:49:03 +00001066static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001067 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001068 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001069 sizeof(compobject),
1070 0,
1071 (destructor)Decomp_dealloc, /*tp_dealloc*/
1072 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001073 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001074 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001075 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001076 0, /*tp_repr*/
1077 0, /*tp_as_number*/
1078 0, /*tp_as_sequence*/
1079 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001080 0, /*tp_hash*/
1081 0, /*tp_call*/
1082 0, /*tp_str*/
1083 0, /*tp_getattro*/
1084 0, /*tp_setattro*/
1085 0, /*tp_as_buffer*/
1086 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1087 0, /*tp_doc*/
1088 0, /*tp_traverse*/
1089 0, /*tp_clear*/
1090 0, /*tp_richcompare*/
1091 0, /*tp_weaklistoffset*/
1092 0, /*tp_iter*/
1093 0, /*tp_iternext*/
1094 Decomp_methods, /*tp_methods*/
1095 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001096};
1097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001098PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001099"The functions in this module allow compression and decompression using the\n"
1100"zlib library, which is based on GNU zip.\n"
1101"\n"
1102"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1103"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001104"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001105"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001106"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001107"decompressobj([wbits]) -- Return a decompressor object.\n"
1108"\n"
1109"'wbits' is window buffer size.\n"
1110"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001111"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001112
Martin v. Löwis1a214512008-06-11 05:26:20 +00001113static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001114 PyModuleDef_HEAD_INIT,
1115 "zlib",
1116 zlib_module_documentation,
1117 -1,
1118 zlib_methods,
1119 NULL,
1120 NULL,
1121 NULL,
1122 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001123};
1124
Mark Hammond62b1ab12002-07-23 06:31:15 +00001125PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001126PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001127{
Fred Drake4baedc12002-04-01 14:53:37 +00001128 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001129 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001130 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001131 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001132 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001133 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001134 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001135 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001136
Fred Drake4baedc12002-04-01 14:53:37 +00001137 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1138 if (ZlibError != NULL) {
1139 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001140 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001141 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001142 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1143 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1144 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1145 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1146 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1147 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1148 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1149 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1150 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001151
Jeremy Hylton9714f992001-10-16 21:19:45 +00001152 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1153 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1154 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1155 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001156
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001157 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001158 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001159 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001160
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001161 PyModule_AddStringConstant(m, "__version__", "1.0");
1162
Martin v. Löwis1a214512008-06-11 05:26:20 +00001163 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001164}