Port msdes to python 3

Fixes #937 (Port msdes plugin to build on python2/python3 )
This commit is contained in:
Kovid Goyal 2019-02-27 08:35:49 +05:30
parent 9fba382f7f
commit 7a99290a14
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -9,8 +9,7 @@
#include <d3des.h> #include <d3des.h>
static char msdes_doc[] = static const char msdes_doc[] = "Provide LIT-specific DES en/decryption.";
"Provide LIT-specific DES en/decryption.";
static PyObject *MsDesError = NULL; static PyObject *MsDesError = NULL;
@ -21,7 +20,11 @@ msdes_deskey(PyObject *self, PyObject *args)
unsigned int len = 0; unsigned int len = 0;
short int edf = 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)) { if (!PyArg_ParseTuple(args, "s#h", &key, &len, &edf)) {
#endif
return NULL; return NULL;
} }
@ -49,7 +52,11 @@ msdes_des(PyObject *self, PyObject *args)
unsigned int off = 0; unsigned int off = 0;
PyObject *retval = NULL; PyObject *retval = NULL;
#if PY_MAJOR_VERSION >= 3
if (!PyArg_ParseTuple(args, "y#", &inbuf, &len)) {
#else
if (!PyArg_ParseTuple(args, "s#", &inbuf, &len)) { if (!PyArg_ParseTuple(args, "s#", &inbuf, &len)) {
#endif
return NULL; return NULL;
} }
@ -59,11 +66,11 @@ msdes_des(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
retval = PyString_FromStringAndSize(NULL, len); retval = PyBytes_FromStringAndSize(NULL, len);
if (retval == NULL) { if (retval == NULL) {
return NULL; return NULL;
} }
outbuf = (unsigned char *)PyString_AS_STRING(retval); outbuf = (unsigned char *)PyBytes_AS_STRING(retval);
for (off = 0; off < len; off += 8) { for (off = 0; off < len; off += 8) {
des((inbuf + off), (outbuf + off)); des((inbuf + off), (outbuf + off));
@ -78,21 +85,44 @@ static PyMethodDef msdes_methods[] = {
{ NULL, NULL } { NULL, NULL }
}; };
CALIBRE_MODINIT_FUNC #if PY_MAJOR_VERSION >= 3
initmsdes(void) #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; PyObject *m;
m = Py_InitModule3("msdes", msdes_methods, msdes_doc); m = INITMODULE;
if (m == NULL) { if (m == NULL) {
return; INITERROR;
} }
MsDesError = PyErr_NewException("msdes.MsDesError", NULL, NULL); MsDesError = PyErr_NewException("msdes.MsDesError", NULL, NULL);
Py_INCREF(MsDesError); Py_INCREF(MsDesError);
PyModule_AddObject(m, "MsDesError", 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, "EN0", PyInt_FromLong(EN0));
PyModule_AddObject(m, "DE1", PyInt_FromLong(DE1)); PyModule_AddObject(m, "DE1", PyInt_FromLong(DE1));
#endif
return;
} }