1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Language bindings for Python */
3 /* Copyright(c) 2016-2017 LoRd_MuldeR <mulder2@gmx.de> */
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: */
11 /* The above copyright notice and this permission notice shall be included in all copies or */
12 /* substantial portions of the Software. */
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 /* ---------------------------------------------------------------------------------------------- */
21 #define MHASH_DISABLE_STL 1
23 #include <mhash_384.h>
26 /* ------------------------------------------------------------------------*/
27 /* METHOD IMPLEMENTATION */
28 /* ------------------------------------------------------------------------*/
30 static PyObject *MHashPy384_Create(PyObject *const self, PyObject *const args)
32 return PyLong_FromVoidPtr(new mhash_384::MHash384());
35 static PyObject *MHashPy384_Update(PyObject *const self, PyObject *const args)
37 PyObject *instance = NULL, *data = NULL, *offset = NULL, *len = NULL;
38 if (PyArg_UnpackTuple(args, "MHash384_Update", 2, 4, &instance, &data, &offset, &len))
40 if (PyLong_Check(instance) && PyBytes_Check(data))
42 void *const inst_ptr = PyLong_AsVoidPtr(instance);
45 const size_t total_size = PyBytes_Size(data);
46 const size_t offset_val = (offset && PyLong_Check(offset)) ? PyLong_AsSize_t(offset) : 0U;
47 if (offset_val < total_size)
49 const size_t len_val = (len && PyLong_Check(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))
53 reinterpret_cast<mhash_384::MHash384*>(inst_ptr)->update(reinterpret_cast<uint8_t*>(PyBytes_AsString(data)) + offset_val, len_val);
63 static PyObject *MHashPy384_Result(PyObject *const self, PyObject *const args)
65 uint8_t buffer[mhash_384::MHash384::HASH_LEN];
66 PyObject *instance = NULL;
67 if (PyArg_UnpackTuple(args, "MHash384_Update", 1, 1, &instance))
69 if (PyLong_Check(instance))
71 void *const inst_ptr = PyLong_AsVoidPtr(instance);
74 reinterpret_cast<mhash_384::MHash384*>(inst_ptr)->finalize(buffer);
75 return PyByteArray_FromStringAndSize(reinterpret_cast<const char*>(buffer), mhash_384::MHash384::HASH_LEN);
82 static PyObject *MHashPy384_FreeUp(PyObject *const self, PyObject *const args)
84 PyObject *instance = NULL;
85 if (PyArg_UnpackTuple(args, "MHash384_Update", 1, 1, &instance))
87 if (PyLong_Check(instance))
89 void *const inst_ptr = PyLong_AsVoidPtr(instance);
92 delete reinterpret_cast<mhash_384::MHash384*>(inst_ptr);
100 static PyObject *MHashPy384_GetVer(PyObject *const self, PyObject *const args)
102 uint16_t major, minor, patch;
103 mhash_384::MHash384::version(major, minor, patch);
104 return Py_BuildValue("(kkk)", (uint32_t)major, (uint32_t)minor, (uint32_t)patch);
107 /* ------------------------------------------------------------------------*/
108 /* MODULE INTERFACCE */
109 /* ------------------------------------------------------------------------*/
111 static PyMethodDef MHashPy384_Methods[] =
113 {"create", MHashPy384_Create, METH_NOARGS, "Create a new MHash384 instance and initialize it."},
114 {"update", MHashPy384_Update, METH_VARARGS, "Process next N bytes of input data. Pass the MHash384 instance followed by a PyBytes object."},
115 {"result", MHashPy384_Result, METH_VARARGS, "Return the final hash (digest) value."},
116 {"freeup", MHashPy384_FreeUp, METH_VARARGS, "Destorys an existing instace and frees all its memory."},
117 {"getver", MHashPy384_GetVer, METH_NOARGS, "Get the library version, returned as a tuple of (Major, Minor, Patch)."},
118 {NULL, NULL, 0, NULL}
121 static struct PyModuleDef MHash384_ModuleDef =
123 PyModuleDef_HEAD_INIT, "MHashPy384_Native", "", -1, MHashPy384_Methods
127 PyInit_MHashPy384_Native(void)
129 return PyModule_Create(&MHash384_ModuleDef);