mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Port msdes to python 3
Fixes #937 (Port msdes plugin to build on python2/python3 )
This commit is contained in:
parent
9fba382f7f
commit
7a99290a14
@ -9,8 +9,7 @@
|
||||
|
||||
#include <d3des.h>
|
||||
|
||||
static char msdes_doc[] =
|
||||
"Provide LIT-specific DES en/decryption.";
|
||||
static const char msdes_doc[] = "Provide LIT-specific DES en/decryption.";
|
||||
|
||||
static PyObject *MsDesError = NULL;
|
||||
|
||||
@ -21,7 +20,11 @@ msdes_deskey(PyObject *self, PyObject *args)
|
||||
unsigned int len = 0;
|
||||
short int edf = 0;
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
if (!PyArg_ParseTuple(args, "y#h", &key, &len, &edf)) {
|
||||
#else
|
||||
if (!PyArg_ParseTuple(args, "s#h", &key, &len, &edf)) {
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -33,10 +36,10 @@ msdes_deskey(PyObject *self, PyObject *args)
|
||||
if ((edf != EN0) && (edf != DE1)) {
|
||||
PyErr_SetString(MsDesError, "En/decryption direction invalid");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
deskey(key, edf);
|
||||
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
@ -49,7 +52,11 @@ msdes_des(PyObject *self, PyObject *args)
|
||||
unsigned int off = 0;
|
||||
PyObject *retval = NULL;
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
if (!PyArg_ParseTuple(args, "y#", &inbuf, &len)) {
|
||||
#else
|
||||
if (!PyArg_ParseTuple(args, "s#", &inbuf, &len)) {
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -59,16 +66,16 @@ msdes_des(PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
retval = PyString_FromStringAndSize(NULL, len);
|
||||
retval = PyBytes_FromStringAndSize(NULL, len);
|
||||
if (retval == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
outbuf = (unsigned char *)PyString_AS_STRING(retval);
|
||||
outbuf = (unsigned char *)PyBytes_AS_STRING(retval);
|
||||
|
||||
for (off = 0; off < len; off += 8) {
|
||||
des((inbuf + off), (outbuf + off));
|
||||
}
|
||||
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
@ -78,21 +85,44 @@ static PyMethodDef msdes_methods[] = {
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
CALIBRE_MODINIT_FUNC
|
||||
initmsdes(void)
|
||||
{
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define INITERROR return NULL
|
||||
#define INITMODULE PyModule_Create(&msdes_module)
|
||||
static struct PyModuleDef msdes_module = {
|
||||
/* m_base */ PyModuleDef_HEAD_INIT,
|
||||
/* m_name */ "msdes",
|
||||
/* m_doc */ msdes_doc,
|
||||
/* m_size */ -1,
|
||||
/* m_methods */ msdes_methods,
|
||||
/* m_slots */ 0,
|
||||
/* m_traverse */ 0,
|
||||
/* m_clear */ 0,
|
||||
/* m_free */ 0,
|
||||
};
|
||||
CALIBRE_MODINIT_FUNC PyInit_msdes(void) {
|
||||
#else
|
||||
#define INITERROR return
|
||||
#define INITMODULE Py_InitModule3("msdes", msdes_methods, msdes_doc)
|
||||
CALIBRE_MODINIT_FUNC initmsdes(void) {
|
||||
#endif
|
||||
|
||||
PyObject *m;
|
||||
|
||||
m = Py_InitModule3("msdes", msdes_methods, msdes_doc);
|
||||
m = INITMODULE;
|
||||
if (m == NULL) {
|
||||
return;
|
||||
INITERROR;
|
||||
}
|
||||
|
||||
|
||||
MsDesError = PyErr_NewException("msdes.MsDesError", NULL, NULL);
|
||||
Py_INCREF(MsDesError);
|
||||
PyModule_AddObject(m, "MsDesError", MsDesError);
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
PyModule_AddObject(m, "EN0", PyLong_FromLong(EN0));
|
||||
PyModule_AddObject(m, "DE1", PyLong_FromLong(DE1));
|
||||
|
||||
return m;
|
||||
#else
|
||||
PyModule_AddObject(m, "EN0", PyInt_FromLong(EN0));
|
||||
PyModule_AddObject(m, "DE1", PyInt_FromLong(DE1));
|
||||
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user