blob: 2f2e214906ffb26d352c5c48460e15a2acfc83ce [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{
55 if (zst.msg == Z_NULL)
56 PyErr_Format(ZlibError, "Error %d %s", err, msg);
57 else
58 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
59}
60
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000062"compressobj([level]) -- Return a compressor object.\n"
63"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000064"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000065
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000066PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000067"decompressobj([wbits]) -- Return a decompressor object.\n"
68"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000069"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000070
Guido van Rossumfb221561997-04-29 15:38:09 +000071static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000072newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000073{
Tim Peters977e5402001-10-17 03:57:20 +000074 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000075 self = PyObject_New(compobject, type);
76 if (self == NULL)
77 return NULL;
78 self->is_initialised = 0;
Gregory P. Smith693fc462008-09-06 20:13:06 +000079 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000080 if (self->unused_data == NULL) {
81 Py_DECREF(self);
82 return NULL;
83 }
Gregory P. Smith693fc462008-09-06 20:13:06 +000084 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +000085 if (self->unconsumed_tail == NULL) {
86 Py_DECREF(self);
87 return NULL;
88 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +000089#ifdef WITH_THREAD
90 self->lock = PyThread_allocate_lock();
91#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +000092 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +000093}
94
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000095PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000096"compress(string[, level]) -- Returned compressed string.\n"
97"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000099
Guido van Rossumfb221561997-04-29 15:38:09 +0000100static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000101PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000102{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000103 PyObject *ReturnVal = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000104 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 Byte *input, *output;
106 int length, level=Z_DEFAULT_COMPRESSION, err;
107 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000108
Jeremy Hylton9714f992001-10-16 21:19:45 +0000109 /* require Python string object, optional 'level' arg */
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000110 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000112 input = pinput.buf;
113 length = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000114
Jeremy Hylton9714f992001-10-16 21:19:45 +0000115 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000116
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000117 output = (Byte*)malloc(zst.avail_out);
118 if (output == NULL) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000119 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000120 PyErr_SetString(PyExc_MemoryError,
121 "Can't allocate memory to compress data");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000122 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000123 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000124
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 /* Past the point of no return. From here on out, we need to make sure
126 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000127
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000128 zst.zalloc = (alloc_func)NULL;
129 zst.zfree = (free_func)Z_NULL;
130 zst.next_out = (Byte *)output;
131 zst.next_in = (Byte *)input;
132 zst.avail_in = length;
133 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000134
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000135 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000136 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000137 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000138 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000139 PyErr_SetString(PyExc_MemoryError,
140 "Out of memory while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000141 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000142 case(Z_STREAM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000143 PyErr_SetString(ZlibError,
144 "Bad compression level");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000145 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000146 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000147 deflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000148 zlib_error(zst, err, "while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000149 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000150 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000151
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000152 Py_BEGIN_ALLOW_THREADS;
153 err = deflate(&zst, Z_FINISH);
154 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000155
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000156 if (err != Z_STREAM_END) {
157 zlib_error(zst, err, "while compressing data");
158 deflateEnd(&zst);
159 goto error;
160 }
Tim Peters977e5402001-10-17 03:57:20 +0000161
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000162 err=deflateEnd(&zst);
163 if (err == Z_OK)
Gregory P. Smith693fc462008-09-06 20:13:06 +0000164 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000165 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000166 else
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000167 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000168
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000169 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000170 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000171 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000172
Jeremy Hylton9714f992001-10-16 21:19:45 +0000173 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000174}
175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000176PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000177"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
178"\n"
179"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000180"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000181
Guido van Rossumfb221561997-04-29 15:38:09 +0000182static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000183PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000184{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000185 PyObject *result_str;
Martin v. Löwis423be952008-08-13 15:53:07 +0000186 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000187 Byte *input;
188 int length, err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000189 int wsize=DEF_WBITS;
190 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000191 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000192
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000193 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Martin v. Löwis423be952008-08-13 15:53:07 +0000194 &pinput, &wsize, &r_strlen))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000195 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000196 input = pinput.buf;
197 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000198
Jeremy Hylton9714f992001-10-16 21:19:45 +0000199 if (r_strlen <= 0)
200 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000201
Jeremy Hylton9714f992001-10-16 21:19:45 +0000202 zst.avail_in = length;
203 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000204
Gregory P. Smith693fc462008-09-06 20:13:06 +0000205 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen))) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000206 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000208 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000209
Jeremy Hylton9714f992001-10-16 21:19:45 +0000210 zst.zalloc = (alloc_func)NULL;
211 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000212 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000213 zst.next_in = (Byte *)input;
214 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000215
Jeremy Hylton9714f992001-10-16 21:19:45 +0000216 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000217 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000218 break;
Tim Peters977e5402001-10-17 03:57:20 +0000219 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000220 PyErr_SetString(PyExc_MemoryError,
221 "Out of memory while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000222 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000223 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000224 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000225 zlib_error(zst, err, "while preparing to decompress data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000226 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000227 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000228
Jeremy Hylton9714f992001-10-16 21:19:45 +0000229 do {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000230 Py_BEGIN_ALLOW_THREADS
231 err=inflate(&zst, Z_FINISH);
232 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000233
Jeremy Hylton9714f992001-10-16 21:19:45 +0000234 switch(err) {
235 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000236 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000237 case(Z_BUF_ERROR):
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000238 /*
239 * If there is at least 1 byte of room according to zst.avail_out
240 * and we get this error, assume that it means zlib cannot
241 * process the inflate call() due to an error in the data.
242 */
Jeremy Hylton0965e082001-10-16 21:56:09 +0000243 if (zst.avail_out > 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000244 PyErr_Format(ZlibError, "Error %i while decompressing data",
245 err);
246 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000247 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000248 }
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000249 /* fall through */
250 case(Z_OK):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000251 /* need more memory */
Gregory P. Smith693fc462008-09-06 20:13:06 +0000252 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000253 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000254 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000255 }
Guido van Rossum776152b2007-05-22 22:44:07 +0000256 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000257 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000258 zst.avail_out = r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000259 r_strlen = r_strlen << 1;
260 break;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000261 default:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000262 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000263 zlib_error(zst, err, "while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000264 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000265 }
266 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000267
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000268 err = inflateEnd(&zst);
269 if (err != Z_OK) {
270 zlib_error(zst, err, "while finishing data decompression");
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000271 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000272 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000273
Gregory P. Smith693fc462008-09-06 20:13:06 +0000274 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000275 goto error;
276
Martin v. Löwis423be952008-08-13 15:53:07 +0000277 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000278 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000279
280 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000281 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000282 Py_XDECREF(result_str);
283 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000284}
285
286static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000287PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000288{
Jeremy Hylton499000002001-10-16 21:59:35 +0000289 compobject *self;
290 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
291 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000292
Jeremy Hylton499000002001-10-16 21:59:35 +0000293 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
294 &memLevel, &strategy))
295 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000296
Jeremy Hylton499000002001-10-16 21:59:35 +0000297 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000298 if (self==NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000299 return(NULL);
300 self->zst.zalloc = (alloc_func)NULL;
301 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000302 self->zst.next_in = NULL;
303 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000304 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
305 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000306 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000307 self->is_initialised = 1;
308 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000309 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000310 Py_DECREF(self);
311 PyErr_SetString(PyExc_MemoryError,
312 "Can't allocate memory for compression object");
313 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000314 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000315 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000316 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000317 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000318 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000319 zlib_error(self->zst, err, "while creating compression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000320 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000321 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000322 }
323}
324
325static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000326PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000327{
Jeremy Hylton499000002001-10-16 21:59:35 +0000328 int wbits=DEF_WBITS, err;
329 compobject *self;
330 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
331 return NULL;
332
333 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000334 if (self == NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000335 return(NULL);
336 self->zst.zalloc = (alloc_func)NULL;
337 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000338 self->zst.next_in = NULL;
339 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000340 err = inflateInit2(&self->zst, wbits);
341 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000342 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000343 self->is_initialised = 1;
344 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000345 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000346 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000347 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000348 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000349 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000350 Py_DECREF(self);
351 PyErr_SetString(PyExc_MemoryError,
352 "Can't allocate memory for decompression object");
353 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000354 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000355 zlib_error(self->zst, err, "while creating decompression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000356 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000357 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000358 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000359}
360
361static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000362Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000363{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000364#ifdef WITH_THREAD
365 PyThread_free_lock(self->lock);
366#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000367 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000368 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000369 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000370}
371
372static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000373Comp_dealloc(compobject *self)
374{
375 if (self->is_initialised)
376 deflateEnd(&self->zst);
377 Dealloc(self);
378}
379
380static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000381Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000382{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000383 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000384 inflateEnd(&self->zst);
385 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000386}
387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000388PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000389"compress(data) -- Return a string containing data compressed.\n"
390"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000391"After calling this function, some of the input data may still\n"
392"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000393"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000394
395
Guido van Rossumfb221561997-04-29 15:38:09 +0000396static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000397PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000398{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000399 int err, inplen, length = DEFAULTALLOC;
400 PyObject *RetVal;
Martin v. Löwis423be952008-08-13 15:53:07 +0000401 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000402 Byte *input;
403 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000404
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000405 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000406 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000407 input = pinput.buf;
408 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000409
Gregory P. Smith693fc462008-09-06 20:13:06 +0000410 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000411 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000412 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000413 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000414
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000415 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000416
Jeremy Hylton9714f992001-10-16 21:19:45 +0000417 start_total_out = self->zst.total_out;
418 self->zst.avail_in = inplen;
419 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000420 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000421 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000422
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000423 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000424 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000425 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000426
Jeremy Hylton9714f992001-10-16 21:19:45 +0000427 /* while Z_OK and the output buffer is full, there might be more output,
428 so extend the output buffer and try again */
429 while (err == Z_OK && self->zst.avail_out == 0) {
Gregory P. Smith693fc462008-09-06 20:13:06 +0000430 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000431 Py_DECREF(RetVal);
432 RetVal = NULL;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000433 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000434 }
435 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000436 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000437 self->zst.avail_out = length;
438 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000439
Jeremy Hylton9714f992001-10-16 21:19:45 +0000440 Py_BEGIN_ALLOW_THREADS
441 err = deflate(&(self->zst), Z_NO_FLUSH);
442 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000443 }
Tim Peters977e5402001-10-17 03:57:20 +0000444 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000445 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000446 condition.
447 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000448
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000449 if (err != Z_OK && err != Z_BUF_ERROR) {
450 zlib_error(self->zst, err, "while compressing");
451 Py_DECREF(RetVal);
452 RetVal = NULL;
453 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000454 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000455 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000456 Py_DECREF(RetVal);
457 RetVal = NULL;
458 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000459
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000460 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000461 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000462 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000463 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000464}
465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000466PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000467"decompress(data, max_length) -- Return a string containing the decompressed\n"
468"version of the data.\n"
469"\n"
470"After calling this function, some of the input data may still be stored in\n"
471"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000472"Call the flush() method to clear these buffers.\n"
473"If the max_length parameter is specified then the return value will be\n"
474"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000475"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000476
Guido van Rossumfb221561997-04-29 15:38:09 +0000477static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000478PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000479{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000480 int err, inplen, old_length, length = DEFAULTALLOC;
481 int max_length = 0;
482 PyObject *RetVal;
Martin v. Löwis423be952008-08-13 15:53:07 +0000483 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000484 Byte *input;
485 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000486
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000487 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Martin v. Löwis423be952008-08-13 15:53:07 +0000488 &max_length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000489 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000490 input = pinput.buf;
491 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000492 if (max_length < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000493 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000494 PyErr_SetString(PyExc_ValueError,
495 "max_length must be greater than zero");
496 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000497 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000498
Jeremy Hylton9714f992001-10-16 21:19:45 +0000499 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000500 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000501 length = max_length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000502 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Martin v. Löwis423be952008-08-13 15:53:07 +0000503 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000504 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000505 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000506
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000507 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000508
Jeremy Hylton9714f992001-10-16 21:19:45 +0000509 start_total_out = self->zst.total_out;
510 self->zst.avail_in = inplen;
511 self->zst.next_in = input;
512 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000513 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000514
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000515 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000516 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000517 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000518
Jeremy Hylton9714f992001-10-16 21:19:45 +0000519 /* While Z_OK and the output buffer is full, there might be more output.
520 So extend the output buffer and try again.
521 */
Tim Peters977e5402001-10-17 03:57:20 +0000522 while (err == Z_OK && self->zst.avail_out == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000523 /* If max_length set, don't continue decompressing if we've already
524 reached the limit.
525 */
526 if (max_length && length >= max_length)
527 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000528
Jeremy Hylton9714f992001-10-16 21:19:45 +0000529 /* otherwise, ... */
530 old_length = length;
531 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000532 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000533 length = max_length;
534
Gregory P. Smith693fc462008-09-06 20:13:06 +0000535 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000536 Py_DECREF(RetVal);
537 RetVal = NULL;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000538 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000539 }
540 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000541 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000542 self->zst.avail_out = length - old_length;
543
544 Py_BEGIN_ALLOW_THREADS
545 err = inflate(&(self->zst), Z_SYNC_FLUSH);
546 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000547 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000548
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000549 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000550 of specified size. Return the unconsumed tail in an attribute.*/
551 if(max_length) {
552 Py_DECREF(self->unconsumed_tail);
Gregory P. Smith693fc462008-09-06 20:13:06 +0000553 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000554 self->zst.avail_in);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000555 if(!self->unconsumed_tail) {
556 Py_DECREF(RetVal);
557 RetVal = NULL;
558 goto error;
559 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000560 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000561
Tim Peters977e5402001-10-17 03:57:20 +0000562 /* The end of the compressed data has been reached, so set the
563 unused_data attribute to a string containing the remainder of the
564 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000565 inflateEnd, but the old behaviour of only calling it on flush() is
566 preserved.
567 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000568 if (err == Z_STREAM_END) {
569 Py_XDECREF(self->unused_data); /* Free original empty string */
Gregory P. Smith693fc462008-09-06 20:13:06 +0000570 self->unused_data = PyBytes_FromStringAndSize(
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000571 (char *)self->zst.next_in, self->zst.avail_in);
572 if (self->unused_data == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000573 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000574 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000575 }
Tim Peters977e5402001-10-17 03:57:20 +0000576 /* We will only get Z_BUF_ERROR if the output buffer was full
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000577 but there wasn't more output when we tried again, so it is
578 not an error condition.
579 */
580 } else if (err != Z_OK && err != Z_BUF_ERROR) {
581 zlib_error(self->zst, err, "while decompressing");
582 Py_DECREF(RetVal);
583 RetVal = NULL;
584 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000585 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000586
Gregory P. Smith693fc462008-09-06 20:13:06 +0000587 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000588 Py_DECREF(RetVal);
589 RetVal = NULL;
590 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000591
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000592 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000593 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000594 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000596}
597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000598PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000599"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000600"\n"
601"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000602"default value used when mode is not specified is Z_FINISH.\n"
603"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000604"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000605
Guido van Rossumfb221561997-04-29 15:38:09 +0000606static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000607PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000608{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000609 int err, length = DEFAULTALLOC;
610 PyObject *RetVal;
611 int flushmode = Z_FINISH;
612 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000613
Jeremy Hylton9714f992001-10-16 21:19:45 +0000614 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
615 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000616
Jeremy Hylton9714f992001-10-16 21:19:45 +0000617 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
618 doing any work at all; just return an empty string. */
619 if (flushmode == Z_NO_FLUSH) {
Gregory P. Smith693fc462008-09-06 20:13:06 +0000620 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000621 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000622
Gregory P. Smith693fc462008-09-06 20:13:06 +0000623 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000624 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000625
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000626 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000627
Jeremy Hylton9714f992001-10-16 21:19:45 +0000628 start_total_out = self->zst.total_out;
629 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000630 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000631 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000632
633 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000634 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000635 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000636
Jeremy Hylton9714f992001-10-16 21:19:45 +0000637 /* while Z_OK and the output buffer is full, there might be more output,
638 so extend the output buffer and try again */
639 while (err == Z_OK && self->zst.avail_out == 0) {
Gregory P. Smith693fc462008-09-06 20:13:06 +0000640 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000641 Py_DECREF(RetVal);
642 RetVal = NULL;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000643 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000644 }
645 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000646 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647 self->zst.avail_out = length;
648 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000649
Jeremy Hylton9714f992001-10-16 21:19:45 +0000650 Py_BEGIN_ALLOW_THREADS
651 err = deflate(&(self->zst), flushmode);
652 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000653 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000654
Jeremy Hylton9714f992001-10-16 21:19:45 +0000655 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000656 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000657 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000658 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
659 err = deflateEnd(&(self->zst));
660 if (err != Z_OK) {
661 zlib_error(self->zst, err, "from deflateEnd()");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000662 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000663 RetVal = NULL;
664 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000665 }
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000666 else
667 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000668
669 /* We will only get Z_BUF_ERROR if the output buffer was full
670 but there wasn't more output when we tried again, so it is
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000671 not an error condition.
672 */
673 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
674 zlib_error(self->zst, err, "while flushing");
675 Py_DECREF(RetVal);
676 RetVal = NULL;
Jeremy Hyltonc72737e2002-04-19 14:37:07 +0000677 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000678 }
Tim Peters977e5402001-10-17 03:57:20 +0000679
Gregory P. Smith693fc462008-09-06 20:13:06 +0000680 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000681 Py_DECREF(RetVal);
682 RetVal = NULL;
683 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000684
Tim Peters977e5402001-10-17 03:57:20 +0000685 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000686 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000687
688 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000689}
690
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000691#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000692PyDoc_STRVAR(comp_copy__doc__,
693"copy() -- Return a copy of the compression object.");
694
695static PyObject *
696PyZlib_copy(compobject *self)
697{
698 compobject *retval = NULL;
699 int err;
700
701 retval = newcompobject(&Comptype);
702 if (!retval) return NULL;
703
704 /* Copy the zstream state
705 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
706 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000707 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 err = deflateCopy(&retval->zst, &self->zst);
709 switch(err) {
710 case(Z_OK):
711 break;
712 case(Z_STREAM_ERROR):
713 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
714 goto error;
715 case(Z_MEM_ERROR):
716 PyErr_SetString(PyExc_MemoryError,
717 "Can't allocate memory for compression object");
718 goto error;
719 default:
720 zlib_error(self->zst, err, "while copying compression object");
721 goto error;
722 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000723 Py_INCREF(self->unused_data);
724 Py_INCREF(self->unconsumed_tail);
725 Py_XDECREF(retval->unused_data);
726 Py_XDECREF(retval->unconsumed_tail);
727 retval->unused_data = self->unused_data;
728 retval->unconsumed_tail = self->unconsumed_tail;
729
730 /* Mark it as being initialized */
731 retval->is_initialised = 1;
732
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000733 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000734 return (PyObject *)retval;
735
736error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000737 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738 Py_XDECREF(retval);
739 return NULL;
740}
741
742PyDoc_STRVAR(decomp_copy__doc__,
743"copy() -- Return a copy of the decompression object.");
744
745static PyObject *
746PyZlib_uncopy(compobject *self)
747{
748 compobject *retval = NULL;
749 int err;
750
751 retval = newcompobject(&Decomptype);
752 if (!retval) return NULL;
753
754 /* Copy the zstream state
755 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
756 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000757 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758 err = inflateCopy(&retval->zst, &self->zst);
759 switch(err) {
760 case(Z_OK):
761 break;
762 case(Z_STREAM_ERROR):
763 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
764 goto error;
765 case(Z_MEM_ERROR):
766 PyErr_SetString(PyExc_MemoryError,
767 "Can't allocate memory for decompression object");
768 goto error;
769 default:
770 zlib_error(self->zst, err, "while copying decompression object");
771 goto error;
772 }
773
774 Py_INCREF(self->unused_data);
775 Py_INCREF(self->unconsumed_tail);
776 Py_XDECREF(retval->unused_data);
777 Py_XDECREF(retval->unconsumed_tail);
778 retval->unused_data = self->unused_data;
779 retval->unconsumed_tail = self->unconsumed_tail;
780
781 /* Mark it as being initialized */
782 retval->is_initialised = 1;
783
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000784 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785 return (PyObject *)retval;
786
787error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000788 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789 Py_XDECREF(retval);
790 return NULL;
791}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000792#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000794PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000795"flush( [length] ) -- Return a string containing any remaining\n"
796"decompressed data. length, if given, is the initial size of the\n"
797"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000798"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000799"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000800
Guido van Rossumfb221561997-04-29 15:38:09 +0000801static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000802PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000803{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000804 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000805 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000806 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000807
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000808 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000809 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000810 if (length <= 0) {
811 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
812 return NULL;
813 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000814 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000815 return NULL;
816
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000817
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000818 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000819
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000820 start_total_out = self->zst.total_out;
821 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000822 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000823
824 Py_BEGIN_ALLOW_THREADS
825 err = inflate(&(self->zst), Z_FINISH);
826 Py_END_ALLOW_THREADS
827
828 /* while Z_OK and the output buffer is full, there might be more output,
829 so extend the output buffer and try again */
830 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Gregory P. Smith693fc462008-09-06 20:13:06 +0000831 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000832 Py_DECREF(retval);
833 retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000834 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000835 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000836 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000837 self->zst.avail_out = length;
838 length = length << 1;
839
840 Py_BEGIN_ALLOW_THREADS
841 err = inflate(&(self->zst), Z_FINISH);
842 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000843 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000844
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000845 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
846 various data structures. Note we should only get Z_STREAM_END when
847 flushmode is Z_FINISH */
848 if (err == Z_STREAM_END) {
849 err = inflateEnd(&(self->zst));
850 self->is_initialised = 0;
851 if (err != Z_OK) {
852 zlib_error(self->zst, err, "from inflateEnd()");
853 Py_DECREF(retval);
854 retval = NULL;
855 goto error;
856 }
857 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000858 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000859 Py_DECREF(retval);
860 retval = NULL;
861 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000862
863error:
864
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000865 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000866
Jeremy Hylton9714f992001-10-16 21:19:45 +0000867 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000868}
869
870static PyMethodDef comp_methods[] =
871{
Tim Peters977e5402001-10-17 03:57:20 +0000872 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000873 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000874 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000875 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000876#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000877 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
878 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000879#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000880 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000881};
882
883static PyMethodDef Decomp_methods[] =
884{
Tim Peters977e5402001-10-17 03:57:20 +0000885 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000886 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000887 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000888 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000889#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
891 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000892#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000893 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000894};
895
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000896#define COMP_OFF(x) offsetof(compobject, x)
897static PyMemberDef Decomp_members[] = {
898 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
899 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
900 {NULL},
901};
Guido van Rossumfb221561997-04-29 15:38:09 +0000902
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000903PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000904"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
905"\n"
906"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000907"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000908
Guido van Rossumfb221561997-04-29 15:38:09 +0000909static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000910PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000911{
Christian Heimescc47b052008-03-25 14:56:36 +0000912 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000913 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000914
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000915 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
916 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000917 /* Releasing the GIL for very small buffers is inefficient
918 and may lower performance */
919 if (pbuf.len > 1024*5) {
920 Py_BEGIN_ALLOW_THREADS
921 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
922 Py_END_ALLOW_THREADS
923 } else {
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000924 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000925 }
926 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000927 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000928}
Tim Peters977e5402001-10-17 03:57:20 +0000929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000930PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000931"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
932"\n"
933"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000934"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000935
936static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000937PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000938{
Christian Heimescc47b052008-03-25 14:56:36 +0000939 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000940 Py_buffer pbuf;
941 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000942
Antoine Pitrou77b338b2009-12-14 18:00:06 +0000943 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000944 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) {
948 Py_BEGIN_ALLOW_THREADS
949 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
950 Py_END_ALLOW_THREADS
951 } else {
952 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
953 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000954 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +0000955 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000956}
Tim Peters977e5402001-10-17 03:57:20 +0000957
Guido van Rossumfb221561997-04-29 15:38:09 +0000958
959static PyMethodDef zlib_methods[] =
960{
Tim Peters977e5402001-10-17 03:57:20 +0000961 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000962 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000963 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000964 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000965 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000966 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000967 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
968 crc32__doc__},
969 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000970 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000971 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000972 decompressobj__doc__},
973 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000974};
975
Tim Peters0c322792002-07-17 16:49:03 +0000976static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000977 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000978 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000979 sizeof(compobject),
980 0,
981 (destructor)Comp_dealloc, /*tp_dealloc*/
982 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000983 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +0000984 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000985 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +0000986 0, /*tp_repr*/
987 0, /*tp_as_number*/
988 0, /*tp_as_sequence*/
989 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000990 0, /*tp_hash*/
991 0, /*tp_call*/
992 0, /*tp_str*/
993 0, /*tp_getattro*/
994 0, /*tp_setattro*/
995 0, /*tp_as_buffer*/
996 Py_TPFLAGS_DEFAULT, /*tp_flags*/
997 0, /*tp_doc*/
998 0, /*tp_traverse*/
999 0, /*tp_clear*/
1000 0, /*tp_richcompare*/
1001 0, /*tp_weaklistoffset*/
1002 0, /*tp_iter*/
1003 0, /*tp_iternext*/
1004 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001005};
1006
Tim Peters0c322792002-07-17 16:49:03 +00001007static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001008 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001009 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001010 sizeof(compobject),
1011 0,
1012 (destructor)Decomp_dealloc, /*tp_dealloc*/
1013 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001014 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001015 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001016 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001017 0, /*tp_repr*/
1018 0, /*tp_as_number*/
1019 0, /*tp_as_sequence*/
1020 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001021 0, /*tp_hash*/
1022 0, /*tp_call*/
1023 0, /*tp_str*/
1024 0, /*tp_getattro*/
1025 0, /*tp_setattro*/
1026 0, /*tp_as_buffer*/
1027 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1028 0, /*tp_doc*/
1029 0, /*tp_traverse*/
1030 0, /*tp_clear*/
1031 0, /*tp_richcompare*/
1032 0, /*tp_weaklistoffset*/
1033 0, /*tp_iter*/
1034 0, /*tp_iternext*/
1035 Decomp_methods, /*tp_methods*/
1036 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001037};
1038
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001039PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001040"The functions in this module allow compression and decompression using the\n"
1041"zlib library, which is based on GNU zip.\n"
1042"\n"
1043"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1044"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001045"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001046"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001047"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001048"decompressobj([wbits]) -- Return a decompressor object.\n"
1049"\n"
1050"'wbits' is window buffer size.\n"
1051"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001052"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001053
Martin v. Löwis1a214512008-06-11 05:26:20 +00001054static struct PyModuleDef zlibmodule = {
1055 PyModuleDef_HEAD_INIT,
1056 "zlib",
1057 zlib_module_documentation,
1058 -1,
1059 zlib_methods,
1060 NULL,
1061 NULL,
1062 NULL,
1063 NULL
1064};
1065
Mark Hammond62b1ab12002-07-23 06:31:15 +00001066PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001067PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001068{
Fred Drake4baedc12002-04-01 14:53:37 +00001069 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001070 if (PyType_Ready(&Comptype) < 0)
1071 return NULL;
1072 if (PyType_Ready(&Decomptype) < 0)
1073 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001074 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001075 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001076 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001077
Fred Drake4baedc12002-04-01 14:53:37 +00001078 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1079 if (ZlibError != NULL) {
1080 Py_INCREF(ZlibError);
1081 PyModule_AddObject(m, "error", ZlibError);
1082 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001083 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1084 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1085 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1086 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1087 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1088 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1089 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1090 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1091 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001092
Jeremy Hylton9714f992001-10-16 21:19:45 +00001093 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1094 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1095 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1096 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001097
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001098 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001099 if (ver != NULL)
1100 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001101
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001102 PyModule_AddStringConstant(m, "__version__", "1.0");
1103
Martin v. Löwis1a214512008-06-11 05:26:20 +00001104 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001105}