Skip to main content

codec_cbor/
lib.rs

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