Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/usr/bin/env python


from setuptools import setup, find_packages, Extension
from distutils.core import setup, Extension
import sys

VERSION = (0, 7, 0)
VERSION_STR = ".".join([str(x) for x in VERSION])

kwarg=dict()
if sys.version_info >= (2, 7):
kwarg['setup_requires']=["nose>=1.0"]
kwarg['test_suite'] = "nose.collector"

setup(
name='lz4',
version=VERSION_STR,
Expand All @@ -14,7 +20,7 @@
author='Steeve Morin',
author_email='[email protected]',
url='https://github.com/steeve/python-lz4',
packages=find_packages('src'),
packages=[],
package_dir={'': 'src'},
ext_modules=[
Extension('lz4', [
Expand All @@ -23,16 +29,14 @@
'src/python-lz4.c'
], extra_compile_args=[
"-std=c99",
"-O3",
# "-O3",
"-Wall",
"-W",
"-Wundef",
"-DVERSION=\"%s\"" % VERSION_STR,
"-DLZ4_VERSION=\"r119\"",
])
],
setup_requires=["nose>=1.0"],
test_suite = "nose.collector",
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: BSD License',
Expand All @@ -42,5 +46,6 @@
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
],
],
**kwarg
)
48 changes: 48 additions & 0 deletions src/python-lz4.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,53 @@ static PyObject *py_lz4_uncompress(PyObject *self, PyObject *args) {
return result;
}

/*
* access to the original lz4_decompress_safe() function
* expect 3 arguments
* - a compressed string in raw format (without the size header)
* - inputSize, the number of bytes to use from the compressed string,
* if zero then use all the string
* - maxOutputSize, the size of the output buffer, if too small then
* raise a ValueError exception, if too big then resized to match
* the uncompressed size.
*/
static PyObject *py_lz4_decompress_safe(PyObject *self, PyObject *args) {
PyObject *result;
const char *source;
int source_size;
int inputSize;
int maxOutputSize;

if (!PyArg_ParseTuple(args, "s#ii", &source, &source_size, &inputSize, &maxOutputSize)) {
return NULL;
}

if (inputSize > source_size) {
PyErr_SetString(PyExc_ValueError, "inputSize bigger than source length");
return NULL;
}

if (inputSize == 0) {
inputSize = source_size;
}

result = PyBytes_FromStringAndSize(NULL, maxOutputSize);
if (result != NULL) {
char *dest = PyBytes_AS_STRING(result);
int osize = LZ4_decompress_safe(source, dest, inputSize, maxOutputSize);
if (osize < 0) {
PyErr_Format(PyExc_ValueError, "corrupt input at byte %d", -osize);
Py_CLEAR(result);
}
if (osize < maxOutputSize) {
if (_PyBytes_Resize(&result, osize) < 0) {
return NULL;
}
}
}
return result;
}

static PyMethodDef Lz4Methods[] = {
{"LZ4_compress", py_lz4_compress, METH_VARARGS, COMPRESS_DOCSTRING},
{"LZ4_uncompress", py_lz4_uncompress, METH_VARARGS, UNCOMPRESS_DOCSTRING},
Expand All @@ -134,6 +181,7 @@ static PyMethodDef Lz4Methods[] = {
{"decompress", py_lz4_uncompress, METH_VARARGS, UNCOMPRESS_DOCSTRING},
{"dumps", py_lz4_compress, METH_VARARGS, COMPRESS_DOCSTRING},
{"loads", py_lz4_uncompress, METH_VARARGS, UNCOMPRESS_DOCSTRING},
{"LZ4_decompress_safe", py_lz4_decompress_safe, METH_VARARGS, LZ4_DECOMPRESS_SAFE_DOCSTRING},
{NULL, NULL, 0, NULL}
};

Expand Down
2 changes: 2 additions & 0 deletions src/python-lz4.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@

static PyObject *py_lz4_compress(PyObject *self, PyObject *args);
static PyObject *py_lz4_uncompress(PyObject *self, PyObject *args);
static PyObject *py_lz4_decompress_safe(PyObject *self, PyObject *args);

PyMODINIT_FUNC initlz4(void);

#define COMPRESS_DOCSTRING "Compress string, returning the compressed data.\nRaises an exception if any error occurs."
#define COMPRESSHC_DOCSTRING COMPRESS_DOCSTRING "\n\nCompared to compress, this gives a better compression ratio, but is much slower."
#define UNCOMPRESS_DOCSTRING "Decompress string, returning the uncompressed data.\nRaises an exception if any error occurs."
#define LZ4_DECOMPRESS_SAFE_DOCSTRING "Decompress raw string, returning the uncompressed data.\nRaises an exception if any error occurs."

#if defined(_WIN32) && defined(_MSC_VER)
# define inline __inline
Expand Down