scoop:scoop.crypto:0.1.0
src/native/results.scoop
Selected production source · src/native/results.scoop
1
package scoop.crypto.native3
import scoop.crypto.CryptoException5
/**6
* The native bridge's value/error pair. Interpret and consume it with the matching operation7
* helper.8
*/9
@CLayout10
public struct CryptoResult(val value: ULong, val error: ULong)12
/**13
* Converts a native result to its value or raises CryptoException after consuming the backend14
* error.15
*/16
@Unsafe17
public fun cryptoValue(result: CryptoResult, operation: String): ULong {18
if (result.error != 0UL) {19
val code = errorCode(result.error)20
val detail = try { String.fromCString(errorText(result.error)) }21
finally { errorFree(result.error) }22
throw CryptoException(operation, code, detail)23
}24
return result.value25
}27
/**28
* Checks a native result and consumes its owned byte handle into a managed byte array.29
*/30
@Unsafe31
public fun cryptoBytes(result: CryptoResult, operation: String): Array<Byte> {32
return consumeCryptoBytes(cryptoValue(result, operation))33
}35
/**36
* Copies an owned native byte result into a managed array and releases the native result handle.37
*/38
@Unsafe39
public fun consumeCryptoBytes(handle: ULong): Array<Byte> {40
try {41
val count = bytesSize(handle)42
val data = bytesData(handle)43
return Array<Byte>(count) { index -> @Unsafe { return@Array data.load(index) } }44
} finally { bytesFree(handle) }45
}47
@Extern(name = "scoop_crypto_error_code") private fun errorCode(handle: ULong): ULong48
@Extern(name = "scoop_crypto_error_text") private fun errorText(handle: ULong): Ptr<Byte>49
@Extern(name = "scoop_crypto_error_free") private fun errorFree(handle: ULong)50
@Extern(name = "scoop_crypto_bytes_size") internal fun bytesSize(handle: ULong): Long51
@Extern(name = "scoop_crypto_bytes_data") private fun bytesData(handle: ULong): Ptr<Byte>52
@Extern(name = "scoop_crypto_bytes_free") internal fun bytesFree(handle: ULong)