1 LZ4 Frame Format Description
2 ============================
6 Copyright (c) 2013-2015 Yann Collet
8 Permission is granted to copy and distribute this document
9 for any purpose and without charge,
10 including translations into other languages
11 and incorporation into compilations,
12 provided that the copyright notice and this notice are preserved,
13 and that any substantive changes or deletions from the original
15 Distribution of this document is unlimited.
25 The purpose of this document is to define a lossless compressed data format,
26 that is independent of CPU type, operating system,
27 file system and character set, suitable for
28 File compression, Pipe and streaming compression
29 using the [LZ4 algorithm](http://www.lz4.org).
31 The data can be produced or consumed,
32 even for an arbitrarily long sequentially presented input data stream,
33 using only an a priori bounded amount of intermediate storage,
34 and hence can be used in data communications.
35 The format uses the LZ4 compression method,
36 and optional [xxHash-32 checksum method](https://github.com/Cyan4973/xxHash),
37 for detection of data corruption.
39 The data format defined by this specification
40 does not attempt to allow random access to compressed data.
42 This specification is intended for use by implementers of software
43 to compress data into LZ4 format and/or decompress data from LZ4 format.
44 The text of the specification assumes a basic background in programming
45 at the level of bits and other primitive data representations.
47 Unless otherwise indicated below,
48 a compliant compressor must produce data sets
49 that conform to the specifications presented here.
50 It doesn’t need to support all options though.
52 A compliant decompressor must be able to decompress
53 at least one working set of parameters
54 that conforms to the specifications presented here.
55 It may also ignore checksums.
56 Whenever it does not support a specific parameter within the compressed stream,
57 it must produce a non-ambiguous error code
58 and associated error message explaining which parameter is unsupported.
61 General Structure of LZ4 Frame format
62 -------------------------------------
64 | MagicNb | F. Descriptor | Block | (...) | EndMark | C. Checksum |
65 |:-------:|:-------------:| ----- | ----- | ------- | ----------- |
66 | 4 bytes | 3-11 bytes | | | 4 bytes | 0-4 bytes |
70 4 Bytes, Little endian format.
75 3 to 11 Bytes, to be detailed in the next part.
76 Most important part of the spec.
80 To be detailed later on.
81 That’s where compressed data is stored.
85 The flow of blocks ends when the last data block has a size of “0”.
86 The size is expressed as a 32-bits value.
90 Content Checksum verify that the full content has been decoded correctly.
91 The content checksum is the result
92 of [xxh32() hash function](https://github.com/Cyan4973/xxHash)
93 digesting the original (decoded) data as input, and a seed of zero.
94 Content checksum is only present when its associated flag
95 is set in the frame descriptor.
96 Content Checksum validates the result,
97 that all blocks were fully transmitted in the correct order and without error,
98 and also that the encoding/decoding process itself generated no distortion.
99 Its usage is recommended.
101 __Frame Concatenation__
103 In some circumstances, it may be preferable to append multiple frames,
104 for example in order to add new data to an existing compressed file
105 without re-framing it.
107 In such case, each frame has its own set of descriptor flags.
108 Each frame is considered independent.
109 The only relation between frames is their sequential order.
111 The ability to decode multiple concatenated frames
112 within a single stream or file
113 is left outside of this specification.
114 As an example, the reference lz4 command line utility behavior is
115 to decode all concatenated frames in their sequential order.
121 | FLG | BD | (Content Size) | HC |
122 | ------- | ------- |:--------------:| ------- |
123 | 1 byte | 1 byte | 0 - 8 bytes | 1 byte |
125 The descriptor uses a minimum of 3 bytes,
126 and up to 11 bytes depending on optional parameters.
130 | BitNb | 7-6 | 5 | 4 | 3 | 2 | 1-0 |
131 | ------- | ------- | ------- | --------- | ------- | --------- | -------- |
132 |FieldName| Version | B.Indep | B.Checksum| C.Size | C.Checksum|*Reserved*|
137 | BitNb | 7 | 6-5-4 | 3-2-1-0 |
138 | ------- | -------- | ------------ | -------- |
139 |FieldName|*Reserved*| Block MaxSize|*Reserved*|
141 In the tables, bit 7 is highest bit, while bit 0 is lowest.
145 2-bits field, must be set to “01”.
146 Any other value cannot be decoded by this version of the specification.
147 Other version numbers will use different flag layouts.
149 __Block Independence flag__
151 If this flag is set to “1”, blocks are independent.
152 If this flag is set to “0”, each block depends on previous ones
153 (up to LZ4 window size, which is 64 KB).
154 In such case, it’s necessary to decode all blocks in sequence.
156 Block dependency improves compression ratio, especially for small blocks.
157 On the other hand, it makes direct jumps or multi-threaded decoding impossible.
159 __Block checksum flag__
161 If this flag is set, each data block will be followed by a 4-bytes checksum,
162 calculated by using the xxHash-32 algorithm on the raw (compressed) data block.
163 The intention is to detect data corruption (storage or transmission errors)
164 immediately, before decoding.
165 Block checksum usage is optional.
167 __Content Size flag__
169 If this flag is set, the uncompressed size of data included within the frame
170 will be present as an 8 bytes unsigned little endian value, after the flags.
171 Content Size usage is optional.
173 __Content checksum flag__
175 If this flag is set, a content checksum will be appended after the EndMark.
177 Recommended value : “1” (content checksum is present)
179 __Block Maximum Size__
181 This information is intended to help the decoder allocate memory.
182 Size here refers to the original (uncompressed) data size.
183 Block Maximum Size is one value among the following table :
185 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
186 | --- | --- | --- | --- | ----- | ------ | ---- | ---- |
187 | N/A | N/A | N/A | N/A | 64 KB | 256 KB | 1 MB | 4 MB |
189 The decoder may refuse to allocate block sizes above a (system-specific) size.
190 Unused values may be used in a future revision of the spec.
191 A decoder conformant to the current version of the spec
192 is only able to decode blocksizes defined in this spec.
196 Value of reserved bits **must** be 0 (zero).
197 Reserved bit might be used in a future version of the specification,
198 typically enabling new optional features.
199 If this happens, a decoder respecting the current version of the specification
200 shall not be able to decode such a frame.
204 This is the original (uncompressed) size.
205 This information is optional, and only present if the associated flag is set.
206 Content size is provided using unsigned 8 Bytes, for a maximum of 16 HexaBytes.
207 Format is Little endian.
208 This value is informational, typically for display or memory allocation.
209 It can be skipped by a decoder, or used to validate content correctness.
213 One-byte checksum of combined descriptor fields, including optional ones.
214 The value is the second byte of xxh32() : ` (xxh32()>>8) & 0xFF `
215 using zero as a seed,
216 and the full Frame Descriptor as an input
217 (including optional fields when they are present).
218 A wrong checksum indicates an error in the descriptor.
219 Header checksum is informational and can be skipped.
225 | Block Size | data | (Block Checksum) |
226 |:----------:| ------ |:----------------:|
227 | 4 bytes | | 0 - 4 bytes |
232 This field uses 4-bytes, format is little-endian.
234 The highest bit is “1” if data in the block is uncompressed.
236 The highest bit is “0” if data in the block is compressed by LZ4.
238 All other bits give the size, in bytes, of the following data block
239 (the size does not include the block checksum if present).
241 Block Size shall never be larger than Block Maximum Size.
242 Such a thing could happen for incompressible source data.
243 In such case, such a data block shall be passed in uncompressed format.
247 Where the actual data to decode stands.
248 It might be compressed or not, depending on previous field indications.
249 Uncompressed size of Data can be any size, up to “block maximum size”.
250 Note that data block is not necessarily full :
251 an arbitrary “flush” may happen anytime. Any block can be “partially filled”.
255 Only present if the associated flag is set.
256 This is a 4-bytes checksum value, in little endian format,
257 calculated by using the xxHash-32 algorithm on the raw (undecoded) data block,
259 The intention is to detect data corruption (storage or transmission errors)
262 Block checksum is cumulative with Content checksum.
268 | Magic Number | Frame Size | User Data |
269 |:------------:|:----------:| --------- |
270 | 4 bytes | 4 bytes | |
272 Skippable frames allow the integration of user-defined data
273 into a flow of concatenated frames.
274 Its design is pretty straightforward,
275 with the sole objective to allow the decoder to quickly skip
276 over user-defined data and continue decoding.
278 For the purpose of facilitating identification,
279 it is discouraged to start a flow of concatenated frames with a skippable frame.
280 If there is a need to start such a flow with some user data
281 encapsulated into a skippable frame,
282 it’s recommended to start with a zero-byte LZ4 frame
283 followed by a skippable frame.
284 This will make it easier for file type identifiers.
289 4 Bytes, Little endian format.
290 Value : 0x184D2A5X, which means any value from 0x184D2A50 to 0x184D2A5F.
291 All 16 values are valid to identify a skippable frame.
295 This is the size, in bytes, of the following User Data
296 (without including the magic number nor the size field itself).
297 4 Bytes, Little endian format, unsigned 32-bits.
298 This means User Data can’t be bigger than (2^32-1) Bytes.
302 User Data can be anything. Data will just be skipped by the decoder.
308 The Legacy frame format was defined into the initial versions of “LZ4Demo”.
309 Newer compressors should not use this format anymore, as it is too restrictive.
311 Main characteristics of the legacy format :
313 - Fixed block size : 8 MB.
314 - All blocks must be completely filled, except the last one.
315 - All blocks are always compressed, even when compression is detrimental.
316 - The last block is detected either because
317 it is followed by the “EOF” (End of File) mark,
318 or because it is followed by a known Frame Magic Number.
320 - Convention is Little endian
322 | MagicNb | B.CSize | CData | B.CSize | CData | (...) | EndMark |
323 | ------- | ------- | ----- | ------- | ----- | ------- | ------- |
324 | 4 bytes | 4 bytes | CSize | 4 bytes | CSize | x times | EOF |
329 4 Bytes, Little endian format.
332 __Block Compressed Size__
334 This is the size, in bytes, of the following compressed data block.
335 4 Bytes, Little endian format.
339 Where the actual compressed data stands.
340 Data is always compressed, even when compression is detrimental.
344 End of legacy frame is implicit only.
345 It must be followed by a standard EOF (End Of File) signal,
346 whether it is a file or a stream.
348 Alternatively, if the frame is followed by a valid Frame Magic Number,
349 it is considered completed.
350 It makes legacy frames compatible with frame concatenation.
352 Any other value will be interpreted as a block size,
353 and trigger an error if it does not fit within acceptable range.
359 1.5.1 : changed format to MarkDown compatible
361 1.5 : removed Dictionary ID from specification
363 1.4.1 : changed wording from “stream” to “frame”
365 1.4 : added skippable streams, re-added stream checksum
367 1.3 : modified header checksum
369 1.2 : reduced choice of “block size”, to postpone decision on “dynamic size of BlockSize Field”.
371 1.1 : optional fields are now part of the descriptor
373 1.0 : changed “block size” specification, adding a compressed/uncompressed flag
375 0.9 : reduced scale of “block maximum size” table
377 0.8 : removed : high compression flag
379 0.7 : removed : stream checksum
381 0.6 : settled : stream size uses 8 bytes, endian convention is little endian
383 0.5: added copyright notice
385 0.4 : changed format to Google Doc compatible OpenDocument