blob: 102740b2b0d805cffa2cadf2e4e08c27446e8aa1 [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;
Nadeem Vawda1c385462011-08-13 15:22:40 +020046 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000047 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020048 PyObject *zdict;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000049 #ifdef WITH_THREAD
50 PyThread_type_lock lock;
51 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000052} compobject;
53
Jeremy Hylton0965e082001-10-16 21:56:09 +000054static void
55zlib_error(z_stream zst, int err, char *msg)
56{
Nadeem Vawda524148a2011-08-28 11:26:46 +020057 const char *zmsg = Z_NULL;
58 /* In case of a version mismatch, zst.msg won't be initialized.
59 Check for this case first, before looking at zst.msg. */
60 if (err == Z_VERSION_ERROR)
61 zmsg = "library version mismatch";
62 if (zmsg == Z_NULL)
63 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000064 if (zmsg == Z_NULL) {
65 switch (err) {
66 case Z_BUF_ERROR:
67 zmsg = "incomplete or truncated stream";
68 break;
69 case Z_STREAM_ERROR:
70 zmsg = "inconsistent stream state";
71 break;
72 case Z_DATA_ERROR:
73 zmsg = "invalid input data";
74 break;
75 }
76 }
77 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000079 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000080 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000081}
82
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083PyDoc_STRVAR(compressobj__doc__,
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020084"compressobj([level[, method[, wbits[, memlevel[, strategy[, zdict]]]]]])\n"
85" -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +000086"\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020087"Optional arg level is the compression level, in 1-9.\n"
88"\n"
89"Optional arg zdict is the predefined compression dictionary - a sequence of\n"
90"bytes containing subsequences that are likely to occur in the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +000091
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000092PyDoc_STRVAR(decompressobj__doc__,
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020093"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +000094"\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020095"Optional arg wbits is the window buffer size.\n"
96"\n"
97"Optional arg zdict is the predefined compression dictionary. This must be\n"
98"the same dictionary as used by the compressor that produced the input data.");
Guido van Rossum3c540301997-06-03 22:21:03 +000099
Guido van Rossumfb221561997-04-29 15:38:09 +0000100static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000101newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000102{
Tim Peters977e5402001-10-17 03:57:20 +0000103 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 self = PyObject_New(compobject, type);
105 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000106 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200107 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200109 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000110 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 Py_DECREF(self);
113 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000115 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000116 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 Py_DECREF(self);
118 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000120#ifdef WITH_THREAD
121 self->lock = PyThread_allocate_lock();
122#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000123 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000124}
125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000126PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000127"compress(string[, level]) -- Returned compressed string.\n"
128"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000129"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000130
Guido van Rossumfb221561997-04-29 15:38:09 +0000131static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000132PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000133{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000134 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000135 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200136 Byte *input, *output = NULL;
137 unsigned int length;
138 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000139 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000140
Jeremy Hylton9714f992001-10-16 21:19:45 +0000141 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000142 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000143 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200144
145 if (pinput.len > UINT_MAX) {
146 PyErr_SetString(PyExc_OverflowError,
147 "Size does not fit in an unsigned int");
148 goto error;
149 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000150 input = pinput.buf;
151 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000152
Jeremy Hylton9714f992001-10-16 21:19:45 +0000153 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000154
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000155 output = (Byte*)malloc(zst.avail_out);
156 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000157 PyErr_SetString(PyExc_MemoryError,
158 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200159 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000160 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000161
Jeremy Hylton9714f992001-10-16 21:19:45 +0000162 /* Past the point of no return. From here on out, we need to make sure
163 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000164
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000165 zst.zalloc = (alloc_func)NULL;
166 zst.zfree = (free_func)Z_NULL;
167 zst.next_out = (Byte *)output;
168 zst.next_in = (Byte *)input;
169 zst.avail_in = length;
170 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000171
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000172 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000173 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000174 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000175 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 PyErr_SetString(PyExc_MemoryError,
177 "Out of memory while compressing data");
178 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000179 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000180 PyErr_SetString(ZlibError,
181 "Bad compression level");
182 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000183 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000184 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000185 zlib_error(zst, err, "while compressing data");
186 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000187 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000188
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000189 Py_BEGIN_ALLOW_THREADS;
190 err = deflate(&zst, Z_FINISH);
191 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000192
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000193 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000194 zlib_error(zst, err, "while compressing data");
195 deflateEnd(&zst);
196 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000197 }
Tim Peters977e5402001-10-17 03:57:20 +0000198
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000199 err=deflateEnd(&zst);
200 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000201 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000202 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000203 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000204 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000205
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000206 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000207 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000208 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000209
Jeremy Hylton9714f992001-10-16 21:19:45 +0000210 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000211}
212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000214"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
215"\n"
216"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000217"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000218
Guido van Rossumfb221561997-04-29 15:38:09 +0000219static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000220PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000221{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200222 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000223 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000224 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200225 unsigned int length;
226 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000227 int wsize=DEF_WBITS;
228 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000229 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000230
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000231 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232 &pinput, &wsize, &r_strlen))
233 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200234
235 if (pinput.len > UINT_MAX) {
236 PyErr_SetString(PyExc_OverflowError,
237 "Size does not fit in an unsigned int");
238 goto error;
239 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000240 input = pinput.buf;
241 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000242
Jeremy Hylton9714f992001-10-16 21:19:45 +0000243 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000244 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000245
Jeremy Hylton9714f992001-10-16 21:19:45 +0000246 zst.avail_in = length;
247 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000248
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200249 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
250 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000251
Jeremy Hylton9714f992001-10-16 21:19:45 +0000252 zst.zalloc = (alloc_func)NULL;
253 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000254 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000255 zst.next_in = (Byte *)input;
256 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000257
Jeremy Hylton9714f992001-10-16 21:19:45 +0000258 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000259 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000260 break;
Tim Peters977e5402001-10-17 03:57:20 +0000261 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000262 PyErr_SetString(PyExc_MemoryError,
263 "Out of memory while decompressing data");
264 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000265 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000266 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000267 zlib_error(zst, err, "while preparing to decompress data");
268 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000269 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000270
Jeremy Hylton9714f992001-10-16 21:19:45 +0000271 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 Py_BEGIN_ALLOW_THREADS
273 err=inflate(&zst, Z_FINISH);
274 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000275
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000276 switch(err) {
277 case(Z_STREAM_END):
278 break;
279 case(Z_BUF_ERROR):
280 /*
281 * If there is at least 1 byte of room according to zst.avail_out
282 * and we get this error, assume that it means zlib cannot
283 * process the inflate call() due to an error in the data.
284 */
285 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000286 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000287 inflateEnd(&zst);
288 goto error;
289 }
290 /* fall through */
291 case(Z_OK):
292 /* need more memory */
293 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
294 inflateEnd(&zst);
295 goto error;
296 }
297 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000298 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000299 zst.avail_out = r_strlen;
300 r_strlen = r_strlen << 1;
301 break;
302 default:
303 inflateEnd(&zst);
304 zlib_error(zst, err, "while decompressing data");
305 goto error;
306 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000307 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000308
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000309 err = inflateEnd(&zst);
310 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200311 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000313 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000314
Gregory P. Smith693fc462008-09-06 20:13:06 +0000315 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000316 goto error;
317
Martin v. Löwis423be952008-08-13 15:53:07 +0000318 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000319 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000320
321 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000322 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000323 Py_XDECREF(result_str);
324 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000325}
326
327static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200328PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000329{
Jeremy Hylton499000002001-10-16 21:59:35 +0000330 compobject *self;
331 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
332 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200333 Py_buffer zdict;
334 static char *kwlist[] = {"level", "method", "wbits",
335 "memLevel", "strategy", "zdict", NULL};
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000336
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200337 zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
338 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
339 kwlist, &level, &method, &wbits,
340 &memLevel, &strategy, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000341 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000342
Jeremy Hylton499000002001-10-16 21:59:35 +0000343 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000344 if (self==NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200345 goto error;
Jeremy Hylton499000002001-10-16 21:59:35 +0000346 self->zst.zalloc = (alloc_func)NULL;
347 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000348 self->zst.next_in = NULL;
349 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000350 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
351 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000352 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000353 self->is_initialised = 1;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200354 if (zdict.buf == NULL) {
355 goto success;
356 } else {
357 err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len);
358 switch (err) {
359 case (Z_OK):
360 goto success;
361 case (Z_STREAM_ERROR):
362 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
363 goto error;
364 default:
365 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
366 goto error;
367 }
368 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000369 case (Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000370 PyErr_SetString(PyExc_MemoryError,
371 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200372 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000374 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200375 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000376 default:
377 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200378 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000379 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200380
381 error:
382 Py_XDECREF(self);
383 self = NULL;
384 success:
385 if (zdict.buf != NULL)
386 PyBuffer_Release(&zdict);
387 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000388}
389
390static PyObject *
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200391PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
Guido van Rossumfb221561997-04-29 15:38:09 +0000392{
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200393 static char *kwlist[] = {"wbits", "zdict", NULL};
Jeremy Hylton499000002001-10-16 21:59:35 +0000394 int wbits=DEF_WBITS, err;
395 compobject *self;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200396 PyObject *zdict=NULL;
397
398 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
399 kwlist, &wbits, &zdict))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000400 return NULL;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200401 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
402 PyErr_SetString(PyExc_TypeError,
403 "zdict argument must support the buffer protocol");
404 return NULL;
405 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000406
407 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000408 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000409 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000410 self->zst.zalloc = (alloc_func)NULL;
411 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000412 self->zst.next_in = NULL;
413 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200414 if (zdict != NULL) {
415 Py_INCREF(zdict);
416 self->zdict = zdict;
417 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000418 err = inflateInit2(&self->zst, wbits);
419 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000420 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000421 self->is_initialised = 1;
422 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000423 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000424 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000425 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
426 return NULL;
427 case (Z_MEM_ERROR):
428 Py_DECREF(self);
429 PyErr_SetString(PyExc_MemoryError,
430 "Can't allocate memory for decompression object");
431 return NULL;
432 default:
433 zlib_error(self->zst, err, "while creating decompression object");
434 Py_DECREF(self);
435 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000436 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000437}
438
439static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000440Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000441{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000442#ifdef WITH_THREAD
443 PyThread_free_lock(self->lock);
444#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000445 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000446 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200447 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000448 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000449}
450
451static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000452Comp_dealloc(compobject *self)
453{
454 if (self->is_initialised)
455 deflateEnd(&self->zst);
456 Dealloc(self);
457}
458
459static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000460Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000461{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000462 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000463 inflateEnd(&self->zst);
464 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000465}
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000468"compress(data) -- Return a string containing data compressed.\n"
469"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000470"After calling this function, some of the input data may still\n"
471"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000472"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000473
474
Guido van Rossumfb221561997-04-29 15:38:09 +0000475static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000476PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000477{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200478 int err;
479 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000480 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200481 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000482 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000483 Byte *input;
484 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000485
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000486 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000487 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200488 if (pinput.len > UINT_MAX) {
489 PyErr_SetString(PyExc_OverflowError,
490 "Size does not fit in an unsigned int");
491 goto error_outer;
492 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000493 input = pinput.buf;
494 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000495
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200496 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
497 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000498
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000499 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000500
Jeremy Hylton9714f992001-10-16 21:19:45 +0000501 start_total_out = self->zst.total_out;
502 self->zst.avail_in = inplen;
503 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000504 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000505 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000506
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000507 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000508 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000509 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000510
Jeremy Hylton9714f992001-10-16 21:19:45 +0000511 /* while Z_OK and the output buffer is full, there might be more output,
512 so extend the output buffer and try again */
513 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000514 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000515 Py_DECREF(RetVal);
516 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000517 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000518 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000519 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000520 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000521 self->zst.avail_out = length;
522 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000523
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000524 Py_BEGIN_ALLOW_THREADS
525 err = deflate(&(self->zst), Z_NO_FLUSH);
526 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000527 }
Tim Peters977e5402001-10-17 03:57:20 +0000528 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000529 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000530 condition.
531 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000532
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000533 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200534 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000535 Py_DECREF(RetVal);
536 RetVal = NULL;
537 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000538 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000539 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000540 Py_DECREF(RetVal);
541 RetVal = NULL;
542 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000543
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000544 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000545 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200546 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000547 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000548 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000549}
550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000551PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000552"decompress(data, max_length) -- Return a string containing the decompressed\n"
553"version of the data.\n"
554"\n"
555"After calling this function, some of the input data may still be stored in\n"
556"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000557"Call the flush() method to clear these buffers.\n"
558"If the max_length parameter is specified then the return value will be\n"
559"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000561
Guido van Rossumfb221561997-04-29 15:38:09 +0000562static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000563PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000564{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200565 int err, max_length = 0;
566 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000567 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200568 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000569 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000570 Byte *input;
571 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000572
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000573 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000574 &max_length))
575 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200576 if (pinput.len > UINT_MAX) {
577 PyErr_SetString(PyExc_OverflowError,
578 "Size does not fit in an unsigned int");
579 goto error_outer;
580 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000581 input = pinput.buf;
582 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000583 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000584 PyErr_SetString(PyExc_ValueError,
585 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200586 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000587 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000588
Jeremy Hylton9714f992001-10-16 21:19:45 +0000589 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000590 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000591 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200592 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
593 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000594
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000595 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000596
Jeremy Hylton9714f992001-10-16 21:19:45 +0000597 start_total_out = self->zst.total_out;
598 self->zst.avail_in = inplen;
599 self->zst.next_in = input;
600 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000601 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000602
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000603 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000604 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000605 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000606
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200607 if (err == Z_NEED_DICT && self->zdict != NULL) {
608 Py_buffer zdict_buf;
609 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
610 Py_DECREF(RetVal);
611 RetVal = NULL;
612 goto error;
613 }
614 err = inflateSetDictionary(&(self->zst), zdict_buf.buf, zdict_buf.len);
615 PyBuffer_Release(&zdict_buf);
616 if (err != Z_OK) {
617 zlib_error(self->zst, err, "while decompressing data");
618 Py_DECREF(RetVal);
619 RetVal = NULL;
620 goto error;
621 }
622 /* repeat the call to inflate! */
623 Py_BEGIN_ALLOW_THREADS
624 err = inflate(&(self->zst), Z_SYNC_FLUSH);
625 Py_END_ALLOW_THREADS
626 }
627
Jeremy Hylton9714f992001-10-16 21:19:45 +0000628 /* While Z_OK and the output buffer is full, there might be more output.
629 So extend the output buffer and try again.
630 */
Tim Peters977e5402001-10-17 03:57:20 +0000631 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000632 /* If max_length set, don't continue decompressing if we've already
633 reached the limit.
634 */
635 if (max_length && length >= max_length)
636 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000638 /* otherwise, ... */
639 old_length = length;
640 length = length << 1;
641 if (max_length && length > max_length)
642 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000644 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000645 Py_DECREF(RetVal);
646 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000647 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000648 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000649 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000650 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000651 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000652
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000653 Py_BEGIN_ALLOW_THREADS
654 err = inflate(&(self->zst), Z_SYNC_FLUSH);
655 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000656 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000657
Jeremy Hylton9714f992001-10-16 21:19:45 +0000658 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200659 /* Not all of the compressed data could be accommodated in a buffer of
660 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000661 Py_DECREF(self->unconsumed_tail);
662 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
663 self->zst.avail_in);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200664 }
665 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
666 /* All of the compressed data was consumed. Clear unconsumed_tail. */
667 Py_DECREF(self->unconsumed_tail);
668 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
669 }
670 if (self->unconsumed_tail == NULL) {
671 Py_DECREF(RetVal);
672 RetVal = NULL;
673 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000674 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000675
Tim Peters977e5402001-10-17 03:57:20 +0000676 /* The end of the compressed data has been reached, so set the
677 unused_data attribute to a string containing the remainder of the
678 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000679 inflateEnd, but the old behaviour of only calling it on flush() is
680 preserved.
681 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000682 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000683 Py_XDECREF(self->unused_data); /* Free original empty string */
684 self->unused_data = PyBytes_FromStringAndSize(
685 (char *)self->zst.next_in, self->zst.avail_in);
686 if (self->unused_data == NULL) {
687 Py_DECREF(RetVal);
688 goto error;
689 }
Nadeem Vawda1c385462011-08-13 15:22:40 +0200690 self->eof = 1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000691 /* We will only get Z_BUF_ERROR if the output buffer was full
692 but there wasn't more output when we tried again, so it is
693 not an error condition.
694 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000695 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200696 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000697 Py_DECREF(RetVal);
698 RetVal = NULL;
699 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000700 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000701
Gregory P. Smith693fc462008-09-06 20:13:06 +0000702 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000703 Py_DECREF(RetVal);
704 RetVal = NULL;
705 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000706
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000707 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000708 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200709 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000710 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000711 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000712}
713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000714PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000715"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000716"\n"
717"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000718"default value used when mode is not specified is Z_FINISH.\n"
719"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000720"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000721
Guido van Rossumfb221561997-04-29 15:38:09 +0000722static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000723PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000724{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000725 int err, length = DEFAULTALLOC;
726 PyObject *RetVal;
727 int flushmode = Z_FINISH;
728 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000729
Jeremy Hylton9714f992001-10-16 21:19:45 +0000730 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000731 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000732
Jeremy Hylton9714f992001-10-16 21:19:45 +0000733 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
734 doing any work at all; just return an empty string. */
735 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000736 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000737 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000738
Gregory P. Smith693fc462008-09-06 20:13:06 +0000739 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000740 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000741
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000742 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000743
Jeremy Hylton9714f992001-10-16 21:19:45 +0000744 start_total_out = self->zst.total_out;
745 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000746 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000747 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000748
749 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000750 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000751 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000752
Jeremy Hylton9714f992001-10-16 21:19:45 +0000753 /* while Z_OK and the output buffer is full, there might be more output,
754 so extend the output buffer and try again */
755 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000756 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000757 Py_DECREF(RetVal);
758 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000759 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000760 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000761 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000762 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000763 self->zst.avail_out = length;
764 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000765
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000766 Py_BEGIN_ALLOW_THREADS
767 err = deflate(&(self->zst), flushmode);
768 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000769 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000770
Jeremy Hylton9714f992001-10-16 21:19:45 +0000771 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000772 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000773 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000774 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000775 err = deflateEnd(&(self->zst));
776 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200777 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000778 Py_DECREF(RetVal);
779 RetVal = NULL;
780 goto error;
781 }
782 else
783 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000784
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000785 /* We will only get Z_BUF_ERROR if the output buffer was full
786 but there wasn't more output when we tried again, so it is
787 not an error condition.
788 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000789 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000790 zlib_error(self->zst, err, "while flushing");
791 Py_DECREF(RetVal);
792 RetVal = NULL;
793 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000794 }
Tim Peters977e5402001-10-17 03:57:20 +0000795
Gregory P. Smith693fc462008-09-06 20:13:06 +0000796 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000797 Py_DECREF(RetVal);
798 RetVal = NULL;
799 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000800
Tim Peters977e5402001-10-17 03:57:20 +0000801 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000802 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000803
804 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000805}
806
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000807#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808PyDoc_STRVAR(comp_copy__doc__,
809"copy() -- Return a copy of the compression object.");
810
811static PyObject *
812PyZlib_copy(compobject *self)
813{
814 compobject *retval = NULL;
815 int err;
816
817 retval = newcompobject(&Comptype);
818 if (!retval) return NULL;
819
820 /* Copy the zstream state
821 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
822 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000823 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824 err = deflateCopy(&retval->zst, &self->zst);
825 switch(err) {
826 case(Z_OK):
827 break;
828 case(Z_STREAM_ERROR):
829 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
830 goto error;
831 case(Z_MEM_ERROR):
832 PyErr_SetString(PyExc_MemoryError,
833 "Can't allocate memory for compression object");
834 goto error;
835 default:
836 zlib_error(self->zst, err, "while copying compression object");
837 goto error;
838 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 Py_INCREF(self->unused_data);
840 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200841 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000842 Py_XDECREF(retval->unused_data);
843 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200844 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845 retval->unused_data = self->unused_data;
846 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200847 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200848 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000849
850 /* Mark it as being initialized */
851 retval->is_initialised = 1;
852
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000853 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854 return (PyObject *)retval;
855
856error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000857 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000858 Py_XDECREF(retval);
859 return NULL;
860}
861
862PyDoc_STRVAR(decomp_copy__doc__,
863"copy() -- Return a copy of the decompression object.");
864
865static PyObject *
866PyZlib_uncopy(compobject *self)
867{
868 compobject *retval = NULL;
869 int err;
870
871 retval = newcompobject(&Decomptype);
872 if (!retval) return NULL;
873
874 /* Copy the zstream state
875 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
876 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000877 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000878 err = inflateCopy(&retval->zst, &self->zst);
879 switch(err) {
880 case(Z_OK):
881 break;
882 case(Z_STREAM_ERROR):
883 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
884 goto error;
885 case(Z_MEM_ERROR):
886 PyErr_SetString(PyExc_MemoryError,
887 "Can't allocate memory for decompression object");
888 goto error;
889 default:
890 zlib_error(self->zst, err, "while copying decompression object");
891 goto error;
892 }
893
894 Py_INCREF(self->unused_data);
895 Py_INCREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200896 Py_XINCREF(self->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000897 Py_XDECREF(retval->unused_data);
898 Py_XDECREF(retval->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200899 Py_XDECREF(retval->zdict);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900 retval->unused_data = self->unused_data;
901 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200902 retval->zdict = self->zdict;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200903 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904
905 /* Mark it as being initialized */
906 retval->is_initialised = 1;
907
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000908 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909 return (PyObject *)retval;
910
911error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000912 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000913 Py_XDECREF(retval);
914 return NULL;
915}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000916#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000918PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919"flush( [length] ) -- Return a string containing any remaining\n"
920"decompressed data. length, if given, is the initial size of the\n"
921"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000922"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000923"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000924
Guido van Rossumfb221561997-04-29 15:38:09 +0000925static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000926PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000927{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000928 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000929 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000930 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000931
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000932 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000933 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000934 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000935 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
936 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000937 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000938 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000939 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000940
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000941
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000942 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000943
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000944 start_total_out = self->zst.total_out;
945 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000946 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000947
948 Py_BEGIN_ALLOW_THREADS
949 err = inflate(&(self->zst), Z_FINISH);
950 Py_END_ALLOW_THREADS
951
952 /* while Z_OK and the output buffer is full, there might be more output,
953 so extend the output buffer and try again */
954 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000955 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000956 Py_DECREF(retval);
957 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000958 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000959 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000960 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
961 self->zst.avail_out = length;
962 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000963
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000964 Py_BEGIN_ALLOW_THREADS
965 err = inflate(&(self->zst), Z_FINISH);
966 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000967 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000968
Nadeem Vawda3bf71c52011-08-13 15:42:50 +0200969 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000970 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200971 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000972 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200973 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000974 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200975 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000976 Py_DECREF(retval);
977 retval = NULL;
978 goto error;
979 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000980 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000981 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000982 Py_DECREF(retval);
983 retval = NULL;
984 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000985
986error:
987
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000988 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000989
Jeremy Hylton9714f992001-10-16 21:19:45 +0000990 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000991}
992
993static PyMethodDef comp_methods[] =
994{
Tim Peters977e5402001-10-17 03:57:20 +0000995 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000996 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000997 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000998 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000999#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
1001 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001002#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001003 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001004};
1005
1006static PyMethodDef Decomp_methods[] =
1007{
Tim Peters977e5402001-10-17 03:57:20 +00001008 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001009 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001010 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001011 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001012#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +00001013 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1014 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +00001016 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001017};
1018
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001019#define COMP_OFF(x) offsetof(compobject, x)
1020static PyMemberDef Decomp_members[] = {
1021 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1022 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001023 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001024 {NULL},
1025};
Guido van Rossumfb221561997-04-29 15:38:09 +00001026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001027PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001028"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1029"\n"
1030"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001031"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +00001032
Guido van Rossumfb221561997-04-29 15:38:09 +00001033static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001034PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001035{
Christian Heimescc47b052008-03-25 14:56:36 +00001036 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001037 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +00001038
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001039 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1040 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001041 /* Releasing the GIL for very small buffers is inefficient
1042 and may lower performance */
1043 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001044 unsigned char *buf = pbuf.buf;
1045 Py_ssize_t len = pbuf.len;
1046
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001047 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001048 /* Avoid truncation of length for very large buffers. adler32() takes
1049 length as an unsigned int, which may be narrower than Py_ssize_t. */
1050 while (len > (size_t) UINT_MAX) {
1051 adler32val = adler32(adler32val, buf, UINT_MAX);
1052 buf += (size_t) UINT_MAX;
1053 len -= (size_t) UINT_MAX;
1054 }
1055 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001056 Py_END_ALLOW_THREADS
1057 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001058 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001059 }
1060 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +00001061 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001062}
Tim Peters977e5402001-10-17 03:57:20 +00001063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001064PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +00001065"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1066"\n"
1067"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001068"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +00001069
1070static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +00001071PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +00001072{
Christian Heimescc47b052008-03-25 14:56:36 +00001073 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001074 Py_buffer pbuf;
1075 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001076
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001077 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001078 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001079 /* Releasing the GIL for very small buffers is inefficient
1080 and may lower performance */
1081 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001082 unsigned char *buf = pbuf.buf;
1083 Py_ssize_t len = pbuf.len;
1084
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001085 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001086 /* Avoid truncation of length for very large buffers. crc32() takes
1087 length as an unsigned int, which may be narrower than Py_ssize_t. */
1088 while (len > (size_t) UINT_MAX) {
1089 crc32val = crc32(crc32val, buf, UINT_MAX);
1090 buf += (size_t) UINT_MAX;
1091 len -= (size_t) UINT_MAX;
1092 }
1093 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001094 Py_END_ALLOW_THREADS
1095 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001096 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001097 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001098 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001099 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001100}
Tim Peters977e5402001-10-17 03:57:20 +00001101
Guido van Rossumfb221561997-04-29 15:38:09 +00001102
1103static PyMethodDef zlib_methods[] =
1104{
Tim Peters977e5402001-10-17 03:57:20 +00001105 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001106 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001107 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001108 compress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001109 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001110 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001111 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1112 crc32__doc__},
1113 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001114 decompress__doc__},
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001115 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001116 decompressobj__doc__},
1117 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001118};
1119
Tim Peters0c322792002-07-17 16:49:03 +00001120static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001121 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001122 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001123 sizeof(compobject),
1124 0,
1125 (destructor)Comp_dealloc, /*tp_dealloc*/
1126 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001127 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001128 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001129 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001130 0, /*tp_repr*/
1131 0, /*tp_as_number*/
1132 0, /*tp_as_sequence*/
1133 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001134 0, /*tp_hash*/
1135 0, /*tp_call*/
1136 0, /*tp_str*/
1137 0, /*tp_getattro*/
1138 0, /*tp_setattro*/
1139 0, /*tp_as_buffer*/
1140 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1141 0, /*tp_doc*/
1142 0, /*tp_traverse*/
1143 0, /*tp_clear*/
1144 0, /*tp_richcompare*/
1145 0, /*tp_weaklistoffset*/
1146 0, /*tp_iter*/
1147 0, /*tp_iternext*/
1148 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001149};
1150
Tim Peters0c322792002-07-17 16:49:03 +00001151static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001152 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001153 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001154 sizeof(compobject),
1155 0,
1156 (destructor)Decomp_dealloc, /*tp_dealloc*/
1157 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001158 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001159 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001160 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001161 0, /*tp_repr*/
1162 0, /*tp_as_number*/
1163 0, /*tp_as_sequence*/
1164 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001165 0, /*tp_hash*/
1166 0, /*tp_call*/
1167 0, /*tp_str*/
1168 0, /*tp_getattro*/
1169 0, /*tp_setattro*/
1170 0, /*tp_as_buffer*/
1171 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1172 0, /*tp_doc*/
1173 0, /*tp_traverse*/
1174 0, /*tp_clear*/
1175 0, /*tp_richcompare*/
1176 0, /*tp_weaklistoffset*/
1177 0, /*tp_iter*/
1178 0, /*tp_iternext*/
1179 Decomp_methods, /*tp_methods*/
1180 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001181};
1182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001183PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001184"The functions in this module allow compression and decompression using the\n"
1185"zlib library, which is based on GNU zip.\n"
1186"\n"
1187"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1188"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001189"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001190"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001191"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001192"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001193"\n"
1194"'wbits' is window buffer size.\n"
1195"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001196"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001197
Martin v. Löwis1a214512008-06-11 05:26:20 +00001198static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001199 PyModuleDef_HEAD_INIT,
1200 "zlib",
1201 zlib_module_documentation,
1202 -1,
1203 zlib_methods,
1204 NULL,
1205 NULL,
1206 NULL,
1207 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001208};
1209
Mark Hammond62b1ab12002-07-23 06:31:15 +00001210PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001211PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001212{
Fred Drake4baedc12002-04-01 14:53:37 +00001213 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001214 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001215 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001216 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001217 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001218 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001219 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001220 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001221
Fred Drake4baedc12002-04-01 14:53:37 +00001222 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1223 if (ZlibError != NULL) {
1224 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001225 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001226 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001227 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1228 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1229 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1230 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1231 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1232 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1233 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1234 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1235 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001236
Jeremy Hylton9714f992001-10-16 21:19:45 +00001237 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1238 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1239 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1240 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001241
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001242 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001243 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001244 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001245
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001246 ver = PyUnicode_FromString(zlibVersion());
1247 if (ver != NULL)
1248 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1249
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001250 PyModule_AddStringConstant(m, "__version__", "1.0");
1251
Martin v. Löwis1a214512008-06-11 05:26:20 +00001252 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001253}