From: LoRd_MuldeR Date: Fri, 25 Mar 2016 17:51:53 +0000 (+0100) Subject: Updated Python bindings. X-Git-Tag: 1.0.1~27 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;ds=sidebyside;h=6a31381fd5f98f1bcbcb140473f87e92798db495;p=mhash384%2Fmhash384.git Updated Python bindings. --- diff --git a/.gitignore b/.gitignore index 8f5f94f..15062ba 100644 --- a/.gitignore +++ b/.gitignore @@ -15,8 +15,8 @@ /bindings/Microsoft.NET/MHashDotNet384.Wrapper/obj /bindings/Microsoft.NET/bin /bindings/Microsoft.NET/ipch -/bindings/Python/wrapper/bin -/bindings/Python/wrapper/obj +/bindings/Python/native/bin +/bindings/Python/native/obj /ipch /obj /out diff --git a/Make.cmd b/Make.cmd index 35689e4..1b6ec3e 100644 --- a/Make.cmd +++ b/Make.cmd @@ -101,7 +101,7 @@ for %%i in (native\obj,native\bin,warapper\bin,warapper\out) do ( del /Q /S /F "%~dp0\bindings\Java\%%i\*.*" ) -for %%i in (wrapper\obj,wrapper\out) do ( +for %%i in (native\obj,native\out) do ( del /Q /S /F "%~dp0\bindings\Python\%%i\*.*" ) @@ -119,7 +119,7 @@ call "%MSVC_PATH%\vcvarsall.bat" set "MSVC_PROJECTS=MHashLib.sln" set "MSVC_PROJECTS=%MSVC_PROJECTS%,bindings\Microsoft.NET\MHashDotNet384.sln" set "MSVC_PROJECTS=%MSVC_PROJECTS%,bindings\Java\native\MHashJava384.sln" -set "MSVC_PROJECTS=%MSVC_PROJECTS%,bindings\Python\wrapper\MHashPy384.sln" +set "MSVC_PROJECTS=%MSVC_PROJECTS%,bindings\Python\native\MHashPy384_Native.sln" for %%q in (%MSVC_PROJECTS%) do ( for %%p in (x86,x64) do ( @@ -197,8 +197,8 @@ REM /////////////////////////////////////////////////////////////////////////// "%~dp0\tools\zip.exe" -j -9 -z "%OUT_PATH_JNI_X86%" "%~dp0\bindings\Java\native\bin\x86\Release\MHashJava384.x86.dll" "%~dp0\bindings\Java\wrapper\out\MHashJava384-Wrapper.jar" "%~dp0\bindings\Java\example\out\MHashJava384-Example.jar" "%~dp0\README.html" "%~dp0\COPYING.txt" < "%~dp0\COPYING.txt" "%~dp0\tools\zip.exe" -j -9 -z "%OUT_PATH_JNI_X64%" "%~dp0\bindings\Java\native\bin\x64\Release\MHashJava384.x64.dll" "%~dp0\bindings\Java\wrapper\out\MHashJava384-Wrapper.jar" "%~dp0\bindings\Java\example\out\MHashJava384-Example.jar" "%~dp0\README.html" "%~dp0\COPYING.txt" < "%~dp0\COPYING.txt" -"%~dp0\tools\zip.exe" -j -9 -z "%OUT_PATH_PYC_X86%" "%~dp0\bindings\Python\wrapper\bin\x86\Release\MHashPy384_Impl.x86.pyd" "%~dp0\bindings\Python\include\MHashPy384.py" "%~dp0\bindings\Python\include\mhash.pth" "%~dp0\bindings\Python\example\Example.py" "%~dp0\README.html" "%~dp0\COPYING.txt" < "%~dp0\COPYING.txt" -"%~dp0\tools\zip.exe" -j -9 -z "%OUT_PATH_PYC_X64%" "%~dp0\bindings\Python\wrapper\bin\x64\Release\MHashPy384_Impl.x64.pyd" "%~dp0\bindings\Python\include\MHashPy384.py" "%~dp0\bindings\Python\include\mhash.pth" "%~dp0\bindings\Python\example\Example.py" "%~dp0\README.html" "%~dp0\COPYING.txt" < "%~dp0\COPYING.txt" +"%~dp0\tools\zip.exe" -j -9 -z "%OUT_PATH_PYC_X86%" "%~dp0\bindings\Python\native\bin\x86\Release\MHashPy384_Native.x86.pyd" "%~dp0\bindings\Python\wrapper\MHashPy384_Wrapper.py" "%~dp0\bindings\Python\wrapper\mhash.pth" "%~dp0\bindings\Python\example\Example.py" "%~dp0\README.html" "%~dp0\COPYING.txt" < "%~dp0\COPYING.txt" +"%~dp0\tools\zip.exe" -j -9 -z "%OUT_PATH_PYC_X64%" "%~dp0\bindings\Python\native\bin\x64\Release\MHashPy384_Native.x64.pyd" "%~dp0\bindings\Python\wrapper\MHashPy384_Wrapper.py" "%~dp0\bindings\Python\wrapper\mhash.pth" "%~dp0\bindings\Python\example\Example.py" "%~dp0\README.html" "%~dp0\COPYING.txt" < "%~dp0\COPYING.txt" "%GIT2_PATH%\git.exe" archive --format tar.gz -9 --verbose --output "%OUT_PATH_SRC_GEN%" HEAD diff --git a/bindings/Python/example/Example.py b/bindings/Python/example/Example.py index 9af5aae..53cfca3 100644 --- a/bindings/Python/example/Example.py +++ b/bindings/Python/example/Example.py @@ -20,8 +20,7 @@ import sys import binascii - -from MHashPy384 import MHash384 +from MHashPy384_Wrapper import MHash384 def read_chunks(fs, chunk_size=4096): while True: @@ -30,12 +29,15 @@ def read_chunks(fs, chunk_size=4096): break yield data -def main(): +def main(argv): + if(len(argv) < 1): + print("Command-line argument is missing!") + return with MHash384() as digest: - with open('E:\\ViDeOz\\Helges gitarrensolo in Texas.mp4', 'rb') as fs: + with open(argv[0], 'rb') as fs: for chunk in read_chunks(fs): digest.update(chunk) print(binascii.hexlify(digest.result())) if __name__ == "__main__": - main() + main(sys.argv[1:]) diff --git a/bindings/Python/wrapper/MHashPy384.sln b/bindings/Python/native/MHashPy384_Native.sln similarity index 88% rename from bindings/Python/wrapper/MHashPy384.sln rename to bindings/Python/native/MHashPy384_Native.sln index b2f3688..7ac3912 100644 --- a/bindings/Python/wrapper/MHashPy384.sln +++ b/bindings/Python/native/MHashPy384_Native.sln @@ -3,7 +3,7 @@ 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}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MHashPy384_Native", "MHashPy384_Native.vcxproj", "{98971AF0-1B4D-4B5A-A174-9B099F7977B8}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/bindings/Python/wrapper/MHashPy384.vcxproj b/bindings/Python/native/MHashPy384_Native.vcxproj similarity index 98% rename from bindings/Python/wrapper/MHashPy384.vcxproj rename to bindings/Python/native/MHashPy384_Native.vcxproj index e16757d..3d5675b 100644 --- a/bindings/Python/wrapper/MHashPy384.vcxproj +++ b/bindings/Python/native/MHashPy384_Native.vcxproj @@ -25,9 +25,9 @@ {98971AF0-1B4D-4B5A-A174-9B099F7977B8} Win32Proj - MHashPy384_Impl + MHashPy384_Native 8.1 - MHashPy384_Impl + MHashPy384_Native diff --git a/bindings/Python/wrapper/MHashPy384.vcxproj.filters b/bindings/Python/native/MHashPy384_Native.vcxproj.filters similarity index 100% rename from bindings/Python/wrapper/MHashPy384.vcxproj.filters rename to bindings/Python/native/MHashPy384_Native.vcxproj.filters diff --git a/bindings/Python/wrapper/src/MHashPy384.cpp b/bindings/Python/native/src/MHashPy384.cpp similarity index 84% rename from bindings/Python/wrapper/src/MHashPy384.cpp rename to bindings/Python/native/src/MHashPy384.cpp index 8175924..3b8da7e 100644 --- a/bindings/Python/wrapper/src/MHashPy384.cpp +++ b/bindings/Python/native/src/MHashPy384.cpp @@ -34,16 +34,26 @@ static PyObject *MHashPy384_Create(PyObject *const self, PyObject *const args) static PyObject *MHashPy384_Update(PyObject *const self, PyObject *const args) { - PyObject *instance = NULL, *data = NULL; - if (PyArg_UnpackTuple(args, "MHash384_Update", 2, 2, &instance, &data)) + PyObject *instance = NULL, *data = NULL, *offset = NULL, *len = NULL; + if (PyArg_UnpackTuple(args, "MHash384_Update", 2, 4, &instance, &data, &offset, &len)) { 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; + const size_t total_size = PyBytes_Size(data); + const size_t offset_val = offset ? PyLong_AsSize_t(offset) : 0U; + if (offset_val < total_size) + { + const size_t len_val = len ? PyLong_AsSize_t(len) : (total_size - offset_val); + const size_t sum = offset_val + len_val; + if ((sum >= offset_val) && (sum >= len_val) && (sum < total_size)) + { + reinterpret_cast(inst_ptr)->update(reinterpret_cast(PyBytes_AsString(data)) + offset_val, len_val); + Py_RETURN_TRUE; + } + } } } } @@ -102,11 +112,11 @@ static PyMethodDef MHashPy384_Methods[] = static struct PyModuleDef MHash384_ModuleDef = { - PyModuleDef_HEAD_INIT, "MHashPy384_Impl", "", -1, MHashPy384_Methods + PyModuleDef_HEAD_INIT, "MHashPy384_Native", "", -1, MHashPy384_Methods }; PyMODINIT_FUNC -PyInit_MHashPy384_Impl(void) +PyInit_MHashPy384_Native(void) { return PyModule_Create(&MHash384_ModuleDef); } diff --git a/bindings/Python/wrapper/src/dllmain.cpp b/bindings/Python/native/src/dllmain.cpp similarity index 100% rename from bindings/Python/wrapper/src/dllmain.cpp rename to bindings/Python/native/src/dllmain.cpp diff --git a/bindings/Python/include/MHashPy384.py b/bindings/Python/wrapper/MHashPy384_Wrapper.py similarity index 91% rename from bindings/Python/include/MHashPy384.py rename to bindings/Python/wrapper/MHashPy384_Wrapper.py index aa8a63f..15ee067 100644 --- a/bindings/Python/include/MHashPy384.py +++ b/bindings/Python/wrapper/MHashPy384_Wrapper.py @@ -18,17 +18,17 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ################################################################################################## -import MHashPy384_Impl as impl +import MHashPy384_Native as native class MHash384: def __init__(self): self.__handle = None def __enter__(self): - self.__handle = impl.create() + self.__handle = native.create() return self def update(self, data): - impl.update(self.__handle, data) + native.update(self.__handle, data) def result(self): - return impl.result(self.__handle) + return native.result(self.__handle) def __exit__(self, type, value, traceback): - impl.freeup(self.__handle) + native.freeup(self.__handle) diff --git a/bindings/Python/include/mhash.pth b/bindings/Python/wrapper/mhash.pth similarity index 100% rename from bindings/Python/include/mhash.pth rename to bindings/Python/wrapper/mhash.pth