LZMA is LZ77 (literals and copies from a sliding window) whose decisions and values
are all coded one bit at a time by an adaptive binary range coder. This page
summarises the decoder in
lzma-specification.txt
from the LZMA SDK, read 2026-11-13; the
same coding is inside LZMA2,
7z and lzip.
Range decoder
- Start:
Range = FFFFFFFF; read 5 bytes intoCode. The first byte must be 0, andCodeequal toRangemarks the stream as corrupted; in an undamaged streamCodeis always less thanRange. - Normalise: whenever
Rangefalls below 224, shiftRangeandCodeleft 8 bits and read the next byte intoCode. - Probability bits: each probability is an 11-bit number (out of 2,048) that the
next bit is 0, starting at 1,024.
bound = (Range >> 11) × prob. IfCode < boundthe bit is 0:Range = bound,prob += (2048 − prob) >> 5. Otherwise it is 1:Range −= bound,Code −= bound,prob −= prob >> 5. - Direct bits, with probability one half:
Range >>= 1; the bit is 1 ifCode ≥ Range, which is then subtracted. - A bit tree of n bits decodes a number from the top bit down, using 2n probabilities indexed by the bits read so far; a reverse bit tree reads the low bit first.
Procedures, from the specification’s code. Initialisation:
Range = 0xFFFFFFFF, Code = 0; read a byte b, then four
bytes shifted into Code (big-endian); b ≠ 0 or Code == Range marks
corruption, and a first byte other than 0 must stop the decoder. The reference decoder ignores the
“corrupted” flag, and compatible decoders must too. Normalise: if
Range < 224 then Range <<= 8 and
Code = (Code << 8) | nextByte (32 bits kept). Direct bits, for
n bits: repeat Range >>= 1; Code −= Range; t = 0 − (Code >> 31)
(all ones if Code went negative); Code += Range & t; if
Code == Range flag corruption; normalise; result = (result << 1) + t + 1.
End of data: after the last bit Code must be 0
(IsFinishedOK). Probabilities are stored in 16-bit integers; every probability starts at
(1 << 11) / 2 = 1,024.
Bit trees. A tree for n-bit symbols has an array of
2n probabilities; index 0 is unused. Normal decode:
m = 1; repeat n times: m = (m << 1) + DecodeBit(Probs[m]); result = m − 2n.
Reverse decode: m = 1; symbol = 0; for i in 0..n−1: bit = DecodeBit(Probs[m]); m = (m << 1) + bit; symbol |= bit << i.
Decoding modes. (1) Unpack size unknown: decoding ends at the end marker, which is mandatory.
(2) Size known, marker optional: the decoder must detect either way of ending. At the top of each loop
iteration, if the size is known, the remaining unpack size is 0 and the marker is not mandatory, the decoder
tests Code: if it is 0 the stream ends here without a marker; if it is not 0 the decoder carries on
decoding and a marker must follow. (3) Size known and the marker
mandatory. With a known size, a literal, match or short repeat that would exceed the size is an error, and a match is
truncated to the size and then reported as an error. The initial state is 0 and rep0 = rep1 = rep2 = rep3 = 0
(zero-based distances).
Packets
The decoder keeps the position pos, the state (0 to 11), the previous byte
and four repeat distances rep0 to rep3.
posState = pos & ((1 << pb) − 1). Each packet starts with
decision bits:
| Bits | Packet | Meaning |
|---|---|---|
0 | LIT | A literal byte. |
1 0 | MATCH | A length and a new distance. |
1 1 0 0 | SHORTREP | One byte from distance rep0. |
1 1 0 1 | LONGREP[0] | A length, distance rep0. |
1 1 1 0 | LONGREP[1] | A length, distance rep1. |
1 1 1 1 0 | LONGREP[2] | A length, distance rep2. |
1 1 1 1 1 | LONGREP[3] | A length, distance rep3. |
The bits come from these probability arrays: IsMatch[state][posState],
IsRep[state], IsRepG0[state],
IsRep0Long[state][posState], IsRepG1[state] and
IsRepG2[state]. A repeat moves the distance it used to rep0
and shifts the others down; a MATCH pushes its new distance in as rep0
and drops rep3.
States
The state remembers the kinds of the last few packets. States 0 to 6 follow a literal; 7 to 11 follow a match or repeat.
| After | New state |
|---|---|
| LIT | 0 if state < 4; state − 3 if state < 10; else state − 6 |
| MATCH | 7 if state < 7, else 10 |
| LONGREP | 8 if state < 7, else 11 |
| SHORTREP | 9 if state < 7, else 11 |
Literals
There are 0x300 << (lc + lp) literal probabilities, 0x300 for each
context. The context is
((pos & ((1 << lp) − 1)) << lc) + (prevByte >> (8 − lc)).
In states below 7 the byte is an 8-bit tree. From state 7 up (just after a match) it
is a “matched literal”: the byte at distance rep0 is used as a guide, and
while its bits agree with the bits decoded, a separate set of probabilities chosen by
the guide bit is used. (The specification’s prose says “state > 7”, but its
decoder code tests state >= 7.)
The procedure, from the specification: prevByte is the previous output byte (0 at the
start); probs = LitProbs + 0x300 × litState; symbol = 1. If
state ≥ 7, matchByte is the output byte rep0 + 1 back; loop: take
matchBit = (matchByte >> 7) & 1 and shift matchByte left by one; decode
bit with probs[((1 + matchBit) << 8) + symbol];
symbol = (symbol << 1) | bit; leave the loop as soon as matchBit ≠ bit, or when
symbol ≥ 0x100. Then, while symbol < 0x100:
symbol = (symbol << 1) | DecodeBit(probs[symbol]). The byte is
symbol − 0x100. After a literal the state becomes 0 if it was below 4, state − 3
if below 10, otherwise state − 6. A literal needs the output size not to be exhausted.
Lengths
MATCH and LONGREP use two length decoders of the same shape, one each. Lengths run from 2 to 273:
| Bits | Length | Coded as |
|---|---|---|
0 | 2 to 9 | 3-bit tree, one per posState (low) |
1 0 | 10 to 17 | 3-bit tree, one per posState (mid) |
1 1 | 18 to 273 | 8-bit tree, shared (high) |
Each length decoder (CLenDecoder) holds two single probabilities, Choice (the
first bit) and Choice2 (the second bit, after a first bit of 1), 16 low trees
(LowCoder[1 << kNumPosBitsMax], kNumPosBitsMax = 4, so one per
posState up to 15), 16 mid trees and one high tree; the trees are 3-bit, 3-bit and 8-bit
(8, 8 and 256 probabilities). The decoded value is 0 to 271 (0 + xxx, 8 + yyy
or 16 + zzzzzzzz), and the real length is that plus kMatchMinLen (2). The decoder
has two instances: LenDecoder for MATCH and RepLenDecoder for the long repeats.
The array sizes: IsMatch[12 << 4], IsRep0Long[12 << 4],
IsRep[12], IsRepG0[12], IsRepG1[12], IsRepG2[12]; the
index into the first two is (state << 4) + posState. The selection flow:
IsMatch 0 literal; 1 then IsRep 0 simple match; IsRep 1 then
IsRepG0 0 (rep0; then IsRep0Long 0 short repeat, 1 long repeat 0); IsRepG0 1 then
IsRepG1 0 (rep1), 1 then IsRepG2 0 (rep2), 1 (rep3). A repeat match requires a non-empty
window.
Distances
lenState = min(len − 2, 3). Decode a 6-bit tree fromPosSlot[lenState]: the distance slot, 0 to 63.- Slots 0 to 3 are the distance itself.
- Otherwise
numDirectBits = (slot >> 1) − 1and the distance starts as(2 | (slot & 1)) << numDirectBits. - Slots 4 to 13: the low
numDirectBitsbits are a reverse bit tree in theSpecPosprobabilities. - Slots 14 to 63:
numDirectBits − 4direct bits, then the low 4 bits as a reverse 4-bit tree in theAlignprobabilities.
The decoded value is the distance minus 1. A value of FFFFFFFF is the
end marker (the real distance 232; the largest real distance is 232 − 1).
Otherwise it must be smaller than the data decoded so far and the
dictionary size. After the marker the decoder checks Code == 0.
Distance probabilities: PosSlotDecoder[4], four 6-bit trees (64 probabilities each);
PosDecoders[1 + kNumFullDistances − kEndPosModelIndex] = 1 + 128 − 14 = 115 probabilities,
shared by the reverse trees of slots 4 to 13 and addressed as PosDecoders + dist − posSlot
where dist is the starting distance of step 3; and one 4-bit AlignDecoder tree (16
probabilities, used reversed). The step 5 direct bits are
DecodeDirectBits(numDirectBits − 4) << 4. After a simple MATCH the history is shifted
(rep3 = rep2; rep2 = rep1; rep1 = rep0) before the new rep0 is decoded. For a repeat:
rep0 and short repeat leave the history alone; rep1 swaps rep1 and rep0; rep2 moves rep2 to rep0 and shifts rep1
and rep0 down; rep3 does the same from rep3. New states: simple match 7 (below 7) or 10, long repeat 8 or 11,
short repeat 9 or 11. A copy is len + 2 bytes from rep0 + 1 back.
Constants
| Name | Value |
|---|---|
kNumBitModelTotalBits | 11 (probabilities out of 2,048) |
kNumMoveBits | 5 |
kNumStates | 12 |
kMatchMinLen | 2 |
kEndPosModelIndex | 14 |
kNumFullDistances | 128 |
kNumAlignBits | 4 |
kNumLenToPosStates | 4 |