📄 zlibmodule.cpp
字号:
}
/*
const static char decomp_decompress__doc__[] =
#ifdef SYMBIAN
"";
#else
"decompress(data, max_length) -- Return a string containing the decompressed\n"
"version of the data.\n"
"\n"
"After calling this function, some of the input data may still be stored in\n"
"internal buffers for later processing.\n"
"Call the flush() method to clear these buffers.\n"
"If the max_length parameter is specified then the return value will be\n"
"no longer than max_length. Unconsumed input data will be stored in\n"
"the unconsumed_tail attribute.";
#endif */
static PyObject *
PyZlib_objdecompress(compobject *self, PyObject *args)
{
int err, inplen, old_length, length = DEFAULTALLOC;
int max_length = 0;
PyObject *RetVal;
Byte *input;
unsigned long start_total_out;
if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,
&inplen, &max_length))
return NULL;
if (max_length < 0) {
PyErr_SetString(PyExc_ValueError,
"max_length must be greater than zero");
return NULL;
}
/* limit amount of data allocated to max_length */
if (max_length && length > max_length)
length = max_length;
if (!(RetVal = PyString_FromStringAndSize(NULL, length)))
return NULL;
ENTER_ZLIB
start_total_out = self->zst.total_out;
self->zst.avail_in = inplen;
self->zst.next_in = input;
self->zst.avail_out = length;
self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);
Py_BEGIN_ALLOW_THREADS
err = inflate(&(self->zst), Z_SYNC_FLUSH);
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 max_length set, don't continue decompressing if we've already
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) == -1) {
RetVal = NULL;
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 accomodated 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;
}
if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0)
RetVal = NULL;
error:
LEAVE_ZLIB
return RetVal;
}
/*
const static char comp_flush__doc__[] =
#ifdef SYMBIAN
"";
#else
"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.\n";
#endif */
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) == -1) {
RetVal = NULL;
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;
}
if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0)
RetVal = NULL;
error:
LEAVE_ZLIB
return RetVal;
}
/*
const static char decomp_flush__doc__[] =
#ifdef SYMBIAN
"";
#else
"flush() -- Return a string containing any remaining decompressed data.\n"
"\n"
"The decompressor object can no longer be used after this call.";
#endif */
static PyObject *
PyZlib_unflush(compobject *self, PyObject *args)
/*decompressor flush is a no-op because all pending data would have been
flushed by the decompress method. However, this routine previously called
inflateEnd, causing any further decompress or flush calls to raise
exceptions. This behaviour has been preserved.*/
{
int err;
PyObject * retval = NULL;
if (!PyArg_ParseTuple(args, ""))
return NULL;
ENTER_ZLIB
err = inflateEnd(&(self->zst));
if (err != Z_OK)
zlib_error(self->zst, err, "from inflateEnd()");
else {
self->is_initialised = 0;
retval = PyString_FromStringAndSize(NULL, 0);
}
LEAVE_ZLIB
return retval;
}
const static PyMethodDef comp_methods[] =
{
{"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,
NULL},
{"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
NULL},
{NULL, NULL}
};
const static PyMethodDef Decomp_methods[] =
{
{"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,
NULL},
{"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
NULL},
{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((PyMethodDef *)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((PyMethodDef *)Decomp_methods, (PyObject *)self, name);
LEAVE_ZLIB
return retval;
}
/*
const static char adler32__doc__[] =
#ifdef SYMBIAN
"";
#else
"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.";
#endif */
static PyObject *
PyZlib_adler32(PyObject *self, PyObject *args)
{
uLong adler32val = adler32(0L, Z_NULL, 0);
Byte *buf;
int len;
if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val))
return NULL;
adler32val = adler32(adler32val, buf, len);
return PyInt_FromLong(adler32val);
}
/*
const static char crc32__doc__[] =
#ifdef SYMBIAN
"";
#else
"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.";
#endif */
static PyObject *
PyZlib_crc32(PyObject *self, PyObject *args)
{
uLong crc32val = crc32(0L, Z_NULL, 0);
Byte *buf;
int len;
if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val))
return NULL;
crc32val = crc32(crc32val, buf, len);
return PyInt_FromLong(crc32val);
}
static const PyMethodDef zlib_methods[] =
{
{"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
NULL},
{"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
NULL},
{"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,
NULL},
{"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,
NULL},
{"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,
NULL},
{"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,
NULL},
{NULL, NULL}
};
const statichere 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*/
};
const statichere 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*/
};
/*
const static char zlib_module_documentation[]=
#ifdef SYMBIAN
"";
#else
"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().";
#endif */
extern "C" {
DL_EXPORT(void)
PyInit_zlib(void)
{
PyObject *m, *d, *ver;
// Comptype.ob_type = &PyType_Type;
PyTypeObject* comp_type = PyObject_New(PyTypeObject, &PyType_Type);
*comp_type = Comptype;
comp_type->ob_type = &PyType_Type;
// Decomptype.ob_type = &PyType_Type;
PyTypeObject* decomp_type = PyObject_New(PyTypeObject, &PyType_Type);
*decomp_type = Decomptype;
decomp_type->ob_type = &PyType_Type;
SPyAddGlobalString("CompType", (PyObject*)comp_type);
SPyAddGlobalString("DecompType", (PyObject*)decomp_type);
m = Py_InitModule4("zlib", (PyMethodDef *)zlib_methods, NULL, (PyObject*)NULL,PYTHON_API_VERSION);
d = PyModule_GetDict(m);
ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
if (ZlibError != NULL)
PyDict_SetItemString(d, "error", ZlibError);
PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);
PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);
PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);
PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);
PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);
PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);
PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);
PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);
PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);
PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);
PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);
ver = PyString_FromString(ZLIB_VERSION);
if (ver != NULL) {
PyDict_SetItemString(d, "ZLIB_VERSION", ver);
Py_DECREF(ver);
}
#ifdef WITH_THREAD
zlib_lock = PyThread_allocate_lock();
#endif // WITH_THREAD
}
} /* extern "C" */
#ifndef EKA2
GLDEF_C TInt E32Dll(TDllReason)
{
return KErrNone;
}
#endif /*EKA2*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -