1 /* zlibmodule.c -- gzip-compatible data compression */
2 /* See http://www.gzip.org/zlib/ */
4 /* Windows users: read Python's PCbuild\readme.txt */
13 /* #defs ripped off from _tkinter.c, even though the situation here is much
14 simpler, because we don't have to worry about waiting for Tcl
15 events! And, since zlib itself is threadsafe, we don't need to worry
16 about re-entering zlib functions.
20 Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
21 that modify the components of preexisting de/compress objects, it
22 could prove to be a performance gain on multiprocessor machines if
23 there was an de/compress object-specific lock. However, for the
24 moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
28 static PyThread_type_lock zlib_lock
= NULL
; /* initialized on module load */
31 Py_BEGIN_ALLOW_THREADS \
32 PyThread_acquire_lock(zlib_lock, 1); \
36 PyThread_release_lock(zlib_lock);
45 /* The following parameters are copied from zutil.h, version 0.95 */
47 #if MAX_MEM_LEVEL >= 8
48 # define DEF_MEM_LEVEL 8
50 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
52 #define DEF_WBITS MAX_WBITS
54 /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
55 #define DEFAULTALLOC (16*1024)
56 #define PyInit_zlib initzlib
58 static PyTypeObject Comptype
;
59 static PyTypeObject Decomptype
;
61 static PyObject
*ZlibError
;
67 PyObject
*unused_data
;
68 PyObject
*unconsumed_tail
;
73 zlib_error(z_stream zst
, int err
, char *msg
)
75 if (zst
.msg
== Z_NULL
)
76 PyErr_Format(ZlibError
, "Error %d %s", err
, msg
);
78 PyErr_Format(ZlibError
, "Error %d %s: %.200s", err
, msg
, zst
.msg
);
81 PyDoc_STRVAR(compressobj__doc__
,
82 "compressobj([level]) -- Return a compressor object.\n"
84 "Optional arg level is the compression level, in 1-9.");
86 PyDoc_STRVAR(decompressobj__doc__
,
87 "decompressobj([wbits]) -- Return a decompressor object.\n"
89 "Optional arg wbits is the window buffer size.");
92 newcompobject(PyTypeObject
*type
)
95 self
= PyObject_New(compobject
, type
);
98 self
->is_initialised
= 0;
99 self
->unused_data
= PyString_FromString("");
100 if (self
->unused_data
== NULL
) {
104 self
->unconsumed_tail
= PyString_FromString("");
105 if (self
->unconsumed_tail
== NULL
) {
112 PyDoc_STRVAR(compress__doc__
,
113 "compress(string[, level]) -- Returned compressed string.\n"
115 "Optional arg level is the compression level, in 1-9.");
118 PyZlib_compress(PyObject
*self
, PyObject
*args
)
120 PyObject
*ReturnVal
= NULL
;
121 Byte
*input
, *output
;
122 int length
, level
=Z_DEFAULT_COMPRESSION
, err
;
125 /* require Python string object, optional 'level' arg */
126 if (!PyArg_ParseTuple(args
, "s#|i:compress", &input
, &length
, &level
))
129 zst
.avail_out
= length
+ length
/1000 + 12 + 1;
131 output
= (Byte
*)malloc(zst
.avail_out
);
132 if (output
== NULL
) {
133 PyErr_SetString(PyExc_MemoryError
,
134 "Can't allocate memory to compress data");
138 /* Past the point of no return. From here on out, we need to make sure
139 we clean up mallocs & INCREFs. */
141 zst
.zalloc
= (alloc_func
)NULL
;
142 zst
.zfree
= (free_func
)Z_NULL
;
143 zst
.next_out
= (Byte
*)output
;
144 zst
.next_in
= (Byte
*)input
;
145 zst
.avail_in
= length
;
146 err
= deflateInit(&zst
, level
);
152 PyErr_SetString(PyExc_MemoryError
,
153 "Out of memory while compressing data");
155 case(Z_STREAM_ERROR
):
156 PyErr_SetString(ZlibError
,
157 "Bad compression level");
161 zlib_error(zst
, err
, "while compressing data");
165 Py_BEGIN_ALLOW_THREADS
;
166 err
= deflate(&zst
, Z_FINISH
);
167 Py_END_ALLOW_THREADS
;
169 if (err
!= Z_STREAM_END
) {
170 zlib_error(zst
, err
, "while compressing data");
175 err
=deflateEnd(&zst
);
177 ReturnVal
= PyString_FromStringAndSize((char *)output
,
180 zlib_error(zst
, err
, "while finishing compression");
188 PyDoc_STRVAR(decompress__doc__
,
189 "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
191 "Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
192 "the initial output buffer size.");
195 PyZlib_decompress(PyObject
*self
, PyObject
*args
)
197 PyObject
*result_str
;
201 Py_ssize_t r_strlen
=DEFAULTALLOC
;
204 if (!PyArg_ParseTuple(args
, "s#|in:decompress",
205 &input
, &length
, &wsize
, &r_strlen
))
211 zst
.avail_in
= length
;
212 zst
.avail_out
= r_strlen
;
214 if (!(result_str
= PyString_FromStringAndSize(NULL
, r_strlen
)))
217 zst
.zalloc
= (alloc_func
)NULL
;
218 zst
.zfree
= (free_func
)Z_NULL
;
219 zst
.next_out
= (Byte
*)PyString_AS_STRING(result_str
);
220 zst
.next_in
= (Byte
*)input
;
221 err
= inflateInit2(&zst
, wsize
);
227 PyErr_SetString(PyExc_MemoryError
,
228 "Out of memory while decompressing data");
232 zlib_error(zst
, err
, "while preparing to decompress data");
237 Py_BEGIN_ALLOW_THREADS
238 err
=inflate(&zst
, Z_FINISH
);
246 * If there is at least 1 byte of room according to zst.avail_out
247 * and we get this error, assume that it means zlib cannot
248 * process the inflate call() due to an error in the data.
250 if (zst
.avail_out
> 0) {
251 PyErr_Format(ZlibError
, "Error %i while decompressing data",
258 /* need more memory */
259 if (_PyString_Resize(&result_str
, r_strlen
<< 1) < 0) {
263 zst
.next_out
= (unsigned char *)PyString_AS_STRING(result_str
) \
265 zst
.avail_out
= r_strlen
;
266 r_strlen
= r_strlen
<< 1;
270 zlib_error(zst
, err
, "while decompressing data");
273 } while (err
!= Z_STREAM_END
);
275 err
= inflateEnd(&zst
);
277 zlib_error(zst
, err
, "while finishing data decompression");
281 _PyString_Resize(&result_str
, zst
.total_out
);
285 Py_XDECREF(result_str
);
290 PyZlib_compressobj(PyObject
*selfptr
, PyObject
*args
)
293 int level
=Z_DEFAULT_COMPRESSION
, method
=DEFLATED
;
294 int wbits
=MAX_WBITS
, memLevel
=DEF_MEM_LEVEL
, strategy
=0, err
;
296 if (!PyArg_ParseTuple(args
, "|iiiii:compressobj", &level
, &method
, &wbits
,
297 &memLevel
, &strategy
))
300 self
= newcompobject(&Comptype
);
303 self
->zst
.zalloc
= (alloc_func
)NULL
;
304 self
->zst
.zfree
= (free_func
)Z_NULL
;
305 self
->zst
.next_in
= NULL
;
306 self
->zst
.avail_in
= 0;
307 err
= deflateInit2(&self
->zst
, level
, method
, wbits
, memLevel
, strategy
);
310 self
->is_initialised
= 1;
311 return (PyObject
*)self
;
314 PyErr_SetString(PyExc_MemoryError
,
315 "Can't allocate memory for compression object");
317 case(Z_STREAM_ERROR
):
319 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
322 zlib_error(self
->zst
, err
, "while creating compression object");
329 PyZlib_decompressobj(PyObject
*selfptr
, PyObject
*args
)
331 int wbits
=DEF_WBITS
, err
;
333 if (!PyArg_ParseTuple(args
, "|i:decompressobj", &wbits
))
336 self
= newcompobject(&Decomptype
);
339 self
->zst
.zalloc
= (alloc_func
)NULL
;
340 self
->zst
.zfree
= (free_func
)Z_NULL
;
341 self
->zst
.next_in
= NULL
;
342 self
->zst
.avail_in
= 0;
343 err
= inflateInit2(&self
->zst
, wbits
);
346 self
->is_initialised
= 1;
347 return (PyObject
*)self
;
348 case(Z_STREAM_ERROR
):
350 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
354 PyErr_SetString(PyExc_MemoryError
,
355 "Can't allocate memory for decompression object");
358 zlib_error(self
->zst
, err
, "while creating decompression object");
365 Comp_dealloc(compobject
*self
)
367 if (self
->is_initialised
)
368 deflateEnd(&self
->zst
);
369 Py_XDECREF(self
->unused_data
);
370 Py_XDECREF(self
->unconsumed_tail
);
375 Decomp_dealloc(compobject
*self
)
377 if (self
->is_initialised
)
378 inflateEnd(&self
->zst
);
379 Py_XDECREF(self
->unused_data
);
380 Py_XDECREF(self
->unconsumed_tail
);
384 PyDoc_STRVAR(comp_compress__doc__
,
385 "compress(data) -- Return a string containing data compressed.\n"
387 "After calling this function, some of the input data may still\n"
388 "be stored in internal buffers for later processing.\n"
389 "Call the flush() method to clear these buffers.");
393 PyZlib_objcompress(compobject
*self
, PyObject
*args
)
395 int err
, inplen
, length
= DEFAULTALLOC
;
398 unsigned long start_total_out
;
400 if (!PyArg_ParseTuple(args
, "s#:compress", &input
, &inplen
))
403 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
)))
408 start_total_out
= self
->zst
.total_out
;
409 self
->zst
.avail_in
= inplen
;
410 self
->zst
.next_in
= input
;
411 self
->zst
.avail_out
= length
;
412 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
);
414 Py_BEGIN_ALLOW_THREADS
415 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
418 /* while Z_OK and the output buffer is full, there might be more output,
419 so extend the output buffer and try again */
420 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
421 if (_PyString_Resize(&RetVal
, length
<< 1) < 0)
423 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
) \
425 self
->zst
.avail_out
= length
;
426 length
= length
<< 1;
428 Py_BEGIN_ALLOW_THREADS
429 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
432 /* We will only get Z_BUF_ERROR if the output buffer was full but
433 there wasn't more output when we tried again, so it is not an error
437 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
438 zlib_error(self
->zst
, err
, "while compressing");
443 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
450 PyDoc_STRVAR(decomp_decompress__doc__
,
451 "decompress(data, max_length) -- Return a string containing the decompressed\n"
452 "version of the data.\n"
454 "After calling this function, some of the input data may still be stored in\n"
455 "internal buffers for later processing.\n"
456 "Call the flush() method to clear these buffers.\n"
457 "If the max_length parameter is specified then the return value will be\n"
458 "no longer than max_length. Unconsumed input data will be stored in\n"
459 "the unconsumed_tail attribute.");
462 PyZlib_objdecompress(compobject
*self
, PyObject
*args
)
464 int err
, inplen
, old_length
, length
= DEFAULTALLOC
;
468 unsigned long start_total_out
;
470 if (!PyArg_ParseTuple(args
, "s#|i:decompress", &input
,
471 &inplen
, &max_length
))
473 if (max_length
< 0) {
474 PyErr_SetString(PyExc_ValueError
,
475 "max_length must be greater than zero");
479 /* limit amount of data allocated to max_length */
480 if (max_length
&& length
> max_length
)
482 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
)))
487 start_total_out
= self
->zst
.total_out
;
488 self
->zst
.avail_in
= inplen
;
489 self
->zst
.next_in
= input
;
490 self
->zst
.avail_out
= length
;
491 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
);
493 Py_BEGIN_ALLOW_THREADS
494 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
497 /* While Z_OK and the output buffer is full, there might be more output.
498 So extend the output buffer and try again.
500 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
501 /* If max_length set, don't continue decompressing if we've already
504 if (max_length
&& length
>= max_length
)
509 length
= length
<< 1;
510 if (max_length
&& length
> max_length
)
513 if (_PyString_Resize(&RetVal
, length
) < 0)
515 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
) \
517 self
->zst
.avail_out
= length
- old_length
;
519 Py_BEGIN_ALLOW_THREADS
520 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
524 /* Not all of the compressed data could be accommodated in the output buffer
525 of specified size. Return the unconsumed tail in an attribute.*/
527 Py_DECREF(self
->unconsumed_tail
);
528 self
->unconsumed_tail
= PyString_FromStringAndSize((char *)self
->zst
.next_in
,
530 if(!self
->unconsumed_tail
) {
537 /* The end of the compressed data has been reached, so set the
538 unused_data attribute to a string containing the remainder of the
539 data in the string. Note that this is also a logical place to call
540 inflateEnd, but the old behaviour of only calling it on flush() is
543 if (err
== Z_STREAM_END
) {
544 Py_XDECREF(self
->unused_data
); /* Free original empty string */
545 self
->unused_data
= PyString_FromStringAndSize(
546 (char *)self
->zst
.next_in
, self
->zst
.avail_in
);
547 if (self
->unused_data
== NULL
) {
551 /* We will only get Z_BUF_ERROR if the output buffer was full
552 but there wasn't more output when we tried again, so it is
553 not an error condition.
555 } else if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
556 zlib_error(self
->zst
, err
, "while decompressing");
562 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
570 PyDoc_STRVAR(comp_flush__doc__
,
571 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
573 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
574 "default value used when mode is not specified is Z_FINISH.\n"
575 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
576 "calling the flush() method. Otherwise, more data can still be compressed.");
579 PyZlib_flush(compobject
*self
, PyObject
*args
)
581 int err
, length
= DEFAULTALLOC
;
583 int flushmode
= Z_FINISH
;
584 unsigned long start_total_out
;
586 if (!PyArg_ParseTuple(args
, "|i:flush", &flushmode
))
589 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
590 doing any work at all; just return an empty string. */
591 if (flushmode
== Z_NO_FLUSH
) {
592 return PyString_FromStringAndSize(NULL
, 0);
595 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
)))
600 start_total_out
= self
->zst
.total_out
;
601 self
->zst
.avail_in
= 0;
602 self
->zst
.avail_out
= length
;
603 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
);
605 Py_BEGIN_ALLOW_THREADS
606 err
= deflate(&(self
->zst
), flushmode
);
609 /* while Z_OK and the output buffer is full, there might be more output,
610 so extend the output buffer and try again */
611 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
612 if (_PyString_Resize(&RetVal
, length
<< 1) < 0)
614 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
) \
616 self
->zst
.avail_out
= length
;
617 length
= length
<< 1;
619 Py_BEGIN_ALLOW_THREADS
620 err
= deflate(&(self
->zst
), flushmode
);
624 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
625 various data structures. Note we should only get Z_STREAM_END when
626 flushmode is Z_FINISH, but checking both for safety*/
627 if (err
== Z_STREAM_END
&& flushmode
== Z_FINISH
) {
628 err
= deflateEnd(&(self
->zst
));
630 zlib_error(self
->zst
, err
, "from deflateEnd()");
636 self
->is_initialised
= 0;
638 /* We will only get Z_BUF_ERROR if the output buffer was full
639 but there wasn't more output when we tried again, so it is
640 not an error condition.
642 } else if (err
!=Z_OK
&& err
!=Z_BUF_ERROR
) {
643 zlib_error(self
->zst
, err
, "while flushing");
649 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
657 #ifdef HAVE_ZLIB_COPY
658 PyDoc_STRVAR(comp_copy__doc__
,
659 "copy() -- Return a copy of the compression object.");
662 PyZlib_copy(compobject
*self
)
664 compobject
*retval
= NULL
;
667 retval
= newcompobject(&Comptype
);
668 if (!retval
) return NULL
;
670 /* Copy the zstream state
671 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
674 err
= deflateCopy(&retval
->zst
, &self
->zst
);
678 case(Z_STREAM_ERROR
):
679 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
682 PyErr_SetString(PyExc_MemoryError
,
683 "Can't allocate memory for compression object");
686 zlib_error(self
->zst
, err
, "while copying compression object");
690 Py_INCREF(self
->unused_data
);
691 Py_INCREF(self
->unconsumed_tail
);
692 Py_XDECREF(retval
->unused_data
);
693 Py_XDECREF(retval
->unconsumed_tail
);
694 retval
->unused_data
= self
->unused_data
;
695 retval
->unconsumed_tail
= self
->unconsumed_tail
;
697 /* Mark it as being initialized */
698 retval
->is_initialised
= 1;
701 return (PyObject
*)retval
;
709 PyDoc_STRVAR(decomp_copy__doc__
,
710 "copy() -- Return a copy of the decompression object.");
713 PyZlib_uncopy(compobject
*self
)
715 compobject
*retval
= NULL
;
718 retval
= newcompobject(&Decomptype
);
719 if (!retval
) return NULL
;
721 /* Copy the zstream state
722 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
725 err
= inflateCopy(&retval
->zst
, &self
->zst
);
729 case(Z_STREAM_ERROR
):
730 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
733 PyErr_SetString(PyExc_MemoryError
,
734 "Can't allocate memory for decompression object");
737 zlib_error(self
->zst
, err
, "while copying decompression object");
741 Py_INCREF(self
->unused_data
);
742 Py_INCREF(self
->unconsumed_tail
);
743 Py_XDECREF(retval
->unused_data
);
744 Py_XDECREF(retval
->unconsumed_tail
);
745 retval
->unused_data
= self
->unused_data
;
746 retval
->unconsumed_tail
= self
->unconsumed_tail
;
748 /* Mark it as being initialized */
749 retval
->is_initialised
= 1;
752 return (PyObject
*)retval
;
761 PyDoc_STRVAR(decomp_flush__doc__
,
762 "flush( [length] ) -- Return a string containing any remaining\n"
763 "decompressed data. length, if given, is the initial size of the\n"
766 "The decompressor object can no longer be used after this call.");
769 PyZlib_unflush(compobject
*self
, PyObject
*args
)
771 int err
, length
= DEFAULTALLOC
;
772 PyObject
* retval
= NULL
;
773 unsigned long start_total_out
;
775 if (!PyArg_ParseTuple(args
, "|i:flush", &length
))
777 if (!(retval
= PyString_FromStringAndSize(NULL
, length
)))
783 start_total_out
= self
->zst
.total_out
;
784 self
->zst
.avail_out
= length
;
785 self
->zst
.next_out
= (Byte
*)PyString_AS_STRING(retval
);
787 Py_BEGIN_ALLOW_THREADS
788 err
= inflate(&(self
->zst
), Z_FINISH
);
791 /* while Z_OK and the output buffer is full, there might be more output,
792 so extend the output buffer and try again */
793 while ((err
== Z_OK
|| err
== Z_BUF_ERROR
) && self
->zst
.avail_out
== 0) {
794 if (_PyString_Resize(&retval
, length
<< 1) < 0)
796 self
->zst
.next_out
= (Byte
*)PyString_AS_STRING(retval
) + length
;
797 self
->zst
.avail_out
= length
;
798 length
= length
<< 1;
800 Py_BEGIN_ALLOW_THREADS
801 err
= inflate(&(self
->zst
), Z_FINISH
);
805 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
806 various data structures. Note we should only get Z_STREAM_END when
807 flushmode is Z_FINISH */
808 if (err
== Z_STREAM_END
) {
809 err
= inflateEnd(&(self
->zst
));
810 self
->is_initialised
= 0;
812 zlib_error(self
->zst
, err
, "from inflateEnd()");
818 _PyString_Resize(&retval
, self
->zst
.total_out
- start_total_out
);
827 static PyMethodDef comp_methods
[] =
829 {"compress", (binaryfunc
)PyZlib_objcompress
, METH_VARARGS
,
830 comp_compress__doc__
},
831 {"flush", (binaryfunc
)PyZlib_flush
, METH_VARARGS
,
833 #ifdef HAVE_ZLIB_COPY
834 {"copy", (PyCFunction
)PyZlib_copy
, METH_NOARGS
,
840 static PyMethodDef Decomp_methods
[] =
842 {"decompress", (binaryfunc
)PyZlib_objdecompress
, METH_VARARGS
,
843 decomp_decompress__doc__
},
844 {"flush", (binaryfunc
)PyZlib_unflush
, METH_VARARGS
,
845 decomp_flush__doc__
},
846 #ifdef HAVE_ZLIB_COPY
847 {"copy", (PyCFunction
)PyZlib_uncopy
, METH_NOARGS
,
854 Comp_getattr(compobject
*self
, char *name
)
856 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
859 return Py_FindMethod(comp_methods
, (PyObject
*)self
, name
);
863 Decomp_getattr(compobject
*self
, char *name
)
869 if (strcmp(name
, "unused_data") == 0) {
870 Py_INCREF(self
->unused_data
);
871 retval
= self
->unused_data
;
872 } else if (strcmp(name
, "unconsumed_tail") == 0) {
873 Py_INCREF(self
->unconsumed_tail
);
874 retval
= self
->unconsumed_tail
;
876 retval
= Py_FindMethod(Decomp_methods
, (PyObject
*)self
, name
);
883 PyDoc_STRVAR(adler32__doc__
,
884 "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
886 "An optional starting value can be specified. The returned checksum is\n"
890 PyZlib_adler32(PyObject
*self
, PyObject
*args
)
892 uLong adler32val
= adler32(0L, Z_NULL
, 0);
896 if (!PyArg_ParseTuple(args
, "s#|k:adler32", &buf
, &len
, &adler32val
))
898 adler32val
= adler32(adler32val
, buf
, len
);
899 return PyInt_FromLong(adler32val
);
902 PyDoc_STRVAR(crc32__doc__
,
903 "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
905 "An optional starting value can be specified. The returned checksum is\n"
909 PyZlib_crc32(PyObject
*self
, PyObject
*args
)
911 uLong crc32val
= crc32(0L, Z_NULL
, 0);
914 if (!PyArg_ParseTuple(args
, "s#|k:crc32", &buf
, &len
, &crc32val
))
916 crc32val
= crc32(crc32val
, buf
, len
);
917 return PyInt_FromLong(crc32val
);
921 static PyMethodDef zlib_methods
[] =
923 {"adler32", (PyCFunction
)PyZlib_adler32
, METH_VARARGS
,
925 {"compress", (PyCFunction
)PyZlib_compress
, METH_VARARGS
,
927 {"compressobj", (PyCFunction
)PyZlib_compressobj
, METH_VARARGS
,
929 {"crc32", (PyCFunction
)PyZlib_crc32
, METH_VARARGS
,
931 {"decompress", (PyCFunction
)PyZlib_decompress
, METH_VARARGS
,
933 {"decompressobj", (PyCFunction
)PyZlib_decompressobj
, METH_VARARGS
,
934 decompressobj__doc__
},
938 static PyTypeObject Comptype
= {
939 PyVarObject_HEAD_INIT(0, 0)
943 (destructor
)Comp_dealloc
, /*tp_dealloc*/
945 (getattrfunc
)Comp_getattr
, /*tp_getattr*/
950 0, /*tp_as_sequence*/
954 static PyTypeObject Decomptype
= {
955 PyVarObject_HEAD_INIT(0, 0)
959 (destructor
)Decomp_dealloc
, /*tp_dealloc*/
961 (getattrfunc
)Decomp_getattr
, /*tp_getattr*/
966 0, /*tp_as_sequence*/
970 PyDoc_STRVAR(zlib_module_documentation
,
971 "The functions in this module allow compression and decompression using the\n"
972 "zlib library, which is based on GNU zip.\n"
974 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
975 "compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
976 "compressobj([level]) -- Return a compressor object.\n"
977 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
978 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
979 "decompressobj([wbits]) -- Return a decompressor object.\n"
981 "'wbits' is window buffer size.\n"
982 "Compressor objects support compress() and flush() methods; decompressor\n"
983 "objects support decompress() and flush().");
989 Py_TYPE(&Comptype
) = &PyType_Type
;
990 Py_TYPE(&Decomptype
) = &PyType_Type
;
991 m
= Py_InitModule4("zlib", zlib_methods
,
992 zlib_module_documentation
,
993 (PyObject
*)NULL
,PYTHON_API_VERSION
);
997 ZlibError
= PyErr_NewException("zlib.error", NULL
, NULL
);
998 if (ZlibError
!= NULL
) {
999 Py_INCREF(ZlibError
);
1000 PyModule_AddObject(m
, "error", ZlibError
);
1002 PyModule_AddIntConstant(m
, "MAX_WBITS", MAX_WBITS
);
1003 PyModule_AddIntConstant(m
, "DEFLATED", DEFLATED
);
1004 PyModule_AddIntConstant(m
, "DEF_MEM_LEVEL", DEF_MEM_LEVEL
);
1005 PyModule_AddIntConstant(m
, "Z_BEST_SPEED", Z_BEST_SPEED
);
1006 PyModule_AddIntConstant(m
, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION
);
1007 PyModule_AddIntConstant(m
, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION
);
1008 PyModule_AddIntConstant(m
, "Z_FILTERED", Z_FILTERED
);
1009 PyModule_AddIntConstant(m
, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY
);
1010 PyModule_AddIntConstant(m
, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY
);
1012 PyModule_AddIntConstant(m
, "Z_FINISH", Z_FINISH
);
1013 PyModule_AddIntConstant(m
, "Z_NO_FLUSH", Z_NO_FLUSH
);
1014 PyModule_AddIntConstant(m
, "Z_SYNC_FLUSH", Z_SYNC_FLUSH
);
1015 PyModule_AddIntConstant(m
, "Z_FULL_FLUSH", Z_FULL_FLUSH
);
1017 ver
= PyString_FromString(ZLIB_VERSION
);
1019 PyModule_AddObject(m
, "ZLIB_VERSION", ver
);
1021 PyModule_AddStringConstant(m
, "__version__", "1.0");
1024 zlib_lock
= PyThread_allocate_lock();
1025 #endif /* WITH_THREAD */