blob: 711004e63f94ba74ec68a6175f9bf4294e2bb9f8 [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{
Antoine Pitrou96f212b2010-05-11 23:49:58 +000056 const char *zmsg = zst.msg;
57 if (zmsg == Z_NULL) {
58 switch (err) {
59 case Z_BUF_ERROR:
60 zmsg = "incomplete or truncated stream";
61 break;
62 case Z_STREAM_ERROR:
63 zmsg = "inconsistent stream state";
64 break;
65 case Z_DATA_ERROR:
66 zmsg = "invalid input data";
67 break;
68 }
69 }
70 if (zmsg == Z_NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000071 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000072 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +000073 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000074}
75
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000076PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000077"compressobj([level]) -- Return a compressor object.\n"
78"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000079"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000080
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000082"decompressobj([wbits]) -- Return a decompressor object.\n"
83"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000085
Guido van Rossumfb221561997-04-29 15:38:09 +000086static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000087newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000088{
Tim Peters977e5402001-10-17 03:57:20 +000089 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000090 self = PyObject_New(compobject, type);
91 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000092 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +020093 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +000094 self->is_initialised = 0;
Gregory P. Smith693fc462008-09-06 20:13:06 +000095 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000096 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000097 Py_DECREF(self);
98 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000100 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000101 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000102 Py_DECREF(self);
103 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000105#ifdef WITH_THREAD
106 self->lock = PyThread_allocate_lock();
107#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000109}
110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000111PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000112"compress(string[, level]) -- Returned compressed string.\n"
113"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000114"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000115
Guido van Rossumfb221561997-04-29 15:38:09 +0000116static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000117PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000118{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000120 Py_buffer pinput;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200121 Byte *input, *output = NULL;
122 unsigned int length;
123 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000124 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000125
Jeremy Hylton9714f992001-10-16 21:19:45 +0000126 /* require Python string object, optional 'level' arg */
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000127 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000128 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200129
130 if (pinput.len > UINT_MAX) {
131 PyErr_SetString(PyExc_OverflowError,
132 "Size does not fit in an unsigned int");
133 goto error;
134 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000135 input = pinput.buf;
136 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000137
Jeremy Hylton9714f992001-10-16 21:19:45 +0000138 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000139
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000140 output = (Byte*)malloc(zst.avail_out);
141 if (output == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000142 PyErr_SetString(PyExc_MemoryError,
143 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200144 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000145 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000146
Jeremy Hylton9714f992001-10-16 21:19:45 +0000147 /* Past the point of no return. From here on out, we need to make sure
148 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000149
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000150 zst.zalloc = (alloc_func)NULL;
151 zst.zfree = (free_func)Z_NULL;
152 zst.next_out = (Byte *)output;
153 zst.next_in = (Byte *)input;
154 zst.avail_in = length;
155 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000156
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000157 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000158 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000159 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000160 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 PyErr_SetString(PyExc_MemoryError,
162 "Out of memory while compressing data");
163 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000164 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000165 PyErr_SetString(ZlibError,
166 "Bad compression level");
167 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000168 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000169 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000170 zlib_error(zst, err, "while compressing data");
171 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000172 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000173
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000174 Py_BEGIN_ALLOW_THREADS;
175 err = deflate(&zst, Z_FINISH);
176 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000177
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000178 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000179 zlib_error(zst, err, "while compressing data");
180 deflateEnd(&zst);
181 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 }
Tim Peters977e5402001-10-17 03:57:20 +0000183
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000184 err=deflateEnd(&zst);
185 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000186 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000187 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000188 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000190
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000191 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000192 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000193 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000194
Jeremy Hylton9714f992001-10-16 21:19:45 +0000195 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000196}
197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000199"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
200"\n"
201"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000202"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000203
Guido van Rossumfb221561997-04-29 15:38:09 +0000204static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000205PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000206{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200207 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000208 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000209 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200210 unsigned int length;
211 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000212 int wsize=DEF_WBITS;
213 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000214 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000215
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000216 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 &pinput, &wsize, &r_strlen))
218 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200219
220 if (pinput.len > UINT_MAX) {
221 PyErr_SetString(PyExc_OverflowError,
222 "Size does not fit in an unsigned int");
223 goto error;
224 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000225 input = pinput.buf;
226 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000227
Jeremy Hylton9714f992001-10-16 21:19:45 +0000228 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000229 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000230
Jeremy Hylton9714f992001-10-16 21:19:45 +0000231 zst.avail_in = length;
232 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000233
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200234 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
235 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000236
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 zst.zalloc = (alloc_func)NULL;
238 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000239 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000240 zst.next_in = (Byte *)input;
241 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000242
Jeremy Hylton9714f992001-10-16 21:19:45 +0000243 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 break;
Tim Peters977e5402001-10-17 03:57:20 +0000246 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000247 PyErr_SetString(PyExc_MemoryError,
248 "Out of memory while decompressing data");
249 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000250 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000251 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252 zlib_error(zst, err, "while preparing to decompress data");
253 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000254 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000255
Jeremy Hylton9714f992001-10-16 21:19:45 +0000256 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000257 Py_BEGIN_ALLOW_THREADS
258 err=inflate(&zst, Z_FINISH);
259 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000260
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000261 switch(err) {
262 case(Z_STREAM_END):
263 break;
264 case(Z_BUF_ERROR):
265 /*
266 * If there is at least 1 byte of room according to zst.avail_out
267 * and we get this error, assume that it means zlib cannot
268 * process the inflate call() due to an error in the data.
269 */
270 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000271 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 inflateEnd(&zst);
273 goto error;
274 }
275 /* fall through */
276 case(Z_OK):
277 /* need more memory */
278 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
279 inflateEnd(&zst);
280 goto error;
281 }
282 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000283 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000284 zst.avail_out = r_strlen;
285 r_strlen = r_strlen << 1;
286 break;
287 default:
288 inflateEnd(&zst);
289 zlib_error(zst, err, "while decompressing data");
290 goto error;
291 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000292 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000293
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000294 err = inflateEnd(&zst);
295 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200296 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000297 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000298 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000299
Gregory P. Smith693fc462008-09-06 20:13:06 +0000300 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000301 goto error;
302
Martin v. Löwis423be952008-08-13 15:53:07 +0000303 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000304 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000305
306 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000307 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000308 Py_XDECREF(result_str);
309 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000310}
311
312static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000313PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000314{
Jeremy Hylton499000002001-10-16 21:59:35 +0000315 compobject *self;
316 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
317 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000318
Jeremy Hylton499000002001-10-16 21:59:35 +0000319 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000320 &memLevel, &strategy))
321 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000322
Jeremy Hylton499000002001-10-16 21:59:35 +0000323 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000324 if (self==NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000325 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000326 self->zst.zalloc = (alloc_func)NULL;
327 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000328 self->zst.next_in = NULL;
329 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000330 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
331 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000332 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000333 self->is_initialised = 1;
334 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000335 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000336 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 PyErr_SetString(PyExc_MemoryError,
338 "Can't allocate memory for compression object");
339 return NULL;
340 case(Z_STREAM_ERROR):
341 Py_DECREF(self);
342 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
343 return NULL;
344 default:
345 zlib_error(self->zst, err, "while creating compression object");
346 Py_DECREF(self);
347 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000348 }
349}
350
351static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000352PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000353{
Jeremy Hylton499000002001-10-16 21:59:35 +0000354 int wbits=DEF_WBITS, err;
355 compobject *self;
356 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000357 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000358
359 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000360 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000361 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000362 self->zst.zalloc = (alloc_func)NULL;
363 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000364 self->zst.next_in = NULL;
365 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000366 err = inflateInit2(&self->zst, wbits);
367 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000368 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000369 self->is_initialised = 1;
370 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000371 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000372 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
374 return NULL;
375 case (Z_MEM_ERROR):
376 Py_DECREF(self);
377 PyErr_SetString(PyExc_MemoryError,
378 "Can't allocate memory for decompression object");
379 return NULL;
380 default:
381 zlib_error(self->zst, err, "while creating decompression object");
382 Py_DECREF(self);
383 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000384 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000385}
386
387static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000388Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000389{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000390#ifdef WITH_THREAD
391 PyThread_free_lock(self->lock);
392#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000393 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000394 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000395 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000396}
397
398static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000399Comp_dealloc(compobject *self)
400{
401 if (self->is_initialised)
402 deflateEnd(&self->zst);
403 Dealloc(self);
404}
405
406static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000407Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000408{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000409 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000410 inflateEnd(&self->zst);
411 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000412}
413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000414PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000415"compress(data) -- Return a string containing data compressed.\n"
416"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000417"After calling this function, some of the input data may still\n"
418"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000419"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000420
421
Guido van Rossumfb221561997-04-29 15:38:09 +0000422static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000423PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000424{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200425 int err;
426 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000427 Py_ssize_t length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200428 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000429 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000430 Byte *input;
431 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000432
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000433 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000434 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200435 if (pinput.len > UINT_MAX) {
436 PyErr_SetString(PyExc_OverflowError,
437 "Size does not fit in an unsigned int");
438 goto error_outer;
439 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000440 input = pinput.buf;
441 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000442
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200443 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
444 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000445
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000446 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000447
Jeremy Hylton9714f992001-10-16 21:19:45 +0000448 start_total_out = self->zst.total_out;
449 self->zst.avail_in = inplen;
450 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000451 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000452 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000453
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000454 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000455 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000456 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000457
Jeremy Hylton9714f992001-10-16 21:19:45 +0000458 /* while Z_OK and the output buffer is full, there might be more output,
459 so extend the output buffer and try again */
460 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000461 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000462 Py_DECREF(RetVal);
463 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000465 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000466 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000467 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000468 self->zst.avail_out = length;
469 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000470
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000471 Py_BEGIN_ALLOW_THREADS
472 err = deflate(&(self->zst), Z_NO_FLUSH);
473 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000474 }
Tim Peters977e5402001-10-17 03:57:20 +0000475 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000476 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000477 condition.
478 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000479
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000480 if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200481 zlib_error(self->zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 Py_DECREF(RetVal);
483 RetVal = NULL;
484 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000485 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000486 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000487 Py_DECREF(RetVal);
488 RetVal = NULL;
489 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000490
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000491 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000492 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200493 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000494 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000495 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000496}
497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000498PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000499"decompress(data, max_length) -- Return a string containing the decompressed\n"
500"version of the data.\n"
501"\n"
502"After calling this function, some of the input data may still be stored in\n"
503"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000504"Call the flush() method to clear these buffers.\n"
505"If the max_length parameter is specified then the return value will be\n"
506"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000507"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000508
Guido van Rossumfb221561997-04-29 15:38:09 +0000509static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000510PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000511{
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200512 int err, max_length = 0;
513 unsigned int inplen;
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000514 Py_ssize_t old_length, length = DEFAULTALLOC;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200515 PyObject *RetVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000516 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000517 Byte *input;
518 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000519
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000520 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000521 &max_length))
522 return NULL;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200523 if (pinput.len > UINT_MAX) {
524 PyErr_SetString(PyExc_OverflowError,
525 "Size does not fit in an unsigned int");
526 goto error_outer;
527 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000528 input = pinput.buf;
529 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000530 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000531 PyErr_SetString(PyExc_ValueError,
532 "max_length must be greater than zero");
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200533 goto error_outer;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000534 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000535
Jeremy Hylton9714f992001-10-16 21:19:45 +0000536 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000537 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000538 length = max_length;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200539 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
540 goto error_outer;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000541
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000542 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000543
Jeremy Hylton9714f992001-10-16 21:19:45 +0000544 start_total_out = self->zst.total_out;
545 self->zst.avail_in = inplen;
546 self->zst.next_in = input;
547 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000548 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000549
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000550 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000551 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000552 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000553
Jeremy Hylton9714f992001-10-16 21:19:45 +0000554 /* While Z_OK and the output buffer is full, there might be more output.
555 So extend the output buffer and try again.
556 */
Tim Peters977e5402001-10-17 03:57:20 +0000557 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000558 /* If max_length set, don't continue decompressing if we've already
559 reached the limit.
560 */
561 if (max_length && length >= max_length)
562 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000563
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000564 /* otherwise, ... */
565 old_length = length;
566 length = length << 1;
567 if (max_length && length > max_length)
568 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000571 Py_DECREF(RetVal);
572 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000573 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000574 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000576 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000578
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000579 Py_BEGIN_ALLOW_THREADS
580 err = inflate(&(self->zst), Z_SYNC_FLUSH);
581 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000582 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000583
Jeremy Hylton9714f992001-10-16 21:19:45 +0000584 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200585 /* Not all of the compressed data could be accommodated in a buffer of
586 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000587 Py_DECREF(self->unconsumed_tail);
588 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
589 self->zst.avail_in);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200590 }
591 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
592 /* All of the compressed data was consumed. Clear unconsumed_tail. */
593 Py_DECREF(self->unconsumed_tail);
594 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
595 }
596 if (self->unconsumed_tail == NULL) {
597 Py_DECREF(RetVal);
598 RetVal = NULL;
599 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000600 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000601
Tim Peters977e5402001-10-17 03:57:20 +0000602 /* The end of the compressed data has been reached, so set the
603 unused_data attribute to a string containing the remainder of the
604 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000605 inflateEnd, but the old behaviour of only calling it on flush() is
606 preserved.
607 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000608 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000609 Py_XDECREF(self->unused_data); /* Free original empty string */
610 self->unused_data = PyBytes_FromStringAndSize(
611 (char *)self->zst.next_in, self->zst.avail_in);
612 if (self->unused_data == NULL) {
613 Py_DECREF(RetVal);
614 goto error;
615 }
Nadeem Vawda1c385462011-08-13 15:22:40 +0200616 self->eof = 1;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 /* We will only get Z_BUF_ERROR if the output buffer was full
618 but there wasn't more output when we tried again, so it is
619 not an error condition.
620 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000621 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200622 zlib_error(self->zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000623 Py_DECREF(RetVal);
624 RetVal = NULL;
625 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000626 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000627
Gregory P. Smith693fc462008-09-06 20:13:06 +0000628 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000629 Py_DECREF(RetVal);
630 RetVal = NULL;
631 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000632
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000633 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000634 LEAVE_ZLIB(self);
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200635 error_outer:
Martin v. Löwis423be952008-08-13 15:53:07 +0000636 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000637 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000638}
639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000640PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000641"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000642"\n"
643"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000644"default value used when mode is not specified is Z_FINISH.\n"
645"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000646"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000647
Guido van Rossumfb221561997-04-29 15:38:09 +0000648static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000649PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000650{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000651 int err, length = DEFAULTALLOC;
652 PyObject *RetVal;
653 int flushmode = Z_FINISH;
654 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000655
Jeremy Hylton9714f992001-10-16 21:19:45 +0000656 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000657 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000658
Jeremy Hylton9714f992001-10-16 21:19:45 +0000659 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
660 doing any work at all; just return an empty string. */
661 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000663 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000664
Gregory P. Smith693fc462008-09-06 20:13:06 +0000665 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000667
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000668 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000669
Jeremy Hylton9714f992001-10-16 21:19:45 +0000670 start_total_out = self->zst.total_out;
671 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000672 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000673 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000674
675 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000676 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000677 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000678
Jeremy Hylton9714f992001-10-16 21:19:45 +0000679 /* while Z_OK and the output buffer is full, there might be more output,
680 so extend the output buffer and try again */
681 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000682 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000683 Py_DECREF(RetVal);
684 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000685 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000686 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000687 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000688 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000689 self->zst.avail_out = length;
690 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000691
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000692 Py_BEGIN_ALLOW_THREADS
693 err = deflate(&(self->zst), flushmode);
694 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000695 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000696
Jeremy Hylton9714f992001-10-16 21:19:45 +0000697 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000698 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000699 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000700 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000701 err = deflateEnd(&(self->zst));
702 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200703 zlib_error(self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000704 Py_DECREF(RetVal);
705 RetVal = NULL;
706 goto error;
707 }
708 else
709 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000710
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000711 /* We will only get Z_BUF_ERROR if the output buffer was full
712 but there wasn't more output when we tried again, so it is
713 not an error condition.
714 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000715 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000716 zlib_error(self->zst, err, "while flushing");
717 Py_DECREF(RetVal);
718 RetVal = NULL;
719 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000720 }
Tim Peters977e5402001-10-17 03:57:20 +0000721
Gregory P. Smith693fc462008-09-06 20:13:06 +0000722 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000723 Py_DECREF(RetVal);
724 RetVal = NULL;
725 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000726
Tim Peters977e5402001-10-17 03:57:20 +0000727 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000728 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000729
730 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000731}
732
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000733#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000734PyDoc_STRVAR(comp_copy__doc__,
735"copy() -- Return a copy of the compression object.");
736
737static PyObject *
738PyZlib_copy(compobject *self)
739{
740 compobject *retval = NULL;
741 int err;
742
743 retval = newcompobject(&Comptype);
744 if (!retval) return NULL;
745
746 /* Copy the zstream state
747 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
748 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000749 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000750 err = deflateCopy(&retval->zst, &self->zst);
751 switch(err) {
752 case(Z_OK):
753 break;
754 case(Z_STREAM_ERROR):
755 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
756 goto error;
757 case(Z_MEM_ERROR):
758 PyErr_SetString(PyExc_MemoryError,
759 "Can't allocate memory for compression object");
760 goto error;
761 default:
762 zlib_error(self->zst, err, "while copying compression object");
763 goto error;
764 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000765 Py_INCREF(self->unused_data);
766 Py_INCREF(self->unconsumed_tail);
767 Py_XDECREF(retval->unused_data);
768 Py_XDECREF(retval->unconsumed_tail);
769 retval->unused_data = self->unused_data;
770 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200771 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772
773 /* Mark it as being initialized */
774 retval->is_initialised = 1;
775
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000776 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777 return (PyObject *)retval;
778
779error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000780 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000781 Py_XDECREF(retval);
782 return NULL;
783}
784
785PyDoc_STRVAR(decomp_copy__doc__,
786"copy() -- Return a copy of the decompression object.");
787
788static PyObject *
789PyZlib_uncopy(compobject *self)
790{
791 compobject *retval = NULL;
792 int err;
793
794 retval = newcompobject(&Decomptype);
795 if (!retval) return NULL;
796
797 /* Copy the zstream state
798 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
799 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000800 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000801 err = inflateCopy(&retval->zst, &self->zst);
802 switch(err) {
803 case(Z_OK):
804 break;
805 case(Z_STREAM_ERROR):
806 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
807 goto error;
808 case(Z_MEM_ERROR):
809 PyErr_SetString(PyExc_MemoryError,
810 "Can't allocate memory for decompression object");
811 goto error;
812 default:
813 zlib_error(self->zst, err, "while copying decompression object");
814 goto error;
815 }
816
817 Py_INCREF(self->unused_data);
818 Py_INCREF(self->unconsumed_tail);
819 Py_XDECREF(retval->unused_data);
820 Py_XDECREF(retval->unconsumed_tail);
821 retval->unused_data = self->unused_data;
822 retval->unconsumed_tail = self->unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200823 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824
825 /* Mark it as being initialized */
826 retval->is_initialised = 1;
827
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000828 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000829 return (PyObject *)retval;
830
831error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000832 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000833 Py_XDECREF(retval);
834 return NULL;
835}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000836#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000838PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839"flush( [length] ) -- Return a string containing any remaining\n"
840"decompressed data. length, if given, is the initial size of the\n"
841"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000842"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000843"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000844
Guido van Rossumfb221561997-04-29 15:38:09 +0000845static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000846PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000847{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000848 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000849 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000850 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000851
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000852 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000853 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000854 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000855 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
856 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000857 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000858 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000860
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000861
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000862 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000863
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000864 start_total_out = self->zst.total_out;
865 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000866 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000867
868 Py_BEGIN_ALLOW_THREADS
869 err = inflate(&(self->zst), Z_FINISH);
870 Py_END_ALLOW_THREADS
871
872 /* while Z_OK and the output buffer is full, there might be more output,
873 so extend the output buffer and try again */
874 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000875 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000876 Py_DECREF(retval);
877 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000879 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000880 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
881 self->zst.avail_out = length;
882 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000883
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884 Py_BEGIN_ALLOW_THREADS
885 err = inflate(&(self->zst), Z_FINISH);
886 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000887 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000888
Nadeem Vawda3bf71c52011-08-13 15:42:50 +0200889 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000890 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200891 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000892 self->is_initialised = 0;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200893 err = inflateEnd(&(self->zst));
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000894 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200895 zlib_error(self->zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 Py_DECREF(retval);
897 retval = NULL;
898 goto error;
899 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000900 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000901 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000902 Py_DECREF(retval);
903 retval = NULL;
904 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000905
906error:
907
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000908 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000909
Jeremy Hylton9714f992001-10-16 21:19:45 +0000910 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000911}
912
913static PyMethodDef comp_methods[] =
914{
Tim Peters977e5402001-10-17 03:57:20 +0000915 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000916 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000917 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000918 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000919#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
921 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000922#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000923 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000924};
925
926static PyMethodDef Decomp_methods[] =
927{
Tim Peters977e5402001-10-17 03:57:20 +0000928 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000929 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000930 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000931 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000932#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000933 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
934 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000935#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000936 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000937};
938
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000939#define COMP_OFF(x) offsetof(compobject, x)
940static PyMemberDef Decomp_members[] = {
941 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
942 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +0200943 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000944 {NULL},
945};
Guido van Rossumfb221561997-04-29 15:38:09 +0000946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000947PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000948"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
949"\n"
950"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000951"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000952
Guido van Rossumfb221561997-04-29 15:38:09 +0000953static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000954PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000955{
Christian Heimescc47b052008-03-25 14:56:36 +0000956 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000957 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000958
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000959 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
960 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000961 /* Releasing the GIL for very small buffers is inefficient
962 and may lower performance */
963 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000964 unsigned char *buf = pbuf.buf;
965 Py_ssize_t len = pbuf.len;
966
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000967 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000968 /* Avoid truncation of length for very large buffers. adler32() takes
969 length as an unsigned int, which may be narrower than Py_ssize_t. */
970 while (len > (size_t) UINT_MAX) {
971 adler32val = adler32(adler32val, buf, UINT_MAX);
972 buf += (size_t) UINT_MAX;
973 len -= (size_t) UINT_MAX;
974 }
975 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000976 Py_END_ALLOW_THREADS
977 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000978 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000979 }
980 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000981 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000982}
Tim Peters977e5402001-10-17 03:57:20 +0000983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000984PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000985"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
986"\n"
987"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000988"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000989
990static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000991PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000992{
Christian Heimescc47b052008-03-25 14:56:36 +0000993 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000994 Py_buffer pbuf;
995 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000996
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000997 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000998 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000999 /* Releasing the GIL for very small buffers is inefficient
1000 and may lower performance */
1001 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001002 unsigned char *buf = pbuf.buf;
1003 Py_ssize_t len = pbuf.len;
1004
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001005 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001006 /* Avoid truncation of length for very large buffers. crc32() takes
1007 length as an unsigned int, which may be narrower than Py_ssize_t. */
1008 while (len > (size_t) UINT_MAX) {
1009 crc32val = crc32(crc32val, buf, UINT_MAX);
1010 buf += (size_t) UINT_MAX;
1011 len -= (size_t) UINT_MAX;
1012 }
1013 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001014 Py_END_ALLOW_THREADS
1015 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001016 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001017 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001018 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001019 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001020}
Tim Peters977e5402001-10-17 03:57:20 +00001021
Guido van Rossumfb221561997-04-29 15:38:09 +00001022
1023static PyMethodDef zlib_methods[] =
1024{
Tim Peters977e5402001-10-17 03:57:20 +00001025 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001026 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001027 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001028 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001029 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001030 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001031 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1032 crc32__doc__},
1033 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001034 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001035 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001036 decompressobj__doc__},
1037 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001038};
1039
Tim Peters0c322792002-07-17 16:49:03 +00001040static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001041 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001042 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001043 sizeof(compobject),
1044 0,
1045 (destructor)Comp_dealloc, /*tp_dealloc*/
1046 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001047 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001048 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001049 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001050 0, /*tp_repr*/
1051 0, /*tp_as_number*/
1052 0, /*tp_as_sequence*/
1053 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001054 0, /*tp_hash*/
1055 0, /*tp_call*/
1056 0, /*tp_str*/
1057 0, /*tp_getattro*/
1058 0, /*tp_setattro*/
1059 0, /*tp_as_buffer*/
1060 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1061 0, /*tp_doc*/
1062 0, /*tp_traverse*/
1063 0, /*tp_clear*/
1064 0, /*tp_richcompare*/
1065 0, /*tp_weaklistoffset*/
1066 0, /*tp_iter*/
1067 0, /*tp_iternext*/
1068 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001069};
1070
Tim Peters0c322792002-07-17 16:49:03 +00001071static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001072 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001073 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001074 sizeof(compobject),
1075 0,
1076 (destructor)Decomp_dealloc, /*tp_dealloc*/
1077 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001078 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001079 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001080 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001081 0, /*tp_repr*/
1082 0, /*tp_as_number*/
1083 0, /*tp_as_sequence*/
1084 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001085 0, /*tp_hash*/
1086 0, /*tp_call*/
1087 0, /*tp_str*/
1088 0, /*tp_getattro*/
1089 0, /*tp_setattro*/
1090 0, /*tp_as_buffer*/
1091 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1092 0, /*tp_doc*/
1093 0, /*tp_traverse*/
1094 0, /*tp_clear*/
1095 0, /*tp_richcompare*/
1096 0, /*tp_weaklistoffset*/
1097 0, /*tp_iter*/
1098 0, /*tp_iternext*/
1099 Decomp_methods, /*tp_methods*/
1100 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001101};
1102
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001103PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001104"The functions in this module allow compression and decompression using the\n"
1105"zlib library, which is based on GNU zip.\n"
1106"\n"
1107"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1108"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001109"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001110"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001111"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001112"decompressobj([wbits]) -- Return a decompressor object.\n"
1113"\n"
1114"'wbits' is window buffer size.\n"
1115"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001116"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001117
Martin v. Löwis1a214512008-06-11 05:26:20 +00001118static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001119 PyModuleDef_HEAD_INIT,
1120 "zlib",
1121 zlib_module_documentation,
1122 -1,
1123 zlib_methods,
1124 NULL,
1125 NULL,
1126 NULL,
1127 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001128};
1129
Mark Hammond62b1ab12002-07-23 06:31:15 +00001130PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001131PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001132{
Fred Drake4baedc12002-04-01 14:53:37 +00001133 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001134 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001135 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001136 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001137 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001138 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001139 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001140 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001141
Fred Drake4baedc12002-04-01 14:53:37 +00001142 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1143 if (ZlibError != NULL) {
1144 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001145 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001146 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001147 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1148 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1149 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1150 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1151 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1152 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1153 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1154 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1155 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001156
Jeremy Hylton9714f992001-10-16 21:19:45 +00001157 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1158 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1159 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1160 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001161
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001162 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001163 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001164 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001165
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001166 PyModule_AddStringConstant(m, "__version__", "1.0");
1167
Martin v. Löwis1a214512008-06-11 05:26:20 +00001168 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001169}