mopenzl 0.2.0

OpenZL compression and decompression in safe Rust; encoding is byte-identical to Meta's reference library
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! The incremental decoder behind sessions.
//!
//! A frame is a sequence of units: the frame header, each chunk, and the
//! end-of-frame marker. A unit is decoded once all of its bytes are present.
//! When a unit lies whole in the caller's input it is decoded in place;
//! otherwise its bytes are gathered in a buffer across calls. A unit's
//! length is known from its header alone, so the stream never accepts a byte
//! past the unit it is assembling: in [`FrameMode::Single`] the bytes after
//! the frame stay with the caller.
//!
//! Headers that straddle calls are measured over the buffered prefix and a
//! window of the new input that doubles until the header parses, so
//! measuring costs time linear in the header's length.

use alloc::vec::Vec;

use super::frame::{self, FrameInfo};
use super::out::VecOut;
use super::reader::Parse;
use super::{Engine, Unit};
use crate::decompressor::config::{DecoderConfig, FrameMode};
use crate::decompressor::error::DecodeError;
use crate::shared::destination::{self, Destination};

/// Smallest window of new input a straddling header is measured with.
const PEEK_MIN: usize = 64;

/// Whether more input may follow a call.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum Op {
    /// More input may follow.
    Process,
    /// This is all the remaining input.
    Finish,
}

/// Why a call stopped.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum StepStatus {
    NeedsInput,
    NeedsOutput,
    Finished,
}

/// What one call did.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) struct Step {
    pub(crate) consumed: usize,
    pub(crate) produced: usize,
    pub(crate) status: StepStatus,
}

/// A failed call, with its progress.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct StepFailure {
    pub(crate) consumed: usize,
    pub(crate) produced: usize,
    pub(crate) error: DecodeError,
}

/// Bytes a call has accepted and delivered so far.
#[derive(Copy, Clone, Debug, Default)]
struct Moved {
    consumed: usize,
    produced: usize,
}

/// Where in the frame the stream is.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
enum Phase {
    /// Expecting a frame header.
    #[default]
    Frame,
    /// Expecting a chunk or the end-of-frame marker of `FrameInfo`.
    Chunk,
    /// The operation is complete.
    Done,
}

/// The state of an operation in progress, with its reusable buffers.
#[derive(Debug, Default)]
pub(crate) struct Stream {
    phase: Phase,
    frame: Option<FrameInfo>,
    /// The header of the frame whose header unit is being assembled.
    pending_frame: Option<FrameInfo>,
    /// Bytes the current frame has regenerated, and its chunks so far.
    decoded: u64,
    chunks: u64,
    /// The current unit's bytes gathered so far, and its length once known.
    unit: Vec<u8>,
    need: Option<usize>,
    peek: Vec<u8>,
    /// Decoded bytes not yet delivered, from `delivered` on.
    output: Vec<u8>,
    delivered: usize,
    total_in: u64,
    total_out: u64,
    frames: u64,
}

impl Stream {
    /// Forgets the operation in progress, keeping the buffers.
    pub(crate) fn reset(&mut self) {
        self.phase = Phase::Frame;
        self.frame = None;
        self.pending_frame = None;
        self.decoded = 0;
        self.chunks = 0;
        self.unit.clear();
        self.need = None;
        self.peek.clear();
        self.output.clear();
        self.delivered = 0;
        self.total_in = 0;
        self.total_out = 0;
        self.frames = 0;
    }

    pub(crate) const fn is_finished(&self) -> bool {
        matches!(self.phase, Phase::Done)
    }

    pub(crate) const fn total_in(&self) -> u64 {
        self.total_in
    }

    pub(crate) const fn total_out(&self) -> u64 {
        self.total_out
    }

    pub(crate) const fn frames(&self) -> u64 {
        self.frames
    }

    pub(crate) fn retained_bytes(&self) -> usize {
        self.unit.capacity() + self.peek.capacity() + self.output.capacity()
    }

    pub(crate) fn release(&mut self) {
        self.unit = Vec::new();
        self.peek = Vec::new();
        self.output = Vec::new();
    }

    fn drain<D: Destination + ?Sized>(&mut self, output: &mut D, at: usize) -> usize {
        let n = destination::fill(output, at, &self.output[self.delivered..]);
        self.delivered += n;
        if self.delivered == self.output.len() {
            self.output.clear();
            self.delivered = 0;
        }
        n
    }

    /// The length of the unit at the front of `bytes`, and the frame header
    /// when the unit is one.
    fn unit_len(
        &self,
        engine: &mut Engine,
        config: &DecoderConfig,
        bytes: &[u8],
    ) -> Result<(usize, Option<FrameInfo>), Parse> {
        match (self.phase, &self.frame) {
            (Phase::Chunk, Some(info)) => match engine.measure(bytes, info)? {
                Unit::End => Ok((1, None)),
                Unit::Chunk(len) => Ok((len, None)),
            },
            _ => {
                let info = frame::parse(bytes, engine.simd(), config.compressed_checksum_check())?;
                Ok((info.header_len, Some(info)))
            }
        }
    }

    /// The current unit's length, if `rest` completes its header.
    fn measure(
        &mut self,
        engine: &mut Engine,
        config: &DecoderConfig,
        rest: &[u8],
    ) -> Result<Option<usize>, DecodeError> {
        if self.unit.is_empty() {
            let parsed = self.unit_len(engine, config, rest);
            return self.settle(parsed);
        }
        let mut window = rest.len().min(PEEK_MIN.max(self.unit.len()));
        let mut peek = core::mem::take(&mut self.peek);
        let parsed = loop {
            peek.clear();
            peek.extend_from_slice(&self.unit);
            peek.extend_from_slice(&rest[..window]);
            match self.unit_len(engine, config, &peek) {
                Err(Parse::Truncated) if window < rest.len() => {
                    window = rest.len().min(window * 2);
                }
                parsed => break parsed,
            }
        };
        self.peek = peek;
        self.settle(parsed)
    }

    /// Records a measurement: the unit's length, if its header is complete.
    fn settle(
        &mut self,
        parsed: Result<(usize, Option<FrameInfo>), Parse>,
    ) -> Result<Option<usize>, DecodeError> {
        match parsed {
            Ok((len, frame)) => {
                self.pending_frame = frame;
                Ok(Some(len))
            }
            Err(Parse::Truncated) => Ok(None),
            Err(Parse::Failed(error)) => Err(error),
        }
    }

    /// Acts on one complete unit.
    fn finish_unit(
        &mut self,
        engine: &mut Engine,
        config: &DecoderConfig,
        bytes: &[u8],
    ) -> Result<(), DecodeError> {
        match (self.phase, self.frame) {
            (Phase::Chunk, Some(info)) => {
                if bytes.len() == 1 && bytes[0] == crate::shared::wire::END_OF_FRAME {
                    if self.chunks == 0 {
                        return Err(DecodeError::invalid(
                            crate::decompressor::error::InvalidDataKind::EmptyFrame,
                        ));
                    }
                    if self.decoded != info.size {
                        return Err(DecodeError::ContentSizeMismatch {
                            declared: info.size,
                            decoded: self.decoded,
                        });
                    }
                    self.frames += 1;
                    self.frame = None;
                    self.phase = match config.frame_mode() {
                        FrameMode::Single => Phase::Done,
                        FrameMode::Concatenated => Phase::Frame,
                    };
                    return Ok(());
                }
                let produced = engine.decode_chunk(
                    bytes,
                    &info,
                    config,
                    info.size - self.decoded,
                    &mut VecOut::new(&mut self.output),
                )?;
                self.decoded += produced;
                self.chunks += 1;
                self.total_out += produced;
                Ok(())
            }
            _ => {
                // Measuring the unit parsed its header.
                let Some(info) = self.pending_frame.take() else {
                    return Err(DecodeError::InvalidState);
                };
                if let Some(limit) = config.limits().max_output_bytes()
                    && self.total_out.saturating_add(info.size) > limit
                {
                    return Err(DecodeError::OutputLimitExceeded { limit });
                }
                engine.open_frame(&info)?;
                self.frame = Some(info);
                self.decoded = 0;
                self.chunks = 0;
                self.phase = Phase::Chunk;
                Ok(())
            }
        }
    }

    /// Moves the operation forward as far as `input`, `output` and `op`
    /// allow.
    pub(crate) fn process<D: Destination + ?Sized>(
        &mut self,
        engine: &mut Engine,
        config: &DecoderConfig,
        input: &[u8],
        output: &mut D,
        op: Op,
    ) -> Result<Step, StepFailure> {
        let mut moved = Moved::default();
        let result = self.run(engine, config, input, output, op, &mut moved);
        let Moved { consumed, produced } = moved;
        self.total_in += consumed as u64;
        match result {
            Ok(status) => Ok(Step {
                consumed,
                produced,
                status,
            }),
            Err(error) => Err(StepFailure {
                consumed,
                produced,
                error,
            }),
        }
    }

    fn run<D: Destination + ?Sized>(
        &mut self,
        engine: &mut Engine,
        config: &DecoderConfig,
        input: &[u8],
        output: &mut D,
        op: Op,
        moved: &mut Moved,
    ) -> Result<StepStatus, DecodeError> {
        let Moved { consumed, produced } = moved;
        loop {
            *produced += self.drain(output, *produced);
            if !self.output.is_empty() {
                return Ok(StepStatus::NeedsOutput);
            }
            if self.phase == Phase::Done {
                return Ok(StepStatus::Finished);
            }
            if let Some(limit) = config.limits().max_input_bytes()
                && self.total_in + *consumed as u64 > limit
            {
                return Err(DecodeError::InputLimitExceeded { limit });
            }
            let rest = &input[*consumed..];
            let between_frames = self.phase == Phase::Frame && self.unit.is_empty();
            if between_frames && self.frames > 0 && rest.is_empty() {
                if op == Op::Finish {
                    self.phase = Phase::Done;
                    continue;
                }
                return Ok(StepStatus::NeedsInput);
            }
            let len = match self.need {
                Some(len) => len,
                None => match self.measure(engine, config, rest)? {
                    Some(len) => {
                        self.need = Some(len);
                        len
                    }
                    None => {
                        self.unit.extend_from_slice(rest);
                        *consumed += rest.len();
                        return match op {
                            Op::Finish => Err(DecodeError::UnexpectedEndOfInput),
                            Op::Process => Ok(StepStatus::NeedsInput),
                        };
                    }
                },
            };
            if self.unit.is_empty() && rest.len() >= len {
                self.finish_unit(engine, config, &rest[..len])?;
                *consumed += len;
            } else {
                let take = (len - self.unit.len()).min(rest.len());
                self.unit.extend_from_slice(&rest[..take]);
                *consumed += take;
                if self.unit.len() < len {
                    return match op {
                        Op::Finish => Err(DecodeError::UnexpectedEndOfInput),
                        Op::Process => Ok(StepStatus::NeedsInput),
                    };
                }
                let unit = core::mem::take(&mut self.unit);
                let outcome = self.finish_unit(engine, config, &unit);
                self.unit = unit;
                self.unit.clear();
                outcome?;
            }
            self.need = None;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Backend;

    fn reference(input: &[u8], chunk_size: usize) -> Vec<u8> {
        openzl_ffi::compress(
            input,
            openzl_ffi::Params {
                chunk_size,
                ..Default::default()
            },
        )
        .unwrap()
    }

    fn drive(
        config: &DecoderConfig,
        src: &[u8],
        piece: usize,
        room: usize,
    ) -> Result<(Vec<u8>, usize), DecodeError> {
        let mut engine = Engine::new(Backend::default());
        let mut stream = Stream::default();
        let mut out = Vec::new();
        let mut buffer = alloc::vec![0u8; room];
        let mut pos = 0;
        loop {
            let end = (pos + piece).min(src.len());
            let op = if end == src.len() {
                Op::Finish
            } else {
                Op::Process
            };
            let step = stream
                .process(&mut engine, config, &src[pos..end], &mut buffer[..], op)
                .map_err(|failure| failure.error)?;
            out.extend_from_slice(&buffer[..step.produced]);
            pos += step.consumed;
            match step.status {
                StepStatus::Finished => return Ok((out, pos)),
                StepStatus::NeedsInput if op == Op::Finish => {
                    return Err(DecodeError::UnexpectedEndOfInput);
                }
                _ => {}
            }
        }
    }

    #[cfg(feature = "zstd")]
    #[test]
    fn any_split_decodes_and_stops_at_the_frame_end() {
        let input: Vec<u8> = (0..90_000u32)
            .map(|i| (i % 241) as u8 ^ (i >> 9) as u8)
            .collect();
        let frame = reference(&input, 32 << 10);
        let src = [&frame[..], b"trailer"].concat();
        let cases: &[(usize, usize)] = if cfg!(miri) {
            &[(5000, 700)]
        } else {
            &[
                (1, 1),
                (3, 4096),
                (64, 7),
                (70_000, 1 << 20),
                (src.len(), 100),
            ]
        };
        for &(piece, room) in cases {
            let (out, consumed) = drive(&DecoderConfig::default(), &src, piece, room).unwrap();
            assert_eq!(out, input, "piece {piece}, room {room}");
            assert_eq!(consumed, frame.len());
        }
    }

    #[test]
    fn concatenated_frames_decode_until_the_input_ends() {
        let first = reference(b"first frame payload", 0);
        let second = reference(b"", 0);
        let src = [&first[..], &second, &first].concat();
        let config = DecoderConfig::from(FrameMode::Concatenated);
        let (out, consumed) = drive(&config, &src, 5, 3).unwrap();
        assert_eq!(out, b"first frame payloadfirst frame payload");
        assert_eq!(consumed, src.len());
    }

    #[cfg(feature = "zstd")]
    #[test]
    fn truncated_and_empty_inputs_fail_at_finish() {
        let frame = reference(&[5u8; 2000], 0);
        for cut in [0, 3, 12, frame.len() - 1] {
            assert_eq!(
                drive(&DecoderConfig::default(), &frame[..cut], 4, 64),
                Err(DecodeError::UnexpectedEndOfInput),
                "{cut}"
            );
        }
    }

    #[test]
    fn errors_report_their_progress_and_bookkeeping_resets() {
        let mut engine = Engine::new(Backend::default());
        let mut stream = Stream::default();
        let mut out = [0u8; 16];
        let failure = stream
            .process(
                &mut engine,
                &DecoderConfig::default(),
                b"not a frame",
                &mut out[..],
                Op::Process,
            )
            .unwrap_err();
        assert_eq!((failure.consumed, failure.produced), (0, 0));
        let frame = reference(b"payload!", 0);
        stream.reset();
        let step = stream
            .process(
                &mut engine,
                &DecoderConfig::default(),
                &frame,
                &mut out[..],
                Op::Finish,
            )
            .unwrap();
        assert_eq!(step.status, StepStatus::Finished);
        assert!(stream.is_finished());
        assert_eq!(
            (stream.total_in(), stream.total_out(), stream.frames()),
            (frame.len() as u64, 8, 1)
        );
        assert!(stream.retained_bytes() > 0 || stream.output.is_empty());
        stream.release();
        assert_eq!(stream.retained_bytes(), 0);
    }

    #[test]
    fn limits_apply_to_streams() {
        let frame = reference(&[5u8; 2000], 0);
        let limits = crate::decompressor::config::DecodeLimits::DEFAULT;
        let config = DecoderConfig::from(limits.with_max_output_bytes(Some(100)));
        assert_eq!(
            drive(&config, &frame, 3, 64),
            Err(DecodeError::OutputLimitExceeded { limit: 100 })
        );
        let config = DecoderConfig::from(limits.with_max_input_bytes(Some(5)));
        assert_eq!(
            drive(&config, &frame, 3, 64),
            Err(DecodeError::InputLimitExceeded { limit: 5 })
        );
    }
}