lbzip2 0.5.8

Pure Rust parallel bzip2 decompressor — fast block scanning, multi-core Burrows-Wheeler decode
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
//! Bit-level scanner for bzip2 block boundaries.
//!
//! bzip2 blocks start with the 48-bit magic 0x314159265359 at arbitrary
//! **bit** offsets (not byte-aligned).  We scan a 64-bit sliding window
//! over the input, checking all 16 bit offsets per 2-byte step.
//!
//! For a 100 MB compressed chunk split 12 ways, each split point requires
//! scanning ~500 bytes forward — total work per slot is ~6 KB, effectively
//! zero cost.

use crate::BLOCK_MAGIC;
use crate::FINAL_MAGIC;
use crate::bitreader::BitReader;

/// A block boundary: byte offset and bit offset within that byte (0–7).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BlockBoundary {
    /// Bit offset from the start of the buffer where the 48-bit magic begins.
    pub bit_offset: u64,
}

impl BlockBoundary {
    #[inline]
    pub fn byte_offset(self) -> usize {
        (self.bit_offset / 8) as usize
    }

    #[inline]
    pub fn bit_within_byte(self) -> u8 {
        (self.bit_offset % 8) as u8
    }
}

/// Scan `buf` for the first bzip2 block magic starting at or after bit
/// position `start_bit`.  Returns the bit offset of the magic, or `None`.
///
/// Uses a 64-bit sliding window with unrolled 16-way bit checks per
/// 2-byte step — ~32 bytes/cycle throughput on modern CPUs.
pub fn find_next_block(buf: &[u8], start_bit: u64) -> Option<BlockBoundary> {
    let start_byte = (start_bit / 8) as usize;

    if buf.len() < start_byte + 8 {
        return None;
    }

    let search = &buf[start_byte..];

    // Step through 2 bytes at a time with an 8-byte window.
    let mut byte_idx = 0usize;
    while byte_idx + 8 <= search.len() {
        let window = u64::from_be_bytes(
            search[byte_idx..byte_idx + 8].try_into().unwrap(),
        );

        // Check all 16 bit offsets within this 2-byte step.
        for shift in 0u32..16 {
            let candidate = (window >> (16 - shift)) & 0xFFFF_FFFF_FFFF;
            if candidate == BLOCK_MAGIC {
                let abs_bit = (start_byte + byte_idx) as u64 * 8 + shift as u64;
                if abs_bit >= start_bit {
                    return Some(BlockBoundary { bit_offset: abs_bit });
                }
            }
        }

        byte_idx += 2;
    }

    None
}

/// Find every stream-end marker (the 48-bit FINAL_MAGIC) in `buf` and read the
/// 32-bit combined stream CRC stored immediately after each one, in order.
///
/// A well-formed single bzip2 stream has exactly one FINAL_MAGIC (at the very
/// end); pbzip2-style concatenations have one per sub-stream. The parallel
/// decoder uses this to verify the combined stream CRC without re-walking every
/// block sequentially. Like the block-magic scan, a coincidental 48-bit pattern
/// inside compressed data can produce a spurious extra entry — callers treat a
/// count other than one conservatively (skip the whole-stream check and rely on
/// the always-correct per-block CRCs) so this never causes a false rejection.
pub fn find_stream_crcs(buf: &[u8]) -> Vec<u32> {
    let mut crcs = Vec::new();
    let total_bits = buf.len() as u64 * 8;
    let mut bit = 0u64;
    while bit + 48 <= total_bits {
        match find_next_magic(buf, bit, FINAL_MAGIC) {
            Some(abs_bit) => {
                let after = abs_bit + 48;
                let mut reader = BitReader::from_bit_offset(buf, after as usize);
                if let Some(crc) = reader.read_u32(32) {
                    crcs.push(crc);
                }
                bit = after;
            }
            None => break,
        }
    }
    crcs
}

/// Parallel sibling of [`find_stream_crcs`].
///
/// The serial version walks the WHOLE compressed buffer on one thread to locate
/// the (usually single) trailing FINAL_MAGIC — a pure serial pass that runs on
/// one core while the other 11 sit idle right after the parallel decode. This
/// splits the buffer into `n_threads` chunks, scans each for FINAL_MAGIC in
/// parallel, and reads the trailing 32-bit CRC from the **global** buffer (so a
/// CRC straddling a chunk edge still reads whole). Results are merged in
/// bit-offset order with the same overlap + sort/dedup discipline as
/// [`find_all_blocks_parallel`], so the returned CRC list is identical to the
/// serial scan for both single and concatenated (pbzip2) streams.
pub fn find_stream_crcs_parallel(buf: &[u8], n_threads: usize) -> Vec<u32> {
    let n = n_threads.max(1);
    if buf.len() < 16 || n == 1 {
        return find_stream_crcs(buf);
    }

    let chunk_size = buf.len() / n;

    let per_thread: Vec<Vec<(u64, u32)>> = gatling::gatling_forkjoin::gatling_for_each(n, 0, |i| {
        let start_byte = i * chunk_size;
        // Overlap +8 bytes into the next chunk so a magic spanning the boundary
        // is caught; the global sort/dedup below drops the duplicate that the
        // neighbouring chunk also sees.
        let end_byte = if i + 1 < n {
            ((i + 1) * chunk_size) + 8
        } else {
            buf.len()
        };
        let start_bit = start_byte as u64 * 8;
        let end_bit = end_byte as u64 * 8;

        let mut result: Vec<(u64, u32)> = Vec::new();
        let mut bit = start_bit;
        while bit < end_bit {
            match find_next_magic(buf, bit, FINAL_MAGIC) {
                Some(abs_bit) if abs_bit < end_bit => {
                    let after = abs_bit + 48;
                    let mut reader = BitReader::from_bit_offset(buf, after as usize);
                    if let Some(crc) = reader.read_u32(32) {
                        result.push((abs_bit, crc));
                    }
                    bit = after;
                }
                _ => break,
            }
        }
        result
    });

    let mut all: Vec<(u64, u32)> = per_thread.into_iter().flatten().collect();
    all.sort_by_key(|&(bit, _)| bit);
    all.dedup_by_key(|&mut (bit, _)| bit);
    all.into_iter().map(|(_, crc)| crc).collect()
}

/// Scan `buf` for the first occurrence of the 48-bit `magic` at or after bit
/// position `start_bit`. Shared bit-window scanner used for both BLOCK_MAGIC and
/// FINAL_MAGIC.
fn find_next_magic(buf: &[u8], start_bit: u64, magic: u64) -> Option<u64> {
    let start_byte = (start_bit / 8) as usize;
    if buf.len() < start_byte + 8 {
        return None;
    }
    let search = &buf[start_byte..];
    let mut byte_idx = 0usize;
    while byte_idx + 8 <= search.len() {
        let window = u64::from_be_bytes(search[byte_idx..byte_idx + 8].try_into().unwrap());
        for shift in 0u32..16 {
            let candidate = (window >> (16 - shift)) & 0xFFFF_FFFF_FFFF;
            if candidate == magic {
                let abs_bit = (start_byte + byte_idx) as u64 * 8 + shift as u64;
                if abs_bit >= start_bit {
                    return Some(abs_bit);
                }
            }
        }
        byte_idx += 2;
    }
    None
}

/// Find all block boundaries in `buf`.
pub fn find_all_blocks(buf: &[u8]) -> Vec<BlockBoundary> {
    let mut result = Vec::new();
    let mut bit = 0u64;
    while let Some(b) = find_next_block(buf, bit) {
        result.push(b);
        bit = b.bit_offset + 48; // skip past this magic
    }
    result
}

/// Find all block boundaries using parallel chunk scanning.
///
/// Splits the buffer into `n_threads` chunks, scans each in parallel,
/// then merges and deduplicates the results in bit-offset order.
pub fn find_all_blocks_parallel(buf: &[u8], n_threads: usize) -> Vec<BlockBoundary> {
    let n = n_threads.max(1);
    if buf.len() < 16 || n == 1 {
        return find_all_blocks(buf);
    }

    let chunk_size = buf.len() / n;

    let per_thread: Vec<Vec<BlockBoundary>> = gatling::gatling_forkjoin::gatling_for_each(n, 0, |i| {
            let start_byte = i * chunk_size;
            // Each chunk scans until the START of the next chunk + 8 bytes overlap
            // (so we don't miss a magic that spans the boundary)
            let end_byte = if i + 1 < n {
                ((i + 1) * chunk_size) + 8
            } else {
                buf.len()
            };
            let start_bit = start_byte as u64 * 8;
            let end_bit = end_byte as u64 * 8;

            let mut result = Vec::new();
            let mut bit = start_bit;
            while bit < end_bit {
                match find_next_block(buf, bit) {
                    Some(b) if b.bit_offset < end_bit => {
                        result.push(b);
                        bit = b.bit_offset + 48;
                    }
                    _ => break,
                }
            }
            result
    });
    let mut all: Vec<BlockBoundary> = per_thread.into_iter().flatten().collect();
    all.sort_by_key(|b| b.bit_offset);
    all.dedup_by_key(|b| b.bit_offset);
    all
}

/// Given a compressed buffer and N desired splits, find the N-1 interior
/// block boundaries closest to the nominal split points.
///
/// Returns up to `n_splits - 1` boundaries.  The caller uses these plus
/// bit 0 and EOF to define the N ranges for parallel decompression.
pub fn split_boundaries(buf: &[u8], n_splits: usize) -> Vec<BlockBoundary> {
    if n_splits <= 1 || buf.is_empty() {
        return Vec::new();
    }

    let total_bits = buf.len() as u64 * 8;
    let mut boundaries = Vec::with_capacity(n_splits - 1);

    for i in 1..n_splits {
        let nominal_bit = total_bits * i as u64 / n_splits as u64;
        if let Some(b) = find_next_block(buf, nominal_bit) {
            // Deduplicate: don't add the same boundary twice.
            if boundaries.last().map_or(true, |prev: &BlockBoundary| prev.bit_offset != b.bit_offset) {
                boundaries.push(b);
            }
        }
    }

    boundaries
}

/// Cheap false-positive check: read the block header fields after
/// the 48-bit magic and verify they're structurally valid.
///
/// Reads only 73 bits (~10 bytes) — no Huffman/MTF/BWT decode.
/// Catches random false positives almost instantly.
#[inline]
fn quick_verify_block(buf: &[u8], magic_bit_offset: u64, max_blocksize: u32) -> bool {
    use crate::bitreader::BitReader;

    let total_bits = buf.len() as u64 * 8;
    let after_magic = magic_bit_offset + 48;
    // Need at least 73 bits: CRC(32) + randomised(1) + orig_ptr(24) + bitmap(16)
    if after_magic + 73 > total_bits {
        return false;
    }

    let mut reader = BitReader::from_bit_offset(buf, after_magic as usize);

    // CRC32 — any value is fine
    if reader.read_u32(32).is_none() { return false; }

    // Randomised flag — must be 0 (randomised blocks are obsolete)
    match reader.read_bit() {
        Some(false) => {}
        _ => return false,
    }

    // orig_ptr — must be < max_blocksize
    match reader.read_u32(24) {
        Some(v) if v < max_blocksize => {}
        _ => return false,
    }

    // Symbol group bitmap — at least one of 16 groups must be present
    match reader.read_u16(16) {
        Some(v) if v != 0 => {}
        _ => return false,
    }

    true
}

/// Find a **quick-verified** block boundary starting from `start_bit`.
///
/// Forward-scans for the 48-bit magic, then checks 73 bits of block
/// header structure to reject false positives.  No full decode needed.
pub fn find_quick_boundary(
    buf: &[u8],
    start_bit: u64,
    max_blocksize: u32,
) -> Option<BlockBoundary> {
    let total_bits = buf.len() as u64 * 8;
    let mut search_bit = start_bit;

    loop {
        let candidate = find_next_block(buf, search_bit)?;
        if candidate.bit_offset + 48 + 73 > total_bits {
            return None;
        }
        if quick_verify_block(buf, candidate.bit_offset, max_blocksize) {
            return Some(candidate);
        }
        search_bit = candidate.bit_offset + 1;
    }
}

/// Parallel split boundary search: each of N cores finds its own boundary.
///
/// Each thread calculates its nominal split position, forward-scans for
/// the next BLOCK_MAGIC, and quick-verifies the header (~10 bytes).
/// All N-1 splits run simultaneously — 12x faster than sequential.
pub fn split_boundaries_parallel(
    buf: &[u8],
    n_splits: usize,
    max_blocksize: u32,
) -> Vec<BlockBoundary> {
    if n_splits <= 1 || buf.is_empty() {
        return Vec::new();
    }

    let total_bits = buf.len() as u64 * 8;

    let boundaries: Vec<Option<BlockBoundary>> = gatling::gatling_forkjoin::gatling_for_each(n_splits - 1, 0, |idx| {
            let i = idx + 1;
            let nominal_bit = total_bits * i as u64 / n_splits as u64;
            find_quick_boundary(buf, nominal_bit, max_blocksize)
    });

    // Filter, sort, deduplicate
    let mut result: Vec<BlockBoundary> = boundaries.into_iter().flatten().collect();
    result.sort_by_key(|b| b.bit_offset);
    result.dedup_by_key(|b| b.bit_offset);
    result
}

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

    #[test]
    fn test_find_magic_byte_aligned() {
        // Place magic at byte offset 10, bit-aligned (bit offset 80).
        let mut buf = vec![0u8; 64];
        let magic_bytes: [u8; 6] = [0x31, 0x41, 0x59, 0x26, 0x53, 0x59];
        buf[10..16].copy_from_slice(&magic_bytes);

        let b = find_next_block(&buf, 0).unwrap();
        assert_eq!(b.bit_offset, 80);
        assert_eq!(b.byte_offset(), 10);
        assert_eq!(b.bit_within_byte(), 0);
    }

    #[test]
    fn test_find_magic_bit_shifted() {
        // Place magic shifted by 3 bits into byte offset 10.
        // 48-bit magic at bit offset 83 (byte 10, bit 3).
        let mut buf = vec![0u8; 64];
        let magic: u64 = BLOCK_MAGIC;
        // Write magic at bit offset 83: byte 10 bit 3.
        // That means bits 83..131 carry the magic.
        let shifted = magic << (64 - 48 - 3); // align to bit 3 of a 64-bit word
        let bytes = shifted.to_be_bytes();
        for (i, &b) in bytes.iter().enumerate() {
            if 10 + i < buf.len() {
                buf[10 + i] |= b;
            }
        }

        let b = find_next_block(&buf, 0).unwrap();
        assert_eq!(b.bit_offset, 83);
        assert_eq!(b.byte_offset(), 10);
        assert_eq!(b.bit_within_byte(), 3);
    }

    #[test]
    fn test_no_magic() {
        let buf = vec![0xFFu8; 64];
        assert!(find_next_block(&buf, 0).is_none());
    }

    /// Core-saturation invariant for the readers' switch from the serial
    /// `find_all_blocks` to `find_all_blocks_parallel`: on a REAL multi-block
    /// bzip2 stream the parallel chunk-scan must return the EXACT same boundary
    /// set (same order, no dupes, no misses) as the serial walk, for every core
    /// count. If these agree, the parallel-scanned readers decode identically.
    #[test]
    fn parallel_scan_matches_serial_multiblock() {
        use bzip2::write::BzEncoder;
        use std::io::Write;

        // Level-1 ⇒ 100 KB uncompressed blocks; ~700 KB of varied (barely
        // compressible) data ⇒ several genuine blocks, hence several magics.
        let payload: Vec<u8> = (0..700_000u32)
            .map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8)
            .collect();
        let mut enc = BzEncoder::new(Vec::new(), bzip2::Compression::new(1));
        enc.write_all(&payload).unwrap();
        let compressed = enc.finish().unwrap();

        let serial = find_all_blocks(&compressed);
        assert!(
            serial.len() >= 3,
            "test needs a multi-block stream; got {} block(s)",
            serial.len()
        );

        for n in [2usize, 3, 4, 8, 12, 16, 32] {
            let par = find_all_blocks_parallel(&compressed, n);
            assert_eq!(
                par, serial,
                "parallel scan (n={n}) must equal the serial boundary set"
            );
        }
    }
}