/src/zlib-ng/deflate_fast.c
Line | Count | Source |
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 | | |
7 | | #include "zbuild.h" |
8 | | #include "zmemory.h" |
9 | | #include "deflate.h" |
10 | | #include "deflate_p.h" |
11 | | #include "functable.h" |
12 | | #include "insert_string_p.h" |
13 | | |
14 | | /* =========================================================================== |
15 | | * Compress as much as possible from the input stream, return the current |
16 | | * block state. |
17 | | * This function does not perform lazy evaluation of matches and inserts |
18 | | * new strings in the dictionary only for unmatched strings or for short |
19 | | * matches. It is used only for the fast compression options. |
20 | | */ |
21 | 1.20k | Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) { |
22 | 1.20k | unsigned char *window = s->window; |
23 | 1.20k | uint32_t max_inserts = MAX(2, s->max_insert_count); // spread insert requires min 2 |
24 | 1.20k | int bflush = 0; /* set if current block must be flushed */ |
25 | 1.20k | uint32_t match_len = 0; |
26 | | |
27 | 21.3M | for (;;) { |
28 | 21.3M | uint8_t lc; |
29 | | |
30 | | /* Make sure that we always have enough lookahead, except |
31 | | * at the end of the input file. We need STD_MAX_MATCH bytes |
32 | | * for the next match, plus WANT_MIN_MATCH bytes to insert the |
33 | | * string following the next match. |
34 | | */ |
35 | 21.3M | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
36 | 74.0k | PREFIX(fill_window)(s); |
37 | 74.0k | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
38 | 180 | return need_more; |
39 | 180 | } |
40 | 73.8k | if (UNLIKELY(s->lookahead == 0)) |
41 | 948 | break; /* flush the current block */ |
42 | 73.8k | } |
43 | | |
44 | | /* Insert the string window[strstart .. strstart+2] in the |
45 | | * dictionary, and set hash_head to the head of the hash chain: |
46 | | */ |
47 | 21.3M | if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
48 | 21.3M | uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + s->strstart)); |
49 | 21.3M | uint32_t hash_head = insert_knuth_val(s, s->strstart, str_val); |
50 | 21.3M | int64_t dist = (int64_t)s->strstart - hash_head; |
51 | 21.3M | lc = (uint8_t)str_val; |
52 | | |
53 | | /* Find the longest match. |
54 | | * At this point we have always match length < WANT_MIN_MATCH |
55 | | */ |
56 | 21.3M | if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) { |
57 | | /* To simplify the code, we prevent matches with the string |
58 | | * of window index 0 (in particular we have to avoid a match |
59 | | * of the string with itself at the start of the input file). |
60 | | */ |
61 | 6.61M | match_len = FUNCTABLE_CALL(longest_match)(s, (uint32_t)hash_head); |
62 | | /* longest_match() sets match_start */ |
63 | 6.61M | } |
64 | 21.3M | } else { |
65 | 1.88k | lc = window[s->strstart]; |
66 | 1.88k | } |
67 | | |
68 | 21.3M | if (match_len >= WANT_MIN_MATCH) { |
69 | 771k | Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t"); |
70 | 771k | Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t"); |
71 | 771k | check_match(s, s->strstart, s->match_start, match_len); |
72 | | |
73 | 771k | const uint32_t cont_dist = s->strstart - s->match_start; |
74 | 771k | bflush = zng_tr_tally_dist(s, cont_dist, match_len - STD_MIN_MATCH); |
75 | | |
76 | 771k | s->lookahead -= match_len; |
77 | | |
78 | | /* Insert new strings in the hash table using a stepped hashing function, saving time */ |
79 | 771k | if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
80 | 770k | insert_knuth_spread_static(s, window, s->strstart + 1, match_len - 1 , max_inserts); |
81 | 770k | } else { |
82 | 422 | insert_knuth(s, window, s->strstart + 2 - STD_MIN_MATCH + match_len); |
83 | | /* If lookahead < WANT_MIN_MATCH, ins_h is garbage, but it does not |
84 | | * matter since it will be recomputed at next deflate call. |
85 | | */ |
86 | 422 | } |
87 | 771k | s->strstart += match_len; |
88 | | |
89 | | /* A max-length match might continue: keep emitting continuations at the same |
90 | | * distance without a full search as long as possible. */ |
91 | 771k | if (UNLIKELY(match_len == STD_MAX_MATCH && s->lookahead >= MIN_LOOKAHEAD)) { |
92 | 55.9k | for (;;) { |
93 | | // Check for continuing match |
94 | 55.9k | uint32_t cont_len = max_match_continues(window, s->strstart, cont_dist); |
95 | 55.9k | if (UNLIKELY(cont_len < 8)) |
96 | 13.7k | break; |
97 | | |
98 | 42.2k | check_match(s, s->strstart, s->strstart - cont_dist, cont_len); |
99 | | |
100 | | // Insert match |
101 | 42.2k | insert_knuth_spread_static(s, window, s->strstart, cont_len, max_inserts); |
102 | 42.2k | s->strstart += cont_len; |
103 | 42.2k | s->lookahead -= cont_len; |
104 | | |
105 | | // Emit match |
106 | 42.2k | bflush |= zng_tr_tally_dist(s, cont_dist, cont_len - STD_MIN_MATCH); |
107 | | |
108 | 42.2k | if (UNLIKELY(bflush)) { |
109 | 70 | FLUSH_BLOCK(s, window, 0); |
110 | 67 | bflush = 0; |
111 | 67 | } |
112 | 42.2k | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
113 | 276 | break; /* not enough lookahead, return to the main loop */ |
114 | 276 | } |
115 | 42.2k | } |
116 | 14.0k | } |
117 | 771k | match_len = 0; |
118 | | |
119 | 20.5M | } else { |
120 | | /* No match, output a literal byte */ |
121 | 20.5M | bflush = zng_tr_tally_lit(s, lc); |
122 | 20.5M | s->lookahead--; |
123 | 20.5M | s->strstart++; |
124 | 20.5M | } |
125 | 21.3M | if (UNLIKELY(bflush)) |
126 | 21.3M | FLUSH_BLOCK(s, window, 0); |
127 | 21.3M | } |
128 | 948 | s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1); |
129 | | |
130 | 948 | if (UNLIKELY(flush == Z_FINISH)) { |
131 | 948 | FLUSH_BLOCK(s, window, 1); |
132 | 934 | return finish_done; |
133 | 948 | } |
134 | 0 | if (UNLIKELY(s->sym_next)) |
135 | 0 | FLUSH_BLOCK(s, window, 0); |
136 | 0 | return block_done; |
137 | 0 | } |