codec_cbor/lib.rs
1// SPDX-FileCopyrightText: 2026 ReallyMe LLC
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! Deterministic generic CBOR and a DAG-CBOR subset with content-ID helpers.
6//!
7//! Both profiles use length-first key ordering (RFC 8949 ยง4.2.3) and reject non-canonical
8//! integers, indefinite-length items, floats, tags, out-of-order map keys,
9//! and trailing bytes, so a given value has exactly one accepted encoding.
10//! DAG-CBOR supports text map keys and signed 64-bit integers. Generic CBOR
11//! also supports integer map keys and the full unsigned 64-bit range.
12//! Input-size and nesting limits bound parser work; declared container lengths
13//! are checked against remaining input before allocating their contents.
14
15mod cid;
16mod decode_dag_cbor;
17mod decode_deterministic_cbor;
18mod deterministic;
19mod encode_dag_cbor;
20mod encode_deterministic_cbor;
21mod error;
22mod value;
23
24/// Maximum array/map nesting depth accepted by DAG-CBOR encode and decode.
25///
26/// Limits recursive traversal of caller-controlled containers. Runtime
27/// adapters may enforce stricter transport limits.
28pub const MAX_NESTING_DEPTH: usize = 64;
29
30/// Maximum semantic nodes accepted by the DAG-CBOR profile.
31pub const MAX_DAG_CBOR_NODES: usize = 65_536;
32
33/// Maximum entries in one DAG-CBOR array or map.
34pub const MAX_DAG_CBOR_CONTAINER_ENTRIES: usize = 16_384;
35
36/// Maximum encoded DAG-CBOR byte length accepted by encode and decode.
37///
38/// This is a defense-in-depth bound for authoritative signed documents. It is
39/// intentionally much larger than expected production payloads while keeping
40/// parser and allocation work predictable under hostile input. Borrowed-byte
41/// hash and CID computation helpers do not enforce this cap or validate CBOR
42/// syntax; CID verification does validate the block.
43pub const MAX_DAG_CBOR_INPUT_LEN: usize = 1_048_576;
44
45pub use cid::{
46 compute_cid_dag_cbor, dag_cbor_multihash, is_valid_cid_string, sha2_256_content_hash,
47 try_parse_cid, verify_dag_cbor_cid, CidVerificationStatus, ContentHash, DagCborCidVerification,
48 DagCborMultihash, ParsedCid, DAG_CBOR_CODEC, MAX_CID_STRING_LEN,
49};
50pub use decode_dag_cbor::decode_dag_cbor;
51pub use decode_deterministic_cbor::decode_deterministic_cbor;
52pub use deterministic::{
53 DeterministicCborError, DeterministicCborInteger, DeterministicCborMapEntry,
54 DeterministicCborMapKey, DeterministicCborNegativeInteger, DeterministicCborProfileError,
55 DeterministicCborValue, DETERMINISTIC_CBOR_NEGATIVE_MAX, DETERMINISTIC_CBOR_NEGATIVE_MIN,
56 MAX_DETERMINISTIC_CBOR_AGGREGATE_BYTE_STRING_BYTES,
57 MAX_DETERMINISTIC_CBOR_AGGREGATE_TEXT_BYTES, MAX_DETERMINISTIC_CBOR_CONTAINER_ENTRIES,
58 MAX_DETERMINISTIC_CBOR_INPUT_LEN, MAX_DETERMINISTIC_CBOR_NESTING_DEPTH,
59 MAX_DETERMINISTIC_CBOR_NODES, MAX_DETERMINISTIC_CBOR_OUTPUT_LEN,
60};
61pub use encode_dag_cbor::encode_dag_cbor;
62pub use encode_deterministic_cbor::encode_deterministic_cbor;
63pub use error::CborError;
64pub use value::CborValue;