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