blob: bc1b6f8d62fbcafb4e4382fde3ab6eb085aa5500 [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;
Jeremy Hyltoncb914041997-09-04 23:39:23 +000070 zst.avail_out = length + length/1000 + 12 + 1;
Guido van Rossumfb221561997-04-29 15:38:09 +000071 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 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000102 if (zst.msg == Z_NULL)
103 PyErr_Format(ZlibError, "Error %i while compressing data",
104 err);
105 else
106 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
107 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000108 deflateEnd(&zst);
109 free(output);
110 return NULL;
111 }
112 break;
113 }
114
115 err=deflate(&zst, Z_FINISH);
116 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000117 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000118 case(Z_STREAM_END):
119 break;
120 /* Are there other errors to be trapped here? */
121 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000122 {
123 if (zst.msg == Z_NULL)
124 PyErr_Format(ZlibError, "Error %i while compressing data",
125 err);
126 else
127 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
128 err, zst.msg);
129 deflateEnd(&zst);
130 free(output);
131 return NULL;
132 }
133 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000134 err=deflateEnd(&zst);
135 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000136 {
137 if (zst.msg == Z_NULL)
138 PyErr_Format(ZlibError, "Error %i while finishing compression",
139 err);
140 else
141 PyErr_Format(ZlibError,
142 "Error %i while finishing compression: %.200s",
143 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000144 free(output);
145 return NULL;
146 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000147 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000148 free(output);
149 return ReturnVal;
150}
151
Guido van Rossum3c540301997-06-03 22:21:03 +0000152static char decompress__doc__[] =
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000153"decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
154"decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
155"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 +0000156;
157
Guido van Rossumfb221561997-04-29 15:38:09 +0000158static PyObject *
159PyZlib_decompress(self, args)
160 PyObject *self;
161 PyObject *args;
162{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000163 PyObject *result_str;
164 Byte *input;
Guido van Rossumfb221561997-04-29 15:38:09 +0000165 int length, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000166 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000167 z_stream zst;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000168 if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000169 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000170
171 if (r_strlen <= 0)
172 r_strlen = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000173
174 zst.avail_in=length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000175 zst.avail_out=r_strlen;
176 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
177 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000178 PyErr_SetString(PyExc_MemoryError,
179 "Can't allocate memory to decompress data");
180 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000181 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000182 zst.zalloc=(alloc_func)NULL;
183 zst.zfree=(free_func)Z_NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000184 zst.next_out=(Byte *)PyString_AsString(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000185 zst.next_in =(Byte *)input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000186 err=inflateInit2(&zst, wsize);
Guido van Rossumfb221561997-04-29 15:38:09 +0000187 switch(err)
188 {
189 case(Z_OK):
190 break;
191 case(Z_MEM_ERROR):
192 PyErr_SetString(PyExc_MemoryError,
193 "Out of memory while decompressing data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000194 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000195 return NULL;
196 default:
197 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000198 if (zst.msg == Z_NULL)
199 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
200 err);
201 else
202 PyErr_Format(ZlibError,
203 "Error %i while preparing to decompress data: %.200s",
204 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000205 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000206 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000207 return NULL;
208 }
209 }
210 do
211 {
212 err=inflate(&zst, Z_FINISH);
213 switch(err)
214 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000215 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000216 break;
217 case(Z_OK):
218 /* need more memory */
219 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
Guido van Rossumfb221561997-04-29 15:38:09 +0000220 {
221 PyErr_SetString(PyExc_MemoryError,
222 "Out of memory while decompressing data");
223 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000224 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000225 return NULL;
226 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000227 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000228 zst.avail_out=r_strlen;
229 r_strlen = r_strlen << 1;
230 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000231 default:
232 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000233 if (zst.msg == Z_NULL)
234 PyErr_Format(ZlibError, "Error %i while decompressing data",
235 err);
236 else
237 PyErr_Format(ZlibError,
238 "Error %i while decompressing data: %.200s",
239 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000240 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000241 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000242 return NULL;
243 }
244 }
245 } while(err!=Z_STREAM_END);
246
247 err=inflateEnd(&zst);
248 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000249 {
250 if (zst.msg == Z_NULL)
251 PyErr_Format(ZlibError,
252 "Error %i while finishing data decompression",
253 err);
254 else
255 PyErr_Format(ZlibError,
256 "Error %i while finishing data decompression: %.200s",
257 err, zst.msg);
258 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000259 return NULL;
260 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000261 _PyString_Resize(&result_str, zst.total_out);
262 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000263}
264
265static PyObject *
266PyZlib_compressobj(selfptr, args)
267 PyObject *selfptr;
268 PyObject *args;
269{
270 compobject *self;
271 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
272 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000273
274 if (!PyArg_ParseTuple(args, "|iiiii", &level, &method, &wbits,
275 &memLevel, &strategy))
276 return NULL;
277
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000278 self = newcompobject(&Comptype);
Guido van Rossumfb221561997-04-29 15:38:09 +0000279 if (self==NULL) return(NULL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000280 self->zst.zalloc = (alloc_func)NULL;
281 self->zst.zfree = (free_func)Z_NULL;
282 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Guido van Rossumfb221561997-04-29 15:38:09 +0000283 switch(err)
284 {
285 case (Z_OK):
286 return (PyObject*)self;
287 break;
288 case (Z_MEM_ERROR):
289 PyErr_SetString(PyExc_MemoryError,
290 "Can't allocate memory for compression object");
291 return NULL;
292 break;
293 case(Z_STREAM_ERROR):
294 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000295 "Invalid initialization option");
Guido van Rossumfb221561997-04-29 15:38:09 +0000296 return NULL;
297 break;
298 default:
299 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000300 if (self->zst.msg == Z_NULL)
301 PyErr_Format(ZlibError,
302 "Error %i while creating compression object",
303 err);
304 else
305 PyErr_Format(ZlibError,
306 "Error %i while creating compression object: %.200s",
307 err, self->zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000308 return NULL;
309 break;
310 }
311 }
312}
313
314static PyObject *
315PyZlib_decompressobj(selfptr, args)
316 PyObject *selfptr;
317 PyObject *args;
318{
319 int wbits=DEF_WBITS, err;
320 compobject *self;
321 if (!PyArg_ParseTuple(args, "|i", &wbits))
322 {
323 return NULL;
324 }
325 self=newcompobject(&Decomptype);
326 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000327 self->zst.zalloc=(alloc_func)NULL;
328 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000329 err=inflateInit2(&self->zst, wbits);
330 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000331 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000332 case (Z_OK):
333 return (PyObject*)self;
334 break;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000335 case(Z_STREAM_ERROR):
336 PyErr_SetString(PyExc_ValueError,
337 "Invalid initialization option");
338 return NULL;
339 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000340 case (Z_MEM_ERROR):
341 PyErr_SetString(PyExc_MemoryError,
342 "Can't allocate memory for decompression object");
343 return NULL;
344 break;
345 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000346 {
347 if (self->zst.msg == Z_NULL)
348 PyErr_Format(ZlibError,
349 "Error %i while creating decompression object",
350 err);
351 else
352 PyErr_Format(ZlibError,
353 "Error %i while creating decompression object: %.200s",
354 err, self->zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000355 return NULL;
356 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000357 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000358 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000359}
360
361static void
362Comp_dealloc(self)
363 compobject *self;
364{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000365 deflateEnd(&self->zst);
366 PyMem_DEL(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000367}
368
369static void
370Decomp_dealloc(self)
371 compobject *self;
372{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000373 inflateEnd(&self->zst);
374 PyMem_DEL(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000375}
376
Guido van Rossum3c540301997-06-03 22:21:03 +0000377static char comp_compress__doc__[] =
378"compress(data) -- Return a string containing a compressed version of the data.\n\n"
379"After calling this function, some of the input data may still\n"
380"be stored in internal buffers for later processing.\n"
381"Call the flush() method to clear these buffers."
382;
383
384
Guido van Rossumfb221561997-04-29 15:38:09 +0000385static PyObject *
386PyZlib_objcompress(self, args)
387 compobject *self;
388 PyObject *args;
389{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000390 int err = Z_OK, inplen;
391 int length = DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000392 PyObject *RetVal;
393 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000394 unsigned long start_total_out;
Guido van Rossumfb221561997-04-29 15:38:09 +0000395
396 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000397 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000398 self->zst.avail_in = inplen;
399 self->zst.next_in = input;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000400 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
401 PyErr_SetString(PyExc_MemoryError,
402 "Can't allocate memory to compress data");
403 return NULL;
404 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000405 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000406 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000407 self->zst.avail_out = length;
408 while (self->zst.avail_in != 0 && err == Z_OK)
409 {
410 err = deflate(&(self->zst), Z_NO_FLUSH);
411 if (self->zst.avail_out <= 0) {
412 if (_PyString_Resize(&RetVal, length << 1) == -1) {
413 PyErr_SetString(PyExc_MemoryError,
414 "Can't allocate memory to compress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000415 Py_DECREF(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000416 return NULL;
417 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000418 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000419 self->zst.avail_out = length;
420 length = length << 1;
421 }
422 }
423 if (err != Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000424 {
425 if (self->zst.msg == Z_NULL)
426 PyErr_Format(ZlibError, "Error %i while compressing",
427 err);
428 else
429 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
430 err, self->zst.msg);
431 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000432 return NULL;
433 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000434 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000435 return RetVal;
436}
437
Guido van Rossum3c540301997-06-03 22:21:03 +0000438static char decomp_decompress__doc__[] =
439"decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
440"After calling this function, some of the input data may still\n"
441"be stored in internal buffers for later processing.\n"
442"Call the flush() method to clear these buffers."
443;
444
Guido van Rossumfb221561997-04-29 15:38:09 +0000445static PyObject *
446PyZlib_objdecompress(self, args)
447 compobject *self;
448 PyObject *args;
449{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000450 int length, err, inplen;
Guido van Rossumfb221561997-04-29 15:38:09 +0000451 PyObject *RetVal;
452 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000453 unsigned long start_total_out;
454
Guido van Rossumfb221561997-04-29 15:38:09 +0000455 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
456 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000457 start_total_out = self->zst.total_out;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000458 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000459 self->zst.avail_in = inplen;
460 self->zst.next_in = input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000461 self->zst.avail_out = length = DEFAULTALLOC;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000462 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000463 err = Z_OK;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000464
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000465 while (self->zst.avail_in != 0 && err == Z_OK)
466 {
467 err = inflate(&(self->zst), Z_NO_FLUSH);
468 if (err == Z_OK && self->zst.avail_out <= 0)
469 {
470 if (_PyString_Resize(&RetVal, length << 1) == -1)
471 {
472 PyErr_SetString(PyExc_MemoryError,
473 "Can't allocate memory to compress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000474 Py_DECREF(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000475 return NULL;
476 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000477 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000478 self->zst.avail_out = length;
479 length = length << 1;
480 }
481 }
482
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000483 if (err != Z_OK && err != Z_STREAM_END)
484 {
485 if (self->zst.msg == Z_NULL)
486 PyErr_Format(ZlibError, "Error %i while decompressing",
487 err);
488 else
489 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
490 err, self->zst.msg);
491 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000492 return NULL;
493 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000494 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000495 return RetVal;
496}
497
Guido van Rossum3c540301997-06-03 22:21:03 +0000498static char comp_flush__doc__[] =
499"flush() -- Return a string containing any remaining compressed data. "
500"The compressor object can no longer be used after this call."
501;
502
Guido van Rossumfb221561997-04-29 15:38:09 +0000503static PyObject *
504PyZlib_flush(self, args)
505 compobject *self;
506 PyObject *args;
507{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000508 int length=DEFAULTALLOC, err = Z_OK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000509 PyObject *RetVal;
510
511 if (!PyArg_NoArgs(args))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000512 return NULL;
513 self->zst.avail_in = 0;
514 self->zst.next_in = Z_NULL;
515 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
516 PyErr_SetString(PyExc_MemoryError,
517 "Can't allocate memory to compress data");
518 return NULL;
519 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000520 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000521 self->zst.avail_out = length;
522 while (err == Z_OK)
523 {
524 err = deflate(&(self->zst), Z_FINISH);
525 if (self->zst.avail_out <= 0) {
526 if (_PyString_Resize(&RetVal, length << 1) == -1) {
527 PyErr_SetString(PyExc_MemoryError,
528 "Can't allocate memory to compress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000529 Py_DECREF(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000530 return NULL;
531 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000532 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000533 self->zst.avail_out = length;
534 length = length << 1;
535 }
536 }
537 if (err!=Z_STREAM_END) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000538 if (self->zst.msg == Z_NULL)
539 PyErr_Format(ZlibError, "Error %i while compressing",
540 err);
541 else
542 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
543 err, self->zst.msg);
544 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000545 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000546 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000547 err=deflateEnd(&(self->zst));
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000548 if (err!=Z_OK) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000549 if (self->zst.msg == Z_NULL)
550 PyErr_Format(ZlibError, "Error %i while flushing compression object",
551 err);
552 else
553 PyErr_Format(ZlibError,
554 "Error %i while flushing compression object: %.200s",
555 err, self->zst.msg);
556 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000557 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000558 }
559 _PyString_Resize(&RetVal,
560 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000561 return RetVal;
562}
563
Guido van Rossum3c540301997-06-03 22:21:03 +0000564static char decomp_flush__doc__[] =
565"flush() -- Return a string containing any remaining decompressed data. "
566"The decompressor object can no longer be used after this call."
567;
568
Guido van Rossumfb221561997-04-29 15:38:09 +0000569static PyObject *
570PyZlib_unflush(self, args)
571 compobject *self;
572 PyObject *args;
573{
574 int length=0, err;
Guido van Rossumfb221561997-04-29 15:38:09 +0000575 PyObject *RetVal;
576
577 if (!PyArg_NoArgs(args))
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000578 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000579 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
580 {
581 PyErr_SetString(PyExc_MemoryError,
582 "Can't allocate memory to decompress data");
583 return NULL;
584 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000585 self->zst.avail_in=0;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000586 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000587 length = self->zst.avail_out = DEFAULTALLOC;
588
589 err = Z_OK;
590 while (err == Z_OK)
591 {
592 err = inflate(&(self->zst), Z_FINISH);
593 if (err == Z_OK && self->zst.avail_out == 0)
594 {
595 if (_PyString_Resize(&RetVal, length << 1) == -1)
596 {
597 PyErr_SetString(PyExc_MemoryError,
598 "Can't allocate memory to decompress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000599 Py_DECREF(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000600 return NULL;
601 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000602 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000603 self->zst.avail_out = length;
604 length = length << 1;
605 }
606 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000607 if (err!=Z_STREAM_END)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000608 {
609 if (self->zst.msg == Z_NULL)
610 PyErr_Format(ZlibError, "Error %i while decompressing",
611 err);
612 else
613 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
614 err, self->zst.msg);
615 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000616 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000617 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000618 err=inflateEnd(&(self->zst));
619 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000620 {
621 if (self->zst.msg == Z_NULL)
622 PyErr_Format(ZlibError,
623 "Error %i while flushing decompression object",
624 err);
625 else
626 PyErr_Format(ZlibError,
627 "Error %i while flushing decompression object: %.200s",
628 err, self->zst.msg);
629 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000630 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000631 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000632 _PyString_Resize(&RetVal,
633 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000634 return RetVal;
635}
636
637static PyMethodDef comp_methods[] =
638{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000639 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
640 {"flush", (binaryfunc)PyZlib_flush, 0, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000641 {NULL, NULL}
642};
643
644static PyMethodDef Decomp_methods[] =
645{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000646 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
647 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000648 {NULL, NULL}
649};
650
651static PyObject *
652Comp_getattr(self, name)
653 compobject *self;
654 char *name;
655{
656 return Py_FindMethod(comp_methods, (PyObject *)self, name);
657}
658
659static PyObject *
660Decomp_getattr(self, name)
661 compobject *self;
662 char *name;
663{
664 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
665}
666
Guido van Rossum3c540301997-06-03 22:21:03 +0000667static char adler32__doc__[] =
668"adler32(string) -- Compute an Adler-32 checksum of string, using "
669"a default starting value, and returning an integer value.\n"
670"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
671"the starting value provided, and returning an integer value\n"
672;
673
Guido van Rossumfb221561997-04-29 15:38:09 +0000674static PyObject *
675PyZlib_adler32(self, args)
676 PyObject *self, *args;
677{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000678 uLong adler32val=adler32(0L, Z_NULL, 0);
679 Byte *buf;
680 int len;
Guido van Rossumfb221561997-04-29 15:38:09 +0000681
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000682 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
683 {
684 return NULL;
685 }
686 adler32val = adler32(adler32val, buf, len);
687 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000688}
689
Guido van Rossum3c540301997-06-03 22:21:03 +0000690static char crc32__doc__[] =
691"crc32(string) -- Compute a CRC-32 checksum of string, using "
692"a default starting value, and returning an integer value.\n"
693"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
694"the starting value provided, and returning an integer value.\n"
695;
Guido van Rossumfb221561997-04-29 15:38:09 +0000696
697static PyObject *
698PyZlib_crc32(self, args)
699 PyObject *self, *args;
700{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000701 uLong crc32val=crc32(0L, Z_NULL, 0);
702 Byte *buf;
703 int len;
704 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
705 {
706 return NULL;
707 }
708 crc32val = crc32(crc32val, buf, len);
709 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000710}
711
712
713static PyMethodDef zlib_methods[] =
714{
Guido van Rossum3c540301997-06-03 22:21:03 +0000715 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
716 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
717 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
718 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
719 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
720 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000721 {NULL, NULL}
722};
723
724statichere PyTypeObject Comptype = {
725 PyObject_HEAD_INIT(&PyType_Type)
726 0,
727 "Compress",
728 sizeof(compobject),
729 0,
730 (destructor)Comp_dealloc, /*tp_dealloc*/
731 0, /*tp_print*/
732 (getattrfunc)Comp_getattr, /*tp_getattr*/
733 0, /*tp_setattr*/
734 0, /*tp_compare*/
735 0, /*tp_repr*/
736 0, /*tp_as_number*/
737 0, /*tp_as_sequence*/
738 0, /*tp_as_mapping*/
739};
740
741statichere PyTypeObject Decomptype = {
742 PyObject_HEAD_INIT(&PyType_Type)
743 0,
744 "Decompress",
745 sizeof(compobject),
746 0,
747 (destructor)Decomp_dealloc, /*tp_dealloc*/
748 0, /*tp_print*/
749 (getattrfunc)Decomp_getattr, /*tp_getattr*/
750 0, /*tp_setattr*/
751 0, /*tp_compare*/
752 0, /*tp_repr*/
753 0, /*tp_as_number*/
754 0, /*tp_as_sequence*/
755 0, /*tp_as_mapping*/
756};
757
758/* The following insint() routine was blatantly ripped off from
759 socketmodule.c */
760
761/* Convenience routine to export an integer value.
762 For simplicity, errors (which are unlikely anyway) are ignored. */
763static void
764insint(d, name, value)
765 PyObject *d;
766 char *name;
767 int value;
768{
769 PyObject *v = PyInt_FromLong((long) value);
770 if (v == NULL) {
771 /* Don't bother reporting this error */
772 PyErr_Clear();
773 }
774 else {
775 PyDict_SetItemString(d, name, v);
776 Py_DECREF(v);
777 }
778}
779
Guido van Rossum3c540301997-06-03 22:21:03 +0000780static char zlib_module_documentation[]=
781"The functions in this module allow compression and decompression "
782"using the zlib library, which is based on GNU zip. \n\n"
783"adler32(string) -- Compute an Adler-32 checksum.\n"
784"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
785"compress(string) -- Compress a string.\n"
786"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
787"compressobj([level]) -- Return a compressor object.\n"
788"crc32(string) -- Compute a CRC-32 checksum.\n"
789"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000790"decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000791"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
792"Compressor objects support compress() and flush() methods; decompressor \n"
793"objects support decompress() and flush()."
794;
795
Guido van Rossumfb221561997-04-29 15:38:09 +0000796void
797PyInit_zlib()
798{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000799 PyObject *m, *d, *ver;
Guido van Rossum3c540301997-06-03 22:21:03 +0000800 m = Py_InitModule4("zlib", zlib_methods,
801 zlib_module_documentation,
802 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000803 d = PyModule_GetDict(m);
804 ZlibError = Py_BuildValue("s", "zlib.error");
805 PyDict_SetItemString(d, "error", ZlibError);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000806
Guido van Rossumfb221561997-04-29 15:38:09 +0000807 insint(d, "MAX_WBITS", MAX_WBITS);
808 insint(d, "DEFLATED", DEFLATED);
809 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000810 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
811 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
812 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
813 insint(d, "Z_FILTERED", Z_FILTERED);
814 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
815 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
816 ver = PyString_FromString(ZLIB_VERSION);
817 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
818
Guido van Rossumfb221561997-04-29 15:38:09 +0000819 if (PyErr_Occurred())
820 Py_FatalError("can't initialize module zlib");
821}