Python algorithm demo for COBS
COBS is a method of encoding a packet of bytes into a form that contains no bytes with value zero (0x00). The input packet of bytes can contain bytes in the full range of 0x00 to 0xFF. The COBS encoded packet is guaranteed to generate packets with bytes only in the range 0x01 to 0xFF. Thus, in a communication protocol, packet boundaries can be reliably delimited with 0x00 bytes.
The COBS encoding does have to increase the packet size to achieve this encoding. However, compared to other byte-stuffing methods, the packet size increase is reasonable and predictable. COBS always adds 1 byte to the message length. Additionally, for longer packets of length n, it may add n/254 (rounded down) additional bytes to the encoded packet size.
For example, compare to the PPP protocol, which uses 0x7E bytes to delimit PPP packets. The PPP protocol uses an "escape" style of byte stuffing, replacing all occurences of 0x7E bytes in the packet with 0x7D 0x5E. But that byte-stuffing method can potentially double the size of the packet in the worst case. COBS uses a different method for byte-stuffing, which has a much more reasonable worst-case overhead.
Details in https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing .
Example | Unencoded data (hex) | Encoded with COBS (hex) | Encode Result | Decode Result |
---|---|---|---|---|
1 | 00 | 01 01 00 | Pass | Pass |
2 | 00 00 | 01 01 01 00 | Pass | Pass |
3 | 00 11 00 | 01 02 11 01 00 | Pass | Pass |
4 | 11 22 00 33 | 03 11 22 02 33 00 | Pass | Pass |
5 | 11 22 33 44 | 05 11 22 33 44 00 | Pass | Pass |
6 | 11 00 00 00 | 02 11 01 01 01 00 | Pass | Pass |
7 | 01 02 03 ... FD FE | FF 01 02 03 ... FD FE 00 | Pass | Pass |
8 | 00 01 02 ... FC FD FE | 01 FF 01 02 ... FC FD FE 00 | Pass | Pass |
9 | 01 02 03 ... FD FE FF | FF 01 02 03 ... FD FE 02 FF 00 | Pass | Pass |
10 | 02 03 04 ... FE FF 00 | FF 02 03 04 ... FE FF 01 01 00 | Pass | Pass |
11 | 03 04 05 ... FF 00 01 | FE 03 04 05 ... FF 02 01 00 | Pass | Pass |
The code is released under the MIT license. See LICENSE for details.
If you find this project useful or helpful to you, please give stars. Thanks