mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Port lzx plugin to build on python2/python3
This commit is contained in:
parent
fdcd6d746c
commit
e20566c7fc
@ -67,7 +67,7 @@ static void
|
|||||||
Compressor_dealloc(Compressor *self)
|
Compressor_dealloc(Compressor *self)
|
||||||
{
|
{
|
||||||
Compressor_clear(self);
|
Compressor_clear(self);
|
||||||
|
|
||||||
if (self->stream) {
|
if (self->stream) {
|
||||||
lzxc_finish(self->stream, NULL);
|
lzxc_finish(self->stream, NULL);
|
||||||
self->stream = NULL;
|
self->stream = NULL;
|
||||||
@ -81,7 +81,7 @@ Compressor_dealloc(Compressor *self)
|
|||||||
self->output.data = NULL;
|
self->output.data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
self->ob_type->tp_free((PyObject *)self);
|
Py_TYPE(self)->tp_free((PyObject *)self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
@ -99,17 +99,17 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||||||
self->wbits = 0;
|
self->wbits = 0;
|
||||||
self->blocksize = 0;
|
self->blocksize = 0;
|
||||||
self->flushing = 0;
|
self->flushing = 0;
|
||||||
|
|
||||||
BUFFER_INIT(self->residue);
|
BUFFER_INIT(self->residue);
|
||||||
BUFFER_INIT(self->input);
|
BUFFER_INIT(self->input);
|
||||||
BUFFER_INIT(self->output);
|
BUFFER_INIT(self->output);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (PyObject *)self;
|
return (PyObject *)self;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
get_bytes(void *context, int nbytes, void *buf)
|
get_bytes(void *context, int nbytes, void *buf)
|
||||||
{
|
{
|
||||||
Compressor *self = (Compressor *)context;
|
Compressor *self = (Compressor *)context;
|
||||||
unsigned char *data = (unsigned char *)buf;
|
unsigned char *data = (unsigned char *)buf;
|
||||||
@ -130,7 +130,7 @@ get_bytes(void *context, int nbytes, void *buf)
|
|||||||
nbytes -= resrem;
|
nbytes -= resrem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inrem == 0) {
|
if (inrem == 0) {
|
||||||
return resrem;
|
return resrem;
|
||||||
} else if (nbytes > inrem) {
|
} else if (nbytes > inrem) {
|
||||||
@ -143,7 +143,7 @@ get_bytes(void *context, int nbytes, void *buf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
at_eof(void *context)
|
at_eof(void *context)
|
||||||
{
|
{
|
||||||
Compressor *self = (Compressor *)context;
|
Compressor *self = (Compressor *)context;
|
||||||
return (self->flushing && (COMPRESSOR_REMAINING(self) == 0));
|
return (self->flushing && (COMPRESSOR_REMAINING(self) == 0));
|
||||||
@ -190,10 +190,10 @@ Compressor_init(Compressor *self, PyObject *args, PyObject *kwds)
|
|||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
self->reset = 1;
|
self->reset = 1;
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(
|
if (!PyArg_ParseTupleAndKeywords(
|
||||||
args, kwds, "I|b", kwlist, &wbits, &self->reset)) {
|
args, kwds, "I|b", kwlist, &wbits, &self->reset)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/* TODO: check window size. */
|
/* TODO: check window size. */
|
||||||
|
|
||||||
@ -216,7 +216,7 @@ Compressor_init(Compressor *self, PyObject *args, PyObject *kwds)
|
|||||||
PyErr_SetString(LZXError, "Failed to create compression stream");
|
PyErr_SetString(LZXError, "Failed to create compression stream");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,12 +234,12 @@ Compressor_compress__(
|
|||||||
PyObject *cdata = NULL;
|
PyObject *cdata = NULL;
|
||||||
PyObject *rtable = NULL;
|
PyObject *rtable = NULL;
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
|
|
||||||
self->flushing = flush;
|
self->flushing = flush;
|
||||||
input->data = data;
|
input->data = data;
|
||||||
input->size = inlen;
|
input->size = inlen;
|
||||||
input->offset = 0;
|
input->offset = 0;
|
||||||
|
|
||||||
outlen = inlen;
|
outlen = inlen;
|
||||||
remainder = outlen % blocksize;
|
remainder = outlen % blocksize;
|
||||||
if (remainder != 0) {
|
if (remainder != 0) {
|
||||||
@ -288,7 +288,7 @@ Compressor_compress__(
|
|||||||
self->rtable = rtable;
|
self->rtable = rtable;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
cdata = PyString_FromStringAndSize(output->data, output->offset);
|
cdata = PyBytes_FromStringAndSize(output->data, output->offset);
|
||||||
if (cdata == NULL) {
|
if (cdata == NULL) {
|
||||||
Py_DECREF(rtable);
|
Py_DECREF(rtable);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -333,43 +333,42 @@ static PyMethodDef Compressor_methods[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
PyTypeObject CompressorType = {
|
PyTypeObject CompressorType = {
|
||||||
PyObject_HEAD_INIT(NULL)
|
PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
0, /*ob_size*/
|
/* tp_name */ "lzx.Compressor",
|
||||||
"lzx.Compressor", /*tp_name*/
|
/* tp_basicsize */ sizeof(Compressor),
|
||||||
sizeof(Compressor), /*tp_basicsize*/
|
/* tp_itemsize */ 0,
|
||||||
0, /*tp_itemsize*/
|
/* tp_dealloc */ (destructor)Compressor_dealloc,
|
||||||
(destructor)Compressor_dealloc, /*tp_dealloc*/
|
/* tp_print */ 0,
|
||||||
0, /*tp_print*/
|
/* tp_getattr */ 0,
|
||||||
0, /*tp_getattr*/
|
/* tp_setattr */ 0,
|
||||||
0, /*tp_setattr*/
|
/* tp_compare */ 0,
|
||||||
0, /*tp_compare*/
|
/* tp_repr */ 0,
|
||||||
0, /*tp_repr*/
|
/* tp_as_number */ 0,
|
||||||
0, /*tp_as_number*/
|
/* tp_as_sequence */ 0,
|
||||||
0, /*tp_as_sequence*/
|
/* tp_as_mapping */ 0,
|
||||||
0, /*tp_as_mapping*/
|
/* tp_hash */ 0,
|
||||||
0, /*tp_hash */
|
/* tp_call */ 0,
|
||||||
0, /*tp_call*/
|
/* tp_str */ 0,
|
||||||
0, /*tp_str*/
|
/* tp_getattro */ 0,
|
||||||
0, /*tp_getattro*/
|
/* tp_setattro */ 0,
|
||||||
0, /*tp_setattro*/
|
/* tp_as_buffer */ 0,
|
||||||
0, /*tp_as_buffer*/
|
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
|
/* tp_doc */ "Compressor objects",
|
||||||
"Compressor objects", /* tp_doc */
|
/* tp_traverse */ (traverseproc)Compressor_traverse,
|
||||||
(traverseproc)Compressor_traverse, /* tp_traverse */
|
/* tp_clear */ (inquiry)Compressor_clear,
|
||||||
(inquiry)Compressor_clear, /* tp_clear */
|
/* tp_richcompare */ 0,
|
||||||
0, /* tp_richcompare */
|
/* tp_weaklistoffset */ 0,
|
||||||
0, /* tp_weaklistoffset */
|
/* tp_iter */ 0,
|
||||||
0, /* tp_iter */
|
/* tp_iternext */ 0,
|
||||||
0, /* tp_iternext */
|
/* tp_methods */ Compressor_methods,
|
||||||
Compressor_methods, /* tp_methods */
|
/* tp_members */ Compressor_members,
|
||||||
Compressor_members, /* tp_members */
|
/* tp_getset */ 0,
|
||||||
0, /* tp_getset */
|
/* tp_base */ 0,
|
||||||
0, /* tp_base */
|
/* tp_dict */ 0,
|
||||||
0, /* tp_dict */
|
/* tp_descr_get */ 0,
|
||||||
0, /* tp_descr_get */
|
/* tp_descr_set */ 0,
|
||||||
0, /* tp_descr_set */
|
/* tp_dictoffset */ 0,
|
||||||
0, /* tp_dictoffset */
|
/* tp_init */ (initproc)Compressor_init,
|
||||||
(initproc)Compressor_init, /* tp_init */
|
/* tp_alloc */ 0,
|
||||||
0, /* tp_alloc */
|
/* tp_new */ Compressor_new,
|
||||||
Compressor_new, /* tp_new */
|
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
#include <lzxmodule.h>
|
#include <lzxmodule.h>
|
||||||
|
|
||||||
static char lzx_doc[] =
|
static char lzx_doc[] =
|
||||||
"Provide basic LZX compression and decompression using the code from\n"
|
"Provide basic LZX compression and decompression using the code from\n"
|
||||||
"liblzxcomp and libmspack respectively.";
|
"liblzxcomp and libmspack respectively.";
|
||||||
|
|
||||||
@ -68,13 +68,13 @@ glue_read(struct mspack_file *file, void *buffer, int bytes)
|
|||||||
|
|
||||||
mem = (memory_file *)file;
|
mem = (memory_file *)file;
|
||||||
if (mem->magic != 0xB5) return -1;
|
if (mem->magic != 0xB5) return -1;
|
||||||
|
|
||||||
remaining = mem->total_bytes - mem->current_bytes;
|
remaining = mem->total_bytes - mem->current_bytes;
|
||||||
if (!remaining) return 0;
|
if (!remaining) return 0;
|
||||||
if (bytes > remaining) bytes = remaining;
|
if (bytes > remaining) bytes = remaining;
|
||||||
memcpy(buffer, (unsigned char *)mem->buffer + mem->current_bytes, bytes);
|
memcpy(buffer, (unsigned char *)mem->buffer + mem->current_bytes, bytes);
|
||||||
mem->current_bytes += bytes;
|
mem->current_bytes += bytes;
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +86,7 @@ glue_write(struct mspack_file *file, void *buffer, int bytes)
|
|||||||
|
|
||||||
mem = (memory_file *)file;
|
mem = (memory_file *)file;
|
||||||
if (mem->magic != 0xB5) return -1;
|
if (mem->magic != 0xB5) return -1;
|
||||||
|
|
||||||
remaining = mem->total_bytes - mem->current_bytes;
|
remaining = mem->total_bytes - mem->current_bytes;
|
||||||
if (bytes > remaining) {
|
if (bytes > remaining) {
|
||||||
PyErr_SetString(LZXError,
|
PyErr_SetString(LZXError,
|
||||||
@ -99,7 +99,7 @@ glue_write(struct mspack_file *file, void *buffer, int bytes)
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct mspack_system lzxglue_system = {
|
struct mspack_system lzxglue_system = {
|
||||||
glue_open,
|
glue_open,
|
||||||
glue_close,
|
glue_close,
|
||||||
glue_read, /* Read */
|
glue_read, /* Read */
|
||||||
glue_write, /* Write */
|
glue_write, /* Write */
|
||||||
@ -125,7 +125,7 @@ init(PyObject *self, PyObject *args)
|
|||||||
if (!PyArg_ParseTuple(args, "i", &window)) {
|
if (!PyArg_ParseTuple(args, "i", &window)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
LZXwindow = window;
|
LZXwindow = window;
|
||||||
lzx_stream = NULL;
|
lzx_stream = NULL;
|
||||||
|
|
||||||
@ -143,7 +143,7 @@ reset(PyObject *self, PyObject *args)
|
|||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
//int LZXdecompress(unsigned char *inbuf, unsigned char *outbuf,
|
//int LZXdecompress(unsigned char *inbuf, unsigned char *outbuf,
|
||||||
// unsigned int inlen, unsigned int outlen)
|
// unsigned int inlen, unsigned int outlen)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
decompress(PyObject *self, PyObject *args)
|
decompress(PyObject *self, PyObject *args)
|
||||||
@ -161,12 +161,12 @@ decompress(PyObject *self, PyObject *args)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = PyString_FromStringAndSize(NULL, outlen);
|
retval = PyBytes_FromStringAndSize(NULL, outlen);
|
||||||
if (retval == NULL) {
|
if (retval == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
outbuf = (unsigned char *)PyString_AS_STRING(retval);
|
outbuf = (unsigned char *)PyBytes_AS_STRING(retval);
|
||||||
|
|
||||||
source.magic = 0xB5;
|
source.magic = 0xB5;
|
||||||
source.buffer = inbuf;
|
source.buffer = inbuf;
|
||||||
source.current_bytes = 0;
|
source.current_bytes = 0;
|
||||||
@ -176,9 +176,9 @@ decompress(PyObject *self, PyObject *args)
|
|||||||
dest.buffer = outbuf;
|
dest.buffer = outbuf;
|
||||||
dest.current_bytes = 0;
|
dest.current_bytes = 0;
|
||||||
dest.total_bytes = outlen;
|
dest.total_bytes = outlen;
|
||||||
|
|
||||||
lzx_stream = lzxd_init(&lzxglue_system, (struct mspack_file *)&source,
|
lzx_stream = lzxd_init(&lzxglue_system, (struct mspack_file *)&source,
|
||||||
(struct mspack_file *)&dest, LZXwindow,
|
(struct mspack_file *)&dest, LZXwindow,
|
||||||
0x7fff /* Never reset, I do it */, 4096, outlen);
|
0x7fff /* Never reset, I do it */, 4096, outlen);
|
||||||
err = -1;
|
err = -1;
|
||||||
if (lzx_stream) err = lzxd_decompress(lzx_stream, outlen);
|
if (lzx_stream) err = lzxd_decompress(lzx_stream, outlen);
|
||||||
@ -191,7 +191,7 @@ decompress(PyObject *self, PyObject *args)
|
|||||||
retval = NULL;
|
retval = NULL;
|
||||||
PyErr_SetString(LZXError, "LZX decompression failed");
|
PyErr_SetString(LZXError, "LZX decompression failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,20 +202,37 @@ static PyMethodDef lzx_methods[] = {
|
|||||||
{ NULL }
|
{ NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
CALIBRE_MODINIT_FUNC
|
#if PY_MAJOR_VERSION >= 3
|
||||||
initlzx(void)
|
#define INITERROR return NULL
|
||||||
{
|
#define INITMODULE PyModule_Create(&lzx_module)
|
||||||
PyObject *m;
|
static struct PyModuleDef lzx_module = {
|
||||||
|
/* m_base */ PyModuleDef_HEAD_INIT,
|
||||||
|
/* m_name */ "lzx",
|
||||||
|
/* m_doc */ lzx_doc,
|
||||||
|
/* m_size */ -1,
|
||||||
|
/* m_methods */ lzx_methods,
|
||||||
|
/* m_slots */ 0,
|
||||||
|
/* m_traverse */ 0,
|
||||||
|
/* m_clear */ 0,
|
||||||
|
/* m_free */ 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
CALIBRE_MODINIT_FUNC PyInit_lzx(void) {
|
||||||
|
#else
|
||||||
|
#define INITERROR return
|
||||||
|
#define INITMODULE Py_InitModule3("lzx", lzx_methods, lzx_doc);
|
||||||
|
CALIBRE_MODINIT_FUNC initlzx(void) {
|
||||||
|
#endif
|
||||||
|
|
||||||
if (PyType_Ready(&CompressorType) < 0) {
|
if (PyType_Ready(&CompressorType) < 0) {
|
||||||
return;
|
INITERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
m = Py_InitModule3("lzx", lzx_methods, lzx_doc);
|
PyObject *m = INITMODULE;
|
||||||
if (m == NULL) {
|
if (m == NULL) {
|
||||||
return;
|
INITERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
LZXError = PyErr_NewException("lzx.LZXError", NULL, NULL);
|
LZXError = PyErr_NewException("lzx.LZXError", NULL, NULL);
|
||||||
Py_INCREF(LZXError);
|
Py_INCREF(LZXError);
|
||||||
PyModule_AddObject(m, "LZXError", LZXError);
|
PyModule_AddObject(m, "LZXError", LZXError);
|
||||||
@ -223,5 +240,7 @@ initlzx(void)
|
|||||||
Py_INCREF(&CompressorType);
|
Py_INCREF(&CompressorType);
|
||||||
PyModule_AddObject(m, "Compressor", (PyObject *)&CompressorType);
|
PyModule_AddObject(m, "Compressor", (PyObject *)&CompressorType);
|
||||||
|
|
||||||
return;
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
return m;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user