deflate_slow.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* deflate_slow.c -- compress data using the slow strategy of deflation algorithm
  2. *
  3. * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
  4. * For conditions of distribution and use, see copyright notice in zlib.h
  5. */
  6. #include "zbuild.h"
  7. #include "deflate.h"
  8. #include "deflate_p.h"
  9. #include "functable.h"
  10. /* ===========================================================================
  11. * Same as deflate_medium, but achieves better compression. We use a lazy
  12. * evaluation for matches: a match is finally adopted only if there is
  13. * no better match at the next window position.
  14. */
  15. Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
  16. Pos hash_head; /* head of hash chain */
  17. int bflush; /* set if current block must be flushed */
  18. int64_t dist;
  19. uint32_t match_len;
  20. match_func longest_match;
  21. if (s->max_chain_length <= 1024)
  22. longest_match = FUNCTABLE_FPTR(longest_match);
  23. else
  24. longest_match = FUNCTABLE_FPTR(longest_match_slow);
  25. /* Process the input block. */
  26. for (;;) {
  27. /* Make sure that we always have enough lookahead, except
  28. * at the end of the input file. We need STD_MAX_MATCH bytes
  29. * for the next match, plus WANT_MIN_MATCH bytes to insert the
  30. * string following the next match.
  31. */
  32. if (s->lookahead < MIN_LOOKAHEAD) {
  33. PREFIX(fill_window)(s);
  34. if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
  35. return need_more;
  36. }
  37. if (UNLIKELY(s->lookahead == 0))
  38. break; /* flush the current block */
  39. }
  40. /* Insert the string window[strstart .. strstart+2] in the
  41. * dictionary, and set hash_head to the head of the hash chain:
  42. */
  43. hash_head = 0;
  44. if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
  45. hash_head = s->quick_insert_string(s, s->strstart);
  46. }
  47. /* Find the longest match, discarding those <= prev_length.
  48. */
  49. s->prev_match = (Pos)s->match_start;
  50. match_len = STD_MIN_MATCH - 1;
  51. dist = (int64_t)s->strstart - hash_head;
  52. if (dist <= MAX_DIST(s) && dist > 0 && s->prev_length < s->max_lazy_match && hash_head != 0) {
  53. /* To simplify the code, we prevent matches with the string
  54. * of window index 0 (in particular we have to avoid a match
  55. * of the string with itself at the start of the input file).
  56. */
  57. match_len = longest_match(s, hash_head);
  58. /* longest_match() sets match_start */
  59. if (match_len <= 5 && (s->strategy == Z_FILTERED)) {
  60. /* If prev_match is also WANT_MIN_MATCH, match_start is garbage
  61. * but we will ignore the current match anyway.
  62. */
  63. match_len = STD_MIN_MATCH - 1;
  64. }
  65. }
  66. /* If there was a match at the previous step and the current
  67. * match is not better, output the previous match:
  68. */
  69. if (s->prev_length >= STD_MIN_MATCH && match_len <= s->prev_length) {
  70. unsigned int max_insert = s->strstart + s->lookahead - STD_MIN_MATCH;
  71. /* Do not insert strings in hash table beyond this. */
  72. check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  73. bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - STD_MIN_MATCH);
  74. /* Insert in hash table all strings up to the end of the match.
  75. * strstart-1 and strstart are already inserted. If there is not
  76. * enough lookahead, the last two strings are not inserted in
  77. * the hash table.
  78. */
  79. s->prev_length -= 1;
  80. s->lookahead -= s->prev_length;
  81. unsigned int mov_fwd = s->prev_length - 1;
  82. if (max_insert > s->strstart) {
  83. unsigned int insert_cnt = mov_fwd;
  84. if (UNLIKELY(insert_cnt > max_insert - s->strstart))
  85. insert_cnt = max_insert - s->strstart;
  86. s->insert_string(s, s->strstart + 1, insert_cnt);
  87. }
  88. s->prev_length = 0;
  89. s->match_available = 0;
  90. s->strstart += mov_fwd + 1;
  91. if (UNLIKELY(bflush))
  92. FLUSH_BLOCK(s, 0);
  93. } else if (s->match_available) {
  94. /* If there was no match at the previous position, output a
  95. * single literal. If there was a match but the current match
  96. * is longer, truncate the previous match to a single literal.
  97. */
  98. bflush = zng_tr_tally_lit(s, s->window[s->strstart-1]);
  99. if (UNLIKELY(bflush))
  100. FLUSH_BLOCK_ONLY(s, 0);
  101. s->prev_length = match_len;
  102. s->strstart++;
  103. s->lookahead--;
  104. if (UNLIKELY(s->strm->avail_out == 0))
  105. return need_more;
  106. } else {
  107. /* There is no previous match to compare with, wait for
  108. * the next step to decide.
  109. */
  110. s->prev_length = match_len;
  111. s->match_available = 1;
  112. s->strstart++;
  113. s->lookahead--;
  114. }
  115. }
  116. Assert(flush != Z_NO_FLUSH, "no flush?");
  117. if (UNLIKELY(s->match_available)) {
  118. Z_UNUSED(zng_tr_tally_lit(s, s->window[s->strstart-1]));
  119. s->match_available = 0;
  120. }
  121. s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
  122. if (UNLIKELY(flush == Z_FINISH)) {
  123. FLUSH_BLOCK(s, 1);
  124. return finish_done;
  125. }
  126. if (UNLIKELY(s->sym_next))
  127. FLUSH_BLOCK(s, 0);
  128. return block_done;
  129. }