Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to pass buffer larger than size, warn on missing docs #78

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/block/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,21 +462,22 @@ pub fn decompress_into_with_dict(
}

/// Decompress all bytes of `input` into a new vec.
/// The passed parameter `min_uncompressed_size` needs to be equal or larger than the uncompressed size.
///
/// # Panics
/// May panic if the parameter `min_uncompressed_size` is smaller than the
/// uncompressed data.

#[inline]
pub fn decompress_with_dict(
input: &[u8],
uncompressed_size: usize,
min_uncompressed_size: usize,
ext_dict: &[u8],
) -> Result<Vec<u8>, DecompressError> {
// Allocate a vector to contain the decompressed stream.
let mut vec = get_vec_with_size(uncompressed_size);
let mut vec = get_vec_with_size(min_uncompressed_size);
let decomp_len = decompress_into_with_dict(input, &mut vec[..], ext_dict)?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
vec.truncate(decomp_len);
Ok(vec)
}

Expand All @@ -489,17 +490,17 @@ pub fn decompress_size_prepended(input: &[u8]) -> Result<Vec<u8>, DecompressErro
}

/// Decompress all bytes of `input` into a new vec.
/// The passed parameter `min_uncompressed_size` needs to be equal or larger than the uncompressed size.
///
/// # Panics
/// May panic if the parameter `min_uncompressed_size` is smaller than the
/// uncompressed data.
#[inline]
pub fn decompress(input: &[u8], uncompressed_size: usize) -> Result<Vec<u8>, DecompressError> {
pub fn decompress(input: &[u8], min_uncompressed_size: usize) -> Result<Vec<u8>, DecompressError> {
// Allocate a vector to contain the decompressed stream.
let mut vec = get_vec_with_size(uncompressed_size);
let mut vec = get_vec_with_size(min_uncompressed_size);
let decomp_len = decompress_into(input, &mut vec[..])?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
vec.truncate(decomp_len);
Ok(vec)
}

Expand Down
36 changes: 18 additions & 18 deletions src/block/decompress_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::convert::TryInto;
use crate::block::DecompressError;
use crate::block::MINMATCH;
use crate::sink::SliceSink;
use alloc::vec;
use alloc::vec::Vec;

/// Read an integer.
Expand Down Expand Up @@ -323,18 +324,18 @@ pub fn decompress_size_prepended(input: &[u8]) -> Result<Vec<u8>, DecompressErro
}

/// Decompress all bytes of `input` into a new vec.
/// The passed parameter `min_uncompressed_size` needs to be equal or larger than the uncompressed size.
///
/// # Panics
/// May panic if the parameter `min_uncompressed_size` is smaller than the
/// uncompressed data.
#[inline]
pub fn decompress(input: &[u8], uncompressed_size: usize) -> Result<Vec<u8>, DecompressError> {
let mut decompressed: Vec<u8> = Vec::with_capacity(uncompressed_size);
decompressed.resize(uncompressed_size, 0);
pub fn decompress(input: &[u8], min_uncompressed_size: usize) -> Result<Vec<u8>, DecompressError> {
let mut decompressed: Vec<u8> = Vec::with_capacity(min_uncompressed_size);
decompressed.resize(min_uncompressed_size, 0);
let decomp_len =
decompress_internal::<false>(input, &mut SliceSink::new(&mut decompressed, 0), b"")?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
decompressed.truncate(decomp_len);
Ok(decompressed)
}

Expand All @@ -350,22 +351,21 @@ pub fn decompress_size_prepended_with_dict(
}

/// Decompress all bytes of `input` into a new vec.
/// The passed parameter `min_uncompressed_size` needs to be equal or larger than the uncompressed size.
///
/// # Panics
/// May panic if the parameter `min_uncompressed_size` is smaller than the
/// uncompressed data.
#[inline]
pub fn decompress_with_dict(
input: &[u8],
uncompressed_size: usize,
min_uncompressed_size: usize,
ext_dict: &[u8],
) -> Result<Vec<u8>, DecompressError> {
let mut decompressed: Vec<u8> = Vec::with_capacity(uncompressed_size);
decompressed.resize(uncompressed_size, 0);
let mut decompressed: Vec<u8> = vec![0; min_uncompressed_size];
let decomp_len =
decompress_internal::<true>(input, &mut SliceSink::new(&mut decompressed, 0), ext_dict)?;
if decomp_len != uncompressed_size {
return Err(DecompressError::UncompressedSizeDiffers {
expected: uncompressed_size,
actual: decomp_len,
});
}
decompressed.truncate(decomp_len);
Ok(decompressed)
}

Expand Down
16 changes: 5 additions & 11 deletions src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ static LZ4_64KLIMIT: usize = (64 * 1024) + (MFLIMIT - 1);
#[derive(Debug)]
#[non_exhaustive]
pub enum DecompressError {
/// The provided output is too small
OutputTooSmall {
/// Minimum expected output size
expected: usize,
actual: usize,
},
UncompressedSizeDiffers {
expected: usize,
/// Actual size of output
actual: usize,
},
/// Literal is out of bounds of the input
Expand All @@ -99,7 +98,9 @@ pub enum DecompressError {

#[derive(Debug)]
#[non_exhaustive]
/// Errors that can happen during compression.
pub enum CompressError {
/// The provided output is too small.
OutputTooSmall,
}

Expand All @@ -123,13 +124,6 @@ impl fmt::Display for DecompressError {
DecompressError::OffsetOutOfBounds => {
f.write_str("the offset to copy is not contained in the decompressed buffer")
}
DecompressError::UncompressedSizeDiffers { actual, expected } => {
write!(
f,
"the expected decompressed size differs, actual {}, expected {}",
actual, expected
)
}
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/frame/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ impl<R: io::Read> FrameDecoder<R> {
}
}

pub fn frame_info(&mut self) -> Option<&FrameInfo> {
self.current_frame_info.as_ref()
}

/// Gets a reference to the underlying reader in this decoder.
pub fn get_ref(&self) -> &R {
&self.r
Expand Down
10 changes: 9 additions & 1 deletion src/frame/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ pub(crate) const MAX_FRAME_INFO_SIZE: usize = 19;
pub(crate) const BLOCK_INFO_SIZE: usize = 4;

#[derive(Clone, Copy, PartialEq, Debug)]
/// Different predefines blocksizes to choose when compressing data.
pub enum BlockSize {
/// The default block size.
Max64KB = 4,
/// 256KB block size.
Max256KB = 5,
/// 1MB block size.
Max1MB = 6,
/// 4MB block size.
Max4MB = 7,
/// 8MB block size.
Max8MB = 8,
}

Expand All @@ -51,7 +56,7 @@ impl Default for BlockSize {
}

impl BlockSize {
pub fn get_size(&self) -> usize {
pub(crate) fn get_size(&self) -> usize {
match self {
BlockSize::Max64KB => 64 * 1024,
BlockSize::Max256KB => 256 * 1024,
Expand All @@ -63,6 +68,7 @@ impl BlockSize {
}

#[derive(Clone, Copy, PartialEq, Debug)]
/// The two `BlockMode` operations that can be set on (`FrameInfo`)[FrameInfo]
pub enum BlockMode {
/// Every block is compressed independently. The default.
Independent,
Expand Down Expand Up @@ -114,6 +120,7 @@ impl Default for BlockMode {
// | 4 bytes | | 0 - 4 bytes |
//
#[derive(Debug, Clone)]
/// The metadata for de/compressing with lz4 frame format.
pub struct FrameInfo {
/// If set, includes the total uncompressed size of data in the frame.
pub content_size: Option<u64>,
Expand Down Expand Up @@ -142,6 +149,7 @@ impl Default for FrameInfo {
}

impl FrameInfo {
/// Create a new `FrameInfo`.
pub fn new() -> Self {
Self {
content_size: None,
Expand Down
8 changes: 7 additions & 1 deletion src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub use header::{BlockMode, BlockSize, FrameInfo};

#[derive(Debug)]
#[non_exhaustive]
/// Errors that can occur when de/compressing lz4.
pub enum Error {
/// Compression error.
CompressionError(crate::block::CompressError),
Expand Down Expand Up @@ -62,7 +63,12 @@ pub enum Error {
/// External dictionaries are not supported.
DictionaryNotSupported,
/// Content length differs.
ContentLengthError { expected: u64, actual: u64 },
ContentLengthError {
/// Expected content length.
expected: u64,
/// Actual content lenght.
actual: u64,
},
}

impl From<Error> for io::Error {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
//!
//!
#![deny(warnings)]
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg_attr(test, macro_use)]
Expand Down