laminapdf-syntax 0.7.0

PDF syntax in Rust: the object model, lexer and parser, stream filters, decryption, and resource limits for untrusted input.
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
//! The PDF object model (ISO 32000-1 §7.3) and its exact serialization rules.

use std::fmt;

/// A reference to an indirect object: `num gen R`.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ObjRef {
    /// Object number.
    pub num: u32,
    /// Generation number.
    pub gen_nr: u16,
}

impl ObjRef {
    /// Creates a reference to object `num` with generation `gen_nr`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn new(num: u32, gen_nr: u16) -> Self {
        Self { num, gen_nr }
    }
}

impl fmt::Display for ObjRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {} R", self.num, self.gen_nr)
    }
}

/// How a string was (or should be) written: `(literal)` or `<hex>`.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StringFormat {
    /// `( … )` with backslash escapes.
    #[default]
    Literal,
    /// `< … >` uppercase hexadecimal.
    Hex,
}

/// A PDF string: raw bytes plus the syntax it was written in.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PdfString {
    /// The decoded string bytes.
    pub bytes: Vec<u8>,
    /// Literal or hex form, remembered so that re-serialization round-trips.
    pub format: StringFormat,
}

impl PdfString {
    /// A literal string `( … )`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn literal(bytes: impl Into<Vec<u8>>) -> Self {
        Self {
            bytes: bytes.into(),
            format: StringFormat::Literal,
        }
    }

    /// A hex string `< … >`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn hex(bytes: impl Into<Vec<u8>>) -> Self {
        Self {
            bytes: bytes.into(),
            format: StringFormat::Hex,
        }
    }
}

/// A PDF name, stored as its decoded bytes without the leading `/`.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
pub struct Name(pub Vec<u8>);

impl Name {
    /// Creates a name from its decoded bytes (no leading `/`).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
        Self(bytes.into())
    }

    /// The decoded bytes of the name.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

impl From<&str> for Name {
    fn from(s: &str) -> Self {
        Self(s.as_bytes().to_vec())
    }
}

/// An insertion-ordered dictionary. Lookups are linear, which is faster than hashing
/// for the handful of keys real PDF dictionaries carry.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Dict {
    entries: Vec<(Name, Object)>,
}

impl Dict {
    /// An empty dictionary.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Looks up `key` (without the leading `/`).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&Object> {
        self.entries.iter().find(|(k, _)| k.0 == key.as_bytes()).map(|(_, v)| v)
    }

    /// Mutable lookup of `key`.
    ///
    /// Added in 0.2.0.
    pub fn get_mut(&mut self, key: &str) -> Option<&mut Object> {
        self.entries
            .iter_mut()
            .find(|(k, _)| k.0 == key.as_bytes())
            .map(|(_, v)| v)
    }

    /// True when `key` is present.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn contains_key(&self, key: &str) -> bool {
        self.get(key).is_some()
    }

    /// Sets `key` to `value`. An existing key keeps its position; a new key is appended.
    ///
    /// Added in 0.2.0.
    pub fn insert(&mut self, key: impl Into<Name>, value: impl Into<Object>) {
        let key = key.into();
        let value = value.into();
        match self.entries.iter_mut().find(|(k, _)| *k == key) {
            Some(slot) => slot.1 = value,
            None => self.entries.push((key, value)),
        }
    }

    /// Removes `key`, returning its value.
    ///
    /// Added in 0.2.0.
    pub fn remove(&mut self, key: &str) -> Option<Object> {
        let pos = self.entries.iter().position(|(k, _)| k.0 == key.as_bytes())?;
        Some(self.entries.remove(pos).1)
    }

    /// Entries in insertion order.
    ///
    /// Added in 0.2.0.
    pub fn iter(&self) -> impl Iterator<Item = (&Name, &Object)> {
        self.entries.iter().map(|(k, v)| (k, v))
    }

    /// Entries in insertion order, with mutable values.
    ///
    /// Added in 0.2.0.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Name, &mut Object)> {
        self.entries.iter_mut().map(|(k, v)| (&*k, v))
    }

    /// Number of entries.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// True when the dictionary has no entries.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// `key` as a name, if present and a name.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn get_name(&self, key: &str) -> Option<&[u8]> {
        match self.get(key) {
            Some(Object::Name(n)) => Some(n.as_bytes()),
            _ => None,
        }
    }

    /// `key` as an integer, if present and an integer.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn get_int(&self, key: &str) -> Option<i64> {
        self.get(key).and_then(Object::as_int)
    }
}

impl<K: Into<Name>, V: Into<Object>> FromIterator<(K, V)> for Dict {
    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
        let mut dict = Self::new();
        for (k, v) in iter {
            dict.insert(k, v);
        }
        dict
    }
}

/// A stream: its dictionary plus the raw (still encoded) bytes between `stream` and `endstream`.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Stream {
    /// The stream dictionary. `/Length` is managed by the writer.
    pub dict: Dict,
    /// Raw stream bytes as stored in the file (encoded by `/Filter`, if any).
    pub data: Vec<u8>,
}

impl Stream {
    /// A stream whose bytes are stored as-is.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn new(dict: Dict, data: impl Into<Vec<u8>>) -> Self {
        Self {
            dict,
            data: data.into(),
        }
    }

    /// A Flate-compressed stream: compresses `decoded` and sets `/Filter /FlateDecode`
    /// (keeping the key's position if the caller already placed it).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn flate(mut dict: Dict, decoded: &[u8]) -> Self {
        dict.insert("Filter", Name::from("FlateDecode"));
        Self {
            dict,
            data: crate::filters::flate_encode(decoded),
        }
    }
}

/// A PDF object.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum Object {
    /// `null`.
    #[default]
    Null,
    /// `true` / `false`.
    Bool(bool),
    /// An integer.
    Integer(i64),
    /// A real number.
    Real(f64),
    /// A literal or hex string.
    String(PdfString),
    /// A name.
    Name(Name),
    /// An array.
    Array(Vec<Object>),
    /// A dictionary.
    Dict(Dict),
    /// A stream (only valid as the direct value of an indirect object).
    Stream(Stream),
    /// An indirect reference.
    Ref(ObjRef),
}

impl Object {
    /// `v` as a number: an integer when it is integral and below 10¹⁵ in magnitude (`612`,
    /// not `612.0`), else a real. PDF has no NaN or infinity, so a non-finite `v` is 0.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn number(v: f64) -> Self {
        if !v.is_finite() {
            return Self::Integer(0);
        }
        #[expect(
            clippy::cast_possible_truncation,
            reason = "only integral values well inside i64 get here"
        )]
        if v.fract() == 0.0 && v.abs() < 1e15 {
            Self::Integer(v as i64)
        } else {
            Self::Real(v)
        }
    }

    /// `v` rounded to `decimals` decimal places (at most 15; more count as 15), then written
    /// as [`Object::number`] writes it: `rounded(v, 0)` is the nearest integer.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn rounded(v: f64, decimals: u8) -> Self {
        // Past 10¹⁵ every f64 is already whole, and scaling it could overflow to infinity.
        if v.abs() >= 1e15 {
            return Self::number(v);
        }
        let scale = 10f64.powi(i32::from(decimals.min(15)));
        Self::number((v * scale).round() / scale)
    }

    /// A name object.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn name(s: &str) -> Self {
        Self::Name(Name::from(s))
    }

    /// The integer value, if this is an integer.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn as_int(&self) -> Option<i64> {
        match self {
            Self::Integer(i) => Some(*i),
            _ => None,
        }
    }

    /// The numeric value of an integer or real.
    ///
    /// Added in 0.2.0.
    #[must_use]
    #[expect(clippy::cast_precision_loss, reason = "PDF numbers are approximate by definition")]
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Self::Integer(i) => Some(*i as f64),
            Self::Real(r) => Some(*r),
            _ => None,
        }
    }

    /// The dictionary, if this is a dictionary (or a stream's dictionary).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn as_dict(&self) -> Option<&Dict> {
        match self {
            Self::Dict(d) => Some(d),
            Self::Stream(s) => Some(&s.dict),
            _ => None,
        }
    }

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

    /// The reference, if this is a reference.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn as_ref(&self) -> Option<ObjRef> {
        match self {
            Self::Ref(r) => Some(*r),
            _ => None,
        }
    }

    /// Serializes this object to a fresh buffer using the crate's exact formatting rules.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut out = Vec::new();
        self.write_to(&mut out);
        out
    }

    /// Appends the serialization of this object to `out`.
    ///
    /// Streams are written as `<<dict>>\nstream\n…\nendstream` with `/Length` set to the
    /// data length (kept in place if present, appended last otherwise).
    ///
    /// Added in 0.2.0.
    pub fn write_to(&self, out: &mut Vec<u8>) {
        match self {
            Self::Null => out.extend_from_slice(b"null"),
            Self::Bool(true) => out.extend_from_slice(b"true"),
            Self::Bool(false) => out.extend_from_slice(b"false"),
            Self::Integer(i) => out.extend_from_slice(i.to_string().as_bytes()),
            Self::Real(r) => out.extend_from_slice(format_real(*r).as_bytes()),
            Self::String(s) => write_string(s, out),
            Self::Name(n) => write_name(n.as_bytes(), out),
            Self::Array(items) => {
                out.push(b'[');
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        out.push(b' ');
                    }
                    item.write_to(out);
                }
                out.push(b']');
            }
            Self::Dict(d) => write_dict(d, out),
            Self::Stream(s) => {
                let mut dict = s.dict.clone();
                let len = i64::try_from(s.data.len()).unwrap_or(i64::MAX);
                dict.insert("Length", len);
                write_dict(&dict, out);
                out.extend_from_slice(b"\nstream\n");
                out.extend_from_slice(&s.data);
                out.extend_from_slice(b"\nendstream");
            }
            Self::Ref(r) => out.extend_from_slice(r.to_string().as_bytes()),
        }
    }
}

macro_rules! impl_from {
    ($($t:ty => $variant:ident),* $(,)?) => {
        $(impl From<$t> for Object {
            fn from(v: $t) -> Self { Self::$variant(v.into()) }
        })*
    };
}

impl_from! {
    bool => Bool, i64 => Integer, i32 => Integer, u32 => Integer, f64 => Real,
    PdfString => String, Name => Name, Vec<Object> => Array, Dict => Dict,
    Stream => Stream, ObjRef => Ref,
}

/// Formats a real: never an exponent, at most 6 fractional digits, trailing zeros and a
/// trailing `.` trimmed, `-0` becomes `0`. Non-finite values (which PDF cannot express)
/// are written as `0`.
///
/// Added in 0.2.0.
#[must_use]
pub fn format_real(value: f64) -> String {
    if !value.is_finite() {
        return "0".to_owned();
    }
    let mut s = format!("{value:.6}");
    if s.contains('.') {
        let trimmed = s.trim_end_matches('0').trim_end_matches('.').len();
        s.truncate(trimmed);
    }
    if s == "-0" {
        s.clear();
        s.push('0');
    }
    s
}

/// Bytes that must be `#xx`-escaped inside a name besides those outside `!`..`~`.
fn name_needs_escape(b: u8) -> bool {
    !(b'!'..=b'~').contains(&b) || b"#()<>[]{}/%".contains(&b)
}

const HEX_UPPER: &[u8; 16] = b"0123456789ABCDEF";

fn push_hex(b: u8, out: &mut Vec<u8>) {
    let digit = |nibble: u8| HEX_UPPER.get(usize::from(nibble)).copied().unwrap_or(b'0');
    out.push(digit(b >> 4));
    out.push(digit(b & 0x0F));
}

pub(crate) fn write_name(bytes: &[u8], out: &mut Vec<u8>) {
    out.push(b'/');
    for &b in bytes {
        if name_needs_escape(b) {
            out.push(b'#');
            push_hex(b, out);
        } else {
            out.push(b);
        }
    }
}

pub(crate) fn write_literal_string(bytes: &[u8], out: &mut Vec<u8>) {
    out.push(b'(');
    for &b in bytes {
        match b {
            b'\\' | b'(' | b')' => {
                out.push(b'\\');
                out.push(b);
            }
            b'\r' => out.extend_from_slice(b"\\r"),
            _ => out.push(b),
        }
    }
    out.push(b')');
}

fn write_string(s: &PdfString, out: &mut Vec<u8>) {
    match s.format {
        StringFormat::Literal => write_literal_string(&s.bytes, out),
        StringFormat::Hex => {
            out.push(b'<');
            for &b in &s.bytes {
                push_hex(b, out);
            }
            out.push(b'>');
        }
    }
}

fn write_dict(d: &Dict, out: &mut Vec<u8>) {
    out.extend_from_slice(b"<<");
    for (k, v) in d.iter() {
        out.push(b' ');
        write_name(k.as_bytes(), out);
        out.push(b' ');
        v.write_to(out);
    }
    out.extend_from_slice(b" >>");
}

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

    fn ser(o: &Object) -> String {
        String::from_utf8_lossy(&o.to_bytes()).into_owned()
    }

    #[test]
    fn number_writes_integral_values_as_integers() {
        assert_eq!(
            (Object::number(612.0), Object::number(0.5), Object::number(-3.0)),
            (Object::Integer(612), Object::Real(0.5), Object::Integer(-3))
        );
    }

    #[test]
    fn number_writes_a_non_finite_value_as_zero() {
        assert_eq!(
            [f64::NAN, f64::INFINITY].map(Object::number),
            [Object::Integer(0), Object::Integer(0)]
        );
    }

    #[test]
    fn rounded_leaves_a_huge_value_as_it_is() {
        assert_eq!(Object::rounded(1e300, 15), Object::Real(1e300));
    }

    #[test]
    fn rounded_to_no_decimals_is_the_nearest_integer() {
        assert_eq!(
            (Object::rounded(95.5, 0), Object::rounded(-292.6, 0)),
            (Object::Integer(96), Object::Integer(-293))
        );
    }

    #[test]
    fn rounded_rounds_then_writes_a_whole_result_as_an_integer() {
        assert_eq!(
            (
                Object::rounded(-12.004, 2),
                Object::rounded(-12.345, 2),
                Object::rounded(600.098, 3)
            ),
            (Object::Integer(-12), Object::Real(-12.35), Object::Real(600.098))
        );
    }

    #[test]
    fn format_real_trims_trailing_zeros() {
        assert_eq!(format_real(0.5), "0.5");
    }

    #[test]
    fn format_real_drops_trailing_dot_for_whole_numbers() {
        assert_eq!(format_real(1.0), "1");
    }

    #[test]
    fn format_real_keeps_sign_of_negative_fraction() {
        assert_eq!(format_real(-0.25), "-0.25");
    }

    #[test]
    fn format_real_turns_negative_zero_into_zero() {
        assert_eq!(format_real(-0.0), "0");
    }

    #[test]
    fn format_real_turns_tiny_negative_rounding_to_zero_into_zero() {
        assert_eq!(format_real(-0.000_000_1), "0");
    }

    #[test]
    fn format_real_never_uses_an_exponent() {
        assert_eq!(format_real(1e20), "100000000000000000000");
    }

    #[test]
    fn format_real_rounds_exact_tie_half_to_even() {
        // 2^-7 = 0.0078125 is exactly representable: a true tie at 6 decimals.
        assert_eq!(format_real(0.007_812_5), "0.007812");
    }

    #[test]
    fn format_real_rounds_to_six_digits() {
        assert_eq!(format_real(1.0 / 3.0), "0.333333");
    }

    #[test]
    fn name_escapes_delimiters_space_and_hash() {
        assert_eq!(
            ser(&Object::Name(Name::new(&b"a b#(c)/%"[..]))),
            "/a#20b#23#28c#29#2F#25"
        );
    }

    #[test]
    fn name_escapes_non_ascii_with_uppercase_hex() {
        assert_eq!(ser(&Object::Name(Name::new(vec![0xE9, b'x']))), "/#E9x");
    }

    #[test]
    fn literal_string_escapes_parens_backslash_and_cr() {
        assert_eq!(
            ser(&PdfString::literal(&b"a(b)c\\d\re\n"[..]).into()),
            "(a\\(b\\)c\\\\d\\re\n)"
        );
    }

    #[test]
    fn hex_string_is_uppercase() {
        assert_eq!(ser(&PdfString::hex(vec![0xAB, 0x01]).into()), "<AB01>");
    }

    #[test]
    fn dict_uses_single_spaces() {
        let d: Dict = [
            ("K", Object::from(1)),
            ("K2", Object::Array(vec![Object::Null, true.into()])),
        ]
        .into_iter()
        .collect();
        assert_eq!(ser(&d.into()), "<< /K 1 /K2 [null true] >>");
    }

    #[test]
    fn empty_dict_serializes_as_double_brackets_with_space() {
        assert_eq!(ser(&Dict::new().into()), "<< >>");
    }

    #[test]
    fn stream_appends_length_last_when_absent() {
        let s = Stream::new([("Type", Object::name("X"))].into_iter().collect(), b"abc".to_vec());
        assert_eq!(ser(&s.into()), "<< /Type /X /Length 3 >>\nstream\nabc\nendstream");
    }

    #[test]
    fn stream_keeps_caller_length_position() {
        let d: Dict = [("Length", Object::from(99)), ("Filter", Object::name("F"))]
            .into_iter()
            .collect();
        assert_eq!(
            ser(&Stream::new(d, b"ab".to_vec()).into()),
            "<< /Length 2 /Filter /F >>\nstream\nab\nendstream"
        );
    }

    #[test]
    fn dict_insert_keeps_existing_key_position() {
        let mut d: Dict = [("A", 1), ("B", 2)].into_iter().collect();
        d.insert("A", 3);
        assert_eq!(ser(&d.into()), "<< /A 3 /B 2 >>");
    }
}