Skip to content

Commit

Permalink
replace anyhow error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mcroomp committed Dec 3, 2024
1 parent 425cebb commit 51083f5
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 234 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ exclude = [
debug=true

[dependencies]
anyhow = { version="1.0", features = ["backtrace"]}
byteorder = "1.4"
cabac = "0.6.0"
default-boxed = "0.2"
Expand Down
15 changes: 8 additions & 7 deletions src/complevel_estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
depth_estimator::{new_depth_estimator, HashTableDepthEstimator},
hash_algorithm::HashAlgorithm,
preflate_constants,
preflate_error::{err_exit_code, ExitCode, Result},
preflate_input::PreflateInput,
preflate_parse_config::{
MatchingType, SLOW_PREFLATE_PARSER_SETTINGS, ZLIB_PREFLATE_PARSER_SETTINGS,
Expand Down Expand Up @@ -240,9 +241,9 @@ impl<'a> CompLevelEstimatorState<'a> {
}
}

fn recommend(&mut self) -> anyhow::Result<CompLevelInfo> {
fn recommend(&mut self) -> Result<CompLevelInfo> {
if self.candidates.is_empty() {
return Err(anyhow::anyhow!("no candidates found"));
return err_exit_code(ExitCode::PredictBlock, "no candidates found");
}

let candidate = self
Expand Down Expand Up @@ -284,10 +285,10 @@ impl<'a> CompLevelEstimatorState<'a> {
}

if candidate.max_chain_found() >= 4096 {
return Err(anyhow::anyhow!(
"max_chain_found too large: {}",
candidate.max_chain_found()
));
return err_exit_code(
ExitCode::PredictBlock,
format!("max_chain_found too large: {}", candidate.max_chain_found()).as_str(),
);
}

let very_far_matches = longest_dist_at_hop_0
Expand Down Expand Up @@ -325,7 +326,7 @@ pub fn estimate_preflate_comp_level(
plain_text: &[u8],
add_policy: DictionaryAddPolicy,
blocks: &Vec<PreflateTokenBlock>,
) -> anyhow::Result<CompLevelInfo> {
) -> Result<CompLevelInfo> {
let mut state =
CompLevelEstimatorState::new(wbits, mem_level, plain_text, add_policy, min_len, blocks);
state.check_dump();
Expand Down
21 changes: 10 additions & 11 deletions src/deflate_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This software incorporates material from third parties. See NOTICE.txt for details.
*--------------------------------------------------------------------------------------------*/

use anyhow::Context;
use crate::preflate_error::{err_exit_code, AddContext, ExitCode, Result};

use std::io::Read;

Expand Down Expand Up @@ -41,7 +41,7 @@ impl<R: Read> DeflateReader<R> {
std::mem::take(&mut self.plain_text)
}

fn read_bit(&mut self) -> anyhow::Result<bool> {
fn read_bit(&mut self) -> Result<bool> {
Ok(self.input.get(1)? != 0)
}

Expand All @@ -61,7 +61,7 @@ impl<R: Read> DeflateReader<R> {
}
}

pub fn read_block(&mut self, last: &mut bool) -> anyhow::Result<PreflateTokenBlock> {
pub fn read_block(&mut self, last: &mut bool) -> Result<PreflateTokenBlock> {
let mut blk;

*last = self.read_bit()?;
Expand All @@ -77,7 +77,7 @@ impl<R: Read> DeflateReader<R> {
let len = self.read_bits(16)?;
let ilen = self.read_bits(16)?;
if (len ^ ilen) != 0xffff {
return Err(anyhow::Error::msg("Blocllength mismatch"));
return err_exit_code(ExitCode::AnalyzeFailed, "Block length mismatch");
}
blk.context_len = 0;

Expand Down Expand Up @@ -105,20 +105,19 @@ impl<R: Read> DeflateReader<R> {

let decoder = HuffmanReader::create_from_original_encoding(&blk.huffman_encoding)?;

self.decode_block(&decoder, &mut blk)
.with_context(|| "decode_block dyn")?;
self.decode_block(&decoder, &mut blk).context()?;
Ok(blk)
}

_ => Err(anyhow::Error::msg("Invalid block type")),
_ => err_exit_code(ExitCode::InvalidDeflate, "Invalid block type"),
}
}

fn decode_block(
&mut self,
decoder: &HuffmanReader,
blk: &mut PreflateTokenBlock,
) -> anyhow::Result<()> {
) -> Result<()> {
let mut earliest_reference = i32::MAX;
let mut cur_pos = 0;

Expand All @@ -134,7 +133,7 @@ impl<R: Read> DeflateReader<R> {
} else {
let lcode: u32 = lit_len - preflate_constants::NONLEN_CODE_COUNT as u32;
if lcode >= preflate_constants::LEN_CODE_COUNT as u32 {
return Err(anyhow::Error::msg("Invalid length code"));
return err_exit_code(ExitCode::InvalidDeflate, "Invalid length code");
}
let len: u32 = preflate_constants::MIN_MATCH
+ preflate_constants::LENGTH_BASE_TABLE[lcode as usize] as u32
Expand All @@ -147,14 +146,14 @@ impl<R: Read> DeflateReader<R> {

let dcode = decoder.fetch_next_distance_char(&mut self.input)? as u32;
if dcode >= preflate_constants::DIST_CODE_COUNT as u32 {
return Err(anyhow::Error::msg("Invalid distance code"));
return err_exit_code(ExitCode::InvalidDeflate, "Invalid distance code");
}
let dist = 1
+ preflate_constants::DIST_BASE_TABLE[dcode as usize] as u32
+ self
.read_bits(preflate_constants::DIST_EXTRA_TABLE[dcode as usize].into())?;
if dist as usize > self.plain_text.len() {
return Err(anyhow::Error::msg("Invalid distance"));
return err_exit_code(ExitCode::InvalidDeflate, "Invalid distance");
}
self.write_reference(dist, len);
blk.add_reference(len, dist, irregular_258);
Expand Down
2 changes: 1 addition & 1 deletion src/deflate_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This software incorporates material from third parties. See NOTICE.txt for details.
*--------------------------------------------------------------------------------------------*/

use anyhow::Result;
use crate::preflate_error::Result;

use crate::{
bit_writer::BitWriter,
Expand Down
2 changes: 1 addition & 1 deletion src/depth_estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub trait HashTableDepthEstimator {
}

#[derive(DefaultBoxed)]
pub struct HashTableDepthEstimatorImpl<H: HashImplementation> {
struct HashTableDepthEstimatorImpl<H: HashImplementation> {
/// Represents the head of the hash chain for a given hash value.
head: [u16; 65536],

Expand Down
22 changes: 12 additions & 10 deletions src/hash_chain_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use crate::hash_algorithm::{
};
use crate::hash_chain::{HashChain, MAX_UPDATE_HASH_BATCH};
use crate::preflate_constants::{MAX_MATCH, MIN_LOOKAHEAD, MIN_MATCH};
use crate::preflate_error::{err_exit_code, ExitCode, Result};
use crate::preflate_input::PreflateInput;
use crate::preflate_parameter_estimator::PreflateStrategy;
use crate::preflate_token::PreflateTokenReference;
use crate::token_predictor::TokenPredictorParameters;

use std::cmp;

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -85,11 +87,11 @@ pub trait HashChainHolder {
&self,
target_reference: &PreflateTokenReference,
input: &PreflateInput,
) -> anyhow::Result<u32>;
) -> Result<u32>;

/// Does the inverse of calculate_hops, where we start from the predicted token and
/// get the new distance based on the number of hops
fn hop_match(&self, len: u32, hops: u32, input: &PreflateInput) -> anyhow::Result<u32>;
fn hop_match(&self, len: u32, hops: u32, input: &PreflateInput) -> Result<u32>;

/// debugging function to verify that the hash chain is correct
fn verify_hash(&self, _dist: Option<PreflateTokenReference>);
Expand Down Expand Up @@ -124,11 +126,11 @@ impl HashChainHolder for () {
&self,
_target_reference: &PreflateTokenReference,
_input: &PreflateInput,
) -> anyhow::Result<u32> {
) -> Result<u32> {
unimplemented!()
}

fn hop_match(&self, _len: u32, _hops: u32, _input: &PreflateInput) -> anyhow::Result<u32> {
fn hop_match(&self, _len: u32, _hops: u32, _input: &PreflateInput) -> Result<u32> {
unimplemented!()
}

Expand Down Expand Up @@ -171,11 +173,11 @@ impl<H: HashImplementation> HashChainHolder for HashChainHolderImpl<H> {
&self,
target_reference: &PreflateTokenReference,
input: &PreflateInput,
) -> anyhow::Result<u32> {
) -> Result<u32> {
let max_len = std::cmp::min(input.remaining(), MAX_MATCH);

if max_len < target_reference.len() {
return Err(anyhow::anyhow!("max_len < target_reference.len()"));
return err_exit_code(ExitCode::PredictBlock, "max_len < target_reference.len()");
}

let max_chain_org = 0xffff; // max hash chain length
Expand Down Expand Up @@ -213,15 +215,15 @@ impl<H: HashImplementation> HashChainHolder for HashChainHolderImpl<H> {
max_chain -= 1;
}

Err(anyhow::anyhow!("no match found"))
err_exit_code(ExitCode::MatchNotFound, "no match found")
}

/// Does the inverse of calculate_hops, where we start from the predicted token and
/// get the new distance based on the number of hops
fn hop_match(&self, len: u32, hops: u32, input: &PreflateInput) -> anyhow::Result<u32> {
fn hop_match(&self, len: u32, hops: u32, input: &PreflateInput) -> Result<u32> {
let max_len = std::cmp::min(input.remaining(), MAX_MATCH);
if max_len < len {
return Err(anyhow::anyhow!("not enough data left to match"));
return err_exit_code(ExitCode::RecompressFailed, "not enough data left to match");
}

let cur_max_dist = std::cmp::min(input.pos(), self.window_bytes);
Expand All @@ -247,7 +249,7 @@ impl<H: HashImplementation> HashChainHolder for HashChainHolderImpl<H> {
}
}

Err(anyhow::anyhow!("no match found"))
err_exit_code(ExitCode::MatchNotFound, "no match found")
}

/// debugging function to verify that the hash chain is correct
Expand Down
25 changes: 12 additions & 13 deletions src/huffman_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This software incorporates material from third parties. See NOTICE.txt for details.
*--------------------------------------------------------------------------------------------*/

use anyhow::Result;
use crate::preflate_error::{err_exit_code, ExitCode, Result};

use crate::{
bit_reader::ReadBits,
Expand Down Expand Up @@ -48,7 +48,7 @@ impl HuffmanOriginalEncoding {
/// Reads a dynamic huffman table from the bit reader. The structure
/// holds all the information necessary to recode the huffman table
/// exactly as it was written.
pub fn read<R: ReadBits>(bit_reader: &mut R) -> anyhow::Result<HuffmanOriginalEncoding> {
pub fn read<R: ReadBits>(bit_reader: &mut R) -> Result<HuffmanOriginalEncoding> {
// 5 Bits: HLIT, # of Literal/Length codes - 257 (257 - 286)
let hlit = bit_reader.get(5)? as usize + 257;
// 5 Bits: HDIST, # of Distance codes - 1 (1 - 32)
Expand Down Expand Up @@ -92,7 +92,9 @@ impl HuffmanOriginalEncoding {
16 => TreeCodeType::Repeat,
17 => TreeCodeType::ZeroShort,
18 => TreeCodeType::ZeroLong,
_ => return Err(anyhow::Error::msg("Invalid code length")),
_ => {
return err_exit_code(ExitCode::InvalidDeflate, "Invalid code length");
}
};

let (sub, bits) = Self::get_tree_code_adjustment(tree_code);
Expand All @@ -105,9 +107,10 @@ impl HuffmanOriginalEncoding {
}

if codes_read != c_lengths_combined {
return Err(anyhow::Error::msg(
return err_exit_code(
ExitCode::InvalidDeflate,
"Code table should be same size as hdist + hlit",
));
);
}

Ok(HuffmanOriginalEncoding {
Expand All @@ -120,11 +123,7 @@ impl HuffmanOriginalEncoding {
}

/// writes dynamic huffman table to the output buffer using the bitwriter
pub fn write(
&self,
bitwriter: &mut BitWriter,
output_buffer: &mut Vec<u8>,
) -> anyhow::Result<()> {
pub fn write(&self, bitwriter: &mut BitWriter, output_buffer: &mut Vec<u8>) -> Result<()> {
bitwriter.write(self.num_literals as u32 - 257, 5, output_buffer);
bitwriter.write(self.num_dist as u32 - 1, 5, output_buffer);
bitwriter.write(self.num_code_lengths as u32 - 4, 4, output_buffer);
Expand Down Expand Up @@ -265,7 +264,7 @@ impl HuffmanReader {
/// clarity. Literal/length values 286-287 will never actually
/// occur in the compressed data, but participate in the code
/// construction.
pub fn create_fixed() -> anyhow::Result<Self> {
pub fn create_fixed() -> Result<Self> {
let (lit_lengths, dist_lengths) = HuffmanOriginalEncoding::get_fixed_distance_lengths();

Ok(HuffmanReader {
Expand All @@ -286,11 +285,11 @@ impl HuffmanReader {
})
}

pub fn fetch_next_literal_code<R: ReadBits>(&self, bit_reader: &mut R) -> anyhow::Result<u16> {
pub fn fetch_next_literal_code<R: ReadBits>(&self, bit_reader: &mut R) -> Result<u16> {
decode_symbol(bit_reader, &self.lit_huff_code_tree)
}

pub fn fetch_next_distance_char<R: ReadBits>(&self, bit_reader: &mut R) -> anyhow::Result<u16> {
pub fn fetch_next_distance_char<R: ReadBits>(&self, bit_reader: &mut R) -> Result<u16> {
decode_symbol(bit_reader, &self.dist_huff_code_tree)
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/huffman_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*--------------------------------------------------------------------------------------------*/

use crate::bit_reader::ReadBits;
use crate::preflate_error::{err_exit_code, ExitCode, Result};
use std::vec;

/// Calculates Huffman code array given an array of Huffman Code Lengths using the RFC 1951 algorithm
pub fn calc_huffman_codes(code_lengths: &[u8]) -> anyhow::Result<Vec<u16>> {
pub fn calc_huffman_codes(code_lengths: &[u8]) -> Result<Vec<u16>> {
let mut result: Vec<u16> = vec![0; code_lengths.len()];

// The following algorithm generates the codes as integers, intended to be read
Expand Down Expand Up @@ -100,9 +101,9 @@ fn is_valid_huffman_code_lengths(code_lengths: &[u8]) -> bool {
/// rg_huff_nodes[N+1] is the array index of the '1' child
/// 2. If rg_huff_nodes[i] is less than zero then it is a leaf and the literal alphabet value is -rg_huff_nodes[i] + 1
/// 3. The root node index 'N' is rg_huff_nodes.len() - 2. Search should start at that node.
pub fn calculate_huffman_code_tree(code_lengths: &[u8]) -> anyhow::Result<Vec<i32>> {
pub fn calculate_huffman_code_tree(code_lengths: &[u8]) -> Result<Vec<i32>> {
if !is_valid_huffman_code_lengths(code_lengths) {
return Err(anyhow::anyhow!("Invalid Huffman code lengths"));
return err_exit_code(ExitCode::InvalidDeflate, "Invalid Huffman code lengths");
}

let mut c_codes: i32 = 0;
Expand Down Expand Up @@ -152,7 +153,7 @@ pub fn calculate_huffman_code_tree(code_lengths: &[u8]) -> anyhow::Result<Vec<i3
/// Huffman Nodes are encoded in the array of ints as follows:
/// '0' child link of node 'N' is at huffman_tree[N], '1' child link is at huffman_tree[N + 1]
/// Root of tree is at huffman_tree.len() - 2
pub fn decode_symbol<R: ReadBits>(bit_reader: &mut R, huffman_tree: &[i32]) -> anyhow::Result<u16> {
pub fn decode_symbol<R: ReadBits>(bit_reader: &mut R, huffman_tree: &[i32]) -> Result<u16> {
let mut i_node_cur: i32 = huffman_tree.len() as i32 - 2; // Start at the root of the Huffman tree

loop {
Expand Down
Loading

0 comments on commit 51083f5

Please sign in to comment.