Debian Codegraph @

third_party/rust/zlib-rs/src/inflate/window.rs

raw
1 use crate::{
2 adler32::{adler32, adler32_fold_copy},
3 allocate::Allocator,
4 crc32::Crc32Fold,
5 weak_slice::WeakSliceMut,
6 };
7
8 // translation guide:
9 //
10 // wsize -> buf.capacity()
11 // wnext -> buf.ptr
12 // whave -> buf.filled.len()
13 #[derive(Debug)]
14 pub struct Window<'a> {
15 buf: WeakSliceMut<'a, u8>,
16
17 have: usize, // number of bytes logically written to the window. this can be higher than
18 // buf.len() if we run out of space in the window
19 next: usize, // write head
20 }
21
22 impl<'a> Window<'a> {
23 pub fn into_raw_parts(self) -> (*mut u8, usize) {
24 self.buf.into_raw_parts()
25 }
26
27 pub fn is_empty(&self) -> bool {
28 self.size() == 0
29 }
30
31 pub fn size(&self) -> usize {
32 // `self.len == 0` is used for uninitialized buffers
33 assert!(self.buf.is_empty() || self.buf.len() >= Self::padding());
34 self.buf.len().saturating_sub(Self::padding())
35 }
36
37 /// number of bytes in the window. Saturates at `Self::capacity`.
38 pub fn have(&self) -> usize {
39 self.have
40 }
41
42 /// Position where the next byte will be written
43 pub fn next(&self) -> usize {
44 self.next
45 }
46
47 pub fn empty() -> Self {
48 Self {
49 buf: WeakSliceMut::empty(),
50 have: 0,
51 next: 0,
52 }
53 }
54
55 pub fn clear(&mut self) {
56 self.have = 0;
57 self.next = 0;
58 }
59
60 pub fn as_slice(&self) -> &[u8] {
61 &self.buf.as_slice()[..self.have]
62 }
63
64 pub fn as_ptr(&self) -> *const u8 {
65 self.buf.as_ptr()
66 }
67
68 #[cfg(test)]
69 fn extend_adler32(&mut self, slice: &[u8], checksum: &mut u32) {
70 self.extend(slice, 0, true, checksum, &mut Crc32Fold::new());
71 }
72
73 pub fn extend(
74 &mut self,
75 slice: &[u8],
76 flags: i32,
77 update_checksum: bool,
78 checksum: &mut u32,
79 crc_fold: &mut Crc32Fold,
80 ) {
81 let len = slice.len();
82 let wsize = self.size();
83
84 if len >= wsize {
85 // We have to split the checksum over non-copied and copied bytes
86 let pos = len.saturating_sub(self.size());
87 let (non_window_slice, window_slice) = slice.split_at(pos);
88
89 if update_checksum {
90 if flags != 0 {
91 crc_fold.fold(non_window_slice, 0);
92 crc_fold.fold_copy(&mut self.buf.as_mut_slice()[..wsize], window_slice);
93 } else {
94 *checksum = adler32(*checksum, non_window_slice);
95 *checksum = adler32_fold_copy(*checksum, self.buf.as_mut_slice(), window_slice);
96 }
97 } else {
98 self.buf.as_mut_slice()[..wsize].copy_from_slice(window_slice);
99 }
100
101 self.next = 0;
102 self.have = self.size();
103 } else {
104 let dist = Ord::min(wsize - self.next, slice.len());
105
106 // the end part goes onto the end of the window. The start part wraps around and is
107 // written to the start of the window.
108 let (end_part, start_part) = slice.split_at(dist);
109
110 if update_checksum {
111 let dst = &mut self.buf.as_mut_slice()[self.next..][..end_part.len()];
112 if flags != 0 {
113 crc_fold.fold_copy(dst, end_part);
114 } else {
115 *checksum = adler32_fold_copy(*checksum, dst, end_part);
116 }
117 } else {
118 self.buf.as_mut_slice()[self.next..][..end_part.len()].copy_from_slice(end_part);
119 }
120
121 if !start_part.is_empty() {
122 let dst = &mut self.buf.as_mut_slice()[..start_part.len()];
123
124 if update_checksum {
125 if flags != 0 {
126 crc_fold.fold_copy(dst, start_part);
127 } else {
128 *checksum = adler32_fold_copy(*checksum, dst, start_part);
129 }
130 } else {
131 dst.copy_from_slice(start_part);
132 }
133
134 self.next = start_part.len();
135 self.have = self.size();
136 } else {
137 self.next += dist;
138 if self.next == self.size() {
139 self.next = 0;
140 }
141 if self.have < self.size() {
142 self.have += dist;
143 }
144 }
145 }
146 }
147
148 pub fn new_in(alloc: &Allocator<'a>, window_bits: usize) -> Option<Self> {
149 let len = (1 << window_bits) + Self::padding();
150 let ptr = alloc.allocate_zeroed(len)?;
151
152 Some(Self {
153 buf: unsafe { WeakSliceMut::from_raw_parts_mut(ptr.as_ptr(), len) },
154 have: 0,
155 next: 0,
156 })
157 }
158
159 pub fn clone_in(&self, alloc: &Allocator<'a>) -> Option<Self> {
160 let len = self.buf.len();
161 let ptr = alloc.allocate_zeroed(len)?;
162
163 Some(Self {
164 buf: unsafe { WeakSliceMut::from_raw_parts_mut(ptr.as_ptr(), len) },
165 have: self.have,
166 next: self.next,
167 })
168 }
169
170 // padding required so that SIMD operations going out-of-bounds are not a problem
171 pub fn padding() -> usize {
172 64 // very conservative
173 }
174 }
175
176 #[cfg(test)]
177 mod test {
178 use super::*;
179
180 use crate::allocate::Allocator;
181
182 fn init_window(window_bits_log2: usize) -> Window<'static> {
183 let mut window = Window::new_in(&Allocator::RUST, window_bits_log2).unwrap();
184 window.have = 0;
185 window.next = 0;
186 window
187 }
188
189 #[test]
190 fn extend_in_bounds() {
191 let mut checksum = 0;
192
193 let mut window = init_window(4);
194
195 window.extend_adler32(&[1; 5], &mut checksum);
196 assert_eq!(window.have, 5);
197 assert_eq!(window.next, 5);
198
199 let slice = &window.buf.as_slice()[..window.size()];
200 assert_eq!(&[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], slice);
201
202 window.extend_adler32(&[2; 7], &mut checksum);
203 assert_eq!(window.have, 12);
204 assert_eq!(window.next, 12);
205
206 let slice = &window.buf.as_slice()[..window.size()];
207 assert_eq!(&[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0], slice);
208
209 assert_eq!(checksum, 6946835);
210
211 unsafe {
212 Allocator::RUST.deallocate(
213 window.buf.as_mut_slice().as_mut_ptr(),
214 window.buf.as_slice().len(),
215 )
216 }
217 }
218
219 #[test]
220 fn extend_crosses_bounds() {
221 let mut checksum = 0;
222
223 let mut window = init_window(2);
224
225 window.extend_adler32(&[1; 3], &mut checksum);
226 assert_eq!(window.have, 3);
227 assert_eq!(window.next, 3);
228
229 let slice = &window.buf.as_slice()[..window.size()];
230 assert_eq!(&[1, 1, 1, 0], slice);
231
232 window.extend_adler32(&[2; 3], &mut checksum);
233 assert_eq!(window.have, 4);
234 assert_eq!(window.next, 2);
235
236 let slice = &window.buf.as_slice()[..window.size()];
237 assert_eq!(&[2, 2, 1, 2], slice);
238
239 assert_eq!(checksum, 1769481);
240
241 unsafe {
242 Allocator::RUST.deallocate(
243 window.buf.as_mut_slice().as_mut_ptr(),
244 window.buf.as_slice().len(),
245 )
246 }
247 }
248
249 #[test]
250 fn extend_out_of_bounds() {
251 let mut checksum = 0;
252
253 let mut window = init_window(3);
254
255 // adds 9 numbers, that won't fit into a window of size 8
256 window.extend_adler32(&[1, 2, 3, 4, 5, 6, 7, 8, 9], &mut checksum);
257 assert_eq!(window.have, 8);
258 assert_eq!(window.next, 0);
259
260 let slice = &window.as_slice()[..window.size()];
261 assert_eq!(&[2, 3, 4, 5, 6, 7, 8, 9], slice);
262
263 assert_eq!(checksum, 10813485);
264
265 unsafe {
266 Allocator::RUST.deallocate(
267 window.buf.as_mut_slice().as_mut_ptr(),
268 window.as_slice().len(),
269 )
270 }
271 }
272 }