Python-2.7.3/Modules/zlibmodule.c

No issues found

   1 /* zlibmodule.c -- gzip-compatible data compression */
   2 /* See http://www.gzip.org/zlib/ */
   3 
   4 /* Windows users:  read Python's PCbuild\readme.txt */
   5 
   6 
   7 #include "Python.h"
   8 #include "zlib.h"
   9 
  10 #ifdef WITH_THREAD
  11 #include "pythread.h"
  12 
  13 /* #defs ripped off from _tkinter.c, even though the situation here is much
  14    simpler, because we don't have to worry about waiting for Tcl
  15    events!  And, since zlib itself is threadsafe, we don't need to worry
  16    about re-entering zlib functions.
  17 
  18    N.B.
  19 
  20    Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
  21    that modify the components of preexisting de/compress objects, it
  22    could prove to be a performance gain on multiprocessor machines if
  23    there was an de/compress object-specific lock.  However, for the
  24    moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
  25    de/compress objects.
  26  */
  27 
  28 static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */
  29 
  30 #define ENTER_ZLIB \
  31         Py_BEGIN_ALLOW_THREADS \
  32         PyThread_acquire_lock(zlib_lock, 1); \
  33         Py_END_ALLOW_THREADS
  34 
  35 #define LEAVE_ZLIB \
  36         PyThread_release_lock(zlib_lock);
  37 
  38 #else
  39 
  40 #define ENTER_ZLIB
  41 #define LEAVE_ZLIB
  42 
  43 #endif
  44 
  45 /* The following parameters are copied from zutil.h, version 0.95 */
  46 #define DEFLATED   8
  47 #if MAX_MEM_LEVEL >= 8
  48 #  define DEF_MEM_LEVEL 8
  49 #else
  50 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
  51 #endif
  52 #define DEF_WBITS MAX_WBITS
  53 
  54 /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
  55 #define DEFAULTALLOC (16*1024)
  56 #define PyInit_zlib initzlib
  57 
  58 static PyTypeObject Comptype;
  59 static PyTypeObject Decomptype;
  60 
  61 static PyObject *ZlibError;
  62 
  63 typedef struct
  64 {
  65     PyObject_HEAD
  66     z_stream zst;
  67     PyObject *unused_data;
  68     PyObject *unconsumed_tail;
  69     int is_initialised;
  70 } compobject;
  71 
  72 static void
  73 zlib_error(z_stream zst, int err, char *msg)
  74 {
  75     const char *zmsg = Z_NULL;
  76     /* In case of a version mismatch, zst.msg won't be initialized.
  77        Check for this case first, before looking at zst.msg. */
  78     if (err == Z_VERSION_ERROR)
  79         zmsg = "library version mismatch";
  80     if (zmsg == Z_NULL)
  81         zmsg = zst.msg;
  82     if (zmsg == Z_NULL) {
  83         switch (err) {
  84         case Z_BUF_ERROR:
  85             zmsg = "incomplete or truncated stream";
  86             break;
  87         case Z_STREAM_ERROR:
  88             zmsg = "inconsistent stream state";
  89             break;
  90         case Z_DATA_ERROR:
  91             zmsg = "invalid input data";
  92             break;
  93         }
  94     }
  95     if (zmsg == Z_NULL)
  96         PyErr_Format(ZlibError, "Error %d %s", err, msg);
  97     else
  98         PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
  99 }
 100 
 101 PyDoc_STRVAR(compressobj__doc__,
 102 "compressobj([level]) -- Return a compressor object.\n"
 103 "\n"
 104 "Optional arg level is the compression level, in 1-9.");
 105 
 106 PyDoc_STRVAR(decompressobj__doc__,
 107 "decompressobj([wbits]) -- Return a decompressor object.\n"
 108 "\n"
 109 "Optional arg wbits is the window buffer size.");
 110 
 111 static compobject *
 112 newcompobject(PyTypeObject *type)
 113 {
 114     compobject *self;
 115     self = PyObject_New(compobject, type);
 116     if (self == NULL)
 117         return NULL;
 118     self->is_initialised = 0;
 119     self->unused_data = PyString_FromString("");
 120     if (self->unused_data == NULL) {
 121         Py_DECREF(self);
 122         return NULL;
 123     }
 124     self->unconsumed_tail = PyString_FromString("");
 125     if (self->unconsumed_tail == NULL) {
 126         Py_DECREF(self);
 127         return NULL;
 128     }
 129     return self;
 130 }
 131 
 132 PyDoc_STRVAR(compress__doc__,
 133 "compress(string[, level]) -- Returned compressed string.\n"
 134 "\n"
 135 "Optional arg level is the compression level, in 1-9.");
 136 
 137 static PyObject *
 138 PyZlib_compress(PyObject *self, PyObject *args)
 139 {
 140     PyObject *ReturnVal = NULL;
 141     Byte *input, *output;
 142     int length, level=Z_DEFAULT_COMPRESSION, err;
 143     z_stream zst;
 144 
 145     /* require Python string object, optional 'level' arg */
 146     if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
 147         return NULL;
 148 
 149     zst.avail_out = length + length/1000 + 12 + 1;
 150 
 151     output = (Byte*)malloc(zst.avail_out);
 152     if (output == NULL) {
 153         PyErr_SetString(PyExc_MemoryError,
 154                         "Can't allocate memory to compress data");
 155         return NULL;
 156     }
 157 
 158     /* Past the point of no return.  From here on out, we need to make sure
 159        we clean up mallocs & INCREFs. */
 160 
 161     zst.zalloc = (alloc_func)NULL;
 162     zst.zfree = (free_func)Z_NULL;
 163     zst.next_out = (Byte *)output;
 164     zst.next_in = (Byte *)input;
 165     zst.avail_in = length;
 166     err = deflateInit(&zst, level);
 167 
 168     switch(err) {
 169     case(Z_OK):
 170         break;
 171     case(Z_MEM_ERROR):
 172         PyErr_SetString(PyExc_MemoryError,
 173                         "Out of memory while compressing data");
 174         goto error;
 175     case(Z_STREAM_ERROR):
 176         PyErr_SetString(ZlibError,
 177                         "Bad compression level");
 178         goto error;
 179     default:
 180         deflateEnd(&zst);
 181         zlib_error(zst, err, "while compressing data");
 182         goto error;
 183     }
 184 
 185     Py_BEGIN_ALLOW_THREADS;
 186     err = deflate(&zst, Z_FINISH);
 187     Py_END_ALLOW_THREADS;
 188 
 189     if (err != Z_STREAM_END) {
 190         zlib_error(zst, err, "while compressing data");
 191         deflateEnd(&zst);
 192         goto error;
 193     }
 194 
 195     err=deflateEnd(&zst);
 196     if (err == Z_OK)
 197         ReturnVal = PyString_FromStringAndSize((char *)output,
 198                                                zst.total_out);
 199     else
 200         zlib_error(zst, err, "while finishing compression");
 201 
 202  error:
 203     free(output);
 204 
 205     return ReturnVal;
 206 }
 207 
 208 PyDoc_STRVAR(decompress__doc__,
 209 "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
 210 "\n"
 211 "Optional arg wbits is the window buffer size.  Optional arg bufsize is\n"
 212 "the initial output buffer size.");
 213 
 214 static PyObject *
 215 PyZlib_decompress(PyObject *self, PyObject *args)
 216 {
 217     PyObject *result_str;
 218     Byte *input;
 219     int length, err;
 220     int wsize=DEF_WBITS;
 221     Py_ssize_t r_strlen=DEFAULTALLOC;
 222     z_stream zst;
 223 
 224     if (!PyArg_ParseTuple(args, "s#|in:decompress",
 225                           &input, &length, &wsize, &r_strlen))
 226         return NULL;
 227 
 228     if (r_strlen <= 0)
 229         r_strlen = 1;
 230 
 231     zst.avail_in = length;
 232     zst.avail_out = r_strlen;
 233 
 234     if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
 235         return NULL;
 236 
 237     zst.zalloc = (alloc_func)NULL;
 238     zst.zfree = (free_func)Z_NULL;
 239     zst.next_out = (Byte *)PyString_AS_STRING(result_str);
 240     zst.next_in = (Byte *)input;
 241     err = inflateInit2(&zst, wsize);
 242 
 243     switch(err) {
 244     case(Z_OK):
 245         break;
 246     case(Z_MEM_ERROR):
 247         PyErr_SetString(PyExc_MemoryError,
 248                         "Out of memory while decompressing data");
 249         goto error;
 250     default:
 251         inflateEnd(&zst);
 252         zlib_error(zst, err, "while preparing to decompress data");
 253         goto error;
 254     }
 255 
 256     do {
 257         Py_BEGIN_ALLOW_THREADS
 258         err=inflate(&zst, Z_FINISH);
 259         Py_END_ALLOW_THREADS
 260 
 261         switch(err) {
 262         case(Z_STREAM_END):
 263             break;
 264         case(Z_BUF_ERROR):
 265             /*
 266              * If there is at least 1 byte of room according to zst.avail_out
 267              * and we get this error, assume that it means zlib cannot
 268              * process the inflate call() due to an error in the data.
 269              */
 270             if (zst.avail_out > 0) {
 271                 zlib_error(zst, err, "while decompressing data");
 272                 inflateEnd(&zst);
 273                 goto error;
 274             }
 275             /* fall through */
 276         case(Z_OK):
 277             /* need more memory */
 278             if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {
 279                 inflateEnd(&zst);
 280                 goto error;
 281             }
 282             zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
 283                 + r_strlen;
 284             zst.avail_out = r_strlen;
 285             r_strlen = r_strlen << 1;
 286             break;
 287         default:
 288             inflateEnd(&zst);
 289             zlib_error(zst, err, "while decompressing data");
 290             goto error;
 291         }
 292     } while (err != Z_STREAM_END);
 293 
 294     err = inflateEnd(&zst);
 295     if (err != Z_OK) {
 296         zlib_error(zst, err, "while finishing data decompression");
 297         goto error;
 298     }
 299 
 300     _PyString_Resize(&result_str, zst.total_out);
 301     return result_str;
 302 
 303  error:
 304     Py_XDECREF(result_str);
 305     return NULL;
 306 }
 307 
 308 static PyObject *
 309 PyZlib_compressobj(PyObject *selfptr, PyObject *args)
 310 {
 311     compobject *self;
 312     int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
 313     int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;
 314 
 315     if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
 316                           &memLevel, &strategy))
 317         return NULL;
 318 
 319     self = newcompobject(&Comptype);
 320     if (self==NULL)
 321         return(NULL);
 322     self->zst.zalloc = (alloc_func)NULL;
 323     self->zst.zfree = (free_func)Z_NULL;
 324     self->zst.next_in = NULL;
 325     self->zst.avail_in = 0;
 326     err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
 327     switch(err) {
 328     case (Z_OK):
 329         self->is_initialised = 1;
 330         return (PyObject*)self;
 331     case (Z_MEM_ERROR):
 332         Py_DECREF(self);
 333         PyErr_SetString(PyExc_MemoryError,
 334                         "Can't allocate memory for compression object");
 335         return NULL;
 336     case(Z_STREAM_ERROR):
 337         Py_DECREF(self);
 338         PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
 339         return NULL;
 340     default:
 341         zlib_error(self->zst, err, "while creating compression object");
 342         Py_DECREF(self);
 343         return NULL;
 344     }
 345 }
 346 
 347 static PyObject *
 348 PyZlib_decompressobj(PyObject *selfptr, PyObject *args)
 349 {
 350     int wbits=DEF_WBITS, err;
 351     compobject *self;
 352     if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
 353         return NULL;
 354 
 355     self = newcompobject(&Decomptype);
 356     if (self == NULL)
 357         return(NULL);
 358     self->zst.zalloc = (alloc_func)NULL;
 359     self->zst.zfree = (free_func)Z_NULL;
 360     self->zst.next_in = NULL;
 361     self->zst.avail_in = 0;
 362     err = inflateInit2(&self->zst, wbits);
 363     switch(err) {
 364     case (Z_OK):
 365         self->is_initialised = 1;
 366         return (PyObject*)self;
 367     case(Z_STREAM_ERROR):
 368         Py_DECREF(self);
 369         PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
 370         return NULL;
 371     case (Z_MEM_ERROR):
 372         Py_DECREF(self);
 373         PyErr_SetString(PyExc_MemoryError,
 374                         "Can't allocate memory for decompression object");
 375         return NULL;
 376     default:
 377         zlib_error(self->zst, err, "while creating decompression object");
 378         Py_DECREF(self);
 379         return NULL;
 380     }
 381 }
 382 
 383 static void
 384 Comp_dealloc(compobject *self)
 385 {
 386     if (self->is_initialised)
 387         deflateEnd(&self->zst);
 388     Py_XDECREF(self->unused_data);
 389     Py_XDECREF(self->unconsumed_tail);
 390     PyObject_Del(self);
 391 }
 392 
 393 static void
 394 Decomp_dealloc(compobject *self)
 395 {
 396     if (self->is_initialised)
 397         inflateEnd(&self->zst);
 398     Py_XDECREF(self->unused_data);
 399     Py_XDECREF(self->unconsumed_tail);
 400     PyObject_Del(self);
 401 }
 402 
 403 PyDoc_STRVAR(comp_compress__doc__,
 404 "compress(data) -- Return a string containing data compressed.\n"
 405 "\n"
 406 "After calling this function, some of the input data may still\n"
 407 "be stored in internal buffers for later processing.\n"
 408 "Call the flush() method to clear these buffers.");
 409 
 410 
 411 static PyObject *
 412 PyZlib_objcompress(compobject *self, PyObject *args)
 413 {
 414     int err, inplen;
 415     Py_ssize_t length = DEFAULTALLOC;
 416     PyObject *RetVal;
 417     Byte *input;
 418     unsigned long start_total_out;
 419 
 420     if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
 421         return NULL;
 422 
 423     if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
 424         return NULL;
 425 
 426     ENTER_ZLIB
 427 
 428     start_total_out = self->zst.total_out;
 429     self->zst.avail_in = inplen;
 430     self->zst.next_in = input;
 431     self->zst.avail_out = length;
 432     self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
 433 
 434     Py_BEGIN_ALLOW_THREADS
 435     err = deflate(&(self->zst), Z_NO_FLUSH);
 436     Py_END_ALLOW_THREADS
 437 
 438     /* while Z_OK and the output buffer is full, there might be more output,
 439        so extend the output buffer and try again */
 440     while (err == Z_OK && self->zst.avail_out == 0) {
 441         if (_PyString_Resize(&RetVal, length << 1) < 0)
 442             goto error;
 443         self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
 444             + length;
 445         self->zst.avail_out = length;
 446         length = length << 1;
 447 
 448         Py_BEGIN_ALLOW_THREADS
 449         err = deflate(&(self->zst), Z_NO_FLUSH);
 450         Py_END_ALLOW_THREADS
 451     }
 452     /* We will only get Z_BUF_ERROR if the output buffer was full but
 453        there wasn't more output when we tried again, so it is not an error
 454        condition.
 455     */
 456 
 457     if (err != Z_OK && err != Z_BUF_ERROR) {
 458         zlib_error(self->zst, err, "while compressing");
 459         Py_DECREF(RetVal);
 460         RetVal = NULL;
 461         goto error;
 462     }
 463     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
 464 
 465  error:
 466     LEAVE_ZLIB
 467     return RetVal;
 468 }
 469 
 470 PyDoc_STRVAR(decomp_decompress__doc__,
 471 "decompress(data, max_length) -- Return a string containing the decompressed\n"
 472 "version of the data.\n"
 473 "\n"
 474 "After calling this function, some of the input data may still be stored in\n"
 475 "internal buffers for later processing.\n"
 476 "Call the flush() method to clear these buffers.\n"
 477 "If the max_length parameter is specified then the return value will be\n"
 478 "no longer than max_length.  Unconsumed input data will be stored in\n"
 479 "the unconsumed_tail attribute.");
 480 
 481 static PyObject *
 482 PyZlib_objdecompress(compobject *self, PyObject *args)
 483 {
 484     int err, inplen, max_length = 0;
 485     Py_ssize_t old_length, length = DEFAULTALLOC;
 486     PyObject *RetVal;
 487     Byte *input;
 488     unsigned long start_total_out;
 489 
 490     if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
 491                           &inplen, &max_length))
 492         return NULL;
 493     if (max_length < 0) {
 494         PyErr_SetString(PyExc_ValueError,
 495                         "max_length must be greater than zero");
 496         return NULL;
 497     }
 498 
 499     /* limit amount of data allocated to max_length */
 500     if (max_length && length > max_length)
 501         length = max_length;
 502     if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
 503         return NULL;
 504 
 505     ENTER_ZLIB
 506 
 507     start_total_out = self->zst.total_out;
 508     self->zst.avail_in = inplen;
 509     self->zst.next_in = input;
 510     self->zst.avail_out = length;
 511     self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
 512 
 513     Py_BEGIN_ALLOW_THREADS
 514     err = inflate(&(self->zst), Z_SYNC_FLUSH);
 515     Py_END_ALLOW_THREADS
 516 
 517     /* While Z_OK and the output buffer is full, there might be more output.
 518        So extend the output buffer and try again.
 519     */
 520     while (err == Z_OK && self->zst.avail_out == 0) {
 521         /* If max_length set, don't continue decompressing if we've already
 522            reached the limit.
 523         */
 524         if (max_length && length >= max_length)
 525             break;
 526 
 527         /* otherwise, ... */
 528         old_length = length;
 529         length = length << 1;
 530         if (max_length && length > max_length)
 531             length = max_length;
 532 
 533         if (_PyString_Resize(&RetVal, length) < 0)
 534             goto error;
 535         self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
 536             + old_length;
 537         self->zst.avail_out = length - old_length;
 538 
 539         Py_BEGIN_ALLOW_THREADS
 540         err = inflate(&(self->zst), Z_SYNC_FLUSH);
 541         Py_END_ALLOW_THREADS
 542     }
 543 
 544     if(max_length) {
 545         /* Not all of the compressed data could be accommodated in a buffer of
 546            the specified size. Return the unconsumed tail in an attribute. */
 547         Py_DECREF(self->unconsumed_tail);
 548         self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,
 549                                                            self->zst.avail_in);
 550     }
 551     else if (PyString_GET_SIZE(self->unconsumed_tail) > 0) {
 552         /* All of the compressed data was consumed. Clear unconsumed_tail. */
 553         Py_DECREF(self->unconsumed_tail);
 554         self->unconsumed_tail = PyString_FromStringAndSize("", 0);
 555     }
 556     if(!self->unconsumed_tail) {
 557         Py_DECREF(RetVal);
 558         RetVal = NULL;
 559         goto error;
 560     }
 561 
 562     /* The end of the compressed data has been reached, so set the
 563        unused_data attribute to a string containing the remainder of the
 564        data in the string.  Note that this is also a logical place to call
 565        inflateEnd, but the old behaviour of only calling it on flush() is
 566        preserved.
 567     */
 568     if (err == Z_STREAM_END) {
 569         Py_XDECREF(self->unused_data);  /* Free original empty string */
 570         self->unused_data = PyString_FromStringAndSize(
 571             (char *)self->zst.next_in, self->zst.avail_in);
 572         if (self->unused_data == NULL) {
 573             Py_DECREF(RetVal);
 574             goto error;
 575         }
 576         /* We will only get Z_BUF_ERROR if the output buffer was full
 577            but there wasn't more output when we tried again, so it is
 578            not an error condition.
 579         */
 580     } else if (err != Z_OK && err != Z_BUF_ERROR) {
 581         zlib_error(self->zst, err, "while decompressing");
 582         Py_DECREF(RetVal);
 583         RetVal = NULL;
 584         goto error;
 585     }
 586 
 587     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
 588 
 589  error:
 590     LEAVE_ZLIB
 591 
 592     return RetVal;
 593 }
 594 
 595 PyDoc_STRVAR(comp_flush__doc__,
 596 "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"
 597 "\n"
 598 "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"
 599 "default value used when mode is not specified is Z_FINISH.\n"
 600 "If mode == Z_FINISH, the compressor object can no longer be used after\n"
 601 "calling the flush() method.  Otherwise, more data can still be compressed.");
 602 
 603 static PyObject *
 604 PyZlib_flush(compobject *self, PyObject *args)
 605 {
 606     int err, length = DEFAULTALLOC;
 607     PyObject *RetVal;
 608     int flushmode = Z_FINISH;
 609     unsigned long start_total_out;
 610 
 611     if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
 612         return NULL;
 613 
 614     /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
 615        doing any work at all; just return an empty string. */
 616     if (flushmode == Z_NO_FLUSH) {
 617         return PyString_FromStringAndSize(NULL, 0);
 618     }
 619 
 620     if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
 621         return NULL;
 622 
 623     ENTER_ZLIB
 624 
 625     start_total_out = self->zst.total_out;
 626     self->zst.avail_in = 0;
 627     self->zst.avail_out = length;
 628     self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
 629 
 630     Py_BEGIN_ALLOW_THREADS
 631     err = deflate(&(self->zst), flushmode);
 632     Py_END_ALLOW_THREADS
 633 
 634     /* while Z_OK and the output buffer is full, there might be more output,
 635        so extend the output buffer and try again */
 636     while (err == Z_OK && self->zst.avail_out == 0) {
 637         if (_PyString_Resize(&RetVal, length << 1) < 0)
 638             goto error;
 639         self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \
 640             + length;
 641         self->zst.avail_out = length;
 642         length = length << 1;
 643 
 644         Py_BEGIN_ALLOW_THREADS
 645         err = deflate(&(self->zst), flushmode);
 646         Py_END_ALLOW_THREADS
 647     }
 648 
 649     /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
 650        various data structures. Note we should only get Z_STREAM_END when
 651        flushmode is Z_FINISH, but checking both for safety*/
 652     if (err == Z_STREAM_END && flushmode == Z_FINISH) {
 653         err = deflateEnd(&(self->zst));
 654         if (err != Z_OK) {
 655             zlib_error(self->zst, err, "from deflateEnd()");
 656             Py_DECREF(RetVal);
 657             RetVal = NULL;
 658             goto error;
 659         }
 660         else
 661             self->is_initialised = 0;
 662 
 663         /* We will only get Z_BUF_ERROR if the output buffer was full
 664            but there wasn't more output when we tried again, so it is
 665            not an error condition.
 666         */
 667     } else if (err!=Z_OK && err!=Z_BUF_ERROR) {
 668         zlib_error(self->zst, err, "while flushing");
 669         Py_DECREF(RetVal);
 670         RetVal = NULL;
 671         goto error;
 672     }
 673 
 674     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
 675 
 676  error:
 677     LEAVE_ZLIB
 678 
 679     return RetVal;
 680 }
 681 
 682 #ifdef HAVE_ZLIB_COPY
 683 PyDoc_STRVAR(comp_copy__doc__,
 684 "copy() -- Return a copy of the compression object.");
 685 
 686 static PyObject *
 687 PyZlib_copy(compobject *self)
 688 {
 689     compobject *retval = NULL;
 690     int err;
 691 
 692     retval = newcompobject(&Comptype);
 693     if (!retval) return NULL;
 694 
 695     /* Copy the zstream state
 696      * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
 697      */
 698     ENTER_ZLIB
 699     err = deflateCopy(&retval->zst, &self->zst);
 700     switch(err) {
 701     case(Z_OK):
 702         break;
 703     case(Z_STREAM_ERROR):
 704         PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
 705         goto error;
 706     case(Z_MEM_ERROR):
 707         PyErr_SetString(PyExc_MemoryError,
 708                         "Can't allocate memory for compression object");
 709         goto error;
 710     default:
 711         zlib_error(self->zst, err, "while copying compression object");
 712         goto error;
 713     }
 714 
 715     Py_INCREF(self->unused_data);
 716     Py_INCREF(self->unconsumed_tail);
 717     Py_XDECREF(retval->unused_data);
 718     Py_XDECREF(retval->unconsumed_tail);
 719     retval->unused_data = self->unused_data;
 720     retval->unconsumed_tail = self->unconsumed_tail;
 721 
 722     /* Mark it as being initialized */
 723     retval->is_initialised = 1;
 724 
 725     LEAVE_ZLIB
 726     return (PyObject *)retval;
 727 
 728 error:
 729     LEAVE_ZLIB
 730     Py_XDECREF(retval);
 731     return NULL;
 732 }
 733 
 734 PyDoc_STRVAR(decomp_copy__doc__,
 735 "copy() -- Return a copy of the decompression object.");
 736 
 737 static PyObject *
 738 PyZlib_uncopy(compobject *self)
 739 {
 740     compobject *retval = NULL;
 741     int err;
 742 
 743     retval = newcompobject(&Decomptype);
 744     if (!retval) return NULL;
 745 
 746     /* Copy the zstream state
 747      * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
 748      */
 749     ENTER_ZLIB
 750     err = inflateCopy(&retval->zst, &self->zst);
 751     switch(err) {
 752     case(Z_OK):
 753         break;
 754     case(Z_STREAM_ERROR):
 755         PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
 756         goto error;
 757     case(Z_MEM_ERROR):
 758         PyErr_SetString(PyExc_MemoryError,
 759                         "Can't allocate memory for decompression object");
 760         goto error;
 761     default:
 762         zlib_error(self->zst, err, "while copying decompression object");
 763         goto error;
 764     }
 765 
 766     Py_INCREF(self->unused_data);
 767     Py_INCREF(self->unconsumed_tail);
 768     Py_XDECREF(retval->unused_data);
 769     Py_XDECREF(retval->unconsumed_tail);
 770     retval->unused_data = self->unused_data;
 771     retval->unconsumed_tail = self->unconsumed_tail;
 772 
 773     /* Mark it as being initialized */
 774     retval->is_initialised = 1;
 775 
 776     LEAVE_ZLIB
 777     return (PyObject *)retval;
 778 
 779 error:
 780     LEAVE_ZLIB
 781     Py_XDECREF(retval);
 782     return NULL;
 783 }
 784 #endif
 785 
 786 PyDoc_STRVAR(decomp_flush__doc__,
 787 "flush( [length] ) -- Return a string containing any remaining\n"
 788 "decompressed data. length, if given, is the initial size of the\n"
 789 "output buffer.\n"
 790 "\n"
 791 "The decompressor object can no longer be used after this call.");
 792 
 793 static PyObject *
 794 PyZlib_unflush(compobject *self, PyObject *args)
 795 {
 796     int err, length = DEFAULTALLOC;
 797     PyObject * retval = NULL;
 798     unsigned long start_total_out;
 799 
 800     if (!PyArg_ParseTuple(args, "|i:flush", &length))
 801         return NULL;
 802     if (length <= 0) {
 803         PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
 804         return NULL;
 805     }
 806     if (!(retval = PyString_FromStringAndSize(NULL, length)))
 807         return NULL;
 808 
 809 
 810     ENTER_ZLIB
 811 
 812     start_total_out = self->zst.total_out;
 813     self->zst.avail_out = length;
 814     self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
 815 
 816     Py_BEGIN_ALLOW_THREADS
 817     err = inflate(&(self->zst), Z_FINISH);
 818     Py_END_ALLOW_THREADS
 819 
 820     /* while Z_OK and the output buffer is full, there might be more output,
 821        so extend the output buffer and try again */
 822     while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
 823         if (_PyString_Resize(&retval, length << 1) < 0)
 824             goto error;
 825         self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;
 826         self->zst.avail_out = length;
 827         length = length << 1;
 828 
 829         Py_BEGIN_ALLOW_THREADS
 830         err = inflate(&(self->zst), Z_FINISH);
 831         Py_END_ALLOW_THREADS
 832     }
 833 
 834     /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
 835        various data structures. Note we should only get Z_STREAM_END when
 836        flushmode is Z_FINISH */
 837     if (err == Z_STREAM_END) {
 838         err = inflateEnd(&(self->zst));
 839         self->is_initialised = 0;
 840         if (err != Z_OK) {
 841             zlib_error(self->zst, err, "from inflateEnd()");
 842             Py_DECREF(retval);
 843             retval = NULL;
 844             goto error;
 845         }
 846     }
 847     _PyString_Resize(&retval, self->zst.total_out - start_total_out);
 848 
 849 error:
 850 
 851     LEAVE_ZLIB
 852 
 853     return retval;
 854 }
 855 
 856 static PyMethodDef comp_methods[] =
 857 {
 858     {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
 859                  comp_compress__doc__},
 860     {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
 861               comp_flush__doc__},
 862 #ifdef HAVE_ZLIB_COPY
 863     {"copy",  (PyCFunction)PyZlib_copy, METH_NOARGS,
 864               comp_copy__doc__},
 865 #endif
 866     {NULL, NULL}
 867 };
 868 
 869 static PyMethodDef Decomp_methods[] =
 870 {
 871     {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
 872                    decomp_decompress__doc__},
 873     {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
 874               decomp_flush__doc__},
 875 #ifdef HAVE_ZLIB_COPY
 876     {"copy",  (PyCFunction)PyZlib_uncopy, METH_NOARGS,
 877               decomp_copy__doc__},
 878 #endif
 879     {NULL, NULL}
 880 };
 881 
 882 static PyObject *
 883 Comp_getattr(compobject *self, char *name)
 884 {
 885   /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
 886      internal data. */
 887 
 888   return Py_FindMethod(comp_methods, (PyObject *)self, name);
 889 }
 890 
 891 static PyObject *
 892 Decomp_getattr(compobject *self, char *name)
 893 {
 894     PyObject * retval;
 895 
 896     ENTER_ZLIB
 897 
 898     if (strcmp(name, "unused_data") == 0) {
 899         Py_INCREF(self->unused_data);
 900         retval = self->unused_data;
 901     } else if (strcmp(name, "unconsumed_tail") == 0) {
 902         Py_INCREF(self->unconsumed_tail);
 903         retval = self->unconsumed_tail;
 904     } else
 905         retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
 906 
 907     LEAVE_ZLIB
 908 
 909     return retval;
 910 }
 911 
 912 PyDoc_STRVAR(adler32__doc__,
 913 "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"
 914 "\n"
 915 "An optional starting value can be specified.  The returned checksum is\n"
 916 "a signed integer.");
 917 
 918 static PyObject *
 919 PyZlib_adler32(PyObject *self, PyObject *args)
 920 {
 921     unsigned int adler32val = 1;  /* adler32(0L, Z_NULL, 0) */
 922     Byte *buf;
 923     int len, signed_val;
 924 
 925     if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))
 926         return NULL;
 927     /* In Python 2.x we return a signed integer regardless of native platform
 928      * long size (the 32bit unsigned long is treated as 32-bit signed and sign
 929      * extended into a 64-bit long inside the integer object).  3.0 does the
 930      * right thing and returns unsigned. http://bugs.python.org/issue1202 */
 931     signed_val = adler32(adler32val, buf, len);
 932     return PyInt_FromLong(signed_val);
 933 }
 934 
 935 PyDoc_STRVAR(crc32__doc__,
 936 "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"
 937 "\n"
 938 "An optional starting value can be specified.  The returned checksum is\n"
 939 "a signed integer.");
 940 
 941 static PyObject *
 942 PyZlib_crc32(PyObject *self, PyObject *args)
 943 {
 944     unsigned int crc32val = 0;  /* crc32(0L, Z_NULL, 0) */
 945     Byte *buf;
 946     int len, signed_val;
 947 
 948     if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))
 949         return NULL;
 950     /* In Python 2.x we return a signed integer regardless of native platform
 951      * long size (the 32bit unsigned long is treated as 32-bit signed and sign
 952      * extended into a 64-bit long inside the integer object).  3.0 does the
 953      * right thing and returns unsigned. http://bugs.python.org/issue1202 */
 954     signed_val = crc32(crc32val, buf, len);
 955     return PyInt_FromLong(signed_val);
 956 }
 957 
 958 
 959 static PyMethodDef zlib_methods[] =
 960 {
 961     {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
 962                 adler32__doc__},
 963     {"compress", (PyCFunction)PyZlib_compress,  METH_VARARGS,
 964                  compress__doc__},
 965     {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
 966                     compressobj__doc__},
 967     {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
 968               crc32__doc__},
 969     {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
 970                    decompress__doc__},
 971     {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
 972                    decompressobj__doc__},
 973     {NULL, NULL}
 974 };
 975 
 976 static PyTypeObject Comptype = {
 977     PyVarObject_HEAD_INIT(0, 0)
 978     "zlib.Compress",
 979     sizeof(compobject),
 980     0,
 981     (destructor)Comp_dealloc,       /*tp_dealloc*/
 982     0,                              /*tp_print*/
 983     (getattrfunc)Comp_getattr,      /*tp_getattr*/
 984     0,                              /*tp_setattr*/
 985     0,                              /*tp_compare*/
 986     0,                              /*tp_repr*/
 987     0,                              /*tp_as_number*/
 988     0,                              /*tp_as_sequence*/
 989     0,                              /*tp_as_mapping*/
 990 };
 991 
 992 static PyTypeObject Decomptype = {
 993     PyVarObject_HEAD_INIT(0, 0)
 994     "zlib.Decompress",
 995     sizeof(compobject),
 996     0,
 997     (destructor)Decomp_dealloc,     /*tp_dealloc*/
 998     0,                              /*tp_print*/
 999     (getattrfunc)Decomp_getattr,    /*tp_getattr*/
1000     0,                              /*tp_setattr*/
1001     0,                              /*tp_compare*/
1002     0,                              /*tp_repr*/
1003     0,                              /*tp_as_number*/
1004     0,                              /*tp_as_sequence*/
1005     0,                              /*tp_as_mapping*/
1006 };
1007 
1008 PyDoc_STRVAR(zlib_module_documentation,
1009 "The functions in this module allow compression and decompression using the\n"
1010 "zlib library, which is based on GNU zip.\n"
1011 "\n"
1012 "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1013 "compress(string[, level]) -- Compress string, with compression level in 1-9.\n"
1014 "compressobj([level]) -- Return a compressor object.\n"
1015 "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1016 "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1017 "decompressobj([wbits]) -- Return a decompressor object.\n"
1018 "\n"
1019 "'wbits' is window buffer size.\n"
1020 "Compressor objects support compress() and flush() methods; decompressor\n"
1021 "objects support decompress() and flush().");
1022 
1023 PyMODINIT_FUNC
1024 PyInit_zlib(void)
1025 {
1026     PyObject *m, *ver;
1027     Py_TYPE(&Comptype) = &PyType_Type;
1028     Py_TYPE(&Decomptype) = &PyType_Type;
1029     m = Py_InitModule4("zlib", zlib_methods,
1030                        zlib_module_documentation,
1031                        (PyObject*)NULL,PYTHON_API_VERSION);
1032     if (m == NULL)
1033         return;
1034 
1035     ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1036     if (ZlibError != NULL) {
1037         Py_INCREF(ZlibError);
1038         PyModule_AddObject(m, "error", ZlibError);
1039     }
1040     PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
1041     PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
1042     PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
1043     PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
1044     PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
1045     PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
1046     PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
1047     PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
1048     PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
1049 
1050     PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
1051     PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
1052     PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
1053     PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
1054 
1055     ver = PyString_FromString(ZLIB_VERSION);
1056     if (ver != NULL)
1057         PyModule_AddObject(m, "ZLIB_VERSION", ver);
1058 
1059     PyModule_AddStringConstant(m, "__version__", "1.0");
1060 
1061 #ifdef WITH_THREAD
1062     zlib_lock = PyThread_allocate_lock();
1063 #endif /* WITH_THREAD */
1064 }