lz_encoder.c revision 207753
1221163Sadrian///////////////////////////////////////////////////////////////////////////////
2221163Sadrian//
3221163Sadrian/// \file       lz_encoder.c
4221163Sadrian/// \brief      LZ in window
5221163Sadrian///
6221163Sadrian//  Authors:    Igor Pavlov
7221163Sadrian//              Lasse Collin
8221163Sadrian//
9221163Sadrian//  This file has been put into the public domain.
10221163Sadrian//  You can do whatever you want with this file.
11221163Sadrian//
12221163Sadrian///////////////////////////////////////////////////////////////////////////////
13221163Sadrian
14221163Sadrian#include "lz_encoder.h"
15221163Sadrian#include "lz_encoder_hash.h"
16221163Sadrian
17221163Sadrian// See lz_encoder_hash.h. This is a bit hackish but avoids making
18221163Sadrian// endianness a conditional in makefiles.
19221163Sadrian#if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL)
20221163Sadrian#	include "lz_encoder_hash_table.h"
21221163Sadrian#endif
22221163Sadrian
23221163Sadrian
24221163Sadrianstruct lzma_coder_s {
25221163Sadrian	/// LZ-based encoder e.g. LZMA
26221163Sadrian	lzma_lz_encoder lz;
27221163Sadrian
28221163Sadrian	/// History buffer and match finder
29221163Sadrian	lzma_mf mf;
30221163Sadrian
31221163Sadrian	/// Next coder in the chain
32221163Sadrian	lzma_next_coder next;
33221163Sadrian};
34221163Sadrian
35221163Sadrian
36221163Sadrian/// \brief      Moves the data in the input window to free space for new data
37221163Sadrian///
38221163Sadrian/// mf->buffer is a sliding input window, which keeps mf->keep_size_before
39221163Sadrian/// bytes of input history available all the time. Now and then we need to
40221163Sadrian/// "slide" the buffer to make space for the new data to the end of the
41221163Sadrian/// buffer. At the same time, data older than keep_size_before is dropped.
42221163Sadrian///
43221163Sadrianstatic void
44221163Sadrianmove_window(lzma_mf *mf)
45221163Sadrian{
46221163Sadrian	// Align the move to a multiple of 16 bytes. Some LZ-based encoders
47221163Sadrian	// like LZMA use the lowest bits of mf->read_pos to know the
48221163Sadrian	// alignment of the uncompressed data. We also get better speed
49221163Sadrian	// for memmove() with aligned buffers.
50221163Sadrian	assert(mf->read_pos > mf->keep_size_before);
51221163Sadrian	const uint32_t move_offset
52221163Sadrian		= (mf->read_pos - mf->keep_size_before) & ~UINT32_C(15);
53221163Sadrian
54221163Sadrian	assert(mf->write_pos > move_offset);
55221163Sadrian	const size_t move_size = mf->write_pos - move_offset;
56221163Sadrian
57221163Sadrian	assert(move_offset + move_size <= mf->size);
58221163Sadrian
59221163Sadrian	memmove(mf->buffer, mf->buffer + move_offset, move_size);
60221163Sadrian
61221163Sadrian	mf->offset += move_offset;
62221163Sadrian	mf->read_pos -= move_offset;
63221163Sadrian	mf->read_limit -= move_offset;
64221163Sadrian	mf->write_pos -= move_offset;
65221163Sadrian
66221163Sadrian	return;
67221163Sadrian}
68221163Sadrian
69221163Sadrian
70221163Sadrian/// \brief      Tries to fill the input window (mf->buffer)
71221163Sadrian///
72221163Sadrian/// If we are the last encoder in the chain, our input data is in in[].
73221163Sadrian/// Otherwise we call the next filter in the chain to process in[] and
74221163Sadrian/// write its output to mf->buffer.
75221163Sadrian///
76221163Sadrian/// This function must not be called once it has returned LZMA_STREAM_END.
77221163Sadrian///
78221163Sadrianstatic lzma_ret
79221163Sadrianfill_window(lzma_coder *coder, lzma_allocator *allocator, const uint8_t *in,
80221163Sadrian		size_t *in_pos, size_t in_size, lzma_action action)
81221163Sadrian{
82221163Sadrian	assert(coder->mf.read_pos <= coder->mf.write_pos);
83221163Sadrian
84221163Sadrian	// Move the sliding window if needed.
85221163Sadrian	if (coder->mf.read_pos >= coder->mf.size - coder->mf.keep_size_after)
86221163Sadrian		move_window(&coder->mf);
87221163Sadrian
88221163Sadrian	// Maybe this is ugly, but lzma_mf uses uint32_t for most things
89221163Sadrian	// (which I find cleanest), but we need size_t here when filling
90221163Sadrian	// the history window.
91221163Sadrian	size_t write_pos = coder->mf.write_pos;
92221163Sadrian	lzma_ret ret;
93221163Sadrian	if (coder->next.code == NULL) {
94221163Sadrian		// Not using a filter, simply memcpy() as much as possible.
95221163Sadrian		lzma_bufcpy(in, in_pos, in_size, coder->mf.buffer,
96221163Sadrian				&write_pos, coder->mf.size);
97221163Sadrian
98221163Sadrian		ret = action != LZMA_RUN && *in_pos == in_size
99221163Sadrian				? LZMA_STREAM_END : LZMA_OK;
100221163Sadrian
101221163Sadrian	} else {
102221163Sadrian		ret = coder->next.code(coder->next.coder, allocator,
103221163Sadrian				in, in_pos, in_size,
104221163Sadrian				coder->mf.buffer, &write_pos,
105221163Sadrian				coder->mf.size, action);
106221163Sadrian	}
107221163Sadrian
108221163Sadrian	coder->mf.write_pos = write_pos;
109221163Sadrian
110221163Sadrian	// If end of stream has been reached or flushing completed, we allow
111221163Sadrian	// the encoder to process all the input (that is, read_pos is allowed
112221163Sadrian	// to reach write_pos). Otherwise we keep keep_size_after bytes
113221163Sadrian	// available as prebuffer.
114221163Sadrian	if (ret == LZMA_STREAM_END) {
115221163Sadrian		assert(*in_pos == in_size);
116221163Sadrian		ret = LZMA_OK;
117221163Sadrian		coder->mf.action = action;
118221163Sadrian		coder->mf.read_limit = coder->mf.write_pos;
119221163Sadrian
120221163Sadrian	} else if (coder->mf.write_pos > coder->mf.keep_size_after) {
121221163Sadrian		// This needs to be done conditionally, because if we got
122221163Sadrian		// only little new input, there may be too little input
123221163Sadrian		// to do any encoding yet.
124221163Sadrian		coder->mf.read_limit = coder->mf.write_pos
125221163Sadrian				- coder->mf.keep_size_after;
126221163Sadrian	}
127221163Sadrian
128221163Sadrian	// Restart the match finder after finished LZMA_SYNC_FLUSH.
129221163Sadrian	if (coder->mf.pending > 0
130221163Sadrian			&& coder->mf.read_pos < coder->mf.read_limit) {
131221163Sadrian		// Match finder may update coder->pending and expects it to
132221163Sadrian		// start from zero, so use a temporary variable.
133221163Sadrian		const size_t pending = coder->mf.pending;
134221163Sadrian		coder->mf.pending = 0;
135221163Sadrian
136221163Sadrian		// Rewind read_pos so that the match finder can hash
137221163Sadrian		// the pending bytes.
138221163Sadrian		assert(coder->mf.read_pos >= pending);
139221163Sadrian		coder->mf.read_pos -= pending;
140221163Sadrian
141221163Sadrian		// Call the skip function directly instead of using
142221163Sadrian		// mf_skip(), since we don't want to touch mf->read_ahead.
143221163Sadrian		coder->mf.skip(&coder->mf, pending);
144221163Sadrian	}
145221163Sadrian
146221163Sadrian	return ret;
147221163Sadrian}
148221163Sadrian
149221163Sadrian
150221163Sadrianstatic lzma_ret
151221163Sadrianlz_encode(lzma_coder *coder, lzma_allocator *allocator,
152221163Sadrian		const uint8_t *restrict in, size_t *restrict in_pos,
153221163Sadrian		size_t in_size,
154221163Sadrian		uint8_t *restrict out, size_t *restrict out_pos,
155221163Sadrian		size_t out_size, lzma_action action)
156221163Sadrian{
157221163Sadrian	while (*out_pos < out_size
158221163Sadrian			&& (*in_pos < in_size || action != LZMA_RUN)) {
159221163Sadrian		// Read more data to coder->mf.buffer if needed.
160221163Sadrian		if (coder->mf.action == LZMA_RUN && coder->mf.read_pos
161221163Sadrian				>= coder->mf.read_limit)
162221163Sadrian			return_if_error(fill_window(coder, allocator,
163221163Sadrian					in, in_pos, in_size, action));
164221163Sadrian
165221163Sadrian		// Encode
166221163Sadrian		const lzma_ret ret = coder->lz.code(coder->lz.coder,
167221163Sadrian				&coder->mf, out, out_pos, out_size);
168221163Sadrian		if (ret != LZMA_OK) {
169221163Sadrian			// Setting this to LZMA_RUN for cases when we are
170221163Sadrian			// flushing. It doesn't matter when finishing or if
171221163Sadrian			// an error occurred.
172221163Sadrian			coder->mf.action = LZMA_RUN;
173221163Sadrian			return ret;
174221163Sadrian		}
175221163Sadrian	}
176221163Sadrian
177221163Sadrian	return LZMA_OK;
178221163Sadrian}
179221163Sadrian
180221163Sadrian
181221163Sadrianstatic bool
182221163Sadrianlz_encoder_prepare(lzma_mf *mf, lzma_allocator *allocator,
183221163Sadrian		const lzma_lz_options *lz_options)
184221163Sadrian{
185221163Sadrian	// For now, the dictionary size is limited to 1.5 GiB. This may grow
186221163Sadrian	// in the future if needed, but it needs a little more work than just
187221163Sadrian	// changing this check.
188221163Sadrian	if (lz_options->dict_size < LZMA_DICT_SIZE_MIN
189221163Sadrian			|| lz_options->dict_size
190221163Sadrian				> (UINT32_C(1) << 30) + (UINT32_C(1) << 29)
191221163Sadrian			|| lz_options->nice_len > lz_options->match_len_max)
192221163Sadrian		return true;
193221163Sadrian
194221163Sadrian	mf->keep_size_before = lz_options->before_size + lz_options->dict_size;
195221163Sadrian
196221163Sadrian	mf->keep_size_after = lz_options->after_size
197221163Sadrian			+ lz_options->match_len_max;
198221163Sadrian
199221163Sadrian	// To avoid constant memmove()s, allocate some extra space. Since
200221163Sadrian	// memmove()s become more expensive when the size of the buffer
201221163Sadrian	// increases, we reserve more space when a large dictionary is
202221163Sadrian	// used to make the memmove() calls rarer.
203221163Sadrian	//
204221163Sadrian	// This works with dictionaries up to about 3 GiB. If bigger
205221163Sadrian	// dictionary is wanted, some extra work is needed:
206221163Sadrian	//   - Several variables in lzma_mf have to be changed from uint32_t
207221163Sadrian	//     to size_t.
208221163Sadrian	//   - Memory usage calculation needs something too, e.g. use uint64_t
209221163Sadrian	//     for mf->size.
210221163Sadrian	uint32_t reserve = lz_options->dict_size / 2;
211221163Sadrian	if (reserve > (UINT32_C(1) << 30))
212221163Sadrian		reserve /= 2;
213221163Sadrian
214221163Sadrian	reserve += (lz_options->before_size + lz_options->match_len_max
215221163Sadrian			+ lz_options->after_size) / 2 + (UINT32_C(1) << 19);
216221163Sadrian
217221163Sadrian	const uint32_t old_size = mf->size;
218221163Sadrian	mf->size = mf->keep_size_before + reserve + mf->keep_size_after;
219221163Sadrian
220221163Sadrian	// Deallocate the old history buffer if it exists but has different
221221163Sadrian	// size than what is needed now.
222221163Sadrian	if (mf->buffer != NULL && old_size != mf->size) {
223221163Sadrian		lzma_free(mf->buffer, allocator);
224221163Sadrian		mf->buffer = NULL;
225221163Sadrian	}
226221163Sadrian
227221163Sadrian	// Match finder options
228221163Sadrian	mf->match_len_max = lz_options->match_len_max;
229221163Sadrian	mf->nice_len = lz_options->nice_len;
230221163Sadrian
231221163Sadrian	// cyclic_size has to stay smaller than 2 Gi. Note that this doesn't
232221163Sadrian	// mean limiting dictionary size to less than 2 GiB. With a match
233221163Sadrian	// finder that uses multibyte resolution (hashes start at e.g. every
234221163Sadrian	// fourth byte), cyclic_size would stay below 2 Gi even when
235221163Sadrian	// dictionary size is greater than 2 GiB.
236221163Sadrian	//
237221163Sadrian	// It would be possible to allow cyclic_size >= 2 Gi, but then we
238221163Sadrian	// would need to be careful to use 64-bit types in various places
239221163Sadrian	// (size_t could do since we would need bigger than 32-bit address
240221163Sadrian	// space anyway). It would also require either zeroing a multigigabyte
241221163Sadrian	// buffer at initialization (waste of time and RAM) or allow
242221163Sadrian	// normalization in lz_encoder_mf.c to access uninitialized
243221163Sadrian	// memory to keep the code simpler. The current way is simple and
244221163Sadrian	// still allows pretty big dictionaries, so I don't expect these
245221163Sadrian	// limits to change.
246221163Sadrian	mf->cyclic_size = lz_options->dict_size + 1;
247221163Sadrian
248221163Sadrian	// Validate the match finder ID and setup the function pointers.
249221163Sadrian	switch (lz_options->match_finder) {
250221163Sadrian#ifdef HAVE_MF_HC3
251221163Sadrian	case LZMA_MF_HC3:
252221163Sadrian		mf->find = &lzma_mf_hc3_find;
253221163Sadrian		mf->skip = &lzma_mf_hc3_skip;
254221163Sadrian		break;
255221163Sadrian#endif
256221163Sadrian#ifdef HAVE_MF_HC4
257221163Sadrian	case LZMA_MF_HC4:
258221163Sadrian		mf->find = &lzma_mf_hc4_find;
259221163Sadrian		mf->skip = &lzma_mf_hc4_skip;
260221163Sadrian		break;
261221163Sadrian#endif
262221163Sadrian#ifdef HAVE_MF_BT2
263221163Sadrian	case LZMA_MF_BT2:
264221163Sadrian		mf->find = &lzma_mf_bt2_find;
265221163Sadrian		mf->skip = &lzma_mf_bt2_skip;
266221163Sadrian		break;
267221163Sadrian#endif
268221163Sadrian#ifdef HAVE_MF_BT3
269221163Sadrian	case LZMA_MF_BT3:
270221163Sadrian		mf->find = &lzma_mf_bt3_find;
271221163Sadrian		mf->skip = &lzma_mf_bt3_skip;
272221163Sadrian		break;
273221163Sadrian#endif
274221163Sadrian#ifdef HAVE_MF_BT4
275221163Sadrian	case LZMA_MF_BT4:
276221163Sadrian		mf->find = &lzma_mf_bt4_find;
277221163Sadrian		mf->skip = &lzma_mf_bt4_skip;
278221163Sadrian		break;
279221163Sadrian#endif
280221163Sadrian
281221163Sadrian	default:
282221163Sadrian		return true;
283221163Sadrian	}
284221163Sadrian
285221163Sadrian	// Calculate the sizes of mf->hash and mf->son and check that
286221163Sadrian	// nice_len is big enough for the selected match finder.
287221163Sadrian	const uint32_t hash_bytes = lz_options->match_finder & 0x0F;
288221163Sadrian	if (hash_bytes > mf->nice_len)
289221163Sadrian		return true;
290221163Sadrian
291221163Sadrian	const bool is_bt = (lz_options->match_finder & 0x10) != 0;
292221163Sadrian	uint32_t hs;
293221163Sadrian
294221163Sadrian	if (hash_bytes == 2) {
295221163Sadrian		hs = 0xFFFF;
296221163Sadrian	} else {
297221163Sadrian		// Round dictionary size up to the next 2^n - 1 so it can
298221163Sadrian		// be used as a hash mask.
299221163Sadrian		hs = lz_options->dict_size - 1;
300221163Sadrian		hs |= hs >> 1;
301		hs |= hs >> 2;
302		hs |= hs >> 4;
303		hs |= hs >> 8;
304		hs >>= 1;
305		hs |= 0xFFFF;
306
307		if (hs > (UINT32_C(1) << 24)) {
308			if (hash_bytes == 3)
309				hs = (UINT32_C(1) << 24) - 1;
310			else
311				hs >>= 1;
312		}
313	}
314
315	mf->hash_mask = hs;
316
317	++hs;
318	if (hash_bytes > 2)
319		hs += HASH_2_SIZE;
320	if (hash_bytes > 3)
321		hs += HASH_3_SIZE;
322/*
323	No match finder uses this at the moment.
324	if (mf->hash_bytes > 4)
325		hs += HASH_4_SIZE;
326*/
327
328	// If the above code calculating hs is modified, make sure that
329	// this assertion stays valid (UINT32_MAX / 5 is not strictly the
330	// exact limit). If it doesn't, you need to calculate that
331	// hash_size_sum + sons_count cannot overflow.
332	assert(hs < UINT32_MAX / 5);
333
334	const uint32_t old_count = mf->hash_size_sum + mf->sons_count;
335	mf->hash_size_sum = hs;
336	mf->sons_count = mf->cyclic_size;
337	if (is_bt)
338		mf->sons_count *= 2;
339
340	const uint32_t new_count = mf->hash_size_sum + mf->sons_count;
341
342	// Deallocate the old hash array if it exists and has different size
343	// than what is needed now.
344	if (mf->hash != NULL && old_count != new_count) {
345		lzma_free(mf->hash, allocator);
346		mf->hash = NULL;
347	}
348
349	// Maximum number of match finder cycles
350	mf->depth = lz_options->depth;
351	if (mf->depth == 0) {
352		mf->depth = 16 + (mf->nice_len / 2);
353		if (!is_bt)
354			mf->depth /= 2;
355	}
356
357	return false;
358}
359
360
361static bool
362lz_encoder_init(lzma_mf *mf, lzma_allocator *allocator,
363		const lzma_lz_options *lz_options)
364{
365	// Allocate the history buffer.
366	if (mf->buffer == NULL) {
367		mf->buffer = lzma_alloc(mf->size, allocator);
368		if (mf->buffer == NULL)
369			return true;
370	}
371
372	// Use cyclic_size as initial mf->offset. This allows
373	// avoiding a few branches in the match finders. The downside is
374	// that match finder needs to be normalized more often, which may
375	// hurt performance with huge dictionaries.
376	mf->offset = mf->cyclic_size;
377	mf->read_pos = 0;
378	mf->read_ahead = 0;
379	mf->read_limit = 0;
380	mf->write_pos = 0;
381	mf->pending = 0;
382
383	// Allocate match finder's hash array.
384	const size_t alloc_count = mf->hash_size_sum + mf->sons_count;
385
386#if UINT32_MAX >= SIZE_MAX / 4
387	// Check for integer overflow. (Huge dictionaries are not
388	// possible on 32-bit CPU.)
389	if (alloc_count > SIZE_MAX / sizeof(uint32_t))
390		return true;
391#endif
392
393	if (mf->hash == NULL) {
394		mf->hash = lzma_alloc(alloc_count * sizeof(uint32_t),
395				allocator);
396		if (mf->hash == NULL)
397			return true;
398	}
399
400	mf->son = mf->hash + mf->hash_size_sum;
401	mf->cyclic_pos = 0;
402
403	// Initialize the hash table. Since EMPTY_HASH_VALUE is zero, we
404	// can use memset().
405/*
406	for (uint32_t i = 0; i < hash_size_sum; ++i)
407		mf->hash[i] = EMPTY_HASH_VALUE;
408*/
409	memzero(mf->hash, (size_t)(mf->hash_size_sum) * sizeof(uint32_t));
410
411	// We don't need to initialize mf->son, but not doing that will
412	// make Valgrind complain in normalization (see normalize() in
413	// lz_encoder_mf.c).
414	//
415	// Skipping this initialization is *very* good when big dictionary is
416	// used but only small amount of data gets actually compressed: most
417	// of the mf->hash won't get actually allocated by the kernel, so
418	// we avoid wasting RAM and improve initialization speed a lot.
419	//memzero(mf->son, (size_t)(mf->sons_count) * sizeof(uint32_t));
420
421	// Handle preset dictionary.
422	if (lz_options->preset_dict != NULL
423			&& lz_options->preset_dict_size > 0) {
424		// If the preset dictionary is bigger than the actual
425		// dictionary, use only the tail.
426		mf->write_pos = MIN(lz_options->preset_dict_size, mf->size);
427		memcpy(mf->buffer, lz_options->preset_dict
428				+ lz_options->preset_dict_size - mf->write_pos,
429				mf->write_pos);
430		mf->action = LZMA_SYNC_FLUSH;
431		mf->skip(mf, mf->write_pos);
432	}
433
434	mf->action = LZMA_RUN;
435
436	return false;
437}
438
439
440extern uint64_t
441lzma_lz_encoder_memusage(const lzma_lz_options *lz_options)
442{
443	// Old buffers must not exist when calling lz_encoder_prepare().
444	lzma_mf mf = {
445		.buffer = NULL,
446		.hash = NULL,
447	};
448
449	// Setup the size information into mf.
450	if (lz_encoder_prepare(&mf, NULL, lz_options))
451		return UINT64_MAX;
452
453	// Calculate the memory usage.
454	return (uint64_t)(mf.hash_size_sum + mf.sons_count)
455				* sizeof(uint32_t)
456			+ (uint64_t)(mf.size) + sizeof(lzma_coder);
457}
458
459
460static void
461lz_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
462{
463	lzma_next_end(&coder->next, allocator);
464
465	lzma_free(coder->mf.hash, allocator);
466	lzma_free(coder->mf.buffer, allocator);
467
468	if (coder->lz.end != NULL)
469		coder->lz.end(coder->lz.coder, allocator);
470	else
471		lzma_free(coder->lz.coder, allocator);
472
473	lzma_free(coder, allocator);
474	return;
475}
476
477
478static lzma_ret
479lz_encoder_update(lzma_coder *coder, lzma_allocator *allocator,
480		const lzma_filter *filters_null lzma_attribute((unused)),
481		const lzma_filter *reversed_filters)
482{
483	if (coder->lz.options_update == NULL)
484		return LZMA_PROG_ERROR;
485
486	return_if_error(coder->lz.options_update(
487			coder->lz.coder, reversed_filters));
488
489	return lzma_next_filter_update(
490			&coder->next, allocator, reversed_filters + 1);
491}
492
493
494extern lzma_ret
495lzma_lz_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
496		const lzma_filter_info *filters,
497		lzma_ret (*lz_init)(lzma_lz_encoder *lz,
498			lzma_allocator *allocator, const void *options,
499			lzma_lz_options *lz_options))
500{
501#ifdef HAVE_SMALL
502	// We need that the CRC32 table has been initialized.
503	lzma_crc32_init();
504#endif
505
506	// Allocate and initialize the base data structure.
507	if (next->coder == NULL) {
508		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
509		if (next->coder == NULL)
510			return LZMA_MEM_ERROR;
511
512		next->code = &lz_encode;
513		next->end = &lz_encoder_end;
514		next->update = &lz_encoder_update;
515
516		next->coder->lz.coder = NULL;
517		next->coder->lz.code = NULL;
518		next->coder->lz.end = NULL;
519
520		next->coder->mf.buffer = NULL;
521		next->coder->mf.hash = NULL;
522
523		next->coder->next = LZMA_NEXT_CODER_INIT;
524	}
525
526	// Initialize the LZ-based encoder.
527	lzma_lz_options lz_options;
528	return_if_error(lz_init(&next->coder->lz, allocator,
529			filters[0].options, &lz_options));
530
531	// Setup the size information into next->coder->mf and deallocate
532	// old buffers if they have wrong size.
533	if (lz_encoder_prepare(&next->coder->mf, allocator, &lz_options))
534		return LZMA_OPTIONS_ERROR;
535
536	// Allocate new buffers if needed, and do the rest of
537	// the initialization.
538	if (lz_encoder_init(&next->coder->mf, allocator, &lz_options))
539		return LZMA_MEM_ERROR;
540
541	// Initialize the next filter in the chain, if any.
542	return lzma_next_filter_init(&next->coder->next, allocator,
543			filters + 1);
544}
545
546
547extern LZMA_API(lzma_bool)
548lzma_mf_is_supported(lzma_match_finder mf)
549{
550	bool ret = false;
551
552#ifdef HAVE_MF_HC3
553	if (mf == LZMA_MF_HC3)
554		ret = true;
555#endif
556
557#ifdef HAVE_MF_HC4
558	if (mf == LZMA_MF_HC4)
559		ret = true;
560#endif
561
562#ifdef HAVE_MF_BT2
563	if (mf == LZMA_MF_BT2)
564		ret = true;
565#endif
566
567#ifdef HAVE_MF_BT3
568	if (mf == LZMA_MF_BT3)
569		ret = true;
570#endif
571
572#ifdef HAVE_MF_BT4
573	if (mf == LZMA_MF_BT4)
574		ret = true;
575#endif
576
577	return ret;
578}
579