Debian Codegraph @

debian/rust-vendor/zlib-rs/src/deflate/algorithm/quick.rs

raw
1 #![forbid(unsafe_code)]
2
3 use crate::deflate::hash_calc::StandardHashCalc;
4 use crate::{
5 deflate::{
6 fill_window, flush_pending, BlockState, BlockType, DeflateStream, State, StaticTreeDesc,
7 MIN_LOOKAHEAD, STD_MAX_MATCH, STD_MIN_MATCH, WANT_MIN_MATCH,
8 },
9 DeflateFlush,
10 };
11
12 pub fn deflate_quick(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockState {
13 let mut state = &mut stream.state;
14
15 macro_rules! quick_end_block {
16 ($last:expr) => {
17 if state.block_open > 0 {
18 state
19 .bit_writer
20 .emit_end_block_and_align(&StaticTreeDesc::L.static_tree, $last);
21 state.block_open = 0;
22 state.block_start = state.strstart as isize;
23 flush_pending(stream);
24 #[allow(unused_assignments)]
25 {
26 state = &mut stream.state;
27 }
28 if stream.avail_out == 0 {
29 return match $last {
30 true => BlockState::FinishStarted,
31 false => BlockState::NeedMore,
32 };
33 }
34 }
35 };
36 }
37
38 macro_rules! quick_start_block {
39 ($last:expr) => {
40 state.bit_writer.emit_tree(BlockType::StaticTrees, $last);
41 state.block_open = 1 + $last as u8;
42 state.block_start = state.strstart as isize;
43 };
44 }
45
46 let last = matches!(flush, DeflateFlush::Finish);
47
48 if last && state.block_open != 2 {
49 /* Emit end of previous block */
50 quick_end_block!(false);
51 /* Emit start of last block */
52 quick_start_block!(last);
53 } else if state.block_open == 0 && state.lookahead > 0 {
54 /* Start new block only when we have lookahead data, so that if no
55 input data is given an empty block will not be written */
56 quick_start_block!(last);
57 }
58
59 loop {
60 if state.bit_writer.pending.pending + State::BIT_BUF_SIZE.div_ceil(8) as usize
61 >= state.pending_buf_size()
62 {
63 flush_pending(stream);
64 state = &mut stream.state;
65 if stream.avail_out == 0 {
66 return if last
67 && stream.avail_in == 0
68 && state.bit_writer.bits_valid == 0
69 && state.block_open == 0
70 {
71 BlockState::FinishStarted
72 } else {
73 BlockState::NeedMore
74 };
75 }
76 }
77
78 if state.lookahead < MIN_LOOKAHEAD {
79 fill_window(stream);
80 state = &mut stream.state;
81
82 if state.lookahead < MIN_LOOKAHEAD && matches!(flush, DeflateFlush::NoFlush) {
83 return BlockState::NeedMore;
84 }
85 if state.lookahead == 0 {
86 break;
87 }
88
89 if state.block_open == 0 {
90 // Start new block when we have lookahead data,
91 // so that if no input data is given an empty block will not be written
92 quick_start_block!(last);
93 }
94 }
95
96 let lc: u8;
97 if state.lookahead >= WANT_MIN_MATCH {
98 macro_rules! first_four_bytes {
99 ($slice:expr, $offset:expr) => {
100 u32::from_le_bytes($slice[$offset..$offset + 4].try_into().unwrap())
101 };
102 }
103
104 let str_val = {
105 let str_start = &state.window.filled()[state.strstart..];
106 first_four_bytes!(str_start, 0)
107 };
108 let hash_head = StandardHashCalc::quick_insert_value(state, state.strstart, str_val);
109 let dist = state.strstart as isize - hash_head as isize;
110
111 if dist <= state.max_dist() as isize && dist > 0 {
112 let match_start = &state.window.filled()[hash_head as usize..];
113
114 if str_val == first_four_bytes!(match_start, 0) {
115 let mut match_len = crate::deflate::compare256::compare256_slice(
116 &state.window.filled()[state.strstart + 2..],
117 &match_start[2..],
118 ) + 2;
119
120 if match_len >= WANT_MIN_MATCH {
121 match_len = Ord::min(match_len, state.lookahead);
122 match_len = Ord::min(match_len, STD_MAX_MATCH);
123
124 // TODO do this with a debug_assert?
125 // check_match(s, state.strstart, hash_head, match_len);
126
127 // The `dist` value is a distance within the window,
128 // and MAX_WBITS == 15 (32k), hence a u16 can always represent this value.
129 let dist = u16::try_from(dist).unwrap();
130
131 state
132 .bit_writer
133 .emit_dist_static((match_len - STD_MIN_MATCH) as u8, dist);
134 state.lookahead -= match_len;
135 state.strstart += match_len;
136 continue;
137 }
138 }
139 }
140 lc = str_val as u8;
141 } else {
142 lc = state.window.filled()[state.strstart];
143 }
144 state.bit_writer.emit_lit(StaticTreeDesc::L.static_tree, lc);
145 state.strstart += 1;
146 state.lookahead -= 1;
147 }
148
149 state.insert = Ord::min(state.strstart, STD_MIN_MATCH - 1);
150
151 quick_end_block!(last);
152
153 if last {
154 BlockState::FinishDone
155 } else {
156 BlockState::BlockDone
157 }
158 }