1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: expandtab:ts=8:sw=4:softtabstop=4:
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       lz_encoder.c
6 /// \brief      LZ in window
7 ///
8 //  Authors:    Igor Pavlov
9 //              Lasse Collin
10 //
11 //  This file has been put into the public domain.
12 //  You can do whatever you want with this file.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15 
16 #include "lz_encoder.h"
17 #include "lz_encoder_hash.h"
18 #include "check.h"
19 
20 
21 struct lzma_coder_s {
22 	/// LZ-based encoder e.g. LZMA
23 	lzma_lz_encoder lz;
24 
25 	/// History buffer and match finder
26 	lzma_mf mf;
27 
28 	/// Next coder in the chain
29 	lzma_next_coder next;
30 };
31 
32 
33 /// \brief      Moves the data in the input window to free space for new data
34 ///
35 /// mf->buffer is a sliding input window, which keeps mf->keep_size_before
36 /// bytes of input history available all the time. Now and then we need to
37 /// "slide" the buffer to make space for the new data to the end of the
38 /// buffer. At the same time, data older than keep_size_before is dropped.
39 ///
40 static void
move_window(lzma_mf * mf)41 move_window(lzma_mf *mf)
42 {
43 	// Align the move to a multiple of 16 bytes. Some LZ-based encoders
44 	// like LZMA use the lowest bits of mf->read_pos to know the
45 	// alignment of the uncompressed data. We also get better speed
46 	// for memmove() with aligned buffers.
47 	assert(mf->read_pos > mf->keep_size_before);
48 	const uint32_t move_offset
49 		= (mf->read_pos - mf->keep_size_before) & ~UINT32_C(15);
50 
51 	assert(mf->write_pos > move_offset);
52 	const size_t move_size = mf->write_pos - move_offset;
53 
54 	assert(move_offset + move_size <= mf->size);
55 
56 	memmove(mf->buffer, mf->buffer + move_offset, move_size);
57 
58 	mf->offset += move_offset;
59 	mf->read_pos -= move_offset;
60 	mf->read_limit -= move_offset;
61 	mf->write_pos -= move_offset;
62 
63 	return;
64 }
65 
66 
67 /// \brief      Tries to fill the input window (mf->buffer)
68 ///
69 /// If we are the last encoder in the chain, our input data is in in[].
70 /// Otherwise we call the next filter in the chain to process in[] and
71 /// write its output to mf->buffer.
72 ///
73 /// This function must not be called once it has returned LZMA_STREAM_END.
74 ///
75 static lzma_ret
fill_window(lzma_coder * coder,lzma_allocator * allocator,const uint8_t * in,size_t * in_pos,size_t in_size,lzma_action action)76 fill_window(lzma_coder *coder, lzma_allocator *allocator, const uint8_t *in,
77 		size_t *in_pos, size_t in_size, lzma_action action)
78 {
79 	assert(coder->mf.read_pos <= coder->mf.write_pos);
80 
81 	// Move the sliding window if needed.
82 	if (coder->mf.read_pos >= coder->mf.size - coder->mf.keep_size_after)
83 		move_window(&coder->mf);
84 
85 	// Maybe this is ugly, but lzma_mf uses uint32_t for most things
86 	// (which I find cleanest), but we need size_t here when filling
87 	// the history window.
88 	size_t write_pos = coder->mf.write_pos;
89 	size_t in_used;
90 	lzma_ret ret;
91 	if (coder->next.code == NULL) {
92 		// Not using a filter, simply memcpy() as much as possible.
93 		in_used = lzma_bufcpy(in, in_pos, in_size, coder->mf.buffer,
94 				&write_pos, coder->mf.size);
95 
96 		ret = action != LZMA_RUN && *in_pos == in_size
97 				? LZMA_STREAM_END : LZMA_OK;
98 
99 	} else {
100 		const size_t in_start = *in_pos;
101 		ret = coder->next.code(coder->next.coder, allocator,
102 				in, in_pos, in_size,
103 				coder->mf.buffer, &write_pos,
104 				coder->mf.size, action);
105 		in_used = *in_pos - in_start;
106 	}
107 
108 	coder->mf.write_pos = write_pos;
109 
110 	// If end of stream has been reached or flushing completed, we allow
111 	// the encoder to process all the input (that is, read_pos is allowed
112 	// to reach write_pos). Otherwise we keep keep_size_after bytes
113 	// available as prebuffer.
114 	if (ret == LZMA_STREAM_END) {
115 		assert(*in_pos == in_size);
116 		ret = LZMA_OK;
117 		coder->mf.action = action;
118 		coder->mf.read_limit = coder->mf.write_pos;
119 
120 	} else if (coder->mf.write_pos > coder->mf.keep_size_after) {
121 		// This needs to be done conditionally, because if we got
122 		// only little new input, there may be too little input
123 		// to do any encoding yet.
124 		coder->mf.read_limit = coder->mf.write_pos
125 				- coder->mf.keep_size_after;
126 	}
127 
128 	// Restart the match finder after finished LZMA_SYNC_FLUSH.
129 	if (coder->mf.pending > 0
130 			&& coder->mf.read_pos < coder->mf.read_limit) {
131 		// Match finder may update coder->pending and expects it to
132 		// start from zero, so use a temporary variable.
133 		const size_t pending = coder->mf.pending;
134 		coder->mf.pending = 0;
135 
136 		// Rewind read_pos so that the match finder can hash
137 		// the pending bytes.
138 		assert(coder->mf.read_pos >= pending);
139 		coder->mf.read_pos -= pending;
140 
141 		// Call the skip function directly instead of using
142 		// mf_skip(), since we don't want to touch mf->read_ahead.
143 		coder->mf.skip(&coder->mf, pending);
144 	}
145 
146 	return ret;
147 }
148 
149 
150 static lzma_ret
lz_encode(lzma_coder * coder,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)151 lz_encode(lzma_coder *coder, lzma_allocator *allocator,
152 		const uint8_t *restrict in, size_t *restrict in_pos,
153 		size_t in_size,
154 		uint8_t *restrict out, size_t *restrict out_pos,
155 		size_t out_size, lzma_action action)
156 {
157 	while (*out_pos < out_size
158 			&& (*in_pos < in_size || action != LZMA_RUN)) {
159 		// Read more data to coder->mf.buffer if needed.
160 		if (coder->mf.action == LZMA_RUN && coder->mf.read_pos
161 				>= coder->mf.read_limit)
162 			return_if_error(fill_window(coder, allocator,
163 					in, in_pos, in_size, action));
164 
165 		// Encode
166 		const lzma_ret ret = coder->lz.code(coder->lz.coder,
167 				&coder->mf, out, out_pos, out_size);
168 		if (ret != LZMA_OK) {
169 			// Setting this to LZMA_RUN for cases when we are
170 			// flushing. It doesn't matter when finishing or if
171 			// an error occurred.
172 			coder->mf.action = LZMA_RUN;
173 			return ret;
174 		}
175 	}
176 
177 	return LZMA_OK;
178 }
179 
180 
181 static bool
lz_encoder_prepare(lzma_mf * mf,lzma_allocator * allocator,const lzma_lz_options * lz_options)182 lz_encoder_prepare(lzma_mf *mf, lzma_allocator *allocator,
183 		const lzma_lz_options *lz_options)
184 {
185 	// For now, the dictionary size is limited to 1.5 GiB. This may grow
186 	// in the future if needed, but it needs a little more work than just
187 	// changing this check.
188 	if (lz_options->dict_size < LZMA_DICT_SIZE_MIN
189 			|| lz_options->dict_size
190 				> (UINT32_C(1) << 30) + (UINT32_C(1) << 29)
191 			|| lz_options->nice_len > lz_options->match_len_max)
192 		return true;
193 
194 	mf->keep_size_before = lz_options->before_size + lz_options->dict_size;
195 
196 	mf->keep_size_after = lz_options->after_size
197 			+ lz_options->match_len_max;
198 
199 	// To avoid constant memmove()s, allocate some extra space. Since
200 	// memmove()s become more expensive when the size of the buffer
201 	// increases, we reserve more space when a large dictionary is
202 	// used to make the memmove() calls rarer.
203 	//
204 	// This works with dictionaries up to about 3 GiB. If bigger
205 	// dictionary is wanted, some extra work is needed:
206 	//   - Several variables in lzma_mf have to be changed from uint32_t
207 	//     to size_t.
208 	//   - Memory usage calculation needs something too, e.g. use uint64_t
209 	//     for mf->size.
210 	uint32_t reserve = lz_options->dict_size / 2;
211 	if (reserve > (UINT32_C(1) << 30))
212 		reserve /= 2;
213 
214 	reserve += (lz_options->before_size + lz_options->match_len_max
215 			+ lz_options->after_size) / 2 + (UINT32_C(1) << 19);
216 
217 	const uint32_t old_size = mf->size;
218 	mf->size = mf->keep_size_before + reserve + mf->keep_size_after;
219 
220 	// Deallocate the old history buffer if it exists but has different
221 	// size than what is needed now.
222 	if (mf->buffer != NULL && old_size != mf->size) {
223 		lzma_free(mf->buffer, allocator);
224 		mf->buffer = NULL;
225 	}
226 
227 	// Match finder options
228 	mf->match_len_max = lz_options->match_len_max;
229 	mf->nice_len = lz_options->nice_len;
230 
231 	// cyclic_size has to stay smaller than 2 Gi. Note that this doesn't
232 	// mean limitting dictionary size to less than 2 GiB. With a match
233 	// finder that uses multibyte resolution (hashes start at e.g. every
234 	// fourth byte), cyclic_size would stay below 2 Gi even when
235 	// dictionary size is greater than 2 GiB.
236 	//
237 	// It would be possible to allow cyclic_size >= 2 Gi, but then we
238 	// would need to be careful to use 64-bit types in various places
239 	// (size_t could do since we would need bigger than 32-bit address
240 	// space anyway). It would also require either zeroing a multigigabyte
241 	// buffer at initialization (waste of time and RAM) or allow
242 	// normalization in lz_encoder_mf.c to access uninitialized
243 	// memory to keep the code simpler. The current way is simple and
244 	// still allows pretty big dictionaries, so I don't expect these
245 	// limits to change.
246 	mf->cyclic_size = lz_options->dict_size + 1;
247 
248 	// Validate the match finder ID and setup the function pointers.
249 	switch (lz_options->match_finder) {
250 #ifdef HAVE_MF_HC3
251 	case LZMA_MF_HC3:
252 		mf->find = &lzma_mf_hc3_find;
253 		mf->skip = &lzma_mf_hc3_skip;
254 		break;
255 #endif
256 #ifdef HAVE_MF_HC4
257 	case LZMA_MF_HC4:
258 		mf->find = &lzma_mf_hc4_find;
259 		mf->skip = &lzma_mf_hc4_skip;
260 		break;
261 #endif
262 #ifdef HAVE_MF_BT2
263 	case LZMA_MF_BT2:
264 		mf->find = &lzma_mf_bt2_find;
265 		mf->skip = &lzma_mf_bt2_skip;
266 		break;
267 #endif
268 #ifdef HAVE_MF_BT3
269 	case LZMA_MF_BT3:
270 		mf->find = &lzma_mf_bt3_find;
271 		mf->skip = &lzma_mf_bt3_skip;
272 		break;
273 #endif
274 #ifdef HAVE_MF_BT4
275 	case LZMA_MF_BT4:
276 		mf->find = &lzma_mf_bt4_find;
277 		mf->skip = &lzma_mf_bt4_skip;
278 		break;
279 #endif
280 
281 	default:
282 		return true;
283 	}
284 
285 	// Calculate the sizes of mf->hash and mf->son and check that
286 	// nice_len is big enough for the selected match finder.
287 	const uint32_t hash_bytes = lz_options->match_finder & 0x0F;
288 	if (hash_bytes > mf->nice_len)
289 		return true;
290 
291 	const bool is_bt = (lz_options->match_finder & 0x10) != 0;
292 	uint32_t hs;
293 
294 	if (hash_bytes == 2) {
295 		hs = 0xFFFF;
296 	} else {
297 		// Round dictionary size up to the next 2^n - 1 so it can
298 		// be used as a hash mask.
299 		hs = lz_options->dict_size - 1;
300 		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 
361 static bool
lz_encoder_init(lzma_mf * mf,lzma_allocator * allocator,const lzma_lz_options * lz_options)362 lz_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 
440 extern uint64_t
lzma_lz_encoder_memusage(const lzma_lz_options * lz_options)441 lzma_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 
460 static void
lz_encoder_end(lzma_coder * coder,lzma_allocator * allocator)461 lz_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 
478 extern lzma_ret
lzma_lz_encoder_init(lzma_next_coder * next,lzma_allocator * allocator,const lzma_filter_info * filters,lzma_ret (* lz_init)(lzma_lz_encoder * lz,lzma_allocator * allocator,const void * options,lzma_lz_options * lz_options))479 lzma_lz_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
480 		const lzma_filter_info *filters,
481 		lzma_ret (*lz_init)(lzma_lz_encoder *lz,
482 			lzma_allocator *allocator, const void *options,
483 			lzma_lz_options *lz_options))
484 {
485 #ifdef HAVE_SMALL
486 	// We need that the CRC32 table has been initialized.
487 	lzma_crc32_init();
488 #endif
489 
490 	// Allocate and initialize the base data structure.
491 	if (next->coder == NULL) {
492 		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
493 		if (next->coder == NULL)
494 			return LZMA_MEM_ERROR;
495 
496 		next->code = &lz_encode;
497 		next->end = &lz_encoder_end;
498 
499 		next->coder->lz.coder = NULL;
500 		next->coder->lz.code = NULL;
501 		next->coder->lz.end = NULL;
502 
503 		next->coder->mf.buffer = NULL;
504 		next->coder->mf.hash = NULL;
505 
506 		next->coder->next = LZMA_NEXT_CODER_INIT;
507 	}
508 
509 	// Initialize the LZ-based encoder.
510 	lzma_lz_options lz_options;
511 	return_if_error(lz_init(&next->coder->lz, allocator,
512 			filters[0].options, &lz_options));
513 
514 	// Setup the size information into next->coder->mf and deallocate
515 	// old buffers if they have wrong size.
516 	if (lz_encoder_prepare(&next->coder->mf, allocator, &lz_options))
517 		return LZMA_OPTIONS_ERROR;
518 
519 	// Allocate new buffers if needed, and do the rest of
520 	// the initialization.
521 	if (lz_encoder_init(&next->coder->mf, allocator, &lz_options))
522 		return LZMA_MEM_ERROR;
523 
524 	// Initialize the next filter in the chain, if any.
525 	return lzma_next_filter_init(&next->coder->next, allocator,
526 			filters + 1);
527 }
528 
529 
530 extern LZMA_API(lzma_bool)
lzma_mf_is_supported(lzma_match_finder mf)531 lzma_mf_is_supported(lzma_match_finder mf)
532 {
533 	bool ret = false;
534 
535 #ifdef HAVE_MF_HC3
536 	if (mf == LZMA_MF_HC3)
537 		ret = true;
538 #endif
539 
540 #ifdef HAVE_MF_HC4
541 	if (mf == LZMA_MF_HC4)
542 		ret = true;
543 #endif
544 
545 #ifdef HAVE_MF_BT2
546 	if (mf == LZMA_MF_BT2)
547 		ret = true;
548 #endif
549 
550 #ifdef HAVE_MF_BT3
551 	if (mf == LZMA_MF_BT3)
552 		ret = true;
553 #endif
554 
555 #ifdef HAVE_MF_BT4
556 	if (mf == LZMA_MF_BT4)
557 		ret = true;
558 #endif
559 
560 	return ret;
561 }
562