blob: 06b0690ecf8ae2b592a53e8b880ec933dce7273a [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"
8#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +00009
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000010#ifdef WITH_THREAD
11#include "pythread.h"
12
13/* #defs ripped off from _tkinter.c, even though the situation here is much
14 simpler, because we don't have to worry about waiting for Tcl
15 events! And, since zlib itself is threadsafe, we don't need to worry
16 about re-entering zlib functions.
17
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018 N.B.
19
20 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
21 that modify the components of preexisting de/compress objects, it
22 could prove to be a performance gain on multiprocessor machines if
23 there was an de/compress object-specific lock. However, for the
24 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
25 de/compress objects.
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000026 */
27
28static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
29
30#define ENTER_ZLIB \
Tim Peters6605c642001-10-17 03:43:54 +000031 Py_BEGIN_ALLOW_THREADS \
32 PyThread_acquire_lock(zlib_lock, 1); \
33 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000034
35#define LEAVE_ZLIB \
Tim Peters6605c642001-10-17 03:43:54 +000036 PyThread_release_lock(zlib_lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000037
38#else
39
40#define ENTER_ZLIB
41#define LEAVE_ZLIB
42
43#endif
44
Guido van Rossumfb221561997-04-29 15:38:09 +000045/* The following parameters are copied from zutil.h, version 0.95 */
46#define DEFLATED 8
47#if MAX_MEM_LEVEL >= 8
48# define DEF_MEM_LEVEL 8
49#else
50# define DEF_MEM_LEVEL MAX_MEM_LEVEL
51#endif
52#define DEF_WBITS MAX_WBITS
53
Guido van Rossumb729a1d1999-04-07 20:23:17 +000054/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
55#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000056#define PyInit_zlib initzlib
57
Jeremy Hylton938ace62002-07-17 16:30:39 +000058static PyTypeObject Comptype;
59static PyTypeObject Decomptype;
Guido van Rossumfb221561997-04-29 15:38:09 +000060
61static PyObject *ZlibError;
62
Tim Peters977e5402001-10-17 03:57:20 +000063typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000064{
Jeremy Hylton9714f992001-10-16 21:19:45 +000065 PyObject_HEAD
66 z_stream zst;
67 PyObject *unused_data;
68 PyObject *unconsumed_tail;
69 int is_initialised;
Guido van Rossumfb221561997-04-29 15:38:09 +000070} compobject;
71
Jeremy Hylton0965e082001-10-16 21:56:09 +000072static void
73zlib_error(z_stream zst, int err, char *msg)
74{
75 if (zst.msg == Z_NULL)
76 PyErr_Format(ZlibError, "Error %d %s", err, msg);
77 else
78 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
79}
80
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000081PyDoc_STRVAR(compressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000082"compressobj([level]) -- Return a compressor object.\n"
83"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000084"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +000085
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000086PyDoc_STRVAR(decompressobj__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +000087"decompressobj([wbits]) -- Return a decompressor object.\n"
88"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000089"Optional arg wbits is the window buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +000090
Guido van Rossumfb221561997-04-29 15:38:09 +000091static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000092newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000093{
Tim Peters977e5402001-10-17 03:57:20 +000094 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +000095 self = PyObject_New(compobject, type);
96 if (self == NULL)
97 return NULL;
98 self->is_initialised = 0;
99 self->unused_data = PyString_FromString("");
100 if (self->unused_data == NULL) {
101 Py_DECREF(self);
102 return NULL;
103 }
104 self->unconsumed_tail = PyString_FromString("");
105 if (self->unconsumed_tail == NULL) {
106 Py_DECREF(self);
107 return NULL;
108 }
109 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000110}
111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000112PyDoc_STRVAR(compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000113"compress(string[, level]) -- Returned compressed string.\n"
114"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115"Optional arg level is the compression level, in 1-9.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000116
Guido van Rossumfb221561997-04-29 15:38:09 +0000117static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000118PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000119{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000120 PyObject *ReturnVal = NULL;
121 Byte *input, *output;
122 int length, level=Z_DEFAULT_COMPRESSION, err;
123 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000124
Jeremy Hylton9714f992001-10-16 21:19:45 +0000125 /* require Python string object, optional 'level' arg */
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000126 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000127 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000128
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000130
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000131 output = (Byte*)malloc(zst.avail_out);
132 if (output == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000133 PyErr_SetString(PyExc_MemoryError,
134 "Can't allocate memory to compress data");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000135 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000136 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000137
Jeremy Hylton9714f992001-10-16 21:19:45 +0000138 /* Past the point of no return. From here on out, we need to make sure
139 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000140
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000141 zst.zalloc = (alloc_func)NULL;
142 zst.zfree = (free_func)Z_NULL;
143 zst.next_out = (Byte *)output;
144 zst.next_in = (Byte *)input;
145 zst.avail_in = length;
146 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000147
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000148 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000149 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000150 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000151 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000152 PyErr_SetString(PyExc_MemoryError,
153 "Out of memory while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000154 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000155 case(Z_STREAM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000156 PyErr_SetString(ZlibError,
157 "Bad compression level");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000158 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000159 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000160 deflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000161 zlib_error(zst, err, "while compressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000162 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000163 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000164
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000165 Py_BEGIN_ALLOW_THREADS;
166 err = deflate(&zst, Z_FINISH);
167 Py_END_ALLOW_THREADS;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000168
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000169 if (err != Z_STREAM_END) {
170 zlib_error(zst, err, "while compressing data");
171 deflateEnd(&zst);
172 goto error;
173 }
Tim Peters977e5402001-10-17 03:57:20 +0000174
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000175 err=deflateEnd(&zst);
176 if (err == Z_OK)
Tim Peters977e5402001-10-17 03:57:20 +0000177 ReturnVal = PyString_FromStringAndSize((char *)output,
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000178 zst.total_out);
Tim Peters977e5402001-10-17 03:57:20 +0000179 else
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000180 zlib_error(zst, err, "while finishing compression");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000181
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000182 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000183 free(output);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000184
Jeremy Hylton9714f992001-10-16 21:19:45 +0000185 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000186}
187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188PyDoc_STRVAR(decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000189"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
190"\n"
191"Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000192"the initial output buffer size.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000193
Guido van Rossumfb221561997-04-29 15:38:09 +0000194static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000195PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000196{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000197 PyObject *result_str;
198 Byte *input;
199 int length, err;
200 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
201 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000202
Tim Peters977e5402001-10-17 03:57:20 +0000203 if (!PyArg_ParseTuple(args, "s#|ii:decompress",
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000204 &input, &length, &wsize, &r_strlen))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000205 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000206
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207 if (r_strlen <= 0)
208 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000209
Jeremy Hylton9714f992001-10-16 21:19:45 +0000210 zst.avail_in = length;
211 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000212
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000213 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000214 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000215
Jeremy Hylton9714f992001-10-16 21:19:45 +0000216 zst.zalloc = (alloc_func)NULL;
217 zst.zfree = (free_func)Z_NULL;
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000218 zst.next_out = (Byte *)PyString_AS_STRING(result_str);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000219 zst.next_in = (Byte *)input;
220 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000221
Jeremy Hylton9714f992001-10-16 21:19:45 +0000222 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000223 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000224 break;
Tim Peters977e5402001-10-17 03:57:20 +0000225 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000226 PyErr_SetString(PyExc_MemoryError,
227 "Out of memory while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000228 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000229 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000230 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000231 zlib_error(zst, err, "while preparing to decompress data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000232 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000233 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000234
Jeremy Hylton9714f992001-10-16 21:19:45 +0000235 do {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000236 Py_BEGIN_ALLOW_THREADS
237 err=inflate(&zst, Z_FINISH);
238 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000239
Jeremy Hylton9714f992001-10-16 21:19:45 +0000240 switch(err) {
241 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000242 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000243 case(Z_BUF_ERROR):
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000244 /*
245 * If there is at least 1 byte of room according to zst.avail_out
246 * and we get this error, assume that it means zlib cannot
247 * process the inflate call() due to an error in the data.
248 */
Jeremy Hylton0965e082001-10-16 21:56:09 +0000249 if (zst.avail_out > 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000250 PyErr_Format(ZlibError, "Error %i while decompressing data",
251 err);
252 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000253 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000254 }
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000255 /* fall through */
256 case(Z_OK):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000257 /* need more memory */
Tim Peters5de98422002-04-27 18:44:32 +0000258 if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000259 inflateEnd(&zst);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000260 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000261 }
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000262 zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000263 + r_strlen;
264 zst.avail_out = r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000265 r_strlen = r_strlen << 1;
266 break;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000267 default:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000268 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000269 zlib_error(zst, err, "while decompressing data");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000270 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000271 }
272 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000273
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000274 err = inflateEnd(&zst);
275 if (err != Z_OK) {
276 zlib_error(zst, err, "while finishing data decompression");
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000277 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000278 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000279
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000280 _PyString_Resize(&result_str, zst.total_out);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000281 return result_str;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000282
283 error:
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000284 Py_XDECREF(result_str);
285 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000286}
287
288static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000289PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000290{
Jeremy Hylton499000002001-10-16 21:59:35 +0000291 compobject *self;
292 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
293 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000294
Jeremy Hylton499000002001-10-16 21:59:35 +0000295 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
296 &memLevel, &strategy))
297 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000298
Jeremy Hylton499000002001-10-16 21:59:35 +0000299 self = newcompobject(&Comptype);
Tim Peters977e5402001-10-17 03:57:20 +0000300 if (self==NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000301 return(NULL);
302 self->zst.zalloc = (alloc_func)NULL;
303 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000304 self->zst.next_in = NULL;
305 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000306 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
307 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000308 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000309 self->is_initialised = 1;
310 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000311 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000312 Py_DECREF(self);
313 PyErr_SetString(PyExc_MemoryError,
314 "Can't allocate memory for compression object");
315 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000316 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000317 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000318 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000319 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000320 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000321 zlib_error(self->zst, err, "while creating compression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000322 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000323 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000324 }
325}
326
327static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000328PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000329{
Jeremy Hylton499000002001-10-16 21:59:35 +0000330 int wbits=DEF_WBITS, err;
331 compobject *self;
332 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
333 return NULL;
334
335 self = newcompobject(&Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000336 if (self == NULL)
Jeremy Hylton499000002001-10-16 21:59:35 +0000337 return(NULL);
338 self->zst.zalloc = (alloc_func)NULL;
339 self->zst.zfree = (free_func)Z_NULL;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000340 self->zst.next_in = NULL;
341 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000342 err = inflateInit2(&self->zst, wbits);
343 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000344 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000345 self->is_initialised = 1;
346 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000347 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000348 Py_DECREF(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000349 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Jeremy Hylton499000002001-10-16 21:59:35 +0000350 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000351 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000352 Py_DECREF(self);
353 PyErr_SetString(PyExc_MemoryError,
354 "Can't allocate memory for decompression object");
355 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000356 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000357 zlib_error(self->zst, err, "while creating decompression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000358 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000359 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000360 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000361}
362
363static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000364Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000365{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000366 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000367 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000368 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000369 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000370 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000371}
372
373static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000374Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000375{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000376 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000377 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000378 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000379 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000380 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000381}
382
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383PyDoc_STRVAR(comp_compress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000384"compress(data) -- Return a string containing data compressed.\n"
385"\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000386"After calling this function, some of the input data may still\n"
387"be stored in internal buffers for later processing.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000388"Call the flush() method to clear these buffers.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000389
390
Guido van Rossumfb221561997-04-29 15:38:09 +0000391static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000392PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000393{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000394 int err, inplen, length = DEFAULTALLOC;
395 PyObject *RetVal;
396 Byte *input;
397 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000398
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000399 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000400 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000401
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000402 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000403 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000404
405 ENTER_ZLIB
406
Jeremy Hylton9714f992001-10-16 21:19:45 +0000407 start_total_out = self->zst.total_out;
408 self->zst.avail_in = inplen;
409 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000410 self->zst.avail_out = length;
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000411 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000412
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000413 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000414 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000415 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000416
Jeremy Hylton9714f992001-10-16 21:19:45 +0000417 /* while Z_OK and the output buffer is full, there might be more output,
418 so extend the output buffer and try again */
419 while (err == Z_OK && self->zst.avail_out == 0) {
Tim Peters5de98422002-04-27 18:44:32 +0000420 if (_PyString_Resize(&RetVal, length << 1) < 0)
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000421 goto error;
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000422 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000423 + length;
424 self->zst.avail_out = length;
425 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000426
Jeremy Hylton9714f992001-10-16 21:19:45 +0000427 Py_BEGIN_ALLOW_THREADS
428 err = deflate(&(self->zst), Z_NO_FLUSH);
429 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000430 }
Tim Peters977e5402001-10-17 03:57:20 +0000431 /* We will only get Z_BUF_ERROR if the output buffer was full but
Jeremy Hylton9714f992001-10-16 21:19:45 +0000432 there wasn't more output when we tried again, so it is not an error
Tim Peters977e5402001-10-17 03:57:20 +0000433 condition.
434 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000435
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000436 if (err != Z_OK && err != Z_BUF_ERROR) {
437 zlib_error(self->zst, err, "while compressing");
438 Py_DECREF(RetVal);
439 RetVal = NULL;
440 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000441 }
Tim Peters5de98422002-04-27 18:44:32 +0000442 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000443
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000444 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000445 LEAVE_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000446 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000447}
448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000449PyDoc_STRVAR(decomp_decompress__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000450"decompress(data, max_length) -- Return a string containing the decompressed\n"
451"version of the data.\n"
452"\n"
453"After calling this function, some of the input data may still be stored in\n"
454"internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000455"Call the flush() method to clear these buffers.\n"
456"If the max_length parameter is specified then the return value will be\n"
457"no longer than max_length. Unconsumed input data will be stored in\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000458"the unconsumed_tail attribute.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000459
Guido van Rossumfb221561997-04-29 15:38:09 +0000460static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000461PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000462{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000463 int err, inplen, old_length, length = DEFAULTALLOC;
464 int max_length = 0;
465 PyObject *RetVal;
466 Byte *input;
467 unsigned long start_total_out;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000468
Tim Peters977e5402001-10-17 03:57:20 +0000469 if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000470 &inplen, &max_length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000471 return NULL;
472 if (max_length < 0) {
473 PyErr_SetString(PyExc_ValueError,
474 "max_length must be greater than zero");
475 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000476 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000477
Jeremy Hylton9714f992001-10-16 21:19:45 +0000478 /* limit amount of data allocated to max_length */
Tim Peters977e5402001-10-17 03:57:20 +0000479 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000480 length = max_length;
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000481 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000482 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000483
484 ENTER_ZLIB
Jeremy Hylton9714f992001-10-16 21:19:45 +0000485
Jeremy Hylton9714f992001-10-16 21:19:45 +0000486 start_total_out = self->zst.total_out;
487 self->zst.avail_in = inplen;
488 self->zst.next_in = input;
489 self->zst.avail_out = length;
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000490 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000491
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000492 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000493 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000494 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000495
Jeremy Hylton9714f992001-10-16 21:19:45 +0000496 /* While Z_OK and the output buffer is full, there might be more output.
497 So extend the output buffer and try again.
498 */
Tim Peters977e5402001-10-17 03:57:20 +0000499 while (err == Z_OK && self->zst.avail_out == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000500 /* If max_length set, don't continue decompressing if we've already
501 reached the limit.
502 */
503 if (max_length && length >= max_length)
504 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000505
Jeremy Hylton9714f992001-10-16 21:19:45 +0000506 /* otherwise, ... */
507 old_length = length;
508 length = length << 1;
Tim Peters977e5402001-10-17 03:57:20 +0000509 if (max_length && length > max_length)
Jeremy Hylton9714f992001-10-16 21:19:45 +0000510 length = max_length;
511
Tim Peters5de98422002-04-27 18:44:32 +0000512 if (_PyString_Resize(&RetVal, length) < 0)
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000513 goto error;
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000514 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000515 + old_length;
516 self->zst.avail_out = length - old_length;
517
518 Py_BEGIN_ALLOW_THREADS
519 err = inflate(&(self->zst), Z_SYNC_FLUSH);
520 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000521 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000522
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000523 /* Not all of the compressed data could be accommodated in the output buffer
Jeremy Hylton9714f992001-10-16 21:19:45 +0000524 of specified size. Return the unconsumed tail in an attribute.*/
525 if(max_length) {
526 Py_DECREF(self->unconsumed_tail);
Jack Jansen72af01a2001-10-23 22:29:06 +0000527 self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000528 self->zst.avail_in);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000529 if(!self->unconsumed_tail) {
530 Py_DECREF(RetVal);
531 RetVal = NULL;
532 goto error;
533 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000534 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000535
Tim Peters977e5402001-10-17 03:57:20 +0000536 /* The end of the compressed data has been reached, so set the
537 unused_data attribute to a string containing the remainder of the
538 data in the string. Note that this is also a logical place to call
Jeremy Hylton9714f992001-10-16 21:19:45 +0000539 inflateEnd, but the old behaviour of only calling it on flush() is
540 preserved.
541 */
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000542 if (err == Z_STREAM_END) {
543 Py_XDECREF(self->unused_data); /* Free original empty string */
544 self->unused_data = PyString_FromStringAndSize(
545 (char *)self->zst.next_in, self->zst.avail_in);
546 if (self->unused_data == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000547 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000548 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000549 }
Tim Peters977e5402001-10-17 03:57:20 +0000550 /* We will only get Z_BUF_ERROR if the output buffer was full
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000551 but there wasn't more output when we tried again, so it is
552 not an error condition.
553 */
554 } else if (err != Z_OK && err != Z_BUF_ERROR) {
555 zlib_error(self->zst, err, "while decompressing");
556 Py_DECREF(RetVal);
557 RetVal = NULL;
558 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000559 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000560
Tim Peters5de98422002-04-27 18:44:32 +0000561 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000562
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000563 error:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000564 LEAVE_ZLIB
565
566 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000567}
568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000569PyDoc_STRVAR(comp_flush__doc__,
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000570"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000571"\n"
572"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000573"default value used when mode is not specified is Z_FINISH.\n"
574"If mode == Z_FINISH, the compressor object can no longer be used after\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000575"calling the flush() method. Otherwise, more data can still be compressed.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000576
Guido van Rossumfb221561997-04-29 15:38:09 +0000577static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000578PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000579{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000580 int err, length = DEFAULTALLOC;
581 PyObject *RetVal;
582 int flushmode = Z_FINISH;
583 unsigned long start_total_out;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000584
Jeremy Hylton9714f992001-10-16 21:19:45 +0000585 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
586 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000587
Jeremy Hylton9714f992001-10-16 21:19:45 +0000588 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
589 doing any work at all; just return an empty string. */
590 if (flushmode == Z_NO_FLUSH) {
591 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000592 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000593
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000594 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000595 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000596
597 ENTER_ZLIB
Tim Peters977e5402001-10-17 03:57:20 +0000598
Jeremy Hylton9714f992001-10-16 21:19:45 +0000599 start_total_out = self->zst.total_out;
600 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000601 self->zst.avail_out = length;
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000602 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000603
604 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000605 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000606 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000607
Jeremy Hylton9714f992001-10-16 21:19:45 +0000608 /* while Z_OK and the output buffer is full, there might be more output,
609 so extend the output buffer and try again */
610 while (err == Z_OK && self->zst.avail_out == 0) {
Tim Peters5de98422002-04-27 18:44:32 +0000611 if (_PyString_Resize(&RetVal, length << 1) < 0)
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000612 goto error;
Jeremy Hyltonba3dd992001-10-16 23:26:08 +0000613 self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
Jeremy Hylton9714f992001-10-16 21:19:45 +0000614 + length;
615 self->zst.avail_out = length;
616 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000617
Jeremy Hylton9714f992001-10-16 21:19:45 +0000618 Py_BEGIN_ALLOW_THREADS
619 err = deflate(&(self->zst), flushmode);
620 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000621 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000622
Jeremy Hylton9714f992001-10-16 21:19:45 +0000623 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000624 various data structures. Note we should only get Z_STREAM_END when
Jeremy Hylton9714f992001-10-16 21:19:45 +0000625 flushmode is Z_FINISH, but checking both for safety*/
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000626 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
627 err = deflateEnd(&(self->zst));
628 if (err != Z_OK) {
629 zlib_error(self->zst, err, "from deflateEnd()");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000630 Py_DECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000631 RetVal = NULL;
632 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000633 }
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000634 else
635 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000636
637 /* We will only get Z_BUF_ERROR if the output buffer was full
638 but there wasn't more output when we tried again, so it is
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000639 not an error condition.
640 */
641 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
642 zlib_error(self->zst, err, "while flushing");
643 Py_DECREF(RetVal);
644 RetVal = NULL;
Jeremy Hyltonc72737e2002-04-19 14:37:07 +0000645 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000646 }
Tim Peters977e5402001-10-17 03:57:20 +0000647
Tim Peters5de98422002-04-27 18:44:32 +0000648 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000649
Tim Peters977e5402001-10-17 03:57:20 +0000650 error:
Tim Petersb1a37c02001-10-17 03:56:45 +0000651 LEAVE_ZLIB
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000652
653 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000654}
655
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656PyDoc_STRVAR(comp_copy__doc__,
657"copy() -- Return a copy of the compression object.");
658
659static PyObject *
660PyZlib_copy(compobject *self)
661{
662 compobject *retval = NULL;
663 int err;
664
665 retval = newcompobject(&Comptype);
666 if (!retval) return NULL;
667
668 /* Copy the zstream state
669 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
670 */
671 ENTER_ZLIB
672 err = deflateCopy(&retval->zst, &self->zst);
673 switch(err) {
674 case(Z_OK):
675 break;
676 case(Z_STREAM_ERROR):
677 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
678 goto error;
679 case(Z_MEM_ERROR):
680 PyErr_SetString(PyExc_MemoryError,
681 "Can't allocate memory for compression object");
682 goto error;
683 default:
684 zlib_error(self->zst, err, "while copying compression object");
685 goto error;
686 }
687
688 Py_INCREF(self->unused_data);
689 Py_INCREF(self->unconsumed_tail);
690 Py_XDECREF(retval->unused_data);
691 Py_XDECREF(retval->unconsumed_tail);
692 retval->unused_data = self->unused_data;
693 retval->unconsumed_tail = self->unconsumed_tail;
694
695 /* Mark it as being initialized */
696 retval->is_initialised = 1;
697
698 LEAVE_ZLIB
699 return (PyObject *)retval;
700
701error:
702 LEAVE_ZLIB
703 Py_XDECREF(retval);
704 return NULL;
705}
706
707PyDoc_STRVAR(decomp_copy__doc__,
708"copy() -- Return a copy of the decompression object.");
709
710static PyObject *
711PyZlib_uncopy(compobject *self)
712{
713 compobject *retval = NULL;
714 int err;
715
716 retval = newcompobject(&Decomptype);
717 if (!retval) return NULL;
718
719 /* Copy the zstream state
720 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
721 */
722 ENTER_ZLIB
723 err = inflateCopy(&retval->zst, &self->zst);
724 switch(err) {
725 case(Z_OK):
726 break;
727 case(Z_STREAM_ERROR):
728 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
729 goto error;
730 case(Z_MEM_ERROR):
731 PyErr_SetString(PyExc_MemoryError,
732 "Can't allocate memory for decompression object");
733 goto error;
734 default:
735 zlib_error(self->zst, err, "while copying decompression object");
736 goto error;
737 }
738
739 Py_INCREF(self->unused_data);
740 Py_INCREF(self->unconsumed_tail);
741 Py_XDECREF(retval->unused_data);
742 Py_XDECREF(retval->unconsumed_tail);
743 retval->unused_data = self->unused_data;
744 retval->unconsumed_tail = self->unconsumed_tail;
745
746 /* Mark it as being initialized */
747 retval->is_initialised = 1;
748
749 LEAVE_ZLIB
750 return (PyObject *)retval;
751
752error:
753 LEAVE_ZLIB
754 Py_XDECREF(retval);
755 return NULL;
756}
757
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000758PyDoc_STRVAR(decomp_flush__doc__,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000759"flush( [length] ) -- Return a string containing any remaining\n"
760"decompressed data. length, if given, is the initial size of the\n"
761"output buffer.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000762"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000763"The decompressor object can no longer be used after this call.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000764
Guido van Rossumfb221561997-04-29 15:38:09 +0000765static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000766PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000767{
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000768 int err, length = DEFAULTALLOC;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000769 PyObject * retval = NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000770 unsigned long start_total_out;
Tim Peters977e5402001-10-17 03:57:20 +0000771
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000772 if (!PyArg_ParseTuple(args, "|i:flush", &length))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000773 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000774 if (!(retval = PyString_FromStringAndSize(NULL, length)))
775 return NULL;
776
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000777
Jeremy Hylton9714f992001-10-16 21:19:45 +0000778 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000779
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000780 start_total_out = self->zst.total_out;
781 self->zst.avail_out = length;
782 self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
783
784 Py_BEGIN_ALLOW_THREADS
785 err = inflate(&(self->zst), Z_FINISH);
786 Py_END_ALLOW_THREADS
787
788 /* while Z_OK and the output buffer is full, there might be more output,
789 so extend the output buffer and try again */
790 while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
791 if (_PyString_Resize(&retval, length << 1) < 0)
792 goto error;
793 self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
794 self->zst.avail_out = length;
795 length = length << 1;
796
797 Py_BEGIN_ALLOW_THREADS
798 err = inflate(&(self->zst), Z_FINISH);
799 Py_END_ALLOW_THREADS
Jeremy Hylton9714f992001-10-16 21:19:45 +0000800 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000801
Guido van Rossum7d9ea502003-02-03 20:45:52 +0000802 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
803 various data structures. Note we should only get Z_STREAM_END when
804 flushmode is Z_FINISH */
805 if (err == Z_STREAM_END) {
806 err = inflateEnd(&(self->zst));
807 self->is_initialised = 0;
808 if (err != Z_OK) {
809 zlib_error(self->zst, err, "from inflateEnd()");
810 Py_DECREF(retval);
811 retval = NULL;
812 goto error;
813 }
814 }
815 _PyString_Resize(&retval, self->zst.total_out - start_total_out);
816
817error:
818
Jeremy Hylton9714f992001-10-16 21:19:45 +0000819 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000820
Jeremy Hylton9714f992001-10-16 21:19:45 +0000821 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000822}
823
824static PyMethodDef comp_methods[] =
825{
Tim Peters977e5402001-10-17 03:57:20 +0000826 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000827 comp_compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000828 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000829 comp_flush__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830 {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
831 comp_copy__doc__},
Jeremy Hylton9714f992001-10-16 21:19:45 +0000832 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000833};
834
835static PyMethodDef Decomp_methods[] =
836{
Tim Peters977e5402001-10-17 03:57:20 +0000837 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000838 decomp_decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000839 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000840 decomp_flush__doc__},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
842 decomp_copy__doc__},
Jeremy Hylton9714f992001-10-16 21:19:45 +0000843 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000844};
845
846static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000847Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000848{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000849 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
850 internal data. */
851
852 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000853}
854
855static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000856Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000857{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000858 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000859
Jeremy Hylton9714f992001-10-16 21:19:45 +0000860 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000861
Tim Peters977e5402001-10-17 03:57:20 +0000862 if (strcmp(name, "unused_data") == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000863 Py_INCREF(self->unused_data);
864 retval = self->unused_data;
Tim Peters977e5402001-10-17 03:57:20 +0000865 } else if (strcmp(name, "unconsumed_tail") == 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000866 Py_INCREF(self->unconsumed_tail);
867 retval = self->unconsumed_tail;
Tim Peters977e5402001-10-17 03:57:20 +0000868 } else
Jeremy Hylton9714f992001-10-16 21:19:45 +0000869 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000870
Jeremy Hylton9714f992001-10-16 21:19:45 +0000871 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000872
Jeremy Hylton9714f992001-10-16 21:19:45 +0000873 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000874}
875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000876PyDoc_STRVAR(adler32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000877"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
878"\n"
879"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000880"an integer.");
Guido van Rossum3c540301997-06-03 22:21:03 +0000881
Guido van Rossumfb221561997-04-29 15:38:09 +0000882static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000883PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000884{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000885 uLong adler32val = adler32(0L, Z_NULL, 0);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000886 Byte *buf;
887 int len;
Tim Peters977e5402001-10-17 03:57:20 +0000888
Andrew M. Kuchlingbb7e8002005-11-22 15:32:28 +0000889 if (!PyArg_ParseTuple(args, "s#|k:adler32", &buf, &len, &adler32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000890 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000891 adler32val = adler32(adler32val, buf, len);
892 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000893}
Tim Peters977e5402001-10-17 03:57:20 +0000894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000895PyDoc_STRVAR(crc32__doc__,
Tim Petersadbd35b2001-10-17 04:16:15 +0000896"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
897"\n"
898"An optional starting value can be specified. The returned checksum is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000899"an integer.");
Guido van Rossumfb221561997-04-29 15:38:09 +0000900
901static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000902PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000903{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000904 uLong crc32val = crc32(0L, Z_NULL, 0);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000905 Byte *buf;
906 int len;
Andrew M. Kuchlingbb7e8002005-11-22 15:32:28 +0000907 if (!PyArg_ParseTuple(args, "s#|k:crc32", &buf, &len, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000908 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000909 crc32val = crc32(crc32val, buf, len);
910 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000911}
Tim Peters977e5402001-10-17 03:57:20 +0000912
Guido van Rossumfb221561997-04-29 15:38:09 +0000913
914static PyMethodDef zlib_methods[] =
915{
Tim Peters977e5402001-10-17 03:57:20 +0000916 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000917 adler32__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000918 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000919 compress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000920 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000921 compressobj__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000922 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
923 crc32__doc__},
924 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000925 decompress__doc__},
Tim Peters977e5402001-10-17 03:57:20 +0000926 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
Jeremy Hylton9714f992001-10-16 21:19:45 +0000927 decompressobj__doc__},
928 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000929};
930
Tim Peters0c322792002-07-17 16:49:03 +0000931static PyTypeObject Comptype = {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000932 PyObject_HEAD_INIT(0)
933 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000934 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000935 sizeof(compobject),
936 0,
937 (destructor)Comp_dealloc, /*tp_dealloc*/
938 0, /*tp_print*/
939 (getattrfunc)Comp_getattr, /*tp_getattr*/
940 0, /*tp_setattr*/
941 0, /*tp_compare*/
942 0, /*tp_repr*/
943 0, /*tp_as_number*/
944 0, /*tp_as_sequence*/
945 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000946};
947
Tim Peters0c322792002-07-17 16:49:03 +0000948static PyTypeObject Decomptype = {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000949 PyObject_HEAD_INIT(0)
950 0,
Guido van Rossum14648392001-12-08 18:02:58 +0000951 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +0000952 sizeof(compobject),
953 0,
954 (destructor)Decomp_dealloc, /*tp_dealloc*/
955 0, /*tp_print*/
956 (getattrfunc)Decomp_getattr, /*tp_getattr*/
957 0, /*tp_setattr*/
958 0, /*tp_compare*/
959 0, /*tp_repr*/
960 0, /*tp_as_number*/
961 0, /*tp_as_sequence*/
962 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000963};
964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000965PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +0000966"The functions in this module allow compression and decompression using the\n"
967"zlib library, which is based on GNU zip.\n"
968"\n"
969"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
970"compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000971"compressobj([level]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000972"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +0000973"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +0000974"decompressobj([wbits]) -- Return a decompressor object.\n"
975"\n"
976"'wbits' is window buffer size.\n"
977"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000978"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +0000979
Mark Hammond62b1ab12002-07-23 06:31:15 +0000980PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000981PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +0000982{
Fred Drake4baedc12002-04-01 14:53:37 +0000983 PyObject *m, *ver;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000984 Comptype.ob_type = &PyType_Type;
985 Decomptype.ob_type = &PyType_Type;
986 m = Py_InitModule4("zlib", zlib_methods,
987 zlib_module_documentation,
988 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000989 if (m == NULL)
990 return;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000991
Fred Drake4baedc12002-04-01 14:53:37 +0000992 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
993 if (ZlibError != NULL) {
994 Py_INCREF(ZlibError);
995 PyModule_AddObject(m, "error", ZlibError);
996 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000997 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
998 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
999 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1000 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1001 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1002 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1003 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1004 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1005 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Tim Peters977e5402001-10-17 03:57:20 +00001006
Jeremy Hylton9714f992001-10-16 21:19:45 +00001007 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1008 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1009 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1010 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
Tim Peters977e5402001-10-17 03:57:20 +00001011
Jeremy Hylton9714f992001-10-16 21:19:45 +00001012 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001013 if (ver != NULL)
1014 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001015
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001016 PyModule_AddStringConstant(m, "__version__", "1.0");
1017
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001018#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001019 zlib_lock = PyThread_allocate_lock();
Sjoerd Mullender556a9382002-03-11 09:20:47 +00001020#endif /* WITH_THREAD */
Guido van Rossumfb221561997-04-29 15:38:09 +00001021}