Skip to content

Commit

Permalink
jxl-coding: IntegerConfig validation (#30)
Browse files Browse the repository at this point in the history
* jxl-coding: IntegerConfig validation

* Update lib.rs
  • Loading branch information
EugeneVert authored May 27, 2023
1 parent 4a723b3 commit 186fffb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions crates/jxl-coding/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
pub enum Error {
Bitstream(jxl_bitstream::Error),
Lz77NotAllowed,
InvalidPrefixHistogram,
InvalidAnsHistogram,
InvalidAnsStream,
InvalidIntegerConfig,
InvalidPermutation,
InvalidPrefixHistogram,
}

impl std::error::Error for Error {
Expand All @@ -23,10 +24,11 @@ impl std::fmt::Display for Error {
match self {
Self::Bitstream(err) => write!(f, "error from bitstream: {}", err),
Self::Lz77NotAllowed => write!(f, "LZ77-enabled decoder when it is not allowed"),
Self::InvalidPrefixHistogram => write!(f, "invalid Brotli prefix code"),
Self::InvalidAnsHistogram => write!(f, "invalid ANS distribution"),
Self::InvalidAnsStream => write!(f, "ANS stream verification failed"),
Self::InvalidIntegerConfig => write!(f, "invalid hybrid integer configuration"),
Self::InvalidPermutation => write!(f, "invalid permutation"),
Self::InvalidPrefixHistogram => write!(f, "invalid Brotli prefix code"),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/jxl-coding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,18 @@ impl IntegerConfig {
let (msb_in_token, lsb_in_token) = if split_exponent != log_alphabet_size {
let msb_bits = add_log2_ceil(split_exponent);
let msb_in_token = bitstream.read_bits(msb_bits)?;
if msb_in_token > split_exponent {
return Err(Error::InvalidIntegerConfig);
}
let lsb_bits = add_log2_ceil(split_exponent - msb_in_token);
let lsb_in_token = bitstream.read_bits(lsb_bits)?;
(msb_in_token, lsb_in_token)
} else {
(0u32, 0u32)
};
if lsb_in_token + msb_in_token > split_exponent {
return Err(Error::InvalidIntegerConfig)
}
Ok(Self {
split_exponent,
split: 1 << split_exponent,
Expand Down

0 comments on commit 186fffb

Please sign in to comment.