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. Byte stuffing is a process that transforms a sequence of data bytes that may contain 'illegal'... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
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. 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. HDLC framing is a well-known example, used particularly in PPP (see RFC 1662 § 4.2). Although HDLC framing has an overhead of [OHB] : Overhead byte (Start of frame) 3+ -------------->| : Points to relative location of first zero symbol 2+-------->| : Is a zero data byte, pointing to next zero symbol [EOP] : Location of end-of-packet zero symbol. 0 1 2 3 4 5 : Byte Position 03 11 22 02 33 00 : COBS Data Frame 11 22 00 33 : Extracted Data OHB = Overhead Byte (Points to next zero symbol) EOP = End Of Packet Examples 7 through 10 show how the overhead varies depending on the data being encoded for packet lengths of 255 or more. ==Implementation== The following code implements a COBS encoder and decoder in the C programming language, processing data byte by byte. include include include /** COBS encode data to buffer @param data Pointer to input data to encode @param length Number of bytes to encode @param buffer Pointer to encoded output buffer @return Encoded buffer length in bytes @note Does not output delimiter byte / size_t cobsEncode(const void *data, size_t length, uint8_t *buffer) { assert(data && buffer); uint8_t *encode = buffer; // Encoded byte pointer uint8_t *codep = encode++; // Output code pointer uint8_t code = 1; // Code value for (const uint8_t *byte = (const uint8_t *)data; length--; ++byte) { if (*byte) // Byte not zero, write it *encode++ = *byte, ++code; if (!*byte || code == 0xff) // Input is zero or block completed, restart { *codep = code, code = 1, codep = encode; if (!*byte || length) ++encode; } } *codep = code; // Write final code value return (size_t)(encode - buffer); } /** COBS decode data from buffer @param buffer Pointer to encoded input bytes @param length Number of bytes to decode @param data Pointer to decoded output data @return Number of bytes successfully decoded @note Stops decoding if delimiter byte is found / size_t cobsDecode(const uint8_t *buffer, size_t length, void *data) { assert(buffer && data); const uint8_t *byte = buffer; // Encoded input byte pointer uint8_t *decode = (uint8_t *)data; // Decoded output byte pointer for (uint8_t code = 0xff, block = 0; byte ==References== ==External links== Python implementation Alternate C implementation Another implementation in C Consistent Overhead Byte Stuffing—Reduced (COBS/R) A patent describing a scheme with a similar result but using a different method
Cite this
"Consistent Overhead Byte Stuffing". 1774257586. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1344930661 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy