diff options
author | Justus Winter <justus@pep-project.org> | 2017-11-30 18:52:45 +0100 |
---|---|---|
committer | Justus Winter <justus@pep-project.org> | 2017-12-01 13:54:28 +0100 |
commit | 8659731e6ecd694e7fa87799497aeb0338b7292c (patch) | |
tree | c5a9f0b6a30a6052ae063945506c90d890409bab /src/ffi.rs | |
parent | e55151e501270875731cb89e83dd4e16402de025 (diff) |
Create context objects.
- Context objects can be used to tweak the library configuration.
Currently, a home and lib directory can be set. Reasonable
defaults are provided.
- Add ffi functions.
Diffstat (limited to 'src/ffi.rs')
-rw-r--r-- | src/ffi.rs | 48 |
1 files changed, 47 insertions, 1 deletions
@@ -1,10 +1,56 @@ extern crate libc; -use self::libc::{uint8_t, size_t}; +use self::libc::{uint8_t, c_char, size_t}; +use std::ffi::CStr; use std::ptr; use std::slice; +use std::str; + use keys::TPK; use openpgp; +use super::Context; + +/// Create a context object. +/// +/// If `home` is not `NULL`, it is used as directory containing shared +/// state and rendezvous nodes. If `lib` is not `NULL`, it is used as +/// directory containing backend servers. If either argument is +/// `NULL`, a reasonable default is used. +/// +/// Returns `NULL` on errors. +#[no_mangle] +pub extern "system" fn sq_context_new(home: *const c_char, + lib: *const c_char) -> *mut Context { + let home = unsafe { + if home.is_null() { None } else { Some(CStr::from_ptr(home)) } + }; + let lib = unsafe { + if lib.is_null() { None } else { Some(CStr::from_ptr(lib)) } + }; + + let mut pre = Context::new(); + + if let Some(home) = home { + pre = pre.home(home.to_string_lossy().as_ref()); + } + if let Some(lib) = lib { + pre = pre.lib(lib.to_string_lossy().as_ref()); + } + + if let Ok(context) = pre.finalize() { + Box::into_raw(Box::new(context)) + } else { + ptr::null_mut() + } +} + +/// Free a context. +#[no_mangle] +pub extern "system" fn sq_context_free(context: *mut Context) { + unsafe { + drop(Box::from_raw(context)); + } +} #[no_mangle] pub extern "system" fn sq_tpk_from_bytes(b: *const uint8_t, len: size_t) -> *mut TPK { |