Improved some error messages for command line processing.
[python/dscho.git] / Modules / zlibmodule.c
blobfc892d064b3021e196ba0608fc34fa24fe998f14
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 */
5 #include "Python.h"
6 #ifdef MS_WIN32
7 #define ZLIB_DLL
8 #endif
9 #include "zlib.h"
11 /* The following parameters are copied from zutil.h, version 0.95 */
12 #define DEFLATED 8
13 #if MAX_MEM_LEVEL >= 8
14 # define DEF_MEM_LEVEL 8
15 #else
16 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
17 #endif
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;
29 typedef struct
31 PyObject_HEAD
32 z_stream zst;
33 } compobject;
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"
45 static compobject *
46 newcompobject(type)
47 PyTypeObject *type;
49 compobject *self;
50 self = PyObject_NEW(compobject, type);
51 if (self == NULL)
52 return NULL;
53 return self;
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"
63 static PyObject *
64 PyZlib_compress(self, args)
65 PyObject *self;
66 PyObject *args;
68 PyObject *ReturnVal;
69 Byte *input, *output;
70 int length, level=Z_DEFAULT_COMPRESSION, err;
71 z_stream zst;
73 if (!PyArg_ParseTuple(args, "s#|i", &input, &length, &level))
74 return NULL;
75 zst.avail_out = length + length/1000 + 12 + 1;
76 output=(Byte*)malloc(zst.avail_out);
77 if (output==NULL)
79 PyErr_SetString(PyExc_MemoryError,
80 "Can't allocate memory to compress data");
81 return NULL;
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;
87 zst.avail_in=length;
88 err=deflateInit(&zst, level);
89 switch(err)
91 case(Z_OK):
92 break;
93 case(Z_MEM_ERROR):
94 PyErr_SetString(PyExc_MemoryError,
95 "Out of memory while compressing data");
96 free(output);
97 return NULL;
98 case(Z_STREAM_ERROR):
99 PyErr_SetString(ZlibError,
100 "Bad compression level");
101 free(output);
102 return NULL;
103 default:
105 if (zst.msg == Z_NULL)
106 PyErr_Format(ZlibError, "Error %i while compressing data",
107 err);
108 else
109 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
110 err, zst.msg);
111 deflateEnd(&zst);
112 free(output);
113 return NULL;
117 err=deflate(&zst, Z_FINISH);
118 switch(err)
120 case(Z_STREAM_END):
121 break;
122 /* Are there other errors to be trapped here? */
123 default:
125 if (zst.msg == Z_NULL)
126 PyErr_Format(ZlibError, "Error %i while compressing data",
127 err);
128 else
129 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
130 err, zst.msg);
131 deflateEnd(&zst);
132 free(output);
133 return NULL;
136 err=deflateEnd(&zst);
137 if (err!=Z_OK)
139 if (zst.msg == Z_NULL)
140 PyErr_Format(ZlibError, "Error %i while finishing compression",
141 err);
142 else
143 PyErr_Format(ZlibError,
144 "Error %i while finishing compression: %.200s",
145 err, zst.msg);
146 free(output);
147 return NULL;
149 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
150 free(output);
151 return ReturnVal;
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"
160 static PyObject *
161 PyZlib_decompress(self, args)
162 PyObject *self;
163 PyObject *args;
165 PyObject *result_str;
166 Byte *input;
167 int length, err;
168 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
169 z_stream zst;
170 if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen))
171 return NULL;
173 if (r_strlen <= 0)
174 r_strlen = 1;
176 zst.avail_in=length;
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");
182 return NULL;
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);
189 switch(err)
191 case(Z_OK):
192 break;
193 case(Z_MEM_ERROR):
194 PyErr_SetString(PyExc_MemoryError,
195 "Out of memory while decompressing data");
196 Py_DECREF(result_str);
197 return NULL;
198 default:
200 if (zst.msg == Z_NULL)
201 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
202 err);
203 else
204 PyErr_Format(ZlibError,
205 "Error %i while preparing to decompress data: %.200s",
206 err, zst.msg);
207 inflateEnd(&zst);
208 Py_DECREF(result_str);
209 return NULL;
214 err=inflate(&zst, Z_FINISH);
215 switch(err)
217 case(Z_STREAM_END):
218 break;
219 case(Z_BUF_ERROR):
220 case(Z_OK):
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");
226 inflateEnd(&zst);
227 Py_DECREF(result_str);
228 return NULL;
230 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
231 zst.avail_out=r_strlen;
232 r_strlen = r_strlen << 1;
233 break;
234 default:
236 if (zst.msg == Z_NULL)
237 PyErr_Format(ZlibError, "Error %i while decompressing data",
238 err);
239 else
240 PyErr_Format(ZlibError,
241 "Error %i while decompressing data: %.200s",
242 err, zst.msg);
243 inflateEnd(&zst);
244 Py_DECREF(result_str);
245 return NULL;
248 } while(err!=Z_STREAM_END);
250 err=inflateEnd(&zst);
251 if (err!=Z_OK)
253 if (zst.msg == Z_NULL)
254 PyErr_Format(ZlibError,
255 "Error %i while finishing data decompression",
256 err);
257 else
258 PyErr_Format(ZlibError,
259 "Error %i while finishing data decompression: %.200s",
260 err, zst.msg);
261 Py_DECREF(result_str);
262 return NULL;
264 _PyString_Resize(&result_str, zst.total_out);
265 return result_str;
268 static PyObject *
269 PyZlib_compressobj(selfptr, args)
270 PyObject *selfptr;
271 PyObject *args;
273 compobject *self;
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))
279 return NULL;
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);
286 switch(err)
288 case (Z_OK):
289 return (PyObject*)self;
290 case (Z_MEM_ERROR):
291 PyErr_SetString(PyExc_MemoryError,
292 "Can't allocate memory for compression object");
293 return NULL;
294 case(Z_STREAM_ERROR):
295 PyErr_SetString(PyExc_ValueError,
296 "Invalid initialization option");
297 return NULL;
298 default:
300 if (self->zst.msg == Z_NULL)
301 PyErr_Format(ZlibError,
302 "Error %i while creating compression object",
303 err);
304 else
305 PyErr_Format(ZlibError,
306 "Error %i while creating compression object: %.200s",
307 err, self->zst.msg);
308 return NULL;
313 static PyObject *
314 PyZlib_decompressobj(selfptr, args)
315 PyObject *selfptr;
316 PyObject *args;
318 int wbits=DEF_WBITS, err;
319 compobject *self;
320 if (!PyArg_ParseTuple(args, "|i", &wbits))
322 return NULL;
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);
329 switch(err)
331 case (Z_OK):
332 return (PyObject*)self;
333 case(Z_STREAM_ERROR):
334 PyErr_SetString(PyExc_ValueError,
335 "Invalid initialization option");
336 return NULL;
337 case (Z_MEM_ERROR):
338 PyErr_SetString(PyExc_MemoryError,
339 "Can't allocate memory for decompression object");
340 return NULL;
341 default:
343 if (self->zst.msg == Z_NULL)
344 PyErr_Format(ZlibError,
345 "Error %i while creating decompression object",
346 err);
347 else
348 PyErr_Format(ZlibError,
349 "Error %i while creating decompression object: %.200s",
350 err, self->zst.msg);
351 return NULL;
356 static void
357 Comp_dealloc(self)
358 compobject *self;
360 deflateEnd(&self->zst);
361 PyMem_DEL(self);
364 static void
365 Decomp_dealloc(self)
366 compobject *self;
368 inflateEnd(&self->zst);
369 PyMem_DEL(self);
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."
380 static PyObject *
381 PyZlib_objcompress(self, args)
382 compobject *self;
383 PyObject *args;
385 int err = Z_OK, inplen;
386 int length = DEFAULTALLOC;
387 PyObject *RetVal;
388 Byte *input;
389 unsigned long start_total_out;
391 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
392 return NULL;
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");
398 return NULL;
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");
410 Py_DECREF(RetVal);
411 return NULL;
413 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
414 self->zst.avail_out = length;
415 length = length << 1;
418 if (err != Z_OK)
420 if (self->zst.msg == Z_NULL)
421 PyErr_Format(ZlibError, "Error %i while compressing",
422 err);
423 else
424 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
425 err, self->zst.msg);
426 Py_DECREF(RetVal);
427 return NULL;
429 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
430 return RetVal;
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."
440 static PyObject *
441 PyZlib_objdecompress(self, args)
442 compobject *self;
443 PyObject *args;
445 int length, err, inplen;
446 PyObject *RetVal;
447 Byte *input;
448 unsigned long start_total_out;
450 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
451 return NULL;
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);
458 err = Z_OK;
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");
469 Py_DECREF(RetVal);
470 return NULL;
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",
482 err);
483 else
484 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
485 err, self->zst.msg);
486 Py_DECREF(RetVal);
487 return NULL;
489 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
490 return RetVal;
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."
498 static PyObject *
499 PyZlib_flush(self, args)
500 compobject *self;
501 PyObject *args;
503 int length=DEFAULTALLOC, err = Z_OK;
504 PyObject *RetVal;
506 if (!PyArg_NoArgs(args))
507 return NULL;
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");
513 return NULL;
515 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
516 self->zst.avail_out = length;
517 while (err == Z_OK)
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");
524 Py_DECREF(RetVal);
525 return NULL;
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",
535 err);
536 else
537 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
538 err, self->zst.msg);
539 Py_DECREF(RetVal);
540 return NULL;
542 err=deflateEnd(&(self->zst));
543 if (err!=Z_OK) {
544 if (self->zst.msg == Z_NULL)
545 PyErr_Format(ZlibError, "Error %i while flushing compression object",
546 err);
547 else
548 PyErr_Format(ZlibError,
549 "Error %i while flushing compression object: %.200s",
550 err, self->zst.msg);
551 Py_DECREF(RetVal);
552 return NULL;
554 _PyString_Resize(&RetVal,
555 (char *)self->zst.next_out - PyString_AsString(RetVal));
556 return 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."
564 static PyObject *
565 PyZlib_unflush(self, args)
566 compobject *self;
567 PyObject *args;
569 int length=0, err;
570 PyObject *RetVal;
572 if (!PyArg_NoArgs(args))
573 return NULL;
574 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
576 PyErr_SetString(PyExc_MemoryError,
577 "Can't allocate memory to decompress data");
578 return NULL;
580 self->zst.avail_in=0;
581 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
582 length = self->zst.avail_out = DEFAULTALLOC;
584 err = Z_OK;
585 while (err == Z_OK)
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");
594 Py_DECREF(RetVal);
595 return NULL;
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",
606 err);
607 else
608 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
609 err, self->zst.msg);
610 Py_DECREF(RetVal);
611 return NULL;
613 err=inflateEnd(&(self->zst));
614 if (err!=Z_OK)
616 if (self->zst.msg == Z_NULL)
617 PyErr_Format(ZlibError,
618 "Error %i while flushing decompression object",
619 err);
620 else
621 PyErr_Format(ZlibError,
622 "Error %i while flushing decompression object: %.200s",
623 err, self->zst.msg);
624 Py_DECREF(RetVal);
625 return NULL;
627 _PyString_Resize(&RetVal,
628 (char *)self->zst.next_out - PyString_AsString(RetVal));
629 return RetVal;
632 static PyMethodDef comp_methods[] =
634 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
635 {"flush", (binaryfunc)PyZlib_flush, 0, comp_flush__doc__},
636 {NULL, NULL}
639 static PyMethodDef Decomp_methods[] =
641 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
642 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
643 {NULL, NULL}
646 static PyObject *
647 Comp_getattr(self, name)
648 compobject *self;
649 char *name;
651 return Py_FindMethod(comp_methods, (PyObject *)self, name);
654 static PyObject *
655 Decomp_getattr(self, name)
656 compobject *self;
657 char *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"
669 static PyObject *
670 PyZlib_adler32(self, args)
671 PyObject *self, *args;
673 uLong adler32val=adler32(0L, Z_NULL, 0);
674 Byte *buf;
675 int len;
677 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
679 return NULL;
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"
692 static PyObject *
693 PyZlib_crc32(self, args)
694 PyObject *self, *args;
696 uLong crc32val=crc32(0L, Z_NULL, 0);
697 Byte *buf;
698 int len;
699 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
701 return NULL;
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__},
716 {NULL, NULL}
719 statichere PyTypeObject Comptype = {
720 PyObject_HEAD_INIT(0)
722 "Compress",
723 sizeof(compobject),
725 (destructor)Comp_dealloc, /*tp_dealloc*/
726 0, /*tp_print*/
727 (getattrfunc)Comp_getattr, /*tp_getattr*/
728 0, /*tp_setattr*/
729 0, /*tp_compare*/
730 0, /*tp_repr*/
731 0, /*tp_as_number*/
732 0, /*tp_as_sequence*/
733 0, /*tp_as_mapping*/
736 statichere PyTypeObject Decomptype = {
737 PyObject_HEAD_INIT(0)
739 "Decompress",
740 sizeof(compobject),
742 (destructor)Decomp_dealloc, /*tp_dealloc*/
743 0, /*tp_print*/
744 (getattrfunc)Decomp_getattr, /*tp_getattr*/
745 0, /*tp_setattr*/
746 0, /*tp_compare*/
747 0, /*tp_repr*/
748 0, /*tp_as_number*/
749 0, /*tp_as_sequence*/
750 0, /*tp_as_mapping*/
753 /* The following insint() routine was blatantly ripped off from
754 socketmodule.c */
756 /* Convenience routine to export an integer value.
757 For simplicity, errors (which are unlikely anyway) are ignored. */
758 static void
759 insint(d, name, value)
760 PyObject *d;
761 char *name;
762 int value;
764 PyObject *v = PyInt_FromLong((long) value);
765 if (v == NULL) {
766 /* Don't bother reporting this error */
767 PyErr_Clear();
769 else {
770 PyDict_SetItemString(d, name, v);
771 Py_DECREF(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()."
791 void
792 PyInit_zlib()
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);