| 1 | mod api; |
| 2 | |
| 3 | use crate::{lz4, Error, ErrorKind, Result}; |
| 4 | use api::ExtState; |
| 5 | use std::{cmp, io::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 compress(src: &[u8], dst: &mut [u8], level: i32) -> Result<usize> { |
| 38 | compress_to_ptr(src, dst.as_mut_ptr(), dst.len(), level) |
| 39 | } |
| 40 | |
| 41 | fn compress_to_ptr(src: &[u8], dst: *mut u8, dst_len: usize, level: i32) -> Result<usize> { |
| 42 | if src.is_empty() { |
| 43 | return Ok(0); |
| 44 | } |
| 45 | let len = ExtState::with(|state, reset| { |
| 46 | if reset { |
| 47 | api::compress_ext_state_fast_reset(&mut state.borrow_mut(), src, dst, dst_len, level) |
| 48 | } else { |
| 49 | api::compress_ext_state(&mut state.borrow_mut(), src, dst, dst_len, level) |
| 50 | } |
| 51 | }); |
| 52 | if len > 0 { |
| 53 | Ok(len) |
| 54 | } else { |
| 55 | Err(Error::new(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_fill(src: &[u8], dst: &mut [u8], level: i32) -> Result<(usize, usize)> { |
| 100 | if src.is_empty() { |
| 101 | return Ok((0, 0)); |
| 102 | } |
| 103 | let (read, wrote) = ExtState::with(|state, _reset| { |
| 104 | api::compress_dest_size(&mut state.borrow_mut(), src, dst, level) |
| 105 | }); |
| 106 | if wrote > 0 { |
| 107 | Ok((read, wrote)) |
| 108 | } else { |
| 109 | Err(Error::new(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_partial<T>(src: &mut Cursor<T>, dst: &mut [u8], level: i32) -> Result<usize> |
| 140 | where |
| 141 | T: AsRef<[u8]>, |
| 142 | { |
| 143 | let src_ref = src.get_ref().as_ref(); |
| 144 | let pos = cmp::min(src_ref.len(), src.position() as usize); |
| 145 | let src_ref = &src_ref[pos..]; |
| 146 | if src_ref.is_empty() || dst.is_empty() { |
| 147 | return Ok(0); |
| 148 | } |
| 149 | let (src_len, dst_len) = ExtState::with(|state, _| { |
| 150 | api::compress_dest_size(&mut state.borrow_mut(), src_ref, dst, level) |
| 151 | }); |
| 152 | src.set_position(src.position() + src_len as u64); |
| 153 | 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_vec(src: &[u8], dst: &mut Vec<u8>, level: i32) -> Result<usize> { |
| 180 | let orig_len = dst.len(); |
| 181 | dst.reserve(lz4::max_compressed_size(src.len())); |
| 182 | #[allow(unsafe_code)] |
| 183 | unsafe { |
| 184 | let result = compress_to_ptr( |
| 185 | src, |
| 186 | dst.as_mut_ptr().add(orig_len), |
| 187 | dst.capacity() - orig_len, |
| 188 | level, |
| 189 | ); |
| 190 | dst.set_len(orig_len + result.as_ref().unwrap_or(&0)); |
| 191 | result |
| 192 | } |
| 193 | } |