Skip to content

Commit c839711

Browse files
authoredFeb 10, 2025··
Don't leak flate2 as an implementation detail by default (#564)
1 parent 466bdae commit c839711

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed
 

‎Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ include = [
2424
bitflags = "2.0"
2525
crc32fast = "1.2.0"
2626
fdeflate = "0.3.3"
27-
flate2 = "1.0.11"
27+
flate2 = "1.0.35"
2828
miniz_oxide = { version = "0.8", features = ["simd"] }
2929

3030
[dev-dependencies]
@@ -38,7 +38,14 @@ rand = "0.8.4"
3838
term = "1.0.1"
3939

4040
[features]
41+
# Use nightly-only features for a minor performance boost in PNG decoding
4142
unstable = ["crc32fast/nightly"]
43+
# Use zlib-rs for faster PNG encoding at the cost of some `unsafe` code.
44+
# WARNING: this changes the flate2 backend for your entire dependency tree!
45+
# While the `png` crate always uses fully memory-safe decoding,
46+
# this enables zlib-rs and introduces some unsafe code to all other crates
47+
# that rely on flate2, including the decoding codepaths.
48+
zlib-rs = ["flate2/zlib-rs"]
4249
benchmarks = []
4350

4451
[lints.rust]

‎fuzz/fuzz_targets/roundtrip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn encode_png<'a>(width: u8, filter: u8, compression: u8, color_type: u8, raw_bi
3030
// compression
3131
let compression = match compression {
3232
0 => png::DeflateCompression::NoCompression,
33-
level @ 1..=9 => png::DeflateCompression::Flate2(level),
33+
level @ 1..=9 => png::DeflateCompression::Level(level),
3434
10 => png::DeflateCompression::FdeflateUltraFast,
3535
_ => return None,
3636
};

‎src/common.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,15 @@ pub enum DeflateCompression {
378378
/// while still providing a decent compression ratio.
379379
FdeflateUltraFast,
380380

381-
/// Uses [flate2](https://crates.io/crates/flate2) crate with the specified [compression level](flate2::Compression::new).
381+
/// Compression level between 1 and 9, where higher values mean better compression at the cost of
382+
/// speed.
382383
///
383-
/// Flate2 has several backends that make different trade-offs.
384-
/// See the flate2 documentation for the available backends for more information.
385-
Flate2(u8),
384+
/// This is currently implemented via [flate2](https://crates.io/crates/flate2) crate
385+
/// by passing through the [compression level](flate2::Compression::new).
386+
///
387+
/// The implementation details and the exact meaning of each level may change in the future,
388+
/// including in semver-compatible releases.
389+
Level(u8),
386390
// Other variants can be added in the future
387391
}
388392

@@ -398,16 +402,16 @@ impl DeflateCompression {
398402
Compression::NoCompression => Self::NoCompression,
399403
Compression::Fastest => Self::FdeflateUltraFast,
400404
Compression::Fast => Self::FdeflateUltraFast,
401-
Compression::Balanced => Self::Flate2(flate2::Compression::default().level() as u8),
402-
Compression::High => Self::Flate2(flate2::Compression::best().level() as u8),
405+
Compression::Balanced => Self::Level(flate2::Compression::default().level() as u8),
406+
Compression::High => Self::Level(flate2::Compression::best().level() as u8),
403407
}
404408
}
405409

406410
pub(crate) fn closest_flate2_level(&self) -> flate2::Compression {
407411
match self {
408412
DeflateCompression::NoCompression => flate2::Compression::none(),
409413
DeflateCompression::FdeflateUltraFast => flate2::Compression::new(1),
410-
DeflateCompression::Flate2(level) => flate2::Compression::new(u32::from(*level)),
414+
DeflateCompression::Level(level) => flate2::Compression::new(u32::from(*level)),
411415
}
412416
}
413417
}

‎src/encoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ impl<W: Write> Writer<W> {
825825
compressed
826826
}
827827
}
828-
DeflateCompression::Flate2(level) => {
828+
DeflateCompression::Level(level) => {
829829
let mut current = vec![0; in_len];
830830

831831
let mut zlib =
@@ -1774,7 +1774,7 @@ mod tests {
17741774
let mut buf = vec![0; reader.output_buffer_size()];
17751775
let info = reader.next_frame(&mut buf).unwrap();
17761776
use DeflateCompression::*;
1777-
for compression in [NoCompression, FdeflateUltraFast, Flate2(4)] {
1777+
for compression in [NoCompression, FdeflateUltraFast, Level(4)] {
17781778
// Encode decoded image
17791779
let mut out = Vec::new();
17801780
{
@@ -1832,7 +1832,7 @@ mod tests {
18321832
let mut buf = vec![0; reader.output_buffer_size()];
18331833
let info = reader.next_frame(&mut buf).unwrap();
18341834
use DeflateCompression::*;
1835-
for compression in [NoCompression, FdeflateUltraFast, Flate2(4)] {
1835+
for compression in [NoCompression, FdeflateUltraFast, Level(4)] {
18361836
// Encode decoded image
18371837
let mut out = Vec::new();
18381838
{

0 commit comments

Comments
 (0)
Please sign in to comment.