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