Repository navigation
Expand file tree
/
Copy pathhelpers.rs
More file actions
68 lines (62 loc) · 2.07 KB
/
Copy pathhelpers.rs
File metadata and controls
68 lines (62 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::ffi::{c_char, c_uchar, c_void, CString};
use cas_lib::error::CasError;
/// Result of a verification (`verify`-style) FFI call.
///
/// cas-lib's verify operations now return `CasResult<bool>`, so a plain `bool`
/// can no longer distinguish "signature did not match" (`is_valid == false`,
/// `error_code == 0`) from "the inputs were malformed" (`is_valid == false`,
/// `error_code != 0`). This struct carries both across the boundary.
#[repr(C)]
pub struct CasVerifyResult {
pub is_valid: bool,
pub error_code: i32,
}
/// Result of an FFI call that hands back a C string (e.g. a password hash).
///
/// `value` is null when `error_code` is non-zero. The caller still frees a
/// non-null `value` with [`free_cstring`].
#[repr(C)]
pub struct CasStringResult {
pub value: *mut c_char,
pub error_code: i32,
}
/// Maps a [`CasError`] to the stable numeric code surfaced through the FFI in
/// the `error_code` field of every result struct. `0` always means success.
///
/// These values are part of the ABI contract with the consumer SDKs, so they
/// must stay stable even if cas-lib reorders the `CasError` enum — do not switch
/// this to an `as i32` cast on the enum.
pub fn cas_error_code(error: &CasError) -> i32 {
match error {
CasError::InvalidKey => 1,
CasError::InvalidNonce => 2,
CasError::InvalidSignature => 3,
CasError::InvalidInput => 4,
CasError::InvalidPemKey => 5,
CasError::InvalidParameters => 6,
CasError::EncryptionFailed => 7,
CasError::DecryptionFailed => 8,
CasError::SigningFailed => 9,
CasError::KeyGenerationFailed => 10,
CasError::PasswordHashingFailed => 11,
CasError::CompressionFailed => 12,
}
}
#[no_mangle]
pub extern "C" fn free_cstring(s: *mut c_char) {
unsafe {
if s.is_null() {
return;
}
drop(CString::from_raw(s));
};
}
#[no_mangle]
pub extern "C" fn free_bytes(ptr: *mut c_uchar) {
unsafe {
if ptr.is_null() {
return;
}
libc::free(ptr as *mut c_void);
}
}