Skip to main content

codec_cbor/
lib.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
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 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 = 128;
29
30/// Maximum encoded DAG-CBOR byte length accepted by encode and decode.
31///
32/// This is a defense-in-depth bound for authoritative signed documents. It is
33/// intentionally much larger than expected production payloads while keeping
34/// parser and allocation work predictable under hostile input. Borrowed-byte
35/// hash and CID helpers do not enforce this cap or validate CBOR syntax.
36pub const MAX_DAG_CBOR_INPUT_LEN: usize = 1024 * 1024;
37
38pub use cid::{
39    compute_cid_dag_cbor, dag_cbor_multihash, is_valid_cid_string, sha2_256_content_hash,
40    try_parse_cid, verify_dag_cbor_cid, ContentHash, DagCborMultihash, DAG_CBOR_CODEC,
41    MAX_CID_STRING_LEN,
42};
43pub use decode_dag_cbor::decode_dag_cbor;
44pub use decode_deterministic_cbor::decode_deterministic_cbor;
45pub use deterministic::{
46    DeterministicCborError, DeterministicCborInteger, DeterministicCborMapEntry,
47    DeterministicCborMapKey, DeterministicCborNegativeInteger, DeterministicCborProfileError,
48    DeterministicCborValue, DETERMINISTIC_CBOR_NEGATIVE_MAX, DETERMINISTIC_CBOR_NEGATIVE_MIN,
49    MAX_DETERMINISTIC_CBOR_AGGREGATE_BYTE_STRING_BYTES,
50    MAX_DETERMINISTIC_CBOR_AGGREGATE_TEXT_BYTES, MAX_DETERMINISTIC_CBOR_CONTAINER_ENTRIES,
51    MAX_DETERMINISTIC_CBOR_INPUT_LEN, MAX_DETERMINISTIC_CBOR_NESTING_DEPTH,
52    MAX_DETERMINISTIC_CBOR_NODES, MAX_DETERMINISTIC_CBOR_OUTPUT_LEN,
53};
54pub use encode_dag_cbor::encode_dag_cbor;
55pub use encode_deterministic_cbor::encode_deterministic_cbor;
56pub use error::CborError;
57pub use value::CborValue;