blob: a6da056ea1b36237ab6ff4d6c0e33e5abb2cb59b [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;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000048 #ifdef WITH_THREAD
49 PyThread_type_lock lock;
50 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000051} compobject;
52
Jeremy Hylton0965e082001-10-16 21:56:09 +000053static void
54zlib_error(z_stream zst, int err, char *msg)
55{
Nadeem Vawda524148a2011-08-28 11:26:46 +020056 const char *zmsg = Z_NULL;
57 /* In case of a version mismatch, zst.msg won't be initialized.
58 Check for this case first, before looking at zst.msg. */
59 if (err == Z_VERSION_ERROR)
60 zmsg = "library version mismatch";
61 if (zmsg == Z_NULL)
62 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000063 if (zmsg == Z_NULL) {
64 switch (err) {
65 case Z_BUF_ERROR:
66 zmsg = "incomplete or truncated stream";
67 break;
68 case Z_STREAM_ERROR:
69 zmsg = "inconsistent stream state";
70 break;
71 case Z_DATA_ERROR:
72 zmsg = "invalid input data";
73 break;
74 }
75 }
76 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000077 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000078 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000079 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000080}
81
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000082PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000083"compressobj([level]) -- Return a compressor object.\n"
84"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000086
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000087PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000088"decompressobj([wbits]) -- Return a decompressor object.\n"
89"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000091
Guido van Rossumfb221561997-04-29 15:38:09 +000092static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000093newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000094{
Tim Peters977e5402001-10-17 03:57:20 +000095 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000096 self = PyObject_New(compobject, type);
97 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000098 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020099 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000100 self->is_initialised = 0;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000101 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000103 Py_DECREF(self);
104 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000106 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000107 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000108 Py_DECREF(self);
109 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000110 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000111#ifdef WITH_THREAD
112 self->lock = PyThread_allocate_lock();
113#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000115}
116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000117PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000118"compress(string[, level]) -- Returned compressed string.\n"
119"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000120"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000121
Guido van Rossumfb221561997-04-29 15:38:09 +0000122static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000123PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000124{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000126 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200127 Byte *input, *output = NULL;
128 unsigned int length;
129 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000130 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000131
Jeremy Hylton9714f992001-10-16 21:19:45 +0000132 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000133 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000134 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200135
136 if (pinput.len > UINT_MAX) {
137 PyErr_SetString(PyExc_OverflowError,
138 "Size does not fit in an unsigned int");
139 goto error;
140 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000141 input = pinput.buf;
142 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000143
Jeremy Hylton9714f992001-10-16 21:19:45 +0000144 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000145
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000146 output = (Byte*)malloc(zst.avail_out);
147 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000148 PyErr_SetString(PyExc_MemoryError,
149 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200150 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000151 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000152
Jeremy Hylton9714f992001-10-16 21:19:45 +0000153 /* Past the point of no return. From here on out, we need to make sure
154 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000155
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000156 zst.zalloc = (alloc_func)NULL;
157 zst.zfree = (free_func)Z_NULL;
158 zst.next_out = (Byte *)output;
159 zst.next_in = (Byte *)input;
160 zst.avail_in = length;
161 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000162
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000163 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000164 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000166 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000167 PyErr_SetString(PyExc_MemoryError,
168 "Out of memory while compressing data");
169 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000170 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 PyErr_SetString(ZlibError,
172 "Bad compression level");
173 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000174 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000175 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 zlib_error(zst, err, "while compressing data");
177 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000178 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000179
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000180 Py_BEGIN_ALLOW_THREADS;
181 err = deflate(&zst, Z_FINISH);
182 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000183
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000184 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000185 zlib_error(zst, err, "while compressing data");
186 deflateEnd(&zst);
187 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000188 }
Tim Peters977e5402001-10-17 03:57:20 +0000189
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000190 err=deflateEnd(&zst);
191 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000193 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000194 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000195 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000196
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000197 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000198 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000199 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000200
Jeremy Hylton9714f992001-10-16 21:19:45 +0000201 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000202}
203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000204PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000205"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
206"\n"
207"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000209
Guido van Rossumfb221561997-04-29 15:38:09 +0000210static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000211PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000212{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200213 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000214 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000215 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200216 unsigned int length;
217 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000218 int wsize=DEF_WBITS;
219 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000220 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000221
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000222 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000223 &pinput, &wsize, &r_strlen))
224 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200225
226 if (pinput.len > UINT_MAX) {
227 PyErr_SetString(PyExc_OverflowError,
228 "Size does not fit in an unsigned int");
229 goto error;
230 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000231 input = pinput.buf;
232 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000233
Jeremy Hylton9714f992001-10-16 21:19:45 +0000234 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000235 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000236
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 zst.avail_in = length;
238 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000239
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200240 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
241 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000242
Jeremy Hylton9714f992001-10-16 21:19:45 +0000243 zst.zalloc = (alloc_func)NULL;
244 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000245 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000246 zst.next_in = (Byte *)input;
247 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000248
Jeremy Hylton9714f992001-10-16 21:19:45 +0000249 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000250 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000251 break;
Tim Peters977e5402001-10-17 03:57:20 +0000252 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000253 PyErr_SetString(PyExc_MemoryError,
254 "Out of memory while decompressing data");
255 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000256 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000257 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000258 zlib_error(zst, err, "while preparing to decompress data");
259 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000260 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000261
Jeremy Hylton9714f992001-10-16 21:19:45 +0000262 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000263 Py_BEGIN_ALLOW_THREADS
264 err=inflate(&zst, Z_FINISH);
265 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000266
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000267 switch(err) {
268 case(Z_STREAM_END):
269 break;
270 case(Z_BUF_ERROR):
271 /*
272 * If there is at least 1 byte of room according to zst.avail_out
273 * and we get this error, assume that it means zlib cannot
274 * process the inflate call() due to an error in the data.
275 */
276 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000277 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000278 inflateEnd(&zst);
279 goto error;
280 }
281 /* fall through */
282 case(Z_OK):
283 /* need more memory */
284 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
285 inflateEnd(&zst);
286 goto error;
287 }
288 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000289 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000290 zst.avail_out = r_strlen;
291 r_strlen = r_strlen << 1;
292 break;
293 default:
294 inflateEnd(&zst);
295 zlib_error(zst, err, "while decompressing data");
296 goto error;
297 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000298 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000299
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000300 err = inflateEnd(&zst);
301 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200302 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000304 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000305
Gregory P. Smith693fc462008-09-06 20:13:06 +0000306 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000307 goto error;
308
Martin v. Löwis423be952008-08-13 15:53:07 +0000309 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000310 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000311
312 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000313 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000314 Py_XDECREF(result_str);
315 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000316}
317
318static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000319PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000320{
Jeremy Hylton499000002001-10-16 21:59:35 +0000321 compobject *self;
322 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
323 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000324
Jeremy Hylton499000002001-10-16 21:59:35 +0000325 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000326 &memLevel, &strategy))
327 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000328
Jeremy Hylton499000002001-10-16 21:59:35 +0000329 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000330 if (self==NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000332 self->zst.zalloc = (alloc_func)NULL;
333 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000334 self->zst.next_in = NULL;
335 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000336 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
337 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000338 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000339 self->is_initialised = 1;
340 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000341 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000342 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000343 PyErr_SetString(PyExc_MemoryError,
344 "Can't allocate memory for compression object");
345 return NULL;
346 case(Z_STREAM_ERROR):
347 Py_DECREF(self);
348 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
349 return NULL;
350 default:
351 zlib_error(self->zst, err, "while creating compression object");
352 Py_DECREF(self);
353 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000354 }
355}
356
357static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000358PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000359{
Jeremy Hylton499000002001-10-16 21:59:35 +0000360 int wbits=DEF_WBITS, err;
361 compobject *self;
362 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000363 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000364
365 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000366 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000368 self->zst.zalloc = (alloc_func)NULL;
369 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000370 self->zst.next_in = NULL;
371 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000372 err = inflateInit2(&self->zst, wbits);
373 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000374 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000375 self->is_initialised = 1;
376 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000377 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000378 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000379 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
380 return NULL;
381 case (Z_MEM_ERROR):
382 Py_DECREF(self);
383 PyErr_SetString(PyExc_MemoryError,
384 "Can't allocate memory for decompression object");
385 return NULL;
386 default:
387 zlib_error(self->zst, err, "while creating decompression object");
388 Py_DECREF(self);
389 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000390 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000391}
392
393static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000394Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000395{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000396#ifdef WITH_THREAD
397 PyThread_free_lock(self->lock);
398#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000399 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000400 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000401 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000402}
403
404static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000405Comp_dealloc(compobject *self)
406{
407 if (self->is_initialised)
408 deflateEnd(&self->zst);
409 Dealloc(self);
410}
411
412static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000413Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000414{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000415 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000416 inflateEnd(&self->zst);
417 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000418}
419
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000420PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000421"compress(data) -- Return a string containing data compressed.\n"
422"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000423"After calling this function, some of the input data may still\n"
424"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000425"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000426
427
Guido van Rossumfb221561997-04-29 15:38:09 +0000428static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000429PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000430{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200431 int err;
432 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000433 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200434 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000435 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000436 Byte *input;
437 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000438
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000439 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000440 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200441 if (pinput.len > UINT_MAX) {
442 PyErr_SetString(PyExc_OverflowError,
443 "Size does not fit in an unsigned int");
444 goto error_outer;
445 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000446 input = pinput.buf;
447 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000448
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200449 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
450 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000451
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000452 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000453
Jeremy Hylton9714f992001-10-16 21:19:45 +0000454 start_total_out = self->zst.total_out;
455 self->zst.avail_in = inplen;
456 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000457 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000458 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000459
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000460 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000461 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000462 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000463
Jeremy Hylton9714f992001-10-16 21:19:45 +0000464 /* while Z_OK and the output buffer is full, there might be more output,
465 so extend the output buffer and try again */
466 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000467 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000468 Py_DECREF(RetVal);
469 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000470 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000471 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000473 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000474 self->zst.avail_out = length;
475 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000476
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000477 Py_BEGIN_ALLOW_THREADS
478 err = deflate(&(self->zst), Z_NO_FLUSH);
479 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000480 }
Tim Peters977e5402001-10-17 03:57:20 +0000481 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000482 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000483 condition.
484 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000485
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000486 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200487 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000488 Py_DECREF(RetVal);
489 RetVal = NULL;
490 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000491 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000492 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000493 Py_DECREF(RetVal);
494 RetVal = NULL;
495 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000496
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000497 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000498 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200499 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000500 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000501 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000502}
503
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000505"decompress(data, max_length) -- Return a string containing the decompressed\n"
506"version of the data.\n"
507"\n"
508"After calling this function, some of the input data may still be stored in\n"
509"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000510"Call the flush() method to clear these buffers.\n"
511"If the max_length parameter is specified then the return value will be\n"
512"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000513"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000514
Guido van Rossumfb221561997-04-29 15:38:09 +0000515static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000516PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000517{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200518 int err, max_length = 0;
519 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000520 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200521 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000522 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000523 Byte *input;
524 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000525
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000526 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000527 &max_length))
528 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200529 if (pinput.len > UINT_MAX) {
530 PyErr_SetString(PyExc_OverflowError,
531 "Size does not fit in an unsigned int");
532 goto error_outer;
533 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000534 input = pinput.buf;
535 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000536 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000537 PyErr_SetString(PyExc_ValueError,
538 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200539 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000540 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000541
Jeremy Hylton9714f992001-10-16 21:19:45 +0000542 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000543 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000544 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200545 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
546 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000547
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000548 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000549
Jeremy Hylton9714f992001-10-16 21:19:45 +0000550 start_total_out = self->zst.total_out;
551 self->zst.avail_in = inplen;
552 self->zst.next_in = input;
553 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000554 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000555
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000556 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000557 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000558 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000559
Jeremy Hylton9714f992001-10-16 21:19:45 +0000560 /* While Z_OK and the output buffer is full, there might be more output.
561 So extend the output buffer and try again.
562 */
Tim Peters977e5402001-10-17 03:57:20 +0000563 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000564 /* If max_length set, don't continue decompressing if we've already
565 reached the limit.
566 */
567 if (max_length && length >= max_length)
568 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 /* otherwise, ... */
571 old_length = length;
572 length = length << 1;
573 if (max_length && length > max_length)
574 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000575
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000576 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000577 Py_DECREF(RetVal);
578 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000579 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000580 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000581 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000582 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000584
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000585 Py_BEGIN_ALLOW_THREADS
586 err = inflate(&(self->zst), Z_SYNC_FLUSH);
587 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000588 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000589
Jeremy Hylton9714f992001-10-16 21:19:45 +0000590 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200591 /* Not all of the compressed data could be accommodated in a buffer of
592 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000593 Py_DECREF(self->unconsumed_tail);
594 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
595 self->zst.avail_in);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200596 }
597 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
598 /* All of the compressed data was consumed. Clear unconsumed_tail. */
599 Py_DECREF(self->unconsumed_tail);
600 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
601 }
602 if (self->unconsumed_tail == NULL) {
603 Py_DECREF(RetVal);
604 RetVal = NULL;
605 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000606 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000607
Tim Peters977e5402001-10-17 03:57:20 +0000608 /* The end of the compressed data has been reached, so set the
609 unused_data attribute to a string containing the remainder of the
610 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000611 inflateEnd, but the old behaviour of only calling it on flush() is
612 preserved.
613 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000614 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000615 Py_XDECREF(self->unused_data); /* Free original empty string */
616 self->unused_data = PyBytes_FromStringAndSize(
617 (char *)self->zst.next_in, self->zst.avail_in);
618 if (self->unused_data == NULL) {
619 Py_DECREF(RetVal);
620 goto error;
621 }
Nadeem Vawda1c385462011-08-13 15:22:40 +0200622 self->eof = 1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000623 /* We will only get Z_BUF_ERROR if the output buffer was full
624 but there wasn't more output when we tried again, so it is
625 not an error condition.
626 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000627 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200628 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000629 Py_DECREF(RetVal);
630 RetVal = NULL;
631 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000632 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000633
Gregory P. Smith693fc462008-09-06 20:13:06 +0000634 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000635 Py_DECREF(RetVal);
636 RetVal = NULL;
637 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000638
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000639 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000640 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200641 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000642 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000643 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000644}
645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000646PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000647"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000648"\n"
649"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000650"default value used when mode is not specified is Z_FINISH.\n"
651"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000653
Guido van Rossumfb221561997-04-29 15:38:09 +0000654static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000655PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000656{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000657 int err, length = DEFAULTALLOC;
658 PyObject *RetVal;
659 int flushmode = Z_FINISH;
660 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000661
Jeremy Hylton9714f992001-10-16 21:19:45 +0000662 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000663 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000664
Jeremy Hylton9714f992001-10-16 21:19:45 +0000665 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
666 doing any work at all; just return an empty string. */
667 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000668 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000669 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000670
Gregory P. Smith693fc462008-09-06 20:13:06 +0000671 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000672 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000673
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000674 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000675
Jeremy Hylton9714f992001-10-16 21:19:45 +0000676 start_total_out = self->zst.total_out;
677 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000678 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000679 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000680
681 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000682 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000683 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000684
Jeremy Hylton9714f992001-10-16 21:19:45 +0000685 /* while Z_OK and the output buffer is full, there might be more output,
686 so extend the output buffer and try again */
687 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000688 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000689 Py_DECREF(RetVal);
690 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000691 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000692 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000693 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000694 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 self->zst.avail_out = length;
696 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000697
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000698 Py_BEGIN_ALLOW_THREADS
699 err = deflate(&(self->zst), flushmode);
700 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000701 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000702
Jeremy Hylton9714f992001-10-16 21:19:45 +0000703 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000704 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000705 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000706 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000707 err = deflateEnd(&(self->zst));
708 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200709 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 Py_DECREF(RetVal);
711 RetVal = NULL;
712 goto error;
713 }
714 else
715 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000716
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000717 /* We will only get Z_BUF_ERROR if the output buffer was full
718 but there wasn't more output when we tried again, so it is
719 not an error condition.
720 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000721 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000722 zlib_error(self->zst, err, "while flushing");
723 Py_DECREF(RetVal);
724 RetVal = NULL;
725 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000726 }
Tim Peters977e5402001-10-17 03:57:20 +0000727
Gregory P. Smith693fc462008-09-06 20:13:06 +0000728 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000729 Py_DECREF(RetVal);
730 RetVal = NULL;
731 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000732
Tim Peters977e5402001-10-17 03:57:20 +0000733 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000734 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000735
736 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000737}
738
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000739#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000740PyDoc_STRVAR(comp_copy__doc__,
741"copy() -- Return a copy of the compression object.");
742
743static PyObject *
744PyZlib_copy(compobject *self)
745{
746 compobject *retval = NULL;
747 int err;
748
749 retval = newcompobject(&Comptype);
750 if (!retval) return NULL;
751
752 /* Copy the zstream state
753 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
754 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000755 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000756 err = deflateCopy(&retval->zst, &self->zst);
757 switch(err) {
758 case(Z_OK):
759 break;
760 case(Z_STREAM_ERROR):
761 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
762 goto error;
763 case(Z_MEM_ERROR):
764 PyErr_SetString(PyExc_MemoryError,
765 "Can't allocate memory for compression object");
766 goto error;
767 default:
768 zlib_error(self->zst, err, "while copying compression object");
769 goto error;
770 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771 Py_INCREF(self->unused_data);
772 Py_INCREF(self->unconsumed_tail);
773 Py_XDECREF(retval->unused_data);
774 Py_XDECREF(retval->unconsumed_tail);
775 retval->unused_data = self->unused_data;
776 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200777 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000778
779 /* Mark it as being initialized */
780 retval->is_initialised = 1;
781
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000782 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783 return (PyObject *)retval;
784
785error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000786 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 Py_XDECREF(retval);
788 return NULL;
789}
790
791PyDoc_STRVAR(decomp_copy__doc__,
792"copy() -- Return a copy of the decompression object.");
793
794static PyObject *
795PyZlib_uncopy(compobject *self)
796{
797 compobject *retval = NULL;
798 int err;
799
800 retval = newcompobject(&Decomptype);
801 if (!retval) return NULL;
802
803 /* Copy the zstream state
804 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
805 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000806 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807 err = inflateCopy(&retval->zst, &self->zst);
808 switch(err) {
809 case(Z_OK):
810 break;
811 case(Z_STREAM_ERROR):
812 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
813 goto error;
814 case(Z_MEM_ERROR):
815 PyErr_SetString(PyExc_MemoryError,
816 "Can't allocate memory for decompression object");
817 goto error;
818 default:
819 zlib_error(self->zst, err, "while copying decompression object");
820 goto error;
821 }
822
823 Py_INCREF(self->unused_data);
824 Py_INCREF(self->unconsumed_tail);
825 Py_XDECREF(retval->unused_data);
826 Py_XDECREF(retval->unconsumed_tail);
827 retval->unused_data = self->unused_data;
828 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200829 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830
831 /* Mark it as being initialized */
832 retval->is_initialised = 1;
833
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000834 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000835 return (PyObject *)retval;
836
837error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000838 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839 Py_XDECREF(retval);
840 return NULL;
841}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000842#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000843
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000844PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845"flush( [length] ) -- Return a string containing any remaining\n"
846"decompressed data. length, if given, is the initial size of the\n"
847"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000848"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000849"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000850
Guido van Rossumfb221561997-04-29 15:38:09 +0000851static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000852PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000853{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000854 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000855 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000856 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000857
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000858 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000860 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000861 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
862 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000863 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000864 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000865 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000866
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000867
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000868 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000869
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000870 start_total_out = self->zst.total_out;
871 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000872 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000873
874 Py_BEGIN_ALLOW_THREADS
875 err = inflate(&(self->zst), Z_FINISH);
876 Py_END_ALLOW_THREADS
877
878 /* while Z_OK and the output buffer is full, there might be more output,
879 so extend the output buffer and try again */
880 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000882 Py_DECREF(retval);
883 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000885 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000886 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
887 self->zst.avail_out = length;
888 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000889
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000890 Py_BEGIN_ALLOW_THREADS
891 err = inflate(&(self->zst), Z_FINISH);
892 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000893 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000894
Nadeem Vawda3bf71c52011-08-13 15:42:50 +0200895 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000896 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200897 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000898 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200899 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000900 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200901 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 Py_DECREF(retval);
903 retval = NULL;
904 goto error;
905 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000906 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000907 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000908 Py_DECREF(retval);
909 retval = NULL;
910 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000911
912error:
913
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000914 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000915
Jeremy Hylton9714f992001-10-16 21:19:45 +0000916 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000917}
918
919static PyMethodDef comp_methods[] =
920{
Tim Peters977e5402001-10-17 03:57:20 +0000921 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000922 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000923 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000924 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000925#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
927 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000928#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000929 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000930};
931
932static PyMethodDef Decomp_methods[] =
933{
Tim Peters977e5402001-10-17 03:57:20 +0000934 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000935 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000936 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000937 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000938#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
940 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000941#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000942 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000943};
944
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000945#define COMP_OFF(x) offsetof(compobject, x)
946static PyMemberDef Decomp_members[] = {
947 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
948 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +0200949 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000950 {NULL},
951};
Guido van Rossumfb221561997-04-29 15:38:09 +0000952
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000953PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000954"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
955"\n"
956"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000957"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000958
Guido van Rossumfb221561997-04-29 15:38:09 +0000959static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000960PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000961{
Christian Heimescc47b052008-03-25 14:56:36 +0000962 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000963 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000964
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000965 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
966 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000967 /* Releasing the GIL for very small buffers is inefficient
968 and may lower performance */
969 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000970 unsigned char *buf = pbuf.buf;
971 Py_ssize_t len = pbuf.len;
972
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000973 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000974 /* Avoid truncation of length for very large buffers. adler32() takes
975 length as an unsigned int, which may be narrower than Py_ssize_t. */
976 while (len > (size_t) UINT_MAX) {
977 adler32val = adler32(adler32val, buf, UINT_MAX);
978 buf += (size_t) UINT_MAX;
979 len -= (size_t) UINT_MAX;
980 }
981 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000982 Py_END_ALLOW_THREADS
983 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000984 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000985 }
986 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000987 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000988}
Tim Peters977e5402001-10-17 03:57:20 +0000989
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000990PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000991"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
992"\n"
993"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000994"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000995
996static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000997PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000998{
Christian Heimescc47b052008-03-25 14:56:36 +0000999 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +00001000 Py_buffer pbuf;
1001 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001002
Antoine Pitrouc8428d32009-12-14 18:23:30 +00001003 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001004 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001005 /* Releasing the GIL for very small buffers is inefficient
1006 and may lower performance */
1007 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001008 unsigned char *buf = pbuf.buf;
1009 Py_ssize_t len = pbuf.len;
1010
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001011 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001012 /* Avoid truncation of length for very large buffers. crc32() takes
1013 length as an unsigned int, which may be narrower than Py_ssize_t. */
1014 while (len > (size_t) UINT_MAX) {
1015 crc32val = crc32(crc32val, buf, UINT_MAX);
1016 buf += (size_t) UINT_MAX;
1017 len -= (size_t) UINT_MAX;
1018 }
1019 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001020 Py_END_ALLOW_THREADS
1021 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001022 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001023 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001024 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001025 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001026}
Tim Peters977e5402001-10-17 03:57:20 +00001027
Guido van Rossumfb221561997-04-29 15:38:09 +00001028
1029static PyMethodDef zlib_methods[] =
1030{
Tim Peters977e5402001-10-17 03:57:20 +00001031 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001032 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001033 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001034 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001035 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001036 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001037 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1038 crc32__doc__},
1039 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001040 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001041 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001042 decompressobj__doc__},
1043 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001044};
1045
Tim Peters0c322792002-07-17 16:49:03 +00001046static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001047 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001048 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001049 sizeof(compobject),
1050 0,
1051 (destructor)Comp_dealloc, /*tp_dealloc*/
1052 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001053 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001054 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001055 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001056 0, /*tp_repr*/
1057 0, /*tp_as_number*/
1058 0, /*tp_as_sequence*/
1059 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001060 0, /*tp_hash*/
1061 0, /*tp_call*/
1062 0, /*tp_str*/
1063 0, /*tp_getattro*/
1064 0, /*tp_setattro*/
1065 0, /*tp_as_buffer*/
1066 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1067 0, /*tp_doc*/
1068 0, /*tp_traverse*/
1069 0, /*tp_clear*/
1070 0, /*tp_richcompare*/
1071 0, /*tp_weaklistoffset*/
1072 0, /*tp_iter*/
1073 0, /*tp_iternext*/
1074 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001075};
1076
Tim Peters0c322792002-07-17 16:49:03 +00001077static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001078 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001079 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001080 sizeof(compobject),
1081 0,
1082 (destructor)Decomp_dealloc, /*tp_dealloc*/
1083 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001084 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001085 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001086 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001087 0, /*tp_repr*/
1088 0, /*tp_as_number*/
1089 0, /*tp_as_sequence*/
1090 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001091 0, /*tp_hash*/
1092 0, /*tp_call*/
1093 0, /*tp_str*/
1094 0, /*tp_getattro*/
1095 0, /*tp_setattro*/
1096 0, /*tp_as_buffer*/
1097 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1098 0, /*tp_doc*/
1099 0, /*tp_traverse*/
1100 0, /*tp_clear*/
1101 0, /*tp_richcompare*/
1102 0, /*tp_weaklistoffset*/
1103 0, /*tp_iter*/
1104 0, /*tp_iternext*/
1105 Decomp_methods, /*tp_methods*/
1106 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001107};
1108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001110"The functions in this module allow compression and decompression using the\n"
1111"zlib library, which is based on GNU zip.\n"
1112"\n"
1113"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1114"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001115"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001116"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001117"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001118"decompressobj([wbits]) -- Return a decompressor object.\n"
1119"\n"
1120"'wbits' is window buffer size.\n"
1121"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001122"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001123
Martin v. Löwis1a214512008-06-11 05:26:20 +00001124static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001125 PyModuleDef_HEAD_INIT,
1126 "zlib",
1127 zlib_module_documentation,
1128 -1,
1129 zlib_methods,
1130 NULL,
1131 NULL,
1132 NULL,
1133 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001134};
1135
Mark Hammond62b1ab12002-07-23 06:31:15 +00001136PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001137PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001138{
Fred Drake4baedc12002-04-01 14:53:37 +00001139 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001140 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001141 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001142 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001143 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001144 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001145 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001146 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001147
Fred Drake4baedc12002-04-01 14:53:37 +00001148 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1149 if (ZlibError != NULL) {
1150 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001151 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001152 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001153 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1154 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1155 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1156 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1157 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1158 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1159 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1160 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1161 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001162
Jeremy Hylton9714f992001-10-16 21:19:45 +00001163 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1164 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1165 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1166 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001167
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001168 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001169 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001170 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001171
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001172 ver = PyUnicode_FromString(zlibVersion());
1173 if (ver != NULL)
1174 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1175
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001176 PyModule_AddStringConstant(m, "__version__", "1.0");
1177
Martin v. Löwis1a214512008-06-11 05:26:20 +00001178 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001179}