diff options
author | Neal H. Walfield <neal@pep.foundation> | 2019-03-22 12:45:42 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2019-03-22 12:47:55 +0100 |
commit | 1d35557a8f7866141f84b2764287f384a8e12872 (patch) | |
tree | 5ca70f2e45553cc26cc580b2133433107e975577 /openpgp-ffi/src/io.rs | |
parent | a533d1e81d683eb0e1679f084c604d0674b5e130 (diff) |
openpgp-ffi: Add convenience functions to work with pgp_reader_t:s.
- pgp_reader_copy: Copy up to n bytes from a reader to a writer.
- pgp_reader_discard: Read to EOF and discard everything that is
read.
Diffstat (limited to 'openpgp-ffi/src/io.rs')
-rw-r--r-- | openpgp-ffi/src/io.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/openpgp-ffi/src/io.rs b/openpgp-ffi/src/io.rs index d0e5ff37..b1c74031 100644 --- a/openpgp-ffi/src/io.rs +++ b/openpgp-ffi/src/io.rs @@ -95,6 +95,46 @@ pub extern "system" fn pgp_reader_read(errp: Option<&mut *mut ::error::Error>, }) } +/// Copies up to `len` bytes from `source` to `dest`. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] +pub extern "system" fn pgp_reader_copy(errp: Option<&mut *mut ::error::Error>, + source: *mut Reader, + dest: *mut Writer, + len: size_t) + -> ssize_t { + let source = source.ref_mut_raw(); + let dest = dest.ref_mut_raw(); + + io::copy(&mut source.take(len as u64), dest) + .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 + }) +} + +/// Reads all data from reader and discards it. +#[::sequoia_ffi_macros::extern_fn] #[no_mangle] +pub extern "system" fn pgp_reader_discard(errp: Option<&mut *mut ::error::Error>, + reader: *mut Reader) + -> ssize_t { + let mut reader = reader.ref_mut_raw(); + + io::copy(&mut reader, &mut io::sink()) + .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 + }) +} /// Wraps a generic writer. #[::ffi_wrapper_type(prefix = "pgp_")] |