blob: 1edfab40da77f2eaab1a1dec719d75d0e7bbbce8 [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;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000452
Guido van Rossumfb221561997-04-29 15:38:09 +0000453 err=inflate(&(self->zst), Z_NO_FLUSH);
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000454
Guido van Rossumfb221561997-04-29 15:38:09 +0000455 } while (self->zst.avail_in!=0 && err==Z_OK);
456 if (err!=Z_OK && err!=Z_STREAM_END)
457 {
458 char temp[500];
459 if (self->zst.msg==Z_NULL) self->zst.msg="";
460 sprintf(temp, "Error %i while decompressing [%s]",
461 err, self->zst.msg);
462 PyErr_SetString(ZlibError, temp);
463 return NULL;
464 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000465 RetVal=PyString_FromStringAndSize((char *)buf, self->zst.next_out-buf);
Guido van Rossumfb221561997-04-29 15:38:09 +0000466 free(buf);
467 return RetVal;
468}
469
Guido van Rossum3c540301997-06-03 22:21:03 +0000470static char comp_flush__doc__[] =
471"flush() -- Return a string containing any remaining compressed data. "
472"The compressor object can no longer be used after this call."
473;
474
Guido van Rossumfb221561997-04-29 15:38:09 +0000475static PyObject *
476PyZlib_flush(self, args)
477 compobject *self;
478 PyObject *args;
479{
480 int length=0, err;
481 Byte *buf=NULL;
482 PyObject *RetVal;
483
484 if (!PyArg_NoArgs(args))
485 return NULL;
486 self->zst.avail_in=0;
487 do
488 {
489 buf=(Byte *)realloc(buf, length+ADDCHUNK);
490 if (buf==NULL)
491 {
492 PyErr_SetString(PyExc_MemoryError,
493 "Can't allocate memory to compress data");
494 return NULL;
495 }
496 self->zst.next_out=buf+length;
497 self->zst.avail_out=ADDCHUNK;
498 length += ADDCHUNK;
499 err=deflate(&(self->zst), Z_FINISH);
500 } while (err==Z_OK);
501 if (err!=Z_STREAM_END)
502 {
503 char temp[500];
504 if (self->zst.msg==Z_NULL) self->zst.msg="";
505 sprintf(temp, "Error %i while compressing [%s]",
506 err, self->zst.msg);
507 PyErr_SetString(ZlibError, temp);
508 return NULL;
509 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000510 RetVal=PyString_FromStringAndSize((char *)buf, self->zst.next_out-buf);
Guido van Rossumfb221561997-04-29 15:38:09 +0000511 free(buf);
512 err=deflateEnd(&(self->zst));
513 if (err!=Z_OK)
514 {
515 char temp[500];
516 if (self->zst.msg==Z_NULL) self->zst.msg="";
517 sprintf(temp, "Error %i while flushing compression object [%s]",
518 err, self->zst.msg);
519 PyErr_SetString(ZlibError, temp);
520 return NULL;
521 }
522 return RetVal;
523}
524
Guido van Rossum3c540301997-06-03 22:21:03 +0000525static char decomp_flush__doc__[] =
526"flush() -- Return a string containing any remaining decompressed data. "
527"The decompressor object can no longer be used after this call."
528;
529
Guido van Rossumfb221561997-04-29 15:38:09 +0000530static PyObject *
531PyZlib_unflush(self, args)
532 compobject *self;
533 PyObject *args;
534{
535 int length=0, err;
536 Byte *buf=NULL;
537 PyObject *RetVal;
538
539 if (!PyArg_NoArgs(args))
540 return NULL;
541 self->zst.avail_in=0;
542 do
543 {
544 buf=(Byte *)realloc(buf, length+ADDCHUNK);
545 if (buf==NULL)
546 {
547 PyErr_SetString(PyExc_MemoryError,
548 "Can't allocate memory to decompress data");
549 return NULL;
550 }
551 self->zst.next_out=buf+length;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000552 self->zst.avail_out = ADDCHUNK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000553 length += ADDCHUNK;
554 err=inflate(&(self->zst), Z_FINISH);
555 } while (err==Z_OK);
556 if (err!=Z_STREAM_END)
557 {
558 char temp[500];
559 if (self->zst.msg==Z_NULL) self->zst.msg="";
560 sprintf(temp, "Error %i while decompressing [%s]",
561 err, self->zst.msg);
562 PyErr_SetString(ZlibError, temp);
563 return NULL;
564 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000565 RetVal=PyString_FromStringAndSize((char *)buf, self->zst.next_out - buf);
Guido van Rossumfb221561997-04-29 15:38:09 +0000566 free(buf);
567 err=inflateEnd(&(self->zst));
568 if (err!=Z_OK)
569 {
570 char temp[500];
571 if (self->zst.msg==Z_NULL) self->zst.msg="";
572 sprintf(temp, "Error %i while flushing decompression object [%s]",
573 err, self->zst.msg);
574 PyErr_SetString(ZlibError, temp);
575 return NULL;
576 }
577 return RetVal;
578}
579
580static PyMethodDef comp_methods[] =
581{
Guido van Rossum3c540301997-06-03 22:21:03 +0000582 {"compress", PyZlib_objcompress, 1, comp_compress__doc__},
583 {"flush", PyZlib_flush, 0, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000584 {NULL, NULL}
585};
586
587static PyMethodDef Decomp_methods[] =
588{
Guido van Rossum3c540301997-06-03 22:21:03 +0000589 {"decompress", PyZlib_objdecompress, 1, decomp_decompress__doc__},
590 {"flush", PyZlib_unflush, 0, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000591 {NULL, NULL}
592};
593
594static PyObject *
595Comp_getattr(self, name)
596 compobject *self;
597 char *name;
598{
599 return Py_FindMethod(comp_methods, (PyObject *)self, name);
600}
601
602static PyObject *
603Decomp_getattr(self, name)
604 compobject *self;
605 char *name;
606{
607 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
608}
609
Guido van Rossum3c540301997-06-03 22:21:03 +0000610static char adler32__doc__[] =
611"adler32(string) -- Compute an Adler-32 checksum of string, using "
612"a default starting value, and returning an integer value.\n"
613"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
614"the starting value provided, and returning an integer value\n"
615;
616
Guido van Rossumfb221561997-04-29 15:38:09 +0000617static PyObject *
618PyZlib_adler32(self, args)
619 PyObject *self, *args;
620{
621 uLong adler32val=adler32(0L, Z_NULL, 0);
622 Byte *buf;
623 int len;
624
625 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
626 {
627 return NULL;
628 }
629 adler32val=adler32(adler32val, buf, len);
630 return Py_BuildValue("l", adler32val);
631}
632
Guido van Rossum3c540301997-06-03 22:21:03 +0000633static char crc32__doc__[] =
634"crc32(string) -- Compute a CRC-32 checksum of string, using "
635"a default starting value, and returning an integer value.\n"
636"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
637"the starting value provided, and returning an integer value.\n"
638;
Guido van Rossumfb221561997-04-29 15:38:09 +0000639
640static PyObject *
641PyZlib_crc32(self, args)
642 PyObject *self, *args;
643{
644 uLong crc32val=crc32(0L, Z_NULL, 0);
645 Byte *buf;
646 int len;
647 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
648 {
649 return NULL;
650 }
651 crc32val=crc32(crc32val, buf, len);
652 return Py_BuildValue("l", crc32val);
653}
654
655
656static PyMethodDef zlib_methods[] =
657{
Guido van Rossum3c540301997-06-03 22:21:03 +0000658 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
659 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
660 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
661 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
662 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
663 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000664 {NULL, NULL}
665};
666
667statichere PyTypeObject Comptype = {
668 PyObject_HEAD_INIT(&PyType_Type)
669 0,
670 "Compress",
671 sizeof(compobject),
672 0,
673 (destructor)Comp_dealloc, /*tp_dealloc*/
674 0, /*tp_print*/
675 (getattrfunc)Comp_getattr, /*tp_getattr*/
676 0, /*tp_setattr*/
677 0, /*tp_compare*/
678 0, /*tp_repr*/
679 0, /*tp_as_number*/
680 0, /*tp_as_sequence*/
681 0, /*tp_as_mapping*/
682};
683
684statichere PyTypeObject Decomptype = {
685 PyObject_HEAD_INIT(&PyType_Type)
686 0,
687 "Decompress",
688 sizeof(compobject),
689 0,
690 (destructor)Decomp_dealloc, /*tp_dealloc*/
691 0, /*tp_print*/
692 (getattrfunc)Decomp_getattr, /*tp_getattr*/
693 0, /*tp_setattr*/
694 0, /*tp_compare*/
695 0, /*tp_repr*/
696 0, /*tp_as_number*/
697 0, /*tp_as_sequence*/
698 0, /*tp_as_mapping*/
699};
700
701/* The following insint() routine was blatantly ripped off from
702 socketmodule.c */
703
704/* Convenience routine to export an integer value.
705 For simplicity, errors (which are unlikely anyway) are ignored. */
706static void
707insint(d, name, value)
708 PyObject *d;
709 char *name;
710 int value;
711{
712 PyObject *v = PyInt_FromLong((long) value);
713 if (v == NULL) {
714 /* Don't bother reporting this error */
715 PyErr_Clear();
716 }
717 else {
718 PyDict_SetItemString(d, name, v);
719 Py_DECREF(v);
720 }
721}
722
Guido van Rossum3c540301997-06-03 22:21:03 +0000723static char zlib_module_documentation[]=
724"The functions in this module allow compression and decompression "
725"using the zlib library, which is based on GNU zip. \n\n"
726"adler32(string) -- Compute an Adler-32 checksum.\n"
727"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
728"compress(string) -- Compress a string.\n"
729"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
730"compressobj([level]) -- Return a compressor object.\n"
731"crc32(string) -- Compute a CRC-32 checksum.\n"
732"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
733"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
734"Compressor objects support compress() and flush() methods; decompressor \n"
735"objects support decompress() and flush()."
736;
737
Guido van Rossumfb221561997-04-29 15:38:09 +0000738void
739PyInit_zlib()
740{
741 PyObject *m, *d;
Guido van Rossum3c540301997-06-03 22:21:03 +0000742 m = Py_InitModule4("zlib", zlib_methods,
743 zlib_module_documentation,
744 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000745 d = PyModule_GetDict(m);
746 ZlibError = Py_BuildValue("s", "zlib.error");
747 PyDict_SetItemString(d, "error", ZlibError);
748 insint(d, "MAX_WBITS", MAX_WBITS);
749 insint(d, "DEFLATED", DEFLATED);
750 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
751
752 if (PyErr_Occurred())
753 Py_FatalError("can't initialize module zlib");
754}