2015-03-26 18:58:19 +00:00
|
|
|
LZ4 Block Format Description
|
|
|
|
============================
|
2019-04-11 20:55:35 +00:00
|
|
|
Last revised: 2019-03-30.
|
2015-03-26 18:58:19 +00:00
|
|
|
Author : Yann Collet
|
2014-01-07 18:47:50 +00:00
|
|
|
|
|
|
|
|
2015-05-07 12:00:21 +00:00
|
|
|
This specification is intended for developers
|
|
|
|
willing to produce LZ4-compatible compressed data blocks
|
2014-01-07 18:47:50 +00:00
|
|
|
using any programming language.
|
|
|
|
|
|
|
|
LZ4 is an LZ77-type compressor with a fixed, byte-oriented encoding.
|
2015-03-26 18:58:19 +00:00
|
|
|
There is no entropy encoder back-end nor framing layer.
|
2019-01-02 22:36:12 +00:00
|
|
|
The latter is assumed to be handled by other parts of the system
|
|
|
|
(see [LZ4 Frame format]).
|
2015-05-07 12:00:21 +00:00
|
|
|
This design is assumed to favor simplicity and speed.
|
|
|
|
It helps later on for optimizations, compactness, and features.
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2015-05-07 12:00:21 +00:00
|
|
|
This document describes only the block format,
|
|
|
|
not how the compressor nor decompressor actually work.
|
2014-01-07 18:47:50 +00:00
|
|
|
The correctness of the decompressor should not depend
|
|
|
|
on implementation details of the compressor, and vice versa.
|
|
|
|
|
2015-10-16 12:25:24 +00:00
|
|
|
[LZ4 Frame format]: lz4_Frame_format.md
|
2015-05-07 12:00:21 +00:00
|
|
|
|
2014-01-07 18:47:50 +00:00
|
|
|
|
|
|
|
|
2015-03-26 18:58:19 +00:00
|
|
|
Compressed block format
|
|
|
|
-----------------------
|
2014-01-07 18:47:50 +00:00
|
|
|
An LZ4 compressed block is composed of sequences.
|
2015-03-30 17:32:21 +00:00
|
|
|
A sequence is a suite of literals (not-compressed bytes),
|
|
|
|
followed by a match copy.
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
Each sequence starts with a `token`.
|
|
|
|
The `token` is a one byte value, separated into two 4-bits fields.
|
2014-01-07 18:47:50 +00:00
|
|
|
Therefore each field ranges from 0 to 15.
|
|
|
|
|
|
|
|
|
|
|
|
The first field uses the 4 high-bits of the token.
|
|
|
|
It provides the length of literals to follow.
|
2015-03-30 17:32:21 +00:00
|
|
|
|
2014-01-07 18:47:50 +00:00
|
|
|
If the field value is 0, then there is no literal.
|
|
|
|
If it is 15, then we need to add some more bytes to indicate the full length.
|
2015-03-30 17:32:21 +00:00
|
|
|
Each additional byte then represent a value from 0 to 255,
|
2014-01-07 18:47:50 +00:00
|
|
|
which is added to the previous value to produce a total length.
|
|
|
|
When the byte value is 255, another byte is output.
|
2018-04-25 13:42:57 +00:00
|
|
|
There can be any number of bytes following `token`. There is no "size limit".
|
2015-03-30 17:32:21 +00:00
|
|
|
(Side note : this is why a not-compressible input block is expanded by 0.4%).
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
Example 1 : A literal length of 48 will be represented as :
|
2015-05-06 19:57:35 +00:00
|
|
|
|
|
|
|
- 15 : value for the 4-bits High field
|
|
|
|
- 33 : (=48-15) remaining length to reach 48
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
Example 2 : A literal length of 280 will be represented as :
|
2015-05-06 19:57:35 +00:00
|
|
|
|
|
|
|
- 15 : value for the 4-bits High field
|
|
|
|
- 255 : following byte is maxed, since 280-15 >= 255
|
|
|
|
- 10 : (=280 - 15 - 255) ) remaining length to reach 280
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
Example 3 : A literal length of 15 will be represented as :
|
2015-05-06 19:57:35 +00:00
|
|
|
|
|
|
|
- 15 : value for the 4-bits High field
|
|
|
|
- 0 : (=15-15) yes, the zero must be output
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
Following `token` and optional length bytes, are the literals themselves.
|
2014-01-07 18:47:50 +00:00
|
|
|
They are exactly as numerous as previously decoded (length of literals).
|
|
|
|
It's possible that there are zero literal.
|
|
|
|
|
|
|
|
|
|
|
|
Following the literals is the match copy operation.
|
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
It starts by the `offset`.
|
2015-03-30 17:32:21 +00:00
|
|
|
This is a 2 bytes value, in little endian format
|
|
|
|
(the 1st byte is the "low" byte, the 2nd one is the "high" byte).
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
The `offset` represents the position of the match to be copied from.
|
2014-01-07 18:47:50 +00:00
|
|
|
1 means "current position - 1 byte".
|
2018-04-25 13:42:57 +00:00
|
|
|
The maximum `offset` value is 65535, 65536 cannot be coded.
|
2016-02-12 22:34:07 +00:00
|
|
|
Note that 0 is an invalid value, not used.
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
Then we need to extract the `matchlength`.
|
2014-01-07 18:47:50 +00:00
|
|
|
For this, we use the second token field, the low 4-bits.
|
|
|
|
Value, obviously, ranges from 0 to 15.
|
|
|
|
However here, 0 means that the copy operation will be minimal.
|
2018-04-25 13:42:57 +00:00
|
|
|
The minimum length of a match, called `minmatch`, is 4.
|
2014-01-07 18:47:50 +00:00
|
|
|
As a consequence, a 0 value means 4 bytes, and a value of 15 means 19+ bytes.
|
2016-02-12 22:34:07 +00:00
|
|
|
Similar to literal length, on reaching the highest possible value (15),
|
2014-01-07 18:47:50 +00:00
|
|
|
we output additional bytes, one at a time, with values ranging from 0 to 255.
|
|
|
|
They are added to total to provide the final match length.
|
|
|
|
A 255 value means there is another byte to read and add.
|
|
|
|
There is no limit to the number of optional bytes that can be output this way.
|
2015-05-06 19:57:35 +00:00
|
|
|
(This points towards a maximum achievable compression ratio of about 250).
|
2014-01-07 18:47:50 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
Decoding the `matchlength` reaches the end of current sequence.
|
2017-06-06 18:20:36 +00:00
|
|
|
Next byte will be the start of another sequence.
|
|
|
|
But before moving to next sequence,
|
|
|
|
it's time to use the decoded match position and length.
|
2018-04-25 13:42:57 +00:00
|
|
|
The decoder copies `matchlength` bytes from match position to current position.
|
2017-06-06 18:20:36 +00:00
|
|
|
|
2018-04-25 13:42:57 +00:00
|
|
|
In some cases, `matchlength` is larger than `offset`.
|
|
|
|
Therefore, `match_pos + matchlength > current_pos`,
|
2017-06-06 18:20:36 +00:00
|
|
|
which means that later bytes to copy are not yet decoded.
|
|
|
|
This is called an "overlap match", and must be handled with special care.
|
2018-04-25 13:42:57 +00:00
|
|
|
A common case is an offset of 1,
|
|
|
|
meaning the last byte is repeated `matchlength` times.
|
2014-01-07 18:47:50 +00:00
|
|
|
|
|
|
|
|
2019-01-02 22:36:12 +00:00
|
|
|
End of block restrictions
|
2015-03-26 18:58:19 +00:00
|
|
|
-----------------------
|
2019-01-02 22:36:12 +00:00
|
|
|
There are specific rules required to terminate a block.
|
|
|
|
|
2019-04-11 20:55:35 +00:00
|
|
|
1. The last sequence contains only literals.
|
|
|
|
The block ends right after them.
|
|
|
|
2. The last 5 bytes of input are always literals.
|
|
|
|
Therefore, the last sequence contains at least 5 bytes.
|
|
|
|
- Special : if input is smaller than 5 bytes,
|
|
|
|
there is only one sequence, it contains the whole input as literals.
|
|
|
|
Empty input can be represented with a zero byte,
|
|
|
|
interpreted as a final token without literal and without a match.
|
|
|
|
3. The last match must start at least 12 bytes before the end of block.
|
2019-01-02 22:36:12 +00:00
|
|
|
The last match is part of the penultimate sequence.
|
2019-04-11 20:55:35 +00:00
|
|
|
It is followed by the last sequence, which contains only literals.
|
|
|
|
- Note that, as a consequence,
|
|
|
|
an independent block < 13 bytes cannot be compressed,
|
|
|
|
because the match must copy "something",
|
|
|
|
so it needs at least one prior byte.
|
|
|
|
- When a block can reference data from another block,
|
|
|
|
it can start immediately with a match and no literal,
|
|
|
|
so a block of 12 bytes can be compressed.
|
|
|
|
|
|
|
|
When a block does not respect these end conditions,
|
|
|
|
a conformant decoder is allowed to reject the block as incorrect.
|
|
|
|
|
|
|
|
These rules are in place to ensure that a conformant decoder
|
2019-01-02 22:36:12 +00:00
|
|
|
can be designed for speed, issuing speculatively instructions,
|
|
|
|
while never reading nor writing beyond provided I/O buffers.
|
2014-01-07 18:47:50 +00:00
|
|
|
|
|
|
|
|
2015-03-26 18:58:19 +00:00
|
|
|
Additional notes
|
|
|
|
-----------------------
|
2019-01-02 22:36:12 +00:00
|
|
|
If the decoder will decompress data from an external source,
|
|
|
|
it is recommended to ensure that the decoder will not be vulnerable to
|
|
|
|
buffer overflow manipulations.
|
|
|
|
Always ensure that read and write operations
|
|
|
|
remain within the limits of provided buffers.
|
|
|
|
Test the decoder with fuzzers
|
|
|
|
to ensure it's resilient to improbable combinations.
|
|
|
|
|
|
|
|
The format makes no assumption nor limits to the way the compressor
|
2014-01-07 18:47:50 +00:00
|
|
|
searches and selects matches within the source data block.
|
2019-01-02 22:36:12 +00:00
|
|
|
Multiple techniques can be considered,
|
|
|
|
featuring distinct time / performance trade offs.
|
|
|
|
As long as the format is respected,
|
|
|
|
the result will be compatible and decodable by any compliant decoder.
|
|
|
|
An upper compression limit can be reached,
|
|
|
|
using a technique called "full optimal parsing", at high cpu cost.
|