walker_common/compression/
mod.rs

1//! Helpers for using compression/decompression.
2
3mod detecting;
4mod limit;
5
6pub use detecting::*;
7pub use limit::*;
8
9use anyhow::anyhow;
10use bytes::Bytes;
11use std::io::Write;
12
13/// Decompress a stream, or fail if no encoder was configured.
14///
15/// This function will not consume the data, but return `None`, if no decompression was required.
16/// This allows one to hold on to the original, compressed, data if necessary.
17pub fn decompress_opt(data: &[u8], name: &str) -> Option<Result<Bytes, anyhow::Error>> {
18    let detector = Detector {
19        file_name: Some(name),
20        ..Default::default()
21    };
22    let detected = detector.detect(data).map_err(|err| anyhow!("{err}"));
23
24    detected
25        .and_then(|detected| detected.decompress_opt(data).map_err(|err| err.into()))
26        .transpose()
27}
28
29/// Decompress a stream, or fail if no encoder was configured.
30pub fn decompress(data: Bytes, name: &str) -> Result<Bytes, anyhow::Error> {
31    decompress_opt(&data, name).unwrap_or_else(|| Ok(data))
32}
33
34/// Decompress bz2 using `bzip2-rs` (pure Rust version)
35#[cfg(all(feature = "bzip2-rs", not(feature = "bzip2")))]
36#[deprecated(since = "0.9.3", note = "Use Compression::decompress instead")]
37pub fn decompress_bzip2(data: &[u8]) -> Result<Bytes, std::io::Error> {
38    #[allow(deprecated)]
39    decompress_bzip2_with(data, &DecompressionOptions::default())
40}
41
42/// Decompress bz2 using `bzip2-rs` (pure Rust version)
43#[cfg(all(feature = "bzip2-rs", not(feature = "bzip2")))]
44#[deprecated(since = "0.9.3", note = "Use Compression::decompress instead")]
45pub fn decompress_bzip2_with(
46    data: &[u8],
47    opts: &DecompressionOptions,
48) -> Result<Bytes, std::io::Error> {
49    let decoder = bzip2_rs::DecoderReader::new(data);
50    decompress_limit(decoder, opts.limit)
51}
52
53/// Decompress bz2 using `bzip2` (based on `libbz2`).
54#[cfg(feature = "bzip2")]
55#[deprecated(since = "0.9.3", note = "Use Compression::decompress instead")]
56pub fn decompress_bzip2(data: &[u8]) -> Result<Bytes, std::io::Error> {
57    decompress_bzip2_with(data, &DecompressionOptions::default())
58}
59
60/// Decompress bz2 using `bzip2` (based on `libbz2`).
61#[cfg(feature = "bzip2")]
62fn decompress_bzip2_with(
63    data: &[u8],
64    opts: &DecompressionOptions,
65) -> Result<Bytes, std::io::Error> {
66    let decoder = bzip2::read::BzDecoder::new(data);
67    decompress_limit(decoder, opts.limit)
68}
69
70/// Decompress xz using `liblzma`.
71#[cfg(feature = "liblzma")]
72#[deprecated(since = "0.9.3", note = "Use Compression::decompress instead")]
73pub fn decompress_xz(data: &[u8]) -> Result<Bytes, std::io::Error> {
74    decompress_xz_with(data, &Default::default())
75}
76
77/// Decompress xz using `liblzma`.
78#[cfg(feature = "liblzma")]
79fn decompress_xz_with(data: &[u8], opts: &DecompressionOptions) -> Result<Bytes, std::io::Error> {
80    let decoder = liblzma::read::XzDecoder::new(data);
81    decompress_limit(decoder, opts.limit)
82}
83
84/// Decompress gzip using `zlib-rs` and `flate2`.
85#[cfg(feature = "flate2")]
86fn decompress_gzip_with(data: &[u8], opts: &DecompressionOptions) -> Result<Bytes, std::io::Error> {
87    let decoder = flate2::read::GzDecoder::new(data);
88    decompress_limit(decoder, opts.limit)
89}
90
91/// Decompress with an uncompressed payload limit.
92#[allow(unused)]
93fn decompress_limit(mut reader: impl std::io::Read, limit: usize) -> Result<Bytes, std::io::Error> {
94    let mut data = vec![];
95
96    let data = if limit > 0 {
97        let mut writer = LimitWriter::new(data, limit);
98        std::io::copy(&mut reader, &mut writer)?;
99        writer.flush()?;
100        writer.close()
101    } else {
102        reader.read_to_end(&mut data)?;
103        data
104    };
105
106    Ok(Bytes::from(data))
107}