blob: 206ef0c6d8c172fea7659d0692535fde069d201c [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin v. Löwis1dbce442001-10-09 10:54:31 +00002/* See http://www.gzip.org/zlib/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Guido van Rossumfb221561997-04-29 15:38:09 +00006
Guido van Rossum97b54571997-06-03 22:21:47 +00007#include "Python.h"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00008#include "structmember.h"
Guido van Rossum97b54571997-06-03 22:21:47 +00009#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000010
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000011#ifdef WITH_THREAD
Antoine Pitrou31f30b12009-01-02 17:34:35 +000012 #include "pythread.h"
13 #define ENTER_ZLIB(obj) \
14 Py_BEGIN_ALLOW_THREADS; \
15 PyThread_acquire_lock((obj)->lock, 1); \
16 Py_END_ALLOW_THREADS;
17 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018#else
Antoine Pitrou31f30b12009-01-02 17:34:35 +000019 #define ENTER_ZLIB(obj)
20 #define LEAVE_ZLIB(obj)
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000021#endif
22
Guido van Rossumfb221561997-04-29 15:38:09 +000023/* The following parameters are copied from zutil.h, version 0.95 */
24#define DEFLATED 8
25#if MAX_MEM_LEVEL >= 8
26# define DEF_MEM_LEVEL 8
27#else
28# define DEF_MEM_LEVEL MAX_MEM_LEVEL
29#endif
30#define DEF_WBITS MAX_WBITS
31
Guido van Rossumb729a1d1999-04-07 20:23:17 +000032/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
33#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000034
Jeremy Hylton938ace62002-07-17 16:30:39 +000035static PyTypeObject Comptype;
36static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000037
38static PyObject *ZlibError;
39
Tim Peters977e5402001-10-17 03:57:20 +000040typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000041{
Jeremy Hylton9714f992001-10-16 21:19:45 +000042 PyObject_HEAD
43 z_stream zst;
44 PyObject *unused_data;
45 PyObject *unconsumed_tail;
46 int is_initialised;
Antoine Pitrou31f30b12009-01-02 17:34:35 +000047 #ifdef WITH_THREAD
48 PyThread_type_lock lock;
49 #endif
Guido van Rossumfb221561997-04-29 15:38:09 +000050} compobject;
51
Jeremy Hylton0965e082001-10-16 21:56:09 +000052static void
53zlib_error(z_stream zst, int err, char *msg)
54{
Antoine Pitrou53b21662010-05-11 23:46:02 +000055 const char *zmsg = zst.msg;
56 if (zmsg == Z_NULL) {
57 switch (err) {
58 case Z_BUF_ERROR:
59 zmsg = "incomplete or truncated stream";
60 break;
61 case Z_STREAM_ERROR:
62 zmsg = "inconsistent stream state";
63 break;
64 case Z_DATA_ERROR:
65 zmsg = "invalid input data";
66 break;
67 }
68 }
69 if (zmsg == Z_NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000071 else
Antoine Pitrou53b21662010-05-11 23:46:02 +000072 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000073}
74
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000075PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000076"compressobj([level]) -- Return a compressor object.\n"
77"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000078"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000080PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000081"decompressobj([wbits]) -- Return a decompressor object.\n"
82"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000083"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000084
Guido van Rossumfb221561997-04-29 15:38:09 +000085static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000086newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000087{
Tim Peters977e5402001-10-17 03:57:20 +000088 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000089 self = PyObject_New(compobject, type);
90 if (self == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +000092 self->is_initialised = 0;
Gregory P. Smith693fc462008-09-06 20:13:06 +000093 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000094 if (self->unused_data == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 Py_DECREF(self);
96 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +000097 }
Gregory P. Smith693fc462008-09-06 20:13:06 +000098 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000099 if (self->unconsumed_tail == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 Py_DECREF(self);
101 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000102 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000103#ifdef WITH_THREAD
104 self->lock = PyThread_allocate_lock();
105#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000106 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000107}
108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000109PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000110"compress(string[, level]) -- Returned compressed string.\n"
111"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000113
Guido van Rossumfb221561997-04-29 15:38:09 +0000114static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000115PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000116{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000117 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000118 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 Byte *input, *output;
Victor Stinner8848c7a2011-01-04 02:07:36 +0000120 unsigned int length;
121 int level=Z_DEFAULT_COMPRESSION, err;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000123
Jeremy Hylton9714f992001-10-16 21:19:45 +0000124 /* require Python string object, optional 'level' arg */
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000125 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 return NULL;
Victor Stinner8848c7a2011-01-04 02:07:36 +0000127
128 if (pinput.len > UINT_MAX) {
129 PyErr_SetString(PyExc_OverflowError,
130 "size does not fit in an unsigned int");
131 return NULL;
132 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000133 length = pinput.len;
Victor Stinner8848c7a2011-01-04 02:07:36 +0000134 input = pinput.buf;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000135
Jeremy Hylton9714f992001-10-16 21:19:45 +0000136 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000137
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000138 output = (Byte*)malloc(zst.avail_out);
139 if (output == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyBuffer_Release(&pinput);
141 PyErr_SetString(PyExc_MemoryError,
142 "Can't allocate memory to compress data");
143 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000144 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000145
Jeremy Hylton9714f992001-10-16 21:19:45 +0000146 /* Past the point of no return. From here on out, we need to make sure
147 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000148
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000149 zst.zalloc = (alloc_func)NULL;
150 zst.zfree = (free_func)Z_NULL;
151 zst.next_out = (Byte *)output;
152 zst.next_in = (Byte *)input;
153 zst.avail_in = length;
154 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000155
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000156 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000157 case(Z_OK):
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000159 case(Z_MEM_ERROR):
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 PyErr_SetString(PyExc_MemoryError,
161 "Out of memory while compressing data");
162 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000163 case(Z_STREAM_ERROR):
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 PyErr_SetString(ZlibError,
165 "Bad compression level");
166 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000167 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000168 deflateEnd(&zst);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 zlib_error(zst, err, "while compressing data");
170 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000171 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000172
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000173 Py_BEGIN_ALLOW_THREADS;
174 err = deflate(&zst, Z_FINISH);
175 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000176
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000177 if (err != Z_STREAM_END) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 zlib_error(zst, err, "while compressing data");
179 deflateEnd(&zst);
180 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000181 }
Tim Peters977e5402001-10-17 03:57:20 +0000182
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000183 err=deflateEnd(&zst);
184 if (err == Z_OK)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000186 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000187 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000189
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000190 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000191 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000192 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000193
Jeremy Hylton9714f992001-10-16 21:19:45 +0000194 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000195}
196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000197PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000198"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
199"\n"
200"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000202
Guido van Rossumfb221561997-04-29 15:38:09 +0000203static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000204PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000205{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000206 PyObject *result_str;
Martin v. Löwis423be952008-08-13 15:53:07 +0000207 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000208 Byte *input;
Victor Stinner8848c7a2011-01-04 02:07:36 +0000209 unsigned int length;
210 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000211 int wsize=DEF_WBITS;
212 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000213 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000214
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000215 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 &pinput, &wsize, &r_strlen))
217 return NULL;
Victor Stinner8848c7a2011-01-04 02:07:36 +0000218
219 if (pinput.len > UINT_MAX) {
220 PyErr_SetString(PyExc_OverflowError,
221 "size does not fit in an unsigned int");
222 return NULL;
223 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000224 length = pinput.len;
Victor Stinner8848c7a2011-01-04 02:07:36 +0000225 input = pinput.buf;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000226
Jeremy Hylton9714f992001-10-16 21:19:45 +0000227 if (r_strlen <= 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000229
Jeremy Hylton9714f992001-10-16 21:19:45 +0000230 zst.avail_in = length;
231 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000232
Gregory P. Smith693fc462008-09-06 20:13:06 +0000233 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 PyBuffer_Release(&pinput);
235 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000236 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000237
Jeremy Hylton9714f992001-10-16 21:19:45 +0000238 zst.zalloc = (alloc_func)NULL;
239 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000240 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000241 zst.next_in = (Byte *)input;
242 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000243
Jeremy Hylton9714f992001-10-16 21:19:45 +0000244 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000245 case(Z_OK):
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 break;
Tim Peters977e5402001-10-17 03:57:20 +0000247 case(Z_MEM_ERROR):
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 PyErr_SetString(PyExc_MemoryError,
249 "Out of memory while decompressing data");
250 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000251 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000252 inflateEnd(&zst);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 zlib_error(zst, err, "while preparing to decompress data");
254 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000255 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000256
Jeremy Hylton9714f992001-10-16 21:19:45 +0000257 do {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_BEGIN_ALLOW_THREADS
259 err=inflate(&zst, Z_FINISH);
260 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 switch(err) {
263 case(Z_STREAM_END):
264 break;
265 case(Z_BUF_ERROR):
266 /*
267 * If there is at least 1 byte of room according to zst.avail_out
268 * and we get this error, assume that it means zlib cannot
269 * process the inflate call() due to an error in the data.
270 */
271 if (zst.avail_out > 0) {
Antoine Pitrou53b21662010-05-11 23:46:02 +0000272 zlib_error(zst, err, "while decompressing data");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 inflateEnd(&zst);
274 goto error;
275 }
276 /* fall through */
277 case(Z_OK):
278 /* need more memory */
279 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
280 inflateEnd(&zst);
281 goto error;
282 }
283 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000284 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 zst.avail_out = r_strlen;
286 r_strlen = r_strlen << 1;
287 break;
288 default:
289 inflateEnd(&zst);
290 zlib_error(zst, err, "while decompressing data");
291 goto error;
292 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000293 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000294
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000295 err = inflateEnd(&zst);
296 if (err != Z_OK) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 zlib_error(zst, err, "while finishing data decompression");
298 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000299 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000300
Gregory P. Smith693fc462008-09-06 20:13:06 +0000301 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000302 goto error;
303
Martin v. Löwis423be952008-08-13 15:53:07 +0000304 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000305 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000306
307 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000308 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000309 Py_XDECREF(result_str);
310 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000311}
312
313static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000314PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000315{
Jeremy Hylton499000002001-10-16 21:59:35 +0000316 compobject *self;
317 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
318 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000319
Jeremy Hylton499000002001-10-16 21:59:35 +0000320 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 &memLevel, &strategy))
322 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000323
Jeremy Hylton499000002001-10-16 21:59:35 +0000324 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000325 if (self==NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000327 self->zst.zalloc = (alloc_func)NULL;
328 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000329 self->zst.next_in = NULL;
330 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000331 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
332 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000333 case (Z_OK):
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 self->is_initialised = 1;
335 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000336 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000337 Py_DECREF(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 PyErr_SetString(PyExc_MemoryError,
339 "Can't allocate memory for compression object");
340 return NULL;
341 case(Z_STREAM_ERROR):
342 Py_DECREF(self);
343 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
344 return NULL;
345 default:
346 zlib_error(self->zst, err, "while creating compression object");
347 Py_DECREF(self);
348 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000349 }
350}
351
352static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000353PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000354{
Jeremy Hylton499000002001-10-16 21:59:35 +0000355 int wbits=DEF_WBITS, err;
356 compobject *self;
357 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000359
360 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000361 if (self == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000363 self->zst.zalloc = (alloc_func)NULL;
364 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000365 self->zst.next_in = NULL;
366 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000367 err = inflateInit2(&self->zst, wbits);
368 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000369 case (Z_OK):
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 self->is_initialised = 1;
371 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000372 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000373 Py_DECREF(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
375 return NULL;
376 case (Z_MEM_ERROR):
377 Py_DECREF(self);
378 PyErr_SetString(PyExc_MemoryError,
379 "Can't allocate memory for decompression object");
380 return NULL;
381 default:
382 zlib_error(self->zst, err, "while creating decompression object");
383 Py_DECREF(self);
384 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000385 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000386}
387
388static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000389Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000390{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000391#ifdef WITH_THREAD
392 PyThread_free_lock(self->lock);
393#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000394 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000395 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000396 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000397}
398
399static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000400Comp_dealloc(compobject *self)
401{
402 if (self->is_initialised)
403 deflateEnd(&self->zst);
404 Dealloc(self);
405}
406
407static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000408Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000409{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000410 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000411 inflateEnd(&self->zst);
412 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000413}
414
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000415PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000416"compress(data) -- Return a string containing data compressed.\n"
417"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000418"After calling this function, some of the input data may still\n"
419"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000420"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000421
422
Guido van Rossumfb221561997-04-29 15:38:09 +0000423static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000424PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000425{
Antoine Pitrou89562712010-05-07 17:04:02 +0000426 int err, inplen;
427 Py_ssize_t length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000428 PyObject *RetVal;
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 Pitrou77b338b2009-12-14 18:00:06 +0000433 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000435 input = pinput.buf;
436 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000437
Gregory P. Smith693fc462008-09-06 20:13:06 +0000438 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 PyBuffer_Release(&pinput);
440 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000441 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000442
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000443 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000444
Jeremy Hylton9714f992001-10-16 21:19:45 +0000445 start_total_out = self->zst.total_out;
446 self->zst.avail_in = inplen;
447 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000448 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000449 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000450
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000451 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000452 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000453 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000454
Jeremy Hylton9714f992001-10-16 21:19:45 +0000455 /* while Z_OK and the output buffer is full, there might be more output,
456 so extend the output buffer and try again */
457 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000459 Py_DECREF(RetVal);
460 RetVal = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000462 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000464 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 self->zst.avail_out = length;
466 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 Py_BEGIN_ALLOW_THREADS
469 err = deflate(&(self->zst), Z_NO_FLUSH);
470 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000471 }
Tim Peters977e5402001-10-17 03:57:20 +0000472 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000473 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000474 condition.
475 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000476
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000477 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 zlib_error(self->zst, err, "while compressing");
479 Py_DECREF(RetVal);
480 RetVal = NULL;
481 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000482 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000483 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000484 Py_DECREF(RetVal);
485 RetVal = NULL;
486 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000487
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000488 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000489 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000490 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000491 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000492}
493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000494PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000495"decompress(data, max_length) -- Return a string containing the decompressed\n"
496"version of the data.\n"
497"\n"
498"After calling this function, some of the input data may still be stored in\n"
499"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000500"Call the flush() method to clear these buffers.\n"
501"If the max_length parameter is specified then the return value will be\n"
502"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000503"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000504
Guido van Rossumfb221561997-04-29 15:38:09 +0000505static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000506PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000507{
Antoine Pitrou89562712010-05-07 17:04:02 +0000508 int err, inplen, max_length = 0;
509 Py_ssize_t old_length, length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000510 PyObject *RetVal;
Martin v. Löwis423be952008-08-13 15:53:07 +0000511 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000512 Byte *input;
513 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000514
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000515 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 &max_length))
517 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000518 input = pinput.buf;
519 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000520 if (max_length < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyBuffer_Release(&pinput);
522 PyErr_SetString(PyExc_ValueError,
523 "max_length must be greater than zero");
524 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000525 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000526
Jeremy Hylton9714f992001-10-16 21:19:45 +0000527 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000528 if (max_length && length > max_length)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 length = max_length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000530 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyBuffer_Release(&pinput);
532 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000533 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000534
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000535 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000536
Jeremy Hylton9714f992001-10-16 21:19:45 +0000537 start_total_out = self->zst.total_out;
538 self->zst.avail_in = inplen;
539 self->zst.next_in = input;
540 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000541 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000542
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000543 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000544 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000545 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000546
Jeremy Hylton9714f992001-10-16 21:19:45 +0000547 /* While Z_OK and the output buffer is full, there might be more output.
548 So extend the output buffer and try again.
549 */
Tim Peters977e5402001-10-17 03:57:20 +0000550 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* If max_length set, don't continue decompressing if we've already
552 reached the limit.
553 */
554 if (max_length && length >= max_length)
555 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* otherwise, ... */
558 old_length = length;
559 length = length << 1;
560 if (max_length && length > max_length)
561 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000564 Py_DECREF(RetVal);
565 RetVal = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000567 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000569 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 Py_BEGIN_ALLOW_THREADS
573 err = inflate(&(self->zst), Z_SYNC_FLUSH);
574 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000575 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000576
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000577 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000578 of specified size. Return the unconsumed tail in an attribute.*/
579 if(max_length) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 Py_DECREF(self->unconsumed_tail);
581 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
582 self->zst.avail_in);
583 if(!self->unconsumed_tail) {
584 Py_DECREF(RetVal);
585 RetVal = NULL;
586 goto error;
587 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000588 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000589
Tim Peters977e5402001-10-17 03:57:20 +0000590 /* The end of the compressed data has been reached, so set the
591 unused_data attribute to a string containing the remainder of the
592 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000593 inflateEnd, but the old behaviour of only calling it on flush() is
594 preserved.
595 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000596 if (err == Z_STREAM_END) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_XDECREF(self->unused_data); /* Free original empty string */
598 self->unused_data = PyBytes_FromStringAndSize(
599 (char *)self->zst.next_in, self->zst.avail_in);
600 if (self->unused_data == NULL) {
601 Py_DECREF(RetVal);
602 goto error;
603 }
604 /* We will only get Z_BUF_ERROR if the output buffer was full
605 but there wasn't more output when we tried again, so it is
606 not an error condition.
607 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000608 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 zlib_error(self->zst, err, "while decompressing");
610 Py_DECREF(RetVal);
611 RetVal = NULL;
612 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000613 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000614
Gregory P. Smith693fc462008-09-06 20:13:06 +0000615 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000616 Py_DECREF(RetVal);
617 RetVal = NULL;
618 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000619
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000620 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000621 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000622 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000623 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000624}
625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000626PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000627"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000628"\n"
629"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000630"default value used when mode is not specified is Z_FINISH.\n"
631"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000632"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000633
Guido van Rossumfb221561997-04-29 15:38:09 +0000634static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000635PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000636{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000637 int err, length = DEFAULTALLOC;
638 PyObject *RetVal;
639 int flushmode = Z_FINISH;
640 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000641
Jeremy Hylton9714f992001-10-16 21:19:45 +0000642 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000644
Jeremy Hylton9714f992001-10-16 21:19:45 +0000645 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
646 doing any work at all; just return an empty string. */
647 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000649 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000650
Gregory P. Smith693fc462008-09-06 20:13:06 +0000651 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000653
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000654 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000655
Jeremy Hylton9714f992001-10-16 21:19:45 +0000656 start_total_out = self->zst.total_out;
657 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000658 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000659 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000660
661 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000662 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000663 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000664
Jeremy Hylton9714f992001-10-16 21:19:45 +0000665 /* while Z_OK and the output buffer is full, there might be more output,
666 so extend the output buffer and try again */
667 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000669 Py_DECREF(RetVal);
670 RetVal = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000672 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000674 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 self->zst.avail_out = length;
676 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 Py_BEGIN_ALLOW_THREADS
679 err = deflate(&(self->zst), flushmode);
680 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000681 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000682
Jeremy Hylton9714f992001-10-16 21:19:45 +0000683 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000684 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000685 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000686 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 err = deflateEnd(&(self->zst));
688 if (err != Z_OK) {
689 zlib_error(self->zst, err, "from deflateEnd()");
690 Py_DECREF(RetVal);
691 RetVal = NULL;
692 goto error;
693 }
694 else
695 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 /* We will only get Z_BUF_ERROR if the output buffer was full
698 but there wasn't more output when we tried again, so it is
699 not an error condition.
700 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000701 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 zlib_error(self->zst, err, "while flushing");
703 Py_DECREF(RetVal);
704 RetVal = NULL;
705 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000706 }
Tim Peters977e5402001-10-17 03:57:20 +0000707
Gregory P. Smith693fc462008-09-06 20:13:06 +0000708 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000709 Py_DECREF(RetVal);
710 RetVal = NULL;
711 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000712
Tim Peters977e5402001-10-17 03:57:20 +0000713 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000714 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000715
716 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000717}
718
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000719#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000720PyDoc_STRVAR(comp_copy__doc__,
721"copy() -- Return a copy of the compression object.");
722
723static PyObject *
724PyZlib_copy(compobject *self)
725{
726 compobject *retval = NULL;
727 int err;
728
729 retval = newcompobject(&Comptype);
730 if (!retval) return NULL;
731
732 /* Copy the zstream state
733 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
734 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000735 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736 err = deflateCopy(&retval->zst, &self->zst);
737 switch(err) {
738 case(Z_OK):
739 break;
740 case(Z_STREAM_ERROR):
741 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
742 goto error;
743 case(Z_MEM_ERROR):
744 PyErr_SetString(PyExc_MemoryError,
745 "Can't allocate memory for compression object");
746 goto error;
747 default:
748 zlib_error(self->zst, err, "while copying compression object");
749 goto error;
750 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751 Py_INCREF(self->unused_data);
752 Py_INCREF(self->unconsumed_tail);
753 Py_XDECREF(retval->unused_data);
754 Py_XDECREF(retval->unconsumed_tail);
755 retval->unused_data = self->unused_data;
756 retval->unconsumed_tail = self->unconsumed_tail;
757
758 /* Mark it as being initialized */
759 retval->is_initialised = 1;
760
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000761 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000762 return (PyObject *)retval;
763
764error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000765 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766 Py_XDECREF(retval);
767 return NULL;
768}
769
770PyDoc_STRVAR(decomp_copy__doc__,
771"copy() -- Return a copy of the decompression object.");
772
773static PyObject *
774PyZlib_uncopy(compobject *self)
775{
776 compobject *retval = NULL;
777 int err;
778
779 retval = newcompobject(&Decomptype);
780 if (!retval) return NULL;
781
782 /* Copy the zstream state
783 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
784 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000785 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786 err = inflateCopy(&retval->zst, &self->zst);
787 switch(err) {
788 case(Z_OK):
789 break;
790 case(Z_STREAM_ERROR):
791 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
792 goto error;
793 case(Z_MEM_ERROR):
794 PyErr_SetString(PyExc_MemoryError,
795 "Can't allocate memory for decompression object");
796 goto error;
797 default:
798 zlib_error(self->zst, err, "while copying decompression object");
799 goto error;
800 }
801
802 Py_INCREF(self->unused_data);
803 Py_INCREF(self->unconsumed_tail);
804 Py_XDECREF(retval->unused_data);
805 Py_XDECREF(retval->unconsumed_tail);
806 retval->unused_data = self->unused_data;
807 retval->unconsumed_tail = self->unconsumed_tail;
808
809 /* Mark it as being initialized */
810 retval->is_initialised = 1;
811
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000812 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000813 return (PyObject *)retval;
814
815error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000816 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000817 Py_XDECREF(retval);
818 return NULL;
819}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000820#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823"flush( [length] ) -- Return a string containing any remaining\n"
824"decompressed data. length, if given, is the initial size of the\n"
825"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000826"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000827"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000828
Guido van Rossumfb221561997-04-29 15:38:09 +0000829static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000830PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000831{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000832 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000833 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000834 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000835
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000836 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000838 if (length <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
840 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000841 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000842 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000844
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000845
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000846 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000847
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000848 start_total_out = self->zst.total_out;
849 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000850 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000851
852 Py_BEGIN_ALLOW_THREADS
853 err = inflate(&(self->zst), Z_FINISH);
854 Py_END_ALLOW_THREADS
855
856 /* while Z_OK and the output buffer is full, there might be more output,
857 so extend the output buffer and try again */
858 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000860 Py_DECREF(retval);
861 retval = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
865 self->zst.avail_out = length;
866 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 Py_BEGIN_ALLOW_THREADS
869 err = inflate(&(self->zst), Z_FINISH);
870 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000871 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000872
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000873 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
874 various data structures. Note we should only get Z_STREAM_END when
875 flushmode is Z_FINISH */
876 if (err == Z_STREAM_END) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000878 self->is_initialised = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (err != Z_OK) {
880 zlib_error(self->zst, err, "from inflateEnd()");
881 Py_DECREF(retval);
882 retval = NULL;
883 goto error;
884 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000885 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000886 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000887 Py_DECREF(retval);
888 retval = NULL;
889 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000890
891error:
892
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000893 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000894
Jeremy Hylton9714f992001-10-16 21:19:45 +0000895 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000896}
897
898static PyMethodDef comp_methods[] =
899{
Tim Peters977e5402001-10-17 03:57:20 +0000900 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000901 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000902 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000903 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000904#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
906 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000907#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000908 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000909};
910
911static PyMethodDef Decomp_methods[] =
912{
Tim Peters977e5402001-10-17 03:57:20 +0000913 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000914 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000915 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000916 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000917#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000918 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
919 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000920#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000921 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000922};
923
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000924#define COMP_OFF(x) offsetof(compobject, x)
925static PyMemberDef Decomp_members[] = {
926 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
927 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
928 {NULL},
929};
Guido van Rossumfb221561997-04-29 15:38:09 +0000930
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000931PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000932"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
933"\n"
934"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000935"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000936
Guido van Rossumfb221561997-04-29 15:38:09 +0000937static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000938PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000939{
Christian Heimescc47b052008-03-25 14:56:36 +0000940 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000941 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000942
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000943 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
944 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000945 /* Releasing the GIL for very small buffers is inefficient
946 and may lower performance */
947 if (pbuf.len > 1024*5) {
Antoine Pitrou4709ec02011-02-21 19:28:40 +0000948 unsigned char *buf = pbuf.buf;
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000949 Py_ssize_t len = pbuf.len;
950
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000951 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000952 /* Avoid truncation of length for very large buffers. adler32() takes
953 length as an unsigned int, which may be narrower than Py_ssize_t. */
Antoine Pitrou3d326642011-02-21 19:05:08 +0000954 while (len > (size_t) UINT_MAX) {
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000955 adler32val = adler32(adler32val, buf, UINT_MAX);
Antoine Pitrou3d326642011-02-21 19:05:08 +0000956 buf += (size_t) UINT_MAX;
957 len -= (size_t) UINT_MAX;
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000958 }
959 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000960 Py_END_ALLOW_THREADS
961 } else {
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000962 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000963 }
964 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000965 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000966}
Tim Peters977e5402001-10-17 03:57:20 +0000967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000968PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000969"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
970"\n"
971"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000972"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000973
974static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000975PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000976{
Christian Heimescc47b052008-03-25 14:56:36 +0000977 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000978 Py_buffer pbuf;
979 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000980
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000981 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000983 /* Releasing the GIL for very small buffers is inefficient
984 and may lower performance */
985 if (pbuf.len > 1024*5) {
Antoine Pitrou4709ec02011-02-21 19:28:40 +0000986 unsigned char *buf = pbuf.buf;
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000987 Py_ssize_t len = pbuf.len;
988
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000989 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000990 /* Avoid truncation of length for very large buffers. crc32() takes
991 length as an unsigned int, which may be narrower than Py_ssize_t. */
Antoine Pitrou3d326642011-02-21 19:05:08 +0000992 while (len > (size_t) UINT_MAX) {
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000993 crc32val = crc32(crc32val, buf, UINT_MAX);
Antoine Pitrou3d326642011-02-21 19:05:08 +0000994 buf += (size_t) UINT_MAX;
995 len -= (size_t) UINT_MAX;
Antoine Pitrou54f0f842011-02-21 18:03:13 +0000996 }
997 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000998 Py_END_ALLOW_THREADS
999 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001001 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001002 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001003 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001004}
Tim Peters977e5402001-10-17 03:57:20 +00001005
Guido van Rossumfb221561997-04-29 15:38:09 +00001006
1007static PyMethodDef zlib_methods[] =
1008{
Tim Peters977e5402001-10-17 03:57:20 +00001009 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001010 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001011 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001012 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001013 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001014 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001015 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1016 crc32__doc__},
1017 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001018 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001019 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001020 decompressobj__doc__},
1021 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001022};
1023
Tim Peters0c322792002-07-17 16:49:03 +00001024static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001025 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001026 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001027 sizeof(compobject),
1028 0,
1029 (destructor)Comp_dealloc, /*tp_dealloc*/
1030 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001031 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001032 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001033 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001034 0, /*tp_repr*/
1035 0, /*tp_as_number*/
1036 0, /*tp_as_sequence*/
1037 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001038 0, /*tp_hash*/
1039 0, /*tp_call*/
1040 0, /*tp_str*/
1041 0, /*tp_getattro*/
1042 0, /*tp_setattro*/
1043 0, /*tp_as_buffer*/
1044 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1045 0, /*tp_doc*/
1046 0, /*tp_traverse*/
1047 0, /*tp_clear*/
1048 0, /*tp_richcompare*/
1049 0, /*tp_weaklistoffset*/
1050 0, /*tp_iter*/
1051 0, /*tp_iternext*/
1052 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001053};
1054
Tim Peters0c322792002-07-17 16:49:03 +00001055static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001056 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001057 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001058 sizeof(compobject),
1059 0,
1060 (destructor)Decomp_dealloc, /*tp_dealloc*/
1061 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001062 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001063 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001064 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001065 0, /*tp_repr*/
1066 0, /*tp_as_number*/
1067 0, /*tp_as_sequence*/
1068 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001069 0, /*tp_hash*/
1070 0, /*tp_call*/
1071 0, /*tp_str*/
1072 0, /*tp_getattro*/
1073 0, /*tp_setattro*/
1074 0, /*tp_as_buffer*/
1075 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1076 0, /*tp_doc*/
1077 0, /*tp_traverse*/
1078 0, /*tp_clear*/
1079 0, /*tp_richcompare*/
1080 0, /*tp_weaklistoffset*/
1081 0, /*tp_iter*/
1082 0, /*tp_iternext*/
1083 Decomp_methods, /*tp_methods*/
1084 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001085};
1086
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001087PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001088"The functions in this module allow compression and decompression using the\n"
1089"zlib library, which is based on GNU zip.\n"
1090"\n"
1091"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1092"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001093"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001094"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001095"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001096"decompressobj([wbits]) -- Return a decompressor object.\n"
1097"\n"
1098"'wbits' is window buffer size.\n"
1099"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001100"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001101
Martin v. Löwis1a214512008-06-11 05:26:20 +00001102static struct PyModuleDef zlibmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 PyModuleDef_HEAD_INIT,
1104 "zlib",
1105 zlib_module_documentation,
1106 -1,
1107 zlib_methods,
1108 NULL,
1109 NULL,
1110 NULL,
1111 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001112};
1113
Mark Hammond62b1ab12002-07-23 06:31:15 +00001114PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001115PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001116{
Fred Drake4baedc12002-04-01 14:53:37 +00001117 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001118 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001120 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001122 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001123 if (m == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001125
Fred Drake4baedc12002-04-01 14:53:37 +00001126 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1127 if (ZlibError != NULL) {
1128 Py_INCREF(ZlibError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001130 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001131 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1132 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1133 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1134 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1135 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1136 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1137 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1138 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1139 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001140
Jeremy Hylton9714f992001-10-16 21:19:45 +00001141 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1142 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1143 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1144 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001145
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001146 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001147 if (ver != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001149
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001150 PyModule_AddStringConstant(m, "__version__", "1.0");
1151
Martin v. Löwis1a214512008-06-11 05:26:20 +00001152 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001153}