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 \ |
Tim Peters | 6605c64 | 2001-10-17 03:43:54 +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 \ |
Tim Peters | 6605c64 | 2001-10-17 03:43:54 +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 | |
| 58 | staticforward PyTypeObject Comptype; |
| 59 | staticforward PyTypeObject Decomptype; |
| 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 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 81 | static char compressobj__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 82 | "compressobj([level]) -- Return a compressor object.\n" |
| 83 | "\n" |
| 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 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 86 | static char decompressobj__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 87 | "decompressobj([wbits]) -- Return a decompressor object.\n" |
| 88 | "\n" |
| 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; |
| 99 | self->unused_data = PyString_FromString(""); |
| 100 | if (self->unused_data == NULL) { |
| 101 | Py_DECREF(self); |
| 102 | return NULL; |
| 103 | } |
| 104 | self->unconsumed_tail = PyString_FromString(""); |
| 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 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 112 | static char compress__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 113 | "compress(string[, level]) -- Returned compressed string.\n" |
| 114 | "\n" |
| 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) |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 177 | ReturnVal = PyString_FromStringAndSize((char *)output, |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +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 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 188 | static char 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" |
| 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; |
| 200 | int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC; |
| 201 | z_stream zst; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 202 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 203 | if (!PyArg_ParseTuple(args, "s#|ii:decompress", |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 204 | &input, &length, &wsize, &r_strlen)) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 205 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 206 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 207 | if (r_strlen <= 0) |
| 208 | r_strlen = 1; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 209 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 210 | zst.avail_in = length; |
| 211 | zst.avail_out = r_strlen; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 212 | |
Jeremy Hylton | 9bc9d66 | 2001-10-16 21:23:58 +0000 | [diff] [blame] | 213 | if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen))) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 214 | return NULL; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 215 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 216 | zst.zalloc = (alloc_func)NULL; |
| 217 | zst.zfree = (free_func)Z_NULL; |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 218 | zst.next_out = (Byte *)PyString_AS_STRING(result_str); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 219 | zst.next_in = (Byte *)input; |
| 220 | err = inflateInit2(&zst, wsize); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 221 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 222 | switch(err) { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 223 | case(Z_OK): |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 224 | break; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 225 | case(Z_MEM_ERROR): |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 226 | PyErr_SetString(PyExc_MemoryError, |
| 227 | "Out of memory while decompressing data"); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 228 | goto error; |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 229 | default: |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 230 | inflateEnd(&zst); |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 231 | zlib_error(zst, err, "while preparing to decompress data"); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 232 | goto error; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 233 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 234 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 235 | do { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 236 | Py_BEGIN_ALLOW_THREADS |
| 237 | err=inflate(&zst, Z_FINISH); |
| 238 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 239 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 240 | switch(err) { |
| 241 | case(Z_STREAM_END): |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 242 | break; |
Guido van Rossum | 115f517 | 1998-04-23 20:22:11 +0000 | [diff] [blame] | 243 | case(Z_BUF_ERROR): |
Andrew M. Kuchling | d923831 | 2000-10-09 14:18:10 +0000 | [diff] [blame] | 244 | /* |
| 245 | * If there is at least 1 byte of room according to zst.avail_out |
| 246 | * and we get this error, assume that it means zlib cannot |
| 247 | * process the inflate call() due to an error in the data. |
| 248 | */ |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 249 | if (zst.avail_out > 0) { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 250 | PyErr_Format(ZlibError, "Error %i while decompressing data", |
| 251 | err); |
| 252 | inflateEnd(&zst); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 253 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 254 | } |
Andrew M. Kuchling | d923831 | 2000-10-09 14:18:10 +0000 | [diff] [blame] | 255 | /* fall through */ |
| 256 | case(Z_OK): |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 257 | /* need more memory */ |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 258 | if (_PyString_Resize(&result_str, r_strlen << 1) == -1) { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 259 | inflateEnd(&zst); |
| 260 | result_str = NULL; |
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 | } |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 263 | zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \ |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 264 | + r_strlen; |
| 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 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +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, |
| 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) |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 302 | return(NULL); |
| 303 | self->zst.zalloc = (alloc_func)NULL; |
| 304 | self->zst.zfree = (free_func)Z_NULL; |
| 305 | err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); |
| 306 | switch(err) { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 307 | case (Z_OK): |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 308 | self->is_initialised = 1; |
| 309 | return (PyObject*)self; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 310 | case (Z_MEM_ERROR): |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 311 | Py_DECREF(self); |
| 312 | PyErr_SetString(PyExc_MemoryError, |
| 313 | "Can't allocate memory for compression object"); |
| 314 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 315 | case(Z_STREAM_ERROR): |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 316 | Py_DECREF(self); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 317 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 318 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 319 | default: |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 320 | zlib_error(self->zst, err, "while creating compression object"); |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 321 | Py_DECREF(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 322 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 327 | PyZlib_decompressobj(PyObject *selfptr, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 328 | { |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 329 | int wbits=DEF_WBITS, err; |
| 330 | compobject *self; |
| 331 | if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits)) |
| 332 | return NULL; |
| 333 | |
| 334 | self = newcompobject(&Decomptype); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 335 | if (self == NULL) |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 336 | return(NULL); |
| 337 | self->zst.zalloc = (alloc_func)NULL; |
| 338 | self->zst.zfree = (free_func)Z_NULL; |
| 339 | err = inflateInit2(&self->zst, wbits); |
| 340 | switch(err) { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 341 | case (Z_OK): |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 342 | self->is_initialised = 1; |
| 343 | return (PyObject*)self; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 344 | case(Z_STREAM_ERROR): |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 345 | Py_DECREF(self); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 346 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 347 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 348 | case (Z_MEM_ERROR): |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 349 | Py_DECREF(self); |
| 350 | PyErr_SetString(PyExc_MemoryError, |
| 351 | "Can't allocate memory for decompression object"); |
| 352 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 353 | default: |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 354 | zlib_error(self->zst, err, "while creating decompression object"); |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 355 | Py_DECREF(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 356 | return NULL; |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 357 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 361 | Comp_dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 362 | { |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 363 | if (self->is_initialised) |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 364 | deflateEnd(&self->zst); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 365 | Py_XDECREF(self->unused_data); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 366 | Py_XDECREF(self->unconsumed_tail); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 367 | PyObject_Del(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 371 | Decomp_dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 372 | { |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 373 | if (self->is_initialised) |
Jeremy Hylton | 49900000 | 2001-10-16 21:59:35 +0000 | [diff] [blame] | 374 | inflateEnd(&self->zst); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 375 | Py_XDECREF(self->unused_data); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 376 | Py_XDECREF(self->unconsumed_tail); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 377 | PyObject_Del(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 380 | static char comp_compress__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 381 | "compress(data) -- Return a string containing data compressed.\n" |
| 382 | "\n" |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 383 | "After calling this function, some of the input data may still\n" |
| 384 | "be stored in internal buffers for later processing.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 385 | "Call the flush() method to clear these buffers."; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 386 | |
| 387 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 388 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 389 | PyZlib_objcompress(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 390 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 391 | int err, inplen, length = DEFAULTALLOC; |
| 392 | PyObject *RetVal; |
| 393 | Byte *input; |
| 394 | unsigned long start_total_out; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 395 | |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 396 | if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen)) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 397 | return NULL; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 398 | |
Jeremy Hylton | 9bc9d66 | 2001-10-16 21:23:58 +0000 | [diff] [blame] | 399 | if (!(RetVal = PyString_FromStringAndSize(NULL, length))) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 400 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 401 | |
| 402 | ENTER_ZLIB |
| 403 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 404 | start_total_out = self->zst.total_out; |
| 405 | self->zst.avail_in = inplen; |
| 406 | self->zst.next_in = input; |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 407 | self->zst.avail_out = length; |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 408 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 409 | |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 410 | Py_BEGIN_ALLOW_THREADS |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 411 | err = deflate(&(self->zst), Z_NO_FLUSH); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 412 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 413 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 414 | /* while Z_OK and the output buffer is full, there might be more output, |
| 415 | so extend the output buffer and try again */ |
| 416 | while (err == Z_OK && self->zst.avail_out == 0) { |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 417 | if (_PyString_Resize(&RetVal, length << 1) == -1) { |
| 418 | RetVal = NULL; |
| 419 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 420 | } |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 421 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \ |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 422 | + length; |
| 423 | self->zst.avail_out = length; |
| 424 | length = length << 1; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 425 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 426 | Py_BEGIN_ALLOW_THREADS |
| 427 | err = deflate(&(self->zst), Z_NO_FLUSH); |
| 428 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 429 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 430 | /* 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] | 431 | 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] | 432 | condition. |
| 433 | */ |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 434 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 435 | if (err != Z_OK && err != Z_BUF_ERROR) { |
| 436 | zlib_error(self->zst, err, "while compressing"); |
| 437 | Py_DECREF(RetVal); |
| 438 | RetVal = NULL; |
| 439 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 440 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 441 | if (_PyString_Resize(&RetVal, |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 442 | self->zst.total_out - start_total_out) < 0) |
| 443 | RetVal = NULL; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 444 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 445 | error: |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 446 | LEAVE_ZLIB |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 447 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 450 | static char decomp_decompress__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 451 | "decompress(data, max_length) -- Return a string containing the decompressed\n" |
| 452 | "version of the data.\n" |
| 453 | "\n" |
| 454 | "After calling this function, some of the input data may still be stored in\n" |
| 455 | "internal buffers for later processing.\n" |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 456 | "Call the flush() method to clear these buffers.\n" |
| 457 | "If the max_length parameter is specified then the return value will be\n" |
| 458 | "no longer than max_length. Unconsumed input data will be stored in\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 459 | "the unconsumed_tail attribute."; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 461 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 462 | PyZlib_objdecompress(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 463 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 464 | int err, inplen, old_length, length = DEFAULTALLOC; |
| 465 | int max_length = 0; |
| 466 | PyObject *RetVal; |
| 467 | Byte *input; |
| 468 | unsigned long start_total_out; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 469 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 470 | if (!PyArg_ParseTuple(args, "s#|i:decompress", &input, |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 471 | &inplen, &max_length)) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 472 | return NULL; |
| 473 | if (max_length < 0) { |
| 474 | PyErr_SetString(PyExc_ValueError, |
| 475 | "max_length must be greater than zero"); |
| 476 | return NULL; |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 477 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 478 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 479 | /* limit amount of data allocated to max_length */ |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 480 | if (max_length && length > max_length) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 481 | length = max_length; |
Jeremy Hylton | 9bc9d66 | 2001-10-16 21:23:58 +0000 | [diff] [blame] | 482 | if (!(RetVal = PyString_FromStringAndSize(NULL, length))) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 483 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 484 | |
| 485 | ENTER_ZLIB |
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 | start_total_out = self->zst.total_out; |
| 488 | self->zst.avail_in = inplen; |
| 489 | self->zst.next_in = input; |
| 490 | self->zst.avail_out = length; |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 491 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal); |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 492 | |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 493 | Py_BEGIN_ALLOW_THREADS |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 494 | err = inflate(&(self->zst), Z_SYNC_FLUSH); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 495 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 496 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 497 | /* While Z_OK and the output buffer is full, there might be more output. |
| 498 | So extend the output buffer and try again. |
| 499 | */ |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 500 | while (err == Z_OK && self->zst.avail_out == 0) { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 501 | /* If max_length set, don't continue decompressing if we've already |
| 502 | reached the limit. |
| 503 | */ |
| 504 | if (max_length && length >= max_length) |
| 505 | break; |
Jeremy Hylton | 511e2ca | 2001-10-16 20:39:49 +0000 | [diff] [blame] | 506 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 507 | /* otherwise, ... */ |
| 508 | old_length = length; |
| 509 | length = length << 1; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 510 | if (max_length && length > max_length) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 511 | length = max_length; |
| 512 | |
| 513 | if (_PyString_Resize(&RetVal, length) == -1) { |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 514 | RetVal = NULL; |
| 515 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 516 | } |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 517 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \ |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 518 | + old_length; |
| 519 | self->zst.avail_out = length - old_length; |
| 520 | |
| 521 | Py_BEGIN_ALLOW_THREADS |
| 522 | err = inflate(&(self->zst), Z_SYNC_FLUSH); |
| 523 | Py_END_ALLOW_THREADS |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 524 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 525 | |
| 526 | /* Not all of the compressed data could be accomodated in the output buffer |
| 527 | of specified size. Return the unconsumed tail in an attribute.*/ |
| 528 | if(max_length) { |
| 529 | Py_DECREF(self->unconsumed_tail); |
Jack Jansen | 72af01a | 2001-10-23 22:29:06 +0000 | [diff] [blame] | 530 | self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 531 | self->zst.avail_in); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 532 | if(!self->unconsumed_tail) { |
| 533 | Py_DECREF(RetVal); |
| 534 | RetVal = NULL; |
| 535 | goto error; |
| 536 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 537 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 538 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 539 | /* The end of the compressed data has been reached, so set the |
| 540 | unused_data attribute to a string containing the remainder of the |
| 541 | 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] | 542 | inflateEnd, but the old behaviour of only calling it on flush() is |
| 543 | preserved. |
| 544 | */ |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 545 | if (err == Z_STREAM_END) { |
| 546 | Py_XDECREF(self->unused_data); /* Free original empty string */ |
| 547 | self->unused_data = PyString_FromStringAndSize( |
| 548 | (char *)self->zst.next_in, self->zst.avail_in); |
| 549 | if (self->unused_data == NULL) { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 550 | Py_DECREF(RetVal); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 551 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 552 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 553 | /* 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] | 554 | but there wasn't more output when we tried again, so it is |
| 555 | not an error condition. |
| 556 | */ |
| 557 | } else if (err != Z_OK && err != Z_BUF_ERROR) { |
| 558 | zlib_error(self->zst, err, "while decompressing"); |
| 559 | Py_DECREF(RetVal); |
| 560 | RetVal = NULL; |
| 561 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 562 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 563 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 564 | if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) |
| 565 | RetVal = NULL; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 566 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 567 | error: |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 568 | LEAVE_ZLIB |
| 569 | |
| 570 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 573 | static char comp_flush__doc__[] = |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 574 | "flush( [mode] ) -- Return a string containing any remaining compressed data.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 575 | "\n" |
| 576 | "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] | 577 | "default value used when mode is not specified is Z_FINISH.\n" |
| 578 | "If mode == Z_FINISH, the compressor object can no longer be used after\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 579 | "calling the flush() method. Otherwise, more data can still be compressed.\n"; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 581 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 582 | PyZlib_flush(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 583 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 584 | int err, length = DEFAULTALLOC; |
| 585 | PyObject *RetVal; |
| 586 | int flushmode = Z_FINISH; |
| 587 | unsigned long start_total_out; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 588 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 589 | if (!PyArg_ParseTuple(args, "|i:flush", &flushmode)) |
| 590 | return NULL; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 591 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 592 | /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in |
| 593 | doing any work at all; just return an empty string. */ |
| 594 | if (flushmode == Z_NO_FLUSH) { |
| 595 | return PyString_FromStringAndSize(NULL, 0); |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 596 | } |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 597 | |
Jeremy Hylton | 9bc9d66 | 2001-10-16 21:23:58 +0000 | [diff] [blame] | 598 | if (!(RetVal = PyString_FromStringAndSize(NULL, length))) |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 599 | return NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 600 | |
| 601 | ENTER_ZLIB |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 602 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 603 | start_total_out = self->zst.total_out; |
| 604 | self->zst.avail_in = 0; |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 605 | self->zst.avail_out = length; |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 606 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 607 | |
| 608 | Py_BEGIN_ALLOW_THREADS |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 609 | err = deflate(&(self->zst), flushmode); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 610 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 611 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 612 | /* while Z_OK and the output buffer is full, there might be more output, |
| 613 | so extend the output buffer and try again */ |
| 614 | while (err == Z_OK && self->zst.avail_out == 0) { |
| 615 | if (_PyString_Resize(&RetVal, length << 1) == -1) { |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 616 | RetVal = NULL; |
| 617 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 618 | } |
Jeremy Hylton | ba3dd99 | 2001-10-16 23:26:08 +0000 | [diff] [blame] | 619 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \ |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 620 | + length; |
| 621 | self->zst.avail_out = length; |
| 622 | length = length << 1; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 623 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 624 | Py_BEGIN_ALLOW_THREADS |
| 625 | err = deflate(&(self->zst), flushmode); |
| 626 | Py_END_ALLOW_THREADS |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 627 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 628 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 629 | /* 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] | 630 | various data structures. Note we should only get Z_STREAM_END when |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 631 | flushmode is Z_FINISH, but checking both for safety*/ |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 632 | if (err == Z_STREAM_END && flushmode == Z_FINISH) { |
| 633 | err = deflateEnd(&(self->zst)); |
| 634 | if (err != Z_OK) { |
| 635 | zlib_error(self->zst, err, "from deflateEnd()"); |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 636 | Py_DECREF(RetVal); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 637 | RetVal = NULL; |
| 638 | goto error; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 639 | } |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 640 | else |
| 641 | self->is_initialised = 0; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 642 | |
| 643 | /* We will only get Z_BUF_ERROR if the output buffer was full |
| 644 | 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] | 645 | not an error condition. |
| 646 | */ |
| 647 | } else if (err!=Z_OK && err!=Z_BUF_ERROR) { |
| 648 | zlib_error(self->zst, err, "while flushing"); |
| 649 | Py_DECREF(RetVal); |
| 650 | RetVal = NULL; |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 651 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 652 | |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 653 | if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) |
| 654 | RetVal = NULL; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 655 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 656 | error: |
Tim Peters | b1a37c0 | 2001-10-17 03:56:45 +0000 | [diff] [blame] | 657 | LEAVE_ZLIB |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 658 | |
| 659 | return RetVal; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 662 | static char decomp_flush__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 663 | "flush() -- Return a string containing any remaining decompressed data.\n" |
| 664 | "\n" |
| 665 | "The decompressor object can no longer be used after this call."; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 666 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 667 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 668 | PyZlib_unflush(compobject *self, PyObject *args) |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 669 | /*decompressor flush is a no-op because all pending data would have been |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 670 | flushed by the decompress method. However, this routine previously called |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 671 | inflateEnd, causing any further decompress or flush calls to raise |
Andrew M. Kuchling | 9aff4a2 | 2001-02-21 02:15:56 +0000 | [diff] [blame] | 672 | exceptions. This behaviour has been preserved.*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 673 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 674 | int err; |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 675 | PyObject * retval = NULL; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 676 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 677 | if (!PyArg_ParseTuple(args, "")) |
| 678 | return NULL; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 679 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 680 | ENTER_ZLIB |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 681 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 682 | err = inflateEnd(&(self->zst)); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 683 | if (err != Z_OK) |
Jeremy Hylton | 0965e08 | 2001-10-16 21:56:09 +0000 | [diff] [blame] | 684 | zlib_error(self->zst, err, "from inflateEnd()"); |
Jeremy Hylton | 9d620d0 | 2001-10-16 23:02:32 +0000 | [diff] [blame] | 685 | else { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 686 | self->is_initialised = 0; |
| 687 | retval = PyString_FromStringAndSize(NULL, 0); |
| 688 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 689 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 690 | LEAVE_ZLIB |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 691 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 692 | return retval; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | static PyMethodDef comp_methods[] = |
| 696 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 697 | {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 698 | comp_compress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 699 | {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 700 | comp_flush__doc__}, |
| 701 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 702 | }; |
| 703 | |
| 704 | static PyMethodDef Decomp_methods[] = |
| 705 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 706 | {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 707 | decomp_decompress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 708 | {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 709 | decomp_flush__doc__}, |
| 710 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 714 | Comp_getattr(compobject *self, char *name) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 715 | { |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 716 | /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch |
| 717 | internal data. */ |
| 718 | |
| 719 | return Py_FindMethod(comp_methods, (PyObject *)self, name); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 723 | Decomp_getattr(compobject *self, char *name) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 724 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 725 | PyObject * retval; |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 726 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 727 | ENTER_ZLIB |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 728 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 729 | if (strcmp(name, "unused_data") == 0) { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 730 | Py_INCREF(self->unused_data); |
| 731 | retval = self->unused_data; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 732 | } else if (strcmp(name, "unconsumed_tail") == 0) { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 733 | Py_INCREF(self->unconsumed_tail); |
| 734 | retval = self->unconsumed_tail; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 735 | } else |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 736 | retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 737 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 738 | LEAVE_ZLIB |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 739 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 740 | return retval; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 743 | static char adler32__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 744 | "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n" |
| 745 | "\n" |
| 746 | "An optional starting value can be specified. The returned checksum is\n" |
| 747 | "an integer."; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 748 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 749 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 750 | PyZlib_adler32(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 751 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 752 | uLong adler32val = adler32(0L, Z_NULL, 0); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 753 | Byte *buf; |
| 754 | int len; |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 755 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 756 | if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val)) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 757 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 758 | adler32val = adler32(adler32val, buf, len); |
| 759 | return PyInt_FromLong(adler32val); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 760 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 761 | |
| 762 | static char crc32__doc__[] = |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 763 | "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n" |
| 764 | "\n" |
| 765 | "An optional starting value can be specified. The returned checksum is\n" |
| 766 | "an integer."; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 767 | |
| 768 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 769 | PyZlib_crc32(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 770 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 771 | uLong crc32val = crc32(0L, Z_NULL, 0); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 772 | Byte *buf; |
| 773 | int len; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 774 | if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val)) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 775 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 776 | crc32val = crc32(crc32val, buf, len); |
| 777 | return PyInt_FromLong(crc32val); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 778 | } |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 779 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 780 | |
| 781 | static PyMethodDef zlib_methods[] = |
| 782 | { |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 783 | {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 784 | adler32__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 785 | {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 786 | compress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 787 | {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 788 | compressobj__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 789 | {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS, |
| 790 | crc32__doc__}, |
| 791 | {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 792 | decompress__doc__}, |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 793 | {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS, |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 794 | decompressobj__doc__}, |
| 795 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
| 798 | statichere PyTypeObject Comptype = { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 799 | PyObject_HEAD_INIT(0) |
| 800 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 801 | "zlib.Compress", |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 802 | sizeof(compobject), |
| 803 | 0, |
| 804 | (destructor)Comp_dealloc, /*tp_dealloc*/ |
| 805 | 0, /*tp_print*/ |
| 806 | (getattrfunc)Comp_getattr, /*tp_getattr*/ |
| 807 | 0, /*tp_setattr*/ |
| 808 | 0, /*tp_compare*/ |
| 809 | 0, /*tp_repr*/ |
| 810 | 0, /*tp_as_number*/ |
| 811 | 0, /*tp_as_sequence*/ |
| 812 | 0, /*tp_as_mapping*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 813 | }; |
| 814 | |
| 815 | statichere PyTypeObject Decomptype = { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 816 | PyObject_HEAD_INIT(0) |
| 817 | 0, |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 818 | "zlib.Decompress", |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 819 | sizeof(compobject), |
| 820 | 0, |
| 821 | (destructor)Decomp_dealloc, /*tp_dealloc*/ |
| 822 | 0, /*tp_print*/ |
| 823 | (getattrfunc)Decomp_getattr, /*tp_getattr*/ |
| 824 | 0, /*tp_setattr*/ |
| 825 | 0, /*tp_compare*/ |
| 826 | 0, /*tp_repr*/ |
| 827 | 0, /*tp_as_number*/ |
| 828 | 0, /*tp_as_sequence*/ |
| 829 | 0, /*tp_as_mapping*/ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 830 | }; |
| 831 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 832 | static char zlib_module_documentation[]= |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 833 | "The functions in this module allow compression and decompression using the\n" |
| 834 | "zlib library, which is based on GNU zip.\n" |
| 835 | "\n" |
| 836 | "adler32(string[, start]) -- Compute an Adler-32 checksum.\n" |
| 837 | "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] | 838 | "compressobj([level]) -- Return a compressor object.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 839 | "crc32(string[, start]) -- Compute a CRC-32 checksum.\n" |
Andrew M. Kuchling | 313a3e3 | 1999-12-20 22:13:38 +0000 | [diff] [blame] | 840 | "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n" |
Tim Peters | adbd35b | 2001-10-17 04:16:15 +0000 | [diff] [blame] | 841 | "decompressobj([wbits]) -- Return a decompressor object.\n" |
| 842 | "\n" |
| 843 | "'wbits' is window buffer size.\n" |
| 844 | "Compressor objects support compress() and flush() methods; decompressor\n" |
| 845 | "objects support decompress() and flush()."; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 847 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 848 | PyInit_zlib(void) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 849 | { |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 850 | PyObject *m, *d, *ver; |
| 851 | Comptype.ob_type = &PyType_Type; |
| 852 | Decomptype.ob_type = &PyType_Type; |
| 853 | m = Py_InitModule4("zlib", zlib_methods, |
| 854 | zlib_module_documentation, |
| 855 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 856 | d = PyModule_GetDict(m); |
| 857 | ZlibError = PyErr_NewException("zlib.error", NULL, NULL); |
| 858 | if (ZlibError != NULL) |
| 859 | PyDict_SetItemString(d, "error", ZlibError); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 860 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 861 | PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS); |
| 862 | PyModule_AddIntConstant(m, "DEFLATED", DEFLATED); |
| 863 | PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); |
| 864 | PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED); |
| 865 | PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION); |
| 866 | PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION); |
| 867 | PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED); |
| 868 | PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY); |
| 869 | PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 870 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 871 | PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH); |
| 872 | PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH); |
| 873 | PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH); |
| 874 | PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH); |
Tim Peters | 977e540 | 2001-10-17 03:57:20 +0000 | [diff] [blame] | 875 | |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 876 | ver = PyString_FromString(ZLIB_VERSION); |
| 877 | if (ver != NULL) { |
| 878 | PyDict_SetItemString(d, "ZLIB_VERSION", ver); |
| 879 | Py_DECREF(ver); |
| 880 | } |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 881 | |
| 882 | #ifdef WITH_THREAD |
Jeremy Hylton | 9714f99 | 2001-10-16 21:19:45 +0000 | [diff] [blame] | 883 | zlib_lock = PyThread_allocate_lock(); |
Martin v. Löwis | 3bd8c1e | 2001-09-07 16:27:31 +0000 | [diff] [blame] | 884 | #endif // WITH_THREAD |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 885 | } |