JoshLambert.xyz

Small businesses, independent infrastructure, and a stubborn bet that things get better.

rar-reader: A Rust Library for Reading RAR Archives

rar-reader: A Rust Library for Reading RAR Archives

Battlefront Eternal is a project of mine, a fan-made engine written in Rust that plays the 2004 game Star Wars: Battlefront and its 2005 sequel. It has a mod manager, and you add a mod by handing it the archive the mod came in. Mods get published as .zip, .7z and .rar files, so the manager has to open all three.

RAR was the one that took some work. The engine is released under the GPL, version 3 only, so whatever reads RAR inside it needs a license that’s compatible with that. A mod is also a file a stranger made and somebody downloaded, which is the kind of input I want handled in safe Rust with a ceiling on how much memory and disk it can ask for.

So my company, Centreville Tech, wrote rar-reader and released it free and open source. It’s a Rust library and a small command line tool. It reads RAR archives and can’t create them.

Where the code came from

The decoding logic is libarchive’s. libarchive has two RAR readers, one for RAR 1.5 through 4.x and one for RAR 5, written in C under the 2-clause BSD license. rar-reader is a port of those two files to Rust, at libarchive 3.8.11.

The other pieces have their own sources, and the NOTICE file in the repo lists them file by file:

  • The PPMd code is ported from libarchive’s copy of Igor Pavlov’s public domain PPMd variant H, which is based on Dmitry Shkarin’s public domain original.
  • BLAKE2s is written from RFC 7693, with the BLAKE2sp tree set up the way libarchive’s bundled reference code does it.
  • CRC-32 is the standard one, with no outside code.

No code from the unRAR source, UnRAR.dll, WinRAR or anything else under the unRAR license was used or read. RARLAB’s public RAR 5.0 format note was used only as documentation of the container layout. Every ported file keeps libarchive’s copyright notice.

rar-reader is under the 2-clause BSD license too. That license is compatible with the GPL, so a GPL-3.0-only program can use the library and ship it as long as the notices stay with it. It’s an independent project, with no affiliation with RARLAB, win.rar GmbH or the libarchive project.

How it reads an archive

A diagram titled “How rar-reader reads an archive,” with five numbered steps. One, find the archive: look for the RAR 4 or RAR 5 signature at the start, and if the file opens with a Windows or ELF program, search past it, 4 MiB at most, without running it. Two, read every header: names, sizes, times, links and checksums from every volume of a set, with nothing decompressed yet. Three, refuse before decoding: encrypted data, a format it doesn’t read, or an entry past the size limits stops with a named error. Four, decode: RAR 4 uses stored, LZSS or PPMd with delta, x86, RGB and audio filters, and RAR 5 uses stored or LZSS with delta, x86 and ARM filters. Five, check the result with CRC-32 or BLAKE2sp, and stop at the size the header declared. A note underneath lists what the command line tool adds: unsafe names are refused, files are written under a temporary name until the checksum matches, and links are never created.
The path from the first bytes of a file to a checked copy of what was packed.

Opening an archive reads every header in every volume and decompresses nothing. You get the list of entries with names, sizes, times, attributes and checksums, and you extract the ones you want. A set split across .part1.rar, .part2.rar and so on comes back as one archive, and so does the older .rar, .r00, .r01 naming. A self-extracting archive is an archive with a program stuck on the front. rar-reader skips the program and never runs it.

RAR 4 has two compressors, LZSS and PPMd, and an archive can switch between them from one block to the next. RAR 5 has only LZSS. Both formats run filters over the output that undo a transform the packer applied to make things like audio, bitmaps and machine code compress better.

Every extraction is checked against the checksum stored in the archive, and it stops at the size the header declared.

Limits for files you don’t trust

Every size an archive can state is checked against a limit the caller sets. The defaults are 320 MiB of decoder memory, 8 GiB for one entry, 64 GiB written in total, 200,000 entries, 1,000 volumes, and a compression ratio of 4,096 to 1 on entries over 16 MiB.

The crate forbids unsafe code and has no dependencies beyond Rust’s standard library. A damaged archive gets a typed error back. libarchive’s test set includes archives with names like ppmd_use_after_free and distance_overflow, which gives you an idea of what the C readers have been through, and rar-reader’s tests run every one of the damaged ones.

What it doesn’t read

What libarchive can’t read, rar-reader doesn’t read either.

  • Encrypted archives are detected and reported, and that’s all. libarchive has no RAR decryption to port, and I didn’t write any for this version.
  • In a solid RAR 4 archive it reads the first file and refuses the ones that continue the stream. Solid RAR 5 archives work, in any order.
  • The old RAR 1.5 and 2.x compression isn’t read, and neither is the newer compression RAR 7 added.
  • RAR 4 lets an archive carry its own filter program. rar-reader runs the five standard ones and refuses any other.

In each case you get an error that names the feature.

A terminal running the rar-reader command line tool on four archives. Testing encrypted.rar reports a.txt and c.txt OK and fails b.txt and d.txt with “unsupported RAR feature: encrypted data.” Listing locked.rar fails with “unsupported RAR feature: encrypted headers.” Testing damaged.rar fails with “damaged RAR archive: too small block encountered.” Testing ppmd.rar reports its one file OK.
Version 0.1.0 on four of libarchive’s test archives, copied under shorter names: two encrypted, one damaged, and one that mixes PPMd and LZSS blocks.

The command line tool

The tool is called rar-reader and it has three commands: list, test and extract. Each one takes --json.

A terminal session. Listing the folder shows four files, solid.part01.rar through solid.part04.rar. The command “rar-reader test solid.part01.rar” prints OK beside nine file names and then “9 files OK, 0 failed.” The command “rar-reader extract solid.part01.rar out2” prints “9 written, 0 skipped, 0 failed,” and listing out2 shows the nine files.
Testing and extracting a solid RAR 5 set split across four volumes. It’s one of libarchive’s test archives, copied under a shorter name.

extract writes only under the folder you name. It refuses names like ../x, /x and C:x, writes each file under a temporary name, and renames it into place once the checksum matches. Links inside an archive are listed and never created.

A terminal showing the JSON listing piped through jq. The archive summary reads format rar5, solid true, multi_volume true, volumes 4, sfx_offset 0. One entry, elf-Linux-ARMv7-ls, shows its raw name in hex, kind file, size 90808, packed size 40916, method best, host OS Unix, first volume 0, last volume 3, a 1 MiB dictionary, a modified time, a CRC-32 and a safe path.
The JSON listing of the same set, cut down with jq to the archive summary and one entry that spans all four volumes.

How I checked it

A port can go wrong in ways that look fine on the first ten archives, so I tested it four ways.

  • Unit tests cover the bit readers, the Huffman tables, the PPMd model, each filter, name decoding, the checksums and the path check.
  • libarchive’s own test archives run in CI on Linux, Windows and macOS. For eight of them the tests also try every truncation and 3,000 corruptions each, and nothing is allowed to panic.
  • On my own machine I compared it with bsdtar and 7-Zip on 101 archives: libarchive’s test archives plus nine RAR archives of game mods published online, the largest 182 MB unpacked. Of the 226 files inside, 202 came out byte for byte equal to bsdtar’s and 220 equal to 7-Zip’s, and none came out different. The rest are files bsdtar can’t read from the command line or that rar-reader refuses.

An excerpt of a GitHub Actions log for the command “cargo test –workspace –release.” It shows 38 unit tests passing, then 17 of 18 archive tests passing with one ignored in 14.62 seconds, then 5 documentation tests passing. Most test lines are cut and marked as such.
The test run on the v0.1.0 tag, from the GitHub Actions log for the Linux job, with most lines cut for length.

The fourth was fuzzing. Six targets feed damaged input to the whole reader and to each part of it by itself. Each target ran for 60 to 90 minutes without a finding, and CI runs all six for 30 seconds on every push.

A table titled “Six fuzz targets, no findings.” The archive target covers the whole reader: 1.6 million runs in 80 minutes, and 18,242 runs in CI. Headers: 79 million runs in 60 minutes, 907,338 in CI. rar4_lz, the RAR 4 decoder: 5.5 million runs in 90 minutes, 17,005 in CI. ppmd: 2.4 million runs in 90 minutes, 3,318 in CI. rar5: 5.8 million runs in 90 minutes, 17,723 in CI. Filters, Huffman tables and names: 21 million runs in 60 minutes, 80,359 in CI.
The long fuzz runs from the README, beside the short runs CI made on the v0.1.0 tag.

One gap is written down in the README: the RAR 4 x86 filter has two forms, and the plainer one is unit tested and fuzzed but doesn’t appear in any test archive I have.

How fast it is

A bar chart titled “Time to decode an archive,” in seconds on one thread. First RAR 4 archive, 142 MB unpacked: rar-reader 0.49, bsdtar 0.50, 7-Zip 0.45. Second RAR 4 archive, 139 MB: rar-reader 0.71, bsdtar 0.72, 7-Zip 0.53. RAR 4 with RGB filters, 27 MB: rar-reader 0.21, bsdtar 0.19, 7-Zip 0.18. RAR 4 with PPMd and LZSS blocks, 242 MB: rar-reader 0.12, bsdtar 0.09, 7-Zip 0.13. RAR 5, 182 MB: rar-reader 0.29, bsdtar 0.32, 7-Zip 0.15.
Decode times from the README: the median of seven runs on one thread of an AMD Ryzen AI 9 HX 370.

On the five archives I timed, rar-reader finished within 0.03 seconds of bsdtar, which is the C code it was ported from. 7-Zip was faster on four of the five, and about twice as fast on the RAR 5 one. Peak memory was 7 MB on the RAR 4 archives and 35 MB on the RAR 5 one, whose header asks for a 32 MiB dictionary.

Those times came from a desktop with other work running, and they moved around between sessions. Compare the three tools on one archive and don’t read much into the differences between archives.

Where to get it

Version 0.1.0 is on GitHub with command line builds for Linux, macOS and Windows. It’s also on crates.io as rar-reader and rar-reader-cli, and it builds with Rust 1.75 or later.

Battlefront Eternal’s movies needed the same treatment, and that became bink-decoder. The rest of what I’ve released lately is for mobile apps and wireless networks: Ti.Socketline, Ti.Paysheet and RESTe 2 for Titanium apps, and a SIM card generator for private LTE and 5G networks.

Do you have a file format nobody on your team wants to touch, or software that has to read one? Send me an email and tell me what it is.