openreadout-codecs 0.4.0

Pure-Rust image and data decoders used by the OpenReadout format readers
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//! Blosc 1 chunk decoding and LZ4 block decoding (pure Rust).
//!
//! Blosc chunk layout from the public c-blosc documentation (`README_CHUNK_FORMAT.rst`) and the
//! BSD-3-licensed c-blosc sources (`blosc.c` block splitting rules, `blosclz.c` decoder,
//! `shuffle.c` bit-unshuffle rules); the bit transpose from the MIT-licensed bitshuffle
//! library's description; Snappy from its public format description; LZ4 blocks through the
//! MIT-licensed `lz4_flex` crate. Provenance: `docs/provenance/ome-zarr.md`.
//!
//! Every length in the input is checked before use; malformed input is an error, never a panic.

use crate::{CodecError, Result};

/// Byte-shuffle filter applied (flags bit 0).
const FLAG_SHUFFLE: u8 = 0x01;
/// The payload is stored uncompressed after the header (flags bit 1).
const FLAG_MEMCPYED: u8 = 0x02;
/// Bit-shuffle filter applied (flags bit 2).
const FLAG_BITSHUFFLE: u8 = 0x04;
/// Blocks are not split into one stream per byte of the type (flags bit 4).
const FLAG_DONT_SPLIT: u8 = 0x10;
const HEADER: usize = 16;
const MAX_SPLITS: usize = 16;
const MIN_BUFFERSIZE: usize = 128;
/// Largest decoded chunk accepted (a guard against absurd headers).
const MAX_DECODED: usize = 1 << 31;

fn err(detail: impl Into<String>) -> CodecError {
    CodecError::Decode {
        codec: "blosc",
        detail: detail.into(),
    }
}

fn le32(b: &[u8], at: usize) -> Option<usize> {
    let s = b.get(at..at.checked_add(4)?)?;
    Some(u32::from_le_bytes([s[0], s[1], s[2], s[3]]) as usize)
}

/// The compressor recorded in a Blosc header (flags bits 5–7).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BloscCompressor {
    /// Blosc's own LZ77 variant.
    BloscLz,
    /// LZ4 block format.
    Lz4,
    /// Snappy.
    Snappy,
    /// zlib (deflate with a zlib header).
    Zlib,
    /// Zstandard.
    Zstd,
    /// A compressor code this decoder does not know.
    Unknown(u8),
}

impl BloscCompressor {
    fn from_flags(flags: u8) -> Self {
        match flags >> 5 {
            0 => Self::BloscLz,
            1 => Self::Lz4,
            2 => Self::Snappy,
            3 => Self::Zlib,
            4 => Self::Zstd,
            n => Self::Unknown(n),
        }
    }
    /// Name as numcodecs spells it (`cname`).
    pub fn name(self) -> &'static str {
        match self {
            Self::BloscLz => "blosclz",
            Self::Lz4 => "lz4",
            Self::Snappy => "snappy",
            Self::Zlib => "zlib",
            Self::Zstd => "zstd",
            Self::Unknown(_) => "unknown",
        }
    }
}

/// Header fields of a Blosc 1 chunk.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BloscHeader {
    /// Format version of the Blosc header.
    pub version: u8,
    /// Flag bits: shuffle mode (bits 0 and 2), memcpy'd (bit 1), compressor (bits 5 to 7).
    pub flags: u8,
    /// Size of one element in bytes, used by the shuffle filters.
    pub typesize: usize,
    /// Decoded size in bytes.
    pub nbytes: usize,
    /// Uncompressed size of one block in bytes.
    pub blocksize: usize,
    /// Compressed size including the header.
    pub cbytes: usize,
    /// The compressor that produced the blocks.
    pub compressor: BloscCompressor,
}

/// Parse the 16-byte Blosc header.
pub fn blosc_header(data: &[u8]) -> Result<BloscHeader> {
    if data.len() < HEADER {
        return Err(err(format!(
            "{} bytes is shorter than the 16-byte header",
            data.len()
        )));
    }
    let flags = data[2];
    Ok(BloscHeader {
        version: data[0],
        flags,
        typesize: data[3] as usize,
        nbytes: le32(data, 4).unwrap_or(0),
        blocksize: le32(data, 8).unwrap_or(0),
        cbytes: le32(data, 12).unwrap_or(0),
        compressor: BloscCompressor::from_flags(flags),
    })
}

/// Decode one Blosc 1 chunk (as written by c-blosc 1.x / numcodecs `Blosc`).
pub fn blosc_decode(data: &[u8]) -> Result<Vec<u8>> {
    let h = blosc_header(data)?;
    if h.nbytes > MAX_DECODED {
        return Err(err(format!("header declares {} decoded bytes", h.nbytes)));
    }
    if h.cbytes > data.len() {
        return Err(err(format!(
            "header declares {} compressed bytes, {} present (truncated)",
            h.cbytes,
            data.len()
        )));
    }
    let data = &data[..h.cbytes];
    if h.nbytes == 0 {
        return Ok(Vec::new());
    }
    if h.flags & FLAG_MEMCPYED != 0 {
        let body = data
            .get(HEADER..HEADER + h.nbytes)
            .ok_or_else(|| err("stored (memcpyed) payload is truncated"))?;
        return Ok(body.to_vec());
    }
    if h.blocksize == 0 || h.typesize == 0 {
        return Err(err("block size or type size is 0"));
    }
    if h.blocksize > MAX_DECODED {
        return Err(err(format!("header declares a {}-byte block", h.blocksize)));
    }
    let nblocks = h.nbytes.div_ceil(h.blocksize);
    let leftover = h.nbytes % h.blocksize;
    let mut out = vec![0u8; h.nbytes];
    let mut tmp = vec![0u8; h.blocksize.min(h.nbytes)];
    let shuffle = h.flags & FLAG_SHUFFLE != 0 && h.typesize > 1;
    let bitshuffle = h.flags & FLAG_BITSHUFFLE != 0 && h.blocksize >= h.typesize;
    for b in 0..nblocks {
        let start = le32(data, HEADER + 4 * b).ok_or_else(|| err("block offsets truncated"))?;
        let is_leftover = leftover != 0 && b == nblocks - 1;
        let bsize = if is_leftover { leftover } else { h.blocksize };
        let nsplits = if h.flags & FLAG_DONT_SPLIT == 0
            && h.typesize <= MAX_SPLITS
            && h.blocksize / h.typesize >= MIN_BUFFERSIZE
            && !is_leftover
        {
            h.typesize
        } else {
            1
        };
        let neblock = bsize / nsplits;
        let mut src = start;
        let mut filled = 0usize;
        for _ in 0..nsplits {
            let cbytes = le32(data, src).ok_or_else(|| err("split size truncated"))?;
            src += 4;
            let end = src
                .checked_add(cbytes)
                .ok_or_else(|| err("split size overflows"))?;
            let stream = data
                .get(src..end)
                .ok_or_else(|| err(format!("block {b}: split runs past the chunk")))?;
            let dst = tmp
                .get_mut(filled..filled + neblock)
                .ok_or_else(|| err("block larger than declared"))?;
            if cbytes == neblock {
                dst.copy_from_slice(stream);
            } else {
                let dec = decompress_stream(h.compressor, stream, neblock)?;
                if dec.len() != neblock {
                    return Err(err(format!(
                        "block {b}: split decoded to {} bytes, expected {neblock}",
                        dec.len()
                    )));
                }
                dst.copy_from_slice(&dec);
            }
            src = end;
            filled += neblock;
        }
        let dest = out
            .get_mut(b * h.blocksize..b * h.blocksize + bsize)
            .ok_or_else(|| err("block outside the decoded chunk"))?;
        if shuffle {
            unshuffle(h.typesize, &tmp[..bsize], dest);
        } else if bitshuffle {
            bitunshuffle(h.typesize, &tmp[..bsize], dest);
        } else {
            dest.copy_from_slice(&tmp[..bsize]);
        }
    }
    Ok(out)
}

/// Undo the Blosc byte shuffle: byte `j` of element `i` was stored at `j * n + i`; trailing
/// bytes that do not fill a whole element are stored as they are.
fn unshuffle(typesize: usize, src: &[u8], dest: &mut [u8]) {
    let n = src.len() / typesize;
    for i in 0..n {
        for j in 0..typesize {
            dest[i * typesize + j] = src[j * n + i];
        }
    }
    let done = n * typesize;
    dest[done..].copy_from_slice(&src[done..]);
}

/// Undo the bit shuffle of one block (c-blosc `bitunshuffle`): when the block holds a multiple
/// of 8 whole elements, bit `k` of byte `j` of element `i` was stored at bit `i % 8` of byte
/// `(j * 8 + k) * n / 8 + i / 8` (`n` elements; one row of `n` bits per bit of each byte);
/// bytes after the whole elements are stored as they are. Other blocks are stored unshuffled.
fn bitunshuffle(typesize: usize, src: &[u8], dest: &mut [u8]) {
    let n = src.len() / typesize;
    if !n.is_multiple_of(8) {
        dest.copy_from_slice(src);
        return;
    }
    let row = n / 8;
    let done = n * typesize;
    // Eight bit rows of byte `j` give, at byte `g` of each row, an 8 × 8 bit matrix whose
    // transpose is byte `j` of elements 8g..8g+8.
    for j in 0..typesize {
        let rows = &src[j * 8 * row..(j + 1) * 8 * row];
        for g in 0..row {
            let mut x = 0u64;
            for k in 0..8 {
                x |= u64::from(rows[k * row + g]) << (8 * k);
            }
            let y = transpose8(x).to_le_bytes();
            for (i, b) in y.iter().enumerate() {
                dest[(g * 8 + i) * typesize + j] = *b;
            }
        }
    }
    dest[done..].copy_from_slice(&src[done..]);
}

/// Transpose an 8 × 8 bit matrix stored as eight bytes (row `r` in byte `r`, column `c` in
/// bit `c`): bit `8r + c` moves to bit `8c + r`.
fn transpose8(mut x: u64) -> u64 {
    let t = (x ^ (x >> 7)) & 0x00AA_00AA_00AA_00AA;
    x ^= t ^ (t << 7);
    let t = (x ^ (x >> 14)) & 0x0000_CCCC_0000_CCCC;
    x ^= t ^ (t << 14);
    let t = (x ^ (x >> 28)) & 0x0000_0000_F0F0_F0F0;
    x ^ t ^ (t << 28)
}

/// The HDF5 bitshuffle filter (registered filter id 32008, bitshuffle library framing): with
/// LZ4 (`compression` 2), a big-endian u64 decoded size and u32 block size in bytes, then per
/// block of whole elements a big-endian u32 compressed size and an LZ4 block holding the
/// bit-shuffled block; the elements left over after the last whole block of 8 are stored as
/// they are at the end. Without compression (`compression` 0) the chunk is the bit-shuffled
/// data itself, cut into blocks of `block_elems` elements (0: the library's default).
/// `expected` is the decoded size when known (0: take it from the chunk).
pub fn bitshuffle_decode(
    src: &[u8],
    elem_size: usize,
    compression: u32,
    block_elems: usize,
    expected: usize,
) -> Result<Vec<u8>> {
    let e = |d: &str| CodecError::Decode {
        codec: "bitshuffle",
        detail: d.into(),
    };
    if elem_size == 0 {
        return Err(e("element size 0"));
    }
    let be = |at: usize, n: usize| -> Option<u64> {
        let s = src.get(at..at.checked_add(n)?)?;
        Some(s.iter().fold(0u64, |a, &b| a << 8 | u64::from(b)))
    };
    let (total, block_bytes, mut at) = match compression {
        0 => (
            if expected == 0 { src.len() } else { expected },
            block_elems.saturating_mul(elem_size),
            0usize,
        ),
        2 => {
            let total = usize::try_from(be(0, 8).ok_or_else(|| e("header truncated"))?)
                .map_err(|_| e("declared size too large"))?;
            let block = be(8, 4).ok_or_else(|| e("header truncated"))? as usize;
            (total, block, 12)
        }
        other => {
            return Err(CodecError::Decode {
                codec: "bitshuffle",
                detail: format!("compression {other} (only none and LZ4 are decoded)"),
            });
        }
    };
    if total > MAX_DECODED || (expected != 0 && total != expected) {
        return Err(e("declared size does not match the chunk"));
    }
    // LZ4 expands at most about 255 times: a larger declared size is not this chunk's.
    if compression == 2 && total > src.len().saturating_mul(256).saturating_add(64) {
        return Err(e("declared size is larger than the chunk can hold"));
    }
    if !total.is_multiple_of(elem_size) {
        return Err(e("decoded size is not a whole number of elements"));
    }
    let n = total / elem_size;
    // The library's default block: 8 KiB of elements, a multiple of 8, at least 128.
    let block_n = if block_bytes == 0 {
        ((8192 / elem_size) / 8 * 8).max(128)
    } else {
        block_bytes / elem_size
    };
    if block_n == 0 || !block_n.is_multiple_of(8) {
        return Err(e("block size is not a multiple of 8 elements"));
    }
    let mut out = vec![0u8; total];
    let mut done = 0usize; // elements decoded
    let tail = n % 8;
    while done + 8 <= n {
        let this = block_n.min((n - tail) - done);
        let bytes = this * elem_size;
        let shuffled: std::borrow::Cow<'_, [u8]> = if compression == 2 {
            let c = be(at, 4).ok_or_else(|| e("block header truncated"))? as usize;
            at += 4;
            let end = at.checked_add(c).ok_or_else(|| e("block size overflows"))?;
            let s = src
                .get(at..end)
                .ok_or_else(|| e("block runs past the chunk"))?;
            at = end;
            std::borrow::Cow::Owned(lz4_block_decode(s, bytes)?)
        } else {
            let s = src
                .get(at..at + bytes)
                .ok_or_else(|| e("chunk shorter than its data"))?;
            at += bytes;
            std::borrow::Cow::Borrowed(s)
        };
        if shuffled.len() != bytes {
            return Err(e("block decoded to the wrong size"));
        }
        let dest = &mut out[done * elem_size..done * elem_size + bytes];
        bitunshuffle(elem_size, &shuffled, dest);
        done += this;
    }
    let rest = tail * elem_size;
    let s = src
        .get(at..at + rest)
        .ok_or_else(|| e("chunk ends before its last elements"))?;
    out[done * elem_size..].copy_from_slice(s);
    Ok(out)
}

/// A Snappy raw block (public format description, `format_description.txt`): a varint decoded
/// length, then literals and back references (1, 2 or 4-byte offsets). Must decode to exactly
/// `expected` bytes.
pub fn snappy_decode(src: &[u8], expected: usize) -> Result<Vec<u8>> {
    let bad = |d: &str| CodecError::Decode {
        codec: "snappy",
        detail: d.into(),
    };
    let mut ip = 0usize;
    let mut len = 0usize;
    let mut shift = 0u32;
    loop {
        let b = *src.get(ip).ok_or_else(|| bad("length truncated"))?;
        ip += 1;
        len |= usize::from(b & 0x7f)
            .checked_shl(shift)
            .ok_or_else(|| bad("length overflows"))?;
        if b & 0x80 == 0 {
            break;
        }
        shift += 7;
        if shift > 28 {
            return Err(bad("length overflows"));
        }
    }
    if len != expected || len > MAX_DECODED {
        return Err(bad(&format!("declares {len} bytes, expected {expected}")));
    }
    let mut out: Vec<u8> = Vec::with_capacity(len);
    let le = |at: usize, n: usize| -> Option<usize> {
        let s = src.get(at..at.checked_add(n)?)?;
        Some(s.iter().rev().fold(0usize, |a, &b| a << 8 | usize::from(b)))
    };
    while ip < src.len() {
        let tag = src[ip];
        ip += 1;
        let (n, offset) = match tag & 3 {
            0 => {
                let mut n = usize::from(tag >> 2);
                if n >= 60 {
                    let extra = n - 59;
                    n = le(ip, extra).ok_or_else(|| bad("literal length truncated"))?;
                    ip += extra;
                }
                n += 1;
                let lit = src
                    .get(ip..ip.checked_add(n).ok_or_else(|| bad("literal overflows"))?)
                    .ok_or_else(|| bad("literal runs past the input"))?;
                if out.len() + n > len {
                    return Err(bad("literal runs past the declared length"));
                }
                out.extend_from_slice(lit);
                ip += n;
                continue;
            }
            1 => {
                let o = *src.get(ip).ok_or_else(|| bad("copy truncated"))?;
                ip += 1;
                (
                    4 + usize::from((tag >> 2) & 7),
                    usize::from(tag >> 5) << 8 | usize::from(o),
                )
            }
            2 => {
                let o = le(ip, 2).ok_or_else(|| bad("copy truncated"))?;
                ip += 2;
                (1 + usize::from(tag >> 2), o)
            }
            _ => {
                let o = le(ip, 4).ok_or_else(|| bad("copy truncated"))?;
                ip += 4;
                (1 + usize::from(tag >> 2), o)
            }
        };
        if offset == 0 || offset > out.len() || out.len() + n > len {
            return Err(bad("back reference outside the output"));
        }
        let from = out.len() - offset;
        for k in 0..n {
            let v = out[from + k];
            out.push(v);
        }
    }
    if out.len() != len {
        return Err(bad(&format!("decoded {} of {len} bytes", out.len())));
    }
    Ok(out)
}

fn decompress_stream(c: BloscCompressor, src: &[u8], expected: usize) -> Result<Vec<u8>> {
    match c {
        BloscCompressor::BloscLz => blosclz_decode(src, expected),
        BloscCompressor::Lz4 => lz4_block_decode(src, expected),
        BloscCompressor::Snappy => snappy_decode(src, expected),
        BloscCompressor::Zlib => crate::zlib_decode(src, expected),
        BloscCompressor::Zstd => crate::zstd_decode(src, expected),
        BloscCompressor::Unknown(_) => Err(err(format!(
            "the {} compressor inside Blosc is not decoded",
            c.name()
        ))),
    }
}

/// BloscLZ (the default Blosc compressor), following `blosclz_decompress` in c-blosc (BSD-3).
pub fn blosclz_decode(src: &[u8], maxout: usize) -> Result<Vec<u8>> {
    const MAX_DISTANCE: usize = 8191;
    let bad = || err("malformed BloscLZ stream");
    let mut out: Vec<u8> = Vec::with_capacity(maxout);
    if src.is_empty() {
        return Ok(out);
    }
    let mut ip = 0usize;
    let mut ctrl = usize::from(src[ip] & 31);
    ip += 1;
    loop {
        if ctrl >= 32 {
            let mut len = (ctrl >> 5) - 1;
            let ofs = (ctrl & 31) << 8;
            if len == 6 {
                loop {
                    if ip + 1 >= src.len() {
                        return Err(bad());
                    }
                    let code = src[ip];
                    ip += 1;
                    len += usize::from(code);
                    if code != 255 {
                        break;
                    }
                }
            } else if ip + 1 >= src.len() {
                return Err(bad());
            }
            let code = usize::from(src[ip]);
            ip += 1;
            len += 3;
            // distance back from the output position, minus one (see `ref--` in C)
            let mut back = ofs + code;
            if code == 255 && ofs == (31 << 8) {
                if ip + 1 >= src.len() {
                    return Err(bad());
                }
                let far = (usize::from(src[ip]) << 8) + usize::from(src[ip + 1]);
                ip += 2;
                back = far + MAX_DISTANCE;
            }
            back += 1;
            if out.len() + len > maxout || back > out.len() {
                return Err(bad());
            }
            let from = out.len() - back;
            for k in 0..len {
                let v = out[from + k];
                out.push(v);
            }
            if ip >= src.len() {
                break;
            }
            ctrl = usize::from(src[ip]);
            ip += 1;
        } else {
            let n = ctrl + 1;
            if out.len() + n > maxout || ip + n > src.len() {
                return Err(bad());
            }
            out.extend_from_slice(&src[ip..ip + n]);
            ip += n;
            if ip >= src.len() {
                break;
            }
            ctrl = usize::from(src[ip]);
            ip += 1;
        }
    }
    Ok(out)
}

/// One raw LZ4 block (no frame, no size prefix) that decodes to exactly `expected` bytes.
pub fn lz4_block_decode(src: &[u8], expected: usize) -> Result<Vec<u8>> {
    #[cfg(feature = "lz4")]
    {
        lz4_flex::block::decompress(src, expected).map_err(|e| CodecError::Decode {
            codec: "lz4",
            detail: e.to_string(),
        })
    }
    #[cfg(not(feature = "lz4"))]
    {
        let _ = (src, expected);
        Err(CodecError::NotCompiled {
            codec: "lz4",
            feature: "lz4",
        })
    }
}

/// numcodecs `LZ4`: a little-endian u32 decoded size, then one LZ4 block.
pub fn lz4_sized_decode(src: &[u8]) -> Result<Vec<u8>> {
    let n = le32(src, 0).ok_or_else(|| CodecError::Decode {
        codec: "lz4",
        detail: "missing 4-byte size prefix".into(),
    })?;
    if n > MAX_DECODED {
        return Err(CodecError::Decode {
            codec: "lz4",
            detail: format!("size prefix declares {n} bytes"),
        });
    }
    lz4_block_decode(&src[4..], n)
}

/// The HDF5 LZ4 filter (registered filter id 32004): a big-endian u64 decoded size and a
/// big-endian u32 block size, then per block a big-endian u32 compressed size and the LZ4
/// block (stored as is when the compressed size equals the block's decoded size).
pub fn hdf5_lz4_decode(src: &[u8]) -> Result<Vec<u8>> {
    let e = |d: &str| CodecError::Decode {
        codec: "hdf5-lz4",
        detail: d.into(),
    };
    let be = |at: usize, n: usize| -> Option<u64> {
        let s = src.get(at..at.checked_add(n)?)?;
        Some(s.iter().fold(0u64, |a, &b| a << 8 | u64::from(b)))
    };
    let total = usize::try_from(be(0, 8).ok_or_else(|| e("header truncated"))?)
        .map_err(|_| e("declared size too large"))?;
    let block = be(8, 4).ok_or_else(|| e("header truncated"))? as usize;
    if total > MAX_DECODED {
        return Err(e("declared size too large"));
    }
    if block == 0 && total > 0 {
        return Err(e("block size 0"));
    }
    let mut out = Vec::with_capacity(total);
    let mut at = 12usize;
    while out.len() < total {
        let want = block.min(total - out.len());
        let c = be(at, 4).ok_or_else(|| e("block header truncated"))? as usize;
        at += 4;
        let end = at.checked_add(c).ok_or_else(|| e("block size overflows"))?;
        let s = src
            .get(at..end)
            .ok_or_else(|| e("block runs past the chunk"))?;
        if c == want {
            out.extend_from_slice(s);
        } else {
            out.extend_from_slice(&lz4_block_decode(s, want)?);
        }
        at = end;
    }
    Ok(out)
}

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

    #[test]
    fn memcpyed_and_shuffled_stored_blocks() {
        // stored payload
        let mut c = vec![
            2u8,
            1,
            FLAG_MEMCPYED,
            2,
            4,
            0,
            0,
            0,
            4,
            0,
            0,
            0,
            20,
            0,
            0,
            0,
        ];
        c.extend_from_slice(&[1, 2, 3, 4]);
        assert_eq!(blosc_decode(&c).unwrap(), vec![1, 2, 3, 4]);
        // one block of 4 u16 values, shuffled, split size == neblock (stored raw)
        let mut c = vec![
            2u8,
            1,
            FLAG_SHUFFLE | FLAG_DONT_SPLIT,
            2,
            8,
            0,
            0,
            0,
            8,
            0,
            0,
            0,
        ];
        let body_start = 16 + 4;
        c.extend_from_slice(&((body_start + 4 + 8) as u32).to_le_bytes());
        c.extend_from_slice(&(body_start as u32).to_le_bytes());
        c.extend_from_slice(&8u32.to_le_bytes());
        // values 0x0201, 0x0403, 0x0605, 0x0807 shuffled: low bytes then high bytes
        c.extend_from_slice(&[1, 3, 5, 7, 2, 4, 6, 8]);
        assert_eq!(blosc_decode(&c).unwrap(), vec![1, 2, 3, 4, 5, 6, 7, 8]);
        // truncated
        assert!(blosc_decode(&c[..c.len() - 3]).is_err());
        assert!(blosc_decode(&c[..10]).is_err());
    }

    #[test]
    fn bitunshuffle_transposes_bits() {
        // 8 u16 elements 0x0001, 0x0002, ..., 0x0008: bit rows of 8 bits (1 byte) each
        let vals: Vec<u16> = (1..=8).collect();
        let mut shuffled = vec![0u8; 16];
        for (i, v) in vals.iter().enumerate() {
            for j in 0..2 {
                let byte = v.to_le_bytes()[j];
                for k in 0..8 {
                    if byte >> k & 1 == 1 {
                        shuffled[j * 8 + k] |= 1 << i;
                    }
                }
            }
        }
        let mut out = vec![0u8; 16];
        bitunshuffle(2, &shuffled, &mut out);
        let back: Vec<u16> = out
            .chunks(2)
            .map(|c| u16::from_le_bytes([c[0], c[1]]))
            .collect();
        assert_eq!(back, vals);
        // not a multiple of 8 elements: stored as it is
        let mut out = vec![0u8; 6];
        bitunshuffle(2, &[1, 2, 3, 4, 5, 6], &mut out);
        assert_eq!(out, [1, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn transpose8_matches_bit_by_bit() {
        for seed in [1u64, 0x0123_4567_89ab_cdef, u64::MAX, 0x8000_0000_0000_0001] {
            let t = transpose8(seed);
            for r in 0..8 {
                for c in 0..8 {
                    assert_eq!(seed >> (8 * r + c) & 1, t >> (8 * c + r) & 1);
                }
            }
        }
    }

    /// Bit-shuffle `vals` (u16) the slow way.
    fn shuffle_u16(vals: &[u16]) -> Vec<u8> {
        let n = vals.len();
        let mut out = vec![0u8; n * 2];
        for (i, v) in vals.iter().enumerate() {
            for j in 0..2 {
                for k in 0..8 {
                    if v.to_le_bytes()[j] >> k & 1 == 1 {
                        out[(j * 8 + k) * n / 8 + i / 8] |= 1 << (i % 8);
                    }
                }
            }
        }
        out
    }

    #[cfg(feature = "lz4")]
    #[test]
    fn bitshuffle_lz4_chunks_with_a_tail() {
        // 21 u16 values: two blocks of 8 (block size 16 bytes), then 5 left over stored raw.
        let vals: Vec<u16> = (0..21).map(|i| (i * 977 + 3) as u16).collect();
        let mut c = Vec::new();
        c.extend_from_slice(&(42u64).to_be_bytes());
        c.extend_from_slice(&(16u32).to_be_bytes());
        for b in 0..2 {
            let block = shuffle_u16(&vals[b * 8..b * 8 + 8]);
            let z = lz4_flex::block::compress(&block);
            c.extend_from_slice(&(z.len() as u32).to_be_bytes());
            c.extend_from_slice(&z);
        }
        for v in &vals[16..] {
            c.extend_from_slice(&v.to_le_bytes());
        }
        let out = bitshuffle_decode(&c, 2, 2, 0, 42).unwrap();
        let back: Vec<u16> = out
            .chunks(2)
            .map(|c| u16::from_le_bytes([c[0], c[1]]))
            .collect();
        assert_eq!(back, vals);
        assert!(bitshuffle_decode(&c[..c.len() - 1], 2, 2, 0, 42).is_err());
        assert!(bitshuffle_decode(&c, 2, 2, 0, 40).is_err());
        assert!(bitshuffle_decode(&c, 2, 3, 0, 42).is_err());
        // fuzz finding: a declared size that is not a whole number of elements
        assert!(bitshuffle_decode(&c, 4, 2, 0, 42).is_err());
        // uncompressed: the shuffled data itself
        let raw = shuffle_u16(&vals[..16]);
        let out = bitshuffle_decode(&raw, 2, 0, 16, 32).unwrap();
        assert_eq!(
            out.chunks(2)
                .map(|c| u16::from_le_bytes([c[0], c[1]]))
                .collect::<Vec<_>>(),
            vals[..16]
        );
    }

    #[test]
    fn snappy_literals_and_copies() {
        // "abcabcabcd": literal "abc", copy len 6 offset 3 (1-byte offset form), literal "d"
        let src = [10u8, 0x08, b'a', b'b', b'c', 0x09, 3, 0x00, b'd'];
        assert_eq!(snappy_decode(&src, 10).unwrap(), b"abcabcabcd");
        assert!(snappy_decode(&src, 9).is_err());
        assert!(snappy_decode(&src[..6], 10).is_err());
        assert!(snappy_decode(&[4, 0x05, 9, 0], 4).is_err());
    }

    #[test]
    fn blosclz_literal_and_match() {
        // literal run of 3 ("abc"), a match of length 3 at distance 3, a literal "d"
        let src = [2u8, b'a', b'b', b'c', 0x20, 2, 0, b'd'];
        assert_eq!(blosclz_decode(&src, 7).unwrap(), b"abcabcd");
        assert!(blosclz_decode(&[0, b'x', 0x20, 5, 0, b'y'], 8).is_err());
    }

    #[test]
    fn hdf5_lz4_stored_block() {
        let mut s = Vec::new();
        s.extend_from_slice(&4u64.to_be_bytes());
        s.extend_from_slice(&4u32.to_be_bytes());
        s.extend_from_slice(&4u32.to_be_bytes());
        s.extend_from_slice(&[9, 8, 7, 6]);
        assert_eq!(hdf5_lz4_decode(&s).unwrap(), vec![9, 8, 7, 6]);
        assert!(hdf5_lz4_decode(&s[..14]).is_err());
    }
}