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