blob: 6df8ca811ae3eebb938841b47090e32b97c785d4 (
plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
//! Handles KeyIDs.
//!
//! Wraps [`sequoia-openpgp::KeyID`].
//!
//! [`sequoia-openpgp::KeyID`]: ../../../sequoia_openpgp/enum.KeyID.html
use std::ffi::{CString, CStr};
use std::hash::{Hash, Hasher};
use std::ptr;
use std::slice;
use libc::{uint8_t, uint64_t, c_char};
extern crate sequoia_openpgp;
use self::sequoia_openpgp::KeyID;
use build_hasher;
/// Reads a binary key ID.
///
/// # Example
///
/// ```c
/// #include <assert.h>
/// #include <string.h>
/// #include <sequoia.h>
///
/// sq_keyid_t mr_b = sq_keyid_from_bytes ("\xbb\xbb\xbb\xbb\xbb\xbb\xbb\xbb");
///
/// char *mr_b_as_string = sq_keyid_to_string (mr_b);
/// assert (strcmp (mr_b_as_string, "BBBB BBBB BBBB BBBB") == 0);
///
/// sq_keyid_free (mr_b);
/// sq_string_free (mr_b_as_string);
/// ```
#[no_mangle]
pub extern "system" fn sq_keyid_from_bytes(id: *const uint8_t) -> *mut KeyID {
assert!(!id.is_null());
let id = unsafe { slice::from_raw_parts(id, 8) };
Box::into_raw(Box::new(KeyID::from_bytes(id)))
}
/// Reads a hex-encoded Key ID.
#[no_mangle]
pub extern "system" fn sq_keyid_from_hex(id: *const c_char) -> *mut KeyID {
assert!(!id.is_null());
let id = unsafe { CStr::from_ptr(id).to_string_lossy() };
KeyID::from_hex(&id)
.map(|id| Box::into_raw(Box::new(id)))
.unwrap_or(ptr::null_mut())
}
/// Frees an `KeyID` object.
#[no_mangle]
pub extern "system" fn sq_keyid_free(keyid: Option<&mut KeyID>) {
ffi_free!(keyid)
}
/// Clones the KeyID.
#[no_mangle]
pub extern "system" fn sq_keyid_clone(id: *const KeyID)
-> *mut KeyID {
let id = ffi_param_ref!(id);
box_raw!(id.clone())
}
/// Hashes the KeyID.
#[no_mangle]
pub extern "system" fn sq_keyid_hash(id: *const KeyID)
-> uint64_t {
let id = ffi_param_ref!(id);
let mut hasher = build_hasher();
id.hash(&mut hasher);
hasher.finish()
}
/// Converts the KeyID to its standard representation.
#[no_mangle]
pub extern "system" fn sq_keyid_to_string(id: *const KeyID)
-> *mut c_char {
let id = ffi_param_ref!(id);
CString::new(id.to_string())
.unwrap() // Errors only on internal nul bytes.
.into_raw()
}
/// Converts the KeyID to a hexadecimal number.
#[no_mangle]
pub extern "system" fn sq_keyid_to_hex(id: *const KeyID)
-> *mut c_char {
let id = ffi_param_ref!(id);
CString::new(id.to_hex())
.unwrap() // Errors only on internal nul bytes.
.into_raw()
}
/// Compares KeyIDs.
#[no_mangle]
pub extern "system" fn sq_keyid_equal(a: *const KeyID,
b: *const KeyID)
-> bool {
let a = ffi_param_ref!(a);
let b = ffi_param_ref!(b);
a == b
}
|