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");
83 zst
.zalloc
=(alloc_func
)NULL
;
84 zst
.zfree
=(free_func
)Z_NULL
;
85 zst
.next_out
=(Byte
*)output
;
86 zst
.next_in
=(Byte
*)input
;
88 err
=deflateInit(&zst
, level
);
94 PyErr_SetString(PyExc_MemoryError
,
95 "Out of memory while compressing data");
99 PyErr_SetString(ZlibError
,
100 "Bad compression level");
105 if (zst
.msg
== Z_NULL
)
106 PyErr_Format(ZlibError
, "Error %i while compressing data",
109 PyErr_Format(ZlibError
, "Error %i while compressing data: %.200s",
117 err
=deflate(&zst
, Z_FINISH
);
122 /* Are there other errors to be trapped here? */
125 if (zst
.msg
== Z_NULL
)
126 PyErr_Format(ZlibError
, "Error %i while compressing data",
129 PyErr_Format(ZlibError
, "Error %i while compressing data: %.200s",
136 err
=deflateEnd(&zst
);
139 if (zst
.msg
== Z_NULL
)
140 PyErr_Format(ZlibError
, "Error %i while finishing compression",
143 PyErr_Format(ZlibError
,
144 "Error %i while finishing compression: %.200s",
149 ReturnVal
=PyString_FromStringAndSize((char *)output
, zst
.total_out
);
154 static char decompress__doc__
[] =
155 "decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
156 "decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
157 "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"
161 PyZlib_decompress(self
, args
)
165 PyObject
*result_str
;
168 int wsize
=DEF_WBITS
, r_strlen
=DEFAULTALLOC
;
170 if (!PyArg_ParseTuple(args
, "s#|ii", &input
, &length
, &wsize
, &r_strlen
))
177 zst
.avail_out
=r_strlen
;
178 if (!(result_str
= PyString_FromStringAndSize(NULL
, r_strlen
)))
180 PyErr_SetString(PyExc_MemoryError
,
181 "Can't allocate memory to decompress data");
184 zst
.zalloc
=(alloc_func
)NULL
;
185 zst
.zfree
=(free_func
)Z_NULL
;
186 zst
.next_out
=(Byte
*)PyString_AsString(result_str
);
187 zst
.next_in
=(Byte
*)input
;
188 err
=inflateInit2(&zst
, wsize
);
194 PyErr_SetString(PyExc_MemoryError
,
195 "Out of memory while decompressing data");
196 Py_DECREF(result_str
);
200 if (zst
.msg
== Z_NULL
)
201 PyErr_Format(ZlibError
, "Error %i preparing to decompress data",
204 PyErr_Format(ZlibError
,
205 "Error %i while preparing to decompress data: %.200s",
208 Py_DECREF(result_str
);
214 err
=inflate(&zst
, Z_FINISH
);
221 /* need more memory */
222 if (_PyString_Resize(&result_str
, r_strlen
<< 1) == -1)
224 PyErr_SetString(PyExc_MemoryError
,
225 "Out of memory while decompressing data");
227 Py_DECREF(result_str
);
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");
413 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
414 self
->zst
.avail_out
= length
;
415 length
= length
<< 1;
420 if (self
->zst
.msg
== Z_NULL
)
421 PyErr_Format(ZlibError
, "Error %i while compressing",
424 PyErr_Format(ZlibError
, "Error %i while compressing: %.200s",
429 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
433 static char decomp_decompress__doc__
[] =
434 "decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
435 "After calling this function, some of the input data may still\n"
436 "be stored in internal buffers for later processing.\n"
437 "Call the flush() method to clear these buffers."
441 PyZlib_objdecompress(self
, args
)
445 int length
, err
, inplen
;
448 unsigned long start_total_out
;
450 if (!PyArg_ParseTuple(args
, "s#", &input
, &inplen
))
452 start_total_out
= self
->zst
.total_out
;
453 RetVal
= PyString_FromStringAndSize(NULL
, DEFAULTALLOC
);
454 self
->zst
.avail_in
= inplen
;
455 self
->zst
.next_in
= input
;
456 self
->zst
.avail_out
= length
= DEFAULTALLOC
;
457 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
);
460 while (self
->zst
.avail_in
!= 0 && err
== Z_OK
)
462 err
= inflate(&(self
->zst
), Z_NO_FLUSH
);
463 if (err
== Z_OK
&& self
->zst
.avail_out
<= 0)
465 if (_PyString_Resize(&RetVal
, length
<< 1) == -1)
467 PyErr_SetString(PyExc_MemoryError
,
468 "Can't allocate memory to compress data");
472 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
473 self
->zst
.avail_out
= length
;
474 length
= length
<< 1;
478 if (err
!= Z_OK
&& err
!= Z_STREAM_END
)
480 if (self
->zst
.msg
== Z_NULL
)
481 PyErr_Format(ZlibError
, "Error %i while decompressing",
484 PyErr_Format(ZlibError
, "Error %i while decompressing: %.200s",
489 _PyString_Resize(&RetVal
, self
->zst
.total_out
- start_total_out
);
493 static char comp_flush__doc__
[] =
494 "flush() -- Return a string containing any remaining compressed data. "
495 "The compressor object can no longer be used after this call."
499 PyZlib_flush(self
, args
)
503 int length
=DEFAULTALLOC
, err
= Z_OK
;
506 if (!PyArg_NoArgs(args
))
508 self
->zst
.avail_in
= 0;
509 self
->zst
.next_in
= Z_NULL
;
510 if (!(RetVal
= PyString_FromStringAndSize(NULL
, length
))) {
511 PyErr_SetString(PyExc_MemoryError
,
512 "Can't allocate memory to compress data");
515 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
);
516 self
->zst
.avail_out
= length
;
519 err
= deflate(&(self
->zst
), Z_FINISH
);
520 if (self
->zst
.avail_out
<= 0) {
521 if (_PyString_Resize(&RetVal
, length
<< 1) == -1) {
522 PyErr_SetString(PyExc_MemoryError
,
523 "Can't allocate memory to compress data");
527 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
528 self
->zst
.avail_out
= length
;
529 length
= length
<< 1;
532 if (err
!=Z_STREAM_END
) {
533 if (self
->zst
.msg
== Z_NULL
)
534 PyErr_Format(ZlibError
, "Error %i while compressing",
537 PyErr_Format(ZlibError
, "Error %i while compressing: %.200s",
542 err
=deflateEnd(&(self
->zst
));
544 if (self
->zst
.msg
== Z_NULL
)
545 PyErr_Format(ZlibError
, "Error %i while flushing compression object",
548 PyErr_Format(ZlibError
,
549 "Error %i while flushing compression object: %.200s",
554 _PyString_Resize(&RetVal
,
555 (char *)self
->zst
.next_out
- PyString_AsString(RetVal
));
559 static char decomp_flush__doc__
[] =
560 "flush() -- Return a string containing any remaining decompressed data. "
561 "The decompressor object can no longer be used after this call."
565 PyZlib_unflush(self
, args
)
572 if (!PyArg_NoArgs(args
))
574 if (!(RetVal
= PyString_FromStringAndSize(NULL
, DEFAULTALLOC
)))
576 PyErr_SetString(PyExc_MemoryError
,
577 "Can't allocate memory to decompress data");
580 self
->zst
.avail_in
=0;
581 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
);
582 length
= self
->zst
.avail_out
= DEFAULTALLOC
;
587 err
= inflate(&(self
->zst
), Z_FINISH
);
588 if (err
== Z_OK
&& self
->zst
.avail_out
== 0)
590 if (_PyString_Resize(&RetVal
, length
<< 1) == -1)
592 PyErr_SetString(PyExc_MemoryError
,
593 "Can't allocate memory to decompress data");
597 self
->zst
.next_out
= (unsigned char *)PyString_AsString(RetVal
) + length
;
598 self
->zst
.avail_out
= length
;
599 length
= length
<< 1;
602 if (err
!=Z_STREAM_END
)
604 if (self
->zst
.msg
== Z_NULL
)
605 PyErr_Format(ZlibError
, "Error %i while decompressing",
608 PyErr_Format(ZlibError
, "Error %i while decompressing: %.200s",
613 err
=inflateEnd(&(self
->zst
));
616 if (self
->zst
.msg
== Z_NULL
)
617 PyErr_Format(ZlibError
,
618 "Error %i while flushing decompression object",
621 PyErr_Format(ZlibError
,
622 "Error %i while flushing decompression object: %.200s",
627 _PyString_Resize(&RetVal
,
628 (char *)self
->zst
.next_out
- PyString_AsString(RetVal
));
632 static PyMethodDef comp_methods
[] =
634 {"compress", (binaryfunc
)PyZlib_objcompress
, 1, comp_compress__doc__
},
635 {"flush", (binaryfunc
)PyZlib_flush
, 0, comp_flush__doc__
},
639 static PyMethodDef Decomp_methods
[] =
641 {"decompress", (binaryfunc
)PyZlib_objdecompress
, 1, decomp_decompress__doc__
},
642 {"flush", (binaryfunc
)PyZlib_unflush
, 0, decomp_flush__doc__
},
647 Comp_getattr(self
, name
)
651 return Py_FindMethod(comp_methods
, (PyObject
*)self
, name
);
655 Decomp_getattr(self
, name
)
659 return Py_FindMethod(Decomp_methods
, (PyObject
*)self
, name
);
662 static char adler32__doc__
[] =
663 "adler32(string) -- Compute an Adler-32 checksum of string, using "
664 "a default starting value, and returning an integer value.\n"
665 "adler32(string, value) -- Compute an Adler-32 checksum of string, using "
666 "the starting value provided, and returning an integer value\n"
670 PyZlib_adler32(self
, args
)
671 PyObject
*self
, *args
;
673 uLong adler32val
=adler32(0L, Z_NULL
, 0);
677 if (!PyArg_ParseTuple(args
, "s#|l", &buf
, &len
, &adler32val
))
681 adler32val
= adler32(adler32val
, buf
, len
);
682 return PyInt_FromLong(adler32val
);
685 static char crc32__doc__
[] =
686 "crc32(string) -- Compute a CRC-32 checksum of string, using "
687 "a default starting value, and returning an integer value.\n"
688 "crc32(string, value) -- Compute a CRC-32 checksum of string, using "
689 "the starting value provided, and returning an integer value.\n"
693 PyZlib_crc32(self
, args
)
694 PyObject
*self
, *args
;
696 uLong crc32val
=crc32(0L, Z_NULL
, 0);
699 if (!PyArg_ParseTuple(args
, "s#|l", &buf
, &len
, &crc32val
))
703 crc32val
= crc32(crc32val
, buf
, len
);
704 return PyInt_FromLong(crc32val
);
708 static PyMethodDef zlib_methods
[] =
710 {"adler32", (PyCFunction
)PyZlib_adler32
, 1, adler32__doc__
},
711 {"compress", (PyCFunction
)PyZlib_compress
, 1, compress__doc__
},
712 {"compressobj", (PyCFunction
)PyZlib_compressobj
, 1, compressobj__doc__
},
713 {"crc32", (PyCFunction
)PyZlib_crc32
, 1, crc32__doc__
},
714 {"decompress", (PyCFunction
)PyZlib_decompress
, 1, decompress__doc__
},
715 {"decompressobj", (PyCFunction
)PyZlib_decompressobj
, 1, decompressobj__doc__
},
719 statichere PyTypeObject Comptype
= {
720 PyObject_HEAD_INIT(0)
725 (destructor
)Comp_dealloc
, /*tp_dealloc*/
727 (getattrfunc
)Comp_getattr
, /*tp_getattr*/
732 0, /*tp_as_sequence*/
736 statichere PyTypeObject Decomptype
= {
737 PyObject_HEAD_INIT(0)
742 (destructor
)Decomp_dealloc
, /*tp_dealloc*/
744 (getattrfunc
)Decomp_getattr
, /*tp_getattr*/
749 0, /*tp_as_sequence*/
753 /* The following insint() routine was blatantly ripped off from
756 /* Convenience routine to export an integer value.
757 For simplicity, errors (which are unlikely anyway) are ignored. */
759 insint(d
, name
, value
)
764 PyObject
*v
= PyInt_FromLong((long) value
);
766 /* Don't bother reporting this error */
770 PyDict_SetItemString(d
, name
, v
);
775 static char zlib_module_documentation
[]=
776 "The functions in this module allow compression and decompression "
777 "using the zlib library, which is based on GNU zip. \n\n"
778 "adler32(string) -- Compute an Adler-32 checksum.\n"
779 "adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
780 "compress(string) -- Compress a string.\n"
781 "compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
782 "compressobj([level]) -- Return a compressor object.\n"
783 "crc32(string) -- Compute a CRC-32 checksum.\n"
784 "crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
785 "decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n"
786 "decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
787 "Compressor objects support compress() and flush() methods; decompressor \n"
788 "objects support decompress() and flush()."
794 PyObject
*m
, *d
, *ver
;
795 Comptype
.ob_type
= &PyType_Type
;
796 Decomptype
.ob_type
= &PyType_Type
;
797 m
= Py_InitModule4("zlib", zlib_methods
,
798 zlib_module_documentation
,
799 (PyObject
*)NULL
,PYTHON_API_VERSION
);
800 d
= PyModule_GetDict(m
);
801 ZlibError
= PyErr_NewException("zlib.error", NULL
, NULL
);
802 PyDict_SetItemString(d
, "error", ZlibError
);
804 insint(d
, "MAX_WBITS", MAX_WBITS
);
805 insint(d
, "DEFLATED", DEFLATED
);
806 insint(d
, "DEF_MEM_LEVEL", DEF_MEM_LEVEL
);
807 insint(d
, "Z_BEST_SPEED", Z_BEST_SPEED
);
808 insint(d
, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION
);
809 insint(d
, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION
);
810 insint(d
, "Z_FILTERED", Z_FILTERED
);
811 insint(d
, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY
);
812 insint(d
, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY
);
813 ver
= PyString_FromString(ZLIB_VERSION
);
814 PyDict_SetItemString(d
, "ZLIB_VERSION", ver
);