blob: cb2a2e5c9716d015f69d7521ce30dfe7b08d2f42 [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):
Andrew M. Kuchlingd9238312000-10-09 14:18:10 +0000217 /*
218 * If there is at least 1 byte of room according to zst.avail_out
219 * and we get this error, assume that it means zlib cannot
220 * process the inflate call() due to an error in the data.
221 */
222 if (zst.avail_out > 0)
223 {
224 PyErr_Format(ZlibError, "Error %i while decompressing data",
225 err);
226 inflateEnd(&zst);
227 Py_DECREF(result_str);
228 return NULL;
229 }
230 /* fall through */
231 case(Z_OK):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000232 /* need more memory */
233 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
Guido van Rossumfb221561997-04-29 15:38:09 +0000234 {
235 PyErr_SetString(PyExc_MemoryError,
236 "Out of memory while decompressing data");
237 inflateEnd(&zst);
238 return NULL;
239 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000240 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000241 zst.avail_out=r_strlen;
242 r_strlen = r_strlen << 1;
243 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 default:
245 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000246 if (zst.msg == Z_NULL)
247 PyErr_Format(ZlibError, "Error %i while decompressing data",
248 err);
249 else
250 PyErr_Format(ZlibError,
251 "Error %i while decompressing data: %.200s",
252 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000253 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000254 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000255 return NULL;
256 }
257 }
258 } while(err!=Z_STREAM_END);
259
260 err=inflateEnd(&zst);
261 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000262 {
263 if (zst.msg == Z_NULL)
264 PyErr_Format(ZlibError,
265 "Error %i while finishing data decompression",
266 err);
267 else
268 PyErr_Format(ZlibError,
269 "Error %i while finishing data decompression: %.200s",
270 err, zst.msg);
271 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000272 return NULL;
273 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000274 _PyString_Resize(&result_str, zst.total_out);
275 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000276}
277
278static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000279PyZlib_compressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000280{
281 compobject *self;
282 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
283 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000284
Guido van Rossum43713e52000-02-29 13:59:29 +0000285 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000286 &memLevel, &strategy))
287 return NULL;
288
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000289 self = newcompobject(&Comptype);
Guido van Rossumfb221561997-04-29 15:38:09 +0000290 if (self==NULL) return(NULL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000291 self->zst.zalloc = (alloc_func)NULL;
292 self->zst.zfree = (free_func)Z_NULL;
293 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Guido van Rossumfb221561997-04-29 15:38:09 +0000294 switch(err)
295 {
296 case (Z_OK):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000297 self->is_initialised = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000298 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000299 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000300 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000301 PyErr_SetString(PyExc_MemoryError,
302 "Can't allocate memory for compression object");
303 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000304 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000305 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000306 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000307 "Invalid initialization option");
Guido van Rossumfb221561997-04-29 15:38:09 +0000308 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000309 default:
310 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000311 if (self->zst.msg == Z_NULL)
312 PyErr_Format(ZlibError,
313 "Error %i while creating compression object",
314 err);
315 else
316 PyErr_Format(ZlibError,
317 "Error %i while creating compression object: %.200s",
318 err, self->zst.msg);
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000319 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000320 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000321 }
322 }
323}
324
325static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000326PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000327{
328 int wbits=DEF_WBITS, err;
329 compobject *self;
Guido van Rossum43713e52000-02-29 13:59:29 +0000330 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Guido van Rossumfb221561997-04-29 15:38:09 +0000331 {
332 return NULL;
333 }
334 self=newcompobject(&Decomptype);
335 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000336 self->zst.zalloc=(alloc_func)NULL;
337 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000338 err=inflateInit2(&self->zst, wbits);
339 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000340 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000341 case (Z_OK):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000342 self->is_initialised = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000343 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000344 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000345 Py_DECREF(self);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000346 PyErr_SetString(PyExc_ValueError,
347 "Invalid initialization option");
348 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000349 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000350 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000351 PyErr_SetString(PyExc_MemoryError,
352 "Can't allocate memory for decompression object");
353 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000354 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000355 {
356 if (self->zst.msg == Z_NULL)
357 PyErr_Format(ZlibError,
358 "Error %i while creating decompression object",
359 err);
360 else
361 PyErr_Format(ZlibError,
362 "Error %i while creating decompression object: %.200s",
363 err, self->zst.msg);
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000364 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000365 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000366 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000367 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000368}
369
370static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000371Comp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000372{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000373 if (self->is_initialised)
374 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000375 Py_XDECREF(self->unused_data);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000376 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000377}
378
379static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000380Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000381{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000382 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000383 Py_XDECREF(self->unused_data);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000384 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000385}
386
Guido van Rossum3c540301997-06-03 22:21:03 +0000387static char comp_compress__doc__[] =
388"compress(data) -- Return a string containing a compressed version of the data.\n\n"
389"After calling this function, some of the input data may still\n"
390"be stored in internal buffers for later processing.\n"
391"Call the flush() method to clear these buffers."
392;
393
394
Guido van Rossumfb221561997-04-29 15:38:09 +0000395static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000396PyZlib_objcompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000397{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000398 int err = Z_OK, inplen;
399 int length = DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000400 PyObject *RetVal;
401 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000402 unsigned long start_total_out;
Guido van Rossumfb221561997-04-29 15:38:09 +0000403
Guido van Rossum43713e52000-02-29 13:59:29 +0000404 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000405 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000406 self->zst.avail_in = inplen;
407 self->zst.next_in = input;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000408 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
409 PyErr_SetString(PyExc_MemoryError,
410 "Can't allocate memory to compress data");
411 return NULL;
412 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000413 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000414 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000415 self->zst.avail_out = length;
416 while (self->zst.avail_in != 0 && err == Z_OK)
417 {
418 err = deflate(&(self->zst), Z_NO_FLUSH);
419 if (self->zst.avail_out <= 0) {
420 if (_PyString_Resize(&RetVal, length << 1) == -1) {
421 PyErr_SetString(PyExc_MemoryError,
422 "Can't allocate memory to compress data");
423 return NULL;
424 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000425 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000426 self->zst.avail_out = length;
427 length = length << 1;
428 }
429 }
430 if (err != Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000431 {
432 if (self->zst.msg == Z_NULL)
433 PyErr_Format(ZlibError, "Error %i while compressing",
434 err);
435 else
436 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
437 err, self->zst.msg);
438 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000439 return NULL;
440 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000441 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000442 return RetVal;
443}
444
Guido van Rossum3c540301997-06-03 22:21:03 +0000445static char decomp_decompress__doc__[] =
446"decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
447"After calling this function, some of the input data may still\n"
448"be stored in internal buffers for later processing.\n"
449"Call the flush() method to clear these buffers."
450;
451
Guido van Rossumfb221561997-04-29 15:38:09 +0000452static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000453PyZlib_objdecompress(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000454{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000455 int length, err, inplen;
Guido van Rossumfb221561997-04-29 15:38:09 +0000456 PyObject *RetVal;
457 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000458 unsigned long start_total_out;
459
Guido van Rossum43713e52000-02-29 13:59:29 +0000460 if (!PyArg_ParseTuple(args, "s#:decompress", &input, &inplen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000461 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000462 start_total_out = self->zst.total_out;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000463 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000464 self->zst.avail_in = inplen;
465 self->zst.next_in = input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000466 self->zst.avail_out = length = DEFAULTALLOC;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000467 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000468 err = Z_OK;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000469
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000470 while (self->zst.avail_in != 0 && err == Z_OK)
471 {
472 err = inflate(&(self->zst), Z_NO_FLUSH);
473 if (err == Z_OK && self->zst.avail_out <= 0)
474 {
475 if (_PyString_Resize(&RetVal, length << 1) == -1)
476 {
477 PyErr_SetString(PyExc_MemoryError,
478 "Can't allocate memory to compress data");
479 return NULL;
480 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000481 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000482 self->zst.avail_out = length;
483 length = length << 1;
484 }
485 }
486
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000487 if (err != Z_OK && err != Z_STREAM_END)
488 {
489 if (self->zst.msg == Z_NULL)
490 PyErr_Format(ZlibError, "Error %i while decompressing",
491 err);
492 else
493 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
494 err, self->zst.msg);
495 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000496 return NULL;
497 }
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000498
499 if (err == Z_STREAM_END)
500 {
501 /* The end of the compressed data has been reached, so set
502 the unused_data attribute to a string containing the
503 remainder of the data in the string. */
504 int pos = self->zst.next_in - input; /* Position in the string */
505 Py_XDECREF(self->unused_data); /* Free the original, empty string */
506
Guido van Rossumaee9bb21999-04-12 14:35:48 +0000507 self->unused_data = PyString_FromStringAndSize((char *)input+pos,
508 inplen-pos);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000509 if (self->unused_data == NULL) return NULL;
510 }
511
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000512 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000513 return RetVal;
514}
515
Guido van Rossum3c540301997-06-03 22:21:03 +0000516static char comp_flush__doc__[] =
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000517"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
518"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
519"default value used when mode is not specified is Z_FINISH.\n"
520"If mode == Z_FINISH, the compressor object can no longer be used after\n"
521"calling the flush() method. Otherwise, more data can still be compressed.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000522;
523
Guido van Rossumfb221561997-04-29 15:38:09 +0000524static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000525PyZlib_flush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000526{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000527 int length=DEFAULTALLOC, err = Z_OK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000528 PyObject *RetVal;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000529 int flushmode = Z_FINISH;
530 unsigned long start_total_out;
531
Guido van Rossum43713e52000-02-29 13:59:29 +0000532 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000533 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000534
535 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
536 doing any work at all; just return an empty string. */
537 if (flushmode == Z_NO_FLUSH)
538 {
539 return PyString_FromStringAndSize(NULL, 0);
540 }
541
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000542 self->zst.avail_in = 0;
543 self->zst.next_in = Z_NULL;
544 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
545 PyErr_SetString(PyExc_MemoryError,
546 "Can't allocate memory to compress data");
547 return NULL;
548 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000549 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000550 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000551 self->zst.avail_out = length;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000552
553 /* When flushing the zstream, there's no input data.
554 If zst.avail_out == 0, that means that more output space is
555 needed to complete the flush operation. */
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000556 while (1) {
557 err = deflate(&(self->zst), flushmode);
558
559 /* If the output is Z_OK, and there's still room in the output
560 buffer, then the flush is complete. */
561 if ( (err == Z_OK) && self->zst.avail_out > 0) break;
562
563 /* A nonzero return indicates some sort of error (but see
564 the comment for the error handler below) */
565 if ( err != Z_OK ) break;
566
567 /* There's no space left for output, so increase the buffer and loop
568 again */
569 if (_PyString_Resize(&RetVal, length << 1) == -1) {
570 PyErr_SetString(PyExc_MemoryError,
571 "Can't allocate memory to compress data");
572 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000573 }
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000574 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
575 self->zst.avail_out = length;
576 length = length << 1;
Jeremy Hylton6892aa31999-01-06 22:56:24 +0000577 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000578
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000579 /* Raise an exception indicating an error. The condition for
580 detecting a error is kind of complicated; Z_OK indicates no
581 error, but if the flushmode is Z_FINISH, then Z_STREAM_END is
582 also not an error. */
583 if (err!=Z_OK && !(flushmode == Z_FINISH && err == Z_STREAM_END) )
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000584 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000585 if (self->zst.msg == Z_NULL)
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000586 PyErr_Format(ZlibError, "Error %i while flushing",
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000587 err);
588 else
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000589 PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000590 err, self->zst.msg);
591 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000592 return NULL;
Jeremy Hylton6892aa31999-01-06 22:56:24 +0000593 }
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000594
595 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to
596 free various data structures */
597
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000598 if (flushmode == Z_FINISH) {
599 err=deflateEnd(&(self->zst));
600 if (err!=Z_OK) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000601 if (self->zst.msg == Z_NULL)
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000602 PyErr_Format(ZlibError, "Error %i from deflateEnd()",
603 err);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000604 else
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000605 PyErr_Format(ZlibError,
606 "Error %i from deflateEnd(): %.200s",
607 err, self->zst.msg);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000608 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000609 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000610 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000611 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000612 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000613 return RetVal;
614}
615
Guido van Rossum3c540301997-06-03 22:21:03 +0000616static char decomp_flush__doc__[] =
617"flush() -- Return a string containing any remaining decompressed data. "
618"The decompressor object can no longer be used after this call."
619;
620
Guido van Rossumfb221561997-04-29 15:38:09 +0000621static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000622PyZlib_unflush(compobject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000623{
624 int length=0, err;
Guido van Rossumfb221561997-04-29 15:38:09 +0000625 PyObject *RetVal;
626
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000627 if (!PyArg_ParseTuple(args, ""))
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000628 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000629 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
630 {
631 PyErr_SetString(PyExc_MemoryError,
632 "Can't allocate memory to decompress data");
633 return NULL;
634 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000635 self->zst.avail_in=0;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000636 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000637 length = self->zst.avail_out = DEFAULTALLOC;
638
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000639 /* I suspect that Z_BUF_ERROR is the only error code we need to check for
640 in the following loop, but will leave the Z_OK in for now to avoid
641 destabilizing this function. --amk */
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000642 err = Z_OK;
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000643 while ( err == Z_OK )
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000644 {
645 err = inflate(&(self->zst), Z_FINISH);
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000646 if ( ( err == Z_OK || err == Z_BUF_ERROR ) && self->zst.avail_out == 0)
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000647 {
648 if (_PyString_Resize(&RetVal, length << 1) == -1)
649 {
650 PyErr_SetString(PyExc_MemoryError,
651 "Can't allocate memory to decompress data");
652 return NULL;
653 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000654 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000655 self->zst.avail_out = length;
656 length = length << 1;
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000657 err = Z_OK;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000658 }
659 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000660 if (err!=Z_STREAM_END)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000661 {
662 if (self->zst.msg == Z_NULL)
663 PyErr_Format(ZlibError, "Error %i while decompressing",
664 err);
665 else
666 PyErr_Format(ZlibError, "Error %i while decompressing: %.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 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000671 err=inflateEnd(&(self->zst));
672 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000673 {
674 if (self->zst.msg == Z_NULL)
675 PyErr_Format(ZlibError,
676 "Error %i while flushing decompression object",
677 err);
678 else
679 PyErr_Format(ZlibError,
680 "Error %i while flushing decompression object: %.200s",
681 err, self->zst.msg);
682 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000683 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000684 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000685 _PyString_Resize(&RetVal,
686 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000687 return RetVal;
688}
689
690static PyMethodDef comp_methods[] =
691{
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000692 {"compress", (binaryfunc)PyZlib_objcompress,
693 METH_VARARGS, comp_compress__doc__},
694 {"flush", (binaryfunc)PyZlib_flush,
695 METH_VARARGS, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000696 {NULL, NULL}
697};
698
699static PyMethodDef Decomp_methods[] =
700{
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000701 {"decompress", (binaryfunc)PyZlib_objdecompress,
702 METH_VARARGS, decomp_decompress__doc__},
703 {"flush", (binaryfunc)PyZlib_unflush,
704 METH_VARARGS, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000705 {NULL, NULL}
706};
707
708static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000709Comp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000710{
711 return Py_FindMethod(comp_methods, (PyObject *)self, name);
712}
713
714static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000715Decomp_getattr(compobject *self, char *name)
Guido van Rossumfb221561997-04-29 15:38:09 +0000716{
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000717 if (strcmp(name, "unused_data") == 0)
718 {
719 Py_INCREF(self->unused_data);
720 return self->unused_data;
721 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000722 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
723}
724
Guido van Rossum3c540301997-06-03 22:21:03 +0000725static char adler32__doc__[] =
726"adler32(string) -- Compute an Adler-32 checksum of string, using "
727"a default starting value, and returning an integer value.\n"
728"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
729"the starting value provided, and returning an integer value\n"
730;
731
Guido van Rossumfb221561997-04-29 15:38:09 +0000732static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000733PyZlib_adler32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000734{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000735 uLong adler32val=adler32(0L, Z_NULL, 0);
736 Byte *buf;
737 int len;
Guido van Rossumfb221561997-04-29 15:38:09 +0000738
Guido van Rossum43713e52000-02-29 13:59:29 +0000739 if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000740 {
741 return NULL;
742 }
743 adler32val = adler32(adler32val, buf, len);
744 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000745}
746
Guido van Rossum3c540301997-06-03 22:21:03 +0000747static char crc32__doc__[] =
748"crc32(string) -- Compute a CRC-32 checksum of string, using "
749"a default starting value, and returning an integer value.\n"
750"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
751"the starting value provided, and returning an integer value.\n"
752;
Guido van Rossumfb221561997-04-29 15:38:09 +0000753
754static PyObject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000755PyZlib_crc32(PyObject *self, PyObject *args)
Guido van Rossumfb221561997-04-29 15:38:09 +0000756{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000757 uLong crc32val=crc32(0L, Z_NULL, 0);
758 Byte *buf;
759 int len;
Guido van Rossum43713e52000-02-29 13:59:29 +0000760 if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000761 {
762 return NULL;
763 }
764 crc32val = crc32(crc32val, buf, len);
765 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000766}
767
768
769static PyMethodDef zlib_methods[] =
770{
Andrew M. Kuchling14f51582000-08-03 02:04:05 +0000771 {"adler32", (PyCFunction)PyZlib_adler32,
772 METH_VARARGS, adler32__doc__},
773 {"compress", (PyCFunction)PyZlib_compress,
774 METH_VARARGS, compress__doc__},
775 {"compressobj", (PyCFunction)PyZlib_compressobj,
776 METH_VARARGS, compressobj__doc__},
777 {"crc32", (PyCFunction)PyZlib_crc32,
778 METH_VARARGS, crc32__doc__},
779 {"decompress", (PyCFunction)PyZlib_decompress,
780 METH_VARARGS, decompress__doc__},
781 {"decompressobj", (PyCFunction)PyZlib_decompressobj,
782 METH_VARARGS, decompressobj__doc__},
783 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +0000784};
785
786statichere PyTypeObject Comptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000787 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000788 0,
789 "Compress",
790 sizeof(compobject),
791 0,
792 (destructor)Comp_dealloc, /*tp_dealloc*/
793 0, /*tp_print*/
794 (getattrfunc)Comp_getattr, /*tp_getattr*/
795 0, /*tp_setattr*/
796 0, /*tp_compare*/
797 0, /*tp_repr*/
798 0, /*tp_as_number*/
799 0, /*tp_as_sequence*/
800 0, /*tp_as_mapping*/
801};
802
803statichere PyTypeObject Decomptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000804 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000805 0,
806 "Decompress",
807 sizeof(compobject),
808 0,
809 (destructor)Decomp_dealloc, /*tp_dealloc*/
810 0, /*tp_print*/
811 (getattrfunc)Decomp_getattr, /*tp_getattr*/
812 0, /*tp_setattr*/
813 0, /*tp_compare*/
814 0, /*tp_repr*/
815 0, /*tp_as_number*/
816 0, /*tp_as_sequence*/
817 0, /*tp_as_mapping*/
818};
819
820/* The following insint() routine was blatantly ripped off from
821 socketmodule.c */
822
823/* Convenience routine to export an integer value.
824 For simplicity, errors (which are unlikely anyway) are ignored. */
825static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000826insint(PyObject *d, char *name, int value)
Guido van Rossumfb221561997-04-29 15:38:09 +0000827{
828 PyObject *v = PyInt_FromLong((long) value);
829 if (v == NULL) {
830 /* Don't bother reporting this error */
831 PyErr_Clear();
832 }
833 else {
834 PyDict_SetItemString(d, name, v);
835 Py_DECREF(v);
836 }
837}
838
Guido van Rossum3c540301997-06-03 22:21:03 +0000839static char zlib_module_documentation[]=
840"The functions in this module allow compression and decompression "
841"using the zlib library, which is based on GNU zip. \n\n"
842"adler32(string) -- Compute an Adler-32 checksum.\n"
843"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
844"compress(string) -- Compress a string.\n"
845"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
846"compressobj([level]) -- Return a compressor object.\n"
847"crc32(string) -- Compute a CRC-32 checksum.\n"
848"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +0000849"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000850"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
851"Compressor objects support compress() and flush() methods; decompressor \n"
852"objects support decompress() and flush()."
853;
854
Guido van Rossum3886bb61998-12-04 18:50:17 +0000855DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000856PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +0000857{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000858 PyObject *m, *d, *ver;
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000859 Comptype.ob_type = &PyType_Type;
860 Decomptype.ob_type = &PyType_Type;
Guido van Rossum3c540301997-06-03 22:21:03 +0000861 m = Py_InitModule4("zlib", zlib_methods,
862 zlib_module_documentation,
863 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000864 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000865 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Fred Drake8972dfd1999-12-22 16:13:54 +0000866 if (ZlibError != NULL)
867 PyDict_SetItemString(d, "error", ZlibError);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000868
Guido van Rossumfb221561997-04-29 15:38:09 +0000869 insint(d, "MAX_WBITS", MAX_WBITS);
870 insint(d, "DEFLATED", DEFLATED);
871 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000872 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
873 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
874 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
875 insint(d, "Z_FILTERED", Z_FILTERED);
876 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
877 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000878
879 insint(d, "Z_FINISH", Z_FINISH);
880 insint(d, "Z_NO_FLUSH", Z_NO_FLUSH);
881 insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
882 insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH);
883
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000884 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake8972dfd1999-12-22 16:13:54 +0000885 if (ver != NULL) {
886 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
887 Py_DECREF(ver);
888 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000889}