1 | /// A wrapper for the output slice used when decompressing. |
2 | /// |
3 | /// Using this rather than `Cursor` lets us implement the writing methods directly on |
4 | /// the buffer and lets us use a usize rather than u64 for the position which helps with |
5 | /// performance on 32-bit systems. |
6 | pub struct OutputBuffer<'a> { |
7 | slice: &'a mut [u8], |
8 | position: usize, |
9 | } |
10 | |
11 | impl<'a> OutputBuffer<'a> { |
12 | #[inline ] |
13 | pub fn from_slice_and_pos(slice: &'a mut [u8], position: usize) -> OutputBuffer<'a> { |
14 | OutputBuffer { slice, position } |
15 | } |
16 | |
17 | #[inline (always)] |
18 | pub const fn position(&self) -> usize { |
19 | self.position |
20 | } |
21 | |
22 | #[inline (always)] |
23 | pub fn set_position(&mut self, position: usize) { |
24 | self.position = position; |
25 | } |
26 | |
27 | /// Write a byte to the current position and increment |
28 | /// |
29 | /// Assumes that there is space. |
30 | #[inline ] |
31 | pub fn write_byte(&mut self, byte: u8) { |
32 | self.slice[self.position] = byte; |
33 | self.position += 1; |
34 | } |
35 | |
36 | /// Write a slice to the current position and increment |
37 | /// |
38 | /// Assumes that there is space. |
39 | #[inline ] |
40 | pub fn write_slice(&mut self, data: &[u8]) { |
41 | let len = data.len(); |
42 | self.slice[self.position..self.position + len].copy_from_slice(data); |
43 | self.position += data.len(); |
44 | } |
45 | |
46 | #[inline ] |
47 | pub const fn bytes_left(&self) -> usize { |
48 | self.slice.len() - self.position |
49 | } |
50 | |
51 | #[inline (always)] |
52 | pub const fn get_ref(&self) -> &[u8] { |
53 | self.slice |
54 | } |
55 | |
56 | #[inline (always)] |
57 | pub fn get_mut(&mut self) -> &mut [u8] { |
58 | self.slice |
59 | } |
60 | } |
61 | |
62 | /// A wrapper for the output slice used when decompressing. |
63 | /// |
64 | /// Using this rather than `Cursor` lets us implement the writing methods directly on |
65 | /// the buffer and lets us use a usize rather than u64 for the position which helps with |
66 | /// performance on 32-bit systems. |
67 | #[derive (Copy, Clone)] |
68 | pub struct InputWrapper<'a> { |
69 | slice: &'a [u8], |
70 | } |
71 | |
72 | impl<'a> InputWrapper<'a> { |
73 | #[inline (always)] |
74 | pub const fn as_slice(&self) -> &[u8] { |
75 | self.slice |
76 | } |
77 | |
78 | #[inline (always)] |
79 | pub const fn from_slice(slice: &'a [u8]) -> InputWrapper<'a> { |
80 | InputWrapper { slice } |
81 | } |
82 | |
83 | #[inline (always)] |
84 | pub fn advance(&mut self, steps: usize) { |
85 | self.slice = &self.slice[steps..]; |
86 | } |
87 | |
88 | #[inline ] |
89 | pub fn read_byte(&mut self) -> Option<u8> { |
90 | self.slice.first().map(|n| { |
91 | self.advance(1); |
92 | *n |
93 | }) |
94 | } |
95 | |
96 | #[inline ] |
97 | #[cfg (target_pointer_width = "64" )] |
98 | pub fn read_u32_le(&mut self) -> u32 { |
99 | let ret = { |
100 | let four_bytes: [u8; 4] = self.slice[..4].try_into().unwrap_or_default(); |
101 | u32::from_le_bytes(four_bytes) |
102 | }; |
103 | self.advance(4); |
104 | ret |
105 | } |
106 | |
107 | #[inline (always)] |
108 | pub const fn bytes_left(&self) -> usize { |
109 | self.slice.len() |
110 | } |
111 | } |
112 | |