mzstd 0.2.8

Zstandard (RFC 8878) compression and decompression in safe Rust; single-threaded encoding is byte-identical to zstd 1.5.7
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use super::{
    core::{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.
///
/// # Examples
///
/// ```
/// use mzstd::framing::*;
/// let items = [FramedItem::Resource(FramedResource::from(&b"hello"[..]))];
/// let mut encoder = FramedCompressor::new(Default::default())?;
/// let bytes = encoder.compress(items.as_slice().into())?;
/// // Every structure is a skippable frame; the payload decodes as Zstandard.
/// assert_eq!(&bytes[8..12], b"MZFR");
/// assert_eq!(mzstd::Decompressor::default().decompress(&bytes)?, b"hello");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[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.
///
/// # Examples
///
/// ```
/// use mzstd::{Backend, RetentionPolicy, framing::*};
/// let encoder = FramedCompressor::builder(Default::default())
///     .with_backend(Backend::default())
///     .with_retention(RetentionPolicy::ReleaseAll)
///     .build()?;
/// assert_eq!(encoder.retention(), RetentionPolicy::ReleaseAll);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Copy, Debug)]
pub struct FramedCompressorBuilder {
    config: FramedEncodeConfig,
    backend: Option<Backend>,
    retention: RetentionPolicy,
}

impl FramedCompressorBuilder {
    /// Selects a host-validated SIMD backend.
    #[must_use]
    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.
    #[must_use]
    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> {
        Engine::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 mzstd::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[8..12], b"MZFR");
    /// let bad = FramingConfig { chunk_bytes: 0, ..Default::default() };
    /// assert!(FramedCompressor::new(FramedEncodeConfig::default().with_framing_config(bad)).is_err());
    /// # 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 mzstd::{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>>(())
    /// ```
    #[must_use]
    pub const fn builder(config: FramedEncodeConfig) -> FramedCompressorBuilder {
        FramedCompressorBuilder {
            config,
            backend: None,
            retention: RetentionPolicy::Aggressive,
        }
    }

    /// Current resource encoder and framing configuration.
    #[must_use]
    pub const fn config(&self) -> &FramedEncodeConfig {
        &self.config
    }

    /// Retention policy applied at completion or cancellation.
    #[must_use]
    pub const fn retention(&self) -> RetentionPolicy {
        self.retention
    }

    /// Owned heap capacities, excluding destinations and borrowed dictionaries.
    #[must_use]
    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 mzstd::{EncoderConfig, Level, 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_level(Level::try_from(1)?)))?;
    /// 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> {
        Engine::validate(*config.framing_config())?;
        config.encoder_config().validate()?;
        if self.active {
            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.allows(self.retained_bytes()) {
            self.engine.clear();
            self.raw.trim(RetentionPolicy::ReleaseAll);
            self.engine = Engine::new(*self.config.framing_config());
        }
    }

    /// Releases storage and cancels a forgotten operation, preserving policies.
    ///
    /// # Examples
    ///
    /// ```
    /// use mzstd::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.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.
    #[must_use]
    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,
        }
    }

    /// Releases the active container; the one path both session shapes end on.
    pub(super) fn cancel(&mut self) {
        self.engine.clear();
        self.active = false;
        self.trim(self.retention);
    }

    /// Claims the owner for one container and queues its header.
    ///
    /// The single start path behind [`Self::start`] and [`Self::into_session`].
    pub(super) fn begin_session(&mut self, stream: FramedEncodeStreamConfig) -> Result<(), 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(())
    }

    /// Starts one exclusive container and queues its header without I/O.
    ///
    /// # Errors
    ///
    /// Returns `AbandonedSession` after a forgotten guard, or an allocation failure.
    ///
    /// # Examples
    ///
    /// ```
    /// use mzstd::{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::Continue)?;
    ///         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::End)?;
    ///         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> {
        self.begin_session(stream)?;
        Ok(FramedEncoderSession { owner: self })
    }

    /// Starts one container that takes ownership of this encoder.
    ///
    /// Validates and starts exactly as [`Self::start`] does, but the returned
    /// [`FramedEncoderSessionOwned`] owns the encoder instead of borrowing it.
    /// Recover the encoder with [`FramedEncoderSessionOwned::into_framed_compressor`].
    ///
    /// # Errors
    ///
    /// As [`Self::start`]. The encoder is dropped with the error; use
    /// [`Self::start`] when it must survive a rejected start.
    ///
    /// # Examples
    ///
    /// ```
    /// use mzstd::framing::*;
    /// let encoder = FramedCompressor::new(Default::default())?;
    /// let mut session = encoder.into_session(Default::default())?;
    /// let mut output = [0; 128];
    /// let progress = session.process(&mut output, FramedEncodeOperation::Process)?;
    /// assert_eq!(&output[8..12], b"MZFR");
    /// assert_eq!(progress.status, FramedEncoderStatus::NeedsInput);
    /// // The unfinished container is cancelled; the encoder starts afresh.
    /// let mut encoder = session.into_framed_compressor();
    /// assert_eq!(encoder.start(Default::default())?.total_out(), 0);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn into_session(
        mut self,
        stream: FramedEncodeStreamConfig,
    ) -> Result<FramedEncoderSessionOwned, E> {
        self.begin_session(stream)?;
        Ok(FramedEncoderSessionOwned { owner: self })
    }

    /// Encodes borrowed resources and metadata as one finalized container.
    ///
    /// # Errors
    ///
    /// Reports malformed commands, length mismatches, limits or encoding failures.
    ///
    /// # Examples
    ///
    /// ```
    /// use mzstd::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())?;
    /// // A plain Zstandard decoder sees only the visible resource.
    /// assert_eq!(mzstd::Decompressor::default().decompress(&bytes)?, b"visible resource");
    /// # 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 mzstd::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 + 8..range.start + 12], b"MZFR");
    /// # 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 mzstd::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: FramedEncodeLocation::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));
            }
        }
    }
}