blob: 9cfcbb721b8bf0c79f26ceb133aa0168210813e2 [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"
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +00006#ifdef MS_WIN32
7#define ZLIB_DLL
8#endif
Guido van Rossum97b54571997-06-03 22:21:47 +00009#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000010
11/* The following parameters are copied from zutil.h, version 0.95 */
12#define DEFLATED 8
13#if MAX_MEM_LEVEL >= 8
14# define DEF_MEM_LEVEL 8
15#else
16# define DEF_MEM_LEVEL MAX_MEM_LEVEL
17#endif
18#define DEF_WBITS MAX_WBITS
19
20/* The output buffer will be increased in chunks of ADDCHUNK bytes. */
Jeremy Hylton41b9f001997-08-13 23:19:55 +000021#define DEFAULTALLOC 16*1024
Guido van Rossumfb221561997-04-29 15:38:09 +000022#define PyInit_zlib initzlib
23
24staticforward PyTypeObject Comptype;
25staticforward PyTypeObject Decomptype;
26
27static PyObject *ZlibError;
28
29typedef struct
30{
31 PyObject_HEAD
32 z_stream zst;
33} compobject;
34
Guido van Rossum3c540301997-06-03 22:21:03 +000035static char compressobj__doc__[] =
36"compressobj() -- Return a compressor object.\n"
37"compressobj(level) -- Return a compressor object, using the given compression level.\n"
38;
39
40static char decompressobj__doc__[] =
41"decompressobj() -- Return a decompressor object.\n"
42"decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
43;
44
Guido van Rossumfb221561997-04-29 15:38:09 +000045static compobject *
46newcompobject(type)
47 PyTypeObject *type;
48{
49 compobject *self;
50 self = PyObject_NEW(compobject, type);
51 if (self == NULL)
52 return NULL;
53 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 *
64PyZlib_compress(self, args)
65 PyObject *self;
66 PyObject *args;
67{
68 PyObject *ReturnVal;
69 Byte *input, *output;
70 int length, level=Z_DEFAULT_COMPRESSION, err;
71 z_stream zst;
72
73 if (!PyArg_ParseTuple(args, "s#|i", &input, &length, &level))
74 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +000075 zst.avail_out = length + length/1000 + 12 + 1;
Guido van Rossumfb221561997-04-29 15:38:09 +000076 output=(Byte*)malloc(zst.avail_out);
77 if (output==NULL)
78 {
79 PyErr_SetString(PyExc_MemoryError,
80 "Can't allocate memory to compress data");
81 return NULL;
82 }
Guido van Rossum97b54571997-06-03 22:21:47 +000083 zst.zalloc=(alloc_func)NULL;
84 zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +000085 zst.next_out=(Byte *)output;
86 zst.next_in =(Byte *)input;
87 zst.avail_in=length;
88 err=deflateInit(&zst, level);
89 switch(err)
90 {
91 case(Z_OK):
92 break;
93 case(Z_MEM_ERROR):
94 PyErr_SetString(PyExc_MemoryError,
95 "Out of memory while compressing data");
96 free(output);
97 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +000098 case(Z_STREAM_ERROR):
99 PyErr_SetString(ZlibError,
100 "Bad compression level");
101 free(output);
102 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000103 default:
104 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000105 if (zst.msg == Z_NULL)
106 PyErr_Format(ZlibError, "Error %i while compressing data",
107 err);
108 else
109 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
110 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000111 deflateEnd(&zst);
112 free(output);
113 return NULL;
114 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000115 }
116
117 err=deflate(&zst, Z_FINISH);
118 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000119 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000120 case(Z_STREAM_END):
121 break;
122 /* Are there other errors to be trapped here? */
123 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000124 {
125 if (zst.msg == Z_NULL)
126 PyErr_Format(ZlibError, "Error %i while compressing data",
127 err);
128 else
129 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
130 err, zst.msg);
131 deflateEnd(&zst);
132 free(output);
133 return NULL;
134 }
135 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000136 err=deflateEnd(&zst);
137 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000138 {
139 if (zst.msg == Z_NULL)
140 PyErr_Format(ZlibError, "Error %i while finishing compression",
141 err);
142 else
143 PyErr_Format(ZlibError,
144 "Error %i while finishing compression: %.200s",
145 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000146 free(output);
147 return NULL;
148 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000149 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000150 free(output);
151 return ReturnVal;
152}
153
Guido van Rossum3c540301997-06-03 22:21:03 +0000154static char decompress__doc__[] =
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000155"decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
156"decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
157"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 +0000158;
159
Guido van Rossumfb221561997-04-29 15:38:09 +0000160static PyObject *
161PyZlib_decompress(self, args)
162 PyObject *self;
163 PyObject *args;
164{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000165 PyObject *result_str;
166 Byte *input;
Guido van Rossumfb221561997-04-29 15:38:09 +0000167 int length, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000168 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000169 z_stream zst;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000170 if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000171 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000172
173 if (r_strlen <= 0)
174 r_strlen = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000175
176 zst.avail_in=length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000177 zst.avail_out=r_strlen;
178 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
179 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000180 PyErr_SetString(PyExc_MemoryError,
181 "Can't allocate memory to decompress data");
182 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000183 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000184 zst.zalloc=(alloc_func)NULL;
185 zst.zfree=(free_func)Z_NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000186 zst.next_out=(Byte *)PyString_AsString(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000187 zst.next_in =(Byte *)input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000188 err=inflateInit2(&zst, wsize);
Guido van Rossumfb221561997-04-29 15:38:09 +0000189 switch(err)
190 {
191 case(Z_OK):
192 break;
193 case(Z_MEM_ERROR):
194 PyErr_SetString(PyExc_MemoryError,
195 "Out of memory while decompressing data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000196 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000197 return NULL;
198 default:
199 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000200 if (zst.msg == Z_NULL)
201 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
202 err);
203 else
204 PyErr_Format(ZlibError,
205 "Error %i while preparing to decompress data: %.200s",
206 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000207 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000208 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000209 return NULL;
210 }
211 }
212 do
213 {
214 err=inflate(&zst, Z_FINISH);
215 switch(err)
216 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000217 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000218 break;
Guido van Rossum115f5171998-04-23 20:22:11 +0000219 case(Z_BUF_ERROR):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000220 case(Z_OK):
221 /* need more memory */
222 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
Guido van Rossumfb221561997-04-29 15:38:09 +0000223 {
224 PyErr_SetString(PyExc_MemoryError,
225 "Out of memory while decompressing data");
226 inflateEnd(&zst);
227 return NULL;
228 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000229 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000230 zst.avail_out=r_strlen;
231 r_strlen = r_strlen << 1;
232 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000233 default:
234 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000235 if (zst.msg == Z_NULL)
236 PyErr_Format(ZlibError, "Error %i while decompressing data",
237 err);
238 else
239 PyErr_Format(ZlibError,
240 "Error %i while decompressing data: %.200s",
241 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000242 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000243 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 return NULL;
245 }
246 }
247 } while(err!=Z_STREAM_END);
248
249 err=inflateEnd(&zst);
250 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000251 {
252 if (zst.msg == Z_NULL)
253 PyErr_Format(ZlibError,
254 "Error %i while finishing data decompression",
255 err);
256 else
257 PyErr_Format(ZlibError,
258 "Error %i while finishing data decompression: %.200s",
259 err, zst.msg);
260 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000261 return NULL;
262 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000263 _PyString_Resize(&result_str, zst.total_out);
264 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000265}
266
267static PyObject *
268PyZlib_compressobj(selfptr, args)
269 PyObject *selfptr;
270 PyObject *args;
271{
272 compobject *self;
273 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
274 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000275
276 if (!PyArg_ParseTuple(args, "|iiiii", &level, &method, &wbits,
277 &memLevel, &strategy))
278 return NULL;
279
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000280 self = newcompobject(&Comptype);
Guido van Rossumfb221561997-04-29 15:38:09 +0000281 if (self==NULL) return(NULL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000282 self->zst.zalloc = (alloc_func)NULL;
283 self->zst.zfree = (free_func)Z_NULL;
284 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Guido van Rossumfb221561997-04-29 15:38:09 +0000285 switch(err)
286 {
287 case (Z_OK):
288 return (PyObject*)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000289 case (Z_MEM_ERROR):
290 PyErr_SetString(PyExc_MemoryError,
291 "Can't allocate memory for compression object");
292 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000293 case(Z_STREAM_ERROR):
294 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000295 "Invalid initialization option");
Guido van Rossumfb221561997-04-29 15:38:09 +0000296 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000297 default:
298 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000299 if (self->zst.msg == Z_NULL)
300 PyErr_Format(ZlibError,
301 "Error %i while creating compression object",
302 err);
303 else
304 PyErr_Format(ZlibError,
305 "Error %i while creating compression object: %.200s",
306 err, self->zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000307 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000308 }
309 }
310}
311
312static PyObject *
313PyZlib_decompressobj(selfptr, args)
314 PyObject *selfptr;
315 PyObject *args;
316{
317 int wbits=DEF_WBITS, err;
318 compobject *self;
319 if (!PyArg_ParseTuple(args, "|i", &wbits))
320 {
321 return NULL;
322 }
323 self=newcompobject(&Decomptype);
324 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000325 self->zst.zalloc=(alloc_func)NULL;
326 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000327 err=inflateInit2(&self->zst, wbits);
328 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000329 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000330 case (Z_OK):
331 return (PyObject*)self;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000332 case(Z_STREAM_ERROR):
333 PyErr_SetString(PyExc_ValueError,
334 "Invalid initialization option");
335 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000336 case (Z_MEM_ERROR):
337 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);
Guido van Rossumfb221561997-04-29 15:38:09 +0000350 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000351 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000352 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000353}
354
355static void
356Comp_dealloc(self)
357 compobject *self;
358{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000359 deflateEnd(&self->zst);
360 PyMem_DEL(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000361}
362
363static void
364Decomp_dealloc(self)
365 compobject *self;
366{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000367 inflateEnd(&self->zst);
368 PyMem_DEL(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000369}
370
Guido van Rossum3c540301997-06-03 22:21:03 +0000371static char comp_compress__doc__[] =
372"compress(data) -- Return a string containing a compressed version of the data.\n\n"
373"After calling this function, some of the input data may still\n"
374"be stored in internal buffers for later processing.\n"
375"Call the flush() method to clear these buffers."
376;
377
378
Guido van Rossumfb221561997-04-29 15:38:09 +0000379static PyObject *
380PyZlib_objcompress(self, args)
381 compobject *self;
382 PyObject *args;
383{
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
390 if (!PyArg_ParseTuple(args, "s#", &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 *
439PyZlib_objdecompress(self, args)
440 compobject *self;
441 PyObject *args;
442{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000443 int length, err, inplen;
Guido van Rossumfb221561997-04-29 15:38:09 +0000444 PyObject *RetVal;
445 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000446 unsigned long start_total_out;
447
Guido van Rossumfb221561997-04-29 15:38:09 +0000448 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
449 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000450 start_total_out = self->zst.total_out;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000451 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000452 self->zst.avail_in = inplen;
453 self->zst.next_in = input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000454 self->zst.avail_out = length = DEFAULTALLOC;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000455 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000456 err = Z_OK;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000457
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000458 while (self->zst.avail_in != 0 && err == Z_OK)
459 {
460 err = inflate(&(self->zst), Z_NO_FLUSH);
461 if (err == Z_OK && self->zst.avail_out <= 0)
462 {
463 if (_PyString_Resize(&RetVal, length << 1) == -1)
464 {
465 PyErr_SetString(PyExc_MemoryError,
466 "Can't allocate memory to compress data");
467 return NULL;
468 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000469 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000470 self->zst.avail_out = length;
471 length = length << 1;
472 }
473 }
474
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000475 if (err != Z_OK && err != Z_STREAM_END)
476 {
477 if (self->zst.msg == Z_NULL)
478 PyErr_Format(ZlibError, "Error %i while decompressing",
479 err);
480 else
481 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
482 err, self->zst.msg);
483 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000484 return NULL;
485 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000486 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000487 return RetVal;
488}
489
Guido van Rossum3c540301997-06-03 22:21:03 +0000490static char comp_flush__doc__[] =
491"flush() -- Return a string containing any remaining compressed data. "
492"The compressor object can no longer be used after this call."
493;
494
Guido van Rossumfb221561997-04-29 15:38:09 +0000495static PyObject *
496PyZlib_flush(self, args)
497 compobject *self;
498 PyObject *args;
499{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000500 int length=DEFAULTALLOC, err = Z_OK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000501 PyObject *RetVal;
502
503 if (!PyArg_NoArgs(args))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000504 return NULL;
505 self->zst.avail_in = 0;
506 self->zst.next_in = Z_NULL;
507 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
508 PyErr_SetString(PyExc_MemoryError,
509 "Can't allocate memory to compress data");
510 return NULL;
511 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000512 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000513 self->zst.avail_out = length;
514 while (err == Z_OK)
515 {
516 err = deflate(&(self->zst), Z_FINISH);
517 if (self->zst.avail_out <= 0) {
518 if (_PyString_Resize(&RetVal, length << 1) == -1) {
519 PyErr_SetString(PyExc_MemoryError,
520 "Can't allocate memory to compress data");
521 return NULL;
522 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000523 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000524 self->zst.avail_out = length;
525 length = length << 1;
526 }
527 }
528 if (err!=Z_STREAM_END) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000529 if (self->zst.msg == Z_NULL)
530 PyErr_Format(ZlibError, "Error %i while compressing",
531 err);
532 else
533 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
534 err, self->zst.msg);
535 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000536 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000537 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000538 err=deflateEnd(&(self->zst));
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000539 if (err!=Z_OK) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000540 if (self->zst.msg == Z_NULL)
541 PyErr_Format(ZlibError, "Error %i while flushing compression object",
542 err);
543 else
544 PyErr_Format(ZlibError,
545 "Error %i while flushing compression object: %.200s",
546 err, self->zst.msg);
547 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000548 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000549 }
550 _PyString_Resize(&RetVal,
551 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000552 return RetVal;
553}
554
Guido van Rossum3c540301997-06-03 22:21:03 +0000555static char decomp_flush__doc__[] =
556"flush() -- Return a string containing any remaining decompressed data. "
557"The decompressor object can no longer be used after this call."
558;
559
Guido van Rossumfb221561997-04-29 15:38:09 +0000560static PyObject *
561PyZlib_unflush(self, args)
562 compobject *self;
563 PyObject *args;
564{
565 int length=0, err;
Guido van Rossumfb221561997-04-29 15:38:09 +0000566 PyObject *RetVal;
567
568 if (!PyArg_NoArgs(args))
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000569 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000570 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
571 {
572 PyErr_SetString(PyExc_MemoryError,
573 "Can't allocate memory to decompress data");
574 return NULL;
575 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000576 self->zst.avail_in=0;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000577 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000578 length = self->zst.avail_out = DEFAULTALLOC;
579
580 err = Z_OK;
581 while (err == Z_OK)
582 {
583 err = inflate(&(self->zst), Z_FINISH);
584 if (err == Z_OK && self->zst.avail_out == 0)
585 {
586 if (_PyString_Resize(&RetVal, length << 1) == -1)
587 {
588 PyErr_SetString(PyExc_MemoryError,
589 "Can't allocate memory to decompress data");
590 return NULL;
591 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000592 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000593 self->zst.avail_out = length;
594 length = length << 1;
595 }
596 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000597 if (err!=Z_STREAM_END)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000598 {
599 if (self->zst.msg == Z_NULL)
600 PyErr_Format(ZlibError, "Error %i while decompressing",
601 err);
602 else
603 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
604 err, self->zst.msg);
605 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000606 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000607 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000608 err=inflateEnd(&(self->zst));
609 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000610 {
611 if (self->zst.msg == Z_NULL)
612 PyErr_Format(ZlibError,
613 "Error %i while flushing decompression object",
614 err);
615 else
616 PyErr_Format(ZlibError,
617 "Error %i while flushing decompression object: %.200s",
618 err, self->zst.msg);
619 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000620 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000621 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000622 _PyString_Resize(&RetVal,
623 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000624 return RetVal;
625}
626
627static PyMethodDef comp_methods[] =
628{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000629 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
630 {"flush", (binaryfunc)PyZlib_flush, 0, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000631 {NULL, NULL}
632};
633
634static PyMethodDef Decomp_methods[] =
635{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000636 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
637 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000638 {NULL, NULL}
639};
640
641static PyObject *
642Comp_getattr(self, name)
643 compobject *self;
644 char *name;
645{
646 return Py_FindMethod(comp_methods, (PyObject *)self, name);
647}
648
649static PyObject *
650Decomp_getattr(self, name)
651 compobject *self;
652 char *name;
653{
654 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
655}
656
Guido van Rossum3c540301997-06-03 22:21:03 +0000657static char adler32__doc__[] =
658"adler32(string) -- Compute an Adler-32 checksum of string, using "
659"a default starting value, and returning an integer value.\n"
660"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
661"the starting value provided, and returning an integer value\n"
662;
663
Guido van Rossumfb221561997-04-29 15:38:09 +0000664static PyObject *
665PyZlib_adler32(self, args)
666 PyObject *self, *args;
667{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000668 uLong adler32val=adler32(0L, Z_NULL, 0);
669 Byte *buf;
670 int len;
Guido van Rossumfb221561997-04-29 15:38:09 +0000671
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000672 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
673 {
674 return NULL;
675 }
676 adler32val = adler32(adler32val, buf, len);
677 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000678}
679
Guido van Rossum3c540301997-06-03 22:21:03 +0000680static char crc32__doc__[] =
681"crc32(string) -- Compute a CRC-32 checksum of string, using "
682"a default starting value, and returning an integer value.\n"
683"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
684"the starting value provided, and returning an integer value.\n"
685;
Guido van Rossumfb221561997-04-29 15:38:09 +0000686
687static PyObject *
688PyZlib_crc32(self, args)
689 PyObject *self, *args;
690{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000691 uLong crc32val=crc32(0L, Z_NULL, 0);
692 Byte *buf;
693 int len;
694 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
695 {
696 return NULL;
697 }
698 crc32val = crc32(crc32val, buf, len);
699 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000700}
701
702
703static PyMethodDef zlib_methods[] =
704{
Guido van Rossum3c540301997-06-03 22:21:03 +0000705 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
706 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
707 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
708 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
709 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
710 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000711 {NULL, NULL}
712};
713
714statichere PyTypeObject Comptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000715 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000716 0,
717 "Compress",
718 sizeof(compobject),
719 0,
720 (destructor)Comp_dealloc, /*tp_dealloc*/
721 0, /*tp_print*/
722 (getattrfunc)Comp_getattr, /*tp_getattr*/
723 0, /*tp_setattr*/
724 0, /*tp_compare*/
725 0, /*tp_repr*/
726 0, /*tp_as_number*/
727 0, /*tp_as_sequence*/
728 0, /*tp_as_mapping*/
729};
730
731statichere PyTypeObject Decomptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000732 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000733 0,
734 "Decompress",
735 sizeof(compobject),
736 0,
737 (destructor)Decomp_dealloc, /*tp_dealloc*/
738 0, /*tp_print*/
739 (getattrfunc)Decomp_getattr, /*tp_getattr*/
740 0, /*tp_setattr*/
741 0, /*tp_compare*/
742 0, /*tp_repr*/
743 0, /*tp_as_number*/
744 0, /*tp_as_sequence*/
745 0, /*tp_as_mapping*/
746};
747
748/* The following insint() routine was blatantly ripped off from
749 socketmodule.c */
750
751/* Convenience routine to export an integer value.
752 For simplicity, errors (which are unlikely anyway) are ignored. */
753static void
754insint(d, name, value)
755 PyObject *d;
756 char *name;
757 int value;
758{
759 PyObject *v = PyInt_FromLong((long) value);
760 if (v == NULL) {
761 /* Don't bother reporting this error */
762 PyErr_Clear();
763 }
764 else {
765 PyDict_SetItemString(d, name, v);
766 Py_DECREF(v);
767 }
768}
769
Guido van Rossum3c540301997-06-03 22:21:03 +0000770static char zlib_module_documentation[]=
771"The functions in this module allow compression and decompression "
772"using the zlib library, which is based on GNU zip. \n\n"
773"adler32(string) -- Compute an Adler-32 checksum.\n"
774"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
775"compress(string) -- Compress a string.\n"
776"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
777"compressobj([level]) -- Return a compressor object.\n"
778"crc32(string) -- Compute a CRC-32 checksum.\n"
779"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000780"decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000781"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
782"Compressor objects support compress() and flush() methods; decompressor \n"
783"objects support decompress() and flush()."
784;
785
Guido van Rossum3886bb61998-12-04 18:50:17 +0000786DL_EXPORT(void)
Guido van Rossumfb221561997-04-29 15:38:09 +0000787PyInit_zlib()
788{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000789 PyObject *m, *d, *ver;
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000790 Comptype.ob_type = &PyType_Type;
791 Decomptype.ob_type = &PyType_Type;
Guido van Rossum3c540301997-06-03 22:21:03 +0000792 m = Py_InitModule4("zlib", zlib_methods,
793 zlib_module_documentation,
794 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000795 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000796 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Guido van Rossumfb221561997-04-29 15:38:09 +0000797 PyDict_SetItemString(d, "error", ZlibError);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000798
Guido van Rossumfb221561997-04-29 15:38:09 +0000799 insint(d, "MAX_WBITS", MAX_WBITS);
800 insint(d, "DEFLATED", DEFLATED);
801 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000802 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
803 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
804 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
805 insint(d, "Z_FILTERED", Z_FILTERED);
806 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
807 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
808 ver = PyString_FromString(ZLIB_VERSION);
809 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
Guido van Rossumfb221561997-04-29 15:38:09 +0000810}