From 85f982016a10c5f65adc5b07805daa5fdb85d85d Mon Sep 17 00:00:00 2001 From: Matthew Nelson Date: Wed, 1 Jan 2025 13:42:46 -0500 Subject: [PATCH] Add compressions function for retrieving the counter --- library/digest/api/digest.api | 1 + library/digest/api/digest.klib.api | 1 + .../kotlin/org/kotlincrypto/core/digest/Digest.kt | 7 +++++++ .../org/kotlincrypto/core/digest/internal/-Buffer.kt | 8 +++----- .../kotlin/org/kotlincrypto/core/digest/Digest.kt | 9 ++++++++- .../kotlin/org/kotlincrypto/core/digest/Digest.kt | 11 +++++++++-- 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/library/digest/api/digest.api b/library/digest/api/digest.api index 5dc49e5..efb9a21 100644 --- a/library/digest/api/digest.api +++ b/library/digest/api/digest.api @@ -5,6 +5,7 @@ public abstract class org/kotlincrypto/core/digest/Digest : java/security/Messag public final fun blockSize ()I public final fun clone ()Ljava/lang/Object; protected abstract fun compress ([BI)V + protected final fun compressions ()J public synthetic fun copy ()Ljava/lang/Object; public final fun copy ()Lorg/kotlincrypto/core/digest/Digest; protected abstract fun copy (Lorg/kotlincrypto/core/digest/internal/DigestState;)Lorg/kotlincrypto/core/digest/Digest; diff --git a/library/digest/api/digest.klib.api b/library/digest/api/digest.klib.api index c0bc8c2..c0f9ade 100644 --- a/library/digest/api/digest.klib.api +++ b/library/digest/api/digest.klib.api @@ -16,6 +16,7 @@ abstract class org.kotlincrypto.core.digest/Digest : org.kotlincrypto.core/Algor abstract fun resetDigest() // org.kotlincrypto.core.digest/Digest.resetDigest|resetDigest(){}[0] final fun algorithm(): kotlin/String // org.kotlincrypto.core.digest/Digest.algorithm|algorithm(){}[0] final fun blockSize(): kotlin/Int // org.kotlincrypto.core.digest/Digest.blockSize|blockSize(){}[0] + final fun compressions(): kotlin/Long // org.kotlincrypto.core.digest/Digest.compressions|compressions(){}[0] final fun copy(): org.kotlincrypto.core.digest/Digest // org.kotlincrypto.core.digest/Digest.copy|copy(){}[0] final fun digest(): kotlin/ByteArray // org.kotlincrypto.core.digest/Digest.digest|digest(){}[0] final fun digest(kotlin/ByteArray): kotlin/ByteArray // org.kotlincrypto.core.digest/Digest.digest|digest(kotlin.ByteArray){}[0] diff --git a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/Digest.kt b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/Digest.kt index 5b94b5d..2648b3b 100644 --- a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/Digest.kt +++ b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/Digest.kt @@ -115,6 +115,13 @@ public expect abstract class Digest: Algorithm, Copyable, Resettable, Up // See Copyable interface documentation public final override fun copy(): Digest + /** + * The number of compressions this [Digest] has completed. Backing + * variable is updated **after** each [compress] invocation, and + * subsequently set to `0` upon [reset] invocation. + * */ + protected fun compressions(): Long + /** * Called by the public [copy] function which produces the * [DigestState] needed to create a wholly new instance. diff --git a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt index 4a2ad51..eec801e 100644 --- a/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt +++ b/library/digest/src/commonMain/kotlin/org/kotlincrypto/core/digest/internal/-Buffer.kt @@ -94,14 +94,13 @@ internal inline fun Buffer.commonUpdate( bufOffs: Int, bufOffsSet: (value: Int) -> Unit, compress: (buf: ByteArray, offset: Int) -> Unit, - compressCountAdd: (value: Int) -> Unit, + compressCountIncrement: () -> Unit, ) { val buf = value val blockSize = buf.size var offsInput = offset val limit = offsInput + len var offsBuf = bufOffs - var compressions = 0 if (offsBuf > 0) { // Need to use buffered data (if possible) @@ -119,7 +118,7 @@ internal inline fun Buffer.commonUpdate( compress(buf, 0) offsBuf = 0 offsInput += needed - compressions++ + compressCountIncrement() } // Chunk blocks (if possible) @@ -134,13 +133,12 @@ internal inline fun Buffer.commonUpdate( } compress(input, offsInput) - compressions++ offsInput = offsNext + compressCountIncrement() } // Update globals bufOffsSet(offsBuf) - compressCountAdd(compressions) } @Suppress("NOTHING_TO_INLINE") diff --git a/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt b/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt index 2087462..97a99e2 100644 --- a/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt +++ b/library/digest/src/jvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt @@ -162,6 +162,13 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab compressCount = compressCount, ).let { copy(it) } + /** + * The number of compressions this [Digest] has completed. Backing variable + * is updated **after** completion of each [compress] invocation, and then + * subsequently set to `0` upon [reset] invocation. + * */ + protected actual fun compressions(): Long = compressCount + /** * Called by the public [copy] function which produces the * [DigestState] needed to create a wholly new instance. @@ -216,7 +223,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab bufOffs = bufOffs, bufOffsSet = { bufOffs = it }, compress = ::compress, - compressCountAdd = { compressCount += it }, + compressCountIncrement = { compressCount++ }, ) } diff --git a/library/digest/src/nonJvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt b/library/digest/src/nonJvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt index 8a40b46..8e9865c 100644 --- a/library/digest/src/nonJvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt +++ b/library/digest/src/nonJvmMain/kotlin/org/kotlincrypto/core/digest/Digest.kt @@ -149,7 +149,7 @@ public actual abstract class Digest: Algorithm, Copyable, Resettable, Up public actual final override fun reset() { buf.value.fill(0) bufOffs = 0 - compressCount = 0 + compressCount = 0L resetDigest() } @@ -161,6 +161,13 @@ public actual abstract class Digest: Algorithm, Copyable, Resettable, Up compressCount = compressCount, ).let { copy(it) } + /** + * The number of compressions this [Digest] has completed. Backing + * variable is updated **after** each [compress] invocation, and + * subsequently set to `0` upon [reset] invocation. + * */ + protected actual fun compressions(): Long = compressCount + /** * Called by the public [copy] function which produces the * [DigestState] needed to create a wholly new instance. @@ -215,7 +222,7 @@ public actual abstract class Digest: Algorithm, Copyable, Resettable, Up bufOffs = bufOffs, bufOffsSet = { bufOffs = it }, compress = ::compress, - compressCountAdd = { compressCount += it }, + compressCountIncrement = { compressCount++ }, ) }