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