Scoop API

scoop:scoop.crypto:0.1.0

src/native/results.scoop

Selected production source · src/native/results.scoop

1package scoop.crypto.native
3import scoop.crypto.CryptoException
5/**
6 * The native bridge's value/error pair. Interpret and consume it with the matching operation
7 * helper.
8 */
9@CLayout
10public struct CryptoResult(val value: ULong, val error: ULong)
12/**
13 * Converts a native result to its value or raises CryptoException after consuming the backend
14 * error.
15 */
16@Unsafe
17public 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.value
27/**
28 * Checks a native result and consumes its owned byte handle into a managed byte array.
29 */
30@Unsafe
31public fun cryptoBytes(result: CryptoResult, operation: String): Array<Byte> {
32 return consumeCryptoBytes(cryptoValue(result, operation))
35/**
36 * Copies an owned native byte result into a managed array and releases the native result handle.
37 */
38@Unsafe
39public 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) }
47@Extern(name = "scoop_crypto_error_code") private fun errorCode(handle: ULong): ULong
48@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): Long
51@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)