Coverage Report

Created: 2026-11-12 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zstd-0.14.3/src/stream/mod.rs
Line
Count
Source
1
//! Compress and decompress Zstd streams.
2
//!
3
//! Zstd streams are the main way to compress and decompress data.
4
//! They are compatible with the `zstd` command-line tool.
5
//!
6
//! This module provides both `Read` and `Write` interfaces to compressing and
7
//! decompressing.
8
9
pub mod read;
10
pub mod write;
11
12
mod functions;
13
pub mod zio;
14
15
#[cfg(test)]
16
mod tests;
17
18
pub mod raw;
19
20
pub use self::functions::{
21
    compress_file, compress_file_into, compress_sized, compress_sized_into,
22
    copy_decode, copy_encode, decode_all, decompressed_size, encode_all,
23
};
24
pub use self::read::Decoder;
25
pub use self::write::{AutoFinishEncoder, Encoder};
26
27
#[doc(hidden)]
28
#[macro_export]
29
/// Common functions for the decoder, both in read and write mode.
30
macro_rules! decoder_parameters {
31
    () => {
32
        /// Sets the maximum back-reference distance.
33
        ///
34
        /// The actual maximum distance is going to be `2^log_distance`.
35
        ///
36
        /// This will need to at least match the value set when compressing.
37
13
        pub fn window_log_max(&mut self, log_distance: u32) -> io::Result<()> {
38
13
            self.set_parameter(zstd_safe::DParameter::WindowLogMax(
39
13
                log_distance,
40
13
            ))
41
13
        }
<zstd::stream::read::Decoder<std::io::buffered::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>::window_log_max
Line
Count
Source
37
5
        pub fn window_log_max(&mut self, log_distance: u32) -> io::Result<()> {
38
5
            self.set_parameter(zstd_safe::DParameter::WindowLogMax(
39
5
                log_distance,
40
5
            ))
41
5
        }
Unexecuted instantiation: <zstd::bulk::decompressor::Decompressor>::window_log_max
Unexecuted instantiation: <zstd::stream::read::Decoder<_>>::window_log_max
Unexecuted instantiation: <zstd::stream::write::Decoder<_>>::window_log_max
<zstd::stream::write::Decoder<suricata_htp::decompressors::BlockingCursor>>::window_log_max
Line
Count
Source
37
8
        pub fn window_log_max(&mut self, log_distance: u32) -> io::Result<()> {
38
8
            self.set_parameter(zstd_safe::DParameter::WindowLogMax(
39
8
                log_distance,
40
8
            ))
41
8
        }
42
43
        #[cfg(feature = "experimental")]
44
        #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
45
        /// Enables or disabled expecting the 4-byte magic header
46
        ///
47
        /// Only available with the `experimental` feature.
48
        ///
49
        /// This will need to match the settings used when compressing.
50
        pub fn include_magicbytes(
51
            &mut self,
52
            include_magicbytes: bool,
53
        ) -> io::Result<()> {
54
            self.set_parameter(zstd_safe::DParameter::Format(
55
                if include_magicbytes {
56
                    zstd_safe::FrameFormat::One
57
                } else {
58
                    zstd_safe::FrameFormat::Magicless
59
                },
60
            ))
61
        }
62
    };
63
}
64
65
#[doc(hidden)]
66
#[macro_export]
67
/// Common functions for the decoder, both in read and write mode.
68
macro_rules! decoder_common {
69
    ($readwrite:ident) => {
70
        /// Sets a decompression parameter on the decompression stream.
71
13
        pub fn set_parameter(
72
13
            &mut self,
73
13
            parameter: zstd_safe::DParameter,
74
13
        ) -> io::Result<()> {
75
13
            self.$readwrite.operation_mut().set_parameter(parameter)
76
13
        }
<zstd::stream::read::Decoder<std::io::buffered::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>::set_parameter
Line
Count
Source
71
5
        pub fn set_parameter(
72
5
            &mut self,
73
5
            parameter: zstd_safe::DParameter,
74
5
        ) -> io::Result<()> {
75
5
            self.$readwrite.operation_mut().set_parameter(parameter)
76
5
        }
Unexecuted instantiation: <zstd::stream::read::Decoder<_>>::set_parameter
Unexecuted instantiation: <zstd::stream::write::Decoder<_>>::set_parameter
<zstd::stream::write::Decoder<suricata_htp::decompressors::BlockingCursor>>::set_parameter
Line
Count
Source
71
8
        pub fn set_parameter(
72
8
            &mut self,
73
8
            parameter: zstd_safe::DParameter,
74
8
        ) -> io::Result<()> {
75
8
            self.$readwrite.operation_mut().set_parameter(parameter)
76
8
        }
77
78
        $crate::decoder_parameters!();
79
    };
80
}
81
82
#[doc(hidden)]
83
#[macro_export]
84
/// Parameter-setters for the encoder. Relies on a `set_parameter` method.
85
macro_rules! encoder_parameters {
86
    () => {
87
        /// Controls whether zstd should include a content checksum at the end
88
        /// of each frame.
89
0
        pub fn include_checksum(
90
0
            &mut self,
91
0
            include_checksum: bool,
92
0
        ) -> io::Result<()> {
93
0
            self.set_parameter(zstd_safe::CParameter::ChecksumFlag(
94
0
                include_checksum,
95
0
            ))
96
0
        }
Unexecuted instantiation: <zstd::bulk::compressor::Compressor>::include_checksum
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::include_checksum
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::include_checksum
97
98
        /// Enables multithreaded compression
99
        ///
100
        /// * If `n_workers == 0` (default), then multithreaded will be
101
        ///   disabled.
102
        /// * If `n_workers >= 1`, then compression will be done in separate
103
        ///   threads.
104
        ///
105
        /// So even `n_workers = 1` may increase performance by separating
106
        /// IO and compression.
107
        ///
108
        /// Note: This is only available if the `zstdmt` cargo feature is activated.
109
        #[cfg(feature = "zstdmt")]
110
        #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "zstdmt")))]
111
        pub fn multithread(&mut self, n_workers: u32) -> io::Result<()> {
112
            self.set_parameter(zstd_safe::CParameter::NbWorkers(n_workers))
113
        }
114
115
        /// Enables or disables storing of the dict id.
116
        ///
117
        /// Defaults to true. If false, the behaviour of decoding with a wrong
118
        /// dictionary is undefined.
119
0
        pub fn include_dictid(
120
0
            &mut self,
121
0
            include_dictid: bool,
122
0
        ) -> io::Result<()> {
123
0
            self.set_parameter(zstd_safe::CParameter::DictIdFlag(
124
0
                include_dictid,
125
0
            ))
126
0
        }
Unexecuted instantiation: <zstd::bulk::compressor::Compressor>::include_dictid
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::include_dictid
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::include_dictid
127
128
        /// Enables or disabled storing of the contentsize.
129
        ///
130
        /// Note that this only has an effect if the size is given with `set_pledged_src_size`.
131
0
        pub fn include_contentsize(
132
0
            &mut self,
133
0
            include_contentsize: bool,
134
0
        ) -> io::Result<()> {
135
0
            self.set_parameter(zstd_safe::CParameter::ContentSizeFlag(
136
0
                include_contentsize,
137
0
            ))
138
0
        }
Unexecuted instantiation: <zstd::bulk::compressor::Compressor>::include_contentsize
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::include_contentsize
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::include_contentsize
139
        /// Enables or disables long-distance matching
140
0
        pub fn long_distance_matching(
141
0
            &mut self,
142
0
            long_distance_matching: bool,
143
0
        ) -> io::Result<()> {
144
0
            self.set_parameter(
145
0
                zstd_safe::CParameter::EnableLongDistanceMatching(
146
0
                    long_distance_matching,
147
0
                ),
148
            )
149
0
        }
Unexecuted instantiation: <zstd::bulk::compressor::Compressor>::long_distance_matching
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::long_distance_matching
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::long_distance_matching
150
151
        /// Sets the target size for compressed blocks.
152
        ///
153
        /// A lower block size may result in slightly lower speed (~2%) and compression ratio
154
        /// (~0.1%), but may decrease end-to-end latency in low-bandwidth environments (time to
155
        /// first decompressed byte).
156
        ///
157
        /// No value, or a value of zero, results in no constraint for the block sizes.
158
0
        pub fn set_target_cblock_size(
159
0
            &mut self,
160
0
            target_size: Option<u32>,
161
0
        ) -> io::Result<()> {
162
0
            self.set_parameter(zstd_safe::CParameter::TargetCBlockSize(
163
0
                target_size.unwrap_or(0),
164
0
            ))
165
0
        }
Unexecuted instantiation: <zstd::bulk::compressor::Compressor>::set_target_cblock_size
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::set_target_cblock_size
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::set_target_cblock_size
166
167
        /// Sets the maximum back-reference distance.
168
        ///
169
        /// The actual maximum distance is going to be `2^log_distance`.
170
        ///
171
        /// Note that decompression will need to use at least the same setting.
172
0
        pub fn window_log(&mut self, log_distance: u32) -> io::Result<()> {
173
0
            self.set_parameter(zstd_safe::CParameter::WindowLog(log_distance))
174
0
        }
Unexecuted instantiation: <zstd::bulk::compressor::Compressor>::window_log
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::window_log
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::window_log
175
176
        #[cfg(feature = "experimental")]
177
        #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
178
        /// Enables or disable the magic bytes at the beginning of each frame.
179
        ///
180
        /// If disabled, include_magicbytes must also be called on the decoder.
181
        ///
182
        /// Only available with the `experimental` feature.
183
        ///
184
        /// Note that decompression will need to use the same setting.
185
        pub fn include_magicbytes(
186
            &mut self,
187
            include_magicbytes: bool,
188
        ) -> io::Result<()> {
189
            self.set_parameter(zstd_safe::CParameter::Format(
190
                if include_magicbytes {
191
                    zstd_safe::FrameFormat::One
192
                } else {
193
                    zstd_safe::FrameFormat::Magicless
194
                },
195
            ))
196
        }
197
    };
198
}
199
200
#[doc(hidden)]
201
#[macro_export]
202
/// Common functions for the encoder, both in read and write mode.
203
macro_rules! encoder_common {
204
    ($readwrite:ident) => {
205
        /// Sets the given zstd compression parameter.
206
0
        pub fn set_parameter(
207
0
            &mut self,
208
0
            parameter: zstd_safe::CParameter,
209
0
        ) -> io::Result<()> {
210
0
            self.$readwrite.operation_mut().set_parameter(parameter)
211
0
        }
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::set_parameter
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::set_parameter
212
213
        /// Sets the expected size of the input.
214
        ///
215
        /// This affects the compression effectiveness.
216
        ///
217
        /// It is an error to give an incorrect size (an error will be returned when closing the
218
        /// stream if the size does not match what was pledged).
219
        ///
220
        /// Giving a `None` size means the size is unknown (this is the default).
221
0
        pub fn set_pledged_src_size(
222
0
            &mut self,
223
0
            size: Option<u64>,
224
0
        ) -> io::Result<()> {
225
0
            self.$readwrite.operation_mut().set_pledged_src_size(size)
226
0
        }
Unexecuted instantiation: <zstd::stream::read::Encoder<_>>::set_pledged_src_size
Unexecuted instantiation: <zstd::stream::write::Encoder<_>>::set_pledged_src_size
227
228
        $crate::encoder_parameters!();
229
    };
230
}