reallyme-codec-cbor 0.4.4

Canonical CBOR codec support for ReallyMe Codec.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};

use super::error::{error, CborError, ErrorReason};
use super::float::{Float, FloatWidth};
use super::limits::{Budget, Limits};
use super::profile::{Profile, Serialization};
use super::read_header::{preferred_width, ArgumentWidth, MajorType, MajorTypeCode};
use super::receipt::Frame;
use super::scan::{add, scan};

/// Borrowed encoding input. Arrays and maps retain caller order. Scalar integer
/// copies zeroize on drop; borrowed storage remains the caller's responsibility.
/// No payload-printing or cloning traits are implemented.
pub enum ValueRef<'a> {
    /// Full unsigned 64-bit integer.
    Unsigned(u64),
    /// Full unsigned argument `n` for the negative integer `-1-n`.
    Negative(u64),
    /// Borrowed byte contents.
    Bytes(&'a [u8]),
    /// Borrowed UTF-8 text.
    Text(&'a str),
    /// Ordered array elements.
    Array(&'a [ValueRef<'a>]),
    /// Ordered unique map entries; equivalence is checked before returning output.
    Map(&'a [MapEntry<'a>]),
    /// Arbitrary tag and its child; application-specific semantics remain opaque.
    Tag(u64, &'a ValueRef<'a>),
    /// Borrowed protected floating-point value.
    Float(&'a Float),
    /// Boolean.
    Bool(bool),
    /// Null.
    Null,
    /// Undefined.
    Undefined,
    /// Simple value (0..23 or 32..255); codes 24..31 are reserved.
    Simple(u8),
    /// Embed the complete received encoding verbatim, subject to the selected
    /// profile and remaining budgets. This variant never normalizes input.
    Received(&'a Frame<'a>),
}

impl Drop for ValueRef<'_> {
    fn drop(&mut self) {
        match self {
            Self::Unsigned(value) | Self::Negative(value) | Self::Tag(value, _) => value.zeroize(),
            Self::Bool(value) => value.zeroize(),
            Self::Simple(value) => value.zeroize(),
            _ => {}
        }
    }
}
impl ZeroizeOnDrop for ValueRef<'_> {}

/// One ordered pair of borrowed encoding inputs, without hidden key sorting.
pub struct MapEntry<'a> {
    key: ValueRef<'a>,
    value: ValueRef<'a>,
}

impl<'a> MapEntry<'a> {
    /// Construct a pair; the encoder validates key uniqueness across the map.
    #[must_use]
    pub const fn new(key: ValueRef<'a>, value: ValueRef<'a>) -> Self {
        Self { key, value }
    }
}
impl ZeroizeOnDrop for MapEntry<'_> {}

/// Protected general CBOR output. The initialized allocation is wiped on drop,
/// including partial output on every encoding or validation failure.
pub struct EncodedCbor {
    bytes: Zeroizing<Vec<u8>>,
}

impl EncodedCbor {
    /// Borrow the protected encoding. Copies made by the caller need an owner
    /// that provides the same memory hygiene as the original allocation.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }
}
impl ZeroizeOnDrop for EncodedCbor {}

struct Encoder {
    bytes: Zeroizing<Vec<u8>>,
    length: usize,
    measure: bool,
    budget: Budget,
    profile: Profile,
}

impl Encoder {
    fn append(&mut self, bytes: &[u8]) -> Result<(), CborError> {
        let end = add(self.length, bytes.len())?;
        if end > self.budget.limits.output {
            return Err(error(ErrorReason::OutputLimit));
        }
        self.budget.charge(bytes.len())?;
        if !self.measure {
            // Preflight reserves once before any payload is copied. Reallocating
            // a populated Vec could leave sensitive bytes in the old allocation.
            if end > self.bytes.capacity() {
                return Err(error(ErrorReason::OutputLimit));
            }
            self.bytes.extend_from_slice(bytes);
        }
        self.length = end;
        Ok(())
    }

    fn argument(&mut self, major: MajorType, value: u64) -> Result<(), CborError> {
        let octets = Zeroizing::new(value.to_be_bytes());
        let (info, start) = match preferred_width(value) {
            ArgumentWidth::Inline => {
                let inline =
                    u8::try_from(value).map_err(|_| error(ErrorReason::ArithmeticOverflow))?;
                return self.append(&[major.prefix()? | inline]);
            }
            ArgumentWidth::One => (24, 7),
            ArgumentWidth::Two => (25, 6),
            ArgumentWidth::Four => (26, 4),
            ArgumentWidth::Eight => (27, 0),
            _ => return Err(error(ErrorReason::ArithmeticOverflow)),
        };
        self.append(&[major.prefix()? | info])?;
        self.append(
            octets
                .get(start..)
                .ok_or(error(ErrorReason::ArithmeticOverflow))?,
        )
    }

    fn count(&mut self, count: usize, map: bool) -> Result<u64, CborError> {
        if count > self.budget.limits.entries {
            return Err(error(ErrorReason::EntryLimit));
        }
        let children = if map {
            count
                .checked_mul(2)
                .ok_or(error(ErrorReason::ArithmeticOverflow))?
        } else {
            count
        };
        self.budget.declared_nodes(children)?;
        u64::try_from(count).map_err(|_| error(ErrorReason::ArithmeticOverflow))
    }

    fn value(&mut self, value: &ValueRef<'_>, depth: usize) -> Result<(), CborError> {
        self.budget.charge(1)?;
        if let ValueRef::Received(frame) = value {
            // Account the original structure, not a second semantic tree. Final
            // profile validation rejects incompatible widths rather than silently
            // rebuilding bytes that may already be authenticated.
            scan(
                frame.received_bytes(),
                &mut self.budget,
                depth,
                None,
                true,
                self.profile.lengths,
            )?;
            return self.append(frame.received_bytes());
        }
        self.budget.check_item_depth(depth)?;
        self.budget.node()?;
        match value {
            ValueRef::Unsigned(value) => self.argument(MajorType::Unsigned, *value),
            ValueRef::Negative(value) => self.argument(MajorType::Negative, *value),
            ValueRef::Bytes(bytes) => self.bytes_value(bytes, false),
            ValueRef::Text(text) => self.bytes_value(text.as_bytes(), true),
            ValueRef::Array(values) => {
                let depth = self.budget.enter(depth)?;
                let count = self.count(values.len(), false)?;
                self.argument(MajorType::Array, count)?;
                for value in *values {
                    self.value(value, depth)?;
                }
                Ok(())
            }
            ValueRef::Map(entries) => {
                let depth = self.budget.enter(depth)?;
                let count = self.count(entries.len(), true)?;
                self.argument(MajorType::Map, count)?;
                for entry in *entries {
                    self.value(&entry.key, depth)?;
                    self.value(&entry.value, depth)?;
                }
                Ok(())
            }
            ValueRef::Tag(tag, child) => {
                let depth = self.budget.enter(depth)?;
                self.argument(MajorType::Tag, *tag)?;
                self.value(child, depth)
            }
            ValueRef::Float(value) => self.float_value(value),
            ValueRef::Bool(value) => self.append(&[if *value { 0xf5 } else { 0xf4 }]),
            ValueRef::Null => self.append(&[0xf6]),
            ValueRef::Undefined => self.append(&[0xf7]),
            ValueRef::Simple(value) => self.simple_value(*value),
            ValueRef::Received(_) => Err(error(ErrorReason::WrongType)),
        }
    }
    fn contract_value(
        &mut self,
        node: &codec_proto::generated::proto::reallyme::codec::v1::CodecGeneralCborValue,
        depth: usize,
    ) -> Result<(), CborError> {
        use codec_proto::generated::proto::reallyme::codec::v1::codec_general_cbor_value::Value;

        let value = node.value.as_ref().ok_or(error(ErrorReason::WrongType))?;
        self.budget.charge(1)?;
        if let Value::ReceivedValue(value) = value {
            // Native Received holds a Frame, which proves exact framing and
            // the hard input ceiling. Re-establish that proof for wire bytes
            // before they can be embedded in a constructed container.
            if value.value.is_empty() {
                return Err(error(ErrorReason::EmptyInput));
            }
            if value.value.len() > super::limits::MAX_BYTES {
                return Err(error(ErrorReason::InputLimit));
            }
            let received = scan(
                &value.value,
                &mut self.budget,
                depth,
                None,
                true,
                self.profile.lengths,
            )?;
            if received.received_bytes().len() != value.value.len() {
                return Err(error(ErrorReason::TrailingData));
            }
            return self.append(&value.value);
        }
        self.budget.check_item_depth(depth)?;
        self.budget.node()?;
        match value {
            Value::ArrayValue(array) => {
                let depth = self.budget.enter(depth)?;
                let count = self.count(array.values.len(), false)?;
                self.argument(MajorType::Array, count)?;
                for child in &array.values {
                    self.contract_value(child, depth)?;
                }
                Ok(())
            }
            Value::MapValue(map) => {
                let depth = self.budget.enter(depth)?;
                let count = self.count(map.entries.len(), true)?;
                self.argument(MajorType::Map, count)?;
                for entry in &map.entries {
                    self.contract_value(
                        entry.key.as_option().ok_or(error(ErrorReason::WrongType))?,
                        depth,
                    )?;
                    self.contract_value(
                        entry
                            .value
                            .as_option()
                            .ok_or(error(ErrorReason::WrongType))?,
                        depth,
                    )?;
                }
                Ok(())
            }
            Value::TagValue(tag) => {
                let depth = self.budget.enter(depth)?;
                self.argument(MajorType::Tag, tag.tag)?;
                self.contract_value(
                    tag.value.as_option().ok_or(error(ErrorReason::WrongType))?,
                    depth,
                )
            }
            // Scalar transport nodes enter the native scalar writer without
            // charging a second structural node or introducing payload copies.
            value => self.contract_scalar(value),
        }
    }

    fn contract_scalar(
        &mut self,
        value: &codec_proto::generated::proto::reallyme::codec::v1::codec_general_cbor_value::Value,
    ) -> Result<(), CborError> {
        use super::encode_contract::float;
        use codec_proto::generated::proto::reallyme::codec::v1::codec_general_cbor_value::Value;
        match value {
            Value::UnsignedValue(v) => self.argument(MajorType::Unsigned, v.argument),
            Value::NegativeValue(v) => self.argument(MajorType::Negative, v.argument),
            Value::BytesValue(v) => self.bytes_value(&v.value, false),
            Value::TextValue(v) => self.bytes_value(v.value.as_bytes(), true),
            Value::FloatValue(v) => self.float_value(&float(v)?),
            Value::BoolValue(v) => self.append(&[if v.value { 0xf5 } else { 0xf4 }]),
            Value::NullValue(_) => self.append(&[0xf6]),
            Value::UndefinedValue(_) => self.append(&[0xf7]),
            Value::SimpleValue(v) => self.simple_value(
                u8::try_from(v.value).map_err(|_| error(ErrorReason::InvalidSimpleValue))?,
            ),
            _ => Err(error(ErrorReason::WrongType)),
        }
    }

    fn bytes_value(&mut self, bytes: &[u8], text: bool) -> Result<(), CborError> {
        self.budget.contents(bytes.len(), text)?;
        self.argument(
            if text {
                MajorType::Text
            } else {
                MajorType::Bytes
            },
            u64::try_from(bytes.len()).map_err(|_| error(ErrorReason::ArithmeticOverflow))?,
        )?;
        self.append(bytes)
    }

    fn simple_value(&mut self, value: u8) -> Result<(), CborError> {
        match value {
            0..=23 => self.append(&[0xe0 | value]),
            32..=255 => self.append(&[0xf8, value]),
            _ => Err(error(ErrorReason::InvalidSimpleValue)),
        }
    }

    fn float_value(&mut self, value: &Float) -> Result<(), CborError> {
        value.check(self.profile.floats)?;
        if self.profile.requires_binary16_nan() {
            value.check_binary16_nan()?;
        }
        let width = if self.profile.serialization == Serialization::Preferred {
            value.shortest()
        } else {
            value.width()
        };
        let bits = Zeroizing::new(value.at_width(width)?.to_be_bytes());
        let (header, start) = match width {
            FloatWidth::Binary16 => (0xf9, 6),
            FloatWidth::Binary32 => (0xfa, 4),
            FloatWidth::Binary64 => (0xfb, 0),
            _ => return Err(error(ErrorReason::UnsupportedPolicy)),
        };
        self.append(&[header])?;
        self.append(
            bits.get(start..)
                .ok_or(error(ErrorReason::ArithmeticOverflow))?,
        )
    }
}

/// Encode general CBOR into protected output, preserving map order and received
/// items. Constructed arguments use shortest widths; the selected profile
/// controls float shortening, validity, and optional key-order enforcement.
pub fn encode(
    value: &ValueRef<'_>,
    profile: Profile,
    limits: Limits,
) -> Result<EncodedCbor, CborError> {
    encode_with(profile, limits, |encoder| encoder.value(value, 0))
}

pub(super) fn encode_generated(
    value: &codec_proto::generated::proto::reallyme::codec::v1::CodecGeneralCborValue,
    profile: Profile,
    limits: Limits,
) -> Result<EncodedCbor, CborError> {
    encode_with(profile, limits, |encoder| encoder.contract_value(value, 0))
}

fn encode_with(
    profile: Profile,
    limits: Limits,
    visit: impl Fn(&mut Encoder) -> Result<(), CborError>,
) -> Result<EncodedCbor, CborError> {
    profile.ensure_supported()?;
    let mut measure = Encoder {
        bytes: Zeroizing::new(Vec::new()),
        length: 0,
        measure: true,
        budget: Budget::for_profile(limits, profile),
        profile,
    };
    visit(&mut measure)?;
    let mut bytes = Zeroizing::new(Vec::new());
    bytes
        .try_reserve_exact(measure.length)
        .map_err(|_| error(ErrorReason::AllocationFailure))?;
    let mut budget = Budget::for_profile(limits, profile);
    budget.work = measure.budget.work;
    let mut encoder = Encoder {
        bytes,
        length: 0,
        measure: false,
        budget,
        profile,
    };
    visit(&mut encoder)?;
    // All passes share work credits. Structural totals are checked separately
    // per pass so the published node/content limits describe the actual output.
    let mut validation = Budget::for_profile(limits, profile);
    validation.work = encoder.budget.work;
    {
        let validated = scan(
            &encoder.bytes,
            &mut validation,
            0,
            Some(profile),
            true,
            profile.lengths,
        )?;
        if validated.received_bytes().len() != encoder.bytes.len() {
            return Err(error(ErrorReason::TrailingData));
        }
    }
    Ok(EncodedCbor {
        bytes: encoder.bytes,
    })
}