⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zlibmodule.cpp

📁 python s60 1.4.5版本的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* zlibmodule.c -- gzip-compatible data compression */
/* See http://www.gzip.org/zlib/ */
/* Windows users:  read Python's PCbuild\readme.txt */
 /* Portions Copyright (c) 2005 - 2007 Nokia Corporation */

#include "Python.h"
#include "symbian_python_ext_util.h"

//#include <eikenv.h> 
#include <e32std.h>

//#include <zlib.h>
#include <ezlib.h>

#ifdef SYMBIAN
#undef WITH_THREAD
#endif

#ifdef WITH_THREAD
#include "pythread.h"

/* #defs ripped off from _tkinter.c, even though the situation here is much
   simpler, because we don't have to worry about waiting for Tcl
   events!  And, since zlib itself is threadsafe, we don't need to worry
   about re-entering zlib functions.

   N.B.

   Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions
   that modify the components of preexisting de/compress objects, it
   could prove to be a performance gain on multiprocessor machines if
   there was an de/compress object-specific lock.  However, for the
   moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL
   de/compress objects.
 */


static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */

#define ENTER_ZLIB \
	Py_BEGIN_ALLOW_THREADS \
	PyThread_acquire_lock(zlib_lock, 1); \
	Py_END_ALLOW_THREADS

#define LEAVE_ZLIB \
	PyThread_release_lock(zlib_lock);

#else

#define ENTER_ZLIB
#define LEAVE_ZLIB

#endif

/* The following parameters are copied from zutil.h, version 0.95 */
#define DEFLATED   8
#if MAX_MEM_LEVEL >= 8
#  define DEF_MEM_LEVEL 8
#else
#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
#endif
#define DEF_WBITS MAX_WBITS

/* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */
#define DEFAULTALLOC (16*1024)
#define PyInit_zlib initzlib

//Symbian changed 
#ifndef EKA2
extern const PyTypeObject Comptype;
extern const PyTypeObject Decomptype;
#endif

#define Comp_type ((PyTypeObject*)SPyGetGlobalString("CompType"))
#define Decomp_type ((PyTypeObject*)SPyGetGlobalString("DecompType"))

//static PyObject *ZlibError;
#define ZlibError (PYTHON_GLOBALS->ZlibError)

typedef struct
{
    PyObject_HEAD
    z_stream zst;
    PyObject *unused_data;
    PyObject *unconsumed_tail;
    int is_initialised;
} compobject;

static void
zlib_error(z_stream zst, int err, char *msg)
{
    if (zst.msg == Z_NULL)
	PyErr_Format(ZlibError, "Error %d %s", err, msg);
    else
	PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg);
}

/*
const static char compressobj__doc__[] =
#ifdef SYMBIAN
"";
#else
"compressobj([level]) -- Return a compressor object.\n"
"\n"
"Optional arg level is the compression level, in 1-9.";
#endif */

/*
const static char decompressobj__doc__[] =
#ifdef SYMBIAN
"";
#else
"decompressobj([wbits]) -- Return a decompressor object.\n"
"\n"
"Optional arg wbits is the window buffer size.";
#endif */

static compobject *
newcompobject(PyTypeObject *type)
{
    compobject *self;
    self = PyObject_New(compobject, type);
    if (self == NULL)
	return NULL;
    self->is_initialised = 0;
    self->unused_data = PyString_FromString("");
    if (self->unused_data == NULL) {
	Py_DECREF(self);
	return NULL;
    }
    self->unconsumed_tail = PyString_FromString("");
    if (self->unconsumed_tail == NULL) {
	Py_DECREF(self);
	return NULL;
    }
    return self;
}

/*
const static char compress__doc__[] =
#ifdef SYMBIAN
"";
#else
"compress(string[, level]) -- Returned compressed string.\n"
"\n"
"Optional arg level is the compression level, in 1-9.";
#endif */

static PyObject *
PyZlib_compress(PyObject *self, PyObject *args)
{
    PyObject *ReturnVal = NULL;
    Byte *input, *output;
    int length, level=Z_DEFAULT_COMPRESSION, err;
    z_stream zst;

    /* require Python string object, optional 'level' arg */
    if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))
	return NULL;

    zst.avail_out = length + length/1000 + 12 + 1;

    output = (Byte*)malloc(zst.avail_out);
    if (output == NULL) {
	PyErr_SetString(PyExc_MemoryError,
			"Can't allocate memory to compress data");
	return NULL;
    }

    /* Past the point of no return.  From here on out, we need to make sure
       we clean up mallocs & INCREFs. */

    zst.zalloc = (alloc_func)NULL;
    zst.zfree = (free_func)Z_NULL;
    zst.next_out = (Byte *)output;
    zst.next_in = (Byte *)input;
    zst.avail_in = length;
    err = deflateInit(&zst, level);

    switch(err) {
    case(Z_OK):
	break;
    case(Z_MEM_ERROR):
	PyErr_SetString(PyExc_MemoryError,
			"Out of memory while compressing data");
	goto error;
    case(Z_STREAM_ERROR):
	PyErr_SetString(ZlibError,
			"Bad compression level");
	goto error;
    default:
        deflateEnd(&zst);
	zlib_error(zst, err, "while compressing data");
	goto error;
    }

    Py_BEGIN_ALLOW_THREADS;
    err = deflate(&zst, Z_FINISH);
    Py_END_ALLOW_THREADS;

    if (err != Z_STREAM_END) {
	zlib_error(zst, err, "while compressing data");
	deflateEnd(&zst);
	goto error;
    }

    err=deflateEnd(&zst);
    if (err == Z_OK)
	ReturnVal = PyString_FromStringAndSize((char *)output,
					       zst.total_out);
    else
	zlib_error(zst, err, "while finishing compression");

 error:
    free(output);

    return ReturnVal;
}

/*
const static char decompress__doc__[] =
#ifdef SYMBIAN
"";
#else
"decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"
"\n"
"Optional arg wbits is the window buffer size.  Optional arg bufsize is\n"
"the initial output buffer size.";
#endif */

static PyObject *
PyZlib_decompress(PyObject *self, PyObject *args)
{
    PyObject *result_str;
    Byte *input;
    int length, err;
    int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC;
    z_stream zst;

    if (!PyArg_ParseTuple(args, "s#|ii:decompress",
			  &input, &length, &wsize, &r_strlen))
	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)))
	return NULL;

    zst.zalloc = (alloc_func)NULL;
    zst.zfree = (free_func)Z_NULL;
    zst.next_out = (Byte *)PyString_AS_STRING(result_str);
    zst.next_in = (Byte *)input;
    err = inflateInit2(&zst, wsize);

    switch(err) {
    case(Z_OK):
	break;
    case(Z_MEM_ERROR):
	PyErr_SetString(PyExc_MemoryError,
			"Out of memory while decompressing data");
	goto error;
    default:
        inflateEnd(&zst);
	zlib_error(zst, err, "while preparing to decompress data");
	goto error;
    }

    do {
	Py_BEGIN_ALLOW_THREADS
	err=inflate(&zst, Z_FINISH);
	Py_END_ALLOW_THREADS

	switch(err) {
	case(Z_STREAM_END):
	    break;
	case(Z_BUF_ERROR):
	    /*
	     * If there is at least 1 byte of room according to zst.avail_out
	     * and we get this error, assume that it means zlib cannot
	     * process the inflate call() due to an error in the data.
	     */
	    if (zst.avail_out > 0) {
		PyErr_Format(ZlibError, "Error %i while decompressing data",
			     err);
		inflateEnd(&zst);
		goto error;
	    }
	    /* fall through */
	case(Z_OK):
	    /* need more memory */
	    if (_PyString_Resize(&result_str, r_strlen << 1) == -1) {
		inflateEnd(&zst);
		result_str = NULL;
		goto error;
	    }
	    zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \
		+ r_strlen;
	    zst.avail_out = r_strlen;
	    r_strlen = r_strlen << 1;
	    break;
	default:
	    inflateEnd(&zst);
	    zlib_error(zst, err, "while decompressing data");
	    goto error;
	}
    } while (err != Z_STREAM_END);

    err = inflateEnd(&zst);
    if (err != Z_OK) {
	zlib_error(zst, err, "while finishing data decompression");
	goto error;
    }

    _PyString_Resize(&result_str, zst.total_out);
    return result_str;

 error:
    Py_XDECREF(result_str);
    return NULL;
}

static PyObject *
PyZlib_compressobj(PyObject */*selfptr*/, PyObject *args)
{
    compobject *self;
    int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;
    int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;

    if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,
			  &memLevel, &strategy))
	return NULL;

//    self = newcompobject((PyTypeObject *)&Comptype);
	self = newcompobject((PyTypeObject *)Comp_type);
    if (self==NULL)
	return(NULL);
    self->zst.zalloc = (alloc_func)NULL;
    self->zst.zfree = (free_func)Z_NULL;
    err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
    switch(err) {
    case (Z_OK):
	self->is_initialised = 1;
	return (PyObject*)self;
    case (Z_MEM_ERROR):
	Py_DECREF(self);
	PyErr_SetString(PyExc_MemoryError,
			"Can't allocate memory for compression object");
	return NULL;
    case(Z_STREAM_ERROR):
	Py_DECREF(self);
	PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
	return NULL;
    default:
	zlib_error(self->zst, err, "while creating compression object");
        Py_DECREF(self);
	return NULL;
    }
}

static PyObject *
PyZlib_decompressobj(PyObject */*selfptr*/, PyObject *args)
{
    int wbits=DEF_WBITS, err;
    compobject *self;
    if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))
	return NULL;

//    self = newcompobject((PyTypeObject *)&Decomptype);
    self = newcompobject((PyTypeObject *)Decomp_type);
    if (self == NULL)
	return(NULL);
    self->zst.zalloc = (alloc_func)NULL;
    self->zst.zfree = (free_func)Z_NULL;
    err = inflateInit2(&self->zst, wbits);
    switch(err) {
    case (Z_OK):
	self->is_initialised = 1;
	return (PyObject*)self;
    case(Z_STREAM_ERROR):
	Py_DECREF(self);
	PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
	return NULL;
    case (Z_MEM_ERROR):
	Py_DECREF(self);
	PyErr_SetString(PyExc_MemoryError,
			"Can't allocate memory for decompression object");
	return NULL;
    default:
	zlib_error(self->zst, err, "while creating decompression object");
        Py_DECREF(self);
	return NULL;
    }
}

static void
Comp_dealloc(compobject *self)
{
    if (self->is_initialised)
	deflateEnd(&self->zst);
    Py_XDECREF(self->unused_data);
    Py_XDECREF(self->unconsumed_tail);
    PyObject_Del(self);
}

static void
Decomp_dealloc(compobject *self)
{
    if (self->is_initialised)
	inflateEnd(&self->zst);
    Py_XDECREF(self->unused_data);
    Py_XDECREF(self->unconsumed_tail);
    PyObject_Del(self);
}

/*
const static char comp_compress__doc__[] =
#ifdef SYMBIAN
"";
#else
"compress(data) -- Return a string containing data compressed.\n"
"\n"
"After calling this function, some of the input data may still\n"
"be stored in internal buffers for later processing.\n"
"Call the flush() method to clear these buffers.";
#endif */


static PyObject *
PyZlib_objcompress(compobject *self, PyObject *args)
{
    int err, inplen, length = DEFAULTALLOC;
    PyObject *RetVal;
    Byte *input;
    unsigned long start_total_out;

    if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))
	return NULL;

    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 = deflate(&(self->zst), Z_NO_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 (_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), 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 (err != Z_OK && err != Z_BUF_ERROR) {
	zlib_error(self->zst, err, "while compressing");
	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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -