|
|
@ -24,26 +24,7 @@ mod tests {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn ffi() -> Result<(), Box<dyn std::error::Error>>
|
|
|
|
fn ffi() -> Result<(), Box<dyn std::error::Error>>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
|
|
|
|
unsafe fn work_with_salt(salt: salt::Salt) -> Result<String, error::Error> {
|
|
|
|
|
|
|
|
let input = b"lolis are super ultra mega cute";
|
|
|
|
|
|
|
|
let salt = salt::into_raw(salt);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Simulate ffi call;
|
|
|
|
|
|
|
|
let mut sz: libc::size_t = 0;
|
|
|
|
|
|
|
|
assert_eq!(GENERIC_SUCCESS, _kana_length(&input[0] as *const u8 as *const libc::c_void, input.len(), salt as *const libc::c_void, &mut sz as *mut size_t));
|
|
|
|
|
|
|
|
assert_eq!(sz, 24);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut output = malloc_array::heap![unsafe u8; sz];
|
|
|
|
|
|
|
|
assert_eq!(GENERIC_SUCCESS, _kana_do(&input[0] as *const u8 as *const libc::c_void, input.len(), salt as *mut libc::c_void, output.as_ptr_mut() as *mut libc::c_char, sz));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let string = std::str::from_utf8(&output[..]).unwrap();
|
|
|
|
|
|
|
|
Ok(string.to_owned())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!("けほほぇほょすゆ", work_with_salt(salt::Salt::None)?);
|
|
|
|
|
|
|
|
assert_eq!("ワイトひはっトと", work_with_salt(salt::Salt::default())?);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -113,12 +94,12 @@ use malloc_array::{
|
|
|
|
/// # Note
|
|
|
|
/// # Note
|
|
|
|
/// Does not consume `salt`
|
|
|
|
/// Does not consume `salt`
|
|
|
|
#[no_mangle]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn _kana_length(bin: *const c_void, sz: size_t, salt: *const c_void, out_len: *mut size_t) -> i32
|
|
|
|
pub unsafe extern "C" fn khash_length(bin: *const c_void, sz: size_t, salt: *const salt::FFI, out_len: *mut size_t) -> i32
|
|
|
|
{
|
|
|
|
{
|
|
|
|
no_unwind!{
|
|
|
|
no_unwind!{
|
|
|
|
try error::Error::Unknown;
|
|
|
|
try error::Error::Unknown;
|
|
|
|
let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
|
|
|
|
let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
|
|
|
|
let string = c_try!(generate(&bin, salt::clone_from_raw(salt as *const salt::FFI)));
|
|
|
|
let string = c_try!(generate(&bin, salt::clone_from_raw(salt)));
|
|
|
|
*out_len = string.bytes().len().into();
|
|
|
|
*out_len = string.bytes().len().into();
|
|
|
|
|
|
|
|
|
|
|
|
GENERIC_SUCCESS
|
|
|
|
GENERIC_SUCCESS
|
|
|
@ -130,12 +111,12 @@ pub unsafe extern "C" fn _kana_length(bin: *const c_void, sz: size_t, salt: *con
|
|
|
|
/// # Note
|
|
|
|
/// # Note
|
|
|
|
/// Consumes `salt`
|
|
|
|
/// Consumes `salt`
|
|
|
|
#[no_mangle]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn _kana_do(bin: *const c_void, sz: size_t, salt: *mut c_void, out_str: *mut c_char, str_len: size_t) -> i32
|
|
|
|
pub unsafe extern "C" fn khash_do(bin: *const c_void, sz: size_t, salt: *mut salt::FFI, out_str: *mut c_char, str_len: size_t) -> i32
|
|
|
|
{
|
|
|
|
{
|
|
|
|
no_unwind!{
|
|
|
|
no_unwind!{
|
|
|
|
try error::Error::Unknown;
|
|
|
|
try error::Error::Unknown;
|
|
|
|
let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
|
|
|
|
let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
|
|
|
|
let string: Vec<u8> = c_try!(generate(&bin, salt::from_raw(salt as *mut salt::FFI))).bytes().collect();
|
|
|
|
let string: Vec<u8> = c_try!(generate(&bin, salt::from_raw(salt))).bytes().collect();
|
|
|
|
|
|
|
|
|
|
|
|
libc::memcpy(out_str as *mut c_void, &string[0] as *const u8 as *const c_void, std::cmp::min(str_len, string.len()));
|
|
|
|
libc::memcpy(out_str as *mut c_void, &string[0] as *const u8 as *const c_void, std::cmp::min(str_len, string.len()));
|
|
|
|
|
|
|
|
|
|
|
@ -143,32 +124,40 @@ pub unsafe extern "C" fn _kana_do(bin: *const c_void, sz: size_t, salt: *mut c_v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Free a salt allocated with `_kana_new_salt`
|
|
|
|
/// Free a salt allocated with `khash_new_salt`
|
|
|
|
#[no_mangle]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn _kana_free_salt(salt: *mut c_void) -> i32
|
|
|
|
pub unsafe extern "C" fn khash_free_salt(salt: *mut salt::FFI) -> i32
|
|
|
|
{
|
|
|
|
{
|
|
|
|
no_unwind!{
|
|
|
|
no_unwind!{
|
|
|
|
drop(salt::from_raw(salt as *mut salt::FFI));
|
|
|
|
drop(salt::from_raw(salt));
|
|
|
|
GENERIC_SUCCESS
|
|
|
|
GENERIC_SUCCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new salt
|
|
|
|
/// Create a new salt
|
|
|
|
#[no_mangle]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn _kana_new_salt(bin: *const c_void, sz: size_t, nptr: *mut *const c_void) -> i32
|
|
|
|
pub unsafe extern "C" fn khash_new_salt(salt_type: u8, bin: *const c_void, sz: size_t, nptr: *mut salt::FFI) -> i32
|
|
|
|
{
|
|
|
|
{
|
|
|
|
no_unwind!{
|
|
|
|
no_unwind!{
|
|
|
|
try error::Error::Unknown;
|
|
|
|
try error::Error::Unknown;
|
|
|
|
let nptr = nptr as *mut *const salt::FFI;
|
|
|
|
match salt_type {
|
|
|
|
if bin.is_null() {
|
|
|
|
salt::SALT_TYPE_SPECIFIC => {
|
|
|
|
*nptr = salt::into_raw(salt::Salt::default());
|
|
|
|
let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
|
|
|
|
} else if sz == 0 {
|
|
|
|
*nptr = salt::into_raw(salt::Salt::unfixed(&bin[..]));
|
|
|
|
*nptr = salt::into_raw(salt::Salt::None);
|
|
|
|
},
|
|
|
|
} else {
|
|
|
|
salt::SALT_TYPE_DEFAULT => {
|
|
|
|
let bin = HeapArray::<u8>::from_raw_copied(bin as *const u8, usize::from(sz));
|
|
|
|
*nptr = salt::into_raw(salt::Salt::default());
|
|
|
|
*nptr = salt::into_raw(salt::Salt::unfixed(&bin[..]));
|
|
|
|
},
|
|
|
|
|
|
|
|
salt::SALT_TYPE_RANDOM => {
|
|
|
|
|
|
|
|
*nptr = salt::into_raw(match salt::Salt::random() {
|
|
|
|
|
|
|
|
Ok(v) => v,
|
|
|
|
|
|
|
|
Err(e) => return i32::from(error::Error::RNG(e)),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
|
|
*nptr = salt::into_raw(salt::Salt::None);
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GENERIC_SUCCESS
|
|
|
|
GENERIC_SUCCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|