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