blob: a1e605b3d2be391f3c9a80e1e28fa03a767a180a [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{
Nadeem Vawda524148a2011-08-28 11:26:46 +020055 const char *zmsg = Z_NULL;
56 /* In case of a version mismatch, zst.msg won't be initialized.
57 Check for this case first, before looking at zst.msg. */
58 if (err == Z_VERSION_ERROR)
59 zmsg = "library version mismatch";
60 if (zmsg == Z_NULL)
61 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000062 if (zmsg == Z_NULL) {
63 switch (err) {
64 case Z_BUF_ERROR:
65 zmsg = "incomplete or truncated stream";
66 break;
67 case Z_STREAM_ERROR:
68 zmsg = "inconsistent stream state";
69 break;
70 case Z_DATA_ERROR:
71 zmsg = "invalid input data";
72 break;
73 }
74 }
75 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000076 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000077 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000078 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000079}
80
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000082"compressobj([level]) -- Return a compressor object.\n"
83"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000086PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000087"decompressobj([wbits]) -- Return a decompressor object.\n"
88"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000089"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000090
Guido van Rossumfb221561997-04-29 15:38:09 +000091static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000092newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000093{
Tim Peters977e5402001-10-17 03:57:20 +000094 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000095 self = PyObject_New(compobject, type);
96 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +000098 self->is_initialised = 0;
Gregory P. Smith693fc462008-09-06 20:13:06 +000099 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000100 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000101 Py_DECREF(self);
102 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000103 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000104 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000106 Py_DECREF(self);
107 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000109#ifdef WITH_THREAD
110 self->lock = PyThread_allocate_lock();
111#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000112 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000113}
114
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000116"compress(string[, level]) -- Returned compressed string.\n"
117"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000118"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000119
Guido van Rossumfb221561997-04-29 15:38:09 +0000120static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000121PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000122{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000123 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000124 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200125 Byte *input, *output = NULL;
126 unsigned int length;
127 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000128 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000129
Jeremy Hylton9714f992001-10-16 21:19:45 +0000130 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000131 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000132 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200133
134 if (pinput.len > UINT_MAX) {
135 PyErr_SetString(PyExc_OverflowError,
136 "Size does not fit in an unsigned int");
137 goto error;
138 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000139 input = pinput.buf;
140 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000141
Jeremy Hylton9714f992001-10-16 21:19:45 +0000142 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000143
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000144 output = (Byte*)malloc(zst.avail_out);
145 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000146 PyErr_SetString(PyExc_MemoryError,
147 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200148 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000149 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000150
Jeremy Hylton9714f992001-10-16 21:19:45 +0000151 /* Past the point of no return. From here on out, we need to make sure
152 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000153
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000154 zst.zalloc = (alloc_func)NULL;
155 zst.zfree = (free_func)Z_NULL;
156 zst.next_out = (Byte *)output;
157 zst.next_in = (Byte *)input;
158 zst.avail_in = length;
159 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000160
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000161 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000162 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000164 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 PyErr_SetString(PyExc_MemoryError,
166 "Out of memory while compressing data");
167 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000168 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000169 PyErr_SetString(ZlibError,
170 "Bad compression level");
171 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000172 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000173 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000174 zlib_error(zst, err, "while compressing data");
175 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000176 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000177
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000178 Py_BEGIN_ALLOW_THREADS;
179 err = deflate(&zst, Z_FINISH);
180 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000181
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000183 zlib_error(zst, err, "while compressing data");
184 deflateEnd(&zst);
185 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000186 }
Tim Peters977e5402001-10-17 03:57:20 +0000187
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000188 err=deflateEnd(&zst);
189 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000191 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000192 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000193 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000194
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000195 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000196 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000197 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000198
Jeremy Hylton9714f992001-10-16 21:19:45 +0000199 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000200}
201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000202PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000203"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
204"\n"
205"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000206"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000207
Guido van Rossumfb221561997-04-29 15:38:09 +0000208static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000209PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000210{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200211 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000212 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000213 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200214 unsigned int length;
215 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000216 int wsize=DEF_WBITS;
217 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000218 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000219
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000220 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000221 &pinput, &wsize, &r_strlen))
222 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200223
224 if (pinput.len > UINT_MAX) {
225 PyErr_SetString(PyExc_OverflowError,
226 "Size does not fit in an unsigned int");
227 goto error;
228 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000229 input = pinput.buf;
230 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000231
Jeremy Hylton9714f992001-10-16 21:19:45 +0000232 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000233 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000234
Jeremy Hylton9714f992001-10-16 21:19:45 +0000235 zst.avail_in = length;
236 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000237
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200238 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
239 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000240
Jeremy Hylton9714f992001-10-16 21:19:45 +0000241 zst.zalloc = (alloc_func)NULL;
242 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000243 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000244 zst.next_in = (Byte *)input;
245 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000246
Jeremy Hylton9714f992001-10-16 21:19:45 +0000247 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000248 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000249 break;
Tim Peters977e5402001-10-17 03:57:20 +0000250 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000251 PyErr_SetString(PyExc_MemoryError,
252 "Out of memory while decompressing data");
253 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000254 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000255 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000256 zlib_error(zst, err, "while preparing to decompress data");
257 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000258 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000259
Jeremy Hylton9714f992001-10-16 21:19:45 +0000260 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000261 Py_BEGIN_ALLOW_THREADS
262 err=inflate(&zst, Z_FINISH);
263 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000264
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 switch(err) {
266 case(Z_STREAM_END):
267 break;
268 case(Z_BUF_ERROR):
269 /*
270 * If there is at least 1 byte of room according to zst.avail_out
271 * and we get this error, assume that it means zlib cannot
272 * process the inflate call() due to an error in the data.
273 */
274 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000275 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000276 inflateEnd(&zst);
277 goto error;
278 }
279 /* fall through */
280 case(Z_OK):
281 /* need more memory */
282 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
283 inflateEnd(&zst);
284 goto error;
285 }
286 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000287 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000288 zst.avail_out = r_strlen;
289 r_strlen = r_strlen << 1;
290 break;
291 default:
292 inflateEnd(&zst);
293 zlib_error(zst, err, "while decompressing data");
294 goto error;
295 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000296 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000297
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000298 err = inflateEnd(&zst);
299 if (err != Z_OK) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000300 zlib_error(zst, err, "while finishing data decompression");
301 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000302 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000303
Gregory P. Smith693fc462008-09-06 20:13:06 +0000304 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000305 goto error;
306
Martin v. Löwis423be952008-08-13 15:53:07 +0000307 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000308 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000309
310 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000311 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000312 Py_XDECREF(result_str);
313 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000314}
315
316static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000317PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000318{
Jeremy Hylton499000002001-10-16 21:59:35 +0000319 compobject *self;
320 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
321 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000322
Jeremy Hylton499000002001-10-16 21:59:35 +0000323 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000324 &memLevel, &strategy))
325 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000326
Jeremy Hylton499000002001-10-16 21:59:35 +0000327 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000328 if (self==NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000329 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000330 self->zst.zalloc = (alloc_func)NULL;
331 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000332 self->zst.next_in = NULL;
333 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000334 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
335 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000336 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 self->is_initialised = 1;
338 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000339 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000340 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000341 PyErr_SetString(PyExc_MemoryError,
342 "Can't allocate memory for compression object");
343 return NULL;
344 case(Z_STREAM_ERROR):
345 Py_DECREF(self);
346 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
347 return NULL;
348 default:
349 zlib_error(self->zst, err, "while creating compression object");
350 Py_DECREF(self);
351 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000352 }
353}
354
355static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000356PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000357{
Jeremy Hylton499000002001-10-16 21:59:35 +0000358 int wbits=DEF_WBITS, err;
359 compobject *self;
360 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000361 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000362
363 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000364 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000365 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000366 self->zst.zalloc = (alloc_func)NULL;
367 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000368 self->zst.next_in = NULL;
369 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000370 err = inflateInit2(&self->zst, wbits);
371 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000372 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 self->is_initialised = 1;
374 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000375 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000376 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
378 return NULL;
379 case (Z_MEM_ERROR):
380 Py_DECREF(self);
381 PyErr_SetString(PyExc_MemoryError,
382 "Can't allocate memory for decompression object");
383 return NULL;
384 default:
385 zlib_error(self->zst, err, "while creating decompression object");
386 Py_DECREF(self);
387 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000388 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000389}
390
391static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000392Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000393{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000394#ifdef WITH_THREAD
395 PyThread_free_lock(self->lock);
396#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000397 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000398 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000399 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000400}
401
402static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000403Comp_dealloc(compobject *self)
404{
405 if (self->is_initialised)
406 deflateEnd(&self->zst);
407 Dealloc(self);
408}
409
410static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000411Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000412{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000413 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000414 inflateEnd(&self->zst);
415 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000416}
417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000418PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000419"compress(data) -- Return a string containing data compressed.\n"
420"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000421"After calling this function, some of the input data may still\n"
422"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000423"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000424
425
Guido van Rossumfb221561997-04-29 15:38:09 +0000426static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000427PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000428{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200429 int err;
430 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000431 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200432 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000433 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000434 Byte *input;
435 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000436
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000437 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000438 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200439 if (pinput.len > UINT_MAX) {
440 PyErr_SetString(PyExc_OverflowError,
441 "Size does not fit in an unsigned int");
442 goto error_outer;
443 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000444 input = pinput.buf;
445 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000446
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200447 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
448 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000449
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000450 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000451
Jeremy Hylton9714f992001-10-16 21:19:45 +0000452 start_total_out = self->zst.total_out;
453 self->zst.avail_in = inplen;
454 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000455 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000456 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000457
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000458 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000459 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000460 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000461
Jeremy Hylton9714f992001-10-16 21:19:45 +0000462 /* while Z_OK and the output buffer is full, there might be more output,
463 so extend the output buffer and try again */
464 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000465 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000466 Py_DECREF(RetVal);
467 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000468 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000469 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000470 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000471 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 self->zst.avail_out = length;
473 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000474
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000475 Py_BEGIN_ALLOW_THREADS
476 err = deflate(&(self->zst), Z_NO_FLUSH);
477 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000478 }
Tim Peters977e5402001-10-17 03:57:20 +0000479 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000480 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000481 condition.
482 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000483
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000484 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000485 zlib_error(self->zst, err, "while compressing");
486 Py_DECREF(RetVal);
487 RetVal = NULL;
488 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000489 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000490 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000491 Py_DECREF(RetVal);
492 RetVal = NULL;
493 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000494
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000495 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000496 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200497 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000498 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000499 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000500}
501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000502PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000503"decompress(data, max_length) -- Return a string containing the decompressed\n"
504"version of the data.\n"
505"\n"
506"After calling this function, some of the input data may still be stored in\n"
507"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000508"Call the flush() method to clear these buffers.\n"
509"If the max_length parameter is specified then the return value will be\n"
510"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000511"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000512
Guido van Rossumfb221561997-04-29 15:38:09 +0000513static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000514PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000515{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200516 int err, max_length = 0;
517 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000518 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200519 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000520 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000521 Byte *input;
522 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000523
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000524 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000525 &max_length))
526 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200527 if (pinput.len > UINT_MAX) {
528 PyErr_SetString(PyExc_OverflowError,
529 "Size does not fit in an unsigned int");
530 goto error_outer;
531 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000532 input = pinput.buf;
533 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000534 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000535 PyErr_SetString(PyExc_ValueError,
536 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200537 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000538 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000539
Jeremy Hylton9714f992001-10-16 21:19:45 +0000540 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000541 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000542 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200543 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
544 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000545
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000546 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000547
Jeremy Hylton9714f992001-10-16 21:19:45 +0000548 start_total_out = self->zst.total_out;
549 self->zst.avail_in = inplen;
550 self->zst.next_in = input;
551 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000552 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000553
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000554 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000555 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000556 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000557
Jeremy Hylton9714f992001-10-16 21:19:45 +0000558 /* While Z_OK and the output buffer is full, there might be more output.
559 So extend the output buffer and try again.
560 */
Tim Peters977e5402001-10-17 03:57:20 +0000561 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000562 /* If max_length set, don't continue decompressing if we've already
563 reached the limit.
564 */
565 if (max_length && length >= max_length)
566 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000567
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000568 /* otherwise, ... */
569 old_length = length;
570 length = length << 1;
571 if (max_length && length > max_length)
572 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000573
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000574 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000575 Py_DECREF(RetVal);
576 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000578 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000579 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000580 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000581 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000582
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 Py_BEGIN_ALLOW_THREADS
584 err = inflate(&(self->zst), Z_SYNC_FLUSH);
585 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000586 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000587
Jeremy Hylton9714f992001-10-16 21:19:45 +0000588 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200589 /* Not all of the compressed data could be accommodated in a buffer of
590 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000591 Py_DECREF(self->unconsumed_tail);
592 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
593 self->zst.avail_in);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200594 }
595 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
596 /* All of the compressed data was consumed. Clear unconsumed_tail. */
597 Py_DECREF(self->unconsumed_tail);
598 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
599 }
600 if (self->unconsumed_tail == NULL) {
601 Py_DECREF(RetVal);
602 RetVal = NULL;
603 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000604 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000605
Tim Peters977e5402001-10-17 03:57:20 +0000606 /* The end of the compressed data has been reached, so set the
607 unused_data attribute to a string containing the remainder of the
608 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000609 inflateEnd, but the old behaviour of only calling it on flush() is
610 preserved.
611 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000612 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000613 Py_XDECREF(self->unused_data); /* Free original empty string */
614 self->unused_data = PyBytes_FromStringAndSize(
615 (char *)self->zst.next_in, self->zst.avail_in);
616 if (self->unused_data == NULL) {
617 Py_DECREF(RetVal);
618 goto error;
619 }
620 /* We will only get Z_BUF_ERROR if the output buffer was full
621 but there wasn't more output when we tried again, so it is
622 not an error condition.
623 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000624 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000625 zlib_error(self->zst, err, "while decompressing");
626 Py_DECREF(RetVal);
627 RetVal = NULL;
628 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000629 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000630
Gregory P. Smith693fc462008-09-06 20:13:06 +0000631 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000632 Py_DECREF(RetVal);
633 RetVal = NULL;
634 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000635
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000636 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000637 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200638 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000639 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000640 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000641}
642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000644"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000645"\n"
646"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000647"default value used when mode is not specified is Z_FINISH.\n"
648"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000649"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000650
Guido van Rossumfb221561997-04-29 15:38:09 +0000651static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000652PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000653{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000654 int err, length = DEFAULTALLOC;
655 PyObject *RetVal;
656 int flushmode = Z_FINISH;
657 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000658
Jeremy Hylton9714f992001-10-16 21:19:45 +0000659 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000660 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000661
Jeremy Hylton9714f992001-10-16 21:19:45 +0000662 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
663 doing any work at all; just return an empty string. */
664 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000666 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000667
Gregory P. Smith693fc462008-09-06 20:13:06 +0000668 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000669 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000670
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000671 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000672
Jeremy Hylton9714f992001-10-16 21:19:45 +0000673 start_total_out = self->zst.total_out;
674 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000675 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000676 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000677
678 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000679 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000680 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000681
Jeremy Hylton9714f992001-10-16 21:19:45 +0000682 /* while Z_OK and the output buffer is full, there might be more output,
683 so extend the output buffer and try again */
684 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000685 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000686 Py_DECREF(RetVal);
687 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000688 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000689 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000690 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000691 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000692 self->zst.avail_out = length;
693 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000694
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 Py_BEGIN_ALLOW_THREADS
696 err = deflate(&(self->zst), flushmode);
697 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000698 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000699
Jeremy Hylton9714f992001-10-16 21:19:45 +0000700 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000701 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000702 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000703 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000704 err = deflateEnd(&(self->zst));
705 if (err != Z_OK) {
706 zlib_error(self->zst, err, "from deflateEnd()");
707 Py_DECREF(RetVal);
708 RetVal = NULL;
709 goto error;
710 }
711 else
712 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000713
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000714 /* We will only get Z_BUF_ERROR if the output buffer was full
715 but there wasn't more output when we tried again, so it is
716 not an error condition.
717 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000718 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 zlib_error(self->zst, err, "while flushing");
720 Py_DECREF(RetVal);
721 RetVal = NULL;
722 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000723 }
Tim Peters977e5402001-10-17 03:57:20 +0000724
Gregory P. Smith693fc462008-09-06 20:13:06 +0000725 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000726 Py_DECREF(RetVal);
727 RetVal = NULL;
728 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000729
Tim Peters977e5402001-10-17 03:57:20 +0000730 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000731 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000732
733 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000734}
735
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000736#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000737PyDoc_STRVAR(comp_copy__doc__,
738"copy() -- Return a copy of the compression object.");
739
740static PyObject *
741PyZlib_copy(compobject *self)
742{
743 compobject *retval = NULL;
744 int err;
745
746 retval = newcompobject(&Comptype);
747 if (!retval) return NULL;
748
749 /* Copy the zstream state
750 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
751 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000752 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000753 err = deflateCopy(&retval->zst, &self->zst);
754 switch(err) {
755 case(Z_OK):
756 break;
757 case(Z_STREAM_ERROR):
758 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
759 goto error;
760 case(Z_MEM_ERROR):
761 PyErr_SetString(PyExc_MemoryError,
762 "Can't allocate memory for compression object");
763 goto error;
764 default:
765 zlib_error(self->zst, err, "while copying compression object");
766 goto error;
767 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000768 Py_INCREF(self->unused_data);
769 Py_INCREF(self->unconsumed_tail);
770 Py_XDECREF(retval->unused_data);
771 Py_XDECREF(retval->unconsumed_tail);
772 retval->unused_data = self->unused_data;
773 retval->unconsumed_tail = self->unconsumed_tail;
774
775 /* Mark it as being initialized */
776 retval->is_initialised = 1;
777
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000778 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779 return (PyObject *)retval;
780
781error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000782 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783 Py_XDECREF(retval);
784 return NULL;
785}
786
787PyDoc_STRVAR(decomp_copy__doc__,
788"copy() -- Return a copy of the decompression object.");
789
790static PyObject *
791PyZlib_uncopy(compobject *self)
792{
793 compobject *retval = NULL;
794 int err;
795
796 retval = newcompobject(&Decomptype);
797 if (!retval) return NULL;
798
799 /* Copy the zstream state
800 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
801 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000802 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000803 err = inflateCopy(&retval->zst, &self->zst);
804 switch(err) {
805 case(Z_OK):
806 break;
807 case(Z_STREAM_ERROR):
808 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
809 goto error;
810 case(Z_MEM_ERROR):
811 PyErr_SetString(PyExc_MemoryError,
812 "Can't allocate memory for decompression object");
813 goto error;
814 default:
815 zlib_error(self->zst, err, "while copying decompression object");
816 goto error;
817 }
818
819 Py_INCREF(self->unused_data);
820 Py_INCREF(self->unconsumed_tail);
821 Py_XDECREF(retval->unused_data);
822 Py_XDECREF(retval->unconsumed_tail);
823 retval->unused_data = self->unused_data;
824 retval->unconsumed_tail = self->unconsumed_tail;
825
826 /* Mark it as being initialized */
827 retval->is_initialised = 1;
828
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000829 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830 return (PyObject *)retval;
831
832error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000833 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000834 Py_XDECREF(retval);
835 return NULL;
836}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000837#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000839PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840"flush( [length] ) -- Return a string containing any remaining\n"
841"decompressed data. length, if given, is the initial size of the\n"
842"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000843"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000844"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000845
Guido van Rossumfb221561997-04-29 15:38:09 +0000846static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000847PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000848{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000849 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000850 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000851 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000852
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000853 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000854 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000855 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000856 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
857 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000858 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000859 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000861
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000862
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000863 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000864
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000865 start_total_out = self->zst.total_out;
866 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000867 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000868
869 Py_BEGIN_ALLOW_THREADS
870 err = inflate(&(self->zst), Z_FINISH);
871 Py_END_ALLOW_THREADS
872
873 /* while Z_OK and the output buffer is full, there might be more output,
874 so extend the output buffer and try again */
875 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000876 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000877 Py_DECREF(retval);
878 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000880 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
882 self->zst.avail_out = length;
883 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 Py_BEGIN_ALLOW_THREADS
886 err = inflate(&(self->zst), Z_FINISH);
887 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000888 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000889
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000890 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
891 various data structures. Note we should only get Z_STREAM_END when
892 flushmode is Z_FINISH */
893 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000894 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000895 self->is_initialised = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 if (err != Z_OK) {
897 zlib_error(self->zst, err, "from inflateEnd()");
898 Py_DECREF(retval);
899 retval = NULL;
900 goto error;
901 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000902 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000903 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000904 Py_DECREF(retval);
905 retval = NULL;
906 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000907
908error:
909
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000910 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000911
Jeremy Hylton9714f992001-10-16 21:19:45 +0000912 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000913}
914
915static PyMethodDef comp_methods[] =
916{
Tim Peters977e5402001-10-17 03:57:20 +0000917 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000918 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000919 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000920 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000921#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
923 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000924#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000925 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000926};
927
928static PyMethodDef Decomp_methods[] =
929{
Tim Peters977e5402001-10-17 03:57:20 +0000930 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000931 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000932 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000933 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000934#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
936 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000937#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000938 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000939};
940
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000941#define COMP_OFF(x) offsetof(compobject, x)
942static PyMemberDef Decomp_members[] = {
943 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
944 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
945 {NULL},
946};
Guido van Rossumfb221561997-04-29 15:38:09 +0000947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000948PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000949"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
950"\n"
951"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000952"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000953
Guido van Rossumfb221561997-04-29 15:38:09 +0000954static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000955PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000956{
Christian Heimescc47b052008-03-25 14:56:36 +0000957 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000958 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000959
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000960 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
961 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000962 /* Releasing the GIL for very small buffers is inefficient
963 and may lower performance */
964 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000965 unsigned char *buf = pbuf.buf;
966 Py_ssize_t len = pbuf.len;
967
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000968 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000969 /* Avoid truncation of length for very large buffers. adler32() takes
970 length as an unsigned int, which may be narrower than Py_ssize_t. */
971 while (len > (size_t) UINT_MAX) {
972 adler32val = adler32(adler32val, buf, UINT_MAX);
973 buf += (size_t) UINT_MAX;
974 len -= (size_t) UINT_MAX;
975 }
976 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000977 Py_END_ALLOW_THREADS
978 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000979 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000980 }
981 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000982 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000983}
Tim Peters977e5402001-10-17 03:57:20 +0000984
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000985PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000986"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
987"\n"
988"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000989"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000990
991static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000992PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000993{
Christian Heimescc47b052008-03-25 14:56:36 +0000994 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000995 Py_buffer pbuf;
996 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000997
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000998 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000999 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001000 /* Releasing the GIL for very small buffers is inefficient
1001 and may lower performance */
1002 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001003 unsigned char *buf = pbuf.buf;
1004 Py_ssize_t len = pbuf.len;
1005
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001006 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001007 /* Avoid truncation of length for very large buffers. crc32() takes
1008 length as an unsigned int, which may be narrower than Py_ssize_t. */
1009 while (len > (size_t) UINT_MAX) {
1010 crc32val = crc32(crc32val, buf, UINT_MAX);
1011 buf += (size_t) UINT_MAX;
1012 len -= (size_t) UINT_MAX;
1013 }
1014 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001015 Py_END_ALLOW_THREADS
1016 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001017 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001018 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001019 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001020 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001021}
Tim Peters977e5402001-10-17 03:57:20 +00001022
Guido van Rossumfb221561997-04-29 15:38:09 +00001023
1024static PyMethodDef zlib_methods[] =
1025{
Tim Peters977e5402001-10-17 03:57:20 +00001026 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001027 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001028 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001029 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001030 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001031 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001032 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1033 crc32__doc__},
1034 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001035 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001036 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001037 decompressobj__doc__},
1038 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001039};
1040
Tim Peters0c322792002-07-17 16:49:03 +00001041static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001042 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001043 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001044 sizeof(compobject),
1045 0,
1046 (destructor)Comp_dealloc, /*tp_dealloc*/
1047 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001048 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001049 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001050 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001051 0, /*tp_repr*/
1052 0, /*tp_as_number*/
1053 0, /*tp_as_sequence*/
1054 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001055 0, /*tp_hash*/
1056 0, /*tp_call*/
1057 0, /*tp_str*/
1058 0, /*tp_getattro*/
1059 0, /*tp_setattro*/
1060 0, /*tp_as_buffer*/
1061 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1062 0, /*tp_doc*/
1063 0, /*tp_traverse*/
1064 0, /*tp_clear*/
1065 0, /*tp_richcompare*/
1066 0, /*tp_weaklistoffset*/
1067 0, /*tp_iter*/
1068 0, /*tp_iternext*/
1069 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001070};
1071
Tim Peters0c322792002-07-17 16:49:03 +00001072static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001073 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001074 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001075 sizeof(compobject),
1076 0,
1077 (destructor)Decomp_dealloc, /*tp_dealloc*/
1078 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001079 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001080 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001081 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001082 0, /*tp_repr*/
1083 0, /*tp_as_number*/
1084 0, /*tp_as_sequence*/
1085 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001086 0, /*tp_hash*/
1087 0, /*tp_call*/
1088 0, /*tp_str*/
1089 0, /*tp_getattro*/
1090 0, /*tp_setattro*/
1091 0, /*tp_as_buffer*/
1092 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1093 0, /*tp_doc*/
1094 0, /*tp_traverse*/
1095 0, /*tp_clear*/
1096 0, /*tp_richcompare*/
1097 0, /*tp_weaklistoffset*/
1098 0, /*tp_iter*/
1099 0, /*tp_iternext*/
1100 Decomp_methods, /*tp_methods*/
1101 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001102};
1103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001104PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001105"The functions in this module allow compression and decompression using the\n"
1106"zlib library, which is based on GNU zip.\n"
1107"\n"
1108"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1109"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001110"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001111"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001112"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001113"decompressobj([wbits]) -- Return a decompressor object.\n"
1114"\n"
1115"'wbits' is window buffer size.\n"
1116"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001117"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001118
Martin v. Löwis1a214512008-06-11 05:26:20 +00001119static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001120 PyModuleDef_HEAD_INIT,
1121 "zlib",
1122 zlib_module_documentation,
1123 -1,
1124 zlib_methods,
1125 NULL,
1126 NULL,
1127 NULL,
1128 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001129};
1130
Mark Hammond62b1ab12002-07-23 06:31:15 +00001131PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001132PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001133{
Fred Drake4baedc12002-04-01 14:53:37 +00001134 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001135 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001136 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001137 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001138 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001139 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001140 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001141 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001142
Fred Drake4baedc12002-04-01 14:53:37 +00001143 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1144 if (ZlibError != NULL) {
1145 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001146 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001147 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001148 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1149 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1150 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1151 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1152 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1153 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1154 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1155 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1156 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001157
Jeremy Hylton9714f992001-10-16 21:19:45 +00001158 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1159 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1160 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1161 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001162
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001163 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001164 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001165 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001166
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001167 PyModule_AddStringConstant(m, "__version__", "1.0");
1168
Martin v. Löwis1a214512008-06-11 05:26:20 +00001169 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001170}