summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/src/packet_pile.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-01-17 11:11:27 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-17 16:48:28 +0100
commit3f58832474a4b270e136544016a401ef773ac065 (patch)
treec617160250c3040ca964c1b72ab5957cd872b82f /openpgp-ffi/src/packet_pile.rs
parent38b4108cc1eac851ac17932c5c33623dd535bebb (diff)
openpgp-ffi: New crate.
- This creates a new crate, 'sequoia-openpgp-ffi', and moves a handful of functions from 'sequoia-ffi' to it. - The 'sequoia-ffi' crate is a superset of the 'sequoia-openpgp-ffi' crate. This is accomplished by some include! magic. - My first attempt involved having 'sequoia-ffi' depend on 'sequoia-openpgp-ffi', so that the former just re-exports the symbols. However, that turned out to be unreliable, and might be not what we want, because it could also duplicate parts of Rust's standard library. - Fixes #144.
Diffstat (limited to 'openpgp-ffi/src/packet_pile.rs')
-rw-r--r--openpgp-ffi/src/packet_pile.rs93
1 files changed, 93 insertions, 0 deletions
diff --git a/openpgp-ffi/src/packet_pile.rs b/openpgp-ffi/src/packet_pile.rs
new file mode 100644
index 00000000..742c782cc
--- /dev/null
+++ b/openpgp-ffi/src/packet_pile.rs
@@ -0,0 +1,93 @@
+//! Handles PacketPiles.
+//!
+//! Wraps [`sequoia-openpgp::PacketPile`].
+//!
+//! [`sequoia-openpgp::PacketPile`]: ../../../sequoia_openpgp/struct.PacketPile.html
+
+use std::slice;
+use std::io::{Read, Write};
+use libc::{uint8_t, c_char, size_t};
+
+extern crate sequoia_openpgp;
+use self::sequoia_openpgp::{
+ PacketPile,
+ parse::Parse,
+ serialize::Serialize,
+};
+
+use ::error::Status;
+
+/// Deserializes the OpenPGP message stored in a `std::io::Read`
+/// object.
+///
+/// Although this method is easier to use to parse an OpenPGP
+/// message than a `PacketParser` or a `PacketPileParser`, this
+/// interface buffers the whole message in memory. Thus, the
+/// caller must be certain that the *deserialized* message is not
+/// too large.
+///
+/// Note: this interface *does* buffer the contents of packets.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_packet_pile_from_reader(errp: Option<&mut *mut failure::Error>,
+ reader: *mut Box<Read>)
+ -> *mut PacketPile {
+ ffi_make_fry_from_errp!(errp);
+ let reader = ffi_param_ref_mut!(reader);
+ ffi_try_box!(PacketPile::from_reader(reader))
+}
+
+/// Deserializes the OpenPGP message stored in the file named by
+/// `filename`.
+///
+/// See `sq_packet_pile_from_reader` for more details and caveats.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_packet_pile_from_file(errp: Option<&mut *mut failure::Error>,
+ filename: *const c_char)
+ -> *mut PacketPile {
+ ffi_make_fry_from_errp!(errp);
+ let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
+ ffi_try_box!(PacketPile::from_file(&filename))
+}
+
+/// Deserializes the OpenPGP message stored in the provided buffer.
+///
+/// See `sq_packet_pile_from_reader` for more details and caveats.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_packet_pile_from_bytes(errp: Option<&mut *mut failure::Error>,
+ b: *const uint8_t, len: size_t)
+ -> *mut PacketPile {
+ ffi_make_fry_from_errp!(errp);
+ assert!(!b.is_null());
+ let buf = unsafe {
+ slice::from_raw_parts(b, len as usize)
+ };
+
+ ffi_try_box!(PacketPile::from_bytes(buf))
+}
+
+/// Frees the packet_pile.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_packet_pile_free(packet_pile: Option<&mut PacketPile>)
+{
+ ffi_free!(packet_pile)
+}
+
+/// Clones the PacketPile.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_packet_pile_clone(packet_pile: *const PacketPile)
+ -> *mut PacketPile {
+ let packet_pile = ffi_param_ref!(packet_pile);
+ box_raw!(packet_pile.clone())
+}
+
+/// Serializes the packet pile.
+#[::ffi_catch_abort] #[no_mangle]
+pub extern "system" fn sq_packet_pile_serialize(errp: Option<&mut *mut failure::Error>,
+ packet_pile: *const PacketPile,
+ writer: *mut Box<Write>)
+ -> Status {
+ ffi_make_fry_from_errp!(errp);
+ let packet_pile = ffi_param_ref!(packet_pile);
+ let writer = ffi_param_ref_mut!(writer);
+ ffi_try_status!(packet_pile.serialize(writer))
+}