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