Field notes/Mediarusty_pdfrusty_pdf_core

A lopdf Alternative in Rust: Testing rusty_pdf_core Against 989 PDFs

How rusty_pdf_core 0.2.0 was checked against lopdf on the pdf.js corpus: a differential harness, hostile-input limits, and a flate2 feature that had silently switched SIMD off.

Signed by Tim Almond·
Painted stone mill beside a stream at dawn, where a wooden mould lifts a pale sheet from a barrel vat, finished sheets hang on a rack and a lit lantern glows on the wall
What makes rusty_pdf_core a credible lopdf alternative?

It is tested against lopdf directly. A parity harness builds files with lopdf, reads them with both libraries and compares object trees. On the 989-file pdf.js corpus the trees match on 957 of 982 files both open, and the differences inspected so far were lopdf's.

Ship it

986 of 989 PDFs Open

On the pinned pdf.js corpus: zero panics and zero timeouts. Rendering and writing are not published yet.

Open the rusty_pdf conversion pin →

I wanted a lopdf alternative in Rust that I could test against lopdf itself, and rusty_pdf_core is the first piece of that. Version 0.2.0 reads 986 of the 989 files in the pdf.js corpus, with no panics and no timeouts. This post is about how that was measured, what the harness found, and a flate2 detail that cost every Flate stream its SIMD path.

The workspace is the public rusty_pdf repository. The crate on crates.io is rusty_pdf_core, not rusty_pdf, for a reason I'll get to in a moment.

Why a lopdf alternative at all

Today the house reads and edits PDF through lopdf, parses for rendering through forks of hayro, and writes through a fork of krilla. That is three upstream stacks with three object models. The repo's plan puts the vendored code at about 126,000 lines, and says Freehold PDF already carries patches to make two of them agree on page size.

One workspace fixes that. One object model gets parsed by the reader, changed by the editor and written by the writer. A fix to a filter or a bound lands for every consumer at once.

Only the first slice has shipped: the reader core. Fonts, the renderer and the writer are phases two and three, and they are not on crates.io. I'd rather say that up front than let the README table do it for me.

The name matters here too. rusty_pdf on crates.io is somebody else's crate, and cargo treats rusty-pdf and rusty_pdf as the same name. So the repository keeps the name and the layer crates carry suffixes. Typing cargo add rusty_pdf gets you a stranger's code.

How a hostile-input reader gets tested

A differential harness against lopdf

The harness builds files with lopdf: classic tables, object and cross-reference streams, and every standard encryption scheme. Then it reads each with both readers and compares the object trees. Next it saves with our writer and reads the result back with both.

A second reader is a better oracle than a hand-written expected value, because it catches the cases you did not think of. It is also only as good as its own bugs, which is why the corpus run matters.

Damage, repair and mutation

PDF readers earn their keep on files that are slightly broken. The tests damage the cross-reference data four ways and check that repair finds the objects again. crates/rusty_pdf_core/tests/hostile.rs runs a deterministic mutation pass under plain cargo test, and fuzz/ holds cargo-fuzz targets.

The design rule is that bad input returns an error or a bounded result. Every stream decode takes a byte cap. Images are bounded by their declared size before allocation, and nesting, reference chains and /Prev chains use depth limits or visited sets. That is the same idea as in our note on parser limits before allocation.

What the corpus run says

The corpus is pdf.js's test set at a pinned commit, 989 files, hash-checked. For a lopdf alternative, that is the number I'd want first. 986 opened. Page content decoded and parsed on 1,829 of 1,843 pages, and 1,133 of 1,141 images decoded. Where both readers open a file, the trees match on 957 of 982.

The README says the differences inspected so far were lopdf's: empty content streams where the file has content, undecrypted data, and objects dropped during repair. That leaves 25 files where the trees differ, and the README does not claim every one has been inspected. "So far" is its phrase, so it is mine too.

The 0.2.0 speed pass

Counting instructions, then timing

The speed work used callgrind for exact instruction counts, then paired ABBA wall-clock runs against a null arm. The 1-bit image samples dropped 82 percent in instructions. Raw image samples fell 38 percent, with a 1.29 times speedup.

Page content parsing fell 24 percent in instructions, and ran 1.41 times faster in one round and 1.23 times in the next. The text layer fell 22 percent. Heap allocations went from 9.0 million to 4.2 million over the corpus, from changes like static operators, exact-size vectors and fonts decoded once per document.

The flate2 feature that was off

The crate uses flate2 with its zlib-rs backend. zlib-rs picks its AVX2 inflate at run time, but flate2 only compiles that detection in through runtime_detection, and default-features = false had dropped it. So every Flate stream inflated on the scalar path, on every CPU.

The new inflate-simd feature turns it back on by default. Flate streams got 1.06 times and 1.12 times faster in two rounds, and output is identical either way. If you pin flate2 with default features off, check yours. The house also forks that backend as rusty_zlib, but flate2 here still pulls upstream's.

A breaking change and a fixed bug

Operation::operator is now an Operator rather than a String, so parsing a content stream no longer allocates one string per operator. The minimum Rust version is 1.89.

The bug fix came from a consumer. FFAI is adopting the crate in its PDF code right now, and a diff over the corpus found one real bug here, a JPEG 2000 image in an indexed space, plus eight differences where the old lopdf path was wrong. The install line and crate pin are on the rusty_pdf repo page, and MATA's product notes cover the ingest side. See also rusty_jbig2 and rusty_jpx, which decode the scan images inside these files.

FAQ

Quick answers for builders evaluating this technology.

How does the parity harness compare the two readers?

It builds PDFs with lopdf in classic and stream cross-reference form, with every standard encryption scheme, and reads each with both libraries. It compares the object trees, saves with this crate's writer and reads the result back with both, then damages the cross-reference data four ways.

Why did SIMD inflate never run before 0.2.0?

flate2 only compiles zlib-rs's CPU detection through its runtime_detection feature, and default-features = false had dropped it. Every Flate stream took the scalar path. 0.2.0 adds an inflate-simd feature, on by default, that turns it back on. Output is identical either way.

What breaks when moving from 0.1.0 to 0.2.0?

Operation::operator is now an Operator type instead of a String. as_str(), comparison with a string, Deref to str and formatting all still work, and the 73 standard operators are static strings. The minimum Rust version also rose to 1.89 because RustCrypto's aes 0.9 needs it.

Which parts of the PDF stack are still missing?

Font programs, the renderer and the writer are planned as later phases and are not published. The reader core can save a document, but the plan still lists qpdf --check on every save, a CPU-hour of libFuzzer and the deputy gate as open.

Why is the repository named rusty_pdf but the crate rusty_pdf_core?

A crate named rusty_pdf already exists on crates.io and belongs to an unrelated project. Cargo treats rusty-pdf and rusty_pdf as the same name, so the team published the layer crate as rusty_pdf_core and kept the repository name for the whole workspace.