COBS encoding
Remove every zero byte from a packet with one byte of overhead. A hard embedded C problem (Protocols, framing and checksums), run and graded in the browser.
hard · Protocols, framing and checksums · framing · cobs
Consistent Overhead Byte Stuffing rewrites a packet so that it contains no zero bytes, which frees 0 to serve as the frame delimiter.
The output is a series of blocks. Each block starts with a code byte, followed by code - 1 data bytes, none of them zero. A code byte says how far ahead the next zero of the original data is: after the block's data bytes, the original had a zero (except at the very end of the packet).
Encode the n bytes at in (n is at most 100) into out and return the encoded length. Do not append a delimiter. Examples: 11 22 00 33 encodes to 03 11 22 02 33, and a single 00 encodes to 01 01.
Starter code
size_t cobs_encode(const uint8_t *in, size_t n, uint8_t *out) {
// Reserve a code byte, copy non-zero bytes after it, and fill the code in when a zero is reached.
return 0;
}Example tests
A zero in the middle
const uint8_t in[] = { 0x11, 0x22, 0x00, 0x33 };
uint8_t out[8];
size_t n = cobs_encode(in, sizeof in, out);
for (size_t i = 0; i < n; i++) printf("%02X ", out[i]);
printf("\n");Expected output:
03 11 22 02 33No zeros at all
const uint8_t in[] = { 0x11, 0x22, 0x33, 0x44 };
uint8_t out[8];
size_t n = cobs_encode(in, sizeof in, out);
for (size_t i = 0; i < n; i++) printf("%02X ", out[i]);
printf("\n");Expected output:
05 11 22 33 44Learn it first
- Protocols, framing and checksums · Checksums, CRCs, byte order, framing and parsing a byte stream.
More Protocols, framing and checksums problems
- XOR checksum · Compute the XOR of every byte in a buffer.
- Two's-complement checksum · Compute the checksum byte of an Intel HEX record.
- CRC-8 · Implement a bitwise CRC-8 with polynomial 0x07.
- CRC-16 for Modbus · Implement the reflected CRC-16 used by Modbus RTU.
- Read a big-endian word · Assemble a 32-bit value from four bytes of a packet.
- I2C address byte · Build the first byte of an I2C transfer.
- BCD conversion · Convert between binary-coded decimal and binary.
- Build a UART frame · Assemble the bits a UART sends for one byte with parity.
- Build a framed message · Wrap a payload with a start byte, a length and a checksum.
- SLIP byte stuffing · Escape reserved bytes so a frame delimiter is unambiguous.
- Variable-length integer · Encode a 32-bit value in as few bytes as it needs.
- Validate an NMEA sentence · Check the checksum of a GPS sentence.
- Byte-stream frame parser · Reassemble frames from a byte stream one byte at a time.
- Fix: the bootloader hangs on large chunks · A loop counter is too narrow for the length it counts to.
- Fix: frame lengths come out 256 times too big · A big-endian length field is decoded as little-endian.
- Reliable delivery over a lossy link · Stop-and-wait with a sequence bit, acknowledgements, a retransmit timer and duplicate detection.
- Solar inverter: decode a status frame · Check a frame from the inverter and decode its power, voltage and temperature.