Move converted code to its own submodule
This commit is contained in:
parent
6d27106e35
commit
3c55eaadbc
2 changed files with 45 additions and 45 deletions
|
@ -5,12 +5,7 @@
|
|||
clippy::too_many_arguments
|
||||
)]
|
||||
|
||||
use std::{
|
||||
ffi::{c_void, CStr, CString},
|
||||
ptr,
|
||||
};
|
||||
|
||||
use thiserror::Error;
|
||||
use std::{ffi::c_void, ptr};
|
||||
|
||||
type size_t = u64;
|
||||
#[derive(Copy, Clone)]
|
||||
|
@ -1030,7 +1025,7 @@ unsafe fn Unmix(buffer: *mut u8, bufSize: size_t, key: *const u8, keySize: size_
|
|||
}
|
||||
}
|
||||
|
||||
unsafe fn Generate(installation_id_str: *const i8, confirmation_id: *mut i8) -> i32 {
|
||||
pub unsafe fn Generate(installation_id_str: *const i8, confirmation_id: *mut i8) -> i32 {
|
||||
let mut installation_id: [u8; 19] = [0; 19]; // 10**45 < 256**19
|
||||
let mut installation_id_len: size_t = 0_i32 as size_t;
|
||||
let mut p: *const i8 = installation_id_str;
|
||||
|
@ -1324,41 +1319,3 @@ unsafe fn Generate(installation_id_str: *const i8, confirmation_id: *mut i8) ->
|
|||
*fresh3 = 0_i32 as i8;
|
||||
0_i32
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ConfirmationIdError {
|
||||
#[error("Installation ID is too short.")]
|
||||
TooShort,
|
||||
#[error("Installation ID is too long.")]
|
||||
TooLarge,
|
||||
#[error("Invalid character in installation ID.")]
|
||||
InvalidCharacter,
|
||||
#[error("Installation ID checksum failed. Please check that it is typed correctly.")]
|
||||
InvalidCheckDigit,
|
||||
#[error("Unknown installation ID version.")]
|
||||
UnknownVersion,
|
||||
#[error("Unable to generate valid confirmation ID.")]
|
||||
Unlucky,
|
||||
}
|
||||
|
||||
pub fn generate(installation_id: &str) -> Result<String, ConfirmationIdError> {
|
||||
let inst_id = CString::new(installation_id).unwrap();
|
||||
let conf_id = [0u8; 49];
|
||||
let result = unsafe { Generate(inst_id.as_ptr(), conf_id.as_ptr() as *mut i8) };
|
||||
match result {
|
||||
0 => {}
|
||||
1 => return Err(ConfirmationIdError::TooShort),
|
||||
2 => return Err(ConfirmationIdError::TooLarge),
|
||||
3 => return Err(ConfirmationIdError::InvalidCharacter),
|
||||
4 => return Err(ConfirmationIdError::InvalidCheckDigit),
|
||||
5 => return Err(ConfirmationIdError::UnknownVersion),
|
||||
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())
|
||||
}
|
||||
}
|
43
src/confid/mod.rs
Normal file
43
src/confid/mod.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
use std::ffi::{CStr, CString};
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
mod black_box;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ConfirmationIdError {
|
||||
#[error("Installation ID is too short.")]
|
||||
TooShort,
|
||||
#[error("Installation ID is too long.")]
|
||||
TooLarge,
|
||||
#[error("Invalid character in installation ID.")]
|
||||
InvalidCharacter,
|
||||
#[error("Installation ID checksum failed. Please check that it is typed correctly.")]
|
||||
InvalidCheckDigit,
|
||||
#[error("Unknown installation ID version.")]
|
||||
UnknownVersion,
|
||||
#[error("Unable to generate valid confirmation ID.")]
|
||||
Unlucky,
|
||||
}
|
||||
|
||||
pub fn generate(installation_id: &str) -> Result<String, ConfirmationIdError> {
|
||||
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) };
|
||||
match result {
|
||||
0 => {}
|
||||
1 => return Err(ConfirmationIdError::TooShort),
|
||||
2 => return Err(ConfirmationIdError::TooLarge),
|
||||
3 => return Err(ConfirmationIdError::InvalidCharacter),
|
||||
4 => return Err(ConfirmationIdError::InvalidCheckDigit),
|
||||
5 => return Err(ConfirmationIdError::UnknownVersion),
|
||||
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())
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue