diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-01-23 17:27:58 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-01-25 14:04:26 +0100 |
commit | 43a64c0d3b5169d9ef3852b7bcbc3bc673a934d4 (patch) | |
tree | 04c81e135247d7b3e0c7cf4b2c55260887bbe42e /openpgp-ffi | |
parent | 228b08f5bf336d8e0b679b4713ca0eaae121d59f (diff) |
openpgp-ffi: Convert Fingerprint.
Diffstat (limited to 'openpgp-ffi')
-rw-r--r-- | openpgp-ffi/src/fingerprint.rs | 50 | ||||
-rw-r--r-- | openpgp-ffi/src/tpk.rs | 5 |
2 files changed, 26 insertions, 29 deletions
diff --git a/openpgp-ffi/src/fingerprint.rs b/openpgp-ffi/src/fingerprint.rs index 8606b478..f75e8235 100644 --- a/openpgp-ffi/src/fingerprint.rs +++ b/openpgp-ffi/src/fingerprint.rs @@ -10,12 +10,12 @@ //! //! [`sequoia-openpgp::Fingerprint`]: ../../sequoia_openpgp/enum.Fingerprint.html -use std::ptr; use std::slice; use libc::{uint8_t, c_char, size_t}; extern crate sequoia_openpgp as openpgp; use self::openpgp::KeyID; +use Maybe; /// Holds a fingerprint. /// @@ -33,15 +33,15 @@ use self::openpgp::KeyID; 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 openpgp::Fingerprint { +#[::ffi_catch_abort] #[no_mangle] pub extern "system" +fn pgp_fingerprint_from_bytes(buf: *const uint8_t, + len: size_t) + -> *mut openpgp::Fingerprint { assert!(!buf.is_null()); let buf = unsafe { slice::from_raw_parts(buf, len as usize) }; - Box::into_raw(Box::new(openpgp::Fingerprint::from_bytes(buf))) + openpgp::Fingerprint::from_bytes(buf).move_into_raw() } /// Reads a hexadecimal fingerprint. @@ -64,24 +64,22 @@ pub extern "system" fn pgp_fingerprint_from_bytes(buf: *const uint8_t, /// free (pretty); /// pgp_fingerprint_free (fp); /// ``` -#[::ffi_catch_abort] #[no_mangle] -pub extern "system" fn pgp_fingerprint_from_hex(hex: *const c_char) - -> *mut openpgp::Fingerprint { +#[::ffi_catch_abort] #[no_mangle] pub extern "system" +fn pgp_fingerprint_from_hex(hex: *const c_char) + -> Maybe<openpgp::Fingerprint> { let hex = ffi_param_cstr!(hex).to_string_lossy(); - openpgp::Fingerprint::from_hex(&hex) - .map(|fp| Box::into_raw(Box::new(fp))) - .unwrap_or(ptr::null_mut()) + openpgp::Fingerprint::from_hex(&hex).ok().move_into_raw() } /// 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 openpgp::Fingerprint, - fp_len: Option<&mut size_t>) - -> *const uint8_t { - let fp = ffi_param_ref!(fp); +#[::ffi_catch_abort] #[no_mangle] pub extern "system" +fn pgp_fingerprint_as_bytes(fp: *const openpgp::Fingerprint, + fp_len: Option<&mut size_t>) + -> *const uint8_t { + let fp = fp.ref_raw(); if let Some(p) = fp_len { *p = fp.as_slice().len(); } @@ -89,17 +87,15 @@ pub extern "system" fn pgp_fingerprint_as_bytes(fp: *const openpgp::Fingerprint, } /// Converts the fingerprint to a hexadecimal number. -#[::ffi_catch_abort] #[no_mangle] -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()) +#[::ffi_catch_abort] #[no_mangle] pub extern "system" +fn pgp_fingerprint_to_hex(fp: *const openpgp::Fingerprint) + -> *mut c_char { + ffi_return_string!(fp.ref_raw().to_hex()) } /// Converts the fingerprint to a key ID. -#[::ffi_catch_abort] #[no_mangle] -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())) +#[::ffi_catch_abort] #[no_mangle] pub extern "system" +fn pgp_fingerprint_to_keyid(fp: *const openpgp::Fingerprint) + -> *mut KeyID { + fp.ref_raw().to_keyid().move_into_raw() } diff --git a/openpgp-ffi/src/tpk.rs b/openpgp-ffi/src/tpk.rs index 62f3f855..a6fcd2c0 100644 --- a/openpgp-ffi/src/tpk.rs +++ b/openpgp-ffi/src/tpk.rs @@ -12,7 +12,6 @@ use libc::{uint8_t, c_char, c_int, size_t, time_t}; extern crate sequoia_openpgp as openpgp; use self::openpgp::{ - Fingerprint, Packet, PacketPile, RevocationStatus, @@ -34,6 +33,8 @@ use self::openpgp::{ }; use ::error::Status; +use super::fingerprint::Fingerprint; +use Maybe; /// A transferable public key (TPK). /// @@ -174,7 +175,7 @@ pub extern "system" fn pgp_tpk_merge_packets(errp: Option<&mut *mut failure::Err pub extern "system" fn pgp_tpk_fingerprint(tpk: *const openpgp::TPK) -> *mut Fingerprint { let tpk = ffi_param_ref!(tpk); - box_raw!(tpk.fingerprint()) + tpk.fingerprint().move_into_raw() } /// Cast the public key into a secret key that allows using the secret |