aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorEugene Kliuchnikov <eustas@google.com>2016-06-13 15:22:13 +0200
committerEugene Kliuchnikov <eustas@google.com>2016-06-13 15:22:13 +0200
commitdb3a11625da0f38ce94452c99eea6ef44c8b9d3e (patch)
tree3702368fdc83a3b307b41e52e84611a54c831088 /python
parent0747bda5e09f249d0bb3bf3973cd5caee6fb8472 (diff)
downloadbrotli-db3a11625da0f38ce94452c99eea6ef44c8b9d3e.zip
brotli-db3a11625da0f38ce94452c99eea6ef44c8b9d3e.tar.gz
brotli-db3a11625da0f38ce94452c99eea6ef44c8b9d3e.tar.bz2
Fix CI build.
Diffstat (limited to 'python')
-rw-r--r--python/brotlimodule.cc70
-rw-r--r--python/tests/custom_dictionary_test.py2
2 files changed, 37 insertions, 35 deletions
diff --git a/python/brotlimodule.cc b/python/brotlimodule.cc
index fbc73ca..de3f095 100644
--- a/python/brotlimodule.cc
+++ b/python/brotlimodule.cc
@@ -1,6 +1,7 @@
#define PY_SSIZE_T_CLEAN 1
#include <Python.h>
#include <bytesobject.h>
+#include <vector>
#include "../enc/encode.h"
#include "../dec/decode.h"
#include "../tools/version.h"
@@ -10,8 +11,6 @@
#define PyInt_AsLong PyLong_AsLong
#endif
-using namespace brotli;
-
static PyObject *BrotliError;
static int as_bounded_int(PyObject *o, int* result, int lower_bound, int upper_bound) {
@@ -23,7 +22,7 @@ static int as_bounded_int(PyObject *o, int* result, int lower_bound, int upper_b
return 1;
}
-static int mode_convertor(PyObject *o, BrotliParams::Mode *mode) {
+static int mode_convertor(PyObject *o, BrotliEncoderMode *mode) {
if (!PyInt_Check(o)) {
PyErr_SetString(BrotliError, "Invalid mode");
return 0;
@@ -34,10 +33,10 @@ static int mode_convertor(PyObject *o, BrotliParams::Mode *mode) {
PyErr_SetString(BrotliError, "Invalid mode");
return 0;
}
- *mode = (BrotliParams::Mode) mode_value;
- if (*mode != BrotliParams::MODE_GENERIC &&
- *mode != BrotliParams::MODE_TEXT &&
- *mode != BrotliParams::MODE_FONT) {
+ *mode = (BrotliEncoderMode) mode_value;
+ if (*mode != BROTLI_MODE_GENERIC &&
+ *mode != BROTLI_MODE_TEXT &&
+ *mode != BROTLI_MODE_FONT) {
PyErr_SetString(BrotliError, "Invalid mode");
return 0;
}
@@ -116,9 +115,10 @@ PyDoc_STRVAR(compress__doc__,
static PyObject* brotli_compress(PyObject *self, PyObject *args, PyObject *keywds) {
PyObject *ret = NULL;
- uint8_t *input, *output, *custom_dictionary;
- size_t length, output_length, custom_dictionary_length;
- BrotliParams::Mode mode = (BrotliParams::Mode) -1;
+ 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;
int quality = -1;
int lgwin = -1;
int lgblock = -1;
@@ -142,35 +142,37 @@ static PyObject* brotli_compress(PyObject *self, PyObject *args, PyObject *keywd
return NULL;
output_length = length + (length >> 2) + 10240;
+ BrotliEncoderState* enc = BrotliEncoderCreateInstance(0, 0, 0);
+ if (!enc) {
+ ok = false;
+ goto end;
+ }
output = new uint8_t[output_length];
- BrotliParams params;
if ((int) mode != -1)
- params.mode = mode;
+ BrotliEncoderSetParameter(enc, BROTLI_PARAM_MODE, (uint32_t)mode);
if (quality != -1)
- params.quality = quality;
+ BrotliEncoderSetParameter(enc, BROTLI_PARAM_QUALITY, (uint32_t)quality);
if (lgwin != -1)
- params.lgwin = lgwin;
+ BrotliEncoderSetParameter(enc, BROTLI_PARAM_LGWIN, (uint32_t)lgwin);
if (lgblock != -1)
- params.lgblock = lgblock;
+ BrotliEncoderSetParameter(enc, BROTLI_PARAM_LGBLOCK, (uint32_t)lgblock);
- if (custom_dictionary_length == 0) {
- ok = BrotliCompressBuffer(params, length, input,
- &output_length, output);
- } else {
- uint8_t *custom_dictionary_start = custom_dictionary;
- BrotliMemIn in(input, length);
- BrotliMemOut out(output, output_length);
- size_t sliding_window_size = ((size_t)1) << params.lgwin;
- if (custom_dictionary_length > sliding_window_size) {
- custom_dictionary_start += custom_dictionary_length - sliding_window_size;
- custom_dictionary_length = sliding_window_size;
- }
- ok = BrotliCompressWithCustomDictionary(custom_dictionary_length,
- custom_dictionary_start, params, &in, &out);
- output_length = out.position();
+ if (custom_dictionary_length != 0) {
+ BrotliEncoderSetCustomDictionary(enc, custom_dictionary_length,
+ custom_dictionary);
}
-
+ 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);
if (ok) {
ret = PyBytes_FromStringAndSize((char*)output, output_length);
} else {
@@ -291,9 +293,9 @@ PyMODINIT_FUNC INIT_BROTLI(void) {
PyModule_AddObject(m, "error", BrotliError);
}
- PyModule_AddIntConstant(m, "MODE_GENERIC", (int) BrotliParams::MODE_GENERIC);
- PyModule_AddIntConstant(m, "MODE_TEXT", (int) BrotliParams::MODE_TEXT);
- PyModule_AddIntConstant(m, "MODE_FONT", (int) BrotliParams::MODE_FONT);
+ 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);
PyModule_AddStringConstant(m, "__version__", BROTLI_VERSION);
diff --git a/python/tests/custom_dictionary_test.py b/python/tests/custom_dictionary_test.py
index 8f5e015..a64ee9d 100644
--- a/python/tests/custom_dictionary_test.py
+++ b/python/tests/custom_dictionary_test.py
@@ -12,7 +12,7 @@ testdata/alice29.txt
testdata/asyoulik.txt
testdata/lcet10.txt
testdata/plrabn12.txt
-../enc/encode.cc
+../enc/encode.c
../common/dictionary.h
../dec/decode.c
%s