diff options
author | Justus Winter <justus@pep-project.org> | 2017-11-23 14:34:45 +0100 |
---|---|---|
committer | Justus Winter <justus@pep-project.org> | 2017-11-23 14:36:36 +0100 |
commit | 9a5725fe55eb5e49cee09712364ac7b7dcefde51 (patch) | |
tree | 4f0b3b5e86a02236a06c503e8adcc03fc6fc1d7e /src/ffi.rs | |
parent | 8cdab3036cfd2111fd3719abc38ab334bc72ebdc (diff) |
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.
Diffstat (limited to 'src/ffi.rs')
-rw-r--r-- | src/ffi.rs | 40 |
1 files changed, 40 insertions, 0 deletions
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)); + } +} |