blob: fe27909ae8a75a64780c42adc4caef876dd87c8e [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
Larry Hastings31826802013-10-19 00:09:25 -070012
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020013#define ENTER_ZLIB(obj) \
14 Py_BEGIN_ALLOW_THREADS; \
15 PyThread_acquire_lock((obj)->lock, 1); \
16 Py_END_ALLOW_THREADS;
17#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000018
Martin Panter3f0ee832016-06-05 10:48:34 +000019#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000020# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000021#endif
22
Guido van Rossumfb221561997-04-29 15:38:09 +000023/* The following parameters are copied from zutil.h, version 0.95 */
24#define DEFLATED 8
25#if MAX_MEM_LEVEL >= 8
26# define DEF_MEM_LEVEL 8
27#else
28# define DEF_MEM_LEVEL MAX_MEM_LEVEL
29#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000030
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020031/* Initial buffer size. */
32#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000033
Dino Viehlanda1ffad02019-09-10 11:27:03 +010034static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +000035
Dino Viehlanda1ffad02019-09-10 11:27:03 +010036typedef struct {
37 PyTypeObject *Comptype;
38 PyTypeObject *Decomptype;
39 PyObject *ZlibError;
40} _zlibstate;
41
Hai Shif707d942020-03-16 21:15:01 +080042static inline _zlibstate*
43get_zlib_state(PyObject *module)
44{
45 void *state = PyModule_GetState(module);
46 assert(state != NULL);
47 return (_zlibstate *)state;
48}
49
Dino Viehlanda1ffad02019-09-10 11:27:03 +010050#define _zlibstate_global ((_zlibstate *)PyModule_GetState(PyState_FindModule(&zlibmodule)))
Guido van Rossumfb221561997-04-29 15:38:09 +000051
Tim Peters977e5402001-10-17 03:57:20 +000052typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000053{
Jeremy Hylton9714f992001-10-16 21:19:45 +000054 PyObject_HEAD
55 z_stream zst;
56 PyObject *unused_data;
57 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020058 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000059 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020060 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020061 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000062} compobject;
63
Jeremy Hylton0965e082001-10-16 21:56:09 +000064static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020065zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000066{
Nadeem Vawda524148a2011-08-28 11:26:46 +020067 const char *zmsg = Z_NULL;
68 /* In case of a version mismatch, zst.msg won't be initialized.
69 Check for this case first, before looking at zst.msg. */
70 if (err == Z_VERSION_ERROR)
71 zmsg = "library version mismatch";
72 if (zmsg == Z_NULL)
73 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000074 if (zmsg == Z_NULL) {
75 switch (err) {
76 case Z_BUF_ERROR:
77 zmsg = "incomplete or truncated stream";
78 break;
79 case Z_STREAM_ERROR:
80 zmsg = "inconsistent stream state";
81 break;
82 case Z_DATA_ERROR:
83 zmsg = "invalid input data";
84 break;
85 }
86 }
87 if (zmsg == Z_NULL)
Dino Viehlanda1ffad02019-09-10 11:27:03 +010088 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000089 else
Dino Viehlanda1ffad02019-09-10 11:27:03 +010090 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000091}
92
Larry Hastings61272b72014-01-07 12:41:53 -080093/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080094module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080095class zlib.Compress "compobject *" "&Comptype"
96class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080097[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030098/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -080099
Guido van Rossumfb221561997-04-29 15:38:09 +0000100static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000101newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000102{
Tim Peters977e5402001-10-17 03:57:20 +0000103 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000104 self = PyObject_New(compobject, type);
105 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000106 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200107 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000108 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200109 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000110 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000111 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000112 Py_DECREF(self);
113 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000114 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000115 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000116 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 Py_DECREF(self);
118 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000119 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000120 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200121 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000122 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200123 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
124 return NULL;
125 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000126 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000127}
128
Victor Stinner5064a522013-07-07 16:50:27 +0200129static void*
130PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
131{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300132 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200133 return NULL;
134 /* PyMem_Malloc() cannot be used: the GIL is not held when
135 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300136 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200137}
138
139static void
140PyZlib_Free(voidpf ctx, void *ptr)
141{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200142 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200143}
144
Martin Panter84544c12016-07-23 03:02:07 +0000145static void
146arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
147{
Segev Finer679b5662017-07-27 01:17:57 +0300148 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000149 *remains -= zst->avail_in;
150}
151
152static Py_ssize_t
153arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
154 Py_ssize_t length,
155 Py_ssize_t max_length)
156{
157 Py_ssize_t occupied;
158
159 if (*buffer == NULL) {
160 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
161 return -1;
162 occupied = 0;
163 }
164 else {
165 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
166
167 if (length == occupied) {
168 Py_ssize_t new_length;
169 assert(length <= max_length);
170 /* can not scale the buffer over max_length */
171 if (length == max_length)
172 return -2;
173 if (length <= (max_length >> 1))
174 new_length = length << 1;
175 else
176 new_length = max_length;
177 if (_PyBytes_Resize(buffer, new_length) < 0)
178 return -1;
179 length = new_length;
180 }
181 }
182
Segev Finer679b5662017-07-27 01:17:57 +0300183 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000184 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
185
186 return length;
187}
188
189static Py_ssize_t
190arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
191{
192 Py_ssize_t ret;
193
194 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
195 PY_SSIZE_T_MAX);
196 if (ret == -2)
197 PyErr_NoMemory();
198
199 return ret;
200}
201
Larry Hastings61272b72014-01-07 12:41:53 -0800202/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800203zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200204
Martin Panter1fe0d132016-02-10 10:06:36 +0000205 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800206 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300207 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200208 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000209 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800210
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200211Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800212[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800213
Guido van Rossumfb221561997-04-29 15:38:09 +0000214static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300215zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
216/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000217{
Martin Panter84544c12016-07-23 03:02:07 +0000218 PyObject *RetVal = NULL;
219 Byte *ibuf;
220 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
221 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000222 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000223
Martin Panter525a9492016-07-23 03:39:49 +0000224 ibuf = data->buf;
225 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000226
Victor Stinner5064a522013-07-07 16:50:27 +0200227 zst.opaque = NULL;
228 zst.zalloc = PyZlib_Malloc;
229 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000230 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000231 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000232
Martin Panter84544c12016-07-23 03:02:07 +0000233 switch (err) {
234 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000235 break;
Martin Panter84544c12016-07-23 03:02:07 +0000236 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000237 PyErr_SetString(PyExc_MemoryError,
238 "Out of memory while compressing data");
239 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000240 case Z_STREAM_ERROR:
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100241 PyErr_SetString(_zlibstate_global->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000242 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000243 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000244 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000245 zlib_error(zst, err, "while compressing data");
246 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000247 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000248
Martin Panter84544c12016-07-23 03:02:07 +0000249 do {
250 arrange_input_buffer(&zst, &ibuflen);
251 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000252
Martin Panter84544c12016-07-23 03:02:07 +0000253 do {
254 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
255 if (obuflen < 0) {
256 deflateEnd(&zst);
257 goto error;
258 }
259
260 Py_BEGIN_ALLOW_THREADS
261 err = deflate(&zst, flush);
262 Py_END_ALLOW_THREADS
263
264 if (err == Z_STREAM_ERROR) {
265 deflateEnd(&zst);
266 zlib_error(zst, err, "while compressing data");
267 goto error;
268 }
269
270 } while (zst.avail_out == 0);
271 assert(zst.avail_in == 0);
272
273 } while (flush != Z_FINISH);
274 assert(err == Z_STREAM_END);
275
276 err = deflateEnd(&zst);
277 if (err == Z_OK) {
278 if (_PyBytes_Resize(&RetVal, zst.next_out -
279 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
280 goto error;
281 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000282 }
Tim Peters977e5402001-10-17 03:57:20 +0000283 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000284 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000285 error:
Martin Panter84544c12016-07-23 03:02:07 +0000286 Py_XDECREF(RetVal);
287 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000288}
289
Larry Hastings61272b72014-01-07 12:41:53 -0800290/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100291
Martin Panter84544c12016-07-23 03:02:07 +0000292class ssize_t_converter(CConverter):
293 type = 'Py_ssize_t'
294 converter = 'ssize_t_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200295 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100296
Larry Hastings61272b72014-01-07 12:41:53 -0800297[python start generated code]*/
Martin Panter84544c12016-07-23 03:02:07 +0000298/*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100299
300static int
Martin Panter84544c12016-07-23 03:02:07 +0000301ssize_t_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100302{
Martin Pantere99e9772015-11-20 08:13:35 +0000303 PyObject *long_obj;
304 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100305
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200306 /* XXX Should be replaced with PyNumber_AsSsize_t after the end of the
307 deprecation period. */
308 long_obj = _PyLong_FromNbIndexOrNbInt(obj);
Martin Pantere99e9772015-11-20 08:13:35 +0000309 if (long_obj == NULL) {
310 return 0;
311 }
312 val = PyLong_AsSsize_t(long_obj);
313 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100314 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000315 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100316 }
Martin Panter84544c12016-07-23 03:02:07 +0000317 *(Py_ssize_t *)ptr = val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100318 return 1;
319}
320
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200321/*[clinic input]
322zlib.decompress
323
324 data: Py_buffer
325 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300326 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200327 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000328 The window buffer size and container format.
Martin Panter84544c12016-07-23 03:02:07 +0000329 bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200330 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200331
332Returns a bytes object containing the uncompressed data.
333[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000334
Guido van Rossumfb221561997-04-29 15:38:09 +0000335static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300336zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000337 Py_ssize_t bufsize)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300338/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000339{
Martin Panter84544c12016-07-23 03:02:07 +0000340 PyObject *RetVal = NULL;
341 Byte *ibuf;
342 Py_ssize_t ibuflen;
343 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000344 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000345
Martin Panter84544c12016-07-23 03:02:07 +0000346 if (bufsize < 0) {
347 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
348 return NULL;
349 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100350 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000351 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000352
Martin Panter84544c12016-07-23 03:02:07 +0000353 ibuf = data->buf;
354 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000355
Victor Stinner5064a522013-07-07 16:50:27 +0200356 zst.opaque = NULL;
357 zst.zalloc = PyZlib_Malloc;
358 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000359 zst.avail_in = 0;
360 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200361 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000362
Martin Panter84544c12016-07-23 03:02:07 +0000363 switch (err) {
364 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000365 break;
Martin Panter84544c12016-07-23 03:02:07 +0000366 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 PyErr_SetString(PyExc_MemoryError,
368 "Out of memory while decompressing data");
369 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000370 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000371 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000372 zlib_error(zst, err, "while preparing to decompress data");
373 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000374 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000375
Jeremy Hylton9714f992001-10-16 21:19:45 +0000376 do {
Martin Panter84544c12016-07-23 03:02:07 +0000377 arrange_input_buffer(&zst, &ibuflen);
378 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000379
Martin Panter84544c12016-07-23 03:02:07 +0000380 do {
381 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
382 if (bufsize < 0) {
383 inflateEnd(&zst);
384 goto error;
385 }
386
387 Py_BEGIN_ALLOW_THREADS
388 err = inflate(&zst, flush);
389 Py_END_ALLOW_THREADS
390
391 switch (err) {
392 case Z_OK: /* fall through */
393 case Z_BUF_ERROR: /* fall through */
394 case Z_STREAM_END:
395 break;
396 case Z_MEM_ERROR:
397 inflateEnd(&zst);
398 PyErr_SetString(PyExc_MemoryError,
399 "Out of memory while decompressing data");
400 goto error;
401 default:
402 inflateEnd(&zst);
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000403 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000404 goto error;
405 }
Martin Panter84544c12016-07-23 03:02:07 +0000406
407 } while (zst.avail_out == 0);
408
409 } while (err != Z_STREAM_END && ibuflen != 0);
410
411
412 if (err != Z_STREAM_END) {
413 inflateEnd(&zst);
414 zlib_error(zst, err, "while decompressing data");
415 goto error;
416 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000417
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000418 err = inflateEnd(&zst);
419 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200420 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000421 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000422 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000423
Martin Panter84544c12016-07-23 03:02:07 +0000424 if (_PyBytes_Resize(&RetVal, zst.next_out -
425 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000426 goto error;
427
Martin Panter84544c12016-07-23 03:02:07 +0000428 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000429
430 error:
Martin Panter84544c12016-07-23 03:02:07 +0000431 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000432 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000433}
434
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200435/*[clinic input]
436zlib.compressobj
437
438 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000439 The compression level (an integer in the range 0-9 or -1; default is
440 currently equivalent to 6). Higher compression levels are slower,
441 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200442 method: int(c_default="DEFLATED") = DEFLATED
443 The compression algorithm. If given, this must be DEFLATED.
444 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000445 +9 to +15: The base-two logarithm of the window size. Include a zlib
446 container.
447 -9 to -15: Generate a raw stream.
448 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200449 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
450 Controls the amount of memory used for internal compression state.
451 Valid values range from 1 to 9. Higher values result in higher memory
452 usage, faster compression, and smaller output.
453 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
454 Used to tune the compression algorithm. Possible values are
455 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
456 zdict: Py_buffer = None
457 The predefined compression dictionary - a sequence of bytes
458 containing subsequences that are likely to occur in the input data.
459
460Return a compressor object.
461[clinic start generated code]*/
462
Guido van Rossumfb221561997-04-29 15:38:09 +0000463static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300464zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400465 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300466/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000467{
Victor Stinnere079edd2013-11-21 22:33:21 +0100468 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200469 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000470
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200471 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100472 PyErr_SetString(PyExc_OverflowError,
473 "zdict length does not fit in an unsigned int");
474 goto error;
475 }
476
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100477 self = newcompobject(_zlibstate_global->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000478 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200479 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200480 self->zst.opaque = NULL;
481 self->zst.zalloc = PyZlib_Malloc;
482 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000483 self->zst.next_in = NULL;
484 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000485 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000486 switch (err) {
487 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000488 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200489 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200490 goto success;
491 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100492 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200493 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200494 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000495 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200496 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000497 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200498 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
499 goto error;
500 default:
501 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
502 goto error;
503 }
504 }
Martin Panter84544c12016-07-23 03:02:07 +0000505 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000506 PyErr_SetString(PyExc_MemoryError,
507 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200508 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000509 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000510 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200511 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000512 default:
513 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200514 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000515 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200516
517 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200518 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200519 success:
Martin Panter84544c12016-07-23 03:02:07 +0000520 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000521}
522
Martin Panter3f0ee832016-06-05 10:48:34 +0000523static int
524set_inflate_zdict(compobject *self)
525{
526 Py_buffer zdict_buf;
527 int err;
528
529 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
530 return -1;
531 }
532 if ((size_t)zdict_buf.len > UINT_MAX) {
533 PyErr_SetString(PyExc_OverflowError,
534 "zdict length does not fit in an unsigned int");
535 PyBuffer_Release(&zdict_buf);
536 return -1;
537 }
Martin Panter84544c12016-07-23 03:02:07 +0000538 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000539 zdict_buf.buf, (unsigned int)zdict_buf.len);
540 PyBuffer_Release(&zdict_buf);
541 if (err != Z_OK) {
542 zlib_error(self->zst, err, "while setting zdict");
543 return -1;
544 }
545 return 0;
546}
547
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200548/*[clinic input]
549zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200550
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200551 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000552 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200553 zdict: object(c_default="NULL") = b''
554 The predefined compression dictionary. This must be the same
555 dictionary as used by the compressor that produced the input data.
556
557Return a decompressor object.
558[clinic start generated code]*/
559
560static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300561zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
562/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200563{
564 int err;
565 compobject *self;
566
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200567 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
568 PyErr_SetString(PyExc_TypeError,
569 "zdict argument must support the buffer protocol");
570 return NULL;
571 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000572
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100573 self = newcompobject(_zlibstate_global->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000574 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000575 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200576 self->zst.opaque = NULL;
577 self->zst.zalloc = PyZlib_Malloc;
578 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000579 self->zst.next_in = NULL;
580 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200581 if (zdict != NULL) {
582 Py_INCREF(zdict);
583 self->zdict = zdict;
584 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000585 err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000586 switch (err) {
587 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000588 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000589 if (self->zdict != NULL && wbits < 0) {
590#ifdef AT_LEAST_ZLIB_1_2_2_1
591 if (set_inflate_zdict(self) < 0) {
592 Py_DECREF(self);
593 return NULL;
594 }
595#else
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100596 PyErr_Format(_zlibstate_global->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000597 "zlib version %s does not allow raw inflate with dictionary",
598 ZLIB_VERSION);
599 Py_DECREF(self);
600 return NULL;
601#endif
602 }
Martin Panter84544c12016-07-23 03:02:07 +0000603 return (PyObject *)self;
604 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000605 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000606 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
607 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000608 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000609 Py_DECREF(self);
610 PyErr_SetString(PyExc_MemoryError,
611 "Can't allocate memory for decompression object");
612 return NULL;
613 default:
614 zlib_error(self->zst, err, "while creating decompression object");
615 Py_DECREF(self);
616 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000617 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000618}
619
620static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000621Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000622{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100623 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000624 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000625 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000626 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200627 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000628 PyObject_Del(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100629 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000630}
631
632static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000633Comp_dealloc(compobject *self)
634{
635 if (self->is_initialised)
636 deflateEnd(&self->zst);
637 Dealloc(self);
638}
639
640static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000641Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000642{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000643 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000644 inflateEnd(&self->zst);
645 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000646}
647
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200648/*[clinic input]
649zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000650
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200651 data: Py_buffer
652 Binary data to be compressed.
653 /
654
655Returns a bytes object containing compressed data.
656
657After calling this function, some of the input data may still
658be stored in internal buffers for later processing.
659Call the flush() method to clear these buffers.
660[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000661
Guido van Rossumfb221561997-04-29 15:38:09 +0000662static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200663zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800664/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000665{
Martin Panter84544c12016-07-23 03:02:07 +0000666 PyObject *RetVal = NULL;
667 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200668 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000669
Martin Panter84544c12016-07-23 03:02:07 +0000670 self->zst.next_in = data->buf;
671 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000672
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000673 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000674
Martin Panter84544c12016-07-23 03:02:07 +0000675 do {
676 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000677
Martin Panter84544c12016-07-23 03:02:07 +0000678 do {
679 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
680 if (obuflen < 0)
681 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000682
Martin Panter84544c12016-07-23 03:02:07 +0000683 Py_BEGIN_ALLOW_THREADS
684 err = deflate(&self->zst, Z_NO_FLUSH);
685 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000686
Martin Panter84544c12016-07-23 03:02:07 +0000687 if (err == Z_STREAM_ERROR) {
688 zlib_error(self->zst, err, "while compressing data");
689 goto error;
690 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000691
Martin Panter84544c12016-07-23 03:02:07 +0000692 } while (self->zst.avail_out == 0);
693 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000694
Martin Panter84544c12016-07-23 03:02:07 +0000695 } while (ibuflen != 0);
696
697 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
698 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
699 goto success;
700
701 error:
702 Py_CLEAR(RetVal);
703 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000704 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000705 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000706}
707
Martin Panter84544c12016-07-23 03:02:07 +0000708/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100709 self->unused_data or self->unconsumed_tail, as appropriate. */
710static int
Martin Panter84544c12016-07-23 03:02:07 +0000711save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100712{
713 if (err == Z_STREAM_END) {
714 /* The end of the compressed data has been reached. Store the leftover
715 input data in self->unused_data. */
716 if (self->zst.avail_in > 0) {
717 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000718 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100719 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000720 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
721 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100722 PyErr_NoMemory();
723 return -1;
724 }
Martin Panter84544c12016-07-23 03:02:07 +0000725 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100726 new_data = PyBytes_FromStringAndSize(NULL, new_size);
727 if (new_data == NULL)
728 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200729 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100730 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200731 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000732 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300733 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100734 self->zst.avail_in = 0;
735 }
736 }
Martin Panter84544c12016-07-23 03:02:07 +0000737
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100738 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
739 /* This code handles two distinct cases:
740 1. Output limit was reached. Save leftover input in unconsumed_tail.
741 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000742 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100743 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000744 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100745 if (new_data == NULL)
746 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300747 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100748 }
Martin Panter84544c12016-07-23 03:02:07 +0000749
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100750 return 0;
751}
752
Larry Hastings61272b72014-01-07 12:41:53 -0800753/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800754zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700755
756 data: Py_buffer
757 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300758 /
Martin Panter84544c12016-07-23 03:02:07 +0000759 max_length: ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700760 The maximum allowable length of the decompressed data.
761 Unconsumed input data will be stored in
762 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700763
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200764Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700765
766After calling this function, some of the input data may still be stored in
767internal buffers for later processing.
768Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800769[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700770
Larry Hastings31826802013-10-19 00:09:25 -0700771static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400772zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000773 Py_ssize_t max_length)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300774/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
Larry Hastings31826802013-10-19 00:09:25 -0700775{
Martin Panter84544c12016-07-23 03:02:07 +0000776 int err = Z_OK;
777 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200778 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000779
Martin Panter84544c12016-07-23 03:02:07 +0000780 if (max_length < 0) {
781 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700782 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000783 } else if (max_length == 0)
784 hard_limit = PY_SSIZE_T_MAX;
785 else
786 hard_limit = max_length;
787
788 self->zst.next_in = data->buf;
789 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000790
Jeremy Hylton9714f992001-10-16 21:19:45 +0000791 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000792 if (max_length && obuflen > max_length)
793 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000794
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800795 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000796
Martin Panter84544c12016-07-23 03:02:07 +0000797 do {
798 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000799
Martin Panter84544c12016-07-23 03:02:07 +0000800 do {
801 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
802 obuflen, hard_limit);
803 if (obuflen == -2) {
804 if (max_length > 0) {
805 goto save;
806 }
807 PyErr_NoMemory();
808 }
809 if (obuflen < 0) {
810 goto abort;
811 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000812
Martin Panter84544c12016-07-23 03:02:07 +0000813 Py_BEGIN_ALLOW_THREADS
814 err = inflate(&self->zst, Z_SYNC_FLUSH);
815 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100816
Martin Panter84544c12016-07-23 03:02:07 +0000817 switch (err) {
818 case Z_OK: /* fall through */
819 case Z_BUF_ERROR: /* fall through */
820 case Z_STREAM_END:
821 break;
822 default:
823 if (err == Z_NEED_DICT && self->zdict != NULL) {
824 if (set_inflate_zdict(self) < 0)
825 goto abort;
826 else
827 break;
828 }
829 goto save;
830 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200831
Martin Panter84544c12016-07-23 03:02:07 +0000832 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000833
Martin Panter84544c12016-07-23 03:02:07 +0000834 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000835
Martin Panter84544c12016-07-23 03:02:07 +0000836 save:
837 if (save_unconsumed_input(self, data, err) < 0)
838 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000839
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000840 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100841 /* This is the logical place to call inflateEnd, but the old behaviour
842 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800843 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100844 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000845 /* We will only get Z_BUF_ERROR if the output buffer was full
846 but there wasn't more output when we tried again, so it is
847 not an error condition.
848 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800849 zlib_error(self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000850 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000851 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000852
Martin Panter84544c12016-07-23 03:02:07 +0000853 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
854 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
855 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000856
Martin Panter84544c12016-07-23 03:02:07 +0000857 abort:
858 Py_CLEAR(RetVal);
859 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800860 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000861 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000862}
863
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200864/*[clinic input]
865zlib.Compress.flush
866
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200867 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200868 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
869 If mode == Z_FINISH, the compressor object can no longer be
870 used after calling the flush() method. Otherwise, more data
871 can still be compressed.
872 /
873
874Return a bytes object containing any remaining compressed data.
875[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000876
Guido van Rossumfb221561997-04-29 15:38:09 +0000877static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200878zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200879/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000880{
Victor Stinnere079edd2013-11-21 22:33:21 +0100881 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000882 Py_ssize_t length = DEF_BUF_SIZE;
883 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000884
Jeremy Hylton9714f992001-10-16 21:19:45 +0000885 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
886 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200887 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000889 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000890
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000891 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000892
Jeremy Hylton9714f992001-10-16 21:19:45 +0000893 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000894
Martin Panter84544c12016-07-23 03:02:07 +0000895 do {
896 length = arrange_output_buffer(&self->zst, &RetVal, length);
897 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200898 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000899 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000900 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000901
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000903 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000904 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000905
906 if (err == Z_STREAM_ERROR) {
907 zlib_error(self->zst, err, "while flushing");
908 Py_CLEAR(RetVal);
909 goto error;
910 }
911 } while (self->zst.avail_out == 0);
912 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000913
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200914 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000915 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200916 mode is Z_FINISH, but checking both for safety*/
917 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000918 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000919 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200920 zlib_error(self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000921 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 goto error;
923 }
924 else
925 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000926
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000927 /* We will only get Z_BUF_ERROR if the output buffer was full
928 but there wasn't more output when we tried again, so it is
929 not an error condition.
930 */
Martin Panter84544c12016-07-23 03:02:07 +0000931 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000932 zlib_error(self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000933 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000934 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000935 }
Tim Peters977e5402001-10-17 03:57:20 +0000936
Martin Panter84544c12016-07-23 03:02:07 +0000937 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
938 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200939 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000940
Tim Peters977e5402001-10-17 03:57:20 +0000941 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000942 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000943 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000944}
945
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000946#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700947
Larry Hastings61272b72014-01-07 12:41:53 -0800948/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800949zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700950
951Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800952[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700953
Larry Hastings3cceb382014-01-04 11:09:09 -0800954static PyObject *
955zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800956/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957{
958 compobject *retval = NULL;
959 int err;
960
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100961 retval = newcompobject(_zlibstate_global->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962 if (!retval) return NULL;
963
964 /* Copy the zstream state
965 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
966 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800967 ENTER_ZLIB(self);
968 err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000969 switch (err) {
970 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000971 break;
Martin Panter84544c12016-07-23 03:02:07 +0000972 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
974 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000975 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976 PyErr_SetString(PyExc_MemoryError,
977 "Can't allocate memory for compression object");
978 goto error;
979 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800980 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000981 goto error;
982 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800983 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300984 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800985 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300986 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800987 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300988 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800989 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000990
991 /* Mark it as being initialized */
992 retval->is_initialised = 1;
993
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800994 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000995 return (PyObject *)retval;
996
997error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800998 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000999 Py_XDECREF(retval);
1000 return NULL;
1001}
1002
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001003/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001004zlib.Compress.__copy__
1005[clinic start generated code]*/
1006
1007static PyObject *
1008zlib_Compress___copy___impl(compobject *self)
1009/*[clinic end generated code: output=1875e6791975442e input=be97a05a788dfd83]*/
1010{
1011 return zlib_Compress_copy_impl(self);
1012}
1013
1014/*[clinic input]
1015zlib.Compress.__deepcopy__
1016
1017 memo: object
1018 /
1019
1020[clinic start generated code]*/
1021
1022static PyObject *
1023zlib_Compress___deepcopy__(compobject *self, PyObject *memo)
1024/*[clinic end generated code: output=f47a2213282c9eb0 input=a9a8b0b40d83388e]*/
1025{
1026 return zlib_Compress_copy_impl(self);
1027}
1028
1029/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001030zlib.Decompress.copy
1031
1032Return a copy of the decompression object.
1033[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034
1035static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001036zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08001037/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038{
1039 compobject *retval = NULL;
1040 int err;
1041
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001042 retval = newcompobject(_zlibstate_global->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043 if (!retval) return NULL;
1044
1045 /* Copy the zstream state
1046 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1047 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001048 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049 err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001050 switch (err) {
1051 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001052 break;
Martin Panter84544c12016-07-23 03:02:07 +00001053 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1055 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001056 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001057 PyErr_SetString(PyExc_MemoryError,
1058 "Can't allocate memory for decompression object");
1059 goto error;
1060 default:
1061 zlib_error(self->zst, err, "while copying decompression object");
1062 goto error;
1063 }
1064
1065 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001066 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001068 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001069 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001070 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001071 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001072
1073 /* Mark it as being initialized */
1074 retval->is_initialised = 1;
1075
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001076 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001077 return (PyObject *)retval;
1078
1079error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001080 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081 Py_XDECREF(retval);
1082 return NULL;
1083}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001084
1085/*[clinic input]
1086zlib.Decompress.__copy__
1087[clinic start generated code]*/
1088
1089static PyObject *
1090zlib_Decompress___copy___impl(compobject *self)
1091/*[clinic end generated code: output=80bae8bc43498ad4 input=efcb98b5472c13d2]*/
1092{
1093 return zlib_Decompress_copy_impl(self);
1094}
1095
1096/*[clinic input]
1097zlib.Decompress.__deepcopy__
1098
1099 memo: object
1100 /
1101
1102[clinic start generated code]*/
1103
1104static PyObject *
1105zlib_Decompress___deepcopy__(compobject *self, PyObject *memo)
1106/*[clinic end generated code: output=1f77286ab490124b input=6e99bd0ac4b9cd8b]*/
1107{
1108 return zlib_Decompress_copy_impl(self);
1109}
1110
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001112
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001113/*[clinic input]
1114zlib.Decompress.flush
1115
Martin Panter84544c12016-07-23 03:02:07 +00001116 length: ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001117 the initial size of the output buffer.
1118 /
1119
1120Return a bytes object containing any remaining decompressed data.
1121[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001122
Guido van Rossumfb221561997-04-29 15:38:09 +00001123static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +00001124zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1125/*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001126{
Martin Panter84544c12016-07-23 03:02:07 +00001127 int err, flush;
1128 Py_buffer data;
1129 PyObject *RetVal = NULL;
1130 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001131
Martin Panter84544c12016-07-23 03:02:07 +00001132 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001133 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1134 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001135 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001136
Martin Panter84544c12016-07-23 03:02:07 +00001137 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001138 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001139
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001140 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001141
Martin Panter84544c12016-07-23 03:02:07 +00001142 self->zst.next_in = data.buf;
1143 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001144
Martin Panter84544c12016-07-23 03:02:07 +00001145 do {
1146 arrange_input_buffer(&self->zst, &ibuflen);
1147 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001148
Martin Panter84544c12016-07-23 03:02:07 +00001149 do {
1150 length = arrange_output_buffer(&self->zst, &RetVal, length);
1151 if (length < 0)
1152 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001153
Martin Panter84544c12016-07-23 03:02:07 +00001154 Py_BEGIN_ALLOW_THREADS
1155 err = inflate(&self->zst, flush);
1156 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001157
Martin Panter84544c12016-07-23 03:02:07 +00001158 switch (err) {
1159 case Z_OK: /* fall through */
1160 case Z_BUF_ERROR: /* fall through */
1161 case Z_STREAM_END:
1162 break;
1163 default:
1164 if (err == Z_NEED_DICT && self->zdict != NULL) {
1165 if (set_inflate_zdict(self) < 0)
1166 goto abort;
1167 else
1168 break;
1169 }
1170 goto save;
1171 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001172
Martin Panter84544c12016-07-23 03:02:07 +00001173 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1174
1175 } while (err != Z_STREAM_END && ibuflen != 0);
1176
1177 save:
1178 if (save_unconsumed_input(self, &data, err) < 0)
1179 goto abort;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001180
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001181 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001182 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001183 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001184 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001185 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001186 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001187 zlib_error(self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001188 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001189 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001190 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001191
Martin Panter84544c12016-07-23 03:02:07 +00001192 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1193 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1194 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001195
Martin Panter84544c12016-07-23 03:02:07 +00001196 abort:
1197 Py_CLEAR(RetVal);
1198 success:
1199 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001200 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001201 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001202}
1203
Christian Heimes936e2f32014-01-27 01:06:57 +01001204#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001205
Guido van Rossumfb221561997-04-29 15:38:09 +00001206static PyMethodDef comp_methods[] =
1207{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001208 ZLIB_COMPRESS_COMPRESS_METHODDEF
1209 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001210 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001211 ZLIB_COMPRESS___COPY___METHODDEF
1212 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001213 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001214};
1215
1216static PyMethodDef Decomp_methods[] =
1217{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001218 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001219 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001220 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001221 ZLIB_DECOMPRESS___COPY___METHODDEF
1222 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001223 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001224};
1225
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001226#define COMP_OFF(x) offsetof(compobject, x)
1227static PyMemberDef Decomp_members[] = {
1228 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1229 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001230 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001231 {NULL},
1232};
Guido van Rossumfb221561997-04-29 15:38:09 +00001233
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001234/*[clinic input]
1235zlib.adler32
1236
1237 data: Py_buffer
1238 value: unsigned_int(bitwise=True) = 1
1239 Starting value of the checksum.
1240 /
1241
1242Compute an Adler-32 checksum of data.
1243
1244The returned checksum is an integer.
1245[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001246
Guido van Rossumfb221561997-04-29 15:38:09 +00001247static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001248zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1249/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001250{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001251 /* Releasing the GIL for very small buffers is inefficient
1252 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001253 if (data->len > 1024*5) {
1254 unsigned char *buf = data->buf;
1255 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001256
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001257 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001258 /* Avoid truncation of length for very large buffers. adler32() takes
1259 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001260 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001261 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001262 buf += (size_t) UINT_MAX;
1263 len -= (size_t) UINT_MAX;
1264 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001265 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001266 Py_END_ALLOW_THREADS
1267 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001268 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001269 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001270 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001271}
Tim Peters977e5402001-10-17 03:57:20 +00001272
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001273/*[clinic input]
1274zlib.crc32
1275
1276 data: Py_buffer
1277 value: unsigned_int(bitwise=True) = 0
1278 Starting value of the checksum.
1279 /
1280
1281Compute a CRC-32 checksum of data.
1282
1283The returned checksum is an integer.
1284[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001285
1286static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001287zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1288/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001289{
Martin v. Löwis423be952008-08-13 15:53:07 +00001290 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001291
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001292 /* Releasing the GIL for very small buffers is inefficient
1293 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001294 if (data->len > 1024*5) {
1295 unsigned char *buf = data->buf;
1296 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001297
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001298 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001299 /* Avoid truncation of length for very large buffers. crc32() takes
1300 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001301 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001302 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001303 buf += (size_t) UINT_MAX;
1304 len -= (size_t) UINT_MAX;
1305 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001306 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001307 Py_END_ALLOW_THREADS
1308 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001309 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001310 }
Christian Heimescc47b052008-03-25 14:56:36 +00001311 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001312}
Tim Peters977e5402001-10-17 03:57:20 +00001313
Guido van Rossumfb221561997-04-29 15:38:09 +00001314
1315static PyMethodDef zlib_methods[] =
1316{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001317 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001318 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001319 ZLIB_COMPRESSOBJ_METHODDEF
1320 ZLIB_CRC32_METHODDEF
1321 ZLIB_DECOMPRESS_METHODDEF
1322 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001323 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001324};
1325
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001326static PyType_Slot Comptype_slots[] = {
1327 {Py_tp_dealloc, Comp_dealloc},
1328 {Py_tp_methods, comp_methods},
1329 {0, 0},
1330};
1331
1332static PyType_Spec Comptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001333 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001334 sizeof(compobject),
1335 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001336 Py_TPFLAGS_DEFAULT,
1337 Comptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001338};
1339
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001340static PyType_Slot Decomptype_slots[] = {
1341 {Py_tp_dealloc, Decomp_dealloc},
1342 {Py_tp_methods, Decomp_methods},
1343 {Py_tp_members, Decomp_members},
1344 {0, 0},
1345};
1346
1347static PyType_Spec Decomptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001348 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001349 sizeof(compobject),
1350 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001351 Py_TPFLAGS_DEFAULT,
1352 Decomptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001353};
1354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001355PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001356"The functions in this module allow compression and decompression using the\n"
1357"zlib library, which is based on GNU zip.\n"
1358"\n"
1359"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001360"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001361"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001362"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001363"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001364"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001365"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001366"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001367"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001368"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001369
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001370static int
1371zlib_clear(PyObject *m)
1372{
Hai Shif707d942020-03-16 21:15:01 +08001373 _zlibstate *state = get_zlib_state(m);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001374 Py_CLEAR(state->Comptype);
1375 Py_CLEAR(state->Decomptype);
1376 Py_CLEAR(state->ZlibError);
1377 return 0;
1378}
1379
1380static int
1381zlib_traverse(PyObject *m, visitproc visit, void *arg)
1382{
Hai Shif707d942020-03-16 21:15:01 +08001383 _zlibstate *state = get_zlib_state(m);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001384 Py_VISIT(state->Comptype);
1385 Py_VISIT(state->Decomptype);
1386 Py_VISIT(state->ZlibError);
1387 return 0;
1388}
1389
1390static void
1391zlib_free(void *m)
1392{
1393 zlib_clear((PyObject *)m);
1394}
1395
Martin v. Löwis1a214512008-06-11 05:26:20 +00001396static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001397 PyModuleDef_HEAD_INIT,
1398 "zlib",
1399 zlib_module_documentation,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001400 sizeof(_zlibstate),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001401 zlib_methods,
1402 NULL,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001403 zlib_traverse,
1404 zlib_clear,
1405 zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001406};
1407
Mark Hammond62b1ab12002-07-23 06:31:15 +00001408PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001409PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001410{
Fred Drake4baedc12002-04-01 14:53:37 +00001411 PyObject *m, *ver;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001412 m = PyState_FindModule(&zlibmodule);
1413 if (m != NULL) {
1414 Py_INCREF(m);
1415 return m;
1416 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001417 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001418 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001419 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001420
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001421 PyTypeObject *Comptype = (PyTypeObject *)PyType_FromSpec(&Comptype_spec);
1422 if (Comptype == NULL)
1423 return NULL;
Hai Shif707d942020-03-16 21:15:01 +08001424 get_zlib_state(m)->Comptype = Comptype;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001425
1426 PyTypeObject *Decomptype = (PyTypeObject *)PyType_FromSpec(&Decomptype_spec);
1427 if (Decomptype == NULL)
1428 return NULL;
Hai Shif707d942020-03-16 21:15:01 +08001429 get_zlib_state(m)->Decomptype = Decomptype;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001430
1431 PyObject *ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Fred Drake4baedc12002-04-01 14:53:37 +00001432 if (ZlibError != NULL) {
1433 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001434 PyModule_AddObject(m, "error", ZlibError);
Hai Shif707d942020-03-16 21:15:01 +08001435 get_zlib_state(m)->ZlibError = ZlibError;
Fred Drake4baedc12002-04-01 14:53:37 +00001436 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001437 PyModule_AddIntMacro(m, MAX_WBITS);
1438 PyModule_AddIntMacro(m, DEFLATED);
1439 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001440 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001441 // compression levels
1442 PyModule_AddIntMacro(m, Z_NO_COMPRESSION);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001443 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1444 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1445 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001446 // compression strategies
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001447 PyModule_AddIntMacro(m, Z_FILTERED);
1448 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001449#ifdef Z_RLE // 1.2.0.1
1450 PyModule_AddIntMacro(m, Z_RLE);
1451#endif
1452#ifdef Z_FIXED // 1.2.2.2
1453 PyModule_AddIntMacro(m, Z_FIXED);
1454#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001455 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001456 // allowed flush values
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001457 PyModule_AddIntMacro(m, Z_NO_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001458 PyModule_AddIntMacro(m, Z_PARTIAL_FLUSH);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001459 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1460 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001461 PyModule_AddIntMacro(m, Z_FINISH);
1462#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1463 PyModule_AddIntMacro(m, Z_BLOCK);
1464#endif
1465#ifdef Z_TREES // 1.2.3.4, only for inflate
1466 PyModule_AddIntMacro(m, Z_TREES);
1467#endif
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001468 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001469 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001470 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001471
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001472 ver = PyUnicode_FromString(zlibVersion());
1473 if (ver != NULL)
1474 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1475
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001476 PyModule_AddStringConstant(m, "__version__", "1.0");
1477
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001478 PyState_AddModule(m, &zlibmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001479 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001480}