Skip to content

Commit

Permalink
chore: drop byteorder dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Barbolini <[email protected]>
  • Loading branch information
paolobarbolini authored and brooksmtownsend committed Apr 8, 2024
1 parent 5c4aed3 commit 333844d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ ed25519-dalek = { version = "2.0.0", default-features = false, features = [
"digest",
] }
rand = "0.8"
byteorder = "1.3.4"
data-encoding = "2.3.0"
log = "0.4.11"
crypto_box = { version = "0.9.1", optional = true } # For xKeys support
Expand Down
26 changes: 8 additions & 18 deletions src/crc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::io::Cursor;

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use super::error::{Error, ErrorKind};
use super::Result;

Expand Down Expand Up @@ -45,26 +43,18 @@ pub(crate) fn valid_checksum(data: &[u8], expected: u16) -> bool {

pub(crate) fn push_crc(data: &mut Vec<u8>) {
let crc = crc16(data);
data.write_u16::<LittleEndian>(crc).unwrap();
data.extend(u16::to_le_bytes(crc));
}

pub(crate) fn extract_crc(data: &mut Vec<u8>) -> Result<u16> {
let crc_end_idx = if data.len() >= 2 {
data.len() - 2
} else {
return Err(Error::new(
ErrorKind::ChecksumFailure,
Some("CRC data vector contains less than two characters"),
));
};
let crc_bytes = data[..crc_end_idx].to_vec();

let mut reader = Cursor::new(crc_bytes);
(*data).truncate(crc_end_idx);
reader.read_u16::<LittleEndian>().map_err(|e| {
let data_len = data.len().checked_sub(2).ok_or_else(|| {
Error::new(
ErrorKind::ChecksumFailure,
Some(format!("failed to read u16: {e}").as_str()),
Some("CRC data vector contains less than two characters"),
)
})
})?;

let crc = u16::from_le_bytes(data[..2].try_into().unwrap());
data.truncate(data_len);
Ok(crc)
}

0 comments on commit 333844d

Please sign in to comment.