Fix the HTML tarball target to generate the HTML if needed instead of
[python/dscho.git] / Modules / zlibmodule.c
blob9402e99fcb30af016f60fac1f8012d6003c0f556
1 /* zlibmodule.c -- gzip-compatible data compression */
2 /* See http://www.cdrom.com/pub/infozip/zlib/ */
3 /* See http://www.winimage.com/zLibDll for Windows */
5 #include "Python.h"
6 #ifdef MS_WIN32
7 #define ZLIB_DLL
8 #endif
9 #include "zlib.h"
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
20 /* The output buffer will be increased in chunks of ADDCHUNK bytes. */
21 #define DEFAULTALLOC 16*1024
22 #define PyInit_zlib initzlib
24 staticforward PyTypeObject Comptype;
25 staticforward PyTypeObject Decomptype;
27 static PyObject *ZlibError;
29 typedef struct
31 PyObject_HEAD
32 z_stream zst;
33 } compobject;
35 static char compressobj__doc__[] =
36 "compressobj() -- Return a compressor object.\n"
37 "compressobj(level) -- Return a compressor object, using the given compression level.\n"
40 static 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"
45 static compobject *
46 newcompobject(type)
47 PyTypeObject *type;
49 compobject *self;
50 self = PyObject_NEW(compobject, type);
51 if (self == NULL)
52 return NULL;
53 return self;
56 static 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"
63 static PyObject *
64 PyZlib_compress(self, args)
65 PyObject *self;
66 PyObject *args;
68 PyObject *ReturnVal;
69 Byte *input, *output;
70 int length, level=Z_DEFAULT_COMPRESSION, err;
71 z_stream zst;
73 if (!PyArg_ParseTuple(args, "s#|i", &input, &length, &level))
74 return NULL;
75 zst.avail_out = length + length/1000 + 12 + 1;
76 output=(Byte*)malloc(zst.avail_out);
77 if (output==NULL)
79 PyErr_SetString(PyExc_MemoryError,
80 "Can't allocate memory to compress data");
81 return NULL;
84 zst.zalloc=(alloc_func)NULL;
85 zst.zfree=(free_func)Z_NULL;
86 zst.next_out=(Byte *)output;
87 zst.next_in =(Byte *)input;
88 zst.avail_in=length;
89 err=deflateInit(&zst, level);
90 switch(err)
92 case(Z_OK):
93 break;
94 case(Z_MEM_ERROR):
95 PyErr_SetString(PyExc_MemoryError,
96 "Out of memory while compressing data");
97 free(output);
98 return NULL;
99 case(Z_STREAM_ERROR):
100 PyErr_SetString(ZlibError,
101 "Bad compression level");
102 free(output);
103 return NULL;
104 default:
106 if (zst.msg == Z_NULL)
107 PyErr_Format(ZlibError, "Error %i while compressing data",
108 err);
109 else
110 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
111 err, zst.msg);
112 deflateEnd(&zst);
113 free(output);
114 return NULL;
118 err=deflate(&zst, Z_FINISH);
119 switch(err)
121 case(Z_STREAM_END):
122 break;
123 /* Are there other errors to be trapped here? */
124 default:
126 if (zst.msg == Z_NULL)
127 PyErr_Format(ZlibError, "Error %i while compressing data",
128 err);
129 else
130 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
131 err, zst.msg);
132 deflateEnd(&zst);
133 free(output);
134 return NULL;
137 err=deflateEnd(&zst);
138 if (err!=Z_OK)
140 if (zst.msg == Z_NULL)
141 PyErr_Format(ZlibError, "Error %i while finishing compression",
142 err);
143 else
144 PyErr_Format(ZlibError,
145 "Error %i while finishing compression: %.200s",
146 err, zst.msg);
147 free(output);
148 return NULL;
150 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
151 free(output);
152 return ReturnVal;
155 static char decompress__doc__[] =
156 "decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
157 "decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
158 "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"
161 static PyObject *
162 PyZlib_decompress(self, args)
163 PyObject *self;
164 PyObject *args;
166 PyObject *result_str;
167 Byte *input;
168 int length, err;
169 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
170 z_stream zst;
171 if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen))
172 return NULL;
174 if (r_strlen <= 0)
175 r_strlen = 1;
177 zst.avail_in=length;
178 zst.avail_out=r_strlen;
179 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
181 PyErr_SetString(PyExc_MemoryError,
182 "Can't allocate memory to decompress data");
183 return NULL;
185 zst.zalloc=(alloc_func)NULL;
186 zst.zfree=(free_func)Z_NULL;
187 zst.next_out=(Byte *)PyString_AsString(result_str);
188 zst.next_in =(Byte *)input;
189 err=inflateInit2(&zst, wsize);
190 switch(err)
192 case(Z_OK):
193 break;
194 case(Z_MEM_ERROR):
195 PyErr_SetString(PyExc_MemoryError,
196 "Out of memory while decompressing data");
197 Py_DECREF(result_str);
198 return NULL;
199 default:
201 if (zst.msg == Z_NULL)
202 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
203 err);
204 else
205 PyErr_Format(ZlibError,
206 "Error %i while preparing to decompress data: %.200s",
207 err, zst.msg);
208 inflateEnd(&zst);
209 Py_DECREF(result_str);
210 return NULL;
215 err=inflate(&zst, Z_FINISH);
216 switch(err)
218 case(Z_STREAM_END):
219 break;
220 case(Z_BUF_ERROR):
221 case(Z_OK):
222 /* need more memory */
223 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
225 PyErr_SetString(PyExc_MemoryError,
226 "Out of memory while decompressing data");
227 inflateEnd(&zst);
228 return NULL;
230 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
231 zst.avail_out=r_strlen;
232 r_strlen = r_strlen << 1;
233 break;
234 default:
236 if (zst.msg == Z_NULL)
237 PyErr_Format(ZlibError, "Error %i while decompressing data",
238 err);
239 else
240 PyErr_Format(ZlibError,
241 "Error %i while decompressing data: %.200s",
242 err, zst.msg);
243 inflateEnd(&zst);
244 Py_DECREF(result_str);
245 return NULL;
248 } while(err!=Z_STREAM_END);
250 err=inflateEnd(&zst);
251 if (err!=Z_OK)
253 if (zst.msg == Z_NULL)
254 PyErr_Format(ZlibError,
255 "Error %i while finishing data decompression",
256 err);
257 else
258 PyErr_Format(ZlibError,
259 "Error %i while finishing data decompression: %.200s",
260 err, zst.msg);
261 Py_DECREF(result_str);
262 return NULL;
264 _PyString_Resize(&result_str, zst.total_out);
265 return result_str;
268 static PyObject *
269 PyZlib_compressobj(selfptr, args)
270 PyObject *selfptr;
271 PyObject *args;
273 compobject *self;
274 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
275 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
277 if (!PyArg_ParseTuple(args, "|iiiii", &level, &method, &wbits,
278 &memLevel, &strategy))
279 return NULL;
281 self = newcompobject(&Comptype);
282 if (self==NULL) return(NULL);
283 self->zst.zalloc = (alloc_func)NULL;
284 self->zst.zfree = (free_func)Z_NULL;
285 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
286 switch(err)
288 case (Z_OK):
289 return (PyObject*)self;
290 case (Z_MEM_ERROR):
291 PyErr_SetString(PyExc_MemoryError,
292 "Can't allocate memory for compression object");
293 return NULL;
294 case(Z_STREAM_ERROR):
295 PyErr_SetString(PyExc_ValueError,
296 "Invalid initialization option");
297 return NULL;
298 default:
300 if (self->zst.msg == Z_NULL)
301 PyErr_Format(ZlibError,
302 "Error %i while creating compression object",
303 err);
304 else
305 PyErr_Format(ZlibError,
306 "Error %i while creating compression object: %.200s",
307 err, self->zst.msg);
308 return NULL;
313 static PyObject *
314 PyZlib_decompressobj(selfptr, args)
315 PyObject *selfptr;
316 PyObject *args;
318 int wbits=DEF_WBITS, err;
319 compobject *self;
320 if (!PyArg_ParseTuple(args, "|i", &wbits))
322 return NULL;
324 self=newcompobject(&Decomptype);
325 if (self==NULL) return(NULL);
326 self->zst.zalloc=(alloc_func)NULL;
327 self->zst.zfree=(free_func)Z_NULL;
328 err=inflateInit2(&self->zst, wbits);
329 switch(err)
331 case (Z_OK):
332 return (PyObject*)self;
333 case(Z_STREAM_ERROR):
334 PyErr_SetString(PyExc_ValueError,
335 "Invalid initialization option");
336 return NULL;
337 case (Z_MEM_ERROR):
338 PyErr_SetString(PyExc_MemoryError,
339 "Can't allocate memory for decompression object");
340 return NULL;
341 default:
343 if (self->zst.msg == Z_NULL)
344 PyErr_Format(ZlibError,
345 "Error %i while creating decompression object",
346 err);
347 else
348 PyErr_Format(ZlibError,
349 "Error %i while creating decompression object: %.200s",
350 err, self->zst.msg);
351 return NULL;
356 static void
357 Comp_dealloc(self)
358 compobject *self;
360 deflateEnd(&self->zst);
361 PyMem_DEL(self);
364 static void
365 Decomp_dealloc(self)
366 compobject *self;
368 inflateEnd(&self->zst);
369 PyMem_DEL(self);
372 static char comp_compress__doc__[] =
373 "compress(data) -- Return a string containing a compressed version of the data.\n\n"
374 "After calling this function, some of the input data may still\n"
375 "be stored in internal buffers for later processing.\n"
376 "Call the flush() method to clear these buffers."
380 static PyObject *
381 PyZlib_objcompress(self, args)
382 compobject *self;
383 PyObject *args;
385 int err = Z_OK, inplen;
386 int length = DEFAULTALLOC;
387 PyObject *RetVal;
388 Byte *input;
389 unsigned long start_total_out;
391 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
392 return NULL;
393 self->zst.avail_in = inplen;
394 self->zst.next_in = input;
395 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
396 PyErr_SetString(PyExc_MemoryError,
397 "Can't allocate memory to compress data");
398 return NULL;
400 start_total_out = self->zst.total_out;
401 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
402 self->zst.avail_out = length;
403 while (self->zst.avail_in != 0 && err == Z_OK)
405 err = deflate(&(self->zst), Z_NO_FLUSH);
406 if (self->zst.avail_out <= 0) {
407 if (_PyString_Resize(&RetVal, length << 1) == -1) {
408 PyErr_SetString(PyExc_MemoryError,
409 "Can't allocate memory to compress data");
410 return NULL;
412 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
413 self->zst.avail_out = length;
414 length = length << 1;
417 if (err != Z_OK)
419 if (self->zst.msg == Z_NULL)
420 PyErr_Format(ZlibError, "Error %i while compressing",
421 err);
422 else
423 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
424 err, self->zst.msg);
425 Py_DECREF(RetVal);
426 return NULL;
428 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
429 return RetVal;
432 static char decomp_decompress__doc__[] =
433 "decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
434 "After calling this function, some of the input data may still\n"
435 "be stored in internal buffers for later processing.\n"
436 "Call the flush() method to clear these buffers."
439 static PyObject *
440 PyZlib_objdecompress(self, args)
441 compobject *self;
442 PyObject *args;
444 int length, err, inplen;
445 PyObject *RetVal;
446 Byte *input;
447 unsigned long start_total_out;
449 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
450 return NULL;
451 start_total_out = self->zst.total_out;
452 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
453 self->zst.avail_in = inplen;
454 self->zst.next_in = input;
455 self->zst.avail_out = length = DEFAULTALLOC;
456 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
457 err = Z_OK;
459 while (self->zst.avail_in != 0 && err == Z_OK)
461 err = inflate(&(self->zst), Z_NO_FLUSH);
462 if (err == Z_OK && self->zst.avail_out <= 0)
464 if (_PyString_Resize(&RetVal, length << 1) == -1)
466 PyErr_SetString(PyExc_MemoryError,
467 "Can't allocate memory to compress data");
468 return NULL;
470 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
471 self->zst.avail_out = length;
472 length = length << 1;
476 if (err != Z_OK && err != Z_STREAM_END)
478 if (self->zst.msg == Z_NULL)
479 PyErr_Format(ZlibError, "Error %i while decompressing",
480 err);
481 else
482 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
483 err, self->zst.msg);
484 Py_DECREF(RetVal);
485 return NULL;
487 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
488 return RetVal;
491 static char comp_flush__doc__[] =
492 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
493 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
494 "default value used when mode is not specified is Z_FINISH.\n"
495 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
496 "calling the flush() method. Otherwise, more data can still be compressed.\n"
499 static PyObject *
500 PyZlib_flush(self, args)
501 compobject *self;
502 PyObject *args;
504 int length=DEFAULTALLOC, err = Z_OK;
505 PyObject *RetVal;
506 int flushmode = Z_FINISH;
507 unsigned long start_total_out;
509 if (!PyArg_ParseTuple(args, "|i", &flushmode))
510 return NULL;
512 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
513 doing any work at all; just return an empty string. */
514 if (flushmode == Z_NO_FLUSH)
516 return PyString_FromStringAndSize(NULL, 0);
519 self->zst.avail_in = 0;
520 self->zst.next_in = Z_NULL;
521 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
522 PyErr_SetString(PyExc_MemoryError,
523 "Can't allocate memory to compress data");
524 return NULL;
526 start_total_out = self->zst.total_out;
527 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
528 self->zst.avail_out = length;
530 /* When flushing the zstream, there's no input data.
531 If zst.avail_out == 0, that means that more output space is
532 needed to complete the flush operation. */
533 while (err == Z_OK) {
534 err = deflate(&(self->zst), Z_FINISH);
535 if (self->zst.avail_out <= 0) {
536 if (_PyString_Resize(&RetVal, length << 1) == -1) {
537 PyErr_SetString(PyExc_MemoryError,
538 "Can't allocate memory to compress data");
539 return NULL;
541 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
542 self->zst.avail_out = length;
543 length = length << 1;
547 if (err != Z_STREAM_END)
549 if (self->zst.msg == Z_NULL)
550 PyErr_Format(ZlibError, "Error %i while flushing",
551 err);
552 else
553 PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
554 err, self->zst.msg);
555 Py_DECREF(RetVal);
556 return NULL;
558 if (flushmode == Z_FINISH) {
559 err=deflateEnd(&(self->zst));
560 if (err!=Z_OK) {
561 if (self->zst.msg == Z_NULL)
562 PyErr_Format(ZlibError, "Error %i from deflateEnd()",
563 err);
564 else
565 PyErr_Format(ZlibError,
566 "Error %i from deflateEnd(): %.200s",
567 err, self->zst.msg);
568 Py_DECREF(RetVal);
569 return NULL;
572 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
573 return RetVal;
576 static char decomp_flush__doc__[] =
577 "flush() -- Return a string containing any remaining decompressed data. "
578 "The decompressor object can no longer be used after this call."
581 static PyObject *
582 PyZlib_unflush(self, args)
583 compobject *self;
584 PyObject *args;
586 int length=0, err;
587 PyObject *RetVal;
589 if (!PyArg_NoArgs(args))
590 return NULL;
591 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
593 PyErr_SetString(PyExc_MemoryError,
594 "Can't allocate memory to decompress data");
595 return NULL;
597 self->zst.avail_in=0;
598 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
599 length = self->zst.avail_out = DEFAULTALLOC;
601 err = Z_OK;
602 while (err == Z_OK)
604 err = inflate(&(self->zst), Z_FINISH);
605 if (err == Z_OK && self->zst.avail_out == 0)
607 if (_PyString_Resize(&RetVal, length << 1) == -1)
609 PyErr_SetString(PyExc_MemoryError,
610 "Can't allocate memory to decompress data");
611 return NULL;
613 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
614 self->zst.avail_out = length;
615 length = length << 1;
618 if (err!=Z_STREAM_END)
620 if (self->zst.msg == Z_NULL)
621 PyErr_Format(ZlibError, "Error %i while decompressing",
622 err);
623 else
624 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
625 err, self->zst.msg);
626 Py_DECREF(RetVal);
627 return NULL;
629 err=inflateEnd(&(self->zst));
630 if (err!=Z_OK)
632 if (self->zst.msg == Z_NULL)
633 PyErr_Format(ZlibError,
634 "Error %i while flushing decompression object",
635 err);
636 else
637 PyErr_Format(ZlibError,
638 "Error %i while flushing decompression object: %.200s",
639 err, self->zst.msg);
640 Py_DECREF(RetVal);
641 return NULL;
643 _PyString_Resize(&RetVal,
644 (char *)self->zst.next_out - PyString_AsString(RetVal));
645 return RetVal;
648 static PyMethodDef comp_methods[] =
650 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
651 {"flush", (binaryfunc)PyZlib_flush, 1, comp_flush__doc__},
652 {NULL, NULL}
655 static PyMethodDef Decomp_methods[] =
657 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
658 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
659 {NULL, NULL}
662 static PyObject *
663 Comp_getattr(self, name)
664 compobject *self;
665 char *name;
667 return Py_FindMethod(comp_methods, (PyObject *)self, name);
670 static PyObject *
671 Decomp_getattr(self, name)
672 compobject *self;
673 char *name;
675 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
678 static char adler32__doc__[] =
679 "adler32(string) -- Compute an Adler-32 checksum of string, using "
680 "a default starting value, and returning an integer value.\n"
681 "adler32(string, value) -- Compute an Adler-32 checksum of string, using "
682 "the starting value provided, and returning an integer value\n"
685 static PyObject *
686 PyZlib_adler32(self, args)
687 PyObject *self, *args;
689 uLong adler32val=adler32(0L, Z_NULL, 0);
690 Byte *buf;
691 int len;
693 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
695 return NULL;
697 adler32val = adler32(adler32val, buf, len);
698 return PyInt_FromLong(adler32val);
701 static char crc32__doc__[] =
702 "crc32(string) -- Compute a CRC-32 checksum of string, using "
703 "a default starting value, and returning an integer value.\n"
704 "crc32(string, value) -- Compute a CRC-32 checksum of string, using "
705 "the starting value provided, and returning an integer value.\n"
708 static PyObject *
709 PyZlib_crc32(self, args)
710 PyObject *self, *args;
712 uLong crc32val=crc32(0L, Z_NULL, 0);
713 Byte *buf;
714 int len;
715 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
717 return NULL;
719 crc32val = crc32(crc32val, buf, len);
720 return PyInt_FromLong(crc32val);
724 static PyMethodDef zlib_methods[] =
726 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
727 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
728 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
729 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
730 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
731 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
732 {NULL, NULL}
735 statichere PyTypeObject Comptype = {
736 PyObject_HEAD_INIT(0)
738 "Compress",
739 sizeof(compobject),
741 (destructor)Comp_dealloc, /*tp_dealloc*/
742 0, /*tp_print*/
743 (getattrfunc)Comp_getattr, /*tp_getattr*/
744 0, /*tp_setattr*/
745 0, /*tp_compare*/
746 0, /*tp_repr*/
747 0, /*tp_as_number*/
748 0, /*tp_as_sequence*/
749 0, /*tp_as_mapping*/
752 statichere PyTypeObject Decomptype = {
753 PyObject_HEAD_INIT(0)
755 "Decompress",
756 sizeof(compobject),
758 (destructor)Decomp_dealloc, /*tp_dealloc*/
759 0, /*tp_print*/
760 (getattrfunc)Decomp_getattr, /*tp_getattr*/
761 0, /*tp_setattr*/
762 0, /*tp_compare*/
763 0, /*tp_repr*/
764 0, /*tp_as_number*/
765 0, /*tp_as_sequence*/
766 0, /*tp_as_mapping*/
769 /* The following insint() routine was blatantly ripped off from
770 socketmodule.c */
772 /* Convenience routine to export an integer value.
773 For simplicity, errors (which are unlikely anyway) are ignored. */
774 static void
775 insint(d, name, value)
776 PyObject *d;
777 char *name;
778 int value;
780 PyObject *v = PyInt_FromLong((long) value);
781 if (v == NULL) {
782 /* Don't bother reporting this error */
783 PyErr_Clear();
785 else {
786 PyDict_SetItemString(d, name, v);
787 Py_DECREF(v);
791 static char zlib_module_documentation[]=
792 "The functions in this module allow compression and decompression "
793 "using the zlib library, which is based on GNU zip. \n\n"
794 "adler32(string) -- Compute an Adler-32 checksum.\n"
795 "adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
796 "compress(string) -- Compress a string.\n"
797 "compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
798 "compressobj([level]) -- Return a compressor object.\n"
799 "crc32(string) -- Compute a CRC-32 checksum.\n"
800 "crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
801 "decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n"
802 "decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
803 "Compressor objects support compress() and flush() methods; decompressor \n"
804 "objects support decompress() and flush()."
807 DL_EXPORT(void)
808 PyInit_zlib()
810 PyObject *m, *d, *ver;
811 Comptype.ob_type = &PyType_Type;
812 Decomptype.ob_type = &PyType_Type;
813 m = Py_InitModule4("zlib", zlib_methods,
814 zlib_module_documentation,
815 (PyObject*)NULL,PYTHON_API_VERSION);
816 d = PyModule_GetDict(m);
817 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
818 PyDict_SetItemString(d, "error", ZlibError);
820 insint(d, "MAX_WBITS", MAX_WBITS);
821 insint(d, "DEFLATED", DEFLATED);
822 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
823 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
824 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
825 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
826 insint(d, "Z_FILTERED", Z_FILTERED);
827 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
828 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
830 insint(d, "Z_FINISH", Z_FINISH);
831 insint(d, "Z_NO_FLUSH", Z_NO_FLUSH);
832 insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
833 insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH);
835 ver = PyString_FromString(ZLIB_VERSION);
836 PyDict_SetItemString(d, "ZLIB_VERSION", ver);