Expand description
§C2PA CBOR Library
A CBOR (Concise Binary Object Representation) encoder/decoder with support for tagged types.
§Architecture
This library uses a dual-path serialization strategy for optimal performance:
- Fast path: When collection sizes are known at compile time (the common case), data is written directly to the output with zero buffering overhead.
- Buffering path: When sizes are unknown (e.g.,
#[serde(flatten)]inserde_transcode), entries are buffered in memory and written as definite-length once the count is known.
This design maintains C2PA’s requirement for deterministic, definite-length encoding while supporting the full serde data model including complex features like flatten.
§Features
- Full support for CBOR major types 0-7
- Tagged types (major type 6) including:
- Date/time strings (tag 0)
- Epoch timestamps (tag 1)
- URIs (tag 32)
- Base64url encoded data (tag 33)
- Base64 encoded data (tag 34)
- RFC 8746 typed arrays (tags 64-87):
- Unsigned integer arrays (uint8, uint16, uint32, uint64) in big-endian and little-endian
- Signed integer arrays (sint8, sint16, sint32, sint64) in big-endian and little-endian
- Floating point arrays (float16, float32, float64, float128) in big-endian and little-endian
- Custom tag support via
write_tag()andread_tag()methods
§Performance
Binary byte arrays are efficiently encoded/decoded with minimal overhead:
- Use
serde_bytes::ByteBufor#[serde(with = "serde_bytes")]for optimal byte array performance - Byte strings are written as raw bytes (1 header byte + length encoding + data)
- 1KB byte array: 3 bytes overhead (header + 2-byte length)
- 100KB byte array: 5 bytes overhead (header + 4-byte length)
- No allocations during encoding; single allocation during decoding
§Speed Characteristics (on typical hardware)
- Encoding: ~30-35 GB/s for large arrays (virtually memcpy speed)
- Decoding: ~24-29 GB/s for large arrays
- Small arrays (1KB): ~160ns encode, ~270ns decode
- Large arrays (1MB): ~30µs encode, ~41µs decode
- Performance scales linearly with data size
- Zero-copy design means encoding is just a memcpy after writing the header
§Example
use c2pa_cbor::{encode_datetime_string, encode_uint8_array, encode_uri, from_slice};
use serde_bytes::ByteBuf;
// Encode a URI with tag 32
let mut buf = Vec::new();
encode_uri(&mut buf, "https://example.com").unwrap();
let decoded: String = from_slice(&buf).unwrap();
assert_eq!(decoded, "https://example.com");
// Encode a typed array with tag 64 (efficient with serde_bytes)
let data = ByteBuf::from(vec![1, 2, 3, 4, 5]);
let mut buf2 = Vec::new();
let mut encoder = c2pa_cbor::Encoder::new(&mut buf2);
encoder.write_tag(64).unwrap();
encoder.encode(&data).unwrap();Re-exports§
pub use error::Error;pub use error::Result;pub use encoder::Encoder;pub use encoder::to_vec;pub use encoder::to_writer;pub use decoder::Decoder;pub use decoder::from_reader;pub use decoder::from_reader_with_limit;pub use decoder::from_slice;pub use decoder::from_slice_with_limit;pub use value::Value;pub use value::from_value;pub use value::to_value;pub use tags::*;
Modules§
- de
- Deserialization module for compatibility with serde_cbor
- decoder
- encoder
- error
- ser
- Serialization module for compatibility with serde_cbor Compatibility layer for serde_cbor API
- tags
- value
Constants§
- DEFAULT_
MAX_ ALLOCATION - Default maximum allocation size (100MB) to prevent OOM attacks from malicious CBOR.
- DEFAULT_
MAX_ DEPTH - Default maximum recursion depth to prevent stack overflow from deeply nested structures.
Type Aliases§
- Deserializer
- Type alias for
Decoder(serde_cbor compatibility) - Serializer
- Type alias for
Encoder(serde_cbor compatibility)