Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1 | /* zlibmodule.c -- gzip-compatible data compression */ |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 2 | /* See http://zlib.net/ */ |
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 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 8 | #include "Python.h" |
| 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 |
| 12 | #include "pythread.h" |
| 13 | |
| 14 | /* #defs ripped off from _tkinter.c, even though the situation here is much |
| 15 | simpler, because we don't have to worry about waiting for Tcl |
| 16 | events! And, since zlib itself is threadsafe, we don't need to worry |
| 17 | about re-entering zlib functions. |
| 18 | |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 19 | N.B. |
| 20 | |
| 21 | Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions |
| 22 | that modify the components of preexisting de/compress objects, it |
| 23 | could prove to be a performance gain on multiprocessor machines if |
| 24 | there was an de/compress object-specific lock. However, for the |
| 25 | moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL |
| 26 | de/compress objects. |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */ |
| 30 | |
| 31 | #define ENTER_ZLIB \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 32 | Py_BEGIN_ALLOW_THREADS \ |
| 33 | PyThread_acquire_lock(zlib_lock, 1); \ |
| 34 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 35 | |
| 36 | #define LEAVE_ZLIB \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 37 | PyThread_release_lock(zlib_lock); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 38 | |
| 39 | #else |
| 40 | |
| 41 | #define ENTER_ZLIB |
| 42 | #define LEAVE_ZLIB |
| 43 | |
| 44 | #endif |
| 45 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 46 | /* The following parameters are copied from zutil.h, version 0.95 */ |
| 47 | #define DEFLATED 8 |
| 48 | #if MAX_MEM_LEVEL >= 8 |
| 49 | # define DEF_MEM_LEVEL 8 |
| 50 | #else |
| 51 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL |
| 52 | #endif |
| 53 | #define DEF_WBITS MAX_WBITS |
| 54 | |
Guido van Rossum | b729a1d | 1999-04-07 20:23:17 +0000 | [diff] [blame] | 55 | /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */ |
| 56 | #define DEFAULTALLOC (16*1024) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 57 | #define PyInit_zlib initzlib |
| 58 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 59 | static PyTypeObject Comptype; |
| 60 | static PyTypeObject Decomptype; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 61 | |
| 62 | static PyObject *ZlibError; |
| 63 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 64 | typedef struct |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 65 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 66 | PyObject_HEAD |
| 67 | z_stream zst; |
| 68 | PyObject *unused_data; |
| 69 | PyObject *unconsumed_tail; |
| 70 | int is_initialised; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 71 | } compobject; |
| 72 | |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 73 | static void |
| 74 | zlib_error(z_stream zst, int err, char *msg) |
| 75 | { |
Nadeem Vawda | bbabbae | 2011-08-28 11:23:57 +0200 | [diff] [blame] | 76 | const char *zmsg = Z_NULL; |
| 77 | /* In case of a version mismatch, zst.msg won't be initialized. |
| 78 | Check for this case first, before looking at zst.msg. */ |
| 79 | if (err == Z_VERSION_ERROR) |
| 80 | zmsg = "library version mismatch"; |
| 81 | if (zmsg == Z_NULL) |
| 82 | zmsg = zst.msg; |
Antoine Pitrou | fc3bfad | 2010-05-11 23:42:28 +0000 | [diff] [blame] | 83 | if (zmsg == Z_NULL) { |
| 84 | switch (err) { |
| 85 | case Z_BUF_ERROR: |
| 86 | zmsg = "incomplete or truncated stream"; |
| 87 | break; |
| 88 | case Z_STREAM_ERROR: |
| 89 | zmsg = "inconsistent stream state"; |
| 90 | break; |
| 91 | case Z_DATA_ERROR: |
| 92 | zmsg = "invalid input data"; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | if (zmsg == Z_NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 97 | PyErr_Format(ZlibError, "Error %d %s", err, msg); |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 98 | else |
Antoine Pitrou | fc3bfad | 2010-05-11 23:42:28 +0000 | [diff] [blame] | 99 | PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg); |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 102 | PyDoc_STRVAR(compressobj__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 103 | "compressobj([level]) -- Return a compressor object.\n" |
| 104 | "\n" |
Martin Panter | 1d269c1 | 2016-02-03 07:06:33 +0000 | [diff] [blame] | 105 | "Optional arg level is the compression level, in 0-9 or -1."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 107 | PyDoc_STRVAR(decompressobj__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 108 | "decompressobj([wbits]) -- Return a decompressor object.\n" |
| 109 | "\n" |
Martin Panter | 9c946bb | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 110 | "Optional arg wbits indicates the window buffer size and container format."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 112 | static compobject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 113 | newcompobject(PyTypeObject *type) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 114 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 115 | compobject *self; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 116 | self = PyObject_New(compobject, type); |
| 117 | if (self == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 118 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 119 | self->is_initialised = 0; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 120 | self->unused_data = PyString_FromString(""); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 121 | if (self->unused_data == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 122 | Py_DECREF(self); |
| 123 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 124 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 125 | self->unconsumed_tail = PyString_FromString(""); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 126 | if (self->unconsumed_tail == NULL) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 127 | Py_DECREF(self); |
| 128 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 129 | } |
| 130 | return self; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 133 | static void |
| 134 | arrange_input_buffer(z_stream *zst, Py_ssize_t *remains) |
| 135 | { |
| 136 | if ((size_t)*remains > UINT_MAX) { |
| 137 | zst->avail_in = UINT_MAX; |
| 138 | } else { |
| 139 | zst->avail_in = *remains; |
| 140 | } |
| 141 | *remains -= zst->avail_in; |
| 142 | } |
| 143 | |
| 144 | static Py_ssize_t |
| 145 | arrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer, |
| 146 | Py_ssize_t length, |
| 147 | Py_ssize_t max_length) |
| 148 | { |
| 149 | Py_ssize_t occupied; |
| 150 | |
| 151 | if (*buffer == NULL) { |
| 152 | if (!(*buffer = PyBytes_FromStringAndSize(NULL, length))) |
| 153 | return -1; |
| 154 | occupied = 0; |
| 155 | } |
| 156 | else { |
| 157 | occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer); |
| 158 | |
| 159 | if (length == occupied) { |
| 160 | Py_ssize_t new_length; |
| 161 | assert(length <= max_length); |
| 162 | /* can not scale the buffer over max_length */ |
| 163 | if (length == max_length) |
| 164 | return -2; |
| 165 | if (length <= (max_length >> 1)) |
| 166 | new_length = length << 1; |
| 167 | else |
| 168 | new_length = max_length; |
| 169 | if (_PyBytes_Resize(buffer, new_length) < 0) |
| 170 | return -1; |
| 171 | length = new_length; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if ((size_t)(length - occupied) > UINT_MAX) { |
| 176 | zst->avail_out = UINT_MAX; |
| 177 | } else { |
| 178 | zst->avail_out = length - occupied; |
| 179 | } |
| 180 | zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied; |
| 181 | |
| 182 | return length; |
| 183 | } |
| 184 | |
| 185 | static Py_ssize_t |
| 186 | arrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length) |
| 187 | { |
| 188 | Py_ssize_t ret; |
| 189 | |
| 190 | ret = arrange_output_buffer_with_maximum(zst, buffer, length, |
| 191 | PY_SSIZE_T_MAX); |
| 192 | if (ret == -2) |
| 193 | PyErr_NoMemory(); |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 198 | PyDoc_STRVAR(compress__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 199 | "compress(string[, level]) -- Returned compressed string.\n" |
| 200 | "\n" |
Nadeem Vawda | 99f9b8d | 2012-11-11 14:01:23 +0100 | [diff] [blame] | 201 | "Optional arg level is the compression level, in 0-9."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 203 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 204 | PyZlib_compress(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 205 | { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 206 | PyObject *RetVal = NULL; |
| 207 | Byte *ibuf; |
| 208 | Py_ssize_t ibuflen, obuflen = DEFAULTALLOC; |
| 209 | int level=Z_DEFAULT_COMPRESSION; |
| 210 | int err, flush; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 211 | z_stream zst; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 212 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 213 | /* require Python string object, optional 'level' arg */ |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 214 | if (!PyArg_ParseTuple(args, "s#|i:compress", &ibuf, &ibuflen, &level)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 215 | return NULL; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 216 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 217 | zst.zalloc = (alloc_func)NULL; |
| 218 | zst.zfree = (free_func)Z_NULL; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 219 | zst.next_in = ibuf; |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 220 | err = deflateInit(&zst, level); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 221 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 222 | switch (err) { |
| 223 | case Z_OK: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 224 | break; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 225 | case Z_MEM_ERROR: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 226 | PyErr_SetString(PyExc_MemoryError, |
| 227 | "Out of memory while compressing data"); |
| 228 | goto error; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 229 | case Z_STREAM_ERROR: |
| 230 | PyErr_SetString(ZlibError, "Bad compression level"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | goto error; |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 232 | default: |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 233 | deflateEnd(&zst); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 234 | zlib_error(zst, err, "while compressing data"); |
| 235 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 236 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 237 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 238 | do { |
| 239 | arrange_input_buffer(&zst, &ibuflen); |
| 240 | flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 241 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 242 | do { |
| 243 | obuflen = arrange_output_buffer(&zst, &RetVal, obuflen); |
| 244 | if (obuflen < 0) { |
| 245 | deflateEnd(&zst); |
| 246 | goto error; |
| 247 | } |
| 248 | |
| 249 | Py_BEGIN_ALLOW_THREADS |
| 250 | err = deflate(&zst, flush); |
| 251 | Py_END_ALLOW_THREADS |
| 252 | |
| 253 | if (err == Z_STREAM_ERROR) { |
| 254 | deflateEnd(&zst); |
| 255 | zlib_error(zst, err, "while compressing data"); |
| 256 | goto error; |
| 257 | } |
| 258 | |
| 259 | } while (zst.avail_out == 0); |
| 260 | assert(zst.avail_in == 0); |
| 261 | |
| 262 | } while (flush != Z_FINISH); |
| 263 | assert(err == Z_STREAM_END); |
| 264 | |
| 265 | err = deflateEnd(&zst); |
| 266 | if (err == Z_OK) { |
| 267 | if (_PyBytes_Resize(&RetVal, zst.next_out - |
| 268 | (Byte *)PyBytes_AS_STRING(RetVal)) < 0) |
| 269 | goto error; |
| 270 | return RetVal; |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 271 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 272 | else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 273 | zlib_error(zst, err, "while finishing compression"); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 274 | error: |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 275 | Py_XDECREF(RetVal); |
| 276 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 279 | PyDoc_STRVAR(decompress__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 280 | "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n" |
| 281 | "\n" |
Martin Panter | 9c946bb | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 282 | "Optional arg wbits indicates the window buffer size and container format.\n" |
| 283 | "Optional arg bufsize is the initial output buffer size."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 285 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 286 | PyZlib_decompress(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 287 | { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 288 | PyObject *RetVal = NULL; |
| 289 | Byte *ibuf; |
| 290 | Py_ssize_t ibuflen; |
| 291 | int err, flush; |
Christian Heimes | 901071b | 2007-11-21 00:46:21 +0000 | [diff] [blame] | 292 | int wsize=DEF_WBITS; |
| 293 | Py_ssize_t r_strlen=DEFAULTALLOC; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 294 | z_stream zst; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 295 | |
Christian Heimes | 901071b | 2007-11-21 00:46:21 +0000 | [diff] [blame] | 296 | if (!PyArg_ParseTuple(args, "s#|in:decompress", |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 297 | &ibuf, &ibuflen, &wsize, &r_strlen)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 299 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 300 | if (r_strlen <= 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 301 | r_strlen = 1; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 302 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 303 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 304 | zst.zalloc = (alloc_func)NULL; |
| 305 | zst.zfree = (free_func)Z_NULL; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 306 | zst.avail_in = 0; |
| 307 | zst.next_in = ibuf; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 308 | err = inflateInit2(&zst, wsize); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 309 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 310 | switch (err) { |
| 311 | case Z_OK: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 312 | break; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 313 | case Z_MEM_ERROR: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 314 | PyErr_SetString(PyExc_MemoryError, |
| 315 | "Out of memory while decompressing data"); |
| 316 | goto error; |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 317 | default: |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 318 | inflateEnd(&zst); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 319 | zlib_error(zst, err, "while preparing to decompress data"); |
| 320 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 321 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 322 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 323 | do { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 324 | arrange_input_buffer(&zst, &ibuflen); |
| 325 | flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 326 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 327 | do { |
| 328 | r_strlen = arrange_output_buffer(&zst, &RetVal, r_strlen); |
| 329 | if (r_strlen < 0) { |
| 330 | inflateEnd(&zst); |
| 331 | goto error; |
| 332 | } |
| 333 | |
| 334 | Py_BEGIN_ALLOW_THREADS |
| 335 | err = inflate(&zst, flush); |
| 336 | Py_END_ALLOW_THREADS |
| 337 | |
| 338 | switch (err) { |
| 339 | case Z_OK: /* fall through */ |
| 340 | case Z_BUF_ERROR: /* fall through */ |
| 341 | case Z_STREAM_END: |
| 342 | break; |
| 343 | case Z_MEM_ERROR: |
| 344 | inflateEnd(&zst); |
| 345 | PyErr_SetString(PyExc_MemoryError, |
| 346 | "Out of memory while decompressing data"); |
| 347 | goto error; |
| 348 | default: |
| 349 | inflateEnd(&zst); |
Antoine Pitrou | fc3bfad | 2010-05-11 23:42:28 +0000 | [diff] [blame] | 350 | zlib_error(zst, err, "while decompressing data"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 351 | goto error; |
| 352 | } |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 353 | |
| 354 | } while (zst.avail_out == 0); |
| 355 | |
| 356 | } while (err != Z_STREAM_END && ibuflen != 0); |
| 357 | |
| 358 | |
| 359 | if (err != Z_STREAM_END) { |
| 360 | inflateEnd(&zst); |
| 361 | zlib_error(zst, err, "while decompressing data"); |
| 362 | goto error; |
| 363 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 364 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 365 | err = inflateEnd(&zst); |
| 366 | if (err != Z_OK) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 367 | zlib_error(zst, err, "while finishing data decompression"); |
| 368 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 369 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 370 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 371 | _PyString_Resize(&RetVal, zst.next_out - |
| 372 | (Byte *)PyBytes_AS_STRING(RetVal)); |
| 373 | return RetVal; |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 374 | |
| 375 | error: |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 376 | Py_XDECREF(RetVal); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 377 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 381 | PyZlib_compressobj(PyObject *selfptr, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 382 | { |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 383 | compobject *self; |
| 384 | int level=Z_DEFAULT_COMPRESSION, method=DEFLATED; |
| 385 | int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 386 | |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 387 | if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 388 | &memLevel, &strategy)) |
| 389 | return NULL; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 390 | |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 391 | self = newcompobject(&Comptype); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 392 | if (self == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 393 | return(NULL); |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 394 | self->zst.zalloc = (alloc_func)NULL; |
| 395 | self->zst.zfree = (free_func)Z_NULL; |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 396 | self->zst.next_in = NULL; |
| 397 | self->zst.avail_in = 0; |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 398 | err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 399 | switch (err) { |
| 400 | case Z_OK: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 401 | self->is_initialised = 1; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 402 | return (PyObject *)self; |
| 403 | case Z_MEM_ERROR: |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 404 | Py_DECREF(self); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 405 | PyErr_SetString(PyExc_MemoryError, |
| 406 | "Can't allocate memory for compression object"); |
| 407 | return NULL; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 408 | case Z_STREAM_ERROR: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 409 | Py_DECREF(self); |
| 410 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
| 411 | return NULL; |
| 412 | default: |
| 413 | zlib_error(self->zst, err, "while creating compression object"); |
| 414 | Py_DECREF(self); |
| 415 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 420 | PyZlib_decompressobj(PyObject *selfptr, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 421 | { |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 422 | int wbits=DEF_WBITS, err; |
| 423 | compobject *self; |
| 424 | if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 425 | return NULL; |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 426 | |
| 427 | self = newcompobject(&Decomptype); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 428 | if (self == NULL) |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 429 | return NULL; |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 430 | self->zst.zalloc = (alloc_func)NULL; |
| 431 | self->zst.zfree = (free_func)Z_NULL; |
Andrew M. Kuchling | 3b585b3 | 2004-12-28 20:10:48 +0000 | [diff] [blame] | 432 | self->zst.next_in = NULL; |
| 433 | self->zst.avail_in = 0; |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 434 | err = inflateInit2(&self->zst, wbits); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 435 | switch (err) { |
| 436 | case Z_OK: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 437 | self->is_initialised = 1; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 438 | return (PyObject *)self; |
| 439 | case Z_STREAM_ERROR: |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 440 | Py_DECREF(self); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 441 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
| 442 | return NULL; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 443 | case Z_MEM_ERROR: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 444 | Py_DECREF(self); |
| 445 | PyErr_SetString(PyExc_MemoryError, |
| 446 | "Can't allocate memory for decompression object"); |
| 447 | return NULL; |
| 448 | default: |
| 449 | zlib_error(self->zst, err, "while creating decompression object"); |
| 450 | Py_DECREF(self); |
| 451 | return NULL; |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 452 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 456 | Comp_dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 457 | { |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 458 | if (self->is_initialised) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 459 | deflateEnd(&self->zst); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 460 | Py_XDECREF(self->unused_data); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 461 | Py_XDECREF(self->unconsumed_tail); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 462 | PyObject_Del(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 466 | Decomp_dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 467 | { |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 468 | if (self->is_initialised) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 469 | inflateEnd(&self->zst); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 470 | Py_XDECREF(self->unused_data); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 471 | Py_XDECREF(self->unconsumed_tail); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 472 | PyObject_Del(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 475 | PyDoc_STRVAR(comp_compress__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 476 | "compress(data) -- Return a string containing data compressed.\n" |
| 477 | "\n" |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 478 | "After calling this function, some of the input data may still\n" |
| 479 | "be stored in internal buffers for later processing.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 480 | "Call the flush() method to clear these buffers."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 481 | |
| 482 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 483 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 484 | PyZlib_objcompress(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 485 | { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 486 | PyObject *RetVal = NULL; |
| 487 | Py_ssize_t ibuflen, obuflen = DEFAULTALLOC; |
| 488 | int err; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 489 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 490 | if (!PyArg_ParseTuple(args, "s#:compress", &self->zst.next_in, &ibuflen)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 491 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 492 | |
| 493 | ENTER_ZLIB |
| 494 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 495 | do { |
| 496 | arrange_input_buffer(&self->zst, &ibuflen); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 497 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 498 | do { |
| 499 | obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen); |
| 500 | if (obuflen < 0) |
| 501 | goto error; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 502 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 503 | Py_BEGIN_ALLOW_THREADS |
| 504 | err = deflate(&self->zst, Z_NO_FLUSH); |
| 505 | Py_END_ALLOW_THREADS |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 506 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 507 | if (err == Z_STREAM_ERROR) { |
| 508 | zlib_error(self->zst, err, "while compressing data"); |
| 509 | goto error; |
| 510 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 511 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 512 | } while (self->zst.avail_out == 0); |
| 513 | assert(self->zst.avail_in == 0); |
| 514 | |
| 515 | } while (ibuflen != 0); |
| 516 | |
| 517 | _PyString_Resize(&RetVal, self->zst.next_out - |
| 518 | (Byte *)PyBytes_AS_STRING(RetVal)); |
| 519 | goto success; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 520 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 521 | error: |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 522 | Py_CLEAR(RetVal); |
| 523 | success: |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 524 | LEAVE_ZLIB |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 525 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 528 | /* Helper for objdecompress() and unflush(). Saves any unconsumed input data in |
| 529 | self->unused_data or self->unconsumed_tail, as appropriate. */ |
| 530 | static int |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 531 | save_unconsumed_input(compobject *self, Byte *input, Py_ssize_t inplen, |
| 532 | int err) |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 533 | { |
| 534 | if (err == Z_STREAM_END) { |
| 535 | /* The end of the compressed data has been reached. Store the leftover |
| 536 | input data in self->unused_data. */ |
| 537 | if (self->zst.avail_in > 0) { |
| 538 | Py_ssize_t old_size = PyString_GET_SIZE(self->unused_data); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 539 | Py_ssize_t new_size, left_size; |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 540 | PyObject *new_data; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 541 | left_size = input + inplen - self->zst.next_in; |
| 542 | if (left_size > (PY_SSIZE_T_MAX - old_size)) { |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 543 | PyErr_NoMemory(); |
| 544 | return -1; |
| 545 | } |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 546 | new_size = old_size + left_size; |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 547 | new_data = PyString_FromStringAndSize(NULL, new_size); |
| 548 | if (new_data == NULL) |
| 549 | return -1; |
| 550 | Py_MEMCPY(PyString_AS_STRING(new_data), |
| 551 | PyString_AS_STRING(self->unused_data), old_size); |
| 552 | Py_MEMCPY(PyString_AS_STRING(new_data) + old_size, |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 553 | self->zst.next_in, left_size); |
Serhiy Storchaka | 763a61c | 2016-04-10 18:05:12 +0300 | [diff] [blame] | 554 | Py_SETREF(self->unused_data, new_data); |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 555 | self->zst.avail_in = 0; |
| 556 | } |
| 557 | } |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 558 | |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 559 | if (self->zst.avail_in > 0 || PyString_GET_SIZE(self->unconsumed_tail)) { |
| 560 | /* This code handles two distinct cases: |
| 561 | 1. Output limit was reached. Save leftover input in unconsumed_tail. |
| 562 | 2. All input data was consumed. Clear unconsumed_tail. */ |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 563 | Py_ssize_t left_size = input + inplen - self->zst.next_in; |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 564 | PyObject *new_data = PyString_FromStringAndSize( |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 565 | (char *)self->zst.next_in, left_size); |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 566 | if (new_data == NULL) |
| 567 | return -1; |
Serhiy Storchaka | 763a61c | 2016-04-10 18:05:12 +0300 | [diff] [blame] | 568 | Py_SETREF(self->unconsumed_tail, new_data); |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 569 | } |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 570 | |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 574 | PyDoc_STRVAR(decomp_decompress__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 575 | "decompress(data, max_length) -- Return a string containing the decompressed\n" |
| 576 | "version of the data.\n" |
| 577 | "\n" |
| 578 | "After calling this function, some of the input data may still be stored in\n" |
| 579 | "internal buffers for later processing.\n" |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 580 | "Call the flush() method to clear these buffers.\n" |
| 581 | "If the max_length parameter is specified then the return value will be\n" |
| 582 | "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] | 583 | "the unconsumed_tail attribute."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 584 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 585 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 586 | PyZlib_objdecompress(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 587 | { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 588 | int err = Z_OK; |
| 589 | Py_ssize_t inplen, max_length = 0; |
| 590 | Py_ssize_t ibuflen, obuflen = DEFAULTALLOC, hard_limit; |
| 591 | PyObject *RetVal = NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 592 | Byte *input; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 593 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 594 | if (!PyArg_ParseTuple(args, "s#|n:decompress", &input, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 595 | &inplen, &max_length)) |
| 596 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 597 | if (max_length < 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 598 | PyErr_SetString(PyExc_ValueError, |
| 599 | "max_length must be greater than zero"); |
| 600 | return NULL; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 601 | } else if (max_length == 0) |
| 602 | hard_limit = PY_SSIZE_T_MAX; |
| 603 | else |
| 604 | hard_limit = max_length; |
| 605 | |
| 606 | self->zst.next_in = input; |
| 607 | ibuflen = inplen; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 608 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 609 | /* limit amount of data allocated to max_length */ |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 610 | if (max_length && obuflen > max_length) |
| 611 | obuflen = max_length; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 612 | |
| 613 | ENTER_ZLIB |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 614 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 615 | do { |
| 616 | arrange_input_buffer(&self->zst, &ibuflen); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 617 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 618 | do { |
| 619 | obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal, |
| 620 | obuflen, hard_limit); |
| 621 | if (obuflen == -2) { |
| 622 | if (max_length > 0) { |
| 623 | goto save; |
| 624 | } |
| 625 | PyErr_NoMemory(); |
| 626 | } |
| 627 | if (obuflen < 0) { |
| 628 | goto abort; |
| 629 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 630 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 631 | Py_BEGIN_ALLOW_THREADS |
| 632 | err = inflate(&self->zst, Z_SYNC_FLUSH); |
| 633 | Py_END_ALLOW_THREADS |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 634 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 635 | switch (err) { |
| 636 | case Z_OK: /* fall through */ |
| 637 | case Z_BUF_ERROR: /* fall through */ |
| 638 | case Z_STREAM_END: |
| 639 | break; |
| 640 | default: |
| 641 | goto save; |
| 642 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 643 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 644 | } while (self->zst.avail_out == 0); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 645 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 646 | } while (err != Z_STREAM_END && ibuflen != 0); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 647 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 648 | save: |
| 649 | if (save_unconsumed_input(self, input, inplen, err) < 0) |
| 650 | goto abort; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 651 | |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 652 | /* This is the logical place to call inflateEnd, but the old behaviour of |
| 653 | only calling it on flush() is preserved. */ |
| 654 | |
| 655 | if (err != Z_STREAM_END && err != Z_OK && err != Z_BUF_ERROR) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 656 | /* We will only get Z_BUF_ERROR if the output buffer was full |
| 657 | but there wasn't more output when we tried again, so it is |
| 658 | not an error condition. |
| 659 | */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 660 | zlib_error(self->zst, err, "while decompressing"); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 661 | goto abort; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 662 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 663 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 664 | _PyString_Resize(&RetVal, self->zst.next_out - |
| 665 | (Byte *)PyBytes_AS_STRING(RetVal)); |
| 666 | goto success; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 667 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 668 | abort: |
| 669 | Py_CLEAR(RetVal); |
| 670 | success: |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 671 | LEAVE_ZLIB |
| 672 | |
| 673 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 676 | PyDoc_STRVAR(comp_flush__doc__, |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 677 | "flush( [mode] ) -- Return a string containing any remaining compressed data.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 678 | "\n" |
| 679 | "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] | 680 | "default value used when mode is not specified is Z_FINISH.\n" |
| 681 | "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] | 682 | "calling the flush() method. Otherwise, more data can still be compressed."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 684 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 685 | PyZlib_flush(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 686 | { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 687 | int err; |
| 688 | Py_ssize_t length = DEFAULTALLOC; |
| 689 | PyObject *RetVal = NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 690 | int flushmode = Z_FINISH; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 691 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 692 | if (!PyArg_ParseTuple(args, "|i:flush", &flushmode)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 693 | return NULL; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 694 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 695 | /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in |
| 696 | doing any work at all; just return an empty string. */ |
| 697 | if (flushmode == Z_NO_FLUSH) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 698 | return PyString_FromStringAndSize(NULL, 0); |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 699 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 700 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 701 | ENTER_ZLIB |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 702 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 703 | self->zst.avail_in = 0; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 704 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 705 | do { |
| 706 | length = arrange_output_buffer(&self->zst, &RetVal, length); |
| 707 | if (length < 0) { |
| 708 | Py_CLEAR(RetVal); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 709 | goto error; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 710 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 712 | Py_BEGIN_ALLOW_THREADS |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 713 | err = deflate(&self->zst, flushmode); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 714 | Py_END_ALLOW_THREADS |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 715 | |
| 716 | if (err == Z_STREAM_ERROR) { |
| 717 | zlib_error(self->zst, err, "while flushing"); |
| 718 | Py_CLEAR(RetVal); |
| 719 | goto error; |
| 720 | } |
| 721 | } while (self->zst.avail_out == 0); |
| 722 | assert(self->zst.avail_in == 0); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 723 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 724 | /* 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] | 725 | various data structures. Note we should only get Z_STREAM_END when |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 726 | flushmode is Z_FINISH, but checking both for safety*/ |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 727 | if (err == Z_STREAM_END && flushmode == Z_FINISH) { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 728 | err = deflateEnd(&self->zst); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 729 | if (err != Z_OK) { |
| 730 | zlib_error(self->zst, err, "from deflateEnd()"); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 731 | Py_CLEAR(RetVal); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 732 | goto error; |
| 733 | } |
| 734 | else |
| 735 | self->is_initialised = 0; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 737 | /* We will only get Z_BUF_ERROR if the output buffer was full |
| 738 | but there wasn't more output when we tried again, so it is |
| 739 | not an error condition. |
| 740 | */ |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 741 | } else if (err != Z_OK && err != Z_BUF_ERROR) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 742 | zlib_error(self->zst, err, "while flushing"); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 743 | Py_CLEAR(RetVal); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 744 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 745 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 746 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 747 | _PyString_Resize(&RetVal, self->zst.next_out - |
| 748 | (Byte *)PyBytes_AS_STRING(RetVal)); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 749 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 750 | error: |
Tim Peters | b1a37c0 | 2001-10-17 03:56:45 +0000 | [diff] [blame] | 751 | LEAVE_ZLIB |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 752 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Neal Norwitz | 6e73aaa | 2006-06-12 03:33:09 +0000 | [diff] [blame] | 755 | #ifdef HAVE_ZLIB_COPY |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 756 | PyDoc_STRVAR(comp_copy__doc__, |
| 757 | "copy() -- Return a copy of the compression object."); |
| 758 | |
| 759 | static PyObject * |
| 760 | PyZlib_copy(compobject *self) |
| 761 | { |
| 762 | compobject *retval = NULL; |
| 763 | int err; |
| 764 | |
| 765 | retval = newcompobject(&Comptype); |
| 766 | if (!retval) return NULL; |
| 767 | |
| 768 | /* Copy the zstream state |
| 769 | * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe |
| 770 | */ |
| 771 | ENTER_ZLIB |
| 772 | err = deflateCopy(&retval->zst, &self->zst); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 773 | switch (err) { |
| 774 | case Z_OK: |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 775 | break; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 776 | case Z_STREAM_ERROR: |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 777 | PyErr_SetString(PyExc_ValueError, "Inconsistent stream state"); |
| 778 | goto error; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 779 | case Z_MEM_ERROR: |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 780 | PyErr_SetString(PyExc_MemoryError, |
| 781 | "Can't allocate memory for compression object"); |
| 782 | goto error; |
| 783 | default: |
| 784 | zlib_error(self->zst, err, "while copying compression object"); |
| 785 | goto error; |
| 786 | } |
| 787 | |
Tim Peters | 402cc24 | 2006-05-17 01:30:11 +0000 | [diff] [blame] | 788 | Py_INCREF(self->unused_data); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 789 | Py_XSETREF(retval->unused_data, self->unused_data); |
Tim Peters | 402cc24 | 2006-05-17 01:30:11 +0000 | [diff] [blame] | 790 | Py_INCREF(self->unconsumed_tail); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 791 | Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail); |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 792 | |
| 793 | /* Mark it as being initialized */ |
| 794 | retval->is_initialised = 1; |
| 795 | |
| 796 | LEAVE_ZLIB |
| 797 | return (PyObject *)retval; |
| 798 | |
| 799 | error: |
| 800 | LEAVE_ZLIB |
Tim Peters | 402cc24 | 2006-05-17 01:30:11 +0000 | [diff] [blame] | 801 | Py_XDECREF(retval); |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 802 | return NULL; |
| 803 | } |
| 804 | |
| 805 | PyDoc_STRVAR(decomp_copy__doc__, |
| 806 | "copy() -- Return a copy of the decompression object."); |
| 807 | |
| 808 | static PyObject * |
| 809 | PyZlib_uncopy(compobject *self) |
| 810 | { |
| 811 | compobject *retval = NULL; |
| 812 | int err; |
| 813 | |
| 814 | retval = newcompobject(&Decomptype); |
| 815 | if (!retval) return NULL; |
| 816 | |
| 817 | /* Copy the zstream state |
| 818 | * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe |
| 819 | */ |
| 820 | ENTER_ZLIB |
| 821 | err = inflateCopy(&retval->zst, &self->zst); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 822 | switch (err) { |
| 823 | case Z_OK: |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 824 | break; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 825 | case Z_STREAM_ERROR: |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 826 | PyErr_SetString(PyExc_ValueError, "Inconsistent stream state"); |
| 827 | goto error; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 828 | case Z_MEM_ERROR: |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 829 | PyErr_SetString(PyExc_MemoryError, |
| 830 | "Can't allocate memory for decompression object"); |
| 831 | goto error; |
| 832 | default: |
| 833 | zlib_error(self->zst, err, "while copying decompression object"); |
| 834 | goto error; |
| 835 | } |
| 836 | |
Tim Peters | 402cc24 | 2006-05-17 01:30:11 +0000 | [diff] [blame] | 837 | Py_INCREF(self->unused_data); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 838 | Py_XSETREF(retval->unused_data, self->unused_data); |
Tim Peters | 402cc24 | 2006-05-17 01:30:11 +0000 | [diff] [blame] | 839 | Py_INCREF(self->unconsumed_tail); |
Serhiy Storchaka | bc62af1 | 2016-04-06 09:51:18 +0300 | [diff] [blame] | 840 | Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail); |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 841 | |
| 842 | /* Mark it as being initialized */ |
| 843 | retval->is_initialised = 1; |
| 844 | |
| 845 | LEAVE_ZLIB |
| 846 | return (PyObject *)retval; |
| 847 | |
| 848 | error: |
| 849 | LEAVE_ZLIB |
| 850 | Py_XDECREF(retval); |
| 851 | return NULL; |
| 852 | } |
Neal Norwitz | 6e73aaa | 2006-06-12 03:33:09 +0000 | [diff] [blame] | 853 | #endif |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 854 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 855 | PyDoc_STRVAR(decomp_flush__doc__, |
Georg Brandl | 22a9dc8 | 2006-04-01 07:39:41 +0000 | [diff] [blame] | 856 | "flush( [length] ) -- Return a string containing any remaining\n" |
| 857 | "decompressed data. length, if given, is the initial size of the\n" |
| 858 | "output buffer.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 859 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 860 | "The decompressor object can no longer be used after this call."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 861 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 862 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 863 | PyZlib_unflush(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 864 | { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 865 | Py_ssize_t length = DEFAULTALLOC; |
| 866 | int err, flush; |
| 867 | PyObject *RetVal = NULL; |
| 868 | Py_ssize_t ibuflen; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 869 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 870 | if (!PyArg_ParseTuple(args, "|n:flush", &length)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 871 | return NULL; |
Gregory P. Smith | 79e42a0 | 2008-04-09 00:25:17 +0000 | [diff] [blame] | 872 | if (length <= 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 873 | PyErr_SetString(PyExc_ValueError, "length must be greater than zero"); |
| 874 | return NULL; |
Gregory P. Smith | 79e42a0 | 2008-04-09 00:25:17 +0000 | [diff] [blame] | 875 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 876 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 877 | ENTER_ZLIB |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 878 | |
Nadeem Vawda | acfdfda | 2012-11-11 03:28:21 +0100 | [diff] [blame] | 879 | self->zst.next_in = (Byte *)PyString_AS_STRING(self->unconsumed_tail); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 880 | ibuflen = PyString_GET_SIZE(self->unconsumed_tail); |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 881 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 882 | do { |
| 883 | arrange_input_buffer(&self->zst, &ibuflen); |
| 884 | flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH; |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 885 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 886 | do { |
| 887 | length = arrange_output_buffer(&self->zst, &RetVal, length); |
| 888 | if (length < 0) |
| 889 | goto abort; |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 890 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 891 | Py_BEGIN_ALLOW_THREADS |
| 892 | err = inflate(&self->zst, flush); |
| 893 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 894 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 895 | switch (err) { |
| 896 | case Z_OK: /* fall through */ |
| 897 | case Z_BUF_ERROR: /* fall through */ |
| 898 | case Z_STREAM_END: |
| 899 | break; |
| 900 | default: |
| 901 | goto save; |
| 902 | } |
| 903 | |
| 904 | } while (self->zst.avail_out == 0); |
| 905 | |
| 906 | } while (err != Z_STREAM_END && ibuflen != 0); |
| 907 | |
| 908 | save: |
| 909 | if (save_unconsumed_input(self, |
| 910 | (Byte *)PyString_AS_STRING(self->unconsumed_tail), |
| 911 | PyString_GET_SIZE(self->unconsumed_tail), err) < 0) |
| 912 | goto abort; |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 913 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 914 | /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free |
| 915 | various data structures. Note we should only get Z_STREAM_END when |
| 916 | flushmode is Z_FINISH */ |
| 917 | if (err == Z_STREAM_END) { |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 918 | err = inflateEnd(&self->zst); |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 919 | self->is_initialised = 0; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 920 | if (err != Z_OK) { |
| 921 | zlib_error(self->zst, err, "from inflateEnd()"); |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 922 | goto abort; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 923 | } |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 924 | } |
Nadeem Vawda | 252f4dc | 2012-11-11 02:14:15 +0100 | [diff] [blame] | 925 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 926 | _PyString_Resize(&RetVal, self->zst.next_out - |
| 927 | (Byte *)PyBytes_AS_STRING(RetVal)); |
| 928 | goto success; |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 929 | |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 930 | abort: |
| 931 | Py_CLEAR(RetVal); |
| 932 | success: |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 933 | LEAVE_ZLIB |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 934 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static PyMethodDef comp_methods[] = |
| 938 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 939 | {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 940 | comp_compress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 941 | {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 942 | comp_flush__doc__}, |
Neal Norwitz | 6e73aaa | 2006-06-12 03:33:09 +0000 | [diff] [blame] | 943 | #ifdef HAVE_ZLIB_COPY |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 944 | {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS, |
| 945 | comp_copy__doc__}, |
Neal Norwitz | 6e73aaa | 2006-06-12 03:33:09 +0000 | [diff] [blame] | 946 | #endif |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 947 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 948 | }; |
| 949 | |
| 950 | static PyMethodDef Decomp_methods[] = |
| 951 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 952 | {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 953 | decomp_decompress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 954 | {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 955 | decomp_flush__doc__}, |
Neal Norwitz | 6e73aaa | 2006-06-12 03:33:09 +0000 | [diff] [blame] | 956 | #ifdef HAVE_ZLIB_COPY |
Georg Brandl | 8d3342b | 2006-05-16 07:38:27 +0000 | [diff] [blame] | 957 | {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS, |
| 958 | decomp_copy__doc__}, |
Neal Norwitz | 6e73aaa | 2006-06-12 03:33:09 +0000 | [diff] [blame] | 959 | #endif |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 960 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 961 | }; |
| 962 | |
| 963 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 964 | Comp_getattr(compobject *self, char *name) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 965 | { |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 966 | /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch |
| 967 | internal data. */ |
| 968 | |
| 969 | return Py_FindMethod(comp_methods, (PyObject *)self, name); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 973 | Decomp_getattr(compobject *self, char *name) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 974 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 975 | PyObject * retval; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 976 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 977 | ENTER_ZLIB |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 978 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 979 | if (strcmp(name, "unused_data") == 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 980 | Py_INCREF(self->unused_data); |
| 981 | retval = self->unused_data; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 982 | } else if (strcmp(name, "unconsumed_tail") == 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 983 | Py_INCREF(self->unconsumed_tail); |
| 984 | retval = self->unconsumed_tail; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 985 | } else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 986 | retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 987 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 988 | LEAVE_ZLIB |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 989 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 990 | return retval; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 993 | PyDoc_STRVAR(adler32__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 994 | "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n" |
| 995 | "\n" |
| 996 | "An optional starting value can be specified. The returned checksum is\n" |
Gregory P. Smith | f48f9d3 | 2008-03-17 18:48:05 +0000 | [diff] [blame] | 997 | "a signed integer."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 998 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 999 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 1000 | PyZlib_adler32(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1001 | { |
Gregory P. Smith | 1fa588e | 2008-03-25 07:31:28 +0000 | [diff] [blame] | 1002 | unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */ |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 1003 | Byte *buf; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 1004 | Py_ssize_t len; |
| 1005 | int signed_val; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1006 | |
Gregory P. Smith | 1fa588e | 2008-03-25 07:31:28 +0000 | [diff] [blame] | 1007 | if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1008 | return NULL; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 1009 | |
| 1010 | /* Avoid truncation of length for very large buffers. adler32() takes |
| 1011 | length as an unsigned int, which may be narrower than Py_ssize_t. */ |
| 1012 | while ((size_t)len > UINT_MAX) { |
| 1013 | adler32val = adler32(adler32val, buf, UINT_MAX); |
| 1014 | buf += (size_t) UINT_MAX; |
| 1015 | len -= (size_t) UINT_MAX; |
| 1016 | } |
Gregory P. Smith | f48f9d3 | 2008-03-17 18:48:05 +0000 | [diff] [blame] | 1017 | /* In Python 2.x we return a signed integer regardless of native platform |
| 1018 | * long size (the 32bit unsigned long is treated as 32-bit signed and sign |
| 1019 | * extended into a 64-bit long inside the integer object). 3.0 does the |
| 1020 | * right thing and returns unsigned. http://bugs.python.org/issue1202 */ |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 1021 | signed_val = adler32(adler32val, buf, (unsigned int)len); |
Gregory P. Smith | f48f9d3 | 2008-03-17 18:48:05 +0000 | [diff] [blame] | 1022 | return PyInt_FromLong(signed_val); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1023 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1024 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1025 | PyDoc_STRVAR(crc32__doc__, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1026 | "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n" |
| 1027 | "\n" |
| 1028 | "An optional starting value can be specified. The returned checksum is\n" |
Gregory P. Smith | f48f9d3 | 2008-03-17 18:48:05 +0000 | [diff] [blame] | 1029 | "a signed integer."); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1030 | |
| 1031 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 1032 | PyZlib_crc32(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1033 | { |
Gregory P. Smith | 1fa588e | 2008-03-25 07:31:28 +0000 | [diff] [blame] | 1034 | unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */ |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 1035 | Byte *buf; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 1036 | Py_ssize_t len; |
| 1037 | int signed_val; |
Gregory P. Smith | f48f9d3 | 2008-03-17 18:48:05 +0000 | [diff] [blame] | 1038 | |
Gregory P. Smith | 1fa588e | 2008-03-25 07:31:28 +0000 | [diff] [blame] | 1039 | if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1040 | return NULL; |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 1041 | |
| 1042 | /* Avoid truncation of length for very large buffers. crc32() takes |
| 1043 | length as an unsigned int, which may be narrower than Py_ssize_t. */ |
| 1044 | while ((size_t)len > UINT_MAX) { |
| 1045 | crc32val = crc32(crc32val, buf, UINT_MAX); |
| 1046 | buf += (size_t) UINT_MAX; |
| 1047 | len -= (size_t) UINT_MAX; |
| 1048 | } |
Gregory P. Smith | f48f9d3 | 2008-03-17 18:48:05 +0000 | [diff] [blame] | 1049 | /* In Python 2.x we return a signed integer regardless of native platform |
| 1050 | * long size (the 32bit unsigned long is treated as 32-bit signed and sign |
| 1051 | * extended into a 64-bit long inside the integer object). 3.0 does the |
| 1052 | * right thing and returns unsigned. http://bugs.python.org/issue1202 */ |
Martin Panter | 1e411c5 | 2016-07-23 04:22:09 +0000 | [diff] [blame] | 1053 | signed_val = crc32(crc32val, buf, (unsigned int)len); |
Gregory P. Smith | f48f9d3 | 2008-03-17 18:48:05 +0000 | [diff] [blame] | 1054 | return PyInt_FromLong(signed_val); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1055 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1056 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1057 | |
| 1058 | static PyMethodDef zlib_methods[] = |
| 1059 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1060 | {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1061 | adler32__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1062 | {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1063 | compress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1064 | {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1065 | compressobj__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1066 | {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS, |
| 1067 | crc32__doc__}, |
| 1068 | {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1069 | decompress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1070 | {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1071 | decompressobj__doc__}, |
| 1072 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1073 | }; |
| 1074 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 1075 | static PyTypeObject Comptype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1076 | PyVarObject_HEAD_INIT(0, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1077 | "zlib.Compress", |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1078 | sizeof(compobject), |
| 1079 | 0, |
| 1080 | (destructor)Comp_dealloc, /*tp_dealloc*/ |
| 1081 | 0, /*tp_print*/ |
| 1082 | (getattrfunc)Comp_getattr, /*tp_getattr*/ |
| 1083 | 0, /*tp_setattr*/ |
| 1084 | 0, /*tp_compare*/ |
| 1085 | 0, /*tp_repr*/ |
| 1086 | 0, /*tp_as_number*/ |
| 1087 | 0, /*tp_as_sequence*/ |
| 1088 | 0, /*tp_as_mapping*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1089 | }; |
| 1090 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 1091 | static PyTypeObject Decomptype = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1092 | PyVarObject_HEAD_INIT(0, 0) |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 1093 | "zlib.Decompress", |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1094 | sizeof(compobject), |
| 1095 | 0, |
| 1096 | (destructor)Decomp_dealloc, /*tp_dealloc*/ |
| 1097 | 0, /*tp_print*/ |
| 1098 | (getattrfunc)Decomp_getattr, /*tp_getattr*/ |
| 1099 | 0, /*tp_setattr*/ |
| 1100 | 0, /*tp_compare*/ |
| 1101 | 0, /*tp_repr*/ |
| 1102 | 0, /*tp_as_number*/ |
| 1103 | 0, /*tp_as_sequence*/ |
| 1104 | 0, /*tp_as_mapping*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1105 | }; |
| 1106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1107 | PyDoc_STRVAR(zlib_module_documentation, |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1108 | "The functions in this module allow compression and decompression using the\n" |
| 1109 | "zlib library, which is based on GNU zip.\n" |
| 1110 | "\n" |
| 1111 | "adler32(string[, start]) -- Compute an Adler-32 checksum.\n" |
Nadeem Vawda | 99f9b8d | 2012-11-11 14:01:23 +0100 | [diff] [blame] | 1112 | "compress(string[, level]) -- Compress string, with compression level in 0-9.\n" |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 1113 | "compressobj([level]) -- Return a compressor object.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1114 | "crc32(string[, start]) -- Compute a CRC-32 checksum.\n" |
Andrew M. Kuchling | 313a3e3 | 1999-12-20 22:13:38 +0000 | [diff] [blame] | 1115 | "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1116 | "decompressobj([wbits]) -- Return a decompressor object.\n" |
| 1117 | "\n" |
Martin Panter | 9c946bb | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 1118 | "'wbits' is window buffer size and container format.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 1119 | "Compressor objects support compress() and flush() methods; decompressor\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1120 | "objects support decompress() and flush()."); |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 1121 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1122 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1123 | PyInit_zlib(void) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1124 | { |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1125 | PyObject *m, *ver; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1126 | Py_TYPE(&Comptype) = &PyType_Type; |
| 1127 | Py_TYPE(&Decomptype) = &PyType_Type; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1128 | m = Py_InitModule4("zlib", zlib_methods, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1129 | zlib_module_documentation, |
| 1130 | (PyObject*)NULL,PYTHON_API_VERSION); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1131 | if (m == NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1132 | return; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 1133 | |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1134 | ZlibError = PyErr_NewException("zlib.error", NULL, NULL); |
| 1135 | if (ZlibError != NULL) { |
| 1136 | Py_INCREF(ZlibError); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1137 | PyModule_AddObject(m, "error", ZlibError); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1138 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1139 | PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS); |
| 1140 | PyModule_AddIntConstant(m, "DEFLATED", DEFLATED); |
| 1141 | PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); |
| 1142 | PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED); |
| 1143 | PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION); |
| 1144 | PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION); |
| 1145 | PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED); |
| 1146 | PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY); |
| 1147 | PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1148 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1149 | PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH); |
| 1150 | PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH); |
| 1151 | PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH); |
| 1152 | PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 1153 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1154 | ver = PyString_FromString(ZLIB_VERSION); |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 1155 | if (ver != NULL) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1156 | PyModule_AddObject(m, "ZLIB_VERSION", ver); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 1157 | |
Guido van Rossum | 7d9ea50 | 2003-02-03 20:45:52 +0000 | [diff] [blame] | 1158 | PyModule_AddStringConstant(m, "__version__", "1.0"); |
| 1159 | |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 1160 | #ifdef WITH_THREAD |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 1161 | zlib_lock = PyThread_allocate_lock(); |
Sjoerd Mullender | 556a938 | 2002-03-11 09:20:47 +0000 | [diff] [blame] | 1162 | #endif /* WITH_THREAD */ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1163 | } |