• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* zlibmodule.c -- gzip-compatible data compression */
2 /* See http://zlib.net/ */
3 
4 /* Windows users:  read Python's PCbuild\readme.txt */
5 
6 #ifndef Py_BUILD_CORE_BUILTIN
7 #  define Py_BUILD_CORE_MODULE 1
8 #endif
9 
10 #include "Python.h"
11 
12 #include "zlib.h"
13 #include "stdbool.h"
14 #include <stddef.h>               // offsetof()
15 
16 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM < 0x1221
17 #error "At least zlib version 1.2.2.1 is required"
18 #endif
19 
20 // Blocks output buffer wrappers
21 #include "pycore_blocks_output_buffer.h"
22 
23 #if OUTPUT_BUFFER_MAX_BLOCK_SIZE > UINT32_MAX
24     #error "The maximum block size accepted by zlib is UINT32_MAX."
25 #endif
26 
27 /* On success, return value >= 0
28    On failure, return -1 */
29 static inline Py_ssize_t
OutputBuffer_InitAndGrow(_BlocksOutputBuffer * buffer,Py_ssize_t max_length,Bytef ** next_out,uint32_t * avail_out)30 OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
31                          Bytef **next_out, uint32_t *avail_out)
32 {
33     Py_ssize_t allocated;
34 
35     allocated = _BlocksOutputBuffer_InitAndGrow(
36                     buffer, max_length, (void**) next_out);
37     *avail_out = (uint32_t) allocated;
38     return allocated;
39 }
40 
41 /* On success, return value >= 0
42    On failure, return -1 */
43 static inline Py_ssize_t
OutputBuffer_Grow(_BlocksOutputBuffer * buffer,Bytef ** next_out,uint32_t * avail_out)44 OutputBuffer_Grow(_BlocksOutputBuffer *buffer,
45                   Bytef **next_out, uint32_t *avail_out)
46 {
47     Py_ssize_t allocated;
48 
49     allocated = _BlocksOutputBuffer_Grow(
50                     buffer, (void**) next_out, (Py_ssize_t) *avail_out);
51     *avail_out = (uint32_t) allocated;
52     return allocated;
53 }
54 
55 static inline Py_ssize_t
OutputBuffer_GetDataSize(_BlocksOutputBuffer * buffer,uint32_t avail_out)56 OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
57 {
58     return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
59 }
60 
61 static inline PyObject *
OutputBuffer_Finish(_BlocksOutputBuffer * buffer,uint32_t avail_out)62 OutputBuffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
63 {
64     return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
65 }
66 
67 static inline void
OutputBuffer_OnError(_BlocksOutputBuffer * buffer)68 OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
69 {
70     _BlocksOutputBuffer_OnError(buffer);
71 }
72 
73 /* The max buffer size accepted by zlib is UINT32_MAX, the initial buffer size
74    `init_size` may > it in 64-bit build. These wrapper functions maintain an
75    UINT32_MAX sliding window for the first block:
76     1. OutputBuffer_WindowInitWithSize()
77     2. OutputBuffer_WindowGrow()
78     3. OutputBuffer_WindowFinish()
79     4. OutputBuffer_WindowOnError()
80 
81    ==== is the sliding window:
82     1. ====------
83            ^ next_posi, left_bytes is 6
84     2. ----====--
85                ^ next_posi, left_bytes is 2
86     3. --------==
87                  ^ next_posi, left_bytes is 0  */
88 typedef struct {
89     Py_ssize_t left_bytes;
90     Bytef *next_posi;
91 } _Uint32Window;
92 
93 /* Initialize the buffer with an initial buffer size.
94 
95    On success, return value >= 0
96    On failure, return value < 0 */
97 static inline Py_ssize_t
OutputBuffer_WindowInitWithSize(_BlocksOutputBuffer * buffer,_Uint32Window * window,Py_ssize_t init_size,Bytef ** next_out,uint32_t * avail_out)98 OutputBuffer_WindowInitWithSize(_BlocksOutputBuffer *buffer, _Uint32Window *window,
99                                 Py_ssize_t init_size,
100                                 Bytef **next_out, uint32_t *avail_out)
101 {
102     Py_ssize_t allocated = _BlocksOutputBuffer_InitWithSize(
103                                buffer, init_size, (void**) next_out);
104 
105     if (allocated >= 0) {
106         // the UINT32_MAX sliding window
107         Py_ssize_t window_size = Py_MIN((size_t)allocated, UINT32_MAX);
108         *avail_out = (uint32_t) window_size;
109 
110         window->left_bytes = allocated - window_size;
111         window->next_posi = *next_out + window_size;
112     }
113     return allocated;
114 }
115 
116 /* Grow the buffer.
117 
118    On success, return value >= 0
119    On failure, return value < 0 */
120 static inline Py_ssize_t
OutputBuffer_WindowGrow(_BlocksOutputBuffer * buffer,_Uint32Window * window,Bytef ** next_out,uint32_t * avail_out)121 OutputBuffer_WindowGrow(_BlocksOutputBuffer *buffer, _Uint32Window *window,
122                         Bytef **next_out, uint32_t *avail_out)
123 {
124     Py_ssize_t allocated;
125 
126     /* ensure no gaps in the data.
127        if inlined, this check could be optimized away.*/
128     if (*avail_out != 0) {
129         PyErr_SetString(PyExc_SystemError,
130                         "*avail_out != 0 in OutputBuffer_WindowGrow().");
131         return -1;
132     }
133 
134     // slide the UINT32_MAX sliding window
135     if (window->left_bytes > 0) {
136         Py_ssize_t window_size = Py_MIN((size_t)window->left_bytes, UINT32_MAX);
137 
138         *next_out = window->next_posi;
139         *avail_out = (uint32_t) window_size;
140 
141         window->left_bytes -= window_size;
142         window->next_posi += window_size;
143 
144         return window_size;
145     }
146     assert(window->left_bytes == 0);
147 
148     // only the first block may > UINT32_MAX
149     allocated = _BlocksOutputBuffer_Grow(
150                     buffer, (void**) next_out, (Py_ssize_t) *avail_out);
151     *avail_out = (uint32_t) allocated;
152     return allocated;
153 }
154 
155 /* Finish the buffer.
156 
157    On success, return a bytes object
158    On failure, return NULL */
159 static inline PyObject *
OutputBuffer_WindowFinish(_BlocksOutputBuffer * buffer,_Uint32Window * window,uint32_t avail_out)160 OutputBuffer_WindowFinish(_BlocksOutputBuffer *buffer, _Uint32Window *window,
161                           uint32_t avail_out)
162 {
163     Py_ssize_t real_avail_out = (Py_ssize_t) avail_out + window->left_bytes;
164     return _BlocksOutputBuffer_Finish(buffer, real_avail_out);
165 }
166 
167 static inline void
OutputBuffer_WindowOnError(_BlocksOutputBuffer * buffer,_Uint32Window * window)168 OutputBuffer_WindowOnError(_BlocksOutputBuffer *buffer, _Uint32Window *window)
169 {
170     _BlocksOutputBuffer_OnError(buffer);
171 }
172 
173 
174 #define ENTER_ZLIB(obj) do {                      \
175     if (!PyThread_acquire_lock((obj)->lock, 0)) { \
176         Py_BEGIN_ALLOW_THREADS                    \
177         PyThread_acquire_lock((obj)->lock, 1);    \
178         Py_END_ALLOW_THREADS                      \
179     } } while (0)
180 #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
181 
182 
183 /* The following parameters are copied from zutil.h, version 0.95 */
184 #define DEFLATED   8
185 #if MAX_MEM_LEVEL >= 8
186 #  define DEF_MEM_LEVEL 8
187 #else
188 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
189 #endif
190 
191 /* Initial buffer size. */
192 #define DEF_BUF_SIZE (16*1024)
193 #define DEF_MAX_INITIAL_BUF_SIZE (16 * 1024 * 1024)
194 
195 static PyModuleDef zlibmodule;
196 
197 typedef struct {
198     PyTypeObject *Comptype;
199     PyTypeObject *Decomptype;
200     PyTypeObject *ZlibDecompressorType;
201     PyObject *ZlibError;
202 } zlibstate;
203 
204 static inline zlibstate*
get_zlib_state(PyObject * module)205 get_zlib_state(PyObject *module)
206 {
207     void *state = PyModule_GetState(module);
208     assert(state != NULL);
209     return (zlibstate *)state;
210 }
211 
212 typedef struct
213 {
214     PyObject_HEAD
215     z_stream zst;
216     PyObject *unused_data;
217     PyObject *unconsumed_tail;
218     char eof;
219     bool is_initialised;
220     PyObject *zdict;
221     PyThread_type_lock lock;
222 } compobject;
223 
224 static void
zlib_error(zlibstate * state,z_stream zst,int err,const char * msg)225 zlib_error(zlibstate *state, z_stream zst, int err, const char *msg)
226 {
227     const char *zmsg = Z_NULL;
228     /* In case of a version mismatch, zst.msg won't be initialized.
229        Check for this case first, before looking at zst.msg. */
230     if (err == Z_VERSION_ERROR)
231         zmsg = "library version mismatch";
232     if (zmsg == Z_NULL)
233         zmsg = zst.msg;
234     if (zmsg == Z_NULL) {
235         switch (err) {
236         case Z_BUF_ERROR:
237             zmsg = "incomplete or truncated stream";
238             break;
239         case Z_STREAM_ERROR:
240             zmsg = "inconsistent stream state";
241             break;
242         case Z_DATA_ERROR:
243             zmsg = "invalid input data";
244             break;
245         }
246     }
247     if (zmsg == Z_NULL)
248         PyErr_Format(state->ZlibError, "Error %d %s", err, msg);
249     else
250         PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
251 }
252 
253 /*[clinic input]
254 module zlib
255 class zlib.Compress "compobject *" "&Comptype"
256 class zlib.Decompress "compobject *" "&Decomptype"
257 [clinic start generated code]*/
258 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
259 
260 static compobject *
newcompobject(PyTypeObject * type)261 newcompobject(PyTypeObject *type)
262 {
263     compobject *self;
264     self = PyObject_New(compobject, type);
265     if (self == NULL)
266         return NULL;
267     self->eof = 0;
268     self->is_initialised = 0;
269     self->zdict = NULL;
270     self->unused_data = PyBytes_FromStringAndSize("", 0);
271     if (self->unused_data == NULL) {
272         Py_DECREF(self);
273         return NULL;
274     }
275     self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
276     if (self->unconsumed_tail == NULL) {
277         Py_DECREF(self);
278         return NULL;
279     }
280     self->lock = PyThread_allocate_lock();
281     if (self->lock == NULL) {
282         Py_DECREF(self);
283         PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
284         return NULL;
285     }
286     return self;
287 }
288 
289 static void*
PyZlib_Malloc(voidpf ctx,uInt items,uInt size)290 PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
291 {
292     if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
293         return NULL;
294     /* PyMem_Malloc() cannot be used: the GIL is not held when
295        inflate() and deflate() are called */
296     return PyMem_RawMalloc((size_t)items * (size_t)size);
297 }
298 
299 static void
PyZlib_Free(voidpf ctx,void * ptr)300 PyZlib_Free(voidpf ctx, void *ptr)
301 {
302     PyMem_RawFree(ptr);
303 }
304 
305 static void
arrange_input_buffer(z_stream * zst,Py_ssize_t * remains)306 arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
307 {
308     zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
309     *remains -= zst->avail_in;
310 }
311 
312 /*[clinic input]
313 zlib.compress
314 
315     data: Py_buffer
316         Binary data to be compressed.
317     /
318     level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
319         Compression level, in 0-9 or -1.
320     wbits: int(c_default="MAX_WBITS") = MAX_WBITS
321         The window buffer size and container format.
322 
323 Returns a bytes object containing compressed data.
324 [clinic start generated code]*/
325 
326 static PyObject *
zlib_compress_impl(PyObject * module,Py_buffer * data,int level,int wbits)327 zlib_compress_impl(PyObject *module, Py_buffer *data, int level, int wbits)
328 /*[clinic end generated code: output=46bd152fadd66df2 input=c4d06ee5782a7e3f]*/
329 {
330     PyObject *return_value;
331     int flush;
332     z_stream zst;
333     _BlocksOutputBuffer buffer = {.list = NULL};
334 
335     zlibstate *state = get_zlib_state(module);
336 
337     Byte *ibuf = data->buf;
338     Py_ssize_t ibuflen = data->len;
339 
340     if (OutputBuffer_InitAndGrow(&buffer, -1, &zst.next_out, &zst.avail_out) < 0) {
341         goto error;
342     }
343 
344     zst.opaque = NULL;
345     zst.zalloc = PyZlib_Malloc;
346     zst.zfree = PyZlib_Free;
347     zst.next_in = ibuf;
348     int err = deflateInit2(&zst, level, DEFLATED, wbits, DEF_MEM_LEVEL,
349                            Z_DEFAULT_STRATEGY);
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 compressing data");
357         goto error;
358     case Z_STREAM_ERROR:
359         PyErr_SetString(state->ZlibError, "Bad compression level");
360         goto error;
361     default:
362         deflateEnd(&zst);
363         zlib_error(state, zst, err, "while compressing data");
364         goto error;
365     }
366 
367     do {
368         arrange_input_buffer(&zst, &ibuflen);
369         flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
370 
371         do {
372             if (zst.avail_out == 0) {
373                 if (OutputBuffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
374                     deflateEnd(&zst);
375                     goto error;
376                 }
377             }
378 
379             Py_BEGIN_ALLOW_THREADS
380             err = deflate(&zst, flush);
381             Py_END_ALLOW_THREADS
382 
383             if (err == Z_STREAM_ERROR) {
384                 deflateEnd(&zst);
385                 zlib_error(state, zst, err, "while compressing data");
386                 goto error;
387             }
388 
389         } while (zst.avail_out == 0);
390         assert(zst.avail_in == 0);
391 
392     } while (flush != Z_FINISH);
393     assert(err == Z_STREAM_END);
394 
395     err = deflateEnd(&zst);
396     if (err == Z_OK) {
397         return_value = OutputBuffer_Finish(&buffer, zst.avail_out);
398         if (return_value == NULL) {
399             goto error;
400         }
401         return return_value;
402     }
403     else
404         zlib_error(state, zst, err, "while finishing compression");
405  error:
406     OutputBuffer_OnError(&buffer);
407     return NULL;
408 }
409 
410 /*[clinic input]
411 zlib.decompress
412 
413     data: Py_buffer
414         Compressed data.
415     /
416     wbits: int(c_default="MAX_WBITS") = MAX_WBITS
417         The window buffer size and container format.
418     bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
419         The initial output buffer size.
420 
421 Returns a bytes object containing the uncompressed data.
422 [clinic start generated code]*/
423 
424 static PyObject *
zlib_decompress_impl(PyObject * module,Py_buffer * data,int wbits,Py_ssize_t bufsize)425 zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
426                      Py_ssize_t bufsize)
427 /*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
428 {
429     PyObject *return_value;
430     Byte *ibuf;
431     Py_ssize_t ibuflen;
432     int err, flush;
433     z_stream zst;
434     _BlocksOutputBuffer buffer = {.list = NULL};
435     _Uint32Window window;  // output buffer's UINT32_MAX sliding window
436 
437     zlibstate *state = get_zlib_state(module);
438 
439     if (bufsize < 0) {
440         PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
441         return NULL;
442     } else if (bufsize == 0) {
443         bufsize = 1;
444     }
445 
446     if (OutputBuffer_WindowInitWithSize(&buffer, &window, bufsize,
447                                         &zst.next_out, &zst.avail_out) < 0) {
448         goto error;
449     }
450 
451     ibuf = data->buf;
452     ibuflen = data->len;
453 
454     zst.opaque = NULL;
455     zst.zalloc = PyZlib_Malloc;
456     zst.zfree = PyZlib_Free;
457     zst.avail_in = 0;
458     zst.next_in = ibuf;
459     err = inflateInit2(&zst, wbits);
460 
461     switch (err) {
462     case Z_OK:
463         break;
464     case Z_MEM_ERROR:
465         PyErr_SetString(PyExc_MemoryError,
466                         "Out of memory while decompressing data");
467         goto error;
468     default:
469         inflateEnd(&zst);
470         zlib_error(state, zst, err, "while preparing to decompress data");
471         goto error;
472     }
473 
474     do {
475         arrange_input_buffer(&zst, &ibuflen);
476         flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
477 
478         do {
479             if (zst.avail_out == 0) {
480                 if (OutputBuffer_WindowGrow(&buffer, &window,
481                                             &zst.next_out, &zst.avail_out) < 0) {
482                     inflateEnd(&zst);
483                     goto error;
484                 }
485             }
486 
487             Py_BEGIN_ALLOW_THREADS
488             err = inflate(&zst, flush);
489             Py_END_ALLOW_THREADS
490 
491             switch (err) {
492             case Z_OK:            /* fall through */
493             case Z_BUF_ERROR:     /* fall through */
494             case Z_STREAM_END:
495                 break;
496             case Z_MEM_ERROR:
497                 inflateEnd(&zst);
498                 PyErr_SetString(PyExc_MemoryError,
499                                 "Out of memory while decompressing data");
500                 goto error;
501             default:
502                 inflateEnd(&zst);
503                 zlib_error(state, zst, err, "while decompressing data");
504                 goto error;
505             }
506 
507         } while (zst.avail_out == 0);
508 
509     } while (err != Z_STREAM_END && ibuflen != 0);
510 
511 
512     if (err != Z_STREAM_END) {
513         inflateEnd(&zst);
514         zlib_error(state, zst, err, "while decompressing data");
515         goto error;
516     }
517 
518     err = inflateEnd(&zst);
519     if (err != Z_OK) {
520         zlib_error(state, zst, err, "while finishing decompression");
521         goto error;
522     }
523 
524     return_value = OutputBuffer_WindowFinish(&buffer, &window, zst.avail_out);
525     if (return_value != NULL) {
526         return return_value;
527     }
528 
529  error:
530     OutputBuffer_WindowOnError(&buffer, &window);
531     return NULL;
532 }
533 
534 /*[clinic input]
535 zlib.compressobj
536 
537     level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
538         The compression level (an integer in the range 0-9 or -1; default is
539         currently equivalent to 6).  Higher compression levels are slower,
540         but produce smaller results.
541     method: int(c_default="DEFLATED") = DEFLATED
542         The compression algorithm.  If given, this must be DEFLATED.
543     wbits: int(c_default="MAX_WBITS") = MAX_WBITS
544         +9 to +15: The base-two logarithm of the window size.  Include a zlib
545             container.
546         -9 to -15: Generate a raw stream.
547         +25 to +31: Include a gzip container.
548     memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
549         Controls the amount of memory used for internal compression state.
550         Valid values range from 1 to 9.  Higher values result in higher memory
551         usage, faster compression, and smaller output.
552     strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
553         Used to tune the compression algorithm.  Possible values are
554         Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
555     zdict: Py_buffer = None
556         The predefined compression dictionary - a sequence of bytes
557         containing subsequences that are likely to occur in the input data.
558 
559 Return a compressor object.
560 [clinic start generated code]*/
561 
562 static PyObject *
zlib_compressobj_impl(PyObject * module,int level,int method,int wbits,int memLevel,int strategy,Py_buffer * zdict)563 zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
564                       int memLevel, int strategy, Py_buffer *zdict)
565 /*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
566 {
567     zlibstate *state = get_zlib_state(module);
568     if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
569         PyErr_SetString(PyExc_OverflowError,
570                         "zdict length does not fit in an unsigned int");
571         return NULL;
572     }
573 
574     compobject *self = newcompobject(state->Comptype);
575     if (self == NULL)
576         goto error;
577     self->zst.opaque = NULL;
578     self->zst.zalloc = PyZlib_Malloc;
579     self->zst.zfree = PyZlib_Free;
580     self->zst.next_in = NULL;
581     self->zst.avail_in = 0;
582     int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
583     switch (err) {
584     case Z_OK:
585         self->is_initialised = 1;
586         if (zdict->buf == NULL) {
587             goto success;
588         } else {
589             err = deflateSetDictionary(&self->zst,
590                                        zdict->buf, (unsigned int)zdict->len);
591             switch (err) {
592             case Z_OK:
593                 goto success;
594             case Z_STREAM_ERROR:
595                 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
596                 goto error;
597             default:
598                 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
599                 goto error;
600             }
601        }
602     case Z_MEM_ERROR:
603         PyErr_SetString(PyExc_MemoryError,
604                         "Can't allocate memory for compression object");
605         goto error;
606     case Z_STREAM_ERROR:
607         PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
608         goto error;
609     default:
610         zlib_error(state, self->zst, err, "while creating compression object");
611         goto error;
612     }
613 
614  error:
615     Py_CLEAR(self);
616  success:
617     return (PyObject *)self;
618 }
619 
620 static int
set_inflate_zdict(zlibstate * state,compobject * self)621 set_inflate_zdict(zlibstate *state, compobject *self)
622 {
623     Py_buffer zdict_buf;
624     if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
625         return -1;
626     }
627     if ((size_t)zdict_buf.len > UINT_MAX) {
628         PyErr_SetString(PyExc_OverflowError,
629                         "zdict length does not fit in an unsigned int");
630         PyBuffer_Release(&zdict_buf);
631         return -1;
632     }
633     int err;
634     err = inflateSetDictionary(&self->zst,
635                                zdict_buf.buf, (unsigned int)zdict_buf.len);
636     PyBuffer_Release(&zdict_buf);
637     if (err != Z_OK) {
638         zlib_error(state, self->zst, err, "while setting zdict");
639         return -1;
640     }
641     return 0;
642 }
643 
644 /*[clinic input]
645 zlib.decompressobj
646 
647     wbits: int(c_default="MAX_WBITS") = MAX_WBITS
648         The window buffer size and container format.
649     zdict: object(c_default="NULL") = b''
650         The predefined compression dictionary.  This must be the same
651         dictionary as used by the compressor that produced the input data.
652 
653 Return a decompressor object.
654 [clinic start generated code]*/
655 
656 static PyObject *
zlib_decompressobj_impl(PyObject * module,int wbits,PyObject * zdict)657 zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
658 /*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
659 {
660     zlibstate *state = get_zlib_state(module);
661 
662     if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
663         PyErr_SetString(PyExc_TypeError,
664                         "zdict argument must support the buffer protocol");
665         return NULL;
666     }
667 
668     compobject *self = newcompobject(state->Decomptype);
669     if (self == NULL)
670         return NULL;
671     self->zst.opaque = NULL;
672     self->zst.zalloc = PyZlib_Malloc;
673     self->zst.zfree = PyZlib_Free;
674     self->zst.next_in = NULL;
675     self->zst.avail_in = 0;
676     if (zdict != NULL) {
677         self->zdict = Py_NewRef(zdict);
678     }
679     int err = inflateInit2(&self->zst, wbits);
680     switch (err) {
681     case Z_OK:
682         self->is_initialised = 1;
683         if (self->zdict != NULL && wbits < 0) {
684             if (set_inflate_zdict(state, self) < 0) {
685                 Py_DECREF(self);
686                 return NULL;
687             }
688         }
689         return (PyObject *)self;
690     case Z_STREAM_ERROR:
691         Py_DECREF(self);
692         PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
693         return NULL;
694     case Z_MEM_ERROR:
695         Py_DECREF(self);
696         PyErr_SetString(PyExc_MemoryError,
697                         "Can't allocate memory for decompression object");
698         return NULL;
699     default:
700         zlib_error(state, self->zst, err, "while creating decompression object");
701         Py_DECREF(self);
702         return NULL;
703     }
704 }
705 
706 static void
Dealloc(compobject * self)707 Dealloc(compobject *self)
708 {
709     PyObject *type = (PyObject *)Py_TYPE(self);
710     PyThread_free_lock(self->lock);
711     Py_XDECREF(self->unused_data);
712     Py_XDECREF(self->unconsumed_tail);
713     Py_XDECREF(self->zdict);
714     PyObject_Free(self);
715     Py_DECREF(type);
716 }
717 
718 static void
Comp_dealloc(compobject * self)719 Comp_dealloc(compobject *self)
720 {
721     if (self->is_initialised)
722         deflateEnd(&self->zst);
723     Dealloc(self);
724 }
725 
726 static void
Decomp_dealloc(compobject * self)727 Decomp_dealloc(compobject *self)
728 {
729     if (self->is_initialised)
730         inflateEnd(&self->zst);
731     Dealloc(self);
732 }
733 
734 /*[clinic input]
735 zlib.Compress.compress
736 
737     cls: defining_class
738     data: Py_buffer
739         Binary data to be compressed.
740     /
741 
742 Returns a bytes object containing compressed data.
743 
744 After calling this function, some of the input data may still
745 be stored in internal buffers for later processing.
746 Call the flush() method to clear these buffers.
747 [clinic start generated code]*/
748 
749 static PyObject *
zlib_Compress_compress_impl(compobject * self,PyTypeObject * cls,Py_buffer * data)750 zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
751                             Py_buffer *data)
752 /*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/
753 {
754     PyObject *return_value;
755     int err;
756     _BlocksOutputBuffer buffer = {.list = NULL};
757     zlibstate *state = PyType_GetModuleState(cls);
758 
759     ENTER_ZLIB(self);
760 
761     self->zst.next_in = data->buf;
762     Py_ssize_t ibuflen = data->len;
763 
764     if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
765         goto error;
766     }
767 
768     do {
769         arrange_input_buffer(&self->zst, &ibuflen);
770 
771         do {
772             if (self->zst.avail_out == 0) {
773                 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
774                     goto error;
775                 }
776             }
777 
778             Py_BEGIN_ALLOW_THREADS
779             err = deflate(&self->zst, Z_NO_FLUSH);
780             Py_END_ALLOW_THREADS
781 
782             if (err == Z_STREAM_ERROR) {
783                 zlib_error(state, self->zst, err, "while compressing data");
784                 goto error;
785             }
786 
787         } while (self->zst.avail_out == 0);
788         assert(self->zst.avail_in == 0);
789 
790     } while (ibuflen != 0);
791 
792     return_value = OutputBuffer_Finish(&buffer, self->zst.avail_out);
793     if (return_value != NULL) {
794         goto success;
795     }
796 
797  error:
798     OutputBuffer_OnError(&buffer);
799     return_value = NULL;
800  success:
801     LEAVE_ZLIB(self);
802     return return_value;
803 }
804 
805 /* Helper for objdecompress() and flush(). Saves any unconsumed input data in
806    self->unused_data or self->unconsumed_tail, as appropriate. */
807 static int
save_unconsumed_input(compobject * self,Py_buffer * data,int err)808 save_unconsumed_input(compobject *self, Py_buffer *data, int err)
809 {
810     if (err == Z_STREAM_END) {
811         /* The end of the compressed data has been reached. Store the leftover
812            input data in self->unused_data. */
813         if (self->zst.avail_in > 0) {
814             Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
815             Py_ssize_t new_size, left_size;
816             PyObject *new_data;
817             left_size = (Byte *)data->buf + data->len - self->zst.next_in;
818             if (left_size > (PY_SSIZE_T_MAX - old_size)) {
819                 PyErr_NoMemory();
820                 return -1;
821             }
822             new_size = old_size + left_size;
823             new_data = PyBytes_FromStringAndSize(NULL, new_size);
824             if (new_data == NULL)
825                 return -1;
826             memcpy(PyBytes_AS_STRING(new_data),
827                       PyBytes_AS_STRING(self->unused_data), old_size);
828             memcpy(PyBytes_AS_STRING(new_data) + old_size,
829                       self->zst.next_in, left_size);
830             Py_SETREF(self->unused_data, new_data);
831             self->zst.avail_in = 0;
832         }
833     }
834 
835     if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
836         /* This code handles two distinct cases:
837            1. Output limit was reached. Save leftover input in unconsumed_tail.
838            2. All input data was consumed. Clear unconsumed_tail. */
839         Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
840         PyObject *new_data = PyBytes_FromStringAndSize(
841                 (char *)self->zst.next_in, left_size);
842         if (new_data == NULL)
843             return -1;
844         Py_SETREF(self->unconsumed_tail, new_data);
845     }
846 
847     return 0;
848 }
849 
850 /*[clinic input]
851 zlib.Decompress.decompress
852 
853     cls: defining_class
854     data: Py_buffer
855         The binary data to decompress.
856     /
857     max_length: Py_ssize_t = 0
858         The maximum allowable length of the decompressed data.
859         Unconsumed input data will be stored in
860         the unconsumed_tail attribute.
861 
862 Return a bytes object containing the decompressed version of the data.
863 
864 After calling this function, some of the input data may still be stored in
865 internal buffers for later processing.
866 Call the flush() method to clear these buffers.
867 [clinic start generated code]*/
868 
869 static PyObject *
zlib_Decompress_decompress_impl(compobject * self,PyTypeObject * cls,Py_buffer * data,Py_ssize_t max_length)870 zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
871                                 Py_buffer *data, Py_ssize_t max_length)
872 /*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/
873 {
874     int err = Z_OK;
875     Py_ssize_t ibuflen;
876     PyObject *return_value;
877     _BlocksOutputBuffer buffer = {.list = NULL};
878 
879     PyObject *module = PyType_GetModule(cls);
880     if (module == NULL)
881         return NULL;
882 
883     zlibstate *state = get_zlib_state(module);
884     if (max_length < 0) {
885         PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
886         return NULL;
887     } else if (max_length == 0) {
888         max_length = -1;
889     }
890 
891     ENTER_ZLIB(self);
892 
893     self->zst.next_in = data->buf;
894     ibuflen = data->len;
895 
896     if (OutputBuffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) {
897         goto abort;
898     }
899 
900     do {
901         arrange_input_buffer(&self->zst, &ibuflen);
902 
903         do {
904             if (self->zst.avail_out == 0) {
905                 if (OutputBuffer_GetDataSize(&buffer, self->zst.avail_out) == max_length) {
906                     goto save;
907                 }
908                 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
909                     goto abort;
910                 }
911             }
912 
913             Py_BEGIN_ALLOW_THREADS
914             err = inflate(&self->zst, Z_SYNC_FLUSH);
915             Py_END_ALLOW_THREADS
916 
917             switch (err) {
918             case Z_OK:            /* fall through */
919             case Z_BUF_ERROR:     /* fall through */
920             case Z_STREAM_END:
921                 break;
922             default:
923                 if (err == Z_NEED_DICT && self->zdict != NULL) {
924                     if (set_inflate_zdict(state, self) < 0) {
925                         goto abort;
926                     }
927                     else
928                         break;
929                 }
930                 goto save;
931             }
932 
933         } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
934 
935     } while (err != Z_STREAM_END && ibuflen != 0);
936 
937  save:
938     if (save_unconsumed_input(self, data, err) < 0)
939         goto abort;
940 
941     if (err == Z_STREAM_END) {
942         /* This is the logical place to call inflateEnd, but the old behaviour
943            of only calling it on flush() is preserved. */
944         self->eof = 1;
945     } else if (err != Z_OK && err != Z_BUF_ERROR) {
946         /* We will only get Z_BUF_ERROR if the output buffer was full
947            but there wasn't more output when we tried again, so it is
948            not an error condition.
949         */
950         zlib_error(state, self->zst, err, "while decompressing data");
951         goto abort;
952     }
953 
954     return_value = OutputBuffer_Finish(&buffer, self->zst.avail_out);
955     if (return_value != NULL) {
956         goto success;
957     }
958 
959  abort:
960     OutputBuffer_OnError(&buffer);
961     return_value = NULL;
962  success:
963     LEAVE_ZLIB(self);
964     return return_value;
965 }
966 
967 /*[clinic input]
968 zlib.Compress.flush
969 
970     cls: defining_class
971     mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
972         One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
973         If mode == Z_FINISH, the compressor object can no longer be
974         used after calling the flush() method.  Otherwise, more data
975         can still be compressed.
976     /
977 
978 Return a bytes object containing any remaining compressed data.
979 [clinic start generated code]*/
980 
981 static PyObject *
zlib_Compress_flush_impl(compobject * self,PyTypeObject * cls,int mode)982 zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
983 /*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/
984 {
985     int err;
986     PyObject *return_value;
987     _BlocksOutputBuffer buffer = {.list = NULL};
988 
989     zlibstate *state = PyType_GetModuleState(cls);
990     /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
991        doing any work at all; just return an empty string. */
992     if (mode == Z_NO_FLUSH) {
993         return PyBytes_FromStringAndSize(NULL, 0);
994     }
995 
996     ENTER_ZLIB(self);
997 
998     self->zst.avail_in = 0;
999 
1000     if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
1001         goto error;
1002     }
1003 
1004     do {
1005         if (self->zst.avail_out == 0) {
1006             if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
1007                 goto error;
1008             }
1009         }
1010 
1011         Py_BEGIN_ALLOW_THREADS
1012         err = deflate(&self->zst, mode);
1013         Py_END_ALLOW_THREADS
1014 
1015         if (err == Z_STREAM_ERROR) {
1016             zlib_error(state, self->zst, err, "while flushing");
1017             goto error;
1018         }
1019     } while (self->zst.avail_out == 0);
1020     assert(self->zst.avail_in == 0);
1021 
1022     /* If mode is Z_FINISH, we also have to call deflateEnd() to free
1023        various data structures. Note we should only get Z_STREAM_END when
1024        mode is Z_FINISH, but checking both for safety*/
1025     if (err == Z_STREAM_END && mode == Z_FINISH) {
1026         err = deflateEnd(&self->zst);
1027         if (err != Z_OK) {
1028             zlib_error(state, self->zst, err, "while finishing compression");
1029             goto error;
1030         }
1031         else
1032             self->is_initialised = 0;
1033 
1034         /* We will only get Z_BUF_ERROR if the output buffer was full
1035            but there wasn't more output when we tried again, so it is
1036            not an error condition.
1037         */
1038     } else if (err != Z_OK && err != Z_BUF_ERROR) {
1039         zlib_error(state, self->zst, err, "while flushing");
1040         goto error;
1041     }
1042 
1043     return_value = OutputBuffer_Finish(&buffer, self->zst.avail_out);
1044     if (return_value != NULL) {
1045         goto success;
1046     }
1047 
1048 error:
1049     OutputBuffer_OnError(&buffer);
1050     return_value = NULL;
1051 success:
1052     LEAVE_ZLIB(self);
1053     return return_value;
1054 }
1055 
1056 #ifdef HAVE_ZLIB_COPY
1057 
1058 /*[clinic input]
1059 zlib.Compress.copy
1060 
1061     cls: defining_class
1062 
1063 Return a copy of the compression object.
1064 [clinic start generated code]*/
1065 
1066 static PyObject *
zlib_Compress_copy_impl(compobject * self,PyTypeObject * cls)1067 zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
1068 /*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
1069 {
1070     zlibstate *state = PyType_GetModuleState(cls);
1071 
1072     compobject *return_value = newcompobject(state->Comptype);
1073     if (!return_value) return NULL;
1074 
1075     /* Copy the zstream state
1076      * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1077      */
1078     ENTER_ZLIB(self);
1079     int err = deflateCopy(&return_value->zst, &self->zst);
1080     switch (err) {
1081     case Z_OK:
1082         break;
1083     case Z_STREAM_ERROR:
1084         PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1085         goto error;
1086     case Z_MEM_ERROR:
1087         PyErr_SetString(PyExc_MemoryError,
1088                         "Can't allocate memory for compression object");
1089         goto error;
1090     default:
1091         zlib_error(state, self->zst, err, "while copying compression object");
1092         goto error;
1093     }
1094     Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
1095     Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
1096     Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
1097     return_value->eof = self->eof;
1098 
1099     /* Mark it as being initialized */
1100     return_value->is_initialised = 1;
1101 
1102     LEAVE_ZLIB(self);
1103     return (PyObject *)return_value;
1104 
1105 error:
1106     LEAVE_ZLIB(self);
1107     Py_XDECREF(return_value);
1108     return NULL;
1109 }
1110 
1111 /*[clinic input]
1112 zlib.Compress.__copy__
1113 
1114     cls: defining_class
1115 
1116 [clinic start generated code]*/
1117 
1118 static PyObject *
zlib_Compress___copy___impl(compobject * self,PyTypeObject * cls)1119 zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
1120 /*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
1121 {
1122     return zlib_Compress_copy_impl(self, cls);
1123 }
1124 
1125 /*[clinic input]
1126 zlib.Compress.__deepcopy__
1127 
1128     cls: defining_class
1129     memo: object
1130     /
1131 
1132 [clinic start generated code]*/
1133 
1134 static PyObject *
zlib_Compress___deepcopy___impl(compobject * self,PyTypeObject * cls,PyObject * memo)1135 zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1136                                 PyObject *memo)
1137 /*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
1138 {
1139     return zlib_Compress_copy_impl(self, cls);
1140 }
1141 
1142 /*[clinic input]
1143 zlib.Decompress.copy
1144 
1145     cls: defining_class
1146 
1147 Return a copy of the decompression object.
1148 [clinic start generated code]*/
1149 
1150 static PyObject *
zlib_Decompress_copy_impl(compobject * self,PyTypeObject * cls)1151 zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
1152 /*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
1153 {
1154     zlibstate *state = PyType_GetModuleState(cls);
1155 
1156     compobject *return_value = newcompobject(state->Decomptype);
1157     if (!return_value) return NULL;
1158 
1159     /* Copy the zstream state
1160      * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1161      */
1162     ENTER_ZLIB(self);
1163     int err = inflateCopy(&return_value->zst, &self->zst);
1164     switch (err) {
1165     case Z_OK:
1166         break;
1167     case Z_STREAM_ERROR:
1168         PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1169         goto error;
1170     case Z_MEM_ERROR:
1171         PyErr_SetString(PyExc_MemoryError,
1172                         "Can't allocate memory for decompression object");
1173         goto error;
1174     default:
1175         zlib_error(state, self->zst, err, "while copying decompression object");
1176         goto error;
1177     }
1178 
1179     Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
1180     Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
1181     Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
1182     return_value->eof = self->eof;
1183 
1184     /* Mark it as being initialized */
1185     return_value->is_initialised = 1;
1186 
1187     LEAVE_ZLIB(self);
1188     return (PyObject *)return_value;
1189 
1190 error:
1191     LEAVE_ZLIB(self);
1192     Py_XDECREF(return_value);
1193     return NULL;
1194 }
1195 
1196 /*[clinic input]
1197 zlib.Decompress.__copy__
1198 
1199     cls: defining_class
1200 
1201 [clinic start generated code]*/
1202 
1203 static PyObject *
zlib_Decompress___copy___impl(compobject * self,PyTypeObject * cls)1204 zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
1205 /*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
1206 {
1207     return zlib_Decompress_copy_impl(self, cls);
1208 }
1209 
1210 /*[clinic input]
1211 zlib.Decompress.__deepcopy__
1212 
1213     cls: defining_class
1214     memo: object
1215     /
1216 
1217 [clinic start generated code]*/
1218 
1219 static PyObject *
zlib_Decompress___deepcopy___impl(compobject * self,PyTypeObject * cls,PyObject * memo)1220 zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1221                                   PyObject *memo)
1222 /*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
1223 {
1224     return zlib_Decompress_copy_impl(self, cls);
1225 }
1226 
1227 #endif
1228 
1229 /*[clinic input]
1230 zlib.Decompress.flush
1231 
1232     cls: defining_class
1233     length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
1234         the initial size of the output buffer.
1235     /
1236 
1237 Return a bytes object containing any remaining decompressed data.
1238 [clinic start generated code]*/
1239 
1240 static PyObject *
zlib_Decompress_flush_impl(compobject * self,PyTypeObject * cls,Py_ssize_t length)1241 zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
1242                            Py_ssize_t length)
1243 /*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/
1244 {
1245     int err, flush;
1246     Py_buffer data;
1247     PyObject *return_value;
1248     Py_ssize_t ibuflen;
1249     _BlocksOutputBuffer buffer = {.list = NULL};
1250     _Uint32Window window;  // output buffer's UINT32_MAX sliding window
1251 
1252     PyObject *module = PyType_GetModule(cls);
1253     if (module == NULL) {
1254         return NULL;
1255     }
1256 
1257     zlibstate *state = get_zlib_state(module);
1258 
1259     if (length <= 0) {
1260         PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1261         return NULL;
1262     }
1263 
1264     ENTER_ZLIB(self);
1265 
1266     if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
1267         LEAVE_ZLIB(self);
1268         return NULL;
1269     }
1270 
1271     self->zst.next_in = data.buf;
1272     ibuflen = data.len;
1273 
1274     if (OutputBuffer_WindowInitWithSize(&buffer, &window, length,
1275                                         &self->zst.next_out, &self->zst.avail_out) < 0) {
1276         goto abort;
1277     }
1278 
1279     do {
1280         arrange_input_buffer(&self->zst, &ibuflen);
1281         flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
1282 
1283         do {
1284             if (self->zst.avail_out == 0) {
1285                 if (OutputBuffer_WindowGrow(&buffer, &window,
1286                                             &self->zst.next_out, &self->zst.avail_out) < 0) {
1287                     goto abort;
1288                 }
1289             }
1290 
1291             Py_BEGIN_ALLOW_THREADS
1292             err = inflate(&self->zst, flush);
1293             Py_END_ALLOW_THREADS
1294 
1295             switch (err) {
1296             case Z_OK:            /* fall through */
1297             case Z_BUF_ERROR:     /* fall through */
1298             case Z_STREAM_END:
1299                 break;
1300             default:
1301                 goto save;
1302             }
1303 
1304         } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1305 
1306     } while (err != Z_STREAM_END && ibuflen != 0);
1307 
1308  save:
1309     if (save_unconsumed_input(self, &data, err) < 0) {
1310         goto abort;
1311     }
1312 
1313     /* If at end of stream, clean up any memory allocated by zlib. */
1314     if (err == Z_STREAM_END) {
1315         self->eof = 1;
1316         self->is_initialised = 0;
1317         err = inflateEnd(&self->zst);
1318         if (err != Z_OK) {
1319             zlib_error(state, self->zst, err, "while finishing decompression");
1320             goto abort;
1321         }
1322     }
1323 
1324     return_value = OutputBuffer_WindowFinish(&buffer, &window, self->zst.avail_out);
1325     if (return_value != NULL) {
1326         goto success;
1327     }
1328 
1329  abort:
1330     OutputBuffer_WindowOnError(&buffer, &window);
1331     return_value = NULL;
1332  success:
1333     PyBuffer_Release(&data);
1334     LEAVE_ZLIB(self);
1335     return return_value;
1336 }
1337 
1338 
1339 typedef struct {
1340     PyObject_HEAD
1341     z_stream zst;
1342     PyObject *zdict;
1343     PyThread_type_lock lock;
1344     PyObject *unused_data;
1345     uint8_t *input_buffer;
1346     Py_ssize_t input_buffer_size;
1347     /* zst>avail_in is only 32 bit, so we store the true length
1348        separately. Conversion and looping is encapsulated in
1349        decompress_buf() */
1350     Py_ssize_t avail_in_real;
1351     bool is_initialised;
1352     char eof;           /* Py_T_BOOL expects a char */
1353     char needs_input;
1354 } ZlibDecompressor;
1355 
1356 /*[clinic input]
1357 class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
1358 [clinic start generated code]*/
1359 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=0658178ab94645df]*/
1360 
1361 static void
ZlibDecompressor_dealloc(ZlibDecompressor * self)1362 ZlibDecompressor_dealloc(ZlibDecompressor *self)
1363 {
1364     PyObject *type = (PyObject *)Py_TYPE(self);
1365     PyThread_free_lock(self->lock);
1366     if (self->is_initialised) {
1367         inflateEnd(&self->zst);
1368     }
1369     PyMem_Free(self->input_buffer);
1370     Py_CLEAR(self->unused_data);
1371     Py_CLEAR(self->zdict);
1372     PyObject_Free(self);
1373     Py_DECREF(type);
1374 }
1375 
1376 static int
set_inflate_zdict_ZlibDecompressor(zlibstate * state,ZlibDecompressor * self)1377 set_inflate_zdict_ZlibDecompressor(zlibstate *state, ZlibDecompressor *self)
1378 {
1379     Py_buffer zdict_buf;
1380     if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
1381         return -1;
1382     }
1383     if ((size_t)zdict_buf.len > UINT_MAX) {
1384         PyErr_SetString(PyExc_OverflowError,
1385                         "zdict length does not fit in an unsigned int");
1386         PyBuffer_Release(&zdict_buf);
1387         return -1;
1388     }
1389     int err;
1390     err = inflateSetDictionary(&self->zst,
1391                                zdict_buf.buf, (unsigned int)zdict_buf.len);
1392     PyBuffer_Release(&zdict_buf);
1393     if (err != Z_OK) {
1394         zlib_error(state, self->zst, err, "while setting zdict");
1395         return -1;
1396     }
1397     return 0;
1398 }
1399 
1400 static Py_ssize_t
arrange_output_buffer_with_maximum(uint32_t * avail_out,uint8_t ** next_out,PyObject ** buffer,Py_ssize_t length,Py_ssize_t max_length)1401 arrange_output_buffer_with_maximum(uint32_t *avail_out,
1402                                    uint8_t **next_out,
1403                                    PyObject **buffer,
1404                                    Py_ssize_t length,
1405                                    Py_ssize_t max_length)
1406 {
1407     Py_ssize_t occupied;
1408 
1409     if (*buffer == NULL) {
1410         if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
1411             return -1;
1412         occupied = 0;
1413     }
1414     else {
1415         occupied = *next_out - (uint8_t *)PyBytes_AS_STRING(*buffer);
1416 
1417         if (length == occupied) {
1418             Py_ssize_t new_length;
1419             assert(length <= max_length);
1420             /* can not scale the buffer over max_length */
1421             if (length == max_length)
1422                 return -2;
1423             if (length <= (max_length >> 1))
1424                 new_length = length << 1;
1425             else
1426                 new_length = max_length;
1427             if (_PyBytes_Resize(buffer, new_length) < 0)
1428                 return -1;
1429             length = new_length;
1430         }
1431     }
1432 
1433     *avail_out = (uint32_t)Py_MIN((size_t)(length - occupied), UINT32_MAX);
1434     *next_out = (uint8_t *)PyBytes_AS_STRING(*buffer) + occupied;
1435 
1436     return length;
1437 }
1438 
1439 /* Decompress data of length self->avail_in_real in self->state.next_in. The
1440    output buffer is allocated dynamically and returned. If the max_length is
1441    of sufficiently low size, max_length is allocated immediately. At most
1442    max_length bytes are returned, so some of the input may not be consumed.
1443    self->state.next_in and self->avail_in_real are updated to reflect the
1444    consumed input. */
1445 static PyObject*
decompress_buf(ZlibDecompressor * self,Py_ssize_t max_length)1446 decompress_buf(ZlibDecompressor *self, Py_ssize_t max_length)
1447 {
1448     /* data_size is strictly positive, but because we repeatedly have to
1449        compare against max_length and PyBytes_GET_SIZE we declare it as
1450        signed */
1451     PyObject *return_value = NULL;
1452     Py_ssize_t hard_limit;
1453     Py_ssize_t obuflen;
1454     zlibstate *state = PyType_GetModuleState(Py_TYPE(self));
1455 
1456     int err = Z_OK;
1457 
1458     /* When sys.maxsize is passed as default use DEF_BUF_SIZE as start buffer.
1459        In this particular case the data may not necessarily be very big, so
1460        it is better to grow dynamically.*/
1461     if ((max_length < 0) || max_length == PY_SSIZE_T_MAX) {
1462         hard_limit = PY_SSIZE_T_MAX;
1463         obuflen = DEF_BUF_SIZE;
1464     } else {
1465         /* Assume that decompressor is used in file decompression with a fixed
1466            block size of max_length. In that case we will reach max_length almost
1467            always (except at the end of the file). So it makes sense to allocate
1468            max_length. */
1469         hard_limit = max_length;
1470         obuflen = max_length;
1471         if (obuflen > DEF_MAX_INITIAL_BUF_SIZE){
1472             // Safeguard against memory overflow.
1473             obuflen = DEF_MAX_INITIAL_BUF_SIZE;
1474         }
1475     }
1476 
1477     do {
1478         arrange_input_buffer(&(self->zst), &(self->avail_in_real));
1479 
1480         do {
1481             obuflen = arrange_output_buffer_with_maximum(&(self->zst.avail_out),
1482                                                         &(self->zst.next_out),
1483                                                         &return_value,
1484                                                         obuflen,
1485                                                         hard_limit);
1486             if (obuflen == -1){
1487                 PyErr_SetString(PyExc_MemoryError,
1488                                 "Insufficient memory for buffer allocation");
1489                 goto error;
1490             }
1491             else if (obuflen == -2) {
1492                 break;
1493             }
1494             Py_BEGIN_ALLOW_THREADS
1495             err = inflate(&self->zst, Z_SYNC_FLUSH);
1496             Py_END_ALLOW_THREADS
1497             switch (err) {
1498             case Z_OK:            /* fall through */
1499             case Z_BUF_ERROR:     /* fall through */
1500             case Z_STREAM_END:
1501                 break;
1502             default:
1503                 if (err == Z_NEED_DICT) {
1504                     goto error;
1505                 }
1506                 else {
1507                     break;
1508                 }
1509             }
1510         } while (self->zst.avail_out == 0);
1511     } while(err != Z_STREAM_END && self->avail_in_real != 0);
1512 
1513     if (err == Z_STREAM_END) {
1514         self->eof = 1;
1515         self->is_initialised = 0;
1516         /* Unlike the Decompress object we call inflateEnd here as there are no
1517            backwards compatibility issues */
1518         err = inflateEnd(&self->zst);
1519         if (err != Z_OK) {
1520             zlib_error(state, self->zst, err, "while finishing decompression");
1521             goto error;
1522         }
1523     } else if (err != Z_OK && err != Z_BUF_ERROR) {
1524         zlib_error(state, self->zst, err, "while decompressing data");
1525         goto error;
1526     }
1527 
1528     self->avail_in_real += self->zst.avail_in;
1529 
1530     if (_PyBytes_Resize(&return_value, self->zst.next_out -
1531                         (uint8_t *)PyBytes_AS_STRING(return_value)) != 0) {
1532         goto error;
1533     }
1534 
1535     goto success;
1536 error:
1537     Py_CLEAR(return_value);
1538 success:
1539     return return_value;
1540 }
1541 
1542 
1543 static PyObject *
decompress(ZlibDecompressor * self,uint8_t * data,size_t len,Py_ssize_t max_length)1544 decompress(ZlibDecompressor *self, uint8_t *data,
1545            size_t len, Py_ssize_t max_length)
1546 {
1547     bool input_buffer_in_use;
1548     PyObject *result;
1549 
1550     /* Prepend unconsumed input if necessary */
1551     if (self->zst.next_in != NULL) {
1552         size_t avail_now, avail_total;
1553 
1554         /* Number of bytes we can append to input buffer */
1555         avail_now = (self->input_buffer + self->input_buffer_size)
1556             - (self->zst.next_in + self->avail_in_real);
1557 
1558         /* Number of bytes we can append if we move existing
1559            contents to beginning of buffer (overwriting
1560            consumed input) */
1561         avail_total = self->input_buffer_size - self->avail_in_real;
1562 
1563         if (avail_total < len) {
1564             size_t offset = self->zst.next_in - self->input_buffer;
1565             uint8_t *tmp;
1566             size_t new_size = self->input_buffer_size + len - avail_now;
1567 
1568             /* Assign to temporary variable first, so we don't
1569                lose address of allocated buffer if realloc fails */
1570             tmp = PyMem_Realloc(self->input_buffer, new_size);
1571             if (tmp == NULL) {
1572                 PyErr_SetNone(PyExc_MemoryError);
1573                 return NULL;
1574             }
1575             self->input_buffer = tmp;
1576             self->input_buffer_size = new_size;
1577 
1578             self->zst.next_in = self->input_buffer + offset;
1579         }
1580         else if (avail_now < len) {
1581             memmove(self->input_buffer, self->zst.next_in,
1582                     self->avail_in_real);
1583             self->zst.next_in = self->input_buffer;
1584         }
1585         memcpy((void*)(self->zst.next_in + self->avail_in_real), data, len);
1586         self->avail_in_real += len;
1587         input_buffer_in_use = 1;
1588     }
1589     else {
1590         self->zst.next_in = data;
1591         self->avail_in_real = len;
1592         input_buffer_in_use = 0;
1593     }
1594 
1595     result = decompress_buf(self, max_length);
1596     if(result == NULL) {
1597         self->zst.next_in = NULL;
1598         return NULL;
1599     }
1600 
1601     if (self->eof) {
1602         self->needs_input = 0;
1603 
1604         if (self->avail_in_real > 0) {
1605             PyObject *unused_data = PyBytes_FromStringAndSize(
1606                 (char *)self->zst.next_in, self->avail_in_real);
1607             if (unused_data == NULL) {
1608                 goto error;
1609             }
1610             Py_XSETREF(self->unused_data, unused_data);
1611         }
1612     }
1613     else if (self->avail_in_real == 0) {
1614         self->zst.next_in = NULL;
1615         self->needs_input = 1;
1616     }
1617     else {
1618         self->needs_input = 0;
1619 
1620         /* If we did not use the input buffer, we now have
1621            to copy the tail from the caller's buffer into the
1622            input buffer */
1623         if (!input_buffer_in_use) {
1624 
1625             /* Discard buffer if it's too small
1626                (resizing it may needlessly copy the current contents) */
1627             if (self->input_buffer != NULL &&
1628                 self->input_buffer_size < self->avail_in_real) {
1629                 PyMem_Free(self->input_buffer);
1630                 self->input_buffer = NULL;
1631             }
1632 
1633             /* Allocate if necessary */
1634             if (self->input_buffer == NULL) {
1635                 self->input_buffer = PyMem_Malloc(self->avail_in_real);
1636                 if (self->input_buffer == NULL) {
1637                     PyErr_SetNone(PyExc_MemoryError);
1638                     goto error;
1639                 }
1640                 self->input_buffer_size = self->avail_in_real;
1641             }
1642 
1643             /* Copy tail */
1644             memcpy(self->input_buffer, self->zst.next_in, self->avail_in_real);
1645             self->zst.next_in = self->input_buffer;
1646         }
1647     }
1648     return result;
1649 
1650 error:
1651     Py_XDECREF(result);
1652     return NULL;
1653 }
1654 
1655 /*[clinic input]
1656 zlib.ZlibDecompressor.decompress
1657 
1658     data: Py_buffer
1659     max_length: Py_ssize_t=-1
1660 
1661 Decompress *data*, returning uncompressed data as bytes.
1662 
1663 If *max_length* is nonnegative, returns at most *max_length* bytes of
1664 decompressed data. If this limit is reached and further output can be
1665 produced, *self.needs_input* will be set to ``False``. In this case, the next
1666 call to *decompress()* may provide *data* as b'' to obtain more of the output.
1667 
1668 If all of the input data was decompressed and returned (either because this
1669 was less than *max_length* bytes, or because *max_length* was negative),
1670 *self.needs_input* will be set to True.
1671 
1672 Attempting to decompress data after the end of stream is reached raises an
1673 EOFError.  Any data found after the end of the stream is ignored and saved in
1674 the unused_data attribute.
1675 [clinic start generated code]*/
1676 
1677 static PyObject *
zlib_ZlibDecompressor_decompress_impl(ZlibDecompressor * self,Py_buffer * data,Py_ssize_t max_length)1678 zlib_ZlibDecompressor_decompress_impl(ZlibDecompressor *self,
1679                                       Py_buffer *data, Py_ssize_t max_length)
1680 /*[clinic end generated code: output=990d32787b775f85 input=0b29d99715250b96]*/
1681 
1682 {
1683     PyObject *result = NULL;
1684 
1685     ENTER_ZLIB(self);
1686     if (self->eof) {
1687         PyErr_SetString(PyExc_EOFError, "End of stream already reached");
1688     }
1689     else {
1690         result = decompress(self, data->buf, data->len, max_length);
1691     }
1692     LEAVE_ZLIB(self);
1693     return result;
1694 }
1695 
1696 PyDoc_STRVAR(ZlibDecompressor__new____doc__,
1697 "_ZlibDecompressor(wbits=15, zdict=b\'\')\n"
1698 "--\n"
1699 "\n"
1700 "Create a decompressor object for decompressing data incrementally.\n"
1701 "\n"
1702 "  wbits = 15\n"
1703 "  zdict\n"
1704 "     The predefined compression dictionary. This is a sequence of bytes\n"
1705 "     (such as a bytes object) containing subsequences that are expected\n"
1706 "     to occur frequently in the data that is to be compressed. Those\n"
1707 "     subsequences that are expected to be most common should come at the\n"
1708 "     end of the dictionary. This must be the same dictionary as used by the\n"
1709 "     compressor that produced the input data.\n"
1710 "\n");
1711 
1712 static PyObject *
ZlibDecompressor__new__(PyTypeObject * cls,PyObject * args,PyObject * kwargs)1713 ZlibDecompressor__new__(PyTypeObject *cls,
1714                         PyObject *args,
1715                         PyObject *kwargs)
1716 {
1717     static char *keywords[] = {"wbits", "zdict", NULL};
1718     static const char * const format = "|iO:_ZlibDecompressor";
1719     int wbits = MAX_WBITS;
1720     PyObject *zdict = NULL;
1721     zlibstate *state = PyType_GetModuleState(cls);
1722 
1723     if (!PyArg_ParseTupleAndKeywords(
1724             args, kwargs, format, keywords, &wbits, &zdict)) {
1725         return NULL;
1726     }
1727     ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls);
1728     if (self == NULL) {
1729         return NULL;
1730     }
1731     self->eof = 0;
1732     self->needs_input = 1;
1733     self->avail_in_real = 0;
1734     self->input_buffer = NULL;
1735     self->input_buffer_size = 0;
1736     self->zdict = Py_XNewRef(zdict);
1737     self->zst.opaque = NULL;
1738     self->zst.zalloc = PyZlib_Malloc;
1739     self->zst.zfree = PyZlib_Free;
1740     self->zst.next_in = NULL;
1741     self->zst.avail_in = 0;
1742     self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
1743     if (self->unused_data == NULL) {
1744         Py_CLEAR(self);
1745         return NULL;
1746     }
1747     self->lock = PyThread_allocate_lock();
1748     if (self->lock == NULL) {
1749         Py_DECREF(self);
1750         PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
1751         return NULL;
1752     }
1753     int err = inflateInit2(&(self->zst), wbits);
1754     switch (err) {
1755         case Z_OK:
1756         self->is_initialised = 1;
1757         if (self->zdict != NULL && wbits < 0) {
1758             if (set_inflate_zdict_ZlibDecompressor(state, self) < 0) {
1759                 Py_DECREF(self);
1760                 return NULL;
1761             }
1762         }
1763         return (PyObject *)self;
1764     case Z_STREAM_ERROR:
1765         Py_DECREF(self);
1766         PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
1767         return NULL;
1768     case Z_MEM_ERROR:
1769         Py_DECREF(self);
1770         PyErr_SetString(PyExc_MemoryError,
1771                         "Can't allocate memory for decompression object");
1772         return NULL;
1773     default:
1774         zlib_error(state, self->zst, err, "while creating decompression object");
1775         Py_DECREF(self);
1776         return NULL;
1777     }
1778 }
1779 
1780 #include "clinic/zlibmodule.c.h"
1781 
1782 static PyMethodDef comp_methods[] =
1783 {
1784     ZLIB_COMPRESS_COMPRESS_METHODDEF
1785     ZLIB_COMPRESS_FLUSH_METHODDEF
1786     ZLIB_COMPRESS_COPY_METHODDEF
1787     ZLIB_COMPRESS___COPY___METHODDEF
1788     ZLIB_COMPRESS___DEEPCOPY___METHODDEF
1789     {NULL, NULL}
1790 };
1791 
1792 static PyMethodDef Decomp_methods[] =
1793 {
1794     ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
1795     ZLIB_DECOMPRESS_FLUSH_METHODDEF
1796     ZLIB_DECOMPRESS_COPY_METHODDEF
1797     ZLIB_DECOMPRESS___COPY___METHODDEF
1798     ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
1799     {NULL, NULL}
1800 };
1801 
1802 static PyMethodDef ZlibDecompressor_methods[] = {
1803     ZLIB_ZLIBDECOMPRESSOR_DECOMPRESS_METHODDEF
1804     {NULL}
1805 };
1806 
1807 #define COMP_OFF(x) offsetof(compobject, x)
1808 static PyMemberDef Decomp_members[] = {
1809     {"unused_data",     _Py_T_OBJECT, COMP_OFF(unused_data), Py_READONLY},
1810     {"unconsumed_tail", _Py_T_OBJECT, COMP_OFF(unconsumed_tail), Py_READONLY},
1811     {"eof",             Py_T_BOOL,   COMP_OFF(eof), Py_READONLY},
1812     {NULL},
1813 };
1814 
1815 PyDoc_STRVAR(ZlibDecompressor_eof__doc__,
1816 "True if the end-of-stream marker has been reached.");
1817 
1818 PyDoc_STRVAR(ZlibDecompressor_unused_data__doc__,
1819 "Data found after the end of the compressed stream.");
1820 
1821 PyDoc_STRVAR(ZlibDecompressor_needs_input_doc,
1822 "True if more input is needed before more decompressed data can be produced.");
1823 
1824 static PyMemberDef ZlibDecompressor_members[] = {
1825     {"eof", Py_T_BOOL, offsetof(ZlibDecompressor, eof),
1826      Py_READONLY, ZlibDecompressor_eof__doc__},
1827     {"unused_data", Py_T_OBJECT_EX, offsetof(ZlibDecompressor, unused_data),
1828      Py_READONLY, ZlibDecompressor_unused_data__doc__},
1829     {"needs_input", Py_T_BOOL, offsetof(ZlibDecompressor, needs_input), Py_READONLY,
1830      ZlibDecompressor_needs_input_doc},
1831     {NULL},
1832 };
1833 
1834 
1835 /*[clinic input]
1836 zlib.adler32
1837 
1838     data: Py_buffer
1839     value: unsigned_int(bitwise=True) = 1
1840         Starting value of the checksum.
1841     /
1842 
1843 Compute an Adler-32 checksum of data.
1844 
1845 The returned checksum is an integer.
1846 [clinic start generated code]*/
1847 
1848 static PyObject *
zlib_adler32_impl(PyObject * module,Py_buffer * data,unsigned int value)1849 zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1850 /*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
1851 {
1852     /* Releasing the GIL for very small buffers is inefficient
1853        and may lower performance */
1854     if (data->len > 1024*5) {
1855         unsigned char *buf = data->buf;
1856         Py_ssize_t len = data->len;
1857 
1858         Py_BEGIN_ALLOW_THREADS
1859         /* Avoid truncation of length for very large buffers. adler32() takes
1860            length as an unsigned int, which may be narrower than Py_ssize_t. */
1861         while ((size_t)len > UINT_MAX) {
1862             value = adler32(value, buf, UINT_MAX);
1863             buf += (size_t) UINT_MAX;
1864             len -= (size_t) UINT_MAX;
1865         }
1866         value = adler32(value, buf, (unsigned int)len);
1867         Py_END_ALLOW_THREADS
1868     } else {
1869         value = adler32(value, data->buf, (unsigned int)data->len);
1870     }
1871     return PyLong_FromUnsignedLong(value & 0xffffffffU);
1872 }
1873 
1874 /*[clinic input]
1875 zlib.crc32 -> unsigned_int
1876 
1877     data: Py_buffer
1878     value: unsigned_int(bitwise=True) = 0
1879         Starting value of the checksum.
1880     /
1881 
1882 Compute a CRC-32 checksum of data.
1883 
1884 The returned checksum is an integer.
1885 [clinic start generated code]*/
1886 
1887 static unsigned int
zlib_crc32_impl(PyObject * module,Py_buffer * data,unsigned int value)1888 zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1889 /*[clinic end generated code: output=b217562e4fe6d6a6 input=1229cb2fb5ea948a]*/
1890 {
1891     /* Releasing the GIL for very small buffers is inefficient
1892        and may lower performance */
1893     if (data->len > 1024*5) {
1894         unsigned char *buf = data->buf;
1895         Py_ssize_t len = data->len;
1896 
1897         Py_BEGIN_ALLOW_THREADS
1898         /* Avoid truncation of length for very large buffers. crc32() takes
1899            length as an unsigned int, which may be narrower than Py_ssize_t.
1900            We further limit size due to bugs in Apple's macOS zlib.
1901            See https://github.com/python/cpython/issues/105967.
1902          */
1903 #define ZLIB_CRC_CHUNK_SIZE 0x40000000
1904 #if ZLIB_CRC_CHUNK_SIZE > INT_MAX
1905 # error "unsupported less than 32-bit platform?"
1906 #endif
1907         while ((size_t)len > ZLIB_CRC_CHUNK_SIZE) {
1908             value = crc32(value, buf, ZLIB_CRC_CHUNK_SIZE);
1909             buf += (size_t) ZLIB_CRC_CHUNK_SIZE;
1910             len -= (size_t) ZLIB_CRC_CHUNK_SIZE;
1911         }
1912 #undef ZLIB_CRC_CHUNK_SIZE
1913         value = crc32(value, buf, (unsigned int)len);
1914         Py_END_ALLOW_THREADS
1915     } else {
1916         value = crc32(value, data->buf, (unsigned int)data->len);
1917     }
1918     return value;
1919 }
1920 
1921 
1922 static PyMethodDef zlib_methods[] =
1923 {
1924     ZLIB_ADLER32_METHODDEF
1925     ZLIB_COMPRESS_METHODDEF
1926     ZLIB_COMPRESSOBJ_METHODDEF
1927     ZLIB_CRC32_METHODDEF
1928     ZLIB_DECOMPRESS_METHODDEF
1929     ZLIB_DECOMPRESSOBJ_METHODDEF
1930     {NULL, NULL}
1931 };
1932 
1933 static PyType_Slot Comptype_slots[] = {
1934     {Py_tp_dealloc, Comp_dealloc},
1935     {Py_tp_methods, comp_methods},
1936     {0, 0},
1937 };
1938 
1939 static PyType_Spec Comptype_spec = {
1940     .name = "zlib.Compress",
1941     .basicsize = sizeof(compobject),
1942     .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1943     .slots= Comptype_slots,
1944 };
1945 
1946 static PyType_Slot Decomptype_slots[] = {
1947     {Py_tp_dealloc, Decomp_dealloc},
1948     {Py_tp_methods, Decomp_methods},
1949     {Py_tp_members, Decomp_members},
1950     {0, 0},
1951 };
1952 
1953 static PyType_Spec Decomptype_spec = {
1954     .name = "zlib.Decompress",
1955     .basicsize = sizeof(compobject),
1956     .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1957     .slots = Decomptype_slots,
1958 };
1959 
1960 static PyType_Slot ZlibDecompressor_type_slots[] = {
1961     {Py_tp_dealloc, ZlibDecompressor_dealloc},
1962     {Py_tp_members, ZlibDecompressor_members},
1963     {Py_tp_new, ZlibDecompressor__new__},
1964     {Py_tp_doc, (char *)ZlibDecompressor__new____doc__},
1965     {Py_tp_methods, ZlibDecompressor_methods},
1966     {0, 0},
1967 };
1968 
1969 static PyType_Spec ZlibDecompressor_type_spec = {
1970     .name = "zlib._ZlibDecompressor",
1971     .basicsize = sizeof(ZlibDecompressor),
1972     // Calling PyType_GetModuleState() on a subclass is not safe.
1973     // ZlibDecompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
1974     // which prevents to create a subclass.
1975     // So calling PyType_GetModuleState() in this file is always safe.
1976     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
1977     .slots = ZlibDecompressor_type_slots,
1978 };
1979 PyDoc_STRVAR(zlib_module_documentation,
1980 "The functions in this module allow compression and decompression using the\n"
1981 "zlib library, which is based on GNU zip.\n"
1982 "\n"
1983 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1984 "compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
1985 "compressobj([level[, ...]]) -- Return a compressor object.\n"
1986 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1987 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1988 "decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
1989 "\n"
1990 "'wbits' is window buffer size and container format.\n"
1991 "Compressor objects support compress() and flush() methods; decompressor\n"
1992 "objects support decompress() and flush().");
1993 
1994 static int
zlib_clear(PyObject * mod)1995 zlib_clear(PyObject *mod)
1996 {
1997     zlibstate *state = get_zlib_state(mod);
1998     Py_CLEAR(state->Comptype);
1999     Py_CLEAR(state->Decomptype);
2000     Py_CLEAR(state->ZlibDecompressorType);
2001     Py_CLEAR(state->ZlibError);
2002     return 0;
2003 }
2004 
2005 static int
zlib_traverse(PyObject * mod,visitproc visit,void * arg)2006 zlib_traverse(PyObject *mod, visitproc visit, void *arg)
2007 {
2008     zlibstate *state = get_zlib_state(mod);
2009     Py_VISIT(state->Comptype);
2010     Py_VISIT(state->Decomptype);
2011     Py_VISIT(state->ZlibDecompressorType);
2012     Py_VISIT(state->ZlibError);
2013     return 0;
2014 }
2015 
2016 static void
zlib_free(void * mod)2017 zlib_free(void *mod)
2018 {
2019     zlib_clear((PyObject *)mod);
2020 }
2021 
2022 static int
zlib_exec(PyObject * mod)2023 zlib_exec(PyObject *mod)
2024 {
2025     zlibstate *state = get_zlib_state(mod);
2026 
2027     state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
2028         mod, &Comptype_spec, NULL);
2029     if (state->Comptype == NULL) {
2030         return -1;
2031     }
2032 
2033     state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
2034         mod, &Decomptype_spec, NULL);
2035     if (state->Decomptype == NULL) {
2036         return -1;
2037     }
2038 
2039     state->ZlibDecompressorType = (PyTypeObject *)PyType_FromModuleAndSpec(
2040         mod, &ZlibDecompressor_type_spec, NULL);
2041     if (state->ZlibDecompressorType == NULL) {
2042         return -1;
2043     }
2044 
2045     state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
2046     if (PyModule_AddObjectRef(mod, "error", state->ZlibError) < 0) {
2047         return -1;
2048     }
2049     if (PyModule_AddObjectRef(mod, "_ZlibDecompressor",
2050                               (PyObject *)state->ZlibDecompressorType) < 0) {
2051         return -1;
2052     }
2053 
2054 #define ZLIB_ADD_INT_MACRO(c)                           \
2055     do {                                                \
2056         if ((PyModule_AddIntConstant(mod, #c, c)) < 0) {  \
2057             return -1;                                  \
2058         }                                               \
2059     } while(0)
2060 
2061     ZLIB_ADD_INT_MACRO(MAX_WBITS);
2062     ZLIB_ADD_INT_MACRO(DEFLATED);
2063     ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
2064     ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
2065     // compression levels
2066     ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
2067     ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
2068     ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
2069     ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
2070     // compression strategies
2071     ZLIB_ADD_INT_MACRO(Z_FILTERED);
2072     ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
2073 #ifdef Z_RLE // 1.2.0.1
2074     ZLIB_ADD_INT_MACRO(Z_RLE);
2075 #endif
2076 #ifdef Z_FIXED // 1.2.2.2
2077     ZLIB_ADD_INT_MACRO(Z_FIXED);
2078 #endif
2079     ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
2080     // allowed flush values
2081     ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
2082     ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
2083     ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
2084     ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
2085     ZLIB_ADD_INT_MACRO(Z_FINISH);
2086 #ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
2087     ZLIB_ADD_INT_MACRO(Z_BLOCK);
2088 #endif
2089 #ifdef Z_TREES // 1.2.3.4, only for inflate
2090     ZLIB_ADD_INT_MACRO(Z_TREES);
2091 #endif
2092     if (PyModule_Add(mod, "ZLIB_VERSION",
2093                      PyUnicode_FromString(ZLIB_VERSION)) < 0) {
2094         return -1;
2095     }
2096     if (PyModule_Add(mod, "ZLIB_RUNTIME_VERSION",
2097                      PyUnicode_FromString(zlibVersion())) < 0) {
2098         return -1;
2099     }
2100     if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
2101         return -1;
2102     }
2103     return 0;
2104 }
2105 
2106 static PyModuleDef_Slot zlib_slots[] = {
2107     {Py_mod_exec, zlib_exec},
2108     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
2109     {Py_mod_gil, Py_MOD_GIL_NOT_USED},
2110     {0, NULL}
2111 };
2112 
2113 static struct PyModuleDef zlibmodule = {
2114     PyModuleDef_HEAD_INIT,
2115     .m_name = "zlib",
2116     .m_doc = zlib_module_documentation,
2117     .m_size = sizeof(zlibstate),
2118     .m_methods = zlib_methods,
2119     .m_slots = zlib_slots,
2120     .m_traverse = zlib_traverse,
2121     .m_clear = zlib_clear,
2122     .m_free = zlib_free,
2123 };
2124 
2125 PyMODINIT_FUNC
PyInit_zlib(void)2126 PyInit_zlib(void)
2127 {
2128     return PyModuleDef_Init(&zlibmodule);
2129 }
2130