// SPDX-License-Identifier: GPL-2.0-or-later /* * decompress_common.c - Code shared by the XPRESS and LZX decompressors * * Copyright (C) 2015 Eric Biggers */ #include "decompress_common.h" /* * make_huffman_decode_table() - * * Build a decoding table for a canonical prefix code, or "Huffman code". * * This is an internal function, not part of the library API! * * This takes as input the length of the codeword for each symbol in the * alphabet and produces as output a table that can be used for fast * decoding of prefix-encoded symbols using read_huffsym(). * * Strictly speaking, a canonical prefix code might not be a Huffman * code. But this algorithm will work either way; and in fact, since * Huffman codes are defined in terms of symbol frequencies, there is no * way for the decompressor to know whether the code is a true Huffman * code or not until all symbols have been decoded. * * Because the prefix code is assumed to be "canonical", it can be * reconstructed directly from the codeword lengths. A prefix code is * canonical if and only if a longer codeword never lexicographically * precedes a shorter codeword, and the lexicographic ordering of * codewords of the same length is the same as the lexicographic ordering * of the corresponding symbols. Consequently, we can sort the symbols * primarily by codeword length and secondarily by symbol value, then * reconstruct the prefix code by generating codewords lexicographically * in that order. * * This function does not, however, generate the prefix code explicitly. * Instead, it directly builds a table for decoding symbols using the * code. The basic idea is this: given the next 'max_codeword_len' bits * in the input, we can look up the decoded symbol by indexing a table * containing 2**max_codeword_len entries. A codeword with length * 'max_codeword_len' will have exactly one entry in this table, whereas * a codeword shorter than 'max_codeword_len' will have multiple entries * in this table. Precisely, a codeword of length n will be represented * by 2**(max_codeword_len - n) entries in this table. The 0-based index * of each such entry will contain the corresponding codeword as a prefix * when zero-padded on the left to 'max_codeword_len' binary digits. * * That's the basic idea, but we implement two optimizations regarding * the format of the decode table itself: * * - For many compression formats, the maximum codeword length is too * long for it to be efficient to build the full decoding table * whenever a new prefix code is used. Instead, we can build the table * using only 2**table_bits entries, where 'table_bits' is some number * less than or equal to 'max_codeword_len'. Then, only codewords of * length 'table_bits' and shorter can be directly looked up. For * longer codewords, the direct lookup instead produces the root of a * binary tree. Using this tree, the decoder can do traditional * bit-by-bit decoding of the remainder of the codeword. Child nodes * are allocated in extra entries at the end of the table; leaf nodes * contain symbols. Note that the long-codeword case is, in general, * not performance critical, since in Huffman codes the most frequently * used symbols are assigned the shortest codeword lengths. * * - When we decode a symbol using a direct lookup of the table, we still * need to know its length so that the bitstream can be advanced by the * appropriate number of bits. The simple solution is to simply retain * the 'lens' array and use the decoded symbol as an index into it. * However, this requires two separate array accesses in the fast path. * The optimization is to store the length directly in the decode * table. We use the bottom 11 bits for the symbol and the top 5 bits * for the length. In addition, to combine this optimization with the * previous one, we introduce a special case where the top 2 bits of * the length are both set if the entry is actually the root of a * binary tree. * * @decode_table: * The array in which to create the decoding table. This must have * a length of at least ((2**table_bits) + 2 * num_syms) entries. * * @num_syms: * The number of symbols in the alphabet; also, the length of the * 'lens' array. Must be less than or equal to 2048. * * @table_bits: * The order of the decode table size, as explained above. Must be * less than or equal to 13. * * @lens: * An array of length @num_syms, indexable by symbol, that gives the * length of the codeword, in bits, for that symbol. The length can * be 0, which means that the symbol does not have a codeword * assigned. * * @max_codeword_len: * The longest codeword length allowed in the compression format. * All entries in 'lens' must be less than or equal to this value. * This must be less than or equal to 23. * * @working_space * A temporary array of length '2 * (max_codeword_len + 1) + * num_syms'. * * Returns 0 on success, or -1 if the lengths do not form a valid prefix * code. */ int make_huffman_decode_table(u16 decode_table[], const u32 num_syms, const u32 table_bits, const u8 lens[], const u32 max_codeword_len, u16 working_space[]) { … }