blob: bb453aed637a41ec651e5356ae2c1f53bf36397c [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"
Nadeem Vawda19e568d2012-11-11 14:04:14 +010084"Optional arg level is the compression level, in 0-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"
Nadeem Vawda19e568d2012-11-11 14:04:14 +0100118"Optional arg level is the compression level, in 0-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
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100502/* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
503 self->unused_data or self->unconsumed_tail, as appropriate. */
504static int
505save_unconsumed_input(compobject *self, int err)
506{
507 if (err == Z_STREAM_END) {
508 /* The end of the compressed data has been reached. Store the leftover
509 input data in self->unused_data. */
510 if (self->zst.avail_in > 0) {
511 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
512 Py_ssize_t new_size;
513 PyObject *new_data;
514 if (self->zst.avail_in > PY_SSIZE_T_MAX - old_size) {
515 PyErr_NoMemory();
516 return -1;
517 }
518 new_size = old_size + self->zst.avail_in;
519 new_data = PyBytes_FromStringAndSize(NULL, new_size);
520 if (new_data == NULL)
521 return -1;
522 Py_MEMCPY(PyBytes_AS_STRING(new_data),
523 PyBytes_AS_STRING(self->unused_data), old_size);
524 Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
525 self->zst.next_in, self->zst.avail_in);
526 Py_DECREF(self->unused_data);
527 self->unused_data = new_data;
528 self->zst.avail_in = 0;
529 }
530 }
531 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
532 /* This code handles two distinct cases:
533 1. Output limit was reached. Save leftover input in unconsumed_tail.
534 2. All input data was consumed. Clear unconsumed_tail. */
535 PyObject *new_data = PyBytes_FromStringAndSize(
536 (char *)self->zst.next_in, self->zst.avail_in);
537 if (new_data == NULL)
538 return -1;
539 Py_DECREF(self->unconsumed_tail);
540 self->unconsumed_tail = new_data;
541 }
542 return 0;
543}
544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000545PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000546"decompress(data, max_length) -- Return a string containing the decompressed\n"
547"version of the data.\n"
548"\n"
549"After calling this function, some of the input data may still be stored in\n"
550"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000551"Call the flush() method to clear these buffers.\n"
552"If the max_length parameter is specified then the return value will be\n"
553"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000554"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000555
Guido van Rossumfb221561997-04-29 15:38:09 +0000556static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000557PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000558{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200559 int err, max_length = 0;
560 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000561 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200562 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000563 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000564 Byte *input;
565 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000566
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000567 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000568 &max_length))
569 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200570 if (pinput.len > UINT_MAX) {
571 PyErr_SetString(PyExc_OverflowError,
572 "Size does not fit in an unsigned int");
573 goto error_outer;
574 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000575 input = pinput.buf;
576 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000577 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000578 PyErr_SetString(PyExc_ValueError,
579 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200580 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000581 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000582
Jeremy Hylton9714f992001-10-16 21:19:45 +0000583 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000584 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000585 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200586 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
587 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000588
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000589 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000590
Jeremy Hylton9714f992001-10-16 21:19:45 +0000591 start_total_out = self->zst.total_out;
592 self->zst.avail_in = inplen;
593 self->zst.next_in = input;
594 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000595 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000596
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000597 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000598 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000599 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000600
Jeremy Hylton9714f992001-10-16 21:19:45 +0000601 /* While Z_OK and the output buffer is full, there might be more output.
602 So extend the output buffer and try again.
603 */
Tim Peters977e5402001-10-17 03:57:20 +0000604 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000605 /* If max_length set, don't continue decompressing if we've already
606 reached the limit.
607 */
608 if (max_length && length >= max_length)
609 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000610
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 /* otherwise, ... */
612 old_length = length;
613 length = length << 1;
614 if (max_length && length > max_length)
615 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000616
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000618 Py_DECREF(RetVal);
619 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000620 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000621 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000623 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000624 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000625
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000626 Py_BEGIN_ALLOW_THREADS
627 err = inflate(&(self->zst), Z_SYNC_FLUSH);
628 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000629 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000630
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100631 if (save_unconsumed_input(self, err) < 0) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200632 Py_DECREF(RetVal);
633 RetVal = NULL;
634 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000635 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000636
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100637 /* This is the logical place to call inflateEnd, but the old behaviour of
638 only calling it on flush() is preserved. */
639
640 if (err != Z_STREAM_END && err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000641 /* We will only get Z_BUF_ERROR if the output buffer was full
642 but there wasn't more output when we tried again, so it is
643 not an error condition.
644 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 zlib_error(self->zst, err, "while decompressing");
646 Py_DECREF(RetVal);
647 RetVal = NULL;
648 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000649 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000650
Gregory P. Smith693fc462008-09-06 20:13:06 +0000651 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000652 Py_DECREF(RetVal);
653 RetVal = NULL;
654 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000655
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000656 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000657 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200658 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000659 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000660 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000661}
662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000663PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000664"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000665"\n"
666"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000667"default value used when mode is not specified is Z_FINISH.\n"
668"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000670
Guido van Rossumfb221561997-04-29 15:38:09 +0000671static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000672PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000673{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000674 int err, length = DEFAULTALLOC;
675 PyObject *RetVal;
676 int flushmode = Z_FINISH;
677 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000678
Jeremy Hylton9714f992001-10-16 21:19:45 +0000679 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000681
Jeremy Hylton9714f992001-10-16 21:19:45 +0000682 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
683 doing any work at all; just return an empty string. */
684 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000685 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000686 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000687
Gregory P. Smith693fc462008-09-06 20:13:06 +0000688 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000689 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000690
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000691 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000692
Jeremy Hylton9714f992001-10-16 21:19:45 +0000693 start_total_out = self->zst.total_out;
694 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000695 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000696 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000697
698 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000699 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000700 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000701
Jeremy Hylton9714f992001-10-16 21:19:45 +0000702 /* while Z_OK and the output buffer is full, there might be more output,
703 so extend the output buffer and try again */
704 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000705 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000706 Py_DECREF(RetVal);
707 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000708 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000709 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000711 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000712 self->zst.avail_out = length;
713 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000714
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000715 Py_BEGIN_ALLOW_THREADS
716 err = deflate(&(self->zst), flushmode);
717 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000718 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000719
Jeremy Hylton9714f992001-10-16 21:19:45 +0000720 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000721 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000722 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000723 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000724 err = deflateEnd(&(self->zst));
725 if (err != Z_OK) {
726 zlib_error(self->zst, err, "from deflateEnd()");
727 Py_DECREF(RetVal);
728 RetVal = NULL;
729 goto error;
730 }
731 else
732 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000733
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000734 /* We will only get Z_BUF_ERROR if the output buffer was full
735 but there wasn't more output when we tried again, so it is
736 not an error condition.
737 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000738 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000739 zlib_error(self->zst, err, "while flushing");
740 Py_DECREF(RetVal);
741 RetVal = NULL;
742 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000743 }
Tim Peters977e5402001-10-17 03:57:20 +0000744
Gregory P. Smith693fc462008-09-06 20:13:06 +0000745 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000746 Py_DECREF(RetVal);
747 RetVal = NULL;
748 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000749
Tim Peters977e5402001-10-17 03:57:20 +0000750 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000751 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000752
753 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000754}
755
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000756#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000757PyDoc_STRVAR(comp_copy__doc__,
758"copy() -- Return a copy of the compression object.");
759
760static PyObject *
761PyZlib_copy(compobject *self)
762{
763 compobject *retval = NULL;
764 int err;
765
766 retval = newcompobject(&Comptype);
767 if (!retval) return NULL;
768
769 /* Copy the zstream state
770 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
771 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000772 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000773 err = deflateCopy(&retval->zst, &self->zst);
774 switch(err) {
775 case(Z_OK):
776 break;
777 case(Z_STREAM_ERROR):
778 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
779 goto error;
780 case(Z_MEM_ERROR):
781 PyErr_SetString(PyExc_MemoryError,
782 "Can't allocate memory for compression object");
783 goto error;
784 default:
785 zlib_error(self->zst, err, "while copying compression object");
786 goto error;
787 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000788 Py_INCREF(self->unused_data);
789 Py_INCREF(self->unconsumed_tail);
790 Py_XDECREF(retval->unused_data);
791 Py_XDECREF(retval->unconsumed_tail);
792 retval->unused_data = self->unused_data;
793 retval->unconsumed_tail = self->unconsumed_tail;
794
795 /* Mark it as being initialized */
796 retval->is_initialised = 1;
797
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000798 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799 return (PyObject *)retval;
800
801error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000802 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000803 Py_XDECREF(retval);
804 return NULL;
805}
806
807PyDoc_STRVAR(decomp_copy__doc__,
808"copy() -- Return a copy of the decompression object.");
809
810static PyObject *
811PyZlib_uncopy(compobject *self)
812{
813 compobject *retval = NULL;
814 int err;
815
816 retval = newcompobject(&Decomptype);
817 if (!retval) return NULL;
818
819 /* Copy the zstream state
820 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
821 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000822 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000823 err = inflateCopy(&retval->zst, &self->zst);
824 switch(err) {
825 case(Z_OK):
826 break;
827 case(Z_STREAM_ERROR):
828 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
829 goto error;
830 case(Z_MEM_ERROR):
831 PyErr_SetString(PyExc_MemoryError,
832 "Can't allocate memory for decompression object");
833 goto error;
834 default:
835 zlib_error(self->zst, err, "while copying decompression object");
836 goto error;
837 }
838
839 Py_INCREF(self->unused_data);
840 Py_INCREF(self->unconsumed_tail);
841 Py_XDECREF(retval->unused_data);
842 Py_XDECREF(retval->unconsumed_tail);
843 retval->unused_data = self->unused_data;
844 retval->unconsumed_tail = self->unconsumed_tail;
845
846 /* Mark it as being initialized */
847 retval->is_initialised = 1;
848
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000849 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000850 return (PyObject *)retval;
851
852error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000853 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854 Py_XDECREF(retval);
855 return NULL;
856}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000857#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000858
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000859PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860"flush( [length] ) -- Return a string containing any remaining\n"
861"decompressed data. length, if given, is the initial size of the\n"
862"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000863"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000864"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000865
Guido van Rossumfb221561997-04-29 15:38:09 +0000866static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000867PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000868{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000869 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000870 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000871 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000872
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000873 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000874 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000875 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000876 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
877 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000878 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000879 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000880 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000881
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000882
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000883 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000884
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000885 start_total_out = self->zst.total_out;
Nadeem Vawda7ee95552012-11-11 03:15:32 +0100886 self->zst.avail_in = PyBytes_GET_SIZE(self->unconsumed_tail);
887 self->zst.next_in = (Byte *)PyBytes_AS_STRING(self->unconsumed_tail);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000888 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000889 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000890
891 Py_BEGIN_ALLOW_THREADS
892 err = inflate(&(self->zst), Z_FINISH);
893 Py_END_ALLOW_THREADS
894
895 /* while Z_OK and the output buffer is full, there might be more output,
896 so extend the output buffer and try again */
897 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000899 Py_DECREF(retval);
900 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000901 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000902 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000903 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
904 self->zst.avail_out = length;
905 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000906
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000907 Py_BEGIN_ALLOW_THREADS
908 err = inflate(&(self->zst), Z_FINISH);
909 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000910 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000911
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100912 if (save_unconsumed_input(self, err) < 0) {
913 Py_DECREF(retval);
914 retval = NULL;
915 goto error;
916 }
917
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000918 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
919 various data structures. Note we should only get Z_STREAM_END when
920 flushmode is Z_FINISH */
921 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000923 self->is_initialised = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000924 if (err != Z_OK) {
925 zlib_error(self->zst, err, "from inflateEnd()");
926 Py_DECREF(retval);
927 retval = NULL;
928 goto error;
929 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000930 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100931
Gregory P. Smith693fc462008-09-06 20:13:06 +0000932 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000933 Py_DECREF(retval);
934 retval = NULL;
935 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000936
937error:
938
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000939 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000940
Jeremy Hylton9714f992001-10-16 21:19:45 +0000941 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000942}
943
944static PyMethodDef comp_methods[] =
945{
Tim Peters977e5402001-10-17 03:57:20 +0000946 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000947 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000948 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000949 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000950#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
952 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000953#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000954 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000955};
956
957static PyMethodDef Decomp_methods[] =
958{
Tim Peters977e5402001-10-17 03:57:20 +0000959 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000960 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000961 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000962 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000963#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
965 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000967 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000968};
969
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000970#define COMP_OFF(x) offsetof(compobject, x)
971static PyMemberDef Decomp_members[] = {
972 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
973 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
974 {NULL},
975};
Guido van Rossumfb221561997-04-29 15:38:09 +0000976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000977PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000978"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
979"\n"
980"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000981"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000982
Guido van Rossumfb221561997-04-29 15:38:09 +0000983static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000984PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000985{
Christian Heimescc47b052008-03-25 14:56:36 +0000986 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000987 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000988
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000989 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
990 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000991 /* Releasing the GIL for very small buffers is inefficient
992 and may lower performance */
993 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000994 unsigned char *buf = pbuf.buf;
995 Py_ssize_t len = pbuf.len;
996
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000997 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000998 /* Avoid truncation of length for very large buffers. adler32() takes
999 length as an unsigned int, which may be narrower than Py_ssize_t. */
1000 while (len > (size_t) UINT_MAX) {
1001 adler32val = adler32(adler32val, buf, UINT_MAX);
1002 buf += (size_t) UINT_MAX;
1003 len -= (size_t) UINT_MAX;
1004 }
1005 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001006 Py_END_ALLOW_THREADS
1007 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001008 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001009 }
1010 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001011 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001012}
Tim Peters977e5402001-10-17 03:57:20 +00001013
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001014PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001015"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1016"\n"
1017"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001018"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001019
1020static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001021PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001022{
Christian Heimescc47b052008-03-25 14:56:36 +00001023 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001024 Py_buffer pbuf;
1025 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001026
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001027 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001028 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001029 /* Releasing the GIL for very small buffers is inefficient
1030 and may lower performance */
1031 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001032 unsigned char *buf = pbuf.buf;
1033 Py_ssize_t len = pbuf.len;
1034
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001035 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001036 /* Avoid truncation of length for very large buffers. crc32() takes
1037 length as an unsigned int, which may be narrower than Py_ssize_t. */
1038 while (len > (size_t) UINT_MAX) {
1039 crc32val = crc32(crc32val, buf, UINT_MAX);
1040 buf += (size_t) UINT_MAX;
1041 len -= (size_t) UINT_MAX;
1042 }
1043 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001044 Py_END_ALLOW_THREADS
1045 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001046 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001047 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001048 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001049 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001050}
Tim Peters977e5402001-10-17 03:57:20 +00001051
Guido van Rossumfb221561997-04-29 15:38:09 +00001052
1053static PyMethodDef zlib_methods[] =
1054{
Tim Peters977e5402001-10-17 03:57:20 +00001055 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001056 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001057 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001058 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001059 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001060 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001061 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1062 crc32__doc__},
1063 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001064 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001065 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001066 decompressobj__doc__},
1067 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001068};
1069
Tim Peters0c322792002-07-17 16:49:03 +00001070static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001071 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001072 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001073 sizeof(compobject),
1074 0,
1075 (destructor)Comp_dealloc, /*tp_dealloc*/
1076 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001077 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001078 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001079 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001080 0, /*tp_repr*/
1081 0, /*tp_as_number*/
1082 0, /*tp_as_sequence*/
1083 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001084 0, /*tp_hash*/
1085 0, /*tp_call*/
1086 0, /*tp_str*/
1087 0, /*tp_getattro*/
1088 0, /*tp_setattro*/
1089 0, /*tp_as_buffer*/
1090 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1091 0, /*tp_doc*/
1092 0, /*tp_traverse*/
1093 0, /*tp_clear*/
1094 0, /*tp_richcompare*/
1095 0, /*tp_weaklistoffset*/
1096 0, /*tp_iter*/
1097 0, /*tp_iternext*/
1098 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001099};
1100
Tim Peters0c322792002-07-17 16:49:03 +00001101static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001102 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001103 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001104 sizeof(compobject),
1105 0,
1106 (destructor)Decomp_dealloc, /*tp_dealloc*/
1107 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001108 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001109 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001110 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001111 0, /*tp_repr*/
1112 0, /*tp_as_number*/
1113 0, /*tp_as_sequence*/
1114 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001115 0, /*tp_hash*/
1116 0, /*tp_call*/
1117 0, /*tp_str*/
1118 0, /*tp_getattro*/
1119 0, /*tp_setattro*/
1120 0, /*tp_as_buffer*/
1121 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1122 0, /*tp_doc*/
1123 0, /*tp_traverse*/
1124 0, /*tp_clear*/
1125 0, /*tp_richcompare*/
1126 0, /*tp_weaklistoffset*/
1127 0, /*tp_iter*/
1128 0, /*tp_iternext*/
1129 Decomp_methods, /*tp_methods*/
1130 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001131};
1132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001133PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001134"The functions in this module allow compression and decompression using the\n"
1135"zlib library, which is based on GNU zip.\n"
1136"\n"
1137"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Nadeem Vawda19e568d2012-11-11 14:04:14 +01001138"compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001139"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001140"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001141"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001142"decompressobj([wbits]) -- Return a decompressor object.\n"
1143"\n"
1144"'wbits' is window buffer size.\n"
1145"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001146"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001147
Martin v. Löwis1a214512008-06-11 05:26:20 +00001148static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001149 PyModuleDef_HEAD_INIT,
1150 "zlib",
1151 zlib_module_documentation,
1152 -1,
1153 zlib_methods,
1154 NULL,
1155 NULL,
1156 NULL,
1157 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001158};
1159
Mark Hammond62b1ab12002-07-23 06:31:15 +00001160PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001161PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001162{
Fred Drake4baedc12002-04-01 14:53:37 +00001163 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001164 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001165 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001166 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001167 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001168 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001169 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001170 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001171
Fred Drake4baedc12002-04-01 14:53:37 +00001172 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1173 if (ZlibError != NULL) {
1174 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001175 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001176 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001177 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1178 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1179 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1180 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1181 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1182 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1183 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1184 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1185 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001186
Jeremy Hylton9714f992001-10-16 21:19:45 +00001187 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1188 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1189 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1190 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001191
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001192 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001193 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001194 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001195
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001196 PyModule_AddStringConstant(m, "__version__", "1.0");
1197
Martin v. Löwis1a214512008-06-11 05:26:20 +00001198 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001199}