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 const char *zmsg
= Z_NULL
;
76 /* In case of a version mismatch, zst.msg won't be initialized.
77 Check for this case first, before looking at zst.msg. */
78 if (err
== Z_VERSION_ERROR
)
79 zmsg
= "library version mismatch";
85 zmsg
= "incomplete or truncated stream";
88 zmsg
= "inconsistent stream state";
91 zmsg
= "invalid input data";
96 PyErr_Format(ZlibError
, "Error %d %s", err
, msg
);
98 PyErr_Format(ZlibError
, "Error %d %s: %.200s", err
, msg
, zmsg
);
101 PyDoc_STRVAR(compressobj__doc__
,
102 "compressobj([level]) -- Return a compressor object.\n"
104 "Optional arg level is the compression level, in 0-9.");
106 PyDoc_STRVAR(decompressobj__doc__
,
107 "decompressobj([wbits]) -- Return a decompressor object.\n"
109 "Optional arg wbits is the window buffer size.");
112 newcompobject(PyTypeObject
*type
)
115 self
= PyObject_New(compobject
, type
);
118 self
->is_initialised
= 0;
119 self
->unused_data
= PyString_FromString("");
120 if (self
->unused_data
== NULL
) {
124 self
->unconsumed_tail
= PyString_FromString("");
125 if (self
->unconsumed_tail
== NULL
) {
132 PyDoc_STRVAR(compress__doc__
,
133 "compress(string[, level]) -- Returned compressed string.\n"
135 "Optional arg level is the compression level, in 0-9.");
138 PyZlib_compress(PyObject
*self
, PyObject
*args
)
140 PyObject
*ReturnVal
= NULL
;
141 Byte
*input
, *output
;
142 int length
, level
=Z_DEFAULT_COMPRESSION
, err
;
145 /* require Python string object, optional 'level' arg */
146 if (!PyArg_ParseTuple(args
, "s#|i:compress", &input
, &length
, &level
))
149 zst
.avail_out
= length
+ length
/1000 + 12 + 1;
151 output
= (Byte
*)malloc(zst
.avail_out
);
152 if (output
== NULL
) {
153 PyErr_SetString(PyExc_MemoryError
,
154 "Can't allocate memory to compress data");
158 /* Past the point of no return. From here on out, we need to make sure
159 we clean up mallocs & INCREFs. */
161 zst
.zalloc
= (alloc_func
)NULL
;
162 zst
.zfree
= (free_func
)Z_NULL
;
163 zst
.next_out
= (Byte
*)output
;
164 zst
.next_in
= (Byte
*)input
;
165 zst
.avail_in
= length
;
166 err
= deflateInit(&zst
, level
);
172 PyErr_SetString(PyExc_MemoryError
,
173 "Out of memory while compressing data");
175 case(Z_STREAM_ERROR
):
176 PyErr_SetString(ZlibError
,
177 "Bad compression level");
181 zlib_error(zst
, err
, "while compressing data");
185 Py_BEGIN_ALLOW_THREADS
;
186 err
= deflate(&zst
, Z_FINISH
);
187 Py_END_ALLOW_THREADS
;
189 if (err
!= Z_STREAM_END
) {
190 zlib_error(zst
, err
, "while compressing data");
195 err
=deflateEnd(&zst
);
197 ReturnVal
= PyString_FromStringAndSize((char *)output
,
200 zlib_error(zst
, err
, "while finishing compression");
208 PyDoc_STRVAR(decompress__doc__
,
209 "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
211 "Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
212 "the initial output buffer size.");
215 PyZlib_decompress(PyObject
*self
, PyObject
*args
)
217 PyObject
*result_str
;
221 Py_ssize_t r_strlen
=DEFAULTALLOC
;
224 if (!PyArg_ParseTuple(args
, "s#|in:decompress",
225 &input
, &length
, &wsize
, &r_strlen
))
231 zst
.avail_in
= length
;
232 zst
.avail_out
= r_strlen
;
234 if (!(result_str
= PyString_FromStringAndSize(NULL
, r_strlen
)))
237 zst
.zalloc
= (alloc_func
)NULL
;
238 zst
.zfree
= (free_func
)Z_NULL
;
239 zst
.next_out
= (Byte
*)PyString_AS_STRING(result_str
);
240 zst
.next_in
= (Byte
*)input
;
241 err
= inflateInit2(&zst
, wsize
);
247 PyErr_SetString(PyExc_MemoryError
,
248 "Out of memory while decompressing data");
252 zlib_error(zst
, err
, "while preparing to decompress data");
257 Py_BEGIN_ALLOW_THREADS
258 err
=inflate(&zst
, Z_FINISH
);
266 * If there is at least 1 byte of room according to zst.avail_out
267 * and we get this error, assume that it means zlib cannot
268 * process the inflate call() due to an error in the data.
270 if (zst
.avail_out
> 0) {
271 zlib_error(zst
, err
, "while decompressing data");
277 /* need more memory */
278 if (_PyString_Resize(&result_str
, r_strlen
<< 1) < 0) {
282 zst
.next_out
= (unsigned char *)PyString_AS_STRING(result_str
) \
284 zst
.avail_out
= r_strlen
;
285 r_strlen
= r_strlen
<< 1;
289 zlib_error(zst
, err
, "while decompressing data");
292 } while (err
!= Z_STREAM_END
);
294 err
= inflateEnd(&zst
);
296 zlib_error(zst
, err
, "while finishing data decompression");
300 _PyString_Resize(&result_str
, zst
.total_out
);
304 Py_XDECREF(result_str
);
309 PyZlib_compressobj(PyObject
*selfptr
, PyObject
*args
)
312 int level
=Z_DEFAULT_COMPRESSION
, method
=DEFLATED
;
313 int wbits
=MAX_WBITS
, memLevel
=DEF_MEM_LEVEL
, strategy
=0, err
;
315 if (!PyArg_ParseTuple(args
, "|iiiii:compressobj", &level
, &method
, &wbits
,
316 &memLevel
, &strategy
))
319 self
= newcompobject(&Comptype
);
322 self
->zst
.zalloc
= (alloc_func
)NULL
;
323 self
->zst
.zfree
= (free_func
)Z_NULL
;
324 self
->zst
.next_in
= NULL
;
325 self
->zst
.avail_in
= 0;
326 err
= deflateInit2(&self
->zst
, level
, method
, wbits
, memLevel
, strategy
);
329 self
->is_initialised
= 1;
330 return (PyObject
*)self
;
333 PyErr_SetString(PyExc_MemoryError
,
334 "Can't allocate memory for compression object");
336 case(Z_STREAM_ERROR
):
338 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
341 zlib_error(self
->zst
, err
, "while creating compression object");
348 PyZlib_decompressobj(PyObject
*selfptr
, PyObject
*args
)
350 int wbits
=DEF_WBITS
, err
;
352 if (!PyArg_ParseTuple(args
, "|i:decompressobj", &wbits
))
355 self
= newcompobject(&Decomptype
);
358 self
->zst
.zalloc
= (alloc_func
)NULL
;
359 self
->zst
.zfree
= (free_func
)Z_NULL
;
360 self
->zst
.next_in
= NULL
;
361 self
->zst
.avail_in
= 0;
362 err
= inflateInit2(&self
->zst
, wbits
);
365 self
->is_initialised
= 1;
366 return (PyObject
*)self
;
367 case(Z_STREAM_ERROR
):
369 PyErr_SetString(PyExc_ValueError
, "Invalid initialization option");
373 PyErr_SetString(PyExc_MemoryError
,
374 "Can't allocate memory for decompression object");
377 zlib_error(self
->zst
, err
, "while creating decompression object");
384 Comp_dealloc(compobject
*self
)
386 if (self
->is_initialised
)
387 deflateEnd(&self
->zst
);
388 Py_XDECREF(self
->unused_data
);
389 Py_XDECREF(self
->unconsumed_tail
);
394 Decomp_dealloc(compobject
*self
)
396 if (self
->is_initialised
)
397 inflateEnd(&self
->zst
);
398 Py_XDECREF(self
->unused_data
);
399 Py_XDECREF(self
->unconsumed_tail
);
403 PyDoc_STRVAR(comp_compress__doc__
,
404 "compress(data) -- Return a string containing data compressed.\n"
406 "After calling this function, some of the input data may still\n"
407 "be stored in internal buffers for later processing.\n"
408 "Call the flush() method to clear these buffers.");
412 PyZlib_objcompress(compobject
*self
, PyObject
*args
)
415 Py_ssize_t length
= DEFAULTALLOC
;
418 unsigned long start_total_out
;
420 if (!PyArg_ParseTuple(args
, "s#:compress", &input
, &inplen
))
423 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
)))
428 start_total_out
= self
->zst
.total_out
;
429 self
->zst
.avail_in
= inplen
;
430 self
->zst
.next_in
= input
;
431 self
->zst
.avail_out
= length
;
432 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
);
434 Py_BEGIN_ALLOW_THREADS
435 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
438 /* while Z_OK and the output buffer is full, there might be more output,
439 so extend the output buffer and try again */
440 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
441 if (_PyString_Resize(&RetVal
, length
<< 1) < 0)
443 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
) \
445 self
->zst
.avail_out
= length
;
446 length
= length
<< 1;
448 Py_BEGIN_ALLOW_THREADS
449 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
452 /* We will only get Z_BUF_ERROR if the output buffer was full but
453 there wasn't more output when we tried again, so it is not an error
457 if (err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
458 zlib_error(self
->zst
, err
, "while compressing");
463 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
470 /* Helper for objdecompress() and unflush(). Saves any unconsumed input data in
471 self->unused_data or self->unconsumed_tail, as appropriate. */
473 save_unconsumed_input(compobject
*self
, int err
)
475 if (err
== Z_STREAM_END
) {
476 /* The end of the compressed data has been reached. Store the leftover
477 input data in self->unused_data. */
478 if (self
->zst
.avail_in
> 0) {
479 Py_ssize_t old_size
= PyString_GET_SIZE(self
->unused_data
);
482 if (self
->zst
.avail_in
> PY_SSIZE_T_MAX
- old_size
) {
486 new_size
= old_size
+ self
->zst
.avail_in
;
487 new_data
= PyString_FromStringAndSize(NULL
, new_size
);
488 if (new_data
== NULL
)
490 Py_MEMCPY(PyString_AS_STRING(new_data
),
491 PyString_AS_STRING(self
->unused_data
), old_size
);
492 Py_MEMCPY(PyString_AS_STRING(new_data
) + old_size
,
493 self
->zst
.next_in
, self
->zst
.avail_in
);
494 Py_DECREF(self
->unused_data
);
495 self
->unused_data
= new_data
;
496 self
->zst
.avail_in
= 0;
499 if (self
->zst
.avail_in
> 0 || PyString_GET_SIZE(self
->unconsumed_tail
)) {
500 /* This code handles two distinct cases:
501 1. Output limit was reached. Save leftover input in unconsumed_tail.
502 2. All input data was consumed. Clear unconsumed_tail. */
503 PyObject
*new_data
= PyString_FromStringAndSize(
504 (char *)self
->zst
.next_in
, self
->zst
.avail_in
);
505 if (new_data
== NULL
)
507 Py_DECREF(self
->unconsumed_tail
);
508 self
->unconsumed_tail
= new_data
;
513 PyDoc_STRVAR(decomp_decompress__doc__
,
514 "decompress(data, max_length) -- Return a string containing the decompressed\n"
515 "version of the data.\n"
517 "After calling this function, some of the input data may still be stored in\n"
518 "internal buffers for later processing.\n"
519 "Call the flush() method to clear these buffers.\n"
520 "If the max_length parameter is specified then the return value will be\n"
521 "no longer than max_length. Unconsumed input data will be stored in\n"
522 "the unconsumed_tail attribute.");
525 PyZlib_objdecompress(compobject
*self
, PyObject
*args
)
527 int err
, inplen
, max_length
= 0;
528 Py_ssize_t old_length
, length
= DEFAULTALLOC
;
531 unsigned long start_total_out
;
533 if (!PyArg_ParseTuple(args
, "s#|i:decompress", &input
,
534 &inplen
, &max_length
))
536 if (max_length
< 0) {
537 PyErr_SetString(PyExc_ValueError
,
538 "max_length must be greater than zero");
542 /* limit amount of data allocated to max_length */
543 if (max_length
&& length
> max_length
)
545 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
)))
550 start_total_out
= self
->zst
.total_out
;
551 self
->zst
.avail_in
= inplen
;
552 self
->zst
.next_in
= input
;
553 self
->zst
.avail_out
= length
;
554 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
);
556 Py_BEGIN_ALLOW_THREADS
557 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
560 /* While Z_OK and the output buffer is full, there might be more output.
561 So extend the output buffer and try again.
563 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
564 /* If max_length set, don't continue decompressing if we've already
567 if (max_length
&& length
>= max_length
)
572 length
= length
<< 1;
573 if (max_length
&& length
> max_length
)
576 if (_PyString_Resize(&RetVal
, length
) < 0)
578 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
) \
580 self
->zst
.avail_out
= length
- old_length
;
582 Py_BEGIN_ALLOW_THREADS
583 err
= inflate(&(self
->zst
), Z_SYNC_FLUSH
);
587 if (save_unconsumed_input(self
, err
) < 0) {
593 /* This is the logical place to call inflateEnd, but the old behaviour of
594 only calling it on flush() is preserved. */
596 if (err
!= Z_STREAM_END
&& err
!= Z_OK
&& err
!= Z_BUF_ERROR
) {
597 /* We will only get Z_BUF_ERROR if the output buffer was full
598 but there wasn't more output when we tried again, so it is
599 not an error condition.
601 zlib_error(self
->zst
, err
, "while decompressing");
607 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
615 PyDoc_STRVAR(comp_flush__doc__
,
616 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
618 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
619 "default value used when mode is not specified is Z_FINISH.\n"
620 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
621 "calling the flush() method. Otherwise, more data can still be compressed.");
624 PyZlib_flush(compobject
*self
, PyObject
*args
)
626 int err
, length
= DEFAULTALLOC
;
628 int flushmode
= Z_FINISH
;
629 unsigned long start_total_out
;
631 if (!PyArg_ParseTuple(args
, "|i:flush", &flushmode
))
634 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
635 doing any work at all; just return an empty string. */
636 if (flushmode
== Z_NO_FLUSH
) {
637 return PyString_FromStringAndSize(NULL
, 0);
640 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
)))
645 start_total_out
= self
->zst
.total_out
;
646 self
->zst
.avail_in
= 0;
647 self
->zst
.avail_out
= length
;
648 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
);
650 Py_BEGIN_ALLOW_THREADS
651 err
= deflate(&(self
->zst
), flushmode
);
654 /* while Z_OK and the output buffer is full, there might be more output,
655 so extend the output buffer and try again */
656 while (err
== Z_OK
&& self
->zst
.avail_out
== 0) {
657 if (_PyString_Resize(&RetVal
, length
<< 1) < 0)
659 self
->zst
.next_out
= (unsigned char *)PyString_AS_STRING(RetVal
) \
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 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
702 #ifdef HAVE_ZLIB_COPY
703 PyDoc_STRVAR(comp_copy__doc__
,
704 "copy() -- Return a copy of the compression object.");
707 PyZlib_copy(compobject
*self
)
709 compobject
*retval
= NULL
;
712 retval
= newcompobject(&Comptype
);
713 if (!retval
) return NULL
;
715 /* Copy the zstream state
716 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
719 err
= deflateCopy(&retval
->zst
, &self
->zst
);
723 case(Z_STREAM_ERROR
):
724 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
727 PyErr_SetString(PyExc_MemoryError
,
728 "Can't allocate memory for compression object");
731 zlib_error(self
->zst
, err
, "while copying compression object");
735 Py_INCREF(self
->unused_data
);
736 Py_INCREF(self
->unconsumed_tail
);
737 Py_XDECREF(retval
->unused_data
);
738 Py_XDECREF(retval
->unconsumed_tail
);
739 retval
->unused_data
= self
->unused_data
;
740 retval
->unconsumed_tail
= self
->unconsumed_tail
;
742 /* Mark it as being initialized */
743 retval
->is_initialised
= 1;
746 return (PyObject
*)retval
;
754 PyDoc_STRVAR(decomp_copy__doc__
,
755 "copy() -- Return a copy of the decompression object.");
758 PyZlib_uncopy(compobject
*self
)
760 compobject
*retval
= NULL
;
763 retval
= newcompobject(&Decomptype
);
764 if (!retval
) return NULL
;
766 /* Copy the zstream state
767 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
770 err
= inflateCopy(&retval
->zst
, &self
->zst
);
774 case(Z_STREAM_ERROR
):
775 PyErr_SetString(PyExc_ValueError
, "Inconsistent stream state");
778 PyErr_SetString(PyExc_MemoryError
,
779 "Can't allocate memory for decompression object");
782 zlib_error(self
->zst
, err
, "while copying decompression object");
786 Py_INCREF(self
->unused_data
);
787 Py_INCREF(self
->unconsumed_tail
);
788 Py_XDECREF(retval
->unused_data
);
789 Py_XDECREF(retval
->unconsumed_tail
);
790 retval
->unused_data
= self
->unused_data
;
791 retval
->unconsumed_tail
= self
->unconsumed_tail
;
793 /* Mark it as being initialized */
794 retval
->is_initialised
= 1;
797 return (PyObject
*)retval
;
806 PyDoc_STRVAR(decomp_flush__doc__
,
807 "flush( [length] ) -- Return a string containing any remaining\n"
808 "decompressed data. length, if given, is the initial size of the\n"
811 "The decompressor object can no longer be used after this call.");
814 PyZlib_unflush(compobject
*self
, PyObject
*args
)
816 int err
, length
= DEFAULTALLOC
;
817 PyObject
* retval
= NULL
;
818 unsigned long start_total_out
;
820 if (!PyArg_ParseTuple(args
, "|i:flush", &length
))
823 PyErr_SetString(PyExc_ValueError
, "length must be greater than zero");
826 if (!(retval
= PyString_FromStringAndSize(NULL
, length
)))
832 start_total_out
= self
->zst
.total_out
;
833 self
->zst
.avail_in
= PyString_GET_SIZE(self
->unconsumed_tail
);
834 self
->zst
.next_in
= (Byte
*)PyString_AS_STRING(self
->unconsumed_tail
);
835 self
->zst
.avail_out
= length
;
836 self
->zst
.next_out
= (Byte
*)PyString_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 (_PyString_Resize(&retval
, length
<< 1) < 0)
847 self
->zst
.next_out
= (Byte
*)PyString_AS_STRING(retval
) + length
;
848 self
->zst
.avail_out
= length
;
849 length
= length
<< 1;
851 Py_BEGIN_ALLOW_THREADS
852 err
= inflate(&(self
->zst
), Z_FINISH
);
856 if (save_unconsumed_input(self
, err
) < 0) {
862 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
863 various data structures. Note we should only get Z_STREAM_END when
864 flushmode is Z_FINISH */
865 if (err
== Z_STREAM_END
) {
866 err
= inflateEnd(&(self
->zst
));
867 self
->is_initialised
= 0;
869 zlib_error(self
->zst
, err
, "from inflateEnd()");
876 _PyString_Resize(&retval
, self
->zst
.total_out
- start_total_out
);
885 static PyMethodDef comp_methods
[] =
887 {"compress", (binaryfunc
)PyZlib_objcompress
, METH_VARARGS
,
888 comp_compress__doc__
},
889 {"flush", (binaryfunc
)PyZlib_flush
, METH_VARARGS
,
891 #ifdef HAVE_ZLIB_COPY
892 {"copy", (PyCFunction
)PyZlib_copy
, METH_NOARGS
,
898 static PyMethodDef Decomp_methods
[] =
900 {"decompress", (binaryfunc
)PyZlib_objdecompress
, METH_VARARGS
,
901 decomp_decompress__doc__
},
902 {"flush", (binaryfunc
)PyZlib_unflush
, METH_VARARGS
,
903 decomp_flush__doc__
},
904 #ifdef HAVE_ZLIB_COPY
905 {"copy", (PyCFunction
)PyZlib_uncopy
, METH_NOARGS
,
912 Comp_getattr(compobject
*self
, char *name
)
914 /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
917 return Py_FindMethod(comp_methods
, (PyObject
*)self
, name
);
921 Decomp_getattr(compobject
*self
, char *name
)
927 if (strcmp(name
, "unused_data") == 0) {
928 Py_INCREF(self
->unused_data
);
929 retval
= self
->unused_data
;
930 } else if (strcmp(name
, "unconsumed_tail") == 0) {
931 Py_INCREF(self
->unconsumed_tail
);
932 retval
= self
->unconsumed_tail
;
934 retval
= Py_FindMethod(Decomp_methods
, (PyObject
*)self
, name
);
941 PyDoc_STRVAR(adler32__doc__
,
942 "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
944 "An optional starting value can be specified. The returned checksum is\n"
945 "a signed integer.");
948 PyZlib_adler32(PyObject
*self
, PyObject
*args
)
950 unsigned int adler32val
= 1; /* adler32(0L, Z_NULL, 0) */
954 if (!PyArg_ParseTuple(args
, "s#|I:adler32", &buf
, &len
, &adler32val
))
956 /* In Python 2.x we return a signed integer regardless of native platform
957 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
958 * extended into a 64-bit long inside the integer object). 3.0 does the
959 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
960 signed_val
= adler32(adler32val
, buf
, len
);
961 return PyInt_FromLong(signed_val
);
964 PyDoc_STRVAR(crc32__doc__
,
965 "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
967 "An optional starting value can be specified. The returned checksum is\n"
968 "a signed integer.");
971 PyZlib_crc32(PyObject
*self
, PyObject
*args
)
973 unsigned int crc32val
= 0; /* crc32(0L, Z_NULL, 0) */
977 if (!PyArg_ParseTuple(args
, "s#|I:crc32", &buf
, &len
, &crc32val
))
979 /* In Python 2.x we return a signed integer regardless of native platform
980 * long size (the 32bit unsigned long is treated as 32-bit signed and sign
981 * extended into a 64-bit long inside the integer object). 3.0 does the
982 * right thing and returns unsigned. http://bugs.python.org/issue1202 */
983 signed_val
= crc32(crc32val
, buf
, len
);
984 return PyInt_FromLong(signed_val
);
988 static PyMethodDef zlib_methods
[] =
990 {"adler32", (PyCFunction
)PyZlib_adler32
, METH_VARARGS
,
992 {"compress", (PyCFunction
)PyZlib_compress
, METH_VARARGS
,
994 {"compressobj", (PyCFunction
)PyZlib_compressobj
, METH_VARARGS
,
996 {"crc32", (PyCFunction
)PyZlib_crc32
, METH_VARARGS
,
998 {"decompress", (PyCFunction
)PyZlib_decompress
, METH_VARARGS
,
1000 {"decompressobj", (PyCFunction
)PyZlib_decompressobj
, METH_VARARGS
,
1001 decompressobj__doc__
},
1005 static PyTypeObject Comptype
= {
1006 PyVarObject_HEAD_INIT(0, 0)
1010 (destructor
)Comp_dealloc
, /*tp_dealloc*/
1012 (getattrfunc
)Comp_getattr
, /*tp_getattr*/
1017 0, /*tp_as_sequence*/
1018 0, /*tp_as_mapping*/
1021 static PyTypeObject Decomptype
= {
1022 PyVarObject_HEAD_INIT(0, 0)
1026 (destructor
)Decomp_dealloc
, /*tp_dealloc*/
1028 (getattrfunc
)Decomp_getattr
, /*tp_getattr*/
1033 0, /*tp_as_sequence*/
1034 0, /*tp_as_mapping*/
1037 PyDoc_STRVAR(zlib_module_documentation
,
1038 "The functions in this module allow compression and decompression using the\n"
1039 "zlib library, which is based on GNU zip.\n"
1041 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1042 "compress(string[, level]) -- Compress string, with compression level in 0-9.\n"
1043 "compressobj([level]) -- Return a compressor object.\n"
1044 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1045 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1046 "decompressobj([wbits]) -- Return a decompressor object.\n"
1048 "'wbits' is window buffer size.\n"
1049 "Compressor objects support compress() and flush() methods; decompressor\n"
1050 "objects support decompress() and flush().");
1056 Py_TYPE(&Comptype
) = &PyType_Type
;
1057 Py_TYPE(&Decomptype
) = &PyType_Type
;
1058 m
= Py_InitModule4("zlib", zlib_methods
,
1059 zlib_module_documentation
,
1060 (PyObject
*)NULL
,PYTHON_API_VERSION
);
1064 ZlibError
= PyErr_NewException("zlib.error", NULL
, NULL
);
1065 if (ZlibError
!= NULL
) {
1066 Py_INCREF(ZlibError
);
1067 PyModule_AddObject(m
, "error", ZlibError
);
1069 PyModule_AddIntConstant(m
, "MAX_WBITS", MAX_WBITS
);
1070 PyModule_AddIntConstant(m
, "DEFLATED", DEFLATED
);
1071 PyModule_AddIntConstant(m
, "DEF_MEM_LEVEL", DEF_MEM_LEVEL
);
1072 PyModule_AddIntConstant(m
, "Z_BEST_SPEED", Z_BEST_SPEED
);
1073 PyModule_AddIntConstant(m
, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION
);
1074 PyModule_AddIntConstant(m
, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION
);
1075 PyModule_AddIntConstant(m
, "Z_FILTERED", Z_FILTERED
);
1076 PyModule_AddIntConstant(m
, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY
);
1077 PyModule_AddIntConstant(m
, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY
);
1079 PyModule_AddIntConstant(m
, "Z_FINISH", Z_FINISH
);
1080 PyModule_AddIntConstant(m
, "Z_NO_FLUSH", Z_NO_FLUSH
);
1081 PyModule_AddIntConstant(m
, "Z_SYNC_FLUSH", Z_SYNC_FLUSH
);
1082 PyModule_AddIntConstant(m
, "Z_FULL_FLUSH", Z_FULL_FLUSH
);
1084 ver
= PyString_FromString(ZLIB_VERSION
);
1086 PyModule_AddObject(m
, "ZLIB_VERSION", ver
);
1088 PyModule_AddStringConstant(m
, "__version__", "1.0");
1091 zlib_lock
= PyThread_allocate_lock();
1092 #endif /* WITH_THREAD */