Debian Codegraph @

src/lz4f/api.rs

raw symbols SCIP
1 #![allow(unsafe_code)]
2
3 use super::{
4 binding,
5 binding::{
6 LZ4FCompressionCtx, LZ4FCompressionDict, LZ4FCompressionOptions, LZ4FDecompressionCtx,
7 LZ4FDecompressionOptions,
8 },
9 Dictionary,
10 };
11 use crate::lz4f::{Error, ErrorKind, FrameInfo, Preferences, Result};
12
13 use std::{mem::MaybeUninit, os::raw::c_void, ptr::NonNull};
14
15 pub const LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH: usize = 5;
16 pub const LZ4F_HEADER_SIZE_MAX: usize = 19;
17
18 pub struct CompressionContext {
19 ctx: NonNull<LZ4FCompressionCtx>,
20 dict: Option<Dictionary>,
21 }
22
23 unsafe impl Send for CompressionContext {}
24
25 impl CompressionContext {
26 pub fn new(dict: Option<Dictionary>) -> Result<Self> {
27 let ctx = MaybeUninit::<*mut LZ4FCompressionCtx>::uninit();
28 unsafe {
29 let code = binding::LZ4F_createCompressionContext(
30 ctx.as_ptr() as *mut *mut binding::LZ4FCompressionCtx,
31 binding::LZ4F_getVersion(),
32 );
33 result_from_code(code).and_then(|_| {
34 Ok(Self {
35 ctx: NonNull::new(ctx.assume_init())
36 .ok_or_else(|| crate::Error::new(crate::ErrorKind::InitializationFailed))?,
37 dict,
38 })
39 })
40 }
41 }
42
43 pub fn begin(&mut self, dst: *mut u8, dst_len: usize, prefs: &Preferences) -> Result<usize> {
44 let code = unsafe {
45 if let Some(dict) = &self.dict {
46 binding::LZ4F_compressBegin_usingCDict(
47 self.ctx.as_ptr(),
48 dst as *mut c_void,
49 dst_len,
50 dict.handle().0.as_ptr(),
51 prefs as *const Preferences,
52 )
53 } else {
54 binding::LZ4F_compressBegin(self.ctx.as_ptr(), dst as *mut c_void, dst_len, prefs)
55 }
56 };
57 result_from_code(code).map(|_| code)
58 }
59
60 pub fn update(
61 &mut self,
62 dst: *mut u8,
63 dst_len: usize,
64 src: &[u8],
65 stable_src: bool,
66 ) -> Result<usize> {
67 let opt = LZ4FCompressionOptions::stable(stable_src);
68 let code = unsafe {
69 binding::LZ4F_compressUpdate(
70 self.ctx.as_ptr(),
71 dst as *mut c_void,
72 dst_len,
73 src.as_ptr() as *const c_void,
74 src.len(),
75 &opt as *const LZ4FCompressionOptions,
76 )
77 };
78 result_from_code(code).map(|_| code)
79 }
80
81 pub fn flush(&mut self, dst: *mut u8, dst_len: usize, stable_src: bool) -> Result<usize> {
82 let opt = LZ4FCompressionOptions::stable(stable_src);
83 let code = unsafe {
84 binding::LZ4F_flush(
85 self.ctx.as_ptr(),
86 dst as *mut c_void,
87 dst_len,
88 &opt as *const LZ4FCompressionOptions,
89 )
90 };
91 result_from_code(code).map(|_| code)
92 }
93
94 pub fn end(&mut self, dst: *mut u8, dst_len: usize, stable_src: bool) -> Result<usize> {
95 let opt = LZ4FCompressionOptions::stable(stable_src);
96 let code = unsafe {
97 binding::LZ4F_compressEnd(
98 self.ctx.as_ptr(),
99 dst as *mut c_void,
100 dst_len,
101 &opt as *const LZ4FCompressionOptions,
102 )
103 };
104 result_from_code(code).map(|_| code)
105 }
106
107 pub fn compress_bound(src_size: usize, prefs: &Preferences) -> usize {
108 unsafe { binding::LZ4F_compressBound(src_size, prefs as *const Preferences) }
109 }
110 }
111
112 impl Drop for CompressionContext {
113 fn drop(&mut self) {
114 unsafe {
115 binding::LZ4F_freeCompressionContext(self.ctx.as_ptr());
116 }
117 }
118 }
119
120 pub struct DecompressionContext {
121 ctx: NonNull<LZ4FDecompressionCtx>,
122 }
123
124 unsafe impl Send for DecompressionContext {}
125
126 impl DecompressionContext {
127 pub fn new() -> Result<Self> {
128 let ctx = MaybeUninit::<*mut LZ4FDecompressionCtx>::uninit();
129 unsafe {
130 let code = binding::LZ4F_createDecompressionContext(
131 ctx.as_ptr() as *mut *mut binding::LZ4FDecompressionCtx,
132 binding::LZ4F_getVersion(),
133 );
134 result_from_code(code).and_then(|_| {
135 Ok(Self {
136 ctx: NonNull::new(ctx.assume_init())
137 .ok_or_else(|| crate::Error::new(crate::ErrorKind::InitializationFailed))?,
138 })
139 })
140 }
141 }
142
143 pub fn get_frame_info(&self, src: &[u8]) -> Result<(FrameInfo, usize)> {
144 let mut info = MaybeUninit::<FrameInfo>::uninit();
145 let mut src_len = src.len();
146 let code = unsafe {
147 binding::LZ4F_getFrameInfo(
148 self.ctx.as_ptr(),
149 info.as_mut_ptr(),
150 src.as_ptr() as *const c_void,
151 &mut src_len as *mut usize,
152 )
153 };
154 result_from_code(code).map(|_| (unsafe { info.assume_init() }, src_len))
155 }
156
157 pub fn decompress_dict(
158 &mut self,
159 src: &[u8],
160 dst: &mut [u8],
161 dict: &[u8],
162 stable_dst: bool,
163 ) -> Result<(usize, usize, usize)> {
164 let mut dst_len = dst.len();
165 let mut src_len = src.len();
166 let opt = LZ4FDecompressionOptions::stable(stable_dst);
167 let code = unsafe {
168 binding::LZ4F_decompress_usingDict(
169 self.ctx.as_ptr(),
170 dst.as_mut_ptr() as *mut c_void,
171 &mut dst_len as *mut usize,
172 src.as_ptr() as *const c_void,
173 &mut src_len as *mut usize,
174 dict.as_ptr() as *const c_void,
175 dict.len(),
176 &opt as *const LZ4FDecompressionOptions,
177 )
178 };
179 result_from_code(code).map(|_| (src_len, dst_len, code))
180 }
181
182 pub fn reset(&mut self) {
183 unsafe {
184 binding::LZ4F_resetDecompressionContext(self.ctx.as_ptr());
185 }
186 }
187 }
188
189 impl Drop for DecompressionContext {
190 fn drop(&mut self) {
191 unsafe {
192 binding::LZ4F_freeDecompressionContext(self.ctx.as_ptr());
193 }
194 }
195 }
196
197 pub fn compress_frame_bound(src_size: usize, prefs: &Preferences) -> usize {
198 unsafe { binding::LZ4F_compressFrameBound(src_size, prefs as *const Preferences) }
199 }
200
201 pub fn header_size(src: &[u8]) -> usize {
202 unsafe { binding::LZ4F_headerSize(src.as_ptr() as *const c_void, src.len()) }
203 }
204
205 pub fn compress(src: &[u8], dst: *mut u8, dst_len: usize, prefs: &Preferences) -> Result<usize> {
206 let code = unsafe {
207 binding::LZ4F_compressFrame(
208 dst as *mut c_void,
209 dst_len,
210 src.as_ptr() as *const c_void,
211 src.len(),
212 prefs as *const Preferences,
213 )
214 };
215 result_from_code(code).map(|_| code)
216 }
217
218 fn result_from_code(code: usize) -> Result<()> {
219 Err(Error::new(match code.wrapping_neg() {
220 1 => ErrorKind::Generic,
221 2 => ErrorKind::MaxBlockSizeInvalid,
222 3 => ErrorKind::BlockModeInvalid,
223 4 => ErrorKind::ContentChecksumFlagInvalid,
224 5 => ErrorKind::CompressionLevelInvalid,
225 6 => ErrorKind::HeaderVersionWrong,
226 7 => ErrorKind::BlockChecksumInvalid,
227 8 => ErrorKind::ReservedFlagSet,
228 9 => ErrorKind::AllocationFailed,
229 10 => ErrorKind::SrcSizeTooLarge,
230 11 => ErrorKind::DstMaxSizeTooSmall,
231 12 => ErrorKind::FrameHeaderIncomplete,
232 13 => ErrorKind::FrameTypeUnknown,
233 14 => ErrorKind::FrameSizeWrong,
234 15 => ErrorKind::SrcPtrWrong,
235 16 => ErrorKind::DecompressionFailed,
236 17 => ErrorKind::HeaderChecksumInvalid,
237 18 => ErrorKind::ContentChecksumInvalid,
238 19 => ErrorKind::FrameDecodingAlreadyStarted,
239 _ => return Ok(()),
240 }))
241 }
242
243 pub struct DictionaryHandle(NonNull<LZ4FCompressionDict>);
244
245 unsafe impl Send for DictionaryHandle {}
246 unsafe impl Sync for DictionaryHandle {}
247
248 impl DictionaryHandle {
249 pub fn new(data: &[u8]) -> Result<Self> {
250 let dict = unsafe { binding::LZ4F_createCDict(data.as_ptr() as *const c_void, data.len()) };
251 NonNull::new(dict)
252 .ok_or_else(|| crate::Error::new(crate::ErrorKind::InitializationFailed).into())
253 .map(Self)
254 }
255 }
256
257 impl Drop for DictionaryHandle {
258 fn drop(&mut self) {
259 unsafe {
260 binding::LZ4F_freeCDict(self.0.as_ptr());
261 }
262 }
263 }