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

Uploaded CPython 3.10+Windows x86-64

corncobs-1.0.1-cp310-abi3-win32.whl (120.5 kB view details)

Uploaded CPython 3.10+Windows x86

corncobs-1.0.1-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.1-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.1-cp310-abi3-macosx_10_12_universal2.whl (433.6 kB view details)

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

File details

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

File metadata

  • Download URL: corncobs-1.0.1-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.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 fd5a1f47d64707ce383f73e45a3f314d20784177aa06a3d31201ffaabed6eb6f
MD5 561d21440e0bf86186c66cb3a014519a
BLAKE2b-256 f851b6f091041192447c34cb75f58004a5e8bc6890b33f5fd0bdea665caa93ca

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: corncobs-1.0.1-cp310-abi3-win32.whl
  • Upload date:
  • Size: 120.5 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.1-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 9c6cda31951e3dca116ca5537f26e91aedc8c31268b48e51ca61f31581dfa806
MD5 a8c8626786fe1e624a0442e6973f0c0b
BLAKE2b-256 43115dae15adca5aee056b2321c0bb3366e3a590454ec801dc24094bf443966d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for corncobs-1.0.1-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 08935cfafe2bea2469fdcb6387dca8b5282c6e0ef0a6b06bbf49468a2c07b0e0
MD5 bc8475312dba8affd996e35d178919c5
BLAKE2b-256 fa09d4b20a426f3ec374d4b020c38548dcc701aa22a3f490473ec28ce5faf5c4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for corncobs-1.0.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3a03364bae56bdb67850a47de16267e4720940a6dd16bcd221262a1938b38e37
MD5 ba7bb87cf8e5a67bc90044f6b6980528
BLAKE2b-256 449c7278de3e6f0fc1b33179462514a408d1dbafaff27d4bc2e3f4e1fda7655f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for corncobs-1.0.1-cp310-abi3-macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 a61e85d1134cd2734e53c0083070fa5a5a44ff11b271580e480cbc104965afd0
MD5 a0217d70960eb7ff12df2f36cd1f5144
BLAKE2b-256 c8ee1edc4419cc01342c47e84af3a13b937f4a1b52d928c014111e64852f18c7

See more details on using hashes here.

Provenance

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