mopenzl 0.2.1

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
//! The context of one node run (`ZL_Encoder`, enc_interface.c).

use alloc::vec::Vec;

use super::super::Params;
use super::super::data::{Bytes, Stream, Type};
use super::super::error::NodeError;
use super::super::params::LocalParams;
use super::super::registry::NodeSpec;
use super::super::registry::dict::Prepared;
#[cfg(feature = "zstd")]
use super::super::zstd::ZstdNode;

/// What a node encoder sees of the engine: its parameters, the global
/// parameters, and the means to create its outputs and its codec header.
///
/// Outputs are collected here and entered into the runtime graph once the
/// node returns: singleton outcomes into the slots reserved for them, variable
/// outcomes appended in the order they were created.
pub(crate) struct NodeCx<'x, 'a> {
    spec: &'x NodeSpec,
    local: Option<&'x LocalParams>,
    #[cfg(any(
        feature = "custom-codec",
        feature = "zstd",
        feature = "lz4",
        feature = "bitunpack",
        feature = "dispatch",
        feature = "field-lz",
        feature = "lz"
    ))]
    params: &'x Params,
    #[cfg(feature = "zstd")]
    pub(crate) zstd: &'x mut ZstdNode,
    /// The node's materialized dictionary (`ZL_Encoder_getMaterializedDict`).
    pub(crate) dictionary: Option<&'x Prepared>,
    /// The SIMD level the compressor selected once; kernels dispatch on it
    /// outside their inner loops.
    pub(crate) simd: fearless_simd::Level,
    singles: Vec<Option<Stream<'a>>>,
    variable: Vec<(usize, Stream<'a>)>,
    header: Option<Vec<u8>>,
}

/// What a finished node run produced.
pub(crate) struct NodeOutputs<'a> {
    /// The singleton outputs, all filled.
    pub(crate) singles: Vec<Stream<'a>>,
    /// The variable outputs as `(outcome, stream)` in creation order.
    pub(crate) variable: Vec<(usize, Stream<'a>)>,
    /// The codec header, if one was sent.
    pub(crate) header: Option<Vec<u8>>,
}

/// The longest codec header a node may send in total.
const HEADER_LIMIT: usize = 1_000_000;

impl<'x, 'a> NodeCx<'x, 'a> {
    pub(crate) fn new(
        spec: &'x NodeSpec,
        local: Option<&'x LocalParams>,
        #[cfg_attr(
            not(any(
                feature = "custom-codec",
                feature = "zstd",
                feature = "lz4",
                feature = "bitunpack",
                feature = "dispatch",
                feature = "field-lz",
                feature = "lz"
            )),
            expect(
                unused_variables,
                reason = "only the nodes that read levels or the format version use them"
            )
        )]
        params: &'x Params,
        #[cfg(feature = "zstd")] zstd: &'x mut ZstdNode,
    ) -> Self {
        Self {
            spec,
            local,
            #[cfg(any(
                feature = "custom-codec",
                feature = "zstd",
                feature = "lz4",
                feature = "bitunpack",
                feature = "dispatch",
                feature = "field-lz",
                feature = "lz"
            ))]
            params,
            #[cfg(feature = "zstd")]
            zstd,
            dictionary: None,
            simd: crate::Backend::default().0,
            singles: (0..spec.singletons.len()).map(|_| None).collect(),
            variable: Vec::new(),
            header: None,
        }
    }

    /// The node being run.
    pub(crate) const fn spec(&self) -> &NodeSpec {
        self.spec
    }

    /// `ZL_CParam_compressionLevel`.
    #[cfg(any(
        feature = "custom-codec",
        feature = "zstd",
        feature = "lz4",
        feature = "field-lz",
        feature = "lz"
    ))]
    pub(crate) const fn level(&self) -> i32 {
        self.params.level
    }

    /// `ZL_CParam_decompressionLevel`.
    #[cfg(feature = "zstd")]
    pub(crate) const fn decompression_level(&self) -> i32 {
        self.params.decompression_level
    }

    /// `ZL_Encoder_getLocalIntParam`: the node's own parameters replace its
    /// defaults when it was parameterized.
    pub(crate) fn int(&self, id: i32) -> Option<i32> {
        match self.local {
            Some(local) => local.int(id),
            None => self
                .spec
                .defaults
                .iter()
                .find(|&&(key, _)| key == id)
                .map(|&(_, value)| value),
        }
    }

    /// All integer parameters in order (`ZL_Encoder_getLocalIntParams`).
    #[cfg(any(feature = "zstd", feature = "lz"))]
    pub(crate) fn ints(&self) -> &[(i32, i32)] {
        match self.local {
            Some(local) => &local.ints[..],
            None => self.spec.defaults,
        }
    }

    /// `ZL_Encoder_getLocalCopyParam`.
    pub(crate) fn copy_param(&self, id: i32) -> Option<&[u8]> {
        self.local?.copy(id)
    }

    /// `ZL_Encoder_getLocalParam`: a reference parameter.
    pub(crate) fn ref_param(&self, id: i32) -> Option<&(dyn core::any::Any + Send + Sync)> {
        self.local?.reference(id)
    }

    fn place(&mut self, outcome: usize, stream: Stream<'a>) -> Result<usize, NodeError> {
        let ty = self
            .spec
            .output_type(outcome)
            .ok_or(NodeError::OutcomeOutOfRange)?;
        if stream.ty() != ty {
            return Err(NodeError::InvalidInput(
                "output type does not match its outcome",
            ));
        }
        let width_ok = match ty {
            Type::Serial | Type::String => stream.width() == 1,
            Type::Numeric => matches!(stream.width(), 1 | 2 | 4 | 8),
            Type::Struct => stream.width() > 0,
        };
        if !width_ok {
            return Err(NodeError::InvalidInput("invalid element width"));
        }
        match self.singles.get_mut(outcome) {
            Some(slot) if slot.is_some() => Err(NodeError::OutputTwice),
            Some(slot) => {
                *slot = Some(stream);
                Ok(outcome)
            }
            None => {
                self.variable.push((outcome, stream));
                Ok(self.singles.len() + self.variable.len() - 1)
            }
        }
    }

    /// `ZL_Encoder_createTypedStream` with its content already written:
    /// outcome `outcome` becomes `bytes` of element width `width`.
    ///
    /// Returns the output's index among the node's outputs.
    ///
    /// # Errors
    ///
    /// An outcome out of range or created twice, or an invalid width.
    pub(crate) fn emit(
        &mut self,
        outcome: usize,
        width: usize,
        bytes: Vec<u8>,
    ) -> Result<usize, NodeError> {
        let ty = self
            .spec
            .output_type(outcome)
            .ok_or(NodeError::OutcomeOutOfRange)?;
        self.place(outcome, Stream::new(ty, width, Bytes::from(bytes)))
    }

    /// `ENC_refTypedStream`: an output that references `n_elts` elements of
    /// `src` starting `offset_bytes` into it, without copying.
    ///
    /// # Errors
    ///
    /// A reference outside `src`, or the errors of [`Self::emit`].
    pub(crate) fn reference(
        &mut self,
        outcome: usize,
        src: &Stream<'a>,
        width: usize,
        n_elts: usize,
        offset_bytes: usize,
    ) -> Result<usize, NodeError> {
        let ty = self
            .spec
            .output_type(outcome)
            .ok_or(NodeError::OutcomeOutOfRange)?;
        let len = width
            .checked_mul(n_elts)
            .ok_or(NodeError::InvalidInput("reference too large"))?;
        let bytes = src
            .content()
            .window(offset_bytes, len)
            .ok_or(NodeError::InvalidInput("reference outside its source"))?;
        self.place(outcome, Stream::new(ty, width, bytes))
    }

    /// A string output that references all of `src` with the given lengths.
    ///
    /// # Errors
    ///
    /// Lengths that do not sum to the size of `src`, or the errors of
    /// [`Self::emit`].
    pub(crate) fn reference_strings(
        &mut self,
        outcome: usize,
        src: &Stream<'a>,
        lens: Vec<u32>,
    ) -> Result<usize, NodeError> {
        let total: u64 = lens.iter().map(|&len| u64::from(len)).sum();
        if total != src.content_size() as u64 {
            return Err(NodeError::InvalidInput(
                "string lengths do not match the content",
            ));
        }
        self.place(outcome, Stream::strings_from(src.content().clone(), lens))
    }

    /// `ZL_Encoder_sendCodecHeader`: the private header the decoder reads;
    /// at most once per run.
    ///
    /// # Errors
    ///
    /// A second header, or one beyond the limit.
    pub(crate) fn send_header(&mut self, header: &[u8]) -> Result<(), NodeError> {
        if self.header.is_some() || header.len() > HEADER_LIMIT {
            return Err(NodeError::Header);
        }
        self.header = Some(header.to_vec());
        Ok(())
    }

    // ---- Entropy package: output metadata ----

    /// [`Self::emit`] followed by `ZL_Output_setIntMetadata` for each pair of
    /// `metadata`: the integer metadata downstream graphs may read (the FSE
    /// and Huffman table streams carry their table log as metadata 0).
    ///
    /// # Errors
    ///
    /// The errors of [`Self::emit`].
    #[cfg(feature = "entropy")]
    pub(crate) fn emit_with_metadata(
        &mut self,
        outcome: usize,
        width: usize,
        bytes: Vec<u8>,
        metadata: &[(i32, i32)],
    ) -> Result<usize, NodeError> {
        let ty = self
            .spec
            .output_type(outcome)
            .ok_or(NodeError::OutcomeOutOfRange)?;
        let mut stream = Stream::new(ty, width, Bytes::from(bytes));
        for &(id, value) in metadata {
            stream.set_metadata(id, value);
        }
        self.place(outcome, stream)
    }

    /// Ends the run: every singleton must have been created.
    pub(crate) fn finish(self) -> Result<NodeOutputs<'a>, NodeError> {
        let singles = self
            .singles
            .into_iter()
            .map(|slot| slot.ok_or(NodeError::OutputNotCommitted))
            .collect::<Result<Vec<_>, _>>()?;
        Ok(NodeOutputs {
            singles,
            variable: self.variable,
            header: self.header,
        })
    }

    // Engine additions of the LZ package: kept together at the end.

    /// `ZL_Output_setIntMetadata`: attaches the integer metadata `id` to the
    /// singleton output `outcome`, which must already have been created.
    ///
    /// # Errors
    ///
    /// An outcome that is not a created singleton.
    #[cfg(feature = "lz")]
    pub(crate) fn set_int_metadata(
        &mut self,
        outcome: usize,
        id: i32,
        value: i32,
    ) -> Result<(), NodeError> {
        match self.singles.get_mut(outcome) {
            Some(Some(stream)) => {
                stream.set_metadata(id, value);
                Ok(())
            }
            _ => Err(NodeError::OutcomeOutOfRange),
        }
    }
}

// Appended by the backends package: the format version for `bitunpack`
// (and `dispatch`).
#[cfg(any(feature = "custom-codec", feature = "bitunpack", feature = "dispatch"))]
impl NodeCx<'_, '_> {
    /// `ZL_Encoder_getCParam(ZL_CParam_formatVersion)`.
    pub(crate) const fn format_version(&self) -> u32 {
        self.params.version
    }
}

// ---------------------------------------------------------------------
// Helpers added by the structure package (tokenize, split, dispatch, ...).
// ---------------------------------------------------------------------

impl<'a> NodeCx<'_, 'a> {
    /// `ZL_Encoder_createStringStream` with its content already written:
    /// outcome `outcome` becomes the strings of lengths `lens` laid out in
    /// `bytes`.
    ///
    /// Returns the output's index among the node's outputs.
    ///
    /// # Errors
    ///
    /// Lengths that do not sum to the size of `bytes`, or the errors of
    /// [`Self::emit`].
    #[cfg(any(
        feature = "custom-codec",
        feature = "tokenize",
        feature = "concat",
        feature = "interleave",
        feature = "prefix",
        feature = "dispatch"
    ))]
    pub(crate) fn emit_strings(
        &mut self,
        outcome: usize,
        bytes: Vec<u8>,
        lens: Vec<u32>,
    ) -> Result<usize, NodeError> {
        let total: u64 = lens.iter().map(|&len| u64::from(len)).sum();
        if total != bytes.len() as u64 {
            return Err(NodeError::InvalidInput(
                "string lengths do not match the content",
            ));
        }
        self.place(outcome, Stream::strings_from(Bytes::from(bytes), lens))
    }

    /// `ZL_Output_setIntMetadata` on the output with index `output` (as
    /// returned by [`Self::emit`] and its siblings).
    ///
    /// # Errors
    ///
    /// An output that was not created yet.
    #[cfg(any(feature = "split", feature = "dispatch"))]
    pub(crate) fn set_output_metadata(
        &mut self,
        output: usize,
        id: i32,
        value: i32,
    ) -> Result<(), NodeError> {
        let singles = self.singles.len();
        let stream = match output.checked_sub(singles) {
            None => self.singles[output].as_mut(),
            Some(index) => self.variable.get_mut(index).map(|(_, stream)| stream),
        };
        stream
            .ok_or(NodeError::OutcomeOutOfRange)?
            .set_metadata(id, value);
        Ok(())
    }
}