This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-crypto.git
The following commit(s) were added to refs/heads/master by this push: new 0d0de80 Better main function 0d0de80 is described below commit 0d0de80de213a1c5ccf3b877bc9ff0412b30b20f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Dec 18 08:16:34 2022 -0500 Better main function - No need for package private class to have public constants - Keep full OpenSSL version - Remove extra EOL in debug output - Output debug string sooner instead of crash in internal code --- .../org/apache/commons/crypto/jna/OpenSslJna.java | 6 ++-- .../commons/crypto/jna/OpenSslNativeJna.java | 35 +++++++++++++--------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java index 842b298..5cdb209 100644 --- a/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java +++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslJna.java @@ -22,10 +22,12 @@ import org.apache.commons.crypto.cipher.CryptoCipher; import org.apache.commons.crypto.random.CryptoRandom; /** - * Public class to give access to the package protected class objects + * Provides access to package protected class objects and a {@link #main(String[])} method that prints version information. */ public final class OpenSslJna { + private static final String NAME = "Apache Commons Crypto"; + /** * Logs debug messages. * @@ -79,7 +81,7 @@ public final class OpenSslJna { } public static void main(final String[] args) throws Throwable { - info("isEnabled(): %s", isEnabled()); + info(NAME + " OpenSslJna: enabled = %s, version = 0x%08X", isEnabled(), OpenSslNativeJna.VERSION); final Throwable initialisationError = initialisationError(); if (initialisationError != null) { info("initialisationError(): %s", initialisationError); diff --git a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java index fdc013d..360079c 100644 --- a/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java +++ b/src/main/java/org/apache/commons/crypto/jna/OpenSslNativeJna.java @@ -38,39 +38,46 @@ final class OpenSslNativeJna { static final Throwable INIT_ERROR; - public static final long VERSION; - public static final long VERSION_1_0_X = 0x10000000; - public static final long VERSION_1_1_X = 0x10100000; - public static final long VERSION_2_0_X = 0x20000000; + /** Full version from JNA call. */ + static final long VERSION; + + /** Major Minor version from JNA call, without the maintenance level. */ + static final long VERSION_X_Y; + + static final long VERSION_1_0_X = 0x10000000; + static final long VERSION_1_1_X = 0x10100000; + static final long VERSION_2_0_X = 0x20000000; + static final long VERSION_3_0_X = 0x30000000; private static final OpenSslInterfaceNativeJna JnaImplementation; static { final String libraryName = System.getProperty(Crypto.CONF_PREFIX + OpenSslNativeJna.class.getSimpleName(), "crypto"); - OpenSslJna.debug("NativeLibrary.getInstance('%s')%n", libraryName); + OpenSslJna.debug("NativeLibrary.getInstance('%s')", libraryName); final NativeLibrary crypto = NativeLibrary.getInstance(libraryName); - Function version = null; + Function versionFunction = null; try { - version = crypto.getFunction("SSLeay"); + versionFunction = crypto.getFunction("SSLeay"); } catch (final UnsatisfiedLinkError e) { - version = crypto.getFunction("OpenSSL_version_num"); + versionFunction = crypto.getFunction("OpenSSL_version_num"); } // Must find one of the above two functions; else give up - VERSION = version.invokeLong(new Object[]{}) & 0xffff0000; // keep only major.minor - if (VERSION == VERSION_1_0_X) { + VERSION = versionFunction.invokeLong(new Object[]{}); + VERSION_X_Y = VERSION & 0xffff0000; // keep only major.minor + OpenSslJna.debug(String.format("Detected version 0x%x", VERSION)); + + if (VERSION_X_Y == VERSION_1_0_X) { JnaImplementation = new OpenSsl10XNativeJna(); - } else if (VERSION == VERSION_1_1_X) { + } else if (VERSION_X_Y == VERSION_1_1_X) { JnaImplementation = new OpenSsl11XNativeJna(); - } else if (VERSION == VERSION_2_0_X) { + } else if (VERSION_X_Y == VERSION_2_0_X) { JnaImplementation = new OpenSsl20XNativeJna(); } else { // TODO: Throw error? JnaImplementation = new OpenSsl10XNativeJna(); } - OpenSslJna.debug(String.format("Detected version 0x%x", VERSION)); - INIT_OK = JnaImplementation._INIT_OK(); INIT_ERROR = INIT_OK ? null : JnaImplementation._INIT_ERROR();