Skip to content

Commit

Permalink
test if native lib is loading properly
Browse files Browse the repository at this point in the history
  • Loading branch information
sken committed Mar 24, 2020
1 parent 47effd1 commit 5f4e17d
Showing 27 changed files with 159 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -3,3 +3,4 @@
*.o
*.so
*.class
/target/
35 changes: 35 additions & 0 deletions README.md
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 added natives/windows_64/blake3.dll
Binary file not shown.
16 changes: 13 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -3,10 +3,20 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.scash</groupId>
<artifactId>BLAKE3jni</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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.
25 changes: 25 additions & 0 deletions src/main/c/org_scash_NativeBLAKE3.c
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;
}
29 changes: 29 additions & 0 deletions src/main/c/org_scash_NativeBLAKE3.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/main/java/org/scash/NativeBLAKE3.java
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);
}
17 changes: 17 additions & 0 deletions src/main/java/org/scash/NativeBLAKE3Util.java
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);
}
}
}
13 changes: 13 additions & 0 deletions src/test/java/org/scash/NativeBLAKE3Test.java
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");
}
}

0 comments on commit 5f4e17d

Please sign in to comment.