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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use super::{
core::{Container, driver::Driver, engine::Engine},
*,
};
use crate::{Backend, Compressor, RetentionPolicy};
use ::core::ops::Range;
use FramedEncodeError as E;
use alloc::vec::Vec;
/// Reusable owner of raw compression and bounded framing storage.
#[derive(Debug)]
pub struct FramedCompressor {
pub(super) raw: Compressor,
pub(super) engine: Engine,
config: FramedEncodeConfig,
retention: RetentionPolicy,
active: bool,
}
/// Construction settings; backend selection occurs once when built.
#[derive(Clone, Copy, Debug)]
pub struct FramedCompressorBuilder {
config: FramedEncodeConfig,
backend: Option<Backend>,
retention: RetentionPolicy,
}
impl FramedCompressorBuilder {
/// Selects a host-validated SIMD backend.
pub const fn with_backend(mut self, backend: Backend) -> Self {
self.backend = Some(backend);
self
}
/// Selects retention at the container boundary, rather than each resource.
pub const fn with_retention(mut self, retention: RetentionPolicy) -> Self {
self.retention = retention;
self
}
/// Validates both policies and creates an empty owner without I/O.
/// # Errors
/// Returns invalid encoder settings or inconsistent framing profiles/budgets.
pub fn build(self) -> Result<FramedCompressor, E> {
Container::validate(*self.config.framing_config())?;
let mut builder = Compressor::builder(*self.config.encoder_config());
if let Some(backend) = self.backend {
builder = builder.with_backend(backend);
}
Ok(FramedCompressor {
raw: builder.build()?,
engine: Engine::new(*self.config.framing_config()),
config: self.config,
retention: self.retention,
active: false,
})
}
}
impl FramedCompressor {
/// Creates an empty reusable framed encoder.
/// # Errors
/// Rejects invalid raw settings and framing profiles or budgets.
/// # Examples
/// ```
/// use mbrotli::framing::*;
/// let items = [FramedItem::Resource(FramedResource::from(&b"hello"[..]))];
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// let bytes = encoder.compress(items.as_slice().into())?;
/// assert_eq!(&bytes[..4], &[0x91, 10, 66, 82]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn new(config: FramedEncodeConfig) -> Result<Self, E> {
Self::builder(config).build()
}
/// Begins configuration without allocating or selecting a backend.
/// # Examples
/// ```
/// use mbrotli::{Backend, RetentionPolicy, framing::*};
/// let encoder = FramedCompressor::builder(Default::default())
/// .with_backend(Backend::default())
/// .with_retention(RetentionPolicy::ReleaseAll)
/// .build()?;
/// assert_eq!(encoder.retained_bytes(), 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub const fn builder(config: FramedEncodeConfig) -> FramedCompressorBuilder {
FramedCompressorBuilder {
config,
backend: None,
retention: RetentionPolicy::Aggressive,
}
}
/// Current resource encoder and framing configuration.
pub const fn config(&self) -> &FramedEncodeConfig {
&self.config
}
/// Retention policy applied at completion or cancellation.
pub const fn retention(&self) -> RetentionPolicy {
self.retention
}
/// Owned heap capacities, excluding destinations and borrowed dictionaries.
pub fn retained_bytes(&self) -> usize {
self.raw.retained_bytes() + self.engine.retained_bytes()
}
/// Validates before replacing policy and clearing abandoned protection.
/// # Errors
/// Invalid configuration leaves both configuration and protection unchanged.
/// # Examples
/// ```
/// use mbrotli::{EncoderConfig, Quality, RetentionPolicy, framing::*};
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// let items = [FramedItem::Resource(FramedResource::from(&b"payload"[..]))];
/// let first = encoder.compress(items.as_slice().into())?;
/// assert_eq!(first, encoder.compress(items.as_slice().into())?);
/// encoder.reconfigure(encoder.config().with_encoder_config(
/// EncoderConfig::default().with_quality(Quality::Q1)))?;
/// let independent = encoder.fork_empty();
/// assert_eq!(independent.config(), encoder.config());
/// encoder.trim(RetentionPolicy::ReleaseAll);
/// assert_eq!(encoder.retained_bytes(), 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn reconfigure(&mut self, config: FramedEncodeConfig) -> Result<(), E> {
Container::validate(*config.framing_config())?;
config.encoder_config().validate()?;
let release = self.active
|| (config != self.config && self.retention == RetentionPolicy::CurrentConfig);
if release {
self.recover();
} else {
self.cancel();
}
self.raw.reconfigure(*config.encoder_config())?;
self.config = config;
self.engine.reconfigure(*config.framing_config());
self.trim(self.retention);
Ok(())
}
/// Applies storage policy without clearing forgotten-session protection.
pub fn trim(&mut self, policy: RetentionPolicy) {
if policy == RetentionPolicy::ReleaseAll
|| matches!(policy, RetentionPolicy::Bounded { max_bytes } if self.retained_bytes() > max_bytes)
{
self.engine.clear(&mut self.raw);
self.raw.trim(RetentionPolicy::ReleaseAll);
self.engine = Engine::new(*self.config.framing_config());
}
}
/// Releases storage and cancels a forgotten operation, preserving policies.
/// # Examples
/// ```
/// use mbrotli::framing::*;
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// core::mem::forget(encoder.start(Default::default())?);
/// assert!(matches!(encoder.start(Default::default()), Err(FramedEncodeError::AbandonedSession)));
/// encoder.recover();
/// let session = encoder.start(Default::default())?;
/// assert_eq!(session.total_in(), 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn recover(&mut self) {
self.engine.clear(&mut self.raw);
self.raw.recover();
self.raw.trim(RetentionPolicy::ReleaseAll);
self.engine = Engine::new(*self.config.framing_config());
self.active = false;
}
/// Copies configuration and backend into an independent owner without buffers.
pub fn fork_empty(&self) -> Self {
Self {
raw: self.raw.fork_empty(),
engine: Engine::new(*self.config.framing_config()),
config: self.config,
retention: self.retention,
active: false,
}
}
pub(super) fn cancel(&mut self) {
self.engine.clear(&mut self.raw);
self.active = false;
self.trim(self.retention);
}
/// Starts one exclusive container and queues its header without I/O.
/// # Errors
/// Returns `AbandonedSession` after a forgotten guard, or an allocation failure.
/// # Examples
/// ```
/// use mbrotli::{InputSize, Operation, framing::*};
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// let mut session = encoder.start(InputSize::Exact(5).into())?;
/// let mut wire = Vec::new();
/// let mut output = [0; 1];
/// loop {
/// let p = session.process(&mut output, FramedEncodeOperation::Process)?;
/// wire.extend_from_slice(&output[..p.produced]);
/// if p.status == FramedEncoderStatus::NeedsInput { break; }
/// }
/// {
/// let mut resource = session.resource(Default::default(), InputSize::Exact(5).into())?;
/// let mut input = &b"hel"[..];
/// loop {
/// let p = resource.process(input, &mut output, Operation::Process)?;
/// input = &input[p.consumed..];
/// wire.extend_from_slice(&output[..p.produced]);
/// if p.status == FramedEncoderStatus::NeedsInput { break; }
/// }
/// let mut input = &b"lo"[..];
/// loop {
/// let p = resource.process(input, &mut output, Operation::Finish)?;
/// input = &input[p.consumed..];
/// wire.extend_from_slice(&output[..p.produced]);
/// if p.status == FramedEncoderStatus::Finished { break; }
/// }
/// }
/// session.metadata(MetadataKind::Footer, &[
/// MetadataField { code: *b"AB", value: b"footer annotation" },
/// ])?;
/// loop {
/// let p = session.process(&mut output, FramedEncodeOperation::Finish)?;
/// wire.extend_from_slice(&output[..p.produced]);
/// if p.status == FramedEncoderStatus::Finished { break; }
/// }
/// assert_eq!(session.resources_encoded(), 1);
/// assert_eq!(session.total_out(), wire.len() as u64);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn start(
&mut self,
stream: FramedEncodeStreamConfig,
) -> Result<FramedEncoderSession<'_>, E> {
if self.active {
return Err(E::AbandonedSession);
}
if self.engine.retained_bytes() > self.config.framing_config().max_buffer_bytes {
self.recover();
}
self.engine.start(stream)?;
self.active = true;
Ok(FramedEncoderSession { owner: self })
}
/// Encodes borrowed resources and metadata as one finalized container.
/// # Errors
/// Reports malformed commands, length mismatches, limits or encoding failures.
/// # Examples
/// ```
/// use mbrotli::framing::*;
/// let fields = [MetadataField { code: *b"id", value: b"dictionary.bin" }];
/// let items = [
/// FramedItem::Metadata { kind: MetadataKind::Resource, fields: &fields, options: Default::default() },
/// FramedItem::Resource(FramedResource {
/// data: b"hidden prefix bytes",
/// options: ResourceOptions { hidden: true, ..Default::default() },
/// stream: Default::default(), encoding: ResourceEncoding::Uncompressed,
/// }),
/// FramedItem::Resource(FramedResource::from(&b"visible resource"[..])),
/// ];
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// let bytes = encoder.compress(items.as_slice().into())?;
/// assert_eq!(&bytes[..4], &[0x91, 10, 66, 82]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn compress(&mut self, input: FramedInput<'_>) -> Result<Vec<u8>, E> {
let mut output = Vec::new();
self.compress_into(input, &mut output)?;
Ok(output)
}
/// Appends a canonical container. Every error restores the original length.
/// # Errors
/// As `compress`, including allocation or late suffix errors; prefix is preserved.
/// # Examples
/// ```
/// use mbrotli::framing::*;
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// let items = [FramedItem::Resource(FramedResource::from(&b"hello"[..]))];
/// let mut output = b"caller prefix".to_vec();
/// let range = encoder.compress_into(items.as_slice().into(), &mut output)?;
/// assert_eq!(&output[..range.start], b"caller prefix");
/// assert_eq!(&output[range.start..range.start + 4], &[0x91, 10, 66, 82]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn compress_into(
&mut self,
input: FramedInput<'_>,
dst: &mut Vec<u8>,
) -> Result<Range<usize>, E> {
let start = dst.len();
let result = (|| {
let stream = Driver::aggregate(input)?.into();
let mut driver = Driver::new(self.start(stream)?, input);
let mut buffer = [0; 8192];
loop {
let p = driver
.process(&mut buffer)
.map_err(FramedEncodeFailure::into_error)?;
dst.try_reserve(p.produced)
.map_err(|_| E::AllocationFailed)?;
dst.extend_from_slice(&buffer[..p.produced]);
if p.status == FramedEncoderStatus::Finished {
return Ok(start..dst.len());
}
}
})();
if result.is_err() {
dst.truncate(start);
}
result
}
/// Writes the canonical stream into the slice prefix without whole-container staging.
/// # Errors
/// Returns cumulative accepted input and written prefix; untouched tail stays intact.
/// # Examples
/// ```
/// use mbrotli::framing::*;
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// let items = [FramedItem::Resource(FramedResource::from(&b"hello"[..]))];
/// let mut output = [0xa5; 512];
/// let written = encoder.compress_to_slice(items.as_slice().into(), &mut output)?;
/// assert!(output[written..].iter().all(|byte| *byte == 0xa5));
/// let failure = encoder.compress_to_slice(items.as_slice().into(), &mut output[..2]).unwrap_err();
/// assert_eq!(failure.produced, 2);
/// assert!(matches!(failure.into_error(), FramedEncodeError::OutputTooSmall));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn compress_to_slice(
&mut self,
input: FramedInput<'_>,
dst: &mut [u8],
) -> Result<usize, FramedEncodeFailure> {
let stream = match Driver::aggregate(input) {
Ok(size) => size.into(),
Err(error) => return Err(self.engine.failure(error, 0, 0)),
};
let session = self.start(stream).map_err(|error| FramedEncodeFailure {
error,
consumed: 0,
produced: 0,
location: Default::default(),
})?;
let mut driver = Driver::new(session, input);
let mut consumed = 0;
let mut produced = 0;
loop {
let p = driver.process(&mut dst[produced..]).map_err(|mut e| {
e.consumed += consumed;
e.produced += produced;
e
})?;
consumed += p.consumed;
produced += p.produced;
if p.status == FramedEncoderStatus::Finished {
return Ok(produced);
}
if produced == dst.len() {
return Err(driver.failure(E::OutputTooSmall, consumed, produced));
}
}
}
}