| 1 | mod apirust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/; |
| 2 | |
| 3 | use craterust-analyzer cargo lzzzz 2.0.0 crate/::{lz4rust-analyzer cargo lzzzz 2.0.0 lz4/, 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_hc/block/api/::ExtStaterust-analyzer cargo lzzzz 2.0.0 lz4_hc/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/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#}; |
| 6 | |
| 7 | /// Performs LZ4_HC block compression. |
| 8 | /// |
| 9 | /// Ensure that the destination slice has enough capacity. |
| 10 | /// If `dst.len()` is smaller than `lz4::max_compressed_size(src.len())`, |
| 11 | /// this function may fail. |
| 12 | /// |
| 13 | /// Returns the number of bytes written into the destination buffer. |
| 14 | /// |
| 15 | /// # Example |
| 16 | /// |
| 17 | /// ``` |
| 18 | /// use lzzzz::{lz4, lz4_hc}; |
| 19 | /// |
| 20 | /// let data = b"The quick brown fox jumps over the lazy dog."; |
| 21 | /// let mut buf = [0u8; 256]; |
| 22 | /// |
| 23 | /// // The slice should have enough capacity. |
| 24 | /// assert!(buf.len() >= lz4::max_compressed_size(data.len())); |
| 25 | /// |
| 26 | /// let len = lz4_hc::compress(data, &mut buf, lz4_hc::CLEVEL_DEFAULT)?; |
| 27 | /// let compressed = &buf[..len]; |
| 28 | /// |
| 29 | /// # let mut buf = [0u8; 256]; |
| 30 | /// # let len = lz4::decompress( |
| 31 | /// # compressed, |
| 32 | /// # &mut buf[..data.len()], |
| 33 | /// # )?; |
| 34 | /// # assert_eq!(&buf[..len], &data[..]); |
| 35 | /// # Ok::<(), std::io::Error>(()) |
| 36 | /// ``` |
| 37 | pub fn compressrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/compress().(src: &[u8], dst: &mut [u8], level: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 38 | compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4_hc/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().(), level) |
| 39 | } |
| 40 | |
| 41 | fn compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/compress_to_ptr().(src: &[u8], dst: *mut u8, dst_len: usize, level: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 42 | if src.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 43 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 44 | } |
| 45 | let len = ExtStaterust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/ExtState#::withrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/impl#[ExtState]with().(|state, reset| { |
| 46 | if reset { |
| 47 | apirust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/::compress_ext_state_fast_resetrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/compress_ext_state_fast_reset().(&mut state.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(), src, dst, dst_len, level) |
| 48 | } else { |
| 49 | apirust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/::compress_ext_staterust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/compress_ext_state().(&mut state.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(), src, dst, dst_len, level) |
| 50 | } |
| 51 | }); |
| 52 | 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 { |
| 53 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(len) |
| 54 | } else { |
| 55 | 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#)) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// Compress data to fill `dst`. |
| 60 | /// |
| 61 | /// This function either compresses the entire `src` buffer into `dst` if it's |
| 62 | /// large enough, or will fill `dst` with as much data as possible from `src`. |
| 63 | /// |
| 64 | /// Returns a pair `(read, wrote)` giving the number of bytes read from `src` |
| 65 | /// and the number of bytes written to `dst`. |
| 66 | /// |
| 67 | /// # Example |
| 68 | /// |
| 69 | /// ``` |
| 70 | /// use lzzzz::{lz4, lz4_hc}; |
| 71 | /// |
| 72 | /// let data = b"The quick brown fox jumps over the lazy dog."; |
| 73 | /// let mut buf = [0u8; 256]; |
| 74 | /// |
| 75 | /// // This slice should have enough capacity. |
| 76 | /// assert!(buf.len() >= lz4::max_compressed_size(data.len())); |
| 77 | /// |
| 78 | /// let (read, wrote) = lz4_hc::compress_fill(data, &mut buf, lz4_hc::CLEVEL_DEFAULT)?; |
| 79 | /// assert_eq!(read, data.len()); |
| 80 | /// let compressed = &buf[..wrote]; |
| 81 | /// |
| 82 | /// # let mut buf = [0u8; 256]; |
| 83 | /// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?; |
| 84 | /// # assert_eq!(&buf[..len], &data[..]); |
| 85 | /// |
| 86 | /// // This slice doesn't have enough capacity, but we can fill it. |
| 87 | /// let mut smallbuf = [0u8; 32]; |
| 88 | /// assert!(smallbuf.len() < lz4::max_compressed_size(data.len())); |
| 89 | /// |
| 90 | /// let (read, wrote) = lz4_hc::compress_fill(data, &mut smallbuf, lz4_hc::CLEVEL_DEFAULT)?; |
| 91 | /// assert_eq!(wrote, smallbuf.len()); |
| 92 | /// let remaining_data = &data[read..]; |
| 93 | /// |
| 94 | /// # let mut buf = [0u8; 256]; |
| 95 | /// # let len = lz4::decompress(&smallbuf, &mut buf)?; |
| 96 | /// # assert_eq!(&buf[..len], &data[..read]); |
| 97 | /// # Ok::<(), std::io::Error>(()) |
| 98 | /// ``` |
| 99 | pub fn compress_fillrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/compress_fill().(src: &[u8], dst: &mut [u8], level: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<(usize, usize)> { |
| 100 | if src.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 101 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((0, 0)); |
| 102 | } |
| 103 | let (read, wrote) = ExtStaterust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/ExtState#::withrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/impl#[ExtState]with().(|state, _reset| { |
| 104 | apirust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/::compress_dest_sizerust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/compress_dest_size().(&mut state.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(), src, dst, level) |
| 105 | }); |
| 106 | if wrote 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 { |
| 107 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((read, wrote)) |
| 108 | } else { |
| 109 | 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#)) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Compresses data until the destination slice fills up. |
| 114 | /// |
| 115 | /// Returns the number of bytes written into the destination buffer. |
| 116 | /// |
| 117 | /// # Example |
| 118 | /// |
| 119 | /// ``` |
| 120 | /// use lzzzz::{lz4, lz4_hc}; |
| 121 | /// use std::io::Cursor; |
| 122 | /// |
| 123 | /// let data = b"The quick brown fox jumps over the lazy dog."; |
| 124 | /// let mut buf = [0u8; 16]; |
| 125 | /// |
| 126 | /// let mut src = Cursor::new(&data[..]); |
| 127 | /// let len = lz4_hc::compress_partial(&mut src, &mut buf, lz4_hc::CLEVEL_DEFAULT)?; |
| 128 | /// let compressed = &buf[..len]; |
| 129 | /// |
| 130 | /// # let mut buf = [0u8; 256]; |
| 131 | /// # let len = lz4::decompress( |
| 132 | /// # compressed, |
| 133 | /// # &mut buf[..data.len()], |
| 134 | /// # )?; |
| 135 | /// # assert_eq!(&buf[..len], &data[..src.position() as usize]); |
| 136 | /// # Ok::<(), std::io::Error>(()) |
| 137 | /// ``` |
| 138 | #[deprecated(since = "1.1.0", note = "Use compress_fill instead.")] |
| 139 | pub fn compress_partialrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/compress_partial().<T>(src: &mut Cursorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/Cursor#<T>, dst: &mut [u8], level: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> |
| 140 | where |
| 141 | T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, |
| 142 | { |
| 143 | let src_ref = src.get_refrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]get_ref().().as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(); |
| 144 | let pos = 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().(src_ref.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), src.positionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]position().() as usize); |
| 145 | let src_ref = &src_ref[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 146 | if src_ref.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() || dst.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 147 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0); |
| 148 | } |
| 149 | let (src_len, dst_len) = ExtStaterust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/ExtState#::withrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/impl#[ExtState]with().(|state, _| { |
| 150 | apirust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/::compress_dest_sizerust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/api/compress_dest_size().(&mut state.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().(), src_ref, dst, level) |
| 151 | }); |
| 152 | src.set_positionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]set_position().(src.positionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/cursor/impl#[`Cursor<T>`]position().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().src_len as u64); |
| 153 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(dst_len) |
| 154 | } |
| 155 | |
| 156 | /// Appends compressed data to `Vec<u8>`. |
| 157 | /// |
| 158 | /// Returns the number of bytes appended to the given `Vec<u8>`. |
| 159 | /// |
| 160 | /// # Example |
| 161 | /// |
| 162 | /// ``` |
| 163 | /// use lzzzz::{lz4, lz4_hc}; |
| 164 | /// |
| 165 | /// let data = b"The quick brown fox jumps over the lazy dog."; |
| 166 | /// let mut buf = Vec::new(); |
| 167 | /// |
| 168 | /// lz4_hc::compress_to_vec(data, &mut buf, lz4_hc::CLEVEL_DEFAULT)?; |
| 169 | /// |
| 170 | /// # let compressed = &buf; |
| 171 | /// # let mut buf = [0u8; 256]; |
| 172 | /// # let len = lz4::decompress( |
| 173 | /// # compressed, |
| 174 | /// # &mut buf[..data.len()], |
| 175 | /// # )?; |
| 176 | /// # assert_eq!(&buf[..len], &data[..]); |
| 177 | /// # Ok::<(), std::io::Error>(()) |
| 178 | /// ``` |
| 179 | pub fn compress_to_vecrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/compress_to_vec().(src: &[u8], dst: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, level: i32) -> Resultrust-analyzer cargo lzzzz 2.0.0 common/error/Result#<usize> { |
| 180 | let orig_len = dst.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 181 | dst.reserverust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]reserve().(lz4rust-analyzer cargo lzzzz 2.0.0 lz4/::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().())); |
| 182 | #[allow(unsafe_code)] |
| 183 | unsafe { |
| 184 | let result = compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4_hc/block/compress_to_ptr().( |
| 185 | src, |
| 186 | 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), |
| 187 | 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, |
| 188 | level, |
| 189 | ); |
| 190 | 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)); |
| 191 | result |
| 192 | } |
| 193 | } |