forked from dyz1990/sevenz-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoders.rs
78 lines (71 loc) · 2.16 KB
/
encoders.rs
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
use std::io::Write;
use crate::{
archive::{SevenZMethod, SevenZMethodConfiguration},
lzma::CountingWriter,
lzma::{LZMA2Options, LZMA2Writer},
method_options::MethodOptions,
Error,
};
pub enum Encoder<W: Write> {
LZMA2(LZMA2Writer<W>),
}
impl<W: Write> Write for Encoder<W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
Encoder::LZMA2(w) => w.write(buf),
}
}
fn flush(&mut self) -> std::io::Result<()> {
match self {
Encoder::LZMA2(w) => w.flush(),
}
}
}
pub fn add_encoder<W: Write>(
input: CountingWriter<W>,
method_config: &SevenZMethodConfiguration,
) -> Result<Encoder<W>, Error> {
let method = method_config.method;
match method.id() {
SevenZMethod::ID_LZMA2 => {
let mut def_opts = LZMA2Options::default();
let options = match method_config.options.as_ref() {
Some(MethodOptions::LZMA2(opts)) => opts,
Some(MethodOptions::Num(n)) => {
def_opts.dict_size = *n;
&def_opts
}
_ => {
def_opts.dict_size = LZMA2Options::DICT_SIZE_DEFAULT;
&def_opts
}
};
let lz = LZMA2Writer::new(input, options);
Ok(Encoder::LZMA2(lz))
}
_ => {
return Err(Error::UnsupportedCompressionMethod(
method.name().to_string(),
));
}
}
}
pub(crate) fn get_options_as_properties<'a>(
method: SevenZMethod,
options: Option<&MethodOptions>,
out: &'a mut [u8],
) -> &'a [u8] {
match method.id() {
SevenZMethod::ID_LZMA2 => {
let dict_size = options
.map(|o| o.get_lzma2_dict_size())
.unwrap_or(LZMA2Options::DICT_SIZE_DEFAULT);
let lead = dict_size.leading_zeros();
let second_bit = (dict_size >> (30u32.wrapping_sub(lead))).wrapping_sub(2);
let prop = (19u32.wrapping_sub(lead) * 2 + second_bit) as u8;
out[0] = prop;
&out[0..1]
}
_ => &[],
}
}