Blame SOURCES/0006-fs-ntfs3-Add-compression.patch

Kmods SIG 8b815c
From 522e010b58379fbe19b38fdef5016bca0c3cf405 Mon Sep 17 00:00:00 2001
Kmods SIG 8b815c
From: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Kmods SIG 8b815c
Date: Fri, 13 Aug 2021 17:21:30 +0300
Kmods SIG 8b815c
Subject: [Backport 522e010b5837] src: Add compression
Kmods SIG 8b815c
Kmods SIG 8b815c
This patch adds different types of NTFS-applicable compressions:
Kmods SIG 8b815c
- lznt
Kmods SIG 8b815c
- lzx
Kmods SIG 8b815c
- xpress
Kmods SIG 8b815c
Latter two (lzx, xpress) implement Windows Compact OS feature and
Kmods SIG 8b815c
were taken from ntfs-3g system comression plugin authored by Eric Biggers
Kmods SIG 8b815c
(https://github.com/ebiggers/ntfs-3g-system-compression)
Kmods SIG 8b815c
which were ported to ntfs3 and adapted to Linux Kernel environment.
Kmods SIG 8b815c
Kmods SIG 8b815c
Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Kmods SIG 8b815c
---
Kmods SIG 8b815c
 src/lib/decompress_common.c | 332 +++++++++++++++
Kmods SIG 8b815c
 src/lib/decompress_common.h | 352 ++++++++++++++++
Kmods SIG 8b815c
 src/lib/lib.h               |  26 ++
Kmods SIG 8b815c
 src/lib/lzx_decompress.c    | 683 +++++++++++++++++++++++++++++++
Kmods SIG 8b815c
 src/lib/xpress_decompress.c | 155 +++++++
Kmods SIG 8b815c
 src/lznt.c                  | 452 ++++++++++++++++++++
Kmods SIG 8b815c
 6 files changed, 2000 insertions(+)
Kmods SIG 8b815c
 create mode 100644 src/lib/decompress_common.c
Kmods SIG 8b815c
 create mode 100644 src/lib/decompress_common.h
Kmods SIG 8b815c
 create mode 100644 src/lib/lib.h
Kmods SIG 8b815c
 create mode 100644 src/lib/lzx_decompress.c
Kmods SIG 8b815c
 create mode 100644 src/lib/xpress_decompress.c
Kmods SIG 8b815c
 create mode 100644 src/lznt.c
Kmods SIG 8b815c
Kmods SIG 8b815c
diff --git a/src/lib/decompress_common.c b/src/lib/decompress_common.c
Kmods SIG 8b815c
new file mode 100644
Kmods SIG 8b815c
index 0000000000000000000000000000000000000000..83c9e93aea77e437b5b1889b49272eee64d4df64
Kmods SIG 8b815c
--- /dev/null
Kmods SIG 8b815c
+++ b/src/lib/decompress_common.c
Kmods SIG 8b815c
@@ -0,0 +1,332 @@
Kmods SIG 8b815c
+// SPDX-License-Identifier: GPL-2.0-or-later
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * decompress_common.c - Code shared by the XPRESS and LZX decompressors
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Copyright (C) 2015 Eric Biggers
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is free software: you can redistribute it and/or modify it under
Kmods SIG 8b815c
+ * the terms of the GNU General Public License as published by the Free Software
Kmods SIG 8b815c
+ * Foundation, either version 2 of the License, or (at your option) any later
Kmods SIG 8b815c
+ * version.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is distributed in the hope that it will be useful, but WITHOUT
Kmods SIG 8b815c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Kmods SIG 8b815c
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
Kmods SIG 8b815c
+ * details.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * You should have received a copy of the GNU General Public License along with
Kmods SIG 8b815c
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+#include "decompress_common.h"
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * make_huffman_decode_table() -
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Build a decoding table for a canonical prefix code, or "Huffman code".
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This is an internal function, not part of the library API!
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This takes as input the length of the codeword for each symbol in the
Kmods SIG 8b815c
+ * alphabet and produces as output a table that can be used for fast
Kmods SIG 8b815c
+ * decoding of prefix-encoded symbols using read_huffsym().
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Strictly speaking, a canonical prefix code might not be a Huffman
Kmods SIG 8b815c
+ * code.  But this algorithm will work either way; and in fact, since
Kmods SIG 8b815c
+ * Huffman codes are defined in terms of symbol frequencies, there is no
Kmods SIG 8b815c
+ * way for the decompressor to know whether the code is a true Huffman
Kmods SIG 8b815c
+ * code or not until all symbols have been decoded.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Because the prefix code is assumed to be "canonical", it can be
Kmods SIG 8b815c
+ * reconstructed directly from the codeword lengths.  A prefix code is
Kmods SIG 8b815c
+ * canonical if and only if a longer codeword never lexicographically
Kmods SIG 8b815c
+ * precedes a shorter codeword, and the lexicographic ordering of
Kmods SIG 8b815c
+ * codewords of the same length is the same as the lexicographic ordering
Kmods SIG 8b815c
+ * of the corresponding symbols.  Consequently, we can sort the symbols
Kmods SIG 8b815c
+ * primarily by codeword length and secondarily by symbol value, then
Kmods SIG 8b815c
+ * reconstruct the prefix code by generating codewords lexicographically
Kmods SIG 8b815c
+ * in that order.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This function does not, however, generate the prefix code explicitly.
Kmods SIG 8b815c
+ * Instead, it directly builds a table for decoding symbols using the
Kmods SIG 8b815c
+ * code.  The basic idea is this: given the next 'max_codeword_len' bits
Kmods SIG 8b815c
+ * in the input, we can look up the decoded symbol by indexing a table
Kmods SIG 8b815c
+ * containing 2**max_codeword_len entries.  A codeword with length
Kmods SIG 8b815c
+ * 'max_codeword_len' will have exactly one entry in this table, whereas
Kmods SIG 8b815c
+ * a codeword shorter than 'max_codeword_len' will have multiple entries
Kmods SIG 8b815c
+ * in this table.  Precisely, a codeword of length n will be represented
Kmods SIG 8b815c
+ * by 2**(max_codeword_len - n) entries in this table.  The 0-based index
Kmods SIG 8b815c
+ * of each such entry will contain the corresponding codeword as a prefix
Kmods SIG 8b815c
+ * when zero-padded on the left to 'max_codeword_len' binary digits.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * That's the basic idea, but we implement two optimizations regarding
Kmods SIG 8b815c
+ * the format of the decode table itself:
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * - For many compression formats, the maximum codeword length is too
Kmods SIG 8b815c
+ *   long for it to be efficient to build the full decoding table
Kmods SIG 8b815c
+ *   whenever a new prefix code is used.  Instead, we can build the table
Kmods SIG 8b815c
+ *   using only 2**table_bits entries, where 'table_bits' is some number
Kmods SIG 8b815c
+ *   less than or equal to 'max_codeword_len'.  Then, only codewords of
Kmods SIG 8b815c
+ *   length 'table_bits' and shorter can be directly looked up.  For
Kmods SIG 8b815c
+ *   longer codewords, the direct lookup instead produces the root of a
Kmods SIG 8b815c
+ *   binary tree.  Using this tree, the decoder can do traditional
Kmods SIG 8b815c
+ *   bit-by-bit decoding of the remainder of the codeword.  Child nodes
Kmods SIG 8b815c
+ *   are allocated in extra entries at the end of the table; leaf nodes
Kmods SIG 8b815c
+ *   contain symbols.  Note that the long-codeword case is, in general,
Kmods SIG 8b815c
+ *   not performance critical, since in Huffman codes the most frequently
Kmods SIG 8b815c
+ *   used symbols are assigned the shortest codeword lengths.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * - When we decode a symbol using a direct lookup of the table, we still
Kmods SIG 8b815c
+ *   need to know its length so that the bitstream can be advanced by the
Kmods SIG 8b815c
+ *   appropriate number of bits.  The simple solution is to simply retain
Kmods SIG 8b815c
+ *   the 'lens' array and use the decoded symbol as an index into it.
Kmods SIG 8b815c
+ *   However, this requires two separate array accesses in the fast path.
Kmods SIG 8b815c
+ *   The optimization is to store the length directly in the decode
Kmods SIG 8b815c
+ *   table.  We use the bottom 11 bits for the symbol and the top 5 bits
Kmods SIG 8b815c
+ *   for the length.  In addition, to combine this optimization with the
Kmods SIG 8b815c
+ *   previous one, we introduce a special case where the top 2 bits of
Kmods SIG 8b815c
+ *   the length are both set if the entry is actually the root of a
Kmods SIG 8b815c
+ *   binary tree.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @decode_table:
Kmods SIG 8b815c
+ *	The array in which to create the decoding table.  This must have
Kmods SIG 8b815c
+ *	a length of at least ((2**table_bits) + 2 * num_syms) entries.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @num_syms:
Kmods SIG 8b815c
+ *	The number of symbols in the alphabet; also, the length of the
Kmods SIG 8b815c
+ *	'lens' array.  Must be less than or equal to 2048.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @table_bits:
Kmods SIG 8b815c
+ *	The order of the decode table size, as explained above.  Must be
Kmods SIG 8b815c
+ *	less than or equal to 13.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @lens:
Kmods SIG 8b815c
+ *	An array of length @num_syms, indexable by symbol, that gives the
Kmods SIG 8b815c
+ *	length of the codeword, in bits, for that symbol.  The length can
Kmods SIG 8b815c
+ *	be 0, which means that the symbol does not have a codeword
Kmods SIG 8b815c
+ *	assigned.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @max_codeword_len:
Kmods SIG 8b815c
+ *	The longest codeword length allowed in the compression format.
Kmods SIG 8b815c
+ *	All entries in 'lens' must be less than or equal to this value.
Kmods SIG 8b815c
+ *	This must be less than or equal to 23.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @working_space
Kmods SIG 8b815c
+ *	A temporary array of length '2 * (max_codeword_len + 1) +
Kmods SIG 8b815c
+ *	num_syms'.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Returns 0 on success, or -1 if the lengths do not form a valid prefix
Kmods SIG 8b815c
+ * code.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+int make_huffman_decode_table(u16 decode_table[], const u32 num_syms,
Kmods SIG 8b815c
+			      const u32 table_bits, const u8 lens[],
Kmods SIG 8b815c
+			      const u32 max_codeword_len,
Kmods SIG 8b815c
+			      u16 working_space[])
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	const u32 table_num_entries = 1 << table_bits;
Kmods SIG 8b815c
+	u16 * const len_counts = &working_space[0];
Kmods SIG 8b815c
+	u16 * const offsets = &working_space[1 * (max_codeword_len + 1)];
Kmods SIG 8b815c
+	u16 * const sorted_syms = &working_space[2 * (max_codeword_len + 1)];
Kmods SIG 8b815c
+	int left;
Kmods SIG 8b815c
+	void *decode_table_ptr;
Kmods SIG 8b815c
+	u32 sym_idx;
Kmods SIG 8b815c
+	u32 codeword_len;
Kmods SIG 8b815c
+	u32 stores_per_loop;
Kmods SIG 8b815c
+	u32 decode_table_pos;
Kmods SIG 8b815c
+	u32 len;
Kmods SIG 8b815c
+	u32 sym;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Count how many symbols have each possible codeword length.
Kmods SIG 8b815c
+	 * Note that a length of 0 indicates the corresponding symbol is not
Kmods SIG 8b815c
+	 * used in the code and therefore does not have a codeword.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	for (len = 0; len <= max_codeword_len; len++)
Kmods SIG 8b815c
+		len_counts[len] = 0;
Kmods SIG 8b815c
+	for (sym = 0; sym < num_syms; sym++)
Kmods SIG 8b815c
+		len_counts[lens[sym]]++;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* We can assume all lengths are <= max_codeword_len, but we
Kmods SIG 8b815c
+	 * cannot assume they form a valid prefix code.  A codeword of
Kmods SIG 8b815c
+	 * length n should require a proportion of the codespace equaling
Kmods SIG 8b815c
+	 * (1/2)^n.  The code is valid if and only if the codespace is
Kmods SIG 8b815c
+	 * exactly filled by the lengths, by this measure.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	left = 1;
Kmods SIG 8b815c
+	for (len = 1; len <= max_codeword_len; len++) {
Kmods SIG 8b815c
+		left <<= 1;
Kmods SIG 8b815c
+		left -= len_counts[len];
Kmods SIG 8b815c
+		if (left < 0) {
Kmods SIG 8b815c
+			/* The lengths overflow the codespace; that is, the code
Kmods SIG 8b815c
+			 * is over-subscribed.
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (left) {
Kmods SIG 8b815c
+		/* The lengths do not fill the codespace; that is, they form an
Kmods SIG 8b815c
+		 * incomplete set.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+		if (left == (1 << max_codeword_len)) {
Kmods SIG 8b815c
+			/* The code is completely empty.  This is arguably
Kmods SIG 8b815c
+			 * invalid, but in fact it is valid in LZX and XPRESS,
Kmods SIG 8b815c
+			 * so we must allow it.  By definition, no symbols can
Kmods SIG 8b815c
+			 * be decoded with an empty code.  Consequently, we
Kmods SIG 8b815c
+			 * technically don't even need to fill in the decode
Kmods SIG 8b815c
+			 * table.  However, to avoid accessing uninitialized
Kmods SIG 8b815c
+			 * memory if the algorithm nevertheless attempts to
Kmods SIG 8b815c
+			 * decode symbols using such a code, we zero out the
Kmods SIG 8b815c
+			 * decode table.
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+			memset(decode_table, 0,
Kmods SIG 8b815c
+			       table_num_entries * sizeof(decode_table[0]));
Kmods SIG 8b815c
+			return 0;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+		return -1;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Sort the symbols primarily by length and secondarily by symbol order.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Initialize 'offsets' so that offsets[len] for 1 <= len <=
Kmods SIG 8b815c
+	 * max_codeword_len is the number of codewords shorter than 'len' bits.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	offsets[1] = 0;
Kmods SIG 8b815c
+	for (len = 1; len < max_codeword_len; len++)
Kmods SIG 8b815c
+		offsets[len + 1] = offsets[len] + len_counts[len];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Use the 'offsets' array to sort the symbols.  Note that we do not
Kmods SIG 8b815c
+	 * include symbols that are not used in the code.  Consequently, fewer
Kmods SIG 8b815c
+	 * than 'num_syms' entries in 'sorted_syms' may be filled.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	for (sym = 0; sym < num_syms; sym++)
Kmods SIG 8b815c
+		if (lens[sym])
Kmods SIG 8b815c
+			sorted_syms[offsets[lens[sym]]++] = sym;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Fill entries for codewords with length <= table_bits
Kmods SIG 8b815c
+	 * --- that is, those short enough for a direct mapping.
Kmods SIG 8b815c
+	 *
Kmods SIG 8b815c
+	 * The table will start with entries for the shortest codeword(s), which
Kmods SIG 8b815c
+	 * have the most entries.  From there, the number of entries per
Kmods SIG 8b815c
+	 * codeword will decrease.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	decode_table_ptr = decode_table;
Kmods SIG 8b815c
+	sym_idx = 0;
Kmods SIG 8b815c
+	codeword_len = 1;
Kmods SIG 8b815c
+	stores_per_loop = (1 << (table_bits - codeword_len));
Kmods SIG 8b815c
+	for (; stores_per_loop != 0; codeword_len++, stores_per_loop >>= 1) {
Kmods SIG 8b815c
+		u32 end_sym_idx = sym_idx + len_counts[codeword_len];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		for (; sym_idx < end_sym_idx; sym_idx++) {
Kmods SIG 8b815c
+			u16 entry;
Kmods SIG 8b815c
+			u16 *p;
Kmods SIG 8b815c
+			u32 n;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			entry = ((u32)codeword_len << 11) | sorted_syms[sym_idx];
Kmods SIG 8b815c
+			p = (u16 *)decode_table_ptr;
Kmods SIG 8b815c
+			n = stores_per_loop;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			do {
Kmods SIG 8b815c
+				*p++ = entry;
Kmods SIG 8b815c
+			} while (--n);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			decode_table_ptr = p;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* If we've filled in the entire table, we are done.  Otherwise,
Kmods SIG 8b815c
+	 * there are codewords longer than table_bits for which we must
Kmods SIG 8b815c
+	 * generate binary trees.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	decode_table_pos = (u16 *)decode_table_ptr - decode_table;
Kmods SIG 8b815c
+	if (decode_table_pos != table_num_entries) {
Kmods SIG 8b815c
+		u32 j;
Kmods SIG 8b815c
+		u32 next_free_tree_slot;
Kmods SIG 8b815c
+		u32 cur_codeword;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* First, zero out the remaining entries.  This is
Kmods SIG 8b815c
+		 * necessary so that these entries appear as
Kmods SIG 8b815c
+		 * "unallocated" in the next part.  Each of these entries
Kmods SIG 8b815c
+		 * will eventually be filled with the representation of
Kmods SIG 8b815c
+		 * the root node of a binary tree.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+		j = decode_table_pos;
Kmods SIG 8b815c
+		do {
Kmods SIG 8b815c
+			decode_table[j] = 0;
Kmods SIG 8b815c
+		} while (++j != table_num_entries);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* We allocate child nodes starting at the end of the
Kmods SIG 8b815c
+		 * direct lookup table.  Note that there should be
Kmods SIG 8b815c
+		 * 2*num_syms extra entries for this purpose, although
Kmods SIG 8b815c
+		 * fewer than this may actually be needed.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+		next_free_tree_slot = table_num_entries;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Iterate through each codeword with length greater than
Kmods SIG 8b815c
+		 * 'table_bits', primarily in order of codeword length
Kmods SIG 8b815c
+		 * and secondarily in order of symbol.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+		for (cur_codeword = decode_table_pos << 1;
Kmods SIG 8b815c
+		     codeword_len <= max_codeword_len;
Kmods SIG 8b815c
+		     codeword_len++, cur_codeword <<= 1) {
Kmods SIG 8b815c
+			u32 end_sym_idx = sym_idx + len_counts[codeword_len];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			for (; sym_idx < end_sym_idx; sym_idx++, cur_codeword++) {
Kmods SIG 8b815c
+				/* 'sorted_sym' is the symbol represented by the
Kmods SIG 8b815c
+				 * codeword.
Kmods SIG 8b815c
+				 */
Kmods SIG 8b815c
+				u32 sorted_sym = sorted_syms[sym_idx];
Kmods SIG 8b815c
+				u32 extra_bits = codeword_len - table_bits;
Kmods SIG 8b815c
+				u32 node_idx = cur_codeword >> extra_bits;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+				/* Go through each bit of the current codeword
Kmods SIG 8b815c
+				 * beyond the prefix of length @table_bits and
Kmods SIG 8b815c
+				 * walk the appropriate binary tree, allocating
Kmods SIG 8b815c
+				 * any slots that have not yet been allocated.
Kmods SIG 8b815c
+				 *
Kmods SIG 8b815c
+				 * Note that the 'pointer' entry to the binary
Kmods SIG 8b815c
+				 * tree, which is stored in the direct lookup
Kmods SIG 8b815c
+				 * portion of the table, is represented
Kmods SIG 8b815c
+				 * identically to other internal (non-leaf)
Kmods SIG 8b815c
+				 * nodes of the binary tree; it can be thought
Kmods SIG 8b815c
+				 * of as simply the root of the tree.  The
Kmods SIG 8b815c
+				 * representation of these internal nodes is
Kmods SIG 8b815c
+				 * simply the index of the left child combined
Kmods SIG 8b815c
+				 * with the special bits 0xC000 to distingush
Kmods SIG 8b815c
+				 * the entry from direct mapping and leaf node
Kmods SIG 8b815c
+				 * entries.
Kmods SIG 8b815c
+				 */
Kmods SIG 8b815c
+				do {
Kmods SIG 8b815c
+					/* At least one bit remains in the
Kmods SIG 8b815c
+					 * codeword, but the current node is an
Kmods SIG 8b815c
+					 * unallocated leaf.  Change it to an
Kmods SIG 8b815c
+					 * internal node.
Kmods SIG 8b815c
+					 */
Kmods SIG 8b815c
+					if (decode_table[node_idx] == 0) {
Kmods SIG 8b815c
+						decode_table[node_idx] =
Kmods SIG 8b815c
+							next_free_tree_slot | 0xC000;
Kmods SIG 8b815c
+						decode_table[next_free_tree_slot++] = 0;
Kmods SIG 8b815c
+						decode_table[next_free_tree_slot++] = 0;
Kmods SIG 8b815c
+					}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+					/* Go to the left child if the next bit
Kmods SIG 8b815c
+					 * in the codeword is 0; otherwise go to
Kmods SIG 8b815c
+					 * the right child.
Kmods SIG 8b815c
+					 */
Kmods SIG 8b815c
+					node_idx = decode_table[node_idx] & 0x3FFF;
Kmods SIG 8b815c
+					--extra_bits;
Kmods SIG 8b815c
+					node_idx += (cur_codeword >> extra_bits) & 1;
Kmods SIG 8b815c
+				} while (extra_bits != 0);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+				/* We've traversed the tree using the entire
Kmods SIG 8b815c
+				 * codeword, and we're now at the entry where
Kmods SIG 8b815c
+				 * the actual symbol will be stored.  This is
Kmods SIG 8b815c
+				 * distinguished from internal nodes by not
Kmods SIG 8b815c
+				 * having its high two bits set.
Kmods SIG 8b815c
+				 */
Kmods SIG 8b815c
+				decode_table[node_idx] = sorted_sym;
Kmods SIG 8b815c
+			}
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+	return 0;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
diff --git a/src/lib/decompress_common.h b/src/lib/decompress_common.h
Kmods SIG 8b815c
new file mode 100644
Kmods SIG 8b815c
index 0000000000000000000000000000000000000000..66297f398403f13abe05f3b5af1aa7c5674351e8
Kmods SIG 8b815c
--- /dev/null
Kmods SIG 8b815c
+++ b/src/lib/decompress_common.h
Kmods SIG 8b815c
@@ -0,0 +1,352 @@
Kmods SIG 8b815c
+/* SPDX-License-Identifier: GPL-2.0-or-later */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * decompress_common.h - Code shared by the XPRESS and LZX decompressors
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Copyright (C) 2015 Eric Biggers
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is free software: you can redistribute it and/or modify it under
Kmods SIG 8b815c
+ * the terms of the GNU General Public License as published by the Free Software
Kmods SIG 8b815c
+ * Foundation, either version 2 of the License, or (at your option) any later
Kmods SIG 8b815c
+ * version.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is distributed in the hope that it will be useful, but WITHOUT
Kmods SIG 8b815c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Kmods SIG 8b815c
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
Kmods SIG 8b815c
+ * details.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * You should have received a copy of the GNU General Public License along with
Kmods SIG 8b815c
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+#include <linux/string.h>
Kmods SIG 8b815c
+#include <linux/compiler.h>
Kmods SIG 8b815c
+#include <linux/types.h>
Kmods SIG 8b815c
+#include <linux/slab.h>
Kmods SIG 8b815c
+#include <asm/unaligned.h>
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* "Force inline" macro (not required, but helpful for performance)  */
Kmods SIG 8b815c
+#define forceinline __always_inline
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Enable whole-word match copying on selected architectures  */
Kmods SIG 8b815c
+#if defined(__i386__) || defined(__x86_64__) || defined(__ARM_FEATURE_UNALIGNED)
Kmods SIG 8b815c
+#  define FAST_UNALIGNED_ACCESS
Kmods SIG 8b815c
+#endif
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Size of a machine word  */
Kmods SIG 8b815c
+#define WORDBYTES (sizeof(size_t))
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static forceinline void
Kmods SIG 8b815c
+copy_unaligned_word(const void *src, void *dst)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	put_unaligned(get_unaligned((const size_t *)src), (size_t *)dst);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Generate a "word" with platform-dependent size whose bytes all contain the
Kmods SIG 8b815c
+ * value 'b'.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline size_t repeat_byte(u8 b)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	size_t v;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	v = b;
Kmods SIG 8b815c
+	v |= v << 8;
Kmods SIG 8b815c
+	v |= v << 16;
Kmods SIG 8b815c
+	v |= v << ((WORDBYTES == 8) ? 32 : 0);
Kmods SIG 8b815c
+	return v;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Structure that encapsulates a block of in-memory data being interpreted as a
Kmods SIG 8b815c
+ * stream of bits, optionally with interwoven literal bytes.  Bits are assumed
Kmods SIG 8b815c
+ * to be stored in little endian 16-bit coding units, with the bits ordered high
Kmods SIG 8b815c
+ * to low.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+struct input_bitstream {
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Bits that have been read from the input buffer.  The bits are
Kmods SIG 8b815c
+	 * left-justified; the next bit is always bit 31.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	u32 bitbuf;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Number of bits currently held in @bitbuf.  */
Kmods SIG 8b815c
+	u32 bitsleft;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Pointer to the next byte to be retrieved from the input buffer.  */
Kmods SIG 8b815c
+	const u8 *next;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Pointer to just past the end of the input buffer.  */
Kmods SIG 8b815c
+	const u8 *end;
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Initialize a bitstream to read from the specified input buffer.  */
Kmods SIG 8b815c
+static forceinline void init_input_bitstream(struct input_bitstream *is,
Kmods SIG 8b815c
+					     const void *buffer, u32 size)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	is->bitbuf = 0;
Kmods SIG 8b815c
+	is->bitsleft = 0;
Kmods SIG 8b815c
+	is->next = buffer;
Kmods SIG 8b815c
+	is->end = is->next + size;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Ensure the bit buffer variable for the bitstream contains at least @num_bits
Kmods SIG 8b815c
+ * bits.  Following this, bitstream_peek_bits() and/or bitstream_remove_bits()
Kmods SIG 8b815c
+ * may be called on the bitstream to peek or remove up to @num_bits bits.  Note
Kmods SIG 8b815c
+ * that @num_bits must be <= 16.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline void bitstream_ensure_bits(struct input_bitstream *is,
Kmods SIG 8b815c
+					      u32 num_bits)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	if (is->bitsleft < num_bits) {
Kmods SIG 8b815c
+		if (is->end - is->next >= 2) {
Kmods SIG 8b815c
+			is->bitbuf |= (u32)get_unaligned_le16(is->next)
Kmods SIG 8b815c
+					<< (16 - is->bitsleft);
Kmods SIG 8b815c
+			is->next += 2;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+		is->bitsleft += 16;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Return the next @num_bits bits from the bitstream, without removing them.
Kmods SIG 8b815c
+ * There must be at least @num_bits remaining in the buffer variable, from a
Kmods SIG 8b815c
+ * previous call to bitstream_ensure_bits().
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline u32
Kmods SIG 8b815c
+bitstream_peek_bits(const struct input_bitstream *is, const u32 num_bits)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return (is->bitbuf >> 1) >> (sizeof(is->bitbuf) * 8 - num_bits - 1);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Remove @num_bits from the bitstream.  There must be at least @num_bits
Kmods SIG 8b815c
+ * remaining in the buffer variable, from a previous call to
Kmods SIG 8b815c
+ * bitstream_ensure_bits().
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline void
Kmods SIG 8b815c
+bitstream_remove_bits(struct input_bitstream *is, u32 num_bits)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	is->bitbuf <<= num_bits;
Kmods SIG 8b815c
+	is->bitsleft -= num_bits;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Remove and return @num_bits bits from the bitstream.  There must be at least
Kmods SIG 8b815c
+ * @num_bits remaining in the buffer variable, from a previous call to
Kmods SIG 8b815c
+ * bitstream_ensure_bits().
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline u32
Kmods SIG 8b815c
+bitstream_pop_bits(struct input_bitstream *is, u32 num_bits)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	u32 bits = bitstream_peek_bits(is, num_bits);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	bitstream_remove_bits(is, num_bits);
Kmods SIG 8b815c
+	return bits;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read and return the next @num_bits bits from the bitstream.  */
Kmods SIG 8b815c
+static forceinline u32
Kmods SIG 8b815c
+bitstream_read_bits(struct input_bitstream *is, u32 num_bits)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	bitstream_ensure_bits(is, num_bits);
Kmods SIG 8b815c
+	return bitstream_pop_bits(is, num_bits);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read and return the next literal byte embedded in the bitstream.  */
Kmods SIG 8b815c
+static forceinline u8
Kmods SIG 8b815c
+bitstream_read_byte(struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	if (unlikely(is->end == is->next))
Kmods SIG 8b815c
+		return 0;
Kmods SIG 8b815c
+	return *is->next++;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read and return the next 16-bit integer embedded in the bitstream.  */
Kmods SIG 8b815c
+static forceinline u16
Kmods SIG 8b815c
+bitstream_read_u16(struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	u16 v;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (unlikely(is->end - is->next < 2))
Kmods SIG 8b815c
+		return 0;
Kmods SIG 8b815c
+	v = get_unaligned_le16(is->next);
Kmods SIG 8b815c
+	is->next += 2;
Kmods SIG 8b815c
+	return v;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read and return the next 32-bit integer embedded in the bitstream.  */
Kmods SIG 8b815c
+static forceinline u32
Kmods SIG 8b815c
+bitstream_read_u32(struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	u32 v;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (unlikely(is->end - is->next < 4))
Kmods SIG 8b815c
+		return 0;
Kmods SIG 8b815c
+	v = get_unaligned_le32(is->next);
Kmods SIG 8b815c
+	is->next += 4;
Kmods SIG 8b815c
+	return v;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read into @dst_buffer an array of literal bytes embedded in the bitstream.
Kmods SIG 8b815c
+ * Return either a pointer to the byte past the last written, or NULL if the
Kmods SIG 8b815c
+ * read overflows the input buffer.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline void *bitstream_read_bytes(struct input_bitstream *is,
Kmods SIG 8b815c
+					      void *dst_buffer, size_t count)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	if ((size_t)(is->end - is->next) < count)
Kmods SIG 8b815c
+		return NULL;
Kmods SIG 8b815c
+	memcpy(dst_buffer, is->next, count);
Kmods SIG 8b815c
+	is->next += count;
Kmods SIG 8b815c
+	return (u8 *)dst_buffer + count;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Align the input bitstream on a coding-unit boundary.  */
Kmods SIG 8b815c
+static forceinline void bitstream_align(struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	is->bitsleft = 0;
Kmods SIG 8b815c
+	is->bitbuf = 0;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+extern int make_huffman_decode_table(u16 decode_table[], const u32 num_syms,
Kmods SIG 8b815c
+				     const u32 num_bits, const u8 lens[],
Kmods SIG 8b815c
+				     const u32 max_codeword_len,
Kmods SIG 8b815c
+				     u16 working_space[]);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Reads and returns the next Huffman-encoded symbol from a bitstream.  If the
Kmods SIG 8b815c
+ * input data is exhausted, the Huffman symbol is decoded as if the missing bits
Kmods SIG 8b815c
+ * are all zeroes.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline u32 read_huffsym(struct input_bitstream *istream,
Kmods SIG 8b815c
+					 const u16 decode_table[],
Kmods SIG 8b815c
+					 u32 table_bits,
Kmods SIG 8b815c
+					 u32 max_codeword_len)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	u32 entry;
Kmods SIG 8b815c
+	u32 key_bits;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	bitstream_ensure_bits(istream, max_codeword_len);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Index the decode table by the next table_bits bits of the input.  */
Kmods SIG 8b815c
+	key_bits = bitstream_peek_bits(istream, table_bits);
Kmods SIG 8b815c
+	entry = decode_table[key_bits];
Kmods SIG 8b815c
+	if (entry < 0xC000) {
Kmods SIG 8b815c
+		/* Fast case: The decode table directly provided the
Kmods SIG 8b815c
+		 * symbol and codeword length.  The low 11 bits are the
Kmods SIG 8b815c
+		 * symbol, and the high 5 bits are the codeword length.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+		bitstream_remove_bits(istream, entry >> 11);
Kmods SIG 8b815c
+		return entry & 0x7FF;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+	/* Slow case: The codeword for the symbol is longer than
Kmods SIG 8b815c
+	 * table_bits, so the symbol does not have an entry
Kmods SIG 8b815c
+	 * directly in the first (1 << table_bits) entries of the
Kmods SIG 8b815c
+	 * decode table.  Traverse the appropriate binary tree
Kmods SIG 8b815c
+	 * bit-by-bit to decode the symbol.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	bitstream_remove_bits(istream, table_bits);
Kmods SIG 8b815c
+	do {
Kmods SIG 8b815c
+		key_bits = (entry & 0x3FFF) + bitstream_pop_bits(istream, 1);
Kmods SIG 8b815c
+	} while ((entry = decode_table[key_bits]) >= 0xC000);
Kmods SIG 8b815c
+	return entry;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * Copy an LZ77 match at (dst - offset) to dst.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * The length and offset must be already validated --- that is, (dst - offset)
Kmods SIG 8b815c
+ * can't underrun the output buffer, and (dst + length) can't overrun the output
Kmods SIG 8b815c
+ * buffer.  Also, the length cannot be 0.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @bufend points to the byte past the end of the output buffer.  This function
Kmods SIG 8b815c
+ * won't write any data beyond this position.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Returns dst + length.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static forceinline u8 *lz_copy(u8 *dst, u32 length, u32 offset, const u8 *bufend,
Kmods SIG 8b815c
+			       u32 min_length)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	const u8 *src = dst - offset;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/*
Kmods SIG 8b815c
+	 * Try to copy one machine word at a time.  On i386 and x86_64 this is
Kmods SIG 8b815c
+	 * faster than copying one byte at a time, unless the data is
Kmods SIG 8b815c
+	 * near-random and all the matches have very short lengths.  Note that
Kmods SIG 8b815c
+	 * since this requires unaligned memory accesses, it won't necessarily
Kmods SIG 8b815c
+	 * be faster on every architecture.
Kmods SIG 8b815c
+	 *
Kmods SIG 8b815c
+	 * Also note that we might copy more than the length of the match.  For
Kmods SIG 8b815c
+	 * example, if a word is 8 bytes and the match is of length 5, then
Kmods SIG 8b815c
+	 * we'll simply copy 8 bytes.  This is okay as long as we don't write
Kmods SIG 8b815c
+	 * beyond the end of the output buffer, hence the check for (bufend -
Kmods SIG 8b815c
+	 * end >= WORDBYTES - 1).
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+#ifdef FAST_UNALIGNED_ACCESS
Kmods SIG 8b815c
+	u8 * const end = dst + length;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (bufend - end >= (ptrdiff_t)(WORDBYTES - 1)) {
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (offset >= WORDBYTES) {
Kmods SIG 8b815c
+			/* The source and destination words don't overlap.  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* To improve branch prediction, one iteration of this
Kmods SIG 8b815c
+			 * loop is unrolled.  Most matches are short and will
Kmods SIG 8b815c
+			 * fail the first check.  But if that check passes, then
Kmods SIG 8b815c
+			 * it becomes increasing likely that the match is long
Kmods SIG 8b815c
+			 * and we'll need to continue copying.
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			copy_unaligned_word(src, dst);
Kmods SIG 8b815c
+			src += WORDBYTES;
Kmods SIG 8b815c
+			dst += WORDBYTES;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (dst < end) {
Kmods SIG 8b815c
+				do {
Kmods SIG 8b815c
+					copy_unaligned_word(src, dst);
Kmods SIG 8b815c
+					src += WORDBYTES;
Kmods SIG 8b815c
+					dst += WORDBYTES;
Kmods SIG 8b815c
+				} while (dst < end);
Kmods SIG 8b815c
+			}
Kmods SIG 8b815c
+			return end;
Kmods SIG 8b815c
+		} else if (offset == 1) {
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* Offset 1 matches are equivalent to run-length
Kmods SIG 8b815c
+			 * encoding of the previous byte.  This case is common
Kmods SIG 8b815c
+			 * if the data contains many repeated bytes.
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+			size_t v = repeat_byte(*(dst - 1));
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			do {
Kmods SIG 8b815c
+				put_unaligned(v, (size_t *)dst);
Kmods SIG 8b815c
+				src += WORDBYTES;
Kmods SIG 8b815c
+				dst += WORDBYTES;
Kmods SIG 8b815c
+			} while (dst < end);
Kmods SIG 8b815c
+			return end;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+		/*
Kmods SIG 8b815c
+		 * We don't bother with special cases for other 'offset <
Kmods SIG 8b815c
+		 * WORDBYTES', which are usually rarer than 'offset == 1'.  Extra
Kmods SIG 8b815c
+		 * checks will just slow things down.  Actually, it's possible
Kmods SIG 8b815c
+		 * to handle all the 'offset < WORDBYTES' cases using the same
Kmods SIG 8b815c
+		 * code, but it still becomes more complicated doesn't seem any
Kmods SIG 8b815c
+		 * faster overall; it definitely slows down the more common
Kmods SIG 8b815c
+		 * 'offset == 1' case.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+#endif /* FAST_UNALIGNED_ACCESS */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Fall back to a bytewise copy.  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (min_length >= 2) {
Kmods SIG 8b815c
+		*dst++ = *src++;
Kmods SIG 8b815c
+		length--;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+	if (min_length >= 3) {
Kmods SIG 8b815c
+		*dst++ = *src++;
Kmods SIG 8b815c
+		length--;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+	do {
Kmods SIG 8b815c
+		*dst++ = *src++;
Kmods SIG 8b815c
+	} while (--length);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return dst;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
diff --git a/src/lib/lib.h b/src/lib/lib.h
Kmods SIG 8b815c
new file mode 100644
Kmods SIG 8b815c
index 0000000000000000000000000000000000000000..f508fbad2e712d946274b13f0ec7b244dc264a4d
Kmods SIG 8b815c
--- /dev/null
Kmods SIG 8b815c
+++ b/src/lib/lib.h
Kmods SIG 8b815c
@@ -0,0 +1,26 @@
Kmods SIG 8b815c
+/* SPDX-License-Identifier: GPL-2.0-or-later */
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * Adapted for linux kernel by Alexander Mamaev:
Kmods SIG 8b815c
+ * - remove implementations of get_unaligned_
Kmods SIG 8b815c
+ * - assume GCC is always defined
Kmods SIG 8b815c
+ * - ISO C90
Kmods SIG 8b815c
+ * - linux kernel code style
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* globals from xpress_decompress.c */
Kmods SIG 8b815c
+struct xpress_decompressor *xpress_allocate_decompressor(void);
Kmods SIG 8b815c
+void xpress_free_decompressor(struct xpress_decompressor *d);
Kmods SIG 8b815c
+int xpress_decompress(struct xpress_decompressor *__restrict d,
Kmods SIG 8b815c
+		      const void *__restrict compressed_data,
Kmods SIG 8b815c
+		      size_t compressed_size,
Kmods SIG 8b815c
+		      void *__restrict uncompressed_data,
Kmods SIG 8b815c
+		      size_t uncompressed_size);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* globals from lzx_decompress.c */
Kmods SIG 8b815c
+struct lzx_decompressor *lzx_allocate_decompressor(void);
Kmods SIG 8b815c
+void lzx_free_decompressor(struct lzx_decompressor *d);
Kmods SIG 8b815c
+int lzx_decompress(struct lzx_decompressor *__restrict d,
Kmods SIG 8b815c
+		   const void *__restrict compressed_data,
Kmods SIG 8b815c
+		   size_t compressed_size, void *__restrict uncompressed_data,
Kmods SIG 8b815c
+		   size_t uncompressed_size);
Kmods SIG 8b815c
diff --git a/src/lib/lzx_decompress.c b/src/lib/lzx_decompress.c
Kmods SIG 8b815c
new file mode 100644
Kmods SIG 8b815c
index 0000000000000000000000000000000000000000..77a381a693d117e3e8c4860130880fedf3af868e
Kmods SIG 8b815c
--- /dev/null
Kmods SIG 8b815c
+++ b/src/lib/lzx_decompress.c
Kmods SIG 8b815c
@@ -0,0 +1,683 @@
Kmods SIG 8b815c
+// SPDX-License-Identifier: GPL-2.0-or-later
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * lzx_decompress.c - A decompressor for the LZX compression format, which can
Kmods SIG 8b815c
+ * be used in "System Compressed" files.  This is based on the code from wimlib.
Kmods SIG 8b815c
+ * This code only supports a window size (dictionary size) of 32768 bytes, since
Kmods SIG 8b815c
+ * this is the only size used in System Compression.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Copyright (C) 2015 Eric Biggers
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is free software: you can redistribute it and/or modify it under
Kmods SIG 8b815c
+ * the terms of the GNU General Public License as published by the Free Software
Kmods SIG 8b815c
+ * Foundation, either version 2 of the License, or (at your option) any later
Kmods SIG 8b815c
+ * version.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is distributed in the hope that it will be useful, but WITHOUT
Kmods SIG 8b815c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Kmods SIG 8b815c
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
Kmods SIG 8b815c
+ * details.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * You should have received a copy of the GNU General Public License along with
Kmods SIG 8b815c
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+#include "decompress_common.h"
Kmods SIG 8b815c
+#include "lib.h"
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of literal byte values  */
Kmods SIG 8b815c
+#define LZX_NUM_CHARS			256
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* The smallest and largest allowed match lengths  */
Kmods SIG 8b815c
+#define LZX_MIN_MATCH_LEN		2
Kmods SIG 8b815c
+#define LZX_MAX_MATCH_LEN		257
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of distinct match lengths that can be represented  */
Kmods SIG 8b815c
+#define LZX_NUM_LENS			(LZX_MAX_MATCH_LEN - LZX_MIN_MATCH_LEN + 1)
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of match lengths for which no length symbol is required  */
Kmods SIG 8b815c
+#define LZX_NUM_PRIMARY_LENS		7
Kmods SIG 8b815c
+#define LZX_NUM_LEN_HEADERS		(LZX_NUM_PRIMARY_LENS + 1)
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Valid values of the 3-bit block type field  */
Kmods SIG 8b815c
+#define LZX_BLOCKTYPE_VERBATIM		1
Kmods SIG 8b815c
+#define LZX_BLOCKTYPE_ALIGNED		2
Kmods SIG 8b815c
+#define LZX_BLOCKTYPE_UNCOMPRESSED	3
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of offset slots for a window size of 32768  */
Kmods SIG 8b815c
+#define LZX_NUM_OFFSET_SLOTS		30
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of symbols in the main code for a window size of 32768  */
Kmods SIG 8b815c
+#define LZX_MAINCODE_NUM_SYMBOLS	\
Kmods SIG 8b815c
+	(LZX_NUM_CHARS + (LZX_NUM_OFFSET_SLOTS * LZX_NUM_LEN_HEADERS))
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of symbols in the length code  */
Kmods SIG 8b815c
+#define LZX_LENCODE_NUM_SYMBOLS		(LZX_NUM_LENS - LZX_NUM_PRIMARY_LENS)
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of symbols in the precode  */
Kmods SIG 8b815c
+#define LZX_PRECODE_NUM_SYMBOLS		20
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of bits in which each precode codeword length is represented  */
Kmods SIG 8b815c
+#define LZX_PRECODE_ELEMENT_SIZE	4
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of low-order bits of each match offset that are entropy-encoded in
Kmods SIG 8b815c
+ * aligned offset blocks
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+#define LZX_NUM_ALIGNED_OFFSET_BITS	3
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of symbols in the aligned offset code  */
Kmods SIG 8b815c
+#define LZX_ALIGNEDCODE_NUM_SYMBOLS	(1 << LZX_NUM_ALIGNED_OFFSET_BITS)
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Mask for the match offset bits that are entropy-encoded in aligned offset
Kmods SIG 8b815c
+ * blocks
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+#define LZX_ALIGNED_OFFSET_BITMASK	((1 << LZX_NUM_ALIGNED_OFFSET_BITS) - 1)
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of bits in which each aligned offset codeword length is represented  */
Kmods SIG 8b815c
+#define LZX_ALIGNEDCODE_ELEMENT_SIZE	3
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Maximum lengths (in bits) of the codewords in each Huffman code  */
Kmods SIG 8b815c
+#define LZX_MAX_MAIN_CODEWORD_LEN	16
Kmods SIG 8b815c
+#define LZX_MAX_LEN_CODEWORD_LEN	16
Kmods SIG 8b815c
+#define LZX_MAX_PRE_CODEWORD_LEN	((1 << LZX_PRECODE_ELEMENT_SIZE) - 1)
Kmods SIG 8b815c
+#define LZX_MAX_ALIGNED_CODEWORD_LEN	((1 << LZX_ALIGNEDCODE_ELEMENT_SIZE) - 1)
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* The default "filesize" value used in pre/post-processing.  In the LZX format
Kmods SIG 8b815c
+ * used in cabinet files this value must be given to the decompressor, whereas
Kmods SIG 8b815c
+ * in the LZX format used in WIM files and system-compressed files this value is
Kmods SIG 8b815c
+ * fixed at 12000000.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+#define LZX_DEFAULT_FILESIZE		12000000
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Assumed block size when the encoded block size begins with a 0 bit.  */
Kmods SIG 8b815c
+#define LZX_DEFAULT_BLOCK_SIZE		32768
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Number of offsets in the recent (or "repeat") offsets queue.  */
Kmods SIG 8b815c
+#define LZX_NUM_RECENT_OFFSETS		3
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* These values are chosen for fast decompression.  */
Kmods SIG 8b815c
+#define LZX_MAINCODE_TABLEBITS		11
Kmods SIG 8b815c
+#define LZX_LENCODE_TABLEBITS		10
Kmods SIG 8b815c
+#define LZX_PRECODE_TABLEBITS		6
Kmods SIG 8b815c
+#define LZX_ALIGNEDCODE_TABLEBITS	7
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+#define LZX_READ_LENS_MAX_OVERRUN	50
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Mapping: offset slot => first match offset that uses that offset slot.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static const u32 lzx_offset_slot_base[LZX_NUM_OFFSET_SLOTS + 1] = {
Kmods SIG 8b815c
+	0,	1,	2,	3,	4,	/* 0  --- 4  */
Kmods SIG 8b815c
+	6,	8,	12,	16,	24,	/* 5  --- 9  */
Kmods SIG 8b815c
+	32,	48,	64,	96,	128,	/* 10 --- 14 */
Kmods SIG 8b815c
+	192,	256,	384,	512,	768,	/* 15 --- 19 */
Kmods SIG 8b815c
+	1024,	1536,	2048,	3072,	4096,   /* 20 --- 24 */
Kmods SIG 8b815c
+	6144,	8192,	12288,	16384,	24576,	/* 25 --- 29 */
Kmods SIG 8b815c
+	32768,					/* extra     */
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Mapping: offset slot => how many extra bits must be read and added to the
Kmods SIG 8b815c
+ * corresponding offset slot base to decode the match offset.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static const u8 lzx_extra_offset_bits[LZX_NUM_OFFSET_SLOTS] = {
Kmods SIG 8b815c
+	0,	0,	0,	0,	1,
Kmods SIG 8b815c
+	1,	2,	2,	3,	3,
Kmods SIG 8b815c
+	4,	4,	5,	5,	6,
Kmods SIG 8b815c
+	6,	7,	7,	8,	8,
Kmods SIG 8b815c
+	9,	9,	10,	10,	11,
Kmods SIG 8b815c
+	11,	12,	12,	13,	13,
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Reusable heap-allocated memory for LZX decompression  */
Kmods SIG 8b815c
+struct lzx_decompressor {
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Huffman decoding tables, and arrays that map symbols to codeword
Kmods SIG 8b815c
+	 * lengths
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	u16 maincode_decode_table[(1 << LZX_MAINCODE_TABLEBITS) +
Kmods SIG 8b815c
+					(LZX_MAINCODE_NUM_SYMBOLS * 2)];
Kmods SIG 8b815c
+	u8 maincode_lens[LZX_MAINCODE_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	u16 lencode_decode_table[(1 << LZX_LENCODE_TABLEBITS) +
Kmods SIG 8b815c
+					(LZX_LENCODE_NUM_SYMBOLS * 2)];
Kmods SIG 8b815c
+	u8 lencode_lens[LZX_LENCODE_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	u16 alignedcode_decode_table[(1 << LZX_ALIGNEDCODE_TABLEBITS) +
Kmods SIG 8b815c
+					(LZX_ALIGNEDCODE_NUM_SYMBOLS * 2)];
Kmods SIG 8b815c
+	u8 alignedcode_lens[LZX_ALIGNEDCODE_NUM_SYMBOLS];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	u16 precode_decode_table[(1 << LZX_PRECODE_TABLEBITS) +
Kmods SIG 8b815c
+				 (LZX_PRECODE_NUM_SYMBOLS * 2)];
Kmods SIG 8b815c
+	u8 precode_lens[LZX_PRECODE_NUM_SYMBOLS];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Temporary space for make_huffman_decode_table()  */
Kmods SIG 8b815c
+	u16 working_space[2 * (1 + LZX_MAX_MAIN_CODEWORD_LEN) +
Kmods SIG 8b815c
+			  LZX_MAINCODE_NUM_SYMBOLS];
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static void undo_e8_translation(void *target, s32 input_pos)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	s32 abs_offset, rel_offset;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	abs_offset = get_unaligned_le32(target);
Kmods SIG 8b815c
+	if (abs_offset >= 0) {
Kmods SIG 8b815c
+		if (abs_offset < LZX_DEFAULT_FILESIZE) {
Kmods SIG 8b815c
+			/* "good translation" */
Kmods SIG 8b815c
+			rel_offset = abs_offset - input_pos;
Kmods SIG 8b815c
+			put_unaligned_le32(rel_offset, target);
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	} else {
Kmods SIG 8b815c
+		if (abs_offset >= -input_pos) {
Kmods SIG 8b815c
+			/* "compensating translation" */
Kmods SIG 8b815c
+			rel_offset = abs_offset + LZX_DEFAULT_FILESIZE;
Kmods SIG 8b815c
+			put_unaligned_le32(rel_offset, target);
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * Undo the 'E8' preprocessing used in LZX.  Before compression, the
Kmods SIG 8b815c
+ * uncompressed data was preprocessed by changing the targets of suspected x86
Kmods SIG 8b815c
+ * CALL instructions from relative offsets to absolute offsets.  After
Kmods SIG 8b815c
+ * match/literal decoding, the decompressor must undo the translation.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static void lzx_postprocess(u8 *data, u32 size)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	/*
Kmods SIG 8b815c
+	 * A worthwhile optimization is to push the end-of-buffer check into the
Kmods SIG 8b815c
+	 * relatively rare E8 case.  This is possible if we replace the last six
Kmods SIG 8b815c
+	 * bytes of data with E8 bytes; then we are guaranteed to hit an E8 byte
Kmods SIG 8b815c
+	 * before reaching end-of-buffer.  In addition, this scheme guarantees
Kmods SIG 8b815c
+	 * that no translation can begin following an E8 byte in the last 10
Kmods SIG 8b815c
+	 * bytes because a 4-byte offset containing E8 as its high byte is a
Kmods SIG 8b815c
+	 * large negative number that is not valid for translation.  That is
Kmods SIG 8b815c
+	 * exactly what we need.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	u8 *tail;
Kmods SIG 8b815c
+	u8 saved_bytes[6];
Kmods SIG 8b815c
+	u8 *p;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (size <= 10)
Kmods SIG 8b815c
+		return;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	tail = &data[size - 6];
Kmods SIG 8b815c
+	memcpy(saved_bytes, tail, 6);
Kmods SIG 8b815c
+	memset(tail, 0xE8, 6);
Kmods SIG 8b815c
+	p = data;
Kmods SIG 8b815c
+	for (;;) {
Kmods SIG 8b815c
+		while (*p != 0xE8)
Kmods SIG 8b815c
+			p++;
Kmods SIG 8b815c
+		if (p >= tail)
Kmods SIG 8b815c
+			break;
Kmods SIG 8b815c
+		undo_e8_translation(p + 1, p - data);
Kmods SIG 8b815c
+		p += 5;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+	memcpy(tail, saved_bytes, 6);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read a Huffman-encoded symbol using the precode.  */
Kmods SIG 8b815c
+static forceinline u32 read_presym(const struct lzx_decompressor *d,
Kmods SIG 8b815c
+					struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return read_huffsym(is, d->precode_decode_table,
Kmods SIG 8b815c
+			    LZX_PRECODE_TABLEBITS, LZX_MAX_PRE_CODEWORD_LEN);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read a Huffman-encoded symbol using the main code.  */
Kmods SIG 8b815c
+static forceinline u32 read_mainsym(const struct lzx_decompressor *d,
Kmods SIG 8b815c
+					 struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return read_huffsym(is, d->maincode_decode_table,
Kmods SIG 8b815c
+			    LZX_MAINCODE_TABLEBITS, LZX_MAX_MAIN_CODEWORD_LEN);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read a Huffman-encoded symbol using the length code.  */
Kmods SIG 8b815c
+static forceinline u32 read_lensym(const struct lzx_decompressor *d,
Kmods SIG 8b815c
+					struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return read_huffsym(is, d->lencode_decode_table,
Kmods SIG 8b815c
+			    LZX_LENCODE_TABLEBITS, LZX_MAX_LEN_CODEWORD_LEN);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Read a Huffman-encoded symbol using the aligned offset code.  */
Kmods SIG 8b815c
+static forceinline u32 read_alignedsym(const struct lzx_decompressor *d,
Kmods SIG 8b815c
+					    struct input_bitstream *is)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return read_huffsym(is, d->alignedcode_decode_table,
Kmods SIG 8b815c
+			    LZX_ALIGNEDCODE_TABLEBITS,
Kmods SIG 8b815c
+			    LZX_MAX_ALIGNED_CODEWORD_LEN);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * Read the precode from the compressed input bitstream, then use it to decode
Kmods SIG 8b815c
+ * @num_lens codeword length values.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @is:		The input bitstream.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @lens:	An array that contains the length values from the previous time
Kmods SIG 8b815c
+ *		the codeword lengths for this Huffman code were read, or all 0's
Kmods SIG 8b815c
+ *		if this is the first time.  This array must have at least
Kmods SIG 8b815c
+ *		(@num_lens + LZX_READ_LENS_MAX_OVERRUN) entries.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @num_lens:	Number of length values to decode.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Returns 0 on success, or -1 if the data was invalid.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static int lzx_read_codeword_lens(struct lzx_decompressor *d,
Kmods SIG 8b815c
+				  struct input_bitstream *is,
Kmods SIG 8b815c
+				  u8 *lens, u32 num_lens)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	u8 *len_ptr = lens;
Kmods SIG 8b815c
+	u8 *lens_end = lens + num_lens;
Kmods SIG 8b815c
+	int i;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Read the lengths of the precode codewords.  These are given
Kmods SIG 8b815c
+	 * explicitly.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	for (i = 0; i < LZX_PRECODE_NUM_SYMBOLS; i++) {
Kmods SIG 8b815c
+		d->precode_lens[i] =
Kmods SIG 8b815c
+			bitstream_read_bits(is, LZX_PRECODE_ELEMENT_SIZE);
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Make the decoding table for the precode.  */
Kmods SIG 8b815c
+	if (make_huffman_decode_table(d->precode_decode_table,
Kmods SIG 8b815c
+				      LZX_PRECODE_NUM_SYMBOLS,
Kmods SIG 8b815c
+				      LZX_PRECODE_TABLEBITS,
Kmods SIG 8b815c
+				      d->precode_lens,
Kmods SIG 8b815c
+				      LZX_MAX_PRE_CODEWORD_LEN,
Kmods SIG 8b815c
+				      d->working_space))
Kmods SIG 8b815c
+		return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Decode the codeword lengths.  */
Kmods SIG 8b815c
+	do {
Kmods SIG 8b815c
+		u32 presym;
Kmods SIG 8b815c
+		u8 len;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Read the next precode symbol.  */
Kmods SIG 8b815c
+		presym = read_presym(d, is);
Kmods SIG 8b815c
+		if (presym < 17) {
Kmods SIG 8b815c
+			/* Difference from old length  */
Kmods SIG 8b815c
+			len = *len_ptr - presym;
Kmods SIG 8b815c
+			if ((s8)len < 0)
Kmods SIG 8b815c
+				len += 17;
Kmods SIG 8b815c
+			*len_ptr++ = len;
Kmods SIG 8b815c
+		} else {
Kmods SIG 8b815c
+			/* Special RLE values  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			u32 run_len;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (presym == 17) {
Kmods SIG 8b815c
+				/* Run of 0's  */
Kmods SIG 8b815c
+				run_len = 4 + bitstream_read_bits(is, 4);
Kmods SIG 8b815c
+				len = 0;
Kmods SIG 8b815c
+			} else if (presym == 18) {
Kmods SIG 8b815c
+				/* Longer run of 0's  */
Kmods SIG 8b815c
+				run_len = 20 + bitstream_read_bits(is, 5);
Kmods SIG 8b815c
+				len = 0;
Kmods SIG 8b815c
+			} else {
Kmods SIG 8b815c
+				/* Run of identical lengths  */
Kmods SIG 8b815c
+				run_len = 4 + bitstream_read_bits(is, 1);
Kmods SIG 8b815c
+				presym = read_presym(d, is);
Kmods SIG 8b815c
+				if (presym > 17)
Kmods SIG 8b815c
+					return -1;
Kmods SIG 8b815c
+				len = *len_ptr - presym;
Kmods SIG 8b815c
+				if ((s8)len < 0)
Kmods SIG 8b815c
+					len += 17;
Kmods SIG 8b815c
+			}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			do {
Kmods SIG 8b815c
+				*len_ptr++ = len;
Kmods SIG 8b815c
+			} while (--run_len);
Kmods SIG 8b815c
+			/* Worst case overrun is when presym == 18,
Kmods SIG 8b815c
+			 * run_len == 20 + 31, and only 1 length was remaining.
Kmods SIG 8b815c
+			 * So LZX_READ_LENS_MAX_OVERRUN == 50.
Kmods SIG 8b815c
+			 *
Kmods SIG 8b815c
+			 * Overrun while reading the first half of maincode_lens
Kmods SIG 8b815c
+			 * can corrupt the previous values in the second half.
Kmods SIG 8b815c
+			 * This doesn't really matter because the resulting
Kmods SIG 8b815c
+			 * lengths will still be in range, and data that
Kmods SIG 8b815c
+			 * generates overruns is invalid anyway.
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	} while (len_ptr < lens_end);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return 0;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * Read the header of an LZX block and save the block type and (uncompressed)
Kmods SIG 8b815c
+ * size in *block_type_ret and *block_size_ret, respectively.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * If the block is compressed, also update the Huffman decode @tables with the
Kmods SIG 8b815c
+ * new Huffman codes.  If the block is uncompressed, also update the match
Kmods SIG 8b815c
+ * offset @queue with the new match offsets.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Return 0 on success, or -1 if the data was invalid.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static int lzx_read_block_header(struct lzx_decompressor *d,
Kmods SIG 8b815c
+				 struct input_bitstream *is,
Kmods SIG 8b815c
+				 int *block_type_ret,
Kmods SIG 8b815c
+				 u32 *block_size_ret,
Kmods SIG 8b815c
+				 u32 recent_offsets[])
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	int block_type;
Kmods SIG 8b815c
+	u32 block_size;
Kmods SIG 8b815c
+	int i;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	bitstream_ensure_bits(is, 4);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* The first three bits tell us what kind of block it is, and should be
Kmods SIG 8b815c
+	 * one of the LZX_BLOCKTYPE_* values.
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	block_type = bitstream_pop_bits(is, 3);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Read the block size.  */
Kmods SIG 8b815c
+	if (bitstream_pop_bits(is, 1)) {
Kmods SIG 8b815c
+		block_size = LZX_DEFAULT_BLOCK_SIZE;
Kmods SIG 8b815c
+	} else {
Kmods SIG 8b815c
+		block_size = 0;
Kmods SIG 8b815c
+		block_size |= bitstream_read_bits(is, 8);
Kmods SIG 8b815c
+		block_size <<= 8;
Kmods SIG 8b815c
+		block_size |= bitstream_read_bits(is, 8);
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	switch (block_type) {
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	case LZX_BLOCKTYPE_ALIGNED:
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Read the aligned offset code and prepare its decode table.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		for (i = 0; i < LZX_ALIGNEDCODE_NUM_SYMBOLS; i++) {
Kmods SIG 8b815c
+			d->alignedcode_lens[i] =
Kmods SIG 8b815c
+				bitstream_read_bits(is,
Kmods SIG 8b815c
+						    LZX_ALIGNEDCODE_ELEMENT_SIZE);
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (make_huffman_decode_table(d->alignedcode_decode_table,
Kmods SIG 8b815c
+					      LZX_ALIGNEDCODE_NUM_SYMBOLS,
Kmods SIG 8b815c
+					      LZX_ALIGNEDCODE_TABLEBITS,
Kmods SIG 8b815c
+					      d->alignedcode_lens,
Kmods SIG 8b815c
+					      LZX_MAX_ALIGNED_CODEWORD_LEN,
Kmods SIG 8b815c
+					      d->working_space))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Fall though, since the rest of the header for aligned offset
Kmods SIG 8b815c
+		 * blocks is the same as that for verbatim blocks.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+		fallthrough;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	case LZX_BLOCKTYPE_VERBATIM:
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Read the main code and prepare its decode table.
Kmods SIG 8b815c
+		 *
Kmods SIG 8b815c
+		 * Note that the codeword lengths in the main code are encoded
Kmods SIG 8b815c
+		 * in two parts: one part for literal symbols, and one part for
Kmods SIG 8b815c
+		 * match symbols.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (lzx_read_codeword_lens(d, is, d->maincode_lens,
Kmods SIG 8b815c
+					   LZX_NUM_CHARS))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (lzx_read_codeword_lens(d, is,
Kmods SIG 8b815c
+					   d->maincode_lens + LZX_NUM_CHARS,
Kmods SIG 8b815c
+					   LZX_MAINCODE_NUM_SYMBOLS - LZX_NUM_CHARS))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (make_huffman_decode_table(d->maincode_decode_table,
Kmods SIG 8b815c
+					      LZX_MAINCODE_NUM_SYMBOLS,
Kmods SIG 8b815c
+					      LZX_MAINCODE_TABLEBITS,
Kmods SIG 8b815c
+					      d->maincode_lens,
Kmods SIG 8b815c
+					      LZX_MAX_MAIN_CODEWORD_LEN,
Kmods SIG 8b815c
+					      d->working_space))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Read the length code and prepare its decode table.  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (lzx_read_codeword_lens(d, is, d->lencode_lens,
Kmods SIG 8b815c
+					   LZX_LENCODE_NUM_SYMBOLS))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (make_huffman_decode_table(d->lencode_decode_table,
Kmods SIG 8b815c
+					      LZX_LENCODE_NUM_SYMBOLS,
Kmods SIG 8b815c
+					      LZX_LENCODE_TABLEBITS,
Kmods SIG 8b815c
+					      d->lencode_lens,
Kmods SIG 8b815c
+					      LZX_MAX_LEN_CODEWORD_LEN,
Kmods SIG 8b815c
+					      d->working_space))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		break;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	case LZX_BLOCKTYPE_UNCOMPRESSED:
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Before reading the three recent offsets from the uncompressed
Kmods SIG 8b815c
+		 * block header, the stream must be aligned on a 16-bit
Kmods SIG 8b815c
+		 * boundary.  But if the stream is *already* aligned, then the
Kmods SIG 8b815c
+		 * next 16 bits must be discarded.
Kmods SIG 8b815c
+		 */
Kmods SIG 8b815c
+		bitstream_ensure_bits(is, 1);
Kmods SIG 8b815c
+		bitstream_align(is);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		recent_offsets[0] = bitstream_read_u32(is);
Kmods SIG 8b815c
+		recent_offsets[1] = bitstream_read_u32(is);
Kmods SIG 8b815c
+		recent_offsets[2] = bitstream_read_u32(is);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Offsets of 0 are invalid.  */
Kmods SIG 8b815c
+		if (recent_offsets[0] == 0 || recent_offsets[1] == 0 ||
Kmods SIG 8b815c
+		    recent_offsets[2] == 0)
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+		break;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	default:
Kmods SIG 8b815c
+		/* Unrecognized block type.  */
Kmods SIG 8b815c
+		return -1;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	*block_type_ret = block_type;
Kmods SIG 8b815c
+	*block_size_ret = block_size;
Kmods SIG 8b815c
+	return 0;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Decompress a block of LZX-compressed data.  */
Kmods SIG 8b815c
+static int lzx_decompress_block(const struct lzx_decompressor *d,
Kmods SIG 8b815c
+				struct input_bitstream *is,
Kmods SIG 8b815c
+				int block_type, u32 block_size,
Kmods SIG 8b815c
+				u8 * const out_begin, u8 *out_next,
Kmods SIG 8b815c
+				u32 recent_offsets[])
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	u8 * const block_end = out_next + block_size;
Kmods SIG 8b815c
+	u32 ones_if_aligned = 0U - (block_type == LZX_BLOCKTYPE_ALIGNED);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	do {
Kmods SIG 8b815c
+		u32 mainsym;
Kmods SIG 8b815c
+		u32 match_len;
Kmods SIG 8b815c
+		u32 match_offset;
Kmods SIG 8b815c
+		u32 offset_slot;
Kmods SIG 8b815c
+		u32 num_extra_bits;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		mainsym = read_mainsym(d, is);
Kmods SIG 8b815c
+		if (mainsym < LZX_NUM_CHARS) {
Kmods SIG 8b815c
+			/* Literal  */
Kmods SIG 8b815c
+			*out_next++ = mainsym;
Kmods SIG 8b815c
+			continue;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Match  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Decode the length header and offset slot.  */
Kmods SIG 8b815c
+		mainsym -= LZX_NUM_CHARS;
Kmods SIG 8b815c
+		match_len = mainsym % LZX_NUM_LEN_HEADERS;
Kmods SIG 8b815c
+		offset_slot = mainsym / LZX_NUM_LEN_HEADERS;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* If needed, read a length symbol to decode the full length. */
Kmods SIG 8b815c
+		if (match_len == LZX_NUM_PRIMARY_LENS)
Kmods SIG 8b815c
+			match_len += read_lensym(d, is);
Kmods SIG 8b815c
+		match_len += LZX_MIN_MATCH_LEN;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (offset_slot < LZX_NUM_RECENT_OFFSETS) {
Kmods SIG 8b815c
+			/* Repeat offset  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* Note: This isn't a real LRU queue, since using the R2
Kmods SIG 8b815c
+			 * offset doesn't bump the R1 offset down to R2.  This
Kmods SIG 8b815c
+			 * quirk allows all 3 recent offsets to be handled by
Kmods SIG 8b815c
+			 * the same code.  (For R0, the swap is a no-op.)
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+			match_offset = recent_offsets[offset_slot];
Kmods SIG 8b815c
+			recent_offsets[offset_slot] = recent_offsets[0];
Kmods SIG 8b815c
+			recent_offsets[0] = match_offset;
Kmods SIG 8b815c
+		} else {
Kmods SIG 8b815c
+			/* Explicit offset  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* Look up the number of extra bits that need to be read
Kmods SIG 8b815c
+			 * to decode offsets with this offset slot.
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+			num_extra_bits = lzx_extra_offset_bits[offset_slot];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* Start with the offset slot base value.  */
Kmods SIG 8b815c
+			match_offset = lzx_offset_slot_base[offset_slot];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* In aligned offset blocks, the low-order 3 bits of
Kmods SIG 8b815c
+			 * each offset are encoded using the aligned offset
Kmods SIG 8b815c
+			 * code.  Otherwise, all the extra bits are literal.
Kmods SIG 8b815c
+			 */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if ((num_extra_bits & ones_if_aligned) >= LZX_NUM_ALIGNED_OFFSET_BITS) {
Kmods SIG 8b815c
+				match_offset +=
Kmods SIG 8b815c
+					bitstream_read_bits(is, num_extra_bits -
Kmods SIG 8b815c
+								LZX_NUM_ALIGNED_OFFSET_BITS)
Kmods SIG 8b815c
+							<< LZX_NUM_ALIGNED_OFFSET_BITS;
Kmods SIG 8b815c
+				match_offset += read_alignedsym(d, is);
Kmods SIG 8b815c
+			} else {
Kmods SIG 8b815c
+				match_offset += bitstream_read_bits(is, num_extra_bits);
Kmods SIG 8b815c
+			}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* Adjust the offset.  */
Kmods SIG 8b815c
+			match_offset -= (LZX_NUM_RECENT_OFFSETS - 1);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* Update the recent offsets.  */
Kmods SIG 8b815c
+			recent_offsets[2] = recent_offsets[1];
Kmods SIG 8b815c
+			recent_offsets[1] = recent_offsets[0];
Kmods SIG 8b815c
+			recent_offsets[0] = match_offset;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Validate the match, then copy it to the current position.  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (match_len > (size_t)(block_end - out_next))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (match_offset > (size_t)(out_next - out_begin))
Kmods SIG 8b815c
+			return -1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		out_next = lz_copy(out_next, match_len, match_offset,
Kmods SIG 8b815c
+				   block_end, LZX_MIN_MATCH_LEN);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	} while (out_next != block_end);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return 0;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * lzx_allocate_decompressor - Allocate an LZX decompressor
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Return the pointer to the decompressor on success, or return NULL and set
Kmods SIG 8b815c
+ * errno on failure.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+struct lzx_decompressor *lzx_allocate_decompressor(void)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return kmalloc(sizeof(struct lzx_decompressor), GFP_NOFS);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * lzx_decompress - Decompress a buffer of LZX-compressed data
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @decompressor:      A decompressor allocated with lzx_allocate_decompressor()
Kmods SIG 8b815c
+ * @compressed_data:	The buffer of data to decompress
Kmods SIG 8b815c
+ * @compressed_size:	Number of bytes of compressed data
Kmods SIG 8b815c
+ * @uncompressed_data:	The buffer in which to store the decompressed data
Kmods SIG 8b815c
+ * @uncompressed_size:	The number of bytes the data decompresses into
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Return 0 on success, or return -1 and set errno on failure.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+int lzx_decompress(struct lzx_decompressor *decompressor,
Kmods SIG 8b815c
+		   const void *compressed_data, size_t compressed_size,
Kmods SIG 8b815c
+		   void *uncompressed_data, size_t uncompressed_size)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	struct lzx_decompressor *d = decompressor;
Kmods SIG 8b815c
+	u8 * const out_begin = uncompressed_data;
Kmods SIG 8b815c
+	u8 *out_next = out_begin;
Kmods SIG 8b815c
+	u8 * const out_end = out_begin + uncompressed_size;
Kmods SIG 8b815c
+	struct input_bitstream is;
Kmods SIG 8b815c
+	u32 recent_offsets[LZX_NUM_RECENT_OFFSETS] = {1, 1, 1};
Kmods SIG 8b815c
+	int e8_status = 0;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	init_input_bitstream(&is, compressed_data, compressed_size);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Codeword lengths begin as all 0's for delta encoding purposes.  */
Kmods SIG 8b815c
+	memset(d->maincode_lens, 0, LZX_MAINCODE_NUM_SYMBOLS);
Kmods SIG 8b815c
+	memset(d->lencode_lens, 0, LZX_LENCODE_NUM_SYMBOLS);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Decompress blocks until we have all the uncompressed data.  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	while (out_next != out_end) {
Kmods SIG 8b815c
+		int block_type;
Kmods SIG 8b815c
+		u32 block_size;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (lzx_read_block_header(d, &is, &block_type, &block_size,
Kmods SIG 8b815c
+					  recent_offsets))
Kmods SIG 8b815c
+			goto invalid;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (block_size < 1 || block_size > (size_t)(out_end - out_next))
Kmods SIG 8b815c
+			goto invalid;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (block_type != LZX_BLOCKTYPE_UNCOMPRESSED) {
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* Compressed block  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (lzx_decompress_block(d,
Kmods SIG 8b815c
+						 &is,
Kmods SIG 8b815c
+						 block_type,
Kmods SIG 8b815c
+						 block_size,
Kmods SIG 8b815c
+						 out_begin,
Kmods SIG 8b815c
+						 out_next,
Kmods SIG 8b815c
+						 recent_offsets))
Kmods SIG 8b815c
+				goto invalid;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			e8_status |= d->maincode_lens[0xe8];
Kmods SIG 8b815c
+			out_next += block_size;
Kmods SIG 8b815c
+		} else {
Kmods SIG 8b815c
+			/* Uncompressed block  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			out_next = bitstream_read_bytes(&is, out_next,
Kmods SIG 8b815c
+							block_size);
Kmods SIG 8b815c
+			if (!out_next)
Kmods SIG 8b815c
+				goto invalid;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (block_size & 1)
Kmods SIG 8b815c
+				bitstream_read_byte(&is);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			e8_status = 1;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Postprocess the data unless it cannot possibly contain 0xe8 bytes. */
Kmods SIG 8b815c
+	if (e8_status)
Kmods SIG 8b815c
+		lzx_postprocess(uncompressed_data, uncompressed_size);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return 0;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+invalid:
Kmods SIG 8b815c
+	return -1;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * lzx_free_decompressor - Free an LZX decompressor
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @decompressor:       A decompressor that was allocated with
Kmods SIG 8b815c
+ *			lzx_allocate_decompressor(), or NULL.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+void lzx_free_decompressor(struct lzx_decompressor *decompressor)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	kfree(decompressor);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
diff --git a/src/lib/xpress_decompress.c b/src/lib/xpress_decompress.c
Kmods SIG 8b815c
new file mode 100644
Kmods SIG 8b815c
index 0000000000000000000000000000000000000000..3d98f36a981e672d4d924b5fcbaf655d18d96355
Kmods SIG 8b815c
--- /dev/null
Kmods SIG 8b815c
+++ b/src/lib/xpress_decompress.c
Kmods SIG 8b815c
@@ -0,0 +1,155 @@
Kmods SIG 8b815c
+// SPDX-License-Identifier: GPL-2.0-or-later
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * xpress_decompress.c - A decompressor for the XPRESS compression format
Kmods SIG 8b815c
+ * (Huffman variant), which can be used in "System Compressed" files.  This is
Kmods SIG 8b815c
+ * based on the code from wimlib.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Copyright (C) 2015 Eric Biggers
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is free software: you can redistribute it and/or modify it under
Kmods SIG 8b815c
+ * the terms of the GNU General Public License as published by the Free Software
Kmods SIG 8b815c
+ * Foundation, either version 2 of the License, or (at your option) any later
Kmods SIG 8b815c
+ * version.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * This program is distributed in the hope that it will be useful, but WITHOUT
Kmods SIG 8b815c
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Kmods SIG 8b815c
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
Kmods SIG 8b815c
+ * details.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * You should have received a copy of the GNU General Public License along with
Kmods SIG 8b815c
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+#include "decompress_common.h"
Kmods SIG 8b815c
+#include "lib.h"
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+#define XPRESS_NUM_SYMBOLS	512
Kmods SIG 8b815c
+#define XPRESS_MAX_CODEWORD_LEN	15
Kmods SIG 8b815c
+#define XPRESS_MIN_MATCH_LEN	3
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* This value is chosen for fast decompression.  */
Kmods SIG 8b815c
+#define XPRESS_TABLEBITS 12
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/* Reusable heap-allocated memory for XPRESS decompression  */
Kmods SIG 8b815c
+struct xpress_decompressor {
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* The Huffman decoding table  */
Kmods SIG 8b815c
+	u16 decode_table[(1 << XPRESS_TABLEBITS) + 2 * XPRESS_NUM_SYMBOLS];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* An array that maps symbols to codeword lengths  */
Kmods SIG 8b815c
+	u8 lens[XPRESS_NUM_SYMBOLS];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Temporary space for make_huffman_decode_table()  */
Kmods SIG 8b815c
+	u16 working_space[2 * (1 + XPRESS_MAX_CODEWORD_LEN) +
Kmods SIG 8b815c
+			  XPRESS_NUM_SYMBOLS];
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * xpress_allocate_decompressor - Allocate an XPRESS decompressor
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Return the pointer to the decompressor on success, or return NULL and set
Kmods SIG 8b815c
+ * errno on failure.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+struct xpress_decompressor *xpress_allocate_decompressor(void)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return kmalloc(sizeof(struct xpress_decompressor), GFP_NOFS);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * xpress_decompress - Decompress a buffer of XPRESS-compressed data
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @decompressor:       A decompressor that was allocated with
Kmods SIG 8b815c
+ *			xpress_allocate_decompressor()
Kmods SIG 8b815c
+ * @compressed_data:	The buffer of data to decompress
Kmods SIG 8b815c
+ * @compressed_size:	Number of bytes of compressed data
Kmods SIG 8b815c
+ * @uncompressed_data:	The buffer in which to store the decompressed data
Kmods SIG 8b815c
+ * @uncompressed_size:	The number of bytes the data decompresses into
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Return 0 on success, or return -1 and set errno on failure.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+int xpress_decompress(struct xpress_decompressor *decompressor,
Kmods SIG 8b815c
+		      const void *compressed_data, size_t compressed_size,
Kmods SIG 8b815c
+		      void *uncompressed_data, size_t uncompressed_size)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	struct xpress_decompressor *d = decompressor;
Kmods SIG 8b815c
+	const u8 * const in_begin = compressed_data;
Kmods SIG 8b815c
+	u8 * const out_begin = uncompressed_data;
Kmods SIG 8b815c
+	u8 *out_next = out_begin;
Kmods SIG 8b815c
+	u8 * const out_end = out_begin + uncompressed_size;
Kmods SIG 8b815c
+	struct input_bitstream is;
Kmods SIG 8b815c
+	u32 i;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Read the Huffman codeword lengths.  */
Kmods SIG 8b815c
+	if (compressed_size < XPRESS_NUM_SYMBOLS / 2)
Kmods SIG 8b815c
+		goto invalid;
Kmods SIG 8b815c
+	for (i = 0; i < XPRESS_NUM_SYMBOLS / 2; i++) {
Kmods SIG 8b815c
+		d->lens[i*2 + 0] = in_begin[i] & 0xF;
Kmods SIG 8b815c
+		d->lens[i*2 + 1] = in_begin[i] >> 4;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Build a decoding table for the Huffman code.  */
Kmods SIG 8b815c
+	if (make_huffman_decode_table(d->decode_table, XPRESS_NUM_SYMBOLS,
Kmods SIG 8b815c
+				      XPRESS_TABLEBITS, d->lens,
Kmods SIG 8b815c
+				      XPRESS_MAX_CODEWORD_LEN,
Kmods SIG 8b815c
+				      d->working_space))
Kmods SIG 8b815c
+		goto invalid;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Decode the matches and literals.  */
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	init_input_bitstream(&is, in_begin + XPRESS_NUM_SYMBOLS / 2,
Kmods SIG 8b815c
+			     compressed_size - XPRESS_NUM_SYMBOLS / 2);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	while (out_next != out_end) {
Kmods SIG 8b815c
+		u32 sym;
Kmods SIG 8b815c
+		u32 log2_offset;
Kmods SIG 8b815c
+		u32 length;
Kmods SIG 8b815c
+		u32 offset;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		sym = read_huffsym(&is, d->decode_table,
Kmods SIG 8b815c
+				   XPRESS_TABLEBITS, XPRESS_MAX_CODEWORD_LEN);
Kmods SIG 8b815c
+		if (sym < 256) {
Kmods SIG 8b815c
+			/* Literal  */
Kmods SIG 8b815c
+			*out_next++ = sym;
Kmods SIG 8b815c
+		} else {
Kmods SIG 8b815c
+			/* Match  */
Kmods SIG 8b815c
+			length = sym & 0xf;
Kmods SIG 8b815c
+			log2_offset = (sym >> 4) & 0xf;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			bitstream_ensure_bits(&is, 16);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			offset = ((u32)1 << log2_offset) |
Kmods SIG 8b815c
+				 bitstream_pop_bits(&is, log2_offset);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (length == 0xf) {
Kmods SIG 8b815c
+				length += bitstream_read_byte(&is);
Kmods SIG 8b815c
+				if (length == 0xf + 0xff)
Kmods SIG 8b815c
+					length = bitstream_read_u16(&is);
Kmods SIG 8b815c
+			}
Kmods SIG 8b815c
+			length += XPRESS_MIN_MATCH_LEN;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (offset > (size_t)(out_next - out_begin))
Kmods SIG 8b815c
+				goto invalid;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (length > (size_t)(out_end - out_next))
Kmods SIG 8b815c
+				goto invalid;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			out_next = lz_copy(out_next, length, offset, out_end,
Kmods SIG 8b815c
+					   XPRESS_MIN_MATCH_LEN);
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+	return 0;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+invalid:
Kmods SIG 8b815c
+	return -1;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * xpress_free_decompressor - Free an XPRESS decompressor
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * @decompressor:       A decompressor that was allocated with
Kmods SIG 8b815c
+ *			xpress_allocate_decompressor(), or NULL.
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+void xpress_free_decompressor(struct xpress_decompressor *decompressor)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	kfree(decompressor);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
diff --git a/src/lznt.c b/src/lznt.c
Kmods SIG 8b815c
new file mode 100644
Kmods SIG 8b815c
index 0000000000000000000000000000000000000000..ead9ab7d69b3082e429ac79b973aab461d3cfd7a
Kmods SIG 8b815c
--- /dev/null
Kmods SIG 8b815c
+++ b/src/lznt.c
Kmods SIG 8b815c
@@ -0,0 +1,452 @@
Kmods SIG 8b815c
+// SPDX-License-Identifier: GPL-2.0
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+#include <linux/blkdev.h>
Kmods SIG 8b815c
+#include <linux/buffer_head.h>
Kmods SIG 8b815c
+#include <linux/fs.h>
Kmods SIG 8b815c
+#include <linux/nls.h>
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+#include "debug.h"
Kmods SIG 8b815c
+#include "ntfs.h"
Kmods SIG 8b815c
+#include "ntfs_fs.h"
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+// clang-format off
Kmods SIG 8b815c
+/* src buffer is zero */
Kmods SIG 8b815c
+#define LZNT_ERROR_ALL_ZEROS	1
Kmods SIG 8b815c
+#define LZNT_CHUNK_SIZE		0x1000
Kmods SIG 8b815c
+// clang-format on
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+struct lznt_hash {
Kmods SIG 8b815c
+	const u8 *p1;
Kmods SIG 8b815c
+	const u8 *p2;
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+struct lznt {
Kmods SIG 8b815c
+	const u8 *unc;
Kmods SIG 8b815c
+	const u8 *unc_end;
Kmods SIG 8b815c
+	const u8 *best_match;
Kmods SIG 8b815c
+	size_t max_len;
Kmods SIG 8b815c
+	bool std;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	struct lznt_hash hash[LZNT_CHUNK_SIZE];
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static inline size_t get_match_len(const u8 *ptr, const u8 *end, const u8 *prev,
Kmods SIG 8b815c
+				   size_t max_len)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	size_t len = 0;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	while (ptr + len < end && ptr[len] == prev[len] && ++len < max_len)
Kmods SIG 8b815c
+		;
Kmods SIG 8b815c
+	return len;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static size_t longest_match_std(const u8 *src, struct lznt *ctx)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	size_t hash_index;
Kmods SIG 8b815c
+	size_t len1 = 0, len2 = 0;
Kmods SIG 8b815c
+	const u8 **hash;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	hash_index =
Kmods SIG 8b815c
+		((40543U * ((((src[0] << 4) ^ src[1]) << 4) ^ src[2])) >> 4) &
Kmods SIG 8b815c
+		(LZNT_CHUNK_SIZE - 1);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	hash = &(ctx->hash[hash_index].p1);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (hash[0] >= ctx->unc && hash[0] < src && hash[0][0] == src[0] &&
Kmods SIG 8b815c
+	    hash[0][1] == src[1] && hash[0][2] == src[2]) {
Kmods SIG 8b815c
+		len1 = 3;
Kmods SIG 8b815c
+		if (ctx->max_len > 3)
Kmods SIG 8b815c
+			len1 += get_match_len(src + 3, ctx->unc_end,
Kmods SIG 8b815c
+					      hash[0] + 3, ctx->max_len - 3);
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (hash[1] >= ctx->unc && hash[1] < src && hash[1][0] == src[0] &&
Kmods SIG 8b815c
+	    hash[1][1] == src[1] && hash[1][2] == src[2]) {
Kmods SIG 8b815c
+		len2 = 3;
Kmods SIG 8b815c
+		if (ctx->max_len > 3)
Kmods SIG 8b815c
+			len2 += get_match_len(src + 3, ctx->unc_end,
Kmods SIG 8b815c
+					      hash[1] + 3, ctx->max_len - 3);
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Compare two matches and select the best one */
Kmods SIG 8b815c
+	if (len1 < len2) {
Kmods SIG 8b815c
+		ctx->best_match = hash[1];
Kmods SIG 8b815c
+		len1 = len2;
Kmods SIG 8b815c
+	} else {
Kmods SIG 8b815c
+		ctx->best_match = hash[0];
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	hash[1] = hash[0];
Kmods SIG 8b815c
+	hash[0] = src;
Kmods SIG 8b815c
+	return len1;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static size_t longest_match_best(const u8 *src, struct lznt *ctx)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	size_t max_len;
Kmods SIG 8b815c
+	const u8 *ptr;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (ctx->unc >= src || !ctx->max_len)
Kmods SIG 8b815c
+		return 0;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	max_len = 0;
Kmods SIG 8b815c
+	for (ptr = ctx->unc; ptr < src; ++ptr) {
Kmods SIG 8b815c
+		size_t len =
Kmods SIG 8b815c
+			get_match_len(src, ctx->unc_end, ptr, ctx->max_len);
Kmods SIG 8b815c
+		if (len >= max_len) {
Kmods SIG 8b815c
+			max_len = len;
Kmods SIG 8b815c
+			ctx->best_match = ptr;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return max_len >= 3 ? max_len : 0;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static const size_t s_max_len[] = {
Kmods SIG 8b815c
+	0x1002, 0x802, 0x402, 0x202, 0x102, 0x82, 0x42, 0x22, 0x12,
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static const size_t s_max_off[] = {
Kmods SIG 8b815c
+	0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
Kmods SIG 8b815c
+};
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static inline u16 make_pair(size_t offset, size_t len, size_t index)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	return ((offset - 1) << (12 - index)) |
Kmods SIG 8b815c
+	       ((len - 3) & (((1 << (12 - index)) - 1)));
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static inline size_t parse_pair(u16 pair, size_t *offset, size_t index)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	*offset = 1 + (pair >> (12 - index));
Kmods SIG 8b815c
+	return 3 + (pair & ((1 << (12 - index)) - 1));
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * compress_chunk
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * returns one of the three values:
Kmods SIG 8b815c
+ * 0 - ok, 'cmpr' contains 'cmpr_chunk_size' bytes of compressed data
Kmods SIG 8b815c
+ * 1 - input buffer is full zero
Kmods SIG 8b815c
+ * -2 - the compressed buffer is too small to hold the compressed data
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+static inline int compress_chunk(size_t (*match)(const u8 *, struct lznt *),
Kmods SIG 8b815c
+				 const u8 *unc, const u8 *unc_end, u8 *cmpr,
Kmods SIG 8b815c
+				 u8 *cmpr_end, size_t *cmpr_chunk_size,
Kmods SIG 8b815c
+				 struct lznt *ctx)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	size_t cnt = 0;
Kmods SIG 8b815c
+	size_t idx = 0;
Kmods SIG 8b815c
+	const u8 *up = unc;
Kmods SIG 8b815c
+	u8 *cp = cmpr + 3;
Kmods SIG 8b815c
+	u8 *cp2 = cmpr + 2;
Kmods SIG 8b815c
+	u8 not_zero = 0;
Kmods SIG 8b815c
+	/* Control byte of 8-bit values: ( 0 - means byte as is, 1 - short pair ) */
Kmods SIG 8b815c
+	u8 ohdr = 0;
Kmods SIG 8b815c
+	u8 *last;
Kmods SIG 8b815c
+	u16 t16;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (unc + LZNT_CHUNK_SIZE < unc_end)
Kmods SIG 8b815c
+		unc_end = unc + LZNT_CHUNK_SIZE;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	last = min(cmpr + LZNT_CHUNK_SIZE + sizeof(short), cmpr_end);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	ctx->unc = unc;
Kmods SIG 8b815c
+	ctx->unc_end = unc_end;
Kmods SIG 8b815c
+	ctx->max_len = s_max_len[0];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	while (up < unc_end) {
Kmods SIG 8b815c
+		size_t max_len;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		while (unc + s_max_off[idx] < up)
Kmods SIG 8b815c
+			ctx->max_len = s_max_len[++idx];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		// Find match
Kmods SIG 8b815c
+		max_len = up + 3 <= unc_end ? (*match)(up, ctx) : 0;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (!max_len) {
Kmods SIG 8b815c
+			if (cp >= last)
Kmods SIG 8b815c
+				goto NotCompressed;
Kmods SIG 8b815c
+			not_zero |= *cp++ = *up++;
Kmods SIG 8b815c
+		} else if (cp + 1 >= last) {
Kmods SIG 8b815c
+			goto NotCompressed;
Kmods SIG 8b815c
+		} else {
Kmods SIG 8b815c
+			t16 = make_pair(up - ctx->best_match, max_len, idx);
Kmods SIG 8b815c
+			*cp++ = t16;
Kmods SIG 8b815c
+			*cp++ = t16 >> 8;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			ohdr |= 1 << cnt;
Kmods SIG 8b815c
+			up += max_len;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		cnt = (cnt + 1) & 7;
Kmods SIG 8b815c
+		if (!cnt) {
Kmods SIG 8b815c
+			*cp2 = ohdr;
Kmods SIG 8b815c
+			ohdr = 0;
Kmods SIG 8b815c
+			cp2 = cp;
Kmods SIG 8b815c
+			cp += 1;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (cp2 < last)
Kmods SIG 8b815c
+		*cp2 = ohdr;
Kmods SIG 8b815c
+	else
Kmods SIG 8b815c
+		cp -= 1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	*cmpr_chunk_size = cp - cmpr;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	t16 = (*cmpr_chunk_size - 3) | 0xB000;
Kmods SIG 8b815c
+	cmpr[0] = t16;
Kmods SIG 8b815c
+	cmpr[1] = t16 >> 8;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return not_zero ? 0 : LZNT_ERROR_ALL_ZEROS;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+NotCompressed:
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if ((cmpr + LZNT_CHUNK_SIZE + sizeof(short)) > last)
Kmods SIG 8b815c
+		return -2;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/*
Kmods SIG 8b815c
+	 * Copy non cmpr data
Kmods SIG 8b815c
+	 * 0x3FFF == ((LZNT_CHUNK_SIZE + 2 - 3) | 0x3000)
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	cmpr[0] = 0xff;
Kmods SIG 8b815c
+	cmpr[1] = 0x3f;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	memcpy(cmpr + sizeof(short), unc, LZNT_CHUNK_SIZE);
Kmods SIG 8b815c
+	*cmpr_chunk_size = LZNT_CHUNK_SIZE + sizeof(short);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return 0;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+static inline ssize_t decompress_chunk(u8 *unc, u8 *unc_end, const u8 *cmpr,
Kmods SIG 8b815c
+				       const u8 *cmpr_end)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	u8 *up = unc;
Kmods SIG 8b815c
+	u8 ch = *cmpr++;
Kmods SIG 8b815c
+	size_t bit = 0;
Kmods SIG 8b815c
+	size_t index = 0;
Kmods SIG 8b815c
+	u16 pair;
Kmods SIG 8b815c
+	size_t offset, length;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Do decompression until pointers are inside range */
Kmods SIG 8b815c
+	while (up < unc_end && cmpr < cmpr_end) {
Kmods SIG 8b815c
+		/* Correct index */
Kmods SIG 8b815c
+		while (unc + s_max_off[index] < up)
Kmods SIG 8b815c
+			index += 1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Check the current flag for zero */
Kmods SIG 8b815c
+		if (!(ch & (1 << bit))) {
Kmods SIG 8b815c
+			/* Just copy byte */
Kmods SIG 8b815c
+			*up++ = *cmpr++;
Kmods SIG 8b815c
+			goto next;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Check for boundary */
Kmods SIG 8b815c
+		if (cmpr + 1 >= cmpr_end)
Kmods SIG 8b815c
+			return -EINVAL;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Read a short from little endian stream */
Kmods SIG 8b815c
+		pair = cmpr[1];
Kmods SIG 8b815c
+		pair <<= 8;
Kmods SIG 8b815c
+		pair |= cmpr[0];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		cmpr += 2;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Translate packed information into offset and length */
Kmods SIG 8b815c
+		length = parse_pair(pair, &offset, index);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Check offset for boundary */
Kmods SIG 8b815c
+		if (unc + offset > up)
Kmods SIG 8b815c
+			return -EINVAL;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Truncate the length if necessary */
Kmods SIG 8b815c
+		if (up + length >= unc_end)
Kmods SIG 8b815c
+			length = unc_end - up;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Now we copy bytes. This is the heart of LZ algorithm. */
Kmods SIG 8b815c
+		for (; length > 0; length--, up++)
Kmods SIG 8b815c
+			*up = *(up - offset);
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+next:
Kmods SIG 8b815c
+		/* Advance flag bit value */
Kmods SIG 8b815c
+		bit = (bit + 1) & 7;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (!bit) {
Kmods SIG 8b815c
+			if (cmpr >= cmpr_end)
Kmods SIG 8b815c
+				break;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			ch = *cmpr++;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* return the size of uncompressed data */
Kmods SIG 8b815c
+	return up - unc;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * 0 - standard compression
Kmods SIG 8b815c
+ * !0 - best compression, requires a lot of cpu
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+struct lznt *get_lznt_ctx(int level)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	struct lznt *r = ntfs_zalloc(level ? offsetof(struct lznt, hash)
Kmods SIG 8b815c
+					   : sizeof(struct lznt));
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (r)
Kmods SIG 8b815c
+		r->std = !level;
Kmods SIG 8b815c
+	return r;
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * compress_lznt
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * Compresses "unc" into "cmpr"
Kmods SIG 8b815c
+ * +x - ok, 'cmpr' contains 'final_compressed_size' bytes of compressed data
Kmods SIG 8b815c
+ * 0 - input buffer is full zero
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+size_t compress_lznt(const void *unc, size_t unc_size, void *cmpr,
Kmods SIG 8b815c
+		     size_t cmpr_size, struct lznt *ctx)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	int err;
Kmods SIG 8b815c
+	size_t (*match)(const u8 *src, struct lznt *ctx);
Kmods SIG 8b815c
+	u8 *p = cmpr;
Kmods SIG 8b815c
+	u8 *end = p + cmpr_size;
Kmods SIG 8b815c
+	const u8 *unc_chunk = unc;
Kmods SIG 8b815c
+	const u8 *unc_end = unc_chunk + unc_size;
Kmods SIG 8b815c
+	bool is_zero = true;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (ctx->std) {
Kmods SIG 8b815c
+		match = &longest_match_std;
Kmods SIG 8b815c
+		memset(ctx->hash, 0, sizeof(ctx->hash));
Kmods SIG 8b815c
+	} else {
Kmods SIG 8b815c
+		match = &longest_match_best;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* compression cycle */
Kmods SIG 8b815c
+	for (; unc_chunk < unc_end; unc_chunk += LZNT_CHUNK_SIZE) {
Kmods SIG 8b815c
+		cmpr_size = 0;
Kmods SIG 8b815c
+		err = compress_chunk(match, unc_chunk, unc_end, p, end,
Kmods SIG 8b815c
+				     &cmpr_size, ctx);
Kmods SIG 8b815c
+		if (err < 0)
Kmods SIG 8b815c
+			return unc_size;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (is_zero && err != LZNT_ERROR_ALL_ZEROS)
Kmods SIG 8b815c
+			is_zero = false;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		p += cmpr_size;
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (p <= end - 2)
Kmods SIG 8b815c
+		p[0] = p[1] = 0;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	return is_zero ? 0 : PtrOffset(cmpr, p);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+/*
Kmods SIG 8b815c
+ * decompress_lznt
Kmods SIG 8b815c
+ *
Kmods SIG 8b815c
+ * decompresses "cmpr" into "unc"
Kmods SIG 8b815c
+ */
Kmods SIG 8b815c
+ssize_t decompress_lznt(const void *cmpr, size_t cmpr_size, void *unc,
Kmods SIG 8b815c
+			size_t unc_size)
Kmods SIG 8b815c
+{
Kmods SIG 8b815c
+	const u8 *cmpr_chunk = cmpr;
Kmods SIG 8b815c
+	const u8 *cmpr_end = cmpr_chunk + cmpr_size;
Kmods SIG 8b815c
+	u8 *unc_chunk = unc;
Kmods SIG 8b815c
+	u8 *unc_end = unc_chunk + unc_size;
Kmods SIG 8b815c
+	u16 chunk_hdr;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	if (cmpr_size < sizeof(short))
Kmods SIG 8b815c
+		return -EINVAL;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* read chunk header */
Kmods SIG 8b815c
+	chunk_hdr = cmpr_chunk[1];
Kmods SIG 8b815c
+	chunk_hdr <<= 8;
Kmods SIG 8b815c
+	chunk_hdr |= cmpr_chunk[0];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* loop through decompressing chunks */
Kmods SIG 8b815c
+	for (;;) {
Kmods SIG 8b815c
+		size_t chunk_size_saved;
Kmods SIG 8b815c
+		size_t unc_use;
Kmods SIG 8b815c
+		size_t cmpr_use = 3 + (chunk_hdr & (LZNT_CHUNK_SIZE - 1));
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Check that the chunk actually fits the supplied buffer */
Kmods SIG 8b815c
+		if (cmpr_chunk + cmpr_use > cmpr_end)
Kmods SIG 8b815c
+			return -EINVAL;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* First make sure the chunk contains compressed data */
Kmods SIG 8b815c
+		if (chunk_hdr & 0x8000) {
Kmods SIG 8b815c
+			/* Decompress a chunk and return if we get an error */
Kmods SIG 8b815c
+			ssize_t err =
Kmods SIG 8b815c
+				decompress_chunk(unc_chunk, unc_end,
Kmods SIG 8b815c
+						 cmpr_chunk + sizeof(chunk_hdr),
Kmods SIG 8b815c
+						 cmpr_chunk + cmpr_use);
Kmods SIG 8b815c
+			if (err < 0)
Kmods SIG 8b815c
+				return err;
Kmods SIG 8b815c
+			unc_use = err;
Kmods SIG 8b815c
+		} else {
Kmods SIG 8b815c
+			/* This chunk does not contain compressed data */
Kmods SIG 8b815c
+			unc_use = unc_chunk + LZNT_CHUNK_SIZE > unc_end
Kmods SIG 8b815c
+					  ? unc_end - unc_chunk
Kmods SIG 8b815c
+					  : LZNT_CHUNK_SIZE;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			if (cmpr_chunk + sizeof(chunk_hdr) + unc_use >
Kmods SIG 8b815c
+			    cmpr_end) {
Kmods SIG 8b815c
+				return -EINVAL;
Kmods SIG 8b815c
+			}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			memcpy(unc_chunk, cmpr_chunk + sizeof(chunk_hdr),
Kmods SIG 8b815c
+			       unc_use);
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Advance pointers */
Kmods SIG 8b815c
+		cmpr_chunk += cmpr_use;
Kmods SIG 8b815c
+		unc_chunk += unc_use;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Check for the end of unc buffer */
Kmods SIG 8b815c
+		if (unc_chunk >= unc_end)
Kmods SIG 8b815c
+			break;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Proceed the next chunk */
Kmods SIG 8b815c
+		if (cmpr_chunk > cmpr_end - 2)
Kmods SIG 8b815c
+			break;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		chunk_size_saved = LZNT_CHUNK_SIZE;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* read chunk header */
Kmods SIG 8b815c
+		chunk_hdr = cmpr_chunk[1];
Kmods SIG 8b815c
+		chunk_hdr <<= 8;
Kmods SIG 8b815c
+		chunk_hdr |= cmpr_chunk[0];
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		if (!chunk_hdr)
Kmods SIG 8b815c
+			break;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+		/* Check the size of unc buffer */
Kmods SIG 8b815c
+		if (unc_use < chunk_size_saved) {
Kmods SIG 8b815c
+			size_t t1 = chunk_size_saved - unc_use;
Kmods SIG 8b815c
+			u8 *t2 = unc_chunk + t1;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			/* 'Zero' memory */
Kmods SIG 8b815c
+			if (t2 >= unc_end)
Kmods SIG 8b815c
+				break;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+			memset(unc_chunk, 0, t1);
Kmods SIG 8b815c
+			unc_chunk = t2;
Kmods SIG 8b815c
+		}
Kmods SIG 8b815c
+	}
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/* Check compression boundary */
Kmods SIG 8b815c
+	if (cmpr_chunk > cmpr_end)
Kmods SIG 8b815c
+		return -EINVAL;
Kmods SIG 8b815c
+
Kmods SIG 8b815c
+	/*
Kmods SIG 8b815c
+	 * The unc size is just a difference between current
Kmods SIG 8b815c
+	 * pointer and original one
Kmods SIG 8b815c
+	 */
Kmods SIG 8b815c
+	return PtrOffset(unc, unc_chunk);
Kmods SIG 8b815c
+}
Kmods SIG 8b815c
-- 
Kmods SIG 8b815c
2.31.1
Kmods SIG 8b815c