blob: fe06094b0027a6c6261a011ba31b119a826c10ff [file] [log] [blame]
Guido van Rossumfb221561997-04-29 15:38:09 +00001/* zlibmodule.c -- gzip-compatible data compression */
Martin Panter84544c12016-07-23 03:02:07 +00002/* See http://zlib.net/ */
Mark Hammondae8c2682001-01-31 10:28:03 +00003
Tim Petersee826f82001-01-31 19:39:44 +00004/* Windows users: read Python's PCbuild\readme.txt */
Mark Hammondae8c2682001-01-31 10:28:03 +00005
Victor Stinnerf18f8712014-07-01 16:48:12 +02006#define PY_SSIZE_T_CLEAN
Guido van Rossumfb221561997-04-29 15:38:09 +00007
Guido van Rossum97b54571997-06-03 22:21:47 +00008#include "Python.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02009#include "structmember.h" // PyMemberDef
Guido van Rossum97b54571997-06-03 22:21:47 +000010#include "zlib.h"
Guido van Rossumfb221561997-04-29 15:38:09 +000011
Ma Linf9bedb62021-04-28 14:58:54 +080012// 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
Ma Lin251ffa92021-05-01 07:32:49 +080022OutputBuffer_InitAndGrow(_BlocksOutputBuffer *buffer, Py_ssize_t max_length,
23 Bytef **next_out, uint32_t *avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080024{
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
Ma Lin251ffa92021-05-01 07:32:49 +080036OutputBuffer_InitWithSize(_BlocksOutputBuffer *buffer, Py_ssize_t init_size,
37 Bytef **next_out, uint32_t *avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080038{
39 Py_ssize_t allocated;
40
Ma Lin251ffa92021-05-01 07:32:49 +080041 if (init_size >= 0 && // ensure (size_t) cast is safe
42 (size_t)init_size > UINT32_MAX)
43 {
44 /* In 32-bit build, never reach this conditional branch.
45 The maximum block size accepted by zlib is UINT32_MAX. */
46 init_size = UINT32_MAX;
Ma Linf9bedb62021-04-28 14:58:54 +080047 }
48
49 allocated = _BlocksOutputBuffer_InitWithSize(
50 buffer, init_size, (void**) next_out);
51 *avail_out = (uint32_t) allocated;
52 return allocated;
53}
54
55/* On success, return value >= 0
56 On failure, return -1 */
57static inline Py_ssize_t
Ma Lin251ffa92021-05-01 07:32:49 +080058OutputBuffer_Grow(_BlocksOutputBuffer *buffer,
59 Bytef **next_out, uint32_t *avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080060{
61 Py_ssize_t allocated;
62
63 allocated = _BlocksOutputBuffer_Grow(
64 buffer, (void**) next_out, (Py_ssize_t) *avail_out);
65 *avail_out = (uint32_t) allocated;
66 return allocated;
67}
68
69static inline Py_ssize_t
Ma Lin251ffa92021-05-01 07:32:49 +080070OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080071{
72 return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
73}
74
75static inline PyObject *
Ma Lin251ffa92021-05-01 07:32:49 +080076OutputBuffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080077{
78 return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
79}
80
81static inline void
Ma Lin251ffa92021-05-01 07:32:49 +080082OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
Ma Linf9bedb62021-04-28 14:58:54 +080083{
84 _BlocksOutputBuffer_OnError(buffer);
85}
86
Larry Hastings31826802013-10-19 00:09:25 -070087
Ma Lin93f41182021-04-27 16:37:11 +080088#define ENTER_ZLIB(obj) do { \
89 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
90 Py_BEGIN_ALLOW_THREADS \
91 PyThread_acquire_lock((obj)->lock, 1); \
92 Py_END_ALLOW_THREADS \
93 } } while (0)
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020094#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000095
Martin Panter3f0ee832016-06-05 10:48:34 +000096#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000097# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000098#endif
99
Guido van Rossumfb221561997-04-29 15:38:09 +0000100/* The following parameters are copied from zutil.h, version 0.95 */
101#define DEFLATED 8
102#if MAX_MEM_LEVEL >= 8
103# define DEF_MEM_LEVEL 8
104#else
105# define DEF_MEM_LEVEL MAX_MEM_LEVEL
106#endif
Guido van Rossumfb221561997-04-29 15:38:09 +0000107
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200108/* Initial buffer size. */
109#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +0000110
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100111static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +0000112
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100113typedef struct {
114 PyTypeObject *Comptype;
115 PyTypeObject *Decomptype;
116 PyObject *ZlibError;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500117} zlibstate;
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100118
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500119static inline zlibstate*
Hai Shif707d942020-03-16 21:15:01 +0800120get_zlib_state(PyObject *module)
121{
122 void *state = PyModule_GetState(module);
123 assert(state != NULL);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500124 return (zlibstate *)state;
Hai Shif707d942020-03-16 21:15:01 +0800125}
126
Tim Peters977e5402001-10-17 03:57:20 +0000127typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +0000128{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000129 PyObject_HEAD
130 z_stream zst;
131 PyObject *unused_data;
132 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200133 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000134 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200135 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200136 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +0000137} compobject;
138
Jeremy Hylton0965e082001-10-16 21:56:09 +0000139static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500140zlib_error(zlibstate *state, z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +0000141{
Nadeem Vawda524148a2011-08-28 11:26:46 +0200142 const char *zmsg = Z_NULL;
143 /* In case of a version mismatch, zst.msg won't be initialized.
144 Check for this case first, before looking at zst.msg. */
145 if (err == Z_VERSION_ERROR)
146 zmsg = "library version mismatch";
147 if (zmsg == Z_NULL)
148 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000149 if (zmsg == Z_NULL) {
150 switch (err) {
151 case Z_BUF_ERROR:
152 zmsg = "incomplete or truncated stream";
153 break;
154 case Z_STREAM_ERROR:
155 zmsg = "inconsistent stream state";
156 break;
157 case Z_DATA_ERROR:
158 zmsg = "invalid input data";
159 break;
160 }
161 }
162 if (zmsg == Z_NULL)
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500163 PyErr_Format(state->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000164 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500165 PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000166}
167
Larry Hastings61272b72014-01-07 12:41:53 -0800168/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800169module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -0800170class zlib.Compress "compobject *" "&Comptype"
171class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -0800172[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300173/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800174
Guido van Rossumfb221561997-04-29 15:38:09 +0000175static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000176newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000177{
Tim Peters977e5402001-10-17 03:57:20 +0000178 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000179 self = PyObject_New(compobject, type);
180 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000181 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200182 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000183 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200184 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000185 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000186 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 Py_DECREF(self);
188 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000189 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000190 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000191 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 Py_DECREF(self);
193 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000194 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000195 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200196 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000197 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200198 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
199 return NULL;
200 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000201 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000202}
203
Victor Stinner5064a522013-07-07 16:50:27 +0200204static void*
205PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
206{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300207 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200208 return NULL;
209 /* PyMem_Malloc() cannot be used: the GIL is not held when
210 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300211 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200212}
213
214static void
215PyZlib_Free(voidpf ctx, void *ptr)
216{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200217 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200218}
219
Martin Panter84544c12016-07-23 03:02:07 +0000220static void
221arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
222{
Segev Finer679b5662017-07-27 01:17:57 +0300223 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000224 *remains -= zst->avail_in;
225}
226
Larry Hastings61272b72014-01-07 12:41:53 -0800227/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800228zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200229
Martin Panter1fe0d132016-02-10 10:06:36 +0000230 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800231 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300232 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200233 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000234 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800235
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200236Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800237[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800238
Guido van Rossumfb221561997-04-29 15:38:09 +0000239static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300240zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
241/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000242{
Ma Linf9bedb62021-04-28 14:58:54 +0800243 PyObject *RetVal;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500244 int flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000245 z_stream zst;
Ma Linf9bedb62021-04-28 14:58:54 +0800246 _BlocksOutputBuffer buffer = {.list = NULL};
Tim Peters977e5402001-10-17 03:57:20 +0000247
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500248 zlibstate *state = get_zlib_state(module);
249
250 Byte *ibuf = data->buf;
251 Py_ssize_t ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000252
Ma Lin251ffa92021-05-01 07:32:49 +0800253 if (OutputBuffer_InitAndGrow(&buffer, -1, &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800254 goto error;
255 }
256
Victor Stinner5064a522013-07-07 16:50:27 +0200257 zst.opaque = NULL;
258 zst.zalloc = PyZlib_Malloc;
259 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000260 zst.next_in = ibuf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500261 int err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000262
Martin Panter84544c12016-07-23 03:02:07 +0000263 switch (err) {
264 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 break;
Martin Panter84544c12016-07-23 03:02:07 +0000266 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000267 PyErr_SetString(PyExc_MemoryError,
268 "Out of memory while compressing data");
269 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000270 case Z_STREAM_ERROR:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500271 PyErr_SetString(state->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000273 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000274 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500275 zlib_error(state, zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000276 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000277 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000278
Martin Panter84544c12016-07-23 03:02:07 +0000279 do {
280 arrange_input_buffer(&zst, &ibuflen);
281 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000282
Martin Panter84544c12016-07-23 03:02:07 +0000283 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800284 if (zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800285 if (OutputBuffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800286 deflateEnd(&zst);
287 goto error;
288 }
Martin Panter84544c12016-07-23 03:02:07 +0000289 }
290
291 Py_BEGIN_ALLOW_THREADS
292 err = deflate(&zst, flush);
293 Py_END_ALLOW_THREADS
294
295 if (err == Z_STREAM_ERROR) {
296 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500297 zlib_error(state, zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000298 goto error;
299 }
300
301 } while (zst.avail_out == 0);
302 assert(zst.avail_in == 0);
303
304 } while (flush != Z_FINISH);
305 assert(err == Z_STREAM_END);
306
307 err = deflateEnd(&zst);
308 if (err == Z_OK) {
Ma Lin251ffa92021-05-01 07:32:49 +0800309 RetVal = OutputBuffer_Finish(&buffer, zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800310 if (RetVal == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000311 goto error;
Ma Linf9bedb62021-04-28 14:58:54 +0800312 }
Martin Panter84544c12016-07-23 03:02:07 +0000313 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000314 }
Tim Peters977e5402001-10-17 03:57:20 +0000315 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500316 zlib_error(state, zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000317 error:
Ma Lin251ffa92021-05-01 07:32:49 +0800318 OutputBuffer_OnError(&buffer);
Martin Panter84544c12016-07-23 03:02:07 +0000319 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000320}
321
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200322/*[clinic input]
323zlib.decompress
324
325 data: Py_buffer
326 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300327 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200328 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000329 The window buffer size and container format.
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300330 bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200331 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200332
333Returns a bytes object containing the uncompressed data.
334[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000335
Guido van Rossumfb221561997-04-29 15:38:09 +0000336static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300337zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000338 Py_ssize_t bufsize)
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300339/*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000340{
Ma Linf9bedb62021-04-28 14:58:54 +0800341 PyObject *RetVal;
Martin Panter84544c12016-07-23 03:02:07 +0000342 Byte *ibuf;
343 Py_ssize_t ibuflen;
344 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000345 z_stream zst;
Ma Linf9bedb62021-04-28 14:58:54 +0800346 _BlocksOutputBuffer buffer = {.list = NULL};
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000347
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500348 zlibstate *state = get_zlib_state(module);
349
Martin Panter84544c12016-07-23 03:02:07 +0000350 if (bufsize < 0) {
351 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
352 return NULL;
353 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100354 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000355 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000356
Ma Lin251ffa92021-05-01 07:32:49 +0800357 if (OutputBuffer_InitWithSize(&buffer, bufsize, &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800358 goto error;
359 }
360
Martin Panter84544c12016-07-23 03:02:07 +0000361 ibuf = data->buf;
362 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000363
Victor Stinner5064a522013-07-07 16:50:27 +0200364 zst.opaque = NULL;
365 zst.zalloc = PyZlib_Malloc;
366 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000367 zst.avail_in = 0;
368 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200369 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000370
Martin Panter84544c12016-07-23 03:02:07 +0000371 switch (err) {
372 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 break;
Martin Panter84544c12016-07-23 03:02:07 +0000374 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000375 PyErr_SetString(PyExc_MemoryError,
376 "Out of memory while decompressing data");
377 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000378 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000379 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500380 zlib_error(state, zst, err, "while preparing to decompress data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000381 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000382 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000383
Jeremy Hylton9714f992001-10-16 21:19:45 +0000384 do {
Martin Panter84544c12016-07-23 03:02:07 +0000385 arrange_input_buffer(&zst, &ibuflen);
386 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000387
Martin Panter84544c12016-07-23 03:02:07 +0000388 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800389 if (zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800390 if (OutputBuffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800391 inflateEnd(&zst);
392 goto error;
393 }
Martin Panter84544c12016-07-23 03:02:07 +0000394 }
395
396 Py_BEGIN_ALLOW_THREADS
397 err = inflate(&zst, flush);
398 Py_END_ALLOW_THREADS
399
400 switch (err) {
401 case Z_OK: /* fall through */
402 case Z_BUF_ERROR: /* fall through */
403 case Z_STREAM_END:
404 break;
405 case Z_MEM_ERROR:
406 inflateEnd(&zst);
407 PyErr_SetString(PyExc_MemoryError,
408 "Out of memory while decompressing data");
409 goto error;
410 default:
411 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500412 zlib_error(state, zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000413 goto error;
414 }
Martin Panter84544c12016-07-23 03:02:07 +0000415
416 } while (zst.avail_out == 0);
417
418 } while (err != Z_STREAM_END && ibuflen != 0);
419
420
421 if (err != Z_STREAM_END) {
422 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500423 zlib_error(state, zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000424 goto error;
425 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000426
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000427 err = inflateEnd(&zst);
428 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500429 zlib_error(state, zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000430 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000431 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000432
Ma Lin251ffa92021-05-01 07:32:49 +0800433 RetVal = OutputBuffer_Finish(&buffer, zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800434 if (RetVal != NULL) {
435 return RetVal;
436 }
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000437
438 error:
Ma Lin251ffa92021-05-01 07:32:49 +0800439 OutputBuffer_OnError(&buffer);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000440 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000441}
442
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200443/*[clinic input]
444zlib.compressobj
445
446 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000447 The compression level (an integer in the range 0-9 or -1; default is
448 currently equivalent to 6). Higher compression levels are slower,
449 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200450 method: int(c_default="DEFLATED") = DEFLATED
451 The compression algorithm. If given, this must be DEFLATED.
452 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000453 +9 to +15: The base-two logarithm of the window size. Include a zlib
454 container.
455 -9 to -15: Generate a raw stream.
456 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200457 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
458 Controls the amount of memory used for internal compression state.
459 Valid values range from 1 to 9. Higher values result in higher memory
460 usage, faster compression, and smaller output.
461 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
462 Used to tune the compression algorithm. Possible values are
463 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
464 zdict: Py_buffer = None
465 The predefined compression dictionary - a sequence of bytes
466 containing subsequences that are likely to occur in the input data.
467
468Return a compressor object.
469[clinic start generated code]*/
470
Guido van Rossumfb221561997-04-29 15:38:09 +0000471static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300472zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400473 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300474/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000475{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500476 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200477 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100478 PyErr_SetString(PyExc_OverflowError,
479 "zdict length does not fit in an unsigned int");
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500480 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +0100481 }
482
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500483 compobject *self = newcompobject(state->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000484 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200485 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200486 self->zst.opaque = NULL;
487 self->zst.zalloc = PyZlib_Malloc;
488 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000489 self->zst.next_in = NULL;
490 self->zst.avail_in = 0;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500491 int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000492 switch (err) {
493 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000494 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200495 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200496 goto success;
497 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100498 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200499 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200500 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000501 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200502 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000503 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200504 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
505 goto error;
506 default:
507 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
508 goto error;
509 }
510 }
Martin Panter84544c12016-07-23 03:02:07 +0000511 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000512 PyErr_SetString(PyExc_MemoryError,
513 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200514 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000515 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000516 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200517 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000518 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500519 zlib_error(state, self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200520 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000521 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200522
523 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200524 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200525 success:
Martin Panter84544c12016-07-23 03:02:07 +0000526 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000527}
528
Martin Panter3f0ee832016-06-05 10:48:34 +0000529static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500530set_inflate_zdict(zlibstate *state, compobject *self)
Martin Panter3f0ee832016-06-05 10:48:34 +0000531{
532 Py_buffer zdict_buf;
Martin Panter3f0ee832016-06-05 10:48:34 +0000533 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
534 return -1;
535 }
536 if ((size_t)zdict_buf.len > UINT_MAX) {
537 PyErr_SetString(PyExc_OverflowError,
538 "zdict length does not fit in an unsigned int");
539 PyBuffer_Release(&zdict_buf);
540 return -1;
541 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500542 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000543 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000544 zdict_buf.buf, (unsigned int)zdict_buf.len);
545 PyBuffer_Release(&zdict_buf);
546 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500547 zlib_error(state, self->zst, err, "while setting zdict");
Martin Panter3f0ee832016-06-05 10:48:34 +0000548 return -1;
549 }
550 return 0;
551}
552
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200553/*[clinic input]
554zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200555
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200556 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000557 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200558 zdict: object(c_default="NULL") = b''
559 The predefined compression dictionary. This must be the same
560 dictionary as used by the compressor that produced the input data.
561
562Return a decompressor object.
563[clinic start generated code]*/
564
565static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300566zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
567/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200568{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500569 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200570
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200571 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
572 PyErr_SetString(PyExc_TypeError,
573 "zdict argument must support the buffer protocol");
574 return NULL;
575 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000576
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500577 compobject *self = newcompobject(state->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000578 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000579 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200580 self->zst.opaque = NULL;
581 self->zst.zalloc = PyZlib_Malloc;
582 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000583 self->zst.next_in = NULL;
584 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200585 if (zdict != NULL) {
586 Py_INCREF(zdict);
587 self->zdict = zdict;
588 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500589 int err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000590 switch (err) {
591 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000592 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000593 if (self->zdict != NULL && wbits < 0) {
594#ifdef AT_LEAST_ZLIB_1_2_2_1
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500595 if (set_inflate_zdict(state, self) < 0) {
Martin Panter3f0ee832016-06-05 10:48:34 +0000596 Py_DECREF(self);
597 return NULL;
598 }
599#else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500600 PyErr_Format(state->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000601 "zlib version %s does not allow raw inflate with dictionary",
602 ZLIB_VERSION);
603 Py_DECREF(self);
604 return NULL;
605#endif
606 }
Martin Panter84544c12016-07-23 03:02:07 +0000607 return (PyObject *)self;
608 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000609 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
611 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000612 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000613 Py_DECREF(self);
614 PyErr_SetString(PyExc_MemoryError,
615 "Can't allocate memory for decompression object");
616 return NULL;
617 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500618 zlib_error(state, self->zst, err, "while creating decompression object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000619 Py_DECREF(self);
620 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000621 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000622}
623
624static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000625Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000626{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100627 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000628 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000629 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000630 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200631 Py_XDECREF(self->zdict);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100632 PyObject_Free(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100633 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000634}
635
636static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000637Comp_dealloc(compobject *self)
638{
639 if (self->is_initialised)
640 deflateEnd(&self->zst);
641 Dealloc(self);
642}
643
644static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000645Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000646{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000647 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000648 inflateEnd(&self->zst);
649 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000650}
651
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200652/*[clinic input]
653zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000654
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500655 cls: defining_class
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200656 data: Py_buffer
657 Binary data to be compressed.
658 /
659
660Returns a bytes object containing compressed data.
661
662After calling this function, some of the input data may still
663be stored in internal buffers for later processing.
664Call the flush() method to clear these buffers.
665[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000666
Guido van Rossumfb221561997-04-29 15:38:09 +0000667static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500668zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
669 Py_buffer *data)
670/*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000671{
Ma Linf9bedb62021-04-28 14:58:54 +0800672 PyObject *RetVal;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200673 int err;
Ma Linf9bedb62021-04-28 14:58:54 +0800674 _BlocksOutputBuffer buffer = {.list = NULL};
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500675 zlibstate *state = PyType_GetModuleState(cls);
676
Ma Lin93f41182021-04-27 16:37:11 +0800677 ENTER_ZLIB(self);
678
Martin Panter84544c12016-07-23 03:02:07 +0000679 self->zst.next_in = data->buf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500680 Py_ssize_t ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000681
Ma Lin251ffa92021-05-01 07:32:49 +0800682 if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800683 goto error;
684 }
685
Martin Panter84544c12016-07-23 03:02:07 +0000686 do {
687 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000688
Martin Panter84544c12016-07-23 03:02:07 +0000689 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800690 if (self->zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800691 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800692 goto error;
Ma Lin251ffa92021-05-01 07:32:49 +0800693 }
Ma Linf9bedb62021-04-28 14:58:54 +0800694 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000695
Martin Panter84544c12016-07-23 03:02:07 +0000696 Py_BEGIN_ALLOW_THREADS
697 err = deflate(&self->zst, Z_NO_FLUSH);
698 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000699
Martin Panter84544c12016-07-23 03:02:07 +0000700 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500701 zlib_error(state, self->zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000702 goto error;
703 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000704
Martin Panter84544c12016-07-23 03:02:07 +0000705 } while (self->zst.avail_out == 0);
706 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000707
Martin Panter84544c12016-07-23 03:02:07 +0000708 } while (ibuflen != 0);
709
Ma Lin251ffa92021-05-01 07:32:49 +0800710 RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800711 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000712 goto success;
Ma Linf9bedb62021-04-28 14:58:54 +0800713 }
Martin Panter84544c12016-07-23 03:02:07 +0000714
715 error:
Ma Lin251ffa92021-05-01 07:32:49 +0800716 OutputBuffer_OnError(&buffer);
Ma Linf9bedb62021-04-28 14:58:54 +0800717 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000718 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000719 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000720 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000721}
722
Martin Panter84544c12016-07-23 03:02:07 +0000723/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100724 self->unused_data or self->unconsumed_tail, as appropriate. */
725static int
Martin Panter84544c12016-07-23 03:02:07 +0000726save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100727{
728 if (err == Z_STREAM_END) {
729 /* The end of the compressed data has been reached. Store the leftover
730 input data in self->unused_data. */
731 if (self->zst.avail_in > 0) {
732 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000733 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100734 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000735 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
736 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100737 PyErr_NoMemory();
738 return -1;
739 }
Martin Panter84544c12016-07-23 03:02:07 +0000740 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100741 new_data = PyBytes_FromStringAndSize(NULL, new_size);
742 if (new_data == NULL)
743 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200744 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100745 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200746 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000747 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300748 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100749 self->zst.avail_in = 0;
750 }
751 }
Martin Panter84544c12016-07-23 03:02:07 +0000752
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100753 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
754 /* This code handles two distinct cases:
755 1. Output limit was reached. Save leftover input in unconsumed_tail.
756 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000757 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100758 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000759 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100760 if (new_data == NULL)
761 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300762 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100763 }
Martin Panter84544c12016-07-23 03:02:07 +0000764
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100765 return 0;
766}
767
Larry Hastings61272b72014-01-07 12:41:53 -0800768/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800769zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700770
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500771 cls: defining_class
Larry Hastings31826802013-10-19 00:09:25 -0700772 data: Py_buffer
773 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300774 /
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300775 max_length: Py_ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700776 The maximum allowable length of the decompressed data.
777 Unconsumed input data will be stored in
778 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700779
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200780Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700781
782After calling this function, some of the input data may still be stored in
783internal buffers for later processing.
784Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800785[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700786
Larry Hastings31826802013-10-19 00:09:25 -0700787static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500788zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
789 Py_buffer *data, Py_ssize_t max_length)
790/*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/
Larry Hastings31826802013-10-19 00:09:25 -0700791{
Martin Panter84544c12016-07-23 03:02:07 +0000792 int err = Z_OK;
Ma Linf9bedb62021-04-28 14:58:54 +0800793 Py_ssize_t ibuflen;
794 PyObject *RetVal;
795 _BlocksOutputBuffer buffer = {.list = NULL};
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000796
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500797 PyObject *module = PyType_GetModule(cls);
798 if (module == NULL)
799 return NULL;
800
801 zlibstate *state = get_zlib_state(module);
Martin Panter84544c12016-07-23 03:02:07 +0000802 if (max_length < 0) {
803 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700804 return NULL;
Ma Lin251ffa92021-05-01 07:32:49 +0800805 } else if (max_length == 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800806 max_length = -1;
Ma Lin251ffa92021-05-01 07:32:49 +0800807 }
Martin Panter84544c12016-07-23 03:02:07 +0000808
Ma Lin93f41182021-04-27 16:37:11 +0800809 ENTER_ZLIB(self);
810
Martin Panter84544c12016-07-23 03:02:07 +0000811 self->zst.next_in = data->buf;
812 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000813
Ma Lin251ffa92021-05-01 07:32:49 +0800814 if (OutputBuffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800815 goto abort;
816 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000817
Martin Panter84544c12016-07-23 03:02:07 +0000818 do {
819 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000820
Martin Panter84544c12016-07-23 03:02:07 +0000821 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800822 if (self->zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800823 if (OutputBuffer_GetDataSize(&buffer, self->zst.avail_out) == max_length) {
Martin Panter84544c12016-07-23 03:02:07 +0000824 goto save;
825 }
Ma Lin251ffa92021-05-01 07:32:49 +0800826 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800827 goto abort;
828 }
Martin Panter84544c12016-07-23 03:02:07 +0000829 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000830
Martin Panter84544c12016-07-23 03:02:07 +0000831 Py_BEGIN_ALLOW_THREADS
832 err = inflate(&self->zst, Z_SYNC_FLUSH);
833 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100834
Martin Panter84544c12016-07-23 03:02:07 +0000835 switch (err) {
836 case Z_OK: /* fall through */
837 case Z_BUF_ERROR: /* fall through */
838 case Z_STREAM_END:
839 break;
840 default:
841 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500842 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +0000843 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500844 }
Martin Panter84544c12016-07-23 03:02:07 +0000845 else
846 break;
847 }
848 goto save;
849 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200850
Martin Panter84544c12016-07-23 03:02:07 +0000851 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000852
Martin Panter84544c12016-07-23 03:02:07 +0000853 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000854
Martin Panter84544c12016-07-23 03:02:07 +0000855 save:
856 if (save_unconsumed_input(self, data, err) < 0)
857 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000858
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000859 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100860 /* This is the logical place to call inflateEnd, but the old behaviour
861 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800862 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100863 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000864 /* We will only get Z_BUF_ERROR if the output buffer was full
865 but there wasn't more output when we tried again, so it is
866 not an error condition.
867 */
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500868 zlib_error(state, self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000869 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000870 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000871
Ma Lin251ffa92021-05-01 07:32:49 +0800872 RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800873 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000874 goto success;
Ma Linf9bedb62021-04-28 14:58:54 +0800875 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000876
Martin Panter84544c12016-07-23 03:02:07 +0000877 abort:
Ma Lin251ffa92021-05-01 07:32:49 +0800878 OutputBuffer_OnError(&buffer);
Ma Linf9bedb62021-04-28 14:58:54 +0800879 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000880 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800881 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000882 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000883}
884
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200885/*[clinic input]
886zlib.Compress.flush
887
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500888 cls: defining_class
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200889 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200890 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
891 If mode == Z_FINISH, the compressor object can no longer be
892 used after calling the flush() method. Otherwise, more data
893 can still be compressed.
894 /
895
896Return a bytes object containing any remaining compressed data.
897[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000898
Guido van Rossumfb221561997-04-29 15:38:09 +0000899static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500900zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
901/*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000902{
Victor Stinnere079edd2013-11-21 22:33:21 +0100903 int err;
Ma Linf9bedb62021-04-28 14:58:54 +0800904 PyObject *RetVal;
905 _BlocksOutputBuffer buffer = {.list = NULL};
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000906
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500907 zlibstate *state = PyType_GetModuleState(cls);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000908 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
909 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200910 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000911 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000912 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000913
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000914 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000915
Jeremy Hylton9714f992001-10-16 21:19:45 +0000916 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000917
Ma Lin251ffa92021-05-01 07:32:49 +0800918 if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800919 goto error;
920 }
921
Martin Panter84544c12016-07-23 03:02:07 +0000922 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800923 if (self->zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800924 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800925 goto error;
926 }
Guido van Rossum776152b2007-05-22 22:44:07 +0000927 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000928
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000929 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000930 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000931 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000932
933 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500934 zlib_error(state, self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000935 goto error;
936 }
937 } while (self->zst.avail_out == 0);
938 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000939
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200940 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000941 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200942 mode is Z_FINISH, but checking both for safety*/
943 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000944 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000945 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500946 zlib_error(state, self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000947 goto error;
948 }
949 else
950 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000951
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000952 /* We will only get Z_BUF_ERROR if the output buffer was full
953 but there wasn't more output when we tried again, so it is
954 not an error condition.
955 */
Martin Panter84544c12016-07-23 03:02:07 +0000956 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500957 zlib_error(state, self->zst, err, "while flushing");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000958 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000959 }
Tim Peters977e5402001-10-17 03:57:20 +0000960
Ma Lin251ffa92021-05-01 07:32:49 +0800961 RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800962 if (RetVal != NULL) {
963 goto success;
964 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000965
Ma Linf9bedb62021-04-28 14:58:54 +0800966error:
Ma Lin251ffa92021-05-01 07:32:49 +0800967 OutputBuffer_OnError(&buffer);
Ma Linf9bedb62021-04-28 14:58:54 +0800968 RetVal = NULL;
969success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000970 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000971 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000972}
973
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000974#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700975
Larry Hastings61272b72014-01-07 12:41:53 -0800976/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800977zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700978
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500979 cls: defining_class
980
Larry Hastings31826802013-10-19 00:09:25 -0700981Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800982[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700983
Larry Hastings3cceb382014-01-04 11:09:09 -0800984static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500985zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
986/*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500988 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000989
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500990 compobject *retval = newcompobject(state->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000991 if (!retval) return NULL;
992
993 /* Copy the zstream state
994 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
995 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800996 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500997 int err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000998 switch (err) {
999 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000 break;
Martin Panter84544c12016-07-23 03:02:07 +00001001 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001002 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1003 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001004 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005 PyErr_SetString(PyExc_MemoryError,
1006 "Can't allocate memory for compression object");
1007 goto error;
1008 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001009 zlib_error(state, self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010 goto error;
1011 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001012 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001013 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001014 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001015 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001016 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001017 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001018 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001019
1020 /* Mark it as being initialized */
1021 retval->is_initialised = 1;
1022
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001023 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024 return (PyObject *)retval;
1025
1026error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001027 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001028 Py_XDECREF(retval);
1029 return NULL;
1030}
1031
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001032/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001033zlib.Compress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001034
1035 cls: defining_class
1036
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001037[clinic start generated code]*/
1038
1039static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001040zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
1041/*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001042{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001043 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001044}
1045
1046/*[clinic input]
1047zlib.Compress.__deepcopy__
1048
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001049 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001050 memo: object
1051 /
1052
1053[clinic start generated code]*/
1054
1055static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001056zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1057 PyObject *memo)
1058/*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001059{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001060 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001061}
1062
1063/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001064zlib.Decompress.copy
1065
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001066 cls: defining_class
1067
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001068Return a copy of the decompression object.
1069[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001070
1071static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001072zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
1073/*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001074{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001075 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001076
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001077 compobject *retval = newcompobject(state->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001078 if (!retval) return NULL;
1079
1080 /* Copy the zstream state
1081 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1082 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001083 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001084 int err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001085 switch (err) {
1086 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001087 break;
Martin Panter84544c12016-07-23 03:02:07 +00001088 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001089 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1090 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001091 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001092 PyErr_SetString(PyExc_MemoryError,
1093 "Can't allocate memory for decompression object");
1094 goto error;
1095 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001096 zlib_error(state, self->zst, err, "while copying decompression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001097 goto error;
1098 }
1099
1100 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001101 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001102 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001103 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001104 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001105 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001106 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001107
1108 /* Mark it as being initialized */
1109 retval->is_initialised = 1;
1110
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001111 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001112 return (PyObject *)retval;
1113
1114error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001115 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001116 Py_XDECREF(retval);
1117 return NULL;
1118}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001119
1120/*[clinic input]
1121zlib.Decompress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001122
1123 cls: defining_class
1124
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001125[clinic start generated code]*/
1126
1127static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001128zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
1129/*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001130{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001131 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001132}
1133
1134/*[clinic input]
1135zlib.Decompress.__deepcopy__
1136
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001137 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001138 memo: object
1139 /
1140
1141[clinic start generated code]*/
1142
1143static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001144zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1145 PyObject *memo)
1146/*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001147{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001148 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001149}
1150
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001151#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001152
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001153/*[clinic input]
1154zlib.Decompress.flush
1155
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001156 cls: defining_class
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001157 length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001158 the initial size of the output buffer.
1159 /
1160
1161Return a bytes object containing any remaining decompressed data.
1162[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001163
Guido van Rossumfb221561997-04-29 15:38:09 +00001164static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001165zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
1166 Py_ssize_t length)
1167/*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001168{
Martin Panter84544c12016-07-23 03:02:07 +00001169 int err, flush;
1170 Py_buffer data;
Ma Linf9bedb62021-04-28 14:58:54 +08001171 PyObject *RetVal;
Martin Panter84544c12016-07-23 03:02:07 +00001172 Py_ssize_t ibuflen;
Ma Linf9bedb62021-04-28 14:58:54 +08001173 _BlocksOutputBuffer buffer = {.list = NULL};
Tim Peters977e5402001-10-17 03:57:20 +00001174
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001175 PyObject *module = PyType_GetModule(cls);
1176 if (module == NULL) {
1177 return NULL;
1178 }
1179
1180 zlibstate *state = get_zlib_state(module);
1181
Martin Panter84544c12016-07-23 03:02:07 +00001182 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001183 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1184 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001185 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001186
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001187 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001188 return NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001189 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001190
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001191 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001192
Martin Panter84544c12016-07-23 03:02:07 +00001193 self->zst.next_in = data.buf;
1194 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001195
Ma Lin251ffa92021-05-01 07:32:49 +08001196 if (OutputBuffer_InitWithSize(&buffer, length, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +08001197 goto abort;
1198 }
1199
Martin Panter84544c12016-07-23 03:02:07 +00001200 do {
1201 arrange_input_buffer(&self->zst, &ibuflen);
1202 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001203
Martin Panter84544c12016-07-23 03:02:07 +00001204 do {
Ma Linf9bedb62021-04-28 14:58:54 +08001205 if (self->zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +08001206 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +08001207 goto abort;
Ma Lin251ffa92021-05-01 07:32:49 +08001208 }
Ma Linf9bedb62021-04-28 14:58:54 +08001209 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001210
Martin Panter84544c12016-07-23 03:02:07 +00001211 Py_BEGIN_ALLOW_THREADS
1212 err = inflate(&self->zst, flush);
1213 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001214
Martin Panter84544c12016-07-23 03:02:07 +00001215 switch (err) {
1216 case Z_OK: /* fall through */
1217 case Z_BUF_ERROR: /* fall through */
1218 case Z_STREAM_END:
1219 break;
1220 default:
1221 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001222 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001223 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001224 }
Martin Panter84544c12016-07-23 03:02:07 +00001225 else
1226 break;
1227 }
1228 goto save;
1229 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001230
Martin Panter84544c12016-07-23 03:02:07 +00001231 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1232
1233 } while (err != Z_STREAM_END && ibuflen != 0);
1234
1235 save:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001236 if (save_unconsumed_input(self, &data, err) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001237 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001238 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001239
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001240 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001241 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001242 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001243 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001244 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001245 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001246 zlib_error(state, self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001247 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001248 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001249 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001250
Ma Lin251ffa92021-05-01 07:32:49 +08001251 RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +08001252 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +00001253 goto success;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001254 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001255
Martin Panter84544c12016-07-23 03:02:07 +00001256 abort:
Ma Lin251ffa92021-05-01 07:32:49 +08001257 OutputBuffer_OnError(&buffer);
Ma Linf9bedb62021-04-28 14:58:54 +08001258 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +00001259 success:
1260 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001261 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001262 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001263}
1264
Christian Heimes936e2f32014-01-27 01:06:57 +01001265#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001266
Guido van Rossumfb221561997-04-29 15:38:09 +00001267static PyMethodDef comp_methods[] =
1268{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001269 ZLIB_COMPRESS_COMPRESS_METHODDEF
1270 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001271 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001272 ZLIB_COMPRESS___COPY___METHODDEF
1273 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001274 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001275};
1276
1277static PyMethodDef Decomp_methods[] =
1278{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001279 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001280 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001281 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001282 ZLIB_DECOMPRESS___COPY___METHODDEF
1283 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001284 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001285};
1286
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001287#define COMP_OFF(x) offsetof(compobject, x)
1288static PyMemberDef Decomp_members[] = {
1289 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1290 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001291 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001292 {NULL},
1293};
Guido van Rossumfb221561997-04-29 15:38:09 +00001294
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001295/*[clinic input]
1296zlib.adler32
1297
1298 data: Py_buffer
1299 value: unsigned_int(bitwise=True) = 1
1300 Starting value of the checksum.
1301 /
1302
1303Compute an Adler-32 checksum of data.
1304
1305The returned checksum is an integer.
1306[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001307
Guido van Rossumfb221561997-04-29 15:38:09 +00001308static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001309zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1310/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001311{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001312 /* Releasing the GIL for very small buffers is inefficient
1313 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001314 if (data->len > 1024*5) {
1315 unsigned char *buf = data->buf;
1316 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001317
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001318 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001319 /* Avoid truncation of length for very large buffers. adler32() takes
1320 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001321 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001322 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001323 buf += (size_t) UINT_MAX;
1324 len -= (size_t) UINT_MAX;
1325 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001326 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001327 Py_END_ALLOW_THREADS
1328 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001329 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001330 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001331 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001332}
Tim Peters977e5402001-10-17 03:57:20 +00001333
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001334/*[clinic input]
1335zlib.crc32
1336
1337 data: Py_buffer
1338 value: unsigned_int(bitwise=True) = 0
1339 Starting value of the checksum.
1340 /
1341
1342Compute a CRC-32 checksum of data.
1343
1344The returned checksum is an integer.
1345[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001346
1347static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001348zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1349/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001350{
Martin v. Löwis423be952008-08-13 15:53:07 +00001351 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001352
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001353 /* Releasing the GIL for very small buffers is inefficient
1354 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001355 if (data->len > 1024*5) {
1356 unsigned char *buf = data->buf;
1357 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001358
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001359 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001360 /* Avoid truncation of length for very large buffers. crc32() takes
1361 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001362 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001363 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001364 buf += (size_t) UINT_MAX;
1365 len -= (size_t) UINT_MAX;
1366 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001367 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001368 Py_END_ALLOW_THREADS
1369 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001370 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001371 }
Christian Heimescc47b052008-03-25 14:56:36 +00001372 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001373}
Tim Peters977e5402001-10-17 03:57:20 +00001374
Guido van Rossumfb221561997-04-29 15:38:09 +00001375
1376static PyMethodDef zlib_methods[] =
1377{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001378 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001379 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001380 ZLIB_COMPRESSOBJ_METHODDEF
1381 ZLIB_CRC32_METHODDEF
1382 ZLIB_DECOMPRESS_METHODDEF
1383 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001384 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001385};
1386
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001387static PyType_Slot Comptype_slots[] = {
1388 {Py_tp_dealloc, Comp_dealloc},
1389 {Py_tp_methods, comp_methods},
1390 {0, 0},
1391};
1392
1393static PyType_Spec Comptype_spec = {
Erlend Egeberg Aasland9746cda2021-04-30 16:04:57 +02001394 .name = "zlib.Compress",
1395 .basicsize = sizeof(compobject),
1396 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1397 .slots= Comptype_slots,
Guido van Rossumfb221561997-04-29 15:38:09 +00001398};
1399
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001400static PyType_Slot Decomptype_slots[] = {
1401 {Py_tp_dealloc, Decomp_dealloc},
1402 {Py_tp_methods, Decomp_methods},
1403 {Py_tp_members, Decomp_members},
1404 {0, 0},
1405};
1406
1407static PyType_Spec Decomptype_spec = {
Erlend Egeberg Aasland9746cda2021-04-30 16:04:57 +02001408 .name = "zlib.Decompress",
1409 .basicsize = sizeof(compobject),
1410 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1411 .slots = Decomptype_slots,
Guido van Rossumfb221561997-04-29 15:38:09 +00001412};
1413
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001414PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001415"The functions in this module allow compression and decompression using the\n"
1416"zlib library, which is based on GNU zip.\n"
1417"\n"
1418"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001419"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001420"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001421"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001422"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001423"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001424"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001425"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001426"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001427"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001428
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001429static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001430zlib_clear(PyObject *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001431{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001432 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001433 Py_CLEAR(state->Comptype);
1434 Py_CLEAR(state->Decomptype);
1435 Py_CLEAR(state->ZlibError);
1436 return 0;
1437}
1438
1439static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001440zlib_traverse(PyObject *mod, visitproc visit, void *arg)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001441{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001442 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001443 Py_VISIT(state->Comptype);
1444 Py_VISIT(state->Decomptype);
1445 Py_VISIT(state->ZlibError);
1446 return 0;
1447}
1448
1449static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001450zlib_free(void *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001451{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001452 zlib_clear((PyObject *)mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001453}
1454
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001455static int
1456zlib_exec(PyObject *mod)
1457{
1458 zlibstate *state = get_zlib_state(mod);
1459
1460 state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1461 mod, &Comptype_spec, NULL);
1462 if (state->Comptype == NULL) {
1463 return -1;
1464 }
1465
1466 state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1467 mod, &Decomptype_spec, NULL);
1468 if (state->Decomptype == NULL) {
1469 return -1;
1470 }
1471
1472 state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1473 if (state->ZlibError == NULL) {
1474 return -1;
1475 }
1476
1477 Py_INCREF(state->ZlibError);
1478 if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
1479 Py_DECREF(state->ZlibError);
1480 return -1;
1481 }
1482
1483#define ZLIB_ADD_INT_MACRO(c) \
1484 do { \
1485 if ((PyModule_AddIntConstant(mod, #c, c)) < 0) { \
1486 return -1; \
1487 } \
1488 } while(0)
1489
1490 ZLIB_ADD_INT_MACRO(MAX_WBITS);
1491 ZLIB_ADD_INT_MACRO(DEFLATED);
1492 ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
1493 ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
1494 // compression levels
1495 ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
1496 ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
1497 ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
1498 ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
1499 // compression strategies
1500 ZLIB_ADD_INT_MACRO(Z_FILTERED);
1501 ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
1502#ifdef Z_RLE // 1.2.0.1
1503 ZLIB_ADD_INT_MACRO(Z_RLE);
1504#endif
1505#ifdef Z_FIXED // 1.2.2.2
1506 ZLIB_ADD_INT_MACRO(Z_FIXED);
1507#endif
1508 ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
1509 // allowed flush values
1510 ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
1511 ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
1512 ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
1513 ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
1514 ZLIB_ADD_INT_MACRO(Z_FINISH);
1515#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1516 ZLIB_ADD_INT_MACRO(Z_BLOCK);
1517#endif
1518#ifdef Z_TREES // 1.2.3.4, only for inflate
1519 ZLIB_ADD_INT_MACRO(Z_TREES);
1520#endif
1521 PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
1522 if (ver == NULL) {
1523 return -1;
1524 }
1525
1526 if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
1527 Py_DECREF(ver);
1528 return -1;
1529 }
1530
1531 ver = PyUnicode_FromString(zlibVersion());
1532 if (ver == NULL) {
1533 return -1;
1534 }
1535
1536 if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
1537 Py_DECREF(ver);
1538 return -1;
1539 }
1540
1541 if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
1542 return -1;
1543 }
1544 return 0;
1545}
1546
1547static PyModuleDef_Slot zlib_slots[] = {
1548 {Py_mod_exec, zlib_exec},
1549 {0, NULL}
1550};
1551
Martin v. Löwis1a214512008-06-11 05:26:20 +00001552static struct PyModuleDef zlibmodule = {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001553 PyModuleDef_HEAD_INIT,
1554 .m_name = "zlib",
1555 .m_doc = zlib_module_documentation,
1556 .m_size = sizeof(zlibstate),
1557 .m_methods = zlib_methods,
1558 .m_slots = zlib_slots,
1559 .m_traverse = zlib_traverse,
1560 .m_clear = zlib_clear,
1561 .m_free = zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001562};
1563
Mark Hammond62b1ab12002-07-23 06:31:15 +00001564PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001565PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001566{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001567 return PyModuleDef_Init(&zlibmodule);
Guido van Rossumfb221561997-04-29 15:38:09 +00001568}