Skip to content

Commit

Permalink
no need to check that the dict still matches at the start of each dec…
Browse files Browse the repository at this point in the history
…ode call
  • Loading branch information
KillingSpark committed Mar 31, 2023
1 parent d73f5e6 commit 343d69b
Showing 1 changed file with 18 additions and 33 deletions.
51 changes: 18 additions & 33 deletions src/frame_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ pub enum FrameDecoderError {
FailedToDrainDecodebuffer(#[source] Error),
#[error("Target must have at least as many bytes as the contentsize of the frame reports")]
TargetTooSmall,
#[error("Frame header specified dictionary id that wasnt provided by add_dict() or reset_with_dict()")]
DictNotProvided,
#[error("Frame header specified dictionary id 0x{dict_id:X} that wasnt provided by add_dict() or reset_with_dict()")]
DictNotProvided { dict_id: u32 },
}

const MAX_WINDOW_SIZE: u64 = 1024 * 1024 * 100;
Expand Down Expand Up @@ -181,13 +181,26 @@ impl FrameDecoder {
///
/// equivalent to init()
pub fn reset(&mut self, source: impl Read) -> Result<(), FrameDecoderError> {
match &mut self.state {
Some(s) => s.reset(source),
use FrameDecoderError as err;
let state = match &mut self.state {
Some(s) => {
s.reset(source)?;
s
}
None => {
self.state = Some(FrameDecoderState::new(source)?);
Ok(())
self.state.as_mut().unwrap()
}
};
if let Some(dict_id) = state.frame.header.dictionary_id() {
let dict = self
.dicts
.get(&dict_id)
.ok_or(err::DictNotProvided { dict_id })?;
state.decoder_scratch.init_from_dict(dict);
state.using_dict = Some(dict_id);
}
Ok(())
}

/// Add a dict to the FrameDecoder that can be used when needed. The FrameDecoder uses the appropriate one dynamically
Expand Down Expand Up @@ -271,20 +284,6 @@ impl FrameDecoder {
use FrameDecoderError as err;
let state = self.state.as_mut().ok_or(err::NotYetInitialized)?;

if let Some(id) = state.frame.header.dictionary_id() {
match state.using_dict {
Some(using_id) => {
//happy
debug_assert!(id == using_id);
}
None => {
let dict = self.dicts.get(&id).ok_or(err::DictNotProvided)?;
state.decoder_scratch.init_from_dict(dict);
state.using_dict = Some(id);
}
}
}

let mut block_dec = decoding::block_decoder::new();

let buffer_size_before = state.decoder_scratch.buffer.len();
Expand Down Expand Up @@ -447,20 +446,6 @@ impl FrameDecoder {
return Ok((4, 0));
}

if let Some(id) = state.frame.header.dictionary_id() {
match state.using_dict {
Some(using_id) => {
//happy
debug_assert!(id == using_id);
}
None => {
let dict = self.dicts.get(&id).ok_or(err::DictNotProvided)?;
state.decoder_scratch.init_from_dict(dict);
state.using_dict = Some(id);
}
}
}

loop {
//check if there are enough bytes for the next header
if mt_source.len() < 3 {
Expand Down

0 comments on commit 343d69b

Please sign in to comment.