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