Skip to main content

compression_codecs/
lib.rs

1//! Adaptors for various compression algorithms.
2
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5use std::io::Result;
6
7pub use compression_core as core;
8
9#[cfg(feature = "brotli")]
10pub mod brotli;
11#[cfg(feature = "bzip2")]
12pub mod bzip2;
13#[cfg(feature = "deflate")]
14pub mod deflate;
15#[cfg(feature = "deflate64")]
16pub mod deflate64;
17#[cfg(feature = "flate2")]
18pub mod flate;
19#[cfg(feature = "gzip")]
20pub mod gzip;
21#[cfg(feature = "lz4")]
22pub mod lz4;
23#[cfg(feature = "lzma")]
24pub mod lzma;
25#[cfg(all(feature = "brotli-mbrotli", async_compression_unstable))]
26pub mod mbrotli;
27#[cfg(all(feature = "zstd-mzstd", async_compression_unstable))]
28pub mod mzstd;
29#[cfg(feature = "xz")]
30pub mod xz;
31#[cfg(feature = "lzma")]
32pub mod xz2;
33#[cfg(feature = "zlib")]
34pub mod zlib;
35#[cfg(all(feature = "zstd-zrip", async_compression_unstable))]
36pub mod zrip;
37#[cfg(feature = "zstd")]
38pub mod zstd;
39
40use compression_core::util::{PartialBuffer, WriteBuffer};
41
42#[cfg(feature = "brotli")]
43pub use self::brotli::{BrotliDecoder, BrotliEncoder};
44#[cfg(feature = "bzip2")]
45pub use self::bzip2::{BzDecoder, BzEncoder};
46#[cfg(feature = "deflate")]
47pub use self::deflate::{DeflateDecoder, DeflateEncoder};
48#[cfg(feature = "deflate64")]
49pub use self::deflate64::Deflate64Decoder;
50#[cfg(feature = "flate2")]
51pub use self::flate::{FlateDecoder, FlateEncoder};
52#[cfg(feature = "gzip")]
53pub use self::gzip::{GzipDecoder, GzipEncoder};
54#[cfg(feature = "lz4")]
55pub use self::lz4::{Lz4Decoder, Lz4Encoder};
56#[cfg(feature = "lzma")]
57pub use self::lzma::{LzmaDecoder, LzmaEncoder};
58#[cfg(all(feature = "brotli-mbrotli", async_compression_unstable))]
59pub use self::mbrotli::{MbrotliDecoder, MbrotliEncoder};
60#[cfg(all(feature = "zstd-mzstd", async_compression_unstable))]
61pub use self::mzstd::{MzstdDecoder, MzstdEncoder};
62#[cfg(feature = "xz")]
63pub use self::xz::{XzDecoder, XzEncoder};
64#[cfg(feature = "lzma")]
65pub use self::xz2::{Xz2Decoder, Xz2Encoder, Xz2FileFormat};
66#[cfg(feature = "zlib")]
67pub use self::zlib::{ZlibDecoder, ZlibEncoder};
68#[cfg(all(feature = "zstd-zrip", async_compression_unstable))]
69pub use self::zrip::{ZripDecoder, ZripEncoder};
70#[cfg(feature = "zstd")]
71pub use self::zstd::{ZstdDecoder, ZstdEncoder};
72
73fn forward_output<R>(
74    output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
75    f: impl FnOnce(&mut WriteBuffer<'_>) -> R,
76) -> R {
77    let written_len = output.written_len();
78
79    let output_buffer = output.get_mut();
80    let mut write_buffer = WriteBuffer::new_initialized(output_buffer.as_mut());
81    write_buffer.advance(written_len);
82
83    let result = f(&mut write_buffer);
84    let new_written_len = write_buffer.written_len();
85    output.advance(new_written_len - written_len);
86    result
87}
88
89fn forward_input_output<R>(
90    input: &mut PartialBuffer<impl AsRef<[u8]>>,
91    output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
92    f: impl FnOnce(&mut PartialBuffer<&[u8]>, &mut WriteBuffer<'_>) -> R,
93) -> R {
94    let written_len = input.written_len();
95
96    let input_buffer = input.get_mut();
97    let mut partial_buffer = PartialBuffer::new(input_buffer.as_ref());
98    partial_buffer.advance(written_len);
99
100    let result = forward_output(output, |output| f(&mut partial_buffer, output));
101    let new_written_len = partial_buffer.written_len();
102    input.advance(new_written_len - written_len);
103    result
104}
105
106pub trait Encode {
107    fn encode(
108        &mut self,
109        input: &mut PartialBuffer<impl AsRef<[u8]>>,
110        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
111    ) -> Result<()>;
112
113    /// Returns whether the internal buffers are flushed
114    fn flush(&mut self, output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>)
115        -> Result<bool>;
116
117    /// Returns whether the internal buffers are flushed and the end of the stream is written
118    fn finish(
119        &mut self,
120        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
121    ) -> Result<bool>;
122}
123impl<T: EncodeV2 + ?Sized> Encode for T {
124    fn encode(
125        &mut self,
126        input: &mut PartialBuffer<impl AsRef<[u8]>>,
127        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
128    ) -> Result<()> {
129        forward_input_output(input, output, |input, output| {
130            EncodeV2::encode(self, input, output)
131        })
132    }
133
134    fn flush(
135        &mut self,
136        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
137    ) -> Result<bool> {
138        forward_output(output, |output| EncodeV2::flush(self, output))
139    }
140
141    fn finish(
142        &mut self,
143        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
144    ) -> Result<bool> {
145        forward_output(output, |output| EncodeV2::finish(self, output))
146    }
147}
148
149/// version 2 of [`Encode`] that is trait object safe.
150///
151/// The different from [`Encode`] is that:
152///  - It doesn't have any generic in it, so it is trait object safe
153///  - It uses [`WriteBuffer`] for output, which will support uninitialized buffer.
154pub trait EncodeV2 {
155    fn encode(
156        &mut self,
157        input: &mut PartialBuffer<&[u8]>,
158        output: &mut WriteBuffer<'_>,
159    ) -> Result<()>;
160
161    /// Returns whether the internal buffers are flushed
162    fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
163
164    /// Returns whether the internal buffers are flushed and the end of the stream is written
165    fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
166}
167
168pub trait Decode {
169    /// Reinitializes this decoder ready to decode a new member/frame of data.
170    fn reinit(&mut self) -> Result<()>;
171
172    /// Returns whether the end of the stream has been read
173    fn decode(
174        &mut self,
175        input: &mut PartialBuffer<impl AsRef<[u8]>>,
176        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
177    ) -> Result<bool>;
178
179    /// Returns whether the internal buffers are flushed
180    fn flush(&mut self, output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>)
181        -> Result<bool>;
182
183    /// Returns whether the internal buffers are flushed
184    fn finish(
185        &mut self,
186        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
187    ) -> Result<bool>;
188}
189
190impl<T: DecodeV2 + ?Sized> Decode for T {
191    fn reinit(&mut self) -> Result<()> {
192        DecodeV2::reinit(self)
193    }
194
195    fn decode(
196        &mut self,
197        input: &mut PartialBuffer<impl AsRef<[u8]>>,
198        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
199    ) -> Result<bool> {
200        forward_input_output(input, output, |input, output| {
201            DecodeV2::decode(self, input, output)
202        })
203    }
204
205    fn flush(
206        &mut self,
207        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
208    ) -> Result<bool> {
209        forward_output(output, |output| DecodeV2::flush(self, output))
210    }
211
212    fn finish(
213        &mut self,
214        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
215    ) -> Result<bool> {
216        forward_output(output, |output| DecodeV2::finish(self, output))
217    }
218}
219
220/// version 2 [`Decode`] that is trait object safe.
221///
222/// The different from [`Decode`] is that:
223///  - It doesn't have any generic in it, so it is trait object safe
224///  - It uses [`WriteBuffer`] for output, which will support uninitialized buffer.
225pub trait DecodeV2 {
226    /// Reinitializes this decoder ready to decode a new member/frame of data.
227    fn reinit(&mut self) -> Result<()>;
228
229    /// Returns whether the end of the stream has been read
230    fn decode(
231        &mut self,
232        input: &mut PartialBuffer<&[u8]>,
233        output: &mut WriteBuffer<'_>,
234    ) -> Result<bool>;
235
236    /// Returns whether the internal buffers are flushed
237    fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
238
239    /// Returns whether the internal buffers are flushed
240    fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
241}
242
243pub trait DecodedSize {
244    /// Returns the size of the input when uncompressed.
245    fn decoded_size(input: &[u8]) -> Result<u64>;
246}