Deleted Added
full compact
lz_encoder.c (292588) lz_encoder.c (312518)
1///////////////////////////////////////////////////////////////////////////////
2//
3/// \file lz_encoder.c
4/// \brief LZ in window
5///
6// Authors: Igor Pavlov
7// Lasse Collin
8//

--- 9 unchanged lines hidden (view full) ---

18// endianness a conditional in makefiles.
19#if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL)
20# include "lz_encoder_hash_table.h"
21#endif
22
23#include "memcmplen.h"
24
25
1///////////////////////////////////////////////////////////////////////////////
2//
3/// \file lz_encoder.c
4/// \brief LZ in window
5///
6// Authors: Igor Pavlov
7// Lasse Collin
8//

--- 9 unchanged lines hidden (view full) ---

18// endianness a conditional in makefiles.
19#if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL)
20# include "lz_encoder_hash_table.h"
21#endif
22
23#include "memcmplen.h"
24
25
26struct lzma_coder_s {
26typedef struct {
27 /// LZ-based encoder e.g. LZMA
28 lzma_lz_encoder lz;
29
30 /// History buffer and match finder
31 lzma_mf mf;
32
33 /// Next coder in the chain
34 lzma_next_coder next;
27 /// LZ-based encoder e.g. LZMA
28 lzma_lz_encoder lz;
29
30 /// History buffer and match finder
31 lzma_mf mf;
32
33 /// Next coder in the chain
34 lzma_next_coder next;
35};
35} lzma_coder;
36
37
38/// \brief Moves the data in the input window to free space for new data
39///
40/// mf->buffer is a sliding input window, which keeps mf->keep_size_before
41/// bytes of input history available all the time. Now and then we need to
42/// "slide" the buffer to make space for the new data to the end of the
43/// buffer. At the same time, data older than keep_size_before is dropped.

--- 108 unchanged lines hidden (view full) ---

152 coder->mf.skip(&coder->mf, pending);
153 }
154
155 return ret;
156}
157
158
159static lzma_ret
36
37
38/// \brief Moves the data in the input window to free space for new data
39///
40/// mf->buffer is a sliding input window, which keeps mf->keep_size_before
41/// bytes of input history available all the time. Now and then we need to
42/// "slide" the buffer to make space for the new data to the end of the
43/// buffer. At the same time, data older than keep_size_before is dropped.

--- 108 unchanged lines hidden (view full) ---

152 coder->mf.skip(&coder->mf, pending);
153 }
154
155 return ret;
156}
157
158
159static lzma_ret
160lz_encode(lzma_coder *coder, const lzma_allocator *allocator,
160lz_encode(void *coder_ptr, const lzma_allocator *allocator,
161 const uint8_t *restrict in, size_t *restrict in_pos,
162 size_t in_size,
163 uint8_t *restrict out, size_t *restrict out_pos,
164 size_t out_size, lzma_action action)
165{
161 const uint8_t *restrict in, size_t *restrict in_pos,
162 size_t in_size,
163 uint8_t *restrict out, size_t *restrict out_pos,
164 size_t out_size, lzma_action action)
165{
166 lzma_coder *coder = coder_ptr;
167
166 while (*out_pos < out_size
167 && (*in_pos < in_size || action != LZMA_RUN)) {
168 // Read more data to coder->mf.buffer if needed.
169 if (coder->mf.action == LZMA_RUN && coder->mf.read_pos
170 >= coder->mf.read_limit)
171 return_if_error(fill_window(coder, allocator,
172 in, in_pos, in_size, action));
173

--- 302 unchanged lines hidden (view full) ---

476
477 // Calculate the memory usage.
478 return ((uint64_t)(mf.hash_count) + mf.sons_count) * sizeof(uint32_t)
479 + mf.size + sizeof(lzma_coder);
480}
481
482
483static void
168 while (*out_pos < out_size
169 && (*in_pos < in_size || action != LZMA_RUN)) {
170 // Read more data to coder->mf.buffer if needed.
171 if (coder->mf.action == LZMA_RUN && coder->mf.read_pos
172 >= coder->mf.read_limit)
173 return_if_error(fill_window(coder, allocator,
174 in, in_pos, in_size, action));
175

--- 302 unchanged lines hidden (view full) ---

478
479 // Calculate the memory usage.
480 return ((uint64_t)(mf.hash_count) + mf.sons_count) * sizeof(uint32_t)
481 + mf.size + sizeof(lzma_coder);
482}
483
484
485static void
484lz_encoder_end(lzma_coder *coder, const lzma_allocator *allocator)
486lz_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
485{
487{
488 lzma_coder *coder = coder_ptr;
489
486 lzma_next_end(&coder->next, allocator);
487
488 lzma_free(coder->mf.son, allocator);
489 lzma_free(coder->mf.hash, allocator);
490 lzma_free(coder->mf.buffer, allocator);
491
492 if (coder->lz.end != NULL)
493 coder->lz.end(coder->lz.coder, allocator);
494 else
495 lzma_free(coder->lz.coder, allocator);
496
497 lzma_free(coder, allocator);
498 return;
499}
500
501
502static lzma_ret
490 lzma_next_end(&coder->next, allocator);
491
492 lzma_free(coder->mf.son, allocator);
493 lzma_free(coder->mf.hash, allocator);
494 lzma_free(coder->mf.buffer, allocator);
495
496 if (coder->lz.end != NULL)
497 coder->lz.end(coder->lz.coder, allocator);
498 else
499 lzma_free(coder->lz.coder, allocator);
500
501 lzma_free(coder, allocator);
502 return;
503}
504
505
506static lzma_ret
503lz_encoder_update(lzma_coder *coder, const lzma_allocator *allocator,
507lz_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
504 const lzma_filter *filters_null lzma_attribute((__unused__)),
505 const lzma_filter *reversed_filters)
506{
508 const lzma_filter *filters_null lzma_attribute((__unused__)),
509 const lzma_filter *reversed_filters)
510{
511 lzma_coder *coder = coder_ptr;
512
507 if (coder->lz.options_update == NULL)
508 return LZMA_PROG_ERROR;
509
510 return_if_error(coder->lz.options_update(
511 coder->lz.coder, reversed_filters));
512
513 return lzma_next_filter_update(
514 &coder->next, allocator, reversed_filters + 1);

--- 8 unchanged lines hidden (view full) ---

523 lzma_lz_options *lz_options))
524{
525#ifdef HAVE_SMALL
526 // We need that the CRC32 table has been initialized.
527 lzma_crc32_init();
528#endif
529
530 // Allocate and initialize the base data structure.
513 if (coder->lz.options_update == NULL)
514 return LZMA_PROG_ERROR;
515
516 return_if_error(coder->lz.options_update(
517 coder->lz.coder, reversed_filters));
518
519 return lzma_next_filter_update(
520 &coder->next, allocator, reversed_filters + 1);

--- 8 unchanged lines hidden (view full) ---

529 lzma_lz_options *lz_options))
530{
531#ifdef HAVE_SMALL
532 // We need that the CRC32 table has been initialized.
533 lzma_crc32_init();
534#endif
535
536 // Allocate and initialize the base data structure.
531 if (next->coder == NULL) {
532 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
533 if (next->coder == NULL)
537 lzma_coder *coder = next->coder;
538 if (coder == NULL) {
539 coder = lzma_alloc(sizeof(lzma_coder), allocator);
540 if (coder == NULL)
534 return LZMA_MEM_ERROR;
535
541 return LZMA_MEM_ERROR;
542
543 next->coder = coder;
536 next->code = &lz_encode;
537 next->end = &lz_encoder_end;
538 next->update = &lz_encoder_update;
539
544 next->code = &lz_encode;
545 next->end = &lz_encoder_end;
546 next->update = &lz_encoder_update;
547
540 next->coder->lz.coder = NULL;
541 next->coder->lz.code = NULL;
542 next->coder->lz.end = NULL;
548 coder->lz.coder = NULL;
549 coder->lz.code = NULL;
550 coder->lz.end = NULL;
543
551
544 next->coder->mf.buffer = NULL;
545 next->coder->mf.hash = NULL;
546 next->coder->mf.son = NULL;
547 next->coder->mf.hash_count = 0;
548 next->coder->mf.sons_count = 0;
552 // mf.size is initialized to silence Valgrind
553 // when used on optimized binaries (GCC may reorder
554 // code in a way that Valgrind gets unhappy).
555 coder->mf.buffer = NULL;
556 coder->mf.size = 0;
557 coder->mf.hash = NULL;
558 coder->mf.son = NULL;
559 coder->mf.hash_count = 0;
560 coder->mf.sons_count = 0;
549
561
550 next->coder->next = LZMA_NEXT_CODER_INIT;
562 coder->next = LZMA_NEXT_CODER_INIT;
551 }
552
553 // Initialize the LZ-based encoder.
554 lzma_lz_options lz_options;
563 }
564
565 // Initialize the LZ-based encoder.
566 lzma_lz_options lz_options;
555 return_if_error(lz_init(&next->coder->lz, allocator,
567 return_if_error(lz_init(&coder->lz, allocator,
556 filters[0].options, &lz_options));
557
568 filters[0].options, &lz_options));
569
558 // Setup the size information into next->coder->mf and deallocate
570 // Setup the size information into coder->mf and deallocate
559 // old buffers if they have wrong size.
571 // old buffers if they have wrong size.
560 if (lz_encoder_prepare(&next->coder->mf, allocator, &lz_options))
572 if (lz_encoder_prepare(&coder->mf, allocator, &lz_options))
561 return LZMA_OPTIONS_ERROR;
562
563 // Allocate new buffers if needed, and do the rest of
564 // the initialization.
573 return LZMA_OPTIONS_ERROR;
574
575 // Allocate new buffers if needed, and do the rest of
576 // the initialization.
565 if (lz_encoder_init(&next->coder->mf, allocator, &lz_options))
577 if (lz_encoder_init(&coder->mf, allocator, &lz_options))
566 return LZMA_MEM_ERROR;
567
568 // Initialize the next filter in the chain, if any.
578 return LZMA_MEM_ERROR;
579
580 // Initialize the next filter in the chain, if any.
569 return lzma_next_filter_init(&next->coder->next, allocator,
570 filters + 1);
581 return lzma_next_filter_init(&coder->next, allocator, filters + 1);
571}
572
573
574extern LZMA_API(lzma_bool)
575lzma_mf_is_supported(lzma_match_finder mf)
576{
577 bool ret = false;
578

--- 27 unchanged lines hidden ---
582}
583
584
585extern LZMA_API(lzma_bool)
586lzma_mf_is_supported(lzma_match_finder mf)
587{
588 bool ret = false;
589

--- 27 unchanged lines hidden ---