| 1 | LZ4 Block Format Description |
| 2 | ============================ |
| 3 | Last revised: 2015-05-07. |
| 4 | Author : Yann Collet |
| 5 | |
| 6 | |
| 7 | This specification is intended for developers |
| 8 | willing to produce LZ4-compatible compressed data blocks |
| 9 | using any programming language. |
| 10 | |
| 11 | LZ4 is an LZ77-type compressor with a fixed, byte-oriented encoding. |
| 12 | There is no entropy encoder back-end nor framing layer. |
| 13 | The latter is assumed to be handled by other parts of the system (see [LZ4 Frame format]). |
| 14 | This design is assumed to favor simplicity and speed. |
| 15 | It helps later on for optimizations, compactness, and features. |
| 16 | |
| 17 | This document describes only the block format, |
| 18 | not how the compressor nor decompressor actually work. |
| 19 | The correctness of the decompressor should not depend |
| 20 | on implementation details of the compressor, and vice versa. |
| 21 | |
| 22 | [LZ4 Frame format]: lz4_Frame_format.md |
| 23 | |
| 24 | |
| 25 | |
| 26 | Compressed block format |
| 27 | ----------------------- |
| 28 | An LZ4 compressed block is composed of sequences. |
| 29 | A sequence is a suite of literals (not-compressed bytes), |
| 30 | followed by a match copy. |
| 31 | |
| 32 | Each sequence starts with a token. |
| 33 | The token is a one byte value, separated into two 4-bits fields. |
| 34 | Therefore each field ranges from 0 to 15. |
| 35 | |
| 36 | |
| 37 | The first field uses the 4 high-bits of the token. |
| 38 | It provides the length of literals to follow. |
| 39 | |
| 40 | If the field value is 0, then there is no literal. |
| 41 | If it is 15, then we need to add some more bytes to indicate the full length. |
| 42 | Each additional byte then represent a value from 0 to 255, |
| 43 | which is added to the previous value to produce a total length. |
| 44 | When the byte value is 255, another byte is output. |
| 45 | There can be any number of bytes following the token. There is no "size limit". |
| 46 | (Side note : this is why a not-compressible input block is expanded by 0.4%). |
| 47 | |
| 48 | Example 1 : A length of 48 will be represented as : |
| 49 | |
| 50 | - 15 : value for the 4-bits High field |
| 51 | - 33 : (=48-15) remaining length to reach 48 |
| 52 | |
| 53 | Example 2 : A length of 280 will be represented as : |
| 54 | |
| 55 | - 15 : value for the 4-bits High field |
| 56 | - 255 : following byte is maxed, since 280-15 >= 255 |
| 57 | - 10 : (=280 - 15 - 255) ) remaining length to reach 280 |
| 58 | |
| 59 | Example 3 : A length of 15 will be represented as : |
| 60 | |
| 61 | - 15 : value for the 4-bits High field |
| 62 | - 0 : (=15-15) yes, the zero must be output |
| 63 | |
| 64 | Following the token and optional length bytes, are the literals themselves. |
| 65 | They are exactly as numerous as previously decoded (length of literals). |
| 66 | It's possible that there are zero literal. |
| 67 | |
| 68 | |
| 69 | Following the literals is the match copy operation. |
| 70 | |
| 71 | It starts by the offset. |
| 72 | This is a 2 bytes value, in little endian format |
| 73 | (the 1st byte is the "low" byte, the 2nd one is the "high" byte). |
| 74 | |
| 75 | The offset represents the position of the match to be copied from. |
| 76 | 1 means "current position - 1 byte". |
| 77 | The maximum offset value is 65535, 65536 cannot be coded. |
| 78 | Note that 0 is an invalid value, not used. |
| 79 | |
| 80 | Then we need to extract the match length. |
| 81 | For this, we use the second token field, the low 4-bits. |
| 82 | Value, obviously, ranges from 0 to 15. |
| 83 | However here, 0 means that the copy operation will be minimal. |
| 84 | The minimum length of a match, called minmatch, is 4. |
| 85 | As a consequence, a 0 value means 4 bytes, and a value of 15 means 19+ bytes. |
| 86 | Similar to literal length, on reaching the highest possible value (15), |
| 87 | we output additional bytes, one at a time, with values ranging from 0 to 255. |
| 88 | They are added to total to provide the final match length. |
| 89 | A 255 value means there is another byte to read and add. |
| 90 | There is no limit to the number of optional bytes that can be output this way. |
| 91 | (This points towards a maximum achievable compression ratio of about 250). |
| 92 | |
| 93 | With the offset and the matchlength, |
| 94 | the decoder can now proceed to copy the data from the already decoded buffer. |
| 95 | On decoding the matchlength, we reach the end of the compressed sequence, |
| 96 | and therefore start another one. |
| 97 | |
| 98 | |
| 99 | Parsing restrictions |
| 100 | ----------------------- |
| 101 | There are specific parsing rules to respect in order to remain compatible |
| 102 | with assumptions made by the decoder : |
| 103 | |
| 104 | 1. The last 5 bytes are always literals |
| 105 | 2. The last match must start at least 12 bytes before end of block. |
| 106 | Consequently, a block with less than 13 bytes cannot be compressed. |
| 107 | |
| 108 | These rules are in place to ensure that the decoder |
| 109 | will never read beyond the input buffer, nor write beyond the output buffer. |
| 110 | |
| 111 | Note that the last sequence is also incomplete, |
| 112 | and stops right after literals. |
| 113 | |
| 114 | |
| 115 | Additional notes |
| 116 | ----------------------- |
| 117 | There is no assumption nor limits to the way the compressor |
| 118 | searches and selects matches within the source data block. |
| 119 | It could be a fast scan, a multi-probe, a full search using BST, |
| 120 | standard hash chains or MMC, well whatever. |
| 121 | |
| 122 | Advanced parsing strategies can also be implemented, such as lazy match, |
| 123 | or full optimal parsing. |
| 124 | |
| 125 | All these trade-off offer distinctive speed/memory/compression advantages. |
| 126 | Whatever the method used by the compressor, its result will be decodable |
| 127 | by any LZ4 decoder if it follows the format specification described above. |