1 /* zlibmodule.c -- gzip-compatible data compression */
2 /* See http://zlib.net/ */
3
4 /* Windows users: read Python's PCbuild\readme.txt */
5
6 #define PY_SSIZE_T_CLEAN
7
8 #include "Python.h"
9 #include "structmember.h"
10 #include "zlib.h"
11
12
13 #include "pythread.h"
14 #define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
19
20 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
21 # define AT_LEAST_ZLIB_1_2_2_1
22 #endif
23
24 /* The following parameters are copied from zutil.h, version 0.95 */
25 #define DEFLATED 8
26 #if MAX_MEM_LEVEL >= 8
27 # define DEF_MEM_LEVEL 8
28 #else
29 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
30 #endif
31
32 /* Initial buffer size. */
33 #define DEF_BUF_SIZE (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 PyThread_type_lock lock;
50 } compobject;
51
52 static void
zlib_error(z_stream zst,int err,const char * msg)53 zlib_error(z_stream zst, int err, const char *msg)
54 {
55 const char *zmsg = Z_NULL;
56 /* In case of a version mismatch, zst.msg won't be initialized.
57 Check for this case first, before looking at zst.msg. */
58 if (err == Z_VERSION_ERROR)
59 zmsg = "library version mismatch";
60 if (zmsg == Z_NULL)
61 zmsg = zst.msg;
62 if (zmsg == Z_NULL) {
63 switch (err) {
64 case Z_BUF_ERROR:
65 zmsg = "incomplete or truncated stream";
66 break;
67 case Z_STREAM_ERROR:
68 zmsg = "inconsistent stream state";
69 break;
70 case Z_DATA_ERROR:
71 zmsg = "invalid input data";
72 break;
73 }
74 }
75 if (zmsg == Z_NULL)
76 PyErr_Format(ZlibError, "Error %d %s", err, msg);
77 else
78 PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
79 }
80
81 /*[clinic input]
82 module zlib
83 class zlib.Compress "compobject *" "&Comptype"
84 class zlib.Decompress "compobject *" "&Decomptype"
85 [clinic start generated code]*/
86 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
87
88 static compobject *
newcompobject(PyTypeObject * type)89 newcompobject(PyTypeObject *type)
90 {
91 compobject *self;
92 self = PyObject_New(compobject, type);
93 if (self == NULL)
94 return NULL;
95 self->eof = 0;
96 self->is_initialised = 0;
97 self->zdict = NULL;
98 self->unused_data = PyBytes_FromStringAndSize("", 0);
99 if (self->unused_data == NULL) {
100 Py_DECREF(self);
101 return NULL;
102 }
103 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
104 if (self->unconsumed_tail == NULL) {
105 Py_DECREF(self);
106 return NULL;
107 }
108 self->lock = PyThread_allocate_lock();
109 if (self->lock == NULL) {
110 Py_DECREF(self);
111 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
112 return NULL;
113 }
114 return self;
115 }
116
117 static void*
PyZlib_Malloc(voidpf ctx,uInt items,uInt size)118 PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
119 {
120 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
121 return NULL;
122 /* PyMem_Malloc() cannot be used: the GIL is not held when
123 inflate() and deflate() are called */
124 return PyMem_RawMalloc((size_t)items * (size_t)size);
125 }
126
127 static void
PyZlib_Free(voidpf ctx,void * ptr)128 PyZlib_Free(voidpf ctx, void *ptr)
129 {
130 PyMem_RawFree(ptr);
131 }
132
133 static void
arrange_input_buffer(z_stream * zst,Py_ssize_t * remains)134 arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
135 {
136 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
137 *remains -= zst->avail_in;
138 }
139
140 static Py_ssize_t
arrange_output_buffer_with_maximum(z_stream * zst,PyObject ** buffer,Py_ssize_t length,Py_ssize_t max_length)141 arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
142 Py_ssize_t length,
143 Py_ssize_t max_length)
144 {
145 Py_ssize_t occupied;
146
147 if (*buffer == NULL) {
148 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
149 return -1;
150 occupied = 0;
151 }
152 else {
153 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
154
155 if (length == occupied) {
156 Py_ssize_t new_length;
157 assert(length <= max_length);
158 /* can not scale the buffer over max_length */
159 if (length == max_length)
160 return -2;
161 if (length <= (max_length >> 1))
162 new_length = length << 1;
163 else
164 new_length = max_length;
165 if (_PyBytes_Resize(buffer, new_length) < 0)
166 return -1;
167 length = new_length;
168 }
169 }
170
171 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
172 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
173
174 return length;
175 }
176
177 static Py_ssize_t
arrange_output_buffer(z_stream * zst,PyObject ** buffer,Py_ssize_t length)178 arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
179 {
180 Py_ssize_t ret;
181
182 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
183 PY_SSIZE_T_MAX);
184 if (ret == -2)
185 PyErr_NoMemory();
186
187 return ret;
188 }
189
190 /*[clinic input]
191 zlib.compress
192
193 data: Py_buffer
194 Binary data to be compressed.
195 /
196 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
197 Compression level, in 0-9 or -1.
198
199 Returns a bytes object containing compressed data.
200 [clinic start generated code]*/
201
202 static PyObject *
zlib_compress_impl(PyObject * module,Py_buffer * data,int level)203 zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
204 /*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
205 {
206 PyObject *RetVal = NULL;
207 Byte *ibuf;
208 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
209 int err, flush;
210 z_stream zst;
211
212 ibuf = data->buf;
213 ibuflen = data->len;
214
215 zst.opaque = NULL;
216 zst.zalloc = PyZlib_Malloc;
217 zst.zfree = PyZlib_Free;
218 zst.next_in = ibuf;
219 err = deflateInit(&zst, level);
220
221 switch (err) {
222 case Z_OK:
223 break;
224 case Z_MEM_ERROR:
225 PyErr_SetString(PyExc_MemoryError,
226 "Out of memory while compressing data");
227 goto error;
228 case Z_STREAM_ERROR:
229 PyErr_SetString(ZlibError, "Bad compression level");
230 goto error;
231 default:
232 deflateEnd(&zst);
233 zlib_error(zst, err, "while compressing data");
234 goto error;
235 }
236
237 do {
238 arrange_input_buffer(&zst, &ibuflen);
239 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
240
241 do {
242 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
243 if (obuflen < 0) {
244 deflateEnd(&zst);
245 goto error;
246 }
247
248 Py_BEGIN_ALLOW_THREADS
249 err = deflate(&zst, flush);
250 Py_END_ALLOW_THREADS
251
252 if (err == Z_STREAM_ERROR) {
253 deflateEnd(&zst);
254 zlib_error(zst, err, "while compressing data");
255 goto error;
256 }
257
258 } while (zst.avail_out == 0);
259 assert(zst.avail_in == 0);
260
261 } while (flush != Z_FINISH);
262 assert(err == Z_STREAM_END);
263
264 err = deflateEnd(&zst);
265 if (err == Z_OK) {
266 if (_PyBytes_Resize(&RetVal, zst.next_out -
267 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
268 goto error;
269 return RetVal;
270 }
271 else
272 zlib_error(zst, err, "while finishing compression");
273 error:
274 Py_XDECREF(RetVal);
275 return NULL;
276 }
277
278 /*[python input]
279
280 class ssize_t_converter(CConverter):
281 type = 'Py_ssize_t'
282 converter = 'ssize_t_converter'
283 c_ignored_default = "0"
284
285 [python start generated code]*/
286 /*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
287
288 static int
ssize_t_converter(PyObject * obj,void * ptr)289 ssize_t_converter(PyObject *obj, void *ptr)
290 {
291 PyObject *long_obj;
292 Py_ssize_t val;
293
294 /* XXX Should be replaced with PyNumber_AsSsize_t after the end of the
295 deprecation period. */
296 long_obj = _PyLong_FromNbIndexOrNbInt(obj);
297 if (long_obj == NULL) {
298 return 0;
299 }
300 val = PyLong_AsSsize_t(long_obj);
301 Py_DECREF(long_obj);
302 if (val == -1 && PyErr_Occurred()) {
303 return 0;
304 }
305 *(Py_ssize_t *)ptr = val;
306 return 1;
307 }
308
309 /*[clinic input]
310 zlib.decompress
311
312 data: Py_buffer
313 Compressed data.
314 /
315 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
316 The window buffer size and container format.
317 bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
318 The initial output buffer size.
319
320 Returns a bytes object containing the uncompressed data.
321 [clinic start generated code]*/
322
323 static PyObject *
zlib_decompress_impl(PyObject * module,Py_buffer * data,int wbits,Py_ssize_t bufsize)324 zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
325 Py_ssize_t bufsize)
326 /*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
327 {
328 PyObject *RetVal = NULL;
329 Byte *ibuf;
330 Py_ssize_t ibuflen;
331 int err, flush;
332 z_stream zst;
333
334 if (bufsize < 0) {
335 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
336 return NULL;
337 } else if (bufsize == 0) {
338 bufsize = 1;
339 }
340
341 ibuf = data->buf;
342 ibuflen = data->len;
343
344 zst.opaque = NULL;
345 zst.zalloc = PyZlib_Malloc;
346 zst.zfree = PyZlib_Free;
347 zst.avail_in = 0;
348 zst.next_in = ibuf;
349 err = inflateInit2(&zst, wbits);
350
351 switch (err) {
352 case Z_OK:
353 break;
354 case Z_MEM_ERROR:
355 PyErr_SetString(PyExc_MemoryError,
356 "Out of memory while decompressing data");
357 goto error;
358 default:
359 inflateEnd(&zst);
360 zlib_error(zst, err, "while preparing to decompress data");
361 goto error;
362 }
363
364 do {
365 arrange_input_buffer(&zst, &ibuflen);
366 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
367
368 do {
369 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
370 if (bufsize < 0) {
371 inflateEnd(&zst);
372 goto error;
373 }
374
375 Py_BEGIN_ALLOW_THREADS
376 err = inflate(&zst, flush);
377 Py_END_ALLOW_THREADS
378
379 switch (err) {
380 case Z_OK: /* fall through */
381 case Z_BUF_ERROR: /* fall through */
382 case Z_STREAM_END:
383 break;
384 case Z_MEM_ERROR:
385 inflateEnd(&zst);
386 PyErr_SetString(PyExc_MemoryError,
387 "Out of memory while decompressing data");
388 goto error;
389 default:
390 inflateEnd(&zst);
391 zlib_error(zst, err, "while decompressing data");
392 goto error;
393 }
394
395 } while (zst.avail_out == 0);
396
397 } while (err != Z_STREAM_END && ibuflen != 0);
398
399
400 if (err != Z_STREAM_END) {
401 inflateEnd(&zst);
402 zlib_error(zst, err, "while decompressing data");
403 goto error;
404 }
405
406 err = inflateEnd(&zst);
407 if (err != Z_OK) {
408 zlib_error(zst, err, "while finishing decompression");
409 goto error;
410 }
411
412 if (_PyBytes_Resize(&RetVal, zst.next_out -
413 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
414 goto error;
415
416 return RetVal;
417
418 error:
419 Py_XDECREF(RetVal);
420 return NULL;
421 }
422
423 /*[clinic input]
424 zlib.compressobj
425
426 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
427 The compression level (an integer in the range 0-9 or -1; default is
428 currently equivalent to 6). Higher compression levels are slower,
429 but produce smaller results.
430 method: int(c_default="DEFLATED") = DEFLATED
431 The compression algorithm. If given, this must be DEFLATED.
432 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
433 +9 to +15: The base-two logarithm of the window size. Include a zlib
434 container.
435 -9 to -15: Generate a raw stream.
436 +25 to +31: Include a gzip container.
437 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
438 Controls the amount of memory used for internal compression state.
439 Valid values range from 1 to 9. Higher values result in higher memory
440 usage, faster compression, and smaller output.
441 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
442 Used to tune the compression algorithm. Possible values are
443 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
444 zdict: Py_buffer = None
445 The predefined compression dictionary - a sequence of bytes
446 containing subsequences that are likely to occur in the input data.
447
448 Return a compressor object.
449 [clinic start generated code]*/
450
451 static PyObject *
zlib_compressobj_impl(PyObject * module,int level,int method,int wbits,int memLevel,int strategy,Py_buffer * zdict)452 zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
453 int memLevel, int strategy, Py_buffer *zdict)
454 /*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
455 {
456 compobject *self = NULL;
457 int err;
458
459 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
460 PyErr_SetString(PyExc_OverflowError,
461 "zdict length does not fit in an unsigned int");
462 goto error;
463 }
464
465 self = newcompobject(&Comptype);
466 if (self == NULL)
467 goto error;
468 self->zst.opaque = NULL;
469 self->zst.zalloc = PyZlib_Malloc;
470 self->zst.zfree = PyZlib_Free;
471 self->zst.next_in = NULL;
472 self->zst.avail_in = 0;
473 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
474 switch (err) {
475 case Z_OK:
476 self->is_initialised = 1;
477 if (zdict->buf == NULL) {
478 goto success;
479 } else {
480 err = deflateSetDictionary(&self->zst,
481 zdict->buf, (unsigned int)zdict->len);
482 switch (err) {
483 case Z_OK:
484 goto success;
485 case Z_STREAM_ERROR:
486 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
487 goto error;
488 default:
489 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
490 goto error;
491 }
492 }
493 case Z_MEM_ERROR:
494 PyErr_SetString(PyExc_MemoryError,
495 "Can't allocate memory for compression object");
496 goto error;
497 case Z_STREAM_ERROR:
498 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
499 goto error;
500 default:
501 zlib_error(self->zst, err, "while creating compression object");
502 goto error;
503 }
504
505 error:
506 Py_CLEAR(self);
507 success:
508 return (PyObject *)self;
509 }
510
511 static int
set_inflate_zdict(compobject * self)512 set_inflate_zdict(compobject *self)
513 {
514 Py_buffer zdict_buf;
515 int err;
516
517 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
518 return -1;
519 }
520 if ((size_t)zdict_buf.len > UINT_MAX) {
521 PyErr_SetString(PyExc_OverflowError,
522 "zdict length does not fit in an unsigned int");
523 PyBuffer_Release(&zdict_buf);
524 return -1;
525 }
526 err = inflateSetDictionary(&self->zst,
527 zdict_buf.buf, (unsigned int)zdict_buf.len);
528 PyBuffer_Release(&zdict_buf);
529 if (err != Z_OK) {
530 zlib_error(self->zst, err, "while setting zdict");
531 return -1;
532 }
533 return 0;
534 }
535
536 /*[clinic input]
537 zlib.decompressobj
538
539 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
540 The window buffer size and container format.
541 zdict: object(c_default="NULL") = b''
542 The predefined compression dictionary. This must be the same
543 dictionary as used by the compressor that produced the input data.
544
545 Return a decompressor object.
546 [clinic start generated code]*/
547
548 static PyObject *
zlib_decompressobj_impl(PyObject * module,int wbits,PyObject * zdict)549 zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
550 /*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
551 {
552 int err;
553 compobject *self;
554
555 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
556 PyErr_SetString(PyExc_TypeError,
557 "zdict argument must support the buffer protocol");
558 return NULL;
559 }
560
561 self = newcompobject(&Decomptype);
562 if (self == NULL)
563 return NULL;
564 self->zst.opaque = NULL;
565 self->zst.zalloc = PyZlib_Malloc;
566 self->zst.zfree = PyZlib_Free;
567 self->zst.next_in = NULL;
568 self->zst.avail_in = 0;
569 if (zdict != NULL) {
570 Py_INCREF(zdict);
571 self->zdict = zdict;
572 }
573 err = inflateInit2(&self->zst, wbits);
574 switch (err) {
575 case Z_OK:
576 self->is_initialised = 1;
577 if (self->zdict != NULL && wbits < 0) {
578 #ifdef AT_LEAST_ZLIB_1_2_2_1
579 if (set_inflate_zdict(self) < 0) {
580 Py_DECREF(self);
581 return NULL;
582 }
583 #else
584 PyErr_Format(ZlibError,
585 "zlib version %s does not allow raw inflate with dictionary",
586 ZLIB_VERSION);
587 Py_DECREF(self);
588 return NULL;
589 #endif
590 }
591 return (PyObject *)self;
592 case Z_STREAM_ERROR:
593 Py_DECREF(self);
594 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
595 return NULL;
596 case Z_MEM_ERROR:
597 Py_DECREF(self);
598 PyErr_SetString(PyExc_MemoryError,
599 "Can't allocate memory for decompression object");
600 return NULL;
601 default:
602 zlib_error(self->zst, err, "while creating decompression object");
603 Py_DECREF(self);
604 return NULL;
605 }
606 }
607
608 static void
Dealloc(compobject * self)609 Dealloc(compobject *self)
610 {
611 PyThread_free_lock(self->lock);
612 Py_XDECREF(self->unused_data);
613 Py_XDECREF(self->unconsumed_tail);
614 Py_XDECREF(self->zdict);
615 PyObject_Del(self);
616 }
617
618 static void
Comp_dealloc(compobject * self)619 Comp_dealloc(compobject *self)
620 {
621 if (self->is_initialised)
622 deflateEnd(&self->zst);
623 Dealloc(self);
624 }
625
626 static void
Decomp_dealloc(compobject * self)627 Decomp_dealloc(compobject *self)
628 {
629 if (self->is_initialised)
630 inflateEnd(&self->zst);
631 Dealloc(self);
632 }
633
634 /*[clinic input]
635 zlib.Compress.compress
636
637 data: Py_buffer
638 Binary data to be compressed.
639 /
640
641 Returns a bytes object containing compressed data.
642
643 After calling this function, some of the input data may still
644 be stored in internal buffers for later processing.
645 Call the flush() method to clear these buffers.
646 [clinic start generated code]*/
647
648 static PyObject *
zlib_Compress_compress_impl(compobject * self,Py_buffer * data)649 zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
650 /*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
651 {
652 PyObject *RetVal = NULL;
653 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
654 int err;
655
656 ENTER_ZLIB(self);
657
658 self->zst.next_in = data->buf;
659 ibuflen = data->len;
660
661 do {
662 arrange_input_buffer(&self->zst, &ibuflen);
663
664 do {
665 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
666 if (obuflen < 0)
667 goto error;
668
669 Py_BEGIN_ALLOW_THREADS
670 err = deflate(&self->zst, Z_NO_FLUSH);
671 Py_END_ALLOW_THREADS
672
673 if (err == Z_STREAM_ERROR) {
674 zlib_error(self->zst, err, "while compressing data");
675 goto error;
676 }
677
678 } while (self->zst.avail_out == 0);
679 assert(self->zst.avail_in == 0);
680
681 } while (ibuflen != 0);
682
683 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
684 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
685 goto success;
686
687 error:
688 Py_CLEAR(RetVal);
689 success:
690 LEAVE_ZLIB(self);
691 return RetVal;
692 }
693
694 /* Helper for objdecompress() and flush(). Saves any unconsumed input data in
695 self->unused_data or self->unconsumed_tail, as appropriate. */
696 static int
save_unconsumed_input(compobject * self,Py_buffer * data,int err)697 save_unconsumed_input(compobject *self, Py_buffer *data, int err)
698 {
699 if (err == Z_STREAM_END) {
700 /* The end of the compressed data has been reached. Store the leftover
701 input data in self->unused_data. */
702 if (self->zst.avail_in > 0) {
703 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
704 Py_ssize_t new_size, left_size;
705 PyObject *new_data;
706 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
707 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
708 PyErr_NoMemory();
709 return -1;
710 }
711 new_size = old_size + left_size;
712 new_data = PyBytes_FromStringAndSize(NULL, new_size);
713 if (new_data == NULL)
714 return -1;
715 memcpy(PyBytes_AS_STRING(new_data),
716 PyBytes_AS_STRING(self->unused_data), old_size);
717 memcpy(PyBytes_AS_STRING(new_data) + old_size,
718 self->zst.next_in, left_size);
719 Py_SETREF(self->unused_data, new_data);
720 self->zst.avail_in = 0;
721 }
722 }
723
724 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
725 /* This code handles two distinct cases:
726 1. Output limit was reached. Save leftover input in unconsumed_tail.
727 2. All input data was consumed. Clear unconsumed_tail. */
728 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
729 PyObject *new_data = PyBytes_FromStringAndSize(
730 (char *)self->zst.next_in, left_size);
731 if (new_data == NULL)
732 return -1;
733 Py_SETREF(self->unconsumed_tail, new_data);
734 }
735
736 return 0;
737 }
738
739 /*[clinic input]
740 zlib.Decompress.decompress
741
742 data: Py_buffer
743 The binary data to decompress.
744 /
745 max_length: ssize_t = 0
746 The maximum allowable length of the decompressed data.
747 Unconsumed input data will be stored in
748 the unconsumed_tail attribute.
749
750 Return a bytes object containing the decompressed version of the data.
751
752 After calling this function, some of the input data may still be stored in
753 internal buffers for later processing.
754 Call the flush() method to clear these buffers.
755 [clinic start generated code]*/
756
757 static PyObject *
zlib_Decompress_decompress_impl(compobject * self,Py_buffer * data,Py_ssize_t max_length)758 zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
759 Py_ssize_t max_length)
760 /*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
761 {
762 int err = Z_OK;
763 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
764 PyObject *RetVal = NULL;
765
766 if (max_length < 0) {
767 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
768 return NULL;
769 } else if (max_length == 0)
770 hard_limit = PY_SSIZE_T_MAX;
771 else
772 hard_limit = max_length;
773
774 ENTER_ZLIB(self);
775
776 self->zst.next_in = data->buf;
777 ibuflen = data->len;
778
779 /* limit amount of data allocated to max_length */
780 if (max_length && obuflen > max_length)
781 obuflen = max_length;
782
783 do {
784 arrange_input_buffer(&self->zst, &ibuflen);
785
786 do {
787 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
788 obuflen, hard_limit);
789 if (obuflen == -2) {
790 if (max_length > 0) {
791 goto save;
792 }
793 PyErr_NoMemory();
794 }
795 if (obuflen < 0) {
796 goto abort;
797 }
798
799 Py_BEGIN_ALLOW_THREADS
800 err = inflate(&self->zst, Z_SYNC_FLUSH);
801 Py_END_ALLOW_THREADS
802
803 switch (err) {
804 case Z_OK: /* fall through */
805 case Z_BUF_ERROR: /* fall through */
806 case Z_STREAM_END:
807 break;
808 default:
809 if (err == Z_NEED_DICT && self->zdict != NULL) {
810 if (set_inflate_zdict(self) < 0)
811 goto abort;
812 else
813 break;
814 }
815 goto save;
816 }
817
818 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
819
820 } while (err != Z_STREAM_END && ibuflen != 0);
821
822 save:
823 if (save_unconsumed_input(self, data, err) < 0)
824 goto abort;
825
826 if (err == Z_STREAM_END) {
827 /* This is the logical place to call inflateEnd, but the old behaviour
828 of only calling it on flush() is preserved. */
829 self->eof = 1;
830 } else if (err != Z_OK && err != Z_BUF_ERROR) {
831 /* We will only get Z_BUF_ERROR if the output buffer was full
832 but there wasn't more output when we tried again, so it is
833 not an error condition.
834 */
835 zlib_error(self->zst, err, "while decompressing data");
836 goto abort;
837 }
838
839 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
840 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
841 goto success;
842
843 abort:
844 Py_CLEAR(RetVal);
845 success:
846 LEAVE_ZLIB(self);
847 return RetVal;
848 }
849
850 /*[clinic input]
851 zlib.Compress.flush
852
853 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
854 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
855 If mode == Z_FINISH, the compressor object can no longer be
856 used after calling the flush() method. Otherwise, more data
857 can still be compressed.
858 /
859
860 Return a bytes object containing any remaining compressed data.
861 [clinic start generated code]*/
862
863 static PyObject *
zlib_Compress_flush_impl(compobject * self,int mode)864 zlib_Compress_flush_impl(compobject *self, int mode)
865 /*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
866 {
867 int err;
868 Py_ssize_t length = DEF_BUF_SIZE;
869 PyObject *RetVal = NULL;
870
871 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
872 doing any work at all; just return an empty string. */
873 if (mode == Z_NO_FLUSH) {
874 return PyBytes_FromStringAndSize(NULL, 0);
875 }
876
877 ENTER_ZLIB(self);
878
879 self->zst.avail_in = 0;
880
881 do {
882 length = arrange_output_buffer(&self->zst, &RetVal, length);
883 if (length < 0) {
884 Py_CLEAR(RetVal);
885 goto error;
886 }
887
888 Py_BEGIN_ALLOW_THREADS
889 err = deflate(&self->zst, mode);
890 Py_END_ALLOW_THREADS
891
892 if (err == Z_STREAM_ERROR) {
893 zlib_error(self->zst, err, "while flushing");
894 Py_CLEAR(RetVal);
895 goto error;
896 }
897 } while (self->zst.avail_out == 0);
898 assert(self->zst.avail_in == 0);
899
900 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
901 various data structures. Note we should only get Z_STREAM_END when
902 mode is Z_FINISH, but checking both for safety*/
903 if (err == Z_STREAM_END && mode == Z_FINISH) {
904 err = deflateEnd(&self->zst);
905 if (err != Z_OK) {
906 zlib_error(self->zst, err, "while finishing compression");
907 Py_CLEAR(RetVal);
908 goto error;
909 }
910 else
911 self->is_initialised = 0;
912
913 /* We will only get Z_BUF_ERROR if the output buffer was full
914 but there wasn't more output when we tried again, so it is
915 not an error condition.
916 */
917 } else if (err != Z_OK && err != Z_BUF_ERROR) {
918 zlib_error(self->zst, err, "while flushing");
919 Py_CLEAR(RetVal);
920 goto error;
921 }
922
923 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
924 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
925 Py_CLEAR(RetVal);
926
927 error:
928 LEAVE_ZLIB(self);
929 return RetVal;
930 }
931
932 #ifdef HAVE_ZLIB_COPY
933
934 /*[clinic input]
935 zlib.Compress.copy
936
937 Return a copy of the compression object.
938 [clinic start generated code]*/
939
940 static PyObject *
zlib_Compress_copy_impl(compobject * self)941 zlib_Compress_copy_impl(compobject *self)
942 /*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
943 {
944 compobject *retval = NULL;
945 int err;
946
947 retval = newcompobject(&Comptype);
948 if (!retval) return NULL;
949
950 /* Copy the zstream state
951 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
952 */
953 ENTER_ZLIB(self);
954 err = deflateCopy(&retval->zst, &self->zst);
955 switch (err) {
956 case Z_OK:
957 break;
958 case Z_STREAM_ERROR:
959 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
960 goto error;
961 case Z_MEM_ERROR:
962 PyErr_SetString(PyExc_MemoryError,
963 "Can't allocate memory for compression object");
964 goto error;
965 default:
966 zlib_error(self->zst, err, "while copying compression object");
967 goto error;
968 }
969 Py_INCREF(self->unused_data);
970 Py_XSETREF(retval->unused_data, self->unused_data);
971 Py_INCREF(self->unconsumed_tail);
972 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
973 Py_XINCREF(self->zdict);
974 Py_XSETREF(retval->zdict, self->zdict);
975 retval->eof = self->eof;
976
977 /* Mark it as being initialized */
978 retval->is_initialised = 1;
979
980 LEAVE_ZLIB(self);
981 return (PyObject *)retval;
982
983 error:
984 LEAVE_ZLIB(self);
985 Py_XDECREF(retval);
986 return NULL;
987 }
988
989 /*[clinic input]
990 zlib.Compress.__copy__
991 [clinic start generated code]*/
992
993 static PyObject *
zlib_Compress___copy___impl(compobject * self)994 zlib_Compress___copy___impl(compobject *self)
995 /*[clinic end generated code: output=1875e6791975442e input=be97a05a788dfd83]*/
996 {
997 return zlib_Compress_copy_impl(self);
998 }
999
1000 /*[clinic input]
1001 zlib.Compress.__deepcopy__
1002
1003 memo: object
1004 /
1005
1006 [clinic start generated code]*/
1007
1008 static PyObject *
zlib_Compress___deepcopy__(compobject * self,PyObject * memo)1009 zlib_Compress___deepcopy__(compobject *self, PyObject *memo)
1010 /*[clinic end generated code: output=f47a2213282c9eb0 input=a9a8b0b40d83388e]*/
1011 {
1012 return zlib_Compress_copy_impl(self);
1013 }
1014
1015 /*[clinic input]
1016 zlib.Decompress.copy
1017
1018 Return a copy of the decompression object.
1019 [clinic start generated code]*/
1020
1021 static PyObject *
zlib_Decompress_copy_impl(compobject * self)1022 zlib_Decompress_copy_impl(compobject *self)
1023 /*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
1024 {
1025 compobject *retval = NULL;
1026 int err;
1027
1028 retval = newcompobject(&Decomptype);
1029 if (!retval) return NULL;
1030
1031 /* Copy the zstream state
1032 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1033 */
1034 ENTER_ZLIB(self);
1035 err = inflateCopy(&retval->zst, &self->zst);
1036 switch (err) {
1037 case Z_OK:
1038 break;
1039 case Z_STREAM_ERROR:
1040 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1041 goto error;
1042 case Z_MEM_ERROR:
1043 PyErr_SetString(PyExc_MemoryError,
1044 "Can't allocate memory for decompression object");
1045 goto error;
1046 default:
1047 zlib_error(self->zst, err, "while copying decompression object");
1048 goto error;
1049 }
1050
1051 Py_INCREF(self->unused_data);
1052 Py_XSETREF(retval->unused_data, self->unused_data);
1053 Py_INCREF(self->unconsumed_tail);
1054 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
1055 Py_XINCREF(self->zdict);
1056 Py_XSETREF(retval->zdict, self->zdict);
1057 retval->eof = self->eof;
1058
1059 /* Mark it as being initialized */
1060 retval->is_initialised = 1;
1061
1062 LEAVE_ZLIB(self);
1063 return (PyObject *)retval;
1064
1065 error:
1066 LEAVE_ZLIB(self);
1067 Py_XDECREF(retval);
1068 return NULL;
1069 }
1070
1071 /*[clinic input]
1072 zlib.Decompress.__copy__
1073 [clinic start generated code]*/
1074
1075 static PyObject *
zlib_Decompress___copy___impl(compobject * self)1076 zlib_Decompress___copy___impl(compobject *self)
1077 /*[clinic end generated code: output=80bae8bc43498ad4 input=efcb98b5472c13d2]*/
1078 {
1079 return zlib_Decompress_copy_impl(self);
1080 }
1081
1082 /*[clinic input]
1083 zlib.Decompress.__deepcopy__
1084
1085 memo: object
1086 /
1087
1088 [clinic start generated code]*/
1089
1090 static PyObject *
zlib_Decompress___deepcopy__(compobject * self,PyObject * memo)1091 zlib_Decompress___deepcopy__(compobject *self, PyObject *memo)
1092 /*[clinic end generated code: output=1f77286ab490124b input=6e99bd0ac4b9cd8b]*/
1093 {
1094 return zlib_Decompress_copy_impl(self);
1095 }
1096
1097 #endif
1098
1099 /*[clinic input]
1100 zlib.Decompress.flush
1101
1102 length: ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
1103 the initial size of the output buffer.
1104 /
1105
1106 Return a bytes object containing any remaining decompressed data.
1107 [clinic start generated code]*/
1108
1109 static PyObject *
zlib_Decompress_flush_impl(compobject * self,Py_ssize_t length)1110 zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1111 /*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
1112 {
1113 int err, flush;
1114 Py_buffer data;
1115 PyObject *RetVal = NULL;
1116 Py_ssize_t ibuflen;
1117
1118 if (length <= 0) {
1119 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1120 return NULL;
1121 }
1122
1123 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
1124 return NULL;
1125
1126 ENTER_ZLIB(self);
1127
1128 self->zst.next_in = data.buf;
1129 ibuflen = data.len;
1130
1131 do {
1132 arrange_input_buffer(&self->zst, &ibuflen);
1133 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
1134
1135 do {
1136 length = arrange_output_buffer(&self->zst, &RetVal, length);
1137 if (length < 0)
1138 goto abort;
1139
1140 Py_BEGIN_ALLOW_THREADS
1141 err = inflate(&self->zst, flush);
1142 Py_END_ALLOW_THREADS
1143
1144 switch (err) {
1145 case Z_OK: /* fall through */
1146 case Z_BUF_ERROR: /* fall through */
1147 case Z_STREAM_END:
1148 break;
1149 default:
1150 if (err == Z_NEED_DICT && self->zdict != NULL) {
1151 if (set_inflate_zdict(self) < 0)
1152 goto abort;
1153 else
1154 break;
1155 }
1156 goto save;
1157 }
1158
1159 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1160
1161 } while (err != Z_STREAM_END && ibuflen != 0);
1162
1163 save:
1164 if (save_unconsumed_input(self, &data, err) < 0)
1165 goto abort;
1166
1167 /* If at end of stream, clean up any memory allocated by zlib. */
1168 if (err == Z_STREAM_END) {
1169 self->eof = 1;
1170 self->is_initialised = 0;
1171 err = inflateEnd(&self->zst);
1172 if (err != Z_OK) {
1173 zlib_error(self->zst, err, "while finishing decompression");
1174 goto abort;
1175 }
1176 }
1177
1178 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1179 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1180 goto success;
1181
1182 abort:
1183 Py_CLEAR(RetVal);
1184 success:
1185 PyBuffer_Release(&data);
1186 LEAVE_ZLIB(self);
1187 return RetVal;
1188 }
1189
1190 #include "clinic/zlibmodule.c.h"
1191
1192 static PyMethodDef comp_methods[] =
1193 {
1194 ZLIB_COMPRESS_COMPRESS_METHODDEF
1195 ZLIB_COMPRESS_FLUSH_METHODDEF
1196 ZLIB_COMPRESS_COPY_METHODDEF
1197 ZLIB_COMPRESS___COPY___METHODDEF
1198 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
1199 {NULL, NULL}
1200 };
1201
1202 static PyMethodDef Decomp_methods[] =
1203 {
1204 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
1205 ZLIB_DECOMPRESS_FLUSH_METHODDEF
1206 ZLIB_DECOMPRESS_COPY_METHODDEF
1207 ZLIB_DECOMPRESS___COPY___METHODDEF
1208 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
1209 {NULL, NULL}
1210 };
1211
1212 #define COMP_OFF(x) offsetof(compobject, x)
1213 static PyMemberDef Decomp_members[] = {
1214 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1215 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
1216 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
1217 {NULL},
1218 };
1219
1220 /*[clinic input]
1221 zlib.adler32
1222
1223 data: Py_buffer
1224 value: unsigned_int(bitwise=True) = 1
1225 Starting value of the checksum.
1226 /
1227
1228 Compute an Adler-32 checksum of data.
1229
1230 The returned checksum is an integer.
1231 [clinic start generated code]*/
1232
1233 static PyObject *
zlib_adler32_impl(PyObject * module,Py_buffer * data,unsigned int value)1234 zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1235 /*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
1236 {
1237 /* Releasing the GIL for very small buffers is inefficient
1238 and may lower performance */
1239 if (data->len > 1024*5) {
1240 unsigned char *buf = data->buf;
1241 Py_ssize_t len = data->len;
1242
1243 Py_BEGIN_ALLOW_THREADS
1244 /* Avoid truncation of length for very large buffers. adler32() takes
1245 length as an unsigned int, which may be narrower than Py_ssize_t. */
1246 while ((size_t)len > UINT_MAX) {
1247 value = adler32(value, buf, UINT_MAX);
1248 buf += (size_t) UINT_MAX;
1249 len -= (size_t) UINT_MAX;
1250 }
1251 value = adler32(value, buf, (unsigned int)len);
1252 Py_END_ALLOW_THREADS
1253 } else {
1254 value = adler32(value, data->buf, (unsigned int)data->len);
1255 }
1256 return PyLong_FromUnsignedLong(value & 0xffffffffU);
1257 }
1258
1259 /*[clinic input]
1260 zlib.crc32
1261
1262 data: Py_buffer
1263 value: unsigned_int(bitwise=True) = 0
1264 Starting value of the checksum.
1265 /
1266
1267 Compute a CRC-32 checksum of data.
1268
1269 The returned checksum is an integer.
1270 [clinic start generated code]*/
1271
1272 static PyObject *
zlib_crc32_impl(PyObject * module,Py_buffer * data,unsigned int value)1273 zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1274 /*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
1275 {
1276 int signed_val;
1277
1278 /* Releasing the GIL for very small buffers is inefficient
1279 and may lower performance */
1280 if (data->len > 1024*5) {
1281 unsigned char *buf = data->buf;
1282 Py_ssize_t len = data->len;
1283
1284 Py_BEGIN_ALLOW_THREADS
1285 /* Avoid truncation of length for very large buffers. crc32() takes
1286 length as an unsigned int, which may be narrower than Py_ssize_t. */
1287 while ((size_t)len > UINT_MAX) {
1288 value = crc32(value, buf, UINT_MAX);
1289 buf += (size_t) UINT_MAX;
1290 len -= (size_t) UINT_MAX;
1291 }
1292 signed_val = crc32(value, buf, (unsigned int)len);
1293 Py_END_ALLOW_THREADS
1294 } else {
1295 signed_val = crc32(value, data->buf, (unsigned int)data->len);
1296 }
1297 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
1298 }
1299
1300
1301 static PyMethodDef zlib_methods[] =
1302 {
1303 ZLIB_ADLER32_METHODDEF
1304 ZLIB_COMPRESS_METHODDEF
1305 ZLIB_COMPRESSOBJ_METHODDEF
1306 ZLIB_CRC32_METHODDEF
1307 ZLIB_DECOMPRESS_METHODDEF
1308 ZLIB_DECOMPRESSOBJ_METHODDEF
1309 {NULL, NULL}
1310 };
1311
1312 static PyTypeObject Comptype = {
1313 PyVarObject_HEAD_INIT(0, 0)
1314 "zlib.Compress",
1315 sizeof(compobject),
1316 0,
1317 (destructor)Comp_dealloc, /*tp_dealloc*/
1318 0, /*tp_vectorcall_offset*/
1319 0, /*tp_getattr*/
1320 0, /*tp_setattr*/
1321 0, /*tp_as_async*/
1322 0, /*tp_repr*/
1323 0, /*tp_as_number*/
1324 0, /*tp_as_sequence*/
1325 0, /*tp_as_mapping*/
1326 0, /*tp_hash*/
1327 0, /*tp_call*/
1328 0, /*tp_str*/
1329 0, /*tp_getattro*/
1330 0, /*tp_setattro*/
1331 0, /*tp_as_buffer*/
1332 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1333 0, /*tp_doc*/
1334 0, /*tp_traverse*/
1335 0, /*tp_clear*/
1336 0, /*tp_richcompare*/
1337 0, /*tp_weaklistoffset*/
1338 0, /*tp_iter*/
1339 0, /*tp_iternext*/
1340 comp_methods, /*tp_methods*/
1341 };
1342
1343 static PyTypeObject Decomptype = {
1344 PyVarObject_HEAD_INIT(0, 0)
1345 "zlib.Decompress",
1346 sizeof(compobject),
1347 0,
1348 (destructor)Decomp_dealloc, /*tp_dealloc*/
1349 0, /*tp_vectorcall_offset*/
1350 0, /*tp_getattr*/
1351 0, /*tp_setattr*/
1352 0, /*tp_as_async*/
1353 0, /*tp_repr*/
1354 0, /*tp_as_number*/
1355 0, /*tp_as_sequence*/
1356 0, /*tp_as_mapping*/
1357 0, /*tp_hash*/
1358 0, /*tp_call*/
1359 0, /*tp_str*/
1360 0, /*tp_getattro*/
1361 0, /*tp_setattro*/
1362 0, /*tp_as_buffer*/
1363 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1364 0, /*tp_doc*/
1365 0, /*tp_traverse*/
1366 0, /*tp_clear*/
1367 0, /*tp_richcompare*/
1368 0, /*tp_weaklistoffset*/
1369 0, /*tp_iter*/
1370 0, /*tp_iternext*/
1371 Decomp_methods, /*tp_methods*/
1372 Decomp_members, /*tp_members*/
1373 };
1374
1375 PyDoc_STRVAR(zlib_module_documentation,
1376 "The functions in this module allow compression and decompression using the\n"
1377 "zlib library, which is based on GNU zip.\n"
1378 "\n"
1379 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1380 "compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
1381 "compressobj([level[, ...]]) -- Return a compressor object.\n"
1382 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1383 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1384 "decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
1385 "\n"
1386 "'wbits' is window buffer size and container format.\n"
1387 "Compressor objects support compress() and flush() methods; decompressor\n"
1388 "objects support decompress() and flush().");
1389
1390 static struct PyModuleDef zlibmodule = {
1391 PyModuleDef_HEAD_INIT,
1392 "zlib",
1393 zlib_module_documentation,
1394 -1,
1395 zlib_methods,
1396 NULL,
1397 NULL,
1398 NULL,
1399 NULL
1400 };
1401
1402 PyMODINIT_FUNC
PyInit_zlib(void)1403 PyInit_zlib(void)
1404 {
1405 PyObject *m, *ver;
1406 if (PyType_Ready(&Comptype) < 0)
1407 return NULL;
1408 if (PyType_Ready(&Decomptype) < 0)
1409 return NULL;
1410 m = PyModule_Create(&zlibmodule);
1411 if (m == NULL)
1412 return NULL;
1413
1414 ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1415 if (ZlibError != NULL) {
1416 Py_INCREF(ZlibError);
1417 PyModule_AddObject(m, "error", ZlibError);
1418 }
1419 PyModule_AddIntMacro(m, MAX_WBITS);
1420 PyModule_AddIntMacro(m, DEFLATED);
1421 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
1422 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
1423 // compression levels
1424 PyModule_AddIntMacro(m, Z_NO_COMPRESSION);
1425 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1426 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1427 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1428 // compression strategies
1429 PyModule_AddIntMacro(m, Z_FILTERED);
1430 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1431 #ifdef Z_RLE // 1.2.0.1
1432 PyModule_AddIntMacro(m, Z_RLE);
1433 #endif
1434 #ifdef Z_FIXED // 1.2.2.2
1435 PyModule_AddIntMacro(m, Z_FIXED);
1436 #endif
1437 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
1438 // allowed flush values
1439 PyModule_AddIntMacro(m, Z_NO_FLUSH);
1440 PyModule_AddIntMacro(m, Z_PARTIAL_FLUSH);
1441 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1442 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
1443 PyModule_AddIntMacro(m, Z_FINISH);
1444 #ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1445 PyModule_AddIntMacro(m, Z_BLOCK);
1446 #endif
1447 #ifdef Z_TREES // 1.2.3.4, only for inflate
1448 PyModule_AddIntMacro(m, Z_TREES);
1449 #endif
1450 ver = PyUnicode_FromString(ZLIB_VERSION);
1451 if (ver != NULL)
1452 PyModule_AddObject(m, "ZLIB_VERSION", ver);
1453
1454 ver = PyUnicode_FromString(zlibVersion());
1455 if (ver != NULL)
1456 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1457
1458 PyModule_AddStringConstant(m, "__version__", "1.0");
1459
1460 return m;
1461 }
1462