/// Validate the contents of the raw packet buffer, checking for checksum
/// consistency, structural correctness, and ASCII validation.
Validate the contents of the raw packet buffer, checking for checksum
consistency, structural correctness, and ASCII validation.
Validate the contents of the raw packet buffer checking for checksum consistency structural correctness and ASCII validation .
pub fn new(pkt_buf: &'a mut [u8]) -> Result<PacketBuf<'a>, PacketParseError> {
        // validate the packet is valid ASCII
        if !pkt_buf.is_ascii() {
            return Err(PacketParseError::NotASCII);
        }
        let end_of_body = pkt_buf
            .iter()
            .position(|b| *b == b'#')
            .ok_or(PacketParseError::MissingChecksum)?;
        // split buffer into body and checksum components
        let (body, checksum) = pkt_buf.split_at_mut(end_of_body);
        let body = &mut body[1..]; // skip the '$'
        let checksum = &mut checksum[1..][..2]; // skip the '#'
        // validate the checksum
        let checksum = decode_hex(checksum).map_err(|_| PacketParseError::MalformedChecksum)?;
        let calculated = body.iter().fold(0u8, |a, x| a.wrapping_add(*x));
        if calculated != checksum {
            return Err(PacketParseError::ChecksumMismatched {
                checksum,
                calculated,
            });
        }
        if log_enabled!(log::Level::Trace) {
            // SAFETY: body confirmed to be `is_ascii()`
            let body = unsafe { core::str::from_utf8_unchecked(body) };
            trace!("<-- ${}#{:02x?}", body, checksum);
        }
        Ok(PacketBuf {
            buf: pkt_buf,
            body_range: 1..end_of_body,
        })
    }
pub fn new ( pkt_buf : & ' a mut [ u8 ] ) -> Result < PacketBuf < ' a > , PacketParseError > { // validate the packet is valid ASCII if ! pkt_buf . is_ascii ( ) { return Err ( PacketParseError :: NotASCII ) ; } let end_of_body = pkt_buf . iter ( ) . position ( | b | * b == b'#' ) . ok_or ( PacketParseError :: MissingChecksum ) ? ; // split buffer into body and checksum components let ( body , checksum ) = pkt_buf . split_at_mut ( end_of_body ) ; let body = & mut body [ 1 .. ] ; // skip the '$' let checksum = & mut checksum [ 1 .. ] [ .. 2 ] ; // skip the '#' // validate the checksum let checksum = decode_hex ( checksum ) . map_err ( | _ | PacketParseError :: MalformedChecksum ) ? ; let calculated = body . iter ( ) . fold ( 0u8 , | a , x | a . wrapping_add ( * x ) ) ; if calculated != checksum { return Err ( PacketParseError :: ChecksumMismatched { checksum , calculated , } ) ; } if log_enabled ! ( log Level Trace ) { // SAFETY: body confirmed to be `is_ascii()` let body = unsafe { core :: str :: from_utf8_unchecked ( body ) } ; trace ! ( " " body checksum ) ; } Ok ( PacketBuf { buf : pkt_buf , body_range : 1 .. end_of_body , } ) }
Validate the contents of the raw packet buffer, checking for checksum
consistency, structural correctness, and ASCII validation.
Validate the contents of the raw packet buffer checking for checksum consistency structural correctness and ASCII validation .
pkt_buf
&'a mut [u8]