summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJustus Winter <justus@pep-project.org>2017-11-23 14:34:45 +0100
committerJustus Winter <justus@pep-project.org>2017-11-23 14:36:36 +0100
commit9a5725fe55eb5e49cee09712364ac7b7dcefde51 (patch)
tree4f0b3b5e86a02236a06c503e8adcc03fc6fc1d7e /src
parent8cdab3036cfd2111fd3719abc38ab334bc72ebdc (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')
-rw-r--r--src/ffi.rs40
-rw-r--r--src/lib.rs1
-rw-r--r--src/sequoia.h10
3 files changed, 51 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));
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index a69113c7..291db413 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -14,3 +14,4 @@ pub mod openpgp;
pub mod keys;
pub mod key_store;
pub mod net;
+pub mod ffi;
diff --git a/src/sequoia.h b/src/sequoia.h
new file mode 100644
index 00000000..0853b9aa
--- /dev/null
+++ b/src/sequoia.h
@@ -0,0 +1,10 @@
+#ifndef SEQUOIA_H
+#define SEQUOIA_H
+
+struct sq_tpk;
+
+struct sq_tpk *sq_tpk_from_bytes (const char *b, size_t len);
+void sq_tpk_dump (const struct sq_tpk *tpk);
+void sq_tpk_free (struct sq_tpk *tpk);
+
+#endif