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