sena-mike/swift-longhair
A Swift Package Wrapper around [Longhair](https://github.com/catid/longhair) for Cauchy 256 encoding and decoding.
Usage
1) Add the package
Swift Package Manager (Package.swift)
dependencies: [
.package(url: "https://github.com/swift-longhair/swift-longhair.git", from: "1.0.0")
]Then add Longhair to your target dependencies.
Xcode
- Add this package to your Xcode project by going to
File>Add Packages...and entering the URL of this repository. - In Xcode project settings pre-processor macros for your target, add
GF256_TARGET_MOBILE=1.
2) Encode blocks and recover missing data
The example below is mirrored by a test in Tests/LonghairTests/LonghairTests.swift (verifyReadmeUsageExample) to keep documentation usage validated.
import Foundation
import Longhair
let original = Data((0..<1024).map { UInt8($0 & 0xFF) })
let blockSize = 128
let recoveryBlockCount = 2
let dataBlocks = stride(from: 0, to: original.count, by: blockSize).map { start in
original.subdata(in: start..<(start + blockSize))
}
var encoded = try Cauchy256.encode(
dataBlocks: dataBlocks,
recoveryBlockCount: recoveryBlockCount
)
// Simulate packet loss (up to recoveryBlockCount blocks)
encoded[2].data = nil
encoded[6].data = nil
let recovered = try Cauchy256.decode(
blocks: encoded,
recoveryBlockCount: Int32(recoveryBlockCount)
)
assert(recovered == original)Local setup
Run the setup script to initialize the Longhair submodule and apply local overlay files (module.modulemap and CLonghair.apinotes):
./scripts/setup.shIf you resync submodules later, re-apply overlays with:
./scripts/sync-clonghair-overlay.shBuild and run tests:
swift build
swift testStyle checks
Format Swift code:
./scripts/swift-style.sh formatLint Swift code:
./scripts/swift-style.sh lintVerify formatting exactly (used in CI):
./scripts/swift-style.sh checkThe formatter configuration is in .swift-format and uses 2-space indentation.
Package Metadata
Repository: sena-mike/swift-longhair
Default branch: main
README: README.md