1 /* zlibmodule.c -- gzip-compatible data compression */
2 /* See http://www.gzip.org/zlib/ */
4 /* Windows users: read Python's PCbuild\readme.txt */
8 #include "structmember.h"
13 #define ENTER_ZLIB(obj) \
14 Py_BEGIN_ALLOW_THREADS; \
15 PyThread_acquire_lock((obj)->lock, 1); \
17 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
19 #define ENTER_ZLIB(obj)
20 #define LEAVE_ZLIB(obj)
23 /* The following parameters are copied from zutil.h, version 0.95 */
25 #if MAX_MEM_LEVEL >= 8
26 # define DEF_MEM_LEVEL 8
28 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
30 #define DEF_WBITS MAX_WBITS
32 /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
33 #define DEFAULTALLOC (16*1024)
35 static PyTypeObject Comptype
;
36 static PyTypeObject Decomptype
;
38 static PyObject
*ZlibError
;
44 PyObject
*unused_data
;
45 PyObject
*unconsumed_tail
;
48 PyThread_type_lock lock
;
53 zlib_error(z_stream zst
, int err
, char *msg
)
55 const char *zmsg
= zst
.msg
;
59 zmsg
= "incomplete or truncated stream";
62 zmsg
= "inconsistent stream state";
65 zmsg
= "invalid input data";
70 PyErr_Format(ZlibError
, "Error %d %s", err
, msg
);
72 PyErr_Format(ZlibError
, "Error %d %s: %.200s", err
, msg
, zmsg
);
75 PyDoc_STRVAR(compressobj__doc__
,
76 "compressobj([level]) -- Return a compressor object.\n"
78 "Optional arg level is the compression level, in 1-9.");
80 PyDoc_STRVAR(decompressobj__doc__
,
81 "decompressobj([wbits]) -- Return a decompressor object.\n"
83 "Optional arg wbits is the window buffer size.");
86 newcompobject(PyTypeObject
*type
)
89 self
= PyObject_New(compobject
, type
);
92 self
->is_initialised
= 0;
93 self
->unused_data
= PyBytes_FromStringAndSize("", 0);
94 if (self
->unused_data
== NULL
) {
98 self
->unconsumed_tail
= PyBytes_FromStringAndSize("", 0);
99 if (self
->unconsumed_tail
== NULL
) {
104 self
->lock
= PyThread_allocate_lock();
109 PyDoc_STRVAR(compress__doc__
,
110 "compress(string[, level]) -- Returned compressed string.\n"
112 "Optional arg level is the compression level, in 1-9.");
115 PyZlib_compress(PyObject
*self
, PyObject
*args
)
117 PyObject
*ReturnVal
= NULL
;
119 Byte
*input
, *output
;
120 int length
, level
=Z_DEFAULT_COMPRESSION
, err
;
123 /* require Python string object, optional 'level' arg */
124 if (!PyArg_ParseTuple(args
, "y*|i:compress", &pinput
, &level
))
129 zst
.avail_out
= length
+ length
/1000 + 12 + 1;
131 output
= (Byte
*)malloc(zst
.avail_out
);
132 if (output
== NULL
) {
133 PyBuffer_Release(&pinput
);
134 PyErr_SetString(PyExc_MemoryError
,
135 "Can't allocate memory to compress data");
139 /* Past the point of no return. From here on out, we need to make sure
140 we clean up mallocs & INCREFs. */
142 zst
.zalloc
= (alloc_func
)NULL
;
143 zst
.zfree
= (free_func
)Z_NULL
;
144 zst
.next_out
= (Byte
*)output
;
145 zst
.next_in
= (Byte
*)input
;
146 zst
.avail_in
= length
;
147 err
= deflateInit(&zst
, level
);
153 PyErr_SetString(PyExc_MemoryError
,
154 "Out of memory while compressing data");
156 case(Z_STREAM_ERROR
):
157 PyErr_SetString(ZlibError
,
158 "Bad compression level");
162 zlib_error(zst
, err
, "while compressing data");
166 Py_BEGIN_ALLOW_THREADS
;
167 err
= deflate(&zst
, Z_FINISH
);
168 Py_END_ALLOW_THREADS
;
170 if (err
!= Z_STREAM_END
) {
171 zlib_error(zst
, err
, "while compressing data");
176 err
=deflateEnd(&zst
);
178 ReturnVal
= PyBytes_FromStringAndSize((char *)output
,
181 zlib_error(zst
, err
, "while finishing compression");
184 PyBuffer_Release(&pinput
);
190 PyDoc_STRVAR(decompress__doc__
,
191 "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
193 "Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
194 "the initial output buffer size.");
197 PyZlib_decompress(PyObject
*self
, PyObject
*args
)
199 PyObject
*result_str
;
204 Py_ssize_t r_strlen
=DEFAULTALLOC
;
207 if (!PyArg_ParseTuple(args
, "y*|in:decompress",
208 &pinput
, &wsize
, &r_strlen
))
216 zst
.avail_in
= length
;
217 zst
.avail_out
= r_strlen
;
219 if (!(result_str
= PyBytes_FromStringAndSize(NULL
, r_strlen
))) {
220 PyBuffer_Release(&pinput
);
224 zst
.zalloc
= (alloc_func
)NULL
;
225 zst
.zfree
= (free_func
)Z_NULL
;
226 zst
.next_out
= (Byte
*)PyBytes_AS_STRING(result_str
);
227 zst
.next_in
= (Byte
*)input
;
228 err
= inflateInit2(&zst
, wsize
);
234 PyErr_SetString(PyExc_MemoryError
,
235 "Out of memory while decompressing data");
239 zlib_error(zst
, err
, "while preparing to decompress data");
244 Py_BEGIN_ALLOW_THREADS
245 err
=inflate(&zst
, Z_FINISH
);
253 * If there is at least 1 byte of room according to zst.avail_out
254 * and we get this error, assume that it means zlib cannot
255 * process the inflate call() due to an error in the data.
257 if (zst
.avail_out
> 0) {
258 zlib_error(zst
, err
, "while decompressing data");
264 /* need more memory */
265 if (_PyBytes_Resize(&result_str
, r_strlen
<< 1) < 0) {
270 (unsigned char *)PyBytes_AS_STRING(result_str
) + r_strlen
;
271 zst
.avail_out
= r_strlen
;
272 r_strlen
= r_strlen
<< 1;
276 zlib_error(zst
, err
, "while decompressing data");
279 } while (err
!= Z_STREAM_END
);
281 err
= inflateEnd(&zst
);
283 zlib_error(zst
, err
, "while finishing data decompression");
287 if (_PyBytes_Resize(&result_str
, zst
.total_out
) < 0)
290 PyBuffer_Release(&pinput
);
294 PyBuffer_Release(&pinput
);
295 Py_XDECREF(result_str
);
300 PyZlib_compressobj(PyObject
*selfptr
, PyObject
*args
)
303 int level
=Z_DEFAULT_COMPRESSION
, method
=DEFLATED
;
304 int wbits
=MAX_WBITS
, memLevel
=DEF_MEM_LEVEL
, strategy
=0, err
;
306 if (!PyArg_ParseTuple(args
, "|iiiii:compressobj", &level
, &method
, &wbits
,
307 &memLevel
, &strategy
))
310 self
= newcompobject(&Comptype
);
313 self
->zst
.zalloc
= (alloc_func
)NULL
;
314 self
->zst
.zfree
= (free_func
)Z_NULL
;
315 self
->zst
.next_in
= NULL
;
316 self
->zst
.avail_in
= 0;
317 err
= deflateInit2(&self
->zst
, level
, method
, wbits
, memLevel
, strategy
);
320 self
->is_initialised
= 1;
321 return (PyObject
*)self
;
324 PyErr_SetString(PyExc_MemoryError
,
325 "Can't allocate memory for compression object");
327 case(Z_STREAM_ERROR
):
329 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
332 zlib_error(self
->zst
, err
, "while creating compression object");
339 PyZlib_decompressobj(PyObject
*selfptr
, PyObject
*args
)
341 int wbits
=DEF_WBITS
, err
;
343 if (!PyArg_ParseTuple(args
, "|i:decompressobj", &wbits
))
346 self
= newcompobject(&Decomptype
);
349 self
->zst
.zalloc
= (alloc_func
)NULL
;
350 self
->zst
.zfree
= (free_func
)Z_NULL
;
351 self
->zst
.next_in
= NULL
;
352 self
->zst
.avail_in
= 0;
353 err
= inflateInit2(&self
->zst
, wbits
);
356 self
->is_initialised
= 1;
357 return (PyObject
*)self
;
358 case(Z_STREAM_ERROR
):
360 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
364 PyErr_SetString(PyExc_MemoryError
,
365 "Can't allocate memory for decompression object");
368 zlib_error(self
->zst
, err
, "while creating decompression object");
375 Dealloc(compobject
*self
)
378 PyThread_free_lock(self
->lock
);
380 Py_XDECREF(self
->unused_data
);
381 Py_XDECREF(self
->unconsumed_tail
);
386 Comp_dealloc(compobject
*self
)
388 if (self
->is_initialised
)
389 deflateEnd(&self
->zst
);
394 Decomp_dealloc(compobject
*self
)
396 if (self
->is_initialised
)
397 inflateEnd(&self
->zst
);
401 PyDoc_STRVAR(comp_compress__doc__
,
402 "compress(data) -- Return a string containing data compressed.\n"
404 "After calling this function, some of the input data may still\n"
405 "be stored in internal buffers for later processing.\n"
406 "Call the flush() method to clear these buffers.");
410 PyZlib_objcompress(compobject
*self
, PyObject
*args
)
413 Py_ssize_t length
= DEFAULTALLOC
;
417 unsigned long start_total_out
;
419 if (!PyArg_ParseTuple(args
, "y*:compress", &pinput
))
424 if (!(RetVal
= PyBytes_FromStringAndSize(NULL
, length
))) {
425 PyBuffer_Release(&pinput
);
431 start_total_out
= self
->zst
.total_out
;
432 self
->zst
.avail_in
= inplen
;
433 self
->zst
.next_in
= input
;
434 self
->zst
.avail_out
= length
;
435 self
->zst
.next_out
= (unsigned char *)PyBytes_AS_STRING(RetVal
);
437 Py_BEGIN_ALLOW_THREADS
438 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
441 /* while Z_OK and the output buffer is full, there might be more output,
442 so extend the output buffer and try again */
443 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
444 if (_PyBytes_Resize(&RetVal
, length
<< 1) < 0) {
450 (unsigned char *)PyBytes_AS_STRING(RetVal
) + length
;
451 self
->zst
.avail_out
= length
;
452 length
= length
<< 1;
454 Py_BEGIN_ALLOW_THREADS
455 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
458 /* We will only get Z_BUF_ERROR if the output buffer was full but
459 there wasn't more output when we tried again, so it is not an error
463 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
464 zlib_error(self
->zst
, err
, "while compressing");
469 if (_PyBytes_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
) < 0) {
476 PyBuffer_Release(&pinput
);
480 PyDoc_STRVAR(decomp_decompress__doc__
,
481 "decompress(data, max_length) -- Return a string containing the decompressed\n"
482 "version of the data.\n"
484 "After calling this function, some of the input data may still be stored in\n"
485 "internal buffers for later processing.\n"
486 "Call the flush() method to clear these buffers.\n"
487 "If the max_length parameter is specified then the return value will be\n"
488 "no longer than max_length. Unconsumed input data will be stored in\n"
489 "the unconsumed_tail attribute.");
492 PyZlib_objdecompress(compobject
*self
, PyObject
*args
)
494 int err
, inplen
, max_length
= 0;
495 Py_ssize_t old_length
, length
= DEFAULTALLOC
;
499 unsigned long start_total_out
;
501 if (!PyArg_ParseTuple(args
, "y*|i:decompress", &pinput
,
506 if (max_length
< 0) {
507 PyBuffer_Release(&pinput
);
508 PyErr_SetString(PyExc_ValueError
,
509 "max_length must be greater than zero");
513 /* limit amount of data allocated to max_length */
514 if (max_length
&& length
> max_length
)
516 if (!(RetVal
= PyBytes_FromStringAndSize(NULL
, length
))) {
517 PyBuffer_Release(&pinput
);
523 start_total_out
= self
->zst
.total_out
;
524 self
->zst
.avail_in
= inplen
;
525 self
->zst
.next_in
= input
;
526 self
->zst
.avail_out
= length
;
527 self
->zst
.next_out
= (unsigned char *)PyBytes_AS_STRING(RetVal
);
529 Py_BEGIN_ALLOW_THREADS
530 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
533 /* While Z_OK and the output buffer is full, there might be more output.
534 So extend the output buffer and try again.
536 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
537 /* If max_length set, don't continue decompressing if we've already
540 if (max_length
&& length
>= max_length
)
545 length
= length
<< 1;
546 if (max_length
&& length
> max_length
)
549 if (_PyBytes_Resize(&RetVal
, length
) < 0) {
555 (unsigned char *)PyBytes_AS_STRING(RetVal
) + old_length
;
556 self
->zst
.avail_out
= length
- old_length
;
558 Py_BEGIN_ALLOW_THREADS
559 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
563 /* Not all of the compressed data could be accommodated in the output buffer
564 of specified size. Return the unconsumed tail in an attribute.*/
566 Py_DECREF(self
->unconsumed_tail
);
567 self
->unconsumed_tail
= PyBytes_FromStringAndSize((char *)self
->zst
.next_in
,
569 if(!self
->unconsumed_tail
) {
576 /* The end of the compressed data has been reached, so set the
577 unused_data attribute to a string containing the remainder of the
578 data in the string. Note that this is also a logical place to call
579 inflateEnd, but the old behaviour of only calling it on flush() is
582 if (err
== Z_STREAM_END
) {
583 Py_XDECREF(self
->unused_data
); /* Free original empty string */
584 self
->unused_data
= PyBytes_FromStringAndSize(
585 (char *)self
->zst
.next_in
, self
->zst
.avail_in
);
586 if (self
->unused_data
== NULL
) {
590 /* We will only get Z_BUF_ERROR if the output buffer was full
591 but there wasn't more output when we tried again, so it is
592 not an error condition.
594 } else if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
595 zlib_error(self
->zst
, err
, "while decompressing");
601 if (_PyBytes_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
) < 0) {
608 PyBuffer_Release(&pinput
);
612 PyDoc_STRVAR(comp_flush__doc__
,
613 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
615 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
616 "default value used when mode is not specified is Z_FINISH.\n"
617 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
618 "calling the flush() method. Otherwise, more data can still be compressed.");
621 PyZlib_flush(compobject
*self
, PyObject
*args
)
623 int err
, length
= DEFAULTALLOC
;
625 int flushmode
= Z_FINISH
;
626 unsigned long start_total_out
;
628 if (!PyArg_ParseTuple(args
, "|i:flush", &flushmode
))
631 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
632 doing any work at all; just return an empty string. */
633 if (flushmode
== Z_NO_FLUSH
) {
634 return PyBytes_FromStringAndSize(NULL
, 0);
637 if (!(RetVal
= PyBytes_FromStringAndSize(NULL
, length
)))
642 start_total_out
= self
->zst
.total_out
;
643 self
->zst
.avail_in
= 0;
644 self
->zst
.avail_out
= length
;
645 self
->zst
.next_out
= (unsigned char *)PyBytes_AS_STRING(RetVal
);
647 Py_BEGIN_ALLOW_THREADS
648 err
= deflate(&(self
->zst
), flushmode
);
651 /* while Z_OK and the output buffer is full, there might be more output,
652 so extend the output buffer and try again */
653 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
654 if (_PyBytes_Resize(&RetVal
, length
<< 1) < 0) {
660 (unsigned char *)PyBytes_AS_STRING(RetVal
) + length
;
661 self
->zst
.avail_out
= length
;
662 length
= length
<< 1;
664 Py_BEGIN_ALLOW_THREADS
665 err
= deflate(&(self
->zst
), flushmode
);
669 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
670 various data structures. Note we should only get Z_STREAM_END when
671 flushmode is Z_FINISH, but checking both for safety*/
672 if (err
== Z_STREAM_END
&& flushmode
== Z_FINISH
) {
673 err
= deflateEnd(&(self
->zst
));
675 zlib_error(self
->zst
, err
, "from deflateEnd()");
681 self
->is_initialised
= 0;
683 /* We will only get Z_BUF_ERROR if the output buffer was full
684 but there wasn't more output when we tried again, so it is
685 not an error condition.
687 } else if (err
!=Z_OK
&& err
!=Z_BUF_ERROR
) {
688 zlib_error(self
->zst
, err
, "while flushing");
694 if (_PyBytes_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
) < 0) {
705 #ifdef HAVE_ZLIB_COPY
706 PyDoc_STRVAR(comp_copy__doc__
,
707 "copy() -- Return a copy of the compression object.");
710 PyZlib_copy(compobject
*self
)
712 compobject
*retval
= NULL
;
715 retval
= newcompobject(&Comptype
);
716 if (!retval
) return NULL
;
718 /* Copy the zstream state
719 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
722 err
= deflateCopy(&retval
->zst
, &self
->zst
);
726 case(Z_STREAM_ERROR
):
727 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
730 PyErr_SetString(PyExc_MemoryError
,
731 "Can't allocate memory for compression object");
734 zlib_error(self
->zst
, err
, "while copying compression object");
737 Py_INCREF(self
->unused_data
);
738 Py_INCREF(self
->unconsumed_tail
);
739 Py_XDECREF(retval
->unused_data
);
740 Py_XDECREF(retval
->unconsumed_tail
);
741 retval
->unused_data
= self
->unused_data
;
742 retval
->unconsumed_tail
= self
->unconsumed_tail
;
744 /* Mark it as being initialized */
745 retval
->is_initialised
= 1;
748 return (PyObject
*)retval
;
756 PyDoc_STRVAR(decomp_copy__doc__
,
757 "copy() -- Return a copy of the decompression object.");
760 PyZlib_uncopy(compobject
*self
)
762 compobject
*retval
= NULL
;
765 retval
= newcompobject(&Decomptype
);
766 if (!retval
) return NULL
;
768 /* Copy the zstream state
769 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
772 err
= inflateCopy(&retval
->zst
, &self
->zst
);
776 case(Z_STREAM_ERROR
):
777 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
780 PyErr_SetString(PyExc_MemoryError
,
781 "Can't allocate memory for decompression object");
784 zlib_error(self
->zst
, err
, "while copying decompression object");
788 Py_INCREF(self
->unused_data
);
789 Py_INCREF(self
->unconsumed_tail
);
790 Py_XDECREF(retval
->unused_data
);
791 Py_XDECREF(retval
->unconsumed_tail
);
792 retval
->unused_data
= self
->unused_data
;
793 retval
->unconsumed_tail
= self
->unconsumed_tail
;
795 /* Mark it as being initialized */
796 retval
->is_initialised
= 1;
799 return (PyObject
*)retval
;
808 PyDoc_STRVAR(decomp_flush__doc__
,
809 "flush( [length] ) -- Return a string containing any remaining\n"
810 "decompressed data. length, if given, is the initial size of the\n"
813 "The decompressor object can no longer be used after this call.");
816 PyZlib_unflush(compobject
*self
, PyObject
*args
)
818 int err
, length
= DEFAULTALLOC
;
819 PyObject
* retval
= NULL
;
820 unsigned long start_total_out
;
822 if (!PyArg_ParseTuple(args
, "|i:flush", &length
))
825 PyErr_SetString(PyExc_ValueError
, "length must be greater than zero");
828 if (!(retval
= PyBytes_FromStringAndSize(NULL
, length
)))
834 start_total_out
= self
->zst
.total_out
;
835 self
->zst
.avail_out
= length
;
836 self
->zst
.next_out
= (Byte
*)PyBytes_AS_STRING(retval
);
838 Py_BEGIN_ALLOW_THREADS
839 err
= inflate(&(self
->zst
), Z_FINISH
);
842 /* while Z_OK and the output buffer is full, there might be more output,
843 so extend the output buffer and try again */
844 while ((err
== Z_OK
|| err
== Z_BUF_ERROR
) && self
->zst
.avail_out
== 0) {
845 if (_PyBytes_Resize(&retval
, length
<< 1) < 0) {
850 self
->zst
.next_out
= (Byte
*)PyBytes_AS_STRING(retval
) + length
;
851 self
->zst
.avail_out
= length
;
852 length
= length
<< 1;
854 Py_BEGIN_ALLOW_THREADS
855 err
= inflate(&(self
->zst
), Z_FINISH
);
859 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
860 various data structures. Note we should only get Z_STREAM_END when
861 flushmode is Z_FINISH */
862 if (err
== Z_STREAM_END
) {
863 err
= inflateEnd(&(self
->zst
));
864 self
->is_initialised
= 0;
866 zlib_error(self
->zst
, err
, "from inflateEnd()");
872 if (_PyBytes_Resize(&retval
, self
->zst
.total_out
- start_total_out
) < 0) {
884 static PyMethodDef comp_methods
[] =
886 {"compress", (binaryfunc
)PyZlib_objcompress
, METH_VARARGS
,
887 comp_compress__doc__
},
888 {"flush", (binaryfunc
)PyZlib_flush
, METH_VARARGS
,
890 #ifdef HAVE_ZLIB_COPY
891 {"copy", (PyCFunction
)PyZlib_copy
, METH_NOARGS
,
897 static PyMethodDef Decomp_methods
[] =
899 {"decompress", (binaryfunc
)PyZlib_objdecompress
, METH_VARARGS
,
900 decomp_decompress__doc__
},
901 {"flush", (binaryfunc
)PyZlib_unflush
, METH_VARARGS
,
902 decomp_flush__doc__
},
903 #ifdef HAVE_ZLIB_COPY
904 {"copy", (PyCFunction
)PyZlib_uncopy
, METH_NOARGS
,
910 #define COMP_OFF(x) offsetof(compobject, x)
911 static PyMemberDef Decomp_members
[] = {
912 {"unused_data", T_OBJECT
, COMP_OFF(unused_data
), READONLY
},
913 {"unconsumed_tail", T_OBJECT
, COMP_OFF(unconsumed_tail
), READONLY
},
917 PyDoc_STRVAR(adler32__doc__
,
918 "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
920 "An optional starting value can be specified. The returned checksum is\n"
924 PyZlib_adler32(PyObject
*self
, PyObject
*args
)
926 unsigned int adler32val
= 1; /* adler32(0L, Z_NULL, 0) */
929 if (!PyArg_ParseTuple(args
, "y*|I:adler32", &pbuf
, &adler32val
))
931 /* Releasing the GIL for very small buffers is inefficient
932 and may lower performance */
933 if (pbuf
.len
> 1024*5) {
934 Py_BEGIN_ALLOW_THREADS
935 adler32val
= adler32(adler32val
, pbuf
.buf
, pbuf
.len
);
938 adler32val
= adler32(adler32val
, pbuf
.buf
, pbuf
.len
);
940 PyBuffer_Release(&pbuf
);
941 return PyLong_FromUnsignedLong(adler32val
& 0xffffffffU
);
944 PyDoc_STRVAR(crc32__doc__
,
945 "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
947 "An optional starting value can be specified. The returned checksum is\n"
951 PyZlib_crc32(PyObject
*self
, PyObject
*args
)
953 unsigned int crc32val
= 0; /* crc32(0L, Z_NULL, 0) */
957 if (!PyArg_ParseTuple(args
, "y*|I:crc32", &pbuf
, &crc32val
))
959 /* Releasing the GIL for very small buffers is inefficient
960 and may lower performance */
961 if (pbuf
.len
> 1024*5) {
962 Py_BEGIN_ALLOW_THREADS
963 signed_val
= crc32(crc32val
, pbuf
.buf
, pbuf
.len
);
966 signed_val
= crc32(crc32val
, pbuf
.buf
, pbuf
.len
);
968 PyBuffer_Release(&pbuf
);
969 return PyLong_FromUnsignedLong(signed_val
& 0xffffffffU
);
973 static PyMethodDef zlib_methods
[] =
975 {"adler32", (PyCFunction
)PyZlib_adler32
, METH_VARARGS
,
977 {"compress", (PyCFunction
)PyZlib_compress
, METH_VARARGS
,
979 {"compressobj", (PyCFunction
)PyZlib_compressobj
, METH_VARARGS
,
981 {"crc32", (PyCFunction
)PyZlib_crc32
, METH_VARARGS
,
983 {"decompress", (PyCFunction
)PyZlib_decompress
, METH_VARARGS
,
985 {"decompressobj", (PyCFunction
)PyZlib_decompressobj
, METH_VARARGS
,
986 decompressobj__doc__
},
990 static PyTypeObject Comptype
= {
991 PyVarObject_HEAD_INIT(0, 0)
995 (destructor
)Comp_dealloc
, /*tp_dealloc*/
1002 0, /*tp_as_sequence*/
1003 0, /*tp_as_mapping*/
1010 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
1014 0, /*tp_richcompare*/
1015 0, /*tp_weaklistoffset*/
1018 comp_methods
, /*tp_methods*/
1021 static PyTypeObject Decomptype
= {
1022 PyVarObject_HEAD_INIT(0, 0)
1026 (destructor
)Decomp_dealloc
, /*tp_dealloc*/
1033 0, /*tp_as_sequence*/
1034 0, /*tp_as_mapping*/
1041 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
1045 0, /*tp_richcompare*/
1046 0, /*tp_weaklistoffset*/
1049 Decomp_methods
, /*tp_methods*/
1050 Decomp_members
, /*tp_members*/
1053 PyDoc_STRVAR(zlib_module_documentation
,
1054 "The functions in this module allow compression and decompression using the\n"
1055 "zlib library, which is based on GNU zip.\n"
1057 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1058 "compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
1059 "compressobj([level]) -- Return a compressor object.\n"
1060 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1061 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1062 "decompressobj([wbits]) -- Return a decompressor object.\n"
1064 "'wbits' is window buffer size.\n"
1065 "Compressor objects support compress() and flush() methods; decompressor\n"
1066 "objects support decompress() and flush().");
1068 static struct PyModuleDef zlibmodule
= {
1069 PyModuleDef_HEAD_INIT
,
1071 zlib_module_documentation
,
1084 if (PyType_Ready(&Comptype
) < 0)
1086 if (PyType_Ready(&Decomptype
) < 0)
1088 m
= PyModule_Create(&zlibmodule
);
1092 ZlibError
= PyErr_NewException("zlib.error", NULL
, NULL
);
1093 if (ZlibError
!= NULL
) {
1094 Py_INCREF(ZlibError
);
1095 PyModule_AddObject(m
, "error", ZlibError
);
1097 PyModule_AddIntConstant(m
, "MAX_WBITS", MAX_WBITS
);
1098 PyModule_AddIntConstant(m
, "DEFLATED", DEFLATED
);
1099 PyModule_AddIntConstant(m
, "DEF_MEM_LEVEL", DEF_MEM_LEVEL
);
1100 PyModule_AddIntConstant(m
, "Z_BEST_SPEED", Z_BEST_SPEED
);
1101 PyModule_AddIntConstant(m
, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION
);
1102 PyModule_AddIntConstant(m
, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION
);
1103 PyModule_AddIntConstant(m
, "Z_FILTERED", Z_FILTERED
);
1104 PyModule_AddIntConstant(m
, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY
);
1105 PyModule_AddIntConstant(m
, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY
);
1107 PyModule_AddIntConstant(m
, "Z_FINISH", Z_FINISH
);
1108 PyModule_AddIntConstant(m
, "Z_NO_FLUSH", Z_NO_FLUSH
);
1109 PyModule_AddIntConstant(m
, "Z_SYNC_FLUSH", Z_SYNC_FLUSH
);
1110 PyModule_AddIntConstant(m
, "Z_FULL_FLUSH", Z_FULL_FLUSH
);
1112 ver
= PyUnicode_FromString(ZLIB_VERSION
);
1114 PyModule_AddObject(m
, "ZLIB_VERSION", ver
);
1116 PyModule_AddStringConstant(m
, "__version__", "1.0");