xref: /aosp_15_r20/external/lz4/doc/lz4_Frame_format.md (revision 27162e4e17433d5aa7cb38e7b6a433a09405fc7f)
1*27162e4eSAndroid Build Coastguard WorkerLZ4 Frame Format Description
2*27162e4eSAndroid Build Coastguard Worker============================
3*27162e4eSAndroid Build Coastguard Worker
4*27162e4eSAndroid Build Coastguard Worker### Notices
5*27162e4eSAndroid Build Coastguard Worker
6*27162e4eSAndroid Build Coastguard WorkerCopyright (c) 2013-2020 Yann Collet
7*27162e4eSAndroid Build Coastguard Worker
8*27162e4eSAndroid Build Coastguard WorkerPermission is granted to copy and distribute this document
9*27162e4eSAndroid Build Coastguard Workerfor any purpose and without charge,
10*27162e4eSAndroid Build Coastguard Workerincluding translations into other languages
11*27162e4eSAndroid Build Coastguard Workerand incorporation into compilations,
12*27162e4eSAndroid Build Coastguard Workerprovided that the copyright notice and this notice are preserved,
13*27162e4eSAndroid Build Coastguard Workerand that any substantive changes or deletions from the original
14*27162e4eSAndroid Build Coastguard Workerare clearly marked.
15*27162e4eSAndroid Build Coastguard WorkerDistribution of this document is unlimited.
16*27162e4eSAndroid Build Coastguard Worker
17*27162e4eSAndroid Build Coastguard Worker### Version
18*27162e4eSAndroid Build Coastguard Worker
19*27162e4eSAndroid Build Coastguard Worker1.6.4 (28/12/2023)
20*27162e4eSAndroid Build Coastguard Worker
21*27162e4eSAndroid Build Coastguard Worker
22*27162e4eSAndroid Build Coastguard WorkerIntroduction
23*27162e4eSAndroid Build Coastguard Worker------------
24*27162e4eSAndroid Build Coastguard Worker
25*27162e4eSAndroid Build Coastguard WorkerThe purpose of this document is to define a lossless compressed data format,
26*27162e4eSAndroid Build Coastguard Workerthat is independent of CPU type, operating system,
27*27162e4eSAndroid Build Coastguard Workerfile system and character set, suitable for
28*27162e4eSAndroid Build Coastguard WorkerFile compression, Pipe and streaming compression
29*27162e4eSAndroid Build Coastguard Workerusing the [LZ4 algorithm](http://www.lz4.org).
30*27162e4eSAndroid Build Coastguard Worker
31*27162e4eSAndroid Build Coastguard WorkerThe data can be produced or consumed,
32*27162e4eSAndroid Build Coastguard Workereven for an arbitrarily long sequentially presented input data stream,
33*27162e4eSAndroid Build Coastguard Workerusing only an a priori bounded amount of intermediate storage,
34*27162e4eSAndroid Build Coastguard Workerand hence can be used in data communications.
35*27162e4eSAndroid Build Coastguard WorkerThe format uses the LZ4 compression method,
36*27162e4eSAndroid Build Coastguard Workerand optional [xxHash-32 checksum method](https://github.com/Cyan4973/xxHash),
37*27162e4eSAndroid Build Coastguard Workerfor detection of data corruption.
38*27162e4eSAndroid Build Coastguard Worker
39*27162e4eSAndroid Build Coastguard WorkerThe data format defined by this specification
40*27162e4eSAndroid Build Coastguard Workerdoes not attempt to allow random access to compressed data.
41*27162e4eSAndroid Build Coastguard Worker
42*27162e4eSAndroid Build Coastguard WorkerThis specification is intended for use by implementers of software
43*27162e4eSAndroid Build Coastguard Workerto compress data into LZ4 format and/or decompress data from LZ4 format.
44*27162e4eSAndroid Build Coastguard WorkerThe text of the specification assumes a basic background in programming
45*27162e4eSAndroid Build Coastguard Workerat the level of bits and other primitive data representations.
46*27162e4eSAndroid Build Coastguard Worker
47*27162e4eSAndroid Build Coastguard WorkerUnless otherwise indicated below,
48*27162e4eSAndroid Build Coastguard Workera compliant compressor must produce data sets
49*27162e4eSAndroid Build Coastguard Workerthat conform to the specifications presented here.
50*27162e4eSAndroid Build Coastguard WorkerIt doesn't need to support all options though.
51*27162e4eSAndroid Build Coastguard Worker
52*27162e4eSAndroid Build Coastguard WorkerA compliant decompressor must be able to decompress
53*27162e4eSAndroid Build Coastguard Workerat least one working set of parameters
54*27162e4eSAndroid Build Coastguard Workerthat conforms to the specifications presented here.
55*27162e4eSAndroid Build Coastguard WorkerIt may also ignore checksums.
56*27162e4eSAndroid Build Coastguard WorkerWhenever it does not support a specific parameter within the compressed stream,
57*27162e4eSAndroid Build Coastguard Workerit must produce a non-ambiguous error code
58*27162e4eSAndroid Build Coastguard Workerand associated error message explaining which parameter is unsupported.
59*27162e4eSAndroid Build Coastguard Worker
60*27162e4eSAndroid Build Coastguard Worker
61*27162e4eSAndroid Build Coastguard WorkerGeneral Structure of LZ4 Frame format
62*27162e4eSAndroid Build Coastguard Worker-------------------------------------
63*27162e4eSAndroid Build Coastguard Worker
64*27162e4eSAndroid Build Coastguard Worker| MagicNb | F. Descriptor | Data Block | (...) | EndMark | C. Checksum |
65*27162e4eSAndroid Build Coastguard Worker|:-------:|:-------------:| ---------- | ----- | ------- | ----------- |
66*27162e4eSAndroid Build Coastguard Worker| 4 bytes |  3-15 bytes   |            |       | 4 bytes | 0-4 bytes   |
67*27162e4eSAndroid Build Coastguard Worker
68*27162e4eSAndroid Build Coastguard Worker__Magic Number__
69*27162e4eSAndroid Build Coastguard Worker
70*27162e4eSAndroid Build Coastguard Worker4 Bytes, Little endian format.
71*27162e4eSAndroid Build Coastguard WorkerValue : 0x184D2204
72*27162e4eSAndroid Build Coastguard Worker
73*27162e4eSAndroid Build Coastguard Worker__Frame Descriptor__
74*27162e4eSAndroid Build Coastguard Worker
75*27162e4eSAndroid Build Coastguard Worker3 to 15 Bytes, to be detailed in its own paragraph,
76*27162e4eSAndroid Build Coastguard Workeras it is the most important part of the spec.
77*27162e4eSAndroid Build Coastguard Worker
78*27162e4eSAndroid Build Coastguard WorkerThe combined _Magic_Number_ and _Frame_Descriptor_ fields are sometimes
79*27162e4eSAndroid Build Coastguard Workercalled ___LZ4 Frame Header___. Its size varies between 7 and 19 bytes.
80*27162e4eSAndroid Build Coastguard Worker
81*27162e4eSAndroid Build Coastguard Worker__Data Blocks__
82*27162e4eSAndroid Build Coastguard Worker
83*27162e4eSAndroid Build Coastguard WorkerTo be detailed in its own paragraph.
84*27162e4eSAndroid Build Coastguard WorkerThat’s where compressed data is stored.
85*27162e4eSAndroid Build Coastguard Worker
86*27162e4eSAndroid Build Coastguard Worker__EndMark__
87*27162e4eSAndroid Build Coastguard Worker
88*27162e4eSAndroid Build Coastguard WorkerThe flow of blocks ends when the last data block is followed by
89*27162e4eSAndroid Build Coastguard Workerthe 32-bit value `0x00000000`.
90*27162e4eSAndroid Build Coastguard Worker
91*27162e4eSAndroid Build Coastguard Worker__Content Checksum__
92*27162e4eSAndroid Build Coastguard Worker
93*27162e4eSAndroid Build Coastguard Worker_Content_Checksum_ verify that the full content has been decoded correctly.
94*27162e4eSAndroid Build Coastguard WorkerThe content checksum is the result of [xxHash-32 algorithm]
95*27162e4eSAndroid Build Coastguard Workerdigesting the original (decoded) data as input, and a seed of zero.
96*27162e4eSAndroid Build Coastguard WorkerContent checksum is only present when its associated flag
97*27162e4eSAndroid Build Coastguard Workeris set in the frame descriptor.
98*27162e4eSAndroid Build Coastguard WorkerContent Checksum validates the result,
99*27162e4eSAndroid Build Coastguard Workerthat all blocks were fully transmitted in the correct order and without error,
100*27162e4eSAndroid Build Coastguard Workerand also that the encoding/decoding process itself generated no distortion.
101*27162e4eSAndroid Build Coastguard WorkerIts usage is recommended.
102*27162e4eSAndroid Build Coastguard Worker
103*27162e4eSAndroid Build Coastguard WorkerThe combined _EndMark_ and _Content_Checksum_ fields might sometimes be
104*27162e4eSAndroid Build Coastguard Workerreferred to as ___LZ4 Frame Footer___. Its size varies between 4 and 8 bytes.
105*27162e4eSAndroid Build Coastguard Worker
106*27162e4eSAndroid Build Coastguard Worker__Frame Concatenation__
107*27162e4eSAndroid Build Coastguard Worker
108*27162e4eSAndroid Build Coastguard WorkerIn some circumstances, it may be preferable to append multiple frames,
109*27162e4eSAndroid Build Coastguard Workerfor example in order to add new data to an existing compressed file
110*27162e4eSAndroid Build Coastguard Workerwithout re-framing it.
111*27162e4eSAndroid Build Coastguard Worker
112*27162e4eSAndroid Build Coastguard WorkerIn such case, each frame has its own set of descriptor flags.
113*27162e4eSAndroid Build Coastguard WorkerEach frame is considered independent.
114*27162e4eSAndroid Build Coastguard WorkerThe only relation between frames is their sequential order.
115*27162e4eSAndroid Build Coastguard Worker
116*27162e4eSAndroid Build Coastguard WorkerThe ability to decode multiple concatenated frames
117*27162e4eSAndroid Build Coastguard Workerwithin a single stream or file
118*27162e4eSAndroid Build Coastguard Workeris left outside of this specification.
119*27162e4eSAndroid Build Coastguard WorkerAs an example, the reference lz4 command line utility behavior is
120*27162e4eSAndroid Build Coastguard Workerto decode all concatenated frames in their sequential order.
121*27162e4eSAndroid Build Coastguard Worker
122*27162e4eSAndroid Build Coastguard Worker
123*27162e4eSAndroid Build Coastguard WorkerFrame Descriptor
124*27162e4eSAndroid Build Coastguard Worker----------------
125*27162e4eSAndroid Build Coastguard Worker
126*27162e4eSAndroid Build Coastguard Worker| FLG     | BD      | (Content Size) | (Dictionary ID) | HC      |
127*27162e4eSAndroid Build Coastguard Worker| ------- | ------- |:--------------:|:---------------:| ------- |
128*27162e4eSAndroid Build Coastguard Worker| 1 byte  | 1 byte  |  0 - 8 bytes   |   0 - 4 bytes   | 1 byte  |
129*27162e4eSAndroid Build Coastguard Worker
130*27162e4eSAndroid Build Coastguard WorkerThe descriptor uses a minimum of 3 bytes,
131*27162e4eSAndroid Build Coastguard Workerand up to 15 bytes depending on optional parameters.
132*27162e4eSAndroid Build Coastguard Worker
133*27162e4eSAndroid Build Coastguard Worker__FLG byte__
134*27162e4eSAndroid Build Coastguard Worker
135*27162e4eSAndroid Build Coastguard Worker|  BitNb  |  7-6  |   5   |    4     |  3   |    2     |    1     |   0  |
136*27162e4eSAndroid Build Coastguard Worker| ------- |-------|-------|----------|------|----------|----------|------|
137*27162e4eSAndroid Build Coastguard Worker|FieldName|Version|B.Indep|B.Checksum|C.Size|C.Checksum|*Reserved*|DictID|
138*27162e4eSAndroid Build Coastguard Worker
139*27162e4eSAndroid Build Coastguard Worker
140*27162e4eSAndroid Build Coastguard Worker__BD byte__
141*27162e4eSAndroid Build Coastguard Worker
142*27162e4eSAndroid Build Coastguard Worker|  BitNb  |     7    |     6-5-4     |  3-2-1-0 |
143*27162e4eSAndroid Build Coastguard Worker| ------- | -------- | ------------- | -------- |
144*27162e4eSAndroid Build Coastguard Worker|FieldName|*Reserved*| Block MaxSize |*Reserved*|
145*27162e4eSAndroid Build Coastguard Worker
146*27162e4eSAndroid Build Coastguard WorkerIn the tables, bit 7 is highest bit, while bit 0 is lowest.
147*27162e4eSAndroid Build Coastguard Worker
148*27162e4eSAndroid Build Coastguard Worker__Version Number__
149*27162e4eSAndroid Build Coastguard Worker
150*27162e4eSAndroid Build Coastguard Worker2-bits field, must be set to `01`.
151*27162e4eSAndroid Build Coastguard WorkerAny other value cannot be decoded by this version of the specification.
152*27162e4eSAndroid Build Coastguard WorkerOther version numbers will use different flag layouts.
153*27162e4eSAndroid Build Coastguard Worker
154*27162e4eSAndroid Build Coastguard Worker__Block Independence flag__
155*27162e4eSAndroid Build Coastguard Worker
156*27162e4eSAndroid Build Coastguard WorkerIf this flag is set to “1”, blocks are independent.
157*27162e4eSAndroid Build Coastguard WorkerIf this flag is set to “0”, each block depends on previous ones
158*27162e4eSAndroid Build Coastguard Worker(up to LZ4 window size, which is 64 KB).
159*27162e4eSAndroid Build Coastguard WorkerIn such case, it’s necessary to decode all blocks in sequence.
160*27162e4eSAndroid Build Coastguard Worker
161*27162e4eSAndroid Build Coastguard WorkerBlock dependency improves compression ratio, especially for small blocks.
162*27162e4eSAndroid Build Coastguard WorkerOn the other hand, it makes random access or multi-threaded decoding impossible.
163*27162e4eSAndroid Build Coastguard Worker
164*27162e4eSAndroid Build Coastguard Worker__Block checksum flag__
165*27162e4eSAndroid Build Coastguard Worker
166*27162e4eSAndroid Build Coastguard WorkerIf this flag is set, each data block will be followed by a 4-bytes checksum,
167*27162e4eSAndroid Build Coastguard Workercalculated by using the xxHash-32 algorithm on the raw (compressed) data block.
168*27162e4eSAndroid Build Coastguard WorkerThe intention is to detect data corruption (storage or transmission errors)
169*27162e4eSAndroid Build Coastguard Workerimmediately, before decoding.
170*27162e4eSAndroid Build Coastguard WorkerBlock checksum usage is optional.
171*27162e4eSAndroid Build Coastguard Worker
172*27162e4eSAndroid Build Coastguard Worker__Content Size flag__
173*27162e4eSAndroid Build Coastguard Worker
174*27162e4eSAndroid Build Coastguard WorkerIf this flag is set, the uncompressed size of data included within the frame
175*27162e4eSAndroid Build Coastguard Workerwill be present as an 8 bytes unsigned little endian value, after the flags.
176*27162e4eSAndroid Build Coastguard WorkerContent Size usage is optional.
177*27162e4eSAndroid Build Coastguard Worker
178*27162e4eSAndroid Build Coastguard Worker__Content checksum flag__
179*27162e4eSAndroid Build Coastguard Worker
180*27162e4eSAndroid Build Coastguard WorkerIf this flag is set, a 32-bits content checksum will be appended
181*27162e4eSAndroid Build Coastguard Workerafter the EndMark.
182*27162e4eSAndroid Build Coastguard Worker
183*27162e4eSAndroid Build Coastguard Worker__Dictionary ID flag__
184*27162e4eSAndroid Build Coastguard Worker
185*27162e4eSAndroid Build Coastguard WorkerIf this flag is set, a 4-bytes Dict-ID field will be present,
186*27162e4eSAndroid Build Coastguard Workerafter the descriptor flags and the Content Size.
187*27162e4eSAndroid Build Coastguard Worker
188*27162e4eSAndroid Build Coastguard Worker__Block Maximum Size__
189*27162e4eSAndroid Build Coastguard Worker
190*27162e4eSAndroid Build Coastguard WorkerThis information is useful to help the decoder allocate memory.
191*27162e4eSAndroid Build Coastguard WorkerSize here refers to the original (uncompressed) data size.
192*27162e4eSAndroid Build Coastguard WorkerBlock Maximum Size is one value among the following table :
193*27162e4eSAndroid Build Coastguard Worker
194*27162e4eSAndroid Build Coastguard Worker|  0  |  1  |  2  |  3  |   4   |   5    |  6   |  7   |
195*27162e4eSAndroid Build Coastguard Worker| --- | --- | --- | --- | ----- | ------ | ---- | ---- |
196*27162e4eSAndroid Build Coastguard Worker| N/A | N/A | N/A | N/A | 64 KB | 256 KB | 1 MB | 4 MB |
197*27162e4eSAndroid Build Coastguard Worker
198*27162e4eSAndroid Build Coastguard WorkerThe decoder may refuse to allocate block sizes above any system-specific size.
199*27162e4eSAndroid Build Coastguard WorkerUnused values may be used in a future revision of the spec.
200*27162e4eSAndroid Build Coastguard WorkerA decoder conformant with the current version of the spec
201*27162e4eSAndroid Build Coastguard Workeris only able to decode block sizes defined in this spec.
202*27162e4eSAndroid Build Coastguard Worker
203*27162e4eSAndroid Build Coastguard Worker__Reserved bits__
204*27162e4eSAndroid Build Coastguard Worker
205*27162e4eSAndroid Build Coastguard WorkerValue of reserved bits **must** be 0 (zero).
206*27162e4eSAndroid Build Coastguard WorkerReserved bit might be used in a future version of the specification,
207*27162e4eSAndroid Build Coastguard Workertypically enabling new optional features.
208*27162e4eSAndroid Build Coastguard WorkerWhen this happens, a decoder respecting the current specification version
209*27162e4eSAndroid Build Coastguard Workershall not be able to decode such a frame.
210*27162e4eSAndroid Build Coastguard Worker
211*27162e4eSAndroid Build Coastguard Worker__Content Size__
212*27162e4eSAndroid Build Coastguard Worker
213*27162e4eSAndroid Build Coastguard WorkerThis is the original (uncompressed) size.
214*27162e4eSAndroid Build Coastguard WorkerThis information is optional, and only present if the associated flag is set.
215*27162e4eSAndroid Build Coastguard WorkerContent size is provided using unsigned 8 Bytes, for a maximum of 16 Exabytes.
216*27162e4eSAndroid Build Coastguard WorkerFormat is Little endian.
217*27162e4eSAndroid Build Coastguard WorkerThis value is informational, typically for display or memory allocation.
218*27162e4eSAndroid Build Coastguard WorkerIt can be skipped by a decoder, or used to validate content correctness.
219*27162e4eSAndroid Build Coastguard Worker
220*27162e4eSAndroid Build Coastguard Worker__Dictionary ID__
221*27162e4eSAndroid Build Coastguard Worker
222*27162e4eSAndroid Build Coastguard WorkerA dictionary is useful to compress short input sequences.
223*27162e4eSAndroid Build Coastguard WorkerWhen present, the compressor can take advantage of dictionary's content
224*27162e4eSAndroid Build Coastguard Workeras a kind of “known prefix” to encode the input in a more compact manner.
225*27162e4eSAndroid Build Coastguard Worker
226*27162e4eSAndroid Build Coastguard WorkerWhen the frame descriptor defines independent blocks,
227*27162e4eSAndroid Build Coastguard Workerevery block is initialized with the same dictionary.
228*27162e4eSAndroid Build Coastguard WorkerIf the frame descriptor defines linked blocks,
229*27162e4eSAndroid Build Coastguard Workerthe dictionary is only used once, at the beginning of the frame.
230*27162e4eSAndroid Build Coastguard Worker
231*27162e4eSAndroid Build Coastguard WorkerThe compressor and the decompressor must employ exactly the same dictionary for the data to be decodable.
232*27162e4eSAndroid Build Coastguard Worker
233*27162e4eSAndroid Build Coastguard WorkerThe Dict-ID field is offered as a way to help the decoder determine
234*27162e4eSAndroid Build Coastguard Workerwhich dictionary must be used to correctly decode the compressed frame.
235*27162e4eSAndroid Build Coastguard WorkerDict-ID is only present if the associated flag is set.
236*27162e4eSAndroid Build Coastguard WorkerIt's an unsigned 32-bits value, stored using little-endian convention.
237*27162e4eSAndroid Build Coastguard WorkerWithin a single frame, only a single Dict-ID field can be defined.
238*27162e4eSAndroid Build Coastguard Worker
239*27162e4eSAndroid Build Coastguard WorkerNote that the Dict-ID field is optional.
240*27162e4eSAndroid Build Coastguard WorkerKnowledge of which dictionary to employ can also be passed off-band,
241*27162e4eSAndroid Build Coastguard Workerfor example, it could be implied by the context of the application.
242*27162e4eSAndroid Build Coastguard Worker
243*27162e4eSAndroid Build Coastguard Worker__Header Checksum__
244*27162e4eSAndroid Build Coastguard Worker
245*27162e4eSAndroid Build Coastguard WorkerOne-byte checksum of combined descriptor fields, including optional ones.
246*27162e4eSAndroid Build Coastguard WorkerThe value is the second byte of `xxh32()` : ` (xxh32()>>8) & 0xFF `
247*27162e4eSAndroid Build Coastguard Workerusing zero as a seed, and the full Frame Descriptor as an input
248*27162e4eSAndroid Build Coastguard Worker(including optional fields when they are present).
249*27162e4eSAndroid Build Coastguard WorkerA wrong checksum indicates that the descriptor is erroneous.
250*27162e4eSAndroid Build Coastguard Worker
251*27162e4eSAndroid Build Coastguard Worker
252*27162e4eSAndroid Build Coastguard WorkerData Blocks
253*27162e4eSAndroid Build Coastguard Worker-----------
254*27162e4eSAndroid Build Coastguard Worker
255*27162e4eSAndroid Build Coastguard Worker| Block Size |  data  | (Block Checksum) |
256*27162e4eSAndroid Build Coastguard Worker|:----------:| ------ |:----------------:|
257*27162e4eSAndroid Build Coastguard Worker|  4 bytes   |        |   0 - 4 bytes    |
258*27162e4eSAndroid Build Coastguard Worker
259*27162e4eSAndroid Build Coastguard Worker
260*27162e4eSAndroid Build Coastguard Worker__Block Size__
261*27162e4eSAndroid Build Coastguard Worker
262*27162e4eSAndroid Build Coastguard WorkerThis field uses 4-bytes, format is little-endian.
263*27162e4eSAndroid Build Coastguard Worker
264*27162e4eSAndroid Build Coastguard WorkerIf the highest bit is set (`1`), the block is uncompressed.
265*27162e4eSAndroid Build Coastguard Worker
266*27162e4eSAndroid Build Coastguard WorkerIf the highest bit is not set (`0`), the block is LZ4-compressed,
267*27162e4eSAndroid Build Coastguard Workerusing the [LZ4 block format specification](https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md).
268*27162e4eSAndroid Build Coastguard Worker
269*27162e4eSAndroid Build Coastguard WorkerAll other bits give the size, in bytes, of the data section.
270*27162e4eSAndroid Build Coastguard WorkerThe size does not include the block checksum if present.
271*27162e4eSAndroid Build Coastguard Worker
272*27162e4eSAndroid Build Coastguard Worker_Block_Size_ shall never be larger than _Block_Maximum_Size_.
273*27162e4eSAndroid Build Coastguard WorkerSuch an outcome could potentially happen for non-compressible sources.
274*27162e4eSAndroid Build Coastguard WorkerIn such a case, such data block **must** be passed using uncompressed format.
275*27162e4eSAndroid Build Coastguard Worker
276*27162e4eSAndroid Build Coastguard WorkerA value of `0x00000000` is invalid, and signifies an _EndMark_ instead.
277*27162e4eSAndroid Build Coastguard WorkerNote that this is different from a value of `0x80000000` (highest bit set),
278*27162e4eSAndroid Build Coastguard Workerwhich is an uncompressed block of size 0 (empty),
279*27162e4eSAndroid Build Coastguard Workerwhich is valid, and therefore doesn't end a frame.
280*27162e4eSAndroid Build Coastguard WorkerNote that, if _Block_checksum_ is enabled,
281*27162e4eSAndroid Build Coastguard Workereven an empty block must be followed by a 32-bit block checksum.
282*27162e4eSAndroid Build Coastguard Worker
283*27162e4eSAndroid Build Coastguard Worker__Data__
284*27162e4eSAndroid Build Coastguard Worker
285*27162e4eSAndroid Build Coastguard WorkerWhere the actual data to decode stands.
286*27162e4eSAndroid Build Coastguard WorkerIt might be compressed or not, depending on previous field indications.
287*27162e4eSAndroid Build Coastguard Worker
288*27162e4eSAndroid Build Coastguard WorkerWhen compressed, the data must respect the [LZ4 block format specification](https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md).
289*27162e4eSAndroid Build Coastguard Worker
290*27162e4eSAndroid Build Coastguard WorkerNote that a block is not necessarily full.
291*27162e4eSAndroid Build Coastguard WorkerUncompressed size of data can be any size __up to__ _Block_Maximum_Size_,
292*27162e4eSAndroid Build Coastguard Workerso it may contain less data than the maximum block size.
293*27162e4eSAndroid Build Coastguard Worker
294*27162e4eSAndroid Build Coastguard Worker__Block checksum__
295*27162e4eSAndroid Build Coastguard Worker
296*27162e4eSAndroid Build Coastguard WorkerOnly present if the associated flag is set.
297*27162e4eSAndroid Build Coastguard WorkerThis is a 4-bytes checksum value, in little endian format,
298*27162e4eSAndroid Build Coastguard Workercalculated by using the [xxHash-32 algorithm] on the __raw__ (undecoded) data block,
299*27162e4eSAndroid Build Coastguard Workerand a seed of zero.
300*27162e4eSAndroid Build Coastguard WorkerThe intention is to detect data corruption (storage or transmission errors)
301*27162e4eSAndroid Build Coastguard Workerbefore decoding.
302*27162e4eSAndroid Build Coastguard Worker
303*27162e4eSAndroid Build Coastguard Worker_Block_checksum_ can be cumulative with _Content_checksum_.
304*27162e4eSAndroid Build Coastguard Worker
305*27162e4eSAndroid Build Coastguard Worker[xxHash-32 algorithm]: https://github.com/Cyan4973/xxHash/blob/release/doc/xxhash_spec.md
306*27162e4eSAndroid Build Coastguard Worker
307*27162e4eSAndroid Build Coastguard Worker
308*27162e4eSAndroid Build Coastguard WorkerSkippable Frames
309*27162e4eSAndroid Build Coastguard Worker----------------
310*27162e4eSAndroid Build Coastguard Worker
311*27162e4eSAndroid Build Coastguard Worker| Magic Number | Frame Size | User Data |
312*27162e4eSAndroid Build Coastguard Worker|:------------:|:----------:| --------- |
313*27162e4eSAndroid Build Coastguard Worker|   4 bytes    |  4 bytes   |           |
314*27162e4eSAndroid Build Coastguard Worker
315*27162e4eSAndroid Build Coastguard WorkerSkippable frames allow the integration of user-defined data
316*27162e4eSAndroid Build Coastguard Workerinto a flow of concatenated frames.
317*27162e4eSAndroid Build Coastguard WorkerIts design is pretty straightforward,
318*27162e4eSAndroid Build Coastguard Workerwith the sole objective to allow the decoder to quickly skip
319*27162e4eSAndroid Build Coastguard Workerover user-defined data and continue decoding.
320*27162e4eSAndroid Build Coastguard Worker
321*27162e4eSAndroid Build Coastguard WorkerFor the purpose of facilitating identification,
322*27162e4eSAndroid Build Coastguard Workerit is discouraged to start a flow of concatenated frames with a skippable frame.
323*27162e4eSAndroid Build Coastguard WorkerIf there is a need to start such a flow with some user data
324*27162e4eSAndroid Build Coastguard Workerencapsulated into a skippable frame,
325*27162e4eSAndroid Build Coastguard Workerit’s recommended to start with a zero-byte LZ4 frame
326*27162e4eSAndroid Build Coastguard Workerfollowed by a skippable frame.
327*27162e4eSAndroid Build Coastguard WorkerThis will make it easier for file type identifiers.
328*27162e4eSAndroid Build Coastguard Worker
329*27162e4eSAndroid Build Coastguard Worker
330*27162e4eSAndroid Build Coastguard Worker__Magic Number__
331*27162e4eSAndroid Build Coastguard Worker
332*27162e4eSAndroid Build Coastguard Worker4 Bytes, Little endian format.
333*27162e4eSAndroid Build Coastguard WorkerValue : 0x184D2A5X, which means any value from 0x184D2A50 to 0x184D2A5F.
334*27162e4eSAndroid Build Coastguard WorkerAll 16 values are valid to identify a skippable frame.
335*27162e4eSAndroid Build Coastguard Worker
336*27162e4eSAndroid Build Coastguard Worker__Frame Size__
337*27162e4eSAndroid Build Coastguard Worker
338*27162e4eSAndroid Build Coastguard WorkerThis is the size, in bytes, of the following User Data
339*27162e4eSAndroid Build Coastguard Worker(without including the magic number nor the size field itself).
340*27162e4eSAndroid Build Coastguard Worker4 Bytes, Little endian format, unsigned 32-bits.
341*27162e4eSAndroid Build Coastguard WorkerThis means User Data can’t be bigger than (2^32-1) Bytes.
342*27162e4eSAndroid Build Coastguard Worker
343*27162e4eSAndroid Build Coastguard Worker__User Data__
344*27162e4eSAndroid Build Coastguard Worker
345*27162e4eSAndroid Build Coastguard WorkerUser Data can be anything. Data will just be skipped by the decoder.
346*27162e4eSAndroid Build Coastguard Worker
347*27162e4eSAndroid Build Coastguard Worker
348*27162e4eSAndroid Build Coastguard WorkerLegacy frame
349*27162e4eSAndroid Build Coastguard Worker------------
350*27162e4eSAndroid Build Coastguard Worker
351*27162e4eSAndroid Build Coastguard WorkerThe Legacy frame format was defined into the initial versions of “LZ4Demo”.
352*27162e4eSAndroid Build Coastguard WorkerNewer compressors should not use this format anymore, as it is too restrictive.
353*27162e4eSAndroid Build Coastguard Worker
354*27162e4eSAndroid Build Coastguard WorkerMain characteristics of the legacy format :
355*27162e4eSAndroid Build Coastguard Worker
356*27162e4eSAndroid Build Coastguard Worker- Fixed block size : 8 MB.
357*27162e4eSAndroid Build Coastguard Worker- All blocks must be completely filled, except the last one.
358*27162e4eSAndroid Build Coastguard Worker- All blocks are always compressed, even when compression is detrimental.
359*27162e4eSAndroid Build Coastguard Worker- The last block is detected either because
360*27162e4eSAndroid Build Coastguard Worker  it is followed by the “EOF” (End of File) mark,
361*27162e4eSAndroid Build Coastguard Worker  or because it is followed by a known Frame Magic Number.
362*27162e4eSAndroid Build Coastguard Worker- No checksum
363*27162e4eSAndroid Build Coastguard Worker- Convention is Little endian
364*27162e4eSAndroid Build Coastguard Worker
365*27162e4eSAndroid Build Coastguard Worker| MagicNb | B.CSize | CData | B.CSize | CData |  (...)  | EndMark |
366*27162e4eSAndroid Build Coastguard Worker| ------- | ------- | ----- | ------- | ----- | ------- | ------- |
367*27162e4eSAndroid Build Coastguard Worker| 4 bytes | 4 bytes | CSize | 4 bytes | CSize | x times |   EOF   |
368*27162e4eSAndroid Build Coastguard Worker
369*27162e4eSAndroid Build Coastguard Worker
370*27162e4eSAndroid Build Coastguard Worker__Magic Number__
371*27162e4eSAndroid Build Coastguard Worker
372*27162e4eSAndroid Build Coastguard Worker4 Bytes, Little endian format.
373*27162e4eSAndroid Build Coastguard WorkerValue : 0x184C2102
374*27162e4eSAndroid Build Coastguard Worker
375*27162e4eSAndroid Build Coastguard Worker__Block Compressed Size__
376*27162e4eSAndroid Build Coastguard Worker
377*27162e4eSAndroid Build Coastguard WorkerThis is the size, in bytes, of the following compressed data block.
378*27162e4eSAndroid Build Coastguard Worker4 Bytes, Little endian format.
379*27162e4eSAndroid Build Coastguard Worker
380*27162e4eSAndroid Build Coastguard Worker__Data__
381*27162e4eSAndroid Build Coastguard Worker
382*27162e4eSAndroid Build Coastguard WorkerWhere the actual compressed data stands.
383*27162e4eSAndroid Build Coastguard WorkerData is always compressed, even when compression is detrimental.
384*27162e4eSAndroid Build Coastguard Worker
385*27162e4eSAndroid Build Coastguard Worker__EndMark__
386*27162e4eSAndroid Build Coastguard Worker
387*27162e4eSAndroid Build Coastguard WorkerEnd of legacy frame is implicit only.
388*27162e4eSAndroid Build Coastguard WorkerIt must be followed by a standard EOF (End Of File) signal,
389*27162e4eSAndroid Build Coastguard Workerwhether it is a file or a stream.
390*27162e4eSAndroid Build Coastguard Worker
391*27162e4eSAndroid Build Coastguard WorkerAlternatively, if the frame is followed by a valid Frame Magic Number,
392*27162e4eSAndroid Build Coastguard Workerit is considered completed.
393*27162e4eSAndroid Build Coastguard WorkerThis policy makes it possible to concatenate legacy frames.
394*27162e4eSAndroid Build Coastguard Worker
395*27162e4eSAndroid Build Coastguard WorkerAny other value will be interpreted as a block size,
396*27162e4eSAndroid Build Coastguard Workerand trigger an error if it does not fit within acceptable range.
397*27162e4eSAndroid Build Coastguard Worker
398*27162e4eSAndroid Build Coastguard Worker
399*27162e4eSAndroid Build Coastguard WorkerVersion changes
400*27162e4eSAndroid Build Coastguard Worker---------------
401*27162e4eSAndroid Build Coastguard Worker
402*27162e4eSAndroid Build Coastguard Worker1.6.4 : minor clarifications for Dictionaries
403*27162e4eSAndroid Build Coastguard Worker
404*27162e4eSAndroid Build Coastguard Worker1.6.3 : minor : clarify Data Block
405*27162e4eSAndroid Build Coastguard Worker
406*27162e4eSAndroid Build Coastguard Worker1.6.2 : clarifies specification of _EndMark_
407*27162e4eSAndroid Build Coastguard Worker
408*27162e4eSAndroid Build Coastguard Worker1.6.1 : introduced terms "LZ4 Frame Header" and "LZ4 Frame Footer"
409*27162e4eSAndroid Build Coastguard Worker
410*27162e4eSAndroid Build Coastguard Worker1.6.0 : restored Dictionary ID field in Frame header
411*27162e4eSAndroid Build Coastguard Worker
412*27162e4eSAndroid Build Coastguard Worker1.5.1 : changed document format to MarkDown
413*27162e4eSAndroid Build Coastguard Worker
414*27162e4eSAndroid Build Coastguard Worker1.5 : removed Dictionary ID from specification
415*27162e4eSAndroid Build Coastguard Worker
416*27162e4eSAndroid Build Coastguard Worker1.4.1 : changed wording from “stream” to “frame”
417*27162e4eSAndroid Build Coastguard Worker
418*27162e4eSAndroid Build Coastguard Worker1.4 : added skippable streams, re-added stream checksum
419*27162e4eSAndroid Build Coastguard Worker
420*27162e4eSAndroid Build Coastguard Worker1.3 : modified header checksum
421*27162e4eSAndroid Build Coastguard Worker
422*27162e4eSAndroid Build Coastguard Worker1.2 : reduced choice of “block size”, to postpone decision on “dynamic size of BlockSize Field”.
423*27162e4eSAndroid Build Coastguard Worker
424*27162e4eSAndroid Build Coastguard Worker1.1 : optional fields are now part of the descriptor
425*27162e4eSAndroid Build Coastguard Worker
426*27162e4eSAndroid Build Coastguard Worker1.0 : changed “block size” specification, adding a compressed/uncompressed flag
427*27162e4eSAndroid Build Coastguard Worker
428*27162e4eSAndroid Build Coastguard Worker0.9 : reduced scale of “block maximum size” table
429*27162e4eSAndroid Build Coastguard Worker
430*27162e4eSAndroid Build Coastguard Worker0.8 : removed : high compression flag
431*27162e4eSAndroid Build Coastguard Worker
432*27162e4eSAndroid Build Coastguard Worker0.7 : removed : stream checksum
433*27162e4eSAndroid Build Coastguard Worker
434*27162e4eSAndroid Build Coastguard Worker0.6 : settled : stream size uses 8 bytes, endian convention is little endian
435*27162e4eSAndroid Build Coastguard Worker
436*27162e4eSAndroid Build Coastguard Worker0.5 : added copyright notice
437*27162e4eSAndroid Build Coastguard Worker
438*27162e4eSAndroid Build Coastguard Worker0.4 : changed format to Google Doc compatible OpenDocument
439