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