blob: 9416528ad4cdfa9fc7c3291865a7c5e60ebac4c1 [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Guido van Rossum821a5e41998-05-08 14:56:29 +00002/* See http://www.cdrom.com/pub/infozip/zlib/ */
3/* See http://www.winimage.com/zLibDll for Windows */
Guido van Rossumfb221561997-04-29 15:38:09 +00004
Guido van Rossum97b54571997-06-03 22:21:47 +00005#include "Python.h"
6#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +00007
8/* The following parameters are copied from zutil.h, version 0.95 */
9#define DEFLATED 8
10#if MAX_MEM_LEVEL >= 8
11# define DEF_MEM_LEVEL 8
12#else
13# define DEF_MEM_LEVEL MAX_MEM_LEVEL
14#endif
15#define DEF_WBITS MAX_WBITS
16
Guido van Rossumb729a1d1999-04-07 20:23:17 +000017/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
18#define DEFAULTALLOC (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000019#define PyInit_zlib initzlib
20
21staticforward PyTypeObject Comptype;
22staticforward PyTypeObject Decomptype;
23
24static PyObject *ZlibError;
25
26typedef struct
27{
28 PyObject_HEAD
29 z_stream zst;
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +000030 PyObject *unused_data;
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +000031 int is_initialised;
Guido van Rossumfb221561997-04-29 15:38:09 +000032} compobject;
33
Guido van Rossum3c540301997-06-03 22:21:03 +000034static char compressobj__doc__[] =
35"compressobj() -- Return a compressor object.\n"
36"compressobj(level) -- Return a compressor object, using the given compression level.\n"
37;
38
39static char decompressobj__doc__[] =
40"decompressobj() -- Return a decompressor object.\n"
41"decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
42;
43
Guido van Rossumfb221561997-04-29 15:38:09 +000044static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000045newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +000046{
47 compobject *self;
Guido van Rossumb18618d2000-05-03 23:44:39 +000048 self = PyObject_New(compobject, type);
Guido van Rossumfb221561997-04-29 15:38:09 +000049 if (self == NULL)
50 return NULL;
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +000051 self->is_initialised = 0;
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +000052 self->unused_data = PyString_FromString("");
Guido van Rossumfb221561997-04-29 15:38:09 +000053 return self;
54}
55
Guido van Rossum3c540301997-06-03 22:21:03 +000056static char compress__doc__[] =
57"compress(string) -- Compress string using the default compression level, "
58"returning a string containing compressed data.\n"
59"compress(string, level) -- Compress string, using the chosen compression "
60"level (from 1 to 9). Return a string containing the compressed data.\n"
61;
62
Guido van Rossumfb221561997-04-29 15:38:09 +000063static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000064PyZlib_compress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +000065{
66 PyObject *ReturnVal;
67 Byte *input, *output;
68 int length, level=Z_DEFAULT_COMPRESSION, err;
69 z_stream zst;
70
Guido van Rossum43713e52000-02-29 13:59:29 +000071 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Guido van Rossumfb221561997-04-29 15:38:09 +000072 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +000073 zst.avail_out = length + length/1000 + 12 + 1;
Guido van Rossumfb221561997-04-29 15:38:09 +000074 output=(Byte*)malloc(zst.avail_out);
75 if (output==NULL)
76 {
77 PyErr_SetString(PyExc_MemoryError,
78 "Can't allocate memory to compress data");
79 return NULL;
80 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +000081
Jeremy Hylton5bc4abe1998-12-21 17:15:00 +000082 zst.zalloc=(alloc_func)NULL;
Guido van Rossum97b54571997-06-03 22:21:47 +000083 zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +000084 zst.next_out=(Byte *)output;
85 zst.next_in =(Byte *)input;
86 zst.avail_in=length;
87 err=deflateInit(&zst, level);
88 switch(err)
89 {
90 case(Z_OK):
91 break;
92 case(Z_MEM_ERROR):
93 PyErr_SetString(PyExc_MemoryError,
94 "Out of memory while compressing data");
95 free(output);
96 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +000097 case(Z_STREAM_ERROR):
98 PyErr_SetString(ZlibError,
99 "Bad compression level");
100 free(output);
101 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000102 default:
103 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000104 if (zst.msg == Z_NULL)
105 PyErr_Format(ZlibError, "Error %i while compressing data",
106 err);
107 else
108 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
109 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000110 deflateEnd(&zst);
111 free(output);
112 return NULL;
113 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000114 }
115
116 err=deflate(&zst, Z_FINISH);
117 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000118 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000119 case(Z_STREAM_END):
120 break;
121 /* Are there other errors to be trapped here? */
122 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000123 {
124 if (zst.msg == Z_NULL)
125 PyErr_Format(ZlibError, "Error %i while compressing data",
126 err);
127 else
128 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
129 err, zst.msg);
130 deflateEnd(&zst);
131 free(output);
132 return NULL;
133 }
134 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000135 err=deflateEnd(&zst);
136 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000137 {
138 if (zst.msg == Z_NULL)
139 PyErr_Format(ZlibError, "Error %i while finishing compression",
140 err);
141 else
142 PyErr_Format(ZlibError,
143 "Error %i while finishing compression: %.200s",
144 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000145 free(output);
146 return NULL;
147 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000148 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000149 free(output);
150 return ReturnVal;
151}
152
Guido van Rossum3c540301997-06-03 22:21:03 +0000153static char decompress__doc__[] =
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000154"decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
155"decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
156"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 +0000157;
158
Guido van Rossumfb221561997-04-29 15:38:09 +0000159static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000160PyZlib_decompress(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000161{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000162 PyObject *result_str;
163 Byte *input;
Guido van Rossumfb221561997-04-29 15:38:09 +0000164 int length, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000165 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000166 z_stream zst;
Guido van Rossum43713e52000-02-29 13:59:29 +0000167 if (!PyArg_ParseTuple(args, "s#|ii:decompress", &input, &length, &wsize, &r_strlen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000168 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000169
170 if (r_strlen <= 0)
171 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000172
Guido van Rossumfb221561997-04-29 15:38:09 +0000173 zst.avail_in=length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000174 zst.avail_out=r_strlen;
175 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
176 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000177 PyErr_SetString(PyExc_MemoryError,
178 "Can't allocate memory to decompress data");
179 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000180 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000181 zst.zalloc=(alloc_func)NULL;
182 zst.zfree=(free_func)Z_NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000183 zst.next_out=(Byte *)PyString_AsString(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000184 zst.next_in =(Byte *)input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000185 err=inflateInit2(&zst, wsize);
Guido van Rossumfb221561997-04-29 15:38:09 +0000186 switch(err)
187 {
188 case(Z_OK):
189 break;
190 case(Z_MEM_ERROR):
191 PyErr_SetString(PyExc_MemoryError,
192 "Out of memory while decompressing data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000193 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000194 return NULL;
195 default:
196 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000197 if (zst.msg == Z_NULL)
198 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
199 err);
200 else
201 PyErr_Format(ZlibError,
202 "Error %i while preparing to decompress data: %.200s",
203 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000204 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000205 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000206 return NULL;
207 }
208 }
209 do
210 {
211 err=inflate(&zst, Z_FINISH);
212 switch(err)
213 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000214 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000215 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000216 case(Z_BUF_ERROR):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000217 case(Z_OK):
218 /* need more memory */
219 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
Guido van Rossumfb221561997-04-29 15:38:09 +0000220 {
221 PyErr_SetString(PyExc_MemoryError,
222 "Out of memory while decompressing data");
223 inflateEnd(&zst);
224 return NULL;
225 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000226 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000227 zst.avail_out=r_strlen;
228 r_strlen = r_strlen << 1;
229 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000230 default:
231 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000232 if (zst.msg == Z_NULL)
233 PyErr_Format(ZlibError, "Error %i while decompressing data",
234 err);
235 else
236 PyErr_Format(ZlibError,
237 "Error %i while decompressing data: %.200s",
238 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000239 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000240 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000241 return NULL;
242 }
243 }
244 } while(err!=Z_STREAM_END);
245
246 err=inflateEnd(&zst);
247 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000248 {
249 if (zst.msg == Z_NULL)
250 PyErr_Format(ZlibError,
251 "Error %i while finishing data decompression",
252 err);
253 else
254 PyErr_Format(ZlibError,
255 "Error %i while finishing data decompression: %.200s",
256 err, zst.msg);
257 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000258 return NULL;
259 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000260 _PyString_Resize(&result_str, zst.total_out);
261 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000262}
263
264static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000265PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000266{
267 compobject *self;
268 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
269 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000270
Guido van Rossum43713e52000-02-29 13:59:29 +0000271 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000272 &memLevel, &strategy))
273 return NULL;
274
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000275 self = newcompobject(&Comptype);
Guido van Rossumfb221561997-04-29 15:38:09 +0000276 if (self==NULL) return(NULL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000277 self->zst.zalloc = (alloc_func)NULL;
278 self->zst.zfree = (free_func)Z_NULL;
279 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Guido van Rossumfb221561997-04-29 15:38:09 +0000280 switch(err)
281 {
282 case (Z_OK):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000283 self->is_initialised = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000284 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000285 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000286 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000287 PyErr_SetString(PyExc_MemoryError,
288 "Can't allocate memory for compression object");
289 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000290 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000291 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000292 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000293 "Invalid initialization option");
Guido van Rossumfb221561997-04-29 15:38:09 +0000294 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000295 default:
296 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000297 if (self->zst.msg == Z_NULL)
298 PyErr_Format(ZlibError,
299 "Error %i while creating compression object",
300 err);
301 else
302 PyErr_Format(ZlibError,
303 "Error %i while creating compression object: %.200s",
304 err, self->zst.msg);
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000305 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000306 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000307 }
308 }
309}
310
311static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000312PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000313{
314 int wbits=DEF_WBITS, err;
315 compobject *self;
Guido van Rossum43713e52000-02-29 13:59:29 +0000316 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Guido van Rossumfb221561997-04-29 15:38:09 +0000317 {
318 return NULL;
319 }
320 self=newcompobject(&Decomptype);
321 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000322 self->zst.zalloc=(alloc_func)NULL;
323 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000324 err=inflateInit2(&self->zst, wbits);
325 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000326 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000327 case (Z_OK):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000328 self->is_initialised = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000329 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000330 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000331 Py_DECREF(self);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000332 PyErr_SetString(PyExc_ValueError,
333 "Invalid initialization option");
334 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000335 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000336 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000337 PyErr_SetString(PyExc_MemoryError,
338 "Can't allocate memory for decompression object");
339 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000340 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000341 {
342 if (self->zst.msg == Z_NULL)
343 PyErr_Format(ZlibError,
344 "Error %i while creating decompression object",
345 err);
346 else
347 PyErr_Format(ZlibError,
348 "Error %i while creating decompression object: %.200s",
349 err, self->zst.msg);
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000350 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000351 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000352 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000353 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000354}
355
356static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000357Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000358{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000359 if (self->is_initialised)
360 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000361 Py_XDECREF(self->unused_data);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000362 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000363}
364
365static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000366Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000367{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000368 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000369 Py_XDECREF(self->unused_data);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000370 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000371}
372
Guido van Rossum3c540301997-06-03 22:21:03 +0000373static char comp_compress__doc__[] =
374"compress(data) -- Return a string containing a compressed version of the data.\n\n"
375"After calling this function, some of the input data may still\n"
376"be stored in internal buffers for later processing.\n"
377"Call the flush() method to clear these buffers."
378;
379
380
Guido van Rossumfb221561997-04-29 15:38:09 +0000381static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000382PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000383{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000384 int err = Z_OK, inplen;
385 int length = DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000386 PyObject *RetVal;
387 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000388 unsigned long start_total_out;
Guido van Rossumfb221561997-04-29 15:38:09 +0000389
Guido van Rossum43713e52000-02-29 13:59:29 +0000390 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000391 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000392 self->zst.avail_in = inplen;
393 self->zst.next_in = input;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000394 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
395 PyErr_SetString(PyExc_MemoryError,
396 "Can't allocate memory to compress data");
397 return NULL;
398 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000399 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000400 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000401 self->zst.avail_out = length;
402 while (self->zst.avail_in != 0 && err == Z_OK)
403 {
404 err = deflate(&(self->zst), Z_NO_FLUSH);
405 if (self->zst.avail_out <= 0) {
406 if (_PyString_Resize(&RetVal, length << 1) == -1) {
407 PyErr_SetString(PyExc_MemoryError,
408 "Can't allocate memory to compress data");
409 return NULL;
410 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000411 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000412 self->zst.avail_out = length;
413 length = length << 1;
414 }
415 }
416 if (err != Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000417 {
418 if (self->zst.msg == Z_NULL)
419 PyErr_Format(ZlibError, "Error %i while compressing",
420 err);
421 else
422 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
423 err, self->zst.msg);
424 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000425 return NULL;
426 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000427 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000428 return RetVal;
429}
430
Guido van Rossum3c540301997-06-03 22:21:03 +0000431static char decomp_decompress__doc__[] =
432"decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
433"After calling this function, some of the input data may still\n"
434"be stored in internal buffers for later processing.\n"
435"Call the flush() method to clear these buffers."
436;
437
Guido van Rossumfb221561997-04-29 15:38:09 +0000438static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000439PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000440{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000441 int length, err, inplen;
Guido van Rossumfb221561997-04-29 15:38:09 +0000442 PyObject *RetVal;
443 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000444 unsigned long start_total_out;
445
Guido van Rossum43713e52000-02-29 13:59:29 +0000446 if (!PyArg_ParseTuple(args, "s#:decompress", &input, &inplen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000447 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000448 start_total_out = self->zst.total_out;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000449 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000450 self->zst.avail_in = inplen;
451 self->zst.next_in = input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000452 self->zst.avail_out = length = DEFAULTALLOC;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000453 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000454 err = Z_OK;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000455
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000456 while (self->zst.avail_in != 0 && err == Z_OK)
457 {
458 err = inflate(&(self->zst), Z_NO_FLUSH);
459 if (err == Z_OK && self->zst.avail_out <= 0)
460 {
461 if (_PyString_Resize(&RetVal, length << 1) == -1)
462 {
463 PyErr_SetString(PyExc_MemoryError,
464 "Can't allocate memory to compress data");
465 return NULL;
466 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000467 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000468 self->zst.avail_out = length;
469 length = length << 1;
470 }
471 }
472
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000473 if (err != Z_OK && err != Z_STREAM_END)
474 {
475 if (self->zst.msg == Z_NULL)
476 PyErr_Format(ZlibError, "Error %i while decompressing",
477 err);
478 else
479 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
480 err, self->zst.msg);
481 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000482 return NULL;
483 }
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000484
485 if (err == Z_STREAM_END)
486 {
487 /* The end of the compressed data has been reached, so set
488 the unused_data attribute to a string containing the
489 remainder of the data in the string. */
490 int pos = self->zst.next_in - input; /* Position in the string */
491 Py_XDECREF(self->unused_data); /* Free the original, empty string */
492
Guido van Rossumaee9bb21999-04-12 14:35:48 +0000493 self->unused_data = PyString_FromStringAndSize((char *)input+pos,
494 inplen-pos);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000495 if (self->unused_data == NULL) return NULL;
496 }
497
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000498 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000499 return RetVal;
500}
501
Guido van Rossum3c540301997-06-03 22:21:03 +0000502static char comp_flush__doc__[] =
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000503"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
504"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
505"default value used when mode is not specified is Z_FINISH.\n"
506"If mode == Z_FINISH, the compressor object can no longer be used after\n"
507"calling the flush() method. Otherwise, more data can still be compressed.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000508;
509
Guido van Rossumfb221561997-04-29 15:38:09 +0000510static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000511PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000512{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000513 int length=DEFAULTALLOC, err = Z_OK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000514 PyObject *RetVal;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000515 int flushmode = Z_FINISH;
516 unsigned long start_total_out;
517
Guido van Rossum43713e52000-02-29 13:59:29 +0000518 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000519 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000520
521 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
522 doing any work at all; just return an empty string. */
523 if (flushmode == Z_NO_FLUSH)
524 {
525 return PyString_FromStringAndSize(NULL, 0);
526 }
527
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000528 self->zst.avail_in = 0;
529 self->zst.next_in = Z_NULL;
530 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
531 PyErr_SetString(PyExc_MemoryError,
532 "Can't allocate memory to compress data");
533 return NULL;
534 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000535 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000536 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000537 self->zst.avail_out = length;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000538
539 /* When flushing the zstream, there's no input data.
540 If zst.avail_out == 0, that means that more output space is
541 needed to complete the flush operation. */
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000542 while (1) {
543 err = deflate(&(self->zst), flushmode);
544
545 /* If the output is Z_OK, and there's still room in the output
546 buffer, then the flush is complete. */
547 if ( (err == Z_OK) && self->zst.avail_out > 0) break;
548
549 /* A nonzero return indicates some sort of error (but see
550 the comment for the error handler below) */
551 if ( err != Z_OK ) break;
552
553 /* There's no space left for output, so increase the buffer and loop
554 again */
555 if (_PyString_Resize(&RetVal, length << 1) == -1) {
556 PyErr_SetString(PyExc_MemoryError,
557 "Can't allocate memory to compress data");
558 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000559 }
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000560 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
561 self->zst.avail_out = length;
562 length = length << 1;
Jeremy Hylton6892aa31999-01-06 22:56:24 +0000563 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000564
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000565 /* Raise an exception indicating an error. The condition for
566 detecting a error is kind of complicated; Z_OK indicates no
567 error, but if the flushmode is Z_FINISH, then Z_STREAM_END is
568 also not an error. */
569 if (err!=Z_OK && !(flushmode == Z_FINISH && err == Z_STREAM_END) )
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000570 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000571 if (self->zst.msg == Z_NULL)
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000572 PyErr_Format(ZlibError, "Error %i while flushing",
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000573 err);
574 else
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000575 PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000576 err, self->zst.msg);
577 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000578 return NULL;
Jeremy Hylton6892aa31999-01-06 22:56:24 +0000579 }
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000580
581 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to
582 free various data structures */
583
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000584 if (flushmode == Z_FINISH) {
585 err=deflateEnd(&(self->zst));
586 if (err!=Z_OK) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000587 if (self->zst.msg == Z_NULL)
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000588 PyErr_Format(ZlibError, "Error %i from deflateEnd()",
589 err);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000590 else
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000591 PyErr_Format(ZlibError,
592 "Error %i from deflateEnd(): %.200s",
593 err, self->zst.msg);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000594 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000595 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000596 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000597 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000598 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000599 return RetVal;
600}
601
Guido van Rossum3c540301997-06-03 22:21:03 +0000602static char decomp_flush__doc__[] =
603"flush() -- Return a string containing any remaining decompressed data. "
604"The decompressor object can no longer be used after this call."
605;
606
Guido van Rossumfb221561997-04-29 15:38:09 +0000607static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000608PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000609{
610 int length=0, err;
Guido van Rossumfb221561997-04-29 15:38:09 +0000611 PyObject *RetVal;
612
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000613 if (!PyArg_ParseTuple(args, ""))
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000614 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000615 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
616 {
617 PyErr_SetString(PyExc_MemoryError,
618 "Can't allocate memory to decompress data");
619 return NULL;
620 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000621 self->zst.avail_in=0;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000622 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000623 length = self->zst.avail_out = DEFAULTALLOC;
624
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000625 /* I suspect that Z_BUF_ERROR is the only error code we need to check for
626 in the following loop, but will leave the Z_OK in for now to avoid
627 destabilizing this function. --amk */
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000628 err = Z_OK;
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000629 while ( err == Z_OK )
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000630 {
631 err = inflate(&(self->zst), Z_FINISH);
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000632 if ( ( err == Z_OK || err == Z_BUF_ERROR ) && self->zst.avail_out == 0)
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000633 {
634 if (_PyString_Resize(&RetVal, length << 1) == -1)
635 {
636 PyErr_SetString(PyExc_MemoryError,
637 "Can't allocate memory to decompress data");
638 return NULL;
639 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000640 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000641 self->zst.avail_out = length;
642 length = length << 1;
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000643 err = Z_OK;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000644 }
645 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000646 if (err!=Z_STREAM_END)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000647 {
648 if (self->zst.msg == Z_NULL)
649 PyErr_Format(ZlibError, "Error %i while decompressing",
650 err);
651 else
652 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
653 err, self->zst.msg);
654 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000655 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000656 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000657 err=inflateEnd(&(self->zst));
658 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000659 {
660 if (self->zst.msg == Z_NULL)
661 PyErr_Format(ZlibError,
662 "Error %i while flushing decompression object",
663 err);
664 else
665 PyErr_Format(ZlibError,
666 "Error %i while flushing decompression object: %.200s",
667 err, self->zst.msg);
668 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000669 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000670 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000671 _PyString_Resize(&RetVal,
672 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000673 return RetVal;
674}
675
676static PyMethodDef comp_methods[] =
677{
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000678 {"compress", (binaryfunc)PyZlib_objcompress,
679 METH_VARARGS, comp_compress__doc__},
680 {"flush", (binaryfunc)PyZlib_flush,
681 METH_VARARGS, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000682 {NULL, NULL}
683};
684
685static PyMethodDef Decomp_methods[] =
686{
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000687 {"decompress", (binaryfunc)PyZlib_objdecompress,
688 METH_VARARGS, decomp_decompress__doc__},
689 {"flush", (binaryfunc)PyZlib_unflush,
690 METH_VARARGS, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000691 {NULL, NULL}
692};
693
694static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000695Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000696{
697 return Py_FindMethod(comp_methods, (PyObject *)self, name);
698}
699
700static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000701Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000702{
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000703 if (strcmp(name, "unused_data") == 0)
704 {
705 Py_INCREF(self->unused_data);
706 return self->unused_data;
707 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000708 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
709}
710
Guido van Rossum3c540301997-06-03 22:21:03 +0000711static char adler32__doc__[] =
712"adler32(string) -- Compute an Adler-32 checksum of string, using "
713"a default starting value, and returning an integer value.\n"
714"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
715"the starting value provided, and returning an integer value\n"
716;
717
Guido van Rossumfb221561997-04-29 15:38:09 +0000718static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000719PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000720{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000721 uLong adler32val=adler32(0L, Z_NULL, 0);
722 Byte *buf;
723 int len;
Guido van Rossumfb221561997-04-29 15:38:09 +0000724
Guido van Rossum43713e52000-02-29 13:59:29 +0000725 if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000726 {
727 return NULL;
728 }
729 adler32val = adler32(adler32val, buf, len);
730 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000731}
732
Guido van Rossum3c540301997-06-03 22:21:03 +0000733static char crc32__doc__[] =
734"crc32(string) -- Compute a CRC-32 checksum of string, using "
735"a default starting value, and returning an integer value.\n"
736"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
737"the starting value provided, and returning an integer value.\n"
738;
Guido van Rossumfb221561997-04-29 15:38:09 +0000739
740static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000741PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000742{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000743 uLong crc32val=crc32(0L, Z_NULL, 0);
744 Byte *buf;
745 int len;
Guido van Rossum43713e52000-02-29 13:59:29 +0000746 if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000747 {
748 return NULL;
749 }
750 crc32val = crc32(crc32val, buf, len);
751 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000752}
753
754
755static PyMethodDef zlib_methods[] =
756{
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000757 {"adler32", (PyCFunction)PyZlib_adler32,
758 METH_VARARGS, adler32__doc__},
759 {"compress", (PyCFunction)PyZlib_compress,
760 METH_VARARGS, compress__doc__},
761 {"compressobj", (PyCFunction)PyZlib_compressobj,
762 METH_VARARGS, compressobj__doc__},
763 {"crc32", (PyCFunction)PyZlib_crc32,
764 METH_VARARGS, crc32__doc__},
765 {"decompress", (PyCFunction)PyZlib_decompress,
766 METH_VARARGS, decompress__doc__},
767 {"decompressobj", (PyCFunction)PyZlib_decompressobj,
768 METH_VARARGS, decompressobj__doc__},
769 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000770};
771
772statichere PyTypeObject Comptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000773 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000774 0,
775 "Compress",
776 sizeof(compobject),
777 0,
778 (destructor)Comp_dealloc, /*tp_dealloc*/
779 0, /*tp_print*/
780 (getattrfunc)Comp_getattr, /*tp_getattr*/
781 0, /*tp_setattr*/
782 0, /*tp_compare*/
783 0, /*tp_repr*/
784 0, /*tp_as_number*/
785 0, /*tp_as_sequence*/
786 0, /*tp_as_mapping*/
787};
788
789statichere PyTypeObject Decomptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000790 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000791 0,
792 "Decompress",
793 sizeof(compobject),
794 0,
795 (destructor)Decomp_dealloc, /*tp_dealloc*/
796 0, /*tp_print*/
797 (getattrfunc)Decomp_getattr, /*tp_getattr*/
798 0, /*tp_setattr*/
799 0, /*tp_compare*/
800 0, /*tp_repr*/
801 0, /*tp_as_number*/
802 0, /*tp_as_sequence*/
803 0, /*tp_as_mapping*/
804};
805
806/* The following insint() routine was blatantly ripped off from
807 socketmodule.c */
808
809/* Convenience routine to export an integer value.
810 For simplicity, errors (which are unlikely anyway) are ignored. */
811static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000812insint(PyObject *d, char *name, int value)
Guido van Rossumfb221561997-04-29 15:38:09 +0000813{
814 PyObject *v = PyInt_FromLong((long) value);
815 if (v == NULL) {
816 /* Don't bother reporting this error */
817 PyErr_Clear();
818 }
819 else {
820 PyDict_SetItemString(d, name, v);
821 Py_DECREF(v);
822 }
823}
824
Guido van Rossum3c540301997-06-03 22:21:03 +0000825static char zlib_module_documentation[]=
826"The functions in this module allow compression and decompression "
827"using the zlib library, which is based on GNU zip. \n\n"
828"adler32(string) -- Compute an Adler-32 checksum.\n"
829"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
830"compress(string) -- Compress a string.\n"
831"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
832"compressobj([level]) -- Return a compressor object.\n"
833"crc32(string) -- Compute a CRC-32 checksum.\n"
834"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +0000835"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000836"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
837"Compressor objects support compress() and flush() methods; decompressor \n"
838"objects support decompress() and flush()."
839;
840
Guido van Rossum3886bb61998-12-04 18:50:17 +0000841DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000842PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +0000843{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000844 PyObject *m, *d, *ver;
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000845 Comptype.ob_type = &PyType_Type;
846 Decomptype.ob_type = &PyType_Type;
Guido van Rossum3c540301997-06-03 22:21:03 +0000847 m = Py_InitModule4("zlib", zlib_methods,
848 zlib_module_documentation,
849 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000850 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000851 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Fred Drake8972dfd1999-12-22 16:13:54 +0000852 if (ZlibError != NULL)
853 PyDict_SetItemString(d, "error", ZlibError);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000854
Guido van Rossumfb221561997-04-29 15:38:09 +0000855 insint(d, "MAX_WBITS", MAX_WBITS);
856 insint(d, "DEFLATED", DEFLATED);
857 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000858 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
859 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
860 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
861 insint(d, "Z_FILTERED", Z_FILTERED);
862 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
863 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000864
865 insint(d, "Z_FINISH", Z_FINISH);
866 insint(d, "Z_NO_FLUSH", Z_NO_FLUSH);
867 insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
868 insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH);
869
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000870 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake8972dfd1999-12-22 16:13:54 +0000871 if (ver != NULL) {
872 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
873 Py_DECREF(ver);
874 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000875}