1 /* zlibmodule.c -- gzip-compatible data compression */
2 /* See http://www.cdrom.com/pub/infozip/zlib/ */
3 /* See http://www.winimage.com/zLibDll for Windows */
11 /* The following parameters are copied from zutil.h, version 0.95 */
13 #if MAX_MEM_LEVEL >= 8
14 # define DEF_MEM_LEVEL 8
16 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
18 #define DEF_WBITS MAX_WBITS
20 /* The output buffer will be increased in chunks of ADDCHUNK bytes. */
21 #define DEFAULTALLOC 16*1024
22 #define PyInit_zlib initzlib
24 staticforward PyTypeObject Comptype
;
25 staticforward PyTypeObject Decomptype
;
27 static PyObject
*ZlibError
;
35 static char compressobj__doc__
[] =
36 "compressobj() -- Return a compressor object.\n"
37 "compressobj(level) -- Return a compressor object, using the given compression level.\n"
40 static char decompressobj__doc__
[] =
41 "decompressobj() -- Return a decompressor object.\n"
42 "decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
50 self
= PyObject_NEW(compobject
, type
);
56 static char compress__doc__
[] =
57 "compress(string) -- Compress string using the default compression level, "
58 "returning a string containing compressed data.\n"
59 "compress(string, level) -- Compress string, using the chosen compression "
60 "level (from 1 to 9). Return a string containing the compressed data.\n"
64 PyZlib_compress(self
, args
)
70 int length
, level
=Z_DEFAULT_COMPRESSION
, err
;
73 if (!PyArg_ParseTuple(args
, "s#|i", &input
, &length
, &level
))
75 zst
.avail_out
= length
+ length
/1000 + 12 + 1;
76 output
=(Byte
*)malloc(zst
.avail_out
);
79 PyErr_SetString(PyExc_MemoryError
,
80 "Can't allocate memory to compress data");
84 zst
.zalloc
=(alloc_func
)NULL
;
85 zst
.zfree
=(free_func
)Z_NULL
;
86 zst
.next_out
=(Byte
*)output
;
87 zst
.next_in
=(Byte
*)input
;
89 err
=deflateInit(&zst
, level
);
95 PyErr_SetString(PyExc_MemoryError
,
96 "Out of memory while compressing data");
100 PyErr_SetString(ZlibError
,
101 "Bad compression level");
106 if (zst
.msg
== Z_NULL
)
107 PyErr_Format(ZlibError
, "Error %i while compressing data",
110 PyErr_Format(ZlibError
, "Error %i while compressing data: %.200s",
118 err
=deflate(&zst
, Z_FINISH
);
123 /* Are there other errors to be trapped here? */
126 if (zst
.msg
== Z_NULL
)
127 PyErr_Format(ZlibError
, "Error %i while compressing data",
130 PyErr_Format(ZlibError
, "Error %i while compressing data: %.200s",
137 err
=deflateEnd(&zst
);
140 if (zst
.msg
== Z_NULL
)
141 PyErr_Format(ZlibError
, "Error %i while finishing compression",
144 PyErr_Format(ZlibError
,
145 "Error %i while finishing compression: %.200s",
150 ReturnVal
=PyString_FromStringAndSize((char *)output
, zst
.total_out
);
155 static char decompress__doc__
[] =
156 "decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
157 "decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
158 "decompress(string, wbits, bufsize) -- Decompress the data in string with a window buffer size of wbits and an initial output buffer size of bufsize.\n"
162 PyZlib_decompress(self
, args
)
166 PyObject
*result_str
;
169 int wsize
=DEF_WBITS
, r_strlen
=DEFAULTALLOC
;
171 if (!PyArg_ParseTuple(args
, "s#|ii", &input
, &length
, &wsize
, &r_strlen
))
178 zst
.avail_out
=r_strlen
;
179 if (!(result_str
= PyString_FromStringAndSize(NULL
, r_strlen
)))
181 PyErr_SetString(PyExc_MemoryError
,
182 "Can't allocate memory to decompress data");
185 zst
.zalloc
=(alloc_func
)NULL
;
186 zst
.zfree
=(free_func
)Z_NULL
;
187 zst
.next_out
=(Byte
*)PyString_AsString(result_str
);
188 zst
.next_in
=(Byte
*)input
;
189 err
=inflateInit2(&zst
, wsize
);
195 PyErr_SetString(PyExc_MemoryError
,
196 "Out of memory while decompressing data");
197 Py_DECREF(result_str
);
201 if (zst
.msg
== Z_NULL
)
202 PyErr_Format(ZlibError
, "Error %i preparing to decompress data",
205 PyErr_Format(ZlibError
,
206 "Error %i while preparing to decompress data: %.200s",
209 Py_DECREF(result_str
);
215 err
=inflate(&zst
, Z_FINISH
);
222 /* need more memory */
223 if (_PyString_Resize(&result_str
, r_strlen
<< 1) == -1)
225 PyErr_SetString(PyExc_MemoryError
,
226 "Out of memory while decompressing data");
230 zst
.next_out
= (unsigned char *)PyString_AsString(result_str
) + r_strlen
;
231 zst
.avail_out
=r_strlen
;
232 r_strlen
= r_strlen
<< 1;
236 if (zst
.msg
== Z_NULL
)
237 PyErr_Format(ZlibError
, "Error %i while decompressing data",
240 PyErr_Format(ZlibError
,
241 "Error %i while decompressing data: %.200s",
244 Py_DECREF(result_str
);
248 } while(err
!=Z_STREAM_END
);
250 err
=inflateEnd(&zst
);
253 if (zst
.msg
== Z_NULL
)
254 PyErr_Format(ZlibError
,
255 "Error %i while finishing data decompression",
258 PyErr_Format(ZlibError
,
259 "Error %i while finishing data decompression: %.200s",
261 Py_DECREF(result_str
);
264 _PyString_Resize(&result_str
, zst
.total_out
);
269 PyZlib_compressobj(selfptr
, args
)
274 int level
=Z_DEFAULT_COMPRESSION
, method
=DEFLATED
;
275 int wbits
=MAX_WBITS
, memLevel
=DEF_MEM_LEVEL
, strategy
=0, err
;
277 if (!PyArg_ParseTuple(args
, "|iiiii", &level
, &method
, &wbits
,
278 &memLevel
, &strategy
))
281 self
= newcompobject(&Comptype
);
282 if (self
==NULL
) return(NULL
);
283 self
->zst
.zalloc
= (alloc_func
)NULL
;
284 self
->zst
.zfree
= (free_func
)Z_NULL
;
285 err
= deflateInit2(&self
->zst
, level
, method
, wbits
, memLevel
, strategy
);
289 return (PyObject
*)self
;
291 PyErr_SetString(PyExc_MemoryError
,
292 "Can't allocate memory for compression object");
294 case(Z_STREAM_ERROR
):
295 PyErr_SetString(PyExc_ValueError
,
296 "Invalid initialization option");
300 if (self
->zst
.msg
== Z_NULL
)
301 PyErr_Format(ZlibError
,
302 "Error %i while creating compression object",
305 PyErr_Format(ZlibError
,
306 "Error %i while creating compression object: %.200s",
314 PyZlib_decompressobj(selfptr
, args
)
318 int wbits
=DEF_WBITS
, err
;
320 if (!PyArg_ParseTuple(args
, "|i", &wbits
))
324 self
=newcompobject(&Decomptype
);
325 if (self
==NULL
) return(NULL
);
326 self
->zst
.zalloc
=(alloc_func
)NULL
;
327 self
->zst
.zfree
=(free_func
)Z_NULL
;
328 err
=inflateInit2(&self
->zst
, wbits
);
332 return (PyObject
*)self
;
333 case(Z_STREAM_ERROR
):
334 PyErr_SetString(PyExc_ValueError
,
335 "Invalid initialization option");
338 PyErr_SetString(PyExc_MemoryError
,
339 "Can't allocate memory for decompression object");
343 if (self
->zst
.msg
== Z_NULL
)
344 PyErr_Format(ZlibError
,
345 "Error %i while creating decompression object",
348 PyErr_Format(ZlibError
,
349 "Error %i while creating decompression object: %.200s",
360 deflateEnd(&self
->zst
);
368 inflateEnd(&self
->zst
);
372 static char comp_compress__doc__
[] =
373 "compress(data) -- Return a string containing a compressed version of the data.\n\n"
374 "After calling this function, some of the input data may still\n"
375 "be stored in internal buffers for later processing.\n"
376 "Call the flush() method to clear these buffers."
381 PyZlib_objcompress(self
, args
)
385 int err
= Z_OK
, inplen
;
386 int length
= DEFAULTALLOC
;
389 unsigned long start_total_out
;
391 if (!PyArg_ParseTuple(args
, "s#", &input
, &inplen
))
393 self
->zst
.avail_in
= inplen
;
394 self
->zst
.next_in
= input
;
395 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
))) {
396 PyErr_SetString(PyExc_MemoryError
,
397 "Can't allocate memory to compress data");
400 start_total_out
= self
->zst
.total_out
;
401 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
);
402 self
->zst
.avail_out
= length
;
403 while (self
->zst
.avail_in
!= 0 && err
== Z_OK
)
405 err
= deflate(&(self
->zst
), Z_NO_FLUSH
);
406 if (self
->zst
.avail_out
<= 0) {
407 if (_PyString_Resize(&RetVal
, length
<< 1) == -1) {
408 PyErr_SetString(PyExc_MemoryError
,
409 "Can't allocate memory to compress data");
412 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
413 self
->zst
.avail_out
= length
;
414 length
= length
<< 1;
419 if (self
->zst
.msg
== Z_NULL
)
420 PyErr_Format(ZlibError
, "Error %i while compressing",
423 PyErr_Format(ZlibError
, "Error %i while compressing: %.200s",
428 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
432 static char decomp_decompress__doc__
[] =
433 "decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
434 "After calling this function, some of the input data may still\n"
435 "be stored in internal buffers for later processing.\n"
436 "Call the flush() method to clear these buffers."
440 PyZlib_objdecompress(self
, args
)
444 int length
, err
, inplen
;
447 unsigned long start_total_out
;
449 if (!PyArg_ParseTuple(args
, "s#", &input
, &inplen
))
451 start_total_out
= self
->zst
.total_out
;
452 RetVal
= PyString_FromStringAndSize(NULL
, DEFAULTALLOC
);
453 self
->zst
.avail_in
= inplen
;
454 self
->zst
.next_in
= input
;
455 self
->zst
.avail_out
= length
= DEFAULTALLOC
;
456 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
);
459 while (self
->zst
.avail_in
!= 0 && err
== Z_OK
)
461 err
= inflate(&(self
->zst
), Z_NO_FLUSH
);
462 if (err
== Z_OK
&& self
->zst
.avail_out
<= 0)
464 if (_PyString_Resize(&RetVal
, length
<< 1) == -1)
466 PyErr_SetString(PyExc_MemoryError
,
467 "Can't allocate memory to compress data");
470 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
471 self
->zst
.avail_out
= length
;
472 length
= length
<< 1;
476 if (err
!= Z_OK
&& err
!= Z_STREAM_END
)
478 if (self
->zst
.msg
== Z_NULL
)
479 PyErr_Format(ZlibError
, "Error %i while decompressing",
482 PyErr_Format(ZlibError
, "Error %i while decompressing: %.200s",
487 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
491 static char comp_flush__doc__
[] =
492 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
493 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
494 "default value used when mode is not specified is Z_FINISH.\n"
495 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
496 "calling the flush() method. Otherwise, more data can still be compressed.\n"
500 PyZlib_flush(self
, args
)
504 int length
=DEFAULTALLOC
, err
= Z_OK
;
506 int flushmode
= Z_FINISH
;
507 unsigned long start_total_out
;
509 if (!PyArg_ParseTuple(args
, "|i", &flushmode
))
512 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
513 doing any work at all; just return an empty string. */
514 if (flushmode
== Z_NO_FLUSH
)
516 return PyString_FromStringAndSize(NULL
, 0);
519 self
->zst
.avail_in
= 0;
520 self
->zst
.next_in
= Z_NULL
;
521 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
))) {
522 PyErr_SetString(PyExc_MemoryError
,
523 "Can't allocate memory to compress data");
526 start_total_out
= self
->zst
.total_out
;
527 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
);
528 self
->zst
.avail_out
= length
;
530 /* When flushing the zstream, there's no input data.
531 If zst.avail_out == 0, that means that more output space is
532 needed to complete the flush operation. */
533 while (err
== Z_OK
) {
534 err
= deflate(&(self
->zst
), Z_FINISH
);
535 if (self
->zst
.avail_out
<= 0) {
536 if (_PyString_Resize(&RetVal
, length
<< 1) == -1) {
537 PyErr_SetString(PyExc_MemoryError
,
538 "Can't allocate memory to compress data");
541 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
542 self
->zst
.avail_out
= length
;
543 length
= length
<< 1;
547 if (err
!= Z_STREAM_END
)
549 if (self
->zst
.msg
== Z_NULL
)
550 PyErr_Format(ZlibError
, "Error %i while flushing",
553 PyErr_Format(ZlibError
, "Error %i while flushing: %.200s",
558 if (flushmode
== Z_FINISH
) {
559 err
=deflateEnd(&(self
->zst
));
561 if (self
->zst
.msg
== Z_NULL
)
562 PyErr_Format(ZlibError
, "Error %i from deflateEnd()",
565 PyErr_Format(ZlibError
,
566 "Error %i from deflateEnd(): %.200s",
572 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
576 static char decomp_flush__doc__
[] =
577 "flush() -- Return a string containing any remaining decompressed data. "
578 "The decompressor object can no longer be used after this call."
582 PyZlib_unflush(self
, args
)
589 if (!PyArg_NoArgs(args
))
591 if (!(RetVal
= PyString_FromStringAndSize(NULL
, DEFAULTALLOC
)))
593 PyErr_SetString(PyExc_MemoryError
,
594 "Can't allocate memory to decompress data");
597 self
->zst
.avail_in
=0;
598 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
);
599 length
= self
->zst
.avail_out
= DEFAULTALLOC
;
604 err
= inflate(&(self
->zst
), Z_FINISH
);
605 if (err
== Z_OK
&& self
->zst
.avail_out
== 0)
607 if (_PyString_Resize(&RetVal
, length
<< 1) == -1)
609 PyErr_SetString(PyExc_MemoryError
,
610 "Can't allocate memory to decompress data");
613 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
614 self
->zst
.avail_out
= length
;
615 length
= length
<< 1;
618 if (err
!=Z_STREAM_END
)
620 if (self
->zst
.msg
== Z_NULL
)
621 PyErr_Format(ZlibError
, "Error %i while decompressing",
624 PyErr_Format(ZlibError
, "Error %i while decompressing: %.200s",
629 err
=inflateEnd(&(self
->zst
));
632 if (self
->zst
.msg
== Z_NULL
)
633 PyErr_Format(ZlibError
,
634 "Error %i while flushing decompression object",
637 PyErr_Format(ZlibError
,
638 "Error %i while flushing decompression object: %.200s",
643 _PyString_Resize(&RetVal
,
644 (char *)self
->zst
.next_out
- PyString_AsString(RetVal
));
648 static PyMethodDef comp_methods
[] =
650 {"compress", (binaryfunc
)PyZlib_objcompress
, 1, comp_compress__doc__
},
651 {"flush", (binaryfunc
)PyZlib_flush
, 1, comp_flush__doc__
},
655 static PyMethodDef Decomp_methods
[] =
657 {"decompress", (binaryfunc
)PyZlib_objdecompress
, 1, decomp_decompress__doc__
},
658 {"flush", (binaryfunc
)PyZlib_unflush
, 0, decomp_flush__doc__
},
663 Comp_getattr(self
, name
)
667 return Py_FindMethod(comp_methods
, (PyObject
*)self
, name
);
671 Decomp_getattr(self
, name
)
675 return Py_FindMethod(Decomp_methods
, (PyObject
*)self
, name
);
678 static char adler32__doc__
[] =
679 "adler32(string) -- Compute an Adler-32 checksum of string, using "
680 "a default starting value, and returning an integer value.\n"
681 "adler32(string, value) -- Compute an Adler-32 checksum of string, using "
682 "the starting value provided, and returning an integer value\n"
686 PyZlib_adler32(self
, args
)
687 PyObject
*self
, *args
;
689 uLong adler32val
=adler32(0L, Z_NULL
, 0);
693 if (!PyArg_ParseTuple(args
, "s#|l", &buf
, &len
, &adler32val
))
697 adler32val
= adler32(adler32val
, buf
, len
);
698 return PyInt_FromLong(adler32val
);
701 static char crc32__doc__
[] =
702 "crc32(string) -- Compute a CRC-32 checksum of string, using "
703 "a default starting value, and returning an integer value.\n"
704 "crc32(string, value) -- Compute a CRC-32 checksum of string, using "
705 "the starting value provided, and returning an integer value.\n"
709 PyZlib_crc32(self
, args
)
710 PyObject
*self
, *args
;
712 uLong crc32val
=crc32(0L, Z_NULL
, 0);
715 if (!PyArg_ParseTuple(args
, "s#|l", &buf
, &len
, &crc32val
))
719 crc32val
= crc32(crc32val
, buf
, len
);
720 return PyInt_FromLong(crc32val
);
724 static PyMethodDef zlib_methods
[] =
726 {"adler32", (PyCFunction
)PyZlib_adler32
, 1, adler32__doc__
},
727 {"compress", (PyCFunction
)PyZlib_compress
, 1, compress__doc__
},
728 {"compressobj", (PyCFunction
)PyZlib_compressobj
, 1, compressobj__doc__
},
729 {"crc32", (PyCFunction
)PyZlib_crc32
, 1, crc32__doc__
},
730 {"decompress", (PyCFunction
)PyZlib_decompress
, 1, decompress__doc__
},
731 {"decompressobj", (PyCFunction
)PyZlib_decompressobj
, 1, decompressobj__doc__
},
735 statichere PyTypeObject Comptype
= {
736 PyObject_HEAD_INIT(0)
741 (destructor
)Comp_dealloc
, /*tp_dealloc*/
743 (getattrfunc
)Comp_getattr
, /*tp_getattr*/
748 0, /*tp_as_sequence*/
752 statichere PyTypeObject Decomptype
= {
753 PyObject_HEAD_INIT(0)
758 (destructor
)Decomp_dealloc
, /*tp_dealloc*/
760 (getattrfunc
)Decomp_getattr
, /*tp_getattr*/
765 0, /*tp_as_sequence*/
769 /* The following insint() routine was blatantly ripped off from
772 /* Convenience routine to export an integer value.
773 For simplicity, errors (which are unlikely anyway) are ignored. */
775 insint(d
, name
, value
)
780 PyObject
*v
= PyInt_FromLong((long) value
);
782 /* Don't bother reporting this error */
786 PyDict_SetItemString(d
, name
, v
);
791 static char zlib_module_documentation
[]=
792 "The functions in this module allow compression and decompression "
793 "using the zlib library, which is based on GNU zip. \n\n"
794 "adler32(string) -- Compute an Adler-32 checksum.\n"
795 "adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
796 "compress(string) -- Compress a string.\n"
797 "compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
798 "compressobj([level]) -- Return a compressor object.\n"
799 "crc32(string) -- Compute a CRC-32 checksum.\n"
800 "crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
801 "decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n"
802 "decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
803 "Compressor objects support compress() and flush() methods; decompressor \n"
804 "objects support decompress() and flush()."
810 PyObject
*m
, *d
, *ver
;
811 Comptype
.ob_type
= &PyType_Type
;
812 Decomptype
.ob_type
= &PyType_Type
;
813 m
= Py_InitModule4("zlib", zlib_methods
,
814 zlib_module_documentation
,
815 (PyObject
*)NULL
,PYTHON_API_VERSION
);
816 d
= PyModule_GetDict(m
);
817 ZlibError
= PyErr_NewException("zlib.error", NULL
, NULL
);
818 PyDict_SetItemString(d
, "error", ZlibError
);
820 insint(d
, "MAX_WBITS", MAX_WBITS
);
821 insint(d
, "DEFLATED", DEFLATED
);
822 insint(d
, "DEF_MEM_LEVEL", DEF_MEM_LEVEL
);
823 insint(d
, "Z_BEST_SPEED", Z_BEST_SPEED
);
824 insint(d
, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION
);
825 insint(d
, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION
);
826 insint(d
, "Z_FILTERED", Z_FILTERED
);
827 insint(d
, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY
);
828 insint(d
, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY
);
830 insint(d
, "Z_FINISH", Z_FINISH
);
831 insint(d
, "Z_NO_FLUSH", Z_NO_FLUSH
);
832 insint(d
, "Z_SYNC_FLUSH", Z_SYNC_FLUSH
);
833 insint(d
, "Z_FULL_FLUSH", Z_FULL_FLUSH
);
835 ver
= PyString_FromString(ZLIB_VERSION
);
836 PyDict_SetItemString(d
, "ZLIB_VERSION", ver
);