blob: cff72932a9d2e3086015b6a9de8e7d58cfcdc951 [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 *
45newcompobject(type)
46 PyTypeObject *type;
47{
48 compobject *self;
Guido van Rossumb18618d2000-05-03 23:44:39 +000049 self = PyObject_New(compobject, type);
Guido van Rossumfb221561997-04-29 15:38:09 +000050 if (self == NULL)
51 return NULL;
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +000052 self->is_initialised = 0;
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +000053 self->unused_data = PyString_FromString("");
Guido van Rossumfb221561997-04-29 15:38:09 +000054 return self;
55}
56
Guido van Rossum3c540301997-06-03 22:21:03 +000057static char compress__doc__[] =
58"compress(string) -- Compress string using the default compression level, "
59"returning a string containing compressed data.\n"
60"compress(string, level) -- Compress string, using the chosen compression "
61"level (from 1 to 9). Return a string containing the compressed data.\n"
62;
63
Guido van Rossumfb221561997-04-29 15:38:09 +000064static PyObject *
65PyZlib_compress(self, args)
66 PyObject *self;
67 PyObject *args;
68{
69 PyObject *ReturnVal;
70 Byte *input, *output;
71 int length, level=Z_DEFAULT_COMPRESSION, err;
72 z_stream zst;
73
Guido van Rossum43713e52000-02-29 13:59:29 +000074 if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
Guido van Rossumfb221561997-04-29 15:38:09 +000075 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +000076 zst.avail_out = length + length/1000 + 12 + 1;
Guido van Rossumfb221561997-04-29 15:38:09 +000077 output=(Byte*)malloc(zst.avail_out);
78 if (output==NULL)
79 {
80 PyErr_SetString(PyExc_MemoryError,
81 "Can't allocate memory to compress data");
82 return NULL;
83 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +000084
Jeremy Hylton5bc4abe1998-12-21 17:15:00 +000085 zst.zalloc=(alloc_func)NULL;
Guido van Rossum97b54571997-06-03 22:21:47 +000086 zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +000087 zst.next_out=(Byte *)output;
88 zst.next_in =(Byte *)input;
89 zst.avail_in=length;
90 err=deflateInit(&zst, level);
91 switch(err)
92 {
93 case(Z_OK):
94 break;
95 case(Z_MEM_ERROR):
96 PyErr_SetString(PyExc_MemoryError,
97 "Out of memory while compressing data");
98 free(output);
99 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000100 case(Z_STREAM_ERROR):
101 PyErr_SetString(ZlibError,
102 "Bad compression level");
103 free(output);
104 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000105 default:
106 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000107 if (zst.msg == Z_NULL)
108 PyErr_Format(ZlibError, "Error %i while compressing data",
109 err);
110 else
111 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
112 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000113 deflateEnd(&zst);
114 free(output);
115 return NULL;
116 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000117 }
118
119 err=deflate(&zst, Z_FINISH);
120 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000121 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000122 case(Z_STREAM_END):
123 break;
124 /* Are there other errors to be trapped here? */
125 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000126 {
127 if (zst.msg == Z_NULL)
128 PyErr_Format(ZlibError, "Error %i while compressing data",
129 err);
130 else
131 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
132 err, zst.msg);
133 deflateEnd(&zst);
134 free(output);
135 return NULL;
136 }
137 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000138 err=deflateEnd(&zst);
139 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000140 {
141 if (zst.msg == Z_NULL)
142 PyErr_Format(ZlibError, "Error %i while finishing compression",
143 err);
144 else
145 PyErr_Format(ZlibError,
146 "Error %i while finishing compression: %.200s",
147 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000148 free(output);
149 return NULL;
150 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000151 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000152 free(output);
153 return ReturnVal;
154}
155
Guido van Rossum3c540301997-06-03 22:21:03 +0000156static char decompress__doc__[] =
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000157"decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
158"decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
159"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 +0000160;
161
Guido van Rossumfb221561997-04-29 15:38:09 +0000162static PyObject *
163PyZlib_decompress(self, args)
164 PyObject *self;
165 PyObject *args;
166{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000167 PyObject *result_str;
168 Byte *input;
Guido van Rossumfb221561997-04-29 15:38:09 +0000169 int length, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000170 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000171 z_stream zst;
Guido van Rossum43713e52000-02-29 13:59:29 +0000172 if (!PyArg_ParseTuple(args, "s#|ii:decompress", &input, &length, &wsize, &r_strlen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000173 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000174
175 if (r_strlen <= 0)
176 r_strlen = 1;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000177
Guido van Rossumfb221561997-04-29 15:38:09 +0000178 zst.avail_in=length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000179 zst.avail_out=r_strlen;
180 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
181 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000182 PyErr_SetString(PyExc_MemoryError,
183 "Can't allocate memory to decompress data");
184 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000185 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000186 zst.zalloc=(alloc_func)NULL;
187 zst.zfree=(free_func)Z_NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000188 zst.next_out=(Byte *)PyString_AsString(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000189 zst.next_in =(Byte *)input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000190 err=inflateInit2(&zst, wsize);
Guido van Rossumfb221561997-04-29 15:38:09 +0000191 switch(err)
192 {
193 case(Z_OK):
194 break;
195 case(Z_MEM_ERROR):
196 PyErr_SetString(PyExc_MemoryError,
197 "Out of memory while decompressing data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000198 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000199 return NULL;
200 default:
201 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000202 if (zst.msg == Z_NULL)
203 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
204 err);
205 else
206 PyErr_Format(ZlibError,
207 "Error %i while preparing to decompress data: %.200s",
208 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000209 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000210 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000211 return NULL;
212 }
213 }
214 do
215 {
216 err=inflate(&zst, Z_FINISH);
217 switch(err)
218 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000219 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000220 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000221 case(Z_BUF_ERROR):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000222 case(Z_OK):
223 /* need more memory */
224 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
Guido van Rossumfb221561997-04-29 15:38:09 +0000225 {
226 PyErr_SetString(PyExc_MemoryError,
227 "Out of memory while decompressing data");
228 inflateEnd(&zst);
229 return NULL;
230 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000231 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000232 zst.avail_out=r_strlen;
233 r_strlen = r_strlen << 1;
234 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000235 default:
236 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000237 if (zst.msg == Z_NULL)
238 PyErr_Format(ZlibError, "Error %i while decompressing data",
239 err);
240 else
241 PyErr_Format(ZlibError,
242 "Error %i while decompressing data: %.200s",
243 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000245 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000246 return NULL;
247 }
248 }
249 } while(err!=Z_STREAM_END);
250
251 err=inflateEnd(&zst);
252 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000253 {
254 if (zst.msg == Z_NULL)
255 PyErr_Format(ZlibError,
256 "Error %i while finishing data decompression",
257 err);
258 else
259 PyErr_Format(ZlibError,
260 "Error %i while finishing data decompression: %.200s",
261 err, zst.msg);
262 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000263 return NULL;
264 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000265 _PyString_Resize(&result_str, zst.total_out);
266 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000267}
268
269static PyObject *
270PyZlib_compressobj(selfptr, args)
271 PyObject *selfptr;
272 PyObject *args;
273{
274 compobject *self;
275 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
276 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000277
Guido van Rossum43713e52000-02-29 13:59:29 +0000278 if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000279 &memLevel, &strategy))
280 return NULL;
281
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000282 self = newcompobject(&Comptype);
Guido van Rossumfb221561997-04-29 15:38:09 +0000283 if (self==NULL) return(NULL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000284 self->zst.zalloc = (alloc_func)NULL;
285 self->zst.zfree = (free_func)Z_NULL;
286 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Guido van Rossumfb221561997-04-29 15:38:09 +0000287 switch(err)
288 {
289 case (Z_OK):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000290 self->is_initialised = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000291 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000292 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000293 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000294 PyErr_SetString(PyExc_MemoryError,
295 "Can't allocate memory for compression object");
296 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000297 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000298 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000299 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000300 "Invalid initialization option");
Guido van Rossumfb221561997-04-29 15:38:09 +0000301 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000302 default:
303 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000304 if (self->zst.msg == Z_NULL)
305 PyErr_Format(ZlibError,
306 "Error %i while creating compression object",
307 err);
308 else
309 PyErr_Format(ZlibError,
310 "Error %i while creating compression object: %.200s",
311 err, self->zst.msg);
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000312 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000313 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000314 }
315 }
316}
317
318static PyObject *
319PyZlib_decompressobj(selfptr, args)
320 PyObject *selfptr;
321 PyObject *args;
322{
323 int wbits=DEF_WBITS, err;
324 compobject *self;
Guido van Rossum43713e52000-02-29 13:59:29 +0000325 if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
Guido van Rossumfb221561997-04-29 15:38:09 +0000326 {
327 return NULL;
328 }
329 self=newcompobject(&Decomptype);
330 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000331 self->zst.zalloc=(alloc_func)NULL;
332 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000333 err=inflateInit2(&self->zst, wbits);
334 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000335 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000336 case (Z_OK):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000337 self->is_initialised = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000338 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000339 case(Z_STREAM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000340 Py_DECREF(self);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000341 PyErr_SetString(PyExc_ValueError,
342 "Invalid initialization option");
343 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000344 case (Z_MEM_ERROR):
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000345 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000346 PyErr_SetString(PyExc_MemoryError,
347 "Can't allocate memory for decompression object");
348 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000349 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000350 {
351 if (self->zst.msg == Z_NULL)
352 PyErr_Format(ZlibError,
353 "Error %i while creating decompression object",
354 err);
355 else
356 PyErr_Format(ZlibError,
357 "Error %i while creating decompression object: %.200s",
358 err, self->zst.msg);
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000359 Py_DECREF(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000360 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000361 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000362 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000363}
364
365static void
366Comp_dealloc(self)
367 compobject *self;
368{
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000369 if (self->is_initialised)
370 deflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000371 Py_XDECREF(self->unused_data);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000372 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000373}
374
375static void
376Decomp_dealloc(self)
377 compobject *self;
378{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000379 inflateEnd(&self->zst);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000380 Py_XDECREF(self->unused_data);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000381 PyObject_Del(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000382}
383
Guido van Rossum3c540301997-06-03 22:21:03 +0000384static char comp_compress__doc__[] =
385"compress(data) -- Return a string containing a compressed version of the data.\n\n"
386"After calling this function, some of the input data may still\n"
387"be stored in internal buffers for later processing.\n"
388"Call the flush() method to clear these buffers."
389;
390
391
Guido van Rossumfb221561997-04-29 15:38:09 +0000392static PyObject *
393PyZlib_objcompress(self, args)
394 compobject *self;
395 PyObject *args;
396{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000397 int err = Z_OK, inplen;
398 int length = DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000399 PyObject *RetVal;
400 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000401 unsigned long start_total_out;
Guido van Rossumfb221561997-04-29 15:38:09 +0000402
Guido van Rossum43713e52000-02-29 13:59:29 +0000403 if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000404 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000405 self->zst.avail_in = inplen;
406 self->zst.next_in = input;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000407 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
408 PyErr_SetString(PyExc_MemoryError,
409 "Can't allocate memory to compress data");
410 return NULL;
411 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000412 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000413 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000414 self->zst.avail_out = length;
415 while (self->zst.avail_in != 0 && err == Z_OK)
416 {
417 err = deflate(&(self->zst), Z_NO_FLUSH);
418 if (self->zst.avail_out <= 0) {
419 if (_PyString_Resize(&RetVal, length << 1) == -1) {
420 PyErr_SetString(PyExc_MemoryError,
421 "Can't allocate memory to compress data");
422 return NULL;
423 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000424 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000425 self->zst.avail_out = length;
426 length = length << 1;
427 }
428 }
429 if (err != Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000430 {
431 if (self->zst.msg == Z_NULL)
432 PyErr_Format(ZlibError, "Error %i while compressing",
433 err);
434 else
435 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
436 err, self->zst.msg);
437 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000438 return NULL;
439 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000440 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000441 return RetVal;
442}
443
Guido van Rossum3c540301997-06-03 22:21:03 +0000444static char decomp_decompress__doc__[] =
445"decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
446"After calling this function, some of the input data may still\n"
447"be stored in internal buffers for later processing.\n"
448"Call the flush() method to clear these buffers."
449;
450
Guido van Rossumfb221561997-04-29 15:38:09 +0000451static PyObject *
452PyZlib_objdecompress(self, args)
453 compobject *self;
454 PyObject *args;
455{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000456 int length, err, inplen;
Guido van Rossumfb221561997-04-29 15:38:09 +0000457 PyObject *RetVal;
458 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000459 unsigned long start_total_out;
460
Guido van Rossum43713e52000-02-29 13:59:29 +0000461 if (!PyArg_ParseTuple(args, "s#:decompress", &input, &inplen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000462 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000463 start_total_out = self->zst.total_out;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000464 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000465 self->zst.avail_in = inplen;
466 self->zst.next_in = input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000467 self->zst.avail_out = length = DEFAULTALLOC;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000468 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000469 err = Z_OK;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000470
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000471 while (self->zst.avail_in != 0 && err == Z_OK)
472 {
473 err = inflate(&(self->zst), Z_NO_FLUSH);
474 if (err == Z_OK && self->zst.avail_out <= 0)
475 {
476 if (_PyString_Resize(&RetVal, length << 1) == -1)
477 {
478 PyErr_SetString(PyExc_MemoryError,
479 "Can't allocate memory to compress data");
480 return NULL;
481 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000482 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000483 self->zst.avail_out = length;
484 length = length << 1;
485 }
486 }
487
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000488 if (err != Z_OK && err != Z_STREAM_END)
489 {
490 if (self->zst.msg == Z_NULL)
491 PyErr_Format(ZlibError, "Error %i while decompressing",
492 err);
493 else
494 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
495 err, self->zst.msg);
496 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000497 return NULL;
498 }
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000499
500 if (err == Z_STREAM_END)
501 {
502 /* The end of the compressed data has been reached, so set
503 the unused_data attribute to a string containing the
504 remainder of the data in the string. */
505 int pos = self->zst.next_in - input; /* Position in the string */
506 Py_XDECREF(self->unused_data); /* Free the original, empty string */
507
Guido van Rossumaee9bb21999-04-12 14:35:48 +0000508 self->unused_data = PyString_FromStringAndSize((char *)input+pos,
509 inplen-pos);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000510 if (self->unused_data == NULL) return NULL;
511 }
512
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000513 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000514 return RetVal;
515}
516
Guido van Rossum3c540301997-06-03 22:21:03 +0000517static char comp_flush__doc__[] =
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000518"flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
519"mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
520"default value used when mode is not specified is Z_FINISH.\n"
521"If mode == Z_FINISH, the compressor object can no longer be used after\n"
522"calling the flush() method. Otherwise, more data can still be compressed.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000523;
524
Guido van Rossumfb221561997-04-29 15:38:09 +0000525static PyObject *
526PyZlib_flush(self, args)
527 compobject *self;
528 PyObject *args;
529{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000530 int length=DEFAULTALLOC, err = Z_OK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000531 PyObject *RetVal;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000532 int flushmode = Z_FINISH;
533 unsigned long start_total_out;
534
Guido van Rossum43713e52000-02-29 13:59:29 +0000535 if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000536 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000537
538 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
539 doing any work at all; just return an empty string. */
540 if (flushmode == Z_NO_FLUSH)
541 {
542 return PyString_FromStringAndSize(NULL, 0);
543 }
544
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000545 self->zst.avail_in = 0;
546 self->zst.next_in = Z_NULL;
547 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
548 PyErr_SetString(PyExc_MemoryError,
549 "Can't allocate memory to compress data");
550 return NULL;
551 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000552 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000553 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000554 self->zst.avail_out = length;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000555
556 /* When flushing the zstream, there's no input data.
557 If zst.avail_out == 0, that means that more output space is
558 needed to complete the flush operation. */
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000559 while (1) {
560 err = deflate(&(self->zst), flushmode);
561
562 /* If the output is Z_OK, and there's still room in the output
563 buffer, then the flush is complete. */
564 if ( (err == Z_OK) && self->zst.avail_out > 0) break;
565
566 /* A nonzero return indicates some sort of error (but see
567 the comment for the error handler below) */
568 if ( err != Z_OK ) break;
569
570 /* There's no space left for output, so increase the buffer and loop
571 again */
572 if (_PyString_Resize(&RetVal, length << 1) == -1) {
573 PyErr_SetString(PyExc_MemoryError,
574 "Can't allocate memory to compress data");
575 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000576 }
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000577 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
578 self->zst.avail_out = length;
579 length = length << 1;
Jeremy Hylton6892aa31999-01-06 22:56:24 +0000580 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000581
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000582 /* Raise an exception indicating an error. The condition for
583 detecting a error is kind of complicated; Z_OK indicates no
584 error, but if the flushmode is Z_FINISH, then Z_STREAM_END is
585 also not an error. */
586 if (err!=Z_OK && !(flushmode == Z_FINISH && err == Z_STREAM_END) )
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000587 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000588 if (self->zst.msg == Z_NULL)
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000589 PyErr_Format(ZlibError, "Error %i while flushing",
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000590 err);
591 else
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000592 PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000593 err, self->zst.msg);
594 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000595 return NULL;
Jeremy Hylton6892aa31999-01-06 22:56:24 +0000596 }
Andrew M. Kuchling173156f1999-03-22 19:25:30 +0000597
598 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to
599 free various data structures */
600
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000601 if (flushmode == Z_FINISH) {
602 err=deflateEnd(&(self->zst));
603 if (err!=Z_OK) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000604 if (self->zst.msg == Z_NULL)
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000605 PyErr_Format(ZlibError, "Error %i from deflateEnd()",
606 err);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000607 else
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000608 PyErr_Format(ZlibError,
609 "Error %i from deflateEnd(): %.200s",
610 err, self->zst.msg);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000611 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000612 return NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000613 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000614 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000615 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000616 return RetVal;
617}
618
Guido van Rossum3c540301997-06-03 22:21:03 +0000619static char decomp_flush__doc__[] =
620"flush() -- Return a string containing any remaining decompressed data. "
621"The decompressor object can no longer be used after this call."
622;
623
Guido van Rossumfb221561997-04-29 15:38:09 +0000624static PyObject *
625PyZlib_unflush(self, args)
626 compobject *self;
627 PyObject *args;
628{
629 int length=0, err;
Guido van Rossumfb221561997-04-29 15:38:09 +0000630 PyObject *RetVal;
631
632 if (!PyArg_NoArgs(args))
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000633 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000634 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
635 {
636 PyErr_SetString(PyExc_MemoryError,
637 "Can't allocate memory to decompress data");
638 return NULL;
639 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000640 self->zst.avail_in=0;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000641 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000642 length = self->zst.avail_out = DEFAULTALLOC;
643
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000644 /* I suspect that Z_BUF_ERROR is the only error code we need to check for
645 in the following loop, but will leave the Z_OK in for now to avoid
646 destabilizing this function. --amk */
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000647 err = Z_OK;
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000648 while ( err == Z_OK )
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000649 {
650 err = inflate(&(self->zst), Z_FINISH);
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000651 if ( ( err == Z_OK || err == Z_BUF_ERROR ) && self->zst.avail_out == 0)
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000652 {
653 if (_PyString_Resize(&RetVal, length << 1) == -1)
654 {
655 PyErr_SetString(PyExc_MemoryError,
656 "Can't allocate memory to decompress data");
657 return NULL;
658 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000659 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000660 self->zst.avail_out = length;
661 length = length << 1;
Guido van Rossumb729a1d1999-04-07 20:23:17 +0000662 err = Z_OK;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000663 }
664 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000665 if (err!=Z_STREAM_END)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000666 {
667 if (self->zst.msg == Z_NULL)
668 PyErr_Format(ZlibError, "Error %i while decompressing",
669 err);
670 else
671 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
672 err, self->zst.msg);
673 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000674 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000675 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000676 err=inflateEnd(&(self->zst));
677 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000678 {
679 if (self->zst.msg == Z_NULL)
680 PyErr_Format(ZlibError,
681 "Error %i while flushing decompression object",
682 err);
683 else
684 PyErr_Format(ZlibError,
685 "Error %i while flushing decompression object: %.200s",
686 err, self->zst.msg);
687 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000688 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000689 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000690 _PyString_Resize(&RetVal,
691 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000692 return RetVal;
693}
694
695static PyMethodDef comp_methods[] =
696{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000697 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000698 {"flush", (binaryfunc)PyZlib_flush, 1, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000699 {NULL, NULL}
700};
701
702static PyMethodDef Decomp_methods[] =
703{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000704 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
705 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000706 {NULL, NULL}
707};
708
709static PyObject *
710Comp_getattr(self, name)
711 compobject *self;
712 char *name;
713{
714 return Py_FindMethod(comp_methods, (PyObject *)self, name);
715}
716
717static PyObject *
718Decomp_getattr(self, name)
719 compobject *self;
720 char *name;
721{
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000722 if (strcmp(name, "unused_data") == 0)
723 {
724 Py_INCREF(self->unused_data);
725 return self->unused_data;
726 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000727 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
728}
729
Guido van Rossum3c540301997-06-03 22:21:03 +0000730static char adler32__doc__[] =
731"adler32(string) -- Compute an Adler-32 checksum of string, using "
732"a default starting value, and returning an integer value.\n"
733"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
734"the starting value provided, and returning an integer value\n"
735;
736
Guido van Rossumfb221561997-04-29 15:38:09 +0000737static PyObject *
738PyZlib_adler32(self, args)
739 PyObject *self, *args;
740{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000741 uLong adler32val=adler32(0L, Z_NULL, 0);
742 Byte *buf;
743 int len;
Guido van Rossumfb221561997-04-29 15:38:09 +0000744
Guido van Rossum43713e52000-02-29 13:59:29 +0000745 if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000746 {
747 return NULL;
748 }
749 adler32val = adler32(adler32val, buf, len);
750 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000751}
752
Guido van Rossum3c540301997-06-03 22:21:03 +0000753static char crc32__doc__[] =
754"crc32(string) -- Compute a CRC-32 checksum of string, using "
755"a default starting value, and returning an integer value.\n"
756"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
757"the starting value provided, and returning an integer value.\n"
758;
Guido van Rossumfb221561997-04-29 15:38:09 +0000759
760static PyObject *
761PyZlib_crc32(self, args)
762 PyObject *self, *args;
763{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000764 uLong crc32val=crc32(0L, Z_NULL, 0);
765 Byte *buf;
766 int len;
Guido van Rossum43713e52000-02-29 13:59:29 +0000767 if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val))
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000768 {
769 return NULL;
770 }
771 crc32val = crc32(crc32val, buf, len);
772 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000773}
774
775
776static PyMethodDef zlib_methods[] =
777{
Guido van Rossum3c540301997-06-03 22:21:03 +0000778 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
779 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
780 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
781 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
782 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
783 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000784 {NULL, NULL}
785};
786
787statichere PyTypeObject Comptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000788 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000789 0,
790 "Compress",
791 sizeof(compobject),
792 0,
793 (destructor)Comp_dealloc, /*tp_dealloc*/
794 0, /*tp_print*/
795 (getattrfunc)Comp_getattr, /*tp_getattr*/
796 0, /*tp_setattr*/
797 0, /*tp_compare*/
798 0, /*tp_repr*/
799 0, /*tp_as_number*/
800 0, /*tp_as_sequence*/
801 0, /*tp_as_mapping*/
802};
803
804statichere PyTypeObject Decomptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000805 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000806 0,
807 "Decompress",
808 sizeof(compobject),
809 0,
810 (destructor)Decomp_dealloc, /*tp_dealloc*/
811 0, /*tp_print*/
812 (getattrfunc)Decomp_getattr, /*tp_getattr*/
813 0, /*tp_setattr*/
814 0, /*tp_compare*/
815 0, /*tp_repr*/
816 0, /*tp_as_number*/
817 0, /*tp_as_sequence*/
818 0, /*tp_as_mapping*/
819};
820
821/* The following insint() routine was blatantly ripped off from
822 socketmodule.c */
823
824/* Convenience routine to export an integer value.
825 For simplicity, errors (which are unlikely anyway) are ignored. */
826static void
827insint(d, name, value)
828 PyObject *d;
829 char *name;
830 int value;
831{
832 PyObject *v = PyInt_FromLong((long) value);
833 if (v == NULL) {
834 /* Don't bother reporting this error */
835 PyErr_Clear();
836 }
837 else {
838 PyDict_SetItemString(d, name, v);
839 Py_DECREF(v);
840 }
841}
842
Guido van Rossum3c540301997-06-03 22:21:03 +0000843static char zlib_module_documentation[]=
844"The functions in this module allow compression and decompression "
845"using the zlib library, which is based on GNU zip. \n\n"
846"adler32(string) -- Compute an Adler-32 checksum.\n"
847"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
848"compress(string) -- Compress a string.\n"
849"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
850"compressobj([level]) -- Return a compressor object.\n"
851"crc32(string) -- Compute a CRC-32 checksum.\n"
852"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +0000853"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000854"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
855"Compressor objects support compress() and flush() methods; decompressor \n"
856"objects support decompress() and flush()."
857;
858
Guido van Rossum3886bb61998-12-04 18:50:17 +0000859DL_EXPORT(void)
Guido van Rossumfb221561997-04-29 15:38:09 +0000860PyInit_zlib()
861{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000862 PyObject *m, *d, *ver;
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000863 Comptype.ob_type = &PyType_Type;
864 Decomptype.ob_type = &PyType_Type;
Guido van Rossum3c540301997-06-03 22:21:03 +0000865 m = Py_InitModule4("zlib", zlib_methods,
866 zlib_module_documentation,
867 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000868 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000869 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Fred Drake8972dfd1999-12-22 16:13:54 +0000870 if (ZlibError != NULL)
871 PyDict_SetItemString(d, "error", ZlibError);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000872
Guido van Rossumfb221561997-04-29 15:38:09 +0000873 insint(d, "MAX_WBITS", MAX_WBITS);
874 insint(d, "DEFLATED", DEFLATED);
875 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000876 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
877 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
878 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
879 insint(d, "Z_FILTERED", Z_FILTERED);
880 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
881 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000882
883 insint(d, "Z_FINISH", Z_FINISH);
884 insint(d, "Z_NO_FLUSH", Z_NO_FLUSH);
885 insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
886 insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH);
887
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000888 ver = PyString_FromString(ZLIB_VERSION);
Fred Drake8972dfd1999-12-22 16:13:54 +0000889 if (ver != NULL) {
890 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
891 Py_DECREF(ver);
892 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000893}