summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/src/keyid.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-01-17 11:11:27 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-17 16:48:28 +0100
commit3f58832474a4b270e136544016a401ef773ac065 (patch)
treec617160250c3040ca964c1b72ab5957cd872b82f /openpgp-ffi/src/keyid.rs
parent38b4108cc1eac851ac17932c5c33623dd535bebb (diff)
openpgp-ffi: New crate.
- This creates a new crate, 'sequoia-openpgp-ffi', and moves a handful of functions from 'sequoia-ffi' to it. - The 'sequoia-ffi' crate is a superset of the 'sequoia-openpgp-ffi' crate. This is accomplished by some include! magic. - My first attempt involved having 'sequoia-ffi' depend on 'sequoia-openpgp-ffi', so that the former just re-exports the symbols. However, that turned out to be unreliable, and might be not what we want, because it could also duplicate parts of Rust's standard library. - Fixes #144.
Diffstat (limited to 'openpgp-ffi/src/keyid.rs')
-rw-r--r--openpgp-ffi/src/keyid.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/openpgp-ffi/src/keyid.rs b/openpgp-ffi/src/keyid.rs
new file mode 100644
index 00000000..1b5e0c20
--- /dev/null
+++ b/openpgp-ffi/src/keyid.rs
@@ -0,0 +1,99 @@
+//! Handles KeyIDs.
+//!
+//! Wraps [`sequoia-openpgp::KeyID`].
+//!
+//! [`sequoia-openpgp::KeyID`]: ../../../sequoia_openpgp/enum.KeyID.html
+
+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 <stdlib.h>
+/// #include <string.h>
+/// #include <sequoia/openpgp.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);
+/// free (mr_b_as_string);
+/// ```
+#[::ffi_catch_abort] #[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.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_keyid_from_hex(id: *const c_char) -> *mut KeyID {
+ let id = ffi_param_cstr!(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.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_keyid_free(keyid: Option<&mut KeyID>) {
+ ffi_free!(keyid)
+}
+
+/// Clones the KeyID.
+#[::ffi_catch_abort] #[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.
+#[::ffi_catch_abort] #[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.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_keyid_to_string(id: *const KeyID)
+ -> *mut c_char {
+ let id = ffi_param_ref!(id);
+ ffi_return_string!(id.to_string())
+}
+
+/// Converts the KeyID to a hexadecimal number.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_keyid_to_hex(id: *const KeyID)
+ -> *mut c_char {
+ let id = ffi_param_ref!(id);
+ ffi_return_string!(id.to_hex())
+}
+
+/// Compares KeyIDs.
+#[::ffi_catch_abort] #[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
+}