Debian Codegraph @

src/lz4_hc/stream/api.rs

raw symbols SCIP
1 #![allow(unsafe_code)]
2
3 use super::super::{binding, binding::LZ4StreamHC};
4 use crate::{Error, ErrorKind, Result};
5
6 use std::{
7 os::raw::{c_char, c_int},
8 ptr::NonNull,
9 ptr::null_mut
10 };
11
12 pub struct CompressionContext {
13 stream: NonNull<LZ4StreamHC>,
14 }
15
16 unsafe impl Send for CompressionContext {}
17
18 impl CompressionContext {
19 pub fn new() -> Result<Self> {
20 let ptr = unsafe { NonNull::new(binding::LZ4_createStreamHC()) };
21 ptr.ok_or_else(|| Error::new(ErrorKind::InitializationFailed))
22 .map(|stream| Self { stream })
23 }
24
25 pub fn set_compression_level(&mut self, compression_level: i32) {
26 unsafe {
27 binding::LZ4_setCompressionLevel(self.stream.as_ptr(), compression_level as c_int)
28 }
29 }
30
31 pub fn set_favor_dec_speed(&mut self, flag: bool) {
32 unsafe { binding::LZ4_favorDecompressionSpeed(self.stream.as_ptr(), i32::from(flag)) }
33 }
34
35 pub fn load_dict(&mut self, dict: &[u8]) {
36 unsafe {
37 binding::LZ4_loadDictHC(
38 self.stream.as_ptr(),
39 dict.as_ptr() as *const c_char,
40 dict.len() as c_int,
41 );
42 }
43 }
44
45 pub fn save_dict(&mut self, dict: &mut [u8]) {
46 unsafe {
47 binding::LZ4_saveDictHC(
48 self.stream.as_ptr(),
49 dict.as_ptr() as *mut c_char,
50 dict.len() as c_int,
51 );
52 }
53 }
54
55 pub fn next(&mut self, src: &[u8], dst: *mut u8, dst_len: usize) -> Result<usize> {
56 if src.is_empty() {
57 return Ok(0);
58 }
59 let dst_len = unsafe {
60 binding::LZ4_compress_HC_continue(
61 self.stream.as_ptr(),
62 src.as_ptr() as *const c_char,
63 dst as *mut c_char,
64 src.len() as c_int,
65 dst_len as c_int,
66 ) as usize
67 };
68 if dst_len > 0 {
69 Ok(dst_len)
70 } else {
71 Err(Error::new(ErrorKind::CompressionFailed))
72 }
73 }
74
75 pub fn next_partial(&mut self, src: &[u8], dst: &mut [u8]) -> Result<(usize, usize)> {
76 if src.is_empty() || dst.is_empty() {
77 return Ok((0, 0));
78 }
79 let mut src_len = src.len() as c_int;
80 let dst_len = unsafe {
81 binding::LZ4_compress_HC_continue_destSize(
82 self.stream.as_ptr(),
83 src.as_ptr() as *const c_char,
84 dst.as_mut_ptr() as *mut c_char,
85 &mut src_len as *mut c_int,
86 dst.len() as c_int,
87 ) as usize
88 };
89 if dst_len > 0 {
90 Ok((src_len as usize, dst_len))
91 } else {
92 Err(Error::new(ErrorKind::CompressionFailed))
93 }
94 }
95
96 pub fn attach_dict(&mut self, dict_stream: Option<&CompressionContext>, compression_level: i32) {
97 unsafe {
98 if dict_stream.is_none() {
99 // Note(sewer56): When detaching dictionary, we need to reset the stream state
100 // This behaviour is consistent with what the LZ4 library itself does internally.
101 // The LZ4HC API does not have a way to retrieve compression level, so we must pass it manually,
102 // since the HC API differs here.
103 binding::LZ4_resetStreamHC_fast(self.stream.as_ptr(), compression_level);
104 }
105 let dict_ptr = dict_stream.map(|ctx| ctx.stream.as_ptr()).unwrap_or(null_mut());
106 binding::LZ4_attach_HC_dictionary(self.stream.as_ptr(), dict_ptr);
107 }
108 }
109 }
110
111 impl Drop for CompressionContext {
112 fn drop(&mut self) {
113 unsafe {
114 binding::LZ4_freeStreamHC(self.stream.as_mut());
115 }
116 }
117 }