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