*** Welcome to piglix ***

Consistent Overhead Byte Stuffing


Consistent Overhead Byte Stuffing (COBS) is an algorithm for encoding data bytes that results in efficient, reliable, unambiguous packet framing regardless of packet content, thus making it easy for receiving applications to recover from malformed packets. It employs a particular byte value, typically zero, to serve as a packet delimiter (a special value that indicates the boundary between packets). When zero is used as a delimiter, the algorithm replaces each zero data byte with a non-zero value so that no zero data bytes will appear in the packet and thus be misinterpreted as packet boundaries. The value substituted for each zero data byte is equal to one plus the number of non-zero data bytes that follow.

Byte stuffing is a process that transforms a sequence of data bytes that may contain 'illegal' or 'reserved' values (such as packet delimiter) into a potentially longer sequence that contains no occurrences of those values. The extra length of the transformed sequence is typically referred to as the overhead of the algorithm. The COBS algorithm tightly bounds the worst-case overhead, limiting it to no more than one byte in 254. The algorithm is computationally inexpensive and its average overhead is low compared to other unambiguous framing algorithms.

When packetized data is sent over any serial medium, a is needed by which to demarcate packet boundaries. This is done by using a framing marker, which is a special bit-sequence or character value that indicates where the boundaries between packets fall. Data stuffing is the process that transforms the packet data before transmission to eliminate all occurrences of the framing marker, so that when the receiver detects a marker, it can be certain that the marker indicates a boundary between packets.

COBS transforms a data set of up to 254 bytes in the range [0,255] into bytes in the range [1,255]. Having eliminated all zero bytes from the data, a zero byte can now be used to unambiguously mark the end of the transformed data. This is done by appending a zero byte to the transformed data, and thus forming a packet consisting of the COBS-encoded data (the payload) and the zero byte, end-of-packet marker.

The overhead of COBS encoding is constant regardless of data content. Every data set is encoded with an overhead of exactly one byte, so that N bytes are always transformed into exactly N+1 encoded bytes. The overhead byte, which equals one plus the number of non-zero bytes that follow, appears at the beginning of the encoded data. Note that the overhead byte is not a transformed data byte; it is an additional byte that precedes the transformed data bytes.


...
Wikipedia

...