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