pub fn encode_escaped(
        &self,
        source_stream: &[u8],
        dest_stream: &mut [u8],
    ) -> Result<usize, DleError> {
        let mut encoded_idx = 0;
        let mut source_idx = 0;
        let max_dest_len = dest_stream.len();
        if self.add_stx_etx {
            if max_dest_len < 1 {
                return Err(DleError::StreamTooShort);
            }
            dest_stream[encoded_idx] = STX_CHAR;
            encoded_idx += 1;
        }
        while encoded_idx < max_dest_len && source_idx < source_stream.len() {
            let next_byte = source_stream[source_idx];
            if next_byte == STX_CHAR
                || next_byte == ETX_CHAR
                || (self.escape_cr && next_byte == CR_CHAR)
            {
                if encoded_idx + 1 >= max_dest_len {
                    return Err(DleError::StreamTooShort);
                } else {
                    dest_stream[encoded_idx] = DLE_CHAR;
                    encoded_idx += 1;
                    // Next byte will be the actual byte + 0x40. This prevents STX and ETX from
                    // appearin in the encoded data stream at all, so when polling an encoded
                    // stream, the transmission can be stopped at ETX. 0x40 was chose at random
                    // with special requirements:
                    // - Prevent going from one control char to another
                    // - Prevent overflow for common characters
                    dest_stream[encoded_idx] = next_byte + 0x40;
                }
            } else if next_byte == DLE_CHAR {
                if encoded_idx + 1 >= max_dest_len {
                    return Err(DleError::StreamTooShort);
                } else {
                    dest_stream[encoded_idx] = DLE_CHAR;
                    encoded_idx += 1;
                    dest_stream[encoded_idx] = DLE_CHAR;
                }
            } else {
                dest_stream[encoded_idx] = next_byte;
            }
            encoded_idx += 1;
            source_idx += 1;
        }

        if source_idx == source_stream.len() {
            if self.add_stx_etx {
                if encoded_idx + 1 >= max_dest_len {
                    return Err(DleError::StreamTooShort);
                }
                dest_stream[encoded_idx] = ETX_CHAR;
                encoded_idx += 1
            }
            Ok(encoded_idx)
        } else {
            Err(DleError::StreamTooShort)
        }
    }
/// This method encodes a given byte stream with ASCII based DLE encoding.
/// It explicitely does so in the escaped mode, which is the default
/// mode.
///
/// # Arguments
///
/// * `source_stream` - The stream to encode
/// * `dest_stream` - Encoded stream will be written here
This method encodes a given byte stream with ASCII based DLE encoding.
It explicitely does so in the escaped mode, which is the default
mode.
This method encodes a given byte stream with ASCII based DLE encoding . It explicitely does so in the escaped mode which is the default mode .
pub fn encode_escaped(
        &self,
        source_stream: &[u8],
        dest_stream: &mut [u8],
    ) -> Result<usize, DleError> {
        let mut encoded_idx = 0;
        let mut source_idx = 0;
        let max_dest_len = dest_stream.len();
        if self.add_stx_etx {
            if max_dest_len < 1 {
                return Err(DleError::StreamTooShort);
            }
            dest_stream[encoded_idx] = STX_CHAR;
            encoded_idx += 1;
        }
        while encoded_idx < max_dest_len && source_idx < source_stream.len() {
            let next_byte = source_stream[source_idx];
            if next_byte == STX_CHAR
                || next_byte == ETX_CHAR
                || (self.escape_cr && next_byte == CR_CHAR)
            {
                if encoded_idx + 1 >= max_dest_len {
                    return Err(DleError::StreamTooShort);
                } else {
                    dest_stream[encoded_idx] = DLE_CHAR;
                    encoded_idx += 1;
                    // Next byte will be the actual byte + 0x40. This prevents STX and ETX from
                    // appearin in the encoded data stream at all, so when polling an encoded
                    // stream, the transmission can be stopped at ETX. 0x40 was chose at random
                    // with special requirements:
                    // - Prevent going from one control char to another
                    // - Prevent overflow for common characters
                    dest_stream[encoded_idx] = next_byte + 0x40;
                }
            } else if next_byte == DLE_CHAR {
                if encoded_idx + 1 >= max_dest_len {
                    return Err(DleError::StreamTooShort);
                } else {
                    dest_stream[encoded_idx] = DLE_CHAR;
                    encoded_idx += 1;
                    dest_stream[encoded_idx] = DLE_CHAR;
                }
            } else {
                dest_stream[encoded_idx] = next_byte;
            }
            encoded_idx += 1;
            source_idx += 1;
        }
        if source_idx == source_stream.len() {
            if self.add_stx_etx {
                if encoded_idx + 1 >= max_dest_len {
                    return Err(DleError::StreamTooShort);
                }
                dest_stream[encoded_idx] = ETX_CHAR;
                encoded_idx += 1
            }
            Ok(encoded_idx)
        } else {
            Err(DleError::StreamTooShort)
        }
    }
pub fn encode_escaped ( & self , source_stream : & [ u8 ] , dest_stream : & mut [ u8 ] , ) -> Result < usize , DleError > { let mut encoded_idx = 0 ; let mut source_idx = 0 ; let max_dest_len = dest_stream . len ( ) ; if self . add_stx_etx { if max_dest_len < 1 { return Err ( DleError :: StreamTooShort ) ; } dest_stream [ encoded_idx ] = STX_CHAR ; encoded_idx += 1 ; } while encoded_idx < max_dest_len && source_idx < source_stream . len ( ) { let next_byte = source_stream [ source_idx ] ; if next_byte == STX_CHAR || next_byte == ETX_CHAR || ( self . escape_cr && next_byte == CR_CHAR ) { if encoded_idx + 1 >= max_dest_len { return Err ( DleError :: StreamTooShort ) ; } else { dest_stream [ encoded_idx ] = DLE_CHAR ; encoded_idx += 1 ; // Next byte will be the actual byte + 0x40. This prevents STX and ETX from // appearin in the encoded data stream at all, so when polling an encoded // stream, the transmission can be stopped at ETX. 0x40 was chose at random // with special requirements: // - Prevent going from one control char to another // - Prevent overflow for common characters dest_stream [ encoded_idx ] = next_byte + 0x40 ; } } else if next_byte == DLE_CHAR { if encoded_idx + 1 >= max_dest_len { return Err ( DleError :: StreamTooShort ) ; } else { dest_stream [ encoded_idx ] = DLE_CHAR ; encoded_idx += 1 ; dest_stream [ encoded_idx ] = DLE_CHAR ; } } else { dest_stream [ encoded_idx ] = next_byte ; } encoded_idx += 1 ; source_idx += 1 ; } if source_idx == source_stream . len ( ) { if self . add_stx_etx { if encoded_idx + 1 >= max_dest_len { return Err ( DleError :: StreamTooShort ) ; } dest_stream [ encoded_idx ] = ETX_CHAR ; encoded_idx += 1 } Ok ( encoded_idx ) } else { Err ( DleError :: StreamTooShort ) } }
This method encodes a given byte stream with ASCII based DLE encoding.
This method encodes a given byte stream with ASCII based DLE encoding .
&self
source_stream
&[u8]
dest_stream
&mut [u8]
&self
source_stream
&[u8]
dest_stream
&mut [u8]