summaryrefslogtreecommitdiffstats
path: root/vendor/ruzstd/src/decoding
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ruzstd/src/decoding')
-rw-r--r--vendor/ruzstd/src/decoding/block_decoder.rs42
-rw-r--r--vendor/ruzstd/src/decoding/decodebuffer.rs41
-rw-r--r--vendor/ruzstd/src/decoding/dictionary.rs3
-rw-r--r--vendor/ruzstd/src/decoding/literals_section_decoder.rs5
-rw-r--r--vendor/ruzstd/src/decoding/ringbuffer.rs19
-rw-r--r--vendor/ruzstd/src/decoding/scratch.rs38
-rw-r--r--vendor/ruzstd/src/decoding/sequence_execution.rs2
-rw-r--r--vendor/ruzstd/src/decoding/sequence_section_decoder.rs68
8 files changed, 94 insertions, 124 deletions
diff --git a/vendor/ruzstd/src/decoding/block_decoder.rs b/vendor/ruzstd/src/decoding/block_decoder.rs
index 11a4c28c1..0fbce8f48 100644
--- a/vendor/ruzstd/src/decoding/block_decoder.rs
+++ b/vendor/ruzstd/src/decoding/block_decoder.rs
@@ -11,7 +11,7 @@ use crate::blocks::literals_section::LiteralsSectionParseError;
use crate::blocks::sequence_section::SequencesHeaderParseError;
use crate::decoding::scratch::DecoderScratch;
use crate::decoding::sequence_execution::execute_sequences;
-use std::io::{self, Read};
+use crate::io::{self, Read};
pub struct BlockDecoder {
header_buffer: [u8; 3],
@@ -203,12 +203,12 @@ impl BlockDecoder {
let mut section = LiteralsSection::new();
let bytes_in_literals_header = section.parse_from_header(raw)?;
let raw = &raw[bytes_in_literals_header as usize..];
- if crate::VERBOSE {
- println!(
- "Found {} literalssection with regenerated size: {}, and compressed size: {:?}",
- section.ls_type, section.regenerated_size, section.compressed_size
- );
- }
+ vprintln!(
+ "Found {} literalssection with regenerated size: {}, and compressed size: {:?}",
+ section.ls_type,
+ section.regenerated_size,
+ section.compressed_size
+ );
let upper_limit_for_literals = match section.compressed_size {
Some(x) => x as usize,
@@ -227,9 +227,7 @@ impl BlockDecoder {
}
let raw_literals = &raw[..upper_limit_for_literals];
- if crate::VERBOSE {
- println!("Slice for literals: {}", raw_literals.len());
- }
+ vprintln!("Slice for literals: {}", raw_literals.len());
workspace.literals_buffer.clear(); //all literals of the previous block must have been used in the sequence execution anyways. just be defensive here
let bytes_used_in_literals_section = decode_literals(
@@ -247,20 +245,16 @@ impl BlockDecoder {
assert!(bytes_used_in_literals_section == upper_limit_for_literals as u32);
let raw = &raw[upper_limit_for_literals..];
- if crate::VERBOSE {
- println!("Slice for sequences with headers: {}", raw.len());
- }
+ vprintln!("Slice for sequences with headers: {}", raw.len());
let mut seq_section = SequencesHeader::new();
let bytes_in_sequence_header = seq_section.parse_from_header(raw)?;
let raw = &raw[bytes_in_sequence_header as usize..];
- if crate::VERBOSE {
- println!(
- "Found sequencessection with sequences: {} and size: {}",
- seq_section.num_sequences,
- raw.len()
- );
- }
+ vprintln!(
+ "Found sequencessection with sequences: {} and size: {}",
+ seq_section.num_sequences,
+ raw.len()
+ );
assert!(
u32::from(bytes_in_literals_header)
@@ -269,9 +263,7 @@ impl BlockDecoder {
+ raw.len() as u32
== header.content_size
);
- if crate::VERBOSE {
- println!("Slice for sequences: {}", raw.len());
- }
+ vprintln!("Slice for sequences: {}", raw.len());
if seq_section.num_sequences != 0 {
decode_sequences(
@@ -280,9 +272,7 @@ impl BlockDecoder {
&mut workspace.fse,
&mut workspace.sequences,
)?;
- if crate::VERBOSE {
- println!("Executing sequences");
- }
+ vprintln!("Executing sequences");
execute_sequences(workspace)?;
} else {
workspace.buffer.push(&workspace.literals_buffer);
diff --git a/vendor/ruzstd/src/decoding/decodebuffer.rs b/vendor/ruzstd/src/decoding/decodebuffer.rs
index 0dea7015e..33cc58c30 100644
--- a/vendor/ruzstd/src/decoding/decodebuffer.rs
+++ b/vendor/ruzstd/src/decoding/decodebuffer.rs
@@ -1,5 +1,6 @@
-use std::hash::Hasher;
-use std::io;
+use crate::io::{Error, Read, Write};
+use alloc::vec::Vec;
+use core::hash::Hasher;
use twox_hash::XxHash64;
@@ -23,8 +24,8 @@ pub enum DecodebufferError {
OffsetTooBig { offset: usize, buf_len: usize },
}
-impl io::Read for Decodebuffer {
- fn read(&mut self, target: &mut [u8]) -> io::Result<usize> {
+impl Read for Decodebuffer {
+ fn read(&mut self, target: &mut [u8]) -> Result<usize, Error> {
let max_amount = self.can_drain_to_window_size().unwrap_or(0);
let amount = max_amount.min(target.len());
@@ -176,7 +177,7 @@ impl Decodebuffer {
}
}
- pub fn drain_to_window_size_writer(&mut self, mut sink: impl io::Write) -> io::Result<usize> {
+ pub fn drain_to_window_size_writer(&mut self, mut sink: impl Write) -> Result<usize, Error> {
match self.can_drain_to_window_size() {
None => Ok(0),
Some(can_drain) => {
@@ -199,14 +200,14 @@ impl Decodebuffer {
vec
}
- pub fn drain_to_writer(&mut self, mut sink: impl io::Write) -> io::Result<usize> {
+ pub fn drain_to_writer(&mut self, mut sink: impl Write) -> Result<usize, Error> {
let len = self.buffer.len();
self.drain_to(len, |buf| write_all_bytes(&mut sink, buf))?;
Ok(len)
}
- pub fn read_all(&mut self, target: &mut [u8]) -> io::Result<usize> {
+ pub fn read_all(&mut self, target: &mut [u8]) -> Result<usize, Error> {
let amount = self.buffer.len().min(target.len());
let mut written = 0;
@@ -224,8 +225,8 @@ impl Decodebuffer {
fn drain_to(
&mut self,
amount: usize,
- mut write_bytes: impl FnMut(&[u8]) -> (usize, io::Result<()>),
- ) -> io::Result<()> {
+ mut write_bytes: impl FnMut(&[u8]) -> (usize, Result<(), Error>),
+ ) -> Result<(), Error> {
if amount == 0 {
return Ok(());
}
@@ -280,7 +281,7 @@ impl Decodebuffer {
}
/// Like Write::write_all but returns partial write length even on error
-fn write_all_bytes(mut sink: impl io::Write, buf: &[u8]) -> (usize, io::Result<()>) {
+fn write_all_bytes(mut sink: impl Write, buf: &[u8]) -> (usize, Result<(), Error>) {
let mut written = 0;
while written < buf.len() {
match sink.write(&buf[written..]) {
@@ -294,7 +295,11 @@ fn write_all_bytes(mut sink: impl io::Write, buf: &[u8]) -> (usize, io::Result<(
#[cfg(test)]
mod tests {
use super::Decodebuffer;
- use std::io::Write;
+ use crate::io::{Error, ErrorKind, Write};
+
+ extern crate std;
+ use alloc::vec;
+ use alloc::vec::Vec;
#[test]
fn short_writer() {
@@ -304,7 +309,7 @@ mod tests {
}
impl Write for ShortWriter {
- fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> {
+ fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, Error> {
if buf.len() > self.write_len {
self.buf.extend_from_slice(&buf[..self.write_len]);
Ok(self.write_len)
@@ -314,7 +319,7 @@ mod tests {
}
}
- fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
+ fn flush(&mut self) -> std::result::Result<(), Error> {
Ok(())
}
}
@@ -352,18 +357,18 @@ mod tests {
}
impl Write for WouldblockWriter {
- fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> {
+ fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, Error> {
if self.last_blocked < self.block_every {
self.buf.extend_from_slice(buf);
self.last_blocked += 1;
Ok(buf.len())
} else {
self.last_blocked = 0;
- Err(std::io::Error::from(std::io::ErrorKind::WouldBlock))
+ Err(Error::from(ErrorKind::WouldBlock))
}
}
- fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
+ fn flush(&mut self) -> std::result::Result<(), Error> {
Ok(())
}
}
@@ -390,7 +395,7 @@ mod tests {
}
}
Err(e) => {
- if e.kind() == std::io::ErrorKind::WouldBlock {
+ if e.kind() == ErrorKind::WouldBlock {
continue;
} else {
panic!("Unexpected error {:?}", e);
@@ -410,7 +415,7 @@ mod tests {
}
}
Err(e) => {
- if e.kind() == std::io::ErrorKind::WouldBlock {
+ if e.kind() == ErrorKind::WouldBlock {
continue;
} else {
panic!("Unexpected error {:?}", e);
diff --git a/vendor/ruzstd/src/decoding/dictionary.rs b/vendor/ruzstd/src/decoding/dictionary.rs
index 51fbcdf0b..aa6769371 100644
--- a/vendor/ruzstd/src/decoding/dictionary.rs
+++ b/vendor/ruzstd/src/decoding/dictionary.rs
@@ -1,4 +1,5 @@
-use std::convert::TryInto;
+use alloc::vec::Vec;
+use core::convert::TryInto;
use crate::decoding::scratch::FSEScratch;
use crate::decoding::scratch::HuffmanScratch;
diff --git a/vendor/ruzstd/src/decoding/literals_section_decoder.rs b/vendor/ruzstd/src/decoding/literals_section_decoder.rs
index d947f87eb..bd7fb18ea 100644
--- a/vendor/ruzstd/src/decoding/literals_section_decoder.rs
+++ b/vendor/ruzstd/src/decoding/literals_section_decoder.rs
@@ -2,6 +2,7 @@ use super::super::blocks::literals_section::{LiteralsSection, LiteralsSectionTyp
use super::bit_reader_reverse::{BitReaderReversed, GetBitsError};
use super::scratch::HuffmanScratch;
use crate::huff0::{HuffmanDecoder, HuffmanDecoderError, HuffmanTableError};
+use alloc::vec::Vec;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
@@ -75,9 +76,7 @@ fn decompress_literals(
LiteralsSectionType::Compressed => {
//read Huffman tree description
bytes_read += scratch.table.build_decoder(source)?;
- if crate::VERBOSE {
- println!("Built huffman table using {} bytes", bytes_read);
- }
+ vprintln!("Built huffman table using {} bytes", bytes_read);
}
LiteralsSectionType::Treeless => {
if scratch.table.max_num_bits == 0 {
diff --git a/vendor/ruzstd/src/decoding/ringbuffer.rs b/vendor/ruzstd/src/decoding/ringbuffer.rs
index 9e3e9ba5e..fc9a2e330 100644
--- a/vendor/ruzstd/src/decoding/ringbuffer.rs
+++ b/vendor/ruzstd/src/decoding/ringbuffer.rs
@@ -1,4 +1,5 @@
-use std::{alloc::Layout, ptr::NonNull, slice};
+use alloc::alloc::{alloc, dealloc};
+use core::{alloc::Layout, ptr::NonNull, slice};
pub struct RingBuffer {
buf: NonNull<u8>,
@@ -70,7 +71,7 @@ impl RingBuffer {
// alloc the new memory region and panic if alloc fails
// TODO maybe rework this to generate an error?
let new_buf = unsafe {
- let new_buf = std::alloc::alloc(new_layout);
+ let new_buf = alloc(new_layout);
NonNull::new(new_buf).expect("Allocating new space for the ringbuffer failed")
};
@@ -85,7 +86,7 @@ impl RingBuffer {
.as_ptr()
.add(s1_len)
.copy_from_nonoverlapping(s2_ptr, s2_len);
- std::alloc::dealloc(self.buf.as_ptr(), current_layout);
+ dealloc(self.buf.as_ptr(), current_layout);
}
self.tail = s1_len + s2_len;
@@ -341,7 +342,7 @@ impl Drop for RingBuffer {
let current_layout = unsafe { Layout::array::<u8>(self.cap).unwrap_unchecked() };
unsafe {
- std::alloc::dealloc(self.buf.as_ptr(), current_layout);
+ dealloc(self.buf.as_ptr(), current_layout);
}
}
}
@@ -448,8 +449,8 @@ unsafe fn copy_with_nobranch_check(
f1_ptr.copy_from_nonoverlapping(m1_ptr, m1_in_f1);
f2_ptr.copy_from_nonoverlapping(m1_ptr.add(m1_in_f1), m1_in_f2);
}
- 6 => std::hint::unreachable_unchecked(),
- 7 => std::hint::unreachable_unchecked(),
+ 6 => core::hint::unreachable_unchecked(),
+ 7 => core::hint::unreachable_unchecked(),
9 => {
f1_ptr.copy_from_nonoverlapping(m1_ptr, m1_in_f1);
f2_ptr.copy_from_nonoverlapping(m2_ptr, m2_in_f2);
@@ -480,9 +481,9 @@ unsafe fn copy_with_nobranch_check(
.add(m1_in_f2)
.copy_from_nonoverlapping(m2_ptr, m2_in_f2);
}
- 14 => std::hint::unreachable_unchecked(),
- 15 => std::hint::unreachable_unchecked(),
- _ => std::hint::unreachable_unchecked(),
+ 14 => core::hint::unreachable_unchecked(),
+ 15 => core::hint::unreachable_unchecked(),
+ _ => core::hint::unreachable_unchecked(),
}
}
diff --git a/vendor/ruzstd/src/decoding/scratch.rs b/vendor/ruzstd/src/decoding/scratch.rs
index 2bd753bde..35d5c61ef 100644
--- a/vendor/ruzstd/src/decoding/scratch.rs
+++ b/vendor/ruzstd/src/decoding/scratch.rs
@@ -3,6 +3,7 @@ use super::decodebuffer::Decodebuffer;
use crate::decoding::dictionary::Dictionary;
use crate::fse::FSETable;
use crate::huff0::HuffmanTable;
+use alloc::vec::Vec;
pub struct DecoderScratch {
pub huf: HuffmanScratch,
@@ -56,30 +57,17 @@ impl DecoderScratch {
self.huf.table.reset();
}
- pub fn use_dict(&mut self, dict: &Dictionary) {
- self.fse = dict.fse.clone();
- self.huf = dict.huf.clone();
+ pub fn init_from_dict(&mut self, dict: &Dictionary) {
+ self.fse.reinit_from(&dict.fse);
+ self.huf.table.reinit_from(&dict.huf.table);
self.offset_hist = dict.offset_hist;
- self.buffer.dict_content = dict.dict_content.clone();
- }
-
- /// parses the dictionary and set the tables
- /// it returns the dict_id for checking with the frame's dict_id
- pub fn load_dict(
- &mut self,
- raw: &[u8],
- ) -> Result<u32, super::dictionary::DictionaryDecodeError> {
- let dict = super::dictionary::Dictionary::decode_dict(raw)?;
-
- self.huf = dict.huf.clone();
- self.fse = dict.fse.clone();
- self.offset_hist = dict.offset_hist;
- self.buffer.dict_content = dict.dict_content.clone();
- Ok(dict.id)
+ self.buffer.dict_content.clear();
+ self.buffer
+ .dict_content
+ .extend_from_slice(&dict.dict_content);
}
}
-#[derive(Clone)]
pub struct HuffmanScratch {
pub table: HuffmanTable,
}
@@ -98,7 +86,6 @@ impl Default for HuffmanScratch {
}
}
-#[derive(Clone)]
pub struct FSEScratch {
pub offsets: FSETable,
pub of_rle: Option<u8>,
@@ -119,6 +106,15 @@ impl FSEScratch {
ml_rle: None,
}
}
+
+ pub fn reinit_from(&mut self, other: &Self) {
+ self.offsets.reinit_from(&other.offsets);
+ self.literal_lengths.reinit_from(&other.literal_lengths);
+ self.match_lengths.reinit_from(&other.match_lengths);
+ self.of_rle = other.of_rle;
+ self.ll_rle = other.ll_rle;
+ self.ml_rle = other.ml_rle;
+ }
}
impl Default for FSEScratch {
diff --git a/vendor/ruzstd/src/decoding/sequence_execution.rs b/vendor/ruzstd/src/decoding/sequence_execution.rs
index 19247ec62..5946df18c 100644
--- a/vendor/ruzstd/src/decoding/sequence_execution.rs
+++ b/vendor/ruzstd/src/decoding/sequence_execution.rs
@@ -18,8 +18,6 @@ pub fn execute_sequences(scratch: &mut DecoderScratch) -> Result<(), ExecuteSequ
for idx in 0..scratch.sequences.len() {
let seq = scratch.sequences[idx];
- if crate::VERBOSE {}
- //println!("{}: {}", idx, seq);
if seq.ll > 0 {
let high = literals_copy_counter + seq.ll as usize;
diff --git a/vendor/ruzstd/src/decoding/sequence_section_decoder.rs b/vendor/ruzstd/src/decoding/sequence_section_decoder.rs
index 3d5990c05..6c366fba1 100644
--- a/vendor/ruzstd/src/decoding/sequence_section_decoder.rs
+++ b/vendor/ruzstd/src/decoding/sequence_section_decoder.rs
@@ -4,6 +4,7 @@ use super::super::blocks::sequence_section::SequencesHeader;
use super::bit_reader_reverse::{BitReaderReversed, GetBitsError};
use super::scratch::FSEScratch;
use crate::fse::{FSEDecoder, FSEDecoderError, FSETableError};
+use alloc::vec::Vec;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
@@ -42,9 +43,7 @@ pub fn decode_sequences(
) -> Result<(), DecodeSequenceError> {
let bytes_read = maybe_update_fse_tables(section, source, scratch)?;
- if crate::VERBOSE {
- println!("Updating tables used {} bytes", bytes_read);
- }
+ vprintln!("Updating tables used {} bytes", bytes_read);
let bit_stream = &source[bytes_read..];
@@ -319,16 +318,13 @@ fn maybe_update_fse_tables(
ModeType::FSECompressed => {
let bytes = scratch.literal_lengths.build_decoder(source, LL_MAX_LOG)?;
bytes_read += bytes;
- if crate::VERBOSE {
- println!("Updating ll table");
- println!("Used bytes: {}", bytes);
- }
+
+ vprintln!("Updating ll table");
+ vprintln!("Used bytes: {}", bytes);
scratch.ll_rle = None;
}
ModeType::RLE => {
- if crate::VERBOSE {
- println!("Use RLE ll table");
- }
+ vprintln!("Use RLE ll table");
if source.is_empty() {
return Err(DecodeSequenceError::MissingByteForRleLlTable);
}
@@ -336,9 +332,7 @@ fn maybe_update_fse_tables(
scratch.ll_rle = Some(source[0]);
}
ModeType::Predefined => {
- if crate::VERBOSE {
- println!("Use predefined ll table");
- }
+ vprintln!("Use predefined ll table");
scratch.literal_lengths.build_from_probabilities(
LL_DEFAULT_ACC_LOG,
&Vec::from(&LITERALS_LENGTH_DEFAULT_DISTRIBUTION[..]),
@@ -346,9 +340,7 @@ fn maybe_update_fse_tables(
scratch.ll_rle = None;
}
ModeType::Repeat => {
- if crate::VERBOSE {
- println!("Repeat ll table");
- }
+ vprintln!("Repeat ll table");
/* Nothing to do */
}
};
@@ -358,17 +350,13 @@ fn maybe_update_fse_tables(
match modes.of_mode() {
ModeType::FSECompressed => {
let bytes = scratch.offsets.build_decoder(of_source, OF_MAX_LOG)?;
- if crate::VERBOSE {
- println!("Updating of table");
- println!("Used bytes: {}", bytes);
- }
+ vprintln!("Updating of table");
+ vprintln!("Used bytes: {}", bytes);
bytes_read += bytes;
scratch.of_rle = None;
}
ModeType::RLE => {
- if crate::VERBOSE {
- println!("Use RLE of table");
- }
+ vprintln!("Use RLE of table");
if of_source.is_empty() {
return Err(DecodeSequenceError::MissingByteForRleOfTable);
}
@@ -376,9 +364,7 @@ fn maybe_update_fse_tables(
scratch.of_rle = Some(of_source[0]);
}
ModeType::Predefined => {
- if crate::VERBOSE {
- println!("Use predefined of table");
- }
+ vprintln!("Use predefined of table");
scratch.offsets.build_from_probabilities(
OF_DEFAULT_ACC_LOG,
&Vec::from(&OFFSET_DEFAULT_DISTRIBUTION[..]),
@@ -386,9 +372,7 @@ fn maybe_update_fse_tables(
scratch.of_rle = None;
}
ModeType::Repeat => {
- if crate::VERBOSE {
- println!("Repeat of table");
- }
+ vprintln!("Repeat of table");
/* Nothing to do */
}
};
@@ -399,16 +383,12 @@ fn maybe_update_fse_tables(
ModeType::FSECompressed => {
let bytes = scratch.match_lengths.build_decoder(ml_source, ML_MAX_LOG)?;
bytes_read += bytes;
- if crate::VERBOSE {
- println!("Updating ml table");
- println!("Used bytes: {}", bytes);
- }
+ vprintln!("Updating ml table");
+ vprintln!("Used bytes: {}", bytes);
scratch.ml_rle = None;
}
ModeType::RLE => {
- if crate::VERBOSE {
- println!("Use RLE ml table");
- }
+ vprintln!("Use RLE ml table");
if ml_source.is_empty() {
return Err(DecodeSequenceError::MissingByteForRleMlTable);
}
@@ -416,9 +396,7 @@ fn maybe_update_fse_tables(
scratch.ml_rle = Some(ml_source[0]);
}
ModeType::Predefined => {
- if crate::VERBOSE {
- println!("Use predefined ml table");
- }
+ vprintln!("Use predefined ml table");
scratch.match_lengths.build_from_probabilities(
ML_DEFAULT_ACC_LOG,
&Vec::from(&MATCH_LENGTH_DEFAULT_DISTRIBUTION[..]),
@@ -426,9 +404,7 @@ fn maybe_update_fse_tables(
scratch.ml_rle = None;
}
ModeType::Repeat => {
- if crate::VERBOSE {
- println!("Repeat ml table");
- }
+ vprintln!("Repeat ml table");
/* Nothing to do */
}
};
@@ -463,10 +439,14 @@ fn test_ll_default() {
)
.unwrap();
+ #[cfg(feature = "std")]
for idx in 0..table.decode.len() {
- println!(
+ std::println!(
"{:3}: {:3} {:3} {:3}",
- idx, table.decode[idx].symbol, table.decode[idx].num_bits, table.decode[idx].base_line
+ idx,
+ table.decode[idx].symbol,
+ table.decode[idx].num_bits,
+ table.decode[idx].base_line
);
}