Commit 9714f99d authored by Jeremy Hylton's avatar Jeremy Hylton

Reformat!

Consistently indent 4 spaces.
Use whitespace around operators.
Put braces in the right places.
parent 3c28863e
...@@ -75,11 +75,11 @@ static PyObject *ZlibError; ...@@ -75,11 +75,11 @@ static PyObject *ZlibError;
typedef struct typedef struct
{ {
PyObject_HEAD PyObject_HEAD
z_stream zst; z_stream zst;
PyObject *unused_data; PyObject *unused_data;
PyObject *unconsumed_tail; PyObject *unconsumed_tail;
int is_initialised; int is_initialised;
} compobject; } compobject;
static char compressobj__doc__[] = static char compressobj__doc__[] =
...@@ -95,22 +95,22 @@ static char decompressobj__doc__[] = ...@@ -95,22 +95,22 @@ static char decompressobj__doc__[] =
static compobject * static compobject *
newcompobject(PyTypeObject *type) newcompobject(PyTypeObject *type)
{ {
compobject *self; compobject *self;
self = PyObject_New(compobject, type); self = PyObject_New(compobject, type);
if (self == NULL) if (self == NULL)
return NULL; return NULL;
self->is_initialised = 0; self->is_initialised = 0;
self->unused_data = PyString_FromString(""); self->unused_data = PyString_FromString("");
if (self->unused_data == NULL) { if (self->unused_data == NULL) {
Py_DECREF(self); Py_DECREF(self);
return NULL; return NULL;
} }
self->unconsumed_tail = PyString_FromString(""); self->unconsumed_tail = PyString_FromString("");
if (self->unconsumed_tail == NULL) { if (self->unconsumed_tail == NULL) {
Py_DECREF(self); Py_DECREF(self);
return NULL; return NULL;
} }
return self; return self;
} }
static char compress__doc__[] = static char compress__doc__[] =
...@@ -123,62 +123,61 @@ static char compress__doc__[] = ...@@ -123,62 +123,61 @@ static char compress__doc__[] =
static PyObject * static PyObject *
PyZlib_compress(PyObject *self, PyObject *args) PyZlib_compress(PyObject *self, PyObject *args)
{ {
PyObject *ReturnVal = NULL; PyObject *ReturnVal = NULL;
Byte *input, *output; Byte *input, *output;
int length, level=Z_DEFAULT_COMPRESSION, err; int length, level=Z_DEFAULT_COMPRESSION, err;
z_stream zst; z_stream zst;
int return_error; int return_error;
PyObject * inputString; PyObject * inputString;
/* require Python string object, optional 'level' arg */ /* require Python string object, optional 'level' arg */
if (!PyArg_ParseTuple(args, "S|i:compress", &inputString, &level)) if (!PyArg_ParseTuple(args, "S|i:compress", &inputString, &level))
return NULL; return NULL;
/* now get a pointer to the internal string */ /* now get a pointer to the internal string */
if (PyString_AsStringAndSize(inputString, (char**)&input, &length) == -1) if (PyString_AsStringAndSize(inputString, (char**)&input, &length) == -1)
return NULL; return NULL;
zst.avail_out = length + length/1000 + 12 + 1; zst.avail_out = length + length/1000 + 12 + 1;
output=(Byte*)malloc(zst.avail_out); output=(Byte*)malloc(zst.avail_out);
if (output==NULL) if (output==NULL)
{ {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data"); "Can't allocate memory to compress data");
free(output); free(output);
return NULL; return NULL;
} }
/* Past the point of no return. From here on out, we need to make sure /* Past the point of no return. From here on out, we need to make sure
we clean up mallocs & INCREFs. */ we clean up mallocs & INCREFs. */
Py_INCREF(inputString); /* increment so that we hold ref */ Py_INCREF(inputString); /* increment so that we hold ref */
zst.zalloc=(alloc_func)NULL; zst.zalloc=(alloc_func)NULL;
zst.zfree=(free_func)Z_NULL; zst.zfree=(free_func)Z_NULL;
zst.next_out=(Byte *)output; zst.next_out=(Byte *)output;
zst.next_in =(Byte *)input; zst.next_in =(Byte *)input;
zst.avail_in=length; zst.avail_in=length;
err=deflateInit(&zst, level); err=deflateInit(&zst, level);
return_error = 0; return_error = 0;
switch(err) switch(err)
{ {
case(Z_OK): case(Z_OK):
break; break;
case(Z_MEM_ERROR): case(Z_MEM_ERROR):
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"Out of memory while compressing data"); "Out of memory while compressing data");
return_error = 1; return_error = 1;
break; break;
case(Z_STREAM_ERROR): case(Z_STREAM_ERROR):
PyErr_SetString(ZlibError, PyErr_SetString(ZlibError,
"Bad compression level"); "Bad compression level");
return_error = 1; return_error = 1;
break; break;
default: default: {
{
if (zst.msg == Z_NULL) if (zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i while compressing data", PyErr_Format(ZlibError, "Error %i while compressing data",
err); err);
...@@ -187,57 +186,55 @@ PyZlib_compress(PyObject *self, PyObject *args) ...@@ -187,57 +186,55 @@ PyZlib_compress(PyObject *self, PyObject *args)
err, zst.msg); err, zst.msg);
deflateEnd(&zst); deflateEnd(&zst);
return_error = 1; return_error = 1;
} }
} }
if (!return_error) { if (!return_error) {
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS;
err=deflate(&zst, Z_FINISH); err=deflate(&zst, Z_FINISH);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS;
switch(err) switch(err)
{
case(Z_STREAM_END):
break;
/* Are there other errors to be trapped here? */
default:
{ {
if (zst.msg == Z_NULL) case(Z_STREAM_END):
PyErr_Format(ZlibError, "Error %i while compressing data", break;
err); /* Are there other errors to be trapped here? */
else default: {
PyErr_Format(ZlibError, "Error %i while compressing data: %.200s", if (zst.msg == Z_NULL)
err, zst.msg); PyErr_Format(ZlibError, "Error %i while compressing data",
err);
else
PyErr_Format(ZlibError, "Error %i while compressing data: %.200s",
err, zst.msg);
deflateEnd(&zst); deflateEnd(&zst);
return_error = 1; return_error = 1;
}
} }
}
if (!return_error) { if (!return_error) {
err=deflateEnd(&zst); err=deflateEnd(&zst);
if (err == Z_OK) if (err == Z_OK)
ReturnVal = PyString_FromStringAndSize((char *)output, zst.total_out); ReturnVal = PyString_FromStringAndSize((char *)output,
else { zst.total_out);
{ else {
if (zst.msg == Z_NULL) if (zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i while finishing compression", PyErr_Format(ZlibError,
err); "Error %i while finishing compression",
else err);
PyErr_Format(ZlibError, else
"Error %i while finishing compression: %.200s", PyErr_Format(ZlibError,
err, zst.msg); "Error %i while finishing compression: %.200s",
err, zst.msg);
}
} }
}
} }
}
free(output); free(output);
Py_DECREF(inputString); Py_DECREF(inputString);
return ReturnVal; return ReturnVal;
} }
static char decompress__doc__[] = static char decompress__doc__[] =
...@@ -249,54 +246,52 @@ static char decompress__doc__[] = ...@@ -249,54 +246,52 @@ static char decompress__doc__[] =
static PyObject * static PyObject *
PyZlib_decompress(PyObject *self, PyObject *args) PyZlib_decompress(PyObject *self, PyObject *args)
{ {
PyObject *result_str; PyObject *result_str;
Byte *input; Byte *input;
int length, err; int length, err;
int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC; int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
z_stream zst; z_stream zst;
int return_error; int return_error;
PyObject * inputString; PyObject * inputString;
if (!PyArg_ParseTuple(args, "S|ii:decompress", &inputString, &wsize, &r_strlen)) if (!PyArg_ParseTuple(args, "S|ii:decompress",
return NULL; &inputString, &wsize, &r_strlen))
if (PyString_AsStringAndSize(inputString, (char**)&input, &length) == -1) return NULL;
return NULL; if (PyString_AsStringAndSize(inputString, (char**)&input, &length) == -1)
return NULL;
if (r_strlen <= 0)
r_strlen = 1;
zst.avail_in=length;
zst.avail_out=r_strlen;
if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))
{
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to decompress data");
return NULL;
}
/* Past the point of no return. From here on out, we need to make sure if (r_strlen <= 0)
we clean up mallocs & INCREFs. */ r_strlen = 1;
Py_INCREF(inputString); /* increment so that we hold ref */ zst.avail_in = length;
zst.avail_out = r_strlen;
zst.zalloc=(alloc_func)NULL; if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen))) {
zst.zfree=(free_func)Z_NULL; PyErr_SetString(PyExc_MemoryError,
zst.next_out=(Byte *)PyString_AsString(result_str); "Can't allocate memory to decompress data");
zst.next_in =(Byte *)input; return NULL;
err=inflateInit2(&zst, wsize); }
return_error = 0; /* Past the point of no return. From here on out, we need to make sure
switch(err) we clean up mallocs & INCREFs. */
{
Py_INCREF(inputString); /* increment so that we hold ref */
zst.zalloc = (alloc_func)NULL;
zst.zfree = (free_func)Z_NULL;
zst.next_out = (Byte *)PyString_AsString(result_str);
zst.next_in = (Byte *)input;
err = inflateInit2(&zst, wsize);
return_error = 0;
switch(err) {
case(Z_OK): case(Z_OK):
break; break;
case(Z_MEM_ERROR): case(Z_MEM_ERROR):
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"Out of memory while decompressing data"); "Out of memory while decompressing data");
return_error = 1; return_error = 1;
default: default: {
{
if (zst.msg == Z_NULL) if (zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i preparing to decompress data", PyErr_Format(ZlibError, "Error %i preparing to decompress data",
err); err);
...@@ -307,21 +302,19 @@ PyZlib_decompress(PyObject *self, PyObject *args) ...@@ -307,21 +302,19 @@ PyZlib_decompress(PyObject *self, PyObject *args)
inflateEnd(&zst); inflateEnd(&zst);
return_error = 1; return_error = 1;
} }
} }
do do {
{ if (return_error)
if (return_error) break;
break;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
err=inflate(&zst, Z_FINISH); err=inflate(&zst, Z_FINISH);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
switch(err) switch(err) {
{ case(Z_STREAM_END):
case(Z_STREAM_END):
break; break;
case(Z_BUF_ERROR): case(Z_BUF_ERROR):
/* /*
...@@ -330,69 +323,66 @@ PyZlib_decompress(PyObject *self, PyObject *args) ...@@ -330,69 +323,66 @@ PyZlib_decompress(PyObject *self, PyObject *args)
* process the inflate call() due to an error in the data. * process the inflate call() due to an error in the data.
*/ */
if (zst.avail_out > 0) if (zst.avail_out > 0)
{ {
PyErr_Format(ZlibError, "Error %i while decompressing data", PyErr_Format(ZlibError, "Error %i while decompressing data",
err); err);
inflateEnd(&zst); inflateEnd(&zst);
return_error = 1; return_error = 1;
break; break;
} }
/* fall through */ /* fall through */
case(Z_OK): case(Z_OK):
/* need more memory */ /* need more memory */
if (_PyString_Resize(&result_str, r_strlen << 1) == -1) if (_PyString_Resize(&result_str, r_strlen << 1) == -1) {
{ PyErr_SetString(PyExc_MemoryError,
PyErr_SetString(PyExc_MemoryError, "Out of memory while decompressing data");
"Out of memory while decompressing data"); inflateEnd(&zst);
inflateEnd(&zst); result_str = NULL;
result_str = NULL; return_error = 1;
return_error = 1; }
} zst.next_out = (unsigned char *)PyString_AsString(result_str) \
zst.next_out = (unsigned char *)PyString_AsString(result_str) + r_strlen; + r_strlen;
zst.avail_out=r_strlen; zst.avail_out = r_strlen;
r_strlen = r_strlen << 1; r_strlen = r_strlen << 1;
break; break;
default: default: {
{ if (zst.msg == Z_NULL)
if (zst.msg == Z_NULL) PyErr_Format(ZlibError, "Error %i while decompressing data",
PyErr_Format(ZlibError, "Error %i while decompressing data", err);
err); else
else PyErr_Format(ZlibError,
PyErr_Format(ZlibError, "Error %i while decompressing data: %.200s",
"Error %i while decompressing data: %.200s", err, zst.msg);
err, zst.msg); inflateEnd(&zst);
inflateEnd(&zst);
return_error = 1; return_error = 1;
} }
} }
} while(err!=Z_STREAM_END); } while (err != Z_STREAM_END);
if (!return_error) { if (!return_error) {
err=inflateEnd(&zst); err = inflateEnd(&zst);
if (err!=Z_OK) if (err != Z_OK) {
{ if (zst.msg == Z_NULL)
if (zst.msg == Z_NULL) PyErr_Format(ZlibError,
PyErr_Format(ZlibError, "Error %i while finishing data decompression",
"Error %i while finishing data decompression", err);
err); else
else PyErr_Format(ZlibError,
PyErr_Format(ZlibError, "Error %i while finishing data decompression: %.200s",
"Error %i while finishing data decompression: %.200s", err, zst.msg);
err, zst.msg);
return_error = 1; return_error = 1;
return NULL; return NULL;
}
} }
}
if (!return_error) if (!return_error)
_PyString_Resize(&result_str, zst.total_out); _PyString_Resize(&result_str, zst.total_out);
else { else
Py_XDECREF(result_str); /* sets result_str == NULL, if not already */ Py_XDECREF(result_str); /* sets result_str == NULL, if not already */
} Py_DECREF(inputString);
Py_DECREF(inputString);
return result_str; return result_str;
} }
static PyObject * static PyObject *
...@@ -526,84 +516,87 @@ static char comp_compress__doc__[] = ...@@ -526,84 +516,87 @@ static char comp_compress__doc__[] =
static PyObject * static PyObject *
PyZlib_objcompress(compobject *self, PyObject *args) PyZlib_objcompress(compobject *self, PyObject *args)
{ {
int err, inplen, length = DEFAULTALLOC; int err, inplen, length = DEFAULTALLOC;
PyObject *RetVal; PyObject *RetVal;
Byte *input; Byte *input;
unsigned long start_total_out; unsigned long start_total_out;
int return_error; int return_error;
PyObject * inputString; PyObject * inputString;
if (!PyArg_ParseTuple(args, "S:compress", &inputString)) if (!PyArg_ParseTuple(args, "S:compress", &inputString))
return NULL; return NULL;
if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -1) if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -1)
return NULL; return NULL;
if (!(RetVal = PyString_FromStringAndSize(NULL, length))) { if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data"); "Can't allocate memory to compress data");
return NULL; return NULL;
} }
ENTER_ZLIB ENTER_ZLIB
Py_INCREF(inputString); Py_INCREF(inputString);
start_total_out = self->zst.total_out; start_total_out = self->zst.total_out;
self->zst.avail_in = inplen; self->zst.avail_in = inplen;
self->zst.next_in = input; self->zst.next_in = input;
self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), Z_NO_FLUSH);
Py_END_ALLOW_THREADS
return_error = 0;
/* 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) == -1) {
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data");
return_error = 1;
break;
}
self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
self->zst.avail_out = length; self->zst.avail_out = length;
length = length << 1; self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), Z_NO_FLUSH); err = deflate(&(self->zst), Z_NO_FLUSH);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
}
/* We will only get Z_BUF_ERROR if the output buffer was full but there return_error = 0;
wasn't more output when we tried again, so it is not an error condition */
/* while Z_OK and the output buffer is full, there might be more output,
if (!return_error) { so extend the output buffer and try again */
if (err != Z_OK && err != Z_BUF_ERROR) { while (err == Z_OK && self->zst.avail_out == 0) {
if (self->zst.msg == Z_NULL) if (_PyString_Resize(&RetVal, length << 1) == -1) {
PyErr_Format(ZlibError, "Error %i while compressing", PyErr_SetString(PyExc_MemoryError,
err); "Can't allocate memory to compress data");
else return_error = 1;
PyErr_Format(ZlibError, "Error %i while compressing: %.200s", break;
err, self->zst.msg); }
self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) \
return_error = 1; + length;
Py_DECREF(RetVal); self->zst.avail_out = length;
length = length << 1;
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), Z_NO_FLUSH);
Py_END_ALLOW_THREADS
} }
} /* 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.
*/
if (return_error) if (!return_error) {
RetVal = NULL; /* should have been handled by DECREF */ if (err != Z_OK && err != Z_BUF_ERROR) {
else if (self->zst.msg == Z_NULL)
_PyString_Resize(&RetVal, self->zst.total_out - start_total_out); PyErr_Format(ZlibError, "Error %i while compressing",
err);
else
PyErr_Format(ZlibError, "Error %i while compressing: %.200s",
err, self->zst.msg);
return_error = 1;
Py_DECREF(RetVal);
}
}
if (return_error)
RetVal = NULL; /* should have been handled by DECREF */
else
_PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
Py_DECREF(inputString); Py_DECREF(inputString);
LEAVE_ZLIB LEAVE_ZLIB
return RetVal; return RetVal;
} }
static char decomp_decompress__doc__[] = static char decomp_decompress__doc__[] =
...@@ -620,130 +613,134 @@ static char decomp_decompress__doc__[] = ...@@ -620,130 +613,134 @@ static char decomp_decompress__doc__[] =
static PyObject * static PyObject *
PyZlib_objdecompress(compobject *self, PyObject *args) PyZlib_objdecompress(compobject *self, PyObject *args)
{ {
int err, inplen, old_length, length = DEFAULTALLOC; int err, inplen, old_length, length = DEFAULTALLOC;
int max_length = 0; int max_length = 0;
PyObject *RetVal; PyObject *RetVal;
Byte *input; Byte *input;
unsigned long start_total_out; unsigned long start_total_out;
int return_error; int return_error;
PyObject * inputString; PyObject * inputString;
if (!PyArg_ParseTuple(args, "S|i:decompress", &inputString, &max_length)) if (!PyArg_ParseTuple(args, "S|i:decompress", &inputString, &max_length))
return NULL; return NULL;
if (max_length < 0) { if (max_length < 0) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"max_length must be greater than zero"); "max_length must be greater than zero");
return NULL; return NULL;
} }
if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -1) if (PyString_AsStringAndSize(inputString, (char**)&input, &inplen) == -1)
return NULL; return NULL;
/* limit amount of data allocated to max_length */ /* limit amount of data allocated to max_length */
if (max_length && length > max_length) if (max_length && length > max_length)
length = max_length; length = max_length;
if (!(RetVal = PyString_FromStringAndSize(NULL, length))) { if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data"); "Can't allocate memory to compress data");
return NULL; return NULL;
} }
ENTER_ZLIB ENTER_ZLIB
return_error = 0; return_error = 0;
Py_INCREF(inputString); Py_INCREF(inputString);
start_total_out = self->zst.total_out; start_total_out = self->zst.total_out;
self->zst.avail_in = inplen; self->zst.avail_in = inplen;
self->zst.next_in = input; self->zst.next_in = input;
self->zst.avail_out = length; self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyString_AsString(RetVal); self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
err = inflate(&(self->zst), Z_SYNC_FLUSH); err = inflate(&(self->zst), Z_SYNC_FLUSH);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
/* While Z_OK and the output buffer is full, there might be more output. /* While Z_OK and the output buffer is full, there might be more output.
So extend the output buffer and try again. So extend the output buffer and try again.
*/
while (err == Z_OK && self->zst.avail_out == 0) {
/* If max_length set, don't continue decompressing if we've already
reached the limit.
*/ */
if (max_length && length >= max_length) while (err == Z_OK && self->zst.avail_out == 0) {
break; /* If max_length set, don't continue decompressing if we've already
reached the limit.
*/
if (max_length && length >= max_length)
break;
/* otherwise, ... */ /* otherwise, ... */
old_length = length; old_length = length;
length = length << 1; length = length << 1;
if (max_length && length > max_length) if (max_length && length > max_length)
length = max_length; length = max_length;
if (_PyString_Resize(&RetVal, length) == -1) { if (_PyString_Resize(&RetVal, length) == -1) {
PyErr_SetString(PyExc_MemoryError, PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data"); "Can't allocate memory to compress data");
return_error = 1; return_error = 1;
break; break;
} }
self->zst.next_out = (unsigned char *)PyString_AsString(RetVal)+old_length; self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) \
self->zst.avail_out = length - old_length; + 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 accomodated in the output buffer Py_BEGIN_ALLOW_THREADS
of specified size. Return the unconsumed tail in an attribute.*/ err = inflate(&(self->zst), Z_SYNC_FLUSH);
if(max_length) { Py_END_ALLOW_THREADS
Py_DECREF(self->unconsumed_tail); }
self->unconsumed_tail = PyString_FromStringAndSize(self->zst.next_in,
self->zst.avail_in);
if(!self->unconsumed_tail)
return_error = 1;
}
/* The end of the compressed data has been reached, so set the unused_data /* Not all of the compressed data could be accomodated in the output buffer
attribute to a string containing the remainder of the data in the string. of specified size. Return the unconsumed tail in an attribute.*/
Note that this is also a logical place to call inflateEnd, but the old if(max_length) {
behaviour of only calling it on flush() is preserved.*/ Py_DECREF(self->unconsumed_tail);
if (!return_error) { self->unconsumed_tail = PyString_FromStringAndSize(self->zst.next_in,
if (err == Z_STREAM_END) { self->zst.avail_in);
Py_XDECREF(self->unused_data); /* Free the original, empty string */ if(!self->unconsumed_tail)
self->unused_data = PyString_FromStringAndSize((char *)self->zst.next_in, return_error = 1;
self->zst.avail_in);
if (self->unused_data == NULL) {
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to unused_data");
Py_DECREF(RetVal);
return_error = 1;
} }
/* 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 /* The end of the compressed data has been reached, so set the
condition */ unused_data attribute to a string containing the remainder of the
} else if (err != Z_OK && err != Z_BUF_ERROR) { data in the string. Note that this is also a logical place to call
if (self->zst.msg == Z_NULL) inflateEnd, but the old behaviour of only calling it on flush() is
PyErr_Format(ZlibError, "Error %i while decompressing", preserved.
err); */
else if (!return_error) {
PyErr_Format(ZlibError, "Error %i while decompressing: %.200s", if (err == Z_STREAM_END) {
err, self->zst.msg); Py_XDECREF(self->unused_data); /* Free original empty string */
Py_DECREF(RetVal); self->unused_data = PyString_FromStringAndSize(
return_error = 1; (char *)self->zst.next_in, self->zst.avail_in);
if (self->unused_data == NULL) {
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to unused_data");
Py_DECREF(RetVal);
return_error = 1;
}
/* 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) {
if (self->zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i while decompressing",
err);
else
PyErr_Format(ZlibError, "Error %i while decompressing: %.200s",
err, self->zst.msg);
Py_DECREF(RetVal);
return_error = 1;
}
} }
}
if (!return_error) { if (!return_error) {
_PyString_Resize(&RetVal, self->zst.total_out - start_total_out); _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
} }
else else
RetVal = NULL; /* should be handled by DECREF */ RetVal = NULL; /* should be handled by DECREF */
Py_DECREF(inputString); Py_DECREF(inputString);
LEAVE_ZLIB LEAVE_ZLIB
return RetVal; return RetVal;
} }
static char comp_flush__doc__[] = static char comp_flush__doc__[] =
...@@ -757,101 +754,103 @@ static char comp_flush__doc__[] = ...@@ -757,101 +754,103 @@ static char comp_flush__doc__[] =
static PyObject * static PyObject *
PyZlib_flush(compobject *self, PyObject *args) PyZlib_flush(compobject *self, PyObject *args)
{ {
int err, length=DEFAULTALLOC; int err, length = DEFAULTALLOC;
PyObject *RetVal; PyObject *RetVal;
int flushmode = Z_FINISH; int flushmode = Z_FINISH;
unsigned long start_total_out; unsigned long start_total_out;
int return_error; int return_error;
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))) {
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data");
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_AsString(RetVal);
Py_BEGIN_ALLOW_THREADS if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))
err = deflate(&(self->zst), flushmode); return NULL;
Py_END_ALLOW_THREADS
return_error = 0; /* 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);
}
/* while Z_OK and the output buffer is full, there might be more output, if (!(RetVal = PyString_FromStringAndSize(NULL, length))) {
so extend the output buffer and try again */ PyErr_SetString(PyExc_MemoryError,
while (err == Z_OK && self->zst.avail_out == 0) { "Can't allocate memory to compress data");
if (_PyString_Resize(&RetVal, length << 1) == -1) { return NULL;
PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data");
return_error = 1;
break;
} }
self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) + length;
ENTER_ZLIB
start_total_out = self->zst.total_out;
self->zst.avail_in = 0;
self->zst.avail_out = length; self->zst.avail_out = length;
length = length << 1; self->zst.next_out = (unsigned char *)PyString_AsString(RetVal);
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), flushmode); err = deflate(&(self->zst), flushmode);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
}
/* If flushmode is Z_FINISH, we also have to call deflateEnd() to free return_error = 0;
various data structures. Note we should only get Z_STREAM_END when
flushmode is Z_FINISH, but checking both for safety*/
if (!return_error) {
if (err == Z_STREAM_END && flushmode == Z_FINISH) {
err=deflateEnd(&(self->zst));
if (err!=Z_OK) {
if (self->zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i from deflateEnd()",
err);
else
PyErr_Format(ZlibError,"Error %i from deflateEnd(): %.200s",
err, self->zst.msg);
Py_DECREF(RetVal); /* while Z_OK and the output buffer is full, there might be more output,
return_error = 1; so extend the output buffer and try again */
} while (err == Z_OK && self->zst.avail_out == 0) {
else if (_PyString_Resize(&RetVal, length << 1) == -1) {
self->is_initialised = 0; PyErr_SetString(PyExc_MemoryError,
"Can't allocate memory to compress data");
return_error = 1;
break;
}
self->zst.next_out = (unsigned char *)PyString_AsString(RetVal) \
+ length;
self->zst.avail_out = length;
length = length << 1;
Py_BEGIN_ALLOW_THREADS
err = deflate(&(self->zst), flushmode);
Py_END_ALLOW_THREADS
}
/* We will only get Z_BUF_ERROR if the output buffer was full but there /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
wasn't more output when we tried again, so it is not an error various data structures. Note we should only get Z_STREAM_END when
condition */ flushmode is Z_FINISH, but checking both for safety*/
} else if (err!=Z_OK && err!=Z_BUF_ERROR) { if (!return_error) {
if (self->zst.msg == Z_NULL) if (err == Z_STREAM_END && flushmode == Z_FINISH) {
PyErr_Format(ZlibError, "Error %i while flushing", err = deflateEnd(&(self->zst));
err); if (err != Z_OK) {
else if (self->zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i while flushing: %.200s", PyErr_Format(ZlibError, "Error %i from deflateEnd()",
err, self->zst.msg); err);
Py_DECREF(RetVal); else
return_error = 1; PyErr_Format(ZlibError,
"Error %i from deflateEnd(): %.200s",
err, self->zst.msg);
Py_DECREF(RetVal);
return_error = 1;
} 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) {
if (self->zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i while flushing",
err);
else
PyErr_Format(ZlibError, "Error %i while flushing: %.200s",
err, self->zst.msg);
Py_DECREF(RetVal);
return_error = 1;
}
} }
}
if (!return_error) if (!return_error)
_PyString_Resize(&RetVal, self->zst.total_out - start_total_out); _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);
else else
RetVal = NULL; /* should have been handled by DECREF */ RetVal = NULL; /* should have been handled by DECREF */
LEAVE_ZLIB LEAVE_ZLIB
return RetVal; return RetVal;
} }
static char decomp_flush__doc__[] = static char decomp_flush__doc__[] =
...@@ -866,51 +865,50 @@ PyZlib_unflush(compobject *self, PyObject *args) ...@@ -866,51 +865,50 @@ PyZlib_unflush(compobject *self, PyObject *args)
inflateEnd, causing any further decompress or flush calls to raise inflateEnd, causing any further decompress or flush calls to raise
exceptions. This behaviour has been preserved.*/ exceptions. This behaviour has been preserved.*/
{ {
int err; int err;
PyObject * retval; PyObject * retval;
if (!PyArg_ParseTuple(args, "")) if (!PyArg_ParseTuple(args, ""))
return NULL; return NULL;
ENTER_ZLIB
err=inflateEnd(&(self->zst)); ENTER_ZLIB
if (err!=Z_OK) {
if (self->zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i from inflateEnd()",
err);
else
PyErr_Format(ZlibError, "Error %i from inflateEnd(): %.200s",
err, self->zst.msg);
retval = NULL; err = inflateEnd(&(self->zst));
if (err != Z_OK) {
if (self->zst.msg == Z_NULL)
PyErr_Format(ZlibError, "Error %i from inflateEnd()",
err);
else
PyErr_Format(ZlibError, "Error %i from inflateEnd(): %.200s",
err, self->zst.msg);
} else { retval = NULL;
self->is_initialised = 0; } else {
retval = PyString_FromStringAndSize(NULL, 0); self->is_initialised = 0;
} retval = PyString_FromStringAndSize(NULL, 0);
}
LEAVE_ZLIB LEAVE_ZLIB
return retval; return retval;
} }
static PyMethodDef comp_methods[] = static PyMethodDef comp_methods[] =
{ {
{"compress", (binaryfunc)PyZlib_objcompress, {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
METH_VARARGS, comp_compress__doc__}, comp_compress__doc__},
{"flush", (binaryfunc)PyZlib_flush, {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
METH_VARARGS, comp_flush__doc__}, comp_flush__doc__},
{NULL, NULL} {NULL, NULL}
}; };
static PyMethodDef Decomp_methods[] = static PyMethodDef Decomp_methods[] =
{ {
{"decompress", (binaryfunc)PyZlib_objdecompress, {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
METH_VARARGS, decomp_decompress__doc__}, decomp_decompress__doc__},
{"flush", (binaryfunc)PyZlib_unflush, {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
METH_VARARGS, decomp_flush__doc__}, decomp_flush__doc__},
{NULL, NULL} {NULL, NULL}
}; };
static PyObject * static PyObject *
...@@ -925,26 +923,22 @@ Comp_getattr(compobject *self, char *name) ...@@ -925,26 +923,22 @@ Comp_getattr(compobject *self, char *name)
static PyObject * static PyObject *
Decomp_getattr(compobject *self, char *name) Decomp_getattr(compobject *self, char *name)
{ {
PyObject * retval; PyObject * retval;
ENTER_ZLIB ENTER_ZLIB
if (strcmp(name, "unused_data") == 0) if (strcmp(name, "unused_data") == 0) {
{ Py_INCREF(self->unused_data);
Py_INCREF(self->unused_data); retval = self->unused_data;
retval = self->unused_data; } else if (strcmp(name, "unconsumed_tail") == 0) {
} Py_INCREF(self->unconsumed_tail);
else if (strcmp(name, "unconsumed_tail") == 0) retval = self->unconsumed_tail;
{ } else
Py_INCREF(self->unconsumed_tail); retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);
retval = self->unconsumed_tail;
} LEAVE_ZLIB
else
retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name); return retval;
LEAVE_ZLIB
return retval;
} }
static char adler32__doc__[] = static char adler32__doc__[] =
...@@ -957,14 +951,12 @@ static char adler32__doc__[] = ...@@ -957,14 +951,12 @@ static char adler32__doc__[] =
static PyObject * static PyObject *
PyZlib_adler32(PyObject *self, PyObject *args) PyZlib_adler32(PyObject *self, PyObject *args)
{ {
uLong adler32val=adler32(0L, Z_NULL, 0); uLong adler32val = adler32(0L, Z_NULL, 0);
Byte *buf; Byte *buf;
int len; int len;
if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val)) if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val))
{
return NULL; return NULL;
}
adler32val = adler32(adler32val, buf, len); adler32val = adler32(adler32val, buf, len);
return PyInt_FromLong(adler32val); return PyInt_FromLong(adler32val);
} }
...@@ -979,13 +971,11 @@ static char crc32__doc__[] = ...@@ -979,13 +971,11 @@ static char crc32__doc__[] =
static PyObject * static PyObject *
PyZlib_crc32(PyObject *self, PyObject *args) PyZlib_crc32(PyObject *self, PyObject *args)
{ {
uLong crc32val=crc32(0L, Z_NULL, 0); uLong crc32val = crc32(0L, Z_NULL, 0);
Byte *buf; Byte *buf;
int len; int len;
if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val)) if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val))
{
return NULL; return NULL;
}
crc32val = crc32(crc32val, buf, len); crc32val = crc32(crc32val, buf, len);
return PyInt_FromLong(crc32val); return PyInt_FromLong(crc32val);
} }
...@@ -993,53 +983,53 @@ PyZlib_crc32(PyObject *self, PyObject *args) ...@@ -993,53 +983,53 @@ PyZlib_crc32(PyObject *self, PyObject *args)
static PyMethodDef zlib_methods[] = static PyMethodDef zlib_methods[] =
{ {
{"adler32", (PyCFunction)PyZlib_adler32, {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
METH_VARARGS, adler32__doc__}, adler32__doc__},
{"compress", (PyCFunction)PyZlib_compress, {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
METH_VARARGS, compress__doc__}, compress__doc__},
{"compressobj", (PyCFunction)PyZlib_compressobj, {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
METH_VARARGS, compressobj__doc__}, compressobj__doc__},
{"crc32", (PyCFunction)PyZlib_crc32, {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
METH_VARARGS, crc32__doc__}, crc32__doc__},
{"decompress", (PyCFunction)PyZlib_decompress, {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
METH_VARARGS, decompress__doc__}, decompress__doc__},
{"decompressobj", (PyCFunction)PyZlib_decompressobj, {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
METH_VARARGS, decompressobj__doc__}, decompressobj__doc__},
{NULL, NULL} {NULL, NULL}
}; };
statichere PyTypeObject Comptype = { statichere PyTypeObject Comptype = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, 0,
"Compress", "Compress",
sizeof(compobject), sizeof(compobject),
0, 0,
(destructor)Comp_dealloc, /*tp_dealloc*/ (destructor)Comp_dealloc, /*tp_dealloc*/
0, /*tp_print*/ 0, /*tp_print*/
(getattrfunc)Comp_getattr, /*tp_getattr*/ (getattrfunc)Comp_getattr, /*tp_getattr*/
0, /*tp_setattr*/ 0, /*tp_setattr*/
0, /*tp_compare*/ 0, /*tp_compare*/
0, /*tp_repr*/ 0, /*tp_repr*/
0, /*tp_as_number*/ 0, /*tp_as_number*/
0, /*tp_as_sequence*/ 0, /*tp_as_sequence*/
0, /*tp_as_mapping*/ 0, /*tp_as_mapping*/
}; };
statichere PyTypeObject Decomptype = { statichere PyTypeObject Decomptype = {
PyObject_HEAD_INIT(0) PyObject_HEAD_INIT(0)
0, 0,
"Decompress", "Decompress",
sizeof(compobject), sizeof(compobject),
0, 0,
(destructor)Decomp_dealloc, /*tp_dealloc*/ (destructor)Decomp_dealloc, /*tp_dealloc*/
0, /*tp_print*/ 0, /*tp_print*/
(getattrfunc)Decomp_getattr, /*tp_getattr*/ (getattrfunc)Decomp_getattr, /*tp_getattr*/
0, /*tp_setattr*/ 0, /*tp_setattr*/
0, /*tp_compare*/ 0, /*tp_compare*/
0, /*tp_repr*/ 0, /*tp_repr*/
0, /*tp_as_number*/ 0, /*tp_as_number*/
0, /*tp_as_sequence*/ 0, /*tp_as_sequence*/
0, /*tp_as_mapping*/ 0, /*tp_as_mapping*/
}; };
/* The following insint() routine was blatantly ripped off from /* The following insint() routine was blatantly ripped off from
...@@ -1080,39 +1070,39 @@ static char zlib_module_documentation[]= ...@@ -1080,39 +1070,39 @@ static char zlib_module_documentation[]=
DL_EXPORT(void) DL_EXPORT(void)
PyInit_zlib(void) PyInit_zlib(void)
{ {
PyObject *m, *d, *ver; PyObject *m, *d, *ver;
Comptype.ob_type = &PyType_Type; Comptype.ob_type = &PyType_Type;
Decomptype.ob_type = &PyType_Type; Decomptype.ob_type = &PyType_Type;
m = Py_InitModule4("zlib", zlib_methods, m = Py_InitModule4("zlib", zlib_methods,
zlib_module_documentation, zlib_module_documentation,
(PyObject*)NULL,PYTHON_API_VERSION); (PyObject*)NULL,PYTHON_API_VERSION);
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
ZlibError = PyErr_NewException("zlib.error", NULL, NULL); ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
if (ZlibError != NULL) if (ZlibError != NULL)
PyDict_SetItemString(d, "error", ZlibError); PyDict_SetItemString(d, "error", ZlibError);
insint(d, "MAX_WBITS", MAX_WBITS); PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
insint(d, "DEFLATED", DEFLATED); PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
insint(d, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
insint(d, "Z_BEST_SPEED", Z_BEST_SPEED); PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
insint(d, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION); PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
insint(d, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION); PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
insint(d, "Z_FILTERED", Z_FILTERED); PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
insint(d, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY); PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
insint(d, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
insint(d, "Z_FINISH", Z_FINISH); PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
insint(d, "Z_NO_FLUSH", Z_NO_FLUSH); PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
insint(d, "Z_SYNC_FLUSH", Z_SYNC_FLUSH); PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
insint(d, "Z_FULL_FLUSH", Z_FULL_FLUSH); PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
ver = PyString_FromString(ZLIB_VERSION); ver = PyString_FromString(ZLIB_VERSION);
if (ver != NULL) { if (ver != NULL) {
PyDict_SetItemString(d, "ZLIB_VERSION", ver); PyDict_SetItemString(d, "ZLIB_VERSION", ver);
Py_DECREF(ver); Py_DECREF(ver);
} }
#ifdef WITH_THREAD #ifdef WITH_THREAD
zlib_lock = PyThread_allocate_lock(); zlib_lock = PyThread_allocate_lock();
#endif // WITH_THREAD #endif // WITH_THREAD
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment