Content deleted Content added
→Encoding examples: Removing additional 01 byte in example 6: not covered by algorithm as described; following the latter, the byte represents an additional trailing 0 in the message, falsifying the msg and expanding it illegally to 255 bytes. |
mNo edit summary |
||
(43 intermediate revisions by 27 users not shown) | |||
Line 1:
{{Short description|Algorithm for encoding data bytes}}
'''Consistent Overhead Byte Stuffing''' ('''COBS''') is an [[algorithm]] for encoding data bytes that results in efficient, reliable, unambiguous [[Packet (information technology)#Packet framing|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 (computing)|overhead]] of the algorithm. [[High-Level_Data_Link_Control#Asynchronous_framing|HDLC framing]] is a well-known example, used particularly in [[Point-to-point protocol|PPP]] (see [https://datatracker.ietf.org/doc/html/rfc1662#section-4.2 RFC 1662 § 4.2]). 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.
{{Cite journal
| last1 = Cheshire | first1 = Stuart
| last2 = Baker | first2 = Mary
| authorlink = Stuart Cheshire
| title = Consistent Overhead Byte Stuffing
|
| volume = 7
| pages = 159–172
| number = 2
|
| doi = 10.1109/90.769765
| url = http://www.stuartcheshire.org/papers/COBSforToN.pdf
|
| s2cid = 47267776
}}
</ref><ref>
{{Cite
|
| last2 = Baker | first2 = Mary
| authorlink = Stuart Cheshire
| title = Consistent Overhead Byte Stuffing
| conference = [[Association for Computing Machinery|ACM]] [[SIGCOMM]] '97
| url = http://conferences.sigcomm.org/sigcomm/1997/papers/p062.pdf
|
| location = [[Cannes]]
| access-date = November 23, 2010 }}
</ref>
COBS does, however, require up to 254 bytes of ''lookahead''. Before transmitting its first byte, it needs to know the position of the first zero byte (if any) in the following 254 bytes.
A 1999 [[Internet Draft]] proposed to standardize COBS as an alternative for HDLC framing in [[Point-to-point protocol|PPP]], due to the aforementioned poor worst-case overhead of HDLC framing.<ref name="ietf-draft">{{cite ietf|draft=draft-ietf-pppext-cobs-00.txt|title=PPP Consistent Overhead Byte Stuffing (COBS)|last1=Carlson|first1=James|last2=Cheshire|first2=Stuart|authorlink2=Stuart Cheshire|last3=Baker|first3=Mary|date=November 1997}}</ref>
==Packet framing and stuffing==
When packetized data is sent over any serial medium,
COBS transforms
(Any other byte value may be reserved as the packet delimiter, but using zero simplifies the description.)
[[File:Cobs encoding with example.png|center|frameless|800px|Consistent Overhead Byte Stuffing (COBS) encoding process]]
There are two equivalent ways to describe the COBS encoding process:
; Prefixed block description
: To encode some bytes, first append a zero byte, then break them into groups of either 254 non-zero bytes, or 0–253 non-zero bytes followed by a zero byte. Because of the appended zero byte, this is always possible.
: Encode each group by deleting the trailing zero byte (if any) and prepending the number of non-zero bytes, plus one. Thus, each encoded group is the same size as the original, except that 254 non-zero bytes are encoded into 255 bytes by prepending a byte of 255.
: As a special exception, if a packet ends with a group of 254 non-zero bytes, it is not necessary to add the trailing zero byte. This saves one byte in some situations.
; Linked list description
: First, insert a zero byte at the beginning of the packet, and after every run of 254 non-zero bytes. This encoding is obviously reversible. It is not necessary to insert a zero byte at the end of the packet if it happens to end with exactly 254 non-zero bytes.
: Second, replace each zero byte with the offset to the next zero byte, or the end of the packet. Because of the extra zeros added in the first step, each offset is guaranteed to be at most 255.
==Encoding examples==
These examples show how various data sequences would be encoded by the COBS algorithm. In the examples, all bytes are expressed as [[hexadecimal]] values, and encoded data is shown with text formatting to illustrate various features:
* '''Bold''' indicates a data byte which has not been altered by encoding. All non-zero data bytes remain unaltered.
* {{green|Green}} indicates a zero data byte that was altered by encoding. All zero data bytes are replaced during encoding by the offset to the following zero byte (i.e. one plus the number of non-zero bytes that follow). It is effectively a pointer to the next packet byte that requires interpretation: if the addressed byte is non-zero then it is the following {{green|group header byte zero data byte}} that points to the next byte requiring interpretation; if the addressed byte is zero then it is the {{blue|end of packet}}.
* {{red|Red}} is an overhead byte which is also a group header byte containing an offset to a following group, but does not correspond to a data byte. These appear in two places: at the beginning of every encoded packet, and after every group of 254 non-zero bytes.
* A {{blue|blue}} zero byte appears at the end of every packet to indicate end-of-packet to the data receiver. This packet delimiter byte is not part of COBS proper; it is an additional framing byte that is appended to the encoded output.
{| class="wikitable"
|-
! Example !! Unencoded data (hex) !! Encoded with COBS (hex)
|-
| 1 ||
|-
| 2 ||
|-
| 3 ||
|-
| 4 ||
|-
| 5 ||
|-
| 6 ||
|-
| 7 || {{mono|01 02 03 ... FD FE}} || {{mono|{{red|FF}} '''01 02 03 ... FD FE''' {{blue|00}}}}
|-
| 8 || {{mono|00 01 02 ... FC FD FE}} || {{mono|{{red|01}} {{green|FF}} '''01 02 ... FC FD FE''' {{blue|00}}}}
|-
| 9 || {{mono|01 02 03 ... FD FE FF}} || {{mono|{{red|FF}} '''01 02 03 ... FD FE''' {{red|02}} '''FF''' {{blue|00}}}}
|-
| 10 || {{mono|02 03 04 ... FE FF 00}} || {{mono|{{red|FF}} '''02 03 04 ... FE FF''' {{red|01}} {{green|01}} {{blue|00}}}}
|-
| 11 || {{mono|03 04 05 ... FF 00 01}} || {{mono|{{red|FE}} '''03 04 05 ... FF''' {{green|02}} '''01''' {{blue|00}}}}
|}
Below is a diagram using example
[OHB] : Overhead byte (Start of frame)
3+ -------------->| : Points to relative location of first zero symbol
Line 72 ⟶ 102:
OHB = Overhead Byte (Points to next zero symbol)
EOP = End Of Packet
</pre
Examples 7 through 10 show how the overhead varies depending on the data being encoded for packet lengths of 255 or more.
==Implementation==
Line 79 ⟶ 111:
<syntaxhighlight lang="c">
#include <stddef.h>
#include <stdint.h>
#include <assert.h>
/** 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 < buffer + length; --block)
{
if (block) // Decode block byte
*decode++ = *byte++;
else
{
block = *byte++; // Fetch the next block length
if (block && (code != 0xff)) // Encoded zero, write it unless it's delimiter.
*decode++ = 0;
code = block;
if (!code) // Delimiter code found
break;
}
}
return (size_t)(decode - (uint8_t *)data);
}
</syntaxhighlight>
== See also ==
* [[Bit stuffing]]
* [[Serial Line Internet Protocol]]
==References==
Line 160 ⟶ 194:
* [https://pypi.python.org/pypi/cobs Python implementation]
* [https://github.com/cmcqueen/cobs-c Alternate C implementation]
* [https://web.archive.org/web/20180719005741/http://www.jacquesf.com/2011/03/consistent-overhead-byte-stuffing/ Another implementation in C]
* [https://pythonhosted.org/cobs/cobsr-intro.html Consistent Overhead Byte Stuffing—Reduced (COBS/R)]
* [https://patents.google.com/patent/US9438411B1 A patent describing a scheme with a similar result but using a different method]
[[Category:Encodings]]
|