verirun-core 0.2.0

Deterministic core for verirun covering RFC 8785 canonical JSON, domain-separated SHA-256 digest framing, DAG validation, and Merkle manifests with std only.
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! The strict JSON parser (docs/ARCHITECTURE.md section 4.5).
//!
//! Recursive descent over a byte slice, with a byte offset on every
//! failure. The parser never panics on malformed input: every index is
//! reached through checked access, and recursion is bounded by
//! [`MAX_DEPTH`].

use crate::canonical::MAX_SAFE_INTEGER;
use crate::error::{ParseError, ParseErrorKind};
use crate::value::Value;

/// The maximum nesting depth of arrays and objects. Deeper input fails
/// with [`crate::error::ParseErrorKind::DepthLimit`]. The limit bounds
/// stack use against adversarial input.
pub const MAX_DEPTH: usize = 128;

/// Parses one complete JSON document into a [`Value`].
///
/// Strictness, each with its byte offset: duplicate object keys are
/// rejected; numbers follow the JSON grammar and the value-based
/// domain of [`crate::canonical`]; `\u` escapes in the surrogate range
/// must form an adjacent valid pair; raw control characters inside
/// strings are rejected; trailing bytes after the top-level value are
/// rejected; nesting is capped at [`MAX_DEPTH`].
///
/// # Errors
///
/// Returns [`ParseError`] describing the first failure, with its kind
/// and byte offset.
pub fn parse(input: &[u8]) -> Result<Value, ParseError> {
    if input.is_empty() {
        return Err(ParseError::new(
            ParseErrorKind::EmptyInput,
            0,
            "input is empty".to_string(),
        ));
    }
    let text = match core::str::from_utf8(input) {
        Ok(text) => text,
        Err(err) => {
            let offset = err.valid_up_to();
            return Err(ParseError::new(
                ParseErrorKind::InvalidUtf8,
                offset,
                format!("invalid UTF-8 starting at byte {offset}"),
            ));
        }
    };
    let mut parser = Parser {
        bytes: text.as_bytes(),
        pos: 0,
    };
    parser.skip_whitespace();
    if parser.pos >= parser.bytes.len() {
        return Err(ParseError::new(
            ParseErrorKind::EmptyInput,
            parser.pos,
            "input holds only whitespace".to_string(),
        ));
    }
    let value = parser.parse_value(1)?;
    parser.skip_whitespace();
    if parser.pos != parser.bytes.len() {
        return Err(ParseError::new(
            ParseErrorKind::TrailingBytes,
            parser.pos,
            format!(
                "trailing byte 0x{:02X} after the top-level value",
                parser.bytes[parser.pos]
            ),
        ));
    }
    Ok(value)
}

struct Parser<'a> {
    bytes: &'a [u8],
    pos: usize,
}

impl Parser<'_> {
    fn peek(&self) -> Option<u8> {
        self.bytes.get(self.pos).copied()
    }

    const fn error(&self, kind: ParseErrorKind, detail: String) -> ParseError {
        ParseError::new(kind, self.pos, detail)
    }

    fn skip_whitespace(&mut self) {
        while matches!(self.peek(), Some(b' ' | b'\t' | b'\n' | b'\r')) {
            self.pos += 1;
        }
    }

    /// Parses one value. `depth` is 1 for the top-level value and
    /// grows by one per enclosing container.
    fn parse_value(&mut self, depth: usize) -> Result<Value, ParseError> {
        match self.peek() {
            None => Err(self.error(
                ParseErrorKind::UnexpectedEnd,
                "input ended where a value was expected".to_string(),
            )),
            Some(b'n') => {
                self.parse_literal("null")?;
                Ok(Value::Null)
            }
            Some(b't') => {
                self.parse_literal("true")?;
                Ok(Value::Bool(true))
            }
            Some(b'f') => {
                self.parse_literal("false")?;
                Ok(Value::Bool(false))
            }
            Some(b'"') => Ok(Value::String(self.parse_string()?)),
            Some(b'[') => self.parse_array(depth),
            Some(b'{') => self.parse_object(depth),
            Some(b'-' | b'0'..=b'9') => self.parse_number(),
            Some(byte) => Err(self.error(
                ParseErrorKind::UnexpectedByte,
                format!("byte 0x{byte:02X} cannot start a value"),
            )),
        }
    }

    fn parse_literal(&mut self, literal: &str) -> Result<(), ParseError> {
        let expected = literal.as_bytes();
        let remaining = &self.bytes[self.pos..];
        if remaining.starts_with(expected) {
            self.pos += expected.len();
            return Ok(());
        }
        if remaining.len() < expected.len() && expected.starts_with(remaining) {
            return Err(self.error(
                ParseErrorKind::UnexpectedEnd,
                format!("input ended inside the literal {literal}"),
            ));
        }
        Err(self.error(
            ParseErrorKind::InvalidLiteral,
            format!("expected the literal {literal}"),
        ))
    }

    fn parse_number(&mut self) -> Result<Value, ParseError> {
        let start = self.pos;
        if self.peek() == Some(b'-') {
            self.pos += 1;
        }
        // Integer part: a single zero, or a non-zero digit run.
        match self.peek() {
            Some(b'0') => {
                self.pos += 1;
                if matches!(self.peek(), Some(b'0'..=b'9')) {
                    return Err(self.error(
                        ParseErrorKind::InvalidNumber,
                        "leading zeros are not part of the number grammar".to_string(),
                    ));
                }
            }
            Some(b'1'..=b'9') => {
                while matches!(self.peek(), Some(b'0'..=b'9')) {
                    self.pos += 1;
                }
            }
            _ => {
                return Err(self.error(
                    ParseErrorKind::InvalidNumber,
                    "a number needs at least one integer digit".to_string(),
                ));
            }
        }
        // Fraction part.
        if self.peek() == Some(b'.') {
            self.pos += 1;
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err(self.error(
                    ParseErrorKind::InvalidNumber,
                    "a fraction needs at least one digit".to_string(),
                ));
            }
            while matches!(self.peek(), Some(b'0'..=b'9')) {
                self.pos += 1;
            }
        }
        // Exponent part.
        if matches!(self.peek(), Some(b'e' | b'E')) {
            self.pos += 1;
            if matches!(self.peek(), Some(b'+' | b'-')) {
                self.pos += 1;
            }
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err(self.error(
                    ParseErrorKind::InvalidNumber,
                    "an exponent needs at least one digit".to_string(),
                ));
            }
            while matches!(self.peek(), Some(b'0'..=b'9')) {
                self.pos += 1;
            }
        }
        let text = core::str::from_utf8(&self.bytes[start..self.pos])
            .expect("number bytes are ASCII by construction");
        let value: f64 = text.parse().map_err(|_| {
            self.error(
                ParseErrorKind::InvalidNumber,
                format!("number literal {text} does not convert to f64"),
            )
        })?;
        if value.is_infinite() {
            return Err(ParseError::new(
                ParseErrorKind::NumberOutOfRange,
                start,
                format!("number literal {text} overflows f64"),
            ));
        }
        #[allow(
            clippy::float_cmp,
            reason = "the integrality check of the value-based domain is exact by specification"
        )]
        if value.fract() == 0.0 && value.abs() > MAX_SAFE_INTEGER {
            return Err(ParseError::new(
                ParseErrorKind::IntegerOutOfSafeRange,
                start,
                format!("integer {text} exceeds the safe bound {MAX_SAFE_INTEGER} (2^53 minus 1)"),
            ));
        }
        Ok(Value::Number(value))
    }

    /// Parses a string starting at the opening quote, consuming
    /// through the closing quote.
    fn parse_string(&mut self) -> Result<String, ParseError> {
        debug_assert_eq!(self.peek(), Some(b'"'));
        self.pos += 1;
        let mut out = String::new();
        let mut segment_start = self.pos;
        loop {
            match self.peek() {
                None => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedEnd,
                        "input ended inside a string".to_string(),
                    ));
                }
                Some(b'"') => {
                    out.push_str(
                        core::str::from_utf8(&self.bytes[segment_start..self.pos])
                            .expect("segments end at ASCII boundaries"),
                    );
                    self.pos += 1;
                    return Ok(out);
                }
                Some(b'\\') => {
                    out.push_str(
                        core::str::from_utf8(&self.bytes[segment_start..self.pos])
                            .expect("segments end at ASCII boundaries"),
                    );
                    self.pos += 1;
                    self.parse_escape(&mut out)?;
                    segment_start = self.pos;
                }
                Some(byte) if byte < 0x20 => {
                    return Err(self.error(
                        ParseErrorKind::ControlCharacterInString,
                        format!("raw control byte 0x{byte:02X} inside a string"),
                    ));
                }
                Some(_) => {
                    self.pos += 1;
                }
            }
        }
    }

    /// Parses one escape sequence. `self.pos` is just past the
    /// backslash on entry.
    fn parse_escape(&mut self, out: &mut String) -> Result<(), ParseError> {
        let escape_offset = self.pos - 1;
        match self.peek() {
            None => Err(self.error(
                ParseErrorKind::UnexpectedEnd,
                "input ended after a string backslash".to_string(),
            )),
            Some(b'"') => {
                self.pos += 1;
                out.push('"');
                Ok(())
            }
            Some(b'\\') => {
                self.pos += 1;
                out.push('\\');
                Ok(())
            }
            Some(b'/') => {
                self.pos += 1;
                out.push('/');
                Ok(())
            }
            Some(b'b') => {
                self.pos += 1;
                out.push('\u{08}');
                Ok(())
            }
            Some(b'f') => {
                self.pos += 1;
                out.push('\u{0C}');
                Ok(())
            }
            Some(b'n') => {
                self.pos += 1;
                out.push('\n');
                Ok(())
            }
            Some(b'r') => {
                self.pos += 1;
                out.push('\r');
                Ok(())
            }
            Some(b't') => {
                self.pos += 1;
                out.push('\t');
                Ok(())
            }
            Some(b'u') => {
                self.pos += 1;
                let code = self.parse_hex4()?;
                if (0xD800..0xDC00).contains(&code) {
                    // A high surrogate must be followed immediately by
                    // a low surrogate escape.
                    if self.peek() == Some(b'\\') && self.bytes.get(self.pos + 1) == Some(&b'u') {
                        self.pos += 2;
                        let low = self.parse_hex4()?;
                        if (0xDC00..0xE000).contains(&low) {
                            let combined = 0x1_0000 + ((code - 0xD800) << 10) + (low - 0xDC00);
                            let ch = char::from_u32(combined)
                                .expect("a valid surrogate pair forms a valid scalar value");
                            out.push(ch);
                            return Ok(());
                        }
                    }
                    Err(ParseError::new(
                        ParseErrorKind::LoneSurrogate,
                        escape_offset,
                        format!("high surrogate \\u{code:04X} has no low surrogate partner"),
                    ))
                } else if (0xDC00..0xE000).contains(&code) {
                    Err(ParseError::new(
                        ParseErrorKind::LoneSurrogate,
                        escape_offset,
                        format!("low surrogate \\u{code:04X} has no high surrogate partner"),
                    ))
                } else {
                    let ch = char::from_u32(code)
                        .expect("a non-surrogate BMP code unit is a valid scalar value");
                    out.push(ch);
                    Ok(())
                }
            }
            Some(byte) => Err(self.error(
                ParseErrorKind::InvalidEscape,
                format!("escape byte 0x{byte:02X} is not a JSON escape"),
            )),
        }
    }

    /// Reads exactly four hexadecimal digits after a `\u`.
    fn parse_hex4(&mut self) -> Result<u32, ParseError> {
        let mut code: u32 = 0;
        for _ in 0..4 {
            match self.peek() {
                None => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedEnd,
                        "input ended inside a unicode escape".to_string(),
                    ));
                }
                Some(byte) => {
                    let digit = match byte {
                        b'0'..=b'9' => u32::from(byte - b'0'),
                        b'a'..=b'f' => u32::from(byte - b'a') + 10,
                        b'A'..=b'F' => u32::from(byte - b'A') + 10,
                        _ => {
                            return Err(self.error(
                                ParseErrorKind::InvalidUnicodeEscape,
                                format!("byte 0x{byte:02X} is not a hexadecimal digit"),
                            ));
                        }
                    };
                    code = code * 16 + digit;
                    self.pos += 1;
                }
            }
        }
        Ok(code)
    }

    fn parse_array(&mut self, depth: usize) -> Result<Value, ParseError> {
        if depth > MAX_DEPTH {
            return Err(self.error(
                ParseErrorKind::DepthLimit,
                format!("nesting exceeds the depth limit of {MAX_DEPTH}"),
            ));
        }
        self.pos += 1; // consume '['
        self.skip_whitespace();
        let mut items = Vec::new();
        if self.peek() == Some(b']') {
            self.pos += 1;
            return Ok(Value::Array(items));
        }
        loop {
            self.skip_whitespace();
            let item = self.parse_value(depth + 1)?;
            items.push(item);
            self.skip_whitespace();
            match self.peek() {
                Some(b',') => {
                    self.pos += 1;
                }
                Some(b']') => {
                    self.pos += 1;
                    return Ok(Value::Array(items));
                }
                Some(byte) => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedByte,
                        format!("byte 0x{byte:02X} where ',' or ']' was expected"),
                    ));
                }
                None => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedEnd,
                        "input ended inside an array".to_string(),
                    ));
                }
            }
        }
    }

    fn parse_object(&mut self, depth: usize) -> Result<Value, ParseError> {
        if depth > MAX_DEPTH {
            return Err(self.error(
                ParseErrorKind::DepthLimit,
                format!("nesting exceeds the depth limit of {MAX_DEPTH}"),
            ));
        }
        self.pos += 1; // consume '{'
        self.skip_whitespace();
        let mut pairs: Vec<(String, Value)> = Vec::new();
        // The duplicate check scans the pairs directly while the
        // object is small, which covers every object the system
        // itself produces, and switches to a membership set past
        // sixteen keys so a hand crafted large object cannot make
        // the check quadratic (finding F16, decided on measurement
        // in perfection round P3). The set serves membership only
        // and is never iterated, so its order cannot reach the
        // output or an error.
        let mut seen_keys: Option<std::collections::HashSet<String>> = None;
        if self.peek() == Some(b'}') {
            self.pos += 1;
            return Ok(Value::Object(pairs));
        }
        loop {
            self.skip_whitespace();
            let key_offset = self.pos;
            if self.peek() != Some(b'"') {
                return Err(self.peek().map_or_else(
                    || {
                        self.error(
                            ParseErrorKind::UnexpectedEnd,
                            "input ended inside an object".to_string(),
                        )
                    },
                    |byte| {
                        self.error(
                            ParseErrorKind::UnexpectedByte,
                            format!("byte 0x{byte:02X} where an object key was expected"),
                        )
                    },
                ));
            }
            let key = self.parse_string()?;
            let duplicate = if pairs.len() < 16 {
                pairs.iter().any(|(existing, _)| existing == &key)
            } else {
                let seen = seen_keys.get_or_insert_with(|| {
                    pairs.iter().map(|(existing, _)| existing.clone()).collect()
                });
                !seen.insert(key.clone())
            };
            if duplicate {
                return Err(ParseError::new(
                    ParseErrorKind::DuplicateKey,
                    key_offset,
                    format!("duplicate object key {key:?}"),
                ));
            }
            self.skip_whitespace();
            match self.peek() {
                Some(b':') => {
                    self.pos += 1;
                }
                Some(byte) => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedByte,
                        format!("byte 0x{byte:02X} where ':' was expected"),
                    ));
                }
                None => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedEnd,
                        "input ended after an object key".to_string(),
                    ));
                }
            }
            self.skip_whitespace();
            let value = self.parse_value(depth + 1)?;
            pairs.push((key, value));
            self.skip_whitespace();
            match self.peek() {
                Some(b',') => {
                    self.pos += 1;
                }
                Some(b'}') => {
                    self.pos += 1;
                    return Ok(Value::Object(pairs));
                }
                Some(byte) => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedByte,
                        format!("byte 0x{byte:02X} where ',' or '}}' was expected"),
                    ));
                }
                None => {
                    return Err(self.error(
                        ParseErrorKind::UnexpectedEnd,
                        "input ended inside an object".to_string(),
                    ));
                }
            }
        }
    }
}