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.2-cp310-abi3-win_amd64.whl (121.1 kB view details)

Uploaded CPython 3.10+Windows x86-64

corncobs-1.0.2-cp310-abi3-win32.whl (120.6 kB view details)

Uploaded CPython 3.10+Windows x86

corncobs-1.0.2-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (248.2 kB view details)

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

corncobs-1.0.2-cp310-abi3-macosx_10_12_x86_64.whl (227.6 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

corncobs-1.0.2-cp310-abi3-macosx_10_12_universal2.whl (433.7 kB view details)

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

File details

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

File metadata

  • Download URL: corncobs-1.0.2-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 121.1 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.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f21282e6dfe6ece9e2679126ec30cd7d7d3f5cab1e49705ee0bd4804f3807e11
MD5 d4c5b4483730cd33d06a24e99c96229a
BLAKE2b-256 2fc7503bae259eae1a828c6ed348730e0be2c5ebaa6500cebfb465dc44abbf13

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.2-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.2-cp310-abi3-win32.whl.

File metadata

  • Download URL: corncobs-1.0.2-cp310-abi3-win32.whl
  • Upload date:
  • Size: 120.6 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.2-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 ce8a80edc09499c6b1f2295515e1203f768fe7b721d251e729401d4c2e73116d
MD5 89b99108a4db62d47ff8d2e2bb9580c7
BLAKE2b-256 14c5d789c4ffdbb115168becc4b22eef5c2e664e9da29d33e8078c0fc929a06a

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.2-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.2-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for corncobs-1.0.2-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 635d4405bbe022aa79635cfb0cb3ac25414a6dfc2d61c04e5d9e0c16ad533e98
MD5 2cfc6e616b4678bc2fe5960f8297c61c
BLAKE2b-256 5934b6a5c625078a16716d6686394e293a78960c726ac8a0dfd29057e96df677

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.2-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.2-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for corncobs-1.0.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f08f3a923aa8e768f5bec4ad4ab49d85f2ea2824dc7fb8d366777252cf0281c6
MD5 7e7814017afe2100fdb2157d1f66317a
BLAKE2b-256 83fde31b42afb9b09ae38c53b044f930f8e596274536f334fde9507b7a9f3428

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.2-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.2-cp310-abi3-macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for corncobs-1.0.2-cp310-abi3-macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 cc27cc8442183c6b5b4869f61d7ec0dd936adbc3ccc49913a55b87766d4c768d
MD5 7c72197f784bcbe973520858e6c2c09c
BLAKE2b-256 29d23cd8b2bd9a55184e96ee729feafe906b05db4773085a017087722cf12f99

See more details on using hashes here.

Provenance

The following attestation bundles were made for corncobs-1.0.2-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