Skip to content

Commit acc59f5

Browse files
authored
Move bitwriter/reader into common module and consolidate/cleanup constants
1 parent 805dfc5 commit acc59f5

29 files changed

+209
-181
lines changed
Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
1-
use super::errors::GetBitsError;
2-
3-
/// Interact with a provided source at a bit level.
1+
/// Wraps a slice and enables reading arbitrary amounts of bits
2+
/// from that slice.
43
pub struct BitReader<'s> {
54
idx: usize, //index counts bits already read
65
source: &'s [u8],
76
}
87

9-
#[cfg(feature = "std")]
10-
impl std::error::Error for GetBitsError {}
11-
12-
impl core::fmt::Display for GetBitsError {
13-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
14-
match self {
15-
GetBitsError::TooManyBits {
16-
num_requested_bits,
17-
limit,
18-
} => {
19-
write!(
20-
f,
21-
"Cant serve this request. The reader is limited to {} bits, requested {} bits",
22-
limit, num_requested_bits,
23-
)
24-
}
25-
GetBitsError::NotEnoughRemainingBits {
26-
requested,
27-
remaining,
28-
} => {
29-
write!(
30-
f,
31-
"Can\'t read {} bits, only have {} bits left",
32-
requested, remaining,
33-
)
34-
}
35-
}
36-
}
37-
}
38-
398
impl<'s> BitReader<'s> {
409
pub fn new(source: &'s [u8]) -> BitReader<'s> {
4110
BitReader { idx: 0, source }
@@ -121,3 +90,46 @@ impl<'s> BitReader<'s> {
12190
Ok(value)
12291
}
12392
}
93+
94+
#[derive(Debug)]
95+
#[non_exhaustive]
96+
pub enum GetBitsError {
97+
TooManyBits {
98+
num_requested_bits: usize,
99+
limit: u8,
100+
},
101+
NotEnoughRemainingBits {
102+
requested: usize,
103+
remaining: usize,
104+
},
105+
}
106+
107+
#[cfg(feature = "std")]
108+
impl std::error::Error for GetBitsError {}
109+
110+
impl core::fmt::Display for GetBitsError {
111+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
112+
match self {
113+
GetBitsError::TooManyBits {
114+
num_requested_bits,
115+
limit,
116+
} => {
117+
write!(
118+
f,
119+
"Cant serve this request. The reader is limited to {} bits, requested {} bits",
120+
limit, num_requested_bits,
121+
)
122+
}
123+
GetBitsError::NotEnoughRemainingBits {
124+
requested,
125+
remaining,
126+
} => {
127+
write!(
128+
f,
129+
"Can\'t read {} bits, only have {} bits left",
130+
requested, remaining,
131+
)
132+
}
133+
}
134+
}
135+
}

src/bit_io/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Encoding agnostic ways to read and write binary data
2+
3+
mod bit_reader;
4+
mod bit_reader_reverse;
5+
mod bit_writer;
6+
7+
pub(crate) use bit_reader::*;
8+
pub(crate) use bit_reader_reverse::*;
9+
pub(crate) use bit_writer::*;

src/blocks/literals_section.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Utilities and representations for the first half of a block, the literals section.
22
//! It contains data that is then copied from by the sequences section.
3-
use crate::decoding::bit_reader::BitReader;
3+
use crate::bit_io::BitReader;
44
use crate::decoding::errors::LiteralsSectionParseError;
55

66
/// A compressed block consists of two sections, a literals section, and a sequences section.

src/common/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Values and interfaces shared between the encoding side
2+
//! and the decoding side.
3+
4+
// --- FRAMES ---
5+
/// This magic number is included at the start of a single Zstandard frame
6+
pub const MAGIC_NUM: u32 = 0xFD2F_B528;
7+
/// Window size refers to the minimum amount of memory needed to decode any given frame.
8+
///
9+
/// The minimum window size is defined as 1 KB
10+
pub const MIN_WINDOW_SIZE: u64 = 1024;
11+
/// Window size refers to the minimum amount of memory needed to decode any given frame.
12+
///
13+
/// The maximum window size allowed by the spec is 3.75TB
14+
pub const MAX_WINDOW_SIZE: u64 = (1 << 41) + 7 * (1 << 38);
15+
16+
// --- BLOCKS ---
17+
/// While the spec limits block size to 128KB, the implementation uses
18+
/// 128kibibytes
19+
///
20+
/// <https://github.com/facebook/zstd/blob/eca205fc7849a61ab287492931a04960ac58e031/doc/educational_decoder/zstd_decompress.c#L28-L29>
21+
pub const MAX_BLOCK_SIZE: u32 = 128 * 1024;

src/decoding/block_decoder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::super::blocks::literals_section::LiteralsSectionType;
55
use super::super::blocks::sequence_section::SequencesHeader;
66
use super::literals_section_decoder::decode_literals;
77
use super::sequence_section_decoder::decode_sequences;
8+
use crate::common::MAX_BLOCK_SIZE;
89
use crate::decoding::errors::DecodeSequenceError;
910
use crate::decoding::errors::{
1011
BlockHeaderReadError, BlockSizeError, BlockTypeError, DecodeBlockContentError,
@@ -34,8 +35,6 @@ pub fn new() -> BlockDecoder {
3435
}
3536
}
3637

37-
pub(crate) const ABSOLUTE_MAXIMUM_BLOCK_SIZE: u32 = 128 * 1024;
38-
3938
impl BlockDecoder {
4039
pub fn decode_block_content(
4140
&mut self,
@@ -296,7 +295,7 @@ impl BlockDecoder {
296295

297296
fn block_content_size(&self) -> Result<u32, BlockSizeError> {
298297
let val = self.block_content_size_unchecked();
299-
if val > ABSOLUTE_MAXIMUM_BLOCK_SIZE {
298+
if val > MAX_BLOCK_SIZE {
300299
Err(BlockSizeError::BlockSizeTooLarge { size: val })
301300
} else {
302301
Ok(val)

src/decoding/errors.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Errors that might occur while decoding zstd formatted data
22
3+
use crate::bit_io::GetBitsError;
34
use crate::blocks::block::BlockType;
45
use crate::blocks::literals_section::LiteralsSectionType;
56
use crate::io::Error;
@@ -8,19 +9,6 @@ use core::fmt;
89
#[cfg(feature = "std")]
910
use std::error::Error as StdError;
1011

11-
#[derive(Debug)]
12-
#[non_exhaustive]
13-
pub enum GetBitsError {
14-
TooManyBits {
15-
num_requested_bits: usize,
16-
limit: u8,
17-
},
18-
NotEnoughRemainingBits {
19-
requested: usize,
20-
remaining: usize,
21-
},
22-
}
23-
2412
#[derive(Debug)]
2513
#[non_exhaustive]
2614
pub enum FrameDescriptorError {
@@ -61,13 +49,13 @@ impl fmt::Display for FrameHeaderError {
6149
f,
6250
"window_size bigger than allowed maximum. Is: {}, Should be lower than: {}",
6351
got,
64-
crate::decoding::frame::MAX_WINDOW_SIZE
52+
crate::common::MAX_WINDOW_SIZE
6553
),
6654
Self::WindowTooSmall { got } => write!(
6755
f,
6856
"window_size smaller than allowed minimum. Is: {}, Should be greater than: {}",
6957
got,
70-
crate::decoding::frame::MIN_WINDOW_SIZE
58+
crate::common::MIN_WINDOW_SIZE
7159
),
7260
Self::FrameDescriptorError(e) => write!(f, "{:?}", e),
7361
Self::DictIdTooSmall { got, expected } => write!(
@@ -261,7 +249,7 @@ impl core::fmt::Display for BlockSizeError {
261249
write!(
262250
f,
263251
"Blocksize was bigger than the absolute maximum {} (128kb). Is: {}",
264-
crate::decoding::block_decoder::ABSOLUTE_MAXIMUM_BLOCK_SIZE,
252+
crate::common::MAX_BLOCK_SIZE,
265253
size,
266254
)
267255
}
@@ -532,7 +520,7 @@ impl core::fmt::Display for FrameDecoderError {
532520
f,
533521
"Specified window_size is too big; Requested: {}, Max: {}",
534522
requested,
535-
crate::decoding::frame::MAX_WINDOW_SIZE,
523+
crate::common::MAX_WINDOW_SIZE,
536524
)
537525
}
538526
FrameDecoderError::DictionaryDecodeError(e) => {

0 commit comments

Comments
 (0)