summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ffi-macros/src/lib.rs50
-rw-r--r--openpgp-ffi/include/sequoia/openpgp.h20
-rw-r--r--openpgp-ffi/src/packet_pile.rs52
-rw-r--r--openpgp-ffi/src/tpk.rs42
-rw-r--r--openpgp-ffi/src/tsk.rs3
5 files changed, 79 insertions, 88 deletions
diff --git a/ffi-macros/src/lib.rs b/ffi-macros/src/lib.rs
index 5e815c3f..1215875f 100644
--- a/ffi-macros/src/lib.rs
+++ b/ffi-macros/src/lib.rs
@@ -234,6 +234,7 @@ fn derive_functions() -> &'static HashMap<&'static str, DeriveFn>
h.insert("Hash", derive_hash as DeriveFn);
h.insert("Display", derive_to_string as DeriveFn);
h.insert("Debug", derive_debug as DeriveFn);
+ h.insert("Parse", derive_parse as DeriveFn);
h.insert("Serialize", derive_serialize as DeriveFn);
h
};
@@ -645,6 +646,55 @@ fn derive_hash(span: proc_macro2::Span, prefix: &str, name: &str,
}
}
+/// Derives prefix_name_parse_*.
+fn derive_parse(span: proc_macro2::Span, prefix: &str, name: &str,
+ wrapper: &syn::Ident, wrapped: &syn::Type)
+ -> TokenStream2
+{
+ let from_reader = syn::Ident::new(&format!("{}{}_from_reader",
+ prefix, name),
+ span);
+ let from_file = syn::Ident::new(&format!("{}{}_from_file",
+ prefix, name),
+ span);
+ let from_bytes = syn::Ident::new(&format!("{}{}_from_bytes",
+ prefix, name),
+ span);
+ quote! {
+ /// Parses an object from the given reader.
+ #[::ffi_catch_abort] #[no_mangle] pub extern "system"
+ fn #from_reader(errp: Option<&mut *mut ::error::Error>,
+ reader: *mut Box<::std::io::Read>)
+ -> ::Maybe<#wrapper> {
+ let reader = ffi_param_ref_mut!(reader);
+ #wrapped::from_reader(reader).move_into_raw(errp)
+ }
+
+ /// Parses an object from the given file.
+ #[::ffi_catch_abort] #[no_mangle] pub extern "system"
+ fn #from_file(errp: Option<&mut *mut ::error::Error>,
+ filename: *const ::libc::c_char)
+ -> ::Maybe<#wrapper> {
+ let filename =
+ ffi_param_cstr!(filename).to_string_lossy().into_owned();
+ #wrapped::from_file(&filename).move_into_raw(errp)
+ }
+
+ /// Parses an object from the given buffer.
+ #[::ffi_catch_abort] #[no_mangle] pub extern "system"
+ fn #from_bytes(errp: Option<&mut *mut ::error::Error>,
+ b: *const ::libc::uint8_t, len: ::libc::size_t)
+ -> ::Maybe<#wrapper> {
+ assert!(!b.is_null());
+ let buf = unsafe {
+ ::std::slice::from_raw_parts(b, len as usize)
+ };
+
+ #wrapped::from_bytes(buf).move_into_raw(errp)
+ }
+ }
+}
+
/// Derives prefix_name_serialize.
fn derive_serialize(span: proc_macro2::Span, prefix: &str, name: &str,
wrapper: &syn::Ident, _wrapped: &syn::Type)
diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h
index 1fc06a55..05d43f2d 100644
--- a/openpgp-ffi/include/sequoia/openpgp.h
+++ b/openpgp-ffi/include/sequoia/openpgp.h
@@ -1084,6 +1084,26 @@ pgp_status_t pgp_tsk_new (pgp_error_t *errp, char *primary_uid,
pgp_tsk_t *tpk, pgp_signature_t *revocation);
/*/
+/// Returns the first TSK encountered in the reader.
+/*/
+pgp_tsk_t pgp_tsk_from_reader (pgp_error_t *errp,
+ pgp_reader_t reader);
+
+/*/
+/// Returns the first TSK encountered in the file.
+/*/
+pgp_tsk_t pgp_tsk_from_file (pgp_error_t *errp,
+ const char *filename);
+
+/*/
+/// Returns the first TSK found in `buf`.
+///
+/// `buf` must be an OpenPGP-encoded TSK.
+/*/
+pgp_tsk_t pgp_tsk_from_bytes (pgp_error_t *errp,
+ const uint8_t *b, size_t len);
+
+/*/
/// Frees the TSK.
/*/
void pgp_tsk_free (pgp_tsk_t tsk);
diff --git a/openpgp-ffi/src/packet_pile.rs b/openpgp-ffi/src/packet_pile.rs
index e1aa97fe..6691bd17 100644
--- a/openpgp-ffi/src/packet_pile.rs
+++ b/openpgp-ffi/src/packet_pile.rs
@@ -5,9 +5,7 @@
//!
//! [`sequoia-openpgp::PacketPile`]: ../../sequoia_openpgp/struct.PacketPile.html
-use std::slice;
-use std::io::{Read, Write};
-use libc::{uint8_t, c_char, size_t};
+use std::io::Write;
extern crate sequoia_openpgp as openpgp;
use self::openpgp::{
@@ -15,7 +13,6 @@ use self::openpgp::{
serialize::Serialize,
};
-use Maybe;
use ::error::Status;
/// A `PacketPile` holds a deserialized sequence of OpenPGP messages.
@@ -24,50 +21,5 @@ use ::error::Status;
///
/// [`sequoia-openpgp::PacketPile`]: ../../sequoia_openpgp/struct.PacketPile.html
#[::ffi_wrapper_type(prefix = "pgp_",
- derive = "Clone, Debug, PartialEq, Serialize")]
+ derive = "Clone, Debug, PartialEq, Parse, Serialize")]
pub struct PacketPile(openpgp::PacketPile);
-
-/// 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 pgp_packet_pile_from_reader(errp: Option<&mut *mut ::error::Error>,
- reader: *mut Box<Read>)
- -> Maybe<PacketPile> {
- let reader = ffi_param_ref_mut!(reader);
- openpgp::PacketPile::from_reader(reader).move_into_raw(errp)
-}
-
-/// Deserializes the OpenPGP message stored in the file named by
-/// `filename`.
-///
-/// See `pgp_packet_pile_from_reader` for more details and caveats.
-#[::ffi_catch_abort] #[no_mangle] pub extern "system"
-fn pgp_packet_pile_from_file(errp: Option<&mut *mut ::error::Error>,
- filename: *const c_char)
- -> Maybe<PacketPile> {
- let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
- openpgp::PacketPile::from_file(&filename).move_into_raw(errp)
-}
-
-/// Deserializes the OpenPGP message stored in the provided buffer.
-///
-/// See `pgp_packet_pile_from_reader` for more details and caveats.
-#[::ffi_catch_abort] #[no_mangle] pub extern "system"
-fn pgp_packet_pile_from_bytes(errp: Option<&mut *mut ::error::Error>,
- b: *const uint8_t, len: size_t)
- -> Maybe<PacketPile> {
- assert!(!b.is_null());
- let buf = unsafe {
- slice::from_raw_parts(b, len as usize)
- };
-
- openpgp::PacketPile::from_bytes(buf).move_into_raw(errp)
-}
diff --git a/openpgp-ffi/src/tpk.rs b/openpgp-ffi/src/tpk.rs
index a17def26..1ddf9b8f 100644
--- a/openpgp-ffi/src/tpk.rs
+++ b/openpgp-ffi/src/tpk.rs
@@ -7,8 +7,8 @@
use std::ptr;
use std::slice;
-use std::io::{Read, Write};
-use libc::{uint8_t, c_char, c_int, size_t, time_t};
+use std::io::Write;
+use libc::{c_char, c_int, size_t, time_t};
extern crate sequoia_openpgp as openpgp;
use self::openpgp::{
@@ -52,28 +52,11 @@ use Maybe;
/// passed through as is.
///
/// [RFC 4880, section 11.1]: https://tools.ietf.org/html/rfc4880#section-11.1
-#[::ffi_wrapper_type(prefix = "pgp_", name = "tpk",
- derive = "Clone, Debug, Display, PartialEq, Serialize")]
+#[::ffi_wrapper_type(
+ prefix = "pgp_", name = "tpk",
+ derive = "Clone, Debug, Display, PartialEq, Parse, Serialize")]
pub struct TPK(openpgp::TPK);
-/// Returns the first TPK encountered in the reader.
-#[::ffi_catch_abort] #[no_mangle] pub extern "system"
-fn pgp_tpk_from_reader(errp: Option<&mut *mut ::error::Error>,
- reader: *mut Box<Read>)
- -> Maybe<TPK> {
- let reader = ffi_param_ref_mut!(reader);
- openpgp::TPK::from_reader(reader).move_into_raw(errp)
-}
-
-/// Returns the first TPK encountered in the file.
-#[::ffi_catch_abort] #[no_mangle] pub extern "system"
-fn pgp_tpk_from_file(errp: Option<&mut *mut ::error::Error>,
- filename: *const c_char)
- -> Maybe<TPK> {
- let filename = ffi_param_cstr!(filename).to_string_lossy().into_owned();
- openpgp::TPK::from_file(&filename).move_into_raw(errp)
-}
-
/// Returns the first TPK found in `m`.
///
/// Consumes `m`.
@@ -84,21 +67,6 @@ fn pgp_tpk_from_packet_pile(errp: Option<&mut *mut ::error::Error>,
openpgp::TPK::from_packet_pile(m.move_from_raw()).move_into_raw(errp)
}
-/// Returns the first TPK found in `buf`.
-///
-/// `buf` must be an OpenPGP-encoded TPK.
-#[::ffi_catch_abort] #[no_mangle] pub extern "system"
-fn pgp_tpk_from_bytes(errp: Option<&mut *mut ::error::Error>,
- b: *const uint8_t, len: size_t)
- -> Maybe<TPK> {
- assert!(!b.is_null());
- let buf = unsafe {
- slice::from_raw_parts(b, len as usize)
- };
-
- openpgp::TPK::from_bytes(buf).move_into_raw(errp)
-}
-
/// Returns the first TPK found in the packet parser.
///
/// Consumes the packet parser result.
diff --git a/openpgp-ffi/src/tsk.rs b/openpgp-ffi/src/tsk.rs
index 2f77645e..ea17ade6 100644
--- a/openpgp-ffi/src/tsk.rs
+++ b/openpgp-ffi/src/tsk.rs
@@ -11,6 +11,7 @@ use libc::c_char;
extern crate sequoia_openpgp as openpgp;
use self::openpgp::{
packet::Signature,
+ parse::Parse,
serialize::Serialize,
};
@@ -28,7 +29,7 @@ use ::error::Status;
///
/// [`sequoia-openpgp::TSK`]: ../../sequoia_openpgp/enum.TSK.html
#[::ffi_wrapper_type(prefix = "pgp_", name = "tsk",
- derive = "Clone, Debug, PartialEq, Serialize")]
+ derive = "Clone, Debug, PartialEq, Parse, Serialize")]
pub struct TSK(openpgp::TSK);
/// Generates a new RSA 3072 bit key with UID `primary_uid`.