08063391e87ea44bfbeb2f378031b64a80a5232c
μCOBS — Consistent Overhead Byte Stuffing encoder/decoder. no_std, zero-alloc, spec-compliant (Cheshire & Baker 1999). 44 tests + doctests, property-based testing, fuzz targets, cross-validation against corncobs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
μCOBS
The COBS implementation for Rust.
Consistent Overhead Byte Stuffing
(COBS) encodes arbitrary bytes so that 0x00 never appears in the output,
allowing 0x00 to serve as an unambiguous frame delimiter. Overhead is exactly
1 byte per 254 input bytes (worst case).
Features
no_std, zero-alloc — runs anywhere, from 8-bit MCUs to servers- ~140 lines of implementation — small, auditable, easy to verify
- 100% spec-compliant — Cheshire & Baker, IEEE/ACM Transactions on Networking, 1999
- Proven interoperability — cross-validated against
corncobs,cobs, and Pythoncobs - Insanely tested — canonical vectors, property-based tests, fuzz targets, cross-crate interop, randomized payloads, and more coming
Usage
// Encode
let mut buf = [0u8; 16];
let n = ucobs::encode(&[0x11, 0x00, 0x33], &mut buf).unwrap();
assert_eq!(&buf[..n], &[0x02, 0x11, 0x02, 0x33]);
// Decode
let mut out = [0u8; 16];
let n = ucobs::decode(&[0x02, 0x11, 0x02, 0x33], &mut out).unwrap();
assert_eq!(&out[..n], &[0x11, 0x00, 0x33]);
// Buffer sizing
let max = ucobs::max_encoded_len(256); // 258
API
| Function | Signature | Description |
|---|---|---|
encode |
(src: &[u8], dest: &mut [u8]) -> Option<usize> |
COBS-encode src into dest. Returns byte count or None if buffer too small. |
decode |
(src: &[u8], dest: &mut [u8]) -> Option<usize> |
Decode COBS data. Returns byte count or None if input is malformed. |
max_encoded_len |
const fn(src_len: usize) -> usize |
Maximum encoded size for a given input length. |
Note: The encoder does not append a trailing 0x00 sentinel. Append it
yourself when framing for transport.
Testing
# Unit + property + interop tests
cargo test
# Fuzz testing (requires nightly + cargo-fuzz)
cd fuzz
cargo +nightly fuzz run fuzz_decode -- -max_total_time=60
cargo +nightly fuzz run fuzz_roundtrip -- -max_total_time=60
License
MIT — Stephen Waits
Description
μCOBS — Consistent Overhead Byte Stuffing. no_std, zero-alloc, fastest, most-tested COBS implementation.
98 KiB
0 Stars
1 Watchers
0 Forks
Languages
Rust
93.8%
Just
3.4%
Python
2.8%