mirror of
https://github.com/signalapp/libsignal.git
synced 2026-04-25 17:25:18 +02:00
ffi: Simplify hex_encode helper
Ever so slightly less efficient in how it will check the arguments, but also reuses existing conversion logic.
This commit is contained in:
@@ -8,7 +8,9 @@
|
||||
|
||||
use std::ffi::{c_char, c_uchar, CString};
|
||||
|
||||
use libsignal_bridge::ffi::*;
|
||||
use libsignal_bridge::ffi::{self, *};
|
||||
use libsignal_bridge::ffi_arg_type;
|
||||
use libsignal_bridge_macros::bridge_fn;
|
||||
#[cfg(feature = "libsignal-bridge-testing")]
|
||||
#[allow(unused_imports)]
|
||||
use libsignal_bridge_testing::*;
|
||||
@@ -99,33 +101,8 @@ pub unsafe extern "C" fn signal_identitykeypair_deserialize(
|
||||
})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn signal_hex_encode(
|
||||
output: *mut c_char,
|
||||
output_len: usize,
|
||||
input: *const u8,
|
||||
input_len: usize,
|
||||
) -> *mut SignalFfiError {
|
||||
run_ffi_safe(|| {
|
||||
if input_len == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
if input_len > output_len / 2 {
|
||||
// We check this early because an output buffer of {NULL, 0} is *valid*, just too small
|
||||
// for anything but a zero-length input, while std::slice::from_raw_parts_mut requires a
|
||||
// non-null base pointer.
|
||||
return Err(SignalProtocolError::InvalidArgument(
|
||||
"output buffer too small".to_string(),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
if input.is_null() || output.is_null() {
|
||||
return Err(NullPointerError.into());
|
||||
}
|
||||
let output = std::slice::from_raw_parts_mut(output, output_len);
|
||||
let output = zerocopy::IntoBytes::as_mut_bytes(output);
|
||||
let input = std::slice::from_raw_parts(input, input_len);
|
||||
hex::encode_to_slice(input, output).expect("checked above");
|
||||
Ok(())
|
||||
})
|
||||
#[bridge_fn(jni = false, node = false)]
|
||||
fn hex_encode(output: &mut [u8], input: &[u8]) -> Result<(), SignalProtocolError> {
|
||||
hex::encode_to_slice(input, output)
|
||||
.map_err(|_| SignalProtocolError::InvalidArgument("output buffer too small".to_string()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user