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