blob: b203dd1013f7afc5c059c598aab5048019c72d86 [file] [log] [blame] [view]
Yann Collet2fa99042016-07-01 20:55:28 +02001Zstandard Compression Format Description
2========================================
3
4### Notices
5
6Copyright (c) 2016 Yann Collet
7
8Permission is granted to copy and distribute this document
9for any purpose and without charge,
10including translations into other languages
11and incorporation into compilations,
12provided that the copyright notice and this notice are preserved,
13and that any substantive changes or deletions from the original
14are clearly marked.
15Distribution of this document is unlimited.
16
17### Version
18
190.1.0 (30/06/2016 - unfinished)
20
21
22Introduction
23------------
24
25The purpose of this document is to define a lossless compressed data format,
26that is independent of CPU type, operating system,
27file system and character set, suitable for
28File compression, Pipe and streaming compression
29using the [Zstandard algorithm](http://www.zstandard.org).
30
31The data can be produced or consumed,
32even for an arbitrarily long sequentially presented input data stream,
33using only an a priori bounded amount of intermediate storage,
34and hence can be used in data communications.
35The format uses the Zstandard compression method,
36and optional [xxHash-64 checksum method](http://www.xxhash.org),
37for detection of data corruption.
38
39The data format defined by this specification
40does not attempt to allow random access to compressed data.
41
42This specification is intended for use by implementers of software
43to compress data into Zstandard format and/or decompress data from Zstandard format.
44The text of the specification assumes a basic background in programming
45at the level of bits and other primitive data representations.
46
47Unless otherwise indicated below,
48a compliant compressor must produce data sets
49that conform to the specifications presented here.
50It doesnt need to support all options though.
51
52A compliant decompressor must be able to decompress
53at least one working set of parameters
54that conforms to the specifications presented here.
55It may also ignore informative fields, such as checksum.
56Whenever it does not support a parameter defined in the compressed stream,
57it must produce a non-ambiguous error code and associated error message
58explaining which parameter is unsupported.
59
60
61Definitions
62-----------
63A content compressed by Zstandard is transformed into a Zstandard __frame__.
64Multiple frames can be appended into a single file or stream.
65A frame is totally independent, has a defined beginning and end,
66and a set of parameters which tells the decoder how to decompress it.
67
68A frame encapsulates one or multiple __blocks__.
69Each block can be compressed or not,
70and has a guaranteed maximum content size, which depends on frame parameters.
71Unlike frames, each block depends on previous blocks for proper decoding.
72However, each block can be decompressed without waiting for its successor,
73allowing streaming operations.
74
75
76General Structure of Zstandard Frame format
77-------------------------------------------
78
79| MagicNb | F. Header | Block | (More blocks) | EndMark |
80|:-------:|:----------:| ----- | ------------- | ------- |
81| 4 bytes | 2-14 bytes | | | 3 bytes |
82
83__Magic Number__
84
854 Bytes, Little endian format.
86Value : 0xFD2FB527
87
88__Frame Header__
89
902 to 14 Bytes, to be detailed in the next part.
91
92__Data Blocks__
93
94To be detailed later on.
95Thats where compressed data is stored.
96
97__EndMark__
98
99The flow of blocks ends when the last block header brings an _end signal_ .
100This last block header may optionally host a __Content Checksum__ .
101
102__Content Checksum__
103
104Content Checksum verify that frame content has been regenrated correctly.
105The content checksum is the result
106of [xxh64() hash function](https://www.xxHash.com)
107digesting the original (decoded) data as input, and a seed of zero.
108Bits from 11 to 32 (included) are extracted to form a 22 bits checksum
109stored into the last block header.
110```
111contentChecksum = (XXH64(content, size, 0) >> 11) & (1<<22)-1);
112```
113Content checksum is only present when its associated flag
114is set in the frame descriptor.
115Its usage is optional.
116
117__Frame Concatenation__
118
119In some circumstances, it may be required to append multiple frames,
120for example in order to add new data to an existing compressed file
121without re-framing it.
122
123In such case, each frame brings its own set of descriptor flags.
124Each frame is considered independent.
125The only relation between frames is their sequential order.
126
127The ability to decode multiple concatenated frames
128within a single stream or file is left outside of this specification.
129As an example, the reference `zstd` command line utility is able
130to decode all concatenated frames in their sequential order,
131delivering the final decompressed result as if it was a single content.
132
133
134Frame Header
135-------------
136
137| FHD | (WD) | (Content Size) | (dictID) |
138| ------- | --------- |:--------------:| --------- |
139| 1 byte | 0-1 byte | 0 - 8 bytes | 0-4 bytes |
140
141Frame header has a variable size, which uses a minimum of 2 bytes,
142and up to 14 bytes depending on optional parameters.
143
144__FHD byte__ (Frame Header Descriptor)
145
146The first Header's byte is called the Frame Header Descriptor.
147It tells which other fields are present.
148Decoding this byte is enough to get the full size of the Frame Header.
149
150| BitNb | 7-6 | 5 | 4 | 3 | 2 | 1-0 |
151| ------- | ------ | ------- | ------ | -------- | -------- | -------- |
152|FieldName| FCSize | Segment | Unused | Reserved | Checksum | dictID |
153
154In this table, bit 7 is highest bit, while bit 0 is lowest.
155
156__Frame Content Size flag__
157
158This is a 2-bits flag (`= FHD >> 6`),
159specifying if decompressed data size is provided within the header.
160
161| Value | 0 | 1 | 2 | 3 |
162| ------- | --- | --- | --- | --- |
163|FieldSize| 0-1 | 2 | 4 | 8 |
164
165Value 0 has a double meaning :
166it either means `0` (size not provided) _if_ the `WD` byte is present,
167or it means `1` byte (size <= 255 bytes).
168
169__Single Segment__
170
171If this flag is set,
172data shall be regenerated within a single continuous memory segment.
173In which case, `WD` byte __is not present__,
174but `Frame Content Size` field necessarily is.
175
176As a consequence, the decoder must allocate a memory segment
177of size `>= Frame Content Size`.
178
179In order to preserve the decoder from unreasonable memory requirement,
180a decoder can refuse a compressed frame
181which requests a memory size beyond decoder's authorized range.
182
183For broader compatibility, decoders are recommended to support
184memory sizes of 8 MB at least.
185However, this is merely a recommendation,
186and each decoder is free to support higher or lower limits,
187depending on local limitations.
188
189__Unused bit__
190
191The value of this bit is unimportant
192and not interpreted by a decoder compliant with this specification version.
193It may be used in a future revision,
194to signal a property which is not required to properly decode the frame.
195
196__Reserved bit__
197
198This bit is reserved for some future feature.
199Its value _must be zero_.
200A decoder compliant with this specification version must ensure it is not set.
201This bit may be used in a future revision,
202to signal a feature that must be interpreted in order to decode the frame.
203
204__Content checksum flag__
205
206If this flag is set, a content checksum will be present into the EndMark.
207The checksum is a 22 bits value extracted from the XXH64() of data.
208See __Content Checksum__ .
209
210__Dictionary ID flag__
211
212This is a 2-bits flag (`= FHD & 3`),
213telling if a dictionary ID is provided within the header
214
215| Value | 0 | 1 | 2 | 3 |
216| ------- | --- | --- | --- | --- |
217|FieldSize| 0 | 1 | 2 | 4 |
218
219__WD byte__ (Window Descriptor)
220
221Provides guarantees on maximum back-reference distance
222that will be present within compressed data.
223This information is useful for decoders to allocate enough memory.
224
225| BitNb | 7-3 | 0-2 |
226| --------- | -------- | -------- |
227| FieldName | Exponent | Mantissa |
228
229Maximum distance is given by the following formulae :
230```
231windowLog = 10 + Exponent;
232windowBase = 1 << windowLog;
233windowAdd = (windowBase / 8) * Mantissa;
234windowSize = windowBase + windowAdd;
235```
236The minimum window size is 1 KB.
237The maximum size is (15*(2^38))-1 bytes, which is almost 1.875 TB.
238
239To properly decode compressed data,
240a decoder will need to allocate a buffer of at least `windowSize` bytes.
241
242Note that `WD` byte is optional. It's not present in `single segment` mode.
243In which case, the maximum back-reference distance is the content size itself,
244which can be any value from 1 to 2^64-1 bytes (16 EB).
245
246In order to preserve decoder from unreasonable memory requirements,
247a decoder can refuse a compressed frame
248which requests a memory size beyond decoder's authorized range.
249
250For better interoperability,
251decoders are recommended to be compatible with window sizes of 8 MB.
252Encoders are recommended to not request more than 8 MB.
253It's merely a recommendation though,
254decoders are free to support larger or lower limits,
255depending on local limitations.
256
257__Frame Content Size__
258
259This is the original (uncompressed) size.
260This information is optional, and only present if associated flag is set.
261Content size is provided using 1, 2, 4 or 8 Bytes.
262Format is Little endian.
263
264| Field Size | Range |
265| ---------- | ---------- |
266| 0 | 0 |
267| 1 | 0 - 255 |
268| 2 | 256 - 65791|
269| 4 | 0 - 2^32-1 |
270| 8 | 0 - 2^64-1 |
271
272When field size is 1, 4 or 8 bytes, the value is read directly.
273When field size is 2, _an offset of 256 is added_.
274It's allowed to represent a small size (ex: `18`) using the 8-bytes variant.
275A size of `0` means `content size is unknown`.
276In which case, the `WD` byte will necessarily be present,
277and becomes the only hint to determine memory allocation.
278
279In order to preserve decoder from unreasonable memory requirement,
280a decoder can refuse a compressed frame
281which requests a memory size beyond decoder's authorized range.
282
283__Dictionary ID__
284
285This is a variable size field, which contains a single ID.
286It checks if the correct dictionary is used for decoding.
287Note that this field is optional. If it's not present,
288it's up to the caller to make sure it uses the correct dictionary.
289
290Field size depends on __Dictionary ID flag__.
2911 byte can represent an ID 0-255.
2922 bytes can represent an ID 0-65535.
2934 bytes can represent an ID 0-(2^32-1).
294
295It's allowed to represent a small ID (for example `13`)
296with a large 4-bytes dictionary ID, losing some efficiency in the process.
297
298
299Data Blocks
300-----------
301
302| B. Header | data |
303|:---------:| ------ |
304| 3 bytes | |
305
306
307__Block Header__
308
309This field uses 3-bytes, format is __big-endian__.
310
311The 2 highest bits represent the `block type`,
312while the remaining 22 bits represent the (compressed) block size.
313
314There are 4 block types :
315
316| Value | 0 | 1 | 2 | 3 |
317| ---------- | ---------- | --- | --- | ------- |
318| Block Type | Compressed | Raw | RLE | EndMark |
319
320- Compressed : this is a Zstandard compressed block,
321 detailed in a later part of this specification.
322 "block size" is the compressed size.
323 Decompressed size is unknown,
324 but its maximum possible value is guaranteed (see below)
325- Raw : this is an uncompressed block.
326 "block size" is the number of bytes to read and copy.
327- RLE : this is a single byte, repeated N times.
328 In which case, "block size" is the size to regenerate,
329 while the "compressed" block is just 1 byte (the byte to repeat).
330- EndMark : this is not a block. Signal the end of the frame.
331 The rest of the field may be optionally filled by a checksum
332 (see frame checksum).
333
334Block sizes must respect a few rules :
335- In compressed mode, compressed size if always strictly `< contentSize`.
336- Block decompressed size is necessarily <= maximum back-reference distance .
337- Block decompressed size is necessarily <= 128 KB
338
339
340__Data__
341
342Where the actual data to decode stands.
343It might be compressed or not, depending on previous field indications.
344A data block is not necessarily "full" :
345since an arbitrary flush may happen anytime,
346block content can be any size, up to Block Maximum Size.
347Block Maximum Size is the smallest of :
348- Max back-reference distance
349- 128 KB
350
351
352Skippable Frames
353----------------
354
355| Magic Number | Frame Size | User Data |
356|:------------:|:----------:| --------- |
357| 4 bytes | 4 bytes | |
358
359Skippable frames allow the insertion of user-defined data
360into a flow of concatenated frames.
361Its design is pretty straightforward,
362with the sole objective to allow the decoder to quickly skip
363over user-defined data and continue decoding.
364
365Skippable frames defined in this specification are compatible with LZ4 ones.
366
367
368__Magic Number__ :
369
3704 Bytes, Little endian format.
371Value : 0x184D2A5X, which means any value from 0x184D2A50 to 0x184D2A5F.
372All 16 values are valid to identify a skippable frame.
373
374__Frame Size__ :
375
376This is the size, in bytes, of the following User Data
377(without including the magic number nor the size field itself).
3784 Bytes, Little endian format, unsigned 32-bits.
379This means User Data cant be bigger than (2^32-1) Bytes.
380
381__User Data__ :
382
383User Data can be anything. Data will just be skipped by the decoder.
384
385
386Compressed block format
387-----------------------
388This specification details the content of a _compressed block_.
389A compressed block has a size, which must be known in order to decode it.
390It also has a guaranteed maximum regenerated size,
391in order to properly allocate destination buffer.
392See "Frame format" for more details.
393
394A compressed block consists of 2 sections :
395- Literals section
396- Sequences section
397
398### Compressed Literals
399
400Literals are compressed using order-0 huffman compression.
401During sequence phase, literals will be entangled with match copy operations.
402All literals are regrouped in the first part of the block.
403They can be decoded first, and then copied during sequence operations,
404or they can be decoded on the flow, as needed by sequences.
405
406| Header | (Tree Description) | Stream1 | (Stream2) | (Stream3) | (Stream4) |
407| ------ | ------------------ | ------- | --------- | --------- | --------- |
408
409Literals can be compressed, or uncompressed.
410When compressed, an optional tree description can be present,
411followed by 1 or 4 streams.
412
413#### Block Literal Header
414
415Header is in charge of describing precisely how literals are packed.
416It's a byte-aligned variable-size bitfield, ranging from 1 to 5 bytes,
417using big-endian convention.
418
419| BlockType | sizes format | (compressed size) | regenerated size |
420| --------- | ------------ | ----------------- | ---------------- |
421| 2 bits | 1 - 2 bits | 0 - 18 bits | 5 - 20 bits |
422
423__Block Type__ :
424
425This is a 2-bits field, describing 4 different block types :
426
427| Value | 0 | 1 | 2 | 3 |
428| ---------- | ---------- | ------ | --- | ------- |
429| Block Type | Compressed | Repeat | Raw | RLE |
430
431- Compressed : This is a standard huffman-compressed block,
432 starting with a huffman tree description.
433 See details below.
434- Repeat Stats : This is a huffman-compressed block,
435 using huffman tree from previous huffman-compressed block.
436 Huffman tree description will be skipped.
437 Compressed stream is equivalent to "compressed" block type.
438- Raw : Literals are stored uncompressed.
439- RLE : Literals consist of a single byte value repeated N times.
440
441__Sizes format__ :
442
443Sizes format are divided into 2 families :
444
445- For compressed block, it requires to decode both the compressed size
446 and the decompressed size. It will also decode the number of streams.
447- For Raw or RLE blocks, it's enough to decode the size to regenerate.
448
449For values spanning several bytes, convention is Big-endian.
450
451__Sizes format for Raw or RLE block__ :
452
453- Value : 0x : Regenerated size uses 5 bits (0-31).
454 Total literal header size is 1 byte.
455 `size = h[0] & 31;`
456- Value : 10 : Regenerated size uses 12 bits (0-4095).
457 Total literal header size is 2 bytes.
458 `size = ((h[0] & 15) << 8) + h[1];`
459- Value : 11 : Regenerated size uses 20 bits (0-1048575).
460 Total literal header size is 2 bytes.
461 `size = ((h[0] & 15) << 16) + (h[1]<<8) + h[2];`
462
463Note : it's allowed to represent a short value (ex : `13`)
464using a long format, accepting the reduced compacity.
465
466__Sizes format for Compressed Block__ :
467
468Note : also applicable to "repeat-stats" blocks.
469- Value : 00 : 4 streams
470 Compressed and regenerated sizes use 10 bits (0-1023)
471 Total literal header size is 3 bytes
472- Value : 01 : _Single stream_
473 Compressed and regenerated sizes use 10 bits (0-1023)
474 Total literal header size is 3 bytes
475- Value : 10 : 4 streams
476 Compressed and regenerated sizes use 14 bits (0-16383)
477 Total literal header size is 4 bytes
478- Value : 10 : 4 streams
479 Compressed and regenerated sizes use 18 bits (0-262143)
480 Total literal header size is 5 bytes
481
482
483
484Version changes
485---------------
4860.1 : initial release