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