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 if (zst
.msg
== Z_NULL
)
56 PyErr_Format(ZlibError
, "Error %d %s", err
, msg
);
58 PyErr_Format(ZlibError
, "Error %d %s: %.200s", err
, msg
, zst
.msg
);
61 PyDoc_STRVAR(compressobj__doc__
,
62 "compressobj([level]) -- Return a compressor object.\n"
64 "Optional arg level is the compression level, in 1-9.");
66 PyDoc_STRVAR(decompressobj__doc__
,
67 "decompressobj([wbits]) -- Return a decompressor object.\n"
69 "Optional arg wbits is the window buffer size.");
72 newcompobject(PyTypeObject
*type
)
75 self
= PyObject_New(compobject
, type
);
78 self
->is_initialised
= 0;
79 self
->unused_data
= PyBytes_FromStringAndSize("", 0);
80 if (self
->unused_data
== NULL
) {
84 self
->unconsumed_tail
= PyBytes_FromStringAndSize("", 0);
85 if (self
->unconsumed_tail
== NULL
) {
90 self
->lock
= PyThread_allocate_lock();
95 PyDoc_STRVAR(compress__doc__
,
96 "compress(string[, level]) -- Returned compressed string.\n"
98 "Optional arg level is the compression level, in 1-9.");
101 PyZlib_compress(PyObject
*self
, PyObject
*args
)
103 PyObject
*ReturnVal
= NULL
;
105 Byte
*input
, *output
;
106 int length
, level
=Z_DEFAULT_COMPRESSION
, err
;
109 /* require Python string object, optional 'level' arg */
110 if (!PyArg_ParseTuple(args
, "s*|i:compress", &pinput
, &level
))
115 zst
.avail_out
= length
+ length
/1000 + 12 + 1;
117 output
= (Byte
*)malloc(zst
.avail_out
);
118 if (output
== NULL
) {
119 PyBuffer_Release(&pinput
);
120 PyErr_SetString(PyExc_MemoryError
,
121 "Can't allocate memory to compress data");
125 /* Past the point of no return. From here on out, we need to make sure
126 we clean up mallocs & INCREFs. */
128 zst
.zalloc
= (alloc_func
)NULL
;
129 zst
.zfree
= (free_func
)Z_NULL
;
130 zst
.next_out
= (Byte
*)output
;
131 zst
.next_in
= (Byte
*)input
;
132 zst
.avail_in
= length
;
133 err
= deflateInit(&zst
, level
);
139 PyErr_SetString(PyExc_MemoryError
,
140 "Out of memory while compressing data");
142 case(Z_STREAM_ERROR
):
143 PyErr_SetString(ZlibError
,
144 "Bad compression level");
148 zlib_error(zst
, err
, "while compressing data");
152 Py_BEGIN_ALLOW_THREADS
;
153 err
= deflate(&zst
, Z_FINISH
);
154 Py_END_ALLOW_THREADS
;
156 if (err
!= Z_STREAM_END
) {
157 zlib_error(zst
, err
, "while compressing data");
162 err
=deflateEnd(&zst
);
164 ReturnVal
= PyBytes_FromStringAndSize((char *)output
,
167 zlib_error(zst
, err
, "while finishing compression");
170 PyBuffer_Release(&pinput
);
176 PyDoc_STRVAR(decompress__doc__
,
177 "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
179 "Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
180 "the initial output buffer size.");
183 PyZlib_decompress(PyObject
*self
, PyObject
*args
)
185 PyObject
*result_str
;
190 Py_ssize_t r_strlen
=DEFAULTALLOC
;
193 if (!PyArg_ParseTuple(args
, "s*|in:decompress",
194 &pinput
, &wsize
, &r_strlen
))
202 zst
.avail_in
= length
;
203 zst
.avail_out
= r_strlen
;
205 if (!(result_str
= PyBytes_FromStringAndSize(NULL
, r_strlen
))) {
206 PyBuffer_Release(&pinput
);
210 zst
.zalloc
= (alloc_func
)NULL
;
211 zst
.zfree
= (free_func
)Z_NULL
;
212 zst
.next_out
= (Byte
*)PyBytes_AS_STRING(result_str
);
213 zst
.next_in
= (Byte
*)input
;
214 err
= inflateInit2(&zst
, wsize
);
220 PyErr_SetString(PyExc_MemoryError
,
221 "Out of memory while decompressing data");
225 zlib_error(zst
, err
, "while preparing to decompress data");
230 Py_BEGIN_ALLOW_THREADS
231 err
=inflate(&zst
, Z_FINISH
);
239 * If there is at least 1 byte of room according to zst.avail_out
240 * and we get this error, assume that it means zlib cannot
241 * process the inflate call() due to an error in the data.
243 if (zst
.avail_out
> 0) {
244 PyErr_Format(ZlibError
, "Error %i while decompressing data",
251 /* need more memory */
252 if (_PyBytes_Resize(&result_str
, r_strlen
<< 1) < 0) {
257 (unsigned char *)PyBytes_AS_STRING(result_str
) + r_strlen
;
258 zst
.avail_out
= r_strlen
;
259 r_strlen
= r_strlen
<< 1;
263 zlib_error(zst
, err
, "while decompressing data");
266 } while (err
!= Z_STREAM_END
);
268 err
= inflateEnd(&zst
);
270 zlib_error(zst
, err
, "while finishing data decompression");
274 if (_PyBytes_Resize(&result_str
, zst
.total_out
) < 0)
277 PyBuffer_Release(&pinput
);
281 PyBuffer_Release(&pinput
);
282 Py_XDECREF(result_str
);
287 PyZlib_compressobj(PyObject
*selfptr
, PyObject
*args
)
290 int level
=Z_DEFAULT_COMPRESSION
, method
=DEFLATED
;
291 int wbits
=MAX_WBITS
, memLevel
=DEF_MEM_LEVEL
, strategy
=0, err
;
293 if (!PyArg_ParseTuple(args
, "|iiiii:compressobj", &level
, &method
, &wbits
,
294 &memLevel
, &strategy
))
297 self
= newcompobject(&Comptype
);
300 self
->zst
.zalloc
= (alloc_func
)NULL
;
301 self
->zst
.zfree
= (free_func
)Z_NULL
;
302 self
->zst
.next_in
= NULL
;
303 self
->zst
.avail_in
= 0;
304 err
= deflateInit2(&self
->zst
, level
, method
, wbits
, memLevel
, strategy
);
307 self
->is_initialised
= 1;
308 return (PyObject
*)self
;
311 PyErr_SetString(PyExc_MemoryError
,
312 "Can't allocate memory for compression object");
314 case(Z_STREAM_ERROR
):
316 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
319 zlib_error(self
->zst
, err
, "while creating compression object");
326 PyZlib_decompressobj(PyObject
*selfptr
, PyObject
*args
)
328 int wbits
=DEF_WBITS
, err
;
330 if (!PyArg_ParseTuple(args
, "|i:decompressobj", &wbits
))
333 self
= newcompobject(&Decomptype
);
336 self
->zst
.zalloc
= (alloc_func
)NULL
;
337 self
->zst
.zfree
= (free_func
)Z_NULL
;
338 self
->zst
.next_in
= NULL
;
339 self
->zst
.avail_in
= 0;
340 err
= inflateInit2(&self
->zst
, wbits
);
343 self
->is_initialised
= 1;
344 return (PyObject
*)self
;
345 case(Z_STREAM_ERROR
):
347 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
351 PyErr_SetString(PyExc_MemoryError
,
352 "Can't allocate memory for decompression object");
355 zlib_error(self
->zst
, err
, "while creating decompression object");
362 Dealloc(compobject
*self
)
365 PyThread_free_lock(self
->lock
);
367 Py_XDECREF(self
->unused_data
);
368 Py_XDECREF(self
->unconsumed_tail
);
373 Comp_dealloc(compobject
*self
)
375 if (self
->is_initialised
)
376 deflateEnd(&self
->zst
);
381 Decomp_dealloc(compobject
*self
)
383 if (self
->is_initialised
)
384 inflateEnd(&self
->zst
);
388 PyDoc_STRVAR(comp_compress__doc__
,
389 "compress(data) -- Return a string containing data compressed.\n"
391 "After calling this function, some of the input data may still\n"
392 "be stored in internal buffers for later processing.\n"
393 "Call the flush() method to clear these buffers.");
397 PyZlib_objcompress(compobject
*self
, PyObject
*args
)
399 int err
, inplen
, length
= DEFAULTALLOC
;
403 unsigned long start_total_out
;
405 if (!PyArg_ParseTuple(args
, "s*:compress", &pinput
))
410 if (!(RetVal
= PyBytes_FromStringAndSize(NULL
, length
))) {
411 PyBuffer_Release(&pinput
);
417 start_total_out
= self
->zst
.total_out
;
418 self
->zst
.avail_in
= inplen
;
419 self
->zst
.next_in
= input
;
420 self
->zst
.avail_out
= length
;
421 self
->zst
.next_out
= (unsigned char *)PyBytes_AS_STRING(RetVal
);
423 Py_BEGIN_ALLOW_THREADS
424 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
427 /* while Z_OK and the output buffer is full, there might be more output,
428 so extend the output buffer and try again */
429 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
430 if (_PyBytes_Resize(&RetVal
, length
<< 1) < 0) {
436 (unsigned char *)PyBytes_AS_STRING(RetVal
) + length
;
437 self
->zst
.avail_out
= length
;
438 length
= length
<< 1;
440 Py_BEGIN_ALLOW_THREADS
441 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
444 /* We will only get Z_BUF_ERROR if the output buffer was full but
445 there wasn't more output when we tried again, so it is not an error
449 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
450 zlib_error(self
->zst
, err
, "while compressing");
455 if (_PyBytes_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
) < 0) {
462 PyBuffer_Release(&pinput
);
466 PyDoc_STRVAR(decomp_decompress__doc__
,
467 "decompress(data, max_length) -- Return a string containing the decompressed\n"
468 "version of the data.\n"
470 "After calling this function, some of the input data may still be stored in\n"
471 "internal buffers for later processing.\n"
472 "Call the flush() method to clear these buffers.\n"
473 "If the max_length parameter is specified then the return value will be\n"
474 "no longer than max_length. Unconsumed input data will be stored in\n"
475 "the unconsumed_tail attribute.");
478 PyZlib_objdecompress(compobject
*self
, PyObject
*args
)
480 int err
, inplen
, old_length
, length
= DEFAULTALLOC
;
485 unsigned long start_total_out
;
487 if (!PyArg_ParseTuple(args
, "s*|i:decompress", &pinput
,
492 if (max_length
< 0) {
493 PyBuffer_Release(&pinput
);
494 PyErr_SetString(PyExc_ValueError
,
495 "max_length must be greater than zero");
499 /* limit amount of data allocated to max_length */
500 if (max_length
&& length
> max_length
)
502 if (!(RetVal
= PyBytes_FromStringAndSize(NULL
, length
))) {
503 PyBuffer_Release(&pinput
);
509 start_total_out
= self
->zst
.total_out
;
510 self
->zst
.avail_in
= inplen
;
511 self
->zst
.next_in
= input
;
512 self
->zst
.avail_out
= length
;
513 self
->zst
.next_out
= (unsigned char *)PyBytes_AS_STRING(RetVal
);
515 Py_BEGIN_ALLOW_THREADS
516 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
519 /* While Z_OK and the output buffer is full, there might be more output.
520 So extend the output buffer and try again.
522 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
523 /* If max_length set, don't continue decompressing if we've already
526 if (max_length
&& length
>= max_length
)
531 length
= length
<< 1;
532 if (max_length
&& length
> max_length
)
535 if (_PyBytes_Resize(&RetVal
, length
) < 0) {
541 (unsigned char *)PyBytes_AS_STRING(RetVal
) + old_length
;
542 self
->zst
.avail_out
= length
- old_length
;
544 Py_BEGIN_ALLOW_THREADS
545 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
549 /* Not all of the compressed data could be accommodated in the output buffer
550 of specified size. Return the unconsumed tail in an attribute.*/
552 Py_DECREF(self
->unconsumed_tail
);
553 self
->unconsumed_tail
= PyBytes_FromStringAndSize((char *)self
->zst
.next_in
,
555 if(!self
->unconsumed_tail
) {
562 /* The end of the compressed data has been reached, so set the
563 unused_data attribute to a string containing the remainder of the
564 data in the string. Note that this is also a logical place to call
565 inflateEnd, but the old behaviour of only calling it on flush() is
568 if (err
== Z_STREAM_END
) {
569 Py_XDECREF(self
->unused_data
); /* Free original empty string */
570 self
->unused_data
= PyBytes_FromStringAndSize(
571 (char *)self
->zst
.next_in
, self
->zst
.avail_in
);
572 if (self
->unused_data
== NULL
) {
576 /* We will only get Z_BUF_ERROR if the output buffer was full
577 but there wasn't more output when we tried again, so it is
578 not an error condition.
580 } else if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
581 zlib_error(self
->zst
, err
, "while decompressing");
587 if (_PyBytes_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
) < 0) {
594 PyBuffer_Release(&pinput
);
598 PyDoc_STRVAR(comp_flush__doc__
,
599 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
601 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
602 "default value used when mode is not specified is Z_FINISH.\n"
603 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
604 "calling the flush() method. Otherwise, more data can still be compressed.");
607 PyZlib_flush(compobject
*self
, PyObject
*args
)
609 int err
, length
= DEFAULTALLOC
;
611 int flushmode
= Z_FINISH
;
612 unsigned long start_total_out
;
614 if (!PyArg_ParseTuple(args
, "|i:flush", &flushmode
))
617 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
618 doing any work at all; just return an empty string. */
619 if (flushmode
== Z_NO_FLUSH
) {
620 return PyBytes_FromStringAndSize(NULL
, 0);
623 if (!(RetVal
= PyBytes_FromStringAndSize(NULL
, length
)))
628 start_total_out
= self
->zst
.total_out
;
629 self
->zst
.avail_in
= 0;
630 self
->zst
.avail_out
= length
;
631 self
->zst
.next_out
= (unsigned char *)PyBytes_AS_STRING(RetVal
);
633 Py_BEGIN_ALLOW_THREADS
634 err
= deflate(&(self
->zst
), flushmode
);
637 /* while Z_OK and the output buffer is full, there might be more output,
638 so extend the output buffer and try again */
639 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
640 if (_PyBytes_Resize(&RetVal
, length
<< 1) < 0) {
646 (unsigned char *)PyBytes_AS_STRING(RetVal
) + length
;
647 self
->zst
.avail_out
= length
;
648 length
= length
<< 1;
650 Py_BEGIN_ALLOW_THREADS
651 err
= deflate(&(self
->zst
), flushmode
);
655 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
656 various data structures. Note we should only get Z_STREAM_END when
657 flushmode is Z_FINISH, but checking both for safety*/
658 if (err
== Z_STREAM_END
&& flushmode
== Z_FINISH
) {
659 err
= deflateEnd(&(self
->zst
));
661 zlib_error(self
->zst
, err
, "from deflateEnd()");
667 self
->is_initialised
= 0;
669 /* We will only get Z_BUF_ERROR if the output buffer was full
670 but there wasn't more output when we tried again, so it is
671 not an error condition.
673 } else if (err
!=Z_OK
&& err
!=Z_BUF_ERROR
) {
674 zlib_error(self
->zst
, err
, "while flushing");
680 if (_PyBytes_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
) < 0) {
691 #ifdef HAVE_ZLIB_COPY
692 PyDoc_STRVAR(comp_copy__doc__
,
693 "copy() -- Return a copy of the compression object.");
696 PyZlib_copy(compobject
*self
)
698 compobject
*retval
= NULL
;
701 retval
= newcompobject(&Comptype
);
702 if (!retval
) return NULL
;
704 /* Copy the zstream state
705 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
708 err
= deflateCopy(&retval
->zst
, &self
->zst
);
712 case(Z_STREAM_ERROR
):
713 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
716 PyErr_SetString(PyExc_MemoryError
,
717 "Can't allocate memory for compression object");
720 zlib_error(self
->zst
, err
, "while copying compression object");
723 Py_INCREF(self
->unused_data
);
724 Py_INCREF(self
->unconsumed_tail
);
725 Py_XDECREF(retval
->unused_data
);
726 Py_XDECREF(retval
->unconsumed_tail
);
727 retval
->unused_data
= self
->unused_data
;
728 retval
->unconsumed_tail
= self
->unconsumed_tail
;
730 /* Mark it as being initialized */
731 retval
->is_initialised
= 1;
734 return (PyObject
*)retval
;
742 PyDoc_STRVAR(decomp_copy__doc__
,
743 "copy() -- Return a copy of the decompression object.");
746 PyZlib_uncopy(compobject
*self
)
748 compobject
*retval
= NULL
;
751 retval
= newcompobject(&Decomptype
);
752 if (!retval
) return NULL
;
754 /* Copy the zstream state
755 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
758 err
= inflateCopy(&retval
->zst
, &self
->zst
);
762 case(Z_STREAM_ERROR
):
763 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
766 PyErr_SetString(PyExc_MemoryError
,
767 "Can't allocate memory for decompression object");
770 zlib_error(self
->zst
, err
, "while copying decompression object");
774 Py_INCREF(self
->unused_data
);
775 Py_INCREF(self
->unconsumed_tail
);
776 Py_XDECREF(retval
->unused_data
);
777 Py_XDECREF(retval
->unconsumed_tail
);
778 retval
->unused_data
= self
->unused_data
;
779 retval
->unconsumed_tail
= self
->unconsumed_tail
;
781 /* Mark it as being initialized */
782 retval
->is_initialised
= 1;
785 return (PyObject
*)retval
;
794 PyDoc_STRVAR(decomp_flush__doc__
,
795 "flush( [length] ) -- Return a string containing any remaining\n"
796 "decompressed data. length, if given, is the initial size of the\n"
799 "The decompressor object can no longer be used after this call.");
802 PyZlib_unflush(compobject
*self
, PyObject
*args
)
804 int err
, length
= DEFAULTALLOC
;
805 PyObject
* retval
= NULL
;
806 unsigned long start_total_out
;
808 if (!PyArg_ParseTuple(args
, "|i:flush", &length
))
811 PyErr_SetString(PyExc_ValueError
, "length must be greater than zero");
814 if (!(retval
= PyBytes_FromStringAndSize(NULL
, length
)))
820 start_total_out
= self
->zst
.total_out
;
821 self
->zst
.avail_out
= length
;
822 self
->zst
.next_out
= (Byte
*)PyBytes_AS_STRING(retval
);
824 Py_BEGIN_ALLOW_THREADS
825 err
= inflate(&(self
->zst
), Z_FINISH
);
828 /* while Z_OK and the output buffer is full, there might be more output,
829 so extend the output buffer and try again */
830 while ((err
== Z_OK
|| err
== Z_BUF_ERROR
) && self
->zst
.avail_out
== 0) {
831 if (_PyBytes_Resize(&retval
, length
<< 1) < 0) {
836 self
->zst
.next_out
= (Byte
*)PyBytes_AS_STRING(retval
) + length
;
837 self
->zst
.avail_out
= length
;
838 length
= length
<< 1;
840 Py_BEGIN_ALLOW_THREADS
841 err
= inflate(&(self
->zst
), Z_FINISH
);
845 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
846 various data structures. Note we should only get Z_STREAM_END when
847 flushmode is Z_FINISH */
848 if (err
== Z_STREAM_END
) {
849 err
= inflateEnd(&(self
->zst
));
850 self
->is_initialised
= 0;
852 zlib_error(self
->zst
, err
, "from inflateEnd()");
858 if (_PyBytes_Resize(&retval
, self
->zst
.total_out
- start_total_out
) < 0) {
870 static PyMethodDef comp_methods
[] =
872 {"compress", (binaryfunc
)PyZlib_objcompress
, METH_VARARGS
,
873 comp_compress__doc__
},
874 {"flush", (binaryfunc
)PyZlib_flush
, METH_VARARGS
,
876 #ifdef HAVE_ZLIB_COPY
877 {"copy", (PyCFunction
)PyZlib_copy
, METH_NOARGS
,
883 static PyMethodDef Decomp_methods
[] =
885 {"decompress", (binaryfunc
)PyZlib_objdecompress
, METH_VARARGS
,
886 decomp_decompress__doc__
},
887 {"flush", (binaryfunc
)PyZlib_unflush
, METH_VARARGS
,
888 decomp_flush__doc__
},
889 #ifdef HAVE_ZLIB_COPY
890 {"copy", (PyCFunction
)PyZlib_uncopy
, METH_NOARGS
,
896 #define COMP_OFF(x) offsetof(compobject, x)
897 static PyMemberDef Decomp_members
[] = {
898 {"unused_data", T_OBJECT
, COMP_OFF(unused_data
), READONLY
},
899 {"unconsumed_tail", T_OBJECT
, COMP_OFF(unconsumed_tail
), READONLY
},
903 PyDoc_STRVAR(adler32__doc__
,
904 "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
906 "An optional starting value can be specified. The returned checksum is\n"
910 PyZlib_adler32(PyObject
*self
, PyObject
*args
)
912 unsigned int adler32val
= 1; /* adler32(0L, Z_NULL, 0) */
915 if (!PyArg_ParseTuple(args
, "s*|I:adler32", &pbuf
, &adler32val
))
917 /* Releasing the GIL for very small buffers is inefficient
918 and may lower performance */
919 if (pbuf
.len
> 1024*5) {
920 Py_BEGIN_ALLOW_THREADS
921 adler32val
= adler32(adler32val
, pbuf
.buf
, pbuf
.len
);
924 adler32val
= adler32(adler32val
, pbuf
.buf
, pbuf
.len
);
926 PyBuffer_Release(&pbuf
);
927 return PyLong_FromUnsignedLong(adler32val
& 0xffffffffU
);
930 PyDoc_STRVAR(crc32__doc__
,
931 "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
933 "An optional starting value can be specified. The returned checksum is\n"
937 PyZlib_crc32(PyObject
*self
, PyObject
*args
)
939 unsigned int crc32val
= 0; /* crc32(0L, Z_NULL, 0) */
943 if (!PyArg_ParseTuple(args
, "s*|I:crc32", &pbuf
, &crc32val
))
945 /* Releasing the GIL for very small buffers is inefficient
946 and may lower performance */
947 if (pbuf
.len
> 1024*5) {
948 Py_BEGIN_ALLOW_THREADS
949 signed_val
= crc32(crc32val
, pbuf
.buf
, pbuf
.len
);
952 signed_val
= crc32(crc32val
, pbuf
.buf
, pbuf
.len
);
954 PyBuffer_Release(&pbuf
);
955 return PyLong_FromUnsignedLong(signed_val
& 0xffffffffU
);
959 static PyMethodDef zlib_methods
[] =
961 {"adler32", (PyCFunction
)PyZlib_adler32
, METH_VARARGS
,
963 {"compress", (PyCFunction
)PyZlib_compress
, METH_VARARGS
,
965 {"compressobj", (PyCFunction
)PyZlib_compressobj
, METH_VARARGS
,
967 {"crc32", (PyCFunction
)PyZlib_crc32
, METH_VARARGS
,
969 {"decompress", (PyCFunction
)PyZlib_decompress
, METH_VARARGS
,
971 {"decompressobj", (PyCFunction
)PyZlib_decompressobj
, METH_VARARGS
,
972 decompressobj__doc__
},
976 static PyTypeObject Comptype
= {
977 PyVarObject_HEAD_INIT(0, 0)
981 (destructor
)Comp_dealloc
, /*tp_dealloc*/
988 0, /*tp_as_sequence*/
996 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
1000 0, /*tp_richcompare*/
1001 0, /*tp_weaklistoffset*/
1004 comp_methods
, /*tp_methods*/
1007 static PyTypeObject Decomptype
= {
1008 PyVarObject_HEAD_INIT(0, 0)
1012 (destructor
)Decomp_dealloc
, /*tp_dealloc*/
1019 0, /*tp_as_sequence*/
1020 0, /*tp_as_mapping*/
1027 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
1031 0, /*tp_richcompare*/
1032 0, /*tp_weaklistoffset*/
1035 Decomp_methods
, /*tp_methods*/
1036 Decomp_members
, /*tp_members*/
1039 PyDoc_STRVAR(zlib_module_documentation
,
1040 "The functions in this module allow compression and decompression using the\n"
1041 "zlib library, which is based on GNU zip.\n"
1043 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1044 "compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
1045 "compressobj([level]) -- Return a compressor object.\n"
1046 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1047 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1048 "decompressobj([wbits]) -- Return a decompressor object.\n"
1050 "'wbits' is window buffer size.\n"
1051 "Compressor objects support compress() and flush() methods; decompressor\n"
1052 "objects support decompress() and flush().");
1054 static struct PyModuleDef zlibmodule
= {
1055 PyModuleDef_HEAD_INIT
,
1057 zlib_module_documentation
,
1070 if (PyType_Ready(&Comptype
) < 0)
1072 if (PyType_Ready(&Decomptype
) < 0)
1074 m
= PyModule_Create(&zlibmodule
);
1078 ZlibError
= PyErr_NewException("zlib.error", NULL
, NULL
);
1079 if (ZlibError
!= NULL
) {
1080 Py_INCREF(ZlibError
);
1081 PyModule_AddObject(m
, "error", ZlibError
);
1083 PyModule_AddIntConstant(m
, "MAX_WBITS", MAX_WBITS
);
1084 PyModule_AddIntConstant(m
, "DEFLATED", DEFLATED
);
1085 PyModule_AddIntConstant(m
, "DEF_MEM_LEVEL", DEF_MEM_LEVEL
);
1086 PyModule_AddIntConstant(m
, "Z_BEST_SPEED", Z_BEST_SPEED
);
1087 PyModule_AddIntConstant(m
, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION
);
1088 PyModule_AddIntConstant(m
, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION
);
1089 PyModule_AddIntConstant(m
, "Z_FILTERED", Z_FILTERED
);
1090 PyModule_AddIntConstant(m
, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY
);
1091 PyModule_AddIntConstant(m
, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY
);
1093 PyModule_AddIntConstant(m
, "Z_FINISH", Z_FINISH
);
1094 PyModule_AddIntConstant(m
, "Z_NO_FLUSH", Z_NO_FLUSH
);
1095 PyModule_AddIntConstant(m
, "Z_SYNC_FLUSH", Z_SYNC_FLUSH
);
1096 PyModule_AddIntConstant(m
, "Z_FULL_FLUSH", Z_FULL_FLUSH
);
1098 ver
= PyUnicode_FromString(ZLIB_VERSION
);
1100 PyModule_AddObject(m
, "ZLIB_VERSION", ver
);
1102 PyModule_AddStringConstant(m
, "__version__", "1.0");