deflate_fast.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* deflate_fast.c -- compress data using the fast 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. * Compress as much as possible from the input stream, return the current
  12. * block state.
  13. * This function does not perform lazy evaluation of matches and inserts
  14. * new strings in the dictionary only for unmatched strings or for short
  15. * matches. It is used only for the fast compression options.
  16. */
  17. Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
  18. Pos hash_head; /* head of the hash chain */
  19. int bflush = 0; /* set if current block must be flushed */
  20. int64_t dist;
  21. uint32_t match_len = 0;
  22. for (;;) {
  23. /* Make sure that we always have enough lookahead, except
  24. * at the end of the input file. We need STD_MAX_MATCH bytes
  25. * for the next match, plus WANT_MIN_MATCH bytes to insert the
  26. * string following the next match.
  27. */
  28. if (s->lookahead < MIN_LOOKAHEAD) {
  29. PREFIX(fill_window)(s);
  30. if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) {
  31. return need_more;
  32. }
  33. if (UNLIKELY(s->lookahead == 0))
  34. break; /* flush the current block */
  35. }
  36. /* Insert the string window[strstart .. strstart+2] in the
  37. * dictionary, and set hash_head to the head of the hash chain:
  38. */
  39. if (s->lookahead >= WANT_MIN_MATCH) {
  40. hash_head = quick_insert_string(s, s->strstart);
  41. dist = (int64_t)s->strstart - hash_head;
  42. /* Find the longest match, discarding those <= prev_length.
  43. * At this point we have always match length < WANT_MIN_MATCH
  44. */
  45. if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) {
  46. /* To simplify the code, we prevent matches with the string
  47. * of window index 0 (in particular we have to avoid a match
  48. * of the string with itself at the start of the input file).
  49. */
  50. match_len = FUNCTABLE_CALL(longest_match)(s, hash_head);
  51. /* longest_match() sets match_start */
  52. }
  53. }
  54. if (match_len >= WANT_MIN_MATCH) {
  55. check_match(s, s->strstart, s->match_start, match_len);
  56. bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
  57. s->lookahead -= match_len;
  58. /* Insert new strings in the hash table only if the match length
  59. * is not too large. This saves time but degrades compression.
  60. */
  61. if (match_len <= s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) {
  62. match_len--; /* string at strstart already in table */
  63. s->strstart++;
  64. insert_string(s, s->strstart, match_len);
  65. s->strstart += match_len;
  66. } else {
  67. s->strstart += match_len;
  68. quick_insert_string(s, s->strstart + 2 - STD_MIN_MATCH);
  69. /* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not
  70. * matter since it will be recomputed at next deflate call.
  71. */
  72. }
  73. match_len = 0;
  74. } else {
  75. /* No match, output a literal byte */
  76. bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
  77. s->lookahead--;
  78. s->strstart++;
  79. }
  80. if (UNLIKELY(bflush))
  81. FLUSH_BLOCK(s, 0);
  82. }
  83. s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1);
  84. if (UNLIKELY(flush == Z_FINISH)) {
  85. FLUSH_BLOCK(s, 1);
  86. return finish_done;
  87. }
  88. if (UNLIKELY(s->sym_next))
  89. FLUSH_BLOCK(s, 0);
  90. return block_done;
  91. }