From 3c55eaadbc4a1ced2c955b1794c9299006e5a673 Mon Sep 17 00:00:00 2001 From: Alex Page Date: Thu, 22 Jun 2023 01:49:34 -0400 Subject: [PATCH] Move converted code to its own submodule --- src/{confid.rs => confid/black_box.rs} | 47 ++------------------------ src/confid/mod.rs | 43 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 45 deletions(-) rename src/{confid.rs => confid/black_box.rs} (96%) create mode 100644 src/confid/mod.rs diff --git a/src/confid.rs b/src/confid/black_box.rs similarity index 96% rename from src/confid.rs rename to src/confid/black_box.rs index 9d66035..1839691 100644 --- a/src/confid.rs +++ b/src/confid/black_box.rs @@ -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 { - 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()) - } -} diff --git a/src/confid/mod.rs b/src/confid/mod.rs new file mode 100644 index 0000000..0bc1e3c --- /dev/null +++ b/src/confid/mod.rs @@ -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 { + 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()) + } +}