pdf2 0.2.0-alpha.2

A PDF 2.0 (ISO 32000-2) reader, writer and text extractor.
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
//! PDF base objects and their serialization.
//!
//! [`Object`] models every base type of ISO 32000-2 ยง7.3: null, booleans,
//! integers, reals, strings, names, arrays, dictionaries and indirect
//! references. The serializers here are the writer side of the round-trip that
//! M2 requires: the tokenizer added in Q2 parses exactly the bytes produced
//! below, so `parse โ†’ write` and `write โ†’ parse` both preserve bytes.

use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use core::fmt;

/// An indirect object number together with its generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ObjectId {
    /// Object number, the first value of an `n g R` reference.
    pub number: u32,
    /// Generation number, the second value of an `n g R` reference.
    pub generation: u16,
}

impl ObjectId {
    /// Build an object id.
    pub const fn new(number: u32, generation: u16) -> Self {
        Self { number, generation }
    }
}

/// The syntax a [`PdfString`] is written with.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StringFormat {
    /// `(...)`, with escapes for parentheses, backslash and binary bytes.
    Literal,
    /// `<...>`, one hex pair per byte, uppercase.
    Hex,
}

/// A PDF string: the decoded bytes plus the syntax they were written with.
///
/// Both forms are kept so that a parsed file writes back byte-for-byte, but
/// equality compares the decoded bytes only โ€” `(Hello)` and `<48656C6C6F>` are
/// the same string object.
#[derive(Debug, Clone)]
pub struct PdfString {
    format: StringFormat,
    bytes: Vec<u8>,
}

impl PdfString {
    /// A string written in literal `(...)` form.
    pub fn literal(bytes: impl Into<Vec<u8>>) -> Self {
        Self {
            format: StringFormat::Literal,
            bytes: bytes.into(),
        }
    }

    /// A string written in hex `<...>` form.
    pub fn hex(bytes: impl Into<Vec<u8>>) -> Self {
        Self {
            format: StringFormat::Hex,
            bytes: bytes.into(),
        }
    }

    /// The syntax this string is written with.
    pub fn format(&self) -> StringFormat {
        self.format
    }

    /// The decoded bytes, without any escaping.
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Consume the string and return its decoded bytes.
    pub fn into_bytes(self) -> Vec<u8> {
        self.bytes
    }

    /// Replace the decoded bytes, keeping the serialization format.
    ///
    /// The decryption path in Q9 uses this to swap in plain text.
    pub fn set_bytes(&mut self, bytes: impl Into<Vec<u8>>) {
        self.bytes = bytes.into();
    }
}

impl PartialEq for PdfString {
    fn eq(&self, other: &Self) -> bool {
        self.bytes == other.bytes
    }
}

impl Eq for PdfString {}

impl From<&str> for PdfString {
    fn from(value: &str) -> Self {
        Self::literal(value)
    }
}

impl From<Vec<u8>> for PdfString {
    fn from(value: Vec<u8>) -> Self {
        Self::literal(value)
    }
}

/// A PDF dictionary: ordered name โ†’ object entries.
///
/// Insertion order is preserved and reproduced by the serializer, so a parsed
/// dictionary writes back as the bytes it came from. PDF dictionaries are not
/// sorted on disk and re-ordering them would break byte-exact round-trips.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Dictionary {
    entries: Vec<(Vec<u8>, Object)>,
}

impl Dictionary {
    /// An empty dictionary.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert `name` โ†’ `value`, replacing an existing entry in place.
    ///
    /// An entry that is already present keeps its position and its old value is
    /// returned.
    pub fn insert(&mut self, name: impl Into<Vec<u8>>, value: Object) -> Option<Object> {
        let name = name.into();
        if let Some((_, slot)) = self.entries.iter_mut().find(|(key, _)| *key == name) {
            return Some(core::mem::replace(slot, value));
        }
        self.entries.push((name, value));
        None
    }

    /// The value stored under `name`, if any.
    pub fn get(&self, name: &[u8]) -> Option<&Object> {
        self.entries
            .iter()
            .find(|(key, _)| key == name)
            .map(|(_, value)| value)
    }

    /// The value stored under `name`, for in-place updates.
    pub fn get_mut(&mut self, name: &[u8]) -> Option<&mut Object> {
        self.entries
            .iter_mut()
            .find(|(key, _)| key == name)
            .map(|(_, value)| value)
    }

    /// Whether `name` has an entry.
    pub fn contains_key(&self, name: &[u8]) -> bool {
        self.get(name).is_some()
    }

    /// Number of entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the dictionary has no entries.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// The entries in insertion order.
    pub fn entries(&self) -> &[(Vec<u8>, Object)] {
        &self.entries
    }

    /// Iterate over `(name, value)` pairs in insertion order.
    pub fn iter(&self) -> impl Iterator<Item = (&[u8], &Object)> + '_ {
        self.entries
            .iter()
            .map(|(name, value)| (name.as_slice(), value))
    }

    fn write_to(&self, out: &mut Vec<u8>) {
        out.extend_from_slice(b"<<");
        for (name, value) in &self.entries {
            out.push(b' ');
            write_name(name, out);
            out.push(b' ');
            value.write_to(out);
        }
        if !self.entries.is_empty() {
            out.push(b' ');
        }
        out.extend_from_slice(b">>");
    }
}

impl From<Vec<(Vec<u8>, Object)>> for Dictionary {
    fn from(entries: Vec<(Vec<u8>, Object)>) -> Self {
        Self { entries }
    }
}

/// A stream object: a dictionary plus the raw bytes between `stream` and
/// `endstream`.
///
/// The data is kept exactly as it appears in the file โ€” still encoded, still
/// encrypted if the document is. Decoding is the filter layer's job (Q4);
/// decryption is Q9's, and both use [`Stream::set_data`] so that `/Length`
/// never drifts away from the bytes.
#[derive(Debug, Clone, PartialEq)]
pub struct Stream {
    dict: Dictionary,
    data: Vec<u8>,
}

impl Stream {
    /// A stream from a dictionary and its raw data.
    ///
    /// `/Length` is not rewritten here: the dictionary is used exactly as
    /// given, which is what a parsed stream needs for a byte-exact write-back.
    pub fn new(dict: Dictionary, data: impl Into<Vec<u8>>) -> Self {
        Self {
            dict,
            data: data.into(),
        }
    }

    /// The stream dictionary.
    pub fn dict(&self) -> &Dictionary {
        &self.dict
    }

    /// The stream dictionary, for in-place edits.
    pub fn dict_mut(&mut self) -> &mut Dictionary {
        &mut self.dict
    }

    /// The raw stream bytes, as stored in the file.
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Consume the stream and return its raw bytes.
    pub fn into_data(self) -> Vec<u8> {
        self.data
    }

    /// Replace the raw data and keep `/Length` in step with it.
    ///
    /// Used after decoding or decrypting, when the bytes change but the
    /// dictionary must stay consistent with what would be written out.
    pub fn set_data(&mut self, data: impl Into<Vec<u8>>) {
        let data = data.into();
        self.dict
            .insert(b"Length", Object::Integer(data.len() as i64));
        self.data = data;
    }
}

/// A PDF object: any value that can appear inside a document.
///
/// `Real` holds an `f64`, so the enum is `PartialEq` but not `Eq`.
#[derive(Debug, Clone, PartialEq)]
pub enum Object {
    /// `null`.
    Null,
    /// `true` or `false`.
    Boolean(bool),
    /// An integer, written without a decimal point.
    Integer(i64),
    /// A real number, always written with a decimal point.
    Real(f64),
    /// A string, literal or hex.
    String(PdfString),
    /// A name, stored decoded: no leading `/`, `#xx` escapes resolved.
    Name(Vec<u8>),
    /// An array.
    Array(Vec<Object>),
    /// A dictionary.
    Dictionary(Dictionary),
    /// A stream, only valid as the value of an indirect object.
    Stream(Stream),
    /// An indirect reference, `n g R`.
    Reference(ObjectId),
}

impl Object {
    /// A name object from decoded bytes.
    pub fn name(name: impl Into<Vec<u8>>) -> Self {
        Self::Name(name.into())
    }

    /// A literal string object.
    pub fn string(bytes: impl Into<Vec<u8>>) -> Self {
        Self::String(PdfString::literal(bytes))
    }

    /// A hex string object.
    pub fn hex_string(bytes: impl Into<Vec<u8>>) -> Self {
        Self::String(PdfString::hex(bytes))
    }

    /// An array object.
    pub fn array(items: impl IntoIterator<Item = Object>) -> Self {
        Self::Array(items.into_iter().collect())
    }

    /// A reference object.
    pub const fn reference(id: ObjectId) -> Self {
        Self::Reference(id)
    }

    /// Whether this is `null`.
    pub fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }

    /// The integer value, if this is an integer.
    pub fn as_integer(&self) -> Option<i64> {
        match self {
            Self::Integer(value) => Some(*value),
            _ => None,
        }
    }

    /// The numeric value as a real, accepting an integer too.
    pub fn as_real(&self) -> Option<f64> {
        match self {
            Self::Integer(value) => Some(*value as f64),
            Self::Real(value) => Some(*value),
            _ => None,
        }
    }

    /// The name bytes, if this is a name.
    pub fn as_name(&self) -> Option<&[u8]> {
        match self {
            Self::Name(name) => Some(name),
            _ => None,
        }
    }

    /// The array items, if this is an array.
    pub fn as_array(&self) -> Option<&[Object]> {
        match self {
            Self::Array(items) => Some(items),
            _ => None,
        }
    }

    /// The dictionary, if this is one.
    pub fn as_dict(&self) -> Option<&Dictionary> {
        match self {
            Self::Dictionary(dict) => Some(dict),
            _ => None,
        }
    }

    /// The stream, if this is one.
    pub fn as_stream(&self) -> Option<&Stream> {
        match self {
            Self::Stream(stream) => Some(stream),
            _ => None,
        }
    }

    /// The string, if this is one.
    pub fn as_string(&self) -> Option<&PdfString> {
        match self {
            Self::String(string) => Some(string),
            _ => None,
        }
    }

    /// The reference target, if this is a reference.
    pub fn as_reference(&self) -> Option<ObjectId> {
        match self {
            Self::Reference(id) => Some(*id),
            _ => None,
        }
    }

    /// Append the serialized form of this object to `out`.
    ///
    /// The output is the canonical text form used by this crate: names keep
    /// only regular characters, strings keep the format they were parsed with,
    /// reals always carry a decimal point, and containers separate their
    /// elements with single spaces.
    pub fn write_to(&self, out: &mut Vec<u8>) {
        match self {
            Self::Null => out.extend_from_slice(b"null"),
            Self::Boolean(value) => out.extend_from_slice(if *value { b"true" } else { b"false" }),
            Self::Integer(value) => out.extend_from_slice(value.to_string().as_bytes()),
            Self::Real(value) => out.extend_from_slice(real_to_string(*value).as_bytes()),
            Self::String(string) => match string.format() {
                StringFormat::Literal => write_literal(string.as_bytes(), out),
                StringFormat::Hex => write_hex(string.as_bytes(), out),
            },
            Self::Name(name) => write_name(name, out),
            Self::Array(items) => {
                out.push(b'[');
                for (index, item) in items.iter().enumerate() {
                    if index > 0 {
                        out.push(b' ');
                    }
                    item.write_to(out);
                }
                out.push(b']');
            }
            Self::Dictionary(dict) => dict.write_to(out),
            Self::Stream(stream) => {
                stream.dict.write_to(out);
                out.extend_from_slice(b"\nstream\n");
                out.extend_from_slice(stream.data());
                out.extend_from_slice(b"\nendstream");
            }
            Self::Reference(id) => {
                out.extend_from_slice(id.number.to_string().as_bytes());
                out.push(b' ');
                out.extend_from_slice(id.generation.to_string().as_bytes());
                out.extend_from_slice(b" R");
            }
        }
    }

    /// Serialize this object into a fresh buffer.
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut out = Vec::new();
        self.write_to(&mut out);
        out
    }
}

/// Whether `byte` may appear in a name without a `#xx` escape.
///
/// Regular characters are printable ASCII minus delimiters and `#`, which is
/// the escape introducer itself (ISO 32000-2 ยง7.3.5).
fn is_regular(byte: u8) -> bool {
    matches!(byte, 0x21..=0x7e)
        && !matches!(
            byte,
            b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%' | b'#'
        )
}

fn write_name(name: &[u8], out: &mut Vec<u8>) {
    out.push(b'/');
    for &byte in name {
        if is_regular(byte) {
            out.push(byte);
        } else {
            out.push(b'#');
            out.extend_from_slice(format!("{byte:02X}").as_bytes());
        }
    }
}

fn write_literal(bytes: &[u8], out: &mut Vec<u8>) {
    out.push(b'(');
    for &byte in bytes {
        match byte {
            b'(' | b')' | b'\\' => {
                out.push(b'\\');
                out.push(byte);
            }
            b'\n' => out.extend_from_slice(b"\\n"),
            b'\r' => out.extend_from_slice(b"\\r"),
            b'\t' => out.extend_from_slice(b"\\t"),
            0x08 => out.extend_from_slice(b"\\b"),
            0x0c => out.extend_from_slice(b"\\f"),
            0x20..=0x7e => out.push(byte),
            _ => {
                out.push(b'\\');
                out.extend_from_slice(format!("{byte:03o}").as_bytes());
            }
        }
    }
    out.push(b')');
}

fn write_hex(bytes: &[u8], out: &mut Vec<u8>) {
    out.push(b'<');
    for &byte in bytes {
        out.extend_from_slice(format!("{byte:02X}").as_bytes());
    }
    out.push(b'>');
}

/// Format a real in plain decimal notation with no exponent.
///
/// Rust's `Display` for `f64` prints the shortest text that parses back to the
/// same value and never uses exponential notation, which is exactly what PDF
/// needs; the only adjustment is appending `.0` so the result stays a real and
/// not an integer. Non-finite values have no PDF representation and are written
/// as `0`.
fn real_to_string(value: f64) -> String {
    if !value.is_finite() {
        return "0".to_owned();
    }
    let mut text = format!("{value}");
    if !text.contains('.') {
        text.push_str(".0");
    }
    text
}

impl fmt::Display for Object {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&String::from_utf8_lossy(&self.to_bytes()))
    }
}

#[cfg(test)]
mod tests {
    use super::{Dictionary, Object, ObjectId, PdfString, Stream};

    fn dict(entries: &[(&str, Object)]) -> Dictionary {
        let mut dict = Dictionary::new();
        for (name, value) in entries {
            dict.insert(name.as_bytes(), value.clone());
        }
        dict
    }

    #[test]
    fn scalars_serialize_canonically() {
        assert_eq!(Object::Null.to_bytes(), b"null");
        assert_eq!(Object::Boolean(true).to_bytes(), b"true");
        assert_eq!(Object::Boolean(false).to_bytes(), b"false");
        assert_eq!(Object::Integer(-17).to_bytes(), b"-17");
        assert_eq!(Object::Real(2.375).to_bytes(), b"2.375");
        assert_eq!(Object::Real(-0.5).to_bytes(), b"-0.5");
    }

    #[test]
    fn reals_always_keep_a_decimal_point() {
        assert_eq!(Object::Real(1.0).to_bytes(), b"1.0");
        assert_eq!(Object::Real(123.0).to_bytes(), b"123.0");
        assert_eq!(Object::Real(-2.0).to_bytes(), b"-2.0");
        assert_eq!(Object::Real(1e-7).to_bytes(), b"0.0000001");
        assert_eq!(Object::Real(f64::NAN).to_bytes(), b"0");
    }

    #[test]
    fn names_escape_irregular_bytes() {
        assert_eq!(Object::name("Type").to_bytes(), b"/Type");
        assert_eq!(Object::name("A Name").to_bytes(), b"/A#20Name");
        assert_eq!(Object::name("#hash").to_bytes(), b"/#23hash");
        assert_eq!(
            Object::name("slash/percent%").to_bytes(),
            b"/slash#2Fpercent#25"
        );
        assert_eq!(Object::name("Name\u{e9}").to_bytes(), b"/Name#C3#A9");
        assert_eq!(Object::name("").to_bytes(), b"/");
    }

    #[test]
    fn literal_strings_escape_delimiters_and_binary() {
        assert_eq!(Object::string("Hello").to_bytes(), b"(Hello)");
        assert_eq!(Object::string("").to_bytes(), b"()");
        assert_eq!(Object::string("a(b)c").to_bytes(), b"(a\\(b\\)c)");
        assert_eq!(Object::string("back\\slash").to_bytes(), b"(back\\\\slash)");
        assert_eq!(Object::string("line\nbreak").to_bytes(), b"(line\\nbreak)");
        assert_eq!(Object::string("tab\there").to_bytes(), b"(tab\\there)");
        assert_eq!(
            Object::string(vec![0x00, 0x07, 0xff]).to_bytes(),
            b"(\\000\\007\\377)"
        );
    }

    #[test]
    fn hex_strings_are_uppercase() {
        assert_eq!(Object::hex_string("Hello").to_bytes(), b"<48656C6C6F>");
        assert_eq!(Object::hex_string(Vec::new()).to_bytes(), b"<>");
        assert_eq!(Object::hex_string(vec![0xde, 0xad]).to_bytes(), b"<DEAD>");
    }

    #[test]
    fn string_equality_ignores_the_serialization_format() {
        assert_eq!(PdfString::literal("Hi"), PdfString::hex("Hi"));
        assert_ne!(PdfString::literal("Hi"), PdfString::literal("ho"));
    }

    #[test]
    fn containers_separate_elements_with_one_space() {
        assert_eq!(Object::array(Vec::new()).to_bytes(), b"[]");
        assert_eq!(
            Object::array([Object::Integer(1), Object::Integer(2), Object::Integer(3)]).to_bytes(),
            b"[1 2 3]"
        );
        assert_eq!(
            Object::array([
                Object::array([Object::Integer(1)]),
                Object::array([Object::Integer(2), Object::Integer(3)])
            ])
            .to_bytes(),
            b"[[1] [2 3]]"
        );
        assert_eq!(
            Object::Dictionary(dict(&[("Type", Object::name("Page"))])).to_bytes(),
            b"<< /Type /Page >>"
        );
        assert_eq!(Object::Dictionary(Dictionary::new()).to_bytes(), b"<<>>");
        assert_eq!(
            Object::Dictionary(dict(&[
                ("Type", Object::name("Page")),
                ("Count", Object::Integer(3)),
                (
                    "Kids",
                    Object::array([Object::reference(ObjectId::new(1, 0))])
                ),
            ]))
            .to_bytes(),
            b"<< /Type /Page /Count 3 /Kids [1 0 R] >>"
        );
    }

    #[test]
    fn nested_dictionaries_serialize_in_insertion_order() {
        let inner = dict(&[("Inner", Object::Null)]);
        let outer = dict(&[
            ("Zed", Object::Integer(1)),
            ("Outer", Object::Dictionary(inner)),
        ]);
        assert_eq!(
            Object::Dictionary(outer).to_bytes(),
            b"<< /Zed 1 /Outer << /Inner null >> >>"
        );
    }

    #[test]
    fn dictionary_insert_replaces_in_place() {
        let mut dict = dict(&[("A", Object::Integer(1)), ("B", Object::Integer(2))]);
        assert_eq!(
            dict.insert("A", Object::Integer(9)),
            Some(Object::Integer(1))
        );
        assert_eq!(dict.len(), 2);
        assert_eq!(Object::Dictionary(dict).to_bytes(), b"<< /A 9 /B 2 >>");
    }

    #[test]
    fn streams_serialize_the_dictionary_then_the_raw_data() {
        let mut dict = Dictionary::new();
        dict.insert("Length", Object::Integer(5));
        dict.insert("Filter", Object::name("FlateDecode"));
        let stream = Stream::new(dict, b"hello");
        assert_eq!(
            Object::Stream(stream).to_bytes(),
            b"<< /Length 5 /Filter /FlateDecode >>\nstream\nhello\nendstream"
        );
    }

    #[test]
    fn set_data_keeps_length_in_step() {
        let mut dict = Dictionary::new();
        dict.insert("Length", Object::Integer(2));
        let mut stream = Stream::new(dict, b"ab");
        stream.set_data(b"abcdef");
        assert_eq!(stream.dict().get(b"Length"), Some(&Object::Integer(6)));
        assert_eq!(stream.data(), b"abcdef");
        assert_eq!(
            Object::Stream(stream).to_bytes(),
            b"<< /Length 6 >>\nstream\nabcdef\nendstream"
        );
    }

    #[test]
    fn references_serialize_as_three_tokens() {
        assert_eq!(
            Object::Reference(ObjectId::new(12, 0)).to_bytes(),
            b"12 0 R"
        );
        assert_eq!(ObjectId::new(7, 65535).generation, 65535);
    }
}