Skip to main content
coreydaley.dev

PAR2 from the spec: CRC32 finds the damage, MD5 proves the repair

New in Purrchiver 1.1. If you don't see it, update Purrchiver from the App Store.

Recovering what’s readable gets back whatever survived. Recovery data gets back everything, up to the amount you added. Purrchiver 1.1 can add it when you create an archive, and repair a damaged archive from it. It’s PAR2, the format Usenet has relied on for more than twenty years, so anyone with a PAR2 tool can use the file, with or without Purrchiver.

I wrote it from the public specification, not from an existing implementation. The best-known one, par2cmdline, is GPL, and Purrchiver isn’t. par2cmdline still earned its keep: as the reference the interop tests check against, and as a source of test files that broke things. Along the way, a test file named for a CRC32 failure turned out not to be one, and that settled how I think about the two hashes PAR2 stores.

The idea in one paragraph

Split the file into equal slices. From those, compute some extra slices, each a different weighted sum of all the originals. Store the extras in a .par2 file beside the archive. Later, if some slices are damaged, you have one equation per recovery slice and one unknown per damaged slice. With enough good recovery slices, you solve for the missing data exactly.

The arithmetic happens in a finite field, GF(2^16), where adding is XOR and every nonzero number has an inverse, so you can divide, and solving a system of equations works the way it does in school algebra. The spec fixes everything: the field’s polynomial (0x1100B), which constant goes with which input slice, and how recovery slices are numbered. Purrchiver numbers its recovery slices from 0. Every weight raised to the power 0 is 1, so recovery slice 0 is plain XOR parity. That falls out of the math rather than being a special case in the code.

Repair builds a small matrix, one row per recovery slice used and one column per damaged slice, inverts it, and multiplies through. The slow part isn’t the matrix. It’s multiplying and adding whole slices, megabytes at a time, so that’s the part with a vector path on Apple silicon: table lookups that do 32 bytes per step, checked against a plain scalar version with random data at lengths from zero to just past 64 KB, odd tails included.

Slices, and what 10% buys

Purrchiver picks the smallest power-of-two slice size, at least 4 KB, that gives at most a thousand slices. The 5, 10 and 20% options set how many recovery slices to make, as a share of that count.

The important consequence: repair capacity is counted in slices, not bytes. In a file Purrchiver protected, ten recovery slices fix any ten damaged slices, whatever happened inside them. One flipped bit costs a whole slice. So does a run of bad sectors that fits in one slice. That’s why the help page says scattered small errors count for more than their size, and why small archives get a little extra: a 10 KB file has three slices, and you can’t make a tenth of one.

Two hashes, two jobs

For every slice, a PAR2 file stores two fingerprints: a CRC32 and an MD5.

CRC32 is the fast filter. It’s very good at catching accidental damage. It catches every single-bit error and every burst of errors up to 32 bits long, guaranteed. In practice, for damage, it’s the one that finds the bad slice. What it isn’t is a proof that two slices are identical: it’s only 32 bits, so different data will sometimes match, and anyone who wants to can make it match.

MD5 is the proof. In Purrchiver a slice counts as good only if both match. A test builds damage CRC32 can’t see, by XORing CRC32’s own polynomial into a slice, and checks that MD5 catches it and repair fixes it. And MD5 gets the final word in two places where being wrong would be expensive:

  • A repaired file is kept only if its whole-file MD5 matches the original’s. If the rebuilt file is off by one byte, it’s deleted, not handed to you as fixed.
  • Every packet in the .par2 file carries an MD5. If part of the recovery file is itself damaged, those packets are dropped rather than trusted. A test corrupts one recovery packet and checks that repair still succeeds without it.

MD5 is broken as a defense against a deliberate forger. Here it’s detecting damage, not tampering, and against damage it’s more than enough.

Test files that broke things

I vendored the test sets from par2cmdline’s own suite and from a Rust PAR2 implementation (data files only, no code), and a few earned their place:

  • A single flipped bit in 9 MB of identical bytes. par2cmdline once failed to repair this one. The test file is even named as if its CRC32 still matches, but it doesn’t: CRC32 catches every single-bit flip, and I checked the numbers. Purrchiver reads slices at fixed positions, finds the damaged one, and rebuilds the file byte for byte, both from recovery data it made and from a set par2cmdline made.
  • A file cut short. The missing tail counts as damaged slices and is rebuilt.
  • File names in an old Windows code page. Older tools wrote names that aren’t UTF-8, so names fall back to Windows-1252 when they don’t decode.
  • Packets with valid checksums and hostile numbers. A slice size near the maximum 64-bit integer, a file length that overflows, forty thousand slices, a one-terabyte slice. The checksum only proves the packet arrived as written, not that whoever wrote it was honest. Each of these must fail with a clear error and leave nothing behind. Before the fix, these crashed the app.

And two deliberate differences from par2cmdline, tested so they stay deliberate: when every slice matches, Purrchiver calls the file intact even if the set’s whole-file hash disagrees, and it matches files by name rather than by the hash of their first 16 KB.

Checked against the reference, both ways

The interop tests run in both directions:

  • Purrchiver creates recovery data, and par2cmdline verifies it.
  • Purrchiver creates it, the archive loses its last 7%, and par2cmdline repairs it.
  • par2cmdline creates it, the archive takes 30 scattered damaged bytes, and Purrchiver repairs it byte for byte.

I checked the tests by breaking the code on purpose. Skip the packet checksum, skip damage detection, or use the wrong coefficients, and tests fail each time.

Never touching the damaged file

Repair always writes a new file, like Photos (repaired).zip, and opens the damaged one read-only. Since that’s a public promise, it’s enforced, not just intended: the code refuses an output path that is the source or its .par2, including through a hard link, and tests check the source’s bytes, size and modification date before and after every create, verify, repair and failed repair.

On a failing drive, Purrchiver reads past unreadable sectors during repair, treating them as damaged slices instead of giving up. It won’t do that when creating recovery data, so it never protects a file full of zeros.

Limits

  • Purrchiver creates recovery data for one archive at a time. It can repair sets that cover several files, made by other tools.
  • Data that has shifted isn’t searched for. If bytes were inserted or removed, rather than changed, slices after that point are at the wrong offsets. par2cmdline searches for them; Purrchiver doesn’t.
  • Not every combination of recovery slices works. PAR2’s construction only guarantees a solution when the recovery slices used are numbered consecutively, which they are in a file Purrchiver made with nothing lost. If some of them are damaged or missing, a gap can leave a particular combination unsolvable. That’s a known property of the format, not of this implementation. When it happens, Purrchiver tries the other recovery slices in order, keeping each one that helps, and only if no combination can rebuild the damage does it say there isn’t enough recovery data.
  • Speed: on an M2 Pro with 16 GB of memory, 1 GB of random data at 10% took about 4.6 seconds to create recovery data for. Repair took about 3.7 seconds with one damaged slice, 4.5 with damage to half of what the recovery data covers, and 5.1 at the limit. I timed the same code the app runs, outside the app, with other work going on: create and the half-damage repair three times each, the other two once. Treat these as rough numbers, not a benchmark.

Adding recovery data is part of the one-time unlock. Repairing is free, and always will be: it’s your file.

When a tool tells you a file is “repaired”, what would you want it to have checked before you believed it?

Related posts