A fast, safe, and efficient CBOR serialization library for Swift on any platform.
swiftpackageindex.com/thecoolwinter/CBOR/1.1.1/documentation/cbor
atproto
swift
cbor
1<p align="center">
2 <img src="https://github.com/thecoolwinter/CBOR/blob/main/.github/Icon-256.png?raw=true" height="128">
3 <h1 align="center">CBOR</h1>
4</p>
5
6<p align="center">
7 <a aria-label="CI Status" href="" target="_blank">
8 <img alt="" src="https://img.shields.io/github/actions/workflow/status/thecoolwinter/CBOR/ci.yml">
9 </a>
10 <a aria-label="Swift Version Compatibility" href="https://swiftpackageindex.com/thecoolwinter/CBOR" target="_blank">
11 <img alt="" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fthecoolwinter%2FCBOR%2Fbadge%3Ftype%3Dswift-versions">
12 </a>
13 <a aria-label="Platform Compatibility" href="https://swiftpackageindex.com/thecoolwinter/CBOR" target="_blank">
14 <img alt="" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fthecoolwinter%2FCBOR%2Fbadge%3Ftype%3Dplatforms">
15 </a>
16</p>
17
18[CBOR](https://cbor.io/) is a flexible data format that emphasizes extremely small code size, small message size, and extendability. This library provides a Swift API for encoding and decoding Swift types using the CBOR serialization format.
19
20The motivation for this library over existing implementations is twofold: performance, and reliability. Existing community implementations do not make correct use of Swift features like `Optional` and memory safety. This library aims to be safe while still outperforming other implementations. To that end, this library has been extensively [fuzz tested](./Fuzz.md) to ensure your application never crashes due to malicious input. At the same time, this library boasts up to a *74% faster* encoding and up to *89% faster* decoding than existing implementations. See [benchmarks](#benchmarks) for more details.
21
22## Features
23
24- `Codable` API for familiar Swift project integration.
25- Customizable encoding options, such as date and key formatting.
26- Customizable decoding, with the ability to reject non-deterministic CBOR data.
27- Encoder creates deterministic CBOR data by default.
28 - Dictionaries are automatically sorted using a min heap for speed.
29 - Duplicate map keys are removed.
30 - Never creates non-deterministic collections (Strings, Byte Strings, Arrays, or Maps).
31- A scan pass allows for decoding only the keys you need from an encoded object.
32- Supports decoding half precision floats (Float16) as a regular Float.
33- Runs on Linux, Android, and Windows using the swift-foundation project when available.
34- Fuzz tested for reliability against crashes.
35- ***NEW*** Supports tagged items with custom tag injection.
36 - Dates are a special case, handled by the library.
37 - Contains UUID example implementation.
38- Flexible date parsing (tags `0` or `1` with support for any numeric value representation).
39- Decoding multiple top-level objects using `decodeMultiple(_:from:)`.
40- ***NEW*** IPLD compatible DAG-CBOR encoder for content addressable data.
41- ***NEW*** Flexible date decoding for untagged date items encoded as strings, floating point values, or integers.
42
43## Usage
44
45### Standard CBOR
46
47This library utilizes Swift's `Codable` API for all (de)serialization operations. It intentionally doesn't support streaming CBOR blobs. After [installing](#installation) the package using Swift Package Manager, use the `CBOREncoder` and `CBORDecoder` types to encode to and decode from CBOR blobs, respectively.
48
49```swift
50// Encode any `Encodable` value.
51let encoder = CBOREncoder()
52try encoder.encode(0) // Result: 0x00
53try encoder.encode([404: "Not Found"]) // Result: 0xA1190194694E6F7420466F756E64
54
55// Decode any `Decodable` type.
56let decoder = CBORDecoder()
57let intValue = try decoder.decode(Int.self, from: Data([0])) // Result: 0
58// Result: ["AB": 1, "A": 2]
59let mapValue = try decoder.decode([String: Int].self, from: Data([162, 98, 65, 66, 1, 97, 65, 2]))
60```
61
62The encoder and decoders can be customized via the en/decoder initializers or via a `En/DecodingOptions` struct.
63
64```swift
65// Customize encoder behavior.
66let encoder = CBOREncoder(
67 forceStringKeys: Bool = false,
68 dateEncodingStrategy: EncodingOptions.DateStrategy = .double
69)
70// or via the options struct
71let options = EncodingOptions(/* ... */)
72let encoder = CBOREncoder(options: options)
73
74// Customize the decoder behavior, such as enforcing deterministic CBOR.
75let decoder = CBORDecoder(rejectIndeterminateLengths: Bool = false)
76// or via the options struct
77let options = DecodingOptions(/* ... */)
78let decoder = CBORDecoder(options: options)
79```
80
81### DAG-CBOR
82
83This library also offers the ability to encode and decode DAG-CBOR data. DAG-CBOR is a superset of the CBOR spec, though very similar. This library provides a `DAGCBOREncoder` type that is automatically configured to produce compatible data. The flags it sets are also available through the `EncodingOptions` struct, but using the specialized type will ensure safety.
84
85```swift
86// !! SEE NOTE !!
87let dagEncoder = DAGCBOREncoder(dateEncodingStrategy: .double)
88```
89
90> [!NOTE]
91> DAG-CBOR does not allow tagged items (besides the CID item), and thus encoding dates must be done by encoding their 'raw' value directly. This is an application specific behavior, so ensure the encoder is using the correct date encoding behavior for compatibility. By default, the encoder will encode dates as an epoch `Double` timestamp.
92
93To use with CIDs, conform your internal CID type to ``CIDType``. Do not conform standard types like `String` or `Data` to ``CIDType``, or the encoder may attempt to encode all of those data as tagged items.
94```swift
95struct CID: CIDType {
96 let data: Data
97
98 init(data: Data) {
99 self.data = data
100 }
101
102 init(from decoder: any Decoder) throws {
103 let container = try decoder.singleValueContainer()
104 var data = try container.decode(Data.self)
105 data.removeFirst()
106 self.data = data
107 }
108
109 func encode(to encoder: any Encoder) throws {
110 var container = encoder.singleValueContainer()
111 try container.encode(Data([0x0]) + data)
112 }
113}
114```
115
116> [!WARNING]
117>
118> It's *your* responsibility to correctly encode CIDs in the data container. That includes the `NULL` byte for raw binary CID encoding (which DAG-CBOR expects).
119
120Now, any time the encoder finds a `CID` type it will encode it using the correct tag.
121```swift
122let cid = CID(bytes: [0,1,2,3,4,5,6,7,8])
123let data = try DAGCBOREncoder().encode(cid)
124
125print(data.hexString())
126// Output:
127// D8 2A # tag(42)
128// 4A # bytes(10)
129// 00000102030405060708 # "\u0000\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b"
130```
131
132## Documentation
133
134[Documentation](https://swiftpackageindex.com/thecoolwinter/CBOR/1.1.0/documentation/cbor) is hosted on the Swift Package Index.
135
136## Installation
137
138You can use the Swift Package Manager to download and import the library into your project:
139
140```swift
141dependencies: [
142 .package(url: "https://github.com/thecoolwinter/CBOR.git", from: "1.1.0")
143]
144```
145
146Then under `targets`:
147
148```swift
149targets: [
150 .target(
151 // ...
152 dependencies: [
153 .product(name: "CBOR", package: "CBOR")
154 ]
155 )
156]
157```
158
159## Benchmarks
160
161These benchmarks range from simple to complex encoding and decoding operations compared to the [SwiftCBOR](https://github.com/valpackett/SwiftCBOR) library. [Benchmarks source](./Benchmarks). Benchmarks are run on 10,000 samples and the p50 values are compared here.
162
163### Decoding (cpu time)
164
165| Benchmark | SwiftCBOR (ns, p50) | CBOR (ns, p50) | % Improvement |
166|-----------|----------------|-----------|------------|
167| Array | 23 | 7 | **70%** |
168| Complex Object | 703 μs | 75 μs | **89%** |
169| Date | 5,251 | 1,083 | **79%** |
170| Dictionary | 17 | 5 | **71%** |
171| Double | 5,295 | 1,001 | **81%** |
172| Float | 5,295 | 1,000 | **81%** |
173| Indeterminate String | 6,251 | 1,417 | **77%** |
174| Int | 5,211 | 1,125 | **78%** |
175| Int Small | 5,211 | 1,083 | **79%** |
176| Simple Object | 36 | 8 | **78%** |
177| String | 5,459 | 1,292 | **76%** |
178| String Small | 5,291 | 1,126 | **79%** |
179
180### Encoding (cpu time)
181
182| Benchmark | SwiftCBOR (ns, p50) | CBOR (ns, p50) | % Improvement |
183|-----------|----------------|-----------|------------|
184| Array | 669 μs | 471 μs | **30%** |
185| Array Small | 7,043 | 2,917 | **59%** |
186| Bool | 3,169 | 1,125 | **64%** |
187| Complex Codable Object | 124 μs | 92 μs | **26%** |
188| Data | 5,335 | 1,250 | **77%** |
189| Data Small | 3,959 | 1000 | **75%** |
190| Dictionary | 11 | 5 | **55%** |
191| Dictionary Small | 7,459 | 2,959 | **60%** |
192| Int | 4,001 | 1,292 | **68%** |
193| Int Small | 3,833 | 1,208 | **68%** |
194| Simple Codable Object | 18 | 9 | **50%** |
195| String | 5,583 | 1,291 | **77%** |
196| String Small | 4,041 | 1,125 | **72%** |
197
198## Contributing
199
200By participating in this project you agree to follow the [Contributor Code of Conduct](https://contributor-covenant.org/version/1/4/). Pull requests and issues are welcome!
201
202## Sponsor
203
204This project has been developed in my spare time. To keep me motivated to continue to maintain it into the future, consider [supporting my work](https://github.com/sponsors/thecoolwinter)!