summaryrefslogtreecommitdiffstats
path: root/ffi/src/openpgp/keyid.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-01-08 13:19:41 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-08 14:24:43 +0100
commitf570314648cf16fdfad3099ad6a215ab00f5543e (patch)
tree2bed7d0a593416df3723ea9b459be26e94cb8fb4 /ffi/src/openpgp/keyid.rs
parent32e06d22c906f66f6cb94c942da0ab06bce53f14 (diff)
ffi: Move KeyID glue to a new module.
Diffstat (limited to 'ffi/src/openpgp/keyid.rs')
-rw-r--r--ffi/src/openpgp/keyid.rs91
1 files changed, 91 insertions, 0 deletions
diff --git a/ffi/src/openpgp/keyid.rs b/ffi/src/openpgp/keyid.rs
new file mode 100644
index 00000000..ed4bb11e
--- /dev/null
+++ b/ffi/src/openpgp/keyid.rs
@@ -0,0 +1,91 @@
+//! 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.
+#[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: *mut KeyID) {
+ if keyid.is_null() { return }
+ unsafe {
+ drop(Box::from_raw(keyid));
+ }
+}
+
+/// Clones the KeyID.
+#[no_mangle]
+pub extern "system" fn sq_keyid_clone(id: Option<&KeyID>)
+ -> *mut KeyID {
+ let id = id.expect("KeyID is NULL");
+ box_raw!(id.clone())
+}
+
+/// Hashes the KeyID.
+#[no_mangle]
+pub extern "system" fn sq_keyid_hash(id: Option<&KeyID>)
+ -> uint64_t {
+ let id = id.expect("KeyID is NULL");
+ 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: Option<&KeyID>)
+ -> *mut c_char {
+ let id = id.expect("KeyID is NULL");
+ 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: Option<&KeyID>)
+ -> *mut c_char {
+ let id = id.expect("KeyID is NULL");
+ 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: Option<&KeyID>,
+ b: Option<&KeyID>)
+ -> bool {
+ let a = a.expect("KeyID 'a' is NULL");
+ let b = b.expect("KeyID 'b' is NULL");
+ a == b
+}