Skip to content

Commit

Permalink
Rename digestProtected parameters to buf && bufPos
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Jan 4, 2025
1 parent f671d61 commit ac0e242
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class DigestBenchmark {
override fun resetProtected() {}
override fun copy(): TestDigest = TestDigest(this)
override fun compressProtected(input: ByteArray, offset: Int) {}
override fun digestProtected(buffer: ByteArray, offset: Int): ByteArray = ByteArray(0)
override fun digestProtected(buf: ByteArray, bufPos: Int): ByteArray = ByteArray(0)
}

private val digest = TestDigest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public expect abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
protected abstract fun compressProtected(input: ByteArray, offset: Int)

/**
* Called to complete the computation, providing any input that may be
* buffered awaiting processing.
* Called to complete the computation, providing any input that may be buffered
* and awaiting processing.
*
* @param [buffer] Unprocessed input
* @param [offset] The index at which the next input would be placed in the [buffer]
* @param [buf] Unprocessed input
* @param [bufPos] The index at which the **next** input would be placed into [buf]
* */
protected abstract fun digestProtected(buffer: ByteArray, offset: Int): ByteArray
protected abstract fun digestProtected(buf: ByteArray, bufPos: Int): ByteArray

/**
* Optional override for implementations to intercept cleansed input before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,41 @@ internal value class Buffer private constructor(internal val value: ByteArray) {
@Suppress("NOTHING_TO_INLINE")
internal inline fun Buffer.commonUpdate(
input: Byte,
bufOffsPlusPlus: Int,
bufOffsSet: (zero: Int) -> Unit,
bufPosPlusPlus: Int,
bufPosSet: (zero: Int) -> Unit,
compressProtected: (buf: ByteArray, offset: Int) -> Unit,
) {
val buf = value
buf[bufOffsPlusPlus] = input
buf[bufPosPlusPlus] = input

// buf.size == blockSize
if ((bufOffsPlusPlus + 1) != buf.size) return
if ((bufPosPlusPlus + 1) != buf.size) return
compressProtected(buf, 0)
bufOffsSet(0)
bufPosSet(0)
}

@Suppress("NOTHING_TO_INLINE")
internal inline fun Buffer.commonUpdate(
input: ByteArray,
offset: Int,
len: Int,
bufOffs: Int,
bufOffsSet: (value: Int) -> Unit,
bufPos: Int,
bufPosSet: (value: Int) -> Unit,
compressProtected: (buf: ByteArray, offset: Int) -> Unit,
) {
val buf = value
val blockSize = buf.size
var offsInput = offset
val limit = offsInput + len
var offsBuf = bufOffs
var offsBuf = bufPos

if (offsBuf > 0) {
// Need to use buffered data (if possible)

if (offsBuf + len < blockSize) {
// Not enough for a compression. Add it to the buffer.
input.copyInto(buf, offsBuf, offsInput, limit)
bufOffsSet(offsBuf + len)
bufPosSet(offsBuf + len)
return
}

Expand Down Expand Up @@ -108,5 +108,5 @@ internal inline fun Buffer.commonUpdate(
}

// Update globals
bufOffsSet(offsBuf)
bufPosSet(offsBuf)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class TestDigest: Digest {
compressions++
}

override fun digestProtected(buffer: ByteArray, offset: Int): ByteArray {
return finalize.invoke(buffer, offset)
override fun digestProtected(buf: ByteArray, bufPos: Int): ByteArray {
return finalize.invoke(buf, bufPos)
}

override fun resetProtected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab

private val digestLength: Int
private val buf: Buffer
private var bufOffs: Int
private var bufPos: Int

/**
* Creates a new [Digest] for the specified parameters.
Expand All @@ -59,7 +59,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
protected actual constructor(algorithm: String, blockSize: Int, digestLength: Int): super(algorithm) {
this.buf = Buffer.initialize(algorithm, blockSize, digestLength)
this.digestLength = digestLength
this.bufOffs = 0
this.bufPos = 0
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
protected actual constructor(other: Digest): super(other.algorithm) {
this.digestLength = other.digestLength
this.buf = other.buf.copy()
this.bufOffs = other.bufOffs
this.bufPos = other.bufPos
}

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
* the resultant array of bytes. The [Digest] is [reset] afterward.
* */
public actual final override fun digest(): ByteArray {
val final = digestProtected(buf.value, bufOffs)
val final = digestProtected(buf.value, bufPos)
reset()
return final
}
Expand All @@ -143,7 +143,7 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
// See Resettable interface documentation
public actual final override fun reset() {
buf.value.fill(0)
bufOffs = 0
bufPos = 0
resetProtected()
}

Expand All @@ -155,13 +155,13 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
protected actual abstract fun compressProtected(input: ByteArray, offset: Int)

/**
* Called to complete the computation, providing any input that may be
* buffered awaiting processing.
* Called to complete the computation, providing any input that may be buffered
* and awaiting processing.
*
* @param [buffer] Unprocessed input
* @param [offset] The index at which the next input would be placed in the [buffer]
* @param [buf] Unprocessed input
* @param [bufPos] The index at which the **next** input would be placed into [buf]
* */
protected actual abstract fun digestProtected(buffer: ByteArray, offset: Int): ByteArray
protected actual abstract fun digestProtected(buf: ByteArray, bufPos: Int): ByteArray

/**
* Optional override for implementations to intercept cleansed input before
Expand All @@ -170,8 +170,8 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
protected actual open fun updateProtected(input: Byte) {
buf.commonUpdate(
input = input,
bufOffsPlusPlus = bufOffs++,
bufOffsSet = { bufOffs = it },
bufPosPlusPlus = bufPos++,
bufPosSet = { bufPos = it },
compressProtected = ::compressProtected,
)
}
Expand All @@ -186,8 +186,8 @@ public actual abstract class Digest: MessageDigest, Algorithm, Cloneable, Copyab
input = input,
offset = offset,
len = len,
bufOffs = bufOffs,
bufOffsSet = { bufOffs = it },
bufPos = bufPos,
bufPosSet = { bufPos = it },
compressProtected = ::compressProtected,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class JvmDigestUnitTest {
delegate.update(input, offset, blockSize())
}

override fun digestProtected(buffer: ByteArray, offset: Int): ByteArray {
delegate.update(buffer, 0, offset)
override fun digestProtected(buf: ByteArray, bufPos: Int): ByteArray {
delegate.update(buf, 0, bufPos)
return delegate.digest()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
private val algorithm: String
private val digestLength: Int
private val buf: Buffer
private var bufOffs: Int
private var bufPos: Int

/**
* Creates a new [Digest] for the specified parameters.
Expand All @@ -55,7 +55,7 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
this.buf = Buffer.initialize(algorithm, blockSize, digestLength)
this.algorithm = algorithm
this.digestLength = digestLength
this.bufOffs = 0
this.bufPos = 0
}

/**
Expand Down Expand Up @@ -84,7 +84,7 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
this.algorithm = other.algorithm
this.digestLength = other.digestLength
this.buf = other.buf.copy()
this.bufOffs = other.bufOffs
this.bufPos = other.bufPos
}

/**
Expand Down Expand Up @@ -122,7 +122,7 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
* the resultant array of bytes. The [Digest] is [reset] afterward.
* */
public actual fun digest(): ByteArray {
val final = digestProtected(buf.value, bufOffs)
val final = digestProtected(buf.value, bufPos)
reset()
return final
}
Expand All @@ -140,7 +140,7 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
// See Resettable interface documentation
public actual final override fun reset() {
buf.value.fill(0)
bufOffs = 0
bufPos = 0
resetProtected()
}

Expand All @@ -152,13 +152,13 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
protected actual abstract fun compressProtected(input: ByteArray, offset: Int)

/**
* Called to complete the computation, providing any input that may be
* buffered awaiting processing.
* Called to complete the computation, providing any input that may be buffered
* and awaiting processing.
*
* @param [buffer] Unprocessed input
* @param [offset] The index at which the next input would be placed in the [buffer]
* @param [buf] Unprocessed input
* @param [bufPos] The index at which the **next** input would be placed into [buf]
* */
protected actual abstract fun digestProtected(buffer: ByteArray, offset: Int): ByteArray
protected actual abstract fun digestProtected(buf: ByteArray, bufPos: Int): ByteArray

/**
* Optional override for implementations to intercept cleansed input before
Expand All @@ -167,8 +167,8 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
protected actual open fun updateProtected(input: Byte) {
buf.commonUpdate(
input = input,
bufOffsPlusPlus = bufOffs++,
bufOffsSet = { bufOffs = it },
bufPosPlusPlus = bufPos++,
bufPosSet = { bufPos = it },
compressProtected = ::compressProtected,
)
}
Expand All @@ -183,8 +183,8 @@ public actual abstract class Digest: Algorithm, Copyable<Digest>, Resettable, Up
input = input,
offset = offset,
len = len,
bufOffs = bufOffs,
bufOffsSet = { bufOffs = it },
bufPos = bufPos,
bufPosSet = { bufPos = it },
compressProtected = ::compressProtected,
)
}
Expand Down

0 comments on commit ac0e242

Please sign in to comment.