Built for Battlefront Eternal · BSD-2-Clause
rar-reader
A reader for RAR archives, written in safe Rust with no unsafe code and no dependencies beyond the standard library. It reads RAR 1.5 to 4.x and RAR 5, single archives, multi-volume sets and self-extracting archives. It is a port of libarchive's RAR readers, and it only reads archives: it cannot create them.
Why the project built it
Many classic Battlefront mods were published as RAR archives. Battlefront Eternal's mod manager installs them without asking players for another program. The engine is GPL-3.0-only and has no unsafe code, and no existing RAR reader fitted both: so the project ported libarchive's, the code behind bsdtar, to safe Rust.
It reads archives that come from anywhere, so every size, count and name in them is checked, and the caller sets how much memory and output an archive may use.
What it reads, and what it refuses
| Part | Read | Not read |
|---|---|---|
| RAR 1.5 to 4.x container | Main, file, comment, recovery and other blocks; Unicode and code page file names; extended times; MS-DOS, Windows and Unix attributes | Encrypted headers (reported as such) |
| RAR 4 stored and LZSS (RAR 2.9 to 4.x) | Yes | RAR 1.5 and 2.x compression (unpack versions 15 to 26) |
| RAR 4 PPMd variant H | Yes, also switching with LZSS blocks | Filters inside PPMd blocks |
| RAR 4 filters | Delta, x86 E8, x86 E8/E9, RGB, audio | Any other filter program |
| RAR 4 solid archives | The first file of a solid stream | Files that continue a solid stream (libarchive does not read them either) |
| RAR 5 container | Main, file, service and end blocks; UTF-8 names; Unix and Windows times; file versions; attributes | Encrypted headers (reported as such) |
| RAR 5 stored and LZSS | Compression version 0, dictionaries up to 4 GiB within the memory limit | RAR 7's compression version 1; files of unknown size |
| RAR 5 filters | Delta, x86 E8, x86 E8/E9, ARM | |
| RAR 5 solid archives | Yes, entries in any order | |
| Checksums | CRC-32 (RAR 4 and 5) and BLAKE2sp (RAR 5), verified on every extraction | |
| Multi-volume sets | RAR 4 (.rar, .r00, .r01, … and .partN.rar) and RAR 5 (.partN.rar), files split across volumes | |
| Self-extracting archives | The archive after a Windows or ELF program stub, which is skipped and never run | |
| Links | Symbolic links, junctions, hard links and RAR 5 file copies, listed with their targets | Never followed or created |
| Encryption | Encrypted entries and headers are detected and reported | Decryption |
Inside a RAR file
Both formats are a signature followed by blocks, each with its own header and checksum. RAR 4 headers are fixed little-endian fields; RAR 5 headers use variable-length numbers. A file's packed data follows its header.
The read path, and where the limits apply
Volumes and solid archives
Limits and path checks
- Limits the caller sets.
Limitsbounds the decoder's memory (dictionary, PPMd model and filters: a RAR 5 file can ask for gigabytes of dictionary), the size of one file, the total written, the compression ratio, the number of entries and volumes, and how far to search for an archive after a self-extractor's program. The defaults are meant for untrusted archives. - Never past the declared size. A file's output stops at the size its header declares.
- Names checked. The path check refuses
.., absolute paths, drive letters, Windows device names (CON,NUL,COM1and the others) and alternate data streams. - Links listed, never made. Symbolic links, junctions, hard links and file copies are reported with their targets; they are never followed or created.
- The command line tool writes only under the target folder, never follows a link already on disk, writes each file under a temporary name and keeps it only once its checksum matches.
How it was checked
- Tests on every change. 38 unit tests and 5 documentation tests on invented data (bit readers, both Huffman code builders, the PPMd model, every filter, header numbers and names, CRC-32 and BLAKE2s against RFC 7693's vector, the path check, volume names), and 17 archive-level tests on 114 of libarchive's own test archives, run on Linux, Windows and macOS: every feature with a test archive, every typed refusal, every limit, every damaged archive in libarchive's set, and for eight archives every truncation and 3,000 corruptions each (24,000 in all), all without a panic.
- Byte for byte against bsdtar and 7-Zip. 101 archives (libarchive's test archives, including its two self-extracting ones, and nine RAR archives of game mods published online, five RAR 4 and four RAR 5, the largest 182 MB unpacked), 226 files: 202 equal to bsdtar 3.8.10, 220 equal to 7-Zip 25.01, none different. The rest are files bsdtar's command line cannot read (RAR 4 volume sets) or that rar-reader refuses (encrypted or damaged entries).
- Fuzzing. Six cargo-fuzz targets, built with debug assertions and overflow checks, seeded with libarchive's test archives. Each ran twice, the second time on the present code, about 115 million runs in all, with no finding.
| Fuzz target | What it drives | Minutes | Runs |
|---|---|---|---|
archive | the whole reader: open, then extract every entry forwards and backwards | 80 | 1.6 million |
headers | the header parsers, also behind a program stub and inside a larger stream | 60 | 79 million |
rar4_lz | the RAR 4 decoder: LZSS, PPMd blocks and filters | 90 | 5.5 million |
ppmd | the PPMd model with RAR's range decoder | 90 | 2.4 million |
rar5 | the RAR 5 decoder, also several files of one solid stream | 90 | 5.8 million |
filters | each RAR 4 filter, filter code, Huffman tables, name decoding | 60 | 21 million |
The RAR 4 x86 filter without E9 is ported and unit-tested, but no test archive uses it.
Speed and memory
About as fast as bsdtar, the C code it was ported from, with less memory; 7-Zip is faster on some archives, most of all on RAR 5. One thread of an AMD Ryzen AI 9 HX 370, median of seven runs. "Decode" checks every file without writing it; "extract" writes the files to a RAM disk.
| Archive | Unpacked | rar-reader decode / extract | bsdtar 3.8.10 | 7-Zip 25.01 |
|---|---|---|---|---|
| RAR 4, LZSS with delta and audio filters | 142 MB | 0.49 s / 0.54 s | 0.50 s / 0.50 s | 0.45 s / 0.42 s |
| RAR 4, LZSS with delta and audio filters | 139 MB | 0.71 s / 0.77 s | 0.72 s / 0.81 s | 0.53 s / 0.62 s |
| RAR 4, LZSS with delta, RGB and audio filters | 27 MB | 0.21 s / 0.22 s | 0.19 s / 0.19 s | 0.18 s / 0.19 s |
| RAR 4, PPMd and LZSS blocks (a libarchive test archive) | 242 MB | 0.12 s / 0.16 s | 0.09 s / 0.13 s | 0.13 s / 0.18 s |
| RAR 5, LZSS with delta filters | 182 MB | 0.29 s / 0.38 s | 0.32 s / 0.35 s | 0.15 s / 0.14 s |
Peak memory: 7 MB for the RAR 4 archives (bsdtar 12 to 16 MB, 7-Zip 11 to 15 MB), and 35 MB for the RAR 5 one, whose header asks for a 32 MiB dictionary (bsdtar 40 MB, 7-Zip 39 MB). Absolute times varied between sessions on this desktop, so compare the columns of one row.
Use it
cargo add rar-readerRust
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut archive = rar_reader::Archive::open_path("mod.rar", rar_reader::Limits::default())?;
for index in 0..archive.entries().len() {
let entry = &archive.entries()[index];
if !entry.is_file() {
continue;
}
let path = entry.sanitized_path()?; // refuses "../x", "/x", "C:x", "NUL", "x:stream"
let mut out = File::create(path.replace('/', "_"))?;
archive.extract(index, &mut out)?; // checksum verified; nothing past the declared size
}
Ok(())
}The command line tool
cargo install rar-reader-cliShell
rar-reader list [--json] mod.rar
rar-reader test [--json] mod.part1.rar
rar-reader extract [--json] [--overwrite] mod.rar target-dirlist shows every file, folder and link with sizes, method, date and checksum; test decodes every file of an archive or volume set and checks it, writing nothing; extract writes into a folder with the checks above. --json is for launchers, mod managers and scripts. Ready-built binaries for Linux (x64, ARM64), Windows (x64, ARM64) and macOS (Apple silicon, Intel), with checksums, are on the release page; the library is rar-reader and the tool rar-reader-cli on crates.io.
Provenance and licence
rar-reader is licensed under the 2-clause BSD license, the license of the code it was ported from. Its RAR 4 reader is ported from libarchive's archive_read_support_format_rar.c (Tim Kientzle, Andres Mejia) and its RAR 5 reader from archive_read_support_format_rar5.c (Grzegorz Antoniak), at libarchive 3.8.11; its PPMd model from libarchive's port of Igor Pavlov's public domain PPMd variant H, based on Dmitry Shkarin's; BLAKE2s is written from RFC 7693. Each ported file keeps libarchive's copyright notice. 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. The 2-clause BSD license is compatible with the GNU GPL, so GPL-3.0-only Battlefront Eternal can use it.
rar-reader is an independent project. It is not affiliated with or endorsed by RARLAB or win.rar GmbH, or by the libarchive project. RAR and WinRAR are trademarks of win.rar GmbH. rar-reader only reads RAR archives and cannot create them.