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
//! qpdf correspondence: shared PDF token serialization helpers used by canonical handle writers.
/// Return whether a parsed real literal is safe to emit verbatim.
pub(crate) fn real_literal_is_safe(literal: &[u8], value: f64) -> bool {
if literal.is_empty()
|| !literal
.iter()
.all(|byte| matches!(*byte, b'0'..=b'9' | b'.' | b'+' | b'-'))
{
return false;
}
// cov:ignore-start: the preceding PDF-number byte grammar rejects every
// non-UTF-8 byte before this defensive conversion boundary.
let Ok(text) = std::str::from_utf8(literal) else {
return false;
};
// cov:ignore-end
text.parse::<f64>()
.map(|parsed| parsed.to_bits() == value.to_bits())
.unwrap_or(false)
}
/// Escape decoded PDF name bytes into a single PDF name token.
///
/// `QPDFTokenizer` uses a raw NUL byte as a sentinel for a recoverable stray
/// `#` (`libqpdf/QPDFTokenizer.cc:441-465`, "Use null to encode a bad #");
/// `QPDF_Name::normalizeName` restores that sentinel to a bare `#` when
/// serializing (`libqpdf/QPDF_Name.cc:35-37`). A genuine `#00` escape is
/// never decoded to a raw NUL by the tokenizer in the first place -- it is
/// kept as the literal three-byte text `#00` (`QPDFTokenizer.cc:471-475`) --
/// so a raw NUL byte reaching this function can only be that sentinel.
pub(crate) fn write_name_escaped(out: &mut Vec<u8>, raw: &[u8]) {
const HEX: &[u8; 16] = b"0123456789abcdef";
for &byte in raw {
if byte == 0 {
out.push(b'#');
continue;
}
let needs_escape = !(0x21..=0x7e).contains(&byte)
|| matches!(
byte,
b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%' | b'#'
);
if needs_escape {
out.push(b'#');
out.push(HEX[(byte >> 4) as usize]);
out.push(HEX[(byte & 0x0f) as usize]);
} else {
out.push(byte);
}
}
}
/// Return whether a PDF string must use hex syntax.
pub(crate) fn use_hex_string(value: &[u8]) -> bool {
let mut non_ascii = 0usize;
for byte in value {
if *byte > 126 {
non_ascii += 1;
} else if *byte >= 32 {
continue;
} else if *byte >= 24 {
non_ascii += 1;
} else if !matches!(*byte, b'\n' | b'\r' | b'\t' | 0x08 | 0x0c) {
return true;
}
}
5 * non_ascii > value.len()
}
fn is_iso_latin1_printable(byte: u8) -> bool {
(32..=126).contains(&byte) || byte >= 160
}
/// Write a PDF literal string with qpdf-compatible escapes.
pub(crate) fn write_literal_string(out: &mut Vec<u8>, value: &[u8]) {
out.push(b'(');
for &byte in value {
match byte {
b'\\' | b'(' | b')' => {
out.push(b'\\');
out.push(byte);
}
b'\n' => out.extend_from_slice(br"\n"),
b'\r' => out.extend_from_slice(br"\r"),
b'\t' => out.extend_from_slice(br"\t"),
0x08 => out.extend_from_slice(br"\b"),
0x0c => out.extend_from_slice(br"\f"),
_ if is_iso_latin1_printable(byte) => out.push(byte),
_ => {
out.push(b'\\');
out.extend_from_slice(format!("{byte:03o}").as_bytes());
}
}
}
out.push(b')');
}
/// Write a string in qpdf's literal-or-hex representation.
pub(crate) fn write_string_value(out: &mut Vec<u8>, value: &[u8]) {
if use_hex_string(value) {
write_hex_string(out, value);
} else {
write_literal_string(out, value);
}
}
/// Write bytes as a lowercase hexadecimal PDF string.
pub(crate) fn write_hex_string(out: &mut Vec<u8>, value: &[u8]) {
const HEX: &[u8; 16] = b"0123456789abcdef";
out.push(b'<');
for &byte in value {
out.push(HEX[(byte >> 4) as usize]);
out.push(HEX[(byte & 0x0f) as usize]);
}
out.push(b'>');
}
/// Callback used by the two-pass trailer writers to emit an /ID value.
pub(crate) type TrailerIdWriter<'a> = &'a mut dyn FnMut(&mut Vec<u8>);
/// Reborrowable two-lifetime form of TrailerIdWriter.
pub(crate) type ReborrowableIdWriter<'r, 'd> = &'r mut (dyn FnMut(&mut Vec<u8>) + 'd);
#[cfg(test)]
mod tests {
use super::{real_literal_is_safe, write_name_escaped};
#[test]
fn real_literal_rejects_non_utf8_source_bytes() {
assert!(!real_literal_is_safe(&[0xff], 0.0));
}
#[test]
fn name_escaping_covers_nul_delimiters_and_non_ascii_bytes() {
let mut output = Vec::new();
write_name_escaped(&mut output, b"a\0#/ \x80z");
assert_eq!(output, b"a##23#2f#20#80z");
}
#[test]
fn name_escaping_restores_the_stray_hash_sentinel_as_a_bare_hash() {
// The tokenizer's recoverable-stray-# sentinel (a raw NUL byte) must
// round-trip back to a bare `#`, matching qpdf's
// `QPDF_Name::normalizeName` (`libqpdf/QPDF_Name.cc:35-37`) -- not
// `#00`, which is a distinct, valid escape for an actual NUL that the
// tokenizer never decodes to a raw byte in the first place.
let mut output = Vec::new();
write_name_escaped(&mut output, b"a\0\x31x");
assert_eq!(output, b"a#1x");
}
}