How Unrar Handles Non-Standard Block Sizes
This article provides an overview of how the unrar
utility processes archives configured with non-standard block sizes,
detailing its internal parsing logic, dynamic buffer allocation, and
error-handling routines. When confronted with non-standard data blocks
or compression dictionaries, unrar relies on metadata
stored in the archive headers to adapt its extraction pipeline, ensuring
data integrity while guarding against memory exhaustion and format
violations.
Dynamic Block Header Parsing
The RAR archive format (across both RAR4 and RAR5 specifications) does not mandate a single, rigid block size for file contents or headers. Instead, it relies on structured data blocks where each header explicitly declares its own size and the size of the payload that follows. In RAR5, these lengths are encoded as variable-length integers (LEB128).
Because of this design, unrar does not assume a fixed,
standard block size during decompression. When it encounters a block
with an unconventional size, it reads the explicit length field from the
header, updates its internal file pointer, and processes the payload
according to the defined byte boundary.
Sliding Window and Buffer Allocation
Block sizes in RAR archives often relate to compression dictionaries
and sliding window parameters. When an archive is created with
non-standard dictionary sizes, unrar evaluates the
compression flags within the file header before initiating
decompression:
- Header Inspection:
unrarreads the dictionary size exponent or discrete byte value defined in the header. - Buffer Allocation: It dynamically allocates a sliding window memory buffer matching that specific size.
- Circular Buffer Streaming: During extraction, uncompressed data streams through this circular buffer. The unpack engine references previously decoded bytes within this window regardless of whether the chunk sizes align with standard 4 MB, 32 MB, or custom-sized intervals.
Limitations and Out-of-Bounds Handling
While unrar flexibly handles non-standard block sizes
within valid operational limits, it enforces strict boundaries to
prevent memory allocation exploits and buffer overflows:
- Maximum Dictionary Thresholds: In modern RAR5
specifications, dictionary sizes can scale up to 1 GB (or 4 GB in
specialized implementations). If a block specifies a dictionary size
that exceeds the maximum supported threshold of the specific
unrarbinary version, the utility terminates extraction and returns an "unsupported format" or "unknown compression method" error. - Corrupted or Manipulated Sizes: If a block header
specifies a size that exceeds the remaining physical size of the archive
file,
unrarflags the archive as damaged or truncated. - Integrity Validation: Every data block is verified
against a checksum (CRC32 for older formats, or BLAKE2sp for RAR5). If
non-standard block sizes cause misaligned byte streams, the checksum
verification fails, prompting
unrarto issue a checksum mismatch error and discard or warn about the extracted file.