1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief LZ in window
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007 Lasse Collin
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 ///////////////////////////////////////////////////////////////////////////////
21 #include "lz_encoder_private.h"
43 /// This is needed in two places so provide a macro.
44 #define get_cyclic_buffer_size(history_size) ((history_size) + 1)
47 /// Calculate certain match finder properties and validate the calculated
48 /// values. This is as its own function, because *num_items is needed to
49 /// calculate memory requirements in common/memory.c.
51 lzma_lz_encoder_hash_properties(lzma_match_finder match_finder,
52 uint32_t history_size, uint32_t *restrict hash_mask,
53 uint32_t *restrict hash_size_sum, uint32_t *restrict num_items)
55 uint32_t fix_hash_size;
58 switch (match_finder) {
61 fix_hash_size = LZMA_HC3_FIX_HASH_SIZE;
67 fix_hash_size = LZMA_HC4_FIX_HASH_SIZE;
73 fix_hash_size = LZMA_BT2_FIX_HASH_SIZE;
79 fix_hash_size = LZMA_BT3_FIX_HASH_SIZE;
85 fix_hash_size = LZMA_BT4_FIX_HASH_SIZE;
96 if (match_finder == LZMA_BT2) {
97 // NOTE: hash_mask is not used by the BT2 match finder,
98 // but it is initialized just in case.
99 hs = LZMA_BT2_HASH_SIZE;
104 hs = history_size - 1;
112 if (hs > (UINT32_C(1) << 24)) {
113 if (match_finder == LZMA_MF_HC4
114 || match_finder == LZMA_MF_BT4)
124 *hash_size_sum = hs + fix_hash_size;
126 *num_items = *hash_size_sum
127 + get_cyclic_buffer_size(history_size) * sons;
134 lzma_lz_encoder_reset(lzma_lz_encoder *lz, lzma_allocator *allocator,
135 bool (*process)(lzma_coder *coder, uint8_t *restrict out,
136 size_t *restrict out_pos, size_t out_size),
137 lzma_vli uncompressed_size,
138 size_t history_size, size_t additional_buffer_before,
139 size_t match_max_len, size_t additional_buffer_after,
140 lzma_match_finder match_finder, uint32_t match_finder_cycles,
141 const uint8_t *preset_dictionary,
142 size_t preset_dictionary_size)
144 // Set uncompressed size.
145 lz->uncompressed_size = uncompressed_size;
151 // Validate history size.
152 if (history_size < LZMA_DICTIONARY_SIZE_MIN
153 || history_size > LZMA_DICTIONARY_SIZE_MAX) {
154 lzma_lz_encoder_end(lz, allocator);
155 return LZMA_HEADER_ERROR;
158 assert(history_size <= MAX_VAL_FOR_NORMALIZE - 256);
159 assert(LZMA_DICTIONARY_SIZE_MAX <= MAX_VAL_FOR_NORMALIZE - 256);
161 // Calculate the size of the history buffer to allocate.
162 // TODO: Get a reason for magic constant of 256.
163 const size_t size_reserv = (history_size + additional_buffer_before
164 + match_max_len + additional_buffer_after) / 2 + 256;
166 lz->keep_size_before = history_size + additional_buffer_before;
167 lz->keep_size_after = match_max_len + additional_buffer_after;
169 const size_t buffer_size = lz->keep_size_before + lz->keep_size_after
172 // Allocate history buffer if its size has changed.
173 if (buffer_size != lz->size) {
174 lzma_free(lz->buffer, allocator);
175 lz->buffer = lzma_alloc(buffer_size, allocator);
176 if (lz->buffer == NULL) {
177 lzma_lz_encoder_end(lz, allocator);
178 return LZMA_MEM_ERROR;
182 // Allocation successful. Store the new size.
183 lz->size = buffer_size;
185 // Reset in window variables.
190 lz->stream_end_was_reached = false;
197 // Validate match_finder, set function pointers and a few match
198 // finder specific variables.
199 switch (match_finder) {
202 lz->get_matches = &lzma_hc3_get_matches;
203 lz->skip = &lzma_hc3_skip;
204 lz->cut_value = 8 + (match_max_len >> 2);
209 lz->get_matches = &lzma_hc4_get_matches;
210 lz->skip = &lzma_hc4_skip;
211 lz->cut_value = 8 + (match_max_len >> 2);
216 lz->get_matches = &lzma_bt2_get_matches;
217 lz->skip = &lzma_bt2_skip;
218 lz->cut_value = 16 + (match_max_len >> 1);
223 lz->get_matches = &lzma_bt3_get_matches;
224 lz->skip = &lzma_bt3_skip;
225 lz->cut_value = 16 + (match_max_len >> 1);
230 lz->get_matches = &lzma_bt4_get_matches;
231 lz->skip = &lzma_bt4_skip;
232 lz->cut_value = 16 + (match_max_len >> 1);
236 lzma_lz_encoder_end(lz, allocator);
237 return LZMA_HEADER_ERROR;
240 // Check if we have been requested to use a non-default cut_value.
241 if (match_finder_cycles > 0)
242 lz->cut_value = match_finder_cycles;
244 lz->match_max_len = match_max_len;
245 lz->cyclic_buffer_size = get_cyclic_buffer_size(history_size);
247 uint32_t hash_size_sum;
249 if (lzma_lz_encoder_hash_properties(match_finder, history_size,
250 &lz->hash_mask, &hash_size_sum, &num_items)) {
251 lzma_lz_encoder_end(lz, allocator);
252 return LZMA_HEADER_ERROR;
255 if (num_items != lz->num_items) {
256 #if UINT32_MAX >= SIZE_MAX / 4
257 // Check for integer overflow. (Huge dictionaries are not
258 // possible on 32-bit CPU.)
259 if (num_items > SIZE_MAX / sizeof(uint32_t)) {
260 lzma_lz_encoder_end(lz, allocator);
261 return LZMA_MEM_ERROR;
265 const size_t size_in_bytes
266 = (size_t)(num_items) * sizeof(uint32_t);
268 lzma_free(lz->hash, allocator);
269 lz->hash = lzma_alloc(size_in_bytes, allocator);
270 if (lz->hash == NULL) {
271 lzma_lz_encoder_end(lz, allocator);
272 return LZMA_MEM_ERROR;
275 lz->num_items = num_items;
278 lz->son = lz->hash + hash_size_sum;
280 // Reset the hash table to empty hash values.
282 uint32_t *restrict items = lz->hash;
284 for (uint32_t i = 0; i < hash_size_sum; ++i)
285 items[i] = EMPTY_HASH_VALUE;
288 lz->cyclic_buffer_pos = 0;
290 // Because zero is used as empty hash value, make the first byte
291 // appear at buffer[1 - offset].
294 // If we are using a preset dictionary, read it now.
295 // TODO: This isn't implemented yet so return LZMA_HEADER_ERROR.
296 if (preset_dictionary != NULL && preset_dictionary_size > 0) {
297 lzma_lz_encoder_end(lz, allocator);
298 return LZMA_HEADER_ERROR;
301 // Set the process function pointer.
302 lz->process = process;
309 lzma_lz_encoder_end(lzma_lz_encoder *lz, lzma_allocator *allocator)
311 lzma_free(lz->hash, allocator);
315 lzma_free(lz->buffer, allocator);
323 /// \brief Moves the data in the input window to free space for new data
325 /// lz->buffer is a sliding input window, which keeps lz->keep_size_before
326 /// bytes of input history available all the time. Now and then we need to
327 /// "slide" the buffer to make space for the new data to the end of the
328 /// buffer. At the same time, data older than keep_size_before is dropped.
331 move_window(lzma_lz_encoder *lz)
333 // buffer[move_offset] will become buffer[0].
334 assert(lz->read_pos > lz->keep_size_after);
335 size_t move_offset = lz->read_pos - lz->keep_size_before;
337 // We need one additional byte, since move_pos() moves on 1 byte.
338 // TODO: Clean up? At least document more.
342 assert(lz->write_pos > move_offset);
343 const size_t move_size = lz->write_pos - move_offset;
345 assert(move_offset + move_size <= lz->size);
347 memmove(lz->buffer, lz->buffer + move_offset, move_size);
349 lz->offset += move_offset;
350 lz->read_pos -= move_offset;
351 lz->read_limit -= move_offset;
352 lz->write_pos -= move_offset;
358 /// \brief Tries to fill the input window (lz->buffer)
360 /// If we are the last encoder in the chain, our input data is in in[].
361 /// Otherwise we call the next filter in the chain to process in[] and
362 /// write its output to lz->buffer.
364 /// This function must not be called once it has returned LZMA_STREAM_END.
367 fill_window(lzma_coder *coder, lzma_allocator *allocator, const uint8_t *in,
368 size_t *in_pos, size_t in_size, lzma_action action)
370 assert(coder->lz.read_pos <= coder->lz.write_pos);
373 // Move the sliding window if needed.
374 if (coder->lz.read_pos >= coder->lz.size - coder->lz.keep_size_after)
375 move_window(&coder->lz);
377 if (coder->next.code == NULL) {
378 // Not using a filter, simply memcpy() as much as possible.
379 bufcpy(in, in_pos, in_size, coder->lz.buffer,
380 &coder->lz.write_pos, coder->lz.size);
382 if (action == LZMA_FINISH && *in_pos == in_size)
383 ret = LZMA_STREAM_END;
388 ret = coder->next.code(coder->next.coder, allocator,
390 coder->lz.buffer, &coder->lz.write_pos,
391 coder->lz.size, action);
394 // If end of stream has been reached, we allow the encoder to process
395 // all the input (that is, read_pos is allowed to reach write_pos).
396 // Otherwise we keep keep_size_after bytes available as prebuffer.
397 if (ret == LZMA_STREAM_END) {
398 coder->lz.stream_end_was_reached = true;
399 coder->lz.read_limit = coder->lz.write_pos;
401 } else if (coder->lz.write_pos > coder->lz.keep_size_after) {
402 // This needs to be done conditionally, because if we got
403 // only little new input, there may be too little input
404 // to do any encoding yet.
405 coder->lz.read_limit = coder->lz.write_pos
406 - coder->lz.keep_size_after;
414 lzma_lz_encode(lzma_coder *coder, lzma_allocator *allocator,
415 const uint8_t *restrict in, size_t *restrict in_pos,
417 uint8_t *restrict out, size_t *restrict out_pos,
418 size_t out_size, lzma_action action)
420 while (*out_pos < out_size
421 && (*in_pos < in_size || action == LZMA_FINISH)) {
422 // Fill the input window if there is no more usable data.
423 if (!coder->lz.stream_end_was_reached && coder->lz.read_pos
424 >= coder->lz.read_limit) {
425 const lzma_ret ret = fill_window(coder, allocator,
426 in, in_pos, in_size, action);
427 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
432 if (coder->lz.process(coder, out, out_pos, out_size))
433 return LZMA_STREAM_END;
440 /// \brief Normalizes hash values
442 /// lzma_lz_normalize is called when lz->pos hits MAX_VAL_FOR_NORMALIZE,
443 /// which currently happens once every 2 GiB of input data (to be exact,
444 /// after the first 2 GiB it happens once every 2 GiB minus dictionary_size
445 /// bytes). lz->pos is incremented by lzma_lz_move_pos().
447 /// lz->hash contains big amount of offsets relative to lz->buffer.
448 /// The offsets are stored as uint32_t, which is the only reasonable
449 /// datatype for these offsets; uint64_t would waste far too much RAM
450 /// and uint16_t would limit the dictionary to 64 KiB (far too small).
452 /// When compressing files over 2 GiB, lz->buffer needs to be moved forward
453 /// to avoid integer overflows. We scan the lz->hash array and fix every
454 /// value to match the updated lz->buffer.
456 lzma_lz_encoder_normalize(lzma_lz_encoder *lz)
458 const uint32_t subvalue = lz->read_pos - lz->cyclic_buffer_size;
459 assert(subvalue <= INT32_MAX);
462 const uint32_t num_items = lz->num_items;
463 uint32_t *restrict items = lz->hash;
465 for (uint32_t i = 0; i < num_items; ++i) {
466 // If the distance is greater than the dictionary
467 // size, we can simply mark the item as empty.
468 if (items[i] <= subvalue)
469 items[i] = EMPTY_HASH_VALUE;
471 items[i] -= subvalue;
475 // Update offset to match the new locations.
476 lz->offset -= subvalue;