pub fn cobs_encode_in_place(buf: &mut [u8], len: usize) -> Result<usize, Error>Expand description
Encodes data in-place using COBS (Consistent Overhead Byte Stuffing) algorithm.
§Algorithm Overview
COBS replaces all zero bytes in the input with overhead bytes that indicate the distance to the next zero byte. This ensures the encoded output contains no zero bytes, making it suitable for packet-based protocols.
§How it Works
- Scan through the input looking for zero bytes
- Replace each zero byte with an overhead byte (1-254, or 255 for special case)
- The overhead byte indicates how many bytes follow until the next zero
- A “virtual zero” is treated as existing at the end of the input
§Special Cases
- Overhead value 255: Indicates 254 non-zero bytes, another overhead follows
- Overhead value N (1-254): Indicates N-1 data bytes, then a zero
- Sequences of 254+ non-zero bytes trigger additional 255 overhead bytes
§In-Place Encoding
This function modifies the buffer in-place by:
- Rotating bytes right to make room for overhead bytes
- Tracking how many overhead bytes have been inserted (expands the data)
- Returning the new encoded length