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