pub fn decode_escaped(
        &self,
        source_stream: &[u8],
        dest_stream: &mut [u8],
        read_len: &mut usize,
    ) -> Result<usize, DleError> {
        let mut encoded_idx = 0;
        let mut decoded_idx = 0;
        let source_stream_len = source_stream.len();
        let dest_stream_len = dest_stream.len();
        *read_len = 0;
        if dest_stream_len < 1 {
            return Err(DleError::StreamTooShort);
        }
        if source_stream[encoded_idx] != STX_CHAR {
            return Err(DleError::DecodingError);
        }
        encoded_idx += 1;
        while encoded_idx < source_stream_len - 1
            && decoded_idx < dest_stream_len
            && source_stream[encoded_idx] != ETX_CHAR
            && source_stream[encoded_idx] != STX_CHAR
        {
            if source_stream[encoded_idx] == DLE_CHAR {
                if encoded_idx + 1 >= source_stream_len {
                    *read_len = source_stream_len;
                    return Err(DleError::DecodingError);
                }
                let next_byte = source_stream[encoded_idx + 1];
                if next_byte == DLE_CHAR {
                    dest_stream[decoded_idx] = next_byte;
                } else if next_byte == STX_CHAR + 0x40
                    || next_byte == ETX_CHAR + 0x40
                    || (self.escape_cr && next_byte == CR_CHAR + 0x40)
                {
                    dest_stream[decoded_idx] = next_byte - 0x40;
                } else {
                    *read_len = encoded_idx + 2;
                    return Err(DleError::DecodingError);
                }
                encoded_idx += 1
            } else {
                dest_stream[decoded_idx] = source_stream[encoded_idx];
            }
            encoded_idx += 1;
            decoded_idx += 1
        }

        if source_stream[encoded_idx] != ETX_CHAR {
            if decoded_idx == dest_stream_len {
                *read_len = 0;
                Err(DleError::StreamTooShort)
            } else {
                *read_len = encoded_idx + 1;
                Err(DleError::DecodingError)
            }
        } else {
            *read_len = encoded_idx + 1;
            Ok(decoded_idx)
        }
    }
/// This method decodes a given byte stream which was encoded with a ASCII
/// DLE encoder. It explicitely does so in the escaped mode, which is the default
/// mode. It returns the length of the decoded buffer or an error code if
/// there is a decoder failure or the destination stream is too short.
///
/// # Arguments
///
/// * `source_stream` - The stream to decode
/// * `dest_stream` - Decoded stream will be written here
/// * `read_len` - The number of read bytes in the source stream will be
///   assigned to this variable
This method decodes a given byte stream which was encoded with a ASCII
DLE encoder. It explicitely does so in the escaped mode, which is the default
mode. It returns the length of the decoded buffer or an error code if
there is a decoder failure or the destination stream is too short.
This method decodes a given byte stream which was encoded with a ASCII DLE encoder . It explicitely does so in the escaped mode which is the default mode . It returns the length of the decoded buffer or an error code if there is a decoder failure or the destination stream is too short .
pub fn decode_escaped(
        &self,
        source_stream: &[u8],
        dest_stream: &mut [u8],
        read_len: &mut usize,
    ) -> Result<usize, DleError> {
        let mut encoded_idx = 0;
        let mut decoded_idx = 0;
        let source_stream_len = source_stream.len();
        let dest_stream_len = dest_stream.len();
        *read_len = 0;
        if dest_stream_len < 1 {
            return Err(DleError::StreamTooShort);
        }
        if source_stream[encoded_idx] != STX_CHAR {
            return Err(DleError::DecodingError);
        }
        encoded_idx += 1;
        while encoded_idx < source_stream_len - 1
            && decoded_idx < dest_stream_len
            && source_stream[encoded_idx] != ETX_CHAR
            && source_stream[encoded_idx] != STX_CHAR
        {
            if source_stream[encoded_idx] == DLE_CHAR {
                if encoded_idx + 1 >= source_stream_len {
                    *read_len = source_stream_len;
                    return Err(DleError::DecodingError);
                }
                let next_byte = source_stream[encoded_idx + 1];
                if next_byte == DLE_CHAR {
                    dest_stream[decoded_idx] = next_byte;
                } else if next_byte == STX_CHAR + 0x40
                    || next_byte == ETX_CHAR + 0x40
                    || (self.escape_cr && next_byte == CR_CHAR + 0x40)
                {
                    dest_stream[decoded_idx] = next_byte - 0x40;
                } else {
                    *read_len = encoded_idx + 2;
                    return Err(DleError::DecodingError);
                }
                encoded_idx += 1
            } else {
                dest_stream[decoded_idx] = source_stream[encoded_idx];
            }
            encoded_idx += 1;
            decoded_idx += 1
        }
        if source_stream[encoded_idx] != ETX_CHAR {
            if decoded_idx == dest_stream_len {
                *read_len = 0;
                Err(DleError::StreamTooShort)
            } else {
                *read_len = encoded_idx + 1;
                Err(DleError::DecodingError)
            }
        } else {
            *read_len = encoded_idx + 1;
            Ok(decoded_idx)
        }
    }
pub fn decode_escaped ( & self , source_stream : & [ u8 ] , dest_stream : & mut [ u8 ] , read_len : & mut usize , ) -> Result < usize , DleError > { let mut encoded_idx = 0 ; let mut decoded_idx = 0 ; let source_stream_len = source_stream . len ( ) ; let dest_stream_len = dest_stream . len ( ) ; * read_len = 0 ; if dest_stream_len < 1 { return Err ( DleError :: StreamTooShort ) ; } if source_stream [ encoded_idx ] != STX_CHAR { return Err ( DleError :: DecodingError ) ; } encoded_idx += 1 ; while encoded_idx < source_stream_len - 1 && decoded_idx < dest_stream_len && source_stream [ encoded_idx ] != ETX_CHAR && source_stream [ encoded_idx ] != STX_CHAR { if source_stream [ encoded_idx ] == DLE_CHAR { if encoded_idx + 1 >= source_stream_len { * read_len = source_stream_len ; return Err ( DleError :: DecodingError ) ; } let next_byte = source_stream [ encoded_idx + 1 ] ; if next_byte == DLE_CHAR { dest_stream [ decoded_idx ] = next_byte ; } else if next_byte == STX_CHAR + 0x40 || next_byte == ETX_CHAR + 0x40 || ( self . escape_cr && next_byte == CR_CHAR + 0x40 ) { dest_stream [ decoded_idx ] = next_byte - 0x40 ; } else { * read_len = encoded_idx + 2 ; return Err ( DleError :: DecodingError ) ; } encoded_idx += 1 } else { dest_stream [ decoded_idx ] = source_stream [ encoded_idx ] ; } encoded_idx += 1 ; decoded_idx += 1 } if source_stream [ encoded_idx ] != ETX_CHAR { if decoded_idx == dest_stream_len { * read_len = 0 ; Err ( DleError :: StreamTooShort ) } else { * read_len = encoded_idx + 1 ; Err ( DleError :: DecodingError ) } } else { * read_len = encoded_idx + 1 ; Ok ( decoded_idx ) } }
This method decodes a given byte stream which was encoded with a ASCII
DLE encoder.
This method decodes a given byte stream which was encoded with a ASCII DLE encoder .
&self
source_stream
&[u8]
dest_stream
&mut [u8]
read_len
&mut usize
&self
source_stream
&[u8]
dest_stream
&mut [u8]
read_len
&mut usize