Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1 | /* zlibmodule.c -- gzip-compatible data compression */ |
Guido van Rossum | 821a5e4 | 1998-05-08 14:56:29 +0000 | [diff] [blame] | 2 | /* See http://www.cdrom.com/pub/infozip/zlib/ */ |
| 3 | /* See http://www.winimage.com/zLibDll for Windows */ |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 5 | #include "Python.h" |
| 6 | #include "zlib.h" |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 7 | |
| 8 | /* The following parameters are copied from zutil.h, version 0.95 */ |
| 9 | #define DEFLATED 8 |
| 10 | #if MAX_MEM_LEVEL >= 8 |
| 11 | # define DEF_MEM_LEVEL 8 |
| 12 | #else |
| 13 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL |
| 14 | #endif |
| 15 | #define DEF_WBITS MAX_WBITS |
| 16 | |
Guido van Rossum | b729a1d | 1999-04-07 20:23:17 +0000 | [diff] [blame] | 17 | /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */ |
| 18 | #define DEFAULTALLOC (16*1024) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 19 | #define PyInit_zlib initzlib |
| 20 | |
| 21 | staticforward PyTypeObject Comptype; |
| 22 | staticforward PyTypeObject Decomptype; |
| 23 | |
| 24 | static PyObject *ZlibError; |
| 25 | |
| 26 | typedef struct |
| 27 | { |
| 28 | PyObject_HEAD |
| 29 | z_stream zst; |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 30 | PyObject *unused_data; |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 31 | int is_initialised; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 32 | } compobject; |
| 33 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 34 | static char compressobj__doc__[] = |
| 35 | "compressobj() -- Return a compressor object.\n" |
| 36 | "compressobj(level) -- Return a compressor object, using the given compression level.\n" |
| 37 | ; |
| 38 | |
| 39 | static char decompressobj__doc__[] = |
| 40 | "decompressobj() -- Return a decompressor object.\n" |
| 41 | "decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n" |
| 42 | ; |
| 43 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 44 | static compobject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 45 | newcompobject(PyTypeObject *type) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 46 | { |
| 47 | compobject *self; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 48 | self = PyObject_New(compobject, type); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 49 | if (self == NULL) |
| 50 | return NULL; |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 51 | self->is_initialised = 0; |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 52 | self->unused_data = PyString_FromString(""); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 53 | return self; |
| 54 | } |
| 55 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 56 | static char compress__doc__[] = |
| 57 | "compress(string) -- Compress string using the default compression level, " |
| 58 | "returning a string containing compressed data.\n" |
| 59 | "compress(string, level) -- Compress string, using the chosen compression " |
| 60 | "level (from 1 to 9). Return a string containing the compressed data.\n" |
| 61 | ; |
| 62 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 63 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 64 | PyZlib_compress(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 65 | { |
| 66 | PyObject *ReturnVal; |
| 67 | Byte *input, *output; |
| 68 | int length, level=Z_DEFAULT_COMPRESSION, err; |
| 69 | z_stream zst; |
| 70 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 71 | if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level)) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 72 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 73 | zst.avail_out = length + length/1000 + 12 + 1; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 74 | output=(Byte*)malloc(zst.avail_out); |
| 75 | if (output==NULL) |
| 76 | { |
| 77 | PyErr_SetString(PyExc_MemoryError, |
| 78 | "Can't allocate memory to compress data"); |
| 79 | return NULL; |
| 80 | } |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 81 | |
Jeremy Hylton | 5bc4abe | 1998-12-21 17:15:00 +0000 | [diff] [blame] | 82 | zst.zalloc=(alloc_func)NULL; |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 83 | zst.zfree=(free_func)Z_NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 84 | zst.next_out=(Byte *)output; |
| 85 | zst.next_in =(Byte *)input; |
| 86 | zst.avail_in=length; |
| 87 | err=deflateInit(&zst, level); |
| 88 | switch(err) |
| 89 | { |
| 90 | case(Z_OK): |
| 91 | break; |
| 92 | case(Z_MEM_ERROR): |
| 93 | PyErr_SetString(PyExc_MemoryError, |
| 94 | "Out of memory while compressing data"); |
| 95 | free(output); |
| 96 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 97 | case(Z_STREAM_ERROR): |
| 98 | PyErr_SetString(ZlibError, |
| 99 | "Bad compression level"); |
| 100 | free(output); |
| 101 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 102 | default: |
| 103 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 104 | if (zst.msg == Z_NULL) |
| 105 | PyErr_Format(ZlibError, "Error %i while compressing data", |
| 106 | err); |
| 107 | else |
| 108 | PyErr_Format(ZlibError, "Error %i while compressing data: %.200s", |
| 109 | err, zst.msg); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 110 | deflateEnd(&zst); |
| 111 | free(output); |
| 112 | return NULL; |
| 113 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | err=deflate(&zst, Z_FINISH); |
| 117 | switch(err) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 118 | { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 119 | case(Z_STREAM_END): |
| 120 | break; |
| 121 | /* Are there other errors to be trapped here? */ |
| 122 | default: |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 123 | { |
| 124 | if (zst.msg == Z_NULL) |
| 125 | PyErr_Format(ZlibError, "Error %i while compressing data", |
| 126 | err); |
| 127 | else |
| 128 | PyErr_Format(ZlibError, "Error %i while compressing data: %.200s", |
| 129 | err, zst.msg); |
| 130 | deflateEnd(&zst); |
| 131 | free(output); |
| 132 | return NULL; |
| 133 | } |
| 134 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 135 | err=deflateEnd(&zst); |
| 136 | if (err!=Z_OK) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 137 | { |
| 138 | if (zst.msg == Z_NULL) |
| 139 | PyErr_Format(ZlibError, "Error %i while finishing compression", |
| 140 | err); |
| 141 | else |
| 142 | PyErr_Format(ZlibError, |
| 143 | "Error %i while finishing compression: %.200s", |
| 144 | err, zst.msg); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 145 | free(output); |
| 146 | return NULL; |
| 147 | } |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 148 | ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 149 | free(output); |
| 150 | return ReturnVal; |
| 151 | } |
| 152 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 153 | static char decompress__doc__[] = |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 154 | "decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n" |
| 155 | "decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n" |
| 156 | "decompress(string, wbits, bufsize) -- Decompress the data in string with a window buffer size of wbits and an initial output buffer size of bufsize.\n" |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 157 | ; |
| 158 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 159 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 160 | PyZlib_decompress(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 161 | { |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 162 | PyObject *result_str; |
| 163 | Byte *input; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 164 | int length, err; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 165 | int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 166 | z_stream zst; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 167 | if (!PyArg_ParseTuple(args, "s#|ii:decompress", &input, &length, &wsize, &r_strlen)) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 168 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 169 | |
| 170 | if (r_strlen <= 0) |
| 171 | r_strlen = 1; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 173 | zst.avail_in=length; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 174 | zst.avail_out=r_strlen; |
| 175 | if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen))) |
| 176 | { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 177 | PyErr_SetString(PyExc_MemoryError, |
| 178 | "Can't allocate memory to decompress data"); |
| 179 | return NULL; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 180 | } |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 181 | zst.zalloc=(alloc_func)NULL; |
| 182 | zst.zfree=(free_func)Z_NULL; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 183 | zst.next_out=(Byte *)PyString_AsString(result_str); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 184 | zst.next_in =(Byte *)input; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 185 | err=inflateInit2(&zst, wsize); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 186 | switch(err) |
| 187 | { |
| 188 | case(Z_OK): |
| 189 | break; |
| 190 | case(Z_MEM_ERROR): |
| 191 | PyErr_SetString(PyExc_MemoryError, |
| 192 | "Out of memory while decompressing data"); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 193 | Py_DECREF(result_str); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 194 | return NULL; |
| 195 | default: |
| 196 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 197 | if (zst.msg == Z_NULL) |
| 198 | PyErr_Format(ZlibError, "Error %i preparing to decompress data", |
| 199 | err); |
| 200 | else |
| 201 | PyErr_Format(ZlibError, |
| 202 | "Error %i while preparing to decompress data: %.200s", |
| 203 | err, zst.msg); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 204 | inflateEnd(&zst); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 205 | Py_DECREF(result_str); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 206 | return NULL; |
| 207 | } |
| 208 | } |
| 209 | do |
| 210 | { |
| 211 | err=inflate(&zst, Z_FINISH); |
| 212 | switch(err) |
| 213 | { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 214 | case(Z_STREAM_END): |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 215 | break; |
Guido van Rossum | 115f517 | 1998-04-23 20:22:11 +0000 | [diff] [blame] | 216 | case(Z_BUF_ERROR): |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 217 | case(Z_OK): |
| 218 | /* need more memory */ |
| 219 | if (_PyString_Resize(&result_str, r_strlen << 1) == -1) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 220 | { |
| 221 | PyErr_SetString(PyExc_MemoryError, |
| 222 | "Out of memory while decompressing data"); |
| 223 | inflateEnd(&zst); |
| 224 | return NULL; |
| 225 | } |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 226 | zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 227 | zst.avail_out=r_strlen; |
| 228 | r_strlen = r_strlen << 1; |
| 229 | break; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 230 | default: |
| 231 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 232 | if (zst.msg == Z_NULL) |
| 233 | PyErr_Format(ZlibError, "Error %i while decompressing data", |
| 234 | err); |
| 235 | else |
| 236 | PyErr_Format(ZlibError, |
| 237 | "Error %i while decompressing data: %.200s", |
| 238 | err, zst.msg); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 239 | inflateEnd(&zst); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 240 | Py_DECREF(result_str); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | } |
| 244 | } while(err!=Z_STREAM_END); |
| 245 | |
| 246 | err=inflateEnd(&zst); |
| 247 | if (err!=Z_OK) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 248 | { |
| 249 | if (zst.msg == Z_NULL) |
| 250 | PyErr_Format(ZlibError, |
| 251 | "Error %i while finishing data decompression", |
| 252 | err); |
| 253 | else |
| 254 | PyErr_Format(ZlibError, |
| 255 | "Error %i while finishing data decompression: %.200s", |
| 256 | err, zst.msg); |
| 257 | Py_DECREF(result_str); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 258 | return NULL; |
| 259 | } |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 260 | _PyString_Resize(&result_str, zst.total_out); |
| 261 | return result_str; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 265 | PyZlib_compressobj(PyObject *selfptr, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 266 | { |
| 267 | compobject *self; |
| 268 | int level=Z_DEFAULT_COMPRESSION, method=DEFLATED; |
| 269 | int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 271 | if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits, |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 272 | &memLevel, &strategy)) |
| 273 | return NULL; |
| 274 | |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 275 | self = newcompobject(&Comptype); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 276 | if (self==NULL) return(NULL); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 277 | self->zst.zalloc = (alloc_func)NULL; |
| 278 | self->zst.zfree = (free_func)Z_NULL; |
| 279 | err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 280 | switch(err) |
| 281 | { |
| 282 | case (Z_OK): |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 283 | self->is_initialised = 1; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 284 | return (PyObject*)self; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 285 | case (Z_MEM_ERROR): |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 286 | Py_DECREF(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 287 | PyErr_SetString(PyExc_MemoryError, |
| 288 | "Can't allocate memory for compression object"); |
| 289 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 290 | case(Z_STREAM_ERROR): |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 291 | Py_DECREF(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 292 | PyErr_SetString(PyExc_ValueError, |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 293 | "Invalid initialization option"); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 294 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 295 | default: |
| 296 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 297 | if (self->zst.msg == Z_NULL) |
| 298 | PyErr_Format(ZlibError, |
| 299 | "Error %i while creating compression object", |
| 300 | err); |
| 301 | else |
| 302 | PyErr_Format(ZlibError, |
| 303 | "Error %i while creating compression object: %.200s", |
| 304 | err, self->zst.msg); |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 305 | Py_DECREF(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 306 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 312 | PyZlib_decompressobj(PyObject *selfptr, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 313 | { |
| 314 | int wbits=DEF_WBITS, err; |
| 315 | compobject *self; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 316 | if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits)) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 317 | { |
| 318 | return NULL; |
| 319 | } |
| 320 | self=newcompobject(&Decomptype); |
| 321 | if (self==NULL) return(NULL); |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 322 | self->zst.zalloc=(alloc_func)NULL; |
| 323 | self->zst.zfree=(free_func)Z_NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 324 | err=inflateInit2(&self->zst, wbits); |
| 325 | switch(err) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 326 | { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 327 | case (Z_OK): |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 328 | self->is_initialised = 1; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 329 | return (PyObject*)self; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 330 | case(Z_STREAM_ERROR): |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 331 | Py_DECREF(self); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 332 | PyErr_SetString(PyExc_ValueError, |
| 333 | "Invalid initialization option"); |
| 334 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 335 | case (Z_MEM_ERROR): |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 336 | Py_DECREF(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 337 | PyErr_SetString(PyExc_MemoryError, |
| 338 | "Can't allocate memory for decompression object"); |
| 339 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 340 | default: |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 341 | { |
| 342 | if (self->zst.msg == Z_NULL) |
| 343 | PyErr_Format(ZlibError, |
| 344 | "Error %i while creating decompression object", |
| 345 | err); |
| 346 | else |
| 347 | PyErr_Format(ZlibError, |
| 348 | "Error %i while creating decompression object: %.200s", |
| 349 | err, self->zst.msg); |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 350 | Py_DECREF(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 351 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 352 | } |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 353 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 357 | Comp_dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 358 | { |
Andrew M. Kuchling | 1c7aaa2 | 1999-01-29 21:49:34 +0000 | [diff] [blame] | 359 | if (self->is_initialised) |
| 360 | deflateEnd(&self->zst); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 361 | Py_XDECREF(self->unused_data); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 362 | PyObject_Del(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 366 | Decomp_dealloc(compobject *self) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 367 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 368 | inflateEnd(&self->zst); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 369 | Py_XDECREF(self->unused_data); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 370 | PyObject_Del(self); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 373 | static char comp_compress__doc__[] = |
| 374 | "compress(data) -- Return a string containing a compressed version of the data.\n\n" |
| 375 | "After calling this function, some of the input data may still\n" |
| 376 | "be stored in internal buffers for later processing.\n" |
| 377 | "Call the flush() method to clear these buffers." |
| 378 | ; |
| 379 | |
| 380 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 381 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 382 | PyZlib_objcompress(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 383 | { |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 384 | int err = Z_OK, inplen; |
| 385 | int length = DEFAULTALLOC; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 386 | PyObject *RetVal; |
| 387 | Byte *input; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 388 | unsigned long start_total_out; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 390 | if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen)) |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 391 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 392 | self->zst.avail_in = inplen; |
| 393 | self->zst.next_in = input; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 394 | if (!(RetVal = PyString_FromStringAndSize(NULL, length))) { |
| 395 | PyErr_SetString(PyExc_MemoryError, |
| 396 | "Can't allocate memory to compress data"); |
| 397 | return NULL; |
| 398 | } |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 399 | start_total_out = self->zst.total_out; |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 400 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal); |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 401 | self->zst.avail_out = length; |
| 402 | while (self->zst.avail_in != 0 && err == Z_OK) |
| 403 | { |
| 404 | err = deflate(&(self->zst), Z_NO_FLUSH); |
| 405 | if (self->zst.avail_out <= 0) { |
| 406 | if (_PyString_Resize(&RetVal, length << 1) == -1) { |
| 407 | PyErr_SetString(PyExc_MemoryError, |
| 408 | "Can't allocate memory to compress data"); |
| 409 | return NULL; |
| 410 | } |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 411 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 412 | self->zst.avail_out = length; |
| 413 | length = length << 1; |
| 414 | } |
| 415 | } |
| 416 | if (err != Z_OK) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 417 | { |
| 418 | if (self->zst.msg == Z_NULL) |
| 419 | PyErr_Format(ZlibError, "Error %i while compressing", |
| 420 | err); |
| 421 | else |
| 422 | PyErr_Format(ZlibError, "Error %i while compressing: %.200s", |
| 423 | err, self->zst.msg); |
| 424 | Py_DECREF(RetVal); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 427 | _PyString_Resize(&RetVal, self->zst.total_out - start_total_out); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 428 | return RetVal; |
| 429 | } |
| 430 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 431 | static char decomp_decompress__doc__[] = |
| 432 | "decompress(data) -- Return a string containing the decompressed version of the data.\n\n" |
| 433 | "After calling this function, some of the input data may still\n" |
| 434 | "be stored in internal buffers for later processing.\n" |
| 435 | "Call the flush() method to clear these buffers." |
| 436 | ; |
| 437 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 438 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 439 | PyZlib_objdecompress(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 440 | { |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 441 | int length, err, inplen; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 442 | PyObject *RetVal; |
| 443 | Byte *input; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 444 | unsigned long start_total_out; |
| 445 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 446 | if (!PyArg_ParseTuple(args, "s#:decompress", &input, &inplen)) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 447 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 448 | start_total_out = self->zst.total_out; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 449 | RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 450 | self->zst.avail_in = inplen; |
| 451 | self->zst.next_in = input; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 452 | self->zst.avail_out = length = DEFAULTALLOC; |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 453 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal); |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 454 | err = Z_OK; |
Jeremy Hylton | a74ef66 | 1997-08-13 21:39:18 +0000 | [diff] [blame] | 455 | |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 456 | while (self->zst.avail_in != 0 && err == Z_OK) |
| 457 | { |
| 458 | err = inflate(&(self->zst), Z_NO_FLUSH); |
| 459 | if (err == Z_OK && self->zst.avail_out <= 0) |
| 460 | { |
| 461 | if (_PyString_Resize(&RetVal, length << 1) == -1) |
| 462 | { |
| 463 | PyErr_SetString(PyExc_MemoryError, |
| 464 | "Can't allocate memory to compress data"); |
| 465 | return NULL; |
| 466 | } |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 467 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 468 | self->zst.avail_out = length; |
| 469 | length = length << 1; |
| 470 | } |
| 471 | } |
| 472 | |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 473 | if (err != Z_OK && err != Z_STREAM_END) |
| 474 | { |
| 475 | if (self->zst.msg == Z_NULL) |
| 476 | PyErr_Format(ZlibError, "Error %i while decompressing", |
| 477 | err); |
| 478 | else |
| 479 | PyErr_Format(ZlibError, "Error %i while decompressing: %.200s", |
| 480 | err, self->zst.msg); |
| 481 | Py_DECREF(RetVal); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 484 | |
| 485 | if (err == Z_STREAM_END) |
| 486 | { |
| 487 | /* The end of the compressed data has been reached, so set |
| 488 | the unused_data attribute to a string containing the |
| 489 | remainder of the data in the string. */ |
| 490 | int pos = self->zst.next_in - input; /* Position in the string */ |
| 491 | Py_XDECREF(self->unused_data); /* Free the original, empty string */ |
| 492 | |
Guido van Rossum | aee9bb2 | 1999-04-12 14:35:48 +0000 | [diff] [blame] | 493 | self->unused_data = PyString_FromStringAndSize((char *)input+pos, |
| 494 | inplen-pos); |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 495 | if (self->unused_data == NULL) return NULL; |
| 496 | } |
| 497 | |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 498 | _PyString_Resize(&RetVal, self->zst.total_out - start_total_out); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 499 | return RetVal; |
| 500 | } |
| 501 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 502 | static char comp_flush__doc__[] = |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 503 | "flush( [mode] ) -- Return a string containing any remaining compressed data.\n" |
| 504 | "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the \n" |
| 505 | "default value used when mode is not specified is Z_FINISH.\n" |
| 506 | "If mode == Z_FINISH, the compressor object can no longer be used after\n" |
| 507 | "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] | 508 | ; |
| 509 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 510 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 511 | PyZlib_flush(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 512 | { |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 513 | int length=DEFAULTALLOC, err = Z_OK; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 514 | PyObject *RetVal; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 515 | int flushmode = Z_FINISH; |
| 516 | unsigned long start_total_out; |
| 517 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 518 | if (!PyArg_ParseTuple(args, "|i:flush", &flushmode)) |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 519 | return NULL; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 520 | |
| 521 | /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in |
| 522 | doing any work at all; just return an empty string. */ |
| 523 | if (flushmode == Z_NO_FLUSH) |
| 524 | { |
| 525 | return PyString_FromStringAndSize(NULL, 0); |
| 526 | } |
| 527 | |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 528 | self->zst.avail_in = 0; |
| 529 | self->zst.next_in = Z_NULL; |
| 530 | if (!(RetVal = PyString_FromStringAndSize(NULL, length))) { |
| 531 | PyErr_SetString(PyExc_MemoryError, |
| 532 | "Can't allocate memory to compress data"); |
| 533 | return NULL; |
| 534 | } |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 535 | start_total_out = self->zst.total_out; |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 536 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal); |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 537 | self->zst.avail_out = length; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 538 | |
| 539 | /* When flushing the zstream, there's no input data. |
| 540 | If zst.avail_out == 0, that means that more output space is |
| 541 | needed to complete the flush operation. */ |
Andrew M. Kuchling | 173156f | 1999-03-22 19:25:30 +0000 | [diff] [blame] | 542 | while (1) { |
| 543 | err = deflate(&(self->zst), flushmode); |
| 544 | |
| 545 | /* If the output is Z_OK, and there's still room in the output |
| 546 | buffer, then the flush is complete. */ |
| 547 | if ( (err == Z_OK) && self->zst.avail_out > 0) break; |
| 548 | |
| 549 | /* A nonzero return indicates some sort of error (but see |
| 550 | the comment for the error handler below) */ |
| 551 | if ( err != Z_OK ) break; |
| 552 | |
| 553 | /* There's no space left for output, so increase the buffer and loop |
| 554 | again */ |
| 555 | if (_PyString_Resize(&RetVal, length << 1) == -1) { |
| 556 | PyErr_SetString(PyExc_MemoryError, |
| 557 | "Can't allocate memory to compress data"); |
| 558 | return NULL; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 559 | } |
Andrew M. Kuchling | 173156f | 1999-03-22 19:25:30 +0000 | [diff] [blame] | 560 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length; |
| 561 | self->zst.avail_out = length; |
| 562 | length = length << 1; |
Jeremy Hylton | 6892aa3 | 1999-01-06 22:56:24 +0000 | [diff] [blame] | 563 | } |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 564 | |
Andrew M. Kuchling | 173156f | 1999-03-22 19:25:30 +0000 | [diff] [blame] | 565 | /* Raise an exception indicating an error. The condition for |
| 566 | detecting a error is kind of complicated; Z_OK indicates no |
| 567 | error, but if the flushmode is Z_FINISH, then Z_STREAM_END is |
| 568 | also not an error. */ |
| 569 | if (err!=Z_OK && !(flushmode == Z_FINISH && err == Z_STREAM_END) ) |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 570 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 571 | if (self->zst.msg == Z_NULL) |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 572 | PyErr_Format(ZlibError, "Error %i while flushing", |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 573 | err); |
| 574 | else |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 575 | PyErr_Format(ZlibError, "Error %i while flushing: %.200s", |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 576 | err, self->zst.msg); |
| 577 | Py_DECREF(RetVal); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 578 | return NULL; |
Jeremy Hylton | 6892aa3 | 1999-01-06 22:56:24 +0000 | [diff] [blame] | 579 | } |
Andrew M. Kuchling | 173156f | 1999-03-22 19:25:30 +0000 | [diff] [blame] | 580 | |
| 581 | /* If flushmode is Z_FINISH, we also have to call deflateEnd() to |
| 582 | free various data structures */ |
| 583 | |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 584 | if (flushmode == Z_FINISH) { |
| 585 | err=deflateEnd(&(self->zst)); |
| 586 | if (err!=Z_OK) { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 587 | if (self->zst.msg == Z_NULL) |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 588 | PyErr_Format(ZlibError, "Error %i from deflateEnd()", |
| 589 | err); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 590 | else |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 591 | PyErr_Format(ZlibError, |
| 592 | "Error %i from deflateEnd(): %.200s", |
| 593 | err, self->zst.msg); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 594 | Py_DECREF(RetVal); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 595 | return NULL; |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 596 | } |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 597 | } |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 598 | _PyString_Resize(&RetVal, self->zst.total_out - start_total_out); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 599 | return RetVal; |
| 600 | } |
| 601 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 602 | static char decomp_flush__doc__[] = |
| 603 | "flush() -- Return a string containing any remaining decompressed data. " |
| 604 | "The decompressor object can no longer be used after this call." |
| 605 | ; |
| 606 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 607 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 608 | PyZlib_unflush(compobject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 609 | { |
| 610 | int length=0, err; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 611 | PyObject *RetVal; |
| 612 | |
Andrew M. Kuchling | 14f5158 | 2000-08-03 02:04:05 +0000 | [diff] [blame] | 613 | if (!PyArg_ParseTuple(args, "")) |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 614 | return NULL; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 615 | if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC))) |
| 616 | { |
| 617 | PyErr_SetString(PyExc_MemoryError, |
| 618 | "Can't allocate memory to decompress data"); |
| 619 | return NULL; |
| 620 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 621 | self->zst.avail_in=0; |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 622 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal); |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 623 | length = self->zst.avail_out = DEFAULTALLOC; |
| 624 | |
Guido van Rossum | b729a1d | 1999-04-07 20:23:17 +0000 | [diff] [blame] | 625 | /* I suspect that Z_BUF_ERROR is the only error code we need to check for |
| 626 | in the following loop, but will leave the Z_OK in for now to avoid |
| 627 | destabilizing this function. --amk */ |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 628 | err = Z_OK; |
Guido van Rossum | b729a1d | 1999-04-07 20:23:17 +0000 | [diff] [blame] | 629 | while ( err == Z_OK ) |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 630 | { |
| 631 | err = inflate(&(self->zst), Z_FINISH); |
Guido van Rossum | b729a1d | 1999-04-07 20:23:17 +0000 | [diff] [blame] | 632 | if ( ( err == Z_OK || err == Z_BUF_ERROR ) && self->zst.avail_out == 0) |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 633 | { |
| 634 | if (_PyString_Resize(&RetVal, length << 1) == -1) |
| 635 | { |
| 636 | PyErr_SetString(PyExc_MemoryError, |
| 637 | "Can't allocate memory to decompress data"); |
| 638 | return NULL; |
| 639 | } |
Guido van Rossum | ed2554a | 1997-08-18 15:31:24 +0000 | [diff] [blame] | 640 | self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 641 | self->zst.avail_out = length; |
| 642 | length = length << 1; |
Guido van Rossum | b729a1d | 1999-04-07 20:23:17 +0000 | [diff] [blame] | 643 | err = Z_OK; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 646 | if (err!=Z_STREAM_END) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 647 | { |
| 648 | if (self->zst.msg == Z_NULL) |
| 649 | PyErr_Format(ZlibError, "Error %i while decompressing", |
| 650 | err); |
| 651 | else |
| 652 | PyErr_Format(ZlibError, "Error %i while decompressing: %.200s", |
| 653 | err, self->zst.msg); |
| 654 | Py_DECREF(RetVal); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 655 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 656 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 657 | err=inflateEnd(&(self->zst)); |
| 658 | if (err!=Z_OK) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 659 | { |
| 660 | if (self->zst.msg == Z_NULL) |
| 661 | PyErr_Format(ZlibError, |
| 662 | "Error %i while flushing decompression object", |
| 663 | err); |
| 664 | else |
| 665 | PyErr_Format(ZlibError, |
| 666 | "Error %i while flushing decompression object: %.200s", |
| 667 | err, self->zst.msg); |
| 668 | Py_DECREF(RetVal); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 669 | return NULL; |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 670 | } |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 671 | _PyString_Resize(&RetVal, |
| 672 | (char *)self->zst.next_out - PyString_AsString(RetVal)); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 673 | return RetVal; |
| 674 | } |
| 675 | |
| 676 | static PyMethodDef comp_methods[] = |
| 677 | { |
Andrew M. Kuchling | 14f5158 | 2000-08-03 02:04:05 +0000 | [diff] [blame] | 678 | {"compress", (binaryfunc)PyZlib_objcompress, |
| 679 | METH_VARARGS, comp_compress__doc__}, |
| 680 | {"flush", (binaryfunc)PyZlib_flush, |
| 681 | METH_VARARGS, comp_flush__doc__}, |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 682 | {NULL, NULL} |
| 683 | }; |
| 684 | |
| 685 | static PyMethodDef Decomp_methods[] = |
| 686 | { |
Andrew M. Kuchling | 14f5158 | 2000-08-03 02:04:05 +0000 | [diff] [blame] | 687 | {"decompress", (binaryfunc)PyZlib_objdecompress, |
| 688 | METH_VARARGS, decomp_decompress__doc__}, |
| 689 | {"flush", (binaryfunc)PyZlib_unflush, |
| 690 | METH_VARARGS, decomp_flush__doc__}, |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 691 | {NULL, NULL} |
| 692 | }; |
| 693 | |
| 694 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 695 | Comp_getattr(compobject *self, char *name) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 696 | { |
| 697 | return Py_FindMethod(comp_methods, (PyObject *)self, name); |
| 698 | } |
| 699 | |
| 700 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 701 | Decomp_getattr(compobject *self, char *name) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 702 | { |
Andrew M. Kuchling | b95227d | 1999-03-25 21:21:08 +0000 | [diff] [blame] | 703 | if (strcmp(name, "unused_data") == 0) |
| 704 | { |
| 705 | Py_INCREF(self->unused_data); |
| 706 | return self->unused_data; |
| 707 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 708 | return Py_FindMethod(Decomp_methods, (PyObject *)self, name); |
| 709 | } |
| 710 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 711 | static char adler32__doc__[] = |
| 712 | "adler32(string) -- Compute an Adler-32 checksum of string, using " |
| 713 | "a default starting value, and returning an integer value.\n" |
| 714 | "adler32(string, value) -- Compute an Adler-32 checksum of string, using " |
| 715 | "the starting value provided, and returning an integer value\n" |
| 716 | ; |
| 717 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 718 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 719 | PyZlib_adler32(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 720 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 721 | uLong adler32val=adler32(0L, Z_NULL, 0); |
| 722 | Byte *buf; |
| 723 | int len; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 724 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 725 | if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val)) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 726 | { |
| 727 | return NULL; |
| 728 | } |
| 729 | adler32val = adler32(adler32val, buf, len); |
| 730 | return PyInt_FromLong(adler32val); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 733 | static char crc32__doc__[] = |
| 734 | "crc32(string) -- Compute a CRC-32 checksum of string, using " |
| 735 | "a default starting value, and returning an integer value.\n" |
| 736 | "crc32(string, value) -- Compute a CRC-32 checksum of string, using " |
| 737 | "the starting value provided, and returning an integer value.\n" |
| 738 | ; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 739 | |
| 740 | static PyObject * |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 741 | PyZlib_crc32(PyObject *self, PyObject *args) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 742 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 743 | uLong crc32val=crc32(0L, Z_NULL, 0); |
| 744 | Byte *buf; |
| 745 | int len; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 746 | if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val)) |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 747 | { |
| 748 | return NULL; |
| 749 | } |
| 750 | crc32val = crc32(crc32val, buf, len); |
| 751 | return PyInt_FromLong(crc32val); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | |
| 755 | static PyMethodDef zlib_methods[] = |
| 756 | { |
Andrew M. Kuchling | 14f5158 | 2000-08-03 02:04:05 +0000 | [diff] [blame] | 757 | {"adler32", (PyCFunction)PyZlib_adler32, |
| 758 | METH_VARARGS, adler32__doc__}, |
| 759 | {"compress", (PyCFunction)PyZlib_compress, |
| 760 | METH_VARARGS, compress__doc__}, |
| 761 | {"compressobj", (PyCFunction)PyZlib_compressobj, |
| 762 | METH_VARARGS, compressobj__doc__}, |
| 763 | {"crc32", (PyCFunction)PyZlib_crc32, |
| 764 | METH_VARARGS, crc32__doc__}, |
| 765 | {"decompress", (PyCFunction)PyZlib_decompress, |
| 766 | METH_VARARGS, decompress__doc__}, |
| 767 | {"decompressobj", (PyCFunction)PyZlib_decompressobj, |
| 768 | METH_VARARGS, decompressobj__doc__}, |
| 769 | {NULL, NULL} |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 770 | }; |
| 771 | |
| 772 | statichere PyTypeObject Comptype = { |
Guido van Rossum | 9ec0f8b | 1997-12-18 05:21:29 +0000 | [diff] [blame] | 773 | PyObject_HEAD_INIT(0) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 774 | 0, |
| 775 | "Compress", |
| 776 | sizeof(compobject), |
| 777 | 0, |
| 778 | (destructor)Comp_dealloc, /*tp_dealloc*/ |
| 779 | 0, /*tp_print*/ |
| 780 | (getattrfunc)Comp_getattr, /*tp_getattr*/ |
| 781 | 0, /*tp_setattr*/ |
| 782 | 0, /*tp_compare*/ |
| 783 | 0, /*tp_repr*/ |
| 784 | 0, /*tp_as_number*/ |
| 785 | 0, /*tp_as_sequence*/ |
| 786 | 0, /*tp_as_mapping*/ |
| 787 | }; |
| 788 | |
| 789 | statichere PyTypeObject Decomptype = { |
Guido van Rossum | 9ec0f8b | 1997-12-18 05:21:29 +0000 | [diff] [blame] | 790 | PyObject_HEAD_INIT(0) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 791 | 0, |
| 792 | "Decompress", |
| 793 | sizeof(compobject), |
| 794 | 0, |
| 795 | (destructor)Decomp_dealloc, /*tp_dealloc*/ |
| 796 | 0, /*tp_print*/ |
| 797 | (getattrfunc)Decomp_getattr, /*tp_getattr*/ |
| 798 | 0, /*tp_setattr*/ |
| 799 | 0, /*tp_compare*/ |
| 800 | 0, /*tp_repr*/ |
| 801 | 0, /*tp_as_number*/ |
| 802 | 0, /*tp_as_sequence*/ |
| 803 | 0, /*tp_as_mapping*/ |
| 804 | }; |
| 805 | |
| 806 | /* The following insint() routine was blatantly ripped off from |
| 807 | socketmodule.c */ |
| 808 | |
| 809 | /* Convenience routine to export an integer value. |
| 810 | For simplicity, errors (which are unlikely anyway) are ignored. */ |
| 811 | static void |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 812 | insint(PyObject *d, char *name, int value) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 813 | { |
| 814 | PyObject *v = PyInt_FromLong((long) value); |
| 815 | if (v == NULL) { |
| 816 | /* Don't bother reporting this error */ |
| 817 | PyErr_Clear(); |
| 818 | } |
| 819 | else { |
| 820 | PyDict_SetItemString(d, name, v); |
| 821 | Py_DECREF(v); |
| 822 | } |
| 823 | } |
| 824 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 825 | static char zlib_module_documentation[]= |
| 826 | "The functions in this module allow compression and decompression " |
| 827 | "using the zlib library, which is based on GNU zip. \n\n" |
| 828 | "adler32(string) -- Compute an Adler-32 checksum.\n" |
| 829 | "adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n" |
| 830 | "compress(string) -- Compress a string.\n" |
| 831 | "compress(string, level) -- Compress a string with the given level of compression (1--9).\n" |
| 832 | "compressobj([level]) -- Return a compressor object.\n" |
| 833 | "crc32(string) -- Compute a CRC-32 checksum.\n" |
| 834 | "crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n" |
Andrew M. Kuchling | 313a3e3 | 1999-12-20 22:13:38 +0000 | [diff] [blame] | 835 | "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n" |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 836 | "decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n" |
| 837 | "Compressor objects support compress() and flush() methods; decompressor \n" |
| 838 | "objects support decompress() and flush()." |
| 839 | ; |
| 840 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 841 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 842 | PyInit_zlib(void) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 843 | { |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 844 | PyObject *m, *d, *ver; |
Guido van Rossum | 9ec0f8b | 1997-12-18 05:21:29 +0000 | [diff] [blame] | 845 | Comptype.ob_type = &PyType_Type; |
| 846 | Decomptype.ob_type = &PyType_Type; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 847 | m = Py_InitModule4("zlib", zlib_methods, |
| 848 | zlib_module_documentation, |
| 849 | (PyObject*)NULL,PYTHON_API_VERSION); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 850 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 851 | ZlibError = PyErr_NewException("zlib.error", NULL, NULL); |
Fred Drake | 8972dfd | 1999-12-22 16:13:54 +0000 | [diff] [blame] | 852 | if (ZlibError != NULL) |
| 853 | PyDict_SetItemString(d, "error", ZlibError); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 854 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 855 | insint(d, "MAX_WBITS", MAX_WBITS); |
| 856 | insint(d, "DEFLATED", DEFLATED); |
| 857 | insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 858 | insint(d, "Z_BEST_SPEED", Z_BEST_SPEED); |
| 859 | insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION); |
| 860 | insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION); |
| 861 | insint(d, "Z_FILTERED", Z_FILTERED); |
| 862 | insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY); |
| 863 | insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); |
Jeremy Hylton | a37e244 | 1998-12-18 22:13:11 +0000 | [diff] [blame] | 864 | |
| 865 | insint(d, "Z_FINISH", Z_FINISH); |
| 866 | insint(d, "Z_NO_FLUSH", Z_NO_FLUSH); |
| 867 | insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH); |
| 868 | insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH); |
| 869 | |
Jeremy Hylton | cb91404 | 1997-09-04 23:39:23 +0000 | [diff] [blame] | 870 | ver = PyString_FromString(ZLIB_VERSION); |
Fred Drake | 8972dfd | 1999-12-22 16:13:54 +0000 | [diff] [blame] | 871 | if (ver != NULL) { |
| 872 | PyDict_SetItemString(d, "ZLIB_VERSION", ver); |
| 873 | Py_DECREF(ver); |
| 874 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 875 | } |