43 #if PY_MAJOR_VERSION >= 3
44 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
46 #define GETSTATE(m) (&_state)
57 size_t size = PyBytes_GET_SIZE(*buf);
58 size_t new_size = size + (size >> 3) + 6;
59 if (new_size > size) {
60 return _PyBytes_Resize(buf, new_size);
62 PyErr_SetString(PyExc_OverflowError,
63 "Unable to allocate buffer - output too large");
79 Py_TYPE(
self)->tp_free((PyObject*)
self);
86 if (!PyArg_ParseTuple(args,
":__init__")) {
92 PyErr_SetString(PyExc_RuntimeError,
"error initializing roslz4 stream");
103 output = PyBytes_FromStringAndSize(NULL, initial_size);
105 if (input != NULL) { PyBuffer_Release(input); }
113 self->stream.input_next = input->buf;
114 self->stream.input_left = input->len;
117 self->stream.input_next = NULL;
118 self->stream.input_left = 0;
120 self->stream.output_next = PyBytes_AS_STRING(output);
121 self->stream.output_left = PyBytes_GET_SIZE(output);
125 int output_written = 0;
127 (action ==
ROSLZ4_RUN && self->stream.input_left > 0)) {
128 int out_start =
self->stream.total_out;
130 output_written +=
self->stream.total_out - out_start;
139 self->stream.output_next = PyBytes_AS_STRING(output) + output_written;
140 self->stream.output_left = PyBytes_GET_SIZE(output) - output_written;
142 PyErr_SetString(PyExc_IOError,
"bad block size parameter");
145 PyErr_SetString(PyExc_IOError,
"error compressing");
148 PyErr_Format(PyExc_RuntimeError,
"unhandled return code %i", status);
154 if (output_written != PyBytes_GET_SIZE(output)) {
155 _PyBytes_Resize(&output, output_written);
158 if (input != NULL) { PyBuffer_Release(input); }
162 if (input != NULL) { PyBuffer_Release(input); }
171 PyObject *output = NULL;
174 if (!PyArg_ParseTuple(args,
"s*:compress", &input)) {
184 PyObject *output = NULL;
186 if (!PyArg_ParseTuple(args,
":flush")) {
201 PyVarObject_HEAD_INIT(NULL, 0)
202 "_roslz4.LZ4Compressor",
221 "LZ4Compressor objects",
250 Py_TYPE(
self)->tp_free((PyObject*)
self);
257 if (!PyArg_ParseTuple(args,
":__init__")) {
263 PyErr_SetString(PyExc_RuntimeError,
"error initializing roslz4 stream");
273 PyObject *output = NULL;
276 if (!PyArg_ParseTuple(args,
"s*:decompress", &input)) {
282 if (self->stream.block_size_id == -1 ) {
288 output = PyBytes_FromStringAndSize(NULL, block_size);
290 PyBuffer_Release(&input);
295 self->stream.input_next = input.buf;
296 self->stream.input_left = input.len;
297 self->stream.output_next = PyBytes_AS_STRING(output);
298 self->stream.output_left = PyBytes_GET_SIZE(output);
300 int output_written = 0;
301 while (self->stream.input_left > 0) {
302 int out_start =
self->stream.total_out;
304 output_written +=
self->stream.total_out - out_start;
313 self->stream.output_next = PyBytes_AS_STRING(output) + output_written;
314 self->stream.output_left = PyBytes_GET_SIZE(output) - output_written;
316 PyErr_SetString(PyExc_IOError,
"error decompressing");
319 PyErr_SetString(PyExc_IOError,
"malformed data to decompress");
322 PyErr_Format(PyExc_RuntimeError,
"unhandled return code %i", status);
327 if (output_written != PyBytes_GET_SIZE(output)) {
328 _PyBytes_Resize(&output, output_written);
331 PyBuffer_Release(&input);
335 PyBuffer_Release(&input);
346 PyVarObject_HEAD_INIT(NULL, 0)
347 "_roslz4.LZ4Decompressor",
366 "LZ4Decompressor objects",
387 #if PY_MAJOR_VERSION >= 3
389 static int roslz4_traverse(PyObject *m, visitproc visit,
void *arg) {
394 static int roslz4_clear(PyObject *m) {
399 static struct PyModuleDef moduledef = {
400 PyModuleDef_HEAD_INIT,
410 #define INITERROR return NULL
416 #define INITERROR return
434 #if PY_MAJOR_VERSION >= 3
435 m = PyModule_Create(&moduledef);
437 m = Py_InitModule(
"_roslz4", NULL);
449 #if PY_MAJOR_VERSION >= 3