1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       lz_encoder.c
4207753Smm/// \brief      LZ in window
5207753Smm///
6207753Smm//  Authors:    Igor Pavlov
7207753Smm//              Lasse Collin
8207753Smm//
9207753Smm//  This file has been put into the public domain.
10207753Smm//  You can do whatever you want with this file.
11207753Smm//
12207753Smm///////////////////////////////////////////////////////////////////////////////
13207753Smm
14207753Smm#include "lz_encoder.h"
15207753Smm#include "lz_encoder_hash.h"
16207753Smm
17207753Smm// See lz_encoder_hash.h. This is a bit hackish but avoids making
18207753Smm// endianness a conditional in makefiles.
19207753Smm#if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL)
20207753Smm#	include "lz_encoder_hash_table.h"
21207753Smm#endif
22207753Smm
23207753Smm
24207753Smmstruct lzma_coder_s {
25207753Smm	/// LZ-based encoder e.g. LZMA
26207753Smm	lzma_lz_encoder lz;
27207753Smm
28207753Smm	/// History buffer and match finder
29207753Smm	lzma_mf mf;
30207753Smm
31207753Smm	/// Next coder in the chain
32207753Smm	lzma_next_coder next;
33207753Smm};
34207753Smm
35207753Smm
36207753Smm/// \brief      Moves the data in the input window to free space for new data
37207753Smm///
38207753Smm/// mf->buffer is a sliding input window, which keeps mf->keep_size_before
39207753Smm/// bytes of input history available all the time. Now and then we need to
40207753Smm/// "slide" the buffer to make space for the new data to the end of the
41207753Smm/// buffer. At the same time, data older than keep_size_before is dropped.
42207753Smm///
43207753Smmstatic void
44207753Smmmove_window(lzma_mf *mf)
45207753Smm{
46207753Smm	// Align the move to a multiple of 16 bytes. Some LZ-based encoders
47207753Smm	// like LZMA use the lowest bits of mf->read_pos to know the
48207753Smm	// alignment of the uncompressed data. We also get better speed
49207753Smm	// for memmove() with aligned buffers.
50207753Smm	assert(mf->read_pos > mf->keep_size_before);
51207753Smm	const uint32_t move_offset
52207753Smm		= (mf->read_pos - mf->keep_size_before) & ~UINT32_C(15);
53207753Smm
54207753Smm	assert(mf->write_pos > move_offset);
55207753Smm	const size_t move_size = mf->write_pos - move_offset;
56207753Smm
57207753Smm	assert(move_offset + move_size <= mf->size);
58207753Smm
59207753Smm	memmove(mf->buffer, mf->buffer + move_offset, move_size);
60207753Smm
61207753Smm	mf->offset += move_offset;
62207753Smm	mf->read_pos -= move_offset;
63207753Smm	mf->read_limit -= move_offset;
64207753Smm	mf->write_pos -= move_offset;
65207753Smm
66207753Smm	return;
67207753Smm}
68207753Smm
69207753Smm
70207753Smm/// \brief      Tries to fill the input window (mf->buffer)
71207753Smm///
72207753Smm/// If we are the last encoder in the chain, our input data is in in[].
73207753Smm/// Otherwise we call the next filter in the chain to process in[] and
74207753Smm/// write its output to mf->buffer.
75207753Smm///
76207753Smm/// This function must not be called once it has returned LZMA_STREAM_END.
77207753Smm///
78207753Smmstatic lzma_ret
79207753Smmfill_window(lzma_coder *coder, lzma_allocator *allocator, const uint8_t *in,
80207753Smm		size_t *in_pos, size_t in_size, lzma_action action)
81207753Smm{
82207753Smm	assert(coder->mf.read_pos <= coder->mf.write_pos);
83207753Smm
84207753Smm	// Move the sliding window if needed.
85207753Smm	if (coder->mf.read_pos >= coder->mf.size - coder->mf.keep_size_after)
86207753Smm		move_window(&coder->mf);
87207753Smm
88207753Smm	// Maybe this is ugly, but lzma_mf uses uint32_t for most things
89207753Smm	// (which I find cleanest), but we need size_t here when filling
90207753Smm	// the history window.
91207753Smm	size_t write_pos = coder->mf.write_pos;
92207753Smm	lzma_ret ret;
93207753Smm	if (coder->next.code == NULL) {
94207753Smm		// Not using a filter, simply memcpy() as much as possible.
95207753Smm		lzma_bufcpy(in, in_pos, in_size, coder->mf.buffer,
96207753Smm				&write_pos, coder->mf.size);
97207753Smm
98207753Smm		ret = action != LZMA_RUN && *in_pos == in_size
99207753Smm				? LZMA_STREAM_END : LZMA_OK;
100207753Smm
101207753Smm	} else {
102207753Smm		ret = coder->next.code(coder->next.coder, allocator,
103207753Smm				in, in_pos, in_size,
104207753Smm				coder->mf.buffer, &write_pos,
105207753Smm				coder->mf.size, action);
106207753Smm	}
107207753Smm
108207753Smm	coder->mf.write_pos = write_pos;
109207753Smm
110207753Smm	// If end of stream has been reached or flushing completed, we allow
111207753Smm	// the encoder to process all the input (that is, read_pos is allowed
112207753Smm	// to reach write_pos). Otherwise we keep keep_size_after bytes
113207753Smm	// available as prebuffer.
114207753Smm	if (ret == LZMA_STREAM_END) {
115207753Smm		assert(*in_pos == in_size);
116207753Smm		ret = LZMA_OK;
117207753Smm		coder->mf.action = action;
118207753Smm		coder->mf.read_limit = coder->mf.write_pos;
119207753Smm
120207753Smm	} else if (coder->mf.write_pos > coder->mf.keep_size_after) {
121207753Smm		// This needs to be done conditionally, because if we got
122207753Smm		// only little new input, there may be too little input
123207753Smm		// to do any encoding yet.
124207753Smm		coder->mf.read_limit = coder->mf.write_pos
125207753Smm				- coder->mf.keep_size_after;
126207753Smm	}
127207753Smm
128207753Smm	// Restart the match finder after finished LZMA_SYNC_FLUSH.
129207753Smm	if (coder->mf.pending > 0
130207753Smm			&& coder->mf.read_pos < coder->mf.read_limit) {
131207753Smm		// Match finder may update coder->pending and expects it to
132207753Smm		// start from zero, so use a temporary variable.
133207753Smm		const size_t pending = coder->mf.pending;
134207753Smm		coder->mf.pending = 0;
135207753Smm
136207753Smm		// Rewind read_pos so that the match finder can hash
137207753Smm		// the pending bytes.
138207753Smm		assert(coder->mf.read_pos >= pending);
139207753Smm		coder->mf.read_pos -= pending;
140207753Smm
141207753Smm		// Call the skip function directly instead of using
142207753Smm		// mf_skip(), since we don't want to touch mf->read_ahead.
143207753Smm		coder->mf.skip(&coder->mf, pending);
144207753Smm	}
145207753Smm
146207753Smm	return ret;
147207753Smm}
148207753Smm
149207753Smm
150207753Smmstatic lzma_ret
151207753Smmlz_encode(lzma_coder *coder, lzma_allocator *allocator,
152207753Smm		const uint8_t *restrict in, size_t *restrict in_pos,
153207753Smm		size_t in_size,
154207753Smm		uint8_t *restrict out, size_t *restrict out_pos,
155207753Smm		size_t out_size, lzma_action action)
156207753Smm{
157207753Smm	while (*out_pos < out_size
158207753Smm			&& (*in_pos < in_size || action != LZMA_RUN)) {
159207753Smm		// Read more data to coder->mf.buffer if needed.
160207753Smm		if (coder->mf.action == LZMA_RUN && coder->mf.read_pos
161207753Smm				>= coder->mf.read_limit)
162207753Smm			return_if_error(fill_window(coder, allocator,
163207753Smm					in, in_pos, in_size, action));
164207753Smm
165207753Smm		// Encode
166207753Smm		const lzma_ret ret = coder->lz.code(coder->lz.coder,
167207753Smm				&coder->mf, out, out_pos, out_size);
168207753Smm		if (ret != LZMA_OK) {
169207753Smm			// Setting this to LZMA_RUN for cases when we are
170207753Smm			// flushing. It doesn't matter when finishing or if
171207753Smm			// an error occurred.
172207753Smm			coder->mf.action = LZMA_RUN;
173207753Smm			return ret;
174207753Smm		}
175207753Smm	}
176207753Smm
177207753Smm	return LZMA_OK;
178207753Smm}
179207753Smm
180207753Smm
181207753Smmstatic bool
182207753Smmlz_encoder_prepare(lzma_mf *mf, lzma_allocator *allocator,
183207753Smm		const lzma_lz_options *lz_options)
184207753Smm{
185207753Smm	// For now, the dictionary size is limited to 1.5 GiB. This may grow
186207753Smm	// in the future if needed, but it needs a little more work than just
187207753Smm	// changing this check.
188207753Smm	if (lz_options->dict_size < LZMA_DICT_SIZE_MIN
189207753Smm			|| lz_options->dict_size
190207753Smm				> (UINT32_C(1) << 30) + (UINT32_C(1) << 29)
191207753Smm			|| lz_options->nice_len > lz_options->match_len_max)
192207753Smm		return true;
193207753Smm
194207753Smm	mf->keep_size_before = lz_options->before_size + lz_options->dict_size;
195207753Smm
196207753Smm	mf->keep_size_after = lz_options->after_size
197207753Smm			+ lz_options->match_len_max;
198207753Smm
199207753Smm	// To avoid constant memmove()s, allocate some extra space. Since
200207753Smm	// memmove()s become more expensive when the size of the buffer
201207753Smm	// increases, we reserve more space when a large dictionary is
202207753Smm	// used to make the memmove() calls rarer.
203207753Smm	//
204207753Smm	// This works with dictionaries up to about 3 GiB. If bigger
205207753Smm	// dictionary is wanted, some extra work is needed:
206207753Smm	//   - Several variables in lzma_mf have to be changed from uint32_t
207207753Smm	//     to size_t.
208207753Smm	//   - Memory usage calculation needs something too, e.g. use uint64_t
209207753Smm	//     for mf->size.
210207753Smm	uint32_t reserve = lz_options->dict_size / 2;
211207753Smm	if (reserve > (UINT32_C(1) << 30))
212207753Smm		reserve /= 2;
213207753Smm
214207753Smm	reserve += (lz_options->before_size + lz_options->match_len_max
215207753Smm			+ lz_options->after_size) / 2 + (UINT32_C(1) << 19);
216207753Smm
217207753Smm	const uint32_t old_size = mf->size;
218207753Smm	mf->size = mf->keep_size_before + reserve + mf->keep_size_after;
219207753Smm
220207753Smm	// Deallocate the old history buffer if it exists but has different
221207753Smm	// size than what is needed now.
222207753Smm	if (mf->buffer != NULL && old_size != mf->size) {
223207753Smm		lzma_free(mf->buffer, allocator);
224207753Smm		mf->buffer = NULL;
225207753Smm	}
226207753Smm
227207753Smm	// Match finder options
228207753Smm	mf->match_len_max = lz_options->match_len_max;
229207753Smm	mf->nice_len = lz_options->nice_len;
230207753Smm
231207753Smm	// cyclic_size has to stay smaller than 2 Gi. Note that this doesn't
232207753Smm	// mean limiting dictionary size to less than 2 GiB. With a match
233207753Smm	// finder that uses multibyte resolution (hashes start at e.g. every
234207753Smm	// fourth byte), cyclic_size would stay below 2 Gi even when
235207753Smm	// dictionary size is greater than 2 GiB.
236207753Smm	//
237207753Smm	// It would be possible to allow cyclic_size >= 2 Gi, but then we
238207753Smm	// would need to be careful to use 64-bit types in various places
239207753Smm	// (size_t could do since we would need bigger than 32-bit address
240207753Smm	// space anyway). It would also require either zeroing a multigigabyte
241207753Smm	// buffer at initialization (waste of time and RAM) or allow
242207753Smm	// normalization in lz_encoder_mf.c to access uninitialized
243207753Smm	// memory to keep the code simpler. The current way is simple and
244207753Smm	// still allows pretty big dictionaries, so I don't expect these
245207753Smm	// limits to change.
246207753Smm	mf->cyclic_size = lz_options->dict_size + 1;
247207753Smm
248207753Smm	// Validate the match finder ID and setup the function pointers.
249207753Smm	switch (lz_options->match_finder) {
250207753Smm#ifdef HAVE_MF_HC3
251207753Smm	case LZMA_MF_HC3:
252207753Smm		mf->find = &lzma_mf_hc3_find;
253207753Smm		mf->skip = &lzma_mf_hc3_skip;
254207753Smm		break;
255207753Smm#endif
256207753Smm#ifdef HAVE_MF_HC4
257207753Smm	case LZMA_MF_HC4:
258207753Smm		mf->find = &lzma_mf_hc4_find;
259207753Smm		mf->skip = &lzma_mf_hc4_skip;
260207753Smm		break;
261207753Smm#endif
262207753Smm#ifdef HAVE_MF_BT2
263207753Smm	case LZMA_MF_BT2:
264207753Smm		mf->find = &lzma_mf_bt2_find;
265207753Smm		mf->skip = &lzma_mf_bt2_skip;
266207753Smm		break;
267207753Smm#endif
268207753Smm#ifdef HAVE_MF_BT3
269207753Smm	case LZMA_MF_BT3:
270207753Smm		mf->find = &lzma_mf_bt3_find;
271207753Smm		mf->skip = &lzma_mf_bt3_skip;
272207753Smm		break;
273207753Smm#endif
274207753Smm#ifdef HAVE_MF_BT4
275207753Smm	case LZMA_MF_BT4:
276207753Smm		mf->find = &lzma_mf_bt4_find;
277207753Smm		mf->skip = &lzma_mf_bt4_skip;
278207753Smm		break;
279207753Smm#endif
280207753Smm
281207753Smm	default:
282207753Smm		return true;
283207753Smm	}
284207753Smm
285207753Smm	// Calculate the sizes of mf->hash and mf->son and check that
286207753Smm	// nice_len is big enough for the selected match finder.
287207753Smm	const uint32_t hash_bytes = lz_options->match_finder & 0x0F;
288207753Smm	if (hash_bytes > mf->nice_len)
289207753Smm		return true;
290207753Smm
291207753Smm	const bool is_bt = (lz_options->match_finder & 0x10) != 0;
292207753Smm	uint32_t hs;
293207753Smm
294207753Smm	if (hash_bytes == 2) {
295207753Smm		hs = 0xFFFF;
296207753Smm	} else {
297207753Smm		// Round dictionary size up to the next 2^n - 1 so it can
298207753Smm		// be used as a hash mask.
299207753Smm		hs = lz_options->dict_size - 1;
300207753Smm		hs |= hs >> 1;
301207753Smm		hs |= hs >> 2;
302207753Smm		hs |= hs >> 4;
303207753Smm		hs |= hs >> 8;
304207753Smm		hs >>= 1;
305207753Smm		hs |= 0xFFFF;
306207753Smm
307207753Smm		if (hs > (UINT32_C(1) << 24)) {
308207753Smm			if (hash_bytes == 3)
309207753Smm				hs = (UINT32_C(1) << 24) - 1;
310207753Smm			else
311207753Smm				hs >>= 1;
312207753Smm		}
313207753Smm	}
314207753Smm
315207753Smm	mf->hash_mask = hs;
316207753Smm
317207753Smm	++hs;
318207753Smm	if (hash_bytes > 2)
319207753Smm		hs += HASH_2_SIZE;
320207753Smm	if (hash_bytes > 3)
321207753Smm		hs += HASH_3_SIZE;
322207753Smm/*
323207753Smm	No match finder uses this at the moment.
324207753Smm	if (mf->hash_bytes > 4)
325207753Smm		hs += HASH_4_SIZE;
326207753Smm*/
327207753Smm
328207753Smm	// If the above code calculating hs is modified, make sure that
329207753Smm	// this assertion stays valid (UINT32_MAX / 5 is not strictly the
330207753Smm	// exact limit). If it doesn't, you need to calculate that
331207753Smm	// hash_size_sum + sons_count cannot overflow.
332207753Smm	assert(hs < UINT32_MAX / 5);
333207753Smm
334207753Smm	const uint32_t old_count = mf->hash_size_sum + mf->sons_count;
335207753Smm	mf->hash_size_sum = hs;
336207753Smm	mf->sons_count = mf->cyclic_size;
337207753Smm	if (is_bt)
338207753Smm		mf->sons_count *= 2;
339207753Smm
340207753Smm	const uint32_t new_count = mf->hash_size_sum + mf->sons_count;
341207753Smm
342207753Smm	// Deallocate the old hash array if it exists and has different size
343207753Smm	// than what is needed now.
344213700Smm	if (old_count != new_count) {
345207753Smm		lzma_free(mf->hash, allocator);
346207753Smm		mf->hash = NULL;
347207753Smm	}
348207753Smm
349207753Smm	// Maximum number of match finder cycles
350207753Smm	mf->depth = lz_options->depth;
351207753Smm	if (mf->depth == 0) {
352213700Smm		if (is_bt)
353213700Smm			mf->depth = 16 + mf->nice_len / 2;
354213700Smm		else
355213700Smm			mf->depth = 4 + mf->nice_len / 4;
356207753Smm	}
357207753Smm
358207753Smm	return false;
359207753Smm}
360207753Smm
361207753Smm
362207753Smmstatic bool
363207753Smmlz_encoder_init(lzma_mf *mf, lzma_allocator *allocator,
364207753Smm		const lzma_lz_options *lz_options)
365207753Smm{
366207753Smm	// Allocate the history buffer.
367207753Smm	if (mf->buffer == NULL) {
368207753Smm		mf->buffer = lzma_alloc(mf->size, allocator);
369207753Smm		if (mf->buffer == NULL)
370207753Smm			return true;
371207753Smm	}
372207753Smm
373207753Smm	// Use cyclic_size as initial mf->offset. This allows
374207753Smm	// avoiding a few branches in the match finders. The downside is
375207753Smm	// that match finder needs to be normalized more often, which may
376207753Smm	// hurt performance with huge dictionaries.
377207753Smm	mf->offset = mf->cyclic_size;
378207753Smm	mf->read_pos = 0;
379207753Smm	mf->read_ahead = 0;
380207753Smm	mf->read_limit = 0;
381207753Smm	mf->write_pos = 0;
382207753Smm	mf->pending = 0;
383207753Smm
384207753Smm	// Allocate match finder's hash array.
385207753Smm	const size_t alloc_count = mf->hash_size_sum + mf->sons_count;
386207753Smm
387207753Smm#if UINT32_MAX >= SIZE_MAX / 4
388207753Smm	// Check for integer overflow. (Huge dictionaries are not
389207753Smm	// possible on 32-bit CPU.)
390207753Smm	if (alloc_count > SIZE_MAX / sizeof(uint32_t))
391207753Smm		return true;
392207753Smm#endif
393207753Smm
394207753Smm	if (mf->hash == NULL) {
395207753Smm		mf->hash = lzma_alloc(alloc_count * sizeof(uint32_t),
396207753Smm				allocator);
397207753Smm		if (mf->hash == NULL)
398207753Smm			return true;
399207753Smm	}
400207753Smm
401207753Smm	mf->son = mf->hash + mf->hash_size_sum;
402207753Smm	mf->cyclic_pos = 0;
403207753Smm
404207753Smm	// Initialize the hash table. Since EMPTY_HASH_VALUE is zero, we
405207753Smm	// can use memset().
406207753Smm/*
407207753Smm	for (uint32_t i = 0; i < hash_size_sum; ++i)
408207753Smm		mf->hash[i] = EMPTY_HASH_VALUE;
409207753Smm*/
410207753Smm	memzero(mf->hash, (size_t)(mf->hash_size_sum) * sizeof(uint32_t));
411207753Smm
412207753Smm	// We don't need to initialize mf->son, but not doing that will
413207753Smm	// make Valgrind complain in normalization (see normalize() in
414207753Smm	// lz_encoder_mf.c).
415207753Smm	//
416207753Smm	// Skipping this initialization is *very* good when big dictionary is
417207753Smm	// used but only small amount of data gets actually compressed: most
418207753Smm	// of the mf->hash won't get actually allocated by the kernel, so
419207753Smm	// we avoid wasting RAM and improve initialization speed a lot.
420207753Smm	//memzero(mf->son, (size_t)(mf->sons_count) * sizeof(uint32_t));
421207753Smm
422207753Smm	// Handle preset dictionary.
423207753Smm	if (lz_options->preset_dict != NULL
424207753Smm			&& lz_options->preset_dict_size > 0) {
425207753Smm		// If the preset dictionary is bigger than the actual
426207753Smm		// dictionary, use only the tail.
427213700Smm		mf->write_pos = my_min(lz_options->preset_dict_size, mf->size);
428207753Smm		memcpy(mf->buffer, lz_options->preset_dict
429207753Smm				+ lz_options->preset_dict_size - mf->write_pos,
430207753Smm				mf->write_pos);
431207753Smm		mf->action = LZMA_SYNC_FLUSH;
432207753Smm		mf->skip(mf, mf->write_pos);
433207753Smm	}
434207753Smm
435207753Smm	mf->action = LZMA_RUN;
436207753Smm
437207753Smm	return false;
438207753Smm}
439207753Smm
440207753Smm
441207753Smmextern uint64_t
442207753Smmlzma_lz_encoder_memusage(const lzma_lz_options *lz_options)
443207753Smm{
444207753Smm	// Old buffers must not exist when calling lz_encoder_prepare().
445207753Smm	lzma_mf mf = {
446207753Smm		.buffer = NULL,
447207753Smm		.hash = NULL,
448213700Smm		.hash_size_sum = 0,
449213700Smm		.sons_count = 0,
450207753Smm	};
451207753Smm
452207753Smm	// Setup the size information into mf.
453207753Smm	if (lz_encoder_prepare(&mf, NULL, lz_options))
454207753Smm		return UINT64_MAX;
455207753Smm
456207753Smm	// Calculate the memory usage.
457207753Smm	return (uint64_t)(mf.hash_size_sum + mf.sons_count)
458207753Smm				* sizeof(uint32_t)
459207753Smm			+ (uint64_t)(mf.size) + sizeof(lzma_coder);
460207753Smm}
461207753Smm
462207753Smm
463207753Smmstatic void
464207753Smmlz_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
465207753Smm{
466207753Smm	lzma_next_end(&coder->next, allocator);
467207753Smm
468207753Smm	lzma_free(coder->mf.hash, allocator);
469207753Smm	lzma_free(coder->mf.buffer, allocator);
470207753Smm
471207753Smm	if (coder->lz.end != NULL)
472207753Smm		coder->lz.end(coder->lz.coder, allocator);
473207753Smm	else
474207753Smm		lzma_free(coder->lz.coder, allocator);
475207753Smm
476207753Smm	lzma_free(coder, allocator);
477207753Smm	return;
478207753Smm}
479207753Smm
480207753Smm
481207753Smmstatic lzma_ret
482207753Smmlz_encoder_update(lzma_coder *coder, lzma_allocator *allocator,
483223935Smm		const lzma_filter *filters_null lzma_attribute((__unused__)),
484207753Smm		const lzma_filter *reversed_filters)
485207753Smm{
486207753Smm	if (coder->lz.options_update == NULL)
487207753Smm		return LZMA_PROG_ERROR;
488207753Smm
489207753Smm	return_if_error(coder->lz.options_update(
490207753Smm			coder->lz.coder, reversed_filters));
491207753Smm
492207753Smm	return lzma_next_filter_update(
493207753Smm			&coder->next, allocator, reversed_filters + 1);
494207753Smm}
495207753Smm
496207753Smm
497207753Smmextern lzma_ret
498207753Smmlzma_lz_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
499207753Smm		const lzma_filter_info *filters,
500207753Smm		lzma_ret (*lz_init)(lzma_lz_encoder *lz,
501207753Smm			lzma_allocator *allocator, const void *options,
502207753Smm			lzma_lz_options *lz_options))
503207753Smm{
504207753Smm#ifdef HAVE_SMALL
505207753Smm	// We need that the CRC32 table has been initialized.
506207753Smm	lzma_crc32_init();
507207753Smm#endif
508207753Smm
509207753Smm	// Allocate and initialize the base data structure.
510207753Smm	if (next->coder == NULL) {
511207753Smm		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
512207753Smm		if (next->coder == NULL)
513207753Smm			return LZMA_MEM_ERROR;
514207753Smm
515207753Smm		next->code = &lz_encode;
516207753Smm		next->end = &lz_encoder_end;
517207753Smm		next->update = &lz_encoder_update;
518207753Smm
519207753Smm		next->coder->lz.coder = NULL;
520207753Smm		next->coder->lz.code = NULL;
521207753Smm		next->coder->lz.end = NULL;
522207753Smm
523207753Smm		next->coder->mf.buffer = NULL;
524207753Smm		next->coder->mf.hash = NULL;
525213700Smm		next->coder->mf.hash_size_sum = 0;
526213700Smm		next->coder->mf.sons_count = 0;
527207753Smm
528207753Smm		next->coder->next = LZMA_NEXT_CODER_INIT;
529207753Smm	}
530207753Smm
531207753Smm	// Initialize the LZ-based encoder.
532207753Smm	lzma_lz_options lz_options;
533207753Smm	return_if_error(lz_init(&next->coder->lz, allocator,
534207753Smm			filters[0].options, &lz_options));
535207753Smm
536207753Smm	// Setup the size information into next->coder->mf and deallocate
537207753Smm	// old buffers if they have wrong size.
538207753Smm	if (lz_encoder_prepare(&next->coder->mf, allocator, &lz_options))
539207753Smm		return LZMA_OPTIONS_ERROR;
540207753Smm
541207753Smm	// Allocate new buffers if needed, and do the rest of
542207753Smm	// the initialization.
543207753Smm	if (lz_encoder_init(&next->coder->mf, allocator, &lz_options))
544207753Smm		return LZMA_MEM_ERROR;
545207753Smm
546207753Smm	// Initialize the next filter in the chain, if any.
547207753Smm	return lzma_next_filter_init(&next->coder->next, allocator,
548207753Smm			filters + 1);
549207753Smm}
550207753Smm
551207753Smm
552207753Smmextern LZMA_API(lzma_bool)
553207753Smmlzma_mf_is_supported(lzma_match_finder mf)
554207753Smm{
555207753Smm	bool ret = false;
556207753Smm
557207753Smm#ifdef HAVE_MF_HC3
558207753Smm	if (mf == LZMA_MF_HC3)
559207753Smm		ret = true;
560207753Smm#endif
561207753Smm
562207753Smm#ifdef HAVE_MF_HC4
563207753Smm	if (mf == LZMA_MF_HC4)
564207753Smm		ret = true;
565207753Smm#endif
566207753Smm
567207753Smm#ifdef HAVE_MF_BT2
568207753Smm	if (mf == LZMA_MF_BT2)
569207753Smm		ret = true;
570207753Smm#endif
571207753Smm
572207753Smm#ifdef HAVE_MF_BT3
573207753Smm	if (mf == LZMA_MF_BT3)
574207753Smm		ret = true;
575207753Smm#endif
576207753Smm
577207753Smm#ifdef HAVE_MF_BT4
578207753Smm	if (mf == LZMA_MF_BT4)
579207753Smm		ret = true;
580207753Smm#endif
581207753Smm
582207753Smm	return ret;
583207753Smm}
584