diff --git a/domo_proto/Cargo.lock b/domo_proto/Cargo.lock index b9a2cb2..2090f24 100644 --- a/domo_proto/Cargo.lock +++ b/domo_proto/Cargo.lock @@ -19,7 +19,7 @@ dependencies = [ [[package]] name = "domo_proto" -version = "1.0.0" +version = "1.0.1" dependencies = [ "crc32fast", "rand", diff --git a/domo_proto/Cargo.toml b/domo_proto/Cargo.toml index 5afa35f..bb6eb33 100644 --- a/domo_proto/Cargo.toml +++ b/domo_proto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "domo_proto" -version = "1.0.0" +version = "1.0.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/domo_proto/src/commands/property_control/mod.rs b/domo_proto/src/commands/property_control/mod.rs index 69d7be5..870b8f9 100644 --- a/domo_proto/src/commands/property_control/mod.rs +++ b/domo_proto/src/commands/property_control/mod.rs @@ -3,7 +3,6 @@ use crate::identifier::Identifier; use crate::packet::packet_data::PacketData; use crate::packet::raw_packet::RawPacket; use crate::packet::{Packet, ToPacket}; -use crate::prelude::as_u32_be; use std::io; pub mod vec; @@ -149,9 +148,14 @@ impl TryFrom for PropertyControlCommand { 0x1A => { if raw_packet.data_length == 36 { Ok(PropertyControlCommand::Subscribe { - device_id: Identifier::from(as_u32_be( - raw_packet.data[0x14..0x18].as_ref(), - )), + device_id: Identifier::from( + u32::from_be_bytes([ + raw_packet.data[0x14], + raw_packet.data[0x15], + raw_packet.data[0x16], + raw_packet.data[0x17], + ]) + ), property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec()) .map_err(|_| { io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8") @@ -165,9 +169,14 @@ impl TryFrom for PropertyControlCommand { 0x1B => { if raw_packet.data_length == 36 { Ok(PropertyControlCommand::Unsubscribe { - device_id: Identifier::from(as_u32_be( - raw_packet.data[0x14..0x18].as_ref(), - )), + device_id: Identifier::from( + u32::from_be_bytes([ + raw_packet.data[0x14], + raw_packet.data[0x15], + raw_packet.data[0x16], + raw_packet.data[0x17], + ]) + ), property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec()) .map_err(|_| { io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8") diff --git a/domo_proto/src/packet/raw_packet/vec.rs b/domo_proto/src/packet/raw_packet/vec.rs index db0ed95..96fb606 100644 --- a/domo_proto/src/packet/raw_packet/vec.rs +++ b/domo_proto/src/packet/raw_packet/vec.rs @@ -1,29 +1,53 @@ use std::io; use crate::packet::PACKET_HEADER_SIZE; use crate::packet::raw_packet::RawPacket; -use crate::prelude::as_u32_be; impl TryFrom> for RawPacket { type Error = io::Error; fn try_from(data: Vec) -> io::Result { if data.len() < PACKET_HEADER_SIZE { - return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket")) + return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket")); } let data_length = (((data[0x12] as u16) << 8) | data[0x13] as u16) as usize; if data.len() < 0x14 + data_length { - return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket")) + return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket")); } - let checksum = as_u32_be(data[(0x14 + data_length)..].as_ref()); + let checksum = u32::from_be_bytes([ + data[0x14 + data_length], + data[0x14 + data_length + 1], + data[0x14 + data_length + 2], + data[0x14 + data_length + 3], + ]); let built_checksum = crc32fast::hash(data[..(data.len() - 4)].as_ref()); if checksum != built_checksum { - return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Checksum does not match {checksum:X?} != {built_checksum:X?}"))) + return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Checksum does not match {checksum:X?} != {built_checksum:X?}"))); } Ok(RawPacket { version: data[0], - dest: as_u32_be(data[0x01..0x05].as_ref()), - src: as_u32_be(data[0x05..0x09].as_ref()), - packet_id: as_u32_be(data[0x09..0x0D].as_ref()), - reply_to: as_u32_be(data[0x0D..0x11].as_ref()), + dest: u32::from_be_bytes([ + data[0x01], + data[0x02], + data[0x03], + data[0x04] + ]), + src: u32::from_be_bytes([ + data[0x05], + data[0x06], + data[0x07], + data[0x08], + ]), + packet_id: u32::from_be_bytes([ + data[0x09], + data[0x0A], + data[0x0B], + data[0x0C], + ]), + reply_to: u32::from_be_bytes([ + data[0x0D], + data[0x0E], + data[0x0F], + data[0x10], + ]), command: data[0x11], data_length, data: data[0x14..(0x14 + data_length)].to_vec(), diff --git a/domo_proto/src/prelude.rs b/domo_proto/src/prelude.rs index 928d1b4..1ac35cc 100644 --- a/domo_proto/src/prelude.rs +++ b/domo_proto/src/prelude.rs @@ -1,21 +1,3 @@ -pub fn as_u32_be(array: &[u8]) -> u32 { - ((array[0] as u32) << 24) - + ((array[1] as u32) << 16) - + ((array[2] as u32) << 8) - + ((array[3] as u32) << 0) -} - -pub fn as_u64_be(array: &[u8]) -> u64 { - ((array[0] as u64) << 56) - + ((array[1] as u64) << 48) - + ((array[2] as u64) << 40) - + ((array[3] as u64) << 32) - + ((array[4] as u64) << 24) - + ((array[5] as u64) << 16) - + ((array[6] as u64) << 8) - + ((array[7] as u64) << 0) -} - macro_rules! impl_into_enum_variant { ($houser: tt::$variant: tt, $impl_type: ty) => { impl Into<$houser> for $impl_type {