Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 1 | /* zlibmodule.c -- gzip-compatible data compression */ |
| 2 | |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 3 | #include "Python.h" |
| 4 | #include "zlib.h" |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 5 | |
| 6 | /* The following parameters are copied from zutil.h, version 0.95 */ |
| 7 | #define DEFLATED 8 |
| 8 | #if MAX_MEM_LEVEL >= 8 |
| 9 | # define DEF_MEM_LEVEL 8 |
| 10 | #else |
| 11 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL |
| 12 | #endif |
| 13 | #define DEF_WBITS MAX_WBITS |
| 14 | |
| 15 | /* The output buffer will be increased in chunks of ADDCHUNK bytes. */ |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 16 | #define DEFAULTALLOC 16*1024 |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 17 | #define PyInit_zlib initzlib |
| 18 | |
| 19 | staticforward PyTypeObject Comptype; |
| 20 | staticforward PyTypeObject Decomptype; |
| 21 | |
| 22 | static PyObject *ZlibError; |
| 23 | |
| 24 | typedef struct |
| 25 | { |
| 26 | PyObject_HEAD |
| 27 | z_stream zst; |
| 28 | } compobject; |
| 29 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 30 | static char compressobj__doc__[] = |
| 31 | "compressobj() -- Return a compressor object.\n" |
| 32 | "compressobj(level) -- Return a compressor object, using the given compression level.\n" |
| 33 | ; |
| 34 | |
| 35 | static char decompressobj__doc__[] = |
| 36 | "decompressobj() -- Return a decompressor object.\n" |
| 37 | "decompressobj(wbits) -- Return a decompressor object, setting the window buffer size to wbits.\n" |
| 38 | ; |
| 39 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 40 | static compobject * |
| 41 | newcompobject(type) |
| 42 | PyTypeObject *type; |
| 43 | { |
| 44 | compobject *self; |
| 45 | self = PyObject_NEW(compobject, type); |
| 46 | if (self == NULL) |
| 47 | return NULL; |
| 48 | return self; |
| 49 | } |
| 50 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 51 | static char compress__doc__[] = |
| 52 | "compress(string) -- Compress string using the default compression level, " |
| 53 | "returning a string containing compressed data.\n" |
| 54 | "compress(string, level) -- Compress string, using the chosen compression " |
| 55 | "level (from 1 to 9). Return a string containing the compressed data.\n" |
| 56 | ; |
| 57 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 58 | static PyObject * |
| 59 | PyZlib_compress(self, args) |
| 60 | PyObject *self; |
| 61 | PyObject *args; |
| 62 | { |
| 63 | PyObject *ReturnVal; |
| 64 | Byte *input, *output; |
| 65 | int length, level=Z_DEFAULT_COMPRESSION, err; |
| 66 | z_stream zst; |
| 67 | |
| 68 | if (!PyArg_ParseTuple(args, "s#|i", &input, &length, &level)) |
| 69 | return NULL; |
| 70 | zst.avail_out=length+length/1000+12+1; |
| 71 | output=(Byte*)malloc(zst.avail_out); |
| 72 | if (output==NULL) |
| 73 | { |
| 74 | PyErr_SetString(PyExc_MemoryError, |
| 75 | "Can't allocate memory to compress data"); |
| 76 | return NULL; |
| 77 | } |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 78 | zst.zalloc=(alloc_func)NULL; |
| 79 | zst.zfree=(free_func)Z_NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 80 | zst.next_out=(Byte *)output; |
| 81 | zst.next_in =(Byte *)input; |
| 82 | zst.avail_in=length; |
| 83 | err=deflateInit(&zst, level); |
| 84 | switch(err) |
| 85 | { |
| 86 | case(Z_OK): |
| 87 | break; |
| 88 | case(Z_MEM_ERROR): |
| 89 | PyErr_SetString(PyExc_MemoryError, |
| 90 | "Out of memory while compressing data"); |
| 91 | free(output); |
| 92 | return NULL; |
| 93 | break; |
| 94 | case(Z_STREAM_ERROR): |
| 95 | PyErr_SetString(ZlibError, |
| 96 | "Bad compression level"); |
| 97 | free(output); |
| 98 | return NULL; |
| 99 | break; |
| 100 | default: |
| 101 | { |
| 102 | char temp[500]; |
| 103 | if (zst.msg==Z_NULL) zst.msg=""; |
| 104 | sprintf(temp, "Error %i while compressing data [%s]", err, zst.msg); |
| 105 | PyErr_SetString(ZlibError, temp); |
| 106 | deflateEnd(&zst); |
| 107 | free(output); |
| 108 | return NULL; |
| 109 | } |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | err=deflate(&zst, Z_FINISH); |
| 114 | switch(err) |
| 115 | { |
| 116 | case(Z_STREAM_END): |
| 117 | break; |
| 118 | /* Are there other errors to be trapped here? */ |
| 119 | default: |
| 120 | { |
| 121 | char temp[500]; |
| 122 | if (zst.msg==Z_NULL) zst.msg=""; |
| 123 | sprintf(temp, "Error %i while compressing data [%s]", err, zst.msg); |
| 124 | PyErr_SetString(ZlibError, temp); |
| 125 | deflateEnd(&zst); |
| 126 | free(output); |
| 127 | return NULL; |
| 128 | } |
| 129 | } |
| 130 | err=deflateEnd(&zst); |
| 131 | if (err!=Z_OK) |
| 132 | { |
| 133 | char temp[500]; |
| 134 | if (zst.msg==Z_NULL) zst.msg=""; |
| 135 | sprintf(temp, "Error %i while finishing data compression [%s]", |
| 136 | err, zst.msg); |
| 137 | PyErr_SetString(ZlibError, temp); |
| 138 | free(output); |
| 139 | return NULL; |
| 140 | } |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 141 | ReturnVal=PyString_FromStringAndSize((char *)output, zst.total_out); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 142 | free(output); |
| 143 | return ReturnVal; |
| 144 | } |
| 145 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 146 | static char decompress__doc__[] = |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 147 | "decompress(string) -- Decompress the data in string, returning a string containing the decompressed data.\n" |
| 148 | "decompress(string, wbits) -- Decompress the data in string with a window buffer size of wbits.\n" |
| 149 | "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] | 150 | ; |
| 151 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 152 | static PyObject * |
| 153 | PyZlib_decompress(self, args) |
| 154 | PyObject *self; |
| 155 | PyObject *args; |
| 156 | { |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 157 | PyObject *result_str; |
| 158 | Byte *input; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 159 | int length, err; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 160 | int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 161 | z_stream zst; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 162 | if (!PyArg_ParseTuple(args, "s#|ii", &input, &length, &wsize, &r_strlen)) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 163 | return NULL; |
| 164 | |
| 165 | zst.avail_in=length; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 166 | zst.avail_out=r_strlen; |
| 167 | if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen))) |
| 168 | { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 169 | PyErr_SetString(PyExc_MemoryError, |
| 170 | "Can't allocate memory to decompress data"); |
| 171 | return NULL; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 172 | } |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 173 | zst.zalloc=(alloc_func)NULL; |
| 174 | zst.zfree=(free_func)Z_NULL; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 175 | zst.next_out=(Byte *)PyString_AsString(result_str); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 176 | zst.next_in =(Byte *)input; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 177 | err=inflateInit2(&zst, wsize); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 178 | switch(err) |
| 179 | { |
| 180 | case(Z_OK): |
| 181 | break; |
| 182 | case(Z_MEM_ERROR): |
| 183 | PyErr_SetString(PyExc_MemoryError, |
| 184 | "Out of memory while decompressing data"); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 185 | return NULL; |
| 186 | default: |
| 187 | { |
| 188 | char temp[500]; |
| 189 | if (zst.msg==Z_NULL) zst.msg=""; |
| 190 | sprintf(temp, "Error %i while preparing to decompress data [%s]", |
| 191 | err, zst.msg); |
| 192 | PyErr_SetString(ZlibError, temp); |
| 193 | inflateEnd(&zst); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 194 | return NULL; |
| 195 | } |
| 196 | } |
| 197 | do |
| 198 | { |
| 199 | err=inflate(&zst, Z_FINISH); |
| 200 | switch(err) |
| 201 | { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 202 | case(Z_STREAM_END): |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 203 | break; |
| 204 | case(Z_OK): |
| 205 | /* need more memory */ |
| 206 | if (_PyString_Resize(&result_str, r_strlen << 1) == -1) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 207 | { |
| 208 | PyErr_SetString(PyExc_MemoryError, |
| 209 | "Out of memory while decompressing data"); |
| 210 | inflateEnd(&zst); |
| 211 | return NULL; |
| 212 | } |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 213 | zst.next_out = PyString_AsString(result_str) + r_strlen; |
| 214 | zst.avail_out=r_strlen; |
| 215 | r_strlen = r_strlen << 1; |
| 216 | break; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 217 | default: |
| 218 | { |
| 219 | char temp[500]; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 220 | if (zst.msg==Z_NULL) zst.msg=""; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 221 | sprintf(temp, "Error %i while decompressing data: [%s]", |
| 222 | err, zst.msg); |
| 223 | PyErr_SetString(ZlibError, temp); |
| 224 | inflateEnd(&zst); |
| 225 | return NULL; |
| 226 | } |
| 227 | } |
| 228 | } while(err!=Z_STREAM_END); |
| 229 | |
| 230 | err=inflateEnd(&zst); |
| 231 | if (err!=Z_OK) |
| 232 | { |
| 233 | char temp[500]; |
| 234 | if (zst.msg==Z_NULL) zst.msg=""; |
| 235 | sprintf(temp, "Error %i while finishing data decompression [%s]", |
| 236 | err, zst.msg); |
| 237 | PyErr_SetString(ZlibError, temp); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 238 | return NULL; |
| 239 | } |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 240 | _PyString_Resize(&result_str, zst.total_out); |
| 241 | return result_str; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static PyObject * |
| 245 | PyZlib_compressobj(selfptr, args) |
| 246 | PyObject *selfptr; |
| 247 | PyObject *args; |
| 248 | { |
| 249 | compobject *self; |
| 250 | int level=Z_DEFAULT_COMPRESSION, method=DEFLATED; |
| 251 | int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 252 | |
| 253 | if (!PyArg_ParseTuple(args, "|iiiii", &level, &method, &wbits, |
| 254 | &memLevel, &strategy)) |
| 255 | return NULL; |
| 256 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 257 | self=newcompobject(&Comptype); |
| 258 | if (self==NULL) return(NULL); |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 259 | self->zst.zalloc=(alloc_func)NULL; |
| 260 | self->zst.zfree=(free_func)Z_NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 261 | err=deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); |
| 262 | switch(err) |
| 263 | { |
| 264 | case (Z_OK): |
| 265 | return (PyObject*)self; |
| 266 | break; |
| 267 | case (Z_MEM_ERROR): |
| 268 | PyErr_SetString(PyExc_MemoryError, |
| 269 | "Can't allocate memory for compression object"); |
| 270 | return NULL; |
| 271 | break; |
| 272 | case(Z_STREAM_ERROR): |
| 273 | PyErr_SetString(PyExc_ValueError, |
| 274 | "Invalid compression level"); |
| 275 | return NULL; |
| 276 | break; |
| 277 | default: |
| 278 | { |
| 279 | char temp[500]; |
| 280 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
| 281 | sprintf(temp, "Error %i while creating compression object [%s]", |
| 282 | err, self->zst.msg); |
| 283 | PyErr_SetString(ZlibError, temp); |
| 284 | return NULL; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static PyObject * |
| 291 | PyZlib_decompressobj(selfptr, args) |
| 292 | PyObject *selfptr; |
| 293 | PyObject *args; |
| 294 | { |
| 295 | int wbits=DEF_WBITS, err; |
| 296 | compobject *self; |
| 297 | if (!PyArg_ParseTuple(args, "|i", &wbits)) |
| 298 | { |
| 299 | return NULL; |
| 300 | } |
| 301 | self=newcompobject(&Decomptype); |
| 302 | if (self==NULL) return(NULL); |
Guido van Rossum | 97b5457 | 1997-06-03 22:21:47 +0000 | [diff] [blame] | 303 | self->zst.zalloc=(alloc_func)NULL; |
| 304 | self->zst.zfree=(free_func)Z_NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 305 | err=inflateInit2(&self->zst, wbits); |
| 306 | switch(err) |
| 307 | { |
| 308 | case (Z_OK): |
| 309 | return (PyObject*)self; |
| 310 | break; |
| 311 | case (Z_MEM_ERROR): |
| 312 | PyErr_SetString(PyExc_MemoryError, |
| 313 | "Can't allocate memory for decompression object"); |
| 314 | return NULL; |
| 315 | break; |
| 316 | default: |
| 317 | { |
| 318 | char temp[500]; |
| 319 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
| 320 | sprintf(temp, "Error %i while creating decompression object [%s]", |
| 321 | err, self->zst.msg); |
| 322 | PyErr_SetString(ZlibError, temp); |
| 323 | return NULL; |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | static void |
| 330 | Comp_dealloc(self) |
| 331 | compobject *self; |
| 332 | { |
| 333 | int err; |
| 334 | err=deflateEnd(&self->zst); /* Deallocate zstream structure */ |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | Decomp_dealloc(self) |
| 339 | compobject *self; |
| 340 | { |
| 341 | int err; |
| 342 | err=inflateEnd(&self->zst); /* Deallocate zstream structure */ |
| 343 | } |
| 344 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 345 | static char comp_compress__doc__[] = |
| 346 | "compress(data) -- Return a string containing a compressed version of the data.\n\n" |
| 347 | "After calling this function, some of the input data may still\n" |
| 348 | "be stored in internal buffers for later processing.\n" |
| 349 | "Call the flush() method to clear these buffers." |
| 350 | ; |
| 351 | |
| 352 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 353 | static PyObject * |
| 354 | PyZlib_objcompress(self, args) |
| 355 | compobject *self; |
| 356 | PyObject *args; |
| 357 | { |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 358 | int err = Z_OK, inplen; |
| 359 | int length = DEFAULTALLOC; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 360 | PyObject *RetVal; |
| 361 | Byte *input; |
| 362 | |
| 363 | if (!PyArg_ParseTuple(args, "s#", &input, &inplen)) |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 364 | return NULL; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 365 | self->zst.avail_in=inplen; |
| 366 | self->zst.next_in=input; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 367 | if (!(RetVal = PyString_FromStringAndSize(NULL, length))) { |
| 368 | PyErr_SetString(PyExc_MemoryError, |
| 369 | "Can't allocate memory to compress data"); |
| 370 | return NULL; |
| 371 | } |
| 372 | self->zst.next_out = PyString_AsString(RetVal); |
| 373 | self->zst.avail_out = length; |
| 374 | while (self->zst.avail_in != 0 && err == Z_OK) |
| 375 | { |
| 376 | err = deflate(&(self->zst), Z_NO_FLUSH); |
| 377 | if (self->zst.avail_out <= 0) { |
| 378 | if (_PyString_Resize(&RetVal, length << 1) == -1) { |
| 379 | PyErr_SetString(PyExc_MemoryError, |
| 380 | "Can't allocate memory to compress data"); |
| 381 | return NULL; |
| 382 | } |
| 383 | self->zst.next_out = PyString_AsString(RetVal) + length; |
| 384 | self->zst.avail_out = length; |
| 385 | length = length << 1; |
| 386 | } |
| 387 | } |
| 388 | if (err != Z_OK) |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 389 | { |
| 390 | char temp[500]; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 391 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 392 | sprintf(temp, "Error %i while compressing [%s]", |
| 393 | err, self->zst.msg); |
| 394 | PyErr_SetString(ZlibError, temp); |
| 395 | return NULL; |
| 396 | } |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 397 | _PyString_Resize(&RetVal, self->zst.total_out); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 398 | return RetVal; |
| 399 | } |
| 400 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 401 | static char decomp_decompress__doc__[] = |
| 402 | "decompress(data) -- Return a string containing the decompressed version of the data.\n\n" |
| 403 | "After calling this function, some of the input data may still\n" |
| 404 | "be stored in internal buffers for later processing.\n" |
| 405 | "Call the flush() method to clear these buffers." |
| 406 | ; |
| 407 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 408 | static PyObject * |
| 409 | PyZlib_objdecompress(self, args) |
| 410 | compobject *self; |
| 411 | PyObject *args; |
| 412 | { |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 413 | int length, err, inplen; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 414 | PyObject *RetVal; |
| 415 | Byte *input; |
| 416 | if (!PyArg_ParseTuple(args, "s#", &input, &inplen)) |
| 417 | return NULL; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 418 | RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 419 | self->zst.avail_in=inplen; |
| 420 | self->zst.next_in=input; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 421 | self->zst.avail_out = length = DEFAULTALLOC; |
| 422 | self->zst.next_out = PyString_AsString(RetVal); |
| 423 | err = Z_OK; |
Jeremy Hylton | a74ef66 | 1997-08-13 21:39:18 +0000 | [diff] [blame] | 424 | |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 425 | while (self->zst.avail_in != 0 && err == Z_OK) |
| 426 | { |
| 427 | err = inflate(&(self->zst), Z_NO_FLUSH); |
| 428 | if (err == Z_OK && self->zst.avail_out <= 0) |
| 429 | { |
| 430 | if (_PyString_Resize(&RetVal, length << 1) == -1) |
| 431 | { |
| 432 | PyErr_SetString(PyExc_MemoryError, |
| 433 | "Can't allocate memory to compress data"); |
| 434 | return NULL; |
| 435 | } |
| 436 | self->zst.next_out = PyString_AsString(RetVal) + length; |
| 437 | self->zst.avail_out = length; |
| 438 | length = length << 1; |
| 439 | } |
| 440 | } |
| 441 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 442 | if (err!=Z_OK && err!=Z_STREAM_END) |
| 443 | { |
| 444 | char temp[500]; |
| 445 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
| 446 | sprintf(temp, "Error %i while decompressing [%s]", |
| 447 | err, self->zst.msg); |
| 448 | PyErr_SetString(ZlibError, temp); |
| 449 | return NULL; |
| 450 | } |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 451 | _PyString_Resize(&RetVal, self->zst.total_out); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 452 | return RetVal; |
| 453 | } |
| 454 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 455 | static char comp_flush__doc__[] = |
| 456 | "flush() -- Return a string containing any remaining compressed data. " |
| 457 | "The compressor object can no longer be used after this call." |
| 458 | ; |
| 459 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 460 | static PyObject * |
| 461 | PyZlib_flush(self, args) |
| 462 | compobject *self; |
| 463 | PyObject *args; |
| 464 | { |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 465 | int length=DEFAULTALLOC, err = Z_OK; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 466 | PyObject *RetVal; |
| 467 | |
| 468 | if (!PyArg_NoArgs(args)) |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 469 | return NULL; |
| 470 | self->zst.avail_in = 0; |
| 471 | self->zst.next_in = Z_NULL; |
| 472 | if (!(RetVal = PyString_FromStringAndSize(NULL, length))) { |
| 473 | PyErr_SetString(PyExc_MemoryError, |
| 474 | "Can't allocate memory to compress data"); |
| 475 | return NULL; |
| 476 | } |
| 477 | self->zst.next_out = PyString_AsString(RetVal); |
| 478 | self->zst.avail_out = length; |
| 479 | while (err == Z_OK) |
| 480 | { |
| 481 | err = deflate(&(self->zst), Z_FINISH); |
| 482 | if (self->zst.avail_out <= 0) { |
| 483 | if (_PyString_Resize(&RetVal, length << 1) == -1) { |
| 484 | PyErr_SetString(PyExc_MemoryError, |
| 485 | "Can't allocate memory to compress data"); |
| 486 | return NULL; |
| 487 | } |
| 488 | self->zst.next_out = PyString_AsString(RetVal) + length; |
| 489 | self->zst.avail_out = length; |
| 490 | length = length << 1; |
| 491 | } |
| 492 | } |
| 493 | if (err!=Z_STREAM_END) { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 494 | char temp[500]; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 495 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 496 | sprintf(temp, "Error %i while compressing [%s]", |
| 497 | err, self->zst.msg); |
| 498 | PyErr_SetString(ZlibError, temp); |
| 499 | return NULL; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 500 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 501 | err=deflateEnd(&(self->zst)); |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 502 | if (err!=Z_OK) { |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 503 | char temp[500]; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 504 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 505 | sprintf(temp, "Error %i while flushing compression object [%s]", |
| 506 | err, self->zst.msg); |
| 507 | PyErr_SetString(ZlibError, temp); |
| 508 | return NULL; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 509 | } |
| 510 | _PyString_Resize(&RetVal, |
| 511 | (char *)self->zst.next_out - PyString_AsString(RetVal)); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 512 | return RetVal; |
| 513 | } |
| 514 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 515 | static char decomp_flush__doc__[] = |
| 516 | "flush() -- Return a string containing any remaining decompressed data. " |
| 517 | "The decompressor object can no longer be used after this call." |
| 518 | ; |
| 519 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 520 | static PyObject * |
| 521 | PyZlib_unflush(self, args) |
| 522 | compobject *self; |
| 523 | PyObject *args; |
| 524 | { |
| 525 | int length=0, err; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 526 | PyObject *RetVal; |
| 527 | |
| 528 | if (!PyArg_NoArgs(args)) |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 529 | return NULL; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 530 | if (!(RetVal = PyString_FromStringAndSize(NULL, DEFAULTALLOC))) |
| 531 | { |
| 532 | PyErr_SetString(PyExc_MemoryError, |
| 533 | "Can't allocate memory to decompress data"); |
| 534 | return NULL; |
| 535 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 536 | self->zst.avail_in=0; |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 537 | self->zst.next_out = PyString_AsString(RetVal); |
| 538 | length = self->zst.avail_out = DEFAULTALLOC; |
| 539 | |
| 540 | err = Z_OK; |
| 541 | while (err == Z_OK) |
| 542 | { |
| 543 | err = inflate(&(self->zst), Z_FINISH); |
| 544 | if (err == Z_OK && self->zst.avail_out == 0) |
| 545 | { |
| 546 | if (_PyString_Resize(&RetVal, length << 1) == -1) |
| 547 | { |
| 548 | PyErr_SetString(PyExc_MemoryError, |
| 549 | "Can't allocate memory to decompress data"); |
| 550 | return NULL; |
| 551 | } |
| 552 | self->zst.next_out = PyString_AsString(RetVal) + length; |
| 553 | self->zst.avail_out = length; |
| 554 | length = length << 1; |
| 555 | } |
| 556 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 557 | if (err!=Z_STREAM_END) |
| 558 | { |
| 559 | char temp[500]; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 560 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 561 | sprintf(temp, "Error %i while decompressing [%s]", |
| 562 | err, self->zst.msg); |
| 563 | PyErr_SetString(ZlibError, temp); |
| 564 | return NULL; |
| 565 | } |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 566 | err=inflateEnd(&(self->zst)); |
| 567 | if (err!=Z_OK) |
| 568 | { |
| 569 | char temp[500]; |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 570 | if (self->zst.msg==Z_NULL) self->zst.msg=""; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 571 | sprintf(temp, "Error %i while flushing decompression object [%s]", |
| 572 | err, self->zst.msg); |
| 573 | PyErr_SetString(ZlibError, temp); |
| 574 | return NULL; |
| 575 | } |
Jeremy Hylton | 644c17d | 1997-08-14 21:06:42 +0000 | [diff] [blame] | 576 | _PyString_Resize(&RetVal, |
| 577 | (char *)self->zst.next_out - PyString_AsString(RetVal)); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 578 | return RetVal; |
| 579 | } |
| 580 | |
| 581 | static PyMethodDef comp_methods[] = |
| 582 | { |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 583 | {"compress", PyZlib_objcompress, 1, comp_compress__doc__}, |
| 584 | {"flush", PyZlib_flush, 0, comp_flush__doc__}, |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 585 | {NULL, NULL} |
| 586 | }; |
| 587 | |
| 588 | static PyMethodDef Decomp_methods[] = |
| 589 | { |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 590 | {"decompress", PyZlib_objdecompress, 1, decomp_decompress__doc__}, |
| 591 | {"flush", PyZlib_unflush, 0, decomp_flush__doc__}, |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 592 | {NULL, NULL} |
| 593 | }; |
| 594 | |
| 595 | static PyObject * |
| 596 | Comp_getattr(self, name) |
| 597 | compobject *self; |
| 598 | char *name; |
| 599 | { |
| 600 | return Py_FindMethod(comp_methods, (PyObject *)self, name); |
| 601 | } |
| 602 | |
| 603 | static PyObject * |
| 604 | Decomp_getattr(self, name) |
| 605 | compobject *self; |
| 606 | char *name; |
| 607 | { |
| 608 | return Py_FindMethod(Decomp_methods, (PyObject *)self, name); |
| 609 | } |
| 610 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 611 | static char adler32__doc__[] = |
| 612 | "adler32(string) -- Compute an Adler-32 checksum of string, using " |
| 613 | "a default starting value, and returning an integer value.\n" |
| 614 | "adler32(string, value) -- Compute an Adler-32 checksum of string, using " |
| 615 | "the starting value provided, and returning an integer value\n" |
| 616 | ; |
| 617 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 618 | static PyObject * |
| 619 | PyZlib_adler32(self, args) |
| 620 | PyObject *self, *args; |
| 621 | { |
| 622 | uLong adler32val=adler32(0L, Z_NULL, 0); |
| 623 | Byte *buf; |
| 624 | int len; |
| 625 | |
| 626 | if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &adler32val)) |
| 627 | { |
| 628 | return NULL; |
| 629 | } |
| 630 | adler32val=adler32(adler32val, buf, len); |
| 631 | return Py_BuildValue("l", adler32val); |
| 632 | } |
| 633 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 634 | static char crc32__doc__[] = |
| 635 | "crc32(string) -- Compute a CRC-32 checksum of string, using " |
| 636 | "a default starting value, and returning an integer value.\n" |
| 637 | "crc32(string, value) -- Compute a CRC-32 checksum of string, using " |
| 638 | "the starting value provided, and returning an integer value.\n" |
| 639 | ; |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 640 | |
| 641 | static PyObject * |
| 642 | PyZlib_crc32(self, args) |
| 643 | PyObject *self, *args; |
| 644 | { |
| 645 | uLong crc32val=crc32(0L, Z_NULL, 0); |
| 646 | Byte *buf; |
| 647 | int len; |
| 648 | if (!PyArg_ParseTuple(args, "s#|l", &buf, &len, &crc32val)) |
| 649 | { |
| 650 | return NULL; |
| 651 | } |
| 652 | crc32val=crc32(crc32val, buf, len); |
| 653 | return Py_BuildValue("l", crc32val); |
| 654 | } |
| 655 | |
| 656 | |
| 657 | static PyMethodDef zlib_methods[] = |
| 658 | { |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 659 | {"adler32", (PyCFunction)PyZlib_adler32, 1, adler32__doc__}, |
| 660 | {"compress", (PyCFunction)PyZlib_compress, 1, compress__doc__}, |
| 661 | {"compressobj", (PyCFunction)PyZlib_compressobj, 1, compressobj__doc__}, |
| 662 | {"crc32", (PyCFunction)PyZlib_crc32, 1, crc32__doc__}, |
| 663 | {"decompress", (PyCFunction)PyZlib_decompress, 1, decompress__doc__}, |
| 664 | {"decompressobj", (PyCFunction)PyZlib_decompressobj, 1, decompressobj__doc__}, |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 665 | {NULL, NULL} |
| 666 | }; |
| 667 | |
| 668 | statichere PyTypeObject Comptype = { |
| 669 | PyObject_HEAD_INIT(&PyType_Type) |
| 670 | 0, |
| 671 | "Compress", |
| 672 | sizeof(compobject), |
| 673 | 0, |
| 674 | (destructor)Comp_dealloc, /*tp_dealloc*/ |
| 675 | 0, /*tp_print*/ |
| 676 | (getattrfunc)Comp_getattr, /*tp_getattr*/ |
| 677 | 0, /*tp_setattr*/ |
| 678 | 0, /*tp_compare*/ |
| 679 | 0, /*tp_repr*/ |
| 680 | 0, /*tp_as_number*/ |
| 681 | 0, /*tp_as_sequence*/ |
| 682 | 0, /*tp_as_mapping*/ |
| 683 | }; |
| 684 | |
| 685 | statichere PyTypeObject Decomptype = { |
| 686 | PyObject_HEAD_INIT(&PyType_Type) |
| 687 | 0, |
| 688 | "Decompress", |
| 689 | sizeof(compobject), |
| 690 | 0, |
| 691 | (destructor)Decomp_dealloc, /*tp_dealloc*/ |
| 692 | 0, /*tp_print*/ |
| 693 | (getattrfunc)Decomp_getattr, /*tp_getattr*/ |
| 694 | 0, /*tp_setattr*/ |
| 695 | 0, /*tp_compare*/ |
| 696 | 0, /*tp_repr*/ |
| 697 | 0, /*tp_as_number*/ |
| 698 | 0, /*tp_as_sequence*/ |
| 699 | 0, /*tp_as_mapping*/ |
| 700 | }; |
| 701 | |
| 702 | /* The following insint() routine was blatantly ripped off from |
| 703 | socketmodule.c */ |
| 704 | |
| 705 | /* Convenience routine to export an integer value. |
| 706 | For simplicity, errors (which are unlikely anyway) are ignored. */ |
| 707 | static void |
| 708 | insint(d, name, value) |
| 709 | PyObject *d; |
| 710 | char *name; |
| 711 | int value; |
| 712 | { |
| 713 | PyObject *v = PyInt_FromLong((long) value); |
| 714 | if (v == NULL) { |
| 715 | /* Don't bother reporting this error */ |
| 716 | PyErr_Clear(); |
| 717 | } |
| 718 | else { |
| 719 | PyDict_SetItemString(d, name, v); |
| 720 | Py_DECREF(v); |
| 721 | } |
| 722 | } |
| 723 | |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 724 | static char zlib_module_documentation[]= |
| 725 | "The functions in this module allow compression and decompression " |
| 726 | "using the zlib library, which is based on GNU zip. \n\n" |
| 727 | "adler32(string) -- Compute an Adler-32 checksum.\n" |
| 728 | "adler32(string, start) -- Compute an Adler-32 checksum using a given starting value.\n" |
| 729 | "compress(string) -- Compress a string.\n" |
| 730 | "compress(string, level) -- Compress a string with the given level of compression (1--9).\n" |
| 731 | "compressobj([level]) -- Return a compressor object.\n" |
| 732 | "crc32(string) -- Compute a CRC-32 checksum.\n" |
| 733 | "crc32(string, start) -- Compute a CRC-32 checksum using a given starting value.\n" |
Jeremy Hylton | 41b9f00 | 1997-08-13 23:19:55 +0000 | [diff] [blame] | 734 | "decompress(string,[wbites],[bufsize]) -- Decompresses a compressed string.\n" |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 735 | "decompressobj([wbits]) -- Return a decompressor object (wbits=window buffer size).\n\n" |
| 736 | "Compressor objects support compress() and flush() methods; decompressor \n" |
| 737 | "objects support decompress() and flush()." |
| 738 | ; |
| 739 | |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 740 | void |
| 741 | PyInit_zlib() |
| 742 | { |
| 743 | PyObject *m, *d; |
Guido van Rossum | 3c54030 | 1997-06-03 22:21:03 +0000 | [diff] [blame] | 744 | m = Py_InitModule4("zlib", zlib_methods, |
| 745 | zlib_module_documentation, |
| 746 | (PyObject*)NULL,PYTHON_API_VERSION); |
Guido van Rossum | fb22156 | 1997-04-29 15:38:09 +0000 | [diff] [blame] | 747 | d = PyModule_GetDict(m); |
| 748 | ZlibError = Py_BuildValue("s", "zlib.error"); |
| 749 | PyDict_SetItemString(d, "error", ZlibError); |
| 750 | insint(d, "MAX_WBITS", MAX_WBITS); |
| 751 | insint(d, "DEFLATED", DEFLATED); |
| 752 | insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); |
| 753 | |
| 754 | if (PyErr_Occurred()) |
| 755 | Py_FatalError("can't initialize module zlib"); |
| 756 | } |