oxigdal-compress 0.1.7

Advanced compression codecs and auto-selection for geospatial data
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
//! Codec chaining pipeline — compose lossless codecs into a single composite codec.
//!
//! A [`CodecPipeline`] walks its stages forward on compress and in reverse on
//! decompress. The compressed output begins with a self-describing frame header
//! so [`CodecPipeline::decompress_self_describing`] can reconstruct the stage
//! list without the caller knowing the original pipeline.
//!
//! # Frame header
//!
//! The frame header is fixed-stride and self-describing:
//!
//! ```text
//! [ 'O', 'X', 'P', 'L', version=1, num_stages, (stage_id, typesize) * num_stages ]
//! ```
//!
//! The header length is therefore `6 + 2 * num_stages` bytes. The `typesize`
//! byte is present for **every** stage to keep the header fixed-stride; it is
//! `0` for all stages except [`PipelineStage::Shuffle`].
//!
//! # Example
//!
//! ```rust
//! use oxigdal_compress::codecs::{CodecPipeline, PipelineStage};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let pipeline = CodecPipeline::new()
//!     .push(PipelineStage::Shuffle { typesize: 4 })
//!     .push(PipelineStage::Zstd);
//!
//! let data = b"repeated payload ".repeat(256);
//! let compressed = pipeline.compress(&data)?;
//! let restored = CodecPipeline::decompress_self_describing(&compressed)?;
//! assert_eq!(restored, data);
//! # Ok(())
//! # }
//! ```

use crate::codecs::{
    BrotliCodec, DeflateCodec, DeltaCodec, DictionaryCodec, Lz4Codec, RleCodec, SnappyCodec,
    ZstdCodec,
};
use crate::error::CompressionError;

/// One stage of a codec pipeline. Lossless codecs only.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PipelineStage {
    /// Byte-shuffle pre-filter grouping bytes by position within each
    /// `typesize`-byte element. Improves downstream entropy-coder ratios on
    /// fixed-width numeric arrays.
    Shuffle {
        /// Element width in bytes (e.g. `4` for `f32`, `8` for `f64`).
        typesize: u8,
    },
    /// First-order byte-wise delta encoding.
    Delta,
    /// Run-length encoding.
    Rle,
    /// LZ4 block compression.
    Lz4,
    /// Zstandard compression.
    Zstd,
    /// Snappy compression.
    Snappy,
    /// Brotli compression.
    Brotli,
    /// DEFLATE compression.
    Deflate,
    /// Dictionary encoding.
    Dictionary,
}

impl PipelineStage {
    /// One-byte stage id for the frame header.
    fn id(self) -> u8 {
        match self {
            PipelineStage::Shuffle { .. } => 1,
            PipelineStage::Delta => 2,
            PipelineStage::Rle => 3,
            PipelineStage::Lz4 => 4,
            PipelineStage::Zstd => 5,
            PipelineStage::Snappy => 6,
            PipelineStage::Brotli => 7,
            PipelineStage::Deflate => 8,
            PipelineStage::Dictionary => 9,
        }
    }

    /// The per-stage typesize byte written to the frame header. Non-zero only
    /// for [`PipelineStage::Shuffle`].
    fn typesize_byte(self) -> u8 {
        match self {
            PipelineStage::Shuffle { typesize } => typesize,
            _ => 0,
        }
    }

    /// Reconstruct a stage from its id byte. Shuffle carries an extra typesize
    /// byte; for every other stage the typesize byte is ignored.
    fn from_id(id: u8, typesize: u8) -> Result<PipelineStage, CompressionError> {
        Ok(match id {
            1 => PipelineStage::Shuffle { typesize },
            2 => PipelineStage::Delta,
            3 => PipelineStage::Rle,
            4 => PipelineStage::Lz4,
            5 => PipelineStage::Zstd,
            6 => PipelineStage::Snappy,
            7 => PipelineStage::Brotli,
            8 => PipelineStage::Deflate,
            9 => PipelineStage::Dictionary,
            other => {
                return Err(CompressionError::InvalidMetadata(format!(
                    "unknown pipeline stage id {other} in frame header"
                )));
            }
        })
    }
}

/// Frame-header magic bytes — `OXPL` (OXigdal PipeLine).
const PIPELINE_MAGIC: [u8; 4] = [b'O', b'X', b'P', b'L'];

/// Frame-header format version.
const PIPELINE_VERSION: u8 = 1;

/// Size of the fixed prefix: magic(4) + version(1) + num_stages(1).
const HEADER_PREFIX_LEN: usize = 6;

/// Bytes per stage descriptor in the header: id(1) + typesize(1).
const STAGE_DESCRIPTOR_LEN: usize = 2;

/// A composite lossless codec built by chaining individual codec stages.
///
/// Stages are applied left-to-right (in `push` order) on compression and
/// right-to-left on decompression. The compressed payload is prefixed with a
/// self-describing frame header.
#[derive(Debug, Clone, Default)]
pub struct CodecPipeline {
    stages: Vec<PipelineStage>,
}

impl CodecPipeline {
    /// Create an empty pipeline.
    ///
    /// An empty pipeline emits a header-only (6-byte) frame on `compress` and
    /// returns the payload unchanged on `decompress`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Append a stage, returning the pipeline for chaining.
    pub fn push(mut self, stage: PipelineStage) -> Self {
        self.stages.push(stage);
        self
    }

    /// The ordered list of stages in this pipeline.
    pub fn stages(&self) -> &[PipelineStage] {
        &self.stages
    }

    /// Compress `input` through every stage in order, prefixed with a
    /// self-describing frame header.
    ///
    /// # Errors
    ///
    /// Returns a [`CompressionError`] if any stage's codec fails.
    pub fn compress(&self, input: &[u8]) -> Result<Vec<u8>, CompressionError> {
        let mut output = encode_header(&self.stages);

        let mut buffer = input.to_vec();
        for &stage in &self.stages {
            buffer = apply_stage_forward(stage, &buffer)?;
        }

        output.extend_from_slice(&buffer);
        Ok(output)
    }

    /// Decompress a frame produced by [`CodecPipeline::compress`].
    ///
    /// The stage list is read **from the frame header**, not from `self`; the
    /// `self` stage list is used only for API symmetry. Stages are walked in
    /// reverse.
    ///
    /// # Errors
    ///
    /// Returns a [`CompressionError`] if the header is malformed, truncated,
    /// has a bad magic/version, carries an unknown stage id, or if any stage's
    /// codec fails.
    pub fn decompress(&self, input: &[u8]) -> Result<Vec<u8>, CompressionError> {
        let (stages, payload) = decode_header(input)?;
        decompress_payload(&stages, payload)
    }

    /// Reconstruct the pipeline purely from the frame header, then decompress.
    ///
    /// This is the entry point for a caller that holds only the compressed
    /// bytes and does not know which stages produced them.
    ///
    /// # Errors
    ///
    /// Returns a [`CompressionError`] if the header is malformed, truncated,
    /// has a bad magic/version, carries an unknown stage id, or if any stage's
    /// codec fails.
    pub fn decompress_self_describing(input: &[u8]) -> Result<Vec<u8>, CompressionError> {
        let (stages, payload) = decode_header(input)?;
        decompress_payload(&stages, payload)
    }
}

/// Encode the frame header for `stages`.
fn encode_header(stages: &[PipelineStage]) -> Vec<u8> {
    let num_stages = stages.len();
    let mut header = Vec::with_capacity(HEADER_PREFIX_LEN + STAGE_DESCRIPTOR_LEN * num_stages);
    header.extend_from_slice(&PIPELINE_MAGIC);
    header.push(PIPELINE_VERSION);
    // `num_stages` is capped at 255 by the single-byte field; pipelines never
    // approach this in practice.
    header.push(num_stages as u8);
    for &stage in stages {
        header.push(stage.id());
        header.push(stage.typesize_byte());
    }
    header
}

/// Parse and validate a frame header, returning the decoded stage list and a
/// slice over the remaining compressed payload.
fn decode_header(input: &[u8]) -> Result<(Vec<PipelineStage>, &[u8]), CompressionError> {
    if input.len() < HEADER_PREFIX_LEN {
        return Err(CompressionError::InvalidMetadata(format!(
            "pipeline frame truncated: need at least {HEADER_PREFIX_LEN} header bytes, got {}",
            input.len()
        )));
    }

    if input[0..4] != PIPELINE_MAGIC {
        return Err(CompressionError::InvalidMetadata(
            "pipeline frame has bad magic (expected 'OXPL')".to_string(),
        ));
    }

    let version = input[4];
    if version != PIPELINE_VERSION {
        return Err(CompressionError::InvalidMetadata(format!(
            "unsupported pipeline frame version {version} (expected {PIPELINE_VERSION})"
        )));
    }

    let num_stages = input[5] as usize;
    let header_len = HEADER_PREFIX_LEN + STAGE_DESCRIPTOR_LEN * num_stages;
    if input.len() < header_len {
        return Err(CompressionError::InvalidMetadata(format!(
            "pipeline frame truncated: header declares {num_stages} stage(s) \
             needing {header_len} bytes, got {}",
            input.len()
        )));
    }

    let mut stages = Vec::with_capacity(num_stages);
    for stage_index in 0..num_stages {
        let offset = HEADER_PREFIX_LEN + STAGE_DESCRIPTOR_LEN * stage_index;
        let id = input[offset];
        let typesize = input[offset + 1];
        stages.push(PipelineStage::from_id(id, typesize)?);
    }

    Ok((stages, &input[header_len..]))
}

/// Walk `stages` in reverse over `payload`, applying each stage's inverse.
fn decompress_payload(
    stages: &[PipelineStage],
    payload: &[u8],
) -> Result<Vec<u8>, CompressionError> {
    let mut buffer = payload.to_vec();
    for &stage in stages.iter().rev() {
        buffer = apply_stage_inverse(stage, &buffer)?;
    }
    Ok(buffer)
}

/// Group `data` bytes by their position within each `typesize`-byte element.
///
/// For `typesize <= 1` the data is returned unchanged. When
/// `data.len() % typesize != 0`, the trailing partial element's bytes are
/// appended verbatim after the shuffled body so the transform stays reversible.
fn byte_shuffle(data: &[u8], typesize: usize) -> Vec<u8> {
    if typesize <= 1 || data.len() < typesize {
        return data.to_vec();
    }
    let element_count = data.len() / typesize;
    let shuffled_len = element_count * typesize;
    let mut output = Vec::with_capacity(data.len());
    for byte_position in 0..typesize {
        for element_index in 0..element_count {
            output.push(data[element_index * typesize + byte_position]);
        }
    }
    // Tail bytes (incomplete trailing element) pass through unchanged.
    output.extend_from_slice(&data[shuffled_len..]);
    output
}

/// Inverse of [`byte_shuffle`] — regroup planar bytes back into interleaved
/// `typesize`-byte elements. The trailing partial element is restored verbatim.
fn byte_unshuffle(data: &[u8], typesize: usize) -> Vec<u8> {
    if typesize <= 1 || data.len() < typesize {
        return data.to_vec();
    }
    let element_count = data.len() / typesize;
    let shuffled_len = element_count * typesize;
    let mut output = vec![0u8; data.len()];
    for byte_position in 0..typesize {
        for element_index in 0..element_count {
            output[element_index * typesize + byte_position] =
                data[byte_position * element_count + element_index];
        }
    }
    // Restore tail bytes (incomplete trailing element).
    output[shuffled_len..].copy_from_slice(&data[shuffled_len..]);
    output
}

/// Apply a single stage in the forward (compress) direction.
fn apply_stage_forward(stage: PipelineStage, data: &[u8]) -> Result<Vec<u8>, CompressionError> {
    match stage {
        PipelineStage::Shuffle { typesize } => Ok(byte_shuffle(data, typesize as usize)),
        PipelineStage::Delta => DeltaCodec::default().compress(data),
        PipelineStage::Rle => RleCodec::default().compress(data),
        PipelineStage::Lz4 => Lz4Codec::default().compress(data),
        PipelineStage::Zstd => ZstdCodec::default().compress(data),
        PipelineStage::Snappy => SnappyCodec::default().compress(data),
        PipelineStage::Brotli => BrotliCodec::default().compress(data),
        PipelineStage::Deflate => DeflateCodec::default().compress(data),
        PipelineStage::Dictionary => DictionaryCodec::default().compress(data),
    }
}

/// Apply a single stage in the inverse (decompress) direction.
fn apply_stage_inverse(stage: PipelineStage, data: &[u8]) -> Result<Vec<u8>, CompressionError> {
    match stage {
        PipelineStage::Shuffle { typesize } => Ok(byte_unshuffle(data, typesize as usize)),
        PipelineStage::Delta => DeltaCodec::default().decompress(data),
        PipelineStage::Rle => RleCodec::default().decompress(data),
        // Zstd frames embed their own content size, so `None` is a genuine
        // self-describing decode. Lz4 raw blocks carry no such field — see
        // `decompress_lz4_stage` for how that case is handled.
        PipelineStage::Lz4 => decompress_lz4_stage(data),
        PipelineStage::Zstd => ZstdCodec::default().decompress(data, None),
        PipelineStage::Snappy => SnappyCodec::default().decompress(data),
        PipelineStage::Brotli => BrotliCodec::default().decompress(data),
        PipelineStage::Deflate => DeflateCodec::default().decompress(data),
        PipelineStage::Dictionary => DictionaryCodec::default().decompress(data),
    }
}

/// Output-size multipliers tried in order when decompressing a
/// [`PipelineStage::Lz4`] payload with no known target size.
///
/// Raw LZ4 blocks (unlike Zstd frames) carry no embedded decompressed-length
/// field, and the pipeline's frame header does not record per-stage
/// intermediate sizes. `Lz4Codec::decompress`'s own `None`-hint fallback
/// guesses `input.len() * 4`, which undershoots whenever a block compresses
/// better than 4:1 — exactly the common case for a `Shuffle`-then-`Lz4`
/// pipeline over smooth numeric data (e.g. a near-linear-ramp `f32` array).
/// `oxiarc_lz4::decompress_block`'s `max_output` is a safe upper bound, not a
/// required exact size, so retrying with a growing bound converges on the
/// true size without any wire-format change.
const LZ4_SIZE_GUESS_MULTIPLIERS: &[usize] = &[4, 16, 64, 256, 1024, 4096, 16384];

/// Decompress a [`PipelineStage::Lz4`] payload, growing the output-size bound
/// on each retry until decoding succeeds. See [`LZ4_SIZE_GUESS_MULTIPLIERS`].
fn decompress_lz4_stage(data: &[u8]) -> Result<Vec<u8>, CompressionError> {
    if data.is_empty() {
        return Ok(Vec::new());
    }
    let codec = Lz4Codec::default();
    let mut last_err = None;
    for &multiplier in LZ4_SIZE_GUESS_MULTIPLIERS {
        let guess = data.len().saturating_mul(multiplier);
        match codec.decompress(data, Some(guess)) {
            Ok(output) => return Ok(output),
            Err(err) => last_err = Some(err),
        }
    }
    Err(last_err.unwrap_or_else(|| {
        CompressionError::InvalidMetadata(
            "lz4 pipeline stage decompression failed for an empty guess schedule".to_string(),
        )
    }))
}

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

    #[test]
    fn stage_id_round_trips_through_from_id() {
        let cases = [
            PipelineStage::Shuffle { typesize: 4 },
            PipelineStage::Delta,
            PipelineStage::Rle,
            PipelineStage::Lz4,
            PipelineStage::Zstd,
            PipelineStage::Snappy,
            PipelineStage::Brotli,
            PipelineStage::Deflate,
            PipelineStage::Dictionary,
        ];
        for stage in cases {
            let restored = PipelineStage::from_id(stage.id(), stage.typesize_byte())
                .expect("known id must decode");
            assert_eq!(stage, restored);
        }
    }

    #[test]
    fn from_id_rejects_unknown_id() {
        assert!(PipelineStage::from_id(0, 0).is_err());
        assert!(PipelineStage::from_id(10, 0).is_err());
        assert!(PipelineStage::from_id(255, 0).is_err());
    }

    #[test]
    fn byte_shuffle_round_trips_with_tail() {
        // 13 bytes, typesize 4 -> 3 full elements + 1 tail byte.
        let data: Vec<u8> = (0..13u8).collect();
        let shuffled = byte_shuffle(&data, 4);
        assert_eq!(shuffled.len(), data.len());
        let restored = byte_unshuffle(&shuffled, 4);
        assert_eq!(restored, data);
    }

    #[test]
    fn byte_shuffle_identity_for_typesize_one() {
        let data: Vec<u8> = (0..32u8).collect();
        assert_eq!(byte_shuffle(&data, 1), data);
        assert_eq!(byte_unshuffle(&data, 1), data);
    }
}