Skip to main content

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.

Metadata

Release files for corncobs 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for corncobs 1.0.3
File
corncobs-1.0.3-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
corncobs-1.0.3-cp310-abi3-win32.whl CPython 3.10 abi3 Windows x86-32 Details
corncobs-1.0.3-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
corncobs-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details
corncobs-1.0.3-cp310-abi3-macosx_10_12_universal2.whl CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64) Details

Total release size: 1.1 MB

Release files / corncobs-1.0.3-cp310-abi3-win_amd64.whl

Download URL corncobs-1.0.3-cp310-abi3-win_amd64.whl
Size 120.2 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
b082240f6f8ec4c9d967ba7d98e9192f79d971b4da0bdab163e1bc92f4bd2621
BLAKE2b-256 checksum
How to use checksums
a7dbfc0a7fb7cc7400338d9d2737c4138599ca3b6c1416244e7d9563c5ea6741
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.

Transparency log

Release files / corncobs-1.0.3-cp310-abi3-win32.whl

Download URL corncobs-1.0.3-cp310-abi3-win32.whl
Size 118.4 kB
Tags CPython 3.10 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
fbfebfdbcf340d2b3fa224b72ed9098246aedd46017590a4769eefb3e2e7d601
BLAKE2b-256 checksum
How to use checksums
40183263b37ddd6860a309d0bfe634df7b2851a2824bd9e31ad21a23d1fa9923
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.

Transparency log

Release files / corncobs-1.0.3-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL corncobs-1.0.3-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 248.1 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
3d2b354535bf4a66e2444c66dbdb109a39cb152abcab06dde550709e517d0e13
BLAKE2b-256 checksum
How to use checksums
f370de24939d848a9c2a809041ab6d09caf73f9c03ce2999926bc51d35b67fd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.

Transparency log

Release files / corncobs-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl

Download URL corncobs-1.0.3-cp310-abi3-macosx_10_12_x86_64.whl
Size 226.2 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
41214436bb16b12fc34599b1762ca3cdd23c0e1cde9dccd2a4a6d025830122c3
BLAKE2b-256 checksum
How to use checksums
d3bc60b743958ae9ad1fd81c726118fefa3af3b6a77e72a942c97a41373a9460
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.

Transparency log

Release files / corncobs-1.0.3-cp310-abi3-macosx_10_12_universal2.whl

Download URL corncobs-1.0.3-cp310-abi3-macosx_10_12_universal2.whl
Size 431.0 kB
Tags CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
0867fe1ec07dcb1b65f556a731a85b47049a1dac3fcf5b30f69a6acefa34d711
BLAKE2b-256 checksum
How to use checksums
8175ad58208bdcfd4ee432657de20344e815bd4e2ef2d8456ce754a3af08c58b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 27, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.3 This release

5 release files

1.0.2

5 release files

1.0.1

5 release files

1.0.0

5 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page