Line data Source code
1 : /* zlibmodule.c -- gzip-compatible data compression */
2 : /* See http://www.gzip.org/zlib/ */
3 :
4 : /* Windows users: read Python's PCbuild\readme.txt */
5 :
6 :
7 : #include "Python.h"
8 : #include "structmember.h"
9 : #include "zlib.h"
10 :
11 : #ifdef WITH_THREAD
12 : #include "pythread.h"
13 : #define ENTER_ZLIB(obj) \
14 : Py_BEGIN_ALLOW_THREADS; \
15 : PyThread_acquire_lock((obj)->lock, 1); \
16 : Py_END_ALLOW_THREADS;
17 : #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
18 : #else
19 : #define ENTER_ZLIB(obj)
20 : #define LEAVE_ZLIB(obj)
21 : #endif
22 :
23 : /* The following parameters are copied from zutil.h, version 0.95 */
24 : #define DEFLATED 8
25 : #if MAX_MEM_LEVEL >= 8
26 : # define DEF_MEM_LEVEL 8
27 : #else
28 : # define DEF_MEM_LEVEL MAX_MEM_LEVEL
29 : #endif
30 : #define DEF_WBITS MAX_WBITS
31 :
32 : /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
33 : #define DEFAULTALLOC (16*1024)
34 :
35 : static PyTypeObject Comptype;
36 : static PyTypeObject Decomptype;
37 :
38 : static PyObject *ZlibError;
39 :
40 : typedef struct
41 : {
42 : PyObject_HEAD
43 : z_stream zst;
44 : PyObject *unused_data;
45 : PyObject *unconsumed_tail;
46 : char eof;
47 : int is_initialised;
48 : PyObject *zdict;
49 : #ifdef WITH_THREAD
50 : PyThread_type_lock lock;
51 : #endif
52 : } compobject;
53 :
54 : static void
55 0 : zlib_error(z_stream zst, int err, char *msg)
56 : {
57 0 : const char *zmsg = Z_NULL;
58 : /* In case of a version mismatch, zst.msg won't be initialized.
59 : Check for this case first, before looking at zst.msg. */
60 0 : if (err == Z_VERSION_ERROR)
61 0 : zmsg = "library version mismatch";
62 0 : if (zmsg == Z_NULL)
63 0 : zmsg = zst.msg;
64 0 : if (zmsg == Z_NULL) {
65 0 : switch (err) {
66 : case Z_BUF_ERROR:
67 0 : zmsg = "incomplete or truncated stream";
68 0 : break;
69 : case Z_STREAM_ERROR:
70 0 : zmsg = "inconsistent stream state";
71 0 : break;
72 : case Z_DATA_ERROR:
73 0 : zmsg = "invalid input data";
74 0 : break;
75 : }
76 : }
77 0 : if (zmsg == Z_NULL)
78 0 : PyErr_Format(ZlibError, "Error %d %s", err, msg);
79 : else
80 0 : PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
81 0 : }
82 :
83 : PyDoc_STRVAR(compressobj__doc__,
84 : "compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
85 : " strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
86 : " -- Return a compressor object.\n"
87 : "\n"
88 : "level is the compression level (an integer in the range 0-9; default is 6).\n"
89 : "Higher compression levels are slower, but produce smaller results.\n"
90 : "\n"
91 : "method is the compression algorithm. If given, this must be DEFLATED.\n"
92 : "\n"
93 : "wbits is the base two logarithm of the window size (range: 8..15).\n"
94 : "\n"
95 : "memlevel controls the amount of memory used for internal compression state.\n"
96 : "Valid values range from 1 to 9. Higher values result in higher memory usage,\n"
97 : "faster compression, and smaller output.\n"
98 : "\n"
99 : "strategy is used to tune the compression algorithm. Possible values are\n"
100 : "Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
101 : "\n"
102 : "zdict is the predefined compression dictionary - a sequence of bytes\n"
103 : "containing subsequences that are likely to occur in the input data.");
104 :
105 : PyDoc_STRVAR(decompressobj__doc__,
106 : "decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
107 : "\n"
108 : "Optional arg wbits is the window buffer size.\n"
109 : "\n"
110 : "Optional arg zdict is the predefined compression dictionary. This must be\n"
111 : "the same dictionary as used by the compressor that produced the input data.");
112 :
113 : static compobject *
114 0 : newcompobject(PyTypeObject *type)
115 : {
116 : compobject *self;
117 0 : self = PyObject_New(compobject, type);
118 0 : if (self == NULL)
119 0 : return NULL;
120 0 : self->eof = 0;
121 0 : self->is_initialised = 0;
122 0 : self->zdict = NULL;
123 0 : self->unused_data = PyBytes_FromStringAndSize("", 0);
124 0 : if (self->unused_data == NULL) {
125 0 : Py_DECREF(self);
126 0 : return NULL;
127 : }
128 0 : self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
129 0 : if (self->unconsumed_tail == NULL) {
130 0 : Py_DECREF(self);
131 0 : return NULL;
132 : }
133 : #ifdef WITH_THREAD
134 0 : self->lock = PyThread_allocate_lock();
135 : #endif
136 0 : return self;
137 : }
138 :
139 : PyDoc_STRVAR(compress__doc__,
140 : "compress(string[, level]) -- Returned compressed string.\n"
141 : "\n"
142 : "Optional arg level is the compression level, in 1-9.");
143 :
144 : static PyObject *
145 0 : PyZlib_compress(PyObject *self, PyObject *args)
146 : {
147 0 : PyObject *ReturnVal = NULL;
148 : Py_buffer pinput;
149 0 : Byte *input, *output = NULL;
150 : unsigned int length;
151 0 : int level=Z_DEFAULT_COMPRESSION, err;
152 : z_stream zst;
153 :
154 : /* require Python string object, optional 'level' arg */
155 0 : if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
156 0 : return NULL;
157 :
158 : if (pinput.len > UINT_MAX) {
159 : PyErr_SetString(PyExc_OverflowError,
160 : "Size does not fit in an unsigned int");
161 : goto error;
162 : }
163 0 : input = pinput.buf;
164 0 : length = pinput.len;
165 :
166 0 : zst.avail_out = length + length/1000 + 12 + 1;
167 :
168 0 : output = (Byte*)malloc(zst.avail_out);
169 0 : if (output == NULL) {
170 0 : PyErr_SetString(PyExc_MemoryError,
171 : "Can't allocate memory to compress data");
172 0 : goto error;
173 : }
174 :
175 : /* Past the point of no return. From here on out, we need to make sure
176 : we clean up mallocs & INCREFs. */
177 :
178 0 : zst.zalloc = (alloc_func)NULL;
179 0 : zst.zfree = (free_func)Z_NULL;
180 0 : zst.next_out = (Byte *)output;
181 0 : zst.next_in = (Byte *)input;
182 0 : zst.avail_in = length;
183 0 : err = deflateInit(&zst, level);
184 :
185 0 : switch(err) {
186 : case(Z_OK):
187 0 : break;
188 : case(Z_MEM_ERROR):
189 0 : PyErr_SetString(PyExc_MemoryError,
190 : "Out of memory while compressing data");
191 0 : goto error;
192 : case(Z_STREAM_ERROR):
193 0 : PyErr_SetString(ZlibError,
194 : "Bad compression level");
195 0 : goto error;
196 : default:
197 0 : deflateEnd(&zst);
198 0 : zlib_error(zst, err, "while compressing data");
199 0 : goto error;
200 : }
201 :
202 0 : Py_BEGIN_ALLOW_THREADS;
203 0 : err = deflate(&zst, Z_FINISH);
204 0 : Py_END_ALLOW_THREADS;
205 :
206 0 : if (err != Z_STREAM_END) {
207 0 : zlib_error(zst, err, "while compressing data");
208 0 : deflateEnd(&zst);
209 0 : goto error;
210 : }
211 :
212 0 : err=deflateEnd(&zst);
213 0 : if (err == Z_OK)
214 0 : ReturnVal = PyBytes_FromStringAndSize((char *)output,
215 0 : zst.total_out);
216 : else
217 0 : zlib_error(zst, err, "while finishing compression");
218 :
219 : error:
220 0 : PyBuffer_Release(&pinput);
221 0 : free(output);
222 :
223 0 : return ReturnVal;
224 : }
225 :
226 : PyDoc_STRVAR(decompress__doc__,
227 : "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
228 : "\n"
229 : "Optional arg wbits is the window buffer size. Optional arg bufsize is\n"
230 : "the initial output buffer size.");
231 :
232 : static PyObject *
233 0 : PyZlib_decompress(PyObject *self, PyObject *args)
234 : {
235 0 : PyObject *result_str = NULL;
236 : Py_buffer pinput;
237 : Byte *input;
238 : unsigned int length;
239 : int err;
240 0 : int wsize=DEF_WBITS;
241 0 : Py_ssize_t r_strlen=DEFAULTALLOC;
242 : z_stream zst;
243 :
244 0 : if (!PyArg_ParseTuple(args, "y*|in:decompress",
245 : &pinput, &wsize, &r_strlen))
246 0 : return NULL;
247 :
248 : if (pinput.len > UINT_MAX) {
249 : PyErr_SetString(PyExc_OverflowError,
250 : "Size does not fit in an unsigned int");
251 : goto error;
252 : }
253 0 : input = pinput.buf;
254 0 : length = pinput.len;
255 :
256 0 : if (r_strlen <= 0)
257 0 : r_strlen = 1;
258 :
259 0 : zst.avail_in = length;
260 0 : zst.avail_out = r_strlen;
261 :
262 0 : if (!(result_str = PyBytes_FromStringAndSize(NULL, r_strlen)))
263 0 : goto error;
264 :
265 0 : zst.zalloc = (alloc_func)NULL;
266 0 : zst.zfree = (free_func)Z_NULL;
267 0 : zst.next_out = (Byte *)PyBytes_AS_STRING(result_str);
268 0 : zst.next_in = (Byte *)input;
269 0 : err = inflateInit2(&zst, wsize);
270 :
271 0 : switch(err) {
272 : case(Z_OK):
273 0 : break;
274 : case(Z_MEM_ERROR):
275 0 : PyErr_SetString(PyExc_MemoryError,
276 : "Out of memory while decompressing data");
277 0 : goto error;
278 : default:
279 0 : inflateEnd(&zst);
280 0 : zlib_error(zst, err, "while preparing to decompress data");
281 0 : goto error;
282 : }
283 :
284 : do {
285 0 : Py_BEGIN_ALLOW_THREADS
286 0 : err=inflate(&zst, Z_FINISH);
287 0 : Py_END_ALLOW_THREADS
288 :
289 0 : switch(err) {
290 : case(Z_STREAM_END):
291 0 : break;
292 : case(Z_BUF_ERROR):
293 : /*
294 : * If there is at least 1 byte of room according to zst.avail_out
295 : * and we get this error, assume that it means zlib cannot
296 : * process the inflate call() due to an error in the data.
297 : */
298 0 : if (zst.avail_out > 0) {
299 0 : zlib_error(zst, err, "while decompressing data");
300 0 : inflateEnd(&zst);
301 0 : goto error;
302 : }
303 : /* fall through */
304 : case(Z_OK):
305 : /* need more memory */
306 0 : if (_PyBytes_Resize(&result_str, r_strlen << 1) < 0) {
307 0 : inflateEnd(&zst);
308 0 : goto error;
309 : }
310 0 : zst.next_out =
311 0 : (unsigned char *)PyBytes_AS_STRING(result_str) + r_strlen;
312 0 : zst.avail_out = r_strlen;
313 0 : r_strlen = r_strlen << 1;
314 0 : break;
315 : default:
316 0 : inflateEnd(&zst);
317 0 : zlib_error(zst, err, "while decompressing data");
318 0 : goto error;
319 : }
320 0 : } while (err != Z_STREAM_END);
321 :
322 0 : err = inflateEnd(&zst);
323 0 : if (err != Z_OK) {
324 0 : zlib_error(zst, err, "while finishing decompression");
325 0 : goto error;
326 : }
327 :
328 0 : if (_PyBytes_Resize(&result_str, zst.total_out) < 0)
329 0 : goto error;
330 :
331 0 : PyBuffer_Release(&pinput);
332 0 : return result_str;
333 :
334 : error:
335 0 : PyBuffer_Release(&pinput);
336 0 : Py_XDECREF(result_str);
337 0 : return NULL;
338 : }
339 :
340 : static PyObject *
341 0 : PyZlib_compressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
342 : {
343 : compobject *self;
344 0 : int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
345 0 : int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
346 : Py_buffer zdict;
347 : static char *kwlist[] = {"level", "method", "wbits",
348 : "memLevel", "strategy", "zdict", NULL};
349 :
350 0 : zdict.buf = NULL; /* Sentinel, so we can tell whether zdict was supplied. */
351 0 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj",
352 : kwlist, &level, &method, &wbits,
353 : &memLevel, &strategy, &zdict))
354 0 : return NULL;
355 :
356 0 : self = newcompobject(&Comptype);
357 0 : if (self==NULL)
358 0 : goto error;
359 0 : self->zst.zalloc = (alloc_func)NULL;
360 0 : self->zst.zfree = (free_func)Z_NULL;
361 0 : self->zst.next_in = NULL;
362 0 : self->zst.avail_in = 0;
363 0 : err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
364 0 : switch(err) {
365 : case (Z_OK):
366 0 : self->is_initialised = 1;
367 0 : if (zdict.buf == NULL) {
368 0 : goto success;
369 : } else {
370 0 : err = deflateSetDictionary(&self->zst, zdict.buf, zdict.len);
371 0 : switch (err) {
372 : case (Z_OK):
373 0 : goto success;
374 : case (Z_STREAM_ERROR):
375 0 : PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
376 0 : goto error;
377 : default:
378 0 : PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
379 0 : goto error;
380 : }
381 : }
382 : case (Z_MEM_ERROR):
383 0 : PyErr_SetString(PyExc_MemoryError,
384 : "Can't allocate memory for compression object");
385 0 : goto error;
386 : case(Z_STREAM_ERROR):
387 0 : PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
388 0 : goto error;
389 : default:
390 0 : zlib_error(self->zst, err, "while creating compression object");
391 0 : goto error;
392 : }
393 :
394 : error:
395 0 : Py_XDECREF(self);
396 0 : self = NULL;
397 : success:
398 0 : if (zdict.buf != NULL)
399 0 : PyBuffer_Release(&zdict);
400 0 : return (PyObject*)self;
401 : }
402 :
403 : static PyObject *
404 0 : PyZlib_decompressobj(PyObject *selfptr, PyObject *args, PyObject *kwargs)
405 : {
406 : static char *kwlist[] = {"wbits", "zdict", NULL};
407 0 : int wbits=DEF_WBITS, err;
408 : compobject *self;
409 0 : PyObject *zdict=NULL;
410 :
411 0 : if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj",
412 : kwlist, &wbits, &zdict))
413 0 : return NULL;
414 0 : if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
415 0 : PyErr_SetString(PyExc_TypeError,
416 : "zdict argument must support the buffer protocol");
417 0 : return NULL;
418 : }
419 :
420 0 : self = newcompobject(&Decomptype);
421 0 : if (self == NULL)
422 0 : return(NULL);
423 0 : self->zst.zalloc = (alloc_func)NULL;
424 0 : self->zst.zfree = (free_func)Z_NULL;
425 0 : self->zst.next_in = NULL;
426 0 : self->zst.avail_in = 0;
427 0 : if (zdict != NULL) {
428 0 : Py_INCREF(zdict);
429 0 : self->zdict = zdict;
430 : }
431 0 : err = inflateInit2(&self->zst, wbits);
432 0 : switch(err) {
433 : case (Z_OK):
434 0 : self->is_initialised = 1;
435 0 : return (PyObject*)self;
436 : case(Z_STREAM_ERROR):
437 0 : Py_DECREF(self);
438 0 : PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
439 0 : return NULL;
440 : case (Z_MEM_ERROR):
441 0 : Py_DECREF(self);
442 0 : PyErr_SetString(PyExc_MemoryError,
443 : "Can't allocate memory for decompression object");
444 0 : return NULL;
445 : default:
446 0 : zlib_error(self->zst, err, "while creating decompression object");
447 0 : Py_DECREF(self);
448 0 : return NULL;
449 : }
450 : }
451 :
452 : static void
453 0 : Dealloc(compobject *self)
454 : {
455 : #ifdef WITH_THREAD
456 0 : PyThread_free_lock(self->lock);
457 : #endif
458 0 : Py_XDECREF(self->unused_data);
459 0 : Py_XDECREF(self->unconsumed_tail);
460 0 : Py_XDECREF(self->zdict);
461 0 : PyObject_Del(self);
462 0 : }
463 :
464 : static void
465 0 : Comp_dealloc(compobject *self)
466 : {
467 0 : if (self->is_initialised)
468 0 : deflateEnd(&self->zst);
469 0 : Dealloc(self);
470 0 : }
471 :
472 : static void
473 0 : Decomp_dealloc(compobject *self)
474 : {
475 0 : if (self->is_initialised)
476 0 : inflateEnd(&self->zst);
477 0 : Dealloc(self);
478 0 : }
479 :
480 : PyDoc_STRVAR(comp_compress__doc__,
481 : "compress(data) -- Return a string containing data compressed.\n"
482 : "\n"
483 : "After calling this function, some of the input data may still\n"
484 : "be stored in internal buffers for later processing.\n"
485 : "Call the flush() method to clear these buffers.");
486 :
487 :
488 : static PyObject *
489 0 : PyZlib_objcompress(compobject *self, PyObject *args)
490 : {
491 : int err;
492 : unsigned int inplen;
493 0 : Py_ssize_t length = DEFAULTALLOC;
494 0 : PyObject *RetVal = NULL;
495 : Py_buffer pinput;
496 : Byte *input;
497 : unsigned long start_total_out;
498 :
499 0 : if (!PyArg_ParseTuple(args, "y*:compress", &pinput))
500 0 : return NULL;
501 : if (pinput.len > UINT_MAX) {
502 : PyErr_SetString(PyExc_OverflowError,
503 : "Size does not fit in an unsigned int");
504 : goto error_outer;
505 : }
506 0 : input = pinput.buf;
507 0 : inplen = pinput.len;
508 :
509 0 : if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
510 0 : goto error_outer;
511 :
512 0 : ENTER_ZLIB(self);
513 :
514 0 : start_total_out = self->zst.total_out;
515 0 : self->zst.avail_in = inplen;
516 0 : self->zst.next_in = input;
517 0 : self->zst.avail_out = length;
518 0 : self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
519 :
520 0 : Py_BEGIN_ALLOW_THREADS
521 0 : err = deflate(&(self->zst), Z_NO_FLUSH);
522 0 : Py_END_ALLOW_THREADS
523 :
524 : /* while Z_OK and the output buffer is full, there might be more output,
525 : so extend the output buffer and try again */
526 0 : while (err == Z_OK && self->zst.avail_out == 0) {
527 0 : if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
528 0 : Py_DECREF(RetVal);
529 0 : RetVal = NULL;
530 0 : goto error;
531 : }
532 0 : self->zst.next_out =
533 0 : (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
534 0 : self->zst.avail_out = length;
535 0 : length = length << 1;
536 :
537 0 : Py_BEGIN_ALLOW_THREADS
538 0 : err = deflate(&(self->zst), Z_NO_FLUSH);
539 0 : Py_END_ALLOW_THREADS
540 : }
541 : /* We will only get Z_BUF_ERROR if the output buffer was full but
542 : there wasn't more output when we tried again, so it is not an error
543 : condition.
544 : */
545 :
546 0 : if (err != Z_OK && err != Z_BUF_ERROR) {
547 0 : zlib_error(self->zst, err, "while compressing data");
548 0 : Py_DECREF(RetVal);
549 0 : RetVal = NULL;
550 0 : goto error;
551 : }
552 0 : if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
553 0 : Py_DECREF(RetVal);
554 0 : RetVal = NULL;
555 : }
556 :
557 : error:
558 0 : LEAVE_ZLIB(self);
559 : error_outer:
560 0 : PyBuffer_Release(&pinput);
561 0 : return RetVal;
562 : }
563 :
564 : PyDoc_STRVAR(decomp_decompress__doc__,
565 : "decompress(data, max_length) -- Return a string containing the decompressed\n"
566 : "version of the data.\n"
567 : "\n"
568 : "After calling this function, some of the input data may still be stored in\n"
569 : "internal buffers for later processing.\n"
570 : "Call the flush() method to clear these buffers.\n"
571 : "If the max_length parameter is specified then the return value will be\n"
572 : "no longer than max_length. Unconsumed input data will be stored in\n"
573 : "the unconsumed_tail attribute.");
574 :
575 : static PyObject *
576 0 : PyZlib_objdecompress(compobject *self, PyObject *args)
577 : {
578 0 : int err, max_length = 0;
579 : unsigned int inplen;
580 0 : Py_ssize_t old_length, length = DEFAULTALLOC;
581 0 : PyObject *RetVal = NULL;
582 : Py_buffer pinput;
583 : Byte *input;
584 : unsigned long start_total_out;
585 :
586 0 : if (!PyArg_ParseTuple(args, "y*|i:decompress", &pinput,
587 : &max_length))
588 0 : return NULL;
589 : if (pinput.len > UINT_MAX) {
590 : PyErr_SetString(PyExc_OverflowError,
591 : "Size does not fit in an unsigned int");
592 : goto error_outer;
593 : }
594 0 : input = pinput.buf;
595 0 : inplen = pinput.len;
596 0 : if (max_length < 0) {
597 0 : PyErr_SetString(PyExc_ValueError,
598 : "max_length must be greater than zero");
599 0 : goto error_outer;
600 : }
601 :
602 : /* limit amount of data allocated to max_length */
603 0 : if (max_length && length > max_length)
604 0 : length = max_length;
605 0 : if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
606 0 : goto error_outer;
607 :
608 0 : ENTER_ZLIB(self);
609 :
610 0 : start_total_out = self->zst.total_out;
611 0 : self->zst.avail_in = inplen;
612 0 : self->zst.next_in = input;
613 0 : self->zst.avail_out = length;
614 0 : self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
615 :
616 0 : Py_BEGIN_ALLOW_THREADS
617 0 : err = inflate(&(self->zst), Z_SYNC_FLUSH);
618 0 : Py_END_ALLOW_THREADS
619 :
620 0 : if (err == Z_NEED_DICT && self->zdict != NULL) {
621 : Py_buffer zdict_buf;
622 0 : if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
623 0 : Py_DECREF(RetVal);
624 0 : RetVal = NULL;
625 : goto error;
626 : }
627 0 : err = inflateSetDictionary(&(self->zst), zdict_buf.buf, zdict_buf.len);
628 0 : PyBuffer_Release(&zdict_buf);
629 0 : if (err != Z_OK) {
630 0 : zlib_error(self->zst, err, "while decompressing data");
631 0 : Py_DECREF(RetVal);
632 0 : RetVal = NULL;
633 : goto error;
634 : }
635 : /* Repeat the call to inflate. */
636 0 : Py_BEGIN_ALLOW_THREADS
637 0 : err = inflate(&(self->zst), Z_SYNC_FLUSH);
638 0 : Py_END_ALLOW_THREADS
639 : }
640 :
641 : /* While Z_OK and the output buffer is full, there might be more output.
642 : So extend the output buffer and try again.
643 : */
644 0 : while (err == Z_OK && self->zst.avail_out == 0) {
645 : /* If max_length set, don't continue decompressing if we've already
646 : reached the limit.
647 : */
648 0 : if (max_length && length >= max_length)
649 0 : break;
650 :
651 : /* otherwise, ... */
652 0 : old_length = length;
653 0 : length = length << 1;
654 0 : if (max_length && length > max_length)
655 0 : length = max_length;
656 :
657 0 : if (_PyBytes_Resize(&RetVal, length) < 0) {
658 0 : Py_DECREF(RetVal);
659 0 : RetVal = NULL;
660 0 : goto error;
661 : }
662 0 : self->zst.next_out =
663 0 : (unsigned char *)PyBytes_AS_STRING(RetVal) + old_length;
664 0 : self->zst.avail_out = length - old_length;
665 :
666 0 : Py_BEGIN_ALLOW_THREADS
667 0 : err = inflate(&(self->zst), Z_SYNC_FLUSH);
668 0 : Py_END_ALLOW_THREADS
669 : }
670 :
671 0 : if(max_length) {
672 : /* Not all of the compressed data could be accommodated in a buffer of
673 : the specified size. Return the unconsumed tail in an attribute. */
674 0 : Py_DECREF(self->unconsumed_tail);
675 0 : self->unconsumed_tail = PyBytes_FromStringAndSize((char *)self->zst.next_in,
676 0 : self->zst.avail_in);
677 : }
678 0 : else if (PyBytes_GET_SIZE(self->unconsumed_tail) > 0) {
679 : /* All of the compressed data was consumed. Clear unconsumed_tail. */
680 0 : Py_DECREF(self->unconsumed_tail);
681 0 : self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
682 : }
683 0 : if (self->unconsumed_tail == NULL) {
684 0 : Py_DECREF(RetVal);
685 0 : RetVal = NULL;
686 0 : goto error;
687 : }
688 :
689 : /* The end of the compressed data has been reached, so set the
690 : unused_data attribute to a string containing the remainder of the
691 : data in the string. Note that this is also a logical place to call
692 : inflateEnd, but the old behaviour of only calling it on flush() is
693 : preserved.
694 : */
695 0 : if (err == Z_STREAM_END) {
696 0 : Py_XDECREF(self->unused_data); /* Free original empty string */
697 0 : self->unused_data = PyBytes_FromStringAndSize(
698 0 : (char *)self->zst.next_in, self->zst.avail_in);
699 0 : if (self->unused_data == NULL) {
700 0 : Py_DECREF(RetVal);
701 0 : goto error;
702 : }
703 0 : self->eof = 1;
704 : /* We will only get Z_BUF_ERROR if the output buffer was full
705 : but there wasn't more output when we tried again, so it is
706 : not an error condition.
707 : */
708 0 : } else if (err != Z_OK && err != Z_BUF_ERROR) {
709 0 : zlib_error(self->zst, err, "while decompressing data");
710 0 : Py_DECREF(RetVal);
711 0 : RetVal = NULL;
712 0 : goto error;
713 : }
714 :
715 0 : if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
716 0 : Py_DECREF(RetVal);
717 0 : RetVal = NULL;
718 : }
719 :
720 : error:
721 0 : LEAVE_ZLIB(self);
722 : error_outer:
723 0 : PyBuffer_Release(&pinput);
724 0 : return RetVal;
725 : }
726 :
727 : PyDoc_STRVAR(comp_flush__doc__,
728 : "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
729 : "\n"
730 : "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
731 : "default value used when mode is not specified is Z_FINISH.\n"
732 : "If mode == Z_FINISH, the compressor object can no longer be used after\n"
733 : "calling the flush() method. Otherwise, more data can still be compressed.");
734 :
735 : static PyObject *
736 0 : PyZlib_flush(compobject *self, PyObject *args)
737 : {
738 0 : int err, length = DEFAULTALLOC;
739 : PyObject *RetVal;
740 0 : int flushmode = Z_FINISH;
741 : unsigned long start_total_out;
742 :
743 0 : if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
744 0 : return NULL;
745 :
746 : /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
747 : doing any work at all; just return an empty string. */
748 0 : if (flushmode == Z_NO_FLUSH) {
749 0 : return PyBytes_FromStringAndSize(NULL, 0);
750 : }
751 :
752 0 : if (!(RetVal = PyBytes_FromStringAndSize(NULL, length)))
753 0 : return NULL;
754 :
755 0 : ENTER_ZLIB(self);
756 :
757 0 : start_total_out = self->zst.total_out;
758 0 : self->zst.avail_in = 0;
759 0 : self->zst.avail_out = length;
760 0 : self->zst.next_out = (unsigned char *)PyBytes_AS_STRING(RetVal);
761 :
762 0 : Py_BEGIN_ALLOW_THREADS
763 0 : err = deflate(&(self->zst), flushmode);
764 0 : Py_END_ALLOW_THREADS
765 :
766 : /* while Z_OK and the output buffer is full, there might be more output,
767 : so extend the output buffer and try again */
768 0 : while (err == Z_OK && self->zst.avail_out == 0) {
769 0 : if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
770 0 : Py_DECREF(RetVal);
771 0 : RetVal = NULL;
772 0 : goto error;
773 : }
774 0 : self->zst.next_out =
775 0 : (unsigned char *)PyBytes_AS_STRING(RetVal) + length;
776 0 : self->zst.avail_out = length;
777 0 : length = length << 1;
778 :
779 0 : Py_BEGIN_ALLOW_THREADS
780 0 : err = deflate(&(self->zst), flushmode);
781 0 : Py_END_ALLOW_THREADS
782 : }
783 :
784 : /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
785 : various data structures. Note we should only get Z_STREAM_END when
786 : flushmode is Z_FINISH, but checking both for safety*/
787 0 : if (err == Z_STREAM_END && flushmode == Z_FINISH) {
788 0 : err = deflateEnd(&(self->zst));
789 0 : if (err != Z_OK) {
790 0 : zlib_error(self->zst, err, "while finishing compression");
791 0 : Py_DECREF(RetVal);
792 0 : RetVal = NULL;
793 0 : goto error;
794 : }
795 : else
796 0 : self->is_initialised = 0;
797 :
798 : /* We will only get Z_BUF_ERROR if the output buffer was full
799 : but there wasn't more output when we tried again, so it is
800 : not an error condition.
801 : */
802 0 : } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
803 0 : zlib_error(self->zst, err, "while flushing");
804 0 : Py_DECREF(RetVal);
805 0 : RetVal = NULL;
806 0 : goto error;
807 : }
808 :
809 0 : if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
810 0 : Py_DECREF(RetVal);
811 0 : RetVal = NULL;
812 : }
813 :
814 : error:
815 0 : LEAVE_ZLIB(self);
816 :
817 0 : return RetVal;
818 : }
819 :
820 : #ifdef HAVE_ZLIB_COPY
821 : PyDoc_STRVAR(comp_copy__doc__,
822 : "copy() -- Return a copy of the compression object.");
823 :
824 : static PyObject *
825 0 : PyZlib_copy(compobject *self)
826 : {
827 0 : compobject *retval = NULL;
828 : int err;
829 :
830 0 : retval = newcompobject(&Comptype);
831 0 : if (!retval) return NULL;
832 :
833 : /* Copy the zstream state
834 : * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
835 : */
836 0 : ENTER_ZLIB(self);
837 0 : err = deflateCopy(&retval->zst, &self->zst);
838 0 : switch(err) {
839 : case(Z_OK):
840 0 : break;
841 : case(Z_STREAM_ERROR):
842 0 : PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
843 0 : goto error;
844 : case(Z_MEM_ERROR):
845 0 : PyErr_SetString(PyExc_MemoryError,
846 : "Can't allocate memory for compression object");
847 0 : goto error;
848 : default:
849 0 : zlib_error(self->zst, err, "while copying compression object");
850 0 : goto error;
851 : }
852 0 : Py_INCREF(self->unused_data);
853 0 : Py_INCREF(self->unconsumed_tail);
854 0 : Py_XINCREF(self->zdict);
855 0 : Py_XDECREF(retval->unused_data);
856 0 : Py_XDECREF(retval->unconsumed_tail);
857 0 : Py_XDECREF(retval->zdict);
858 0 : retval->unused_data = self->unused_data;
859 0 : retval->unconsumed_tail = self->unconsumed_tail;
860 0 : retval->zdict = self->zdict;
861 0 : retval->eof = self->eof;
862 :
863 : /* Mark it as being initialized */
864 0 : retval->is_initialised = 1;
865 :
866 0 : LEAVE_ZLIB(self);
867 0 : return (PyObject *)retval;
868 :
869 : error:
870 0 : LEAVE_ZLIB(self);
871 0 : Py_XDECREF(retval);
872 0 : return NULL;
873 : }
874 :
875 : PyDoc_STRVAR(decomp_copy__doc__,
876 : "copy() -- Return a copy of the decompression object.");
877 :
878 : static PyObject *
879 0 : PyZlib_uncopy(compobject *self)
880 : {
881 0 : compobject *retval = NULL;
882 : int err;
883 :
884 0 : retval = newcompobject(&Decomptype);
885 0 : if (!retval) return NULL;
886 :
887 : /* Copy the zstream state
888 : * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
889 : */
890 0 : ENTER_ZLIB(self);
891 0 : err = inflateCopy(&retval->zst, &self->zst);
892 0 : switch(err) {
893 : case(Z_OK):
894 0 : break;
895 : case(Z_STREAM_ERROR):
896 0 : PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
897 0 : goto error;
898 : case(Z_MEM_ERROR):
899 0 : PyErr_SetString(PyExc_MemoryError,
900 : "Can't allocate memory for decompression object");
901 0 : goto error;
902 : default:
903 0 : zlib_error(self->zst, err, "while copying decompression object");
904 0 : goto error;
905 : }
906 :
907 0 : Py_INCREF(self->unused_data);
908 0 : Py_INCREF(self->unconsumed_tail);
909 0 : Py_XINCREF(self->zdict);
910 0 : Py_XDECREF(retval->unused_data);
911 0 : Py_XDECREF(retval->unconsumed_tail);
912 0 : Py_XDECREF(retval->zdict);
913 0 : retval->unused_data = self->unused_data;
914 0 : retval->unconsumed_tail = self->unconsumed_tail;
915 0 : retval->zdict = self->zdict;
916 0 : retval->eof = self->eof;
917 :
918 : /* Mark it as being initialized */
919 0 : retval->is_initialised = 1;
920 :
921 0 : LEAVE_ZLIB(self);
922 0 : return (PyObject *)retval;
923 :
924 : error:
925 0 : LEAVE_ZLIB(self);
926 0 : Py_XDECREF(retval);
927 0 : return NULL;
928 : }
929 : #endif
930 :
931 : PyDoc_STRVAR(decomp_flush__doc__,
932 : "flush( [length] ) -- Return a string containing any remaining\n"
933 : "decompressed data. length, if given, is the initial size of the\n"
934 : "output buffer.\n"
935 : "\n"
936 : "The decompressor object can no longer be used after this call.");
937 :
938 : static PyObject *
939 0 : PyZlib_unflush(compobject *self, PyObject *args)
940 : {
941 0 : int err, length = DEFAULTALLOC;
942 0 : PyObject * retval = NULL;
943 : unsigned long start_total_out;
944 :
945 0 : if (!PyArg_ParseTuple(args, "|i:flush", &length))
946 0 : return NULL;
947 0 : if (length <= 0) {
948 0 : PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
949 0 : return NULL;
950 : }
951 0 : if (!(retval = PyBytes_FromStringAndSize(NULL, length)))
952 0 : return NULL;
953 :
954 :
955 0 : ENTER_ZLIB(self);
956 :
957 0 : start_total_out = self->zst.total_out;
958 0 : self->zst.avail_out = length;
959 0 : self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval);
960 :
961 0 : Py_BEGIN_ALLOW_THREADS
962 0 : err = inflate(&(self->zst), Z_FINISH);
963 0 : Py_END_ALLOW_THREADS
964 :
965 : /* while Z_OK and the output buffer is full, there might be more output,
966 : so extend the output buffer and try again */
967 0 : while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
968 0 : if (_PyBytes_Resize(&retval, length << 1) < 0) {
969 0 : Py_DECREF(retval);
970 0 : retval = NULL;
971 0 : goto error;
972 : }
973 0 : self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
974 0 : self->zst.avail_out = length;
975 0 : length = length << 1;
976 :
977 0 : Py_BEGIN_ALLOW_THREADS
978 0 : err = inflate(&(self->zst), Z_FINISH);
979 0 : Py_END_ALLOW_THREADS
980 : }
981 :
982 : /* If at end of stream, clean up any memory allocated by zlib. */
983 0 : if (err == Z_STREAM_END) {
984 0 : self->eof = 1;
985 0 : self->is_initialised = 0;
986 0 : err = inflateEnd(&(self->zst));
987 0 : if (err != Z_OK) {
988 0 : zlib_error(self->zst, err, "while finishing decompression");
989 0 : Py_DECREF(retval);
990 0 : retval = NULL;
991 0 : goto error;
992 : }
993 : }
994 0 : if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
995 0 : Py_DECREF(retval);
996 0 : retval = NULL;
997 : }
998 :
999 : error:
1000 :
1001 0 : LEAVE_ZLIB(self);
1002 :
1003 0 : return retval;
1004 : }
1005 :
1006 : static PyMethodDef comp_methods[] =
1007 : {
1008 : {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
1009 : comp_compress__doc__},
1010 : {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
1011 : comp_flush__doc__},
1012 : #ifdef HAVE_ZLIB_COPY
1013 : {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,
1014 : comp_copy__doc__},
1015 : #endif
1016 : {NULL, NULL}
1017 : };
1018 :
1019 : static PyMethodDef Decomp_methods[] =
1020 : {
1021 : {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
1022 : decomp_decompress__doc__},
1023 : {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
1024 : decomp_flush__doc__},
1025 : #ifdef HAVE_ZLIB_COPY
1026 : {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,
1027 : decomp_copy__doc__},
1028 : #endif
1029 : {NULL, NULL}
1030 : };
1031 :
1032 : #define COMP_OFF(x) offsetof(compobject, x)
1033 : static PyMemberDef Decomp_members[] = {
1034 : {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1035 : {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
1036 : {"eof", T_BOOL, COMP_OFF(eof), READONLY},
1037 : {NULL},
1038 : };
1039 :
1040 : PyDoc_STRVAR(adler32__doc__,
1041 : "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
1042 : "\n"
1043 : "An optional starting value can be specified. The returned checksum is\n"
1044 : "an integer.");
1045 :
1046 : static PyObject *
1047 0 : PyZlib_adler32(PyObject *self, PyObject *args)
1048 : {
1049 0 : unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */
1050 : Py_buffer pbuf;
1051 :
1052 0 : if (!PyArg_ParseTuple(args, "y*|I:adler32", &pbuf, &adler32val))
1053 0 : return NULL;
1054 : /* Releasing the GIL for very small buffers is inefficient
1055 : and may lower performance */
1056 0 : if (pbuf.len > 1024*5) {
1057 0 : unsigned char *buf = pbuf.buf;
1058 0 : Py_ssize_t len = pbuf.len;
1059 :
1060 0 : Py_BEGIN_ALLOW_THREADS
1061 : /* Avoid truncation of length for very large buffers. adler32() takes
1062 : length as an unsigned int, which may be narrower than Py_ssize_t. */
1063 0 : while (len > (size_t) UINT_MAX) {
1064 : adler32val = adler32(adler32val, buf, UINT_MAX);
1065 : buf += (size_t) UINT_MAX;
1066 : len -= (size_t) UINT_MAX;
1067 : }
1068 0 : adler32val = adler32(adler32val, buf, len);
1069 0 : Py_END_ALLOW_THREADS
1070 : } else {
1071 0 : adler32val = adler32(adler32val, pbuf.buf, pbuf.len);
1072 : }
1073 0 : PyBuffer_Release(&pbuf);
1074 0 : return PyLong_FromUnsignedLong(adler32val & 0xffffffffU);
1075 : }
1076 :
1077 : PyDoc_STRVAR(crc32__doc__,
1078 : "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
1079 : "\n"
1080 : "An optional starting value can be specified. The returned checksum is\n"
1081 : "an integer.");
1082 :
1083 : static PyObject *
1084 0 : PyZlib_crc32(PyObject *self, PyObject *args)
1085 : {
1086 0 : unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */
1087 : Py_buffer pbuf;
1088 : int signed_val;
1089 :
1090 0 : if (!PyArg_ParseTuple(args, "y*|I:crc32", &pbuf, &crc32val))
1091 0 : return NULL;
1092 : /* Releasing the GIL for very small buffers is inefficient
1093 : and may lower performance */
1094 0 : if (pbuf.len > 1024*5) {
1095 0 : unsigned char *buf = pbuf.buf;
1096 0 : Py_ssize_t len = pbuf.len;
1097 :
1098 0 : Py_BEGIN_ALLOW_THREADS
1099 : /* Avoid truncation of length for very large buffers. crc32() takes
1100 : length as an unsigned int, which may be narrower than Py_ssize_t. */
1101 0 : while (len > (size_t) UINT_MAX) {
1102 : crc32val = crc32(crc32val, buf, UINT_MAX);
1103 : buf += (size_t) UINT_MAX;
1104 : len -= (size_t) UINT_MAX;
1105 : }
1106 0 : signed_val = crc32(crc32val, buf, len);
1107 0 : Py_END_ALLOW_THREADS
1108 : } else {
1109 0 : signed_val = crc32(crc32val, pbuf.buf, pbuf.len);
1110 : }
1111 0 : PyBuffer_Release(&pbuf);
1112 0 : return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
1113 : }
1114 :
1115 :
1116 : static PyMethodDef zlib_methods[] =
1117 : {
1118 : {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
1119 : adler32__doc__},
1120 : {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
1121 : compress__doc__},
1122 : {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
1123 : compressobj__doc__},
1124 : {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
1125 : crc32__doc__},
1126 : {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
1127 : decompress__doc__},
1128 : {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS|METH_KEYWORDS,
1129 : decompressobj__doc__},
1130 : {NULL, NULL}
1131 : };
1132 :
1133 : static PyTypeObject Comptype = {
1134 : PyVarObject_HEAD_INIT(0, 0)
1135 : "zlib.Compress",
1136 : sizeof(compobject),
1137 : 0,
1138 : (destructor)Comp_dealloc, /*tp_dealloc*/
1139 : 0, /*tp_print*/
1140 : 0, /*tp_getattr*/
1141 : 0, /*tp_setattr*/
1142 : 0, /*tp_reserved*/
1143 : 0, /*tp_repr*/
1144 : 0, /*tp_as_number*/
1145 : 0, /*tp_as_sequence*/
1146 : 0, /*tp_as_mapping*/
1147 : 0, /*tp_hash*/
1148 : 0, /*tp_call*/
1149 : 0, /*tp_str*/
1150 : 0, /*tp_getattro*/
1151 : 0, /*tp_setattro*/
1152 : 0, /*tp_as_buffer*/
1153 : Py_TPFLAGS_DEFAULT, /*tp_flags*/
1154 : 0, /*tp_doc*/
1155 : 0, /*tp_traverse*/
1156 : 0, /*tp_clear*/
1157 : 0, /*tp_richcompare*/
1158 : 0, /*tp_weaklistoffset*/
1159 : 0, /*tp_iter*/
1160 : 0, /*tp_iternext*/
1161 : comp_methods, /*tp_methods*/
1162 : };
1163 :
1164 : static PyTypeObject Decomptype = {
1165 : PyVarObject_HEAD_INIT(0, 0)
1166 : "zlib.Decompress",
1167 : sizeof(compobject),
1168 : 0,
1169 : (destructor)Decomp_dealloc, /*tp_dealloc*/
1170 : 0, /*tp_print*/
1171 : 0, /*tp_getattr*/
1172 : 0, /*tp_setattr*/
1173 : 0, /*tp_reserved*/
1174 : 0, /*tp_repr*/
1175 : 0, /*tp_as_number*/
1176 : 0, /*tp_as_sequence*/
1177 : 0, /*tp_as_mapping*/
1178 : 0, /*tp_hash*/
1179 : 0, /*tp_call*/
1180 : 0, /*tp_str*/
1181 : 0, /*tp_getattro*/
1182 : 0, /*tp_setattro*/
1183 : 0, /*tp_as_buffer*/
1184 : Py_TPFLAGS_DEFAULT, /*tp_flags*/
1185 : 0, /*tp_doc*/
1186 : 0, /*tp_traverse*/
1187 : 0, /*tp_clear*/
1188 : 0, /*tp_richcompare*/
1189 : 0, /*tp_weaklistoffset*/
1190 : 0, /*tp_iter*/
1191 : 0, /*tp_iternext*/
1192 : Decomp_methods, /*tp_methods*/
1193 : Decomp_members, /*tp_members*/
1194 : };
1195 :
1196 : PyDoc_STRVAR(zlib_module_documentation,
1197 : "The functions in this module allow compression and decompression using the\n"
1198 : "zlib library, which is based on GNU zip.\n"
1199 : "\n"
1200 : "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1201 : "compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
1202 : "compressobj([level[, ...]]) -- Return a compressor object.\n"
1203 : "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1204 : "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1205 : "decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
1206 : "\n"
1207 : "'wbits' is window buffer size.\n"
1208 : "Compressor objects support compress() and flush() methods; decompressor\n"
1209 : "objects support decompress() and flush().");
1210 :
1211 : static struct PyModuleDef zlibmodule = {
1212 : PyModuleDef_HEAD_INIT,
1213 : "zlib",
1214 : zlib_module_documentation,
1215 : -1,
1216 : zlib_methods,
1217 : NULL,
1218 : NULL,
1219 : NULL,
1220 : NULL
1221 : };
1222 :
1223 : PyMODINIT_FUNC
1224 0 : PyInit_zlib(void)
1225 : {
1226 : PyObject *m, *ver;
1227 0 : if (PyType_Ready(&Comptype) < 0)
1228 0 : return NULL;
1229 0 : if (PyType_Ready(&Decomptype) < 0)
1230 0 : return NULL;
1231 0 : m = PyModule_Create(&zlibmodule);
1232 0 : if (m == NULL)
1233 0 : return NULL;
1234 :
1235 0 : ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1236 0 : if (ZlibError != NULL) {
1237 0 : Py_INCREF(ZlibError);
1238 0 : PyModule_AddObject(m, "error", ZlibError);
1239 : }
1240 0 : PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1241 0 : PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1242 0 : PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1243 0 : PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1244 0 : PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1245 0 : PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1246 0 : PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1247 0 : PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1248 0 : PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
1249 :
1250 0 : PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1251 0 : PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1252 0 : PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1253 0 : PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
1254 :
1255 0 : ver = PyUnicode_FromString(ZLIB_VERSION);
1256 0 : if (ver != NULL)
1257 0 : PyModule_AddObject(m, "ZLIB_VERSION", ver);
1258 :
1259 0 : ver = PyUnicode_FromString(zlibVersion());
1260 0 : if (ver != NULL)
1261 0 : PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1262 :
1263 0 : PyModule_AddStringConstant(m, "__version__", "1.0");
1264 :
1265 0 : return m;
1266 : }
|