use std::time::{Duration, Instant};
use crate::{
block::{BlockHeader, BlockType},
decode_out::DecodeOut,
dictionary::{DecoderDictionary, Dictionary},
error::{Error, Result},
frame::{
Format, FrameHeader, SkippableFrame, ZstandardFrameHeader, parse_frame_header_with_format,
},
literals::LiteralsState,
sequence::{
OutputLimit, RepeatOffsets, SequenceCommand, SequenceTablesState, TableTarget,
decode_and_execute_sequences, decode_sequence_commands_into_stats,
execute_sequences_with_total_match_bytes_profiled, parse_sequence_section,
},
xxhash::xxh64,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecoderOptions {
pub max_window_size: Option<u64>,
pub max_output_size: Option<usize>,
pub verify_checksum: bool,
pub single_frame: bool,
pub format: Format,
}
impl DecoderOptions {
pub const DEFAULT_MAX_WINDOW_SIZE: u64 = 1 << 27;
}
impl Default for DecoderOptions {
fn default() -> Self {
Self {
max_window_size: Some(Self::DEFAULT_MAX_WINDOW_SIZE),
max_output_size: None,
verify_checksum: true,
single_frame: false,
format: Format::Zstd1,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[doc(hidden)]
pub struct DecodeStageProfile {
pub total: Duration,
pub literals: Duration,
pub sequence_tables: Duration,
pub sequence_commands: Duration,
pub sequence_execute: Duration,
pub sequence_execute_literal_copy: Duration,
pub sequence_execute_prefix_match_copy: Duration,
pub sequence_execute_dictionary_match_copy: Duration,
pub blocks: usize,
pub compressed_blocks: usize,
pub raw_blocks: usize,
pub rle_blocks: usize,
pub output_bytes: usize,
}
#[derive(Default)]
struct DecodeStageProfiler {
profile: DecodeStageProfile,
}
impl DecodeStageProfiler {
fn finish(self) -> DecodeStageProfile {
self.profile
}
}
pub fn decode_all(src: &[u8]) -> Result<Vec<u8>> {
decode_all_with_options(src, DecoderOptions::default())
}
pub fn decode_all_with_options(src: &[u8], options: DecoderOptions) -> Result<Vec<u8>> {
decode_all_with_scratch(src, options, None, &mut DecodeScratch::default())
}
pub fn decode_all_with_prepared_dict(src: &[u8], dict: &DecoderDictionary<'_>) -> Result<Vec<u8>> {
decode_all_with_prepared_dict_and_options(src, dict, DecoderOptions::default())
}
pub fn decode_all_with_prepared_dict_and_options(
src: &[u8],
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<Vec<u8>> {
decode_all_with_scratch(
src,
options,
Some(dict.as_inner()),
&mut DecodeScratch::default(),
)
}
pub fn decode_all_with_dict(src: &[u8], dict: &[u8]) -> Result<Vec<u8>> {
decode_all_with_dict_and_options(src, dict, DecoderOptions::default())
}
pub fn decode_all_with_dict_and_options(
src: &[u8],
dict: &[u8],
options: DecoderOptions,
) -> Result<Vec<u8>> {
let dictionary = DecoderDictionary::new(dict)?;
decode_all_with_prepared_dict_and_options(src, &dictionary, options)
}
pub fn decode_into_slice(src: &[u8], dst: &mut [u8]) -> Result<usize> {
decode_into_slice_with_options(src, dst, DecoderOptions::default())
}
pub fn decode_into_slice_with_options(
src: &[u8],
dst: &mut [u8],
options: DecoderOptions,
) -> Result<usize> {
decode_into_slice_with_scratch(src, options, None, &mut DecodeScratch::default(), dst)
}
#[cfg(any(feature = "internal-trace", test))]
#[cfg_attr(not(feature = "internal-trace"), allow(dead_code))]
#[doc(hidden)]
pub fn profile_first_block_decode_with_options(
src: &[u8],
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_first_block_decode_with_scratch(src, options, None, &mut DecodeScratch::default())
}
#[cfg(any(feature = "internal-trace", test))]
#[cfg_attr(not(feature = "internal-trace"), allow(dead_code))]
#[doc(hidden)]
pub fn profile_frame_decode_with_options(
src: &[u8],
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_frame_decode_with_scratch(src, options, None, &mut DecodeScratch::default())
}
#[cfg(any(feature = "internal-trace", test))]
#[cfg_attr(not(feature = "internal-trace"), allow(dead_code))]
#[doc(hidden)]
pub fn profile_frame_decode_with_prepared_dict_and_options(
src: &[u8],
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_frame_decode_with_scratch(
src,
options,
Some(dict.as_inner()),
&mut DecodeScratch::default(),
)
}
#[cfg(any(feature = "internal-trace", test))]
#[cfg_attr(not(feature = "internal-trace"), allow(dead_code))]
#[doc(hidden)]
pub fn profile_first_block_decode_with_prepared_dict_and_options(
src: &[u8],
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_first_block_decode_with_scratch(
src,
options,
Some(dict.as_inner()),
&mut DecodeScratch::default(),
)
}
#[derive(Default)]
struct DecodeScratch {
literals: Vec<u8>,
sequences: Vec<SequenceCommand>,
}
pub struct Decoder {
scratch: DecodeScratch,
}
impl Decoder {
pub fn new() -> Self {
Self {
scratch: DecodeScratch::default(),
}
}
pub fn decode_all(&mut self, src: &[u8]) -> Result<Vec<u8>> {
self.decode_all_with_options(src, DecoderOptions::default())
}
pub fn decode_all_with_options(
&mut self,
src: &[u8],
options: DecoderOptions,
) -> Result<Vec<u8>> {
decode_all_with_scratch(src, options, None, &mut self.scratch)
}
pub fn decode_all_with_prepared_dict(
&mut self,
src: &[u8],
dict: &DecoderDictionary<'_>,
) -> Result<Vec<u8>> {
self.decode_all_with_prepared_dict_and_options(src, dict, DecoderOptions::default())
}
pub fn decode_all_with_prepared_dict_and_options(
&mut self,
src: &[u8],
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<Vec<u8>> {
decode_all_with_scratch(src, options, Some(dict.as_inner()), &mut self.scratch)
}
pub fn decode_all_with_dict(&mut self, src: &[u8], dict: &[u8]) -> Result<Vec<u8>> {
self.decode_all_with_dict_and_options(src, dict, DecoderOptions::default())
}
pub fn decode_all_with_dict_and_options(
&mut self,
src: &[u8],
dict: &[u8],
options: DecoderOptions,
) -> Result<Vec<u8>> {
let dictionary = DecoderDictionary::new(dict)?;
self.decode_all_with_prepared_dict_and_options(src, &dictionary, options)
}
pub fn decode_all_into(&mut self, src: &[u8], dst: &mut Vec<u8>) -> Result<()> {
self.decode_all_into_with_options(src, dst, DecoderOptions::default())
}
pub fn decode_all_into_with_options(
&mut self,
src: &[u8],
dst: &mut Vec<u8>,
options: DecoderOptions,
) -> Result<()> {
decode_into_with_scratch(
src,
options,
None,
&mut self.scratch,
&mut DecodeOut::growable(dst),
None,
)
}
pub fn decode_all_into_with_prepared_dict(
&mut self,
src: &[u8],
dst: &mut Vec<u8>,
dict: &DecoderDictionary<'_>,
) -> Result<()> {
self.decode_all_into_with_prepared_dict_and_options(
src,
dst,
dict,
DecoderOptions::default(),
)
}
pub fn decode_all_into_with_prepared_dict_and_options(
&mut self,
src: &[u8],
dst: &mut Vec<u8>,
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<()> {
decode_into_with_scratch(
src,
options,
Some(dict.as_inner()),
&mut self.scratch,
&mut DecodeOut::growable(dst),
None,
)
}
pub fn decode_into_slice(&mut self, src: &[u8], dst: &mut [u8]) -> Result<usize> {
self.decode_into_slice_with_options(src, dst, DecoderOptions::default())
}
pub fn decode_into_slice_with_options(
&mut self,
src: &[u8],
dst: &mut [u8],
options: DecoderOptions,
) -> Result<usize> {
decode_into_slice_with_scratch(src, options, None, &mut self.scratch, dst)
}
pub fn decode_into_slice_with_prepared_dict(
&mut self,
src: &[u8],
dst: &mut [u8],
dict: &DecoderDictionary<'_>,
) -> Result<usize> {
self.decode_into_slice_with_prepared_dict_and_options(
src,
dst,
dict,
DecoderOptions::default(),
)
}
pub fn decode_into_slice_with_prepared_dict_and_options(
&mut self,
src: &[u8],
dst: &mut [u8],
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<usize> {
decode_into_slice_with_scratch(src, options, Some(dict.as_inner()), &mut self.scratch, dst)
}
#[doc(hidden)]
pub fn profile_first_block_decode_with_options(
&mut self,
src: &[u8],
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_first_block_decode_with_scratch(src, options, None, &mut self.scratch)
}
#[doc(hidden)]
pub fn profile_first_block_decode_with_prepared_dict_and_options(
&mut self,
src: &[u8],
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_first_block_decode_with_scratch(
src,
options,
Some(dict.as_inner()),
&mut self.scratch,
)
}
#[doc(hidden)]
pub fn profile_frame_decode_with_options(
&mut self,
src: &[u8],
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_frame_decode_with_scratch(src, options, None, &mut self.scratch)
}
#[doc(hidden)]
pub fn profile_frame_decode_with_prepared_dict_and_options(
&mut self,
src: &[u8],
dict: &DecoderDictionary<'_>,
options: DecoderOptions,
) -> Result<DecodeStageProfile> {
profile_frame_decode_with_scratch(src, options, Some(dict.as_inner()), &mut self.scratch)
}
}
impl Default for Decoder {
fn default() -> Self {
Self::new()
}
}
fn profile_first_block_decode_with_scratch(
src: &[u8],
options: DecoderOptions,
dictionary: Option<&Dictionary<'_>>,
scratch: &mut DecodeScratch,
) -> Result<DecodeStageProfile> {
let total_start = Instant::now();
if src.is_empty() {
return Err(Error::UnexpectedEof);
}
let mut cursor = Cursor::new(src);
while !cursor.is_empty() {
match parse_frame_header_with_format(cursor.remaining_slice(), options.format)? {
FrameHeader::Skippable(skippable) => skip_skippable_frame(&mut cursor, skippable)?,
FrameHeader::Zstandard(header) => {
let mut profiler = DecodeStageProfiler::default();
profile_first_zstandard_block(
&mut cursor,
header,
options,
dictionary,
&mut scratch.literals,
&mut scratch.sequences,
&mut profiler,
)?;
let mut profile = profiler.finish();
profile.total = total_start.elapsed();
return Ok(profile);
}
}
}
Err(Error::UnexpectedEof)
}
fn profile_frame_decode_with_scratch(
src: &[u8],
options: DecoderOptions,
dictionary: Option<&Dictionary<'_>>,
scratch: &mut DecodeScratch,
) -> Result<DecodeStageProfile> {
let mut profiler = DecodeStageProfiler::default();
let mut out = Vec::new();
let total_start = Instant::now();
decode_into_with_scratch(
src,
options,
dictionary,
scratch,
&mut DecodeOut::growable(&mut out),
Some(&mut profiler),
)?;
let mut profile = profiler.finish();
profile.total = total_start.elapsed();
profile.output_bytes = out.len();
Ok(profile)
}
fn decode_into_slice_with_scratch(
src: &[u8],
options: DecoderOptions,
dictionary: Option<&Dictionary<'_>>,
scratch: &mut DecodeScratch,
dst: &mut [u8],
) -> Result<usize> {
let mut out = DecodeOut::fixed(dst);
decode_into_with_scratch(src, options, dictionary, scratch, &mut out, None)?;
Ok(out.len())
}
fn decode_all_with_scratch(
src: &[u8],
options: DecoderOptions,
dictionary: Option<&Dictionary<'_>>,
scratch: &mut DecodeScratch,
) -> Result<Vec<u8>> {
let mut out = Vec::new();
decode_into_with_scratch(
src,
options,
dictionary,
scratch,
&mut DecodeOut::growable(&mut out),
None,
)?;
Ok(out)
}
fn decode_into_with_scratch(
src: &[u8],
options: DecoderOptions,
dictionary: Option<&Dictionary<'_>>,
scratch: &mut DecodeScratch,
out: &mut DecodeOut<'_>,
mut profiler: Option<&mut DecodeStageProfiler>,
) -> Result<()> {
out.clear();
if src.is_empty() {
return Err(Error::UnexpectedEof);
}
let mut cursor = Cursor::new(src);
while !cursor.is_empty() {
match parse_frame_header_with_format(cursor.remaining_slice(), options.format)? {
FrameHeader::Skippable(skippable) => {
if options.single_frame {
return Err(Error::TrailingInput {
offset: cursor.position(),
});
}
skip_skippable_frame(&mut cursor, skippable)?
}
FrameHeader::Zstandard(header) => {
decode_zstandard_frame(
&mut cursor,
header,
options,
dictionary,
&mut scratch.literals,
&mut scratch.sequences,
out,
profiler.as_deref_mut(),
)?;
if options.single_frame && !cursor.is_empty() {
return Err(Error::TrailingInput {
offset: cursor.position(),
});
}
}
}
}
Ok(())
}
fn skip_skippable_frame(cursor: &mut Cursor<'_>, frame: SkippableFrame) -> Result<()> {
let total_size = frame
.header_size
.checked_add(frame.size as usize)
.ok_or(Error::OutputSizeOverflow)?;
cursor.read_exact(total_size)?;
Ok(())
}
fn decode_zstandard_frame(
cursor: &mut Cursor<'_>,
header: ZstandardFrameHeader,
options: DecoderOptions,
dictionary: Option<&Dictionary<'_>>,
literals_scratch: &mut Vec<u8>,
sequences_scratch: &mut Vec<SequenceCommand>,
out: &mut DecodeOut<'_>,
mut profiler: Option<&mut DecodeStageProfiler>,
) -> Result<()> {
cursor.read_exact(header.header_size)?;
let dictionary = match (dictionary, header.dictionary_id) {
(None, Some(dictionary_id)) => {
return Err(Error::DictionaryRequired(Some(dictionary_id)));
}
(Some(dictionary), Some(dictionary_id)) if dictionary.id() != dictionary_id => {
return Err(Error::DictionaryMismatch {
expected: dictionary_id,
actual: dictionary.id(),
});
}
_ => dictionary,
};
if let Some(max_window_size) = options.max_window_size {
if header.window_size > max_window_size {
return Err(Error::WindowSizeTooLarge {
window_size: header.window_size,
max_window_size,
});
}
}
let frame_start = out.len();
let mut literals_state =
dictionary.map_or_else(LiteralsState::default, Dictionary::literals_state);
let mut sequence_tables =
dictionary.map_or_else(SequenceTablesState::default, Dictionary::sequence_tables);
let mut repeat_offsets =
dictionary.map_or_else(RepeatOffsets::default, Dictionary::repeat_offsets);
if let Some(content_size) = header.content_size {
ensure_total_size_limit(out.len(), content_size, options.max_output_size)?;
let reserve =
usize::try_from(content_size).map_err(|_| Error::ContentSizeTooLarge(content_size))?;
let expansion_bound = (cursor.remaining_slice().len() / (BlockHeader::SIZE + 1))
.saturating_mul(header.block_size_max as usize);
out.try_reserve(reserve.min(expansion_bound).saturating_add(32))?;
}
loop {
let header_bytes = cursor.read_exact(BlockHeader::SIZE)?;
let block_header = BlockHeader::parse(header_bytes)?;
if let Some(profiler) = profiler.as_deref_mut() {
profiler.profile.blocks += 1;
}
match block_header.block_type {
BlockType::Raw => {
if let Some(profiler) = profiler.as_deref_mut() {
profiler.profile.raw_blocks += 1;
}
if block_header.block_size > header.block_size_max {
return Err(Error::Corruption(
"block size exceeds frame block size limit",
));
}
let data = cursor.read_exact(block_header.payload_size())?;
append_bytes(
out,
data,
OutputLimit::whole_output(options.max_output_size),
)?;
}
BlockType::Rle => {
if let Some(profiler) = profiler.as_deref_mut() {
profiler.profile.rle_blocks += 1;
}
if block_header.block_size > header.block_size_max {
return Err(Error::Corruption(
"block size exceeds frame block size limit",
));
}
let byte = cursor.read_u8()?;
append_repeated(
out,
byte,
block_header.block_size as usize,
OutputLimit::whole_output(options.max_output_size),
)?;
}
BlockType::Compressed => {
if let Some(profiler) = profiler.as_deref_mut() {
profiler.profile.compressed_blocks += 1;
}
let data = cursor.read_exact(block_header.payload_size())?;
decode_compressed_block_inner(
data,
header.block_size_max as usize,
frame_start,
usize::try_from(header.window_size).unwrap_or(usize::MAX),
dictionary.map(Dictionary::content),
&mut literals_state,
&mut sequence_tables,
&mut repeat_offsets,
literals_scratch,
sequences_scratch,
out,
options.max_output_size,
profiler.as_deref_mut(),
)?;
}
}
if block_header.last_block {
break;
}
}
if header.checksum {
let expected = cursor.read_u32_le()?;
if options.verify_checksum {
let actual = xxh64(&out.as_slice()[frame_start..], 0) as u32;
if actual != expected {
return Err(Error::ChecksumMismatch { expected, actual });
}
}
}
if let Some(expected_size) = header.content_size {
let actual_size = (out.len() - frame_start) as u64;
if actual_size != expected_size {
return Err(Error::ContentSizeMismatch {
expected: expected_size,
actual: actual_size,
});
}
}
Ok(())
}
fn profile_first_zstandard_block(
cursor: &mut Cursor<'_>,
header: ZstandardFrameHeader,
options: DecoderOptions,
dictionary: Option<&Dictionary<'_>>,
literals_scratch: &mut Vec<u8>,
sequences_scratch: &mut Vec<SequenceCommand>,
profiler: &mut DecodeStageProfiler,
) -> Result<()> {
cursor.read_exact(header.header_size)?;
let dictionary = match (dictionary, header.dictionary_id) {
(None, Some(dictionary_id)) => {
return Err(Error::DictionaryRequired(Some(dictionary_id)));
}
(Some(dictionary), Some(dictionary_id)) if dictionary.id() != dictionary_id => {
return Err(Error::DictionaryMismatch {
expected: dictionary_id,
actual: dictionary.id(),
});
}
_ => dictionary,
};
if let Some(max_window_size) = options.max_window_size {
if header.window_size > max_window_size {
return Err(Error::WindowSizeTooLarge {
window_size: header.window_size,
max_window_size,
});
}
}
let mut out = Vec::new();
let mut out = DecodeOut::growable(&mut out);
let mut literals_state =
dictionary.map_or_else(LiteralsState::default, Dictionary::literals_state);
let mut sequence_tables =
dictionary.map_or_else(SequenceTablesState::default, Dictionary::sequence_tables);
let mut repeat_offsets =
dictionary.map_or_else(RepeatOffsets::default, Dictionary::repeat_offsets);
if let Some(content_size) = header.content_size {
ensure_total_size_limit(out.len(), content_size, options.max_output_size)?;
let reserve =
usize::try_from(content_size).map_err(|_| Error::ContentSizeTooLarge(content_size))?;
out.try_reserve(
reserve
.min(header.block_size_max as usize)
.saturating_add(32),
)?;
}
let header_bytes = cursor.read_exact(BlockHeader::SIZE)?;
let block_header = BlockHeader::parse(header_bytes)?;
profiler.profile.blocks = 1;
match block_header.block_type {
BlockType::Raw => {
profiler.profile.raw_blocks = 1;
if block_header.block_size > header.block_size_max {
return Err(Error::Corruption(
"block size exceeds frame block size limit",
));
}
let data = cursor.read_exact(block_header.payload_size())?;
append_bytes(
&mut out,
data,
OutputLimit::whole_output(options.max_output_size),
)?;
}
BlockType::Rle => {
profiler.profile.rle_blocks = 1;
if block_header.block_size > header.block_size_max {
return Err(Error::Corruption(
"block size exceeds frame block size limit",
));
}
let byte = cursor.read_u8()?;
append_repeated(
&mut out,
byte,
block_header.block_size as usize,
OutputLimit::whole_output(options.max_output_size),
)?;
}
BlockType::Compressed => {
profiler.profile.compressed_blocks = 1;
let data = cursor.read_exact(block_header.payload_size())?;
decode_compressed_block_profiled(
data,
header.block_size_max as usize,
0,
usize::try_from(header.window_size).unwrap_or(usize::MAX),
dictionary.map(Dictionary::content),
&mut literals_state,
&mut sequence_tables,
&mut repeat_offsets,
literals_scratch,
sequences_scratch,
&mut out,
options.max_output_size,
profiler,
)?;
}
}
Ok(())
}
fn decode_compressed_block_profiled(
src: &[u8],
block_size_max: usize,
frame_start: usize,
window_size: usize,
dictionary: Option<&[u8]>,
literals_state: &mut LiteralsState,
sequence_tables: &mut SequenceTablesState,
repeat_offsets: &mut RepeatOffsets,
literals_scratch: &mut Vec<u8>,
sequences_scratch: &mut Vec<SequenceCommand>,
out: &mut DecodeOut<'_>,
max_output_size: Option<usize>,
profiler: &mut DecodeStageProfiler,
) -> Result<()> {
decode_compressed_block_inner(
src,
block_size_max,
frame_start,
window_size,
dictionary,
literals_state,
sequence_tables,
repeat_offsets,
literals_scratch,
sequences_scratch,
out,
max_output_size,
Some(profiler),
)
}
pub(crate) fn decode_compressed_block_into(
src: &[u8],
block_size_max: usize,
frame_start: usize,
window_size: usize,
dictionary: Option<&[u8]>,
literals_state: &mut LiteralsState,
sequence_tables: &mut SequenceTablesState,
repeat_offsets: &mut RepeatOffsets,
literals_scratch: &mut Vec<u8>,
out: &mut DecodeOut<'_>,
limit: OutputLimit,
) -> Result<()> {
let (literals_size, literals) = crate::literals::decode_literals_section_into(
src,
literals_state,
block_size_max,
literals_scratch,
)?;
if literals.len() > block_size_max {
return Err(Error::Corruption(
"compressed block literals exceed the frame block size limit",
));
}
let sequence_section = src.get(literals_size..).ok_or(Error::UnexpectedEof)?;
let parsed_sequences =
parse_sequence_section(sequence_section, sequence_tables, TableTarget::SequenceOnly)?;
if parsed_sequences.number_of_sequences == 0
&& parsed_sequences.header_size != sequence_section.len()
{
return Err(Error::Corruption(
"zero-sequence blocks must not contain additional sequence payload",
));
}
decode_and_execute_sequences(
&parsed_sequences,
sequence_tables,
out,
frame_start,
window_size,
block_size_max,
dictionary,
literals,
repeat_offsets,
limit,
)
}
fn decode_compressed_block_inner(
src: &[u8],
block_size_max: usize,
frame_start: usize,
window_size: usize,
dictionary: Option<&[u8]>,
literals_state: &mut LiteralsState,
sequence_tables: &mut SequenceTablesState,
repeat_offsets: &mut RepeatOffsets,
literals_scratch: &mut Vec<u8>,
sequences_scratch: &mut Vec<SequenceCommand>,
out: &mut DecodeOut<'_>,
max_output_size: Option<usize>,
profiler: Option<&mut DecodeStageProfiler>,
) -> Result<()> {
let Some(profiler) = profiler else {
return decode_compressed_block_into(
src,
block_size_max,
frame_start,
window_size,
dictionary,
literals_state,
sequence_tables,
repeat_offsets,
literals_scratch,
out,
OutputLimit::whole_output(max_output_size),
);
};
let literals_start = Instant::now();
let (literals_size, literals) = crate::literals::decode_literals_section_into(
src,
literals_state,
block_size_max,
literals_scratch,
)?;
if literals.len() > block_size_max {
return Err(Error::Corruption(
"compressed block literals exceed the frame block size limit",
));
}
profiler.profile.literals += literals_start.elapsed();
{
let sequence_start = Instant::now();
let sequence_section = src.get(literals_size..).ok_or(Error::UnexpectedEof)?;
let parsed_sequences =
parse_sequence_section(sequence_section, sequence_tables, TableTarget::Both)?;
profiler.profile.sequence_tables += sequence_start.elapsed();
if parsed_sequences.number_of_sequences == 0
&& parsed_sequences.header_size != sequence_section.len()
{
return Err(Error::Corruption(
"zero-sequence blocks must not contain additional sequence payload",
));
}
let command_start = Instant::now();
let decoded_stats = decode_sequence_commands_into_stats(
&parsed_sequences,
sequence_tables,
sequences_scratch,
)?;
profiler.profile.sequence_commands += command_start.elapsed();
let execute_start = Instant::now();
let result = execute_sequences_with_total_match_bytes_profiled(
out,
frame_start,
window_size,
block_size_max,
dictionary,
literals.as_slice(),
sequences_scratch,
repeat_offsets,
max_output_size,
decoded_stats.total_match_bytes,
);
profiler.profile.sequence_execute += execute_start.elapsed();
if let Ok(execute_profile) = &result {
profiler.profile.sequence_execute_literal_copy += execute_profile.literal_copy;
profiler.profile.sequence_execute_prefix_match_copy +=
execute_profile.prefix_match_copy;
profiler.profile.sequence_execute_dictionary_match_copy +=
execute_profile.dictionary_match_copy;
}
result.map(|_| ())
}
}
pub(crate) fn append_bytes(
out: &mut DecodeOut<'_>,
bytes: &[u8],
limit: OutputLimit,
) -> Result<()> {
let new_len = checked_output_len(out.len(), bytes.len())?;
limit.check_total(new_len)?;
out.append(bytes)
}
pub(crate) fn append_repeated(
out: &mut DecodeOut<'_>,
byte: u8,
count: usize,
limit: OutputLimit,
) -> Result<()> {
let new_len = checked_output_len(out.len(), count)?;
limit.check_total(new_len)?;
out.append_repeated(byte, count)
}
fn ensure_total_size_limit(
current_output_size: usize,
content_size: u64,
max_output_size: Option<usize>,
) -> Result<()> {
if let Some(limit) = max_output_size {
let total = (current_output_size as u64)
.checked_add(content_size)
.ok_or(Error::OutputSizeOverflow)?;
if total > limit as u64 {
return Err(Error::OutputSizeTooLarge {
output_size: total,
max_output_size: limit,
});
}
}
Ok(())
}
fn checked_output_len(current: usize, add: usize) -> Result<usize> {
current.checked_add(add).ok_or(Error::OutputSizeOverflow)
}
struct Cursor<'a> {
src: &'a [u8],
pos: usize,
}
impl<'a> Cursor<'a> {
fn new(src: &'a [u8]) -> Self {
Self { src, pos: 0 }
}
fn is_empty(&self) -> bool {
self.pos == self.src.len()
}
fn position(&self) -> usize {
self.pos
}
fn remaining_slice(&self) -> &'a [u8] {
&self.src[self.pos..]
}
fn read_u8(&mut self) -> Result<u8> {
let byte = *self.src.get(self.pos).ok_or(Error::UnexpectedEof)?;
self.pos += 1;
Ok(byte)
}
fn read_u32_le(&mut self) -> Result<u32> {
let bytes = self.read_exact(4)?;
Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
}
fn read_exact(&mut self, len: usize) -> Result<&'a [u8]> {
let end = self.pos.checked_add(len).ok_or(Error::UnexpectedEof)?;
let bytes = self.src.get(self.pos..end).ok_or(Error::UnexpectedEof)?;
self.pos = end;
Ok(bytes)
}
}
impl std::fmt::Debug for Decoder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Decoder")
.field("literals_capacity", &self.scratch.literals.capacity())
.field("sequences_capacity", &self.scratch.sequences.capacity())
.finish()
}
}