From ebb0d43ba773ec13e47d74b7baf035aaf7f0fb17 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 22 Jan 2019 13:23:54 +0100 Subject: openpgp-ffi: Wrap Fingerprint. --- openpgp-ffi/include/sequoia/openpgp.h | 6 +++ openpgp-ffi/src/fingerprint.rs | 78 +++++++++++------------------------ openpgp-ffi/src/lib.rs | 9 ++-- 3 files changed, 36 insertions(+), 57 deletions(-) (limited to 'openpgp-ffi') diff --git a/openpgp-ffi/include/sequoia/openpgp.h b/openpgp-ffi/include/sequoia/openpgp.h index 7289e021..0756e6b6 100644 --- a/openpgp-ffi/include/sequoia/openpgp.h +++ b/openpgp-ffi/include/sequoia/openpgp.h @@ -135,6 +135,12 @@ uint8_t *pgp_fingerprint_as_bytes (const pgp_fingerprint_t fp, size_t *fp_len); /*/ char *pgp_fingerprint_to_string (const pgp_fingerprint_t fp); +/*/ +/// Returns a human readable description of this object suitable for +/// debugging. +/*/ +char *pgp_fingerprint_debug (const pgp_fingerprint_t fp); + /*/ /// Converts the fingerprint to a hexadecimal number. /*/ diff --git a/openpgp-ffi/src/fingerprint.rs b/openpgp-ffi/src/fingerprint.rs index f53b2dbc..8606b478 100644 --- a/openpgp-ffi/src/fingerprint.rs +++ b/openpgp-ffi/src/fingerprint.rs @@ -10,26 +10,38 @@ //! //! [`sequoia-openpgp::Fingerprint`]: ../../sequoia_openpgp/enum.Fingerprint.html -use std::hash::{Hash, Hasher}; use std::ptr; use std::slice; -use libc::{uint8_t, uint64_t, c_char, size_t}; +use libc::{uint8_t, c_char, size_t}; -extern crate sequoia_openpgp; -use self::sequoia_openpgp::{Fingerprint, KeyID}; +extern crate sequoia_openpgp as openpgp; +use self::openpgp::KeyID; -use build_hasher; +/// Holds a fingerprint. +/// +/// A fingerprint uniquely identifies a public key. For more details +/// about how a fingerprint is generated, see [Section 12.2 of RFC +/// 4880]. +/// +/// [Section 12.2 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-12.2 +/// +/// Wraps [`sequoia-openpgp::Fingerprint`]. +/// +/// [`sequoia-openpgp::Fingerprint`]: ../../sequoia_openpgp/enum.Fingerprint.html +#[::ffi_wrapper_type(prefix = "pgp_", + derive = "Clone, Debug, Display, Hash, PartialEq")] +pub struct Fingerprint(openpgp::Fingerprint); /// Reads a binary fingerprint. #[::ffi_catch_abort] #[no_mangle] pub extern "system" fn pgp_fingerprint_from_bytes(buf: *const uint8_t, len: size_t) - -> *mut Fingerprint { + -> *mut openpgp::Fingerprint { assert!(!buf.is_null()); let buf = unsafe { slice::from_raw_parts(buf, len as usize) }; - Box::into_raw(Box::new(Fingerprint::from_bytes(buf))) + Box::into_raw(Box::new(openpgp::Fingerprint::from_bytes(buf))) } /// Reads a hexadecimal fingerprint. @@ -54,43 +66,19 @@ pub extern "system" fn pgp_fingerprint_from_bytes(buf: *const uint8_t, /// ``` #[::ffi_catch_abort] #[no_mangle] pub extern "system" fn pgp_fingerprint_from_hex(hex: *const c_char) - -> *mut Fingerprint { + -> *mut openpgp::Fingerprint { let hex = ffi_param_cstr!(hex).to_string_lossy(); - Fingerprint::from_hex(&hex) + openpgp::Fingerprint::from_hex(&hex) .map(|fp| Box::into_raw(Box::new(fp))) .unwrap_or(ptr::null_mut()) } -/// Frees a pgp_fingerprint_t. -#[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_free(fp: Option<&mut Fingerprint>) { - ffi_free!(fp) -} - -/// Clones the Fingerprint. -#[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_clone(fp: *const Fingerprint) - -> *mut Fingerprint { - let fp = ffi_param_ref!(fp); - box_raw!(fp.clone()) -} - -/// Hashes the Fingerprint. -#[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_hash(fp: *const Fingerprint) - -> uint64_t { - let fp = ffi_param_ref!(fp); - let mut hasher = build_hasher(); - fp.hash(&mut hasher); - hasher.finish() -} - /// Returns a reference to the raw Fingerprint. /// /// This returns a reference to the internal buffer that is valid as /// long as the fingerprint is. #[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_as_bytes(fp: *const Fingerprint, +pub extern "system" fn pgp_fingerprint_as_bytes(fp: *const openpgp::Fingerprint, fp_len: Option<&mut size_t>) -> *const uint8_t { let fp = ffi_param_ref!(fp); @@ -100,17 +88,9 @@ pub extern "system" fn pgp_fingerprint_as_bytes(fp: *const Fingerprint, fp.as_slice().as_ptr() } -/// Converts the fingerprint to its standard representation. -#[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_to_string(fp: *const Fingerprint) - -> *mut c_char { - let fp = ffi_param_ref!(fp); - ffi_return_string!(fp.to_string()) -} - /// Converts the fingerprint to a hexadecimal number. #[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_to_hex(fp: *const Fingerprint) +pub extern "system" fn pgp_fingerprint_to_hex(fp: *const openpgp::Fingerprint) -> *mut c_char { let fp = ffi_param_ref!(fp); ffi_return_string!(fp.to_hex()) @@ -118,18 +98,8 @@ pub extern "system" fn pgp_fingerprint_to_hex(fp: *const Fingerprint) /// Converts the fingerprint to a key ID. #[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_to_keyid(fp: *const Fingerprint) +pub extern "system" fn pgp_fingerprint_to_keyid(fp: *const openpgp::Fingerprint) -> *mut KeyID { let fp = ffi_param_ref!(fp); Box::into_raw(Box::new(fp.to_keyid())) } - -/// Compares Fingerprints. -#[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_equal(a: *const Fingerprint, - b: *const Fingerprint) - -> bool { - let a = ffi_param_ref!(a); - let b = ffi_param_ref!(b); - a == b -} diff --git a/openpgp-ffi/src/lib.rs b/openpgp-ffi/src/lib.rs index 92f546f5..3e8be671 100644 --- a/openpgp-ffi/src/lib.rs +++ b/openpgp-ffi/src/lib.rs @@ -134,7 +134,7 @@ //! or the allocation for the string failed, which is considered //! undefined behavior. //! -//! [`pgp_fingerprint_to_string`]: fingerprint/fn.pgp_fingerprint_to_string.html +//! [`pgp_fingerprint_to_string`]: struct.Fingerprint.html#method.pgp_fingerprint_to_string //! //! Failing functions signal failure either in-band (e.g. `NULL`, or //! -1), using `pgp_status_t`, and may store complex error information @@ -145,7 +145,7 @@ //! on the other hand, will return `NULL` and store a complex error at //! the location given using the `errp` parameter. //! -//! [`pgp_fingerprint_from_hex`]: fingerprint/fn.pgp_fingerprint_from_hex.html +//! [`pgp_fingerprint_from_hex`]: struct.Fingerprint.html#method.pgp_fingerprint_from_hex //! [`pgp_packet_parser_from_bytes`]: parse/fn.pgp_packet_parser_from_bytes.html //! //! Errors may be inspected using [`pgp_error_status`], and formatted @@ -316,6 +316,9 @@ extern crate lazy_static; extern crate libc; extern crate sequoia_ffi_macros; -use sequoia_ffi_macros::ffi_catch_abort; +use sequoia_ffi_macros::{ + ffi_catch_abort, + ffi_wrapper_type, +}; include!("common.rs"); -- cgit v1.2.3