2014-11-20 12:16:26 +00:00
|
|
|
#define PY_SSIZE_T_CLEAN 1
|
|
|
|
#include <Python.h>
|
|
|
|
#include <bytesobject.h>
|
2016-08-22 13:44:12 +00:00
|
|
|
#include <cstdio>
|
2016-06-13 13:22:13 +00:00
|
|
|
#include <vector>
|
2016-08-22 13:44:12 +00:00
|
|
|
#include "../common/version.h"
|
2016-08-22 12:00:42 +00:00
|
|
|
#include "../public/encode.h"
|
|
|
|
#include "../public/decode.h"
|
2014-11-20 12:16:26 +00:00
|
|
|
|
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
#define PyInt_Check PyLong_Check
|
|
|
|
#define PyInt_AsLong PyLong_AsLong
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static PyObject *BrotliError;
|
|
|
|
|
2016-01-25 14:13:17 +00:00
|
|
|
static int as_bounded_int(PyObject *o, int* result, int lower_bound, int upper_bound) {
|
2016-01-25 10:39:05 +00:00
|
|
|
long value = PyInt_AsLong(o);
|
2016-01-25 14:13:17 +00:00
|
|
|
if ((value < (long) lower_bound) || (value > (long) upper_bound)) {
|
2016-01-25 10:39:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*result = (int) value;
|
2016-01-25 10:40:20 +00:00
|
|
|
return 1;
|
2016-01-25 10:39:05 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 13:22:13 +00:00
|
|
|
static int mode_convertor(PyObject *o, BrotliEncoderMode *mode) {
|
2014-11-20 12:16:26 +00:00
|
|
|
if (!PyInt_Check(o)) {
|
|
|
|
PyErr_SetString(BrotliError, "Invalid mode");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-25 11:07:39 +00:00
|
|
|
int mode_value = -1;
|
2016-01-25 14:13:17 +00:00
|
|
|
if (!as_bounded_int(o, &mode_value, 0, 255)) {
|
2016-01-25 11:07:39 +00:00
|
|
|
PyErr_SetString(BrotliError, "Invalid mode");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-06-13 13:22:13 +00:00
|
|
|
*mode = (BrotliEncoderMode) mode_value;
|
|
|
|
if (*mode != BROTLI_MODE_GENERIC &&
|
|
|
|
*mode != BROTLI_MODE_TEXT &&
|
|
|
|
*mode != BROTLI_MODE_FONT) {
|
2014-11-20 12:16:26 +00:00
|
|
|
PyErr_SetString(BrotliError, "Invalid mode");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-08 11:38:22 +00:00
|
|
|
static int quality_convertor(PyObject *o, int *quality) {
|
|
|
|
if (!PyInt_Check(o)) {
|
|
|
|
PyErr_SetString(BrotliError, "Invalid quality");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:13:17 +00:00
|
|
|
if (!as_bounded_int(o, quality, 0, 11)) {
|
2015-05-08 11:38:22 +00:00
|
|
|
PyErr_SetString(BrotliError, "Invalid quality. Range is 0 to 11.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lgwin_convertor(PyObject *o, int *lgwin) {
|
|
|
|
if (!PyInt_Check(o)) {
|
|
|
|
PyErr_SetString(BrotliError, "Invalid lgwin");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:13:17 +00:00
|
|
|
if (!as_bounded_int(o, lgwin, 10, 24)) {
|
2015-10-06 18:49:11 +00:00
|
|
|
PyErr_SetString(BrotliError, "Invalid lgwin. Range is 10 to 24.");
|
2015-05-08 11:38:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lgblock_convertor(PyObject *o, int *lgblock) {
|
|
|
|
if (!PyInt_Check(o)) {
|
|
|
|
PyErr_SetString(BrotliError, "Invalid lgblock");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-25 14:13:17 +00:00
|
|
|
if (!as_bounded_int(o, lgblock, 0, 24) || (*lgblock != 0 && *lgblock < 16)) {
|
2015-05-08 11:38:22 +00:00
|
|
|
PyErr_SetString(BrotliError, "Invalid lgblock. Can be 0 or in range 16 to 24.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-20 12:16:26 +00:00
|
|
|
PyDoc_STRVAR(compress__doc__,
|
2015-05-08 12:02:05 +00:00
|
|
|
"Compress a byte string.\n"
|
|
|
|
"\n"
|
|
|
|
"Signature:\n"
|
2016-02-23 16:42:55 +00:00
|
|
|
" compress(string, mode=MODE_GENERIC, quality=11, lgwin=22, lgblock=0, dictionary='')\n"
|
2014-11-20 12:16:26 +00:00
|
|
|
"\n"
|
2015-05-08 10:23:08 +00:00
|
|
|
"Args:\n"
|
2015-05-08 12:02:05 +00:00
|
|
|
" string (bytes): The input data.\n"
|
2015-05-11 10:09:36 +00:00
|
|
|
" mode (int, optional): The compression mode can be MODE_GENERIC (default),\n"
|
|
|
|
" MODE_TEXT (for UTF-8 format text input) or MODE_FONT (for WOFF 2.0). \n"
|
2015-05-08 11:02:08 +00:00
|
|
|
" quality (int, optional): Controls the compression-speed vs compression-\n"
|
|
|
|
" density tradeoff. The higher the quality, the slower the compression.\n"
|
|
|
|
" Range is 0 to 11. Defaults to 11.\n"
|
|
|
|
" lgwin (int, optional): Base 2 logarithm of the sliding window size. Range\n"
|
2015-10-06 18:49:11 +00:00
|
|
|
" is 10 to 24. Defaults to 22.\n"
|
2015-05-08 11:02:08 +00:00
|
|
|
" lgblock (int, optional): Base 2 logarithm of the maximum input block size.\n"
|
|
|
|
" Range is 16 to 24. If set to 0, the value will be set based on the\n"
|
|
|
|
" quality. Defaults to 0.\n"
|
2016-02-24 15:01:35 +00:00
|
|
|
" dictionary (bytes, optional): Custom dictionary. Only last sliding window\n"
|
|
|
|
" size bytes will be used.\n"
|
2015-05-08 10:23:08 +00:00
|
|
|
"\n"
|
|
|
|
"Returns:\n"
|
2015-05-08 12:02:05 +00:00
|
|
|
" The compressed byte string.\n"
|
2015-05-08 10:23:08 +00:00
|
|
|
"\n"
|
|
|
|
"Raises:\n"
|
2015-05-08 11:38:22 +00:00
|
|
|
" brotli.error: If arguments are invalid, or compressor fails.\n");
|
2014-11-20 12:16:26 +00:00
|
|
|
|
2015-05-08 10:23:08 +00:00
|
|
|
static PyObject* brotli_compress(PyObject *self, PyObject *args, PyObject *keywds) {
|
2014-11-20 12:16:26 +00:00
|
|
|
PyObject *ret = NULL;
|
2016-06-13 13:22:13 +00:00
|
|
|
uint8_t *input, *output = NULL, *custom_dictionary, *next_out;
|
|
|
|
const uint8_t *next_in;
|
|
|
|
size_t length, output_length, custom_dictionary_length, available_in, available_out;
|
|
|
|
BrotliEncoderMode mode = (BrotliEncoderMode) -1;
|
2015-05-08 11:38:22 +00:00
|
|
|
int quality = -1;
|
|
|
|
int lgwin = -1;
|
|
|
|
int lgblock = -1;
|
2014-11-20 12:16:26 +00:00
|
|
|
int ok;
|
|
|
|
|
2016-02-23 16:42:55 +00:00
|
|
|
static const char *kwlist[] = {
|
|
|
|
"string", "mode", "quality", "lgwin", "lgblock", "dictionary", NULL};
|
2015-05-08 10:23:08 +00:00
|
|
|
|
2016-02-23 16:42:55 +00:00
|
|
|
custom_dictionary = NULL;
|
|
|
|
custom_dictionary_length = 0;
|
|
|
|
|
|
|
|
ok = PyArg_ParseTupleAndKeywords(args, keywds, "s#|O&O&O&O&s#:compress",
|
2015-08-10 13:39:44 +00:00
|
|
|
const_cast<char **>(kwlist),
|
2014-11-20 12:16:26 +00:00
|
|
|
&input, &length,
|
|
|
|
&mode_convertor, &mode,
|
2015-05-08 11:38:22 +00:00
|
|
|
&quality_convertor, &quality,
|
|
|
|
&lgwin_convertor, &lgwin,
|
2016-02-23 16:42:55 +00:00
|
|
|
&lgblock_convertor, &lgblock,
|
|
|
|
&custom_dictionary, &custom_dictionary_length);
|
2014-11-20 12:16:26 +00:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
|
2016-01-25 10:39:05 +00:00
|
|
|
output_length = length + (length >> 2) + 10240;
|
2016-06-13 13:22:13 +00:00
|
|
|
BrotliEncoderState* enc = BrotliEncoderCreateInstance(0, 0, 0);
|
|
|
|
if (!enc) {
|
|
|
|
ok = false;
|
|
|
|
goto end;
|
|
|
|
}
|
2014-11-20 12:16:26 +00:00
|
|
|
output = new uint8_t[output_length];
|
|
|
|
|
2016-01-25 10:39:05 +00:00
|
|
|
if ((int) mode != -1)
|
2016-06-13 13:22:13 +00:00
|
|
|
BrotliEncoderSetParameter(enc, BROTLI_PARAM_MODE, (uint32_t)mode);
|
2015-05-08 11:38:22 +00:00
|
|
|
if (quality != -1)
|
2016-06-13 13:22:13 +00:00
|
|
|
BrotliEncoderSetParameter(enc, BROTLI_PARAM_QUALITY, (uint32_t)quality);
|
2015-05-08 11:38:22 +00:00
|
|
|
if (lgwin != -1)
|
2016-06-13 13:22:13 +00:00
|
|
|
BrotliEncoderSetParameter(enc, BROTLI_PARAM_LGWIN, (uint32_t)lgwin);
|
2015-05-08 11:38:22 +00:00
|
|
|
if (lgblock != -1)
|
2016-06-13 13:22:13 +00:00
|
|
|
BrotliEncoderSetParameter(enc, BROTLI_PARAM_LGBLOCK, (uint32_t)lgblock);
|
2014-11-20 12:16:26 +00:00
|
|
|
|
2016-06-13 13:22:13 +00:00
|
|
|
if (custom_dictionary_length != 0) {
|
|
|
|
BrotliEncoderSetCustomDictionary(enc, custom_dictionary_length,
|
|
|
|
custom_dictionary);
|
2016-02-23 16:42:55 +00:00
|
|
|
}
|
2016-06-13 13:22:13 +00:00
|
|
|
available_out = output_length;
|
|
|
|
next_out = output;
|
|
|
|
available_in = length;
|
|
|
|
next_in = input;
|
|
|
|
BrotliEncoderCompressStream(enc, BROTLI_OPERATION_FINISH,
|
|
|
|
&available_in, &next_in,
|
|
|
|
&available_out, &next_out, 0);
|
|
|
|
ok = BrotliEncoderIsFinished(enc);
|
|
|
|
|
|
|
|
end:
|
|
|
|
BrotliEncoderDestroyInstance(enc);
|
2014-11-20 12:16:26 +00:00
|
|
|
if (ok) {
|
2016-06-23 09:02:53 +00:00
|
|
|
ret = PyBytes_FromStringAndSize((char*)output, output_length - available_out);
|
2014-11-20 12:16:26 +00:00
|
|
|
} else {
|
|
|
|
PyErr_SetString(BrotliError, "BrotliCompressBuffer failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] output;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(decompress__doc__,
|
2015-05-08 12:02:05 +00:00
|
|
|
"Decompress a compressed byte string.\n"
|
|
|
|
"\n"
|
|
|
|
"Signature:\n"
|
|
|
|
" decompress(string)\n"
|
|
|
|
"\n"
|
|
|
|
"Args:\n"
|
|
|
|
" string (bytes): The compressed input data.\n"
|
2016-02-23 16:42:55 +00:00
|
|
|
" dictionary (bytes, optional): Custom dictionary. MUST be the same data\n"
|
|
|
|
" as passed to compress method.\n"
|
2015-05-08 12:02:05 +00:00
|
|
|
"\n"
|
|
|
|
"Returns:\n"
|
|
|
|
" The decompressed byte string.\n"
|
|
|
|
"\n"
|
|
|
|
"Raises:\n"
|
|
|
|
" brotli.error: If decompressor fails.\n");
|
2014-11-20 12:16:26 +00:00
|
|
|
|
2016-02-23 16:42:55 +00:00
|
|
|
static PyObject* brotli_decompress(PyObject *self, PyObject *args, PyObject *keywds) {
|
2014-11-20 12:16:26 +00:00
|
|
|
PyObject *ret = NULL;
|
2016-02-23 16:42:55 +00:00
|
|
|
const uint8_t *input, *custom_dictionary;
|
|
|
|
size_t length, custom_dictionary_length;
|
2014-11-20 12:16:26 +00:00
|
|
|
int ok;
|
|
|
|
|
2016-02-23 16:42:55 +00:00
|
|
|
static const char *kwlist[] = {"string", "dictionary", NULL};
|
|
|
|
|
|
|
|
custom_dictionary = NULL;
|
|
|
|
custom_dictionary_length = 0;
|
|
|
|
|
|
|
|
ok = PyArg_ParseTupleAndKeywords(args, keywds, "s#|s#:decompress",
|
|
|
|
const_cast<char **>(kwlist),
|
|
|
|
&input, &length,
|
|
|
|
&custom_dictionary, &custom_dictionary_length);
|
2014-11-20 12:16:26 +00:00
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-13 21:55:03 +00:00
|
|
|
std::vector<uint8_t> output;
|
2016-01-22 13:01:46 +00:00
|
|
|
const size_t kBufferSize = 65536;
|
|
|
|
uint8_t* buffer = new uint8_t[kBufferSize];
|
2016-04-19 14:43:42 +00:00
|
|
|
BrotliState* state = BrotliCreateState(0, 0, 0);
|
2016-02-23 16:42:55 +00:00
|
|
|
if (custom_dictionary_length != 0) {
|
2016-04-19 14:43:42 +00:00
|
|
|
BrotliSetCustomDictionary(custom_dictionary_length, custom_dictionary, state);
|
2016-02-23 16:42:55 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 13:01:46 +00:00
|
|
|
BrotliResult result = BROTLI_RESULT_NEEDS_MORE_OUTPUT;
|
|
|
|
while (result == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
|
|
|
|
size_t available_out = kBufferSize;
|
|
|
|
uint8_t* next_out = buffer;
|
|
|
|
size_t total_out = 0;
|
|
|
|
result = BrotliDecompressStream(&length, &input,
|
|
|
|
&available_out, &next_out,
|
2016-04-19 14:43:42 +00:00
|
|
|
&total_out, state);
|
2016-01-22 13:01:46 +00:00
|
|
|
size_t used_out = kBufferSize - available_out;
|
|
|
|
if (used_out != 0)
|
2016-01-22 13:09:25 +00:00
|
|
|
output.insert(output.end(), buffer, buffer + used_out);
|
2016-01-22 13:01:46 +00:00
|
|
|
}
|
|
|
|
ok = result == BROTLI_RESULT_SUCCESS;
|
2014-11-20 12:16:26 +00:00
|
|
|
if (ok) {
|
2015-10-05 10:09:11 +00:00
|
|
|
ret = PyBytes_FromStringAndSize((char*)(output.size() ? &output[0] : NULL), output.size());
|
2014-11-20 12:16:26 +00:00
|
|
|
} else {
|
2015-03-13 21:55:03 +00:00
|
|
|
PyErr_SetString(BrotliError, "BrotliDecompress failed");
|
2014-11-20 12:16:26 +00:00
|
|
|
}
|
2016-01-22 13:01:46 +00:00
|
|
|
|
2016-04-19 14:43:42 +00:00
|
|
|
BrotliDestroyState(state);
|
2016-01-22 13:01:46 +00:00
|
|
|
delete[] buffer;
|
2014-11-20 12:16:26 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef brotli_methods[] = {
|
2015-05-08 10:23:08 +00:00
|
|
|
{"compress", (PyCFunction)brotli_compress, METH_VARARGS | METH_KEYWORDS, compress__doc__},
|
2016-02-23 16:42:55 +00:00
|
|
|
{"decompress", (PyCFunction)brotli_decompress, METH_VARARGS | METH_KEYWORDS, decompress__doc__},
|
2014-11-20 12:16:26 +00:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(brotli__doc__,
|
|
|
|
"The functions in this module allow compression and decompression using the\n"
|
2015-05-08 12:02:05 +00:00
|
|
|
"Brotli library.\n\n");
|
2014-11-20 12:16:26 +00:00
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2016-06-13 13:22:13 +00:00
|
|
|
PyModule_AddIntConstant(m, "MODE_GENERIC", (int) BROTLI_MODE_GENERIC);
|
|
|
|
PyModule_AddIntConstant(m, "MODE_TEXT", (int) BROTLI_MODE_TEXT);
|
|
|
|
PyModule_AddIntConstant(m, "MODE_FONT", (int) BROTLI_MODE_FONT);
|
2014-11-20 12:16:26 +00:00
|
|
|
|
2016-08-22 13:44:12 +00:00
|
|
|
char version[16];
|
|
|
|
snprintf(version, sizeof(version), "%d.%d.%d",
|
|
|
|
BROTLI_VERSION >> 24, (BROTLI_VERSION >> 12) & 0xFFF, BROTLI_VERSION & 0xFFF);
|
|
|
|
PyModule_AddStringConstant(m, "__version__", version);
|
2015-08-11 09:42:22 +00:00
|
|
|
|
2014-11-20 12:16:26 +00:00
|
|
|
RETURN_BROTLI;
|
|
|
|
}
|