xref: /third_party/python/Modules/zlibmodule.c (revision 7db96d56)
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 */
21static inline Py_ssize_t
22OutputBuffer_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 */
35static inline Py_ssize_t
36OutputBuffer_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
47static inline Py_ssize_t
48OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
49{
50    return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
51}
52
53static inline PyObject *
54OutputBuffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
55{
56    return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
57}
58
59static inline void
60OutputBuffer_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  */
80typedef 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 */
89static inline Py_ssize_t
90OutputBuffer_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 */
112static inline Py_ssize_t
113OutputBuffer_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 */
151static inline PyObject *
152OutputBuffer_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
159static inline void
160OutputBuffer_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
189static PyModuleDef zlibmodule;
190
191typedef struct {
192    PyTypeObject *Comptype;
193    PyTypeObject *Decomptype;
194    PyObject *ZlibError;
195} zlibstate;
196
197static inline zlibstate*
198get_zlib_state(PyObject *module)
199{
200    void *state = PyModule_GetState(module);
201    assert(state != NULL);
202    return (zlibstate *)state;
203}
204
205typedef 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
217static void
218zlib_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]
247module zlib
248class zlib.Compress "compobject *" "&Comptype"
249class zlib.Decompress "compobject *" "&Decomptype"
250[clinic start generated code]*/
251/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
252
253static compobject *
254newcompobject(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
282static void*
283PyZlib_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
292static void
293PyZlib_Free(voidpf ctx, void *ptr)
294{
295    PyMem_RawFree(ptr);
296}
297
298static void
299arrange_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]
306zlib.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
316Returns a bytes object containing compressed data.
317[clinic start generated code]*/
318
319static PyObject *
320zlib_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    _BlocksOutputBuffer buffer = {.list = NULL};
327
328    zlibstate *state = get_zlib_state(module);
329
330    Byte *ibuf = data->buf;
331    Py_ssize_t ibuflen = data->len;
332
333    if (OutputBuffer_InitAndGrow(&buffer, -1, &zst.next_out, &zst.avail_out) < 0) {
334        goto error;
335    }
336
337    zst.opaque = NULL;
338    zst.zalloc = PyZlib_Malloc;
339    zst.zfree = PyZlib_Free;
340    zst.next_in = ibuf;
341    int err = deflateInit2(&zst, level, DEFLATED, wbits, DEF_MEM_LEVEL,
342                           Z_DEFAULT_STRATEGY);
343
344    switch (err) {
345    case Z_OK:
346        break;
347    case Z_MEM_ERROR:
348        PyErr_SetString(PyExc_MemoryError,
349                        "Out of memory while compressing data");
350        goto error;
351    case Z_STREAM_ERROR:
352        PyErr_SetString(state->ZlibError, "Bad compression level");
353        goto error;
354    default:
355        deflateEnd(&zst);
356        zlib_error(state, zst, err, "while compressing data");
357        goto error;
358    }
359
360    do {
361        arrange_input_buffer(&zst, &ibuflen);
362        flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
363
364        do {
365            if (zst.avail_out == 0) {
366                if (OutputBuffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
367                    deflateEnd(&zst);
368                    goto error;
369                }
370            }
371
372            Py_BEGIN_ALLOW_THREADS
373            err = deflate(&zst, flush);
374            Py_END_ALLOW_THREADS
375
376            if (err == Z_STREAM_ERROR) {
377                deflateEnd(&zst);
378                zlib_error(state, zst, err, "while compressing data");
379                goto error;
380            }
381
382        } while (zst.avail_out == 0);
383        assert(zst.avail_in == 0);
384
385    } while (flush != Z_FINISH);
386    assert(err == Z_STREAM_END);
387
388    err = deflateEnd(&zst);
389    if (err == Z_OK) {
390        RetVal = OutputBuffer_Finish(&buffer, zst.avail_out);
391        if (RetVal == NULL) {
392            goto error;
393        }
394        return RetVal;
395    }
396    else
397        zlib_error(state, zst, err, "while finishing compression");
398 error:
399    OutputBuffer_OnError(&buffer);
400    return NULL;
401}
402
403/*[clinic input]
404zlib.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
414Returns a bytes object containing the uncompressed data.
415[clinic start generated code]*/
416
417static PyObject *
418zlib_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    _BlocksOutputBuffer buffer = {.list = NULL};
428    _Uint32Window window;  // output buffer's UINT32_MAX sliding window
429
430    zlibstate *state = get_zlib_state(module);
431
432    if (bufsize < 0) {
433        PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
434        return NULL;
435    } else if (bufsize == 0) {
436        bufsize = 1;
437    }
438
439    if (OutputBuffer_WindowInitWithSize(&buffer, &window, bufsize,
440                                        &zst.next_out, &zst.avail_out) < 0) {
441        goto error;
442    }
443
444    ibuf = data->buf;
445    ibuflen = data->len;
446
447    zst.opaque = NULL;
448    zst.zalloc = PyZlib_Malloc;
449    zst.zfree = PyZlib_Free;
450    zst.avail_in = 0;
451    zst.next_in = ibuf;
452    err = inflateInit2(&zst, wbits);
453
454    switch (err) {
455    case Z_OK:
456        break;
457    case Z_MEM_ERROR:
458        PyErr_SetString(PyExc_MemoryError,
459                        "Out of memory while decompressing data");
460        goto error;
461    default:
462        inflateEnd(&zst);
463        zlib_error(state, zst, err, "while preparing to decompress data");
464        goto error;
465    }
466
467    do {
468        arrange_input_buffer(&zst, &ibuflen);
469        flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
470
471        do {
472            if (zst.avail_out == 0) {
473                if (OutputBuffer_WindowGrow(&buffer, &window,
474                                            &zst.next_out, &zst.avail_out) < 0) {
475                    inflateEnd(&zst);
476                    goto error;
477                }
478            }
479
480            Py_BEGIN_ALLOW_THREADS
481            err = inflate(&zst, flush);
482            Py_END_ALLOW_THREADS
483
484            switch (err) {
485            case Z_OK:            /* fall through */
486            case Z_BUF_ERROR:     /* fall through */
487            case Z_STREAM_END:
488                break;
489            case Z_MEM_ERROR:
490                inflateEnd(&zst);
491                PyErr_SetString(PyExc_MemoryError,
492                                "Out of memory while decompressing data");
493                goto error;
494            default:
495                inflateEnd(&zst);
496                zlib_error(state, zst, err, "while decompressing data");
497                goto error;
498            }
499
500        } while (zst.avail_out == 0);
501
502    } while (err != Z_STREAM_END && ibuflen != 0);
503
504
505    if (err != Z_STREAM_END) {
506        inflateEnd(&zst);
507        zlib_error(state, zst, err, "while decompressing data");
508        goto error;
509    }
510
511    err = inflateEnd(&zst);
512    if (err != Z_OK) {
513        zlib_error(state, zst, err, "while finishing decompression");
514        goto error;
515    }
516
517    RetVal = OutputBuffer_WindowFinish(&buffer, &window, zst.avail_out);
518    if (RetVal != NULL) {
519        return RetVal;
520    }
521
522 error:
523    OutputBuffer_WindowOnError(&buffer, &window);
524    return NULL;
525}
526
527/*[clinic input]
528zlib.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
552Return a compressor object.
553[clinic start generated code]*/
554
555static PyObject *
556zlib_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    zlibstate *state = get_zlib_state(module);
561    if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
562        PyErr_SetString(PyExc_OverflowError,
563                        "zdict length does not fit in an unsigned int");
564        return NULL;
565    }
566
567    compobject *self = newcompobject(state->Comptype);
568    if (self == NULL)
569        goto error;
570    self->zst.opaque = NULL;
571    self->zst.zalloc = PyZlib_Malloc;
572    self->zst.zfree = PyZlib_Free;
573    self->zst.next_in = NULL;
574    self->zst.avail_in = 0;
575    int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
576    switch (err) {
577    case Z_OK:
578        self->is_initialised = 1;
579        if (zdict->buf == NULL) {
580            goto success;
581        } else {
582            err = deflateSetDictionary(&self->zst,
583                                       zdict->buf, (unsigned int)zdict->len);
584            switch (err) {
585            case Z_OK:
586                goto success;
587            case Z_STREAM_ERROR:
588                PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
589                goto error;
590            default:
591                PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
592                goto error;
593            }
594       }
595    case Z_MEM_ERROR:
596        PyErr_SetString(PyExc_MemoryError,
597                        "Can't allocate memory for compression object");
598        goto error;
599    case Z_STREAM_ERROR:
600        PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
601        goto error;
602    default:
603        zlib_error(state, self->zst, err, "while creating compression object");
604        goto error;
605    }
606
607 error:
608    Py_CLEAR(self);
609 success:
610    return (PyObject *)self;
611}
612
613static int
614set_inflate_zdict(zlibstate *state, compobject *self)
615{
616    Py_buffer zdict_buf;
617    if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
618        return -1;
619    }
620    if ((size_t)zdict_buf.len > UINT_MAX) {
621        PyErr_SetString(PyExc_OverflowError,
622                        "zdict length does not fit in an unsigned int");
623        PyBuffer_Release(&zdict_buf);
624        return -1;
625    }
626    int err;
627    err = inflateSetDictionary(&self->zst,
628                               zdict_buf.buf, (unsigned int)zdict_buf.len);
629    PyBuffer_Release(&zdict_buf);
630    if (err != Z_OK) {
631        zlib_error(state, self->zst, err, "while setting zdict");
632        return -1;
633    }
634    return 0;
635}
636
637/*[clinic input]
638zlib.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
646Return a decompressor object.
647[clinic start generated code]*/
648
649static PyObject *
650zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
651/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
652{
653    zlibstate *state = get_zlib_state(module);
654
655    if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
656        PyErr_SetString(PyExc_TypeError,
657                        "zdict argument must support the buffer protocol");
658        return NULL;
659    }
660
661    compobject *self = newcompobject(state->Decomptype);
662    if (self == NULL)
663        return NULL;
664    self->zst.opaque = NULL;
665    self->zst.zalloc = PyZlib_Malloc;
666    self->zst.zfree = PyZlib_Free;
667    self->zst.next_in = NULL;
668    self->zst.avail_in = 0;
669    if (zdict != NULL) {
670        Py_INCREF(zdict);
671        self->zdict = zdict;
672    }
673    int err = inflateInit2(&self->zst, wbits);
674    switch (err) {
675    case Z_OK:
676        self->is_initialised = 1;
677        if (self->zdict != NULL && wbits < 0) {
678#ifdef AT_LEAST_ZLIB_1_2_2_1
679            if (set_inflate_zdict(state, self) < 0) {
680                Py_DECREF(self);
681                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        return (PyObject *)self;
692    case Z_STREAM_ERROR:
693        Py_DECREF(self);
694        PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
695        return NULL;
696    case Z_MEM_ERROR:
697        Py_DECREF(self);
698        PyErr_SetString(PyExc_MemoryError,
699                        "Can't allocate memory for decompression object");
700        return NULL;
701    default:
702        zlib_error(state, self->zst, err, "while creating decompression object");
703        Py_DECREF(self);
704        return NULL;
705    }
706}
707
708static void
709Dealloc(compobject *self)
710{
711    PyObject *type = (PyObject *)Py_TYPE(self);
712    PyThread_free_lock(self->lock);
713    Py_XDECREF(self->unused_data);
714    Py_XDECREF(self->unconsumed_tail);
715    Py_XDECREF(self->zdict);
716    PyObject_Free(self);
717    Py_DECREF(type);
718}
719
720static void
721Comp_dealloc(compobject *self)
722{
723    if (self->is_initialised)
724        deflateEnd(&self->zst);
725    Dealloc(self);
726}
727
728static void
729Decomp_dealloc(compobject *self)
730{
731    if (self->is_initialised)
732        inflateEnd(&self->zst);
733    Dealloc(self);
734}
735
736/*[clinic input]
737zlib.Compress.compress
738
739    cls: defining_class
740    data: Py_buffer
741        Binary data to be compressed.
742    /
743
744Returns a bytes object containing compressed data.
745
746After calling this function, some of the input data may still
747be stored in internal buffers for later processing.
748Call the flush() method to clear these buffers.
749[clinic start generated code]*/
750
751static PyObject *
752zlib_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    _BlocksOutputBuffer buffer = {.list = NULL};
759    zlibstate *state = PyType_GetModuleState(cls);
760
761    ENTER_ZLIB(self);
762
763    self->zst.next_in = data->buf;
764    Py_ssize_t ibuflen = data->len;
765
766    if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
767        goto error;
768    }
769
770    do {
771        arrange_input_buffer(&self->zst, &ibuflen);
772
773        do {
774            if (self->zst.avail_out == 0) {
775                if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
776                    goto error;
777                }
778            }
779
780            Py_BEGIN_ALLOW_THREADS
781            err = deflate(&self->zst, Z_NO_FLUSH);
782            Py_END_ALLOW_THREADS
783
784            if (err == Z_STREAM_ERROR) {
785                zlib_error(state, self->zst, err, "while compressing data");
786                goto error;
787            }
788
789        } while (self->zst.avail_out == 0);
790        assert(self->zst.avail_in == 0);
791
792    } while (ibuflen != 0);
793
794    RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
795    if (RetVal != NULL) {
796        goto success;
797    }
798
799 error:
800    OutputBuffer_OnError(&buffer);
801    RetVal = NULL;
802 success:
803    LEAVE_ZLIB(self);
804    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. */
809static int
810save_unconsumed_input(compobject *self, Py_buffer *data, int err)
811{
812    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        if (self->zst.avail_in > 0) {
816            Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
817            Py_ssize_t new_size, left_size;
818            PyObject *new_data;
819            left_size = (Byte *)data->buf + data->len - self->zst.next_in;
820            if (left_size > (PY_SSIZE_T_MAX - old_size)) {
821                PyErr_NoMemory();
822                return -1;
823            }
824            new_size = old_size + left_size;
825            new_data = PyBytes_FromStringAndSize(NULL, new_size);
826            if (new_data == NULL)
827                return -1;
828            memcpy(PyBytes_AS_STRING(new_data),
829                      PyBytes_AS_STRING(self->unused_data), old_size);
830            memcpy(PyBytes_AS_STRING(new_data) + old_size,
831                      self->zst.next_in, left_size);
832            Py_SETREF(self->unused_data, new_data);
833            self->zst.avail_in = 0;
834        }
835    }
836
837    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        Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
842        PyObject *new_data = PyBytes_FromStringAndSize(
843                (char *)self->zst.next_in, left_size);
844        if (new_data == NULL)
845            return -1;
846        Py_SETREF(self->unconsumed_tail, new_data);
847    }
848
849    return 0;
850}
851
852/*[clinic input]
853zlib.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
864Return a bytes object containing the decompressed version of the data.
865
866After calling this function, some of the input data may still be stored in
867internal buffers for later processing.
868Call the flush() method to clear these buffers.
869[clinic start generated code]*/
870
871static PyObject *
872zlib_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    int err = Z_OK;
877    Py_ssize_t ibuflen;
878    PyObject *RetVal;
879    _BlocksOutputBuffer buffer = {.list = NULL};
880
881    PyObject *module = PyType_GetModule(cls);
882    if (module == NULL)
883        return NULL;
884
885    zlibstate *state = get_zlib_state(module);
886    if (max_length < 0) {
887        PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
888        return NULL;
889    } else if (max_length == 0) {
890        max_length = -1;
891    }
892
893    ENTER_ZLIB(self);
894
895    self->zst.next_in = data->buf;
896    ibuflen = data->len;
897
898    if (OutputBuffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) {
899        goto abort;
900    }
901
902    do {
903        arrange_input_buffer(&self->zst, &ibuflen);
904
905        do {
906            if (self->zst.avail_out == 0) {
907                if (OutputBuffer_GetDataSize(&buffer, self->zst.avail_out) == max_length) {
908                    goto save;
909                }
910                if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
911                    goto abort;
912                }
913            }
914
915            Py_BEGIN_ALLOW_THREADS
916            err = inflate(&self->zst, Z_SYNC_FLUSH);
917            Py_END_ALLOW_THREADS
918
919            switch (err) {
920            case Z_OK:            /* fall through */
921            case Z_BUF_ERROR:     /* fall through */
922            case Z_STREAM_END:
923                break;
924            default:
925                if (err == Z_NEED_DICT && self->zdict != NULL) {
926                    if (set_inflate_zdict(state, self) < 0) {
927                        goto abort;
928                    }
929                    else
930                        break;
931                }
932                goto save;
933            }
934
935        } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
936
937    } while (err != Z_STREAM_END && ibuflen != 0);
938
939 save:
940    if (save_unconsumed_input(self, data, err) < 0)
941        goto abort;
942
943    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        self->eof = 1;
947    } 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        zlib_error(state, self->zst, err, "while decompressing data");
953        goto abort;
954    }
955
956    RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
957    if (RetVal != NULL) {
958        goto success;
959    }
960
961 abort:
962    OutputBuffer_OnError(&buffer);
963    RetVal = NULL;
964 success:
965    LEAVE_ZLIB(self);
966    return RetVal;
967}
968
969/*[clinic input]
970zlib.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
980Return a bytes object containing any remaining compressed data.
981[clinic start generated code]*/
982
983static PyObject *
984zlib_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    _BlocksOutputBuffer buffer = {.list = NULL};
990
991    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    if (mode == Z_NO_FLUSH) {
995        return PyBytes_FromStringAndSize(NULL, 0);
996    }
997
998    ENTER_ZLIB(self);
999
1000    self->zst.avail_in = 0;
1001
1002    if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
1003        goto error;
1004    }
1005
1006    do {
1007        if (self->zst.avail_out == 0) {
1008            if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
1009                goto error;
1010            }
1011        }
1012
1013        Py_BEGIN_ALLOW_THREADS
1014        err = deflate(&self->zst, mode);
1015        Py_END_ALLOW_THREADS
1016
1017        if (err == Z_STREAM_ERROR) {
1018            zlib_error(state, self->zst, err, "while flushing");
1019            goto error;
1020        }
1021    } while (self->zst.avail_out == 0);
1022    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    if (err == Z_STREAM_END && mode == Z_FINISH) {
1028        err = deflateEnd(&self->zst);
1029        if (err != Z_OK) {
1030            zlib_error(state, self->zst, err, "while finishing compression");
1031            goto error;
1032        }
1033        else
1034            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    } else if (err != Z_OK && err != Z_BUF_ERROR) {
1041        zlib_error(state, self->zst, err, "while flushing");
1042        goto error;
1043    }
1044
1045    RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
1046    if (RetVal != NULL) {
1047        goto success;
1048    }
1049
1050error:
1051    OutputBuffer_OnError(&buffer);
1052    RetVal = NULL;
1053success:
1054    LEAVE_ZLIB(self);
1055    return RetVal;
1056}
1057
1058#ifdef HAVE_ZLIB_COPY
1059
1060/*[clinic input]
1061zlib.Compress.copy
1062
1063    cls: defining_class
1064
1065Return a copy of the compression object.
1066[clinic start generated code]*/
1067
1068static PyObject *
1069zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
1070/*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
1071{
1072    zlibstate *state = PyType_GetModuleState(cls);
1073
1074    compobject *retval = newcompobject(state->Comptype);
1075    if (!retval) return NULL;
1076
1077    /* Copy the zstream state
1078     * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1079     */
1080    ENTER_ZLIB(self);
1081    int err = deflateCopy(&retval->zst, &self->zst);
1082    switch (err) {
1083    case Z_OK:
1084        break;
1085    case Z_STREAM_ERROR:
1086        PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1087        goto error;
1088    case Z_MEM_ERROR:
1089        PyErr_SetString(PyExc_MemoryError,
1090                        "Can't allocate memory for compression object");
1091        goto error;
1092    default:
1093        zlib_error(state, self->zst, err, "while copying compression object");
1094        goto error;
1095    }
1096    Py_INCREF(self->unused_data);
1097    Py_XSETREF(retval->unused_data, self->unused_data);
1098    Py_INCREF(self->unconsumed_tail);
1099    Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
1100    Py_XINCREF(self->zdict);
1101    Py_XSETREF(retval->zdict, self->zdict);
1102    retval->eof = self->eof;
1103
1104    /* Mark it as being initialized */
1105    retval->is_initialised = 1;
1106
1107    LEAVE_ZLIB(self);
1108    return (PyObject *)retval;
1109
1110error:
1111    LEAVE_ZLIB(self);
1112    Py_XDECREF(retval);
1113    return NULL;
1114}
1115
1116/*[clinic input]
1117zlib.Compress.__copy__
1118
1119    cls: defining_class
1120
1121[clinic start generated code]*/
1122
1123static PyObject *
1124zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
1125/*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
1126{
1127    return zlib_Compress_copy_impl(self, cls);
1128}
1129
1130/*[clinic input]
1131zlib.Compress.__deepcopy__
1132
1133    cls: defining_class
1134    memo: object
1135    /
1136
1137[clinic start generated code]*/
1138
1139static PyObject *
1140zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1141                                PyObject *memo)
1142/*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
1143{
1144    return zlib_Compress_copy_impl(self, cls);
1145}
1146
1147/*[clinic input]
1148zlib.Decompress.copy
1149
1150    cls: defining_class
1151
1152Return a copy of the decompression object.
1153[clinic start generated code]*/
1154
1155static PyObject *
1156zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
1157/*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
1158{
1159    zlibstate *state = PyType_GetModuleState(cls);
1160
1161    compobject *retval = newcompobject(state->Decomptype);
1162    if (!retval) return NULL;
1163
1164    /* Copy the zstream state
1165     * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1166     */
1167    ENTER_ZLIB(self);
1168    int err = inflateCopy(&retval->zst, &self->zst);
1169    switch (err) {
1170    case Z_OK:
1171        break;
1172    case Z_STREAM_ERROR:
1173        PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1174        goto error;
1175    case Z_MEM_ERROR:
1176        PyErr_SetString(PyExc_MemoryError,
1177                        "Can't allocate memory for decompression object");
1178        goto error;
1179    default:
1180        zlib_error(state, self->zst, err, "while copying decompression object");
1181        goto error;
1182    }
1183
1184    Py_INCREF(self->unused_data);
1185    Py_XSETREF(retval->unused_data, self->unused_data);
1186    Py_INCREF(self->unconsumed_tail);
1187    Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
1188    Py_XINCREF(self->zdict);
1189    Py_XSETREF(retval->zdict, self->zdict);
1190    retval->eof = self->eof;
1191
1192    /* Mark it as being initialized */
1193    retval->is_initialised = 1;
1194
1195    LEAVE_ZLIB(self);
1196    return (PyObject *)retval;
1197
1198error:
1199    LEAVE_ZLIB(self);
1200    Py_XDECREF(retval);
1201    return NULL;
1202}
1203
1204/*[clinic input]
1205zlib.Decompress.__copy__
1206
1207    cls: defining_class
1208
1209[clinic start generated code]*/
1210
1211static PyObject *
1212zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
1213/*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
1214{
1215    return zlib_Decompress_copy_impl(self, cls);
1216}
1217
1218/*[clinic input]
1219zlib.Decompress.__deepcopy__
1220
1221    cls: defining_class
1222    memo: object
1223    /
1224
1225[clinic start generated code]*/
1226
1227static PyObject *
1228zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1229                                  PyObject *memo)
1230/*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
1231{
1232    return zlib_Decompress_copy_impl(self, cls);
1233}
1234
1235#endif
1236
1237/*[clinic input]
1238zlib.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
1245Return a bytes object containing any remaining decompressed data.
1246[clinic start generated code]*/
1247
1248static PyObject *
1249zlib_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    _BlocksOutputBuffer buffer = {.list = NULL};
1258    _Uint32Window window;  // output buffer's UINT32_MAX sliding window
1259
1260    PyObject *module = PyType_GetModule(cls);
1261    if (module == NULL) {
1262        return NULL;
1263    }
1264
1265    zlibstate *state = get_zlib_state(module);
1266
1267    if (length <= 0) {
1268        PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1269        return NULL;
1270    }
1271
1272    ENTER_ZLIB(self);
1273
1274    if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
1275        LEAVE_ZLIB(self);
1276        return NULL;
1277    }
1278
1279    self->zst.next_in = data.buf;
1280    ibuflen = data.len;
1281
1282    if (OutputBuffer_WindowInitWithSize(&buffer, &window, length,
1283                                        &self->zst.next_out, &self->zst.avail_out) < 0) {
1284        goto abort;
1285    }
1286
1287    do {
1288        arrange_input_buffer(&self->zst, &ibuflen);
1289        flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
1290
1291        do {
1292            if (self->zst.avail_out == 0) {
1293                if (OutputBuffer_WindowGrow(&buffer, &window,
1294                                            &self->zst.next_out, &self->zst.avail_out) < 0) {
1295                    goto abort;
1296                }
1297            }
1298
1299            Py_BEGIN_ALLOW_THREADS
1300            err = inflate(&self->zst, flush);
1301            Py_END_ALLOW_THREADS
1302
1303            switch (err) {
1304            case Z_OK:            /* fall through */
1305            case Z_BUF_ERROR:     /* fall through */
1306            case Z_STREAM_END:
1307                break;
1308            default:
1309                if (err == Z_NEED_DICT && self->zdict != NULL) {
1310                    if (set_inflate_zdict(state, self) < 0) {
1311                        goto abort;
1312                    }
1313                    else
1314                        break;
1315                }
1316                goto save;
1317            }
1318
1319        } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1320
1321    } while (err != Z_STREAM_END && ibuflen != 0);
1322
1323 save:
1324    if (save_unconsumed_input(self, &data, err) < 0) {
1325        goto abort;
1326    }
1327
1328    /* If at end of stream, clean up any memory allocated by zlib. */
1329    if (err == Z_STREAM_END) {
1330        self->eof = 1;
1331        self->is_initialised = 0;
1332        err = inflateEnd(&self->zst);
1333        if (err != Z_OK) {
1334            zlib_error(state, self->zst, err, "while finishing decompression");
1335            goto abort;
1336        }
1337    }
1338
1339    RetVal = OutputBuffer_WindowFinish(&buffer, &window, self->zst.avail_out);
1340    if (RetVal != NULL) {
1341        goto success;
1342    }
1343
1344 abort:
1345    OutputBuffer_WindowOnError(&buffer, &window);
1346    RetVal = NULL;
1347 success:
1348    PyBuffer_Release(&data);
1349    LEAVE_ZLIB(self);
1350    return RetVal;
1351}
1352
1353#include "clinic/zlibmodule.c.h"
1354
1355static 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
1365static 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)
1376static 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]
1384zlib.adler32
1385
1386    data: Py_buffer
1387    value: unsigned_int(bitwise=True) = 1
1388        Starting value of the checksum.
1389    /
1390
1391Compute an Adler-32 checksum of data.
1392
1393The returned checksum is an integer.
1394[clinic start generated code]*/
1395
1396static PyObject *
1397zlib_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    if (data->len > 1024*5) {
1403        unsigned char *buf = data->buf;
1404        Py_ssize_t len = data->len;
1405
1406        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        while ((size_t)len > UINT_MAX) {
1410            value = adler32(value, buf, UINT_MAX);
1411            buf += (size_t) UINT_MAX;
1412            len -= (size_t) UINT_MAX;
1413        }
1414        value = adler32(value, buf, (unsigned int)len);
1415        Py_END_ALLOW_THREADS
1416    } else {
1417        value = adler32(value, data->buf, (unsigned int)data->len);
1418    }
1419    return PyLong_FromUnsignedLong(value & 0xffffffffU);
1420}
1421
1422/*[clinic input]
1423zlib.crc32 -> unsigned_int
1424
1425    data: Py_buffer
1426    value: unsigned_int(bitwise=True) = 0
1427        Starting value of the checksum.
1428    /
1429
1430Compute a CRC-32 checksum of data.
1431
1432The returned checksum is an integer.
1433[clinic start generated code]*/
1434
1435static unsigned int
1436zlib_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    if (data->len > 1024*5) {
1442        unsigned char *buf = data->buf;
1443        Py_ssize_t len = data->len;
1444
1445        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        while ((size_t)len > UINT_MAX) {
1449            value = crc32(value, buf, UINT_MAX);
1450            buf += (size_t) UINT_MAX;
1451            len -= (size_t) UINT_MAX;
1452        }
1453        value = crc32(value, buf, (unsigned int)len);
1454        Py_END_ALLOW_THREADS
1455    } else {
1456        value = crc32(value, data->buf, (unsigned int)data->len);
1457    }
1458    return value;
1459}
1460
1461
1462static 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
1473static PyType_Slot Comptype_slots[] = {
1474    {Py_tp_dealloc, Comp_dealloc},
1475    {Py_tp_methods, comp_methods},
1476    {0, 0},
1477};
1478
1479static 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
1486static 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
1493static 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
1500PyDoc_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
1515static int
1516zlib_clear(PyObject *mod)
1517{
1518    zlibstate *state = get_zlib_state(mod);
1519    Py_CLEAR(state->Comptype);
1520    Py_CLEAR(state->Decomptype);
1521    Py_CLEAR(state->ZlibError);
1522    return 0;
1523}
1524
1525static int
1526zlib_traverse(PyObject *mod, visitproc visit, void *arg)
1527{
1528    zlibstate *state = get_zlib_state(mod);
1529    Py_VISIT(state->Comptype);
1530    Py_VISIT(state->Decomptype);
1531    Py_VISIT(state->ZlibError);
1532    return 0;
1533}
1534
1535static void
1536zlib_free(void *mod)
1537{
1538    zlib_clear((PyObject *)mod);
1539}
1540
1541static int
1542zlib_exec(PyObject *mod)
1543{
1544    zlibstate *state = get_zlib_state(mod);
1545
1546    state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1547        mod, &Comptype_spec, NULL);
1548    if (state->Comptype == NULL) {
1549        return -1;
1550    }
1551
1552    state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1553        mod, &Decomptype_spec, NULL);
1554    if (state->Decomptype == NULL) {
1555        return -1;
1556    }
1557
1558    state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1559    if (state->ZlibError == NULL) {
1560        return -1;
1561    }
1562
1563    Py_INCREF(state->ZlibError);
1564    if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
1565        Py_DECREF(state->ZlibError);
1566        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    ZLIB_ADD_INT_MACRO(MAX_WBITS);
1577    ZLIB_ADD_INT_MACRO(DEFLATED);
1578    ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
1579    ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
1580    // compression levels
1581    ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
1582    ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
1583    ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
1584    ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
1585    // compression strategies
1586    ZLIB_ADD_INT_MACRO(Z_FILTERED);
1587    ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
1588#ifdef Z_RLE // 1.2.0.1
1589    ZLIB_ADD_INT_MACRO(Z_RLE);
1590#endif
1591#ifdef Z_FIXED // 1.2.2.2
1592    ZLIB_ADD_INT_MACRO(Z_FIXED);
1593#endif
1594    ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
1595    // allowed flush values
1596    ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
1597    ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
1598    ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
1599    ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
1600    ZLIB_ADD_INT_MACRO(Z_FINISH);
1601#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1602    ZLIB_ADD_INT_MACRO(Z_BLOCK);
1603#endif
1604#ifdef Z_TREES // 1.2.3.4, only for inflate
1605    ZLIB_ADD_INT_MACRO(Z_TREES);
1606#endif
1607    PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
1608    if (ver == NULL) {
1609        return -1;
1610    }
1611
1612    if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
1613        Py_DECREF(ver);
1614        return -1;
1615    }
1616
1617    ver = PyUnicode_FromString(zlibVersion());
1618    if (ver == NULL) {
1619        return -1;
1620    }
1621
1622    if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
1623        Py_DECREF(ver);
1624        return -1;
1625    }
1626
1627    if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
1628        return -1;
1629    }
1630    return 0;
1631}
1632
1633static PyModuleDef_Slot zlib_slots[] = {
1634    {Py_mod_exec, zlib_exec},
1635    {0, NULL}
1636};
1637
1638static 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
1650PyMODINIT_FUNC
1651PyInit_zlib(void)
1652{
1653    return PyModuleDef_Init(&zlibmodule);
1654}
1655