lzma_encoder_optimum_fast.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lzma_encoder_optimum_fast.c
  4. //
  5. // Author: Igor Pavlov
  6. //
  7. // This file has been put into the public domain.
  8. // You can do whatever you want with this file.
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "lzma_encoder_private.h"
  12. #include "memcmplen.h"
  13. #define change_pair(small_dist, big_dist) \
  14. (((big_dist) >> 7) > (small_dist))
  15. extern void
  16. lzma_lzma_optimum_fast(lzma_lzma1_encoder *restrict coder,
  17. lzma_mf *restrict mf,
  18. uint32_t *restrict back_res, uint32_t *restrict len_res)
  19. {
  20. const uint32_t nice_len = mf->nice_len;
  21. uint32_t len_main;
  22. uint32_t matches_count;
  23. if (mf->read_ahead == 0) {
  24. len_main = mf_find(mf, &matches_count, coder->matches);
  25. } else {
  26. assert(mf->read_ahead == 1);
  27. len_main = coder->longest_match_length;
  28. matches_count = coder->matches_count;
  29. }
  30. const uint8_t *buf = mf_ptr(mf) - 1;
  31. const uint32_t buf_avail = my_min(mf_avail(mf) + 1, MATCH_LEN_MAX);
  32. if (buf_avail < 2) {
  33. // There's not enough input left to encode a match.
  34. *back_res = UINT32_MAX;
  35. *len_res = 1;
  36. return;
  37. }
  38. // Look for repeated matches; scan the previous four match distances
  39. uint32_t rep_len = 0;
  40. uint32_t rep_index = 0;
  41. for (uint32_t i = 0; i < REPS; ++i) {
  42. // Pointer to the beginning of the match candidate
  43. const uint8_t *const buf_back = buf - coder->reps[i] - 1;
  44. // If the first two bytes (2 == MATCH_LEN_MIN) do not match,
  45. // this rep is not useful.
  46. if (not_equal_16(buf, buf_back))
  47. continue;
  48. // The first two bytes matched.
  49. // Calculate the length of the match.
  50. const uint32_t len = lzma_memcmplen(
  51. buf, buf_back, 2, buf_avail);
  52. // If we have found a repeated match that is at least
  53. // nice_len long, return it immediately.
  54. if (len >= nice_len) {
  55. *back_res = i;
  56. *len_res = len;
  57. mf_skip(mf, len - 1);
  58. return;
  59. }
  60. if (len > rep_len) {
  61. rep_index = i;
  62. rep_len = len;
  63. }
  64. }
  65. // We didn't find a long enough repeated match. Encode it as a normal
  66. // match if the match length is at least nice_len.
  67. if (len_main >= nice_len) {
  68. *back_res = coder->matches[matches_count - 1].dist + REPS;
  69. *len_res = len_main;
  70. mf_skip(mf, len_main - 1);
  71. return;
  72. }
  73. uint32_t back_main = 0;
  74. if (len_main >= 2) {
  75. back_main = coder->matches[matches_count - 1].dist;
  76. while (matches_count > 1 && len_main ==
  77. coder->matches[matches_count - 2].len + 1) {
  78. if (!change_pair(coder->matches[
  79. matches_count - 2].dist,
  80. back_main))
  81. break;
  82. --matches_count;
  83. len_main = coder->matches[matches_count - 1].len;
  84. back_main = coder->matches[matches_count - 1].dist;
  85. }
  86. if (len_main == 2 && back_main >= 0x80)
  87. len_main = 1;
  88. }
  89. if (rep_len >= 2) {
  90. if (rep_len + 1 >= len_main
  91. || (rep_len + 2 >= len_main
  92. && back_main > (UINT32_C(1) << 9))
  93. || (rep_len + 3 >= len_main
  94. && back_main > (UINT32_C(1) << 15))) {
  95. *back_res = rep_index;
  96. *len_res = rep_len;
  97. mf_skip(mf, rep_len - 1);
  98. return;
  99. }
  100. }
  101. if (len_main < 2 || buf_avail <= 2) {
  102. *back_res = UINT32_MAX;
  103. *len_res = 1;
  104. return;
  105. }
  106. // Get the matches for the next byte. If we find a better match,
  107. // the current byte is encoded as a literal.
  108. coder->longest_match_length = mf_find(mf,
  109. &coder->matches_count, coder->matches);
  110. if (coder->longest_match_length >= 2) {
  111. const uint32_t new_dist = coder->matches[
  112. coder->matches_count - 1].dist;
  113. if ((coder->longest_match_length >= len_main
  114. && new_dist < back_main)
  115. || (coder->longest_match_length == len_main + 1
  116. && !change_pair(back_main, new_dist))
  117. || (coder->longest_match_length > len_main + 1)
  118. || (coder->longest_match_length + 1 >= len_main
  119. && len_main >= 3
  120. && change_pair(new_dist, back_main))) {
  121. *back_res = UINT32_MAX;
  122. *len_res = 1;
  123. return;
  124. }
  125. }
  126. // In contrast to LZMA SDK, dictionary could not have been moved
  127. // between mf_find() calls, thus it is safe to just increment
  128. // the old buf pointer instead of recalculating it with mf_ptr().
  129. ++buf;
  130. const uint32_t limit = my_max(2, len_main - 1);
  131. for (uint32_t i = 0; i < REPS; ++i) {
  132. if (memcmp(buf, buf - coder->reps[i] - 1, limit) == 0) {
  133. *back_res = UINT32_MAX;
  134. *len_res = 1;
  135. return;
  136. }
  137. }
  138. *back_res = back_main + REPS;
  139. *len_res = len_main;
  140. mf_skip(mf, len_main - 2);
  141. return;
  142. }