blob: fa07739a16f8b0a7634185b084faf58251b6c2ee [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 Pitrou96f212b2010-05-11 23:49:58 +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 Pitrou7f14f0d2010-05-09 16:14:21 +000070 PyErr_Format(ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000071 else
Antoine Pitrou96f212b2010-05-11 23:49:58 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200119 Byte *input, *output = NULL;
120 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 Pitrouc8428d32009-12-14 18:23:30 +0000125 if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200127
128 if (pinput.len > UINT_MAX) {
129 PyErr_SetString(PyExc_OverflowError,
130 "Size does not fit in an unsigned int");
131 goto error;
132 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000133 input = pinput.buf;
134 length = pinput.len;
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 Pitrou7f14f0d2010-05-09 16:14:21 +0000140 PyErr_SetString(PyExc_MemoryError,
141 "Can't allocate memory to compress data");
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200142 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000143 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000144
Jeremy Hylton9714f992001-10-16 21:19:45 +0000145 /* Past the point of no return. From here on out, we need to make sure
146 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000147
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000148 zst.zalloc = (alloc_func)NULL;
149 zst.zfree = (free_func)Z_NULL;
150 zst.next_out = (Byte *)output;
151 zst.next_in = (Byte *)input;
152 zst.avail_in = length;
153 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000154
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000155 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000156 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000157 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000158 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000159 PyErr_SetString(PyExc_MemoryError,
160 "Out of memory while compressing data");
161 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000162 case(Z_STREAM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 PyErr_SetString(ZlibError,
164 "Bad compression level");
165 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000166 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000167 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 zlib_error(zst, err, "while compressing data");
169 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000170 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000171
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000172 Py_BEGIN_ALLOW_THREADS;
173 err = deflate(&zst, Z_FINISH);
174 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000175
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000176 if (err != Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000177 zlib_error(zst, err, "while compressing data");
178 deflateEnd(&zst);
179 goto error;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000180 }
Tim Peters977e5402001-10-17 03:57:20 +0000181
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 err=deflateEnd(&zst);
183 if (err == Z_OK)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000184 ReturnVal = PyBytes_FromStringAndSize((char *)output,
Guido van Rossum776152b2007-05-22 22:44:07 +0000185 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000186 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000188
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000189 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000190 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000191 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000192
Jeremy Hylton9714f992001-10-16 21:19:45 +0000193 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000194}
195
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000197"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
198"\n"
199"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000201
Guido van Rossumfb221561997-04-29 15:38:09 +0000202static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000203PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000204{
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200205 PyObject *result_str = NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000206 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207 Byte *input;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200208 unsigned int length;
209 int err;
Guido van Rossumcd4d4522007-11-22 00:30:02 +0000210 int wsize=DEF_WBITS;
211 Py_ssize_t r_strlen=DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000212 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000213
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000214 if (!PyArg_ParseTuple(args, "y*|in:decompress",
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000215 &pinput, &wsize, &r_strlen))
216 return NULL;
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200217
218 if (pinput.len > UINT_MAX) {
219 PyErr_SetString(PyExc_OverflowError,
220 "Size does not fit in an unsigned int");
221 goto error;
222 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000223 input = pinput.buf;
224 length = pinput.len;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000225
Jeremy Hylton9714f992001-10-16 21:19:45 +0000226 if (r_strlen <= 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000227 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000228
Jeremy Hylton9714f992001-10-16 21:19:45 +0000229 zst.avail_in = length;
230 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000231
Nadeem Vawda1b8a4172011-05-14 22:26:55 +0200232 if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
233 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000234
Jeremy Hylton9714f992001-10-16 21:19:45 +0000235 zst.zalloc = (alloc_func)NULL;
236 zst.zfree = (free_func)Z_NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000237 zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000238 zst.next_in = (Byte *)input;
239 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000240
Jeremy Hylton9714f992001-10-16 21:19:45 +0000241 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000242 case(Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000243 break;
Tim Peters977e5402001-10-17 03:57:20 +0000244 case(Z_MEM_ERROR):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 PyErr_SetString(PyExc_MemoryError,
246 "Out of memory while decompressing data");
247 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000248 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000249 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000250 zlib_error(zst, err, "while preparing to decompress data");
251 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000252 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000253
Jeremy Hylton9714f992001-10-16 21:19:45 +0000254 do {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000255 Py_BEGIN_ALLOW_THREADS
256 err=inflate(&zst, Z_FINISH);
257 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000258
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 switch(err) {
260 case(Z_STREAM_END):
261 break;
262 case(Z_BUF_ERROR):
263 /*
264 * If there is at least 1 byte of room according to zst.avail_out
265 * and we get this error, assume that it means zlib cannot
266 * process the inflate call() due to an error in the data.
267 */
268 if (zst.avail_out > 0) {
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000269 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000270 inflateEnd(&zst);
271 goto error;
272 }
273 /* fall through */
274 case(Z_OK):
275 /* need more memory */
276 if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
277 inflateEnd(&zst);
278 goto error;
279 }
280 zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000281 (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000282 zst.avail_out = r_strlen;
283 r_strlen = r_strlen << 1;
284 break;
285 default:
286 inflateEnd(&zst);
287 zlib_error(zst, err, "while decompressing data");
288 goto error;
289 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000290 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000291
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000292 err = inflateEnd(&zst);
293 if (err != Z_OK) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000294 zlib_error(zst, err, "while finishing data decompression");
295 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000296 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000297
Gregory P. Smith693fc462008-09-06 20:13:06 +0000298 if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000299 goto error;
300
Martin v. Löwis423be952008-08-13 15:53:07 +0000301 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000302 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000303
304 error:
Martin v. Löwis423be952008-08-13 15:53:07 +0000305 PyBuffer_Release(&pinput);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000306 Py_XDECREF(result_str);
307 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000308}
309
310static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000311PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000312{
Jeremy Hylton499000002001-10-16 21:59:35 +0000313 compobject *self;
314 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
315 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000316
Jeremy Hylton499000002001-10-16 21:59:35 +0000317 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000318 &memLevel, &strategy))
319 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000320
Jeremy Hylton499000002001-10-16 21:59:35 +0000321 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000322 if (self==NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000324 self->zst.zalloc = (alloc_func)NULL;
325 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000326 self->zst.next_in = NULL;
327 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000328 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
329 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000330 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 self->is_initialised = 1;
332 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000333 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000334 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 PyErr_SetString(PyExc_MemoryError,
336 "Can't allocate memory for compression object");
337 return NULL;
338 case(Z_STREAM_ERROR):
339 Py_DECREF(self);
340 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
341 return NULL;
342 default:
343 zlib_error(self->zst, err, "while creating compression object");
344 Py_DECREF(self);
345 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000346 }
347}
348
349static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000350PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000351{
Jeremy Hylton499000002001-10-16 21:59:35 +0000352 int wbits=DEF_WBITS, err;
353 compobject *self;
354 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000355 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000356
357 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000358 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000359 return(NULL);
Jeremy Hylton499000002001-10-16 21:59:35 +0000360 self->zst.zalloc = (alloc_func)NULL;
361 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000362 self->zst.next_in = NULL;
363 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000364 err = inflateInit2(&self->zst, wbits);
365 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000366 case (Z_OK):
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 self->is_initialised = 1;
368 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000369 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000370 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
372 return NULL;
373 case (Z_MEM_ERROR):
374 Py_DECREF(self);
375 PyErr_SetString(PyExc_MemoryError,
376 "Can't allocate memory for decompression object");
377 return NULL;
378 default:
379 zlib_error(self->zst, err, "while creating decompression object");
380 Py_DECREF(self);
381 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000382 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000383}
384
385static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000386Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000387{
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000388#ifdef WITH_THREAD
389 PyThread_free_lock(self->lock);
390#endif
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000391 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000392 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000393 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000394}
395
396static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000397Comp_dealloc(compobject *self)
398{
399 if (self->is_initialised)
400 deflateEnd(&self->zst);
401 Dealloc(self);
402}
403
404static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000405Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000406{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000407 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000408 inflateEnd(&self->zst);
409 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000410}
411
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000413"compress(data) -- Return a string containing data compressed.\n"
414"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000415"After calling this function, some of the input data may still\n"
416"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000418
419
Guido van Rossumfb221561997-04-29 15:38:09 +0000420static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000421PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000422{
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000423 int err, inplen;
424 Py_ssize_t length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000425 PyObject *RetVal;
Martin v. Löwis423be952008-08-13 15:53:07 +0000426 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000427 Byte *input;
428 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000429
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000430 if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000431 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000432 input = pinput.buf;
433 inplen = pinput.len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000434
Gregory P. Smith693fc462008-09-06 20:13:06 +0000435 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000436 PyBuffer_Release(&pinput);
437 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000438 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000439
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000440 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000441
Jeremy Hylton9714f992001-10-16 21:19:45 +0000442 start_total_out = self->zst.total_out;
443 self->zst.avail_in = inplen;
444 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000445 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000446 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000447
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000448 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000449 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000450 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000451
Jeremy Hylton9714f992001-10-16 21:19:45 +0000452 /* while Z_OK and the output buffer is full, there might be more output,
453 so extend the output buffer and try again */
454 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000456 Py_DECREF(RetVal);
457 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000458 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000459 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000460 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000461 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 self->zst.avail_out = length;
463 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000464
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000465 Py_BEGIN_ALLOW_THREADS
466 err = deflate(&(self->zst), Z_NO_FLUSH);
467 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000468 }
Tim Peters977e5402001-10-17 03:57:20 +0000469 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000470 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000471 condition.
472 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000473
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000474 if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000475 zlib_error(self->zst, err, "while compressing");
476 Py_DECREF(RetVal);
477 RetVal = NULL;
478 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000479 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000480 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000481 Py_DECREF(RetVal);
482 RetVal = NULL;
483 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000484
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000485 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000486 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000487 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000488 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000489}
490
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000491PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000492"decompress(data, max_length) -- Return a string containing the decompressed\n"
493"version of the data.\n"
494"\n"
495"After calling this function, some of the input data may still be stored in\n"
496"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000497"Call the flush() method to clear these buffers.\n"
498"If the max_length parameter is specified then the return value will be\n"
499"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000500"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000501
Guido van Rossumfb221561997-04-29 15:38:09 +0000502static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000503PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000504{
Antoine Pitrou4b3fe142010-05-07 17:08:54 +0000505 int err, inplen, max_length = 0;
506 Py_ssize_t old_length, length = DEFAULTALLOC;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000507 PyObject *RetVal;
Martin v. Löwis423be952008-08-13 15:53:07 +0000508 Py_buffer pinput;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000509 Byte *input;
510 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000511
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000512 if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000513 &max_length))
514 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000515 input = pinput.buf;
516 inplen = pinput.len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000517 if (max_length < 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000518 PyBuffer_Release(&pinput);
519 PyErr_SetString(PyExc_ValueError,
520 "max_length must be greater than zero");
521 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000522 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000523
Jeremy Hylton9714f992001-10-16 21:19:45 +0000524 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000525 if (max_length && length > max_length)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000526 length = max_length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000527 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000528 PyBuffer_Release(&pinput);
529 return NULL;
Martin v. Löwis423be952008-08-13 15:53:07 +0000530 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000531
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000532 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000533
Jeremy Hylton9714f992001-10-16 21:19:45 +0000534 start_total_out = self->zst.total_out;
535 self->zst.avail_in = inplen;
536 self->zst.next_in = input;
537 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000538 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000539
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000540 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000541 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000542 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000543
Jeremy Hylton9714f992001-10-16 21:19:45 +0000544 /* While Z_OK and the output buffer is full, there might be more output.
545 So extend the output buffer and try again.
546 */
Tim Peters977e5402001-10-17 03:57:20 +0000547 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000548 /* If max_length set, don't continue decompressing if we've already
549 reached the limit.
550 */
551 if (max_length && length >= max_length)
552 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000553
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 /* otherwise, ... */
555 old_length = length;
556 length = length << 1;
557 if (max_length && length > max_length)
558 length = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000559
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 if (_PyBytes_Resize(&RetVal, length) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000561 Py_DECREF(RetVal);
562 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000563 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000564 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000565 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000566 (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000567 self->zst.avail_out = length - old_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000568
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000569 Py_BEGIN_ALLOW_THREADS
570 err = inflate(&(self->zst), Z_SYNC_FLUSH);
571 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000572 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000573
Jeremy Hylton9714f992001-10-16 21:19:45 +0000574 if(max_length) {
Nadeem Vawda7619e882011-05-14 14:05:20 +0200575 /* Not all of the compressed data could be accommodated in a buffer of
576 the specified size. Return the unconsumed tail in an attribute. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 Py_DECREF(self->unconsumed_tail);
578 self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
579 self->zst.avail_in);
Nadeem Vawda7619e882011-05-14 14:05:20 +0200580 }
581 else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
582 /* All of the compressed data was consumed. Clear unconsumed_tail. */
583 Py_DECREF(self->unconsumed_tail);
584 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
585 }
586 if (self->unconsumed_tail == NULL) {
587 Py_DECREF(RetVal);
588 RetVal = NULL;
589 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000590 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000591
Tim Peters977e5402001-10-17 03:57:20 +0000592 /* The end of the compressed data has been reached, so set the
593 unused_data attribute to a string containing the remainder of the
594 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595 inflateEnd, but the old behaviour of only calling it on flush() is
596 preserved.
597 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000598 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000599 Py_XDECREF(self->unused_data); /* Free original empty string */
600 self->unused_data = PyBytes_FromStringAndSize(
601 (char *)self->zst.next_in, self->zst.avail_in);
602 if (self->unused_data == NULL) {
603 Py_DECREF(RetVal);
604 goto error;
605 }
606 /* We will only get Z_BUF_ERROR if the output buffer was full
607 but there wasn't more output when we tried again, so it is
608 not an error condition.
609 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000610 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 zlib_error(self->zst, err, "while decompressing");
612 Py_DECREF(RetVal);
613 RetVal = NULL;
614 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000615 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000616
Gregory P. Smith693fc462008-09-06 20:13:06 +0000617 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000618 Py_DECREF(RetVal);
619 RetVal = NULL;
620 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000621
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000622 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000623 LEAVE_ZLIB(self);
Martin v. Löwis423be952008-08-13 15:53:07 +0000624 PyBuffer_Release(&pinput);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000625 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000626}
627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000628PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000629"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000630"\n"
631"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000632"default value used when mode is not specified is Z_FINISH.\n"
633"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000634"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000635
Guido van Rossumfb221561997-04-29 15:38:09 +0000636static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000637PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000638{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000639 int err, length = DEFAULTALLOC;
640 PyObject *RetVal;
641 int flushmode = Z_FINISH;
642 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000643
Jeremy Hylton9714f992001-10-16 21:19:45 +0000644 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000646
Jeremy Hylton9714f992001-10-16 21:19:45 +0000647 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
648 doing any work at all; just return an empty string. */
649 if (flushmode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000651 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000652
Gregory P. Smith693fc462008-09-06 20:13:06 +0000653 if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000654 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000655
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000656 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000657
Jeremy Hylton9714f992001-10-16 21:19:45 +0000658 start_total_out = self->zst.total_out;
659 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000660 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000661 self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000662
663 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000664 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000665 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000666
Jeremy Hylton9714f992001-10-16 21:19:45 +0000667 /* while Z_OK and the output buffer is full, there might be more output,
668 so extend the output buffer and try again */
669 while (err == Z_OK && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000670 if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000671 Py_DECREF(RetVal);
672 RetVal = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000673 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000674 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000675 self->zst.next_out =
Gregory P. Smith693fc462008-09-06 20:13:06 +0000676 (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000677 self->zst.avail_out = length;
678 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000679
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 Py_BEGIN_ALLOW_THREADS
681 err = deflate(&(self->zst), flushmode);
682 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000683 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000684
Jeremy Hylton9714f992001-10-16 21:19:45 +0000685 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000686 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000687 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000688 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000689 err = deflateEnd(&(self->zst));
690 if (err != Z_OK) {
691 zlib_error(self->zst, err, "from deflateEnd()");
692 Py_DECREF(RetVal);
693 RetVal = NULL;
694 goto error;
695 }
696 else
697 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000698
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000699 /* We will only get Z_BUF_ERROR if the output buffer was full
700 but there wasn't more output when we tried again, so it is
701 not an error condition.
702 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000703 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000704 zlib_error(self->zst, err, "while flushing");
705 Py_DECREF(RetVal);
706 RetVal = NULL;
707 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000708 }
Tim Peters977e5402001-10-17 03:57:20 +0000709
Gregory P. Smith693fc462008-09-06 20:13:06 +0000710 if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000711 Py_DECREF(RetVal);
712 RetVal = NULL;
713 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000714
Tim Peters977e5402001-10-17 03:57:20 +0000715 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000716 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000717
718 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000719}
720
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000721#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000722PyDoc_STRVAR(comp_copy__doc__,
723"copy() -- Return a copy of the compression object.");
724
725static PyObject *
726PyZlib_copy(compobject *self)
727{
728 compobject *retval = NULL;
729 int err;
730
731 retval = newcompobject(&Comptype);
732 if (!retval) return NULL;
733
734 /* Copy the zstream state
735 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
736 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000737 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000738 err = deflateCopy(&retval->zst, &self->zst);
739 switch(err) {
740 case(Z_OK):
741 break;
742 case(Z_STREAM_ERROR):
743 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
744 goto error;
745 case(Z_MEM_ERROR):
746 PyErr_SetString(PyExc_MemoryError,
747 "Can't allocate memory for compression object");
748 goto error;
749 default:
750 zlib_error(self->zst, err, "while copying compression object");
751 goto error;
752 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000753 Py_INCREF(self->unused_data);
754 Py_INCREF(self->unconsumed_tail);
755 Py_XDECREF(retval->unused_data);
756 Py_XDECREF(retval->unconsumed_tail);
757 retval->unused_data = self->unused_data;
758 retval->unconsumed_tail = self->unconsumed_tail;
759
760 /* Mark it as being initialized */
761 retval->is_initialised = 1;
762
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000763 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000764 return (PyObject *)retval;
765
766error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000767 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000768 Py_XDECREF(retval);
769 return NULL;
770}
771
772PyDoc_STRVAR(decomp_copy__doc__,
773"copy() -- Return a copy of the decompression object.");
774
775static PyObject *
776PyZlib_uncopy(compobject *self)
777{
778 compobject *retval = NULL;
779 int err;
780
781 retval = newcompobject(&Decomptype);
782 if (!retval) return NULL;
783
784 /* Copy the zstream state
785 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
786 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000787 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000788 err = inflateCopy(&retval->zst, &self->zst);
789 switch(err) {
790 case(Z_OK):
791 break;
792 case(Z_STREAM_ERROR):
793 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
794 goto error;
795 case(Z_MEM_ERROR):
796 PyErr_SetString(PyExc_MemoryError,
797 "Can't allocate memory for decompression object");
798 goto error;
799 default:
800 zlib_error(self->zst, err, "while copying decompression object");
801 goto error;
802 }
803
804 Py_INCREF(self->unused_data);
805 Py_INCREF(self->unconsumed_tail);
806 Py_XDECREF(retval->unused_data);
807 Py_XDECREF(retval->unconsumed_tail);
808 retval->unused_data = self->unused_data;
809 retval->unconsumed_tail = self->unconsumed_tail;
810
811 /* Mark it as being initialized */
812 retval->is_initialised = 1;
813
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000814 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815 return (PyObject *)retval;
816
817error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000818 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819 Py_XDECREF(retval);
820 return NULL;
821}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000822#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000823
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000824PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825"flush( [length] ) -- Return a string containing any remaining\n"
826"decompressed data. length, if given, is the initial size of the\n"
827"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000828"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000829"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000830
Guido van Rossumfb221561997-04-29 15:38:09 +0000831static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000832PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000833{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000834 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000835 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000836 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000837
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000838 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000839 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000840 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000841 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
842 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +0000843 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000844 if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000845 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000846
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000847
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000848 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000849
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000850 start_total_out = self->zst.total_out;
851 self->zst.avail_out = length;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000852 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000853
854 Py_BEGIN_ALLOW_THREADS
855 err = inflate(&(self->zst), Z_FINISH);
856 Py_END_ALLOW_THREADS
857
858 /* while Z_OK and the output buffer is full, there might be more output,
859 so extend the output buffer and try again */
860 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000861 if (_PyBytes_Resize(&retval, length << 1) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000862 Py_DECREF(retval);
863 retval = NULL;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000864 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000865 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000866 self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
867 self->zst.avail_out = length;
868 length = length << 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000869
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000870 Py_BEGIN_ALLOW_THREADS
871 err = inflate(&(self->zst), Z_FINISH);
872 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000873 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000874
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000875 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
876 various data structures. Note we should only get Z_STREAM_END when
877 flushmode is Z_FINISH */
878 if (err == Z_STREAM_END) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 err = inflateEnd(&(self->zst));
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000880 self->is_initialised = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000881 if (err != Z_OK) {
882 zlib_error(self->zst, err, "from inflateEnd()");
883 Py_DECREF(retval);
884 retval = NULL;
885 goto error;
886 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000887 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000888 if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
Guido van Rossum776152b2007-05-22 22:44:07 +0000889 Py_DECREF(retval);
890 retval = NULL;
891 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000892
893error:
894
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000895 LEAVE_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000896
Jeremy Hylton9714f992001-10-16 21:19:45 +0000897 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000898}
899
900static PyMethodDef comp_methods[] =
901{
Tim Peters977e5402001-10-17 03:57:20 +0000902 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000903 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000904 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000905 comp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000906#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
908 comp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000909#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000910 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000911};
912
913static PyMethodDef Decomp_methods[] =
914{
Tim Peters977e5402001-10-17 03:57:20 +0000915 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000916 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000917 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000918 decomp_flush__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000919#ifdef HAVE_ZLIB_COPY
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
921 decomp_copy__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000922#endif
Jeremy Hylton9714f992001-10-16 21:19:45 +0000923 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000924};
925
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +0000926#define COMP_OFF(x) offsetof(compobject, x)
927static PyMemberDef Decomp_members[] = {
928 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
929 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
930 {NULL},
931};
Guido van Rossumfb221561997-04-29 15:38:09 +0000932
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000933PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000934"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
935"\n"
936"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000937"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000938
Guido van Rossumfb221561997-04-29 15:38:09 +0000939static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000940PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000941{
Christian Heimescc47b052008-03-25 14:56:36 +0000942 unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000943 Py_buffer pbuf;
Tim Peters977e5402001-10-17 03:57:20 +0000944
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000945 if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
946 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000947 /* Releasing the GIL for very small buffers is inefficient
948 and may lower performance */
949 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000950 unsigned char *buf = pbuf.buf;
951 Py_ssize_t len = pbuf.len;
952
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000953 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000954 /* Avoid truncation of length for very large buffers. adler32() takes
955 length as an unsigned int, which may be narrower than Py_ssize_t. */
956 while (len > (size_t) UINT_MAX) {
957 adler32val = adler32(adler32val, buf, UINT_MAX);
958 buf += (size_t) UINT_MAX;
959 len -= (size_t) UINT_MAX;
960 }
961 adler32val = adler32(adler32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000962 Py_END_ALLOW_THREADS
963 } else {
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000964 adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000965 }
966 PyBuffer_Release(&pbuf);
Gregory P. Smith27275032008-03-20 06:20:09 +0000967 return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +0000968}
Tim Peters977e5402001-10-17 03:57:20 +0000969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000970PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000971"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
972"\n"
973"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000974"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000975
976static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000977PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000978{
Christian Heimescc47b052008-03-25 14:56:36 +0000979 unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
Martin v. Löwis423be952008-08-13 15:53:07 +0000980 Py_buffer pbuf;
981 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +0000982
Antoine Pitrouc8428d32009-12-14 18:23:30 +0000983 if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000984 return NULL;
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000985 /* Releasing the GIL for very small buffers is inefficient
986 and may lower performance */
987 if (pbuf.len > 1024*5) {
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000988 unsigned char *buf = pbuf.buf;
989 Py_ssize_t len = pbuf.len;
990
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000991 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +0000992 /* Avoid truncation of length for very large buffers. crc32() takes
993 length as an unsigned int, which may be narrower than Py_ssize_t. */
994 while (len > (size_t) UINT_MAX) {
995 crc32val = crc32(crc32val, buf, UINT_MAX);
996 buf += (size_t) UINT_MAX;
997 len -= (size_t) UINT_MAX;
998 }
999 signed_val = crc32(crc32val, buf, len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001000 Py_END_ALLOW_THREADS
1001 } else {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001002 signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001003 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001004 PyBuffer_Release(&pbuf);
Christian Heimescc47b052008-03-25 14:56:36 +00001005 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001006}
Tim Peters977e5402001-10-17 03:57:20 +00001007
Guido van Rossumfb221561997-04-29 15:38:09 +00001008
1009static PyMethodDef zlib_methods[] =
1010{
Tim Peters977e5402001-10-17 03:57:20 +00001011 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001012 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001013 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001014 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001015 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001016 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001017 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1018 crc32__doc__},
1019 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001020 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +00001021 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +00001022 decompressobj__doc__},
1023 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001024};
1025
Tim Peters0c322792002-07-17 16:49:03 +00001026static PyTypeObject Comptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001027 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001028 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001029 sizeof(compobject),
1030 0,
1031 (destructor)Comp_dealloc, /*tp_dealloc*/
1032 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001033 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001034 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001035 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001036 0, /*tp_repr*/
1037 0, /*tp_as_number*/
1038 0, /*tp_as_sequence*/
1039 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001040 0, /*tp_hash*/
1041 0, /*tp_call*/
1042 0, /*tp_str*/
1043 0, /*tp_getattro*/
1044 0, /*tp_setattro*/
1045 0, /*tp_as_buffer*/
1046 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1047 0, /*tp_doc*/
1048 0, /*tp_traverse*/
1049 0, /*tp_clear*/
1050 0, /*tp_richcompare*/
1051 0, /*tp_weaklistoffset*/
1052 0, /*tp_iter*/
1053 0, /*tp_iternext*/
1054 comp_methods, /*tp_methods*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001055};
1056
Tim Peters0c322792002-07-17 16:49:03 +00001057static PyTypeObject Decomptype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001058 PyVarObject_HEAD_INIT(0, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00001059 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001060 sizeof(compobject),
1061 0,
1062 (destructor)Decomp_dealloc, /*tp_dealloc*/
1063 0, /*tp_print*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001064 0, /*tp_getattr*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001065 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00001066 0, /*tp_reserved*/
Jeremy Hylton9714f992001-10-16 21:19:45 +00001067 0, /*tp_repr*/
1068 0, /*tp_as_number*/
1069 0, /*tp_as_sequence*/
1070 0, /*tp_as_mapping*/
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001071 0, /*tp_hash*/
1072 0, /*tp_call*/
1073 0, /*tp_str*/
1074 0, /*tp_getattro*/
1075 0, /*tp_setattro*/
1076 0, /*tp_as_buffer*/
1077 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1078 0, /*tp_doc*/
1079 0, /*tp_traverse*/
1080 0, /*tp_clear*/
1081 0, /*tp_richcompare*/
1082 0, /*tp_weaklistoffset*/
1083 0, /*tp_iter*/
1084 0, /*tp_iternext*/
1085 Decomp_methods, /*tp_methods*/
1086 Decomp_members, /*tp_members*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001087};
1088
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001089PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001090"The functions in this module allow compression and decompression using the\n"
1091"zlib library, which is based on GNU zip.\n"
1092"\n"
1093"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1094"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +00001095"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001096"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001097"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001098"decompressobj([wbits]) -- Return a decompressor object.\n"
1099"\n"
1100"'wbits' is window buffer size.\n"
1101"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001102"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001103
Martin v. Löwis1a214512008-06-11 05:26:20 +00001104static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001105 PyModuleDef_HEAD_INIT,
1106 "zlib",
1107 zlib_module_documentation,
1108 -1,
1109 zlib_methods,
1110 NULL,
1111 NULL,
1112 NULL,
1113 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001114};
1115
Mark Hammond62b1ab12002-07-23 06:31:15 +00001116PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001117PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001118{
Fred Drake4baedc12002-04-01 14:53:37 +00001119 PyObject *m, *ver;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001120 if (PyType_Ready(&Comptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001121 return NULL;
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001122 if (PyType_Ready(&Decomptype) < 0)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001123 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001124 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001125 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001126 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001127
Fred Drake4baedc12002-04-01 14:53:37 +00001128 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1129 if (ZlibError != NULL) {
1130 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001131 PyModule_AddObject(m, "error", ZlibError);
Fred Drake4baedc12002-04-01 14:53:37 +00001132 }
Jeremy Hylton9714f992001-10-16 21:19:45 +00001133 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1134 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1135 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1136 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1137 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1138 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1139 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1140 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1141 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001142
Jeremy Hylton9714f992001-10-16 21:19:45 +00001143 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1144 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1145 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1146 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001147
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001148 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001149 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001150 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001151
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001152 PyModule_AddStringConstant(m, "__version__", "1.0");
1153
Martin v. Löwis1a214512008-06-11 05:26:20 +00001154 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001155}