blob: 49d231a4baacbf090f3627a69daba404613a934c [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"
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00009#include "structmember.h"
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#include "pythread.h"
14#define ENTER_ZLIB(obj) \
15 Py_BEGIN_ALLOW_THREADS; \
16 PyThread_acquire_lock((obj)->lock, 1); \
17 Py_END_ALLOW_THREADS;
18#define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +000019
Martin Panter3f0ee832016-06-05 10:48:34 +000020#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
Martin Panter84544c12016-07-23 03:02:07 +000021# define AT_LEAST_ZLIB_1_2_2_1
Martin Panter3f0ee832016-06-05 10:48:34 +000022#endif
23
Guido van Rossumfb221561997-04-29 15:38:09 +000024/* The following parameters are copied from zutil.h, version 0.95 */
25#define DEFLATED 8
26#if MAX_MEM_LEVEL >= 8
27# define DEF_MEM_LEVEL 8
28#else
29# define DEF_MEM_LEVEL MAX_MEM_LEVEL
30#endif
Guido van Rossumfb221561997-04-29 15:38:09 +000031
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020032/* Initial buffer size. */
33#define DEF_BUF_SIZE (16*1024)
Guido van Rossumfb221561997-04-29 15:38:09 +000034
Dino Viehlanda1ffad02019-09-10 11:27:03 +010035static PyModuleDef zlibmodule;
Guido van Rossumfb221561997-04-29 15:38:09 +000036
Dino Viehlanda1ffad02019-09-10 11:27:03 +010037typedef struct {
38 PyTypeObject *Comptype;
39 PyTypeObject *Decomptype;
40 PyObject *ZlibError;
41} _zlibstate;
42
Hai Shif707d942020-03-16 21:15:01 +080043static inline _zlibstate*
44get_zlib_state(PyObject *module)
45{
46 void *state = PyModule_GetState(module);
47 assert(state != NULL);
48 return (_zlibstate *)state;
49}
50
Dino Viehlanda1ffad02019-09-10 11:27:03 +010051#define _zlibstate_global ((_zlibstate *)PyModule_GetState(PyState_FindModule(&zlibmodule)))
Guido van Rossumfb221561997-04-29 15:38:09 +000052
Tim Peters977e5402001-10-17 03:57:20 +000053typedef struct
Guido van Rossumfb221561997-04-29 15:38:09 +000054{
Jeremy Hylton9714f992001-10-16 21:19:45 +000055 PyObject_HEAD
56 z_stream zst;
57 PyObject *unused_data;
58 PyObject *unconsumed_tail;
Nadeem Vawda1c385462011-08-13 15:22:40 +020059 char eof;
Jeremy Hylton9714f992001-10-16 21:19:45 +000060 int is_initialised;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020061 PyObject *zdict;
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020062 PyThread_type_lock lock;
Guido van Rossumfb221561997-04-29 15:38:09 +000063} compobject;
64
Jeremy Hylton0965e082001-10-16 21:56:09 +000065static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020066zlib_error(z_stream zst, int err, const char *msg)
Jeremy Hylton0965e082001-10-16 21:56:09 +000067{
Nadeem Vawda524148a2011-08-28 11:26:46 +020068 const char *zmsg = Z_NULL;
69 /* In case of a version mismatch, zst.msg won't be initialized.
70 Check for this case first, before looking at zst.msg. */
71 if (err == Z_VERSION_ERROR)
72 zmsg = "library version mismatch";
73 if (zmsg == Z_NULL)
74 zmsg = zst.msg;
Antoine Pitrou96f212b2010-05-11 23:49:58 +000075 if (zmsg == Z_NULL) {
76 switch (err) {
77 case Z_BUF_ERROR:
78 zmsg = "incomplete or truncated stream";
79 break;
80 case Z_STREAM_ERROR:
81 zmsg = "inconsistent stream state";
82 break;
83 case Z_DATA_ERROR:
84 zmsg = "invalid input data";
85 break;
86 }
87 }
88 if (zmsg == Z_NULL)
Dino Viehlanda1ffad02019-09-10 11:27:03 +010089 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s", err, msg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000090 else
Dino Viehlanda1ffad02019-09-10 11:27:03 +010091 PyErr_Format(_zlibstate_global->ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
Jeremy Hylton0965e082001-10-16 21:56:09 +000092}
93
Larry Hastings61272b72014-01-07 12:41:53 -080094/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -080095module zlib
Larry Hastingsc2047262014-01-25 20:43:29 -080096class zlib.Compress "compobject *" "&Comptype"
97class zlib.Decompress "compobject *" "&Decomptype"
Larry Hastings61272b72014-01-07 12:41:53 -080098[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030099/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800100
Guido van Rossumfb221561997-04-29 15:38:09 +0000101static compobject *
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000102newcompobject(PyTypeObject *type)
Guido van Rossumfb221561997-04-29 15:38:09 +0000103{
Tim Peters977e5402001-10-17 03:57:20 +0000104 compobject *self;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000105 self = PyObject_New(compobject, type);
106 if (self == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000107 return NULL;
Nadeem Vawda1c385462011-08-13 15:22:40 +0200108 self->eof = 0;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000109 self->is_initialised = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200110 self->zdict = NULL;
Gregory P. Smith693fc462008-09-06 20:13:06 +0000111 self->unused_data = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000112 if (self->unused_data == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000113 Py_DECREF(self);
114 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000115 }
Gregory P. Smith693fc462008-09-06 20:13:06 +0000116 self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000117 if (self->unconsumed_tail == NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000118 Py_DECREF(self);
119 return NULL;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000120 }
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000121 self->lock = PyThread_allocate_lock();
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200122 if (self->lock == NULL) {
Martin Panter84544c12016-07-23 03:02:07 +0000123 Py_DECREF(self);
Victor Stinnerbf2e2f92013-07-09 00:29:03 +0200124 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
125 return NULL;
126 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000127 return self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000128}
129
Victor Stinner5064a522013-07-07 16:50:27 +0200130static void*
131PyZlib_Malloc(voidpf ctx, uInt items, uInt size)
132{
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300133 if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size)
Victor Stinner5064a522013-07-07 16:50:27 +0200134 return NULL;
135 /* PyMem_Malloc() cannot be used: the GIL is not held when
136 inflate() and deflate() are called */
Alexey Izbyshev3d4fabb2018-10-28 19:45:50 +0300137 return PyMem_RawMalloc((size_t)items * (size_t)size);
Victor Stinner5064a522013-07-07 16:50:27 +0200138}
139
140static void
141PyZlib_Free(voidpf ctx, void *ptr)
142{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200143 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200144}
145
Martin Panter84544c12016-07-23 03:02:07 +0000146static void
147arrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
148{
Segev Finer679b5662017-07-27 01:17:57 +0300149 zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000150 *remains -= zst->avail_in;
151}
152
153static Py_ssize_t
154arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
155 Py_ssize_t length,
156 Py_ssize_t max_length)
157{
158 Py_ssize_t occupied;
159
160 if (*buffer == NULL) {
161 if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
162 return -1;
163 occupied = 0;
164 }
165 else {
166 occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
167
168 if (length == occupied) {
169 Py_ssize_t new_length;
170 assert(length <= max_length);
171 /* can not scale the buffer over max_length */
172 if (length == max_length)
173 return -2;
174 if (length <= (max_length >> 1))
175 new_length = length << 1;
176 else
177 new_length = max_length;
178 if (_PyBytes_Resize(buffer, new_length) < 0)
179 return -1;
180 length = new_length;
181 }
182 }
183
Segev Finer679b5662017-07-27 01:17:57 +0300184 zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX);
Martin Panter84544c12016-07-23 03:02:07 +0000185 zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
186
187 return length;
188}
189
190static Py_ssize_t
191arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
192{
193 Py_ssize_t ret;
194
195 ret = arrange_output_buffer_with_maximum(zst, buffer, length,
196 PY_SSIZE_T_MAX);
197 if (ret == -2)
198 PyErr_NoMemory();
199
200 return ret;
201}
202
Larry Hastings61272b72014-01-07 12:41:53 -0800203/*[clinic input]
Larry Hastingsebdcb502013-11-23 14:54:00 -0800204zlib.compress
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200205
Martin Panter1fe0d132016-02-10 10:06:36 +0000206 data: Py_buffer
Larry Hastingsebdcb502013-11-23 14:54:00 -0800207 Binary data to be compressed.
Serhiy Storchaka95657cd2016-06-25 22:43:05 +0300208 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200209 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter1fe0d132016-02-10 10:06:36 +0000210 Compression level, in 0-9 or -1.
Larry Hastingsebdcb502013-11-23 14:54:00 -0800211
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200212Returns a bytes object containing compressed data.
Larry Hastings61272b72014-01-07 12:41:53 -0800213[clinic start generated code]*/
Larry Hastingsebdcb502013-11-23 14:54:00 -0800214
Guido van Rossumfb221561997-04-29 15:38:09 +0000215static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300216zlib_compress_impl(PyObject *module, Py_buffer *data, int level)
217/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000218{
Martin Panter84544c12016-07-23 03:02:07 +0000219 PyObject *RetVal = NULL;
220 Byte *ibuf;
221 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
222 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000223 z_stream zst;
Tim Peters977e5402001-10-17 03:57:20 +0000224
Martin Panter525a9492016-07-23 03:39:49 +0000225 ibuf = data->buf;
226 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000227
Victor Stinner5064a522013-07-07 16:50:27 +0200228 zst.opaque = NULL;
229 zst.zalloc = PyZlib_Malloc;
230 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000231 zst.next_in = ibuf;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000232 err = deflateInit(&zst, level);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000233
Martin Panter84544c12016-07-23 03:02:07 +0000234 switch (err) {
235 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 break;
Martin Panter84544c12016-07-23 03:02:07 +0000237 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238 PyErr_SetString(PyExc_MemoryError,
239 "Out of memory while compressing data");
240 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000241 case Z_STREAM_ERROR:
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100242 PyErr_SetString(_zlibstate_global->ZlibError, "Bad compression level");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000243 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000244 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000245 deflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000246 zlib_error(zst, err, "while compressing data");
247 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000248 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000249
Martin Panter84544c12016-07-23 03:02:07 +0000250 do {
251 arrange_input_buffer(&zst, &ibuflen);
252 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000253
Martin Panter84544c12016-07-23 03:02:07 +0000254 do {
255 obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
256 if (obuflen < 0) {
257 deflateEnd(&zst);
258 goto error;
259 }
260
261 Py_BEGIN_ALLOW_THREADS
262 err = deflate(&zst, flush);
263 Py_END_ALLOW_THREADS
264
265 if (err == Z_STREAM_ERROR) {
266 deflateEnd(&zst);
267 zlib_error(zst, err, "while compressing data");
268 goto error;
269 }
270
271 } while (zst.avail_out == 0);
272 assert(zst.avail_in == 0);
273
274 } while (flush != Z_FINISH);
275 assert(err == Z_STREAM_END);
276
277 err = deflateEnd(&zst);
278 if (err == Z_OK) {
279 if (_PyBytes_Resize(&RetVal, zst.next_out -
280 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
281 goto error;
282 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000283 }
Tim Peters977e5402001-10-17 03:57:20 +0000284 else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 zlib_error(zst, err, "while finishing compression");
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000286 error:
Martin Panter84544c12016-07-23 03:02:07 +0000287 Py_XDECREF(RetVal);
288 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000289}
290
Larry Hastings61272b72014-01-07 12:41:53 -0800291/*[python input]
Victor Stinnere079edd2013-11-21 22:33:21 +0100292
Martin Panter84544c12016-07-23 03:02:07 +0000293class ssize_t_converter(CConverter):
294 type = 'Py_ssize_t'
295 converter = 'ssize_t_converter'
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200296 c_ignored_default = "0"
Victor Stinnere079edd2013-11-21 22:33:21 +0100297
Larry Hastings61272b72014-01-07 12:41:53 -0800298[python start generated code]*/
Martin Panter84544c12016-07-23 03:02:07 +0000299/*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
Victor Stinnere079edd2013-11-21 22:33:21 +0100300
301static int
Martin Panter84544c12016-07-23 03:02:07 +0000302ssize_t_converter(PyObject *obj, void *ptr)
Victor Stinnere079edd2013-11-21 22:33:21 +0100303{
Martin Pantere99e9772015-11-20 08:13:35 +0000304 PyObject *long_obj;
305 Py_ssize_t val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100306
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200307 /* XXX Should be replaced with PyNumber_AsSsize_t after the end of the
308 deprecation period. */
309 long_obj = _PyLong_FromNbIndexOrNbInt(obj);
Martin Pantere99e9772015-11-20 08:13:35 +0000310 if (long_obj == NULL) {
311 return 0;
312 }
313 val = PyLong_AsSsize_t(long_obj);
314 Py_DECREF(long_obj);
Victor Stinnere079edd2013-11-21 22:33:21 +0100315 if (val == -1 && PyErr_Occurred()) {
Martin Pantere99e9772015-11-20 08:13:35 +0000316 return 0;
Victor Stinnere079edd2013-11-21 22:33:21 +0100317 }
Martin Panter84544c12016-07-23 03:02:07 +0000318 *(Py_ssize_t *)ptr = val;
Victor Stinnere079edd2013-11-21 22:33:21 +0100319 return 1;
320}
321
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200322/*[clinic input]
323zlib.decompress
324
325 data: Py_buffer
326 Compressed data.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300327 /
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200328 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000329 The window buffer size and container format.
Martin Panter84544c12016-07-23 03:02:07 +0000330 bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200331 The initial output buffer size.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200332
333Returns a bytes object containing the uncompressed data.
334[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000335
Guido van Rossumfb221561997-04-29 15:38:09 +0000336static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300337zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +0000338 Py_ssize_t bufsize)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300339/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000340{
Martin Panter84544c12016-07-23 03:02:07 +0000341 PyObject *RetVal = NULL;
342 Byte *ibuf;
343 Py_ssize_t ibuflen;
344 int err, flush;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000345 z_stream zst;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000346
Martin Panter84544c12016-07-23 03:02:07 +0000347 if (bufsize < 0) {
348 PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
349 return NULL;
350 } else if (bufsize == 0) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100351 bufsize = 1;
Martin Panter84544c12016-07-23 03:02:07 +0000352 }
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000353
Martin Panter84544c12016-07-23 03:02:07 +0000354 ibuf = data->buf;
355 ibuflen = data->len;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000356
Victor Stinner5064a522013-07-07 16:50:27 +0200357 zst.opaque = NULL;
358 zst.zalloc = PyZlib_Malloc;
359 zst.zfree = PyZlib_Free;
Martin Panter84544c12016-07-23 03:02:07 +0000360 zst.avail_in = 0;
361 zst.next_in = ibuf;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200362 err = inflateInit2(&zst, wbits);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000363
Martin Panter84544c12016-07-23 03:02:07 +0000364 switch (err) {
365 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 break;
Martin Panter84544c12016-07-23 03:02:07 +0000367 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000368 PyErr_SetString(PyExc_MemoryError,
369 "Out of memory while decompressing data");
370 goto error;
Jeremy Hylton0965e082001-10-16 21:56:09 +0000371 default:
Guido van Rossumfb221561997-04-29 15:38:09 +0000372 inflateEnd(&zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 zlib_error(zst, err, "while preparing to decompress data");
374 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000375 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000376
Jeremy Hylton9714f992001-10-16 21:19:45 +0000377 do {
Martin Panter84544c12016-07-23 03:02:07 +0000378 arrange_input_buffer(&zst, &ibuflen);
379 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000380
Martin Panter84544c12016-07-23 03:02:07 +0000381 do {
382 bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
383 if (bufsize < 0) {
384 inflateEnd(&zst);
385 goto error;
386 }
387
388 Py_BEGIN_ALLOW_THREADS
389 err = inflate(&zst, flush);
390 Py_END_ALLOW_THREADS
391
392 switch (err) {
393 case Z_OK: /* fall through */
394 case Z_BUF_ERROR: /* fall through */
395 case Z_STREAM_END:
396 break;
397 case Z_MEM_ERROR:
398 inflateEnd(&zst);
399 PyErr_SetString(PyExc_MemoryError,
400 "Out of memory while decompressing data");
401 goto error;
402 default:
403 inflateEnd(&zst);
Antoine Pitrou96f212b2010-05-11 23:49:58 +0000404 zlib_error(zst, err, "while decompressing data");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000405 goto error;
406 }
Martin Panter84544c12016-07-23 03:02:07 +0000407
408 } while (zst.avail_out == 0);
409
410 } while (err != Z_STREAM_END && ibuflen != 0);
411
412
413 if (err != Z_STREAM_END) {
414 inflateEnd(&zst);
415 zlib_error(zst, err, "while decompressing data");
416 goto error;
417 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000418
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000419 err = inflateEnd(&zst);
420 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200421 zlib_error(zst, err, "while finishing decompression");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000422 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000423 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000424
Martin Panter84544c12016-07-23 03:02:07 +0000425 if (_PyBytes_Resize(&RetVal, zst.next_out -
426 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Guido van Rossum776152b2007-05-22 22:44:07 +0000427 goto error;
428
Martin Panter84544c12016-07-23 03:02:07 +0000429 return RetVal;
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000430
431 error:
Martin Panter84544c12016-07-23 03:02:07 +0000432 Py_XDECREF(RetVal);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000433 return NULL;
Guido van Rossumfb221561997-04-29 15:38:09 +0000434}
435
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200436/*[clinic input]
437zlib.compressobj
438
439 level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
Martin Panter567d5132016-02-03 07:06:33 +0000440 The compression level (an integer in the range 0-9 or -1; default is
441 currently equivalent to 6). Higher compression levels are slower,
442 but produce smaller results.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200443 method: int(c_default="DEFLATED") = DEFLATED
444 The compression algorithm. If given, this must be DEFLATED.
445 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000446 +9 to +15: The base-two logarithm of the window size. Include a zlib
447 container.
448 -9 to -15: Generate a raw stream.
449 +25 to +31: Include a gzip container.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200450 memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
451 Controls the amount of memory used for internal compression state.
452 Valid values range from 1 to 9. Higher values result in higher memory
453 usage, faster compression, and smaller output.
454 strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
455 Used to tune the compression algorithm. Possible values are
456 Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
457 zdict: Py_buffer = None
458 The predefined compression dictionary - a sequence of bytes
459 containing subsequences that are likely to occur in the input data.
460
461Return a compressor object.
462[clinic start generated code]*/
463
Guido van Rossumfb221561997-04-29 15:38:09 +0000464static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300465zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400466 int memLevel, int strategy, Py_buffer *zdict)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300467/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000468{
Victor Stinnere079edd2013-11-21 22:33:21 +0100469 compobject *self = NULL;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200470 int err;
Jeremy Hylton41b9f001997-08-13 23:19:55 +0000471
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200472 if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
Victor Stinnere079edd2013-11-21 22:33:21 +0100473 PyErr_SetString(PyExc_OverflowError,
474 "zdict length does not fit in an unsigned int");
475 goto error;
476 }
477
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100478 self = newcompobject(_zlibstate_global->Comptype);
Martin Panter84544c12016-07-23 03:02:07 +0000479 if (self == NULL)
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200480 goto error;
Victor Stinner5064a522013-07-07 16:50:27 +0200481 self->zst.opaque = NULL;
482 self->zst.zalloc = PyZlib_Malloc;
483 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000484 self->zst.next_in = NULL;
485 self->zst.avail_in = 0;
Jeremy Hylton499000002001-10-16 21:59:35 +0000486 err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
Martin Panter84544c12016-07-23 03:02:07 +0000487 switch (err) {
488 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000489 self->is_initialised = 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200490 if (zdict->buf == NULL) {
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200491 goto success;
492 } else {
Victor Stinnere079edd2013-11-21 22:33:21 +0100493 err = deflateSetDictionary(&self->zst,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200494 zdict->buf, (unsigned int)zdict->len);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200495 switch (err) {
Martin Panter84544c12016-07-23 03:02:07 +0000496 case Z_OK:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200497 goto success;
Martin Panter84544c12016-07-23 03:02:07 +0000498 case Z_STREAM_ERROR:
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200499 PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
500 goto error;
501 default:
502 PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
503 goto error;
504 }
505 }
Martin Panter84544c12016-07-23 03:02:07 +0000506 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 PyErr_SetString(PyExc_MemoryError,
508 "Can't allocate memory for compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200509 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000510 case Z_STREAM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000511 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200512 goto error;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000513 default:
514 zlib_error(self->zst, err, "while creating compression object");
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200515 goto error;
Guido van Rossumfb221561997-04-29 15:38:09 +0000516 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200517
518 error:
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200519 Py_CLEAR(self);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200520 success:
Martin Panter84544c12016-07-23 03:02:07 +0000521 return (PyObject *)self;
Guido van Rossumfb221561997-04-29 15:38:09 +0000522}
523
Martin Panter3f0ee832016-06-05 10:48:34 +0000524static int
525set_inflate_zdict(compobject *self)
526{
527 Py_buffer zdict_buf;
528 int err;
529
530 if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
531 return -1;
532 }
533 if ((size_t)zdict_buf.len > UINT_MAX) {
534 PyErr_SetString(PyExc_OverflowError,
535 "zdict length does not fit in an unsigned int");
536 PyBuffer_Release(&zdict_buf);
537 return -1;
538 }
Martin Panter84544c12016-07-23 03:02:07 +0000539 err = inflateSetDictionary(&self->zst,
Martin Panter3f0ee832016-06-05 10:48:34 +0000540 zdict_buf.buf, (unsigned int)zdict_buf.len);
541 PyBuffer_Release(&zdict_buf);
542 if (err != Z_OK) {
543 zlib_error(self->zst, err, "while setting zdict");
544 return -1;
545 }
546 return 0;
547}
548
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200549/*[clinic input]
550zlib.decompressobj
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200551
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200552 wbits: int(c_default="MAX_WBITS") = MAX_WBITS
Martin Panter0fdf41d2016-05-27 07:32:11 +0000553 The window buffer size and container format.
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200554 zdict: object(c_default="NULL") = b''
555 The predefined compression dictionary. This must be the same
556 dictionary as used by the compressor that produced the input data.
557
558Return a decompressor object.
559[clinic start generated code]*/
560
561static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300562zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
563/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200564{
565 int err;
566 compobject *self;
567
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200568 if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
569 PyErr_SetString(PyExc_TypeError,
570 "zdict argument must support the buffer protocol");
571 return NULL;
572 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000573
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100574 self = newcompobject(_zlibstate_global->Decomptype);
Tim Peters977e5402001-10-17 03:57:20 +0000575 if (self == NULL)
Martin Panter84544c12016-07-23 03:02:07 +0000576 return NULL;
Victor Stinner5064a522013-07-07 16:50:27 +0200577 self->zst.opaque = NULL;
578 self->zst.zalloc = PyZlib_Malloc;
579 self->zst.zfree = PyZlib_Free;
Andrew M. Kuchling3b585b32004-12-28 20:10:48 +0000580 self->zst.next_in = NULL;
581 self->zst.avail_in = 0;
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200582 if (zdict != NULL) {
583 Py_INCREF(zdict);
584 self->zdict = zdict;
585 }
Jeremy Hylton499000002001-10-16 21:59:35 +0000586 err = inflateInit2(&self->zst, wbits);
Martin Panter84544c12016-07-23 03:02:07 +0000587 switch (err) {
588 case Z_OK:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000589 self->is_initialised = 1;
Martin Panter3f0ee832016-06-05 10:48:34 +0000590 if (self->zdict != NULL && wbits < 0) {
591#ifdef AT_LEAST_ZLIB_1_2_2_1
592 if (set_inflate_zdict(self) < 0) {
593 Py_DECREF(self);
594 return NULL;
595 }
596#else
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100597 PyErr_Format(_zlibstate_global->ZlibError,
Martin Panter3f0ee832016-06-05 10:48:34 +0000598 "zlib version %s does not allow raw inflate with dictionary",
599 ZLIB_VERSION);
600 Py_DECREF(self);
601 return NULL;
602#endif
603 }
Martin Panter84544c12016-07-23 03:02:07 +0000604 return (PyObject *)self;
605 case Z_STREAM_ERROR:
Andrew M. Kuchling1c7aaa21999-01-29 21:49:34 +0000606 Py_DECREF(self);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607 PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
608 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000609 case Z_MEM_ERROR:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 Py_DECREF(self);
611 PyErr_SetString(PyExc_MemoryError,
612 "Can't allocate memory for decompression object");
613 return NULL;
614 default:
615 zlib_error(self->zst, err, "while creating decompression object");
616 Py_DECREF(self);
617 return NULL;
Jeremy Hylton499000002001-10-16 21:59:35 +0000618 }
Guido van Rossumfb221561997-04-29 15:38:09 +0000619}
620
621static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000622Dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000623{
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100624 PyObject *type = (PyObject *)Py_TYPE(self);
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000625 PyThread_free_lock(self->lock);
Andrew M. Kuchlingb95227d1999-03-25 21:21:08 +0000626 Py_XDECREF(self->unused_data);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000627 Py_XDECREF(self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200628 Py_XDECREF(self->zdict);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000629 PyObject_Del(self);
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100630 Py_DECREF(type);
Guido van Rossumfb221561997-04-29 15:38:09 +0000631}
632
633static void
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000634Comp_dealloc(compobject *self)
635{
636 if (self->is_initialised)
637 deflateEnd(&self->zst);
638 Dealloc(self);
639}
640
641static void
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +0000642Decomp_dealloc(compobject *self)
Guido van Rossumfb221561997-04-29 15:38:09 +0000643{
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000644 if (self->is_initialised)
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000645 inflateEnd(&self->zst);
646 Dealloc(self);
Guido van Rossumfb221561997-04-29 15:38:09 +0000647}
648
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200649/*[clinic input]
650zlib.Compress.compress
Guido van Rossum3c540301997-06-03 22:21:03 +0000651
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200652 data: Py_buffer
653 Binary data to be compressed.
654 /
655
656Returns a bytes object containing compressed data.
657
658After calling this function, some of the input data may still
659be stored in internal buffers for later processing.
660Call the flush() method to clear these buffers.
661[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000662
Guido van Rossumfb221561997-04-29 15:38:09 +0000663static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200664zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800665/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000666{
Martin Panter84544c12016-07-23 03:02:07 +0000667 PyObject *RetVal = NULL;
668 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200669 int err;
Tim Peters977e5402001-10-17 03:57:20 +0000670
Martin Panter84544c12016-07-23 03:02:07 +0000671 self->zst.next_in = data->buf;
672 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000673
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000674 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000675
Martin Panter84544c12016-07-23 03:02:07 +0000676 do {
677 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000678
Martin Panter84544c12016-07-23 03:02:07 +0000679 do {
680 obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
681 if (obuflen < 0)
682 goto error;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000683
Martin Panter84544c12016-07-23 03:02:07 +0000684 Py_BEGIN_ALLOW_THREADS
685 err = deflate(&self->zst, Z_NO_FLUSH);
686 Py_END_ALLOW_THREADS
Tim Peters977e5402001-10-17 03:57:20 +0000687
Martin Panter84544c12016-07-23 03:02:07 +0000688 if (err == Z_STREAM_ERROR) {
689 zlib_error(self->zst, err, "while compressing data");
690 goto error;
691 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000692
Martin Panter84544c12016-07-23 03:02:07 +0000693 } while (self->zst.avail_out == 0);
694 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000695
Martin Panter84544c12016-07-23 03:02:07 +0000696 } while (ibuflen != 0);
697
698 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
699 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
700 goto success;
701
702 error:
703 Py_CLEAR(RetVal);
704 success:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000705 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000706 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000707}
708
Martin Panter84544c12016-07-23 03:02:07 +0000709/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100710 self->unused_data or self->unconsumed_tail, as appropriate. */
711static int
Martin Panter84544c12016-07-23 03:02:07 +0000712save_unconsumed_input(compobject *self, Py_buffer *data, int err)
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100713{
714 if (err == Z_STREAM_END) {
715 /* The end of the compressed data has been reached. Store the leftover
716 input data in self->unused_data. */
717 if (self->zst.avail_in > 0) {
718 Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
Martin Panter84544c12016-07-23 03:02:07 +0000719 Py_ssize_t new_size, left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100720 PyObject *new_data;
Martin Panter84544c12016-07-23 03:02:07 +0000721 left_size = (Byte *)data->buf + data->len - self->zst.next_in;
722 if (left_size > (PY_SSIZE_T_MAX - old_size)) {
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100723 PyErr_NoMemory();
724 return -1;
725 }
Martin Panter84544c12016-07-23 03:02:07 +0000726 new_size = old_size + left_size;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100727 new_data = PyBytes_FromStringAndSize(NULL, new_size);
728 if (new_data == NULL)
729 return -1;
Christian Heimesf051e432016-09-13 20:22:02 +0200730 memcpy(PyBytes_AS_STRING(new_data),
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100731 PyBytes_AS_STRING(self->unused_data), old_size);
Christian Heimesf051e432016-09-13 20:22:02 +0200732 memcpy(PyBytes_AS_STRING(new_data) + old_size,
Martin Panter84544c12016-07-23 03:02:07 +0000733 self->zst.next_in, left_size);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300734 Py_SETREF(self->unused_data, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100735 self->zst.avail_in = 0;
736 }
737 }
Martin Panter84544c12016-07-23 03:02:07 +0000738
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100739 if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
740 /* This code handles two distinct cases:
741 1. Output limit was reached. Save leftover input in unconsumed_tail.
742 2. All input data was consumed. Clear unconsumed_tail. */
Martin Panter84544c12016-07-23 03:02:07 +0000743 Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100744 PyObject *new_data = PyBytes_FromStringAndSize(
Martin Panter84544c12016-07-23 03:02:07 +0000745 (char *)self->zst.next_in, left_size);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100746 if (new_data == NULL)
747 return -1;
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300748 Py_SETREF(self->unconsumed_tail, new_data);
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100749 }
Martin Panter84544c12016-07-23 03:02:07 +0000750
Nadeem Vawdaee7889d2012-11-11 02:14:36 +0100751 return 0;
752}
753
Larry Hastings61272b72014-01-07 12:41:53 -0800754/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800755zlib.Decompress.decompress
Larry Hastings31826802013-10-19 00:09:25 -0700756
757 data: Py_buffer
758 The binary data to decompress.
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300759 /
Martin Panter84544c12016-07-23 03:02:07 +0000760 max_length: ssize_t = 0
Larry Hastings31826802013-10-19 00:09:25 -0700761 The maximum allowable length of the decompressed data.
762 Unconsumed input data will be stored in
763 the unconsumed_tail attribute.
Larry Hastings31826802013-10-19 00:09:25 -0700764
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200765Return a bytes object containing the decompressed version of the data.
Larry Hastings31826802013-10-19 00:09:25 -0700766
767After calling this function, some of the input data may still be stored in
768internal buffers for later processing.
769Call the flush() method to clear these buffers.
Larry Hastings61272b72014-01-07 12:41:53 -0800770[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700771
Larry Hastings31826802013-10-19 00:09:25 -0700772static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400773zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000774 Py_ssize_t max_length)
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300775/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
Larry Hastings31826802013-10-19 00:09:25 -0700776{
Martin Panter84544c12016-07-23 03:02:07 +0000777 int err = Z_OK;
778 Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
Nadeem Vawda0c3d96a2011-05-15 00:19:50 +0200779 PyObject *RetVal = NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +0000780
Martin Panter84544c12016-07-23 03:02:07 +0000781 if (max_length < 0) {
782 PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
Larry Hastings31826802013-10-19 00:09:25 -0700783 return NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000784 } else if (max_length == 0)
785 hard_limit = PY_SSIZE_T_MAX;
786 else
787 hard_limit = max_length;
788
789 self->zst.next_in = data->buf;
790 ibuflen = data->len;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000791
Jeremy Hylton9714f992001-10-16 21:19:45 +0000792 /* limit amount of data allocated to max_length */
Martin Panter84544c12016-07-23 03:02:07 +0000793 if (max_length && obuflen > max_length)
794 obuflen = max_length;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000795
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800796 ENTER_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000797
Martin Panter84544c12016-07-23 03:02:07 +0000798 do {
799 arrange_input_buffer(&self->zst, &ibuflen);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000800
Martin Panter84544c12016-07-23 03:02:07 +0000801 do {
802 obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
803 obuflen, hard_limit);
804 if (obuflen == -2) {
805 if (max_length > 0) {
806 goto save;
807 }
808 PyErr_NoMemory();
809 }
810 if (obuflen < 0) {
811 goto abort;
812 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000813
Martin Panter84544c12016-07-23 03:02:07 +0000814 Py_BEGIN_ALLOW_THREADS
815 err = inflate(&self->zst, Z_SYNC_FLUSH);
816 Py_END_ALLOW_THREADS
Victor Stinnere079edd2013-11-21 22:33:21 +0100817
Martin Panter84544c12016-07-23 03:02:07 +0000818 switch (err) {
819 case Z_OK: /* fall through */
820 case Z_BUF_ERROR: /* fall through */
821 case Z_STREAM_END:
822 break;
823 default:
824 if (err == Z_NEED_DICT && self->zdict != NULL) {
825 if (set_inflate_zdict(self) < 0)
826 goto abort;
827 else
828 break;
829 }
830 goto save;
831 }
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200832
Martin Panter84544c12016-07-23 03:02:07 +0000833 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
Jeremy Hylton511e2ca2001-10-16 20:39:49 +0000834
Martin Panter84544c12016-07-23 03:02:07 +0000835 } while (err != Z_STREAM_END && ibuflen != 0);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000836
Martin Panter84544c12016-07-23 03:02:07 +0000837 save:
838 if (save_unconsumed_input(self, data, err) < 0)
839 goto abort;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000840
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000841 if (err == Z_STREAM_END) {
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100842 /* This is the logical place to call inflateEnd, but the old behaviour
843 of only calling it on flush() is preserved. */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800844 self->eof = 1;
Nadeem Vawdadd1253a2012-11-11 02:21:22 +0100845 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000846 /* We will only get Z_BUF_ERROR if the output buffer was full
847 but there wasn't more output when we tried again, so it is
848 not an error condition.
849 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800850 zlib_error(self->zst, err, "while decompressing data");
Martin Panter84544c12016-07-23 03:02:07 +0000851 goto abort;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000852 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000853
Martin Panter84544c12016-07-23 03:02:07 +0000854 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
855 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
856 goto success;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000857
Martin Panter84544c12016-07-23 03:02:07 +0000858 abort:
859 Py_CLEAR(RetVal);
860 success:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800861 LEAVE_ZLIB(self);
Jeremy Hylton9714f992001-10-16 21:19:45 +0000862 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000863}
864
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200865/*[clinic input]
866zlib.Compress.flush
867
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200868 mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200869 One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
870 If mode == Z_FINISH, the compressor object can no longer be
871 used after calling the flush() method. Otherwise, more data
872 can still be compressed.
873 /
874
875Return a bytes object containing any remaining compressed data.
876[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +0000877
Guido van Rossumfb221561997-04-29 15:38:09 +0000878static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200879zlib_Compress_flush_impl(compobject *self, int mode)
Serhiy Storchaka54c13912014-02-05 13:34:01 +0200880/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
Guido van Rossumfb221561997-04-29 15:38:09 +0000881{
Victor Stinnere079edd2013-11-21 22:33:21 +0100882 int err;
Martin Panter84544c12016-07-23 03:02:07 +0000883 Py_ssize_t length = DEF_BUF_SIZE;
884 PyObject *RetVal = NULL;
Jeremy Hyltona37e2441998-12-18 22:13:11 +0000885
Jeremy Hylton9714f992001-10-16 21:19:45 +0000886 /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
887 doing any work at all; just return an empty string. */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200888 if (mode == Z_NO_FLUSH) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000889 return PyBytes_FromStringAndSize(NULL, 0);
Andrew M. Kuchling9aff4a22001-02-21 02:15:56 +0000890 }
Jeremy Hylton9714f992001-10-16 21:19:45 +0000891
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000892 ENTER_ZLIB(self);
Tim Peters977e5402001-10-17 03:57:20 +0000893
Jeremy Hylton9714f992001-10-16 21:19:45 +0000894 self->zst.avail_in = 0;
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000895
Martin Panter84544c12016-07-23 03:02:07 +0000896 do {
897 length = arrange_output_buffer(&self->zst, &RetVal, length);
898 if (length < 0) {
Victor Stinner79799262013-07-09 00:35:22 +0200899 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000900 goto error;
Guido van Rossum776152b2007-05-22 22:44:07 +0000901 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000902
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000903 Py_BEGIN_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000904 err = deflate(&self->zst, mode);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000905 Py_END_ALLOW_THREADS
Martin Panter84544c12016-07-23 03:02:07 +0000906
907 if (err == Z_STREAM_ERROR) {
908 zlib_error(self->zst, err, "while flushing");
909 Py_CLEAR(RetVal);
910 goto error;
911 }
912 } while (self->zst.avail_out == 0);
913 assert(self->zst.avail_in == 0);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000914
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200915 /* If mode is Z_FINISH, we also have to call deflateEnd() to free
Tim Peters977e5402001-10-17 03:57:20 +0000916 various data structures. Note we should only get Z_STREAM_END when
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200917 mode is Z_FINISH, but checking both for safety*/
918 if (err == Z_STREAM_END && mode == Z_FINISH) {
Martin Panter84544c12016-07-23 03:02:07 +0000919 err = deflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000920 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +0200921 zlib_error(self->zst, err, "while finishing compression");
Martin Panter84544c12016-07-23 03:02:07 +0000922 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000923 goto error;
924 }
925 else
926 self->is_initialised = 0;
Tim Peters977e5402001-10-17 03:57:20 +0000927
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000928 /* We will only get Z_BUF_ERROR if the output buffer was full
929 but there wasn't more output when we tried again, so it is
930 not an error condition.
931 */
Martin Panter84544c12016-07-23 03:02:07 +0000932 } else if (err != Z_OK && err != Z_BUF_ERROR) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000933 zlib_error(self->zst, err, "while flushing");
Martin Panter84544c12016-07-23 03:02:07 +0000934 Py_CLEAR(RetVal);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000935 goto error;
Jeremy Hylton9714f992001-10-16 21:19:45 +0000936 }
Tim Peters977e5402001-10-17 03:57:20 +0000937
Martin Panter84544c12016-07-23 03:02:07 +0000938 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
939 (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
Victor Stinner79799262013-07-09 00:35:22 +0200940 Py_CLEAR(RetVal);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +0000941
Tim Peters977e5402001-10-17 03:57:20 +0000942 error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +0000943 LEAVE_ZLIB(self);
Jeremy Hylton9d620d02001-10-16 23:02:32 +0000944 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +0000945}
946
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000947#ifdef HAVE_ZLIB_COPY
Larry Hastings31826802013-10-19 00:09:25 -0700948
Larry Hastings61272b72014-01-07 12:41:53 -0800949/*[clinic input]
Larry Hastingsed4a1c52013-11-18 09:32:13 -0800950zlib.Compress.copy
Larry Hastings31826802013-10-19 00:09:25 -0700951
952Return a copy of the compression object.
Larry Hastings61272b72014-01-07 12:41:53 -0800953[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -0700954
Larry Hastings3cceb382014-01-04 11:09:09 -0800955static PyObject *
956zlib_Compress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800957/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958{
959 compobject *retval = NULL;
960 int err;
961
Dino Viehlanda1ffad02019-09-10 11:27:03 +0100962 retval = newcompobject(_zlibstate_global->Comptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963 if (!retval) return NULL;
964
965 /* Copy the zstream state
966 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
967 */
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800968 ENTER_ZLIB(self);
969 err = deflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +0000970 switch (err) {
971 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972 break;
Martin Panter84544c12016-07-23 03:02:07 +0000973 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000974 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
975 goto error;
Martin Panter84544c12016-07-23 03:02:07 +0000976 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000977 PyErr_SetString(PyExc_MemoryError,
978 "Can't allocate memory for compression object");
979 goto error;
980 default:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800981 zlib_error(self->zst, err, "while copying compression object");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000982 goto error;
983 }
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800984 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300985 Py_XSETREF(retval->unused_data, self->unused_data);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800986 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300987 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800988 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300989 Py_XSETREF(retval->zdict, self->zdict);
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800990 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000991
992 /* Mark it as being initialized */
993 retval->is_initialised = 1;
994
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800995 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000996 return (PyObject *)retval;
997
998error:
Larry Hastingsdc6aaec2013-11-24 04:41:57 -0800999 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000 Py_XDECREF(retval);
1001 return NULL;
1002}
1003
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001004/*[clinic input]
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001005zlib.Compress.__copy__
1006[clinic start generated code]*/
1007
1008static PyObject *
1009zlib_Compress___copy___impl(compobject *self)
1010/*[clinic end generated code: output=1875e6791975442e input=be97a05a788dfd83]*/
1011{
1012 return zlib_Compress_copy_impl(self);
1013}
1014
1015/*[clinic input]
1016zlib.Compress.__deepcopy__
1017
1018 memo: object
1019 /
1020
1021[clinic start generated code]*/
1022
1023static PyObject *
1024zlib_Compress___deepcopy__(compobject *self, PyObject *memo)
1025/*[clinic end generated code: output=f47a2213282c9eb0 input=a9a8b0b40d83388e]*/
1026{
1027 return zlib_Compress_copy_impl(self);
1028}
1029
1030/*[clinic input]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001031zlib.Decompress.copy
1032
1033Return a copy of the decompression object.
1034[clinic start generated code]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035
1036static PyObject *
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001037zlib_Decompress_copy_impl(compobject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08001038/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039{
1040 compobject *retval = NULL;
1041 int err;
1042
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001043 retval = newcompobject(_zlibstate_global->Decomptype);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044 if (!retval) return NULL;
1045
1046 /* Copy the zstream state
1047 * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1048 */
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001049 ENTER_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050 err = inflateCopy(&retval->zst, &self->zst);
Martin Panter84544c12016-07-23 03:02:07 +00001051 switch (err) {
1052 case Z_OK:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053 break;
Martin Panter84544c12016-07-23 03:02:07 +00001054 case Z_STREAM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055 PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1056 goto error;
Martin Panter84544c12016-07-23 03:02:07 +00001057 case Z_MEM_ERROR:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001058 PyErr_SetString(PyExc_MemoryError,
1059 "Can't allocate memory for decompression object");
1060 goto error;
1061 default:
1062 zlib_error(self->zst, err, "while copying decompression object");
1063 goto error;
1064 }
1065
1066 Py_INCREF(self->unused_data);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001067 Py_XSETREF(retval->unused_data, self->unused_data);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068 Py_INCREF(self->unconsumed_tail);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001069 Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001070 Py_XINCREF(self->zdict);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001071 Py_XSETREF(retval->zdict, self->zdict);
Nadeem Vawda1c385462011-08-13 15:22:40 +02001072 retval->eof = self->eof;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001073
1074 /* Mark it as being initialized */
1075 retval->is_initialised = 1;
1076
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001077 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001078 return (PyObject *)retval;
1079
1080error:
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001081 LEAVE_ZLIB(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001082 Py_XDECREF(retval);
1083 return NULL;
1084}
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001085
1086/*[clinic input]
1087zlib.Decompress.__copy__
1088[clinic start generated code]*/
1089
1090static PyObject *
1091zlib_Decompress___copy___impl(compobject *self)
1092/*[clinic end generated code: output=80bae8bc43498ad4 input=efcb98b5472c13d2]*/
1093{
1094 return zlib_Decompress_copy_impl(self);
1095}
1096
1097/*[clinic input]
1098zlib.Decompress.__deepcopy__
1099
1100 memo: object
1101 /
1102
1103[clinic start generated code]*/
1104
1105static PyObject *
1106zlib_Decompress___deepcopy__(compobject *self, PyObject *memo)
1107/*[clinic end generated code: output=1f77286ab490124b input=6e99bd0ac4b9cd8b]*/
1108{
1109 return zlib_Decompress_copy_impl(self);
1110}
1111
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001112#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00001113
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001114/*[clinic input]
1115zlib.Decompress.flush
1116
Martin Panter84544c12016-07-23 03:02:07 +00001117 length: ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001118 the initial size of the output buffer.
1119 /
1120
1121Return a bytes object containing any remaining decompressed data.
1122[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001123
Guido van Rossumfb221561997-04-29 15:38:09 +00001124static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +00001125zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1126/*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001127{
Martin Panter84544c12016-07-23 03:02:07 +00001128 int err, flush;
1129 Py_buffer data;
1130 PyObject *RetVal = NULL;
1131 Py_ssize_t ibuflen;
Tim Peters977e5402001-10-17 03:57:20 +00001132
Martin Panter84544c12016-07-23 03:02:07 +00001133 if (length <= 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001134 PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1135 return NULL;
Christian Heimes5e696852008-04-09 08:37:03 +00001136 }
Victor Stinnere079edd2013-11-21 22:33:21 +01001137
Martin Panter84544c12016-07-23 03:02:07 +00001138 if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001139 return NULL;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001140
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001141 ENTER_ZLIB(self);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001142
Martin Panter84544c12016-07-23 03:02:07 +00001143 self->zst.next_in = data.buf;
1144 ibuflen = data.len;
Victor Stinnere079edd2013-11-21 22:33:21 +01001145
Martin Panter84544c12016-07-23 03:02:07 +00001146 do {
1147 arrange_input_buffer(&self->zst, &ibuflen);
1148 flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001149
Martin Panter84544c12016-07-23 03:02:07 +00001150 do {
1151 length = arrange_output_buffer(&self->zst, &RetVal, length);
1152 if (length < 0)
1153 goto abort;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001154
Martin Panter84544c12016-07-23 03:02:07 +00001155 Py_BEGIN_ALLOW_THREADS
1156 err = inflate(&self->zst, flush);
1157 Py_END_ALLOW_THREADS
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001158
Martin Panter84544c12016-07-23 03:02:07 +00001159 switch (err) {
1160 case Z_OK: /* fall through */
1161 case Z_BUF_ERROR: /* fall through */
1162 case Z_STREAM_END:
1163 break;
1164 default:
1165 if (err == Z_NEED_DICT && self->zdict != NULL) {
1166 if (set_inflate_zdict(self) < 0)
1167 goto abort;
1168 else
1169 break;
1170 }
1171 goto save;
1172 }
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001173
Martin Panter84544c12016-07-23 03:02:07 +00001174 } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1175
1176 } while (err != Z_STREAM_END && ibuflen != 0);
1177
1178 save:
1179 if (save_unconsumed_input(self, &data, err) < 0)
1180 goto abort;
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001181
Nadeem Vawda3bf71c52011-08-13 15:42:50 +02001182 /* If at end of stream, clean up any memory allocated by zlib. */
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001183 if (err == Z_STREAM_END) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001184 self->eof = 1;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001185 self->is_initialised = 0;
Martin Panter84544c12016-07-23 03:02:07 +00001186 err = inflateEnd(&self->zst);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001187 if (err != Z_OK) {
Nadeem Vawda1c385462011-08-13 15:22:40 +02001188 zlib_error(self->zst, err, "while finishing decompression");
Martin Panter84544c12016-07-23 03:02:07 +00001189 goto abort;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001190 }
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001191 }
Nadeem Vawdaee7889d2012-11-11 02:14:36 +01001192
Martin Panter84544c12016-07-23 03:02:07 +00001193 if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1194 (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1195 goto success;
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001196
Martin Panter84544c12016-07-23 03:02:07 +00001197 abort:
1198 Py_CLEAR(RetVal);
1199 success:
1200 PyBuffer_Release(&data);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001201 LEAVE_ZLIB(self);
Martin Panter84544c12016-07-23 03:02:07 +00001202 return RetVal;
Guido van Rossumfb221561997-04-29 15:38:09 +00001203}
1204
Christian Heimes936e2f32014-01-27 01:06:57 +01001205#include "clinic/zlibmodule.c.h"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001206
Guido van Rossumfb221561997-04-29 15:38:09 +00001207static PyMethodDef comp_methods[] =
1208{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001209 ZLIB_COMPRESS_COMPRESS_METHODDEF
1210 ZLIB_COMPRESS_FLUSH_METHODDEF
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001211 ZLIB_COMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001212 ZLIB_COMPRESS___COPY___METHODDEF
1213 ZLIB_COMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001214 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001215};
1216
1217static PyMethodDef Decomp_methods[] =
1218{
Larry Hastingsed4a1c52013-11-18 09:32:13 -08001219 ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001220 ZLIB_DECOMPRESS_FLUSH_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001221 ZLIB_DECOMPRESS_COPY_METHODDEF
Zackery Spytzd2cbfff2018-06-27 12:04:51 -06001222 ZLIB_DECOMPRESS___COPY___METHODDEF
1223 ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001224 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001225};
1226
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001227#define COMP_OFF(x) offsetof(compobject, x)
1228static PyMemberDef Decomp_members[] = {
1229 {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1230 {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
Nadeem Vawda1c385462011-08-13 15:22:40 +02001231 {"eof", T_BOOL, COMP_OFF(eof), READONLY},
Amaury Forgeot d'Arce43d33a2008-07-02 20:50:16 +00001232 {NULL},
1233};
Guido van Rossumfb221561997-04-29 15:38:09 +00001234
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001235/*[clinic input]
1236zlib.adler32
1237
1238 data: Py_buffer
1239 value: unsigned_int(bitwise=True) = 1
1240 Starting value of the checksum.
1241 /
1242
1243Compute an Adler-32 checksum of data.
1244
1245The returned checksum is an integer.
1246[clinic start generated code]*/
Guido van Rossum3c540301997-06-03 22:21:03 +00001247
Guido van Rossumfb221561997-04-29 15:38:09 +00001248static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001249zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1250/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001251{
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001252 /* Releasing the GIL for very small buffers is inefficient
1253 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001254 if (data->len > 1024*5) {
1255 unsigned char *buf = data->buf;
1256 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001257
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001258 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001259 /* Avoid truncation of length for very large buffers. adler32() takes
1260 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001261 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001262 value = adler32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001263 buf += (size_t) UINT_MAX;
1264 len -= (size_t) UINT_MAX;
1265 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001266 value = adler32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001267 Py_END_ALLOW_THREADS
1268 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001269 value = adler32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001270 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001271 return PyLong_FromUnsignedLong(value & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001272}
Tim Peters977e5402001-10-17 03:57:20 +00001273
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001274/*[clinic input]
1275zlib.crc32
1276
1277 data: Py_buffer
1278 value: unsigned_int(bitwise=True) = 0
1279 Starting value of the checksum.
1280 /
1281
1282Compute a CRC-32 checksum of data.
1283
1284The returned checksum is an integer.
1285[clinic start generated code]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001286
1287static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001288zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1289/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
Guido van Rossumfb221561997-04-29 15:38:09 +00001290{
Martin v. Löwis423be952008-08-13 15:53:07 +00001291 int signed_val;
Christian Heimescc47b052008-03-25 14:56:36 +00001292
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001293 /* Releasing the GIL for very small buffers is inefficient
1294 and may lower performance */
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001295 if (data->len > 1024*5) {
1296 unsigned char *buf = data->buf;
1297 Py_ssize_t len = data->len;
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001298
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001299 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001300 /* Avoid truncation of length for very large buffers. crc32() takes
1301 length as an unsigned int, which may be narrower than Py_ssize_t. */
Victor Stinnere079edd2013-11-21 22:33:21 +01001302 while ((size_t)len > UINT_MAX) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001303 value = crc32(value, buf, UINT_MAX);
Antoine Pitrou9e719b62011-02-28 23:48:16 +00001304 buf += (size_t) UINT_MAX;
1305 len -= (size_t) UINT_MAX;
1306 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001307 signed_val = crc32(value, buf, (unsigned int)len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001308 Py_END_ALLOW_THREADS
1309 } else {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001310 signed_val = crc32(value, data->buf, (unsigned int)data->len);
Antoine Pitrou31f30b12009-01-02 17:34:35 +00001311 }
Christian Heimescc47b052008-03-25 14:56:36 +00001312 return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
Guido van Rossumfb221561997-04-29 15:38:09 +00001313}
Tim Peters977e5402001-10-17 03:57:20 +00001314
Guido van Rossumfb221561997-04-29 15:38:09 +00001315
1316static PyMethodDef zlib_methods[] =
1317{
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001318 ZLIB_ADLER32_METHODDEF
Larry Hastingsebdcb502013-11-23 14:54:00 -08001319 ZLIB_COMPRESS_METHODDEF
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001320 ZLIB_COMPRESSOBJ_METHODDEF
1321 ZLIB_CRC32_METHODDEF
1322 ZLIB_DECOMPRESS_METHODDEF
1323 ZLIB_DECOMPRESSOBJ_METHODDEF
Jeremy Hylton9714f992001-10-16 21:19:45 +00001324 {NULL, NULL}
Guido van Rossumfb221561997-04-29 15:38:09 +00001325};
1326
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001327static PyType_Slot Comptype_slots[] = {
1328 {Py_tp_dealloc, Comp_dealloc},
1329 {Py_tp_methods, comp_methods},
1330 {0, 0},
1331};
1332
1333static PyType_Spec Comptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001334 "zlib.Compress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001335 sizeof(compobject),
1336 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001337 Py_TPFLAGS_DEFAULT,
1338 Comptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001339};
1340
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001341static PyType_Slot Decomptype_slots[] = {
1342 {Py_tp_dealloc, Decomp_dealloc},
1343 {Py_tp_methods, Decomp_methods},
1344 {Py_tp_members, Decomp_members},
1345 {0, 0},
1346};
1347
1348static PyType_Spec Decomptype_spec = {
Guido van Rossum14648392001-12-08 18:02:58 +00001349 "zlib.Decompress",
Jeremy Hylton9714f992001-10-16 21:19:45 +00001350 sizeof(compobject),
1351 0,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001352 Py_TPFLAGS_DEFAULT,
1353 Decomptype_slots
Guido van Rossumfb221561997-04-29 15:38:09 +00001354};
1355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001356PyDoc_STRVAR(zlib_module_documentation,
Tim Petersadbd35b2001-10-17 04:16:15 +00001357"The functions in this module allow compression and decompression using the\n"
1358"zlib library, which is based on GNU zip.\n"
1359"\n"
1360"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
Martin Panter1fe0d132016-02-10 10:06:36 +00001361"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001362"compressobj([level[, ...]]) -- Return a compressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001363"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
Andrew M. Kuchling313a3e31999-12-20 22:13:38 +00001364"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
Nadeem Vawdafd8a8382012-06-21 02:13:12 +02001365"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001366"\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +00001367"'wbits' is window buffer size and container format.\n"
Tim Petersadbd35b2001-10-17 04:16:15 +00001368"Compressor objects support compress() and flush() methods; decompressor\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369"objects support decompress() and flush().");
Guido van Rossum3c540301997-06-03 22:21:03 +00001370
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001371static int
1372zlib_clear(PyObject *m)
1373{
Hai Shif707d942020-03-16 21:15:01 +08001374 _zlibstate *state = get_zlib_state(m);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001375 Py_CLEAR(state->Comptype);
1376 Py_CLEAR(state->Decomptype);
1377 Py_CLEAR(state->ZlibError);
1378 return 0;
1379}
1380
1381static int
1382zlib_traverse(PyObject *m, visitproc visit, void *arg)
1383{
Hai Shif707d942020-03-16 21:15:01 +08001384 _zlibstate *state = get_zlib_state(m);
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001385 Py_VISIT(state->Comptype);
1386 Py_VISIT(state->Decomptype);
1387 Py_VISIT(state->ZlibError);
1388 return 0;
1389}
1390
1391static void
1392zlib_free(void *m)
1393{
1394 zlib_clear((PyObject *)m);
1395}
1396
Martin v. Löwis1a214512008-06-11 05:26:20 +00001397static struct PyModuleDef zlibmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001398 PyModuleDef_HEAD_INIT,
1399 "zlib",
1400 zlib_module_documentation,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001401 sizeof(_zlibstate),
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001402 zlib_methods,
1403 NULL,
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001404 zlib_traverse,
1405 zlib_clear,
1406 zlib_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001407};
1408
Mark Hammond62b1ab12002-07-23 06:31:15 +00001409PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001410PyInit_zlib(void)
Guido van Rossumfb221561997-04-29 15:38:09 +00001411{
Fred Drake4baedc12002-04-01 14:53:37 +00001412 PyObject *m, *ver;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001413 m = PyState_FindModule(&zlibmodule);
1414 if (m != NULL) {
1415 Py_INCREF(m);
1416 return m;
1417 }
Martin v. Löwis1a214512008-06-11 05:26:20 +00001418 m = PyModule_Create(&zlibmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00001419 if (m == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001420 return NULL;
Jeremy Hyltoncb914041997-09-04 23:39:23 +00001421
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001422 PyTypeObject *Comptype = (PyTypeObject *)PyType_FromSpec(&Comptype_spec);
1423 if (Comptype == NULL)
1424 return NULL;
Hai Shif707d942020-03-16 21:15:01 +08001425 get_zlib_state(m)->Comptype = Comptype;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001426
1427 PyTypeObject *Decomptype = (PyTypeObject *)PyType_FromSpec(&Decomptype_spec);
1428 if (Decomptype == NULL)
1429 return NULL;
Hai Shif707d942020-03-16 21:15:01 +08001430 get_zlib_state(m)->Decomptype = Decomptype;
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001431
1432 PyObject *ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
Fred Drake4baedc12002-04-01 14:53:37 +00001433 if (ZlibError != NULL) {
1434 Py_INCREF(ZlibError);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001435 PyModule_AddObject(m, "error", ZlibError);
Hai Shif707d942020-03-16 21:15:01 +08001436 get_zlib_state(m)->ZlibError = ZlibError;
Fred Drake4baedc12002-04-01 14:53:37 +00001437 }
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001438 PyModule_AddIntMacro(m, MAX_WBITS);
1439 PyModule_AddIntMacro(m, DEFLATED);
1440 PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001441 PyModule_AddIntMacro(m, DEF_BUF_SIZE);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001442 // compression levels
1443 PyModule_AddIntMacro(m, Z_NO_COMPRESSION);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001444 PyModule_AddIntMacro(m, Z_BEST_SPEED);
1445 PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1446 PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001447 // compression strategies
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001448 PyModule_AddIntMacro(m, Z_FILTERED);
1449 PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001450#ifdef Z_RLE // 1.2.0.1
1451 PyModule_AddIntMacro(m, Z_RLE);
1452#endif
1453#ifdef Z_FIXED // 1.2.2.2
1454 PyModule_AddIntMacro(m, Z_FIXED);
1455#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001456 PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001457 // allowed flush values
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001458 PyModule_AddIntMacro(m, Z_NO_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001459 PyModule_AddIntMacro(m, Z_PARTIAL_FLUSH);
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02001460 PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1461 PyModule_AddIntMacro(m, Z_FULL_FLUSH);
Xiang Zhangbc3f2282018-03-07 13:05:37 +08001462 PyModule_AddIntMacro(m, Z_FINISH);
1463#ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate
1464 PyModule_AddIntMacro(m, Z_BLOCK);
1465#endif
1466#ifdef Z_TREES // 1.2.3.4, only for inflate
1467 PyModule_AddIntMacro(m, Z_TREES);
1468#endif
Neal Norwitz53cbdaa2007-08-23 21:42:55 +00001469 ver = PyUnicode_FromString(ZLIB_VERSION);
Fred Drake4baedc12002-04-01 14:53:37 +00001470 if (ver != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001471 PyModule_AddObject(m, "ZLIB_VERSION", ver);
Martin v. Löwis3bd8c1e2001-09-07 16:27:31 +00001472
Nadeem Vawda64d25dd2011-09-12 00:04:13 +02001473 ver = PyUnicode_FromString(zlibVersion());
1474 if (ver != NULL)
1475 PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1476
Guido van Rossum7d9ea502003-02-03 20:45:52 +00001477 PyModule_AddStringConstant(m, "__version__", "1.0");
1478
Dino Viehlanda1ffad02019-09-10 11:27:03 +01001479 PyState_AddModule(m, &zlibmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001480 return m;
Guido van Rossumfb221561997-04-29 15:38:09 +00001481}