Allocate sufficient space for libdefate

This commit is contained in:
Andrew 2022-10-22 11:23:45 +13:00 committed by Josh Holmer
parent 248c961191
commit d5cfec8cfc

View file

@ -4,7 +4,11 @@ use libdeflater::{CompressionError, CompressionLvl, Compressor};
pub fn deflate(data: &[u8], level: u8, max_size: &AtomicMin) -> PngResult<Vec<u8>> {
let mut compressor = Compressor::new(CompressionLvl::new(level.into()).unwrap());
let capacity = max_size.get().unwrap_or(data.len() / 2);
// If adhering to a max_size we need to include at least 9 extra bytes of slack space (as specified in docs).
let capacity = max_size
.get()
.unwrap_or_else(|| compressor.zlib_compress_bound(data.len()))
+ 9;
let mut dest = vec![0; capacity];
let len = compressor
.zlib_compress(data, &mut dest)