| 1 | //! Compress and decompress data in bulk. |
| 2 | //! |
| 3 | //! These methods process all the input data at once. |
| 4 | //! It is therefore best used with relatively small blocks |
| 5 | //! (like small network packets). |
| 6 | |
| 7 | mod compressor; |
| 8 | mod decompressor; |
| 9 | |
| 10 | #[cfg(test)] |
| 11 | mod tests; |
| 12 | |
| 13 | pub use self::compressor::Compressor; |
| 14 | pub use self::decompressor::Decompressor; |
| 15 | |
| 16 | use std::io; |
| 17 | |
| 18 | /// Compresses a single block of data to the given destination buffer. |
| 19 | /// |
| 20 | /// Returns the number of bytes written, or an error if something happened |
| 21 | /// (for instance if the destination buffer was too small). |
| 22 | /// |
| 23 | /// A level of `0` uses zstd's default (currently `3`). |
| 24 | pub fn compress_to_buffer( |
| 25 | source: &[u8], |
| 26 | destination: &mut [u8], |
| 27 | level: i32, |
| 28 | ) -> io::Result<usize> { |
| 29 | Compressor::new(level)?.compress_to_buffer(source, destination) |
| 30 | } |
| 31 | |
| 32 | /// Compresses a block of data and returns the compressed result. |
| 33 | /// |
| 34 | /// A level of `0` uses zstd's default (currently `3`). |
| 35 | pub fn compress(data: &[u8], level: i32) -> io::Result<Vec<u8>> { |
| 36 | Compressor::new(level)?.compress(data) |
| 37 | } |
| 38 | |
| 39 | /// Deompress a single block of data to the given destination buffer. |
| 40 | /// |
| 41 | /// Returns the number of bytes written, or an error if something happened |
| 42 | /// (for instance if the destination buffer was too small). |
| 43 | pub fn decompress_to_buffer( |
| 44 | source: &[u8], |
| 45 | destination: &mut [u8], |
| 46 | ) -> io::Result<usize> { |
| 47 | Decompressor::new()?.decompress_to_buffer(source, destination) |
| 48 | } |
| 49 | |
| 50 | /// Decompresses a block of data and returns the decompressed result. |
| 51 | /// |
| 52 | /// The decompressed data should be at most `capacity` bytes, |
| 53 | /// or an error will be returned. |
| 54 | pub fn decompress(data: &[u8], capacity: usize) -> io::Result<Vec<u8>> { |
| 55 | Decompressor::new()?.decompress(data, capacity) |
| 56 | } |