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