From 5cf693b7bb836efb4e826078ece6785c3c06a8b1 Mon Sep 17 00:00:00 2001 From: jane400 Date: Wed, 28 Aug 2024 15:14:42 +0200 Subject: [PATCH] chore: move primitive{reader,build} to utils --- libpaket/src/locker/command.rs | 99 +--------------------------------- libpaket/src/locker/mod.rs | 2 + libpaket/src/locker/utils.rs | 93 ++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 98 deletions(-) create mode 100644 libpaket/src/locker/utils.rs diff --git a/libpaket/src/locker/command.rs b/libpaket/src/locker/command.rs index 7817b41..b4d085e 100644 --- a/libpaket/src/locker/command.rs +++ b/libpaket/src/locker/command.rs @@ -1,5 +1,6 @@ use num_enum::TryFromPrimitive; use uuid::Uuid; +use super::utils::{PrimitiveBuilder, PrimitiveReader}; use crate::{LibraryError, LibraryResult}; @@ -81,104 +82,6 @@ pub struct Command { metadata: Vec } -struct PrimitiveBuilder { - bin: Vec, -} - -impl PrimitiveBuilder { - fn new() -> Self { - PrimitiveBuilder { - bin: Vec::new() - } - } - - fn write_u8(self, number: u8) -> Self { - self.write_array(&[number]) - } - - fn write_u16(self, number: u16) -> Self { - self.write_array(&number.to_be_bytes()) - } - - fn write_u32(self, number: u32) -> Self { - self.write_array(&number.to_be_bytes()) - } - - fn write_array(mut self, new: &[u8]) -> Self { - for b in new { - self.bin.push(*b); - } - - self - } - - fn write_array_with_len(self, bin: &[u8]) -> Self { - self - .write_u32(bin.len() as u32) - .write_array(bin) - } - - fn finish(self) -> Vec { - self.bin - } -} - -struct PrimitiveReader<'a> { - offset: usize, - vec: &'a [u8], -} - -// Yes, I know Cursor exists and eio exists. from_be_bytes should be a stdlib trait tho. -impl<'a> PrimitiveReader<'a> { - fn read_u8(&mut self) -> u8 { - let number = self.vec[self.offset]; - self.offset += 1; - number - } - - fn read_u16(&mut self) -> u16 { - let arr: [u8; 2] = self.read_arr_const(); - u16::from_be_bytes(arr) - } - - fn read_u32(&mut self) -> u32 { - let arr: [u8; 4] = self.read_arr_const(); - u32::from_be_bytes(arr) - } - - fn read_u64(&mut self) -> u64 { - let arr: [u8; 8] = self.read_arr_const(); - u64::from_be_bytes(arr) - } - - fn read_arr_const(&mut self) -> [u8; N] { - let mut arr = [0u8; N]; - for n in 0..N { - arr[n] = self.read_u8(); - } - - return arr; - } - - fn read_arr(&mut self, n: usize) -> Vec { - let mut arr: Vec = vec![]; - for _ in 0..n { - arr.push(self.read_u8()); - } - - arr - } - - fn read_arr_from_len(&mut self) -> Vec { - let size = self.read_u32() as usize; - self.read_arr(size) - } - - fn left_to_process(&self) -> usize { - self.vec.len() - self.offset - } -} - impl Command { fn checksum(bin: &[u8]) -> u16 { // CRC16 of some kind... diff --git a/libpaket/src/locker/mod.rs b/libpaket/src/locker/mod.rs index eee28ae..991186a 100644 --- a/libpaket/src/locker/mod.rs +++ b/libpaket/src/locker/mod.rs @@ -8,6 +8,8 @@ pub use types::*; mod api; pub mod crypto; +pub(crate) mod utils; + #[cfg(feature = "locker_register_base")] mod register_base; #[cfg(feature = "locker_register_regtoken")] diff --git a/libpaket/src/locker/utils.rs b/libpaket/src/locker/utils.rs new file mode 100644 index 0000000..e6dff03 --- /dev/null +++ b/libpaket/src/locker/utils.rs @@ -0,0 +1,93 @@ +pub(crate) struct PrimitiveBuilder { + pub bin: Vec, +} + +impl PrimitiveBuilder { + pub(crate) fn new() -> Self { + PrimitiveBuilder { bin: Vec::new() } + } + + pub(crate) fn write_u8(self, number: u8) -> Self { + self.write_array(&[number]) + } + + pub(crate) fn write_u16(self, number: u16) -> Self { + self.write_array(&number.to_be_bytes()) + } + + pub(crate) fn write_u32(self, number: u32) -> Self { + self.write_array(&number.to_be_bytes()) + } + + pub(crate) fn write_array(mut self, new: &[u8]) -> Self { + for b in new { + self.bin.push(*b); + } + + self + } + + pub(crate) fn write_array_with_len(self, bin: &[u8]) -> Self { + self.write_u32(bin.len() as u32).write_array(bin) + } + + pub(crate) fn finish(self) -> Vec { + self.bin + } +} + +pub(crate) struct PrimitiveReader<'a> { + pub offset: usize, + pub vec: &'a [u8], +} + +// Yes, I know Cursor exists and eio exists. from_be_bytes should be a stdlib trait tho. +impl<'a> PrimitiveReader<'a> { + pub(crate) fn read_u8(&mut self) -> u8 { + let number = self.vec[self.offset]; + self.offset += 1; + number + } + + pub(crate) fn read_u16(&mut self) -> u16 { + let arr: [u8; 2] = self.read_arr_const(); + u16::from_be_bytes(arr) + } + + pub(crate) fn read_u32(&mut self) -> u32 { + let arr: [u8; 4] = self.read_arr_const(); + u32::from_be_bytes(arr) + } + + pub(crate) fn read_u64(&mut self) -> u64 { + let arr: [u8; 8] = self.read_arr_const(); + u64::from_be_bytes(arr) + } + + pub(crate) fn read_arr_const(&mut self) -> [u8; N] { + let mut arr = [0u8; N]; + for n in 0..N { + arr[n] = self.read_u8(); + } + + return arr; + } + + pub(crate) fn read_arr(&mut self, n: usize) -> Vec { + let mut arr: Vec = vec![]; + for _ in 0..n { + arr.push(self.read_u8()); + } + + arr + } + + pub(crate) fn read_arr_from_len(&mut self) -> Vec { + let size = self.read_u32() as usize; + self.read_arr(size) + } + + pub(crate) fn left_to_process(&self) -> usize { + self.vec.len() - self.offset + } +}