mirror of
https://github.com/google/brotli.git
synced 2024-11-08 13:20:05 +00:00
commit
cb0ca3a3c6
160
python/brotlimodule.cc
Normal file
160
python/brotlimodule.cc
Normal file
@ -0,0 +1,160 @@
|
||||
#define PY_SSIZE_T_CLEAN 1
|
||||
#include <Python.h>
|
||||
#include <bytesobject.h>
|
||||
#include "enc/encode.h"
|
||||
#include "dec/decode.h"
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define PyInt_Check PyLong_Check
|
||||
#define PyInt_AsLong PyLong_AsLong
|
||||
#endif
|
||||
|
||||
using namespace brotli;
|
||||
|
||||
static PyObject *BrotliError;
|
||||
|
||||
static int mode_convertor(PyObject *o, BrotliParams::Mode *mode) {
|
||||
if (!PyInt_Check(o)) {
|
||||
PyErr_SetString(BrotliError, "Invalid mode");
|
||||
return 0;
|
||||
}
|
||||
|
||||
*mode = (BrotliParams::Mode) PyInt_AsLong(o);
|
||||
if (*mode != BrotliParams::Mode::MODE_TEXT &&
|
||||
*mode != BrotliParams::Mode::MODE_FONT) {
|
||||
PyErr_SetString(BrotliError, "Invalid mode");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(compress__doc__,
|
||||
"compress(string[, mode[, transform]]) -- Returned compressed string.\n"
|
||||
"\n"
|
||||
"Optional arg mode is the compression mode, either MODE_TEXT (default) or\n"
|
||||
"MODE_FONT. Optional boolean arg transform controls whether to enable\n"
|
||||
"encoder transforms or not, defaults to False.");
|
||||
|
||||
static PyObject* brotli_compress(PyObject *self, PyObject *args) {
|
||||
PyObject *ret = NULL;
|
||||
PyObject* transform = NULL;
|
||||
uint8_t *input, *output;
|
||||
size_t length, output_length;
|
||||
BrotliParams::Mode mode = (BrotliParams::Mode) -1;
|
||||
int ok;
|
||||
|
||||
ok = PyArg_ParseTuple(args, "s#|O&O!:compress",
|
||||
&input, &length,
|
||||
&mode_convertor, &mode,
|
||||
&PyBool_Type, &transform);
|
||||
|
||||
if (!ok)
|
||||
return NULL;
|
||||
|
||||
output_length = 1.2 * length + 10240;
|
||||
output = new uint8_t[output_length];
|
||||
|
||||
BrotliParams params;
|
||||
if (mode != -1)
|
||||
params.mode = mode;
|
||||
if (transform)
|
||||
params.enable_transforms = PyObject_IsTrue(transform);
|
||||
|
||||
ok = BrotliCompressBuffer(params, length, input,
|
||||
&output_length, output);
|
||||
if (ok) {
|
||||
ret = PyBytes_FromStringAndSize((char*)output, output_length);
|
||||
} else {
|
||||
PyErr_SetString(BrotliError, "BrotliCompressBuffer failed");
|
||||
}
|
||||
|
||||
delete[] output;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(decompress__doc__,
|
||||
"decompress(string[, bufsize]) -- Return decompressed string."
|
||||
"\n"
|
||||
"Optional arg bufsize is the initial output buffer size.");
|
||||
|
||||
static PyObject* brotli_decompress(PyObject *self, PyObject *args) {
|
||||
PyObject *ret = NULL;
|
||||
uint8_t *input, *output;
|
||||
size_t length, output_length = 0;
|
||||
int ok;
|
||||
|
||||
ok = PyArg_ParseTuple(args, "s#|n:decompress",
|
||||
&input, &length, &output_length);
|
||||
if (!ok)
|
||||
return NULL;
|
||||
|
||||
if (output_length <= 0) {
|
||||
// Just an arbitrary value, should be big enough
|
||||
output_length = 4 * length;
|
||||
}
|
||||
|
||||
output = new uint8_t[output_length];
|
||||
|
||||
ok = BrotliDecompressBuffer(length, input, &output_length, output);
|
||||
if (ok) {
|
||||
ret = PyBytes_FromStringAndSize((char*)output, output_length);
|
||||
} else {
|
||||
PyErr_SetString(BrotliError, "BrotliDecompressBuffer failed");
|
||||
}
|
||||
|
||||
delete[] output;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static PyMethodDef brotli_methods[] = {
|
||||
{"compress", brotli_compress, METH_VARARGS, compress__doc__},
|
||||
{"decompress", brotli_decompress, METH_VARARGS, decompress__doc__},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
PyDoc_STRVAR(brotli__doc__,
|
||||
"The functions in this module allow compression and decompression using the\n"
|
||||
"Brotli library.\n"
|
||||
"\n"
|
||||
"compress(string[, mode, transform]) -- Compress string.\n"
|
||||
"decompress(string) -- Decompresses a compressed string.\n");
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define INIT_BROTLI PyInit_brotli
|
||||
#define CREATE_BROTLI PyModule_Create(&brotli_module)
|
||||
#define RETURN_BROTLI return m
|
||||
|
||||
static struct PyModuleDef brotli_module = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"brotli",
|
||||
brotli__doc__,
|
||||
0,
|
||||
brotli_methods,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
#else
|
||||
#define INIT_BROTLI initbrotli
|
||||
#define CREATE_BROTLI Py_InitModule3("brotli", brotli_methods, brotli__doc__)
|
||||
#define RETURN_BROTLI return
|
||||
#endif
|
||||
|
||||
PyMODINIT_FUNC INIT_BROTLI(void) {
|
||||
PyObject *m = CREATE_BROTLI;
|
||||
|
||||
BrotliError = PyErr_NewException((char*) "brotli.error", NULL, NULL);
|
||||
|
||||
if (BrotliError != NULL) {
|
||||
Py_INCREF(BrotliError);
|
||||
PyModule_AddObject(m, "error", BrotliError);
|
||||
}
|
||||
|
||||
PyModule_AddIntConstant(m, "MODE_TEXT", (int) BrotliParams::Mode::MODE_TEXT);
|
||||
PyModule_AddIntConstant(m, "MODE_FONT", (int) BrotliParams::Mode::MODE_FONT);
|
||||
|
||||
RETURN_BROTLI;
|
||||
}
|
1
python/dec
Symbolic link
1
python/dec
Symbolic link
@ -0,0 +1 @@
|
||||
../dec/
|
1
python/enc
Symbolic link
1
python/enc
Symbolic link
@ -0,0 +1 @@
|
||||
../enc/
|
124
python/setup.py
Normal file
124
python/setup.py
Normal file
@ -0,0 +1,124 @@
|
||||
from distutils.core import setup, Extension
|
||||
from distutils.command.build_ext import build_ext
|
||||
import platform
|
||||
|
||||
class BuildExt(build_ext):
|
||||
def get_source_files(self):
|
||||
filenames = build_ext.get_source_files(self)
|
||||
for ext in self.extensions:
|
||||
filenames.extend(ext.depends)
|
||||
return filenames
|
||||
|
||||
def build_extension(self, ext):
|
||||
c_sources = []
|
||||
cxx_sources = []
|
||||
for source in ext.sources:
|
||||
if source.endswith(".c"):
|
||||
c_sources.append(source)
|
||||
else:
|
||||
cxx_sources.append(source)
|
||||
extra_args = ext.extra_compile_args or []
|
||||
|
||||
objects = []
|
||||
for lang, sources in (("c", c_sources), ("c++", cxx_sources)):
|
||||
if lang == "c++":
|
||||
extra_args.append("-std=c++0x")
|
||||
|
||||
macros = ext.define_macros[:]
|
||||
if platform.system() == "Darwin":
|
||||
macros.append(("OS_MACOSX", "1"))
|
||||
for undef in ext.undef_macros:
|
||||
macros.append((undef,))
|
||||
|
||||
objs = self.compiler.compile(sources,
|
||||
output_dir=self.build_temp,
|
||||
macros=macros,
|
||||
include_dirs=ext.include_dirs,
|
||||
debug=self.debug,
|
||||
extra_postargs=extra_args,
|
||||
depends=ext.depends)
|
||||
objects.extend(objs)
|
||||
|
||||
self._built_objects = objects[:]
|
||||
if ext.extra_objects:
|
||||
objects.extend(ext.extra_objects)
|
||||
extra_args = ext.extra_link_args or []
|
||||
|
||||
ext_path = self.get_ext_fullpath(ext.name)
|
||||
# Detect target language, if not provided
|
||||
language = ext.language or self.compiler.detect_language(sources)
|
||||
|
||||
self.compiler.link_shared_object(
|
||||
objects, ext_path,
|
||||
libraries=self.get_libraries(ext),
|
||||
library_dirs=ext.library_dirs,
|
||||
runtime_library_dirs=ext.runtime_library_dirs,
|
||||
extra_postargs=extra_args,
|
||||
export_symbols=self.get_export_symbols(ext),
|
||||
debug=self.debug,
|
||||
build_temp=self.build_temp,
|
||||
target_lang=language)
|
||||
|
||||
brotli = Extension("brotli",
|
||||
sources=[
|
||||
"brotlimodule.cc",
|
||||
"enc/backward_references.cc",
|
||||
"enc/block_splitter.cc",
|
||||
"enc/brotli_bit_stream.cc",
|
||||
"enc/encode.cc",
|
||||
"enc/entropy_encode.cc",
|
||||
"enc/histogram.cc",
|
||||
"enc/literal_cost.cc",
|
||||
"dec/bit_reader.c",
|
||||
"dec/decode.c",
|
||||
"dec/huffman.c",
|
||||
"dec/safe_malloc.c",
|
||||
"dec/streams.c",
|
||||
],
|
||||
depends=[
|
||||
"enc/backward_references.h",
|
||||
"enc/bit_cost.h",
|
||||
"enc/block_splitter.h",
|
||||
"enc/brotli_bit_stream.h",
|
||||
"enc/cluster.h",
|
||||
"enc/command.h",
|
||||
"enc/context.h",
|
||||
"enc/dictionary.h",
|
||||
"enc/encode.h",
|
||||
"enc/entropy_encode.h",
|
||||
"enc/fast_log.h",
|
||||
"enc/find_match_length.h",
|
||||
"enc/hash.h",
|
||||
"enc/histogram.h",
|
||||
"enc/literal_cost.h",
|
||||
"enc/port.h",
|
||||
"enc/prefix.h",
|
||||
"enc/ringbuffer.h",
|
||||
"enc/static_dict.h",
|
||||
"enc/transform.h",
|
||||
"enc/write_bits.h",
|
||||
"dec/bit_reader.h",
|
||||
"dec/context.h",
|
||||
"dec/decode.h",
|
||||
"dec/dictionary.h",
|
||||
"dec/huffman.h",
|
||||
"dec/prefix.h",
|
||||
"dec/safe_malloc.h",
|
||||
"dec/streams.h",
|
||||
"dec/transform.h",
|
||||
"dec/types.h",
|
||||
],
|
||||
language="c++",
|
||||
)
|
||||
|
||||
setup(
|
||||
name="Brotli",
|
||||
version="0.1",
|
||||
url="https://github.com/google/brotli",
|
||||
description="Python binding of the Brotli compression library",
|
||||
author="Khaled Hosny",
|
||||
author_email="khaledhosny@eglug.org",
|
||||
license="Apache 2.0",
|
||||
ext_modules=[brotli],
|
||||
cmdclass={'build_ext': BuildExt},
|
||||
)
|
Loading…
Reference in New Issue
Block a user