1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#![allow(clippy::expl_impl_clone_on_copy)]
#![allow(clippy::use_self)]
#[cxx::bridge(namespace = "lzma_cxx")]
pub mod ffi {
#[repr(u8)]
#[derive(Debug, Clone)]
enum LZMAType {
LZMA1 = 0,
LZMA2,
}
unsafe extern "C++" {
include!("wolf_system/src/compression/cxx/lzma/lzma.hpp");
/// Compress a buffer via LZMA algorithm
///
/// # Arguments
///
/// * `p_source_buffer` - The source buffer of bytes.
/// * `p_compress_type` - The type of LZMA compression.
/// * `pLevel` - The level of compression. ( 0 <= level <= 9)
/// * `pTrace` - The trace log which will be provided by the C++ side
///
/// # Examples
///
/// ```
/// use wolf_system::compression::lzma;
///
/// let content = "HELLO WOLF!".as_bytes();
/// let mut trace = String::new();
/// let compress = lzma::ffi::compress(content, lzma::ffi::LZMAType::LZMA1, 5, &mut trace);
///
/// println!("{:?}", compress);
/// ```
pub fn compress(
p_source_buffer: &[u8],
p_compress_type: LZMAType,
p_level: u32,
p_trace: &mut String,
) -> Vec<u8>;
/// Compress a buffer via LZMA1 algorithm
///
/// # Arguments
///
/// * `p_source_buffer` - The source buffer of bytes.
/// * `p_compress_type` - The type of LZMA compression.
/// * `pTrace` - The trace log which will be provided by the C++ side
///
/// # Examples
///
/// ```
/// use wolf_system::compression::lzma;
///
/// let content = [93, 0, 16, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 36, 17, 69, 207, 114, 217, 14, 201, 65, 103, 35, 131, 32, 189, 192];
/// let mut trace = String::new();
/// let decompress = lzma::ffi::decompress(content.as_slice(), lzma::ffi::LZMAType::LZMA1, &mut trace);
/// ```
pub fn decompress(
p_source_buffer: &[u8],
p_compress_type: LZMAType,
p_trace: &mut String,
) -> Vec<u8>;
}
}
#[test]
fn test() {
use crate::compression::lzma;
let content = "HELLO WOLF\r\nHELLO WOLF!*&%!HELLO WOLF!07*&%!".as_bytes();
println!(
"original memory is {:?} with size {}",
std::str::from_utf8(content),
content.len(),
);
let mut now = std::time::Instant::now();
let mut trace = String::new();
let compress_1 = lzma::ffi::compress(content, lzma::ffi::LZMAType::LZMA1, 5, &mut trace);
let mut duration = std::time::Instant::now() - now;
println!(
"lzma compressed done in {} sec. Memory is {:?} with size {}. trace info: {:?}",
duration.as_secs_f64(),
compress_1,
compress_1.len(),
trace
);
trace.clear();
now = std::time::Instant::now();
let decompress_1 = lzma::ffi::decompress(
compress_1.as_slice(),
lzma::ffi::LZMAType::LZMA1,
&mut trace,
);
duration = std::time::Instant::now() - now;
println!(
"lzma de-compressed done in {} sec. Memory is {:?} with size {}. trace info: {:?}",
duration.as_secs_f64(),
std::str::from_utf8(decompress_1.as_slice()),
decompress_1.len(),
trace
);
trace.clear();
now = std::time::Instant::now();
let compress_2 = lzma::ffi::compress(content, lzma::ffi::LZMAType::LZMA2, 5, &mut trace);
duration = std::time::Instant::now() - now;
println!(
"lzma2 compressed done in {}. Memory is {:?} with size {}. trace info: {:?}",
duration.as_secs_f64(),
compress_2,
compress_2.len(),
trace
);
trace.clear();
now = std::time::Instant::now();
let decompress_2 = lzma::ffi::decompress(
compress_2.as_slice(),
lzma::ffi::LZMAType::LZMA2,
&mut trace,
);
duration = std::time::Instant::now() - now;
println!(
"lzma2 de-compressed done in {}. Memory is {:?}with size {}. trace info: {:?}",
duration.as_secs_f64(),
std::str::from_utf8(decompress_2.as_slice()),
decompress_2.len(),
trace
);
}