|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Matthew Nelson |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | +@file:Suppress("KotlinRedundantDiagnosticSuppress") |
| 17 | + |
| 18 | +package org.kotlincrypto.core.digest.internal |
| 19 | + |
| 20 | +import kotlin.jvm.JvmInline |
| 21 | +import kotlin.jvm.JvmSynthetic |
| 22 | + |
| 23 | +@JvmInline |
| 24 | +internal value class Buffer private constructor(internal val value: ByteArray) { |
| 25 | + |
| 26 | + internal fun toState( |
| 27 | + algorithm: String, |
| 28 | + digestLength: Int, |
| 29 | + bufOffs: Int, |
| 30 | + compressCount: Long, |
| 31 | + ): DigestState = State( |
| 32 | + algorithm, |
| 33 | + digestLength, |
| 34 | + bufOffs, |
| 35 | + compressCount, |
| 36 | + this, |
| 37 | + ) |
| 38 | + |
| 39 | + private class State( |
| 40 | + algorithm: String, |
| 41 | + digestLength: Int, |
| 42 | + bufOffs: Int, |
| 43 | + compressCount: Long, |
| 44 | + buf: Buffer, |
| 45 | + ): DigestState( |
| 46 | + algorithm, |
| 47 | + digestLength, |
| 48 | + bufOffs, |
| 49 | + compressCount, |
| 50 | + ) { |
| 51 | + val buf = Buffer(buf.value.copyOf()) |
| 52 | + } |
| 53 | + |
| 54 | + internal companion object { |
| 55 | + |
| 56 | + @JvmSynthetic |
| 57 | + internal fun DigestState.buf(): Buffer = Buffer((this as State).buf.value.copyOf()) |
| 58 | + |
| 59 | + @JvmSynthetic |
| 60 | + @Throws(IllegalArgumentException::class) |
| 61 | + internal fun initialize( |
| 62 | + algorithm: String, |
| 63 | + blockSize: Int, |
| 64 | + digestLength: Int, |
| 65 | + ): Buffer { |
| 66 | + require(algorithm.isNotBlank()) { "algorithm cannot be blank" } |
| 67 | + require(blockSize > 0) { "blockSize must be greater than 0" } |
| 68 | + require(blockSize % 8 == 0) { "blockSize must be a factor of 8" } |
| 69 | + require(digestLength >= 0) { "digestLength cannot be negative" } |
| 70 | + return Buffer(ByteArray(blockSize)) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +@Suppress("NOTHING_TO_INLINE") |
| 76 | +internal inline fun Buffer.update( |
| 77 | + input: Byte, |
| 78 | + bufOffsGetAndIncrement: () -> Int, |
| 79 | + bufOffsReset: () -> Unit, |
| 80 | + compress: (buf: ByteArray, offset: Int) -> Unit, |
| 81 | + compressCountIncrement: () -> Unit, |
| 82 | +) { |
| 83 | + val bufOffs = bufOffsGetAndIncrement() |
| 84 | + value[bufOffs] = input |
| 85 | + |
| 86 | + // value.size == blockSize |
| 87 | + if ((bufOffs + 1) != value.size) return |
| 88 | + compress(value, 0) |
| 89 | + compressCountIncrement() |
| 90 | + bufOffsReset() |
| 91 | +} |
| 92 | + |
| 93 | +@Suppress("NOTHING_TO_INLINE") |
| 94 | +internal inline fun Buffer.update( |
| 95 | + input: ByteArray, |
| 96 | + offset: Int, |
| 97 | + len: Int, |
| 98 | + bufOffsGet: () -> Int, |
| 99 | + bufOffsGetAndIncrement: () -> Int, |
| 100 | + bufOffsReset: () -> Unit, |
| 101 | + compress: (buf: ByteArray, offset: Int) -> Unit, |
| 102 | + compressCountIncrement: () -> Unit, |
| 103 | +) { |
| 104 | + var i = offset |
| 105 | + var remaining = len |
| 106 | + |
| 107 | + // Fill buffer if not already empty |
| 108 | + while (bufOffsGet() != 0 && remaining > 0) { |
| 109 | + update( |
| 110 | + input[i++], |
| 111 | + bufOffsGetAndIncrement, |
| 112 | + bufOffsReset, |
| 113 | + compress, |
| 114 | + compressCountIncrement, |
| 115 | + ) |
| 116 | + --remaining |
| 117 | + } |
| 118 | + |
| 119 | + // Chunk |
| 120 | + while (remaining >= value.size) { |
| 121 | + compress(input, i) |
| 122 | + i += value.size |
| 123 | + remaining -= value.size |
| 124 | + compressCountIncrement() |
| 125 | + } |
| 126 | + |
| 127 | + // Add remaining to buffer |
| 128 | + while (remaining-- > 0) { |
| 129 | + update( |
| 130 | + input[i++], |
| 131 | + bufOffsGetAndIncrement, |
| 132 | + bufOffsReset, |
| 133 | + compress, |
| 134 | + compressCountIncrement, |
| 135 | + ) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +@Suppress("NOTHING_TO_INLINE") |
| 140 | +internal inline fun Buffer.digest( |
| 141 | + bufOffs: Int, |
| 142 | + compressCount: Long, |
| 143 | + digest: (bitLength: Long, offs: Int, buf: ByteArray) -> ByteArray, |
| 144 | + resetBufOffs: () -> Unit, |
| 145 | + resetCompressCount: () -> Unit, |
| 146 | + resetDigest: () -> Unit, |
| 147 | +): ByteArray { |
| 148 | + val bitLength = ((compressCount * value.size) + bufOffs) * 8 |
| 149 | + val final = digest(bitLength, bufOffs, value) |
| 150 | + reset(resetBufOffs, resetCompressCount, resetDigest) |
| 151 | + return final |
| 152 | +} |
| 153 | + |
| 154 | +@Suppress("NOTHING_TO_INLINE") |
| 155 | +internal inline fun Buffer.reset( |
| 156 | + resetBufOffs: () -> Unit, |
| 157 | + resetCompressCount: () -> Unit, |
| 158 | + resetDigest: () -> Unit, |
| 159 | +) { |
| 160 | + value.fill(0) |
| 161 | + resetBufOffs() |
| 162 | + resetCompressCount() |
| 163 | + resetDigest() |
| 164 | +} |
0 commit comments