jniNativeCommonMain.dev.datlag.sekret.JNIUtils.kt Maven / Gradle / Ivy
package dev.datlag.sekret
import kotlinx.cinterop.*
import kotlin.experimental.xor
@OptIn(ExperimentalForeignApi::class)
expect fun CPointer.newString(chars: CPointer, length: Int): jString?
@OptIn(ExperimentalForeignApi::class)
internal fun String.toJString(env: CPointer): jString? = memScoped {
env.newString(wcstr.ptr, length)
}
@OptIn(ExperimentalForeignApi::class)
expect fun jString.getStringUTFChars(env: CPointer): CPointer?
@OptIn(ExperimentalForeignApi::class)
internal fun jString.getString(env: CPointer): String {
val chars = getStringUTFChars(env)
return chars?.toKStringFromUtf8() ?: error("Unable to create String from the given jString")
}
@OptIn(ExperimentalForeignApi::class)
public fun getOriginalValue(
secret: IntArray,
key: jString,
env: CPointer
): jString? {
val obfuscator = key.getString(env)
return getOriginalValue(secret, obfuscator).toJString(env)
}