oxideav-pdf 0.1.2

Pure-Rust PDF writer for the oxideav framework — vector-stays-vector path
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
//! PDF object parser — [`Lexer`] tokens → [`Object`] tree.
//!
//! Round-3 Commit 2: builds on [`crate::reader::lex`] to recognise
//! every variant the writer can emit (numbers, names, strings,
//! arrays, dicts, indirect references, stream objects, null).
//!
//! Streams are matched by the trailing `stream`+EOL marker per ISO
//! 32000-1 §7.3.8. The body is sliced verbatim from the input — no
//! filter decoding here; the reader's stream object handler (round-3
//! commit 4) re-runs FlateDecode if the dictionary's `/Filter` says
//! so.
//!
//! Object streams (PDF 1.5+, `/Type /ObjStm`) are deliberately out of
//! scope for round 3 — the writer doesn't emit them and parsing them
//! requires a full /XRef stream walker. Round-4+.

use crate::error::PdfError;
use crate::objects::{Dict, Object, ObjectId, Stream};
use crate::reader::lex::{Lexer, Token, TokenKind};

/// Caller-supplied hook that resolves an indirect `/Length` reference
/// on a stream dictionary to the underlying integer.
///
/// ISO 32000-1 §7.3.10 Example 3 makes indirect `/Length` normative on
/// stream objects: a one-pass writer that doesn't know the encoded
/// stream size up front emits `<< /Length 8 0 R >>` and writes the
/// length-carrying integer object after the stream. A reader walking
/// such a PDF needs the xref table to fetch the integer.
///
/// The trait receives the [`ObjectId`] from the reference and is
/// expected to return the resolved integer (or a [`PdfError`] when the
/// target is missing / not an integer / a cycle is detected). Using a
/// trait (rather than a free `FnMut`) sidesteps the lifetime
/// gymnastics needed to thread an `Option<&mut dyn FnMut>` through
/// multiple recursive parse helpers — implementers just write a tiny
/// struct holding the borrowed state they need.
///
/// When the resolver is the unit no-op [`NoLengthResolver`] (the
/// [`Parser::parse_indirect`] / [`Parser::parse_object`] entry
/// points), an indirect `/Length` is rejected — that path is reserved
/// for xref-stream parsing, where the xref isn't built yet and the
/// spec (§7.5.8) effectively requires the direct-integer form anyway.
pub trait LengthResolver {
    /// Resolve the integer carried by the indirect object at
    /// `length_ref`. Returning `Err(_)` flows up the parse stack and
    /// aborts the stream-object decode.
    fn resolve_length(&mut self, length_ref: ObjectId) -> Result<i64, PdfError>;
}

/// No-op [`LengthResolver`] — every indirect `/Length` is rejected.
/// Used by [`Parser::parse_indirect`] and [`Parser::parse_object`] on
/// code paths where the xref table isn't built yet (the xref-stream
/// parser, the `parse_xref_stream_at` helper).
pub struct NoLengthResolver;

impl LengthResolver for NoLengthResolver {
    fn resolve_length(&mut self, _length_ref: ObjectId) -> Result<i64, PdfError> {
        Err(PdfError::other(
            "PDF parser: stream /Length is an indirect reference but no resolver \
             was supplied (call parse_indirect_with_length_resolver)",
        ))
    }
}

/// Blanket impl so closures with the same signature work as
/// [`LengthResolver`]s without writing a struct.
impl<F> LengthResolver for F
where
    F: FnMut(ObjectId) -> Result<i64, PdfError>,
{
    fn resolve_length(&mut self, length_ref: ObjectId) -> Result<i64, PdfError> {
        (self)(length_ref)
    }
}

/// Recursive-descent parser over a [`Lexer`].
pub struct Parser<'a> {
    lex: Lexer<'a>,
    /// Lookahead slot — when [`Self::peek`] reads a token, it stores
    /// it here so the next [`Self::next`] returns it instead of
    /// pulling from the lexer. Matches the conventional 1-token
    /// lookahead recursive-descent shape.
    peeked: Option<Token<'a>>,
}

impl<'a> Parser<'a> {
    pub fn new(input: &'a [u8]) -> Self {
        Self {
            lex: Lexer::new(input),
            peeked: None,
        }
    }

    /// Construct from an existing [`Lexer`] — used by the xref /
    /// trailer scanner so the same cursor state is shared.
    pub fn from_lexer(lex: Lexer<'a>) -> Self {
        Self { lex, peeked: None }
    }

    /// Borrow the underlying lexer (for `seek` / `slice` / `position`).
    pub fn lexer_mut(&mut self) -> &mut Lexer<'a> {
        // Drop any peeked token — its position cache is stale once the
        // caller moves the cursor.
        self.peeked = None;
        &mut self.lex
    }

    /// Current byte position. Returns the start of the peeked token
    /// when one is buffered (so the position reflects "the next
    /// token's start", not "wherever the lexer happens to be").
    pub fn position(&self) -> usize {
        if let Some(t) = &self.peeked {
            t.start
        } else {
            self.lex.position()
        }
    }

    fn next(&mut self) -> Result<Option<Token<'a>>, PdfError> {
        if let Some(t) = self.peeked.take() {
            return Ok(Some(t));
        }
        self.lex.next_token()
    }

    fn peek(&mut self) -> Result<Option<&Token<'a>>, PdfError> {
        if self.peeked.is_none() {
            self.peeked = self.lex.next_token()?;
        }
        Ok(self.peeked.as_ref())
    }

    /// Parse one [`Object`]. Returns `None` if the input is exhausted.
    ///
    /// Streams encountered with an indirect `/Length` (a `Reference`
    /// value rather than a direct integer) are rejected — call
    /// [`Self::parse_object_with_length_resolver`] from a context that
    /// can resolve the reference (typically the top-level
    /// [`crate::reader::document::DocumentReader::resolve`]).
    pub fn parse_object(&mut self) -> Result<Option<Object>, PdfError> {
        self.parse_object_with_length_resolver(&mut NoLengthResolver)
    }

    /// Variant of [`Self::parse_object`] that accepts a
    /// [`LengthResolver`] to resolve indirect `/Length` references on
    /// stream-object dictionaries per ISO 32000-1 §7.3.10 Example 3.
    pub fn parse_object_with_length_resolver(
        &mut self,
        resolver: &mut dyn LengthResolver,
    ) -> Result<Option<Object>, PdfError> {
        let Some(tok) = self.next()? else {
            return Ok(None);
        };
        Ok(Some(self.object_from_token(tok, resolver)?))
    }

    fn object_from_token(
        &mut self,
        tok: Token<'a>,
        resolver: &mut dyn LengthResolver,
    ) -> Result<Object, PdfError> {
        match tok.kind {
            TokenKind::Integer(n) => self.maybe_indirect_ref(n, tok.end),
            TokenKind::Real(f) => Ok(Object::Real(f)),
            TokenKind::Name(bytes) => Ok(Object::Name(String::from_utf8(bytes).map_err(|_| {
                PdfError::other(format!("PDF parser: non-UTF-8 name at byte {}", tok.start))
            })?)),
            TokenKind::LiteralString(bytes) => Ok(Object::LiteralString(bytes)),
            TokenKind::HexString(bytes) => Ok(Object::HexString(bytes)),
            TokenKind::ArrayStart => self.parse_array(resolver),
            TokenKind::DictStart => self.parse_dict_or_stream(tok.start, resolver),
            TokenKind::Keyword(kw) => match kw {
                b"true" => Ok(Object::Bool(true)),
                b"false" => Ok(Object::Bool(false)),
                b"null" => Ok(Object::Null),
                other => Err(PdfError::other(format!(
                    "PDF parser: unexpected keyword `{}` at byte {}",
                    String::from_utf8_lossy(other),
                    tok.start
                ))),
            },
            TokenKind::ArrayEnd => Err(PdfError::other(format!(
                "PDF parser: unexpected `]` at byte {}",
                tok.start
            ))),
            TokenKind::DictEnd => Err(PdfError::other(format!(
                "PDF parser: unexpected `>>` at byte {}",
                tok.start
            ))),
        }
    }

    /// An integer at the start of an object can be the front of an
    /// indirect reference (`<num> <gen> R`). Pull the next two tokens
    /// non-destructively and only consume them if they form `int int
    /// R`; otherwise fall back to a plain integer.
    fn maybe_indirect_ref(&mut self, n: i64, _start_end: usize) -> Result<Object, PdfError> {
        // We can't easily peek 2 ahead without nesting Option<Option<…>>.
        // Save the lexer position so we can roll back if the
        // lookahead doesn't match.
        let saved_pos = self.lex.position();
        let saved_peeked = self.peeked.clone();

        let t2 = self.next()?;
        let Some(t2) = t2 else {
            // EOF after the int — not a ref.
            return Ok(Object::Integer(n));
        };
        let TokenKind::Integer(gen) = t2.kind else {
            // Not int int — restore and emit Integer.
            self.peeked = Some(t2);
            // (saved positions don't need restoring — peeked covers
            // the next-token slot)
            let _ = saved_pos;
            let _ = saved_peeked;
            return Ok(Object::Integer(n));
        };
        let t3 = self.next()?;
        let Some(t3) = t3 else {
            // We've consumed gen — can't put two tokens back. Restore
            // via lexer rewind.
            self.lex.seek(saved_pos);
            self.peeked = saved_peeked;
            return Ok(Object::Integer(n));
        };
        let TokenKind::Keyword(b"R") = t3.kind else {
            // Not a ref — restore both consumed tokens by rewinding
            // the lexer to where we were before we touched it.
            self.lex.seek(saved_pos);
            self.peeked = saved_peeked;
            return Ok(Object::Integer(n));
        };
        if n < 1 || n > u32::MAX as i64 || gen < 0 || gen > u16::MAX as i64 {
            return Err(PdfError::other(format!(
                "PDF parser: indirect ref out of range `{n} {gen} R`"
            )));
        }
        Ok(Object::Reference(ObjectId {
            number: n as u32,
            generation: gen as u16,
        }))
    }

    fn parse_array(&mut self, resolver: &mut dyn LengthResolver) -> Result<Object, PdfError> {
        let mut items = Vec::new();
        loop {
            let tok = self.next()?.ok_or_else(|| {
                PdfError::other("PDF parser: unterminated array (EOF before `]`)")
            })?;
            if let TokenKind::ArrayEnd = tok.kind {
                return Ok(Object::Array(items));
            }
            items.push(self.object_from_token(tok, resolver)?);
        }
    }

    fn parse_dict_or_stream(
        &mut self,
        start: usize,
        resolver: &mut dyn LengthResolver,
    ) -> Result<Object, PdfError> {
        let mut dict = Dict::new();
        loop {
            let tok = self.next()?.ok_or_else(|| {
                PdfError::other(format!(
                    "PDF parser: unterminated dict starting at byte {start} (EOF before `>>`)"
                ))
            })?;
            if let TokenKind::DictEnd = tok.kind {
                break;
            }
            // Key must be a Name.
            let TokenKind::Name(key_bytes) = tok.kind else {
                return Err(PdfError::other(format!(
                    "PDF parser: dict key must be a Name at byte {} (got {:?})",
                    tok.start, tok.kind
                )));
            };
            let key = String::from_utf8(key_bytes).map_err(|_| {
                PdfError::other(format!(
                    "PDF parser: non-UTF-8 dict key at byte {}",
                    tok.start
                ))
            })?;
            // Value. Note: nested dict / array values inside a stream
            // dictionary are themselves direct objects (§7.3.8.1 — the
            // stream dict shall be a direct object). The resolver only
            // applies to the top-level /Length lookup, so nested calls
            // pass `None`.
            let val = self.parse_object()?.ok_or_else(|| {
                PdfError::other(format!(
                    "PDF parser: dict key `{key}` at byte {} has no value",
                    tok.start
                ))
            })?;
            dict.set(&key, val);
        }
        // Check whether this dict is a stream header — `stream` keyword
        // immediately follows (after optional whitespace).
        let after = self.peek()?.cloned();
        if let Some(t) = after {
            if let TokenKind::Keyword(b"stream") = t.kind {
                // Consume the `stream` keyword.
                let _ = self.next()?;
                // Per §7.3.8.1, the stream data starts immediately
                // after the EOL marker that follows the `stream`
                // keyword. The marker is CRLF or just LF.
                let raw = self.lex.input();
                let mut data_start = t.end;
                if data_start < raw.len() && raw[data_start] == b'\r' {
                    data_start += 1;
                }
                if data_start < raw.len() && raw[data_start] == b'\n' {
                    data_start += 1;
                }
                // /Length is required per §7.3.8.2 — use it to find
                // the body's end. ISO 32000-1 §7.3.10 Example 3
                // makes indirect /Length normative on stream objects
                // (`<< /Length 8 0 R >>` for one-pass writers). When a
                // resolver is supplied (the top-level
                // `DocumentReader::resolve` path), call it to fetch the
                // referenced integer; when no resolver is supplied
                // (the xref-stream parsing path, where the xref isn't
                // yet built) the indirect form is rejected.
                let length_obj = dict.entries().iter().find_map(|(k, v)| {
                    if k == "Length" {
                        Some(v.clone())
                    } else {
                        None
                    }
                });
                let len: usize = match length_obj {
                    Some(Object::Integer(n)) if n >= 0 => n as usize,
                    Some(Object::Reference(id)) => {
                        let resolved = resolver.resolve_length(id)?;
                        if resolved < 0 {
                            return Err(PdfError::other(format!(
                                "PDF parser: indirect /Length {id:?} resolved to \
                                 negative integer {resolved}"
                            )));
                        }
                        // Patch the dict so downstream consumers (e.g.
                        // `decode_stream`, encryption length tracking)
                        // see the resolved direct integer rather than
                        // the now-stale `Reference`.
                        dict.set("Length", Object::Integer(resolved));
                        resolved as usize
                    }
                    Some(other) => {
                        return Err(PdfError::other(format!(
                            "PDF parser: stream /Length must be a non-negative integer (got {other:?})"
                        )));
                    }
                    None => {
                        return Err(PdfError::other(
                            "PDF parser: stream object missing required /Length entry",
                        ));
                    }
                };
                let data_end = data_start.saturating_add(len).min(raw.len());
                let data = self.lex.slice(data_start, data_end).to_vec();
                // Skip the body + the EOL before `endstream` + the
                // `endstream` keyword itself.
                self.lex.seek(data_end);
                self.peeked = None;
                // Drop any whitespace + EOL between the data and
                // `endstream`.
                let endstream = self
                    .next()?
                    .ok_or_else(|| PdfError::other("PDF parser: stream missing `endstream`"))?;
                let TokenKind::Keyword(b"endstream") = endstream.kind else {
                    return Err(PdfError::other(format!(
                        "PDF parser: expected `endstream` after stream body at byte {} (got {:?})",
                        endstream.start, endstream.kind
                    )));
                };
                return Ok(Object::Stream(Stream::new(dict, data)));
            }
        }
        Ok(Object::Dict(dict))
    }

    /// Parse one indirect object (`<n> <gen> obj … endobj`). Returns
    /// `(ObjectId, Object)`. The caller is responsible for positioning
    /// the parser at the first byte of the indirect object header.
    ///
    /// Streams with an indirect `/Length` are rejected — use
    /// [`Self::parse_indirect_with_length_resolver`] from a context
    /// that has a built xref table.
    pub fn parse_indirect(&mut self) -> Result<(ObjectId, Object), PdfError> {
        self.parse_indirect_with_length_resolver(&mut NoLengthResolver)
    }

    /// Variant of [`Self::parse_indirect`] that accepts a
    /// [`LengthResolver`] to resolve indirect `/Length` references on
    /// stream-object dictionaries (ISO 32000-1 §7.3.10 Example 3).
    pub fn parse_indirect_with_length_resolver(
        &mut self,
        resolver: &mut dyn LengthResolver,
    ) -> Result<(ObjectId, Object), PdfError> {
        let n = self.expect_integer("indirect-object number")?;
        let gen = self.expect_integer("indirect-object generation")?;
        let obj_kw = self
            .next()?
            .ok_or_else(|| PdfError::other("PDF parser: unexpected EOF before `obj` keyword"))?;
        let TokenKind::Keyword(b"obj") = obj_kw.kind else {
            return Err(PdfError::other(format!(
                "PDF parser: expected `obj` keyword at byte {} (got {:?})",
                obj_kw.start, obj_kw.kind
            )));
        };
        let body = self
            .parse_object_with_length_resolver(resolver)?
            .ok_or_else(|| PdfError::other("PDF parser: indirect object missing body"))?;
        let endobj = self
            .next()?
            .ok_or_else(|| PdfError::other("PDF parser: indirect object missing `endobj`"))?;
        let TokenKind::Keyword(b"endobj") = endobj.kind else {
            return Err(PdfError::other(format!(
                "PDF parser: expected `endobj` at byte {} (got {:?})",
                endobj.start, endobj.kind
            )));
        };
        if n < 1 || n > u32::MAX as i64 || !(0..=u16::MAX as i64).contains(&gen) {
            return Err(PdfError::other(format!(
                "PDF parser: indirect-object id `{n} {gen}` out of range"
            )));
        }
        Ok((
            ObjectId {
                number: n as u32,
                generation: gen as u16,
            },
            body,
        ))
    }

    fn expect_integer(&mut self, what: &str) -> Result<i64, PdfError> {
        let tok = self
            .next()?
            .ok_or_else(|| PdfError::other(format!("PDF parser: expected {what}, got EOF")))?;
        match tok.kind {
            TokenKind::Integer(n) => Ok(n),
            other => Err(PdfError::other(format!(
                "PDF parser: expected {what} (integer), got {other:?} at byte {}",
                tok.start
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_one(input: &[u8]) -> Object {
        Parser::new(input)
            .parse_object()
            .unwrap()
            .expect("parsed object")
    }

    #[test]
    fn primitives_parse_to_objects() {
        assert!(matches!(parse_one(b"true"), Object::Bool(true)));
        assert!(matches!(parse_one(b"false"), Object::Bool(false)));
        assert!(matches!(parse_one(b"null"), Object::Null));
        assert!(matches!(parse_one(b"42"), Object::Integer(42)));
        match parse_one(b"2.5") {
            Object::Real(f) => assert!((f - 2.5).abs() < 1e-9),
            other => panic!("expected real, got {other:?}"),
        }
        match parse_one(b"(hello)") {
            Object::LiteralString(b) => assert_eq!(b, b"hello".to_vec()),
            other => panic!("expected literal string, got {other:?}"),
        }
        match parse_one(b"<48656C6C6F>") {
            Object::HexString(b) => assert_eq!(b, b"Hello".to_vec()),
            other => panic!("expected hex string, got {other:?}"),
        }
        match parse_one(b"/Pages") {
            Object::Name(s) => assert_eq!(s, "Pages"),
            other => panic!("expected name, got {other:?}"),
        }
    }

    #[test]
    fn array_parses_recursively() {
        let arr = parse_one(b"[1 2.5 (a) /b [3 4]]");
        let Object::Array(items) = arr else {
            panic!("expected array")
        };
        assert_eq!(items.len(), 5);
        assert!(matches!(items[0], Object::Integer(1)));
        assert!(matches!(items[1], Object::Real(_)));
        assert!(matches!(items[2], Object::LiteralString(_)));
        assert!(matches!(items[3], Object::Name(_)));
        assert!(matches!(items[4], Object::Array(_)));
    }

    #[test]
    fn dict_parses_with_named_keys() {
        let d = parse_one(b"<< /Type /Page /Count 3 /Kids [1 2 3] >>");
        let Object::Dict(d) = d else {
            panic!("expected dict")
        };
        let entries = d.entries();
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].0, "Type");
        assert_eq!(entries[1].0, "Count");
        assert_eq!(entries[2].0, "Kids");
    }

    #[test]
    fn indirect_reference_recognised() {
        let r = parse_one(b"5 0 R");
        let Object::Reference(id) = r else {
            panic!("expected ref")
        };
        assert_eq!(id.number, 5);
        assert_eq!(id.generation, 0);
    }

    #[test]
    fn integer_followed_by_non_ref_does_not_become_ref() {
        // `5 0` (without R) → array of two integers.
        let arr = parse_one(b"[5 0]");
        let Object::Array(items) = arr else {
            panic!("expected array")
        };
        assert_eq!(items.len(), 2);
        assert!(matches!(items[0], Object::Integer(5)));
        assert!(matches!(items[1], Object::Integer(0)));
    }

    #[test]
    fn indirect_object_round_trip() {
        let input = b"3 0 obj\n<< /Type /Page >>\nendobj\n";
        let mut p = Parser::new(input);
        let (id, body) = p.parse_indirect().unwrap();
        assert_eq!(id.number, 3);
        assert!(matches!(body, Object::Dict(_)));
    }

    #[test]
    fn stream_object_extracts_body_per_length() {
        let input = b"4 0 obj\n<< /Length 5 >>\nstream\nABCDE\nendstream\nendobj\n";
        let mut p = Parser::new(input);
        let (_, body) = p.parse_indirect().unwrap();
        let Object::Stream(s) = body else {
            panic!("expected stream")
        };
        assert_eq!(s.data, b"ABCDE".to_vec());
    }

    #[test]
    fn stream_with_crlf_eol_marker() {
        // After `stream`, the EOL can be CRLF (or LF). The body
        // begins immediately after the marker.
        let mut input = Vec::new();
        input.extend_from_slice(b"4 0 obj\n<< /Length 3 >>\nstream\r\nXYZ\nendstream\nendobj\n");
        let mut p = Parser::new(&input);
        let (_, body) = p.parse_indirect().unwrap();
        let Object::Stream(s) = body else {
            panic!("expected stream")
        };
        assert_eq!(s.data, b"XYZ".to_vec());
    }

    #[test]
    fn stream_indirect_length_without_resolver_is_rejected() {
        // /Length 7 0 R — an indirect reference; the resolver-less
        // entry point (used by the xref-stream parser, before any
        // xref table exists) must still reject this so a malformed
        // xref-stream object doesn't silently read past EOF.
        let input = b"4 0 obj\n<< /Length 7 0 R >>\nstream\nXYZ\nendstream\nendobj\n";
        let mut p = Parser::new(input);
        let err = p.parse_indirect().unwrap_err();
        assert!(format!("{err}").contains("indirect reference"));
    }

    #[test]
    fn stream_indirect_length_with_resolver_resolves() {
        // Round-91: ISO 32000-1 §7.3.10 Example 3 — stream's /Length
        // is an indirect reference; with a resolver, the parser
        // fetches the target integer and slices the body accordingly.
        let input = b"4 0 obj\n<< /Length 7 0 R >>\nstream\nXYZ\nendstream\nendobj\n";
        let mut p = Parser::new(input);
        let mut resolver = |id: ObjectId| -> Result<i64, PdfError> {
            assert_eq!(id, ObjectId::new(7));
            Ok(3)
        };
        let (id, body) = p
            .parse_indirect_with_length_resolver(&mut resolver)
            .unwrap();
        assert_eq!(id, ObjectId::new(4));
        let Object::Stream(s) = body else {
            panic!("expected stream, got {body:?}")
        };
        assert_eq!(s.data, b"XYZ".to_vec());
        // The dict should now carry the resolved direct integer so
        // downstream consumers (decoders, encryption) don't see the
        // stale Reference.
        let length = s
            .dict
            .entries()
            .iter()
            .find(|(k, _)| k == "Length")
            .map(|(_, v)| v.clone());
        assert!(matches!(length, Some(Object::Integer(3))));
    }

    #[test]
    fn stream_indirect_length_negative_resolution_errors() {
        // A resolver that returns a negative integer must be flagged
        // (a real PDF should never do this, but we don't trust input).
        let input = b"4 0 obj\n<< /Length 7 0 R >>\nstream\nXYZ\nendstream\nendobj\n";
        let mut p = Parser::new(input);
        let mut resolver = |_id: ObjectId| -> Result<i64, PdfError> { Ok(-1) };
        let err = p
            .parse_indirect_with_length_resolver(&mut resolver)
            .unwrap_err();
        assert!(format!("{err}").contains("negative"));
    }

    #[test]
    fn stream_indirect_length_resolver_error_propagates() {
        // The resolver may legitimately fail (missing xref entry,
        // wrong-typed target object). Errors flow up the parser.
        let input = b"4 0 obj\n<< /Length 7 0 R >>\nstream\nXYZ\nendstream\nendobj\n";
        let mut p = Parser::new(input);
        let mut resolver =
            |_id: ObjectId| -> Result<i64, PdfError> { Err(PdfError::other("test: not found")) };
        let err = p
            .parse_indirect_with_length_resolver(&mut resolver)
            .unwrap_err();
        assert!(format!("{err}").contains("not found"));
    }

    #[test]
    fn stream_missing_length_is_rejected() {
        let input = b"4 0 obj\n<< /Type /XObject >>\nstream\nXYZ\nendstream\nendobj\n";
        let mut p = Parser::new(input);
        let err = p.parse_indirect().unwrap_err();
        assert!(format!("{err}").contains("/Length"));
    }

    #[test]
    fn nested_dict_in_array() {
        // The bracket-state tracking has to handle dicts nested inside
        // arrays and vice versa.
        let arr = parse_one(b"[<< /A 1 >> << /B 2 >>]");
        let Object::Array(items) = arr else {
            panic!("expected array")
        };
        assert_eq!(items.len(), 2);
        assert!(matches!(items[0], Object::Dict(_)));
        assert!(matches!(items[1], Object::Dict(_)));
    }

    #[test]
    fn dict_with_indirect_reference_value() {
        let d = parse_one(b"<< /Pages 2 0 R >>");
        let Object::Dict(d) = d else {
            panic!("expected dict")
        };
        let entries = d.entries();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].0, "Pages");
        assert!(matches!(entries[0].1, Object::Reference(_)));
    }
}