summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-01-15 16:11:25 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-15 18:02:37 +0100
commit1f504ddf2d09f62ea3a68aab6deeac24aa813b54 (patch)
treee102f9b89476e3c870cca1625aac01a02f406c33
parentaf870aa2bf8b844c8fab3f09cc7942c4c761dc06 (diff)
ffi: Introduce macro for *char parameters.
-rw-r--r--ffi/src/core.rs32
-rw-r--r--ffi/src/lib.rs16
-rw-r--r--ffi/src/net.rs17
-rw-r--r--ffi/src/openpgp/armor.rs24
-rw-r--r--ffi/src/openpgp/fingerprint.rs5
-rw-r--r--ffi/src/openpgp/keyid.rs5
-rw-r--r--ffi/src/openpgp/mod.rs11
-rw-r--r--ffi/src/openpgp/packet_pile.rs6
-rw-r--r--ffi/src/openpgp/tpk.rs17
-rw-r--r--ffi/src/openpgp/tsk.rs8
-rw-r--r--ffi/src/store.rs29
11 files changed, 58 insertions, 112 deletions
diff --git a/ffi/src/core.rs b/ffi/src/core.rs
index 683f0d86..609b65f9 100644
--- a/ffi/src/core.rs
+++ b/ffi/src/core.rs
@@ -40,7 +40,7 @@
use failure;
use std::fs::File;
-use std::ffi::{CString, CStr};
+use std::ffi::CString;
use std::io::{self, Read, Write, Cursor};
use std::path::Path;
use std::ptr;
@@ -96,10 +96,7 @@ pub extern "system" fn sq_string_free(s: *mut c_char) {
pub extern "system" fn sq_context_new(domain: *const c_char,
errp: Option<&mut *mut failure::Error>)
-> *mut Context {
- assert!(! domain.is_null());
- let domain = unsafe {
- CStr::from_ptr(domain).to_string_lossy()
- };
+ let domain = ffi_param_cstr!(domain).to_string_lossy();
match core::Context::new(&domain) {
Ok(context) =>
@@ -131,10 +128,7 @@ pub extern "system" fn sq_context_free(context: Option<&mut Context>) {
#[no_mangle]
pub extern "system" fn sq_context_configure(domain: *const c_char)
-> *mut Config {
- assert!(! domain.is_null());
- let domain = unsafe {
- CStr::from_ptr(domain).to_string_lossy()
- };
+ let domain = ffi_param_cstr!(domain).to_string_lossy();
Box::into_raw(Box::new(core::Context::configure(&domain)))
}
@@ -211,10 +205,7 @@ pub extern "system" fn sq_config_build(cfg: *mut Config,
pub extern "system" fn sq_config_home(cfg: *mut Config,
home: *const c_char) {
let cfg = ffi_param_ref_mut!(cfg);
- assert!(! home.is_null());
- let home = unsafe {
- CStr::from_ptr(home).to_string_lossy()
- };
+ let home = ffi_param_cstr!(home).to_string_lossy();
cfg.set_home(home.as_ref())
}
@@ -223,10 +214,7 @@ pub extern "system" fn sq_config_home(cfg: *mut Config,
pub extern "system" fn sq_config_lib(cfg: *mut Config,
lib: *const c_char) {
let cfg = ffi_param_ref_mut!(cfg);
- assert!(! lib.is_null());
- let lib = unsafe {
- CStr::from_ptr(lib).to_string_lossy()
- };
+ let lib = ffi_param_cstr!(lib).to_string_lossy();
cfg.set_lib(&lib.as_ref())
}
@@ -267,10 +255,7 @@ pub extern "system" fn sq_reader_from_file(ctx: *mut Context,
filename: *const c_char)
-> *mut Box<Read> {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! filename.is_null());
- let filename = unsafe {
- CStr::from_ptr(filename).to_string_lossy().into_owned()
- };
+ let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
fry_box!(ctx, File::open(Path::new(&filename))
.map(|r| Box::new(r))
.map_err(|e| e.into()))
@@ -327,10 +312,7 @@ pub extern "system" fn sq_writer_from_file(ctx: *mut Context,
filename: *const c_char)
-> *mut Box<Write> {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! filename.is_null());
- let filename = unsafe {
- CStr::from_ptr(filename).to_string_lossy().into_owned()
- };
+ let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
fry_box!(ctx, File::create(Path::new(&filename))
.map(|r| Box::new(r))
.map_err(|e| e.into()))
diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs
index cbf159ba..dd87e6fa 100644
--- a/ffi/src/lib.rs
+++ b/ffi/src/lib.rs
@@ -193,6 +193,22 @@ macro_rules! ffi_param_ref_mut {
}};
}
+/// Transfers a reference to a string from C to Rust.
+///
+/// # Panics
+///
+/// Panics if called with NULL.
+macro_rules! ffi_param_cstr {
+ ($name:expr) => {{
+ if $name.is_null() {
+ panic!("Parameter {} is NULL", stringify!($name));
+ }
+ unsafe {
+ ::std::ffi::CStr::from_ptr($name)
+ }
+ }};
+}
+
/// Like try! for ffi glue.
///
/// Evaluates the given expression. On success, evaluate to
diff --git a/ffi/src/net.rs b/ffi/src/net.rs
index 66f951ce..6935a564 100644
--- a/ffi/src/net.rs
+++ b/ffi/src/net.rs
@@ -29,7 +29,6 @@
use libc::{uint8_t, c_char, size_t};
use native_tls::Certificate;
-use std::ffi::CStr;
use std::ptr;
use std::slice;
@@ -52,11 +51,9 @@ use super::core::Context;
pub extern "system" fn sq_keyserver_new(ctx: *mut Context,
uri: *const c_char) -> *mut KeyServer {
let ctx = ffi_param_ref_mut!(ctx);
- let uri = unsafe {
- if uri.is_null() { None } else { Some(CStr::from_ptr(uri)) }
- };
+ let uri = ffi_param_cstr!(uri).to_string_lossy();
- fry_box!(ctx, KeyServer::new(&ctx.c, &uri.unwrap().to_string_lossy()))
+ fry_box!(ctx, KeyServer::new(&ctx.c, &uri))
}
/// Returns a handle for the given URI.
@@ -72,11 +69,9 @@ pub extern "system" fn sq_keyserver_with_cert(ctx: *mut Context,
cert: *const uint8_t,
len: size_t) -> *mut KeyServer {
let ctx = ffi_param_ref_mut!(ctx);
- let uri = unsafe {
- if uri.is_null() { None } else { Some(CStr::from_ptr(uri)) }
- };
+ let uri = ffi_param_cstr!(uri).to_string_lossy();
- if uri.is_none() || cert.is_null() {
+ if cert.is_null() {
return ptr::null_mut();
}
@@ -86,9 +81,7 @@ pub extern "system" fn sq_keyserver_with_cert(ctx: *mut Context,
let cert = fry!(ctx, Certificate::from_der(cert)
.map_err(|e| e.into()));
- fry_box!(ctx, KeyServer::with_cert(&ctx.c,
- &uri.unwrap().to_string_lossy(),
- cert))
+ fry_box!(ctx, KeyServer::with_cert(&ctx.c, &uri, cert))
}
/// Returns a handle for the SKS keyserver pool.
diff --git a/ffi/src/openpgp/armor.rs b/ffi/src/openpgp/armor.rs
index 80b0e74c..c51b289b 100644
--- a/ffi/src/openpgp/armor.rs
+++ b/ffi/src/openpgp/armor.rs
@@ -4,7 +4,6 @@
//!
//! [`sequoia-openpgp::armor`]: ../../../sequoia_openpgp/armor/index.html
-use std::ffi::CStr;
use std::mem::size_of;
use std::ptr;
use std::slice;
@@ -146,10 +145,7 @@ pub extern "system" fn sq_armor_reader_from_file(ctx: *mut Context,
kind: c_int)
-> *mut Box<Read> {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! filename.is_null());
- let filename = unsafe {
- CStr::from_ptr(filename).to_string_lossy().into_owned()
- };
+ let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
let kind = int_to_kind(kind);
fry_box!(ctx, armor::Reader::from_file(&filename, kind)
@@ -358,17 +354,15 @@ pub extern "system" fn sq_armor_writer_new
let mut header_ = Vec::new();
if header_len > 0 {
- let header = ffi_param_ref!(header);
- let header = unsafe {
- slice::from_raw_parts(header, header_len)
+ let headers = ffi_param_ref!(header);
+ let headers = unsafe {
+ slice::from_raw_parts(headers, header_len)
};
- for h in header {
- assert!(! h.key.is_null());
- assert!(! h.value.is_null());
- header_.push(unsafe {
- (CStr::from_ptr(h.key).to_string_lossy(),
- CStr::from_ptr(h.value).to_string_lossy())
- });
+ for header in headers {
+ header_.push(
+ (ffi_param_cstr!(header.key).to_string_lossy(),
+ ffi_param_cstr!(header.value).to_string_lossy())
+ );
}
}
diff --git a/ffi/src/openpgp/fingerprint.rs b/ffi/src/openpgp/fingerprint.rs
index b2937f43..0a8ed08b 100644
--- a/ffi/src/openpgp/fingerprint.rs
+++ b/ffi/src/openpgp/fingerprint.rs
@@ -4,7 +4,7 @@
//!
//! [`sequoia-openpgp::Fingerprint`]: ../../../sequoia_openpgp/enum.Fingerprint.html
-use std::ffi::{CString, CStr};
+use std::ffi::CString;
use std::hash::{Hash, Hasher};
use std::ptr;
use std::slice;
@@ -31,8 +31,7 @@ pub extern "system" fn sq_fingerprint_from_bytes(buf: *const uint8_t,
#[no_mangle]
pub extern "system" fn sq_fingerprint_from_hex(hex: *const c_char)
-> *mut Fingerprint {
- assert!(!hex.is_null());
- let hex = unsafe { CStr::from_ptr(hex).to_string_lossy() };
+ let hex = ffi_param_cstr!(hex).to_string_lossy();
Fingerprint::from_hex(&hex)
.map(|fp| Box::into_raw(Box::new(fp)))
.unwrap_or(ptr::null_mut())
diff --git a/ffi/src/openpgp/keyid.rs b/ffi/src/openpgp/keyid.rs
index 6df8ca81..045e5133 100644
--- a/ffi/src/openpgp/keyid.rs
+++ b/ffi/src/openpgp/keyid.rs
@@ -4,7 +4,7 @@
//!
//! [`sequoia-openpgp::KeyID`]: ../../../sequoia_openpgp/enum.KeyID.html
-use std::ffi::{CString, CStr};
+use std::ffi::CString;
use std::hash::{Hash, Hasher};
use std::ptr;
use std::slice;
@@ -42,8 +42,7 @@ pub extern "system" fn sq_keyid_from_bytes(id: *const uint8_t) -> *mut KeyID {
/// 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() };
+ let id = ffi_param_cstr!(id).to_string_lossy();
KeyID::from_hex(&id)
.map(|id| Box::into_raw(Box::new(id)))
.unwrap_or(ptr::null_mut())
diff --git a/ffi/src/openpgp/mod.rs b/ffi/src/openpgp/mod.rs
index ca3b5531..9fe0022d 100644
--- a/ffi/src/openpgp/mod.rs
+++ b/ffi/src/openpgp/mod.rs
@@ -1,7 +1,6 @@
//! XXX
use failure;
-use std::ffi::CStr;
use std::mem::forget;
use std::ptr;
use std::slice;
@@ -625,10 +624,7 @@ pub extern "system" fn sq_packet_parser_from_file
(ctx: *mut Context, filename: *const c_char)
-> *mut PacketParserResult<'static> {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! filename.is_null());
- let filename = unsafe {
- CStr::from_ptr(filename).to_string_lossy().into_owned()
- };
+ let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
fry_box!(ctx, PacketParser::from_file(&filename))
}
@@ -1197,9 +1193,8 @@ pub extern "system" fn sq_encryptor_new
slice::from_raw_parts(passwords, passwords_len)
};
for password in passwords {
- passwords_.push(unsafe {
- CStr::from_ptr(*password)
- }.to_bytes().to_owned().into());
+ passwords_.push(ffi_param_cstr!(*password)
+ .to_bytes().to_owned().into());
}
}
let recipients = if recipients_len > 0 {
diff --git a/ffi/src/openpgp/packet_pile.rs b/ffi/src/openpgp/packet_pile.rs
index e9e32173..3f634e4f 100644
--- a/ffi/src/openpgp/packet_pile.rs
+++ b/ffi/src/openpgp/packet_pile.rs
@@ -4,7 +4,6 @@
//!
//! [`sequoia-openpgp::PacketPile`]: ../../../sequoia_openpgp/struct.PacketPile.html
-use std::ffi::CStr;
use std::slice;
use std::io::{Read, Write};
use libc::{uint8_t, c_char, size_t};
@@ -47,10 +46,7 @@ pub extern "system" fn sq_packet_pile_from_file(ctx: *mut Context,
filename: *const c_char)
-> *mut PacketPile {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! filename.is_null());
- let filename = unsafe {
- CStr::from_ptr(filename).to_string_lossy().into_owned()
- };
+ let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
fry_box!(ctx, PacketPile::from_file(&filename))
}
diff --git a/ffi/src/openpgp/tpk.rs b/ffi/src/openpgp/tpk.rs
index 6acb45de..66f38a5e 100644
--- a/ffi/src/openpgp/tpk.rs
+++ b/ffi/src/openpgp/tpk.rs
@@ -5,7 +5,7 @@
//! [`sequoia-openpgp::TPK`]: ../../../sequoia_openpgp/struct.TPK.html
//! [related functionality]: ../../../sequoia_openpgp/tpk/index.html
-use std::ffi::{CString, CStr};
+use std::ffi::CString;
use std::ptr;
use std::slice;
use std::io::{Read, Write};
@@ -54,10 +54,7 @@ pub extern "system" fn sq_tpk_from_file(ctx: *mut Context,
filename: *const c_char)
-> *mut TPK {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! filename.is_null());
- let filename = unsafe {
- CStr::from_ptr(filename).to_string_lossy().into_owned()
- };
+ let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
fry_box!(ctx, TPK::from_file(&filename))
}
@@ -304,9 +301,7 @@ pub extern "system" fn sq_tpk_revoke(ctx: *mut Context,
let signer = ffi_param_ref_mut!(primary_signer);
let code = int_to_reason_for_revocation(code);
let reason = if let Some(reason) = reason {
- unsafe {
- CStr::from_ptr(reason).to_bytes()
- }
+ ffi_param_cstr!(reason as *const c_char).to_bytes()
} else {
b""
};
@@ -373,9 +368,7 @@ pub extern "system" fn sq_tpk_revoke_in_place(ctx: *mut Context,
let signer = ffi_param_ref_mut!(primary_signer);
let code = int_to_reason_for_revocation(code);
let reason = if let Some(reason) = reason {
- unsafe {
- CStr::from_ptr(reason).to_bytes()
- }
+ ffi_param_cstr!(reason as *const c_char).to_bytes()
} else {
b""
};
@@ -662,7 +655,7 @@ pub extern "system" fn sq_tpk_builder_add_userid
{
let tpkb = ffi_param_ref_mut!(tpkb);
let tpkb_ = ffi_param_move!(*tpkb);
- let uid = unsafe { CStr::from_ptr(uid).to_string_lossy().to_string() };
+ let uid = ffi_param_cstr!(uid).to_string_lossy().to_string();
let tpkb_ = tpkb_.add_userid(uid.as_ref());
*tpkb = box_raw!(tpkb_);
}
diff --git a/ffi/src/openpgp/tsk.rs b/ffi/src/openpgp/tsk.rs
index a92124d9..915ab702 100644
--- a/ffi/src/openpgp/tsk.rs
+++ b/ffi/src/openpgp/tsk.rs
@@ -5,7 +5,6 @@
//! [`sequoia-openpgp::TSK`]: ../../../sequoia_openpgp/struct.TSK.html
use failure;
-use std::ffi::CStr;
use std::io::Write;
use libc::c_char;
@@ -29,13 +28,10 @@ pub extern "system" fn sq_tsk_new(ctx: *mut Context,
-> Status
{
let ctx = ffi_param_ref_mut!(ctx);
- assert!(!primary_uid.is_null());
let tsk_out = ffi_param_ref_mut!(tsk_out);
let revocation_out = ffi_param_ref_mut!(revocation_out);
- let primary_uid = unsafe {
- CStr::from_ptr(primary_uid)
- };
- match TSK::new(primary_uid.to_string_lossy()) {
+ let primary_uid = ffi_param_cstr!(primary_uid).to_string_lossy();
+ match TSK::new(primary_uid) {
Ok((tsk, revocation)) => {
*tsk_out = box_raw!(tsk);
*revocation_out = box_raw!(revocation);
diff --git a/ffi/src/store.rs b/ffi/src/store.rs
index 6ba7e387..71cb20a0 100644
--- a/ffi/src/store.rs
+++ b/ffi/src/store.rs
@@ -24,7 +24,7 @@
use libc::{uint8_t, uint64_t, c_char};
-use std::ffi::{CStr, CString};
+use std::ffi::CString;
use std::ptr;
extern crate sequoia_openpgp as openpgp;
@@ -48,11 +48,7 @@ pub extern "system" fn sq_store_list_stores(ctx: *mut Context,
domain_prefix: *const c_char)
-> *mut StoreIter {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! domain_prefix.is_null());
-
- let domain_prefix = unsafe {
- CStr::from_ptr(domain_prefix).to_string_lossy()
- };
+ let domain_prefix = ffi_param_cstr!(domain_prefix).to_string_lossy();
fry_box!(ctx, Store::list(&ctx.c, &domain_prefix))
}
@@ -203,11 +199,7 @@ pub extern "system" fn sq_store_open(ctx: *mut Context,
name: *const c_char)
-> *mut Store {
let ctx = ffi_param_ref_mut!(ctx);
- assert!(! name.is_null());
-
- let name = unsafe {
- CStr::from_ptr(name).to_string_lossy()
- };
+ let name = ffi_param_cstr!(name).to_string_lossy();
fry_box!(ctx, Store::open(&ctx.c, &name))
}
@@ -227,10 +219,7 @@ pub extern "system" fn sq_store_add(ctx: *mut Context,
-> *mut Binding {
let ctx = ffi_param_ref_mut!(ctx);
let store = ffi_param_ref!(store);
- assert!(! label.is_null());
- let label = unsafe {
- CStr::from_ptr(label).to_string_lossy()
- };
+ let label = ffi_param_cstr!(label).to_string_lossy();
let fingerprint = ffi_param_ref!(fingerprint);
fry_box!(ctx, store.add(&label, fingerprint))
@@ -245,10 +234,7 @@ pub extern "system" fn sq_store_import(ctx: *mut Context,
-> *mut TPK {
let ctx = ffi_param_ref_mut!(ctx);
let store = ffi_param_ref!(store);
- assert!(! label.is_null());
- let label = unsafe {
- CStr::from_ptr(label).to_string_lossy()
- };
+ let label = ffi_param_cstr!(label).to_string_lossy();
let tpk = ffi_param_ref!(tpk);
fry_box!(ctx, store.import(&label, tpk))
@@ -262,10 +248,7 @@ pub extern "system" fn sq_store_lookup(ctx: *mut Context,
-> *mut Binding {
let ctx = ffi_param_ref_mut!(ctx);
let store = ffi_param_ref!(store);
- assert!(! label.is_null());
- let label = unsafe {
- CStr::from_ptr(label).to_string_lossy()
- };
+ let label = ffi_param_cstr!(label).to_string_lossy();
fry_box!(ctx, store.lookup(&label))
}