pub fn stuff_inplace(buffer: &mut [u8], len: usize) -> Result<usize, ()> {
	let stuffing_required = stuffing_required(&buffer[..len]);
	if stuffing_required == 0 {
		return Ok(len);
	}

	let mut read = 0;
	let mut stuffing_applied = 0;

	if buffer.len() < len + stuffing_required {
		return Err(());
	}

	while read < len && stuffing_applied < stuffing_required {
		let read_pos = len - read - 1;
		let write_pos = read_pos + (stuffing_required - stuffing_applied);
		if read_pos >= 2 && buffer[read_pos - 2..][..3] == PATTERN[..3] {
			buffer[write_pos - 3..][..4].copy_from_slice(&PATTERN);
			read += 3;
			stuffing_applied += 1;
		} else {
			buffer[write_pos] = buffer[read_pos];
			read += 1;
		}
	}

	debug_assert_eq!(stuffing_applied, stuffing_required);
	Ok(len + stuffing_required)
}
/// Perform byte-stuffing in-place and return the length of the stuffed data.
///
/// The actual length of the unstuffed data must be passed in through the `len` parameter.
///
/// The full buffer must be large enough to hold the stuffed data,
/// or an error is returned.
Perform byte-stuffing in-place and return the length of the stuffed data.
The actual length of the unstuffed data must be passed in through the `len` parameter.

The full buffer must be large enough to hold the stuffed data,
or an error is returned.
Perform byte - stuffing in - place and return the length of the stuffed data . The actual length of the unstuffed data must be passed in through the ` len ` parameter . The full buffer must be large enough to hold the stuffed data or an error is returned .
pub fn stuff_inplace(buffer: &mut [u8], len: usize) -> Result<usize, ()> {
	let stuffing_required = stuffing_required(&buffer[..len]);
	if stuffing_required == 0 {
		return Ok(len);
	}
	let mut read = 0;
	let mut stuffing_applied = 0;
	if buffer.len() < len + stuffing_required {
		return Err(());
	}
	while read < len && stuffing_applied < stuffing_required {
		let read_pos = len - read - 1;
		let write_pos = read_pos + (stuffing_required - stuffing_applied);
		if read_pos >= 2 && buffer[read_pos - 2..][..3] == PATTERN[..3] {
			buffer[write_pos - 3..][..4].copy_from_slice(&PATTERN);
			read += 3;
			stuffing_applied += 1;
		} else {
			buffer[write_pos] = buffer[read_pos];
			read += 1;
		}
	}
	debug_assert_eq!(stuffing_applied, stuffing_required);
	Ok(len + stuffing_required)
}
pub fn stuff_inplace ( buffer : & mut [ u8 ] , len : usize ) -> Result < usize , ( ) > { let stuffing_required = stuffing_required ( & buffer [ .. len ] ) ; if stuffing_required == 0 { return Ok ( len ) ; } let mut read = 0 ; let mut stuffing_applied = 0 ; if buffer . len ( ) < len + stuffing_required { return Err ( ( ) ) ; } while read < len && stuffing_applied < stuffing_required { let read_pos = len - read - 1 ; let write_pos = read_pos + ( stuffing_required - stuffing_applied ) ; if read_pos >= 2 && buffer [ read_pos - 2 .. ] [ .. 3 ] == PATTERN [ .. 3 ] { buffer [ write_pos - 3 .. ] [ .. 4 ] . copy_from_slice ( & PATTERN ) ; read += 3 ; stuffing_applied += 1 ; } else { buffer [ write_pos ] = buffer [ read_pos ] ; read += 1 ; } } debug_assert_eq ! ( stuffing_applied stuffing_required ) ; Ok ( len + stuffing_required ) }
Perform byte-stuffing in-place and return the length of the stuffed data.
Perform byte - stuffing in - place and return the length of the stuffed data .
buffer
&mut [u8]
len
usize
buffer
&mut [u8]
len
usize