blob: a6940f2fd43a3fc3bc1984c69b5145234896a477 [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_Grow(_BlocksOutputBuffer *buffer,
37 Bytef **next_out, uint32_t *avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080038{
39 Py_ssize_t allocated;
40
41 allocated = _BlocksOutputBuffer_Grow(
42 buffer, (void**) next_out, (Py_ssize_t) *avail_out);
43 *avail_out = (uint32_t) allocated;
44 return allocated;
45}
46
47static inline Py_ssize_t
Ma Lin251ffa92021-05-01 07:32:49 +080048OutputBuffer_GetDataSize(_BlocksOutputBuffer *buffer, uint32_t avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080049{
50 return _BlocksOutputBuffer_GetDataSize(buffer, (Py_ssize_t) avail_out);
51}
52
53static inline PyObject *
Ma Lin251ffa92021-05-01 07:32:49 +080054OutputBuffer_Finish(_BlocksOutputBuffer *buffer, uint32_t avail_out)
Ma Linf9bedb62021-04-28 14:58:54 +080055{
56 return _BlocksOutputBuffer_Finish(buffer, (Py_ssize_t) avail_out);
57}
58
59static inline void
Ma Lin251ffa92021-05-01 07:32:49 +080060OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
Ma Linf9bedb62021-04-28 14:58:54 +080061{
62 _BlocksOutputBuffer_OnError(buffer);
63}
64
Miss Islington (bot)22bcc072021-07-04 18:32:56 -070065/* The max buffer size accepted by zlib is UINT32_MAX, the initial buffer size
66 `init_size` may > it in 64-bit build. These wrapper functions maintain an
67 UINT32_MAX sliding window for the first block:
68 1. OutputBuffer_WindowInitWithSize()
69 2. OutputBuffer_WindowGrow()
70 3. OutputBuffer_WindowFinish()
71 4. OutputBuffer_WindowOnError()
72
73 ==== is the sliding window:
74 1. ====------
75 ^ next_posi, left_bytes is 6
76 2. ----====--
77 ^ next_posi, left_bytes is 2
78 3. --------==
79 ^ next_posi, left_bytes is 0 */
80typedef struct {
81 Py_ssize_t left_bytes;
82 Bytef *next_posi;
83} _Uint32Window;
84
Miss Islington (bot)5afc5bb2021-10-07 01:55:18 -070085/* Initialize the buffer with an initial buffer size.
Miss Islington (bot)22bcc072021-07-04 18:32:56 -070086
87 On success, return value >= 0
88 On failure, return value < 0 */
89static inline Py_ssize_t
90OutputBuffer_WindowInitWithSize(_BlocksOutputBuffer *buffer, _Uint32Window *window,
91 Py_ssize_t init_size,
92 Bytef **next_out, uint32_t *avail_out)
93{
94 Py_ssize_t allocated = _BlocksOutputBuffer_InitWithSize(
95 buffer, init_size, (void**) next_out);
96
97 if (allocated >= 0) {
98 // the UINT32_MAX sliding window
99 Py_ssize_t window_size = Py_MIN((size_t)allocated, UINT32_MAX);
100 *avail_out = (uint32_t) window_size;
101
102 window->left_bytes = allocated - window_size;
103 window->next_posi = *next_out + window_size;
104 }
105 return allocated;
106}
107
108/* Grow the buffer.
109
110 On success, return value >= 0
111 On failure, return value < 0 */
112static inline Py_ssize_t
113OutputBuffer_WindowGrow(_BlocksOutputBuffer *buffer, _Uint32Window *window,
114 Bytef **next_out, uint32_t *avail_out)
115{
116 Py_ssize_t allocated;
117
118 /* ensure no gaps in the data.
119 if inlined, this check could be optimized away.*/
120 if (*avail_out != 0) {
121 PyErr_SetString(PyExc_SystemError,
122 "*avail_out != 0 in OutputBuffer_WindowGrow().");
123 return -1;
124 }
125
126 // slide the UINT32_MAX sliding window
127 if (window->left_bytes > 0) {
128 Py_ssize_t window_size = Py_MIN((size_t)window->left_bytes, UINT32_MAX);
129
130 *next_out = window->next_posi;
131 *avail_out = (uint32_t) window_size;
132
133 window->left_bytes -= window_size;
134 window->next_posi += window_size;
135
136 return window_size;
137 }
138 assert(window->left_bytes == 0);
139
140 // only the first block may > UINT32_MAX
141 allocated = _BlocksOutputBuffer_Grow(
142 buffer, (void**) next_out, (Py_ssize_t) *avail_out);
143 *avail_out = (uint32_t) allocated;
144 return allocated;
145}
146
147/* Finish the buffer.
148
149 On success, return a bytes object
150 On failure, return NULL */
151static inline PyObject *
152OutputBuffer_WindowFinish(_BlocksOutputBuffer *buffer, _Uint32Window *window,
153 uint32_t avail_out)
154{
155 Py_ssize_t real_avail_out = (Py_ssize_t) avail_out + window->left_bytes;
156 return _BlocksOutputBuffer_Finish(buffer, real_avail_out);
157}
158
159static inline void
160OutputBuffer_WindowOnError(_BlocksOutputBuffer *buffer, _Uint32Window *window)
161{
162 _BlocksOutputBuffer_OnError(buffer);
163}
164
Larry Hastings31826802013-10-19 00:09:25 -0700165
Ma Lin93f41182021-04-27 16:37:11 +0800166#define ENTER_ZLIB(obj) do { \
167 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
168 Py_BEGIN_ALLOW_THREADS \
169 PyThread_acquire_lock((obj)->lock, 1); \
170 Py_END_ALLOW_THREADS \
171 } } while (0)
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200172#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000173
Martin Panter3f0ee832016-06-05 10:48:34 +0000174#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +0000175# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +0000176#endif
177
Guido van Rossumfb221561997-04-29 15:38:09 +0000178/* The following parameters are copied from zutil.h, version 0.95 */
179#define DEFLATED 8
180#if MAX_MEM_LEVEL >= 8
181# define DEF_MEM_LEVEL 8
182#else
183# define DEF_MEM_LEVEL MAX_MEM_LEVEL
184#endif
Guido van Rossumfb221561997-04-29 15:38:09 +0000185
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200186/* Initial buffer size. */
187#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +0000188
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100189static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +0000190
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100191typedef struct {
192 PyTypeObject *Comptype;
193 PyTypeObject *Decomptype;
194 PyObject *ZlibError;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500195} zlibstate;
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100196
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500197static inline zlibstate*
Hai Shif707d942020-03-16 21:15:01 +0800198get_zlib_state(PyObject *module)
199{
200 void *state = PyModule_GetState(module);
201 assert(state != NULL);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500202 return (zlibstate *)state;
Hai Shif707d942020-03-16 21:15:01 +0800203}
204
Tim Peters977e5402001-10-17 03:57:20 +0000205typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +0000206{
Jeremy Hylton9714f992001-10-16 21:19:45 +0000207 PyObject_HEAD
208 z_stream zst;
209 PyObject *unused_data;
210 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200211 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000212 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200213 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200214 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +0000215} compobject;
216
Jeremy Hylton0965e082001-10-16 21:56:09 +0000217static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500218zlib_error(zlibstate *state, z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +0000219{
Nadeem Vawda524148a2011-08-28 11:26:46 +0200220 const char *zmsg = Z_NULL;
221 /* In case of a version mismatch, zst.msg won't be initialized.
222 Check for this case first, before looking at zst.msg. */
223 if (err == Z_VERSION_ERROR)
224 zmsg = "library version mismatch";
225 if (zmsg == Z_NULL)
226 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000227 if (zmsg == Z_NULL) {
228 switch (err) {
229 case Z_BUF_ERROR:
230 zmsg = "incomplete or truncated stream";
231 break;
232 case Z_STREAM_ERROR:
233 zmsg = "inconsistent stream state";
234 break;
235 case Z_DATA_ERROR:
236 zmsg = "invalid input data";
237 break;
238 }
239 }
240 if (zmsg == Z_NULL)
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500241 PyErr_Format(state->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000242 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500243 PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +0000244}
245
Larry Hastings61272b72014-01-07 12:41:53 -0800246/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800247module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -0800248class zlib.Compress "compobject *" "&Comptype"
249class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -0800250[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300251/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800252
Guido van Rossumfb221561997-04-29 15:38:09 +0000253static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000254newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000255{
Tim Peters977e5402001-10-17 03:57:20 +0000256 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000257 self = PyObject_New(compobject, type);
258 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200260 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000261 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200262 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000263 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000264 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 Py_DECREF(self);
266 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000267 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000268 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000269 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000270 Py_DECREF(self);
271 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000272 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000273 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200274 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000275 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200276 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
277 return NULL;
278 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000279 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000280}
281
Victor Stinner5064a522013-07-07 16:50:27 +0200282static void*
283PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
284{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300285 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200286 return NULL;
287 /* PyMem_Malloc() cannot be used: the GIL is not held when
288 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300289 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200290}
291
292static void
293PyZlib_Free(voidpf ctx, void *ptr)
294{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200295 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200296}
297
Martin Panter84544c12016-07-23 03:02:07 +0000298static void
299arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
300{
Segev Finer679b5662017-07-27 01:17:57 +0300301 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000302 *remains -= zst->avail_in;
303}
304
Larry Hastings61272b72014-01-07 12:41:53 -0800305/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800306zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200307
Martin Panter1fe0d132016-02-10 10:06:36 +0000308 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800309 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300310 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200311 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000312 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800313
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200314Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800315[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800316
Guido van Rossumfb221561997-04-29 15:38:09 +0000317static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300318zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
319/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000320{
Ma Linf9bedb62021-04-28 14:58:54 +0800321 PyObject *RetVal;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500322 int flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000323 z_stream zst;
Ma Linf9bedb62021-04-28 14:58:54 +0800324 _BlocksOutputBuffer buffer = {.list = NULL};
Tim Peters977e5402001-10-17 03:57:20 +0000325
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500326 zlibstate *state = get_zlib_state(module);
327
328 Byte *ibuf = data->buf;
329 Py_ssize_t ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000330
Ma Lin251ffa92021-05-01 07:32:49 +0800331 if (OutputBuffer_InitAndGrow(&buffer, -1, &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800332 goto error;
333 }
334
Victor Stinner5064a522013-07-07 16:50:27 +0200335 zst.opaque = NULL;
336 zst.zalloc = PyZlib_Malloc;
337 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000338 zst.next_in = ibuf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500339 int err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000340
Martin Panter84544c12016-07-23 03:02:07 +0000341 switch (err) {
342 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000343 break;
Martin Panter84544c12016-07-23 03:02:07 +0000344 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000345 PyErr_SetString(PyExc_MemoryError,
346 "Out of memory while compressing data");
347 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000348 case Z_STREAM_ERROR:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500349 PyErr_SetString(state->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000350 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000351 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000352 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500353 zlib_error(state, zst, err, "while compressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000354 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000355 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000356
Martin Panter84544c12016-07-23 03:02:07 +0000357 do {
358 arrange_input_buffer(&zst, &ibuflen);
359 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000360
Martin Panter84544c12016-07-23 03:02:07 +0000361 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800362 if (zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800363 if (OutputBuffer_Grow(&buffer, &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800364 deflateEnd(&zst);
365 goto error;
366 }
Martin Panter84544c12016-07-23 03:02:07 +0000367 }
368
369 Py_BEGIN_ALLOW_THREADS
370 err = deflate(&zst, flush);
371 Py_END_ALLOW_THREADS
372
373 if (err == Z_STREAM_ERROR) {
374 deflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500375 zlib_error(state, zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000376 goto error;
377 }
378
379 } while (zst.avail_out == 0);
380 assert(zst.avail_in == 0);
381
382 } while (flush != Z_FINISH);
383 assert(err == Z_STREAM_END);
384
385 err = deflateEnd(&zst);
386 if (err == Z_OK) {
Ma Lin251ffa92021-05-01 07:32:49 +0800387 RetVal = OutputBuffer_Finish(&buffer, zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800388 if (RetVal == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000389 goto error;
Ma Linf9bedb62021-04-28 14:58:54 +0800390 }
Martin Panter84544c12016-07-23 03:02:07 +0000391 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000392 }
Tim Peters977e5402001-10-17 03:57:20 +0000393 else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500394 zlib_error(state, zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000395 error:
Ma Lin251ffa92021-05-01 07:32:49 +0800396 OutputBuffer_OnError(&buffer);
Martin Panter84544c12016-07-23 03:02:07 +0000397 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000398}
399
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200400/*[clinic input]
401zlib.decompress
402
403 data: Py_buffer
404 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300405 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200406 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000407 The window buffer size and container format.
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300408 bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200409 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200410
411Returns a bytes object containing the uncompressed data.
412[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000413
Guido van Rossumfb221561997-04-29 15:38:09 +0000414static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300415zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000416 Py_ssize_t bufsize)
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300417/*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000418{
Ma Linf9bedb62021-04-28 14:58:54 +0800419 PyObject *RetVal;
Martin Panter84544c12016-07-23 03:02:07 +0000420 Byte *ibuf;
421 Py_ssize_t ibuflen;
422 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000423 z_stream zst;
Ma Linf9bedb62021-04-28 14:58:54 +0800424 _BlocksOutputBuffer buffer = {.list = NULL};
Miss Islington (bot)22bcc072021-07-04 18:32:56 -0700425 _Uint32Window window; // output buffer's UINT32_MAX sliding window
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000426
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500427 zlibstate *state = get_zlib_state(module);
428
Martin Panter84544c12016-07-23 03:02:07 +0000429 if (bufsize < 0) {
430 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
431 return NULL;
432 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100433 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000434 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000435
Miss Islington (bot)22bcc072021-07-04 18:32:56 -0700436 if (OutputBuffer_WindowInitWithSize(&buffer, &window, bufsize,
437 &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800438 goto error;
439 }
440
Martin Panter84544c12016-07-23 03:02:07 +0000441 ibuf = data->buf;
442 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000443
Victor Stinner5064a522013-07-07 16:50:27 +0200444 zst.opaque = NULL;
445 zst.zalloc = PyZlib_Malloc;
446 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000447 zst.avail_in = 0;
448 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200449 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000450
Martin Panter84544c12016-07-23 03:02:07 +0000451 switch (err) {
452 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000453 break;
Martin Panter84544c12016-07-23 03:02:07 +0000454 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 PyErr_SetString(PyExc_MemoryError,
456 "Out of memory while decompressing data");
457 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000458 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000459 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500460 zlib_error(state, zst, err, "while preparing to decompress data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000461 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000462 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000463
Jeremy Hylton9714f992001-10-16 21:19:45 +0000464 do {
Martin Panter84544c12016-07-23 03:02:07 +0000465 arrange_input_buffer(&zst, &ibuflen);
466 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000467
Martin Panter84544c12016-07-23 03:02:07 +0000468 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800469 if (zst.avail_out == 0) {
Miss Islington (bot)22bcc072021-07-04 18:32:56 -0700470 if (OutputBuffer_WindowGrow(&buffer, &window,
471 &zst.next_out, &zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800472 inflateEnd(&zst);
473 goto error;
474 }
Martin Panter84544c12016-07-23 03:02:07 +0000475 }
476
477 Py_BEGIN_ALLOW_THREADS
478 err = inflate(&zst, flush);
479 Py_END_ALLOW_THREADS
480
481 switch (err) {
482 case Z_OK: /* fall through */
483 case Z_BUF_ERROR: /* fall through */
484 case Z_STREAM_END:
485 break;
486 case Z_MEM_ERROR:
487 inflateEnd(&zst);
488 PyErr_SetString(PyExc_MemoryError,
489 "Out of memory while decompressing data");
490 goto error;
491 default:
492 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500493 zlib_error(state, zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000494 goto error;
495 }
Martin Panter84544c12016-07-23 03:02:07 +0000496
497 } while (zst.avail_out == 0);
498
499 } while (err != Z_STREAM_END && ibuflen != 0);
500
501
502 if (err != Z_STREAM_END) {
503 inflateEnd(&zst);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500504 zlib_error(state, zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000505 goto error;
506 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000507
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000508 err = inflateEnd(&zst);
509 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500510 zlib_error(state, zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000511 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000512 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000513
Miss Islington (bot)22bcc072021-07-04 18:32:56 -0700514 RetVal = OutputBuffer_WindowFinish(&buffer, &window, zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800515 if (RetVal != NULL) {
516 return RetVal;
517 }
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000518
519 error:
Miss Islington (bot)22bcc072021-07-04 18:32:56 -0700520 OutputBuffer_WindowOnError(&buffer, &window);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000521 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000522}
523
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200524/*[clinic input]
525zlib.compressobj
526
527 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000528 The compression level (an integer in the range 0-9 or -1; default is
529 currently equivalent to 6). Higher compression levels are slower,
530 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200531 method: int(c_default="DEFLATED") = DEFLATED
532 The compression algorithm. If given, this must be DEFLATED.
533 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000534 +9 to +15: The base-two logarithm of the window size. Include a zlib
535 container.
536 -9 to -15: Generate a raw stream.
537 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200538 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
539 Controls the amount of memory used for internal compression state.
540 Valid values range from 1 to 9. Higher values result in higher memory
541 usage, faster compression, and smaller output.
542 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
543 Used to tune the compression algorithm. Possible values are
544 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
545 zdict: Py_buffer = None
546 The predefined compression dictionary - a sequence of bytes
547 containing subsequences that are likely to occur in the input data.
548
549Return a compressor object.
550[clinic start generated code]*/
551
Guido van Rossumfb221561997-04-29 15:38:09 +0000552static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300553zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400554 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300555/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000556{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500557 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200558 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100559 PyErr_SetString(PyExc_OverflowError,
560 "zdict length does not fit in an unsigned int");
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500561 return NULL;
Victor Stinnere079edd2013-11-21 22:33:21 +0100562 }
563
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500564 compobject *self = newcompobject(state->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000565 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200566 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200567 self->zst.opaque = NULL;
568 self->zst.zalloc = PyZlib_Malloc;
569 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000570 self->zst.next_in = NULL;
571 self->zst.avail_in = 0;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500572 int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000573 switch (err) {
574 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200576 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200577 goto success;
578 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100579 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200580 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200581 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000582 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200583 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000584 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200585 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
586 goto error;
587 default:
588 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
589 goto error;
590 }
591 }
Martin Panter84544c12016-07-23 03:02:07 +0000592 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000593 PyErr_SetString(PyExc_MemoryError,
594 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200595 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000596 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200598 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000599 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500600 zlib_error(state, self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200601 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000602 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200603
604 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200605 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200606 success:
Martin Panter84544c12016-07-23 03:02:07 +0000607 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000608}
609
Martin Panter3f0ee832016-06-05 10:48:34 +0000610static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500611set_inflate_zdict(zlibstate *state, compobject *self)
Martin Panter3f0ee832016-06-05 10:48:34 +0000612{
613 Py_buffer zdict_buf;
Martin Panter3f0ee832016-06-05 10:48:34 +0000614 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
615 return -1;
616 }
617 if ((size_t)zdict_buf.len > UINT_MAX) {
618 PyErr_SetString(PyExc_OverflowError,
619 "zdict length does not fit in an unsigned int");
620 PyBuffer_Release(&zdict_buf);
621 return -1;
622 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500623 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000624 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000625 zdict_buf.buf, (unsigned int)zdict_buf.len);
626 PyBuffer_Release(&zdict_buf);
627 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500628 zlib_error(state, self->zst, err, "while setting zdict");
Martin Panter3f0ee832016-06-05 10:48:34 +0000629 return -1;
630 }
631 return 0;
632}
633
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200634/*[clinic input]
635zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200636
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200637 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000638 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200639 zdict: object(c_default="NULL") = b''
640 The predefined compression dictionary. This must be the same
641 dictionary as used by the compressor that produced the input data.
642
643Return a decompressor object.
644[clinic start generated code]*/
645
646static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300647zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
648/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200649{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500650 zlibstate *state = get_zlib_state(module);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200651
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200652 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
653 PyErr_SetString(PyExc_TypeError,
654 "zdict argument must support the buffer protocol");
655 return NULL;
656 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000657
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500658 compobject *self = newcompobject(state->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000659 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000660 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200661 self->zst.opaque = NULL;
662 self->zst.zalloc = PyZlib_Malloc;
663 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000664 self->zst.next_in = NULL;
665 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200666 if (zdict != NULL) {
667 Py_INCREF(zdict);
668 self->zdict = zdict;
669 }
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500670 int err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000671 switch (err) {
672 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000673 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000674 if (self->zdict != NULL && wbits < 0) {
675#ifdef AT_LEAST_ZLIB_1_2_2_1
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500676 if (set_inflate_zdict(state, self) < 0) {
Martin Panter3f0ee832016-06-05 10:48:34 +0000677 Py_DECREF(self);
678 return NULL;
679 }
680#else
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500681 PyErr_Format(state->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000682 "zlib version %s does not allow raw inflate with dictionary",
683 ZLIB_VERSION);
684 Py_DECREF(self);
685 return NULL;
686#endif
687 }
Martin Panter84544c12016-07-23 03:02:07 +0000688 return (PyObject *)self;
689 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000690 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000691 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
692 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000693 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000694 Py_DECREF(self);
695 PyErr_SetString(PyExc_MemoryError,
696 "Can't allocate memory for decompression object");
697 return NULL;
698 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500699 zlib_error(state, self->zst, err, "while creating decompression object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000700 Py_DECREF(self);
701 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000702 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000703}
704
705static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000706Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000707{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100708 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000709 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000710 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000711 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200712 Py_XDECREF(self->zdict);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100713 PyObject_Free(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100714 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000715}
716
717static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000718Comp_dealloc(compobject *self)
719{
720 if (self->is_initialised)
721 deflateEnd(&self->zst);
722 Dealloc(self);
723}
724
725static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000726Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000727{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000728 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000729 inflateEnd(&self->zst);
730 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000731}
732
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200733/*[clinic input]
734zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000735
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500736 cls: defining_class
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200737 data: Py_buffer
738 Binary data to be compressed.
739 /
740
741Returns a bytes object containing compressed data.
742
743After calling this function, some of the input data may still
744be stored in internal buffers for later processing.
745Call the flush() method to clear these buffers.
746[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000747
Guido van Rossumfb221561997-04-29 15:38:09 +0000748static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500749zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
750 Py_buffer *data)
751/*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000752{
Ma Linf9bedb62021-04-28 14:58:54 +0800753 PyObject *RetVal;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200754 int err;
Ma Linf9bedb62021-04-28 14:58:54 +0800755 _BlocksOutputBuffer buffer = {.list = NULL};
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500756 zlibstate *state = PyType_GetModuleState(cls);
757
Ma Lin93f41182021-04-27 16:37:11 +0800758 ENTER_ZLIB(self);
759
Martin Panter84544c12016-07-23 03:02:07 +0000760 self->zst.next_in = data->buf;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500761 Py_ssize_t ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000762
Ma Lin251ffa92021-05-01 07:32:49 +0800763 if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800764 goto error;
765 }
766
Martin Panter84544c12016-07-23 03:02:07 +0000767 do {
768 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000769
Martin Panter84544c12016-07-23 03:02:07 +0000770 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800771 if (self->zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800772 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800773 goto error;
Ma Lin251ffa92021-05-01 07:32:49 +0800774 }
Ma Linf9bedb62021-04-28 14:58:54 +0800775 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000776
Martin Panter84544c12016-07-23 03:02:07 +0000777 Py_BEGIN_ALLOW_THREADS
778 err = deflate(&self->zst, Z_NO_FLUSH);
779 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000780
Martin Panter84544c12016-07-23 03:02:07 +0000781 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500782 zlib_error(state, self->zst, err, "while compressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000783 goto error;
784 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000785
Martin Panter84544c12016-07-23 03:02:07 +0000786 } while (self->zst.avail_out == 0);
787 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000788
Martin Panter84544c12016-07-23 03:02:07 +0000789 } while (ibuflen != 0);
790
Ma Lin251ffa92021-05-01 07:32:49 +0800791 RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800792 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000793 goto success;
Ma Linf9bedb62021-04-28 14:58:54 +0800794 }
Martin Panter84544c12016-07-23 03:02:07 +0000795
796 error:
Ma Lin251ffa92021-05-01 07:32:49 +0800797 OutputBuffer_OnError(&buffer);
Ma Linf9bedb62021-04-28 14:58:54 +0800798 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000799 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000800 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000801 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000802}
803
Martin Panter84544c12016-07-23 03:02:07 +0000804/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100805 self->unused_data or self->unconsumed_tail, as appropriate. */
806static int
Martin Panter84544c12016-07-23 03:02:07 +0000807save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100808{
809 if (err == Z_STREAM_END) {
810 /* The end of the compressed data has been reached. Store the leftover
811 input data in self->unused_data. */
812 if (self->zst.avail_in > 0) {
813 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000814 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100815 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000816 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
817 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100818 PyErr_NoMemory();
819 return -1;
820 }
Martin Panter84544c12016-07-23 03:02:07 +0000821 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100822 new_data = PyBytes_FromStringAndSize(NULL, new_size);
823 if (new_data == NULL)
824 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200825 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100826 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200827 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000828 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300829 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100830 self->zst.avail_in = 0;
831 }
832 }
Martin Panter84544c12016-07-23 03:02:07 +0000833
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100834 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
835 /* This code handles two distinct cases:
836 1. Output limit was reached. Save leftover input in unconsumed_tail.
837 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000838 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100839 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000840 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100841 if (new_data == NULL)
842 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300843 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100844 }
Martin Panter84544c12016-07-23 03:02:07 +0000845
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100846 return 0;
847}
848
Larry Hastings61272b72014-01-07 12:41:53 -0800849/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800850zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700851
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500852 cls: defining_class
Larry Hastings31826802013-10-19 00:09:25 -0700853 data: Py_buffer
854 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300855 /
Serhiy Storchaka578c3952020-05-26 18:43:38 +0300856 max_length: Py_ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700857 The maximum allowable length of the decompressed data.
858 Unconsumed input data will be stored in
859 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700860
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200861Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700862
863After calling this function, some of the input data may still be stored in
864internal buffers for later processing.
865Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800866[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700867
Larry Hastings31826802013-10-19 00:09:25 -0700868static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500869zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
870 Py_buffer *data, Py_ssize_t max_length)
871/*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/
Larry Hastings31826802013-10-19 00:09:25 -0700872{
Martin Panter84544c12016-07-23 03:02:07 +0000873 int err = Z_OK;
Ma Linf9bedb62021-04-28 14:58:54 +0800874 Py_ssize_t ibuflen;
875 PyObject *RetVal;
876 _BlocksOutputBuffer buffer = {.list = NULL};
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000877
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500878 PyObject *module = PyType_GetModule(cls);
879 if (module == NULL)
880 return NULL;
881
882 zlibstate *state = get_zlib_state(module);
Martin Panter84544c12016-07-23 03:02:07 +0000883 if (max_length < 0) {
884 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700885 return NULL;
Ma Lin251ffa92021-05-01 07:32:49 +0800886 } else if (max_length == 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800887 max_length = -1;
Ma Lin251ffa92021-05-01 07:32:49 +0800888 }
Martin Panter84544c12016-07-23 03:02:07 +0000889
Ma Lin93f41182021-04-27 16:37:11 +0800890 ENTER_ZLIB(self);
891
Martin Panter84544c12016-07-23 03:02:07 +0000892 self->zst.next_in = data->buf;
893 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894
Ma Lin251ffa92021-05-01 07:32:49 +0800895 if (OutputBuffer_InitAndGrow(&buffer, max_length, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800896 goto abort;
897 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000898
Martin Panter84544c12016-07-23 03:02:07 +0000899 do {
900 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000901
Martin Panter84544c12016-07-23 03:02:07 +0000902 do {
Ma Linf9bedb62021-04-28 14:58:54 +0800903 if (self->zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +0800904 if (OutputBuffer_GetDataSize(&buffer, self->zst.avail_out) == max_length) {
Martin Panter84544c12016-07-23 03:02:07 +0000905 goto save;
906 }
Ma Lin251ffa92021-05-01 07:32:49 +0800907 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +0800908 goto abort;
909 }
Martin Panter84544c12016-07-23 03:02:07 +0000910 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000911
Martin Panter84544c12016-07-23 03:02:07 +0000912 Py_BEGIN_ALLOW_THREADS
913 err = inflate(&self->zst, Z_SYNC_FLUSH);
914 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100915
Martin Panter84544c12016-07-23 03:02:07 +0000916 switch (err) {
917 case Z_OK: /* fall through */
918 case Z_BUF_ERROR: /* fall through */
919 case Z_STREAM_END:
920 break;
921 default:
922 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500923 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +0000924 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500925 }
Martin Panter84544c12016-07-23 03:02:07 +0000926 else
927 break;
928 }
929 goto save;
930 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200931
Martin Panter84544c12016-07-23 03:02:07 +0000932 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000933
Martin Panter84544c12016-07-23 03:02:07 +0000934 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000935
Martin Panter84544c12016-07-23 03:02:07 +0000936 save:
937 if (save_unconsumed_input(self, data, err) < 0)
938 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000939
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000940 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100941 /* This is the logical place to call inflateEnd, but the old behaviour
942 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800943 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100944 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000945 /* We will only get Z_BUF_ERROR if the output buffer was full
946 but there wasn't more output when we tried again, so it is
947 not an error condition.
948 */
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500949 zlib_error(state, self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000950 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000951 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000952
Ma Lin251ffa92021-05-01 07:32:49 +0800953 RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +0800954 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000955 goto success;
Ma Linf9bedb62021-04-28 14:58:54 +0800956 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000957
Martin Panter84544c12016-07-23 03:02:07 +0000958 abort:
Ma Lin251ffa92021-05-01 07:32:49 +0800959 OutputBuffer_OnError(&buffer);
Ma Linf9bedb62021-04-28 14:58:54 +0800960 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000961 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800962 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000963 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000964}
965
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200966/*[clinic input]
967zlib.Compress.flush
968
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500969 cls: defining_class
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200970 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200971 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
972 If mode == Z_FINISH, the compressor object can no longer be
973 used after calling the flush() method. Otherwise, more data
974 can still be compressed.
975 /
976
977Return a bytes object containing any remaining compressed data.
978[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000979
Guido van Rossumfb221561997-04-29 15:38:09 +0000980static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500981zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode)
982/*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000983{
Victor Stinnere079edd2013-11-21 22:33:21 +0100984 int err;
Ma Linf9bedb62021-04-28 14:58:54 +0800985 PyObject *RetVal;
986 _BlocksOutputBuffer buffer = {.list = NULL};
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000987
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -0500988 zlibstate *state = PyType_GetModuleState(cls);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000989 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
990 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200991 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000992 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000993 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000994
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000995 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000996
Jeremy Hylton9714f992001-10-16 21:19:45 +0000997 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000998
Ma Lin251ffa92021-05-01 07:32:49 +0800999 if (OutputBuffer_InitAndGrow(&buffer, -1, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +08001000 goto error;
1001 }
1002
Martin Panter84544c12016-07-23 03:02:07 +00001003 do {
Ma Linf9bedb62021-04-28 14:58:54 +08001004 if (self->zst.avail_out == 0) {
Ma Lin251ffa92021-05-01 07:32:49 +08001005 if (OutputBuffer_Grow(&buffer, &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +08001006 goto error;
1007 }
Guido van Rossum776152b2007-05-22 22:44:07 +00001008 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001009
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001010 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +00001011 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001012 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +00001013
1014 if (err == Z_STREAM_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001015 zlib_error(state, self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +00001016 goto error;
1017 }
1018 } while (self->zst.avail_out == 0);
1019 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001020
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001021 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +00001022 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001023 mode is Z_FINISH, but checking both for safety*/
1024 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +00001025 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001026 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001027 zlib_error(state, self->zst, err, "while finishing compression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001028 goto error;
1029 }
1030 else
1031 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +00001032
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001033 /* We will only get Z_BUF_ERROR if the output buffer was full
1034 but there wasn't more output when we tried again, so it is
1035 not an error condition.
1036 */
Martin Panter84544c12016-07-23 03:02:07 +00001037 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001038 zlib_error(state, self->zst, err, "while flushing");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001039 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +00001040 }
Tim Peters977e5402001-10-17 03:57:20 +00001041
Ma Lin251ffa92021-05-01 07:32:49 +08001042 RetVal = OutputBuffer_Finish(&buffer, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +08001043 if (RetVal != NULL) {
1044 goto success;
1045 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001046
Ma Linf9bedb62021-04-28 14:58:54 +08001047error:
Ma Lin251ffa92021-05-01 07:32:49 +08001048 OutputBuffer_OnError(&buffer);
Ma Linf9bedb62021-04-28 14:58:54 +08001049 RetVal = NULL;
1050success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001051 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +00001052 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001053}
1054
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001055#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -07001056
Larry Hastings61272b72014-01-07 12:41:53 -08001057/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001058zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -07001059
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001060 cls: defining_class
1061
Larry Hastings31826802013-10-19 00:09:25 -07001062Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -08001063[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07001064
Larry Hastings3cceb382014-01-04 11:09:09 -08001065static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001066zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
1067/*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001069 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001070
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001071 compobject *retval = newcompobject(state->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001072 if (!retval) return NULL;
1073
1074 /* Copy the zstream state
1075 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1076 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001077 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001078 int err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001079 switch (err) {
1080 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081 break;
Martin Panter84544c12016-07-23 03:02:07 +00001082 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001083 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1084 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001085 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001086 PyErr_SetString(PyExc_MemoryError,
1087 "Can't allocate memory for compression object");
1088 goto error;
1089 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001090 zlib_error(state, self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001091 goto error;
1092 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001093 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001094 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001095 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001096 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001097 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001098 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001099 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001100
1101 /* Mark it as being initialized */
1102 retval->is_initialised = 1;
1103
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001104 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001105 return (PyObject *)retval;
1106
1107error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -08001108 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001109 Py_XDECREF(retval);
1110 return NULL;
1111}
1112
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001113/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001114zlib.Compress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001115
1116 cls: defining_class
1117
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001118[clinic start generated code]*/
1119
1120static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001121zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls)
1122/*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001123{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001124 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001125}
1126
1127/*[clinic input]
1128zlib.Compress.__deepcopy__
1129
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001130 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001131 memo: object
1132 /
1133
1134[clinic start generated code]*/
1135
1136static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001137zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1138 PyObject *memo)
1139/*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001140{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001141 return zlib_Compress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001142}
1143
1144/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001145zlib.Decompress.copy
1146
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001147 cls: defining_class
1148
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001149Return a copy of the decompression object.
1150[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001151
1152static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001153zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
1154/*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001155{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001156 zlibstate *state = PyType_GetModuleState(cls);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001157
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001158 compobject *retval = newcompobject(state->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001159 if (!retval) return NULL;
1160
1161 /* Copy the zstream state
1162 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1163 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001164 ENTER_ZLIB(self);
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001165 int err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001166 switch (err) {
1167 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001168 break;
Martin Panter84544c12016-07-23 03:02:07 +00001169 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001170 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1171 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001172 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001173 PyErr_SetString(PyExc_MemoryError,
1174 "Can't allocate memory for decompression object");
1175 goto error;
1176 default:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001177 zlib_error(state, self->zst, err, "while copying decompression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001178 goto error;
1179 }
1180
1181 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001182 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001183 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001184 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001185 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001186 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001187 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001188
1189 /* Mark it as being initialized */
1190 retval->is_initialised = 1;
1191
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001192 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001193 return (PyObject *)retval;
1194
1195error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001196 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001197 Py_XDECREF(retval);
1198 return NULL;
1199}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001200
1201/*[clinic input]
1202zlib.Decompress.__copy__
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001203
1204 cls: defining_class
1205
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001206[clinic start generated code]*/
1207
1208static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001209zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls)
1210/*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001211{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001212 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001213}
1214
1215/*[clinic input]
1216zlib.Decompress.__deepcopy__
1217
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001218 cls: defining_class
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001219 memo: object
1220 /
1221
1222[clinic start generated code]*/
1223
1224static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001225zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
1226 PyObject *memo)
1227/*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001228{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001229 return zlib_Decompress_copy_impl(self, cls);
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001230}
1231
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001232#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001233
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001234/*[clinic input]
1235zlib.Decompress.flush
1236
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001237 cls: defining_class
Serhiy Storchaka578c3952020-05-26 18:43:38 +03001238 length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001239 the initial size of the output buffer.
1240 /
1241
1242Return a bytes object containing any remaining decompressed data.
1243[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001244
Guido van Rossumfb221561997-04-29 15:38:09 +00001245static PyObject *
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001246zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
1247 Py_ssize_t length)
1248/*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001249{
Martin Panter84544c12016-07-23 03:02:07 +00001250 int err, flush;
1251 Py_buffer data;
Ma Linf9bedb62021-04-28 14:58:54 +08001252 PyObject *RetVal;
Martin Panter84544c12016-07-23 03:02:07 +00001253 Py_ssize_t ibuflen;
Ma Linf9bedb62021-04-28 14:58:54 +08001254 _BlocksOutputBuffer buffer = {.list = NULL};
Miss Islington (bot)22bcc072021-07-04 18:32:56 -07001255 _Uint32Window window; // output buffer's UINT32_MAX sliding window
Tim Peters977e5402001-10-17 03:57:20 +00001256
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001257 PyObject *module = PyType_GetModule(cls);
1258 if (module == NULL) {
1259 return NULL;
1260 }
1261
1262 zlibstate *state = get_zlib_state(module);
1263
Martin Panter84544c12016-07-23 03:02:07 +00001264 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001265 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1266 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001267 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001268
Miss Islington (bot)57100c82021-11-26 16:42:00 -08001269 ENTER_ZLIB(self);
1270
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001271 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) {
Miss Islington (bot)57100c82021-11-26 16:42:00 -08001272 LEAVE_ZLIB(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001273 return NULL;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001274 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001275
Martin Panter84544c12016-07-23 03:02:07 +00001276 self->zst.next_in = data.buf;
1277 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001278
Miss Islington (bot)22bcc072021-07-04 18:32:56 -07001279 if (OutputBuffer_WindowInitWithSize(&buffer, &window, length,
1280 &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +08001281 goto abort;
1282 }
1283
Martin Panter84544c12016-07-23 03:02:07 +00001284 do {
1285 arrange_input_buffer(&self->zst, &ibuflen);
1286 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001287
Martin Panter84544c12016-07-23 03:02:07 +00001288 do {
Ma Linf9bedb62021-04-28 14:58:54 +08001289 if (self->zst.avail_out == 0) {
Miss Islington (bot)22bcc072021-07-04 18:32:56 -07001290 if (OutputBuffer_WindowGrow(&buffer, &window,
1291 &self->zst.next_out, &self->zst.avail_out) < 0) {
Ma Linf9bedb62021-04-28 14:58:54 +08001292 goto abort;
Ma Lin251ffa92021-05-01 07:32:49 +08001293 }
Ma Linf9bedb62021-04-28 14:58:54 +08001294 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001295
Martin Panter84544c12016-07-23 03:02:07 +00001296 Py_BEGIN_ALLOW_THREADS
1297 err = inflate(&self->zst, flush);
1298 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001299
Martin Panter84544c12016-07-23 03:02:07 +00001300 switch (err) {
1301 case Z_OK: /* fall through */
1302 case Z_BUF_ERROR: /* fall through */
1303 case Z_STREAM_END:
1304 break;
1305 default:
1306 if (err == Z_NEED_DICT && self->zdict != NULL) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001307 if (set_inflate_zdict(state, self) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001308 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001309 }
Martin Panter84544c12016-07-23 03:02:07 +00001310 else
1311 break;
1312 }
1313 goto save;
1314 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001315
Martin Panter84544c12016-07-23 03:02:07 +00001316 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1317
1318 } while (err != Z_STREAM_END && ibuflen != 0);
1319
1320 save:
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001321 if (save_unconsumed_input(self, &data, err) < 0) {
Martin Panter84544c12016-07-23 03:02:07 +00001322 goto abort;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001323 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001324
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001325 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001326 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001327 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001328 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001329 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001330 if (err != Z_OK) {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001331 zlib_error(state, self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001332 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001333 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001334 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001335
Miss Islington (bot)22bcc072021-07-04 18:32:56 -07001336 RetVal = OutputBuffer_WindowFinish(&buffer, &window, self->zst.avail_out);
Ma Linf9bedb62021-04-28 14:58:54 +08001337 if (RetVal != NULL) {
Martin Panter84544c12016-07-23 03:02:07 +00001338 goto success;
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001339 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001340
Martin Panter84544c12016-07-23 03:02:07 +00001341 abort:
Miss Islington (bot)22bcc072021-07-04 18:32:56 -07001342 OutputBuffer_WindowOnError(&buffer, &window);
Ma Linf9bedb62021-04-28 14:58:54 +08001343 RetVal = NULL;
Martin Panter84544c12016-07-23 03:02:07 +00001344 success:
1345 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001346 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001347 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001348}
1349
Christian Heimes936e2f32014-01-27 01:06:57 +01001350#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001351
Guido van Rossumfb221561997-04-29 15:38:09 +00001352static PyMethodDef comp_methods[] =
1353{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001354 ZLIB_COMPRESS_COMPRESS_METHODDEF
1355 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001356 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001357 ZLIB_COMPRESS___COPY___METHODDEF
1358 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001359 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001360};
1361
1362static PyMethodDef Decomp_methods[] =
1363{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001364 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001365 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001366 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001367 ZLIB_DECOMPRESS___COPY___METHODDEF
1368 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001369 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001370};
1371
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001372#define COMP_OFF(x) offsetof(compobject, x)
1373static PyMemberDef Decomp_members[] = {
1374 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1375 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001376 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001377 {NULL},
1378};
Guido van Rossumfb221561997-04-29 15:38:09 +00001379
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001380/*[clinic input]
1381zlib.adler32
1382
1383 data: Py_buffer
1384 value: unsigned_int(bitwise=True) = 1
1385 Starting value of the checksum.
1386 /
1387
1388Compute an Adler-32 checksum of data.
1389
1390The returned checksum is an integer.
1391[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001392
Guido van Rossumfb221561997-04-29 15:38:09 +00001393static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001394zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1395/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001396{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001397 /* Releasing the GIL for very small buffers is inefficient
1398 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001399 if (data->len > 1024*5) {
1400 unsigned char *buf = data->buf;
1401 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001402
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001403 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001404 /* Avoid truncation of length for very large buffers. adler32() takes
1405 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001406 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001407 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001408 buf += (size_t) UINT_MAX;
1409 len -= (size_t) UINT_MAX;
1410 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001411 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001412 Py_END_ALLOW_THREADS
1413 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001414 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001415 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001416 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001417}
Tim Peters977e5402001-10-17 03:57:20 +00001418
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001419/*[clinic input]
1420zlib.crc32
1421
1422 data: Py_buffer
1423 value: unsigned_int(bitwise=True) = 0
1424 Starting value of the checksum.
1425 /
1426
1427Compute a CRC-32 checksum of data.
1428
1429The returned checksum is an integer.
1430[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001431
1432static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001433zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1434/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001435{
Martin v. Löwis423be952008-08-13 15:53:07 +00001436 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001437
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001438 /* Releasing the GIL for very small buffers is inefficient
1439 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001440 if (data->len > 1024*5) {
1441 unsigned char *buf = data->buf;
1442 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001443
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001444 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001445 /* Avoid truncation of length for very large buffers. crc32() takes
1446 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001447 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001448 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001449 buf += (size_t) UINT_MAX;
1450 len -= (size_t) UINT_MAX;
1451 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001452 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001453 Py_END_ALLOW_THREADS
1454 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001455 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001456 }
Christian Heimescc47b052008-03-25 14:56:36 +00001457 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001458}
Tim Peters977e5402001-10-17 03:57:20 +00001459
Guido van Rossumfb221561997-04-29 15:38:09 +00001460
1461static PyMethodDef zlib_methods[] =
1462{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001463 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001464 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001465 ZLIB_COMPRESSOBJ_METHODDEF
1466 ZLIB_CRC32_METHODDEF
1467 ZLIB_DECOMPRESS_METHODDEF
1468 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001469 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001470};
1471
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001472static PyType_Slot Comptype_slots[] = {
1473 {Py_tp_dealloc, Comp_dealloc},
1474 {Py_tp_methods, comp_methods},
1475 {0, 0},
1476};
1477
1478static PyType_Spec Comptype_spec = {
Erlend Egeberg Aasland9746cda2021-04-30 16:04:57 +02001479 .name = "zlib.Compress",
1480 .basicsize = sizeof(compobject),
1481 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1482 .slots= Comptype_slots,
Guido van Rossumfb221561997-04-29 15:38:09 +00001483};
1484
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001485static PyType_Slot Decomptype_slots[] = {
1486 {Py_tp_dealloc, Decomp_dealloc},
1487 {Py_tp_methods, Decomp_methods},
1488 {Py_tp_members, Decomp_members},
1489 {0, 0},
1490};
1491
1492static PyType_Spec Decomptype_spec = {
Erlend Egeberg Aasland9746cda2021-04-30 16:04:57 +02001493 .name = "zlib.Decompress",
1494 .basicsize = sizeof(compobject),
1495 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
1496 .slots = Decomptype_slots,
Guido van Rossumfb221561997-04-29 15:38:09 +00001497};
1498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001499PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001500"The functions in this module allow compression and decompression using the\n"
1501"zlib library, which is based on GNU zip.\n"
1502"\n"
1503"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001504"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001505"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001506"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001507"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Łukasz Langa8c1e1da2021-09-22 01:33:59 +02001508"decompressobj([wbits[, zdict]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001509"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001510"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001511"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001512"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001513
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001514static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001515zlib_clear(PyObject *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001516{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001517 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001518 Py_CLEAR(state->Comptype);
1519 Py_CLEAR(state->Decomptype);
1520 Py_CLEAR(state->ZlibError);
1521 return 0;
1522}
1523
1524static int
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001525zlib_traverse(PyObject *mod, visitproc visit, void *arg)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001526{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001527 zlibstate *state = get_zlib_state(mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001528 Py_VISIT(state->Comptype);
1529 Py_VISIT(state->Decomptype);
1530 Py_VISIT(state->ZlibError);
1531 return 0;
1532}
1533
1534static void
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001535zlib_free(void *mod)
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001536{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001537 zlib_clear((PyObject *)mod);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001538}
1539
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001540static int
1541zlib_exec(PyObject *mod)
1542{
1543 zlibstate *state = get_zlib_state(mod);
1544
1545 state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1546 mod, &Comptype_spec, NULL);
1547 if (state->Comptype == NULL) {
1548 return -1;
1549 }
1550
1551 state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec(
1552 mod, &Decomptype_spec, NULL);
1553 if (state->Decomptype == NULL) {
1554 return -1;
1555 }
1556
1557 state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1558 if (state->ZlibError == NULL) {
1559 return -1;
1560 }
1561
1562 Py_INCREF(state->ZlibError);
1563 if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
1564 Py_DECREF(state->ZlibError);
1565 return -1;
1566 }
1567
1568#define ZLIB_ADD_INT_MACRO(c) \
1569 do { \
1570 if ((PyModule_AddIntConstant(mod, #c, c)) < 0) { \
1571 return -1; \
1572 } \
1573 } while(0)
1574
1575 ZLIB_ADD_INT_MACRO(MAX_WBITS);
1576 ZLIB_ADD_INT_MACRO(DEFLATED);
1577 ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL);
1578 ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE);
1579 // compression levels
1580 ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION);
1581 ZLIB_ADD_INT_MACRO(Z_BEST_SPEED);
1582 ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION);
1583 ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION);
1584 // compression strategies
1585 ZLIB_ADD_INT_MACRO(Z_FILTERED);
1586 ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY);
1587#ifdef Z_RLE // 1.2.0.1
1588 ZLIB_ADD_INT_MACRO(Z_RLE);
1589#endif
1590#ifdef Z_FIXED // 1.2.2.2
1591 ZLIB_ADD_INT_MACRO(Z_FIXED);
1592#endif
1593 ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY);
1594 // allowed flush values
1595 ZLIB_ADD_INT_MACRO(Z_NO_FLUSH);
1596 ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH);
1597 ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH);
1598 ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH);
1599 ZLIB_ADD_INT_MACRO(Z_FINISH);
1600#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1601 ZLIB_ADD_INT_MACRO(Z_BLOCK);
1602#endif
1603#ifdef Z_TREES // 1.2.3.4, only for inflate
1604 ZLIB_ADD_INT_MACRO(Z_TREES);
1605#endif
1606 PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
1607 if (ver == NULL) {
1608 return -1;
1609 }
1610
1611 if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
1612 Py_DECREF(ver);
1613 return -1;
1614 }
1615
1616 ver = PyUnicode_FromString(zlibVersion());
1617 if (ver == NULL) {
1618 return -1;
1619 }
1620
1621 if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
1622 Py_DECREF(ver);
1623 return -1;
1624 }
1625
1626 if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
1627 return -1;
1628 }
1629 return 0;
1630}
1631
1632static PyModuleDef_Slot zlib_slots[] = {
1633 {Py_mod_exec, zlib_exec},
1634 {0, NULL}
1635};
1636
Martin v. Löwis1a214512008-06-11 05:26:20 +00001637static struct PyModuleDef zlibmodule = {
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001638 PyModuleDef_HEAD_INIT,
1639 .m_name = "zlib",
1640 .m_doc = zlib_module_documentation,
1641 .m_size = sizeof(zlibstate),
1642 .m_methods = zlib_methods,
1643 .m_slots = zlib_slots,
1644 .m_traverse = zlib_traverse,
1645 .m_clear = zlib_clear,
1646 .m_free = zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001647};
1648
Mark Hammond62b1ab12002-07-23 06:31:15 +00001649PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001650PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001651{
Mohamed Koubaa1aaa21f2020-09-07 03:27:55 -05001652 return PyModuleDef_Init(&zlibmodule);
Guido van Rossumfb221561997-04-29 15:38:09 +00001653}