Skip to main content

Consistent Overhead Byte Stuffing (COBS)

Project description

Author:

Craig McQueen

Contact:
http://craig.mcqueen.id.au/

Python functions for encoding and decoding COBS.

Intro

The cobs package is provided, which contains modules containing functions for encoding and decoding according to COBS methods.

What Is 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.

For more details about COBS, see the references [1] [2].

I have included a variant on COBS, COBS/R, which slightly modifies COBS to often avoid the +1 byte overhead of COBS. So in many cases, especially for smaller packets, the size of a COBS/R encoded packet is the same size as the original packet. See below for more details about COBS/R.

References

Modules Provided

Module

Short Name

Long Name

cobs.cobs

COBS

Consistent Overhead Byte Stuffing (basic method) [1]

cobs.cobsr

COBS/R

Consistent Overhead Byte Stuffing–Reduced

Consistent Overhead Byte Stuffing–Reduced” (COBS/R) is my own invention, a modification of basic COBS encoding, and is described in more detail below.

The following are not implemented:

Short Name

Long Name

COBS/ZPE

Consistent Overhead Byte Stuffing–Zero Pair Elimination [1]

COBS/ZRE

Consistent Overhead Byte Stuffing–Zero Run Elimination [2]

A pure Python implementation and a C extension implementation are provided. If the C extension is not available for some reason, the pure Python version will be used.

Usage

The modules provide an encode and a decode function.

In Python 2.x, the input should be a byte string. Basic usage (example in Python 2.x):

>>> from cobs import cobs
>>> encoded = cobs.encode('Hello world\x00This is a test')
>>> encoded
'\x0cHello world\x0fThis is a test'
>>> cobs.decode(encoded)
'Hello world\x00This is a test'

COBS/R usage is almost identical:

>>> from cobs import cobsr
>>> encoded = cobsr.encode('Hello world\x00This is a test')
>>> encoded
'\x0cHello worldtThis is a tes'
>>> cobsr.decode(encoded)
'Hello world\x00This is a test'

For Python 3.x, input cannot be Unicode strings. Byte strings are acceptable input. Any type that implements the buffer protocol, providing a single block of bytes, is also acceptable as input:

>>> from cobs import cobs
>>> encoded = cobs.encode(bytearray(b'Hello world\x00This is a test'))
>>> encoded
b'\x0cHello world\x0fThis is a test'
>>> cobs.decode(encoded)
b'Hello world\x00This is a test'

Supported Python Versions

Python >= 2.4 and 3.x are supported, and have both a C extension and a pure Python implementation.

Python versions < 2.4 might work, but have not been tested. Python 3.0 has also not been tested.

Installation

The cobs package is installed using distutils. If you have the tools installed to build a Python extension module, run the following command:

python setup.py install

If you cannot build the C extension, you may install just the pure Python implementation, using the following command:

python setup.py build_py install --skip-build

Unit Testing

Basic unit testing is in the test sub-module, e.g. cobs.cobs.test. To run it on Python >=2.5:

python -m cobs.cobs.test
python -m cobs.cobsr.test

Alternatively, in the test directory run:

python test_cobs.py
python test_cobsr.py

Documentation

Documentation is written with Sphinx. Source files are provided in the doc directory. It can be built using Sphinx 0.6.5. It uses the pngmath Sphinx extension, which requires Latex and dvipng to be installed.

The documentation is available online at: http://packages.python.org/cobs/

License

The code is released under the MIT license. See LICENSE.txt for details.

Consistent Overhead Byte Stuffing–Reduced (COBS/R)

A modification of COBS, which I’m calling “Consistent Overhead Byte Stuffing–Reduced” (COBS/R), is provided in the cobs.cobsr module. Its purpose is to save one byte from the encoded form in some cases. Plain COBS encoding always has a +1 byte encoding overhead. See the references for details [1]. COBS/R can often avoid the +1 byte, which can be a useful savings if it is mostly small messages that are being encoded.

In plain COBS, the last length code byte in the message has some inherent redundancy: if it is greater than the number of remaining bytes, this is detected as an error.

In COBS/R, instead we opportunistically replace the final length code byte with the final data byte, whenever the value of the final data byte is greater than or equal to what the final length value would normally be. This variation can be unambiguously decoded: the decoder notices that the length code is greater than the number of remaining bytes.

Examples

The byte values in the examples are in hex.

First example:

Input:

2F

A2

00

92

73

02

This example is encoded the same in COBS and COBS/R. Encoded (length code bytes are bold):

03

2F

A2

04

92

73

02

Second example:

The second example is almost the same, except the final data byte value is greater than what the length byte would be.

Input:

2F

A2

00

92

73

26

Encoded in plain COBS (length code bytes are bold):

03

2F

A2

04

92

73

26

Encoded in COBS/R:

03

2F

A2

26

92

73

Because the last data byte (26) is greater than the usual length code (04), the last data byte can be inserted in place of the length code, and removed from the end of the sequence. This avoids the usual +1 byte overhead of the COBS encoding.

The decoder detects this variation on the encoding simply by detecting that the length code is greater than the number of remaining bytes. That situation would be a decoding error in regular COBS, but in COBS/R it is used to save one byte in the encoded message.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cobs-1.1.4.tar.gz (20.1 kB view details)

Uploaded Source

Built Distributions

cobs-1.1.4-cp310-cp310-win_amd64.whl (25.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

cobs-1.1.4-cp310-cp310-win32.whl (24.4 kB view details)

Uploaded CPython 3.10 Windows x86

cobs-1.1.4-cp39-cp39-win_amd64.whl (25.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

cobs-1.1.4-cp39-cp39-win32.whl (24.3 kB view details)

Uploaded CPython 3.9 Windows x86

cobs-1.1.4-cp38-cp38-win_amd64.whl (25.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

cobs-1.1.4-cp38-cp38-win32.whl (24.4 kB view details)

Uploaded CPython 3.8 Windows x86

File details

Details for the file cobs-1.1.4.tar.gz.

File metadata

  • Download URL: cobs-1.1.4.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for cobs-1.1.4.tar.gz
Algorithm Hash digest
SHA256 c4fc36e3e1d3ff57670982a1b0856ca15255ab56c73c6f915ed6a45b51fa341c
MD5 0dbfbf6e5a0f33660a48b1154d0179b8
BLAKE2b-256 4649e49f943433faae9e6b6e802a04f1004b0013bca2cafeb1f3c6a13b1deb18

See more details on using hashes here.

File details

Details for the file cobs-1.1.4-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: cobs-1.1.4-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0rc2

File hashes

Hashes for cobs-1.1.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6cf5e31b2b56d7601486d6c46e10c25c92a77f786bdd9b5dc59fc94bb261771e
MD5 3a3df06bead3cde3ad986994d2c8f264
BLAKE2b-256 db0ce459ebc60ad421f558eaa52cdf677d43b365ec1fdf56c403f8626ab8e087

See more details on using hashes here.

File details

Details for the file cobs-1.1.4-cp310-cp310-win32.whl.

File metadata

  • Download URL: cobs-1.1.4-cp310-cp310-win32.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: CPython 3.10, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0rc2

File hashes

Hashes for cobs-1.1.4-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 095639889cc9ec5799bd4d416ba06d503dddb45bc648cfd7cac63cb8503e0db3
MD5 3cb956751db906a4dcb1409cabc5fc6f
BLAKE2b-256 988bd672fdfb6d41ea23b2a4d2846ca8ad6e13184045d3689286c45dd8461932

See more details on using hashes here.

File details

Details for the file cobs-1.1.4-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: cobs-1.1.4-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for cobs-1.1.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 e1c8c527c777f8b6c58bce249a0de7b8b6d9f85aca751fad556b395d983715ce
MD5 47dd5aadf44e7af58a7361359feb06af
BLAKE2b-256 6fb05bc9ab0c056fea3982a9928132af66ac8387892023e5867732f35cdd90e3

See more details on using hashes here.

File details

Details for the file cobs-1.1.4-cp39-cp39-win32.whl.

File metadata

  • Download URL: cobs-1.1.4-cp39-cp39-win32.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for cobs-1.1.4-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 0a6c55ec46789b8b01cc6bfcf562f6f75082f5baef1ecf1384440769aba4b016
MD5 12139c0ec6a27429cb4d2e90d35df74f
BLAKE2b-256 3d56fd040f1636b61d4a448f28bc3cf92b45d45d0b208bc4313c698087d54819

See more details on using hashes here.

File details

Details for the file cobs-1.1.4-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: cobs-1.1.4-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for cobs-1.1.4-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a0ee59869cd99133ab3febaa810832dbda4bdc87f7f4b31d0d66e1a2709bfeaf
MD5 ece80ea243754511dfc32b9aa1df13a8
BLAKE2b-256 5291602c69e62805d3bf82466c72ce2200354b8412de3d886f1f342b4421460a

See more details on using hashes here.

File details

Details for the file cobs-1.1.4-cp38-cp38-win32.whl.

File metadata

  • Download URL: cobs-1.1.4-cp38-cp38-win32.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for cobs-1.1.4-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 d32931d7250a3c57989bd53e99fd7d3c3200affcbf60d4e2470ffd29af99e45d
MD5 e278817c81faf6047dc81fd911779ca3
BLAKE2b-256 5ce41150c902692a4aa0418afe388b7bca88825c00b1ccd9996f2ef0ac743ab8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page