flpdf 0.3.0

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
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
//! Part 1 emitter — file header + linearization parameter dictionary.
//!
//! ISO 32000-1 Annex F §F.2 specifies that the first body section of a
//! linearized PDF contains:
//!
//! 1. The file header (`%PDF-x.y` + binary marker).
//! 2. Object 1 — the linearization parameter dictionary with fixed-width
//!    (10-digit) decimal placeholders for all numeric values that are not
//!    yet known at layout time.
//!
//! # Placeholder discipline + trailing pad
//!
//! Every numeric value in the dict is initially represented as a **10-digit,
//! zero-padded** ASCII decimal string (e.g. `0000000000`).  This fixed-width
//! encoding means that a back-patcher can overwrite values
//! in-place without shifting any downstream byte offsets.
//!
//! Immediately after `endobj\n`, the emitter reserves
//! [`PARAM_DICT_TRAILING_PAD`] bytes of ASCII space (`b' '`).  Once the
//! back-patcher knows the final numeric values, it rewrites the dict with
//! **variable-width** decimals (matching qpdf's byte format)
//! and grows this trailing pad to absorb the bytes saved, so the total Part 1
//! byte length stays constant.  Without the reserve, shrinking the dict would
//! shift every downstream offset and force a second convergence loop.
//!
//! [`Part1Placeholders`] records the exact byte ranges of each placeholder
//! (pre-back-patch, 10-wide; post-back-patch the back-patcher updates them
//! to point at the rewritten variable-width value bytes).  `dict_writable_region`
//! covers the splice target: from `<<` through the end of the trailing pad.
//!
//! # Scope
//!
//! This module emits **header + object 1 + trailing pad** only.  The Part 1
//! xref subsection and trailer (required by the linearized format) are
//! written by the full orchestrator.

use std::ops::Range;

use super::plan::LinearizationPlan;
use super::renumber::RenumberMap;

// ---------------------------------------------------------------------------
// Placeholder width and trailing pad reserve
// ---------------------------------------------------------------------------

/// Initial number of ASCII digits used for every numeric placeholder.
///
/// After the back-patcher compacts the dict to variable-width decimals, each
/// field's actual byte count is `value.to_string().len()` (1..=10 for any
/// value ≤ 10^10 − 1).
pub const PLACEHOLDER_WIDTH: usize = 10;

/// The placeholder bytes (10 ASCII `b'0'` characters).
const PLACEHOLDER: &[u8] = b"0000000000";

/// Number of pad bytes reserved after `endobj\n` so the dict can be rewritten
/// with variable-width decimal values without shifting Part 1's total length.
///
/// Two requirements drive this value:
///
/// 1. **Compaction reserve.** It must be at least `7 × (PLACEHOLDER_WIDTH − 1)`
///    = 63, the tight upper bound on bytes saved by compaction: each of the 7
///    numeric fields shrinks by at most `PLACEHOLDER_WIDTH − 1` characters
///    (= 10 − 1 for value `0` rendered as the single byte `b"0"`).
/// 2. **First-page xref alignment.** qpdf lands the first-page `xref` keyword
///    at a fixed offset (observed: 216 for the 15-byte `%PDF-1.3` header and a
///    single-digit param-dict object number, i.e. `{n} 0 obj\n` = 8 bytes plus
///    the 118-byte max-width dict body plus `\nendobj\n` = 8 bytes). The
///    first-page xref is emitted immediately after this trailing pad, so the
///    pad width fixes that offset. Padding to 66 reaches qpdf's 216 target:
///    `15 + 8 + (118 + 8 + 66) + 1` = the position of the `xref` keyword after
///    the leading `\n`. 66 also exceeds the 63-byte compaction reserve, so the
///    variable-width back-patch never overflows the region.
pub const PARAM_DICT_TRAILING_PAD: usize = 66;

// ---------------------------------------------------------------------------
// Part1Placeholders
// ---------------------------------------------------------------------------

/// Byte ranges of each numeric value field inside the [`Part1Bytes`] buffer.
///
/// **Pre-back-patch**: every range is exactly [`PLACEHOLDER_WIDTH`] bytes of
/// ASCII `b'0'`.  This is the [`all_valid`](Self::all_valid) invariant.
///
/// **Post-back-patch** (after [`back_patch_param_dict`](crate::linearization::back_patch_param_dict)):
/// the back-patcher splices the variable-width compact dict into place and
/// updates these ranges to point at the rewritten value bytes — each range
/// becomes 1..=10 bytes wide (`value.to_string().len()`).  The
/// [`all_valid`](Self::all_valid) check no longer holds and is intentionally
/// not maintained post-splice.
///
/// ## Which fields need back-patching
///
/// | Field       | Value source                                  |
/// |-------------|-----------------------------------------------|
/// | `l`         | `/L` — total file length (back-patch)         |
/// | `h_offset`  | `/H[0]` — hint stream byte offset (back-patch)|
/// | `h_length`  | `/H[1]` — hint stream byte length (back-patch)|
/// | `o`         | `/O` — first-page object new number (back-patch) |
/// | `e`         | `/E` — end of first-page section (back-patch) |
/// | `t`         | `/T` — offset of last xref (back-patch)       |
/// | `n`         | `/N` — page count (back-patch)                |
///
/// All seven fields are placeholders for uniform treatment, even though `/O`
/// and `/N` could in principle be computed immediately.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Part1Placeholders {
    /// `/L` — total file length in bytes.
    pub l: Range<usize>,
    /// `/H[0]` — hint stream byte offset.
    pub h_offset: Range<usize>,
    /// `/H[1]` — hint stream byte length.
    pub h_length: Range<usize>,
    /// `/O` — first-page object new number (typically 2).
    pub o: Range<usize>,
    /// `/E` — end of first-page section offset.
    pub e: Range<usize>,
    /// `/T` — offset of last cross-reference section.
    pub t: Range<usize>,
    /// `/N` — number of pages.
    pub n: Range<usize>,
}

impl Part1Placeholders {
    /// Return all seven ranges as a slice in dict-key order: L, `H[0]`, `H[1]`,
    /// O, E, T, N.
    ///
    /// Useful for checking disjoint and ordering invariants.
    pub fn as_slice(&self) -> [Range<usize>; 7] {
        [
            self.l.clone(),
            self.h_offset.clone(),
            self.h_length.clone(),
            self.o.clone(),
            self.e.clone(),
            self.t.clone(),
            self.n.clone(),
        ]
    }

    /// Returns `true` iff this is the **pre-back-patch** layout:
    /// all seven placeholder ranges are pairwise disjoint and each has a
    /// width of exactly [`PLACEHOLDER_WIDTH`].
    ///
    /// Post-back-patch the ranges shrink to their variable-width value bytes,
    /// so this returns `false` after a successful
    /// [`back_patch_param_dict`](crate::linearization::back_patch_param_dict)
    /// call — the writer asserts this invariant only on the freshly built
    /// `Part1Bytes`.
    pub fn all_valid(&self) -> bool {
        let ranges = self.as_slice();
        for r in &ranges {
            if r.len() != PLACEHOLDER_WIDTH {
                return false;
            }
        }
        // Check pairwise disjoint: sort by start and verify no overlap.
        let mut starts: Vec<(usize, usize)> = ranges.iter().map(|r| (r.start, r.end)).collect();
        starts.sort_unstable_by_key(|&(s, _)| s);
        for window in starts.windows(2) {
            if window[0].1 > window[1].0 {
                return false;
            }
        }
        true
    }
}

// ---------------------------------------------------------------------------
// Part1Bytes
// ---------------------------------------------------------------------------

/// The serialized bytes for Part 1 (header + linearization parameter dict +
/// trailing pad) together with the placeholder positions needed for
/// back-patching and the writable region used by the variable-width compaction.
///
/// Construct via [`Part1Bytes::build`].
#[derive(Debug, Clone)]
pub struct Part1Bytes {
    /// Raw bytes: file header, then `N 0 obj\n<< ... >>\nendobj\n`, then
    /// [`PARAM_DICT_TRAILING_PAD`] bytes of ASCII space pad.
    pub bytes: Vec<u8>,
    /// Byte positions of every numeric value field in `bytes`.  Pre-back-patch
    /// these are 10 ASCII `0` characters; post-back-patch they point at the
    /// rewritten variable-width decimal bytes.
    pub placeholders: Part1Placeholders,
    /// Byte offset within `bytes` of the `N 0 obj` token (i.e. the start of
    /// the param-dict object's body, after the file header).  The linearized
    /// writer needs this for the Part 1 xref subsection's offset entry.
    ///
    /// Exposed instead of having callers hardcode the header length so that
    /// changes to the header format (PDF version bump, binary marker change)
    /// do not silently break downstream xref entries.
    pub obj1_offset: usize,
    /// Byte range spanned by the rewritable param-dict region:
    /// `<<` through the end of the trailing pad (inclusive of `\nendobj\n`).
    ///
    /// The variable-width back-patcher overwrites this entire region with a
    /// compact dict body + `\nendobj\n` + spaces to fill, keeping the total
    /// region length (and thus all downstream offsets) constant.
    pub dict_writable_region: Range<usize>,
}

impl Part1Bytes {
    /// Serialize Part 1 from `plan` and `renumber`.
    ///
    /// The output is deterministic: the same inputs always produce the same
    /// bytes.
    ///
    /// # Header
    ///
    /// `%PDF-1.7\n` followed by a binary marker line
    /// `%<0xBF 0xF7 0xA2 0xFE>\n` (shared `crate::writer::QPDF_BINARY_MARKER`).
    ///
    /// # Object 1 format
    ///
    /// ```text
    /// 1 0 obj
    /// << /Linearized 1 /L XXXXXXXXXX /H [ XXXXXXXXXX XXXXXXXXXX ] /O XXXXXXXXXX /E XXXXXXXXXX /N XXXXXXXXXX /T XXXXXXXXXX >>
    /// endobj
    /// ```
    ///
    /// where each `XXXXXXXXXX` is a 10-digit zero placeholder.
    ///
    /// `pdf_version` is the effective PDF version string to emit in the header
    /// (e.g. `"1.3"`, `"1.7"`).  Callers should compute this via
    /// [`crate::writer::effective_pdf_version`] before calling `build`.
    pub fn build(_plan: &LinearizationPlan, renumber: &RenumberMap, pdf_version: &str) -> Self {
        // The param-dict slot must still hold its sentinel — overwriting it
        // would cause the writer to emit a duplicate object definition on the
        // same number, corrupting the xref. The slot number itself is now
        // dynamic (qpdf places the param dict after the Pages tree / Info
        // promotion), so the writer reads it from the renumber map.
        debug_assert!(
            renumber.param_dict_slot_is_reserved(),
            "Part1Bytes::build: RenumberMap allocated a plan object on top \
             of the param-dict slot ({param_slot}) — slot must stay reserved",
            param_slot = renumber.param_dict_ref().number
        );

        let param_dict_obj_number = renumber.param_dict_ref().number;

        let mut bytes: Vec<u8> = Vec::new();

        // ------------------------------------------------------------------
        // File header
        // ------------------------------------------------------------------
        // Emit the caller-supplied version (computed via effective_pdf_version).
        bytes.extend_from_slice(format!("%PDF-{pdf_version}\n").as_bytes());
        // Binary marker: four bytes >= 128 signals a binary file. Share the
        // marker with the plain rewrite path so linearized output stays
        // byte-identical to qpdf (which uses the same marker for both paths).
        bytes.extend_from_slice(crate::writer::QPDF_BINARY_MARKER);

        // ------------------------------------------------------------------
        // Object {param_dict_obj_number}: linearization parameter dictionary
        // ------------------------------------------------------------------
        // Capture the absolute offset of the object header token so callers
        // do not need to compute the file header length out of band.
        let param_dict_offset = bytes.len();
        bytes.extend_from_slice(format!("{param_dict_obj_number} 0 obj\n").as_bytes());
        // Capture the start of the dict body — this is where the back-patcher's
        // splice begins (everything from `<<` through the trailing pad is
        // rewritable).
        let dict_writable_start = bytes.len();
        bytes.extend_from_slice(b"<< /Linearized 1 /L ");

        // /L placeholder
        let l_start = bytes.len();
        bytes.extend_from_slice(PLACEHOLDER);
        let l_end = bytes.len();

        bytes.extend_from_slice(b" /H [ ");

        // /H[0] placeholder — hint stream offset
        let h_offset_start = bytes.len();
        bytes.extend_from_slice(PLACEHOLDER);
        let h_offset_end = bytes.len();

        bytes.extend_from_slice(b" ");

        // /H[1] placeholder — hint stream length
        let h_length_start = bytes.len();
        bytes.extend_from_slice(PLACEHOLDER);
        let h_length_end = bytes.len();

        bytes.extend_from_slice(b" ] /O ");

        // /O placeholder — first-page object new number
        let o_start = bytes.len();
        bytes.extend_from_slice(PLACEHOLDER);
        let o_end = bytes.len();

        bytes.extend_from_slice(b" /E ");

        // /E placeholder — end of first-page section
        let e_start = bytes.len();
        bytes.extend_from_slice(PLACEHOLDER);
        let e_end = bytes.len();

        // qpdf emits the linearization param dict with /N preceding /T
        // (the spec only requires /Linearized to come first; the remaining
        // keys' order is implementation-defined). Match qpdf so writers
        // doing byte comparisons across tools agree.
        bytes.extend_from_slice(b" /N ");

        // /N placeholder — page count
        let n_start = bytes.len();
        bytes.extend_from_slice(PLACEHOLDER);
        let n_end = bytes.len();

        bytes.extend_from_slice(b" /T ");

        // /T placeholder — offset of last xref
        let t_start = bytes.len();
        bytes.extend_from_slice(PLACEHOLDER);
        let t_end = bytes.len();

        bytes.extend_from_slice(b" >>\n");
        bytes.extend_from_slice(b"endobj\n");

        // ------------------------------------------------------------------
        // Trailing pad: reserve space for variable-width dict compaction.
        //
        // The back-patcher rewrites the dict with variable-width decimals
        // (qpdf byte format) and grows this pad to absorb the saved bytes
        // — total Part 1 byte length stays constant so downstream offsets
        // never shift.
        //
        // qpdf lands the first-page xref at a FIXED offset regardless of the
        // param-dict object number's digit count: it absorbs the extra
        // `{n} 0 obj\n` digits into this pad so the param-dict OBJECT's total
        // span stays constant. `PARAM_DICT_TRAILING_PAD` is tuned for a
        // single-digit object number; when the param dict is numbered with more
        // digits (e.g. obj 72 in a file with >70 second-half objects), shorten
        // the pad by `digits − 1` so the xref still lands where qpdf places it.
        // ------------------------------------------------------------------
        let obj_number_digits = param_dict_obj_number.to_string().len();
        let trailing_pad = PARAM_DICT_TRAILING_PAD - (obj_number_digits - 1);
        let dict_writable_end = bytes.len() + trailing_pad;
        bytes.resize(dict_writable_end, b' ');

        let placeholders = Part1Placeholders {
            l: l_start..l_end,
            h_offset: h_offset_start..h_offset_end,
            h_length: h_length_start..h_length_end,
            o: o_start..o_end,
            e: e_start..e_end,
            t: t_start..t_end,
            n: n_start..n_end,
        };

        Self {
            bytes,
            placeholders,
            obj1_offset: param_dict_offset,
            dict_writable_region: dict_writable_start..dict_writable_end,
        }
    }

    /// Length of the serialized Part 1 in bytes.
    pub fn byte_length(&self) -> usize {
        self.bytes.len()
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::linearization::plan::{LinearizationPlan, PageHintEntry};
    use crate::ObjectRef;

    // -----------------------------------------------------------------------
    // Fixture helpers
    // -----------------------------------------------------------------------

    fn minimal_plan() -> LinearizationPlan {
        // Single page, no shared objects.
        LinearizationPlan {
            part2_objects: vec![ObjectRef::new(3, 0), ObjectRef::new(2, 0)],
            part4_rest: vec![ObjectRef::new(1, 0)],
            total_object_count: 3,
            root_ref: Some(ObjectRef::new(1, 0)),
            page_hints: vec![PageHintEntry::placeholder(ObjectRef::new(3, 0))],
            ..Default::default()
        }
    }

    fn build_part1() -> Part1Bytes {
        let plan = minimal_plan();
        let renumber = RenumberMap::from_plan(&plan);
        Part1Bytes::build(&plan, &renumber, "1.4")
    }

    // -----------------------------------------------------------------------
    // 1. byte_length is deterministic
    // -----------------------------------------------------------------------
    #[test]
    fn byte_length_is_deterministic() {
        let p1 = build_part1();
        let p2 = build_part1();
        assert_eq!(
            p1.bytes, p2.bytes,
            "Part 1 must be bytewise identical on repeated calls"
        );
        assert_eq!(p1.byte_length(), p2.byte_length());
    }

    // -----------------------------------------------------------------------
    // 2. Output starts with %PDF- header
    // -----------------------------------------------------------------------
    #[test]
    fn output_starts_with_pdf_header() {
        let p1 = build_part1();
        assert!(
            p1.bytes.starts_with(b"%PDF-"),
            "Part 1 must start with %PDF-"
        );
    }

    // -----------------------------------------------------------------------
    // 3. Output contains "1 0 obj"
    // -----------------------------------------------------------------------
    #[test]
    fn output_contains_object_1_header() {
        let p1 = build_part1();
        assert!(
            p1.bytes.windows(b"1 0 obj".len()).any(|w| w == b"1 0 obj"),
            "Part 1 must contain '1 0 obj'"
        );
    }

    // -----------------------------------------------------------------------
    // 4. Output contains /Linearized 1
    // -----------------------------------------------------------------------
    #[test]
    fn output_contains_linearized_key() {
        let p1 = build_part1();
        let needle = b"/Linearized 1";
        assert!(
            p1.bytes.windows(needle.len()).any(|w| w == needle),
            "Part 1 must contain '/Linearized 1'"
        );
    }

    // -----------------------------------------------------------------------
    // 5. Each placeholder is exactly PLACEHOLDER_WIDTH ASCII '0' bytes
    // -----------------------------------------------------------------------
    #[test]
    fn placeholders_are_ten_zero_digits() {
        let p1 = build_part1();
        let ph = &p1.placeholders;
        for range in ph.as_slice() {
            assert_eq!(
                range.len(),
                PLACEHOLDER_WIDTH,
                "placeholder range {:?} must have width {}",
                range,
                PLACEHOLDER_WIDTH
            );
            assert!(
                p1.bytes[range.clone()].iter().all(|&b| b == b'0'),
                "placeholder bytes at {:?} must all be b'0'",
                range
            );
        }
    }

    // -----------------------------------------------------------------------
    // 6. Placeholder byte ranges match the bytes in the buffer
    // -----------------------------------------------------------------------
    #[test]
    fn placeholder_ranges_point_to_zero_bytes() {
        let p1 = build_part1();
        let ph = &p1.placeholders;

        // Spot-check each named field.
        assert_eq!(&p1.bytes[ph.l.clone()], PLACEHOLDER);
        assert_eq!(&p1.bytes[ph.h_offset.clone()], PLACEHOLDER);
        assert_eq!(&p1.bytes[ph.h_length.clone()], PLACEHOLDER);
        assert_eq!(&p1.bytes[ph.o.clone()], PLACEHOLDER);
        assert_eq!(&p1.bytes[ph.e.clone()], PLACEHOLDER);
        assert_eq!(&p1.bytes[ph.t.clone()], PLACEHOLDER);
        assert_eq!(&p1.bytes[ph.n.clone()], PLACEHOLDER);
    }

    // -----------------------------------------------------------------------
    // 7. All placeholder ranges are disjoint
    // -----------------------------------------------------------------------
    #[test]
    fn placeholder_ranges_are_disjoint() {
        let p1 = build_part1();
        assert!(
            p1.placeholders.all_valid(),
            "Part1Placeholders must be valid (all width={}, all disjoint)",
            PLACEHOLDER_WIDTH
        );
    }

    // -----------------------------------------------------------------------
    // 8. Placeholder ranges appear in ascending order in the buffer
    // -----------------------------------------------------------------------
    #[test]
    fn placeholders_are_in_ascending_order() {
        let p1 = build_part1();
        let ph = &p1.placeholders;
        // qpdf-aligned emission order: /L /H[0] /H[1] /O /E /N /T
        let starts = [
            ph.l.start,
            ph.h_offset.start,
            ph.h_length.start,
            ph.o.start,
            ph.e.start,
            ph.n.start,
            ph.t.start,
        ];
        let names = ["l", "h_offset", "h_length", "o", "e", "n", "t"];
        for i in 1..starts.len() {
            assert!(
                starts[i] > starts[i - 1],
                "placeholder '{}' (start {}) must come after '{}' (start {})",
                names[i],
                starts[i],
                names[i - 1],
                starts[i - 1]
            );
        }
    }

    // -----------------------------------------------------------------------
    // 9. byte_length is positive and consistent with buffer
    // -----------------------------------------------------------------------
    #[test]
    fn byte_length_equals_buffer_len() {
        let p1 = build_part1();
        assert_eq!(p1.byte_length(), p1.bytes.len());
        assert!(p1.byte_length() > 0);
    }

    // -----------------------------------------------------------------------
    // 10. Binary marker line follows the header
    // -----------------------------------------------------------------------
    #[test]
    fn binary_marker_present() {
        let p1 = build_part1();
        // The binary marker bytes follow immediately after the header line.
        // build_part1() uses version "1.4", so the header is "%PDF-1.4\n".
        let expected_marker: &[u8] = crate::writer::QPDF_BINARY_MARKER;
        let header_len = b"%PDF-1.4\n".len();
        assert!(
            p1.bytes.len() > header_len + expected_marker.len(),
            "buffer too short to contain binary marker"
        );
        assert_eq!(
            &p1.bytes[header_len..header_len + expected_marker.len()],
            expected_marker,
            "binary marker must follow the PDF header"
        );
    }

    // -----------------------------------------------------------------------
    // 11. Placeholders are followed/preceded by valid PDF separators
    //     (no two placeholders adjacent without whitespace)
    // -----------------------------------------------------------------------
    #[test]
    fn placeholder_boundaries_have_separators() {
        let p1 = build_part1();
        let ph = &p1.placeholders;
        // After /H[0] there must be a space before /H[1].
        let byte_between = p1.bytes[ph.h_offset.end];
        assert_eq!(
            byte_between, b' ',
            "there must be a space between /H[0] and /H[1] placeholders"
        );
    }

    // -----------------------------------------------------------------------
    // 12. obj1_offset matches the actual position of "1 0 obj" in bytes
    // -----------------------------------------------------------------------
    #[test]
    fn obj1_offset_matches_token_position() {
        let p1 = build_part1();
        let token = b"1 0 obj";
        let found = p1
            .bytes
            .windows(token.len())
            .position(|w| w == token)
            .expect("Part 1 must contain the `1 0 obj` token");
        assert_eq!(
            p1.obj1_offset, found,
            "Part1Bytes::obj1_offset must point at the start of `1 0 obj`"
        );
        // Also verify the slice at that offset starts with the token
        // (catches the off-by-one case where obj1_offset points one byte
        // past the start).
        assert!(
            p1.bytes[p1.obj1_offset..].starts_with(token),
            "bytes[obj1_offset..] must start with `1 0 obj`"
        );
    }
}