blob: fc63ca9445a8a54c557b8d575421b7012d950216 [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
22Buffer_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
36Buffer_InitWithSize(_BlocksOutputBuffer *buffer, Py_ssize_t init_size,
37 Bytef **next_out, uint32_t *avail_out)
38{
39 Py_ssize_t allocated;
40
41 if (init_size < 0 || (size_t)init_size > UINT32_MAX) {
42 PyErr_SetString(PyExc_ValueError,
43 "Initial buffer size should (0 <= size <= UINT32_MAX)");
44 return -1;
45 }
46
47 allocated = _BlocksOutputBuffer_InitWithSize(
48 buffer, init_size, (void**) next_out);
49 *avail_out = (uint32_t) allocated;
50 return allocated;
51}
52
53/* On success, return value >= 0
54 On failure, return -1 */
55static inline Py_ssize_t
56Buffer_Grow(_BlocksOutputBuffer *buffer,
57 Bytef **next_out, uint32_t *avail_out)
58{
59 Py_ssize_t allocated;
60
61 allocated = _BlocksOutputBuffer_Grow(
62 buffer, (void**) next_out, (Py_ssize_t) *avail_out);
63 *avail_out = (uint32_t) allocated;
64 return allocated;
65}
66
67static inline Py_ssize_t
68Buffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
69{
70 return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
71}
72
73static inline PyObject *
74Buffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
75{
76 return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
77}
78
79static inline void
80Buffer_OnError(_BlocksOutputBuffer *buffer)
81{
82 _BlocksOutputBuffer_OnError(buffer);
83}
84
Larry Hastings31826802013-10-19 00:09:25 -070085
Ma Lin93f41182021-04-27 16:37:11 +080086#define ENTER_ZLIB(obj) do { \
87 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
88 Py_BEGIN_ALLOW_THREADS \
89 PyThread_acquire_lock((obj)->lock, 1); \
90 Py_END_ALLOW_THREADS \
91 } } while (0)
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020092#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000093
Martin Panter3f0ee832016-06-05 10:48:34 +000094#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000095# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000096#endif
97
Guido van Rossumfb221561997-04-29 15:38:09 +000098/* The following parameters are copied from zutil.h, version 0.95 */
99#define DEFLATED 8
100#if MAX_MEM_LEVEL >= 8
101# define DEF_MEM_LEVEL 8
102#else
103# define DEF_MEM_LEVEL MAX_MEM_LEVEL
104#endif
Guido van Rossumfb221561997-04-29 15:38:09 +0000105
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200106/* Initial buffer size. */
107#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +0000108
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100109static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +0000110
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100111typedef struct {
112 PyTypeObject *Comptype;
113 PyTypeObject *Decomptype;
114 PyObject *ZlibError;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500115} zlibstate;
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100116
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500117static inline zlibstate*
Hai Shif707d942020-03-16 21:15:01 +0800118get_zlib_state(PyObject *module)
119{
120 void *state = PyModule_GetState(module);
121 assert(state != NULL);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500122 return (zlibstate *)state;
Hai Shif707d942020-03-16 21:15:01 +0800123}
124
Tim Peters977e5402001-10-17 03:57:20 +0000125typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +0000126{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000127 PyObject_HEAD
128 z_stream zst;
129 PyObject *unused_data;
130 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200131 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000132 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200133 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200134 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +0000135} compobject;
136
Jeremy Hylton0965e082001-10-16 21:56:09 +0000137static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500138zlib_error(zlibstate *state, z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +0000139{
Nadeem Vawda524148a2011-08-28 11:26:46 +0200140 const char *zmsg = Z_NULL;
141 /* In case of a version mismatch, zst.msg won't be initialized.
142 Check for this case first, before looking at zst.msg. */
143 if (err == Z_VERSION_ERROR)
144 zmsg = "library version mismatch";
145 if (zmsg == Z_NULL)
146 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000147 if (zmsg == Z_NULL) {
148 switch (err) {
149 case Z_BUF_ERROR:
150 zmsg = "incomplete or truncated stream";
151 break;
152 case Z_STREAM_ERROR:
153 zmsg = "inconsistent stream state";
154 break;
155 case Z_DATA_ERROR:
156 zmsg = "invalid input data";
157 break;
158 }
159 }
160 if (zmsg == Z_NULL)
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500161 PyErr_Format(state->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000162 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500163 PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000164}
165
Larry Hastings61272b72014-01-07 12:41:53 -0800166/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800167module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -0800168class zlib.Compress "compobject *" "&Comptype"
169class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -0800170[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300171/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800172
Guido van Rossumfb221561997-04-29 15:38:09 +0000173static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000174newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000175{
Tim Peters977e5402001-10-17 03:57:20 +0000176 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000177 self = PyObject_New(compobject, type);
178 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000179 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200180 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000181 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200182 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000183 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000184 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000185 Py_DECREF(self);
186 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000187 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000188 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000189 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 Py_DECREF(self);
191 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000192 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000193 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200194 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000195 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200196 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
197 return NULL;
198 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000199 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000200}
201
Victor Stinner5064a522013-07-07 16:50:27 +0200202static void*
203PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
204{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300205 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200206 return NULL;
207 /* PyMem_Malloc() cannot be used: the GIL is not held when
208 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300209 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200210}
211
212static void
213PyZlib_Free(voidpf ctx, void *ptr)
214{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200215 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200216}
217
Martin Panter84544c12016-07-23 03:02:07 +0000218static void
219arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
220{
Segev Finer679b5662017-07-27 01:17:57 +0300221 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000222 *remains -= zst->avail_in;
223}
224
Larry Hastings61272b72014-01-07 12:41:53 -0800225/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800226zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200227
Martin Panter1fe0d132016-02-10 10:06:36 +0000228 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800229 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300230 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200231 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000232 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800233
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200234Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800235[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800236
Guido van Rossumfb221561997-04-29 15:38:09 +0000237static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300238zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
239/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000240{
Ma Linf9bedb62021-04-28 14:58:54 +0800241 PyObject *RetVal;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500242 int flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000243 z_stream zst;
Ma Linf9bedb62021-04-28 14:58:54 +0800244 _BlocksOutputBuffer buffer = {.list = NULL};
Tim Peters977e5402001-10-17 03:57:20 +0000245
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500246 zlibstate *state = get_zlib_state(module);
247
248 Byte *ibuf = data->buf;
249 Py_ssize_t ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000250
Ma Linf9bedb62021-04-28 14:58:54 +0800251 if (Buffer_InitAndGrow(&buffer, -1, &zst.next_out, &zst.avail_out) < 0) {
252 goto error;
253 }
254
Victor Stinner5064a522013-07-07 16:50:27 +0200255 zst.opaque = NULL;
256 zst.zalloc = PyZlib_Malloc;
257 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000258 zst.next_in = ibuf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500259 int err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000260
Martin Panter84544c12016-07-23 03:02:07 +0000261 switch (err) {
262 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000263 break;
Martin Panter84544c12016-07-23 03:02:07 +0000264 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 PyErr_SetString(PyExc_MemoryError,
266 "Out of memory while compressing data");
267 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000268 case Z_STREAM_ERROR:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500269 PyErr_SetString(state->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000270 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000271 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000272 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500273 zlib_error(state, zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000274 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000275 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000276
Martin Panter84544c12016-07-23 03:02:07 +0000277 do {
278 arrange_input_buffer(&zst, &ibuflen);
279 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000280
Martin Panter84544c12016-07-23 03:02:07 +0000281 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800282 if (zst.avail_out == 0) {
283 if (Buffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
284 deflateEnd(&zst);
285 goto error;
286 }
Martin Panter84544c12016-07-23 03:02:07 +0000287 }
288
289 Py_BEGIN_ALLOW_THREADS
290 err = deflate(&zst, flush);
291 Py_END_ALLOW_THREADS
292
293 if (err == Z_STREAM_ERROR) {
294 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500295 zlib_error(state, zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000296 goto error;
297 }
298
299 } while (zst.avail_out == 0);
300 assert(zst.avail_in == 0);
301
302 } while (flush != Z_FINISH);
303 assert(err == Z_STREAM_END);
304
305 err = deflateEnd(&zst);
306 if (err == Z_OK) {
Ma Linf9bedb62021-04-28 14:58:54 +0800307 RetVal = Buffer_Finish(&buffer, zst.avail_out);
308 if (RetVal == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000309 goto error;
Ma Linf9bedb62021-04-28 14:58:54 +0800310 }
Martin Panter84544c12016-07-23 03:02:07 +0000311 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000312 }
Tim Peters977e5402001-10-17 03:57:20 +0000313 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500314 zlib_error(state, zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000315 error:
Ma Linf9bedb62021-04-28 14:58:54 +0800316 Buffer_OnError(&buffer);
Martin Panter84544c12016-07-23 03:02:07 +0000317 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000318}
319
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200320/*[clinic input]
321zlib.decompress
322
323 data: Py_buffer
324 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300325 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200326 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000327 The window buffer size and container format.
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300328 bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200329 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200330
331Returns a bytes object containing the uncompressed data.
332[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000333
Guido van Rossumfb221561997-04-29 15:38:09 +0000334static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300335zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000336 Py_ssize_t bufsize)
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300337/*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000338{
Ma Linf9bedb62021-04-28 14:58:54 +0800339 PyObject *RetVal;
Martin Panter84544c12016-07-23 03:02:07 +0000340 Byte *ibuf;
341 Py_ssize_t ibuflen;
342 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000343 z_stream zst;
Ma Linf9bedb62021-04-28 14:58:54 +0800344 _BlocksOutputBuffer buffer = {.list = NULL};
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000345
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500346 zlibstate *state = get_zlib_state(module);
347
Martin Panter84544c12016-07-23 03:02:07 +0000348 if (bufsize < 0) {
349 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
350 return NULL;
351 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100352 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000353 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000354
Ma Linf9bedb62021-04-28 14:58:54 +0800355 if (Buffer_InitWithSize(&buffer, bufsize, &zst.next_out, &zst.avail_out) < 0) {
356 goto error;
357 }
358
Martin Panter84544c12016-07-23 03:02:07 +0000359 ibuf = data->buf;
360 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000361
Victor Stinner5064a522013-07-07 16:50:27 +0200362 zst.opaque = NULL;
363 zst.zalloc = PyZlib_Malloc;
364 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000365 zst.avail_in = 0;
366 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200367 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000368
Martin Panter84544c12016-07-23 03:02:07 +0000369 switch (err) {
370 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 break;
Martin Panter84544c12016-07-23 03:02:07 +0000372 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 PyErr_SetString(PyExc_MemoryError,
374 "Out of memory while decompressing data");
375 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000376 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000377 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500378 zlib_error(state, zst, err, "while preparing to decompress data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000379 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000380 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000381
Jeremy Hylton9714f992001-10-16 21:19:45 +0000382 do {
Martin Panter84544c12016-07-23 03:02:07 +0000383 arrange_input_buffer(&zst, &ibuflen);
384 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000385
Martin Panter84544c12016-07-23 03:02:07 +0000386 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800387 if (zst.avail_out == 0) {
388 if (Buffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
389 inflateEnd(&zst);
390 goto error;
391 }
Martin Panter84544c12016-07-23 03:02:07 +0000392 }
393
394 Py_BEGIN_ALLOW_THREADS
395 err = inflate(&zst, flush);
396 Py_END_ALLOW_THREADS
397
398 switch (err) {
399 case Z_OK: /* fall through */
400 case Z_BUF_ERROR: /* fall through */
401 case Z_STREAM_END:
402 break;
403 case Z_MEM_ERROR:
404 inflateEnd(&zst);
405 PyErr_SetString(PyExc_MemoryError,
406 "Out of memory while decompressing data");
407 goto error;
408 default:
409 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500410 zlib_error(state, zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000411 goto error;
412 }
Martin Panter84544c12016-07-23 03:02:07 +0000413
414 } while (zst.avail_out == 0);
415
416 } while (err != Z_STREAM_END && ibuflen != 0);
417
418
419 if (err != Z_STREAM_END) {
420 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500421 zlib_error(state, zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000422 goto error;
423 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000424
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000425 err = inflateEnd(&zst);
426 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500427 zlib_error(state, zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000428 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000429 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000430
Ma Linf9bedb62021-04-28 14:58:54 +0800431 RetVal = Buffer_Finish(&buffer, zst.avail_out);
432 if (RetVal != NULL) {
433 return RetVal;
434 }
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000435
436 error:
Ma Linf9bedb62021-04-28 14:58:54 +0800437 Buffer_OnError(&buffer);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000438 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000439}
440
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200441/*[clinic input]
442zlib.compressobj
443
444 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000445 The compression level (an integer in the range 0-9 or -1; default is
446 currently equivalent to 6). Higher compression levels are slower,
447 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200448 method: int(c_default="DEFLATED") = DEFLATED
449 The compression algorithm. If given, this must be DEFLATED.
450 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000451 +9 to +15: The base-two logarithm of the window size. Include a zlib
452 container.
453 -9 to -15: Generate a raw stream.
454 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200455 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
456 Controls the amount of memory used for internal compression state.
457 Valid values range from 1 to 9. Higher values result in higher memory
458 usage, faster compression, and smaller output.
459 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
460 Used to tune the compression algorithm. Possible values are
461 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
462 zdict: Py_buffer = None
463 The predefined compression dictionary - a sequence of bytes
464 containing subsequences that are likely to occur in the input data.
465
466Return a compressor object.
467[clinic start generated code]*/
468
Guido van Rossumfb221561997-04-29 15:38:09 +0000469static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300470zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400471 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300472/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000473{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500474 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200475 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100476 PyErr_SetString(PyExc_OverflowError,
477 "zdict length does not fit in an unsigned int");
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500478 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +0100479 }
480
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500481 compobject *self = newcompobject(state->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000482 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200483 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200484 self->zst.opaque = NULL;
485 self->zst.zalloc = PyZlib_Malloc;
486 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000487 self->zst.next_in = NULL;
488 self->zst.avail_in = 0;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500489 int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000490 switch (err) {
491 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000492 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200493 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200494 goto success;
495 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100496 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200497 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200498 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000499 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200500 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000501 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200502 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
503 goto error;
504 default:
505 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
506 goto error;
507 }
508 }
Martin Panter84544c12016-07-23 03:02:07 +0000509 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000510 PyErr_SetString(PyExc_MemoryError,
511 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200512 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000513 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000514 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200515 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000516 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500517 zlib_error(state, self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200518 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000519 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200520
521 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200522 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200523 success:
Martin Panter84544c12016-07-23 03:02:07 +0000524 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000525}
526
Martin Panter3f0ee832016-06-05 10:48:34 +0000527static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500528set_inflate_zdict(zlibstate *state, compobject *self)
Martin Panter3f0ee832016-06-05 10:48:34 +0000529{
530 Py_buffer zdict_buf;
Martin Panter3f0ee832016-06-05 10:48:34 +0000531 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
532 return -1;
533 }
534 if ((size_t)zdict_buf.len > UINT_MAX) {
535 PyErr_SetString(PyExc_OverflowError,
536 "zdict length does not fit in an unsigned int");
537 PyBuffer_Release(&zdict_buf);
538 return -1;
539 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500540 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000541 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000542 zdict_buf.buf, (unsigned int)zdict_buf.len);
543 PyBuffer_Release(&zdict_buf);
544 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500545 zlib_error(state, self->zst, err, "while setting zdict");
Martin Panter3f0ee832016-06-05 10:48:34 +0000546 return -1;
547 }
548 return 0;
549}
550
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200551/*[clinic input]
552zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200553
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200554 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000555 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200556 zdict: object(c_default="NULL") = b''
557 The predefined compression dictionary. This must be the same
558 dictionary as used by the compressor that produced the input data.
559
560Return a decompressor object.
561[clinic start generated code]*/
562
563static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300564zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
565/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200566{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500567 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200568
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200569 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
570 PyErr_SetString(PyExc_TypeError,
571 "zdict argument must support the buffer protocol");
572 return NULL;
573 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000574
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500575 compobject *self = newcompobject(state->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000576 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000577 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200578 self->zst.opaque = NULL;
579 self->zst.zalloc = PyZlib_Malloc;
580 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000581 self->zst.next_in = NULL;
582 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200583 if (zdict != NULL) {
584 Py_INCREF(zdict);
585 self->zdict = zdict;
586 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500587 int err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000588 switch (err) {
589 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000590 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000591 if (self->zdict != NULL && wbits < 0) {
592#ifdef AT_LEAST_ZLIB_1_2_2_1
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500593 if (set_inflate_zdict(state, self) < 0) {
Martin Panter3f0ee832016-06-05 10:48:34 +0000594 Py_DECREF(self);
595 return NULL;
596 }
597#else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500598 PyErr_Format(state->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000599 "zlib version %s does not allow raw inflate with dictionary",
600 ZLIB_VERSION);
601 Py_DECREF(self);
602 return NULL;
603#endif
604 }
Martin Panter84544c12016-07-23 03:02:07 +0000605 return (PyObject *)self;
606 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000607 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000608 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
609 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000610 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 Py_DECREF(self);
612 PyErr_SetString(PyExc_MemoryError,
613 "Can't allocate memory for decompression object");
614 return NULL;
615 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500616 zlib_error(state, self->zst, err, "while creating decompression object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 Py_DECREF(self);
618 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000619 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000620}
621
622static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000623Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000624{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100625 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000626 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000627 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000628 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200629 Py_XDECREF(self->zdict);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100630 PyObject_Free(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100631 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000632}
633
634static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000635Comp_dealloc(compobject *self)
636{
637 if (self->is_initialised)
638 deflateEnd(&self->zst);
639 Dealloc(self);
640}
641
642static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000643Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000644{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000645 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000646 inflateEnd(&self->zst);
647 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000648}
649
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200650/*[clinic input]
651zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000652
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500653 cls: defining_class
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200654 data: Py_buffer
655 Binary data to be compressed.
656 /
657
658Returns a bytes object containing compressed data.
659
660After calling this function, some of the input data may still
661be stored in internal buffers for later processing.
662Call the flush() method to clear these buffers.
663[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000664
Guido van Rossumfb221561997-04-29 15:38:09 +0000665static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500666zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
667 Py_buffer *data)
668/*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000669{
Ma Linf9bedb62021-04-28 14:58:54 +0800670 PyObject *RetVal;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200671 int err;
Ma Linf9bedb62021-04-28 14:58:54 +0800672 _BlocksOutputBuffer buffer = {.list = NULL};
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500673 zlibstate *state = PyType_GetModuleState(cls);
674
Ma Lin93f41182021-04-27 16:37:11 +0800675 ENTER_ZLIB(self);
676
Martin Panter84544c12016-07-23 03:02:07 +0000677 self->zst.next_in = data->buf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500678 Py_ssize_t ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000679
Ma Linf9bedb62021-04-28 14:58:54 +0800680 if (Buffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
681 goto error;
682 }
683
Martin Panter84544c12016-07-23 03:02:07 +0000684 do {
685 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000686
Martin Panter84544c12016-07-23 03:02:07 +0000687 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800688 if (self->zst.avail_out == 0) {
689 if (Buffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0)
690 goto error;
691 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000692
Martin Panter84544c12016-07-23 03:02:07 +0000693 Py_BEGIN_ALLOW_THREADS
694 err = deflate(&self->zst, Z_NO_FLUSH);
695 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000696
Martin Panter84544c12016-07-23 03:02:07 +0000697 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500698 zlib_error(state, self->zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000699 goto error;
700 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000701
Martin Panter84544c12016-07-23 03:02:07 +0000702 } while (self->zst.avail_out == 0);
703 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000704
Martin Panter84544c12016-07-23 03:02:07 +0000705 } while (ibuflen != 0);
706
Ma Linf9bedb62021-04-28 14:58:54 +0800707 RetVal = Buffer_Finish(&buffer, self->zst.avail_out);
708 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000709 goto success;
Ma Linf9bedb62021-04-28 14:58:54 +0800710 }
Martin Panter84544c12016-07-23 03:02:07 +0000711
712 error:
Ma Linf9bedb62021-04-28 14:58:54 +0800713 Buffer_OnError(&buffer);
714 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000715 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000716 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000717 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000718}
719
Martin Panter84544c12016-07-23 03:02:07 +0000720/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100721 self->unused_data or self->unconsumed_tail, as appropriate. */
722static int
Martin Panter84544c12016-07-23 03:02:07 +0000723save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100724{
725 if (err == Z_STREAM_END) {
726 /* The end of the compressed data has been reached. Store the leftover
727 input data in self->unused_data. */
728 if (self->zst.avail_in > 0) {
729 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000730 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100731 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000732 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
733 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100734 PyErr_NoMemory();
735 return -1;
736 }
Martin Panter84544c12016-07-23 03:02:07 +0000737 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100738 new_data = PyBytes_FromStringAndSize(NULL, new_size);
739 if (new_data == NULL)
740 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200741 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100742 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200743 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000744 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300745 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100746 self->zst.avail_in = 0;
747 }
748 }
Martin Panter84544c12016-07-23 03:02:07 +0000749
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100750 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
751 /* This code handles two distinct cases:
752 1. Output limit was reached. Save leftover input in unconsumed_tail.
753 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000754 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100755 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000756 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100757 if (new_data == NULL)
758 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300759 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100760 }
Martin Panter84544c12016-07-23 03:02:07 +0000761
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100762 return 0;
763}
764
Larry Hastings61272b72014-01-07 12:41:53 -0800765/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800766zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700767
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500768 cls: defining_class
Larry Hastings31826802013-10-19 00:09:25 -0700769 data: Py_buffer
770 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300771 /
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300772 max_length: Py_ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700773 The maximum allowable length of the decompressed data.
774 Unconsumed input data will be stored in
775 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700776
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200777Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700778
779After calling this function, some of the input data may still be stored in
780internal buffers for later processing.
781Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800782[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700783
Larry Hastings31826802013-10-19 00:09:25 -0700784static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500785zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
786 Py_buffer *data, Py_ssize_t max_length)
787/*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/
Larry Hastings31826802013-10-19 00:09:25 -0700788{
Martin Panter84544c12016-07-23 03:02:07 +0000789 int err = Z_OK;
Ma Linf9bedb62021-04-28 14:58:54 +0800790 Py_ssize_t ibuflen;
791 PyObject *RetVal;
792 _BlocksOutputBuffer buffer = {.list = NULL};
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000793
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500794 PyObject *module = PyType_GetModule(cls);
795 if (module == NULL)
796 return NULL;
797
798 zlibstate *state = get_zlib_state(module);
Martin Panter84544c12016-07-23 03:02:07 +0000799 if (max_length < 0) {
800 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700801 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000802 } else if (max_length == 0)
Ma Linf9bedb62021-04-28 14:58:54 +0800803 max_length = -1;
Martin Panter84544c12016-07-23 03:02:07 +0000804
Ma Lin93f41182021-04-27 16:37:11 +0800805 ENTER_ZLIB(self);
806
Martin Panter84544c12016-07-23 03:02:07 +0000807 self->zst.next_in = data->buf;
808 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000809
Ma Linf9bedb62021-04-28 14:58:54 +0800810 if (Buffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) {
811 goto abort;
812 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000813
Martin Panter84544c12016-07-23 03:02:07 +0000814 do {
815 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000816
Martin Panter84544c12016-07-23 03:02:07 +0000817 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800818 if (self->zst.avail_out == 0) {
819 if (Buffer_GetDataSize(&buffer, self->zst.avail_out) == max_length) {
Martin Panter84544c12016-07-23 03:02:07 +0000820 goto save;
821 }
Ma Linf9bedb62021-04-28 14:58:54 +0800822 if (Buffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
823 goto abort;
824 }
Martin Panter84544c12016-07-23 03:02:07 +0000825 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000826
Martin Panter84544c12016-07-23 03:02:07 +0000827 Py_BEGIN_ALLOW_THREADS
828 err = inflate(&self->zst, Z_SYNC_FLUSH);
829 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100830
Martin Panter84544c12016-07-23 03:02:07 +0000831 switch (err) {
832 case Z_OK: /* fall through */
833 case Z_BUF_ERROR: /* fall through */
834 case Z_STREAM_END:
835 break;
836 default:
837 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500838 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +0000839 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500840 }
Martin Panter84544c12016-07-23 03:02:07 +0000841 else
842 break;
843 }
844 goto save;
845 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200846
Martin Panter84544c12016-07-23 03:02:07 +0000847 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000848
Martin Panter84544c12016-07-23 03:02:07 +0000849 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000850
Martin Panter84544c12016-07-23 03:02:07 +0000851 save:
852 if (save_unconsumed_input(self, data, err) < 0)
853 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000854
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000855 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100856 /* This is the logical place to call inflateEnd, but the old behaviour
857 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800858 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100859 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 /* We will only get Z_BUF_ERROR if the output buffer was full
861 but there wasn't more output when we tried again, so it is
862 not an error condition.
863 */
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500864 zlib_error(state, self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000865 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000866 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000867
Ma Linf9bedb62021-04-28 14:58:54 +0800868 RetVal = Buffer_Finish(&buffer, self->zst.avail_out);
869 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000870 goto success;
Ma Linf9bedb62021-04-28 14:58:54 +0800871 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000872
Martin Panter84544c12016-07-23 03:02:07 +0000873 abort:
Ma Linf9bedb62021-04-28 14:58:54 +0800874 Buffer_OnError(&buffer);
875 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000876 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800877 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000878 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000879}
880
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200881/*[clinic input]
882zlib.Compress.flush
883
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500884 cls: defining_class
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200885 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200886 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
887 If mode == Z_FINISH, the compressor object can no longer be
888 used after calling the flush() method. Otherwise, more data
889 can still be compressed.
890 /
891
892Return a bytes object containing any remaining compressed data.
893[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000894
Guido van Rossumfb221561997-04-29 15:38:09 +0000895static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500896zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
897/*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000898{
Victor Stinnere079edd2013-11-21 22:33:21 +0100899 int err;
Ma Linf9bedb62021-04-28 14:58:54 +0800900 PyObject *RetVal;
901 _BlocksOutputBuffer buffer = {.list = NULL};
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000902
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500903 zlibstate *state = PyType_GetModuleState(cls);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000904 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
905 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200906 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000907 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000908 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000909
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000910 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000911
Jeremy Hylton9714f992001-10-16 21:19:45 +0000912 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000913
Ma Linf9bedb62021-04-28 14:58:54 +0800914 if (Buffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
915 goto error;
916 }
917
Martin Panter84544c12016-07-23 03:02:07 +0000918 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800919 if (self->zst.avail_out == 0) {
920 if (Buffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
921 goto error;
922 }
Guido van Rossum776152b2007-05-22 22:44:07 +0000923 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000924
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000925 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000926 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000927 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000928
929 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500930 zlib_error(state, self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000931 goto error;
932 }
933 } while (self->zst.avail_out == 0);
934 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000935
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200936 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000937 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200938 mode is Z_FINISH, but checking both for safety*/
939 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000940 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000941 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500942 zlib_error(state, self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000943 goto error;
944 }
945 else
946 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000947
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000948 /* 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 */
Martin Panter84544c12016-07-23 03:02:07 +0000952 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500953 zlib_error(state, self->zst, err, "while flushing");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000954 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000955 }
Tim Peters977e5402001-10-17 03:57:20 +0000956
Ma Linf9bedb62021-04-28 14:58:54 +0800957 RetVal = Buffer_Finish(&buffer, self->zst.avail_out);
958 if (RetVal != NULL) {
959 goto success;
960 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000961
Ma Linf9bedb62021-04-28 14:58:54 +0800962error:
963 Buffer_OnError(&buffer);
964 RetVal = NULL;
965success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000966 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000967 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000968}
969
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000970#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700971
Larry Hastings61272b72014-01-07 12:41:53 -0800972/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800973zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700974
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500975 cls: defining_class
976
Larry Hastings31826802013-10-19 00:09:25 -0700977Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800978[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700979
Larry Hastings3cceb382014-01-04 11:09:09 -0800980static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500981zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
982/*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000983{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500984 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000985
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500986 compobject *retval = newcompobject(state->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987 if (!retval) return NULL;
988
989 /* Copy the zstream state
990 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
991 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800992 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500993 int err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000994 switch (err) {
995 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000996 break;
Martin Panter84544c12016-07-23 03:02:07 +0000997 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000998 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
999 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001000 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001 PyErr_SetString(PyExc_MemoryError,
1002 "Can't allocate memory for compression object");
1003 goto error;
1004 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001005 zlib_error(state, self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006 goto error;
1007 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001008 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001009 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001010 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001011 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001012 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001013 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001014 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001015
1016 /* Mark it as being initialized */
1017 retval->is_initialised = 1;
1018
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001019 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020 return (PyObject *)retval;
1021
1022error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001023 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024 Py_XDECREF(retval);
1025 return NULL;
1026}
1027
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001028/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001029zlib.Compress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001030
1031 cls: defining_class
1032
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001033[clinic start generated code]*/
1034
1035static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001036zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
1037/*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001038{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001039 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001040}
1041
1042/*[clinic input]
1043zlib.Compress.__deepcopy__
1044
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001045 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001046 memo: object
1047 /
1048
1049[clinic start generated code]*/
1050
1051static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001052zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1053 PyObject *memo)
1054/*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001055{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001056 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001057}
1058
1059/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001060zlib.Decompress.copy
1061
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001062 cls: defining_class
1063
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001064Return a copy of the decompression object.
1065[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001066
1067static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001068zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
1069/*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001070{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001071 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001072
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001073 compobject *retval = newcompobject(state->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001074 if (!retval) return NULL;
1075
1076 /* Copy the zstream state
1077 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1078 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001079 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001080 int err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001081 switch (err) {
1082 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001083 break;
Martin Panter84544c12016-07-23 03:02:07 +00001084 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001085 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1086 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001087 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001088 PyErr_SetString(PyExc_MemoryError,
1089 "Can't allocate memory for decompression object");
1090 goto error;
1091 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001092 zlib_error(state, self->zst, err, "while copying decompression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001093 goto error;
1094 }
1095
1096 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001097 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001098 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001099 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001100 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001101 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001102 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001103
1104 /* Mark it as being initialized */
1105 retval->is_initialised = 1;
1106
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001107 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001108 return (PyObject *)retval;
1109
1110error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001111 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001112 Py_XDECREF(retval);
1113 return NULL;
1114}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001115
1116/*[clinic input]
1117zlib.Decompress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001118
1119 cls: defining_class
1120
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001121[clinic start generated code]*/
1122
1123static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001124zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
1125/*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001126{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001127 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001128}
1129
1130/*[clinic input]
1131zlib.Decompress.__deepcopy__
1132
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001133 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001134 memo: object
1135 /
1136
1137[clinic start generated code]*/
1138
1139static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001140zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1141 PyObject *memo)
1142/*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001143{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001144 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001145}
1146
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001147#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001148
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001149/*[clinic input]
1150zlib.Decompress.flush
1151
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001152 cls: defining_class
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001153 length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001154 the initial size of the output buffer.
1155 /
1156
1157Return a bytes object containing any remaining decompressed data.
1158[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001159
Guido van Rossumfb221561997-04-29 15:38:09 +00001160static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001161zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
1162 Py_ssize_t length)
1163/*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001164{
Martin Panter84544c12016-07-23 03:02:07 +00001165 int err, flush;
1166 Py_buffer data;
Ma Linf9bedb62021-04-28 14:58:54 +08001167 PyObject *RetVal;
Martin Panter84544c12016-07-23 03:02:07 +00001168 Py_ssize_t ibuflen;
Ma Linf9bedb62021-04-28 14:58:54 +08001169 _BlocksOutputBuffer buffer = {.list = NULL};
Tim Peters977e5402001-10-17 03:57:20 +00001170
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001171 PyObject *module = PyType_GetModule(cls);
1172 if (module == NULL) {
1173 return NULL;
1174 }
1175
1176 zlibstate *state = get_zlib_state(module);
1177
Martin Panter84544c12016-07-23 03:02:07 +00001178 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001179 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1180 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001181 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001182
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001183 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001184 return NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001185 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001186
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001187 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001188
Martin Panter84544c12016-07-23 03:02:07 +00001189 self->zst.next_in = data.buf;
1190 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001191
Ma Linf9bedb62021-04-28 14:58:54 +08001192 if (Buffer_InitWithSize(&buffer, length, &self->zst.next_out, &self->zst.avail_out) < 0) {
1193 goto abort;
1194 }
1195
Martin Panter84544c12016-07-23 03:02:07 +00001196 do {
1197 arrange_input_buffer(&self->zst, &ibuflen);
1198 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001199
Martin Panter84544c12016-07-23 03:02:07 +00001200 do {
Ma Linf9bedb62021-04-28 14:58:54 +08001201 if (self->zst.avail_out == 0) {
1202 if (Buffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0)
1203 goto abort;
1204 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001205
Martin Panter84544c12016-07-23 03:02:07 +00001206 Py_BEGIN_ALLOW_THREADS
1207 err = inflate(&self->zst, flush);
1208 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001209
Martin Panter84544c12016-07-23 03:02:07 +00001210 switch (err) {
1211 case Z_OK: /* fall through */
1212 case Z_BUF_ERROR: /* fall through */
1213 case Z_STREAM_END:
1214 break;
1215 default:
1216 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001217 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001218 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001219 }
Martin Panter84544c12016-07-23 03:02:07 +00001220 else
1221 break;
1222 }
1223 goto save;
1224 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001225
Martin Panter84544c12016-07-23 03:02:07 +00001226 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1227
1228 } while (err != Z_STREAM_END && ibuflen != 0);
1229
1230 save:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001231 if (save_unconsumed_input(self, &data, err) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001232 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001233 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001234
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001235 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001236 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001237 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001238 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001239 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001240 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001241 zlib_error(state, self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001242 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001243 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001244 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001245
Ma Linf9bedb62021-04-28 14:58:54 +08001246 RetVal = Buffer_Finish(&buffer, self->zst.avail_out);
1247 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +00001248 goto success;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001249 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001250
Martin Panter84544c12016-07-23 03:02:07 +00001251 abort:
Ma Linf9bedb62021-04-28 14:58:54 +08001252 Buffer_OnError(&buffer);
1253 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +00001254 success:
1255 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001256 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001257 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001258}
1259
Christian Heimes936e2f32014-01-27 01:06:57 +01001260#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001261
Guido van Rossumfb221561997-04-29 15:38:09 +00001262static PyMethodDef comp_methods[] =
1263{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001264 ZLIB_COMPRESS_COMPRESS_METHODDEF
1265 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001266 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001267 ZLIB_COMPRESS___COPY___METHODDEF
1268 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001269 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001270};
1271
1272static PyMethodDef Decomp_methods[] =
1273{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001274 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001275 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001276 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001277 ZLIB_DECOMPRESS___COPY___METHODDEF
1278 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001279 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001280};
1281
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001282#define COMP_OFF(x) offsetof(compobject, x)
1283static PyMemberDef Decomp_members[] = {
1284 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1285 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001286 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001287 {NULL},
1288};
Guido van Rossumfb221561997-04-29 15:38:09 +00001289
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001290/*[clinic input]
1291zlib.adler32
1292
1293 data: Py_buffer
1294 value: unsigned_int(bitwise=True) = 1
1295 Starting value of the checksum.
1296 /
1297
1298Compute an Adler-32 checksum of data.
1299
1300The returned checksum is an integer.
1301[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001302
Guido van Rossumfb221561997-04-29 15:38:09 +00001303static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001304zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1305/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001306{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001307 /* Releasing the GIL for very small buffers is inefficient
1308 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001309 if (data->len > 1024*5) {
1310 unsigned char *buf = data->buf;
1311 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001312
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001313 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001314 /* Avoid truncation of length for very large buffers. adler32() takes
1315 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001316 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001317 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001318 buf += (size_t) UINT_MAX;
1319 len -= (size_t) UINT_MAX;
1320 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001321 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001322 Py_END_ALLOW_THREADS
1323 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001324 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001325 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001326 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001327}
Tim Peters977e5402001-10-17 03:57:20 +00001328
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001329/*[clinic input]
1330zlib.crc32
1331
1332 data: Py_buffer
1333 value: unsigned_int(bitwise=True) = 0
1334 Starting value of the checksum.
1335 /
1336
1337Compute a CRC-32 checksum of data.
1338
1339The returned checksum is an integer.
1340[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001341
1342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001343zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1344/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001345{
Martin v. Löwis423be952008-08-13 15:53:07 +00001346 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001347
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001348 /* Releasing the GIL for very small buffers is inefficient
1349 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001350 if (data->len > 1024*5) {
1351 unsigned char *buf = data->buf;
1352 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001353
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001354 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001355 /* Avoid truncation of length for very large buffers. crc32() takes
1356 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001357 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001358 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001359 buf += (size_t) UINT_MAX;
1360 len -= (size_t) UINT_MAX;
1361 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001362 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001363 Py_END_ALLOW_THREADS
1364 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001365 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001366 }
Christian Heimescc47b052008-03-25 14:56:36 +00001367 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001368}
Tim Peters977e5402001-10-17 03:57:20 +00001369
Guido van Rossumfb221561997-04-29 15:38:09 +00001370
1371static PyMethodDef zlib_methods[] =
1372{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001373 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001374 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001375 ZLIB_COMPRESSOBJ_METHODDEF
1376 ZLIB_CRC32_METHODDEF
1377 ZLIB_DECOMPRESS_METHODDEF
1378 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001379 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001380};
1381
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001382static PyType_Slot Comptype_slots[] = {
1383 {Py_tp_dealloc, Comp_dealloc},
1384 {Py_tp_methods, comp_methods},
1385 {0, 0},
1386};
1387
1388static PyType_Spec Comptype_spec = {
Erlend Egeberg Aasland9746cda2021-04-30 16:04:57 +02001389 .name = "zlib.Compress",
1390 .basicsize = sizeof(compobject),
1391 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1392 .slots= Comptype_slots,
Guido van Rossumfb221561997-04-29 15:38:09 +00001393};
1394
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001395static PyType_Slot Decomptype_slots[] = {
1396 {Py_tp_dealloc, Decomp_dealloc},
1397 {Py_tp_methods, Decomp_methods},
1398 {Py_tp_members, Decomp_members},
1399 {0, 0},
1400};
1401
1402static PyType_Spec Decomptype_spec = {
Erlend Egeberg Aasland9746cda2021-04-30 16:04:57 +02001403 .name = "zlib.Decompress",
1404 .basicsize = sizeof(compobject),
1405 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1406 .slots = Decomptype_slots,
Guido van Rossumfb221561997-04-29 15:38:09 +00001407};
1408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001409PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001410"The functions in this module allow compression and decompression using the\n"
1411"zlib library, which is based on GNU zip.\n"
1412"\n"
1413"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001414"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001415"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001416"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001417"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001418"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001419"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001420"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001421"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001422"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001423
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001424static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001425zlib_clear(PyObject *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001426{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001427 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001428 Py_CLEAR(state->Comptype);
1429 Py_CLEAR(state->Decomptype);
1430 Py_CLEAR(state->ZlibError);
1431 return 0;
1432}
1433
1434static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001435zlib_traverse(PyObject *mod, visitproc visit, void *arg)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001436{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001437 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001438 Py_VISIT(state->Comptype);
1439 Py_VISIT(state->Decomptype);
1440 Py_VISIT(state->ZlibError);
1441 return 0;
1442}
1443
1444static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001445zlib_free(void *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001446{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001447 zlib_clear((PyObject *)mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001448}
1449
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001450static int
1451zlib_exec(PyObject *mod)
1452{
1453 zlibstate *state = get_zlib_state(mod);
1454
1455 state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1456 mod, &Comptype_spec, NULL);
1457 if (state->Comptype == NULL) {
1458 return -1;
1459 }
1460
1461 state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1462 mod, &Decomptype_spec, NULL);
1463 if (state->Decomptype == NULL) {
1464 return -1;
1465 }
1466
1467 state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1468 if (state->ZlibError == NULL) {
1469 return -1;
1470 }
1471
1472 Py_INCREF(state->ZlibError);
1473 if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
1474 Py_DECREF(state->ZlibError);
1475 return -1;
1476 }
1477
1478#define ZLIB_ADD_INT_MACRO(c) \
1479 do { \
1480 if ((PyModule_AddIntConstant(mod, #c, c)) < 0) { \
1481 return -1; \
1482 } \
1483 } while(0)
1484
1485 ZLIB_ADD_INT_MACRO(MAX_WBITS);
1486 ZLIB_ADD_INT_MACRO(DEFLATED);
1487 ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
1488 ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
1489 // compression levels
1490 ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
1491 ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
1492 ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
1493 ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
1494 // compression strategies
1495 ZLIB_ADD_INT_MACRO(Z_FILTERED);
1496 ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
1497#ifdef Z_RLE // 1.2.0.1
1498 ZLIB_ADD_INT_MACRO(Z_RLE);
1499#endif
1500#ifdef Z_FIXED // 1.2.2.2
1501 ZLIB_ADD_INT_MACRO(Z_FIXED);
1502#endif
1503 ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
1504 // allowed flush values
1505 ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
1506 ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
1507 ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
1508 ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
1509 ZLIB_ADD_INT_MACRO(Z_FINISH);
1510#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1511 ZLIB_ADD_INT_MACRO(Z_BLOCK);
1512#endif
1513#ifdef Z_TREES // 1.2.3.4, only for inflate
1514 ZLIB_ADD_INT_MACRO(Z_TREES);
1515#endif
1516 PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
1517 if (ver == NULL) {
1518 return -1;
1519 }
1520
1521 if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
1522 Py_DECREF(ver);
1523 return -1;
1524 }
1525
1526 ver = PyUnicode_FromString(zlibVersion());
1527 if (ver == NULL) {
1528 return -1;
1529 }
1530
1531 if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
1532 Py_DECREF(ver);
1533 return -1;
1534 }
1535
1536 if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
1537 return -1;
1538 }
1539 return 0;
1540}
1541
1542static PyModuleDef_Slot zlib_slots[] = {
1543 {Py_mod_exec, zlib_exec},
1544 {0, NULL}
1545};
1546
Martin v. Löwis1a214512008-06-11 05:26:20 +00001547static struct PyModuleDef zlibmodule = {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001548 PyModuleDef_HEAD_INIT,
1549 .m_name = "zlib",
1550 .m_doc = zlib_module_documentation,
1551 .m_size = sizeof(zlibstate),
1552 .m_methods = zlib_methods,
1553 .m_slots = zlib_slots,
1554 .m_traverse = zlib_traverse,
1555 .m_clear = zlib_clear,
1556 .m_free = zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001557};
1558
Mark Hammond62b1ab12002-07-23 06:31:15 +00001559PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001560PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001561{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001562 return PyModuleDef_Init(&zlibmodule);
Guido van Rossumfb221561997-04-29 15:38:09 +00001563}