cinap_lenrekadd hg and python4bb9e2215 years agoHistory
1027 lines · 28.1 KB · C
RawDownload
	   reached the limit.	*/	if (max_length && length >= max_length)	    break; 	/* otherwise, ... */	old_length = length;	length = length << 1;	if (max_length && length > max_length)	    length = max_length; 	if (_PyString_Resize(&RetVal, length) < 0)	    goto error;	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \	    + old_length;	self->zst.avail_out = length - old_length; 	Py_BEGIN_ALLOW_THREADS	err = inflate(&(self->zst), Z_SYNC_FLUSH);	Py_END_ALLOW_THREADS    }     /* Not all of the compressed data could be accommodated in the output buffer       of specified size. Return the unconsumed tail in an attribute.*/    if(max_length) {	Py_DECREF(self->unconsumed_tail);	self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in,							   self->zst.avail_in);	if(!self->unconsumed_tail) {	    Py_DECREF(RetVal);	    RetVal = NULL;	    goto error;	}    }     /* The end of the compressed data has been reached, so set the       unused_data attribute to a string containing the remainder of the       data in the string.  Note that this is also a logical place to call       inflateEnd, but the old behaviour of only calling it on flush() is       preserved.    */    if (err == Z_STREAM_END) {	Py_XDECREF(self->unused_data);  /* Free original empty string */	self->unused_data = PyString_FromStringAndSize(	    (char *)self->zst.next_in, self->zst.avail_in);	if (self->unused_data == NULL) {	    Py_DECREF(RetVal);	    goto error;	}	/* We will only get Z_BUF_ERROR if the output buffer was full	   but there wasn't more output when we tried again, so it is	   not an error condition.	*/    } else if (err != Z_OK && err != Z_BUF_ERROR) {	zlib_error(self->zst, err, "while decompressing");	Py_DECREF(RetVal);	RetVal = NULL;	goto error;    }     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);  error:    LEAVE_ZLIB     return RetVal;} PyDoc_STRVAR(comp_flush__doc__,"flush( [mode] ) -- Return a string containing any remaining compressed data.\n""\n""mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n""default value used when mode is not specified is Z_FINISH.\n""If mode == Z_FINISH, the compressor object can no longer be used after\n""calling the flush() method.  Otherwise, more data can still be compressed."); static PyObject *PyZlib_flush(compobject *self, PyObject *args){    int err, length = DEFAULTALLOC;    PyObject *RetVal;    int flushmode = Z_FINISH;    unsigned long start_total_out;     if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))	return NULL;     /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in       doing any work at all; just return an empty string. */    if (flushmode == Z_NO_FLUSH) {	return PyString_FromStringAndSize(NULL, 0);    }     if (!(RetVal = PyString_FromStringAndSize(NULL, length)))	return NULL;     ENTER_ZLIB     start_total_out = self->zst.total_out;    self->zst.avail_in = 0;    self->zst.avail_out = length;    self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);     Py_BEGIN_ALLOW_THREADS    err = deflate(&(self->zst), flushmode);    Py_END_ALLOW_THREADS     /* while Z_OK and the output buffer is full, there might be more output,       so extend the output buffer and try again */    while (err == Z_OK && self->zst.avail_out == 0) {	if (_PyString_Resize(&RetVal, length << 1) < 0)	    goto error;	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \	    + length;	self->zst.avail_out = length;	length = length << 1; 	Py_BEGIN_ALLOW_THREADS	err = deflate(&(self->zst), flushmode);	Py_END_ALLOW_THREADS    }     /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free       various data structures. Note we should only get Z_STREAM_END when       flushmode is Z_FINISH, but checking both for safety*/    if (err == Z_STREAM_END && flushmode == Z_FINISH) {	err = deflateEnd(&(self->zst));	if (err != Z_OK) {	    zlib_error(self->zst, err, "from deflateEnd()");	    Py_DECREF(RetVal);	    RetVal = NULL;	    goto error;	}	else	    self->is_initialised = 0; 	/* We will only get Z_BUF_ERROR if the output buffer was full	   but there wasn't more output when we tried again, so it is	   not an error condition.	*/    } else if (err!=Z_OK && err!=Z_BUF_ERROR) {	zlib_error(self->zst, err, "while flushing");	Py_DECREF(RetVal);	RetVal = NULL;	goto error;    }     _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);  error:    LEAVE_ZLIB     return RetVal;} #ifdef HAVE_ZLIB_COPYPyDoc_STRVAR(comp_copy__doc__,"copy() -- Return a copy of the compression object."); static PyObject *PyZlib_copy(compobject *self){    compobject *retval = NULL;    int err;     retval = newcompobject(&Comptype);    if (!retval) return NULL;     /* Copy the zstream state     * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe     */    ENTER_ZLIB    err = deflateCopy(&retval->zst, &self->zst);    switch(err) {    case(Z_OK):        break;    case(Z_STREAM_ERROR):        PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");        goto error;    case(Z_MEM_ERROR):        PyErr_SetString(PyExc_MemoryError,                        "Can't allocate memory for compression object");        goto error;    default:        zlib_error(self->zst, err, "while copying compression object");        goto error;    }     Py_INCREF(self->unused_data);    Py_INCREF(self->unconsumed_tail);    Py_XDECREF(retval->unused_data);    Py_XDECREF(retval->unconsumed_tail);    retval->unused_data = self->unused_data;    retval->unconsumed_tail = self->unconsumed_tail;     /* Mark it as being initialized */    retval->is_initialised = 1;     LEAVE_ZLIB    return (PyObject *)retval; error:    LEAVE_ZLIB    Py_XDECREF(retval);    return NULL;} PyDoc_STRVAR(decomp_copy__doc__,"copy() -- Return a copy of the decompression object."); static PyObject *PyZlib_uncopy(compobject *self){    compobject *retval = NULL;    int err;     retval = newcompobject(&Decomptype);    if (!retval) return NULL;     /* Copy the zstream state     * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe     */    ENTER_ZLIB    err = inflateCopy(&retval->zst, &self->zst);    switch(err) {    case(Z_OK):        break;    case(Z_STREAM_ERROR):        PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");        goto error;    case(Z_MEM_ERROR):        PyErr_SetString(PyExc_MemoryError,                        "Can't allocate memory for decompression object");        goto error;    default:        zlib_error(self->zst, err, "while copying decompression object");        goto error;    }     Py_INCREF(self->unused_data);    Py_INCREF(self->unconsumed_tail);    Py_XDECREF(retval->unused_data);    Py_XDECREF(retval->unconsumed_tail);    retval->unused_data = self->unused_data;    retval->unconsumed_tail = self->unconsumed_tail;     /* Mark it as being initialized */    retval->is_initialised = 1;     LEAVE_ZLIB    return (PyObject *)retval; error:    LEAVE_ZLIB    Py_XDECREF(retval);    return NULL;}#endif PyDoc_STRVAR(decomp_flush__doc__,"flush( [length] ) -- Return a string containing any remaining\n""decompressed data. length, if given, is the initial size of the\n""output buffer.\n""\n""The decompressor object can no longer be used after this call."); static PyObject *PyZlib_unflush(compobject *self, PyObject *args){    int err, length = DEFAULTALLOC;    PyObject * retval = NULL;    unsigned long start_total_out;     if (!PyArg_ParseTuple(args, "|i:flush", &length))	return NULL;    if (!(retval = PyString_FromStringAndSize(NULL, length)))	return NULL;      ENTER_ZLIB     start_total_out = self->zst.total_out;    self->zst.avail_out = length;    self->zst.next_out = (Byte *)PyString_AS_STRING(retval);     Py_BEGIN_ALLOW_THREADS    err = inflate(&(self->zst), Z_FINISH);    Py_END_ALLOW_THREADS     /* while Z_OK and the output buffer is full, there might be more output,       so extend the output buffer and try again */    while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {	if (_PyString_Resize(&retval, length << 1) < 0)	    goto error;	self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;	self->zst.avail_out = length;	length = length << 1; 	Py_BEGIN_ALLOW_THREADS	err = inflate(&(self->zst), Z_FINISH);	Py_END_ALLOW_THREADS    }     /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free       various data structures. Note we should only get Z_STREAM_END when       flushmode is Z_FINISH */    if (err == Z_STREAM_END) {	err = inflateEnd(&(self->zst));        self->is_initialised = 0;	if (err != Z_OK) {	    zlib_error(self->zst, err, "from inflateEnd()");	    Py_DECREF(retval);	    retval = NULL;	    goto error;	}    }    _PyString_Resize(&retval, self->zst.total_out - start_total_out); error:     LEAVE_ZLIB     return retval;} static PyMethodDef comp_methods[] ={    {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,                 comp_compress__doc__},    {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,              comp_flush__doc__},#ifdef HAVE_ZLIB_COPY    {"copy",  (PyCFunction)PyZlib_copy, METH_NOARGS,              comp_copy__doc__},#endif    {NULL, NULL}}; static PyMethodDef Decomp_methods[] ={    {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,                   decomp_decompress__doc__},    {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,              decomp_flush__doc__},#ifdef HAVE_ZLIB_COPY    {"copy",  (PyCFunction)PyZlib_uncopy, METH_NOARGS,              decomp_copy__doc__},#endif    {NULL, NULL}}; static PyObject *Comp_getattr(compobject *self, char *name){  /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch     internal data. */   return Py_FindMethod(comp_methods, (PyObject *)self, name);} static PyObject *Decomp_getattr(compobject *self, char *name){    PyObject * retval;     ENTER_ZLIB     if (strcmp(name, "unused_data") == 0) {	Py_INCREF(self->unused_data);	retval = self->unused_data;    } else if (strcmp(name, "unconsumed_tail") == 0) {	Py_INCREF(self->unconsumed_tail);	retval = self->unconsumed_tail;    } else	retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);     LEAVE_ZLIB     return retval;} PyDoc_STRVAR(adler32__doc__,"adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n""\n""An optional starting value can be specified.  The returned checksum is\n""an integer."); static PyObject *PyZlib_adler32(PyObject *self, PyObject *args){    uLong adler32val = adler32(0L, Z_NULL, 0);    Byte *buf;    int len;     if (!PyArg_ParseTuple(args, "s#|k:adler32", &buf, &len, &adler32val))	return NULL;    adler32val = adler32(adler32val, buf, len);    return PyInt_FromLong(adler32val);} PyDoc_STRVAR(crc32__doc__,"crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n""\n""An optional starting value can be specified.  The returned checksum is\n""an integer."); static PyObject *PyZlib_crc32(PyObject *self, PyObject *args){    uLong crc32val = crc32(0L, Z_NULL, 0);    Byte *buf;    int len;    if (!PyArg_ParseTuple(args, "s#|k:crc32", &buf, &len, &crc32val))	return NULL;    crc32val = crc32(crc32val, buf, len);    return PyInt_FromLong(crc32val);}  static PyMethodDef zlib_methods[] ={    {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,                adler32__doc__},    {"compress", (PyCFunction)PyZlib_compress,  METH_VARARGS,                 compress__doc__},    {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,                    compressobj__doc__},    {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,              crc32__doc__},    {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,                   decompress__doc__},    {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,                   decompressobj__doc__},    {NULL, NULL}}; static PyTypeObject Comptype = {    PyObject_HEAD_INIT(0)    0,    "zlib.Compress",    sizeof(compobject),    0,    (destructor)Comp_dealloc,       /*tp_dealloc*/    0,                              /*tp_print*/    (getattrfunc)Comp_getattr,      /*tp_getattr*/    0,                              /*tp_setattr*/    0,                              /*tp_compare*/    0,                              /*tp_repr*/    0,                              /*tp_as_number*/    0,                              /*tp_as_sequence*/    0,                              /*tp_as_mapping*/}; static PyTypeObject Decomptype = {    PyObject_HEAD_INIT(0)    0,    "zlib.Decompress",    sizeof(compobject),    0,    (destructor)Decomp_dealloc,     /*tp_dealloc*/    0,                              /*tp_print*/    (getattrfunc)Decomp_getattr,    /*tp_getattr*/    0,                              /*tp_setattr*/    0,                              /*tp_compare*/    0,                              /*tp_repr*/    0,                              /*tp_as_number*/    0,                              /*tp_as_sequence*/    0,                              /*tp_as_mapping*/}; PyDoc_STRVAR(zlib_module_documentation,"The functions in this module allow compression and decompression using the\n""zlib library, which is based on GNU zip.\n""\n""adler32(string[, start]) -- Compute an Adler-32 checksum.\n""compress(string[, level]) -- Compress string, with compression level in 1-9.\n""compressobj([level]) -- Return a compressor object.\n""crc32(string[, start]) -- Compute a CRC-32 checksum.\n""decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n""decompressobj([wbits]) -- Return a decompressor object.\n""\n""'wbits' is window buffer size.\n""Compressor objects support compress() and flush() methods; decompressor\n""objects support decompress() and flush()."); PyMODINIT_FUNCPyInit_zlib(void){    PyObject *m, *ver;    Comptype.ob_type = &PyType_Type;    Decomptype.ob_type = &PyType_Type;    m = Py_InitModule4("zlib", zlib_methods,		       zlib_module_documentation,		       (PyObject*)NULL,PYTHON_API_VERSION);    if (m == NULL)	return;     ZlibError = PyErr_NewException("zlib.error", NULL, NULL);    if (ZlibError != NULL) {        Py_INCREF(ZlibError);

Lines 501–1000 of 1027. Previous lines