Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Modules / zlibmodule.c
blob82863399e5c95987f11fce842382faf33ae00a62
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 DEFAULTALLOC 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 PyObject *unused_data;
34 int is_initialised;
35 } compobject;
37 static char compressobj__doc__[] =
38 "compressobj() -- Return a compressor object.\n"
39 "compressobj(level) -- Return a compressor object, using the given compression level.\n"
42 static char decompressobj__doc__[] =
43 "decompressobj() -- Return a decompressor object.\n"
44 "decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
47 static compobject *
48 newcompobject(type)
49 PyTypeObject *type;
51 compobject *self;
52 self = PyObject_NEW(compobject, type);
53 if (self == NULL)
54 return NULL;
55 self->is_initialised = 0;
56 self->unused_data = PyString_FromString("");
57 return self;
60 static char compress__doc__[] =
61 "compress(string) -- Compress string using the default compression level, "
62 "returning a string containing compressed data.\n"
63 "compress(string, level) -- Compress string, using the chosen compression "
64 "level (from 1 to 9). Return a string containing the compressed data.\n"
67 static PyObject *
68 PyZlib_compress(self, args)
69 PyObject *self;
70 PyObject *args;
72 PyObject *ReturnVal;
73 Byte *input, *output;
74 int length, level=Z_DEFAULT_COMPRESSION, err;
75 z_stream zst;
77 if (!PyArg_ParseTuple(args, "s#|i", &input, &length, &level))
78 return NULL;
79 zst.avail_out = length + length/1000 + 12 + 1;
80 output=(Byte*)malloc(zst.avail_out);
81 if (output==NULL)
83 PyErr_SetString(PyExc_MemoryError,
84 "Can't allocate memory to compress data");
85 return NULL;
88 zst.zalloc=(alloc_func)NULL;
89 zst.zfree=(free_func)Z_NULL;
90 zst.next_out=(Byte *)output;
91 zst.next_in =(Byte *)input;
92 zst.avail_in=length;
93 err=deflateInit(&zst, level);
94 switch(err)
96 case(Z_OK):
97 break;
98 case(Z_MEM_ERROR):
99 PyErr_SetString(PyExc_MemoryError,
100 "Out of memory while compressing data");
101 free(output);
102 return NULL;
103 case(Z_STREAM_ERROR):
104 PyErr_SetString(ZlibError,
105 "Bad compression level");
106 free(output);
107 return NULL;
108 default:
110 if (zst.msg == Z_NULL)
111 PyErr_Format(ZlibError, "Error %i while compressing data",
112 err);
113 else
114 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
115 err, zst.msg);
116 deflateEnd(&zst);
117 free(output);
118 return NULL;
122 err=deflate(&zst, Z_FINISH);
123 switch(err)
125 case(Z_STREAM_END):
126 break;
127 /* Are there other errors to be trapped here? */
128 default:
130 if (zst.msg == Z_NULL)
131 PyErr_Format(ZlibError, "Error %i while compressing data",
132 err);
133 else
134 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
135 err, zst.msg);
136 deflateEnd(&zst);
137 free(output);
138 return NULL;
141 err=deflateEnd(&zst);
142 if (err!=Z_OK)
144 if (zst.msg == Z_NULL)
145 PyErr_Format(ZlibError, "Error %i while finishing compression",
146 err);
147 else
148 PyErr_Format(ZlibError,
149 "Error %i while finishing compression: %.200s",
150 err, zst.msg);
151 free(output);
152 return NULL;
154 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
155 free(output);
156 return ReturnVal;
159 static char decompress__doc__[] =
160 "decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
161 "decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
162 "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"
165 static PyObject *
166 PyZlib_decompress(self, args)
167 PyObject *self;
168 PyObject *args;
170 PyObject *result_str;
171 Byte *input;
172 int length, err;
173 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
174 z_stream zst;
175 if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen))
176 return NULL;
178 if (r_strlen <= 0)
179 r_strlen = 1;
181 zst.avail_in=length;
182 zst.avail_out=r_strlen;
183 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
185 PyErr_SetString(PyExc_MemoryError,
186 "Can't allocate memory to decompress data");
187 return NULL;
189 zst.zalloc=(alloc_func)NULL;
190 zst.zfree=(free_func)Z_NULL;
191 zst.next_out=(Byte *)PyString_AsString(result_str);
192 zst.next_in =(Byte *)input;
193 err=inflateInit2(&zst, wsize);
194 switch(err)
196 case(Z_OK):
197 break;
198 case(Z_MEM_ERROR):
199 PyErr_SetString(PyExc_MemoryError,
200 "Out of memory while decompressing data");
201 Py_DECREF(result_str);
202 return NULL;
203 default:
205 if (zst.msg == Z_NULL)
206 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
207 err);
208 else
209 PyErr_Format(ZlibError,
210 "Error %i while preparing to decompress data: %.200s",
211 err, zst.msg);
212 inflateEnd(&zst);
213 Py_DECREF(result_str);
214 return NULL;
219 err=inflate(&zst, Z_FINISH);
220 switch(err)
222 case(Z_STREAM_END):
223 break;
224 case(Z_BUF_ERROR):
225 case(Z_OK):
226 /* need more memory */
227 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
229 PyErr_SetString(PyExc_MemoryError,
230 "Out of memory while decompressing data");
231 inflateEnd(&zst);
232 return NULL;
234 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
235 zst.avail_out=r_strlen;
236 r_strlen = r_strlen << 1;
237 break;
238 default:
240 if (zst.msg == Z_NULL)
241 PyErr_Format(ZlibError, "Error %i while decompressing data",
242 err);
243 else
244 PyErr_Format(ZlibError,
245 "Error %i while decompressing data: %.200s",
246 err, zst.msg);
247 inflateEnd(&zst);
248 Py_DECREF(result_str);
249 return NULL;
252 } while(err!=Z_STREAM_END);
254 err=inflateEnd(&zst);
255 if (err!=Z_OK)
257 if (zst.msg == Z_NULL)
258 PyErr_Format(ZlibError,
259 "Error %i while finishing data decompression",
260 err);
261 else
262 PyErr_Format(ZlibError,
263 "Error %i while finishing data decompression: %.200s",
264 err, zst.msg);
265 Py_DECREF(result_str);
266 return NULL;
268 _PyString_Resize(&result_str, zst.total_out);
269 return result_str;
272 static PyObject *
273 PyZlib_compressobj(selfptr, args)
274 PyObject *selfptr;
275 PyObject *args;
277 compobject *self;
278 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
279 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
281 if (!PyArg_ParseTuple(args, "|iiiii", &level, &method, &wbits,
282 &memLevel, &strategy))
283 return NULL;
285 self = newcompobject(&Comptype);
286 if (self==NULL) return(NULL);
287 self->zst.zalloc = (alloc_func)NULL;
288 self->zst.zfree = (free_func)Z_NULL;
289 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
290 switch(err)
292 case (Z_OK):
293 self->is_initialised = 1;
294 return (PyObject*)self;
295 case (Z_MEM_ERROR):
296 Py_DECREF(self);
297 PyErr_SetString(PyExc_MemoryError,
298 "Can't allocate memory for compression object");
299 return NULL;
300 case(Z_STREAM_ERROR):
301 Py_DECREF(self);
302 PyErr_SetString(PyExc_ValueError,
303 "Invalid initialization option");
304 return NULL;
305 default:
307 if (self->zst.msg == Z_NULL)
308 PyErr_Format(ZlibError,
309 "Error %i while creating compression object",
310 err);
311 else
312 PyErr_Format(ZlibError,
313 "Error %i while creating compression object: %.200s",
314 err, self->zst.msg);
315 Py_DECREF(self);
316 return NULL;
321 static PyObject *
322 PyZlib_decompressobj(selfptr, args)
323 PyObject *selfptr;
324 PyObject *args;
326 int wbits=DEF_WBITS, err;
327 compobject *self;
328 if (!PyArg_ParseTuple(args, "|i", &wbits))
330 return NULL;
332 self=newcompobject(&Decomptype);
333 if (self==NULL) return(NULL);
334 self->zst.zalloc=(alloc_func)NULL;
335 self->zst.zfree=(free_func)Z_NULL;
336 err=inflateInit2(&self->zst, wbits);
337 switch(err)
339 case (Z_OK):
340 self->is_initialised = 1;
341 return (PyObject*)self;
342 case(Z_STREAM_ERROR):
343 Py_DECREF(self);
344 PyErr_SetString(PyExc_ValueError,
345 "Invalid initialization option");
346 return NULL;
347 case (Z_MEM_ERROR):
348 Py_DECREF(self);
349 PyErr_SetString(PyExc_MemoryError,
350 "Can't allocate memory for decompression object");
351 return NULL;
352 default:
354 if (self->zst.msg == Z_NULL)
355 PyErr_Format(ZlibError,
356 "Error %i while creating decompression object",
357 err);
358 else
359 PyErr_Format(ZlibError,
360 "Error %i while creating decompression object: %.200s",
361 err, self->zst.msg);
362 Py_DECREF(self);
363 return NULL;
368 static void
369 Comp_dealloc(self)
370 compobject *self;
372 if (self->is_initialised)
373 deflateEnd(&self->zst);
374 Py_XDECREF(self->unused_data);
375 PyMem_DEL(self);
378 static void
379 Decomp_dealloc(self)
380 compobject *self;
382 inflateEnd(&self->zst);
383 Py_XDECREF(self->unused_data);
384 PyMem_DEL(self);
387 static char comp_compress__doc__[] =
388 "compress(data) -- Return a string containing a compressed version of the data.\n\n"
389 "After calling this function, some of the input data may still\n"
390 "be stored in internal buffers for later processing.\n"
391 "Call the flush() method to clear these buffers."
395 static PyObject *
396 PyZlib_objcompress(self, args)
397 compobject *self;
398 PyObject *args;
400 int err = Z_OK, inplen;
401 int length = DEFAULTALLOC;
402 PyObject *RetVal;
403 Byte *input;
404 unsigned long start_total_out;
406 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
407 return NULL;
408 self->zst.avail_in = inplen;
409 self->zst.next_in = input;
410 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
411 PyErr_SetString(PyExc_MemoryError,
412 "Can't allocate memory to compress data");
413 return NULL;
415 start_total_out = self->zst.total_out;
416 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
417 self->zst.avail_out = length;
418 while (self->zst.avail_in != 0 && err == Z_OK)
420 err = deflate(&(self->zst), Z_NO_FLUSH);
421 if (self->zst.avail_out <= 0) {
422 if (_PyString_Resize(&RetVal, length << 1) == -1) {
423 PyErr_SetString(PyExc_MemoryError,
424 "Can't allocate memory to compress data");
425 return NULL;
427 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
428 self->zst.avail_out = length;
429 length = length << 1;
432 if (err != Z_OK)
434 if (self->zst.msg == Z_NULL)
435 PyErr_Format(ZlibError, "Error %i while compressing",
436 err);
437 else
438 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
439 err, self->zst.msg);
440 Py_DECREF(RetVal);
441 return NULL;
443 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
444 return RetVal;
447 static char decomp_decompress__doc__[] =
448 "decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
449 "After calling this function, some of the input data may still\n"
450 "be stored in internal buffers for later processing.\n"
451 "Call the flush() method to clear these buffers."
454 static PyObject *
455 PyZlib_objdecompress(self, args)
456 compobject *self;
457 PyObject *args;
459 int length, err, inplen;
460 PyObject *RetVal;
461 Byte *input;
462 unsigned long start_total_out;
464 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
465 return NULL;
466 start_total_out = self->zst.total_out;
467 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
468 self->zst.avail_in = inplen;
469 self->zst.next_in = input;
470 self->zst.avail_out = length = DEFAULTALLOC;
471 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
472 err = Z_OK;
474 while (self->zst.avail_in != 0 && err == Z_OK)
476 err = inflate(&(self->zst), Z_NO_FLUSH);
477 if (err == Z_OK && self->zst.avail_out <= 0)
479 if (_PyString_Resize(&RetVal, length << 1) == -1)
481 PyErr_SetString(PyExc_MemoryError,
482 "Can't allocate memory to compress data");
483 return NULL;
485 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
486 self->zst.avail_out = length;
487 length = length << 1;
491 if (err != Z_OK && err != Z_STREAM_END)
493 if (self->zst.msg == Z_NULL)
494 PyErr_Format(ZlibError, "Error %i while decompressing",
495 err);
496 else
497 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
498 err, self->zst.msg);
499 Py_DECREF(RetVal);
500 return NULL;
503 if (err == Z_STREAM_END)
505 /* The end of the compressed data has been reached, so set
506 the unused_data attribute to a string containing the
507 remainder of the data in the string. */
508 int pos = self->zst.next_in - input; /* Position in the string */
509 Py_XDECREF(self->unused_data); /* Free the original, empty string */
511 self->unused_data = PyString_FromStringAndSize((char *)input+pos,
512 inplen-pos);
513 if (self->unused_data == NULL) return NULL;
516 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
517 return RetVal;
520 static char comp_flush__doc__[] =
521 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
522 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n"
523 "default value used when mode is not specified is Z_FINISH.\n"
524 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
525 "calling the flush() method. Otherwise, more data can still be compressed.\n"
528 static PyObject *
529 PyZlib_flush(self, args)
530 compobject *self;
531 PyObject *args;
533 int length=DEFAULTALLOC, err = Z_OK;
534 PyObject *RetVal;
535 int flushmode = Z_FINISH;
536 unsigned long start_total_out;
538 if (!PyArg_ParseTuple(args, "|i", &flushmode))
539 return NULL;
541 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
542 doing any work at all; just return an empty string. */
543 if (flushmode == Z_NO_FLUSH)
545 return PyString_FromStringAndSize(NULL, 0);
548 self->zst.avail_in = 0;
549 self->zst.next_in = Z_NULL;
550 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
551 PyErr_SetString(PyExc_MemoryError,
552 "Can't allocate memory to compress data");
553 return NULL;
555 start_total_out = self->zst.total_out;
556 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
557 self->zst.avail_out = length;
559 /* When flushing the zstream, there's no input data.
560 If zst.avail_out == 0, that means that more output space is
561 needed to complete the flush operation. */
562 while (1) {
563 err = deflate(&(self->zst), flushmode);
565 /* If the output is Z_OK, and there's still room in the output
566 buffer, then the flush is complete. */
567 if ( (err == Z_OK) && self->zst.avail_out > 0) break;
569 /* A nonzero return indicates some sort of error (but see
570 the comment for the error handler below) */
571 if ( err != Z_OK ) break;
573 /* There's no space left for output, so increase the buffer and loop
574 again */
575 if (_PyString_Resize(&RetVal, length << 1) == -1) {
576 PyErr_SetString(PyExc_MemoryError,
577 "Can't allocate memory to compress data");
578 return NULL;
580 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
581 self->zst.avail_out = length;
582 length = length << 1;
585 /* Raise an exception indicating an error. The condition for
586 detecting a error is kind of complicated; Z_OK indicates no
587 error, but if the flushmode is Z_FINISH, then Z_STREAM_END is
588 also not an error. */
589 if (err!=Z_OK && !(flushmode == Z_FINISH && err == Z_STREAM_END) )
591 if (self->zst.msg == Z_NULL)
592 PyErr_Format(ZlibError, "Error %i while flushing",
593 err);
594 else
595 PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
596 err, self->zst.msg);
597 Py_DECREF(RetVal);
598 return NULL;
601 /* If flushmode is Z_FINISH, we also have to call deflateEnd() to
602 free various data structures */
604 if (flushmode == Z_FINISH) {
605 err=deflateEnd(&(self->zst));
606 if (err!=Z_OK) {
607 if (self->zst.msg == Z_NULL)
608 PyErr_Format(ZlibError, "Error %i from deflateEnd()",
609 err);
610 else
611 PyErr_Format(ZlibError,
612 "Error %i from deflateEnd(): %.200s",
613 err, self->zst.msg);
614 Py_DECREF(RetVal);
615 return NULL;
618 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
619 return RetVal;
622 static char decomp_flush__doc__[] =
623 "flush() -- Return a string containing any remaining decompressed data. "
624 "The decompressor object can no longer be used after this call."
627 static PyObject *
628 PyZlib_unflush(self, args)
629 compobject *self;
630 PyObject *args;
632 int length=0, err;
633 PyObject *RetVal;
635 if (!PyArg_NoArgs(args))
636 return NULL;
637 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
639 PyErr_SetString(PyExc_MemoryError,
640 "Can't allocate memory to decompress data");
641 return NULL;
643 self->zst.avail_in=0;
644 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
645 length = self->zst.avail_out = DEFAULTALLOC;
647 /* I suspect that Z_BUF_ERROR is the only error code we need to check for
648 in the following loop, but will leave the Z_OK in for now to avoid
649 destabilizing this function. --amk */
650 err = Z_OK;
651 while ( err == Z_OK )
653 err = inflate(&(self->zst), Z_FINISH);
654 if ( ( err == Z_OK || err == Z_BUF_ERROR ) && self->zst.avail_out == 0)
656 if (_PyString_Resize(&RetVal, length << 1) == -1)
658 PyErr_SetString(PyExc_MemoryError,
659 "Can't allocate memory to decompress data");
660 return NULL;
662 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
663 self->zst.avail_out = length;
664 length = length << 1;
665 err = Z_OK;
668 if (err!=Z_STREAM_END)
670 if (self->zst.msg == Z_NULL)
671 PyErr_Format(ZlibError, "Error %i while decompressing",
672 err);
673 else
674 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
675 err, self->zst.msg);
676 Py_DECREF(RetVal);
677 return NULL;
679 err=inflateEnd(&(self->zst));
680 if (err!=Z_OK)
682 if (self->zst.msg == Z_NULL)
683 PyErr_Format(ZlibError,
684 "Error %i while flushing decompression object",
685 err);
686 else
687 PyErr_Format(ZlibError,
688 "Error %i while flushing decompression object: %.200s",
689 err, self->zst.msg);
690 Py_DECREF(RetVal);
691 return NULL;
693 _PyString_Resize(&RetVal,
694 (char *)self->zst.next_out - PyString_AsString(RetVal));
695 return RetVal;
698 static PyMethodDef comp_methods[] =
700 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
701 {"flush", (binaryfunc)PyZlib_flush, 1, comp_flush__doc__},
702 {NULL, NULL}
705 static PyMethodDef Decomp_methods[] =
707 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
708 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
709 {NULL, NULL}
712 static PyObject *
713 Comp_getattr(self, name)
714 compobject *self;
715 char *name;
717 return Py_FindMethod(comp_methods, (PyObject *)self, name);
720 static PyObject *
721 Decomp_getattr(self, name)
722 compobject *self;
723 char *name;
725 if (strcmp(name, "unused_data") == 0)
727 Py_INCREF(self->unused_data);
728 return self->unused_data;
730 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
733 static char adler32__doc__[] =
734 "adler32(string) -- Compute an Adler-32 checksum of string, using "
735 "a default starting value, and returning an integer value.\n"
736 "adler32(string, value) -- Compute an Adler-32 checksum of string, using "
737 "the starting value provided, and returning an integer value\n"
740 static PyObject *
741 PyZlib_adler32(self, args)
742 PyObject *self, *args;
744 uLong adler32val=adler32(0L, Z_NULL, 0);
745 Byte *buf;
746 int len;
748 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
750 return NULL;
752 adler32val = adler32(adler32val, buf, len);
753 return PyInt_FromLong(adler32val);
756 static char crc32__doc__[] =
757 "crc32(string) -- Compute a CRC-32 checksum of string, using "
758 "a default starting value, and returning an integer value.\n"
759 "crc32(string, value) -- Compute a CRC-32 checksum of string, using "
760 "the starting value provided, and returning an integer value.\n"
763 static PyObject *
764 PyZlib_crc32(self, args)
765 PyObject *self, *args;
767 uLong crc32val=crc32(0L, Z_NULL, 0);
768 Byte *buf;
769 int len;
770 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
772 return NULL;
774 crc32val = crc32(crc32val, buf, len);
775 return PyInt_FromLong(crc32val);
779 static PyMethodDef zlib_methods[] =
781 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
782 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
783 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
784 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
785 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
786 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
787 {NULL, NULL}
790 statichere PyTypeObject Comptype = {
791 PyObject_HEAD_INIT(0)
793 "Compress",
794 sizeof(compobject),
796 (destructor)Comp_dealloc, /*tp_dealloc*/
797 0, /*tp_print*/
798 (getattrfunc)Comp_getattr, /*tp_getattr*/
799 0, /*tp_setattr*/
800 0, /*tp_compare*/
801 0, /*tp_repr*/
802 0, /*tp_as_number*/
803 0, /*tp_as_sequence*/
804 0, /*tp_as_mapping*/
807 statichere PyTypeObject Decomptype = {
808 PyObject_HEAD_INIT(0)
810 "Decompress",
811 sizeof(compobject),
813 (destructor)Decomp_dealloc, /*tp_dealloc*/
814 0, /*tp_print*/
815 (getattrfunc)Decomp_getattr, /*tp_getattr*/
816 0, /*tp_setattr*/
817 0, /*tp_compare*/
818 0, /*tp_repr*/
819 0, /*tp_as_number*/
820 0, /*tp_as_sequence*/
821 0, /*tp_as_mapping*/
824 /* The following insint() routine was blatantly ripped off from
825 socketmodule.c */
827 /* Convenience routine to export an integer value.
828 For simplicity, errors (which are unlikely anyway) are ignored. */
829 static void
830 insint(d, name, value)
831 PyObject *d;
832 char *name;
833 int value;
835 PyObject *v = PyInt_FromLong((long) value);
836 if (v == NULL) {
837 /* Don't bother reporting this error */
838 PyErr_Clear();
840 else {
841 PyDict_SetItemString(d, name, v);
842 Py_DECREF(v);
846 static char zlib_module_documentation[]=
847 "The functions in this module allow compression and decompression "
848 "using the zlib library, which is based on GNU zip. \n\n"
849 "adler32(string) -- Compute an Adler-32 checksum.\n"
850 "adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
851 "compress(string) -- Compress a string.\n"
852 "compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
853 "compressobj([level]) -- Return a compressor object.\n"
854 "crc32(string) -- Compute a CRC-32 checksum.\n"
855 "crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
856 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
857 "decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
858 "Compressor objects support compress() and flush() methods; decompressor \n"
859 "objects support decompress() and flush()."
862 DL_EXPORT(void)
863 PyInit_zlib()
865 PyObject *m, *d, *ver;
866 Comptype.ob_type = &PyType_Type;
867 Decomptype.ob_type = &PyType_Type;
868 m = Py_InitModule4("zlib", zlib_methods,
869 zlib_module_documentation,
870 (PyObject*)NULL,PYTHON_API_VERSION);
871 d = PyModule_GetDict(m);
872 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
873 if (ZlibError != NULL)
874 PyDict_SetItemString(d, "error", ZlibError);
876 insint(d, "MAX_WBITS", MAX_WBITS);
877 insint(d, "DEFLATED", DEFLATED);
878 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
879 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
880 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
881 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
882 insint(d, "Z_FILTERED", Z_FILTERED);
883 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
884 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
886 insint(d, "Z_FINISH", Z_FINISH);
887 insint(d, "Z_NO_FLUSH", Z_NO_FLUSH);
888 insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
889 insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH);
891 ver = PyString_FromString(ZLIB_VERSION);
892 if (ver != NULL) {
893 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
894 Py_DECREF(ver);