Introduction
LZMA is the compression method Igor Pavlov created for
7-Zip’s 7z archives.
An .lzma file is a single file compressed with it, in the simple container
from his LZMA SDK, known as LZMA_Alone. LZMA Utils made it popular on Linux for a few
years, but the container has no magic number and no integrity check, so it was replaced
by xz from 2009. Old .tar.lzma files and
some firmware images still use it.
Structure
Numbers are little-endian.
0Properties lc, lp and pb packed into one byte as (pb × 5 + lp) × 9 + lc1 byte1Dictionary size4 bytes5Uncompressed size or all ones if unknown8 bytes13LZMA datathe rest
With the usual settings (lc 3, lp 0, pb 2) the first byte is 5D, which is
the nearest thing the format has to a signature. When the size is unknown, the data ends
with an end marker instead. The header
and LZMA coding pages give the details.
Scope and the explorer
The header and the decoding procedures on the pages cover everything lzma-specification.txt from
the LZMA SDK and lzma-file-format.txt from the xz project define for the .lzma file. Not part of the format: the
encoder side (match finders, price tables, the optimal parser) and the decoder’s partial-decoding mode.
The specification’s two informative sections lie outside the .lzma file too: the
one-byte dictionary-size scheme that LZMA2 uses (see XZ blocks) and
the RAM usage (1846 + (768 << (lc + lp)) probabilities).
Not part of the format: a bare LZMA stream with no 13-byte header, as inside a
7z archive or a firmware image, is not an .lzma file (it has no
signature, and its properties are stored elsewhere); the viewer shows such streams inside the files that carry
them.
Large streams: the stream is decoded to its end when the file is opened, through a window that holds only the dictionary, however much output it makes, so where it ends, whether it is sound and whether bytes follow it are found for a stream of any size (there is no cap on the output; a header that claims more than the data holds stops where the data runs out). A dictionary above 1 GiB, which a valid file can declare (the field goes up to 232 − 1), is not a limit either: the decoder holds at most 1 GiB of window, and a match that reaches back further is answered by decoding that part of the stream again from its start, which is slow for data that really uses such distances and never wrong. The data opens as a windowed file of any size, an unknown-size stream being decoded once to find its size.
Problems: every problem a file has is kept, with its true count. The problems list shows the first 500 and pages through the rest, 200 rows at a time. The same viewer reads .xz files, whose LZMA2 chunks may not hold an LZMA end-of-payload marker: one that does is named as such.
Which headers xz accepts: xz’s own test (is_format_lzma in src/xz/coder.c, used
even with --format=lzma) refuses a file as “File format not recognized” when its properties give lc + lp above
4 (lzma_properties_decode allows at most 4), when its dictionary size does not pass the test’s bit check, or when
its uncompressed size is over 238 bytes. Each is a problem that says so; the format and the LZMA SDK decoder allow
all three. The viewer copies all three parts of xz’s test and checks each against the installed xz.
Links
Software
Developers
Reference
- LZMA SDK – includes
lzma-specification.txt, the description of LZMA and this header