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