From 8d393f9344650f361f76418ef2db8c9dfb4bba17 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Fri, 25 Mar 2016 15:04:48 +0100 Subject: [PATCH] Added language binding for Python. --- .gitignore | 2 + bindings/Python/wrapper/MHashPy384.sln | 28 +++ bindings/Python/wrapper/MHashPy384.vcxproj | 191 +++++++++++++++++++++ bindings/Python/wrapper/MHashPy384.vcxproj.filters | 25 +++ bindings/Python/wrapper/src/MHashPy384.cpp | 151 ++++++++++++++++ bindings/Python/wrapper/src/dllmain.cpp | 35 ++++ 6 files changed, 432 insertions(+) create mode 100644 bindings/Python/wrapper/MHashPy384.sln create mode 100644 bindings/Python/wrapper/MHashPy384.vcxproj create mode 100644 bindings/Python/wrapper/MHashPy384.vcxproj.filters create mode 100644 bindings/Python/wrapper/src/MHashPy384.cpp create mode 100644 bindings/Python/wrapper/src/dllmain.cpp diff --git a/.gitignore b/.gitignore index e019926..8f5f94f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,8 @@ /bindings/Microsoft.NET/MHashDotNet384.Wrapper/obj /bindings/Microsoft.NET/bin /bindings/Microsoft.NET/ipch +/bindings/Python/wrapper/bin +/bindings/Python/wrapper/obj /ipch /obj /out diff --git a/bindings/Python/wrapper/MHashPy384.sln b/bindings/Python/wrapper/MHashPy384.sln new file mode 100644 index 0000000..b2f3688 --- /dev/null +++ b/bindings/Python/wrapper/MHashPy384.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.24720.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MHashPy384", "MHashPy384.vcxproj", "{98971AF0-1B4D-4B5A-A174-9B099F7977B8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Debug|x64.ActiveCfg = Debug|x64 + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Debug|x64.Build.0 = Debug|x64 + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Debug|x86.ActiveCfg = Debug|Win32 + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Debug|x86.Build.0 = Debug|Win32 + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Release|x64.ActiveCfg = Release|x64 + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Release|x64.Build.0 = Release|x64 + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Release|x86.ActiveCfg = Release|Win32 + {98971AF0-1B4D-4B5A-A174-9B099F7977B8}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/bindings/Python/wrapper/MHashPy384.vcxproj b/bindings/Python/wrapper/MHashPy384.vcxproj new file mode 100644 index 0000000..9c0e5a1 --- /dev/null +++ b/bindings/Python/wrapper/MHashPy384.vcxproj @@ -0,0 +1,191 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + {98971AF0-1B4D-4B5A-A174-9B099F7977B8} + Win32Proj + MHashPy384 + 8.1 + + + + DynamicLibrary + true + v140_xp + Unicode + + + DynamicLibrary + false + v140_xp + true + Unicode + + + DynamicLibrary + true + v140_xp + Unicode + + + DynamicLibrary + false + v140_xp + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)\obj\$(PlatformShortName)\$(Configuration)\ + $(ProjectName).$(PlatformShortName) + .pyd + + + true + $(SolutionDir)\bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)\obj\$(PlatformShortName)\$(Configuration)\ + $(ProjectName).$(PlatformShortName) + .pyd + + + false + $(SolutionDir)\bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)\obj\$(PlatformShortName)\$(Configuration)\ + $(ProjectName).$(PlatformShortName) + .pyd + + + false + $(SolutionDir)\bin\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)\obj\$(PlatformShortName)\$(Configuration)\ + $(ProjectName).$(PlatformShortName) + .pyd + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;MHASHJAVA384_EXPORTS;%(PreprocessorDefinitions) + $(SolutionDir)\include;$(SolutionDir)\..\..\..\include;$(PYTHON_HOME_X86)\include + + + Windows + true + $(PYTHON_HOME_X86)\libs;%(AdditionalLibraryDirectories) + + + + + + + Level3 + Disabled + _DEBUG;_WINDOWS;_USRDLL;MHASHJAVA384_EXPORTS;%(PreprocessorDefinitions) + $(SolutionDir)\include;$(SolutionDir)\..\..\..\include;$(PYTHON_HOME_X64)\include + + + Windows + true + $(PYTHON_HOME_X64)\libs;%(AdditionalLibraryDirectories) + + + + + Level3 + + + Full + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;MHASHJAVA384_EXPORTS;%(PreprocessorDefinitions) + $(SolutionDir)\include;$(SolutionDir)\..\..\..\include;$(PYTHON_HOME_X86)\include + AnySuitable + Speed + true + true + MultiThreaded + StreamingSIMDExtensions + false + + + Windows + true + true + No + UseLinkTimeCodeGeneration + $(PYTHON_HOME_X86)\libs;%(AdditionalLibraryDirectories) + + + + + Level3 + + + Full + true + true + NDEBUG;_WINDOWS;_USRDLL;MHASHJAVA384_EXPORTS;%(PreprocessorDefinitions) + $(SolutionDir)\include;$(SolutionDir)\..\..\..\include;$(PYTHON_HOME_X64)\include + AnySuitable + Speed + true + true + MultiThreaded + NotSet + false + + + Windows + true + true + No + UseLinkTimeCodeGeneration + $(PYTHON_HOME_X64)\libs;%(AdditionalLibraryDirectories) + + + + + + \ No newline at end of file diff --git a/bindings/Python/wrapper/MHashPy384.vcxproj.filters b/bindings/Python/wrapper/MHashPy384.vcxproj.filters new file mode 100644 index 0000000..b3baaa9 --- /dev/null +++ b/bindings/Python/wrapper/MHashPy384.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/bindings/Python/wrapper/src/MHashPy384.cpp b/bindings/Python/wrapper/src/MHashPy384.cpp new file mode 100644 index 0000000..10f3baf --- /dev/null +++ b/bindings/Python/wrapper/src/MHashPy384.cpp @@ -0,0 +1,151 @@ +/* ---------------------------------------------------------------------------------------------- */ +/* MHash-384 - Language bindings for Java */ +/* Copyright(c) 2016 LoRd_MuldeR */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */ +/* and associated documentation files (the "Software"), to deal in the Software without */ +/* restriction, including without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the */ +/* Software is furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in all copies or */ +/* substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */ +/* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ +/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */ +/* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* ---------------------------------------------------------------------------------------------- */ + +#define MHASH_DISABLE_STL 1 + +#include +#include + +/* ------------------------------------------------------------------------*/ +/* METHOD IMPLEMENTATION */ +/* ------------------------------------------------------------------------*/ + +static PyObject *MHash384_Create(PyObject *const self, PyObject *const args) +{ + return PyLong_FromVoidPtr(new mhash::MHash384()); +} + +static PyObject *MHash384_Update(PyObject *const self, PyObject *const args) +{ + PyObject *instance = NULL, *data = NULL; + if (PyArg_UnpackTuple(args, "MHash384_Update", 2, 2, &instance, &data)) + { + if (PyLong_Check(instance) && PyBytes_Check(data)) + { + void *const inst_ptr = PyLong_AsVoidPtr(instance); + if (inst_ptr) + { + reinterpret_cast(inst_ptr)->update(reinterpret_cast(PyBytes_AsString(data)), PyBytes_Size(data)); + Py_RETURN_TRUE; + } + } + } + Py_RETURN_FALSE; +} + +static PyObject *MHash384_Result(PyObject *const self, PyObject *const args) +{ + uint8_t buffer[MHASH_384_LEN]; + PyObject *instance = NULL; + if (PyArg_UnpackTuple(args, "MHash384_Update", 1, 1, &instance)) + { + if (PyLong_Check(instance)) + { + void *const inst_ptr = PyLong_AsVoidPtr(instance); + if (inst_ptr) + { + reinterpret_cast(inst_ptr)->finalize(buffer); + return PyByteArray_FromStringAndSize(reinterpret_cast(buffer), MHASH_384_LEN); + } + } + } + Py_RETURN_NONE; +} + +static PyObject *MHash384_FreeUp(PyObject *const self, PyObject *const args) +{ + PyObject *instance = NULL; + if (PyArg_UnpackTuple(args, "MHash384_Update", 1, 1, &instance)) + { + if (PyLong_Check(instance)) + { + void *const inst_ptr = PyLong_AsVoidPtr(instance); + if (inst_ptr) + { + delete reinterpret_cast(inst_ptr); + Py_RETURN_TRUE; + } + } + } + Py_RETURN_FALSE; +} + +/* ------------------------------------------------------------------------*/ +/* MODULE INTERFACCE */ +/* ------------------------------------------------------------------------*/ + +static PyMethodDef MHash384_Methods[] = +{ + {"create", MHash384_Create, METH_NOARGS, "Create a new MHash384 instance and initialize it."}, + {"update", MHash384_Update, METH_VARARGS, "Process next N bytes of input data. Pass the MHash384 instance followed by a PyBytes object."}, + {"result", MHash384_Result, METH_VARARGS, "Return the final hash (digest) value."}, + {"freeup", MHash384_FreeUp, METH_VARARGS, "Destorys an existing instace and frees all its memory."}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef MHash384_ModuleDef = +{ + PyModuleDef_HEAD_INIT, "MHash384", "", -1, MHash384_Methods +}; + +PyMODINIT_FUNC +PyInit_MHash384(void) +{ + return PyModule_Create(&MHash384_ModuleDef); +} + +/* +JNIEXPORT jlong JNICALL Java_mhash_MHash384_00024Internals_createInstance(JNIEnv*, jclass) +{ + return reinterpret_cast(new mhash::MHash384()); +} + +JNIEXPORT void JNICALL Java_mhash_MHash384_00024Internals_update(JNIEnv *const env, jclass, const jlong instance, const jbyteArray input, const jint offset, const jint len) +{ + jbyte *const input_ptr = env->GetByteArrayElements(input, NULL); + reinterpret_cast(instance)->update(reinterpret_cast(input_ptr + offset), len); + env->ReleaseByteArrayElements(input, input_ptr, JNI_ABORT); +} + +JNIEXPORT jbyteArray JNICALL Java_mhash_MHash384_00024Internals_finalize(JNIEnv *const env, jclass, const jlong instance) +{ + const jbyteArray result = env->NewByteArray(MHASH_384_LEN); + jbyte *const result_ptr = env->GetByteArrayElements(result, NULL); + reinterpret_cast(instance)->finalize(reinterpret_cast(result_ptr)); + env->ReleaseByteArrayElements(result, result_ptr, 0); + return result; +} + +JNIEXPORT void JNICALL Java_mhash_MHash384_00024Internals_destroyInstance(JNIEnv*, jclass, const jlong instance) +{ + delete reinterpret_cast(instance); +} + +JNIEXPORT jintArray JNICALL Java_mhash_MHash384_00024Internals_getVersion(JNIEnv *const env, jclass) +{ + const jintArray version = env->NewIntArray(3); + jint *const version_ptr = env->GetIntArrayElements(version, NULL); + version_ptr[0] = (jint) MHASH_384_VERSION_MAJOR; + version_ptr[1] = (jint) MHASH_384_VERSION_MINOR; + version_ptr[2] = (jint) MHASH_384_VERSION_PATCH; + env->ReleaseIntArrayElements(version, version_ptr, 0); + return version; +} +*/ diff --git a/bindings/Python/wrapper/src/dllmain.cpp b/bindings/Python/wrapper/src/dllmain.cpp new file mode 100644 index 0000000..747a984 --- /dev/null +++ b/bindings/Python/wrapper/src/dllmain.cpp @@ -0,0 +1,35 @@ +/* ---------------------------------------------------------------------------------------------- */ +/* MHash-384 - Language bindings for Java */ +/* Copyright(c) 2016 LoRd_MuldeR */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */ +/* and associated documentation files (the "Software"), to deal in the Software without */ +/* restriction, including without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the */ +/* Software is furnished to do so, subject to the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be included in all copies or */ +/* substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */ +/* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ +/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */ +/* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* ---------------------------------------------------------------------------------------------- */ + +#define WIN32_LEAN_AND_MEAN 1 +#include + +BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} -- 2.11.0