-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test if native lib is loading properly
- Loading branch information
sken
committed
Mar 24, 2020
1 parent
47effd1
commit 5f4e17d
Showing
27 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*.o | ||
*.so | ||
*.class | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#JNI Bindings for Blake3 | ||
WIP | ||
|
||
C Bindings for the API of [Blake 3](https://github.com/BLAKE3-team/BLAKE3) cryptographic hash function | ||
|
||
Generating the JNI header | ||
``` | ||
javac *NativeBlake3.java -h src/main/resources/include/ | ||
``` | ||
|
||
The library is cross compiled from linux | ||
|
||
for more platforms go here [here](https://github.com/BLAKE3-team/BLAKE3/tree/master/c) | ||
|
||
####Linux | ||
|
||
``` | ||
cd src/main/c | ||
gcc -fPIC -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux \ | ||
-shared -o ../../../natives/linux_64/libblake3.so blake3.c blake3_dispatch.c blake3_portable.c \ | ||
blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S \ | ||
blake3_avx512_x86-64_unix.S org_scash_NativeBLAKE3.c | ||
``` | ||
|
||
####Windows 64 | ||
|
||
``` | ||
cd src/main/c | ||
x86_64-w64-mingw32-gcc -I/usr/lib/jvm/default-java/include -shared -o ../../../natives/windows_64/blake3.dll \ | ||
blake3.c blake3_dispatch.c blake3_portable.c blake3_sse41_x86-64_windows_gnu.S blake3_avx512_x86-64_windows_gnu.S \ | ||
blake3_avx2_x86-64_windows_gnu.S org_scash_NativeBLAKE3.c | ||
``` | ||
|
||
This would result in the native library binaries to be inside the `/natives/` folder. The project will automatically detect the OS | ||
and load the correct binary |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include "blake3.h" | ||
#include "org_scash_NativeBLAKE3.h" | ||
|
||
JNIEXPORT jlong JNICALL Java_org_scash_NativeBLAKE3_createHasher | ||
(JNIEnv *env, jclass classObject) | ||
{ | ||
blake3_hasher *hasher = malloc (sizeof (blake3_hasher)); | ||
|
||
(void)classObject;(void)env; | ||
|
||
return (uintptr_t)hasher; | ||
} | ||
|
||
JNIEXPORT void JNICALL Java_org_scash_NativeBLAKE3_destroyHasher | ||
(JNIEnv *env, jclass classObject, jlong lp) | ||
{ | ||
blake3_hasher *hasher = (blake3_hasher*)(uintptr_t)lp; | ||
if (hasher != NULL) { | ||
free(hasher); | ||
} | ||
(void)classObject;(void)env; | ||
return; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.scash; | ||
import org.scijava.nativelib.NativeLoader; | ||
|
||
public class NativeBLAKE3 { | ||
private static final boolean enabled; | ||
|
||
static { | ||
boolean isEnabled = false; | ||
try { | ||
NativeLoader.loadLibrary("blake3"); | ||
isEnabled = true; | ||
} catch (java.io.IOException e) { | ||
System.out.println("UnsatisfiedLinkError: " + e.toString()); | ||
} finally { | ||
enabled = isEnabled; | ||
} | ||
} | ||
|
||
public static boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
private static native long createHasher(); | ||
|
||
private static native void destroyHasher(long hasher); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.scash; | ||
|
||
public class NativeBLAKE3Util { | ||
|
||
public static void assertEquals(boolean val, boolean val2, String message) throws AssertFailException { | ||
if (val != val2) | ||
throw new AssertFailException("FAIL: " + message); | ||
else | ||
System.out.println("PASS: " + message); | ||
} | ||
|
||
public static class AssertFailException extends Exception { | ||
public AssertFailException(String message) { | ||
super(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.scash; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.scash.NativeBLAKE3Util.assertEquals; | ||
import static org.scash.NativeBLAKE3Util.AssertFailException; | ||
|
||
public class NativeBLAKE3Test { | ||
@Test | ||
public void testLibrary() throws AssertFailException { | ||
assertEquals(NativeBLAKE3.isEnabled(), true, "isEnabled"); | ||
} | ||
} |