xref: /linux/fs/ntfs/lib/lzx_decompress.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lzx_decompress.c - A decompressor for the LZX compression format
4  *
5  * This is a port of the upstream wimlib "lzx_decompress.c" which uses a
6  * subtable-based Huffman decode table format.  The window size is fixed at
7  * 32768 bytes, which is the only size used in System-compressed (WOF) files.
8  *
9  * Copyright (C) 2012-2016 Eric Biggers
10  */
11 
12 #include <linux/array_size.h>
13 #include <linux/bits.h>
14 
15 #include "decompress_common.h"
16 #include "lib.h"
17 #include "../ntfs_codec.h"
18 
19 /* Number of literal byte values. */
20 #define LZX_NUM_CHARS		256
21 
22 /* The smallest and largest allowed match lengths. */
23 #define LZX_MIN_MATCH_LEN	2
24 #define LZX_MAX_MATCH_LEN	257
25 
26 /* Number of distinct match lengths that can be represented. */
27 #define LZX_NUM_LENS		(LZX_MAX_MATCH_LEN - LZX_MIN_MATCH_LEN + 1)
28 
29 /* Number of match lengths for which no length symbol is required. */
30 #define LZX_NUM_PRIMARY_LENS	7
31 #define LZX_NUM_LEN_HEADERS	(LZX_NUM_PRIMARY_LENS + 1)
32 
33 /* Valid values of the 3-bit block type field. */
34 #define LZX_BLOCKTYPE_VERBATIM		1
35 #define LZX_BLOCKTYPE_ALIGNED		2
36 #define LZX_BLOCKTYPE_UNCOMPRESSED	3
37 
38 /* LZX window size is fixed at 32768 bytes for System-compressed files. */
39 
40 /* Number of offset slots for a 32768-byte window. */
41 #define LZX_NUM_OFFSET_SLOTS	30
42 
43 /* Number of symbols in the main code. */
44 #define LZX_MAINCODE_NUM_SYMBOLS	\
45 	(LZX_NUM_CHARS + (LZX_NUM_OFFSET_SLOTS * LZX_NUM_LEN_HEADERS))
46 
47 /* Number of symbols in the length code. */
48 #define LZX_LENCODE_NUM_SYMBOLS		(LZX_NUM_LENS - LZX_NUM_PRIMARY_LENS)
49 
50 /* Number of symbols in the precode. */
51 #define LZX_PRECODE_NUM_SYMBOLS		20
52 
53 /* Number of bits in which each precode codeword length is represented. */
54 #define LZX_PRECODE_ELEMENT_SIZE	4
55 
56 /* Number of low-order bits of each match offset that are entropy-encoded in
57  * aligned offset blocks.
58  */
59 #define LZX_NUM_ALIGNED_OFFSET_BITS	3
60 
61 /* Number of symbols in the aligned offset code. */
62 #define LZX_ALIGNEDCODE_NUM_SYMBOLS	BIT(LZX_NUM_ALIGNED_OFFSET_BITS)
63 
64 /* Mask for the match offset bits that are entropy-encoded in aligned offset
65  * blocks.
66  */
67 #define LZX_ALIGNED_OFFSET_BITMASK	(BIT(LZX_NUM_ALIGNED_OFFSET_BITS) - 1)
68 
69 /* Number of bits in which each aligned offset codeword length is represented. */
70 #define LZX_ALIGNEDCODE_ELEMENT_SIZE	3
71 
72 /* The first offset slot which requires an aligned offset symbol in aligned
73  * offset blocks.
74  */
75 #define LZX_MIN_ALIGNED_OFFSET_SLOT	8
76 
77 /* Maximum lengths (in bits) of the codewords in each Huffman code. */
78 #define LZX_MAX_MAIN_CODEWORD_LEN	16
79 #define LZX_MAX_LEN_CODEWORD_LEN	16
80 #define LZX_MAX_PRE_CODEWORD_LEN	((1 << LZX_PRECODE_ELEMENT_SIZE) - 1)
81 #define LZX_MAX_ALIGNED_CODEWORD_LEN	((1 << LZX_ALIGNEDCODE_ELEMENT_SIZE) - 1)
82 
83 /* For LZX-compressed blocks in WIM/system-compressed files this value is
84  * always used as the filesize parameter for the E8 call preprocessing.
85  */
86 #define LZX_WIM_MAGIC_FILESIZE	12000000
87 
88 /* Assumed LZX block size when the encoded block size begins with a 0 bit. */
89 #define LZX_DEFAULT_BLOCK_SIZE	32768
90 
91 /* Number of offsets in the recent (or "repeat") offsets queue. */
92 #define LZX_NUM_RECENT_OFFSETS	3
93 
94 /* An offset of n bytes is actually encoded as (n + LZX_OFFSET_ADJUSTMENT). */
95 #define LZX_OFFSET_ADJUSTMENT	(LZX_NUM_RECENT_OFFSETS - 1)
96 
97 /* These values are chosen for fast decompression. */
98 #define LZX_MAINCODE_TABLEBITS		11
99 #define LZX_LENCODE_TABLEBITS		9
100 #define LZX_PRECODE_TABLEBITS		6
101 #define LZX_ALIGNEDCODE_TABLEBITS	7
102 
103 #define LZX_READ_LENS_MAX_OVERRUN	50
104 
105 /* Mapping: offset slot => first match offset that uses that offset slot.
106  * The offset slots for repeat offsets map to "fake" offsets < 1.
107  */
108 static const s32 lzx_offset_slot_base[LZX_NUM_OFFSET_SLOTS + 1] = {
109 	-2,      -1,      0,       1,       2,       /* 0  --- 4  */
110 	4,       6,       10,      14,      22,      /* 5  --- 9  */
111 	30,      46,      62,      94,      126,     /* 10 --- 14 */
112 	190,     254,     382,     510,     766,     /* 15 --- 19 */
113 	1022,    1534,    2046,    3070,    4094,    /* 20 --- 24 */
114 	6142,    8190,    12286,   16382,   24574,   /* 25 --- 29 */
115 	32766,					 /* extra     */
116 };
117 
118 /* Mapping: offset slot => how many extra bits must be read and added to the
119  * corresponding offset slot base to decode the match offset.
120  */
121 static const u8 lzx_extra_offset_bits[LZX_NUM_OFFSET_SLOTS] = {
122 	0,  0,  0,  0,  1,
123 	1,  2,  2,  3,  3,
124 	4,  4,  5,  5,  6,
125 	6,  7,  7,  8,  8,
126 	9,  9,  10, 10, 11,
127 	11, 12, 12, 13, 13,
128 };
129 
130 /* Like lzx_extra_offset_bits[], but with the entropy-coded aligned offset
131  * bits already subtracted.  Valid only for offset slots that may appear in
132  * aligned offset blocks.
133  */
134 static const u8 lzx_extra_offset_bits_minus_aligned[LZX_NUM_OFFSET_SLOTS] = {
135 	0,  0,  0,  0,  1,
136 	1,  2,  2,  0,  0,
137 	1,  1,  2,  2,  3,
138 	3,  4,  4,  5,  5,
139 	6,  6,  7,  7,  8,
140 	8,  9,  9,  10, 10,
141 };
142 
143 /* Reusable heap-allocated memory for LZX decompression.  The decode tables and
144  * their corresponding codeword length arrays are grouped in unions so the
145  * memory can be reused across phases, and the per-code working spaces share a
146  * single union since only one is needed at a time.
147  */
148 struct lzx_decompressor {
149 	DECODE_TABLE(maincode_decode_table, LZX_MAINCODE_NUM_SYMBOLS,
150 		     LZX_MAINCODE_TABLEBITS, LZX_MAX_MAIN_CODEWORD_LEN);
151 	u8 maincode_lens[LZX_MAINCODE_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
152 
153 	DECODE_TABLE(lencode_decode_table, LZX_LENCODE_NUM_SYMBOLS,
154 		     LZX_LENCODE_TABLEBITS, LZX_MAX_LEN_CODEWORD_LEN);
155 	u8 lencode_lens[LZX_LENCODE_NUM_SYMBOLS + LZX_READ_LENS_MAX_OVERRUN];
156 
157 	union {
158 		DECODE_TABLE(alignedcode_decode_table,
159 			     LZX_ALIGNEDCODE_NUM_SYMBOLS,
160 			     LZX_ALIGNEDCODE_TABLEBITS,
161 			     LZX_MAX_ALIGNED_CODEWORD_LEN);
162 		u8 alignedcode_lens[LZX_ALIGNEDCODE_NUM_SYMBOLS];
163 	};
164 
165 	union {
166 		DECODE_TABLE(precode_decode_table, LZX_PRECODE_NUM_SYMBOLS,
167 			     LZX_PRECODE_TABLEBITS, LZX_MAX_PRE_CODEWORD_LEN);
168 		u8 precode_lens[LZX_PRECODE_NUM_SYMBOLS];
169 		/* extra_offset_bits[] is used as scratch in aligned blocks. */
170 		u8 extra_offset_bits[LZX_NUM_OFFSET_SLOTS];
171 	};
172 
173 	union {
174 		DECODE_TABLE_WORKING_SPACE(maincode_working_space,
175 					   LZX_MAINCODE_NUM_SYMBOLS,
176 					   LZX_MAX_MAIN_CODEWORD_LEN);
177 		DECODE_TABLE_WORKING_SPACE(lencode_working_space,
178 					   LZX_LENCODE_NUM_SYMBOLS,
179 					   LZX_MAX_LEN_CODEWORD_LEN);
180 		DECODE_TABLE_WORKING_SPACE(alignedcode_working_space,
181 					   LZX_ALIGNEDCODE_NUM_SYMBOLS,
182 					   LZX_MAX_ALIGNED_CODEWORD_LEN);
183 		DECODE_TABLE_WORKING_SPACE(precode_working_space,
184 					   LZX_PRECODE_NUM_SYMBOLS,
185 					   LZX_MAX_PRE_CODEWORD_LEN);
186 	};
187 } __aligned(DECODE_TABLE_ALIGNMENT);
188 
189 static forceinline unsigned int read_presym(const struct lzx_decompressor *d,
190 					    struct input_bitstream *is)
191 {
192 	return read_huffsym(is, d->precode_decode_table, LZX_PRECODE_TABLEBITS,
193 			    LZX_MAX_PRE_CODEWORD_LEN);
194 }
195 
196 static forceinline unsigned int read_mainsym(const struct lzx_decompressor *d,
197 					     struct input_bitstream *is)
198 {
199 	return read_huffsym(is, d->maincode_decode_table,
200 			    LZX_MAINCODE_TABLEBITS, LZX_MAX_MAIN_CODEWORD_LEN);
201 }
202 
203 static forceinline unsigned int read_lensym(const struct lzx_decompressor *d,
204 					    struct input_bitstream *is)
205 {
206 	return read_huffsym(is, d->lencode_decode_table, LZX_LENCODE_TABLEBITS,
207 			    LZX_MAX_LEN_CODEWORD_LEN);
208 }
209 
210 static forceinline unsigned int
211 read_alignedsym(const struct lzx_decompressor *d, struct input_bitstream *is)
212 {
213 	return read_huffsym(is, d->alignedcode_decode_table,
214 			    LZX_ALIGNEDCODE_TABLEBITS,
215 			    LZX_MAX_ALIGNED_CODEWORD_LEN);
216 }
217 
218 /*
219  * Read a precode from the compressed bitstream, then use it to decode
220  * @num_lens codeword length values and write them to @lens.
221  */
222 static int lzx_read_codeword_lens(struct lzx_decompressor *d,
223 				  struct input_bitstream *is, u8 *lens,
224 				  u32 num_lens)
225 {
226 	u8 *len_ptr = lens;
227 	u8 *lens_end = lens + num_lens;
228 	u32 i;
229 
230 	/* Read the lengths of the precode codewords.  These are stored
231 	 * explicitly.
232 	 */
233 	for (i = 0; i < LZX_PRECODE_NUM_SYMBOLS; i++) {
234 		d->precode_lens[i] =
235 			bitstream_read_bits(is, LZX_PRECODE_ELEMENT_SIZE);
236 	}
237 
238 	/* Build the decoding table for the precode. */
239 	if (make_huffman_decode_table(d->precode_decode_table,
240 				      LZX_PRECODE_NUM_SYMBOLS,
241 				      LZX_PRECODE_TABLEBITS,
242 				      d->precode_lens,
243 				      LZX_MAX_PRE_CODEWORD_LEN,
244 				      d->precode_working_space,
245 				      ARRAY_SIZE(d->precode_decode_table)))
246 		return -1;
247 
248 	/* Decode the codeword lengths. */
249 	do {
250 		u32 presym;
251 		u8 len;
252 
253 		presym = read_presym(d, is);
254 		if (presym < 17) {
255 			/* Difference from old length. */
256 			len = *len_ptr - presym;
257 			if ((s8)len < 0)
258 				len += 17;
259 			*len_ptr++ = len;
260 		} else {
261 			/* Special RLE values. */
262 			u32 run_len;
263 
264 			if (presym == 17) {
265 				run_len = 4 + bitstream_read_bits(is, 4);
266 				len = 0;
267 			} else if (presym == 18) {
268 				run_len = 20 + bitstream_read_bits(is, 5);
269 				len = 0;
270 			} else {
271 				run_len = 4 + bitstream_read_bits(is, 1);
272 				presym = read_presym(d, is);
273 				if (unlikely(presym > 17))
274 					return -1;
275 				len = *len_ptr - presym;
276 				if ((s8)len < 0)
277 					len += 17;
278 			}
279 
280 			do {
281 				*len_ptr++ = len;
282 			} while (--run_len);
283 			/* The worst case overrun is when presym == 18,
284 			 * run_len == 20 + 31, and only 1 length was
285 			 * remaining, so LZX_READ_LENS_MAX_OVERRUN == 50.
286 			 * Overrun while reading the first half of
287 			 * maincode_lens can corrupt the previous values in
288 			 * the second half, but the resulting lengths will
289 			 * still be in range, and data that generates overruns
290 			 * is invalid anyway.
291 			 */
292 		}
293 	} while (len_ptr < lens_end);
294 
295 	return 0;
296 }
297 
298 static void undo_translate_target(void *target, s32 input_pos)
299 {
300 	s32 abs_offset, rel_offset;
301 
302 	abs_offset = get_unaligned_le32(target);
303 	if (abs_offset >= 0) {
304 		if (abs_offset < LZX_WIM_MAGIC_FILESIZE) {
305 			/* "good translation" */
306 			rel_offset = abs_offset - input_pos;
307 			put_unaligned_le32(rel_offset, target);
308 		}
309 	} else {
310 		if (abs_offset >= -input_pos) {
311 			/* "compensating translation" */
312 			rel_offset = abs_offset + LZX_WIM_MAGIC_FILESIZE;
313 			put_unaligned_le32(rel_offset, target);
314 		}
315 	}
316 }
317 
318 /*
319  * Undo the 'E8' preprocessing used in LZX.  Before compression, the
320  * uncompressed data was preprocessed by changing the targets of suspected x86
321  * CALL instructions from relative offsets to absolute offsets.  After
322  * match/literal decoding, the decompressor must undo the translation.
323  *
324  * E8 preprocessing is disabled in the last 6 bytes of the data, which means
325  * the 5-byte call instruction cannot start in the last 10 bytes.  The scalar
326  * implementation below exploits this by replacing the last 6 bytes with 0xE8
327  * trap bytes, eliminating end-of-buffer checks from the inner loop.
328  */
329 static void lzx_postprocess(u8 *data, u32 size)
330 {
331 	u8 *tail;
332 	u8 saved_bytes[6];
333 	u8 *p;
334 
335 	if (size <= 10)
336 		return;
337 
338 	tail = &data[size - 6];
339 	memcpy(saved_bytes, tail, 6);
340 	memset(tail, 0xE8, 6);
341 	p = data;
342 	for (;;) {
343 		while (*p != 0xE8)
344 			p++;
345 		if (p >= tail)
346 			break;
347 		undo_translate_target(p + 1, (s32)(p - data));
348 		p += 5;
349 	}
350 	memcpy(tail, saved_bytes, 6);
351 }
352 
353 static int lzx_read_block_header(struct lzx_decompressor *d,
354 				 struct input_bitstream *is,
355 				 u32 recent_offsets[], int *block_type_ret,
356 				 u32 *block_size_ret)
357 {
358 	int block_type;
359 	u32 block_size;
360 	u32 i;
361 
362 	bitstream_ensure_bits(is, 4);
363 
364 	/* Read the block type. */
365 	block_type = bitstream_pop_bits(is, 3);
366 
367 	/* Read the block size.  With the 32768-byte window used in system
368 	 * compression, block sizes are always encoded in 16 bits.
369 	 */
370 	if (bitstream_pop_bits(is, 1))
371 		block_size = LZX_DEFAULT_BLOCK_SIZE;
372 	else
373 		block_size = bitstream_read_bits(is, 16);
374 
375 	switch (block_type) {
376 	case LZX_BLOCKTYPE_ALIGNED:
377 		/* Read the aligned offset codeword lengths. */
378 		for (i = 0; i < LZX_ALIGNEDCODE_NUM_SYMBOLS; i++) {
379 			d->alignedcode_lens[i] =
380 				bitstream_read_bits(is,
381 						    LZX_ALIGNEDCODE_ELEMENT_SIZE);
382 		}
383 		/* Fall though, since the rest of the header for aligned offset
384 		 * blocks is the same as that for verbatim blocks.
385 		 */
386 		fallthrough;
387 
388 	case LZX_BLOCKTYPE_VERBATIM:
389 		/* Read the main codeword lengths, which are divided into two
390 		 * parts: literal symbols and match headers.
391 		 */
392 		if (lzx_read_codeword_lens(d, is, d->maincode_lens,
393 					   LZX_NUM_CHARS))
394 			return -1;
395 		if (lzx_read_codeword_lens(d, is,
396 					   d->maincode_lens + LZX_NUM_CHARS,
397 					   LZX_MAINCODE_NUM_SYMBOLS - LZX_NUM_CHARS))
398 			return -1;
399 
400 		/* Read the length codeword lengths. */
401 		if (lzx_read_codeword_lens(d, is, d->lencode_lens,
402 					   LZX_LENCODE_NUM_SYMBOLS))
403 			return -1;
404 		break;
405 
406 	case LZX_BLOCKTYPE_UNCOMPRESSED:
407 		/* The header of an uncompressed block contains new values for
408 		 * the recent offsets queue, starting on the next 16-bit
409 		 * boundary in the bitstream.  If the stream is *already*
410 		 * aligned, the next 16 bits must be discarded.
411 		 */
412 		bitstream_ensure_bits(is, 1);
413 		bitstream_align(is);
414 		recent_offsets[0] = bitstream_read_u32(is);
415 		recent_offsets[1] = bitstream_read_u32(is);
416 		recent_offsets[2] = bitstream_read_u32(is);
417 
418 		/* Offsets of 0 are invalid. */
419 		if (recent_offsets[0] == 0 || recent_offsets[1] == 0 ||
420 		    recent_offsets[2] == 0)
421 			return -1;
422 		break;
423 
424 	default:
425 		/* Unrecognized block type. */
426 		return -1;
427 	}
428 
429 	*block_type_ret = block_type;
430 	*block_size_ret = block_size;
431 	return 0;
432 }
433 
434 static int lzx_decompress_block(struct lzx_decompressor *d,
435 				struct input_bitstream *is, int block_type,
436 				u32 block_size, u8 *const out_begin,
437 				u8 *out_next, u32 recent_offsets[])
438 {
439 	u8 *const block_end = out_next + block_size;
440 	unsigned int min_aligned_offset_slot;
441 	const u8 *extra_offset_bits;
442 
443 	/* Build the Huffman decode tables.  The main and length tables are
444 	 * always needed; for aligned blocks the aligned offset table is also
445 	 * needed.
446 	 */
447 	if (make_huffman_decode_table(d->maincode_decode_table,
448 				      LZX_MAINCODE_NUM_SYMBOLS,
449 				      LZX_MAINCODE_TABLEBITS, d->maincode_lens,
450 				      LZX_MAX_MAIN_CODEWORD_LEN,
451 				      d->maincode_working_space,
452 				      ARRAY_SIZE(d->maincode_decode_table)))
453 		return -1;
454 
455 	if (make_huffman_decode_table(d->lencode_decode_table,
456 				      LZX_LENCODE_NUM_SYMBOLS,
457 				      LZX_LENCODE_TABLEBITS, d->lencode_lens,
458 				      LZX_MAX_LEN_CODEWORD_LEN,
459 				      d->lencode_working_space,
460 				      ARRAY_SIZE(d->lencode_decode_table)))
461 		return -1;
462 
463 	if (block_type == LZX_BLOCKTYPE_ALIGNED) {
464 		if (make_huffman_decode_table(d->alignedcode_decode_table,
465 					      LZX_ALIGNEDCODE_NUM_SYMBOLS,
466 					      LZX_ALIGNEDCODE_TABLEBITS,
467 					      d->alignedcode_lens,
468 					      LZX_MAX_ALIGNED_CODEWORD_LEN,
469 					      d->alignedcode_working_space,
470 					      ARRAY_SIZE(d->alignedcode_decode_table)))
471 			return -1;
472 		min_aligned_offset_slot = LZX_MIN_ALIGNED_OFFSET_SLOT;
473 		extra_offset_bits = lzx_extra_offset_bits_minus_aligned;
474 	} else {
475 		min_aligned_offset_slot = LZX_NUM_OFFSET_SLOTS;
476 		extra_offset_bits = lzx_extra_offset_bits;
477 	}
478 
479 	/* Decode the literals and matches. */
480 	do {
481 		unsigned int mainsym;
482 		unsigned int length;
483 		u32 offset;
484 		unsigned int offset_slot;
485 
486 		mainsym = read_mainsym(d, is);
487 		if (mainsym < LZX_NUM_CHARS) {
488 			/* Literal */
489 			*out_next++ = mainsym;
490 			continue;
491 		}
492 
493 		/* Match */
494 
495 		/* Decode the length header and offset slot.
496 		 */
497 		STATIC_ASSERT(LZX_NUM_CHARS % LZX_NUM_LEN_HEADERS == 0);
498 		length = mainsym % LZX_NUM_LEN_HEADERS;
499 		offset_slot = (mainsym - LZX_NUM_CHARS) / LZX_NUM_LEN_HEADERS;
500 
501 		/* If needed, read a length symbol to decode the full length. */
502 		if (length == LZX_NUM_PRIMARY_LENS)
503 			length += read_lensym(d, is);
504 		length += LZX_MIN_MATCH_LEN;
505 
506 		if (offset_slot < LZX_NUM_RECENT_OFFSETS) {
507 			/* Repeat offset.  This isn't a real LRU queue, since
508 			 * using the R2 offset doesn't bump the R1 offset down
509 			 * to R2.
510 			 */
511 			offset = recent_offsets[offset_slot];
512 			recent_offsets[offset_slot] = recent_offsets[0];
513 		} else {
514 			/* Explicit offset. */
515 			offset = bitstream_read_bits(is,
516 						     extra_offset_bits[offset_slot]);
517 			if (offset_slot >= min_aligned_offset_slot) {
518 				offset = (offset << LZX_NUM_ALIGNED_OFFSET_BITS) |
519 					 read_alignedsym(d, is);
520 			}
521 			offset += lzx_offset_slot_base[offset_slot];
522 
523 			/* Update the match offset LRU queue. */
524 			STATIC_ASSERT(LZX_NUM_RECENT_OFFSETS == 3);
525 			recent_offsets[2] = recent_offsets[1];
526 			recent_offsets[1] = recent_offsets[0];
527 		}
528 		recent_offsets[0] = offset;
529 
530 		/* Validate the match and copy it to the current position. */
531 		if (unlikely(lz_copy(length, offset, out_begin, out_next,
532 				     block_end, LZX_MIN_MATCH_LEN)))
533 			return -1;
534 		out_next += length;
535 	} while (out_next != block_end);
536 
537 	return 0;
538 }
539 
540 int lzx_decompress(struct lzx_decompressor *d, const void *compressed_data,
541 		   size_t compressed_size, void *uncompressed_data,
542 		   size_t uncompressed_size)
543 {
544 	u8 *const out_begin = uncompressed_data;
545 	u8 *out_next = out_begin;
546 	u8 *const out_end = out_begin + uncompressed_size;
547 	struct input_bitstream is;
548 
549 	STATIC_ASSERT(LZX_NUM_RECENT_OFFSETS == 3);
550 	u32 recent_offsets[LZX_NUM_RECENT_OFFSETS] = {1, 1, 1};
551 	bool may_have_e8_byte = false;
552 
553 	init_input_bitstream(&is, compressed_data, compressed_size);
554 
555 	/* Codeword lengths begin as all 0's for delta encoding purposes. */
556 	memset(d->maincode_lens, 0, LZX_MAINCODE_NUM_SYMBOLS);
557 	memset(d->lencode_lens, 0, LZX_LENCODE_NUM_SYMBOLS);
558 
559 	/* Decompress blocks until we have all the uncompressed data.
560 	 */
561 	while (out_next != out_end) {
562 		int block_type;
563 		u32 block_size;
564 
565 		if (lzx_read_block_header(d, &is, recent_offsets, &block_type,
566 					  &block_size))
567 			return -1;
568 
569 		if (block_size < 1 || block_size > (u32)(out_end - out_next))
570 			return -1;
571 
572 		if (likely(block_type != LZX_BLOCKTYPE_UNCOMPRESSED)) {
573 			/* Compressed block. */
574 			if (lzx_decompress_block(d, &is, block_type, block_size,
575 						 out_begin, out_next,
576 						 recent_offsets))
577 				return -1;
578 
579 			/* If the first E8 byte was in this block, then it
580 			 * must have been encoded as a literal (mainsym E8).
581 			 */
582 			if (d->maincode_lens[0xE8])
583 				may_have_e8_byte = true;
584 		} else {
585 			/* Uncompressed block. */
586 			if (bitstream_read_bytes(&is, out_next, block_size))
587 				return -1;
588 			if (block_size & 1)
589 				bitstream_read_byte(&is);
590 			/* There may have been an E8 byte in the block. */
591 			may_have_e8_byte = true;
592 		}
593 		out_next += block_size;
594 	}
595 
596 	/* Postprocess the data unless it cannot possibly contain E8 bytes. */
597 	if (may_have_e8_byte)
598 		lzx_postprocess(uncompressed_data, uncompressed_size);
599 
600 	return 0;
601 }
602 
603 struct lzx_decompressor *lzx_allocate_decompressor(void)
604 {
605 	return kmalloc_obj(struct lzx_decompressor, GFP_NOFS);
606 }
607 
608 void lzx_free_decompressor(struct lzx_decompressor *d)
609 {
610 	kfree(d);
611 }
612 
613 static size_t lzx_scratch_size(u32 chunk_size)
614 {
615 	return sizeof(struct lzx_decompressor);
616 }
617 
618 static int lzx_decompress_chunk(void *scratch, const void *src, size_t src_len,
619 				void *dst, size_t dst_len, u32 chunk_size)
620 {
621 	struct lzx_decompressor *d = scratch;
622 
623 	return lzx_decompress(d, src, src_len, dst, dst_len);
624 }
625 
626 const struct ntfs_codec_ops ntfs_lzx32k_codec_ops = {
627 	.id = NTFS_CODEC_LZX32K,
628 	.name = "lzx32k",
629 	.scratch_size = lzx_scratch_size,
630 	.decompress_chunk = lzx_decompress_chunk,
631 };
632