| 1 | mod apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/; |
| 2 | |
| 3 | use craterust-analyzer cargo lzzzz 2.0.0 crate/::{Errorrust-analyzer cargo lzzzz 2.0.0 common/error/Error#, ErrorKindrust-analyzer cargo lzzzz 2.0.0 common/error/ErrorKind#, Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#}; |
| 4 | use apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::ExtStaterust-analyzer cargo lzzzz 2.0.0 lz4/block/api/ExtState#; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core 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_sizerust-analyzer cargo lzzzz 2.0.0 lz4/block/max_compressed_size().(original_size: usize) -> usize { |
| 12 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::compress_boundrust-analyzer cargo lzzzz 2.0.0 lz4/block/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 compressrust-analyzer cargo lzzzz 2.0.0 lz4/block/compress().(src: &[u8], dst: &mut [u8], acc: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 43 | compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4/block/compress_to_ptr().(src, dst.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(), dst.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), acc) |
| 44 | } |
| 45 | |
| 46 | fn compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4/block/compress_to_ptr().(src: &[u8], dst: *mut u8, dst_len: usize, acc: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 47 | if src.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 48 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 49 | } |
| 50 | |
| 51 | let acc = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(acc, 33_554_431); |
| 52 | |
| 53 | let len = ExtStaterust-analyzer cargo lzzzz 2.0.0 lz4/block/api/ExtState#::withrust-analyzer cargo lzzzz 2.0.0 lz4/block/api/impl#[ExtState]with().(|state, reset| { |
| 54 | let mut state = state.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(); |
| 55 | if reset { |
| 56 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::compress_fast_ext_state_fast_resetrust-analyzer cargo lzzzz 2.0.0 lz4/block/api/compress_fast_ext_state_fast_reset().(&mut state, src, dst, dst_len, acc) |
| 57 | } else { |
| 58 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::compress_fast_ext_staterust-analyzer cargo lzzzz 2.0.0 lz4/block/api/compress_fast_ext_state().(&mut state, src, dst, dst_len, acc) |
| 59 | } |
| 60 | }); |
| 61 | if len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 62 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(len) |
| 63 | } else { |
| 64 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo lzzzz 2.0.0 common/error/Error#::newrust-analyzer cargo lzzzz 2.0.0 common/error/impl#[Error]new().(ErrorKindrust-analyzer cargo lzzzz 2.0.0 common/error/ErrorKind#::CompressionFailedrust-analyzer cargo lzzzz 2.0.0 common/error/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_vecrust-analyzer cargo lzzzz 2.0.0 lz4/block/compress_to_vec().(src: &[u8], dst: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, acc: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 88 | let orig_len = dst.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 89 | dst.reserverust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]reserve().(max_compressed_sizerust-analyzer cargo lzzzz 2.0.0 lz4/block/max_compressed_size().(src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())); |
| 90 | #[allow(unsafe_code)] |
| 91 | unsafe { |
| 92 | let result = compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4/block/compress_to_ptr().( |
| 93 | src, |
| 94 | dst.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(orig_len), |
| 95 | dst.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().orig_len, |
| 96 | acc, |
| 97 | ); |
| 98 | dst.set_lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]set_len().(orig_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<&usize>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<&usize>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<&usize>`]add().result.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]as_ref().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]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_fillrust-analyzer cargo lzzzz 2.0.0 lz4/block/compress_fill().(src: &[u8], dst: &mut [u8]) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<(usize, usize)> { |
| 144 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::compress_dest_sizerust-analyzer cargo lzzzz 2.0.0 lz4/block/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 decompressrust-analyzer cargo lzzzz 2.0.0 lz4/block/decompress().(src: &[u8], dst: &mut [u8]) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 174 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::decompress_saferust-analyzer cargo lzzzz 2.0.0 lz4/block/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_partialrust-analyzer cargo lzzzz 2.0.0 lz4/block/decompress_partial().(src: &[u8], dst: &mut [u8], original_size: usize) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 199 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::decompress_safe_partialrust-analyzer cargo lzzzz 2.0.0 lz4/block/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_dictrust-analyzer cargo lzzzz 2.0.0 lz4/block/decompress_with_dict().(src: &[u8], dst: &mut [u8], dict: &[u8]) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 227 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::decompress_safe_using_dictrust-analyzer cargo lzzzz 2.0.0 lz4/block/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_dictrust-analyzer cargo lzzzz 2.0.0 lz4/block/decompress_partial_with_dict().( |
| 255 | src: &[u8], |
| 256 | dst: &mut [u8], |
| 257 | original_size: usize, |
| 258 | dict: &[u8], |
| 259 | ) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 260 | apirust-analyzer cargo lzzzz 2.0.0 lz4/block/api/::decompress_safe_partial_using_dictrust-analyzer cargo lzzzz 2.0.0 lz4/block/api/decompress_safe_partial_using_dict().(src, dst, original_size, dict) |
| 261 | } |