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