Compare commits

...

12 commits

2 changed files with 276 additions and 462 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,3 @@
use std::ffi::{CStr, CString};
use thiserror::Error;
mod black_box;
@ -27,9 +25,9 @@ pub fn generate(installation_id: &str) -> Result<String, ConfirmationIdError> {
if installation_id.len() > 54 {
return Err(ConfirmationIdError::TooLarge);
}
let inst_id = CString::new(installation_id).unwrap();
let conf_id = [0u8; 49];
let result = unsafe { black_box::generate(inst_id.as_ptr(), conf_id.as_ptr() as *mut i8) };
let inst_id = installation_id.as_bytes();
let mut conf_id = [0u8; 48];
let result = black_box::generate(inst_id, &mut conf_id);
match result {
0 => {}
1 => return Err(ConfirmationIdError::TooShort),
@ -40,12 +38,7 @@ pub fn generate(installation_id: &str) -> Result<String, ConfirmationIdError> {
6 => return Err(ConfirmationIdError::Unlucky),
_ => panic!("Unknown error code: {}", result),
}
unsafe {
Ok(CStr::from_ptr(conf_id.as_ptr() as *const i8)
.to_str()
.unwrap()
.to_string())
}
Ok(String::from_utf8_lossy(&conf_id).into())
}
#[cfg(test)]