{"id":51421895,"url":"https://github.com/firechip/cobs_codec","last_synced_at":"2026-07-05T00:17:23.741Z","repository":{"id":369256489,"uuid":"1288594805","full_name":"firechip/cobs_codec","owner":"firechip","description":"Consistent Overhead Byte Stuffing (COBS) and COBS/R codecs for zero-free, low-overhead framing of serial, UART and packet byte streams in Dart \u0026 Flutter.","archived":false,"fork":false,"pushed_at":"2026-07-04T11:53:01.000Z","size":94,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-07-04T12:06:51.284Z","etag":null,"topics":["byte-stuffing","cobs","cobs-r","dart","embedded","encoding","flutter","framing","serial","serialization"],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/firechip.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-07-03T19:00:52.000Z","updated_at":"2026-07-04T11:53:05.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/firechip/cobs_codec","commit_stats":null,"previous_names":["firechip/cobs_codec"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/firechip/cobs_codec","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firechip%2Fcobs_codec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firechip%2Fcobs_codec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firechip%2Fcobs_codec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firechip%2Fcobs_codec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/firechip","download_url":"https://codeload.github.com/firechip/cobs_codec/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firechip%2Fcobs_codec/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35140187,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-04T02:00:05.987Z","response_time":113,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["byte-stuffing","cobs","cobs-r","dart","embedded","encoding","flutter","framing","serial","serialization"],"created_at":"2026-07-05T00:17:23.185Z","updated_at":"2026-07-05T00:17:23.722Z","avatar_url":"https://github.com/firechip.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cobs_codec\n\n[![CI](https://github.com/firechip/cobs_codec/actions/workflows/ci.yml/badge.svg)](https://github.com/firechip/cobs_codec/actions/workflows/ci.yml)\n[![pub package](https://img.shields.io/pub/v/cobs_codec.svg)](https://pub.dev/packages/cobs_codec)\n[![pub points](https://img.shields.io/pub/points/cobs_codec)](https://pub.dev/packages/cobs_codec/score)\n[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nFast, dependency-free **Consistent Overhead Byte Stuffing (COBS)** and **COBS/R**\ncodecs for Dart and Flutter, with first-class `dart:convert` integration and\nstream framing for serial links.\n\nCOBS encodes an arbitrary byte sequence into one that contains **no zero\n(`0x00`) bytes**, at a small and *predictable* cost — at most **one extra byte\nper 254 bytes**, plus one. That lets a single `0x00` reliably delimit packets on\na byte stream (serial/UART, USB-CDC, TCP, BLE, …), so a receiver can always\nresynchronise on the next `0x00` even after a corrupt packet. Unlike escape-based\nschemes such as PPP byte stuffing, COBS never doubles a packet's size.\n\n## Features\n\n- ⚡ **Basic COBS** and **COBS/R (Reduced)** — pick the standard scheme, or COBS/R\n  to shave the trailing overhead byte off small messages.\n- 🧩 **`dart:convert` native** — `cobs` and `cobsr` are `Codec`s, so they\n  `fuse`, `transform` streams, and compose like `json`/`utf8`/`base64`.\n- 🔌 **Stream framing built in** — turn a raw serial byte stream into a stream of\n  decoded packets with `CobsFrameDecoder`; chunk boundaries don't have to align\n  with frames.\n- 🚩 **Configurable delimiter (sentinel)** — encode and decode COBS *and* COBS/R\n  so the output avoids any chosen byte, not just `0x00`, letting that byte delimit\n  frames instead.\n- 🏷️ **Sentinel-aware framing** — the framing helpers and the stream decoder can\n  split on the chosen `sentinel` rather than `0x00`.\n- ♻️ **In-place decode** — decode basic COBS inside the same buffer with no second\n  allocation; COBS never expands on decode.\n- 🎯 **Zero dependencies, all platforms** — pure Dart (`dart:typed_data`), works\n  on mobile, desktop, web, server and CLI. Uses `Uint8List` throughout.\n- 📏 **Predictable sizing** — `maxEncodedLength` / `encodingOverhead` for buffer\n  pre-allocation.\n- ✅ **Reference-verified** — tested against the golden vectors from the original\n  COBS and COBS/R implementations.\n\n## Install\n\n```console\ndart pub add cobs_codec\n```\n\n```yaml\ndependencies:\n  cobs_codec: ^1.1.0\n```\n\n## Usage\n\n### Encode and decode\n\n```dart\nimport 'package:cobs_codec/cobs_codec.dart';\n\nvoid main() {\n  final data = [0x11, 0x22, 0x00, 0x33];\n\n  final encoded = cobs.encode(data); // [0x03, 0x11, 0x22, 0x02, 0x33] — no 0x00\n  final decoded = cobs.decode(encoded); // [0x11, 0x22, 0x00, 0x33]\n}\n```\n\n`cobs.encode` returns a `Uint8List` and never fails — any input is encodable.\n`cobs.decode` throws a `CobsDecodeException` (a `FormatException`) if the input\nis not valid COBS.\n\n### COBS/R — save a byte\n\nCOBS always adds exactly one byte to messages of 254 bytes or fewer. COBS/R\nopportunistically avoids that byte when the final data byte allows it:\n\n```dart\ncobs.encode([0x31, 0x32, 0x33, 0x34, 0x35]);  // [0x06, 0x31, 0x32, 0x33, 0x34, 0x35]\ncobsr.encode([0x31, 0x32, 0x33, 0x34, 0x35]); // [0x35, 0x31, 0x32, 0x33, 0x34]  (same size!)\n```\n\nBoth round-trip losslessly; just decode with the matching codec.\n\n### Framing a packet stream\n\nCOBS output has no `0x00`, so append one to delimit frames:\n\n```dart\nfinal frame = cobsFrame([0x11, 0x00, 0x22]); // [0x02, 0x11, 0x02, 0x22, 0x00]\n\nfinal packets = cobsUnframe(buffer); // List\u003cUint8List\u003e, one per 0x00-delimited frame\n```\n\n### Reading packets from a serial stream\n\n`CobsFrameDecoder` is a `StreamTransformer` that buffers bytes across arbitrarily\nchunked reads and emits one decoded packet per completed frame — exactly what you\nwant on a UART:\n\n```dart\nimport 'package:cobs_codec/cobs_codec.dart';\n\n// `serialPort` is any Stream\u003cList\u003cint\u003e\u003e of incoming bytes.\nserialPort\n    .transform(CobsFrameDecoder(\n      // Keep receiving even if a frame is corrupted on a noisy link.\n      onInvalidFrame: (error, rawFrame) =\u003e print('dropped bad frame: $error'),\n      // Bound memory if a peer never sends the 0x00 delimiter.\n      maxFrameLength: 4096,\n    ))\n    .listen((packet) =\u003e handlePacket(packet));\n\n// Sending: encode + delimit each outgoing packet.\noutgoingPackets\n    .transform(const CobsFrameEncoder())\n    .listen(serialPort.add);\n```\n\n### A custom delimiter byte (sentinel)\n\nNeed a delimiter other than `0x00` — say a byte your protocol reserves for\nframing? The `…WithSentinel` variants make the encoding avoid an arbitrary\n`sentinel` byte instead (they XOR the finished encoding with it), so that byte can\ndelimit frames:\n\n```dart\nfinal data = [0x11, 0x22, 0x00, 0x33];\n\nfinal encoded = cobsEncodeWithSentinel(data, 0xAA); // [0xA9, 0xBB, 0x88, 0xA8, 0x99] — no 0xAA\nfinal decoded = cobsDecodeWithSentinel(encoded, 0xAA); // [0x11, 0x22, 0x00, 0x33]\n\n// COBS/R has the same pair.\ncobsrEncodeWithSentinel(data, 0xAA); // [0xA9, 0xBB, 0x88, 0x99]\n```\n\n`sentinel == 0` is byte-for-byte identical to the plain codec, and decoding never\nmutates its input.\n\n### Decoding in place\n\nBasic COBS never expands on decode, so it can be decoded within the same buffer —\nno second allocation. `cobsDecodeInPlace` overwrites the buffer with the decoded\nbytes and returns their length:\n\n```dart\nfinal buffer = Uint8List.fromList(cobs.encode([0x11, 0x22, 0x00, 0x33]));\n// buffer == [0x03, 0x11, 0x22, 0x02, 0x33]\n\nfinal n = cobsDecodeInPlace(buffer);                 // 4\nfinal decoded = Uint8List.sublistView(buffer, 0, n); // [0x11, 0x22, 0x00, 0x33]\n```\n\n`cobsDecodeInPlaceWithSentinel(buffer, sentinel)` does the same for a custom\ndelimiter. (COBS/R can expand on decode, so it has no in-place form.)\n\n### Framing on a custom sentinel\n\n`cobsFrame` / `cobsUnframe` and both stream transformers take the same optional\n`sentinel`, so a whole link can be delimited by a byte other than `0x00`:\n\n```dart\nfinal frame = cobsFrame([0x11, 0x00, 0x22], sentinel: 0xAA); // [0xA8, 0xBB, 0xA8, 0x88, 0xAA]\nfinal packets = cobsUnframe(frame, sentinel: 0xAA);          // [[0x11, 0x00, 0x22]]\n\n// The stream decoder frames on the same byte:\nserialPort.transform(const CobsFrameDecoder(sentinel: 0xAA));\n```\n\n### Composing with other codecs\n\nBecause they are `Codec`s, you can `fuse` COBS with anything:\n\n```dart\n// Encode a Dart object to JSON, to UTF-8 bytes, then COBS-frame it.\nfinal pipeline = json.fuse(utf8).fuse(cobs);\nfinal wire = pipeline.encode({'id': 7, 'ok': true});\nfinal obj = pipeline.decode(wire);\n```\n\n### Sizing buffers\n\n```dart\nencodingOverhead(0);      // 1\nencodingOverhead(254);    // 1\nencodingOverhead(255);    // 2\nmaxEncodedLength(1000);   // 1004\n```\n\n## How much overhead?\n\n| Input length *n*      | Max encoded length | Overhead      |\n| --------------------- | ------------------ | ------------- |\n| 0 (empty)             | 1                  | +1 byte       |\n| 1 – 254               | *n* + 1            | +1 byte       |\n| 255 – 508             | *n* + 2            | +2 bytes      |\n| *n*                   | *n* + ⌈*n* / 254⌉  | ≤ ~0.4%       |\n\nThe overhead is *data-independent*: worst case and average case are almost the\nsame. Compare PPP/SLIP escape stuffing, whose worst case **doubles** the packet.\n\n## API overview\n\n| Symbol | Description |\n| ------ | ----------- |\n| `cobs` / `cobsr` | Shared `Codec` instances (basic COBS and COBS/R). |\n| `cobsEncode` / `cobsDecode` | Direct basic-COBS functions. |\n| `cobsrEncode` / `cobsrDecode` | Direct COBS/R functions. |\n| `cobsEncodeWithSentinel` / `cobsDecodeWithSentinel` | Basic COBS against an arbitrary delimiter byte. |\n| `cobsrEncodeWithSentinel` / `cobsrDecodeWithSentinel` | COBS/R against an arbitrary delimiter byte. |\n| `cobsDecodeInPlace` / `cobsDecodeInPlaceWithSentinel` | Decode basic COBS within the buffer; returns the length. |\n| `CobsCodec`, `CobsEncoder`, `CobsDecoder` | `dart:convert` classes for basic COBS. |\n| `CobsrCodec`, `CobsrEncoder`, `CobsrDecoder` | `dart:convert` classes for COBS/R. |\n| `cobsFrame` / `cobsUnframe` | Add / split the frame delimiter (`0x00` or a custom `sentinel`). |\n| `CobsFrameEncoder` / `CobsFrameDecoder` | Stream transformers for framed links (optional `sentinel`). |\n| `cobsDelimiter` | The frame delimiter byte (`0x00`). |\n| `encodingOverhead` / `maxEncodedLength` | Size bounds. |\n| `cobsMaxBlockLength` | Max data bytes per COBS block (`254`). |\n| `CobsDecodeException` | Thrown on invalid encoded input. |\n\n\u003e **Note on decoding.** `decode` expects a *single* encoded packet with **no**\n\u003e surrounding `0x00` delimiter — split a delimited stream into frames first (with\n\u003e `cobsUnframe` or `CobsFrameDecoder`). This matches the reference COBS\n\u003e implementations, where framing is the application's responsibility.\n\n## Benchmarks\n\nSingle-threaded throughput on a 1 KiB payload\n(`dart run benchmark/cobs_benchmark.dart`), Dart 3.x on an AMD Ryzen 7 3800XT\nunder WSL2 — indicative, not a controlled benchmark:\n\n| Operation | Throughput |\n| --------- | ---------- |\n| `cobsEncode` | ~580 MB/s |\n| `cobsDecode` | ~570 MB/s |\n| `cobsrEncode` | ~610 MB/s |\n\n## Background\n\nCOBS is described in:\n\n\u003e Stuart Cheshire and Mary Baker, \"Consistent Overhead Byte Stuffing,\"\n\u003e *IEEE/ACM Transactions on Networking*, Vol. 7, No. 2, April 1999.\n\n**COBS/R** (\"Reduced\") was devised by **Craig McQueen**, whose C and Python\nreference implementations were used to validate this package's test vectors. The\nCOBS/ZPE and COBS/ZRE variants are not implemented.\n\n## Contributing\n\nContributions are welcome — see [CONTRIBUTING.md](CONTRIBUTING.md).\n\n## License\n\nMIT © 2026 Alexander Salas Bastidas ([Firechip](https://firechip.dev)). See\n[LICENSE](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirechip%2Fcobs_codec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffirechip%2Fcobs_codec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirechip%2Fcobs_codec/lists"}