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
//! High-performance parallel bzip2 decompression library.
//!
//! This library provides efficient parallel decompression of bzip2 files by processing
//! multiple blocks concurrently. It achieves significant speedups on multi-core systems
//! compared to sequential decompression.
//!
//! # Features
//!
//! - **Parallel block decompression**: Utilizes all available CPU cores
//! - **Streaming API**: Implements `std::io::Read` for easy integration
//! - **Memory-efficient**: Uses bounded channels to limit memory usage
//! - **Zero-copy where possible**: Memory-mapped I/O for file access
//! - **Full bzip2 format support**: Handles both single-stream and multi-stream bzip2 files
//! - **Error handling**: Comprehensive error reporting with `anyhow` integration
//!
//! # Architecture
//!
//! The library uses a multi-stage pipeline:
//!
//! 1. **Scanning**: Identifies block boundaries using parallel pattern matching
//! 2. **Decompression**: Processes blocks in parallel using Rayon
//! 3. **Reordering**: Ensures output maintains correct block order
//!
//! # Quick Start
//!
//! The easiest way to use this library is through the `Bz2Decoder`:
//!
//! ```no_run
//! use parallel_bzip2_decoder::Bz2Decoder;
//! use std::io::Read;
//!
//! let mut decoder = Bz2Decoder::open("file.bz2").unwrap();
//! let mut data = Vec::new();
//! decoder.read_to_end(&mut data).unwrap();
//! ```
//!
//! # Advanced Usage
//!
//! For more control, you can use the lower-level functions:
//!
//! ```no_run
//! use parallel_bzip2_decoder::{scan_blocks, decompress_block};
//!
//! let compressed_data = std::fs::read("file.bz2").unwrap();
//! let block_receiver = scan_blocks(&compressed_data);
//!
//! for (start_bit, end_bit) in block_receiver {
//! let decompressed = decompress_block(&compressed_data, start_bit, end_bit).unwrap();
//! // Process decompressed block...
//! }
//! ```
//!
//! # Performance
//!
//! Performance scales nearly linearly with the number of CPU cores. On an 8-core system,
//! expect 6-7x speedup compared to single-threaded bzip2 decompression.
//!
//! # Thread Safety
//!
//! All public types are thread-safe. The library uses Rayon's global thread pool by default,
//! but creates dedicated pools where needed to avoid deadlocks.
//!
//! # Error Handling
//!
//! This crate uses `anyhow` for comprehensive error handling. Most functions return
//! `Result<T, anyhow::Error>` for easy error propagation using the `?` operator.
//!
//! # Memory Usage
//!
//! The library is designed with memory efficiency in mind:
//! - Memory-mapped I/O for large files
//! - Bounded channels to prevent unbounded memory growth
//! - Buffer reuse in block processing
//!
//! # Benchmarks
//!
//! Run benchmarks with `cargo bench` to measure performance on your system.
//! Various benchmark suites test different aspects of performance:
//! - Decode benchmarks with various file sizes
//! - Scanner performance
//! - End-to-end pipeline performance
pub use Bz2Decoder;
pub use ;
pub use ;
/// Maximum allowed uncompressed size for a single bzip2 block (2MB).
/// This protects against decompression bomb attacks.
pub const MAX_BLOCK_SIZE: usize = 2 * 1024 * 1024;
use BzDecoder;
use bounded;
use HashMap;
use Read;
/// Scans bzip2 data for block boundaries and returns them via a channel.
///
/// This function spawns background threads to scan the data in parallel and identify
/// block start and end positions. The results are sent through a channel as
/// (start_bit, end_bit) tuples representing block boundaries.
///
/// # Architecture
///
/// The function creates a two-stage pipeline:
/// 1. **Scanner thread**: Performs parallel chunk-based scanning
/// 2. **Reordering thread**: Collects chunks and converts markers to block boundaries
///
/// # Arguments
///
/// * `data` - The bzip2 compressed data to scan
///
/// # Returns
///
/// A receiver that yields (start_bit, end_bit) tuples for each block found.
/// The receiver will be closed when all blocks have been identified.
///
/// # Performance
///
/// - **Channel buffer**: Sized at 100 to balance memory usage and throughput
/// - **Chunk buffer**: Limited to 4 chunks to prevent excessive memory usage
/// - **Thread safety**: Creates its own thread pool to avoid deadlock
///
/// # Examples
///
/// ```no_run
/// use parallel_bzip2_decoder::scan_blocks;
///
/// let data = std::fs::read("file.bz2").unwrap();
/// let blocks = scan_blocks(&data);
///
/// for (start, end) in blocks {
/// println!("Block from bit {} to bit {}", start, end);
/// }
/// ```
/// Decompresses a single bzip2 block and returns the decompressed data.
///
/// This is a convenience wrapper around `decompress_block_into` that allocates
/// the output buffer for you. For better performance when decompressing multiple
/// blocks, use `decompress_block_into` with reused buffers.
///
/// # Arguments
///
/// * `data` - The complete bzip2 file data
/// * `start_bit` - Bit offset where the block starts
/// * `end_bit` - Bit offset where the block ends
///
/// # Returns
///
/// The decompressed block data
///
/// # Errors
///
/// Returns an error if the block is corrupted or cannot be decompressed.
///
/// # Examples
///
/// ```no_run
/// use parallel_bzip2_decoder::{scan_blocks, decompress_block};
///
/// let data = std::fs::read("file.bz2").unwrap();
/// let blocks = scan_blocks(&data);
///
/// if let Some((start, end)) = blocks.iter().next() {
/// let decompressed = decompress_block(&data, start, end).unwrap();
/// println!("Decompressed {} bytes", decompressed.len());
/// }
/// ```
/// Decompresses a single bzip2 block into provided buffers (zero-allocation).
///
/// This function is optimized for decompressing multiple blocks by reusing buffers.
/// It's used internally by the parallel decoder for maximum performance.
///
/// # Arguments
///
/// * `data` - The complete bzip2 file data
/// * `start_bit` - Bit offset where the block starts
/// * `end_bit` - Bit offset where the block ends
/// * `out` - Output buffer for decompressed data (will be cleared)
/// * `scratch` - Scratch buffer for compressed data with header (will be cleared)
///
/// # Performance
///
/// By reusing `scratch` across multiple calls, this function avoids allocating
/// a new buffer for each block. This is especially important in parallel scenarios
/// where thousands of blocks may be processed.
///
/// # Errors
///
/// Returns an error if the block is corrupted or cannot be decompressed.
///
/// # Examples
///
/// ```no_run
/// use parallel_bzip2_decoder::{scan_blocks, decompress_block_into};
///
/// let data = std::fs::read("file.bz2").unwrap();
/// let blocks = scan_blocks(&data);
///
/// let mut out = Vec::new();
/// let mut scratch = Vec::new();
///
/// for (start, end) in blocks {
/// decompress_block_into(&data, start, end, &mut out, &mut scratch).unwrap();
/// // Process `out`...
/// }
/// ```
/// Decompresses an entire bzip2 file into memory.
///
/// This is a convenience function that combines scanning and decompression.
/// It's primarily used for testing but can be useful for simple use cases.
///
/// For more control or streaming decompression, use [`Bz2Decoder`] instead.
///
/// # Arguments
///
/// * `path` - Path to the bzip2 file
///
/// # Returns
///
/// The complete decompressed file contents
///
/// # Errors
///
/// Returns an error if:
/// - The file cannot be opened
/// - The file is not a valid bzip2 file
/// - Decompression fails
///
/// # Examples
///
/// ```no_run
/// use parallel_bzip2_decoder::decompress_file;
///
/// let data = decompress_file("file.bz2").unwrap();
/// println!("Decompressed {} bytes", data.len());
/// ```
/// Decompresses an entire bzip2 file and returns the decompressed data.
///
/// # Deprecated
///
/// This function has been renamed to [`decompress_file`] for clarity.
/// The name "cat" is Unix jargon that may not be immediately clear to all users.
///
/// # Examples
///
/// ```no_run
/// use parallel_bzip2_decoder::decompress_file;
///
/// let data = decompress_file("file.bz2").unwrap();
/// println!("Decompressed {} bytes", data.len());
/// ```