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
//! Byte-identity demonstration: flpdf plain rewrite == `qpdf --static-id`.
//!
//! This is the capstone of the qpdf byte-identical roadmap. It is gated on the
//! `qpdf-zlib-compat` feature because byte-identity requires flpdf's deflate
//! output to match qpdf's classic-libz output (the Pure-Rust miniz_oxide default
//! produces equivalent but not byte-identical compression). Three independent
//! pieces must all line up:
//!
//! 1. Stream-dictionary key order — `/Length` pulled out, `/Filter` last on
//! re-filtered streams (matches `QPDFWriter::unparseObject`).
//! 2. Trailer on the `trailer ` line with keys sorted and `/ID` last.
//! 3. No newline before `endstream` ([`NewlineBeforeEndstream::Never`]) —
//! qpdf's default output writes exactly `/Length` bytes then `endstream`.
//!
//! plus deflate parity (this feature) and the deterministic `--static-id` trailer
//! `/ID`. With all of these, flpdf's full rewrite is `cmp`-diff-0 against the
//! committed `qpdf --static-id` golden references.
//!
//! CAVEAT: byte-identity pins to the linked libz version (captured with zlib1g
//! 1:1.3.dfsg-3.1ubuntu2.1 / qpdf 11.9.0); a different libz may shift the deflate
//! bytes and require re-blessing the goldens.
#![cfg(feature = "qpdf-zlib-compat")]
use flpdf::{
write_pdf_with_options, NewlineBeforeEndstream, ObjectStreamMode, Pdf, StreamDataMode,
WriteOptions,
};
use std::path::Path;
/// Full-rewrite `fixture` with the qpdf-matching option set and return the bytes.
fn rewrite_qpdf_equivalent(fixture: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(fixture);
let file = std::fs::File::open(&path).unwrap_or_else(|e| panic!("open {path:?}: {e}"));
let mut pdf = Pdf::open(std::io::BufReader::new(file)).unwrap();
let mut opts = WriteOptions::default();
opts.full_rewrite = true;
opts.static_id = true;
// qpdf's default output writes no newline before endstream.
opts.newline_before_endstream = NewlineBeforeEndstream::Never;
// compress_streams defaults to Yes (decode + re-encode to single FlateDecode).
let mut out = Vec::new();
write_pdf_with_options(&mut pdf, &mut out, &opts).unwrap();
out
}
fn golden(fixture_stem: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/golden/references")
.join(fixture_stem)
.join("static-id.pdf");
std::fs::read(&path).unwrap_or_else(|e| panic!("read golden {path:?}: {e}"))
}
/// Report the first differing byte offset for a readable failure message.
fn first_diff(a: &[u8], b: &[u8]) -> Option<usize> {
if a == b {
return None;
}
let common = a.len().min(b.len());
for i in 0..common {
if a[i] != b[i] {
return Some(i);
}
}
Some(common)
}
fn assert_cmp_diff_zero(fixture: &str, stem: &str) {
let actual = rewrite_qpdf_equivalent(fixture);
let expected = golden(stem);
if let Some(off) = first_diff(&actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"{fixture}: not byte-identical to qpdf --static-id golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
/// Full-rewrite `fixture` with an explicit object-stream `mode` + forced version
/// (qpdf-matching options).
fn rewrite_mode_force_qpdf_equivalent(
fixture: &str,
mode: ObjectStreamMode,
force: &str,
) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(fixture);
let file = std::fs::File::open(&path).unwrap_or_else(|e| panic!("open {path:?}: {e}"));
let mut pdf = Pdf::open(std::io::BufReader::new(file)).unwrap();
let mut opts = WriteOptions::default();
opts.full_rewrite = true;
opts.object_streams = mode;
opts.force_version = Some(force.to_string());
opts.static_id = true;
opts.newline_before_endstream = NewlineBeforeEndstream::Never;
let mut out = Vec::new();
write_pdf_with_options(&mut pdf, &mut out, &opts).unwrap();
out
}
/// Read a named golden under `references/<stem>/`.
fn golden_named(stem: &str, name: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/golden/references")
.join(stem)
.join(name);
std::fs::read(&path).unwrap_or_else(|e| panic!("read golden {path:?}: {e}"))
}
/// Full-rewrite `fixture` in `--stream-data=preserve` mode with the qpdf-matching
/// option set (matches `qpdf --static-id --stream-data=preserve`).
fn rewrite_preserve_qpdf_equivalent(fixture: &str) -> Vec<u8> {
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../tests/fixtures/compat")
.join(fixture);
let file = std::fs::File::open(&path).unwrap_or_else(|e| panic!("open {path:?}: {e}"));
let mut pdf = Pdf::open(std::io::BufReader::new(file)).unwrap();
let mut opts = WriteOptions::default();
opts.full_rewrite = true;
opts.static_id = true;
opts.stream_data = Some(StreamDataMode::Preserve);
opts.newline_before_endstream = NewlineBeforeEndstream::Never;
let mut out = Vec::new();
write_pdf_with_options(&mut pdf, &mut out, &opts).unwrap();
out
}
/// Assert `actual` is byte-identical to the named golden under `references/<stem>/`.
fn assert_cmp_diff_zero_named(actual: &[u8], stem: &str, name: &str) {
let expected = golden_named(stem, name);
if let Some(off) = first_diff(actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"{stem}/{name}: not byte-identical to qpdf golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
#[test]
fn force_below_1_5_downgrades_xref_stream_source_byte_identical_to_qpdf() {
// The xref-stream -> classic-table DOWNGRADE path is new (ipc6 only ever did
// table -> stream UPGRADES). Anchor it to qpdf: flpdf preserve+force1.4 on an
// ObjStm/xref-stream source must be byte-identical to qpdf's classic-table
// output (qpdf --object-streams=preserve --force-version=1.4 --static-id).
let actual = rewrite_mode_force_qpdf_equivalent(
"three-page-objstm.pdf",
ObjectStreamMode::Preserve,
"1.4",
);
let expected = golden_named("three-page-objstm", "downgrade-force14.pdf");
if let Some(off) = first_diff(&actual, &expected) {
let lo = off.saturating_sub(16);
panic!(
"xref-stream downgrade not byte-identical to qpdf golden \
(flpdf={} bytes, golden={} bytes, first diff at byte {off})\n\
flpdf : {:?}\ngolden: {:?}",
actual.len(),
expected.len(),
&actual[lo..(off + 16).min(actual.len())],
&expected[lo..(off + 16).min(expected.len())],
);
}
}
#[test]
fn one_page_plain_rewrite_is_byte_identical_to_qpdf_static_id() {
assert_cmp_diff_zero("one-page.pdf", "one-page");
}
#[test]
fn two_page_plain_rewrite_is_byte_identical_to_qpdf_static_id() {
assert_cmp_diff_zero("two-page.pdf", "two-page");
}
#[test]
fn three_page_plain_rewrite_is_byte_identical_to_qpdf_static_id() {
assert_cmp_diff_zero("three-page.pdf", "three-page");
}
#[test]
fn lone_flate_l9_plain_rewrite_is_byte_identical_to_qpdf_static_id() {
// A lone /FlateDecode source compressed at level 9: flpdf must preserve the
// bytes verbatim (qpdf default), so re-encoding at level 6 would diverge.
assert_cmp_diff_zero("lone-flate-l9.pdf", "lone-flate-l9");
}
#[test]
fn od_indirect_length_plain_rewrite_drops_orphan_holder_byte_identical_to_qpdf() {
// The catalog's /OpenAction reaches a JavaScript stream (obj 6) with an
// INDIRECT /Length (7 0 R); the holder (obj 7) is reachable ONLY through
// that /Length edge. Once /Length is normalized to a direct integer the
// holder orphans, and qpdf garbage-collects it. The plain full-rewrite path
// must drop it too (flpdf-sqkq), shifting object numbers contiguously — not
// emit it as a trailing integer object.
assert_cmp_diff_zero(
"objstm-lin-od-indirect-length.pdf",
"objstm-lin-od-indirect-length",
);
}
#[test]
fn od_indirect_length_flate_plain_rewrite_drops_orphan_holder_byte_identical_to_qpdf() {
// Same orphan structure, but the JS stream is a lone /FlateDecode (the
// writer's verbatim-preserve path): /Length is direct-ized to the
// compressed byte count when the holder is dropped.
assert_cmp_diff_zero(
"objstm-lin-od-indirect-length-flate.pdf",
"objstm-lin-od-indirect-length-flate",
);
}
#[test]
fn od_indirect_length_preserve_drops_orphan_holder_byte_identical_to_qpdf() {
// --stream-data=preserve keeps the stream bytes verbatim, but qpdf still
// direct-izes every stream's /Length and GCs the orphaned holder (flpdf-3g8o).
// The orphan-drop gate must therefore fire for preserve too, not only when
// streams are recompressed.
let actual = rewrite_preserve_qpdf_equivalent("objstm-lin-od-indirect-length.pdf");
assert_cmp_diff_zero_named(&actual, "objstm-lin-od-indirect-length", "preserve.pdf");
}
#[test]
fn od_indirect_length_flate_preserve_drops_orphan_holder_byte_identical_to_qpdf() {
// Same as above with a lone /FlateDecode JS stream: under preserve the
// compressed bytes are kept verbatim and /Length is direct-ized to the
// compressed byte count when the holder is dropped.
let actual = rewrite_preserve_qpdf_equivalent("objstm-lin-od-indirect-length-flate.pdf");
assert_cmp_diff_zero_named(
&actual,
"objstm-lin-od-indirect-length-flate",
"preserve.pdf",
);
}
#[test]
fn kept_indirect_length_preserve_directizes_kept_holder_byte_identical_to_qpdf() {
// The dual of the orphan case: the image XObject's indirect /Length holder is
// ALSO referenced by the catalog (/KeepHolder), so it stays live. qpdf still
// direct-izes the stream's /Length even under preserve (it normalizes EVERY
// stream's /Length to a direct integer), keeping the holder as a now
// length-unreferenced live integer. flpdf must match (flpdf-3g8o).
let actual = rewrite_preserve_qpdf_equivalent("kept-indirect-length.pdf");
assert_cmp_diff_zero_named(&actual, "kept-indirect-length", "preserve.pdf");
}
#[test]
fn kept_indirect_length_plain_rewrite_directizes_length_keeps_holder_byte_identical_to_qpdf() {
// Dual of the orphan case (flpdf-q1j2): an image XObject (obj 5) declares
// /Filter /DCTDecode — which flpdf cannot decode, so it is passed through
// verbatim — and carries an INDIRECT /Length (6 0 R) whose holder (obj 6) is
// ALSO referenced by the catalog (/KeepHolder 6 0 R). qpdf direct-izes the
// /Length to the raw byte count (every emitted stream gets a direct /Length)
// while KEEPING the holder (it has another live reference). The decode-failure
// passthrough path used to leak the renumbered indirect /Length; this pins it
// byte-identical to qpdf.
assert_cmp_diff_zero("kept-indirect-length.pdf", "kept-indirect-length");
}