Skip to main content

Consistent Overhead Byte Stuffing (COBS) backed by the corncobs Rust crate

Project description

corncobs

corncobs is a high-performance Python wrapper around the Rust crate corncobs, providing fast and allocation-efficient implementations of Consistent Overhead Byte Stuffing (COBS) encoding and decoding.

COBS ensures that arbitrary binary data can be framed without ambiguity by eliminating all zero bytes—making it ideal for serial protocols, embedded systems, and any application where reliable packet boundaries matter.

By leveraging Rust’s speed and safety guarantees, corncobs delivers significantly faster performance than pure-Python alternatives while maintaining a clean, Pythonic API.

Features

  • 🚀 Blazing-fast COBS encoding/decoding powered by Rust
  • 🛡️ Memory-safe and efficient: minimal allocations, predictable overhead
  • 🐍 Simple, no-frills API for basic COBS encoding and decoding, with an option for incremental decoding for streaming data
  • 🔧 Drop-in replacement for the cobs.cobs module of cobs itself. All unit tests of cobs also pass with corncobs.
  • 🧪 Fully tested and validated

Installation

pip install corncobs

Wheels are provided for major platforms. Building from source requires a Rust toolchain.

Usage

from corncobs import encode, decode

data = b"\x00\x11\x22\x00\x33"
encoded = encode(data)
decoded = decode(encoded)

assert decoded == data

For cases when the encoded data is guaranteed to be valid COBS-encoded data (or when you have other mechanisms suchs as checksums to detect data corruption), you can disable strict mode in the decoder for a slight performance boost:

decoded = decode(encoded, strict=False)

If you have an input stream that produces chunks of bytes to be decoded, you can build an incremental decoder object that keeps track of partially decoded messages. You can feed single bytes into the decoder with its advance() method, or entire chunks with its advance_many() method. advance() returns the newly parsed message in response to a zero byte, or None if more bytes are needed:

from corncobs import Decoder

decoder = Decoder()
encoded = b"\x051234"
for ch in encoded:
    assert decoder.advance(ch) is None
assert decoder.advance(0) == b"1234"

Note that the decoder does not return the message until it receives the terminating null byte as it cannot know whether new bytes would arrive to extend the current message or not.

advance_many() accepts entire chunks at once and returns a list of messages parsed from the chunk. You can also retrieve the collected-but-not-yet-returned bytes from the decoder with its pending property, or the number of pending bytes with its num_pending property (which is more efficient because you do not need to copy bytes from the Rust side to the Python side):

decoder = Decoder()
encoded = b"\x051234\x00\x07abcdef\x00"
assert decoder.advance_many(encoded[:9]) == [b"1234"]
assert decoder.pending == b"ab"
assert decoder.num_pending == 2
assert decoder.advance_many(encoded[9:]) == [b"abcdef"]
assert not decoder.pending

advance_many() is equivalent to calling the decoder as if it was a function, so you can write code like this (even with async iterators):

for chunk in stream:
    for message in decoder(chunk):
        print(message.hex(" "))

Decoders are strict by default, but you can also disable strict mode for a slight performance boost. In non-strict mode, decoders will assume that they receive valid COBS-encoded input and may freely copy bytes from the input to the output until the next overhead byte without checking that the copied chunk contains no zeros. In this case, you should also set an upper limit on the number of pending bytes with the max_length property to drive the parser into an error state if it seems like the current parsed message would be too long (which can happen for erroneous inputs):

decoder = Decoder(strict=False)
assert decoder(
    b"this is an invalid\x00message in COBS because it has an extra "
    b"null byte in the middle"
) == []

decoder = Decoder(max_length=10, strict=False)
assert decoder(
    b"this is an invalid\x00message in COBS because it has an extra "
    b"null byte in the middle. But look, there is a valid message "
    b"at the end:\x00\x051234\x00"
) == [b"1234"]

Project Status

corncobs is stable and production-ready.

License

corncobs is licensed under the Mozilla Public License, version 2.0, matching the upstream Rust project.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

corncobs-1.0.3-cp310-abi3-win_amd64.whl (120.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

corncobs-1.0.3-cp310-abi3-win32.whl (118.4 kB view details)

Uploaded CPython 3.10+Windows x86

corncobs-1.0.3-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (248.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

corncobs-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl (226.2 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

corncobs-1.0.3-cp310-abi3-macosx_10_12_universal2.whl (431.0 kB view details)

Uploaded CPython 3.10+macOS 10.12+ universal2 (ARM64, x86-64)

File details

Details for the file corncobs-1.0.3-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: corncobs-1.0.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 120.2 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for corncobs-1.0.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b082240f6f8ec4c9d967ba7d98e9192f79d971b4da0bdab163e1bc92f4bd2621
MD5 c1737e5aac9edc99130cdc27d1e6eb1d
BLAKE2b-256 a7dbfc0a7fb7cc7400338d9d2737c4138599ca3b6c1416244e7d9563c5ea6741

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.3-cp310-abi3-win_amd64.whl:

Publisher: build.yml on ntamas/corncobs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file corncobs-1.0.3-cp310-abi3-win32.whl.

File metadata

  • Download URL: corncobs-1.0.3-cp310-abi3-win32.whl
  • Upload date:
  • Size: 118.4 kB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for corncobs-1.0.3-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 fbfebfdbcf340d2b3fa224b72ed9098246aedd46017590a4769eefb3e2e7d601
MD5 fd9482c34e0e620cda156941946def04
BLAKE2b-256 40183263b37ddd6860a309d0bfe634df7b2851a2824bd9e31ad21a23d1fa9923

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.3-cp310-abi3-win32.whl:

Publisher: build.yml on ntamas/corncobs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file corncobs-1.0.3-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for corncobs-1.0.3-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3d2b354535bf4a66e2444c66dbdb109a39cb152abcab06dde550709e517d0e13
MD5 99d6db4e2127aed42e1baa3b4d4540a8
BLAKE2b-256 f370de24939d848a9c2a809041ab6d09caf73f9c03ce2999926bc51d35b67fd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.3-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl:

Publisher: build.yml on ntamas/corncobs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file corncobs-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for corncobs-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 41214436bb16b12fc34599b1762ca3cdd23c0e1cde9dccd2a4a6d025830122c3
MD5 a027293ae2e2ccb5147b17626f59125c
BLAKE2b-256 d3bc60b743958ae9ad1fd81c726118fefa3af3b6a77e72a942c97a41373a9460

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: build.yml on ntamas/corncobs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file corncobs-1.0.3-cp310-abi3-macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for corncobs-1.0.3-cp310-abi3-macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 0867fe1ec07dcb1b65f556a731a85b47049a1dac3fcf5b30f69a6acefa34d711
MD5 bf66f8a20dc4718ee2be6e2feede627c
BLAKE2b-256 8175ad58208bdcfd4ee432657de20344e815bd4e2ef2d8456ce754a3af08c58b

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.3-cp310-abi3-macosx_10_12_universal2.whl:

Publisher: build.yml on ntamas/corncobs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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