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