| 1 | #![allow(unsafe_code)] |
| 2 | |
| 3 | use super::super::{ |
| 4 | binding, |
| 5 | binding::{LZ4DecStream, LZ4Stream}, |
| 6 | }; |
| 7 | use crate::{Error, ErrorKind, Result}; |
| 8 | |
| 9 | use std::{ |
| 10 | mem::{size_of, MaybeUninit}, |
| 11 | os::raw::{c_char, c_int, c_void}, |
| 12 | ptr::NonNull, |
| 13 | ptr::null_mut |
| 14 | }; |
| 15 | |
| 16 | #[allow(clippy::large_enum_variant)] |
| 17 | enum Stream { |
| 18 | Stack(LZ4Stream), |
| 19 | Heap(NonNull<LZ4Stream>), |
| 20 | } |
| 21 | |
| 22 | pub struct CompressionContext { |
| 23 | stream: Stream, |
| 24 | } |
| 25 | |
| 26 | unsafe impl Send for CompressionContext {} |
| 27 | |
| 28 | impl CompressionContext { |
| 29 | pub fn new() -> Result<Self> { |
| 30 | let mut stream = MaybeUninit::<LZ4Stream>::uninit(); |
| 31 | unsafe { |
| 32 | let ptr = binding::LZ4_initStream( |
| 33 | stream.as_mut_ptr() as *mut c_void, |
| 34 | size_of::<LZ4Stream>(), |
| 35 | ); |
| 36 | if !ptr.is_null() { |
| 37 | return Ok(Self { |
| 38 | stream: Stream::Stack(stream.assume_init()), |
| 39 | }); |
| 40 | } |
| 41 | NonNull::new(binding::LZ4_createStream()) |
| 42 | } |
| 43 | .ok_or_else(|| Error::new(ErrorKind::InitializationFailed)) |
| 44 | .map(|stream| Self { |
| 45 | stream: Stream::Heap(stream), |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | fn get_ptr(&mut self) -> *mut LZ4Stream { |
| 50 | match &mut self.stream { |
| 51 | Stream::Stack(stream) => stream as *mut LZ4Stream, |
| 52 | Stream::Heap(ptr) => ptr.as_ptr(), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub fn next(&mut self, src: &[u8], dst: *mut u8, dst_len: usize, acceleration: i32) -> usize { |
| 57 | unsafe { |
| 58 | binding::LZ4_compress_fast_continue( |
| 59 | self.get_ptr(), |
| 60 | src.as_ptr() as *const c_char, |
| 61 | dst as *mut c_char, |
| 62 | src.len() as c_int, |
| 63 | dst_len as c_int, |
| 64 | acceleration as c_int, |
| 65 | ) as usize |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn load_dict(&mut self, dict: &[u8]) { |
| 70 | unsafe { |
| 71 | binding::LZ4_loadDict( |
| 72 | self.get_ptr(), |
| 73 | dict.as_ptr() as *const c_char, |
| 74 | dict.len() as c_int, |
| 75 | ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub fn load_dict_slow(&mut self, dict: &[u8]) { |
| 80 | unsafe { |
| 81 | binding::LZ4_loadDictSlow( |
| 82 | self.get_ptr(), |
| 83 | dict.as_ptr() as *const c_char, |
| 84 | dict.len() as c_int, |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn save_dict(&mut self, dict: &mut [u8]) { |
| 90 | unsafe { |
| 91 | binding::LZ4_saveDict( |
| 92 | self.get_ptr(), |
| 93 | dict.as_ptr() as *mut c_char, |
| 94 | dict.len() as c_int, |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | pub fn attach_dict(&mut self, dict_stream: Option<&mut CompressionContext>) { |
| 100 | unsafe { |
| 101 | if dict_stream.is_none() { |
| 102 | // Note(sewer56): When detaching dictionary, we need to reset the stream state |
| 103 | // This behaviour is consistent with what the LZ4 library itself does internally. |
| 104 | binding::LZ4_resetStream_fast(self.get_ptr()); |
| 105 | } |
| 106 | |
| 107 | let dict_ptr = dict_stream.map(|ctx| ctx.get_ptr()).unwrap_or(null_mut()); |
| 108 | binding::LZ4_attach_dictionary(self.get_ptr(), dict_ptr); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | impl Drop for CompressionContext { |
| 114 | fn drop(&mut self) { |
| 115 | if let Stream::Heap(mut ptr) = self.stream { |
| 116 | unsafe { |
| 117 | binding::LZ4_freeStream(ptr.as_mut()); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | pub struct DecompressionContext { |
| 124 | stream: NonNull<LZ4DecStream>, |
| 125 | } |
| 126 | |
| 127 | unsafe impl Send for DecompressionContext {} |
| 128 | |
| 129 | impl DecompressionContext { |
| 130 | pub fn new() -> Result<Self> { |
| 131 | unsafe { |
| 132 | let ptr = NonNull::new(binding::LZ4_createStreamDecode()); |
| 133 | ptr.ok_or_else(|| Error::new(ErrorKind::InitializationFailed)) |
| 134 | .map(|stream| Self { stream }) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | pub fn reset(&mut self, dict: &[u8]) -> Result<()> { |
| 139 | let result = unsafe { |
| 140 | binding::LZ4_setStreamDecode( |
| 141 | self.stream.as_ptr(), |
| 142 | dict.as_ptr() as *const c_char, |
| 143 | dict.len() as c_int, |
| 144 | ) |
| 145 | }; |
| 146 | if result == 1 { |
| 147 | Ok(()) |
| 148 | } else { |
| 149 | Err(Error::new(ErrorKind::InitializationFailed)) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | pub fn decompress(&mut self, src: &[u8], dst: *mut u8, dst_len: usize) -> Result<usize> { |
| 154 | let result = unsafe { |
| 155 | binding::LZ4_decompress_safe_continue( |
| 156 | self.stream.as_ptr(), |
| 157 | src.as_ptr() as *const c_char, |
| 158 | dst as *mut c_char, |
| 159 | src.len() as c_int, |
| 160 | dst_len as c_int, |
| 161 | ) |
| 162 | }; |
| 163 | if result < 0 { |
| 164 | Err(Error::new(ErrorKind::DecompressionFailed)) |
| 165 | } else { |
| 166 | Ok(result as usize) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl Drop for DecompressionContext { |
| 172 | fn drop(&mut self) { |
| 173 | unsafe { |
| 174 | binding::LZ4_freeStreamDecode(self.stream.as_mut()); |
| 175 | } |
| 176 | } |
| 177 | } |