summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-02-05 16:49:17 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-02-05 17:58:00 +0100
commit35b03999024955f68008d9ef5287cff753ed178b (patch)
tree5aaf056c8327ddcd06e1f1aaf02812975f31528c
parent575d302977af7f832a6a8f7292a00521843fdefd (diff)
openpgp-ffi: Convert pgp_writer_t.
-rw-r--r--ffi-macros/src/lib.rs8
-rw-r--r--openpgp-ffi/src/armor.rs16
-rw-r--r--openpgp-ffi/src/common.rs14
-rw-r--r--openpgp-ffi/src/io.rs78
-rw-r--r--openpgp-ffi/src/serialize.rs6
5 files changed, 63 insertions, 59 deletions
diff --git a/ffi-macros/src/lib.rs b/ffi-macros/src/lib.rs
index 076918b0..8a3a39f1 100644
--- a/ffi-macros/src/lib.rs
+++ b/ffi-macros/src/lib.rs
@@ -775,13 +775,13 @@ fn derive_serialize(span: proc_macro2::Span, prefix: &str, name: &str,
/// Serializes this object.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
fn #ident (errp: Option<&mut *mut ::error::Error>,
- tsk: *const #wrapper,
- writer: *mut Box<::std::io::Write>)
+ this: *const #wrapper,
+ writer: *mut super::io::Writer)
-> ::error::Status {
use ::RefRaw;
+ use ::RefMutRaw;
use ::MoveResultIntoRaw;
- let writer = ffi_param_ref_mut!(writer);
- tsk.ref_raw().serialize(writer).move_into_raw(errp)
+ this.ref_raw().serialize(writer.ref_mut_raw()).move_into_raw(errp)
}
}
}
diff --git a/openpgp-ffi/src/armor.rs b/openpgp-ffi/src/armor.rs
index bec0ab80..8b0fcfbd 100644
--- a/openpgp-ffi/src/armor.rs
+++ b/openpgp-ffi/src/armor.rs
@@ -7,7 +7,7 @@
use std::mem::size_of;
use std::ptr;
use std::slice;
-use std::io::Write;
+use std::io;
use libc::{self, uint8_t, c_char, c_int, size_t};
extern crate sequoia_openpgp;
@@ -343,14 +343,13 @@ pub extern "system" fn pgp_armor_reader_headers(errp: Option<&mut *mut ::error::
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "system" fn pgp_armor_writer_new
(errp: Option<&mut *mut ::error::Error>,
- inner: *mut Box<Write>,
+ inner: *mut super::io::Writer,
kind: c_int,
header: *const ArmorHeader,
header_len: size_t)
- -> *mut Box<Write>
+ -> Maybe<super::io::Writer>
{
- ffi_make_fry_from_errp!(errp);
- let inner = ffi_param_ref_mut!(inner);
+ let inner = inner.ref_mut_raw();
let kind = int_to_kind(kind).expect("KIND must not be PGP_ARMOR_KIND_ANY");
let mut header_ = Vec::new();
@@ -370,7 +369,8 @@ pub extern "system" fn pgp_armor_writer_new
let header: Vec<(&str, &str)> =
header_.iter().map(|h| (h.0.as_ref(), h.1.as_ref())).collect();
- ffi_try_box!(armor::Writer::new(inner, kind, &header)
- .map(|r| Box::new(r))
- .map_err(|e| ::failure::Error::from(e)))
+ armor::Writer::new(inner, kind, &header)
+ .map(|w| -> Box<io::Write> { Box::new(w) })
+ .map_err(|e| ::failure::Error::from(e))
+ .move_into_raw(errp)
}
diff --git a/openpgp-ffi/src/common.rs b/openpgp-ffi/src/common.rs
index 45d788e0..d76ea7a1 100644
--- a/openpgp-ffi/src/common.rs
+++ b/openpgp-ffi/src/common.rs
@@ -291,7 +291,7 @@ pub mod tsk;
use std::ptr;
use std::slice;
use std::io as std_io;
-use std::io::{Read, Write};
+use std::io::Read;
use libc::{c_int, size_t, c_void};
use failure::ResultExt;
@@ -575,7 +575,7 @@ impl VerificationHelper for VHelper {
fn verify_real<'a>(input: &'a mut Read,
dsig: Option<&mut io::ReaderKind>,
- output: Option<&'a mut Box<'a + Write>>,
+ output: Option<&mut Box<::std::io::Write>>,
get_public_keys: GetPublicKeysCallback,
check_signatures: CheckSignaturesCallback,
cookie: *mut HelperCookie)
@@ -626,7 +626,7 @@ fn verify_real<'a>(input: &'a mut Read,
pub fn pgp_verify<'a>(errp: Option<&mut *mut ::error::Error>,
input: *mut io::Reader,
dsig: Maybe<io::Reader>,
- output: Option<&'a mut Box<'a + Write>>,
+ output: Maybe<io::Writer>,
get_public_keys: GetPublicKeysCallback,
check_signatures: CheckSignaturesCallback,
cookie: *mut HelperCookie)
@@ -635,7 +635,7 @@ pub fn pgp_verify<'a>(errp: Option<&mut *mut ::error::Error>,
ffi_make_fry_from_errp!(errp);
let input = input.ref_mut_raw();
- let r = verify_real(input, dsig.ref_mut_raw(), output,
+ let r = verify_real(input, dsig.ref_mut_raw(), output.ref_mut_raw(),
get_public_keys, check_signatures, cookie);
ffi_try_status!(r)
@@ -708,7 +708,7 @@ impl DecryptionHelper for DHelper {
// A helper function that returns a Result so that we can use ? to
// propagate errors.
fn decrypt_real<'a>(input: &'a mut io::ReaderKind,
- output: &'a mut Box<'a + Write>,
+ output: &'a mut ::std::io::Write,
get_public_keys: GetPublicKeysCallback,
get_secret_keys: GetSecretKeysCallback,
check_signatures: CheckSignaturesCallback,
@@ -748,7 +748,7 @@ fn decrypt_real<'a>(input: &'a mut io::ReaderKind,
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub fn pgp_decrypt<'a>(errp: Option<&mut *mut ::error::Error>,
input: *mut io::Reader,
- output: *mut Box<'a + Write>,
+ output: *mut io::Writer,
get_public_keys: GetPublicKeysCallback,
get_secret_keys: GetSecretKeysCallback,
check_signatures: CheckSignaturesCallback,
@@ -757,7 +757,7 @@ pub fn pgp_decrypt<'a>(errp: Option<&mut *mut ::error::Error>,
{
ffi_make_fry_from_errp!(errp);
let input = input.ref_mut_raw();
- let output = ffi_param_ref_mut!(output);
+ let output = output.ref_mut_raw();
let r = decrypt_real(input, output,
get_public_keys, get_secret_keys, check_signatures, cookie);
diff --git a/openpgp-ffi/src/io.rs b/openpgp-ffi/src/io.rs
index d5f4e540..d0e5ff37 100644
--- a/openpgp-ffi/src/io.rs
+++ b/openpgp-ffi/src/io.rs
@@ -96,39 +96,42 @@ pub extern "system" fn pgp_reader_read(errp: Option<&mut *mut ::error::Error>,
}
+/// Wraps a generic writer.
+#[::ffi_wrapper_type(prefix = "pgp_")]
+pub struct Writer(Box<io::Write>);
+
/// Opens a file returning a writer.
///
/// The file will be created if it does not exist, or be truncated
/// otherwise. If you need more control, use `pgp_writer_from_fd`.
-#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
-pub extern "system" fn pgp_writer_from_file(errp: Option<&mut *mut ::error::Error>,
- filename: *const c_char)
- -> *mut Box<Write> {
- ffi_make_fry_from_errp!(errp);
+#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
+fn pgp_writer_from_file(errp: Option<&mut *mut ::error::Error>,
+ filename: *const c_char)
+ -> Maybe<Writer> {
let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
- ffi_try_box!(File::create(Path::new(&filename))
- .map(|r| Box::new(r))
- .map_err(|e| ::failure::Error::from(e)))
+ File::create(Path::new(&filename))
+ .map(|w| -> Box<io::Write> { Box::new(w) })
+ .map_err(|e| ::failure::Error::from(e))
+ .move_into_raw(errp)
}
/// Opens a file descriptor returning a writer.
#[cfg(unix)]
-#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
-pub extern "system" fn pgp_writer_from_fd(fd: c_int)
- -> *mut Box<Write> {
- box_raw!(Box::new(unsafe { File::from_raw_fd(fd) }))
+#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
+fn pgp_writer_from_fd(fd: c_int) -> *mut Writer {
+ let w: Box<io::Write> = Box::new(unsafe { File::from_raw_fd(fd) });
+ w.move_into_raw()
}
/// Creates a writer from a buffer.
-#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
-pub extern "system" fn pgp_writer_from_bytes(buf: *mut uint8_t,
- len: size_t)
- -> *mut Box<Write> {
+#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
+fn pgp_writer_from_bytes(buf: *mut uint8_t, len: size_t) -> *mut Writer {
assert!(!buf.is_null());
let buf = unsafe {
slice::from_raw_parts_mut(buf, len as usize)
};
- box_raw!(Box::new(Cursor::new(buf)))
+ let w: Box<io::Write> = Box::new(Cursor::new(buf));
+ w.move_into_raw()
}
/// Creates an allocating writer.
@@ -139,17 +142,17 @@ pub extern "system" fn pgp_writer_from_bytes(buf: *mut uint8_t,
/// reference a chunk of memory allocated using libc's heap allocator.
/// The caller is responsible to `free` it once the writer has been
/// destroyed.
-#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
-pub extern "system" fn pgp_writer_alloc(buf: *mut *mut c_void,
- len: *mut size_t)
- -> *mut Box<Write> {
+#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
+fn pgp_writer_alloc(buf: *mut *mut c_void, len: *mut size_t)
+ -> *mut Writer {
let buf = ffi_param_ref_mut!(buf);
let len = ffi_param_ref_mut!(len);
- box_raw!(Box::new(WriterAlloc {
+ let w: Box<io::Write> = Box::new(WriterAlloc {
buf: buf,
len: len,
- }))
+ });
+ w.move_into_raw()
}
struct WriterAlloc {
@@ -185,23 +188,24 @@ impl Write for WriterAlloc {
}
}
-/// Frees a writer.
-#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
-pub extern "system" fn pgp_writer_free(writer: Option<&mut Box<Write>>) {
- ffi_free!(writer)
-}
-
/// Writes up to `len` bytes of `buf` into `writer`.
-#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
-pub extern "system" fn pgp_writer_write(errp: Option<&mut *mut ::error::Error>,
- writer: *mut Box<Write>,
- buf: *const uint8_t, len: size_t)
- -> ssize_t {
- ffi_make_fry_from_errp!(errp);
- let writer = ffi_param_ref_mut!(writer);
+#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
+fn pgp_writer_write(errp: Option<&mut *mut ::error::Error>,
+ writer: *mut Writer,
+ buf: *const uint8_t, len: size_t)
+ -> ssize_t {
assert!(!buf.is_null());
let buf = unsafe {
slice::from_raw_parts(buf, len as usize)
};
- ffi_try_or!(writer.write(buf).map_err(|e| ::failure::Error::from(e)), -1) as ssize_t
+ writer.ref_mut_raw().write(buf)
+ .map(|n_read| n_read as ssize_t)
+ .unwrap_or_else(|e| {
+ if let Some(errp) = errp {
+ *errp = ::failure::Error::from(e).move_into_raw();
+ };
+
+ // Signal failure.
+ -1
+ })
}
diff --git a/openpgp-ffi/src/serialize.rs b/openpgp-ffi/src/serialize.rs
index 571a0bf8..04240d0c 100644
--- a/openpgp-ffi/src/serialize.rs
+++ b/openpgp-ffi/src/serialize.rs
@@ -21,6 +21,7 @@ use self::openpgp::constants::{
};
use error::Status;
+use MoveFromRaw;
use self::openpgp::serialize::{
writer,
@@ -40,11 +41,10 @@ use super::openpgp::TPK;
/// Streams an OpenPGP message.
#[::sequoia_ffi_macros::extern_fn] #[no_mangle]
pub extern "system" fn pgp_writer_stack_message
- (writer: *mut Box<Write>)
+ (writer: *mut super::io::Writer)
-> *mut writer::Stack<'static, Cookie>
{
- let writer = ffi_param_move!(writer);
- box_raw!(Message::new(writer))
+ box_raw!(Message::new(writer.move_from_raw()))
}
/// Writes up to `len` bytes of `buf` into `writer`.