blob: 1f99646ba5a0d1d5e3ee5929eaf1e227941bba10 [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"
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +00004#ifdef MS_WIN32
5#define ZLIB_DLL
6#endif
Guido van Rossum97b54571997-06-03 22:21:47 +00007#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +00008
9/* The following parameters are copied from zutil.h, version 0.95 */
10#define DEFLATED 8
11#if MAX_MEM_LEVEL >= 8
12# define DEF_MEM_LEVEL 8
13#else
14# define DEF_MEM_LEVEL MAX_MEM_LEVEL
15#endif
16#define DEF_WBITS MAX_WBITS
17
18/* The output buffer will be increased in chunks of ADDCHUNK bytes. */
Jeremy Hylton41b9f001997-08-13 23:19:55 +000019#define DEFAULTALLOC 16*1024
Guido van Rossumfb221561997-04-29 15:38:09 +000020#define PyInit_zlib initzlib
21
22staticforward PyTypeObject Comptype;
23staticforward PyTypeObject Decomptype;
24
25static PyObject *ZlibError;
26
27typedef struct
28{
29 PyObject_HEAD
30 z_stream zst;
31} compobject;
32
Guido van Rossum3c540301997-06-03 22:21:03 +000033static char compressobj__doc__[] =
34"compressobj() -- Return a compressor object.\n"
35"compressobj(level) -- Return a compressor object, using the given compression level.\n"
36;
37
38static char decompressobj__doc__[] =
39"decompressobj() -- Return a decompressor object.\n"
40"decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n"
41;
42
Guido van Rossumfb221561997-04-29 15:38:09 +000043static compobject *
44newcompobject(type)
45 PyTypeObject *type;
46{
47 compobject *self;
48 self = PyObject_NEW(compobject, type);
49 if (self == NULL)
50 return NULL;
51 return self;
52}
53
Guido van Rossum3c540301997-06-03 22:21:03 +000054static char compress__doc__[] =
55"compress(string) -- Compress string using the default compression level, "
56"returning a string containing compressed data.\n"
57"compress(string, level) -- Compress string, using the chosen compression "
58"level (from 1 to 9). Return a string containing the compressed data.\n"
59;
60
Guido van Rossumfb221561997-04-29 15:38:09 +000061static PyObject *
62PyZlib_compress(self, args)
63 PyObject *self;
64 PyObject *args;
65{
66 PyObject *ReturnVal;
67 Byte *input, *output;
68 int length, level=Z_DEFAULT_COMPRESSION, err;
69 z_stream zst;
70
71 if (!PyArg_ParseTuple(args, "s#|i", &input, &length, &level))
72 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +000073 zst.avail_out = length + length/1000 + 12 + 1;
Guido van Rossumfb221561997-04-29 15:38:09 +000074 output=(Byte*)malloc(zst.avail_out);
75 if (output==NULL)
76 {
77 PyErr_SetString(PyExc_MemoryError,
78 "Can't allocate memory to compress data");
79 return NULL;
80 }
Guido van Rossum97b54571997-06-03 22:21:47 +000081 zst.zalloc=(alloc_func)NULL;
82 zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +000083 zst.next_out=(Byte *)output;
84 zst.next_in =(Byte *)input;
85 zst.avail_in=length;
86 err=deflateInit(&zst, level);
87 switch(err)
88 {
89 case(Z_OK):
90 break;
91 case(Z_MEM_ERROR):
92 PyErr_SetString(PyExc_MemoryError,
93 "Out of memory while compressing data");
94 free(output);
95 return NULL;
96 break;
97 case(Z_STREAM_ERROR):
98 PyErr_SetString(ZlibError,
99 "Bad compression level");
100 free(output);
101 return NULL;
102 break;
103 default:
104 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000105 if (zst.msg == Z_NULL)
106 PyErr_Format(ZlibError, "Error %i while compressing data",
107 err);
108 else
109 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
110 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000111 deflateEnd(&zst);
112 free(output);
113 return NULL;
114 }
115 break;
116 }
117
118 err=deflate(&zst, Z_FINISH);
119 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000120 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000121 case(Z_STREAM_END):
122 break;
123 /* Are there other errors to be trapped here? */
124 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000125 {
126 if (zst.msg == Z_NULL)
127 PyErr_Format(ZlibError, "Error %i while compressing data",
128 err);
129 else
130 PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
131 err, zst.msg);
132 deflateEnd(&zst);
133 free(output);
134 return NULL;
135 }
136 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000137 err=deflateEnd(&zst);
138 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000139 {
140 if (zst.msg == Z_NULL)
141 PyErr_Format(ZlibError, "Error %i while finishing compression",
142 err);
143 else
144 PyErr_Format(ZlibError,
145 "Error %i while finishing compression: %.200s",
146 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000147 free(output);
148 return NULL;
149 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000150 ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000151 free(output);
152 return ReturnVal;
153}
154
Guido van Rossum3c540301997-06-03 22:21:03 +0000155static char decompress__doc__[] =
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000156"decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n"
157"decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n"
158"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 +0000159;
160
Guido van Rossumfb221561997-04-29 15:38:09 +0000161static PyObject *
162PyZlib_decompress(self, args)
163 PyObject *self;
164 PyObject *args;
165{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000166 PyObject *result_str;
167 Byte *input;
Guido van Rossumfb221561997-04-29 15:38:09 +0000168 int length, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000169 int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000170 z_stream zst;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000171 if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen))
Guido van Rossumfb221561997-04-29 15:38:09 +0000172 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000173
174 if (r_strlen <= 0)
175 r_strlen = 1;
Guido van Rossumfb221561997-04-29 15:38:09 +0000176
177 zst.avail_in=length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000178 zst.avail_out=r_strlen;
179 if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
180 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000181 PyErr_SetString(PyExc_MemoryError,
182 "Can't allocate memory to decompress data");
183 return NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000184 }
Guido van Rossum97b54571997-06-03 22:21:47 +0000185 zst.zalloc=(alloc_func)NULL;
186 zst.zfree=(free_func)Z_NULL;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000187 zst.next_out=(Byte *)PyString_AsString(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000188 zst.next_in =(Byte *)input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000189 err=inflateInit2(&zst, wsize);
Guido van Rossumfb221561997-04-29 15:38:09 +0000190 switch(err)
191 {
192 case(Z_OK):
193 break;
194 case(Z_MEM_ERROR):
195 PyErr_SetString(PyExc_MemoryError,
196 "Out of memory while decompressing data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000197 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000198 return NULL;
199 default:
200 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000201 if (zst.msg == Z_NULL)
202 PyErr_Format(ZlibError, "Error %i preparing to decompress data",
203 err);
204 else
205 PyErr_Format(ZlibError,
206 "Error %i while preparing to decompress data: %.200s",
207 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000208 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000209 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000210 return NULL;
211 }
212 }
213 do
214 {
215 err=inflate(&zst, Z_FINISH);
216 switch(err)
217 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000218 case(Z_STREAM_END):
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000219 break;
220 case(Z_OK):
221 /* need more memory */
222 if (_PyString_Resize(&result_str, r_strlen << 1) == -1)
Guido van Rossumfb221561997-04-29 15:38:09 +0000223 {
224 PyErr_SetString(PyExc_MemoryError,
225 "Out of memory while decompressing data");
226 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000227 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000228 return NULL;
229 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000230 zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000231 zst.avail_out=r_strlen;
232 r_strlen = r_strlen << 1;
233 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000234 default:
235 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000236 if (zst.msg == Z_NULL)
237 PyErr_Format(ZlibError, "Error %i while decompressing data",
238 err);
239 else
240 PyErr_Format(ZlibError,
241 "Error %i while decompressing data: %.200s",
242 err, zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000243 inflateEnd(&zst);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000244 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000245 return NULL;
246 }
247 }
248 } while(err!=Z_STREAM_END);
249
250 err=inflateEnd(&zst);
251 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000252 {
253 if (zst.msg == Z_NULL)
254 PyErr_Format(ZlibError,
255 "Error %i while finishing data decompression",
256 err);
257 else
258 PyErr_Format(ZlibError,
259 "Error %i while finishing data decompression: %.200s",
260 err, zst.msg);
261 Py_DECREF(result_str);
Guido van Rossumfb221561997-04-29 15:38:09 +0000262 return NULL;
263 }
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000264 _PyString_Resize(&result_str, zst.total_out);
265 return result_str;
Guido van Rossumfb221561997-04-29 15:38:09 +0000266}
267
268static PyObject *
269PyZlib_compressobj(selfptr, args)
270 PyObject *selfptr;
271 PyObject *args;
272{
273 compobject *self;
274 int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
275 int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000276
277 if (!PyArg_ParseTuple(args, "|iiiii", &level, &method, &wbits,
278 &memLevel, &strategy))
279 return NULL;
280
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000281 self = newcompobject(&Comptype);
Guido van Rossumfb221561997-04-29 15:38:09 +0000282 if (self==NULL) return(NULL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000283 self->zst.zalloc = (alloc_func)NULL;
284 self->zst.zfree = (free_func)Z_NULL;
285 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Guido van Rossumfb221561997-04-29 15:38:09 +0000286 switch(err)
287 {
288 case (Z_OK):
289 return (PyObject*)self;
290 break;
291 case (Z_MEM_ERROR):
292 PyErr_SetString(PyExc_MemoryError,
293 "Can't allocate memory for compression object");
294 return NULL;
295 break;
296 case(Z_STREAM_ERROR):
297 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000298 "Invalid initialization option");
Guido van Rossumfb221561997-04-29 15:38:09 +0000299 return NULL;
300 break;
301 default:
302 {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000303 if (self->zst.msg == Z_NULL)
304 PyErr_Format(ZlibError,
305 "Error %i while creating compression object",
306 err);
307 else
308 PyErr_Format(ZlibError,
309 "Error %i while creating compression object: %.200s",
310 err, self->zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000311 return NULL;
312 break;
313 }
314 }
315}
316
317static PyObject *
318PyZlib_decompressobj(selfptr, args)
319 PyObject *selfptr;
320 PyObject *args;
321{
322 int wbits=DEF_WBITS, err;
323 compobject *self;
324 if (!PyArg_ParseTuple(args, "|i", &wbits))
325 {
326 return NULL;
327 }
328 self=newcompobject(&Decomptype);
329 if (self==NULL) return(NULL);
Guido van Rossum97b54571997-06-03 22:21:47 +0000330 self->zst.zalloc=(alloc_func)NULL;
331 self->zst.zfree=(free_func)Z_NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000332 err=inflateInit2(&self->zst, wbits);
333 switch(err)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000334 {
Guido van Rossumfb221561997-04-29 15:38:09 +0000335 case (Z_OK):
336 return (PyObject*)self;
337 break;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000338 case(Z_STREAM_ERROR):
339 PyErr_SetString(PyExc_ValueError,
340 "Invalid initialization option");
341 return NULL;
342 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000343 case (Z_MEM_ERROR):
344 PyErr_SetString(PyExc_MemoryError,
345 "Can't allocate memory for decompression object");
346 return NULL;
347 break;
348 default:
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000349 {
350 if (self->zst.msg == Z_NULL)
351 PyErr_Format(ZlibError,
352 "Error %i while creating decompression object",
353 err);
354 else
355 PyErr_Format(ZlibError,
356 "Error %i while creating decompression object: %.200s",
357 err, self->zst.msg);
Guido van Rossumfb221561997-04-29 15:38:09 +0000358 return NULL;
359 break;
Guido van Rossumfb221561997-04-29 15:38:09 +0000360 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000361 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000362}
363
364static void
365Comp_dealloc(self)
366 compobject *self;
367{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000368 deflateEnd(&self->zst);
369 PyMem_DEL(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000370}
371
372static void
373Decomp_dealloc(self)
374 compobject *self;
375{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000376 inflateEnd(&self->zst);
377 PyMem_DEL(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000378}
379
Guido van Rossum3c540301997-06-03 22:21:03 +0000380static char comp_compress__doc__[] =
381"compress(data) -- Return a string containing a compressed version of the data.\n\n"
382"After calling this function, some of the input data may still\n"
383"be stored in internal buffers for later processing.\n"
384"Call the flush() method to clear these buffers."
385;
386
387
Guido van Rossumfb221561997-04-29 15:38:09 +0000388static PyObject *
389PyZlib_objcompress(self, args)
390 compobject *self;
391 PyObject *args;
392{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000393 int err = Z_OK, inplen;
394 int length = DEFAULTALLOC;
Guido van Rossumfb221561997-04-29 15:38:09 +0000395 PyObject *RetVal;
396 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000397 unsigned long start_total_out;
Guido van Rossumfb221561997-04-29 15:38:09 +0000398
399 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000400 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000401 self->zst.avail_in = inplen;
402 self->zst.next_in = input;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000403 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
404 PyErr_SetString(PyExc_MemoryError,
405 "Can't allocate memory to compress data");
406 return NULL;
407 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000408 start_total_out = self->zst.total_out;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000409 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000410 self->zst.avail_out = length;
411 while (self->zst.avail_in != 0 && err == Z_OK)
412 {
413 err = deflate(&(self->zst), Z_NO_FLUSH);
414 if (self->zst.avail_out <= 0) {
415 if (_PyString_Resize(&RetVal, length << 1) == -1) {
416 PyErr_SetString(PyExc_MemoryError,
417 "Can't allocate memory to compress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000418 Py_DECREF(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000419 return NULL;
420 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000421 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000422 self->zst.avail_out = length;
423 length = length << 1;
424 }
425 }
426 if (err != Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000427 {
428 if (self->zst.msg == Z_NULL)
429 PyErr_Format(ZlibError, "Error %i while compressing",
430 err);
431 else
432 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
433 err, self->zst.msg);
434 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000435 return NULL;
436 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000437 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000438 return RetVal;
439}
440
Guido van Rossum3c540301997-06-03 22:21:03 +0000441static char decomp_decompress__doc__[] =
442"decompress(data) -- Return a string containing the decompressed version of the data.\n\n"
443"After calling this function, some of the input data may still\n"
444"be stored in internal buffers for later processing.\n"
445"Call the flush() method to clear these buffers."
446;
447
Guido van Rossumfb221561997-04-29 15:38:09 +0000448static PyObject *
449PyZlib_objdecompress(self, args)
450 compobject *self;
451 PyObject *args;
452{
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000453 int length, err, inplen;
Guido van Rossumfb221561997-04-29 15:38:09 +0000454 PyObject *RetVal;
455 Byte *input;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000456 unsigned long start_total_out;
457
Guido van Rossumfb221561997-04-29 15:38:09 +0000458 if (!PyArg_ParseTuple(args, "s#", &input, &inplen))
459 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000460 start_total_out = self->zst.total_out;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000461 RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000462 self->zst.avail_in = inplen;
463 self->zst.next_in = input;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000464 self->zst.avail_out = length = DEFAULTALLOC;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000465 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000466 err = Z_OK;
Jeremy Hyltona74ef661997-08-13 21:39:18 +0000467
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000468 while (self->zst.avail_in != 0 && err == Z_OK)
469 {
470 err = inflate(&(self->zst), Z_NO_FLUSH);
471 if (err == Z_OK && self->zst.avail_out <= 0)
472 {
473 if (_PyString_Resize(&RetVal, length << 1) == -1)
474 {
475 PyErr_SetString(PyExc_MemoryError,
476 "Can't allocate memory to compress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000477 Py_DECREF(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000478 return NULL;
479 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000480 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000481 self->zst.avail_out = length;
482 length = length << 1;
483 }
484 }
485
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000486 if (err != Z_OK && err != Z_STREAM_END)
487 {
488 if (self->zst.msg == Z_NULL)
489 PyErr_Format(ZlibError, "Error %i while decompressing",
490 err);
491 else
492 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
493 err, self->zst.msg);
494 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000495 return NULL;
496 }
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000497 _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Guido van Rossumfb221561997-04-29 15:38:09 +0000498 return RetVal;
499}
500
Guido van Rossum3c540301997-06-03 22:21:03 +0000501static char comp_flush__doc__[] =
502"flush() -- Return a string containing any remaining compressed data. "
503"The compressor object can no longer be used after this call."
504;
505
Guido van Rossumfb221561997-04-29 15:38:09 +0000506static PyObject *
507PyZlib_flush(self, args)
508 compobject *self;
509 PyObject *args;
510{
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000511 int length=DEFAULTALLOC, err = Z_OK;
Guido van Rossumfb221561997-04-29 15:38:09 +0000512 PyObject *RetVal;
513
514 if (!PyArg_NoArgs(args))
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000515 return NULL;
516 self->zst.avail_in = 0;
517 self->zst.next_in = Z_NULL;
518 if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
519 PyErr_SetString(PyExc_MemoryError,
520 "Can't allocate memory to compress data");
521 return NULL;
522 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000523 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000524 self->zst.avail_out = length;
525 while (err == Z_OK)
526 {
527 err = deflate(&(self->zst), Z_FINISH);
528 if (self->zst.avail_out <= 0) {
529 if (_PyString_Resize(&RetVal, length << 1) == -1) {
530 PyErr_SetString(PyExc_MemoryError,
531 "Can't allocate memory to compress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000532 Py_DECREF(RetVal);
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000533 return NULL;
534 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000535 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000536 self->zst.avail_out = length;
537 length = length << 1;
538 }
539 }
540 if (err!=Z_STREAM_END) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000541 if (self->zst.msg == Z_NULL)
542 PyErr_Format(ZlibError, "Error %i while compressing",
543 err);
544 else
545 PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
546 err, self->zst.msg);
547 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000548 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000549 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000550 err=deflateEnd(&(self->zst));
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000551 if (err!=Z_OK) {
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000552 if (self->zst.msg == Z_NULL)
553 PyErr_Format(ZlibError, "Error %i while flushing compression object",
554 err);
555 else
556 PyErr_Format(ZlibError,
557 "Error %i while flushing compression object: %.200s",
558 err, self->zst.msg);
559 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000560 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000561 }
562 _PyString_Resize(&RetVal,
563 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000564 return RetVal;
565}
566
Guido van Rossum3c540301997-06-03 22:21:03 +0000567static char decomp_flush__doc__[] =
568"flush() -- Return a string containing any remaining decompressed data. "
569"The decompressor object can no longer be used after this call."
570;
571
Guido van Rossumfb221561997-04-29 15:38:09 +0000572static PyObject *
573PyZlib_unflush(self, args)
574 compobject *self;
575 PyObject *args;
576{
577 int length=0, err;
Guido van Rossumfb221561997-04-29 15:38:09 +0000578 PyObject *RetVal;
579
580 if (!PyArg_NoArgs(args))
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000581 return NULL;
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000582 if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC)))
583 {
584 PyErr_SetString(PyExc_MemoryError,
585 "Can't allocate memory to decompress data");
586 return NULL;
587 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000588 self->zst.avail_in=0;
Guido van Rossumed2554a1997-08-18 15:31:24 +0000589 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000590 length = self->zst.avail_out = DEFAULTALLOC;
591
592 err = Z_OK;
593 while (err == Z_OK)
594 {
595 err = inflate(&(self->zst), Z_FINISH);
596 if (err == Z_OK && self->zst.avail_out == 0)
597 {
598 if (_PyString_Resize(&RetVal, length << 1) == -1)
599 {
600 PyErr_SetString(PyExc_MemoryError,
601 "Can't allocate memory to decompress data");
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000602 Py_DECREF(RetVal);
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000603 return NULL;
604 }
Guido van Rossumed2554a1997-08-18 15:31:24 +0000605 self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000606 self->zst.avail_out = length;
607 length = length << 1;
608 }
609 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000610 if (err!=Z_STREAM_END)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000611 {
612 if (self->zst.msg == Z_NULL)
613 PyErr_Format(ZlibError, "Error %i while decompressing",
614 err);
615 else
616 PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
617 err, self->zst.msg);
618 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000619 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000620 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000621 err=inflateEnd(&(self->zst));
622 if (err!=Z_OK)
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000623 {
624 if (self->zst.msg == Z_NULL)
625 PyErr_Format(ZlibError,
626 "Error %i while flushing decompression object",
627 err);
628 else
629 PyErr_Format(ZlibError,
630 "Error %i while flushing decompression object: %.200s",
631 err, self->zst.msg);
632 Py_DECREF(RetVal);
Guido van Rossumfb221561997-04-29 15:38:09 +0000633 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000634 }
Jeremy Hylton644c17d1997-08-14 21:06:42 +0000635 _PyString_Resize(&RetVal,
636 (char *)self->zst.next_out - PyString_AsString(RetVal));
Guido van Rossumfb221561997-04-29 15:38:09 +0000637 return RetVal;
638}
639
640static PyMethodDef comp_methods[] =
641{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000642 {"compress", (binaryfunc)PyZlib_objcompress, 1, comp_compress__doc__},
643 {"flush", (binaryfunc)PyZlib_flush, 0, comp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000644 {NULL, NULL}
645};
646
647static PyMethodDef Decomp_methods[] =
648{
Guido van Rossumc1f08821997-08-28 21:21:22 +0000649 {"decompress", (binaryfunc)PyZlib_objdecompress, 1, decomp_decompress__doc__},
650 {"flush", (binaryfunc)PyZlib_unflush, 0, decomp_flush__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000651 {NULL, NULL}
652};
653
654static PyObject *
655Comp_getattr(self, name)
656 compobject *self;
657 char *name;
658{
659 return Py_FindMethod(comp_methods, (PyObject *)self, name);
660}
661
662static PyObject *
663Decomp_getattr(self, name)
664 compobject *self;
665 char *name;
666{
667 return Py_FindMethod(Decomp_methods, (PyObject *)self, name);
668}
669
Guido van Rossum3c540301997-06-03 22:21:03 +0000670static char adler32__doc__[] =
671"adler32(string) -- Compute an Adler-32 checksum of string, using "
672"a default starting value, and returning an integer value.\n"
673"adler32(string, value) -- Compute an Adler-32 checksum of string, using "
674"the starting value provided, and returning an integer value\n"
675;
676
Guido van Rossumfb221561997-04-29 15:38:09 +0000677static PyObject *
678PyZlib_adler32(self, args)
679 PyObject *self, *args;
680{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000681 uLong adler32val=adler32(0L, Z_NULL, 0);
682 Byte *buf;
683 int len;
Guido van Rossumfb221561997-04-29 15:38:09 +0000684
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000685 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val))
686 {
687 return NULL;
688 }
689 adler32val = adler32(adler32val, buf, len);
690 return PyInt_FromLong(adler32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000691}
692
Guido van Rossum3c540301997-06-03 22:21:03 +0000693static char crc32__doc__[] =
694"crc32(string) -- Compute a CRC-32 checksum of string, using "
695"a default starting value, and returning an integer value.\n"
696"crc32(string, value) -- Compute a CRC-32 checksum of string, using "
697"the starting value provided, and returning an integer value.\n"
698;
Guido van Rossumfb221561997-04-29 15:38:09 +0000699
700static PyObject *
701PyZlib_crc32(self, args)
702 PyObject *self, *args;
703{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000704 uLong crc32val=crc32(0L, Z_NULL, 0);
705 Byte *buf;
706 int len;
707 if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val))
708 {
709 return NULL;
710 }
711 crc32val = crc32(crc32val, buf, len);
712 return PyInt_FromLong(crc32val);
Guido van Rossumfb221561997-04-29 15:38:09 +0000713}
714
715
716static PyMethodDef zlib_methods[] =
717{
Guido van Rossum3c540301997-06-03 22:21:03 +0000718 {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__},
719 {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__},
720 {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__},
721 {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__},
722 {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__},
723 {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__},
Guido van Rossumfb221561997-04-29 15:38:09 +0000724 {NULL, NULL}
725};
726
727statichere PyTypeObject Comptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000728 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000729 0,
730 "Compress",
731 sizeof(compobject),
732 0,
733 (destructor)Comp_dealloc, /*tp_dealloc*/
734 0, /*tp_print*/
735 (getattrfunc)Comp_getattr, /*tp_getattr*/
736 0, /*tp_setattr*/
737 0, /*tp_compare*/
738 0, /*tp_repr*/
739 0, /*tp_as_number*/
740 0, /*tp_as_sequence*/
741 0, /*tp_as_mapping*/
742};
743
744statichere PyTypeObject Decomptype = {
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000745 PyObject_HEAD_INIT(0)
Guido van Rossumfb221561997-04-29 15:38:09 +0000746 0,
747 "Decompress",
748 sizeof(compobject),
749 0,
750 (destructor)Decomp_dealloc, /*tp_dealloc*/
751 0, /*tp_print*/
752 (getattrfunc)Decomp_getattr, /*tp_getattr*/
753 0, /*tp_setattr*/
754 0, /*tp_compare*/
755 0, /*tp_repr*/
756 0, /*tp_as_number*/
757 0, /*tp_as_sequence*/
758 0, /*tp_as_mapping*/
759};
760
761/* The following insint() routine was blatantly ripped off from
762 socketmodule.c */
763
764/* Convenience routine to export an integer value.
765 For simplicity, errors (which are unlikely anyway) are ignored. */
766static void
767insint(d, name, value)
768 PyObject *d;
769 char *name;
770 int value;
771{
772 PyObject *v = PyInt_FromLong((long) value);
773 if (v == NULL) {
774 /* Don't bother reporting this error */
775 PyErr_Clear();
776 }
777 else {
778 PyDict_SetItemString(d, name, v);
779 Py_DECREF(v);
780 }
781}
782
Guido van Rossum3c540301997-06-03 22:21:03 +0000783static char zlib_module_documentation[]=
784"The functions in this module allow compression and decompression "
785"using the zlib library, which is based on GNU zip. \n\n"
786"adler32(string) -- Compute an Adler-32 checksum.\n"
787"adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n"
788"compress(string) -- Compress a string.\n"
789"compress(string, level) -- Compress a string with the given level of compression (1--9).\n"
790"compressobj([level]) -- Return a compressor object.\n"
791"crc32(string) -- Compute a CRC-32 checksum.\n"
792"crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n"
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000793"decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n"
Guido van Rossum3c540301997-06-03 22:21:03 +0000794"decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n"
795"Compressor objects support compress() and flush() methods; decompressor \n"
796"objects support decompress() and flush()."
797;
798
Guido van Rossumfb221561997-04-29 15:38:09 +0000799void
800PyInit_zlib()
801{
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000802 PyObject *m, *d, *ver;
Guido van Rossum9ec0f8b1997-12-18 05:21:29 +0000803 Comptype.ob_type = &PyType_Type;
804 Decomptype.ob_type = &PyType_Type;
Guido van Rossum3c540301997-06-03 22:21:03 +0000805 m = Py_InitModule4("zlib", zlib_methods,
806 zlib_module_documentation,
807 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossumfb221561997-04-29 15:38:09 +0000808 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000809 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Guido van Rossumfb221561997-04-29 15:38:09 +0000810 PyDict_SetItemString(d, "error", ZlibError);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000811
Guido van Rossumfb221561997-04-29 15:38:09 +0000812 insint(d, "MAX_WBITS", MAX_WBITS);
813 insint(d, "DEFLATED", DEFLATED);
814 insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000815 insint(d, "Z_BEST_SPEED", Z_BEST_SPEED);
816 insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
817 insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
818 insint(d, "Z_FILTERED", Z_FILTERED);
819 insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
820 insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
821 ver = PyString_FromString(ZLIB_VERSION);
822 PyDict_SetItemString(d, "ZLIB_VERSION", ver);
Guido van Rossumfb221561997-04-29 15:38:09 +0000823}