/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 | 0 | Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) { |
22 | 0 | unsigned char *window = s->window; |
23 | 0 | uint32_t max_inserts = MAX(2, s->max_insert_count); // spread insert requires min 2 |
24 | 0 | int bflush = 0; /* set if current block must be flushed */ |
25 | 0 | uint32_t match_len = 0; |
26 | |
|
27 | 0 | for (;;) { |
28 | 0 | 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 | 0 | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
36 | 0 | PREFIX(fill_window)(s); |
37 | 0 | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
38 | 0 | return need_more; |
39 | 0 | } |
40 | 0 | if (UNLIKELY(s->lookahead == 0)) |
41 | 0 | break; /* flush the current block */ |
42 | 0 | } |
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 | 0 | if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
48 | 0 | uint32_t str_val = Z_U32_FROM_LE(zng_memread_4(window + s->strstart)); |
49 | 0 | uint32_t hash_head = insert_knuth_val(s, s->strstart, str_val); |
50 | 0 | int64_t dist = (int64_t)s->strstart - hash_head; |
51 | 0 | 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 | 0 | 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 | 0 | match_len = FUNCTABLE_CALL(longest_match)(s, (uint32_t)hash_head); |
62 | | /* longest_match() sets match_start */ |
63 | 0 | } |
64 | 0 | } else { |
65 | 0 | lc = window[s->strstart]; |
66 | 0 | } |
67 | |
|
68 | 0 | if (match_len >= WANT_MIN_MATCH) { |
69 | 0 | Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t"); |
70 | 0 | Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t"); |
71 | 0 | check_match(s, s->strstart, s->match_start, match_len); |
72 | |
|
73 | 0 | const uint32_t cont_dist = s->strstart - s->match_start; |
74 | 0 | bflush = zng_tr_tally_dist(s, cont_dist, match_len - STD_MIN_MATCH); |
75 | |
|
76 | 0 | s->lookahead -= match_len; |
77 | | |
78 | | /* Insert new strings in the hash table using a stepped hashing function, saving time */ |
79 | 0 | if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) { |
80 | 0 | insert_knuth_spread_static(s, window, s->strstart + 1, match_len - 1 , max_inserts); |
81 | 0 | } else { |
82 | 0 | 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 | 0 | } |
87 | 0 | 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 | 0 | if (UNLIKELY(match_len == STD_MAX_MATCH && s->lookahead >= MIN_LOOKAHEAD)) { |
92 | 0 | for (;;) { |
93 | | // Check for continuing match |
94 | 0 | uint32_t cont_len = max_match_continues(window, s->strstart, cont_dist); |
95 | 0 | if (UNLIKELY(cont_len < 8)) |
96 | 0 | break; |
97 | | |
98 | 0 | check_match(s, s->strstart, s->strstart - cont_dist, cont_len); |
99 | | |
100 | | // Insert match |
101 | 0 | insert_knuth_spread_static(s, window, s->strstart, cont_len, max_inserts); |
102 | 0 | s->strstart += cont_len; |
103 | 0 | s->lookahead -= cont_len; |
104 | | |
105 | | // Emit match |
106 | 0 | bflush |= zng_tr_tally_dist(s, cont_dist, cont_len - STD_MIN_MATCH); |
107 | |
|
108 | 0 | if (UNLIKELY(bflush)) { |
109 | 0 | FLUSH_BLOCK(s, window, 0); |
110 | 0 | bflush = 0; |
111 | 0 | } |
112 | 0 | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD)) { |
113 | 0 | break; /* not enough lookahead, return to the main loop */ |
114 | 0 | } |
115 | 0 | } |
116 | 0 | } |
117 | 0 | match_len = 0; |
118 | |
|
119 | 0 | } else { |
120 | | /* No match, output a literal byte */ |
121 | 0 | bflush = zng_tr_tally_lit(s, lc); |
122 | 0 | s->lookahead--; |
123 | 0 | s->strstart++; |
124 | 0 | } |
125 | 0 | if (UNLIKELY(bflush)) |
126 | 0 | FLUSH_BLOCK(s, window, 0); |
127 | 0 | } |
128 | 0 | s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1); |
129 | |
|
130 | 0 | if (UNLIKELY(flush == Z_FINISH)) { |
131 | 0 | FLUSH_BLOCK(s, window, 1); |
132 | 0 | return finish_done; |
133 | 0 | } |
134 | 0 | if (UNLIKELY(s->sym_next)) |
135 | 0 | FLUSH_BLOCK(s, window, 0); |
136 | 0 | return block_done; |
137 | 0 | } |