blob: 6f416b4b2a9c9a5f748481b9f8904d55f41ac264 [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. */
Jeremy Hylton41b9f001997-08-13 23:19:55 +000016#define DEFAULTALLOC 16*1024
Guido van Rossumfb221561997-04-29 15:38:09 +000017#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__[] =
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000147"decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
148"decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
149"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"
Guido van Rossum3c540301997-06-03 22:21:03 +0000150;
151
Guido van Rossumfb221561997-04-29 15:38:09 +0000152static PyObject *
153PyZlib_decompress(self, args)
154 PyObject *self;
155 PyObject *args;
156{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000157 PyObject *result_str;
158 Byte *input;
Guido van Rossumfb221561997-04-29 15:38:09 +0000159 int length, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000160 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000161 z_stream zst;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000162 if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000163 return NULL;
164
165 zst.avail_in=length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000166 zst.avail_out=r_strlen;
167 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
168 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000169 PyErr_SetString(PyExc_MemoryError,
170 "Can't allocate memory to decompress data");
171 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000172 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000173 zst.zalloc=(alloc_func)NULL;
174 zst.zfree=(free_func)Z_NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000175 zst.next_out=(Byte *)PyString_AsString(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000176 zst.next_in =(Byte *)input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000177 err=inflateInit2(&zst, wsize);
Guido van Rossumfb221561997-04-29 15:38:09 +0000178 switch(err)
179 {
180 case(Z_OK):
181 break;
182 case(Z_MEM_ERROR):
183 PyErr_SetString(PyExc_MemoryError,
184 "Out of memory while decompressing data");
Guido van Rossumfb221561997-04-29 15:38:09 +0000185 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);
Guido van Rossumfb221561997-04-29 15:38:09 +0000194 return NULL;
195 }
196 }
197 do
198 {
199 err=inflate(&zst, Z_FINISH);
200 switch(err)
201 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000202 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000203 break;
204 case(Z_OK):
205 /* need more memory */
206 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
Guido van Rossumfb221561997-04-29 15:38:09 +0000207 {
208 PyErr_SetString(PyExc_MemoryError,
209 "Out of memory while decompressing data");
210 inflateEnd(&zst);
211 return NULL;
212 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000213 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000214 zst.avail_out=r_strlen;
215 r_strlen = r_strlen << 1;
216 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000217 default:
218 {
219 char temp[500];
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000220 if (zst.msg==Z_NULL) zst.msg="";
Guido van Rossumfb221561997-04-29 15:38:09 +0000221 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);
Guido van Rossumfb221561997-04-29 15:38:09 +0000238 return NULL;
239 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000240 _PyString_Resize(&result_str, zst.total_out);
241 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000242}
243
244static PyObject *
245PyZlib_compressobj(selfptr, args)
246 PyObject *selfptr;
247 PyObject *args;
248{
249 compobject *self;
250 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
251 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000252
253 if (!PyArg_ParseTuple(args, "|iiiii", &level, &method, &wbits,
254 &memLevel, &strategy))
255 return NULL;
256
Guido van Rossumfb221561997-04-29 15:38:09 +0000257 self=newcompobject(&Comptype);
258 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000259 self->zst.zalloc=(alloc_func)NULL;
260 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000261 err=deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
262 switch(err)
263 {
264 case (Z_OK):
265 return (PyObject*)self;
266 break;
267 case (Z_MEM_ERROR):
268 PyErr_SetString(PyExc_MemoryError,
269 "Can't allocate memory for compression object");
270 return NULL;
271 break;
272 case(Z_STREAM_ERROR):
273 PyErr_SetString(PyExc_ValueError,
274 "Invalid compression level");
275 return NULL;
276 break;
277 default:
278 {
279 char temp[500];
280 if (self->zst.msg==Z_NULL) self->zst.msg="";
281 sprintf(temp, "Error %i while creating compression object [%s]",
282 err, self->zst.msg);
283 PyErr_SetString(ZlibError, temp);
284 return NULL;
285 break;
286 }
287 }
288}
289
290static PyObject *
291PyZlib_decompressobj(selfptr, args)
292 PyObject *selfptr;
293 PyObject *args;
294{
295 int wbits=DEF_WBITS, err;
296 compobject *self;
297 if (!PyArg_ParseTuple(args, "|i", &wbits))
298 {
299 return NULL;
300 }
301 self=newcompobject(&Decomptype);
302 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000303 self->zst.zalloc=(alloc_func)NULL;
304 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000305 err=inflateInit2(&self->zst, wbits);
306 switch(err)
307 {
308 case (Z_OK):
309 return (PyObject*)self;
310 break;
311 case (Z_MEM_ERROR):
312 PyErr_SetString(PyExc_MemoryError,
313 "Can't allocate memory for decompression object");
314 return NULL;
315 break;
316 default:
317 {
318 char temp[500];
319 if (self->zst.msg==Z_NULL) self->zst.msg="";
320 sprintf(temp, "Error %i while creating decompression object [%s]",
321 err, self->zst.msg);
322 PyErr_SetString(ZlibError, temp);
323 return NULL;
324 break;
325 }
326 }
327}
328
329static void
330Comp_dealloc(self)
331 compobject *self;
332{
333 int err;
334 err=deflateEnd(&self->zst); /* Deallocate zstream structure */
335}
336
337static void
338Decomp_dealloc(self)
339 compobject *self;
340{
341 int err;
342 err=inflateEnd(&self->zst); /* Deallocate zstream structure */
343}
344
Guido van Rossum3c540301997-06-03 22:21:03 +0000345static char comp_compress__doc__[] =
346"compress(data) -- Return a string containing a compressed version of the data.\n\n"
347"After calling this function, some of the input data may still\n"
348"be stored in internal buffers for later processing.\n"
349"Call the flush() method to clear these buffers."
350;
351
352
Guido van Rossumfb221561997-04-29 15:38:09 +0000353static PyObject *
354PyZlib_objcompress(self, args)
355 compobject *self;
356 PyObject *args;
357{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000358 int err = Z_OK, inplen;
359 int length = DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000360 PyObject *RetVal;
361 Byte *input;
362
363 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000364 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000365 self->zst.avail_in=inplen;
366 self->zst.next_in=input;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000367 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
368 PyErr_SetString(PyExc_MemoryError,
369 "Can't allocate memory to compress data");
370 return NULL;
371 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000372 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000373 self->zst.avail_out = length;
374 while (self->zst.avail_in != 0 && err == Z_OK)
375 {
376 err = deflate(&(self->zst), Z_NO_FLUSH);
377 if (self->zst.avail_out <= 0) {
378 if (_PyString_Resize(&RetVal, length << 1) == -1) {
379 PyErr_SetString(PyExc_MemoryError,
380 "Can't allocate memory to compress data");
381 return NULL;
382 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000383 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000384 self->zst.avail_out = length;
385 length = length << 1;
386 }
387 }
388 if (err != Z_OK)
Guido van Rossumfb221561997-04-29 15:38:09 +0000389 {
390 char temp[500];
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000391 if (self->zst.msg==Z_NULL) self->zst.msg="";
Guido van Rossumfb221561997-04-29 15:38:09 +0000392 sprintf(temp, "Error %i while compressing [%s]",
393 err, self->zst.msg);
394 PyErr_SetString(ZlibError, temp);
395 return NULL;
396 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000397 _PyString_Resize(&RetVal, self->zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000398 return RetVal;
399}
400
Guido van Rossum3c540301997-06-03 22:21:03 +0000401static char decomp_decompress__doc__[] =
402"decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
403"After calling this function, some of the input data may still\n"
404"be stored in internal buffers for later processing.\n"
405"Call the flush() method to clear these buffers."
406;
407
Guido van Rossumfb221561997-04-29 15:38:09 +0000408static PyObject *
409PyZlib_objdecompress(self, args)
410 compobject *self;
411 PyObject *args;
412{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000413 int length, err, inplen;
Guido van Rossumfb221561997-04-29 15:38:09 +0000414 PyObject *RetVal;
415 Byte *input;
416 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
417 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000418 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
Guido van Rossumfb221561997-04-29 15:38:09 +0000419 self->zst.avail_in=inplen;
420 self->zst.next_in=input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000421 self->zst.avail_out = length = DEFAULTALLOC;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000422 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000423 err = Z_OK;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000424
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000425 while (self->zst.avail_in != 0 && err == Z_OK)
426 {
427 err = inflate(&(self->zst), Z_NO_FLUSH);
428 if (err == Z_OK && self->zst.avail_out <= 0)
429 {
430 if (_PyString_Resize(&RetVal, length << 1) == -1)
431 {
432 PyErr_SetString(PyExc_MemoryError,
433 "Can't allocate memory to compress data");
434 return NULL;
435 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000436 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000437 self->zst.avail_out = length;
438 length = length << 1;
439 }
440 }
441
Guido van Rossumfb221561997-04-29 15:38:09 +0000442 if (err!=Z_OK && err!=Z_STREAM_END)
443 {
444 char temp[500];
445 if (self->zst.msg==Z_NULL) self->zst.msg="";
446 sprintf(temp, "Error %i while decompressing [%s]",
447 err, self->zst.msg);
448 PyErr_SetString(ZlibError, temp);
449 return NULL;
450 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000451 _PyString_Resize(&RetVal, self->zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000452 return RetVal;
453}
454
Guido van Rossum3c540301997-06-03 22:21:03 +0000455static char comp_flush__doc__[] =
456"flush() -- Return a string containing any remaining compressed data. "
457"The compressor object can no longer be used after this call."
458;
459
Guido van Rossumfb221561997-04-29 15:38:09 +0000460static PyObject *
461PyZlib_flush(self, args)
462 compobject *self;
463 PyObject *args;
464{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000465 int length=DEFAULTALLOC, err = Z_OK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000466 PyObject *RetVal;
467
468 if (!PyArg_NoArgs(args))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000469 return NULL;
470 self->zst.avail_in = 0;
471 self->zst.next_in = Z_NULL;
472 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
473 PyErr_SetString(PyExc_MemoryError,
474 "Can't allocate memory to compress data");
475 return NULL;
476 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000477 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000478 self->zst.avail_out = length;
479 while (err == Z_OK)
480 {
481 err = deflate(&(self->zst), Z_FINISH);
482 if (self->zst.avail_out <= 0) {
483 if (_PyString_Resize(&RetVal, length << 1) == -1) {
484 PyErr_SetString(PyExc_MemoryError,
485 "Can't allocate memory to compress data");
486 return NULL;
487 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000488 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000489 self->zst.avail_out = length;
490 length = length << 1;
491 }
492 }
493 if (err!=Z_STREAM_END) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000494 char temp[500];
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000495 if (self->zst.msg==Z_NULL) self->zst.msg="";
Guido van Rossumfb221561997-04-29 15:38:09 +0000496 sprintf(temp, "Error %i while compressing [%s]",
497 err, self->zst.msg);
498 PyErr_SetString(ZlibError, temp);
499 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000500 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000501 err=deflateEnd(&(self->zst));
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000502 if (err!=Z_OK) {
Guido van Rossumfb221561997-04-29 15:38:09 +0000503 char temp[500];
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000504 if (self->zst.msg==Z_NULL) self->zst.msg="";
Guido van Rossumfb221561997-04-29 15:38:09 +0000505 sprintf(temp, "Error %i while flushing compression object [%s]",
506 err, self->zst.msg);
507 PyErr_SetString(ZlibError, temp);
508 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000509 }
510 _PyString_Resize(&RetVal,
511 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000512 return RetVal;
513}
514
Guido van Rossum3c540301997-06-03 22:21:03 +0000515static char decomp_flush__doc__[] =
516"flush() -- Return a string containing any remaining decompressed data. "
517"The decompressor object can no longer be used after this call."
518;
519
Guido van Rossumfb221561997-04-29 15:38:09 +0000520static PyObject *
521PyZlib_unflush(self, args)
522 compobject *self;
523 PyObject *args;
524{
525 int length=0, err;
Guido van Rossumfb221561997-04-29 15:38:09 +0000526 PyObject *RetVal;
527
528 if (!PyArg_NoArgs(args))
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000529 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000530 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
531 {
532 PyErr_SetString(PyExc_MemoryError,
533 "Can't allocate memory to decompress data");
534 return NULL;
535 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000536 self->zst.avail_in=0;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000537 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000538 length = self->zst.avail_out = DEFAULTALLOC;
539
540 err = Z_OK;
541 while (err == Z_OK)
542 {
543 err = inflate(&(self->zst), Z_FINISH);
544 if (err == Z_OK && self->zst.avail_out == 0)
545 {
546 if (_PyString_Resize(&RetVal, length << 1) == -1)
547 {
548 PyErr_SetString(PyExc_MemoryError,
549 "Can't allocate memory to decompress data");
550 return NULL;
551 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000552 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000553 self->zst.avail_out = length;
554 length = length << 1;
555 }
556 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000557 if (err!=Z_STREAM_END)
558 {
559 char temp[500];
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000560 if (self->zst.msg==Z_NULL) self->zst.msg="";
Guido van Rossumfb221561997-04-29 15:38:09 +0000561 sprintf(temp, "Error %i while decompressing [%s]",
562 err, self->zst.msg);
563 PyErr_SetString(ZlibError, temp);
564 return NULL;
565 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000566 err=inflateEnd(&(self->zst));
567 if (err!=Z_OK)
568 {
569 char temp[500];
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000570 if (self->zst.msg==Z_NULL) self->zst.msg="";
Guido van Rossumfb221561997-04-29 15:38:09 +0000571 sprintf(temp, "Error %i while flushing decompression object [%s]",
572 err, self->zst.msg);
573 PyErr_SetString(ZlibError, temp);
574 return NULL;
575 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000576 _PyString_Resize(&RetVal,
577 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000578 return RetVal;
579}
580
581static PyMethodDef comp_methods[] =
582{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000583 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
584 {"flush", (binaryfunc)PyZlib_flush, 0, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000585 {NULL, NULL}
586};
587
588static PyMethodDef Decomp_methods[] =
589{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000590 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
591 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000592 {NULL, NULL}
593};
594
595static PyObject *
596Comp_getattr(self, name)
597 compobject *self;
598 char *name;
599{
600 return Py_FindMethod(comp_methods, (PyObject *)self, name);
601}
602
603static PyObject *
604Decomp_getattr(self, name)
605 compobject *self;
606 char *name;
607{
608 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
609}
610
Guido van Rossum3c540301997-06-03 22:21:03 +0000611static char adler32__doc__[] =
612"adler32(string) -- Compute an Adler-32 checksum of string, using "
613"a default starting value, and returning an integer value.\n"
614"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
615"the starting value provided, and returning an integer value\n"
616;
617
Guido van Rossumfb221561997-04-29 15:38:09 +0000618static PyObject *
619PyZlib_adler32(self, args)
620 PyObject *self, *args;
621{
622 uLong adler32val=adler32(0L, Z_NULL, 0);
623 Byte *buf;
624 int len;
625
626 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
627 {
628 return NULL;
629 }
630 adler32val=adler32(adler32val, buf, len);
631 return Py_BuildValue("l", adler32val);
632}
633
Guido van Rossum3c540301997-06-03 22:21:03 +0000634static char crc32__doc__[] =
635"crc32(string) -- Compute a CRC-32 checksum of string, using "
636"a default starting value, and returning an integer value.\n"
637"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
638"the starting value provided, and returning an integer value.\n"
639;
Guido van Rossumfb221561997-04-29 15:38:09 +0000640
641static PyObject *
642PyZlib_crc32(self, args)
643 PyObject *self, *args;
644{
645 uLong crc32val=crc32(0L, Z_NULL, 0);
646 Byte *buf;
647 int len;
648 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
649 {
650 return NULL;
651 }
652 crc32val=crc32(crc32val, buf, len);
653 return Py_BuildValue("l", crc32val);
654}
655
656
657static PyMethodDef zlib_methods[] =
658{
Guido van Rossum3c540301997-06-03 22:21:03 +0000659 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
660 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
661 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
662 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
663 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
664 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000665 {NULL, NULL}
666};
667
668statichere PyTypeObject Comptype = {
669 PyObject_HEAD_INIT(&PyType_Type)
670 0,
671 "Compress",
672 sizeof(compobject),
673 0,
674 (destructor)Comp_dealloc, /*tp_dealloc*/
675 0, /*tp_print*/
676 (getattrfunc)Comp_getattr, /*tp_getattr*/
677 0, /*tp_setattr*/
678 0, /*tp_compare*/
679 0, /*tp_repr*/
680 0, /*tp_as_number*/
681 0, /*tp_as_sequence*/
682 0, /*tp_as_mapping*/
683};
684
685statichere PyTypeObject Decomptype = {
686 PyObject_HEAD_INIT(&PyType_Type)
687 0,
688 "Decompress",
689 sizeof(compobject),
690 0,
691 (destructor)Decomp_dealloc, /*tp_dealloc*/
692 0, /*tp_print*/
693 (getattrfunc)Decomp_getattr, /*tp_getattr*/
694 0, /*tp_setattr*/
695 0, /*tp_compare*/
696 0, /*tp_repr*/
697 0, /*tp_as_number*/
698 0, /*tp_as_sequence*/
699 0, /*tp_as_mapping*/
700};
701
702/* The following insint() routine was blatantly ripped off from
703 socketmodule.c */
704
705/* Convenience routine to export an integer value.
706 For simplicity, errors (which are unlikely anyway) are ignored. */
707static void
708insint(d, name, value)
709 PyObject *d;
710 char *name;
711 int value;
712{
713 PyObject *v = PyInt_FromLong((long) value);
714 if (v == NULL) {
715 /* Don't bother reporting this error */
716 PyErr_Clear();
717 }
718 else {
719 PyDict_SetItemString(d, name, v);
720 Py_DECREF(v);
721 }
722}
723
Guido van Rossum3c540301997-06-03 22:21:03 +0000724static char zlib_module_documentation[]=
725"The functions in this module allow compression and decompression "
726"using the zlib library, which is based on GNU zip. \n\n"
727"adler32(string) -- Compute an Adler-32 checksum.\n"
728"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
729"compress(string) -- Compress a string.\n"
730"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
731"compressobj([level]) -- Return a compressor object.\n"
732"crc32(string) -- Compute a CRC-32 checksum.\n"
733"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000734"decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000735"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
736"Compressor objects support compress() and flush() methods; decompressor \n"
737"objects support decompress() and flush()."
738;
739
Guido van Rossumfb221561997-04-29 15:38:09 +0000740void
741PyInit_zlib()
742{
743 PyObject *m, *d;
Guido van Rossum3c540301997-06-03 22:21:03 +0000744 m = Py_InitModule4("zlib", zlib_methods,
745 zlib_module_documentation,
746 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000747 d = PyModule_GetDict(m);
748 ZlibError = Py_BuildValue("s", "zlib.error");
749 PyDict_SetItemString(d, "error", ZlibError);
750 insint(d, "MAX_WBITS", MAX_WBITS);
751 insint(d, "DEFLATED", DEFLATED);
752 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
753
754 if (PyErr_Occurred())
755 Py_FatalError("can't initialize module zlib");
756}