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