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.

The input should be a byte string, not a Unicode string. Basic usage:

>>> from cobs import cobs
>>> encoded = cobs.encode(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'

COBS/R usage is almost identical:

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

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 >= 3.6 are supported, and have both a C extension and a pure Python implementation.

Python versions < 3.6 might work, but have 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:

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

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.2.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cobs-1.2.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

cobs-1.2.0-cp311-cp311-win_amd64.whl (25.7 kB view details)

Uploaded CPython 3.11Windows x86-64

cobs-1.2.0-cp311-cp311-win32.whl (24.3 kB view details)

Uploaded CPython 3.11Windows x86

cobs-1.2.0-cp310-cp310-win_amd64.whl (25.7 kB view details)

Uploaded CPython 3.10Windows x86-64

cobs-1.2.0-cp310-cp310-win32.whl (24.3 kB view details)

Uploaded CPython 3.10Windows x86

cobs-1.2.0-cp39-cp39-win_amd64.whl (25.7 kB view details)

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9Windows x86

cobs-1.2.0-cp38-cp38-win_amd64.whl (25.8 kB view details)

Uploaded CPython 3.8Windows x86-64

cobs-1.2.0-cp38-cp38-win32.whl (24.3 kB view details)

Uploaded CPython 3.8Windows x86

File details

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

File metadata

  • Download URL: cobs-1.2.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for cobs-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d93b10b5370dc8868960aefb70adb1f73a4e6107b16918307a4aeeefd3edb8f6
MD5 ee0b20abde664044abeb52488cee5bee
BLAKE2b-256 50919607e8013f96e1df6a4e17a8d7e1d72707ce131692af114152c9fb3a1f77

See more details on using hashes here.

File details

Details for the file cobs-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: cobs-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for cobs-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f2e8ef22070c3a95ed998303b81c209dd49aaaebf45173499a1a48b8b17bb6f
MD5 a95a1ad4b0b2125a8e132cfc2995ed46
BLAKE2b-256 1cb464db6f3ed33bb3851f7b2cfc163cc60932eb39674c2cd88f3c71477ddbfd

See more details on using hashes here.

File details

Details for the file cobs-1.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: cobs-1.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for cobs-1.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d0f7e7fcad3020676feef505a0212bcd0c86549d3e4c3680f6a7c77ef599e52b
MD5 9325a683c970f59db542696fb5ce2b7b
BLAKE2b-256 c54aa439c22c9a07d427764865d1a55e7184e04f77d69da79ca08516706a1645

See more details on using hashes here.

File details

Details for the file cobs-1.2.0-cp311-cp311-win32.whl.

File metadata

  • Download URL: cobs-1.2.0-cp311-cp311-win32.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for cobs-1.2.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 7c4a9e246c11f48e12c1231ad5738ea927f663b7b4ffd1de0be3b1cae5bb3884
MD5 c7d7122db72c4431ebc272aa432fdf83
BLAKE2b-256 dce563e7747879f8a40915f6b73302bd6279eb9450e0913e0e7b7857cb122a7a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cobs-1.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 25.7 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.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4625f7648e8b0ea50702cf213dcfcc0a50dc78fc6678b174f1f5e0d6fc3687c1
MD5 df42bdccdd0f072b573696f42ee295dd
BLAKE2b-256 968246d4316c70577d1f34e5b9867353a8c61fe9a301383876f604342486bbff

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cobs-1.2.0-cp310-cp310-win32.whl
  • Upload date:
  • Size: 24.3 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.2.0-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 991fc195f0581e456d3dd21e29df493fe9ca1b772084c26921601e48dbc34bd2
MD5 c95b4589c42060c7886ddb9b859f2e89
BLAKE2b-256 13ff591f62ae9d0432f9bb735eeea4911ea280a706cd961e903660fc863793e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cobs-1.2.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: CPython 3.9, 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.2.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c747c4b385f04203e1d2b767d61a69580b58c97eea7a6ed998edd42ddd9fcdc5
MD5 bbc7311b72808bca90fddd31cd86ce3e
BLAKE2b-256 5660e879fb1ba3f5f2c666d49c3db1596c7c549f7fd2385cf5a1621e0cea1f65

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cobs-1.2.0-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.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.2.0-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 b4af64be97f2a7215c3ffcd5cca26e6aa166e49cd90941efab95c76211b6710c
MD5 6c074cb7673b459f1aa87699adb80e3e
BLAKE2b-256 203ca7e3aeb22aaf041def0b5428c18c3e5d8a2d5c5543ac0cae12eaa4e83995

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cobs-1.2.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: CPython 3.8, 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.2.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 f68ee5e70ae6be79b424cb270835e864b86320a9fcb8048575443b658f61ee0a
MD5 8084fdf34427052a7b029efb5cab3d11
BLAKE2b-256 d4f98226d105ff6394ef908a279963781f60d239e1266c7231d810b0d8e5717e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cobs-1.2.0-cp38-cp38-win32.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: CPython 3.8, 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.2.0-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 cb573184d982edf8e7c3c08c753b765790790e7ffdca4c2ca5c258bbd9099f33
MD5 0475a19019ab3ae8583cf48d9e4397c7
BLAKE2b-256 5db9333db30a002dfa39953b0c188aee539ec31446cb8297f309a532c35c0cb0

See more details on using hashes here.

Supported by

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