From 1d8f8a640dc7948a64d27e88befd915026925a04 Mon Sep 17 00:00:00 2001 From: Lilianne-Blaze <39486911+Lilianne-Blaze@users.noreply.github.com> Date: Mon, 3 Feb 2025 17:40:15 +0100 Subject: [PATCH] Added simple lib detection --- .../muquit/libsodiumjna/SodiumLibrary.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java index a5adf31..43486d8 100755 --- a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java +++ b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java @@ -9,7 +9,10 @@ import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.NativeLong; +import com.sun.jna.Platform; import com.sun.jna.Pointer; +import java.nio.file.Files; +import java.nio.file.Paths; /** * SodiumLibrary is a Java binding to libsodium crypto C APIs @@ -114,6 +117,14 @@ public static Sodium sodium() { if (SodiumLibrary.libPath == null) { + logger.debug("libpath not set, trying to find lib"); + tryFindLib(); + if( SodiumLibrary.libPath == null) + { + logger.info("libpath not set, throw exception"); + throw new RuntimeException("Please set the absolute path of the libsodium libary by calling SodiumLibrary.setLibraryPath(path)"); + } + logger.info("libpath not set, throw exception"); throw new RuntimeException("Please set the absolute path of the libsodium libary by calling SodiumLibrary.setLibraryPath(path)"); } @@ -132,6 +143,25 @@ public static Sodium sodium() return sodium; } + private static void tryFindLib() { + if (Platform.isWindows()) { + // use env var, as it's possible we're running in Windows PE with X:\Windows + String s1 = System.getenv("SystemRoot") + "\\System32\\libsodium.dll"; + if (Files.isReadable(Paths.get(s1))) { + SodiumLibrary.libPath = s1; + logger.debug("found {}",s1); + return; + } + } + + String s2 = "./libsodium.dll"; + if (Files.isReadable(Paths.get(s2))) { + SodiumLibrary.libPath = s2; + logger.debug("found {}",s2); + return; + } + } + private static final class SingletonHelper { public static final Sodium instance = (Sodium) Native.loadLibrary(libPath,Sodium.class);