LCOV - code coverage report
Current view: top level - Modules - zlibmodule.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 535 706 75.8 %
Date: 2022-07-07 18:19:46 Functions: 41 41 100.0 %

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

Generated by: LCOV version 1.14