OSDN Git Service

Updated Python bindings.
[mhash384/mhash384.git] / bindings / Python / native / src / MHashPy384.cpp
1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Language bindings for Python                                                       */
3 /* Copyright(c) 2016 LoRd_MuldeR <mulder2@gmx.de>                                                 */
4 /*                                                                                                */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software  */
6 /* and associated documentation files (the "Software"), to deal in the Software without           */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish,     */
8 /* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the  */
9 /* Software is furnished to do so, subject to the following conditions:                           */
10 /*                                                                                                */
11 /* The above copyright notice and this permission notice shall be included in all copies or       */
12 /* substantial portions of the Software.                                                          */
13 /*                                                                                                */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING  */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND     */
16 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.        */
19 /* ---------------------------------------------------------------------------------------------- */
20
21 #define MHASH_DISABLE_STL 1
22
23 #include <mhash_384.h>
24 #include <Python.h>
25
26 /* ------------------------------------------------------------------------*/
27 /* METHOD IMPLEMENTATION                                                   */
28 /* ------------------------------------------------------------------------*/
29
30 static PyObject *MHashPy384_Create(PyObject *const self, PyObject *const args)
31 {
32         return PyLong_FromVoidPtr(new mhash::MHash384());
33 }
34
35 static PyObject *MHashPy384_Update(PyObject *const self, PyObject *const args)
36 {
37         PyObject *instance = NULL, *data = NULL, *offset = NULL, *len = NULL;
38         if (PyArg_UnpackTuple(args, "MHash384_Update", 2, 4, &instance, &data, &offset, &len))
39         {
40                 if (PyLong_Check(instance) && PyBytes_Check(data))
41                 {
42                         void *const inst_ptr = PyLong_AsVoidPtr(instance);
43                         if (inst_ptr)
44                         {
45                                 const size_t total_size = PyBytes_Size(data);
46                                 const size_t offset_val = offset ? PyLong_AsSize_t(offset) : 0U;
47                                 if (offset_val < total_size)
48                                 {
49                                         const size_t len_val = len ? PyLong_AsSize_t(len) : (total_size - offset_val);
50                                         const size_t sum = offset_val + len_val;
51                                         if ((sum >= offset_val) && (sum >= len_val) && (sum < total_size))
52                                         {
53                                                 reinterpret_cast<mhash::MHash384*>(inst_ptr)->update(reinterpret_cast<uint8_t*>(PyBytes_AsString(data)) + offset_val, len_val);
54                                                 Py_RETURN_TRUE;
55                                         }
56                                 }
57                         }
58                 }
59         }
60         Py_RETURN_FALSE;
61 }
62
63 static PyObject *MHashPy384_Result(PyObject *const self, PyObject *const args)
64 {
65         uint8_t buffer[MHASH_384_LEN];
66         PyObject *instance = NULL;
67         if (PyArg_UnpackTuple(args, "MHash384_Update", 1, 1, &instance))
68         {
69                 if (PyLong_Check(instance))
70                 {
71                         void *const inst_ptr = PyLong_AsVoidPtr(instance);
72                         if (inst_ptr)
73                         {
74                                 reinterpret_cast<mhash::MHash384*>(inst_ptr)->finalize(buffer);
75                                 return PyByteArray_FromStringAndSize(reinterpret_cast<const char*>(buffer), MHASH_384_LEN);
76                         }
77                 }
78         }
79         Py_RETURN_NONE;
80 }
81
82 static PyObject *MHashPy384_FreeUp(PyObject *const self, PyObject *const args)
83 {
84         PyObject *instance = NULL;
85         if (PyArg_UnpackTuple(args, "MHash384_Update", 1, 1, &instance))
86         {
87                 if (PyLong_Check(instance))
88                 {
89                         void *const inst_ptr = PyLong_AsVoidPtr(instance);
90                         if (inst_ptr)
91                         {
92                                 delete reinterpret_cast<mhash::MHash384*>(inst_ptr);
93                                 Py_RETURN_TRUE;
94                         }
95                 }
96         }
97         Py_RETURN_FALSE;
98 }
99
100 /* ------------------------------------------------------------------------*/
101 /* MODULE INTERFACCE                                                       */
102 /* ------------------------------------------------------------------------*/
103
104 static PyMethodDef MHashPy384_Methods[] =
105 {
106         {"create", MHashPy384_Create, METH_NOARGS,  "Create a new MHash384 instance and initialize it."},
107         {"update", MHashPy384_Update, METH_VARARGS, "Process next N bytes of input data. Pass the MHash384 instance followed by a PyBytes object."},
108         {"result", MHashPy384_Result, METH_VARARGS, "Return the final hash (digest) value."},
109         {"freeup", MHashPy384_FreeUp, METH_VARARGS, "Destorys an existing instace and frees all its memory."},
110         {NULL, NULL, 0, NULL}
111 };
112
113 static struct PyModuleDef MHash384_ModuleDef =
114 {
115         PyModuleDef_HEAD_INIT, "MHashPy384_Native", "", -1, MHashPy384_Methods
116 };
117
118 PyMODINIT_FUNC
119 PyInit_MHashPy384_Native(void)
120 {
121         return PyModule_Create(&MHash384_ModuleDef);
122 }