Skip to content

Commit

Permalink
Add compressions function for retrieving the counter
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Jan 1, 2025
1 parent a3ede6c commit 85f9820
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions library/digest/api/digest.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions library/digest/api/digest.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public expect abstract class Digest: Algorithm, Copyable<Digest>, 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -119,7 +118,7 @@ internal inline fun Buffer.commonUpdate(
compress(buf, 0)
offsBuf = 0
offsInput += needed
compressions++
compressCountIncrement()
}

// Chunk blocks (if possible)
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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++ },
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
public actual final override fun reset() {
buf.value.fill(0)
bufOffs = 0
compressCount = 0
compressCount = 0L
resetDigest()
}

Expand All @@ -161,6 +161,13 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, 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.
Expand Down Expand Up @@ -215,7 +222,7 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
bufOffs = bufOffs,
bufOffsSet = { bufOffs = it },
compress = ::compress,
compressCountAdd = { compressCount += it },
compressCountIncrement = { compressCount++ },
)
}

Expand Down

0 comments on commit 85f9820

Please sign in to comment.