https://github.com/firechip/cobs_codec_swift
Pure-Swift, dependency-free Consistent Overhead Byte Stuffing (COBS) and COBS/R codec (Swift Package Manager). The Swift member of the Firechip COBS family.
https://github.com/firechip/cobs_codec_swift
byte-stuffing cobs cobs-r encoding framing macos serial spm swift swift-package-manager
Last synced: about 8 hours ago
JSON representation
Pure-Swift, dependency-free Consistent Overhead Byte Stuffing (COBS) and COBS/R codec (Swift Package Manager). The Swift member of the Firechip COBS family.
- Host: GitHub
- URL: https://github.com/firechip/cobs_codec_swift
- Owner: firechip
- License: mit
- Created: 2026-07-04T20:52:48.000Z (about 11 hours ago)
- Default Branch: main
- Last Pushed: 2026-07-04T21:47:38.000Z (about 10 hours ago)
- Last Synced: 2026-07-04T23:08:31.740Z (about 9 hours ago)
- Topics: byte-stuffing, cobs, cobs-r, encoding, framing, macos, serial, spm, swift, swift-package-manager
- Language: Swift
- Size: 28.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# CobsCodec
[](https://github.com/firechip/cobs_codec_swift/actions/workflows/ci.yml)
[](https://swift.org)
[](#)
[](LICENSE)
Pure-Swift **Consistent Overhead Byte Stuffing (COBS)** and **COBS/R** codec — the
Swift member of the Firechip COBS family (alongside the
[Rust](https://crates.io/crates/cobs_codec_rs),
[Dart](https://pub.dev/packages/cobs_codec) and
[Kotlin](https://github.com/firechip/cobs_codec_kt) implementations), verified
byte-identical against the shared
[conformance vectors](https://github.com/firechip/cobs-conformance).
COBS encodes an arbitrary byte sequence into one that contains no zero (`0x00`)
byte, at a small, predictable cost (at most one extra byte per 254 bytes, plus
one). A single `0x00` can then reliably delimit packets on a serial/UART, USB, or
BLE link. The `CobsCodec` library is **Swift-standard-library-only** — no
Foundation, no Apple frameworks, no dependencies — so it runs on macOS, iOS,
tvOS, watchOS, and Linux (and embedded Swift).
## Install
Swift Package Manager — add the dependency to your `Package.swift`:
```swift
dependencies: [
.package(url: "https://github.com/firechip/cobs_codec_swift.git", from: "1.0.0"),
],
targets: [
.target(name: "YourTarget", dependencies: ["CobsCodec"]),
]
```
Or in Xcode: **File ▸ Add Package Dependencies…** and paste the repository URL.
## Usage
```swift
import CobsCodec
// Encode / decode a single packet.
let encoded = Cobs.encode([0x11, 0x22, 0x00, 0x33]) // [0x03, 0x11, 0x22, 0x02, 0x33] — no 0x00
let decoded = try Cobs.decode(encoded) // [0x11, 0x22, 0x00, 0x33]
// COBS/R often saves the trailing overhead byte for small messages.
Cobsr.encode(Array("12345".utf8)) // [0x35, 0x31, 0x32, 0x33, 0x34] — "51234"
// A custom delimiter byte (sentinel): the output avoids it, so it can delimit
// frames instead of 0x00. `sentinel: 0` is identical to the plain codec.
let stuffed = Cobs.encode([0x11, 0x00, 0x22], sentinel: 0xAA) // [0xA8, 0xBB, 0xA8, 0x88] — no 0xAA
try Cobs.decode(stuffed, sentinel: 0xAA) // [0x11, 0x00, 0x22]
// Decode in place, without a second buffer (COBS never expands on decode).
var buffer = Cobs.encode([0x11, 0x00, 0x22])
let n = try Cobs.decodeInPlace(&buffer) // n == 3; buffer[0..