An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# CobsCodec

[![CI](https://github.com/firechip/cobs_codec_swift/actions/workflows/ci.yml/badge.svg)](https://github.com/firechip/cobs_codec_swift/actions/workflows/ci.yml)
[![Swift 6](https://img.shields.io/badge/swift-6-orange.svg)](https://swift.org)
[![Platforms](https://img.shields.io/badge/platforms-macOS%20%7C%20iOS%20%7C%20Linux-lightgrey.svg)](#)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](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..