#cobs #codec #fuzz-testing #no-std

no-std corncobs

COBS encode/decode for Rust no_std targets

3 releases

0.1.3 Nov 10, 2022
0.1.1 Feb 6, 2022
0.1.0 Feb 6, 2022

#275 in Embedded development

Download history 1660/week @ 2024-06-09 1940/week @ 2024-06-16 2571/week @ 2024-06-23 2199/week @ 2024-06-30 3228/week @ 2024-07-07 1953/week @ 2024-07-14 807/week @ 2024-07-21 644/week @ 2024-07-28 964/week @ 2024-08-04 489/week @ 2024-08-11 428/week @ 2024-08-18 444/week @ 2024-08-25 219/week @ 2024-09-01 395/week @ 2024-09-08 748/week @ 2024-09-15 797/week @ 2024-09-22

2,184 downloads per month
Used in 4 crates (3 directly)

MPL-2.0 license

33KB
284 lines

corncobs: Corny COBS encoding/decoding in Rust

This crate provides Consistent Overhead Byte Stuffing (COBS) support for Rust programs, with a particular focus on resource-limited embedded no_std targets:

  • Provides both fast (buffer-to-buffer) and small (in-place or iterator-based) versions of both encode and decode routines.

  • Provides a const fn for computing the maximum encoded size for a given input size, so you can define fixed-size buffers precisely without magic numbers.

  • Has pretty good test coverage, Criterion benchmarks, and a honggfuzz fuzz testing suite to try to ensure code quality.

Cargo features

No features are enabled by default. Embedded programmers do not need to specify default-features = false when using corncobs because who said std should be the default anyhow? People with lots of RAM, that's who.

Features:

  • std: if you're on one of them "big computers" with "infinite memory" and can afford the inherent nondeterminism of dynamic memory allocation, this feature enables routines for encoding to-from Vec, and an Error impl for CobsError.

When to use COBS

COBS lets us take an arbitrary blob of bytes and turn it into a slightly longer blob that doesn't contain a certain byte, except as a terminator at the very end. corncobs implements the version of this where the byte is zero. That is, corncobs can take a sequence of arbitrary bytes, and turn it into a slightly longer sequence that doesn't contain zero except at the end.

The main reason you'd want to do this is framing. If you're transmitting a series of messages over a stream, you need some way to tell where the messages begin and end. There are many ways to do this -- such as by transmitting a length before every message -- but most of them don't support sync recovery. Sync recovery lets a receiver tune in anywhere in a stream and figure out (correctly) where the next message boundary is. The easiest way to provide sync recovery is to use a marker at the beginning/end of each message that you can reliably tell apart from the data in the messages. To find message boundaries in an arbitrary data stream, you only need to hunt for the end of the current message and start parsing from there. COBS can do this by ensuring that the message terminator character (0) only appears between messages.

Unlike a lot of framing methods (particularly SLIP), COBS guarantees an upper bound to the size of the encoded output: the original length, plus two bytes, plus one byte per 254 input bytes. corncobs provides the max_encoded_len function for sizing buffers to allow for worst-case encoding overhead, at compile time.

When to use this implementation of COBS

I wrote corncobs for an art project that required streaming video over a 3-10 Mbit/s RS485 link on an 80MHz Cortex-M4. Its performance is more than sufficient for this task.

At the time, I had identified two main alternatives: cobs-rs and postcard-cobs. (Note: postcard-cobs says it's a fork of cobs-rs, but it isn't, it's a fork of cobs. This confused me too.) They didn't quite work for my application:

  • At about 8 CPU cycles per incoming bit, I needed decoding to be very, very fast. corncobs decoding is about 60x faster and met my needs. (Encode is about 3x faster. Both numbers are for non-pathological data, i.e. not all zeroes. See the benchmark suite for details.)

  • I was receiving messages in a circular buffer via DMA, where they'd be concatenated but separated with zeroes. This meant I couldn't express the length of incoming messages at compile time, and I needed an exact number of bytes consumed for each message, both of which made using cobs-rs difficult.

  • I was operating in an environment where data transmission was not perfectly reliable, and needed the firmware to recover gracefully from corruption or lost data, i.e. not panic. This made using postcard-cobs difficult. (They don't have a public bug tracker, so I was unable to report the panics.)

However, corncobs and postcard-cobs are compatible; the tests/compat.rs test suite in corncobs proves this. (Note that you need to make sure to strip trailing zeroes before handing data to postcard-cobs to avoid panics.) corncobs is also mostly compatible with cobs-rs with the exception of the encoding of empty messages, an area where I think cobs-rs has a bug. So, you can mix and match -- if you would like the slower-but-more-predictable encoding performance of postcard-cobs and the faster decode of corncobs, go for it.

The performance tests I used to reach these conclusions are checked in. I keep an eye on them, in case I can stop maintaining my own crate some day. :-) You can run them with:

$ cargo bench comparison

Tips for using COBS

If you're designing a protocol or message format and considering using COBS, you have some options.

Optimizing for size: COBS encoding has the least overhead when the data being encoded contains 0x00 bytes, at least one for every 254 bytes sent. In practice, most data formats achieve this. However...

Optimizing for speed: COBS encode/decode, and particularly the corncobs implementation, goes fastest when data contains as few 0x00 bytes as possible -- ideally none. If you can adjust the data you're encoding to avoid zero, you can achieve higher encode/decode rates. For instance, in one of my projects that sends RGB video data, I just declared that red/green/blue value 1 is the same as 0, and made all the 0s into 1s, for a large performance improvement.

Running the tests and stuff

For my future self, when I have forgotten the incantations. Or for you!

Tests: cargo test

Benchmarks: cargo bench (easy enough so far)

Fuzzing:

cargo install honggfuzz
cargo hfuzz run encode  # or...
cargo hfuzz run decode

No runtime deps