/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 | int bflush = 0; /* set if current block must be flushed */ |
24 | 0 | uint32_t match_len = 0; |
25 | |
|
26 | 0 | for (;;) { |
27 | 0 | uint8_t lc; |
28 | | |
29 | | /* Make sure that we always have enough lookahead, except |
30 | | * at the end of the input file. We need STD_MAX_MATCH bytes |
31 | | * for the next match, plus WANT_MIN_MATCH bytes to insert the |
32 | | * string following the next match. |
33 | | */ |
34 | 0 | if (s->lookahead < MIN_LOOKAHEAD) { |
35 | 0 | PREFIX(fill_window)(s); |
36 | 0 | if (UNLIKELY(s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH)) { |
37 | 0 | return need_more; |
38 | 0 | } |
39 | 0 | if (UNLIKELY(s->lookahead == 0)) |
40 | 0 | break; /* flush the current block */ |
41 | 0 | } |
42 | | |
43 | | /* Insert the string window[strstart .. strstart+2] in the |
44 | | * dictionary, and set hash_head to the head of the hash chain: |
45 | | */ |
46 | 0 | if (s->lookahead >= WANT_MIN_MATCH) { |
47 | 0 | #if BYTE_ORDER == LITTLE_ENDIAN |
48 | 0 | uint32_t str_val = zng_memread_4(window + s->strstart); |
49 | | #else |
50 | | uint32_t str_val = ZSWAP32(zng_memread_4(window + s->strstart)); |
51 | | #endif |
52 | 0 | uint32_t hash_head = quick_insert_value(s, s->strstart, str_val); |
53 | 0 | int64_t dist = (int64_t)s->strstart - hash_head; |
54 | 0 | lc = (uint8_t)str_val; |
55 | | |
56 | | /* Find the longest match, discarding those <= prev_length. |
57 | | * At this point we have always match length < WANT_MIN_MATCH |
58 | | */ |
59 | 0 | if (dist <= MAX_DIST(s) && dist > 0 && hash_head != 0) { |
60 | | /* To simplify the code, we prevent matches with the string |
61 | | * of window index 0 (in particular we have to avoid a match |
62 | | * of the string with itself at the start of the input file). |
63 | | */ |
64 | 0 | match_len = FUNCTABLE_CALL(longest_match)(s, (uint32_t)hash_head); |
65 | | /* longest_match() sets match_start */ |
66 | 0 | } |
67 | 0 | } else { |
68 | 0 | lc = window[s->strstart]; |
69 | 0 | } |
70 | |
|
71 | 0 | if (match_len >= WANT_MIN_MATCH) { |
72 | 0 | Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t"); |
73 | 0 | Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t"); |
74 | 0 | check_match(s, s->strstart, s->match_start, match_len); |
75 | |
|
76 | 0 | bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH); |
77 | |
|
78 | 0 | s->lookahead -= match_len; |
79 | | |
80 | | /* Insert new strings in the hash table only if the match length |
81 | | * is not too large. This saves time but degrades compression. |
82 | | */ |
83 | 0 | if (match_len <= s->max_insert_length && s->lookahead >= WANT_MIN_MATCH) { |
84 | 0 | match_len--; /* string at strstart already in table */ |
85 | 0 | s->strstart++; |
86 | |
|
87 | 0 | insert_string_static(s, s->strstart, match_len); |
88 | 0 | s->strstart += match_len; |
89 | 0 | } else { |
90 | 0 | s->strstart += match_len; |
91 | 0 | quick_insert_string(s, s->strstart + 2 - STD_MIN_MATCH); |
92 | | |
93 | | /* If lookahead < STD_MIN_MATCH, ins_h is garbage, but it does not |
94 | | * matter since it will be recomputed at next deflate call. |
95 | | */ |
96 | 0 | } |
97 | 0 | match_len = 0; |
98 | 0 | } else { |
99 | | /* No match, output a literal byte */ |
100 | 0 | bflush = zng_tr_tally_lit(s, lc); |
101 | 0 | s->lookahead--; |
102 | 0 | s->strstart++; |
103 | 0 | } |
104 | 0 | if (UNLIKELY(bflush)) |
105 | 0 | FLUSH_BLOCK(s, 0); |
106 | 0 | } |
107 | 0 | s->insert = s->strstart < (STD_MIN_MATCH - 1) ? s->strstart : (STD_MIN_MATCH - 1); |
108 | |
|
109 | 0 | if (UNLIKELY(flush == Z_FINISH)) { |
110 | 0 | FLUSH_BLOCK(s, 1); |
111 | 0 | return finish_done; |
112 | 0 | } |
113 | 0 | if (UNLIKELY(s->sym_next)) |
114 | 0 | FLUSH_BLOCK(s, 0); |
115 | 0 | return block_done; |
116 | 0 | } |