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