Debian Codegraph @

src/lz4/block/mod.rs

raw symbols SCIP
1 mod api;
2
3 use crate::{Error, ErrorKind, Result};
4 use api::ExtState;
5 use std::cmp;
6
7 /// Calculates the maximum size of the compressed output.
8 ///
9 /// If `original_size` is too large to compress, this returns `0`.
10 #[must_use]
11 pub const fn max_compressed_size(original_size: usize) -> usize {
12 api::compress_bound(original_size)
13 }
14
15 /// Performs LZ4 block compression.
16 ///
17 /// Ensure that the destination slice has enough capacity.
18 /// If `dst.len()` is smaller than `lz4::max_compressed_size(src.len())`,
19 /// this function may fail.
20 ///
21 /// Returns the number of bytes written into the destination buffer.
22 ///
23 /// # Example
24 ///
25 /// ```
26 /// use lzzzz::lz4;
27 ///
28 /// let data = b"The quick brown fox jumps over the lazy dog.";
29 /// let mut buf = [0u8; 256];
30 ///
31 /// // The slice should have enough capacity.
32 /// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
33 ///
34 /// let len = lz4::compress(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
35 /// let compressed = &buf[..len];
36 ///
37 /// # let mut buf = [0u8; 256];
38 /// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
39 /// # assert_eq!(&buf[..len], &data[..]);
40 /// # Ok::<(), std::io::Error>(())
41 /// ```
42 pub fn compress(src: &[u8], dst: &mut [u8], acc: i32) -> Result<usize> {
43 compress_to_ptr(src, dst.as_mut_ptr(), dst.len(), acc)
44 }
45
46 fn compress_to_ptr(src: &[u8], dst: *mut u8, dst_len: usize, acc: i32) -> Result<usize> {
47 if src.is_empty() {
48 return Ok(0);
49 }
50
51 let acc = cmp::min(acc, 33_554_431);
52
53 let len = ExtState::with(|state, reset| {
54 let mut state = state.borrow_mut();
55 if reset {
56 api::compress_fast_ext_state_fast_reset(&mut state, src, dst, dst_len, acc)
57 } else {
58 api::compress_fast_ext_state(&mut state, src, dst, dst_len, acc)
59 }
60 });
61 if len > 0 {
62 Ok(len)
63 } else {
64 Err(Error::new(ErrorKind::CompressionFailed))
65 }
66 }
67
68 /// Appends compressed data to `Vec<u8>`.
69 ///
70 /// Returns the number of bytes appended to the given `Vec<u8>`.
71 ///
72 /// # Example
73 ///
74 /// ```
75 /// use lzzzz::lz4;
76 ///
77 /// let data = b"The quick brown fox jumps over the lazy dog.";
78 /// let mut buf = Vec::new();
79 ///
80 /// lz4::compress_to_vec(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
81 /// # let compressed = &buf;
82 /// # let mut buf = [0u8; 256];
83 /// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
84 /// # assert_eq!(&buf[..len], &data[..]);
85 /// # Ok::<(), std::io::Error>(())
86 /// ```
87 pub fn compress_to_vec(src: &[u8], dst: &mut Vec<u8>, acc: i32) -> Result<usize> {
88 let orig_len = dst.len();
89 dst.reserve(max_compressed_size(src.len()));
90 #[allow(unsafe_code)]
91 unsafe {
92 let result = compress_to_ptr(
93 src,
94 dst.as_mut_ptr().add(orig_len),
95 dst.capacity() - orig_len,
96 acc,
97 );
98 dst.set_len(orig_len + result.as_ref().unwrap_or(&0));
99 result
100 }
101 }
102
103 /// Compress data to fill `dst`.
104 ///
105 /// This function either compresses the entire `src` buffer into `dst` if it's
106 /// large enough, or will fill `dst` with as much data as possible from `src`.
107 ///
108 /// Returns a pair `(read, wrote)` giving the number of bytes read from `src`
109 /// and the number of bytes written to `dst`.
110 ///
111 /// # Example
112 ///
113 /// ```
114 /// use lzzzz::lz4;
115 ///
116 /// let data = b"The quick brown fox jumps over the lazy dog.";
117 /// let mut buf = [0u8; 256];
118 ///
119 /// // This slice should have enough capacity.
120 /// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
121 ///
122 /// let (read, wrote) = lz4::compress_fill(data, &mut buf)?;
123 /// assert_eq!(read, data.len());
124 /// let compressed = &buf[..wrote];
125 ///
126 /// # let mut buf = [0u8; 256];
127 /// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
128 /// # assert_eq!(&buf[..len], &data[..]);
129 ///
130 /// // This slice doesn't have enough capacity, but we can fill it.
131 /// let mut smallbuf = [0u8; 32];
132 /// assert!(smallbuf.len() < lz4::max_compressed_size(data.len()));
133 ///
134 /// let (read, wrote) = lz4::compress_fill(data, &mut smallbuf)?;
135 /// assert_eq!(wrote, smallbuf.len());
136 /// let remaining_data = &data[read..];
137 ///
138 /// # let mut buf = [0u8; 256];
139 /// # let len = lz4::decompress(&smallbuf, &mut buf)?;
140 /// # assert_eq!(&buf[..len], &data[..read]);
141 /// # Ok::<(), std::io::Error>(())
142 /// ```
143 pub fn compress_fill(src: &[u8], dst: &mut [u8]) -> Result<(usize, usize)> {
144 api::compress_dest_size(src, dst)
145 }
146
147 /// Decompresses an LZ4 block.
148 ///
149 /// The length of the destination slice must be equal to the original data length.
150 ///
151 /// Returns the number of bytes written into the destination buffer.
152 ///
153 /// # Example
154 ///
155 /// ```
156 /// use lzzzz::lz4;
157 ///
158 /// const ORIGINAL_SIZE: usize = 44;
159 /// const COMPRESSED_DATA: &str =
160 /// "8B1UaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLg==";
161 ///
162 /// let data = base64::decode(COMPRESSED_DATA).unwrap();
163 /// let mut buf = [0u8; ORIGINAL_SIZE];
164 ///
165 /// lz4::decompress(&data[..], &mut buf[..])?;
166 ///
167 /// assert_eq!(
168 /// &buf[..],
169 /// &b"The quick brown fox jumps over the lazy dog."[..]
170 /// );
171 /// # Ok::<(), std::io::Error>(())
172 /// ```
173 pub fn decompress(src: &[u8], dst: &mut [u8]) -> Result<usize> {
174 api::decompress_safe(src, dst)
175 }
176
177 /// Decompresses an LZ4 block until the destination slice fills up.
178 ///
179 /// Returns the number of bytes written into the destination buffer.
180 ///
181 /// # Example
182 ///
183 /// ```
184 /// use lzzzz::lz4;
185 ///
186 /// const ORIGINAL_SIZE: usize = 44;
187 /// const COMPRESSED_DATA: &str =
188 /// "8B1UaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLg==";
189 ///
190 /// let data = base64::decode(COMPRESSED_DATA).unwrap();
191 /// let mut buf = [0u8; 24];
192 ///
193 /// lz4::decompress_partial(&data[..], &mut buf[..], ORIGINAL_SIZE)?;
194 ///
195 /// assert_eq!(&buf[..], &b"The quick brown fox jump"[..]);
196 /// # Ok::<(), std::io::Error>(())
197 /// ```
198 pub fn decompress_partial(src: &[u8], dst: &mut [u8], original_size: usize) -> Result<usize> {
199 api::decompress_safe_partial(src, dst, original_size)
200 }
201
202 /// Decompresses an LZ4 block with a dictionary.
203 ///
204 /// Returns the number of bytes written into the destination buffer.
205 ///
206 /// # Example
207 ///
208 /// ```
209 /// use lzzzz::lz4;
210 ///
211 /// const ORIGINAL_SIZE: usize = 44;
212 /// const COMPRESSED_DATA: &str = "DywAFFAgZG9nLg==";
213 /// const DICT_DATA: &[u8] = b"The quick brown fox jumps over the lazy cat.";
214 ///
215 /// let data = base64::decode(COMPRESSED_DATA).unwrap();
216 /// let mut buf = [0u8; ORIGINAL_SIZE];
217 ///
218 /// lz4::decompress_with_dict(&data[..], &mut buf[..], DICT_DATA)?;
219 ///
220 /// assert_eq!(
221 /// &buf[..],
222 /// &b"The quick brown fox jumps over the lazy dog."[..]
223 /// );
224 /// # Ok::<(), std::io::Error>(())
225 /// ```
226 pub fn decompress_with_dict(src: &[u8], dst: &mut [u8], dict: &[u8]) -> Result<usize> {
227 api::decompress_safe_using_dict(src, dst, dict)
228 }
229
230 /// Decompresses an LZ4 block with a dictionary until the destination slice fills up.
231 ///
232 /// Returns the number of bytes written into the destination buffer.
233 ///
234 /// # Example
235 ///
236 /// ```
237 /// use lzzzz::lz4;
238 ///
239 /// const ORIGINAL_SIZE: usize = 44;
240 /// const COMPRESSED_DATA: &str = "DywAFFAgZG9nLg==";
241 /// const DICT_DATA: &[u8] = b"The quick brown fox jumps over the lazy cat.";
242 ///
243 /// let data = base64::decode(COMPRESSED_DATA).unwrap();
244 /// let mut buf = [0u8; 24];
245 ///
246 /// lz4::decompress_partial_with_dict(&data[..], &mut buf[..], ORIGINAL_SIZE, DICT_DATA)?;
247 ///
248 /// assert_eq!(
249 /// &buf[..],
250 /// &b"The quick brown fox jump"[..]
251 /// );
252 /// # Ok::<(), std::io::Error>(())
253 /// ```
254 pub fn decompress_partial_with_dict(
255 src: &[u8],
256 dst: &mut [u8],
257 original_size: usize,
258 dict: &[u8],
259 ) -> Result<usize> {
260 api::decompress_safe_partial_using_dict(src, dst, original_size, dict)
261 }