blob: 54ab9a149959ff12a19d46529931a281c70c362e [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;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 Byte *input, *output;
120 int length, level=Z_DEFAULT_COMPRESSION, err;
121 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000122
Jeremy Hylton9714f992001-10-16 21:19:45 +0000123 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000124 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000125 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000126 input = pinput.buf;
127 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000128
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000130
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000131 output = (Byte*)malloc(zst.avail_out);
132 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000133 PyBuffer_Release(&pinput);
134 PyErr_SetString(PyExc_MemoryError,
135 "Can't allocate memory to compress data");
136 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000137 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000138
Jeremy Hylton9714f992001-10-16 21:19:45 +0000139 /* Past the point of no return. From here on out, we need to make sure
140 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000141
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000142 zst.zalloc = (alloc_func)NULL;
143 zst.zfree = (free_func)Z_NULL;
144 zst.next_out = (Byte *)output;
145 zst.next_in = (Byte *)input;
146 zst.avail_in = length;
147 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000148
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000149 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000150 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000151 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000152 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000153 PyErr_SetString(PyExc_MemoryError,
154 "Out of memory while compressing data");
155 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000156 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000157 PyErr_SetString(ZlibError,
158 "Bad compression level");
159 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000160 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000161 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000162 zlib_error(zst, err, "while compressing data");
163 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000164 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000165
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000166 Py_BEGIN_ALLOW_THREADS;
167 err = deflate(&zst, Z_FINISH);
168 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000169
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000170 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 zlib_error(zst, err, "while compressing data");
172 deflateEnd(&zst);
173 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000174 }
Tim Peters977e5402001-10-17 03:57:20 +0000175
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000176 err=deflateEnd(&zst);
177 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000178 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000179 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000180 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000181 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000182
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000183 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000184 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000185 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000186
Jeremy Hylton9714f992001-10-16 21:19:45 +0000187 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000188}
189
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000190PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000191"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
192"\n"
193"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000195
Guido van Rossumfb221561997-04-29 15:38:09 +0000196static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000197PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000198{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000199 PyObject *result_str;
Martin v. Löwis423be952008-08-13 15:53:07 +0000200 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000201 Byte *input;
202 int length, err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000203 int wsize=DEF_WBITS;
204 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000205 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000206
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000207 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000208 &pinput, &wsize, &r_strlen))
209 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000210 input = pinput.buf;
211 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000212
Jeremy Hylton9714f992001-10-16 21:19:45 +0000213 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000215
Jeremy Hylton9714f992001-10-16 21:19:45 +0000216 zst.avail_in = length;
217 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000218
Gregory P. Smith693fc462008-09-06 20:13:06 +0000219 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 PyBuffer_Release(&pinput);
221 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000222 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000223
Jeremy Hylton9714f992001-10-16 21:19:45 +0000224 zst.zalloc = (alloc_func)NULL;
225 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000226 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000227 zst.next_in = (Byte *)input;
228 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000229
Jeremy Hylton9714f992001-10-16 21:19:45 +0000230 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000231 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232 break;
Tim Peters977e5402001-10-17 03:57:20 +0000233 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 PyErr_SetString(PyExc_MemoryError,
235 "Out of memory while decompressing data");
236 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000237 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000238 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 zlib_error(zst, err, "while preparing to decompress data");
240 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000241 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000242
Jeremy Hylton9714f992001-10-16 21:19:45 +0000243 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000244 Py_BEGIN_ALLOW_THREADS
245 err=inflate(&zst, Z_FINISH);
246 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000247
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000248 switch(err) {
249 case(Z_STREAM_END):
250 break;
251 case(Z_BUF_ERROR):
252 /*
253 * If there is at least 1 byte of room according to zst.avail_out
254 * and we get this error, assume that it means zlib cannot
255 * process the inflate call() due to an error in the data.
256 */
257 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000258 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 inflateEnd(&zst);
260 goto error;
261 }
262 /* fall through */
263 case(Z_OK):
264 /* need more memory */
265 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
266 inflateEnd(&zst);
267 goto error;
268 }
269 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000270 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000271 zst.avail_out = r_strlen;
272 r_strlen = r_strlen << 1;
273 break;
274 default:
275 inflateEnd(&zst);
276 zlib_error(zst, err, "while decompressing data");
277 goto error;
278 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000279 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000280
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000281 err = inflateEnd(&zst);
282 if (err != Z_OK) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000283 zlib_error(zst, err, "while finishing data decompression");
284 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000285 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000286
Gregory P. Smith693fc462008-09-06 20:13:06 +0000287 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000288 goto error;
289
Martin v. Löwis423be952008-08-13 15:53:07 +0000290 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000291 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000292
293 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000294 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000295 Py_XDECREF(result_str);
296 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000297}
298
299static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000300PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000301{
Jeremy Hylton499000002001-10-16 21:59:35 +0000302 compobject *self;
303 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
304 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000305
Jeremy Hylton499000002001-10-16 21:59:35 +0000306 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000307 &memLevel, &strategy))
308 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000309
Jeremy Hylton499000002001-10-16 21:59:35 +0000310 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000311 if (self==NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000313 self->zst.zalloc = (alloc_func)NULL;
314 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000315 self->zst.next_in = NULL;
316 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000317 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
318 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000319 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000320 self->is_initialised = 1;
321 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000322 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000323 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000324 PyErr_SetString(PyExc_MemoryError,
325 "Can't allocate memory for compression object");
326 return NULL;
327 case(Z_STREAM_ERROR):
328 Py_DECREF(self);
329 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
330 return NULL;
331 default:
332 zlib_error(self->zst, err, "while creating compression object");
333 Py_DECREF(self);
334 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000335 }
336}
337
338static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000339PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000340{
Jeremy Hylton499000002001-10-16 21:59:35 +0000341 int wbits=DEF_WBITS, err;
342 compobject *self;
343 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000344 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000345
346 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000347 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000348 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000349 self->zst.zalloc = (alloc_func)NULL;
350 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000351 self->zst.next_in = NULL;
352 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000353 err = inflateInit2(&self->zst, wbits);
354 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000355 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000356 self->is_initialised = 1;
357 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000358 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000359 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000360 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
361 return NULL;
362 case (Z_MEM_ERROR):
363 Py_DECREF(self);
364 PyErr_SetString(PyExc_MemoryError,
365 "Can't allocate memory for decompression object");
366 return NULL;
367 default:
368 zlib_error(self->zst, err, "while creating decompression object");
369 Py_DECREF(self);
370 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000371 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000372}
373
374static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000375Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000376{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000377#ifdef WITH_THREAD
378 PyThread_free_lock(self->lock);
379#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000380 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000381 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000382 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000383}
384
385static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000386Comp_dealloc(compobject *self)
387{
388 if (self->is_initialised)
389 deflateEnd(&self->zst);
390 Dealloc(self);
391}
392
393static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000394Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000395{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000396 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000397 inflateEnd(&self->zst);
398 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000399}
400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000401PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000402"compress(data) -- Return a string containing data compressed.\n"
403"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000404"After calling this function, some of the input data may still\n"
405"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000406"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000407
408
Guido van Rossumfb221561997-04-29 15:38:09 +0000409static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000410PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000411{
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000412 int err, inplen;
413 Py_ssize_t length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000414 PyObject *RetVal;
Martin v. Löwis423be952008-08-13 15:53:07 +0000415 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000416 Byte *input;
417 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000418
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000419 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000420 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000421 input = pinput.buf;
422 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000423
Gregory P. Smith693fc462008-09-06 20:13:06 +0000424 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000425 PyBuffer_Release(&pinput);
426 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000427 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000428
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000429 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000430
Jeremy Hylton9714f992001-10-16 21:19:45 +0000431 start_total_out = self->zst.total_out;
432 self->zst.avail_in = inplen;
433 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000434 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000435 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000436
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000437 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000438 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000439 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000440
Jeremy Hylton9714f992001-10-16 21:19:45 +0000441 /* while Z_OK and the output buffer is full, there might be more output,
442 so extend the output buffer and try again */
443 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000444 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000445 Py_DECREF(RetVal);
446 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000447 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000448 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000449 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000450 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000451 self->zst.avail_out = length;
452 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000453
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000454 Py_BEGIN_ALLOW_THREADS
455 err = deflate(&(self->zst), Z_NO_FLUSH);
456 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000457 }
Tim Peters977e5402001-10-17 03:57:20 +0000458 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000459 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000460 condition.
461 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000462
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000463 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 zlib_error(self->zst, err, "while compressing");
465 Py_DECREF(RetVal);
466 RetVal = NULL;
467 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000468 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000469 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000470 Py_DECREF(RetVal);
471 RetVal = NULL;
472 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000473
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000474 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000475 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000476 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000477 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000478}
479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000481"decompress(data, max_length) -- Return a string containing the decompressed\n"
482"version of the data.\n"
483"\n"
484"After calling this function, some of the input data may still be stored in\n"
485"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000486"Call the flush() method to clear these buffers.\n"
487"If the max_length parameter is specified then the return value will be\n"
488"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000489"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000490
Guido van Rossumfb221561997-04-29 15:38:09 +0000491static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000492PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000493{
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000494 int err, inplen, max_length = 0;
495 Py_ssize_t old_length, length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000496 PyObject *RetVal;
Martin v. Löwis423be952008-08-13 15:53:07 +0000497 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000498 Byte *input;
499 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000500
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000501 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000502 &max_length))
503 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000504 input = pinput.buf;
505 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000506 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 PyBuffer_Release(&pinput);
508 PyErr_SetString(PyExc_ValueError,
509 "max_length must be greater than zero");
510 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000511 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000512
Jeremy Hylton9714f992001-10-16 21:19:45 +0000513 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000514 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 length = max_length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000516 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000517 PyBuffer_Release(&pinput);
518 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000519 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000520
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000521 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000522
Jeremy Hylton9714f992001-10-16 21:19:45 +0000523 start_total_out = self->zst.total_out;
524 self->zst.avail_in = inplen;
525 self->zst.next_in = input;
526 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000527 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000528
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000529 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000530 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000531 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000532
Jeremy Hylton9714f992001-10-16 21:19:45 +0000533 /* While Z_OK and the output buffer is full, there might be more output.
534 So extend the output buffer and try again.
535 */
Tim Peters977e5402001-10-17 03:57:20 +0000536 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000537 /* If max_length set, don't continue decompressing if we've already
538 reached the limit.
539 */
540 if (max_length && length >= max_length)
541 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000542
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000543 /* otherwise, ... */
544 old_length = length;
545 length = length << 1;
546 if (max_length && length > max_length)
547 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000548
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000549 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000550 Py_DECREF(RetVal);
551 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000552 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000553 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000555 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000557
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000558 Py_BEGIN_ALLOW_THREADS
559 err = inflate(&(self->zst), Z_SYNC_FLUSH);
560 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000561 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000562
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000563 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000564 of specified size. Return the unconsumed tail in an attribute.*/
565 if(max_length) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000566 Py_DECREF(self->unconsumed_tail);
567 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
568 self->zst.avail_in);
569 if(!self->unconsumed_tail) {
570 Py_DECREF(RetVal);
571 RetVal = NULL;
572 goto error;
573 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000574 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000575
Tim Peters977e5402001-10-17 03:57:20 +0000576 /* The end of the compressed data has been reached, so set the
577 unused_data attribute to a string containing the remainder of the
578 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000579 inflateEnd, but the old behaviour of only calling it on flush() is
580 preserved.
581 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000582 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 Py_XDECREF(self->unused_data); /* Free original empty string */
584 self->unused_data = PyBytes_FromStringAndSize(
585 (char *)self->zst.next_in, self->zst.avail_in);
586 if (self->unused_data == NULL) {
587 Py_DECREF(RetVal);
588 goto error;
589 }
590 /* We will only get Z_BUF_ERROR if the output buffer was full
591 but there wasn't more output when we tried again, so it is
592 not an error condition.
593 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000594 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 zlib_error(self->zst, err, "while decompressing");
596 Py_DECREF(RetVal);
597 RetVal = NULL;
598 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000599 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000600
Gregory P. Smith693fc462008-09-06 20:13:06 +0000601 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000602 Py_DECREF(RetVal);
603 RetVal = NULL;
604 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000605
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000606 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000607 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000608 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000609 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000610}
611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000612PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000613"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000614"\n"
615"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000616"default value used when mode is not specified is Z_FINISH.\n"
617"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000618"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000619
Guido van Rossumfb221561997-04-29 15:38:09 +0000620static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000621PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000622{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000623 int err, length = DEFAULTALLOC;
624 PyObject *RetVal;
625 int flushmode = Z_FINISH;
626 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000627
Jeremy Hylton9714f992001-10-16 21:19:45 +0000628 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000629 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000630
Jeremy Hylton9714f992001-10-16 21:19:45 +0000631 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
632 doing any work at all; just return an empty string. */
633 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000634 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000635 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000636
Gregory P. Smith693fc462008-09-06 20:13:06 +0000637 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000638 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000639
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000640 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000641
Jeremy Hylton9714f992001-10-16 21:19:45 +0000642 start_total_out = self->zst.total_out;
643 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000644 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000645 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000646
647 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000648 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000649 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000650
Jeremy Hylton9714f992001-10-16 21:19:45 +0000651 /* while Z_OK and the output buffer is full, there might be more output,
652 so extend the output buffer and try again */
653 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000654 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000655 Py_DECREF(RetVal);
656 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000657 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000658 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000659 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000660 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000661 self->zst.avail_out = length;
662 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000663
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000664 Py_BEGIN_ALLOW_THREADS
665 err = deflate(&(self->zst), flushmode);
666 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000667 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000668
Jeremy Hylton9714f992001-10-16 21:19:45 +0000669 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000670 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000671 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000672 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000673 err = deflateEnd(&(self->zst));
674 if (err != Z_OK) {
675 zlib_error(self->zst, err, "from deflateEnd()");
676 Py_DECREF(RetVal);
677 RetVal = NULL;
678 goto error;
679 }
680 else
681 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000682
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000683 /* We will only get Z_BUF_ERROR if the output buffer was full
684 but there wasn't more output when we tried again, so it is
685 not an error condition.
686 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000687 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000688 zlib_error(self->zst, err, "while flushing");
689 Py_DECREF(RetVal);
690 RetVal = NULL;
691 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000692 }
Tim Peters977e5402001-10-17 03:57:20 +0000693
Gregory P. Smith693fc462008-09-06 20:13:06 +0000694 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000695 Py_DECREF(RetVal);
696 RetVal = NULL;
697 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000698
Tim Peters977e5402001-10-17 03:57:20 +0000699 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000700 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000701
702 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000703}
704
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000705#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000706PyDoc_STRVAR(comp_copy__doc__,
707"copy() -- Return a copy of the compression object.");
708
709static PyObject *
710PyZlib_copy(compobject *self)
711{
712 compobject *retval = NULL;
713 int err;
714
715 retval = newcompobject(&Comptype);
716 if (!retval) return NULL;
717
718 /* Copy the zstream state
719 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
720 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000721 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000722 err = deflateCopy(&retval->zst, &self->zst);
723 switch(err) {
724 case(Z_OK):
725 break;
726 case(Z_STREAM_ERROR):
727 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
728 goto error;
729 case(Z_MEM_ERROR):
730 PyErr_SetString(PyExc_MemoryError,
731 "Can't allocate memory for compression object");
732 goto error;
733 default:
734 zlib_error(self->zst, err, "while copying compression object");
735 goto error;
736 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000737 Py_INCREF(self->unused_data);
738 Py_INCREF(self->unconsumed_tail);
739 Py_XDECREF(retval->unused_data);
740 Py_XDECREF(retval->unconsumed_tail);
741 retval->unused_data = self->unused_data;
742 retval->unconsumed_tail = self->unconsumed_tail;
743
744 /* Mark it as being initialized */
745 retval->is_initialised = 1;
746
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000747 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000748 return (PyObject *)retval;
749
750error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000751 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000752 Py_XDECREF(retval);
753 return NULL;
754}
755
756PyDoc_STRVAR(decomp_copy__doc__,
757"copy() -- Return a copy of the decompression object.");
758
759static PyObject *
760PyZlib_uncopy(compobject *self)
761{
762 compobject *retval = NULL;
763 int err;
764
765 retval = newcompobject(&Decomptype);
766 if (!retval) return NULL;
767
768 /* Copy the zstream state
769 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
770 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000771 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772 err = inflateCopy(&retval->zst, &self->zst);
773 switch(err) {
774 case(Z_OK):
775 break;
776 case(Z_STREAM_ERROR):
777 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
778 goto error;
779 case(Z_MEM_ERROR):
780 PyErr_SetString(PyExc_MemoryError,
781 "Can't allocate memory for decompression object");
782 goto error;
783 default:
784 zlib_error(self->zst, err, "while copying decompression object");
785 goto error;
786 }
787
788 Py_INCREF(self->unused_data);
789 Py_INCREF(self->unconsumed_tail);
790 Py_XDECREF(retval->unused_data);
791 Py_XDECREF(retval->unconsumed_tail);
792 retval->unused_data = self->unused_data;
793 retval->unconsumed_tail = self->unconsumed_tail;
794
795 /* Mark it as being initialized */
796 retval->is_initialised = 1;
797
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000798 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799 return (PyObject *)retval;
800
801error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000802 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000803 Py_XDECREF(retval);
804 return NULL;
805}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000808PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000809"flush( [length] ) -- Return a string containing any remaining\n"
810"decompressed data. length, if given, is the initial size of the\n"
811"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000812"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000813"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000814
Guido van Rossumfb221561997-04-29 15:38:09 +0000815static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000816PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000817{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000818 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000819 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000820 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000821
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000822 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000823 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000824 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000825 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
826 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000827 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000828 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000829 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000830
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000831
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000832 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000833
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000834 start_total_out = self->zst.total_out;
835 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000836 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000837
838 Py_BEGIN_ALLOW_THREADS
839 err = inflate(&(self->zst), Z_FINISH);
840 Py_END_ALLOW_THREADS
841
842 /* while Z_OK and the output buffer is full, there might be more output,
843 so extend the output buffer and try again */
844 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000845 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000846 Py_DECREF(retval);
847 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000848 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000849 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000850 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
851 self->zst.avail_out = length;
852 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000853
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000854 Py_BEGIN_ALLOW_THREADS
855 err = inflate(&(self->zst), Z_FINISH);
856 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000857 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000858
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000859 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
860 various data structures. Note we should only get Z_STREAM_END when
861 flushmode is Z_FINISH */
862 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000863 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000864 self->is_initialised = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000865 if (err != Z_OK) {
866 zlib_error(self->zst, err, "from inflateEnd()");
867 Py_DECREF(retval);
868 retval = NULL;
869 goto error;
870 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000871 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000872 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000873 Py_DECREF(retval);
874 retval = NULL;
875 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000876
877error:
878
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000879 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000880
Jeremy Hylton9714f992001-10-16 21:19:45 +0000881 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000882}
883
884static PyMethodDef comp_methods[] =
885{
Tim Peters977e5402001-10-17 03:57:20 +0000886 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000887 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000888 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000889 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000890#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
892 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000893#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000895};
896
897static PyMethodDef Decomp_methods[] =
898{
Tim Peters977e5402001-10-17 03:57:20 +0000899 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000900 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000901 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000902 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000903#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
905 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000906#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000907 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000908};
909
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000910#define COMP_OFF(x) offsetof(compobject, x)
911static PyMemberDef Decomp_members[] = {
912 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
913 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
914 {NULL},
915};
Guido van Rossumfb221561997-04-29 15:38:09 +0000916
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000917PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000918"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
919"\n"
920"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000921"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000922
Guido van Rossumfb221561997-04-29 15:38:09 +0000923static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000924PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000925{
Christian Heimescc47b052008-03-25 14:56:36 +0000926 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000927 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000928
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000929 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
930 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000931 /* Releasing the GIL for very small buffers is inefficient
932 and may lower performance */
933 if (pbuf.len > 1024*5) {
934 Py_BEGIN_ALLOW_THREADS
935 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
936 Py_END_ALLOW_THREADS
937 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000938 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000939 }
940 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000941 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000942}
Tim Peters977e5402001-10-17 03:57:20 +0000943
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000944PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000945"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
946"\n"
947"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000948"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000949
950static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000951PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000952{
Christian Heimescc47b052008-03-25 14:56:36 +0000953 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000954 Py_buffer pbuf;
955 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000956
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000957 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000958 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000959 /* Releasing the GIL for very small buffers is inefficient
960 and may lower performance */
961 if (pbuf.len > 1024*5) {
962 Py_BEGIN_ALLOW_THREADS
963 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
964 Py_END_ALLOW_THREADS
965 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000966 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000967 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000968 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +0000969 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000970}
Tim Peters977e5402001-10-17 03:57:20 +0000971
Guido van Rossumfb221561997-04-29 15:38:09 +0000972
973static PyMethodDef zlib_methods[] =
974{
Tim Peters977e5402001-10-17 03:57:20 +0000975 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000976 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000977 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000978 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000979 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000980 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000981 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
982 crc32__doc__},
983 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000984 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000985 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000986 decompressobj__doc__},
987 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000988};
989
Tim Peters0c322792002-07-17 16:49:03 +0000990static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000991 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000992 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000993 sizeof(compobject),
994 0,
995 (destructor)Comp_dealloc, /*tp_dealloc*/
996 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000997 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +0000998 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000999 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001000 0, /*tp_repr*/
1001 0, /*tp_as_number*/
1002 0, /*tp_as_sequence*/
1003 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001004 0, /*tp_hash*/
1005 0, /*tp_call*/
1006 0, /*tp_str*/
1007 0, /*tp_getattro*/
1008 0, /*tp_setattro*/
1009 0, /*tp_as_buffer*/
1010 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1011 0, /*tp_doc*/
1012 0, /*tp_traverse*/
1013 0, /*tp_clear*/
1014 0, /*tp_richcompare*/
1015 0, /*tp_weaklistoffset*/
1016 0, /*tp_iter*/
1017 0, /*tp_iternext*/
1018 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001019};
1020
Tim Peters0c322792002-07-17 16:49:03 +00001021static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001022 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001023 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001024 sizeof(compobject),
1025 0,
1026 (destructor)Decomp_dealloc, /*tp_dealloc*/
1027 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001028 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001029 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001030 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001031 0, /*tp_repr*/
1032 0, /*tp_as_number*/
1033 0, /*tp_as_sequence*/
1034 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001035 0, /*tp_hash*/
1036 0, /*tp_call*/
1037 0, /*tp_str*/
1038 0, /*tp_getattro*/
1039 0, /*tp_setattro*/
1040 0, /*tp_as_buffer*/
1041 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1042 0, /*tp_doc*/
1043 0, /*tp_traverse*/
1044 0, /*tp_clear*/
1045 0, /*tp_richcompare*/
1046 0, /*tp_weaklistoffset*/
1047 0, /*tp_iter*/
1048 0, /*tp_iternext*/
1049 Decomp_methods, /*tp_methods*/
1050 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001051};
1052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001053PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001054"The functions in this module allow compression and decompression using the\n"
1055"zlib library, which is based on GNU zip.\n"
1056"\n"
1057"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1058"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001059"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001060"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001061"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001062"decompressobj([wbits]) -- Return a decompressor object.\n"
1063"\n"
1064"'wbits' is window buffer size.\n"
1065"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001066"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001067
Martin v. Löwis1a214512008-06-11 05:26:20 +00001068static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001069 PyModuleDef_HEAD_INIT,
1070 "zlib",
1071 zlib_module_documentation,
1072 -1,
1073 zlib_methods,
1074 NULL,
1075 NULL,
1076 NULL,
1077 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001078};
1079
Mark Hammond62b1ab12002-07-23 06:31:15 +00001080PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001081PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001082{
Fred Drake4baedc12002-04-01 14:53:37 +00001083 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001084 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001085 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001086 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001087 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001088 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001089 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001090 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001091
Fred Drake4baedc12002-04-01 14:53:37 +00001092 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1093 if (ZlibError != NULL) {
1094 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001095 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001096 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001097 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1098 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1099 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1100 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1101 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1102 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1103 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1104 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1105 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001106
Jeremy Hylton9714f992001-10-16 21:19:45 +00001107 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1108 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1109 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1110 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001111
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001112 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001113 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001114 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001115
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001116 PyModule_AddStringConstant(m, "__version__", "1.0");
1117
Martin v. Löwis1a214512008-06-11 05:26:20 +00001118 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001119}