xref: /dflybsd-src/contrib/xz/src/liblzma/lz/lz_encoder.c (revision 46a2189dd86b644c3a76ac281d84b4182fd66b95)
12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
32940b44dSPeter Avalos /// \file       lz_encoder.c
42940b44dSPeter Avalos /// \brief      LZ in window
52940b44dSPeter Avalos ///
62940b44dSPeter Avalos //  Authors:    Igor Pavlov
72940b44dSPeter Avalos //              Lasse Collin
82940b44dSPeter Avalos //
92940b44dSPeter Avalos //  This file has been put into the public domain.
102940b44dSPeter Avalos //  You can do whatever you want with this file.
112940b44dSPeter Avalos //
122940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
132940b44dSPeter Avalos 
142940b44dSPeter Avalos #include "lz_encoder.h"
152940b44dSPeter Avalos #include "lz_encoder_hash.h"
162940b44dSPeter Avalos 
172940b44dSPeter Avalos // See lz_encoder_hash.h. This is a bit hackish but avoids making
182940b44dSPeter Avalos // endianness a conditional in makefiles.
192940b44dSPeter Avalos #if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL)
202940b44dSPeter Avalos #	include "lz_encoder_hash_table.h"
212940b44dSPeter Avalos #endif
222940b44dSPeter Avalos 
2315ab8c86SJohn Marino #include "memcmplen.h"
2415ab8c86SJohn Marino 
252940b44dSPeter Avalos 
26*46a2189dSzrj typedef struct {
272940b44dSPeter Avalos 	/// LZ-based encoder e.g. LZMA
282940b44dSPeter Avalos 	lzma_lz_encoder lz;
292940b44dSPeter Avalos 
302940b44dSPeter Avalos 	/// History buffer and match finder
312940b44dSPeter Avalos 	lzma_mf mf;
322940b44dSPeter Avalos 
332940b44dSPeter Avalos 	/// Next coder in the chain
342940b44dSPeter Avalos 	lzma_next_coder next;
35*46a2189dSzrj } lzma_coder;
362940b44dSPeter Avalos 
372940b44dSPeter Avalos 
382940b44dSPeter Avalos /// \brief      Moves the data in the input window to free space for new data
392940b44dSPeter Avalos ///
402940b44dSPeter Avalos /// mf->buffer is a sliding input window, which keeps mf->keep_size_before
412940b44dSPeter Avalos /// bytes of input history available all the time. Now and then we need to
422940b44dSPeter Avalos /// "slide" the buffer to make space for the new data to the end of the
432940b44dSPeter Avalos /// buffer. At the same time, data older than keep_size_before is dropped.
442940b44dSPeter Avalos ///
452940b44dSPeter Avalos static void
move_window(lzma_mf * mf)462940b44dSPeter Avalos move_window(lzma_mf *mf)
472940b44dSPeter Avalos {
482940b44dSPeter Avalos 	// Align the move to a multiple of 16 bytes. Some LZ-based encoders
492940b44dSPeter Avalos 	// like LZMA use the lowest bits of mf->read_pos to know the
502940b44dSPeter Avalos 	// alignment of the uncompressed data. We also get better speed
512940b44dSPeter Avalos 	// for memmove() with aligned buffers.
522940b44dSPeter Avalos 	assert(mf->read_pos > mf->keep_size_before);
532940b44dSPeter Avalos 	const uint32_t move_offset
542940b44dSPeter Avalos 		= (mf->read_pos - mf->keep_size_before) & ~UINT32_C(15);
552940b44dSPeter Avalos 
562940b44dSPeter Avalos 	assert(mf->write_pos > move_offset);
572940b44dSPeter Avalos 	const size_t move_size = mf->write_pos - move_offset;
582940b44dSPeter Avalos 
592940b44dSPeter Avalos 	assert(move_offset + move_size <= mf->size);
602940b44dSPeter Avalos 
612940b44dSPeter Avalos 	memmove(mf->buffer, mf->buffer + move_offset, move_size);
622940b44dSPeter Avalos 
632940b44dSPeter Avalos 	mf->offset += move_offset;
642940b44dSPeter Avalos 	mf->read_pos -= move_offset;
652940b44dSPeter Avalos 	mf->read_limit -= move_offset;
662940b44dSPeter Avalos 	mf->write_pos -= move_offset;
672940b44dSPeter Avalos 
682940b44dSPeter Avalos 	return;
692940b44dSPeter Avalos }
702940b44dSPeter Avalos 
712940b44dSPeter Avalos 
722940b44dSPeter Avalos /// \brief      Tries to fill the input window (mf->buffer)
732940b44dSPeter Avalos ///
742940b44dSPeter Avalos /// If we are the last encoder in the chain, our input data is in in[].
752940b44dSPeter Avalos /// Otherwise we call the next filter in the chain to process in[] and
762940b44dSPeter Avalos /// write its output to mf->buffer.
772940b44dSPeter Avalos ///
782940b44dSPeter Avalos /// This function must not be called once it has returned LZMA_STREAM_END.
792940b44dSPeter Avalos ///
802940b44dSPeter Avalos static lzma_ret
fill_window(lzma_coder * coder,const lzma_allocator * allocator,const uint8_t * in,size_t * in_pos,size_t in_size,lzma_action action)8115ab8c86SJohn Marino fill_window(lzma_coder *coder, const lzma_allocator *allocator,
8215ab8c86SJohn Marino 		const uint8_t *in, size_t *in_pos, size_t in_size,
8315ab8c86SJohn Marino 		lzma_action action)
842940b44dSPeter Avalos {
852940b44dSPeter Avalos 	assert(coder->mf.read_pos <= coder->mf.write_pos);
862940b44dSPeter Avalos 
872940b44dSPeter Avalos 	// Move the sliding window if needed.
882940b44dSPeter Avalos 	if (coder->mf.read_pos >= coder->mf.size - coder->mf.keep_size_after)
892940b44dSPeter Avalos 		move_window(&coder->mf);
902940b44dSPeter Avalos 
912940b44dSPeter Avalos 	// Maybe this is ugly, but lzma_mf uses uint32_t for most things
922940b44dSPeter Avalos 	// (which I find cleanest), but we need size_t here when filling
932940b44dSPeter Avalos 	// the history window.
942940b44dSPeter Avalos 	size_t write_pos = coder->mf.write_pos;
952940b44dSPeter Avalos 	lzma_ret ret;
962940b44dSPeter Avalos 	if (coder->next.code == NULL) {
972940b44dSPeter Avalos 		// Not using a filter, simply memcpy() as much as possible.
982940b44dSPeter Avalos 		lzma_bufcpy(in, in_pos, in_size, coder->mf.buffer,
992940b44dSPeter Avalos 				&write_pos, coder->mf.size);
1002940b44dSPeter Avalos 
1012940b44dSPeter Avalos 		ret = action != LZMA_RUN && *in_pos == in_size
1022940b44dSPeter Avalos 				? LZMA_STREAM_END : LZMA_OK;
1032940b44dSPeter Avalos 
1042940b44dSPeter Avalos 	} else {
1052940b44dSPeter Avalos 		ret = coder->next.code(coder->next.coder, allocator,
1062940b44dSPeter Avalos 				in, in_pos, in_size,
1072940b44dSPeter Avalos 				coder->mf.buffer, &write_pos,
1082940b44dSPeter Avalos 				coder->mf.size, action);
1092940b44dSPeter Avalos 	}
1102940b44dSPeter Avalos 
1112940b44dSPeter Avalos 	coder->mf.write_pos = write_pos;
1122940b44dSPeter Avalos 
11315ab8c86SJohn Marino 	// Silence Valgrind. lzma_memcmplen() can read extra bytes
11415ab8c86SJohn Marino 	// and Valgrind will give warnings if those bytes are uninitialized
11515ab8c86SJohn Marino 	// because Valgrind cannot see that the values of the uninitialized
11615ab8c86SJohn Marino 	// bytes are eventually ignored.
11715ab8c86SJohn Marino 	memzero(coder->mf.buffer + write_pos, LZMA_MEMCMPLEN_EXTRA);
11815ab8c86SJohn Marino 
1192940b44dSPeter Avalos 	// If end of stream has been reached or flushing completed, we allow
1202940b44dSPeter Avalos 	// the encoder to process all the input (that is, read_pos is allowed
1212940b44dSPeter Avalos 	// to reach write_pos). Otherwise we keep keep_size_after bytes
1222940b44dSPeter Avalos 	// available as prebuffer.
1232940b44dSPeter Avalos 	if (ret == LZMA_STREAM_END) {
1242940b44dSPeter Avalos 		assert(*in_pos == in_size);
1252940b44dSPeter Avalos 		ret = LZMA_OK;
1262940b44dSPeter Avalos 		coder->mf.action = action;
1272940b44dSPeter Avalos 		coder->mf.read_limit = coder->mf.write_pos;
1282940b44dSPeter Avalos 
1292940b44dSPeter Avalos 	} else if (coder->mf.write_pos > coder->mf.keep_size_after) {
1302940b44dSPeter Avalos 		// This needs to be done conditionally, because if we got
1312940b44dSPeter Avalos 		// only little new input, there may be too little input
1322940b44dSPeter Avalos 		// to do any encoding yet.
1332940b44dSPeter Avalos 		coder->mf.read_limit = coder->mf.write_pos
1342940b44dSPeter Avalos 				- coder->mf.keep_size_after;
1352940b44dSPeter Avalos 	}
1362940b44dSPeter Avalos 
1372940b44dSPeter Avalos 	// Restart the match finder after finished LZMA_SYNC_FLUSH.
1382940b44dSPeter Avalos 	if (coder->mf.pending > 0
1392940b44dSPeter Avalos 			&& coder->mf.read_pos < coder->mf.read_limit) {
1402940b44dSPeter Avalos 		// Match finder may update coder->pending and expects it to
1412940b44dSPeter Avalos 		// start from zero, so use a temporary variable.
14215ab8c86SJohn Marino 		const uint32_t pending = coder->mf.pending;
1432940b44dSPeter Avalos 		coder->mf.pending = 0;
1442940b44dSPeter Avalos 
1452940b44dSPeter Avalos 		// Rewind read_pos so that the match finder can hash
1462940b44dSPeter Avalos 		// the pending bytes.
1472940b44dSPeter Avalos 		assert(coder->mf.read_pos >= pending);
1482940b44dSPeter Avalos 		coder->mf.read_pos -= pending;
1492940b44dSPeter Avalos 
1502940b44dSPeter Avalos 		// Call the skip function directly instead of using
1512940b44dSPeter Avalos 		// mf_skip(), since we don't want to touch mf->read_ahead.
1522940b44dSPeter Avalos 		coder->mf.skip(&coder->mf, pending);
1532940b44dSPeter Avalos 	}
1542940b44dSPeter Avalos 
1552940b44dSPeter Avalos 	return ret;
1562940b44dSPeter Avalos }
1572940b44dSPeter Avalos 
1582940b44dSPeter Avalos 
1592940b44dSPeter Avalos static lzma_ret
lz_encode(void * coder_ptr,const lzma_allocator * allocator,const uint8_t * restrict in,size_t * restrict in_pos,size_t in_size,uint8_t * restrict out,size_t * restrict out_pos,size_t out_size,lzma_action action)160*46a2189dSzrj lz_encode(void *coder_ptr, const lzma_allocator *allocator,
1612940b44dSPeter Avalos 		const uint8_t *restrict in, size_t *restrict in_pos,
1622940b44dSPeter Avalos 		size_t in_size,
1632940b44dSPeter Avalos 		uint8_t *restrict out, size_t *restrict out_pos,
1642940b44dSPeter Avalos 		size_t out_size, lzma_action action)
1652940b44dSPeter Avalos {
166*46a2189dSzrj 	lzma_coder *coder = coder_ptr;
167*46a2189dSzrj 
1682940b44dSPeter Avalos 	while (*out_pos < out_size
1692940b44dSPeter Avalos 			&& (*in_pos < in_size || action != LZMA_RUN)) {
1702940b44dSPeter Avalos 		// Read more data to coder->mf.buffer if needed.
1712940b44dSPeter Avalos 		if (coder->mf.action == LZMA_RUN && coder->mf.read_pos
1722940b44dSPeter Avalos 				>= coder->mf.read_limit)
1732940b44dSPeter Avalos 			return_if_error(fill_window(coder, allocator,
1742940b44dSPeter Avalos 					in, in_pos, in_size, action));
1752940b44dSPeter Avalos 
1762940b44dSPeter Avalos 		// Encode
1772940b44dSPeter Avalos 		const lzma_ret ret = coder->lz.code(coder->lz.coder,
1782940b44dSPeter Avalos 				&coder->mf, out, out_pos, out_size);
1792940b44dSPeter Avalos 		if (ret != LZMA_OK) {
1802940b44dSPeter Avalos 			// Setting this to LZMA_RUN for cases when we are
1812940b44dSPeter Avalos 			// flushing. It doesn't matter when finishing or if
1822940b44dSPeter Avalos 			// an error occurred.
1832940b44dSPeter Avalos 			coder->mf.action = LZMA_RUN;
1842940b44dSPeter Avalos 			return ret;
1852940b44dSPeter Avalos 		}
1862940b44dSPeter Avalos 	}
1872940b44dSPeter Avalos 
1882940b44dSPeter Avalos 	return LZMA_OK;
1892940b44dSPeter Avalos }
1902940b44dSPeter Avalos 
1912940b44dSPeter Avalos 
1922940b44dSPeter Avalos static bool
lz_encoder_prepare(lzma_mf * mf,const lzma_allocator * allocator,const lzma_lz_options * lz_options)19315ab8c86SJohn Marino lz_encoder_prepare(lzma_mf *mf, const lzma_allocator *allocator,
1942940b44dSPeter Avalos 		const lzma_lz_options *lz_options)
1952940b44dSPeter Avalos {
1962940b44dSPeter Avalos 	// For now, the dictionary size is limited to 1.5 GiB. This may grow
1972940b44dSPeter Avalos 	// in the future if needed, but it needs a little more work than just
1982940b44dSPeter Avalos 	// changing this check.
1992940b44dSPeter Avalos 	if (lz_options->dict_size < LZMA_DICT_SIZE_MIN
2002940b44dSPeter Avalos 			|| lz_options->dict_size
2012940b44dSPeter Avalos 				> (UINT32_C(1) << 30) + (UINT32_C(1) << 29)
2022940b44dSPeter Avalos 			|| lz_options->nice_len > lz_options->match_len_max)
2032940b44dSPeter Avalos 		return true;
2042940b44dSPeter Avalos 
2052940b44dSPeter Avalos 	mf->keep_size_before = lz_options->before_size + lz_options->dict_size;
2062940b44dSPeter Avalos 
2072940b44dSPeter Avalos 	mf->keep_size_after = lz_options->after_size
2082940b44dSPeter Avalos 			+ lz_options->match_len_max;
2092940b44dSPeter Avalos 
2102940b44dSPeter Avalos 	// To avoid constant memmove()s, allocate some extra space. Since
2112940b44dSPeter Avalos 	// memmove()s become more expensive when the size of the buffer
2122940b44dSPeter Avalos 	// increases, we reserve more space when a large dictionary is
2132940b44dSPeter Avalos 	// used to make the memmove() calls rarer.
2142940b44dSPeter Avalos 	//
2152940b44dSPeter Avalos 	// This works with dictionaries up to about 3 GiB. If bigger
2162940b44dSPeter Avalos 	// dictionary is wanted, some extra work is needed:
2172940b44dSPeter Avalos 	//   - Several variables in lzma_mf have to be changed from uint32_t
2182940b44dSPeter Avalos 	//     to size_t.
2192940b44dSPeter Avalos 	//   - Memory usage calculation needs something too, e.g. use uint64_t
2202940b44dSPeter Avalos 	//     for mf->size.
2212940b44dSPeter Avalos 	uint32_t reserve = lz_options->dict_size / 2;
2222940b44dSPeter Avalos 	if (reserve > (UINT32_C(1) << 30))
2232940b44dSPeter Avalos 		reserve /= 2;
2242940b44dSPeter Avalos 
2252940b44dSPeter Avalos 	reserve += (lz_options->before_size + lz_options->match_len_max
2262940b44dSPeter Avalos 			+ lz_options->after_size) / 2 + (UINT32_C(1) << 19);
2272940b44dSPeter Avalos 
2282940b44dSPeter Avalos 	const uint32_t old_size = mf->size;
2292940b44dSPeter Avalos 	mf->size = mf->keep_size_before + reserve + mf->keep_size_after;
2302940b44dSPeter Avalos 
2312940b44dSPeter Avalos 	// Deallocate the old history buffer if it exists but has different
2322940b44dSPeter Avalos 	// size than what is needed now.
2332940b44dSPeter Avalos 	if (mf->buffer != NULL && old_size != mf->size) {
2342940b44dSPeter Avalos 		lzma_free(mf->buffer, allocator);
2352940b44dSPeter Avalos 		mf->buffer = NULL;
2362940b44dSPeter Avalos 	}
2372940b44dSPeter Avalos 
2382940b44dSPeter Avalos 	// Match finder options
2392940b44dSPeter Avalos 	mf->match_len_max = lz_options->match_len_max;
2402940b44dSPeter Avalos 	mf->nice_len = lz_options->nice_len;
2412940b44dSPeter Avalos 
2422940b44dSPeter Avalos 	// cyclic_size has to stay smaller than 2 Gi. Note that this doesn't
2432940b44dSPeter Avalos 	// mean limiting dictionary size to less than 2 GiB. With a match
2442940b44dSPeter Avalos 	// finder that uses multibyte resolution (hashes start at e.g. every
2452940b44dSPeter Avalos 	// fourth byte), cyclic_size would stay below 2 Gi even when
2462940b44dSPeter Avalos 	// dictionary size is greater than 2 GiB.
2472940b44dSPeter Avalos 	//
2482940b44dSPeter Avalos 	// It would be possible to allow cyclic_size >= 2 Gi, but then we
2492940b44dSPeter Avalos 	// would need to be careful to use 64-bit types in various places
2502940b44dSPeter Avalos 	// (size_t could do since we would need bigger than 32-bit address
2512940b44dSPeter Avalos 	// space anyway). It would also require either zeroing a multigigabyte
2522940b44dSPeter Avalos 	// buffer at initialization (waste of time and RAM) or allow
2532940b44dSPeter Avalos 	// normalization in lz_encoder_mf.c to access uninitialized
2542940b44dSPeter Avalos 	// memory to keep the code simpler. The current way is simple and
2552940b44dSPeter Avalos 	// still allows pretty big dictionaries, so I don't expect these
2562940b44dSPeter Avalos 	// limits to change.
2572940b44dSPeter Avalos 	mf->cyclic_size = lz_options->dict_size + 1;
2582940b44dSPeter Avalos 
2592940b44dSPeter Avalos 	// Validate the match finder ID and setup the function pointers.
2602940b44dSPeter Avalos 	switch (lz_options->match_finder) {
2612940b44dSPeter Avalos #ifdef HAVE_MF_HC3
2622940b44dSPeter Avalos 	case LZMA_MF_HC3:
2632940b44dSPeter Avalos 		mf->find = &lzma_mf_hc3_find;
2642940b44dSPeter Avalos 		mf->skip = &lzma_mf_hc3_skip;
2652940b44dSPeter Avalos 		break;
2662940b44dSPeter Avalos #endif
2672940b44dSPeter Avalos #ifdef HAVE_MF_HC4
2682940b44dSPeter Avalos 	case LZMA_MF_HC4:
2692940b44dSPeter Avalos 		mf->find = &lzma_mf_hc4_find;
2702940b44dSPeter Avalos 		mf->skip = &lzma_mf_hc4_skip;
2712940b44dSPeter Avalos 		break;
2722940b44dSPeter Avalos #endif
2732940b44dSPeter Avalos #ifdef HAVE_MF_BT2
2742940b44dSPeter Avalos 	case LZMA_MF_BT2:
2752940b44dSPeter Avalos 		mf->find = &lzma_mf_bt2_find;
2762940b44dSPeter Avalos 		mf->skip = &lzma_mf_bt2_skip;
2772940b44dSPeter Avalos 		break;
2782940b44dSPeter Avalos #endif
2792940b44dSPeter Avalos #ifdef HAVE_MF_BT3
2802940b44dSPeter Avalos 	case LZMA_MF_BT3:
2812940b44dSPeter Avalos 		mf->find = &lzma_mf_bt3_find;
2822940b44dSPeter Avalos 		mf->skip = &lzma_mf_bt3_skip;
2832940b44dSPeter Avalos 		break;
2842940b44dSPeter Avalos #endif
2852940b44dSPeter Avalos #ifdef HAVE_MF_BT4
2862940b44dSPeter Avalos 	case LZMA_MF_BT4:
2872940b44dSPeter Avalos 		mf->find = &lzma_mf_bt4_find;
2882940b44dSPeter Avalos 		mf->skip = &lzma_mf_bt4_skip;
2892940b44dSPeter Avalos 		break;
2902940b44dSPeter Avalos #endif
2912940b44dSPeter Avalos 
2922940b44dSPeter Avalos 	default:
2932940b44dSPeter Avalos 		return true;
2942940b44dSPeter Avalos 	}
2952940b44dSPeter Avalos 
2962940b44dSPeter Avalos 	// Calculate the sizes of mf->hash and mf->son and check that
2972940b44dSPeter Avalos 	// nice_len is big enough for the selected match finder.
2982940b44dSPeter Avalos 	const uint32_t hash_bytes = lz_options->match_finder & 0x0F;
2992940b44dSPeter Avalos 	if (hash_bytes > mf->nice_len)
3002940b44dSPeter Avalos 		return true;
3012940b44dSPeter Avalos 
3022940b44dSPeter Avalos 	const bool is_bt = (lz_options->match_finder & 0x10) != 0;
3032940b44dSPeter Avalos 	uint32_t hs;
3042940b44dSPeter Avalos 
3052940b44dSPeter Avalos 	if (hash_bytes == 2) {
3062940b44dSPeter Avalos 		hs = 0xFFFF;
3072940b44dSPeter Avalos 	} else {
3082940b44dSPeter Avalos 		// Round dictionary size up to the next 2^n - 1 so it can
3092940b44dSPeter Avalos 		// be used as a hash mask.
3102940b44dSPeter Avalos 		hs = lz_options->dict_size - 1;
3112940b44dSPeter Avalos 		hs |= hs >> 1;
3122940b44dSPeter Avalos 		hs |= hs >> 2;
3132940b44dSPeter Avalos 		hs |= hs >> 4;
3142940b44dSPeter Avalos 		hs |= hs >> 8;
3152940b44dSPeter Avalos 		hs >>= 1;
3162940b44dSPeter Avalos 		hs |= 0xFFFF;
3172940b44dSPeter Avalos 
3182940b44dSPeter Avalos 		if (hs > (UINT32_C(1) << 24)) {
3192940b44dSPeter Avalos 			if (hash_bytes == 3)
3202940b44dSPeter Avalos 				hs = (UINT32_C(1) << 24) - 1;
3212940b44dSPeter Avalos 			else
3222940b44dSPeter Avalos 				hs >>= 1;
3232940b44dSPeter Avalos 		}
3242940b44dSPeter Avalos 	}
3252940b44dSPeter Avalos 
3262940b44dSPeter Avalos 	mf->hash_mask = hs;
3272940b44dSPeter Avalos 
3282940b44dSPeter Avalos 	++hs;
3292940b44dSPeter Avalos 	if (hash_bytes > 2)
3302940b44dSPeter Avalos 		hs += HASH_2_SIZE;
3312940b44dSPeter Avalos 	if (hash_bytes > 3)
3322940b44dSPeter Avalos 		hs += HASH_3_SIZE;
3332940b44dSPeter Avalos /*
3342940b44dSPeter Avalos 	No match finder uses this at the moment.
3352940b44dSPeter Avalos 	if (mf->hash_bytes > 4)
3362940b44dSPeter Avalos 		hs += HASH_4_SIZE;
3372940b44dSPeter Avalos */
3382940b44dSPeter Avalos 
33915ab8c86SJohn Marino 	const uint32_t old_hash_count = mf->hash_count;
34015ab8c86SJohn Marino 	const uint32_t old_sons_count = mf->sons_count;
34115ab8c86SJohn Marino 	mf->hash_count = hs;
3422940b44dSPeter Avalos 	mf->sons_count = mf->cyclic_size;
3432940b44dSPeter Avalos 	if (is_bt)
3442940b44dSPeter Avalos 		mf->sons_count *= 2;
3452940b44dSPeter Avalos 
3462940b44dSPeter Avalos 	// Deallocate the old hash array if it exists and has different size
3472940b44dSPeter Avalos 	// than what is needed now.
34815ab8c86SJohn Marino 	if (old_hash_count != mf->hash_count
34915ab8c86SJohn Marino 			|| old_sons_count != mf->sons_count) {
3502940b44dSPeter Avalos 		lzma_free(mf->hash, allocator);
3512940b44dSPeter Avalos 		mf->hash = NULL;
35215ab8c86SJohn Marino 
35315ab8c86SJohn Marino 		lzma_free(mf->son, allocator);
35415ab8c86SJohn Marino 		mf->son = NULL;
3552940b44dSPeter Avalos 	}
3562940b44dSPeter Avalos 
3572940b44dSPeter Avalos 	// Maximum number of match finder cycles
3582940b44dSPeter Avalos 	mf->depth = lz_options->depth;
3592940b44dSPeter Avalos 	if (mf->depth == 0) {
3602940b44dSPeter Avalos 		if (is_bt)
3612940b44dSPeter Avalos 			mf->depth = 16 + mf->nice_len / 2;
3622940b44dSPeter Avalos 		else
3632940b44dSPeter Avalos 			mf->depth = 4 + mf->nice_len / 4;
3642940b44dSPeter Avalos 	}
3652940b44dSPeter Avalos 
3662940b44dSPeter Avalos 	return false;
3672940b44dSPeter Avalos }
3682940b44dSPeter Avalos 
3692940b44dSPeter Avalos 
3702940b44dSPeter Avalos static bool
lz_encoder_init(lzma_mf * mf,const lzma_allocator * allocator,const lzma_lz_options * lz_options)37115ab8c86SJohn Marino lz_encoder_init(lzma_mf *mf, const lzma_allocator *allocator,
3722940b44dSPeter Avalos 		const lzma_lz_options *lz_options)
3732940b44dSPeter Avalos {
3742940b44dSPeter Avalos 	// Allocate the history buffer.
3752940b44dSPeter Avalos 	if (mf->buffer == NULL) {
37615ab8c86SJohn Marino 		// lzma_memcmplen() is used for the dictionary buffer
37715ab8c86SJohn Marino 		// so we need to allocate a few extra bytes to prevent
37815ab8c86SJohn Marino 		// it from reading past the end of the buffer.
37915ab8c86SJohn Marino 		mf->buffer = lzma_alloc(mf->size + LZMA_MEMCMPLEN_EXTRA,
38015ab8c86SJohn Marino 				allocator);
3812940b44dSPeter Avalos 		if (mf->buffer == NULL)
3822940b44dSPeter Avalos 			return true;
38315ab8c86SJohn Marino 
38415ab8c86SJohn Marino 		// Keep Valgrind happy with lzma_memcmplen() and initialize
38515ab8c86SJohn Marino 		// the extra bytes whose value may get read but which will
38615ab8c86SJohn Marino 		// effectively get ignored.
38715ab8c86SJohn Marino 		memzero(mf->buffer + mf->size, LZMA_MEMCMPLEN_EXTRA);
3882940b44dSPeter Avalos 	}
3892940b44dSPeter Avalos 
3902940b44dSPeter Avalos 	// Use cyclic_size as initial mf->offset. This allows
3912940b44dSPeter Avalos 	// avoiding a few branches in the match finders. The downside is
3922940b44dSPeter Avalos 	// that match finder needs to be normalized more often, which may
3932940b44dSPeter Avalos 	// hurt performance with huge dictionaries.
3942940b44dSPeter Avalos 	mf->offset = mf->cyclic_size;
3952940b44dSPeter Avalos 	mf->read_pos = 0;
3962940b44dSPeter Avalos 	mf->read_ahead = 0;
3972940b44dSPeter Avalos 	mf->read_limit = 0;
3982940b44dSPeter Avalos 	mf->write_pos = 0;
3992940b44dSPeter Avalos 	mf->pending = 0;
4002940b44dSPeter Avalos 
4012940b44dSPeter Avalos #if UINT32_MAX >= SIZE_MAX / 4
4022940b44dSPeter Avalos 	// Check for integer overflow. (Huge dictionaries are not
4032940b44dSPeter Avalos 	// possible on 32-bit CPU.)
40415ab8c86SJohn Marino 	if (mf->hash_count > SIZE_MAX / sizeof(uint32_t)
40515ab8c86SJohn Marino 			|| mf->sons_count > SIZE_MAX / sizeof(uint32_t))
4062940b44dSPeter Avalos 		return true;
4072940b44dSPeter Avalos #endif
4082940b44dSPeter Avalos 
40915ab8c86SJohn Marino 	// Allocate and initialize the hash table. Since EMPTY_HASH_VALUE
41015ab8c86SJohn Marino 	// is zero, we can use lzma_alloc_zero() or memzero() for mf->hash.
41115ab8c86SJohn Marino 	//
41215ab8c86SJohn Marino 	// We don't need to initialize mf->son, but not doing that may
41315ab8c86SJohn Marino 	// make Valgrind complain in normalization (see normalize() in
41415ab8c86SJohn Marino 	// lz_encoder_mf.c). Skipping the initialization is *very* good
41515ab8c86SJohn Marino 	// when big dictionary is used but only small amount of data gets
41615ab8c86SJohn Marino 	// actually compressed: most of the mf->son won't get actually
41715ab8c86SJohn Marino 	// allocated by the kernel, so we avoid wasting RAM and improve
41815ab8c86SJohn Marino 	// initialization speed a lot.
4192940b44dSPeter Avalos 	if (mf->hash == NULL) {
42015ab8c86SJohn Marino 		mf->hash = lzma_alloc_zero(mf->hash_count * sizeof(uint32_t),
4212940b44dSPeter Avalos 				allocator);
42215ab8c86SJohn Marino 		mf->son = lzma_alloc(mf->sons_count * sizeof(uint32_t),
42315ab8c86SJohn Marino 				allocator);
42415ab8c86SJohn Marino 
42515ab8c86SJohn Marino 		if (mf->hash == NULL || mf->son == NULL) {
42615ab8c86SJohn Marino 			lzma_free(mf->hash, allocator);
42715ab8c86SJohn Marino 			mf->hash = NULL;
42815ab8c86SJohn Marino 
42915ab8c86SJohn Marino 			lzma_free(mf->son, allocator);
43015ab8c86SJohn Marino 			mf->son = NULL;
43115ab8c86SJohn Marino 
4322940b44dSPeter Avalos 			return true;
4332940b44dSPeter Avalos 		}
43415ab8c86SJohn Marino 	} else {
4352940b44dSPeter Avalos /*
43615ab8c86SJohn Marino 		for (uint32_t i = 0; i < mf->hash_count; ++i)
4372940b44dSPeter Avalos 			mf->hash[i] = EMPTY_HASH_VALUE;
4382940b44dSPeter Avalos */
43915ab8c86SJohn Marino 		memzero(mf->hash, mf->hash_count * sizeof(uint32_t));
44015ab8c86SJohn Marino 	}
4412940b44dSPeter Avalos 
44215ab8c86SJohn Marino 	mf->cyclic_pos = 0;
4432940b44dSPeter Avalos 
4442940b44dSPeter Avalos 	// Handle preset dictionary.
4452940b44dSPeter Avalos 	if (lz_options->preset_dict != NULL
4462940b44dSPeter Avalos 			&& lz_options->preset_dict_size > 0) {
4472940b44dSPeter Avalos 		// If the preset dictionary is bigger than the actual
4482940b44dSPeter Avalos 		// dictionary, use only the tail.
4492940b44dSPeter Avalos 		mf->write_pos = my_min(lz_options->preset_dict_size, mf->size);
4502940b44dSPeter Avalos 		memcpy(mf->buffer, lz_options->preset_dict
4512940b44dSPeter Avalos 				+ lz_options->preset_dict_size - mf->write_pos,
4522940b44dSPeter Avalos 				mf->write_pos);
4532940b44dSPeter Avalos 		mf->action = LZMA_SYNC_FLUSH;
4542940b44dSPeter Avalos 		mf->skip(mf, mf->write_pos);
4552940b44dSPeter Avalos 	}
4562940b44dSPeter Avalos 
4572940b44dSPeter Avalos 	mf->action = LZMA_RUN;
4582940b44dSPeter Avalos 
4592940b44dSPeter Avalos 	return false;
4602940b44dSPeter Avalos }
4612940b44dSPeter Avalos 
4622940b44dSPeter Avalos 
4632940b44dSPeter Avalos extern uint64_t
lzma_lz_encoder_memusage(const lzma_lz_options * lz_options)4642940b44dSPeter Avalos lzma_lz_encoder_memusage(const lzma_lz_options *lz_options)
4652940b44dSPeter Avalos {
4662940b44dSPeter Avalos 	// Old buffers must not exist when calling lz_encoder_prepare().
4672940b44dSPeter Avalos 	lzma_mf mf = {
4682940b44dSPeter Avalos 		.buffer = NULL,
4692940b44dSPeter Avalos 		.hash = NULL,
47015ab8c86SJohn Marino 		.son = NULL,
47115ab8c86SJohn Marino 		.hash_count = 0,
4722940b44dSPeter Avalos 		.sons_count = 0,
4732940b44dSPeter Avalos 	};
4742940b44dSPeter Avalos 
4752940b44dSPeter Avalos 	// Setup the size information into mf.
4762940b44dSPeter Avalos 	if (lz_encoder_prepare(&mf, NULL, lz_options))
4772940b44dSPeter Avalos 		return UINT64_MAX;
4782940b44dSPeter Avalos 
4792940b44dSPeter Avalos 	// Calculate the memory usage.
48015ab8c86SJohn Marino 	return ((uint64_t)(mf.hash_count) + mf.sons_count) * sizeof(uint32_t)
48115ab8c86SJohn Marino 			+ mf.size + sizeof(lzma_coder);
4822940b44dSPeter Avalos }
4832940b44dSPeter Avalos 
4842940b44dSPeter Avalos 
4852940b44dSPeter Avalos static void
lz_encoder_end(void * coder_ptr,const lzma_allocator * allocator)486*46a2189dSzrj lz_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
4872940b44dSPeter Avalos {
488*46a2189dSzrj 	lzma_coder *coder = coder_ptr;
489*46a2189dSzrj 
4902940b44dSPeter Avalos 	lzma_next_end(&coder->next, allocator);
4912940b44dSPeter Avalos 
49215ab8c86SJohn Marino 	lzma_free(coder->mf.son, allocator);
4932940b44dSPeter Avalos 	lzma_free(coder->mf.hash, allocator);
4942940b44dSPeter Avalos 	lzma_free(coder->mf.buffer, allocator);
4952940b44dSPeter Avalos 
4962940b44dSPeter Avalos 	if (coder->lz.end != NULL)
4972940b44dSPeter Avalos 		coder->lz.end(coder->lz.coder, allocator);
4982940b44dSPeter Avalos 	else
4992940b44dSPeter Avalos 		lzma_free(coder->lz.coder, allocator);
5002940b44dSPeter Avalos 
5012940b44dSPeter Avalos 	lzma_free(coder, allocator);
5022940b44dSPeter Avalos 	return;
5032940b44dSPeter Avalos }
5042940b44dSPeter Avalos 
5052940b44dSPeter Avalos 
5062940b44dSPeter Avalos static lzma_ret
lz_encoder_update(void * coder_ptr,const lzma_allocator * allocator,const lzma_filter * filters_null lzma_attribute ((__unused__)),const lzma_filter * reversed_filters)507*46a2189dSzrj lz_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
508114db65bSPeter Avalos 		const lzma_filter *filters_null lzma_attribute((__unused__)),
5092940b44dSPeter Avalos 		const lzma_filter *reversed_filters)
5102940b44dSPeter Avalos {
511*46a2189dSzrj 	lzma_coder *coder = coder_ptr;
512*46a2189dSzrj 
5132940b44dSPeter Avalos 	if (coder->lz.options_update == NULL)
5142940b44dSPeter Avalos 		return LZMA_PROG_ERROR;
5152940b44dSPeter Avalos 
5162940b44dSPeter Avalos 	return_if_error(coder->lz.options_update(
5172940b44dSPeter Avalos 			coder->lz.coder, reversed_filters));
5182940b44dSPeter Avalos 
5192940b44dSPeter Avalos 	return lzma_next_filter_update(
5202940b44dSPeter Avalos 			&coder->next, allocator, reversed_filters + 1);
5212940b44dSPeter Avalos }
5222940b44dSPeter Avalos 
5232940b44dSPeter Avalos 
5242940b44dSPeter Avalos extern lzma_ret
lzma_lz_encoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_filter_info * filters,lzma_ret (* lz_init)(lzma_lz_encoder * lz,const lzma_allocator * allocator,const void * options,lzma_lz_options * lz_options))52515ab8c86SJohn Marino lzma_lz_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
5262940b44dSPeter Avalos 		const lzma_filter_info *filters,
5272940b44dSPeter Avalos 		lzma_ret (*lz_init)(lzma_lz_encoder *lz,
52815ab8c86SJohn Marino 			const lzma_allocator *allocator, const void *options,
5292940b44dSPeter Avalos 			lzma_lz_options *lz_options))
5302940b44dSPeter Avalos {
5312940b44dSPeter Avalos #ifdef HAVE_SMALL
5322940b44dSPeter Avalos 	// We need that the CRC32 table has been initialized.
5332940b44dSPeter Avalos 	lzma_crc32_init();
5342940b44dSPeter Avalos #endif
5352940b44dSPeter Avalos 
5362940b44dSPeter Avalos 	// Allocate and initialize the base data structure.
537*46a2189dSzrj 	lzma_coder *coder = next->coder;
538*46a2189dSzrj 	if (coder == NULL) {
539*46a2189dSzrj 		coder = lzma_alloc(sizeof(lzma_coder), allocator);
540*46a2189dSzrj 		if (coder == NULL)
5412940b44dSPeter Avalos 			return LZMA_MEM_ERROR;
5422940b44dSPeter Avalos 
543*46a2189dSzrj 		next->coder = coder;
5442940b44dSPeter Avalos 		next->code = &lz_encode;
5452940b44dSPeter Avalos 		next->end = &lz_encoder_end;
5462940b44dSPeter Avalos 		next->update = &lz_encoder_update;
5472940b44dSPeter Avalos 
548*46a2189dSzrj 		coder->lz.coder = NULL;
549*46a2189dSzrj 		coder->lz.code = NULL;
550*46a2189dSzrj 		coder->lz.end = NULL;
5512940b44dSPeter Avalos 
552*46a2189dSzrj 		// mf.size is initialized to silence Valgrind
553*46a2189dSzrj 		// when used on optimized binaries (GCC may reorder
554*46a2189dSzrj 		// code in a way that Valgrind gets unhappy).
555*46a2189dSzrj 		coder->mf.buffer = NULL;
556*46a2189dSzrj 		coder->mf.size = 0;
557*46a2189dSzrj 		coder->mf.hash = NULL;
558*46a2189dSzrj 		coder->mf.son = NULL;
559*46a2189dSzrj 		coder->mf.hash_count = 0;
560*46a2189dSzrj 		coder->mf.sons_count = 0;
5612940b44dSPeter Avalos 
562*46a2189dSzrj 		coder->next = LZMA_NEXT_CODER_INIT;
5632940b44dSPeter Avalos 	}
5642940b44dSPeter Avalos 
5652940b44dSPeter Avalos 	// Initialize the LZ-based encoder.
5662940b44dSPeter Avalos 	lzma_lz_options lz_options;
567*46a2189dSzrj 	return_if_error(lz_init(&coder->lz, allocator,
5682940b44dSPeter Avalos 			filters[0].options, &lz_options));
5692940b44dSPeter Avalos 
570*46a2189dSzrj 	// Setup the size information into coder->mf and deallocate
5712940b44dSPeter Avalos 	// old buffers if they have wrong size.
572*46a2189dSzrj 	if (lz_encoder_prepare(&coder->mf, allocator, &lz_options))
5732940b44dSPeter Avalos 		return LZMA_OPTIONS_ERROR;
5742940b44dSPeter Avalos 
5752940b44dSPeter Avalos 	// Allocate new buffers if needed, and do the rest of
5762940b44dSPeter Avalos 	// the initialization.
577*46a2189dSzrj 	if (lz_encoder_init(&coder->mf, allocator, &lz_options))
5782940b44dSPeter Avalos 		return LZMA_MEM_ERROR;
5792940b44dSPeter Avalos 
5802940b44dSPeter Avalos 	// Initialize the next filter in the chain, if any.
581*46a2189dSzrj 	return lzma_next_filter_init(&coder->next, allocator, filters + 1);
5822940b44dSPeter Avalos }
5832940b44dSPeter Avalos 
5842940b44dSPeter Avalos 
5852940b44dSPeter Avalos extern LZMA_API(lzma_bool)
lzma_mf_is_supported(lzma_match_finder mf)5862940b44dSPeter Avalos lzma_mf_is_supported(lzma_match_finder mf)
5872940b44dSPeter Avalos {
5882940b44dSPeter Avalos 	bool ret = false;
5892940b44dSPeter Avalos 
5902940b44dSPeter Avalos #ifdef HAVE_MF_HC3
5912940b44dSPeter Avalos 	if (mf == LZMA_MF_HC3)
5922940b44dSPeter Avalos 		ret = true;
5932940b44dSPeter Avalos #endif
5942940b44dSPeter Avalos 
5952940b44dSPeter Avalos #ifdef HAVE_MF_HC4
5962940b44dSPeter Avalos 	if (mf == LZMA_MF_HC4)
5972940b44dSPeter Avalos 		ret = true;
5982940b44dSPeter Avalos #endif
5992940b44dSPeter Avalos 
6002940b44dSPeter Avalos #ifdef HAVE_MF_BT2
6012940b44dSPeter Avalos 	if (mf == LZMA_MF_BT2)
6022940b44dSPeter Avalos 		ret = true;
6032940b44dSPeter Avalos #endif
6042940b44dSPeter Avalos 
6052940b44dSPeter Avalos #ifdef HAVE_MF_BT3
6062940b44dSPeter Avalos 	if (mf == LZMA_MF_BT3)
6072940b44dSPeter Avalos 		ret = true;
6082940b44dSPeter Avalos #endif
6092940b44dSPeter Avalos 
6102940b44dSPeter Avalos #ifdef HAVE_MF_BT4
6112940b44dSPeter Avalos 	if (mf == LZMA_MF_BT4)
6122940b44dSPeter Avalos 		ret = true;
6132940b44dSPeter Avalos #endif
6142940b44dSPeter Avalos 
6152940b44dSPeter Avalos 	return ret;
6162940b44dSPeter Avalos }
617