Debian Codegraph @

src/window.rs

raw symbols SCIP
1 use crate::{Bitstream, DecodeFailed, MAX_CHUNK_SIZE};
2
3 /// The window size is not stored in the compressed data stream and must be known before
4 /// decoding begins.
5 ///
6 /// The window size should be the smallest power of two between 2^17 and 2^25 that is greater
7 /// than or equal to the sum of the size of the reference data rounded up to a multiple of
8 /// 32_768 and the size of the subject data. However, some implementations also seem to support
9 /// a window size of less than 2^17, and this one is no exception.
10 #[repr(u32)]
11 #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
12 pub enum WindowSize {
13 /// Window size of 32 KB (2^15 bytes).
14 KB32 = 0x0000_8000,
15 /// Window size of 64 KB (2^16 bytes).
16 KB64 = 0x0001_0000,
17 /// Window size of 128 KB (2^17 bytes).
18 KB128 = 0x0002_0000,
19 /// Window size of 256 KB (2^18 bytes).
20 KB256 = 0x0004_0000,
21 /// Window size of 512 KB (2^19 bytes).
22 KB512 = 0x0008_0000,
23 /// Window size of 1 MB (2^20 bytes).
24 MB1 = 0x0010_0000,
25 /// Window size of 2 MB (2^21 bytes).
26 MB2 = 0x0020_0000,
27 /// Window size of 4 MB (2^22 bytes).
28 MB4 = 0x0040_0000,
29 /// Window size of 8 MB (2^23 bytes).
30 MB8 = 0x0080_0000,
31 /// Window size of 16 MB (2^24 bytes).
32 MB16 = 0x0100_0000,
33 /// Window size of 32 MB (2^25 bytes).
34 MB32 = 0x0200_0000,
35 }
36
37 /// A sliding window of a certain size.
38 ///
39 /// A `std::collections::VecDeque` is not used because the `deque_make_contiguous` feature
40 /// is [nightly-only experimental](https://github.com/rust-lang/rust/issues/70929).
41 pub struct Window {
42 pos: usize,
43 buffer: Box<[u8]>,
44 }
45
46 impl WindowSize {
47 /// The window size determines the number of window subdivisions, or position slots.
48 pub(crate) fn position_slots(&self) -> usize {
49 use WindowSize::*;
50
51 match self {
52 KB32 => 30,
53 KB64 => 32,
54 KB128 => 34,
55 KB256 => 36,
56 KB512 => 38,
57 MB1 => 42,
58 MB2 => 50,
59 MB4 => 66,
60 MB8 => 98,
61 MB16 => 162,
62 MB32 => 290,
63 }
64 }
65
66 fn value(&self) -> usize {
67 *self as usize
68 }
69
70 pub(crate) fn create_buffer(&self) -> Window {
71 // The window must be at least as big as the smallest chunk, or else we can't possibly
72 // contain an entire chunk inside of the sliding window.
73 assert!(self.value() >= MAX_CHUNK_SIZE);
74
75 // We can use bit operations if we rely on this assumption so make sure it holds.
76 assert!(self.value().is_power_of_two());
77
78 Window {
79 pos: 0,
80 buffer: vec![0; self.value()].into_boxed_slice(),
81 }
82 }
83 }
84
85 impl Window {
86 fn advance(&mut self, delta: usize) {
87 self.pos += delta;
88 if self.pos >= self.buffer.len() {
89 self.pos -= self.buffer.len();
90 }
91 }
92
93 pub fn push(&mut self, value: u8) {
94 self.buffer[self.pos] = value;
95 self.advance(1);
96 }
97
98 pub fn copy_from_self(&mut self, offset: usize, length: usize) {
99 // For the fast path:
100 // * Source cannot wrap around
101 // * `copy_within` won't overwrite as we go but we need that
102 // * Destination cannot wrap around
103 if offset <= self.pos && length <= offset && self.pos + length < self.buffer.len() {
104 // Best case: neither source or destination wrap around
105 // TODO write a test for this because it used to fail
106 let start = self.pos - offset;
107 self.buffer.copy_within(start..start + length, self.pos);
108 } else {
109 // Either source or destination wrap around. We could expand this case into three
110 // (one for only source wrapping, one for only destination wrapping, one for both)
111 // but it's not really worth the effort.
112 //
113 // We could work out the ranges for use in `copy_within` but this is a lot simpler.
114 let mask = self.buffer.len() - 1; // relying on power of two assumption
115
116 for i in 0..length {
117 let dst = (self.pos + i) & mask;
118 let src = (self.buffer.len() + self.pos + i - offset) & mask;
119 self.buffer[dst] = self.buffer[src];
120 }
121 }
122
123 self.advance(length);
124 }
125
126 pub fn copy_from_bitstream(
127 &mut self,
128 bitstream: &mut Bitstream,
129 len: usize,
130 ) -> Result<(), DecodeFailed> {
131 if len > self.buffer.len() {
132 return Err(DecodeFailed::WindowTooSmall);
133 }
134
135 if self.pos + len > self.buffer.len() {
136 let shift = self.pos + len - self.buffer.len();
137 self.pos -= shift;
138
139 // No need to actually save the part we're about to overwrite because when reading
140 // with the bitstream we would also overwrite it anyway.
141 self.buffer.copy_within(shift.., 0);
142 }
143
144 bitstream.read_raw(&mut self.buffer[self.pos..self.pos + len])?;
145 self.advance(len);
146 Ok(())
147 }
148
149 pub fn past_view(&mut self, len: usize) -> Result<&mut [u8], DecodeFailed> {
150 if len > MAX_CHUNK_SIZE {
151 return Err(DecodeFailed::ChunkTooLong);
152 }
153
154 // Being at zero means we're actually at max length where is impossible for `len` to be
155 // bigger and we would not want to bother shifting the entire array to end where it was.
156 if self.pos != 0 && len > self.pos {
157 let shift = len - self.pos;
158 self.advance(shift);
159
160 let tmp = self.buffer[self.buffer.len() - shift..].to_vec();
161 self.buffer.copy_within(0..self.buffer.len() - shift, shift);
162 self.buffer[..shift].copy_from_slice(&tmp);
163 }
164
165 // Because we want to read behind us, being at zero means we're at the end
166 let pos = if self.pos == 0 {
167 self.buffer.len()
168 } else {
169 self.pos
170 };
171
172 Ok(&mut self.buffer[pos - len..pos])
173 }
174 }
175
176 #[cfg(test)]
177 mod tests {
178 use super::*;
179
180 #[test]
181 fn check_push() {
182 let mut window = WindowSize::KB32.create_buffer();
183 window.push(1);
184 window.push(2);
185 window.push(3);
186 assert_eq!(window.pos, 3);
187 assert_eq!(&window.buffer[..3], &[1, 2, 3]);
188 assert!(window.buffer[3..].iter().all(|&x| x == 0));
189 }
190
191 #[test]
192 fn check_push_before_boundary() {
193 let mut window = WindowSize::KB32.create_buffer();
194 window.pos = window.buffer.len() - 1;
195 window.push(1);
196 assert_eq!(window.pos, 0);
197 }
198
199 #[test]
200 fn check_push_at_boundary() {
201 let mut window = WindowSize::KB32.create_buffer();
202 for _ in 0..((1 << 15) - 2) {
203 window.push(0);
204 }
205 window.push(1);
206 window.push(2);
207 window.push(3);
208 window.push(4);
209 assert_eq!(window.pos, 2);
210 assert_eq!(&window.buffer[window.buffer.len() - 2..], &[1, 2]);
211 assert_eq!(&window.buffer[..2], &[3, 4]);
212 assert!(window.buffer[2..window.buffer.len() - 2]
213 .iter()
214 .all(|&x| x == 0));
215 }
216
217 #[test]
218 fn check_copy_from_self() {
219 let mut window = WindowSize::KB32.create_buffer();
220 window.buffer[0] = 1;
221 window.buffer[1] = 2;
222 window.buffer[2] = 3;
223 window.pos = 3;
224 window.copy_from_self(3, 2);
225 assert_eq!(window.pos, 5);
226 assert_eq!(&window.buffer[..5], &[1, 2, 3, 1, 2]);
227 assert!(window.buffer[5..].iter().all(|&x| x == 0));
228 }
229
230 #[test]
231 fn check_copy_from_self_overlap() {
232 let mut window = WindowSize::KB32.create_buffer();
233 window.buffer[0] = 1;
234 window.buffer[1] = 2;
235 window.buffer[2] = 3;
236 window.pos = 3;
237 window.copy_from_self(2, 3);
238 assert_eq!(window.pos, 6);
239 assert_eq!(&window.buffer[..6], &[1, 2, 3, 2, 3, 2]);
240 assert!(window.buffer[6..].iter().all(|&x| x == 0));
241 }
242
243 #[test]
244 fn check_copy_at_boundary_from_self() {
245 let mut window = WindowSize::KB32.create_buffer();
246 window.buffer[window.buffer.len() - 3] = 1;
247 window.buffer[window.buffer.len() - 2] = 2;
248 window.pos = window.buffer.len() - 1;
249 window.copy_from_self(2, 2);
250 assert_eq!(window.pos, 1);
251 assert_eq!(window.buffer[0], 2);
252 assert_eq!(&window.buffer[window.buffer.len() - 3..], &[1, 2, 1]);
253 assert!(window.buffer[1..window.buffer.len() - 3]
254 .iter()
255 .all(|&x| x == 0));
256 }
257
258 #[test]
259 fn check_copy_from_self_before_boundary() {
260 let mut window = WindowSize::KB32.create_buffer();
261 window.buffer[window.buffer.len() - 4] = 1;
262 window.buffer[window.buffer.len() - 3] = 2;
263 window.pos = window.buffer.len() - 2;
264 window.copy_from_self(2, 2);
265 assert_eq!(window.pos, 0);
266 }
267
268 #[test]
269 fn check_copy_from_self_at_boundary() {
270 let mut window = WindowSize::KB32.create_buffer();
271 window.buffer[window.buffer.len() - 2] = 1;
272 window.buffer[window.buffer.len() - 1] = 2;
273 window.buffer[0] = 3;
274 window.buffer[1] = 4;
275 window.pos = 2;
276 window.copy_from_self(4, 3);
277 assert_eq!(window.pos, 5);
278 assert_eq!(&window.buffer[..5], &[3, 4, 1, 2, 3]);
279 assert_eq!(&window.buffer[window.buffer.len() - 2..], &[1, 2]);
280 assert!(window.buffer[5..window.buffer.len() - 2]
281 .iter()
282 .all(|&x| x == 0));
283 }
284
285 #[test]
286 fn check_bitstream() {
287 let buffer = [1, 2, 3, 4];
288 let mut bitstream = Bitstream::new(&buffer);
289 let mut window = WindowSize::KB32.create_buffer();
290 window.copy_from_bitstream(&mut bitstream, 4).unwrap();
291 assert_eq!(window.pos, 4);
292 assert_eq!(&window.buffer[..4], &[1, 2, 3, 4]);
293 assert!(window.buffer[4..].iter().all(|&x| x == 0));
294 }
295
296 #[test]
297 fn check_bitstream_before_boundary() {
298 let buffer = [1, 2, 3, 4];
299 let mut bitstream = Bitstream::new(&buffer);
300 let mut window = WindowSize::KB32.create_buffer();
301 window.pos = window.buffer.len() - 4;
302 window.copy_from_bitstream(&mut bitstream, 4).unwrap();
303 assert_eq!(window.pos, 0);
304 }
305
306 #[test]
307 fn check_bitstream_at_boundary() {
308 let buffer = [1, 2, 3, 4];
309 let mut bitstream = Bitstream::new(&buffer);
310 let mut window = WindowSize::KB32.create_buffer();
311 window.pos = window.buffer.len() - 2;
312 window.copy_from_bitstream(&mut bitstream, 4).unwrap();
313 assert_eq!(window.pos, 0);
314 assert_eq!(&window.buffer[window.buffer.len() - 4..], &[1, 2, 3, 4]);
315 assert!(window.buffer[..window.buffer.len() - 4]
316 .iter()
317 .all(|&x| x == 0));
318 }
319
320 #[test]
321 fn check_past_view() {
322 let mut window = WindowSize::KB32.create_buffer();
323 window.buffer[0] = 1;
324 window.buffer[1] = 2;
325 window.buffer[2] = 3;
326 window.pos = 3;
327 assert_eq!(window.past_view(2).unwrap(), &[2, 3]);
328 assert_eq!(window.past_view(3).unwrap(), &[1, 2, 3]);
329 }
330
331 #[test]
332 fn check_past_view_at_boundary() {
333 let mut window = WindowSize::KB32.create_buffer();
334 window.buffer[window.buffer.len() - 2] = 1;
335 window.buffer[window.buffer.len() - 1] = 2;
336 window.buffer[0] = 3;
337 window.buffer[1] = 4;
338 window.pos = 2;
339 assert_eq!(window.past_view(4).unwrap(), &[1, 2, 3, 4]);
340 }
341
342 #[test]
343 fn check_past_view_too_long() {
344 let mut window = WindowSize::KB32.create_buffer();
345 assert_eq!(
346 window.past_view(1 << 15 + 1),
347 Err(DecodeFailed::ChunkTooLong)
348 );
349 }
350
351 #[test]
352 fn check_past_view_new_max_size() {
353 let mut window = WindowSize::KB32.create_buffer();
354 assert!(window.past_view(1 << 15).is_ok());
355 }
356
357 #[test]
358 fn check_past_view_shifted_max_size() {
359 let mut window = WindowSize::KB32.create_buffer();
360 window.pos = 123;
361 assert!(window.past_view(1 << 15).is_ok());
362 }
363 }