vole-document 0.1.0-alpha.6

Byte-exact procedural document storage: deterministic reconstruction state, typed residuals, and entropy-coded channels that materialize the exact original document bytes.
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! PDF layout candidate: regenerate classic cross-reference entry offsets and
//! the `startxref` value from positions marked during materialization.
//!
//! This is the first Phase-5 candidate that replaces literal structural bytes
//! with *procedurally determined* ones. The mechanism is deliberately narrow and
//! conservative:
//!
//! * It applies only to files that already carry a classic cross-reference
//!   section ([`PhysicalKind::XrefSection`]) and contain no
//!   [`ObjRole::XRefStream`] object. Anything else declines (`Ok(None)`).
//! * Every literal byte is accumulated into a single data object and the whole
//!   reconstruction is one compact [`Op::PackSegments`] item table, so the
//!   per-segment framing is paid once rather than once per span or xref entry.
//! * It records each indirect object's introducer offset with a
//!   [`PackItem::Mark`] (slot = the object's index in [`PdfPhysical::objects`])
//!   and, for every `n`-status xref entry whose 10-digit offset field equals the
//!   marked position of its target object, emits that field with a
//!   [`PackItem::Emit`] rather than storing the digits literally.
//! * The most recent `xref` section start is marked in the reserved slot
//!   [`XREF_SLOT`] (`255`); each `startxref` value is regenerated from it only
//!   when the emitted position equals the source value.
//! * Whenever a precondition fails — a non-standard offset field, a mismatched
//!   position, a malformed table, too many objects — the site (or the whole
//!   section) falls back to a literal [`PackItem::Literal`]. Prediction never
//!   invents bytes: a fallback is always byte-exact, and a prediction is only
//!   emitted when it reproduces the source digits exactly.
//!
//! After building the program the candidate is verified end-to-end (serialize,
//! parse, materialize, byte-compare) before it is returned; if that round trip
//! is not exact, the proposal declines rather than emitting an inexact
//! candidate.

use crate::SOURCE_FORMAT_PDF;
use crate::container::{Descriptor, UNIVERSE};
use crate::dra::op::PackItem;
use crate::dra::{Op, Program};
use crate::encode::candidates::{Candidate, CandidateKind};
use crate::error::Result;
use crate::integrity::sha256;
use crate::limits::Limits;

use super::physical::{ObjRole, PdfObjectSpan, PhysicalKind, scan};

/// Reserved slot index for the most recent classic `xref` section start.
pub const XREF_SLOT: u8 = u8::MAX;

/// Largest number of indirect objects that can be marked (indices `0..=254`),
/// leaving slot `255` free for [`XREF_SLOT`].
pub const MAX_MARKED_OBJECTS: usize = XREF_SLOT as usize;

/// Propose a layout candidate that regenerates xref offsets / startxref, or
/// `None`.
///
/// Declines (`Ok(None)`) whenever a precondition fails or the built program does
/// not materialize byte-for-byte. See the module documentation for the algorithm.
pub fn propose_pdf_layout(input: &[u8], limits: Limits) -> Result<Option<Candidate>> {
    let physical = match scan(input, limits) {
        Ok(p) => p,
        Err(_) => return Ok(None),
    };

    // Precondition: a classic cross-reference section must exist, and no
    // cross-reference stream may be present.
    if !physical
        .spans
        .iter()
        .any(|s| s.kind == PhysicalKind::XrefSection)
    {
        return Ok(None);
    }
    if physical
        .objects
        .iter()
        .any(|o| o.role == ObjRole::XRefStream)
    {
        return Ok(None);
    }
    if physical.objects.len() > MAX_MARKED_OBJECTS {
        return Ok(None);
    }

    // The data object carries every literal byte; the compact item table
    // interleaves literal runs, position marks, and regenerated offsets so the
    // per-segment framing is paid once rather than once per span/entry.
    let mut data: Vec<u8> = Vec::new();
    let mut items: Vec<PackItem> = Vec::new();
    // Simulated output position. The physical cover is contiguous, so this
    // tracks the source offset of the next byte exactly: literals add their
    // length, emits add their width, and marks add nothing.
    let mut pos: u64 = 0;
    let mut slot_value = [0u64; 256];
    let mut slot_marked = [false; 256];
    let mut xref_predicted: u64 = 0;
    let mut xref_literal: u64 = 0;
    let mut startxref_predicted: u64 = 0;

    for span in &physical.spans {
        let start = span.start as usize;
        let end = start + span.len as usize;
        let bytes = &input[start..end];

        if pos != span.start {
            // A non-contiguous simulation would break the offset contract; bail.
            return Ok(None);
        }

        match span.kind {
            PhysicalKind::ObjHeader => {
                if let Some(idx) = object_index_at(&physical.objects, span.start) {
                    let slot = idx as u8;
                    items.push(PackItem::Mark { slot });
                    slot_value[slot as usize] = pos;
                    slot_marked[slot as usize] = true;
                }
                if !push_pack_literal(&mut data, &mut items, bytes, &mut pos) {
                    return Ok(None);
                }
            }
            PhysicalKind::XrefSection => {
                // Mark this section's start in the reserved slot, then emit.
                items.push(PackItem::Mark { slot: XREF_SLOT });
                slot_value[XREF_SLOT as usize] = pos;
                slot_marked[XREF_SLOT as usize] = true;

                match parse_classic_xref(bytes) {
                    Some(pieces) => {
                        for piece in pieces {
                            match piece {
                                XrefPiece::Literal { start, len } => {
                                    if !push_pack_literal(
                                        &mut data,
                                        &mut items,
                                        &bytes[start..start + len],
                                        &mut pos,
                                    ) {
                                        return Ok(None);
                                    }
                                }
                                XrefPiece::Entry {
                                    start,
                                    number,
                                    offset,
                                    in_use,
                                } => {
                                    let slot = if in_use {
                                        offset.and_then(|value| {
                                            predicted_slot(
                                                &physical.objects,
                                                &slot_value,
                                                &slot_marked,
                                                number,
                                                value,
                                            )
                                        })
                                    } else {
                                        None
                                    };
                                    match slot {
                                        Some(slot) => {
                                            items.push(PackItem::Emit { slot, width: 10 });
                                            pos += 10;
                                            if !push_pack_literal(
                                                &mut data,
                                                &mut items,
                                                &bytes[start + 10..start + 20],
                                                &mut pos,
                                            ) {
                                                return Ok(None);
                                            }
                                            xref_predicted += 1;
                                        }
                                        None => {
                                            if !push_pack_literal(
                                                &mut data,
                                                &mut items,
                                                &bytes[start..start + 20],
                                                &mut pos,
                                            ) {
                                                return Ok(None);
                                            }
                                            xref_literal += 1;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    None => {
                        // Not a classic table we understand: literal whole section.
                        if !push_pack_literal(&mut data, &mut items, bytes, &mut pos) {
                            return Ok(None);
                        }
                    }
                }
            }
            PhysicalKind::StartXref => match predict_startxref(bytes, &slot_value, &slot_marked) {
                Some((prefix_len, width)) => {
                    if !push_pack_literal(&mut data, &mut items, &bytes[..prefix_len], &mut pos) {
                        return Ok(None);
                    }
                    items.push(PackItem::Emit {
                        slot: XREF_SLOT,
                        width,
                    });
                    pos += width as u64;
                    startxref_predicted += 1;
                }
                None => {
                    if !push_pack_literal(&mut data, &mut items, bytes, &mut pos) {
                        return Ok(None);
                    }
                }
            },
            _ => {
                if !push_pack_literal(&mut data, &mut items, bytes, &mut pos) {
                    return Ok(None);
                }
            }
        }
    }

    let format_basis = format!(
        "pdf-layout;objects={};xref_predicted={};xref_literal={};startxref_predicted={}",
        physical.objects.len(),
        xref_predicted,
        xref_literal,
        startxref_predicted
    );

    let descriptor = Descriptor {
        universe: UNIVERSE.to_string(),
        source_format: SOURCE_FORMAT_PDF,
        format_basis,
        models: vec![],
        channels: vec![],
        objects: vec![data],
        program: Program::new(vec![Op::PackSegments {
            data_object: 0,
            items,
        }]),
        source_sha256: sha256(input),
        source_len: input.len() as u64,
    };

    let candidate = Candidate {
        kind: CandidateKind::PdfLayout,
        descriptor,
    };

    // Verify byte-exactness through the normative decoder before returning. An
    // inexact program must never be emitted.
    let (encoded, _) = candidate.descriptor.serialize()?;
    let parsed = match Descriptor::parse(&encoded, limits) {
        Ok(p) => p,
        Err(_) => return Ok(None),
    };
    let out = match crate::materialize::materialize(&parsed, limits) {
        Ok(o) => o,
        Err(_) => return Ok(None),
    };
    if out != input {
        return Ok(None);
    }

    Ok(Some(candidate))
}

/// Append `bytes` to the packed data object, recording one [`PackItem::Literal`]
/// when non-empty, and advance the simulated output position. Returns `false`
/// (so the caller declines) when a single run would not fit a `u32` length.
fn push_pack_literal(
    data: &mut Vec<u8>,
    items: &mut Vec<PackItem>,
    bytes: &[u8],
    pos: &mut u64,
) -> bool {
    if !push_literal(items, data, bytes) {
        return false;
    }
    *pos += bytes.len() as u64;
    true
}

/// Append `bytes` to the packed data object, coalescing them into the immediately
/// preceding [`PackItem::Literal`] when one is present so that consecutive literal
/// runs collapse into the fewest possible items. The merge never crosses a
/// [`PackItem::Mark`] or [`PackItem::Emit`], empty pushes are ignored, and the
/// combined length must remain expressible as a `u32`. Returns `false` (so the
/// caller declines) when no safe item shape exists.
fn push_literal(items: &mut Vec<PackItem>, data: &mut Vec<u8>, bytes: &[u8]) -> bool {
    if bytes.is_empty() {
        return true;
    }
    let Ok(len) = u32::try_from(bytes.len()) else {
        return false;
    };
    if let Some(PackItem::Literal { len: prev }) = items.last_mut() {
        let Some(total) = prev.checked_add(len) else {
            return false;
        };
        *prev = total;
    } else {
        items.push(PackItem::Literal { len });
    }
    data.extend_from_slice(bytes);
    true
}

/// Index of the first object whose introducer starts at `start`.
fn object_index_at(objects: &[PdfObjectSpan], start: u64) -> Option<usize> {
    objects.iter().position(|o| o.start == start)
}

/// The slot marking the target object `number` at position `value`, if any
/// earlier [`Op::MarkOffset`] recorded exactly that position.
fn predicted_slot(
    objects: &[PdfObjectSpan],
    slot_value: &[u64; 256],
    slot_marked: &[bool; 256],
    number: u64,
    value: u64,
) -> Option<u8> {
    objects.iter().enumerate().find_map(|(i, o)| {
        let slot = i as u8;
        (o.number == number && slot_marked[slot as usize] && slot_value[slot as usize] == value)
            .then_some(slot)
    })
}

/// Predict a whole `startxref` span: the trailing run of decimal digits is the
/// value. Returns `(prefix_len, width)` when the value equals the marked `xref`
/// position and its width is in `1..=20`.
fn predict_startxref(
    bytes: &[u8],
    slot_value: &[u64; 256],
    slot_marked: &[bool; 256],
) -> Option<(usize, u8)> {
    if !slot_marked[XREF_SLOT as usize] {
        return None;
    }
    let mut i = bytes.len();
    while i > 0 && bytes[i - 1].is_ascii_digit() {
        i -= 1;
    }
    let width = bytes.len() - i;
    if width == 0 || width > 20 {
        return None;
    }
    let value = parse_digits(&bytes[i..])?;
    if value != slot_value[XREF_SLOT as usize] {
        return None;
    }
    Some((i, width as u8))
}

/// One ordered slice of a classic `xref` section: either literal bytes or a
/// 20-byte entry whose offset field may be regenerated.
enum XrefPiece {
    /// Verbatim bytes `[start, start + len)` of the section.
    Literal { start: usize, len: usize },
    /// A 20-byte entry `[start, start + 20)`.
    Entry {
        /// Offset of the entry within the section.
        start: usize,
        /// Target object number (`subsection_start + i`).
        number: u64,
        /// Parsed value of the 10-digit offset field, if it is all digits.
        offset: Option<u64>,
        /// Whether the status byte is `n` (in use).
        in_use: bool,
    },
}

/// Parse a classic cross-reference table from a section's bytes, returning an
/// ordered tiling of the section. Returns `None` (so the caller emits the whole
/// section literally) when the bytes do not match the classic grammar.
///
/// Grammar accepted: `xref` EOL, then one or more `<start> <count>` EOL headers
/// each followed by exactly `count` 20-byte entries of the shape
/// `10-digit-offset SP 5-digit-generation SP status 2-byte-EOL`. The two EOL
/// bytes may be `CR LF`, `LF CR`, `SP LF`, or `SP CR`.
fn parse_classic_xref(bytes: &[u8]) -> Option<Vec<XrefPiece>> {
    if !bytes.starts_with(b"xref") {
        return None;
    }
    let mut pieces = Vec::new();
    let mut pos = 4usize;

    // EOL after the `xref` keyword.
    let eol = eol_len(&bytes[pos..])?;
    pieces.push(XrefPiece::Literal {
        start: 0,
        len: pos + eol,
    });
    pos += eol;

    let mut any = false;
    while pos < bytes.len() {
        // Subsection header: `<start> <count>` EOL.
        let header_start = pos;
        let (start, after_start) = parse_uint_at(bytes, pos)?;
        pos = after_start;
        let spaces_start = pos;
        while pos < bytes.len() && bytes[pos] == b' ' {
            pos += 1;
        }
        if pos == spaces_start {
            return None;
        }
        let (count, after_count) = parse_uint_at(bytes, pos)?;
        pos = after_count;
        let eol = eol_len(&bytes[pos..])?;
        let header_end = pos + eol;
        pieces.push(XrefPiece::Literal {
            start: header_start,
            len: header_end - header_start,
        });
        pos = header_end;

        for i in 0..count {
            let end = pos.checked_add(20)?;
            if end > bytes.len() {
                return None;
            }
            let entry = &bytes[pos..end];
            if !is_entry_shape(entry) {
                return None;
            }
            let number = start.checked_add(i)?;
            let in_use = entry[17] == b'n';
            let offset = parse_digits(&entry[0..10]);
            pieces.push(XrefPiece::Entry {
                start: pos,
                number,
                offset,
                in_use,
            });
            pos = end;
        }
        any = true;
    }

    if !any || pos != bytes.len() {
        return None;
    }
    Some(pieces)
}

/// Whether a 20-byte window matches the classic cross-reference entry shape.
fn is_entry_shape(entry: &[u8]) -> bool {
    if entry.len() != 20 {
        return false;
    }
    if entry[10] != b' ' || entry[16] != b' ' {
        return false;
    }
    if !entry[11..16].iter().all(u8::is_ascii_digit) {
        return false;
    }
    if entry[17] != b'n' && entry[17] != b'f' {
        return false;
    }
    matches!(
        (entry[18], entry[19]),
        (b'\r', b'\n') | (b'\n', b'\r') | (b' ', b'\n') | (b' ', b'\r')
    )
}

/// Length of an end-of-line marker at the start of `bytes`, if any.
fn eol_len(bytes: &[u8]) -> Option<usize> {
    match bytes {
        [b'\r', b'\n', ..] => Some(2),
        [b'\n', ..] | [b'\r', ..] => Some(1),
        _ => None,
    }
}

/// Parse a non-negative decimal integer at `at`, returning `(value, next)`.
fn parse_uint_at(bytes: &[u8], at: usize) -> Option<(u64, usize)> {
    let mut i = at;
    let mut value: u64 = 0;
    while i < bytes.len() && bytes[i].is_ascii_digit() {
        value = value
            .checked_mul(10)?
            .checked_add(u64::from(bytes[i] - b'0'))?;
        i += 1;
    }
    if i == at {
        return None;
    }
    Some((value, i))
}

/// Parse an all-digit slice as a non-negative decimal integer.
fn parse_digits(digits: &[u8]) -> Option<u64> {
    if digits.is_empty() {
        return None;
    }
    let mut value: u64 = 0;
    for &b in digits {
        if !b.is_ascii_digit() {
            return None;
        }
        value = value.checked_mul(10)?.checked_add(u64::from(b - b'0'))?;
    }
    Some(value)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapter::pdf::samples::{is_negative_control, sample_pdfs};
    use crate::container::Descriptor;

    fn sample(name: &str) -> Vec<u8> {
        sample_pdfs()
            .into_iter()
            .find(|(n, _)| *n == name)
            .unwrap_or_else(|| panic!("sample {name} missing"))
            .1
    }

    fn basis_field(basis: &str, key: &str) -> Option<u64> {
        basis.split(';').find_map(|part| {
            let (k, v) = part.split_once('=')?;
            (k == key).then(|| v.parse().ok()).flatten()
        })
    }

    /// The item table of the single `PackSegments` op this candidate builds.
    fn pack_items(cand: &Candidate) -> &[PackItem] {
        cand.descriptor
            .program
            .ops
            .iter()
            .find_map(|op| match op {
                Op::PackSegments { items, .. } => Some(items.as_slice()),
                _ => None,
            })
            .expect("layout program is one PackSegments op")
    }

    /// Number of regenerated offsets in the packed item table.
    fn pack_emit_count(cand: &Candidate) -> usize {
        pack_items(cand)
            .iter()
            .filter(|item| matches!(item, PackItem::Emit { .. }))
            .count()
    }

    fn assert_materializes_exactly(name: &str, bytes: &[u8]) {
        let cand = propose_pdf_layout(bytes, Limits::DEFAULT)
            .unwrap()
            .unwrap_or_else(|| panic!("{name} must propose a layout candidate"));
        assert_eq!(cand.kind, CandidateKind::PdfLayout);
        assert_eq!(cand.descriptor.source_format, SOURCE_FORMAT_PDF);
        assert_eq!(cand.descriptor.source_len, bytes.len() as u64);
        // Exactly one object: the packed data object holding every literal byte.
        assert_eq!(
            cand.descriptor.objects.len(),
            1,
            "{name} layout carries one packed data object"
        );
        assert!(matches!(
            cand.descriptor.program.ops.as_slice(),
            [Op::PackSegments { .. }]
        ));
        assert!(cand.descriptor.models.is_empty());
        assert!(cand.descriptor.channels.is_empty());

        let (encoded, _) = cand.descriptor.serialize().unwrap();
        let parsed = Descriptor::parse(&encoded, Limits::DEFAULT).unwrap();
        let out = crate::materialize::materialize(&parsed, Limits::DEFAULT).unwrap();
        assert_eq!(out, bytes, "{name} layout candidate materializes exactly");
        assert_eq!(sha256(&out), sha256(bytes), "{name} layout sha");
    }

    #[test]
    fn layout_is_exact_on_corpus() {
        let mut accepted = 0usize;
        for (name, bytes) in sample_pdfs() {
            if is_negative_control(name) {
                assert!(
                    propose_pdf_layout(&bytes, Limits::DEFAULT)
                        .unwrap()
                        .is_none(),
                    "{name} is not a classic-xref PDF and must decline"
                );
                continue;
            }
            if propose_pdf_layout(&bytes, Limits::DEFAULT)
                .unwrap()
                .is_some()
            {
                assert_materializes_exactly(name, &bytes);
                accepted += 1;
            }
        }
        assert!(
            accepted >= 3,
            "expected several accepted classic-xref samples, found {accepted}"
        );
    }

    #[test]
    fn layout_v2_exact() {
        for name in ["classic.pdf", "many.pdf", "bigtext.pdf"] {
            assert_materializes_exactly(name, &sample(name));
        }
    }

    #[test]
    fn layout_v2_predicts_many_entries() {
        let bytes = sample("many.pdf");
        let cand = propose_pdf_layout(&bytes, Limits::DEFAULT)
            .unwrap()
            .unwrap();
        let emits = pack_emit_count(&cand);
        assert!(
            emits >= 100,
            "many.pdf must predict at least 100 xref offsets, got {emits}"
        );
        // Every Emit is either a predicted xref entry or the predicted startxref.
        let predicted = basis_field(&cand.descriptor.format_basis, "xref_predicted")
            .expect("format basis must report xref_predicted");
        let startxref = basis_field(&cand.descriptor.format_basis, "startxref_predicted")
            .expect("format basis must report startxref_predicted");
        assert_eq!(predicted + startxref, emits as u64);
        assert!(predicted >= 100, "many.pdf xref predictions: {predicted}");

        // The classic sample still predicts, and the pack framing stays compact.
        let classic = propose_pdf_layout(&sample("classic.pdf"), Limits::DEFAULT)
            .unwrap()
            .unwrap();
        assert!(pack_emit_count(&classic) > 0);
    }

    #[test]
    fn layout_falls_back_on_bad_offset() {
        // Object 1's real offset is `off1`, but the xref entry records a wrong
        // value. The entry must stay literal. `startxref` is still correct, so it
        // is the only predicted offset in the whole program.
        let mut b: Vec<u8> = Vec::new();
        b.extend_from_slice(b"%PDF-1.4\n");
        let off1 = b.len() as u64;
        b.extend_from_slice(b"1 0 obj\n<< /Type /Catalog >>\nendobj\n");
        let xref = b.len() as u64;
        let wrong = off1 + 3;
        b.extend_from_slice(
            format!("xref\n0 2\n0000000000 65535 f \n{wrong:010} 00000 n \n").as_bytes(),
        );
        b.extend_from_slice(
            format!("trailer\n<< /Size 2 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n").as_bytes(),
        );

        let cand = propose_pdf_layout(&b, Limits::DEFAULT).unwrap().unwrap();
        assert_eq!(
            basis_field(&cand.descriptor.format_basis, "xref_predicted"),
            Some(0),
            "a mismatched offset must never be predicted"
        );

        // The only regenerated offset is the correctly predicted `startxref`.
        let emits = pack_emit_count(&cand);
        assert_eq!(
            emits, 1,
            "only startxref is predicted; bad entry is literal"
        );

        let (encoded, _) = cand.descriptor.serialize().unwrap();
        let parsed = Descriptor::parse(&encoded, Limits::DEFAULT).unwrap();
        let out = crate::materialize::materialize(&parsed, Limits::DEFAULT).unwrap();
        assert_eq!(out, b, "fallback must still be byte-exact");
    }

    #[test]
    fn layout_v2_declines() {
        // A cross-reference-stream PDF has no classic table to regenerate.
        assert!(
            propose_pdf_layout(&sample("xrefstream.pdf"), Limits::DEFAULT)
                .unwrap()
                .is_none(),
            "an xref-stream PDF must decline the layout candidate"
        );
        // More objects than the 255 markable slots cannot be expressed exactly
        // under the slot bound, so the candidate must decline rather than guess.
        assert!(
            propose_pdf_layout(&classic_with_objects(256), Limits::DEFAULT)
                .unwrap()
                .is_none(),
            "256 objects exceeds the 255 markable slots"
        );
        // 255 objects still fit: indices 0..=254, slot 255 reserved for xref.
        assert!(
            propose_pdf_layout(&classic_with_objects(255), Limits::DEFAULT)
                .unwrap()
                .is_some(),
            "255 objects must still be markable"
        );
    }

    /// Build a classic-xref PDF with `n` indirect objects and correct offsets.
    fn classic_with_objects(n: usize) -> Vec<u8> {
        let mut b: Vec<u8> = Vec::new();
        b.extend_from_slice(b"%PDF-1.4\n");
        let mut offsets = Vec::with_capacity(n);
        for number in 1..=n {
            offsets.push(b.len() as u64);
            b.extend_from_slice(format!("{number} 0 obj\n<< >>\nendobj\n").as_bytes());
        }
        let xref = b.len() as u64;
        b.extend_from_slice(format!("xref\n0 {}\n", n + 1).as_bytes());
        b.extend_from_slice(b"0000000000 65535 f \n");
        for &off in &offsets {
            b.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
        }
        b.extend_from_slice(
            format!(
                "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n",
                n + 1
            )
            .as_bytes(),
        );
        b
    }

    #[test]
    fn layout_v2_deterministic() {
        for name in ["classic.pdf", "many.pdf", "bigtext.pdf"] {
            let bytes = sample(name);
            let a = propose_pdf_layout(&bytes, Limits::DEFAULT)
                .unwrap()
                .unwrap()
                .descriptor
                .serialize()
                .unwrap()
                .0;
            let b = propose_pdf_layout(&bytes, Limits::DEFAULT)
                .unwrap()
                .unwrap()
                .descriptor
                .serialize()
                .unwrap()
                .0;
            assert_eq!(a, b, "{name} layout bytes must be deterministic");
        }
    }

    #[test]
    fn layout_measurements_report() {
        for (name, bytes) in sample_pdfs() {
            let Some(cand) = propose_pdf_layout(&bytes, Limits::DEFAULT).unwrap() else {
                eprintln!("layout[{name}]: declined");
                continue;
            };
            let (layout_bytes, _) = cand.descriptor.serialize().unwrap();
            let emits = pack_emit_count(&cand);
            eprintln!(
                "layout[{name}] source={} items={} emits={emits} layout={}",
                bytes.len(),
                pack_items(&cand).len(),
                layout_bytes.len(),
            );
        }

        for name in ["classic.pdf", "bigtext.pdf", "many.pdf"] {
            let bytes = sample(name);
            let (layout_bytes, _) =
                crate::encode::encode_with(&bytes, Limits::DEFAULT, Some(CandidateKind::PdfLayout))
                    .unwrap();
            let (raw_bytes, _) =
                crate::encode::encode_with(&bytes, Limits::DEFAULT, Some(CandidateKind::Raw))
                    .unwrap();
            #[cfg(feature = "rans")]
            let (byte_rans_bytes, _) =
                crate::encode::encode_with(&bytes, Limits::DEFAULT, Some(CandidateKind::ByteRans))
                    .unwrap();
            #[cfg(not(feature = "rans"))]
            let byte_rans_bytes: Vec<u8> = Vec::new();
            let (_, auto) = crate::encode::encode(&bytes, Limits::DEFAULT).unwrap();
            eprintln!(
                "sizes[{name}] source={} layout_v2={} raw={} byte_rans={} auto={}({})",
                bytes.len(),
                layout_bytes.len(),
                raw_bytes.len(),
                byte_rans_bytes.len(),
                auto.kind.name(),
                auto.encoded_len,
            );
        }
    }
}