blob: c0dd7cd6c0e7140fc3b43739bf10a2c05202f828 [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
Jeremy Hylton9714f992001-10-16 21:19:45 +0000563 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200564 /* Not all of the compressed data could be accommodated in a buffer of
565 the specified size. Return the unconsumed tail in an attribute. */
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);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200569 }
570 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
571 /* All of the compressed data was consumed. Clear unconsumed_tail. */
572 Py_DECREF(self->unconsumed_tail);
573 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
574 }
575 if (self->unconsumed_tail == NULL) {
576 Py_DECREF(RetVal);
577 RetVal = NULL;
578 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000579 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000580
Tim Peters977e5402001-10-17 03:57:20 +0000581 /* The end of the compressed data has been reached, so set the
582 unused_data attribute to a string containing the remainder of the
583 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000584 inflateEnd, but the old behaviour of only calling it on flush() is
585 preserved.
586 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000587 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000588 Py_XDECREF(self->unused_data); /* Free original empty string */
589 self->unused_data = PyBytes_FromStringAndSize(
590 (char *)self->zst.next_in, self->zst.avail_in);
591 if (self->unused_data == NULL) {
592 Py_DECREF(RetVal);
593 goto error;
594 }
595 /* We will only get Z_BUF_ERROR if the output buffer was full
596 but there wasn't more output when we tried again, so it is
597 not an error condition.
598 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000599 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000600 zlib_error(self->zst, err, "while decompressing");
601 Py_DECREF(RetVal);
602 RetVal = NULL;
603 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000604 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000605
Gregory P. Smith693fc462008-09-06 20:13:06 +0000606 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000607 Py_DECREF(RetVal);
608 RetVal = NULL;
609 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000610
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000611 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000612 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000613 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000614 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000615}
616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000617PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000618"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000619"\n"
620"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000621"default value used when mode is not specified is Z_FINISH.\n"
622"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000623"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000624
Guido van Rossumfb221561997-04-29 15:38:09 +0000625static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000626PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000627{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000628 int err, length = DEFAULTALLOC;
629 PyObject *RetVal;
630 int flushmode = Z_FINISH;
631 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000632
Jeremy Hylton9714f992001-10-16 21:19:45 +0000633 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000634 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000635
Jeremy Hylton9714f992001-10-16 21:19:45 +0000636 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
637 doing any work at all; just return an empty string. */
638 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000639 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000640 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000641
Gregory P. Smith693fc462008-09-06 20:13:06 +0000642 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000643 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000644
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000645 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000646
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647 start_total_out = self->zst.total_out;
648 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000649 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000650 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000651
652 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000653 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000654 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000655
Jeremy Hylton9714f992001-10-16 21:19:45 +0000656 /* while Z_OK and the output buffer is full, there might be more output,
657 so extend the output buffer and try again */
658 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000659 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000660 Py_DECREF(RetVal);
661 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000663 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000664 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000665 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 self->zst.avail_out = length;
667 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000668
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000669 Py_BEGIN_ALLOW_THREADS
670 err = deflate(&(self->zst), flushmode);
671 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000672 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000673
Jeremy Hylton9714f992001-10-16 21:19:45 +0000674 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000675 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000676 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000677 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000678 err = deflateEnd(&(self->zst));
679 if (err != Z_OK) {
680 zlib_error(self->zst, err, "from deflateEnd()");
681 Py_DECREF(RetVal);
682 RetVal = NULL;
683 goto error;
684 }
685 else
686 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000687
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000688 /* We will only get Z_BUF_ERROR if the output buffer was full
689 but there wasn't more output when we tried again, so it is
690 not an error condition.
691 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000692 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000693 zlib_error(self->zst, err, "while flushing");
694 Py_DECREF(RetVal);
695 RetVal = NULL;
696 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000697 }
Tim Peters977e5402001-10-17 03:57:20 +0000698
Gregory P. Smith693fc462008-09-06 20:13:06 +0000699 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000700 Py_DECREF(RetVal);
701 RetVal = NULL;
702 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000703
Tim Peters977e5402001-10-17 03:57:20 +0000704 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000705 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000706
707 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000708}
709
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000710#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000711PyDoc_STRVAR(comp_copy__doc__,
712"copy() -- Return a copy of the compression object.");
713
714static PyObject *
715PyZlib_copy(compobject *self)
716{
717 compobject *retval = NULL;
718 int err;
719
720 retval = newcompobject(&Comptype);
721 if (!retval) return NULL;
722
723 /* Copy the zstream state
724 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
725 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000726 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000727 err = deflateCopy(&retval->zst, &self->zst);
728 switch(err) {
729 case(Z_OK):
730 break;
731 case(Z_STREAM_ERROR):
732 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
733 goto error;
734 case(Z_MEM_ERROR):
735 PyErr_SetString(PyExc_MemoryError,
736 "Can't allocate memory for compression object");
737 goto error;
738 default:
739 zlib_error(self->zst, err, "while copying compression object");
740 goto error;
741 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000742 Py_INCREF(self->unused_data);
743 Py_INCREF(self->unconsumed_tail);
744 Py_XDECREF(retval->unused_data);
745 Py_XDECREF(retval->unconsumed_tail);
746 retval->unused_data = self->unused_data;
747 retval->unconsumed_tail = self->unconsumed_tail;
748
749 /* Mark it as being initialized */
750 retval->is_initialised = 1;
751
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000752 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000753 return (PyObject *)retval;
754
755error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000756 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000757 Py_XDECREF(retval);
758 return NULL;
759}
760
761PyDoc_STRVAR(decomp_copy__doc__,
762"copy() -- Return a copy of the decompression object.");
763
764static PyObject *
765PyZlib_uncopy(compobject *self)
766{
767 compobject *retval = NULL;
768 int err;
769
770 retval = newcompobject(&Decomptype);
771 if (!retval) return NULL;
772
773 /* Copy the zstream state
774 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
775 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000776 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777 err = inflateCopy(&retval->zst, &self->zst);
778 switch(err) {
779 case(Z_OK):
780 break;
781 case(Z_STREAM_ERROR):
782 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
783 goto error;
784 case(Z_MEM_ERROR):
785 PyErr_SetString(PyExc_MemoryError,
786 "Can't allocate memory for decompression object");
787 goto error;
788 default:
789 zlib_error(self->zst, err, "while copying decompression object");
790 goto error;
791 }
792
793 Py_INCREF(self->unused_data);
794 Py_INCREF(self->unconsumed_tail);
795 Py_XDECREF(retval->unused_data);
796 Py_XDECREF(retval->unconsumed_tail);
797 retval->unused_data = self->unused_data;
798 retval->unconsumed_tail = self->unconsumed_tail;
799
800 /* Mark it as being initialized */
801 retval->is_initialised = 1;
802
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000803 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000804 return (PyObject *)retval;
805
806error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000807 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808 Py_XDECREF(retval);
809 return NULL;
810}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000811#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000813PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000814"flush( [length] ) -- Return a string containing any remaining\n"
815"decompressed data. length, if given, is the initial size of the\n"
816"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000817"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000819
Guido van Rossumfb221561997-04-29 15:38:09 +0000820static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000821PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000822{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000823 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000824 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000825 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000826
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000827 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000828 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000829 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000830 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
831 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000832 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000833 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000834 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000835
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000836
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000837 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000838
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000839 start_total_out = self->zst.total_out;
840 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000841 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000842
843 Py_BEGIN_ALLOW_THREADS
844 err = inflate(&(self->zst), Z_FINISH);
845 Py_END_ALLOW_THREADS
846
847 /* while Z_OK and the output buffer is full, there might be more output,
848 so extend the output buffer and try again */
849 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000850 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000851 Py_DECREF(retval);
852 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000853 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000854 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000855 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
856 self->zst.avail_out = length;
857 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000858
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 Py_BEGIN_ALLOW_THREADS
860 err = inflate(&(self->zst), Z_FINISH);
861 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000862 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000863
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000864 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
865 various data structures. Note we should only get Z_STREAM_END when
866 flushmode is Z_FINISH */
867 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000868 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000869 self->is_initialised = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000870 if (err != Z_OK) {
871 zlib_error(self->zst, err, "from inflateEnd()");
872 Py_DECREF(retval);
873 retval = NULL;
874 goto error;
875 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000876 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000877 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000878 Py_DECREF(retval);
879 retval = NULL;
880 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000881
882error:
883
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000884 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000885
Jeremy Hylton9714f992001-10-16 21:19:45 +0000886 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000887}
888
889static PyMethodDef comp_methods[] =
890{
Tim Peters977e5402001-10-17 03:57:20 +0000891 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000892 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000893 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000895#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
897 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000898#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000899 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000900};
901
902static PyMethodDef Decomp_methods[] =
903{
Tim Peters977e5402001-10-17 03:57:20 +0000904 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000905 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000906 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000907 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000908#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
910 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000911#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000912 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000913};
914
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000915#define COMP_OFF(x) offsetof(compobject, x)
916static PyMemberDef Decomp_members[] = {
917 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
918 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
919 {NULL},
920};
Guido van Rossumfb221561997-04-29 15:38:09 +0000921
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000922PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000923"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
924"\n"
925"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000926"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000927
Guido van Rossumfb221561997-04-29 15:38:09 +0000928static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000929PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000930{
Christian Heimescc47b052008-03-25 14:56:36 +0000931 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000932 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000933
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000934 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
935 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000936 /* Releasing the GIL for very small buffers is inefficient
937 and may lower performance */
938 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000939 unsigned char *buf = pbuf.buf;
940 Py_ssize_t len = pbuf.len;
941
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000942 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000943 /* Avoid truncation of length for very large buffers. adler32() takes
944 length as an unsigned int, which may be narrower than Py_ssize_t. */
945 while (len > (size_t) UINT_MAX) {
946 adler32val = adler32(adler32val, buf, UINT_MAX);
947 buf += (size_t) UINT_MAX;
948 len -= (size_t) UINT_MAX;
949 }
950 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000951 Py_END_ALLOW_THREADS
952 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000953 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000954 }
955 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000956 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000957}
Tim Peters977e5402001-10-17 03:57:20 +0000958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000959PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000960"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
961"\n"
962"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000963"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000964
965static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000966PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000967{
Christian Heimescc47b052008-03-25 14:56:36 +0000968 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000969 Py_buffer pbuf;
970 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000971
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000972 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000973 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000974 /* Releasing the GIL for very small buffers is inefficient
975 and may lower performance */
976 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000977 unsigned char *buf = pbuf.buf;
978 Py_ssize_t len = pbuf.len;
979
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000980 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000981 /* Avoid truncation of length for very large buffers. crc32() takes
982 length as an unsigned int, which may be narrower than Py_ssize_t. */
983 while (len > (size_t) UINT_MAX) {
984 crc32val = crc32(crc32val, buf, UINT_MAX);
985 buf += (size_t) UINT_MAX;
986 len -= (size_t) UINT_MAX;
987 }
988 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000989 Py_END_ALLOW_THREADS
990 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000991 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000992 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000993 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +0000994 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000995}
Tim Peters977e5402001-10-17 03:57:20 +0000996
Guido van Rossumfb221561997-04-29 15:38:09 +0000997
998static PyMethodDef zlib_methods[] =
999{
Tim Peters977e5402001-10-17 03:57:20 +00001000 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001001 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001002 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001003 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001004 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001005 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001006 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1007 crc32__doc__},
1008 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001009 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001010 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001011 decompressobj__doc__},
1012 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001013};
1014
Tim Peters0c322792002-07-17 16:49:03 +00001015static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001016 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001017 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001018 sizeof(compobject),
1019 0,
1020 (destructor)Comp_dealloc, /*tp_dealloc*/
1021 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001022 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001023 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001024 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001025 0, /*tp_repr*/
1026 0, /*tp_as_number*/
1027 0, /*tp_as_sequence*/
1028 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001029 0, /*tp_hash*/
1030 0, /*tp_call*/
1031 0, /*tp_str*/
1032 0, /*tp_getattro*/
1033 0, /*tp_setattro*/
1034 0, /*tp_as_buffer*/
1035 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1036 0, /*tp_doc*/
1037 0, /*tp_traverse*/
1038 0, /*tp_clear*/
1039 0, /*tp_richcompare*/
1040 0, /*tp_weaklistoffset*/
1041 0, /*tp_iter*/
1042 0, /*tp_iternext*/
1043 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001044};
1045
Tim Peters0c322792002-07-17 16:49:03 +00001046static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001047 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001048 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001049 sizeof(compobject),
1050 0,
1051 (destructor)Decomp_dealloc, /*tp_dealloc*/
1052 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001053 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001054 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001055 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001056 0, /*tp_repr*/
1057 0, /*tp_as_number*/
1058 0, /*tp_as_sequence*/
1059 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001060 0, /*tp_hash*/
1061 0, /*tp_call*/
1062 0, /*tp_str*/
1063 0, /*tp_getattro*/
1064 0, /*tp_setattro*/
1065 0, /*tp_as_buffer*/
1066 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1067 0, /*tp_doc*/
1068 0, /*tp_traverse*/
1069 0, /*tp_clear*/
1070 0, /*tp_richcompare*/
1071 0, /*tp_weaklistoffset*/
1072 0, /*tp_iter*/
1073 0, /*tp_iternext*/
1074 Decomp_methods, /*tp_methods*/
1075 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001076};
1077
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001078PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001079"The functions in this module allow compression and decompression using the\n"
1080"zlib library, which is based on GNU zip.\n"
1081"\n"
1082"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1083"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001084"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001085"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001086"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001087"decompressobj([wbits]) -- Return a decompressor object.\n"
1088"\n"
1089"'wbits' is window buffer size.\n"
1090"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001091"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001092
Martin v. Löwis1a214512008-06-11 05:26:20 +00001093static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001094 PyModuleDef_HEAD_INIT,
1095 "zlib",
1096 zlib_module_documentation,
1097 -1,
1098 zlib_methods,
1099 NULL,
1100 NULL,
1101 NULL,
1102 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001103};
1104
Mark Hammond62b1ab12002-07-23 06:31:15 +00001105PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001106PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001107{
Fred Drake4baedc12002-04-01 14:53:37 +00001108 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001109 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001110 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001111 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001112 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001113 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001114 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001115 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001116
Fred Drake4baedc12002-04-01 14:53:37 +00001117 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1118 if (ZlibError != NULL) {
1119 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001120 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001121 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001122 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1123 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1124 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1125 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1126 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1127 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1128 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1129 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1130 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001131
Jeremy Hylton9714f992001-10-16 21:19:45 +00001132 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1133 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1134 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1135 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001136
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001137 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001138 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001139 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001140
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001141 PyModule_AddStringConstant(m, "__version__", "1.0");
1142
Martin v. Löwis1a214512008-06-11 05:26:20 +00001143 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001144}