xref: /linux/fs/ntfs/lib/decompress_common.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1*f39cd3f7SHyunchul Lee // SPDX-License-Identifier: MIT
2*f39cd3f7SHyunchul Lee /*
3*f39cd3f7SHyunchul Lee  * decompress_common.c - Code shared by the XPRESS and LZX decompressors
4*f39cd3f7SHyunchul Lee  *
5*f39cd3f7SHyunchul Lee  * This is a port of the upstream wimlib "decompress_common.c" which builds
6*f39cd3f7SHyunchul Lee  * subtable-based Huffman decode tables, as opposed to the older
7*f39cd3f7SHyunchul Lee  * binary-tree-based format previously used in this library.  The vectorized
8*f39cd3f7SHyunchul Lee  * (SSE2/AVX2) fill paths are omitted for portability in the kernel.
9*f39cd3f7SHyunchul Lee  *
10*f39cd3f7SHyunchul Lee  * Copyright (C) 2022 Eric Biggers
11*f39cd3f7SHyunchul Lee  */
12*f39cd3f7SHyunchul Lee 
13*f39cd3f7SHyunchul Lee #include "decompress_common.h"
14*f39cd3f7SHyunchul Lee 
15*f39cd3f7SHyunchul Lee /* Compute the number of bits with which a subtable must be indexed for a
16*f39cd3f7SHyunchul Lee  * codeword of length @codeword_len, given that the root table is indexed with
17*f39cd3f7SHyunchul Lee  * @table_bits bits.
18*f39cd3f7SHyunchul Lee  */
19*f39cd3f7SHyunchul Lee static u32 compute_subtable_bits(u32 table_bits,
20*f39cd3f7SHyunchul Lee 				 u32 codeword_len, u16 len_counts[])
21*f39cd3f7SHyunchul Lee {
22*f39cd3f7SHyunchul Lee 	u32 subtable_bits = codeword_len - table_bits;
23*f39cd3f7SHyunchul Lee 	s32 remainder = (s32)1 << subtable_bits;
24*f39cd3f7SHyunchul Lee 
25*f39cd3f7SHyunchul Lee 	for (;;) {
26*f39cd3f7SHyunchul Lee 		remainder -= len_counts[table_bits + subtable_bits];
27*f39cd3f7SHyunchul Lee 		if (remainder <= 0)
28*f39cd3f7SHyunchul Lee 			break;
29*f39cd3f7SHyunchul Lee 		subtable_bits++;
30*f39cd3f7SHyunchul Lee 		remainder <<= 1;
31*f39cd3f7SHyunchul Lee 	}
32*f39cd3f7SHyunchul Lee 	return subtable_bits;
33*f39cd3f7SHyunchul Lee }
34*f39cd3f7SHyunchul Lee 
35*f39cd3f7SHyunchul Lee /* Build the subtables for codewords longer than table_bits. */
36*f39cd3f7SHyunchul Lee static int build_subtables(u16 decode_table[], u32 num_syms, u32 table_bits,
37*f39cd3f7SHyunchul Lee 			   u16 len_counts[], const u16 sorted_syms[], u32 sym_idx,
38*f39cd3f7SHyunchul Lee 			   u32 decode_table_pos, u32 decode_table_size)
39*f39cd3f7SHyunchul Lee {
40*f39cd3f7SHyunchul Lee 	u32 subtable_pos = 1U << table_bits;
41*f39cd3f7SHyunchul Lee 	u32 subtable_bits = table_bits;
42*f39cd3f7SHyunchul Lee 	u32 subtable_prefix = (u32)-1;
43*f39cd3f7SHyunchul Lee 	u32 codeword_len = table_bits + 1;
44*f39cd3f7SHyunchul Lee 	u32 codeword = decode_table_pos << 1;
45*f39cd3f7SHyunchul Lee 	u32 prefix;
46*f39cd3f7SHyunchul Lee 	u16 entry;
47*f39cd3f7SHyunchul Lee 	u32 n;
48*f39cd3f7SHyunchul Lee 
49*f39cd3f7SHyunchul Lee 	for (; sym_idx < num_syms; sym_idx++) {
50*f39cd3f7SHyunchul Lee 		while (len_counts[codeword_len] == 0) {
51*f39cd3f7SHyunchul Lee 			codeword_len++;
52*f39cd3f7SHyunchul Lee 			codeword <<= 1;
53*f39cd3f7SHyunchul Lee 		}
54*f39cd3f7SHyunchul Lee 
55*f39cd3f7SHyunchul Lee 		prefix = codeword >> (codeword_len - table_bits);
56*f39cd3f7SHyunchul Lee 
57*f39cd3f7SHyunchul Lee 		if (prefix != subtable_prefix) {
58*f39cd3f7SHyunchul Lee 			subtable_prefix = prefix;
59*f39cd3f7SHyunchul Lee 			subtable_bits = compute_subtable_bits(table_bits, codeword_len,
60*f39cd3f7SHyunchul Lee 							      len_counts);
61*f39cd3f7SHyunchul Lee 			decode_table[subtable_prefix] =
62*f39cd3f7SHyunchul Lee 				MAKE_DECODE_TABLE_ENTRY(subtable_pos, subtable_bits);
63*f39cd3f7SHyunchul Lee 		}
64*f39cd3f7SHyunchul Lee 
65*f39cd3f7SHyunchul Lee 		entry = MAKE_DECODE_TABLE_ENTRY(sorted_syms[sym_idx],
66*f39cd3f7SHyunchul Lee 						codeword_len - table_bits);
67*f39cd3f7SHyunchul Lee 		n = 1U << (subtable_bits - (codeword_len - table_bits));
68*f39cd3f7SHyunchul Lee 
69*f39cd3f7SHyunchul Lee 		/* Defensive bound check: 'lens' is derived from untrusted
70*f39cd3f7SHyunchul Lee 		 * on-disk compressed data, and subtable growth depends on
71*f39cd3f7SHyunchul Lee 		 * its content.  This should never trigger for a correctly
72*f39cd3f7SHyunchul Lee 		 * sized DECODE_TABLE_ENOUGH() value, but turns a wrong value
73*f39cd3f7SHyunchul Lee 		 * into a clean decode failure instead of writing past the
74*f39cd3f7SHyunchul Lee 		 * caller's decode_table[].
75*f39cd3f7SHyunchul Lee 		 */
76*f39cd3f7SHyunchul Lee 		if (unlikely(subtable_pos + n > decode_table_size))
77*f39cd3f7SHyunchul Lee 			return -1;
78*f39cd3f7SHyunchul Lee 
79*f39cd3f7SHyunchul Lee 		do {
80*f39cd3f7SHyunchul Lee 			decode_table[subtable_pos++] = entry;
81*f39cd3f7SHyunchul Lee 		} while (--n);
82*f39cd3f7SHyunchul Lee 
83*f39cd3f7SHyunchul Lee 		len_counts[codeword_len]--;
84*f39cd3f7SHyunchul Lee 		codeword++;
85*f39cd3f7SHyunchul Lee 	}
86*f39cd3f7SHyunchul Lee 
87*f39cd3f7SHyunchul Lee 	return 0;
88*f39cd3f7SHyunchul Lee }
89*f39cd3f7SHyunchul Lee 
90*f39cd3f7SHyunchul Lee /*
91*f39cd3f7SHyunchul Lee  * Given an alphabet of symbols and the length of each symbol's codeword in a
92*f39cd3f7SHyunchul Lee  * canonical prefix code, build a table for quickly decoding symbols that were
93*f39cd3f7SHyunchul Lee  * encoded with that code.
94*f39cd3f7SHyunchul Lee  *
95*f39cd3f7SHyunchul Lee  * The root table is indexed with 'table_bits' bits.  Codewords not longer than
96*f39cd3f7SHyunchul Lee  * 'table_bits' are decoded directly from the root table.  Longer codewords are
97*f39cd3f7SHyunchul Lee  * decoded via subtables: the corresponding root entry is a pointer (the index
98*f39cd3f7SHyunchul Lee  * of the subtable plus the number of bits with which the subtable is indexed),
99*f39cd3f7SHyunchul Lee  * and the subtable is indexed with the remaining bits of the codeword.
100*f39cd3f7SHyunchul Lee  *
101*f39cd3f7SHyunchul Lee  * Each entry stores both the symbol (high 12 bits) and the codeword length (low
102*f39cd3f7SHyunchul Lee  * 4 bits), so a single lookup yields the symbol and lets the bitstream be
103*f39cd3f7SHyunchul Lee  * advanced by the correct number of bits.
104*f39cd3f7SHyunchul Lee  *
105*f39cd3f7SHyunchul Lee  * @decode_table:  array in which to build the table (declared with
106*f39cd3f7SHyunchul Lee  *		   DECODE_TABLE()).  May alias @lens.
107*f39cd3f7SHyunchul Lee  * @num_syms:      number of symbols in the alphabet.
108*f39cd3f7SHyunchul Lee  * @table_bits:    log2 of the number of root table entries.
109*f39cd3f7SHyunchul Lee  * @lens:         array of @num_syms codeword lengths, indexed by symbol.
110*f39cd3f7SHyunchul Lee  * @max_codeword_len: longest codeword length allowed for this code.
111*f39cd3f7SHyunchul Lee  * @working_space: temporary array declared with DECODE_TABLE_WORKING_SPACE().
112*f39cd3f7SHyunchul Lee  * @decode_table_size: number of u16 entries in @decode_table (i.e.
113*f39cd3f7SHyunchul Lee  *		   ARRAY_SIZE(decode_table) at the call site).  Used only as a
114*f39cd3f7SHyunchul Lee  *		   defensive bound check against @lens-dependent subtable growth.
115*f39cd3f7SHyunchul Lee  *
116*f39cd3f7SHyunchul Lee  * Returns 0 on success, or -1 if the lengths do not form a valid prefix code,
117*f39cd3f7SHyunchul Lee  * or if building the subtables would overflow @decode_table_size entries.
118*f39cd3f7SHyunchul Lee  */
119*f39cd3f7SHyunchul Lee int make_huffman_decode_table(u16 decode_table[], u32 num_syms, u32 table_bits,
120*f39cd3f7SHyunchul Lee 			      const u8 lens[], u32 max_codeword_len,
121*f39cd3f7SHyunchul Lee 			      u16 working_space[], u32 decode_table_size)
122*f39cd3f7SHyunchul Lee {
123*f39cd3f7SHyunchul Lee 	u16 *const len_counts = &working_space[0];
124*f39cd3f7SHyunchul Lee 	u16 *const offsets = &working_space[1 * (max_codeword_len + 1)];
125*f39cd3f7SHyunchul Lee 	u16 *const sorted_syms = &working_space[2 * (max_codeword_len + 1)];
126*f39cd3f7SHyunchul Lee 	u32 decode_table_pos = 0;
127*f39cd3f7SHyunchul Lee 	u32 sym_idx;
128*f39cd3f7SHyunchul Lee 	u32 codeword_len;
129*f39cd3f7SHyunchul Lee 	s32 remainder = 1;
130*f39cd3f7SHyunchul Lee 	void *entry_ptr = decode_table;
131*f39cd3f7SHyunchul Lee 	u32 len;
132*f39cd3f7SHyunchul Lee 	u32 sym;
133*f39cd3f7SHyunchul Lee 
134*f39cd3f7SHyunchul Lee 	/* Count how many codewords have each length, including 0. */
135*f39cd3f7SHyunchul Lee 	for (len = 0; len <= max_codeword_len; len++)
136*f39cd3f7SHyunchul Lee 		len_counts[len] = 0;
137*f39cd3f7SHyunchul Lee 	for (sym = 0; sym < num_syms; sym++)
138*f39cd3f7SHyunchul Lee 		len_counts[lens[sym]]++;
139*f39cd3f7SHyunchul Lee 
140*f39cd3f7SHyunchul Lee 	/* A codeword of length n should require a proportion of the codespace
141*f39cd3f7SHyunchul Lee 	 * equaling (1/2)^n.  The code is complete iff the codespace is exactly
142*f39cd3f7SHyunchul Lee 	 * filled by the lengths.
143*f39cd3f7SHyunchul Lee 	 */
144*f39cd3f7SHyunchul Lee 	for (len = 1; len <= max_codeword_len; len++) {
145*f39cd3f7SHyunchul Lee 		remainder = (remainder << 1) - len_counts[len];
146*f39cd3f7SHyunchul Lee 		if (unlikely(remainder < 0))
147*f39cd3f7SHyunchul Lee 			return -1;	/* over-subscribed */
148*f39cd3f7SHyunchul Lee 	}
149*f39cd3f7SHyunchul Lee 
150*f39cd3f7SHyunchul Lee 	if (remainder != 0) {
151*f39cd3f7SHyunchul Lee 		/* Incomplete code.  Permitted only if the code is empty. */
152*f39cd3f7SHyunchul Lee 		if (unlikely(remainder != (s32)(1U << max_codeword_len)))
153*f39cd3f7SHyunchul Lee 			return -1;
154*f39cd3f7SHyunchul Lee 
155*f39cd3f7SHyunchul Lee 		/* Empty code: zero the root table so lookups yield symbol 0
156*f39cd3f7SHyunchul Lee 		 * without consuming any bits.
157*f39cd3f7SHyunchul Lee 		 */
158*f39cd3f7SHyunchul Lee 		memset(decode_table, 0, sizeof(decode_table[0]) << table_bits);
159*f39cd3f7SHyunchul Lee 		return 0;
160*f39cd3f7SHyunchul Lee 	}
161*f39cd3f7SHyunchul Lee 
162*f39cd3f7SHyunchul Lee 	/* Sort the symbols primarily by increasing codeword length and
163*f39cd3f7SHyunchul Lee 	 * secondarily by increasing symbol value.
164*f39cd3f7SHyunchul Lee 	 */
165*f39cd3f7SHyunchul Lee 	offsets[0] = 0;
166*f39cd3f7SHyunchul Lee 	for (len = 0; len < max_codeword_len; len++)
167*f39cd3f7SHyunchul Lee 		offsets[len + 1] = offsets[len] + len_counts[len];
168*f39cd3f7SHyunchul Lee 	for (sym = 0; sym < num_syms; sym++)
169*f39cd3f7SHyunchul Lee 		sorted_syms[offsets[lens[sym]]++] = sym;
170*f39cd3f7SHyunchul Lee 
171*f39cd3f7SHyunchul Lee 	/* Fill the root table entries for codewords no longer than table_bits. */
172*f39cd3f7SHyunchul Lee 	sym_idx = offsets[0];
173*f39cd3f7SHyunchul Lee 	codeword_len = 1;
174*f39cd3f7SHyunchul Lee 	for (; codeword_len <= table_bits; codeword_len++) {
175*f39cd3f7SHyunchul Lee 		u32 stores_per_loop = 1U << (table_bits - codeword_len);
176*f39cd3f7SHyunchul Lee 		u32 end_sym_idx = sym_idx + len_counts[codeword_len];
177*f39cd3f7SHyunchul Lee 
178*f39cd3f7SHyunchul Lee 		for (; sym_idx < end_sym_idx; sym_idx++) {
179*f39cd3f7SHyunchul Lee 			u16 v = MAKE_DECODE_TABLE_ENTRY(sorted_syms[sym_idx],
180*f39cd3f7SHyunchul Lee 							codeword_len);
181*f39cd3f7SHyunchul Lee 			u32 n = stores_per_loop;
182*f39cd3f7SHyunchul Lee 			u16 *p = entry_ptr;
183*f39cd3f7SHyunchul Lee 
184*f39cd3f7SHyunchul Lee 			do {
185*f39cd3f7SHyunchul Lee 				*p++ = v;
186*f39cd3f7SHyunchul Lee 			} while (--n);
187*f39cd3f7SHyunchul Lee 			entry_ptr = p;
188*f39cd3f7SHyunchul Lee 		}
189*f39cd3f7SHyunchul Lee 	}
190*f39cd3f7SHyunchul Lee 	decode_table_pos = (u16 *)entry_ptr - decode_table;
191*f39cd3f7SHyunchul Lee 
192*f39cd3f7SHyunchul Lee 	/* If all symbols were processed, no subtables are required. */
193*f39cd3f7SHyunchul Lee 	if (sym_idx == num_syms)
194*f39cd3f7SHyunchul Lee 		return 0;
195*f39cd3f7SHyunchul Lee 
196*f39cd3f7SHyunchul Lee 	/* At least one subtable is required.  Process the remaining symbols. */
197*f39cd3f7SHyunchul Lee 	return build_subtables(decode_table, num_syms, table_bits, len_counts,
198*f39cd3f7SHyunchul Lee 			       sorted_syms, sym_idx, decode_table_pos,
199*f39cd3f7SHyunchul Lee 			       decode_table_size);
200*f39cd3f7SHyunchul Lee }
201