From c43678ddf39e518e7289f505772d9d3b4ffc1cb3 Mon Sep 17 00:00:00 2001 From: Ahmad Kemsan Date: Tue, 30 Jul 2024 09:22:25 +0530 Subject: [PATCH 1/2] feat: unlimited implementation of meter attribute getter --- .../HostLicenseMeterAttribute.java | 20 +++++++++++++++---- .../lexfloatclient/LexFloatClient.java | 16 ++++++++++----- .../lexfloatclient/LexFloatClientNative.java | 5 +++-- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java b/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java index 0f4a303..5107dc3 100644 --- a/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java +++ b/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java @@ -2,15 +2,27 @@ public class HostLicenseMeterAttribute { + /** + * The name of the meter attribute. + */ public String name; - public int allowedUses; + /** + * The allowed uses of the meter attribute. A value of -1 indicates unlimited allowed uses. + */ + public long allowedUses; - public int totalUses; + /** + * The total uses of the meter attribute. + */ + public long totalUses; - public int grossUses; + /** + * The gross uses of the meter attribute. + */ + public long grossUses; - public HostLicenseMeterAttribute(String name, int allowedUses, int totalUses, int grossUses) { + public HostLicenseMeterAttribute(String name, long allowedUses, long totalUses, long grossUses) { this.name = name; this.allowedUses = allowedUses; this.totalUses = totalUses; diff --git a/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java b/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java index db09553..d1dc5fd 100644 --- a/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java +++ b/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java @@ -6,6 +6,8 @@ import java.nio.CharBuffer; import java.io.UnsupportedEncodingException; import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.LongByReference; +import java.math.BigInteger; import java.util.ArrayList; import java.util.List; @@ -24,6 +26,10 @@ public class LexFloatClient { */ public static final int LF_FAIL = 1; + private static long toUnsignedLong(long value) { + return BigInteger.valueOf(value).and(BigInteger.valueOf(0xFFFFFFFFFFFFFFFFL)).longValue(); + } + /** * Sets the product id of your application * @@ -257,19 +263,19 @@ public static String GetHostLicenseMetadata(String key) throws LexFloatClientExc */ public static HostLicenseMeterAttribute GetHostLicenseMeterAttribute(String name) throws LexFloatClientException, UnsupportedEncodingException { int status; - IntByReference allowedUses = new IntByReference(0); - IntByReference totalUses = new IntByReference(0); - IntByReference grossUses = new IntByReference(0); + LongByReference allowedUses = new LongByReference(0); + LongByReference totalUses = new LongByReference(0); + LongByReference grossUses = new LongByReference(0); if (Platform.isWindows()) { status = LexFloatClientNative.GetHostLicenseMeterAttribute(new WString(name), allowedUses, totalUses, grossUses); if (LF_OK == status) { - return new HostLicenseMeterAttribute(name, allowedUses.getValue(), totalUses.getValue(), grossUses.getValue()); + return new HostLicenseMeterAttribute(name, allowedUses.getValue(), toUnsignedLong(totalUses.getValue()), toUnsignedLong(grossUses.getValue())); } } else { status = LexFloatClientNative.GetHostLicenseMeterAttribute(name, allowedUses, totalUses, grossUses); if (LF_OK == status) { - return new HostLicenseMeterAttribute(name, allowedUses.getValue(), totalUses.getValue(), grossUses.getValue()); + return new HostLicenseMeterAttribute(name, allowedUses.getValue(), toUnsignedLong(totalUses.getValue()), toUnsignedLong(grossUses.getValue())); } } throw new LexFloatClientException(status); diff --git a/src/main/java/com/cryptlex/lexfloatclient/LexFloatClientNative.java b/src/main/java/com/cryptlex/lexfloatclient/LexFloatClientNative.java index 90cd0e3..46a7e09 100644 --- a/src/main/java/com/cryptlex/lexfloatclient/LexFloatClientNative.java +++ b/src/main/java/com/cryptlex/lexfloatclient/LexFloatClientNative.java @@ -8,6 +8,7 @@ import java.nio.CharBuffer; import java.nio.ByteBuffer; import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.LongByReference; import com.sun.jna.Callback; import java.io.File; @@ -55,9 +56,9 @@ public interface CallbackType extends Callback { public static native int GetHostLicenseMetadata(WString key, CharBuffer value, int length); - public static native int GetHostLicenseMeterAttribute(String name, IntByReference allowedUses, IntByReference totalUses, IntByReference grossUses); + public static native int GetHostLicenseMeterAttribute(String name, LongByReference allowedUses, LongByReference totalUses, LongByReference grossUses); - public static native int GetHostLicenseMeterAttribute(WString name, IntByReference allowedUses, IntByReference totalUses, IntByReference grossUses); + public static native int GetHostLicenseMeterAttribute(WString name, LongByReference allowedUses, LongByReference totalUses, LongByReference grossUses); public static native int GetHostLicenseExpiryDate(IntByReference expiryDate); From 6c09fe2bbdecba531e497cd877a59b50db9016bd Mon Sep 17 00:00:00 2001 From: Ahmad Kemsan Date: Tue, 30 Jul 2024 10:46:37 +0530 Subject: [PATCH 2/2] fix: handle uint64_t values --- .../lexfloatclient/HostLicenseMeterAttribute.java | 7 ++++--- .../com/cryptlex/lexfloatclient/LexFloatClient.java | 10 ++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java b/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java index 5107dc3..177bc10 100644 --- a/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java +++ b/src/main/java/com/cryptlex/lexfloatclient/HostLicenseMeterAttribute.java @@ -1,4 +1,5 @@ package com.cryptlex.lexfloatclient; +import java.math.BigInteger; public class HostLicenseMeterAttribute { @@ -15,14 +16,14 @@ public class HostLicenseMeterAttribute { /** * The total uses of the meter attribute. */ - public long totalUses; + public BigInteger totalUses; /** * The gross uses of the meter attribute. */ - public long grossUses; + public BigInteger grossUses; - public HostLicenseMeterAttribute(String name, long allowedUses, long totalUses, long grossUses) { + public HostLicenseMeterAttribute(String name, long allowedUses, BigInteger totalUses, BigInteger grossUses) { this.name = name; this.allowedUses = allowedUses; this.totalUses = totalUses; diff --git a/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java b/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java index d1dc5fd..7e513cd 100644 --- a/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java +++ b/src/main/java/com/cryptlex/lexfloatclient/LexFloatClient.java @@ -26,8 +26,9 @@ public class LexFloatClient { */ public static final int LF_FAIL = 1; - private static long toUnsignedLong(long value) { - return BigInteger.valueOf(value).and(BigInteger.valueOf(0xFFFFFFFFFFFFFFFFL)).longValue(); + // Convert long to BigInteger to correctly handle unsigned 64-bit values + private static BigInteger toUnsignedBigInteger(long value) { + return BigInteger.valueOf(value).and(BigInteger.valueOf(0xFFFFFFFFFFFFFFFFL)); } /** @@ -264,18 +265,19 @@ public static String GetHostLicenseMetadata(String key) throws LexFloatClientExc public static HostLicenseMeterAttribute GetHostLicenseMeterAttribute(String name) throws LexFloatClientException, UnsupportedEncodingException { int status; LongByReference allowedUses = new LongByReference(0); + // These references can still hold the uint64_t values populated by the native function LongByReference totalUses = new LongByReference(0); LongByReference grossUses = new LongByReference(0); if (Platform.isWindows()) { status = LexFloatClientNative.GetHostLicenseMeterAttribute(new WString(name), allowedUses, totalUses, grossUses); if (LF_OK == status) { - return new HostLicenseMeterAttribute(name, allowedUses.getValue(), toUnsignedLong(totalUses.getValue()), toUnsignedLong(grossUses.getValue())); + return new HostLicenseMeterAttribute(name, allowedUses.getValue(), toUnsignedBigInteger(totalUses.getValue()), toUnsignedBigInteger(grossUses.getValue())); } } else { status = LexFloatClientNative.GetHostLicenseMeterAttribute(name, allowedUses, totalUses, grossUses); if (LF_OK == status) { - return new HostLicenseMeterAttribute(name, allowedUses.getValue(), toUnsignedLong(totalUses.getValue()), toUnsignedLong(grossUses.getValue())); + return new HostLicenseMeterAttribute(name, allowedUses.getValue(), toUnsignedBigInteger(totalUses.getValue()), toUnsignedBigInteger(grossUses.getValue())); } } throw new LexFloatClientException(status);