blob: 7261ac8cb269a9a0030c9e5b2471b2ca29f3fbe7 [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
18 What we _do_ have to worry about is releasing the global lock _in
19 general_ in the zlibmodule functions, because of all the calls to
20 Python functions, which assume that the global lock is held. So
21 only two types of calls are wrapped in Py_BEGIN/END_ALLOW_THREADS:
22 those that grab the zlib lock, and those that involve other
23 time-consuming functions where we need to worry about holding up
24 other Python threads.
25
26 We don't need to worry about the string inputs being modified out
27 from underneath us, because string objects are immutable. However,
28 we do need to make sure we take on ownership, so that the strings
29 are not deleted out from under us during a thread swap.
30
31 N.B.
32
33 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
34 that modify the components of preexisting de/compress objects, it
35 could prove to be a performance gain on multiprocessor machines if
36 there was an de/compress object-specific lock. However, for the
37 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
38 de/compress objects.
39
40 */
41
42static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
43
44#define ENTER_ZLIB \
45 { Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock(zlib_lock, 1); \
46 Py_END_ALLOW_THREADS
47
48#define LEAVE_ZLIB \
49 PyThread_release_lock(zlib_lock); }
50
51#else
52
53#define ENTER_ZLIB
54#define LEAVE_ZLIB
55
56#endif
57
Guido van Rossumfb221561997-04-29 15:38:09 +000058/* The following parameters are copied from zutil.h, version 0.95 */
59#define DEFLATED 8
60#if MAX_MEM_LEVEL >= 8
61# define DEF_MEM_LEVEL 8
62#else
63# define DEF_MEM_LEVEL MAX_MEM_LEVEL
64#endif
65#define DEF_WBITS MAX_WBITS
66
Guido van Rossumb729a1d1999-04-07 20:23:17 +000067/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
68#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000069#define PyInit_zlib initzlib
70
71staticforward PyTypeObject Comptype;
72staticforward PyTypeObject Decomptype;
73
74static PyObject *ZlibError;
75
76typedef struct
77{
Jeremy Hylton9714f992001-10-16 21:19:45 +000078 PyObject_HEAD
79 z_stream zst;
80 PyObject *unused_data;
81 PyObject *unconsumed_tail;
82 int is_initialised;
Guido van Rossumfb221561997-04-29 15:38:09 +000083} compobject;
84
Jeremy Hylton0965e082001-10-16 21:56:09 +000085static void
86zlib_error(z_stream zst, int err, char *msg)
87{
88 if (zst.msg == Z_NULL)
89 PyErr_Format(ZlibError, "Error %d %s", err, msg);
90 else
91 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
92}
93
Guido van Rossum3c540301997-06-03 22:21:03 +000094static char compressobj__doc__[] =
95"compressobj() -- Return a compressor object.\n"
96"compressobj(level) -- Return a compressor object, using the given compression level.\n"
97;
98
99static char decompressobj__doc__[] =
100"decompressobj() -- Return a decompressor object.\n"
101"decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
102;
103
Guido van Rossumfb221561997-04-29 15:38:09 +0000104static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000105newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000106{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000107 compobject *self;
108 self = PyObject_New(compobject, type);
109 if (self == NULL)
110 return NULL;
111 self->is_initialised = 0;
112 self->unused_data = PyString_FromString("");
113 if (self->unused_data == NULL) {
114 Py_DECREF(self);
115 return NULL;
116 }
117 self->unconsumed_tail = PyString_FromString("");
118 if (self->unconsumed_tail == NULL) {
119 Py_DECREF(self);
120 return NULL;
121 }
122 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000123}
124
Guido van Rossum3c540301997-06-03 22:21:03 +0000125static char compress__doc__[] =
126"compress(string) -- Compress string using the default compression level, "
127"returning a string containing compressed data.\n"
128"compress(string, level) -- Compress string, using the chosen compression "
129"level (from 1 to 9). Return a string containing the compressed data.\n"
130;
131
Guido van Rossumfb221561997-04-29 15:38:09 +0000132static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000133PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000134{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000135 PyObject *ReturnVal = NULL;
136 Byte *input, *output;
137 int length, level=Z_DEFAULT_COMPRESSION, err;
138 z_stream zst;
139 int return_error;
140 PyObject * inputString;
Guido van Rossumfb221561997-04-29 15:38:09 +0000141
Jeremy Hylton9714f992001-10-16 21:19:45 +0000142 /* require Python string object, optional 'level' arg */
143 if (!PyArg_ParseTuple(args, "S|i:compress", &inputString, &level))
144 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000145
Jeremy Hylton9714f992001-10-16 21:19:45 +0000146 /* now get a pointer to the internal string */
147 if (PyString_AsStringAndSize(inputString, (char**)&input, &length) == -1)
148 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000149
Jeremy Hylton9714f992001-10-16 21:19:45 +0000150 zst.avail_out = length + length/1000 + 12 + 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000151
Jeremy Hylton9714f992001-10-16 21:19:45 +0000152 output=(Byte*)malloc(zst.avail_out);
153 if (output==NULL)
Guido van Rossumfb221561997-04-29 15:38:09 +0000154 {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000155 PyErr_SetString(PyExc_MemoryError,
156 "Can't allocate memory to compress data");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000157 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000158 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000159
Jeremy Hylton9714f992001-10-16 21:19:45 +0000160 /* Past the point of no return. From here on out, we need to make sure
161 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000162
Jeremy Hylton9714f992001-10-16 21:19:45 +0000163 Py_INCREF(inputString); /* increment so that we hold ref */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000164
Jeremy Hylton9714f992001-10-16 21:19:45 +0000165 zst.zalloc=(alloc_func)NULL;
166 zst.zfree=(free_func)Z_NULL;
167 zst.next_out=(Byte *)output;
168 zst.next_in =(Byte *)input;
169 zst.avail_in=length;
170 err=deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000171
Jeremy Hylton9714f992001-10-16 21:19:45 +0000172 return_error = 0;
173 switch(err)
Guido van Rossumfb221561997-04-29 15:38:09 +0000174 {
175 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000176 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000177 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000178 PyErr_SetString(PyExc_MemoryError,
179 "Out of memory while compressing data");
180 return_error = 1;
181 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000182 case(Z_STREAM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000183 PyErr_SetString(ZlibError,
184 "Bad compression level");
185 return_error = 1;
186 break;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000187 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000188 deflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000189 zlib_error(zst, err, "while compressing data");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000190 return_error = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000191 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000192
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000193 if (!return_error) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000194 Py_BEGIN_ALLOW_THREADS;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000195 err = deflate(&zst, Z_FINISH);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000196 Py_END_ALLOW_THREADS;
197
Jeremy Hylton0965e082001-10-16 21:56:09 +0000198 if (err != Z_STREAM_END) {
199 zlib_error(zst, err, "while compressing data");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000200 deflateEnd(&zst);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000201 return_error = 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000202 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000203
204 if (!return_error) {
205 err=deflateEnd(&zst);
206 if (err == Z_OK)
207 ReturnVal = PyString_FromStringAndSize((char *)output,
208 zst.total_out);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000209 else
210 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000211 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000212 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000213
Jeremy Hylton9714f992001-10-16 21:19:45 +0000214 free(output);
215 Py_DECREF(inputString);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000216
Jeremy Hylton9714f992001-10-16 21:19:45 +0000217 return ReturnVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000218}
219
Guido van Rossum3c540301997-06-03 22:21:03 +0000220static char decompress__doc__[] =
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000221"decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
222"decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
223"decompress(string, wbits, bufsize) -- Decompress the data in string with a window buffer size of wbits and an initial output buffer size of bufsize.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000224;
225
Guido van Rossumfb221561997-04-29 15:38:09 +0000226static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000227PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000228{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000229 PyObject *result_str;
230 Byte *input;
231 int length, err;
232 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
233 z_stream zst;
234 int return_error;
235 PyObject * inputString;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000236
Jeremy Hylton9714f992001-10-16 21:19:45 +0000237 if (!PyArg_ParseTuple(args, "S|ii:decompress",
238 &inputString, &wsize, &r_strlen))
239 return NULL;
240 if (PyString_AsStringAndSize(inputString, (char**)&input, &length) == -1)
241 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000242
Jeremy Hylton9714f992001-10-16 21:19:45 +0000243 if (r_strlen <= 0)
244 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000245
Jeremy Hylton9714f992001-10-16 21:19:45 +0000246 zst.avail_in = length;
247 zst.avail_out = r_strlen;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000248
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000249 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000250 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000251
Jeremy Hylton9714f992001-10-16 21:19:45 +0000252 /* Past the point of no return. From here on out, we need to make sure
253 we clean up mallocs & INCREFs. */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000254
Jeremy Hylton9714f992001-10-16 21:19:45 +0000255 Py_INCREF(inputString); /* increment so that we hold ref */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000256
Jeremy Hylton9714f992001-10-16 21:19:45 +0000257 zst.zalloc = (alloc_func)NULL;
258 zst.zfree = (free_func)Z_NULL;
259 zst.next_out = (Byte *)PyString_AsString(result_str);
260 zst.next_in = (Byte *)input;
261 err = inflateInit2(&zst, wsize);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000262
Jeremy Hylton9714f992001-10-16 21:19:45 +0000263 return_error = 0;
264 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000265 case(Z_OK):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000266 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000267 case(Z_MEM_ERROR):
Jeremy Hylton9714f992001-10-16 21:19:45 +0000268 PyErr_SetString(PyExc_MemoryError,
269 "Out of memory while decompressing data");
270 return_error = 1;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000271 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000272 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000273 zlib_error(zst, err, "while preparing to decompress data");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000274 return_error = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000275 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000276
Jeremy Hylton9714f992001-10-16 21:19:45 +0000277 do {
278 if (return_error)
279 break;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000280
Jeremy Hylton9714f992001-10-16 21:19:45 +0000281 Py_BEGIN_ALLOW_THREADS
282 err=inflate(&zst, Z_FINISH);
283 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000284
Jeremy Hylton9714f992001-10-16 21:19:45 +0000285 switch(err) {
286 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000287 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000288 case(Z_BUF_ERROR):
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000289 /*
290 * If there is at least 1 byte of room according to zst.avail_out
291 * and we get this error, assume that it means zlib cannot
292 * process the inflate call() due to an error in the data.
293 */
Jeremy Hylton0965e082001-10-16 21:56:09 +0000294 if (zst.avail_out > 0) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000295 PyErr_Format(ZlibError, "Error %i while decompressing data",
296 err);
297 inflateEnd(&zst);
298 return_error = 1;
299 break;
300 }
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000301 /* fall through */
302 case(Z_OK):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000303 /* need more memory */
Jeremy Hylton9714f992001-10-16 21:19:45 +0000304 if (_PyString_Resize(&result_str, r_strlen << 1) == -1) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000305 inflateEnd(&zst);
306 result_str = NULL;
307 return_error = 1;
308 }
309 zst.next_out = (unsigned char *)PyString_AsString(result_str) \
310 + r_strlen;
311 zst.avail_out = r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000312 r_strlen = r_strlen << 1;
313 break;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000314 default:
Jeremy Hylton9714f992001-10-16 21:19:45 +0000315 inflateEnd(&zst);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000316 zlib_error(zst, err, "while decompressing data");
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000317 return_error = 1;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000318 }
319 } while (err != Z_STREAM_END);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000320
Jeremy Hylton9714f992001-10-16 21:19:45 +0000321 if (!return_error) {
322 err = inflateEnd(&zst);
323 if (err != Z_OK) {
Jeremy Hylton0965e082001-10-16 21:56:09 +0000324 zlib_error(zst, err, "while finishing data decompression");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000325 return NULL;
326 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000327 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000328
Jeremy Hylton9714f992001-10-16 21:19:45 +0000329 if (!return_error)
330 _PyString_Resize(&result_str, zst.total_out);
331 else
332 Py_XDECREF(result_str); /* sets result_str == NULL, if not already */
333 Py_DECREF(inputString);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000334
Jeremy Hylton9714f992001-10-16 21:19:45 +0000335 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000336}
337
338static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000339PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000340{
Jeremy Hylton499000002001-10-16 21:59:35 +0000341 compobject *self;
342 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
343 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000344
Jeremy Hylton499000002001-10-16 21:59:35 +0000345 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
346 &memLevel, &strategy))
347 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000348
Jeremy Hylton499000002001-10-16 21:59:35 +0000349 self = newcompobject(&Comptype);
350 if (self==NULL)
351 return(NULL);
352 self->zst.zalloc = (alloc_func)NULL;
353 self->zst.zfree = (free_func)Z_NULL;
354 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
355 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000356 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000357 self->is_initialised = 1;
358 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000359 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000360 Py_DECREF(self);
361 PyErr_SetString(PyExc_MemoryError,
362 "Can't allocate memory for compression object");
363 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000364 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000365 Py_DECREF(self);
366 PyErr_SetString(PyExc_ValueError,
367 "Invalid initialization option");
368 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000369 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000370 zlib_error(self->zst, err, "while creating compression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000371 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000372 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000373 }
374}
375
376static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000377PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000378{
Jeremy Hylton499000002001-10-16 21:59:35 +0000379 int wbits=DEF_WBITS, err;
380 compobject *self;
381 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
382 return NULL;
383
384 self = newcompobject(&Decomptype);
385 if (self==NULL)
386 return(NULL);
387 self->zst.zalloc = (alloc_func)NULL;
388 self->zst.zfree = (free_func)Z_NULL;
389 err = inflateInit2(&self->zst, wbits);
390 switch(err) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000391 case (Z_OK):
Jeremy Hylton499000002001-10-16 21:59:35 +0000392 self->is_initialised = 1;
393 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000394 case(Z_STREAM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000395 Py_DECREF(self);
396 PyErr_SetString(PyExc_ValueError,
397 "Invalid initialization option");
398 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000399 case (Z_MEM_ERROR):
Jeremy Hylton499000002001-10-16 21:59:35 +0000400 Py_DECREF(self);
401 PyErr_SetString(PyExc_MemoryError,
402 "Can't allocate memory for decompression object");
403 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000404 default:
Jeremy Hylton0965e082001-10-16 21:56:09 +0000405 zlib_error(self->zst, err, "while creating decompression object");
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000406 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000407 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000408 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000409}
410
411static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000412Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000413{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000414 ENTER_ZLIB
415
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000416 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000417 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000418 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000419 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000420 PyObject_Del(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000421
422 LEAVE_ZLIB
Guido van Rossumfb221561997-04-29 15:38:09 +0000423}
424
425static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000426Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000427{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000428 ENTER_ZLIB
429
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000430 if (self->is_initialised)
Jeremy Hylton499000002001-10-16 21:59:35 +0000431 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000432 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000433 Py_XDECREF(self->unconsumed_tail);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000434 PyObject_Del(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000435
436 LEAVE_ZLIB
Guido van Rossumfb221561997-04-29 15:38:09 +0000437}
438
Guido van Rossum3c540301997-06-03 22:21:03 +0000439static char comp_compress__doc__[] =
440"compress(data) -- Return a string containing a compressed version of the data.\n\n"
441"After calling this function, some of the input data may still\n"
442"be stored in internal buffers for later processing.\n"
443"Call the flush() method to clear these buffers."
444;
445
446
Guido van Rossumfb221561997-04-29 15:38:09 +0000447static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000448PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000449{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000450 int err, inplen, length = DEFAULTALLOC;
451 PyObject *RetVal;
452 Byte *input;
453 unsigned long start_total_out;
454 int return_error;
455 PyObject * inputString;
Guido van Rossumfb221561997-04-29 15:38:09 +0000456
Jeremy Hylton9714f992001-10-16 21:19:45 +0000457 if (!PyArg_ParseTuple(args, "S:compress", &inputString))
458 return NULL;
459 if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -1)
460 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000461
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000462 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000463 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000464
465 ENTER_ZLIB
466
467 Py_INCREF(inputString);
468
469 start_total_out = self->zst.total_out;
470 self->zst.avail_in = inplen;
471 self->zst.next_in = input;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000472 self->zst.avail_out = length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000473 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
474
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000475 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000476 err = deflate(&(self->zst), Z_NO_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000477 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000478
Jeremy Hylton9714f992001-10-16 21:19:45 +0000479 return_error = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000480
Jeremy Hylton9714f992001-10-16 21:19:45 +0000481 /* while Z_OK and the output buffer is full, there might be more output,
482 so extend the output buffer and try again */
483 while (err == Z_OK && self->zst.avail_out == 0) {
484 if (_PyString_Resize(&RetVal, length << 1) == -1) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000485 return_error = 1;
486 break;
487 }
488 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) \
489 + length;
490 self->zst.avail_out = length;
491 length = length << 1;
492
493 Py_BEGIN_ALLOW_THREADS
494 err = deflate(&(self->zst), Z_NO_FLUSH);
495 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000496 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000497 /* We will only get Z_BUF_ERROR if the output buffer was full but
498 there wasn't more output when we tried again, so it is not an error
499 condition.
500 */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000501
Jeremy Hylton9714f992001-10-16 21:19:45 +0000502 if (!return_error) {
503 if (err != Z_OK && err != Z_BUF_ERROR) {
504 if (self->zst.msg == Z_NULL)
505 PyErr_Format(ZlibError, "Error %i while compressing",
506 err);
507 else
508 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
509 err, self->zst.msg);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000510
Jeremy Hylton9714f992001-10-16 21:19:45 +0000511 return_error = 1;
512 Py_DECREF(RetVal);
513 }
514 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000515
Jeremy Hylton9714f992001-10-16 21:19:45 +0000516 if (return_error)
517 RetVal = NULL; /* should have been handled by DECREF */
518 else
519 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000520
Jeremy Hylton9714f992001-10-16 21:19:45 +0000521 Py_DECREF(inputString);
522
523 LEAVE_ZLIB
524
525 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000526}
527
Guido van Rossum3c540301997-06-03 22:21:03 +0000528static char decomp_decompress__doc__[] =
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000529"decompress(data, max_length) -- Return a string containing\n"
530"the decompressed version of the data.\n\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000531"After calling this function, some of the input data may still\n"
532"be stored in internal buffers for later processing.\n"
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000533"Call the flush() method to clear these buffers.\n"
534"If the max_length parameter is specified then the return value will be\n"
535"no longer than max_length. Unconsumed input data will be stored in\n"
536"the unconsumed_tail attribute."
Guido van Rossum3c540301997-06-03 22:21:03 +0000537;
538
Guido van Rossumfb221561997-04-29 15:38:09 +0000539static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000540PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000541{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000542 int err, inplen, old_length, length = DEFAULTALLOC;
543 int max_length = 0;
544 PyObject *RetVal;
545 Byte *input;
546 unsigned long start_total_out;
547 int return_error;
548 PyObject * inputString;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000549
Jeremy Hylton9714f992001-10-16 21:19:45 +0000550 if (!PyArg_ParseTuple(args, "S|i:decompress", &inputString, &max_length))
551 return NULL;
552 if (max_length < 0) {
553 PyErr_SetString(PyExc_ValueError,
554 "max_length must be greater than zero");
555 return NULL;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000556 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000557
558 if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -1)
559 return NULL;
560
561 /* limit amount of data allocated to max_length */
562 if (max_length && length > max_length)
563 length = max_length;
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000564 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000565 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000566
567 ENTER_ZLIB
568 return_error = 0;
569
570 Py_INCREF(inputString);
571
572 start_total_out = self->zst.total_out;
573 self->zst.avail_in = inplen;
574 self->zst.next_in = input;
575 self->zst.avail_out = length;
576 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000577
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000578 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000579 err = inflate(&(self->zst), Z_SYNC_FLUSH);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000580 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000581
Jeremy Hylton9714f992001-10-16 21:19:45 +0000582 /* While Z_OK and the output buffer is full, there might be more output.
583 So extend the output buffer and try again.
584 */
585 while (err == Z_OK && self->zst.avail_out == 0) {
586 /* If max_length set, don't continue decompressing if we've already
587 reached the limit.
588 */
589 if (max_length && length >= max_length)
590 break;
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000591
Jeremy Hylton9714f992001-10-16 21:19:45 +0000592 /* otherwise, ... */
593 old_length = length;
594 length = length << 1;
595 if (max_length && length > max_length)
596 length = max_length;
597
598 if (_PyString_Resize(&RetVal, length) == -1) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000599 return_error = 1;
600 break;
601 }
602 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) \
603 + old_length;
604 self->zst.avail_out = length - old_length;
605
606 Py_BEGIN_ALLOW_THREADS
607 err = inflate(&(self->zst), Z_SYNC_FLUSH);
608 Py_END_ALLOW_THREADS
Guido van Rossumfb221561997-04-29 15:38:09 +0000609 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000610
611 /* Not all of the compressed data could be accomodated in the output buffer
612 of specified size. Return the unconsumed tail in an attribute.*/
613 if(max_length) {
614 Py_DECREF(self->unconsumed_tail);
615 self->unconsumed_tail = PyString_FromStringAndSize(self->zst.next_in,
616 self->zst.avail_in);
617 if(!self->unconsumed_tail)
618 return_error = 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000619 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000620
Jeremy Hylton9714f992001-10-16 21:19:45 +0000621 /* The end of the compressed data has been reached, so set the
622 unused_data attribute to a string containing the remainder of the
623 data in the string. Note that this is also a logical place to call
624 inflateEnd, but the old behaviour of only calling it on flush() is
625 preserved.
626 */
627 if (!return_error) {
628 if (err == Z_STREAM_END) {
629 Py_XDECREF(self->unused_data); /* Free original empty string */
630 self->unused_data = PyString_FromStringAndSize(
631 (char *)self->zst.next_in, self->zst.avail_in);
632 if (self->unused_data == NULL) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000633 Py_DECREF(RetVal);
634 return_error = 1;
635 }
636 /* We will only get Z_BUF_ERROR if the output buffer was full
637 but there wasn't more output when we tried again, so it is
638 not an error condition.
639 */
640 } else if (err != Z_OK && err != Z_BUF_ERROR) {
641 if (self->zst.msg == Z_NULL)
642 PyErr_Format(ZlibError, "Error %i while decompressing",
643 err);
644 else
645 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
646 err, self->zst.msg);
647 Py_DECREF(RetVal);
648 return_error = 1;
649 }
650 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000651
Jeremy Hylton9714f992001-10-16 21:19:45 +0000652 if (!return_error) {
653 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
654 }
655 else
656 RetVal = NULL; /* should be handled by DECREF */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000657
Jeremy Hylton9714f992001-10-16 21:19:45 +0000658 Py_DECREF(inputString);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000659
Jeremy Hylton9714f992001-10-16 21:19:45 +0000660 LEAVE_ZLIB
661
662 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000663}
664
Guido van Rossum3c540301997-06-03 22:21:03 +0000665static char comp_flush__doc__[] =
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000666"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
667"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
668"default value used when mode is not specified is Z_FINISH.\n"
669"If mode == Z_FINISH, the compressor object can no longer be used after\n"
670"calling the flush() method. Otherwise, more data can still be compressed.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000671;
672
Guido van Rossumfb221561997-04-29 15:38:09 +0000673static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000674PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000675{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000676 int err, length = DEFAULTALLOC;
677 PyObject *RetVal;
678 int flushmode = Z_FINISH;
679 unsigned long start_total_out;
680 int return_error;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000681
Jeremy Hylton9714f992001-10-16 21:19:45 +0000682 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
683 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000684
Jeremy Hylton9714f992001-10-16 21:19:45 +0000685 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
686 doing any work at all; just return an empty string. */
687 if (flushmode == Z_NO_FLUSH) {
688 return PyString_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000689 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000690
Jeremy Hylton9bc9d662001-10-16 21:23:58 +0000691 if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
Jeremy Hylton9714f992001-10-16 21:19:45 +0000692 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000693
694 ENTER_ZLIB
695
696 start_total_out = self->zst.total_out;
697 self->zst.avail_in = 0;
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000698 self->zst.avail_out = length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000699 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000700
701 Py_BEGIN_ALLOW_THREADS
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000702 err = deflate(&(self->zst), flushmode);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000703 Py_END_ALLOW_THREADS
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000704
Jeremy Hylton9714f992001-10-16 21:19:45 +0000705 return_error = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000706
Jeremy Hylton9714f992001-10-16 21:19:45 +0000707 /* while Z_OK and the output buffer is full, there might be more output,
708 so extend the output buffer and try again */
709 while (err == Z_OK && self->zst.avail_out == 0) {
710 if (_PyString_Resize(&RetVal, length << 1) == -1) {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000711 return_error = 1;
712 break;
713 }
714 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) \
715 + length;
716 self->zst.avail_out = length;
717 length = length << 1;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000718
Jeremy Hylton9714f992001-10-16 21:19:45 +0000719 Py_BEGIN_ALLOW_THREADS
720 err = deflate(&(self->zst), flushmode);
721 Py_END_ALLOW_THREADS
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000722 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000723
Jeremy Hylton9714f992001-10-16 21:19:45 +0000724 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
725 various data structures. Note we should only get Z_STREAM_END when
726 flushmode is Z_FINISH, but checking both for safety*/
727 if (!return_error) {
728 if (err == Z_STREAM_END && flushmode == Z_FINISH) {
729 err = deflateEnd(&(self->zst));
730 if (err != Z_OK) {
731 if (self->zst.msg == Z_NULL)
732 PyErr_Format(ZlibError, "Error %i from deflateEnd()",
733 err);
734 else
735 PyErr_Format(ZlibError,
736 "Error %i from deflateEnd(): %.200s",
737 err, self->zst.msg);
738
739 Py_DECREF(RetVal);
740 return_error = 1;
741 } else
742 self->is_initialised = 0;
743
744 /* We will only get Z_BUF_ERROR if the output buffer was full
745 but there wasn't more output when we tried again, so it is
746 not an error condition.
747 */
748 } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
749 if (self->zst.msg == Z_NULL)
750 PyErr_Format(ZlibError, "Error %i while flushing",
751 err);
752 else
753 PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
754 err, self->zst.msg);
755 Py_DECREF(RetVal);
756 return_error = 1;
757 }
758 }
759
760 if (!return_error)
761 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
762 else
763 RetVal = NULL; /* should have been handled by DECREF */
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000764
Jeremy Hylton9714f992001-10-16 21:19:45 +0000765 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000766
Jeremy Hylton9714f992001-10-16 21:19:45 +0000767 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000768}
769
Guido van Rossum3c540301997-06-03 22:21:03 +0000770static char decomp_flush__doc__[] =
771"flush() -- Return a string containing any remaining decompressed data. "
772"The decompressor object can no longer be used after this call."
773;
774
Guido van Rossumfb221561997-04-29 15:38:09 +0000775static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000776PyZlib_unflush(compobject *self, PyObject *args)
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000777/*decompressor flush is a no-op because all pending data would have been
778 flushed by the decompress method. However, this routine previously called
779 inflateEnd, causing any further decompress or flush calls to raise
780 exceptions. This behaviour has been preserved.*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000781{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000782 int err;
783 PyObject * retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000784
Jeremy Hylton9714f992001-10-16 21:19:45 +0000785 if (!PyArg_ParseTuple(args, ""))
786 return NULL;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000787
Jeremy Hylton9714f992001-10-16 21:19:45 +0000788 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000789
Jeremy Hylton9714f992001-10-16 21:19:45 +0000790 err = inflateEnd(&(self->zst));
791 if (err != Z_OK) {
Jeremy Hylton0965e082001-10-16 21:56:09 +0000792 zlib_error(self->zst, err, "from inflateEnd()");
Jeremy Hylton9714f992001-10-16 21:19:45 +0000793 retval = NULL;
794 } else {
795 self->is_initialised = 0;
796 retval = PyString_FromStringAndSize(NULL, 0);
797 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000798
Jeremy Hylton9714f992001-10-16 21:19:45 +0000799 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000800
Jeremy Hylton9714f992001-10-16 21:19:45 +0000801 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000802}
803
804static PyMethodDef comp_methods[] =
805{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000806 {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
807 comp_compress__doc__},
808 {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
809 comp_flush__doc__},
810 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000811};
812
813static PyMethodDef Decomp_methods[] =
814{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000815 {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
816 decomp_decompress__doc__},
817 {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
818 decomp_flush__doc__},
819 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000820};
821
822static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000823Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000824{
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000825 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
826 internal data. */
827
828 return Py_FindMethod(comp_methods, (PyObject *)self, name);
Guido van Rossumfb221561997-04-29 15:38:09 +0000829}
830
831static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000832Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000833{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000834 PyObject * retval;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000835
Jeremy Hylton9714f992001-10-16 21:19:45 +0000836 ENTER_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000837
Jeremy Hylton9714f992001-10-16 21:19:45 +0000838 if (strcmp(name, "unused_data") == 0) {
839 Py_INCREF(self->unused_data);
840 retval = self->unused_data;
841 } else if (strcmp(name, "unconsumed_tail") == 0) {
842 Py_INCREF(self->unconsumed_tail);
843 retval = self->unconsumed_tail;
844 } else
845 retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000846
Jeremy Hylton9714f992001-10-16 21:19:45 +0000847 LEAVE_ZLIB
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000848
Jeremy Hylton9714f992001-10-16 21:19:45 +0000849 return retval;
Guido van Rossumfb221561997-04-29 15:38:09 +0000850}
851
Guido van Rossum3c540301997-06-03 22:21:03 +0000852static char adler32__doc__[] =
853"adler32(string) -- Compute an Adler-32 checksum of string, using "
854"a default starting value, and returning an integer value.\n"
855"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
856"the starting value provided, and returning an integer value\n"
857;
858
Guido van Rossumfb221561997-04-29 15:38:09 +0000859static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000860PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000861{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000862 uLong adler32val = adler32(0L, Z_NULL, 0);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000863 Byte *buf;
864 int len;
Guido van Rossumfb221561997-04-29 15:38:09 +0000865
Guido van Rossum43713e52000-02-29 13:59:29 +0000866 if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000867 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000868 adler32val = adler32(adler32val, buf, len);
869 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000870}
871
Guido van Rossum3c540301997-06-03 22:21:03 +0000872static char crc32__doc__[] =
873"crc32(string) -- Compute a CRC-32 checksum of string, using "
874"a default starting value, and returning an integer value.\n"
875"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
876"the starting value provided, and returning an integer value.\n"
877;
Guido van Rossumfb221561997-04-29 15:38:09 +0000878
879static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000880PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000881{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000882 uLong crc32val = crc32(0L, Z_NULL, 0);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000883 Byte *buf;
884 int len;
Guido van Rossum43713e52000-02-29 13:59:29 +0000885 if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000886 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000887 crc32val = crc32(crc32val, buf, len);
888 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000889}
890
891
892static PyMethodDef zlib_methods[] =
893{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894 {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
895 adler32__doc__},
896 {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
897 compress__doc__},
898 {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
899 compressobj__doc__},
900 {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
901 crc32__doc__},
902 {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
903 decompress__doc__},
904 {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
905 decompressobj__doc__},
906 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000907};
908
909statichere PyTypeObject Comptype = {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000910 PyObject_HEAD_INIT(0)
911 0,
912 "Compress",
913 sizeof(compobject),
914 0,
915 (destructor)Comp_dealloc, /*tp_dealloc*/
916 0, /*tp_print*/
917 (getattrfunc)Comp_getattr, /*tp_getattr*/
918 0, /*tp_setattr*/
919 0, /*tp_compare*/
920 0, /*tp_repr*/
921 0, /*tp_as_number*/
922 0, /*tp_as_sequence*/
923 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000924};
925
926statichere PyTypeObject Decomptype = {
Jeremy Hylton9714f992001-10-16 21:19:45 +0000927 PyObject_HEAD_INIT(0)
928 0,
929 "Decompress",
930 sizeof(compobject),
931 0,
932 (destructor)Decomp_dealloc, /*tp_dealloc*/
933 0, /*tp_print*/
934 (getattrfunc)Decomp_getattr, /*tp_getattr*/
935 0, /*tp_setattr*/
936 0, /*tp_compare*/
937 0, /*tp_repr*/
938 0, /*tp_as_number*/
939 0, /*tp_as_sequence*/
940 0, /*tp_as_mapping*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000941};
942
943/* The following insint() routine was blatantly ripped off from
944 socketmodule.c */
945
946/* Convenience routine to export an integer value.
947 For simplicity, errors (which are unlikely anyway) are ignored. */
948static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000949insint(PyObject *d, char *name, int value)
Guido van Rossumfb221561997-04-29 15:38:09 +0000950{
951 PyObject *v = PyInt_FromLong((long) value);
952 if (v == NULL) {
953 /* Don't bother reporting this error */
954 PyErr_Clear();
955 }
956 else {
957 PyDict_SetItemString(d, name, v);
958 Py_DECREF(v);
959 }
960}
961
Guido van Rossum3c540301997-06-03 22:21:03 +0000962static char zlib_module_documentation[]=
963"The functions in this module allow compression and decompression "
964"using the zlib library, which is based on GNU zip. \n\n"
965"adler32(string) -- Compute an Adler-32 checksum.\n"
966"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
967"compress(string) -- Compress a string.\n"
968"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
969"compressobj([level]) -- Return a compressor object.\n"
970"crc32(string) -- Compute a CRC-32 checksum.\n"
971"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +0000972"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000973"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
974"Compressor objects support compress() and flush() methods; decompressor \n"
975"objects support decompress() and flush()."
976;
977
Guido van Rossum3886bb61998-12-04 18:50:17 +0000978DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000979PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +0000980{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000981 PyObject *m, *d, *ver;
982 Comptype.ob_type = &PyType_Type;
983 Decomptype.ob_type = &PyType_Type;
984 m = Py_InitModule4("zlib", zlib_methods,
985 zlib_module_documentation,
986 (PyObject*)NULL,PYTHON_API_VERSION);
987 d = PyModule_GetDict(m);
988 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
989 if (ZlibError != NULL)
990 PyDict_SetItemString(d, "error", ZlibError);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000991
Jeremy Hylton9714f992001-10-16 21:19:45 +0000992 PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
993 PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
994 PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
995 PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
996 PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
997 PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
998 PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
999 PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1000 PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
1001
1002 PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1003 PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1004 PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1005 PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
1006
1007 ver = PyString_FromString(ZLIB_VERSION);
1008 if (ver != NULL) {
1009 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
1010 Py_DECREF(ver);
1011 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001012
1013#ifdef WITH_THREAD
Jeremy Hylton9714f992001-10-16 21:19:45 +00001014 zlib_lock = PyThread_allocate_lock();
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001015#endif // WITH_THREAD
Guido van Rossumfb221561997-04-29 15:38:09 +00001016}