Debian Codegraph @

debian/rust-vendor/zlib-rs/src/inflate/window.rs

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