Debian Codegraph @

third_party/rust/zlib-rs/src/deflate/algorithm/quick.rs

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