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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
mod block_encoder;
mod ecc_encoder;
mod file_encoder;
mod streaming_encoder;
pub use block_encoder::TOABlockWriter;
pub use ecc_encoder::ECCEncoder;
#[cfg(feature = "std")]
pub use file_encoder::TOAFileEncoder;
pub use streaming_encoder::TOAStreamingEncoder;
use crate::{
ErrorCorrection, Prefilter, lzma,
lzma::{EncodeMode, lz::MFType},
};
/// Options for TOA compression.
#[derive(Debug, Clone, Copy)]
pub struct TOAOptions {
/// Prefilter to apply before compression.
pub(crate) prefilter: Prefilter,
/// Reed-Solomon error correction level for data protection.
pub(crate) error_correction: ErrorCorrection,
/// Dictionary size to use for the LZMA compression algorithm as a power of two.
pub(crate) dictionary_size_exponent: u8,
/// LZMA literal context bits (0-8).
pub(crate) lc: u8,
/// LZMA literal position bits (0-4).
pub(crate) lp: u8,
/// LZMA position bits (0-4).
pub(crate) pb: u8,
/// Compression mode.
pub(crate) mode: EncodeMode,
/// Match finder nice length.
pub(crate) nice_len: u16,
/// Match finder type.
pub(crate) mf: MFType,
/// Match finder depth limit.
pub(crate) depth_limit: u8,
/// Block size as power of two exponent. If None, all data will be written in one block.
/// Block size = 2^exponent bytes. Valid range: 10-62 (1 KiB to 4 EiB).
pub(crate) block_size_exponent: Option<u8>,
}
impl Default for TOAOptions {
fn default() -> Self {
Self {
prefilter: Prefilter::None,
error_correction: ErrorCorrection::None,
dictionary_size_exponent: 26,
lc: 3,
lp: 0,
pb: 2,
mode: EncodeMode::Normal,
nice_len: 64,
mf: MFType::BT4,
depth_limit: 0,
block_size_exponent: None,
}
}
}
impl TOAOptions {
const PRESET_TO_DICT_SIZE_LOG2: &'static [u8] = &[
19, // (0) 512 KiB
20, // (1) 1 MiB
21, // (2) 2 MiB
22, // (3) 4 MiB
23, // (4) 8 MiB
24, // (5) 16 MiB
25, // (6) 32 MiB
26, // (7) 64 MiB
27, // (8) 128 MiB
28, // (9) 256 MiB
];
const PRESET_TO_DEPTH_LIMIT: &'static [u8] = &[4, 8, 24, 48];
/// Create options with a specific preset level (0-9).
///
/// # Dictionary size
///
/// - Preset 0: 512 KiB
/// - Preset 1: 1 MiB
/// - Preset 2: 2 MiB
/// - Preset 3: 4 MiB
/// - Preset 4: 8 MiB
/// - Preset 5: 16 MiB
/// - Preset 6: 32 MiB
/// - Preset 7: 64 MiB
/// - Preset 8: 128 MiB
/// - Preset 9: 256 MiB
///
/// Block size is set to the dictionary size.
pub fn from_preset(preset: u32) -> Self {
let preset = preset.min(9);
let error_correction = ErrorCorrection::None;
let prefilter = Prefilter::None;
let lc = 3;
let lp = 0;
let pb = 2;
let dictionary_size_exponent = Self::PRESET_TO_DICT_SIZE_LOG2[preset as usize];
let mode;
let mf;
let nice_len;
let depth_limit;
if preset <= 3 {
mode = EncodeMode::Fast;
mf = MFType::HC4;
nice_len = if preset <= 1 { 128 } else { 273 };
depth_limit = Self::PRESET_TO_DEPTH_LIMIT[preset as usize];
} else {
mode = EncodeMode::Normal;
mf = MFType::BT4;
nice_len = if preset == 4 {
16
} else if preset == 5 {
32
} else {
64
};
depth_limit = 0;
}
Self {
prefilter,
error_correction,
dictionary_size_exponent,
lc,
lp,
pb,
mode,
nice_len,
mf,
depth_limit,
block_size_exponent: Some(dictionary_size_exponent),
}
}
/// Sets the LZMA literal context bits.
///
/// Clamped in range of 0 and 8.
pub fn with_lc(mut self, lc: u8) -> Self {
self.lc = lc.clamp(0, 8);
self
}
/// Sets the LZMA literal position bits.
///
/// Clamped in range of 0 and 4.
pub fn with_lp(mut self, lp: u8) -> Self {
self.lp = lp.clamp(0, 4);
self
}
/// Sets the LZMA position bits (0-4).
///
/// Clamped in range of 0 and 4.
pub fn with_pb(mut self, pb: u8) -> Self {
self.pb = pb.clamp(0, 4);
self
}
/// Set the prefilter to use.
pub fn with_prefilter(mut self, prefilter: Prefilter) -> Self {
self.prefilter = prefilter;
self
}
/// Set the Reed-Solomon error correction level for data protection.
pub fn with_error_correction(mut self, error_correction: ErrorCorrection) -> Self {
self.error_correction = error_correction;
self
}
/// Set the dictionary size exponent of the LZMA compression algorithm.
///
/// Dictionary size = 2^exponent bytes. Valid range: 16-31 (64 KiB to 4 EiB).
pub fn with_dictionary_exponent(mut self, dictionary_size_log2: u8) -> Self {
self.dictionary_size_exponent = dictionary_size_log2.clamp(16, 31);
self
}
/// Set the block size exponent for multi-block compression.
///
/// Block size = 2^exponent bytes. Valid range: 16-62 (64 KiB to 4 EiB).
pub fn with_block_size_exponent(mut self, block_size_exponent: Option<u8>) -> Self {
self.block_size_exponent = block_size_exponent.map(|exp| exp.clamp(16, 62));
self
}
/// Get dictionary size in bytes.
pub fn dict_size(&self) -> u32 {
2u32.pow(self.dictionary_size_exponent as u32)
.min(lzma::DICT_SIZE_MAX)
}
/// Get dictionary size as power of 2 exponent.
pub fn dict_size_exponent(&self) -> u8 {
self.dictionary_size_exponent
}
/// Get block size as power of 2 exponent.
pub fn block_size_exponent(&self) -> Option<u8> {
self.block_size_exponent
}
/// Get block size in bytes. Returns None if single-block mode.
pub fn block_size(&self) -> Option<u64> {
self.block_size_exponent.map(|exp| 2u64.pow(exp as u32))
}
}