Respect deadline in miniz (#200)

This commit is contained in:
Ingvar Stepanyan 2020-04-01 04:31:17 +01:00 committed by GitHub
parent 878bbedb37
commit f747269151
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View file

@ -3,12 +3,13 @@ use crate::error::PngError;
use crate::PngResult; use crate::PngResult;
use miniz_oxide::deflate::core::*; use miniz_oxide::deflate::core::*;
pub fn compress_to_vec_oxipng( pub(crate) fn compress_to_vec_oxipng(
input: &[u8], input: &[u8],
level: u8, level: u8,
window_bits: i32, window_bits: i32,
strategy: i32, strategy: i32,
max_size: &AtomicMin, max_size: &AtomicMin,
deadline: &crate::Deadline,
) -> PngResult<Vec<u8>> { ) -> PngResult<Vec<u8>> {
// The comp flags function sets the zlib flag if the window_bits parameter is > 0. // The comp flags function sets the zlib flag if the window_bits parameter is > 0.
let flags = create_comp_flags_from_zip_params(level.into(), window_bits, strategy); let flags = create_comp_flags_from_zip_params(level.into(), window_bits, strategy);
@ -45,6 +46,9 @@ pub fn compress_to_vec_oxipng(
return Err(PngError::DeflatedDataTooLong(output.len())); return Err(PngError::DeflatedDataTooLong(output.len()));
} }
} }
if deadline.passed() {
return Err(PngError::TimedOut);
}
// We need more space, so extend the vector. // We need more space, so extend the vector.
if output.len().saturating_sub(out_pos) < 30 { if output.len().saturating_sub(out_pos) < 30 {
let current_len = output.len(); let current_len = output.len();

View file

@ -44,7 +44,7 @@ pub(crate) fn deflate(
} }
} }
miniz_stream::compress_to_vec_oxipng(data, zc, zw.into(), zs.into(), max_size) miniz_stream::compress_to_vec_oxipng(data, zc, zw.into(), zs.into(), max_size, deadline)
} }
pub fn zopfli_deflate(data: &[u8]) -> PngResult<Vec<u8>> { pub fn zopfli_deflate(data: &[u8]) -> PngResult<Vec<u8>> {