Skip to content

Commit

Permalink
since this is now both an encoder and decoder the modules need to be …
Browse files Browse the repository at this point in the history
…sorted out a bit
  • Loading branch information
KillingSpark committed Dec 14, 2024
1 parent 1d3f062 commit c5f1785
Show file tree
Hide file tree
Showing 28 changed files with 124 additions and 99 deletions.
2 changes: 1 addition & 1 deletion benches/decode_all.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{criterion_group, criterion_main, Criterion};
use ruzstd::FrameDecoder;
use ruzstd::decoding::frame_decoder::FrameDecoder;

fn criterion_benchmark(c: &mut Criterion) {
let mut fr = FrameDecoder::new();
Expand Down
5 changes: 3 additions & 2 deletions fuzz/fuzz_targets/decode.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
#[macro_use]
extern crate libfuzzer_sys;
extern crate ruzstd;
use std::io::Read;

fuzz_target!(|data: &[u8]| {
if let Ok(mut decoder) = ruzstd::StreamingDecoder::new(data) {
if let Ok(mut decoder) = ruzstd::decoding::streaming_decoder::StreamingDecoder::new(data) {
let mut output = Vec::new();
_ = decoder.read_to_end(&mut output);
}
Expand Down
11 changes: 6 additions & 5 deletions fuzz/fuzz_targets/encode.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
#[macro_use]
extern crate libfuzzer_sys;
extern crate ruzstd;
use ruzstd::encoding::{FrameCompressor, CompressionLevel};
use ruzstd::encoding::frame_compressor::{CompressionLevel, FrameCompressor};

fuzz_target!(|data: &[u8]| {
let mut output = Vec::new();
let mut compressor = FrameCompressor::new(data, &mut output, CompressionLevel::Uncompressed);
compressor.compress();

let mut decoded = Vec::with_capacity(data.len());
let mut decoder = ruzstd::FrameDecoder::new();
let mut decoder = ruzstd::decoding::frame_decoder::FrameDecoder::new();
decoder.decode_all_to_vec(&output, &mut decoded).unwrap();
assert_eq!(data, &decoded);

Expand All @@ -18,7 +19,7 @@ fuzz_target!(|data: &[u8]| {
compressor.compress();

let mut decoded = Vec::with_capacity(data.len());
let mut decoder = ruzstd::FrameDecoder::new();
let mut decoder = ruzstd::decoding::frame_decoder::FrameDecoder::new();
decoder.decode_all_to_vec(&output, &mut decoded).unwrap();
assert_eq!(data, &decoded);
});
});
5 changes: 3 additions & 2 deletions fuzz/fuzz_targets/fse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
#[macro_use]
extern crate libfuzzer_sys;
extern crate ruzstd;
use ruzstd::fse::round_trip;

fuzz_target!(|data: &[u8]| {
round_trip(data);
});
});
5 changes: 3 additions & 2 deletions fuzz/fuzz_targets/huff0.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
#[macro_use]
extern crate libfuzzer_sys;
extern crate ruzstd;
use ruzstd::huff0::round_trip;

fuzz_target!(|data: &[u8]| {
round_trip(data);
});
});
18 changes: 13 additions & 5 deletions fuzz/fuzz_targets/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ extern crate ruzstd;
use std::io::Read;

fn decode_ruzstd(data: &mut dyn std::io::Read) -> Vec<u8> {
let mut decoder = ruzstd::StreamingDecoder::new(data).unwrap();
let mut decoder = ruzstd::decoding::streaming_decoder::StreamingDecoder::new(data).unwrap();
let mut result: Vec<u8> = Vec::new();
decoder.read_to_end(&mut result).expect("Decoding failed");
result
}

fn decode_ruzstd_writer(mut data: impl Read) -> Vec<u8> {
let mut decoder = ruzstd::FrameDecoder::new();
let mut decoder = ruzstd::decoding::frame_decoder::FrameDecoder::new();
decoder.reset(&mut data).unwrap();
let mut result = vec![];
while !decoder.is_finished() || decoder.can_collect() > 0 {
decoder
.decode_blocks(
&mut data,
ruzstd::BlockDecodingStrategy::UptoBytes(1024 * 1024),
ruzstd::decoding::frame_decoder::BlockDecodingStrategy::UptoBytes(1024 * 1024),
)
.unwrap();
decoder.collect_to_writer(&mut result).unwrap();
Expand All @@ -35,7 +35,11 @@ fn encode_ruzstd_uncompressed(data: &mut dyn std::io::Read) -> Vec<u8> {
let mut input = Vec::new();
let mut output = Vec::new();
data.read_to_end(&mut input).unwrap();
let mut compressor = ruzstd::encoding::FrameCompressor::new(input.as_slice(), &mut output, ruzstd::encoding::CompressionLevel::Uncompressed);
let mut compressor = ruzstd::encoding::frame_compressor::FrameCompressor::new(
input.as_slice(),
&mut output,
ruzstd::encoding::frame_compressor::CompressionLevel::Uncompressed,
);
compressor.compress();
output
}
Expand All @@ -44,7 +48,11 @@ fn encode_ruzstd_compressed(data: &mut dyn std::io::Read) -> Vec<u8> {
let mut input = Vec::new();
let mut output = Vec::new();
data.read_to_end(&mut input).unwrap();
let mut compressor = ruzstd::encoding::FrameCompressor::new(input.as_slice(), &mut output, ruzstd::encoding::CompressionLevel::Fastest);
let mut compressor = ruzstd::encoding::frame_compressor::FrameCompressor::new(
input.as_slice(),
&mut output,
ruzstd::encoding::frame_compressor::CompressionLevel::Fastest,
);
compressor.compress();
output
}
Expand Down
17 changes: 11 additions & 6 deletions src/bin/zstd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::io::SeekFrom;
use std::io::Write;
use std::time::Instant;

use ruzstd::encoding::CompressionLevel;
use ruzstd::encoding::FrameCompressor;
use ruzstd::frame::ReadFrameHeaderError;
use ruzstd::frame_decoder::FrameDecoderError;
use ruzstd::decoding::frame::ReadFrameHeaderError;
use ruzstd::decoding::frame_decoder::FrameDecoderError;
use ruzstd::encoding::frame_compressor::CompressionLevel;
use ruzstd::encoding::frame_compressor::FrameCompressor;

struct StateTracker {
bytes_used: u64,
Expand Down Expand Up @@ -41,7 +41,7 @@ fn decompress(flags: &[String], file_paths: &[String]) {
return;
}

let mut frame_dec = ruzstd::FrameDecoder::new();
let mut frame_dec = ruzstd::decoding::frame_decoder::FrameDecoder::new();

for path in file_paths {
eprintln!("File: {}", path);
Expand Down Expand Up @@ -79,7 +79,12 @@ fn decompress(flags: &[String], file_paths: &[String]) {

while !frame_dec.is_finished() {
frame_dec
.decode_blocks(&mut f, ruzstd::BlockDecodingStrategy::UptoBytes(batch_size))
.decode_blocks(
&mut f,
ruzstd::decoding::frame_decoder::BlockDecodingStrategy::UptoBytes(
batch_size,
),
)
.unwrap();

if frame_dec.can_collect() > batch_size {
Expand Down
3 changes: 2 additions & 1 deletion src/bin/zstd_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fn main() {
let f = File::open(path).unwrap();
let mut buf_read = std::io::BufReader::new(f);

let mut decoder = ruzstd::StreamingDecoder::new(&mut buf_read).unwrap();
let mut decoder =
ruzstd::decoding::streaming_decoder::StreamingDecoder::new(&mut buf_read).unwrap();
let mut buf = [0u8; 1024 * 1024];
let mut stdout = std::io::stdout();
while !decoder.decoder.is_finished() || decoder.decoder.can_collect() > 0 {
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/literals_section.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Utilities and representations for the first half of a block, the literals section.
//! It contains data that is then copied from by the sequences section.
use super::super::decoding::bit_reader::{BitReader, GetBitsError};
use crate::decoding::bit_reader::BitReader;
use crate::decoding::GetBitsError;

/// A compressed block consists of two sections, a literals section, and a sequences section.
///
Expand Down
15 changes: 2 additions & 13 deletions src/decoding/bit_reader.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
use super::GetBitsError;

/// Interact with a provided source at a bit level.
pub struct BitReader<'s> {
idx: usize, //index counts bits already read
source: &'s [u8],
}

#[derive(Debug)]
#[non_exhaustive]
pub enum GetBitsError {
TooManyBits {
num_requested_bits: usize,
limit: u8,
},
NotEnoughRemainingBits {
requested: usize,
remaining: usize,
},
}

#[cfg(feature = "std")]
impl std::error::Error for GetBitsError {}

Expand Down
4 changes: 1 addition & 3 deletions src/decoding/bit_reader_reverse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::convert::TryInto;

pub use super::bit_reader::GetBitsError;
use crate::io::Read;
use core::convert::TryInto;

/// Zstandard encodes some types of data in a way that the data must be read
/// back to front to decode it properly. `BitReaderReversed` provides a
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/frame_decoder.rs → src/decoding/frame_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Framedecoder is the man struct users interact with to decode zstd frames
//!
//! Zstandard compressed data is made of one or more [Frame]s. Each frame is independent and can be
//! Zstandard compressed data is made of one or more [crate::decoding::frame::Frame]s. Each frame is independent and can be
//! decompressed independently of other frames. This module contains structures
//! and utilities that can be used to decode a frame.
Expand All @@ -26,7 +26,7 @@ use std::error::Error as StdError;
///
/// Workflow is as follows:
/// ```
/// use ruzstd::frame_decoder::BlockDecodingStrategy;
/// use ruzstd::decoding::frame_decoder::BlockDecodingStrategy;
///
/// # #[cfg(feature = "std")]
/// use std::io::{Read, Write};
Expand All @@ -37,7 +37,7 @@ use std::error::Error as StdError;
///
/// fn decode_this(mut file: impl Read) {
/// //Create a new decoder
/// let mut frame_dec = ruzstd::FrameDecoder::new();
/// let mut frame_dec = ruzstd::decoding::frame_decoder::FrameDecoder::new();
/// let mut result = Vec::new();
///
/// // Use reset or init to make the decoder ready to decode the frame from the io::Read
Expand Down
5 changes: 3 additions & 2 deletions src/decoding/literals_section_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! This module contains the [decompress_literals] function, used to take a
//! This module contains the decompress_literals function, used to take a
//! parsed literals header and a source and decompress it.
use super::super::blocks::literals_section::{LiteralsSection, LiteralsSectionType};
use super::bit_reader_reverse::{BitReaderReversed, GetBitsError};
use super::bit_reader_reverse::BitReaderReversed;
use super::scratch::HuffmanScratch;
use super::GetBitsError;
use crate::huff0::{HuffmanDecoder, HuffmanDecoderError, HuffmanTableError};
use alloc::vec::Vec;

Expand Down
16 changes: 16 additions & 0 deletions src/decoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@ pub mod bit_reader_reverse;
pub mod block_decoder;
pub mod decodebuffer;
pub mod dictionary;
pub mod frame;
pub mod frame_decoder;
pub mod literals_section_decoder;
mod ringbuffer;
#[allow(dead_code)]
pub mod scratch;
pub mod sequence_execution;
pub mod sequence_section_decoder;
pub mod streaming_decoder;

#[derive(Debug)]
#[non_exhaustive]
pub enum GetBitsError {
TooManyBits {
num_requested_bits: usize,
limit: u8,
},
NotEnoughRemainingBits {
requested: usize,
remaining: usize,
},
}
3 changes: 2 additions & 1 deletion src/decoding/sequence_section_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::super::blocks::sequence_section::ModeType;
use super::super::blocks::sequence_section::Sequence;
use super::super::blocks::sequence_section::SequencesHeader;
use super::bit_reader_reverse::{BitReaderReversed, GetBitsError};
use super::bit_reader_reverse::BitReaderReversed;
use super::scratch::FSEScratch;
use crate::blocks::sequence_section::{
MAX_LITERAL_LENGTH_CODE, MAX_MATCH_LENGTH_CODE, MAX_OFFSET_CODE,
};
use crate::decoding::GetBitsError;
use crate::fse::{FSEDecoder, FSEDecoderError, FSETableError};
use alloc::vec::Vec;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::borrow::BorrowMut;

use crate::frame_decoder::{BlockDecodingStrategy, FrameDecoder, FrameDecoderError};
use crate::decoding::frame_decoder::{BlockDecodingStrategy, FrameDecoder, FrameDecoderError};
use crate::io::{Error, ErrorKind, Read};

/// High level Zstandard frame decoder that can be used to decompress a given Zstandard frame.
Expand All @@ -19,7 +19,7 @@ use crate::io::{Error, ErrorKind, Read};
///
/// To decode all the frames in a finite stream, the calling code needs to recreate
/// the instance of the decoder and handle
/// [crate::frame::ReadFrameHeaderError::SkipFrame]
/// [crate::decoding::frame::ReadFrameHeaderError::SkipFrame]
/// errors by skipping forward the `length` amount of bytes, see <https://github.com/KillingSpark/zstd-rs/issues/57>
///
/// ```no_run
Expand All @@ -28,7 +28,7 @@ use crate::io::{Error, ErrorKind, Read};
/// {
/// use std::fs::File;
/// use std::io::Read;
/// use ruzstd::{StreamingDecoder};
/// use ruzstd::decoding::streaming_decoder::StreamingDecoder;
///
/// // Read a Zstandard archive from the filesystem then decompress it into a vec.
/// let mut f: File = todo!("Read a .zstd archive from somewhere");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum CompressionLevel {
///
/// # Examples
/// ```
/// use ruzstd::encoding::{FrameCompressor, CompressionLevel};
/// use ruzstd::encoding::frame_compressor::{FrameCompressor, CompressionLevel};
/// let mock_data: &[_] = &[0x1, 0x2, 0x3, 0x4];
/// let mut output = std::vec::Vec::new();
/// // Initialize a compressor.
Expand Down Expand Up @@ -190,7 +190,7 @@ mod tests {
use alloc::vec;

use super::FrameCompressor;
use crate::{frame::MAGIC_NUM, FrameDecoder};
use crate::decoding::{frame::MAGIC_NUM, frame_decoder::FrameDecoder};
use alloc::vec::Vec;

#[test]
Expand Down Expand Up @@ -286,21 +286,24 @@ mod tests {
fn fuzz_targets() {
use std::io::Read;
fn decode_ruzstd(data: &mut dyn std::io::Read) -> Vec<u8> {
let mut decoder = crate::StreamingDecoder::new(data).unwrap();
let mut decoder =
crate::decoding::streaming_decoder::StreamingDecoder::new(data).unwrap();
let mut result: Vec<u8> = Vec::new();
decoder.read_to_end(&mut result).expect("Decoding failed");
result
}

fn decode_ruzstd_writer(mut data: impl Read) -> Vec<u8> {
let mut decoder = crate::FrameDecoder::new();
let mut decoder = crate::decoding::frame_decoder::FrameDecoder::new();
decoder.reset(&mut data).unwrap();
let mut result = vec![];
while !decoder.is_finished() || decoder.can_collect() > 0 {
decoder
.decode_blocks(
&mut data,
crate::BlockDecodingStrategy::UptoBytes(1024 * 1024),
crate::decoding::frame_decoder::BlockDecodingStrategy::UptoBytes(
1024 * 1024,
),
)
.unwrap();
decoder.collect_to_writer(&mut result).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/encoding/frame_header.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Utilities and representations for a frame header.
use crate::decoding::frame;
use crate::encoding::{
bit_writer::BitWriter,
util::{find_min_size, minify_val},
};
use crate::frame;
use alloc::vec::Vec;

/// A header for a single Zstandard frame.
Expand Down Expand Up @@ -162,7 +162,7 @@ fn minify_val_fcs(val: u64) -> Vec<u8> {
#[cfg(test)]
mod tests {
use super::FrameHeader;
use crate::frame::{read_frame_header, FrameDescriptor};
use crate::decoding::frame::{read_frame_header, FrameDescriptor};
use alloc::vec::Vec;

#[test]
Expand Down
Loading

0 comments on commit c5f1785

Please sign in to comment.