From 9a5725fe55eb5e49cee09712364ac7b7dcefde51 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Thu, 23 Nov 2017 14:34:45 +0100 Subject: Add a foreign function interface. - For now, we keep the ffi in this crate, later on we may want to move it to sequoia-ffi. - Example code how to use the library from C is added as well. --- src/ffi.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/ffi.rs (limited to 'src/ffi.rs') diff --git a/src/ffi.rs b/src/ffi.rs new file mode 100644 index 00000000..3a7b1c66 --- /dev/null +++ b/src/ffi.rs @@ -0,0 +1,40 @@ +extern crate libc; +use self::libc::{uint8_t, size_t}; + +use std::ptr; +use std::slice; +use keys::TPK; +use openpgp; + +#[no_mangle] +pub extern "system" fn sq_tpk_from_bytes(b: *const uint8_t, len: size_t) -> *mut TPK { + assert!(!b.is_null()); + let bytes = unsafe { + slice::from_raw_parts(b, len as usize) + }; + let m = openpgp::Message::from_bytes(bytes); + + if let Some(tpk) = m.ok().and_then(|m| TPK::from_message(m)) { + Box::into_raw(Box::new(tpk)) + } else { + ptr::null_mut() + } +} + +#[no_mangle] +pub extern "system" fn sq_tpk_dump(tpk: *mut TPK) { + assert!(!tpk.is_null()); + unsafe { + println!("{:?}", *tpk); + } +} + +#[no_mangle] +pub extern "system" fn sq_tpk_free(tpk: *mut TPK) { + if tpk.is_null() { + return + } + unsafe { + drop(Box::from_raw(tpk)); + } +} -- cgit v1.2.3