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.[src] It employs a particular byte value, typically zero, to serve as a packet delimiter (a special value that indicates the boundary between packets).[src] 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.[src] 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.[src] The extra length of the transformed sequence is typically referred to as the overhead of the algorithm.[src] HDLC framing is a well-known example, used particularly in PPP (see RFC 1662 § 4.2).[src] Although HDLC framing has an overhead of <1% in the average case, it suffers from a very poor worst-case overhead of 100%; for inputs that consist entirely of bytes that require escaping, HDLC byte stuffing will double the size of the input.[src] The COBS algorithm, on the other hand, tightly bounds the worst-case overhead.…[src]
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.[src] It employs a particular byte value, typically zero, to serve as a packet delimiter (a special value that indicates the boundary between packets).[src] 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.[src] 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.[src] The extra length of the transformed sequence is typically referred to as the overhead of the algorithm.[src] HDLC framing is a well-known example, used particularly in PPP (see RFC 1662 § 4.2).[src] 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.[src] 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.[src] ==Implementation== The following code implements a COBS encoder and decoder in the C programming language, processing data byte by byte.[src] 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[src]
18 sentences · 18 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Consistent Overhead Byte Stuffing." encyclopegia.org, 2026, https://encyclopegia.org/wiki/consistent-overhead-byte-stuffing. Accessed 28 November 2026.
Chicago
"Consistent Overhead Byte Stuffing." encyclopegia.org. Accessed November 28, 2026. https://encyclopegia.org/wiki/consistent-overhead-byte-stuffing.
APA
encyclopegia.org. (2026). Consistent Overhead Byte Stuffing. Retrieved November 28, 2026, from https://encyclopegia.org/wiki/consistent-overhead-byte-stuffing
Accessed November 28, 2026 · CC BY-SA 4.0 · citation page · JSON