Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1 | /* zlibmodule.c -- gzip-compatible data compression */ |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 2 | /* See http://zlib.net/ */ |
Mark Hammond | ae8c268 | 2001-01-31 10:28:03 +0000 | [diff] [blame] | 3 | |
Tim Peters | ee826f8 | 2001-01-31 19:39:44 +0000 | [diff] [blame] | 4 | /* Windows users: read Python's PCbuild\readme.txt */ |
Mark Hammond | ae8c268 | 2001-01-31 10:28:03 +0000 | [diff] [blame] | 5 | |
Victor Stinner | f18f871 | 2014-07-01 16:48:12 +0200 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 9 | #include "structmember.h" // PyMemberDef |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 10 | #include "zlib.h" |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 11 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 12 | |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 13 | #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öwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 18 | |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 19 | #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221 |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 20 | # define AT_LEAST_ZLIB_1_2_2_1 |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 23 | /* 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 Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 30 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 31 | /* Initial buffer size. */ |
| 32 | #define DEF_BUF_SIZE (16*1024) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 33 | |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 34 | static PyModuleDef zlibmodule; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 35 | |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 36 | typedef struct { |
| 37 | PyTypeObject *Comptype; |
| 38 | PyTypeObject *Decomptype; |
| 39 | PyObject *ZlibError; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 40 | } zlibstate; |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 41 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 42 | static inline zlibstate* |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 43 | get_zlib_state(PyObject *module) |
| 44 | { |
| 45 | void *state = PyModule_GetState(module); |
| 46 | assert(state != NULL); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 47 | return (zlibstate *)state; |
Hai Shi | f707d94 | 2020-03-16 21:15:01 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 50 | typedef struct |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 51 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 52 | PyObject_HEAD |
| 53 | z_stream zst; |
| 54 | PyObject *unused_data; |
| 55 | PyObject *unconsumed_tail; |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 56 | char eof; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 57 | int is_initialised; |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 58 | PyObject *zdict; |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 59 | PyThread_type_lock lock; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 60 | } compobject; |
| 61 | |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 62 | static void |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 63 | zlib_error(zlibstate *state, z_stream zst, int err, const char *msg) |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 64 | { |
Nadeem Vawda | 524148a | 2011-08-28 11:26:46 +0200 | [diff] [blame] | 65 | const char *zmsg = Z_NULL; |
| 66 | /* In case of a version mismatch, zst.msg won't be initialized. |
| 67 | Check for this case first, before looking at zst.msg. */ |
| 68 | if (err == Z_VERSION_ERROR) |
| 69 | zmsg = "library version mismatch"; |
| 70 | if (zmsg == Z_NULL) |
| 71 | zmsg = zst.msg; |
Antoine Pitrou | 96f212b | 2010-05-11 23:49:58 +0000 | [diff] [blame] | 72 | if (zmsg == Z_NULL) { |
| 73 | switch (err) { |
| 74 | case Z_BUF_ERROR: |
| 75 | zmsg = "incomplete or truncated stream"; |
| 76 | break; |
| 77 | case Z_STREAM_ERROR: |
| 78 | zmsg = "inconsistent stream state"; |
| 79 | break; |
| 80 | case Z_DATA_ERROR: |
| 81 | zmsg = "invalid input data"; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | if (zmsg == Z_NULL) |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 86 | PyErr_Format(state->ZlibError, "Error %d %s", err, msg); |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 87 | else |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 88 | PyErr_Format(state->ZlibError, "Error %d %s: %.200s", err, msg, zmsg); |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 91 | /*[clinic input] |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 92 | module zlib |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 93 | class zlib.Compress "compobject *" "&Comptype" |
| 94 | class zlib.Decompress "compobject *" "&Decomptype" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 95 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 96 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 97 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 98 | static compobject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 99 | newcompobject(PyTypeObject *type) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 100 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 101 | compobject *self; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 102 | self = PyObject_New(compobject, type); |
| 103 | if (self == NULL) |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 104 | return NULL; |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 105 | self->eof = 0; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 106 | self->is_initialised = 0; |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 107 | self->zdict = NULL; |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 108 | self->unused_data = PyBytes_FromStringAndSize("", 0); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 109 | if (self->unused_data == NULL) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 110 | Py_DECREF(self); |
| 111 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 112 | } |
Gregory P. Smith | 693fc46 | 2008-09-06 20:13:06 +0000 | [diff] [blame] | 113 | self->unconsumed_tail = PyBytes_FromStringAndSize("", 0); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 114 | if (self->unconsumed_tail == NULL) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 115 | Py_DECREF(self); |
| 116 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 117 | } |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 118 | self->lock = PyThread_allocate_lock(); |
Victor Stinner | bf2e2f9 | 2013-07-09 00:29:03 +0200 | [diff] [blame] | 119 | if (self->lock == NULL) { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 120 | Py_DECREF(self); |
Victor Stinner | bf2e2f9 | 2013-07-09 00:29:03 +0200 | [diff] [blame] | 121 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); |
| 122 | return NULL; |
| 123 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 124 | return self; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 127 | static void* |
| 128 | PyZlib_Malloc(voidpf ctx, uInt items, uInt size) |
| 129 | { |
Alexey Izbyshev | 3d4fabb | 2018-10-28 19:45:50 +0300 | [diff] [blame] | 130 | if (size != 0 && items > (size_t)PY_SSIZE_T_MAX / size) |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 131 | return NULL; |
| 132 | /* PyMem_Malloc() cannot be used: the GIL is not held when |
| 133 | inflate() and deflate() are called */ |
Alexey Izbyshev | 3d4fabb | 2018-10-28 19:45:50 +0300 | [diff] [blame] | 134 | return PyMem_RawMalloc((size_t)items * (size_t)size); |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void |
| 138 | PyZlib_Free(voidpf ctx, void *ptr) |
| 139 | { |
Victor Stinner | b7f1f65 | 2013-07-07 17:10:34 +0200 | [diff] [blame] | 140 | PyMem_RawFree(ptr); |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 143 | static void |
| 144 | arrange_input_buffer(z_stream *zst, Py_ssize_t *remains) |
| 145 | { |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 146 | zst->avail_in = (uInt)Py_MIN((size_t)*remains, UINT_MAX); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 147 | *remains -= zst->avail_in; |
| 148 | } |
| 149 | |
| 150 | static Py_ssize_t |
| 151 | arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer, |
| 152 | Py_ssize_t length, |
| 153 | Py_ssize_t max_length) |
| 154 | { |
| 155 | Py_ssize_t occupied; |
| 156 | |
| 157 | if (*buffer == NULL) { |
| 158 | if (!(*buffer = PyBytes_FromStringAndSize(NULL, length))) |
| 159 | return -1; |
| 160 | occupied = 0; |
| 161 | } |
| 162 | else { |
| 163 | occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer); |
| 164 | |
| 165 | if (length == occupied) { |
| 166 | Py_ssize_t new_length; |
| 167 | assert(length <= max_length); |
| 168 | /* can not scale the buffer over max_length */ |
| 169 | if (length == max_length) |
| 170 | return -2; |
| 171 | if (length <= (max_length >> 1)) |
| 172 | new_length = length << 1; |
| 173 | else |
| 174 | new_length = max_length; |
| 175 | if (_PyBytes_Resize(buffer, new_length) < 0) |
| 176 | return -1; |
| 177 | length = new_length; |
| 178 | } |
| 179 | } |
| 180 | |
Segev Finer | 679b566 | 2017-07-27 01:17:57 +0300 | [diff] [blame] | 181 | zst->avail_out = (uInt)Py_MIN((size_t)(length - occupied), UINT_MAX); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 182 | zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied; |
| 183 | |
| 184 | return length; |
| 185 | } |
| 186 | |
| 187 | static Py_ssize_t |
| 188 | arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length) |
| 189 | { |
| 190 | Py_ssize_t ret; |
| 191 | |
| 192 | ret = arrange_output_buffer_with_maximum(zst, buffer, length, |
| 193 | PY_SSIZE_T_MAX); |
| 194 | if (ret == -2) |
| 195 | PyErr_NoMemory(); |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 200 | /*[clinic input] |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 201 | zlib.compress |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 202 | |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 203 | data: Py_buffer |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 204 | Binary data to be compressed. |
Serhiy Storchaka | 95657cd | 2016-06-25 22:43:05 +0300 | [diff] [blame] | 205 | / |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 206 | level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 207 | Compression level, in 0-9 or -1. |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 208 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 209 | Returns a bytes object containing compressed data. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 210 | [clinic start generated code]*/ |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 211 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 212 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 213 | zlib_compress_impl(PyObject *module, Py_buffer *data, int level) |
| 214 | /*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 215 | { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 216 | PyObject *RetVal = NULL; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 217 | Py_ssize_t obuflen = DEF_BUF_SIZE; |
| 218 | int flush; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 219 | z_stream zst; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 220 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 221 | zlibstate *state = get_zlib_state(module); |
| 222 | |
| 223 | Byte *ibuf = data->buf; |
| 224 | Py_ssize_t ibuflen = data->len; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 225 | |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 226 | zst.opaque = NULL; |
| 227 | zst.zalloc = PyZlib_Malloc; |
| 228 | zst.zfree = PyZlib_Free; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 229 | zst.next_in = ibuf; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 230 | int err = deflateInit(&zst, level); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 231 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 232 | switch (err) { |
| 233 | case Z_OK: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 234 | break; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 235 | case Z_MEM_ERROR: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 236 | PyErr_SetString(PyExc_MemoryError, |
| 237 | "Out of memory while compressing data"); |
| 238 | goto error; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 239 | case Z_STREAM_ERROR: |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 240 | PyErr_SetString(state->ZlibError, "Bad compression level"); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 241 | goto error; |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 242 | default: |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 243 | deflateEnd(&zst); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 244 | zlib_error(state, zst, err, "while compressing data"); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 245 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 246 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 247 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 248 | do { |
| 249 | arrange_input_buffer(&zst, &ibuflen); |
| 250 | flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 251 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 252 | do { |
| 253 | obuflen = arrange_output_buffer(&zst, &RetVal, obuflen); |
| 254 | if (obuflen < 0) { |
| 255 | deflateEnd(&zst); |
| 256 | goto error; |
| 257 | } |
| 258 | |
| 259 | Py_BEGIN_ALLOW_THREADS |
| 260 | err = deflate(&zst, flush); |
| 261 | Py_END_ALLOW_THREADS |
| 262 | |
| 263 | if (err == Z_STREAM_ERROR) { |
| 264 | deflateEnd(&zst); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 265 | zlib_error(state, zst, err, "while compressing data"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 266 | goto error; |
| 267 | } |
| 268 | |
| 269 | } while (zst.avail_out == 0); |
| 270 | assert(zst.avail_in == 0); |
| 271 | |
| 272 | } while (flush != Z_FINISH); |
| 273 | assert(err == Z_STREAM_END); |
| 274 | |
| 275 | err = deflateEnd(&zst); |
| 276 | if (err == Z_OK) { |
| 277 | if (_PyBytes_Resize(&RetVal, zst.next_out - |
| 278 | (Byte *)PyBytes_AS_STRING(RetVal)) < 0) |
| 279 | goto error; |
| 280 | return RetVal; |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 281 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 282 | else |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 283 | zlib_error(state, zst, err, "while finishing compression"); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 284 | error: |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 285 | Py_XDECREF(RetVal); |
| 286 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 289 | /*[clinic input] |
| 290 | zlib.decompress |
| 291 | |
| 292 | data: Py_buffer |
| 293 | Compressed data. |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 294 | / |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 295 | wbits: int(c_default="MAX_WBITS") = MAX_WBITS |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 296 | The window buffer size and container format. |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 297 | bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 298 | The initial output buffer size. |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 299 | |
| 300 | Returns a bytes object containing the uncompressed data. |
| 301 | [clinic start generated code]*/ |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 303 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 304 | zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 305 | Py_ssize_t bufsize) |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 306 | /*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 307 | { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 308 | PyObject *RetVal = NULL; |
| 309 | Byte *ibuf; |
| 310 | Py_ssize_t ibuflen; |
| 311 | int err, flush; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 312 | z_stream zst; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 313 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 314 | zlibstate *state = get_zlib_state(module); |
| 315 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 316 | if (bufsize < 0) { |
| 317 | PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative"); |
| 318 | return NULL; |
| 319 | } else if (bufsize == 0) { |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 320 | bufsize = 1; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 321 | } |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 322 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 323 | ibuf = data->buf; |
| 324 | ibuflen = data->len; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 325 | |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 326 | zst.opaque = NULL; |
| 327 | zst.zalloc = PyZlib_Malloc; |
| 328 | zst.zfree = PyZlib_Free; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 329 | zst.avail_in = 0; |
| 330 | zst.next_in = ibuf; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 331 | err = inflateInit2(&zst, wbits); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 332 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 333 | switch (err) { |
| 334 | case Z_OK: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 335 | break; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 336 | case Z_MEM_ERROR: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 337 | PyErr_SetString(PyExc_MemoryError, |
| 338 | "Out of memory while decompressing data"); |
| 339 | goto error; |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 340 | default: |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 341 | inflateEnd(&zst); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 342 | zlib_error(state, zst, err, "while preparing to decompress data"); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 343 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 344 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 345 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 346 | do { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 347 | arrange_input_buffer(&zst, &ibuflen); |
| 348 | flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 349 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 350 | do { |
| 351 | bufsize = arrange_output_buffer(&zst, &RetVal, bufsize); |
| 352 | if (bufsize < 0) { |
| 353 | inflateEnd(&zst); |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | Py_BEGIN_ALLOW_THREADS |
| 358 | err = inflate(&zst, flush); |
| 359 | Py_END_ALLOW_THREADS |
| 360 | |
| 361 | switch (err) { |
| 362 | case Z_OK: /* fall through */ |
| 363 | case Z_BUF_ERROR: /* fall through */ |
| 364 | case Z_STREAM_END: |
| 365 | break; |
| 366 | case Z_MEM_ERROR: |
| 367 | inflateEnd(&zst); |
| 368 | PyErr_SetString(PyExc_MemoryError, |
| 369 | "Out of memory while decompressing data"); |
| 370 | goto error; |
| 371 | default: |
| 372 | inflateEnd(&zst); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 373 | zlib_error(state, zst, err, "while decompressing data"); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 374 | goto error; |
| 375 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 376 | |
| 377 | } while (zst.avail_out == 0); |
| 378 | |
| 379 | } while (err != Z_STREAM_END && ibuflen != 0); |
| 380 | |
| 381 | |
| 382 | if (err != Z_STREAM_END) { |
| 383 | inflateEnd(&zst); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 384 | zlib_error(state, zst, err, "while decompressing data"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 385 | goto error; |
| 386 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 387 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 388 | err = inflateEnd(&zst); |
| 389 | if (err != Z_OK) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 390 | zlib_error(state, zst, err, "while finishing decompression"); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 391 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 392 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 393 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 394 | if (_PyBytes_Resize(&RetVal, zst.next_out - |
| 395 | (Byte *)PyBytes_AS_STRING(RetVal)) < 0) |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 396 | goto error; |
| 397 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 398 | return RetVal; |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 399 | |
| 400 | error: |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 401 | Py_XDECREF(RetVal); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 402 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 405 | /*[clinic input] |
| 406 | zlib.compressobj |
| 407 | |
| 408 | level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION |
Martin Panter | 567d513 | 2016-02-03 07:06:33 +0000 | [diff] [blame] | 409 | The compression level (an integer in the range 0-9 or -1; default is |
| 410 | currently equivalent to 6). Higher compression levels are slower, |
| 411 | but produce smaller results. |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 412 | method: int(c_default="DEFLATED") = DEFLATED |
| 413 | The compression algorithm. If given, this must be DEFLATED. |
| 414 | wbits: int(c_default="MAX_WBITS") = MAX_WBITS |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 415 | +9 to +15: The base-two logarithm of the window size. Include a zlib |
| 416 | container. |
| 417 | -9 to -15: Generate a raw stream. |
| 418 | +25 to +31: Include a gzip container. |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 419 | memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL |
| 420 | Controls the amount of memory used for internal compression state. |
| 421 | Valid values range from 1 to 9. Higher values result in higher memory |
| 422 | usage, faster compression, and smaller output. |
| 423 | strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY |
| 424 | Used to tune the compression algorithm. Possible values are |
| 425 | Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY. |
| 426 | zdict: Py_buffer = None |
| 427 | The predefined compression dictionary - a sequence of bytes |
| 428 | containing subsequences that are likely to occur in the input data. |
| 429 | |
| 430 | Return a compressor object. |
| 431 | [clinic start generated code]*/ |
| 432 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 433 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 434 | zlib_compressobj_impl(PyObject *module, int level, int method, int wbits, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 435 | int memLevel, int strategy, Py_buffer *zdict) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 436 | /*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 437 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 438 | zlibstate *state = get_zlib_state(module); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 439 | if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) { |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 440 | PyErr_SetString(PyExc_OverflowError, |
| 441 | "zdict length does not fit in an unsigned int"); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 442 | return NULL; |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 445 | compobject *self = newcompobject(state->Comptype); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 446 | if (self == NULL) |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 447 | goto error; |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 448 | self->zst.opaque = NULL; |
| 449 | self->zst.zalloc = PyZlib_Malloc; |
| 450 | self->zst.zfree = PyZlib_Free; |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 451 | self->zst.next_in = NULL; |
| 452 | self->zst.avail_in = 0; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 453 | int err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 454 | switch (err) { |
| 455 | case Z_OK: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 456 | self->is_initialised = 1; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 457 | if (zdict->buf == NULL) { |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 458 | goto success; |
| 459 | } else { |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 460 | err = deflateSetDictionary(&self->zst, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 461 | zdict->buf, (unsigned int)zdict->len); |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 462 | switch (err) { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 463 | case Z_OK: |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 464 | goto success; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 465 | case Z_STREAM_ERROR: |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 466 | PyErr_SetString(PyExc_ValueError, "Invalid dictionary"); |
| 467 | goto error; |
| 468 | default: |
| 469 | PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()"); |
| 470 | goto error; |
| 471 | } |
| 472 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 473 | case Z_MEM_ERROR: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 474 | PyErr_SetString(PyExc_MemoryError, |
| 475 | "Can't allocate memory for compression object"); |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 476 | goto error; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 477 | case Z_STREAM_ERROR: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 478 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 479 | goto error; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 480 | default: |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 481 | zlib_error(state, self->zst, err, "while creating compression object"); |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 482 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 483 | } |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 484 | |
| 485 | error: |
Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 486 | Py_CLEAR(self); |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 487 | success: |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 488 | return (PyObject *)self; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 491 | static int |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 492 | set_inflate_zdict(zlibstate *state, compobject *self) |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 493 | { |
| 494 | Py_buffer zdict_buf; |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 495 | if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) { |
| 496 | return -1; |
| 497 | } |
| 498 | if ((size_t)zdict_buf.len > UINT_MAX) { |
| 499 | PyErr_SetString(PyExc_OverflowError, |
| 500 | "zdict length does not fit in an unsigned int"); |
| 501 | PyBuffer_Release(&zdict_buf); |
| 502 | return -1; |
| 503 | } |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 504 | int err; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 505 | err = inflateSetDictionary(&self->zst, |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 506 | zdict_buf.buf, (unsigned int)zdict_buf.len); |
| 507 | PyBuffer_Release(&zdict_buf); |
| 508 | if (err != Z_OK) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 509 | zlib_error(state, self->zst, err, "while setting zdict"); |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 510 | return -1; |
| 511 | } |
| 512 | return 0; |
| 513 | } |
| 514 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 515 | /*[clinic input] |
| 516 | zlib.decompressobj |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 517 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 518 | wbits: int(c_default="MAX_WBITS") = MAX_WBITS |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 519 | The window buffer size and container format. |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 520 | zdict: object(c_default="NULL") = b'' |
| 521 | The predefined compression dictionary. This must be the same |
| 522 | dictionary as used by the compressor that produced the input data. |
| 523 | |
| 524 | Return a decompressor object. |
| 525 | [clinic start generated code]*/ |
| 526 | |
| 527 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 528 | zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict) |
| 529 | /*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/ |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 530 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 531 | zlibstate *state = get_zlib_state(module); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 532 | |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 533 | if (zdict != NULL && !PyObject_CheckBuffer(zdict)) { |
| 534 | PyErr_SetString(PyExc_TypeError, |
| 535 | "zdict argument must support the buffer protocol"); |
| 536 | return NULL; |
| 537 | } |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 538 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 539 | compobject *self = newcompobject(state->Decomptype); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 540 | if (self == NULL) |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 541 | return NULL; |
Victor Stinner | 5064a52 | 2013-07-07 16:50:27 +0200 | [diff] [blame] | 542 | self->zst.opaque = NULL; |
| 543 | self->zst.zalloc = PyZlib_Malloc; |
| 544 | self->zst.zfree = PyZlib_Free; |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 545 | self->zst.next_in = NULL; |
| 546 | self->zst.avail_in = 0; |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 547 | if (zdict != NULL) { |
| 548 | Py_INCREF(zdict); |
| 549 | self->zdict = zdict; |
| 550 | } |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 551 | int err = inflateInit2(&self->zst, wbits); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 552 | switch (err) { |
| 553 | case Z_OK: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 554 | self->is_initialised = 1; |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 555 | if (self->zdict != NULL && wbits < 0) { |
| 556 | #ifdef AT_LEAST_ZLIB_1_2_2_1 |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 557 | if (set_inflate_zdict(state, self) < 0) { |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 558 | Py_DECREF(self); |
| 559 | return NULL; |
| 560 | } |
| 561 | #else |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 562 | PyErr_Format(state->ZlibError, |
Martin Panter | 3f0ee83 | 2016-06-05 10:48:34 +0000 | [diff] [blame] | 563 | "zlib version %s does not allow raw inflate with dictionary", |
| 564 | ZLIB_VERSION); |
| 565 | Py_DECREF(self); |
| 566 | return NULL; |
| 567 | #endif |
| 568 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 569 | return (PyObject *)self; |
| 570 | case Z_STREAM_ERROR: |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 571 | Py_DECREF(self); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 572 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
| 573 | return NULL; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 574 | case Z_MEM_ERROR: |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 575 | Py_DECREF(self); |
| 576 | PyErr_SetString(PyExc_MemoryError, |
| 577 | "Can't allocate memory for decompression object"); |
| 578 | return NULL; |
| 579 | default: |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 580 | zlib_error(state, self->zst, err, "while creating decompression object"); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 581 | Py_DECREF(self); |
| 582 | return NULL; |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 583 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static void |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 587 | Dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 588 | { |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 589 | PyObject *type = (PyObject *)Py_TYPE(self); |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 590 | PyThread_free_lock(self->lock); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 591 | Py_XDECREF(self->unused_data); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 592 | Py_XDECREF(self->unconsumed_tail); |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 593 | Py_XDECREF(self->zdict); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 594 | PyObject_Free(self); |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 595 | Py_DECREF(type); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static void |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 599 | Comp_dealloc(compobject *self) |
| 600 | { |
| 601 | if (self->is_initialised) |
| 602 | deflateEnd(&self->zst); |
| 603 | Dealloc(self); |
| 604 | } |
| 605 | |
| 606 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 607 | Decomp_dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 608 | { |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 609 | if (self->is_initialised) |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 610 | inflateEnd(&self->zst); |
| 611 | Dealloc(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 614 | /*[clinic input] |
| 615 | zlib.Compress.compress |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 616 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 617 | cls: defining_class |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 618 | data: Py_buffer |
| 619 | Binary data to be compressed. |
| 620 | / |
| 621 | |
| 622 | Returns a bytes object containing compressed data. |
| 623 | |
| 624 | After calling this function, some of the input data may still |
| 625 | be stored in internal buffers for later processing. |
| 626 | Call the flush() method to clear these buffers. |
| 627 | [clinic start generated code]*/ |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 629 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 630 | zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls, |
| 631 | Py_buffer *data) |
| 632 | /*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 633 | { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 634 | PyObject *RetVal = NULL; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 635 | Py_ssize_t obuflen = DEF_BUF_SIZE; |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 636 | int err; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 637 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 638 | zlibstate *state = PyType_GetModuleState(cls); |
| 639 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 640 | self->zst.next_in = data->buf; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 641 | Py_ssize_t ibuflen = data->len; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 643 | ENTER_ZLIB(self); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 644 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 645 | do { |
| 646 | arrange_input_buffer(&self->zst, &ibuflen); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 647 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 648 | do { |
| 649 | obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen); |
| 650 | if (obuflen < 0) |
| 651 | goto error; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 652 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 653 | Py_BEGIN_ALLOW_THREADS |
| 654 | err = deflate(&self->zst, Z_NO_FLUSH); |
| 655 | Py_END_ALLOW_THREADS |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 656 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 657 | if (err == Z_STREAM_ERROR) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 658 | zlib_error(state, self->zst, err, "while compressing data"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 659 | goto error; |
| 660 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 661 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 662 | } while (self->zst.avail_out == 0); |
| 663 | assert(self->zst.avail_in == 0); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 664 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 665 | } while (ibuflen != 0); |
| 666 | |
| 667 | if (_PyBytes_Resize(&RetVal, self->zst.next_out - |
| 668 | (Byte *)PyBytes_AS_STRING(RetVal)) == 0) |
| 669 | goto success; |
| 670 | |
| 671 | error: |
| 672 | Py_CLEAR(RetVal); |
| 673 | success: |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 674 | LEAVE_ZLIB(self); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 675 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 678 | /* Helper for objdecompress() and flush(). Saves any unconsumed input data in |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 679 | self->unused_data or self->unconsumed_tail, as appropriate. */ |
| 680 | static int |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 681 | save_unconsumed_input(compobject *self, Py_buffer *data, int err) |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 682 | { |
| 683 | if (err == Z_STREAM_END) { |
| 684 | /* The end of the compressed data has been reached. Store the leftover |
| 685 | input data in self->unused_data. */ |
| 686 | if (self->zst.avail_in > 0) { |
| 687 | Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 688 | Py_ssize_t new_size, left_size; |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 689 | PyObject *new_data; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 690 | left_size = (Byte *)data->buf + data->len - self->zst.next_in; |
| 691 | if (left_size > (PY_SSIZE_T_MAX - old_size)) { |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 692 | PyErr_NoMemory(); |
| 693 | return -1; |
| 694 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 695 | new_size = old_size + left_size; |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 696 | new_data = PyBytes_FromStringAndSize(NULL, new_size); |
| 697 | if (new_data == NULL) |
| 698 | return -1; |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 699 | memcpy(PyBytes_AS_STRING(new_data), |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 700 | PyBytes_AS_STRING(self->unused_data), old_size); |
Christian Heimes | f051e43 | 2016-09-13 20:22:02 +0200 | [diff] [blame] | 701 | memcpy(PyBytes_AS_STRING(new_data) + old_size, |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 702 | self->zst.next_in, left_size); |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 703 | Py_SETREF(self->unused_data, new_data); |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 704 | self->zst.avail_in = 0; |
| 705 | } |
| 706 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 707 | |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 708 | if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) { |
| 709 | /* This code handles two distinct cases: |
| 710 | 1. Output limit was reached. Save leftover input in unconsumed_tail. |
| 711 | 2. All input data was consumed. Clear unconsumed_tail. */ |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 712 | Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in; |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 713 | PyObject *new_data = PyBytes_FromStringAndSize( |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 714 | (char *)self->zst.next_in, left_size); |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 715 | if (new_data == NULL) |
| 716 | return -1; |
Serhiy Storchaka | 57a01d3 | 2016-04-10 18:05:40 +0300 | [diff] [blame] | 717 | Py_SETREF(self->unconsumed_tail, new_data); |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 718 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 719 | |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 723 | /*[clinic input] |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 724 | zlib.Decompress.decompress |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 725 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 726 | cls: defining_class |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 727 | data: Py_buffer |
| 728 | The binary data to decompress. |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 729 | / |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 730 | max_length: Py_ssize_t = 0 |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 731 | The maximum allowable length of the decompressed data. |
| 732 | Unconsumed input data will be stored in |
| 733 | the unconsumed_tail attribute. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 734 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 735 | Return a bytes object containing the decompressed version of the data. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 736 | |
| 737 | After calling this function, some of the input data may still be stored in |
| 738 | internal buffers for later processing. |
| 739 | Call the flush() method to clear these buffers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 740 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 741 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 742 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 743 | zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls, |
| 744 | Py_buffer *data, Py_ssize_t max_length) |
| 745 | /*[clinic end generated code: output=b024a93c2c922d57 input=bfb37b3864cfb606]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 746 | { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 747 | int err = Z_OK; |
| 748 | Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit; |
Nadeem Vawda | 0c3d96a | 2011-05-15 00:19:50 +0200 | [diff] [blame] | 749 | PyObject *RetVal = NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 750 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 751 | PyObject *module = PyType_GetModule(cls); |
| 752 | if (module == NULL) |
| 753 | return NULL; |
| 754 | |
| 755 | zlibstate *state = get_zlib_state(module); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 756 | if (max_length < 0) { |
| 757 | PyErr_SetString(PyExc_ValueError, "max_length must be non-negative"); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 758 | return NULL; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 759 | } else if (max_length == 0) |
| 760 | hard_limit = PY_SSIZE_T_MAX; |
| 761 | else |
| 762 | hard_limit = max_length; |
| 763 | |
| 764 | self->zst.next_in = data->buf; |
| 765 | ibuflen = data->len; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 766 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 767 | /* limit amount of data allocated to max_length */ |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 768 | if (max_length && obuflen > max_length) |
| 769 | obuflen = max_length; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 770 | |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 771 | ENTER_ZLIB(self); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 772 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 773 | do { |
| 774 | arrange_input_buffer(&self->zst, &ibuflen); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 775 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 776 | do { |
| 777 | obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal, |
| 778 | obuflen, hard_limit); |
| 779 | if (obuflen == -2) { |
| 780 | if (max_length > 0) { |
| 781 | goto save; |
| 782 | } |
| 783 | PyErr_NoMemory(); |
| 784 | } |
| 785 | if (obuflen < 0) { |
| 786 | goto abort; |
| 787 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 788 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 789 | Py_BEGIN_ALLOW_THREADS |
| 790 | err = inflate(&self->zst, Z_SYNC_FLUSH); |
| 791 | Py_END_ALLOW_THREADS |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 792 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 793 | switch (err) { |
| 794 | case Z_OK: /* fall through */ |
| 795 | case Z_BUF_ERROR: /* fall through */ |
| 796 | case Z_STREAM_END: |
| 797 | break; |
| 798 | default: |
| 799 | if (err == Z_NEED_DICT && self->zdict != NULL) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 800 | if (set_inflate_zdict(state, self) < 0) { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 801 | goto abort; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 802 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 803 | else |
| 804 | break; |
| 805 | } |
| 806 | goto save; |
| 807 | } |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 808 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 809 | } while (self->zst.avail_out == 0 || err == Z_NEED_DICT); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 810 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 811 | } while (err != Z_STREAM_END && ibuflen != 0); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 812 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 813 | save: |
| 814 | if (save_unconsumed_input(self, data, err) < 0) |
| 815 | goto abort; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 816 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 817 | if (err == Z_STREAM_END) { |
Nadeem Vawda | dd1253a | 2012-11-11 02:21:22 +0100 | [diff] [blame] | 818 | /* This is the logical place to call inflateEnd, but the old behaviour |
| 819 | of only calling it on flush() is preserved. */ |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 820 | self->eof = 1; |
Nadeem Vawda | dd1253a | 2012-11-11 02:21:22 +0100 | [diff] [blame] | 821 | } else if (err != Z_OK && err != Z_BUF_ERROR) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 822 | /* We will only get Z_BUF_ERROR if the output buffer was full |
| 823 | but there wasn't more output when we tried again, so it is |
| 824 | not an error condition. |
| 825 | */ |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 826 | zlib_error(state, self->zst, err, "while decompressing data"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 827 | goto abort; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 828 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 829 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 830 | if (_PyBytes_Resize(&RetVal, self->zst.next_out - |
| 831 | (Byte *)PyBytes_AS_STRING(RetVal)) == 0) |
| 832 | goto success; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 833 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 834 | abort: |
| 835 | Py_CLEAR(RetVal); |
| 836 | success: |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 837 | LEAVE_ZLIB(self); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 838 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 841 | /*[clinic input] |
| 842 | zlib.Compress.flush |
| 843 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 844 | cls: defining_class |
Serhiy Storchaka | 54c1391 | 2014-02-05 13:34:01 +0200 | [diff] [blame] | 845 | mode: int(c_default="Z_FINISH") = zlib.Z_FINISH |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 846 | One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH. |
| 847 | If mode == Z_FINISH, the compressor object can no longer be |
| 848 | used after calling the flush() method. Otherwise, more data |
| 849 | can still be compressed. |
| 850 | / |
| 851 | |
| 852 | Return a bytes object containing any remaining compressed data. |
| 853 | [clinic start generated code]*/ |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 854 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 855 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 856 | zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode) |
| 857 | /*[clinic end generated code: output=c7efd13efd62add2 input=286146e29442eb6c]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 858 | { |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 859 | int err; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 860 | Py_ssize_t length = DEF_BUF_SIZE; |
| 861 | PyObject *RetVal = NULL; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 862 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 863 | zlibstate *state = PyType_GetModuleState(cls); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 864 | /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in |
| 865 | doing any work at all; just return an empty string. */ |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 866 | if (mode == Z_NO_FLUSH) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 867 | return PyBytes_FromStringAndSize(NULL, 0); |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 868 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 870 | ENTER_ZLIB(self); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 871 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 872 | self->zst.avail_in = 0; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 873 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 874 | do { |
| 875 | length = arrange_output_buffer(&self->zst, &RetVal, length); |
| 876 | if (length < 0) { |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 877 | Py_CLEAR(RetVal); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 878 | goto error; |
Guido van Rossum | 776152b | 2007-05-22 22:44:07 +0000 | [diff] [blame] | 879 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 880 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 881 | Py_BEGIN_ALLOW_THREADS |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 882 | err = deflate(&self->zst, mode); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 883 | Py_END_ALLOW_THREADS |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 884 | |
| 885 | if (err == Z_STREAM_ERROR) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 886 | zlib_error(state, self->zst, err, "while flushing"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 887 | Py_CLEAR(RetVal); |
| 888 | goto error; |
| 889 | } |
| 890 | } while (self->zst.avail_out == 0); |
| 891 | assert(self->zst.avail_in == 0); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 892 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 893 | /* If mode is Z_FINISH, we also have to call deflateEnd() to free |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 894 | various data structures. Note we should only get Z_STREAM_END when |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 895 | mode is Z_FINISH, but checking both for safety*/ |
| 896 | if (err == Z_STREAM_END && mode == Z_FINISH) { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 897 | err = deflateEnd(&self->zst); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 898 | if (err != Z_OK) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 899 | zlib_error(state, self->zst, err, "while finishing compression"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 900 | Py_CLEAR(RetVal); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 901 | goto error; |
| 902 | } |
| 903 | else |
| 904 | self->is_initialised = 0; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 906 | /* We will only get Z_BUF_ERROR if the output buffer was full |
| 907 | but there wasn't more output when we tried again, so it is |
| 908 | not an error condition. |
| 909 | */ |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 910 | } else if (err != Z_OK && err != Z_BUF_ERROR) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 911 | zlib_error(state, self->zst, err, "while flushing"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 912 | Py_CLEAR(RetVal); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 913 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 914 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 915 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 916 | if (_PyBytes_Resize(&RetVal, self->zst.next_out - |
| 917 | (Byte *)PyBytes_AS_STRING(RetVal)) < 0) |
Victor Stinner | 7979926 | 2013-07-09 00:35:22 +0200 | [diff] [blame] | 918 | Py_CLEAR(RetVal); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 919 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 920 | error: |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 921 | LEAVE_ZLIB(self); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 922 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 925 | #ifdef HAVE_ZLIB_COPY |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 926 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 927 | /*[clinic input] |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 928 | zlib.Compress.copy |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 929 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 930 | cls: defining_class |
| 931 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 932 | Return a copy of the compression object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 933 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 934 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 935 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 936 | zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls) |
| 937 | /*[clinic end generated code: output=c4d2cfb4b0d7350b input=235497e482d40986]*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 938 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 939 | zlibstate *state = PyType_GetModuleState(cls); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 940 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 941 | compobject *retval = newcompobject(state->Comptype); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 942 | if (!retval) return NULL; |
| 943 | |
| 944 | /* Copy the zstream state |
| 945 | * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe |
| 946 | */ |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 947 | ENTER_ZLIB(self); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 948 | int err = deflateCopy(&retval->zst, &self->zst); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 949 | switch (err) { |
| 950 | case Z_OK: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 951 | break; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 952 | case Z_STREAM_ERROR: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 953 | PyErr_SetString(PyExc_ValueError, "Inconsistent stream state"); |
| 954 | goto error; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 955 | case Z_MEM_ERROR: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 956 | PyErr_SetString(PyExc_MemoryError, |
| 957 | "Can't allocate memory for compression object"); |
| 958 | goto error; |
| 959 | default: |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 960 | zlib_error(state, self->zst, err, "while copying compression object"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 961 | goto error; |
| 962 | } |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 963 | Py_INCREF(self->unused_data); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 964 | Py_XSETREF(retval->unused_data, self->unused_data); |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 965 | Py_INCREF(self->unconsumed_tail); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 966 | Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail); |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 967 | Py_XINCREF(self->zdict); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 968 | Py_XSETREF(retval->zdict, self->zdict); |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 969 | retval->eof = self->eof; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 970 | |
| 971 | /* Mark it as being initialized */ |
| 972 | retval->is_initialised = 1; |
| 973 | |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 974 | LEAVE_ZLIB(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 975 | return (PyObject *)retval; |
| 976 | |
| 977 | error: |
Larry Hastings | dc6aaec | 2013-11-24 04:41:57 -0800 | [diff] [blame] | 978 | LEAVE_ZLIB(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 979 | Py_XDECREF(retval); |
| 980 | return NULL; |
| 981 | } |
| 982 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 983 | /*[clinic input] |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 984 | zlib.Compress.__copy__ |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 985 | |
| 986 | cls: defining_class |
| 987 | |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 988 | [clinic start generated code]*/ |
| 989 | |
| 990 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 991 | zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls) |
| 992 | /*[clinic end generated code: output=074613db332cb668 input=5c0188367ab0fe64]*/ |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 993 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 994 | return zlib_Compress_copy_impl(self, cls); |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /*[clinic input] |
| 998 | zlib.Compress.__deepcopy__ |
| 999 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1000 | cls: defining_class |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1001 | memo: object |
| 1002 | / |
| 1003 | |
| 1004 | [clinic start generated code]*/ |
| 1005 | |
| 1006 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1007 | zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls, |
| 1008 | PyObject *memo) |
| 1009 | /*[clinic end generated code: output=24b3aed785f54033 input=c90347319a514430]*/ |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1010 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1011 | return zlib_Compress_copy_impl(self, cls); |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /*[clinic input] |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1015 | zlib.Decompress.copy |
| 1016 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1017 | cls: defining_class |
| 1018 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1019 | Return a copy of the decompression object. |
| 1020 | [clinic start generated code]*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1021 | |
| 1022 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1023 | zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls) |
| 1024 | /*[clinic end generated code: output=a7ddc016e1d0a781 input=20ef3aa208282ff2]*/ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1025 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1026 | zlibstate *state = PyType_GetModuleState(cls); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1027 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1028 | compobject *retval = newcompobject(state->Decomptype); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1029 | if (!retval) return NULL; |
| 1030 | |
| 1031 | /* Copy the zstream state |
| 1032 | * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe |
| 1033 | */ |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1034 | ENTER_ZLIB(self); |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1035 | int err = inflateCopy(&retval->zst, &self->zst); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1036 | switch (err) { |
| 1037 | case Z_OK: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1038 | break; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1039 | case Z_STREAM_ERROR: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1040 | PyErr_SetString(PyExc_ValueError, "Inconsistent stream state"); |
| 1041 | goto error; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1042 | case Z_MEM_ERROR: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1043 | PyErr_SetString(PyExc_MemoryError, |
| 1044 | "Can't allocate memory for decompression object"); |
| 1045 | goto error; |
| 1046 | default: |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1047 | zlib_error(state, self->zst, err, "while copying decompression object"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1048 | goto error; |
| 1049 | } |
| 1050 | |
| 1051 | Py_INCREF(self->unused_data); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1052 | Py_XSETREF(retval->unused_data, self->unused_data); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1053 | Py_INCREF(self->unconsumed_tail); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1054 | Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail); |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 1055 | Py_XINCREF(self->zdict); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 1056 | Py_XSETREF(retval->zdict, self->zdict); |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 1057 | retval->eof = self->eof; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1058 | |
| 1059 | /* Mark it as being initialized */ |
| 1060 | retval->is_initialised = 1; |
| 1061 | |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1062 | LEAVE_ZLIB(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1063 | return (PyObject *)retval; |
| 1064 | |
| 1065 | error: |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1066 | LEAVE_ZLIB(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1067 | Py_XDECREF(retval); |
| 1068 | return NULL; |
| 1069 | } |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1070 | |
| 1071 | /*[clinic input] |
| 1072 | zlib.Decompress.__copy__ |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1073 | |
| 1074 | cls: defining_class |
| 1075 | |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1076 | [clinic start generated code]*/ |
| 1077 | |
| 1078 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1079 | zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls) |
| 1080 | /*[clinic end generated code: output=cf1e6473744f53fa input=cc3143067b622bdf]*/ |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1081 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1082 | return zlib_Decompress_copy_impl(self, cls); |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /*[clinic input] |
| 1086 | zlib.Decompress.__deepcopy__ |
| 1087 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1088 | cls: defining_class |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1089 | memo: object |
| 1090 | / |
| 1091 | |
| 1092 | [clinic start generated code]*/ |
| 1093 | |
| 1094 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1095 | zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls, |
| 1096 | PyObject *memo) |
| 1097 | /*[clinic end generated code: output=34f7b719a0c0d51b input=fc13b9c58622544e]*/ |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1098 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1099 | return zlib_Decompress_copy_impl(self, cls); |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1100 | } |
| 1101 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1102 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1103 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1104 | /*[clinic input] |
| 1105 | zlib.Decompress.flush |
| 1106 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1107 | cls: defining_class |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 1108 | length: Py_ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1109 | the initial size of the output buffer. |
| 1110 | / |
| 1111 | |
| 1112 | Return a bytes object containing any remaining decompressed data. |
| 1113 | [clinic start generated code]*/ |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 1114 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1115 | static PyObject * |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1116 | zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls, |
| 1117 | Py_ssize_t length) |
| 1118 | /*[clinic end generated code: output=4532fc280bd0f8f2 input=42f1f4b75230e2cd]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1119 | { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1120 | int err, flush; |
| 1121 | Py_buffer data; |
| 1122 | PyObject *RetVal = NULL; |
| 1123 | Py_ssize_t ibuflen; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1124 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1125 | PyObject *module = PyType_GetModule(cls); |
| 1126 | if (module == NULL) { |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | |
| 1130 | zlibstate *state = get_zlib_state(module); |
| 1131 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1132 | if (length <= 0) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1133 | PyErr_SetString(PyExc_ValueError, "length must be greater than zero"); |
| 1134 | return NULL; |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1135 | } |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 1136 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1137 | if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1138 | return NULL; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1139 | } |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1140 | |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1141 | ENTER_ZLIB(self); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 1142 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1143 | self->zst.next_in = data.buf; |
| 1144 | ibuflen = data.len; |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 1145 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1146 | do { |
| 1147 | arrange_input_buffer(&self->zst, &ibuflen); |
| 1148 | flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH; |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1149 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1150 | do { |
| 1151 | length = arrange_output_buffer(&self->zst, &RetVal, length); |
| 1152 | if (length < 0) |
| 1153 | goto abort; |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1154 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1155 | Py_BEGIN_ALLOW_THREADS |
| 1156 | err = inflate(&self->zst, flush); |
| 1157 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1158 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1159 | 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) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1166 | if (set_inflate_zdict(state, self) < 0) { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1167 | goto abort; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1168 | } |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1169 | else |
| 1170 | break; |
| 1171 | } |
| 1172 | goto save; |
| 1173 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 1174 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1175 | } while (self->zst.avail_out == 0 || err == Z_NEED_DICT); |
| 1176 | |
| 1177 | } while (err != Z_STREAM_END && ibuflen != 0); |
| 1178 | |
| 1179 | save: |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1180 | if (save_unconsumed_input(self, &data, err) < 0) { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1181 | goto abort; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1182 | } |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 1183 | |
Nadeem Vawda | 3bf71c5 | 2011-08-13 15:42:50 +0200 | [diff] [blame] | 1184 | /* If at end of stream, clean up any memory allocated by zlib. */ |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1185 | if (err == Z_STREAM_END) { |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 1186 | self->eof = 1; |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1187 | self->is_initialised = 0; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1188 | err = inflateEnd(&self->zst); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1189 | if (err != Z_OK) { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1190 | zlib_error(state, self->zst, err, "while finishing decompression"); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1191 | goto abort; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 1192 | } |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1193 | } |
Nadeem Vawda | ee7889d | 2012-11-11 02:14:36 +0100 | [diff] [blame] | 1194 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1195 | if (_PyBytes_Resize(&RetVal, self->zst.next_out - |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1196 | (Byte *)PyBytes_AS_STRING(RetVal)) == 0) { |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1197 | goto success; |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1198 | } |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1199 | |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1200 | abort: |
| 1201 | Py_CLEAR(RetVal); |
| 1202 | success: |
| 1203 | PyBuffer_Release(&data); |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1204 | LEAVE_ZLIB(self); |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 1205 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Christian Heimes | 936e2f3 | 2014-01-27 01:06:57 +0100 | [diff] [blame] | 1208 | #include "clinic/zlibmodule.c.h" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1209 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1210 | static PyMethodDef comp_methods[] = |
| 1211 | { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1212 | ZLIB_COMPRESS_COMPRESS_METHODDEF |
| 1213 | ZLIB_COMPRESS_FLUSH_METHODDEF |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1214 | ZLIB_COMPRESS_COPY_METHODDEF |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1215 | ZLIB_COMPRESS___COPY___METHODDEF |
| 1216 | ZLIB_COMPRESS___DEEPCOPY___METHODDEF |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1217 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1218 | }; |
| 1219 | |
| 1220 | static PyMethodDef Decomp_methods[] = |
| 1221 | { |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 1222 | ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1223 | ZLIB_DECOMPRESS_FLUSH_METHODDEF |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1224 | ZLIB_DECOMPRESS_COPY_METHODDEF |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 1225 | ZLIB_DECOMPRESS___COPY___METHODDEF |
| 1226 | ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1227 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1228 | }; |
| 1229 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1230 | #define COMP_OFF(x) offsetof(compobject, x) |
| 1231 | static PyMemberDef Decomp_members[] = { |
| 1232 | {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY}, |
| 1233 | {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY}, |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 1234 | {"eof", T_BOOL, COMP_OFF(eof), READONLY}, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1235 | {NULL}, |
| 1236 | }; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1237 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1238 | /*[clinic input] |
| 1239 | zlib.adler32 |
| 1240 | |
| 1241 | data: Py_buffer |
| 1242 | value: unsigned_int(bitwise=True) = 1 |
| 1243 | Starting value of the checksum. |
| 1244 | / |
| 1245 | |
| 1246 | Compute an Adler-32 checksum of data. |
| 1247 | |
| 1248 | The returned checksum is an integer. |
| 1249 | [clinic start generated code]*/ |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 1250 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1251 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1252 | zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value) |
| 1253 | /*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1254 | { |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1255 | /* Releasing the GIL for very small buffers is inefficient |
| 1256 | and may lower performance */ |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1257 | if (data->len > 1024*5) { |
| 1258 | unsigned char *buf = data->buf; |
| 1259 | Py_ssize_t len = data->len; |
Antoine Pitrou | 9e719b6 | 2011-02-28 23:48:16 +0000 | [diff] [blame] | 1260 | |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1261 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 9e719b6 | 2011-02-28 23:48:16 +0000 | [diff] [blame] | 1262 | /* Avoid truncation of length for very large buffers. adler32() takes |
| 1263 | length as an unsigned int, which may be narrower than Py_ssize_t. */ |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 1264 | while ((size_t)len > UINT_MAX) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1265 | value = adler32(value, buf, UINT_MAX); |
Antoine Pitrou | 9e719b6 | 2011-02-28 23:48:16 +0000 | [diff] [blame] | 1266 | buf += (size_t) UINT_MAX; |
| 1267 | len -= (size_t) UINT_MAX; |
| 1268 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1269 | value = adler32(value, buf, (unsigned int)len); |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1270 | Py_END_ALLOW_THREADS |
| 1271 | } else { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1272 | value = adler32(value, data->buf, (unsigned int)data->len); |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1273 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1274 | return PyLong_FromUnsignedLong(value & 0xffffffffU); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1275 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1276 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1277 | /*[clinic input] |
| 1278 | zlib.crc32 |
| 1279 | |
| 1280 | data: Py_buffer |
| 1281 | value: unsigned_int(bitwise=True) = 0 |
| 1282 | Starting value of the checksum. |
| 1283 | / |
| 1284 | |
| 1285 | Compute a CRC-32 checksum of data. |
| 1286 | |
| 1287 | The returned checksum is an integer. |
| 1288 | [clinic start generated code]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1289 | |
| 1290 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1291 | zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value) |
| 1292 | /*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1293 | { |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1294 | int signed_val; |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 1295 | |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1296 | /* Releasing the GIL for very small buffers is inefficient |
| 1297 | and may lower performance */ |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1298 | if (data->len > 1024*5) { |
| 1299 | unsigned char *buf = data->buf; |
| 1300 | Py_ssize_t len = data->len; |
Antoine Pitrou | 9e719b6 | 2011-02-28 23:48:16 +0000 | [diff] [blame] | 1301 | |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1302 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | 9e719b6 | 2011-02-28 23:48:16 +0000 | [diff] [blame] | 1303 | /* Avoid truncation of length for very large buffers. crc32() takes |
| 1304 | length as an unsigned int, which may be narrower than Py_ssize_t. */ |
Victor Stinner | e079edd | 2013-11-21 22:33:21 +0100 | [diff] [blame] | 1305 | while ((size_t)len > UINT_MAX) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1306 | value = crc32(value, buf, UINT_MAX); |
Antoine Pitrou | 9e719b6 | 2011-02-28 23:48:16 +0000 | [diff] [blame] | 1307 | buf += (size_t) UINT_MAX; |
| 1308 | len -= (size_t) UINT_MAX; |
| 1309 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1310 | signed_val = crc32(value, buf, (unsigned int)len); |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1311 | Py_END_ALLOW_THREADS |
| 1312 | } else { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1313 | signed_val = crc32(value, data->buf, (unsigned int)data->len); |
Antoine Pitrou | 31f30b1 | 2009-01-02 17:34:35 +0000 | [diff] [blame] | 1314 | } |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 1315 | return PyLong_FromUnsignedLong(signed_val & 0xffffffffU); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1316 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1317 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1318 | |
| 1319 | static PyMethodDef zlib_methods[] = |
| 1320 | { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1321 | ZLIB_ADLER32_METHODDEF |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 1322 | ZLIB_COMPRESS_METHODDEF |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1323 | ZLIB_COMPRESSOBJ_METHODDEF |
| 1324 | ZLIB_CRC32_METHODDEF |
| 1325 | ZLIB_DECOMPRESS_METHODDEF |
| 1326 | ZLIB_DECOMPRESSOBJ_METHODDEF |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1327 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1328 | }; |
| 1329 | |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1330 | static PyType_Slot Comptype_slots[] = { |
| 1331 | {Py_tp_dealloc, Comp_dealloc}, |
| 1332 | {Py_tp_methods, comp_methods}, |
| 1333 | {0, 0}, |
| 1334 | }; |
| 1335 | |
| 1336 | static PyType_Spec Comptype_spec = { |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1337 | "zlib.Compress", |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1338 | sizeof(compobject), |
| 1339 | 0, |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1340 | Py_TPFLAGS_DEFAULT, |
| 1341 | Comptype_slots |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1342 | }; |
| 1343 | |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1344 | static PyType_Slot Decomptype_slots[] = { |
| 1345 | {Py_tp_dealloc, Decomp_dealloc}, |
| 1346 | {Py_tp_methods, Decomp_methods}, |
| 1347 | {Py_tp_members, Decomp_members}, |
| 1348 | {0, 0}, |
| 1349 | }; |
| 1350 | |
| 1351 | static PyType_Spec Decomptype_spec = { |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1352 | "zlib.Decompress", |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1353 | sizeof(compobject), |
| 1354 | 0, |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1355 | Py_TPFLAGS_DEFAULT, |
| 1356 | Decomptype_slots |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1357 | }; |
| 1358 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1359 | PyDoc_STRVAR(zlib_module_documentation, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1360 | "The functions in this module allow compression and decompression using the\n" |
| 1361 | "zlib library, which is based on GNU zip.\n" |
| 1362 | "\n" |
| 1363 | "adler32(string[, start]) -- Compute an Adler-32 checksum.\n" |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 1364 | "compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n" |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 1365 | "compressobj([level[, ...]]) -- Return a compressor object.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1366 | "crc32(string[, start]) -- Compute a CRC-32 checksum.\n" |
Andrew M. Kuchling | 313a3e3 | 1999-12-20 22:13:38 +0000 | [diff] [blame] | 1367 | "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n" |
Nadeem Vawda | fd8a838 | 2012-06-21 02:13:12 +0200 | [diff] [blame] | 1368 | "decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1369 | "\n" |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 1370 | "'wbits' is window buffer size and container format.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1371 | "Compressor objects support compress() and flush() methods; decompressor\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1372 | "objects support decompress() and flush()."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 1373 | |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1374 | static int |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1375 | zlib_clear(PyObject *mod) |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1376 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1377 | zlibstate *state = get_zlib_state(mod); |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1378 | Py_CLEAR(state->Comptype); |
| 1379 | Py_CLEAR(state->Decomptype); |
| 1380 | Py_CLEAR(state->ZlibError); |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | static int |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1385 | zlib_traverse(PyObject *mod, visitproc visit, void *arg) |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1386 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1387 | zlibstate *state = get_zlib_state(mod); |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1388 | Py_VISIT(state->Comptype); |
| 1389 | Py_VISIT(state->Decomptype); |
| 1390 | Py_VISIT(state->ZlibError); |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | static void |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1395 | zlib_free(void *mod) |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1396 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1397 | zlib_clear((PyObject *)mod); |
Dino Viehland | a1ffad0 | 2019-09-10 11:27:03 +0100 | [diff] [blame] | 1398 | } |
| 1399 | |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1400 | static int |
| 1401 | zlib_exec(PyObject *mod) |
| 1402 | { |
| 1403 | zlibstate *state = get_zlib_state(mod); |
| 1404 | |
| 1405 | state->Comptype = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 1406 | mod, &Comptype_spec, NULL); |
| 1407 | if (state->Comptype == NULL) { |
| 1408 | return -1; |
| 1409 | } |
| 1410 | |
| 1411 | state->Decomptype = (PyTypeObject *)PyType_FromModuleAndSpec( |
| 1412 | mod, &Decomptype_spec, NULL); |
| 1413 | if (state->Decomptype == NULL) { |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
| 1417 | state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL); |
| 1418 | if (state->ZlibError == NULL) { |
| 1419 | return -1; |
| 1420 | } |
| 1421 | |
| 1422 | Py_INCREF(state->ZlibError); |
| 1423 | if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) { |
| 1424 | Py_DECREF(state->ZlibError); |
| 1425 | return -1; |
| 1426 | } |
| 1427 | |
| 1428 | #define ZLIB_ADD_INT_MACRO(c) \ |
| 1429 | do { \ |
| 1430 | if ((PyModule_AddIntConstant(mod, #c, c)) < 0) { \ |
| 1431 | return -1; \ |
| 1432 | } \ |
| 1433 | } while(0) |
| 1434 | |
| 1435 | ZLIB_ADD_INT_MACRO(MAX_WBITS); |
| 1436 | ZLIB_ADD_INT_MACRO(DEFLATED); |
| 1437 | ZLIB_ADD_INT_MACRO(DEF_MEM_LEVEL); |
| 1438 | ZLIB_ADD_INT_MACRO(DEF_BUF_SIZE); |
| 1439 | // compression levels |
| 1440 | ZLIB_ADD_INT_MACRO(Z_NO_COMPRESSION); |
| 1441 | ZLIB_ADD_INT_MACRO(Z_BEST_SPEED); |
| 1442 | ZLIB_ADD_INT_MACRO(Z_BEST_COMPRESSION); |
| 1443 | ZLIB_ADD_INT_MACRO(Z_DEFAULT_COMPRESSION); |
| 1444 | // compression strategies |
| 1445 | ZLIB_ADD_INT_MACRO(Z_FILTERED); |
| 1446 | ZLIB_ADD_INT_MACRO(Z_HUFFMAN_ONLY); |
| 1447 | #ifdef Z_RLE // 1.2.0.1 |
| 1448 | ZLIB_ADD_INT_MACRO(Z_RLE); |
| 1449 | #endif |
| 1450 | #ifdef Z_FIXED // 1.2.2.2 |
| 1451 | ZLIB_ADD_INT_MACRO(Z_FIXED); |
| 1452 | #endif |
| 1453 | ZLIB_ADD_INT_MACRO(Z_DEFAULT_STRATEGY); |
| 1454 | // allowed flush values |
| 1455 | ZLIB_ADD_INT_MACRO(Z_NO_FLUSH); |
| 1456 | ZLIB_ADD_INT_MACRO(Z_PARTIAL_FLUSH); |
| 1457 | ZLIB_ADD_INT_MACRO(Z_SYNC_FLUSH); |
| 1458 | ZLIB_ADD_INT_MACRO(Z_FULL_FLUSH); |
| 1459 | ZLIB_ADD_INT_MACRO(Z_FINISH); |
| 1460 | #ifdef Z_BLOCK // 1.2.0.5 for inflate, 1.2.3.4 for deflate |
| 1461 | ZLIB_ADD_INT_MACRO(Z_BLOCK); |
| 1462 | #endif |
| 1463 | #ifdef Z_TREES // 1.2.3.4, only for inflate |
| 1464 | ZLIB_ADD_INT_MACRO(Z_TREES); |
| 1465 | #endif |
| 1466 | PyObject *ver = PyUnicode_FromString(ZLIB_VERSION); |
| 1467 | if (ver == NULL) { |
| 1468 | return -1; |
| 1469 | } |
| 1470 | |
| 1471 | if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) { |
| 1472 | Py_DECREF(ver); |
| 1473 | return -1; |
| 1474 | } |
| 1475 | |
| 1476 | ver = PyUnicode_FromString(zlibVersion()); |
| 1477 | if (ver == NULL) { |
| 1478 | return -1; |
| 1479 | } |
| 1480 | |
| 1481 | if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) { |
| 1482 | Py_DECREF(ver); |
| 1483 | return -1; |
| 1484 | } |
| 1485 | |
| 1486 | if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) { |
| 1487 | return -1; |
| 1488 | } |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | static PyModuleDef_Slot zlib_slots[] = { |
| 1493 | {Py_mod_exec, zlib_exec}, |
| 1494 | {0, NULL} |
| 1495 | }; |
| 1496 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1497 | static struct PyModuleDef zlibmodule = { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1498 | PyModuleDef_HEAD_INIT, |
| 1499 | .m_name = "zlib", |
| 1500 | .m_doc = zlib_module_documentation, |
| 1501 | .m_size = sizeof(zlibstate), |
| 1502 | .m_methods = zlib_methods, |
| 1503 | .m_slots = zlib_slots, |
| 1504 | .m_traverse = zlib_traverse, |
| 1505 | .m_clear = zlib_clear, |
| 1506 | .m_free = zlib_free, |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1507 | }; |
| 1508 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1509 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1510 | PyInit_zlib(void) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1511 | { |
Mohamed Koubaa | 1aaa21f | 2020-09-07 03:27:55 -0500 | [diff] [blame] | 1512 | return PyModuleDef_Init(&zlibmodule); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1513 | } |