summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-01-22 13:23:54 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-22 17:47:45 +0100
commitebb0d43ba773ec13e47d74b7baf035aaf7f0fb17 (patch)
tree2c7f55f52486c9e5ca9cb51663d9d3301d810abc /openpgp-ffi
parent20e89f09aa9cae3dd56ffff1b45d88ddbf3d0acd (diff)
openpgp-ffi: Wrap Fingerprint.
Diffstat (limited to 'openpgp-ffi')
-rw-r--r--openpgp-ffi/include/sequoia/openpgp.h6
-rw-r--r--openpgp-ffi/src/fingerprint.rs78
-rw-r--r--openpgp-ffi/src/lib.rs9
3 files changed, 36 insertions, 57 deletions
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
@@ -136,6 +136,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.
/*/
char *pgp_fingerprint_to_hex (const pgp_fingerprint_t fp);
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");