1*522e010bSKonstantin Komarov // SPDX-License-Identifier: GPL-2.0-or-later
2*522e010bSKonstantin Komarov /*
3*522e010bSKonstantin Komarov * lzx_decompress.c - A decompressor for the LZX compression format, which can
4*522e010bSKonstantin Komarov * be used in "System Compressed" files. This is based on the code from wimlib.
5*522e010bSKonstantin Komarov * This code only supports a window size (dictionary size) of 32768 bytes, since
6*522e010bSKonstantin Komarov * this is the only size used in System Compression.
7*522e010bSKonstantin Komarov *
8*522e010bSKonstantin Komarov * Copyright (C) 2015 Eric Biggers
9*522e010bSKonstantin Komarov */
10*522e010bSKonstantin Komarov
11*522e010bSKonstantin Komarov #include "decompress_common.h"
12*522e010bSKonstantin Komarov #include "lib.h"
13*522e010bSKonstantin Komarov
14*522e010bSKonstantin Komarov /* Number of literal byte values */
15*522e010bSKonstantin Komarov #define LZX_NUM_CHARS 256
16*522e010bSKonstantin Komarov
17*522e010bSKonstantin Komarov /* The smallest and largest allowed match lengths */
18*522e010bSKonstantin Komarov #define LZX_MIN_MATCH_LEN 2
19*522e010bSKonstantin Komarov #define LZX_MAX_MATCH_LEN 257
20*522e010bSKonstantin Komarov
21*522e010bSKonstantin Komarov /* Number of distinct match lengths that can be represented */
22*522e010bSKonstantin Komarov #define LZX_NUM_LENS (LZX_MAX_MATCH_LEN - LZX_MIN_MATCH_LEN + 1)
23*522e010bSKonstantin Komarov
24*522e010bSKonstantin Komarov /* Number of match lengths for which no length symbol is required */
25*522e010bSKonstantin Komarov #define LZX_NUM_PRIMARY_LENS 7
26*522e010bSKonstantin Komarov #define LZX_NUM_LEN_HEADERS (LZX_NUM_PRIMARY_LENS + 1)
27*522e010bSKonstantin Komarov
28*522e010bSKonstantin Komarov /* Valid values of the 3-bit block type field */
29*522e010bSKonstantin Komarov #define LZX_BLOCKTYPE_VERBATIM 1
30*522e010bSKonstantin Komarov #define LZX_BLOCKTYPE_ALIGNED 2
31*522e010bSKonstantin Komarov #define LZX_BLOCKTYPE_UNCOMPRESSED 3
32*522e010bSKonstantin Komarov
33*522e010bSKonstantin Komarov /* Number of offset slots for a window size of 32768 */
34*522e010bSKonstantin Komarov #define LZX_NUM_OFFSET_SLOTS 30
35*522e010bSKonstantin Komarov
36*522e010bSKonstantin Komarov /* Number of symbols in the main code for a window size of 32768 */
37*522e010bSKonstantin Komarov #define LZX_MAINCODE_NUM_SYMBOLS \
38*522e010bSKonstantin Komarov (LZX_NUM_CHARS + (LZX_NUM_OFFSET_SLOTS * LZX_NUM_LEN_HEADERS))
39*522e010bSKonstantin Komarov
40*522e010bSKonstantin Komarov /* Number of symbols in the length code */
41*522e010bSKonstantin Komarov #define LZX_LENCODE_NUM_SYMBOLS (LZX_NUM_LENS - LZX_NUM_PRIMARY_LENS)
42*522e010bSKonstantin Komarov
43*522e010bSKonstantin Komarov /* Number of symbols in the precode */
44*522e010bSKonstantin Komarov #define LZX_PRECODE_NUM_SYMBOLS 20
45*522e010bSKonstantin Komarov
46*522e010bSKonstantin Komarov /* Number of bits in which each precode codeword length is represented */
47*522e010bSKonstantin Komarov #define LZX_PRECODE_ELEMENT_SIZE 4
48*522e010bSKonstantin Komarov
49*522e010bSKonstantin Komarov /* Number of low-order bits of each match offset that are entropy-encoded in
50*522e010bSKonstantin Komarov * aligned offset blocks
51*522e010bSKonstantin Komarov */
52*522e010bSKonstantin Komarov #define LZX_NUM_ALIGNED_OFFSET_BITS 3
53*522e010bSKonstantin Komarov
54*522e010bSKonstantin Komarov /* Number of symbols in the aligned offset code */
55*522e010bSKonstantin Komarov #define LZX_ALIGNEDCODE_NUM_SYMBOLS (1 << LZX_NUM_ALIGNED_OFFSET_BITS)
56*522e010bSKonstantin Komarov
57*522e010bSKonstantin Komarov /* Mask for the match offset bits that are entropy-encoded in aligned offset
58*522e010bSKonstantin Komarov * blocks
59*522e010bSKonstantin Komarov */
60*522e010bSKonstantin Komarov #define LZX_ALIGNED_OFFSET_BITMASK ((1 << LZX_NUM_ALIGNED_OFFSET_BITS) - 1)
61*522e010bSKonstantin Komarov
62*522e010bSKonstantin Komarov /* Number of bits in which each aligned offset codeword length is represented */
63*522e010bSKonstantin Komarov #define LZX_ALIGNEDCODE_ELEMENT_SIZE 3
64*522e010bSKonstantin Komarov
65*522e010bSKonstantin Komarov /* Maximum lengths (in bits) of the codewords in each Huffman code */
66*522e010bSKonstantin Komarov #define LZX_MAX_MAIN_CODEWORD_LEN 16
67*522e010bSKonstantin Komarov #define LZX_MAX_LEN_CODEWORD_LEN 16
68*522e010bSKonstantin Komarov #define LZX_MAX_PRE_CODEWORD_LEN ((1 << LZX_PRECODE_ELEMENT_SIZE) - 1)
69*522e010bSKonstantin Komarov #define LZX_MAX_ALIGNED_CODEWORD_LEN ((1 << LZX_ALIGNEDCODE_ELEMENT_SIZE) - 1)
70*522e010bSKonstantin Komarov
71*522e010bSKonstantin Komarov /* The default "filesize" value used in pre/post-processing. In the LZX format
72*522e010bSKonstantin Komarov * used in cabinet files this value must be given to the decompressor, whereas
73*522e010bSKonstantin Komarov * in the LZX format used in WIM files and system-compressed files this value is
74*522e010bSKonstantin Komarov * fixed at 12000000.
75*522e010bSKonstantin Komarov */
76*522e010bSKonstantin Komarov #define LZX_DEFAULT_FILESIZE 12000000
77*522e010bSKonstantin Komarov
78*522e010bSKonstantin Komarov /* Assumed block size when the encoded block size begins with a 0 bit. */
79*522e010bSKonstantin Komarov #define LZX_DEFAULT_BLOCK_SIZE 32768
80*522e010bSKonstantin Komarov
81*522e010bSKonstantin Komarov /* Number of offsets in the recent (or "repeat") offsets queue. */
82*522e010bSKonstantin Komarov #define LZX_NUM_RECENT_OFFSETS 3
83*522e010bSKonstantin Komarov
84*522e010bSKonstantin Komarov /* These values are chosen for fast decompression. */
85*522e010bSKonstantin Komarov #define LZX_MAINCODE_TABLEBITS 11
86*522e010bSKonstantin Komarov #define LZX_LENCODE_TABLEBITS 10
87*522e010bSKonstantin Komarov #define LZX_PRECODE_TABLEBITS 6
88*522e010bSKonstantin Komarov #define LZX_ALIGNEDCODE_TABLEBITS 7
89*522e010bSKonstantin Komarov
90*522e010bSKonstantin Komarov #define LZX_READ_LENS_MAX_OVERRUN 50
91*522e010bSKonstantin Komarov
92*522e010bSKonstantin Komarov /* Mapping: offset slot => first match offset that uses that offset slot.
93*522e010bSKonstantin Komarov */
94*522e010bSKonstantin Komarov static const u32 lzx_offset_slot_base[LZX_NUM_OFFSET_SLOTS + 1] = {
95*522e010bSKonstantin Komarov 0, 1, 2, 3, 4, /* 0 --- 4 */
96*522e010bSKonstantin Komarov 6, 8, 12, 16, 24, /* 5 --- 9 */
97*522e010bSKonstantin Komarov 32, 48, 64, 96, 128, /* 10 --- 14 */
98*522e010bSKonstantin Komarov 192, 256, 384, 512, 768, /* 15 --- 19 */
99*522e010bSKonstantin Komarov 1024, 1536, 2048, 3072, 4096, /* 20 --- 24 */
100*522e010bSKonstantin Komarov 6144, 8192, 12288, 16384, 24576, /* 25 --- 29 */
101*522e010bSKonstantin Komarov 32768, /* extra */
102*522e010bSKonstantin Komarov };
103*522e010bSKonstantin Komarov
104*522e010bSKonstantin Komarov /* Mapping: offset slot => how many extra bits must be read and added to the
105*522e010bSKonstantin Komarov * corresponding offset slot base to decode the match offset.
106*522e010bSKonstantin Komarov */
107*522e010bSKonstantin Komarov static const u8 lzx_extra_offset_bits[LZX_NUM_OFFSET_SLOTS] = {
108*522e010bSKonstantin Komarov 0, 0, 0, 0, 1,
109*522e010bSKonstantin Komarov 1, 2, 2, 3, 3,
110*522e010bSKonstantin Komarov 4, 4, 5, 5, 6,
111*522e010bSKonstantin Komarov 6, 7, 7, 8, 8,
112*522e010bSKonstantin Komarov 9, 9, 10, 10, 11,
113*522e010bSKonstantin Komarov 11, 12, 12, 13, 13,
114*522e010bSKonstantin Komarov };
115*522e010bSKonstantin Komarov
116*522e010bSKonstantin Komarov /* Reusable heap-allocated memory for LZX decompression */
117*522e010bSKonstantin Komarov struct lzx_decompressor {
118*522e010bSKonstantin Komarov
119*522e010bSKonstantin Komarov /* Huffman decoding tables, and arrays that map symbols to codeword
120*522e010bSKonstantin Komarov * lengths
121*522e010bSKonstantin Komarov */
122*522e010bSKonstantin Komarov
123*522e010bSKonstantin Komarov u16 maincode_decode_table[(1 << LZX_MAINCODE_TABLEBITS) +
124*522e010bSKonstantin Komarov (LZX_MAINCODE_NUM_SYMBOLS * 2)];
125*522e010bSKonstantin Komarov u8 maincode_lens[LZX_MAINCODE_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
126*522e010bSKonstantin Komarov
127*522e010bSKonstantin Komarov
128*522e010bSKonstantin Komarov u16 lencode_decode_table[(1 << LZX_LENCODE_TABLEBITS) +
129*522e010bSKonstantin Komarov (LZX_LENCODE_NUM_SYMBOLS * 2)];
130*522e010bSKonstantin Komarov u8 lencode_lens[LZX_LENCODE_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
131*522e010bSKonstantin Komarov
132*522e010bSKonstantin Komarov
133*522e010bSKonstantin Komarov u16 alignedcode_decode_table[(1 << LZX_ALIGNEDCODE_TABLEBITS) +
134*522e010bSKonstantin Komarov (LZX_ALIGNEDCODE_NUM_SYMBOLS * 2)];
135*522e010bSKonstantin Komarov u8 alignedcode_lens[LZX_ALIGNEDCODE_NUM_SYMBOLS];
136*522e010bSKonstantin Komarov
137*522e010bSKonstantin Komarov u16 precode_decode_table[(1 << LZX_PRECODE_TABLEBITS) +
138*522e010bSKonstantin Komarov (LZX_PRECODE_NUM_SYMBOLS * 2)];
139*522e010bSKonstantin Komarov u8 precode_lens[LZX_PRECODE_NUM_SYMBOLS];
140*522e010bSKonstantin Komarov
141*522e010bSKonstantin Komarov /* Temporary space for make_huffman_decode_table() */
142*522e010bSKonstantin Komarov u16 working_space[2 * (1 + LZX_MAX_MAIN_CODEWORD_LEN) +
143*522e010bSKonstantin Komarov LZX_MAINCODE_NUM_SYMBOLS];
144*522e010bSKonstantin Komarov };
145*522e010bSKonstantin Komarov
undo_e8_translation(void * target,s32 input_pos)146*522e010bSKonstantin Komarov static void undo_e8_translation(void *target, s32 input_pos)
147*522e010bSKonstantin Komarov {
148*522e010bSKonstantin Komarov s32 abs_offset, rel_offset;
149*522e010bSKonstantin Komarov
150*522e010bSKonstantin Komarov abs_offset = get_unaligned_le32(target);
151*522e010bSKonstantin Komarov if (abs_offset >= 0) {
152*522e010bSKonstantin Komarov if (abs_offset < LZX_DEFAULT_FILESIZE) {
153*522e010bSKonstantin Komarov /* "good translation" */
154*522e010bSKonstantin Komarov rel_offset = abs_offset - input_pos;
155*522e010bSKonstantin Komarov put_unaligned_le32(rel_offset, target);
156*522e010bSKonstantin Komarov }
157*522e010bSKonstantin Komarov } else {
158*522e010bSKonstantin Komarov if (abs_offset >= -input_pos) {
159*522e010bSKonstantin Komarov /* "compensating translation" */
160*522e010bSKonstantin Komarov rel_offset = abs_offset + LZX_DEFAULT_FILESIZE;
161*522e010bSKonstantin Komarov put_unaligned_le32(rel_offset, target);
162*522e010bSKonstantin Komarov }
163*522e010bSKonstantin Komarov }
164*522e010bSKonstantin Komarov }
165*522e010bSKonstantin Komarov
166*522e010bSKonstantin Komarov /*
167*522e010bSKonstantin Komarov * Undo the 'E8' preprocessing used in LZX. Before compression, the
168*522e010bSKonstantin Komarov * uncompressed data was preprocessed by changing the targets of suspected x86
169*522e010bSKonstantin Komarov * CALL instructions from relative offsets to absolute offsets. After
170*522e010bSKonstantin Komarov * match/literal decoding, the decompressor must undo the translation.
171*522e010bSKonstantin Komarov */
lzx_postprocess(u8 * data,u32 size)172*522e010bSKonstantin Komarov static void lzx_postprocess(u8 *data, u32 size)
173*522e010bSKonstantin Komarov {
174*522e010bSKonstantin Komarov /*
175*522e010bSKonstantin Komarov * A worthwhile optimization is to push the end-of-buffer check into the
176*522e010bSKonstantin Komarov * relatively rare E8 case. This is possible if we replace the last six
177*522e010bSKonstantin Komarov * bytes of data with E8 bytes; then we are guaranteed to hit an E8 byte
178*522e010bSKonstantin Komarov * before reaching end-of-buffer. In addition, this scheme guarantees
179*522e010bSKonstantin Komarov * that no translation can begin following an E8 byte in the last 10
180*522e010bSKonstantin Komarov * bytes because a 4-byte offset containing E8 as its high byte is a
181*522e010bSKonstantin Komarov * large negative number that is not valid for translation. That is
182*522e010bSKonstantin Komarov * exactly what we need.
183*522e010bSKonstantin Komarov */
184*522e010bSKonstantin Komarov u8 *tail;
185*522e010bSKonstantin Komarov u8 saved_bytes[6];
186*522e010bSKonstantin Komarov u8 *p;
187*522e010bSKonstantin Komarov
188*522e010bSKonstantin Komarov if (size <= 10)
189*522e010bSKonstantin Komarov return;
190*522e010bSKonstantin Komarov
191*522e010bSKonstantin Komarov tail = &data[size - 6];
192*522e010bSKonstantin Komarov memcpy(saved_bytes, tail, 6);
193*522e010bSKonstantin Komarov memset(tail, 0xE8, 6);
194*522e010bSKonstantin Komarov p = data;
195*522e010bSKonstantin Komarov for (;;) {
196*522e010bSKonstantin Komarov while (*p != 0xE8)
197*522e010bSKonstantin Komarov p++;
198*522e010bSKonstantin Komarov if (p >= tail)
199*522e010bSKonstantin Komarov break;
200*522e010bSKonstantin Komarov undo_e8_translation(p + 1, p - data);
201*522e010bSKonstantin Komarov p += 5;
202*522e010bSKonstantin Komarov }
203*522e010bSKonstantin Komarov memcpy(tail, saved_bytes, 6);
204*522e010bSKonstantin Komarov }
205*522e010bSKonstantin Komarov
206*522e010bSKonstantin Komarov /* Read a Huffman-encoded symbol using the precode. */
read_presym(const struct lzx_decompressor * d,struct input_bitstream * is)207*522e010bSKonstantin Komarov static forceinline u32 read_presym(const struct lzx_decompressor *d,
208*522e010bSKonstantin Komarov struct input_bitstream *is)
209*522e010bSKonstantin Komarov {
210*522e010bSKonstantin Komarov return read_huffsym(is, d->precode_decode_table,
211*522e010bSKonstantin Komarov LZX_PRECODE_TABLEBITS, LZX_MAX_PRE_CODEWORD_LEN);
212*522e010bSKonstantin Komarov }
213*522e010bSKonstantin Komarov
214*522e010bSKonstantin Komarov /* Read a Huffman-encoded symbol using the main code. */
read_mainsym(const struct lzx_decompressor * d,struct input_bitstream * is)215*522e010bSKonstantin Komarov static forceinline u32 read_mainsym(const struct lzx_decompressor *d,
216*522e010bSKonstantin Komarov struct input_bitstream *is)
217*522e010bSKonstantin Komarov {
218*522e010bSKonstantin Komarov return read_huffsym(is, d->maincode_decode_table,
219*522e010bSKonstantin Komarov LZX_MAINCODE_TABLEBITS, LZX_MAX_MAIN_CODEWORD_LEN);
220*522e010bSKonstantin Komarov }
221*522e010bSKonstantin Komarov
222*522e010bSKonstantin Komarov /* Read a Huffman-encoded symbol using the length code. */
read_lensym(const struct lzx_decompressor * d,struct input_bitstream * is)223*522e010bSKonstantin Komarov static forceinline u32 read_lensym(const struct lzx_decompressor *d,
224*522e010bSKonstantin Komarov struct input_bitstream *is)
225*522e010bSKonstantin Komarov {
226*522e010bSKonstantin Komarov return read_huffsym(is, d->lencode_decode_table,
227*522e010bSKonstantin Komarov LZX_LENCODE_TABLEBITS, LZX_MAX_LEN_CODEWORD_LEN);
228*522e010bSKonstantin Komarov }
229*522e010bSKonstantin Komarov
230*522e010bSKonstantin Komarov /* Read a Huffman-encoded symbol using the aligned offset code. */
read_alignedsym(const struct lzx_decompressor * d,struct input_bitstream * is)231*522e010bSKonstantin Komarov static forceinline u32 read_alignedsym(const struct lzx_decompressor *d,
232*522e010bSKonstantin Komarov struct input_bitstream *is)
233*522e010bSKonstantin Komarov {
234*522e010bSKonstantin Komarov return read_huffsym(is, d->alignedcode_decode_table,
235*522e010bSKonstantin Komarov LZX_ALIGNEDCODE_TABLEBITS,
236*522e010bSKonstantin Komarov LZX_MAX_ALIGNED_CODEWORD_LEN);
237*522e010bSKonstantin Komarov }
238*522e010bSKonstantin Komarov
239*522e010bSKonstantin Komarov /*
240*522e010bSKonstantin Komarov * Read the precode from the compressed input bitstream, then use it to decode
241*522e010bSKonstantin Komarov * @num_lens codeword length values.
242*522e010bSKonstantin Komarov *
243*522e010bSKonstantin Komarov * @is: The input bitstream.
244*522e010bSKonstantin Komarov *
245*522e010bSKonstantin Komarov * @lens: An array that contains the length values from the previous time
246*522e010bSKonstantin Komarov * the codeword lengths for this Huffman code were read, or all 0's
247*522e010bSKonstantin Komarov * if this is the first time. This array must have at least
248*522e010bSKonstantin Komarov * (@num_lens + LZX_READ_LENS_MAX_OVERRUN) entries.
249*522e010bSKonstantin Komarov *
250*522e010bSKonstantin Komarov * @num_lens: Number of length values to decode.
251*522e010bSKonstantin Komarov *
252*522e010bSKonstantin Komarov * Returns 0 on success, or -1 if the data was invalid.
253*522e010bSKonstantin Komarov */
lzx_read_codeword_lens(struct lzx_decompressor * d,struct input_bitstream * is,u8 * lens,u32 num_lens)254*522e010bSKonstantin Komarov static int lzx_read_codeword_lens(struct lzx_decompressor *d,
255*522e010bSKonstantin Komarov struct input_bitstream *is,
256*522e010bSKonstantin Komarov u8 *lens, u32 num_lens)
257*522e010bSKonstantin Komarov {
258*522e010bSKonstantin Komarov u8 *len_ptr = lens;
259*522e010bSKonstantin Komarov u8 *lens_end = lens + num_lens;
260*522e010bSKonstantin Komarov int i;
261*522e010bSKonstantin Komarov
262*522e010bSKonstantin Komarov /* Read the lengths of the precode codewords. These are given
263*522e010bSKonstantin Komarov * explicitly.
264*522e010bSKonstantin Komarov */
265*522e010bSKonstantin Komarov for (i = 0; i < LZX_PRECODE_NUM_SYMBOLS; i++) {
266*522e010bSKonstantin Komarov d->precode_lens[i] =
267*522e010bSKonstantin Komarov bitstream_read_bits(is, LZX_PRECODE_ELEMENT_SIZE);
268*522e010bSKonstantin Komarov }
269*522e010bSKonstantin Komarov
270*522e010bSKonstantin Komarov /* Make the decoding table for the precode. */
271*522e010bSKonstantin Komarov if (make_huffman_decode_table(d->precode_decode_table,
272*522e010bSKonstantin Komarov LZX_PRECODE_NUM_SYMBOLS,
273*522e010bSKonstantin Komarov LZX_PRECODE_TABLEBITS,
274*522e010bSKonstantin Komarov d->precode_lens,
275*522e010bSKonstantin Komarov LZX_MAX_PRE_CODEWORD_LEN,
276*522e010bSKonstantin Komarov d->working_space))
277*522e010bSKonstantin Komarov return -1;
278*522e010bSKonstantin Komarov
279*522e010bSKonstantin Komarov /* Decode the codeword lengths. */
280*522e010bSKonstantin Komarov do {
281*522e010bSKonstantin Komarov u32 presym;
282*522e010bSKonstantin Komarov u8 len;
283*522e010bSKonstantin Komarov
284*522e010bSKonstantin Komarov /* Read the next precode symbol. */
285*522e010bSKonstantin Komarov presym = read_presym(d, is);
286*522e010bSKonstantin Komarov if (presym < 17) {
287*522e010bSKonstantin Komarov /* Difference from old length */
288*522e010bSKonstantin Komarov len = *len_ptr - presym;
289*522e010bSKonstantin Komarov if ((s8)len < 0)
290*522e010bSKonstantin Komarov len += 17;
291*522e010bSKonstantin Komarov *len_ptr++ = len;
292*522e010bSKonstantin Komarov } else {
293*522e010bSKonstantin Komarov /* Special RLE values */
294*522e010bSKonstantin Komarov
295*522e010bSKonstantin Komarov u32 run_len;
296*522e010bSKonstantin Komarov
297*522e010bSKonstantin Komarov if (presym == 17) {
298*522e010bSKonstantin Komarov /* Run of 0's */
299*522e010bSKonstantin Komarov run_len = 4 + bitstream_read_bits(is, 4);
300*522e010bSKonstantin Komarov len = 0;
301*522e010bSKonstantin Komarov } else if (presym == 18) {
302*522e010bSKonstantin Komarov /* Longer run of 0's */
303*522e010bSKonstantin Komarov run_len = 20 + bitstream_read_bits(is, 5);
304*522e010bSKonstantin Komarov len = 0;
305*522e010bSKonstantin Komarov } else {
306*522e010bSKonstantin Komarov /* Run of identical lengths */
307*522e010bSKonstantin Komarov run_len = 4 + bitstream_read_bits(is, 1);
308*522e010bSKonstantin Komarov presym = read_presym(d, is);
309*522e010bSKonstantin Komarov if (presym > 17)
310*522e010bSKonstantin Komarov return -1;
311*522e010bSKonstantin Komarov len = *len_ptr - presym;
312*522e010bSKonstantin Komarov if ((s8)len < 0)
313*522e010bSKonstantin Komarov len += 17;
314*522e010bSKonstantin Komarov }
315*522e010bSKonstantin Komarov
316*522e010bSKonstantin Komarov do {
317*522e010bSKonstantin Komarov *len_ptr++ = len;
318*522e010bSKonstantin Komarov } while (--run_len);
319*522e010bSKonstantin Komarov /* Worst case overrun is when presym == 18,
320*522e010bSKonstantin Komarov * run_len == 20 + 31, and only 1 length was remaining.
321*522e010bSKonstantin Komarov * So LZX_READ_LENS_MAX_OVERRUN == 50.
322*522e010bSKonstantin Komarov *
323*522e010bSKonstantin Komarov * Overrun while reading the first half of maincode_lens
324*522e010bSKonstantin Komarov * can corrupt the previous values in the second half.
325*522e010bSKonstantin Komarov * This doesn't really matter because the resulting
326*522e010bSKonstantin Komarov * lengths will still be in range, and data that
327*522e010bSKonstantin Komarov * generates overruns is invalid anyway.
328*522e010bSKonstantin Komarov */
329*522e010bSKonstantin Komarov }
330*522e010bSKonstantin Komarov } while (len_ptr < lens_end);
331*522e010bSKonstantin Komarov
332*522e010bSKonstantin Komarov return 0;
333*522e010bSKonstantin Komarov }
334*522e010bSKonstantin Komarov
335*522e010bSKonstantin Komarov /*
336*522e010bSKonstantin Komarov * Read the header of an LZX block and save the block type and (uncompressed)
337*522e010bSKonstantin Komarov * size in *block_type_ret and *block_size_ret, respectively.
338*522e010bSKonstantin Komarov *
339*522e010bSKonstantin Komarov * If the block is compressed, also update the Huffman decode @tables with the
340*522e010bSKonstantin Komarov * new Huffman codes. If the block is uncompressed, also update the match
341*522e010bSKonstantin Komarov * offset @queue with the new match offsets.
342*522e010bSKonstantin Komarov *
343*522e010bSKonstantin Komarov * Return 0 on success, or -1 if the data was invalid.
344*522e010bSKonstantin Komarov */
lzx_read_block_header(struct lzx_decompressor * d,struct input_bitstream * is,int * block_type_ret,u32 * block_size_ret,u32 recent_offsets[])345*522e010bSKonstantin Komarov static int lzx_read_block_header(struct lzx_decompressor *d,
346*522e010bSKonstantin Komarov struct input_bitstream *is,
347*522e010bSKonstantin Komarov int *block_type_ret,
348*522e010bSKonstantin Komarov u32 *block_size_ret,
349*522e010bSKonstantin Komarov u32 recent_offsets[])
350*522e010bSKonstantin Komarov {
351*522e010bSKonstantin Komarov int block_type;
352*522e010bSKonstantin Komarov u32 block_size;
353*522e010bSKonstantin Komarov int i;
354*522e010bSKonstantin Komarov
355*522e010bSKonstantin Komarov bitstream_ensure_bits(is, 4);
356*522e010bSKonstantin Komarov
357*522e010bSKonstantin Komarov /* The first three bits tell us what kind of block it is, and should be
358*522e010bSKonstantin Komarov * one of the LZX_BLOCKTYPE_* values.
359*522e010bSKonstantin Komarov */
360*522e010bSKonstantin Komarov block_type = bitstream_pop_bits(is, 3);
361*522e010bSKonstantin Komarov
362*522e010bSKonstantin Komarov /* Read the block size. */
363*522e010bSKonstantin Komarov if (bitstream_pop_bits(is, 1)) {
364*522e010bSKonstantin Komarov block_size = LZX_DEFAULT_BLOCK_SIZE;
365*522e010bSKonstantin Komarov } else {
366*522e010bSKonstantin Komarov block_size = 0;
367*522e010bSKonstantin Komarov block_size |= bitstream_read_bits(is, 8);
368*522e010bSKonstantin Komarov block_size <<= 8;
369*522e010bSKonstantin Komarov block_size |= bitstream_read_bits(is, 8);
370*522e010bSKonstantin Komarov }
371*522e010bSKonstantin Komarov
372*522e010bSKonstantin Komarov switch (block_type) {
373*522e010bSKonstantin Komarov
374*522e010bSKonstantin Komarov case LZX_BLOCKTYPE_ALIGNED:
375*522e010bSKonstantin Komarov
376*522e010bSKonstantin Komarov /* Read the aligned offset code and prepare its decode table.
377*522e010bSKonstantin Komarov */
378*522e010bSKonstantin Komarov
379*522e010bSKonstantin Komarov for (i = 0; i < LZX_ALIGNEDCODE_NUM_SYMBOLS; i++) {
380*522e010bSKonstantin Komarov d->alignedcode_lens[i] =
381*522e010bSKonstantin Komarov bitstream_read_bits(is,
382*522e010bSKonstantin Komarov LZX_ALIGNEDCODE_ELEMENT_SIZE);
383*522e010bSKonstantin Komarov }
384*522e010bSKonstantin Komarov
385*522e010bSKonstantin Komarov if (make_huffman_decode_table(d->alignedcode_decode_table,
386*522e010bSKonstantin Komarov LZX_ALIGNEDCODE_NUM_SYMBOLS,
387*522e010bSKonstantin Komarov LZX_ALIGNEDCODE_TABLEBITS,
388*522e010bSKonstantin Komarov d->alignedcode_lens,
389*522e010bSKonstantin Komarov LZX_MAX_ALIGNED_CODEWORD_LEN,
390*522e010bSKonstantin Komarov d->working_space))
391*522e010bSKonstantin Komarov return -1;
392*522e010bSKonstantin Komarov
393*522e010bSKonstantin Komarov /* Fall though, since the rest of the header for aligned offset
394*522e010bSKonstantin Komarov * blocks is the same as that for verbatim blocks.
395*522e010bSKonstantin Komarov */
396*522e010bSKonstantin Komarov fallthrough;
397*522e010bSKonstantin Komarov
398*522e010bSKonstantin Komarov case LZX_BLOCKTYPE_VERBATIM:
399*522e010bSKonstantin Komarov
400*522e010bSKonstantin Komarov /* Read the main code and prepare its decode table.
401*522e010bSKonstantin Komarov *
402*522e010bSKonstantin Komarov * Note that the codeword lengths in the main code are encoded
403*522e010bSKonstantin Komarov * in two parts: one part for literal symbols, and one part for
404*522e010bSKonstantin Komarov * match symbols.
405*522e010bSKonstantin Komarov */
406*522e010bSKonstantin Komarov
407*522e010bSKonstantin Komarov if (lzx_read_codeword_lens(d, is, d->maincode_lens,
408*522e010bSKonstantin Komarov LZX_NUM_CHARS))
409*522e010bSKonstantin Komarov return -1;
410*522e010bSKonstantin Komarov
411*522e010bSKonstantin Komarov if (lzx_read_codeword_lens(d, is,
412*522e010bSKonstantin Komarov d->maincode_lens + LZX_NUM_CHARS,
413*522e010bSKonstantin Komarov LZX_MAINCODE_NUM_SYMBOLS - LZX_NUM_CHARS))
414*522e010bSKonstantin Komarov return -1;
415*522e010bSKonstantin Komarov
416*522e010bSKonstantin Komarov if (make_huffman_decode_table(d->maincode_decode_table,
417*522e010bSKonstantin Komarov LZX_MAINCODE_NUM_SYMBOLS,
418*522e010bSKonstantin Komarov LZX_MAINCODE_TABLEBITS,
419*522e010bSKonstantin Komarov d->maincode_lens,
420*522e010bSKonstantin Komarov LZX_MAX_MAIN_CODEWORD_LEN,
421*522e010bSKonstantin Komarov d->working_space))
422*522e010bSKonstantin Komarov return -1;
423*522e010bSKonstantin Komarov
424*522e010bSKonstantin Komarov /* Read the length code and prepare its decode table. */
425*522e010bSKonstantin Komarov
426*522e010bSKonstantin Komarov if (lzx_read_codeword_lens(d, is, d->lencode_lens,
427*522e010bSKonstantin Komarov LZX_LENCODE_NUM_SYMBOLS))
428*522e010bSKonstantin Komarov return -1;
429*522e010bSKonstantin Komarov
430*522e010bSKonstantin Komarov if (make_huffman_decode_table(d->lencode_decode_table,
431*522e010bSKonstantin Komarov LZX_LENCODE_NUM_SYMBOLS,
432*522e010bSKonstantin Komarov LZX_LENCODE_TABLEBITS,
433*522e010bSKonstantin Komarov d->lencode_lens,
434*522e010bSKonstantin Komarov LZX_MAX_LEN_CODEWORD_LEN,
435*522e010bSKonstantin Komarov d->working_space))
436*522e010bSKonstantin Komarov return -1;
437*522e010bSKonstantin Komarov
438*522e010bSKonstantin Komarov break;
439*522e010bSKonstantin Komarov
440*522e010bSKonstantin Komarov case LZX_BLOCKTYPE_UNCOMPRESSED:
441*522e010bSKonstantin Komarov
442*522e010bSKonstantin Komarov /* Before reading the three recent offsets from the uncompressed
443*522e010bSKonstantin Komarov * block header, the stream must be aligned on a 16-bit
444*522e010bSKonstantin Komarov * boundary. But if the stream is *already* aligned, then the
445*522e010bSKonstantin Komarov * next 16 bits must be discarded.
446*522e010bSKonstantin Komarov */
447*522e010bSKonstantin Komarov bitstream_ensure_bits(is, 1);
448*522e010bSKonstantin Komarov bitstream_align(is);
449*522e010bSKonstantin Komarov
450*522e010bSKonstantin Komarov recent_offsets[0] = bitstream_read_u32(is);
451*522e010bSKonstantin Komarov recent_offsets[1] = bitstream_read_u32(is);
452*522e010bSKonstantin Komarov recent_offsets[2] = bitstream_read_u32(is);
453*522e010bSKonstantin Komarov
454*522e010bSKonstantin Komarov /* Offsets of 0 are invalid. */
455*522e010bSKonstantin Komarov if (recent_offsets[0] == 0 || recent_offsets[1] == 0 ||
456*522e010bSKonstantin Komarov recent_offsets[2] == 0)
457*522e010bSKonstantin Komarov return -1;
458*522e010bSKonstantin Komarov break;
459*522e010bSKonstantin Komarov
460*522e010bSKonstantin Komarov default:
461*522e010bSKonstantin Komarov /* Unrecognized block type. */
462*522e010bSKonstantin Komarov return -1;
463*522e010bSKonstantin Komarov }
464*522e010bSKonstantin Komarov
465*522e010bSKonstantin Komarov *block_type_ret = block_type;
466*522e010bSKonstantin Komarov *block_size_ret = block_size;
467*522e010bSKonstantin Komarov return 0;
468*522e010bSKonstantin Komarov }
469*522e010bSKonstantin Komarov
470*522e010bSKonstantin Komarov /* Decompress a block of LZX-compressed data. */
lzx_decompress_block(const struct lzx_decompressor * d,struct input_bitstream * is,int block_type,u32 block_size,u8 * const out_begin,u8 * out_next,u32 recent_offsets[])471*522e010bSKonstantin Komarov static int lzx_decompress_block(const struct lzx_decompressor *d,
472*522e010bSKonstantin Komarov struct input_bitstream *is,
473*522e010bSKonstantin Komarov int block_type, u32 block_size,
474*522e010bSKonstantin Komarov u8 * const out_begin, u8 *out_next,
475*522e010bSKonstantin Komarov u32 recent_offsets[])
476*522e010bSKonstantin Komarov {
477*522e010bSKonstantin Komarov u8 * const block_end = out_next + block_size;
478*522e010bSKonstantin Komarov u32 ones_if_aligned = 0U - (block_type == LZX_BLOCKTYPE_ALIGNED);
479*522e010bSKonstantin Komarov
480*522e010bSKonstantin Komarov do {
481*522e010bSKonstantin Komarov u32 mainsym;
482*522e010bSKonstantin Komarov u32 match_len;
483*522e010bSKonstantin Komarov u32 match_offset;
484*522e010bSKonstantin Komarov u32 offset_slot;
485*522e010bSKonstantin Komarov u32 num_extra_bits;
486*522e010bSKonstantin Komarov
487*522e010bSKonstantin Komarov mainsym = read_mainsym(d, is);
488*522e010bSKonstantin Komarov if (mainsym < LZX_NUM_CHARS) {
489*522e010bSKonstantin Komarov /* Literal */
490*522e010bSKonstantin Komarov *out_next++ = mainsym;
491*522e010bSKonstantin Komarov continue;
492*522e010bSKonstantin Komarov }
493*522e010bSKonstantin Komarov
494*522e010bSKonstantin Komarov /* Match */
495*522e010bSKonstantin Komarov
496*522e010bSKonstantin Komarov /* Decode the length header and offset slot. */
497*522e010bSKonstantin Komarov mainsym -= LZX_NUM_CHARS;
498*522e010bSKonstantin Komarov match_len = mainsym % LZX_NUM_LEN_HEADERS;
499*522e010bSKonstantin Komarov offset_slot = mainsym / LZX_NUM_LEN_HEADERS;
500*522e010bSKonstantin Komarov
501*522e010bSKonstantin Komarov /* If needed, read a length symbol to decode the full length. */
502*522e010bSKonstantin Komarov if (match_len == LZX_NUM_PRIMARY_LENS)
503*522e010bSKonstantin Komarov match_len += read_lensym(d, is);
504*522e010bSKonstantin Komarov match_len += LZX_MIN_MATCH_LEN;
505*522e010bSKonstantin Komarov
506*522e010bSKonstantin Komarov if (offset_slot < LZX_NUM_RECENT_OFFSETS) {
507*522e010bSKonstantin Komarov /* Repeat offset */
508*522e010bSKonstantin Komarov
509*522e010bSKonstantin Komarov /* Note: This isn't a real LRU queue, since using the R2
510*522e010bSKonstantin Komarov * offset doesn't bump the R1 offset down to R2. This
511*522e010bSKonstantin Komarov * quirk allows all 3 recent offsets to be handled by
512*522e010bSKonstantin Komarov * the same code. (For R0, the swap is a no-op.)
513*522e010bSKonstantin Komarov */
514*522e010bSKonstantin Komarov match_offset = recent_offsets[offset_slot];
515*522e010bSKonstantin Komarov recent_offsets[offset_slot] = recent_offsets[0];
516*522e010bSKonstantin Komarov recent_offsets[0] = match_offset;
517*522e010bSKonstantin Komarov } else {
518*522e010bSKonstantin Komarov /* Explicit offset */
519*522e010bSKonstantin Komarov
520*522e010bSKonstantin Komarov /* Look up the number of extra bits that need to be read
521*522e010bSKonstantin Komarov * to decode offsets with this offset slot.
522*522e010bSKonstantin Komarov */
523*522e010bSKonstantin Komarov num_extra_bits = lzx_extra_offset_bits[offset_slot];
524*522e010bSKonstantin Komarov
525*522e010bSKonstantin Komarov /* Start with the offset slot base value. */
526*522e010bSKonstantin Komarov match_offset = lzx_offset_slot_base[offset_slot];
527*522e010bSKonstantin Komarov
528*522e010bSKonstantin Komarov /* In aligned offset blocks, the low-order 3 bits of
529*522e010bSKonstantin Komarov * each offset are encoded using the aligned offset
530*522e010bSKonstantin Komarov * code. Otherwise, all the extra bits are literal.
531*522e010bSKonstantin Komarov */
532*522e010bSKonstantin Komarov
533*522e010bSKonstantin Komarov if ((num_extra_bits & ones_if_aligned) >= LZX_NUM_ALIGNED_OFFSET_BITS) {
534*522e010bSKonstantin Komarov match_offset +=
535*522e010bSKonstantin Komarov bitstream_read_bits(is, num_extra_bits -
536*522e010bSKonstantin Komarov LZX_NUM_ALIGNED_OFFSET_BITS)
537*522e010bSKonstantin Komarov << LZX_NUM_ALIGNED_OFFSET_BITS;
538*522e010bSKonstantin Komarov match_offset += read_alignedsym(d, is);
539*522e010bSKonstantin Komarov } else {
540*522e010bSKonstantin Komarov match_offset += bitstream_read_bits(is, num_extra_bits);
541*522e010bSKonstantin Komarov }
542*522e010bSKonstantin Komarov
543*522e010bSKonstantin Komarov /* Adjust the offset. */
544*522e010bSKonstantin Komarov match_offset -= (LZX_NUM_RECENT_OFFSETS - 1);
545*522e010bSKonstantin Komarov
546*522e010bSKonstantin Komarov /* Update the recent offsets. */
547*522e010bSKonstantin Komarov recent_offsets[2] = recent_offsets[1];
548*522e010bSKonstantin Komarov recent_offsets[1] = recent_offsets[0];
549*522e010bSKonstantin Komarov recent_offsets[0] = match_offset;
550*522e010bSKonstantin Komarov }
551*522e010bSKonstantin Komarov
552*522e010bSKonstantin Komarov /* Validate the match, then copy it to the current position. */
553*522e010bSKonstantin Komarov
554*522e010bSKonstantin Komarov if (match_len > (size_t)(block_end - out_next))
555*522e010bSKonstantin Komarov return -1;
556*522e010bSKonstantin Komarov
557*522e010bSKonstantin Komarov if (match_offset > (size_t)(out_next - out_begin))
558*522e010bSKonstantin Komarov return -1;
559*522e010bSKonstantin Komarov
560*522e010bSKonstantin Komarov out_next = lz_copy(out_next, match_len, match_offset,
561*522e010bSKonstantin Komarov block_end, LZX_MIN_MATCH_LEN);
562*522e010bSKonstantin Komarov
563*522e010bSKonstantin Komarov } while (out_next != block_end);
564*522e010bSKonstantin Komarov
565*522e010bSKonstantin Komarov return 0;
566*522e010bSKonstantin Komarov }
567*522e010bSKonstantin Komarov
568*522e010bSKonstantin Komarov /*
569*522e010bSKonstantin Komarov * lzx_allocate_decompressor - Allocate an LZX decompressor
570*522e010bSKonstantin Komarov *
571*522e010bSKonstantin Komarov * Return the pointer to the decompressor on success, or return NULL and set
572*522e010bSKonstantin Komarov * errno on failure.
573*522e010bSKonstantin Komarov */
lzx_allocate_decompressor(void)574*522e010bSKonstantin Komarov struct lzx_decompressor *lzx_allocate_decompressor(void)
575*522e010bSKonstantin Komarov {
576*522e010bSKonstantin Komarov return kmalloc(sizeof(struct lzx_decompressor), GFP_NOFS);
577*522e010bSKonstantin Komarov }
578*522e010bSKonstantin Komarov
579*522e010bSKonstantin Komarov /*
580*522e010bSKonstantin Komarov * lzx_decompress - Decompress a buffer of LZX-compressed data
581*522e010bSKonstantin Komarov *
582*522e010bSKonstantin Komarov * @decompressor: A decompressor allocated with lzx_allocate_decompressor()
583*522e010bSKonstantin Komarov * @compressed_data: The buffer of data to decompress
584*522e010bSKonstantin Komarov * @compressed_size: Number of bytes of compressed data
585*522e010bSKonstantin Komarov * @uncompressed_data: The buffer in which to store the decompressed data
586*522e010bSKonstantin Komarov * @uncompressed_size: The number of bytes the data decompresses into
587*522e010bSKonstantin Komarov *
588*522e010bSKonstantin Komarov * Return 0 on success, or return -1 and set errno on failure.
589*522e010bSKonstantin Komarov */
lzx_decompress(struct lzx_decompressor * decompressor,const void * compressed_data,size_t compressed_size,void * uncompressed_data,size_t uncompressed_size)590*522e010bSKonstantin Komarov int lzx_decompress(struct lzx_decompressor *decompressor,
591*522e010bSKonstantin Komarov const void *compressed_data, size_t compressed_size,
592*522e010bSKonstantin Komarov void *uncompressed_data, size_t uncompressed_size)
593*522e010bSKonstantin Komarov {
594*522e010bSKonstantin Komarov struct lzx_decompressor *d = decompressor;
595*522e010bSKonstantin Komarov u8 * const out_begin = uncompressed_data;
596*522e010bSKonstantin Komarov u8 *out_next = out_begin;
597*522e010bSKonstantin Komarov u8 * const out_end = out_begin + uncompressed_size;
598*522e010bSKonstantin Komarov struct input_bitstream is;
599*522e010bSKonstantin Komarov u32 recent_offsets[LZX_NUM_RECENT_OFFSETS] = {1, 1, 1};
600*522e010bSKonstantin Komarov int e8_status = 0;
601*522e010bSKonstantin Komarov
602*522e010bSKonstantin Komarov init_input_bitstream(&is, compressed_data, compressed_size);
603*522e010bSKonstantin Komarov
604*522e010bSKonstantin Komarov /* Codeword lengths begin as all 0's for delta encoding purposes. */
605*522e010bSKonstantin Komarov memset(d->maincode_lens, 0, LZX_MAINCODE_NUM_SYMBOLS);
606*522e010bSKonstantin Komarov memset(d->lencode_lens, 0, LZX_LENCODE_NUM_SYMBOLS);
607*522e010bSKonstantin Komarov
608*522e010bSKonstantin Komarov /* Decompress blocks until we have all the uncompressed data. */
609*522e010bSKonstantin Komarov
610*522e010bSKonstantin Komarov while (out_next != out_end) {
611*522e010bSKonstantin Komarov int block_type;
612*522e010bSKonstantin Komarov u32 block_size;
613*522e010bSKonstantin Komarov
614*522e010bSKonstantin Komarov if (lzx_read_block_header(d, &is, &block_type, &block_size,
615*522e010bSKonstantin Komarov recent_offsets))
616*522e010bSKonstantin Komarov goto invalid;
617*522e010bSKonstantin Komarov
618*522e010bSKonstantin Komarov if (block_size < 1 || block_size > (size_t)(out_end - out_next))
619*522e010bSKonstantin Komarov goto invalid;
620*522e010bSKonstantin Komarov
621*522e010bSKonstantin Komarov if (block_type != LZX_BLOCKTYPE_UNCOMPRESSED) {
622*522e010bSKonstantin Komarov
623*522e010bSKonstantin Komarov /* Compressed block */
624*522e010bSKonstantin Komarov
625*522e010bSKonstantin Komarov if (lzx_decompress_block(d,
626*522e010bSKonstantin Komarov &is,
627*522e010bSKonstantin Komarov block_type,
628*522e010bSKonstantin Komarov block_size,
629*522e010bSKonstantin Komarov out_begin,
630*522e010bSKonstantin Komarov out_next,
631*522e010bSKonstantin Komarov recent_offsets))
632*522e010bSKonstantin Komarov goto invalid;
633*522e010bSKonstantin Komarov
634*522e010bSKonstantin Komarov e8_status |= d->maincode_lens[0xe8];
635*522e010bSKonstantin Komarov out_next += block_size;
636*522e010bSKonstantin Komarov } else {
637*522e010bSKonstantin Komarov /* Uncompressed block */
638*522e010bSKonstantin Komarov
639*522e010bSKonstantin Komarov out_next = bitstream_read_bytes(&is, out_next,
640*522e010bSKonstantin Komarov block_size);
641*522e010bSKonstantin Komarov if (!out_next)
642*522e010bSKonstantin Komarov goto invalid;
643*522e010bSKonstantin Komarov
644*522e010bSKonstantin Komarov if (block_size & 1)
645*522e010bSKonstantin Komarov bitstream_read_byte(&is);
646*522e010bSKonstantin Komarov
647*522e010bSKonstantin Komarov e8_status = 1;
648*522e010bSKonstantin Komarov }
649*522e010bSKonstantin Komarov }
650*522e010bSKonstantin Komarov
651*522e010bSKonstantin Komarov /* Postprocess the data unless it cannot possibly contain 0xe8 bytes. */
652*522e010bSKonstantin Komarov if (e8_status)
653*522e010bSKonstantin Komarov lzx_postprocess(uncompressed_data, uncompressed_size);
654*522e010bSKonstantin Komarov
655*522e010bSKonstantin Komarov return 0;
656*522e010bSKonstantin Komarov
657*522e010bSKonstantin Komarov invalid:
658*522e010bSKonstantin Komarov return -1;
659*522e010bSKonstantin Komarov }
660*522e010bSKonstantin Komarov
661*522e010bSKonstantin Komarov /*
662*522e010bSKonstantin Komarov * lzx_free_decompressor - Free an LZX decompressor
663*522e010bSKonstantin Komarov *
664*522e010bSKonstantin Komarov * @decompressor: A decompressor that was allocated with
665*522e010bSKonstantin Komarov * lzx_allocate_decompressor(), or NULL.
666*522e010bSKonstantin Komarov */
lzx_free_decompressor(struct lzx_decompressor * decompressor)667*522e010bSKonstantin Komarov void lzx_free_decompressor(struct lzx_decompressor *decompressor)
668*522e010bSKonstantin Komarov {
669*522e010bSKonstantin Komarov kfree(decompressor);
670*522e010bSKonstantin Komarov }
671