Download

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

PartReadNot read
RAR 1.5 to 4.x containerMain, file, comment, recovery and other blocks; Unicode and code page file names; extended times; MS-DOS, Windows and Unix attributesEncrypted headers (reported as such)
RAR 4 stored and LZSS (RAR 2.9 to 4.x)YesRAR 1.5 and 2.x compression (unpack versions 15 to 26)
RAR 4 PPMd variant HYes, also switching with LZSS blocksFilters inside PPMd blocks
RAR 4 filtersDelta, x86 E8, x86 E8/E9, RGB, audioAny other filter program
RAR 4 solid archivesThe first file of a solid streamFiles that continue a solid stream (libarchive does not read them either)
RAR 5 containerMain, file, service and end blocks; UTF-8 names; Unix and Windows times; file versions; attributesEncrypted headers (reported as such)
RAR 5 stored and LZSSCompression version 0, dictionaries up to 4 GiB within the memory limitRAR 7's compression version 1; files of unknown size
RAR 5 filtersDelta, x86 E8, x86 E8/E9, ARM
RAR 5 solid archivesYes, entries in any order
ChecksumsCRC-32 (RAR 4 and 5) and BLAKE2sp (RAR 5), verified on every extraction
Multi-volume setsRAR 4 (.rar, .r00, .r01, … and .partN.rar) and RAR 5 (.partN.rar), files split across volumes
Self-extracting archivesThe archive after a Windows or ELF program stub, which is skipped and never run
LinksSymbolic links, junctions, hard links and RAR 5 file copies, listed with their targetsNever followed or created
EncryptionEncrypted entries and headers are detected and reportedDecryption
Refused plainly: encrypted archives are detected and reported, not opened (there is no password support in this version); RAR 1.5 and 2.x compression, files that continue a RAR 4 solid stream and RAR 7's new compression method are refused with their own errors. libarchive does not read these either.

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 RAR 4 and RAR 5 container layoutsA RAR 4 archive starts with the seven-byte marker Rar! 1A 07 00, then a main header, then for each file a file header followed by its packed data, then other blocks and an end block. Each RAR 4 block header holds a CRC-16, a type, flags and a size. A RAR 5 archive starts with the eight-byte signature Rar! 1A 07 01 00, then a main header, file headers each followed by a data area, service headers, and an end header. Each RAR 5 header holds a CRC-32, a size, a type, flags and optional extra and data sizes, as variable-length numbers.RAR 1.5 to 4.xMarkerRar! 1A 07 00Mainarchive flagsFile headername, sizesPacked dataLZSS or PPMdFile headerData… Endrecovery, commentEvery block header: CRC-16, type, flags, size (fixed little-endian fields)RAR 5SignatureRar! 1A 07 01 00MainflagsFile headername, timesData areaLZSSFile headerData areaService, Endcomment, …Every header: CRC-32, then size, type, flags and optional extra and data sizesas variable-length numbers; the extra area carries times, versions and links
A self-extracting archive is the same container after a Windows or ELF program, which the reader skips and never runs.

The read path, and where the limits apply

How a file is readOpening an archive finds the signature, skipping a self-extractor's program within a search limit, and reads every header, within the limits on entries and volumes. Extracting a file runs the decompressor of its family, stored, LZSS or PPMd, within the memory limit for the dictionary and model; then the filters, delta, x86, ARM, RGB or audio; then the CRC-32 or BLAKE2sp checksum; then the output, never past the file's declared size and within the limits on one file, the total written and the compression ratio. The command line tool also checks every name and keeps a file only once its checksum matches.Openfind the signatureRead all headersnames, sizes, flagsDecompressstored, LZSS (RAR 4 and 5) or PPMd (RAR 4)Filtersdelta, x86, ARM, RGB, audioChecksumCRC-32 or BLAKE2spOutputnever past the declared sizelimit: program searchlimits: entries, volumeslimit: dictionary and model memorylimits: one file, total written, ratiocommand line: name check, temporary name,kept only once the checksum matches

Volumes and solid archives

Multi-volume sets and solid archivesA volume set is several files read as one archive, part1, part2, part3 or name.rar, name.r00, name.r01; a file may start in one volume and continue in the next. In a solid archive the files share one compression stream: file C can only be decoded after A and B. A RAR 5 solid archive can be extracted in any order, and in archive order each file is decoded once. In a RAR 4 solid archive only the first file of a stream is read.A volume setmod.part1.rarfile 1, start of file 2mod.part2.rarrest of file 2, file 3mod.part3.rarfile 4The older naming works too: mod.rar, mod.r00, mod.r01, … (RAR 4)A solid archiveFile AFile BFile Cone compression stream: C needs A and Bdecoded firstRAR 5: any order, each file decoded once in archive order. RAR 4: the first file of a stream only.

Limits and path checks

  • Limits the caller sets. Limits bounds 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, COM1 and 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 targetWhat it drivesMinutesRuns
archivethe whole reader: open, then extract every entry forwards and backwards801.6 million
headersthe header parsers, also behind a program stub and inside a larger stream6079 million
rar4_lzthe RAR 4 decoder: LZSS, PPMd blocks and filters905.5 million
ppmdthe PPMd model with RAR's range decoder902.4 million
rar5the RAR 5 decoder, also several files of one solid stream905.8 million
filterseach RAR 4 filter, filter code, Huffman tables, name decoding6021 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.

ArchiveUnpackedrar-reader decode / extractbsdtar 3.8.107-Zip 25.01
RAR 4, LZSS with delta and audio filters142 MB0.49 s / 0.54 s0.50 s / 0.50 s0.45 s / 0.42 s
RAR 4, LZSS with delta and audio filters139 MB0.71 s / 0.77 s0.72 s / 0.81 s0.53 s / 0.62 s
RAR 4, LZSS with delta, RGB and audio filters27 MB0.21 s / 0.22 s0.19 s / 0.19 s0.18 s / 0.19 s
RAR 4, PPMd and LZSS blocks (a libarchive test archive)242 MB0.12 s / 0.16 s0.09 s / 0.13 s0.13 s / 0.18 s
RAR 5, LZSS with delta filters182 MB0.29 s / 0.38 s0.32 s / 0.35 s0.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-reader

Rust

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-cli

Shell

rar-reader list [--json] mod.rar
rar-reader test [--json] mod.part1.rar
rar-reader extract [--json] [--overwrite] mod.rar target-dir

list 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.