Skip to content

Commit

Permalink
fix: url can be null here
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed May 19, 2024
1 parent 6855d22 commit 5a43453
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/main/java/org/openquantumsafe/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,35 @@ public static void loadNativeLibrary() {
System.loadLibrary("oqs-jni");
// Otherwise load the library from the liboqs-java.jar
} catch (UnsatisfiedLinkError e) {
String libName = "llliboqs-jni.so";
if (Common.isLinux()) {
libName = "liboqs-jni.so";
} else if (Common.isMac()) {
libName = "liboqs-jni.jnilib";
} else if (Common.isWindows()) {
libName = "oqs-jni.dll";
}
URL url = KEMs.class.getResource("/" + libName);
File tmpDir;
try {
tmpDir = Files.createTempDirectory("oqs-native-lib").toFile();
tmpDir.deleteOnExit();
File nativeLibTmpFile = new File(tmpDir, libName);
nativeLibTmpFile.deleteOnExit();
InputStream in = url.openStream();
Files.copy(in, nativeLibTmpFile.toPath());
System.load(nativeLibTmpFile.getAbsolutePath());
} catch (IOException ioException) {
ioException.printStackTrace();
}
System.out.println("Native library not found in java.library.path. Loading from JAR...");
loadNativeLibraryFromResources();
}
}

private static void loadNativeLibraryFromResources() {
String libName = "llliboqs-jni.so";
if (Common.isLinux()) {
libName = "liboqs-jni.so";
} else if (Common.isMac()) {
libName = "liboqs-jni.jnilib";
} else if (Common.isWindows()) {
libName = "oqs-jni.dll";
}
URL url = KEMs.class.getResource("/" + libName);
if(url == null) {
throw new RuntimeException("Native library not found in JAR: " + libName);
}
File tmpDir;
try {
tmpDir = Files.createTempDirectory("oqs-native-lib").toFile();
tmpDir.deleteOnExit();
File nativeLibTmpFile = new File(tmpDir, libName);
nativeLibTmpFile.deleteOnExit();
InputStream in = url.openStream();
Files.copy(in, nativeLibTmpFile.toPath());
System.load(nativeLibTmpFile.getAbsolutePath());
} catch (IOException ioException) {
ioException.printStackTrace();
}
}

Expand Down

0 comments on commit 5a43453

Please sign in to comment.