From 81359406f6ff14af3df2cdc5d76381c63b0c7af8 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Wed, 20 Sep 2023 12:28:18 -0700 Subject: [PATCH] switch usize to i64 in FFI Signed-off-by: Andrew Whitehead --- indy-credx/src/ffi/credential.rs | 17 +++++------ indy-credx/src/ffi/error.rs | 2 +- indy-credx/src/ffi/mod.rs | 2 +- indy-credx/src/ffi/presentation.rs | 41 +++++++------------------- indy-credx/src/ffi/revocation.rs | 4 +-- indy-credx/src/ffi/util.rs | 23 +++++++-------- wrappers/python/indy_credx/bindings.py | 11 +++---- 7 files changed, 39 insertions(+), 61 deletions(-) diff --git a/indy-credx/src/ffi/credential.rs b/indy-credx/src/ffi/credential.rs index 7b679bc..e4c2353 100644 --- a/indy-credx/src/ffi/credential.rs +++ b/indy-credx/src/ffi/credential.rs @@ -70,13 +70,13 @@ pub extern "C" fn credx_create_credential( "Mismatch between length of attribute names and raw values" )); } - let enc_values = attr_enc_values.as_slice(); + let enc_values = attr_enc_values.as_slice()?; let mut cred_values = MakeCredentialValues::default(); - let mut attr_idx = 0; - for (name, raw) in attr_names - .as_slice() - .into_iter() - .zip(attr_raw_values.as_slice()) + for (attr_idx, (name, raw)) in attr_names + .as_slice()? + .iter() + .zip(attr_raw_values.as_slice()?) + .enumerate() { let name = name .as_opt_str() @@ -96,12 +96,11 @@ pub extern "C" fn credx_create_credential( } else { cred_values.add_raw(name, raw)?; } - attr_idx += 1; } let revocation_config = if !revocation.is_null() { let revocation = unsafe { &*revocation }; let mut reg_used = HashSet::new(); - for reg_idx in revocation.reg_used.as_slice() { + for reg_idx in revocation.reg_used.as_slice()? { reg_used.insert( (*reg_idx) .try_into() @@ -157,7 +156,7 @@ pub extern "C" fn credx_encode_credential_attributes( ) -> ErrorCode { catch_error(|| { let mut result = String::new(); - for raw_val in attr_raw_values.as_slice() { + for raw_val in attr_raw_values.as_slice()? { let enc_val = encode_credential_attribute( raw_val .as_opt_str() diff --git a/indy-credx/src/ffi/error.rs b/indy-credx/src/ffi/error.rs index d586e3f..8eb66ea 100644 --- a/indy-credx/src/ffi/error.rs +++ b/indy-credx/src/ffi/error.rs @@ -11,7 +11,7 @@ use once_cell::sync::Lazy; static LAST_ERROR: Lazy>> = Lazy::new(|| RwLock::new(None)); #[derive(Debug, PartialEq, Copy, Clone, Serialize)] -#[repr(usize)] +#[repr(i64)] pub enum ErrorCode { Success = 0, Input = 1, diff --git a/indy-credx/src/ffi/mod.rs b/indy-credx/src/ffi/mod.rs index b44cabc..535ca19 100644 --- a/indy-credx/src/ffi/mod.rs +++ b/indy-credx/src/ffi/mod.rs @@ -10,7 +10,7 @@ ffi_support::define_string_destructor!(credx_string_free); #[no_mangle] pub extern "C" fn credx_buffer_free(buffer: ByteBuffer) { ffi_support::abort_on_panic::with_abort_on_panic(|| { - drop(buffer.destroy_into_vec().zeroize()); + buffer.destroy_into_vec().zeroize(); }) } diff --git a/indy-credx/src/ffi/presentation.rs b/indy-credx/src/ffi/presentation.rs index c29b246..a2e5c5f 100644 --- a/indy-credx/src/ffi/presentation.rs +++ b/indy-credx/src/ffi/presentation.rs @@ -76,26 +76,16 @@ pub extern "C" fn credx_create_presentation( )); } - let entries = { - let credentials = credentials.as_slice(); - credentials.into_iter().try_fold( - Vec::with_capacity(credentials.len()), - |mut r, ffi_entry| { - r.push(ffi_entry.load()?); - Result::Ok(r) - }, - )? - }; - - let schemas = IndyObjectList::load(schemas.as_slice())?; - let cred_defs = IndyObjectList::load(cred_defs.as_slice())?; + let entries = credentials.try_collect(|entry| entry.load())?; + let schemas = IndyObjectList::load(schemas.as_slice()?)?; + let cred_defs = IndyObjectList::load(cred_defs.as_slice()?)?; let self_attested = if !self_attest_names.is_empty() { let mut self_attested = HashMap::new(); for (name, raw) in self_attest_names - .as_slice() - .into_iter() - .zip(self_attest_values.as_slice()) + .as_slice()? + .iter() + .zip(self_attest_values.as_slice()?) { let name = name .as_opt_str() @@ -125,7 +115,7 @@ pub extern "C" fn credx_create_presentation( .transpose()?, ); - for prove in credentials_prove.as_slice() { + for prove in credentials_prove.as_slice()? { if prove.entry_idx < 0 { return Err(err_msg!("Invalid credential index")); } @@ -239,19 +229,10 @@ fn _credx_verify_presentation( result_p: *mut i8, ) -> ErrorCode { catch_error(|| { - let schemas = IndyObjectList::load(schemas.as_slice())?; - let cred_defs = IndyObjectList::load(cred_defs.as_slice())?; - let rev_reg_defs = IndyObjectList::load(rev_reg_defs.as_slice())?; - let rev_reg_entries = { - let entries = rev_reg_entries.as_slice(); - entries.into_iter().try_fold( - Vec::with_capacity(entries.len()), - |mut r, ffi_entry| { - r.push(ffi_entry.load()?); - Result::Ok(r) - }, - )? - }; + let schemas = IndyObjectList::load(schemas.as_slice()?)?; + let cred_defs = IndyObjectList::load(cred_defs.as_slice()?)?; + let rev_reg_defs = IndyObjectList::load(rev_reg_defs.as_slice()?)?; + let rev_reg_entries = rev_reg_entries.try_collect(|entry| entry.load())?; let mut rev_regs = HashMap::new(); for (idx, entry, timestamp) in rev_reg_entries.iter() { if *idx > rev_reg_defs.len() { diff --git a/indy-credx/src/ffi/revocation.rs b/indy-credx/src/ffi/revocation.rs index 8e710da..65305be 100644 --- a/indy-credx/src/ffi/revocation.rs +++ b/indy-credx/src/ffi/revocation.rs @@ -99,8 +99,8 @@ pub extern "C" fn credx_update_revocation_registry( catch_error(|| { check_useful_c_ptr!(rev_reg_p); check_useful_c_ptr!(rev_reg_delta_p); - let issued = registry_indices_to_set(issued.as_slice().into_iter().cloned())?; - let revoked = registry_indices_to_set(revoked.as_slice().into_iter().cloned())?; + let issued = registry_indices_to_set(issued.as_slice()?.iter().cloned())?; + let revoked = registry_indices_to_set(revoked.as_slice()?.iter().cloned())?; let (rev_reg, rev_reg_delta) = update_revocation_registry( cred_def.load()?.cast_ref()?, rev_reg_def.load()?.cast_ref()?, diff --git a/indy-credx/src/ffi/util.rs b/indy-credx/src/ffi/util.rs index a40ce2a..a4317b2 100644 --- a/indy-credx/src/ffi/util.rs +++ b/indy-credx/src/ffi/util.rs @@ -8,29 +8,26 @@ use crate::error::Result; #[derive(Debug)] #[repr(C)] pub struct FfiList<'a, T> { - count: usize, + count: i64, data: *const T, _pd: PhantomData<&'a ()>, } impl<'a, T> FfiList<'a, T> { #[inline] - pub fn as_slice(&self) -> &[T] { - if self.data.is_null() { - &[] + pub fn as_slice(&self) -> Result<&[T]> { + if self.data.is_null() || self.count == 0 { + Ok(&[]) + } else if self.count < 0 { + return Err(err_msg!(Input, "Invalid index for result set")); } else { - unsafe { slice::from_raw_parts(self.data, self.count) } + Ok(unsafe { slice::from_raw_parts(self.data, self.count as usize) }) } } #[inline] - pub fn try_collect(&self, mut f: impl FnMut(&T) -> Result) -> Result> { - self.as_slice() - .into_iter() - .try_fold(Vec::with_capacity(self.len()), |mut rs, v| { - rs.push(f(v)?); - Ok(rs) - }) + pub fn try_collect(&self, f: impl FnMut(&T) -> Result) -> Result> { + self.as_slice()?.iter().map(f).collect() } #[inline] @@ -39,7 +36,7 @@ impl<'a, T> FfiList<'a, T> { } #[inline] - pub fn len(&self) -> usize { + pub fn len(&self) -> i64 { if self.data.is_null() { 0 } else { diff --git a/wrappers/python/indy_credx/bindings.py b/wrappers/python/indy_credx/bindings.py index e6803bb..a157ee3 100644 --- a/wrappers/python/indy_credx/bindings.py +++ b/wrappers/python/indy_credx/bindings.py @@ -16,7 +16,6 @@ c_char_p, c_int8, c_int64, - c_size_t, c_ubyte, pointer, ) @@ -230,7 +229,7 @@ def _cleanup(cls, buffer: c_char_p): class FfiObjectHandleList(Structure): _fields_ = [ - ("count", c_size_t), + ("count", c_int64), ("data", POINTER(ObjectHandle)), ] @@ -246,7 +245,7 @@ def create(cls, values: Optional[Sequence[ObjectHandle]]) -> "FfiObjectHandleLis class FfiIntList(Structure): _fields_ = [ - ("count", c_size_t), + ("count", c_int64), ("data", POINTER(c_int64)), ] @@ -262,7 +261,7 @@ def create(cls, values: Optional[Sequence[str]]) -> "FfiIntList": class FfiStrList(Structure): _fields_ = [ - ("count", c_size_t), + ("count", c_int64), ("data", POINTER(c_char_p)), ] @@ -796,7 +795,9 @@ def verify_presentation( entry_list.count = len(rev_regs) entry_list.data = (RevocationEntry * entry_list.count)(*rev_regs) do_call( - "credx_verify_presentation_legacy" if accept_legacy_revocation else "credx_verify_presentation", + "credx_verify_presentation_legacy" + if accept_legacy_revocation + else "credx_verify_presentation", presentation, pres_req, FfiObjectHandleList.create(schemas),