ableos_userland/libraries/ari_interface/src/lib.rs

80 lines
2.5 KiB
Rust

use log::trace;
// ARI is a set of programs to manage an ableOS remote install.
// In the case of this being enabled ableOS will require a dedicated Server with an ableOS install and a number of Client devices
// A Client will fetch programs and files from the Server and cache them locally for a configurable amount of time
// TODO: Expand this explination
/// The size of blocks sent over the network by ARI
pub const ARIFILE_BLOCK_SIZE: usize = 4096/*bytes*/;
#[derive(Debug, Hash, PartialEq)]
pub struct ARIFileBlock([u8; ARIFILE_BLOCK_SIZE]);
#[derive(Debug, Hash, PartialEq)]
pub struct ARIFile {
inner_data: Vec<ARIFileBlock>,
}
impl ARIFile {
pub fn new() -> Self {
Self { inner_data: vec![] }
}
pub fn file_from_vec(vec: Vec<u8>) -> Self {
let mut data = vec![];
let mut data_chunk = vec![];
for x in vec.into_iter() {
if data_chunk.len() == ARIFILE_BLOCK_SIZE {
let data_block: [u8; ARIFILE_BLOCK_SIZE] = data_chunk[0..ARIFILE_BLOCK_SIZE]
.try_into()
.expect("slice with incorrect length");
data.push(ARIFileBlock(data_block));
data_chunk = vec![];
}
data_chunk.push(x);
}
if !data_chunk.is_empty() {
let missing_byte_count = ARIFILE_BLOCK_SIZE - data_chunk.len();
let msg: String = format!("Length Not Aligned missing {} bytes", missing_byte_count);
trace!("{}", msg);
for _ in data_chunk.len()..ARIFILE_BLOCK_SIZE {
data_chunk.push(0)
}
data.push(ARIFileBlock(
data_chunk[0..ARIFILE_BLOCK_SIZE]
.try_into()
.expect("slice with incorrect length"),
))
}
Self { inner_data: data }
}
}
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
pub fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
#[test]
pub fn file_diffrence_hash() {
let file_1 = ARIFile::file_from_vec(vec![1]);
let hash_1 = calculate_hash(&file_1.inner_data[0]);
let file_2 = ARIFile::file_from_vec(vec![1, 1, 1, 1, 1, 1, 1, 1]);
let hash_2 = calculate_hash(&file_2.inner_data[0]);
assert_ne!(hash_1, hash_2);
}
#[test]
pub fn hash_test() {
let file = ARIFile::file_from_vec(vec![1]);
assert_eq!(calculate_hash(&file.inner_data[0]), 174020520262829630);
}