blob: 04ffabbaee517803bbde0effae7c63335a5d7a3f [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
2
Guido van Rossum97b54571997-06-03 22:21:47 +00003#include "Python.h"
4#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +00005
6/* The following parameters are copied from zutil.h, version 0.95 */
7#define DEFLATED 8
8#if MAX_MEM_LEVEL >= 8
9# define DEF_MEM_LEVEL 8
10#else
11# define DEF_MEM_LEVEL MAX_MEM_LEVEL
12#endif
13#define DEF_WBITS MAX_WBITS
14
15/* The output buffer will be increased in chunks of ADDCHUNK bytes. */
16#define ADDCHUNK 2048
17#define PyInit_zlib initzlib
18
19staticforward PyTypeObject Comptype;
20staticforward PyTypeObject Decomptype;
21
22static PyObject *ZlibError;
23
24typedef struct
25{
26 PyObject_HEAD
27 z_stream zst;
28} compobject;
29
Guido van Rossum3c540301997-06-03 22:21:03 +000030static char compressobj__doc__[] =
31"compressobj() -- Return a compressor object.\n"
32"compressobj(level) -- Return a compressor object, using the given compression level.\n"
33;
34
35static char decompressobj__doc__[] =
36"decompressobj() -- Return a decompressor object.\n"
37"decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
38;
39
Guido van Rossumfb221561997-04-29 15:38:09 +000040static compobject *
41newcompobject(type)
42 PyTypeObject *type;
43{
44 compobject *self;
45 self = PyObject_NEW(compobject, type);
46 if (self == NULL)
47 return NULL;
48 return self;
49}
50
Guido van Rossum3c540301997-06-03 22:21:03 +000051static char compress__doc__[] =
52"compress(string) -- Compress string using the default compression level, "
53"returning a string containing compressed data.\n"
54"compress(string, level) -- Compress string, using the chosen compression "
55"level (from 1 to 9). Return a string containing the compressed data.\n"
56;
57
Guido van Rossumfb221561997-04-29 15:38:09 +000058static PyObject *
59PyZlib_compress(self, args)
60 PyObject *self;
61 PyObject *args;
62{
63 PyObject *ReturnVal;
64 Byte *input, *output;
65 int length, level=Z_DEFAULT_COMPRESSION, err;
66 z_stream zst;
67
68 if (!PyArg_ParseTuple(args, "s#|i", &input, &length, &level))
69 return NULL;
70 zst.avail_out=length+length/1000+12+1;
71 output=(Byte*)malloc(zst.avail_out);
72 if (output==NULL)
73 {
74 PyErr_SetString(PyExc_MemoryError,
75 "Can't allocate memory to compress data");
76 return NULL;
77 }
Guido van Rossum97b54571997-06-03 22:21:47 +000078 zst.zalloc=(alloc_func)NULL;
79 zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +000080 zst.next_out=(Byte *)output;
81 zst.next_in =(Byte *)input;
82 zst.avail_in=length;
83 err=deflateInit(&zst, level);
84 switch(err)
85 {
86 case(Z_OK):
87 break;
88 case(Z_MEM_ERROR):
89 PyErr_SetString(PyExc_MemoryError,
90 "Out of memory while compressing data");
91 free(output);
92 return NULL;
93 break;
94 case(Z_STREAM_ERROR):
95 PyErr_SetString(ZlibError,
96 "Bad compression level");
97 free(output);
98 return NULL;
99 break;
100 default:
101 {
102 char temp[500];
103 if (zst.msg==Z_NULL) zst.msg="";
104 sprintf(temp, "Error %i while compressing data [%s]", err, zst.msg);
105 PyErr_SetString(ZlibError, temp);
106 deflateEnd(&zst);
107 free(output);
108 return NULL;
109 }
110 break;
111 }
112
113 err=deflate(&zst, Z_FINISH);
114 switch(err)
115 {
116 case(Z_STREAM_END):
117 break;
118 /* Are there other errors to be trapped here? */
119 default:
120 {
121 char temp[500];
122 if (zst.msg==Z_NULL) zst.msg="";
123 sprintf(temp, "Error %i while compressing data [%s]", err, zst.msg);
124 PyErr_SetString(ZlibError, temp);
125 deflateEnd(&zst);
126 free(output);
127 return NULL;
128 }
129 }
130 err=deflateEnd(&zst);
131 if (err!=Z_OK)
132 {
133 char temp[500];
134 if (zst.msg==Z_NULL) zst.msg="";
135 sprintf(temp, "Error %i while finishing data compression [%s]",
136 err, zst.msg);
137 PyErr_SetString(ZlibError, temp);
138 free(output);
139 return NULL;
140 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000141 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000142 free(output);
143 return ReturnVal;
144}
145
Guido van Rossum3c540301997-06-03 22:21:03 +0000146static char decompress__doc__[] =
147"decompress(string) -- Decompress the data in string, returning "
148"a string containing the decompressed data."
149;
150
Guido van Rossumfb221561997-04-29 15:38:09 +0000151static PyObject *
152PyZlib_decompress(self, args)
153 PyObject *self;
154 PyObject *args;
155{
156 PyObject *ReturnVal;
157 Byte *input, *output;
158 int length, err;
159 z_stream zst;
160 if (!PyArg_ParseTuple(args, "s#", &input, &length))
161 return NULL;
162
163 zst.avail_in=length;
164 zst.avail_out=length=length*2;
165 output=(Byte*)malloc(zst.avail_out);
166 if (output==NULL)
167 {
168 PyErr_SetString(PyExc_MemoryError,
169 "Can't allocate memory to decompress data");
170 return NULL;
171 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000172 zst.zalloc=(alloc_func)NULL;
173 zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000174 zst.next_out=(Byte *)output;
175 zst.next_in =(Byte *)input;
176 err=inflateInit(&zst);
177 switch(err)
178 {
179 case(Z_OK):
180 break;
181 case(Z_MEM_ERROR):
182 PyErr_SetString(PyExc_MemoryError,
183 "Out of memory while decompressing data");
184 free(output);
185 return NULL;
186 default:
187 {
188 char temp[500];
189 if (zst.msg==Z_NULL) zst.msg="";
190 sprintf(temp, "Error %i while preparing to decompress data [%s]",
191 err, zst.msg);
192 PyErr_SetString(ZlibError, temp);
193 inflateEnd(&zst);
194 free(output);
195 return NULL;
196 }
197 }
198 do
199 {
200 err=inflate(&zst, Z_FINISH);
201 switch(err)
202 {
203 case(Z_OK):
204 case(Z_STREAM_END):
205 output=(Byte *)realloc(output, length+ADDCHUNK);
206 if (output==NULL)
207 {
208 PyErr_SetString(PyExc_MemoryError,
209 "Out of memory while decompressing data");
210 inflateEnd(&zst);
211 return NULL;
212 }
213 zst.next_out=output+length;
214 zst.avail_out=ADDCHUNK;
215 length += ADDCHUNK;
216 break;
217 default:
218 {
219 char temp[500];
220 if (zst.msg==Z_NULL) zst.msg="";
221 sprintf(temp, "Error %i while decompressing data: [%s]",
222 err, zst.msg);
223 PyErr_SetString(ZlibError, temp);
224 inflateEnd(&zst);
225 return NULL;
226 }
227 }
228 } while(err!=Z_STREAM_END);
229
230 err=inflateEnd(&zst);
231 if (err!=Z_OK)
232 {
233 char temp[500];
234 if (zst.msg==Z_NULL) zst.msg="";
235 sprintf(temp, "Error %i while finishing data decompression [%s]",
236 err, zst.msg);
237 PyErr_SetString(ZlibError, temp);
238 free(output);
239 return NULL;
240 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000241 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000242 free(output);
243 return ReturnVal;
244}
245
246static PyObject *
247PyZlib_compressobj(selfptr, args)
248 PyObject *selfptr;
249 PyObject *args;
250{
251 compobject *self;
252 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
253 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
254 /* XXX Argh! Is there a better way to have multiple levels of */
255 /* optional arguments? */
256 if (!PyArg_ParseTuple(args, "iiiii", &level, &method, &wbits, &memLevel, &strategy))
257 {
258 PyErr_Clear();
259 if (!PyArg_ParseTuple(args, "iiii", &level, &method, &wbits,
260 &memLevel))
261 {
262 PyErr_Clear();
263 if (!PyArg_ParseTuple(args, "iii", &level, &method, &wbits))
264 {
265 PyErr_Clear();
266 if (!PyArg_ParseTuple(args, "ii", &level, &method))
267 {
268 PyErr_Clear();
269 if (!PyArg_ParseTuple(args, "i", &level))
270 {
271 PyErr_Clear();
272 if (!PyArg_ParseTuple(args, ""))
273 return (NULL);
274 }
275 }
276 }
277 }
278 }
279 self=newcompobject(&Comptype);
280 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000281 self->zst.zalloc=(alloc_func)NULL;
282 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000283 err=deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
284 switch(err)
285 {
286 case (Z_OK):
287 return (PyObject*)self;
288 break;
289 case (Z_MEM_ERROR):
290 PyErr_SetString(PyExc_MemoryError,
291 "Can't allocate memory for compression object");
292 return NULL;
293 break;
294 case(Z_STREAM_ERROR):
295 PyErr_SetString(PyExc_ValueError,
296 "Invalid compression level");
297 return NULL;
298 break;
299 default:
300 {
301 char temp[500];
302 if (self->zst.msg==Z_NULL) self->zst.msg="";
303 sprintf(temp, "Error %i while creating compression object [%s]",
304 err, self->zst.msg);
305 PyErr_SetString(ZlibError, temp);
306 return NULL;
307 break;
308 }
309 }
310}
311
312static PyObject *
313PyZlib_decompressobj(selfptr, args)
314 PyObject *selfptr;
315 PyObject *args;
316{
317 int wbits=DEF_WBITS, err;
318 compobject *self;
319 if (!PyArg_ParseTuple(args, "|i", &wbits))
320 {
321 return NULL;
322 }
323 self=newcompobject(&Decomptype);
324 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000325 self->zst.zalloc=(alloc_func)NULL;
326 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000327 /* XXX If illegal values of wbits are allowed to get here, Python
328 coredumps, instead of raising an exception as it should.
329 This is a bug in zlib 0.95; I have reported it. */
330 err=inflateInit2(&self->zst, wbits);
331 switch(err)
332 {
333 case (Z_OK):
334 return (PyObject*)self;
335 break;
336 case (Z_MEM_ERROR):
337 PyErr_SetString(PyExc_MemoryError,
338 "Can't allocate memory for decompression object");
339 return NULL;
340 break;
341 default:
342 {
343 char temp[500];
344 if (self->zst.msg==Z_NULL) self->zst.msg="";
345 sprintf(temp, "Error %i while creating decompression object [%s]",
346 err, self->zst.msg);
347 PyErr_SetString(ZlibError, temp);
348 return NULL;
349 break;
350 }
351 }
352}
353
354static void
355Comp_dealloc(self)
356 compobject *self;
357{
358 int err;
359 err=deflateEnd(&self->zst); /* Deallocate zstream structure */
360}
361
362static void
363Decomp_dealloc(self)
364 compobject *self;
365{
366 int err;
367 err=inflateEnd(&self->zst); /* Deallocate zstream structure */
368}
369
Guido van Rossum3c540301997-06-03 22:21:03 +0000370static char comp_compress__doc__[] =
371"compress(data) -- Return a string containing a compressed version of the data.\n\n"
372"After calling this function, some of the input data may still\n"
373"be stored in internal buffers for later processing.\n"
374"Call the flush() method to clear these buffers."
375;
376
377
Guido van Rossumfb221561997-04-29 15:38:09 +0000378static PyObject *
379PyZlib_objcompress(self, args)
380 compobject *self;
381 PyObject *args;
382{
383 int length=0, err, inplen;
384 Byte *buf=NULL;
385 PyObject *RetVal;
386 Byte *input;
387
388 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
389 return NULL;
390 self->zst.avail_in=inplen;
391 self->zst.next_in=input;
392 do
393 {
394 buf=(Byte *)realloc(buf, length+ADDCHUNK);
395 if (buf==NULL)
396 {
397 PyErr_SetString(PyExc_MemoryError,
398 "Can't allocate memory to compress data");
399 return NULL;
400 }
401 self->zst.next_out=buf+length;
402 self->zst.avail_out=ADDCHUNK;
403 length += ADDCHUNK;
404 err=deflate(&(self->zst), Z_NO_FLUSH);
405 } while (self->zst.avail_in!=0 && err==Z_OK);
406 if (err!=Z_OK)
407 {
408 char temp[500];
409 if (self->zst.msg==Z_NULL) self->zst.msg="";
410 sprintf(temp, "Error %i while compressing [%s]",
411 err, self->zst.msg);
412 PyErr_SetString(ZlibError, temp);
413 return NULL;
414 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000415 RetVal=PyString_FromStringAndSize((char *)buf, self->zst.next_out-buf);
Guido van Rossumfb221561997-04-29 15:38:09 +0000416 free(buf);
417 return RetVal;
418}
419
Guido van Rossum3c540301997-06-03 22:21:03 +0000420static char decomp_decompress__doc__[] =
421"decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
422"After calling this function, some of the input data may still\n"
423"be stored in internal buffers for later processing.\n"
424"Call the flush() method to clear these buffers."
425;
426
Guido van Rossumfb221561997-04-29 15:38:09 +0000427static PyObject *
428PyZlib_objdecompress(self, args)
429 compobject *self;
430 PyObject *args;
431{
432 int length=0, err, inplen;
433 Byte *buf=NULL;
434 PyObject *RetVal;
435 Byte *input;
436 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
437 return NULL;
438 self->zst.avail_in=inplen;
439 self->zst.next_in=input;
440 do
441 {
442 buf=(Byte *)realloc(buf, length+ADDCHUNK);
443 if (buf==NULL)
444 {
445 PyErr_SetString(PyExc_MemoryError,
446 "Can't allocate memory to decompress data");
447 return NULL;
448 }
449 self->zst.next_out=buf+length;
450 self->zst.avail_out=ADDCHUNK;
451 length += ADDCHUNK;
452 err=inflate(&(self->zst), Z_NO_FLUSH);
453 } while (self->zst.avail_in!=0 && err==Z_OK);
454 if (err!=Z_OK && err!=Z_STREAM_END)
455 {
456 char temp[500];
457 if (self->zst.msg==Z_NULL) self->zst.msg="";
458 sprintf(temp, "Error %i while decompressing [%s]",
459 err, self->zst.msg);
460 PyErr_SetString(ZlibError, temp);
461 return NULL;
462 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000463 RetVal=PyString_FromStringAndSize((char *)buf, self->zst.next_out-buf);
Guido van Rossumfb221561997-04-29 15:38:09 +0000464 free(buf);
465 return RetVal;
466}
467
Guido van Rossum3c540301997-06-03 22:21:03 +0000468static char comp_flush__doc__[] =
469"flush() -- Return a string containing any remaining compressed data. "
470"The compressor object can no longer be used after this call."
471;
472
Guido van Rossumfb221561997-04-29 15:38:09 +0000473static PyObject *
474PyZlib_flush(self, args)
475 compobject *self;
476 PyObject *args;
477{
478 int length=0, err;
479 Byte *buf=NULL;
480 PyObject *RetVal;
481
482 if (!PyArg_NoArgs(args))
483 return NULL;
484 self->zst.avail_in=0;
485 do
486 {
487 buf=(Byte *)realloc(buf, length+ADDCHUNK);
488 if (buf==NULL)
489 {
490 PyErr_SetString(PyExc_MemoryError,
491 "Can't allocate memory to compress data");
492 return NULL;
493 }
494 self->zst.next_out=buf+length;
495 self->zst.avail_out=ADDCHUNK;
496 length += ADDCHUNK;
497 err=deflate(&(self->zst), Z_FINISH);
498 } while (err==Z_OK);
499 if (err!=Z_STREAM_END)
500 {
501 char temp[500];
502 if (self->zst.msg==Z_NULL) self->zst.msg="";
503 sprintf(temp, "Error %i while compressing [%s]",
504 err, self->zst.msg);
505 PyErr_SetString(ZlibError, temp);
506 return NULL;
507 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000508 RetVal=PyString_FromStringAndSize((char *)buf, self->zst.next_out-buf);
Guido van Rossumfb221561997-04-29 15:38:09 +0000509 free(buf);
510 err=deflateEnd(&(self->zst));
511 if (err!=Z_OK)
512 {
513 char temp[500];
514 if (self->zst.msg==Z_NULL) self->zst.msg="";
515 sprintf(temp, "Error %i while flushing compression object [%s]",
516 err, self->zst.msg);
517 PyErr_SetString(ZlibError, temp);
518 return NULL;
519 }
520 return RetVal;
521}
522
Guido van Rossum3c540301997-06-03 22:21:03 +0000523static char decomp_flush__doc__[] =
524"flush() -- Return a string containing any remaining decompressed data. "
525"The decompressor object can no longer be used after this call."
526;
527
Guido van Rossumfb221561997-04-29 15:38:09 +0000528static PyObject *
529PyZlib_unflush(self, args)
530 compobject *self;
531 PyObject *args;
532{
533 int length=0, err;
534 Byte *buf=NULL;
535 PyObject *RetVal;
536
537 if (!PyArg_NoArgs(args))
538 return NULL;
539 self->zst.avail_in=0;
540 do
541 {
542 buf=(Byte *)realloc(buf, length+ADDCHUNK);
543 if (buf==NULL)
544 {
545 PyErr_SetString(PyExc_MemoryError,
546 "Can't allocate memory to decompress data");
547 return NULL;
548 }
549 self->zst.next_out=buf+length;
550 length += ADDCHUNK;
551 err=inflate(&(self->zst), Z_FINISH);
552 } while (err==Z_OK);
553 if (err!=Z_STREAM_END)
554 {
555 char temp[500];
556 if (self->zst.msg==Z_NULL) self->zst.msg="";
557 sprintf(temp, "Error %i while decompressing [%s]",
558 err, self->zst.msg);
559 PyErr_SetString(ZlibError, temp);
560 return NULL;
561 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000562 RetVal=PyString_FromStringAndSize((char *)buf, self->zst.next_out - buf);
Guido van Rossumfb221561997-04-29 15:38:09 +0000563 free(buf);
564 err=inflateEnd(&(self->zst));
565 if (err!=Z_OK)
566 {
567 char temp[500];
568 if (self->zst.msg==Z_NULL) self->zst.msg="";
569 sprintf(temp, "Error %i while flushing decompression object [%s]",
570 err, self->zst.msg);
571 PyErr_SetString(ZlibError, temp);
572 return NULL;
573 }
574 return RetVal;
575}
576
577static PyMethodDef comp_methods[] =
578{
Guido van Rossum3c540301997-06-03 22:21:03 +0000579 {"compress", PyZlib_objcompress, 1, comp_compress__doc__},
580 {"flush", PyZlib_flush, 0, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000581 {NULL, NULL}
582};
583
584static PyMethodDef Decomp_methods[] =
585{
Guido van Rossum3c540301997-06-03 22:21:03 +0000586 {"decompress", PyZlib_objdecompress, 1, decomp_decompress__doc__},
587 {"flush", PyZlib_unflush, 0, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000588 {NULL, NULL}
589};
590
591static PyObject *
592Comp_getattr(self, name)
593 compobject *self;
594 char *name;
595{
596 return Py_FindMethod(comp_methods, (PyObject *)self, name);
597}
598
599static PyObject *
600Decomp_getattr(self, name)
601 compobject *self;
602 char *name;
603{
604 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
605}
606
Guido van Rossum3c540301997-06-03 22:21:03 +0000607static char adler32__doc__[] =
608"adler32(string) -- Compute an Adler-32 checksum of string, using "
609"a default starting value, and returning an integer value.\n"
610"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
611"the starting value provided, and returning an integer value\n"
612;
613
Guido van Rossumfb221561997-04-29 15:38:09 +0000614static PyObject *
615PyZlib_adler32(self, args)
616 PyObject *self, *args;
617{
618 uLong adler32val=adler32(0L, Z_NULL, 0);
619 Byte *buf;
620 int len;
621
622 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
623 {
624 return NULL;
625 }
626 adler32val=adler32(adler32val, buf, len);
627 return Py_BuildValue("l", adler32val);
628}
629
Guido van Rossum3c540301997-06-03 22:21:03 +0000630static char crc32__doc__[] =
631"crc32(string) -- Compute a CRC-32 checksum of string, using "
632"a default starting value, and returning an integer value.\n"
633"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
634"the starting value provided, and returning an integer value.\n"
635;
Guido van Rossumfb221561997-04-29 15:38:09 +0000636
637static PyObject *
638PyZlib_crc32(self, args)
639 PyObject *self, *args;
640{
641 uLong crc32val=crc32(0L, Z_NULL, 0);
642 Byte *buf;
643 int len;
644 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
645 {
646 return NULL;
647 }
648 crc32val=crc32(crc32val, buf, len);
649 return Py_BuildValue("l", crc32val);
650}
651
652
653static PyMethodDef zlib_methods[] =
654{
Guido van Rossum3c540301997-06-03 22:21:03 +0000655 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
656 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
657 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
658 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
659 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
660 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000661 {NULL, NULL}
662};
663
664statichere PyTypeObject Comptype = {
665 PyObject_HEAD_INIT(&PyType_Type)
666 0,
667 "Compress",
668 sizeof(compobject),
669 0,
670 (destructor)Comp_dealloc, /*tp_dealloc*/
671 0, /*tp_print*/
672 (getattrfunc)Comp_getattr, /*tp_getattr*/
673 0, /*tp_setattr*/
674 0, /*tp_compare*/
675 0, /*tp_repr*/
676 0, /*tp_as_number*/
677 0, /*tp_as_sequence*/
678 0, /*tp_as_mapping*/
679};
680
681statichere PyTypeObject Decomptype = {
682 PyObject_HEAD_INIT(&PyType_Type)
683 0,
684 "Decompress",
685 sizeof(compobject),
686 0,
687 (destructor)Decomp_dealloc, /*tp_dealloc*/
688 0, /*tp_print*/
689 (getattrfunc)Decomp_getattr, /*tp_getattr*/
690 0, /*tp_setattr*/
691 0, /*tp_compare*/
692 0, /*tp_repr*/
693 0, /*tp_as_number*/
694 0, /*tp_as_sequence*/
695 0, /*tp_as_mapping*/
696};
697
698/* The following insint() routine was blatantly ripped off from
699 socketmodule.c */
700
701/* Convenience routine to export an integer value.
702 For simplicity, errors (which are unlikely anyway) are ignored. */
703static void
704insint(d, name, value)
705 PyObject *d;
706 char *name;
707 int value;
708{
709 PyObject *v = PyInt_FromLong((long) value);
710 if (v == NULL) {
711 /* Don't bother reporting this error */
712 PyErr_Clear();
713 }
714 else {
715 PyDict_SetItemString(d, name, v);
716 Py_DECREF(v);
717 }
718}
719
Guido van Rossum3c540301997-06-03 22:21:03 +0000720static char zlib_module_documentation[]=
721"The functions in this module allow compression and decompression "
722"using the zlib library, which is based on GNU zip. \n\n"
723"adler32(string) -- Compute an Adler-32 checksum.\n"
724"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
725"compress(string) -- Compress a string.\n"
726"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
727"compressobj([level]) -- Return a compressor object.\n"
728"crc32(string) -- Compute a CRC-32 checksum.\n"
729"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
730"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
731"Compressor objects support compress() and flush() methods; decompressor \n"
732"objects support decompress() and flush()."
733;
734
Guido van Rossumfb221561997-04-29 15:38:09 +0000735void
736PyInit_zlib()
737{
738 PyObject *m, *d;
Guido van Rossum3c540301997-06-03 22:21:03 +0000739 m = Py_InitModule4("zlib", zlib_methods,
740 zlib_module_documentation,
741 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000742 d = PyModule_GetDict(m);
743 ZlibError = Py_BuildValue("s", "zlib.error");
744 PyDict_SetItemString(d, "error", ZlibError);
745 insint(d, "MAX_WBITS", MAX_WBITS);
746 insint(d, "DEFLATED", DEFLATED);
747 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
748
749 if (PyErr_Occurred())
750 Py_FatalError("can't initialize module zlib");
751}