pub fn decode_non_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 < 2 {
            return Err(DleError::StreamTooShort);
        }
        if source_stream[encoded_idx] != DLE_CHAR {
            return Err(DleError::DecodingError);
        }
        encoded_idx += 1;
        if source_stream[encoded_idx] != STX_CHAR {
            *read_len = 1;
            return Err(DleError::DecodingError);
        }
        encoded_idx += 1;
        while encoded_idx < source_stream_len && decoded_idx < dest_stream_len {
            if source_stream[encoded_idx] == DLE_CHAR {
                if encoded_idx + 1 >= source_stream_len {
                    *read_len = encoded_idx;
                    return Err(DleError::DecodingError);
                }
                let next_byte = source_stream[encoded_idx + 1];
                if next_byte == STX_CHAR {
                    // Set read_len so the DLE/STX char combination is preserved
                    // It could be the start of another frame
                    *read_len = encoded_idx;
                    return Err(DleError::DecodingError);
                } else if next_byte == DLE_CHAR {
                    dest_stream[decoded_idx] = next_byte;
                    encoded_idx += 1;
                } else if next_byte == ETX_CHAR {
                    // End of stream reached
                    *read_len = encoded_idx + 2;
                    return Ok(decoded_idx);
                } else {
                    *read_len = encoded_idx;
                    return Err(DleError::DecodingError);
                }
            } else {
                dest_stream[decoded_idx] = source_stream[encoded_idx];
            }
            encoded_idx += 1;
            decoded_idx += 1;
        }

        if decoded_idx == dest_stream_len {
            // So far we did not find anything wrong here, let the user try
            // again
            *read_len = 0;
            Err(DleError::StreamTooShort)
        } else {
            *read_len = encoded_idx;
            Err(DleError::DecodingError)
        }
    }
/// This method decodes a given byte stream which was encoded with a ASCII
/// DLE encoder. It explicitely does so in the non-escaped 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
///
/// # Examples
///
/// ```
/// use dle_encoder::DleEncoder;
///
/// let dle_encoder = DleEncoder::default();
/// let mut decoding_buffer: [u8; 16] = [0; 16];
/// let encoded_array: [u8; 6] = [0x10, 0x02, 0x02, 0x03, 0x10, 0x03];
/// let mut read_len = 0;
/// let decode_result = dle_encoder.decode_non_escaped(
///     &encoded_array, &mut decoding_buffer, &mut read_len
/// );
/// assert!(decode_result.is_ok());
/// let decoded_len = decode_result.unwrap();
/// assert_eq!(decoded_len, 2);
///
/// println!("Source buffer: {:?}", encoded_array);
/// println!("Encoded stream: {:?}", &decoding_buffer[ .. decoded_len])
/// ```
This method decodes a given byte stream which was encoded with a ASCII
DLE encoder. It explicitely does so in the non-escaped 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 non - escaped 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_non_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 < 2 {
            return Err(DleError::StreamTooShort);
        }
        if source_stream[encoded_idx] != DLE_CHAR {
            return Err(DleError::DecodingError);
        }
        encoded_idx += 1;
        if source_stream[encoded_idx] != STX_CHAR {
            *read_len = 1;
            return Err(DleError::DecodingError);
        }
        encoded_idx += 1;
        while encoded_idx < source_stream_len && decoded_idx < dest_stream_len {
            if source_stream[encoded_idx] == DLE_CHAR {
                if encoded_idx + 1 >= source_stream_len {
                    *read_len = encoded_idx;
                    return Err(DleError::DecodingError);
                }
                let next_byte = source_stream[encoded_idx + 1];
                if next_byte == STX_CHAR {
                    // Set read_len so the DLE/STX char combination is preserved
                    // It could be the start of another frame
                    *read_len = encoded_idx;
                    return Err(DleError::DecodingError);
                } else if next_byte == DLE_CHAR {
                    dest_stream[decoded_idx] = next_byte;
                    encoded_idx += 1;
                } else if next_byte == ETX_CHAR {
                    // End of stream reached
                    *read_len = encoded_idx + 2;
                    return Ok(decoded_idx);
                } else {
                    *read_len = encoded_idx;
                    return Err(DleError::DecodingError);
                }
            } else {
                dest_stream[decoded_idx] = source_stream[encoded_idx];
            }
            encoded_idx += 1;
            decoded_idx += 1;
        }
        if decoded_idx == dest_stream_len {
            // So far we did not find anything wrong here, let the user try
            // again
            *read_len = 0;
            Err(DleError::StreamTooShort)
        } else {
            *read_len = encoded_idx;
            Err(DleError::DecodingError)
        }
    }
pub fn decode_non_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 < 2 { return Err ( DleError :: StreamTooShort ) ; } if source_stream [ encoded_idx ] != DLE_CHAR { return Err ( DleError :: DecodingError ) ; } encoded_idx += 1 ; if source_stream [ encoded_idx ] != STX_CHAR { * read_len = 1 ; return Err ( DleError :: DecodingError ) ; } encoded_idx += 1 ; while encoded_idx < source_stream_len && decoded_idx < dest_stream_len { if source_stream [ encoded_idx ] == DLE_CHAR { if encoded_idx + 1 >= source_stream_len { * read_len = encoded_idx ; return Err ( DleError :: DecodingError ) ; } let next_byte = source_stream [ encoded_idx + 1 ] ; if next_byte == STX_CHAR { // Set read_len so the DLE/STX char combination is preserved // It could be the start of another frame * read_len = encoded_idx ; return Err ( DleError :: DecodingError ) ; } else if next_byte == DLE_CHAR { dest_stream [ decoded_idx ] = next_byte ; encoded_idx += 1 ; } else if next_byte == ETX_CHAR { // End of stream reached * read_len = encoded_idx + 2 ; return Ok ( decoded_idx ) ; } else { * read_len = encoded_idx ; return Err ( DleError :: DecodingError ) ; } } else { dest_stream [ decoded_idx ] = source_stream [ encoded_idx ] ; } encoded_idx += 1 ; decoded_idx += 1 ; } if decoded_idx == dest_stream_len { // So far we did not find anything wrong here, let the user try // again * read_len = 0 ; Err ( DleError :: StreamTooShort ) } else { * read_len = encoded_idx ; Err ( DleError :: DecodingError ) } }
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
examples
```
use dle_encoder::DleEncoder.

let dle_encoder = DleEncoder::default();
let mut decoding_buffer: [u8; 16] = [0; 16];
let encoded_array: [u8; 6] = [0x10, 0x02, 0x02, 0x03, 0x10, 0x03];
let mut read_len = 0;
let decode_result = dle_encoder.decode_non_escaped(
&encoded_array, &mut decoding_buffer, &mut read_len
);
assert!(decode_result.is_ok());
let decoded_len = decode_result.unwrap();
assert_eq!(decoded_len, 2).

println!("Source buffer: {:?}", encoded_array);
println!("Encoded stream: {:?}", &decoding_buffer[ ..
` ` ` use dle_encoder :: DleEncoder . let dle_encoder = DleEncoder :: default () ; let mut decoding_buffer : [ u8 ; 16 ] = [ 0 ; 16 ] ; let encoded_array : [ u8 ; 6 ] = [ 0x10 0x02 0x02 0x03 0x10 0x03 ] ; let mut read_len = 0 ; let decode_result = dle_encoder . decode_non_escaped ( &encoded_array &mut decoding_buffer &mut read_len ) ; assert! ( decode_result . is_ok () ) ; let decoded_len = decode_result . unwrap () ; assert_eq! ( decoded_len 2 ) . println! ( " Source buffer : { : ? } " encoded_array ) ; println! ( " Encoded stream : { : ? } " &decoding_buffer [ ..