4. Entropy Encoding Two types of entropy encoding are used by the Zstandard format: FSE and Huffman coding. Huffman is used to compress literals, while FSE is used for all other symbols (Literals_Length_Code, Match_Length_Code, and offset codes) and to compress Huffman headers.

4.1. FSE FSE, short for Finite State Entropy, is an entropy codec based on [ANS]. FSE encoding/decoding involves a state that is carried over between symbols, so decoding must be done in the opposite direction as encoding. Therefore, all FSE bitstreams are read from end to beginning. Note that the order of the bits in the stream is not reversed; they are simply read in the reverse order from which they were written. For additional details on FSE, see Finite State Entropy [FSE]. FSE decoding involves a decoding table that has a power of 2 size and contains three elements: Symbol, Num_Bits, and Baseline. The base 2 logarithm of the table size is its Accuracy_Log. An FSE state value represents an index in this table. To obtain the initial state value, consume Accuracy_Log bits from the stream as a little-endian value. The next symbol in the stream is the Symbol indicated in the table for that state. To obtain the next state value, the decoder should consume Num_Bits bits from the stream as a little-endian value and add it to Baseline. 4.1.1. FSE Table Description To decode FSE streams, it is necessary to construct the decoding table. The Zstandard format encodes FSE table descriptions as described here. An FSE distribution table describes the probabilities of all symbols from 0 to the last present one (included) on a normalized scale of (1 << Accuracy_Log). Note that there must be two or more symbols with non-zero probability. A bitstream is read forward, in little-endian fashion. It is not necessary to know its exact size, since the size will be discovered and reported by the decoding process. The bitstream starts by reporting on which scale it operates. If low4bits designates the lowest 4 bits of the first byte, then Accuracy_Log = low4bits + 5.

This is followed by each symbol value, from 0 to the last present one. The number of bits used by each field is variable and depends on: Remaining probabilities + 1: For example, presuming an Accuracy_Log of 8, and presuming 100 probabilities points have already been distributed, the decoder may read any value from 0 to (256 - 100 + 1) == 157, inclusive. Therefore, it must read log2sup(157) == 8 bits. Value decoded: Small values use 1 fewer bit. For example, presuming values from 0 to 157 (inclusive) are possible, 255 - 157 = 98 values are remaining in an 8-bit field. The first 98 values (hence from 0 to 97) use only 7 bits, and values from 98 to 157 use 8 bits. This is achieved through this scheme: +------------+---------------+-----------+ | Value Read | Value Decoded | Bits Used | +------------+---------------+-----------+ | 0 - 97 | 0 - 97 | 7 | +------------+---------------+-----------+ | 98 - 127 | 98 - 127 | 8 | +------------+---------------+-----------+ | 128 - 225 | 0 - 97 | 7 | +------------+---------------+-----------+ | 226 - 255 | 128 - 157 | 8 | +------------+---------------+-----------+ Symbol probabilities are read one by one, in order. The probability is obtained from Value decoded using the formula P = Value - 1. This means the value 0 becomes the negative probability -1. This is a special probability that means "less than 1". Its effect on the distribution table is described below. For the purpose of calculating total allocated probability points, it counts as 1. When a symbol has a probability of zero, it is followed by a 2-bit repeat flag. This repeat flag tells how many probabilities of zeroes follow the current one. It provides a number ranging from 0 to 3. If it is a 3, another 2-bit repeat flag follows, and so on. When the last symbol reaches a cumulated total of (1 << Accuracy_Log), decoding is complete. If the last symbol makes the cumulated total go above (1 << Accuracy_Log), distribution is considered corrupted.

Finally, the decoder can tell how many bytes were used in this process and how many symbols are present. The bitstream consumes a round number of bytes. Any remaining bit within the last byte is simply unused. The distribution of normalized probabilities is enough to create a unique decoding table. The table has a size of (1 << Accuracy_Log). Each cell describes the symbol decoded and instructions to get the next state. Symbols are scanned in their natural order for "less than 1" probabilities as described above. Symbols with this probability are being attributed a single cell, starting from the end of the table and retreating. These symbols define a full state reset, reading Accuracy_Log bits. All remaining symbols are allocated in their natural order. Starting from symbol 0 and table position 0, each symbol gets allocated as many cells as its probability. Cell allocation is spread, not linear; each successor position follows this rule: position += (tableSize >> 1) + (tableSize >> 3) + 3; position &= tableSize - 1; A position is skipped if it is already occupied by a "less than 1" probability symbol. Position does not reset between symbols; it simply iterates through each position in the table, switching to the next symbol when enough states have been allocated to the current one. The result is a list of state values. Each state will decode the current symbol. To get the Number_of_Bits and Baseline required for the next state, it is first necessary to sort all states in their natural order. The lower states will need 1 more bit than higher ones. The process is repeated for each symbol. For example, presuming a symbol has a probability of 5, it receives five state values. States are sorted in natural order. The next power of 2 is 8. The space of probabilities is divided into 8 equal parts. Presuming the Accuracy_Log is 7, this defines 128 states, and each share (divided by 8) is 16 in size. In order to reach 8, 8 - 5 = 3 lowest states will count "double", doubling the number of shares (32 in width), requiring 1 more bit in the process.

Baseline is assigned starting from the higher states using fewer bits, and proceeding naturally, then resuming at the first state, each taking its allocated width from Baseline. +----------------+-------+-------+--------+------+-------+ | state order | 0 | 1 | 2 | 3 | 4 | +----------------+-------+-------+--------+------+-------+ | width | 32 | 32 | 32 | 16 | 16 | +----------------+-------+-------+--------+------+-------+ | Number_of_Bits | 5 | 5 | 5 | 4 | 4 | +----------------+-------+-------+--------+------+-------+ | range number | 2 | 4 | 6 | 0 | 1 | +----------------+-------+-------+--------+------+-------+ | Baseline | 32 | 64 | 96 | 0 | 16 | +----------------+-------+-------+--------+------+-------+ | range | 32-63 | 64-95 | 96-127 | 0-15 | 16-31 | +----------------+-------+-------+--------+------+-------+ The next state is determined from the current state by reading the required Number_of_Bits and adding the specified Baseline. See Appendix A for the results of this process that are applied to the default distributions. 4.2. Huffman Coding Zstandard Huffman-coded streams are read backwards, similar to the FSE bitstreams. Therefore, to find the start of the bitstream, it is necessary to know the offset of the last byte of the Huffman-coded stream. After writing the last bit containing information, the compressor writes a single 1 bit and then fills the byte with 0-7 0 bits of padding. The last byte of the compressed bitstream cannot be 0 for that reason. When decompressing, the last byte containing the padding is the first byte to read. The decompressor needs to skip 0-7 initial 0 bits and the first 1 bit that occurs. Afterwards, the useful part of the bitstream begins. The bitstream contains Huffman-coded symbols in little-endian order, with the codes defined by the method below.

4.2.1. Huffman Tree Description Prefix coding represents symbols from an a priori known alphabet by bit sequences (codewords), one codeword for each symbol, in a manner such that different symbols may be represented by bit sequences of different lengths, but a parser can always parse an encoded string unambiguously symbol by symbol. Given an alphabet with known symbol frequencies, the Huffman algorithm allows the construction of an optimal prefix code using the fewest bits of any possible prefix codes for that alphabet. The prefix code must not exceed a maximum code length. More bits improve accuracy but yield a larger header size and require more memory or more complex decoding operations. This specification limits the maximum code length to 11 bits. All literal values from zero (included) to the last present one (excluded) are represented by Weight with values from 0 to Max_Number_of_Bits. Transformation from Weight to Number_of_Bits follows this pseudocode: if Weight == 0 Number_of_Bits = 0 else Number_of_Bits = Max_Number_of_Bits + 1 - Weight The last symbol's Weight is deduced from previously decoded ones, by completing to the nearest power of 2. This power of 2 gives Max_Number_of_Bits the depth of the current tree. For example, presume the following Huffman tree must be described: +---------------+----------------+ | Literal Value | Number_of_Bits | +---------------+----------------+ | 0 | 1 | +---------------+----------------+ | 1 | 2 | +---------------+----------------+ | 2 | 3 | +---------------+----------------+ | 3 | 0 | +---------------+----------------+ | 4 | 4 | +---------------+----------------+ | 5 | 4 | +---------------+----------------+

The tree depth is 4, since its longest element uses 4 bits. (The longest elements are those with the smallest frequencies.) Value 5 will not be listed as it can be determined from the values for 0-4, nor will values above 5 as they are all 0. Values from 0 to 4 will be listed using Weight instead of Number_of_Bits. The pseudocode to determine Weight is: if Number_of_Bits == 0 Weight = 0 else Weight = Max_Number_of_Bits + 1 - Number_of_Bits It gives the following series of weights: +---------------+--------+ | Literal Value | Weight | +---------------+--------+ | 0 | 4 | +---------------+--------+ | 1 | 3 | +---------------+--------+ | 2 | 2 | +---------------+--------+ | 3 | 0 | +---------------+--------+ | 4 | 1 | +---------------+--------+ The decoder will do the inverse operation: having collected weights of literals from 0 to 4, it knows the last literal, 5, is present with a non-zero Weight. The Weight of 5 can be determined by advancing to the next power of 2. The sum of 2^(Weight-1) (excluding 0's) is 15. The nearest power of 2 is 16. Therefore, Max_Number_of_Bits = 4 and Weight[5] = 16 - 15 = 1. 4.2.1.1. Huffman Tree Header This is a single byte value (0-255), which describes how the series of weights is encoded. headerByte < 128: The series of weights is compressed using FSE (see below). The length of the FSE-compressed series is equal to headerByte (0-127).

headerByte >= 128: This is a direct representation, where each Weight is written directly as a 4-bit field (0-15). They are encoded forward, 2 weights to a byte with the first weight taking the top 4 bits and the second taking the bottom 4; for example, the following operations could be used to read the weights: Weight[0] = (Byte[0] >> 4) Weight[1] = (Byte[0] & 0xf), etc. The full representation occupies ceiling(Number_of_Symbols/2) bytes, meaning it uses only full bytes even if Number_of_Symbols is odd. Number_of_Symbols = headerByte - 127. Note that maximum Number_of_Symbols is 255 - 127 = 128. If any literal has a value over 128, raw header mode is not possible, and it is necessary to use FSE compression. 4.2.1.2. FSE Compression of Huffman Weights In this case, the series of Huffman weights is compressed using FSE compression. It is a single bitstream with two interleaved states, sharing a single distribution table. To decode an FSE bitstream, it is necessary to know its compressed size. Compressed size is provided by headerByte. It's also necessary to know its maximum possible decompressed size, which is 255, since literal values span from 0 to 255, and the last symbol's Weight is not represented. An FSE bitstream starts by a header, describing probabilities distribution. It will create a decoding table. For a list of Huffman weights, the maximum accuracy log is 6 bits. For more details, see Section 4.1.1. The Huffman header compression uses two states, which share the same FSE distribution table. The first state (State1) encodes the even- numbered index symbols, and the second (State2) encodes the odd- numbered index symbols. State1 is initialized first, and then State2, and they take turns decoding a single symbol and updating their state. For more details on these FSE operations, see Section 4.1. The number of symbols to be decoded is determined by tracking the bitStream overflow condition: If updating state after decoding a symbol would require more bits than remain in the stream, it is assumed that extra bits are zero. Then, symbols for each of the final states are decoded and the process is complete.

4.2.1.3. Conversion from Weights to Huffman Prefix Codes All present symbols will now have a Weight value. It is possible to transform weights into Number_of_Bits, using this formula: if Weight > 0 Number_of_Bits = Max_Number_of_Bits + 1 - Weight else Number_of_Bits = 0 Symbols are sorted by Weight. Within the same Weight, symbols keep natural sequential order. Symbols with a Weight of zero are removed. Then, starting from the lowest Weight, prefix codes are distributed in sequential order. For example, assume the following list of weights has been decoded: +---------+--------+ | Literal | Weight | +---------+--------+ | 0 | 4 | +---------+--------+ | 1 | 3 | +---------+--------+ | 2 | 2 | +---------+--------+ | 3 | 0 | +---------+--------+ | 4 | 1 | +---------+--------+ | 5 | 1 | +---------+--------+

Sorting by weight and then the natural sequential order yields the following distribution: +---------+--------+----------------+--------------+ | Literal | Weight | Number_Of_Bits | Prefix Codes | +---------+--------+----------------|--------------+ | 3 | 0 | 0 | N/A | +---------+--------+----------------|--------------+ | 4 | 1 | 4 | 0000 | +---------+--------+----------------|--------------+ | 5 | 1 | 4 | 0001 | +---------+--------+----------------|--------------+ | 2 | 2 | 3 | 001 | +---------+--------+----------------|--------------+ | 1 | 3 | 2 | 01 | +---------+--------+----------------|--------------+ | 0 | 4 | 1 | 1 | +---------+--------+----------------|--------------+ 4.2.2. Huffman-Coded Streams Given a Huffman decoding table, it is possible to decode a Huffman- coded stream. Each bitstream must be read backward, which starts from the end and goes up to the beginning. Therefore, it is necessary to know the size of each bitstream. It is also necessary to know exactly which bit is the last. This is detected by a final bit flag: the highest bit of the last byte is a final-bit-flag. Consequently, a last byte of 0 is not possible. And the final-bit-flag itself is not part of the useful bitstream. Hence, the last byte contains between 0 and 7 useful bits. Starting from the end, it is possible to read the bitstream in a little-endian fashion, keeping track of already used bits. Since the bitstream is encoded in reverse order, starting from the end, read symbols in forward order.

For example, if the literal sequence "0145" was encoded using the above prefix code, it would be encoded (in reverse order) as: +---------+----------+ | Symbol | Encoding | +---------+----------+ | 5 | 0000 | +---------+----------+ | 4 | 0001 | +---------+----------+ | 1 | 01 | +---------+----------+ | 0 | 1 | +---------+----------+ | Padding | 00001 | +---------+----------+ This results in the following 2-byte bitstream: 00010000 00001101 Here is an alternative representation with the symbol codes separated by underscores: 0001_0000 00001_1_01 Reading the highest Max_Number_of_Bits bits, it's possible to compare the extracted value to the decoding table, determining the symbol to decode and number of bits to discard. The process continues reading up to the required number of symbols per stream. If a bitstream is not entirely and exactly consumed, hence reaching exactly its beginning position with all bits consumed, the decoding process is considered faulty. 5. Dictionary Format Zstandard is compatible with "raw content" dictionaries, free of any format restriction, except that they must be at least 8 bytes. These dictionaries function as if they were just the content part of a formatted dictionary. However, dictionaries created by "zstd --train" in the reference implementation follow a specific format, described here. Dictionaries are not included in the compressed content but rather are provided out of band. That is, the Dictionary_ID identifies which should be used, but this specification does not describe the

mechanism by which the dictionary is obtained prior to use during compression or decompression. A dictionary has a size, defined either by a buffer limit or a file size. The general format is: +--------------+---------------+----------------+---------+ | Magic_Number | Dictionary_ID | Entropy_Tables | Content | +--------------+---------------+----------------+---------+ Magic_Number: 4 bytes ID, value 0xEC30A437, little-endian format. Dictionary_ID: 4 bytes, stored in little-endian format. Dictionary_ID can be any value, except 0 (which means no Dictionary_ID). It is used by decoders to check if they use the correct dictionary. If the frame is going to be distributed in a private environment, any Dictionary_ID can be used. However, for public distribution of compressed frames, the following ranges are reserved and shall not be used: low range: <= 32767 high range: >= (2^31) Entropy_Tables: Follow the same format as the tables in compressed blocks. See the relevant FSE and Huffman sections for how to decode these tables. They are stored in the following order: Huffman table for literals, FSE table for offsets, FSE table for match lengths, and FSE table for literals lengths. These tables populate the Repeat Stats literals mode and Repeat distribution mode for sequence decoding. It is finally followed by 3 offset values, populating repeat offsets (instead of using {1,4,8}), stored in order, 4-bytes little-endian each, for a total of 12 bytes. Each repeat offset must have a value less than the dictionary size. Content: The rest of the dictionary is its content. The content acts as a "past" in front of data to be compressed or decompressed, so it can be referenced in sequence commands. As long as the amount of data decoded from this frame is less than or equal to Window_Size, sequence commands may specify offsets longer than the total length of decoded output so far to reference back to the dictionary, even parts of the dictionary with offsets larger than Window_Size. After the total output has surpassed Window_Size, however, this is no longer allowed, and the dictionary is no longer accessible.

6. IANA Considerations IANA has made two registrations, as described below. 6.1. The 'application/zstd' Media Type The 'application/zstd' media type identifies a block of data that is compressed using zstd compression. The data is a stream of bytes as described in this document. IANA has added the following to the "Media Types" registry: Type name: application Subtype name: zstd Required parameters: N/A Optional parameters: N/A Encoding considerations: binary Security considerations: See Section 7 of RFC 8478 Interoperability considerations: N/A Published specification: RFC 8478 Applications that use this media type: anywhere data size is an issue Additional information: Magic number(s): 4 bytes, little-endian format. Value: 0xFD2FB528 File extension(s): zst Macintosh file type code(s): N/A For further information: See [ZSTD] Intended usage: common Restrictions on usage: N/A Author: Murray S. Kucherawy Change Controller: IETF

Provisional registration: no 6.2. Content Encoding IANA has added the following entry to the "HTTP Content Coding Registry" within the "Hypertext Transfer Protocol (HTTP) Parameters" registry: Name: zstd Description: A stream of bytes compressed using the Zstandard protocol Pointer to specification text: RFC 8478 6.3. Dictionaries Work in progress includes development of dictionaries that will optimize compression and decompression of particular types of data. Specification of such dictionaries for public use will necessitate registration of a code point from the reserved range described in Section 3.1.1.1.3 and its association with a specific dictionary. However, there are at present no such dictionaries published for public use, so this document makes no immediate request of IANA to create such a registry. 7. Security Considerations Any data compression method involves the reduction of redundancy in the data. Zstandard is no exception, and the usual precautions apply. One should never compress a message whose content must remain secret with a message generated by a third party. Such a compression can be used to guess the content of the secret message through analysis of entropy reduction. This was demonstrated in the Compression Ratio Info-leak Made Easy (CRIME) attack [CRIME], for example. A decoder has to demonstrate capabilities to detect and prevent any kind of data tampering in the compressed frame from triggering system faults, such as reading or writing beyond allowed memory ranges. This can be guaranteed by either the implementation language or careful bound checkings. Of particular note is the encoding of Number_of_Sequences values that cause the decoder to read into the block header (and beyond), as well as the indication of a Frame_Content_Size that is smaller than the actual decompressed data, in an attempt to trigger a buffer overflow. It is highly recommended

to fuzz-test (i.e., provide invalid, unexpected, or random input and verify safe operation of) decoder implementations to test and harden their capability to detect bad frames and deal with them without any adverse system side effect. An attacker may provide correctly formed compressed frames with unreasonable memory requirements. A decoder must always control memory requirements and enforce some (system-specific) limits in order to protect memory usage from such scenarios. Compression can be optimized by training a dictionary on a variety of related content payloads. This dictionary must then be available at the decoder for decompression of the payload to be possible. While this document does not specify how to acquire a dictionary for a given compressed payload, it is worth noting that third-party dictionaries may interact unexpectedly with a decoder, leading to possible memory or other resource exhaustion attacks. We expect such topics to be discussed in further detail in the Security Considerations section of a forthcoming RFC for dictionary acquisition and transmission, but highlight this issue now out of an abundance of caution. As discussed in Section 3.1.2, it is possible to store arbitrary user metadata in skippable frames. While such frames are ignored during decompression of the data, they can be used as a watermark to track the path of the compressed payload. 8. Implementation Status Source code for a C language implementation of a Zstandard-compliant library is available at [ZSTD-GITHUB]. This implementation is considered to be the reference implementation and is production ready; it implements the full range of the specification. It is routinely tested against security hazards and widely deployed within Facebook infrastructure. The reference version is optimized for speed and is highly portable. It has been proven to run safely on multiple architectures (e.g., x86, x64, ARM, MIPS, PowerPC, IA64) featuring 32- or 64-bit addressing schemes, a little- or big-endian storage scheme, a number of different operating systems (e.g., UNIX (including Linux, BSD, OS-X, and Solaris) and Windows), and a number of compilers (e.g., gcc, clang, visual, and icc).

9. References 9.1. Normative References [ZSTD] "Zstandard", <http://www.zstd.net>. 9.2. Informative References [ANS] Duda, J., "Asymmetric numeral systems: entropy coding combining speed of Huffman coding with compression rate of arithmetic coding", January 2014, <https://arxiv.org/pdf/1311.2540>. [CRIME] "CRIME", June 2018, <https://en.wikipedia.org/w/ index.php?title=CRIME&oldid=844538656>. [FSE] "FiniteStateEntropy", commit 6efa78a, June 2018, <https://github.com/Cyan4973/FiniteStateEntropy/>. [LZ4] "LZ4 Frame Format Description", commit d03224b, January 2018, <https://github.com/lz4/lz4/blob/master/doc/ lz4_Frame_format.md>. [RFC1952] Deutsch, P., "GZIP file format specification version 4.3", RFC 1952, DOI 10.17487/RFC1952, May 1996, <https://www.rfc-editor.org/info/rfc1952>. [XXHASH] "XXHASH Algorithm", <http://www.xxhash.org>. [ZSTD-GITHUB] "zstd", commit 8514bd8, August 2018, <https://github.com/facebook/zstd>.

Appendix A. Decoding Tables for Predefined Codes This appendix contains FSE decoding tables for the predefined literal length, match length, and offset codes. The tables have been constructed using the algorithm as given above in Section 4.1.1. The tables here can be used as examples to crosscheck that an implementation has built its decoding tables correctly. A.1. Literal Length Code Table +-------+--------+----------------+------+ | State | Symbol | Number_Of_Bits | Base | +-------+--------+----------------+------+ | 0 | 0 | 0 | 0 | +-------+--------+----------------+------+ | 0 | 0 | 4 | 0 | +-------+--------+----------------+------+ | 1 | 0 | 4 | 16 | +-------+--------+----------------+------+ | 2 | 1 | 5 | 32 | +-------+--------+----------------+------+ | 3 | 3 | 5 | 0 | +-------+--------+----------------+------+ | 4 | 4 | 5 | 0 | +-------+--------+----------------+------+ | 5 | 6 | 5 | 0 | +-------+--------+----------------+------+ | 6 | 7 | 5 | 0 | +-------+--------+----------------+------+ | 7 | 9 | 5 | 0 | +-------+--------+----------------+------+ | 8 | 10 | 5 | 0 | +-------+--------+----------------+------+ | 9 | 12 | 5 | 0 | +-------+--------+----------------+------+ | 10 | 14 | 6 | 0 | +-------+--------+----------------+------+ | 11 | 16 | 5 | 0 | +-------+--------+----------------+------+ | 12 | 18 | 5 | 0 | +-------+--------+----------------+------+ | 13 | 19 | 5 | 0 | +-------+--------+----------------+------+ | 14 | 21 | 5 | 0 | +-------+--------+----------------+------+ | 15 | 22 | 5 | 0 | +-------+--------+----------------+------+ | 16 | 24 | 5 | 0 |

+-------+--------+----------------+------+ | 17 | 25 | 5 | 32 | +-------+--------+----------------+------+ | 18 | 26 | 5 | 0 | +-------+--------+----------------+------+ | 19 | 27 | 6 | 0 | +-------+--------+----------------+------+ | 20 | 29 | 6 | 0 | +-------+--------+----------------+------+ | 21 | 31 | 6 | 0 | +-------+--------+----------------+------+ | 22 | 0 | 4 | 32 | +-------+--------+----------------+------+ | 23 | 1 | 4 | 0 | +-------+--------+----------------+------+ | 24 | 2 | 5 | 0 | +-------+--------+----------------+------+ | 25 | 4 | 5 | 32 | +-------+--------+----------------+------+ | 26 | 5 | 5 | 0 | +-------+--------+----------------+------+ | 27 | 7 | 5 | 32 | +-------+--------+----------------+------+ | 28 | 8 | 5 | 0 | +-------+--------+----------------+------+ | 29 | 10 | 5 | 32 | +-------+--------+----------------+------+ | 30 | 11 | 5 | 0 | +-------+--------+----------------+------+ | 31 | 13 | 6 | 0 | +-------+--------+----------------+------+ | 32 | 16 | 5 | 32 | +-------+--------+----------------+------+ | 33 | 17 | 5 | 0 | +-------+--------+----------------+------+ | 34 | 19 | 5 | 32 | +-------+--------+----------------+------+ | 35 | 20 | 5 | 0 | +-------+--------+----------------+------+ | 36 | 22 | 5 | 32 | +-------+--------+----------------+------+ | 37 | 23 | 5 | 0 | +-------+--------+----------------+------+ | 38 | 25 | 4 | 0 | +-------+--------+----------------+------+ | 39 | 25 | 4 | 16 | +-------+--------+----------------+------+ | 40 | 26 | 5 | 32 |

+-------+--------+----------------+------+ | 41 | 28 | 6 | 0 | +-------+--------+----------------+------+ | 42 | 30 | 6 | 0 | +-------+--------+----------------+------+ | 43 | 0 | 4 | 48 | +-------+--------+----------------+------+ | 44 | 1 | 4 | 16 | +-------+--------+----------------+------+ | 45 | 2 | 5 | 32 | +-------+--------+----------------+------+ | 46 | 3 | 5 | 32 | +-------+--------+----------------+------+ | 47 | 5 | 5 | 32 | +-------+--------+----------------+------+ | 48 | 6 | 5 | 32 | +-------+--------+----------------+------+ | 49 | 8 | 5 | 32 | +-------+--------+----------------+------+ | 50 | 9 | 5 | 32 | +-------+--------+----------------+------+ | 51 | 11 | 5 | 32 | +-------+--------+----------------+------+ | 52 | 12 | 5 | 32 | +-------+--------+----------------+------+ | 53 | 15 | 6 | 0 | +-------+--------+----------------+------+ | 54 | 17 | 5 | 32 | +-------+--------+----------------+------+ | 55 | 18 | 5 | 32 | +-------+--------+----------------+------+ | 56 | 20 | 5 | 32 | +-------+--------+----------------+------+ | 57 | 21 | 5 | 32 | +-------+--------+----------------+------+ | 58 | 23 | 5 | 32 | +-------+--------+----------------+------+ | 59 | 24 | 5 | 32 | +-------+--------+----------------+------+ | 60 | 35 | 6 | 0 | +-------+--------+----------------+------+ | 61 | 34 | 6 | 0 | +-------+--------+----------------+------+ | 62 | 33 | 6 | 0 | +-------+--------+----------------+------+ | 63 | 32 | 6 | 0 | +-------+--------+----------------+------+

A.2. Match Length Code Table +-------+--------+----------------+------+ | State | Symbol | Number_Of_Bits | Base | +-------+--------+----------------+------+ | 0 | 0 | 0 | 0 | +-------+--------+----------------+------+ | 0 | 0 | 6 | 0 | +-------+--------+----------------+------+ | 1 | 1 | 4 | 0 | +-------+--------+----------------+------+ | 2 | 2 | 5 | 32 | +-------+--------+----------------+------+ | 3 | 3 | 5 | 0 | +-------+--------+----------------+------+ | 4 | 5 | 5 | 0 | +-------+--------+----------------+------+ | 5 | 6 | 5 | 0 | +-------+--------+----------------+------+ | 6 | 8 | 5 | 0 | +-------+--------+----------------+------+ | 7 | 10 | 6 | 0 | +-------+--------+----------------+------+ | 8 | 13 | 6 | 0 | +-------+--------+----------------+------+ | 9 | 16 | 6 | 0 | +-------+--------+----------------+------+ | 10 | 19 | 6 | 0 | +-------+--------+----------------+------+ | 11 | 22 | 6 | 0 | +-------+--------+----------------+------+ | 12 | 25 | 6 | 0 | +-------+--------+----------------+------+ | 13 | 28 | 6 | 0 | +-------+--------+----------------+------+ | 14 | 31 | 6 | 0 | +-------+--------+----------------+------+ | 15 | 33 | 6 | 0 | +-------+--------+----------------+------+ | 16 | 35 | 6 | 0 | +-------+--------+----------------+------+ | 17 | 37 | 6 | 0 | +-------+--------+----------------+------+ | 18 | 39 | 6 | 0 | +-------+--------+----------------+------+ | 19 | 41 | 6 | 0 | +-------+--------+----------------+------+ | 20 | 43 | 6 | 0 |

+-------+--------+----------------+------+ | 21 | 45 | 6 | 0 | +-------+--------+----------------+------+ | 22 | 1 | 4 | 16 | +-------+--------+----------------+------+ | 23 | 2 | 4 | 0 | +-------+--------+----------------+------+ | 24 | 3 | 5 | 32 | +-------+--------+----------------+------+ | 25 | 4 | 5 | 0 | +-------+--------+----------------+------+ | 26 | 6 | 5 | 32 | +-------+--------+----------------+------+ | 27 | 7 | 5 | 0 | +-------+--------+----------------+------+ | 28 | 9 | 6 | 0 | +-------+--------+----------------+------+ | 29 | 12 | 6 | 0 | +-------+--------+----------------+------+ | 30 | 15 | 6 | 0 | +-------+--------+----------------+------+ | 31 | 18 | 6 | 0 | +-------+--------+----------------+------+ | 32 | 21 | 6 | 0 | +-------+--------+----------------+------+ | 33 | 24 | 6 | 0 | +-------+--------+----------------+------+ | 34 | 27 | 6 | 0 | +-------+--------+----------------+------+ | 35 | 30 | 6 | 0 | +-------+--------+----------------+------+ | 36 | 32 | 6 | 0 | +-------+--------+----------------+------+ | 37 | 34 | 6 | 0 | +-------+--------+----------------+------+ | 38 | 36 | 6 | 0 | +-------+--------+----------------+------+ | 39 | 38 | 6 | 0 | +-------+--------+----------------+------+ | 40 | 40 | 6 | 0 | +-------+--------+----------------+------+ | 41 | 42 | 6 | 0 | +-------+--------+----------------+------+ | 42 | 44 | 6 | 0 | +-------+--------+----------------+------+ | 43 | 1 | 4 | 32 | +-------+--------+----------------+------+ | 44 | 1 | 4 | 48 |

+-------+--------+----------------+------+ | 45 | 2 | 4 | 16 | +-------+--------+----------------+------+ | 46 | 4 | 5 | 32 | +-------+--------+----------------+------+ | 47 | 5 | 5 | 32 | +-------+--------+----------------+------+ | 48 | 7 | 5 | 32 | +-------+--------+----------------+------+ | 49 | 8 | 5 | 32 | +-------+--------+----------------+------+ | 50 | 11 | 6 | 0 | +-------+--------+----------------+------+ | 51 | 14 | 6 | 0 | +-------+--------+----------------+------+ | 52 | 17 | 6 | 0 | +-------+--------+----------------+------+ | 53 | 20 | 6 | 0 | +-------+--------+----------------+------+ | 54 | 23 | 6 | 0 | +-------+--------+----------------+------+ | 55 | 26 | 6 | 0 | +-------+--------+----------------+------+ | 56 | 29 | 6 | 0 | +-------+--------+----------------+------+ | 57 | 52 | 6 | 0 | +-------+--------+----------------+------+ | 58 | 51 | 6 | 0 | +-------+--------+----------------+------+ | 59 | 50 | 6 | 0 | +-------+--------+----------------+------+ | 60 | 49 | 6 | 0 | +-------+--------+----------------+------+ | 61 | 48 | 6 | 0 | +-------+--------+----------------+------+ | 62 | 47 | 6 | 0 | +-------+--------+----------------+------+ | 63 | 46 | 6 | 0 | +-------+--------+----------------+------+

A.3. Offset Code Table +-------+--------+----------------+------+ | State | Symbol | Number_Of_Bits | Base | +-------+--------+----------------+------+ | 0 | 0 | 0 | 0 | +-------+--------+----------------+------+ | 0 | 0 | 5 | 0 | +-------+--------+----------------+------+ | 1 | 6 | 4 | 0 | +-------+--------+----------------+------+ | 2 | 9 | 5 | 0 | +-------+--------+----------------+------+ | 3 | 15 | 5 | 0 | +-------+--------+----------------+------+ | 4 | 21 | 5 | 0 | +-------+--------+----------------+------+ | 5 | 3 | 5 | 0 | +-------+--------+----------------+------+ | 6 | 7 | 4 | 0 | +-------+--------+----------------+------+ | 7 | 12 | 5 | 0 | +-------+--------+----------------+------+ | 8 | 18 | 5 | 0 | +-------+--------+----------------+------+ | 9 | 23 | 5 | 0 | +-------+--------+----------------+------+ | 10 | 5 | 5 | 0 | +-------+--------+----------------+------+ | 11 | 8 | 4 | 0 | +-------+--------+----------------+------+ | 12 | 14 | 5 | 0 | +-------+--------+----------------+------+ | 13 | 20 | 5 | 0 | +-------+--------+----------------+------+ | 14 | 2 | 5 | 0 | +-------+--------+----------------+------+ | 15 | 7 | 4 | 16 | +-------+--------+----------------+------+ | 16 | 11 | 5 | 0 | +-------+--------+----------------+------+ | 17 | 17 | 5 | 0 | +-------+--------+----------------+------+ | 18 | 22 | 5 | 0 | +-------+--------+----------------+------+ | 19 | 4 | 5 | 0 | +-------+--------+----------------+------+ | 20 | 8 | 4 | 16 |

+-------+--------+----------------+------+ | 21 | 13 | 5 | 0 | +-------+--------+----------------+------+ | 22 | 19 | 5 | 0 | +-------+--------+----------------+------+ | 23 | 1 | 5 | 0 | +-------+--------+----------------+------+ | 24 | 6 | 4 | 16 | +-------+--------+----------------+------+ | 25 | 10 | 5 | 0 | +-------+--------+----------------+------+ | 26 | 16 | 5 | 0 | +-------+--------+----------------+------+ | 27 | 28 | 5 | 0 | +-------+--------+----------------+------+ | 28 | 27 | 5 | 0 | +-------+--------+----------------+------+ | 29 | 26 | 5 | 0 | +-------+--------+----------------+------+ | 30 | 25 | 5 | 0 | +-------+--------+----------------+------+ | 31 | 24 | 5 | 0 | +-------+--------+----------------+------+ Acknowledgments zstd was developed by Yann Collet. Bobo Bose-Kolanu, Felix Handte, Kyle Nekritz, Nick Terrell, and David Schleimer provided helpful feedback during the development of this document.

Authors' Addresses Yann Collet Facebook 1 Hacker Way Menlo Park, CA 94025 United States of America Email: cyan@fb.com Murray S. Kucherawy (editor) Facebook 1 Hacker Way Menlo Park, CA 94025 United States of America Email: msk@fb.com